falcon 0.6.0

A Binary Analysis Framework in Rust
Documentation
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
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
use crate::architecture::*;
use crate::loader::*;
use crate::memory::backing::Memory;
use crate::Error;
use log::warn;
use std::collections::BTreeMap;
use std::fs::File;
use std::io::Read;
use std::path::{Path, PathBuf};

/// The address where the first library will be loaded
const DEFAULT_LIB_BASE: u64 = 0x4000_0000;
/// The step in address between where we will load libraries.
const LIB_BASE_STEP: u64 = 0x0200_0000;

// Some MIPS-specific DT entries. This will eventually land in Goblin.
const DT_MIPS_LOCAL_GOTNO: u64 = 0x7000_000a;
const DT_MIPS_GOTSYM: u64 = 0x7000_0013;
const DT_MIPS_SYMTABNO: u64 = 0x7000_0011;

/// A helper to build an ElfLinker using the builder pattern.
#[derive(Clone, Debug)]
pub struct ElfLinkerBuilder {
    filename: PathBuf,
    do_relocations: bool,
    just_interpreter: bool,
    ld_paths: Option<Vec<PathBuf>>,
}

impl ElfLinkerBuilder {
    /// Create a new ElfLinker
    pub fn new(filename: PathBuf) -> ElfLinkerBuilder {
        ElfLinkerBuilder {
            filename,
            do_relocations: true,
            just_interpreter: false,
            ld_paths: None,
        }
    }

    /// This ElfLinker should perform relocations (default true)
    pub fn do_relocations(mut self, do_relocations: bool) -> Self {
        self.do_relocations = do_relocations;
        self
    }

    /// This ElfLinker should only link in the program interpreter, specified
    /// by DT_INTERPRETER (default false)
    pub fn just_interpreter(mut self, just_interpreter: bool) -> Self {
        self.just_interpreter = just_interpreter;
        self
    }

    /// Set the paths where the ElfLinker should look for shared objects and
    /// depenedncies
    pub fn ld_paths<P: Into<PathBuf>>(mut self, ld_paths: Option<Vec<P>>) -> Self {
        self.ld_paths = ld_paths.map(|v| v.into_iter().map(|p| p.into()).collect::<Vec<PathBuf>>());
        self
    }

    /// Get the ElfLinker for this ElfLinkerBuilder
    pub fn link(self) -> Result<ElfLinker, Error> {
        ElfLinker::new(
            self.filename,
            self.do_relocations,
            self.just_interpreter,
            self.ld_paths,
        )
    }
}

/// Loader which links together multiple Elf files.
///
/// Can do some rudimentary linking of binaries.
#[derive(Debug)]
pub struct ElfLinker {
    /// The filename (path included) of the file we're loading.
    filename: PathBuf,
    /// A mapping from lib name (for example `libc.so.6`) to Elf.
    loaded: BTreeMap<String, Elf>,
    /// The current memory mapping.
    memory: Memory,
    /// A mapping of function symbol names to addresses
    symbols: BTreeMap<String, u64>,
    /// The address we will place the next library at.
    next_lib_address: u64,
    /// Functions as specified by the user
    user_functions: Vec<u64>,
    /// If set, we will do relocations as we link
    do_relocations: bool,
    /// If set, we will only bring in the DT_INTERPRETER entry, as would happen
    /// if a process was loaded normally.
    just_interpreter: bool,
    /// The paths where ElfLinker will look for dependencies
    ld_paths: Option<Vec<PathBuf>>,
}

impl ElfLinker {
    /// Create a new ElfLinker.
    ///
    /// It is recommended you use ElfLinkerBuilder to build an ElfLinker.
    pub fn new(
        filename: PathBuf,
        do_relocations: bool,
        just_interpreter: bool,
        ld_paths: Option<Vec<PathBuf>>,
    ) -> Result<ElfLinker, Error> {
        let mut file = File::open(&filename)?;
        let mut buf = Vec::new();
        file.read_to_end(&mut buf)?;

        // get the endianness of this elf for the memory model
        let mut endian = Endian::Big;
        if let goblin::Object::Elf(elf_peek) = goblin::Object::parse(&buf)? {
            if elf_peek.header.endianness()?.is_little() {
                endian = Endian::Little;
            }
        } else {
            return Err(Error::InvalidFileFormat(format!(
                "{} was not an elf",
                filename.to_str().unwrap()
            )));
        }

        let mut elf_linker = ElfLinker {
            filename: filename.clone(),
            loaded: BTreeMap::new(),
            memory: Memory::new(endian),
            symbols: BTreeMap::new(),
            next_lib_address: DEFAULT_LIB_BASE,
            user_functions: Vec::new(),
            do_relocations,
            just_interpreter,
            ld_paths,
        };

        elf_linker.load_elf(&filename, 0)?;

        Ok(elf_linker)
    }

