ext4fs-core 0.2.3

Forensic-grade ext4 filesystem parser
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
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
#![forbid(unsafe_code)]

use crate::error::{Ext4Error, Result};
use crate::inode::InodeReader;
use crate::ondisk::journal::{
    JournalBlockTag, JournalBlockType, JournalCommit, JournalHeader, JournalRevoke,
    JournalSuperblock,
};
use std::io::{Read, Seek};

/// A mapping from a journal data block to a filesystem block.
#[derive(Debug, Clone)]
pub struct JournalMapping {
    pub journal_block: u64,
    pub filesystem_block: u64,
}

/// A parsed journal transaction.
#[derive(Debug, Clone)]
pub struct Transaction {
    pub sequence: u32,
    pub commit_seconds: i64,
    pub commit_nanoseconds: u32,
    pub mappings: Vec<JournalMapping>,
    pub revoked_blocks: Vec<u64>,
}

/// Parsed journal.
#[derive(Debug, Clone)]
pub struct Journal {
    pub block_size: u32,
    pub total_blocks: u32,
    pub first_block: u32,
    pub transactions: Vec<Transaction>,
    pub is_64bit: bool,
    pub has_csum_v3: bool,
}

/// A historical version of an inode from the journal.
#[derive(Debug, Clone)]
pub struct InodeVersion {
    pub sequence: u32,
    pub commit_seconds: i64,
    pub commit_nanoseconds: u32,
    pub inode: crate::ondisk::Inode,
}

/// Parse the jbd2 journal from the journal inode.
pub fn parse_journal<R: Read + Seek>(reader: &mut InodeReader<R>) -> Result<Journal> {
    // Copy superblock fields before mutable borrow
    let (has_journal, journal_ino, fs_block_size) = {
        let sb = reader.block_reader().superblock();
        (
            sb.has_journal(),
            u64::from(sb.journal_inum),
            sb.block_size as usize,
        )
    };

    if !has_journal {
        return Err(Ext4Error::NoJournal);
    }
    if journal_ino == 0 {
        return Err(Ext4Error::NoJournal);
    }

    // Read entire journal inode data
    let journal_data = reader.read_inode_data(journal_ino)?;
    if journal_data.len() < 1024 {
        return Err(Ext4Error::JournalCorrupt("journal too small".into()));
    }

    // Parse journal superblock (first block of journal)
    let jsb = JournalSuperblock::parse(&journal_data[..fs_block_size.min(journal_data.len())])?;
    let j_block_size = jsb.block_size as usize;
    if j_block_size == 0 {
        return Err(Ext4Error::JournalCorrupt("journal block size is 0".into()));
    }

    let is_64bit = jsb.is_64bit();
    let has_csum_v3 = jsb.has_csum_v3();
    let total_blocks = jsb.max_len;
    let first_block = jsb.first;

    // Scan journal blocks for transactions
    let mut transactions = Vec::new();
    let mut current_mappings: Vec<JournalMapping> = Vec::new();
    let mut current_revoked: Vec<u64> = Vec::new();
    let mut pending_tags: Vec<u64> = Vec::new();

    let mut block_idx = first_block as usize;
    while block_idx < total_blocks as usize {
        let offset = block_idx * j_block_size;
        if offset + 12 > journal_data.len() {
            break;
        }

        let end = (offset + j_block_size).min(journal_data.len());
        let block_data = &journal_data[offset..end];

        if let Ok(header) = JournalHeader::parse(block_data) {
            match header.block_type {
                JournalBlockType::Descriptor => {
                    pending_tags.clear();
                    current_mappings.clear();
                    current_revoked.clear();
                    let mut tag_offset = 12;
                    loop {
                        if tag_offset + 16 > block_data.len() {
                            break;
                        }
                        let tag = JournalBlockTag::parse_v3(&block_data[tag_offset..], is_64bit);
                        pending_tags.push(tag.blocknr);
                        let is_last = tag.last_tag;
                        if tag.tag_size == 0 {
                            break;
                        }
                        tag_offset += tag.tag_size;
                        if is_last {
                            break;
                        }
                    }
                    // Map each tag to its corresponding data block
                    for (i, &fs_block) in pending_tags.iter().enumerate() {
                        let data_block = block_idx as u64 + 1 + i as u64;
                        current_mappings.push(JournalMapping {
                            journal_block: data_block,
                            filesystem_block: fs_block,
                        });
                    }
                    // Skip past descriptor + data blocks
                    block_idx += 1 + pending_tags.len();
                    continue;
                }
                JournalBlockType::Commit => {
                    // Only record the transaction if we can parse the commit
                    // block. Fabricating an epoch timestamp on parse failure
                    // would be forensically misleading.
                    if let Ok(commit) = JournalCommit::parse(block_data) {
                        transactions.push(Transaction {
                            sequence: commit.sequence,
                            commit_seconds: commit.commit_seconds,
                            commit_nanoseconds: commit.commit_nanoseconds,
                            mappings: std::mem::take(&mut current_mappings),
                            revoked_blocks: std::mem::take(&mut current_revoked),
                        });
                    } else {
                        // Discard the uncommitted mappings/revokes
                        current_mappings.clear();
                        current_revoked.clear();
                    }
                }
                JournalBlockType::Revoke => {
                    if let Ok(revoke) = JournalRevoke::parse(block_data, is_64bit) {
                        current_revoked.extend(revoke.revoked_blocks);
                    }
                }
                JournalBlockType::SuperblockV1
                | JournalBlockType::SuperblockV2
                | JournalBlockType::Unknown(_) => {}
            }
        }
        block_idx += 1;
    }

    Ok(Journal {
        block_size: j_block_size as u32,
        total_blocks,
        first_block,
        transactions,
        is_64bit,
        has_csum_v3,
    })
}

