Skip to content

Commit

Permalink
elf: error out if sbpf_version.enable_elf_vaddr() and config.optimize…
Browse files Browse the repository at this point in the history
…_rodata=false (#478)

Enabling virtual addresses requires optimize_rodata=true. Otherwise with
optimize_rodata=false we allocate a vec where we join all the readonly
sections, and if we allowed virtual addresses then we'd potentially have
to allocate huge amounts of memory.
  • Loading branch information
alessandrod authored Jun 30, 2023
1 parent d91f880 commit 6971ce9
Show file tree
Hide file tree
Showing 4 changed files with 45 additions and 19 deletions.
14 changes: 12 additions & 2 deletions src/elf.rs
Original file line number Diff line number Diff line change
Expand Up @@ -718,6 +718,13 @@ impl<V: Verifier, C: ContextObject> Executable<V, C> {
};

if sbpf_version.enable_elf_vaddr() {
if !config.optimize_rodata {
// When optimize_rodata=false, we allocate a vector and copy all
// rodata sections into it. In that case we can't allow virtual
// addresses or we'd potentially have to do huge allocations.
return Err(ElfError::UnsupportedSBPFVersion);
}

// This is needed to avoid an overflow error in header.vm_range() as
// used by relocate(). See https://github.com/m4b/goblin/pull/306.
//
Expand Down Expand Up @@ -845,6 +852,8 @@ impl<V: Verifier, C: ContextObject> Executable<V, C> {
// sh_offset for backwards compatibility
if !invalid_offsets {
if sbpf_version.enable_elf_vaddr() {
// This is enforced in validate()
debug_assert!(config.optimize_rodata);
if section_addr < section_header.sh_offset() {
invalid_offsets = true;
} else {
Expand Down Expand Up @@ -1969,8 +1978,9 @@ mod test {
(Some(b".dynamic"), &s2),
(Some(b".rodata"), &s3),
];
// V2 requires optimize_rodata=true
let ro_section =
ElfExecutable::parse_ro_sections(&config, &SBPFVersion::V2, sections, &elf_bytes)
ElfExecutable::parse_ro_sections(&config, &SBPFVersion::V1, sections, &elf_bytes)
.unwrap();
let ro_region = get_ro_region(&ro_section, &elf_bytes);
let owned_section = match &ro_section {
Expand Down Expand Up @@ -2069,7 +2079,7 @@ mod test {
assert!(matches!(
ElfExecutable::parse_ro_sections(
&config,
&SBPFVersion::V2,
&SBPFVersion::V1, // v2 requires optimize_rodata=true
sections,
&elf_bytes,
),
Expand Down
4 changes: 4 additions & 0 deletions tests/elfs/elfs.sh
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,10 @@ rm bss_section.o
"$LLVM_DIR"ld.lld $LD_FLAGS -o rodata.so rodata.o
rm rodata.o

"$LLVM_DIR"clang $CC_FLAGS -mcpu=generic -o rodata.o -c rodata.c
"$LLVM_DIR"ld.lld $LD_FLAGS -o rodata_sbpfv1.so rodata.o
rm rodata.o

"$LLVM_DIR"clang $CC_FLAGS -o rodata_high_vaddr.o -c rodata.c
"$LLVM_DIR"ld.lld -z notext -shared --Bdynamic -entry entrypoint --nmagic --section-start=.text=0x100000000 --section-start=.rodata=0x100000020 -o rodata_high_vaddr.so rodata_high_vaddr.o
rm rodata_high_vaddr.o
Expand Down
Binary file added tests/elfs/rodata_sbpfv1.so
Binary file not shown.
46 changes: 29 additions & 17 deletions tests/execution.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3097,23 +3097,35 @@ fn test_load_elf_empty_rodata() {
}

#[test]
fn test_load_elf_rodata() {
// checks that the program loads the correct rodata offset with both
// borrowed and owned rodata
for optimize_rodata in [false, true] {
let config = Config {
optimize_rodata,
..Config::default()
};
test_interpreter_and_jit_elf!(
"tests/elfs/rodata.so",
config,
[],
(),
TestContextObject::new(3),
ProgramResult::Ok(42),
);
}
fn test_load_elf_rodata_sbpfv2() {
let config = Config {
optimize_rodata: true,
..Config::default()
};
test_interpreter_and_jit_elf!(
"tests/elfs/rodata.so",
config,
[],
(),
TestContextObject::new(3),
ProgramResult::Ok(42),
);
}

#[test]
fn test_load_elf_rodata_sbpfv1() {
let config = Config {
optimize_rodata: false,
..Config::default()
};
test_interpreter_and_jit_elf!(
"tests/elfs/rodata_sbpfv1.so",
config,
[],
(),
TestContextObject::new(3),
ProgramResult::Ok(42),
);
}

#[test]
Expand Down

0 comments on commit 6971ce9

Please sign in to comment.