Skip to content

Commit 9124a26

Browse files
committed
kunit/fortify: Validate __alloc_size attribute results
Validate the effect of the __alloc_size attribute on allocators. If the compiler doesn't support __builtin_dynamic_object_size(), skip the associated tests. (For GCC, just remove the "--make_options" line below...) $ ./tools/testing/kunit/kunit.py run --arch x86_64 \ --kconfig_add CONFIG_FORTIFY_SOURCE=y \ --make_options LLVM=1 fortify ... [15:16:30] ================== fortify (10 subtests) =================== [15:16:30] [PASSED] known_sizes_test [15:16:30] [PASSED] control_flow_split_test [15:16:30] [PASSED] alloc_size_kmalloc_const_test [15:16:30] [PASSED] alloc_size_kmalloc_dynamic_test [15:16:30] [PASSED] alloc_size_vmalloc_const_test [15:16:30] [PASSED] alloc_size_vmalloc_dynamic_test [15:16:30] [PASSED] alloc_size_kvmalloc_const_test [15:16:30] [PASSED] alloc_size_kvmalloc_dynamic_test [15:16:30] [PASSED] alloc_size_devm_kmalloc_const_test [15:16:30] [PASSED] alloc_size_devm_kmalloc_dynamic_test [15:16:30] ===================== [PASSED] fortify ===================== [15:16:30] ============================================================ [15:16:30] Testing complete. Ran 10 tests: passed: 10 [15:16:31] Elapsed time: 8.348s total, 0.002s configuring, 6.923s building, 1.075s running For earlier GCC prior to version 12, the dynamic tests will be skipped: [15:18:59] ================== fortify (10 subtests) =================== [15:18:59] [PASSED] known_sizes_test [15:18:59] [PASSED] control_flow_split_test [15:18:59] [PASSED] alloc_size_kmalloc_const_test [15:18:59] [SKIPPED] alloc_size_kmalloc_dynamic_test [15:18:59] [PASSED] alloc_size_vmalloc_const_test [15:18:59] [SKIPPED] alloc_size_vmalloc_dynamic_test [15:18:59] [PASSED] alloc_size_kvmalloc_const_test [15:18:59] [SKIPPED] alloc_size_kvmalloc_dynamic_test [15:18:59] [PASSED] alloc_size_devm_kmalloc_const_test [15:18:59] [SKIPPED] alloc_size_devm_kmalloc_dynamic_test [15:18:59] ===================== [PASSED] fortify ===================== [15:18:59] ============================================================ [15:18:59] Testing complete. Ran 10 tests: passed: 6, skipped: 4 [15:18:59] Elapsed time: 11.965s total, 0.002s configuring, 10.540s building, 1.068s running Cc: David Gow <[email protected]> Cc: [email protected] Signed-off-by: Kees Cook <[email protected]>
1 parent 0ad811c commit 9124a26

File tree

2 files changed

+256
-0
lines changed

2 files changed

+256
-0
lines changed

lib/Makefile

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -378,6 +378,7 @@ CFLAGS_overflow_kunit.o = $(call cc-disable-warning, tautological-constant-out-o
378378
obj-$(CONFIG_OVERFLOW_KUNIT_TEST) += overflow_kunit.o
379379
CFLAGS_stackinit_kunit.o += $(call cc-disable-warning, switch-unreachable)
380380
obj-$(CONFIG_STACKINIT_KUNIT_TEST) += stackinit_kunit.o
381+
CFLAGS_fortify_kunit.o += $(call cc-disable-warning, unsequenced)
381382
obj-$(CONFIG_FORTIFY_KUNIT_TEST) += fortify_kunit.o
382383
obj-$(CONFIG_STRSCPY_KUNIT_TEST) += strscpy_kunit.o
383384
obj-$(CONFIG_SIPHASH_KUNIT_TEST) += siphash_kunit.o

lib/fortify_kunit.c

Lines changed: 255 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,10 @@
1616
#define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
1717

1818
#include <kunit/test.h>
19+
#include <linux/device.h>
20+
#include <linux/slab.h>
1921
#include <linux/string.h>
22+
#include <linux/vmalloc.h>
2023

2124
static const char array_of_10[] = "this is 10";
2225
static const char *ptr_of_11 = "this is 11!";
@@ -60,9 +63,261 @@ static void control_flow_split_test(struct kunit *test)
6063
KUNIT_EXPECT_EQ(test, want_minus_one(pick), SIZE_MAX);
6164
}
6265