/// Find all journal versions of a specific inode's metadata block.
///
/// Scans journal transactions for writes to the block containing the given
/// inode, then parses the inode from each historical copy of that block.
/// Results are sorted by transaction sequence number (oldest first).
pub fn inode_history<R: Read + Seek>(
    reader: &mut InodeReader<R>,
    journal: &Journal,
    ino: u64,
) -> Result<Vec<InodeVersion>> {
    // Copy superblock fields before mutable borrow
    let (ipg, inode_size_u64, inode_size_u16, block_size, journal_ino) = {
        let sb = reader.block_reader().superblock();
        (
            u64::from(sb.inodes_per_group),
            u64::from(sb.inode_size),
            sb.inode_size,
            u64::from(sb.block_size),
            u64::from(sb.journal_inum),
        )
    };

    let group = ((ino - 1) / ipg) as u32;
    let index = (ino - 1) % ipg;
    let inode_table = reader.block_reader().inode_table_block(group)?;
    let inode_offset_in_table = index * inode_size_u64;
    let target_block = inode_table + inode_offset_in_table / block_size;
    let offset_in_block = (inode_offset_in_table % block_size) as usize;

    let journal_data = reader.read_inode_data(journal_ino)?;
    let j_block_size = journal.block_size as usize;

    let mut versions = Vec::new();
    for txn in &journal.transactions {
        for mapping in &txn.mappings {
            if mapping.filesystem_block == target_block {
                let j_offset = mapping.journal_block as usize * j_block_size;
                if j_offset + j_block_size <= journal_data.len() {
                    let block_data = &journal_data[j_offset..j_offset + j_block_size];
                    let end = offset_in_block + inode_size_u64 as usize;
                    if end <= block_data.len() {
                        if let Ok(inode) = crate::ondisk::Inode::parse(
                            &block_data[offset_in_block..end],
                            inode_size_u16,
                        ) {
                            versions.push(InodeVersion {
                                sequence: txn.sequence,
                                commit_seconds: txn.commit_seconds,
                                commit_nanoseconds: txn.commit_nanoseconds,
                                inode,
                            });
                        }
                    }
                }
            }
        }
    }

    versions.sort_by_key(|v| v.sequence);
    Ok(versions)
}

#[cfg(test)]
mod tests {
    use super::*;
    use crate::block::BlockReader;
    use crate::inode::InodeReader;
    use std::io::Cursor;

