use crate::{elf_parse as elf, util};
use thiserror::Error;
use zx::{self as zx, AsHandleRef};
#[derive(Error, Debug)]
pub enum ElfLoadError {
#[error("ELF load segments were empty")]
NothingToLoad,
#[error("Failed to allocate VMAR for ELF: {}", _0)]
VmarAllocate(zx::Status),
#[error("Failed to map VMAR: {}", _0)]
VmarMap(zx::Status),
#[error("Failed to create CoW VMO clone: {}", _0)]
VmoCowClone(zx::Status),
#[error("Failed to create VMO: {}", _0)]
VmoCreate(zx::Status),
#[error("Failed to read from VMO: {}", _0)]
VmoRead(zx::Status),
#[error("Failed to write to VMO: {}", _0)]
VmoWrite(zx::Status),
#[error("Failed to get VMO name: {}", _0)]
GetVmoName(zx::Status),
#[error("Failed to set VMO name: {}", _0)]
SetVmoName(zx::Status),
}
impl ElfLoadError {
pub fn as_zx_status(&self) -> zx::Status {
match self {
ElfLoadError::NothingToLoad => zx::Status::NOT_FOUND,
ElfLoadError::VmarAllocate(s)
| ElfLoadError::VmarMap(s)
| ElfLoadError::VmoCowClone(s)
| ElfLoadError::VmoCreate(s)
| ElfLoadError::VmoRead(s)
| ElfLoadError::VmoWrite(s)
| ElfLoadError::GetVmoName(s)
| ElfLoadError::SetVmoName(s) => *s,
}
}
}
#[derive(Debug)]
pub struct LoadedElfInfo {
pub low: usize,
pub high: usize,
pub max_perm: elf::SegmentFlags,
}
pub fn loaded_elf_info(headers: &elf::Elf64Headers) -> LoadedElfInfo {
let (mut first, mut low, mut high) = (true, 0, 0);
let mut max_perm = elf::SegmentFlags::empty();
for hdr in headers.program_headers_with_type(elf::SegmentType::Load) {
if first {
low = util::page_start(hdr.vaddr);
first = false;
}
high = util::page_end(hdr.vaddr + hdr.memsz as usize);
max_perm |= hdr.flags();
}
LoadedElfInfo { low, high, max_perm }
}
#[derive(Debug)]
pub struct LoadedElf {
pub vmar: zx::Vmar,
pub vmar_base: usize,
pub entry: usize,
}
pub trait Mapper {
fn map(
&self,
vmar_offset: usize,
vmo: &zx::Vmo,
vmo_offset: u64,
length: usize,
flags: zx::VmarFlags,
) -> Result<usize, zx::Status>;
}
impl Mapper for zx::Vmar {
fn map(
&self,
vmar_offset: usize,
vmo: &zx::Vmo,
vmo_offset: u64,
length: usize,
flags: zx::VmarFlags,
) -> Result<usize, zx::Status> {
Self::map(self, vmar_offset, vmo, vmo_offset, length, flags)
}
}
pub fn load_elf(
vmo: &zx::Vmo,
headers: &elf::Elf64Headers,
root_vmar: &zx::Vmar,
) -> Result<LoadedElf, ElfLoadError> {
let info = loaded_elf_info(headers);
let size = info.high - info.low;
if size == 0 {
return Err(ElfLoadError::NothingToLoad);
}
let flags = zx::VmarFlags::CAN_MAP_SPECIFIC | elf_to_vmar_can_map_flags(&info.max_perm);
let (vmar, vmar_base) =
root_vmar.allocate(0, size, flags).map_err(|s| ElfLoadError::VmarAllocate(s))?;
let vaddr_bias = vmar_base.wrapping_sub(info.low);
map_elf_segments(vmo, headers, &vmar, vmar_base, vaddr_bias)?;
Ok(LoadedElf { vmar, vmar_base, entry: headers.file_header().entry.wrapping_add(vaddr_bias) })
}
pub fn map_elf_segments(
vmo: &zx::Vmo,
headers: &elf::Elf64Headers,
mapper: &dyn Mapper,
mapper_base: usize,
vaddr_bias: usize,
) -> Result<(), ElfLoadError> {
let mapper_relative_bias = vaddr_bias.wrapping_sub(mapper_base);
let vmo_name = vmo.get_name().map_err(|s| ElfLoadError::GetVmoName(s))?;
for hdr in headers.program_headers_with_type(elf::SegmentType::Load) {
let adjust = util::page_offset(hdr.offset);
let mut file_offset = hdr.offset - adjust;
let file_size = hdr.filesz + adjust as u64;
let virt_offset = hdr.vaddr - adjust;
let virt_size = hdr.memsz + adjust as u64;
let virt_addr = virt_offset.wrapping_add(mapper_relative_bias);
let must_write = virt_size > file_size && util::page_offset(file_size as usize) != 0;
let vmo_to_map: &zx::Vmo;
let writeable_vmo: zx::Vmo;
if must_write || (file_size > 0 && hdr.flags().contains(elf::SegmentFlags::WRITE)) {
writeable_vmo = vmo
.create_child(
zx::VmoChildOptions::SNAPSHOT_AT_LEAST_ON_WRITE,
file_offset as u64,
util::page_end(file_size as usize) as u64,
)
.map_err(ElfLoadError::VmoCowClone)?;
writeable_vmo
.set_name(&vmo_name_with_prefix(&vmo_name, VMO_NAME_PREFIX_DATA))
.map_err(ElfLoadError::SetVmoName)?;
file_offset = 0;
if virt_size > file_size {
let memset_size = util::page_end(file_size as usize) - file_size as usize;
if memset_size > 0 {
writeable_vmo
.write(&vec![0u8; memset_size], file_size)
.map_err(|s| ElfLoadError::VmoWrite(s))?;
}
}
vmo_to_map = &writeable_vmo;
} else {
vmo_to_map = vmo;
}
let flags = zx::VmarFlags::SPECIFIC
| zx::VmarFlags::ALLOW_FAULTS
| elf_to_vmar_perm_flags(&hdr.flags());
if file_size != 0 {
mapper
.map(
virt_addr,
vmo_to_map,
file_offset as u64,
util::page_end(file_size as usize),
flags,
)
.map_err(ElfLoadError::VmarMap)?;
}
if virt_size > file_size {
let bss_vmo_start = util::page_end(file_size as usize);
let bss_vmo_size = util::page_end(virt_size as usize) - bss_vmo_start;
if bss_vmo_size > 0 {
let anon_vmo =
zx::Vmo::create(bss_vmo_size as u64).map_err(|s| ElfLoadError::VmoCreate(s))?;
anon_vmo
.set_name(&vmo_name_with_prefix(&vmo_name, VMO_NAME_PREFIX_BSS))
.map_err(ElfLoadError::SetVmoName)?;
mapper
.map(virt_addr + bss_vmo_start, &anon_vmo, 0, bss_vmo_size, flags)
.map_err(ElfLoadError::VmarMap)?;
}
}
}
Ok(())
}
const VMO_NAME_PREFIX_BSS: &str = "bss:";
const VMO_NAME_PREFIX_DATA: &str = "data:";
fn vmo_name_with_prefix(name: &zx::Name, prefix: &str) -> zx::Name {
assert!(prefix.len() <= zx::sys::ZX_MAX_NAME_LEN - 1);
if name.is_empty() {
zx::Name::new_lossy(&format!("{prefix}<unknown ELF>"))
} else {
zx::Name::new_lossy(&format!("{prefix}{name}"))
}
}
fn elf_to_vmar_can_map_flags(elf_flags: &elf::SegmentFlags) -> zx::VmarFlags {
let mut flags = zx::VmarFlags::empty();
if elf_flags.contains(elf::SegmentFlags::READ) {
flags |= zx::VmarFlags::CAN_MAP_READ;
}
if elf_flags.contains(elf::SegmentFlags::WRITE) {
flags |= zx::VmarFlags::CAN_MAP_WRITE;
}
if elf_flags.contains(elf::SegmentFlags::EXECUTE) {
flags |= zx::VmarFlags::CAN_MAP_EXECUTE | zx::VmarFlags::CAN_MAP_READ;
}
flags
}
fn elf_to_vmar_perm_flags(elf_flags: &elf::SegmentFlags) -> zx::VmarFlags {
let mut flags = zx::VmarFlags::empty();
if elf_flags.contains(elf::SegmentFlags::READ) {
flags |= zx::VmarFlags::PERM_READ;
}
if elf_flags.contains(elf::SegmentFlags::WRITE) {
flags |= zx::VmarFlags::PERM_WRITE;
}
if elf_flags.contains(elf::SegmentFlags::EXECUTE) {
flags |= zx::VmarFlags::PERM_EXECUTE | zx::VmarFlags::PERM_READ_IF_XOM_UNSUPPORTED;
}
flags
}
#[cfg(test)]
mod tests {
use super::*;
use crate::elf_parse;
use assert_matches::assert_matches;
use fidl::HandleBased;
use lazy_static::lazy_static;
use std::cell::RefCell;
use std::mem::size_of;
#[test]
fn test_vmo_name_with_prefix() {
let empty_vmo_name = zx::Name::default();
let short_vmo_name = zx::Name::new("short_vmo_name").unwrap();
let max_vmo_name = zx::Name::new("a_great_maximum_length_vmo_name").unwrap();
assert_eq!(vmo_name_with_prefix(&empty_vmo_name, VMO_NAME_PREFIX_BSS), "bss:<unknown ELF>");
assert_eq!(
vmo_name_with_prefix(&short_vmo_name, VMO_NAME_PREFIX_BSS),
"bss:short_vmo_name",
);
assert_eq!(
vmo_name_with_prefix(&max_vmo_name, VMO_NAME_PREFIX_BSS),
"bss:a_great_maximum_length_vmo_",
);
assert_eq!(
vmo_name_with_prefix(&max_vmo_name, VMO_NAME_PREFIX_DATA),
"data:a_great_maximum_length_vmo",
);
assert_eq!(
vmo_name_with_prefix(&empty_vmo_name, "a_long_vmo_name_prefix:"),
"a_long_vmo_name_prefix:<unknown",
);
assert_eq!(
vmo_name_with_prefix(&empty_vmo_name, "a_great_maximum_length_vmo_name"),
max_vmo_name,
);
assert_eq!(
vmo_name_with_prefix(&max_vmo_name, "anystringhere"),
"anystringherea_great_maximum_le"
);
}
#[derive(Debug)]
struct RecordedMapping {
vmo: zx::Vmo,
vmo_offset: u64,
length: usize,
flags: zx::VmarFlags,
}
struct TrackingMapper(RefCell<Vec<RecordedMapping>>);
impl TrackingMapper {
fn new() -> Self {
Self(RefCell::new(Vec::new()))
}
}
impl IntoIterator for TrackingMapper {
type Item = RecordedMapping;
type IntoIter = std::vec::IntoIter<Self::Item>;
fn into_iter(self) -> Self::IntoIter {
self.0.into_inner().into_iter()
}
}
impl Mapper for TrackingMapper {
fn map(
&self,
vmar_offset: usize,
vmo: &zx::Vmo,
vmo_offset: u64,
length: usize,
flags: zx::VmarFlags,
) -> Result<usize, zx::Status> {
self.0.borrow_mut().push(RecordedMapping {
vmo: vmo.as_handle_ref().duplicate(zx::Rights::SAME_RIGHTS).unwrap().into(),
vmo_offset,
length,
flags,
});
Ok(vmar_offset)
}
}
const ELF_FILE_HEADER: &elf_parse::Elf64FileHeader = &elf_parse::Elf64FileHeader {
ident: elf_parse::ElfIdent {
magic: elf_parse::ELF_MAGIC,
class: elf_parse::ElfClass::Elf64 as u8,
data: elf_parse::NATIVE_ENCODING as u8,
version: elf_parse::ElfVersion::Current as u8,
osabi: 0x00,
abiversion: 0x00,
pad: [0; 7],
},
elf_type: elf_parse::ElfType::SharedObject as u16,
machine: elf_parse::CURRENT_ARCH as u16,
version: elf_parse::ElfVersion::Current as u32,
entry: 0x10000,
phoff: size_of::<elf_parse::Elf64FileHeader>(),
shoff: 0,
flags: 0,
ehsize: size_of::<elf_parse::Elf64FileHeader>() as u16,
phentsize: size_of::<elf_parse::Elf64ProgramHeader>() as u16,
phnum: 1,
shentsize: 0,
shnum: 0,
shstrndx: 0,
};
const VMO_DEFAULT_RIGHTS: zx::Rights = zx::Rights::from_bits_truncate(
zx::Rights::DUPLICATE.bits()
| zx::Rights::TRANSFER.bits()
| zx::Rights::READ.bits()
| zx::Rights::WRITE.bits()
| zx::Rights::MAP.bits()
| zx::Rights::GET_PROPERTY.bits()
| zx::Rights::SET_PROPERTY.bits(),
);
#[test]
fn map_read_only_with_page_unaligned_bss() {
const ELF_DATA: &[u8; 8] = b"FUCHSIA!";
lazy_static! {
static ref PAGE_SIZE: usize = zx::system_get_page_size() as usize;
static ref ELF_PROGRAM_HEADER: elf_parse::Elf64ProgramHeader =
elf_parse::Elf64ProgramHeader {
segment_type: elf_parse::SegmentType::Load as u32,
flags: elf_parse::SegmentFlags::from_bits_truncate(
elf_parse::SegmentFlags::READ.bits()
| elf_parse::SegmentFlags::EXECUTE.bits(),
)
.bits(),
offset: *PAGE_SIZE,
vaddr: 0x10000,
paddr: 0x10000,
filesz: ELF_DATA.len() as u64,
memsz: 0x100,
align: *PAGE_SIZE as u64,
};
}
let headers = elf_parse::Elf64Headers::new_for_test(
ELF_FILE_HEADER,
Some(std::slice::from_ref(&ELF_PROGRAM_HEADER)),
);
let vmo = zx::Vmo::create(*PAGE_SIZE as u64 * 2).expect("create VMO");
let data = vec![0xff; *PAGE_SIZE * 2];
vmo.write(&data, 0).expect("fill VMO with 0xff");
vmo.write(ELF_DATA, *PAGE_SIZE as u64).expect("write data to VMO");
let vmo =
vmo.replace_handle(VMO_DEFAULT_RIGHTS - zx::Rights::WRITE).expect("remove WRITE right");
let mapper = TrackingMapper::new();
map_elf_segments(&vmo, &headers, &mapper, 0, 0).expect("map ELF segments");
let mut mapping_iter = mapper.into_iter();
let mapping = mapping_iter.next().expect("mapping from ELF VMO");
let mut data = vec![0; *PAGE_SIZE];
mapping.vmo.read(&mut data, mapping.vmo_offset).expect("read VMO");
let expected = ELF_DATA
.into_iter()
.cloned()
.chain(std::iter::repeat(0).take(*PAGE_SIZE - ELF_DATA.len()))
.collect::<Vec<u8>>();
assert_eq!(&expected, &data);
assert_matches!(mapping_iter.next(), None);
}
#[test]
fn map_read_only_vmo_with_page_aligned_bss() {
lazy_static! {
static ref PAGE_SIZE: usize = zx::system_get_page_size() as usize;
static ref ELF_PROGRAM_HEADER: elf_parse::Elf64ProgramHeader =
elf_parse::Elf64ProgramHeader {
segment_type: elf_parse::SegmentType::Load as u32,
flags: elf_parse::SegmentFlags::from_bits_truncate(
elf_parse::SegmentFlags::READ.bits()
| elf_parse::SegmentFlags::EXECUTE.bits(),
)
.bits(),
offset: *PAGE_SIZE as usize,
vaddr: 0x10000,
paddr: 0x10000,
filesz: *PAGE_SIZE as u64,
memsz: *PAGE_SIZE as u64 * 2,
align: *PAGE_SIZE as u64,
};
}
let headers = elf_parse::Elf64Headers::new_for_test(
ELF_FILE_HEADER,
Some(std::slice::from_ref(&ELF_PROGRAM_HEADER)),
);
let vmo = zx::Vmo::create(*PAGE_SIZE as u64 * 2).expect("create VMO");
let pattern = vec![0xff; *PAGE_SIZE * 2];
vmo.write(&pattern, 0).expect("fill VMO with 0xff");
let vmo =
vmo.replace_handle(VMO_DEFAULT_RIGHTS - zx::Rights::WRITE).expect("remove WRITE right");
let mapper = TrackingMapper::new();
map_elf_segments(&vmo, &headers, &mapper, 0, 0).expect("map ELF segments");
let mut mapping_iter = mapper.into_iter();
let mapping = mapping_iter.next().expect("mapping from ELF VMO");
assert_eq!(mapping.vmo.get_koid().unwrap(), vmo.get_koid().unwrap());
let mut data = vec![0; *PAGE_SIZE];
mapping.vmo.read(&mut data, mapping.vmo_offset).expect("read ELF VMO");
assert_eq!(&data, &pattern[0..*PAGE_SIZE]);
let mapping = mapping_iter.next().expect("mapping from BSS VMO");
mapping.vmo.read(&mut data, mapping.vmo_offset).expect("read BSS VMO");
let zero = vec![0; *PAGE_SIZE];
assert_eq!(&data, &zero);
assert_matches!(mapping_iter.next(), None);
}
#[test]
fn map_read_only_vmo_with_no_bss() {
lazy_static! {
static ref PAGE_SIZE: usize = zx::system_get_page_size() as usize;
static ref ELF_PROGRAM_HEADER: elf_parse::Elf64ProgramHeader =
elf_parse::Elf64ProgramHeader {
segment_type: elf_parse::SegmentType::Load as u32,
flags: elf_parse::SegmentFlags::from_bits_truncate(
elf_parse::SegmentFlags::READ.bits()
| elf_parse::SegmentFlags::EXECUTE.bits(),
)
.bits(),
offset: *PAGE_SIZE as usize,
vaddr: 0x10000,
paddr: 0x10000,
filesz: *PAGE_SIZE as u64,
memsz: *PAGE_SIZE as u64,
align: *PAGE_SIZE as u64,
};
}
let headers = elf_parse::Elf64Headers::new_for_test(
ELF_FILE_HEADER,
Some(std::slice::from_ref(&ELF_PROGRAM_HEADER)),
);
let vmo = zx::Vmo::create(*PAGE_SIZE as u64 * 2).expect("create VMO");
let pattern = vec![0xff; *PAGE_SIZE * 2];
vmo.write(&pattern, 0).expect("fill VMO with 0xff");
let vmo =
vmo.replace_handle(VMO_DEFAULT_RIGHTS - zx::Rights::WRITE).expect("remove WRITE right");
let mapper = TrackingMapper::new();
map_elf_segments(&vmo, &headers, &mapper, 0, 0).expect("map ELF segments");
let mut mapping_iter = mapper.into_iter();
let mapping = mapping_iter.next().expect("mapping from ELF VMO");
assert_eq!(mapping.vmo.get_koid().unwrap(), vmo.get_koid().unwrap());
let mut data = vec![0; *PAGE_SIZE];
mapping.vmo.read(&mut data, mapping.vmo_offset).expect("read ELF VMO");
assert_eq!(&data, &pattern[0..*PAGE_SIZE]);
assert_matches!(mapping_iter.next(), None);
}
#[test]
fn map_read_only_vmo_with_write_flag() {
lazy_static! {
static ref PAGE_SIZE: usize = zx::system_get_page_size() as usize;
static ref ELF_PROGRAM_HEADER: elf_parse::Elf64ProgramHeader =
elf_parse::Elf64ProgramHeader {
segment_type: elf_parse::SegmentType::Load as u32,
flags: elf_parse::SegmentFlags::from_bits_truncate(
elf_parse::SegmentFlags::READ.bits()
| elf_parse::SegmentFlags::WRITE.bits(),
)
.bits(),
offset: *PAGE_SIZE as usize,
vaddr: 0x10000,
paddr: 0x10000,
filesz: *PAGE_SIZE as u64,
memsz: *PAGE_SIZE as u64,
align: *PAGE_SIZE as u64,
};
}
let headers = elf_parse::Elf64Headers::new_for_test(
ELF_FILE_HEADER,
Some(std::slice::from_ref(&ELF_PROGRAM_HEADER)),
);
let vmo = zx::Vmo::create(*PAGE_SIZE as u64 * 2).expect("create VMO");
let vmo =
vmo.replace_handle(VMO_DEFAULT_RIGHTS - zx::Rights::WRITE).expect("remove WRITE right");
let mapper = TrackingMapper::new();
map_elf_segments(&vmo, &headers, &mapper, 0, 0).expect("map ELF segments");
let mut mapping_iter = mapper.into_iter();
let mapping = mapping_iter.next().expect("mapping from ELF VMO");
assert_ne!(mapping.vmo.get_koid().unwrap(), vmo.get_koid().unwrap());
mapping.vmo.write(b"FUCHSIA!", mapping.vmo_offset).expect("write to COW VMO");
assert_matches!(mapping_iter.next(), None);
}
#[test]
fn segment_with_zero_file_size() {
lazy_static! {
static ref PAGE_SIZE: usize = zx::system_get_page_size() as usize;
static ref ELF_PROGRAM_HEADER: elf_parse::Elf64ProgramHeader =
elf_parse::Elf64ProgramHeader {
segment_type: elf_parse::SegmentType::Load as u32,
flags: elf_parse::SegmentFlags::from_bits_truncate(
elf_parse::SegmentFlags::READ.bits()
| elf_parse::SegmentFlags::WRITE.bits(),
)
.bits(),
offset: *PAGE_SIZE as usize,
vaddr: 0x10000,
paddr: 0x10000,
filesz: 0,
memsz: 1,
align: *PAGE_SIZE as u64,
};
}
let headers = elf_parse::Elf64Headers::new_for_test(
ELF_FILE_HEADER,
Some(std::slice::from_ref(&ELF_PROGRAM_HEADER)),
);
let vmo = zx::Vmo::create(*PAGE_SIZE as u64 * 2).expect("create VMO");
let mapper = TrackingMapper::new();
map_elf_segments(&vmo, &headers, &mapper, 0, 0).expect("map ELF segments");
for mapping in mapper.into_iter() {
assert!(mapping.length != 0);
}
}
#[test]
fn map_execute_only_segment() {
lazy_static! {
static ref PAGE_SIZE: usize = zx::system_get_page_size() as usize;
static ref ELF_PROGRAM_HEADER: elf_parse::Elf64ProgramHeader =
elf_parse::Elf64ProgramHeader {
segment_type: elf_parse::SegmentType::Load as u32,
flags: elf_parse::SegmentFlags::from_bits_truncate(
elf_parse::SegmentFlags::EXECUTE.bits(),
)
.bits(),
offset: *PAGE_SIZE as usize,
vaddr: 0x10000,
paddr: 0x10000,
filesz: 0x10,
memsz: 0x10,
align: *PAGE_SIZE as u64,
};
}
let headers = elf_parse::Elf64Headers::new_for_test(
ELF_FILE_HEADER,
Some(std::slice::from_ref(&ELF_PROGRAM_HEADER)),
);
let vmo = zx::Vmo::create(*PAGE_SIZE as u64 * 2).expect("create VMO");
let mapper = TrackingMapper::new();
map_elf_segments(&vmo, &headers, &mapper, 0, 0).expect("map ELF segments");
let mut mapping_iter = mapper.into_iter();
let mapping = mapping_iter.next().expect("mapping from ELF VMO");
assert_eq!(
mapping.flags,
zx::VmarFlags::SPECIFIC
| zx::VmarFlags::ALLOW_FAULTS
| zx::VmarFlags::PERM_EXECUTE
| zx::VmarFlags::PERM_READ_IF_XOM_UNSUPPORTED
);
assert_matches!(mapping_iter.next(), None);
}
}