From: Vincent Mailhol <mailhol.vincent@wanadoo.fr>
To: Dan Carpenter <dan.carpenter@linaro.org>, Max Staudt <max@enpas.org>
Cc: Marc Kleine-Budde <mkl@pengutronix.de>,
Andrew Lunn <andrew+netdev@lunn.ch>,
"David S. Miller" <davem@davemloft.net>,
Eric Dumazet <edumazet@google.com>,
Jakub Kicinski <kuba@kernel.org>, Paolo Abeni <pabeni@redhat.com>,
linux-can@vger.kernel.org, netdev@vger.kernel.org,
linux-kernel@vger.kernel.org, kernel-janitors@vger.kernel.org
Subject: Re: [PATCH net] can: can327: fix snprintf() limit in can327_handle_prompt()
Date: Thu, 14 Nov 2024 18:34:49 +0900 [thread overview]
Message-ID: <6db4d783-6db2-4b86-887c-3c95d6763774@wanadoo.fr> (raw)
In-Reply-To: <c896ba5d-7147-4978-9e25-86cfd88ff9dc@stanley.mountain>
Hi Dan,
On 14/11/2024 at 18:03, Dan Carpenter wrote:
> 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.
But, can frame->len be too long? Classical CAN frame maximum length is 8
bytes. And I do not see a path for a malformed frame to reach this part
of the driver.
If such a path exists, I think this should be explained. Else, I am just
not sure if this needs a Fixes: tag.
> 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;
Yours sincerely,
Vincent Mailhol
next prev parent reply other threads:[~2024-11-14 9:34 UTC|newest]
Thread overview: 14+ messages / expand[flat|nested] mbox.gz Atom feed top
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 [this message]
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
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=6db4d783-6db2-4b86-887c-3c95d6763774@wanadoo.fr \
--to=mailhol.vincent@wanadoo.fr \
--cc=andrew+netdev@lunn.ch \
--cc=dan.carpenter@linaro.org \
--cc=davem@davemloft.net \
--cc=edumazet@google.com \
--cc=kernel-janitors@vger.kernel.org \
--cc=kuba@kernel.org \
--cc=linux-can@vger.kernel.org \
--cc=linux-kernel@vger.kernel.org \
--cc=max@enpas.org \
--cc=mkl@pengutronix.de \
--cc=netdev@vger.kernel.org \
--cc=pabeni@redhat.com \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox