1 /* Copyright (C) 2013-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/>. */
24 #include <stackguard-macros.h>
31 /* Requires _GNU_SOURCE */
34 #ifndef POINTER_CHK_GUARD
35 extern uintptr_t __pointer_chk_guard;
36 # define POINTER_CHK_GUARD __pointer_chk_guard
39 static const char *command;
41 static uintptr_t ptr_chk_guard_copy;
42 static bool ptr_chk_guard_copy_set;
45 static void __attribute__ ((constructor))
48 ptr_chk_guard_copy = POINTER_CHK_GUARD;
49 ptr_chk_guard_copy_set = true;
53 uintptr_t_cmp (const void *a, const void *b)
55 if (*(uintptr_t *) a < *(uintptr_t *) b)
57 if (*(uintptr_t *) a > *(uintptr_t *) b)
65 if (!ptr_chk_guard_copy_set)
67 puts ("constructor has not been run");
71 if (ptr_chk_guard_copy != POINTER_CHK_GUARD)
73 puts ("POINTER_CHK_GUARD changed between constructor and do_test");
79 write (2, &ptr_chk_guard_copy, sizeof (ptr_chk_guard_copy));
85 puts ("missing --command or --child argument");
90 uintptr_t child_ptr_chk_guards[N + 1];
91 child_ptr_chk_guards[N] = ptr_chk_guard_copy;
93 for (i = 0; i < N; ++i)
97 printf ("couldn't create pipe: %m\n");
104 printf ("fork failed: %m\n");
110 if (ptr_chk_guard_copy != POINTER_CHK_GUARD)
112 puts ("POINTER_CHK_GUARD changed after fork");
127 if (TEMP_FAILURE_RETRY (read (fds[0], &child_ptr_chk_guards[i],
128 sizeof (uintptr_t))) != sizeof (uintptr_t))
130 puts ("could not read ptr_chk_guard value from child");
138 termpid = TEMP_FAILURE_RETRY (waitpid (pid, &status, 0));
141 printf ("waitpid failed: %m\n");
144 else if (termpid != pid)
146 printf ("waitpid returned %ld != %ld\n",
147 (long int) termpid, (long int) pid);
150 else if (!WIFEXITED (status) || WEXITSTATUS (status))
152 puts ("child hasn't exited with exit status 0");
157 qsort (child_ptr_chk_guards, N + 1, sizeof (uintptr_t), uintptr_t_cmp);
159 /* The default pointer guard is the same as the default stack guard.
160 They are only set to default if dl_random is NULL. */
161 uintptr_t default_guard = 0;
162 unsigned char *p = (unsigned char *) &default_guard;
163 p[sizeof (uintptr_t) - 1] = 255;
164 p[sizeof (uintptr_t) - 2] = '\n';
167 /* Test if the pointer guard canaries are either randomized,
168 or equal to the default pointer guard value.
169 Even with randomized pointer guards it might happen
170 that the random number generator generates the same
171 values, but if that happens in more than half from
172 the 16 runs, something is very wrong. */
173 int ndifferences = 0;
175 for (i = 0; i < N; ++i)
177 if (child_ptr_chk_guards[i] != child_ptr_chk_guards[i+1])
179 else if (child_ptr_chk_guards[i] == default_guard)
183 printf ("differences %d defaults %d\n", ndifferences, ndefaults);
185 if (ndifferences < N / 2 && ndefaults < N / 2)
187 puts ("pointer guard values are not randomized enough");
188 puts ("nor equal to the default value");
195 #define OPT_COMMAND 10000
196 #define OPT_CHILD 10001
197 #define CMDLINE_OPTIONS \
198 { "command", required_argument, NULL, OPT_COMMAND }, \
199 { "child", no_argument, NULL, OPT_CHILD },
201 static void __attribute((used))
202 cmdline_process_function (int c)
215 #define CMDLINE_PROCESS cmdline_process_function
217 #include <support/test-driver.c>