Repository navigation
patina_dxe_core: Fail LoadImage when the file buffer cannot be allocated - #2021
AsakuraMizu wants to merge 2 commits into
Conversation
⌛ QEMU Validation PendingQEMU validation is pending on successful CI completion.
This comment was automatically generated by the Patina QEMU PR Validation workflow. |
| 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); |
There was a problem hiding this comment.
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?
There was a problem hiding this comment.
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);
}There was a problem hiding this comment.
That makes sense. I used new + try_reserve_exact because try_with_capacity is not stable yet.
Codecov Report✅ All modified and coverable lines are covered by tests. 📢 Thoughts on this report? Let us know! |
There was a problem hiding this comment.
This is a similar class of vector allocation that it seems like should be addressed to for consistency.
There was a problem hiding this comment.
Similar class of vector allocation from image here.
There was a problem hiding this comment.
Similar class of issue here. info_size is from the file protocols get_info() callback. It could report a very large size.
dae01f4 to
12b017c
Compare
|
While addressing review comments, I noticed a memory leak issue. The second commit fixes that. |
Description
LoadImage()copies the image into buffers allocated withvec!orto_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 failingLoadImage(), e.g. when running it from the UEFI Shell. Allocate them withtry_reserve_exact()and returnOutOfResourcesinstead. The buffers filled byRead(),GetInfo()andLoadFile()are no longer zeroed first, and are truncated to the size those calls return.get_file_buffer_from_fw()also never freed the bufferReadSection()allocates for it, leaking a copy of the PE32 section on everyLoadImage()from an FV device path.How This Was Tested
cargo nextest run --workspace,cargo clippy(host andx86_64-unknown-uefi),cargo fmtmemory allocation of 1610612736 bytes failed; with it, the load fails and the Shell returns to its promptIntegration Instructions
N/A