debug: Add fortify syslog tests
[glibc.git] / debug / tst-fortify-syslog.c
blob26a93d9be805ff3f89c3ff0cf61c91cabf302ef7
1 /* Fortify tests for syslog interface.
2 Copyright (C) 2023 Free Software Foundation, Inc.
3 Copyright The GNU Toolchain Authors.
4 This file is part of the GNU C Library.
6 The GNU C Library is free software; you can redistribute it and/or
7 modify it under the terms of the GNU Lesser General Public
8 License as published by the Free Software Foundation; either
9 version 2.1 of the License, or (at your option) any later version.
11 The GNU C Library is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 Lesser General Public License for more details.
16 You should have received a copy of the GNU Lesser General Public
17 License along with the GNU C Library; if not, see
18 <https://www.gnu.org/licenses/>. */
20 #include <stdarg.h>
21 #include <setjmp.h>
22 #include <syslog.h>
23 #include <string.h>
24 #include <unistd.h>
25 #include <stdio.h>
27 #include <support/check.h>
28 #include <support/support.h>
29 #include <support/capture_subprocess.h>
31 static const char *str2 = "F";
32 static char buf2[10] = "%s";
34 static volatile int chk_fail_ok;
35 static jmp_buf chk_fail_buf;
37 static void
38 handler (int sig)
40 if (chk_fail_ok)
42 chk_fail_ok = 0;
43 longjmp (chk_fail_buf, 1);
45 else
46 _exit (127);
49 #define FAIL() \
50 do { \
51 printf ("Failure on line %d\n", __LINE__); \
52 support_record_failure (); \
53 } while (0)
54 #define CHK_FAIL_START \
55 chk_fail_ok = 1; \
56 if (! setjmp (chk_fail_buf)) \
58 #define CHK_FAIL_END \
59 chk_fail_ok = 0; \
60 FAIL (); \
63 static void
64 call_vsyslog (int priority, const char *format, ...)
66 va_list va;
67 va_start (va, format);
68 vsyslog (priority, format, va);
69 va_end (va);
72 static void
73 run_syslog_chk (void *closure)
75 int n1;
76 CHK_FAIL_START
77 syslog (LOG_USER | LOG_DEBUG, buf2, str2, &n1, str2, &n1);
78 CHK_FAIL_END
81 static void
82 run_vsyslog_chk (void *closure)
84 int n1;
85 CHK_FAIL_START
86 call_vsyslog (LOG_USER | LOG_DEBUG, buf2, str2, &n1, str2, &n1);
87 CHK_FAIL_END
90 static int
91 do_test (void)
93 set_fortify_handler (handler);
95 int n1, n2;
97 n1 = n2 = 0;
98 syslog (LOG_USER | LOG_DEBUG, "%s%n%s%n", str2, &n1, str2, &n2);
99 TEST_COMPARE (n1, 1);
100 TEST_COMPARE (n2, 2);
102 n1 = n2 = 0;
103 call_vsyslog (LOG_USER | LOG_DEBUG, "%s%n%s%n", str2, &n1, str2, &n2);
104 TEST_COMPARE (n1, 1);
105 TEST_COMPARE (n2, 2);
107 strcpy (buf2 + 2, "%n%s%n");
109 /* The wrapper tests need to be in a subprocess because the abort called by
110 printf does not unlock the internal syslog lock. */
112 struct support_capture_subprocess result
113 = support_capture_subprocess (run_syslog_chk, NULL);
114 support_capture_subprocess_check (&result, "syslog", 0, sc_allow_stderr);
115 support_capture_subprocess_free (&result);
119 struct support_capture_subprocess result
120 = support_capture_subprocess (run_vsyslog_chk, NULL);
121 support_capture_subprocess_check (&result, "syslog", 0, sc_allow_stderr);
122 support_capture_subprocess_free (&result);
125 return 0;
128 #include <support/test-driver.c>