Skip to contentSkip to conversation

i2c: mlxbf: Fix use-after-free in mlxbf_i2c_init_resource()

PATCH Applied Xuanqiang Luo 2 participants 2 messages Jul 14

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

PATCH 1/1 i2c: mlxbf: Fix use-after-free in mlxbf_i2c_init_resource()

From: Xuanqiang Luo <[email protected]>

If devm_platform_get_and_ioremap_resource() returns an error, mlxbf_i2c_init_resource() frees tmp_res before reading tmp_res->io to get the error code. This results in a use-after-free.

Save the error code before freeing tmp_res.

Fixes b5b5b32081cd ("i2c: mlxbf: I2C SMBus driver for Mellanox BlueField SoC")
---
 drivers/i2c/busses/i2c-mlxbf.c | 4 +++-
 1 file changed, 3 insertions(+), 1 deletion(-)
drivers/i2c/busses/i2c-mlxbf.c +3 -1
index 6c1cfe9ec8ac..e33512b25353 100644
@@ -1051,8 +1051,10 @@ static int mlxbf_i2c_init_resource(struct platform_device *pdev,
 
 	tmp_res->io = devm_platform_get_and_ioremap_resource(pdev, type, &tmp_res->params);
 	if (IS_ERR(tmp_res->io)) {
+		int ret = PTR_ERR(tmp_res->io);
+
 		devm_kfree(dev, tmp_res);
-		return PTR_ERR(tmp_res->io);
+		return ret;
 	}
 
 	tmp_res->type = type;
Signature · 1 line
2.51.0
maintainer Applied
#1 lore raw

Hi Xuanqiang,

Quoted 11 lines · Xuanqiang Luo

From: Xuanqiang Luo <[email protected]>

If devm_platform_get_and_ioremap_resource() returns an error, mlxbf_i2c_init_resource() frees tmp_res before reading tmp_res->io to get the error code. This results in a use-after-free.

Save the error code before freeing tmp_res.

Fixes b5b5b32081cd ("i2c: mlxbf: I2C SMBus driver for Mellanox BlueField SoC")

merged to i2c/i2c-fixes.

Thanks,
Andi