fstool 0.4.26

Build disk images and filesystems (ext2/3/4, MBR, GPT) from a directory tree and TOML spec, in the spirit of genext2fs.
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
//! LUKS backend validation against the reference tools.
//!
//! Two independent implementations check us here:
//!
//! - **`cryptsetup`** — the reference. It formats containers we must open,
//!   reads back headers we wrote, and `luksDump --dump-master-key` lets us
//!   compare the master key we recovered against the one it recovers, byte
//!   for byte. All of this works on a plain file without root; only
//!   `luksOpen` needs device-mapper, and we never call it.
//! - **`qemu-io`** — QEMU's own LUKS driver, which reads and writes the
//!   *payload* without root. That closes the loop the header dumps cannot:
//!   plaintext written by one implementation must read back through the
//!   other. (QEMU's LUKS driver is LUKS1-only in the versions shipping
//!   today, so the payload round-trips are LUKS1.)
//!
//! Every test skips silently when its tool isn't on PATH.

#![cfg(feature = "luks")]

use std::path::Path;
use std::process::Command;

use fstool::block::luks::{FormatOpts, LuksBackend, Version, format};
use fstool::block::{BlockDevice, FileBackend};
use tempfile::TempDir;

fn which(tool: &str) -> bool {
    Command::new("sh")
        .arg("-c")
        .arg(format!("command -v {tool}"))
        .output()
        .map(|o| o.status.success())
        .unwrap_or(false)
}

const PASSPHRASE: &str = "correct horse battery staple";

/// `cryptsetup luksFormat` with a deliberately cheap KDF, so the tests
/// stay fast. Returns false when cryptsetup refused (e.g. a cipher the
/// host kernel's crypto API doesn't offer), so the caller can skip.
fn cryptsetup_format(path: &Path, extra: &[&str]) -> bool {
    let mut cmd = Command::new("cryptsetup");
    cmd.arg("luksFormat")
        .arg("-q")
        .arg("--pbkdf")
        .arg("pbkdf2")
        .arg("--pbkdf-force-iterations")
        .arg("1000")
        .args(extra)
        .arg(path)
        .arg("-");
    let out = cmd
        .stdin(std::process::Stdio::piped())
        .stdout(std::process::Stdio::piped())
        .stderr(std::process::Stdio::piped())
        .spawn()
        .and_then(|mut child| {
            use std::io::Write as _;
            child
                .stdin
                .as_mut()
                .expect("stdin piped")
                .write_all(PASSPHRASE.as_bytes())?;
            child.wait_with_output()
        })
        .expect("spawning cryptsetup");
    if !out.status.success() {
        eprintln!(
            "cryptsetup luksFormat {extra:?} failed: {}",
            String::from_utf8_lossy(&out.stderr)
        );
    }
    out.status.success()
}

/// The master key `cryptsetup luksDump --dump-master-key` recovers, as raw
/// bytes. Returns `None` when cryptsetup couldn't open the header.
fn cryptsetup_master_key(path: &Path) -> Option<Vec<u8>> {
    let out = Command::new("cryptsetup")
        .args(["luksDump", "--dump-master-key", "-q"])
        .arg(path)
        .arg("-")
        .stdin(std::process::Stdio::piped())
        .stdout(std::process::Stdio::piped())
        .stderr(std::process::Stdio::piped())
        .spawn()
        .and_then(|mut child| {
            use std::io::Write as _;
            child
                .stdin
                .as_mut()
                .expect("stdin piped")
                .write_all(PASSPHRASE.as_bytes())?;
            child.wait_with_output()
        })
        .ok()?;
    if !out.status.success() {
        eprintln!(
            "cryptsetup luksDump failed: {}",
            String::from_utf8_lossy(&out.stderr)
        );
        return None;
    }
    // The dump prints `MK dump:` followed by indented hex lines; hex runs
    // to the end of the output.
    let text = String::from_utf8_lossy(&out.stdout);
    let after = text.split("MK dump:").nth(1)?;
    let mut key = Vec::new();
    for tok in after.split_whitespace() {
        match u8::from_str_radix(tok, 16) {
            Ok(b) if tok.len() == 2 => key.push(b),
            _ => break,
        }
    }
    (!key.is_empty()).then_some(key)
}

