Subject: [PATCH v2] clk: Sanitize possible_parent_show to Handle Return Value of of_clk_get_parent_name

From: Alessandro Carminati <[email protected]>

In the possible_parent_show function, ensure proper handling of the return
value from of_clk_get_parent_name to prevent potential issues arising from
a NULL return.
The current implementation invokes seq_puts directly on the result of
of_clk_get_parent_name without verifying the return value, which can lead
to kernel panic if the function returns NULL.

This patch addresses the concern by introducing a check on the return
value of of_clk_get_parent_name. If the return value is not NULL, the
function proceeds to call seq_puts, providing the returned value as
argument.
However, if of_clk_get_parent_name returns NULL, the function provides a
static string as argument, avoiding the panic.

Fixes: 1ccc0ddf046a ("clk: Use seq_puts() in possible_parent_show()")
Reported-by: Philip Daly <[email protected]>
Signed-off-by: Alessandro Carminati (Red Hat) <[email protected]>
---
drivers/clk/clk.c | 21 ++++++++++++---------
1 file changed, 12 insertions(+), 9 deletions(-)

diff --git a/drivers/clk/clk.c b/drivers/clk/clk.c
index c249f9791ae8..473563bc7496 100644
--- a/drivers/clk/clk.c
+++ b/drivers/clk/clk.c
@@ -3416,6 +3416,7 @@ static void possible_parent_show(struct seq_file *s, struct clk_core *core,
unsigned int i, char terminator)
{
struct clk_core *parent;
+ const char *name = NULL;

/*
* Go through the following options to fetch a parent's name.
@@ -3430,18 +3431,20 @@ static void possible_parent_show(struct seq_file *s, struct clk_core *core,
* registered (yet).
*/
parent = clk_core_get_parent_by_index(core, i);
- if (parent)
+ if (parent) {
seq_puts(s, parent->name);
- else if (core->parents[i].name)
+ } else if (core->parents[i].name) {
seq_puts(s, core->parents[i].name);
- else if (core->parents[i].fw_name)
+ } else if (core->parents[i].fw_name) {
seq_printf(s, "<%s>(fw)", core->parents[i].fw_name);
- else if (core->parents[i].index >= 0)
- seq_puts(s,
- of_clk_get_parent_name(core->of_node,
- core->parents[i].index));
- else
- seq_puts(s, "(missing)");
+ } else {
+ if (core->parents[i].index >= 0)
+ name = of_clk_get_parent_name(core->of_node, core->parents[i].index);
+ if (!name)
+ name = "(missing)";
+
+ seq_puts(s, name);
+ }

seq_putc(s, terminator);
}
--
2.34.1


Subject: Re: [PATCH v2] clk: Sanitize possible_parent_show to Handle Return Value of of_clk_get_parent_name

gentle ping

In the original email, I acknowledge that I misplaced the maintainers mail
addresse from the "to" field.

Il giorno gio 21 set 2023 alle ore 09:33 Alessandro Carminati (Red
Hat) <[email protected]> ha scritto:
>
> From: Alessandro Carminati <[email protected]>
>
> In the possible_parent_show function, ensure proper handling of the return
> value from of_clk_get_parent_name to prevent potential issues arising from
> a NULL return.
> The current implementation invokes seq_puts directly on the result of
> of_clk_get_parent_name without verifying the return value, which can lead
> to kernel panic if the function returns NULL.
>
> This patch addresses the concern by introducing a check on the return
> value of of_clk_get_parent_name. If the return value is not NULL, the
> function proceeds to call seq_puts, providing the returned value as
> argument.
> However, if of_clk_get_parent_name returns NULL, the function provides a
> static string as argument, avoiding the panic.
>
> Fixes: 1ccc0ddf046a ("clk: Use seq_puts() in possible_parent_show()")
> Reported-by: Philip Daly <[email protected]>
> Signed-off-by: Alessandro Carminati (Red Hat) <[email protected]>
> ---
> drivers/clk/clk.c | 21 ++++++++++++---------
> 1 file changed, 12 insertions(+), 9 deletions(-)
>
> diff --git a/drivers/clk/clk.c b/drivers/clk/clk.c
> index c249f9791ae8..473563bc7496 100644
> --- a/drivers/clk/clk.c
> +++ b/drivers/clk/clk.c
> @@ -3416,6 +3416,7 @@ static void possible_parent_show(struct seq_file *s, struct clk_core *core,
> unsigned int i, char terminator)
> {
> struct clk_core *parent;
> + const char *name = NULL;
>
> /*
> * Go through the following options to fetch a parent's name.
> @@ -3430,18 +3431,20 @@ static void possible_parent_show(struct seq_file *s, struct clk_core *core,
> * registered (yet).
> */
> parent = clk_core_get_parent_by_index(core, i);
> - if (parent)
> + if (parent) {
> seq_puts(s, parent->name);
> - else if (core->parents[i].name)
> + } else if (core->parents[i].name) {
> seq_puts(s, core->parents[i].name);
> - else if (core->parents[i].fw_name)
> + } else if (core->parents[i].fw_name) {
> seq_printf(s, "<%s>(fw)", core->parents[i].fw_name);
> - else if (core->parents[i].index >= 0)
> - seq_puts(s,
> - of_clk_get_parent_name(core->of_node,
> - core->parents[i].index));
> - else
> - seq_puts(s, "(missing)");
> + } else {
> + if (core->parents[i].index >= 0)
> + name = of_clk_get_parent_name(core->of_node, core->parents[i].index);
> + if (!name)
> + name = "(missing)";
> +
> + seq_puts(s, name);
> + }
>
> seq_putc(s, terminator);
> }
> --
> 2.34.1
>

2023-10-12 23:22:12

by Stephen Boyd

[permalink] [raw]
Subject: Re: [PATCH v2] clk: Sanitize possible_parent_show to Handle Return Value of of_clk_get_parent_name

Quoting Alessandro Carminati (Red Hat) (2023-09-21 00:32:17)
> From: Alessandro Carminati <[email protected]>
>
> In the possible_parent_show function, ensure proper handling of the return
> value from of_clk_get_parent_name to prevent potential issues arising from
> a NULL return.
> The current implementation invokes seq_puts directly on the result of
> of_clk_get_parent_name without verifying the return value, which can lead
> to kernel panic if the function returns NULL.
>
> This patch addresses the concern by introducing a check on the return
> value of of_clk_get_parent_name. If the return value is not NULL, the
> function proceeds to call seq_puts, providing the returned value as
> argument.
> However, if of_clk_get_parent_name returns NULL, the function provides a
> static string as argument, avoiding the panic.
>
> Fixes: 1ccc0ddf046a ("clk: Use seq_puts() in possible_parent_show()")
> Reported-by: Philip Daly <[email protected]>
> Signed-off-by: Alessandro Carminati (Red Hat) <[email protected]>
> ---

Applied to clk-fixes