xref: /linux/lib/test_ubsan.c (revision 6315d93541f8a5f77c5ef5c4f25233e66d189603)
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 
14 static void test_ubsan_add_overflow(void)
15 {
16 	volatile int val = INT_MAX;
17 
18 	UBSAN_TEST(CONFIG_UBSAN_INTEGER_WRAP);
19 	val += 2;
20 }
21 
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_INTEGER_WRAP);
28 	val -= val2;
29 }
30 
31 static void test_ubsan_mul_overflow(void)
32 {
33 	volatile int val = INT_MAX / 2;
34 
35 	UBSAN_TEST(CONFIG_UBSAN_INTEGER_WRAP);
36 	val *= 3;
37 }
38 
39 static void test_ubsan_negate_overflow(void)
40 {
41 	volatile int val = INT_MIN;
42 
43 	UBSAN_TEST(CONFIG_UBSAN_INTEGER_WRAP);
44 	val = -val;
45 }
46 
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 
56 static void test_ubsan_truncate_signed(void)
57 {
58 	volatile long val = LONG_MAX;
59 	volatile int val2 = 0;
60 
61 	UBSAN_TEST(CONFIG_UBSAN_INTEGER_WRAP);
62 	val2 = val;
63 }
64 
65 static void test_ubsan_shift_out_of_bounds(void)
66 {
67 	volatile int neg = -1, wrap = 4;
68 	volatile int val1 = 10;
69 	volatile int val2 = INT_MAX;
70 
71 	UBSAN_TEST(CONFIG_UBSAN_SHIFT, "negative exponent");
72 	val1 <<= neg;
73 
74 	UBSAN_TEST(CONFIG_UBSAN_SHIFT, "left overflow");
75 	val2 <<= wrap;
76 }
77 
78 static void test_ubsan_out_of_bounds(void)
79 {
80 	volatile int i = 4, j = 5, k = -1;
81 	volatile char above[4] = { }; /* Protect surrounding memory. */
82 	volatile int arr[4];
83 	volatile char below[4] = { }; /* Protect surrounding memory. */
84 
85 	above[0] = below[0];
86 
87 	UBSAN_TEST(CONFIG_UBSAN_BOUNDS, "above");
88 	arr[j] = i;
89 
90 	UBSAN_TEST(CONFIG_UBSAN_BOUNDS, "below");
91 	arr[k] = i;
92 }
93 
94 enum ubsan_test_enum {
95 	UBSAN_TEST_ZERO = 0,
96 	UBSAN_TEST_ONE,
97 	UBSAN_TEST_MAX,
98 };
99 
100 static void test_ubsan_load_invalid_value(void)
101 {
102 	volatile char *dst, *src;
103 	bool val, val2, *ptr;
104 	enum ubsan_test_enum eval, eval2, *eptr;
105 	unsigned char c = 0xff;
106 
107 	UBSAN_TEST(CONFIG_UBSAN_BOOL, "bool");
108 	dst = (char *)&val;
109 	src = &c;
110 	*dst = *src;
111 
112 	ptr = &val2;
113 	val2 = val;
114 
115 	UBSAN_TEST(CONFIG_UBSAN_ENUM, "enum");
116 	dst = (char *)&eval;
117 	src = &c;
118 	*dst = *src;
119 
120 	eptr = &eval2;
121 	eval2 = eval;
122 }
123 
124 static void test_ubsan_misaligned_access(void)
125 {
126 	volatile char arr[5] __aligned(4) = {1, 2, 3, 4, 5};
127 	volatile int *ptr, val = 6;
128 
129 	UBSAN_TEST(CONFIG_UBSAN_ALIGNMENT);
130 	ptr = (int *)(arr + 1);
131 	*ptr = val;
132 }
133 
134 static const test_ubsan_fp test_ubsan_array[] = {
135 	test_ubsan_add_overflow,
136 	test_ubsan_sub_overflow,
137 	test_ubsan_mul_overflow,
138 	test_ubsan_negate_overflow,
139 	test_ubsan_truncate_signed,
140 	test_ubsan_shift_out_of_bounds,
141 	test_ubsan_out_of_bounds,
142 	test_ubsan_load_invalid_value,
143 	test_ubsan_misaligned_access,
144 };
145 
146 /* Excluded because they Oops the module. */
147 static __used const test_ubsan_fp skip_ubsan_array[] = {
148 	test_ubsan_divrem_overflow,
149 };
150 
151 static int __init test_ubsan_init(void)
152 {
153 	unsigned int i;
154 
155 	for (i = 0; i < ARRAY_SIZE(test_ubsan_array); i++)
156 		test_ubsan_array[i]();
157 
158 	return 0;
159 }
160 module_init(test_ubsan_init);
161 
162 static void __exit test_ubsan_exit(void)
163 {
164 	/* do nothing */
165 }
166 module_exit(test_ubsan_exit);
167 
168 MODULE_AUTHOR("Jinbum Park <jinb.park7@gmail.com>");
169 MODULE_DESCRIPTION("UBSAN unit test");
170 MODULE_LICENSE("GPL v2");
171