Skip to content

patina_dxe_core: Fail LoadImage when the file buffer cannot be allocated - #2021

Open
AsakuraMizu wants to merge 2 commits into
OpenDevicePartnership:mainfrom
AsakuraMizu:fix/image-load-alloc
Open

AsakuraMizu wants to merge 2 commits into
OpenDevicePartnership:mainfrom
AsakuraMizu:fix/image-load-alloc

Conversation

@AsakuraMizu

@AsakuraMizu AsakuraMizu commented Nov 20, 2026 •

Copy link
Copy Markdown
Contributor

Description

LoadImage() copies the image into buffers allocated with vec! or to_vec() using sizes reported by the file system, the LoadFile provider, the FV or the caller, so an image larger than the available memory aborts the firmware instead of failing LoadImage(), e.g. when running it from the UEFI Shell. Allocate them with try_reserve_exact() and return OutOfResources instead. The buffers filled by Read(), GetInfo() and LoadFile() are no longer zeroed first, and are truncated to the size those calls return.

get_file_buffer_from_fw() also never freed the buffer ReadSection() allocates for it, leaking a copy of the PE32 section on every LoadImage() from an FV device path.

  • Impacts functionality?
  • Impacts security?
  • Breaking change?
  • Includes tests?
  • Includes documentation?

How This Was Tested

  • cargo nextest run --workspace, cargo clippy (host and x86_64-unknown-uefi), cargo fmt
  • QEMU Q35 with 1 GiB of memory, running a 1.5 GiB file from a FAT32 disk in the UEFI Shell: without the fix the firmware panics with memory allocation of 1610612736 bytes failed; with it, the load fails and the Shell returns to its prompt
  • QEMU Q35 boot to the EFI shell

Integration Instructions

N/A

@patina-automation

patina-automation Bot commented Nov 20, 2026 •

Copy link
Copy Markdown
Contributor

⌛ QEMU Validation Pending

QEMU validation is pending on successful CI completion.

Note: Any previous results are available in this comment's edit history.

This comment was automatically generated by the Patina QEMU PR Validation workflow.

@github-actions github-actions Bot added the impact:testing Affects testing label Nov 20, 2026
let mut file_buffer = vec![0u8; buffer_size];
let mut file_buffer = Vec::new();
file_buffer.try_reserve_exact(buffer_size).map_err(|_| EfiError::OutOfResources)?;
file_buffer.resize(buffer_size, 0);

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This change is good on its own, but, this is another case where we are zeroing something and then immediately writing to it. I think we should probably have a MaybeUninit vec instead in both cases and the functions we are calling are responsible to initialize it? It is unfortunate to have to rely on initialization to occur across an FFI boundary, but I also think for these large buffers we don't want to initialize them first.

@makubacki @Javagedes @joschock thoughts?

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

how about something like:

let mut file_buffer = Vec::try_with_capacity(buffer_size).map_err(|_| EfiError::OutOfResources)?;
let file_slice:  &mut [MaybeUninit<u8>] = file_buffer.spare_capacity_mut();
let status = unsafe {
        (load_file.load_file)(
            load_file,
            remaining_file_path.as_ptr(),
            boot_policy.into(),
            core::ptr::addr_of_mut!(buffer_size),
            file_slice.as_mut_ptr().cast::<c_void>(),
        )
    };

    //... error handle bad status ..
    //... maybe check that buffer_size didn't change ...

    // FFI load_file guarantees that the first `buffer_size` elements are initialized.
    unsafe {
        file_buffer.set_len(buffer_size);
    }

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

That makes sense. I used new + try_reserve_exact because try_with_capacity is not stable yet.

@codecov

codecov Bot commented Nov 20, 2026

Copy link
Copy Markdown

Codecov Report

✅ All modified and coverable lines are covered by tests.

📢 Thoughts on this report? Let us know!

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is a similar class of vector allocation that it seems like should be addressed to for consistency.

Comment on lines 621 to 623

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Similar class of vector allocation from image here.

Comment thread patina_dxe_core/src/filesystems.rs Outdated

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Similar class of issue here. info_size is from the file protocols get_info() callback. It could report a very large size.

@AsakuraMizu

Copy link
Copy Markdown
Contributor Author

While addressing review comments, I noticed a memory leak issue. The second commit fixes that.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

impact:testing Affects testing

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants