Use ASCII in LC_TIME of om_ET for better readability
[glibc.git] / elf / tst-stackguard1.c
blob78e33c708341c815c2506a96b1ac148cfc42b033
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/>. */
19 #include <errno.h>
20 #include <getopt.h>
21 #include <stdbool.h>
22 #include <stdio.h>
23 #include <stdlib.h>
24 #include <string.h>
25 #include <sys/wait.h>
26 #include <stackguard-macros.h>
27 #include <tls.h>
28 #include <unistd.h>
30 static const char *command;
31 static bool child;
32 static uintptr_t stack_chk_guard_copy;
33 static bool stack_chk_guard_copy_set;
34 static int fds[2];
36 static void __attribute__ ((constructor))
37 con (void)
39 stack_chk_guard_copy = STACK_CHK_GUARD;
40 stack_chk_guard_copy_set = true;
43 static int
44 uintptr_t_cmp (const void *a, const void *b)
46 if (*(uintptr_t *) a < *(uintptr_t *) b)
47 return 1;
48 if (*(uintptr_t *) a > *(uintptr_t *) b)
49 return -1;
50 return 0;
53 static int
54 do_test (void)
56 if (!stack_chk_guard_copy_set)
58 puts ("constructor has not been run");
59 return 1;
62 if (stack_chk_guard_copy != STACK_CHK_GUARD)
64 puts ("STACK_CHK_GUARD changed between constructor and do_test");
65 return 1;
68 if (child)
70 write (2, &stack_chk_guard_copy, sizeof (stack_chk_guard_copy));
71 return 0;
74 if (command == NULL)
76 puts ("missing --command or --child argument");
77 return 1;
80 #define N 16
81 uintptr_t child_stack_chk_guards[N + 1];
82 child_stack_chk_guards[N] = stack_chk_guard_copy;
83 int i;
84 for (i = 0; i < N; ++i)
86 if (pipe (fds) < 0)
88 printf ("couldn't create pipe: %m\n");
89 return 1;
92 pid_t pid = fork ();
93 if (pid < 0)
95 printf ("fork failed: %m\n");
96 return 1;
99 if (!pid)
101 if (stack_chk_guard_copy != STACK_CHK_GUARD)
103 puts ("STACK_CHK_GUARD changed after fork");
104 exit (1);
107 close (fds[0]);
108 close (2);
109 dup2 (fds[1], 2);
110 close (fds[1]);
112 system (command);
113 exit (0);
116 close (fds[1]);
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");
122 return 1;
125 close (fds[0]);
127 pid_t termpid;
128 int status;
129 termpid = TEMP_FAILURE_RETRY (waitpid (pid, &status, 0));
130 if (termpid == -1)
132 printf ("waitpid failed: %m\n");
133 return 1;
135 else if (termpid != pid)
137 printf ("waitpid returned %ld != %ld\n",
138 (long int) termpid, (long int) pid);
139 return 1;
141 else if (!WIFEXITED (status) || WEXITSTATUS (status))
143 puts ("child hasn't exited with exit status 0");
144 return 1;
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';
154 p[0] = 0;
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;
163 int ndefaults = 0;
164 for (i = 0; i < N; ++i)
166 if (child_stack_chk_guards[i] != child_stack_chk_guards[i+1])
167 ndifferences++;
168 else if (child_stack_chk_guards[i] == default_guard)
169 ndefaults++;
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");
178 return 1;
181 return 0;
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)
193 switch (c)
195 case OPT_COMMAND:
196 command = optarg;
197 break;
198 case OPT_CHILD:
199 child = true;
200 break;
203 #define CMDLINE_PROCESS cmdline_process_function
205 #include <support/test-driver.c>