    fn open_minimal() -> Option<InodeReader<Cursor<Vec<u8>>>> {
        let path = concat!(env!("CARGO_MANIFEST_DIR"), "/../tests/data/minimal.img");
        let data = std::fs::read(path).ok()?;
        let br = BlockReader::open(Cursor::new(data)).ok()?;
        Some(InodeReader::new(br))
    }

    fn open_forensic() -> Option<InodeReader<Cursor<Vec<u8>>>> {
        let path = concat!(env!("CARGO_MANIFEST_DIR"), "/../tests/data/forensic.img");
        let data = std::fs::read(path).ok()?;
        let br = BlockReader::open(Cursor::new(data)).ok()?;
        Some(InodeReader::new(br))
    }

    fn forensic_raw() -> Option<Vec<u8>> {
        let path = concat!(env!("CARGO_MANIFEST_DIR"), "/../tests/data/forensic.img");
        std::fs::read(path).ok()
    }

    // ---------------------------------------------------------------
    // Existing tests (minimal.img — no journal, exercises skip paths)
    // ---------------------------------------------------------------

    #[test]
    fn parse_journal_from_minimal() {
        let mut reader = if let Some(r) = open_minimal() {
            r
        } else {
            eprintln!("skip: minimal.img not found");
            return;
        };
        if !reader.block_reader().superblock().has_journal() {
            eprintln!("skip: no journal in image");
            return;
        }
        let journal = parse_journal(&mut reader).unwrap();
        assert!(journal.block_size > 0);
        assert!(!journal.transactions.is_empty());
    }

    #[test]
    fn transactions_have_commit_timestamps() {
        let mut reader = if let Some(r) = open_minimal() {
            r
        } else {
            eprintln!("skip: minimal.img not found");
            return;
        };
        if !reader.block_reader().superblock().has_journal() {
            return;
        }
        let journal = parse_journal(&mut reader).unwrap();
        for txn in &journal.transactions {
            assert!(txn.sequence > 0);
        }
    }

    // ---------------------------------------------------------------
    // NoJournal error path — minimal.img has no journal
    // ---------------------------------------------------------------

    #[test]
    fn parse_journal_no_journal_error() {
        let mut reader = if let Some(r) = open_minimal() {
            r
        } else {
            eprintln!("skip: minimal.img not found");
            return;
        };
        // minimal.img has no journal feature
        assert!(!reader.block_reader().superblock().has_journal());
        match parse_journal(&mut reader) {
            Err(Ext4Error::NoJournal) => {} // expected
            other => panic!("expected NoJournal, got: {other:?}"),
        }
    }

    // ---------------------------------------------------------------
    // forensic.img tests — HAS journal
    // ---------------------------------------------------------------

    #[test]
    fn parse_journal_from_forensic() {
        let mut reader = if let Some(r) = open_forensic() {
            r
        } else {
            eprintln!("skip: forensic.img not found");
            return;
        };
        assert!(reader.block_reader().superblock().has_journal());
        let journal = parse_journal(&mut reader).unwrap();
        assert!(journal.block_size > 0);
        assert!(journal.total_blocks > 0);
        assert!(journal.first_block > 0);
        // A 32 MiB image that's been used should have at least one transaction
        assert!(
            !journal.transactions.is_empty(),
            "forensic.img journal should have transactions"
        );
    }

    #[test]
    fn forensic_transactions_have_sequence_and_timestamps() {
        let mut reader = if let Some(r) = open_forensic() {
            r
        } else {
            eprintln!("skip: forensic.img not found");
            return;
        };
        let journal = parse_journal(&mut reader).unwrap();
        for txn in &journal.transactions {
            assert!(txn.sequence > 0, "sequence should be positive");
            // Commit timestamps should be reasonable (after 2020)
            assert!(
                txn.commit_seconds >= 1_577_836_800, // 2020-01-01
                "commit_seconds {} too small",
                txn.commit_seconds
            );
        }
    }

    #[test]
    fn forensic_transactions_have_mappings() {
        let mut reader = if let Some(r) = open_forensic() {
            r
        } else {
            eprintln!("skip: forensic.img not found");
            return;
        };
        let journal = parse_journal(&mut reader).unwrap();
        // At least some transactions should have block mappings
        let has_mappings = journal.transactions.iter().any(|t| !t.mappings.is_empty());
        assert!(has_mappings, "some transactions should have block mappings");

        // Verify mapping fields are reasonable
        for txn in &journal.transactions {
            for m in &txn.mappings {
                assert!(m.journal_block > 0, "journal_block should be > 0");
            }
        }
    }

