1 | /* Fortify tests for syslog interface. |
2 | Copyright (C) 2023-2024 Free Software Foundation, Inc. |
3 | Copyright The GNU Toolchain Authors. |
4 | This file is part of the GNU C Library. |
5 | |
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. |
10 | |
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. |
15 | |
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/>. */ |
19 | |
20 | #include <stdarg.h> |
21 | #include <setjmp.h> |
22 | #include <syslog.h> |
23 | #include <string.h> |
24 | #include <unistd.h> |
25 | |
26 | #include <support/check.h> |
27 | #include <support/support.h> |
28 | #include <support/capture_subprocess.h> |
29 | |
30 | static const char *str2 = "F" ; |
31 | static char buf2[10] = "%s" ; |
32 | |
33 | static volatile int chk_fail_ok; |
34 | static jmp_buf chk_fail_buf; |
35 | |
36 | static void |
37 | handler (int sig) |
38 | { |
39 | if (chk_fail_ok) |
40 | { |
41 | chk_fail_ok = 0; |
42 | longjmp (chk_fail_buf, 1); |
43 | } |
44 | else |
45 | _exit (127); |
46 | } |
47 | |
48 | #define CHK_FAIL_START \ |
49 | chk_fail_ok = 1; \ |
50 | if (! setjmp (chk_fail_buf)) \ |
51 | { |
52 | #define CHK_FAIL_END \ |
53 | chk_fail_ok = 0; \ |
54 | FAIL ("not supposed to reach here"); \ |
55 | } |
56 | |
57 | static void |
58 | call_vsyslog (int priority, const char *format, ...) |
59 | { |
60 | va_list va; |
61 | va_start (va, format); |
62 | vsyslog (pri: priority, fmt: format, ap: va); |
63 | va_end (va); |
64 | } |
65 | |
66 | static void |
67 | run_syslog_chk (void *closure) |
68 | { |
69 | int n1; |
70 | CHK_FAIL_START |
71 | syslog (LOG_USER | LOG_DEBUG, buf2, str2, &n1, str2, &n1); |
72 | CHK_FAIL_END |
73 | } |
74 | |
75 | static void |
76 | run_vsyslog_chk (void *closure) |
77 | { |
78 | int n1; |
79 | CHK_FAIL_START |
80 | call_vsyslog (LOG_USER | LOG_DEBUG, format: buf2, str2, &n1, str2, &n1); |
81 | CHK_FAIL_END |
82 | } |
83 | |
84 | static int |
85 | do_test (void) |
86 | { |
87 | set_fortify_handler (handler); |
88 | |
89 | int n1, n2; |
90 | |
91 | n1 = n2 = 0; |
92 | syslog (LOG_USER | LOG_DEBUG, "%s%n%s%n" , str2, &n1, str2, &n2); |
93 | TEST_COMPARE (n1, 1); |
94 | TEST_COMPARE (n2, 2); |
95 | |
96 | n1 = n2 = 0; |
97 | call_vsyslog (LOG_USER | LOG_DEBUG, format: "%s%n%s%n" , str2, &n1, str2, &n2); |
98 | TEST_COMPARE (n1, 1); |
99 | TEST_COMPARE (n2, 2); |
100 | |
101 | strcpy (buf2 + 2, "%n%s%n" ); |
102 | |
103 | /* The wrapper tests need to be in a subprocess because the abort called by |
104 | printf does not unlock the internal syslog lock. */ |
105 | { |
106 | struct support_capture_subprocess result |
107 | = support_capture_subprocess (callback: run_syslog_chk, NULL); |
108 | support_capture_subprocess_check (&result, context: "syslog" , status_or_signal: 0, allowed: sc_allow_stderr); |
109 | support_capture_subprocess_free (&result); |
110 | } |
111 | |
112 | { |
113 | struct support_capture_subprocess result |
114 | = support_capture_subprocess (callback: run_vsyslog_chk, NULL); |
115 | support_capture_subprocess_check (&result, context: "syslog" , status_or_signal: 0, allowed: sc_allow_stderr); |
116 | support_capture_subprocess_free (&result); |
117 | } |
118 | |
119 | return 0; |
120 | } |
121 | |
122 | #include <support/test-driver.c> |
123 | |