Skip to contentSkip to conversation

clk: bcm: rpi: Fix slab-out-of-bounds during raspberrypi_discover_clocks.

PATCH In review [email protected] 2 participants 3 messages Mar 12, 2022 – Mar 19, 2022

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

PATCH v2 1/1 clk: bcm: rpi: Fix slab-out-of-bounds during raspberrypi_discover_clocks.

From: Peng Wei <[email protected]>

There's no way to break the loop until any error is returned. It causes overflow bug when accessing the clks.

The KASAN warning:

BUG: KASAN: slab-out-of-bounds in raspberrypi_clk_probe+0x1d8/0x2f8

Read of size 4 at addr ffffff8040bd8704 by task swapper/0/1

Call trace:
 ...
 __asan_load4+0x94/0xd0
 raspberrypi_clk_probe+0x1d8/0x2f8
 platform_probe+0x94/0x110
 ...

v2:
- Improve the comments
- Change to for loop
- Add fixes tag
- Remove the modification about enum rpi_firmware_clk_id

Fixes 93d2725affd6 ("clk: bcm: rpi: Discover the firmware clocks")
---
 drivers/clk/bcm/clk-raspberrypi.c | 14 +++++++-------
 1 file changed, 7 insertions(+), 7 deletions(-)
drivers/clk/bcm/clk-raspberrypi.c +7 -7
index dd3b71eafabf..5f963254d4eb 100644
@@ -250,6 +250,7 @@ static int raspberrypi_discover_clocks(struct raspberrypi_clk *rpi,
 {
 	struct rpi_firmware_get_clocks_response *clks;
 	int ret;
+	int i;
 
 	clks = devm_kcalloc(rpi->dev,
 			    sizeof(*clks), RPI_FIRMWARE_NUM_CLK_ID,
@@ -263,26 +264,25 @@ static int raspberrypi_discover_clocks(struct raspberrypi_clk *rpi,
 	if (ret)
 		return ret;
 
-	while (clks->id) {
+	for (i = 0; i < RPI_FIRMWARE_NUM_CLK_ID; i++) {
 		struct clk_hw *hw;
 
-		switch (clks->id) {
+		switch (clks[i].id) {
 		case RPI_FIRMWARE_ARM_CLK_ID:
 		case RPI_FIRMWARE_CORE_CLK_ID:
 		case RPI_FIRMWARE_M2MC_CLK_ID:
 		case RPI_FIRMWARE_V3D_CLK_ID:
 		case RPI_FIRMWARE_PIXEL_BVB_CLK_ID:
-			hw = raspberrypi_clk_register(rpi, clks->parent,
-						      clks->id);
+			hw = raspberrypi_clk_register(rpi, clks[i].parent,
+						      clks[i].id);
 			if (IS_ERR(hw))
 				return PTR_ERR(hw);
 
-			data->hws[clks->id] = hw;
-			data->num = clks->id + 1;
+			data->hws[clks[i].id] = hw;
+			data->num = clks[i].id + 1;
 			fallthrough;
 
 		default:
-			clks++;
 			break;
 		}
 	}
Signature · 1 line
2.25.1
#1 lore raw

Hi,

Quoted 59 lines · On Sat, Mar 12, 2022 at 11:42:44PM +0800, [email protected] wrote:

From: Peng Wei <[email protected]>

There's no way to break the loop until any error is returned. It causes overflow bug when accessing the clks.

The KASAN warning:

BUG: KASAN: slab-out-of-bounds in raspberrypi_clk_probe+0x1d8/0x2f8

Read of size 4 at addr ffffff8040bd8704 by task swapper/0/1

Call trace:
 ...
 __asan_load4+0x94/0xd0
 raspberrypi_clk_probe+0x1d8/0x2f8
 platform_probe+0x94/0x110
 ...

v2:
- Improve the comments
- Change to for loop
- Add fixes tag
- Remove the modification about enum rpi_firmware_clk_id

Fixes 93d2725affd6 ("clk: bcm: rpi: Discover the firmware clocks")
---
 drivers/clk/bcm/clk-raspberrypi.c | 14 +++++++-------
 1 file changed, 7 insertions(+), 7 deletions(-)
drivers/clk/bcm/clk-raspberrypi.c +3 -4
index dd3b71eafabf..5f963254d4eb 100644
@@ -250,6 +250,7 @@ static int raspberrypi_discover_clocks(struct raspberrypi_clk *rpi,
 {
 	struct rpi_firmware_get_clocks_response *clks;
 	int ret;
+	int i;
 
 	clks = devm_kcalloc(rpi->dev,
 			    sizeof(*clks), RPI_FIRMWARE_NUM_CLK_ID,
@@ -263,26 +264,25 @@ static int raspberrypi_discover_clocks(struct raspberrypi_clk *rpi,
 	if (ret)
 		return ret;
 
-	while (clks->id) {
+	for (i = 0; i < RPI_FIRMWARE_NUM_CLK_ID; i++) {
 		struct clk_hw *hw;
 
-		switch (clks->id) {
+		switch (clks[i].id) {
 		case RPI_FIRMWARE_ARM_CLK_ID:
 		case RPI_FIRMWARE_CORE_CLK_ID:
 		case RPI_FIRMWARE_M2MC_CLK_ID:
 		case RPI_FIRMWARE_V3D_CLK_ID:
 		case RPI_FIRMWARE_PIXEL_BVB_CLK_ID:
-			hw = raspberrypi_clk_register(rpi, clks->parent,
-						      clks->id);

drivers/clk/bcm/clk-raspberrypi.c static int raspberrypi_discover_clocks(struct raspberrypi_clk *rpi, · line 283

Quoted 10 lines
drivers/clk/bcm/clk-raspberrypi.c +4 -2
+			hw = raspberrypi_clk_register(rpi, clks[i].parent,
+						      clks[i].id);
 			if (IS_ERR(hw))
 				return PTR_ERR(hw);
 
-			data->hws[clks->id] = hw;
-			data->num = clks->id + 1;
+			data->hws[clks[i].id] = hw;
+			data->num = clks[i].id + 1;
 			fallthrough;

This will conflict with:
https://lore.kernel.org/linux-clk/20220316083741.5hvuli2cvxlqr2ji@houat/T/#m3fc26816f7de420d74e4aa853eb92017b0b28fff

which should also fix the out-of-bound access?

maxime

author
#2 lore raw
Quoted 43 lines · Maxime Ripard

Hi,

Quoted 40 lines · On Sat, Mar 12, 2022 at 11:42:44PM +0800, [email protected] wrote:

From: Peng Wei <[email protected]>

There's no way to break the loop until any error is returned. It causes overflow bug when accessing the clks.

The KASAN warning:

BUG: KASAN: slab-out-of-bounds in raspberrypi_clk_probe+0x1d8/0x2f8

Read of size 4 at addr ffffff8040bd8704 by task swapper/0/1

Call trace:
  ...
  __asan_load4+0x94/0xd0
  raspberrypi_clk_probe+0x1d8/0x2f8
  platform_probe+0x94/0x110
  ...

v2:
- Improve the comments
- Change to for loop
- Add fixes tag
- Remove the modification about enum rpi_firmware_clk_id

Fixes 93d2725affd6 ("clk: bcm: rpi: Discover the firmware clocks")
---
  drivers/clk/bcm/clk-raspberrypi.c | 14 +++++++-------
  1 file changed, 7 insertions(+), 7 deletions(-)
drivers/clk/bcm/clk-raspberrypi.c +1 -0
index dd3b71eafabf..5f963254d4eb 100644
@@ -250,6 +250,7 @@ static int raspberrypi_discover_clocks(struct raspberrypi_clk *rpi,
  {
  	struct rpi_firmware_get_clocks_response *clks;
  	int ret;
+	int i;
 
Quoted 7 lines

drivers/clk/bcm/clk-raspberrypi.c static int raspberrypi_discover_clocks(struct raspberrypi_clk *rpi, · line 266

Quoted 7 lines
drivers/clk/bcm/clk-raspberrypi.c +0 -1
  	clks = devm_kcalloc(rpi->dev,
  			    sizeof(*clks), RPI_FIRMWARE_NUM_CLK_ID,
@@ -263,26 +264,25 @@ static int raspberrypi_discover_clocks(struct raspberrypi_clk *rpi,
  	if (ret)
  		return ret;
 
-	while (clks->id) {

This line triggers the KASAN warning.

Quoted 26 lines
Quoted 12 lines
drivers/clk/bcm/clk-raspberrypi.c +2 -3
+	for (i = 0; i < RPI_FIRMWARE_NUM_CLK_ID; i++) {
  		struct clk_hw *hw;
 
-		switch (clks->id) {
+		switch (clks[i].id) {
  		case RPI_FIRMWARE_ARM_CLK_ID:
  		case RPI_FIRMWARE_CORE_CLK_ID:
  		case RPI_FIRMWARE_M2MC_CLK_ID:
  		case RPI_FIRMWARE_V3D_CLK_ID:
  		case RPI_FIRMWARE_PIXEL_BVB_CLK_ID:
-			hw = raspberrypi_clk_register(rpi, clks->parent,
-						      clks->id);

drivers/clk/bcm/clk-raspberrypi.c static int raspberrypi_discover_clocks(struct raspberrypi_clk *rpi, · line 283

Quoted 10 lines
drivers/clk/bcm/clk-raspberrypi.c +4 -2
+			hw = raspberrypi_clk_register(rpi, clks[i].parent,
+						      clks[i].id);
  			if (IS_ERR(hw))
  				return PTR_ERR(hw);
 
-			data->hws[clks->id] = hw;
-			data->num = clks->id + 1;
+			data->hws[clks[i].id] = hw;
+			data->num = clks[i].id + 1;
  			fallthrough;

This will conflict with:
https://lore.kernel.org/linux-clk/20220316083741.5hvuli2cvxlqr2ji@houat/T/#m3fc26816f7de420d74e4aa853eb92017b0b28fff

Quoted 3 lines

which should also fix the out-of-bound access?

maxime

Yes it is.