1 /* Copyright (C) 2005-2017 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_set
)
58 puts ("constructor has not been run");
62 if (stack_chk_guard_copy
!= STACK_CHK_GUARD
)
64 puts ("STACK_CHK_GUARD changed between constructor and do_test");
70 write (2, &stack_chk_guard_copy
, sizeof (stack_chk_guard_copy
));
76 puts ("missing --command or --child argument");
81 uintptr_t child_stack_chk_guards
[N
+ 1];
82 child_stack_chk_guards
[N
] = stack_chk_guard_copy
;
84 for (i
= 0; i
< N
; ++i
)
88 printf ("couldn't create pipe: %m\n");
95 printf ("fork failed: %m\n");
101 if (stack_chk_guard_copy
!= STACK_CHK_GUARD
)
103 puts ("STACK_CHK_GUARD changed after fork");
118 if (TEMP_FAILURE_RETRY (read (fds
[0], &child_stack_chk_guards
[i
],
119 sizeof (uintptr_t))) != sizeof (uintptr_t))
121 puts ("could not read stack_chk_guard value from child");
129 termpid
= TEMP_FAILURE_RETRY (waitpid (pid
, &status
, 0));
132 printf ("waitpid failed: %m\n");
135 else if (termpid
!= pid
)
137 printf ("waitpid returned %ld != %ld\n",
138 (long int) termpid
, (long int) pid
);
141 else if (!WIFEXITED (status
) || WEXITSTATUS (status
))
143 puts ("child hasn't exited with exit status 0");
148 qsort (child_stack_chk_guards
, N
+ 1, sizeof (uintptr_t), uintptr_t_cmp
);
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';
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;
164 for (i
= 0; i
< N
; ++i
)
166 if (child_stack_chk_guards
[i
] != child_stack_chk_guards
[i
+1])
168 else if (child_stack_chk_guards
[i
] == default_guard
)
172 printf ("differences %d defaults %d\n", ndifferences
, ndefaults
);
174 if (ndifferences
< N
/ 2 && ndefaults
< N
/ 2)
176 puts ("stack guard canaries are not randomized enough");
177 puts ("nor equal to the default canary value");
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 },
190 static void __attribute__((used
))
191 cmdline_process_function (int c
)
203 #define CMDLINE_PROCESS cmdline_process_function
205 #include <support/test-driver.c>