Linux clock framework development
 help / color / mirror / Atom feed
* [PATCH 0/3] clk: zynqmp: fix out-of-bounds accesses in firmware clock parsing
@ 2026-12-01  6:43 Radhey Shyam Pandey
  2026-12-01  6:43 ` [PATCH 1/3] clk: zynqmp: Fix off-by-one loop bound in zynqmp_clock_get_topology() Radhey Shyam Pandey
                   ` (2 more replies)
  0 siblings, 3 replies; 5+ messages in thread
From: Radhey Shyam Pandey @ 2026-12-01  6:43 UTC (permalink / raw)
  To: sboyd, bmasney+clk, jbrunet+clk, michal.simek, shubhrajyoti.datta
  Cc: linux-clk, linux-arm-kernel, git, Radhey Shyam Pandey

The ZynqMP clock driver builds its clock tree from PM firmware query
responses. The parsing helpers in clkc.c bound their writes by loop
arithmetic alone and never validate the values firmware returns, so a
response can be written past the end of the destination array or used
as an unchecked table index.

All three issues have been present since the driver was added:

  1/3  The topology fetch loop runs one iteration too many, writing up
       to three entries past the MAX_NODES topology array.

  2/3  The parent lookup is bounded by the count of resolved parents,
       which lags the write index, so parents[] can be overrun.

  3/3  The 4-bit topology type from firmware indexes clk_topology[] and
       clk_type_postfix[], which hold only seven entries; an
       out-of-range value can reach a function-pointer dereference.

Valid firmware responses are unaffected by this series.

Beesam Sridhar Sagar (3):
  clk: zynqmp: Fix off-by-one loop bound in zynqmp_clock_get_topology()
  clk: zynqmp: Fix unbounded parent array overflow in clock parent
    lookup
  clk: zynqmp: Fix unchecked type index into clock topology tables

 drivers/clk/zynqmp/clkc.c | 22 +++++++++++++++-------
 1 file changed, 15 insertions(+), 7 deletions(-)


base-commit: 19cc08c1a6a650f8e54c0461ff198be6448f425d
-- 
2.43.0


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

* [PATCH 1/3] clk: zynqmp: Fix off-by-one loop bound in zynqmp_clock_get_topology()
  2026-12-01  6:43 [PATCH 0/3] clk: zynqmp: fix out-of-bounds accesses in firmware clock parsing Radhey Shyam Pandey
@ 2026-12-01  6:43 ` Radhey Shyam Pandey
  2026-12-01  6:43 ` [PATCH 2/3] clk: zynqmp: Fix unbounded parent array overflow in clock parent lookup Radhey Shyam Pandey
  2026-12-01  6:43 ` [PATCH 3/3] clk: zynqmp: Fix unchecked type index into clock topology tables Radhey Shyam Pandey
  2 siblings, 0 replies; 5+ messages in thread
From: Radhey Shyam Pandey @ 2026-12-01  6:43 UTC (permalink / raw)
  To: sboyd, bmasney+clk, jbrunet+clk, michal.simek, shubhrajyoti.datta
  Cc: linux-clk, linux-arm-kernel, git, Beesam Sridhar Sagar,
	Radhey Shyam Pandey

From: Beesam Sridhar Sagar <sagar.beesamsridhar@amd.com>

The topology fetch loop iterates while j <= MAX_NODES, allowing one
extra iteration that queries firmware with an offset of MAX_NODES and
writes into topology[] past its MAX_NODES-sized bounds via
__zynqmp_clock_get_topology(). Change the condition to j < MAX_NODES
so the loop never issues a request that can overflow the topology
array.

Fixes: 3fde0e16d016 ("drivers: clk: Add ZynqMP clock driver")
Signed-off-by: Beesam Sridhar Sagar <sagar.beesamsridhar@amd.com>
Signed-off-by: Radhey Shyam Pandey <radhey.shyam.pandey@amd.com>
---
 drivers/clk/zynqmp/clkc.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)

diff --git a/drivers/clk/zynqmp/clkc.c b/drivers/clk/zynqmp/clkc.c
index d62f1901d0ab..9650c67336e6 100644
--- a/drivers/clk/zynqmp/clkc.c
+++ b/drivers/clk/zynqmp/clkc.c
@@ -450,7 +450,7 @@ static int zynqmp_clock_get_topology(u32 clk_id,
 	struct topology_resp response = { };
 
 	*num_nodes = 0;
-	for (j = 0; j <= MAX_NODES; j += ARRAY_SIZE(response.topology)) {
+	for (j = 0; j < MAX_NODES; j += ARRAY_SIZE(response.topology)) {
 		ret = zynqmp_pm_clock_get_topology(clock[clk_id].clk_id, j,
 						   &response);
 		if (ret)
-- 
2.43.0


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

* [PATCH 2/3] clk: zynqmp: Fix unbounded parent array overflow in clock parent lookup
  2026-12-01  6:43 [PATCH 0/3] clk: zynqmp: fix out-of-bounds accesses in firmware clock parsing Radhey Shyam Pandey
  2026-12-01  6:43 ` [PATCH 1/3] clk: zynqmp: Fix off-by-one loop bound in zynqmp_clock_get_topology() Radhey Shyam Pandey
