linux-arm-kernel.lists.infradead.org archive mirror
 help / color / mirror / Atom feed
* [PATCH] clk: zynq: Prevent null pointer dereference caused by kmalloc failure
@ 2024-02-14  7:33 Duoming Zhou
  2024-02-19 13:35 ` Michal Simek
  2024-02-28 23:07 ` Stephen Boyd
  0 siblings, 2 replies; 3+ messages in thread
From: Duoming Zhou @ 2024-02-14  7:33 UTC (permalink / raw)
  To: linux-kernel
  Cc: mturquette, sboyd, michal.simek, linux-clk, linux-arm-kernel,
	Duoming Zhou

The kmalloc in zynq_clk_setup will return null if the
physical memory has run out. As a result, if we use
snprintf to write data to the null address, the null
pointer dereference bug will happen.

The required memory is only 11 bytes, so this patch
adds a "__GFP_NOFAIL" flag in kmalloc in order that
the kmalloc will never return with failure.

Fixes: 0ee52b157b8e ("clk: zynq: Add clock controller driver")
Signed-off-by: Duoming Zhou <duoming@zju.edu.cn>
---
 drivers/clk/zynq/clkc.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)

diff --git a/drivers/clk/zynq/clkc.c b/drivers/clk/zynq/clkc.c
index 7bdeaff2bfd..7621c2f0046 100644
--- a/drivers/clk/zynq/clkc.c
+++ b/drivers/clk/zynq/clkc.c
@@ -427,7 +427,7 @@ static void __init zynq_clk_setup(struct device_node *np)
 			SLCR_GEM1_CLK_CTRL, 0, 0, &gem1clk_lock);
 
 	tmp = strlen("mio_clk_00x");
-	clk_name = kmalloc(tmp, GFP_KERNEL);
+	clk_name = kmalloc(tmp, GFP_KERNEL | __GFP_NOFAIL);
 	for (i = 0; i < NUM_MIO_PINS; i++) {
 		int idx;
 
-- 
2.17.1


_______________________________________________
linux-arm-kernel mailing list
linux-arm-kernel@lists.infradead.org
http://lists.infradead.org/mailman/listinfo/linux-arm-kernel

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

* Re: [PATCH] clk: zynq: Prevent null pointer dereference caused by kmalloc failure
  2024-02-14  7:33 [PATCH] clk: zynq: Prevent null pointer dereference caused by kmalloc failure Duoming Zhou
@ 2024-02-19 13:35 ` Michal Simek
  2024-02-28 23:07 ` Stephen Boyd
  1 sibling, 0 replies; 3+ messages in thread
From: Michal Simek @ 2024-02-19 13:35 UTC (permalink / raw)
  To: Duoming Zhou, linux-kernel; +Cc: mturquette, sboyd, linux-clk, linux-arm-kernel



On 2/14/24 08:33, Duoming Zhou wrote:
> The kmalloc in zynq_clk_setup will return null if the
> physical memory has run out. As a result, if we use
> snprintf to write data to the null address, the null
> pointer dereference bug will happen.
> 
> The required memory is only 11 bytes, so this patch
> adds a "__GFP_NOFAIL" flag in kmalloc in order that
> the kmalloc will never return with failure.
> 
> Fixes: 0ee52b157b8e ("clk: zynq: Add clock controller driver")
> Signed-off-by: Duoming Zhou <duoming@zju.edu.cn>
> ---
>   drivers/clk/zynq/clkc.c | 2 +-
>   1 file changed, 1 insertion(+), 1 deletion(-)
> 
> diff --git a/drivers/clk/zynq/clkc.c b/drivers/clk/zynq/clkc.c
> index 7bdeaff2bfd..7621c2f0046 100644
> --- a/drivers/clk/zynq/clkc.c
> +++ b/drivers/clk/zynq/clkc.c
> @@ -427,7 +427,7 @@ static void __init zynq_clk_setup(struct device_node *np)
>   			SLCR_GEM1_CLK_CTRL, 0, 0, &gem1clk_lock);
>   
>   	tmp = strlen("mio_clk_00x");
> -	clk_name = kmalloc(tmp, GFP_KERNEL);
> +	clk_name = kmalloc(tmp, GFP_KERNEL | __GFP_NOFAIL);
>   	for (i = 0; i < NUM_MIO_PINS; i++) {
>   		int idx;
>   

Reviewed-by: Michal Simek <michal.simek@amd.com>

Thanks,
Michal

_______________________________________________
linux-arm-kernel mailing list
linux-arm-kernel@lists.infradead.org
http://lists.infradead.org/mailman/listinfo/linux-arm-kernel

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

* Re: [PATCH] clk: zynq: Prevent null pointer dereference caused by kmalloc failure
  2024-02-14  7:33 [PATCH] clk: zynq: Prevent null pointer dereference caused by kmalloc failure Duoming Zhou
  2024-02-19 13:35 ` Michal Simek
@ 2024-02-28 23:07 ` Stephen Boyd
  1 sibling, 0 replies; 3+ messages in thread
From: Stephen Boyd @ 2024-02-28 23:07 UTC (permalink / raw)
  To: Duoming Zhou, linux-kernel
  Cc: mturquette, michal.simek, linux-clk, linux-arm-kernel,
	Duoming Zhou

Quoting Duoming Zhou (2024-02-13 23:33:18)
> diff --git a/drivers/clk/zynq/clkc.c b/drivers/clk/zynq/clkc.c
> index 7bdeaff2bfd..7621c2f0046 100644
> --- a/drivers/clk/zynq/clkc.c
> +++ b/drivers/clk/zynq/clkc.c
> @@ -427,7 +427,7 @@ static void __init zynq_clk_setup(struct device_node *np)
>                         SLCR_GEM1_CLK_CTRL, 0, 0, &gem1clk_lock);
>  
>         tmp = strlen("mio_clk_00x");
> -       clk_name = kmalloc(tmp, GFP_KERNEL);
> +       clk_name = kmalloc(tmp, GFP_KERNEL | __GFP_NOFAIL);

Just make a stack variable please. __GFP_NOFAIL is a bad code smell.

>         for (i = 0; i < NUM_MIO_PINS; i++) {
>                 int idx;
>

_______________________________________________
linux-arm-kernel mailing list
linux-arm-kernel@lists.infradead.org
http://lists.infradead.org/mailman/listinfo/linux-arm-kernel

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

end of thread, other threads:[~2024-02-28 23:07 UTC | newest]

Thread overview: 3+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2024-02-14  7:33 [PATCH] clk: zynq: Prevent null pointer dereference caused by kmalloc failure Duoming Zhou
2024-02-19 13:35 ` Michal Simek
2024-02-28 23:07 ` Stephen Boyd

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).