ue-write-oob.cpp 2.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980
  1. /*
  2. * Copyright (c) 2021, Ben Wiederhake <BenWiederhake.GitHub@gmx.de>
  3. *
  4. * SPDX-License-Identifier: BSD-2-Clause
  5. */
  6. #include <AK/Assertions.h>
  7. #include <LibCore/ArgsParser.h>
  8. #include <stdint.h>
  9. #include <stdio.h>
  10. #include <stdlib.h>
  11. #include <sys/mman.h>
  12. static void write8(void* ptr) { *(volatile uint8_t*)ptr = 1; }
  13. static void write16(void* ptr) { *(volatile uint16_t*)ptr = 1; }
  14. static void write32(void* ptr) { *(volatile uint32_t*)ptr = 1; }
  15. static void write64(void* ptr) { *(volatile double*)ptr = 1.0; }
  16. // A u64 write might be translated by the compiler as a 32-then-32-bit write:
  17. // static void write64_bad(void* ptr) { *(volatile uint64_t*)ptr = 1.0; }
  18. // Let's hope this won't be translated like that.
  19. // Godbolt says yes: https://godbolt.org/z/1b9WGo
  20. static void run_test(void* region, ssize_t offset, size_t bits)
  21. {
  22. void* ptr = (char*)region + offset;
  23. printf("Writing to %p\n", ptr);
  24. switch (bits) {
  25. case 8:
  26. write8(ptr);
  27. break;
  28. case 16:
  29. write16(ptr);
  30. break;
  31. case 32:
  32. write32(ptr);
  33. break;
  34. case 64:
  35. write64(ptr);
  36. break;
  37. default:
  38. VERIFY_NOT_REACHED();
  39. }
  40. }
  41. int main(int argc, char** argv)
  42. {
  43. bool do_static = false;
  44. int size = 10 * PAGE_SIZE;
  45. int offset = 10 * PAGE_SIZE - 1;
  46. int bits = 16;
  47. auto args_parser = Core::ArgsParser();
  48. args_parser.set_general_help(
  49. "Access out of bounds memory; a great testcase for UserEmulator.");
  50. args_parser.add_option(do_static, "Use a static region instead of an mmap'ed region. Fixes 'size' to 10*PAGESIZE = 40960. (Default: false)", "static", 'S');
  51. args_parser.add_option(size, "The size of the region to allocate. (Default: 10*PAGESIZE = 40960)", "size", 's', "size");
  52. args_parser.add_option(offset, "The signed offset at which to start writing. (Default: 10*PAGESIZE-1 = 40959)", "offset", 'o', "offset");
  53. args_parser.add_option(bits, "Amount of bits to write in a single instruction. (Default: 16)", "bits", 'b', "bits");
  54. args_parser.parse(argc, argv);
  55. if (do_static)
  56. size = 10 * PAGE_SIZE;
  57. printf("Writing %d bits to %s region of size %d at offset %d.\n",
  58. bits, do_static ? "static" : "MMAP", size, offset);
  59. if (do_static) {
  60. // Let's just hope the linker puts nothing after it!
  61. static unsigned char region[PAGE_SIZE * 10] = { 0 };
  62. run_test(region, offset, 64);
  63. } else {
  64. void* region = mmap(nullptr, size, PROT_READ | PROT_WRITE, MAP_ANONYMOUS | MAP_PRIVATE, 0, 0);
  65. VERIFY(region);
  66. run_test(region, offset, bits);
  67. }
  68. printf("FAIL (should have caused SIGSEGV)\n");
  69. return 1;
  70. }