Skip to contentSkip to conversation

firmware: xilinx: fix debugfs write handler

PATCH In review Jann Horn 3 participants 4 messages Feb 18, 2019 – Mar 18, 2019 Arm/Zynq architecture

lore
Share & tools
lore

Jann Horn

author
#0 lore raw

PATCH 1/1 firmware: xilinx: fix debugfs write handler

- Userspace wants to write a string with len bytes, not counting the terminating NULL, so we should allocate len+1 bytes. It looks like the current code relied on having a nullbyte directly behind kern_buff, which happens to work reliably as long as len isn't one of the kmalloc size classes.
- strncpy_from_user() is completely wrong here; userspace is giving us a (not necessarily null-terminated) buffer and its length. strncpy_from_user() is for cases in which we don't know the length.
- Don't let broken userspace allocate arbitrarily big kmalloc allocations.

Just use memdup_user_nul(), which is designed precisely for things like this.

---
WARNING: completely untested patch

drivers/firmware/xilinx/zynqmp-debug.c | 15 ++++-----------

 1 file changed, 4 insertions(+), 11 deletions(-)
drivers/firmware/xilinx/zynqmp-debug.c +4 -11
index 2771df6df379..90b66cdbfd58 100644
@@ -163,21 +163,14 @@ static ssize_t zynqmp_pm_debugfs_api_write(struct file *file,
 
 	strcpy(debugfs_buf, "");
 
-	if (*off != 0 || len == 0)
+	if (*off != 0 || len <= 1 || len > PAGE_SIZE - 1)
 		return -EINVAL;
 
-	kern_buff = kzalloc(len, GFP_KERNEL);
-	if (!kern_buff)
-		return -ENOMEM;
-
+	kern_buff = memdup_user_nul(ptr, len);
+	if (IS_ERR(kern_buff))
+		return PTR_ERR(kern_buff);
 	tmp_buff = kern_buff;
 
-	ret = strncpy_from_user(kern_buff, ptr, len);
-	if (ret < 0) {
-		ret = -EFAULT;
-		goto err;
-	}
-
 	/* Read the API name from a user request */
 	pm_api_req = strsep(&kern_buff, " ");
 
Signature · 1 line
2.21.0.rc0.258.g878e2cd30e-goog
#1 lore raw
Quoted 41 lines · Jann Horn

- Userspace wants to write a string with len bytes, not counting the terminating NULL, so we should allocate len+1 bytes. It looks like the current code relied on having a nullbyte directly behind kern_buff, which happens to work reliably as long as len isn't one of the kmalloc size classes.
- strncpy_from_user() is completely wrong here; userspace is giving us a (not necessarily null-terminated) buffer and its length. strncpy_from_user() is for cases in which we don't know the length.
- Don't let broken userspace allocate arbitrarily big kmalloc allocations.

Just use memdup_user_nul(), which is designed precisely for things like this.

---
WARNING: completely untested patch

drivers/firmware/xilinx/zynqmp-debug.c | 15 ++++-----------

 1 file changed, 4 insertions(+), 11 deletions(-)
drivers/firmware/xilinx/zynqmp-debug.c +4 -5
index 2771df6df379..90b66cdbfd58 100644
@@ -163,21 +163,14 @@ static ssize_t zynqmp_pm_debugfs_api_write(struct file *file,
 
 	strcpy(debugfs_buf, "");
 
-	if (*off != 0 || len == 0)
+	if (*off != 0 || len <= 1 || len > PAGE_SIZE - 1)
 		return -EINVAL;
 
-	kern_buff = kzalloc(len, GFP_KERNEL);
-	if (!kern_buff)
-		return -ENOMEM;
-
+	kern_buff = memdup_user_nul(ptr, len);
+	if (IS_ERR(kern_buff))
+		return PTR_ERR(kern_buff);
 	tmp_buff = kern_buff;

drivers/firmware/xilinx/zynqmp-debug.c static ssize_t zynqmp_pm_debugfs_api_write(struct file *file, · line 176

Quoted 10 lines
drivers/firmware/xilinx/zynqmp-debug.c +0 -6
 
-	ret = strncpy_from_user(kern_buff, ptr, len);
-	if (ret < 0) {
-		ret = -EFAULT;
-		goto err;
-	}
-
 	/* Read the API name from a user request */
 	pm_api_req = strsep(&kern_buff, " ");
 

Jolly: Can you please retest it and ACK?

Thanks,
Michal

Acked-by on v1 1/1
#2 lore raw
In reply to Jann Horn
Quoted 62 lines

-----Original Message-----
From: Jann Horn <[email protected]>
Sent: Monday, February 18, 2019 1:43 PM
To: Michal Simek <[email protected]>; [email protected]
Cc: Rajan Vaja <[email protected]>; Jolly Shah <[email protected]>; linux- [email protected]; [email protected]
Subject: [PATCH] firmware: xilinx: fix debugfs write handler

- Userspace wants to write a string with len bytes, not counting the terminating NULL, so we should allocate len+1 bytes. It looks like the current code relied on having a nullbyte directly behind kern_buff, which happens to work reliably as long as len isn't one of the kmalloc size classes.
- strncpy_from_user() is completely wrong here; userspace is giving us a (not necessarily null-terminated) buffer and its length. strncpy_from_user() is for cases in which we don't know the length.
- Don't let broken userspace allocate arbitrarily big kmalloc allocations.

Just use memdup_user_nul(), which is designed precisely for things like this.

---
WARNING: completely untested patch

drivers/firmware/xilinx/zynqmp-debug.c | 15 ++++-----------

 1 file changed, 4 insertions(+), 11 deletions(-)
drivers/firmware/xilinx/zynqmp-debug.c +4 -12
index 2771df6df379..90b66cdbfd58 100644
@@ -163,21 +163,14 @@ static ssize_t zynqmp_pm_debugfs_api_write(struct
 file *file,
 
 	strcpy(debugfs_buf, "");
 
-	if (*off != 0 || len == 0)
+	if (*off != 0 || len <= 1 || len > PAGE_SIZE - 1)
 		return -EINVAL;
 
-	kern_buff = kzalloc(len, GFP_KERNEL);
-	if (!kern_buff)
-		return -ENOMEM;
-
+	kern_buff = memdup_user_nul(ptr, len);
+	if (IS_ERR(kern_buff))
+		return PTR_ERR(kern_buff);
 	tmp_buff = kern_buff;
 
-	ret = strncpy_from_user(kern_buff, ptr, len);
-	if (ret < 0) {
-		ret = -EFAULT;
-		goto err;
-	}
-
 	/* Read the API name from a user request */
 	pm_api_req = strsep(&kern_buff, " ");
 
--

2.21.0.rc0.258.g878e2cd30e-goog

#3 lore raw
In reply to Jann Horn
Quoted 41 lines · Jann Horn

- Userspace wants to write a string with len bytes, not counting the terminating NULL, so we should allocate len+1 bytes. It looks like the current code relied on having a nullbyte directly behind kern_buff, which happens to work reliably as long as len isn't one of the kmalloc size classes.
- strncpy_from_user() is completely wrong here; userspace is giving us a (not necessarily null-terminated) buffer and its length. strncpy_from_user() is for cases in which we don't know the length.
- Don't let broken userspace allocate arbitrarily big kmalloc allocations.

Just use memdup_user_nul(), which is designed precisely for things like this.

---
WARNING: completely untested patch

drivers/firmware/xilinx/zynqmp-debug.c | 15 ++++-----------

 1 file changed, 4 insertions(+), 11 deletions(-)
drivers/firmware/xilinx/zynqmp-debug.c +4 -5
index 2771df6df379..90b66cdbfd58 100644
@@ -163,21 +163,14 @@ static ssize_t zynqmp_pm_debugfs_api_write(struct file *file,
 
 	strcpy(debugfs_buf, "");
 
-	if (*off != 0 || len == 0)
+	if (*off != 0 || len <= 1 || len > PAGE_SIZE - 1)
 		return -EINVAL;
 
-	kern_buff = kzalloc(len, GFP_KERNEL);
-	if (!kern_buff)
-		return -ENOMEM;
-
+	kern_buff = memdup_user_nul(ptr, len);
+	if (IS_ERR(kern_buff))
+		return PTR_ERR(kern_buff);
 	tmp_buff = kern_buff;

drivers/firmware/xilinx/zynqmp-debug.c static ssize_t zynqmp_pm_debugfs_api_write(struct file *file, · line 176

Quoted 10 lines
drivers/firmware/xilinx/zynqmp-debug.c +0 -6
 
-	ret = strncpy_from_user(kern_buff, ptr, len);
-	if (ret < 0) {
-		ret = -EFAULT;
-		goto err;
-	}
-
 	/* Read the API name from a user request */
 	pm_api_req = strsep(&kern_buff, " ");
 

applied with Jolly's ack.

Thanks,
Michal