mirror of https://lore.kernel.org/lkml/
 help / color / mirror / Atom feed
* [PATCH net] net/mlx5: fix a stack over-read in the QP debugfs files
@ 2026-11-20  7:50 Aibing Chen
  2026-11-20  7:53 ` netdev-bot+sinfo
  0 siblings, 1 reply; 3+ messages in thread
From: Aibing Chen @ 2026-11-20  7:50 UTC (permalink / raw)
  To: Saeed Mahameed, Leon Romanovsky, Tariq Toukan, Mark Bloch
  Cc: Andrew Lunn, David S . Miller, Eric Dumazet, Jakub Kicinski,
	Paolo Abeni, netdev, linux-rdma, linux-kernel

dbg_read() formats the field into char tbuf[18] and passes the return
value of snprintf() to simple_read_from_buffer() as the number of
available bytes.  snprintf() returns the length the output would have
had, not the number of bytes written, so when the string does not fit
that value is larger than the buffer: the longest QP transport string is
"Invalid transport type" (22 characters), so with the trailing newline
the function returns 23 while only 18 bytes were written.

simple_read_from_buffer() uses that length to bound the copy to
userspace, so reading .../qp/<qpn>/transport copies up to 23 bytes out of
the 18 byte stack array, leaking 5 bytes of stack.

Enlarge the buffer so the longest string fits and use scnprintf(), which
returns the number of bytes actually written.

Signed-off-by: Aibing Chen <chenaibing@cmss.chinamobile.com>
---
 drivers/net/ethernet/mellanox/mlx5/core/debugfs.c | 7 ++++---
 1 file changed, 4 insertions(+), 3 deletions(-)

diff --git a/drivers/net/ethernet/mellanox/mlx5/core/debugfs.c b/drivers/net/ethernet/mellanox/mlx5/core/debugfs.c
index 30be2b631..3e75f66f7 100644
--- a/drivers/net/ethernet/mellanox/mlx5/core/debugfs.c
+++ b/drivers/net/ethernet/mellanox/mlx5/core/debugfs.c
@@ -496,7 +496,7 @@ static ssize_t dbg_read(struct file *filp, char __user *buf, size_t count,
 {
 	struct mlx5_field_desc *desc;
 	struct mlx5_rsc_debug *d;
-	char tbuf[18];
+	char tbuf[24];
 	int is_str = 0;
 	u64 field;
 	int ret;
@@ -522,9 +522,10 @@ static ssize_t dbg_read(struct file *filp, char __user *buf, size_t count,
 	}
 
 	if (is_str)
-		ret = snprintf(tbuf, sizeof(tbuf), "%s\n", (const char *)(unsigned long)field);
+		ret = scnprintf(tbuf, sizeof(tbuf), "%s\n",
+				(const char *)(unsigned long)field);
 	else
-		ret = snprintf(tbuf, sizeof(tbuf), "0x%llx\n", field);
+		ret = scnprintf(tbuf, sizeof(tbuf), "0x%llx\n", field);
 
 	return simple_read_from_buffer(buf, count, pos, tbuf, ret);
 }



^ permalink raw reply	[flat|nested] 3+ messages in thread

end of thread, other threads:[~2026-11-20  9:53 UTC | newest]

Thread overview: 3+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2026-11-20  7:50 [PATCH net] net/mlx5: fix a stack over-read in the QP debugfs files Aibing Chen
2026-11-20  7:53 ` netdev-bot+sinfo
2026-11-20  9:53   ` Aibing Chen

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox

all inboxes | Powered by JetHome®