Skip to contentSkip to conversation

clk: visconti: prevent array overflow in visconti_clk_register_gates()

PATCH Applied Dan Carpenter 3 participants 3 messages Mar 16, 2022 – Mar 17, 2022

Subsystems (3)
lore
Share & tools
lore
#0 lore raw

PATCH 1/1 clk: visconti: prevent array overflow in visconti_clk_register_gates()

This code was using -1 to represent that there was no reset function. Unfortunately, the -1 was stored in u8 so the if (clks[i].rs_id >= 0) condition was always true. This lead to an out of bounds access in visconti_clk_register_gates().

Fixes b4cbe606dc36 ("clk: visconti: Add support common clock driver and reset driver")
---
 drivers/clk/visconti/clkc.h          | 3 +++
 drivers/clk/visconti/clkc-tmpv770x.c | 2 +-
 drivers/clk/visconti/clkc.c          | 2 +-
 3 files changed, 5 insertions(+), 2 deletions(-)
drivers/clk/visconti/clkc.h +3 -0
index 09ed82ff64e4..8756a1ec42ef 100644
@@ -73,4 +73,7 @@ int visconti_clk_register_gates(struct visconti_clk_provider *data,
 				 int num_gate,
 				 const struct visconti_reset_data *reset,
 				 spinlock_t *lock);
+
+#define NO_RESET 0xFF
+
 #endif /* _VISCONTI_CLKC_H_ */
drivers/clk/visconti/clkc-tmpv770x.c +1 -1
index c2b2f41a85a4..6c753b2cb558 100644
@@ -176,7 +176,7 @@ static const struct visconti_clk_gate_table clk_gate_tables[] = {
 	{ TMPV770X_CLK_WRCK, "wrck",
 		clks_parent_data, ARRAY_SIZE(clks_parent_data),
 		0, 0x68, 0x168, 9, 32,
-		-1, }, /* No reset */
+		NO_RESET, },
 	{ TMPV770X_CLK_PICKMON, "pickmon",
 		clks_parent_data, ARRAY_SIZE(clks_parent_data),
 		0, 0x10, 0x110, 8, 4,
drivers/clk/visconti/clkc.c +1 -1
index 56a8a4ffebca..d0b193b5d0b3 100644
@@ -147,7 +147,7 @@ int visconti_clk_register_gates(struct visconti_clk_provider *ctx,
 		if (!dev_name)
 			return -ENOMEM;
 
-		if (clks[i].rs_id >= 0) {
+		if (clks[i].rs_id != NO_RESET) {
 			rson_offset = reset[clks[i].rs_id].rson_offset;
 			rsoff_offset = reset[clks[i].rs_id].rsoff_offset;
 			rs_idx = reset[clks[i].rs_id].rs_idx;
Signature · 1 line
2.20.1
Acked-by on v1 1/1
#1 lore raw

Hi Dan,

Thanks for your patch.
And I forgot to update and fix on the report you sent me before, sorry.

Quoted 10 lines

-----Original Message-----
From: Dan Carpenter <[email protected]>
Sent: Wednesday, March 16, 2022 5:36 PM
To: Michael Turquette <[email protected]>; iwamatsu nobuhiro(岩松 信洋 □SWC◯ACT) <[email protected]>
Cc: Stephen Boyd <[email protected]>; [email protected]; [email protected]
Subject: [PATCH] clk: visconti: prevent array overflow in visconti_clk_register_gates()

Quoted 8 lines

This code was using -1 to represent that there was no reset function. Unfortunately, the -1 was stored in u8 so the if (clks[i].rs_id >= 0) condition was always true. This lead to an out of bounds access in visconti_clk_register_gates().

Fixes b4cbe606dc36 ("clk: visconti: Add support common clock driver and

reset driver")

Looks good to me.

Best regards,
Nobuhiro

Quoted 50 lines
---
 drivers/clk/visconti/clkc.h          | 3 +++
 drivers/clk/visconti/clkc-tmpv770x.c | 2 +-
 drivers/clk/visconti/clkc.c          | 2 +-
 3 files changed, 5 insertions(+), 2 deletions(-)
drivers/clk/visconti/clkc.h index renamed from drivers/clk/visconti/clkc.h +0 -0

09ed82ff64e4..8756a1ec42ef 100644

drivers/clk/visconti/clkc.h +3 -0
@@ -73,4 +73,7 @@ int visconti_clk_register_gates(struct
 visconti_clk_provider *data,
 				 int num_gate,
 				 const struct visconti_reset_data *reset,
 				 spinlock_t *lock);
+
+#define NO_RESET 0xFF
+

#endif /* _VISCONTI_CLKC_H_ */

drivers/clk/visconti/clkc-tmpv770x.c +1 -1
index c2b2f41a85a4..6c753b2cb558 100644
@@ -176,7 +176,7 @@ static const struct visconti_clk_gate_table
 clk_gate_tables[] = {
 	{ TMPV770X_CLK_WRCK, "wrck",
 		clks_parent_data, ARRAY_SIZE(clks_parent_data),
 		0, 0x68, 0x168, 9, 32,
-		-1, }, /* No reset */
+		NO_RESET, },
 	{ TMPV770X_CLK_PICKMON, "pickmon",
 		clks_parent_data, ARRAY_SIZE(clks_parent_data),
drivers/clk/visconti/clkc-tmpv770x.c +0 -0
 		0, 0x10, 0x110, 8, 4,
drivers/clk/visconti/clkc.c index renamed from drivers/clk/visconti/clkc.c +0 -0

56a8a4ffebca..d0b193b5d0b3 100644

drivers/clk/visconti/clkc.c +1 -1
@@ -147,7 +147,7 @@ int visconti_clk_register_gates(struct
 visconti_clk_provider *ctx,
 		if (!dev_name)
 			return -ENOMEM;
 
-		if (clks[i].rs_id >= 0) {
+		if (clks[i].rs_id != NO_RESET) {
 			rson_offset = reset[clks[i].rs_id].rson_offset;
 			rsoff_offset = reset[clks[i].rs_id].rsoff_offset;

rs_idx = reset[clks[i].rs_id].rs_idx;
--
2.20.1

maintainer Applied
#2 lore raw
In reply to Dan Carpenter

Quoting Dan Carpenter (2022-03-16 01:35:33)

Quoted 8 lines

This code was using -1 to represent that there was no reset function. Unfortunately, the -1 was stored in u8 so the if (clks[i].rs_id >= 0) condition was always true. This lead to an out of bounds access in visconti_clk_register_gates().

Fixes b4cbe606dc36 ("clk: visconti: Add support common clock driver and reset driver")

---

Applied to clk-next