FAILED: patch "[PATCH] bootconfig: fix NULL-pointer arithmetic in" failed to apply to 6.1-stable tree
Subsystems (3)
[email protected]
maintainerThe patch below does not apply to the 6.1-stable tree.
If someone wants it applied there, or to any other stable or longterm tree, then please email the backport, including the original git commit id to <[email protected]>.
To reproduce the conflict and resubmit, you may use the following commands:
git fetch https://git.kernel.org/pub/scm/linux/kernel/git/stable/linux.git/ linux-6.1.y
git checkout FETCH_HEAD
git cherry-pick -x dec4d8118c179b3d12bca7e609054c6011c4f2ce
# <resolve conflicts, build, test, etc.>
git commit -s
git send-email --to '<[email protected]>' --in-reply-to '2026072113-streak-grandma-0f8a@gregkh' --subject-prefix 'PATCH 6.1.y' 'HEAD^..'
Possible dependencies:
thanks,
greg k-h
------------------ original commit in Linus's tree ------------------
From dec4d8118c179b3d12bca7e609054c6011c4f2ce Mon Sep 17 00:00:00 2001
From: Breno Leitao <[email protected]>
Date: Fri, 26 Jun 2026 05:50:10 -0700
Subject: [PATCH] bootconfig: fix NULL-pointer arithmetic in
xbc_snprint_cmdline()
xbc_snprint_cmdline() is meant to be called twice: first with buf=NULL, size=0 to probe the rendered length, then with a real buffer to fill it (the standard snprintf() two-pass pattern). The probe call makes the function compute "buf + size" (NULL + 0) and, on every iteration, advance "buf += ret" from that NULL base and pass the result back into snprintf().
Pointer arithmetic on a NULL pointer is undefined behavior. It is harmless in the in-kernel callers today, but the follow-up patches run this same code in the userspace tools/bootconfig parser at kernel build time, where host UBSan / FORTIFY_SOURCE abort the build.
Track a running written length (size_t) instead of mutating @buf, and only form "buf + len" when @buf is non-NULL. snprintf(NULL, 0, ...) is itself well defined and returns the would-be length, so the two-pass "probe then fill" usage returns identical byte counts.
lib/bootconfig.c +16 -7
@@ -427,10 +427,18 @@ static char xbc_namebuf[XBC_KEYLEN_MAX] __initdata;int __init xbc_snprint_cmdline (char *buf,size_t size,struct xbc_node *root) {struct xbc_node *knode, *vnode; -char *end = buf + size;const char *val, *q; +size_t len =0 ;int ret; +/* +* Track the running written length rather than advancing @buf, so we +* never form "buf + size" or "buf += ret" while @buf is NULL (the +* size-probe call passes buf=NULL, size=0). NULL pointer arithmetic +* is undefined behavior and trips host UBSan / FORTIFY_SOURCE when +* this renderer runs at kernel build time. snprintf(NULL, 0, ...) +* itself is well defined and returns the would-be length. +*/ xbc_node_for_each_key_value (root, knode, val) { ret =xbc_node_compose_key_after (root, knode, xbc_namebuf,XBC_KEYLEN_MAX );
@@ -439,10 +447,11 @@ int __init xbc_snprint_cmdline(char *buf, size_t size, struct xbc_node *root) vnode =xbc_node_get_child (knode);if (!vnode) { - ret =snprintf (buf,rest (buf, end),"%s " , xbc_namebuf); + ret =snprintf (buf ? buf + len :NULL ,rest (len, size), +"%s " , xbc_namebuf);if (ret <0 )return ret; - buf += ret; + len += ret;continue ; }xbc_array_for_each_value (vnode, val) {
@@ -452,15 +461,15 @@ int __init xbc_snprint_cmdline(char *buf, size_t size, struct xbc_node *root)* whitespace. */ q =strpbrk (val," \t\r\n" ) ?"\"" :"" ; - ret =snprintf (buf,rest (buf, end),"%s=%s%s%s " , - xbc_namebuf, q, val, q); + ret =snprintf (buf ? buf + len :NULL ,rest (len, size), +"%s=%s%s%s " , xbc_namebuf, q, val, q);if (ret <0 )return ret; - buf += ret; + len += ret; } } -return buf - (end - size); +return len; }#undef rest
Sasha Levin
maintainerPATCH 1/3 bootconfig: do not put quotes on cmdline items unless necessary
From: Rasmus Villemoes <[email protected]>
[ Upstream commit 212f863fa8811c780abacc1d0404c573fdc0a2de ]
When trying to migrate to using bootconfig to embed the kernel's and PID1's command line with the kernel image itself, and so allowing changing that without modifying the bootloader, I noticed that /proc/cmdline changed from e.g.
console=ttymxc0,115200n8 cma=128M quiet -- --log-level=notice
to
console="ttymxc0,115200n8" cma="128M" quiet -- --log-level="notice"
The kernel parameters are parsed just fine, and the quotes are indeed stripped from the actual argv[] given to PID1. However, the quoting doesn't really serve any purpose and looks excessive, and might confuse some (naive) userspace tool trying to parse /proc/cmdline. So do not quote the value unless it contains whitespace.
Stable-dep-of: dec4d8118c17 ("bootconfig: fix NULL-pointer arithmetic in xbc_snprint_cmdline()")
--- init/main.c | 12 +++++++++--- 1 file changed, 9 insertions(+), 3 deletions(-)
init/main.c +9 -3
@@ -332,7 +332,7 @@ static int __init xbc_snprint_cmdline(char *buf, size_t size, {struct xbc_node *knode, *vnode;char *end = buf + size; -const char *val; +const char *val, *q;int ret;xbc_node_for_each_key_value (root, knode, val) {
@@ -350,8 +350,14 @@ static int __init xbc_snprint_cmdline(char *buf, size_t size,continue ; }xbc_array_for_each_value (vnode, val) { - ret =snprintf (buf,rest (buf, end),"%s=\"%s\" " , - xbc_namebuf, val); +/* +* For prettier and more readable /proc/cmdline, only +* quote the value when necessary, i.e. when it contains +* whitespace. +*/ + q =strpbrk (val," \t\r\n" ) ?"\"" :"" ; + ret =snprintf (buf,rest (buf, end),"%s=%s%s%s " , + xbc_namebuf, q, val, q);if (ret <0 )return ret; buf += ret;
Signature · 1 line
2.53.0
Sasha Levin
maintainerPATCH 2/3 bootconfig: move xbc_snprint_cmdline() to lib/bootconfig.c
From: Breno Leitao <[email protected]>
[ Upstream commit 5a643e4623238e14b03d75ca0d4eda0645720cee ]
Move xbc_snprint_cmdline() from init/main.c to lib/bootconfig.c so the function (and its xbc_namebuf scratch buffer) becomes part of the shared parser library. tools/bootconfig already compiles lib/bootconfig.c directly, which lets a follow-up patch reuse the same renderer in the userspace tool to convert a bootconfig file into a flat cmdline string at build time.
No functional change.
Stable-dep-of: dec4d8118c17 ("bootconfig: fix NULL-pointer arithmetic in xbc_snprint_cmdline()")
--- include/linux/bootconfig.h | 3 ++ init/main.c | 45 ------------------------------ lib/bootconfig.c | 56 ++++++++++++++++++++++++++++++++++++++ 3 files changed, 59 insertions(+), 45 deletions(-)
include/linux/bootconfig.h +3 -0
@@ -264,6 +264,9 @@ static inline struct xbc_node * __init xbc_node_get_subkey(struct xbc_node *nodeint __init xbc_node_compose_key_after (struct xbc_node *root,struct xbc_node *node,char *buf,size_t size); +/* Render key/value pairs under @root as a flat cmdline string */ +int __init xbc_snprint_cmdline (char *buf,size_t size,struct xbc_node *root); +/** * xbc_node_compose_key() - Compose full key string of the XBC node * @node: An XBC node.
init/main.c +0 -45
51 lines not on this page · raw message
lib/bootconfig.c +56 -0
62 lines not on this page · raw message
Signature · 1 line
2.53.0
Sasha Levin
maintainerPATCH 3/3 bootconfig: fix NULL-pointer arithmetic in xbc_snprint_cmdline()
In reply to Sasha LevinFrom: Breno Leitao <[email protected]>
[ Upstream commit dec4d8118c179b3d12bca7e609054c6011c4f2ce ]
xbc_snprint_cmdline() is meant to be called twice: first with buf=NULL, size=0 to probe the rendered length, then with a real buffer to fill it (the standard snprintf() two-pass pattern). The probe call makes the function compute "buf + size" (NULL + 0) and, on every iteration, advance "buf += ret" from that NULL base and pass the result back into snprintf().
Pointer arithmetic on a NULL pointer is undefined behavior. It is harmless in the in-kernel callers today, but the follow-up patches run this same code in the userspace tools/bootconfig parser at kernel build time, where host UBSan / FORTIFY_SOURCE abort the build.
Track a running written length (size_t) instead of mutating @buf, and only form "buf + len" when @buf is non-NULL. snprintf(NULL, 0, ...) is itself well defined and returns the would-be length, so the two-pass "probe then fill" usage returns identical byte counts.
--- lib/bootconfig.c | 23 ++++++++++++++++------- 1 file changed, 16 insertions(+), 7 deletions(-)
lib/bootconfig.c +16 -7
@@ -426,10 +426,18 @@ static char xbc_namebuf[XBC_KEYLEN_MAX] __initdata;int __init xbc_snprint_cmdline (char *buf,size_t size,struct xbc_node *root) {struct xbc_node *knode, *vnode; -char *end = buf + size;const char *val, *q; +size_t len =0 ;int ret; +/* +* Track the running written length rather than advancing @buf, so we +* never form "buf + size" or "buf += ret" while @buf is NULL (the +* size-probe call passes buf=NULL, size=0). NULL pointer arithmetic +* is undefined behavior and trips host UBSan / FORTIFY_SOURCE when +* this renderer runs at kernel build time. snprintf(NULL, 0, ...) +* itself is well defined and returns the would-be length. +*/ xbc_node_for_each_key_value (root, knode, val) { ret =xbc_node_compose_key_after (root, knode, xbc_namebuf,XBC_KEYLEN_MAX );
@@ -438,10 +446,11 @@ int __init xbc_snprint_cmdline(char *buf, size_t size, struct xbc_node *root) vnode =xbc_node_get_child (knode);if (!vnode) { - ret =snprintf (buf,rest (buf, end),"%s " , xbc_namebuf); + ret =snprintf (buf ? buf + len :NULL ,rest (len, size), +"%s " , xbc_namebuf);if (ret <0 )return ret; - buf += ret; + len += ret;continue ; }xbc_array_for_each_value (vnode, val) {
@@ -451,15 +460,15 @@ int __init xbc_snprint_cmdline(char *buf, size_t size, struct xbc_node *root)* whitespace. */ q =strpbrk (val," \t\r\n" ) ?"\"" :"" ; - ret =snprintf (buf,rest (buf, end),"%s=%s%s%s " , - xbc_namebuf, q, val, q); + ret =snprintf (buf ? buf + len :NULL ,rest (len, size), +"%s=%s%s%s " , xbc_namebuf, q, val, q);if (ret <0 )return ret; - buf += ret; + len += ret; } } -return buf - (end - size); +return len; }#undef rest
Signature · 1 line
2.53.0