tst-ptrguard1.c 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217
  1. /* Copyright (C) 2013-2019 Free Software Foundation, Inc.
  2. This file is part of the GNU C Library.
  3. The GNU C Library is free software; you can redistribute it and/or
  4. modify it under the terms of the GNU Lesser General Public
  5. License as published by the Free Software Foundation; either
  6. version 2.1 of the License, or (at your option) any later version.
  7. The GNU C Library is distributed in the hope that it will be useful,
  8. but WITHOUT ANY WARRANTY; without even the implied warranty of
  9. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  10. Lesser General Public License for more details.
  11. You should have received a copy of the GNU Lesser General Public
  12. License along with the GNU C Library; if not, see
  13. <http://www.gnu.org/licenses/>. */
  14. #include <errno.h>
  15. #include <stdbool.h>
  16. #include <stdio.h>
  17. #include <stdlib.h>
  18. #include <string.h>
  19. #include <sys/wait.h>
  20. #include <stackguard-macros.h>
  21. #include <tls.h>
  22. #include <unistd.h>
  23. #ifndef _GNU_SOURCE
  24. #define _GNU_SOURCE
  25. #endif
  26. /* Requires _GNU_SOURCE */
  27. #include <getopt.h>
  28. #ifndef POINTER_CHK_GUARD
  29. extern uintptr_t __pointer_chk_guard;
  30. # define POINTER_CHK_GUARD __pointer_chk_guard
  31. #endif
  32. static const char *command;
  33. static bool child;
  34. static uintptr_t ptr_chk_guard_copy;
  35. static bool ptr_chk_guard_copy_set;
  36. static int fds[2];
  37. static void __attribute__ ((constructor))
  38. con (void)
  39. {
  40. ptr_chk_guard_copy = POINTER_CHK_GUARD;
  41. ptr_chk_guard_copy_set = true;
  42. }
  43. static int
  44. uintptr_t_cmp (const void *a, const void *b)
  45. {
  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;
  51. }
  52. static int
  53. do_test (void)
  54. {
  55. if (!ptr_chk_guard_copy_set)
  56. {
  57. puts ("constructor has not been run");
  58. return 1;
  59. }
  60. if (ptr_chk_guard_copy != POINTER_CHK_GUARD)
  61. {
  62. puts ("POINTER_CHK_GUARD changed between constructor and do_test");
  63. return 1;
  64. }
  65. if (child)
  66. {
  67. write (2, &ptr_chk_guard_copy, sizeof (ptr_chk_guard_copy));
  68. return 0;
  69. }
  70. if (command == NULL)
  71. {
  72. puts ("missing --command or --child argument");
  73. return 1;
  74. }
  75. #define N 16
  76. uintptr_t child_ptr_chk_guards[N + 1];
  77. child_ptr_chk_guards[N] = ptr_chk_guard_copy;
  78. int i;
  79. for (i = 0; i < N; ++i)
  80. {
  81. if (pipe (fds) < 0)
  82. {
  83. printf ("couldn't create pipe: %m\n");
  84. return 1;
  85. }
  86. pid_t pid = fork ();
  87. if (pid < 0)
  88. {
  89. printf ("fork failed: %m\n");
  90. return 1;
  91. }
  92. if (!pid)
  93. {
  94. if (ptr_chk_guard_copy != POINTER_CHK_GUARD)
  95. {
  96. puts ("POINTER_CHK_GUARD changed after fork");
  97. exit (1);
  98. }
  99. close (fds[0]);
  100. close (2);
  101. dup2 (fds[1], 2);
  102. close (fds[1]);
  103. system (command);
  104. exit (0);
  105. }
  106. close (fds[1]);
  107. if (TEMP_FAILURE_RETRY (read (fds[0], &child_ptr_chk_guards[i],
  108. sizeof (uintptr_t))) != sizeof (uintptr_t))
  109. {
  110. puts ("could not read ptr_chk_guard value from child");
  111. return 1;
  112. }
  113. close (fds[0]);
  114. pid_t termpid;
  115. int status;
  116. termpid = TEMP_FAILURE_RETRY (waitpid (pid, &status, 0));
  117. if (termpid == -1)
  118. {
  119. printf ("waitpid failed: %m\n");
  120. return 1;
  121. }
  122. else if (termpid != pid)
  123. {
  124. printf ("waitpid returned %ld != %ld\n",
  125. (long int) termpid, (long int) pid);
  126. return 1;
  127. }
  128. else if (!WIFEXITED (status) || WEXITSTATUS (status))
  129. {
  130. puts ("child hasn't exited with exit status 0");
  131. return 1;
  132. }
  133. }
  134. qsort (child_ptr_chk_guards, N + 1, sizeof (uintptr_t), uintptr_t_cmp);
  135. /* The default pointer guard is the same as the default stack guard.
  136. They are only set to default if dl_random is NULL. */
  137. uintptr_t default_guard = 0;
  138. unsigned char *p = (unsigned char *) &default_guard;
  139. p[sizeof (uintptr_t) - 1] = 255;
  140. p[sizeof (uintptr_t) - 2] = '\n';
  141. p[0] = 0;
  142. /* Test if the pointer guard canaries are either randomized,
  143. or equal to the default pointer guard value.
  144. Even with randomized pointer guards it might happen
  145. that the random number generator generates the same
  146. values, but if that happens in more than half from
  147. the 16 runs, something is very wrong. */
  148. int ndifferences = 0;
  149. int ndefaults = 0;
  150. for (i = 0; i < N; ++i)
  151. {
  152. if (child_ptr_chk_guards[i] != child_ptr_chk_guards[i+1])
  153. ndifferences++;
  154. else if (child_ptr_chk_guards[i] == default_guard)
  155. ndefaults++;
  156. }
  157. printf ("differences %d defaults %d\n", ndifferences, ndefaults);
  158. if (ndifferences < N / 2 && ndefaults < N / 2)
  159. {
  160. puts ("pointer guard values are not randomized enough");
  161. puts ("nor equal to the default value");
  162. return 1;
  163. }
  164. return 0;
  165. }
  166. #define OPT_COMMAND 10000
  167. #define OPT_CHILD 10001
  168. #define CMDLINE_OPTIONS \
  169. { "command", required_argument, NULL, OPT_COMMAND }, \
  170. { "child", no_argument, NULL, OPT_CHILD },
  171. static void __attribute((used))
  172. cmdline_process_function (int c)
  173. {
  174. switch (c)
  175. {
  176. case OPT_COMMAND:
  177. command = optarg;
  178. break;
  179. case OPT_CHILD:
  180. child = true;
  181. break;
  182. }
  183. }
  184. #define CMDLINE_PROCESS cmdline_process_function
  185. #include <support/test-driver.c>