blob: fc680562d8b69fc80cf2dbe9fab71d9e65303662 [file] [log] [blame]
Rasmus Villemoes455a35a2018-05-08 00:36:28 +02001// SPDX-License-Identifier: GPL-2.0 OR MIT
2/*
3 * Test cases for arithmetic overflow checks.
4 */
5#define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
6
Kees Cookca908002018-05-10 16:40:03 -07007#include <linux/device.h>
Rasmus Villemoes455a35a2018-05-08 00:36:28 +02008#include <linux/init.h>
9#include <linux/kernel.h>
Kees Cookca908002018-05-10 16:40:03 -070010#include <linux/mm.h>
Rasmus Villemoes455a35a2018-05-08 00:36:28 +020011#include <linux/module.h>
12#include <linux/overflow.h>
Kees Cookca908002018-05-10 16:40:03 -070013#include <linux/slab.h>
Rasmus Villemoes455a35a2018-05-08 00:36:28 +020014#include <linux/types.h>
Kees Cookca908002018-05-10 16:40:03 -070015#include <linux/vmalloc.h>
Rasmus Villemoes455a35a2018-05-08 00:36:28 +020016
17#define DEFINE_TEST_ARRAY(t) \
18 static const struct test_ ## t { \
19 t a, b; \
20 t sum, diff, prod; \
21 bool s_of, d_of, p_of; \
22 } t ## _tests[] __initconst
23
24DEFINE_TEST_ARRAY(u8) = {
25 {0, 0, 0, 0, 0, false, false, false},
26 {1, 1, 2, 0, 1, false, false, false},
27 {0, 1, 1, U8_MAX, 0, false, true, false},
28 {1, 0, 1, 1, 0, false, false, false},
29 {0, U8_MAX, U8_MAX, 1, 0, false, true, false},
30 {U8_MAX, 0, U8_MAX, U8_MAX, 0, false, false, false},
31 {1, U8_MAX, 0, 2, U8_MAX, true, true, false},
32 {U8_MAX, 1, 0, U8_MAX-1, U8_MAX, true, false, false},
33 {U8_MAX, U8_MAX, U8_MAX-1, 0, 1, true, false, true},
34
35 {U8_MAX, U8_MAX-1, U8_MAX-2, 1, 2, true, false, true},
36 {U8_MAX-1, U8_MAX, U8_MAX-2, U8_MAX, 2, true, true, true},
37
38 {1U << 3, 1U << 3, 1U << 4, 0, 1U << 6, false, false, false},
39 {1U << 4, 1U << 4, 1U << 5, 0, 0, false, false, true},
40 {1U << 4, 1U << 3, 3*(1U << 3), 1U << 3, 1U << 7, false, false, false},
41 {1U << 7, 1U << 7, 0, 0, 0, true, false, true},
42
43 {48, 32, 80, 16, 0, false, false, true},
44 {128, 128, 0, 0, 0, true, false, true},
45 {123, 234, 101, 145, 110, true, true, true},
46};
47DEFINE_TEST_ARRAY(u16) = {
48 {0, 0, 0, 0, 0, false, false, false},
49 {1, 1, 2, 0, 1, false, false, false},
50 {0, 1, 1, U16_MAX, 0, false, true, false},
51 {1, 0, 1, 1, 0, false, false, false},
52 {0, U16_MAX, U16_MAX, 1, 0, false, true, false},
53 {U16_MAX, 0, U16_MAX, U16_MAX, 0, false, false, false},
54 {1, U16_MAX, 0, 2, U16_MAX, true, true, false},
55 {U16_MAX, 1, 0, U16_MAX-1, U16_MAX, true, false, false},
56 {U16_MAX, U16_MAX, U16_MAX-1, 0, 1, true, false, true},
57
58 {U16_MAX, U16_MAX-1, U16_MAX-2, 1, 2, true, false, true},
59 {U16_MAX-1, U16_MAX, U16_MAX-2, U16_MAX, 2, true, true, true},
60
61 {1U << 7, 1U << 7, 1U << 8, 0, 1U << 14, false, false, false},
62 {1U << 8, 1U << 8, 1U << 9, 0, 0, false, false, true},
63 {1U << 8, 1U << 7, 3*(1U << 7), 1U << 7, 1U << 15, false, false, false},
64 {1U << 15, 1U << 15, 0, 0, 0, true, false, true},
65
66 {123, 234, 357, 65425, 28782, false, true, false},
67 {1234, 2345, 3579, 64425, 10146, false, true, true},
68};
69DEFINE_TEST_ARRAY(u32) = {
70 {0, 0, 0, 0, 0, false, false, false},
71 {1, 1, 2, 0, 1, false, false, false},
72 {0, 1, 1, U32_MAX, 0, false, true, false},
73 {1, 0, 1, 1, 0, false, false, false},
74 {0, U32_MAX, U32_MAX, 1, 0, false, true, false},
75 {U32_MAX, 0, U32_MAX, U32_MAX, 0, false, false, false},
76 {1, U32_MAX, 0, 2, U32_MAX, true, true, false},
77 {U32_MAX, 1, 0, U32_MAX-1, U32_MAX, true, false, false},
78 {U32_MAX, U32_MAX, U32_MAX-1, 0, 1, true, false, true},
79
80 {U32_MAX, U32_MAX-1, U32_MAX-2, 1, 2, true, false, true},
81 {U32_MAX-1, U32_MAX, U32_MAX-2, U32_MAX, 2, true, true, true},
82
83 {1U << 15, 1U << 15, 1U << 16, 0, 1U << 30, false, false, false},
84 {1U << 16, 1U << 16, 1U << 17, 0, 0, false, false, true},
85 {1U << 16, 1U << 15, 3*(1U << 15), 1U << 15, 1U << 31, false, false, false},
86 {1U << 31, 1U << 31, 0, 0, 0, true, false, true},
87
88 {-2U, 1U, -1U, -3U, -2U, false, false, false},
89 {-4U, 5U, 1U, -9U, -20U, true, false, true},
90};
91
92DEFINE_TEST_ARRAY(u64) = {
93 {0, 0, 0, 0, 0, false, false, false},
94 {1, 1, 2, 0, 1, false, false, false},
95 {0, 1, 1, U64_MAX, 0, false, true, false},
96 {1, 0, 1, 1, 0, false, false, false},
97 {0, U64_MAX, U64_MAX, 1, 0, false, true, false},
98 {U64_MAX, 0, U64_MAX, U64_MAX, 0, false, false, false},
99 {1, U64_MAX, 0, 2, U64_MAX, true, true, false},
100 {U64_MAX, 1, 0, U64_MAX-1, U64_MAX, true, false, false},
101 {U64_MAX, U64_MAX, U64_MAX-1, 0, 1, true, false, true},
102
103 {U64_MAX, U64_MAX-1, U64_MAX-2, 1, 2, true, false, true},
104 {U64_MAX-1, U64_MAX, U64_MAX-2, U64_MAX, 2, true, true, true},
105
106 {1ULL << 31, 1ULL << 31, 1ULL << 32, 0, 1ULL << 62, false, false, false},
107 {1ULL << 32, 1ULL << 32, 1ULL << 33, 0, 0, false, false, true},
108 {1ULL << 32, 1ULL << 31, 3*(1ULL << 31), 1ULL << 31, 1ULL << 63, false, false, false},
109 {1ULL << 63, 1ULL << 63, 0, 0, 0, true, false, true},
110 {1000000000ULL /* 10^9 */, 10000000000ULL /* 10^10 */,
111 11000000000ULL, 18446744064709551616ULL, 10000000000000000000ULL,
112 false, true, false},
113 {-15ULL, 10ULL, -5ULL, -25ULL, -150ULL, false, false, true},
114};
115
116DEFINE_TEST_ARRAY(s8) = {
117 {0, 0, 0, 0, 0, false, false, false},
118
119 {0, S8_MAX, S8_MAX, -S8_MAX, 0, false, false, false},
120 {S8_MAX, 0, S8_MAX, S8_MAX, 0, false, false, false},
121 {0, S8_MIN, S8_MIN, S8_MIN, 0, false, true, false},
122 {S8_MIN, 0, S8_MIN, S8_MIN, 0, false, false, false},
123
124 {-1, S8_MIN, S8_MAX, S8_MAX, S8_MIN, true, false, true},
125 {S8_MIN, -1, S8_MAX, -S8_MAX, S8_MIN, true, false, true},
126 {-1, S8_MAX, S8_MAX-1, S8_MIN, -S8_MAX, false, false, false},
127 {S8_MAX, -1, S8_MAX-1, S8_MIN, -S8_MAX, false, true, false},
128 {-1, -S8_MAX, S8_MIN, S8_MAX-1, S8_MAX, false, false, false},
129 {-S8_MAX, -1, S8_MIN, S8_MIN+2, S8_MAX, false, false, false},
130
131 {1, S8_MIN, -S8_MAX, -S8_MAX, S8_MIN, false, true, false},
132 {S8_MIN, 1, -S8_MAX, S8_MAX, S8_MIN, false, true, false},
133 {1, S8_MAX, S8_MIN, S8_MIN+2, S8_MAX, true, false, false},
134 {S8_MAX, 1, S8_MIN, S8_MAX-1, S8_MAX, true, false, false},
135
136 {S8_MIN, S8_MIN, 0, 0, 0, true, false, true},
137 {S8_MAX, S8_MAX, -2, 0, 1, true, false, true},
138
139 {-4, -32, -36, 28, -128, false, false, true},
140 {-4, 32, 28, -36, -128, false, false, false},
141};
142
143DEFINE_TEST_ARRAY(s16) = {
144 {0, 0, 0, 0, 0, false, false, false},
145
146 {0, S16_MAX, S16_MAX, -S16_MAX, 0, false, false, false},
147 {S16_MAX, 0, S16_MAX, S16_MAX, 0, false, false, false},
148 {0, S16_MIN, S16_MIN, S16_MIN, 0, false, true, false},
149 {S16_MIN, 0, S16_MIN, S16_MIN, 0, false, false, false},
150
151 {-1, S16_MIN, S16_MAX, S16_MAX, S16_MIN, true, false, true},
152 {S16_MIN, -1, S16_MAX, -S16_MAX, S16_MIN, true, false, true},
153 {-1, S16_MAX, S16_MAX-1, S16_MIN, -S16_MAX, false, false, false},
154 {S16_MAX, -1, S16_MAX-1, S16_MIN, -S16_MAX, false, true, false},
155 {-1, -S16_MAX, S16_MIN, S16_MAX-1, S16_MAX, false, false, false},
156 {-S16_MAX, -1, S16_MIN, S16_MIN+2, S16_MAX, false, false, false},
157
158 {1, S16_MIN, -S16_MAX, -S16_MAX, S16_MIN, false, true, false},
159 {S16_MIN, 1, -S16_MAX, S16_MAX, S16_MIN, false, true, false},
160 {1, S16_MAX, S16_MIN, S16_MIN+2, S16_MAX, true, false, false},
161 {S16_MAX, 1, S16_MIN, S16_MAX-1, S16_MAX, true, false, false},
162
163 {S16_MIN, S16_MIN, 0, 0, 0, true, false, true},
164 {S16_MAX, S16_MAX, -2, 0, 1, true, false, true},
165};
166DEFINE_TEST_ARRAY(s32) = {
167 {0, 0, 0, 0, 0, false, false, false},
168
169 {0, S32_MAX, S32_MAX, -S32_MAX, 0, false, false, false},
170 {S32_MAX, 0, S32_MAX, S32_MAX, 0, false, false, false},
171 {0, S32_MIN, S32_MIN, S32_MIN, 0, false, true, false},
172 {S32_MIN, 0, S32_MIN, S32_MIN, 0, false, false, false},
173
174 {-1, S32_MIN, S32_MAX, S32_MAX, S32_MIN, true, false, true},
175 {S32_MIN, -1, S32_MAX, -S32_MAX, S32_MIN, true, false, true},
176 {-1, S32_MAX, S32_MAX-1, S32_MIN, -S32_MAX, false, false, false},
177 {S32_MAX, -1, S32_MAX-1, S32_MIN, -S32_MAX, false, true, false},
178 {-1, -S32_MAX, S32_MIN, S32_MAX-1, S32_MAX, false, false, false},
179 {-S32_MAX, -1, S32_MIN, S32_MIN+2, S32_MAX, false, false, false},
180
181 {1, S32_MIN, -S32_MAX, -S32_MAX, S32_MIN, false, true, false},
182 {S32_MIN, 1, -S32_MAX, S32_MAX, S32_MIN, false, true, false},
183 {1, S32_MAX, S32_MIN, S32_MIN+2, S32_MAX, true, false, false},
184 {S32_MAX, 1, S32_MIN, S32_MAX-1, S32_MAX, true, false, false},
185
186 {S32_MIN, S32_MIN, 0, 0, 0, true, false, true},
187 {S32_MAX, S32_MAX, -2, 0, 1, true, false, true},
188};
189DEFINE_TEST_ARRAY(s64) = {
190 {0, 0, 0, 0, 0, false, false, false},
191
192 {0, S64_MAX, S64_MAX, -S64_MAX, 0, false, false, false},
193 {S64_MAX, 0, S64_MAX, S64_MAX, 0, false, false, false},
194 {0, S64_MIN, S64_MIN, S64_MIN, 0, false, true, false},
195 {S64_MIN, 0, S64_MIN, S64_MIN, 0, false, false, false},
196
197 {-1, S64_MIN, S64_MAX, S64_MAX, S64_MIN, true, false, true},
198 {S64_MIN, -1, S64_MAX, -S64_MAX, S64_MIN, true, false, true},
199 {-1, S64_MAX, S64_MAX-1, S64_MIN, -S64_MAX, false, false, false},
200 {S64_MAX, -1, S64_MAX-1, S64_MIN, -S64_MAX, false, true, false},
201 {-1, -S64_MAX, S64_MIN, S64_MAX-1, S64_MAX, false, false, false},
202 {-S64_MAX, -1, S64_MIN, S64_MIN+2, S64_MAX, false, false, false},
203
204 {1, S64_MIN, -S64_MAX, -S64_MAX, S64_MIN, false, true, false},
205 {S64_MIN, 1, -S64_MAX, S64_MAX, S64_MIN, false, true, false},
206 {1, S64_MAX, S64_MIN, S64_MIN+2, S64_MAX, true, false, false},
207 {S64_MAX, 1, S64_MIN, S64_MAX-1, S64_MAX, true, false, false},
208
209 {S64_MIN, S64_MIN, 0, 0, 0, true, false, true},
210 {S64_MAX, S64_MAX, -2, 0, 1, true, false, true},
211
212 {-1, -1, -2, 0, 1, false, false, false},
213 {-1, -128, -129, 127, 128, false, false, false},
214 {-128, -1, -129, -127, 128, false, false, false},
215 {0, -S64_MAX, -S64_MAX, S64_MAX, 0, false, false, false},
216};
217
Rasmus Villemoes6d334432018-06-04 08:41:27 +0200218#define check_one_op(t, fmt, op, sym, a, b, r, of) do { \
219 t _r; \
220 bool _of; \
221 \
222 _of = check_ ## op ## _overflow(a, b, &_r); \
223 if (_of != of) { \
224 pr_warn("expected "fmt" "sym" "fmt \
225 " to%s overflow (type %s)\n", \
226 a, b, of ? "" : " not", #t); \
Kees Cook8fee81a2018-05-09 22:13:28 -0700227 err = 1; \
Rasmus Villemoes6d334432018-06-04 08:41:27 +0200228 } \
229 if (_r != r) { \
230 pr_warn("expected "fmt" "sym" "fmt" == " \
231 fmt", got "fmt" (type %s)\n", \
232 a, b, r, _r, #t); \
Kees Cook8fee81a2018-05-09 22:13:28 -0700233 err = 1; \
Rasmus Villemoes6d334432018-06-04 08:41:27 +0200234 } \
235} while (0)
236
Rasmus Villemoes455a35a2018-05-08 00:36:28 +0200237#define DEFINE_TEST_FUNC(t, fmt) \
Kees Cook8fee81a2018-05-09 22:13:28 -0700238static int __init do_test_ ## t(const struct test_ ## t *p) \
Rasmus Villemoes455a35a2018-05-08 00:36:28 +0200239{ \
Kees Cook8fee81a2018-05-09 22:13:28 -0700240 int err = 0; \
241 \
Rasmus Villemoes6d334432018-06-04 08:41:27 +0200242 check_one_op(t, fmt, add, "+", p->a, p->b, p->sum, p->s_of); \
243 check_one_op(t, fmt, add, "+", p->b, p->a, p->sum, p->s_of); \
244 check_one_op(t, fmt, sub, "-", p->a, p->b, p->diff, p->d_of); \
245 check_one_op(t, fmt, mul, "*", p->a, p->b, p->prod, p->p_of); \
246 check_one_op(t, fmt, mul, "*", p->b, p->a, p->prod, p->p_of); \
Kees Cook8fee81a2018-05-09 22:13:28 -0700247 \
248 return err; \
Rasmus Villemoes455a35a2018-05-08 00:36:28 +0200249} \
250 \
Kees Cook8fee81a2018-05-09 22:13:28 -0700251static int __init test_ ## t ## _overflow(void) { \
252 int err = 0; \
Rasmus Villemoes455a35a2018-05-08 00:36:28 +0200253 unsigned i; \
254 \
Kees Cookd36b6ad2018-08-01 14:25:40 -0700255 pr_info("%-3s: %zu arithmetic tests\n", #t, \
256 ARRAY_SIZE(t ## _tests)); \
Rasmus Villemoes455a35a2018-05-08 00:36:28 +0200257 for (i = 0; i < ARRAY_SIZE(t ## _tests); ++i) \
Kees Cook8fee81a2018-05-09 22:13:28 -0700258 err |= do_test_ ## t(&t ## _tests[i]); \
259 return err; \
Rasmus Villemoes455a35a2018-05-08 00:36:28 +0200260}
261
262DEFINE_TEST_FUNC(u8, "%d");
263DEFINE_TEST_FUNC(s8, "%d");
264DEFINE_TEST_FUNC(u16, "%d");
265DEFINE_TEST_FUNC(s16, "%d");
266DEFINE_TEST_FUNC(u32, "%u");
267DEFINE_TEST_FUNC(s32, "%d");
268#if BITS_PER_LONG == 64
269DEFINE_TEST_FUNC(u64, "%llu");
270DEFINE_TEST_FUNC(s64, "%lld");
271#endif
272
Kees Cook8fee81a2018-05-09 22:13:28 -0700273static int __init test_overflow_calculation(void)
Rasmus Villemoes455a35a2018-05-08 00:36:28 +0200274{
Kees Cook8fee81a2018-05-09 22:13:28 -0700275 int err = 0;
276
277 err |= test_u8_overflow();
278 err |= test_s8_overflow();
279 err |= test_u16_overflow();
280 err |= test_s16_overflow();
281 err |= test_u32_overflow();
282 err |= test_s32_overflow();
Rasmus Villemoes455a35a2018-05-08 00:36:28 +0200283#if BITS_PER_LONG == 64
Kees Cook8fee81a2018-05-09 22:13:28 -0700284 err |= test_u64_overflow();
285 err |= test_s64_overflow();
Rasmus Villemoes455a35a2018-05-08 00:36:28 +0200286#endif
287
Kees Cook8fee81a2018-05-09 22:13:28 -0700288 return err;
289}
Rasmus Villemoes455a35a2018-05-08 00:36:28 +0200290
Kees Cookd36b6ad2018-08-01 14:25:40 -0700291static int __init test_overflow_shift(void)
292{
293 int err = 0;
294
295/* Args are: value, shift, type, expected result, overflow expected */
296#define TEST_ONE_SHIFT(a, s, t, expect, of) ({ \
297 int __failed = 0; \
298 typeof(a) __a = (a); \
299 typeof(s) __s = (s); \
300 t __e = (expect); \
301 t __d; \
302 bool __of = check_shl_overflow(__a, __s, &__d); \
303 if (__of != of) { \
304 pr_warn("expected (%s)(%s << %s) to%s overflow\n", \
305 #t, #a, #s, of ? "" : " not"); \
306 __failed = 1; \
307 } else if (!__of && __d != __e) { \
308 pr_warn("expected (%s)(%s << %s) == %s\n", \
309 #t, #a, #s, #expect); \
310 if ((t)-1 < 0) \
311 pr_warn("got %lld\n", (s64)__d); \
312 else \
313 pr_warn("got %llu\n", (u64)__d); \
314 __failed = 1; \
315 } \
316 if (!__failed) \
317 pr_info("ok: (%s)(%s << %s) == %s\n", #t, #a, #s, \
318 of ? "overflow" : #expect); \
319 __failed; \
320})
321
322 /* Sane shifts. */
323 err |= TEST_ONE_SHIFT(1, 0, u8, 1 << 0, false);
324 err |= TEST_ONE_SHIFT(1, 4, u8, 1 << 4, false);
325 err |= TEST_ONE_SHIFT(1, 7, u8, 1 << 7, false);
326 err |= TEST_ONE_SHIFT(0xF, 4, u8, 0xF << 4, false);
327 err |= TEST_ONE_SHIFT(1, 0, u16, 1 << 0, false);
328 err |= TEST_ONE_SHIFT(1, 10, u16, 1 << 10, false);
329 err |= TEST_ONE_SHIFT(1, 15, u16, 1 << 15, false);
330 err |= TEST_ONE_SHIFT(0xFF, 8, u16, 0xFF << 8, false);
331 err |= TEST_ONE_SHIFT(1, 0, int, 1 << 0, false);
332 err |= TEST_ONE_SHIFT(1, 16, int, 1 << 16, false);
333 err |= TEST_ONE_SHIFT(1, 30, int, 1 << 30, false);
334 err |= TEST_ONE_SHIFT(1, 0, s32, 1 << 0, false);
335 err |= TEST_ONE_SHIFT(1, 16, s32, 1 << 16, false);
336 err |= TEST_ONE_SHIFT(1, 30, s32, 1 << 30, false);
337 err |= TEST_ONE_SHIFT(1, 0, unsigned int, 1U << 0, false);
338 err |= TEST_ONE_SHIFT(1, 20, unsigned int, 1U << 20, false);
339 err |= TEST_ONE_SHIFT(1, 31, unsigned int, 1U << 31, false);
340 err |= TEST_ONE_SHIFT(0xFFFFU, 16, unsigned int, 0xFFFFU << 16, false);
341 err |= TEST_ONE_SHIFT(1, 0, u32, 1U << 0, false);
342 err |= TEST_ONE_SHIFT(1, 20, u32, 1U << 20, false);
343 err |= TEST_ONE_SHIFT(1, 31, u32, 1U << 31, false);
344 err |= TEST_ONE_SHIFT(0xFFFFU, 16, u32, 0xFFFFU << 16, false);
345 err |= TEST_ONE_SHIFT(1, 0, u64, 1ULL << 0, false);
346 err |= TEST_ONE_SHIFT(1, 40, u64, 1ULL << 40, false);
347 err |= TEST_ONE_SHIFT(1, 63, u64, 1ULL << 63, false);
348 err |= TEST_ONE_SHIFT(0xFFFFFFFFULL, 32, u64,
349 0xFFFFFFFFULL << 32, false);
350
351 /* Sane shift: start and end with 0, without a too-wide shift. */
352 err |= TEST_ONE_SHIFT(0, 7, u8, 0, false);
353 err |= TEST_ONE_SHIFT(0, 15, u16, 0, false);
354 err |= TEST_ONE_SHIFT(0, 31, unsigned int, 0, false);
355 err |= TEST_ONE_SHIFT(0, 31, u32, 0, false);
356 err |= TEST_ONE_SHIFT(0, 63, u64, 0, false);
357
358 /* Sane shift: start and end with 0, without reaching signed bit. */
359 err |= TEST_ONE_SHIFT(0, 6, s8, 0, false);
360 err |= TEST_ONE_SHIFT(0, 14, s16, 0, false);
361 err |= TEST_ONE_SHIFT(0, 30, int, 0, false);
362 err |= TEST_ONE_SHIFT(0, 30, s32, 0, false);
363 err |= TEST_ONE_SHIFT(0, 62, s64, 0, false);
364
365 /* Overflow: shifted the bit off the end. */
366 err |= TEST_ONE_SHIFT(1, 8, u8, 0, true);
367 err |= TEST_ONE_SHIFT(1, 16, u16, 0, true);
368 err |= TEST_ONE_SHIFT(1, 32, unsigned int, 0, true);
369 err |= TEST_ONE_SHIFT(1, 32, u32, 0, true);
370 err |= TEST_ONE_SHIFT(1, 64, u64, 0, true);
371
372 /* Overflow: shifted into the signed bit. */
373 err |= TEST_ONE_SHIFT(1, 7, s8, 0, true);
374 err |= TEST_ONE_SHIFT(1, 15, s16, 0, true);
375 err |= TEST_ONE_SHIFT(1, 31, int, 0, true);
376 err |= TEST_ONE_SHIFT(1, 31, s32, 0, true);
377 err |= TEST_ONE_SHIFT(1, 63, s64, 0, true);
378
379 /* Overflow: high bit falls off unsigned types. */
380 /* 10010110 */
381 err |= TEST_ONE_SHIFT(150, 1, u8, 0, true);
382 /* 1000100010010110 */
383 err |= TEST_ONE_SHIFT(34966, 1, u16, 0, true);
384 /* 10000100000010001000100010010110 */
385 err |= TEST_ONE_SHIFT(2215151766U, 1, u32, 0, true);
386 err |= TEST_ONE_SHIFT(2215151766U, 1, unsigned int, 0, true);
387 /* 1000001000010000010000000100000010000100000010001000100010010110 */
388 err |= TEST_ONE_SHIFT(9372061470395238550ULL, 1, u64, 0, true);
389
390 /* Overflow: bit shifted into signed bit on signed types. */
391 /* 01001011 */
392 err |= TEST_ONE_SHIFT(75, 1, s8, 0, true);
393 /* 0100010001001011 */
394 err |= TEST_ONE_SHIFT(17483, 1, s16, 0, true);
395 /* 01000010000001000100010001001011 */
396 err |= TEST_ONE_SHIFT(1107575883, 1, s32, 0, true);
397 err |= TEST_ONE_SHIFT(1107575883, 1, int, 0, true);
398 /* 0100000100001000001000000010000001000010000001000100010001001011 */
399 err |= TEST_ONE_SHIFT(4686030735197619275LL, 1, s64, 0, true);
400
401 /* Overflow: bit shifted past signed bit on signed types. */
402 /* 01001011 */
403 err |= TEST_ONE_SHIFT(75, 2, s8, 0, true);
404 /* 0100010001001011 */
405 err |= TEST_ONE_SHIFT(17483, 2, s16, 0, true);
406 /* 01000010000001000100010001001011 */
407 err |= TEST_ONE_SHIFT(1107575883, 2, s32, 0, true);
408 err |= TEST_ONE_SHIFT(1107575883, 2, int, 0, true);
409 /* 0100000100001000001000000010000001000010000001000100010001001011 */
410 err |= TEST_ONE_SHIFT(4686030735197619275LL, 2, s64, 0, true);
411
412 /* Overflow: values larger than destination type. */
413 err |= TEST_ONE_SHIFT(0x100, 0, u8, 0, true);
414 err |= TEST_ONE_SHIFT(0xFF, 0, s8, 0, true);
415 err |= TEST_ONE_SHIFT(0x10000U, 0, u16, 0, true);
416 err |= TEST_ONE_SHIFT(0xFFFFU, 0, s16, 0, true);
417 err |= TEST_ONE_SHIFT(0x100000000ULL, 0, u32, 0, true);
418 err |= TEST_ONE_SHIFT(0x100000000ULL, 0, unsigned int, 0, true);
419 err |= TEST_ONE_SHIFT(0xFFFFFFFFUL, 0, s32, 0, true);
420 err |= TEST_ONE_SHIFT(0xFFFFFFFFUL, 0, int, 0, true);
421 err |= TEST_ONE_SHIFT(0xFFFFFFFFFFFFFFFFULL, 0, s64, 0, true);
422
423 /* Nonsense: negative initial value. */
424 err |= TEST_ONE_SHIFT(-1, 0, s8, 0, true);
425 err |= TEST_ONE_SHIFT(-1, 0, u8, 0, true);
426 err |= TEST_ONE_SHIFT(-5, 0, s16, 0, true);
427 err |= TEST_ONE_SHIFT(-5, 0, u16, 0, true);
428 err |= TEST_ONE_SHIFT(-10, 0, int, 0, true);
429 err |= TEST_ONE_SHIFT(-10, 0, unsigned int, 0, true);
430 err |= TEST_ONE_SHIFT(-100, 0, s32, 0, true);
431 err |= TEST_ONE_SHIFT(-100, 0, u32, 0, true);
432 err |= TEST_ONE_SHIFT(-10000, 0, s64, 0, true);
433 err |= TEST_ONE_SHIFT(-10000, 0, u64, 0, true);
434
435 /* Nonsense: negative shift values. */
436 err |= TEST_ONE_SHIFT(0, -5, s8, 0, true);
437 err |= TEST_ONE_SHIFT(0, -5, u8, 0, true);
438 err |= TEST_ONE_SHIFT(0, -10, s16, 0, true);
439 err |= TEST_ONE_SHIFT(0, -10, u16, 0, true);
440 err |= TEST_ONE_SHIFT(0, -15, int, 0, true);
441 err |= TEST_ONE_SHIFT(0, -15, unsigned int, 0, true);
442 err |= TEST_ONE_SHIFT(0, -20, s32, 0, true);
443 err |= TEST_ONE_SHIFT(0, -20, u32, 0, true);
444 err |= TEST_ONE_SHIFT(0, -30, s64, 0, true);
445 err |= TEST_ONE_SHIFT(0, -30, u64, 0, true);
446
447 /* Overflow: shifted at or beyond entire type's bit width. */
448 err |= TEST_ONE_SHIFT(0, 8, u8, 0, true);
449 err |= TEST_ONE_SHIFT(0, 9, u8, 0, true);
450 err |= TEST_ONE_SHIFT(0, 8, s8, 0, true);
451 err |= TEST_ONE_SHIFT(0, 9, s8, 0, true);
452 err |= TEST_ONE_SHIFT(0, 16, u16, 0, true);
453 err |= TEST_ONE_SHIFT(0, 17, u16, 0, true);
454 err |= TEST_ONE_SHIFT(0, 16, s16, 0, true);
455 err |= TEST_ONE_SHIFT(0, 17, s16, 0, true);
456 err |= TEST_ONE_SHIFT(0, 32, u32, 0, true);
457 err |= TEST_ONE_SHIFT(0, 33, u32, 0, true);
458 err |= TEST_ONE_SHIFT(0, 32, int, 0, true);
459 err |= TEST_ONE_SHIFT(0, 33, int, 0, true);
460 err |= TEST_ONE_SHIFT(0, 32, s32, 0, true);
461 err |= TEST_ONE_SHIFT(0, 33, s32, 0, true);
462 err |= TEST_ONE_SHIFT(0, 64, u64, 0, true);
463 err |= TEST_ONE_SHIFT(0, 65, u64, 0, true);
464 err |= TEST_ONE_SHIFT(0, 64, s64, 0, true);
465 err |= TEST_ONE_SHIFT(0, 65, s64, 0, true);
466
467 /*
468 * Corner case: for unsigned types, we fail when we've shifted
469 * through the entire width of bits. For signed types, we might
470 * want to match this behavior, but that would mean noticing if
471 * we shift through all but the signed bit, and this is not
472 * currently detected (but we'll notice an overflow into the
473 * signed bit). So, for now, we will test this condition but
474 * mark it as not expected to overflow.
475 */
476 err |= TEST_ONE_SHIFT(0, 7, s8, 0, false);
477 err |= TEST_ONE_SHIFT(0, 15, s16, 0, false);
478 err |= TEST_ONE_SHIFT(0, 31, int, 0, false);
479 err |= TEST_ONE_SHIFT(0, 31, s32, 0, false);
480 err |= TEST_ONE_SHIFT(0, 63, s64, 0, false);
481
482 return err;
483}
484
Kees Cookca908002018-05-10 16:40:03 -0700485/*
486 * Deal with the various forms of allocator arguments. See comments above
487 * the DEFINE_TEST_ALLOC() instances for mapping of the "bits".
488 */
489#define alloc010(alloc, arg, sz) alloc(sz, GFP_KERNEL)
490#define alloc011(alloc, arg, sz) alloc(sz, GFP_KERNEL, NUMA_NO_NODE)
491#define alloc000(alloc, arg, sz) alloc(sz)
492#define alloc001(alloc, arg, sz) alloc(sz, NUMA_NO_NODE)
493#define alloc110(alloc, arg, sz) alloc(arg, sz, GFP_KERNEL)
494#define free0(free, arg, ptr) free(ptr)
495#define free1(free, arg, ptr) free(arg, ptr)
496
497/* Wrap around to 8K */
498#define TEST_SIZE (9 << PAGE_SHIFT)
499
500#define DEFINE_TEST_ALLOC(func, free_func, want_arg, want_gfp, want_node)\
501static int __init test_ ## func (void *arg) \
502{ \
503 volatile size_t a = TEST_SIZE; \
504 volatile size_t b = (SIZE_MAX / TEST_SIZE) + 1; \
505 void *ptr; \
506 \
507 /* Tiny allocation test. */ \
508 ptr = alloc ## want_arg ## want_gfp ## want_node (func, arg, 1);\
509 if (!ptr) { \
510 pr_warn(#func " failed regular allocation?!\n"); \
511 return 1; \
512 } \
513 free ## want_arg (free_func, arg, ptr); \
514 \
515 /* Wrapped allocation test. */ \
516 ptr = alloc ## want_arg ## want_gfp ## want_node (func, arg, \
517 a * b); \
518 if (!ptr) { \
519 pr_warn(#func " unexpectedly failed bad wrapping?!\n"); \
520 return 1; \
521 } \
522 free ## want_arg (free_func, arg, ptr); \
523 \
524 /* Saturated allocation test. */ \
525 ptr = alloc ## want_arg ## want_gfp ## want_node (func, arg, \
526 array_size(a, b)); \
527 if (ptr) { \
528 pr_warn(#func " missed saturation!\n"); \
529 free ## want_arg (free_func, arg, ptr); \
530 return 1; \
531 } \
532 pr_info(#func " detected saturation\n"); \
533 return 0; \
534}
535
536/*
537 * Allocator uses a trailing node argument --------+ (e.g. kmalloc_node())
538 * Allocator uses the gfp_t argument -----------+ | (e.g. kmalloc())
539 * Allocator uses a special leading argument + | | (e.g. devm_kmalloc())
540 * | | |
541 */
542DEFINE_TEST_ALLOC(kmalloc, kfree, 0, 1, 0);
543DEFINE_TEST_ALLOC(kmalloc_node, kfree, 0, 1, 1);
544DEFINE_TEST_ALLOC(kzalloc, kfree, 0, 1, 0);
545DEFINE_TEST_ALLOC(kzalloc_node, kfree, 0, 1, 1);
546DEFINE_TEST_ALLOC(vmalloc, vfree, 0, 0, 0);
547DEFINE_TEST_ALLOC(vmalloc_node, vfree, 0, 0, 1);
548DEFINE_TEST_ALLOC(vzalloc, vfree, 0, 0, 0);
549DEFINE_TEST_ALLOC(vzalloc_node, vfree, 0, 0, 1);
550DEFINE_TEST_ALLOC(kvmalloc, kvfree, 0, 1, 0);
551DEFINE_TEST_ALLOC(kvmalloc_node, kvfree, 0, 1, 1);
552DEFINE_TEST_ALLOC(kvzalloc, kvfree, 0, 1, 0);
553DEFINE_TEST_ALLOC(kvzalloc_node, kvfree, 0, 1, 1);
554DEFINE_TEST_ALLOC(devm_kmalloc, devm_kfree, 1, 1, 0);
555DEFINE_TEST_ALLOC(devm_kzalloc, devm_kfree, 1, 1, 0);
556
557static int __init test_overflow_allocation(void)
558{
559 const char device_name[] = "overflow-test";
560 struct device *dev;
561 int err = 0;
562
563 /* Create dummy device for devm_kmalloc()-family tests. */
564 dev = root_device_register(device_name);
Dan Carpenter8958fd42018-06-12 15:04:33 +0300565 if (IS_ERR(dev)) {
Kees Cookca908002018-05-10 16:40:03 -0700566 pr_warn("Cannot register test device\n");
567 return 1;
568 }
569
570 err |= test_kmalloc(NULL);
571 err |= test_kmalloc_node(NULL);
572 err |= test_kzalloc(NULL);
573 err |= test_kzalloc_node(NULL);
574 err |= test_kvmalloc(NULL);
575 err |= test_kvmalloc_node(NULL);
576 err |= test_kvzalloc(NULL);
577 err |= test_kvzalloc_node(NULL);
578 err |= test_vmalloc(NULL);
579 err |= test_vmalloc_node(NULL);
580 err |= test_vzalloc(NULL);
581 err |= test_vzalloc_node(NULL);
582 err |= test_devm_kmalloc(dev);
583 err |= test_devm_kzalloc(dev);
584
585 device_unregister(dev);
586
587 return err;
588}
589
Kees Cook8fee81a2018-05-09 22:13:28 -0700590static int __init test_module_init(void)
591{
592 int err = 0;
593
594 err |= test_overflow_calculation();
Kees Cookd36b6ad2018-08-01 14:25:40 -0700595 err |= test_overflow_shift();
Kees Cookca908002018-05-10 16:40:03 -0700596 err |= test_overflow_allocation();
Kees Cook8fee81a2018-05-09 22:13:28 -0700597
598 if (err) {
599 pr_warn("FAIL!\n");
600 err = -EINVAL;
601 } else {
602 pr_info("all tests passed\n");
603 }
604
605 return err;
Rasmus Villemoes455a35a2018-05-08 00:36:28 +0200606}
607
608static void __exit test_module_exit(void)
609{ }
610
Kees Cook8fee81a2018-05-09 22:13:28 -0700611module_init(test_module_init);
Rasmus Villemoes455a35a2018-05-08 00:36:28 +0200612module_exit(test_module_exit);
613MODULE_LICENSE("Dual MIT/GPL");