write-oob.cpp 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100
  1. /*
  2. * Copyright (c) 2021, Ben Wiederhake <BenWiederhake.GitHub@gmx.de>
  3. * All rights reserved.
  4. *
  5. * Redistribution and use in source and binary forms, with or without
  6. * modification, are permitted provided that the following conditions are met:
  7. *
  8. * 1. Redistributions of source code must retain the above copyright notice, this
  9. * list of conditions and the following disclaimer.
  10. *
  11. * 2. Redistributions in binary form must reproduce the above copyright notice,
  12. * this list of conditions and the following disclaimer in the documentation
  13. * and/or other materials provided with the distribution.
  14. *
  15. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
  16. * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  17. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
  18. * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
  19. * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  20. * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
  21. * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
  22. * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
  23. * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  24. * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  25. */
  26. #include <AK/Assertions.h>
  27. #include <LibCore/ArgsParser.h>
  28. #include <mman.h>
  29. #include <stdint.h>
  30. #include <stdio.h>
  31. #include <stdlib.h>
  32. static void write8(void* ptr) { *(volatile uint8_t*)ptr = 1; }
  33. static void write16(void* ptr) { *(volatile uint16_t*)ptr = 1; }
  34. static void write32(void* ptr) { *(volatile uint32_t*)ptr = 1; }
  35. static void write64(void* ptr) { *(volatile double*)ptr = 1.0; }
  36. // A u64 write might be translated by the compiler as a 32-then-32-bit write:
  37. // static void write64_bad(void* ptr) { *(volatile uint64_t*)ptr = 1.0; }
  38. // Let's hope this won't be translated like that.
  39. // Godbolt says yes: https://godbolt.org/z/1b9WGo
  40. static void run_test(void* region, ssize_t offset, size_t bits)
  41. {
  42. void* ptr = (char*)region + offset;
  43. printf("Writing to %p\n", ptr);
  44. switch (bits) {
  45. case 8:
  46. write8(ptr);
  47. break;
  48. case 16:
  49. write16(ptr);
  50. break;
  51. case 32:
  52. write32(ptr);
  53. break;
  54. case 64:
  55. write64(ptr);
  56. break;
  57. default:
  58. ASSERT_NOT_REACHED();
  59. }
  60. }
  61. int main(int argc, char** argv)
  62. {
  63. bool do_static = false;
  64. int size = 10 * PAGE_SIZE;
  65. int offset = 10 * PAGE_SIZE - 1;
  66. int bits = 16;
  67. auto args_parser = Core::ArgsParser();
  68. args_parser.set_general_help(
  69. "Access out of bounds memory; a great testcase for UserEmulator.");
  70. 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');
  71. args_parser.add_option(size, "The size of the region to allocate. (Default: 10*PAGESIZE = 40960)", "size", 's', "size");
  72. args_parser.add_option(offset, "The signed offset at which to start writing. (Default: 10*PAGESIZE-1 = 40959)", "offset", 'o', "offset");
  73. args_parser.add_option(bits, "Amount of bits to write in a single instruction. (Default: 16)", "bits", 'b', "bits");
  74. args_parser.parse(argc, argv);
  75. if (do_static)
  76. size = 10 * PAGE_SIZE;
  77. printf("Writing %d bits to %s region of size %d at offset %d.\n",
  78. bits, do_static ? "static" : "MMAP", size, offset);
  79. if (do_static) {
  80. // Let's just hope the linker puts nothing after it!
  81. static unsigned char region[PAGE_SIZE * 10] = { 0 };
  82. run_test(region, offset, 64);
  83. } else {
  84. void* region = mmap(nullptr, size, PROT_READ | PROT_WRITE, MAP_ANONYMOUS | MAP_PRIVATE, 0, 0);
  85. ASSERT(region);
  86. run_test(region, offset, bits);
  87. }
  88. printf("FAIL (should have caused SIGSEGV)\n");
  89. return 1;
  90. }