Skip to contentSkip to conversation

i2c: dev: zero out array used for i2c reads from userspace

PATCH In review Greg Kroah-Hartman 2 participants 3 messages Jul 29, 2021 – Aug 11, 2021

Subsystems (3)
lore
Share & tools
lore

Greg Kroah-Hartman

author maintainer
#0 lore raw

PATCH v2 1/1 i2c: dev: zero out array used for i2c reads from userspace

If an i2c driver happens to not provide the full amount of data that a user asks for, it is possible that some uninitialized data could be sent to userspace. While all in-kernel drivers look to be safe, just be sure by initializing the buffer to zero before it is passed to the i2c driver so that any future drivers will not have this issue.

Also properly copy the amount of data recvieved to the userspace buffer, as pointed out by Dan Carpenter.

---
v2: Add copy_to_user() change as pointed out by Dan

 drivers/i2c/i2c-dev.c | 5 +++--
 1 file changed, 3 insertions(+), 2 deletions(-)
drivers/i2c/i2c-dev.c +3 -2
index cb64fe649390..77f576e51652 100644
@@ -141,7 +141,7 @@ static ssize_t i2cdev_read(struct file *file, char __user *buf, size_t count,
 	if (count > 8192)
 		count = 8192;
 
-	tmp = kmalloc(count, GFP_KERNEL);
+	tmp = kzalloc(count, GFP_KERNEL);
 	if (tmp == NULL)
 		return -ENOMEM;
 
@@ -150,7 +150,8 @@ static ssize_t i2cdev_read(struct file *file, char __user *buf, size_t count,
 
 	ret = i2c_master_recv(client, tmp, count);
 	if (ret >= 0)
-		ret = copy_to_user(buf, tmp, count) ? -EFAULT : ret;
+		if (copy_to_user(buf, tmp, ret))
+			ret = -EFAULT;
 	kfree(tmp);
 	return ret;
 }
Signature · 1 line
2.32.0
#1 lore raw
Quoted 6 lines · Greg Kroah-Hartman

If an i2c driver happens to not provide the full amount of data that a user asks for, it is possible that some uninitialized data could be sent to userspace. While all in-kernel drivers look to be safe, just be sure by initializing the buffer to zero before it is passed to the i2c driver so that any future drivers will not have this issue.

Quoted 7 lines

Also properly copy the amount of data recvieved to the userspace buffer, as pointed out by Dan Carpenter.

Fixed checkpatch warning "WARNING: Invalid email format for stable:
'stable <[email protected]>', prefer '[email protected]' " and applied to for-current, thanks!

author maintainer
#2 lore raw
Quoted 10 lines · Wolfram Sang
Quoted 9 lines · Greg Kroah-Hartman

If an i2c driver happens to not provide the full amount of data that a user asks for, it is possible that some uninitialized data could be sent to userspace. While all in-kernel drivers look to be safe, just be sure by initializing the buffer to zero before it is passed to the i2c driver so that any future drivers will not have this issue.

Also properly copy the amount of data recvieved to the userspace buffer, as pointed out by Dan Carpenter.

Quoted 8 lines
Quoted 4 lines

Fixed checkpatch warning "WARNING: Invalid email format for stable:
'stable <[email protected]>', prefer '[email protected]' " and applied to for-current, thanks!

That is a crazy warning, never even knew it was there. But as the stable maintainer, it does not look correct as both are just fine...

Anyway, thanks for taking the patch!

greg k-h