FAILED: patch "[PATCH] tools/bootconfig: Fix integer overflow and truncation in size" failed to apply to 5.15-stable tree
Subsystems (2)
[email protected]
maintainerThe patch below does not apply to the 5.15-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.15.y
git checkout FETCH_HEAD
git cherry-pick -x 462d0b066b613103f579793031429db2ca23abc0
# <resolve conflicts, build, test, etc.>
git commit -s
git send-email --to '<[email protected]>' --in-reply-to '2026091616-brisket-appease-387d@gregkh' --subject-prefix 'PATCH 5.15.y' 'HEAD^..'
Possible dependencies:
thanks,
greg k-h
------------------ original commit in Linus's tree ------------------
From 462d0b066b613103f579793031429db2ca23abc0 Mon Sep 17 00:00:00 2001
From: "Masami Hiramatsu (Google)" <[email protected]>
Date: Fri, 11 Sep 2026 00:15:24 +0900
Subject: [PATCH] tools/bootconfig: Fix integer overflow and truncation in size
checks
Sashiko reported that on 32-bit systems, if an attacker crafts size in the bootconfig footer such that adding BOOTCONFIG_FOOTER_SIZE wraps around (for instance, if size is 0xFFFFFFFF), the size check in load_xbc_from_initrd() can be bypassed:
if (stat.st_size < size + BOOTCONFIG_FOOTER_SIZE) {
pr_err("bootconfig size is too big\n");
return -E2BIG;
}Furthermore, on 64-bit systems with an initrd > 4.29 GB, comparing a corrupted 32-bit size (e.g. 0xFFFFFFFF) against
stat.st_size - BOOTCONFIG_FOOTER_SIZE can also bypass the check if size is not bounded. Similarly, load_xbc_file() passes 64-bit stat.st_size directly into the 32-bit int size parameter of load_xbc_fd(), truncating large standalone files (>= 2GB).
In both cases, passing 0xFFFFFFFF to load_xbc_fd() truncates to -1, resulting in malloc(0), an integer overflow in read(), and an out-of-bounds null-byte write.
Fix this by:
1. Rejecting size > XBC_DATA_MAX or
size > stat.st_size - BOOTCONFIG_FOOTER_SIZE in load_xbc_from_initrd().
2. Rejecting stat.st_size > XBC_DATA_MAX in load_xbc_file() before passing it to load_xbc_fd().
3. Checking size < 0 || size > XBC_DATA_MAX defensively in load_xbc_fd().
tools/bootconfig/main.c +12 -1
@@ -140,6 +140,9 @@ static int load_xbc_fd(int fd, char **buf, int size) {int ret; +if (size <0 || size >XBC_DATA_MAX ) +return -EINVAL ; + *buf =malloc (size +1 );if (!*buf)return -ENOMEM ;
@@ -168,6 +171,13 @@ static int load_xbc_file(const char *path, char **buf)return ret; } +if (stat.st_size >XBC_DATA_MAX ) { +pr_err ("%s size is too big\n" , path); + ret = -E2BIG ; +close (fd); +return ret; + } + ret =load_xbc_fd (fd, buf, stat.st_size);close (fd);
@@ -218,7 +228,8 @@ static int load_xbc_from_initrd(int fd, char **buf) csum =le32toh (csum);/* Wrong size error */ -if (stat.st_size < size +BOOTCONFIG_FOOTER_SIZE ) { +if (size >XBC_DATA_MAX || + size > stat.st_size -BOOTCONFIG_FOOTER_SIZE ) {pr_err ("bootconfig size is too big\n" );return -E2BIG ; }
Sasha Levin
maintainerPATCH 1/4 tools/bootconfig: Print all error message in stderr
From: Masami Hiramatsu <[email protected]>
[ Upstream commit 160321b2602ff8e42ea77a09f2e3f1b35e3acfaf ]
Print all error message in stderr. This also removes unneeded tools/bootconfig/include/linux/printk.h.
Stable-dep-of: 462d0b066b61 ("tools/bootconfig: Fix integer overflow and truncation in size checks")
--- tools/bootconfig/include/linux/kernel.h | 2 -- tools/bootconfig/include/linux/printk.h | 14 -------------- tools/bootconfig/main.c | 2 ++ 3 files changed, 2 insertions(+), 16 deletions(-) delete mode 100644 tools/bootconfig/include/linux/printk.h
tools/bootconfig/include/linux/kernel.h +0 -2
@@ -5,8 +5,6 @@#include <stdlib.h> #include <stdbool.h> -#include <linux/printk.h> -typedef unsigned short u16 ;typedef unsigned int u32 ;
tools/bootconfig/include/linux/printk.h deleted +0 -14
@@ -1,14 +0,0 @@ -/* SPDX-License-Identifier: GPL-2.0 */ -#ifndef _SKC_LINUX_PRINTK_H -#define _SKC_LINUX_PRINTK_H - -#include <stdio.h> - -#define printk (fmt, ...) printf(fmt, ##__VA_ARGS__) - -#define pr_err printk -#define pr_warn printk -#define pr_info printk -#define pr_debug printk - -#endif
tools/bootconfig/main.c +2 -0
@@ -15,6 +15,8 @@#include <linux/kernel.h> #include <linux/bootconfig.h> +#define pr_err (fmt, ...) fprintf(stderr, fmt, ##__VA_ARGS__) +static int xbc_show_value (struct xbc_node *node,bool semicolon) {const char *val, *eol;
Signature · 1 line
2.53.0
Sasha Levin
maintainerPATCH 2/4 bootconfig: Replace u16 and u32 with uint16_t and uint32_t
From: Masami Hiramatsu <[email protected]>
[ Upstream commit 4f292c4886bfdfc2a7191ef4ff3f7aac69d1cc3f ]
Replace u16 and u32 with uint16_t and uint32_t so
that the tools/bootconfig only needs <stdint.h>.
Stable-dep-of: 462d0b066b61 ("tools/bootconfig: Fix integer overflow and truncation in size checks")
--- include/linux/bootconfig.h | 12 ++++++------ lib/bootconfig.c | 16 ++++++++-------- tools/bootconfig/include/linux/kernel.h | 4 +--- tools/bootconfig/main.c | 20 ++++++++++---------- 4 files changed, 25 insertions(+), 27 deletions(-)
include/linux/bootconfig.h +6 -6
@@ -25,10 +25,10 @@* The checksum will be used with the BOOTCONFIG_MAGIC and the size for * embedding the bootconfig in the initrd image. */ -static inline __init u32 xbc_calc_checksum (void *data,u32 size) +static inline __init uint32_t xbc_calc_checksum (void *data,uint32_t size) {unsigned char *p = data; -u32 ret =0 ; +uint32_t ret =0 ;while (size--) ret += *p++;
@@ -38,10 +38,10 @@ static inline __init u32 xbc_calc_checksum(void *data, u32 size)/* XBC tree node */ struct xbc_node { -u16 next; -u16 child; -u16 parent; -u16 data; +uint16_t next; +uint16_t child; +uint16_t parent; +uint16_t data; } __attribute__ ((__packed__ ));#define XBC_KEY 0
lib/bootconfig.c +8 -8
@@ -226,7 +226,7 @@ int __init xbc_node_compose_key_after(struct xbc_node *root,struct xbc_node *node, char *buf, size_t size) { - u16 keys[XBC_DEPTH_MAX]; + uint16_t keys[XBC_DEPTH_MAX];int depth =0 , ret =0 , total =0 ;if (!node || node == root)
@@ -406,21 +406,21 @@ int __init xbc_snprint_cmdline(char *buf, size_t size, struct xbc_node *root)/* XBC parse and tree build */ -static int __init xbc_init_node (struct xbc_node *node,char *data,u32 flag) +static int __init xbc_init_node (struct xbc_node *node,char *data,uint32_t flag) {unsigned long offset = data - xbc_data;if (WARN_ON (offset >=XBC_DATA_MAX ))return -EINVAL ; - node->data = (u16 )offset | flag; + node->data = (uint16_t )offset | flag; node->child =0 ; node->next =0 ;return 0 ; } -static struct xbc_node *__init xbc_add_node (char *data,u32 flag) +static struct xbc_node *__init xbc_add_node (char *data,uint32_t flag) {struct xbc_node *node;
@@ -450,7 +450,7 @@ static inline __init struct xbc_node *xbc_last_child(struct xbc_node *node)return node; } -static struct xbc_node *__init __xbc_add_sibling (char *data,u32 flag,bool head) +static struct xbc_node *__init __xbc_add_sibling (char *data,uint32_t flag,bool head) {struct xbc_node *sib, *node =xbc_add_node (data, flag);
@@ -477,17 +477,17 @@ static struct xbc_node * __init __xbc_add_sibling(char *data, u32 flag, bool heareturn node; } -static inline struct xbc_node *__init xbc_add_sibling (char *data,u32 flag) +static inline struct xbc_node *__init xbc_add_sibling (char *data,uint32_t flag) {return __xbc_add_sibling (data, flag, false); } -static inline struct xbc_node *__init xbc_add_head_sibling (char *data,u32 flag) +static inline struct xbc_node *__init xbc_add_head_sibling (char *data,uint32_t flag) {return __xbc_add_sibling (data, flag, true); } -static inline __init struct xbc_node *xbc_add_child (char *data,u32 flag) +static inline __init struct xbc_node *xbc_add_child (char *data,uint32_t flag) {struct xbc_node *node =xbc_add_sibling (data, flag);
tools/bootconfig/include/linux/kernel.h +1 -3
@@ -3,11 +3,9 @@#define _SKC_LINUX_KERNEL_H#include <stdlib.h> +#include <stdint.h> #include <stdbool.h> -typedef unsigned short u16 ; -typedef unsigned int u32 ; -#define unlikely (cond) (cond)#define __init
tools/bootconfig/main.c +10 -10
@@ -181,7 +181,7 @@ static int load_xbc_from_initrd(int fd, char **buf) {struct stat stat;int ret; -u32 size =0 , csum =0 , rcsum; +uint32_t size =0 , csum =0 , rcsum;char magic[BOOTCONFIG_MAGIC_LEN ];const char *msg;
@@ -205,11 +205,11 @@ static int load_xbc_from_initrd(int fd, char **buf)if (lseek (fd, -(8 +BOOTCONFIG_MAGIC_LEN ),SEEK_END ) <0 )return pr_errno ("Failed to lseek for size" , -errno); -if (read (fd, &size,sizeof (u32)) <0 ) +if (read (fd, &size,sizeof (uint32_t )) <0 )return pr_errno ("Failed to read size" , -errno); size =le32toh (size); -if (read (fd, &csum,sizeof (u32)) <0 ) +if (read (fd, &csum,sizeof (uint32_t )) <0 )return pr_errno ("Failed to read checksum" , -errno); csum =le32toh (csum);
@@ -367,7 +367,7 @@ static int apply_xbc(const char *path, const char *xbc_path)size_t total_size;struct stat stat;const char *msg; -u32 size, csum; +uint32_t size, csum;int pos, pad;int ret, fd;
@@ -381,7 +381,7 @@ static int apply_xbc(const char *path, const char *xbc_path)/* Backup the bootconfig data */ data =calloc (size +BOOTCONFIG_ALIGN + -sizeof (u32) +sizeof (u32) +BOOTCONFIG_MAGIC_LEN ,1 ); +sizeof (uint32_t ) +sizeof (uint32_t ) +BOOTCONFIG_MAGIC_LEN ,1 );if (!data)return -ENOMEM ;memcpy (data, buf, size);
@@ -428,17 +428,17 @@ static int apply_xbc(const char *path, const char *xbc_path) }/* To align up the total size to BOOTCONFIG_ALIGN, get padding size */ - total_size = stat.st_size + size +sizeof (u32) *2 +BOOTCONFIG_MAGIC_LEN ; + total_size = stat.st_size + size +sizeof (uint32_t ) *2 +BOOTCONFIG_MAGIC_LEN ; pad = ((total_size +BOOTCONFIG_ALIGN -1 ) & (~BOOTCONFIG_ALIGN_MASK )) - total_size; size += pad;/* Add a footer */ p = data + size; - *(u32 *)p =htole32 (size); - p +=sizeof (u32); + *(uint32_t *)p =htole32 (size); + p +=sizeof (uint32_t ); - *(u32 *)p =htole32 (csum); - p +=sizeof (u32); + *(uint32_t *)p =htole32 (csum); + p +=sizeof (uint32_t );memcpy (p,BOOTCONFIG_MAGIC ,BOOTCONFIG_MAGIC_LEN ); p +=BOOTCONFIG_MAGIC_LEN ;
Signature · 1 line
2.53.0
Sasha Levin
maintainerPATCH 3/4 tools/bootconfig: Cleanup bootconfig footer size calculations
In reply to Sasha LevinFrom: "Masami Hiramatsu (Google)" <[email protected]>
[ Upstream commit 26dda57695090e05c1a99c3e8f802f862d1ac474 ]
There are many same pattern of 8 + BOOTCONFIG_MAGIC_LEN for calculating the size of bootconfig footer. Use BOOTCONFIG_FOOTER_SIZE macro to clean up those magic numbers.
Stable-dep-of: 462d0b066b61 ("tools/bootconfig: Fix integer overflow and truncation in size checks")
--- tools/bootconfig/main.c | 19 +++++++++++-------- 1 file changed, 11 insertions(+), 8 deletions(-)
tools/bootconfig/main.c +11 -8
@@ -17,6 +17,10 @@#define pr_err (fmt, ...) fprintf(stderr, fmt, ##__VA_ARGS__) +/* Bootconfig footer is [size][csum][BOOTCONFIG_MAGIC]. */ +#define BOOTCONFIG_FOOTER_SIZE \ + (sizeof(uint32_t) * 2 + BOOTCONFIG_MAGIC_LEN) +static int xbc_show_value (struct xbc_node *node,bool semicolon) {const char *val, *eol;
@@ -189,7 +193,7 @@ static int load_xbc_from_initrd(int fd, char **buf)if (ret <0 )return -errno; -if (stat.st_size <8 +BOOTCONFIG_MAGIC_LEN ) +if (stat.st_size <BOOTCONFIG_FOOTER_SIZE )return 0 ;if (lseek (fd, -BOOTCONFIG_MAGIC_LEN ,SEEK_END ) <0 )
@@ -202,7 +206,7 @@ static int load_xbc_from_initrd(int fd, char **buf)if (memcmp (magic,BOOTCONFIG_MAGIC ,BOOTCONFIG_MAGIC_LEN ) !=0 )return 0 ; -if (lseek (fd, -(8 +BOOTCONFIG_MAGIC_LEN ),SEEK_END ) <0 ) +if (lseek (fd, -BOOTCONFIG_FOOTER_SIZE ,SEEK_END ) <0 )return pr_errno ("Failed to lseek for size" , -errno);if (read (fd, &size,sizeof (uint32_t )) <0 )
@@ -214,12 +218,12 @@ static int load_xbc_from_initrd(int fd, char **buf) csum =le32toh (csum);/* Wrong size error */ -if (stat.st_size < size +8 +BOOTCONFIG_MAGIC_LEN ) { +if (stat.st_size < size +BOOTCONFIG_FOOTER_SIZE ) {pr_err ("bootconfig size is too big\n" );return -E2BIG ; } -if (lseek (fd, stat.st_size - (size +8 +BOOTCONFIG_MAGIC_LEN ), +if (lseek (fd, stat.st_size - (size +BOOTCONFIG_FOOTER_SIZE ),SEEK_SET ) <0 )return pr_errno ("Failed to lseek" , -errno);
@@ -350,7 +354,7 @@ static int delete_xbc(const char *path) ret =fstat (fd, &stat);if (!ret) ret =ftruncate (fd, stat.st_size - - size -8 -BOOTCONFIG_MAGIC_LEN ); + - size -BOOTCONFIG_FOOTER_SIZE );if (ret) ret = -errno; }/* Ignore if there is no boot config in initrd */
@@ -380,8 +384,7 @@ static int apply_xbc(const char *path, const char *xbc_path) csum =xbc_calc_checksum (buf, size);/* Backup the bootconfig data */ - data =calloc (size +BOOTCONFIG_ALIGN + -sizeof (uint32_t ) +sizeof (uint32_t ) +BOOTCONFIG_MAGIC_LEN ,1 ); + data =calloc (size +BOOTCONFIG_ALIGN +BOOTCONFIG_FOOTER_SIZE ,1 );if (!data)return -ENOMEM ;memcpy (data, buf, size);
@@ -428,7 +431,7 @@ static int apply_xbc(const char *path, const char *xbc_path) }/* To align up the total size to BOOTCONFIG_ALIGN, get padding size */ - total_size = stat.st_size + size +sizeof (uint32_t ) *2 +BOOTCONFIG_MAGIC_LEN ; + total_size = stat.st_size + size +BOOTCONFIG_FOOTER_SIZE ; pad = ((total_size +BOOTCONFIG_ALIGN -1 ) & (~BOOTCONFIG_ALIGN_MASK )) - total_size; size += pad;
Signature · 1 line
2.53.0
Sasha Levin
maintainerPATCH 4/4 tools/bootconfig: Fix integer overflow and truncation in size checks
In reply to Sasha LevinFrom: "Masami Hiramatsu (Google)" <[email protected]>
[ Upstream commit 462d0b066b613103f579793031429db2ca23abc0 ]
Sashiko reported that on 32-bit systems, if an attacker crafts size in the bootconfig footer such that adding BOOTCONFIG_FOOTER_SIZE wraps around (for instance, if size is 0xFFFFFFFF), the size check in load_xbc_from_initrd() can be bypassed:
if (stat.st_size < size + BOOTCONFIG_FOOTER_SIZE) {
pr_err("bootconfig size is too big\n");
return -E2BIG;
}Furthermore, on 64-bit systems with an initrd > 4.29 GB, comparing a corrupted 32-bit size (e.g. 0xFFFFFFFF) against
stat.st_size - BOOTCONFIG_FOOTER_SIZE can also bypass the check if size is not bounded. Similarly, load_xbc_file() passes 64-bit stat.st_size directly into the 32-bit int size parameter of load_xbc_fd(), truncating large standalone files (>= 2GB).
In both cases, passing 0xFFFFFFFF to load_xbc_fd() truncates to -1, resulting in malloc(0), an integer overflow in read(), and an out-of-bounds null-byte write.
Fix this by:
1. Rejecting size > XBC_DATA_MAX or
size > stat.st_size - BOOTCONFIG_FOOTER_SIZE in load_xbc_from_initrd().
2. Rejecting stat.st_size > XBC_DATA_MAX in load_xbc_file() before passing it to load_xbc_fd().
3. Checking size < 0 || size > XBC_DATA_MAX defensively in load_xbc_fd().
--- tools/bootconfig/main.c | 13 ++++++++++++- 1 file changed, 12 insertions(+), 1 deletion(-)
tools/bootconfig/main.c +12 -1
@@ -140,6 +140,9 @@ static int load_xbc_fd(int fd, char **buf, int size) {int ret; +if (size <0 || size >XBC_DATA_MAX ) +return -EINVAL ; + *buf =malloc (size +1 );if (!*buf)return -ENOMEM ;
@@ -168,6 +171,13 @@ static int load_xbc_file(const char *path, char **buf)return ret; } +if (stat.st_size >XBC_DATA_MAX ) { +pr_err ("%s size is too big\n" , path); + ret = -E2BIG ; +close (fd); +return ret; + } + ret =load_xbc_fd (fd, buf, stat.st_size);close (fd);
@@ -218,7 +228,8 @@ static int load_xbc_from_initrd(int fd, char **buf) csum =le32toh (csum);/* Wrong size error */ -if (stat.st_size < size +BOOTCONFIG_FOOTER_SIZE ) { +if (size >XBC_DATA_MAX || + size > stat.st_size -BOOTCONFIG_FOOTER_SIZE ) {pr_err ("bootconfig size is too big\n" );return -E2BIG ; }
Signature · 1 line
2.53.0