Skip to contentSkip to conversation

lib/bootconfig: guard xbc_node_compose_key_after() buffer size

PATCH Superseded Josh Law 2 participants 2 messages Mar 17

Subsystems (3)

v2 of 4 Superseded Latest is v4 →

lore
Share & tools
lore

Josh Law

author
#0 lore raw

PATCH v2 1/1 lib/bootconfig: guard xbc_node_compose_key_after() buffer size

xbc_node_compose_key_after() passes a size_t buffer length to snprintf(), but snprintf() returns int. Guard against size values above INT_MAX before the loop so the existing truncation check can continue to compare ret against (int)size safely.

Add a small WARN_ON_ONCE shim for the tools/bootconfig userspace build so the same source continues to build there.

---
 lib/bootconfig.c                            | 3 +++
 tools/bootconfig/include/linux/bootconfig.h | 5 +++++
 2 files changed, 8 insertions(+)
lib/bootconfig.c +3 -0
index 96cbe6738ffe..730209c83e62 100644
@@ -313,6 +313,9 @@ int __init xbc_node_compose_key_after(struct xbc_node *root,
 	if (!node && root)
 		return -EINVAL;
 
+	if (WARN_ON_ONCE(size > INT_MAX))
+		return -EINVAL;
+
 	while (--depth >= 0) {
 		node = xbc_nodes + keys[depth];
 		ret = snprintf(buf, size, "%s%s", xbc_node_get_data(node),
tools/bootconfig/include/linux/bootconfig.h +5 -0
index 6784296a0692..48383c10e036 100644
@@ -8,6 +8,7 @@
 #include <stdbool.h>
 #include <ctype.h>
 #include <errno.h>
+#include <limits.h>
 #include <string.h>
 
 
@@ -19,6 +20,10 @@
 	((cond) ? printf("Internal warning(%s:%d, %s): %s\n",	\
 			__FILE__, __LINE__, __func__, #cond) : 0)
 
+#ifndef WARN_ON_ONCE
+#define WARN_ON_ONCE(cond)	WARN_ON(cond)
+#endif
+
 #define unlikely(cond)	(cond)
 
 /* Copied from lib/string.c */
Signature · 1 line
2.34.1
maintainer
#1 lore raw
Quoted 10 lines · On Tue, 17 Mar 2026 18:06:05 +0000 Josh Law <[email protected]> wrote:

xbc_node_compose_key_after() passes a size_t buffer length to snprintf(), but snprintf() returns int. Guard against size values above INT_MAX before the loop so the existing truncation check can continue to compare ret against (int)size safely.

Add a small WARN_ON_ONCE shim for the tools/bootconfig userspace build so the same source continues to build there.

---

BTW, you can add here:

Changes since v1: https://lore.kernel.org/all/[email protected]/

- Removed typecasting ret to size_t, as it is not needed (Steven Rostedt)

lib/bootconfig.c int __init xbc_node_compose_key_after(struct xbc_node *root, · line 315

Quoted 12 lines
 lib/bootconfig.c                            | 3 +++
 tools/bootconfig/include/linux/bootconfig.h | 5 +++++
 2 files changed, 8 insertions(+)
lib/bootconfig.c +0 -0
index 96cbe6738ffe..730209c83e62 100644
@@ -313,6 +313,9 @@ int __init xbc_node_compose_key_after(struct xbc_node *root,
 	if (!node && root)
 		return -EINVAL;
 

I wonder if this should have a comment here:

/*
* Greater than 2G isn't needed for the bootconfig. Warn if it is
* bigger as to not need to worry about overruns of snprintf()
* return value.
*/

lib/bootconfig.c int __init xbc_node_compose_key_after(struct xbc_node *root, · line 318

Quoted 3 lines
lib/bootconfig.c +3 -0
+	if (WARN_ON_ONCE(size > INT_MAX))
+		return -EINVAL;
+

-- Steve

Quoted 26 lines
lib/bootconfig.c +0 -0
 	while (--depth >= 0) {
 		node = xbc_nodes + keys[depth];
 		ret = snprintf(buf, size, "%s%s", xbc_node_get_data(node),
tools/bootconfig/include/linux/bootconfig.h +5 -0
index 6784296a0692..48383c10e036 100644
@@ -8,6 +8,7 @@
 #include <stdbool.h>
 #include <ctype.h>
 #include <errno.h>
+#include <limits.h>
 #include <string.h>
 
 
@@ -19,6 +20,10 @@
 	((cond) ? printf("Internal warning(%s:%d, %s): %s\n",	\
 			__FILE__, __LINE__, __func__, #cond) : 0)
 
+#ifndef WARN_ON_ONCE
+#define WARN_ON_ONCE(cond)	WARN_ON(cond)
+#endif
+
 #define unlikely(cond)	(cond)
 
 /* Copied from lib/string.c */