* [PATCH v2] clk: zynq: Prevent null pointer dereference caused by kmalloc failure
@ 2024-02-29 12:22 Duoming Zhou
2024-02-29 12:45 ` Michal Simek
0 siblings, 1 reply; 6+ messages in thread
From: Duoming Zhou @ 2024-02-29 12:22 UTC (permalink / raw)
To: linux-kernel
Cc: linux-arm-kernel, linux-clk, michal.simek, sboyd, mturquette,
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.
This patch adds a stack variable to replace the kmalloc().
Fixes: 0ee52b157b8e ("clk: zynq: Add clock controller driver")
Signed-off-by: Duoming Zhou <duoming@zju.edu.cn>
---
Changes in v2:
- Use stack variable to replace kmalloc().
drivers/clk/zynq/clkc.c | 3 +--
1 file changed, 1 insertion(+), 2 deletions(-)
diff --git a/drivers/clk/zynq/clkc.c b/drivers/clk/zynq/clkc.c
index 7bdeaff2bfd..e4c4c9adf79 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);
+ char clk_name[tmp];
for (i = 0; i < NUM_MIO_PINS; i++) {
int idx;
@@ -439,7 +439,6 @@ static void __init zynq_clk_setup(struct device_node *np)
else
can_mio_mux_parents[i] = dummy_nm;
}
- kfree(clk_name);
clk_register_mux(NULL, "can_mux", periph_parents, 4,
CLK_SET_RATE_NO_REPARENT, SLCR_CAN_CLK_CTRL, 4, 2, 0,
&canclk_lock);
--
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] 6+ messages in thread
* Re: [PATCH v2] clk: zynq: Prevent null pointer dereference caused by kmalloc failure
2024-02-29 12:22 [PATCH v2] clk: zynq: Prevent null pointer dereference caused by kmalloc failure Duoming Zhou
@ 2024-02-29 12:45 ` Michal Simek
2024-02-29 14:16 ` duoming
2024-03-01 1:21 ` Stephen Boyd
0 siblings, 2 replies; 6+ messages in thread
From: Michal Simek @ 2024-02-29 12:45 UTC (permalink / raw)
To: Duoming Zhou, linux-kernel; +Cc: linux-arm-kernel, linux-clk, sboyd, mturquette
On 2/29/24 13:22, 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.
>
> This patch adds a stack variable to replace the kmalloc().
>
> Fixes: 0ee52b157b8e ("clk: zynq: Add clock controller driver")
> Signed-off-by: Duoming Zhou <duoming@zju.edu.cn>
> ---
> Changes in v2:
> - Use stack variable to replace kmalloc().
>
> drivers/clk/zynq/clkc.c | 3 +--
> 1 file changed, 1 insertion(+), 2 deletions(-)
>
> diff --git a/drivers/clk/zynq/clkc.c b/drivers/clk/zynq/clkc.c
> index 7bdeaff2bfd..e4c4c9adf79 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);
> + char clk_name[tmp];
I know that Stephen asked for it but variable with variable length in the middle
of code doesn't look good or useful.
I would allocate rather bigger array on stack with size bigger than max length
which will use it.
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] 6+ messages in thread
* Re: [PATCH v2] clk: zynq: Prevent null pointer dereference caused by kmalloc failure
2024-02-29 12:45 ` Michal Simek
@ 2024-02-29 14:16 ` duoming
2024-03-01 1:22 ` Stephen Boyd
2024-03-01 1:21 ` Stephen Boyd
1 sibling, 1 reply; 6+ messages in thread
From: duoming @ 2024-02-29 14:16 UTC (permalink / raw)
To: Michal Simek; +Cc: linux-kernel, linux-arm-kernel, linux-clk, sboyd, mturquette
On Thu, 29 Feb 2024 13:45:54 +0100 Michal Simek 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.
> >
> > This patch adds a stack variable to replace the kmalloc().
> >
> > Fixes: 0ee52b157b8e ("clk: zynq: Add clock controller driver")
> > Signed-off-by: Duoming Zhou <duoming@zju.edu.cn>
> > ---
> > Changes in v2:
> > - Use stack variable to replace kmalloc().
> >
> > drivers/clk/zynq/clkc.c | 3 +--
> > 1 file changed, 1 insertion(+), 2 deletions(-)
> >
> > diff --git a/drivers/clk/zynq/clkc.c b/drivers/clk/zynq/clkc.c
> > index 7bdeaff2bfd..e4c4c9adf79 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);
> > + char clk_name[tmp];
>
> I know that Stephen asked for it but variable with variable length in the middle
> of code doesn't look good or useful.
> I would allocate rather bigger array on stack with size bigger than max length
> which will use it.
The length of "mio_clk_00x" is 11 bytes, and the kernel will alloc 16 bytes to it.
I use a local variable whose size is 16 bytes to replace it. The detail is shown
below:
diff --git a/drivers/clk/zynq/clkc.c b/drivers/clk/zynq/clkc.c
index 7bdeaff2bfd..81d530e3357 100644
--- a/drivers/clk/zynq/clkc.c
+++ b/drivers/clk/zynq/clkc.c
@@ -215,7 +215,7 @@ static void __init zynq_clk_setup(struct device_node *np)
int i;
u32 tmp;
int ret;
- char *clk_name;
+ char clk_name[16];
unsigned int fclk_enable = 0;
const char *clk_output_name[clk_max];
const char *cpu_parents[4];
@@ -427,7 +427,6 @@ 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);
for (i = 0; i < NUM_MIO_PINS; i++) {
int idx;
@@ -439,7 +438,6 @@ static void __init zynq_clk_setup(struct device_node *np)
else
can_mio_mux_parents[i] = dummy_nm;
}
- kfree(clk_name);
clk_register_mux(NULL, "can_mux", periph_parents, 4,
CLK_SET_RATE_NO_REPARENT, SLCR_CAN_CLK_CTRL, 4, 2, 0,
&canclk_lock);
Do you think the above is better?
Best regards,
Duoming Zhou
_______________________________________________
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] 6+ messages in thread
* Re: [PATCH v2] clk: zynq: Prevent null pointer dereference caused by kmalloc failure
2024-02-29 12:45 ` Michal Simek
2024-02-29 14:16 ` duoming
@ 2024-03-01 1:21 ` Stephen Boyd
2024-03-01 7:02 ` Michal Simek
1 sibling, 1 reply; 6+ messages in thread
From: Stephen Boyd @ 2024-03-01 1:21 UTC (permalink / raw)
To: Duoming Zhou, Michal Simek, linux-kernel
Cc: linux-arm-kernel, linux-clk, mturquette
Quoting Michal Simek (2024-02-29 04:45:54)
> On 2/29/24 13:22, Duoming Zhou wrote:
> > diff --git a/drivers/clk/zynq/clkc.c b/drivers/clk/zynq/clkc.c
> > index 7bdeaff2bfd..e4c4c9adf79 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);
> > + char clk_name[tmp];
>
> I know that Stephen asked for it but variable with variable length in the middle
> of code doesn't look good or useful.
I didn't ask for it to be in the middle of the function :)
_______________________________________________
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] 6+ messages in thread
* Re: [PATCH v2] clk: zynq: Prevent null pointer dereference caused by kmalloc failure
2024-02-29 14:16 ` duoming
@ 2024-03-01 1:22 ` Stephen Boyd
0 siblings, 0 replies; 6+ messages in thread
From: Stephen Boyd @ 2024-03-01 1:22 UTC (permalink / raw)
To: Michal Simek, duoming
Cc: linux-kernel, linux-arm-kernel, linux-clk, mturquette
Quoting duoming@zju.edu.cn (2024-02-29 06:16:28)
> On Thu, 29 Feb 2024 13:45:54 +0100 Michal Simek wrote:
>
> The length of "mio_clk_00x" is 11 bytes, and the kernel will alloc 16 bytes to it.
> I use a local variable whose size is 16 bytes to replace it. The detail is shown
> below:
>
> diff --git a/drivers/clk/zynq/clkc.c b/drivers/clk/zynq/clkc.c
> index 7bdeaff2bfd..81d530e3357 100644
> --- a/drivers/clk/zynq/clkc.c
> +++ b/drivers/clk/zynq/clkc.c
> @@ -215,7 +215,7 @@ static void __init zynq_clk_setup(struct device_node *np)
> int i;
> u32 tmp;
> int ret;
> - char *clk_name;
> + char clk_name[16];
> unsigned int fclk_enable = 0;
> const char *clk_output_name[clk_max];
> const char *cpu_parents[4];
> @@ -427,7 +427,6 @@ static void __init zynq_clk_setup(struct device_node *np)
> SLCR_GEM1_CLK_CTRL, 0, 0, &gem1clk_lock);
>
> tmp = strlen("mio_clk_00x");
Is tmp used now?
> - clk_name = kmalloc(tmp, GFP_KERNEL);
> for (i = 0; i < NUM_MIO_PINS; i++) {
> int idx;
>
> @@ -439,7 +438,6 @@ static void __init zynq_clk_setup(struct device_node *np)
> else
> can_mio_mux_parents[i] = dummy_nm;
> }
> - kfree(clk_name);
> clk_register_mux(NULL, "can_mux", periph_parents, 4,
> CLK_SET_RATE_NO_REPARENT, SLCR_CAN_CLK_CTRL, 4, 2, 0,
> &canclk_lock);
>
> Do you think the above is better?
>
It's getting there.
_______________________________________________
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] 6+ messages in thread
* Re: [PATCH v2] clk: zynq: Prevent null pointer dereference caused by kmalloc failure
2024-03-01 1:21 ` Stephen Boyd
@ 2024-03-01 7:02 ` Michal Simek
0 siblings, 0 replies; 6+ messages in thread
From: Michal Simek @ 2024-03-01 7:02 UTC (permalink / raw)
To: Stephen Boyd, Duoming Zhou, linux-kernel
Cc: linux-arm-kernel, linux-clk, mturquette
On 3/1/24 02:21, Stephen Boyd wrote:
> Quoting Michal Simek (2024-02-29 04:45:54)
>> On 2/29/24 13:22, Duoming Zhou wrote:
>>> diff --git a/drivers/clk/zynq/clkc.c b/drivers/clk/zynq/clkc.c
>>> index 7bdeaff2bfd..e4c4c9adf79 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);
>>> + char clk_name[tmp];
>>
>> I know that Stephen asked for it but variable with variable length in the middle
>> of code doesn't look good or useful.
>
> I didn't ask for it to be in the middle of the function :)
I didn't say it. :-)
I know it was about putting it on the stack instead.
M
_______________________________________________
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] 6+ messages in thread
end of thread, other threads:[~2024-03-01 7:02 UTC | newest]
Thread overview: 6+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2024-02-29 12:22 [PATCH v2] clk: zynq: Prevent null pointer dereference caused by kmalloc failure Duoming Zhou
2024-02-29 12:45 ` Michal Simek
2024-02-29 14:16 ` duoming
2024-03-01 1:22 ` Stephen Boyd
2024-03-01 1:21 ` Stephen Boyd
2024-03-01 7:02 ` Michal Simek
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).