public inbox for linux-kernel@vger.kernel.org
 help / color / mirror / Atom feed
* [PATCH 1/3] lib/bootconfig: fix off-by-one in xbc_verify_tree() unclosed brace error
@ 2026-03-12 18:45 Josh Law
  2026-03-12 18:45 ` [PATCH 2/3] lib/bootconfig: check bounds before writing in __xbc_open_brace() Josh Law
  2026-03-12 18:45 ` [PATCH 3/3] lib/bootconfig: fix snprintf truncation check in xbc_node_compose_key_after() Josh Law
  0 siblings, 2 replies; 3+ messages in thread
From: Josh Law @ 2026-03-12 18:45 UTC (permalink / raw)
  To: Masami Hiramatsu, Andrew Morton
  Cc: Josh Law, linux-kernel, linux-trace-kernel

__xbc_open_brace() pushes entries with post-increment
(open_brace[brace_index++]), so brace_index always points one past
the last valid entry.  xbc_verify_tree() reads open_brace[brace_index]
to report which brace is unclosed, but this is one past the last
pushed entry and contains stale/zero data, causing the error message
to reference the wrong node.

Use open_brace[brace_index - 1] to correctly identify the unclosed
brace.  brace_index is known to be > 0 here since we are inside the
if (brace_index) guard.

Signed-off-by: Josh Law <objecting@objecting.org>
---
 lib/bootconfig.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)

diff --git a/lib/bootconfig.c b/lib/bootconfig.c
index 2bcd5c2aa87e..a1e6a2e14b01 100644
--- a/lib/bootconfig.c
+++ b/lib/bootconfig.c
@@ -802,7 +802,7 @@ static int __init xbc_verify_tree(void)
 
 	/* Brace closing */
 	if (brace_index) {
-		n = &xbc_nodes[open_brace[brace_index]];
+		n = &xbc_nodes[open_brace[brace_index - 1]];
 		return xbc_parse_error("Brace is not closed",
 					xbc_node_get_data(n));
 	}
-- 
2.34.1


^ permalink raw reply related	[flat|nested] 3+ messages in thread

* [PATCH 2/3] lib/bootconfig: check bounds before writing in __xbc_open_brace()
  2026-03-12 18:45 [PATCH 1/3] lib/bootconfig: fix off-by-one in xbc_verify_tree() unclosed brace error Josh Law
@ 2026-03-12 18:45 ` Josh Law
  2026-03-12 18:45 ` [PATCH 3/3] lib/bootconfig: fix snprintf truncation check in xbc_node_compose_key_after() Josh Law
  1 sibling, 0 replies; 3+ messages in thread
From: Josh Law @ 2026-03-12 18:45 UTC (permalink / raw)
  To: Masami Hiramatsu, Andrew Morton
  Cc: Josh Law, linux-kernel, linux-trace-kernel

The bounds check for brace_index happens after the array write.
While the current call pattern prevents an actual out-of-bounds
access (the previous call would have returned an error), the
write-before-check pattern is fragile and would become a real
out-of-bounds write if the error return were ever not propagated.

Move the bounds check before the array write so the function is
self-contained and safe regardless of caller behavior.

Signed-off-by: Josh Law <objecting@objecting.org>
---
 lib/bootconfig.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)

diff --git a/lib/bootconfig.c b/lib/bootconfig.c
index a1e6a2e14b01..62b4ed7a0ba6 100644
--- a/lib/bootconfig.c
+++ b/lib/bootconfig.c
@@ -532,9 +532,9 @@ static char *skip_spaces_until_newline(char *p)
 static int __init __xbc_open_brace(char *p)
 {
 	/* Push the last key as open brace */
-	open_brace[brace_index++] = xbc_node_index(last_parent);
 	if (brace_index >= XBC_DEPTH_MAX)
 		return xbc_parse_error("Exceed max depth of braces", p);
+	open_brace[brace_index++] = xbc_node_index(last_parent);
 
 	return 0;
 }
-- 
2.34.1


^ permalink raw reply related	[flat|nested] 3+ messages in thread

* [PATCH 3/3] lib/bootconfig: fix snprintf truncation check in xbc_node_compose_key_after()
  2026-03-12 18:45 [PATCH 1/3] lib/bootconfig: fix off-by-one in xbc_verify_tree() unclosed brace error Josh Law
  2026-03-12 18:45 ` [PATCH 2/3] lib/bootconfig: check bounds before writing in __xbc_open_brace() Josh Law
@ 2026-03-12 18:45 ` Josh Law
  1 sibling, 0 replies; 3+ messages in thread
From: Josh Law @ 2026-03-12 18:45 UTC (permalink / raw)
  To: Masami Hiramatsu, Andrew Morton
  Cc: Josh Law, linux-kernel, linux-trace-kernel

snprintf() returns the number of characters that would have been
written excluding the NUL terminator.  Output is truncated when the
return value is >= the buffer size, not just > the buffer size.

When ret == size, the current code takes the non-truncated path,
advancing buf by ret and reducing size to 0.  This is wrong because
the output was actually truncated (the last character was replaced by
NUL).  Fix by using >= so the truncation path is taken correctly.

Signed-off-by: Josh Law <objecting@objecting.org>
---
 lib/bootconfig.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)

diff --git a/lib/bootconfig.c b/lib/bootconfig.c
index 62b4ed7a0ba6..b0ef1e74e98a 100644
--- a/lib/bootconfig.c
+++ b/lib/bootconfig.c
@@ -316,7 +316,7 @@ int __init xbc_node_compose_key_after(struct xbc_node *root,
 			       depth ? "." : "");
 		if (ret < 0)
 			return ret;
-		if (ret > size) {
+		if (ret >= size) {
 			size = 0;
 		} else {
 			size -= ret;
-- 
2.34.1


^ permalink raw reply related	[flat|nested] 3+ messages in thread

end of thread, other threads:[~2026-03-12 18:45 UTC | newest]

Thread overview: 3+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2026-03-12 18:45 [PATCH 1/3] lib/bootconfig: fix off-by-one in xbc_verify_tree() unclosed brace error Josh Law
2026-03-12 18:45 ` [PATCH 2/3] lib/bootconfig: check bounds before writing in __xbc_open_brace() Josh Law
2026-03-12 18:45 ` [PATCH 3/3] lib/bootconfig: fix snprintf truncation check in xbc_node_compose_key_after() Josh Law

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox