blob: 5e5d9355ef499d44221308ac28380ab7b7745afe [file] [log] [blame]
Jinbum Park854686f2018-04-10 16:32:58 -07001// SPDX-License-Identifier: GPL-2.0
2#include <linux/init.h>
3#include <linux/kernel.h>
4#include <linux/module.h>
5
6typedef void(*test_ubsan_fp)(void);
7
Kees Cook4a26f492020-12-15 20:46:43 -08008#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
Jinbum Park854686f2018-04-10 16:32:58 -070014static void test_ubsan_add_overflow(void)
15{
16 volatile int val = INT_MAX;
Kees Cook4a26f492020-12-15 20:46:43 -080017 volatile unsigned int uval = UINT_MAX;
Jinbum Park854686f2018-04-10 16:32:58 -070018
Kees Cook4a26f492020-12-15 20:46:43 -080019 UBSAN_TEST(CONFIG_UBSAN_SIGNED_OVERFLOW);
Jinbum Park854686f2018-04-10 16:32:58 -070020 val += 2;
Kees Cook4a26f492020-12-15 20:46:43 -080021
22 UBSAN_TEST(CONFIG_UBSAN_UNSIGNED_OVERFLOW);
23 uval += 2;
Jinbum Park854686f2018-04-10 16:32:58 -070024}
25
26static void test_ubsan_sub_overflow(void)
27{
28 volatile int val = INT_MIN;
Kees Cook4a26f492020-12-15 20:46:43 -080029 volatile unsigned int uval = 0;
Jinbum Park854686f2018-04-10 16:32:58 -070030 volatile int val2 = 2;
31
Kees Cook4a26f492020-12-15 20:46:43 -080032 UBSAN_TEST(CONFIG_UBSAN_SIGNED_OVERFLOW);
Jinbum Park854686f2018-04-10 16:32:58 -070033 val -= val2;
Kees Cook4a26f492020-12-15 20:46:43 -080034
35 UBSAN_TEST(CONFIG_UBSAN_UNSIGNED_OVERFLOW);
36 uval -= val2;
Jinbum Park854686f2018-04-10 16:32:58 -070037}
38
39static void test_ubsan_mul_overflow(void)
40{
41 volatile int val = INT_MAX / 2;
Kees Cook4a26f492020-12-15 20:46:43 -080042 volatile unsigned int uval = UINT_MAX / 2;
Jinbum Park854686f2018-04-10 16:32:58 -070043
Kees Cook4a26f492020-12-15 20:46:43 -080044 UBSAN_TEST(CONFIG_UBSAN_SIGNED_OVERFLOW);
Jinbum Park854686f2018-04-10 16:32:58 -070045 val *= 3;
Kees Cook4a26f492020-12-15 20:46:43 -080046
47 UBSAN_TEST(CONFIG_UBSAN_UNSIGNED_OVERFLOW);
48 uval *= 3;
Jinbum Park854686f2018-04-10 16:32:58 -070049}
50
51static void test_ubsan_negate_overflow(void)
52{
53 volatile int val = INT_MIN;
54
Kees Cook4a26f492020-12-15 20:46:43 -080055 UBSAN_TEST(CONFIG_UBSAN_SIGNED_OVERFLOW);
Jinbum Park854686f2018-04-10 16:32:58 -070056 val = -val;
57}
58
59static void test_ubsan_divrem_overflow(void)
60{
61 volatile int val = 16;
62 volatile int val2 = 0;
63
Kees Cook4a26f492020-12-15 20:46:43 -080064 UBSAN_TEST(CONFIG_UBSAN_DIV_ZERO);
Jinbum Park854686f2018-04-10 16:32:58 -070065 val /= val2;
66}
67
Jinbum Park854686f2018-04-10 16:32:58 -070068static void test_ubsan_shift_out_of_bounds(void)
69{
Kees Cook4a26f492020-12-15 20:46:43 -080070 volatile int neg = -1, wrap = 4;
71 int val1 = 10;
72 int val2 = INT_MAX;
Jinbum Park854686f2018-04-10 16:32:58 -070073
Kees Cook4a26f492020-12-15 20:46:43 -080074 UBSAN_TEST(CONFIG_UBSAN_SHIFT, "negative exponent");
75 val1 <<= neg;
76
77 UBSAN_TEST(CONFIG_UBSAN_SHIFT, "left overflow");
78 val2 <<= wrap;
Jinbum Park854686f2018-04-10 16:32:58 -070079}
80
81static void test_ubsan_out_of_bounds(void)
82{
Kees Cook4a26f492020-12-15 20:46:43 -080083 volatile int i = 4, j = 5, k = -1;
84 volatile char above[4] = { }; /* Protect surrounding memory. */
Olof Johansson9d7ca612019-03-07 16:28:21 -080085 volatile int arr[4];
Kees Cook4a26f492020-12-15 20:46:43 -080086 volatile char below[4] = { }; /* Protect surrounding memory. */
Jinbum Park854686f2018-04-10 16:32:58 -070087
Kees Cook4a26f492020-12-15 20:46:43 -080088 above[0] = below[0];
89
90 UBSAN_TEST(CONFIG_UBSAN_BOUNDS, "above");
Jinbum Park854686f2018-04-10 16:32:58 -070091 arr[j] = i;
Kees Cook4a26f492020-12-15 20:46:43 -080092
93 UBSAN_TEST(CONFIG_UBSAN_BOUNDS, "below");
94 arr[k] = i;
Jinbum Park854686f2018-04-10 16:32:58 -070095}
96
Kees Cook4a26f492020-12-15 20:46:43 -080097enum ubsan_test_enum {
98 UBSAN_TEST_ZERO = 0,
99 UBSAN_TEST_ONE,
100 UBSAN_TEST_MAX,
101};
102
Jinbum Park854686f2018-04-10 16:32:58 -0700103static void test_ubsan_load_invalid_value(void)
104{
105 volatile char *dst, *src;
106 bool val, val2, *ptr;
Kees Cook4a26f492020-12-15 20:46:43 -0800107 enum ubsan_test_enum eval, eval2, *eptr;
108 unsigned char c = 0xff;
Jinbum Park854686f2018-04-10 16:32:58 -0700109
Kees Cook4a26f492020-12-15 20:46:43 -0800110 UBSAN_TEST(CONFIG_UBSAN_BOOL, "bool");
Jinbum Park854686f2018-04-10 16:32:58 -0700111 dst = (char *)&val;
112 src = &c;
113 *dst = *src;
114
115 ptr = &val2;
116 val2 = val;
Kees Cook4a26f492020-12-15 20:46:43 -0800117
118 UBSAN_TEST(CONFIG_UBSAN_ENUM, "enum");
119 dst = (char *)&eval;
120 src = &c;
121 *dst = *src;
122
123 eptr = &eval2;
124 eval2 = eval;
Jinbum Park854686f2018-04-10 16:32:58 -0700125}
126
127static void test_ubsan_null_ptr_deref(void)
128{
129 volatile int *ptr = NULL;
130 int val;
131
Kees Cook4a26f492020-12-15 20:46:43 -0800132 UBSAN_TEST(CONFIG_UBSAN_OBJECT_SIZE);
Jinbum Park854686f2018-04-10 16:32:58 -0700133 val = *ptr;
134}
135
Colin Ian King31750602018-04-10 16:33:02 -0700136static void test_ubsan_misaligned_access(void)
Jinbum Park854686f2018-04-10 16:32:58 -0700137{
138 volatile char arr[5] __aligned(4) = {1, 2, 3, 4, 5};
139 volatile int *ptr, val = 6;
140
Kees Cook4a26f492020-12-15 20:46:43 -0800141 UBSAN_TEST(CONFIG_UBSAN_ALIGNMENT);
Jinbum Park854686f2018-04-10 16:32:58 -0700142 ptr = (int *)(arr + 1);
143 *ptr = val;
144}
145
146static void test_ubsan_object_size_mismatch(void)
147{
148 /* "((aligned(8)))" helps this not into be misaligned for ptr-access. */
149 volatile int val __aligned(8) = 4;
150 volatile long long *ptr, val2;
151
Kees Cook4a26f492020-12-15 20:46:43 -0800152 UBSAN_TEST(CONFIG_UBSAN_OBJECT_SIZE);
Jinbum Park854686f2018-04-10 16:32:58 -0700153 ptr = (long long *)&val;
154 val2 = *ptr;
155}
156
157static const test_ubsan_fp test_ubsan_array[] = {
158 test_ubsan_add_overflow,
159 test_ubsan_sub_overflow,
160 test_ubsan_mul_overflow,
161 test_ubsan_negate_overflow,
Jinbum Park854686f2018-04-10 16:32:58 -0700162 test_ubsan_shift_out_of_bounds,
163 test_ubsan_out_of_bounds,
164 test_ubsan_load_invalid_value,
Jinbum Park854686f2018-04-10 16:32:58 -0700165 test_ubsan_misaligned_access,
166 test_ubsan_object_size_mismatch,
167};
168
Kees Cook4a26f492020-12-15 20:46:43 -0800169/* Excluded because they Oops the module. */
170static const test_ubsan_fp skip_ubsan_array[] = {
171 test_ubsan_divrem_overflow,
172 test_ubsan_null_ptr_deref,
173};
174
Jinbum Park854686f2018-04-10 16:32:58 -0700175static int __init test_ubsan_init(void)
176{
177 unsigned int i;
178
179 for (i = 0; i < ARRAY_SIZE(test_ubsan_array); i++)
180 test_ubsan_array[i]();
181
Jinbum Park854686f2018-04-10 16:32:58 -0700182 return 0;
183}
184module_init(test_ubsan_init);
185
186static void __exit test_ubsan_exit(void)
187{
188 /* do nothing */
189}
190module_exit(test_ubsan_exit);
191
192MODULE_AUTHOR("Jinbum Park <jinb.park7@gmail.com>");
193MODULE_LICENSE("GPL v2");