@ 2026-12-01  6:43 ` Radhey Shyam Pandey
  2026-12-01  6:43 ` [PATCH 3/3] clk: zynqmp: Fix unchecked type index into clock topology tables Radhey Shyam Pandey
  2 siblings, 0 replies; 5+ messages in thread
From: Radhey Shyam Pandey @ 2026-12-01  6:43 UTC (permalink / raw)
  To: sboyd, bmasney+clk, jbrunet+clk, michal.simek, shubhrajyoti.datta
  Cc: linux-clk, linux-arm-kernel, git, Beesam Sridhar Sagar,
	Radhey Shyam Pandey

From: Beesam Sridhar Sagar <sagar.beesamsridhar@amd.com>

zynqmp_clock_get_parents() used a do/while loop keyed off *num_parents,
which counts only successfully resolved parents and can lag behind the
write index j. This allowed __zynqmp_clock_get_parents() to keep
writing into parents[] beyond MAX_PARENT entries. Bound the write
index directly against MAX_PARENT and pass the remaining capacity into
__zynqmp_clock_get_parents() so it never writes past the caller's
buffer.

Fixes: 3fde0e16d016 ("drivers: clk: Add ZynqMP clock driver")
Signed-off-by: Beesam Sridhar Sagar <sagar.beesamsridhar@amd.com>
Signed-off-by: Radhey Shyam Pandey <radhey.shyam.pandey@amd.com>
---
 drivers/clk/zynqmp/clkc.c | 16 ++++++++++------
 1 file changed, 10 insertions(+), 6 deletions(-)

