| 1 |
use crate::errors::AndroidError; |
| 2 |
use crate::maps_reader::MappingInfo; |
| 3 |
use crate::ptrace_dumper::PtraceDumper; |
| 4 |
use crate::Pid; |
| 5 |
use goblin::elf; |
| 6 |
|
| 7 |
cfg_if::cfg_if! { |
| 8 |
if #[cfg(target_pointer_width = "32")] { |
| 9 |
use elf::dynamic::dyn32::{Dyn, SIZEOF_DYN}; |
| 10 |
use elf::header::header32 as elf_header; |
| 11 |
use elf::program_header::program_header32::ProgramHeader; |
| 12 |
|
| 13 |
const DT_ANDROID_REL: u32 = (elf::dynamic::DT_LOOS + 2) as u32; |
| 14 |
const DT_ANDROID_RELA: u32 = (elf::dynamic::DT_LOOS + 4) as u32; |
| 15 |
} else if #[cfg(target_pointer_width = "64")] { |
| 16 |
use elf::dynamic::dyn64::{Dyn, SIZEOF_DYN}; |
| 17 |
use elf::header::header64 as elf_header; |
| 18 |
use elf::program_header::program_header64::ProgramHeader; |
| 19 |
|
| 20 |
const DT_ANDROID_REL: u64 = elf::dynamic::DT_LOOS + 2; |
| 21 |
const DT_ANDROID_RELA: u64 = elf::dynamic::DT_LOOS + 4; |
| 22 |
} else { |
| 23 |
compile_error!("invalid pointer width"); |
| 24 |
} |
| 25 |
} |
| 26 |
|
| 27 |
type Result<T> = std::result::Result<T, AndroidError>; |
| 28 |
|
| 29 |
struct DynVaddresses { |
| 30 |
min_vaddr: usize, |
| 31 |
dyn_vaddr: usize, |
| 32 |
dyn_count: usize, |
| 33 |
} |
| 34 |
|
| 35 |
fn has_android_packed_relocations(pid: Pid, load_bias: usize, vaddrs: DynVaddresses) -> Result<()> { |
| 36 |
let dyn_addr = load_bias + vaddrs.dyn_vaddr; |
| 37 |
for idx in 0..vaddrs.dyn_count { |
| 38 |
let addr = dyn_addr + SIZEOF_DYN * idx; |
| 39 |
let dyn_data = PtraceDumper::copy_from_process(pid, addr, SIZEOF_DYN)?; |
| 40 |
// TODO: Couldn't find a nice way to use goblin for that, to avoid the unsafe-block |
| 41 |
let dyn_obj: Dyn; |
| 42 |
unsafe { |
| 43 |
dyn_obj = std::mem::transmute::<[u8; SIZEOF_DYN], Dyn>(dyn_data.as_slice().try_into()?); |
| 44 |
} |
| 45 |
|
| 46 |
if dyn_obj.d_tag == DT_ANDROID_REL || dyn_obj.d_tag == DT_ANDROID_RELA { |
| 47 |
return Ok(()); |
| 48 |
} |
| 49 |
} |
| 50 |
Err(AndroidError::NoRelFound) |
| 51 |
} |
| 52 |
|
| 53 |
fn get_effective_load_bias(pid: Pid, ehdr: &elf_header::Header, address: usize) -> usize { |
| 54 |
let ph = parse_loaded_elf_program_headers(pid, ehdr, address); |
| 55 |
// If |min_vaddr| is non-zero and we find Android packed relocation tags, |
| 56 |
// return the effective load bias. |
| 57 |
|
| 58 |
if ph.min_vaddr != 0 { |
| 59 |
let load_bias = address - ph.min_vaddr; |
| 60 |
if has_android_packed_relocations(pid, load_bias, ph).is_ok() { |
| 61 |
return load_bias; |
| 62 |
} |
| 63 |
} |
| 64 |
// Either |min_vaddr| is zero, or it is non-zero but we did not find the |
| 65 |
// expected Android packed relocations tags. |
| 66 |
address |
| 67 |
} |
| 68 |
|
| 69 |
fn parse_loaded_elf_program_headers( |
| 70 |
pid: Pid, |
| 71 |
ehdr: &elf_header::Header, |
| 72 |
address: usize, |
| 73 |
) -> DynVaddresses { |
| 74 |
let phdr_addr = address + ehdr.e_phoff as usize; |
| 75 |
let mut min_vaddr = usize::MAX; |
| 76 |
let mut dyn_vaddr = 0; |
| 77 |
let mut dyn_count = 0; |
| 78 |
|
| 79 |
let phdr_opt = PtraceDumper::copy_from_process( |
| 80 |
pid, |
| 81 |
phdr_addr, |
| 82 |
elf_header::SIZEOF_EHDR * ehdr.e_phnum as usize, |
| 83 |
); |
| 84 |
if let Ok(ph_data) = phdr_opt { |
| 85 |
// TODO: The original C code doesn't have error-handling here at all. |
| 86 |
// We silently ignore "not parsable" for now, but might bubble it up. |
| 87 |
// TODO2: `from_bytes` might panic, `parse()` would return a Result<>, so maybe better |
| 88 |
// to switch to that at some point. |
| 89 |
for phdr in ProgramHeader::from_bytes(&ph_data, ehdr.e_phnum as usize) { |
| 90 |
let p_vaddr = phdr.p_vaddr as usize; |
| 91 |
if phdr.p_type == elf::program_header::PT_LOAD && p_vaddr < min_vaddr { |
| 92 |
min_vaddr = p_vaddr; |
| 93 |
} |
| 94 |
|
| 95 |
if phdr.p_type == elf::program_header::PT_DYNAMIC { |
| 96 |
dyn_vaddr = p_vaddr; |
| 97 |
dyn_count = phdr.p_memsz as usize / SIZEOF_DYN; |
| 98 |
} |
| 99 |
} |
| 100 |
} |
| 101 |
|
| 102 |
DynVaddresses { |
| 103 |
min_vaddr, |
| 104 |
dyn_vaddr, |
| 105 |
dyn_count, |
| 106 |
} |
| 107 |
} |
| 108 |
|
| 109 |
pub fn late_process_mappings(pid: Pid, mappings: &mut [MappingInfo]) -> Result<()> { |
| 110 |
// Only consider exec mappings that indicate a file path was mapped, and |
| 111 |
// where the ELF header indicates a mapped shared library. |
| 112 |
for map in mappings |
| 113 |
.iter_mut() |
| 114 |
.filter(|m| m.is_executable() && m.name_is_path()) |
| 115 |
{ |
| 116 |
let ehdr_opt = |
| 117 |
PtraceDumper::copy_from_process(pid, map.start_address, elf_header::SIZEOF_EHDR) |
| 118 |
.ok() |
| 119 |
.and_then(|x| elf_header::Header::parse(&x).ok()); |
| 120 |
|
| 121 |
if let Some(ehdr) = ehdr_opt { |
| 122 |
if ehdr.e_type == elf_header::ET_DYN { |
| 123 |
// Compute the effective load bias for this mapped library, and update |
| 124 |
// the mapping to hold that rather than |start_addr|, at the same time |
| 125 |
// adjusting |size| to account for the change in |start_addr|. Where |
| 126 |
// the library does not contain Android packed relocations, |
| 127 |
// GetEffectiveLoadBias() returns |start_addr| and the mapping entry |
| 128 |
// is not changed. |
| 129 |
let load_bias = get_effective_load_bias(pid, &ehdr, map.start_address); |
| 130 |
map.size += map.start_address - load_bias; |
| 131 |
map.start_address = load_bias; |
| 132 |
} |
| 133 |
} |
| 134 |
} |
| 135 |
Ok(()) |
| 136 |
} |