    /// Get the ELFs loaded and linked in this loader
    pub fn loaded(&self) -> &BTreeMap<String, Elf> {
        &self.loaded
    }

    /// Get the filename of the ELF we're loading
    pub fn filename(&self) -> &Path {
        &self.filename
    }

    /// Takes the path to an Elf, and a base address the Elf should be loaded
    /// at. Loads the Elf, all it's dependencies (DT_NEEDED), and then handles
    /// the supported relocations.
    pub fn load_elf(&mut self, filename: &Path, base_address: u64) -> Result<(), Error> {
        let path = self
            .ld_paths
            .as_ref()
            .map(|ld_paths| {
                ld_paths
                    .iter()
                    .map(|ld_path| {
                        let filename = if filename.starts_with("/") {
                            let filename = filename.to_str().unwrap();
                            Path::new(filename.split_at(1).1)
                        } else {
                            filename
                        };
                        ld_path.to_path_buf().join(filename)
                    })
                    .find(|path| path.exists())
                    .unwrap_or_else(|| filename.to_path_buf())
            })
            .unwrap_or_else(|| filename.to_path_buf());

        let elf = Elf::from_file_with_base_address(&path, base_address)?;

        // Update our memory map based on what's in the Elf
        for (address, section) in elf.memory()?.sections() {
            self.memory
                .set_memory(*address, section.data().to_owned(), section.permissions());
        }

        // Add this Elf to the loaded Elfs
        let filename = filename.file_name().unwrap().to_str().unwrap().to_string();
        self.loaded.insert(filename.clone(), elf);

        {
            let elf = &self.loaded[&filename];

            // Add its exported symbols to our symbols
            for symbol in elf.exported_symbols() {
                if self.symbols.contains_key(symbol.name()) {
                    continue;
                }
                self.symbols.insert(
                    symbol.name().to_string(),
                    elf.base_address() + symbol.address(),
                );
            }
        }

        if self.just_interpreter {
            let interpreter_filename = self.loaded[&filename]
                .elf()
                .interpreter
                .map(|s| s.to_string());
            if let Some(interpreter_filename) = interpreter_filename {
                self.load_elf(Path::new(&interpreter_filename), DEFAULT_LIB_BASE)?;
            }
        } else {
            // Ensure all shared objects we rely on are loaded
            for so_name in self.loaded[&filename].dt_needed()? {
                if !self.loaded.contains_key(&so_name) {
                    self.next_lib_address += LIB_BASE_STEP;
                    let next_lib_address = self.next_lib_address;
                    self.load_elf(Path::new(&so_name), next_lib_address)?;
                }
            }
        }

        if self.do_relocations {
            match self.loaded[&filename].elf().header.e_machine {
                goblin::elf::header::EM_386 => self.relocations_x86(&filename)?,
                goblin::elf::header::EM_MIPS => self.relocations_mips(&filename)?,
                _ => return Err(Error::ElfLinkerRelocationsUnsupported),
            }
        }

        Ok(())
    }

    /// Get the `Elf` for the primary elf loaded.
    pub fn get_elf(&self) -> Result<&Elf, Error> {
        let loaded = self.loaded();
        let filename = self
            .filename()
            .file_name()
            .and_then(|filename| filename.to_str())
            .ok_or("Could not get filename for ElfLinker's primary program")?;

        let elf = loaded
            .get(filename)
            .ok_or(format!("Could not get {} from ElfLinker", filename))?;

        Ok(elf)
    }

    /// If the primary `Elf` we're loading has an interpreter designated in its
    /// dynamic sectino, get the `Elf` for the interpreter.
    pub fn get_interpreter(&self) -> Result<Option<&Elf>, Error> {
        let elf = self.get_elf()?;

        let interpreter_elf = match elf.elf().interpreter {
            Some(interpreter_filename) => {
                let interpreter_filename = Path::new(interpreter_filename)
                    .file_name()
                    .and_then(|filename| filename.to_str())
                    .ok_or_else(|| {
                        Error::Custom(
                            "Failed to get filename portion of interpreter filename".to_string(),
                        )
                    })?;
                Some(self.loaded().get(interpreter_filename).ok_or(format!(
                    "Could not find interpreter {}",
                    interpreter_filename
                ))?)
            }
            None => None,
        };

        Ok(interpreter_elf)
    }

