Skip to contentSkip to conversation

debugfs: Reserve space for string terminators

PATCH In review Jiale Yao 9 participants 14 messages Sep 26 – Oct 8

Subsystems (5)
lore
Share & tools
lore

Jiale Yao

author
#0 lore raw

cover debugfs: Reserve space for string terminators

The same boundary mistake appears in three debugfs write handlers. Each handler has a zero-initialized buffer and allows a user write to fill the entire buffer. That overwrites the only NUL terminator before the input is parsed with sscanf(), strsep(), or strcasecmp(), which can then read beyond the end of the buffer.

The write paths are independent, so the fixes are split by file and can be applied separately. Each patch reserves one byte for the terminating NUL while preserving the normal input size for that handler.

Jiale Yao (3):
platform/olpc: Reserve space for a string terminator
wifi: ath12k: Reserve space for a string terminator
dmaengine: xilinx: dpdma: Reserve space for a string terminator

 drivers/dma/xilinx/xilinx_dpdma.c                   | 2 +-
 drivers/net/wireless/ath/ath12k/debugfs_htt_stats.c | 2 +-
 drivers/platform/olpc/olpc-ec.c                     | 2 +-
 3 files changed, 3 insertions(+), 3 deletions(-)
Signature · 1 line
2.34.1
author
#1 lore raw

PATCH 1/3 platform/olpc: Reserve space for a string terminator

ec_dbgfs_cmd_write() copies user input into a zero-initialized stack buffer and passes it to sscanf(). A write that fills the entire buffer overwrites its only terminator, so the subsequent parsing can read beyond the buffer.

Limit the copy to leave room for the trailing NUL.

Fixes 6cca83d498bd ("Platform: OLPC: move debugfs support from x86 EC driver")
---
 drivers/platform/olpc/olpc-ec.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
