Skip to contentSkip to conversation

i2c: dev: Fix memory leak when underlying adapter does not support I2C

PATCH Superseded Igor Pylypiv 2 participants 3 messages Nov 7, 2024 – Nov 8, 2024

Subsystems (2)

v1 of 2 Superseded Latest is v2 →

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 i2c: dev: Fix memory leak when underlying adapter does not support I2C

i2cdev_ioctl_rdwr() receives a buffer which is allocated by the caller.

Fixes 97ca843f6ad3 ("i2c: dev: Check for I2C_FUNC_I2C before calling i2c_transfer")
---
 drivers/i2c/i2c-dev.c | 4 +++-
 1 file changed, 3 insertions(+), 1 deletion(-)
drivers/i2c/i2c-dev.c +3 -1
index 61f7c4003d2f..5d15519ef737 100644
@@ -247,8 +247,10 @@ static noinline int i2cdev_ioctl_rdwr(struct i2c_client *client,
 	int i, res;
 
 	/* Adapter must support I2C transfers */
-	if (!i2c_check_functionality(client->adapter, I2C_FUNC_I2C))
+	if (!i2c_check_functionality(client->adapter, I2C_FUNC_I2C)) {
+		kfree(msgs);
 		return -EOPNOTSUPP;
+	}
 
 	data_ptrs = kmalloc_array(nmsgs, sizeof(u8 __user *), GFP_KERNEL);
 	if (data_ptrs == NULL) {
Signature · 1 line
2.47.0.277.g8800431eea-goog
maintainer
#1 lore raw

Hi Igor,

that's a good catch, but...

Quoted 1 line · Igor Pylypiv

i2cdev_ioctl_rdwr() receives a buffer which is allocated by the caller.

This needs to be a bit re-written. In the commit log you should describe what the patch does. You are telling where the buffer is allocated.

Quoted 11 lines
Fixes 97ca843f6ad3 ("i2c: dev: Check for I2C_FUNC_I2C before calling i2c_transfer")
---
 drivers/i2c/i2c-dev.c | 4 +++-
 1 file changed, 3 insertions(+), 1 deletion(-)
drivers/i2c/i2c-dev.c +0 -0
index 61f7c4003d2f..5d15519ef737 100644

drivers/i2c/i2c-dev.c static noinline int i2cdev_ioctl_rdwr(struct i2c_client *client, · line 252

Quoted 7 lines
drivers/i2c/i2c-dev.c +2 -1
@@ -247,8 +247,10 @@ static noinline int i2cdev_ioctl_rdwr(struct i2c_client *client,
 	int i, res;
 
 	/* Adapter must support I2C transfers */
-	if (!i2c_check_functionality(client->adapter, I2C_FUNC_I2C))
+	if (!i2c_check_functionality(client->adapter, I2C_FUNC_I2C)) {
+		kfree(msgs);
 		return -EOPNOTSUPP;

Please, don't free it here, free it where it has been allocated, i.e. in compat_i2cdev_ioctl().

Andi

Quoted 7 lines
drivers/i2c/i2c-dev.c +1 -0
+	}
 
 	data_ptrs = kmalloc_array(nmsgs, sizeof(u8 __user *), GFP_KERNEL);
 	if (data_ptrs == NULL) {
Signature · 1 line
2.47.0.277.g8800431eea-goog
#2 lore raw
Quoted 10 lines · Andi Shyti

Hi Igor,

that's a good catch, but...

Quoted 1 line · Igor Pylypiv

i2cdev_ioctl_rdwr() receives a buffer which is allocated by the caller.

This needs to be a bit re-written. In the commit log you should describe what the patch does. You are telling where the buffer is allocated.

I thought subject line covered what the patch does. Ack. I'll update the commit message in v2.

Quoted 14 lines
Quoted 14 lines
Fixes 97ca843f6ad3 ("i2c: dev: Check for I2C_FUNC_I2C before calling i2c_transfer")
---
 drivers/i2c/i2c-dev.c | 4 +++-
 1 file changed, 3 insertions(+), 1 deletion(-)
drivers/i2c/i2c-dev.c +0 -0
index 61f7c4003d2f..5d15519ef737 100644
@@ -247,8 +247,10 @@ static noinline int i2cdev_ioctl_rdwr(struct i2c_client *client,
 	int i, res;
 
Quoted 9 lines

drivers/i2c/i2c-dev.c static noinline int i2cdev_ioctl_rdwr(struct i2c_client *client, · line 252

Quoted 5 lines
drivers/i2c/i2c-dev.c +2 -1
 	/* Adapter must support I2C transfers */
-	if (!i2c_check_functionality(client->adapter, I2C_FUNC_I2C))
+	if (!i2c_check_functionality(client->adapter, I2C_FUNC_I2C)) {
+		kfree(msgs);
 		return -EOPNOTSUPP;

Please, don't free it here, free it where it has been allocated, i.e. in compat_i2cdev_ioctl().

Sounds good. I'll move kfree() in v2.

Thanks,
Igor

Quoted 9 lines

Andi

Quoted 7 lines
drivers/i2c/i2c-dev.c +1 -0
+	}
 
 	data_ptrs = kmalloc_array(nmsgs, sizeof(u8 __user *), GFP_KERNEL);
 	if (data_ptrs == NULL) {
Signature · 1 line
2.47.0.277.g8800431eea-goog