elf/
file.rs

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
//! Parsing the ELF File Header
use crate::abi;
use crate::endian::EndianParse;
use crate::parse::ParseError;

/// Represents the ELF file word size (32-bit vs 64-bit)
#[derive(Debug, Copy, Clone, PartialEq, Eq)]
pub enum Class {
    ELF32,
    ELF64,
}

/// C-style 32-bit ELF File Header definition
///
/// These C-style definitions are for users who want to implement their own ELF manipulation logic.
#[derive(Debug)]
#[repr(C)]
pub struct Elf32_Ehdr {
    pub e_ident: [u8; abi::EI_NIDENT],
    pub e_type: u16,
    pub e_machine: u16,
    pub e_version: u32,
    pub e_entry: u32,
    pub e_phoff: u32,
    pub e_shoff: u32,
    pub e_flags: u32,
    pub e_ehsize: u16,
    pub e_phentsize: u16,
    pub e_phnum: u16,
    pub e_shentsize: u16,
    pub e_shnum: u16,
    pub e_shstrndx: u16,
}

/// C-style 64-bit ELF File Header definition
///
/// These C-style definitions are for users who want to implement their own ELF manipulation logic.
#[derive(Debug)]
#[repr(C)]
pub struct Elf64_Ehdr {
    pub e_ident: [u8; abi::EI_NIDENT],
    pub e_type: u16,
    pub e_machine: u16,
    pub e_version: u32,
    pub e_entry: u64,
    pub e_phoff: u64,
    pub e_shoff: u64,
    pub e_flags: u32,
    pub e_ehsize: u16,
    pub e_phentsize: u16,
    pub e_phnum: u16,
    pub e_shentsize: u16,
    pub e_shnum: u16,
    pub e_shstrndx: u16,
}

/// Encapsulates the contents of the ELF File Header
///
/// The ELF File Header starts off every ELF file and both identifies the
/// file contents and informs how to interpret said contents. This includes
/// the width of certain fields (32-bit vs 64-bit), the data endianness, the
/// file type, and more.
#[derive(Copy, Clone, Debug, PartialEq, Eq)]
pub struct FileHeader<E: EndianParse> {
    /// 32-bit vs 64-bit
    pub class: Class,
    // file byte order
    pub endianness: E,
    /// elf version
    pub version: u32,
    /// OS ABI
    pub osabi: u8,
    /// Version of the OS ABI
    pub abiversion: u8,
    /// ELF file type
    pub e_type: u16,
    /// Target machine architecture
    pub e_machine: u16,
    /// Virtual address of program entry point
    /// This member gives the virtual address to which the system first transfers control,
    /// thus starting the process. If the file has no associated entry point, this member holds zero.
    ///
    /// Note: Type is Elf32_Addr or Elf64_Addr which are either 4 or 8 bytes. We aren't trying to zero-copy
    /// parse the FileHeader since there's only one per file and its only ~45 bytes anyway, so we use
    /// u64 for the three Elf*_Addr and Elf*_Off fields here.
    pub e_entry: u64,
    /// This member holds the program header table's file offset in bytes. If the file has no program header
    /// table, this member holds zero.
    pub e_phoff: u64,
    /// This member holds the section header table's file offset in bytes. If the file has no section header
    /// table, this member holds zero.
    pub e_shoff: u64,
    /// This member holds processor-specific flags associated with the file. Flag names take the form EF_machine_flag.
    pub e_flags: u32,
    /// This member holds the ELF header's size in bytes.
    pub e_ehsize: u16,
    /// This member holds the size in bytes of one entry in the file's program header table; all entries are the same size.
    pub e_phentsize: u16,
    /// This member holds the number of entries in the program header table. Thus the product of e_phentsize and e_phnum
    /// gives the table's size in bytes. If a file has no program header table, e_phnum holds the value zero.
    pub e_phnum: u16,
    /// This member holds a section header's size in bytes. A section header is one entry in the section header table;
    /// all entries are the same size.
    pub e_shentsize: u16,
    /// This member holds the number of entries in the section header table. Thus the product of e_shentsize and e_shnum
    /// gives the section header table's size in bytes. If a file has no section header table, e_shnum holds the value zero.
    ///
    /// If the number of sections is greater than or equal to SHN_LORESERVE (0xff00), this member has the value zero and
    /// the actual number of section header table entries is contained in the sh_size field of the section header at index 0.
    /// (Otherwise, the sh_size member of the initial entry contains 0.)
    pub e_shnum: u16,
    /// This member holds the section header table index of the entry associated with the section name string table. If the
    /// file has no section name string table, this member holds the value SHN_UNDEF.
    ///
    /// If the section name string table section index is greater than or equal to SHN_LORESERVE (0xff00), this member has
    /// the value SHN_XINDEX (0xffff) and the actual index of the section name string table section is contained in the
    /// sh_link field of the section header at index 0. (Otherwise, the sh_link member of the initial entry contains 0.)
    pub e_shstrndx: u16,
}

