Skip to content

Commit 5556876

Browse files
ipylypivgregkh
authored andcommitted
i2c: dev: Fix memory leak when underlying adapter does not support I2C
[ Upstream commit 48730a9 ] Early return in i2cdev_ioctl_rdwr() failed to free the memory allocated by the caller. Move freeing the memory to the function where it has been allocated to prevent similar leaks in the future. Fixes: 97ca843 ("i2c: dev: Check for I2C_FUNC_I2C before calling i2c_transfer") Signed-off-by: Igor Pylypiv <[email protected]> [wsa: replaced '== NULL' with '!'] Signed-off-by: Wolfram Sang <[email protected]> Signed-off-by: Sasha Levin <[email protected]>
1 parent 57860a8 commit 5556876

File tree

1 file changed

+9
-8
lines changed

1 file changed

+9
-8
lines changed

drivers/i2c/i2c-dev.c

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -251,10 +251,8 @@ static noinline int i2cdev_ioctl_rdwr(struct i2c_client *client,
251251
return -EOPNOTSUPP;
252252

253253
data_ptrs = kmalloc_array(nmsgs, sizeof(u8 __user *), GFP_KERNEL);
254-
if (data_ptrs == NULL) {
255-
kfree(msgs);
254+
if (!data_ptrs)
256255
return -ENOMEM;
257-
}
258256

259257
res = 0;
260258
for (i = 0; i < nmsgs; i++) {
@@ -302,7 +300,6 @@ static noinline int i2cdev_ioctl_rdwr(struct i2c_client *client,
302300
for (j = 0; j < i; ++j)
303301
kfree(msgs[j].buf);
304302
kfree(data_ptrs);
305-
kfree(msgs);
306303
return res;
307304
}
308305

@@ -316,7 +313,6 @@ static noinline int i2cdev_ioctl_rdwr(struct i2c_client *client,
316313
kfree(msgs[i].buf);
317314
}
318315
kfree(data_ptrs);
319-
kfree(msgs);
320316
return res;
321317
}
322318

@@ -446,6 +442,7 @@ static long i2cdev_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
446442
case I2C_RDWR: {
447443
struct i2c_rdwr_ioctl_data rdwr_arg;
448444
struct i2c_msg *rdwr_pa;
445+
int res;
449446

450447
if (copy_from_user(&rdwr_arg,
451448
(struct i2c_rdwr_ioctl_data __user *)arg,
@@ -467,7 +464,9 @@ static long i2cdev_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
467464
if (IS_ERR(rdwr_pa))
468465
return PTR_ERR(rdwr_pa);
469466

470-
return i2cdev_ioctl_rdwr(client, rdwr_arg.nmsgs, rdwr_pa);
467+
res = i2cdev_ioctl_rdwr(client, rdwr_arg.nmsgs, rdwr_pa);
468+
kfree(rdwr_pa);
469+
return res;
471470
}
472471

473472
case I2C_SMBUS: {
@@ -540,7 +539,7 @@ static long compat_i2cdev_ioctl(struct file *file, unsigned int cmd, unsigned lo
540539
struct i2c_rdwr_ioctl_data32 rdwr_arg;
541540
struct i2c_msg32 __user *p;
542541
struct i2c_msg *rdwr_pa;
543-
int i;
542+
int i, res;
544543

545544
if (copy_from_user(&rdwr_arg,
546545
(struct i2c_rdwr_ioctl_data32 __user *)arg,
@@ -573,7 +572,9 @@ static long compat_i2cdev_ioctl(struct file *file, unsigned int cmd, unsigned lo
573572
};
574573
}
575574

576-
return i2cdev_ioctl_rdwr(client, rdwr_arg.nmsgs, rdwr_pa);
575+
res = i2cdev_ioctl_rdwr(client, rdwr_arg.nmsgs, rdwr_pa);
576+
kfree(rdwr_pa);
577+
return res;
577578
}
578579
case I2C_SMBUS: {
579580
struct i2c_smbus_ioctl_data32 data32;

0 commit comments

Comments
 (0)