crash.cpp 13 KB

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