tst-fortify-syslog.c 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122
  1. /* Fortify tests for syslog interface.
  2. Copyright (C) 2023-2026 Free Software Foundation, Inc.
  3. Copyright The GNU Toolchain Authors.
  4. This file is part of the GNU C Library.
  5. The GNU C Library is free software; you can redistribute it and/or
  6. modify it under the terms of the GNU Lesser General Public
  7. License as published by the Free Software Foundation; either
  8. version 2.1 of the License, or (at your option) any later version.
  9. The GNU C Library is distributed in the hope that it will be useful,
  10. but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  12. Lesser General Public License for more details.
  13. You should have received a copy of the GNU Lesser General Public
  14. License along with the GNU C Library; if not, see
  15. <https://www.gnu.org/licenses/>. */
  16. #include <stdarg.h>
  17. #include <setjmp.h>
  18. #include <syslog.h>
  19. #include <string.h>
  20. #include <unistd.h>
  21. #include <support/check.h>
  22. #include <support/support.h>
  23. #include <support/capture_subprocess.h>
  24. static const char *str2 = "F";
  25. static char buf2[10] = "%s";
  26. static volatile int chk_fail_ok;
  27. static jmp_buf chk_fail_buf;
  28. static void
  29. handler (int sig)
  30. {
  31. if (chk_fail_ok)
  32. {
  33. chk_fail_ok = 0;
  34. longjmp (chk_fail_buf, 1);
  35. }
  36. else
  37. _exit (127);
  38. }
  39. #define CHK_FAIL_START \
  40. chk_fail_ok = 1; \
  41. if (! setjmp (chk_fail_buf)) \
  42. {
  43. #define CHK_FAIL_END \
  44. chk_fail_ok = 0; \
  45. FAIL ("not supposed to reach here"); \
  46. }
  47. static void
  48. call_vsyslog (int priority, const char *format, ...)
  49. {
  50. va_list va;
  51. va_start (va, format);
  52. vsyslog (priority, format, va);
  53. va_end (va);
  54. }
  55. static void
  56. run_syslog_chk (void *closure)
  57. {
  58. int n1;
  59. CHK_FAIL_START
  60. syslog (LOG_USER | LOG_DEBUG, buf2, str2, &n1, str2, &n1);
  61. CHK_FAIL_END
  62. }
  63. static void
  64. run_vsyslog_chk (void *closure)
  65. {
  66. int n1;
  67. CHK_FAIL_START
  68. call_vsyslog (LOG_USER | LOG_DEBUG, buf2, str2, &n1, str2, &n1);
  69. CHK_FAIL_END
  70. }
  71. static int
  72. do_test (void)
  73. {
  74. set_fortify_handler (handler);
  75. int n1, n2;
  76. n1 = n2 = 0;
  77. syslog (LOG_USER | LOG_DEBUG, "%s%n%s%n", str2, &n1, str2, &n2);
  78. TEST_COMPARE (n1, 1);
  79. TEST_COMPARE (n2, 2);
  80. n1 = n2 = 0;
  81. call_vsyslog (LOG_USER | LOG_DEBUG, "%s%n%s%n", str2, &n1, str2, &n2);
  82. TEST_COMPARE (n1, 1);
  83. TEST_COMPARE (n2, 2);
  84. strcpy (buf2 + 2, "%n%s%n");
  85. /* The wrapper tests need to be in a subprocess because the abort called by
  86. printf does not unlock the internal syslog lock. */
  87. {
  88. struct support_capture_subprocess result
  89. = support_capture_subprocess (run_syslog_chk, NULL);
  90. support_capture_subprocess_check (&result, "syslog", 0, sc_allow_stderr);
  91. support_capture_subprocess_free (&result);
  92. }
  93. {
  94. struct support_capture_subprocess result
  95. = support_capture_subprocess (run_vsyslog_chk, NULL);
  96. support_capture_subprocess_check (&result, "syslog", 0, sc_allow_stderr);
  97. support_capture_subprocess_free (&result);
  98. }
  99. return 0;
  100. }
  101. #include <support/test-driver.c>