Skip to content

Commit f947af0

Browse files
Alexandra IordacheSamuel Ortiz
Alexandra Iordache
authored and
Samuel Ortiz
committed
fix potential undefined behavior condition
Fixed an unchecked arithmetic operation that could cause undefined behavior. Attempting to load a malformed ELF kernel image which contains a large enough entry address in the ELF header, or a valid ELF image at a large enough offset in guest memory, can lead to arithmetic overflow, causing the result to wrap around. The result is meant to be used as the value for the instruction pointer where the guest will start booting from. This can result in the guest executing code from undefined locations in guest memory when the vCPUs start. Signed-off-by: Alexandra Iordache <[email protected]>
1 parent d72752a commit f947af0

File tree

1 file changed

+22
-1
lines changed

1 file changed

+22
-1
lines changed

src/loader/x86_64/elf/mod.rs

Lines changed: 22 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -223,7 +223,12 @@ impl KernelLoader for Elf {
223223

224224
// Address where the kernel will be loaded.
225225
loader_result.kernel_load = match kernel_offset {
226-
Some(k_offset) => GuestAddress(k_offset.raw_value() + (ehdr.e_entry as u64)),
226+
Some(k_offset) => GuestAddress(
227+
k_offset
228+
.raw_value()
229+
.checked_add(ehdr.e_entry as u64)
230+
.ok_or(Error::Overflow)?,
231+
),
227232
None => GuestAddress(ehdr.e_entry as u64),
228233
};
229234

@@ -566,4 +571,20 @@ mod tests {
566571
Elf::load(&gm, None, &mut Cursor::new(&bad_align_image), None).err()
567572
);
568573
}
574+
575+
#[test]
576+
fn test_overflow_loadaddr() {
577+
let gm = create_guest_mem();
578+
let image = make_elf_bin();
579+
assert_eq!(
580+
Some(KernelLoaderError::Elf(Error::Overflow)),
581+
Elf::load(
582+
&gm,
583+
Some(GuestAddress(u64::MAX)),
584+
&mut Cursor::new(&image),
585+
None
586+
)
587+
.err()
588+
);
589+
}
569590
}

0 commit comments

Comments
 (0)