diff --git a/drivers/clk/zynqmp/clkc.c b/drivers/clk/zynqmp/clkc.c
index 9650c67336e6..5d827b2bb0ad 100644
--- a/drivers/clk/zynqmp/clkc.c
+++ b/drivers/clk/zynqmp/clkc.c
@@ -469,18 +469,20 @@ static int zynqmp_clock_get_topology(u32 clk_id,
  *				   response data
  * @parents:		Clock parents
  * @response:		Clock parents data received from firmware
+ * @max_write:		Maximum number of entries that may be written to @parents
  * @nparent:		Number of parent
  *
  * Return: 0 on success else error+reason
  */
 static int __zynqmp_clock_get_parents(struct clock_parent *parents,
 				      struct parents_resp *response,
-				      u32 *nparent)
+				      u32 max_write, u32 *nparent)
 {
 	int i;
 	struct clock_parent *parent;
 
-	for (i = 0; i < ARRAY_SIZE(response->parents); i++) {
+	/* Never write past the caller-provided capacity of the parents[] array */
+	for (i = 0; i < ARRAY_SIZE(response->parents) && i < max_write; i++) {
 		if (response->parents[i] == NA_PARENT)
 			return END_OF_PARENTS;
 
@@ -512,11 +514,13 @@ static int __zynqmp_clock_get_parents(struct clock_parent *parents,
 static int zynqmp_clock_get_parents(u32 clk_id, struct clock_parent *parents,
 				    u32 *num_parents)
 {
-	int j = 0, ret;
 	struct parents_resp response = { };
+	u32 j = 0;
+	int ret;
 
 	*num_parents = 0;
-	do {
+	/* Bound the loop by the write index so parents[] is never overrun */
+	while (j < MAX_PARENT) {
 		/* Get parents from firmware */
 		ret = zynqmp_pm_clock_get_parents(clock[clk_id].clk_id, j,
 						  &response);
@@ -524,11 +528,11 @@ static int zynqmp_clock_get_parents(u32 clk_id, struct clock_parent *parents,
 			return ret;
 
 		ret = __zynqmp_clock_get_parents(&parents[j], &response,
-						 num_parents);
+						 MAX_PARENT - j, num_parents);
 		if (ret == END_OF_PARENTS)
 			return 0;
 		j += ARRAY_SIZE(response.parents);
-	} while (*num_parents <= MAX_PARENT);
+	}
 
 	return 0;
 }
-- 
2.43.0


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

* [PATCH 3/3] clk: zynqmp: Fix unchecked type index into clock topology tables
  2026-12-01  6:43 [PATCH 0/3] clk: zynqmp: fix out-of-bounds accesses in firmware clock parsing Radhey Shyam Pandey
  2026-12-01  6:43 ` [PATCH 1/3] clk: zynqmp: Fix off-by-one loop bound in zynqmp_clock_get_topology() Radhey Shyam Pandey
  2026-12-01  6:43 ` [PATCH 2/3] clk: zynqmp: Fix unbounded parent array overflow in clock parent lookup Radhey Shyam Pandey
@ 2026-12-01  6:43 ` Radhey Shyam Pandey
  2026-12-01  6:54   ` sashiko-bot
  2 siblings, 1 reply; 5+ messages in thread
From: Radhey Shyam Pandey @ 2026-12-01  6:43 UTC (permalink / raw)
  To: sboyd, bmasney+clk, jbrunet+clk, michal.simek, shubhrajyoti.datta
  Cc: linux-clk, linux-arm-kernel, git, Beesam Sridhar Sagar,
	Radhey Shyam Pandey

From: Beesam Sridhar Sagar <sagar.beesamsridhar@amd.com>

The topology 'type' value received from firmware is stored without
validation and later used as an index into the clk_topology[]
function-pointer table and clk_type_postfix[] string table in
zynqmp_register_clk_topology().

CLK_TOPOLOGY_TYPE is a 4-bit field, allowing values in the range
0..15, while enum topology_type and the corresponding tables contain
only 7 entries. Values outside the valid range can therefore lead to
out-of-bounds accesses, including a function-pointer dereference.

Validate the topology type while parsing the firmware response and
return an error when an invalid value is detected.

Fixes: 3fde0e16d016 ("drivers: clk: Add ZynqMP clock driver")
Signed-off-by: Beesam Sridhar Sagar <sagar.beesamsridhar@amd.com>
Signed-off-by: Radhey Shyam Pandey <radhey.shyam.pandey@amd.com>
---
 drivers/clk/zynqmp/clkc.c | 4 ++++
 1 file changed, 4 insertions(+)

diff --git a/drivers/clk/zynqmp/clkc.c b/drivers/clk/zynqmp/clkc.c
index 5d827b2bb0ad..4ce376023d7f 100644
--- a/drivers/clk/zynqmp/clkc.c
+++ b/drivers/clk/zynqmp/clkc.c
@@ -418,6 +418,8 @@ static int __zynqmp_clock_get_topology(struct clock_topology *topology,
 		type = FIELD_GET(CLK_TOPOLOGY_TYPE, response->topology[i]);
 		if (type == TYPE_INVALID)
 			return END_OF_TOPOLOGY_NODE;
+		if (type >= ARRAY_SIZE(clk_topology))
+			return -EINVAL;
 		topology[*nnodes].type = type;
 		topology[*nnodes].flag = FIELD_GET(CLK_TOPOLOGY_FLAGS,
 						   response->topology[i]);
@@ -459,6 +461,8 @@ static int zynqmp_clock_get_topology(u32 clk_id,
 						  num_nodes);
 		if (ret == END_OF_TOPOLOGY_NODE)
 			return 0;
+		if (ret < 0)
+			return ret;
 	}
 
 	return 0;
-- 
2.43.0


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

* Re: [PATCH 3/3] clk: zynqmp: Fix unchecked type index into clock topology tables
  2026-12-01  6:43 ` [PATCH 3/3] clk: zynqmp: Fix unchecked type index into clock topology tables Radhey Shyam Pandey
@ 2026-12-01  6:54   ` sashiko-bot
  0 siblings, 0 replies; 5+ messages in thread
From: sashiko-bot @ 2026-12-01  6:54 UTC (permalink / raw)
  To: Radhey Shyam Pandey; +Cc: linux-clk

Thank you for your contribution! Sashiko AI review found 1 potential
issue to consider:
- [Medium] clk: zynqmp: stale num_nodes on error in
  zynqmp_clock_get_topology()
--

commit 77850bd46c12a2a7c44b3c382e25533305881a70
Author: Beesam Sridhar Sagar <sagar.beesamsridhar@amd.com>

clk: zynqmp: Fix unchecked type index into clock topology tables

This commit validates the clock topology type returned by firmware against
the size of clk_topology[] and propagates the error from
__zynqmp_clock_get_topology().

> diff --git a/drivers/clk/zynqmp/clkc.c b/drivers/clk/zynqmp/clkc.c
> index 5d827b2bb0ad3..4ce376023d7fd 100644
> --- a/drivers/clk/zynqmp/clkc.c
> +++ b/drivers/clk/zynqmp/clkc.c
> @@ -418,6 +418,8 @@ static int __zynqmp_clock_get_topology(struct clock_topology *topology,
>  		type = FIELD_GET(CLK_TOPOLOGY_TYPE, response->topology[i]);
>  		if (type == TYPE_INVALID)
>  			return END_OF_TOPOLOGY_NODE;
> +		if (type >= ARRAY_SIZE(clk_topology))
> +			return -EINVAL;
>  		topology[*nnodes].type = type;
>  		topology[*nnodes].flag = FIELD_GET(CLK_TOPOLOGY_FLAGS,
>  						   response->topology[i]);
> @@ -459,6 +461,8 @@ static int zynqmp_clock_get_topology(u32 clk_id,
>  						  num_nodes);
>  		if (ret == END_OF_TOPOLOGY_NODE)
>  			return 0;
> +		if (ret < 0)
> +			return ret;
>  	}
>  
>  	return 0;

[Severity: Medium]
Does returning an error here without resetting *num_nodes to 0 leave a
partially populated topology when an invalid type appears after one or more
valid nodes?

In zynqmp_get_clock_info(), a failure from zynqmp_clock_get_topology()
skips zynqmp_clock_get_parents(), leaving clock[i].num_nodes > 0 and
clock[i].num_parents == 0 while the clock remains valid:

drivers/clk/zynqmp/clkc.c:zynqmp_get_clock_info() {
    ...
		ret = zynqmp_clock_get_topology(i, clock[i].node,
						&clock[i].num_nodes);
		if (ret)
			continue;

		ret = zynqmp_clock_get_parents(i, clock[i].parent,
					       &clock[i].num_parents);
    ...
}

zynqmp_register_clocks() then calls zynqmp_register_clk_topology() on the
partial nodes with total_parents == 0 and an untouched parent_names[]
array.  Callbacks such as zynqmp_clk_register_fixed_factor() and
zynqmp_clk_register_gate() unconditionally pass parent_names[0] to the
clock framework, dereferencing either an uninitialized stack pointer or the
clk_out[j] buffer freed at the end of the previous clock's
zynqmp_register_clk_topology() call.

Resetting *num_nodes = 0 before returning a negative error from
zynqmp_clock_get_topology() would prevent registering the partial nodes.

-- 
Sashiko AI review · https://sashiko.dev/#/patchset/20261201064353.238312-1-radhey.shyam.pandey@amd.com?part=3

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

end of thread, other threads:[~2026-12-01  6:54 UTC | newest]

Thread overview: 5+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2026-12-01  6:43 [PATCH 0/3] clk: zynqmp: fix out-of-bounds accesses in firmware clock parsing Radhey Shyam Pandey
2026-12-01  6:43 ` [PATCH 1/3] clk: zynqmp: Fix off-by-one loop bound in zynqmp_clock_get_topology() Radhey Shyam Pandey
2026-12-01  6:43 ` [PATCH 2/3] clk: zynqmp: Fix unbounded parent array overflow in clock parent lookup Radhey Shyam Pandey
2026-12-01  6:43 ` [PATCH 3/3] clk: zynqmp: Fix unchecked type index into clock topology tables Radhey Shyam Pandey
2026-12-01  6:54   ` sashiko-bot

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox