clk: bcm: rpi: Fix slab-out-of-bounds during raspberrypi_discover_clocks.
Subsystems (2)
PATCH 1/1 clk: bcm: rpi: Fix slab-out-of-bounds during raspberrypi_discover_clocks.
From: Peng Wei <[email protected]>
There're two potential bugs here:
1) RPI_FIRMWARE_EMMC_CLK_ID the first element of enum rpi_firmware_clk_id is set to be 1.So RPI_FIRMWARE_NUM_CLK_ID will be bigger than the total number of the enum rpi_firmware_clk_id.It maybe cause overflow bug when accessing the array of clks.Besides it will cost more memory when alloc memory for clks.
2) When iterating the clks,there's no way to break the loop until any error is returned.
So change the RPI_FIRMWARE_EMMC_CLK_ID from 1 to 0.And then tell the loop the way to break.
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 ...
--- drivers/clk/bcm/clk-raspberrypi.c | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-)
drivers/clk/bcm/clk-raspberrypi.c +3 -2
@@ -19,7 +19,7 @@#include <soc/bcm2835/raspberrypi-firmware.h> enum rpi_firmware_clk_id { -RPI_FIRMWARE_EMMC_CLK_ID =1 , +RPI_FIRMWARE_EMMC_CLK_ID =0 ,RPI_FIRMWARE_UART_CLK_ID ,RPI_FIRMWARE_ARM_CLK_ID ,RPI_FIRMWARE_CORE_CLK_ID ,
@@ -252,6 +252,7 @@ static int raspberrypi_discover_clocks(struct raspberrypi_clk *rpi, {struct rpi_firmware_get_clocks_response *clks;int ret; +int count =0 ; clks = devm_kcalloc(rpi->dev, sizeof(*clks), RPI_FIRMWARE_NUM_CLK_ID,
@@ -265,7 +266,7 @@ static int raspberrypi_discover_clocks(struct raspberrypi_clk *rpi,if (ret)return ret; -while (clks->id) { +while (count++ <RPI_FIRMWARE_NUM_CLK_ID && clks->id) {struct clk_hw *hw;switch (clks->id) {
Signature · 1 line
2.25.1
Stephen Boyd
maintainerQuoting [email protected] (2022-02-25 09:14:53)
Quoted 5 lines
From: Peng Wei <[email protected]>
There're two potential bugs here:
1)RPI_FIRMWARE_EMMC_CLK_IDthe first element of enumrpi_firmware_clk_idis set to be 1.SoRPI_FIRMWARE_NUM_CLK_IDwill be bigger than the total
s/1\.So/1 so/
Quoted 1 line
number of the enum rpi_firmware_clk_id.It maybe cause overflow bug when
s/clk_id\.It/clk_id. It/
Quoted 1 line
accessing the array of clks.Besides it will cost more memory when alloc
You get the idea.
Quoted 4 lines
memory for clks.
2) When iterating the clks,there's no way to break the loop until any error is returned.
So change theRPI_FIRMWARE_EMMC_CLK_IDfrom 1 to 0.And then tell the loop
s/0\.And/0 and/
Quoted 15 lines
the way to break.
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 ...
Quoted 1 line
Signed-off-by Peng Wei <[email protected]>
Please add a Fixes tag.
Quoted 26 lines
--- drivers/clk/bcm/clk-raspberrypi.c | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-)drivers/clk/bcm/clk-raspberrypi.c +2
-1@@ -19,7 +19,7 @@#include <soc/bcm2835/raspberrypi-firmware.h> enum rpi_firmware_clk_id { -RPI_FIRMWARE_EMMC_CLK_ID =1 , +RPI_FIRMWARE_EMMC_CLK_ID =0 ,RPI_FIRMWARE_UART_CLK_ID ,RPI_FIRMWARE_ARM_CLK_ID ,RPI_FIRMWARE_CORE_CLK_ID ,@@ -252,6 +252,7 @@ static int raspberrypi_discover_clocks(struct raspberrypi_clk *rpi, {struct rpi_firmware_get_clocks_response *clks;int ret; +int count =0 ; clks = devm_kcalloc(rpi->dev, sizeof(*clks), RPI_FIRMWARE_NUM_CLK_ID,
drivers/clk/bcm/clk-raspberrypi.c static int raspberrypi_discover_clocks(struct raspberrypi_clk *rpi, · line 269
Quoted 5 lines
drivers/clk/bcm/clk-raspberrypi.c +1
-1@@ -265,7 +266,7 @@ static int raspberrypi_discover_clocks(struct raspberrypi_clk *rpi,if (ret)return ret; -while (clks->id) { +while (count++ <RPI_FIRMWARE_NUM_CLK_ID && clks->id) {
Use a for loop please.
Quoted 6 lines
drivers/clk/bcm/clk-raspberrypi.c +0
-0struct clk_hw *hw;switch (clks->id) {Signature · 1 line
2.25.1