hashchk_test.c 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233
  1. // SPDX-License-Identifier: GPL-2.0+
  2. #define _GNU_SOURCE
  3. #include <errno.h>
  4. #include <fcntl.h>
  5. #include <limits.h>
  6. #include <sched.h>
  7. #include <setjmp.h>
  8. #include <signal.h>
  9. #include <stdio.h>
  10. #include <stdlib.h>
  11. #include <string.h>
  12. #include <sys/mman.h>
  13. #include <sys/prctl.h>
  14. #include <unistd.h>
  15. #include "dexcr.h"
  16. #include "utils.h"
  17. static int require_nphie(void)
  18. {
  19. SKIP_IF_MSG(!dexcr_exists(), "DEXCR not supported");
  20. pr_set_dexcr(PR_PPC_DEXCR_NPHIE, PR_PPC_DEXCR_CTRL_SET | PR_PPC_DEXCR_CTRL_SET_ONEXEC);
  21. if (get_dexcr(EFFECTIVE) & DEXCR_PR_NPHIE)
  22. return 0;
  23. SKIP_IF_MSG(!(get_dexcr(EFFECTIVE) & DEXCR_PR_NPHIE),
  24. "Failed to enable DEXCR[NPHIE]");
  25. return 0;
  26. }
  27. static jmp_buf hashchk_detected_buf;
  28. static const char *hashchk_failure_msg;
  29. static void hashchk_handler(int signum, siginfo_t *info, void *context)
  30. {
  31. if (signum != SIGILL)
  32. hashchk_failure_msg = "wrong signal received";
  33. else if (info->si_code != ILL_ILLOPN)
  34. hashchk_failure_msg = "wrong signal code received";
  35. longjmp(hashchk_detected_buf, 0);
  36. }
  37. /*
  38. * Check that hashchk triggers when DEXCR[NPHIE] is enabled
  39. * and is detected as such by the kernel exception handler
  40. */
  41. static int hashchk_detected_test(void)
  42. {
  43. struct sigaction old;
  44. int err;
  45. err = require_nphie();
  46. if (err)
  47. return err;
  48. old = push_signal_handler(SIGILL, hashchk_handler);
  49. if (setjmp(hashchk_detected_buf))
  50. goto out;
  51. hashchk_failure_msg = NULL;
  52. do_bad_hashchk();
  53. hashchk_failure_msg = "hashchk failed to trigger";
  54. out:
  55. pop_signal_handler(SIGILL, old);
  56. FAIL_IF_MSG(hashchk_failure_msg, hashchk_failure_msg);
  57. return 0;
  58. }
  59. #define HASH_COUNT 8
  60. static unsigned long hash_values[HASH_COUNT + 1];
  61. static void fill_hash_values(void)
  62. {
  63. for (unsigned long i = 0; i < HASH_COUNT; i++)
  64. hashst(i, &hash_values[i]);
  65. /* Used to ensure the checks uses the same addresses as the hashes */
  66. hash_values[HASH_COUNT] = (unsigned long)&hash_values;
  67. }
  68. static unsigned int count_hash_values_matches(void)
  69. {
  70. unsigned long matches = 0;
  71. for (unsigned long i = 0; i < HASH_COUNT; i++) {
  72. unsigned long orig_hash = hash_values[i];
  73. hash_values[i] = 0;
  74. hashst(i, &hash_values[i]);
  75. if (hash_values[i] == orig_hash)
  76. matches++;
  77. }
  78. return matches;
  79. }
  80. static int hashchk_exec_child(void)
  81. {
  82. ssize_t count;
  83. fill_hash_values();
  84. count = write(STDOUT_FILENO, hash_values, sizeof(hash_values));
  85. return count == sizeof(hash_values) ? 0 : EOVERFLOW;
  86. }
  87. static char *hashchk_exec_child_args[] = { "hashchk_exec_child", NULL };
  88. /*
  89. * Check that new programs get different keys so a malicious process
  90. * can't recreate a victim's hash values.
  91. */
  92. static int hashchk_exec_random_key_test(void)
  93. {
  94. pid_t pid;
  95. int err;
  96. int pipefd[2];
  97. err = require_nphie();
  98. if (err)
  99. return err;
  100. FAIL_IF_MSG(pipe(pipefd), "failed to create pipe");
  101. pid = fork();
  102. if (pid == 0) {
  103. if (dup2(pipefd[1], STDOUT_FILENO) == -1)
  104. _exit(errno);
  105. execve("/proc/self/exe", hashchk_exec_child_args, NULL);
  106. _exit(errno);
  107. }
  108. await_child_success(pid);
  109. FAIL_IF_MSG(read(pipefd[0], hash_values, sizeof(hash_values)) != sizeof(hash_values),
  110. "missing expected child output");
  111. /* Verify the child used the same hash_values address */
  112. FAIL_IF_EXIT_MSG(hash_values[HASH_COUNT] != (unsigned long)&hash_values,
  113. "bad address check");
  114. /* If all hashes are the same it means (most likely) same key */
  115. FAIL_IF_MSG(count_hash_values_matches() == HASH_COUNT, "shared key detected");
  116. return 0;
  117. }
  118. /*
  119. * Check that forks share the same key so that existing hash values
  120. * remain valid.
  121. */
  122. static int hashchk_fork_share_key_test(void)
  123. {
  124. pid_t pid;
  125. int err;
  126. err = require_nphie();
  127. if (err)
  128. return err;
  129. fill_hash_values();
  130. pid = fork();
  131. if (pid == 0) {
  132. if (count_hash_values_matches() != HASH_COUNT)
  133. _exit(1);
  134. _exit(0);
  135. }
  136. await_child_success(pid);
  137. return 0;
  138. }
  139. #define STACK_SIZE (1024 * 1024)
  140. static int hashchk_clone_child_fn(void *args)
  141. {
  142. fill_hash_values();
  143. return 0;
  144. }
  145. /*
  146. * Check that threads share the same key so that existing hash values
  147. * remain valid.
  148. */
  149. static int hashchk_clone_share_key_test(void)
  150. {
  151. void *child_stack;
  152. pid_t pid;
  153. int err;
  154. err = require_nphie();
  155. if (err)
  156. return err;
  157. child_stack = mmap(NULL, STACK_SIZE, PROT_READ | PROT_WRITE,
  158. MAP_PRIVATE | MAP_ANONYMOUS | MAP_STACK, -1, 0);
  159. FAIL_IF_MSG(child_stack == MAP_FAILED, "failed to map child stack");
  160. pid = clone(hashchk_clone_child_fn, child_stack + STACK_SIZE,
  161. CLONE_VM | SIGCHLD, NULL);
  162. await_child_success(pid);
  163. FAIL_IF_MSG(count_hash_values_matches() != HASH_COUNT,
  164. "different key detected");
  165. return 0;
  166. }
  167. int main(int argc, char *argv[])
  168. {
  169. int err = 0;
  170. if (argc >= 1 && !strcmp(argv[0], hashchk_exec_child_args[0]))
  171. return hashchk_exec_child();
  172. err |= test_harness(hashchk_detected_test, "hashchk_detected");
  173. err |= test_harness(hashchk_exec_random_key_test, "hashchk_exec_random_key");
  174. err |= test_harness(hashchk_fork_share_key_test, "hashchk_fork_share_key");
  175. err |= test_harness(hashchk_clone_share_key_test, "hashchk_clone_share_key");
  176. return err;
  177. }