1 // SPDX-License-Identifier: GPL-2.0
3 * Runtime test cases for CONFIG_FORTIFY_SOURCE that aren't expected to
4 * Oops the kernel on success. (For those, see drivers/misc/lkdtm/fortify.c)
6 * For corner cases with UBSAN, try testing with:
8 * ./tools/testing/kunit/kunit.py run --arch=x86_64 \
9 * --kconfig_add CONFIG_FORTIFY_SOURCE=y \
10 * --kconfig_add CONFIG_UBSAN=y \
11 * --kconfig_add CONFIG_UBSAN_TRAP=y \
12 * --kconfig_add CONFIG_UBSAN_BOUNDS=y \
13 * --kconfig_add CONFIG_UBSAN_LOCAL_BOUNDS=y \
14 * --make_options LLVM=1 fortify
16 #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
18 #include <kunit/test.h>
19 #include <linux/device.h>
20 #include <linux/slab.h>
21 #include <linux/string.h>
22 #include <linux/vmalloc.h>
24 static const char array_of_10[] = "this is 10";
25 static const char *ptr_of_11 = "this is 11!";
26 static char array_unknown[] = "compiler thinks I might change";
28 static void known_sizes_test(struct kunit *test)
30 KUNIT_EXPECT_EQ(test, __compiletime_strlen("88888888"), 8);
31 KUNIT_EXPECT_EQ(test, __compiletime_strlen(array_of_10), 10);
32 KUNIT_EXPECT_EQ(test, __compiletime_strlen(ptr_of_11), 11);
34 KUNIT_EXPECT_EQ(test, __compiletime_strlen(array_unknown), SIZE_MAX);
35 /* Externally defined and dynamically sized string pointer: */
36 KUNIT_EXPECT_EQ(test, __compiletime_strlen(test->name), SIZE_MAX);
39 /* This is volatile so the optimizer can't perform DCE below. */
40 static volatile int pick;
42 /* Not inline to keep optimizer from figuring out which string we want. */
43 static noinline size_t want_minus_one(int pick)
58 return __compiletime_strlen(str);
61 static void control_flow_split_test(struct kunit *test)
63 KUNIT_EXPECT_EQ(test, want_minus_one(pick), SIZE_MAX);
66 #define KUNIT_EXPECT_BOS(test, p, expected, name) \
67 KUNIT_EXPECT_EQ_MSG(test, __builtin_object_size(p, 1), \
69 "__alloc_size() not working with __bos on " name "\n")
71 #if !__has_builtin(__builtin_dynamic_object_size)
72 #define KUNIT_EXPECT_BDOS(test, p, expected, name) \
73 /* Silence "unused variable 'expected'" warning. */ \
74 KUNIT_EXPECT_EQ(test, expected, expected)
76 #define KUNIT_EXPECT_BDOS(test, p, expected, name) \
77 KUNIT_EXPECT_EQ_MSG(test, __builtin_dynamic_object_size(p, 1), \
79 "__alloc_size() not working with __bdos on " name "\n")
82 /* If the execpted size is a constant value, __bos can see it. */
83 #define check_const(_expected, alloc, free) do { \
84 size_t expected = (_expected); \
86 KUNIT_EXPECT_TRUE_MSG(test, p != NULL, #alloc " failed?!\n"); \
87 KUNIT_EXPECT_BOS(test, p, expected, #alloc); \
88 KUNIT_EXPECT_BDOS(test, p, expected, #alloc); \
92 /* If the execpted size is NOT a constant value, __bos CANNOT see it. */
93 #define check_dynamic(_expected, alloc, free) do { \
94 size_t expected = (_expected); \
96 KUNIT_EXPECT_TRUE_MSG(test, p != NULL, #alloc " failed?!\n"); \
97 KUNIT_EXPECT_BOS(test, p, SIZE_MAX, #alloc); \
98 KUNIT_EXPECT_BDOS(test, p, expected, #alloc); \
102 /* Assortment of constant-value kinda-edge cases. */
103 #define CONST_TEST_BODY(TEST_alloc) do { \
104 /* Special-case vmalloc()-family to skip 0-sized allocs. */ \
105 if (strcmp(#TEST_alloc, "TEST_vmalloc") != 0) \
106 TEST_alloc(check_const, 0, 0); \
107 TEST_alloc(check_const, 1, 1); \
108 TEST_alloc(check_const, 128, 128); \
109 TEST_alloc(check_const, 1023, 1023); \
110 TEST_alloc(check_const, 1025, 1025); \
111 TEST_alloc(check_const, 4096, 4096); \
112 TEST_alloc(check_const, 4097, 4097); \
115 static volatile size_t zero_size;
116 static volatile size_t unknown_size = 50;
118 #if !__has_builtin(__builtin_dynamic_object_size)
119 #define DYNAMIC_TEST_BODY(TEST_alloc) \
120 kunit_skip(test, "Compiler is missing __builtin_dynamic_object_size() support\n")
122 #define DYNAMIC_TEST_BODY(TEST_alloc) do { \
123 size_t size = unknown_size; \
126 * Expected size is "size" in each test, before it is then \
127 * internally incremented in each test. Requires we disable \
130 TEST_alloc(check_dynamic, size, size++); \
131 /* Make sure incrementing actually happened. */ \
132 KUNIT_EXPECT_NE(test, size, unknown_size); \
136 #define DEFINE_ALLOC_SIZE_TEST_PAIR(allocator) \
137 static void alloc_size_##allocator##_const_test(struct kunit *test) \
139 CONST_TEST_BODY(TEST_##allocator); \
141 static void alloc_size_##allocator##_dynamic_test(struct kunit *test) \
143 DYNAMIC_TEST_BODY(TEST_##allocator); \
146 #define TEST_kmalloc(checker, expected_size, alloc_size) do { \
147 gfp_t gfp = GFP_KERNEL | __GFP_NOWARN; \
151 checker(expected_size, kmalloc(alloc_size, gfp), \
153 checker(expected_size, \
154 kmalloc_node(alloc_size, gfp, NUMA_NO_NODE), \
156 checker(expected_size, kzalloc(alloc_size, gfp), \
158 checker(expected_size, \
159 kzalloc_node(alloc_size, gfp, NUMA_NO_NODE), \
161 checker(expected_size, kcalloc(1, alloc_size, gfp), \
163 checker(expected_size, kcalloc(alloc_size, 1, gfp), \
165 checker(expected_size, \
166 kcalloc_node(1, alloc_size, gfp, NUMA_NO_NODE), \
168 checker(expected_size, \
169 kcalloc_node(alloc_size, 1, gfp, NUMA_NO_NODE), \
171 checker(expected_size, kmalloc_array(1, alloc_size, gfp), \
173 checker(expected_size, kmalloc_array(alloc_size, 1, gfp), \
175 checker(expected_size, \
176 kmalloc_array_node(1, alloc_size, gfp, NUMA_NO_NODE), \
178 checker(expected_size, \
179 kmalloc_array_node(alloc_size, 1, gfp, NUMA_NO_NODE), \
181 checker(expected_size, __kmalloc(alloc_size, gfp), \
183 checker(expected_size, \
184 __kmalloc_node(alloc_size, gfp, NUMA_NO_NODE), \
187 orig = kmalloc(alloc_size, gfp); \
188 KUNIT_EXPECT_TRUE(test, orig != NULL); \
189 checker((expected_size) * 2, \
190 krealloc(orig, (alloc_size) * 2, gfp), \
192 orig = kmalloc(alloc_size, gfp); \
193 KUNIT_EXPECT_TRUE(test, orig != NULL); \
194 checker((expected_size) * 2, \
195 krealloc_array(orig, 1, (alloc_size) * 2, gfp), \
197 orig = kmalloc(alloc_size, gfp); \
198 KUNIT_EXPECT_TRUE(test, orig != NULL); \
199 checker((expected_size) * 2, \
200 krealloc_array(orig, (alloc_size) * 2, 1, gfp), \
204 /* Using memdup() with fixed size, so force unknown length. */ \
205 if (!__builtin_constant_p(expected_size)) \
207 checker(len, kmemdup("hello there", len, gfp), kfree(p)); \
209 DEFINE_ALLOC_SIZE_TEST_PAIR(kmalloc)
211 /* Sizes are in pages, not bytes. */
212 #define TEST_vmalloc(checker, expected_pages, alloc_pages) do { \
213 gfp_t gfp = GFP_KERNEL | __GFP_NOWARN; \
214 checker((expected_pages) * PAGE_SIZE, \
215 vmalloc((alloc_pages) * PAGE_SIZE), vfree(p)); \
216 checker((expected_pages) * PAGE_SIZE, \
217 vzalloc((alloc_pages) * PAGE_SIZE), vfree(p)); \
218 checker((expected_pages) * PAGE_SIZE, \
219 __vmalloc((alloc_pages) * PAGE_SIZE, gfp), vfree(p)); \
221 DEFINE_ALLOC_SIZE_TEST_PAIR(vmalloc)
223 /* Sizes are in pages (and open-coded for side-effects), not bytes. */
224 #define TEST_kvmalloc(checker, expected_pages, alloc_pages) do { \
225 gfp_t gfp = GFP_KERNEL | __GFP_NOWARN; \
229 checker((expected_pages) * PAGE_SIZE, \
230 kvmalloc((alloc_pages) * PAGE_SIZE, gfp), \
232 checker((expected_pages) * PAGE_SIZE, \
233 kvmalloc_node((alloc_pages) * PAGE_SIZE, gfp, NUMA_NO_NODE), \
235 checker((expected_pages) * PAGE_SIZE, \
236 kvzalloc((alloc_pages) * PAGE_SIZE, gfp), \
238 checker((expected_pages) * PAGE_SIZE, \
239 kvzalloc_node((alloc_pages) * PAGE_SIZE, gfp, NUMA_NO_NODE), \
241 checker((expected_pages) * PAGE_SIZE, \
242 kvcalloc(1, (alloc_pages) * PAGE_SIZE, gfp), \
244 checker((expected_pages) * PAGE_SIZE, \
245 kvcalloc((alloc_pages) * PAGE_SIZE, 1, gfp), \
247 checker((expected_pages) * PAGE_SIZE, \
248 kvmalloc_array(1, (alloc_pages) * PAGE_SIZE, gfp), \
250 checker((expected_pages) * PAGE_SIZE, \
251 kvmalloc_array((alloc_pages) * PAGE_SIZE, 1, gfp), \
254 prev_size = (expected_pages) * PAGE_SIZE; \
255 orig = kvmalloc(prev_size, gfp); \
256 KUNIT_EXPECT_TRUE(test, orig != NULL); \
257 checker(((expected_pages) * PAGE_SIZE) * 2, \
258 kvrealloc(orig, prev_size, \
259 ((alloc_pages) * PAGE_SIZE) * 2, gfp), \
262 DEFINE_ALLOC_SIZE_TEST_PAIR(kvmalloc)
264 #define TEST_devm_kmalloc(checker, expected_size, alloc_size) do { \
265 gfp_t gfp = GFP_KERNEL | __GFP_NOWARN; \
266 const char dev_name[] = "fortify-test"; \
267 struct device *dev; \
271 /* Create dummy device for devm_kmalloc()-family tests. */ \
272 dev = root_device_register(dev_name); \
273 KUNIT_ASSERT_FALSE_MSG(test, IS_ERR(dev), \
274 "Cannot register test device\n"); \
276 checker(expected_size, devm_kmalloc(dev, alloc_size, gfp), \
277 devm_kfree(dev, p)); \
278 checker(expected_size, devm_kzalloc(dev, alloc_size, gfp), \
279 devm_kfree(dev, p)); \
280 checker(expected_size, \
281 devm_kmalloc_array(dev, 1, alloc_size, gfp), \
282 devm_kfree(dev, p)); \
283 checker(expected_size, \
284 devm_kmalloc_array(dev, alloc_size, 1, gfp), \
285 devm_kfree(dev, p)); \
286 checker(expected_size, \
287 devm_kcalloc(dev, 1, alloc_size, gfp), \
288 devm_kfree(dev, p)); \
289 checker(expected_size, \
290 devm_kcalloc(dev, alloc_size, 1, gfp), \
291 devm_kfree(dev, p)); \
293 orig = devm_kmalloc(dev, alloc_size, gfp); \
294 KUNIT_EXPECT_TRUE(test, orig != NULL); \
295 checker((expected_size) * 2, \
296 devm_krealloc(dev, orig, (alloc_size) * 2, gfp), \
297 devm_kfree(dev, p)); \
300 /* Using memdup() with fixed size, so force unknown length. */ \
301 if (!__builtin_constant_p(expected_size)) \
303 checker(len, devm_kmemdup(dev, "Ohai", len, gfp), \
304 devm_kfree(dev, p)); \
306 device_unregister(dev); \
308 DEFINE_ALLOC_SIZE_TEST_PAIR(devm_kmalloc)
310 static struct kunit_case fortify_test_cases[] = {
311 KUNIT_CASE(known_sizes_test),
312 KUNIT_CASE(control_flow_split_test),
313 KUNIT_CASE(alloc_size_kmalloc_const_test),
314 KUNIT_CASE(alloc_size_kmalloc_dynamic_test),
315 KUNIT_CASE(alloc_size_vmalloc_const_test),
316 KUNIT_CASE(alloc_size_vmalloc_dynamic_test),
317 KUNIT_CASE(alloc_size_kvmalloc_const_test),
318 KUNIT_CASE(alloc_size_kvmalloc_dynamic_test),
319 KUNIT_CASE(alloc_size_devm_kmalloc_const_test),
320 KUNIT_CASE(alloc_size_devm_kmalloc_dynamic_test),
324 static struct kunit_suite fortify_test_suite = {
326 .test_cases = fortify_test_cases,
329 kunit_test_suite(fortify_test_suite);
331 MODULE_LICENSE("GPL");