devicetree-compiler.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
From: Rob Herring <robh-DgEjT+Ai2ygdnm+yROfE0A@public.gmane.org>
To: Tadeusz Struk <tadeusz.struk-QSEj5FYQhm4dnm+yROfE0A@public.gmane.org>
Cc: David Gibson
	<david-xT8FGy+AXnRB3Ne2BGzF6laj5H9X9Tb+@public.gmane.org>,
	devicetree-compiler-u79uwXL29TY76Z2rM5mHXA@public.gmane.org
Subject: Re: [PATCH 1/2] libfdt: prevent integer overflow in fdt_next_tag
Date: Fri, 30 Sep 2022 08:18:30 -0500	[thread overview]
Message-ID: <CAL_JsqJ7E6D=jxmHXnh5cR_UyitpYVSPEQASsWu+eDyQ4J-Eig@mail.gmail.com> (raw)
In-Reply-To: <20220929235536.618370-1-tadeusz.struk-QSEj5FYQhm4dnm+yROfE0A@public.gmane.org>

On Thu, Sep 29, 2022 at 6:55 PM Tadeusz Struk <tadeusz.struk-QSEj5FYQhm4dnm+yROfE0A@public.gmane.org> wrote:
>
> Since fdt_next_tag() in a public API function all input parameters,
> including the fdt blob should not be trusted. It is possible to forge
> a blob with invalid property length that will cause integer overflow
> during offset calculation. To prevent that validate the property length

comma: ...that, validate...

> read from the blob before doing calculations.
>
> Signed-off-by: Tadeusz Struk <tadeusz.struk-QSEj5FYQhm4dnm+yROfE0A@public.gmane.org>
> ---
>  libfdt/fdt.c | 8 +++++++-
>  1 file changed, 7 insertions(+), 1 deletion(-)
>
> diff --git a/libfdt/fdt.c b/libfdt/fdt.c
> index 90a39e8..c3e112a 100644
> --- a/libfdt/fdt.c
> +++ b/libfdt/fdt.c
> @@ -186,11 +186,17 @@ uint32_t fdt_next_tag(const void *fdt, int startoffset, int *nextoffset)
>
>         case FDT_PROP:
>                 lenp = fdt_offset_ptr(fdt, offset, sizeof(*lenp));
> -               if (!can_assume(VALID_DTB) && !lenp)
> +               if (!can_assume(VALID_DTB) &&
> +                   (!lenp || (INT_MAX <= fdt32_to_cpu(*lenp))))

We now have fdt32_to_cpu(*lenp) 4 times. Stick it in a local var.

>                         return FDT_END; /* premature end */
> +
>                 /* skip-name offset, length and value */
>                 offset += sizeof(struct fdt_property) - FDT_TAGSIZE
>                         + fdt32_to_cpu(*lenp);
> +
> +               if (offset < 0)

Needs a can_assume(VALID_DTB) check.

> +                       return FDT_END; /* premature end */
> +
>                 if (!can_assume(LATEST) &&
>                     fdt_version(fdt) < 0x10 && fdt32_to_cpu(*lenp) >= 8 &&
>                     ((offset - fdt32_to_cpu(*lenp)) % 8) != 0)
> --
> 2.37.3
>

      parent reply	other threads:[~2022-09-30 13:18 UTC|newest]

Thread overview: 3+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2022-09-29 23:55 [PATCH 1/2] libfdt: prevent integer overflow in fdt_next_tag Tadeusz Struk
     [not found] ` <20220929235536.618370-1-tadeusz.struk-QSEj5FYQhm4dnm+yROfE0A@public.gmane.org>
2022-09-29 23:55   ` [PATCH 2/2] libfdt: tests: add get_next_tag_invalid_prop_len Tadeusz Struk
2022-09-30 13:18   ` Rob Herring [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='CAL_JsqJ7E6D=jxmHXnh5cR_UyitpYVSPEQASsWu+eDyQ4J-Eig@mail.gmail.com' \
    --to=robh-dgejt+ai2ygdnm+yrofe0a@public.gmane.org \
    --cc=david-xT8FGy+AXnRB3Ne2BGzF6laj5H9X9Tb+@public.gmane.org \
    --cc=devicetree-compiler-u79uwXL29TY76Z2rM5mHXA@public.gmane.org \
    --cc=tadeusz.struk-QSEj5FYQhm4dnm+yROfE0A@public.gmane.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).