    /// Perform x86-specific relocations
    fn relocations_x86(&mut self, filename: &str) -> Result<(), Error> {
        // Process relocations
        let elf = &self.loaded[filename];
        let dynsyms = elf.elf().dynsyms;
        let dynstrtab = elf.elf().dynstrtab;
        for reloc in elf
            .elf()
            .dynrelas
            .iter()
            .chain(elf.elf().dynrels.iter().chain(elf.elf().pltrelocs.iter()))
        {
            match reloc.r_type {
                goblin::elf::reloc::R_386_32 => {
                    let sym = &dynsyms
                        .get(reloc.r_sym)
                        .expect("Unable to resolve relocation symbol");
                    let sym_name = &dynstrtab[sym.st_name];
                    let value = match self.symbols.get(sym_name) {
                        Some(v) => v.to_owned() as u32,
                        None => {
                            return Err(Error::Custom(format!(
                                "Could not resolve symbol {}",
                                sym_name
                            )))
                        }
                    };
                    self.memory
                        .set32(reloc.r_offset + elf.base_address(), value)?;
                }
                goblin::elf::reloc::R_386_GOT32 => {
                    return Err(Error::Custom("R_386_GOT32".to_string()))
                }
                goblin::elf::reloc::R_386_PLT32 => {
                    let sym = &dynsyms
                        .get(reloc.r_sym)
                        .expect("Unable to resolve relocation symbol");
                    let sym_name = &dynstrtab[sym.st_name];
                    return Err(Error::Custom(format!(
                        "R_386_PLT32 {:?}:0x{:x}:{}",
                        self.filename, reloc.r_offset, sym_name
                    )));
                }
                goblin::elf::reloc::R_386_COPY => {
                    return Err(Error::Custom("R_386_COPY".to_string()))
                }
                goblin::elf::reloc::R_386_GLOB_DAT => {
                    let sym = &dynsyms
                        .get(reloc.r_sym)
                        .expect("Unable to resolve relocation symbol");
                    let sym_name = &dynstrtab[sym.st_name];
                    let value = match self.symbols.get(sym_name) {
                        Some(v) => v.to_owned() as u32,
                        None => {
                            warn!("Could not resolve symbol {}", sym_name);
                            continue;
                        }
                    };
                    self.memory
                        .set32(reloc.r_offset + elf.base_address(), value)?;
                }
                goblin::elf::reloc::R_386_JMP_SLOT => {
                    let sym = &dynsyms
                        .get(reloc.r_sym)
                        .expect("Unable to resolve relocation symbol");
                    let sym_name = &dynstrtab[sym.st_name];
                    let value = match self.symbols.get(sym_name) {
                        Some(v) => v.to_owned() as u32,
                        None => {
                            return Err(Error::Custom(format!(
                                "Could not resolve symbol {}",
                                sym_name
                            )))
                        }
                    };
                    self.memory
                        .set32(reloc.r_offset + elf.base_address(), value)?;
                }
                goblin::elf::reloc::R_386_RELATIVE => {
                    let value = self.memory.get32(reloc.r_offset + elf.base_address());
                    let value = match value {
                        Some(value) => elf.base_address() as u32 + value,
                        None => {
                            return Err(Error::Custom(format!(
                                "Invalid address for R_386_RELATIVE {:?}:{:x}",
                                self.filename, reloc.r_offset,
                            )))
                        }
                    };
                    self.memory
                        .set32(reloc.r_offset + elf.base_address(), value)?;
                }
                goblin::elf::reloc::R_386_GOTPC => {
                    return Err(Error::Custom("R_386_GOT_PC".to_string()))
                }
                goblin::elf::reloc::R_386_TLS_TPOFF => {
                    return Err(Error::Custom(
                        "Ignoring R_386_TLS_TPOFF Relocation".to_string(),
                    ))
                }
                goblin::elf::reloc::R_386_IRELATIVE => {
                    return Err(Error::Custom(format!(
                        "R_386_IRELATIVE {:?}:0x{:x} going unprocessed",
                        self.filename, reloc.r_offset
                    )))
                }
                _ => {
                    return Err(Error::Custom(format!(
                        "unhandled relocation type {}",
                        reloc.r_type
                    )))
                }
            }
        }
        Ok(())
    }

