hermit-run 0.5.0

Hermit: a deterministic Linux execution engine for reproducible runs, record and replay, and concurrency-race search.
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
539
540
541
542
543
544
545
546
547
/*
 * Copyright (c) Meta Platforms, Inc. and affiliates.
 * All rights reserved.
 *
 * This source code is licensed under the BSD-style license found in the
 * LICENSE file in the root directory of this source tree.
 */

//! Minimal Detcore preload discovery, separate from full artifact qualification.

use std::io;
use std::ops::Range;

use goblin::elf::Elf;
use goblin::elf::dynamic;
use goblin::elf::header;
use goblin::elf::program_header;
use goblin::elf::reloc;
use goblin::elf::section_header;
use goblin::elf::sym;

pub(crate) const ROOT: &str = "detcore_liteinst_initialize";

fn invalid(message: &str) -> io::Error {
    io::Error::new(io::ErrorKind::InvalidData, message)
}

fn file_span(bytes: &[u8], offset: u64, size: u64) -> io::Result<Range<usize>> {
    let start = usize::try_from(offset).map_err(|_| invalid("file extent offset overflow"))?;
    let size = usize::try_from(size).map_err(|_| invalid("file extent size overflow"))?;
    let end = start
        .checked_add(size)
        .ok_or_else(|| invalid("file extent end overflow"))?;
    bytes
        .get(start..end)
        .ok_or_else(|| invalid("file extent is truncated"))?;
    Ok(start..end)
}

fn loaded_file_span(
    bytes: &[u8],
    elf: &Elf<'_>,
    address: u64,
    size: u64,
) -> io::Result<Range<usize>> {
    let end = address
        .checked_add(size)
        .ok_or_else(|| invalid("loaded extent end overflow"))?;
    let mut found = None;
    for segment in &elf.program_headers {
        if segment.p_type != program_header::PT_LOAD {
            continue;
        }
        let top = segment
            .p_vaddr
            .checked_add(segment.p_filesz)
            .ok_or_else(|| invalid("loaded file extent overflow"))?;
        if address < segment.p_vaddr || end > top {
            continue;
        }
        if size == 0 || segment.p_filesz > segment.p_memsz || found.is_some() {
            return Err(invalid("loaded file extent is empty or ambiguous"));
        }
        let offset = segment
            .p_offset
            .checked_add(address - segment.p_vaddr)
            .ok_or_else(|| invalid("loaded file offset overflow"))?;
        found = Some(file_span(bytes, offset, size)?);
    }
    found.ok_or_else(|| invalid("loaded extent has no complete file backing"))
}

fn tag(elf: &Elf<'_>, wanted: u64) -> io::Result<Option<u64>> {
    let dynamic = elf
        .dynamic
        .as_ref()
        .ok_or_else(|| invalid("Detcore has no dynamic table"))?;
    let mut entries = dynamic.dyns.iter().filter(|entry| entry.d_tag == wanted);
    let value = entries.next().map(|entry| entry.d_val);
    if entries.next().is_some() {
        return Err(invalid("duplicate constructor dynamic tag"));
    }
    Ok(value)
}

// x86-64 ABI write widths. COPY and unknown relocation operations cannot prove
// a sole constructor writer and are unsupported in this discovery branch.
fn relocation_width(kind: u32) -> io::Result<u64> {
    match kind {
        0 | 35 => Ok(0), // NONE, TLSDESC_CALL (no data write)
        1 | 6..=8 | 16..=18 | 24..=25 | 27..=31 | 33 | 37..=38 => Ok(8),
        2..=4 | 9..=11 | 19..=23 | 26 | 32 | 34 | 41..=42 => Ok(4),
        12..=13 => Ok(2),
        14..=15 => Ok(1),
        36 => Ok(16), // TLSDESC
        _ => Err(invalid("unsupported constructor relocation writer")),
    }
}

