Skip to content

Wrong Arithmetic When Computing Guest Code Size #98

@ppenna

Description

@ppenna

Context

The ElfInfo::get_va_size() function is used for determining the size of the gest_code slot during the construction of the sand memory layout.

This function calculates the size based on the loadable program headers, which represent the size of the ELF binary when loaded into memory.

Problem

Although the current implementation works, it does not account for guest binaries that have non-loadable segments which still occupy memory at runtime, such as the BSS segment. As a result, the size computation for these binaries will be incorrect.

Solution

To fix this, we should consider arithmetic on sections that are tagged as SHF_ALLOC. From the ELF Specification [1]:

The section occupies memory during process execution. Some control
sections do not reside in the memory image of an object file; this attribute
is off for those sections.

Code References

pub(crate) fn get_va_size(&self) -> usize {
let max_phdr = self
.phdrs
.iter()
.rev()
.find(|phdr| phdr.p_type == PT_LOAD)
.unwrap(); // guaranteed not to panic because of the check in new()
(max_phdr.p_vaddr + max_phdr.p_memsz - self.get_base_va()) as usize
}

pub(super) fn new(
cfg: SandboxConfiguration,
code_size: usize,
stack_size: usize,
heap_size: usize,
) -> Result<Self> {

External References

  1. https://refspecs.linuxfoundation.org/elf/elf.pdf

Metadata

Metadata

Assignees

Labels

Type

No type

Projects

Status

No status

Milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions