From: David Gibson <david-xT8FGy+AXnRB3Ne2BGzF6laj5H9X9Tb+@public.gmane.org>
To: Andre Przywara <andre.przywara-5wv7dgnIgG8@public.gmane.org>
Cc: devicetree-compiler-u79uwXL29TY76Z2rM5mHXA@public.gmane.org,
Rob Herring <robh-DgEjT+Ai2ygdnm+yROfE0A@public.gmane.org>,
Simon Glass <sjg-F7+t8E8rja9g9hUCZPvPmw@public.gmane.org>
Subject: Re: [PATCH 1/4] libfdt: Check for invalid memreserve block
Date: Sat, 12 Jun 2021 12:25:29 +1000 [thread overview]
Message-ID: <YMQbGbZVxdB/5xDo@yekko> (raw)
In-Reply-To: <20210611115823.31583-2-andre.przywara-5wv7dgnIgG8@public.gmane.org>
[-- Attachment #1: Type: text/plain, Size: 2722 bytes --]
On Fri, Jun 11, 2021 at 12:58:20PM +0100, Andre Przywara wrote:
> fdt_num_mem_rsv() can return a negative error value, when the memreserve
> block is not properly terminated or is exceeding its advertised size,
> This can happen on malformed DTBs, and FDT_RO_PROBE() does not cover this.
>
> Check the return value of fdt_num_mem_rsv() first for an error
> condition, and also check if the total size of the memreserve block
> would provoke a signed integer overflow.
>
> This fixes issues with malformed DT blobs, where we would end up with
> negative offsets into buffers.
>
> Signed-off-by: Andre Przywara <andre.przywara-5wv7dgnIgG8@public.gmane.org>
> ---
> libfdt/fdt_rw.c | 19 +++++++++++++++----
> 1 file changed, 15 insertions(+), 4 deletions(-)
>
> diff --git a/libfdt/fdt_rw.c b/libfdt/fdt_rw.c
> index 3621d36..a0f03d0 100644
> --- a/libfdt/fdt_rw.c
> +++ b/libfdt/fdt_rw.c
> @@ -427,8 +427,13 @@ int fdt_open_into(const void *fdt, void *buf, int bufsize)
>
> FDT_RO_PROBE(fdt);
>
> - mem_rsv_size = (fdt_num_mem_rsv(fdt)+1)
> - * sizeof(struct fdt_reserve_entry);
> + mem_rsv_size = fdt_num_mem_rsv(fdt);
> + if (mem_rsv_size < 0)
You want a can_assume(VALID_DTB) on this test as well. The people
doing fdt on miniscule first stage roms really hate every extra
instruction.
> + return mem_rsv_size;
> +
> + mem_rsv_size = (mem_rsv_size + 1) * sizeof(struct fdt_reserve_entry);
> + if (!can_assume(VALID_DTB) && mem_rsv_size < 0)
> + return -FDT_ERR_NOSPACE;
This second test is not necessary. fdt_num_mem_rsv() implicitly
checks for a minimally sane mem reserve block, which implies its size
lies within totalsize, which in turn is <= INT_MAX.
If it were necessary, I don't think it's quite safe, because signed
overflow is UB, IIUC.
> if (can_assume(LATEST) || fdt_version(fdt) >= 17) {
> struct_size = fdt_size_dt_struct(fdt);
> @@ -490,8 +495,14 @@ int fdt_pack(void *fdt)
>
> FDT_RW_PROBE(fdt);
>
> - mem_rsv_size = (fdt_num_mem_rsv(fdt)+1)
> - * sizeof(struct fdt_reserve_entry);
> + mem_rsv_size = fdt_num_mem_rsv(fdt);
> + if (mem_rsv_size < 0)
> + return mem_rsv_size;
> +
> + mem_rsv_size = (mem_rsv_size + 1) * sizeof(struct fdt_reserve_entry);
> + if (!can_assume(VALID_DTB) && mem_rsv_size < 0)
> + return -FDT_ERR_NOSPACE;
Same comments here.
> fdt_packblocks_(fdt, fdt, mem_rsv_size, fdt_size_dt_struct(fdt),
> fdt_size_dt_strings(fdt));
> fdt_set_totalsize(fdt, fdt_data_size_(fdt));
--
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 --]
next prev parent reply other threads:[~2021-06-12 2:25 UTC|newest]
Thread overview: 9+ messages / expand[flat|nested] mbox.gz Atom feed top
2021-06-11 11:58 [PATCH 0/4] libfdt: Check for integer overflows Andre Przywara
[not found] ` <20210611115823.31583-1-andre.przywara-5wv7dgnIgG8@public.gmane.org>
2021-06-11 11:58 ` [PATCH 1/4] libfdt: Check for invalid memreserve block Andre Przywara
[not found] ` <20210611115823.31583-2-andre.przywara-5wv7dgnIgG8@public.gmane.org>
2021-06-12 2:25 ` David Gibson [this message]
2021-06-11 11:58 ` [PATCH 2/4] tests: Enhance truncated_memrsv to test fdt_open_into() Andre Przywara
[not found] ` <20210611115823.31583-3-andre.przywara-5wv7dgnIgG8@public.gmane.org>
2021-06-12 2:26 ` David Gibson
2021-06-11 11:58 ` [PATCH 3/4] libfdt: Check DT struct size for integer overflow Andre Przywara
[not found] ` <20210611115823.31583-4-andre.przywara-5wv7dgnIgG8@public.gmane.org>
2021-06-12 2:29 ` David Gibson
2021-06-11 11:58 ` [PATCH 4/4] libfdt: Protect buffer size checks against " Andre Przywara
[not found] ` <20210611115823.31583-5-andre.przywara-5wv7dgnIgG8@public.gmane.org>
2021-06-15 0:42 ` David Gibson
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=YMQbGbZVxdB/5xDo@yekko \
--to=david-xt8fgy+axnrb3ne2bgzf6laj5h9x9tb+@public.gmane.org \
--cc=andre.przywara-5wv7dgnIgG8@public.gmane.org \
--cc=devicetree-compiler-u79uwXL29TY76Z2rM5mHXA@public.gmane.org \
--cc=robh-DgEjT+Ai2ygdnm+yROfE0A@public.gmane.org \
--cc=sjg-F7+t8E8rja9g9hUCZPvPmw@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).