/// This discovers the strict public binding/registration shape. It does not
/// prove the full constructor bytes, native stack transition, loader layouts,
/// allocator or preparation closure; those require the shared qualifier.
pub(crate) fn validate(bytes: &[u8], elf: &Elf<'_>) -> io::Result<()> {
    if !elf.is_64
        || !elf.little_endian
        || elf.header.e_type != header::ET_DYN
        || elf.header.e_machine != header::EM_X86_64
    {
        return Err(invalid("Detcore is not a little-endian x86-64 DSO"));
    }
    let mut roots = elf
        .dynsyms
        .iter()
        .filter(|symbol| elf.dynstrtab.get_at(symbol.st_name) == Some(ROOT));
    let root = roots
        .next()
        .ok_or_else(|| invalid("Detcore constructor root is absent"))?;
    if roots.next().is_some()
        || root.st_bind() != sym::STB_GLOBAL
        || root.st_visibility() != sym::STV_PROTECTED
        || root.st_type() != sym::STT_FUNC
        || root.st_shndx == section_header::SHN_UNDEF as usize
        || root.st_size != 16
        || root.st_value == 0
    {
        return Err(invalid(
            "Detcore constructor root is not unique strong PROTECTED FUNC16",
        ));
    }
    let root_end = root
        .st_value
        .checked_add(root.st_size)
        .ok_or_else(|| invalid("Detcore constructor root extent overflow"))?;
    let section = elf
        .section_headers
        .get(root.st_shndx)
        .ok_or_else(|| invalid("Detcore constructor root section is absent"))?;
    if section.sh_type != section_header::SHT_PROGBITS
        || section.sh_flags & u64::from(section_header::SHF_EXECINSTR) == 0
        || root.st_value < section.sh_addr
        || section
            .sh_addr
            .checked_add(section.sh_size)
            .is_none_or(|end| root_end > end)
    {
        return Err(invalid(
            "Detcore constructor root has no executable complete section",
        ));
    }
    let root_offset = section
        .sh_offset
        .checked_add(root.st_value - section.sh_addr)
        .ok_or_else(|| invalid("Detcore constructor root file offset overflow"))?;
    if loaded_file_span(bytes, elf, root.st_value, root.st_size)?
        != file_span(bytes, root_offset, root.st_size)?
        || !elf.program_headers.iter().any(|segment| {
            segment.p_type == program_header::PT_LOAD
                && segment.p_flags & (program_header::PF_R | program_header::PF_X)
                    == (program_header::PF_R | program_header::PF_X)
                && segment.p_flags & program_header::PF_W == 0
                && root.st_value >= segment.p_vaddr
                && segment
                    .p_vaddr
                    .checked_add(segment.p_filesz)
                    .is_some_and(|end| root_end <= end)
        })
    {
        return Err(invalid(
            "Detcore constructor root has no unique file-backed RX mapping",
        ));
    }
    let mut dynamics = elf
        .program_headers
        .iter()
        .filter(|segment| segment.p_type == program_header::PT_DYNAMIC);
    let dynamic_segment = dynamics
        .next()
        .ok_or_else(|| invalid("Detcore dynamic mapping is absent"))?;
    if dynamics.next().is_some()
        || dynamic_segment.p_filesz == 0
        || !dynamic_segment.p_filesz.is_multiple_of(16)
        || loaded_file_span(
            bytes,
            elf,
            dynamic_segment.p_vaddr,
            dynamic_segment.p_filesz,
        )? != file_span(bytes, dynamic_segment.p_offset, dynamic_segment.p_filesz)?
    {
        return Err(invalid(
            "Detcore dynamic mapping is ambiguous or physically inconsistent",
        ));
    }
    let mut arrays = elf
        .section_headers
        .iter()
        .filter(|section| section.sh_type == section_header::SHT_INIT_ARRAY);
    let array = arrays
        .next()
        .ok_or_else(|| invalid("Detcore has no constructor array"))?;
    if arrays.next().is_some()
        || array.sh_size == 0
        || !array.sh_addr.is_multiple_of(8)
        || !array.sh_offset.is_multiple_of(8)
        || !array.sh_size.is_multiple_of(8)
        || !matches!(array.sh_entsize, 0 | 8)
        || array.sh_flags & u64::from(section_header::SHF_ALLOC) == 0
    {
        return Err(invalid(
            "Detcore constructor array is empty, ambiguous or unaligned",
        ));
    }
    let end = array
        .sh_addr
        .checked_add(array.sh_size)
        .ok_or_else(|| invalid("Detcore constructor array extent overflow"))?;
    let array_file = file_span(bytes, array.sh_offset, array.sh_size)?;
    if tag(elf, dynamic::DT_INIT_ARRAY)? != Some(array.sh_addr)
        || tag(elf, dynamic::DT_INIT_ARRAYSZ)? != Some(array.sh_size)
        || loaded_file_span(bytes, elf, array.sh_addr, array.sh_size)? != array_file
    {
        return Err(invalid(
            "Detcore constructor dynamic/section/file identity differs",
        ));
    }
    for packed in [35, 36, 37] {
        // DT_RELRSZ, DT_RELR, DT_RELRENT
        if tag(elf, packed)?.is_some() {
            return Err(invalid(
                "packed constructor relocation writers are unsupported",
            ));
        }
    }
    if tag(elf, dynamic::DT_PREINIT_ARRAY)?.is_some_and(|value| value != 0)
        || tag(elf, dynamic::DT_PREINIT_ARRAYSZ)?.is_some_and(|value| value != 0)
        || tag(elf, dynamic::DT_INIT)?
            .is_some_and(|entry| (root.st_value..root_end).contains(&entry))
    {
        return Err(invalid("Detcore has a competing constructor registration"));
    }
    let mut slots = vec![None; array_file.len() / 8];
    for relocation in elf
        .dynrelas
        .iter()
        .chain(elf.dynrels.iter())
        .chain(elf.pltrelocs.iter())
    {
        let width = relocation_width(relocation.r_type)?;
        let writer_end = relocation
            .r_offset
            .checked_add(width)
            .ok_or_else(|| invalid("constructor relocation writer overflow"))?;
        if width == 0 || relocation.r_offset >= end || writer_end <= array.sh_addr {
            continue;
        }
        if width != 8
            || relocation.r_offset < array.sh_addr
            || writer_end > end
            || !(relocation.r_offset - array.sh_addr).is_multiple_of(8)
        {
            return Err(invalid(
                "constructor array has a partial or unaligned writer",
            ));
        }
        if relocation.r_type != reloc::R_X86_64_RELATIVE || relocation.r_sym != 0 {
            return Err(invalid("constructor slot is not symbol-zero RELATIVE"));
        }
        let target = relocation
            .r_addend
            .and_then(|addend| u64::try_from(addend).ok())
            .ok_or_else(|| invalid("constructor slot has no nonnegative RELA addend"))?;
        let index = usize::try_from((relocation.r_offset - array.sh_addr) / 8)
            .map_err(|_| invalid("constructor slot index overflow"))?;
        if slots[index].replace(target).is_some() {
            return Err(invalid("constructor slot has duplicate relocation writers"));
        }
        if target != root.st_value && (root.st_value..root_end).contains(&target) {
            return Err(invalid("constructor slot enters the root interior"));
        }
    }
    if slots.iter().any(Option::is_none) {
        return Err(invalid("constructor slot has no sole RELATIVE relocation"));
    }
    if slots
        .iter()
        .filter(|target| **target == Some(root.st_value))
        .count()
        != 1
    {
        return Err(invalid(
            "Detcore has no unique exact-root RELATIVE constructor slot",
        ));
    }
    Ok(())
}

