Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add error message when elf_parser fails on long section names #537

Merged
merged 1 commit into from
Oct 6, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 14 additions & 0 deletions src/elf.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1188,6 +1188,7 @@ mod test {
// FIXME consts::{ELFCLASS32, ELFDATA2MSB, ET_REL},
consts::{ELFCLASS32, ELFDATA2MSB, ET_REL},
types::{Elf64Ehdr, Elf64Shdr},
SECTION_NAME_LENGTH_MAXIMUM,
},
error::ProgramResult,
fuzz::fuzz,
Expand Down Expand Up @@ -1966,4 +1967,17 @@ mod test {
LittleEndian::write_i32(&mut elf_bytes[0x1064..0x1068], 5);
ElfExecutable::load(&elf_bytes, loader()).expect("validation failed");
}

#[test]
fn test_long_section_name() {
let elf_bytes = std::fs::read("tests/elfs/long_section_name.so").unwrap();
assert_error!(
NewParser::parse(&elf_bytes),
"FailedToParse(\"Section or symbol name `{}` is longer than `{}` bytes\")",
".bss.__rust_no_alloc_shim_is_unstable"
.get(0..SECTION_NAME_LENGTH_MAXIMUM)
.unwrap(),
SECTION_NAME_LENGTH_MAXIMUM
);
}
}
13 changes: 11 additions & 2 deletions src/elf_parser/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,8 @@ use std::{fmt, mem, ops::Range, slice};
use crate::{ArithmeticOverflow, ErrCheckedArithmetic};
use {consts::*, types::*};

const SECTION_NAME_LENGTH_MAXIMUM: usize = 16;
/// Maximum length of section name allowed.
pub const SECTION_NAME_LENGTH_MAXIMUM: usize = 16;
const SYMBOL_NAME_LENGTH_MAXIMUM: usize = 64;

/// Error definitions
Expand All @@ -26,6 +27,9 @@ pub enum ElfParserError {
/// Section or symbol name is not UTF8 or too long
#[error("invalid string")]
InvalidString,
/// Section or symbol name is too long
#[error("Section or symbol name `{0}` is longer than `{1}` bytes")]
StringTooLong(String, usize),
/// An index or memory range does exeed its boundaries
#[error("value out of bounds")]
OutOfBounds,
Expand Down Expand Up @@ -396,7 +400,12 @@ impl<'a> Elf64<'a> {
.iter()
.position(|byte| *byte == 0x00)
.and_then(|string_length| unterminated_string_bytes.get(0..string_length))
.ok_or(ElfParserError::InvalidString)
.ok_or_else(|| {
ElfParserError::StringTooLong(
String::from_utf8_lossy(unterminated_string_bytes).to_string(),
maximum_length,
)
})
}

/// Returns the string corresponding to the given `sh_name`
Expand Down
1 change: 1 addition & 0 deletions src/elf_parser_glue.rs
Original file line number Diff line number Diff line change
Expand Up @@ -527,6 +527,7 @@ impl From<ElfParserError> for ElfError {
match err {
ElfParserError::InvalidSectionHeader
| ElfParserError::InvalidString
| ElfParserError::StringTooLong(_, _)
| ElfParserError::InvalidSize
| ElfParserError::Overlap
| ElfParserError::SectionNotInOrder
Expand Down
Binary file added tests/elfs/long_section_name.so
Binary file not shown.