From: Rasmus Villemoes <rasmus.villemoes@prevas.dk>
To: Arnd Bergmann <arnd@kernel.org>,
Dmitry Torokhov <dmitry.torokhov@gmail.com>
Cc: Arnd Bergmann <arnd@arndb.de>,
Zhang Qilong <zhangqilong3@huawei.com>,
linux-input@vger.kernel.org, linux-kernel@vger.kernel.org
Subject: Re: [PATCH] Input: analog - fix invalid snprintf() call
Date: Tue, 23 Mar 2021 14:29:15 +0100 [thread overview]
Message-ID: <a87d99c0-4527-1430-996b-b30826ecc752@prevas.dk> (raw)
In-Reply-To: <20210323131456.2600132-1-arnd@kernel.org>
On 23/03/2021 14.14, Arnd Bergmann wrote:
> From: Arnd Bergmann <arnd@arndb.de>
>
> overlapping input and output arguments to snprintf() are
> undefined behavior in C99:
>
Good luck:
https://lore.kernel.org/lkml/1457469654-17059-1-git-send-email-linux@rasmusvillemoes.dk/
At least 5 years ago the consensus from old-timers was that "the
kernel's snprintf supports this use case, just keep it working that way".
> diff --git a/drivers/input/joystick/analog.c b/drivers/input/joystick/analog.c
> index f798922a4598..8c9fed3f13e2 100644
> --- a/drivers/input/joystick/analog.c
> +++ b/drivers/input/joystick/analog.c
> @@ -419,14 +419,16 @@ static void analog_calibrate_timer(struct analog_port *port)
>
> static void analog_name(struct analog *analog)
> {
> - snprintf(analog->name, sizeof(analog->name), "Analog %d-axis %d-button",
> + int len;
> +
> + len = snprintf(analog->name, sizeof(analog->name), "Analog %d-axis %d-button",
> hweight8(analog->mask & ANALOG_AXES_STD),
> hweight8(analog->mask & ANALOG_BTNS_STD) + !!(analog->mask & ANALOG_BTNS_CHF) * 2 +
> hweight16(analog->mask & ANALOG_BTNS_GAMEPAD) + !!(analog->mask & ANALOG_HBTN_CHF) * 4);
>
> if (analog->mask & ANALOG_HATS_ALL)
> - snprintf(analog->name, sizeof(analog->name), "%s %d-hat",
> - analog->name, hweight16(analog->mask & ANALOG_HATS_ALL));
> + len += snprintf(analog->name + len, sizeof(analog->name) - len, "%d-hat",
> + hweight16(analog->mask & ANALOG_HATS_ALL));
Use scnprintf, this is too fragile and hard to verify. If the first
snprintf overflows, the second passes a huge size_t to snprintf which
will WARN.
Rasmus
next prev parent reply other threads:[~2021-03-23 13:30 UTC|newest]
Thread overview: 3+ messages / expand[flat|nested] mbox.gz Atom feed top
2021-03-23 13:14 [PATCH] Input: analog - fix invalid snprintf() call Arnd Bergmann
2021-03-23 13:29 ` Rasmus Villemoes [this message]
2021-03-23 18:37 ` Dmitry Torokhov
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=a87d99c0-4527-1430-996b-b30826ecc752@prevas.dk \
--to=rasmus.villemoes@prevas.dk \
--cc=arnd@arndb.de \
--cc=arnd@kernel.org \
--cc=dmitry.torokhov@gmail.com \
--cc=linux-input@vger.kernel.org \
--cc=linux-kernel@vger.kernel.org \
--cc=zhangqilong3@huawei.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