diff mbox series

[V2,1/3] nvmem: u-boot-env: error if NVMEM device is too small

Message ID 20240729080733.16839-1-zajec5@gmail.com
State Handled Elsewhere
Delegated to: Tom Rini
Headers show
Series [V2,1/3] nvmem: u-boot-env: error if NVMEM device is too small | expand

Commit Message

Rafał Miłecki July 29, 2024, 8:07 a.m. UTC
From: John Thomson <git@johnthomson.fastmail.com.au>

Verify data size before trying to parse it to avoid reading out of
buffer. This could happen in case of problems at MTD level or invalid DT
bindings.

Signed-off-by: John Thomson <git@johnthomson.fastmail.com.au>
Fixes: d5542923f200 ("nvmem: add driver handling U-Boot environment variables")
[rmilecki: simplify commit description & rebase]
Signed-off-by: Rafał Miłecki <rafal@milecki.pl>
---
V2: New patch in the series

 drivers/nvmem/u-boot-env.c | 7 +++++++
 1 file changed, 7 insertions(+)

Comments

Srinivas Kandagatla Aug. 7, 2024, 9:55 a.m. UTC | #1
On Mon, 29 Jul 2024 10:07:31 +0200, Rafał Miłecki wrote:
> Verify data size before trying to parse it to avoid reading out of
> buffer. This could happen in case of problems at MTD level or invalid DT
> bindings.
> 
> 

Applied, thanks!

[1/3] nvmem: u-boot-env: error if NVMEM device is too small
      commit: a80f2ebe78d7a81354b1c9d59fcb79b77982df2d
[2/3] dt-bindings: nvmem: convert U-Boot env to a layout
      commit: 1b3f5b88c541318a95fc74db80083b87c1020e03
[3/3] nvmem: layouts: add U-Boot env layout
      commit: 641d6abbe15f96a4d2cce5dc48940fd22916d15e

Best regards,
diff mbox series

Patch

diff --git a/drivers/nvmem/u-boot-env.c b/drivers/nvmem/u-boot-env.c
index 936e39b20b38..593f0bf4a395 100644
--- a/drivers/nvmem/u-boot-env.c
+++ b/drivers/nvmem/u-boot-env.c
@@ -176,6 +176,13 @@  static int u_boot_env_parse(struct u_boot_env *priv)
 		data_offset = offsetof(struct u_boot_env_image_broadcom, data);
 		break;
 	}
+
+	if (dev_size < data_offset) {
+		dev_err(dev, "Device too small for u-boot-env\n");
+		err = -EIO;
+		goto err_kfree;
+	}
+
 	crc32_addr = (__le32 *)(buf + crc32_offset);
 	crc32 = le32_to_cpu(*crc32_addr);
 	crc32_data_len = dev_size - crc32_data_offset;