test_nx.c 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181
  1. /*
  2. * test_nx.c: functional test for NX functionality
  3. *
  4. * (C) Copyright 2008 Intel Corporation
  5. * Author: Arjan van de Ven <arjan@linux.intel.com>
  6. *
  7. * This program is free software; you can redistribute it and/or
  8. * modify it under the terms of the GNU General Public License
  9. * as published by the Free Software Foundation; version 2
  10. * of the License.
  11. */
  12. #include <linux/module.h>
  13. #include <linux/sort.h>
  14. #include <asm/uaccess.h>
  15. extern int rodata_test_data;
  16. /*
  17. * This file checks 4 things:
  18. * 1) Check if the stack is not executable
  19. * 2) Check if kmalloc memory is not executable
  20. * 3) Check if the .rodata section is not executable
  21. * 4) Check if the .data section of a module is not executable
  22. *
  23. * To do this, the test code tries to execute memory in stack/kmalloc/etc,
  24. * and then checks if the expected trap happens.
  25. *
  26. * Sadly, this implies having a dynamic exception handling table entry.
  27. * ... which can be done (and will make Rusty cry)... but it can only
  28. * be done in a stand-alone module with only 1 entry total.
  29. * (otherwise we'd have to sort and that's just too messy)
  30. */
  31. /*
  32. * We want to set up an exception handling point on our stack,
  33. * which means a variable value. This function is rather dirty
  34. * and walks the exception table of the module, looking for a magic
  35. * marker and replaces it with a specific function.
  36. */
  37. static void fudze_exception_table(void *marker, void *new)
  38. {
  39. struct module *mod = THIS_MODULE;
  40. struct exception_table_entry *extable;
  41. /*
  42. * Note: This module has only 1 exception table entry,
  43. * so searching and sorting is not needed. If that changes,
  44. * this would be the place to search and re-sort the exception
  45. * table.
  46. */
  47. if (mod->num_exentries > 1) {
  48. printk(KERN_ERR "test_nx: too many exception table entries!\n");
  49. printk(KERN_ERR "test_nx: test results are not reliable.\n");
  50. return;
  51. }
  52. extable = (struct exception_table_entry *)mod->extable;
  53. extable[0].insn = (unsigned long)new;
  54. }
  55. /*
  56. * exception tables get their symbols translated so we need
  57. * to use a fake function to put in there, which we can then
  58. * replace at runtime.
  59. */
  60. void foo_label(void);
  61. /*
  62. * returns 0 for not-executable, negative for executable
  63. *
  64. * Note: we cannot allow this function to be inlined, because
  65. * that would give us more than 1 exception table entry.
  66. * This in turn would break the assumptions above.
  67. */
  68. static noinline int test_address(void *address)
  69. {
  70. unsigned long result;
  71. /* Set up an exception table entry for our address */
  72. fudze_exception_table(&foo_label, address);
  73. result = 1;
  74. asm volatile(
  75. "foo_label:\n"
  76. "0: call *%[fake_code]\n"
  77. "1:\n"
  78. ".section .fixup,\"ax\"\n"
  79. "2: mov %[zero], %[rslt]\n"
  80. " ret\n"
  81. ".previous\n"
  82. ".section __ex_table,\"a\"\n"
  83. " .align 8\n"
  84. #ifdef CONFIG_X86_32
  85. " .long 0b\n"
  86. " .long 2b\n"
  87. #else
  88. " .quad 0b\n"
  89. " .quad 2b\n"
  90. #endif
  91. ".previous\n"
  92. : [rslt] "=r" (result)
  93. : [fake_code] "r" (address), [zero] "r" (0UL), "0" (result)
  94. );
  95. /* change the exception table back for the next round */
  96. fudze_exception_table(address, &foo_label);
  97. if (result)
  98. return -ENODEV;
  99. return 0;
  100. }
  101. static unsigned char test_data = 0xC3; /* 0xC3 is the opcode for "ret" */
  102. static int test_NX(void)
  103. {
  104. int ret = 0;
  105. /* 0xC3 is the opcode for "ret" */
  106. char stackcode[] = {0xC3, 0x90, 0 };
  107. char *heap;
  108. test_data = 0xC3;
  109. printk(KERN_INFO "Testing NX protection\n");
  110. /* Test 1: check if the stack is not executable */
  111. if (test_address(&stackcode)) {
  112. printk(KERN_ERR "test_nx: stack was executable\n");
  113. ret = -ENODEV;
  114. }
  115. /* Test 2: Check if the heap is executable */
  116. heap = kmalloc(64, GFP_KERNEL);
  117. if (!heap)
  118. return -ENOMEM;
  119. heap[0] = 0xC3; /* opcode for "ret" */
  120. if (test_address(heap)) {
  121. printk(KERN_ERR "test_nx: heap was executable\n");
  122. ret = -ENODEV;
  123. }
  124. kfree(heap);
  125. /*
  126. * The following 2 tests currently fail, this needs to get fixed
  127. * Until then, don't run them to avoid too many people getting scared
  128. * by the error message
  129. */
  130. #if 0
  131. #ifdef CONFIG_DEBUG_RODATA
  132. /* Test 3: Check if the .rodata section is executable */
  133. if (rodata_test_data != 0xC3) {
  134. printk(KERN_ERR "test_nx: .rodata marker has invalid value\n");
  135. ret = -ENODEV;
  136. } else if (test_address(&rodata_test_data)) {
  137. printk(KERN_ERR "test_nx: .rodata section is executable\n");
  138. ret = -ENODEV;
  139. }
  140. #endif
  141. /* Test 4: Check if the .data section of a module is executable */
  142. if (test_address(&test_data)) {
  143. printk(KERN_ERR "test_nx: .data section is executable\n");
  144. ret = -ENODEV;
  145. }
  146. #endif
  147. return 0;
  148. }
  149. static void test_exit(void)
  150. {
  151. }
  152. module_init(test_NX);
  153. module_exit(test_exit);
  154. MODULE_LICENSE("GPL");
  155. MODULE_DESCRIPTION("Testcase for the NX infrastructure");
  156. MODULE_AUTHOR("Arjan van de Ven <arjan@linux.intel.com>");