Skip to contentSkip to conversation

driver core: Add __alloc_size hint to devm allocators

PATCH In review Kees Cook 2 participants 2 messages Oct 29, 2022 Driver core

lore
Share & tools
lore

Kees Cook

author
#0 lore raw

PATCH v2 1/1 driver core: Add __alloc_size hint to devm allocators

Mark the devm_*alloc()-family of allocations with appropriate __alloc_size()/__realloc_size() hints so the compiler can attempt to reason about buffer lengths from allocations.

Cc Greg Kroah-Hartman <[email protected]>
Cc Rasmus Villemoes <[email protected]>
Cc Thomas Gleixner <[email protected]>
Cc Jason Gunthorpe <[email protected]>
Cc Nishanth Menon <[email protected]>
Cc Michael Kelley <[email protected]>
Cc Dan Williams <[email protected]>
Cc Won Chung <[email protected]>

---
I'm hoping to carry this via the hardening tree so I can add a
KUnit test that depends on it...
v2: use __realloc_size instead of __alloc_size
v1: https://lore.kernel.org/linux-hardening/[email protected]/

---
 include/linux/device.h | 7 ++++---
 1 file changed, 4 insertions(+), 3 deletions(-)
include/linux/device.h +4 -3
index 424b55df0272..5e4cd857e74f 100644
@@ -197,9 +197,9 @@ void devres_remove_group(struct device *dev, void *id);
 int devres_release_group(struct device *dev, void *id);
 
 /* managed devm_k.alloc/kfree for device drivers */
-void *devm_kmalloc(struct device *dev, size_t size, gfp_t gfp) __malloc;
+void *devm_kmalloc(struct device *dev, size_t size, gfp_t gfp) __alloc_size(2);
 void *devm_krealloc(struct device *dev, void *ptr, size_t size,
-		    gfp_t gfp) __must_check;
+		    gfp_t gfp) __must_check __realloc_size(3);
 __printf(3, 0) char *devm_kvasprintf(struct device *dev, gfp_t gfp,
 				     const char *fmt, va_list ap) __malloc;
 __printf(3, 4) char *devm_kasprintf(struct device *dev, gfp_t gfp,
@@ -226,7 +226,8 @@ static inline void *devm_kcalloc(struct device *dev,
 void devm_kfree(struct device *dev, const void *p);
 char *devm_kstrdup(struct device *dev, const char *s, gfp_t gfp) __malloc;
 const char *devm_kstrdup_const(struct device *dev, const char *s, gfp_t gfp);
-void *devm_kmemdup(struct device *dev, const void *src, size_t len, gfp_t gfp);
+void *devm_kmemdup(struct device *dev, const void *src, size_t len, gfp_t gfp)
+	__realloc_size(3);
 
 unsigned long devm_get_free_pages(struct device *dev,
 				  gfp_t gfp_mask, unsigned int order);
Signature · 1 line
2.34.1
maintainer Reviewed-by on v2 1/1
#1 lore raw
Quoted 13 lines · Kees Cook

Mark the devm_*alloc()-family of allocations with appropriate __alloc_size()/__realloc_size() hints so the compiler can attempt to reason about buffer lengths from allocations.

Cc Greg Kroah-Hartman <[email protected]>
Cc Rasmus Villemoes <[email protected]>
Cc Thomas Gleixner <[email protected]>
Cc Jason Gunthorpe <[email protected]>
Cc Nishanth Menon <[email protected]>
Cc Michael Kelley <[email protected]>
Cc Dan Williams <[email protected]>
Cc Won Chung <[email protected]>
Quoted 9 lines

---
I'm hoping to carry this via the hardening tree so I can add a
KUnit test that depends on it...
v2: use __realloc_size instead of __alloc_size
v1: https://lore.kernel.org/linux-hardening/[email protected]/

---
 include/linux/device.h | 7 ++++---
 1 file changed, 4 insertions(+), 3 deletions(-)