From: Simon Horman <horms@kernel.org>
To: litenglong <litenglong@kylinos.cn>
Cc: kexec@lists.infradead.org
Subject: Re: [PATCH] Fix buffer overflow in dump_dmesg_structured and dump_record
Date: Tue, 1 Dec 2026 20:04:02 +0000 [thread overview]
Message-ID: <20261201200402.GB456681@horms.kernel.org> (raw)
In-Reply-To: <20261112131022.1076471-1-litenglong@kylinos.cn>
On Thu, Nov 12, 2026 at 09:10:23PM +0800, litenglong wrote:
> Both functions accumulate output across multiple dmesg records into a
> fixed-size stack buffer out_buf[OUT_BUF_SIZE] (4096 bytes) via sprintf
> with a cumulative offset that is never reset across loop iterations.
>
> The hex-byte escape loops already have a post-write flush guard at each
> character boundary (len >= OUT_BUF_SIZE - 64), which was present in the
> original upstream code. However, two write sites in each function lacked
> any bounds check:
>
> dump_dmesg_structured:
> - timestamp prefix sprintf: len += sprintf(out_buf + len, "[%5llu.%06llu] ", ...)
> - trailing newline: out_buf[len++] = '\n'
>
> dump_record:
> - timestamp prefix sprintf: offset += sprintf(out_buf + offset, "[%5llu.%06llu] ", ...)
> - trailing newline at out:: out_buf[offset++] = '\n'
>
> On a long log record, the cumulative offset can approach OUT_BUF_SIZE.
> Without a guard before these writes, the timestamp sprintf (up to ~30
> bytes) or the newline byte can write past out_buf[OUT_BUF_SIZE - 1],
> causing a stack buffer overflow.
>
> Fix: add a flush-and-reset guard before each of the four unprotected
> write sites, using the same threshold (OUT_BUF_SIZE - 64) as the
> existing loop guards. When the buffer is nearly full, the accumulated
> output is emitted via the handler callback and the offset resets to
> zero, so subsequent writes start from the beginning of the buffer.
>
> The 64-byte margin is sufficient: the timestamp prefix is at most ~30
> bytes, and the newline is a single byte. The loop guards continue to
> protect the character-by-character escape writes as before.
>
> Signed-off-by: litenglong <litenglong@kylinos.cn>
Thanks, applied.
- Fix buffer overflow in dump_dmesg_structured and dump_record
https://git.kernel.org/pub/scm/utils/kernel/kexec/kexec-tools.git/commit/?id=57fc3341e3e0
prev parent reply other threads:[~2026-12-01 20:04 UTC|newest]
Thread overview: 2+ messages / expand[flat|nested] mbox.gz Atom feed top
2026-11-12 13:10 [PATCH] Fix buffer overflow in dump_dmesg_structured and dump_record litenglong
2026-12-01 20:04 ` Simon Horman [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=20261201200402.GB456681@horms.kernel.org \
--to=horms@kernel.org \
--cc=kexec@lists.infradead.org \
--cc=litenglong@kylinos.cn \
/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 an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.