1 /* Copyright (C) 2005-2014 Free Software Foundation, Inc.
2 This file is part of the GNU C Library.
3 Contributed by Jakub Jelinek <jakub@redhat.com>, 2005.
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.
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.
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/>. */
26 #include <stackguard-macros.h>
30 static const char *command
;
32 static uintptr_t stack_chk_guard_copy
;
33 static bool stack_chk_guard_copy_set
;
36 static void __attribute__ ((constructor
))
39 stack_chk_guard_copy
= STACK_CHK_GUARD
;
40 stack_chk_guard_copy_set
= true;
44 uintptr_t_cmp (const void *a
, const void *b
)
46 if (*(uintptr_t *) a
< *(uintptr_t *) b
)
48 if (*(uintptr_t *) a
> *(uintptr_t *) b
)
56 if (stack_chk_guard_copy
!= STACK_CHK_GUARD
)
58 puts ("STACK_CHK_GUARD changed in thread");
67 if (!stack_chk_guard_copy_set
)
69 puts ("constructor has not been run");
73 if (stack_chk_guard_copy
!= STACK_CHK_GUARD
)
75 puts ("STACK_CHK_GUARD changed between constructor and do_test");
84 for (i
= 0; i
< 4; ++i
)
85 if (pthread_create (&th
[i
], NULL
, tf
, NULL
))
87 puts ("thread creation failed");
90 for (i
= 0; i
< 4; ++i
)
91 if (pthread_join (th
[i
], &ret
))
93 puts ("thread join failed");
99 write (2, &stack_chk_guard_copy
, sizeof (stack_chk_guard_copy
));
105 puts ("missing --command or --child argument");
110 uintptr_t child_stack_chk_guards
[N
+ 1];
111 child_stack_chk_guards
[N
] = stack_chk_guard_copy
;
113 for (i
= 0; i
< N
; ++i
)
117 printf ("couldn't create pipe: %m\n");
124 printf ("fork failed: %m\n");
130 if (stack_chk_guard_copy
!= STACK_CHK_GUARD
)
132 puts ("STACK_CHK_GUARD changed after fork");
147 if (TEMP_FAILURE_RETRY (read (fds
[0], &child_stack_chk_guards
[i
],
148 sizeof (uintptr_t))) != sizeof (uintptr_t))
150 puts ("could not read stack_chk_guard value from child");
158 termpid
= TEMP_FAILURE_RETRY (waitpid (pid
, &status
, 0));
161 printf ("waitpid failed: %m\n");
164 else if (termpid
!= pid
)
166 printf ("waitpid returned %ld != %ld\n",
167 (long int) termpid
, (long int) pid
);
170 else if (!WIFEXITED (status
) || WEXITSTATUS (status
))
172 puts ("child hasn't exited with exit status 0");
177 qsort (child_stack_chk_guards
, N
+ 1, sizeof (uintptr_t), uintptr_t_cmp
);
179 uintptr_t default_guard
= 0;
180 unsigned char *p
= (unsigned char *) &default_guard
;
181 p
[sizeof (uintptr_t) - 1] = 255;
182 p
[sizeof (uintptr_t) - 2] = '\n';
185 /* Test if the stack guard canaries are either randomized,
186 or equal to the default stack guard canary value.
187 Even with randomized stack guards it might happen
188 that the random number generator generates the same
189 values, but if that happens in more than half from
190 the 16 runs, something is very wrong. */
191 int ndifferences
= 0;
193 for (i
= 0; i
< N
; ++i
)
195 if (child_stack_chk_guards
[i
] != child_stack_chk_guards
[i
+1])
197 else if (child_stack_chk_guards
[i
] == default_guard
)
201 printf ("differences %d defaults %d\n", ndifferences
, ndefaults
);
203 if (ndifferences
< N
/ 2 && ndefaults
< N
/ 2)
205 puts ("stack guard canaries are not randomized enough");
206 puts ("nor equal to the default canary value");
213 #define OPT_COMMAND 10000
214 #define OPT_CHILD 10001
215 #define CMDLINE_OPTIONS \
216 { "command", required_argument, NULL, OPT_COMMAND }, \
217 { "child", no_argument, NULL, OPT_CHILD },
218 #define CMDLINE_PROCESS \
225 #define TEST_FUNCTION do_test ()
226 #include "../test-skeleton.c"