mirror of https://lore.kernel.org/lkml/
 help / color / mirror / Atom feed
* [PATCH] nvmem: brcm_nvram: validate cached NVRAM length
@ 2026-07-15  8:45 Pengpeng Hou
  2026-07-16 21:50 ` Srinivas Kandagatla
  0 siblings, 1 reply; 2+ messages in thread
From: Pengpeng Hou @ 2026-07-15  8:45 UTC (permalink / raw)
  To: srini; +Cc: Pengpeng Hou, rafal, linux-kernel, stable

The cached-content conversion stops at the partition padding.  Therefore,
data_len may be smaller than nvmem_size.  brcm_nvram_parse() checks
header->len against nvmem_size, then passes that length to
brcm_nvram_add_cells(), which temporarily writes data[len - 1] and parses
NUL-separated entries.

A corrupted header can declare a length that fits the MMIO resource but
exceeds the cached allocation.  Also reject a cached object that is too
small for the header, and reject declared lengths shorter than the header.

Validate the declaration against data_len, which bounds the parsed object.

Fixes: 1e37bf84afac ("nvmem: brcm_nvram: store a copy of NVRAM content")
Cc: stable@vger.kernel.org
Signed-off-by: Pengpeng Hou <pengpeng@iscas.ac.cn>
---
 drivers/nvmem/brcm_nvram.c | 12 +++++++++---
 1 file changed, 9 insertions(+), 3 deletions(-)

diff --git a/drivers/nvmem/brcm_nvram.c b/drivers/nvmem/brcm_nvram.c
index aaa6537798bf..4701772cfa47 100644
--- a/drivers/nvmem/brcm_nvram.c
+++ b/drivers/nvmem/brcm_nvram.c
@@ -181,15 +181,21 @@ static int brcm_nvram_parse(struct brcm_nvram *priv)
 	size_t len;
 	int err;
 
+	if (priv->data_len < sizeof(*header)) {
+		dev_err(dev, "NVRAM content (%zu) is smaller than header (%zu)\n",
+			priv->data_len, sizeof(*header));
+		return -EINVAL;
+	}
+
 	if (memcmp(header->magic, NVRAM_MAGIC, 4)) {
 		dev_err(dev, "Invalid NVRAM magic\n");
 		return -EINVAL;
 	}
 
 	len = le32_to_cpu(header->len);
-	if (len > priv->nvmem_size) {
-		dev_err(dev, "NVRAM length (%zd) exceeds mapped size (%zd)\n", len,
-			priv->nvmem_size);
+	if (len < sizeof(*header) || len > priv->data_len) {
+		dev_err(dev, "NVRAM length (%zu) is outside cached content (%zu)\n",
+			len, priv->data_len);
 		return -EINVAL;
 	}
 
-- 
2.43.0


^ permalink raw reply	[flat|nested] 2+ messages in thread

* Re: [PATCH] nvmem: brcm_nvram: validate cached NVRAM length
  2026-07-15  8:45 [PATCH] nvmem: brcm_nvram: validate cached NVRAM length Pengpeng Hou
@ 2026-07-16 21:50 ` Srinivas Kandagatla
  0 siblings, 0 replies; 2+ messages in thread
From: Srinivas Kandagatla @ 2026-07-16 21:50 UTC (permalink / raw)
  To: Pengpeng Hou, srini; +Cc: rafal, linux-kernel, stable



On 7/15/26 9:45 AM, Pengpeng Hou wrote:
> The cached-content conversion stops at the partition padding.  Therefore,
> data_len may be smaller than nvmem_size.  brcm_nvram_parse() checks
> header->len against nvmem_size, then passes that length to
> brcm_nvram_add_cells(), which temporarily writes data[len - 1] and parses
> NUL-separated entries.
> 
> A corrupted header can declare a length that fits the MMIO resource but
> exceeds the cached allocation.  Also reject a cached object that is too
> small for the header, and reject declared lengths shorter than the header.
> 
> Validate the declaration against data_len, which bounds the parsed object.
> 
> Fixes: 1e37bf84afac ("nvmem: brcm_nvram: store a copy of NVRAM content")
> Cc: stable@vger.kernel.org
> Signed-off-by: Pengpeng Hou <pengpeng@iscas.ac.cn>

There are already few patches on the list which are doing pretty much
same changes, i have applied them which should appear in linux-next in a
day.

https://lkml.org/lkml/2026/7/3/2291

--srini
> ---
>  drivers/nvmem/brcm_nvram.c | 12 +++++++++---
>  1 file changed, 9 insertions(+), 3 deletions(-)
> 
> diff --git a/drivers/nvmem/brcm_nvram.c b/drivers/nvmem/brcm_nvram.c
> index aaa6537798bf..4701772cfa47 100644
> --- a/drivers/nvmem/brcm_nvram.c
> +++ b/drivers/nvmem/brcm_nvram.c
> @@ -181,15 +181,21 @@ static int brcm_nvram_parse(struct brcm_nvram *priv)
>  	size_t len;
>  	int err;
>  
> +	if (priv->data_len < sizeof(*header)) {
> +		dev_err(dev, "NVRAM content (%zu) is smaller than header (%zu)\n",
> +			priv->data_len, sizeof(*header));
> +		return -EINVAL;
> +	}
> +
>  	if (memcmp(header->magic, NVRAM_MAGIC, 4)) {
>  		dev_err(dev, "Invalid NVRAM magic\n");
>  		return -EINVAL;
>  	}
>  
>  	len = le32_to_cpu(header->len);
> -	if (len > priv->nvmem_size) {
> -		dev_err(dev, "NVRAM length (%zd) exceeds mapped size (%zd)\n", len,
> -			priv->nvmem_size);
> +	if (len < sizeof(*header) || len > priv->data_len) {
> +		dev_err(dev, "NVRAM length (%zu) is outside cached content (%zu)\n",
> +			len, priv->data_len);
>  		return -EINVAL;
>  	}
>  


^ permalink raw reply	[flat|nested] 2+ messages in thread

end of thread, other threads:[~2026-07-16 21:50 UTC | newest]

Thread overview: 2+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2026-07-15  8:45 [PATCH] nvmem: brcm_nvram: validate cached NVRAM length Pengpeng Hou
2026-07-16 21:50 ` Srinivas Kandagatla

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox

Powered by JetHome