fn cryptsetup_dump(path: &Path) -> String {
    let out = Command::new("cryptsetup")
        .arg("luksDump")
        .arg(path)
        .output()
        .expect("spawning cryptsetup");
    assert!(
        out.status.success(),
        "cryptsetup luksDump rejected our header: {}",
        String::from_utf8_lossy(&out.stderr)
    );
    String::from_utf8_lossy(&out.stdout).into_owned()
}

/// Run `qemu-io` against a LUKS image through QEMU's own crypto driver.
fn qemu_io(path: &Path, cmds: &[&str]) -> std::process::Output {
    let mut cmd = Command::new("qemu-io");
    cmd.arg("--object")
        .arg(format!("secret,id=sec0,data={PASSPHRASE}"))
        .arg("--image-opts")
        .arg(format!(
            "driver=luks,file.filename={},key-secret=sec0",
            path.display()
        ));
    for c in cmds {
        cmd.arg("-c").arg(c);
    }
    cmd.output().expect("spawning qemu-io")
}

/// Create a sparse file of `size` bytes for a tool to format.
fn blank(dir: &TempDir, name: &str, size: u64) -> std::path::PathBuf {
    let path = dir.path().join(name);
    let f = std::fs::File::create(&path).unwrap();
    f.set_len(size).unwrap();
    path
}

// ---------------------------------------------------- cryptsetup → fstool

/// The master key fstool recovers from a cryptsetup-made container must be
/// the one cryptsetup itself recovers.
fn open_cryptsetup_container(name: &str, extra: &[&str]) {
    if !which("cryptsetup") {
        eprintln!("skipping: cryptsetup not installed");
        return;
    }
    let dir = TempDir::new().unwrap();
    let img = blank(&dir, name, 64 * 1024 * 1024);
    if !cryptsetup_format(&img, extra) {
        eprintln!("skipping {name}: this host's cryptsetup refused {extra:?}");
        return;
    }
    let Some(expect) = cryptsetup_master_key(&img) else {
        eprintln!("skipping {name}: cryptsetup would not dump the master key");
        return;
    };

    let vol = LuksBackend::open(FileBackend::open(&img).unwrap(), PASSPHRASE).unwrap();
    assert_eq!(
        vol.master_key().as_bytes(),
        &expect[..],
        "{name}: master key mismatch"
    );
    assert!(vol.total_size() > 0);
    assert!(!vol.header().uuid().is_empty());
}

#[test]
fn opens_cryptsetup_luks1() {
    open_cryptsetup_container("cs-luks1.img", &["--type", "luks1"]);
}

#[test]
fn opens_cryptsetup_luks2() {
    open_cryptsetup_container("cs-luks2.img", &["--type", "luks2"]);
}

/// LUKS1's other historical default: AES-CBC with an ESSIV IV generator.
#[test]
fn opens_cryptsetup_luks1_cbc_essiv() {
    open_cryptsetup_container(
        "cs-luks1-cbc.img",
        &["--type", "luks1", "-c", "aes-cbc-essiv:sha256", "-s", "256"],
    );
}

/// A LUKS2 volume with 4096-byte payload sectors — the sector size has to
/// reach the IV generator, or every sector past the first decrypts wrong.
#[test]
fn opens_cryptsetup_luks2_4k_sectors() {
    open_cryptsetup_container(
        "cs-luks2-4k.img",
        &["--type", "luks2", "--sector-size", "4096"],
    );
}

/// An Argon2id keyslot, with the cost dialled to the floor so the test is
/// quick. This is the KDF cryptsetup actually defaults to.
#[test]
fn opens_cryptsetup_luks2_argon2id() {
    if !which("cryptsetup") {
        eprintln!("skipping: cryptsetup not installed");
        return;
    }
    let dir = TempDir::new().unwrap();
    let img = blank(&dir, "cs-argon2id.img", 64 * 1024 * 1024);
    // Not via cryptsetup_format: that helper forces pbkdf2.
    let ok = Command::new("cryptsetup")
        .args([
            "luksFormat",
            "-q",
            "--type",
            "luks2",
            "--pbkdf",
            "argon2id",
            "--pbkdf-force-iterations",
            "4",
            "--pbkdf-memory",
            "32",
            "--pbkdf-parallel",
            "1",
        ])
        .arg(&img)
        .arg("-")
        .stdin(std::process::Stdio::piped())
        .stdout(std::process::Stdio::piped())
        .stderr(std::process::Stdio::piped())
        .spawn()
        .and_then(|mut child| {
            use std::io::Write as _;
            child
                .stdin
                .as_mut()
                .unwrap()
                .write_all(PASSPHRASE.as_bytes())?;
            child.wait_with_output()
        })
        .expect("spawning cryptsetup");
    if !ok.status.success() {
        eprintln!(
            "skipping: cryptsetup refused argon2id: {}",
            String::from_utf8_lossy(&ok.stderr)
        );
        return;
    }
    let Some(expect) = cryptsetup_master_key(&img) else {
        eprintln!("skipping: cryptsetup would not dump the master key");
        return;
    };
    let vol = LuksBackend::open(FileBackend::open(&img).unwrap(), PASSPHRASE).unwrap();
    assert_eq!(vol.master_key().as_bytes(), &expect[..]);
}

