xref: /openbmc/linux/lib/test_ubsan.c (revision 03ab8e6297acd1bc0eedaa050e2a1635c576fd11)
1854686f4SJinbum Park // SPDX-License-Identifier: GPL-2.0
2854686f4SJinbum Park #include <linux/init.h>
3854686f4SJinbum Park #include <linux/kernel.h>
4854686f4SJinbum Park #include <linux/module.h>
5854686f4SJinbum Park 
6854686f4SJinbum Park typedef void(*test_ubsan_fp)(void);
7854686f4SJinbum Park 
8*4a26f49bSKees Cook #define UBSAN_TEST(config, ...)	do {					\
9*4a26f49bSKees Cook 		pr_info("%s " __VA_ARGS__ "%s(%s=%s)\n", __func__,	\
10*4a26f49bSKees Cook 			sizeof(" " __VA_ARGS__) > 2 ? " " : "",		\
11*4a26f49bSKees Cook 			#config, IS_ENABLED(config) ? "y" : "n");	\
12*4a26f49bSKees Cook 	} while (0)
13*4a26f49bSKees Cook 
test_ubsan_divrem_overflow(void)14854686f4SJinbum Park static void test_ubsan_divrem_overflow(void)
15854686f4SJinbum Park {
16854686f4SJinbum Park 	volatile int val = 16;
17854686f4SJinbum Park 	volatile int val2 = 0;
18854686f4SJinbum Park 
19*4a26f49bSKees Cook 	UBSAN_TEST(CONFIG_UBSAN_DIV_ZERO);
20854686f4SJinbum Park 	val /= val2;
21854686f4SJinbum Park }
22854686f4SJinbum Park 
test_ubsan_shift_out_of_bounds(void)23854686f4SJinbum Park static void test_ubsan_shift_out_of_bounds(void)
24854686f4SJinbum Park {
25*4a26f49bSKees Cook 	volatile int neg = -1, wrap = 4;
26*4a26f49bSKees Cook 	int val1 = 10;
27*4a26f49bSKees Cook 	int val2 = INT_MAX;
28854686f4SJinbum Park 
29*4a26f49bSKees Cook 	UBSAN_TEST(CONFIG_UBSAN_SHIFT, "negative exponent");
30*4a26f49bSKees Cook 	val1 <<= neg;
31*4a26f49bSKees Cook 
32*4a26f49bSKees Cook 	UBSAN_TEST(CONFIG_UBSAN_SHIFT, "left overflow");
33*4a26f49bSKees Cook 	val2 <<= wrap;
34854686f4SJinbum Park }
35854686f4SJinbum Park 
test_ubsan_out_of_bounds(void)36854686f4SJinbum Park static void test_ubsan_out_of_bounds(void)
37854686f4SJinbum Park {
38*4a26f49bSKees Cook 	volatile int i = 4, j = 5, k = -1;
39*4a26f49bSKees Cook 	volatile char above[4] = { }; /* Protect surrounding memory. */
409d7ca61bSOlof Johansson 	volatile int arr[4];
41*4a26f49bSKees Cook 	volatile char below[4] = { }; /* Protect surrounding memory. */
42854686f4SJinbum Park 
43*4a26f49bSKees Cook 	above[0] = below[0];
44*4a26f49bSKees Cook 
45*4a26f49bSKees Cook 	UBSAN_TEST(CONFIG_UBSAN_BOUNDS, "above");
46854686f4SJinbum Park 	arr[j] = i;
47*4a26f49bSKees Cook 
48*4a26f49bSKees Cook 	UBSAN_TEST(CONFIG_UBSAN_BOUNDS, "below");
49*4a26f49bSKees Cook 	arr[k] = i;
50854686f4SJinbum Park }
51854686f4SJinbum Park 
52*4a26f49bSKees Cook enum ubsan_test_enum {
53*4a26f49bSKees Cook 	UBSAN_TEST_ZERO = 0,
54*4a26f49bSKees Cook 	UBSAN_TEST_ONE,
55*4a26f49bSKees Cook 	UBSAN_TEST_MAX,
56*4a26f49bSKees Cook };
57*4a26f49bSKees Cook 
test_ubsan_load_invalid_value(void)58854686f4SJinbum Park static void test_ubsan_load_invalid_value(void)
59854686f4SJinbum Park {
60854686f4SJinbum Park 	volatile char *dst, *src;
61854686f4SJinbum Park 	bool val, val2, *ptr;
62*4a26f49bSKees Cook 	enum ubsan_test_enum eval, eval2, *eptr;
63*4a26f49bSKees Cook 	unsigned char c = 0xff;
64854686f4SJinbum Park 
65*4a26f49bSKees Cook 	UBSAN_TEST(CONFIG_UBSAN_BOOL, "bool");
66854686f4SJinbum Park 	dst = (char *)&val;
67854686f4SJinbum Park 	src = &c;
68854686f4SJinbum Park 	*dst = *src;
69854686f4SJinbum Park 
70854686f4SJinbum Park 	ptr = &val2;
71854686f4SJinbum Park 	val2 = val;
72*4a26f49bSKees Cook 
73*4a26f49bSKees Cook 	UBSAN_TEST(CONFIG_UBSAN_ENUM, "enum");
74*4a26f49bSKees Cook 	dst = (char *)&eval;
75*4a26f49bSKees Cook 	src = &c;
76*4a26f49bSKees Cook 	*dst = *src;
77*4a26f49bSKees Cook 
78*4a26f49bSKees Cook 	eptr = &eval2;
79*4a26f49bSKees Cook 	eval2 = eval;
80854686f4SJinbum Park }
81854686f4SJinbum Park 
test_ubsan_misaligned_access(void)8231750600SColin Ian King static void test_ubsan_misaligned_access(void)
83854686f4SJinbum Park {
84854686f4SJinbum Park 	volatile char arr[5] __aligned(4) = {1, 2, 3, 4, 5};
85854686f4SJinbum Park 	volatile int *ptr, val = 6;
86854686f4SJinbum Park 
87*4a26f49bSKees Cook 	UBSAN_TEST(CONFIG_UBSAN_ALIGNMENT);
88854686f4SJinbum Park 	ptr = (int *)(arr + 1);
89854686f4SJinbum Park 	*ptr = val;
90854686f4SJinbum Park }
91854686f4SJinbum Park 
92854686f4SJinbum Park static const test_ubsan_fp test_ubsan_array[] = {
93854686f4SJinbum Park 	test_ubsan_shift_out_of_bounds,
94854686f4SJinbum Park 	test_ubsan_out_of_bounds,
95854686f4SJinbum Park 	test_ubsan_load_invalid_value,
96854686f4SJinbum Park 	test_ubsan_misaligned_access,
97854686f4SJinbum Park };
98854686f4SJinbum Park 
99*4a26f49bSKees Cook /* Excluded because they Oops the module. */
100*4a26f49bSKees Cook static const test_ubsan_fp skip_ubsan_array[] = {
101*4a26f49bSKees Cook 	test_ubsan_divrem_overflow,
102*4a26f49bSKees Cook };
103*4a26f49bSKees Cook 
test_ubsan_init(void)104854686f4SJinbum Park static int __init test_ubsan_init(void)
105854686f4SJinbum Park {
106854686f4SJinbum Park 	unsigned int i;
107854686f4SJinbum Park 
108854686f4SJinbum Park 	for (i = 0; i < ARRAY_SIZE(test_ubsan_array); i++)
109854686f4SJinbum Park 		test_ubsan_array[i]();
110854686f4SJinbum Park 
111854686f4SJinbum Park 	return 0;
112854686f4SJinbum Park }
113854686f4SJinbum Park module_init(test_ubsan_init);
114854686f4SJinbum Park 
test_ubsan_exit(void)115854686f4SJinbum Park static void __exit test_ubsan_exit(void)
116854686f4SJinbum Park {
117854686f4SJinbum Park 	/* do nothing */
118854686f4SJinbum Park }
119854686f4SJinbum Park module_exit(test_ubsan_exit);
120854686f4SJinbum Park 
121854686f4SJinbum Park MODULE_AUTHOR("Jinbum Park <jinb.park7@gmail.com>");
122854686f4SJinbum Park MODULE_LICENSE("GPL v2");
123