pub const ELF32_EHDR_TAILSIZE: usize = 36;
pub const ELF64_EHDR_TAILSIZE: usize = 48;

fn verify_ident(buf: &[u8]) -> Result<(), ParseError> {
    // Verify the magic number
    let magic = buf.split_at(abi::EI_CLASS).0;
    if magic != abi::ELFMAGIC {
        return Err(ParseError::BadMagic([
            magic[0], magic[1], magic[2], magic[3],
        ]));
    }

    // Verify ELF Version
    let version = buf[abi::EI_VERSION];
    if version != abi::EV_CURRENT {
        return Err(ParseError::UnsupportedVersion((
            version as u64,
            abi::EV_CURRENT as u64,
        )));
    }

    Ok(())
}

pub fn parse_ident<E: EndianParse>(data: &[u8]) -> Result<(E, Class, u8, u8), ParseError> {
    verify_ident(data)?;

    let e_class = data[abi::EI_CLASS];
    let class = match e_class {
        abi::ELFCLASS32 => Class::ELF32,
        abi::ELFCLASS64 => Class::ELF64,
        _ => {
            return Err(ParseError::UnsupportedElfClass(e_class));
        }
    };

    // Verify endianness is something we know how to parse
    let file_endian = E::from_ei_data(data[abi::EI_DATA])?;

    Ok((
        file_endian,
        class,
        data[abi::EI_OSABI],
        data[abi::EI_ABIVERSION],
    ))
}

impl<E: EndianParse> FileHeader<E> {
    pub fn parse_tail(ident: (E, Class, u8, u8), data: &[u8]) -> Result<FileHeader<E>, ParseError> {
        let (file_endian, class, osabi, abiversion) = ident;

        let mut offset = 0;
        let e_type = file_endian.parse_u16_at(&mut offset, data)?;
        let e_machine = file_endian.parse_u16_at(&mut offset, data)?;
        let version = file_endian.parse_u32_at(&mut offset, data)?;

        let e_entry: u64;
        let e_phoff: u64;
        let e_shoff: u64;

        if class == Class::ELF32 {
            e_entry = file_endian.parse_u32_at(&mut offset, data)? as u64;
            e_phoff = file_endian.parse_u32_at(&mut offset, data)? as u64;
            e_shoff = file_endian.parse_u32_at(&mut offset, data)? as u64;
        } else {
            e_entry = file_endian.parse_u64_at(&mut offset, data)?;
            e_phoff = file_endian.parse_u64_at(&mut offset, data)?;
            e_shoff = file_endian.parse_u64_at(&mut offset, data)?;
        }

        let e_flags = file_endian.parse_u32_at(&mut offset, data)?;
        let e_ehsize = file_endian.parse_u16_at(&mut offset, data)?;
        let e_phentsize = file_endian.parse_u16_at(&mut offset, data)?;
        let e_phnum = file_endian.parse_u16_at(&mut offset, data)?;
        let e_shentsize = file_endian.parse_u16_at(&mut offset, data)?;
        let e_shnum = file_endian.parse_u16_at(&mut offset, data)?;
        let e_shstrndx = file_endian.parse_u16_at(&mut offset, data)?;

        Ok(FileHeader {
            class,
            endianness: file_endian,
            version,
            e_type,
            e_machine,
            osabi,
            abiversion,
            e_entry,
            e_phoff,
            e_shoff,
            e_flags,
            e_ehsize,
            e_phentsize,
            e_phnum,
            e_shentsize,
            e_shnum,
            e_shstrndx,
        })
    }
}

