Skip to contentSkip to conversation

NTB: ntb_tool: uninitialized heap data in tool_fn_write()

PATCH In review Dan Carpenter 3 participants 3 messages Jul 20, 2022 – Aug 12, 2022

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

PATCH 1/1 NTB: ntb_tool: uninitialized heap data in tool_fn_write()

The call to:

	ret = simple_write_to_buffer(buf, size, offp, ubuf, size);

will return success if it is able to write even one byte to "buf". The value of "*offp" controls which byte. This could result in reading uninitialized data when we do the sscanf() on the next line.

This code is not really desigined to handle partial writes where *offp is non-zero and the "buf" is preserved and re-used between writes. Just ban partial writes and replace the simple_write_to_buffer() with copy_from_user().

Fixes 578b881ba9c4 ("NTB: Add tool test client")
---
 drivers/ntb/test/ntb_tool.c | 8 +++++---
 1 file changed, 5 insertions(+), 3 deletions(-)
drivers/ntb/test/ntb_tool.c +5 -3
index b7bf3f863d79..5ee0afa621a9 100644
@@ -367,14 +367,16 @@ static ssize_t tool_fn_write(struct tool_ctx *tc,
 	u64 bits;
 	int n;
 
+	if (*offp)
+		return 0;
+
 	buf = kmalloc(size + 1, GFP_KERNEL);
 	if (!buf)
 		return -ENOMEM;
 
-	ret = simple_write_to_buffer(buf, size, offp, ubuf, size);
-	if (ret < 0) {
+	if (copy_from_user(buf, ubuf, size)) {
 		kfree(buf);
-		return ret;
+		return -EFAULT;
 	}
 
 	buf[size] = 0;
Signature · 1 line
2.35.1
Reviewed-by on v1 1/1
#1 lore raw
Quoted 8 lines · Dan Carpenter

The call to:

	ret = simple_write_to_buffer(buf, size, offp, ubuf, size);

will return success if it is able to write even one byte to "buf". The value of "*offp" controls which byte. This could result in reading uninitialized data when we do the sscanf() on the next line.

Quoted 6 lines

This code is not really desigined to handle partial writes where *offp is non-zero and the "buf" is preserved and re-used between writes. Just ban partial writes and replace the simple_write_to_buffer() with copy_from_user().

Fixes 578b881ba9c4 ("NTB: Add tool test client")

Looks good. Thanks.

-Sergey

Quoted 33 lines
---
 drivers/ntb/test/ntb_tool.c | 8 +++++---
 1 file changed, 5 insertions(+), 3 deletions(-)
drivers/ntb/test/ntb_tool.c +5 -3
index b7bf3f863d79..5ee0afa621a9 100644
@@ -367,14 +367,16 @@ static ssize_t tool_fn_write(struct tool_ctx *tc,
 	u64 bits;
 	int n;
 
+	if (*offp)
+		return 0;
+
 	buf = kmalloc(size + 1, GFP_KERNEL);
 	if (!buf)
 		return -ENOMEM;
 
-	ret = simple_write_to_buffer(buf, size, offp, ubuf, size);
-	if (ret < 0) {
+	if (copy_from_user(buf, ubuf, size)) {
 		kfree(buf);
-		return ret;
+		return -EFAULT;
 	}
 
 	buf[size] = 0;
Signature · 1 line
2.35.1
maintainer
#2 lore raw
Quoted 14 lines · Serge Semin
Quoted 13 lines · Dan Carpenter

The call to:

	ret = simple_write_to_buffer(buf, size, offp, ubuf, size);

will return success if it is able to write even one byte to "buf". The value of "*offp" controls which byte. This could result in reading uninitialized data when we do the sscanf() on the next line.

This code is not really desigined to handle partial writes where *offp is non-zero and the "buf" is preserved and re-used between writes. Just ban partial writes and replace the simple_write_to_buffer() with copy_from_user().

Quoted 6 lines
Quoted 1 line
Fixes 578b881ba9c4 ("NTB: Add tool test client")

Looks good. Thanks.

-Sergey

Sorry for the extremely long delay in response. This series is in my ntb branch and will be in my pull request for v5.20 which should be going out later today.

Thanks,
Jon

Quoted 34 lines
Quoted 34 lines
---
 drivers/ntb/test/ntb_tool.c | 8 +++++---
 1 file changed, 5 insertions(+), 3 deletions(-)
drivers/ntb/test/ntb_tool.c +5 -3
index b7bf3f863d79..5ee0afa621a9 100644
@@ -367,14 +367,16 @@ static ssize_t tool_fn_write(struct tool_ctx *tc,
 	u64 bits;
 	int n;
 
+	if (*offp)
+		return 0;
+
 	buf = kmalloc(size + 1, GFP_KERNEL);
 	if (!buf)
 		return -ENOMEM;
 
-	ret = simple_write_to_buffer(buf, size, offp, ubuf, size);
-	if (ret < 0) {
+	if (copy_from_user(buf, ubuf, size)) {
 		kfree(buf);
-		return ret;
+		return -EFAULT;
 	}
 
 	buf[size] = 0;
Signature · 1 line
2.35.1