From: "Masami Hiramatsu (Google)" <mhiramat@kernel.org>
To: Masami Hiramatsu <mhiramat@kernel.org>,
Steven Rostedt <rostedt@goodmis.org>,
Andrew Morton <akpm@linux-foundation.org>
Cc: linux-kernel@vger.kernel.org, linux-trace-kernel@vger.kernel.org
Subject: [PATCH 3/4] bootconfig: Avoid out-of-bounds pointer arithmetic in xbc_snprint_cmdline_filter()
Date: Sun, 22 Nov 2026 10:01:50 +0900 [thread overview]
Message-ID: <179530931041.109991.12049130742435464205.stgit@devnote2> (raw)
In-Reply-To: <179530928294.109991.3079999508446852151.stgit@devnote2>
From: Masami Hiramatsu (Google) <mhiramat@kernel.org>
xbc_snprint_cmdline_filter() passes `buf ? buf + len : NULL` to
snprintf(). While this avoids NULL pointer arithmetic when @buf is NULL,
if @buf is non-NULL and the rendered output exceeds @size (len > size),
evaluating `buf + len` forms a pointer past the end of the @buf
allocation, which is undefined behavior in C even though rest(len, size)
is 0.
Pass `buf && len < size ? buf + len : NULL` so that snprintf() receives
NULL whenever the remaining buffer capacity is 0.
Fixes: dec4d8118c17 ("bootconfig: fix NULL-pointer arithmetic in xbc_snprint_cmdline()")
Cc: stable@vger.kernel.org
Signed-off-by: Masami Hiramatsu (Google) <mhiramat@kernel.org>
---
lib/bootconfig.c | 7 ++++---
1 file changed, 4 insertions(+), 3 deletions(-)
diff --git a/lib/bootconfig.c b/lib/bootconfig.c
index 280b7e73321b..9ae9a215b1da 100644
--- a/lib/bootconfig.c
+++ b/lib/bootconfig.c
@@ -587,8 +587,8 @@ int __init xbc_snprint_cmdline_filter(char *buf, size_t size, struct xbc_node *r
vnode = xbc_node_get_child(knode);
if (!vnode) {
- ret = snprintf(buf ? buf + len : NULL, rest(len, size),
- "%s ", xbc_namebuf);
+ ret = snprintf(buf && len < size ? buf + len : NULL,
+ rest(len, size), "%s ", xbc_namebuf);
if (ret < 0)
return ret;
len += ret;
@@ -601,7 +601,8 @@ int __init xbc_snprint_cmdline_filter(char *buf, size_t size, struct xbc_node *r
* whitespace.
*/
q = strpbrk(val, " \t\r\n") ? "\"" : "";
- ret = snprintf(buf ? buf + len : NULL, rest(len, size),
+ ret = snprintf(buf && len < size ? buf + len : NULL,
+ rest(len, size),
"%s=%s%s%s ", xbc_namebuf, q, val, q);
if (ret < 0)
return ret;
next prev parent reply other threads:[~2026-11-22 1:01 UTC|newest]
Thread overview: 5+ messages / expand[flat|nested] mbox.gz Atom feed top
2026-11-22 1:01 [PATCH 0/4] bootconfig: Fix tree verification, key composition, and cmdline rendering bugs Masami Hiramatsu (Google)
2026-11-22 1:01 ` [PATCH 1/4] bootconfig: Fix xbc_verify_tree() to check subkeys of valued keys Masami Hiramatsu (Google)
2026-11-22 1:01 ` [PATCH 2/4] bootconfig: Fix xbc_node_compose_key_after() for value nodes Masami Hiramatsu (Google)
2026-11-22 1:01 ` Masami Hiramatsu (Google) [this message]
2026-11-22 1:01 ` [PATCH 4/4] tools/bootconfig: Add tests for unclosed brace position and mixed-kv limits Masami Hiramatsu (Google)
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=179530931041.109991.12049130742435464205.stgit@devnote2 \
--to=mhiramat@kernel.org \
--cc=akpm@linux-foundation.org \
--cc=linux-kernel@vger.kernel.org \
--cc=linux-trace-kernel@vger.kernel.org \
--cc=rostedt@goodmis.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®