[PATCH 3/4] bootconfig: Avoid out-of-bounds pointer arithmetic in xbc_snprint_cmdline_filter()
From: Masami Hiramatsu (Google)
Date: Sat Nov 21 2026 - 20:01:56 EST
From: Masami Hiramatsu (Google) <mhiramat@xxxxxxxxxx>
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@xxxxxxxxxxxxxxx
Signed-off-by: Masami Hiramatsu (Google) <mhiramat@xxxxxxxxxx>
---
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;