    /// Perform MIPS-specific relocations
    fn relocations_mips(&mut self, filename: &str) -> Result<(), Error> {
        let elf = &self.loaded[filename];

        fn get_dynamic(elf: &Elf, tag: u64) -> Option<u64> {
            elf.elf().dynamic.and_then(|dynamic| {
                dynamic
                    .dyns
                    .iter()
                    .find(|dyn_| dyn_.d_tag == tag)
                    .map(|dyn_| dyn_.d_val)
            })
        }

        // The number of local GOT entries. Also an index into the GOT
        // for the first external GOT entry.
        let local_gotno =
            get_dynamic(elf, DT_MIPS_LOCAL_GOTNO).ok_or("Could not get DT_MIPS_LOCAL_GOTNO")?;

        // Index of the first dynamic symbol table entry that corresponds
        // to an entry in the GOT.
        let gotsym = get_dynamic(elf, DT_MIPS_GOTSYM).ok_or("Could not get DT_MIPS_GOTSYM")?;

        // The number of entries in the dynamic symbol table
        let symtabno =
            get_dynamic(elf, DT_MIPS_SYMTABNO).ok_or("Could not get DT_MIPS_SYMTABNO")?;

        // The address of the GOT section
        let pltgot =
            get_dynamic(elf, goblin::elf::dynamic::DT_PLTGOT).ok_or("Could not get DT_PLTGOT")?;

        // Start by adding the base address to all entries in the GOT
        for i in 0..(local_gotno + (symtabno - gotsym)) {
            let address = elf.base_address() + (i * 4) + pltgot;
            let value = self.memory.get32(address).ok_or(format!(
                "Could not get memory at address 0x{:x} for adding base address",
                address
            ))?;
            self.memory
                .set32(address, value.wrapping_add(elf.base_address() as u32))?;
        }

        let dynstrtab = elf.elf().dynstrtab;
        let dynsyms = elf.elf().dynsyms;
        let mut address = pltgot + elf.base_address() + (local_gotno * 4);
        for i in gotsym..(symtabno) {
            let sym = dynsyms
                .get(i as usize)
                .ok_or(format!("Could not get symbol {}", i))?;
            let symbol_name = dynstrtab
                .get_at(sym.st_name)
                .ok_or(format!("Could not get symbol name for {}", i))?;
            // Internal entries have already been relocated, so we only need to
            // relocate external entries
            if sym.st_shndx == 0 {
                if let Some(value) = self.symbols.get(symbol_name) {
                    self.memory.set32(address, *value as u32)?;
                } else {
                    return Err(Error::ElfLinkerMissingSymbol(symbol_name.to_string()));
                }
            }
            address += 4;
        }

        // handle all relocation entries
        for dynrel in elf.elf().dynrels.iter() {
            if dynrel.r_type == goblin::elf::reloc::R_MIPS_REL32 {
                let value = self
                    .memory
                    .get32(dynrel.r_offset + elf.base_address())
                    .ok_or(format!(
                        "Could not load R_MIPS_REL32 at 0x{:x}",
                        dynrel.r_offset + elf.base_address()
                    ))?;
                self.memory.set32(
                    dynrel.r_offset + elf.base_address(),
                    value + (elf.base_address() as u32),
                )?;
            }
        }

        Ok(())
    }

    /// Inform the linker of a function at the given address.
    ///
    /// This function will be added to calls to `function_entries` and will be automatically
    /// lifted when calling `to_program`.
    pub fn add_user_function(&mut self, address: u64) {
        self.user_functions.push(address);
    }
}

impl Loader for ElfLinker {
    fn memory(&self) -> Result<Memory, Error> {
        Ok(self.memory.clone())
    }

    fn function_entries(&self) -> Result<Vec<FunctionEntry>, Error> {
        let mut function_entries = Vec::new();
        for loaded in &self.loaded {
            // let fe = loaded.1.function_entries()?;
            // for e in &fe {
            //     println!("{} 0x{:x}", loaded.0, e.address());
            // }
            function_entries.append(&mut loaded.1.function_entries()?);
        }
        for address in &self.user_functions {
            function_entries.push(FunctionEntry::new(*address, None));
        }
        Ok(function_entries)
    }

    // TODO Just maybe a bit too much unwrapping here.
    fn program_entry(&self) -> u64 {
        let filename = self
            .filename
            .as_path()
            .file_name()
            .unwrap()
            .to_str()
            .unwrap();
        self.loaded[filename].program_entry()
    }

    fn architecture(&self) -> &dyn Architecture {
        let filename = self
            .filename
            .as_path()
            .file_name()
            .unwrap()
            .to_str()
            .unwrap();
        self.loaded[filename].architecture()
    }

    fn as_any(&self) -> &dyn Any {
        self
    }

    fn symbols(&self) -> Vec<Symbol> {
        self.loaded
            .iter()
            .flat_map(|(_, elf)| elf.symbols())
            .collect()
    }
}