/// A wrong passphrase must be refused, not silently accepted with a
/// garbage key.
#[test]
fn refuses_a_wrong_passphrase_on_a_real_container() {
    if !which("cryptsetup") {
        eprintln!("skipping: cryptsetup not installed");
        return;
    }
    let dir = TempDir::new().unwrap();
    let img = blank(&dir, "cs-wrongpw.img", 64 * 1024 * 1024);
    if !cryptsetup_format(&img, &["--type", "luks2"]) {
        return;
    }
    let err =
        LuksBackend::open(FileBackend::open(&img).unwrap(), "not the passphrase").unwrap_err();
    assert!(matches!(err, fstool::Error::InvalidArgument(_)), "{err}");
}

// ---------------------------------------------------- fstool → cryptsetup

/// A header fstool wrote must satisfy cryptsetup: it has to parse the
/// metadata *and* recover the same master key from our keyslot.
fn cryptsetup_reads_our_header(version: Version, name: &str) {
    if !which("cryptsetup") {
        eprintln!("skipping: cryptsetup not installed");
        return;
    }
    let dir = TempDir::new().unwrap();
    let path = dir.path().join(name);
    let dev = FileBackend::create(&path, 64 * 1024 * 1024).unwrap();
    let opts = FormatOpts {
        version,
        ..FormatOpts::fast_for_tests()
    };
    let vol = format(dev, PASSPHRASE, &opts).unwrap();
    let ours = vol.master_key().as_bytes().to_vec();
    let uuid = vol.header().uuid().to_owned();
    drop(vol);

    let dump = cryptsetup_dump(&path);
    assert!(
        dump.contains(&uuid),
        "cryptsetup did not report our UUID:\n{dump}"
    );
    assert!(
        dump.contains("aes-xts-plain64") || dump.contains("xts-plain64"),
        "cryptsetup did not report our cipher:\n{dump}"
    );

    let theirs = cryptsetup_master_key(&path)
        .unwrap_or_else(|| panic!("cryptsetup could not unlock our {version:?} keyslot"));
    assert_eq!(theirs, ours, "{name}: cryptsetup recovered a different key");
}

#[test]
fn cryptsetup_reads_our_luks1_header() {
    cryptsetup_reads_our_header(Version::V1, "ours-luks1.img");
}

#[test]
fn cryptsetup_reads_our_luks2_header() {
    cryptsetup_reads_our_header(Version::V2, "ours-luks2.img");
}

// ------------------------------------------------- payload interop (qemu)

/// Plaintext written by QEMU's LUKS driver must read back through fstool.
#[test]
fn reads_payload_written_by_qemu_io() {
    if !which("qemu-img") || !which("qemu-io") {
        eprintln!("skipping: qemu-img / qemu-io not installed");
        return;
    }
    let dir = TempDir::new().unwrap();
    let path = dir.path().join("qemu.luks");
    let out = Command::new("qemu-img")
        .arg("create")
        .arg("--object")
        .arg(format!("secret,id=sec0,data={PASSPHRASE}"))
        .args(["-f", "luks", "-o", "key-secret=sec0,iter-time=10"])
        .arg(&path)
        .arg("8M")
        .output()
        .expect("spawning qemu-img");
    if !out.status.success() {
        eprintln!(
            "skipping: qemu-img cannot create LUKS here: {}",
            String::from_utf8_lossy(&out.stderr)
        );
        return;
    }

    let w = qemu_io(
        &path,
        &["write -P 0xab 0 8192", "write -P 0x5c 1048576 4096"],
    );
    assert!(
        w.status.success(),
        "qemu-io write failed: {}",
        String::from_utf8_lossy(&w.stderr)
    );

    let mut vol = LuksBackend::open(FileBackend::open(&path).unwrap(), PASSPHRASE).unwrap();
    let mut buf = vec![0u8; 8192];
    vol.read_at(0, &mut buf).unwrap();
    assert!(buf.iter().all(|&b| b == 0xab), "head pattern mismatch");
    let mut buf = vec![0u8; 4096];
    vol.read_at(1024 * 1024, &mut buf).unwrap();
    assert!(buf.iter().all(|&b| b == 0x5c), "1 MiB pattern mismatch");
}

