Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .github/workflows/build.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down
7 changes: 7 additions & 0 deletions .github/workflows/release.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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 }}
Expand Down Expand Up @@ -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'
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down
2 changes: 2 additions & 0 deletions .github/workflows/windows-preview.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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: |
Expand Down
2 changes: 1 addition & 1 deletion docs/DISTRIBUTION.md
Original file line number Diff line number Diff line change
Expand Up @@ -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.

Expand Down
3 changes: 3 additions & 0 deletions docs/TESTING.md
Original file line number Diff line number Diff line change
Expand Up @@ -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/<user>`, `/run/media/<user>`, `C:\Users\<user>`), 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.
Expand Down
6 changes: 6 additions & 0 deletions rust-toolchain.toml
Original file line number Diff line number Diff line change
@@ -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"]
4 changes: 2 additions & 2 deletions snap/snapcraft.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down
40 changes: 32 additions & 8 deletions src/allocation/journal.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<usize> {
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),
Expand Down Expand Up @@ -378,14 +390,9 @@ pub(super) fn deleted_files(
return Ok(Vec::new());
}
let log_block = |index: usize| -> Option<u64> {
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.
Expand Down Expand Up @@ -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);
}
}
50 changes: 50 additions & 0 deletions src/recovery_tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<Vec<u8>>,
stop: Arc<AtomicBool>,
}
impl std::io::Read for StopsDuringFirstRead {
fn read(&mut self, buffer: &mut [u8]) -> std::io::Result<usize> {
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<u64> {
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());
}
5 changes: 5 additions & 0 deletions tests/audit/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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()));
Expand Down
132 changes: 131 additions & 1 deletion tests/audit/rules.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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
];

Expand Down Expand Up @@ -642,3 +642,133 @@ fn dedupe(mut findings: Vec<Finding>) -> Vec<Finding> {
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<String> {
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<String> {
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/<user>/...` in its superblock and journal.
pub fn fixture_privacy(ws: &Workspace) -> Vec<Finding> {
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());
}
Loading