All of lore.kernel.org
 help / color / mirror / Atom feed
From: Richard Purdie <richard.purdie@linuxfoundation.org>
To: Mark Hatle <mark.hatle@kernel.crashing.org>,
	 yocto-patches@lists.yoctoproject.org
Cc: seebs@seebs.net
Subject: Re: [pseudo] [PATCH 1/7] pseudo.h: Avoid accessing unallocated memory
Date: Wed, 01 Jul 2026 17:47:03 +0100	[thread overview]
Message-ID: <340bdc220ec5eff7e0542e825ddde89ce451531f.camel@linuxfoundation.org> (raw)
In-Reply-To: <1a6863b7-b95f-4330-913d-8230827212a9@kernel.crashing.org>

On Wed, 2026-07-01 at 11:40 -0500, Mark Hatle wrote:
> On 7/1/26 8:13 AM, Richard Purdie wrote:
> > We can call STARTSWITH in cases where the item being searched for is longer
> > than the string itself. Switch from memcmp to strncmp to avoid accessing
> > unassigned memory.
> > 
> > Signed-off-by: Richard Purdie <richard.purdie@linuxfoundation.org>
> > ---
> >   pseudo.h | 2 +-
> >   1 file changed, 1 insertion(+), 1 deletion(-)
> > 
> > diff --git a/pseudo.h b/pseudo.h
> > index b6c13f2..1152c19 100644
> > --- a/pseudo.h
> > +++ b/pseudo.h
> > @@ -99,7 +99,7 @@ extern char *pseudo_version;
> >    #define PSEUDO_LIBDIR "lib"
> >   #endif
> >   
> > -#define STARTSWITH(x, y) (!memcmp((x), (y), sizeof(y) - 1))
> > +#define STARTSWITH(x, y) (strncmp(y, x, strlen(y)) == 0)
> 
> I've not looked at the users of STARTSWITH.  If they are all strings this is 
> fine.  I believe memcmp was originally used because it could be items with nulls 
> in it.  The bug may be that 'sizeof' was not calculated properly (specifically 
> checking both X and Y, only y.. which can lead that the issue.)

It is only used by pseudo_util.c against envp:

pseudo_util.c:		if (STARTSWITH(envp[i], PRELINK_LIBRARIES "=")) {
pseudo_util.c:		if (STARTSWITH(envp[i], PRELINK_LIBRARIES "=")) {
pseudo_util.c:		if (STARTSWITH(envp[i], PRELINK_PATH "=")) {
pseudo_util.c:		if (STARTSWITH(envp[i], PRELINK_LIBRARIES "=")) continue;
pseudo_util.c:		if (STARTSWITH(envp[i], PRELINK_PATH "=")) continue;

i.e. always on strings.

The bug is that y can be longer than x and the memcmp check doesn't
check for that. I could have put more logic in there but we may as well
just use strncmp.

Cheers,

Richard


  reply	other threads:[~2026-07-01 16:47 UTC|newest]

Thread overview: 12+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2026-07-01 13:13 [pseudo] [PATCH 1/7] pseudo.h: Avoid accessing unallocated memory Richard Purdie
2026-07-01 13:13 ` [pseudo] [PATCH 2/7] pseudo_util: Avoid accidental free calls for without_libpseudo() Richard Purdie
2026-07-01 13:13 ` [pseudo] [PATCH 3/7] pseudo_util: Ensure pseudo_setupenvp handles memory consistently Richard Purdie
2026-07-01 13:13 ` [pseudo] [PATCH 4/7] pseudo_util: Avoid a memory leak in pseudo_dropenv() Richard Purdie
2026-07-01 13:13 ` [pseudo] [PATCH 5/7] pseudo_util: Clean up memory handling for setupenvp results Richard Purdie
2026-07-01 13:13 ` [pseudo] [PATCH 6/7] exec*: Replace bash workaround to avoid memory corruption Richard Purdie
2026-07-02  5:54   ` [yocto-patches] " Gaël PORTAY
2026-07-02  6:23     ` Richard Purdie
2026-07-01 13:13 ` [pseudo] [PATCH 7/7] pseudo_util: Correctly free memory allocated by pseudo_setupenvp Richard Purdie
2026-07-01 16:40 ` [pseudo] [PATCH 1/7] pseudo.h: Avoid accessing unallocated memory Mark Hatle
2026-07-01 16:47   ` Richard Purdie [this message]
2026-07-01 18:59     ` [yocto-patches] " Mark Hatle

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=340bdc220ec5eff7e0542e825ddde89ce451531f.camel@linuxfoundation.org \
    --to=richard.purdie@linuxfoundation.org \
    --cc=mark.hatle@kernel.crashing.org \
    --cc=seebs@seebs.net \
    --cc=yocto-patches@lists.yoctoproject.org \
    /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 an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.