summaryrefslogtreecommitdiff
path: root/tools
diff options
context:
space:
mode:
authorLinus Torvalds <torvalds@linux-foundation.org>2026-10-23 12:20:54 -1000
committerLinus Torvalds <torvalds@linux-foundation.org>2026-10-23 12:20:54 -1000
commit90b754d40117b2889bbe847ce7bb4dcf968e7cb8 (patch)
tree95c8c91b5a5b33cc7ed7837768fe54dbccd58339 /tools
parent3f942403bbad36a120432be69445154891b65398 (diff)
parent677f83795fdfc25ddc51b665a9111f5ff199f7ca (diff)
Merge tag 'bootconfig-v7.4' of git://git.kernel.org/pub/scm/linux/kernel/git/trace/linux-trace
Pull bootconfig updates from Masami Hiramatsu: "Parser robustness and validation improvements: - Skip printing early params to cmdline from bootconfig Suppress early_param keys from /proc/cmdline and warn the user, since bootconfig does not invoke early_param handlers and silently ignoring them is misleading - Reject unexpected data after null character Detect and reject non-null data following an intermediate null character in the bootconfig buffer, preventing silently ignored configuration after an early termination Code cleanups: - Merge _xbc_exit() into xbc_exit() Since memblock_free() now handles late freeing and NULL addresses, the separate _xbc_exit() helper and its early argument are unnecessary. Inline it into xbc_exit() - Remove redundant assignment in xbc_parse_array() The node->child = 0 assignment after the loop duplicates what xbc_init_node() already did - Skip internal tree sanity checks in kernel Move the node index bounds loop from xbc_verify_tree() into a userspace-only xbc_sanity_check_tree() to reduce boot-time overhead, since out-of-bounds indices cannot be produced by malformed input - Move BOOTCONFIG_FOOTER_SIZE to include/linux/bootconfig.h Share the footer size constant between kernel and user-space tools, replacing the hardcoded (BOOTCONFIG_MAGIC_LEN + 8) expression in init/main.c Tooling improvements: - Remove unused PAGE_SIZE macro from the bootconfig tool - Consolidate xbc_init() to error message wrapper Use init_xbc_with_error() for all bootconfig initialization paths in the tool for simpler and more consistent error handling" * tag 'bootconfig-v7.4' of git://git.kernel.org/pub/scm/linux/kernel/git/trace/linux-trace: bootconfig: Move BOOTCONFIG_FOOTER_SIZE to include/linux/bootconfig.h bootconfig: Skip internal tree sanity checks in kernel tools/bootconfig: Consolidate xbc_init() to error message wrapper bootconfig: Reject unexpected data after null character bootconfig: Skip printing early params to cmdline from bootconfig tools/bootconfig: remove unused PAGE_SIZE macro bootconfig: remove redundant assignment in xbc_parse_array() bootconfig: merge _xbc_exit() into xbc_exit()
Diffstat (limited to 'tools')
-rw-r--r--tools/bootconfig/main.c108
-rwxr-xr-xtools/bootconfig/test-bootconfig.sh12
2 files changed, 57 insertions, 63 deletions
diff --git a/tools/bootconfig/main.c b/tools/bootconfig/main.c
index df0534035857..6f476e3c06b8 100644
--- a/tools/bootconfig/main.c
+++ b/tools/bootconfig/main.c
@@ -17,9 +17,38 @@
#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 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)
+{
+ const char *msg;
+ int ret, pos;
+
+ ret = xbc_init(buf, len, &msg, &pos);
+ if (ret < 0)
+ show_xbc_error(buf, msg, pos);
+
+ return ret;
+}
static int xbc_show_value(struct xbc_node *node, bool semicolon)
{
@@ -134,8 +163,6 @@ static void xbc_show_list(void)
}
}
-#define PAGE_SIZE 4096
-
static int load_xbc_fd(int fd, char **buf, int size)
{
int ret;
@@ -197,7 +224,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)
@@ -249,52 +275,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;
- }
-
- 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);
+ ret = init_xbc_with_error(*buf, size);
-}
-
-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)
@@ -423,9 +406,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);
@@ -433,9 +415,18 @@ 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);
+ /* 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) {
@@ -444,15 +435,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);
diff --git a/tools/bootconfig/test-bootconfig.sh b/tools/bootconfig/test-bootconfig.sh
index fc69f815ce4a..530ce7e28d63 100755
--- a/tools/bootconfig/test-bootconfig.sh
+++ b/tools/bootconfig/test-bootconfig.sh
@@ -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