crash.cpp 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292
  1. /*
  2. * Copyright (c) 2018-2020, Andreas Kling <kling@serenityos.org>
  3. * Copyright (c) 2019-2020, Shannon Booth <shannon.ml.booth@gmail.com>
  4. *
  5. * SPDX-License-Identifier: BSD-2-Clause
  6. */
  7. #include <AK/Assertions.h>
  8. #include <AK/Function.h>
  9. #include <AK/String.h>
  10. #include <Kernel/IO.h>
  11. #include <LibCore/ArgsParser.h>
  12. #include <LibCore/Object.h>
  13. #include <LibTest/CrashTest.h>
  14. #include <stdio.h>
  15. #include <stdlib.h>
  16. #include <sys/mman.h>
  17. #include <sys/wait.h>
  18. #include <syscall.h>
  19. #include <unistd.h>
  20. using Test::Crash;
  21. #ifdef __clang__
  22. # pragma clang optimize off
  23. #else
  24. # pragma GCC optimize("O0")
  25. #endif
  26. int main(int argc, char** argv)
  27. {
  28. bool do_all_crash_types = false;
  29. bool do_segmentation_violation = false;
  30. bool do_division_by_zero = false;
  31. bool do_illegal_instruction = false;
  32. bool do_abort = false;
  33. bool do_write_to_uninitialized_malloc_memory = false;
  34. bool do_write_to_freed_memory = false;
  35. bool do_write_to_read_only_memory = false;
  36. bool do_read_from_uninitialized_malloc_memory = false;
  37. bool do_read_from_freed_memory = false;
  38. bool do_invalid_stack_pointer_on_syscall = false;
  39. bool do_invalid_stack_pointer_on_page_fault = false;
  40. bool do_syscall_from_writeable_memory = false;
  41. bool do_execute_non_executable_memory = false;
  42. bool do_trigger_user_mode_instruction_prevention = false;
  43. bool do_use_io_instruction = false;
  44. bool do_read_cpu_counter = false;
  45. bool do_pledge_violation = false;
  46. bool do_failing_assertion = false;
  47. bool do_deref_null_refptr = false;
  48. auto args_parser = Core::ArgsParser();
  49. args_parser.set_general_help(
  50. "Exercise error-handling paths of the execution environment "
  51. "(i.e., Kernel or UE) by crashing in many different ways.");
  52. args_parser.add_option(do_all_crash_types, "Test that all of the following crash types crash as expected", nullptr, 'A');
  53. args_parser.add_option(do_segmentation_violation, "Perform a segmentation violation by dereferencing an invalid pointer", nullptr, 's');
  54. args_parser.add_option(do_division_by_zero, "Perform a division by zero", nullptr, 'd');
  55. args_parser.add_option(do_illegal_instruction, "Execute an illegal CPU instruction", nullptr, 'i');
  56. args_parser.add_option(do_abort, "Call `abort()`", nullptr, 'a');
  57. args_parser.add_option(do_read_from_uninitialized_malloc_memory, "Read a pointer from uninitialized malloc memory, then read from it", nullptr, 'm');
  58. args_parser.add_option(do_read_from_freed_memory, "Read a pointer from memory freed using `free()`, then read from it", nullptr, 'f');
  59. args_parser.add_option(do_write_to_uninitialized_malloc_memory, "Read a pointer from uninitialized malloc memory, then write to it", nullptr, 'M');
  60. args_parser.add_option(do_write_to_freed_memory, "Read a pointer from memory freed using `free()`, then write to it", nullptr, 'F');
  61. args_parser.add_option(do_write_to_read_only_memory, "Write to read-only memory", nullptr, 'r');
  62. args_parser.add_option(do_invalid_stack_pointer_on_syscall, "Make a syscall while using an invalid stack pointer", nullptr, 'T');
  63. args_parser.add_option(do_invalid_stack_pointer_on_page_fault, "Trigger a page fault while using an invalid stack pointer", nullptr, 't');
  64. args_parser.add_option(do_syscall_from_writeable_memory, "Make a syscall from writeable memory", nullptr, 'S');
  65. args_parser.add_option(do_execute_non_executable_memory, "Attempt to execute non-executable memory (not mapped with PROT_EXEC)", nullptr, 'X');
  66. args_parser.add_option(do_trigger_user_mode_instruction_prevention, "Attempt to trigger an x86 User Mode Instruction Prevention fault", nullptr, 'U');
  67. args_parser.add_option(do_use_io_instruction, "Use an x86 I/O instruction in userspace", nullptr, 'I');
  68. args_parser.add_option(do_read_cpu_counter, "Read the x86 TSC (Time Stamp Counter) directly", nullptr, 'c');
  69. args_parser.add_option(do_pledge_violation, "Violate pledge()'d promises", nullptr, 'p');
  70. args_parser.add_option(do_failing_assertion, "Perform a failing assertion", nullptr, 'n');
  71. args_parser.add_option(do_deref_null_refptr, "Dereference a null RefPtr", nullptr, 'R');
  72. if (argc != 2) {
  73. args_parser.print_usage(stderr, argv[0]);
  74. exit(1);
  75. }
  76. args_parser.parse(argc, argv);
  77. Crash::RunType run_type = do_all_crash_types ? Crash::RunType::UsingChildProcess
  78. : Crash::RunType::UsingCurrentProcess;
  79. if (do_segmentation_violation || do_all_crash_types) {
  80. Crash("Segmentation violation", []() {
  81. volatile int* crashme = nullptr;
  82. *crashme = 0xbeef;
  83. return Crash::Failure::DidNotCrash;
  84. }).run(run_type);
  85. }
  86. if (do_division_by_zero || do_all_crash_types) {
  87. Crash("Division by zero", []() {
  88. volatile int lala = 10;
  89. volatile int zero = 0;
  90. [[maybe_unused]] volatile int test = lala / zero;
  91. return Crash::Failure::DidNotCrash;
  92. }).run(run_type);
  93. }
  94. if (do_illegal_instruction || do_all_crash_types) {
  95. Crash("Illegal instruction", []() {
  96. asm volatile("ud2");
  97. return Crash::Failure::DidNotCrash;
  98. }).run(run_type);
  99. }
  100. if (do_abort || do_all_crash_types) {
  101. Crash("Abort", []() {
  102. abort();
  103. return Crash::Failure::DidNotCrash;
  104. }).run(run_type);
  105. }
  106. if (do_read_from_uninitialized_malloc_memory || do_all_crash_types) {
  107. Crash("Read from uninitialized malloc memory", []() {
  108. auto* uninitialized_memory = (volatile u32**)malloc(1024);
  109. if (!uninitialized_memory)
  110. return Crash::Failure::UnexpectedError;
  111. [[maybe_unused]] volatile auto x = uninitialized_memory[0][0];
  112. return Crash::Failure::DidNotCrash;
  113. }).run(run_type);
  114. }
  115. if (do_read_from_freed_memory || do_all_crash_types) {
  116. Crash("Read from freed memory", []() {
  117. auto* uninitialized_memory = (volatile u32**)malloc(1024);
  118. if (!uninitialized_memory)
  119. return Crash::Failure::UnexpectedError;
  120. free(uninitialized_memory);
  121. [[maybe_unused]] volatile auto x = uninitialized_memory[4][0];
  122. return Crash::Failure::DidNotCrash;
  123. }).run(run_type);
  124. }
  125. if (do_write_to_uninitialized_malloc_memory || do_all_crash_types) {
  126. Crash("Write to uninitialized malloc memory", []() {
  127. auto* uninitialized_memory = (volatile u32**)malloc(1024);
  128. if (!uninitialized_memory)
  129. return Crash::Failure::UnexpectedError;
  130. uninitialized_memory[4][0] = 1;
  131. return Crash::Failure::DidNotCrash;
  132. }).run(run_type);
  133. }
  134. if (do_write_to_freed_memory || do_all_crash_types) {
  135. Crash("Write to freed memory", []() {
  136. auto* uninitialized_memory = (volatile u32**)malloc(1024);
  137. if (!uninitialized_memory)
  138. return Crash::Failure::UnexpectedError;
  139. free(uninitialized_memory);
  140. uninitialized_memory[4][0] = 1;
  141. return Crash::Failure::DidNotCrash;
  142. }).run(run_type);
  143. }
  144. if (do_write_to_read_only_memory || do_all_crash_types) {
  145. Crash("Write to read only memory", []() {
  146. auto* ptr = (u8*)mmap(nullptr, 4096, PROT_READ | PROT_WRITE, MAP_ANON | MAP_PRIVATE, 0, 0);
  147. if (ptr == MAP_FAILED)
  148. return Crash::Failure::UnexpectedError;
  149. *ptr = 'x'; // This should work fine.
  150. int rc = mprotect(ptr, 4096, PROT_READ);
  151. if (rc != 0 || *ptr != 'x')
  152. return Crash::Failure::UnexpectedError;
  153. *ptr = 'y'; // This should crash!
  154. return Crash::Failure::DidNotCrash;
  155. }).run(run_type);
  156. }
  157. if (do_invalid_stack_pointer_on_syscall || do_all_crash_types) {
  158. Crash("Invalid stack pointer on syscall", []() {
  159. u8* makeshift_stack = (u8*)mmap(nullptr, 0, PROT_READ | PROT_WRITE, MAP_ANONYMOUS | MAP_PRIVATE | MAP_STACK, 0, 0);
  160. if (!makeshift_stack)
  161. return Crash::Failure::UnexpectedError;
  162. u8* makeshift_esp = makeshift_stack + 2048;
  163. asm volatile("mov %%eax, %%esp" ::"a"(makeshift_esp));
  164. getuid();
  165. dbgln("Survived syscall with MAP_STACK stack");
  166. u8* bad_stack = (u8*)mmap(nullptr, PAGE_SIZE, PROT_READ | PROT_WRITE, MAP_ANONYMOUS | MAP_PRIVATE, 0, 0);
  167. if (!bad_stack)
  168. return Crash::Failure::UnexpectedError;
  169. u8* bad_esp = bad_stack + 2048;
  170. asm volatile("mov %%eax, %%esp" ::"a"(bad_esp));
  171. getuid();
  172. return Crash::Failure::DidNotCrash;
  173. }).run(run_type);
  174. }
  175. if (do_invalid_stack_pointer_on_page_fault || do_all_crash_types) {
  176. Crash("Invalid stack pointer on page fault", []() {
  177. u8* bad_stack = (u8*)mmap(nullptr, PAGE_SIZE, PROT_READ | PROT_WRITE, MAP_ANONYMOUS | MAP_PRIVATE, 0, 0);
  178. if (!bad_stack)
  179. return Crash::Failure::UnexpectedError;
  180. u8* bad_esp = bad_stack + 2048;
  181. #ifndef __LP64__
  182. asm volatile("mov %%eax, %%esp" ::"a"(bad_esp));
  183. asm volatile("pushl $0");
  184. #else
  185. asm volatile("movq %%rax, %%rsp" ::"a"(bad_esp));
  186. asm volatile("pushq $0");
  187. #endif
  188. return Crash::Failure::DidNotCrash;
  189. }).run(run_type);
  190. }
  191. if (do_syscall_from_writeable_memory || do_all_crash_types) {
  192. Crash("Syscall from writable memory", []() {
  193. u8 buffer[] = { 0xb8, Syscall::SC_getuid, 0, 0, 0, 0xcd, 0x82 };
  194. ((void (*)())buffer)();
  195. return Crash::Failure::DidNotCrash;
  196. }).run(run_type);
  197. }
  198. if (do_execute_non_executable_memory || do_all_crash_types) {
  199. Crash("Execute non executable memory", []() {
  200. auto* ptr = (u8*)mmap(nullptr, PAGE_SIZE, PROT_READ | PROT_WRITE, MAP_ANONYMOUS | MAP_PRIVATE, 0, 0);
  201. if (ptr == MAP_FAILED)
  202. return Crash::Failure::UnexpectedError;
  203. ptr[0] = 0xc3; // ret
  204. typedef void* (*CrashyFunctionPtr)();
  205. ((CrashyFunctionPtr)ptr)();
  206. return Crash::Failure::DidNotCrash;
  207. }).run(run_type);
  208. }
  209. if (do_trigger_user_mode_instruction_prevention || do_all_crash_types) {
  210. Crash("Trigger x86 User Mode Instruction Prevention", []() {
  211. asm volatile("str %eax");
  212. return Crash::Failure::DidNotCrash;
  213. }).run(run_type);
  214. }
  215. if (do_use_io_instruction || do_all_crash_types) {
  216. Crash("Attempt to use an I/O instruction", [] {
  217. u8 keyboard_status = IO::in8(0x64);
  218. outln("Keyboard status: {:#02x}", keyboard_status);
  219. return Crash::Failure::DidNotCrash;
  220. }).run(run_type);
  221. }
  222. if (do_read_cpu_counter || do_all_crash_types) {
  223. Crash("Read the CPU timestamp counter", [] {
  224. asm volatile("rdtsc");
  225. return Crash::Failure::DidNotCrash;
  226. }).run(run_type);
  227. }
  228. if (do_pledge_violation || do_all_crash_types) {
  229. Crash("Violate pledge()'d promises", [] {
  230. if (pledge("", nullptr) < 0) {
  231. perror("pledge");
  232. return Crash::Failure::DidNotCrash;
  233. }
  234. outln("Didn't pledge 'stdio', this should fail!");
  235. return Crash::Failure::DidNotCrash;
  236. }).run(run_type);
  237. }
  238. if (do_failing_assertion || do_all_crash_types) {
  239. Crash("Perform a failing assertion", [] {
  240. VERIFY(1 == 2);
  241. return Crash::Failure::DidNotCrash;
  242. }).run(run_type);
  243. }
  244. if (do_deref_null_refptr || do_all_crash_types) {
  245. Crash("Dereference a null RefPtr", [] {
  246. RefPtr<Core::Object> p;
  247. *p;
  248. return Crash::Failure::DidNotCrash;
  249. }).run(run_type);
  250. }
  251. return 0;
  252. }