66+
#define KUNIT_EXPECT_BOS(test, p, expected, name) \
67+
KUNIT_EXPECT_EQ_MSG(test, __builtin_object_size(p, 1), \
68+
expected, \
69+
"__alloc_size() not working with __bos on " name "\n")
70+
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)
75+
#else
76+
#define KUNIT_EXPECT_BDOS(test, p, expected, name) \
77+
KUNIT_EXPECT_EQ_MSG(test, __builtin_dynamic_object_size(p, 1), \
78+
expected, \
79+
"__alloc_size() not working with __bdos on " name "\n")
80+
#endif
81+
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); \
85+
void *p = alloc; \
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); \
89+
free; \
90+
} while (0)
91+
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); \
95+
void *p = alloc; \
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); \
99+
free; \
100+
} while (0)
101+
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); \
113+
} while (0)
114+
115+
static volatile size_t zero_size;
116+
static volatile size_t unknown_size = 50;
117+
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")
121+
#else
122+
#define DYNAMIC_TEST_BODY(TEST_alloc) do { \
123+
size_t size = unknown_size; \
124+
\
125+
/* \
126+
* Expected size is "size" in each test, before it is then \
127+
* internally incremented in each test. Requires we disable \
128+
* -Wunsequenced. \
129+
*/ \
130+
TEST_alloc(check_dynamic, size, size++); \
131+
/* Make sure incrementing actually happened. */ \
132+
KUNIT_EXPECT_NE(test, size, unknown_size); \
133+
} while (0)
134+
#endif
135+
136+
#define DEFINE_ALLOC_SIZE_TEST_PAIR(allocator) \
137+
static void alloc_size_##allocator##_const_test(struct kunit *test) \
138+
{ \
139+
CONST_TEST_BODY(TEST_##allocator); \
140+
} \
141+
static void alloc_size_##allocator##_dynamic_test(struct kunit *test) \
142+
{ \
143+
DYNAMIC_TEST_BODY(TEST_##allocator); \
144+
}
145+
146+
#define TEST_kmalloc(checker, expected_size, alloc_size) do { \
147+
gfp_t gfp = GFP_KERNEL | __GFP_NOWARN; \
148+
void *orig; \
149+
size_t len; \
150+
\
151+
checker(expected_size, kmalloc(alloc_size, gfp), \
152+
kfree(p)); \
153+
checker(expected_size, \
154+
kmalloc_node(alloc_size, gfp, NUMA_NO_NODE), \
155+
kfree(p)); \
156+
checker(expected_size, kzalloc(alloc_size, gfp), \
157+
kfree(p)); \
158+
checker(expected_size, \
159+
kzalloc_node(alloc_size, gfp, NUMA_NO_NODE), \
160+
kfree(p)); \
161+
checker(expected_size, kcalloc(1, alloc_size, gfp), \
162+
kfree(p)); \
163+
checker(expected_size, kcalloc(alloc_size, 1, gfp), \
164+
kfree(p)); \
165+
checker(expected_size, \
166+
kcalloc_node(1, alloc_size, gfp, NUMA_NO_NODE), \
167+
kfree(p)); \
168+
checker(expected_size, \
169+
kcalloc_node(alloc_size, 1, gfp, NUMA_NO_NODE), \
170+
kfree(p)); \
171+
checker(expected_size, kmalloc_array(1, alloc_size, gfp), \
172+
kfree(p)); \
173+
checker(expected_size, kmalloc_array(alloc_size, 1, gfp), \
174+
kfree(p)); \
175+
checker(expected_size, \
176+
kmalloc_array_node(1, alloc_size, gfp, NUMA_NO_NODE), \
177+
kfree(p)); \
178+
checker(expected_size, \
179+
kmalloc_array_node(alloc_size, 1, gfp, NUMA_NO_NODE), \
180+
kfree(p)); \
181+
checker(expected_size, __kmalloc(alloc_size, gfp), \
182+
kfree(p)); \
183+
checker(expected_size, \
184+
__kmalloc_node(alloc_size, gfp, NUMA_NO_NODE), \
185+
kfree(p)); \
186+
\
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), \
191+
kfree(p)); \
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), \
196+
kfree(p)); \
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), \
201+
kfree(p)); \
202+
\
203+
len = 11; \
204+
/* Using memdup() with fixed size, so force unknown length. */ \
205+
if (!__builtin_constant_p(expected_size)) \
206+
len += zero_size; \
207+
checker(len, kmemdup("hello there", len, gfp), kfree(p)); \
208+
} while (0)
209+
DEFINE_ALLOC_SIZE_TEST_PAIR(kmalloc)
210+
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)); \
220+
} while (0)
221+
DEFINE_ALLOC_SIZE_TEST_PAIR(vmalloc)
222+
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; \
226+
size_t prev_size; \
227+
void *orig; \
228+
\
229+
checker((expected_pages) * PAGE_SIZE, \
230+
kvmalloc((alloc_pages) * PAGE_SIZE, gfp), \
231+
vfree(p)); \
232+
checker((expected_pages) * PAGE_SIZE, \
233+
kvmalloc_node((alloc_pages) * PAGE_SIZE, gfp, NUMA_NO_NODE), \
234+
vfree(p)); \
235+
checker((expected_pages) * PAGE_SIZE, \
236+
kvzalloc((alloc_pages) * PAGE_SIZE, gfp), \
237+
vfree(p)); \
238+
checker((expected_pages) * PAGE_SIZE, \
239+
kvzalloc_node((alloc_pages) * PAGE_SIZE, gfp, NUMA_NO_NODE), \
240+
vfree(p)); \
241+
checker((expected_pages) * PAGE_SIZE, \
242+
kvcalloc(1, (alloc_pages) * PAGE_SIZE, gfp), \
243+
vfree(p)); \
244+
checker((expected_pages) * PAGE_SIZE, \
245+
kvcalloc((alloc_pages) * PAGE_SIZE, 1, gfp), \
246+
vfree(p)); \
247+
checker((expected_pages) * PAGE_SIZE, \
248+
kvmalloc_array(1, (alloc_pages) * PAGE_SIZE, gfp), \
249+
vfree(p)); \
250+
checker((expected_pages) * PAGE_SIZE, \
251+
kvmalloc_array((alloc_pages) * PAGE_SIZE, 1, gfp), \
252+
vfree(p)); \
253+
\
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), \
260+
kvfree(p)); \
261+
} while (0)
262+
DEFINE_ALLOC_SIZE_TEST_PAIR(kvmalloc)
263+
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; \
268+
void *orig; \
269+
size_t len; \
270+
\
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"); \
275+
\
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)); \
292+
\
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)); \
298+
\
299+
len = 4; \
300+
/* Using memdup() with fixed size, so force unknown length. */ \
301+
if (!__builtin_constant_p(expected_size)) \
302+
len += zero_size; \
303+
checker(len, devm_kmemdup(dev, "Ohai", len, gfp), \
304+
devm_kfree(dev, p)); \
305+
\
306+
device_unregister(dev); \
307+
} while (0)
308+
DEFINE_ALLOC_SIZE_TEST_PAIR(devm_kmalloc)
309+
63310
static struct kunit_case fortify_test_cases[] = {
64311
KUNIT_CASE(known_sizes_test),
65312
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),
66321
{}
67322
};
68323

0 commit comments

Comments
 (0)