    #[test]
    fn forensic_journal_sequence_monotonic() {
        let mut reader = if let Some(r) = open_forensic() {
            r
        } else {
            eprintln!("skip: forensic.img not found");
            return;
        };
        let journal = parse_journal(&mut reader).unwrap();
        // Transactions should be in increasing sequence order
        for w in journal.transactions.windows(2) {
            assert!(
                w[1].sequence >= w[0].sequence,
                "sequences should be monotonic: {} >= {}",
                w[1].sequence,
                w[0].sequence
            );
        }
    }

    // ---------------------------------------------------------------
    // inode_history tests
    // ---------------------------------------------------------------

    #[test]
    fn inode_history_for_known_inode() {
        let mut reader = if let Some(r) = open_forensic() {
            r
        } else {
            eprintln!("skip: forensic.img not found");
            return;
        };
        let journal = parse_journal(&mut reader).unwrap();
        // Inode 12 is hello.txt — should appear in journal if inode table
        // block was written
        let versions = inode_history(&mut reader, &journal, 12).unwrap();
        // May or may not have versions depending on journal contents
        for v in &versions {
            assert!(v.sequence > 0);
            assert!(v.commit_seconds >= 0);
        }
        // If there are multiple versions, they should be sorted by sequence
        for w in versions.windows(2) {
            assert!(w[0].sequence <= w[1].sequence);
        }
    }

    #[test]
    fn inode_history_for_deleted_inode() {
        let mut reader = if let Some(r) = open_forensic() {
            r
        } else {
            eprintln!("skip: forensic.img not found");
            return;
        };
        let journal = parse_journal(&mut reader).unwrap();
        // Inode 21 was deleted — may have historical versions showing
        // the pre-deletion state
        let versions = inode_history(&mut reader, &journal, 21).unwrap();
        // Check that if versions exist, they have valid sequence numbers
        for v in &versions {
            assert!(v.sequence > 0);
        }
    }

    #[test]
    fn inode_history_for_nonexistent_inode() {
        let mut reader = if let Some(r) = open_forensic() {
            r
        } else {
            eprintln!("skip: forensic.img not found");
            return;
        };
        let journal = parse_journal(&mut reader).unwrap();
        // Use a high inode number that likely has no journal history
        // but is within range (use inode count from superblock)
        let max_ino = reader.block_reader().superblock().inodes_count;
        if max_ino > 100 {
            let versions = inode_history(&mut reader, &journal, u64::from(max_ino - 1)).unwrap();
            // Should return empty or minimal versions
            // (just checking it doesn't crash)
            let _ = versions;
        }
    }

    // ---------------------------------------------------------------
    // JournalCorrupt error paths
    // ---------------------------------------------------------------

    #[test]
    fn journal_corrupt_truncated_data() {
        let mut data = if let Some(d) = forensic_raw() {
            d
        } else {
            eprintln!("skip: forensic.img not found");
            return;
        };
        // Find the journal inode data and corrupt it by zeroing the journal
        // superblock area. The journal inode is typically inode 8.
        // We'll read the image, find the journal, then corrupt it.
        let reader = open_forensic().unwrap();
        let sb = reader.block_reader().superblock();
        let journal_ino = u64::from(sb.journal_inum);
        assert!(journal_ino > 0);

        // Read journal inode to find its data blocks
        let reader2 = open_forensic().unwrap();
        let mappings = reader2.inode_block_map(journal_ino).unwrap();
        if mappings.is_empty() {
            eprintln!("skip: journal inode has no block mappings");
            return;
        }

        // Zero out the journal superblock (first block of journal data)
        // to make it unparseable, causing JournalCorrupt or similar error
        let first_phys = mappings[0].physical_block;
        let block_size = sb.block_size as usize;
        let offset = first_phys as usize * block_size;
        if offset + block_size <= data.len() {
            // Zero just the journal superblock magic to make parse fail
            for b in &mut data[offset..offset + 12] {
                *b = 0;
            }
        }

        let br = BlockReader::open(Cursor::new(data)).unwrap();
        let mut reader3 = InodeReader::new(br);
        let result = parse_journal(&mut reader3);
        // Should fail with some error (corrupt journal superblock)
        assert!(result.is_err(), "corrupted journal should fail to parse");
    }

