crash.cpp 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305
  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/Arch/x86/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_legitimate_syscall = false;
  42. bool do_execute_non_executable_memory = false;
  43. bool do_trigger_user_mode_instruction_prevention = false;
  44. bool do_use_io_instruction = false;
  45. bool do_read_cpu_counter = false;
  46. bool do_pledge_violation = false;
  47. bool do_failing_assertion = false;
  48. bool do_deref_null_refptr = false;
  49. auto args_parser = Core::ArgsParser();
  50. args_parser.set_general_help(
  51. "Exercise error-handling paths of the execution environment "
  52. "(i.e., Kernel or UE) by crashing in many different ways.");
  53. args_parser.add_option(do_all_crash_types, "Test that all (except -U) of the following crash types crash as expected (default behavior)", nullptr, 'A');
  54. args_parser.add_option(do_segmentation_violation, "Perform a segmentation violation by dereferencing an invalid pointer", nullptr, 's');
  55. args_parser.add_option(do_division_by_zero, "Perform a division by zero", nullptr, 'd');
  56. args_parser.add_option(do_illegal_instruction, "Execute an illegal CPU instruction", nullptr, 'i');
  57. args_parser.add_option(do_abort, "Call `abort()`", nullptr, 'a');
  58. args_parser.add_option(do_read_from_uninitialized_malloc_memory, "Read a pointer from uninitialized malloc memory, then read from it", nullptr, 'm');
  59. args_parser.add_option(do_read_from_freed_memory, "Read a pointer from memory freed using `free()`, then read from it", nullptr, 'f');
  60. args_parser.add_option(do_write_to_uninitialized_malloc_memory, "Read a pointer from uninitialized malloc memory, then write to it", nullptr, 'M');
  61. args_parser.add_option(do_write_to_freed_memory, "Read a pointer from memory freed using `free()`, then write to it", nullptr, 'F');
  62. args_parser.add_option(do_write_to_read_only_memory, "Write to read-only memory", nullptr, 'r');
  63. args_parser.add_option(do_invalid_stack_pointer_on_syscall, "Make a syscall while using an invalid stack pointer", nullptr, 'T');
  64. args_parser.add_option(do_invalid_stack_pointer_on_page_fault, "Trigger a page fault while using an invalid stack pointer", nullptr, 't');
  65. args_parser.add_option(do_syscall_from_writeable_memory, "Make a syscall from writeable memory", nullptr, 'S');
  66. args_parser.add_option(do_legitimate_syscall, "Make a syscall from legitimate memory (but outside msyscall)", nullptr, 'y');
  67. args_parser.add_option(do_execute_non_executable_memory, "Attempt to execute non-executable memory (not mapped with PROT_EXEC)", nullptr, 'X');
  68. args_parser.add_option(do_trigger_user_mode_instruction_prevention, "Attempt to trigger an x86 User Mode Instruction Prevention fault. WARNING: This test runs only when invoked manually, see #10042.", nullptr, 'U');
  69. args_parser.add_option(do_use_io_instruction, "Use an x86 I/O instruction in userspace", nullptr, 'I');
  70. args_parser.add_option(do_read_cpu_counter, "Read the x86 TSC (Time Stamp Counter) directly", nullptr, 'c');
  71. args_parser.add_option(do_pledge_violation, "Violate pledge()'d promises", nullptr, 'p');
  72. args_parser.add_option(do_failing_assertion, "Perform a failing assertion", nullptr, 'n');
  73. args_parser.add_option(do_deref_null_refptr, "Dereference a null RefPtr", nullptr, 'R');
  74. if (argc == 1) {
  75. do_all_crash_types = true;
  76. } else if (argc != 2) {
  77. args_parser.print_usage(stderr, argv[0]);
  78. exit(1);
  79. }
  80. args_parser.parse(argc, argv);
  81. Crash::RunType run_type = do_all_crash_types ? Crash::RunType::UsingChildProcess
  82. : Crash::RunType::UsingCurrentProcess;
  83. bool any_failures = false;
  84. if (do_segmentation_violation || do_all_crash_types) {
  85. any_failures |= !Crash("Segmentation violation", []() {
  86. volatile int* crashme = nullptr;
  87. *crashme = 0xbeef;
  88. return Crash::Failure::DidNotCrash;
  89. }).run(run_type);
  90. }
  91. if (do_division_by_zero || do_all_crash_types) {
  92. any_failures |= !Crash("Division by zero", []() {
  93. volatile int lala = 10;
  94. volatile int zero = 0;
  95. [[maybe_unused]] volatile int test = lala / zero;
  96. return Crash::Failure::DidNotCrash;
  97. }).run(run_type);
  98. }
  99. if (do_illegal_instruction || do_all_crash_types) {
  100. any_failures |= !Crash("Illegal instruction", []() {
  101. asm volatile("ud2");
  102. return Crash::Failure::DidNotCrash;
  103. }).run(run_type);
  104. }
  105. if (do_abort || do_all_crash_types) {
  106. any_failures |= !Crash("Abort", []() {
  107. abort();
  108. return Crash::Failure::DidNotCrash;
  109. }).run(run_type);
  110. }
  111. if (do_read_from_uninitialized_malloc_memory || do_all_crash_types) {
  112. any_failures |= !Crash("Read from uninitialized malloc memory", []() {
  113. auto* uninitialized_memory = (volatile u32**)malloc(1024);
  114. if (!uninitialized_memory)
  115. return Crash::Failure::UnexpectedError;
  116. [[maybe_unused]] volatile auto x = uninitialized_memory[0][0];
  117. return Crash::Failure::DidNotCrash;
  118. }).run(run_type);
  119. }
  120. if (do_read_from_freed_memory || do_all_crash_types) {
  121. any_failures |= !Crash("Read from freed memory", []() {
  122. auto* uninitialized_memory = (volatile u32**)malloc(1024);
  123. if (!uninitialized_memory)
  124. return Crash::Failure::UnexpectedError;
  125. free(uninitialized_memory);
  126. [[maybe_unused]] volatile auto x = uninitialized_memory[4][0];
  127. return Crash::Failure::DidNotCrash;
  128. }).run(run_type);
  129. }
  130. if (do_write_to_uninitialized_malloc_memory || do_all_crash_types) {
  131. any_failures |= !Crash("Write to uninitialized malloc memory", []() {
  132. auto* uninitialized_memory = (volatile u32**)malloc(1024);
  133. if (!uninitialized_memory)
  134. return Crash::Failure::UnexpectedError;
  135. uninitialized_memory[4][0] = 1;
  136. return Crash::Failure::DidNotCrash;
  137. }).run(run_type);
  138. }
  139. if (do_write_to_freed_memory || do_all_crash_types) {
  140. any_failures |= !Crash("Write to freed memory", []() {
  141. auto* uninitialized_memory = (volatile u32**)malloc(1024);
  142. if (!uninitialized_memory)
  143. return Crash::Failure::UnexpectedError;
  144. free(uninitialized_memory);
  145. uninitialized_memory[4][0] = 1;
  146. return Crash::Failure::DidNotCrash;
  147. }).run(run_type);
  148. }
  149. if (do_write_to_read_only_memory || do_all_crash_types) {
  150. any_failures |= !Crash("Write to read only memory", []() {
  151. auto* ptr = (u8*)mmap(nullptr, 4096, PROT_READ | PROT_WRITE, MAP_ANON | MAP_PRIVATE, 0, 0);
  152. if (ptr == MAP_FAILED)
  153. return Crash::Failure::UnexpectedError;
  154. *ptr = 'x'; // This should work fine.
  155. int rc = mprotect(ptr, 4096, PROT_READ);
  156. if (rc != 0 || *ptr != 'x')
  157. return Crash::Failure::UnexpectedError;
  158. *ptr = 'y'; // This should crash!
  159. return Crash::Failure::DidNotCrash;
  160. }).run(run_type);
  161. }
  162. if (do_invalid_stack_pointer_on_syscall || do_all_crash_types) {
  163. any_failures |= !Crash("Invalid stack pointer on syscall", []() {
  164. u8* makeshift_stack = (u8*)mmap(nullptr, 0, PROT_READ | PROT_WRITE, MAP_ANONYMOUS | MAP_PRIVATE | MAP_STACK, 0, 0);
  165. if (!makeshift_stack)
  166. return Crash::Failure::UnexpectedError;
  167. u8* makeshift_esp = makeshift_stack + 2048;
  168. asm volatile("mov %%eax, %%esp" ::"a"(makeshift_esp));
  169. getuid();
  170. dbgln("Survived syscall with MAP_STACK stack");
  171. u8* bad_stack = (u8*)mmap(nullptr, PAGE_SIZE, PROT_READ | PROT_WRITE, MAP_ANONYMOUS | MAP_PRIVATE, 0, 0);
  172. if (!bad_stack)
  173. return Crash::Failure::UnexpectedError;
  174. u8* bad_esp = bad_stack + 2048;
  175. asm volatile("mov %%eax, %%esp" ::"a"(bad_esp));
  176. getuid();
  177. return Crash::Failure::DidNotCrash;
  178. }).run(run_type);
  179. }
  180. if (do_invalid_stack_pointer_on_page_fault || do_all_crash_types) {
  181. any_failures |= !Crash("Invalid stack pointer on page fault", []() {
  182. u8* bad_stack = (u8*)mmap(nullptr, PAGE_SIZE, PROT_READ | PROT_WRITE, MAP_ANONYMOUS | MAP_PRIVATE, 0, 0);
  183. if (!bad_stack)
  184. return Crash::Failure::UnexpectedError;
  185. u8* bad_esp = bad_stack + 2048;
  186. #if ARCH(I386)
  187. asm volatile("mov %%eax, %%esp" ::"a"(bad_esp));
  188. asm volatile("pushl $0");
  189. #else
  190. asm volatile("movq %%rax, %%rsp" ::"a"(bad_esp));
  191. asm volatile("pushq $0");
  192. #endif
  193. return Crash::Failure::DidNotCrash;
  194. }).run(run_type);
  195. }
  196. if (do_syscall_from_writeable_memory || do_all_crash_types) {
  197. any_failures |= !Crash("Syscall from writable memory", []() {
  198. u8 buffer[] = { 0xb8, Syscall::SC_getuid, 0, 0, 0, 0xcd, 0x82 };
  199. ((void (*)())buffer)();
  200. return Crash::Failure::DidNotCrash;
  201. }).run(run_type);
  202. }
  203. if (do_legitimate_syscall || do_all_crash_types) {
  204. any_failures |= !Crash("Regular syscall from outside msyscall", []() {
  205. // Since 'crash' is dynamically linked, and DynamicLoader only allows LibSystem to make syscalls, this should kill us:
  206. Syscall::invoke(Syscall::SC_getuid);
  207. return Crash::Failure::DidNotCrash;
  208. }).run(run_type);
  209. }
  210. if (do_execute_non_executable_memory || do_all_crash_types) {
  211. any_failures |= !Crash("Execute non executable memory", []() {
  212. auto* ptr = (u8*)mmap(nullptr, PAGE_SIZE, PROT_READ | PROT_WRITE, MAP_ANONYMOUS | MAP_PRIVATE, 0, 0);
  213. if (ptr == MAP_FAILED)
  214. return Crash::Failure::UnexpectedError;
  215. ptr[0] = 0xc3; // ret
  216. typedef void* (*CrashyFunctionPtr)();
  217. ((CrashyFunctionPtr)ptr)();
  218. return Crash::Failure::DidNotCrash;
  219. }).run(run_type);
  220. }
  221. if (do_trigger_user_mode_instruction_prevention) {
  222. any_failures |= !Crash("Trigger x86 User Mode Instruction Prevention", []() {
  223. asm volatile("str %eax");
  224. return Crash::Failure::DidNotCrash;
  225. }).run(run_type);
  226. }
  227. if (do_use_io_instruction || do_all_crash_types) {
  228. any_failures |= !Crash("Attempt to use an I/O instruction", [] {
  229. u8 keyboard_status = IO::in8(0x64);
  230. outln("Keyboard status: {:#02x}", keyboard_status);
  231. return Crash::Failure::DidNotCrash;
  232. }).run(run_type);
  233. }
  234. if (do_read_cpu_counter || do_all_crash_types) {
  235. any_failures |= !Crash("Read the CPU timestamp counter", [] {
  236. asm volatile("rdtsc");
  237. return Crash::Failure::DidNotCrash;
  238. }).run(run_type);
  239. }
  240. if (do_pledge_violation || do_all_crash_types) {
  241. any_failures |= !Crash("Violate pledge()'d promises", [] {
  242. if (pledge("", nullptr) < 0) {
  243. perror("pledge");
  244. return Crash::Failure::DidNotCrash;
  245. }
  246. outln("Didn't pledge 'stdio', this should fail!");
  247. return Crash::Failure::DidNotCrash;
  248. }).run(run_type);
  249. }
  250. if (do_failing_assertion || do_all_crash_types) {
  251. any_failures |= !Crash("Perform a failing assertion", [] {
  252. VERIFY(1 == 2);
  253. return Crash::Failure::DidNotCrash;
  254. }).run(run_type);
  255. }
  256. if (do_deref_null_refptr || do_all_crash_types) {
  257. any_failures |= !Crash("Dereference a null RefPtr", [] {
  258. RefPtr<Core::Object> p;
  259. *p;
  260. return Crash::Failure::DidNotCrash;
  261. }).run(run_type);
  262. }
  263. return any_failures;
  264. }