blob: 2062be1f2e80f61122e801819be9b3a26e3cb3e7 [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_divrem_overflow(void)
15{
16 volatile int val = 16;
17 volatile int val2 = 0;
18
Kees Cook4a26f492020-12-15 20:46:43 -080019 UBSAN_TEST(CONFIG_UBSAN_DIV_ZERO);
Jinbum Park854686f2018-04-10 16:32:58 -070020 val /= val2;
21}
22
Jinbum Park854686f2018-04-10 16:32:58 -070023static void test_ubsan_shift_out_of_bounds(void)
24{
Kees Cook4a26f492020-12-15 20:46:43 -080025 volatile int neg = -1, wrap = 4;
26 int val1 = 10;
27 int val2 = INT_MAX;
Jinbum Park854686f2018-04-10 16:32:58 -070028
Kees Cook4a26f492020-12-15 20:46:43 -080029 UBSAN_TEST(CONFIG_UBSAN_SHIFT, "negative exponent");
30 val1 <<= neg;
31
32 UBSAN_TEST(CONFIG_UBSAN_SHIFT, "left overflow");
33 val2 <<= wrap;
Jinbum Park854686f2018-04-10 16:32:58 -070034}
35
36static void test_ubsan_out_of_bounds(void)
37{
Kees Cook4a26f492020-12-15 20:46:43 -080038 volatile int i = 4, j = 5, k = -1;
39 volatile char above[4] = { }; /* Protect surrounding memory. */
Olof Johansson9d7ca612019-03-07 16:28:21 -080040 volatile int arr[4];
Kees Cook4a26f492020-12-15 20:46:43 -080041 volatile char below[4] = { }; /* Protect surrounding memory. */
Jinbum Park854686f2018-04-10 16:32:58 -070042
Kees Cook4a26f492020-12-15 20:46:43 -080043 above[0] = below[0];
44
45 UBSAN_TEST(CONFIG_UBSAN_BOUNDS, "above");
Jinbum Park854686f2018-04-10 16:32:58 -070046 arr[j] = i;
Kees Cook4a26f492020-12-15 20:46:43 -080047
48 UBSAN_TEST(CONFIG_UBSAN_BOUNDS, "below");
49 arr[k] = i;
Jinbum Park854686f2018-04-10 16:32:58 -070050}
51
Kees Cook4a26f492020-12-15 20:46:43 -080052enum ubsan_test_enum {
53 UBSAN_TEST_ZERO = 0,
54 UBSAN_TEST_ONE,
55 UBSAN_TEST_MAX,
56};
57
Jinbum Park854686f2018-04-10 16:32:58 -070058static void test_ubsan_load_invalid_value(void)
59{
60 volatile char *dst, *src;
61 bool val, val2, *ptr;
Kees Cook4a26f492020-12-15 20:46:43 -080062 enum ubsan_test_enum eval, eval2, *eptr;
63 unsigned char c = 0xff;
Jinbum Park854686f2018-04-10 16:32:58 -070064
Kees Cook4a26f492020-12-15 20:46:43 -080065 UBSAN_TEST(CONFIG_UBSAN_BOOL, "bool");
Jinbum Park854686f2018-04-10 16:32:58 -070066 dst = (char *)&val;
67 src = &c;
68 *dst = *src;
69
70 ptr = &val2;
71 val2 = val;
Kees Cook4a26f492020-12-15 20:46:43 -080072
73 UBSAN_TEST(CONFIG_UBSAN_ENUM, "enum");
74 dst = (char *)&eval;
75 src = &c;
76 *dst = *src;
77
78 eptr = &eval2;
79 eval2 = eval;
Jinbum Park854686f2018-04-10 16:32:58 -070080}
81
Colin Ian King31750602018-04-10 16:33:02 -070082static void test_ubsan_misaligned_access(void)
Jinbum Park854686f2018-04-10 16:32:58 -070083{
84 volatile char arr[5] __aligned(4) = {1, 2, 3, 4, 5};
85 volatile int *ptr, val = 6;
86
Kees Cook4a26f492020-12-15 20:46:43 -080087 UBSAN_TEST(CONFIG_UBSAN_ALIGNMENT);
Jinbum Park854686f2018-04-10 16:32:58 -070088 ptr = (int *)(arr + 1);
89 *ptr = val;
90}
91
Jinbum Park854686f2018-04-10 16:32:58 -070092static const test_ubsan_fp test_ubsan_array[] = {
Jinbum Park854686f2018-04-10 16:32:58 -070093 test_ubsan_shift_out_of_bounds,
94 test_ubsan_out_of_bounds,
95 test_ubsan_load_invalid_value,
Jinbum Park854686f2018-04-10 16:32:58 -070096 test_ubsan_misaligned_access,
Jinbum Park854686f2018-04-10 16:32:58 -070097};
98
Kees Cook4a26f492020-12-15 20:46:43 -080099/* Excluded because they Oops the module. */
100static const test_ubsan_fp skip_ubsan_array[] = {
101 test_ubsan_divrem_overflow,
Kees Cook4a26f492020-12-15 20:46:43 -0800102};
103
Jinbum Park854686f2018-04-10 16:32:58 -0700104static int __init test_ubsan_init(void)
105{
106 unsigned int i;
107
108 for (i = 0; i < ARRAY_SIZE(test_ubsan_array); i++)
109 test_ubsan_array[i]();
110
Jinbum Park854686f2018-04-10 16:32:58 -0700111 return 0;
112}
113module_init(test_ubsan_init);
114
115static void __exit test_ubsan_exit(void)
116{
117 /* do nothing */
118}
119module_exit(test_ubsan_exit);
120
121MODULE_AUTHOR("Jinbum Park <jinb.park7@gmail.com>");
122MODULE_LICENSE("GPL v2");