1 /* Copyright (C) 2013-2017 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 <http://www.gnu.org/licenses/>. */
24 #include <stackguard-macros.h>
28 #ifndef POINTER_CHK_GUARD
29 extern uintptr_t __pointer_chk_guard
;
30 # define POINTER_CHK_GUARD __pointer_chk_guard
33 static const char *command
;
35 static uintptr_t ptr_chk_guard_copy
;
36 static bool ptr_chk_guard_copy_set
;
39 static void __attribute__ ((constructor
))
42 ptr_chk_guard_copy
= POINTER_CHK_GUARD
;
43 ptr_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 (!ptr_chk_guard_copy_set
)
61 puts ("constructor has not been run");
65 if (ptr_chk_guard_copy
!= POINTER_CHK_GUARD
)
67 puts ("POINTER_CHK_GUARD changed between constructor and do_test");
73 write (2, &ptr_chk_guard_copy
, sizeof (ptr_chk_guard_copy
));
79 puts ("missing --command or --child argument");
84 uintptr_t child_ptr_chk_guards
[N
+ 1];
85 child_ptr_chk_guards
[N
] = ptr_chk_guard_copy
;
87 for (i
= 0; i
< N
; ++i
)
91 printf ("couldn't create pipe: %m\n");
98 printf ("fork failed: %m\n");
104 if (ptr_chk_guard_copy
!= POINTER_CHK_GUARD
)
106 puts ("POINTER_CHK_GUARD changed after fork");
121 if (TEMP_FAILURE_RETRY (read (fds
[0], &child_ptr_chk_guards
[i
],
122 sizeof (uintptr_t))) != sizeof (uintptr_t))
124 puts ("could not read ptr_chk_guard value from child");
132 termpid
= TEMP_FAILURE_RETRY (waitpid (pid
, &status
, 0));
135 printf ("waitpid failed: %m\n");
138 else if (termpid
!= pid
)
140 printf ("waitpid returned %ld != %ld\n",
141 (long int) termpid
, (long int) pid
);
144 else if (!WIFEXITED (status
) || WEXITSTATUS (status
))
146 puts ("child hasn't exited with exit status 0");
151 qsort (child_ptr_chk_guards
, N
+ 1, sizeof (uintptr_t), uintptr_t_cmp
);
153 /* The default pointer guard is the same as the default stack guard.
154 They are only set to default if dl_random is NULL. */
155 uintptr_t default_guard
= 0;
156 unsigned char *p
= (unsigned char *) &default_guard
;
157 p
[sizeof (uintptr_t) - 1] = 255;
158 p
[sizeof (uintptr_t) - 2] = '\n';
161 /* Test if the pointer guard canaries are either randomized,
162 or equal to the default pointer guard value.
163 Even with randomized pointer guards it might happen
164 that the random number generator generates the same
165 values, but if that happens in more than half from
166 the 16 runs, something is very wrong. */
167 int ndifferences
= 0;
169 for (i
= 0; i
< N
; ++i
)
171 if (child_ptr_chk_guards
[i
] != child_ptr_chk_guards
[i
+1])
173 else if (child_ptr_chk_guards
[i
] == default_guard
)
177 printf ("differences %d defaults %d\n", ndifferences
, ndefaults
);
179 if (ndifferences
< N
/ 2 && ndefaults
< N
/ 2)
181 puts ("pointer guard values are not randomized enough");
182 puts ("nor equal to the default value");
189 #define OPT_COMMAND 10000
190 #define OPT_CHILD 10001
191 #define CMDLINE_OPTIONS \
192 { "command", required_argument, NULL, OPT_COMMAND }, \
193 { "child", no_argument, NULL, OPT_CHILD },
194 #define CMDLINE_PROCESS \
201 #define TEST_FUNCTION do_test ()
202 #include "../test-skeleton.c"