FAILED: patch "[PATCH] bootconfig: Fix integer overflow in initrd size check" failed to apply to 5.10-stable tree
[email protected]
maintainerThe patch below does not apply to the 5.10-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-5.10.y
git checkout FETCH_HEAD
git cherry-pick -x 7812d6dab0698001e50e8c2f901e17da3eb6f429
# <resolve conflicts, build, test, etc.>
git commit -s
git send-email --to '<[email protected]>' --in-reply-to '2026091618-used-flashbulb-70ef@gregkh' --subject-prefix 'PATCH 5.10.y' 'HEAD^..'
Possible dependencies:
thanks,
greg k-h
------------------ original commit in Linus's tree ------------------
From 7812d6dab0698001e50e8c2f901e17da3eb6f429 Mon Sep 17 00:00:00 2001
From: "Masami Hiramatsu (Google)" <[email protected]>
Date: Fri, 11 Sep 2026 00:15:34 +0900
Subject: [PATCH] bootconfig: Fix integer overflow in initrd size check
Sashiko reported that in get_boot_config_from_initrd(), a crafted initrd with a huge bootconfig size (such as 0xFFFFFFFF) can cause the pointer arithmetic:
data = ((void *)hdr) - size;
to wrap around on 32-bit systems (or when pointer subtraction overflows). Because data wraps around, the subsequent bounds check:
if ((unsigned long)data < initrd_start)
evaluates to false, bypassing the check. The kernel then calls xbc_calc_checksum(data, size), which attempts to read 4GB of memory, hitting unmapped pages and triggering a fatal kernel page fault during early boot. Furthermore, on 64-bit systems with an initrd > 4.29 GB, an unbounded 32-bit size can similarly bypass the initrd_start check.
Fix this by:
1. Ensuring the initrd is at least large enough to contain the bootconfig footer and verifying hdr is within the initrd bounds.
2. Checking that size does not exceed XBC_DATA_MAX and does not exceed the available space between initrd_start and hdr before performing pointer subtraction.
init/main.c +15 -10
@@ -277,7 +277,8 @@ static void * __init get_boot_config_from_initrd(size_t *_size)u8 *hdr;int i; -if (!initrd_end) +if (!initrd_end || initrd_end < initrd_start || + initrd_end - initrd_start <BOOTCONFIG_MAGIC_LEN +8 )return NULL ; data = (char *)initrd_end -BOOTCONFIG_MAGIC_LEN ;
@@ -294,16 +295,26 @@ static void * __init get_boot_config_from_initrd(size_t *_size) found: hdr = (u8 *)(data -8 ); +if ((unsigned long )hdr < initrd_start) +return NULL ; + size =get_unaligned_le32 (hdr); csum =get_unaligned_le32 (hdr +4 ); - data = ((void *)hdr) - size; -if ((unsigned long )data < initrd_start) { -pr_err ("bootconfig size %d is greater than initrd size %ld\n" , +if (size >XBC_DATA_MAX ) { +pr_err ("bootconfig size %u is greater than max size %d\n" , + size,XBC_DATA_MAX ); +return NULL ; + } + +if (size > ((unsigned long )hdr - initrd_start)) { +pr_err ("bootconfig size %u is greater than initrd size %lu\n" , size, initrd_end - initrd_start);return NULL ; } + data = ((void *)hdr) - size; +if (xbc_calc_checksum (data, size) != csum) {pr_err ("bootconfig checksum failed\n" );return NULL ;
@@ -394,12 +405,6 @@ static void __init setup_boot_config(void)return ; } -if (size >=XBC_DATA_MAX ) { -pr_err ("bootconfig size %ld greater than max size %d\n" , - (long )size,XBC_DATA_MAX ); -return ; - } - ret =xbc_init (data, size, &msg, &pos);if (ret <0 ) {if (pos <0 )
Sasha Levin
maintainerPATCH 1/4 ns: move to_ns_common() to ns_common.h
From: Christian Brauner <[email protected]>
[ Upstream commit e3e1812f8e25ac277f5cc9249802365300c582e3 ]
Move the helper to ns_common.h where it belongs.
Stable-dep-of: 7812d6dab069 ("bootconfig: Fix integer overflow in initrd size check")
--- include/linux/ns_common.h | 20 ++++++++++++++++++++ 1 file changed, 20 insertions(+)
include/linux/ns_common.h +20 -0
@@ -4,10 +4,30 @@struct proc_ns_operations ; +struct cgroup_namespace ; +struct ipc_namespace ; +struct mnt_namespace ; +struct net ; +struct pid_namespace ; +struct time_namespace ; +struct user_namespace ; +struct uts_namespace ; +struct ns_common {atomic_long_t stashed;const struct proc_ns_operations *ops;unsigned int inum; }; +#define to_ns_common (__ns) \ + _Generic((__ns), \ + struct cgroup_namespace *: &(__ns)->ns, \ + struct ipc_namespace *: &(__ns)->ns, \ + struct mnt_namespace *: &(__ns)->ns, \ + struct net *: &(__ns)->ns, \ + struct pid_namespace *: &(__ns)->ns, \ + struct time_namespace *: &(__ns)->ns, \ + struct user_namespace *: &(__ns)->ns, \ + struct uts_namespace *: &(__ns)->ns) +#endif
Signature · 1 line
2.53.0
Sasha Levin
maintainerPATCH 2/4 ns: uniformly initialize ns_common
From: Christian Brauner <[email protected]>
[ Upstream commit 660def10b01b248fd97255afacb7b0e305ac833a ]
No point in cargo-culting the same code across all the different types. Use one common initializer.
Stable-dep-of: 7812d6dab069 ("bootconfig: Fix integer overflow in initrd size check")
--- include/linux/proc_ns.h | 16 ++++++++++++++++ 1 file changed, 16 insertions(+)
include/linux/proc_ns.h +16 -0
@@ -70,6 +70,22 @@ static inline int ns_alloc_inum(struct ns_common *ns)return proc_alloc_inum (&ns->inum); } +static inline int ns_common_init (struct ns_common *ns, +const struct proc_ns_operations *ops, +bool alloc_inum) +{ +if (alloc_inum) { +int ret; + ret =proc_alloc_inum (&ns->inum); +if (ret) +return ret; + } +refcount_set (&ns->count,1 ); + ns->stashed =NULL ; + ns->ops = ops; +return 0 ; +} +#define ns_free_inum (ns) proc_free_inum((ns)->inum)extern struct file *proc_ns_fget (int fd);
Signature · 1 line
2.53.0
Sasha Levin
maintainerPATCH 3/4 init/main: read bootconfig header with get_unaligned_le32()
In reply to Sasha LevinFrom: Sun Jian <[email protected]>
[ Upstream commit 499f86de4f8c34e19a57daf2b6f0cba848e91994 ]
get_boot_config_from_initrd() scans up to 3 bytes before initrd_end to handle GRUB 4-byte alignment. As a result, the bootconfig header immediately preceding the magic may be unaligned.
Read the size and checksum fields with get_unaligned_le32() instead of casting to u32 * and using le32_to_cpu(), avoiding potential unaligned access and silencing sparse "cast to restricted __le32" warnings.
Sparse warnings (gcc + C=1):
init/main.c:292:16: warning: cast to restricted __le32 init/main.c:293:16: warning: cast to restricted __le32
No functional change intended.
Stable backport notes:
- Use <asm/unaligned.h>, the unaligned-access header provided by 5.10.
- Reuse and relocate the existing boot_config_checksum() implementation as xbc_calc_checksum(), and validate the checksum before trimming the initrd, as in 765b8552a200. No additional function is introduced.
- Adapt the existing xbc_init() and its callers to the explicit-size, internally owned buffer interface from bdac5c2b243f. Use the 5.10 memblock_free(__pa(...), ...) API, handle partial allocation cleanup, and initialize the parser result after removing the strlen() assignment.
- Repair the inherited ns_common_init() from b033981390f7: 5.10 has no common count field and stashed is atomic_long_t, so initialize it with atomic_long_set() and leave per-namespace reference counts alone.
- Match the size-check diagnostic context so target 7812d6dab069 applies unchanged. Its overflow and footer-bounds checks remain in the target.
These adaptations bridge the older bootconfig implementation without pulling in unrelated modern init headers or adding functions.
Stable-dep-of: 7812d6dab069 ("bootconfig: Fix integer overflow in initrd size check")
--- include/linux/bootconfig.h | 2 +- include/linux/proc_ns.h | 3 +- init/main.c | 69 +++++++++++++++++--------------------- lib/bootconfig.c | 41 ++++++++++++++-------- tools/bootconfig/main.c | 6 ++-- 5 files changed, 62 insertions(+), 59 deletions(-)
include/linux/bootconfig.h +1 -1
@@ -222,7 +222,7 @@ static inline int __init xbc_node_compose_key(struct xbc_node *node, }/* XBC node initializer */ -int __init xbc_init (char *buf,const char **emsg,int *epos); +int __init xbc_init (const char *data,size_t size,const char **emsg,int *epos);/* XBC cleanup data structures */
include/linux/proc_ns.h +1 -2
@@ -80,8 +80,7 @@ static inline int ns_common_init(struct ns_common *ns,if (ret)return ret; } -refcount_set (&ns->count,1 ); - ns->stashed =NULL ; +atomic_long_set (&ns->stashed,0 ); ns->ops = ops;return 0 ; }
init/main.c +30 -39
134 lines not on this page · raw message
lib/bootconfig.c +27 -14
86 lines not on this page · raw message
tools/bootconfig/main.c +3 -3
@@ -214,7 +214,7 @@ static int load_xbc_from_initrd(int fd, char **buf)return -EINVAL ; } - ret =xbc_init (*buf, &msg,NULL ); + ret =xbc_init (*buf, size, &msg,NULL );/* Wrong data */ if (ret <0 ) {pr_err ("parse error: %s.\n" , msg);
@@ -254,7 +254,7 @@ static int init_xbc_with_error(char *buf, int len)if (!copy)return -ENOMEM ; - ret =xbc_init (buf, &msg, &pos); + ret =xbc_init (buf, len, &msg, &pos);if (ret <0 )show_xbc_error (copy, msg, pos);free (copy);
@@ -367,7 +367,7 @@ static int apply_xbc(const char *path, const char *xbc_path)memcpy (data, buf, size);/* Check the data format */ - ret =xbc_init (buf, &msg, &pos); + ret =xbc_init (buf, size, &msg, &pos);if (ret <0 ) {show_xbc_error (data, msg, pos);free (data);
Signature · 1 line
2.53.0
Sasha Levin
maintainerPATCH 4/4 bootconfig: Fix integer overflow in initrd size check
In reply to Sasha LevinFrom: "Masami Hiramatsu (Google)" <[email protected]>
[ Upstream commit 7812d6dab0698001e50e8c2f901e17da3eb6f429 ]
Sashiko reported that in get_boot_config_from_initrd(), a crafted initrd with a huge bootconfig size (such as 0xFFFFFFFF) can cause the pointer arithmetic:
data = ((void *)hdr) - size;
to wrap around on 32-bit systems (or when pointer subtraction overflows). Because data wraps around, the subsequent bounds check:
if ((unsigned long)data < initrd_start)
evaluates to false, bypassing the check. The kernel then calls xbc_calc_checksum(data, size), which attempts to read 4GB of memory, hitting unmapped pages and triggering a fatal kernel page fault during early boot. Furthermore, on 64-bit systems with an initrd > 4.29 GB, an unbounded 32-bit size can similarly bypass the initrd_start check.
Fix this by:
1. Ensuring the initrd is at least large enough to contain the bootconfig footer and verifying hdr is within the initrd bounds.
2. Checking that size does not exceed XBC_DATA_MAX and does not exceed the available space between initrd_start and hdr before performing pointer subtraction.
--- init/main.c | 25 +++++++++++++++---------- 1 file changed, 15 insertions(+), 10 deletions(-)
init/main.c +15 -10
@@ -281,7 +281,8 @@ static void * __init get_boot_config_from_initrd(u32 *_size)u8 *hdr;int i; -if (!initrd_end) +if (!initrd_end || initrd_end < initrd_start || + initrd_end - initrd_start <BOOTCONFIG_MAGIC_LEN +8 )return NULL ; data = (char *)initrd_end -BOOTCONFIG_MAGIC_LEN ;
@@ -298,16 +299,26 @@ static void * __init get_boot_config_from_initrd(u32 *_size) found: hdr = (u8 *)(data -8 ); +if ((unsigned long )hdr < initrd_start) +return NULL ; + size =get_unaligned_le32 (hdr); csum =get_unaligned_le32 (hdr +4 ); - data = ((void *)hdr) - size; -if ((unsigned long )data < initrd_start) { -pr_err ("bootconfig size %d is greater than initrd size %ld\n" , +if (size >XBC_DATA_MAX ) { +pr_err ("bootconfig size %u is greater than max size %d\n" , + size,XBC_DATA_MAX ); +return NULL ; + } + +if (size > ((unsigned long )hdr - initrd_start)) { +pr_err ("bootconfig size %u is greater than initrd size %lu\n" , size, initrd_end - initrd_start);return NULL ; } + data = ((void *)hdr) - size; +if (xbc_calc_checksum (data, size) != csum) {pr_err ("bootconfig checksum failed\n" );return NULL ;
@@ -398,12 +409,6 @@ static void __init setup_boot_config(const char *cmdline)return ; } -if (size >=XBC_DATA_MAX ) { -pr_err ("bootconfig size %ld greater than max size %d\n" , - (long )size,XBC_DATA_MAX ); -return ; - } - ret =xbc_init (data, size, &msg, &pos);if (ret <0 ) {if (pos <0 )
Signature · 1 line
2.53.0