ARM: mvebu: potential dereference of null pointer

Message ID 202301191051184033370@zte.com.cn
State New
Headers
Series ARM: mvebu: potential dereference of null pointer |

Commit Message

ye.xingchen@zte.com.cn Jan. 19, 2023, 2:51 a.m. UTC
  From: Minghao Chi <chi.minghao@zte.com.cn>

The return value of kzalloc() needs to be checked.
To avoid use of null pointer in case of the failure of alloc.

Reported-by: Zeal Robot <zealci@zte.com.cn>
Signed-off-by: Minghao Chi <chi.minghao@zte.com.cn>
Signed-off-by: ye xingchen <ye.xingchen@zte.com.cn>
---
 arch/arm/mach-mvebu/board-v7.c  | 4 ++++
 arch/arm/mach-mvebu/coherency.c | 4 ++++
 2 files changed, 8 insertions(+)
  

Comments

Andrew Lunn Jan. 19, 2023, 1:38 p.m. UTC | #1
On Thu, Jan 19, 2023 at 10:51:18AM +0800, ye.xingchen@zte.com.cn wrote:
> From: Minghao Chi <chi.minghao@zte.com.cn>
> 
> The return value of kzalloc() needs to be checked.
> To avoid use of null pointer in case of the failure of alloc.
> 
> Reported-by: Zeal Robot <zealci@zte.com.cn>
> Signed-off-by: Minghao Chi <chi.minghao@zte.com.cn>
> Signed-off-by: ye xingchen <ye.xingchen@zte.com.cn>

Reviewed-by: Andrew Lunn <andrew@lunn.ch>

    Andrew
  
Russell King (Oracle) Jan. 19, 2023, 6:01 p.m. UTC | #2
On Thu, Jan 19, 2023 at 10:51:18AM +0800, ye.xingchen@zte.com.cn wrote:
> From: Minghao Chi <chi.minghao@zte.com.cn>
> 
> The return value of kzalloc() needs to be checked.
> To avoid use of null pointer in case of the failure of alloc.
> 
> Reported-by: Zeal Robot <zealci@zte.com.cn>
> Signed-off-by: Minghao Chi <chi.minghao@zte.com.cn>
> Signed-off-by: ye xingchen <ye.xingchen@zte.com.cn>
> ---
>  arch/arm/mach-mvebu/board-v7.c  | 4 ++++
>  arch/arm/mach-mvebu/coherency.c | 4 ++++
>  2 files changed, 8 insertions(+)
> 
> diff --git a/arch/arm/mach-mvebu/board-v7.c b/arch/arm/mach-mvebu/board-v7.c
> index fd5d0c8ff695..3c031b2efe16 100644
> --- a/arch/arm/mach-mvebu/board-v7.c
> +++ b/arch/arm/mach-mvebu/board-v7.c
> @@ -125,11 +125,15 @@ static void __init i2c_quirk(void)
>  		struct property *new_compat;
> 
>  		new_compat = kzalloc(sizeof(*new_compat), GFP_KERNEL);
> +		if (!new_compat)
> +			return;
> 
>  		new_compat->name = kstrdup("compatible", GFP_KERNEL);
>  		new_compat->length = sizeof("marvell,mv78230-a0-i2c");
>  		new_compat->value = kstrdup("marvell,mv78230-a0-i2c",
>  						GFP_KERNEL);
> +		if (!new_compat->name || !new_compat->value)
> +			return;

... and then someone else comes along and spots that "new_compat"
gets leaked, so we get another patch to add a kfree() for new_compat.

Why not do the job properly first time around?

>  		of_update_property(np, new_compat);
>  	}
> diff --git a/arch/arm/mach-mvebu/coherency.c b/arch/arm/mach-mvebu/coherency.c
> index a6b621ff0b87..8291185c52cc 100644
> --- a/arch/arm/mach-mvebu/coherency.c
> +++ b/arch/arm/mach-mvebu/coherency.c
> @@ -191,7 +191,11 @@ static void __init armada_375_380_coherency_init(struct device_node *np)
>  		struct property *p;
> 
>  		p = kzalloc(sizeof(*p), GFP_KERNEL);
> +		if (!p)
> +			return;
>  		p->name = kstrdup("arm,io-coherent", GFP_KERNEL);
> +		if (!p->name)
> +			return;

Same problem here.
  

Patch

diff --git a/arch/arm/mach-mvebu/board-v7.c b/arch/arm/mach-mvebu/board-v7.c
index fd5d0c8ff695..3c031b2efe16 100644
--- a/arch/arm/mach-mvebu/board-v7.c
+++ b/arch/arm/mach-mvebu/board-v7.c
@@ -125,11 +125,15 @@  static void __init i2c_quirk(void)
 		struct property *new_compat;

 		new_compat = kzalloc(sizeof(*new_compat), GFP_KERNEL);
+		if (!new_compat)
+			return;

 		new_compat->name = kstrdup("compatible", GFP_KERNEL);
 		new_compat->length = sizeof("marvell,mv78230-a0-i2c");
 		new_compat->value = kstrdup("marvell,mv78230-a0-i2c",
 						GFP_KERNEL);
+		if (!new_compat->name || !new_compat->value)
+			return;

 		of_update_property(np, new_compat);
 	}
diff --git a/arch/arm/mach-mvebu/coherency.c b/arch/arm/mach-mvebu/coherency.c
index a6b621ff0b87..8291185c52cc 100644
--- a/arch/arm/mach-mvebu/coherency.c
+++ b/arch/arm/mach-mvebu/coherency.c
@@ -191,7 +191,11 @@  static void __init armada_375_380_coherency_init(struct device_node *np)
 		struct property *p;

 		p = kzalloc(sizeof(*p), GFP_KERNEL);
+		if (!p)
+			return;
 		p->name = kstrdup("arm,io-coherent", GFP_KERNEL);
+		if (!p->name)
+			return;
 		of_add_property(cache_dn, p);
 	}
 }