From: Masami Hiramatsu (Google) 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) --- 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;