Skip to content

Commit

Permalink
Add error message when elf_parser fails on long section names
Browse files Browse the repository at this point in the history
Addresses: #532
  • Loading branch information
hiraditya authored and ksolana committed Oct 4, 2023
1 parent 32b1a6f commit f1f0042
Show file tree
Hide file tree
Showing 4 changed files with 34 additions and 2 deletions.
15 changes: 13 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/symbol name size `{0}` longer than `{1}` bytes.")]
InvalidStringTooLong(String, usize),
/// An index or memory range does exeed its boundaries
#[error("value out of bounds")]
OutOfBounds,
Expand Down Expand Up @@ -396,7 +400,14 @@ 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(|| {
let string_bytes_unparsed =
match String::from_utf8(unterminated_string_bytes.to_vec()) {
Ok(x) => x,
Err(_) => ElfParserError::InvalidString.to_string(),
};
ElfParserError::InvalidStringTooLong(string_bytes_unparsed, 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::InvalidStringTooLong(_, _)
| ElfParserError::InvalidSize
| ElfParserError::Overlap
| ElfParserError::SectionNotInOrder
Expand Down
Binary file added tests/elfs/long_section_name.so
Binary file not shown.
20 changes: 20 additions & 0 deletions tests/execution.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2208,6 +2208,26 @@ fn test_relative_call() {
);
}

#[test]
#[should_panic(
expected = r#"called `Result::unwrap()` on an `Err` value: ElfError(FailedToParse("Section/symbol name size `.bss.__rust_no_a` longer than `16` bytes."))"#
)]
fn test_long_section_name() {
test_interpreter_and_jit_elf!(
"tests/elfs/long_section_name.so",
[1],
(),
TestContextObject::new(18),
ProgramResult::Err(EbpfError::ElfError(
solana_rbpf::elf_parser::ElfParserError::InvalidStringTooLong(
".bss.__rust_no_a".to_string(),
solana_rbpf::elf_parser::SECTION_NAME_LENGTH_MAXIMUM
)
.into()
)),
);
}

#[test]
fn test_bpf_to_bpf_scratch_registers() {
test_interpreter_and_jit_asm!(
Expand Down

0 comments on commit f1f0042

Please sign in to comment.