#[cfg(test)]
mod parse_tests {
    use super::*;
    use crate::endian::AnyEndian;

    #[test]
    fn test_verify_ident_valid() {
        let data: [u8; abi::EI_NIDENT] = [
            abi::ELFMAG0,
            abi::ELFMAG1,
            abi::ELFMAG2,
            abi::ELFMAG3,
            abi::ELFCLASS32,
            abi::ELFDATA2LSB,
            abi::EV_CURRENT,
            abi::ELFOSABI_LINUX,
            0,
            0,
            0,
            0,
            0,
            0,
            0,
            0,
        ];
        verify_ident(data.as_ref()).expect("Expected Ok result");
    }

    #[test]
    fn test_verify_ident_invalid_mag0() {
        let data: [u8; abi::EI_NIDENT] = [
            0xFF,
            abi::ELFMAG1,
            abi::ELFMAG2,
            abi::ELFMAG3,
            abi::ELFCLASS32,
            abi::ELFDATA2LSB,
            abi::EV_CURRENT,
            abi::ELFOSABI_LINUX,
            0,
            0,
            0,
            0,
            0,
            0,
            0,
            0,
        ];
        let result = verify_ident(data.as_ref()).expect_err("Expected an error");
        assert!(
            matches!(result, ParseError::BadMagic(_)),
            "Unexpected Error type found: {result}"
        );
    }

    #[test]
    fn test_verify_ident_invalid_mag1() {
        let data: [u8; abi::EI_NIDENT] = [
            abi::ELFMAG0,
            0xFF,
            abi::ELFMAG2,
            abi::ELFMAG3,
            abi::ELFCLASS32,
            abi::ELFDATA2LSB,
            abi::EV_CURRENT,
            abi::ELFOSABI_LINUX,
            0,
            0,
            0,
            0,
            0,
            0,
            0,
            0,
        ];
        let result = verify_ident(data.as_ref()).expect_err("Expected an error");
        assert!(
            matches!(result, ParseError::BadMagic(_)),
            "Unexpected Error type found: {result}"
        );
    }

    #[test]
    fn test_verify_ident_invalid_mag2() {
        let data: [u8; abi::EI_NIDENT] = [
            abi::ELFMAG0,
            abi::ELFMAG1,
            0xFF,
            abi::ELFMAG3,
            abi::ELFCLASS32,
            abi::ELFDATA2LSB,
            abi::EV_CURRENT,
            abi::ELFOSABI_LINUX,
            0,
            0,
            0,
            0,
            0,
            0,
            0,
            0,
        ];
        let result = verify_ident(data.as_ref()).expect_err("Expected an error");
        assert!(
            matches!(result, ParseError::BadMagic(_)),
            "Unexpected Error type found: {result}"
        );
    }

    #[test]
    fn test_verify_ident_invalid_mag3() {
        let data: [u8; abi::EI_NIDENT] = [
            abi::ELFMAG0,
            abi::ELFMAG1,
            abi::ELFMAG2,
            0xFF,
            abi::ELFCLASS32,
            abi::ELFDATA2LSB,
            abi::EV_CURRENT,
            abi::ELFOSABI_LINUX,
            0,
            0,
            0,
            0,
            0,
            0,
            0,
            0,
        ];
        let result = verify_ident(data.as_ref()).expect_err("Expected an error");
        assert!(
            matches!(result, ParseError::BadMagic(_)),
            "Unexpected Error type found: {result}"
        );
    }