    #[test]
    fn journal_corrupt_zero_block_size() {
        let mut data = if let Some(d) = forensic_raw() {
            d
        } else {
            eprintln!("skip: forensic.img not found");
            return;
        };
        let reader = open_forensic().unwrap();
        let sb = reader.block_reader().superblock();
        let journal_ino = u64::from(sb.journal_inum);

        let reader2 = open_forensic().unwrap();
        let mappings = reader2.inode_block_map(journal_ino).unwrap();
        if mappings.is_empty() {
            eprintln!("skip: journal inode has no block mappings");
            return;
        }

        let first_phys = mappings[0].physical_block;
        let block_size = sb.block_size as usize;
        let offset = first_phys as usize * block_size;

        // The journal superblock has blocksize at offset 12 (big-endian u32).
        // Set it to 0 to trigger the "journal block size is 0" error.
        // But we need to keep the magic valid so JournalSuperblock::parse succeeds.
        // Journal magic is at offset 0: 0xC03B3998 (big-endian)
        if offset + 16 <= data.len() {
            // Zero the block_size field (offset 12-15 in journal superblock)
            data[offset + 12] = 0;
            data[offset + 13] = 0;
            data[offset + 14] = 0;
            data[offset + 15] = 0;
        }

        let br = BlockReader::open(Cursor::new(data)).unwrap();
        let mut reader3 = InodeReader::new(br);
        let result = parse_journal(&mut reader3);
        match result {
            Err(Ext4Error::JournalCorrupt(msg)) => {
                assert!(
                    msg.contains("block size is 0"),
                    "expected 'block size is 0' in: {msg}"
                );
            }
            // If the journal superblock parse itself fails due to the zero
            // block size affecting other validation, that's also acceptable
            Err(_) => {}
            Ok(_) => panic!("should have failed with zero journal block size"),
        }
    }

    #[test]
    fn journal_corrupt_too_small() {
        let data = if let Some(d) = forensic_raw() {
            d
        } else {
            eprintln!("skip: forensic.img not found");
            return;
        };
        let reader = open_forensic().unwrap();
        let sb = reader.block_reader().superblock();
        let journal_ino = u64::from(sb.journal_inum);

        // Patch the journal inode's size to be very small (< 1024)
        // so we trigger the "journal too small" path
        let mut patched = data.clone();

        // Find inode position for journal inode
        let ipg = u64::from(sb.inodes_per_group);
        let inode_size = u64::from(sb.inode_size);
        let bs = u64::from(sb.block_size);
        let group = ((journal_ino - 1) / ipg) as u32;
        let index = (journal_ino - 1) % ipg;
        let inode_table = reader.block_reader().inode_table_block(group).unwrap();
        let ino_offset = (inode_table * bs + index * inode_size) as usize;

        // Set i_size_lo (offset 0x04) to 512 and i_size_hi (offset 0x6C) to 0
        // Also zero the block count so read_inode_data returns minimal data
        patched[ino_offset + 0x04] = 0x00;
        patched[ino_offset + 0x05] = 0x02; // 512 in little-endian
        patched[ino_offset + 0x06] = 0x00;
        patched[ino_offset + 0x07] = 0x00;
        patched[ino_offset + 0x6C] = 0x00; // size_hi = 0
        patched[ino_offset + 0x6D] = 0x00;
        patched[ino_offset + 0x6E] = 0x00;
        patched[ino_offset + 0x6F] = 0x00;

        let br = BlockReader::open(Cursor::new(patched)).unwrap();
        let mut reader3 = InodeReader::new(br);
        let result = parse_journal(&mut reader3);
        // read_inode_data may truncate differently or still parse — either way
        // the truncated path was exercised; only the corrupt case asserts a msg.
        if let Err(Ext4Error::JournalCorrupt(msg)) = result {
            assert!(msg.contains("too small"), "expected 'too small' in: {msg}");
        }
    }
}