Skip to content

Commit 7d78a77

Browse files
committed
string: Add additional __realloc_size() annotations for "dup" helpers
Several other "dup"-style interfaces could use the __realloc_size() attribute. (As a reminder to myself and others: "realloc" is used here instead of "alloc" because the "alloc_size" attribute implies that the memory contents are uninitialized. Since we're copying contents into the resulting allocation, it must use "realloc_size" to avoid confusing the compiler's optimization passes.) Add KUnit test coverage where possible. (KUnit still does not have the ability to manipulate userspace memory.) Reviewed-by: Andy Shevchenko <[email protected]> Link: https://lore.kernel.org/r/[email protected] Signed-off-by: Kees Cook <[email protected]>
1 parent 74df224 commit 7d78a77

File tree

2 files changed

+34
-5
lines changed

2 files changed

+34
-5
lines changed

include/linux/string.h

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -14,8 +14,8 @@
1414
#include <uapi/linux/string.h>
1515

1616
extern char *strndup_user(const char __user *, long);
17-
extern void *memdup_user(const void __user *, size_t);
18-
extern void *vmemdup_user(const void __user *, size_t);
17+
extern void *memdup_user(const void __user *, size_t) __realloc_size(2);
18+
extern void *vmemdup_user(const void __user *, size_t) __realloc_size(2);
1919
extern void *memdup_user_nul(const void __user *, size_t);
2020

2121
/**
@@ -27,7 +27,8 @@ extern void *memdup_user_nul(const void __user *, size_t);
2727
* Return: an ERR_PTR() on failure. Result is physically
2828
* contiguous, to be freed by kfree().
2929
*/
30-
static inline void *memdup_array_user(const void __user *src, size_t n, size_t size)
30+
static inline __realloc_size(2, 3)
31+
void *memdup_array_user(const void __user *src, size_t n, size_t size)
3132
{
3233
size_t nbytes;
3334

@@ -46,7 +47,8 @@ static inline void *memdup_array_user(const void __user *src, size_t n, size_t s
4647
* Return: an ERR_PTR() on failure. Result may be not
4748
* physically contiguous. Use kvfree() to free.
4849
*/
49-
static inline void *vmemdup_array_user(const void __user *src, size_t n, size_t size)
50+
static inline __realloc_size(2, 3)
51+
void *vmemdup_array_user(const void __user *src, size_t n, size_t size)
5052
{
5153
size_t nbytes;
5254

@@ -285,7 +287,8 @@ extern char *kstrndup(const char *s, size_t len, gfp_t gfp);
285287
extern void *kmemdup(const void *src, size_t len, gfp_t gfp) __realloc_size(2);
286288
extern void *kvmemdup(const void *src, size_t len, gfp_t gfp) __realloc_size(2);
287289
extern char *kmemdup_nul(const char *s, size_t len, gfp_t gfp);
288-
extern void *kmemdup_array(const void *src, size_t element_size, size_t count, gfp_t gfp);
290+
extern void *kmemdup_array(const void *src, size_t element_size, size_t count, gfp_t gfp)
291+
__realloc_size(2, 3);
289292

290293
/* lib/argv_split.c */
291294
extern char **argv_split(gfp_t gfp, const char *str, int *argcp);

lib/fortify_kunit.c

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -363,6 +363,31 @@ DEFINE_ALLOC_SIZE_TEST_PAIR(kvmalloc)
363363
} while (0)
364364
DEFINE_ALLOC_SIZE_TEST_PAIR(devm_kmalloc)
365365

366+
static const char * const test_strs[] = {
367+
"",
368+
"Hello there",
369+
"A longer string, just for variety",
370+
};
371+
372+
#define TEST_realloc(checker) do { \
373+
gfp_t gfp = GFP_KERNEL; \
374+
size_t len; \
375+
int i; \
376+
\
377+
for (i = 0; i < ARRAY_SIZE(test_strs); i++) { \
378+
len = strlen(test_strs[i]); \
379+
KUNIT_EXPECT_EQ(test, __builtin_constant_p(len), 0); \
380+
checker(len, kmemdup_array(test_strs[i], len, 1, gfp), \
381+
kfree(p)); \
382+
checker(len, kmemdup(test_strs[i], len, gfp), \
383+
kfree(p)); \
384+
} \
385+
} while (0)
386+
static void fortify_test_realloc_size(struct kunit *test)
387+
{
388+
TEST_realloc(check_dynamic);
389+
}
390+
366391
/*
367392
* We can't have an array at the end of a structure or else
368393
* builds without -fstrict-flex-arrays=3 will report them as
@@ -1046,6 +1071,7 @@ static struct kunit_case fortify_test_cases[] = {
10461071
KUNIT_CASE(fortify_test_alloc_size_kvmalloc_dynamic),
10471072
KUNIT_CASE(fortify_test_alloc_size_devm_kmalloc_const),
10481073
KUNIT_CASE(fortify_test_alloc_size_devm_kmalloc_dynamic),
1074+
KUNIT_CASE(fortify_test_realloc_size),
10491075
KUNIT_CASE(fortify_test_strlen),
10501076
KUNIT_CASE(fortify_test_strnlen),
10511077
KUNIT_CASE(fortify_test_strcpy),

0 commit comments

Comments
 (0)