drivers/platform/olpc/olpc-ec.c +1 -1
index 4a2d36f7331e..063f7e55c421 100644
@@ -269,7 +269,7 @@ static ssize_t ec_dbgfs_cmd_write(struct file *file, const char __user *buf,
 
 	mutex_lock(&ec_dbgfs_lock);
 
-	size = simple_write_to_buffer(cmdbuf, sizeof(cmdbuf), ppos, buf, size);
+	size = simple_write_to_buffer(cmdbuf, sizeof(cmdbuf) - 1, ppos, buf, size);
 
 	m = sscanf(cmdbuf, "%x:%u %x %x %x %x %x", &ec_cmd_int[0],
 			&ec_dbgfs_resp_bytes, &ec_cmd_int[1], &ec_cmd_int[2],
Signature · 1 line
2.34.1
author
#2 lore raw

PATCH 2/3 wifi: ath12k: Reserve space for a string terminator

In reply to Jiale Yao

ath12k_write_htt_stats_type() accepts count == size, which fills the zero-initialized buffer without a terminating NUL. sscanf() then reads beyond the buffer.

Reject input that leaves no room for the trailing NUL.

Fixes 8c7a5031a6b0 ("wifi: ath12k: Fix buffer overflow in debugfs")
---
 drivers/net/wireless/ath/ath12k/debugfs_htt_stats.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
drivers/net/wireless/ath/ath12k/debugfs_htt_stats.c +1 -1
index b772181a496e..f84f1828275a 100644
@@ -6190,7 +6190,7 @@ static ssize_t ath12k_write_htt_stats_type(struct file *file,
 	const int size = 32;
 	int num_args;
 
-	if (count > size)
+	if (count >= size)
 		return -EINVAL;
 
 	char *buf __free(kfree) = kzalloc(size, GFP_KERNEL);
Signature · 1 line
2.34.1
Reviewed-by on v1 2/3
#3 lore raw

Re: [PATCH 2/3] wifi: ath12k: Reserve space for a string terminator

Quoted 9 lines · Jiale Yao

ath12k_write_htt_stats_type() accepts count == size, which fills the zero-initialized buffer without a terminating NUL. sscanf() then reads beyond the buffer.

Reject input that leaves no room for the trailing NUL.

Fixes 8c7a5031a6b0 ("wifi: ath12k: Fix buffer overflow in debugfs")

---

regards,
dan carpenter

Reviewed-by on v1 2/3
#4 lore raw

Re: [PATCH 2/3] wifi: ath12k: Reserve space for a string terminator

In reply to Jiale Yao
Quoted 17 lines · Jiale Yao

ath12k_write_htt_stats_type() accepts count == size, which fills the zero-initialized buffer without a terminating NUL. sscanf() then reads beyond the buffer.

Reject input that leaves no room for the trailing NUL.

Fixes 8c7a5031a6b0 ("wifi: ath12k: Fix buffer overflow in debugfs")
---
  drivers/net/wireless/ath/ath12k/debugfs_htt_stats.c | 2 +-
  1 file changed, 1 insertion(+), 1 deletion(-)
drivers/net/wireless/ath/ath12k/debugfs_htt_stats.c +0 -0
index b772181a496e..f84f1828275a 100644

drivers/net/wireless/ath/ath12k/debugfs_htt_stats.c static ssize_t ath12k_write_htt_stats_type(struct file *file, · line 6196

Quoted 8 lines
drivers/net/wireless/ath/ath12k/debugfs_htt_stats.c +1 -1
@@ -6190,7 +6190,7 @@ static ssize_t ath12k_write_htt_stats_type(struct file *file,
  	const int size = 32;
  	int num_args;
 
-	if (count > size)
+	if (count >= size)
  		return -EINVAL;
 
  	char *buf __free(kfree) = kzalloc(size, GFP_KERNEL);
Reviewed-by on v1 2/3
#5 lore raw

Re: [PATCH 2/3] wifi: ath12k: Reserve space for a string terminator

In reply to Jiale Yao
Quoted 17 lines · Jiale Yao

ath12k_write_htt_stats_type() accepts count == size, which fills the zero-initialized buffer without a terminating NUL. sscanf() then reads beyond the buffer.

Reject input that leaves no room for the trailing NUL.

Fixes 8c7a5031a6b0 ("wifi: ath12k: Fix buffer overflow in debugfs")
---
 drivers/net/wireless/ath/ath12k/debugfs_htt_stats.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
drivers/net/wireless/ath/ath12k/debugfs_htt_stats.c +0 -0
index b772181a496e..f84f1828275a 100644

drivers/net/wireless/ath/ath12k/debugfs_htt_stats.c static ssize_t ath12k_write_htt_stats_type(struct file *file, · line 6196

Quoted 8 lines
drivers/net/wireless/ath/ath12k/debugfs_htt_stats.c +1 -1
@@ -6190,7 +6190,7 @@ static ssize_t ath12k_write_htt_stats_type(struct file *file,
 	const int size = 32;
 	int num_args;
 
-	if (count > size)
+	if (count >= size)
 		return -EINVAL;
 
 	char *buf __free(kfree) = kzalloc(size, GFP_KERNEL);
#6 lore raw

Re: [PATCH 2/3] wifi: ath12k: Reserve space for a string terminator

In reply to Jiale Yao
Quoted 17 lines · Jiale Yao

ath12k_write_htt_stats_type() accepts count == size, which fills the zero-initialized buffer without a terminating NUL. sscanf() then reads beyond the buffer.

Reject input that leaves no room for the trailing NUL.

Fixes 8c7a5031a6b0 ("wifi: ath12k: Fix buffer overflow in debugfs")
---
 drivers/net/wireless/ath/ath12k/debugfs_htt_stats.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
drivers/net/wireless/ath/ath12k/debugfs_htt_stats.c +0 -0
index b772181a496e..f84f1828275a 100644

drivers/net/wireless/ath/ath12k/debugfs_htt_stats.c static ssize_t ath12k_write_htt_stats_type(struct file *file, · line 6196

Quoted 8 lines
drivers/net/wireless/ath/ath12k/debugfs_htt_stats.c +1 -1
@@ -6190,7 +6190,7 @@ static ssize_t ath12k_write_htt_stats_type(struct file *file,
 	const int size = 32;
 	int num_args;
 
-	if (count > size)
+	if (count >= size)
 		return -EINVAL;
 
 	char *buf __free(kfree) = kzalloc(size, GFP_KERNEL);

Documenting this was reposted as a standalone patch:
https://msgid.link/[email protected]

I'm taking that patch through the ath tree.

/jeff

author
#7 lore raw

PATCH 3/3 dmaengine: xilinx: dpdma: Reserve space for a string terminator

In reply to Jiale Yao

xilinx_dpdma_debugfs_write() allocates a buffer of size bytes, and strncpy_from_user() can fill it without a terminating NUL when the input has no NUL in the copied range. strsep() and strcasecmp() then read beyond the buffer.

Allocate an extra byte and keep that byte zero-initialized, so the input copied remains unchanged and the buffer is always terminated.

Fixes 1d220435cab3 ("dmaengine: xilinx: dpdma: Add debugfs support")
---
 drivers/dma/xilinx/xilinx_dpdma.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
drivers/dma/xilinx/xilinx_dpdma.c +1 -1
index d9a3542c4531..b61ef3062d84 100644
@@ -410,7 +410,7 @@ static ssize_t xilinx_dpdma_debugfs_write(struct file *f,
 	if (dpdma_debugfs.testcase != DPDMA_TC_NONE)
 		return -EBUSY;
 
-	kern_buff = kzalloc(size, GFP_KERNEL);
+	kern_buff = kzalloc(size + 1, GFP_KERNEL);
 	if (!kern_buff)
 		return -ENOMEM;
 	kern_buff_start = kern_buff;
Signature · 1 line
2.34.1
maintainer
#8 lore raw

Re: [PATCH 3/3] dmaengine: xilinx: dpdma: Reserve space for a string terminator

Quoted 4 lines · Jiale Yao

xilinx_dpdma_debugfs_write() allocates a buffer of size bytes, and strncpy_from_user() can fill it without a terminating NUL when the input has no NUL in the copied range. strsep() and strcasecmp() then read beyond the buffer.

strncpy() has long been considered unsafe, and has finally been removed from the kernel in v7.2. A better fix would be to similarly replace strncpy_from_user() with a safe equivalent.

Quoted 22 lines

Allocate an extra byte and keep that byte zero-initialized, so the input copied remains unchanged and the buffer is always terminated.

Fixes 1d220435cab3 ("dmaengine: xilinx: dpdma: Add debugfs support")
---
 drivers/dma/xilinx/xilinx_dpdma.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
drivers/dma/xilinx/xilinx_dpdma.c +1 -1
index d9a3542c4531..b61ef3062d84 100644
@@ -410,7 +410,7 @@ static ssize_t xilinx_dpdma_debugfs_write(struct file *f,
 	if (dpdma_debugfs.testcase != DPDMA_TC_NONE)
 		return -EBUSY;
 
-	kern_buff = kzalloc(size, GFP_KERNEL);
+	kern_buff = kzalloc(size + 1, GFP_KERNEL);
 	if (!kern_buff)
 		return -ENOMEM;
 	kern_buff_start = kern_buff;
Signature · 3 lines
Regards,

Laurent Pinchart
#9 lore raw

Re: [PATCH 3/3] dmaengine: xilinx: dpdma: Reserve space for a string terminator

Quoted 9 lines · Laurent Pinchart
Quoted 4 lines · Jiale Yao

xilinx_dpdma_debugfs_write() allocates a buffer of size bytes, and strncpy_from_user() can fill it without a terminating NUL when the input has no NUL in the copied range. strsep() and strcasecmp() then read beyond the buffer.

strncpy() has long been considered unsafe, and has finally been removed from the kernel in v7.2. A better fix would be to similarly replace strncpy_from_user() with a safe equivalent.

Do you means use strndup_user()?

Frank

Quoted 28 lines
Quoted 15 lines

Allocate an extra byte and keep that byte zero-initialized, so the input copied remains unchanged and the buffer is always terminated.

Fixes 1d220435cab3 ("dmaengine: xilinx: dpdma: Add debugfs support")
---
 drivers/dma/xilinx/xilinx_dpdma.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
drivers/dma/xilinx/xilinx_dpdma.c +0 -0
index d9a3542c4531..b61ef3062d84 100644

drivers/dma/xilinx/xilinx_dpdma.c static ssize_t xilinx_dpdma_debugfs_write(struct file *f, · line 416

Quoted 8 lines
drivers/dma/xilinx/xilinx_dpdma.c +1 -1
@@ -410,7 +410,7 @@ static ssize_t xilinx_dpdma_debugfs_write(struct file *f,
 	if (dpdma_debugfs.testcase != DPDMA_TC_NONE)
 		return -EBUSY;
 
-	kern_buff = kzalloc(size, GFP_KERNEL);
+	kern_buff = kzalloc(size + 1, GFP_KERNEL);
 	if (!kern_buff)
 		return -ENOMEM;
 	kern_buff_start = kern_buff;

--
Regards,

Laurent Pinchart

maintainer
#10 lore raw

Re: [PATCH 3/3] dmaengine: xilinx: dpdma: Reserve space for a string terminator

Quoted 7 lines · Frank Li
Quoted 6 lines · Laurent Pinchart
Quoted 4 lines · Jiale Yao

xilinx_dpdma_debugfs_write() allocates a buffer of size bytes, and strncpy_from_user() can fill it without a terminating NUL when the input has no NUL in the copied range. strsep() and strcasecmp() then read beyond the buffer.

Quoted 5 lines
Quoted 3 lines

strncpy() has long been considered unsafe, and has finally been removed from the kernel in v7.2. A better fix would be to similarly replace strncpy_from_user() with a safe equivalent.

Do you means use strndup_user()?

I was thinking about adding a strscpy_user(), but a dup version could possibly be interesting too if there are enough users that call k[zm]alloc + strncpy_from_user.

Quoted 22 lines
Quoted 22 lines
Quoted 22 lines

Allocate an extra byte and keep that byte zero-initialized, so the input copied remains unchanged and the buffer is always terminated.

Fixes 1d220435cab3 ("dmaengine: xilinx: dpdma: Add debugfs support")
---
 drivers/dma/xilinx/xilinx_dpdma.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
drivers/dma/xilinx/xilinx_dpdma.c +1 -1
index d9a3542c4531..b61ef3062d84 100644
@@ -410,7 +410,7 @@ static ssize_t xilinx_dpdma_debugfs_write(struct file *f,
 	if (dpdma_debugfs.testcase != DPDMA_TC_NONE)
 		return -EBUSY;
 
-	kern_buff = kzalloc(size, GFP_KERNEL);
+	kern_buff = kzalloc(size + 1, GFP_KERNEL);
 	if (!kern_buff)
 		return -ENOMEM;
 	kern_buff_start = kern_buff;
Signature · 3 lines
Regards,

Laurent Pinchart
#11 lore raw

Re:Re: [PATCH 3/3] dmaengine: xilinx: dpdma: Reserve space for a string terminator

Quoted 12 lines · Laurent Pinchart
Quoted 11 lines · Frank Li
Quoted 9 lines · Laurent Pinchart
Quoted 4 lines · Jiale Yao

xilinx_dpdma_debugfs_write() allocates a buffer of size bytes, and strncpy_from_user() can fill it without a terminating NUL when the input has no NUL in the copied range. strsep() and strcasecmp() then read beyond the buffer.

strncpy() has long been considered unsafe, and has finally been removed from the kernel in v7.2. A better fix would be to similarly replace strncpy_from_user() with a safe equivalent.

Quoted 5 lines
Quoted 1 line

Do you means use strndup_user()?

I was thinking about adding a strscpy_user(), but a dup version could possibly be interesting too if there are enough users that call k[zm]alloc + strncpy_from_user.

Thanks for all reviews, how about this one?
https://lore.kernel.org/all/[email protected]/

Quoted 28 lines
Quoted 23 lines
Quoted 23 lines
Quoted 23 lines

Allocate an extra byte and keep that byte zero-initialized, so the input copied remains unchanged and the buffer is always terminated.

Fixes 1d220435cab3 ("dmaengine: xilinx: dpdma: Add debugfs support")
---
 drivers/dma/xilinx/xilinx_dpdma.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
drivers/dma/xilinx/xilinx_dpdma.c +1 -1
index d9a3542c4531..b61ef3062d84 100644
@@ -410,7 +410,7 @@ static ssize_t xilinx_dpdma_debugfs_write(struct file *f,
 	if (dpdma_debugfs.testcase != DPDMA_TC_NONE)
 		return -EBUSY;
 
-	kern_buff = kzalloc(size, GFP_KERNEL);
+	kern_buff = kzalloc(size + 1, GFP_KERNEL);
 	if (!kern_buff)
 		return -ENOMEM;
 	kern_buff_start = kern_buff;
Signature · 3 lines
Regards,

Laurent Pinchart
#12 lore raw

Re: Re: [PATCH 3/3] dmaengine: xilinx: dpdma: Reserve space for a string terminator

Quoted 15 lines · jiale yao

[You don't often get email from [email protected]. Learn why this is important at https://aka.ms/LearnAboutSenderIdentification ]

Quoted 12 lines · Laurent Pinchart
Quoted 11 lines · Frank Li
Quoted 9 lines · Laurent Pinchart
Quoted 4 lines · Jiale Yao

xilinx_dpdma_debugfs_write() allocates a buffer of size bytes, and strncpy_from_user() can fill it without a terminating NUL when the input has no NUL in the copied range. strsep() and strcasecmp() then read beyond the buffer.

strncpy() has long been considered unsafe, and has finally been removed from the kernel in v7.2. A better fix would be to similarly replace strncpy_from_user() with a safe equivalent.

Quoted 8 lines
Quoted 5 lines
Quoted 1 line

Do you means use strndup_user()?

I was thinking about adding a strscpy_user(), but a dup version could possibly be interesting too if there are enough users that call k[zm]alloc + strncpy_from_user.

Thanks for all reviews, how about this one?
https://lore.kernel.org/all/[email protected]/

Looks good.

Frank

Quoted 28 lines
Quoted 28 lines
Quoted 18 lines
Quoted 18 lines
Quoted 18 lines

Allocate an extra byte and keep that byte zero-initialized, so the input copied remains unchanged and the buffer is always terminated.

Fixes 1d220435cab3 ("dmaengine: xilinx: dpdma: Add debugfs support")
---
 drivers/dma/xilinx/xilinx_dpdma.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
drivers/dma/xilinx/xilinx_dpdma.c +0 -0
index d9a3542c4531..b61ef3062d84 100644
@@ -410,7 +410,7 @@ static ssize_t xilinx_dpdma_debugfs_write(struct file *f,
         if (dpdma_debugfs.testcase != DPDMA_TC_NONE)
                 return -EBUSY;
 
Quoted 5 lines
Quoted 5 lines

drivers/dma/xilinx/xilinx_dpdma.c static ssize_t xilinx_dpdma_debugfs_write(struct file *f, · line 416

Quoted 5 lines
drivers/dma/xilinx/xilinx_dpdma.c +1 -1
-        kern_buff = kzalloc(size, GFP_KERNEL);
+        kern_buff = kzalloc(size + 1, GFP_KERNEL);
         if (!kern_buff)
                 return -ENOMEM;
         kern_buff_start = kern_buff;

--
Regards,

Laurent Pinchart

maintainer
#13 lore raw
In reply to Jiale Yao
Quoted 11 lines · Jiale Yao

The same boundary mistake appears in three debugfs write handlers. Each handler has a zero-initialized buffer and allows a user write to fill the entire buffer. That overwrites the only NUL terminator before the input is parsed with sscanf(), strsep(), or strcasecmp(), which can then read beyond the end of the buffer.

The write paths are independent, so the fixes are split by file and can be applied separately. Each patch reserves one byte for the terminating NUL while preserving the normal input size for that handler.

[...]

Thank you for your contribution, it has been applied to my local review-ilpo-next branch. Note it will show up in the public platform-drivers-x86/review-ilpo-next branch only once I've pushed my local branch there, which might take a while.

FYI [if applicable to your patch], as per Linus' policy change, also fixes are mostly routed through for-next unless the fix is for a commit introduced in the most recent cycle or is clearly a regression fix.

The list of commits applied:
[1/3] platform/olpc: Reserve space for a string terminator

      commit: 4ef563af57cc48d5a9662d7c69aa4447c2cc1b2c

[2/3] wifi: ath12k: Reserve space for a string terminator
(no commit info)
[3/3] dmaengine: xilinx: dpdma: Reserve space for a string terminator
(no commit info)

--
i.