|
Packit |
6c4009 |
/* Copyright (C) 2013-2018 Free Software Foundation, Inc.
|
|
Packit |
6c4009 |
This file is part of the GNU C Library.
|
|
Packit |
6c4009 |
|
|
Packit |
6c4009 |
The GNU C Library is free software; you can redistribute it and/or
|
|
Packit |
6c4009 |
modify it under the terms of the GNU Lesser General Public
|
|
Packit |
6c4009 |
License as published by the Free Software Foundation; either
|
|
Packit |
6c4009 |
version 2.1 of the License, or (at your option) any later version.
|
|
Packit |
6c4009 |
|
|
Packit |
6c4009 |
The GNU C Library is distributed in the hope that it will be useful,
|
|
Packit |
6c4009 |
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
Packit |
6c4009 |
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
|
Packit |
6c4009 |
Lesser General Public License for more details.
|
|
Packit |
6c4009 |
|
|
Packit |
6c4009 |
You should have received a copy of the GNU Lesser General Public
|
|
Packit |
6c4009 |
License along with the GNU C Library; if not, see
|
|
Packit |
6c4009 |
<http://www.gnu.org/licenses/>. */
|
|
Packit |
6c4009 |
|
|
Packit |
6c4009 |
#include <errno.h>
|
|
Packit |
6c4009 |
#include <stdbool.h>
|
|
Packit |
6c4009 |
#include <stdio.h>
|
|
Packit |
6c4009 |
#include <stdlib.h>
|
|
Packit |
6c4009 |
#include <string.h>
|
|
Packit |
6c4009 |
#include <sys/wait.h>
|
|
Packit |
6c4009 |
#include <stackguard-macros.h>
|
|
Packit |
6c4009 |
#include <tls.h>
|
|
Packit |
6c4009 |
#include <unistd.h>
|
|
Packit |
6c4009 |
|
|
Packit |
6c4009 |
#ifndef _GNU_SOURCE
|
|
Packit |
6c4009 |
#define _GNU_SOURCE
|
|
Packit |
6c4009 |
#endif
|
|
Packit |
6c4009 |
/* Requires _GNU_SOURCE */
|
|
Packit |
6c4009 |
#include <getopt.h>
|
|
Packit |
6c4009 |
|
|
Packit |
6c4009 |
#ifndef POINTER_CHK_GUARD
|
|
Packit |
6c4009 |
extern uintptr_t __pointer_chk_guard;
|
|
Packit |
6c4009 |
# define POINTER_CHK_GUARD __pointer_chk_guard
|
|
Packit |
6c4009 |
#endif
|
|
Packit |
6c4009 |
|
|
Packit |
6c4009 |
static const char *command;
|
|
Packit |
6c4009 |
static bool child;
|
|
Packit |
6c4009 |
static uintptr_t ptr_chk_guard_copy;
|
|
Packit |
6c4009 |
static bool ptr_chk_guard_copy_set;
|
|
Packit |
6c4009 |
static int fds[2];
|
|
Packit |
6c4009 |
|
|
Packit |
6c4009 |
static void __attribute__ ((constructor))
|
|
Packit |
6c4009 |
con (void)
|
|
Packit |
6c4009 |
{
|
|
Packit |
6c4009 |
ptr_chk_guard_copy = POINTER_CHK_GUARD;
|
|
Packit |
6c4009 |
ptr_chk_guard_copy_set = true;
|
|
Packit |
6c4009 |
}
|
|
Packit |
6c4009 |
|
|
Packit |
6c4009 |
static int
|
|
Packit |
6c4009 |
uintptr_t_cmp (const void *a, const void *b)
|
|
Packit |
6c4009 |
{
|
|
Packit |
6c4009 |
if (*(uintptr_t *) a < *(uintptr_t *) b)
|
|
Packit |
6c4009 |
return 1;
|
|
Packit |
6c4009 |
if (*(uintptr_t *) a > *(uintptr_t *) b)
|
|
Packit |
6c4009 |
return -1;
|
|
Packit |
6c4009 |
return 0;
|
|
Packit |
6c4009 |
}
|
|
Packit |
6c4009 |
|
|
Packit |
6c4009 |
static int
|
|
Packit |
6c4009 |
do_test (void)
|
|
Packit |
6c4009 |
{
|
|
Packit |
6c4009 |
if (!ptr_chk_guard_copy_set)
|
|
Packit |
6c4009 |
{
|
|
Packit |
6c4009 |
puts ("constructor has not been run");
|
|
Packit |
6c4009 |
return 1;
|
|
Packit |
6c4009 |
}
|
|
Packit |
6c4009 |
|
|
Packit |
6c4009 |
if (ptr_chk_guard_copy != POINTER_CHK_GUARD)
|
|
Packit |
6c4009 |
{
|
|
Packit |
6c4009 |
puts ("POINTER_CHK_GUARD changed between constructor and do_test");
|
|
Packit |
6c4009 |
return 1;
|
|
Packit |
6c4009 |
}
|
|
Packit |
6c4009 |
|
|
Packit |
6c4009 |
if (child)
|
|
Packit |
6c4009 |
{
|
|
Packit |
6c4009 |
write (2, &ptr_chk_guard_copy, sizeof (ptr_chk_guard_copy));
|
|
Packit |
6c4009 |
return 0;
|
|
Packit |
6c4009 |
}
|
|
Packit |
6c4009 |
|
|
Packit |
6c4009 |
if (command == NULL)
|
|
Packit |
6c4009 |
{
|
|
Packit |
6c4009 |
puts ("missing --command or --child argument");
|
|
Packit |
6c4009 |
return 1;
|
|
Packit |
6c4009 |
}
|
|
Packit |
6c4009 |
|
|
Packit |
6c4009 |
#define N 16
|
|
Packit |
6c4009 |
uintptr_t child_ptr_chk_guards[N + 1];
|
|
Packit |
6c4009 |
child_ptr_chk_guards[N] = ptr_chk_guard_copy;
|
|
Packit |
6c4009 |
int i;
|
|
Packit |
6c4009 |
for (i = 0; i < N; ++i)
|
|
Packit |
6c4009 |
{
|
|
Packit |
6c4009 |
if (pipe (fds) < 0)
|
|
Packit |
6c4009 |
{
|
|
Packit |
6c4009 |
printf ("couldn't create pipe: %m\n");
|
|
Packit |
6c4009 |
return 1;
|
|
Packit |
6c4009 |
}
|
|
Packit |
6c4009 |
|
|
Packit |
6c4009 |
pid_t pid = fork ();
|
|
Packit |
6c4009 |
if (pid < 0)
|
|
Packit |
6c4009 |
{
|
|
Packit |
6c4009 |
printf ("fork failed: %m\n");
|
|
Packit |
6c4009 |
return 1;
|
|
Packit |
6c4009 |
}
|
|
Packit |
6c4009 |
|
|
Packit |
6c4009 |
if (!pid)
|
|
Packit |
6c4009 |
{
|
|
Packit |
6c4009 |
if (ptr_chk_guard_copy != POINTER_CHK_GUARD)
|
|
Packit |
6c4009 |
{
|
|
Packit |
6c4009 |
puts ("POINTER_CHK_GUARD changed after fork");
|
|
Packit |
6c4009 |
exit (1);
|
|
Packit |
6c4009 |
}
|
|
Packit |
6c4009 |
|
|
Packit |
6c4009 |
close (fds[0]);
|
|
Packit |
6c4009 |
close (2);
|
|
Packit |
6c4009 |
dup2 (fds[1], 2);
|
|
Packit |
6c4009 |
close (fds[1]);
|
|
Packit |
6c4009 |
|
|
Packit |
6c4009 |
system (command);
|
|
Packit |
6c4009 |
exit (0);
|
|
Packit |
6c4009 |
}
|
|
Packit |
6c4009 |
|
|
Packit |
6c4009 |
close (fds[1]);
|
|
Packit |
6c4009 |
|
|
Packit |
6c4009 |
if (TEMP_FAILURE_RETRY (read (fds[0], &child_ptr_chk_guards[i],
|
|
Packit |
6c4009 |
sizeof (uintptr_t))) != sizeof (uintptr_t))
|
|
Packit |
6c4009 |
{
|
|
Packit |
6c4009 |
puts ("could not read ptr_chk_guard value from child");
|
|
Packit |
6c4009 |
return 1;
|
|
Packit |
6c4009 |
}
|
|
Packit |
6c4009 |
|
|
Packit |
6c4009 |
close (fds[0]);
|
|
Packit |
6c4009 |
|
|
Packit |
6c4009 |
pid_t termpid;
|
|
Packit |
6c4009 |
int status;
|
|
Packit |
6c4009 |
termpid = TEMP_FAILURE_RETRY (waitpid (pid, &status, 0));
|
|
Packit |
6c4009 |
if (termpid == -1)
|
|
Packit |
6c4009 |
{
|
|
Packit |
6c4009 |
printf ("waitpid failed: %m\n");
|
|
Packit |
6c4009 |
return 1;
|
|
Packit |
6c4009 |
}
|
|
Packit |
6c4009 |
else if (termpid != pid)
|
|
Packit |
6c4009 |
{
|
|
Packit |
6c4009 |
printf ("waitpid returned %ld != %ld\n",
|
|
Packit |
6c4009 |
(long int) termpid, (long int) pid);
|
|
Packit |
6c4009 |
return 1;
|
|
Packit |
6c4009 |
}
|
|
Packit |
6c4009 |
else if (!WIFEXITED (status) || WEXITSTATUS (status))
|
|
Packit |
6c4009 |
{
|
|
Packit |
6c4009 |
puts ("child hasn't exited with exit status 0");
|
|
Packit |
6c4009 |
return 1;
|
|
Packit |
6c4009 |
}
|
|
Packit |
6c4009 |
}
|
|
Packit |
6c4009 |
|
|
Packit |
6c4009 |
qsort (child_ptr_chk_guards, N + 1, sizeof (uintptr_t), uintptr_t_cmp);
|
|
Packit |
6c4009 |
|
|
Packit |
6c4009 |
/* The default pointer guard is the same as the default stack guard.
|
|
Packit |
6c4009 |
They are only set to default if dl_random is NULL. */
|
|
Packit |
6c4009 |
uintptr_t default_guard = 0;
|
|
Packit |
6c4009 |
unsigned char *p = (unsigned char *) &default_guard;
|
|
Packit |
6c4009 |
p[sizeof (uintptr_t) - 1] = 255;
|
|
Packit |
6c4009 |
p[sizeof (uintptr_t) - 2] = '\n';
|
|
Packit |
6c4009 |
p[0] = 0;
|
|
Packit |
6c4009 |
|
|
Packit |
6c4009 |
/* Test if the pointer guard canaries are either randomized,
|
|
Packit |
6c4009 |
or equal to the default pointer guard value.
|
|
Packit |
6c4009 |
Even with randomized pointer guards it might happen
|
|
Packit |
6c4009 |
that the random number generator generates the same
|
|
Packit |
6c4009 |
values, but if that happens in more than half from
|
|
Packit |
6c4009 |
the 16 runs, something is very wrong. */
|
|
Packit |
6c4009 |
int ndifferences = 0;
|
|
Packit |
6c4009 |
int ndefaults = 0;
|
|
Packit |
6c4009 |
for (i = 0; i < N; ++i)
|
|
Packit |
6c4009 |
{
|
|
Packit |
6c4009 |
if (child_ptr_chk_guards[i] != child_ptr_chk_guards[i+1])
|
|
Packit |
6c4009 |
ndifferences++;
|
|
Packit |
6c4009 |
else if (child_ptr_chk_guards[i] == default_guard)
|
|
Packit |
6c4009 |
ndefaults++;
|
|
Packit |
6c4009 |
}
|
|
Packit |
6c4009 |
|
|
Packit |
6c4009 |
printf ("differences %d defaults %d\n", ndifferences, ndefaults);
|
|
Packit |
6c4009 |
|
|
Packit |
6c4009 |
if (ndifferences < N / 2 && ndefaults < N / 2)
|
|
Packit |
6c4009 |
{
|
|
Packit |
6c4009 |
puts ("pointer guard values are not randomized enough");
|
|
Packit |
6c4009 |
puts ("nor equal to the default value");
|
|
Packit |
6c4009 |
return 1;
|
|
Packit |
6c4009 |
}
|
|
Packit |
6c4009 |
|
|
Packit |
6c4009 |
return 0;
|
|
Packit |
6c4009 |
}
|
|
Packit |
6c4009 |
|
|
Packit |
6c4009 |
#define OPT_COMMAND 10000
|
|
Packit |
6c4009 |
#define OPT_CHILD 10001
|
|
Packit |
6c4009 |
#define CMDLINE_OPTIONS \
|
|
Packit |
6c4009 |
{ "command", required_argument, NULL, OPT_COMMAND }, \
|
|
Packit |
6c4009 |
{ "child", no_argument, NULL, OPT_CHILD },
|
|
Packit |
6c4009 |
|
|
Packit |
6c4009 |
static void __attribute((used))
|
|
Packit |
6c4009 |
cmdline_process_function (int c)
|
|
Packit |
6c4009 |
{
|
|
Packit |
6c4009 |
switch (c)
|
|
Packit |
6c4009 |
{
|
|
Packit |
6c4009 |
case OPT_COMMAND:
|
|
Packit |
6c4009 |
command = optarg;
|
|
Packit |
6c4009 |
break;
|
|
Packit |
6c4009 |
case OPT_CHILD:
|
|
Packit |
6c4009 |
child = true;
|
|
Packit |
6c4009 |
break;
|
|
Packit |
6c4009 |
}
|
|
Packit |
6c4009 |
}
|
|
Packit |
6c4009 |
|
|
Packit |
6c4009 |
#define CMDLINE_PROCESS cmdline_process_function
|
|
Packit |
6c4009 |
|
|
Packit |
6c4009 |
#include <support/test-driver.c>
|