]> git.ipfire.org Git - thirdparty/glibc.git/blob - elf/tst-stackguard1.c
8a8e4719d6b3541f7c104685b816af4ca0c4f2e7
[thirdparty/glibc.git] / elf / tst-stackguard1.c
1 /* Copyright (C) 2005-2019 Free Software Foundation, Inc.
2 This file is part of the GNU C Library.
3 Contributed by Jakub Jelinek <jakub@redhat.com>, 2005.
4
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
10 The GNU C Library is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 Lesser General Public License for more details.
14
15 You should have received a copy of the GNU Lesser General Public
16 License along with the GNU C Library; if not, see
17 <http://www.gnu.org/licenses/>. */
18
19 #include <errno.h>
20 #include <getopt.h>
21 #include <stdbool.h>
22 #include <stdio.h>
23 #include <stdlib.h>
24 #include <string.h>
25 #include <sys/wait.h>
26 #include <stackguard-macros.h>
27 #include <tls.h>
28 #include <unistd.h>
29
30 static const char *command;
31 static bool child;
32 static uintptr_t stack_chk_guard_copy;
33 static bool stack_chk_guard_copy_set;
34 static int fds[2];
35
36 static void __attribute__ ((constructor))
37 con (void)
38 {
39 stack_chk_guard_copy = STACK_CHK_GUARD;
40 stack_chk_guard_copy_set = true;
41 }
42
43 static int
44 uintptr_t_cmp (const void *a, const void *b)
45 {
46 if (*(uintptr_t *) a < *(uintptr_t *) b)
47 return 1;
48 if (*(uintptr_t *) a > *(uintptr_t *) b)
49 return -1;
50 return 0;
51 }
52
53 static int
54 do_test (void)
55 {
56 if (!stack_chk_guard_copy_set)
57 {
58 puts ("constructor has not been run");
59 return 1;
60 }
61
62 if (stack_chk_guard_copy != STACK_CHK_GUARD)
63 {
64 puts ("STACK_CHK_GUARD changed between constructor and do_test");
65 return 1;
66 }
67
68 if (child)
69 {
70 write (2, &stack_chk_guard_copy, sizeof (stack_chk_guard_copy));
71 return 0;
72 }
73
74 if (command == NULL)
75 {
76 puts ("missing --command or --child argument");
77 return 1;
78 }
79
80 #define N 16
81 uintptr_t child_stack_chk_guards[N + 1];
82 child_stack_chk_guards[N] = stack_chk_guard_copy;
83 int i;
84 for (i = 0; i < N; ++i)
85 {
86 if (pipe (fds) < 0)
87 {
88 printf ("couldn't create pipe: %m\n");
89 return 1;
90 }
91
92 pid_t pid = fork ();
93 if (pid < 0)
94 {
95 printf ("fork failed: %m\n");
96 return 1;
97 }
98
99 if (!pid)
100 {
101 if (stack_chk_guard_copy != STACK_CHK_GUARD)
102 {
103 puts ("STACK_CHK_GUARD changed after fork");
104 exit (1);
105 }
106
107 close (fds[0]);
108 close (2);
109 dup2 (fds[1], 2);
110 close (fds[1]);
111
112 system (command);
113 exit (0);
114 }
115
116 close (fds[1]);
117
118 if (TEMP_FAILURE_RETRY (read (fds[0], &child_stack_chk_guards[i],
119 sizeof (uintptr_t))) != sizeof (uintptr_t))
120 {
121 puts ("could not read stack_chk_guard value from child");
122 return 1;
123 }
124
125 close (fds[0]);
126
127 pid_t termpid;
128 int status;
129 termpid = TEMP_FAILURE_RETRY (waitpid (pid, &status, 0));
130 if (termpid == -1)
131 {
132 printf ("waitpid failed: %m\n");
133 return 1;
134 }
135 else if (termpid != pid)
136 {
137 printf ("waitpid returned %ld != %ld\n",
138 (long int) termpid, (long int) pid);
139 return 1;
140 }
141 else if (!WIFEXITED (status) || WEXITSTATUS (status))
142 {
143 puts ("child hasn't exited with exit status 0");
144 return 1;
145 }
146 }
147
148 qsort (child_stack_chk_guards, N + 1, sizeof (uintptr_t), uintptr_t_cmp);
149
150 uintptr_t default_guard = 0;
151 unsigned char *p = (unsigned char *) &default_guard;
152 p[sizeof (uintptr_t) - 1] = 255;
153 p[sizeof (uintptr_t) - 2] = '\n';
154 p[0] = 0;
155
156 /* Test if the stack guard canaries are either randomized,
157 or equal to the default stack guard canary value.
158 Even with randomized stack guards it might happen
159 that the random number generator generates the same
160 values, but if that happens in more than half from
161 the 16 runs, something is very wrong. */
162 int ndifferences = 0;
163 int ndefaults = 0;
164 for (i = 0; i < N; ++i)
165 {
166 if (child_stack_chk_guards[i] != child_stack_chk_guards[i+1])
167 ndifferences++;
168 else if (child_stack_chk_guards[i] == default_guard)
169 ndefaults++;
170 }
171
172 printf ("differences %d defaults %d\n", ndifferences, ndefaults);
173
174 if (ndifferences < N / 2 && ndefaults < N / 2)
175 {
176 puts ("stack guard canaries are not randomized enough");
177 puts ("nor equal to the default canary value");
178 return 1;
179 }
180
181 return 0;
182 }
183
184 #define OPT_COMMAND 10000
185 #define OPT_CHILD 10001
186 #define CMDLINE_OPTIONS \
187 { "command", required_argument, NULL, OPT_COMMAND }, \
188 { "child", no_argument, NULL, OPT_CHILD },
189
190 static void __attribute__((used))
191 cmdline_process_function (int c)
192 {
193 switch (c)
194 {
195 case OPT_COMMAND:
196 command = optarg;
197 break;
198 case OPT_CHILD:
199 child = true;
200 break;
201 }
202 }
203 #define CMDLINE_PROCESS cmdline_process_function
204
205 #include <support/test-driver.c>