[<prev] [next>] [<thread-prev] [thread-next>] [day] [month] [year] [list]
Message-ID: <202601080939.B99FEFF780@keescook>
Date: Thu, 8 Jan 2026 09:40:59 -0800
From: Kees Cook <kees@...nel.org>
To: Vlastimil Babka <vbabka@...e.cz>
Cc: Jonathan Corbet <corbet@....net>,
Andrew Morton <akpm@...ux-foundation.org>,
Christoph Lameter <cl@...two.org>,
David Rientjes <rientjes@...gle.com>,
Roman Gushchin <roman.gushchin@...ux.dev>,
Harry Yoo <harry.yoo@...cle.com>,
"Gustavo A. R. Silva" <gustavoars@...nel.org>,
workflows@...r.kernel.org, linux-doc@...r.kernel.org,
linux-mm@...ck.org, linux-hardening@...r.kernel.org,
Linus Torvalds <torvalds@...ux-foundation.org>,
Randy Dunlap <rdunlap@...radead.org>,
Miguel Ojeda <ojeda@...nel.org>,
Przemek Kitszel <przemyslaw.kitszel@...el.com>,
Matthew Wilcox <willy@...radead.org>,
John Hubbard <jhubbard@...dia.com>, Joe Perches <joe@...ches.com>,
Christoph Lameter <cl@...ux.com>, Marco Elver <elver@...gle.com>,
Vegard Nossum <vegard.nossum@...cle.com>,
Pekka Enberg <penberg@...nel.org>,
Joonsoo Kim <iamjoonsoo.kim@....com>,
Bill Wendling <morbo@...gle.com>,
Justin Stitt <justinstitt@...gle.com>, Jann Horn <jannh@...gle.com>,
Greg Kroah-Hartman <gregkh@...uxfoundation.org>,
Sasha Levin <sashal@...nel.org>,
Nathan Chancellor <nathan@...nel.org>,
Peter Zijlstra <peterz@...radead.org>,
Nick Desaulniers <nick.desaulniers+lkml@...il.com>,
Jakub Kicinski <kuba@...nel.org>,
Yafang Shao <laoar.shao@...il.com>,
Tony Ambardar <tony.ambardar@...il.com>,
Alexander Lobakin <aleksander.lobakin@...el.com>,
Jan Hendrik Farr <kernel@...rr.cc>,
Alexander Potapenko <glider@...gle.com>,
linux-kernel@...r.kernel.org, llvm@...ts.linux.dev
Subject: Re: [PATCH v6 4/5] slab: Introduce kmalloc_flex() and family
On Thu, Jan 08, 2026 at 03:06:31PM +0100, Vlastimil Babka wrote:
> On 12/4/25 00:30, Kees Cook wrote:
> > As done for kmalloc_obj*(), introduce a type-aware allocator for flexible
> > arrays, which may also have "counted_by" annotations:
> >
> > ptr = kmalloc(struct_size(ptr, flex_member, count), gfp);
> >
> > becomes:
> >
> > ptr = kmalloc_flex(*ptr, flex_member, count, gfp);
> >
> > The internal use of __flex_counter() allows for automatically setting
> > the counter member of a struct's flexible array member when it has
> > been annotated with __counted_by(), avoiding any missed early size
> > initializations while __counted_by() annotations are added to the
> > kernel. Additionally, this also checks for "too large" allocations based
> > on the type size of the counter variable. For example:
> >
> > if (count > type_max(ptr->flex_counter))
> > fail...;
> > size = struct_size(ptr, flex_member, count);
> > ptr = kmalloc(size, gfp);
> > ptr->flex_counter = count;
> >
> > becomes (n.b. unchanged from earlier example):
> >
> > ptr = kmalloc_flex(*ptr, flex_member, count, gfp);
> > ptr->flex_count = count;
>
> ^ flex_counter ?
>
> But if it was "too large", ptr is NULL so this will oops?
I've changed this to:
...
based on the type size of the counter variable. For example:
if (count > type_max(ptr->flex_counter))
fail...;
size = struct_size(ptr, flex_member, count);
ptr = kmalloc(size, gfp);
if (!ptr)
fail...;
ptr->flex_counter = count;
becomes (n.b. unchanged from earlier example):
ptr = kmalloc_flex(*ptr, flex_member, count, gfp);
if (!ptr)
fail...;
ptr->flex_counter = count;
May I add your Acked-by for this 4/5 patch?
--
Kees Cook
Powered by blists - more mailing lists