Netdev List
 help / color / mirror / Atom feed
* [PATCH net] can: can327: fix snprintf() limit in can327_handle_prompt()
@ 2024-11-14  9:03 Dan Carpenter
  2024-11-14  9:19 ` Max Staudt
  2024-11-14  9:34 ` Vincent Mailhol
  0 siblings, 2 replies; 14+ messages in thread
From: Dan Carpenter @ 2024-11-14  9:03 UTC (permalink / raw)
  To: Max Staudt
  Cc: Marc Kleine-Budde, Vincent Mailhol, Andrew Lunn, David S. Miller,
	Eric Dumazet, Jakub Kicinski, Paolo Abeni, linux-can, netdev,
	linux-kernel, kernel-janitors

This code is printing hex values to the &local_txbuf buffer and it's
using the snprintf() function to try prevent buffer overflows.  The
problem is that it's not passing the correct limit to the snprintf()
function so the limit doesn't do anything.  On each iteration we print
two digits so the remaining size should also decrease by two, but
instead it passes the sizeof() the entire buffer each time.

If the frame->len were too long it would result in a buffer overflow.

I've also changed the function from snprintf() to scnprintf().  The
difference between the two functions is that snprintf() returns the number
of bytes which would have been printed if there were space while the
scnprintf() function returns the number of bytes which are actually
printed.

Fixes: 43da2f07622f ("can: can327: CAN/ldisc driver for ELM327 based OBD-II adapters")
Signed-off-by: Dan Carpenter <dan.carpenter@linaro.org>
---
---
 drivers/net/can/can327.c | 10 +++++-----
 1 file changed, 5 insertions(+), 5 deletions(-)

diff --git a/drivers/net/can/can327.c b/drivers/net/can/can327.c
index 24af63961030..5c05ebc72318 100644
--- a/drivers/net/can/can327.c
+++ b/drivers/net/can/can327.c
@@ -623,16 +623,16 @@ static void can327_handle_prompt(struct can327 *elm)
 			snprintf(local_txbuf, sizeof(local_txbuf), "ATRTR\r");
 		} else {
 			/* Send a regular CAN data frame */
+			int off = 0;
 			int i;
 
 			for (i = 0; i < frame->len; i++) {
-				snprintf(&local_txbuf[2 * i],
-					 sizeof(local_txbuf), "%02X",
-					 frame->data[i]);
+				off += scnprintf(&local_txbuf[off],
+						 sizeof(local_txbuf) - off,
+						 "%02X", frame->data[i]);
 			}
 
-			snprintf(&local_txbuf[2 * i], sizeof(local_txbuf),
-				 "\r");
+			scnprintf(&local_txbuf[off], sizeof(local_txbuf) - off, "\r");
 		}
 
 		elm->drop_next_line = 1;
-- 
2.45.2


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

end of thread, other threads:[~2024-11-19  0:49 UTC | newest]

Thread overview: 14+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2024-11-14  9:03 [PATCH net] can: can327: fix snprintf() limit in can327_handle_prompt() Dan Carpenter
2024-11-14  9:19 ` Max Staudt
2024-11-14  9:26   ` Dan Carpenter
2024-11-14  9:29     ` Dan Carpenter
2024-11-14 10:11       ` Max Staudt
2024-11-14  9:34 ` Vincent Mailhol
2024-11-14  9:57   ` Dan Carpenter
2024-11-14 12:35     ` Vincent Mailhol
2024-11-14 13:34       ` Marc Kleine-Budde
2024-11-14 14:54         ` Vincent Mailhol
2024-11-14 15:08           ` Dan Carpenter
2024-11-14 15:24             ` Vincent Mailhol
2024-11-14 15:42               ` Dan Carpenter
2024-11-19  0:48                 ` Max Staudt

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