diff mbox series

[1/3] clk: bcm: rpi: Prevent out-of-bounds access

Message ID 20220713154953.3336-2-stefan.wahren@i2se.com (mailing list archive)
State New, archived
Headers show
Series clk: bcm: rpi: Fixes and improvement | expand

Commit Message

Stefan Wahren July 13, 2022, 3:49 p.m. UTC
The while loop in raspberrypi_discover_clocks() relies on the assumption
that the id of the last clock element is zero. Because this data comes
from the Videocore firmware and it doesn't guarantuee such a behavior
this could lead to out-of-bounds access. So fix this by providing
a sentinel element.

Fixes: 93d2725affd6 ("clk: bcm: rpi: Discover the firmware clocks")
Link: https://github.com/raspberrypi/firmware/issues/1688
Suggested-by: Phil Elwell <phil@raspberrypi.com>
Signed-off-by: Stefan Wahren <stefan.wahren@i2se.com>
---
 drivers/clk/bcm/clk-raspberrypi.c | 7 ++++++-
 1 file changed, 6 insertions(+), 1 deletion(-)

Comments

Florian Fainelli July 13, 2022, 4:25 p.m. UTC | #1
On 7/13/22 08:49, Stefan Wahren wrote:
> The while loop in raspberrypi_discover_clocks() relies on the assumption
> that the id of the last clock element is zero. Because this data comes
> from the Videocore firmware and it doesn't guarantuee such a behavior
> this could lead to out-of-bounds access. So fix this by providing
> a sentinel element.
> 
> Fixes: 93d2725affd6 ("clk: bcm: rpi: Discover the firmware clocks")
> Link: https://github.com/raspberrypi/firmware/issues/1688
> Suggested-by: Phil Elwell <phil@raspberrypi.com>
> Signed-off-by: Stefan Wahren <stefan.wahren@i2se.com>

Acked-by: Florian Fainelli <f.fainelli@gmail.com>
Stephen Boyd Aug. 23, 2022, 10:52 p.m. UTC | #2
Quoting Stefan Wahren (2022-07-13 08:49:51)
> The while loop in raspberrypi_discover_clocks() relies on the assumption
> that the id of the last clock element is zero. Because this data comes
> from the Videocore firmware and it doesn't guarantuee such a behavior
> this could lead to out-of-bounds access. So fix this by providing
> a sentinel element.
> 
> Fixes: 93d2725affd6 ("clk: bcm: rpi: Discover the firmware clocks")
> Link: https://github.com/raspberrypi/firmware/issues/1688
> Suggested-by: Phil Elwell <phil@raspberrypi.com>
> Signed-off-by: Stefan Wahren <stefan.wahren@i2se.com>
> ---

Applied to clk-fixes
diff mbox series

Patch

diff --git a/drivers/clk/bcm/clk-raspberrypi.c b/drivers/clk/bcm/clk-raspberrypi.c
index 73518009a0f2..79cbf0c0b401 100644
--- a/drivers/clk/bcm/clk-raspberrypi.c
+++ b/drivers/clk/bcm/clk-raspberrypi.c
@@ -344,8 +344,13 @@  static int raspberrypi_discover_clocks(struct raspberrypi_clk *rpi,
 	struct rpi_firmware_get_clocks_response *clks;
 	int ret;
 
+	/*
+	 * The firmware doesn't guarantee that the last element of
+	 * RPI_FIRMWARE_GET_CLOCKS is zeroed. So allocate an additional
+	 * zero element as sentinel.
+	 */
 	clks = devm_kcalloc(rpi->dev,
-			    RPI_FIRMWARE_NUM_CLK_ID, sizeof(*clks),
+			    RPI_FIRMWARE_NUM_CLK_ID + 1, sizeof(*clks),
 			    GFP_KERNEL);
 	if (!clks)
 		return -ENOMEM;