|
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index] [PATCH] EFI: avoid use-after-free when chaining config files
When following "chain=", efi_start() frees the buffer holding the
current config file (if it was allocated) and only afterwards converts
the chained file's name, which points into that buffer, with s2w().
The section name, when taken from "default=", also points there and is
used for the subsequent lookups in the chained file.
read_file() may well get back the very pages just freed (as observed
with OVMF), so the section name may end up referring to arbitrary text
of the chained file, selecting the wrong section (or none). With
firmware overwriting freed memory (e.g. Ubuntu's OVMF 2025.11, which
fills it with 0xAF), even the file name is garbage, and loading the
chained file fails with "Open failed for ...: Invalid parameter".
Convert the file name before freeing the buffer, and copy the section
name. A configuration relying on the stale pointer happening to refer
to the chained file's own "default=" value will now use the section
named in the original file instead.
Fixes: 81da4016bfeb ("x86-64/EFI: allow chaining of config files")
Assisted-by: Claude
Signed-off-by: Anandu Padinhare <Anandu.Padinhare@xxxxxxxxxxxxxx>
---
Tested in QEMU, booting xen.efi directly: on x86 (i440fx, Ubuntu
OVMF 2025.11) with a Linux PV Dom0, on Arm64 (virt, AAVMF 2025.11,
cross-built) up to Dom0 construction with a dummy kernel. Before the
patch, every test chaining from a config file read from disk failed
("Open failed for ..." on x86, "Chained configuration file '...'not
found." with a truncated name on Arm64). With it, "kernel=", "options="
and "ramdisk=" (x86) or "dtb=" (Arm64) come from the chained file, also
across several levels; with the section given on the xen.efi command
line (x86, started from the UEFI shell) the right section was used too.
Booting without "chain=" is unaffected. A build fixing only the file
name showed the chained file loaded into the same pages as the freed
one, with the section name then taken from its text.
For simplicity the section name is also copied when it came from the
command line (and hence isn't in the buffer). The copies aren't freed:
they are EfiLoaderData, which Xen uses as ordinary RAM after
ExitBootServices() anyway. Not tested on real hardware.
xen/common/efi/boot.c | 17 ++++++++++++++++-
1 file changed, 16 insertions(+), 1 deletion(-)
diff --git a/xen/common/efi/boot.c b/xen/common/efi/boot.c
index 89b5831691..fccde265a4 100644
--- a/xen/common/efi/boot.c
+++ b/xen/common/efi/boot.c
@@ -1584,9 +1584,24 @@ void EFIAPI __init noreturn efi_start(EFI_HANDLE
ImageHandle,
name.s = get_value(&cfg, "global", "chain");
if ( !name.s )
break;
+ /*
+ * name.s, and section.s if it came from "default=", point into
+ * the buffer freed below. Copy both before freeing it.
+ */
+ name.w = s2w(&name);
+ if ( section.s )
+ {
+ size_t len = strlen(section.s) + 1;
+ void *ptr;
+
+ if ( efi_bs->AllocatePool(EfiLoaderData, len,
+ &ptr) != EFI_SUCCESS )
+ blexit(L"Unable to allocate string buffer");
+ section.s = memcpy(ptr, section.s, len);
+ }
free_cfg();
ensure_dir_handle(loaded_image, &dir_handle, &file_name);
- if ( !read_file(dir_handle, s2w(&name), &cfg, NULL) )
+ if ( !read_file(dir_handle, name.w, &cfg, NULL) )
{
PrintStr(L"Chained configuration file '");
PrintStr(name.w);
base-commit: 97bf370375113caf92fccf04e2877fc6b5ff887f
|
![]() |
Lists.xenproject.org is hosted with RackSpace, monitoring our |