test_ubsan.c 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160
  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_SIGNED_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_SIGNED_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_SIGNED_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_SIGNED_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_shift_out_of_bounds(void)
  44. {
  45. volatile int neg = -1, wrap = 4;
  46. volatile int val1 = 10;
  47. volatile int val2 = INT_MAX;
  48. UBSAN_TEST(CONFIG_UBSAN_SHIFT, "negative exponent");
  49. val1 <<= neg;
  50. UBSAN_TEST(CONFIG_UBSAN_SHIFT, "left overflow");
  51. val2 <<= wrap;
  52. }
  53. static void test_ubsan_out_of_bounds(void)
  54. {
  55. volatile int i = 4, j = 5, k = -1;
  56. volatile char above[4] = { }; /* Protect surrounding memory. */
  57. volatile int arr[4];
  58. volatile char below[4] = { }; /* Protect surrounding memory. */
  59. above[0] = below[0];
  60. UBSAN_TEST(CONFIG_UBSAN_BOUNDS, "above");
  61. arr[j] = i;
  62. UBSAN_TEST(CONFIG_UBSAN_BOUNDS, "below");
  63. arr[k] = i;
  64. }
  65. enum ubsan_test_enum {
  66. UBSAN_TEST_ZERO = 0,
  67. UBSAN_TEST_ONE,
  68. UBSAN_TEST_MAX,
  69. };
  70. static void test_ubsan_load_invalid_value(void)
  71. {
  72. volatile char *dst, *src;
  73. bool val, val2, *ptr;
  74. enum ubsan_test_enum eval, eval2, *eptr;
  75. unsigned char c = 0xff;
  76. UBSAN_TEST(CONFIG_UBSAN_BOOL, "bool");
  77. dst = (char *)&val;
  78. src = &c;
  79. *dst = *src;
  80. ptr = &val2;
  81. val2 = val;
  82. UBSAN_TEST(CONFIG_UBSAN_ENUM, "enum");
  83. dst = (char *)&eval;
  84. src = &c;
  85. *dst = *src;
  86. eptr = &eval2;
  87. eval2 = eval;
  88. }
  89. static void test_ubsan_misaligned_access(void)
  90. {
  91. volatile char arr[5] __aligned(4) = {1, 2, 3, 4, 5};
  92. volatile int *ptr, val = 6;
  93. UBSAN_TEST(CONFIG_UBSAN_ALIGNMENT);
  94. ptr = (int *)(arr + 1);
  95. *ptr = val;
  96. }
  97. static const test_ubsan_fp test_ubsan_array[] = {
  98. test_ubsan_add_overflow,
  99. test_ubsan_sub_overflow,
  100. test_ubsan_mul_overflow,
  101. test_ubsan_negate_overflow,
  102. test_ubsan_shift_out_of_bounds,
  103. test_ubsan_out_of_bounds,
  104. test_ubsan_load_invalid_value,
  105. test_ubsan_misaligned_access,
  106. };
  107. /* Excluded because they Oops the module. */
  108. static __used const test_ubsan_fp skip_ubsan_array[] = {
  109. test_ubsan_divrem_overflow,
  110. };
  111. static int __init test_ubsan_init(void)
  112. {
  113. unsigned int i;
  114. for (i = 0; i < ARRAY_SIZE(test_ubsan_array); i++)
  115. test_ubsan_array[i]();
  116. return 0;
  117. }
  118. module_init(test_ubsan_init);
  119. static void __exit test_ubsan_exit(void)
  120. {
  121. /* do nothing */
  122. }
  123. module_exit(test_ubsan_exit);
  124. MODULE_AUTHOR("Jinbum Park <jinb.park7@gmail.com>");
  125. MODULE_DESCRIPTION("UBSAN unit test");
  126. MODULE_LICENSE("GPL v2");