bootconfig: Fix tree verification, key composition, and cmdline rendering bugs
Subsystems (3)
Masami Hiramatsu (Google)
maintainercover bootconfig: Fix tree verification, key composition, and cmdline rendering bugs
Hi,
This series fixes bugs in the Extra Boot Config (bootconfig) parser and tree query/rendering API (lib/bootconfig.c), and adds regression tests to tools/bootconfig/test-bootconfig.sh. These issues were found while formally modeling and verifying the bootconfig parser in Lean 4.
Summary of changes:
1. Fix xbc_verify_tree() to use xbc_node_get_subkey() instead of xbc_node_get_child() when descending the key tree. Previously, if a key node had both a value and subkeys, its first child was the value node, causing xbc_verify_tree() to skip all subkeys under it and bypass both XBC_DEPTH_MAX (16) and XBC_KEYLEN_MAX (256) checks.
2. Fix xbc_node_compose_key_after() for value nodes: climb all parent value nodes in a loop (while (node && xbc_node_is_value(node))) so that 2nd+ array value nodes resolve to their owning key node, and perform if (!node || node == root) after climbing so that passing a value node belonging to the subtree @root key node itself (e.g. foo = "val" with @root = xbc_find_node("foo")) returns -EINVAL rather than 0 with an untouched buffer.
3. Fix xbc_snprint_cmdline_filter() to pass
buf && len < size ? buf + len : NULL to snprintf(), avoiding out-of-bounds pointer arithmetic (buf + len with len > size) when @buf is non-NULL and the rendered output exceeds @size.
4. Add regression tests to tools/bootconfig/test-bootconfig.sh for unclosed brace error position reporting and mixed value+subkey depth/length limit enforcement.
Thank you,
---
Masami Hiramatsu (Google) (4):
bootconfig: Fix xbc_verify_tree() to check subkeys of valued keys
bootconfig: Fix xbc_node_compose_key_after() for value nodes
bootconfig: Avoid out-of-bounds pointer arithmetic in xbc_snprint_cmdline_filter() tools/bootconfig: Add tests for unclosed brace position and mixed-kv limits
lib/bootconfig.c | 17 +++++++++-------- tools/bootconfig/test-bootconfig.sh | 33 +++++++++++++++++++++++++++++++++ 2 files changed, 42 insertions(+), 8 deletions(-)
--
Masami Hiramatsu (Google) <[email protected]>
Masami Hiramatsu (Google)
maintainerPATCH 1/4 bootconfig: Fix xbc_verify_tree() to check subkeys of valued keys
From: Masami Hiramatsu (Google) <[email protected]>
When a key node has both a value and subkeys, the value node is always inserted as the first child of the key node (xbc_add_head_sibling()), and subkeys follow as next siblings of that value node.
However, xbc_verify_tree() uses xbc_node_get_child(n) and checks xbc_node_is_key(m) to descend into subkeys. When a key node has a value, xbc_node_get_child(n) returns the value node, so xbc_node_is_key(m) is false and xbc_verify_tree() completely skips verifying all subkeys under that key. This allows subkeys under a valued key to bypass both the XBC_DEPTH_MAX (16) and XBC_KEYLEN_MAX (256) checks during xbc_init(), causing runtime -ERANGE failures in xbc_node_compose_key_after().
Use xbc_node_get_subkey(n) so that subkeys of mixed value+subkey nodes are properly verified.
--- lib/bootconfig.c | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-)
lib/bootconfig.c +2 -2
@@ -1058,8 +1058,8 @@ static int __init xbc_verify_tree(void)return xbc_parse_error ("Too long key length" ,xbc_node_get_data (n)); - m =xbc_node_get_child (n); -if (m &&xbc_node_is_key (m)) { + m =xbc_node_get_subkey (n); +if (m) { n = m; depth++;if (depth >XBC_DEPTH_MAX )
Masami Hiramatsu (Google)
maintainerPATCH 2/4 bootconfig: Fix xbc_node_compose_key_after() for value nodes
In reply to Masami Hiramatsu (Google)From: Masami Hiramatsu (Google) <[email protected]>
Fix two issues in xbc_node_compose_key_after() when @node is a value node:
1. Since array values are chained via the child pointer in xbc_parse_array(), the 2nd and subsequent value nodes of an array have the preceding value node as their parent rather than the owning key node. Climbing only one parent (if (xbc_node_is_value(node))) caused preceding array value strings to be included in the composed key (e.g. "foo.bar.val1" instead of "foo.bar").
2. Checking if (!node || node == root) before climbing to the owning key node allowed a value node belonging to the subtree @root key node itself (e.g. foo = "val" with @root = xbc_find_node("foo")) to pass the check, climb to @root, skip the key-collection loop (depth == 0), and return 0 without writing anything (not even a NUL terminator) to @buf.
Climb parent pointers in a loop while the node is a value node, and perform the if (!node || node == root) check afterward.
--- lib/bootconfig.c | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-)
lib/bootconfig.c +3 -3
@@ -421,12 +421,12 @@ int __init xbc_node_compose_key_after(struct xbc_node *root,uint16_t keys[XBC_DEPTH_MAX ];int depth =0 , ret =0 , total =0 ; +while (node &&xbc_node_is_value (node)) + node =xbc_node_get_parent (node); +if (!node || node == root)return -EINVAL ; -if (xbc_node_is_value (node)) - node =xbc_node_get_parent (node); -while (node && node != root) {if (depth ==XBC_DEPTH_MAX )return -ERANGE ;
Masami Hiramatsu (Google)
maintainerPATCH 3/4 bootconfig: Avoid out-of-bounds pointer arithmetic in xbc_snprint_cmdline_filter()
In reply to Masami Hiramatsu (Google)From: Masami Hiramatsu (Google) <[email protected]>
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.
--- lib/bootconfig.c | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-)
lib/bootconfig.c +4 -3
@@ -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;
Masami Hiramatsu (Google)
maintainerPATCH 4/4 tools/bootconfig: Add tests for unclosed brace position and mixed-kv limits
In reply to Masami Hiramatsu (Google)From: Masami Hiramatsu (Google) <[email protected]>
Add regression test cases to test-bootconfig.sh for:
1. Unclosed brace error position reporting in xbc_verify_tree() (verifying both samples/bad-tree.bconf and a multi-block config).
2. XBC_DEPTH_MAX (16) and XBC_KEYLEN_MAX (256) enforcement on subkeys under a key node that also has a value (mixed value+subkey).
--- tools/bootconfig/test-bootconfig.sh | 33 +++++++++++++++++++++++++++++++++ 1 file changed, 33 insertions(+)
tools/bootconfig/test-bootconfig.sh +33 -0
@@ -220,6 +220,39 @@ xpass $BOOTCONF -a $TEMPCONF $INITRD $BOOTCONF $INITRD > $OUTFILE xpass grep -q "value" $OUTFILE +echo "Unclosed brace error position" +cat > $TEMPCONF << EOF +a { + b = 1 +} +c { + d = 2 +EOF +echo > $INITRD + +xfail $BOOTCONF -a $TEMPCONF $INITRD +$BOOTCONF -a $TEMPCONF $INITRD > $OUTFILE 2>&1 +xpass grep -q "Brace.is.not.closed.at.4:1" $OUTFILE +$BOOTCONF -a samples/bad-tree.bconf $INITRD > $OUTFILE 2>&1 +xpass grep -q "Brace.is.not.closed.at.2:1" $OUTFILE + +echo "Mixed value and subkey depth/length limits" +cat > $TEMPCONF << EOF +k1 = v1 +k1.k2.k3.k4.k5.k6.k7.k8.k9.k10.k11.k12.k13.k14.k15.k16.k17 = v2 +EOF +xfail $BOOTCONF -a $TEMPCONF $INITRD + +awk ' +BEGIN { + printf("root = v\nroot.") + for (i = 0; i < 255; i += 1) + printf("a") + printf(" = v2\n") +} +' > $TEMPCONF +xfail $BOOTCONF -a $TEMPCONF $INITRD + echo "=== expected failure cases ===" for i in samples/bad-* ; do xfail $BOOTCONF -a $i $INITRD