    #[allow(deprecated)]
    #[test]
    fn test_verify_ident_invalid_version() {
        let data: [u8; abi::EI_NIDENT] = [
            abi::ELFMAG0,
            abi::ELFMAG1,
            abi::ELFMAG2,
            abi::ELFMAG3,
            abi::ELFCLASS32,
            abi::ELFDATA2LSB,
            42,
            abi::ELFOSABI_LINUX,
            0,
            0,
            0,
            0,
            0,
            0,
            0,
            0,
        ];
        let result = verify_ident(data.as_ref()).expect_err("Expected an error");
        assert!(
            matches!(result, ParseError::UnsupportedVersion((42, 1))),
            "Unexpected Error type found: {result}"
        );
    }

    #[test]
    fn test_parse_ehdr32_works() {
        let ident = (AnyEndian::Little, Class::ELF32, abi::ELFOSABI_LINUX, 7u8);
        let mut tail = [0u8; ELF64_EHDR_TAILSIZE];
        for (n, elem) in tail.iter_mut().enumerate().take(ELF64_EHDR_TAILSIZE) {
            *elem = n as u8;
        }

        assert_eq!(
            FileHeader::parse_tail(ident, &tail).unwrap(),
            FileHeader {
                class: Class::ELF32,
                endianness: AnyEndian::Little,
                version: 0x7060504,
                osabi: abi::ELFOSABI_LINUX,
                abiversion: 7,
                e_type: 0x100,
                e_machine: 0x302,
                e_entry: 0x0B0A0908,
                e_phoff: 0x0F0E0D0C,
                e_shoff: 0x13121110,
                e_flags: 0x17161514,
                e_ehsize: 0x1918,
                e_phentsize: 0x1B1A,
                e_phnum: 0x1D1C,
                e_shentsize: 0x1F1E,
                e_shnum: 0x2120,
                e_shstrndx: 0x2322,
            }
        );
    }

    #[test]
    fn test_parse_ehdr32_fuzz_too_short() {
        let ident = (AnyEndian::Little, Class::ELF32, abi::ELFOSABI_LINUX, 7u8);
        let tail = [0u8; ELF32_EHDR_TAILSIZE];

        for n in 0..ELF32_EHDR_TAILSIZE {
            let buf = tail.split_at(n).0;
            let result = FileHeader::parse_tail(ident, buf).expect_err("Expected an error");
            assert!(
                matches!(result, ParseError::SliceReadError(_)),
                "Unexpected Error type found: {result:?}"
            );
        }
    }

    #[test]
    fn test_parse_ehdr64_works() {
        let ident = (AnyEndian::Big, Class::ELF64, abi::ELFOSABI_LINUX, 7u8);
        let mut tail = [0u8; ELF64_EHDR_TAILSIZE];
        for (n, elem) in tail.iter_mut().enumerate().take(ELF64_EHDR_TAILSIZE) {
            *elem = n as u8;
        }

        assert_eq!(
            FileHeader::parse_tail(ident, &tail).unwrap(),
            FileHeader {
                class: Class::ELF64,
                endianness: AnyEndian::Big,
                version: 0x04050607,
                osabi: abi::ELFOSABI_LINUX,
                abiversion: 7,
                e_type: 0x0001,
                e_machine: 0x0203,
                e_entry: 0x08090A0B0C0D0E0F,
                e_phoff: 0x1011121314151617,
                e_shoff: 0x18191A1B1C1D1E1F,
                e_flags: 0x20212223,
                e_ehsize: 0x2425,
                e_phentsize: 0x2627,
                e_phnum: 0x2829,
                e_shentsize: 0x2A2B,
                e_shnum: 0x2C2D,
                e_shstrndx: 0x2E2F,
            }
        );
    }

    #[test]
    fn test_parse_ehdr64_fuzz_too_short() {
        let ident = (AnyEndian::Little, Class::ELF64, abi::ELFOSABI_LINUX, 7u8);
        let tail = [0u8; ELF64_EHDR_TAILSIZE];

        for n in 0..ELF64_EHDR_TAILSIZE {
            let buf = tail.split_at(n).0;
            let result = FileHeader::parse_tail(ident, buf).expect_err("Expected an error");
            assert!(
                matches!(result, ParseError::SliceReadError(_)),
                "Unexpected Error type found: {result:?}"
            );
        }
    }
}