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