tst-sprintf-fortify-unchecked.c 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126
  1. /* Tests for fortified sprintf with unknown buffer bounds (bug 30039).
  2. Copyright (C) 2023-2026 Free Software Foundation, Inc.
  3. This file is part of the GNU C Library.
  4. The GNU C Library is free software; you can redistribute it and/or
  5. modify it under the terms of the GNU Lesser General Public
  6. License as published by the Free Software Foundation; either
  7. version 2.1 of the License, or (at your option) any later version.
  8. The GNU C Library is distributed in the hope that it will be useful,
  9. but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  11. Lesser General Public License for more details.
  12. You should have received a copy of the GNU Lesser General Public
  13. License along with the GNU C Library; if not, see
  14. <https://www.gnu.org/licenses/>. */
  15. #include <printf.h>
  16. #include <stdarg.h>
  17. #include <stdio.h>
  18. #include <string.h>
  19. #include <support/check.h>
  20. /* This test is not built with _FORTIFY_SOURCE. Instead it calls the
  21. appropriate implementation directly. The fortify mode is specified
  22. in this variable. */
  23. static int fortify_mode;
  24. /* This does not handle long-double redirects etc., but we test only
  25. format strings that stay within the confines of the base
  26. implementation. */
  27. int __vsprintf_chk (char *s, int flag, size_t slen, const char *format,
  28. va_list ap);
  29. /* Invoke vsprintf or __vsprintf_chk according to fortify_mode. */
  30. static int
  31. my_vsprintf (char *buf, const char *format, va_list ap)
  32. {
  33. int result;
  34. if (fortify_mode == 0)
  35. result = vsprintf (buf, format, ap);
  36. else
  37. /* Call the fortified version with an unspecified length. */
  38. result = __vsprintf_chk (buf, fortify_mode - 1, -1, format, ap);
  39. return result;
  40. }
  41. /* Run one test, with the specified expected output. */
  42. static void __attribute ((format (printf, 2, 3)))
  43. do_check (const char *expected, const char *format, ...)
  44. {
  45. va_list ap;
  46. va_start (ap, format);
  47. char buf_expected[24];
  48. memset (buf_expected, '@', sizeof (buf_expected));
  49. TEST_VERIFY (strlen (expected) < sizeof (buf_expected));
  50. strcpy (buf_expected, expected);
  51. char buf[sizeof (buf_expected)];
  52. memset (buf, '@', sizeof (buf));
  53. int ret = my_vsprintf (buf, format, ap);
  54. TEST_COMPARE_BLOB (buf_expected, sizeof (buf_expected), buf, sizeof (buf));
  55. TEST_COMPARE (ret, strlen (expected));
  56. va_end (ap);
  57. }
  58. /* Run the tests in all fortify modes. */
  59. static void
  60. do_tests (void)
  61. {
  62. for (fortify_mode = 0; fortify_mode <= 3; ++fortify_mode)
  63. {
  64. do_check ("0", "%d", 0);
  65. do_check ("-2147483648", "%d", -2147483647 - 1);
  66. do_check ("-9223372036854775808", "%lld", -9223372036854775807LL - 1);
  67. do_check ("", "%s", "");
  68. do_check (" ", "%22s", "");
  69. do_check ("XXXXXXXXXXXXXXXXXXXXXX", "%s", "XXXXXXXXXXXXXXXXXXXXXX");
  70. do_check ("1.125000", "%f", 1.125);
  71. do_check ("1.125", "%g", 1.125);
  72. do_check ("1.125", "%.8g", 1.125);
  73. }
  74. }
  75. /* printf callback that falls back to the glibc-supplied
  76. implementation. */
  77. static int
  78. dummy_printf_function (FILE *__stream,
  79. const struct printf_info *__info,
  80. const void *const *__args)
  81. {
  82. return -2; /* Request fallback. */
  83. }
  84. /* Likewise for the type information. */
  85. static int
  86. dummy_arginfo_function (const struct printf_info *info,
  87. size_t n, int *argtypes, int *size)
  88. {
  89. return -1; /* Request fallback. */
  90. }
  91. static int
  92. do_test (void)
  93. {
  94. do_tests ();
  95. /* Activate __printf_function_invoke mode. */
  96. register_printf_specifier ('d', dummy_printf_function,
  97. dummy_arginfo_function);
  98. register_printf_specifier ('g', dummy_printf_function,
  99. dummy_arginfo_function);
  100. register_printf_specifier ('s', dummy_printf_function,
  101. dummy_arginfo_function);
  102. /* Rerun the tests with callback functions. */
  103. do_tests ();
  104. return 0;
  105. }
  106. #include <support/test-driver.c>