From: Joe Perches <joe@perches.com>
To: Sylvain Munaut <s.munaut@whatever-company.com>
Cc: torvalds@linux-foundation.org, linux-kernel@vger.kernel.org,
Kay Sievers <kay@vrfy.org>,
stable@kernel.org
Subject: Re: [PATCH] printk: Fix print_time length computation when no buffer is given
Date: Thu, 06 Dec 2012 18:40:15 -0800 [thread overview]
Message-ID: <1354848015.22463.17.camel@joe-AO722> (raw)
In-Reply-To: <1354813775-32451-1-git-send-email-s.munaut@whatever-company.com>
On Thu, 2012-12-06 at 18:09 +0100, Sylvain Munaut wrote:
> The "%5lu" part of the sprintf only guarantees a minimum length,
> not a maximum one. This patch should make it correct for any
> possible timestamp.
>
> This problem dates back from the printk conversion to records AFAICT.
>
> I've tested this locally both by verifying 'dmesg' no longer
> segfaulted and also by checking that before this patch the
> buffer was overrun by exactly the number of char corresponding to
> extra digits in the timestamps.
>
> Cc: Kay Sievers <kay@vrfy.org>
> Cc: stable@kernel.org
> Signed-off-by: Sylvain Munaut <s.munaut@whatever-company.com>
> ---
> kernel/printk.c | 13 +++++++++++--
> 1 file changed, 11 insertions(+), 2 deletions(-)
>
> diff --git a/kernel/printk.c b/kernel/printk.c
> index 66a2ea3..2ceceea 100644
> --- a/kernel/printk.c
> +++ b/kernel/printk.c
> @@ -847,8 +847,17 @@ static size_t print_time(u64 ts, char *buf)
> if (!printk_time)
> return 0;
>
> - if (!buf)
> - return 15;
> + if (!buf) {
> + size_t len = 15;
> + u64 ts_limit = (u64)100000 * (u64)1000000000;
> +
> + while ((ts > ts_limit) && (len < 21)) {
> + len++;
> + ts_limit = 10 * ts_limit;
> + }
Perhaps use snprintf for sizing something like below?
(untested/uncompiled)
static size_t print_time(u64 ts, char *buf)
{
unsigned long rem_nsec;
char *tmpbuf = buf;
int size = 15; /* expected size of non-null buf */
char local_buf[1];
if (!printk_time)
return 0;
if (!buf) { /* emit to a dummy buf instead */
tmpbuf = local_buf;
size = sizeof(local_buf);
}
rem_nsec = do_div(ts, 1000000000);
return snprintf(tmpbuf, size, "[%5lu.%06lu] ",
(unsigned long)ts, rem_nsec / 1000);
}
It's perhaps unfortunate that print_time doesn't take a
a buffer and a size_t of buffer.
prev parent reply other threads:[~2012-12-07 2:40 UTC|newest]
Thread overview: 5+ messages / expand[flat|nested] mbox.gz Atom feed top
2012-12-05 10:27 Wrong length / buffer overlow by the syslog syscall Sylvain Munaut
2012-12-05 11:26 ` Sylvain Munaut
2012-12-05 14:02 ` [RFC] printk: Fix print_time length computation when no buffer is given Sylvain Munaut
2012-12-06 17:09 ` [PATCH] " Sylvain Munaut
2012-12-07 2:40 ` Joe Perches [this message]
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=1354848015.22463.17.camel@joe-AO722 \
--to=joe@perches.com \
--cc=kay@vrfy.org \
--cc=linux-kernel@vger.kernel.org \
--cc=s.munaut@whatever-company.com \
--cc=stable@kernel.org \
--cc=torvalds@linux-foundation.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
all inboxes | Powered by JetHome®