bootconfig: Reject unexpected data after null character and cleanups
Subsystems (3)
Masami Hiramatsu (Google)
maintainercover bootconfig: Reject unexpected data after null character and cleanups
Hi,
Here are patches for bootconfig to rejects unexpected config data after null character and other cleanups including tools/bootconfig to consolidate bootconfig initialization with errors, and skipping internal tree sanity check in kernel.
The last one is initially reported by Sang-Heon Jeon [1].
[1] https://lore.kernel.org/all/[email protected]/
Thank you,
---
base-commit: 13e8b7707efcea3a401d242958ba0a392f227847
Masami Hiramatsu (Google) (3):
bootconfig: Reject unexpected data after null character tools/bootconfig: Consolidate xbc_init() to error message wrapper
bootconfig: Skip internal tree sanity checks in kernel
lib/bootconfig.c | 45 ++++++++++---- tools/bootconfig/main.c | 112 +++++++++++++++++------------------ tools/bootconfig/test-bootconfig.sh | 12 ++++ 3 files changed, 99 insertions(+), 70 deletions(-)
--
Masami Hiramatsu (Google) <[email protected]>
Masami Hiramatsu (Google)
maintainerPATCH 1/3 bootconfig: Reject unexpected data after null character
From: Masami Hiramatsu (Google) <[email protected]>
If a bootconfig buffer contains an intermediate null character in the middle of the configuration, xbc_parse_tree() stops at the null character because string delimiter searches (e.g. strpbrk()) stop at '\0', and cleanly breaks out of the loop without error. As a result, any configuration data following the intermediate null character is silently ignored, allowing unparsed or potentially malicious data to be hidden after an early termination.
Fix this in xbc_parse_tree() by checking that no non-null data remains between the parser termination point and the end of the input buffer. Trailing null characters (such as alignment padding in initrd) continue to be accepted as valid.
Also update apply_xbc() in tools/bootconfig/main.c to calculate the buffer size based on the loaded file size rather than strlen(), so that files with intermediate null characters are not truncated before validation.
--- lib/bootconfig.c | 7 +++++++ tools/bootconfig/main.c | 4 +++- tools/bootconfig/test-bootconfig.sh | 12 ++++++++++++ 3 files changed, 22 insertions(+), 1 deletion(-)
lib/bootconfig.c +7 -0
@@ -1116,6 +1116,13 @@ static int __init xbc_parse_tree(void) } }while (!ret); +if (!ret) { +while (p < xbc_data + xbc_data_size -1 && *p =='\0' ) + p++; +if (p < xbc_data + xbc_data_size -1 ) + ret =xbc_parse_error ("Unexpected data after null character" , p); + } +return ret; }
tools/bootconfig/main.c +3 -1
@@ -422,7 +422,9 @@ static int apply_xbc(const char *path, const char *xbc_path)pr_err ("Failed to load %s : %d\n" , xbc_path, ret);return ret; } - size =strlen (buf) +1 ; + size = ret; +if (size ==0 || buf[size -1 ] !='\0' ) + size++; csum =xbc_calc_checksum (buf, size);/* Backup the bootconfig data */
tools/bootconfig/test-bootconfig.sh +12 -0
@@ -180,6 +180,18 @@ EOF $BOOTCONF -a $TEMPCONF $INITRD 2> $OUTFILE xpass grep -q "1:1" $OUTFILE +echo "Intermediate null character test" +printf "key = value\n\0extra = data\n" > $TEMPCONF +xfail $BOOTCONF -a $TEMPCONF $INITRD +$BOOTCONF -a $TEMPCONF $INITRD 2> $OUTFILE +xpass grep -q "Unexpected" $OUTFILE + +echo "Trailing null character test" +printf "key = value\n\0" > $TEMPCONF +xpass $BOOTCONF -a $TEMPCONF $INITRD +$BOOTCONF $INITRD > $OUTFILE +xpass grep -q "value" $OUTFILE + echo "=== expected failure cases ===" for i in samples/bad-* ; do xfail $BOOTCONF -a $i $INITRD
Masami Hiramatsu (Google)
maintainerPATCH 2/3 tools/bootconfig: Consolidate xbc_init() to error message wrapper
In reply to Masami Hiramatsu (Google)From: Masami Hiramatsu (Google) <[email protected]>
Use init_xbc_with_error() for all bootconfig initialization in the bootconfig tool instead of showing errors in different way.
This simplifies the code logic and make it easy to maintain.
--- tools/bootconfig/main.c | 108 ++++++++++++++++++++++------------------------- 1 file changed, 51 insertions(+), 57 deletions(-)
tools/bootconfig/main.c +51 -57
@@ -21,6 +21,47 @@#define BOOTCONFIG_FOOTER_SIZE \ (sizeof(uint32_t) * 2 + BOOTCONFIG_MAGIC_LEN) +static void show_xbc_error (const char *data,const char *msg,int pos) +{ +int lin =1 , col, i; + +if (pos <0 ) { +pr_err ("Error: %s.\n" , msg); +return ; + } + +/* Note that pos starts from 0 but lin and col should start from 1. */ + col = pos +1 ; +for (i =0 ; i < pos; i++) { +if (data[i] =='\n' ) { + lin++; + col = pos - i; + } + } +pr_err ("Parse Error: %s at %d:%d\n" , msg, lin, col); + +} + +static int init_xbc_with_error (char *buf,int len) +{ +char *copy =malloc (len); +const char *msg; +int ret, pos; + +if (!copy) +return -ENOMEM ; + +memcpy (copy, buf, len); +/* We do not terminate the copy with \0 for sanity checking */ + + ret =xbc_init (buf, len, &msg, &pos); +if (ret <0 ) +show_xbc_error (copy, msg, pos); +free (copy); + +return ret; +} +static int xbc_show_value (struct xbc_node *node,bool semicolon) {const char *val, *eol;
@@ -187,7 +228,6 @@ static int load_xbc_from_initrd(int fd, char **buf)int ret;uint32_t size =0 , csum =0 , rcsum;char magic[BOOTCONFIG_MAGIC_LEN ]; -const char *msg; ret =fstat (fd, &stat);if (ret <0 )
@@ -238,52 +278,9 @@ static int load_xbc_from_initrd(int fd, char **buf)return -EINVAL ; } - ret =xbc_init (*buf, size, &msg,NULL ); -/* Wrong data */ -if (ret <0 ) { -pr_err ("parse error: %s.\n" , msg); -return ret; - } + ret =init_xbc_with_error (*buf, size); -return size; -} - -static void show_xbc_error (const char *data,const char *msg,int pos) -{ -int lin =1 , col, i; - -if (pos <0 ) { -pr_err ("Error: %s.\n" , msg); -return ; - } - -/* Note that pos starts from 0 but lin and col should start from 1. */ - col = pos +1 ; -for (i =0 ; i < pos; i++) { -if (data[i] =='\n' ) { - lin++; - col = pos - i; - } - } -pr_err ("Parse Error: %s at %d:%d\n" , msg, lin, col); - -} - -static int init_xbc_with_error (char *buf,int len) -{ -char *copy =strdup (buf); -const char *msg; -int ret, pos; - -if (!copy) -return -ENOMEM ; - - ret =xbc_init (buf, len, &msg, &pos); -if (ret <0 ) -show_xbc_error (copy, msg, pos); -free (copy); - -return ret; +return ret <0 ? ret : size; }static int show_xbc_kernel_cmdline (void )
@@ -412,9 +409,8 @@ static int apply_xbc(const char *path, const char *xbc_path)char *buf, *data;size_t total_size;struct stat stat; -const char *msg;uint32_t size, csum; -int pos, pad; +int pad;int ret, fd; ret =load_xbc_file (xbc_path, &buf);
@@ -427,6 +423,13 @@ static int apply_xbc(const char *path, const char *xbc_path) size++; csum =xbc_calc_checksum (buf, size); +/* Verify the data format */ + ret =init_xbc_with_error (buf, size); +if (ret <0 ) { +free (buf); +return ret; + } +/* Backup the bootconfig data */ data =calloc (size +BOOTCONFIG_ALIGN +BOOTCONFIG_FOOTER_SIZE ,1 );if (!data) {
@@ -435,15 +438,6 @@ static int apply_xbc(const char *path, const char *xbc_path) }memcpy (data, buf, size); -/* Check the data format */ - ret =xbc_init (buf, size, &msg, &pos); -if (ret <0 ) { -show_xbc_error (data, msg, pos); -free (data); -free (buf); - -return ret; - }printf ("Apply %s to %s\n" , xbc_path, path);xbc_get_info (&ret,NULL );printf ("\tNumber of nodes: %d\n" , ret);
Masami Hiramatsu (Google)
maintainerPATCH 3/3 bootconfig: Skip internal tree sanity checks in kernel
In reply to Masami Hiramatsu (Google)From: Masami Hiramatsu (Google) <[email protected]>
In xbc_verify_tree(), the loop iterating through all nodes to check that xbc_nodes[i].next < xbc_node_num and xbc_nodes[i].child < xbc_node_num is a defensive sanity check against implementation regressions (such an out-of-bounds index cannot be produced by malformed input).
Running this check in the kernel adds unnecessary boot-time overhead. Split this check out into xbc_sanity_check_tree() for userspace, so that it continues to run during userspace bootconfig validation (e.g. when applying or testing bootconfig with tools/bootconfig), but is omitted in the kernel to speed up initialization.
--- lib/bootconfig.c | 38 ++++++++++++++++++++++++++------------ 1 file changed, 26 insertions(+), 12 deletions(-)
lib/bootconfig.c +26 -12
@@ -1000,9 +1000,30 @@ static int __init xbc_close_brace(char **k, char *n)return __xbc_close_brace (n -1 ); } +#ifndef __KERNEL__ +/* Sanity check for regression: node indices must be within bounds */ +static int __init xbc_sanity_check_tree (void ) +{ +int i; + +for (i =0 ; i < xbc_node_num; i++) { +if (xbc_nodes[i].next >= xbc_node_num) { +return xbc_parse_error ("No closing brace" , +xbc_node_get_data (xbc_nodes + i)); + } +if (xbc_nodes[i].child >= xbc_node_num) { +return xbc_parse_error ("Broken child node" , +xbc_node_get_data (xbc_nodes + i)); + } + } + +return 0 ; +} +#endif +static int __init xbc_verify_tree (void ) { -int i, depth; +int depth;size_t len, wlen;struct xbc_node *n, *m;
@@ -1019,17 +1040,6 @@ static int __init xbc_verify_tree(void)return -ENOENT ; } -for (i =0 ; i < xbc_node_num; i++) { -if (xbc_nodes[i].next >= xbc_node_num) { -return xbc_parse_error ("No closing brace" , -xbc_node_get_data (xbc_nodes + i)); - } -if (xbc_nodes[i].child >= xbc_node_num) { -return xbc_parse_error ("Broken child node" , -xbc_node_get_data (xbc_nodes + i)); - } - } -/* Key tree limitation check */ n = &xbc_nodes[0 ]; depth =1 ;
@@ -1199,6 +1209,10 @@ int __init xbc_init(const char *data, size_t size, const char **emsg, int *epos) ret =xbc_parse_tree ();if (!ret) ret =xbc_verify_tree (); +#ifndef __KERNEL__ +if (!ret) + ret =xbc_sanity_check_tree (); +#endif if (ret <0 ) {if (epos)
[email protected]
AI reviewRe: [PATCH 2/3] tools/bootconfig: Consolidate xbc_init() to error message wrapper
In reply to Masami Hiramatsu (Google)Thank you for your contribution! Sashiko AI review found 1 potential issue(s) to consider:
Thank you for your contribution! Sashiko AI review found 1 potential issue(s) to consider:
Pre-existing issues:
- [High] Integer overflow in initrd bootconfig size calculation leading to a heap buffer overflow. --
commit 51beb61f7aa059b732b7d7560c90f8155150ccd7
Author: Masami Hiramatsu (Google) <[email protected]>
tools/bootconfig: Consolidate xbc_init() to error message wrapper
Use init_xbc_with_error() for all bootconfig initialization in the bootconfig tool instead of showing errors in different way.
This simplifies the code logic and make it easy to maintain.
Quoted 4 lines
tools/bootconfig/main.c +0
-0
[ ... ]
tools/bootconfig/main.c static int load_xbc_from_initrd(int fd, char **buf) · line 281
Quoted 11 lines
tools/bootconfig/main.c +1
-6@@ -238,52 +278,9 @@ static int load_xbc_from_initrd(int fd, char **buf)return -EINVAL ; } - ret =xbc_init (*buf, size, &msg,NULL ); -/* Wrong data */ -if (ret <0 ) { -pr_err ("parse error: %s.\n" , msg); -return ret; - } + ret =init_xbc_with_error (*buf, size);
[Severity: High]
This is a pre-existing issue, but is it possible for a 32-bit integer overflow to bypass the size validation check in load_xbc_from_initrd()?
In load_xbc_from_initrd(), size is read from the file as a uint32_t:
tools/bootconfig/main.c:load_xbc_from_initrd() {
...
if (read(fd, &size, sizeof(uint32_t)) < 0)
return pr_errno("Failed to read size", -errno);
size = le32toh(size);
...
if (stat.st_size < size + BOOTCONFIG_FOOTER_SIZE) {
pr_err("bootconfig size is too big\n");
return -E2BIG;
}
...
}On 32-bit systems, if an attacker crafts size such that adding BOOTCONFIG_FOOTER_SIZE (20) wraps around (for instance, if size is 0xFFFFFFFF), the stat.st_size < size + BOOTCONFIG_FOOTER_SIZE check can be bypassed.
This would lead to load_xbc_fd() being called with size evaluating to -1:
tools/bootconfig/main.c:load_xbc_fd() {
...
*buf = malloc(size + 1);
if (!*buf)
return -ENOMEM;
ret = read(fd, *buf, size);
if (ret < 0)
return -errno;
(*buf)[size] = '\0';
...
}The malloc(size + 1) would result in a 0-byte allocation, while the read(fd, *buf, size) would cast the negative size to size_t (0xFFFFFFFF) and overflow the chunk. Finally, (*buf)[size] = '\0' would write to a negative index (-1), which corrupts heap metadata.
Could we make this size check more robust against integer overflow?
Quoted 3 lines
tools/bootconfig/main.c +0
-2-return size; -}
[ ... ]
Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/178896921555.177508.434402948295885560.stgit@devnote2?part=2
Masami Hiramatsu (Google)
maintainerRe: [PATCH 2/3] tools/bootconfig: Consolidate xbc_init() to error message wrapper
Quoted 25 lines · On Wed, 09 Sep 2026 16:11:12 +0000 [email protected] wrote:
Thank you for your contribution! Sashiko AI review found 1 potential issue(s) to consider:
Pre-existing issues:
- [High] Integer overflow in initrd bootconfig size calculation leading to a heap buffer overflow. --commit
51beb61f7aa059b732b7d7560c90f8155150ccd7
Author: Masami Hiramatsu (Google) <[email protected]>
tools/bootconfig: Consolidatexbc_init()to error message wrapperUse
init_xbc_with_error()for all bootconfig initialization in the bootconfig tool instead of showing errors in different way.This simplifies the code logic and make it easy to maintain.
Quoted 4 lines
tools/bootconfig/main.c +0
-0[ ... ]
Quoted 4 lines
tools/bootconfig/main.c +0
-0@@ -238,52 +278,9 @@ static int load_xbc_from_initrd(int fd, char **buf)return -EINVAL ; }
Quoted 11 lines
tools/bootconfig/main.c static int load_xbc_from_initrd(int fd, char **buf) · line 281
Quoted 7 lines
tools/bootconfig/main.c +1
-6- ret =xbc_init (*buf, size, &msg,NULL ); -/* Wrong data */ -if (ret <0 ) { -pr_err ("parse error: %s.\n" , msg); -return ret; - } + ret =init_xbc_with_error (*buf, size);[Severity: High]
This is a pre-existing issue, but is it possible for a 32-bit integer overflow to bypass the size validation check inload_xbc_from_initrd()?
Oh, I never thought I could shoot myself in the foot like that.
Anyway, since bootconfig file size should be smaller than 32KB, so we can check the file size before opening it.
Quoted 35 lines
In
load_xbc_from_initrd(), size is read from the file as auint32_t:tools/bootconfig/main.c:load_xbc_from_initrd() { ... if (read(fd, &size, sizeof(uint32_t)) < 0) return pr_errno("Failed to read size", -errno); size = le32toh(size); ... if (stat.st_size < size + BOOTCONFIG_FOOTER_SIZE) { pr_err("bootconfig size is too big\n"); return -E2BIG; } ... }On 32-bit systems, if an attacker crafts size such that adding
BOOTCONFIG_FOOTER_SIZE(20) wraps around (for instance, if size is0xFFFFFFFF), the stat.st_size < size +BOOTCONFIG_FOOTER_SIZEcheck can be bypassed.This would lead to
load_xbc_fd()being called with size evaluating to -1:tools/bootconfig/main.c:load_xbc_fd() { ... *buf = malloc(size + 1); if (!*buf) return -ENOMEM; ret = read(fd, *buf, size); if (ret < 0) return -errno; (*buf)[size] = '\0'; ... }
Quoted 4 lines
The malloc(size + 1) would result in a 0-byte allocation, while the
read(fd, *buf, size)would cast the negative size tosize_t(0xFFFFFFFF) and overflow the chunk. Finally, (*buf)[size] = '\0' would write to a negative index (-1), which corrupts heap metadata.
Good catch!
Quoted 2 lines
Could we make this size check more robust against integer overflow?
OK, let me fix it too.
Thanks!
Signature · 1 line
Masami Hiramatsu (Google) <[email protected]>
Re: [PATCH 1/3] bootconfig: Reject unexpected data after null character
In reply to Masami Hiramatsu (Google)Quoted 76 lines · Masami Hiramatsu (Google)
From: Masami Hiramatsu (Google) <[email protected]>
If a bootconfig buffer contains an intermediate null character in the middle of the configuration,
xbc_parse_tree()stops at the null character because string delimiter searches (e.g.strpbrk()) stop at '\0', and cleanly breaks out of the loop without error. As a result, any configuration data following the intermediate null character is silently ignored, allowing unparsed or potentially malicious data to be hidden after an early termination.Fix this in
xbc_parse_tree()by checking that no non-null data remains between the parser termination point and the end of the input buffer. Trailing null characters (such as alignment padding in initrd) continue to be accepted as valid.Also update
apply_xbc()intools/bootconfig/main.cto calculate the buffer size based on the loaded file size rather thanstrlen(), so that files with intermediate null characters are not truncated before validation.Assisted-by Antigravity:gemini-3.8-flashSigned-off-by Masami Hiramatsu (Google) <[email protected]>--- lib/bootconfig.c | 7 +++++++ tools/bootconfig/main.c | 4 +++- tools/bootconfig/test-bootconfig.sh | 12 ++++++++++++ 3 files changed, 22 insertions(+), 1 deletion(-)lib/bootconfig.c +7
-0@@ -1116,6 +1116,13 @@ static int __init xbc_parse_tree(void) } }while (!ret); +if (!ret) { +while (p < xbc_data + xbc_data_size -1 && *p =='\0' ) + p++; +if (p < xbc_data + xbc_data_size -1 ) + ret =xbc_parse_error ("Unexpected data after null character" , p); + } +return ret; }tools/bootconfig/main.c +3
-1@@ -422,7 +422,9 @@ static int apply_xbc(const char *path, const char *xbc_path)pr_err ("Failed to load %s : %d\n" , xbc_path, ret);return ret; } - size =strlen (buf) +1 ; + size = ret; +if (size ==0 || buf[size -1 ] !='\0' ) + size++; csum =xbc_calc_checksum (buf, size);/* Backup the bootconfig data */ tools/bootconfig/test-bootconfig.sh +5
-0@@ -180,6 +180,18 @@ EOF $BOOTCONF -a $TEMPCONF $INITRD 2> $OUTFILE xpass grep -q "1:1" $OUTFILE +echo "Intermediate null character test" +printf "key = value\n\0extra = data\n" > $TEMPCONF +xfail $BOOTCONF -a $TEMPCONF $INITRD +$BOOTCONF -a $TEMPCONF $INITRD 2> $OUTFILE +xpass grep -q "Unexpected" $OUTFILE
tools/bootconfig/test-bootconfig.sh EOF · line 197
Quoted 10 lines
tools/bootconfig/test-bootconfig.sh +7
-0+ +echo "Trailing null character test" +printf "key = value\n\0" > $TEMPCONF +xpass $BOOTCONF -a $TEMPCONF $INITRD +$BOOTCONF $INITRD > $OUTFILE +xpass grep -q "value" $OUTFILE + echo "=== expected failure cases ===" for i in samples/bad-* ; do xfail $BOOTCONF -a $i $INITRD
Thanks for doing this.
Re: [PATCH 2/3] tools/bootconfig: Consolidate xbc_init() to error message wrapper
In reply to Masami Hiramatsu (Google)Quoted 42 lines · Masami Hiramatsu (Google)
From: Masami Hiramatsu (Google) <[email protected]>
Use
init_xbc_with_error()for all bootconfig initialization in the bootconfig tool instead of showing errors in different way.This simplifies the code logic and make it easy to maintain.
Signed-off-by Masami Hiramatsu (Google) <[email protected]>--- tools/bootconfig/main.c | 108 ++++++++++++++++++++++------------------------- 1 file changed, 51 insertions(+), 57 deletions(-)tools/bootconfig/main.c +21
-0@@ -21,6 +21,47 @@#define BOOTCONFIG_FOOTER_SIZE \ (sizeof(uint32_t) * 2 + BOOTCONFIG_MAGIC_LEN) +static void show_xbc_error (const char *data,const char *msg,int pos) +{ +int lin =1 , col, i; + +if (pos <0 ) { +pr_err ("Error: %s.\n" , msg); +return ; + } + +/* Note that pos starts from 0 but lin and col should start from 1. */ + col = pos +1 ; +for (i =0 ; i < pos; i++) { +if (data[i] =='\n' ) { + lin++; + col = pos - i; + } + } +pr_err ("Parse Error: %s at %d:%d\n" , msg, lin, col); + +} +
tools/bootconfig/main.c line 54
Quoted 10 lines
tools/bootconfig/main.c +10
-0+static int init_xbc_with_error (char *buf,int len) +{ +char *copy =malloc (len); +const char *msg; +int ret, pos; + +if (!copy) +return -ENOMEM ; + +memcpy (copy, buf, len);
Can we remove this? xbc_init() already copies buf and does not change it, so show_xbc_error() can use buf directly.
Quoted 118 lines
tools/bootconfig/main.c +20
-57+/* We do not terminate the copy with \0 for sanity checking */ + + ret =xbc_init (buf, len, &msg, &pos); +if (ret <0 ) +show_xbc_error (copy, msg, pos); +free (copy); + +return ret; +} +static int xbc_show_value (struct xbc_node *node,bool semicolon) {const char *val, *eol;@@ -187,7 +228,6 @@ static int load_xbc_from_initrd(int fd, char **buf)int ret;uint32_t size =0 , csum =0 , rcsum;char magic[BOOTCONFIG_MAGIC_LEN ]; -const char *msg; ret =fstat (fd, &stat);if (ret <0 )@@ -238,52 +278,9 @@ static int load_xbc_from_initrd(int fd, char **buf)return -EINVAL ; } - ret =xbc_init (*buf, size, &msg,NULL ); -/* Wrong data */ -if (ret <0 ) { -pr_err ("parse error: %s.\n" , msg); -return ret; - } + ret =init_xbc_with_error (*buf, size); -return size; -} - -static void show_xbc_error (const char *data,const char *msg,int pos) -{ -int lin =1 , col, i; - -if (pos <0 ) { -pr_err ("Error: %s.\n" , msg); -return ; - } - -/* Note that pos starts from 0 but lin and col should start from 1. */ - col = pos +1 ; -for (i =0 ; i < pos; i++) { -if (data[i] =='\n' ) { - lin++; - col = pos - i; - } - } -pr_err ("Parse Error: %s at %d:%d\n" , msg, lin, col); - -} - -static int init_xbc_with_error (char *buf,int len) -{ -char *copy =strdup (buf); -const char *msg; -int ret, pos; - -if (!copy) -return -ENOMEM ; - - ret =xbc_init (buf, len, &msg, &pos); -if (ret <0 ) -show_xbc_error (copy, msg, pos); -free (copy); - -return ret; +return ret <0 ? ret : size; }static int show_xbc_kernel_cmdline (void )@@ -412,9 +409,8 @@ static int apply_xbc(const char *path, const char *xbc_path)char *buf, *data;size_t total_size;struct stat stat; -const char *msg;uint32_t size, csum; -int pos, pad; +int pad;int ret, fd; ret =load_xbc_file (xbc_path, &buf);@@ -427,6 +423,13 @@ static int apply_xbc(const char *path, const char *xbc_path) size++; csum =xbc_calc_checksum (buf, size); +/* Verify the data format */ + ret =init_xbc_with_error (buf, size); +if (ret <0 ) { +free (buf); +return ret; + } +/* Backup the bootconfig data */ data =calloc (size +BOOTCONFIG_ALIGN +BOOTCONFIG_FOOTER_SIZE ,1 );if (!data) {@@ -435,15 +438,6 @@ static int apply_xbc(const char *path, const char *xbc_path) }memcpy (data, buf, size); -/* Check the data format */ - ret =xbc_init (buf, size, &msg, &pos); -if (ret <0 ) { -show_xbc_error (data, msg, pos); -free (data); -free (buf); - -return ret; - }printf ("Apply %s to %s\n" , xbc_path, path);xbc_get_info (&ret,NULL );printf ("\tNumber of nodes: %d\n" , ret);
Re: [PATCH 3/3] bootconfig: Skip internal tree sanity checks in kernel
In reply to Masami Hiramatsu (Google)Quoted 77 lines · Masami Hiramatsu (Google)
From: Masami Hiramatsu (Google) <[email protected]>
In
xbc_verify_tree(), the loop iterating through all nodes to check that xbc_nodes[i].next <xbc_node_numand xbc_nodes[i].child <xbc_node_numis a defensive sanity check against implementation regressions (such an out-of-bounds index cannot be produced by malformed input).Running this check in the kernel adds unnecessary boot-time overhead. Split this check out into
xbc_sanity_check_tree()for userspace, so that it continues to run during userspace bootconfig validation (e.g. when applying or testing bootconfig withtools/bootconfig), but is omitted in the kernel to speed up initialization.Reported-by Sang-Heon Jeon <[email protected]>Signed-off-by Masami Hiramatsu (Google) <[email protected]>--- lib/bootconfig.c | 38 ++++++++++++++++++++++++++------------ 1 file changed, 26 insertions(+), 12 deletions(-)lib/bootconfig.c +22
-12@@ -1000,9 +1000,30 @@ static int __init xbc_close_brace(char **k, char *n)return __xbc_close_brace (n -1 ); } +#ifndef __KERNEL__ +/* Sanity check for regression: node indices must be within bounds */ +static int __init xbc_sanity_check_tree (void ) +{ +int i; + +for (i =0 ; i < xbc_node_num; i++) { +if (xbc_nodes[i].next >= xbc_node_num) { +return xbc_parse_error ("No closing brace" , +xbc_node_get_data (xbc_nodes + i)); + } +if (xbc_nodes[i].child >= xbc_node_num) { +return xbc_parse_error ("Broken child node" , +xbc_node_get_data (xbc_nodes + i)); + } + } + +return 0 ; +} +#endif +static int __init xbc_verify_tree (void ) { -int i, depth; +int depth;size_t len, wlen;struct xbc_node *n, *m;@@ -1019,17 +1040,6 @@ static int __init xbc_verify_tree(void)return -ENOENT ; } -for (i =0 ; i < xbc_node_num; i++) { -if (xbc_nodes[i].next >= xbc_node_num) { -return xbc_parse_error ("No closing brace" , -xbc_node_get_data (xbc_nodes + i)); - } -if (xbc_nodes[i].child >= xbc_node_num) { -return xbc_parse_error ("Broken child node" , -xbc_node_get_data (xbc_nodes + i)); - } - } -/* Key tree limitation check */ n = &xbc_nodes[0 ]; depth =1 ;
lib/bootconfig.c int __init xbc_init(const char *data, size_t size, const char **emsg, int *epos) · line 1218
Quoted 10 lines
lib/bootconfig.c +4
-0@@ -1199,6 +1209,10 @@ int __init xbc_init(const char *data, size_t size, const char **emsg, int *epos) ret =xbc_parse_tree ();if (!ret) ret =xbc_verify_tree (); +#ifndef __KERNEL__ +if (!ret) + ret =xbc_sanity_check_tree (); +#endif if (ret <0 ) {if (epos)
Thanks for doing this.
Masami Hiramatsu (Google)
maintainerRe: [PATCH 2/3] tools/bootconfig: Consolidate xbc_init() to error message wrapper
In reply to Sang-Heon JeonQuoted 5 lines · On Thu, 10 Sep 2026 14:30:02 +0900 Sang-Heon Jeon <[email protected]> wrote:
Quoted 5 lines
tools/bootconfig/main.c +5
-0+static int init_xbc_with_error (char *buf,int len) +{ +char *copy =malloc (len); +const char *msg; +int ret, pos;
Quoted 8 lines
tools/bootconfig/main.c line 54
Quoted 5 lines
tools/bootconfig/main.c +5
-0+ +if (!copy) +return -ENOMEM ; + +memcpy (copy, buf, len);Can we remove this?
xbc_init()already copies buf and does not change it, soshow_xbc_error()can use buf directly.
Ah, good catch! OK, let me fix it.
Thanks,
Signature · 1 line
Masami Hiramatsu (Google) <[email protected]>