Skip to content

Commit 9e4a617

Browse files
committed
string: Add __realloc_size hint to kmemdup()
Add __realloc_size() hint to kmemdup() so the compiler can reason about the length of the returned buffer. (These must not use __alloc_size, since those include __malloc which says the contents aren't defined[1]). [1] https://lore.kernel.org/linux-hardening/[email protected]/ Cc: Rasmus Villemoes <[email protected]> Cc: Guenter Roeck <[email protected]> Cc: Andy Shevchenko <[email protected]> Cc: Paolo Abeni <[email protected]> Cc: Geert Uytterhoeven <[email protected]> Signed-off-by: Kees Cook <[email protected]>
1 parent 96fce38 commit 9e4a617

File tree

2 files changed

+3
-2
lines changed

2 files changed

+3
-2
lines changed

include/linux/fortify-string.h

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -659,7 +659,8 @@ __FORTIFY_INLINE void *memchr_inv(const void * const POS0 p, int c, size_t size)
659659
return __real_memchr_inv(p, c, size);
660660
}
661661

662-
extern void *__real_kmemdup(const void *src, size_t len, gfp_t gfp) __RENAME(kmemdup);
662+
extern void *__real_kmemdup(const void *src, size_t len, gfp_t gfp) __RENAME(kmemdup)
663+
__realloc_size(2);
663664
__FORTIFY_INLINE void *kmemdup(const void * const POS0 p, size_t size, gfp_t gfp)
664665
{
665666
size_t p_size = __struct_size(p);

include/linux/string.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -176,7 +176,7 @@ extern void kfree_const(const void *x);
176176
extern char *kstrdup(const char *s, gfp_t gfp) __malloc;
177177
extern const char *kstrdup_const(const char *s, gfp_t gfp);
178178
extern char *kstrndup(const char *s, size_t len, gfp_t gfp);
179-
extern void *kmemdup(const void *src, size_t len, gfp_t gfp);
179+
extern void *kmemdup(const void *src, size_t len, gfp_t gfp) __realloc_size(2);
180180
extern char *kmemdup_nul(const char *s, size_t len, gfp_t gfp);
181181

182182
extern char **argv_split(gfp_t gfp, const char *str, int *argcp);

0 commit comments

Comments
 (0)