From: Sascha Hauer <s.hauer@pengutronix.de>
To: BAREBOX <barebox@lists.infradead.org>
Subject: [PATCH 1/4] boot: fix use-after-free when restoring bootm variables
Date: Fri, 30 Oct 2026 08:34:41 +0100 [thread overview]
Message-ID: <20261030-boot-mem-fixes-v1-1-6d64824356eb@pengutronix.de> (raw)
In-Reply-To: <20261030-boot-mem-fixes-v1-0-6d64824356eb@pengutronix.de>
bootscript_boot() takes a backup of the global.bootm. variables with
bootm_data_init_defaults() to restore them after the boot script
returns. The string members of struct bootm_data are not copies, though:
getenv_nonempty() returns a pointer to the globalvar's current value.
When the boot script assigns one of these variables, the parameter code
frees the old value, leaving the backup pointing to freed memory, which
bootm_data_restore_defaults() then copies back. This is easily observed
with a boot script that sets global.bootm.image to a nonexistent file:
after the failed boot, global.bootm.image holds the script's value
instead of the original one, because the new value was allocated into
the just freed chunk.
Fix this by duplicating the strings in the backup and freeing them after
restoring.
Fixes: 926985a74ee0 ("bootm: don't clobber global.bootm. variables after script boot fails")
Assisted-by: Claude:claude-opus-5-5
Signed-off-by: Sascha Hauer <s.hauer@pengutronix.de>
---
common/boot.c | 17 +++++++++++++++++
1 file changed, 17 insertions(+)
diff --git a/common/boot.c b/common/boot.c
index 149bfeca00..1d8768b864 100644
--- a/common/boot.c
+++ b/common/boot.c
@@ -107,6 +107,16 @@ static int bootscript_boot(struct bootentry *entry, int verbose, int dryrun)
bootm_data_init_defaults(&backup);
+ /*
+ * The strings point to the globalvars' current values, which the
+ * script frees when it assigns new ones. Keep our own copies.
+ */
+ backup.os_file = xstrdup(backup.os_file);
+ backup.initrd_file = xstrdup(backup.initrd_file);
+ backup.oftree_file = xstrdup(backup.oftree_file);
+ backup.root_dev = xstrdup(backup.root_dev);
+ backup.root_param = xstrdup(backup.root_param);
+
globalvar_add_simple("linux.bootargs.dyn.ip", NULL);
globalvar_add_simple("linux.bootargs.dyn.root", NULL);
@@ -136,6 +146,13 @@ static int bootscript_boot(struct bootentry *entry, int verbose, int dryrun)
ret = bootm_entry(entry, &data);
out:
bootm_data_restore_defaults(&backup);
+
+ free_const(backup.os_file);
+ free_const(backup.initrd_file);
+ free_const(backup.oftree_file);
+ free_const(backup.root_dev);
+ free_const(backup.root_param);
+
return ret;
}
--
2.47.3
next prev parent reply other threads:[~2026-10-30 7:35 UTC|newest]
Thread overview: 6+ messages / expand[flat|nested] mbox.gz Atom feed top
2026-10-30 7:34 [PATCH 0/4] boot: UAF and double free fixes Sascha Hauer
2026-10-30 7:34 ` Sascha Hauer [this message]
2026-10-30 7:34 ` [PATCH 2/4] bootargs: fix double free in linux_bootargs_get() Sascha Hauer
2026-10-30 7:45 ` Ahmad Fatoum
2026-10-30 7:34 ` [PATCH 3/4] commands: boot: fix use-after-free of overrides taken from globalvars Sascha Hauer
2026-10-30 7:34 ` [PATCH 4/4] commands: boot: list bootm.image in the -o help text again Sascha Hauer
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=20261030-boot-mem-fixes-v1-1-6d64824356eb@pengutronix.de \
--to=s.hauer@pengutronix.de \
--cc=barebox@lists.infradead.org \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox