[PATCH] rtc: pcf85063: fix potential OOB write in PCF85063 NVMEM read

Ahmad Fatoum posted 1 patch 12 months ago
drivers/rtc/rtc-pcf85063.c | 11 ++++++++++-
1 file changed, 10 insertions(+), 1 deletion(-)
[PATCH] rtc: pcf85063: fix potential OOB write in PCF85063 NVMEM read
Posted by Ahmad Fatoum 12 months ago
From: Oleksij Rempel <o.rempel@pengutronix.de>

The nvmem interface supports variable buffer sizes, while the regmap
interface operates with fixed-size storage. If an nvmem client uses a
buffer size less than 4 bytes, regmap_read will write out of bounds
as it expects the buffer to point at an unsigned int.

Fix this by using an intermediary unsigned int to hold the value.

Fixes: fadfd092ee91 ("rtc: pcf85063: add nvram support")
Signed-off-by: Oleksij Rempel <o.rempel@pengutronix.de>
Signed-off-by: Ahmad Fatoum <a.fatoum@pengutronix.de>
---
 drivers/rtc/rtc-pcf85063.c | 11 ++++++++++-
 1 file changed, 10 insertions(+), 1 deletion(-)

diff --git a/drivers/rtc/rtc-pcf85063.c b/drivers/rtc/rtc-pcf85063.c
index fdbc07f14036aff0399c15adf71d89a30fe7e2cb..905986c616559b87826d1102dbc25614f7dcbcbb 100644
--- a/drivers/rtc/rtc-pcf85063.c
+++ b/drivers/rtc/rtc-pcf85063.c
@@ -322,7 +322,16 @@ static const struct rtc_class_ops pcf85063_rtc_ops = {
 static int pcf85063_nvmem_read(void *priv, unsigned int offset,
 			       void *val, size_t bytes)
 {
-	return regmap_read(priv, PCF85063_REG_RAM, val);
+	unsigned int tmp;
+	int ret;
+
+	ret = regmap_read(priv, PCF85063_REG_RAM, &tmp);
+	if (ret < 0)
+		return ret;
+
+	*(u8 *)val = tmp;
+
+	return 0;
 }
 
 static int pcf85063_nvmem_write(void *priv, unsigned int offset,

---
base-commit: 78d4f34e2115b517bcbfe7ec0d018bbbb6f9b0b8
change-id: 20241218-rtc-pcf85063-stack-corruption-9f0f401fd227

Best regards,
-- 
Ahmad Fatoum <a.fatoum@pengutronix.de>
Re: [PATCH] rtc: pcf85063: fix potential OOB write in PCF85063 NVMEM read
Posted by Alexandre Belloni 11 months, 1 week ago
On Wed, 18 Dec 2024 20:34:58 +0100, Ahmad Fatoum wrote:
> The nvmem interface supports variable buffer sizes, while the regmap
> interface operates with fixed-size storage. If an nvmem client uses a
> buffer size less than 4 bytes, regmap_read will write out of bounds
> as it expects the buffer to point at an unsigned int.
> 
> Fix this by using an intermediary unsigned int to hold the value.
> 
> [...]

Applied, thanks!

[1/1] rtc: pcf85063: fix potential OOB write in PCF85063 NVMEM read
      https://git.kernel.org/abelloni/c/3ab8c5ed4f84

Best regards,

-- 
Alexandre Belloni, co-owner and COO, Bootlin
Embedded Linux and Kernel engineering
https://bootlin.com