devicetree-compiler.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
From: David Gibson <david@gibson.dropbear.id.au>
To: Mike McTernan <mikemcternan@google.com>
Cc: "Pierre-Clément Tosi" <ptosi@google.com>,
	devicetree-compiler@vger.kernel.org,
	"Simon Glass" <sjg@chromium.org>
Subject: Re: [PATCH] libfdt: fdt_get_alias_namelen: Validate aliases
Date: Wed, 11 Oct 2023 11:36:16 +1100	[thread overview]
Message-ID: <ZSXuAC/oRhHAKPDl@zatzit> (raw)
In-Reply-To: <CA+QomtZPEAOC0AFgoDx93AoKe-mUgBW4GM3CyxVaEpp1p1pjfQ@mail.gmail.com>

[-- Attachment #1: Type: text/plain, Size: 3842 bytes --]

On Tue, Oct 10, 2023 at 03:18:04PM +0100, Mike McTernan wrote:
> On Sat, 7 Oct 2023 at 12:07, Pierre-Clément Tosi <ptosi@google.com> wrote:
> >
> > Ensure that the alias found is valid i.e.
> >
> >     An alias value is a device path and is encoded as a string.
> >     The value represents the full path to a node, ...
> >
> > This protects against a stack overflow (fdt_path_offset_namelen() calls
> > fdt_get_alias_namelen() then fdt_path_offset(alias), ...) when /aliases
> > has an empty property with an empty name.
> >
> > Co-developed-by: Mike McTernan <mikemcternan@google.com>
> > Signed-off-by: Pierre-Clément Tosi <ptosi@google.com>
> 
> Acked-by: Mike McTernan <mikemcternan@google.com>

Thanks, I've added this to the patch on merge.

> 
> > ---
> >  libfdt/fdt_ro.c   | 11 ++++++++++-
> >  tests/aliases.dts |  3 +++
> >  tests/get_alias.c | 12 +++++++++++-
> >  3 files changed, 24 insertions(+), 2 deletions(-)
> >
> > diff --git a/libfdt/fdt_ro.c b/libfdt/fdt_ro.c
> > index c4c520c..bda5c0d 100644
> > --- a/libfdt/fdt_ro.c
> > +++ b/libfdt/fdt_ro.c
> > @@ -537,7 +537,16 @@ static const void *fdt_path_getprop_namelen(const void *fdt, const char *path,
> >  const char *fdt_get_alias_namelen(const void *fdt,
> >                                   const char *name, int namelen)
> >  {
> > -       return fdt_path_getprop_namelen(fdt, "/aliases", name, namelen, NULL);
> > +       int len;
> > +       const char *alias;
> > +
> > +       alias = fdt_path_getprop_namelen(fdt, "/aliases", name, namelen, &len);
> > +
> > +       if (!can_assume(VALID_DTB) &&
> > +           !(len > 0 && alias && memchr(alias, '\0', len) && *alias == '/'))
> > +               return NULL;
> > +
> > +       return alias;
> >  }
> >
> >  const char *fdt_get_alias(const void *fdt, const char *name)
> > diff --git a/tests/aliases.dts b/tests/aliases.dts
> > index 853479a..8820974 100644
> > --- a/tests/aliases.dts
> > +++ b/tests/aliases.dts
> > @@ -8,6 +8,9 @@
> >                 s1 = &sub1;
> >                 ss1 = &subsub1;
> >                 sss1 = &subsubsub1;
> > +               badpath = "wrong";
> > +               badpathlong = "wrong/with/parts";
> > +               empty = "";
> >         };
> >
> >         sub1: subnode@1 {
> > diff --git a/tests/get_alias.c b/tests/get_alias.c
> > index fb2c38c..4f3f6fd 100644
> > --- a/tests/get_alias.c
> > +++ b/tests/get_alias.c
> > @@ -21,9 +21,16 @@ static void check_alias(void *fdt, const char *path, const char *alias)
> >
> >         aliaspath = fdt_get_alias(fdt, alias);
> >
> > -       if (path && !aliaspath)
> > +       if (!path && !aliaspath)
> > +               return;
> > +
> > +       if (!aliaspath)
> >                 FAIL("fdt_get_alias(%s) failed\n", alias);
> >
> > +       if (!path)
> > +               FAIL("fdt_get_alias(%s) returned %s instead of NULL",
> > +                    alias, aliaspath);
> > +
> >         if (strcmp(aliaspath, path) != 0)
> >                 FAIL("fdt_get_alias(%s) returned %s instead of %s\n",
> >                      alias, aliaspath, path);
> > @@ -36,6 +43,9 @@ int main(int argc, char *argv[])
> >         test_init(argc, argv);
> >         fdt = load_blob_arg(argc, argv);
> >
> > +       check_alias(fdt, NULL, "badpath");
> > +       check_alias(fdt, NULL, "badpathlong");
> > +       check_alias(fdt, NULL, "empty");
> >         check_alias(fdt, "/subnode@1", "s1");
> >         check_alias(fdt, "/subnode@1/subsubnode", "ss1");
> >         check_alias(fdt, "/subnode@1/subsubnode/subsubsubnode", "sss1");
> >
> >
> 

-- 
David Gibson			| I'll have my music baroque, and my code
david AT gibson.dropbear.id.au	| minimalist, thank you.  NOT _the_ _other_
				| _way_ _around_!
http://www.ozlabs.org/~dgibson

[-- Attachment #2: signature.asc --]
[-- Type: application/pgp-signature, Size: 833 bytes --]

      reply	other threads:[~2023-10-11  0:38 UTC|newest]

Thread overview: 6+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2023-10-07 11:07 [PATCH] libfdt: fdt_get_alias_namelen: Validate aliases Pierre-Clément Tosi
2023-10-08  7:13 ` David Gibson
2023-10-09 14:09   ` Pierre-Clément Tosi
2023-10-10  4:04     ` David Gibson
2023-10-10 14:18 ` Mike McTernan
2023-10-11  0:36   ` David Gibson [this message]

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=ZSXuAC/oRhHAKPDl@zatzit \
    --to=david@gibson.dropbear.id.au \
    --cc=devicetree-compiler@vger.kernel.org \
    --cc=mikemcternan@google.com \
    --cc=ptosi@google.com \
    --cc=sjg@chromium.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 a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).