2022-10-18 09:51:03

by Kees Cook

[permalink] [raw]
Subject: [PATCH] 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.

Cc: Andrew Morton <[email protected]>
Cc: [email protected]
Signed-off-by: Kees Cook <[email protected]>
---
mm/mempool.c | 2 +-
1 file changed, 1 insertion(+), 1 deletion(-)

diff --git a/mm/mempool.c b/mm/mempool.c
index 96488b13a1ef..0f3107b28e6b 100644
--- a/mm/mempool.c
+++ b/mm/mempool.c
@@ -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);
--
2.34.1


2022-10-18 23:05:51

by Andrew Morton

[permalink] [raw]
Subject: Re: [PATCH] mempool: Use kmalloc_size_roundup() to match ksize() usage

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?

>
> diff --git a/mm/mempool.c b/mm/mempool.c
> index 96488b13a1ef..0f3107b28e6b 100644
> --- a/mm/mempool.c
> +++ b/mm/mempool.c
> @@ -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);
> --
> 2.34.1

2022-10-19 06:00:01

by Kees Cook

[permalink] [raw]
Subject: Re: [PATCH] mempool: Use kmalloc_size_roundup() to match ksize() usage

On Tue, Oct 18, 2022 at 03:51:37PM -0700, Andrew Morton wrote:
> 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]/

--
Kees Cook

2022-10-24 19:44:46

by Vlastimil Babka (SUSE)

[permalink] [raw]
Subject: Re: [PATCH] mempool: Use kmalloc_size_roundup() to match ksize() usage

On 10/19/22 07:37, Kees Cook wrote:
> On Tue, Oct 18, 2022 at 03:51:37PM -0700, Andrew Morton wrote:
>> 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]/

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]/


2022-10-25 23:30:49

by Kees Cook

[permalink] [raw]
Subject: Re: [PATCH] mempool: Use kmalloc_size_roundup() to match ksize() usage

On Mon, Oct 24, 2022 at 08:03:34PM +0200, Vlastimil Babka (SUSE) wrote:
> On 10/19/22 07:37, Kees Cook wrote:
> > On Tue, Oct 18, 2022 at 03:51:37PM -0700, Andrew Morton wrote:
> >> 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]/
>
> 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!

--
Kees Cook