From: Kees Cook <kees@kernel.org>
To: Przemek Kitszel <przemyslaw.kitszel@intel.com>
Cc: Vlastimil Babka <vbabka@suse.cz>,
Christoph Lameter <cl@linux.com>,
Pekka Enberg <penberg@kernel.org>,
David Rientjes <rientjes@google.com>,
Joonsoo Kim <iamjoonsoo.kim@lge.com>,
Andrew Morton <akpm@linux-foundation.org>,
Roman Gushchin <roman.gushchin@linux.dev>,
Hyeonggon Yoo <42.hyeyoo@gmail.com>,
linux-mm@kvack.org, Jann Horn <jannh@google.com>,
Tony Luck <tony.luck@intel.com>,
Nick Desaulniers <ndesaulniers@google.com>,
Miguel Ojeda <ojeda@kernel.org>, Marco Elver <elver@google.com>,
Nathan Chancellor <nathan@kernel.org>,
Hao Luo <haoluo@google.com>,
"Guilherme G. Piccoli" <gpiccoli@igalia.com>,
Mark Rutland <mark.rutland@arm.com>,
Jakub Kicinski <kuba@kernel.org>,
Petr Pavlu <petr.pavlu@suse.com>,
Alexander Lobakin <aleksander.lobakin@intel.com>,
Tony Ambardar <tony.ambardar@gmail.com>,
linux-kernel@vger.kernel.org, linux-hardening@vger.kernel.org
Subject: Re: [RFC][PATCH 2/4] slab: Detect negative size values and saturate
Date: Tue, 9 Jul 2024 09:09:57 -0700 [thread overview]
Message-ID: <202407090903.38C2F463@keescook> (raw)
In-Reply-To: <fc080824-37ff-4b69-ad4a-e76b458218d6@intel.com>
On Tue, Jul 09, 2024 at 08:57:55AM +0200, Przemek Kitszel wrote:
> 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@kernel.org>
> > ---
> > Cc: Christoph Lameter <cl@linux.com>
> > Cc: Pekka Enberg <penberg@kernel.org>
> > Cc: David Rientjes <rientjes@google.com>
> > Cc: Joonsoo Kim <iamjoonsoo.kim@lge.com>
> > Cc: Andrew Morton <akpm@linux-foundation.org>
> > Cc: Vlastimil Babka <vbabka@suse.cz>
> > Cc: Roman Gushchin <roman.gushchin@linux.dev>
> > Cc: Hyeonggon Yoo <42.hyeyoo@gmail.com>
> > Cc: linux-mm@kvack.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!
Thanks!
> What about bool?
> What about long long?
Ah yes, I will add these. LKP also found a weird one (a bitfield!) that
I'm fixing at the source:
https://lore.kernel.org/lkml/20240709154953.work.953-kees@kernel.org/
--
Kees Cook
next prev parent reply other threads:[~2024-07-09 16:10 UTC|newest]
Thread overview: 16+ messages / expand[flat|nested] mbox.gz Atom feed top
2024-07-08 19:18 [RFC][PATCH 0/4] slab: Allow for type introspection during allocation Kees Cook
2024-07-08 19:18 ` [RFC][PATCH 1/4] compiler_types: Add integral/pointer type helper macros Kees Cook
2024-07-08 19:18 ` [RFC][PATCH 2/4] slab: Detect negative size values and saturate Kees Cook
2024-07-09 6:57 ` Przemek Kitszel
2024-07-09 16:09 ` Kees Cook [this message]
2024-07-08 19:18 ` [RFC][PATCH 3/4] slab: Allow for type introspection during allocation Kees Cook
2024-07-08 19:18 ` [RFC][PATCH 4/4] pstore: Replace classic kmalloc code pattern with typed argument Kees Cook
2024-07-09 7:06 ` Przemek Kitszel
2024-07-09 16:32 ` Kees Cook
2024-07-09 16:57 ` [RFC][PATCH 0/4] slab: Allow for type introspection during allocation Roman Gushchin
2024-07-09 18:57 ` Kees Cook
2024-07-09 17:26 ` Christoph Lameter (Ampere)
2024-07-09 20:28 ` Kees Cook
2024-07-09 21:02 ` Marco Elver
2024-07-09 23:28 ` Kees Cook
2024-07-10 4:42 ` Przemek Kitszel
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=202407090903.38C2F463@keescook \
--to=kees@kernel.org \
--cc=42.hyeyoo@gmail.com \
--cc=akpm@linux-foundation.org \
--cc=aleksander.lobakin@intel.com \
--cc=cl@linux.com \
--cc=elver@google.com \
--cc=gpiccoli@igalia.com \
--cc=haoluo@google.com \
--cc=iamjoonsoo.kim@lge.com \
--cc=jannh@google.com \
--cc=kuba@kernel.org \
--cc=linux-hardening@vger.kernel.org \
--cc=linux-kernel@vger.kernel.org \
--cc=linux-mm@kvack.org \
--cc=mark.rutland@arm.com \
--cc=nathan@kernel.org \
--cc=ndesaulniers@google.com \
--cc=ojeda@kernel.org \
--cc=penberg@kernel.org \
--cc=petr.pavlu@suse.com \
--cc=przemyslaw.kitszel@intel.com \
--cc=rientjes@google.com \
--cc=roman.gushchin@linux.dev \
--cc=tony.ambardar@gmail.com \
--cc=tony.luck@intel.com \
--cc=vbabka@suse.cz \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox