From 1fa3dcff14f2c84a2b245b11d26609feb2777f80 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Alexander=20Mei=C3=9Fner?= Date: Fri, 18 Oct 2024 20:01:30 +0100 Subject: [PATCH] Makes the lenient parser SBPF v1 only. --- src/elf.rs | 27 +++++++++++++------------ tests/elfs/program_headers_overflow.so | Bin 5408 -> 5408 bytes 2 files changed, 14 insertions(+), 13 deletions(-) diff --git a/src/elf.rs b/src/elf.rs index 70a14173..c51e27be 100644 --- a/src/elf.rs +++ b/src/elf.rs @@ -370,7 +370,7 @@ impl Executable { }) } - /// Fully loads an ELF, including validation and relocation + /// Fully loads an ELF pub fn load(bytes: &[u8], loader: Arc>) -> Result { let e_flags = LittleEndian::read_u32(bytes.get(48..52).ok_or(ElfParserError::OutOfBounds)?); let config = loader.get_config(); @@ -391,10 +391,19 @@ impl Executable { return Err(ElfError::UnsupportedSBPFVersion); } - Self::load_with_parser(bytes, loader) + let mut executable = match sbpf_version { + SBPFVersion::V1 => Self::load_with_lenient_parser(bytes, loader)?, + _ => return Err(ElfError::UnsupportedSBPFVersion), + }; + executable.sbpf_version = sbpf_version; + Ok(executable) } - fn load_with_parser(bytes: &[u8], loader: Arc>) -> Result { + /// Loads an ELF with relocation + fn load_with_lenient_parser( + bytes: &[u8], + loader: Arc>, + ) -> Result { // We always need one memory copy to take ownership and for relocations let aligned_memory = AlignedMemory::<{ HOST_ALIGN }>::from_slice(bytes); let (mut elf_bytes, elf) = if is_memory_aligned(bytes.as_ptr() as usize, HOST_ALIGN) { @@ -409,11 +418,7 @@ impl Executable { let config = loader.get_config(); let header = elf.file_header(); - let sbpf_version = if header.e_flags == EF_SBPF_V2 { - SBPFVersion::V2 - } else { - SBPFVersion::V1 - }; + let sbpf_version = SBPFVersion::V1; Self::validate(config, &elf, elf_bytes.as_slice())?; @@ -536,11 +541,7 @@ impl Executable { return Err(ElfError::WrongType); } - let sbpf_version = if header.e_flags == EF_SBPF_V2 { - SBPFVersion::V2 - } else { - SBPFVersion::V1 - }; + let sbpf_version = SBPFVersion::V1; if !config.enabled_sbpf_versions.contains(&sbpf_version) { return Err(ElfError::UnsupportedSBPFVersion); } diff --git a/tests/elfs/program_headers_overflow.so b/tests/elfs/program_headers_overflow.so index 7b3469358499e360600e9c44725a5c90bddbacb2..4e6b92d75e6b3746e2005bc039147e93cd7df0c1 100755 GIT binary patch delta 12 TcmZ3WwLoiv0VBgk!}lTp95Vzv delta 12 TcmZ3WwLoiv0i(i3!}lTp9Mc3_