Skip to contentSkip to conversation

mempool: Use kmalloc_size_roundup() to match ksize() usage

PATCH In review Kees Cook 3 participants 5 messages Oct 18, 2022 – Oct 25, 2022

Subsystems (2)
lore
Share & tools
lore

Kees Cook

author
#0 lore raw

PATCH 1/1 mempool: Use kmalloc_size_roundup() to match ksize() usage

Round up allocations with kmalloc_size_roundup() so that mempool's use of ksize() is always accurate and no special handling of the memory is needed by KASAN, UBSAN_BOUNDS, nor FORTIFY_SOURCE.

---
 mm/mempool.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
mm/mempool.c +1 -1
index 96488b13a1ef..0f3107b28e6b 100644
@@ -526,7 +526,7 @@ EXPORT_SYMBOL(mempool_free_slab);
  */
 void *mempool_kmalloc(gfp_t gfp_mask, void *pool_data)
 {
-	size_t size = (size_t)pool_data;
+	size_t size = kmalloc_size_roundup((size_t)pool_data);
 	return kmalloc(size, gfp_mask);
 }
 EXPORT_SYMBOL(mempool_kmalloc);
Signature · 1 line
2.34.1
maintainer
#1 lore raw
Quoted 3 lines · On Tue, 18 Oct 2022 02:03:29 -0700 Kees Cook <[email protected]> wrote:

Round up allocations with kmalloc_size_roundup() so that mempool's use of ksize() is always accurate and no special handling of the memory is needed by KASAN, UBSAN_BOUNDS, nor FORTIFY_SOURCE.

Confused. If the special handling is not needed, why doesn't the patch removed the no longer needed special handling?

Quoted 16 lines
mm/mempool.c +1 -1
index 96488b13a1ef..0f3107b28e6b 100644
@@ -526,7 +526,7 @@ EXPORT_SYMBOL(mempool_free_slab);
  */
 void *mempool_kmalloc(gfp_t gfp_mask, void *pool_data)
 {
-	size_t size = (size_t)pool_data;
+	size_t size = kmalloc_size_roundup((size_t)pool_data);
 	return kmalloc(size, gfp_mask);
 }
 EXPORT_SYMBOL(mempool_kmalloc);
Signature · 1 line
2.34.1
author
#2 lore raw
Quoted 8 lines · Andrew Morton
Quoted 3 lines · On Tue, 18 Oct 2022 02:03:29 -0700 Kees Cook <[email protected]> wrote:

Round up allocations with kmalloc_size_roundup() so that mempool's use of ksize() is always accurate and no special handling of the memory is needed by KASAN, UBSAN_BOUNDS, nor FORTIFY_SOURCE.

Confused. If the special handling is not needed, why doesn't the patch removed the no longer needed special handling?

The special handling is in the ksize() implementation, so it can't be removed[1] until all the ksize()-affected users are updated to see their true allocation sizes first.

[1] https://lore.kernel.org/lkml/[email protected]/

Signature · 1 line
Kees Cook
#3 lore raw
Quoted 7 lines · Kees Cook
Quoted 6 lines · Andrew Morton
Quoted 3 lines · On Tue, 18 Oct 2022 02:03:29 -0700 Kees Cook <[email protected]> wrote:

Round up allocations with kmalloc_size_roundup() so that mempool's use of ksize() is always accurate and no special handling of the memory is needed by KASAN, UBSAN_BOUNDS, nor FORTIFY_SOURCE.

Quoted 8 lines
Quoted 2 lines

Confused. If the special handling is not needed, why doesn't the patch removed the no longer needed special handling?

The special handling is in the ksize() implementation, so it can't be removed[1] until all the ksize()-affected users are updated to see their true allocation sizes first.

[1] https://lore.kernel.org/lkml/[email protected]/

But in the previous version I was wondering if we can just stop doing ksize()-like poison handling in mempool completely, if no mempool consumers call ksize() to expand their use of the allocated objects. You seemed to agree but this version is uncahnged?

https://lore.kernel.org/all/[email protected]/

author
#4 lore raw
Quoted 15 lines · Vlastimil Babka (SUSE)
Quoted 14 lines · Kees Cook
Quoted 8 lines · Andrew Morton
Quoted 3 lines · On Tue, 18 Oct 2022 02:03:29 -0700 Kees Cook <[email protected]> wrote:

Round up allocations with kmalloc_size_roundup() so that mempool's use of ksize() is always accurate and no special handling of the memory is needed by KASAN, UBSAN_BOUNDS, nor FORTIFY_SOURCE.

Confused. If the special handling is not needed, why doesn't the patch removed the no longer needed special handling?

The special handling is in the ksize() implementation, so it can't be removed[1] until all the ksize()-affected users are updated to see their true allocation sizes first.

Quoted 8 lines
Quoted 1 line

[1] https://lore.kernel.org/lkml/[email protected]/

But in the previous version I was wondering if we can just stop doing ksize()-like poison handling in mempool completely, if no mempool consumers call ksize() to expand their use of the allocated objects. You seemed to agree but this version is uncahnged?

https://lore.kernel.org/all/[email protected]/

Oops, yes. This failed to get on my TODO list. New version coming!

Signature · 1 line
Kees Cook