* [PATCH 1/2] bootconfig: Exclude alignment padding from bootconfig max size
2026-11-17 23:47 [PATCH 0/2] bootconfig: Fix max size padding and initrd memory leak Masami Hiramatsu (Google)
@ 2026-11-17 23:47 ` Masami Hiramatsu (Google)
2026-11-17 23:47 ` [PATCH 2/2] bootconfig: Free the initrd memory used by bootconfig Masami Hiramatsu (Google)
1 sibling, 0 replies; 3+ messages in thread
From: Masami Hiramatsu (Google) @ 2026-11-17 23:47 UTC (permalink / raw)
To: Masami Hiramatsu, Andrew Morton, Christian Brauner
Cc: Alexander Viro, Jan Kara, Breno Leitao, Ryan Roberts,
Steven Rostedt, linux-kernel, linux-fsdevel, linux-trace-kernel
From: Masami Hiramatsu (Google) <mhiramat@kernel.org>
When attaching a bootconfig to an initrd image, apply_xbc() appends 0-3
null padding bytes to align the total image size to 4 bytes and includes
those padding bytes in the footer size field. As documented in
Documentation/admin-guide/bootconfig.rst, the maximum bootconfig size
(XBC_DATA_MAX, 32767 bytes including the terminal null) is not supposed
to include these padding null characters.
However, both load_xbc_from_initrd() in tools/bootconfig and
get_boot_config_from_initrd() / xbc_init() in the kernel check the raw
footer size directly against XBC_DATA_MAX. As a result, a valid
32766-byte config (size = 32767 with the terminal null) gets a footer
size of 32768..32770 when alignment padding is added, and is rejected
with "bootconfig size is too big" depending on the initrd size.
Define XBC_DATA_MAX_PADDED (XBC_DATA_MAX + BOOTCONFIG_ALIGN - 1) for the
raw trailer size check, and add xbc_exclude_padding() to strip up to
BOOTCONFIG_ALIGN - 1 trailing null padding bytes (keeping the terminal
null byte) before checking the unpadded bootconfig size against
XBC_DATA_MAX and passing it to xbc_init(). Keep using the raw footer
size when truncating or trimming the initrd so that both the bootconfig
and its alignment padding are removed.
Also expand test-bootconfig.sh to verify applying, reading back, and
deleting a maximum-size bootconfig across all 4 alignment paddings.
Fixes: e1cef2d4c379 ("tools/bootconfig: Align the bootconfig applied initrd image size to 4")
Cc: stable@vger.kernel.org
Signed-off-by: Masami Hiramatsu (Google) <mhiramat@kernel.org>
---
include/linux/bootconfig.h | 24 ++++++++++++++++++++++++
init/main.c | 23 +++++++++++++++--------
tools/bootconfig/main.c | 14 ++++++++++----
tools/bootconfig/test-bootconfig.sh | 17 +++++++++++++++--
4 files changed, 64 insertions(+), 14 deletions(-)
diff --git a/include/linux/bootconfig.h b/include/linux/bootconfig.h
index 260a6f46042e..f745068c07cd 100644
--- a/include/linux/bootconfig.h
+++ b/include/linux/bootconfig.h
@@ -63,6 +63,30 @@ struct xbc_node {
#define XBC_VALUE (1 << 15)
/* Maximum size of boot config is 32KB - 1 */
#define XBC_DATA_MAX (XBC_VALUE - 1)
+/* Maximum size of padded boot config in initrd trailer */
+#define XBC_DATA_MAX_PADDED (XBC_DATA_MAX + BOOTCONFIG_ALIGN - 1)
+
+/**
+ * xbc_exclude_padding() - Exclude trailing null padding bytes from size
+ * @data: Bootconfig data (including padding).
+ * @size: The size of @data from the bootconfig footer.
+ *
+ * Exclude up to BOOTCONFIG_ALIGN - 1 trailing null padding bytes while
+ * keeping the terminal null byte of the bootconfig data, and return the
+ * unpadded bootconfig size.
+ */
+static inline __init uint32_t xbc_exclude_padding(const char *data, uint32_t size)
+{
+ int i;
+
+ for (i = 0; i < BOOTCONFIG_ALIGN - 1; i++) {
+ if (size <= 1 || data[size - 1] != '\0' || data[size - 2] != '\0')
+ break;
+ size--;
+ }
+
+ return size;
+}
#define XBC_NODE_MAX 8192
#define XBC_KEYLEN_MAX 256
diff --git a/init/main.c b/init/main.c
index 7b0c8c34e6a9..9471ece3d7d5 100644
--- a/init/main.c
+++ b/init/main.c
@@ -268,7 +268,7 @@ early_param("loglevel", loglevel);
#ifdef CONFIG_BLK_DEV_INITRD
static void * __init get_boot_config_from_initrd(size_t *_size)
{
- u32 size, csum;
+ u32 size, raw_size, csum;
char *data;
u8 *hdr;
int i;
@@ -294,24 +294,31 @@ static void * __init get_boot_config_from_initrd(size_t *_size)
if ((unsigned long)hdr < initrd_start)
return NULL;
- size = get_unaligned_le32(hdr);
+ raw_size = get_unaligned_le32(hdr);
csum = get_unaligned_le32(hdr + 4);
- if (size > XBC_DATA_MAX) {
+ if (raw_size > XBC_DATA_MAX_PADDED) {
pr_err("bootconfig size %u is greater than max size %d\n",
- size, XBC_DATA_MAX);
+ raw_size, XBC_DATA_MAX);
return NULL;
}
- if (size > ((unsigned long)hdr - initrd_start)) {
+ if (raw_size > ((unsigned long)hdr - initrd_start)) {
pr_err("bootconfig size %u is greater than initrd size %lu\n",
- size, initrd_end - initrd_start);
+ raw_size, initrd_end - initrd_start);
return NULL;
}
- data = ((void *)hdr) - size;
+ data = ((void *)hdr) - raw_size;
+
+ size = xbc_exclude_padding(data, raw_size);
+ if (size > XBC_DATA_MAX) {
+ pr_err("bootconfig size %u is greater than max size %d\n",
+ size, XBC_DATA_MAX);
+ return NULL;
+ }
- if (xbc_calc_checksum(data, size) != csum) {
+ if (xbc_calc_checksum(data, raw_size) != csum) {
pr_err("bootconfig checksum failed\n");
return NULL;
}
diff --git a/tools/bootconfig/main.c b/tools/bootconfig/main.c
index d3f840c5adce..c32fb1e2140c 100644
--- a/tools/bootconfig/main.c
+++ b/tools/bootconfig/main.c
@@ -182,7 +182,7 @@ static int load_xbc_fd(int fd, char **buf, int size)
{
int ret;
- if (size < 0 || size > XBC_DATA_MAX)
+ if (size < 0 || size > XBC_DATA_MAX_PADDED)
return -EINVAL;
*buf = malloc(size + 1);
@@ -237,7 +237,7 @@ static int load_xbc_from_initrd(int fd, char **buf)
{
struct stat stat;
int ret;
- uint32_t size = 0, csum = 0, rcsum;
+ uint32_t size = 0, data_size, csum = 0, rcsum;
char magic[BOOTCONFIG_MAGIC_LEN];
ret = fstat(fd, &stat);
@@ -269,7 +269,7 @@ static int load_xbc_from_initrd(int fd, char **buf)
csum = le32toh(csum);
/* Wrong size error */
- if (size > XBC_DATA_MAX ||
+ if (size > XBC_DATA_MAX_PADDED ||
size > stat.st_size - BOOTCONFIG_FOOTER_SIZE) {
pr_err("bootconfig size is too big\n");
return -E2BIG;
@@ -283,6 +283,12 @@ static int load_xbc_from_initrd(int fd, char **buf)
if (ret < 0)
return ret;
+ data_size = xbc_exclude_padding(*buf, size);
+ if (data_size > XBC_DATA_MAX) {
+ pr_err("bootconfig size is too big\n");
+ return -E2BIG;
+ }
+
/* Wrong Checksum */
rcsum = xbc_calc_checksum(*buf, size);
if (csum != rcsum) {
@@ -290,7 +296,7 @@ static int load_xbc_from_initrd(int fd, char **buf)
return -EINVAL;
}
- ret = init_xbc_with_error(*buf, size);
+ ret = init_xbc_with_error(*buf, data_size);
return ret < 0 ? ret : size;
}
diff --git a/tools/bootconfig/test-bootconfig.sh b/tools/bootconfig/test-bootconfig.sh
index f584f7637e12..e03301ad76c4 100755
--- a/tools/bootconfig/test-bootconfig.sh
+++ b/tools/bootconfig/test-bootconfig.sh
@@ -118,10 +118,23 @@ dd if=/dev/urandom bs=768 count=32 | base64 -w0 >> $TEMPCONF
printf "\"\n" >> $TEMPCONF
xfail $BOOTCONF -a $TEMPCONF $INITRD
+dd if=$TEMPCONF of=$OUTFILE bs=1 count=32765
+cp $OUTFILE $TEMPCONF
+printf "\"\n" >> $TEMPCONF # 32767 bytes + terminal ('\0') = 32768 (> max)
+xfail $BOOTCONF -a $TEMPCONF $INITRD
+
dd if=$TEMPCONF of=$OUTFILE bs=1 count=32764
cp $OUTFILE $TEMPCONF
-printf "\"\n" >> $TEMPCONF # add 2 bytes + terminal ('\"\n\0')
-xpass $BOOTCONF -a $TEMPCONF $INITRD
+printf "\"\n" >> $TEMPCONF # add 2 bytes + terminal ('\"\n\0') = 32767
+# Test max size across all 4 alignment paddings (0..3 bytes)
+for sz in 4096 4097 4098 4099; do
+ dd if=/dev/zero of=$INITRD bs=1 count=$sz 2>/dev/null
+ xpass $BOOTCONF -a $TEMPCONF $INITRD
+ $BOOTCONF -l $INITRD > $OUTFILE
+ xpass cmp $TEMPCONF $OUTFILE
+ xpass $BOOTCONF -d $INITRD
+ xpass test $(wc -c < $INITRD) -eq $sz
+done
echo "Adding same-key values"
cat > $TEMPCONF << EOF
^ permalink raw reply related [flat|nested] 3+ messages in thread* [PATCH 2/2] bootconfig: Free the initrd memory used by bootconfig
2026-11-17 23:47 [PATCH 0/2] bootconfig: Fix max size padding and initrd memory leak Masami Hiramatsu (Google)
2026-11-17 23:47 ` [PATCH 1/2] bootconfig: Exclude alignment padding from bootconfig max size Masami Hiramatsu (Google)
@ 2026-11-17 23:47 ` Masami Hiramatsu (Google)
1 sibling, 0 replies; 3+ messages in thread
From: Masami Hiramatsu (Google) @ 2026-11-17 23:47 UTC (permalink / raw)
To: Masami Hiramatsu, Andrew Morton, Christian Brauner
Cc: Alexander Viro, Jan Kara, Breno Leitao, Ryan Roberts,
Steven Rostedt, linux-kernel, linux-fsdevel, linux-trace-kernel
From: Masami Hiramatsu (Google) <mhiramat@kernel.org>
get_boot_config_from_initrd() removes the bootconfig from the initrd by
decrementing initrd_end in place. However, the whole initrd including
the bootconfig is reserved, e.g. reserve_initrd_mem() reserves
[initrd_start, PAGE_ALIGN(original initrd_end)), and do_populate_rootfs()
frees it with free_initrd_mem(initrd_start, initrd_end). Thus the full
pages in [PAGE_ALIGN(new initrd_end), PAGE_ALIGN(original initrd_end))
are never freed. This leaks up to 8 pages for a 32KB bootconfig.
Save the original initrd_end in initrd_mem_end when trimming the
bootconfig, and restore initrd_end before freeing the initrd memory.
The bootconfig data has already been copied by xbc_init() at that point.
The trimmed initrd_end is kept when the initrd is retained, so that
/sys/firmware/initrd does not expose the bootconfig.
Fixes: de462e5f1071 ("bootconfig: Fix to remove bootconfig data from initrd while boot")
Cc: stable@vger.kernel.org
Signed-off-by: Masami Hiramatsu (Google) <mhiramat@kernel.org>
---
include/linux/initrd.h | 2 ++
init/initramfs.c | 10 ++++++++++
init/main.c | 8 +++++++-
3 files changed, 19 insertions(+), 1 deletion(-)
diff --git a/include/linux/initrd.h b/include/linux/initrd.h
index 7e5d26c8136f..3086a462da76 100644
--- a/include/linux/initrd.h
+++ b/include/linux/initrd.h
@@ -17,6 +17,8 @@ extern unsigned long initrd_start, initrd_end;
extern void free_initrd_mem(unsigned long, unsigned long);
#ifdef CONFIG_BLK_DEV_INITRD
+/* initrd_end before trailing data (e.g. bootconfig) was trimmed off */
+extern unsigned long initrd_mem_end;
extern void __init reserve_initrd_mem(void);
extern void wait_for_initramfs(void);
#else
diff --git a/init/initramfs.c b/init/initramfs.c
index ebddde9c8f0d..11030cc1f35d 100644
--- a/init/initramfs.c
+++ b/init/initramfs.c
@@ -582,6 +582,7 @@ char * __init unpack_to_rootfs(char *buf, unsigned long len)
}
static int __initdata do_retain_initrd;
+unsigned long initrd_mem_end __initdata;
static int __init retain_initrd_param(char *str)
{
@@ -750,6 +751,15 @@ static void __init do_populate_rootfs(void *unused, async_cookie_t cookie)
security_initramfs_populated();
}
+ /*
+ * The bootconfig loader trims the bootconfig off initrd_end. Restore
+ * it to free the memory used by the bootconfig too, unless the initrd
+ * is retained (which must not expose the bootconfig).
+ */
+ if (!do_retain_initrd && initrd_start && initrd_end < initrd_mem_end)
+ initrd_end = initrd_mem_end;
+ initrd_mem_end = 0;
+
/*
* If the initrd region is overlapped with crashkernel reserved region,
* free only memory that is not part of crashkernel region.
diff --git a/init/main.c b/init/main.c
index 9471ece3d7d5..f14780345051 100644
--- a/init/main.c
+++ b/init/main.c
@@ -323,7 +323,13 @@ static void * __init get_boot_config_from_initrd(size_t *_size)
return NULL;
}
- /* Remove bootconfig from initramfs/initrd */
+ /*
+ * Remove bootconfig from initramfs/initrd. Since the initrd memory
+ * (including the bootconfig) is freed with initrd_end, save the
+ * original end to free the whole region later.
+ */
+ if (!initrd_mem_end)
+ initrd_mem_end = initrd_end;
initrd_end = (unsigned long)data;
if (_size)
*_size = size;
^ permalink raw reply related [flat|nested] 3+ messages in thread