/// …and the other way: a container fstool formatted and wrote into must
/// read back through QEMU's driver, byte for byte.
#[test]
fn qemu_io_reads_payload_we_wrote() {
    if !which("qemu-io") {
        eprintln!("skipping: qemu-io not installed");
        return;
    }
    let dir = TempDir::new().unwrap();
    let path = dir.path().join("ours-payload.luks");
    let dev = FileBackend::create(&path, 32 * 1024 * 1024).unwrap();
    let opts = FormatOpts {
        // QEMU's LUKS driver reads LUKS1 only.
        version: Version::V1,
        ..FormatOpts::fast_for_tests()
    };
    let mut vol = format(dev, PASSPHRASE, &opts).unwrap();
    vol.write_at(0, &[0x31u8; 8192]).unwrap();
    vol.write_at(2 * 1024 * 1024, b"fstool wrote this").unwrap();
    vol.sync().unwrap();
    drop(vol);

    // 16 bytes exactly: `read -v` dumps 16 per line, so the marker lands
    // in a single hex row and its ASCII column is not split.
    let r = qemu_io(&path, &["read -P 0x31 0 8192", "read -v 2097152 16"]);
    let stdout = String::from_utf8_lossy(&r.stdout);
    assert!(
        r.status.success(),
        "qemu-io read failed: {}\n{stdout}",
        String::from_utf8_lossy(&r.stderr)
    );
    // The ASCII column renders our marker with `.` for the spaces.
    assert!(
        stdout.contains("fstool.wrote.thi"),
        "qemu-io did not see our bytes:\n{stdout}"
    );
}

// ------------------------------------------------------ filesystem on top

/// The point of all this: put a real filesystem inside the encrypted
/// volume, close the whole stack, and read it back through the LUKS
/// layer from the container file.
#[test]
fn hosts_an_ext2_filesystem() {
    use fstool::fs::ext::{Ext, FormatOpts as ExtOpts};
    use fstool::fs::{FileMeta, FileSource, Filesystem};

    let dir = TempDir::new().unwrap();
    let path = dir.path().join("fs.luks");
    let dev = FileBackend::create(&path, 32 * 1024 * 1024).unwrap();
    let mut vol = format(dev, PASSPHRASE, &FormatOpts::fast_for_tests()).unwrap();

    // Size the filesystem to whatever the payload turned out to be.
    let ext_opts = ExtOpts {
        blocks_count: (vol.total_size() / 1024) as u32,
        ..ExtOpts::default()
    };
    let mut fs = Ext::format_with(&mut vol, &ext_opts).unwrap();
    let body = b"encrypted at rest\n";
    fs.create_file(
        &mut vol,
        Path::new("/hello.txt"),
        FileSource::Reader {
            reader: Box::new(std::io::Cursor::new(body.to_vec())),
            len: body.len() as u64,
        },
        FileMeta::default(),
    )
    .unwrap();
    fs.flush(&mut vol).unwrap();
    vol.sync().unwrap();
    drop(fs);
    drop(vol);

    // Reopen the whole stack from the container file.
    let mut vol = LuksBackend::open(FileBackend::open(&path).unwrap(), PASSPHRASE).unwrap();
    let mut fs = Ext::open(&mut vol).unwrap();
    let mut got = Vec::new();
    {
        use std::io::Read as _;
        let mut r = fs.read_file(&mut vol, Path::new("/hello.txt")).unwrap();
        r.read_to_end(&mut got).unwrap();
    }
    assert_eq!(got, body);
}