From: Rasmus Villemoes <ravi@prevas.dk>
To: Andy Shevchenko <andy.shevchenko@gmail.com>
Cc: Jiri Slaby <jirislaby@kernel.org>,
Bartosz Golaszewski <brgl@bgdev.pl>, Kees Cook <kees@kernel.org>,
Andy Shevchenko <andy@kernel.org>,
Andrew Morton <akpm@linux-foundation.org>,
James Bottomley <James.Bottomley@hansenpartnership.com>,
Greg KH <gregkh@linuxfoundation.org>,
linux-hardening@vger.kernel.org, linux-kernel@vger.kernel.org,
Bartosz Golaszewski <bartosz.golaszewski@linaro.org>,
stable@vger.kernel.org
Subject: Re: [PATCH] lib: string_helpers: fix potential snprintf() output truncation
Date: Tue, 22 Oct 2024 15:46:08 +0200 [thread overview]
Message-ID: <87y12gp7xb.fsf@prevas.dk> (raw)
In-Reply-To: <CAHp75Ve1WUKYmv6sfGZ6amujs=C7MnxauLM+C2MeW8vxBV1NfQ@mail.gmail.com> (Andy Shevchenko's message of "Tue, 22 Oct 2024 12:15:41 +0300")
On Tue, Oct 22 2024, Andy Shevchenko <andy.shevchenko@gmail.com> wrote:
> On Tue, Oct 22, 2024 at 10:15 AM Jiri Slaby <jirislaby@kernel.org> wrote:
>>
>> On 21. 10. 24, 12:04, Bartosz Golaszewski wrote:
>> > From: Bartosz Golaszewski <bartosz.golaszewski@linaro.org>
>> >
>> > The output of ".%03u" with the unsigned int in range [0, 4294966295] may
>> > get truncated if the target buffer is not 12 bytes.
>>
>> Perhaps, if you elaborate on how 'remainder' can become > 999?
>
> The problem here that we have a two-way road: on one hand we ought to
> fix the bugs in the kernel, on the other hand the compiler warnings
> (even false positives) better to be fixed as we don't know which
> compiler gets it fixed, but now we have a problem with building with
> `make W=1` for the default configurations (it prevents build due to
> compilation errors), so this change is definitely is an improvement.
Well, yes, kind of, and of course a 12 byte stack buffer doesn't hurt
more than an 8 byte one (i.e. not at all). However, it does feel rather
arbitrary.
Can't we fix the code to avoid the tmp buffer and do one less
snprintf()? Something like this entirely untested thing:
diff --git a/lib/string_helpers.c b/lib/string_helpers.c
index 9982344cca34..7aa592f9a494 100644
--- a/lib/string_helpers.c
+++ b/lib/string_helpers.c
@@ -50,13 +50,10 @@ void string_get_size(u64 size, u64 blk_size, const enum string_size_units units,
[STRING_UNITS_2] = 1024,
};
static const unsigned int rounding[] = { 500, 50, 5 };
- int i = 0, j;
+ int i = 0, j = 0;
u32 remainder = 0, sf_cap;
- char tmp[8];
const char *unit;
- tmp[0] = '\0';
-
if (blk_size == 0)
size = 0;
if (size == 0)
@@ -115,19 +112,16 @@ void string_get_size(u64 size, u64 blk_size, const enum string_size_units units,
size += 1;
}
- if (j) {
- snprintf(tmp, sizeof(tmp), ".%03u", remainder);
- tmp[j+1] = '\0';
- }
-
out:
if (i >= ARRAY_SIZE(units_2))
unit = "UNK";
else
unit = units_str[units][i];
- snprintf(buf, len, "%u%s %s", (u32)size,
- tmp, unit);
+ if (j)
+ snprintf(buf, len, "%u.%03u %s", (u32)size, remainder, unit);
+ else
+ snprintf(buf, len, "%u %s", (u32)size, unit);
}
EXPORT_SYMBOL(string_get_size);
Rasmus
next prev parent reply other threads:[~2024-10-22 13:46 UTC|newest]
Thread overview: 14+ messages / expand[flat|nested] mbox.gz Atom feed top
2024-10-21 10:04 [PATCH] lib: string_helpers: fix potential snprintf() output truncation Bartosz Golaszewski
2024-10-21 10:05 ` Bartosz Golaszewski
2024-10-22 7:15 ` Jiri Slaby
2024-10-22 7:30 ` Bartosz Golaszewski
2024-10-22 9:18 ` Andy Shevchenko
2024-10-22 11:07 ` David Laight
2024-10-22 9:15 ` Andy Shevchenko
2024-10-22 13:46 ` Rasmus Villemoes [this message]
2024-10-22 14:30 ` James Bottomley
-- strict thread matches above, loose matches on Subject: below --
2024-10-21 9:14 Bartosz Golaszewski
2024-10-21 9:25 ` Andy Shevchenko
2024-10-21 9:34 ` Greg KH
2024-10-21 9:36 ` Bartosz Golaszewski
2024-10-21 9:50 ` Greg KH
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=87y12gp7xb.fsf@prevas.dk \
--to=ravi@prevas.dk \
--cc=James.Bottomley@hansenpartnership.com \
--cc=akpm@linux-foundation.org \
--cc=andy.shevchenko@gmail.com \
--cc=andy@kernel.org \
--cc=bartosz.golaszewski@linaro.org \
--cc=brgl@bgdev.pl \
--cc=gregkh@linuxfoundation.org \
--cc=jirislaby@kernel.org \
--cc=kees@kernel.org \
--cc=linux-hardening@vger.kernel.org \
--cc=linux-kernel@vger.kernel.org \
--cc=stable@vger.kernel.org \
/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