Skip to contentSkip to conversation

slab: Saturate to SIZE_MAX for allocation size overflows

PATCH In review Kees Cook 4 participants 5 messages Feb 25 – Feb 26

Subsystems (2)
lore
Share & tools
lore

Kees Cook

author
#0 lore raw

PATCH 1/1 slab: Saturate to SIZE_MAX for allocation size overflows

Instead of silently returning NULL on size overflows from array allocations, saturate the request to SIZE_MAX so the error will be surfaced to the allocator (and still return NULL).

---
Cc: Vlastimil Babka <[email protected]>
Cc: Andrew Morton <[email protected]>
Cc: Christoph Lameter <[email protected]>
Cc: David Rientjes <[email protected]>
Cc: Roman Gushchin <[email protected]>
Cc: Harry Yoo <[email protected]>
Cc: <[email protected]>

---
 include/linux/slab.h | 8 ++++----
 1 file changed, 4 insertions(+), 4 deletions(-)
include/linux/slab.h +4 -4
index a5a5e4108ae5..8453c81c75c3 100644
@@ -1105,7 +1105,7 @@ static inline __alloc_size(1, 2) void *kmalloc_array_noprof(size_t n, size_t siz
 	size_t bytes;
 
 	if (unlikely(check_mul_overflow(n, size, &bytes)))
-		return NULL;
+		bytes = SIZE_MAX;
 	return kmalloc_noprof(bytes, flags);
 }
 #define kmalloc_array(...)			alloc_hooks(kmalloc_array_noprof(__VA_ARGS__))
@@ -1135,7 +1135,7 @@ static inline __realloc_size(2, 3) void * __must_check krealloc_array_noprof(voi
 	size_t bytes;
 
 	if (unlikely(check_mul_overflow(new_n, new_size, &bytes)))
-		return NULL;
+		bytes = SIZE_MAX;
 
 	return krealloc_noprof(p, bytes, flags);
 }
@@ -1175,7 +1175,7 @@ static inline __alloc_size(1, 2) void *kmalloc_array_node_noprof(size_t n, size_
 	size_t bytes;
 
 	if (unlikely(check_mul_overflow(n, size, &bytes)))
-		return NULL;
+		bytes = SIZE_MAX;
 	if (__builtin_constant_p(n) && __builtin_constant_p(size))
 		return kmalloc_node_noprof(bytes, flags, node);
 	return __kmalloc_node_noprof(PASS_BUCKET_PARAMS(bytes, NULL), flags, node);
@@ -1223,7 +1223,7 @@ kvmalloc_array_node_noprof(size_t n, size_t size, gfp_t flags, int node)
 	size_t bytes;
 
 	if (unlikely(check_mul_overflow(n, size, &bytes)))
-		return NULL;
+		bytes = SIZE_MAX;
 
 	return kvmalloc_node_align_noprof(bytes, 1, flags, node);
 }
Signature · 1 line
2.34.1
Reviewed-by on v1 1/1
#1 lore raw
Quoted 8 lines · Kees Cook

Instead of silently returning NULL on size overflows from array allocations, saturate the request to SIZE_MAX so the error will be surfaced to the allocator (and still return NULL).

---

Yes, since it's larger than order-1, it'll be passed to the page allocator and will hit the order > MAX_PAGE_ORDER warning in __alloc_frozen_pages_noprof().

Looks good to me,

Signature · 2 lines
Cheers,
Harry / Hyeonggon
#2 lore raw
In reply to Kees Cook

include/linux/slab.h static inline __alloc_size(1, 2) void *kmalloc_array_noprof(size_t n, size_t siz · line 1109

Quoted 8 lines · Kees Cook

+++ b/include/linux/slab.h

include/linux/slab.h +1 -1
@@ -1105,7 +1105,7 @@ static inline __alloc_size(1, 2) void *kmalloc_array_noprof(size_t n, size_t siz
 	size_t bytes;
 
 	if (unlikely(check_mul_overflow(n, size, &bytes)))
-		return NULL;
+		bytes = SIZE_MAX;
 	return kmalloc_noprof(bytes, flags);

Wouldn't this be better written as:

	return kmalloc_noprof(size_mul(n, size), flags);

(etc)

author
#3 lore raw
Quoted 12 lines · Matthew Wilcox

include/linux/slab.h static inline __alloc_size(1, 2) void *kmalloc_array_noprof(size_t n, size_t siz · line 1109

Quoted 8 lines · Kees Cook

+++ b/include/linux/slab.h

include/linux/slab.h +1 -1
@@ -1105,7 +1105,7 @@ static inline __alloc_size(1, 2) void *kmalloc_array_noprof(size_t n, size_t siz
 	size_t bytes;
 
 	if (unlikely(check_mul_overflow(n, size, &bytes)))
-		return NULL;
+		bytes = SIZE_MAX;
 	return kmalloc_noprof(bytes, flags);

Wouldn't this be better written as:

Quoted 3 lines
	return kmalloc_noprof(size_mul(n, size), flags);

(etc)

Sure, I can convert them all that way if that's preferred?

Signature · 1 line
Kees Cook
#4 lore raw
Quoted 13 lines · Kees Cook
Quoted 12 lines · Matthew Wilcox

include/linux/slab.h static inline __alloc_size(1, 2) void *kmalloc_array_noprof(size_t n, size_t siz · line 1109

Quoted 8 lines · Kees Cook

+++ b/include/linux/slab.h

include/linux/slab.h +1 -1
@@ -1105,7 +1105,7 @@ static inline __alloc_size(1, 2) void *kmalloc_array_noprof(size_t n, size_t siz
 	size_t bytes;
 
 	if (unlikely(check_mul_overflow(n, size, &bytes)))
-		return NULL;
+		bytes = SIZE_MAX;
 	return kmalloc_noprof(bytes, flags);

Wouldn't this be better written as:

Quoted 5 lines
Quoted 3 lines
	return kmalloc_noprof(size_mul(n, size), flags);

(etc)

Sure, I can convert them all that way if that's preferred?

Yeah I think it's better than effectively open-code the same thing?