test_ubsan.c 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174
  1. // SPDX-License-Identifier: GPL-2.0
  2. #include <linux/init.h>
  3. #include <linux/kernel.h>
  4. #include <linux/module.h>
  5. typedef void(*test_ubsan_fp)(void);
  6. #define UBSAN_TEST(config, ...) do { \
  7. pr_info("%s " __VA_ARGS__ "%s(%s=%s)\n", __func__, \
  8. sizeof(" " __VA_ARGS__) > 2 ? " " : "", \
  9. #config, IS_ENABLED(config) ? "y" : "n"); \
  10. } while (0)
  11. static void test_ubsan_add_overflow(void)
  12. {
  13. volatile int val = INT_MAX;
  14. UBSAN_TEST(CONFIG_UBSAN_INTEGER_WRAP);
  15. val += 2;
  16. }
  17. static void test_ubsan_sub_overflow(void)
  18. {
  19. volatile int val = INT_MIN;
  20. volatile int val2 = 2;
  21. UBSAN_TEST(CONFIG_UBSAN_INTEGER_WRAP);
  22. val -= val2;
  23. }
  24. static void test_ubsan_mul_overflow(void)
  25. {
  26. volatile int val = INT_MAX / 2;
  27. UBSAN_TEST(CONFIG_UBSAN_INTEGER_WRAP);
  28. val *= 3;
  29. }
  30. static void test_ubsan_negate_overflow(void)
  31. {
  32. volatile int val = INT_MIN;
  33. UBSAN_TEST(CONFIG_UBSAN_INTEGER_WRAP);
  34. val = -val;
  35. }
  36. static void test_ubsan_divrem_overflow(void)
  37. {
  38. volatile int val = 16;
  39. volatile int val2 = 0;
  40. UBSAN_TEST(CONFIG_UBSAN_DIV_ZERO);
  41. val /= val2;
  42. }
  43. static void test_ubsan_truncate_signed(void)
  44. {
  45. volatile long val = LONG_MAX;
  46. volatile int val2 = 0;
  47. UBSAN_TEST(CONFIG_UBSAN_INTEGER_WRAP);
  48. val2 = val;
  49. }
  50. static void test_ubsan_shift_out_of_bounds(void)
  51. {
  52. volatile int neg = -1, wrap = 4;
  53. volatile int val1 = 10;
  54. volatile int val2 = INT_MAX;
  55. UBSAN_TEST(CONFIG_UBSAN_SHIFT, "negative exponent");
  56. val1 <<= neg;
  57. UBSAN_TEST(CONFIG_UBSAN_SHIFT, "left overflow");
  58. val2 <<= wrap;
  59. }
  60. static void test_ubsan_out_of_bounds(void)
  61. {
  62. int i = 4, j = 4, k = -1;
  63. volatile struct {
  64. char above[4]; /* Protect surrounding memory. */
  65. int arr[4];
  66. char below[4]; /* Protect surrounding memory. */
  67. } data;
  68. OPTIMIZER_HIDE_VAR(i);
  69. OPTIMIZER_HIDE_VAR(j);
  70. OPTIMIZER_HIDE_VAR(k);
  71. UBSAN_TEST(CONFIG_UBSAN_BOUNDS, "above");
  72. data.arr[j] = i;
  73. UBSAN_TEST(CONFIG_UBSAN_BOUNDS, "below");
  74. data.arr[k] = i;
  75. }
  76. enum ubsan_test_enum {
  77. UBSAN_TEST_ZERO = 0,
  78. UBSAN_TEST_ONE,
  79. UBSAN_TEST_MAX,
  80. };
  81. static void test_ubsan_load_invalid_value(void)
  82. {
  83. volatile char *dst, *src;
  84. bool val, val2, *ptr;
  85. enum ubsan_test_enum eval, eval2, *eptr;
  86. unsigned char c = 0xff;
  87. UBSAN_TEST(CONFIG_UBSAN_BOOL, "bool");
  88. dst = (char *)&val;
  89. src = &c;
  90. *dst = *src;
  91. ptr = &val2;
  92. val2 = val;
  93. UBSAN_TEST(CONFIG_UBSAN_ENUM, "enum");
  94. dst = (char *)&eval;
  95. src = &c;
  96. *dst = *src;
  97. eptr = &eval2;
  98. eval2 = eval;
  99. }
  100. static void test_ubsan_misaligned_access(void)
  101. {
  102. volatile char arr[5] __aligned(4) = {1, 2, 3, 4, 5};
  103. volatile int *ptr, val = 6;
  104. UBSAN_TEST(CONFIG_UBSAN_ALIGNMENT);
  105. ptr = (int *)(arr + 1);
  106. *ptr = val;
  107. }
  108. static const test_ubsan_fp test_ubsan_array[] = {
  109. test_ubsan_add_overflow,
  110. test_ubsan_sub_overflow,
  111. test_ubsan_mul_overflow,
  112. test_ubsan_negate_overflow,
  113. test_ubsan_truncate_signed,
  114. test_ubsan_shift_out_of_bounds,
  115. test_ubsan_out_of_bounds,
  116. test_ubsan_load_invalid_value,
  117. test_ubsan_misaligned_access,
  118. };
  119. /* Excluded because they Oops the module. */
  120. static __used const test_ubsan_fp skip_ubsan_array[] = {
  121. test_ubsan_divrem_overflow,
  122. };
  123. static int __init test_ubsan_init(void)
  124. {
  125. unsigned int i;
  126. for (i = 0; i < ARRAY_SIZE(test_ubsan_array); i++)
  127. test_ubsan_array[i]();
  128. return 0;
  129. }
  130. module_init(test_ubsan_init);
  131. static void __exit test_ubsan_exit(void)
  132. {
  133. /* do nothing */
  134. }
  135. module_exit(test_ubsan_exit);
  136. MODULE_AUTHOR("Jinbum Park <jinb.park7@gmail.com>");
  137. MODULE_DESCRIPTION("UBSAN unit test");
  138. MODULE_LICENSE("GPL v2");