2022-12-12 04:04:46

by Jiasheng Jiang

[permalink] [raw]
Subject: [PATCH] nvmem: brcm_nvram: Add check for kzalloc

Add the check for the return value of kzalloc in order to avoid
NULL pointer dereference.

Fixes: 6e977eaa8280 ("nvmem: brcm_nvram: parse NVRAM content into NVMEM cells")
Signed-off-by: Jiasheng Jiang <[email protected]>
---
drivers/nvmem/brcm_nvram.c | 3 +++
1 file changed, 3 insertions(+)

diff --git a/drivers/nvmem/brcm_nvram.c b/drivers/nvmem/brcm_nvram.c
index 4441daa20965..7bd65fc2942e 100644
--- a/drivers/nvmem/brcm_nvram.c
+++ b/drivers/nvmem/brcm_nvram.c
@@ -97,6 +97,9 @@ static int brcm_nvram_parse(struct brcm_nvram *priv)
len = le32_to_cpu(header.len);

data = kzalloc(len, GFP_KERNEL);
+ if (!data)
+ return -ENOMEM;
+
memcpy_fromio(data, priv->base, len);
data[len - 1] = '\0';

--
2.25.1


2022-12-12 11:10:00

by Srinivas Kandagatla

[permalink] [raw]
Subject: Re: [PATCH] nvmem: brcm_nvram: Add check for kzalloc



On 12/12/2022 03:39, Jiasheng Jiang wrote:
> Add the check for the return value of kzalloc in order to avoid
> NULL pointer dereference.
>
> Fixes: 6e977eaa8280 ("nvmem: brcm_nvram: parse NVRAM content into NVMEM cells")
> Signed-off-by: Jiasheng Jiang <[email protected]>

Applied thanks,

--srini
> ---
> drivers/nvmem/brcm_nvram.c | 3 +++
> 1 file changed, 3 insertions(+)
>
> diff --git a/drivers/nvmem/brcm_nvram.c b/drivers/nvmem/brcm_nvram.c
> index 4441daa20965..7bd65fc2942e 100644
> --- a/drivers/nvmem/brcm_nvram.c
> +++ b/drivers/nvmem/brcm_nvram.c
> @@ -97,6 +97,9 @@ static int brcm_nvram_parse(struct brcm_nvram *priv)
> len = le32_to_cpu(header.len);
>
> data = kzalloc(len, GFP_KERNEL);
> + if (!data)
> + return -ENOMEM;
> +
> memcpy_fromio(data, priv->base, len);
> data[len - 1] = '\0';
>