Repository navigation
Expand file tree
/
Copy pathlib.rs
More file actions
244 lines (222 loc) · 8.4 KB
/
Copy pathlib.rs
File metadata and controls
244 lines (222 loc) · 8.4 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
#![deny(clippy::unwrap_used, clippy::expect_used, clippy::panic, clippy::unimplemented, clippy::todo, clippy::unreachable)]
#![forbid(unsafe_code)]
//! ELF program headers and dynamic section entries from untrusted bytes, 32- or 64-bit in either
//! byte order: [`program_headers`], [`dynamic_entries`], [`va_to_off`], and the segment type,
//! permission and dynamic tag names.
//!
//! From Craft Anything (`crates/formats/src/elf.rs`), where the mitigations, dynamic symbols,
//! symbol versions and core-file notes built on these stay. With the `serde` feature,
//! [`ProgramHeader`] is `Serialize` with `vaddr` as a `0x…` string.
mod bytes;
#[cfg(feature = "serde")]
mod hex;
use crate::bytes::Reader;
/// Program headers read from one table.
pub const MAX_PHDRS: usize = 4096;
/// Dynamic entries read from one segment.
pub const MAX_DYN: usize = 65_536;
/// Why a read gave up.
#[derive(Debug, Clone, PartialEq, Eq, thiserror::Error)]
pub enum ElfError {
/// The ELF header ends before the file does.
#[error("the file ends early ({0})")]
Truncated(&'static str),
/// No ELF magic, or an invalid class, encoding or field.
#[error("the file is malformed ({0})")]
Malformed(String),
}
/// Endian/class-aware reads over an ELF image.
#[derive(Clone, Copy)]
struct E<'a> {
r: Reader<'a>,
is64: bool,
le: bool,
}
impl<'a> E<'a> {
fn new(data: &'a [u8]) -> Result<Self, ElfError> {
if !data.starts_with(b"\x7fELF") {
return Err(ElfError::Malformed("no ELF magic".into()));
}
let r = Reader::new(data);
let class = r.u8(4).ok_or(ElfError::Truncated("ELF ident"))?;
let enc = r.u8(5).ok_or(ElfError::Truncated("ELF ident"))?;
if !matches!(class, 1 | 2) || !matches!(enc, 1 | 2) {
return Err(ElfError::Malformed("bad ELF class or data encoding".into()));
}
Ok(E { r, is64: class == 2, le: enc == 1 })
}
fn u16(&self, off: usize) -> Option<u16> {
if self.le { self.r.u16_le(off) } else { self.r.u16_be(off) }
}
fn u32(&self, off: usize) -> Option<u32> {
if self.le { self.r.u32_le(off) } else { self.r.u32_be(off) }
}
fn u64(&self, off: usize) -> Option<u64> {
if self.le { self.r.u64_le(off) } else { self.r.u64_be(off) }
}
fn word(&self, off: usize) -> Option<u64> {
if self.is64 { self.u64(off) } else { self.u32(off).map(u64::from) }
}
fn word_size(&self) -> usize {
if self.is64 { 8 } else { 4 }
}
}
/// One program header.
#[derive(Debug, Clone, PartialEq, Eq)]
#[cfg_attr(feature = "serde", derive(serde::Serialize))]
pub struct ProgramHeader {
/// [`segment_type_name`] of `p_type`.
pub kind: String,
/// [`permissions`] of `p_flags`.
pub flags: String,
pub offset: u64,
#[cfg_attr(feature = "serde", serde(serialize_with = "hex::serialize"))]
pub vaddr: u64,
pub file_size: u64,
pub mem_size: u64,
pub align: u64,
/// The raw segment type.
#[cfg_attr(feature = "serde", serde(skip))]
pub p_type: u32,
/// The raw `PF_R`/`PF_W`/`PF_X` flags.
#[cfg_attr(feature = "serde", serde(skip))]
pub p_flags: u32,
}
/// A segment type's `PT_` name without the prefix (`LOAD`, `GNU_RELRO`…), or its number in hex.
pub fn segment_type_name(t: u32) -> String {
match t {
0 => "NULL".into(),
1 => "LOAD".into(),
2 => "DYNAMIC".into(),
3 => "INTERP".into(),
4 => "NOTE".into(),
5 => "SHLIB".into(),
6 => "PHDR".into(),
7 => "TLS".into(),
0x6474_e550 => "GNU_EH_FRAME".into(),
0x6474_e551 => "GNU_STACK".into(),
0x6474_e552 => "GNU_RELRO".into(),
0x6474_e553 => "GNU_PROPERTY".into(),
0x7000_0001 => "ARM_EXIDX".into(),
other => format!("{other:#x}"),
}
}
/// `p_flags` as `rwx`, with `-` for each permission missing.
pub fn permissions(f: u32) -> String {
format!("{}{}{}", if f & 4 != 0 { 'r' } else { '-' }, if f & 2 != 0 { 'w' } else { '-' }, if f & 1 != 0 { 'x' } else { '-' })
}
/// The program headers of an ELF file, at most [`MAX_PHDRS`]. A table that runs past the file
/// ends at the last complete entry.
pub fn program_headers(data: &[u8]) -> Result<Vec<ProgramHeader>, ElfError> {
let e = E::new(data)?;
let (phoff, phentsize, phnum) = if e.is64 { (e.u64(32), e.u16(54), e.u16(56)) } else { (e.u32(28).map(u64::from), e.u16(42), e.u16(44)) };
let phoff = usize::try_from(phoff.ok_or(ElfError::Truncated("ELF header"))?).map_err(|_| ElfError::Malformed("phoff".into()))?;
let phentsize = usize::from(phentsize.unwrap_or(0));
let phnum = usize::from(phnum.unwrap_or(0)).min(MAX_PHDRS);
let min = if e.is64 { 56 } else { 32 };
if phnum > 0 && phentsize < min {
return Err(ElfError::Malformed("program header entry too small".into()));
}
let mut out = Vec::new();
for i in 0..phnum {
// The fields below sit within `phentsize` bytes of `o`, so their offsets can't overflow either.
let Some(o) = i.checked_mul(phentsize).and_then(|x| x.checked_add(phoff)).filter(|&o| o.checked_add(phentsize).is_some()) else { break };
let ph = if e.is64 {
(e.u32(o), e.u32(o + 4), e.u64(o + 8), e.u64(o + 16), e.u64(o + 32), e.u64(o + 40), e.u64(o + 48))
} else {
(
e.u32(o),
e.u32(o + 24),
e.u32(o + 4).map(u64::from),
e.u32(o + 8).map(u64::from),
e.u32(o + 16).map(u64::from),
e.u32(o + 20).map(u64::from),
e.u32(o + 28).map(u64::from),
)
};
let (Some(t), Some(f), Some(off), Some(va), Some(fs), Some(ms), Some(al)) = ph else { break };
out.push(ProgramHeader {
kind: segment_type_name(t),
flags: permissions(f),
offset: off,
vaddr: va,
file_size: fs,
mem_size: ms,
align: al,
p_type: t,
p_flags: f,
});
}
Ok(out)
}
/// A dynamic tag's `DT_` name without the prefix (`NEEDED`, `GNU_HASH`…), or its number in hex.
pub fn dynamic_tag_name(tag: u64) -> String {
match tag {
0 => "NULL",
1 => "NEEDED",
2 => "PLTRELSZ",
3 => "PLTGOT",
4 => "HASH",
5 => "STRTAB",
6 => "SYMTAB",
7 => "RELA",
8 => "RELASZ",
9 => "RELAENT",
10 => "STRSZ",
11 => "SYMENT",
12 => "INIT",
13 => "FINI",
14 => "SONAME",
15 => "RPATH",
16 => "SYMBOLIC",
17 => "REL",
18 => "RELSZ",
19 => "RELENT",
20 => "PLTREL",
21 => "DEBUG",
22 => "TEXTREL",
23 => "JMPREL",
24 => "BIND_NOW",
25 => "INIT_ARRAY",
26 => "FINI_ARRAY",
27 => "INIT_ARRAYSZ",
28 => "FINI_ARRAYSZ",
29 => "RUNPATH",
30 => "FLAGS",
0x6fff_fef5 => "GNU_HASH",
0x6fff_fffb => "FLAGS_1",
0x6fff_fff0 => "VERSYM",
0x6fff_fffe => "VERNEED",
0x6fff_ffff => "VERNEEDNUM",
_ => return format!("{tag:#x}"),
}
.into()
}
/// Map a virtual address to a file offset using `PT_LOAD` headers. Addresses in a segment's
/// zero-filled tail, or whose offset would overflow, map to nothing.
pub fn va_to_off(phdrs: &[ProgramHeader], va: u64) -> Option<u64> {
phdrs.iter().filter(|p| p.p_type == 1).find(|p| va >= p.vaddr && va - p.vaddr < p.file_size).and_then(|p| p.offset.checked_add(va - p.vaddr))
}
/// The (tag, value) pairs of the `PT_DYNAMIC` segment in `phdrs`, up to its `DT_NULL`, at most
/// [`MAX_DYN`]. Empty without a dynamic segment.
pub fn dynamic_entries(data: &[u8], phdrs: &[ProgramHeader]) -> Result<Vec<(u64, u64)>, ElfError> {
Ok(entries(&E::new(data)?, phdrs))
}
fn entries(e: &E<'_>, phdrs: &[ProgramHeader]) -> Vec<(u64, u64)> {
let mut out = Vec::new();
let Some(d) = phdrs.iter().find(|p| p.p_type == 2) else { return out };
let ent = 2 * e.word_size();
let n = usize::try_from(d.file_size).unwrap_or(0) / ent;
let start = usize::try_from(d.offset).unwrap_or(usize::MAX);
for i in 0..n.min(MAX_DYN) {
let Some(o) = i.checked_mul(ent).and_then(|x| start.checked_add(x)) else { break };
let (Some(tag), Some(val)) = (e.word(o), o.checked_add(e.word_size()).and_then(|v| e.word(v))) else { break };
if tag == 0 {
break;
}
out.push((tag, val));
}
out
}
#[cfg(test)]
mod tests;