Re: [PATCH] mtd: physmap: physmap-bt1-rom: Fix unintentional stack access

From: Miquel Raynal
Date: Fri Feb 12 2021 - 09:13:15 EST


Hi Gustavo,

"Gustavo A. R. Silva" <gustavoars@xxxxxxxxxx> wrote on Fri, 12 Feb 2021
04:40:22 -0600:

> Cast &data to (char *) in order to avoid unintentionally accessing
> the stack.
>
> Notice that data is of type u32, so any increment to &data
> will be in the order of 4-byte chunks, and this piece of code
> is actually intended to be a byte offset.

I don't have the same reading. I don't say that Coverity report is
wrong, but let's discuss this a bit further.

Given that &data is of type u32 *, you say that "&data + shift"
produces increments of 4-bytes, ie. we would access "&data + 4 *
shift"? Because I don't think this is the case (again, I may be wrong).

I'm sure this would be the case if we dereferenced data like
data[shift] though, but in this case I don't see what this cast is
fixing. Can you enlighten me?

Could the out-of-bounds warning come from the fact that shift
might be bigger than the data array spread?

> Fixes: b3e79e7682e0 ("mtd: physmap: Add Baikal-T1 physically mapped ROM support")
> Addresses-Coverity-ID: 1497765 ("Out-of-bounds access")
> Cc: stable@xxxxxxxxxxxxxxx
> Signed-off-by: Gustavo A. R. Silva <gustavoars@xxxxxxxxxx>
> ---
> drivers/mtd/maps/physmap-bt1-rom.c | 2 +-
> 1 file changed, 1 insertion(+), 1 deletion(-)
>
> diff --git a/drivers/mtd/maps/physmap-bt1-rom.c b/drivers/mtd/maps/physmap-bt1-rom.c
> index a35450002284..58782cfaf71c 100644
> --- a/drivers/mtd/maps/physmap-bt1-rom.c
> +++ b/drivers/mtd/maps/physmap-bt1-rom.c
> @@ -79,7 +79,7 @@ static void __xipram bt1_rom_map_copy_from(struct map_info *map,
> if (shift) {
> chunk = min_t(ssize_t, 4 - shift, len);
> data = readl_relaxed(src - shift);
> - memcpy(to, &data + shift, chunk);
> + memcpy(to, (char *)&data + shift, chunk);
> src += chunk;
> to += chunk;
> len -= chunk;

Thanks,
Miquèl