#[cfg(test)]
mod tests {
    use std::io::Write;

    use super::*;

    const ROOT_ADDRESS: u64 = 0x1100;
    const ARRAY_ADDRESS: u64 = 0x2000;
    const DYNAMIC: usize = 0x240;
    const SYMBOL: usize = 0x318;
    const RELA: usize = 0x400;
    const SECTIONS: usize = 0x600;

    fn word(bytes: &mut [u8], offset: usize, value: u16) {
        bytes[offset..offset + 2].copy_from_slice(&value.to_le_bytes());
    }

    fn dword(bytes: &mut [u8], offset: usize, value: u32) {
        bytes[offset..offset + 4].copy_from_slice(&value.to_le_bytes());
    }

    fn qword(bytes: &mut [u8], offset: usize, value: u64) {
        bytes[offset..offset + 8].copy_from_slice(&value.to_le_bytes());
    }

    fn section(bytes: &mut [u8], index: usize, fields: (u32, u64, u64, u64, u64, u64)) {
        let (kind, flags, address, offset, size, entry_size) = fields;
        let at = SECTIONS + index * 64;
        dword(bytes, at + 4, kind);
        qword(bytes, at + 8, flags);
        qword(bytes, at + 16, address);
        qword(bytes, at + 24, offset);
        qword(bytes, at + 32, size);
        qword(bytes, at + 48, 8);
        qword(bytes, at + 56, entry_size);
    }

