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 /* Handle being built without CONFIG_FORTIFY_SOURCE */
29 #ifndef __compiletime_strlen
30 # define __compiletime_strlen __builtin_strlen
33 static void known_sizes_test(struct kunit *test)
35 KUNIT_EXPECT_EQ(test, __compiletime_strlen("88888888"), 8);
36 KUNIT_EXPECT_EQ(test, __compiletime_strlen(array_of_10), 10);
37 KUNIT_EXPECT_EQ(test, __compiletime_strlen(ptr_of_11), 11);
39 KUNIT_EXPECT_EQ(test, __compiletime_strlen(array_unknown), SIZE_MAX);
40 /* Externally defined and dynamically sized string pointer: */
41 KUNIT_EXPECT_EQ(test, __compiletime_strlen(test->name), SIZE_MAX);
44 /* This is volatile so the optimizer can't perform DCE below. */
45 static volatile int pick;
47 /* Not inline to keep optimizer from figuring out which string we want. */
48 static noinline size_t want_minus_one(int pick)
63 return __compiletime_strlen(str);
66 static void control_flow_split_test(struct kunit *test)
68 KUNIT_EXPECT_EQ(test, want_minus_one(pick), SIZE_MAX);
71 #define KUNIT_EXPECT_BOS(test, p, expected, name) \
72 KUNIT_EXPECT_EQ_MSG(test, __builtin_object_size(p, 1), \
74 "__alloc_size() not working with __bos on " name "\n")
76 #if !__has_builtin(__builtin_dynamic_object_size)
77 #define KUNIT_EXPECT_BDOS(test, p, expected, name) \
78 /* Silence "unused variable 'expected'" warning. */ \
79 KUNIT_EXPECT_EQ(test, expected, expected)
81 #define KUNIT_EXPECT_BDOS(test, p, expected, name) \
82 KUNIT_EXPECT_EQ_MSG(test, __builtin_dynamic_object_size(p, 1), \
84 "__alloc_size() not working with __bdos on " name "\n")
87 /* If the execpted size is a constant value, __bos can see it. */
88 #define check_const(_expected, alloc, free) do { \
89 size_t expected = (_expected); \
91 KUNIT_EXPECT_TRUE_MSG(test, p != NULL, #alloc " failed?!\n"); \
92 KUNIT_EXPECT_BOS(test, p, expected, #alloc); \
93 KUNIT_EXPECT_BDOS(test, p, expected, #alloc); \
97 /* If the execpted size is NOT a constant value, __bos CANNOT see it. */
98 #define check_dynamic(_expected, alloc, free) do { \
99 size_t expected = (_expected); \
101 KUNIT_EXPECT_TRUE_MSG(test, p != NULL, #alloc " failed?!\n"); \
102 KUNIT_EXPECT_BOS(test, p, SIZE_MAX, #alloc); \
103 KUNIT_EXPECT_BDOS(test, p, expected, #alloc); \
107 /* Assortment of constant-value kinda-edge cases. */
108 #define CONST_TEST_BODY(TEST_alloc) do { \
109 /* Special-case vmalloc()-family to skip 0-sized allocs. */ \
110 if (strcmp(#TEST_alloc, "TEST_vmalloc") != 0) \
111 TEST_alloc(check_const, 0, 0); \
112 TEST_alloc(check_const, 1, 1); \
113 TEST_alloc(check_const, 128, 128); \
114 TEST_alloc(check_const, 1023, 1023); \
115 TEST_alloc(check_const, 1025, 1025); \
116 TEST_alloc(check_const, 4096, 4096); \
117 TEST_alloc(check_const, 4097, 4097); \
120 static volatile size_t zero_size;
121 static volatile size_t unknown_size = 50;
123 #if !__has_builtin(__builtin_dynamic_object_size)
124 #define DYNAMIC_TEST_BODY(TEST_alloc) \
125 kunit_skip(test, "Compiler is missing __builtin_dynamic_object_size() support\n")
127 #define DYNAMIC_TEST_BODY(TEST_alloc) do { \
128 size_t size = unknown_size; \
131 * Expected size is "size" in each test, before it is then \
132 * internally incremented in each test. Requires we disable \
135 TEST_alloc(check_dynamic, size, size++); \
136 /* Make sure incrementing actually happened. */ \
137 KUNIT_EXPECT_NE(test, size, unknown_size); \
141 #define DEFINE_ALLOC_SIZE_TEST_PAIR(allocator) \
142 static void alloc_size_##allocator##_const_test(struct kunit *test) \
144 CONST_TEST_BODY(TEST_##allocator); \
146 static void alloc_size_##allocator##_dynamic_test(struct kunit *test) \
148 DYNAMIC_TEST_BODY(TEST_##allocator); \
151 #define TEST_kmalloc(checker, expected_size, alloc_size) do { \
152 gfp_t gfp = GFP_KERNEL | __GFP_NOWARN; \
156 checker(expected_size, kmalloc(alloc_size, gfp), \
158 checker(expected_size, \
159 kmalloc_node(alloc_size, gfp, NUMA_NO_NODE), \
161 checker(expected_size, kzalloc(alloc_size, gfp), \
163 checker(expected_size, \
164 kzalloc_node(alloc_size, gfp, NUMA_NO_NODE), \
166 checker(expected_size, kcalloc(1, alloc_size, gfp), \
168 checker(expected_size, kcalloc(alloc_size, 1, gfp), \
170 checker(expected_size, \
171 kcalloc_node(1, alloc_size, gfp, NUMA_NO_NODE), \
173 checker(expected_size, \
174 kcalloc_node(alloc_size, 1, gfp, NUMA_NO_NODE), \
176 checker(expected_size, kmalloc_array(1, alloc_size, gfp), \
178 checker(expected_size, kmalloc_array(alloc_size, 1, gfp), \
180 checker(expected_size, \
181 kmalloc_array_node(1, alloc_size, gfp, NUMA_NO_NODE), \
183 checker(expected_size, \
184 kmalloc_array_node(alloc_size, 1, gfp, NUMA_NO_NODE), \
186 checker(expected_size, __kmalloc(alloc_size, gfp), \
188 checker(expected_size, \
189 __kmalloc_node(alloc_size, gfp, NUMA_NO_NODE), \
192 orig = kmalloc(alloc_size, gfp); \
193 KUNIT_EXPECT_TRUE(test, orig != NULL); \
194 checker((expected_size) * 2, \
195 krealloc(orig, (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, 1, (alloc_size) * 2, gfp), \
202 orig = kmalloc(alloc_size, gfp); \
203 KUNIT_EXPECT_TRUE(test, orig != NULL); \
204 checker((expected_size) * 2, \
205 krealloc_array(orig, (alloc_size) * 2, 1, gfp), \
209 /* Using memdup() with fixed size, so force unknown length. */ \
210 if (!__builtin_constant_p(expected_size)) \
212 checker(len, kmemdup("hello there", len, gfp), kfree(p)); \
214 DEFINE_ALLOC_SIZE_TEST_PAIR(kmalloc)
216 /* Sizes are in pages, not bytes. */
217 #define TEST_vmalloc(checker, expected_pages, alloc_pages) do { \
218 gfp_t gfp = GFP_KERNEL | __GFP_NOWARN; \
219 checker((expected_pages) * PAGE_SIZE, \
220 vmalloc((alloc_pages) * PAGE_SIZE), vfree(p)); \
221 checker((expected_pages) * PAGE_SIZE, \
222 vzalloc((alloc_pages) * PAGE_SIZE), vfree(p)); \
223 checker((expected_pages) * PAGE_SIZE, \
224 __vmalloc((alloc_pages) * PAGE_SIZE, gfp), vfree(p)); \
226 DEFINE_ALLOC_SIZE_TEST_PAIR(vmalloc)
228 /* Sizes are in pages (and open-coded for side-effects), not bytes. */
229 #define TEST_kvmalloc(checker, expected_pages, alloc_pages) do { \
230 gfp_t gfp = GFP_KERNEL | __GFP_NOWARN; \
234 checker((expected_pages) * PAGE_SIZE, \
235 kvmalloc((alloc_pages) * PAGE_SIZE, gfp), \
237 checker((expected_pages) * PAGE_SIZE, \
238 kvmalloc_node((alloc_pages) * PAGE_SIZE, gfp, NUMA_NO_NODE), \
240 checker((expected_pages) * PAGE_SIZE, \
241 kvzalloc((alloc_pages) * PAGE_SIZE, gfp), \
243 checker((expected_pages) * PAGE_SIZE, \
244 kvzalloc_node((alloc_pages) * PAGE_SIZE, gfp, NUMA_NO_NODE), \
246 checker((expected_pages) * PAGE_SIZE, \
247 kvcalloc(1, (alloc_pages) * PAGE_SIZE, gfp), \
249 checker((expected_pages) * PAGE_SIZE, \
250 kvcalloc((alloc_pages) * PAGE_SIZE, 1, gfp), \
252 checker((expected_pages) * PAGE_SIZE, \
253 kvmalloc_array(1, (alloc_pages) * PAGE_SIZE, gfp), \
255 checker((expected_pages) * PAGE_SIZE, \
256 kvmalloc_array((alloc_pages) * PAGE_SIZE, 1, gfp), \
259 prev_size = (expected_pages) * PAGE_SIZE; \
260 orig = kvmalloc(prev_size, gfp); \
261 KUNIT_EXPECT_TRUE(test, orig != NULL); \
262 checker(((expected_pages) * PAGE_SIZE) * 2, \
263 kvrealloc(orig, prev_size, \
264 ((alloc_pages) * PAGE_SIZE) * 2, gfp), \
267 DEFINE_ALLOC_SIZE_TEST_PAIR(kvmalloc)
269 #define TEST_devm_kmalloc(checker, expected_size, alloc_size) do { \
270 gfp_t gfp = GFP_KERNEL | __GFP_NOWARN; \
271 const char dev_name[] = "fortify-test"; \
272 struct device *dev; \
276 /* Create dummy device for devm_kmalloc()-family tests. */ \
277 dev = root_device_register(dev_name); \
278 KUNIT_ASSERT_FALSE_MSG(test, IS_ERR(dev), \
279 "Cannot register test device\n"); \
281 checker(expected_size, devm_kmalloc(dev, alloc_size, gfp), \
282 devm_kfree(dev, p)); \
283 checker(expected_size, devm_kzalloc(dev, alloc_size, gfp), \
284 devm_kfree(dev, p)); \
285 checker(expected_size, \
286 devm_kmalloc_array(dev, 1, alloc_size, gfp), \
287 devm_kfree(dev, p)); \
288 checker(expected_size, \
289 devm_kmalloc_array(dev, alloc_size, 1, gfp), \
290 devm_kfree(dev, p)); \
291 checker(expected_size, \
292 devm_kcalloc(dev, 1, alloc_size, gfp), \
293 devm_kfree(dev, p)); \
294 checker(expected_size, \
295 devm_kcalloc(dev, alloc_size, 1, gfp), \
296 devm_kfree(dev, p)); \
298 orig = devm_kmalloc(dev, alloc_size, gfp); \
299 KUNIT_EXPECT_TRUE(test, orig != NULL); \
300 checker((expected_size) * 2, \
301 devm_krealloc(dev, orig, (alloc_size) * 2, gfp), \
302 devm_kfree(dev, p)); \
305 /* Using memdup() with fixed size, so force unknown length. */ \
306 if (!__builtin_constant_p(expected_size)) \
308 checker(len, devm_kmemdup(dev, "Ohai", len, gfp), \
309 devm_kfree(dev, p)); \
311 device_unregister(dev); \
313 DEFINE_ALLOC_SIZE_TEST_PAIR(devm_kmalloc)
315 static int fortify_test_init(struct kunit *test)
317 if (!IS_ENABLED(CONFIG_FORTIFY_SOURCE))
318 kunit_skip(test, "Not built with CONFIG_FORTIFY_SOURCE=y");
323 static struct kunit_case fortify_test_cases[] = {
324 KUNIT_CASE(known_sizes_test),
325 KUNIT_CASE(control_flow_split_test),
326 KUNIT_CASE(alloc_size_kmalloc_const_test),
327 KUNIT_CASE(alloc_size_kmalloc_dynamic_test),
328 KUNIT_CASE(alloc_size_vmalloc_const_test),
329 KUNIT_CASE(alloc_size_vmalloc_dynamic_test),
330 KUNIT_CASE(alloc_size_kvmalloc_const_test),
331 KUNIT_CASE(alloc_size_kvmalloc_dynamic_test),
332 KUNIT_CASE(alloc_size_devm_kmalloc_const_test),
333 KUNIT_CASE(alloc_size_devm_kmalloc_dynamic_test),
337 static struct kunit_suite fortify_test_suite = {
339 .init = fortify_test_init,
340 .test_cases = fortify_test_cases,
343 kunit_test_suite(fortify_test_suite);
345 MODULE_LICENSE("GPL");