Skip to contentSkip to conversation

eeprom: idt_89hpesx: uninitialized data in idt_dbgfs_csr_write()

PATCH In review Dan Carpenter 2 participants 2 messages Jul 8, 2022

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

PATCH v2 1/1 eeprom: idt_89hpesx: uninitialized data in idt_dbgfs_csr_write()

The simple_write_to_buffer() function will return positive/success if it is able to write a single byte anywhere within the buffer. However that potentially leaves a lot of the buffer uninitialized.

In this code it's better to return 0 if the offset is non-zero. This code is not written to support partial writes. And then return -EFAULT if the buffer is not completely initialized.

Fixes cfad6425382e ("eeprom: Add IDT 89HPESx EEPROM/CSR driver")

---
v2: In v1, I tried to be fancy and use strndup_user(). That potentially breaks the user space API. I cannot test this code so it's better to be conservative.

 drivers/misc/eeprom/idt_89hpesx.c | 8 ++++++--
 1 file changed, 6 insertions(+), 2 deletions(-)
drivers/misc/eeprom/idt_89hpesx.c +6 -2
index b0cff4b152da..7f430742ce2b 100644
@@ -909,14 +909,18 @@ static ssize_t idt_dbgfs_csr_write(struct file *filep, const char __user *ubuf,
 	u32 csraddr, csrval;
 	char *buf;
 
+	if (*offp)
+		return 0;
+
 	/* Copy data from User-space */
 	buf = kmalloc(count + 1, GFP_KERNEL);
 	if (!buf)
 		return -ENOMEM;
 
-	ret = simple_write_to_buffer(buf, count, offp, ubuf, count);
-	if (ret < 0)
+	if (copy_from_user(buf, ubuf, count)) {
+		ret = -EFAULT;
 		goto free_buf;
+	}
 	buf[count] = 0;
 
 	/* Find position of colon in the buffer */
Signature · 1 line
2.35.1
Reviewed-by on v2 1/1
#1 lore raw
Quoted 10 lines · Dan Carpenter

The simple_write_to_buffer() function will return positive/success if it is able to write a single byte anywhere within the buffer. However that potentially leaves a lot of the buffer uninitialized.

In this code it's better to return 0 if the offset is non-zero. This code is not written to support partial writes. And then return -EFAULT if the buffer is not completely initialized.

Fixes cfad6425382e ("eeprom: Add IDT 89HPESx EEPROM/CSR driver")

Seems reasonable. Thanks.

-Sergey

Quoted 36 lines

---
v2: In v1, I tried to be fancy and use strndup_user(). That potentially breaks the user space API. I cannot test this code so it's better to be conservative.

 drivers/misc/eeprom/idt_89hpesx.c | 8 ++++++--
 1 file changed, 6 insertions(+), 2 deletions(-)
drivers/misc/eeprom/idt_89hpesx.c +6 -2
index b0cff4b152da..7f430742ce2b 100644
@@ -909,14 +909,18 @@ static ssize_t idt_dbgfs_csr_write(struct file *filep, const char __user *ubuf,
 	u32 csraddr, csrval;
 	char *buf;
 
+	if (*offp)
+		return 0;
+
 	/* Copy data from User-space */
 	buf = kmalloc(count + 1, GFP_KERNEL);
 	if (!buf)
 		return -ENOMEM;
 
-	ret = simple_write_to_buffer(buf, count, offp, ubuf, count);
-	if (ret < 0)
+	if (copy_from_user(buf, ubuf, count)) {
+		ret = -EFAULT;
 		goto free_buf;
+	}
 	buf[count] = 0;
 
 	/* Find position of colon in the buffer */
Signature · 1 line
2.35.1