devicetree-compiler.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
From: David Gibson <david-xT8FGy+AXnRB3Ne2BGzF6laj5H9X9Tb+@public.gmane.org>
To: Heinrich Schuchardt
	<heinrich.schuchardt-Z7WLFzj8eWMS+FvcfC7Uqw@public.gmane.org>
Cc: devicetree-compiler-u79uwXL29TY76Z2rM5mHXA@public.gmane.org
Subject: Re: [PATCH 1/1] dtc: fix asm_emit_data()
Date: Thu, 24 Aug 2023 15:15:10 +1000	[thread overview]
Message-ID: <ZObnXq5cN6RgWBB/@zatzit> (raw)
In-Reply-To: <20230822110627.141540-1-heinrich.schuchardt-Z7WLFzj8eWMS+FvcfC7Uqw@public.gmane.org>

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

On Tue, Aug 22, 2023 at 01:06:27PM +0200, Heinrich Schuchardt wrote:
> ((d.len - off) >= sizeof(uint32_t)) will possibly be true if off > d.len:

Technically true, but the code is constructed such that off is
always less than or equal to d.len.

> sizeof(uint32_t)) is an unsigned type. The C language will convert
> (d.len - off) to an unsigned type before executing the comparison.
> 
> Correct the inequality.
> Change the equality for byte output to match the one for uint32_t.
> 
> Fixes: 53359016caf6 ("dtc: Use stdint.h types throughout dtc")
> Signed-off-by: Heinrich Schuchardt <heinrich.schuchardt-Z7WLFzj8eWMS+FvcfC7Uqw@public.gmane.org>
> ---
>  flattree.c | 4 ++--
>  1 file changed, 2 insertions(+), 2 deletions(-)
> 
> diff --git a/flattree.c b/flattree.c
> index 1bcd808..9f67104 100644
> --- a/flattree.c
> +++ b/flattree.c
> @@ -156,12 +156,12 @@ static void asm_emit_data(void *e, struct data d)
>  	for_each_marker_of_type(m, LABEL)
>  		emit_offset_label(f, m->ref, m->offset);
>  
> -	while ((d.len - off) >= sizeof(uint32_t)) {
> +	while (d.len + sizeof(uint32_t) >= off) {

This eliminates the possibility of underflow, but replaces it with a
possibility of overflow.  Now there will be an overflow if d.len is
within 4 of UINT_MAX, again giving a surprising result.

Now, in practice I don't think d.len can ever exceed 2^31, but
confirming that requires looking through the entire codebase.  In
contrast confirming that off <= d.len requires only examining the few
lines of this function.

So, nack.

>  		asm_emit_cell(e, dtb_ld32(d.val + off));
>  		off += sizeof(uint32_t);
>  	}
>  
> -	while ((d.len - off) >= 1) {
> +	while (d.len + 1 >= off) {
>  		fprintf(f, "\t.byte\t0x%hhx\n", d.val[off]);
>  		off += 1;
>  	}

-- 
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 --]

      parent reply	other threads:[~2023-08-24  5:15 UTC|newest]

Thread overview: 2+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2023-08-22 11:06 [PATCH 1/1] dtc: fix asm_emit_data() Heinrich Schuchardt
     [not found] ` <20230822110627.141540-1-heinrich.schuchardt-Z7WLFzj8eWMS+FvcfC7Uqw@public.gmane.org>
2023-08-24  5:15   ` 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=ZObnXq5cN6RgWBB/@zatzit \
    --to=david-xt8fgy+axnrb3ne2bgzf6laj5h9x9tb+@public.gmane.org \
    --cc=devicetree-compiler-u79uwXL29TY76Z2rM5mHXA@public.gmane.org \
    --cc=heinrich.schuchardt-Z7WLFzj8eWMS+FvcfC7Uqw@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 NNTP newsgroup(s).