Skip to content

Commit 7938e9c

Browse files
stonezdmbebarino
authored andcommitted
clk: zynq: Prevent null pointer dereference caused by kmalloc failure
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 uses a stack variable to replace the kmalloc(). Fixes: 0ee52b1 ("clk: zynq: Add clock controller driver") Suggested-by: Michal Simek <[email protected]> Suggested-by: Stephen Boyd <[email protected]> Signed-off-by: Duoming Zhou <[email protected]> Link: https://lore.kernel.org/r/[email protected] Acked-by: Michal Simek <[email protected]> Signed-off-by: Stephen Boyd <[email protected]>
1 parent c1ab111 commit 7938e9c

File tree

1 file changed

+3
-5
lines changed

1 file changed

+3
-5
lines changed

drivers/clk/zynq/clkc.c

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,7 @@ static void __iomem *zynq_clkc_base;
4242
#define SLCR_SWDT_CLK_SEL (zynq_clkc_base + 0x204)
4343

4444
#define NUM_MIO_PINS 54
45+
#define CLK_NAME_LEN 16
4546

4647
#define DBG_CLK_CTRL_CLKACT_TRC BIT(0)
4748
#define DBG_CLK_CTRL_CPU_1XCLKACT BIT(1)
@@ -215,7 +216,7 @@ static void __init zynq_clk_setup(struct device_node *np)
215216
int i;
216217
u32 tmp;
217218
int ret;
218-
char *clk_name;
219+
char clk_name[CLK_NAME_LEN];
219220
unsigned int fclk_enable = 0;
220221
const char *clk_output_name[clk_max];
221222
const char *cpu_parents[4];
@@ -426,20 +427,17 @@ static void __init zynq_clk_setup(struct device_node *np)
426427
"gem1_emio_mux", CLK_SET_RATE_PARENT,
427428
SLCR_GEM1_CLK_CTRL, 0, 0, &gem1clk_lock);
428429

429-
tmp = strlen("mio_clk_00x");
430-
clk_name = kmalloc(tmp, GFP_KERNEL);
431430
for (i = 0; i < NUM_MIO_PINS; i++) {
432431
int idx;
433432

434-
snprintf(clk_name, tmp, "mio_clk_%2.2d", i);
433+
snprintf(clk_name, CLK_NAME_LEN, "mio_clk_%2.2d", i);
435434
idx = of_property_match_string(np, "clock-names", clk_name);
436435
if (idx >= 0)
437436
can_mio_mux_parents[i] = of_clk_get_parent_name(np,
438437
idx);
439438
else
440439
can_mio_mux_parents[i] = dummy_nm;
441440
}
442-
kfree(clk_name);
443441
clk_register_mux(NULL, "can_mux", periph_parents, 4,
444442
CLK_SET_RATE_NO_REPARENT, SLCR_CAN_CLK_CTRL, 4, 2, 0,
445443
&canclk_lock);

0 commit comments

Comments
 (0)