[<prev] [next>] [<thread-prev] [thread-next>] [day] [month] [year] [list]
Message-ID: <fc080824-37ff-4b69-ad4a-e76b458218d6@intel.com>
Date: Tue, 9 Jul 2024 08:57:55 +0200
From: Przemek Kitszel <przemyslaw.kitszel@...el.com>
To: Kees Cook <kees@...nel.org>, Vlastimil Babka <vbabka@...e.cz>
CC: Christoph Lameter <cl@...ux.com>, Pekka Enberg <penberg@...nel.org>, David
Rientjes <rientjes@...gle.com>, Joonsoo Kim <iamjoonsoo.kim@....com>, Andrew
Morton <akpm@...ux-foundation.org>, Roman Gushchin
<roman.gushchin@...ux.dev>, Hyeonggon Yoo <42.hyeyoo@...il.com>,
<linux-mm@...ck.org>, Jann Horn <jannh@...gle.com>, Tony Luck
<tony.luck@...el.com>, Nick Desaulniers <ndesaulniers@...gle.com>, "Miguel
Ojeda" <ojeda@...nel.org>, Marco Elver <elver@...gle.com>, Nathan Chancellor
<nathan@...nel.org>, Hao Luo <haoluo@...gle.com>, "Guilherme G. Piccoli"
<gpiccoli@...lia.com>, Mark Rutland <mark.rutland@....com>, Jakub Kicinski
<kuba@...nel.org>, Petr Pavlu <petr.pavlu@...e.com>, Alexander Lobakin
<aleksander.lobakin@...el.com>, Tony Ambardar <tony.ambardar@...il.com>,
<linux-kernel@...r.kernel.org>, <linux-hardening@...r.kernel.org>
Subject: Re: [RFC][PATCH 2/4] slab: Detect negative size values and saturate
On 7/8/24 21:18, Kees Cook wrote:
> The allocator will already reject giant sizes seen from negative size
> arguments, so this commit mainly services as an example for initial
> type-based filtering. The size argument is checked for negative values
> in signed arguments, saturating any if found instead of passing them on.
>
> For example, now the size is checked:
>
> Before:
> /* %rdi unchecked */
> 1eb: be c0 0c 00 00 mov $0xcc0,%esi
> 1f0: e8 00 00 00 00 call 1f5 <do_SLAB_NEGATIVE+0x15>
> 1f1: R_X86_64_PLT32 __kmalloc_noprof-0x4
>
> After:
> 6d0: 48 63 c7 movslq %edi,%rax
> 6d3: 85 ff test %edi,%edi
> 6d5: be c0 0c 00 00 mov $0xcc0,%esi
> 6da: 48 c7 c2 ff ff ff ff mov $0xffffffffffffffff,%rdx
> 6e1: 48 0f 49 d0 cmovns %rax,%rdx
> 6e5: 48 89 d7 mov %rdx,%rdi
> 6e8: e8 00 00 00 00 call 6ed <do_SLAB_NEGATIVE+0x1d>
> 6e9: R_X86_64_PLT32 __kmalloc_noprof-0x4
>
> Signed-off-by: Kees Cook <kees@...nel.org>
> ---
> Cc: Christoph Lameter <cl@...ux.com>
> Cc: Pekka Enberg <penberg@...nel.org>
> Cc: David Rientjes <rientjes@...gle.com>
> Cc: Joonsoo Kim <iamjoonsoo.kim@....com>
> Cc: Andrew Morton <akpm@...ux-foundation.org>
> Cc: Vlastimil Babka <vbabka@...e.cz>
> Cc: Roman Gushchin <roman.gushchin@...ux.dev>
> Cc: Hyeonggon Yoo <42.hyeyoo@...il.com>
> Cc: linux-mm@...ck.org
> ---
> include/linux/slab.h | 19 ++++++++++++++++++-
> 1 file changed, 18 insertions(+), 1 deletion(-)
>
> diff --git a/include/linux/slab.h b/include/linux/slab.h
> index d99afce36098..7353756cbec6 100644
> --- a/include/linux/slab.h
> +++ b/include/linux/slab.h
> @@ -684,7 +684,24 @@ static __always_inline __alloc_size(1) void *kmalloc_noprof(size_t size, gfp_t f
> }
> return __kmalloc_noprof(size, flags);
> }
> -#define kmalloc(...) alloc_hooks(kmalloc_noprof(__VA_ARGS__))
> +#define kmalloc_sized(...) alloc_hooks(kmalloc_noprof(__VA_ARGS__))
> +
> +#define __size_force_positive(x) \
> + ({ \
> + typeof(__force_integral_expr(x)) __forced_val = \
> + __force_integral_expr(x); \
> + __forced_val < 0 ? SIZE_MAX : __forced_val; \
> + })
> +
> +#define kmalloc(p, gfp) _Generic((p), \
> + unsigned char: kmalloc_sized(__force_integral_expr(p), gfp), \
> + unsigned short: kmalloc_sized(__force_integral_expr(p), gfp), \
> + unsigned int: kmalloc_sized(__force_integral_expr(p), gfp), \
> + unsigned long: kmalloc_sized(__force_integral_expr(p), gfp), \
> + signed char: kmalloc_sized(__size_force_positive(p), gfp), \
> + signed short: kmalloc_sized(__size_force_positive(p), gfp), \
> + signed int: kmalloc_sized(__size_force_positive(p), gfp), \
> + signed long: kmalloc_sized(__size_force_positive(p), gfp))
I like this idea and series very much, thank you!
What about bool?
What about long long?
(by this commit one will get a rather easy to parse compile error, but
the next one will obscure it a bit)
Consider the following correct (albeit somewhat weird) code:
/* header */
char *state;
/* .c impl, init part */
bool needs_state = some_expr();
state = kmalloc(needs_state, GFP_KERNEL);
/* .c, other part */
if (ZERO_OR_NULL_PTR(state))
return _EARLY;
*state = state_machine_action(*state);
>
> #define kmem_buckets_alloc(_b, _size, _flags) \
> alloc_hooks(__kmalloc_node_noprof(PASS_BUCKET_PARAMS(_size, _b), _flags, NUMA_NO_NODE))
Powered by blists - more mailing lists