Linux-ARM-Kernel Archive on lore.kernel.org
 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; 6+ 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] 6+ 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 13:21   ` Prasanna Kumar T S M
  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, 1 reply; 6+ 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] 6+ 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; 6+ 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] 6+ 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 13:21   ` Prasanna Kumar T S M
  2 siblings, 1 reply; 6+ 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] 6+ messages in thread

* Re: [PATCH 1/3] clk: zynqmp: Fix off-by-one loop bound in zynqmp_clock_get_topology()
  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 13:21   ` Prasanna Kumar T S M
  0 siblings, 0 replies; 6+ messages in thread
From: Prasanna Kumar T S M @ 2026-12-01 13:21 UTC (permalink / raw)
  To: Radhey Shyam Pandey, sboyd, bmasney+clk, jbrunet+clk,
	michal.simek, shubhrajyoti.datta
  Cc: linux-clk, linux-arm-kernel, git, Beesam Sridhar Sagar



On 01-12-2026 12:13, Radhey Shyam Pandey wrote:
> 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)

Looks good to me.

Reviewed-by: Prasanna Kumar T S M <ptsm@linux.microsoft.com>


^ permalink raw reply	[flat|nested] 6+ 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 13:21   ` Prasanna Kumar T S M
  0 siblings, 0 replies; 6+ messages in thread
From: Prasanna Kumar T S M @ 2026-12-01 13:21 UTC (permalink / raw)
  To: Radhey Shyam Pandey, sboyd, bmasney+clk, jbrunet+clk,
	michal.simek, shubhrajyoti.datta
  Cc: linux-clk, linux-arm-kernel, git, Beesam Sridhar Sagar



On 01-12-2026 12:13, Radhey Shyam Pandey wrote:
> 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;

Looks good.

Reviewed-by: Prasanna Kumar T S M <ptsm@linux.microsoft.com>


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

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

Thread overview: 6+ 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 13:21   ` Prasanna Kumar T S M
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 13:21   ` Prasanna Kumar T S M

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