lkdtm_heap.c 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143
  1. /*
  2. * This is for all the tests relating directly to heap memory, including
  3. * page allocation and slab allocations.
  4. */
  5. #include "lkdtm.h"
  6. #include <linux/slab.h>
  7. #include <linux/sched.h>
  8. /*
  9. * This tries to stay within the next largest power-of-2 kmalloc cache
  10. * to avoid actually overwriting anything important if it's not detected
  11. * correctly.
  12. */
  13. void lkdtm_OVERWRITE_ALLOCATION(void)
  14. {
  15. size_t len = 1020;
  16. u32 *data = kmalloc(len, GFP_KERNEL);
  17. data[1024 / sizeof(u32)] = 0x12345678;
  18. kfree(data);
  19. }
  20. void lkdtm_WRITE_AFTER_FREE(void)
  21. {
  22. int *base, *again;
  23. size_t len = 1024;
  24. /*
  25. * The slub allocator uses the first word to store the free
  26. * pointer in some configurations. Use the middle of the
  27. * allocation to avoid running into the freelist
  28. */
  29. size_t offset = (len / sizeof(*base)) / 2;
  30. base = kmalloc(len, GFP_KERNEL);
  31. pr_info("Allocated memory %p-%p\n", base, &base[offset * 2]);
  32. pr_info("Attempting bad write to freed memory at %p\n",
  33. &base[offset]);
  34. kfree(base);
  35. base[offset] = 0x0abcdef0;
  36. /* Attempt to notice the overwrite. */
  37. again = kmalloc(len, GFP_KERNEL);
  38. kfree(again);
  39. if (again != base)
  40. pr_info("Hmm, didn't get the same memory range.\n");
  41. }
  42. void lkdtm_READ_AFTER_FREE(void)
  43. {
  44. int *base, *val, saw;
  45. size_t len = 1024;
  46. /*
  47. * The slub allocator uses the first word to store the free
  48. * pointer in some configurations. Use the middle of the
  49. * allocation to avoid running into the freelist
  50. */
  51. size_t offset = (len / sizeof(*base)) / 2;
  52. base = kmalloc(len, GFP_KERNEL);
  53. if (!base) {
  54. pr_info("Unable to allocate base memory.\n");
  55. return;
  56. }
  57. val = kmalloc(len, GFP_KERNEL);
  58. if (!val) {
  59. pr_info("Unable to allocate val memory.\n");
  60. kfree(base);
  61. return;
  62. }
  63. *val = 0x12345678;
  64. base[offset] = *val;
  65. pr_info("Value in memory before free: %x\n", base[offset]);
  66. kfree(base);
  67. pr_info("Attempting bad read from freed memory\n");
  68. saw = base[offset];
  69. if (saw != *val) {
  70. /* Good! Poisoning happened, so declare a win. */
  71. pr_info("Memory correctly poisoned (%x)\n", saw);
  72. BUG();
  73. }
  74. pr_info("Memory was not poisoned\n");
  75. kfree(val);
  76. }
  77. void lkdtm_WRITE_BUDDY_AFTER_FREE(void)
  78. {
  79. unsigned long p = __get_free_page(GFP_KERNEL);
  80. if (!p) {
  81. pr_info("Unable to allocate free page\n");
  82. return;
  83. }
  84. pr_info("Writing to the buddy page before free\n");
  85. memset((void *)p, 0x3, PAGE_SIZE);
  86. free_page(p);
  87. schedule();
  88. pr_info("Attempting bad write to the buddy page after free\n");
  89. memset((void *)p, 0x78, PAGE_SIZE);
  90. /* Attempt to notice the overwrite. */
  91. p = __get_free_page(GFP_KERNEL);
  92. free_page(p);
  93. schedule();
  94. }
  95. void lkdtm_READ_BUDDY_AFTER_FREE(void)
  96. {
  97. unsigned long p = __get_free_page(GFP_KERNEL);
  98. int saw, *val;
  99. int *base;
  100. if (!p) {
  101. pr_info("Unable to allocate free page\n");
  102. return;
  103. }
  104. val = kmalloc(1024, GFP_KERNEL);
  105. if (!val) {
  106. pr_info("Unable to allocate val memory.\n");
  107. free_page(p);
  108. return;
  109. }
  110. base = (int *)p;
  111. *val = 0x12345678;
  112. base[0] = *val;
  113. pr_info("Value in memory before free: %x\n", base[0]);
  114. free_page(p);
  115. pr_info("Attempting to read from freed memory\n");
  116. saw = base[0];
  117. if (saw != *val) {
  118. /* Good! Poisoning happened, so declare a win. */
  119. pr_info("Memory correctly poisoned (%x)\n", saw);
  120. BUG();
  121. }
  122. pr_info("Buddy page was not poisoned\n");
  123. kfree(val);
  124. }