From: Tudor Ambarus <tudor.ambarus@linaro.org>
To: Andrea Righi <andrea.righi@canonical.com>,
Pratyush Yadav <pratyush@kernel.org>
Cc: Michael Walle <michael@walle.cc>,
Miquel Raynal <miquel.raynal@bootlin.com>,
Richard Weinberger <richard@nod.at>,
Vignesh Raghavendra <vigneshr@ti.com>,
Cristian Birsan <cristian.birsan@microchip.com>,
Boris Brezillon <bbrezillon@kernel.org>,
Emil Renner Berthing <emil.renner.berthing@canonical.com>,
linux-mtd@lists.infradead.org, linux-kernel@vger.kernel.org
Subject: Re: [PATCH] mtd: spi-nor: fix shift-out-of-bounds in spi_nor_set_erase_type()
Date: Wed, 22 Feb 2023 14:50:42 +0000 [thread overview]
Message-ID: <d87efbd8-5290-b462-beb3-c2d3be267ade@linaro.org> (raw)
In-Reply-To: <20230221111346.34268-1-andrea.righi@canonical.com>
Hi!
This should be fixed by:
https://git.kernel.org/pub/scm/linux/kernel/git/mtd/linux.git/commit/?h=spi-nor/next&id=f0f0cfdc3a024e21161714f2e05f0df3b84d42ad
Which base did you use?
Cheers,
ta
On 2/21/23 11:13, Andrea Righi wrote:
> It seems that according to JEDEC JESD216B Standard erase size needs to
> be a power of 2, but sometimes we set the size to 0 (e.g., in
> spi_nor_parse_4bait()) causing UBSAN warnings like the following:
>
> UBSAN: shift-out-of-bounds in drivers/mtd/spi-nor/core.c:2026:24
> shift exponent 4294967295 is too large for 32-bit type 'int'
> Hardware name: Dell Inc. XPS 13 9300/077Y9N, BIOS 1.11.0 03/22/2022
> Call Trace:
> <TASK>
> show_stack+0x4e/0x61
> dump_stack_lvl+0x4a/0x6f
> dump_stack+0x10/0x18
> ubsan_epilogue+0x9/0x3a
> __ubsan_handle_shift_out_of_bounds.cold+0x61/0xef
> spi_nor_set_erase_type.cold+0x16/0x1e [spi_nor]
> spi_nor_parse_4bait+0x270/0x380 [spi_nor]
> spi_nor_parse_sfdp+0x47f/0x610 [spi_nor]
>
> Fix by checking if size is a power when setting struct
> spi_nor_erase_type, otherwise consider size, mask and shift as invalid.
>
> Fixes: 5390a8df769e ("mtd: spi-nor: add support to non-uniform SFDP SPI NOR flash memories")
> Reported-by: Emil Renner Berthing <emil.renner.berthing@canonical.com>
> Signed-off-by: Andrea Righi <andrea.righi@canonical.com>
> ---
> drivers/mtd/spi-nor/core.c | 12 +++++++++---
> 1 file changed, 9 insertions(+), 3 deletions(-)
>
> diff --git a/drivers/mtd/spi-nor/core.c b/drivers/mtd/spi-nor/core.c
> index d67c926bca8b..3c5b5bf9cbd1 100644
> --- a/drivers/mtd/spi-nor/core.c
> +++ b/drivers/mtd/spi-nor/core.c
> @@ -2019,11 +2019,17 @@ spi_nor_spimem_adjust_hwcaps(struct spi_nor *nor, u32 *hwcaps)
> void spi_nor_set_erase_type(struct spi_nor_erase_type *erase, u32 size,
> u8 opcode)
> {
> - erase->size = size;
> erase->opcode = opcode;
> /* JEDEC JESD216B Standard imposes erase sizes to be power of 2. */
> - erase->size_shift = ffs(erase->size) - 1;
> - erase->size_mask = (1 << erase->size_shift) - 1;
> + if (likely(is_power_of_2(size))) {
> + erase->size = size;
> + erase->size_shift = ffs(erase->size) - 1;
> + erase->size_mask = (1 << erase->size_shift) - 1;
> + } else {
> + erase->size = 0u;
> + erase->size_shift = ~0u;
> + erase->size_mask = ~0u;
> + }
> }
>
> /**
______________________________________________________
Linux MTD discussion mailing list
http://lists.infradead.org/mailman/listinfo/linux-mtd/
next prev parent reply other threads:[~2023-02-22 14:51 UTC|newest]
Thread overview: 3+ messages / expand[flat|nested] mbox.gz Atom feed top
2023-02-21 11:13 [PATCH] mtd: spi-nor: fix shift-out-of-bounds in spi_nor_set_erase_type() Andrea Righi
2023-02-22 14:50 ` Tudor Ambarus [this message]
2023-02-22 15:37 ` Andrea Righi
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=d87efbd8-5290-b462-beb3-c2d3be267ade@linaro.org \
--to=tudor.ambarus@linaro.org \
--cc=andrea.righi@canonical.com \
--cc=bbrezillon@kernel.org \
--cc=cristian.birsan@microchip.com \
--cc=emil.renner.berthing@canonical.com \
--cc=linux-kernel@vger.kernel.org \
--cc=linux-mtd@lists.infradead.org \
--cc=michael@walle.cc \
--cc=miquel.raynal@bootlin.com \
--cc=pratyush@kernel.org \
--cc=richard@nod.at \
--cc=vigneshr@ti.com \
/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