1 /* Copyright (C) 2005-2023 Free Software Foundation, Inc.
2 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.
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.
14 You should have received a copy of the GNU Lesser General Public
15 License along with the GNU C Library; if not, see
16 <https://www.gnu.org/licenses/>. */
26 #include <stackguard-macros.h>
30 #include <support/xunistd.h>
31 #include <support/xstdlib.h>
33 static const char *command
;
35 static uintptr_t stack_chk_guard_copy
;
36 static bool stack_chk_guard_copy_set
;
39 static void __attribute__ ((constructor
))
42 stack_chk_guard_copy
= STACK_CHK_GUARD
;
43 stack_chk_guard_copy_set
= true;
47 uintptr_t_cmp (const void *a
, const void *b
)
49 if (*(uintptr_t *) a
< *(uintptr_t *) b
)
51 if (*(uintptr_t *) a
> *(uintptr_t *) b
)
59 if (stack_chk_guard_copy
!= STACK_CHK_GUARD
)
61 puts ("STACK_CHK_GUARD changed in thread");
70 if (!stack_chk_guard_copy_set
)
72 puts ("constructor has not been run");
76 if (stack_chk_guard_copy
!= STACK_CHK_GUARD
)
78 puts ("STACK_CHK_GUARD changed between constructor and do_test");
87 for (i
= 0; i
< 4; ++i
)
88 if (pthread_create (&th
[i
], NULL
, tf
, NULL
))
90 puts ("thread creation failed");
93 for (i
= 0; i
< 4; ++i
)
94 if (pthread_join (th
[i
], &ret
))
96 puts ("thread join failed");
102 xwrite (2, &stack_chk_guard_copy
, sizeof (stack_chk_guard_copy
));
108 puts ("missing --command or --child argument");
113 uintptr_t child_stack_chk_guards
[N
+ 1];
114 child_stack_chk_guards
[N
] = stack_chk_guard_copy
;
116 for (i
= 0; i
< N
; ++i
)
120 printf ("couldn't create pipe: %m\n");
127 printf ("fork failed: %m\n");
133 if (stack_chk_guard_copy
!= STACK_CHK_GUARD
)
135 puts ("STACK_CHK_GUARD changed after fork");
151 if (TEMP_FAILURE_RETRY (read (fds
[0], &child_stack_chk_guards
[i
],
152 sizeof (uintptr_t))) != sizeof (uintptr_t))
154 puts ("could not read stack_chk_guard value from child");
162 termpid
= TEMP_FAILURE_RETRY (waitpid (pid
, &status
, 0));
165 printf ("waitpid failed: %m\n");
168 else if (termpid
!= pid
)
170 printf ("waitpid returned %ld != %ld\n",
171 (long int) termpid
, (long int) pid
);
174 else if (!WIFEXITED (status
) || WEXITSTATUS (status
))
176 puts ("child hasn't exited with exit status 0");
181 qsort (child_stack_chk_guards
, N
+ 1, sizeof (uintptr_t), uintptr_t_cmp
);
183 uintptr_t default_guard
= 0;
184 unsigned char *p
= (unsigned char *) &default_guard
;
185 p
[sizeof (uintptr_t) - 1] = 255;
186 p
[sizeof (uintptr_t) - 2] = '\n';
189 /* Test if the stack guard canaries are either randomized,
190 or equal to the default stack guard canary value.
191 Even with randomized stack guards it might happen
192 that the random number generator generates the same
193 values, but if that happens in more than half from
194 the 16 runs, something is very wrong. */
195 int ndifferences
= 0;
197 for (i
= 0; i
< N
; ++i
)
199 if (child_stack_chk_guards
[i
] != child_stack_chk_guards
[i
+1])
201 else if (child_stack_chk_guards
[i
] == default_guard
)
205 printf ("differences %d defaults %d\n", ndifferences
, ndefaults
);
207 if (ndifferences
< N
/ 2 && ndefaults
< N
/ 2)
209 puts ("stack guard canaries are not randomized enough");
210 puts ("nor equal to the default canary value");
217 #define OPT_COMMAND 10000
218 #define OPT_CHILD 10001
219 #define CMDLINE_OPTIONS \
220 { "command", required_argument, NULL, OPT_COMMAND }, \
221 { "child", no_argument, NULL, OPT_CHILD },
222 #define CMDLINE_PROCESS \
229 #define TEST_FUNCTION do_test ()
230 #include "../test-skeleton.c"