Skip to contentSkip to conversation

can: slcan: use scnprintf() as a hardening measure

PATCH New Dan Carpenter 1 participant 1 message Jul 6, 2022

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

PATCH 1/1 can: slcan: use scnprintf() as a hardening measure

The snprintf() function returns the number of bytes which *would* have been copied if there were no space. So, since this code does not check the return value, there if the buffer was not large enough then there would be a buffer overflow two lines later when it does:

	actual = sl->tty->ops->write(sl->tty, sl->xbuff, n);

Use scnprintf() instead because that returns the number of bytes which were actually copied.

Fixes 52f9ac85b876 ("can: slcan: allow to send commands to the adapter")
---
 drivers/net/can/slcan/slcan-core.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
drivers/net/can/slcan/slcan-core.c +1 -1
index 54d29a410ad5..92bdd49996d1 100644
@@ -647,7 +647,7 @@ static int slcan_transmit_cmd(struct slcan *sl, const unsigned char *cmd)
 		return -ENODEV;
 	}
 
-	n = snprintf(sl->xbuff, sizeof(sl->xbuff), "%s", cmd);
+	n = scnprintf(sl->xbuff, sizeof(sl->xbuff), "%s", cmd);
 	set_bit(TTY_DO_WRITE_WAKEUP, &sl->tty->flags);
 	actual = sl->tty->ops->write(sl->tty, sl->xbuff, n);
 	sl->xleft = n - actual;
Signature · 1 line
2.35.1