test_ubsan.c 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132
  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_divrem_overflow(void)
  12. {
  13. volatile int val = 16;
  14. volatile int val2 = 0;
  15. UBSAN_TEST(CONFIG_UBSAN_DIV_ZERO);
  16. val /= val2;
  17. }
  18. static void test_ubsan_shift_out_of_bounds(void)
  19. {
  20. volatile int neg = -1, wrap = 4;
  21. int val1 = 10;
  22. int val2 = INT_MAX;
  23. UBSAN_TEST(CONFIG_UBSAN_SHIFT, "negative exponent");
  24. val1 <<= neg;
  25. UBSAN_TEST(CONFIG_UBSAN_SHIFT, "left overflow");
  26. val2 <<= wrap;
  27. }
  28. static void test_ubsan_out_of_bounds(void)
  29. {
  30. volatile int i = 4, j = 5, k = -1;
  31. volatile char above[4] = { }; /* Protect surrounding memory. */
  32. volatile int arr[4];
  33. volatile char below[4] = { }; /* Protect surrounding memory. */
  34. above[0] = below[0];
  35. UBSAN_TEST(CONFIG_UBSAN_BOUNDS, "above");
  36. arr[j] = i;
  37. UBSAN_TEST(CONFIG_UBSAN_BOUNDS, "below");
  38. arr[k] = i;
  39. }
  40. enum ubsan_test_enum {
  41. UBSAN_TEST_ZERO = 0,
  42. UBSAN_TEST_ONE,
  43. UBSAN_TEST_MAX,
  44. };
  45. static void test_ubsan_load_invalid_value(void)
  46. {
  47. volatile char *dst, *src;
  48. bool val, val2, *ptr;
  49. enum ubsan_test_enum eval, eval2, *eptr;
  50. unsigned char c = 0xff;
  51. UBSAN_TEST(CONFIG_UBSAN_BOOL, "bool");
  52. dst = (char *)&val;
  53. src = &c;
  54. *dst = *src;
  55. ptr = &val2;
  56. val2 = val;
  57. UBSAN_TEST(CONFIG_UBSAN_ENUM, "enum");
  58. dst = (char *)&eval;
  59. src = &c;
  60. *dst = *src;
  61. eptr = &eval2;
  62. eval2 = eval;
  63. }
  64. static void test_ubsan_null_ptr_deref(void)
  65. {
  66. volatile int *ptr = NULL;
  67. int val;
  68. UBSAN_TEST(CONFIG_UBSAN_OBJECT_SIZE);
  69. val = *ptr;
  70. }
  71. static void test_ubsan_misaligned_access(void)
  72. {
  73. volatile char arr[5] __aligned(4) = {1, 2, 3, 4, 5};
  74. volatile int *ptr, val = 6;
  75. UBSAN_TEST(CONFIG_UBSAN_ALIGNMENT);
  76. ptr = (int *)(arr + 1);
  77. *ptr = val;
  78. }
  79. static const test_ubsan_fp test_ubsan_array[] = {
  80. test_ubsan_shift_out_of_bounds,
  81. test_ubsan_out_of_bounds,
  82. test_ubsan_load_invalid_value,
  83. test_ubsan_misaligned_access,
  84. };
  85. /* Excluded because they Oops the module. */
  86. static const test_ubsan_fp skip_ubsan_array[] = {
  87. test_ubsan_divrem_overflow,
  88. test_ubsan_null_ptr_deref,
  89. };
  90. static int __init test_ubsan_init(void)
  91. {
  92. unsigned int i;
  93. for (i = 0; i < ARRAY_SIZE(test_ubsan_array); i++)
  94. test_ubsan_array[i]();
  95. return 0;
  96. }
  97. module_init(test_ubsan_init);
  98. static void __exit test_ubsan_exit(void)
  99. {
  100. /* do nothing */
  101. }
  102. module_exit(test_ubsan_exit);
  103. MODULE_AUTHOR("Jinbum Park <jinb.park7@gmail.com>");
  104. MODULE_LICENSE("GPL v2");