    fn image(entries: usize) -> Vec<u8> {
        let mut bytes = vec![0; 0x800];
        bytes[..7].copy_from_slice(b"\x7fELF\x02\x01\x01");
        word(&mut bytes, 16, header::ET_DYN);
        word(&mut bytes, 18, header::EM_X86_64);
        dword(&mut bytes, 20, 1);
        qword(&mut bytes, 32, 64);
        qword(&mut bytes, 40, SECTIONS as u64);
        word(&mut bytes, 52, 64);
        word(&mut bytes, 54, 56);
        word(&mut bytes, 56, 3);
        word(&mut bytes, 58, 64);
        word(&mut bytes, 60, 6);
        for (index, kind, flags, offset, address, size) in [
            (0, program_header::PT_LOAD, 5, 0, 0x1000, 0x200),
            (1, program_header::PT_LOAD, 6, 0x200, 0x2000, 0x380),
            (
                2,
                program_header::PT_DYNAMIC,
                6,
                DYNAMIC as u64,
                0x2040,
                176,
            ),
        ] {
            let at = 64 + index * 56;
            dword(&mut bytes, at, kind);
            dword(&mut bytes, at + 4, flags);
            qword(&mut bytes, at + 8, offset);
            qword(&mut bytes, at + 16, address);
            qword(&mut bytes, at + 32, size);
            qword(&mut bytes, at + 40, size);
            qword(&mut bytes, at + 48, 1);
        }
        bytes[0x100..0x110].copy_from_slice(&[0x90; 16]);
        let strings = format!("\0{ROOT}\0");
        bytes[0x360..0x360 + strings.len()].copy_from_slice(strings.as_bytes());
        for (index, (kind, value)) in [
            (dynamic::DT_STRTAB, 0x2160),
            (dynamic::DT_SYMTAB, 0x2100),
            (dynamic::DT_STRSZ, strings.len() as u64),
            (dynamic::DT_SYMENT, 24),
            (dynamic::DT_HASH, 0x21a0),
            (dynamic::DT_RELA, 0x2200),
            (dynamic::DT_RELASZ, entries as u64 * 24),
            (dynamic::DT_RELAENT, 24),
            (dynamic::DT_INIT_ARRAY, ARRAY_ADDRESS),
            (dynamic::DT_INIT_ARRAYSZ, entries as u64 * 8),
            (dynamic::DT_NULL, 0),
        ]
        .into_iter()
        .enumerate()
        {
            qword(&mut bytes, DYNAMIC + index * 16, kind);
            qword(&mut bytes, DYNAMIC + index * 16 + 8, value);
        }
        dword(&mut bytes, SYMBOL, 1);
        bytes[SYMBOL + 4] = (sym::STB_GLOBAL << 4) | sym::STT_FUNC;
        bytes[SYMBOL + 5] = sym::STV_PROTECTED;
        word(&mut bytes, SYMBOL + 6, 1);
        qword(&mut bytes, SYMBOL + 8, ROOT_ADDRESS);
        qword(&mut bytes, SYMBOL + 16, 16);
        for (index, value) in [1, 2, 1, 0, 0].into_iter().enumerate() {
            dword(&mut bytes, 0x3a0 + index * 4, value);
        }
        for index in 0..entries {
            qword(
                &mut bytes,
                RELA + index * 24,
                ARRAY_ADDRESS + index as u64 * 8,
            );
            qword(
                &mut bytes,
                RELA + index * 24 + 8,
                u64::from(reloc::R_X86_64_RELATIVE),
            );
            qword(&mut bytes, RELA + index * 24 + 16, ROOT_ADDRESS);
        }
        section(
            &mut bytes,
            1,
            (section_header::SHT_PROGBITS, 6, ROOT_ADDRESS, 0x100, 16, 0),
        );
        section(
            &mut bytes,
            2,
            (
                section_header::SHT_INIT_ARRAY,
                3,
                ARRAY_ADDRESS,
                0x200,
                entries as u64 * 8,
                8,
            ),
        );
        section(
            &mut bytes,
            3,
            (section_header::SHT_DYNSYM, 2, 0x2100, 0x300, 48, 24),
        );
        dword(&mut bytes, SECTIONS + 3 * 64 + 40, 4);
        section(
            &mut bytes,
            4,
            (
                section_header::SHT_STRTAB,
                2,
                0x2160,
                0x360,
                strings.len() as u64,
                0,
            ),
        );
        section(
            &mut bytes,
            5,
            (
                section_header::SHT_RELA,
                2,
                0x2200,
                RELA as u64,
                entries as u64 * 24,
                24,
            ),
        );
        dword(&mut bytes, SECTIONS + 5 * 64 + 40, 3);
        bytes
    }

