diff --git a/.github/workflows/build.yml b/.github/workflows/build.yml index 151cd78..c415ad5 100644 --- a/.github/workflows/build.yml +++ b/.github/workflows/build.yml @@ -30,6 +30,7 @@ jobs: persist-credentials: false - uses: dtolnay/rust-toolchain@b725eda525362e7cf34af9a7243cac1e3f01a7ec # stable with: + toolchain: 1.100.0 components: rustfmt, clippy - uses: Swatinem/rust-cache@e9d7b9f1e587218970671bac74e5511ed9506e6a # v2 with: diff --git a/.github/workflows/release.yml b/.github/workflows/release.yml index 6b0b88b..280f465 100644 --- a/.github/workflows/release.yml +++ b/.github/workflows/release.yml @@ -18,6 +18,8 @@ jobs: with: persist-credentials: false - uses: dtolnay/rust-toolchain@b725eda525362e7cf34af9a7243cac1e3f01a7ec # stable + with: + toolchain: 1.100.0 - name: Validate version tag env: RELEASE_REF: ${{ github.ref }} @@ -59,6 +61,7 @@ jobs: persist-credentials: false - uses: dtolnay/rust-toolchain@b725eda525362e7cf34af9a7243cac1e3f01a7ec # stable with: + toolchain: 1.100.0 targets: ${{ matrix.target }} - name: Linux dependencies if: runner.os == 'Linux' @@ -108,6 +111,8 @@ jobs: with: persist-credentials: false - uses: dtolnay/rust-toolchain@b725eda525362e7cf34af9a7243cac1e3f01a7ec # stable + with: + toolchain: 1.100.0 - name: Install Flatpak tooling and runtime run: | sudo apt-get -o Acquire::Retries=3 update @@ -146,6 +151,8 @@ jobs: path: dist merge-multiple: true - uses: dtolnay/rust-toolchain@b725eda525362e7cf34af9a7243cac1e3f01a7ec # stable + with: + toolchain: 1.100.0 - name: Install signing tool run: cargo install rsign2 --version =0.6.7 --locked - name: Sign every download with the release key diff --git a/.github/workflows/windows-preview.yml b/.github/workflows/windows-preview.yml index 3c44de4..7140352 100644 --- a/.github/workflows/windows-preview.yml +++ b/.github/workflows/windows-preview.yml @@ -12,6 +12,8 @@ jobs: with: persist-credentials: false - uses: dtolnay/rust-toolchain@b725eda525362e7cf34af9a7243cac1e3f01a7ec # stable + with: + toolchain: 1.100.0 - uses: Swatinem/rust-cache@e9d7b9f1e587218970671bac74e5511ed9506e6a # v2 with: workspaces: | diff --git a/docs/DISTRIBUTION.md b/docs/DISTRIBUTION.md index de5b9e2..8a6768f 100644 --- a/docs/DISTRIBUTION.md +++ b/docs/DISTRIBUTION.md @@ -104,7 +104,7 @@ mkdir -p dist mv -f reclaim_*_amd64.snap dist/ ``` -The recipe installs Ubuntu's Rustup package and invokes Rust 1.99.0 explicitly. +The recipe installs Ubuntu's Rustup package and invokes Rust 1.100.0 explicitly, the same version CI pins. Toolchain files are installed only in the build environment; they are not shipped in the application snap. diff --git a/docs/TESTING.md b/docs/TESTING.md index 26ec689..09aeffc 100644 --- a/docs/TESTING.md +++ b/docs/TESTING.md @@ -49,6 +49,9 @@ tagged by CWE: - Debug leftovers, silenced warnings, orphan files, em dashes in user text. - An interface message missing from a catalog, dead catalog keys, mismatched placeholders, or `ui/app.slint` defaults out of step with their translations. +- A test fixture, such as a disk image, holding the local user path of whoever + made it (`/home/`, `/run/media/`, `C:\Users\`), also + inside gzip. - An informal "you" in the German, French or Russian catalog (all three address the user formally), or a Latin term ending in `+` or `#` ("HFS+") in Hebrew or Arabic without the left-to-right mark that keeps the symbol in place. diff --git a/rust-toolchain.toml b/rust-toolchain.toml new file mode 100644 index 0000000..2cdc071 --- /dev/null +++ b/rust-toolchain.toml @@ -0,0 +1,6 @@ +# One Rust version for local builds, CI and packages. CI pins the same +# version in .github/workflows; the Snap recipe pins it in snap/snapcraft.yaml. +# Update all three together, on purpose, after checking the new Clippy lints. +[toolchain] +channel = "1.100.0" +components = ["rustfmt", "clippy"] diff --git a/snap/snapcraft.yaml b/snap/snapcraft.yaml index 321659b..ae7196e 100644 --- a/snap/snapcraft.yaml +++ b/snap/snapcraft.yaml @@ -72,8 +72,8 @@ parts: craftctl default craftctl set version="$(python3 -c 'import tomllib; print(tomllib.load(open("Cargo.toml", "rb"))["package"]["version"])')" override-build: | - /usr/bin/rustup toolchain install 1.99.0 --profile minimal --no-self-update - /usr/bin/rustup run 1.99.0 cargo build --locked --release --bin reclaim + /usr/bin/rustup toolchain install 1.100.0 --profile minimal --no-self-update + /usr/bin/rustup run 1.100.0 cargo build --locked --release --bin reclaim install -Dm755 target/release/reclaim "$CRAFT_PART_INSTALL/bin/reclaim" install -Dm644 snap/gui/reclaim.desktop "$CRAFT_PART_INSTALL/usr/share/applications/reclaim.desktop" install -Dm644 LICENSE "$CRAFT_PART_INSTALL/usr/share/licenses/reclaim/LICENSE" diff --git a/src/allocation/journal.rs b/src/allocation/journal.rs index 729af24..600908c 100644 --- a/src/allocation/journal.rs +++ b/src/allocation/journal.rs @@ -320,6 +320,18 @@ fn tag_size(incompat: u32) -> usize { } } +/// Where the journal's `index`-th logged block really is. The log is a ring +/// from `first_log` to `end`: past `end` it continues at `first_log`. `None` +/// when even the wrapped position falls outside the log. +fn ring_index(index: usize, first_log: usize, end: usize) -> Option { + if index < end { + return Some(index); + } + first_log + .checked_add(index.checked_sub(end)?) + .filter(|at| *at < end) +} + /// Deleted regular files whose inode survives in the journal. pub(super) fn deleted_files( input: &mut (impl Read + Seek), @@ -378,14 +390,9 @@ pub(super) fn deleted_files( return Ok(Vec::new()); } let log_block = |index: usize| -> Option { - let wrapped = if index < end { - index - } else { - first_log - .checked_add(index.checked_sub(end)?) - .filter(|at| *at < end)? - }; - journal_blocks.get(wrapped).copied() + journal_blocks + .get(ring_index(index, first_log, end)?) + .copied() }; let max_copies = bytes::to_usize(MAX_COPY_BYTES.checked_div(volume.inode_size).unwrap_or(0)); // Latest journal copy of each inode that still had a block list, and names. @@ -539,3 +546,20 @@ pub(super) fn deleted_files( } Ok(files) } + +#[cfg(test)] +mod tests { + use super::ring_index; + + #[test] + fn logged_blocks_past_the_end_continue_at_the_start_of_the_log() { + // Block 0 is the journal superblock; the log runs from 1 to 99. + assert_eq!(ring_index(98, 1, 100), Some(98)); + assert_eq!(ring_index(99, 1, 100), Some(99)); + assert_eq!(ring_index(100, 1, 100), Some(1)); + assert_eq!(ring_index(102, 1, 100), Some(3)); + // A wrap that would run past the log again is refused, not looped. + assert_eq!(ring_index(199, 1, 100), None); + assert_eq!(ring_index(usize::MAX, 1, 100), None); + } +} diff --git a/src/recovery_tests.rs b/src/recovery_tests.rs index 6277002..fa9a2ab 100644 --- a/src/recovery_tests.rs +++ b/src/recovery_tests.rs @@ -473,3 +473,53 @@ fn a_second_recovery_cannot_start_while_one_runs() { assert!(app.export_receiver.is_none()); assert_eq!(std::fs::read_dir(directory.path()).unwrap().count(), 0); } + +/// A source that asks recovery to stop while its first block is being read. +struct StopsDuringFirstRead { + data: std::io::Cursor>, + stop: Arc, +} +impl std::io::Read for StopsDuringFirstRead { + fn read(&mut self, buffer: &mut [u8]) -> std::io::Result { + self.stop.store(true, Ordering::Relaxed); + self.data.read(buffer) + } +} +impl std::io::Seek for StopsDuringFirstRead { + fn seek(&mut self, position: std::io::SeekFrom) -> std::io::Result { + self.data.seek(position) + } +} + +#[test] +fn stop_recovery_interrupts_a_large_file_and_leaves_nothing_behind() { + use std::io::{Read, Seek}; + let size = 4 << 20; + let stop = Arc::new(AtomicBool::new(false)); + let mut input = StoppableReader { + inner: StopsDuringFirstRead { + data: std::io::Cursor::new(vec![b'x'; size]), + stop: stop.clone(), + }, + stop: stop.clone(), + }; + let file = Found { + offset: 0, + size: size as u64, + kind: "bin", + selected: true, + validation: reclaim::Validation::Unverified, + original_name: Some("large.bin".into()), + resident_data: None, + ntfs: None, + extents: None, + }; + let directory = tempfile::tempdir().unwrap(); + assert!(reclaim::recover_unique(&mut input, directory.path(), &file, 1).is_err()); + assert!(stop.load(Ordering::Relaxed)); + // Neither the file nor its private temporary file is left in the folder. + assert_eq!(std::fs::read_dir(directory.path()).unwrap().count(), 0); + // The wrapper only refuses reads; seeking is unaffected. + assert_eq!(input.seek(std::io::SeekFrom::Start(0)).unwrap(), 0); + assert!(input.read(&mut [0; 4]).is_err()); +} diff --git a/tests/audit/main.rs b/tests/audit/main.rs index 9d40a6b..8d9a10e 100644 --- a/tests/audit/main.rs +++ b/tests/audit/main.rs @@ -63,6 +63,11 @@ fn no_secrets_in_the_repository() { report(rules::secrets(workspace())); } +#[test] +fn fixtures_carry_no_local_user_paths() { + report(rules::fixture_privacy(workspace())); +} + #[test] fn temporary_files_are_unpredictable() { report(rules::temporary_files(workspace())); diff --git a/tests/audit/rules.rs b/tests/audit/rules.rs index 76c605d..a899ba5 100644 --- a/tests/audit/rules.rs +++ b/tests/audit/rules.rs @@ -11,7 +11,7 @@ const UNSAFE_FILES: &[&str] = &[ "src/drives/windows.rs", // volume and device IOCTLs "src/installer.rs", // ShellExecuteW "src/native_process.rs", // posix_spawn / CreateProcessW - "src/preview.rs", // reopening a validated Windows handle + "src/preview.rs", // reopening a Windows handle, extended volume reads "src/update_runtime.rs", // geteuid for the elevated update helper ]; @@ -642,3 +642,133 @@ fn dedupe(mut findings: Vec) -> Vec { findings.dedup_by(|a, b| a.file == b.file && a.line == b.line && a.rule == b.rule); findings } + +/// Largest fixture file read, and most bytes inflated from a gzip fixture. +const MAX_FIXTURE_BYTES: u64 = 64 << 20; +const MAX_INFLATED_BYTES: u64 = 256 << 20; + +/// Path prefixes that put a local user name into a file: a home directory, a +/// desktop's removable-media mount point (which disk images record as their +/// last mount), and the Windows and macOS profile folders. +const USER_PATH_PREFIXES: &[&[u8]] = &[b"/home/", b"/run/media/", b"/Users/", b"\\Users\\"]; + +/// The user name that follows `prefix` at `at`, if it looks like one. +fn user_after(bytes: &[u8], at: usize, prefix: &[u8]) -> Option { + let rest = bytes.get(at + prefix.len()..)?; + let length = rest + .iter() + .take(64) + .take_while(|b| b.is_ascii_alphanumeric() || matches!(b, b'.' | b'_' | b'-')) + .count(); + let separator = rest.get(length)?; + (length > 0 && matches!(separator, b'/' | b'\\' | 0)) + .then(|| String::from_utf8_lossy(&rest[..length]).into_owned()) +} + +/// Every local user path in `bytes`. +fn user_paths(bytes: &[u8]) -> Vec { + let mut found = Vec::new(); + for prefix in USER_PATH_PREFIXES { + for at in bytes + .windows(prefix.len()) + .enumerate() + .filter(|(_, window)| window == prefix) + .map(|(at, _)| at) + { + if let Some(user) = user_after(bytes, at, prefix) { + let path = format!("{}{user}", String::from_utf8_lossy(prefix)); + if !found.contains(&path) { + found.push(path); + } + } + } + } + found +} + +/// Test fixtures, disk images most of all, must not carry the local user +/// name of whoever made them (CWE-538). Mounting an ext image, for example, +/// records `/run/media//...` in its superblock and journal. +pub fn fixture_privacy(ws: &Workspace) -> Vec { + use std::io::Read; + let mut findings = Vec::new(); + let mut pending = vec![ws.root.join("tests/fixtures")]; + while let Some(dir) = pending.pop() { + let Ok(entries) = std::fs::read_dir(&dir) else { + continue; + }; + for entry in entries.flatten() { + let path = entry.path(); + let Ok(kind) = entry.file_type() else { + continue; + }; + if kind.is_dir() { + pending.push(path); + continue; + } + let relative = path + .strip_prefix(&ws.root) + .unwrap_or(&path) + .to_string_lossy() + .replace('\\', "/"); + let Ok(file) = std::fs::File::open(&path) else { + continue; + }; + let mut bytes = Vec::new(); + if file + .take(MAX_FIXTURE_BYTES + 1) + .read_to_end(&mut bytes) + .is_err() + || bytes.len() as u64 > MAX_FIXTURE_BYTES + { + findings.push(Finding::new( + "PRIV-FIXTURE", + Some(538), + &relative, + 1, + "fixture is unreadable or larger than the audit reads; keep fixtures small", + )); + continue; + } + let mut contents = user_paths(&bytes); + if bytes.starts_with(&[0x1f, 0x8b]) { + let mut inflated = Vec::new(); + if let Err(e) = flate2::read::MultiGzDecoder::new(bytes.as_slice()) + .take(MAX_INFLATED_BYTES) + .read_to_end(&mut inflated) + { + // What did inflate is still searched below. + findings.push(Finding::new( + "PRIV-FIXTURE", + Some(538), + &relative, + 1, + format!("gzip fixture could not be fully checked: {e}"), + )); + } + for path in user_paths(&inflated) { + if !contents.contains(&path) { + contents.push(path); + } + } + } + for path in contents { + findings.push(Finding::new( + "PRIV-FIXTURE", + Some(538), + &relative, + 1, + format!("contains the local user path {path:?}; scrub it before committing"), + )); + } + } + } + findings +} + +#[test] +fn user_paths_are_found_in_raw_bytes() { + let image = b"\0\0/run/media/alex/1234-ABCD\0\0C:\\Users\\Sam\\Desktop\0word/media/x"; + assert_eq!(user_paths(image), vec!["/run/media/alex", "\\Users\\Sam"]); + assert!(user_paths(b"word/media/image1.png /home/ alone").is_empty()); +}