[PATCH 2/3] clk: zynqmp: Fix unbounded parent array overflow in clock parent lookup

Radhey Shyam Pandey radhey.shyam.pandey at amd.com
Mon Nov 30 22:43:52 PST 2026


From: Beesam Sridhar Sagar <sagar.beesamsridhar at 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 at amd.com>
Signed-off-by: Radhey Shyam Pandey <radhey.shyam.pandey at 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




More information about the linux-arm-kernel mailing list