    fn discover(bytes: &[u8]) -> io::Result<()> {
        let mut file = tempfile::NamedTempFile::new()?;
        file.write_all(bytes)?;
        crate::validate_preload_constructor_library(file.path(), "unit Detcore", &[ROOT], ROOT)
            .map(|_| ())
    }

    fn refused(bytes: &[u8], reason: &str) {
        let error = discover(bytes).expect_err("invalid constructor was discovered");
        assert_eq!(error.kind(), io::ErrorKind::InvalidData);
        assert!(
            error.to_string().contains(reason),
            "{error}; expected {reason}"
        );
    }

    #[test]
    fn protected_relative_constructor_with_zero_raw_slot_is_discovered() {
        let bytes = image(1);
        assert_eq!(&bytes[0x200..0x208], &[0; 8]);
        discover(&bytes).expect("local RELATIVE addend must be resolved without raw pointers");
        // LLD may leave this advisory section field absent. The x86-64 ABI,
        // full table extent and sole eight-byte writers still determine width.
        let mut absent_entry_size = bytes.clone();
        qword(&mut absent_entry_size, SECTIONS + 2 * 64 + 56, 0);
        discover(&absent_entry_size)
            .expect("absent section entry size must retain ABI pointer width");
    }

    #[test]
    fn preemptible_and_symbol_relocated_constructor_controls_are_refused() {
        let mut old = image(1);
        old[SYMBOL + 5] = sym::STV_DEFAULT;
        qword(
            &mut old,
            RELA + 8,
            (1_u64 << 32) | u64::from(reloc::R_X86_64_64),
        );
        qword(&mut old, RELA + 16, 0);
        refused(&old, "strong PROTECTED");
        let mut protected_r64 = image(1);
        qword(
            &mut protected_r64,
            RELA + 8,
            (1_u64 << 32) | u64::from(reloc::R_X86_64_64),
        );
        refused(&protected_r64, "symbol-zero RELATIVE");
        let mut weak = image(1);
        weak[SYMBOL + 4] = (sym::STB_WEAK << 4) | sym::STT_FUNC;
        refused(&weak, "strong PROTECTED");
    }

    #[test]
    fn constructor_registration_physical_controls_are_refused() {
        let mut wrong = image(1);
        qword(&mut wrong, RELA + 16, ROOT_ADDRESS + 0x100);
        refused(&wrong, "unique exact-root");
        refused(&image(2), "unique exact-root");
        let mut duplicate_writer = image(2);
        qword(&mut duplicate_writer, RELA + 24, ARRAY_ADDRESS);
        refused(&duplicate_writer, "duplicate relocation");
        let mut unaligned = image(1);
        qword(&mut unaligned, RELA, ARRAY_ADDRESS + 1);
        refused(&unaligned, "partial or unaligned");
        let mut partial = image(1);
        qword(&mut partial, RELA, ARRAY_ADDRESS - 1);
        refused(&partial, "partial or unaligned");
        let mut truncated = image(1);
        qword(&mut truncated, SECTIONS + 2 * 64 + 24, 0x800);
        refused(&truncated, "file extent is truncated");
        let mut missing = image(1);
        qword(&mut missing, DYNAMIC + 6 * 16 + 8, 0);
        refused(&missing, "no sole RELATIVE");
        qword(&mut missing, 0x200, ROOT_ADDRESS);
        refused(&missing, "no sole RELATIVE");
        let mut different_dynamic = image(1);
        qword(&mut different_dynamic, DYNAMIC + 9 * 16 + 8, 16);
        refused(&different_dynamic, "dynamic/section/file identity");
        let mut section_alias = image(1);
        qword(&mut section_alias, SECTIONS + 2 * 64 + 24, 0x208);
        refused(&section_alias, "dynamic/section/file identity");
        let mut incompatible_entry_size = image(1);
        qword(&mut incompatible_entry_size, SECTIONS + 2 * 64 + 56, 4);
        refused(&incompatible_entry_size, "constructor array");
    }
}