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
use std;
use std::io;
use ::Time;
use ::parse_error;
use ::errors::*;
use ::errors::Result;
use ::errors::ErrorKind::*;
use ::read_le16;
use ::read_le32;
use std::collections::HashMap;
use crc;
use byteorder::{ReadBytesExt, LittleEndian, ByteOrder};
use std::io::Read;
use std::io::Seek;
const EXT4_SUPER_MAGIC: u16 = 0xEF53;
const INODE_BASE_LEN: usize = 128;
const XATTR_MAGIC: u32 = 0xEA020000;
bitflags! {
struct CompatibleFeature: u32 {
const DIR_PREALLOC = 0x0001;
const IMAGIC_INODES = 0x0002;
const HAS_JOURNAL = 0x0004;
const EXT_ATTR = 0x0008;
const RESIZE_INODE = 0x0010;
const DIR_INDEX = 0x0020;
const SPARSE_SUPER2 = 0x0200;
}
}
bitflags! {
struct CompatibleFeatureReadOnly: u32 {
const SPARSE_SUPER = 0x0001;
const LARGE_FILE = 0x0002;
const BTREE_DIR = 0x0004;
const HUGE_FILE = 0x0008;
const GDT_CSUM = 0x0010;
const DIR_NLINK = 0x0020;
const EXTRA_ISIZE = 0x0040;
const QUOTA = 0x0100;
const BIGALLOC = 0x0200;
const METADATA_CSUM = 0x0400;
const READONLY = 0x1000;
const PROJECT = 0x2000;
}
}
bitflags! {
struct IncompatibleFeature: u32 {
const INCOMPAT_COMPRESSION = 0x0001;
const INCOMPAT_FILETYPE = 0x0002;
const INCOMPAT_RECOVER = 0x0004;
const INCOMPAT_JOURNAL_DEV = 0x0008;
const INCOMPAT_META_BG = 0x0010;
const INCOMPAT_EXTENTS = 0x0040;
const INCOMPAT_64BIT = 0x0080;
const INCOMPAT_MMP = 0x0100;
const INCOMPAT_FLEX_BG = 0x0200;
const INCOMPAT_EA_INODE = 0x0400;
const INCOMPAT_DIRDATA = 0x1000;
const INCOMPAT_CSUM_SEED = 0x2000;
const INCOMPAT_LARGEDIR = 0x4000;
const INCOMPAT_INLINE_DATA = 0x8000;
const INCOMPAT_ENCRYPT = 0x10000;
}
}
pub fn superblock<R>(mut reader: R, options: &::Options) -> Result<::SuperBlock<R>>
where R: io::Read + io::Seek {
let mut entire_superblock = [0u8; 1024];
reader.read_exact(&mut entire_superblock)?;
let mut inner = io::Cursor::new(&mut entire_superblock[..]);
inner.read_u32::<LittleEndian>()?;
let s_blocks_count_lo =
inner.read_u32::<LittleEndian>()?;
inner.read_u32::<LittleEndian>()?;
inner.read_u32::<LittleEndian>()?;
inner.read_u32::<LittleEndian>()?;
let s_first_data_block =
inner.read_u32::<LittleEndian>()?;
let s_log_block_size =
inner.read_u32::<LittleEndian>()?;
inner.read_u32::<LittleEndian>()?;
let s_blocks_per_group =
inner.read_u32::<LittleEndian>()?;
inner.read_u32::<LittleEndian>()?;
let s_inodes_per_group =
inner.read_u32::<LittleEndian>()?;
inner.read_u32::<LittleEndian>()?;
inner.read_u32::<LittleEndian>()?;
inner.read_u16::<LittleEndian>()?;
inner.read_u16::<LittleEndian>()?;
let s_magic =
inner.read_u16::<LittleEndian>()?;
ensure!(EXT4_SUPER_MAGIC == s_magic,
NotFound(format!("invalid magic number: {:x}", s_magic)));
let s_state =
inner.read_u16::<LittleEndian>()?;
inner.read_u16::<LittleEndian>()?;
inner.read_u16::<LittleEndian>()?;
inner.read_u32::<LittleEndian>()?;
inner.read_u32::<LittleEndian>()?;
let s_creator_os =
inner.read_u32::<LittleEndian>()?;
ensure!(0 == s_creator_os,
UnsupportedFeature(format!("only support filesystems created on linux, not '{}'", s_creator_os)));
let s_rev_level =
inner.read_u32::<LittleEndian>()?;
inner.read_u16::<LittleEndian>()?;
inner.read_u16::<LittleEndian>()?;
inner.read_u32::<LittleEndian>()?;
let s_inode_size =
inner.read_u16::<LittleEndian>()?;
inner.read_u16::<LittleEndian>()?;
let s_feature_compat =
inner.read_u32::<LittleEndian>()?;
let compatible_features = CompatibleFeature::from_bits_truncate(s_feature_compat);
let load_xattrs = compatible_features.contains(EXT_ATTR);
let s_feature_incompat =
inner.read_u32::<LittleEndian>()?;
let incompatible_features = IncompatibleFeature::from_bits(s_feature_incompat)
.ok_or_else(|| parse_error(format!("completely unsupported incompatible feature flag: {:b}", s_feature_incompat)))?;
let supported_incompatible_features =
INCOMPAT_FILETYPE
| INCOMPAT_EXTENTS
| INCOMPAT_FLEX_BG
| INCOMPAT_RECOVER
| INCOMPAT_64BIT;
if incompatible_features.intersects(!supported_incompatible_features) {
return Err(parse_error(format!("some unsupported incompatible feature flags: {:?}",
incompatible_features & !supported_incompatible_features)));
}
let long_structs = incompatible_features.contains(INCOMPAT_64BIT);
let s_feature_ro_compat =
inner.read_u32::<LittleEndian>()?;
let compatible_features_read_only = CompatibleFeatureReadOnly::from_bits_truncate(s_feature_ro_compat);
let has_checksums = compatible_features_read_only.contains(METADATA_CSUM);
ensure!(!(has_checksums && compatible_features_read_only.contains(GDT_CSUM)),
AssumptionFailed("metadata checksums are incompatible with the GDT checksum feature".to_string())
);
ensure!(has_checksums || ::Checksums::Required != options.checksums,
NotFound("checksums are disabled, but required by options".to_string()));
let mut s_uuid = [0; 16];
inner.read_exact(&mut s_uuid)?;
let mut s_volume_name = [0u8; 16];
inner.read_exact(&mut s_volume_name)?;
let mut s_last_mounted = [0u8; 64];
inner.read_exact(&mut s_last_mounted)?;
inner.read_u32::<LittleEndian>()?;
inner.read_u8()?;
inner.read_u8()?;
inner.read_u16::<LittleEndian>()?;
let mut s_journal_uuid = [0u8; 16];
inner.read_exact(&mut s_journal_uuid)?;
inner.read_u32::<LittleEndian>()?;
inner.read_u32::<LittleEndian>()?;
inner.read_u32::<LittleEndian>()?;
let mut s_hash_seed = [0u8; 4 * 4];
inner.read_exact(&mut s_hash_seed)?;
inner.read_u8()?;
inner.read_u8()?;
let s_desc_size =
inner.read_u16::<LittleEndian>()?;
inner.read_u32::<LittleEndian>()?;
inner.read_u32::<LittleEndian>()?;
inner.read_u32::<LittleEndian>()?;
let mut s_jnl_blocks = [0; 17 * 4];
inner.read_exact(&mut s_jnl_blocks)?;
let s_blocks_count_hi =
if !long_structs { None } else {
Some(inner.read_u32::<LittleEndian>()?)
};
if has_checksums {
inner.seek(io::SeekFrom::End(-4))?;
let s_checksum = inner.read_u32::<LittleEndian>()?;
let expected = ext4_style_crc32c_le(!0, &inner.into_inner()[..(1024-4)]);
ensure!(s_checksum == expected,
AssumptionFailed(format!("superblock reports checksums supported, but didn't match: {:x} != {:x}",
s_checksum, expected)));
}
{
const S_STATE_UNMOUNTED_CLEANLY: u16 = 0b01;
const S_STATE_ERRORS_DETECTED: u16 = 0b10;
if s_state & S_STATE_UNMOUNTED_CLEANLY == 0 || s_state & S_STATE_ERRORS_DETECTED != 0 {
return Err(parse_error(format!("filesystem is not in a clean state: {:b}", s_state)));
}
}
if 0 == s_inodes_per_group {
return Err(parse_error("inodes per group cannot be zero".to_string()));
}
let block_size: u32 = match s_log_block_size {
0 => 1024,
1 => 2048,
2 => 4096,
6 => 65536,
_ => {
return Err(parse_error(format!("unexpected block size: 2^{}", s_log_block_size + 10)));
}
};
if !long_structs {
ensure!(0 == s_desc_size,
AssumptionFailed(format!("outside long mode, block group desc size must be zero, not {}", s_desc_size)));
}
ensure!(1 == s_rev_level,
UnsupportedFeature(format!("rev level {}", s_rev_level)));
let group_table_pos = if 1024 == block_size {
1024
+ 1024
} else {
block_size
};
reader.seek(io::SeekFrom::Start(group_table_pos as u64))?;
let blocks_count = (
s_blocks_count_lo as u64
+ ((s_blocks_count_hi.unwrap_or(0) as u64) << 32)
- s_first_data_block as u64 + s_blocks_per_group as u64 - 1
) / s_blocks_per_group as u64;
let groups = ::block_groups::BlockGroups::new(&mut reader, blocks_count,
s_desc_size, s_inodes_per_group,
block_size, s_inode_size)?;
let uuid_checksum = if has_checksums {
Some(ext4_style_crc32c_le(!0, &s_uuid))
} else {
None
};
Ok(::SuperBlock {
inner: reader,
load_xattrs,
uuid_checksum,
groups,
})
}
pub struct ParsedInode {
pub stat: ::Stat,
pub flags: ::InodeFlags,
pub core: [u8; ::INODE_CORE_SIZE],
pub checksum_prefix: Option<u32>,
}
pub fn inode<F>(mut data: Vec<u8>, load_block: F, uuid_checksum: Option<u32>, number: u32) -> Result<ParsedInode>
where F: FnOnce(u64) -> Result<Vec<u8>> {
ensure!(data.len() >= INODE_BASE_LEN,
AssumptionFailed("inode isn't bigger than the minimum length".to_string()));
let i_mode = read_le16(&data[0x00..0x02]);
let i_uid = read_le16(&data[0x02..0x04]);
let i_size_lo = read_le32(&data[0x04..0x08]);
let i_atime = read_le32(&data[0x08..0x0C]);
let i_ctime = read_le32(&data[0x0C..0x10]);
let i_mtime = read_le32(&data[0x10..0x14]);
let i_gid = read_le16(&data[0x18..0x1A]);
let i_links_count = read_le16(&data[0x1A..0x1C]);
let i_flags = read_le32(&data[0x20..0x24]);
let mut i_block = [0u8; ::INODE_CORE_SIZE];
i_block.clone_from_slice(&data[0x28..0x64]);
let i_generation = read_le32(&data[0x64..0x68]);
let i_file_acl_lo = read_le32(&data[0x68..0x6C]);
let i_size_high = read_le32(&data[0x6C..0x70]);
let l_i_file_acl_high = read_le16(&data[0x76..0x78]);
let l_i_uid_high = read_le16(&data[0x78..0x7A]);
let l_i_gid_high = read_le16(&data[0x7A..0x7C]);
let l_i_checksum_lo = read_le16(&data[0x7C..0x7E]);
let i_extra_isize = if data.len() < 0x82 { 0 } else { read_le16(&data[0x80..0x82]) };
let inode_end = INODE_BASE_LEN + i_extra_isize as usize;
ensure!(inode_end <= data.len(),
AssumptionFailed(format!("more extra inode ({}) than inode ({})", inode_end, data.len())));
let i_checksum_hi = if i_extra_isize < 2 + 2 { None } else { Some(read_le16(&data[0x82..0x84])) };
let i_ctime_extra = if i_extra_isize < 6 + 2 { None } else { Some(read_le32(&data[0x84..0x88])) };
let i_mtime_extra = if i_extra_isize < 10 + 2 { None } else { Some(read_le32(&data[0x88..0x8C])) };
let i_atime_extra = if i_extra_isize < 14 + 2 { None } else { Some(read_le32(&data[0x8C..0x90])) };
let i_crtime = if i_extra_isize < 18 + 2 { None } else { Some(read_le32(&data[0x90..0x94])) };
let i_crtime_extra = if i_extra_isize < 22 + 2 { None } else { Some(read_le32(&data[0x94..0x98])) };
let mut checksum_prefix = None;
if let Some(uuid_checksum) = uuid_checksum {
data[0x7C] = 0;
data[0x7D] = 0;
let mut bytes = [0u8; 8];
LittleEndian::write_u32(&mut bytes[0..4], number);
LittleEndian::write_u32(&mut bytes[4..8], i_generation);
checksum_prefix = Some(ext4_style_crc32c_le(uuid_checksum, &bytes));
if i_checksum_hi.is_some() {
data[0x82] = 0;
data[0x83] = 0;
}
let computed = ext4_style_crc32c_le(checksum_prefix.unwrap(), &data);
if let Some(high) = i_checksum_hi {
let expected = (l_i_checksum_lo as u32) | ((high as u32) << 16);
ensure!(expected == computed,
AssumptionFailed(format!("full checksum mismatch: on-disc: {:08x} computed: {:08x}",
expected, computed)));
} else {
let short_computed = computed as u16;
ensure!(l_i_checksum_lo == short_computed,
AssumptionFailed(format!("short checksum mismatch: on-disc: {:04x} computed: {:04x}",
l_i_checksum_lo, short_computed)));
}
}
let mut xattrs = HashMap::new();
if inode_end + 4 <= data.len() && XATTR_MAGIC == read_le32(&data[inode_end..(inode_end + 4)]) {
let table_start = &data[inode_end + 4..];
read_xattrs(&mut xattrs, table_start, table_start)?;
}
if 0 != i_file_acl_lo || 0 != l_i_file_acl_high {
let block = i_file_acl_lo as u64 | ((l_i_file_acl_high as u64) << 32);
xattr_block(&mut xattrs, load_block(block)?, uuid_checksum, block)
.chain_err(|| format!("loading xattr block {}", block))?
}
let stat = ::Stat {
extracted_type: ::FileType::from_mode(i_mode)
.ok_or_else(|| UnsupportedFeature(format!("unexpected file type in mode: {:b}", i_mode)))?,
file_mode: i_mode & 0b111_111_111_111,
uid: i_uid as u32 | ((l_i_uid_high as u32) << 16),
gid: i_gid as u32 | ((l_i_gid_high as u32) << 16),
size: (i_size_lo as u64) | ((i_size_high as u64) << 32),
atime: Time {
epoch_secs: i_atime,
nanos: i_atime_extra,
},
ctime: Time {
epoch_secs: i_ctime,
nanos: i_ctime_extra,
},
mtime: Time {
epoch_secs: i_mtime,
nanos: i_mtime_extra,
},
btime: i_crtime.map(|epoch_secs| Time {
epoch_secs,
nanos: i_crtime_extra,
}),
link_count: i_links_count,
xattrs,
};
Ok(ParsedInode {
stat,
flags: ::InodeFlags::from_bits(i_flags)
.ok_or_else(|| UnsupportedFeature(format!("unrecognised inode flags: {:b}", i_flags)))?,
core: i_block,
checksum_prefix,
})
}
fn xattr_block(xattrs: &mut HashMap<String, Vec<u8>>, mut data: Vec<u8>,
uuid_checksum: Option<u32>, block_number: u64) -> Result<()> {
ensure!(data.len() > 0x20,
AssumptionFailed("xattr block is way too short".to_string()));
ensure!(XATTR_MAGIC == read_le32(&data[0x00..0x04]),
AssumptionFailed("xattr block contained invalid magic number".to_string()));
let x_blocks_used = read_le32(&data[0x08..0x0C]);
let x_checksum = read_le32(&data[0x10..0x14]);
if let Some(uuid_checksum) = uuid_checksum {
data[0x10] = 0;
data[0x11] = 0;
data[0x12] = 0;
data[0x13] = 0;
let mut bytes = [0u8; 8];
LittleEndian::write_u64(&mut bytes[0..8], block_number);
let base = ext4_style_crc32c_le(uuid_checksum, &bytes);
let computed = ext4_style_crc32c_le(base, &data);
ensure!(x_checksum == computed,
AssumptionFailed(format!("xattr block checksum invalid: on-disk: {:08x}, computed: {:08x}",
x_checksum, computed)));
}
ensure!(1 == x_blocks_used,
UnsupportedFeature(format!("must have exactly one xattr block, not {}", x_blocks_used)));
read_xattrs(xattrs, &data[0x20..], &data[..])
}
fn read_xattrs(xattrs: &mut HashMap<String, Vec<u8>>, mut reading: &[u8], block_offset_start: &[u8]) -> Result<()> {
loop {
ensure!(reading.len() > 0x10,
AssumptionFailed("out of block while reading xattr header".to_string()));
let e_name_len = reading[0x00];
let e_name_prefix_magic = reading[0x01];
let e_value_offset = read_le16(&reading[0x02..0x04]);
let e_block = read_le32(&reading[0x04..0x08]);
if 0 == e_name_len && 0 == e_name_prefix_magic && 0 == e_value_offset && 0 == e_block {
break;
}
let e_value_size = read_le32(&reading[0x08..0x0C]);
let end_of_name = 0x10 + e_name_len as usize;
ensure!(reading.len() > end_of_name,
AssumptionFailed("out of block while reading xattr name".to_string()));
let name_suffix = &reading[0x10..end_of_name];
let name = format!("{}{}",
match e_name_prefix_magic {
0 => "",
1 => "user.",
2 => "system.posix_acl_access",
3 => "system.posix_acl_default",
4 => "trusted.",
6 => "security.",
7 => "system.",
_ => bail!(UnsupportedFeature(format!("unsupported name prefix encoding: {}", e_name_prefix_magic))),
},
std::str::from_utf8(name_suffix).chain_err(|| "name is invalid utf-8")?
);
let start = e_value_offset as usize;
let end = start + e_value_size as usize;
ensure!(start <= block_offset_start.len() && end <= block_offset_start.len(),
AssumptionFailed(format!("xattr value out of range: {}-{} > {}", start, end, block_offset_start.len())));
xattrs.insert(name, block_offset_start[start..end].to_vec());
let next_record = end_of_name + ((4 - (end_of_name % 4)) % 4);
reading = &reading[next_record..];
}
Ok(())
}
pub fn ext4_style_crc32c_le(seed: u32, buf: &[u8]) -> u32 {
crc::crc32::update(seed ^ (!0), &crc::crc32::CASTAGNOLI_TABLE, buf) ^ (!0u32)
}
#[cfg(test)]
mod tests {
use super::ext4_style_crc32c_le;
#[test]
fn crcs() {
assert_eq!(0xffff_ffffu32, !0);
assert_eq!(0x1cf96d7cu32, 0xe3069283u32 ^ !0);
assert_crc(0x1cf96d7c, !0, b"123456789");
assert_crc(0x58e3fa20, 0, b"123456789");
}
fn assert_crc(ex: u32, seed: u32, input: &[u8]) {
let ac = ext4_style_crc32c_le(seed, input);
if ex != ac {
panic!("CRC didn't match! ex: {:08x}, ac: {:08x}, len: {}", ex, ac, input.len());
}
}
}