[PATCH] EDAC: thunderx: avoid stack disclosure through debugfs

Ziheng Wei posted 1 patch 1 day, 19 hours ago
drivers/edac/thunderx_edac.c | 8 ++++----
1 file changed, 4 insertions(+), 4 deletions(-)
[PATCH] EDAC: thunderx: avoid stack disclosure through debugfs
Posted by Ziheng Wei 1 day, 19 hours ago
The debugfs readers copy sizeof(buf) bytes instead of the formatted
length, exposing uninitialized stack bytes. DEBUGFS_FIELD_ATTR also uses
the read count as the snprintf() size, so short reads with lseek() can
expose additional bytes.

Format with sizeof(buf) and return only strlen(buf) bytes.

Found by LLM-assisted static analysis. Cross-compiled for arm64; no
runtime test on the affected hardware.

Fixes: 41003396f932 ("EDAC, thunderx: Add Cavium ThunderX EDAC driver")
Cc: stable@vger.kernel.org
Signed-off-by: Ziheng Wei <zihengw03@163.com>
---
 drivers/edac/thunderx_edac.c | 8 ++++----
 1 file changed, 4 insertions(+), 4 deletions(-)

diff --git a/drivers/edac/thunderx_edac.c b/drivers/edac/thunderx_edac.c
index 9c0a1e48f..d24d74fd2 100644
--- a/drivers/edac/thunderx_edac.c
+++ b/drivers/edac/thunderx_edac.c
@@ -227,9 +227,9 @@ static ssize_t thunderx_##_type##_##_field##_read(struct file *file,	    \
 	struct thunderx_##_type *pdata = file->private_data;		    \
 	char buf[20];							    \
 									    \
-	snprintf(buf, count, "0x%016llx", pdata->_field);		    \
+	snprintf(buf, sizeof(buf), "0x%016llx", pdata->_field);		    \
 	return simple_read_from_buffer(data, count, ppos,		    \
-				       buf, sizeof(buf));		    \
+				       buf, strlen(buf));		    \
 }									    \
 									    \
 static ssize_t thunderx_##_type##_##_field##_write(struct file *file,	    \
@@ -258,7 +258,7 @@ static ssize_t thunderx_##_type##_##_name##_read(struct file *file,	    \
 									    \
 	sprintf(buf, "0x%016llx", readq(pdata->regs + _reg));		    \
 	return simple_read_from_buffer(data, count, ppos,		    \
-				       buf, sizeof(buf));		    \
+				       buf, strlen(buf));		    \
 }									    \
 									    \
 static ssize_t thunderx_##_type##_##_name##_write(struct file *file,	    \
@@ -322,7 +322,7 @@ static ssize_t thunderx_lmc_int_read(struct file *file,
 	u64 lmc_int = readq(lmc->regs + LMC_INT);
 
 	snprintf(buf, sizeof(buf), "0x%016llx", lmc_int);
-	return simple_read_from_buffer(data, count, ppos, buf, sizeof(buf));
+	return simple_read_from_buffer(data, count, ppos, buf, strlen(buf));
 }
 
 #define TEST_PATTERN 0xa5

base-commit: 97d73c8251226cf27cc6656b4c542869bb8c69fa
-- 
2.43.0