From: Kees Cook <kees@kernel.org>
To: Thorsten Blum <thorsten.blum@linux.dev>
Cc: Andy Shevchenko <andriy.shevchenko@intel.com>,
Andrew Morton <akpm@linux-foundation.org>,
Andy Shevchenko <andy@kernel.org>,
linux-hardening@vger.kernel.org, linux-kernel@vger.kernel.org
Subject: Re: [PATCH 1/2] lib/string_helpers: drop redundant allocation in kasprintf_strarray
Date: Wed, 13 May 2026 10:53:39 -0700 [thread overview]
Message-ID: <202605131047.BD31D2130@keescook> (raw)
In-Reply-To: <afNps6X5Hii2jcSQ@linux.dev>
On Thu, Apr 30, 2026 at 04:39:47PM +0200, Thorsten Blum wrote:
> Hi Kees and Andrew,
>
> On Thu, Apr 16, 2026 at 10:48:51AM +0300, Andy Shevchenko wrote:
> > On Wed, Apr 15, 2026 at 05:30:50PM +0200, Thorsten Blum wrote:
> > > On Wed, Apr 15, 2026 at 05:42:41PM +0300, Andy Shevchenko wrote:
> > > > On Wed, Apr 15, 2026 at 02:25:43PM +0200, Thorsten Blum wrote:
> > > > > kasprintf_strarray() returns an array of N strings and kfree_strarray()
> > > > > also frees N entries. However, kasprintf_strarray() currently allocates
> > > > > N+1 char pointers. Allocate exactly N pointers instead of N+1.
> > > > >
> > > > > Also update the kernel-doc for @n.
> > > >
> > > > Have you checked all current users that they do not rely on the NULL terminated
> > > > array?
> > >
> > > Yes, I've checked all call sites, and none of them rely on the NULL
> > > terminator. Specifically, I checked:
> > >
> > > drivers/gpio/gpio-mockup.c
> > >
> > > which uses PROPERTY_ENTRY_STRING_ARRAY_LEN(), and
> > >
> > > drivers/pinctrl/bcm/pinctrl-bcm4908.c
> > > drivers/pinctrl/intel/pinctrl-intel-platform.c
> > > drivers/pinctrl/meson/pinctrl-amlogic-a4.c
> > > drivers/pinctrl/mvebu/pinctrl-armada-37xx.c
> > > drivers/pinctrl/pinctrl-at91.c
> > > drivers/pinctrl/pinctrl-rockchip.c
> > > drivers/pinctrl/pinctrl-st.c
> > >
> > > all of which use the size N to iterate over the returned array.
> >
> > Thanks for confirming.
> >
> > > Also, kfree_strarray() explicitly takes the number of entries N,
> > > indicating that callers are expected to keep track of it.
> >
> > Still we might have an API that requires a NULL terminated arrays (when it
> > doesn't take size), which a caller wants to use.
What I find problematic here is that we allocate N+1 and free N. And
this is repeated in wrappers too, like devm_kasprintf_strarray(), which
explicitly tracks N for the later devm free. How has kmemleak missed
this?
> > > > Note, that was done on purpose that once allocated it can allow user
> > > > to drop the track of the number of strings and rely on NULL terminator.
> > > > I.o.w. the number of strings may be just a local variable somewhere
> > > > where kasprintf_strarray() is called.
> > > >
> > > > I tend to NAK this change, rather you can update kernel-doc to explain
> > > > why it's done this way (see above).
> >
> > Given pros and cons, and what David said I'm still not sure that this is
> > going to be a beneficial patch. I leave it Kees and Andrew to decide.
>
> What's your take on this and the __counted_by_ptr() annotation from
> patch 2/2?
I think both patches look good.
--
Kees Cook
next prev parent reply other threads:[~2026-05-13 17:53 UTC|newest]
Thread overview: 9+ messages / expand[flat|nested] mbox.gz Atom feed top
2026-04-15 12:25 Thorsten Blum
2026-04-15 12:25 ` [PATCH 2/2] lib/string_helpers: annotate struct strarray with __counted_by_ptr Thorsten Blum
2026-04-15 14:42 ` [PATCH 1/2] lib/string_helpers: drop redundant allocation in kasprintf_strarray Andy Shevchenko
2026-04-15 15:30 ` Thorsten Blum
2026-04-16 7:48 ` Andy Shevchenko
2026-04-30 14:39 ` Thorsten Blum
2026-05-13 17:53 ` Kees Cook [this message]
2026-04-15 18:38 ` David Laight
2026-05-13 17:54 ` Kees Cook
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=202605131047.BD31D2130@keescook \
--to=kees@kernel.org \
--cc=akpm@linux-foundation.org \
--cc=andriy.shevchenko@intel.com \
--cc=andy@kernel.org \
--cc=linux-hardening@vger.kernel.org \
--cc=linux-kernel@vger.kernel.org \
--cc=thorsten.blum@linux.dev \
/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
Powered by JetHome