diff --git a/crates/composefs-integration-tests/src/tests/privileged.rs b/crates/composefs-integration-tests/src/tests/privileged.rs index f4e2d571..f32db520 100644 --- a/crates/composefs-integration-tests/src/tests/privileged.rs +++ b/crates/composefs-integration-tests/src/tests/privileged.rs @@ -335,7 +335,7 @@ impl Drop for FuseOverlayTempDir { /// support `O_TMPFILE`, ingesting object data used to fail outright with /// `EOPNOTSUPP` ("Opening temp file in objects directory"). This is /// exactly the path bootc's composefs digest computation exercises -/// (`ensure_object_from_reader`, via `ensure_object_from_fd`) when reading +/// (`ensure_object_from_fd`) when reading /// a container rootfs through a storage backend without O_TMPFILE support. /// /// Verifies the repository's named-tmpfile fallback lets object ingestion diff --git a/crates/composefs-oci/src/delta.rs b/crates/composefs-oci/src/delta.rs index 9f894902..6838ae8c 100644 --- a/crates/composefs-oci/src/delta.rs +++ b/crates/composefs-oci/src/delta.rs @@ -164,7 +164,7 @@ fn reconstruct_layer( expected_diff_id: &OciDigest, ) -> Result { let tmpfile_fd = repo - .create_object_tmpfile() + .create_scratch_tmpfile() .context("Creating temp file for layer reconstruction")?; let mut tmpfile = File::from(tmpfile_fd); let mut data_source = ComposeFsDataSource { diff --git a/crates/composefs-oci/src/layer.rs b/crates/composefs-oci/src/layer.rs index 1373c498..e21c0823 100644 --- a/crates/composefs-oci/src/layer.rs +++ b/crates/composefs-oci/src/layer.rs @@ -93,21 +93,22 @@ where /// Use this for non-tar blobs (OCI artifacts) where the caller will create /// the splitstream wrapper. /// +/// The blob must be exactly `size` bytes long. +/// /// Returns (object_id, size, store_method) of the stored object. pub async fn store_blob_async( repo: &Repository, mut reader: R, + size: u64, ) -> Result<(ObjectID, u64, ObjectStoreMethod)> where ObjectID: FsVerityHashValue, R: AsyncRead + Unpin, { - let tmpfile = repo.create_object_tmpfile()?; - let mut writer = tokio::fs::File::from(std::fs::File::from(tmpfile)); - let size = tokio::io::copy(&mut reader, &mut writer).await?; + let mut writer = repo.create_object(size)?; + tokio::io::copy(&mut reader, &mut writer).await?; writer.flush().await?; - let tmpfile = writer.into_std().await; - let (object_id, method) = repo.finalize_object_tmpfile(tmpfile, size)?; + let (object_id, method) = writer.finish_async().await?; Ok((object_id, size, method)) } diff --git a/crates/composefs-oci/src/oci_layout.rs b/crates/composefs-oci/src/oci_layout.rs index 659e5dfa..6bf07100 100644 --- a/crates/composefs-oci/src/oci_layout.rs +++ b/crates/composefs-oci/src/oci_layout.rs @@ -490,7 +490,10 @@ async fn import_layer_from_blob( } else { // Non-tar blob: store as object and create splitstream wrapper. // Run the progress driver concurrently with the blob store. - let (store_result, ()) = tokio::join!(store_blob_async(repo, async_file), progress_driver); + let (store_result, ()) = tokio::join!( + store_blob_async(repo, async_file, layer_size), + progress_driver + ); let (object_id, size, method) = store_result?; let mut stats = ImportStats::default(); diff --git a/crates/composefs-oci/src/skopeo.rs b/crates/composefs-oci/src/skopeo.rs index fdcc5beb..f7fb2f22 100644 --- a/crates/composefs-oci/src/skopeo.rs +++ b/crates/composefs-oci/src/skopeo.rs @@ -231,8 +231,10 @@ impl ImageOp { } else { // Non-tar layers (OCI artifacts): stream raw bytes to object store. // Run the progress driver concurrently with the blob store. - let (store_result, ()) = - tokio::join!(store_blob_async(&self.repo, blob_reader), progress_driver); + let (store_result, ()) = tokio::join!( + store_blob_async(&self.repo, blob_reader, descriptor.size()), + progress_driver + ); let (object_id, size, method) = store_result?; driver.await?; @@ -556,7 +558,7 @@ impl oci_delta::DeltaBlobReader for ProxyBlobReader let tmpfile = self .image_op .repo - .create_object_tmpfile() + .create_scratch_tmpfile() .context("Creating temp file for delta blob")?; let copy_fut = async { let mut async_dst = tokio::fs::File::from(std::fs::File::from(tmpfile)); diff --git a/crates/composefs-oci/src/tar.rs b/crates/composefs-oci/src/tar.rs index abb4cb9a..af45006e 100644 --- a/crates/composefs-oci/src/tar.rs +++ b/crates/composefs-oci/src/tar.rs @@ -13,7 +13,6 @@ use std::{ collections::BTreeMap, ffi::{OsStr, OsString}, fmt, - fs::File, os::unix::prelude::{OsStrExt, OsStringExt}, path::PathBuf, sync::Arc, @@ -85,21 +84,16 @@ fn receive_and_finalize_object( ) -> Result<(ObjectID, ObjectStoreMethod)> { use std::io::Write; - // Create tmpfile in the blocking context - let tmpfile_fd = repo.create_object_tmpfile()?; - let mut tmpfile = std::io::BufWriter::with_capacity(IO_BUF_CAPACITY, File::from(tmpfile_fd)); + let mut writer = repo.create_object(size)?; - // Receive chunks and write to tmpfile + // Receive chunks and write them to the object let mut rx = rx; while let Some(chunk) = rx.blocking_recv() { - tmpfile.write_all(&chunk)?; + writer.write_all(&chunk)?; } - // Flush and get the File back - let tmpfile = tmpfile.into_inner()?; - // Finalize: enable verity, get digest, link into objects/ - repo.finalize_object_tmpfile(tmpfile, size) + writer.finish() } /// Stream a large file's content through a channel to a background storage task. diff --git a/crates/composefs/Cargo.toml b/crates/composefs/Cargo.toml index 135e7bb0..d549f91e 100644 --- a/crates/composefs/Cargo.toml +++ b/crates/composefs/Cargo.toml @@ -24,13 +24,14 @@ serde = { version = "1.0", default-features = false, features = ["derive", "allo serde_repr = "0.1" fn-error-context = "0.2" hex = { version = "0.4.0", default-features = false, features = ["std"] } +libc = "0.2" log = { version = "0.4.8", default-features = false } once_cell = { version = "1.21.3", default-features = false, features = ["std"] } -rustix = { version = "1.0.0", default-features = false, features = ["fs", "mount", "process", "std", "thread"] } +rustix = { version = "1.0.0", default-features = false, features = ["event", "fs", "mount", "net", "pipe", "process", "rand", "std", "thread"] } serde_json = { version = "1.0", default-features = false, features = ["std"] } openssl = { version = "0.10", default-features = false } thiserror = { version = "2.0.0", default-features = false } -tokio = { version = "1.24.2", default-features = false, features = ["macros", "process", "io-util", "rt-multi-thread", "sync"] } +tokio = { version = "1.36.0", default-features = false, features = ["macros", "net", "process", "io-util", "rt-multi-thread", "sync"] } tempfile = { version = "3.8.0", optional = true, default-features = false } xxhash-rust = { version = "0.8.2", default-features = false, features = ["xxh32"] } zerocopy = { version = "0.8.0", default-features = false, features = ["derive", "std"] } diff --git a/crates/composefs/src/fs.rs b/crates/composefs/src/fs.rs index 3bf0ac58..1d02d8e5 100644 --- a/crates/composefs/src/fs.rs +++ b/crates/composefs/src/fs.rs @@ -34,13 +34,13 @@ use zerocopy::IntoBytes; use crate::{ INLINE_CONTENT_MAX_V0, - fsverity::{FsVerityHashValue, FsVerityHasher}, + fsverity::{FsVerityHashValue, FsVerityHasher, FsVerityHelper}, generic_tree, generic_tree::OciTransformOptions, repository::Repository, shared_internals::IO_BUF_CAPACITY, tree::{Directory, FileSystem, Inode, Leaf, LeafContent, RegularFile, Stat}, - util::{create_tmpfile_in, proc_self_fd, reopen_tmpfile_ro}, + util::proc_self_fd, }; // --------------------------------------------------------------------------- @@ -86,6 +86,8 @@ pub struct FlatDigestStore { /// Open directory fd for the store root. root: Arc, semaphore: Arc, + /// Creates object files with fs-verity enabled. + helper: FsVerityHelper, /// If true, fall back to userspace hashing when kernel fs-verity is /// unavailable (e.g. tmpfs, overlayfs). Matches `Repository::insecure`. insecure: bool, @@ -117,9 +119,16 @@ impl FlatDigestStore { ) .with_context(|| format!("Failed to open flat digest store: {path:?}"))?; + let helper = FsVerityHelper::builder() + .max_workers(concurrency) + .userspace_digest(insecure) + .start() + .context("Starting fs-verity helper")?; + Ok(Self { root: Arc::new(root), semaphore: Arc::new(Semaphore::new(concurrency)), + helper, insecure, }) } @@ -127,72 +136,47 @@ impl FlatDigestStore { impl ObjectStore for FlatDigestStore { fn ensure_object_from_fd(&self, fd: OwnedFd, size: u64) -> Result { - use crate::fsverity::{EnableVerityError, enable_verity_maybe_copy, measure_verity}; - use std::io::BufRead as _; - - // 1. Create an anonymous O_TMPFILE in the store root. - // No name collision possible; invisible until linked. - let tmpfile_fd = create_tmpfile_in(self.root.as_fd()) - .context("Creating O_TMPFILE in flat digest store")?; - - // 2. Stream from source fd into tmpfile (no in-memory buffering). - let mut src = std::io::BufReader::with_capacity(IO_BUF_CAPACITY, File::from(fd)); - let mut dst = File::from(tmpfile_fd.try_clone().context("Cloning tmpfile fd")?); - let copied = std::io::copy(&mut src, &mut dst).context("Copying object data to tmpfile")?; - ensure!( - copied == size, - "object size mismatch: expected {size}, copied {copied}" - ); - drop(dst); - - // 3. Reopen as read-only (kernel requires no writable fds to enable verity). - let ro_fd = - reopen_tmpfile_ro(File::from(tmpfile_fd)).context("Reopening tmpfile as read-only")?; - - // 4. Enable kernel fs-verity (kernel reads and hashes the file for us). - let (ro_fd, verity_enabled) = - match enable_verity_maybe_copy::(self.root.as_fd(), ro_fd.as_fd()) { - Ok(None) => (ro_fd, true), - Ok(Some(new_fd)) => (new_fd, true), - Err(EnableVerityError::AlreadyEnabled) => (ro_fd, true), - Err(EnableVerityError::FilesystemNotSupported) if self.insecure => (ro_fd, false), - Err(e) => { - return Err(anyhow::anyhow!(e)).context("Enabling verity on object tmpfile"); - } - }; - - // 5. Get the digest — from the kernel (fast) or userspace fallback. - let id: ObjectID = if verity_enabled { - measure_verity(&ro_fd).context("Measuring verity digest after enable")? - } else { - // Insecure fallback: re-read the tmpfile to compute the digest. - let mut reader = std::io::BufReader::with_capacity( - IO_BUF_CAPACITY, - File::from(ro_fd.try_clone().context("Cloning ro_fd for digest")?), - ); - let mut hasher = FsVerityHasher::::new(); - loop { - let buf = reader.fill_buf().context("Reading tmpfile for digest")?; - if buf.is_empty() { - break; - } - let chunk = &buf[..buf.len().min(FsVerityHasher::::BLOCK_SIZE)]; - hasher.add_block(chunk); - let n = chunk.len(); - reader.consume(n); + use crate::fsverity::{FinishError, FinishedFile}; + + // 1. Copy the data into a new file with fs-verity enabled (by + // reflink if possible), via the helper so no writable + // descriptor to it can leak into a forked child. + let result = self + .helper + .create_from_fd::(self.root.as_fd(), &fd, size); + drop(fd); + + // 2. Get the digest: from the kernel, or (insecure mode, without + // fs-verity support) computed in userspace. + let (file, id) = match result { + Ok(file) => { + let id = file + .digest::() + .context("fs-verity digest has the wrong algorithm")?; + (FinishedFile::Verity(file), id) } - hasher.digest() + Err(FinishError::NotSupported(file)) if self.insecure => { + let id = match file.userspace_digest::() { + Some(id) => id, + None => compute_verity_from_fd( + file.as_fd() + .try_clone_to_owned() + .context("Cloning fd for digest")?, + )?, + }; + (FinishedFile::Unverified(file), id) + } + Err(e) => return Err(e).context("Creating object in flat digest store"), }; - // 6. Derive flat path: XX/rest-of-hex (C-compatible layout). + // 3. Derive flat path: XX/rest-of-hex (C-compatible layout). let obj_path = id.to_object_pathname(); let slash = obj_path .find('/') .expect("to_object_pathname always has '/'"); let dir_name = &obj_path[..slash]; - let file_name = &obj_path[slash + 1..]; - // 7. Create XX/ subdirectory if needed. + // 4. Create XX/ subdirectory if needed. match mkdirat(self.root.as_fd(), dir_name, Mode::from_raw_mode(0o755)) { Ok(()) | Err(rustix::io::Errno::EXIST) => {} Err(e) => { @@ -201,31 +185,10 @@ impl ObjectStore for FlatDigestStore { } } - // 8. Open the XX/ subdirectory for use as linkat target. - let subdir = openat( - self.root.as_fd(), - dir_name, - OFlags::RDONLY | OFlags::DIRECTORY | OFlags::CLOEXEC, - Mode::empty(), - ) - .with_context(|| format!("Opening digest store subdirectory {dir_name:?}"))?; - - // 9. Atomically link the tmpfile into its final content-addressed path. - // EEXIST means another writer already stored the same object — fine. - match linkat( - CWD, - proc_self_fd(&ro_fd), - &subdir, - file_name, - AtFlags::SYMLINK_FOLLOW, - ) { - Ok(()) | Err(rustix::io::Errno::EXIST) => {} - Err(e) => { - return Err(e).with_context(|| { - format!("Linking object into flat digest store: {obj_path:?}") - }); - } - } + // 5. Atomically put the file at its final content-addressed path. + // If it already exists, another writer stored the same object. + file.publish(self.root.as_fd(), &obj_path) + .with_context(|| format!("Linking object into flat digest store: {obj_path:?}"))?; Ok(id) } @@ -1057,6 +1020,36 @@ mod tests { Ok(()) } + /// A secure `FlatDigestStore` gives its objects fs-verity. + #[test] + fn test_flat_digest_store_verity() -> Result<()> { + use crate::fsverity::{Sha256HashValue, compute_verity, measure_verity}; + + let td = crate::test::tempdir(); + let store_path = td.path().join("store"); + let store = FlatDigestStore::open(&store_path, 2, false)?; + + let content = b"protected by fs-verity".repeat(100); + let src_path = td.path().join("file"); + std::fs::write(&src_path, &content)?; + let src_fd = openat( + CWD, + &src_path, + OFlags::RDONLY | OFlags::CLOEXEC, + Mode::empty(), + )?; + let id = >::ensure_object_from_fd( + &store, + src_fd, + content.len() as u64, + )?; + assert_eq!(id, compute_verity::(&content)); + + let stored = File::open(store_path.join(id.to_object_pathname()))?; + assert_eq!(measure_verity::(&stored)?, id); + Ok(()) + } + /// Verify that `FlatDigestStore` stores objects in the C-compatible `XX/DIGEST` layout. #[test] fn test_flat_digest_store_layout() -> Result<()> { diff --git a/crates/composefs/src/fsverity/helper.rs b/crates/composefs/src/fsverity/helper.rs new file mode 100644 index 00000000..379b6b24 --- /dev/null +++ b/crates/composefs/src/fsverity/helper.rs @@ -0,0 +1,1929 @@ +//! Create files with fs-verity enabled, safely in multithreaded programs. +//! +//! fs-verity can only be enabled on a file when nobody holds it open for +//! writing. In a multithreaded program that is hard to guarantee: if +//! another thread calls `fork()` while we hold the writable descriptor +//! used to fill the file, the child inherits a copy, and until it calls +//! `exec()` enabling verity fails with `ETXTBSY`. Linux has no +//! `O_CLOFORK` to prevent this. +//! +//! [`FsVerityHelper`] avoids the race entirely. It runs its work on +//! threads with a private file descriptor table (see +//! [`crate::isolation`]), so writable descriptors to the new +//! files never exist in the table that `fork()` copies. Callers feed +//! data in through a pipe ([`VerityWriter`]) or by handing over a source +//! descriptor ([`FsVerityHelper::create_from_fd`]) and get back a +//! read-only descriptor with verity already enabled ([`VerityFile`]). +//! +//! Requests and results travel over Unix sockets, with descriptors +//! passed as `SCM_RIGHTS`, so the kernel translates them between tables. +//! The pipes and sockets on our side can still be inherited by forked +//! children, so nothing here waits for them to be closed: end of data +//! and shutdown are signalled explicitly. +//! +//! Code on the helper threads must not touch process-wide state that +//! holds descriptors (loggers, caches), since those refer to the other +//! table. In particular it does no logging. + +use std::{ + collections::HashMap, + fs::File, + io::{self, IoSlice, IoSliceMut, PipeWriter, Write}, + mem::MaybeUninit, + os::fd::{AsFd, AsRawFd, BorrowedFd, OwnedFd}, + panic::AssertUnwindSafe, + path::Path, + pin::Pin, + sync::{ + Arc, Condvar, Mutex, RwLock, + atomic::{AtomicUsize, Ordering}, + mpsc, + }, + task::{Context, Poll}, + thread::JoinHandle, +}; + +use rustix::{ + event::{PollFd, PollFlags, poll}, + fs::{AtFlags, CWD, Mode, OFlags, RenameFlags, linkat, openat, renameat_with, unlinkat}, + io::Errno, + net::{ + AddressFamily, RecvAncillaryBuffer, RecvAncillaryMessage, RecvFlags, ReturnFlags, + SendAncillaryBuffer, SendAncillaryMessage, SendFlags, Shutdown, SocketFlags, SocketType, + recvmsg, sendmsg, socketpair, + }, + pipe::{PipeFlags, SpliceFlags, fcntl_setpipe_size, pipe_with, splice}, +}; +use thiserror::Error; +use tokio::{io::AsyncWrite, net::unix::pipe::Sender}; + +use zerocopy::IntoBytes as _; + +use super::{ + Algorithm, DEFAULT_LG_BLOCKSIZE, EnableVerityError, FsVerityHashValue, FsVerityHasher, + Sha256HashValue, Sha512HashValue, + ioctl::{fs_ioc_enable_verity_with_algorithm, fs_ioc_measure_verity_with_algorithm}, +}; + +/// The default pipe buffer size on Linux. +const DEFAULT_PIPE_SIZE: u64 = 64 * 1024; +/// Largest pipe buffer we ask for; the default `pipe-max-size` limit. +const MAX_PIPE_SIZE: usize = 1 << 20; +/// Prefix for named temporary files on filesystems without `O_TMPFILE`. +const TMPNAME_PREFIX: &str = ".tmp-verity-"; + +const KIND_PIPE: u8 = 1; +const KIND_FROM_FD: u8 = 2; +const KIND_REFLINK: u8 = 3; +const FLAG_FORCE_NAMED: u8 = 1; +const FLAG_USERSPACE_DIGEST: u8 = 2; +const REQUEST_LEN: usize = 16; + +const COMMIT: u8 = 1; + +const STATUS_OK: u8 = 0; +const STATUS_NOT_SUPPORTED: u8 = 1; +const STATUS_SIZE_MISMATCH: u8 = 2; +const STATUS_ERROR: u8 = 3; +const STATUS_REFLINK_UNSUPPORTED: u8 = 4; +const REPLY_HEADER_LEN: usize = 10; +const REPLY_MAX_LEN: usize = 4096; + +/// Errors from finishing a file in the [`FsVerityHelper`]. +#[derive(Debug, Error)] +pub enum FinishError { + /// The filesystem doesn't support fs-verity. The file was written, + /// but it is *not* protected by fs-verity. + #[error("filesystem does not support fs-verity; the file was written without it")] + NotSupported(UnverifiedFile), + /// The amount of data written didn't match the size given up front. + /// The file was discarded. + #[error("size mismatch: expected {expected} bytes, got {actual}")] + SizeMismatch { + /// The size given when the file was created. + expected: u64, + /// The number of bytes actually received. + actual: u64, + }, + /// The helper failed while creating the file. The file was discarded. + #[error("fs-verity helper: {context}")] + Helper { + /// What the helper was doing. + context: String, + /// The underlying error. + #[source] + source: io::Error, + }, + /// [`FsVerityHelper::reflink_from_fd`] was asked to reflink between + /// filesystems (or on a filesystem) that can't. + #[error("reflinks are not supported here")] + ReflinkUnsupported, + /// Communicating with the helper failed. The file was discarded. + #[error("fs-verity helper: {0}")] + Io(#[from] io::Error), +} + +/// Builder for [`FsVerityHelper`]. +#[derive(Debug, Clone)] +pub struct FsVerityHelperBuilder { + max_workers: usize, + userspace_digest: bool, + force_named_tmpfile: bool, +} + +impl Default for FsVerityHelperBuilder { + fn default() -> Self { + Self { + max_workers: std::thread::available_parallelism() + .map(|n| n.get()) + .unwrap_or(4), + userspace_digest: false, + force_named_tmpfile: false, + } + } +} + +impl FsVerityHelperBuilder { + /// Set the maximum number of worker threads (at least 1). + /// + /// Each file being written occupies one worker from [`create`] until + /// [`finish`] or drop, and [`create_from_fd`] occupies one until it + /// returns. Requests beyond this limit wait for a free worker, and + /// until then nothing reads from their pipes; so a thread must not + /// write to (or create from) one file while holding this many + /// unfinished others, or it deadlocks. + /// + /// [`create`]: FsVerityHelper::create + /// [`create_from_fd`]: FsVerityHelper::create_from_fd + /// [`finish`]: VerityWriter::finish + pub fn max_workers(mut self, n: usize) -> Self { + self.max_workers = n.max(1); + self + } + + /// On filesystems without fs-verity support, compute the digest in + /// userspace instead, while the data is copied, and return it with + /// the [`UnverifiedFile`] (see [`UnverifiedFile::userspace_digest`]). + /// + /// Off by default. When on, the helper finds out once per filesystem + /// whether fs-verity works there (by trying it on an empty file), so + /// files where the kernel computes the digest aren't hashed twice. + pub fn userspace_digest(mut self, enable: bool) -> Self { + self.userspace_digest = enable; + self + } + + /// Always use a named temporary file, as on filesystems without + /// `O_TMPFILE`. For testing. + #[cfg(any(test, feature = "test"))] + #[doc(hidden)] + pub fn force_named_tmpfile(mut self, force: bool) -> Self { + self.force_named_tmpfile = force; + self + } + + /// Start the helper threads. + pub fn start(self) -> io::Result { + let (ours, theirs) = socketpair( + AddressFamily::UNIX, + SocketType::SEQPACKET, + SocketFlags::CLOEXEC, + None, + )?; + // SAFETY: code on the helper threads follows the rules in the + // `# Safety` section of `spawn_isolated`: it does no logging, generates names with + // the getrandom() syscall rather than a library that might cache + // a descriptor, and only uses descriptors it opens itself or + // receives over its sockets. + #[allow(unsafe_code)] + let control = unsafe { + crate::isolation::spawn_isolated( + "fsverity-helper".into(), + vec![theirs], + self.max_workers, + control_main, + ) + }?; + Ok(FsVerityHelper(Arc::new(Inner { + socket: RwLock::new(Some(ours)), + control: Mutex::new(Some(control)), + outstanding: Mutex::new(0), + idle: Condvar::new(), + userspace_digest: self.userspace_digest, + force_named_tmpfile: self.force_named_tmpfile, + }))) + } +} + +/// A handle to the fs-verity helper threads. +/// +/// Cloning the handle is cheap; all clones share the same threads. The +/// threads exit when [`shutdown`](Self::shutdown) is called or the last +/// handle (including those held by [`VerityWriter`]s) is dropped. +/// Dropping the last handle waits for the threads to exit, so avoid +/// doing that on an async runtime thread. +#[derive(Debug, Clone)] +pub struct FsVerityHelper(Arc); + +#[derive(Debug)] +struct Inner { + /// Our end of the request socket; `None` once shut down. + socket: RwLock>, + control: Mutex>>, + /// Number of requests in progress. + outstanding: Mutex, + idle: Condvar, + userspace_digest: bool, + force_named_tmpfile: bool, +} + +impl FsVerityHelper { + /// Return a builder to configure the helper. + pub fn builder() -> FsVerityHelperBuilder { + FsVerityHelperBuilder::default() + } + + /// Start a helper with the default configuration. + pub fn start() -> io::Result { + Self::builder().start() + } + + /// Start writing a new file of `size` bytes in `dirfd`, to be + /// protected by fs-verity with hash type `H`. + pub fn create( + &self, + dirfd: impl AsFd, + size: u64, + ) -> io::Result { + self.create_with_algorithm(dirfd, size, H::ALGORITHM) + } + + /// Like [`create`](Self::create), with the algorithm chosen at runtime. + pub fn create_with_algorithm( + &self, + dirfd: impl AsFd, + size: u64, + algorithm: Algorithm, + ) -> io::Result { + let dir = dirfd.as_fd().try_clone_to_owned()?; + let (pipe_r, pipe_w) = pipe_with(PipeFlags::CLOEXEC)?; + if size > DEFAULT_PIPE_SIZE { + // Best effort: a bigger buffer means fewer context switches. + let want = usize::try_from(size).map_or(MAX_PIPE_SIZE, |s| s.min(MAX_PIPE_SIZE)); + let _ = fcntl_setpipe_size(&pipe_w, want); + } + + let (reply, outstanding) = + self.request(KIND_PIPE, algorithm, size, dir.as_fd(), pipe_r.as_fd())?; + Ok(VerityWriter { + pipe: Some(Pipe::Blocking(PipeWriter::from(pipe_w))), + reply, + dir: Some(dir), + size, + _outstanding: outstanding, + }) + } + + /// Create a new file in `dirfd` with the entire contents of `src` + /// (which must be `size` bytes long), protected by fs-verity with + /// hash type `H`. + /// + /// The data is reflinked if the filesystem supports it, and copied + /// in the kernel otherwise. `src`'s file offset is not used or + /// changed. + pub fn create_from_fd( + &self, + dirfd: impl AsFd, + src: impl AsFd, + size: u64, + ) -> Result { + let dir = dirfd.as_fd().try_clone_to_owned()?; + let (reply, _outstanding) = + self.request(KIND_FROM_FD, H::ALGORITHM, size, dir.as_fd(), src.as_fd())?; + receive_reply(&reply, dir, size) + } + + /// Like [`create_from_fd`](Self::create_from_fd), but only by reflink: + /// fails with [`FinishError::ReflinkUnsupported`] rather than copying. + pub fn reflink_from_fd( + &self, + dirfd: impl AsFd, + src: impl AsFd, + size: u64, + ) -> Result { + let dir = dirfd.as_fd().try_clone_to_owned()?; + let (reply, _outstanding) = + self.request(KIND_REFLINK, H::ALGORITHM, size, dir.as_fd(), src.as_fd())?; + receive_reply(&reply, dir, size) + } + + /// Block until all files being written have been finished or dropped. + /// + /// Calling this while holding a [`VerityWriter`] deadlocks. + pub fn wait(&self) { + let mut n = self.0.outstanding.lock().unwrap(); + while *n > 0 { + n = self.0.idle.wait(n).unwrap(); + } + } + + /// Stop the helper threads and wait for them to exit. + /// + /// Fails if files are still being written; use [`wait`](Self::wait) + /// first. Afterwards, creating files with any handle fails. + /// Calling this again does nothing. + pub fn shutdown(&self) -> io::Result<()> { + { + let mut socket = self.0.socket.write().unwrap(); + let outstanding = *self.0.outstanding.lock().unwrap(); + if outstanding > 0 { + return Err(io::Error::other(format!( + "fs-verity helper still has {outstanding} files in progress" + ))); + } + close_socket(socket.take()); + } + self.0.join() + } + + /// Send a request, returning our end of its reply socket. + fn request( + &self, + kind: u8, + algorithm: Algorithm, + size: u64, + dir: BorrowedFd<'_>, + data: BorrowedFd<'_>, + ) -> io::Result<(OwnedFd, Outstanding)> { + let socket = self.0.socket.read().unwrap(); + let Some(socket) = socket.as_ref() else { + return Err(io::Error::other("fs-verity helper has been shut down")); + }; + // Registered before the request goes out (and while `socket` is + // held) so that shutdown() can't race with it. + let outstanding = Outstanding::new(&self.0); + + let (ours, theirs) = socketpair( + AddressFamily::UNIX, + SocketType::SEQPACKET, + SocketFlags::CLOEXEC, + None, + )?; + + let mut msg = [0u8; REQUEST_LEN]; + msg[0] = kind; + if self.0.force_named_tmpfile { + msg[1] |= FLAG_FORCE_NAMED; + } + if self.0.userspace_digest { + msg[1] |= FLAG_USERSPACE_DIGEST; + } + msg[2] = algorithm.kernel_id(); + msg[3] = algorithm.lg_blocksize(); + msg[4..12].copy_from_slice(&size.to_le_bytes()); + msg[12..16].copy_from_slice(&0o644u32.to_le_bytes()); + + send_with_fds(socket, &msg, &[dir, data, theirs.as_fd()])?; + Ok((ours, outstanding)) + } +} + +/// Signal end-of-stream on a socket even if forked children hold copies +/// of it, then close our copy. +fn close_socket(socket: Option) { + if let Some(socket) = socket { + let _ = rustix::net::shutdown(&socket, Shutdown::Write); + } +} + +impl Inner { + fn join(&self) -> io::Result<()> { + match self.control.lock().unwrap().take() { + Some(handle) => handle + .join() + .map_err(|_| io::Error::other("fs-verity helper thread panicked")), + None => Ok(()), + } + } +} + +impl Drop for Inner { + fn drop(&mut self) { + // Writers hold a reference, so none can be in progress here. + close_socket(self.socket.get_mut().unwrap().take()); + if let Err(e) = self.join() { + log::error!("{e}"); + } + } +} + +/// Counts a request as in progress for as long as it lives. +#[derive(Debug)] +struct Outstanding(Arc); + +impl Outstanding { + fn new(inner: &Arc) -> Self { + *inner.outstanding.lock().unwrap() += 1; + Self(Arc::clone(inner)) + } +} + +impl Drop for Outstanding { + fn drop(&mut self) { + let mut n = self.0.outstanding.lock().unwrap(); + *n -= 1; + if *n == 0 { + self.0.idle.notify_all(); + } + } +} + +#[derive(Debug)] +enum Pipe { + Blocking(PipeWriter), + Async(Sender), +} + +/// Writes the contents of a new file that will be protected by fs-verity. +/// +/// Created by [`FsVerityHelper::create`]. Write exactly the size given +/// there, via [`Write`], [`AsyncWrite`] or [`pipe_mut`](Self::pipe_mut), +/// then call [`finish`](Self::finish). Dropping the writer without +/// finishing discards the file. +/// +/// If the helper fails unexpectedly while data is still being written, +/// writes fail with `EPIPE`; programs that don't ignore `SIGPIPE` (Rust +/// programs do by default) are killed by it instead. +#[derive(Debug)] +pub struct VerityWriter { + pipe: Option, + reply: OwnedFd, + dir: Option, + size: u64, + _outstanding: Outstanding, +} + +impl VerityWriter { + /// Return the write end of the pipe that feeds the file. + /// + /// Writing to it directly lets [`std::io::copy`] move data from a + /// file or socket with `splice()`, without copying it through + /// userspace. + pub fn pipe_mut(&mut self) -> io::Result<&mut PipeWriter> { + if let Some(Pipe::Async(_)) = &self.pipe { + let Some(Pipe::Async(sender)) = self.pipe.take() else { + unreachable!() + }; + self.pipe = Some(Pipe::Blocking(PipeWriter::from(sender.into_blocking_fd()?))); + } + match &mut self.pipe { + Some(Pipe::Blocking(pipe)) => Ok(pipe), + _ => unreachable!("pipe is only taken in finish()"), + } + } + + fn async_pipe(&mut self) -> io::Result<&mut Sender> { + if let Some(Pipe::Blocking(_)) = &self.pipe { + let Some(Pipe::Blocking(pipe)) = self.pipe.take() else { + unreachable!() + }; + // Sets O_NONBLOCK and registers with the current runtime. + self.pipe = Some(Pipe::Async(Sender::from_owned_fd(pipe.into())?)); + } + match &mut self.pipe { + Some(Pipe::Async(sender)) => Ok(sender), + _ => unreachable!("pipe is only taken in finish()"), + } + } + + /// Finish writing: enable fs-verity on the file and return it. + /// + /// This blocks while the kernel reads the file to build the Merkle + /// tree; see [`finish_async`](Self::finish_async). + pub fn finish(mut self) -> Result { + // All writes have completed, so everything is in the pipe. + drop(self.pipe.take()); + send_with_fds(&self.reply, &[COMMIT], &[])?; + let dir = self.dir.take().expect("dir is only taken here"); + receive_reply(&self.reply, dir, self.size) + } + + /// Like [`finish`](Self::finish), but runs on tokio's blocking pool. + pub async fn finish_async(self) -> Result { + tokio::task::spawn_blocking(move || self.finish()) + .await + .map_err(io::Error::other)? + } +} + +impl Drop for VerityWriter { + fn drop(&mut self) { + // Tells the worker to give up (unless we already committed). + let _ = rustix::net::shutdown(&self.reply, Shutdown::Write); + } +} + +impl Write for VerityWriter { + fn write(&mut self, buf: &[u8]) -> io::Result { + self.pipe_mut()?.write(buf) + } + + fn flush(&mut self) -> io::Result<()> { + Ok(()) + } +} + +impl AsyncWrite for VerityWriter { + fn poll_write( + self: Pin<&mut Self>, + cx: &mut Context<'_>, + buf: &[u8], + ) -> Poll> { + let sender = self.get_mut().async_pipe()?; + Pin::new(sender).poll_write(cx, buf) + } + + fn poll_flush(self: Pin<&mut Self>, _cx: &mut Context<'_>) -> Poll> { + Poll::Ready(Ok(())) + } + + fn poll_shutdown(self: Pin<&mut Self>, _cx: &mut Context<'_>) -> Poll> { + Poll::Ready(Ok(())) + } +} + +/// Where a finished file was put by `publish()`. +#[derive(Debug, Clone, Copy, PartialEq, Eq)] +pub enum Published { + /// The file was put in place. + Created, + /// Something already existed at the path; our file was discarded. + AlreadyExists, +} + +/// How the data of a file got there. +#[derive(Debug, Clone, Copy, PartialEq, Eq)] +#[repr(u8)] +pub enum CopyMethod { + /// Written through a [`VerityWriter`]. + Written = 0, + /// Reflinked from the source file. + Reflinked = 1, + /// Copied from the source file. + Copied = 2, +} + +impl CopyMethod { + fn decode(value: u8) -> Option { + match value { + 0 => Some(Self::Written), + 1 => Some(Self::Reflinked), + 2 => Some(Self::Copied), + _ => None, + } + } +} + +/// A finished, read-only file not yet linked at its final path. +#[derive(Debug)] +struct Staged { + fd: OwnedFd, + /// For files created without `O_TMPFILE`: the directory and + /// temporary name, removed on drop. + name: Option<(OwnedFd, String)>, +} + +impl Staged { + fn publish(mut self, dir: impl AsFd, path: &Path) -> io::Result { + let result = match &self.name { + None => linkat( + CWD, + thread_self_fd(&self.fd), + &dir, + path, + AtFlags::SYMLINK_FOLLOW, + ), + Some((tmpdir, name)) => { + renameat_with(tmpdir, name.as_str(), &dir, path, RenameFlags::NOREPLACE) + } + }; + match result { + Ok(()) => { + // A renamed file no longer has its temporary name. + self.name = None; + Ok(Published::Created) + } + Err(Errno::EXIST) => Ok(Published::AlreadyExists), + Err(e) => Err(e.into()), + } + } +} + +impl Drop for Staged { + fn drop(&mut self) { + if let Some((dir, name)) = &self.name { + let _ = unlinkat(dir, name.as_str(), AtFlags::empty()); + } + } +} + +/// A read-only file with fs-verity enabled. +#[derive(Debug)] +pub struct VerityFile { + staged: Staged, + algorithm: Algorithm, + digest: Vec, + method: CopyMethod, +} + +impl VerityFile { + /// The algorithm fs-verity was enabled with. + pub fn algorithm(&self) -> Algorithm { + self.algorithm + } + + /// The fs-verity digest as measured by the kernel, or `None` if `H` + /// doesn't match [`algorithm`](Self::algorithm). + pub fn digest(&self) -> Option { + if self.algorithm != H::ALGORITHM { + return None; + } + H::read_from_bytes(&self.digest).ok() + } + + /// The raw fs-verity digest as measured by the kernel. + pub fn digest_bytes(&self) -> &[u8] { + &self.digest + } + + /// How the data got into the file. + pub fn copy_method(&self) -> CopyMethod { + self.method + } + + /// Atomically put the file at `path` relative to `dir`, unless + /// something already exists there. + pub fn publish(self, dir: impl AsFd, path: impl AsRef) -> io::Result { + self.staged.publish(dir, path.as_ref()) + } +} + +impl AsFd for VerityFile { + fn as_fd(&self) -> BorrowedFd<'_> { + self.staged.fd.as_fd() + } +} + +/// A read-only file written on a filesystem without fs-verity support. +/// +/// It is *not* protected by fs-verity. See [`FinishError::NotSupported`]. +#[derive(Debug)] +pub struct UnverifiedFile { + staged: Staged, + algorithm: Algorithm, + digest: Option>, + method: CopyMethod, +} + +impl UnverifiedFile { + /// The fs-verity digest computed in userspace while the data was + /// copied, if the helper was configured with + /// [`userspace_digest`](FsVerityHelperBuilder::userspace_digest) and + /// `H` matches the requested algorithm. + /// + /// This is *not* enforced by the kernel: nothing stops the file from + /// being changed later. + pub fn userspace_digest(&self) -> Option { + if self.algorithm != H::ALGORITHM { + return None; + } + H::read_from_bytes(self.digest.as_deref()?).ok() + } + + /// How the data got into the file. + pub fn copy_method(&self) -> CopyMethod { + self.method + } + + /// Atomically put the file at `path` relative to `dir`, unless + /// something already exists there. + pub fn publish(self, dir: impl AsFd, path: impl AsRef) -> io::Result { + self.staged.publish(dir, path.as_ref()) + } +} + +impl AsFd for UnverifiedFile { + fn as_fd(&self) -> BorrowedFd<'_> { + self.staged.fd.as_fd() + } +} + +/// Either kind of finished file, for callers that accept both. +#[derive(Debug)] +pub(crate) enum FinishedFile { + Verity(VerityFile), + Unverified(UnverifiedFile), +} + +impl FinishedFile { + pub(crate) fn as_fd(&self) -> BorrowedFd<'_> { + match self { + Self::Verity(file) => file.as_fd(), + Self::Unverified(file) => file.as_fd(), + } + } + + pub(crate) fn copy_method(&self) -> CopyMethod { + match self { + Self::Verity(file) => file.copy_method(), + Self::Unverified(file) => file.copy_method(), + } + } + + pub(crate) fn publish(self, dir: impl AsFd, path: impl AsRef) -> io::Result { + match self { + Self::Verity(file) => file.publish(dir, path), + Self::Unverified(file) => file.publish(dir, path), + } + } +} + +/// The `/proc` path of a descriptor in the calling thread's table. +/// +/// `/proc/self/fd` would be the table of the thread group leader, which +/// is the wrong one on the helper threads. +fn thread_self_fd(fd: impl AsFd) -> String { + format!("/proc/thread-self/fd/{}", fd.as_fd().as_raw_fd()) +} + +fn send_with_fds(socket: impl AsFd, data: &[u8], fds: &[BorrowedFd<'_>]) -> io::Result<()> { + let mut space = [MaybeUninit::uninit(); rustix::cmsg_space!(ScmRights(3))]; + let mut control = SendAncillaryBuffer::new(&mut space); + if !fds.is_empty() && !control.push(SendAncillaryMessage::ScmRights(fds)) { + return Err(io::Error::other("too many descriptors")); + } + // NOSIGNAL: if the other side is gone, report EPIPE instead of + // killing the process with SIGPIPE. + sendmsg( + socket, + &[IoSlice::new(data)], + &mut control, + SendFlags::NOSIGNAL, + )?; + Ok(()) +} + +/// Receive one message; returns the data length (0 means the peer +/// closed the socket) and any descriptors that came with it. +fn recv_with_fds(socket: impl AsFd, buf: &mut [u8]) -> io::Result<(usize, Vec)> { + let mut space = [MaybeUninit::uninit(); rustix::cmsg_space!(ScmRights(3))]; + let mut control = RecvAncillaryBuffer::new(&mut space); + let msg = loop { + match recvmsg( + &socket, + &mut [IoSliceMut::new(buf)], + &mut control, + RecvFlags::CMSG_CLOEXEC, + ) { + Err(Errno::INTR) => continue, + other => break other?, + } + }; + let mut fds = Vec::new(); + for message in control.drain() { + if let RecvAncillaryMessage::ScmRights(received) = message { + fds.extend(received); + } + } + if msg + .flags + .intersects(ReturnFlags::TRUNC | ReturnFlags::CTRUNC) + { + return Err(io::Error::other("truncated message")); + } + Ok((msg.bytes, fds)) +} + +fn receive_reply(reply: &OwnedFd, dir: OwnedFd, expected: u64) -> Result { + let mut buf = vec![0u8; REPLY_MAX_LEN]; + let (len, mut fds) = recv_with_fds(reply, &mut buf)?; + if len == 0 { + return Err(io::Error::other("fs-verity helper exited without replying").into()); + } + let malformed = || io::Error::other("malformed reply from fs-verity helper"); + if len < REPLY_HEADER_LEN { + return Err(malformed().into()); + } + let buf = &buf[..len]; + let status = buf[0]; + let aux = u64::from_le_bytes(buf[1..9].try_into().unwrap()); + let digest_len = usize::from(buf[9]); + let (digest, text) = buf[REPLY_HEADER_LEN..] + .split_at_checked(digest_len) + .ok_or_else(malformed)?; + let text = String::from_utf8_lossy(text).into_owned(); + + if let STATUS_OK | STATUS_NOT_SUPPORTED = status { + // Take charge of the temporary name (if any) before anything + // else can fail, so it gets cleaned up. + let fd = fds.pop().ok_or_else(malformed)?; + let name = (!text.is_empty()).then_some((dir, text)); + let staged = Staged { fd, name }; + let algorithm = decode_algorithm(aux as u8, (aux >> 8) as u8).ok_or_else(malformed)?; + let method = CopyMethod::decode((aux >> 16) as u8).ok_or_else(malformed)?; + if status == STATUS_NOT_SUPPORTED { + return Err(FinishError::NotSupported(UnverifiedFile { + staged, + algorithm, + digest: (!digest.is_empty()).then(|| digest.to_vec()), + method, + })); + } + return Ok(VerityFile { + staged, + algorithm, + digest: digest.to_vec(), + method, + }); + } + + match status { + STATUS_REFLINK_UNSUPPORTED => Err(FinishError::ReflinkUnsupported), + STATUS_SIZE_MISMATCH => Err(FinishError::SizeMismatch { + expected, + actual: aux, + }), + STATUS_ERROR => { + let errno = aux as i32; + let source = if errno != 0 { + io::Error::from_raw_os_error(errno) + } else { + io::Error::other(text.clone()) + }; + Err(FinishError::Helper { + context: text, + source, + }) + } + _ => Err(malformed().into()), + } +} + +fn decode_algorithm(kernel_id: u8, lg_blocksize: u8) -> Option { + match kernel_id { + 1 => Some(Algorithm::Sha256 { lg_blocksize }), + 2 => Some(Algorithm::Sha512 { lg_blocksize }), + _ => None, + } +} + +// --------------------------------------------------------------------------- +// Everything below runs on the isolated helper threads. No logging here: +// see the module documentation. +// --------------------------------------------------------------------------- + +/// A request, as received by the control thread. +struct Job { + kind: u8, + force_named: bool, + userspace_digest: bool, + algorithm: Algorithm, + size: u64, + mode: Mode, + dir: OwnedFd, + data: OwnedFd, + reply: OwnedFd, +} + +fn control_main(fds: Vec, max_workers: usize) { + let [socket]: [OwnedFd; 1] = fds.try_into().expect("one control socket"); + let (tx, rx) = mpsc::channel::(); + let rx = Arc::new(Mutex::new(rx)); + // Jobs handed out and not yet finished. Counted here rather than by + // the workers so that a job still sitting in the queue counts too. + let busy = Arc::new(AtomicUsize::new(0)); + let probes = Arc::new(Probes::default()); + let mut workers: Vec> = Vec::new(); + + loop { + let mut buf = [0u8; 64]; + let (len, fds) = match recv_with_fds(&socket, &mut buf) { + Ok((0, _)) | Err(_) => break, + Ok(msg) => msg, + }; + let job = match decode_job(&buf[..len], fds) { + Ok(job) => job, + Err(Some(reply)) => { + send_error(&reply, "decoding request", &io::Error::other("malformed")); + continue; + } + Err(None) => continue, + }; + + // Threads spawned here share our private descriptor table. + if busy.load(Ordering::SeqCst) >= workers.len() && workers.len() < max_workers { + let rx = Arc::clone(&rx); + let busy = Arc::clone(&busy); + let probes = Arc::clone(&probes); + match std::thread::Builder::new() + .name("fsverity-worker".into()) + .spawn(move || worker_main(&rx, &busy, &probes)) + { + Ok(handle) => workers.push(handle), + Err(e) if workers.is_empty() => { + send_error(&job.reply, "starting fs-verity worker", &e); + continue; + } + // Queue it for the workers we have. + Err(_) => {} + } + } + busy.fetch_add(1, Ordering::SeqCst); + if tx.send(job).is_err() { + break; + } + } + + drop(tx); + for worker in workers { + // Workers catch panics from jobs, so this can't fail. + let _ = worker.join(); + } +} + +/// Decode a request. On failure, returns its reply socket if it has one. +fn decode_job(msg: &[u8], fds: Vec) -> Result> { + let [dir, data, reply]: [OwnedFd; 3] = fds.try_into().map_err(|_| None)?; + let decode = || -> Option<_> { + let msg: &[u8; REQUEST_LEN] = msg.try_into().ok()?; + Some(( + msg[0], + msg[1] & FLAG_FORCE_NAMED != 0, + msg[1] & FLAG_USERSPACE_DIGEST != 0, + decode_algorithm(msg[2], msg[3])?, + u64::from_le_bytes(msg[4..12].try_into().ok()?), + Mode::from_raw_mode(u32::from_le_bytes(msg[12..16].try_into().ok()?)), + )) + }; + match decode() { + Some(( + kind @ (KIND_PIPE | KIND_FROM_FD | KIND_REFLINK), + force_named, + userspace_digest, + algorithm, + size, + mode, + )) => Ok(Job { + kind, + force_named, + userspace_digest, + algorithm, + size, + mode, + dir, + data, + reply, + }), + _ => Err(Some(reply)), + } +} + +fn worker_main(rx: &Mutex>, busy: &AtomicUsize, probes: &Probes) { + loop { + let job = rx.lock().unwrap().recv(); + match job { + Ok(job) => { + // A panic drops the job, and with it the reply socket, so + // the caller gets an error; the worker carries on. + let _ = std::panic::catch_unwind(AssertUnwindSafe(|| run_job(job, probes))); + busy.fetch_sub(1, Ordering::SeqCst); + } + Err(mpsc::RecvError) => break, + } + } +} + +/// The file being built, unlinked on drop if it has a name. +struct Tmpfile<'a> { + file: Option, + dir: &'a OwnedFd, + name: Option, +} + +impl<'a> Tmpfile<'a> { + fn create(dir: &'a OwnedFd, mode: Mode, force_named: bool) -> io::Result { + if !force_named { + match openat( + dir, + ".", + OFlags::RDWR | OFlags::TMPFILE | OFlags::CLOEXEC, + mode, + ) { + Ok(fd) => { + return Ok(Self { + file: Some(fd.into()), + dir, + name: None, + }); + } + Err(Errno::OPNOTSUPP | Errno::ISDIR) => {} + Err(e) => return Err(e.into()), + } + } + for _ in 0..16 { + let name = tmpname()?; + match openat( + dir, + name.as_str(), + OFlags::RDWR | OFlags::CREATE | OFlags::EXCL | OFlags::CLOEXEC, + mode, + ) { + Ok(fd) => { + return Ok(Self { + file: Some(fd.into()), + dir, + name: Some(name), + }); + } + Err(Errno::EXIST) => continue, + Err(e) => return Err(e.into()), + } + } + Err(Errno::EXIST.into()) + } + + fn file(&self) -> &File { + self.file.as_ref().expect("file is open") + } +} + +impl Drop for Tmpfile<'_> { + fn drop(&mut self) { + if let Some(name) = &self.name { + let _ = unlinkat(self.dir, name.as_str(), AtFlags::empty()); + } + } +} + +/// A random temporary name. Uses the getrandom() syscall directly, +/// since random number libraries may keep a descriptor in a static. +fn tmpname() -> io::Result { + let mut bytes = [0u8; 8]; + let mut filled = 0; + while filled < bytes.len() { + match rustix::rand::getrandom(&mut bytes[filled..], rustix::rand::GetRandomFlags::empty()) { + Ok(n) => filled += n, + Err(Errno::INTR) => {} + Err(e) => return Err(e.into()), + } + } + Ok(format!("{TMPNAME_PREFIX}{}", hex::encode(bytes))) +} + +/// Whether fs-verity works on each filesystem seen so far, by `st_dev`. +#[derive(Default)] +struct Probes(Mutex>); + +impl Probes { + /// Find out whether fs-verity is supported for files in `dir`, by + /// trying it on an empty file the first time we see the filesystem. + /// When unsure, says it is. + fn verity_supported(&self, dir: &OwnedFd, algorithm: Algorithm, force_named: bool) -> bool { + let Ok(stat) = rustix::fs::fstat(dir) else { + return true; + }; + let mut known = self.0.lock().unwrap(); + *known + .entry(stat.st_dev) + .or_insert_with(|| probe_verity(dir, algorithm, force_named)) + } +} + +fn probe_verity(dir: &OwnedFd, algorithm: Algorithm, force_named: bool) -> bool { + let Ok(mut tmp) = Tmpfile::create(dir, Mode::from_raw_mode(0o600), force_named) else { + return true; + }; + let Ok(ro) = rustix::fs::open( + thread_self_fd(tmp.file()), + OFlags::RDONLY | OFlags::CLOEXEC, + Mode::empty(), + ) else { + return true; + }; + drop(tmp.file.take()); + !matches!( + fs_ioc_enable_verity_with_algorithm(&ro, algorithm), + Err(EnableVerityError::FilesystemNotSupported) + ) +} + +/// Computes an fs-verity digest in userspace, for a runtime algorithm. +enum Hasher { + Sha256(FsVerityHasher), + Sha512(FsVerityHasher), +} + +impl Hasher { + fn new(algorithm: Algorithm) -> Option { + // FsVerityHasher only does the default block size. + if algorithm.lg_blocksize() != DEFAULT_LG_BLOCKSIZE { + return None; + } + Some(match algorithm { + Algorithm::Sha256 { .. } => Self::Sha256(FsVerityHasher::new()), + Algorithm::Sha512 { .. } => Self::Sha512(FsVerityHasher::new()), + }) + } + + fn update(&mut self, data: &[u8]) { + let result = match self { + Self::Sha256(h) => h.write_all(data), + Self::Sha512(h) => h.write_all(data), + }; + result.expect("hashing doesn't fail"); + } + + fn finish(self) -> Vec { + match self { + Self::Sha256(mut h) => h.digest().as_bytes().to_vec(), + Self::Sha512(mut h) => h.digest().as_bytes().to_vec(), + } + } +} + +fn run_job(job: Job, probes: &Probes) { + let Job { + kind, + force_named, + userspace_digest, + algorithm, + size, + mode, + dir, + data, + reply, + } = job; + + let tmp = Tmpfile::create(&dir, mode, force_named); + let file = tmp.as_ref().ok().map(Tmpfile::file); + // Hash while copying only where the kernel can't do it for us. + let mut hasher = match file { + Some(_) if userspace_digest && !probes.verity_supported(&dir, algorithm, force_named) => { + Hasher::new(algorithm) + } + _ => None, + }; + + let copied = match (kind, file) { + // Even if creating the file failed, the data is consumed so the + // writer doesn't block, and the error is reported on commit. + (KIND_PIPE, _) => match receive_pipe(&data, &reply, file, hasher.as_mut()) { + None => return, + Some((n, None)) => Ok(Some((n, CopyMethod::Written))), + Some((_, Some(e))) => Err(e), + }, + (_, Some(file)) => copy_from_fd(&data, file, kind == KIND_REFLINK, hasher.as_mut()), + (_, None) => Ok(Some((0, CopyMethod::Copied))), + }; + drop(data); + + let mut tmp = match tmp { + Ok(tmp) => tmp, + Err(e) => return send_error(&reply, "creating file", &e), + }; + let (copied, method) = match copied { + Ok(Some(copied)) => copied, + Ok(None) => { + let _ = send_reply(&reply, STATUS_REFLINK_UNSUPPORTED, 0, &[], "", None); + return; + } + Err(e) => return send_error(&reply, "writing file", &e), + }; + if copied != size { + let _ = send_reply(&reply, STATUS_SIZE_MISMATCH, copied, &[], "", None); + return; + } + + let ro = match rustix::fs::open( + thread_self_fd(tmp.file()), + OFlags::RDONLY | OFlags::CLOEXEC, + Mode::empty(), + ) { + Ok(fd) => fd, + Err(e) => return send_error(&reply, "reopening file read-only", &e.into()), + }; + drop(tmp.file.take()); + + let name = tmp.name.as_deref().unwrap_or(""); + let aux = u64::from(algorithm.kernel_id()) + | (u64::from(algorithm.lg_blocksize()) << 8) + | (u64::from(method as u8) << 16); + let sent = match fs_ioc_enable_verity_with_algorithm(&ro, algorithm) { + Ok(()) => match fs_ioc_measure_verity_with_algorithm(&ro, algorithm) { + Ok(digest) => send_reply(&reply, STATUS_OK, aux, &digest, name, Some(ro.as_fd())), + Err(e) => { + return send_error(&reply, "measuring fs-verity", &io::Error::other(e)); + } + }, + Err(EnableVerityError::FilesystemNotSupported) => { + let digest = hasher.map(Hasher::finish).unwrap_or_default(); + send_reply( + &reply, + STATUS_NOT_SUPPORTED, + aux, + &digest, + name, + Some(ro.as_fd()), + ) + } + Err(EnableVerityError::Io(e)) => return send_error(&reply, "enabling fs-verity", &e), + Err(EnableVerityError::FileOpenedForWrite) => { + return send_error(&reply, "enabling fs-verity", &Errno::TXTBSY.into()); + } + Err(e) => return send_error(&reply, "enabling fs-verity", &io::Error::other(e)), + }; + if sent.is_ok() { + // The name now belongs to the caller. + tmp.name = None; + } +} + +/// Copy data from the pipe into `file` until the writer commits. +/// +/// Returns `None` if the writer gave up, otherwise the number of bytes +/// received and the first error writing them. After an error (or with +/// no `file`) the data is read and discarded, so the writer doesn't get +/// stuck. +/// +/// End of data is signalled by the commit message, not by the pipe +/// closing: a forked child may still hold a copy of the write end. +fn receive_pipe( + pipe: &OwnedFd, + reply: &OwnedFd, + file: Option<&File>, + hasher: Option<&mut Hasher>, +) -> Option<(u64, Option)> { + let mut state = PipeState { + total: 0, + error: None, + // splice() keeps the data out of userspace, where we'd hash it. + use_splice: file.is_some() && hasher.is_none(), + hasher, + buf: Vec::new(), + }; + if let Err(e) = rustix::fs::fcntl_setfl(pipe, OFlags::NONBLOCK) { + state.error = Some(e.into()); + } + let mut pipe_open = true; + + loop { + let mut fds = [ + PollFd::new(reply, PollFlags::IN), + PollFd::new(pipe, PollFlags::IN), + ]; + let n = if pipe_open { 2 } else { 1 }; + match poll(&mut fds[..n], None) { + Ok(_) | Err(Errno::INTR) => {} + Err(_) => return None, + } + let reply_ready = !fds[0].revents().is_empty(); + let pipe_ready = pipe_open && !fds[1].revents().is_empty(); + + if pipe_ready && state.pump(pipe, file) { + pipe_open = false; + } + if reply_ready { + let mut msg = [0u8; 1]; + return match recv_with_fds(reply, &mut msg) { + Ok((1, _)) if msg[0] == COMMIT => { + // All writes finished before the commit was sent. + if pipe_open { + state.pump(pipe, file); + } + Some((state.total, state.error)) + } + _ => None, + }; + } + } +} + +struct PipeState<'a> { + total: u64, + error: Option, + use_splice: bool, + hasher: Option<&'a mut Hasher>, + buf: Vec, +} + +impl PipeState<'_> { + /// Move whatever is in the (non-blocking) pipe into `file`. + /// Returns true at end of file. + fn pump(&mut self, pipe: &OwnedFd, file: Option<&File>) -> bool { + loop { + if let (true, None, Some(file)) = (self.use_splice, &self.error, file) { + match splice(pipe, None, file, None, MAX_PIPE_SIZE, SpliceFlags::MOVE) { + Ok(0) => return true, + Ok(n) => self.total += n as u64, + Err(Errno::AGAIN) => return false, + Err(Errno::INTR) => {} + // This filesystem can't splice; nothing was consumed. + Err(Errno::INVAL) => self.use_splice = false, + Err(e) => self.error = Some(e.into()), + } + continue; + } + + if self.buf.is_empty() { + self.buf.resize(128 * 1024, 0); + } + match rustix::io::read(pipe, &mut self.buf) { + Ok(0) => return true, + Ok(n) => { + self.total += n as u64; + if let (Some(mut file), None) = (file, &self.error) { + if let Some(hasher) = self.hasher.as_deref_mut() { + hasher.update(&self.buf[..n]); + } + if let Err(e) = file.write_all(&self.buf[..n]) { + self.error = Some(e); + } + } + } + Err(Errno::AGAIN) => return false, + Err(Errno::INTR) => {} + Err(e) => { + self.error.get_or_insert(e.into()); + return true; + } + } + } + } +} + +/// Copy all of `src` into the (empty) `file`: reflink if possible, then +/// (unless `reflink_only`) `copy_file_range()`, then plain reads and +/// writes. Feeds the data to `hasher`, if any. +/// +/// Returns the size and how it was copied, or `None` if `reflink_only` +/// and the filesystems can't reflink. +fn copy_from_fd( + src: &OwnedFd, + file: &File, + reflink_only: bool, + mut hasher: Option<&mut Hasher>, +) -> io::Result> { + match rustix::fs::ioctl_ficlone(file, src) { + Ok(()) => { + if let Some(hasher) = hasher { + hash_file(file, hasher)?; + } + return Ok(Some((file.metadata()?.len(), CopyMethod::Reflinked))); + } + Err(Errno::OPNOTSUPP | Errno::XDEV) if reflink_only => return Ok(None), + Err(e) if reflink_only => return Err(e.into()), + Err(_) => {} + } + + let mut offset = 0u64; + // copy_file_range() keeps the data out of userspace, where we'd hash it. + while hasher.is_none() { + match rustix::fs::copy_file_range(src, Some(&mut offset), file, None, 1 << 30) { + Ok(0) => return Ok(Some((offset, CopyMethod::Copied))), + Ok(_) => {} + Err(Errno::INTR) => {} + Err(Errno::XDEV | Errno::INVAL | Errno::OPNOTSUPP | Errno::NOSYS) if offset == 0 => { + break; + } + Err(e) => return Err(e.into()), + } + } + + let mut buf = vec![0u8; 128 * 1024]; + let mut writer = file; + loop { + match rustix::io::pread(src, &mut buf, offset) { + Ok(0) => return Ok(Some((offset, CopyMethod::Copied))), + Ok(n) => { + if let Some(hasher) = hasher.as_deref_mut() { + hasher.update(&buf[..n]); + } + writer.write_all(&buf[..n])?; + offset += n as u64; + } + Err(Errno::INTR) => {} + Err(e) => return Err(e.into()), + } + } +} + +fn hash_file(file: &File, hasher: &mut Hasher) -> io::Result<()> { + let mut buf = vec![0u8; 128 * 1024]; + let mut offset = 0u64; + loop { + match rustix::io::pread(file, &mut buf, offset) { + Ok(0) => return Ok(()), + Ok(n) => { + hasher.update(&buf[..n]); + offset += n as u64; + } + Err(Errno::INTR) => {} + Err(e) => return Err(e.into()), + } + } +} + +fn send_reply( + reply: &OwnedFd, + status: u8, + aux: u64, + digest: &[u8], + text: &str, + fd: Option>, +) -> io::Result<()> { + let mut msg = Vec::with_capacity(REPLY_HEADER_LEN + digest.len() + text.len()); + msg.push(status); + msg.extend_from_slice(&aux.to_le_bytes()); + msg.push(u8::try_from(digest.len()).expect("digest fits")); + msg.extend_from_slice(digest); + let mut end = text.len().min(REPLY_MAX_LEN - msg.len()); + while !text.is_char_boundary(end) { + end -= 1; + } + msg.extend_from_slice(&text.as_bytes()[..end]); + let fds: Vec> = fd.into_iter().collect(); + send_with_fds(reply, &msg, &fds) +} + +fn send_error(reply: &OwnedFd, context: &str, error: &io::Error) { + let (errno, text) = match error.raw_os_error() { + Some(errno) => (errno, context.to_owned()), + None => (0, format!("{context}: {error}")), + }; + // If the caller is gone there is nobody left to tell. + let _ = send_reply(reply, STATUS_ERROR, errno as u32 as u64, &[], &text, None); +} + +#[cfg(test)] +mod tests { + use std::{os::unix::process::ExitStatusExt as _, time::Duration}; + + use composefs_ioctls::test_utils_pub::CommandExt as _; + use rustix::fs::{Mode, OFlags, open}; + + use super::*; + use crate::{ + fsverity::{Sha256HashValue, Sha512HashValue, compute_verity, measure_verity}, + test::tempdir, + }; + + fn open_dir(path: &Path) -> OwnedFd { + open( + path, + OFlags::RDONLY | OFlags::DIRECTORY | OFlags::CLOEXEC, + Mode::empty(), + ) + .unwrap() + } + + fn dir_entries(path: &Path) -> Vec { + let mut names: Vec = std::fs::read_dir(path) + .unwrap() + .map(|e| e.unwrap().file_name().into_string().unwrap()) + .collect(); + names.sort(); + names + } + + fn roundtrip(force_named: bool) { + let tmp = tempdir(); + let dir = open_dir(tmp.path()); + let helper = FsVerityHelper::builder() + .force_named_tmpfile(force_named) + .start() + .unwrap(); + let data = b"hello world".repeat(10000); + + let mut w = helper.create::(&dir, data.len() as u64).unwrap(); + w.write_all(&data).unwrap(); + let file = w.finish().unwrap(); + + let expected = compute_verity::(&data); + assert_eq!(file.algorithm(), H::ALGORITHM); + assert_eq!(file.digest::().unwrap(), expected); + assert_eq!(measure_verity::(&file).unwrap(), expected); + if force_named { + assert_eq!(dir_entries(tmp.path()).len(), 1); + } else { + assert!(dir_entries(tmp.path()).is_empty()); + } + + assert_eq!(file.publish(&dir, "obj").unwrap(), Published::Created); + assert_eq!(dir_entries(tmp.path()), ["obj"]); + let published = std::fs::File::open(tmp.path().join("obj")).unwrap(); + assert_eq!(measure_verity::(&published).unwrap(), expected); + assert_eq!(std::fs::read(tmp.path().join("obj")).unwrap(), data); + + // A second identical file finds the first one in place, and its + // temporary name (if any) is cleaned up. + let mut w = helper.create::(&dir, data.len() as u64).unwrap(); + w.write_all(&data).unwrap(); + let file = w.finish().unwrap(); + assert_eq!(file.publish(&dir, "obj").unwrap(), Published::AlreadyExists); + assert_eq!(dir_entries(tmp.path()), ["obj"]); + + helper.shutdown().unwrap(); + } + + #[test] + fn test_roundtrip_sha256() { + roundtrip::(false); + } + + #[test] + fn test_roundtrip_sha512() { + roundtrip::(false); + } + + #[test] + fn test_roundtrip_named() { + roundtrip::(true); + } + + #[test] + fn test_digest_wrong_type() { + let tmp = tempdir(); + let dir = open_dir(tmp.path()); + let helper = FsVerityHelper::start().unwrap(); + let file = helper + .create::(&dir, 0) + .unwrap() + .finish() + .unwrap(); + assert!(file.digest::().is_none()); + assert_eq!( + file.digest::().unwrap(), + compute_verity::(b"") + ); + } + + #[test] + fn test_pipe_mut_copy_from_file() { + let tmp = tempdir(); + let dir = open_dir(tmp.path()); + let helper = FsVerityHelper::start().unwrap(); + let data: Vec = (0..3_000_000u32).map(|i| i as u8).collect(); + let src_path = tmp.path().join("src"); + std::fs::write(&src_path, &data).unwrap(); + + let mut w = helper + .create::(&dir, data.len() as u64) + .unwrap(); + let mut src = std::fs::File::open(&src_path).unwrap(); + std::io::copy(&mut src, w.pipe_mut().unwrap()).unwrap(); + let file = w.finish().unwrap(); + assert_eq!( + file.digest::().unwrap(), + compute_verity::(&data) + ); + } + + #[test] + fn test_size_mismatch() { + let tmp = tempdir(); + let dir = open_dir(tmp.path()); + for force_named in [false, true] { + let helper = FsVerityHelper::builder() + .force_named_tmpfile(force_named) + .start() + .unwrap(); + for (expected, data) in [(5u64, &b"hello world"[..]), (100, b"short")] { + let mut w = helper.create::(&dir, expected).unwrap(); + w.write_all(data).unwrap(); + match w.finish() { + Err(FinishError::SizeMismatch { + expected: e, + actual, + }) => { + assert_eq!(e, expected); + assert_eq!(actual, data.len() as u64); + } + other => panic!("unexpected {other:?}"), + } + } + helper.shutdown().unwrap(); + assert!(dir_entries(tmp.path()).is_empty()); + } + } + + #[test] + fn test_abort() { + let tmp = tempdir(); + let dir = open_dir(tmp.path()); + for force_named in [false, true] { + let helper = FsVerityHelper::builder() + .force_named_tmpfile(force_named) + .start() + .unwrap(); + let mut w = helper.create::(&dir, 100).unwrap(); + w.write_all(b"partial").unwrap(); + drop(w); + helper.wait(); + // The worker cleans up once it notices; shutdown waits for it. + helper.shutdown().unwrap(); + assert!(dir_entries(tmp.path()).is_empty()); + } + } + + #[test] + fn test_create_from_fd() { + let tmp = tempdir(); + let dir = open_dir(tmp.path()); + let helper = FsVerityHelper::start().unwrap(); + let data = b"some data for reflinking".repeat(1000); + let src_path = tmp.path().join("src"); + std::fs::write(&src_path, &data).unwrap(); + let src = std::fs::File::open(&src_path).unwrap(); + + let file = helper + .create_from_fd::(&dir, &src, data.len() as u64) + .unwrap(); + assert_eq!( + file.digest::().unwrap(), + compute_verity::(&data) + ); + + assert!(matches!( + helper.create_from_fd::(&dir, &src, 1), + Err(FinishError::SizeMismatch { expected: 1, .. }) + )); + } + + #[test_with::path(/dev/shm)] + #[test] + fn test_not_supported() { + let tmp = tempfile::TempDir::new_in("/dev/shm").unwrap(); + let dir = open_dir(tmp.path()); + let helper = FsVerityHelper::start().unwrap(); + let mut w = helper.create::(&dir, 5).unwrap(); + w.write_all(b"hello").unwrap(); + let Err(FinishError::NotSupported(file)) = w.finish() else { + panic!("expected NotSupported"); + }; + assert_eq!(file.publish(&dir, "obj").unwrap(), Published::Created); + assert_eq!(std::fs::read(tmp.path().join("obj")).unwrap(), b"hello"); + } + + #[test_with::path(/dev/shm)] + #[test] + fn test_userspace_digest() { + let tmp = tempfile::TempDir::new_in("/dev/shm").unwrap(); + let dir = open_dir(tmp.path()); + let data = b"hash me in userspace".repeat(5000); + let src_path = tmp.path().join("src"); + std::fs::write(&src_path, &data).unwrap(); + let src = std::fs::File::open(&src_path).unwrap(); + + for enabled in [false, true] { + let helper = FsVerityHelper::builder() + .userspace_digest(enabled) + .start() + .unwrap(); + + let mut w = helper + .create::(&dir, data.len() as u64) + .unwrap(); + w.write_all(&data).unwrap(); + let Err(FinishError::NotSupported(file)) = w.finish() else { + panic!("expected NotSupported"); + }; + assert_eq!(file.copy_method(), CopyMethod::Written); + assert!(file.userspace_digest::().is_none()); + assert_eq!( + file.userspace_digest::(), + enabled.then(|| compute_verity::(&data)) + ); + + let Err(FinishError::NotSupported(file)) = + helper.create_from_fd::(&dir, &src, data.len() as u64) + else { + panic!("expected NotSupported"); + }; + assert_eq!( + file.userspace_digest::(), + enabled.then(|| compute_verity::(&data)) + ); + } + } + + /// With verity available, asking for userspace digests changes nothing. + #[test] + fn test_userspace_digest_with_verity() { + let tmp = tempdir(); + let dir = open_dir(tmp.path()); + let helper = FsVerityHelper::builder() + .userspace_digest(true) + .start() + .unwrap(); + for _ in 0..2 { + let mut w = helper.create::(&dir, 5).unwrap(); + w.write_all(b"hello").unwrap(); + let file = w.finish().unwrap(); + assert_eq!(file.copy_method(), CopyMethod::Written); + assert_eq!( + file.digest::().unwrap(), + compute_verity::(b"hello") + ); + } + // The probe leaves nothing behind. + assert!(dir_entries(tmp.path()).is_empty()); + } + + #[test] + fn test_reflink_from_fd() { + let tmp = tempdir(); + let dir = open_dir(tmp.path()); + let helper = FsVerityHelper::start().unwrap(); + let data = b"reflink me".repeat(1000); + let src_path = tmp.path().join("src"); + std::fs::write(&src_path, &data).unwrap(); + let src = std::fs::File::open(&src_path).unwrap(); + + match helper.reflink_from_fd::(&dir, &src, data.len() as u64) { + Ok(file) => { + assert_eq!(file.copy_method(), CopyMethod::Reflinked); + assert_eq!( + file.digest::().unwrap(), + compute_verity::(&data) + ); + } + Err(FinishError::ReflinkUnsupported) => { + let file = helper + .create_from_fd::(&dir, &src, data.len() as u64) + .unwrap(); + assert_eq!(file.copy_method(), CopyMethod::Copied); + } + Err(e) => panic!("{e}"), + } + assert_eq!(dir_entries(tmp.path()), ["src"]); + } + + #[test] + fn test_shutdown() { + let tmp = tempdir(); + let dir = open_dir(tmp.path()); + let helper = FsVerityHelper::start().unwrap(); + let other = helper.clone(); + let w = helper.create::(&dir, 0).unwrap(); + assert!(helper.shutdown().is_err()); + w.finish().unwrap(); + other.wait(); + helper.shutdown().unwrap(); + helper.shutdown().unwrap(); + assert!(other.create::(&dir, 0).is_err()); + } + + #[test] + fn test_drop_without_shutdown() { + let tmp = tempdir(); + let dir = open_dir(tmp.path()); + let helper = FsVerityHelper::start().unwrap(); + let w = helper.create::(&dir, 0).unwrap(); + drop(helper); + // The writer keeps the helper alive. + w.finish().unwrap(); + } + + /// A copy of the pipe's write end held elsewhere (as by a forked + /// child that hasn't exec'd yet) must not keep finish() waiting. + #[test] + fn test_finish_with_pipe_copy() { + let tmp = tempdir(); + let dir = open_dir(tmp.path()); + let helper = FsVerityHelper::start().unwrap(); + let mut w = helper.create::(&dir, 5).unwrap(); + let copy = w.pipe_mut().unwrap().try_clone().unwrap(); + w.write_all(b"hello").unwrap(); + let file = w.finish().unwrap(); + assert_eq!( + file.digest::().unwrap(), + compute_verity::(b"hello") + ); + + // Likewise for aborting. + let mut w = helper.create::(&dir, 5).unwrap(); + let copy2 = w.pipe_mut().unwrap().try_clone().unwrap(); + drop(w); + helper.wait(); + helper.shutdown().unwrap(); + drop((copy, copy2)); + assert!(dir_entries(tmp.path()).is_empty()); + } + + /// More writers in flight at once than there are workers wouldn't + /// work, but up to the limit they must not block each other. + #[test] + fn test_interleaved_writers() { + const N: usize = 4; + let tmp = tempdir(); + let dir = open_dir(tmp.path()); + let helper = FsVerityHelper::builder().max_workers(N).start().unwrap(); + // Several pipe buffers' worth, so each writer blocks if its + // worker isn't draining. + let chunk = vec![7u8; 256 * 1024]; + let rounds = 16; + let size = (chunk.len() * rounds) as u64; + let mut writers: Vec<_> = (0..N) + .map(|_| helper.create::(&dir, size).unwrap()) + .collect(); + for _ in 0..rounds { + for w in &mut writers { + w.write_all(&chunk).unwrap(); + } + } + let expected = compute_verity::(&chunk.repeat(rounds)); + for w in writers { + assert_eq!( + w.finish().unwrap().digest::().unwrap(), + expected + ); + } + } + + #[tokio::test] + async fn test_async_write() { + let tmp = tempdir(); + let dir = open_dir(tmp.path()); + let helper = FsVerityHelper::start().unwrap(); + let data = b"async data".repeat(100_000); + + let mut w = helper + .create::(&dir, data.len() as u64) + .unwrap(); + tokio::io::copy(&mut &data[..10], &mut w).await.unwrap(); + // Switching back to blocking writes works too. + w.write_all(&data[10..20]).unwrap(); + tokio::io::AsyncWriteExt::write_all(&mut w, &data[20..]) + .await + .unwrap(); + let file = w.finish_async().await.unwrap(); + assert_eq!( + file.digest::().unwrap(), + compute_verity::(&data) + ); + } + + /// The race this module exists for: other threads forking while we + /// write. Every file must get fs-verity on the first try. + #[test] + fn test_forking() { + let tmp = tempdir(); + let dir = open_dir(tmp.path()); + let helper = FsVerityHelper::start().unwrap(); + let stop = Arc::new(std::sync::atomic::AtomicBool::new(false)); + + let forkers: Vec<_> = (0..4) + .map(|_| { + let stop = Arc::clone(&stop); + std::thread::spawn(move || { + while !stop.load(Ordering::Relaxed) { + let status = std::process::Command::new("true") + .pre_exec_sleep(Duration::from_millis(5)) + .status() + .unwrap(); + assert!(status.success() || status.signal().is_some()); + } + }) + }) + .collect(); + + let result = std::panic::catch_unwind(|| { + std::thread::scope(|s| { + for _ in 0..4 { + s.spawn(|| { + for i in 0..100u32 { + let data = i.to_le_bytes().repeat(1000); + let mut w = helper + .create::(&dir, data.len() as u64) + .unwrap(); + w.write_all(&data).unwrap(); + let file = w.finish().unwrap(); + assert_eq!( + file.digest::().unwrap(), + compute_verity::(&data) + ); + } + }); + } + }); + }); + + stop.store(true, Ordering::Relaxed); + for f in forkers { + f.join().unwrap(); + } + if let Err(e) = result { + std::panic::resume_unwind(e); + } + } +} diff --git a/crates/composefs/src/fsverity/ioctl.rs b/crates/composefs/src/fsverity/ioctl.rs index a603df72..9629d2a3 100644 --- a/crates/composefs/src/fsverity/ioctl.rs +++ b/crates/composefs/src/fsverity/ioctl.rs @@ -8,7 +8,7 @@ use std::os::fd::AsFd; // Re-export error types from composefs-ioctls pub use composefs_ioctls::fsverity::{EnableVerityError, MeasureVerityError}; -use super::FsVerityHashValue; +use super::{Algorithm, FsVerityHashValue}; /// Enable fsverity on the target file. /// @@ -21,6 +21,43 @@ pub(super) fn fs_ioc_enable_verity( composefs_ioctls::fsverity::fs_ioc_enable_verity(fd.as_fd(), H::ALGORITHM.kernel_id(), 4096) } +/// Enable fsverity on the target file, with the algorithm chosen at runtime. +/// +/// Unlike [`fs_ioc_enable_verity`], this honours the block size carried by +/// `algorithm`. +pub(crate) fn fs_ioc_enable_verity_with_algorithm( + fd: impl AsFd, + algorithm: Algorithm, +) -> Result<(), EnableVerityError> { + composefs_ioctls::fsverity::fs_ioc_enable_verity( + fd.as_fd(), + algorithm.kernel_id(), + 1u32 << algorithm.lg_blocksize(), + ) +} + +/// Measure the fsverity digest of a file, with the algorithm chosen at runtime. +/// +/// Returns the raw digest bytes (32 for SHA-256, 64 for SHA-512). +pub(crate) fn fs_ioc_measure_verity_with_algorithm( + fd: impl AsFd, + algorithm: Algorithm, +) -> Result, MeasureVerityError> { + let kid = algorithm.kernel_id(); + match algorithm { + Algorithm::Sha256 { .. } => { + let digest: [u8; 32] = + composefs_ioctls::fsverity::fs_ioc_measure_verity(fd.as_fd(), kid)?; + Ok(digest.to_vec()) + } + Algorithm::Sha512 { .. } => { + let digest: [u8; 64] = + composefs_ioctls::fsverity::fs_ioc_measure_verity(fd.as_fd(), kid)?; + Ok(digest.to_vec()) + } + } +} + /// Measure the fsverity digest of the provided file descriptor. /// /// Returns the digest as the appropriate FsVerityHashValue type. @@ -89,4 +126,56 @@ mod tests { // Note: This test uses unsafe code via ManuallyDrop + from_raw_fd. // Since we forbid unsafe in this crate, we test bad fd behavior differently. // The composefs-ioctls crate has its own tests for error handling. + + fn rdonly_file_with(data: &[u8]) -> OwnedFd { + use std::io::Write; + + let mut file = tempfile(); + file.write_all(data).unwrap(); + let fd = rustix::fs::open( + crate::util::proc_self_fd(&file), + rustix::fs::OFlags::RDONLY | rustix::fs::OFlags::CLOEXEC, + rustix::fs::Mode::empty(), + ) + .unwrap(); + drop(file); + fd + } + + fn check_runtime_algorithm() { + let data = b"hello world"; + let fd = rdonly_file_with(data); + assert!(matches!( + fs_ioc_measure_verity_with_algorithm(&fd, H::ALGORITHM), + Err(MeasureVerityError::VerityMissing) + )); + // Other tests fork concurrently, which can briefly leave the + // writable descriptor from rdonly_file_with() open in a child. + let mut attempts = 0; + loop { + match fs_ioc_enable_verity_with_algorithm(&fd, H::ALGORITHM) { + Err(EnableVerityError::FileOpenedForWrite) if attempts < 100 => { + attempts += 1; + std::thread::sleep(std::time::Duration::from_millis(10)) + } + other => break other.unwrap(), + } + } + let digest = fs_ioc_measure_verity_with_algorithm(&fd, H::ALGORITHM).unwrap(); + assert_eq!( + digest, + crate::fsverity::compute_verity::(data).as_bytes() + ); + assert_eq!(fs_ioc_measure_verity::(&fd).unwrap().as_bytes(), digest); + } + + #[test] + fn test_runtime_algorithm_sha256() { + check_runtime_algorithm::(); + } + + #[test] + fn test_runtime_algorithm_sha512() { + check_runtime_algorithm::(); + } } diff --git a/crates/composefs/src/fsverity/mod.rs b/crates/composefs/src/fsverity/mod.rs index 9cc26680..891f449f 100644 --- a/crates/composefs/src/fsverity/mod.rs +++ b/crates/composefs/src/fsverity/mod.rs @@ -6,9 +6,15 @@ mod digest; mod hashvalue; +mod helper; mod ioctl; pub use digest::FsVerityHasher; +pub(crate) use helper::FinishedFile; +pub use helper::{ + CopyMethod, FinishError, FsVerityHelper, FsVerityHelperBuilder, Published, UnverifiedFile, + VerityFile, VerityWriter, +}; use std::{ fs::File, @@ -101,6 +107,14 @@ pub fn enable_verity_raw(fd: impl AsFd) -> Result<(), Enab /// quickly under these circumstances, so this function will try to /// enable verity three times, pausing for one millisecond between /// attempts. +/// +/// Deprecated: retrying can't guarantee success. Create files with +/// [`FsVerityHelper`] instead, which never exposes writable descriptors +/// to forked children. +#[deprecated( + since = "0.11.0", + note = "retrying can't guarantee success; create files with FsVerityHelper instead" +)] pub fn enable_verity_with_retry( fd: impl AsFd, ) -> Result<(), EnableVerityError> { @@ -137,6 +151,15 @@ pub fn enable_verity_with_retry( /// # Return Value: /// * `Ok(None)` is returned if verity was enabled on the original file /// * `Ok(Some(OwnedFd))` is returned if a copy was made +/// +/// Deprecated: it is easy to keep using the original file by mistake +/// when a copy is returned. Create files with [`FsVerityHelper`] +/// instead, which never exposes writable descriptors to forked children. +#[deprecated( + since = "0.11.0", + note = "easy to misuse; create files with FsVerityHelper instead" +)] +#[allow(deprecated)] pub fn enable_verity_maybe_copy( dirfd: impl AsFd, fd: BorrowedFd, @@ -154,6 +177,7 @@ pub fn enable_verity_maybe_copy( /// Enable fs-verity on a new copy of `fd`, consuming `fd` and /// returning the new copy. The copy is created via O_TMPFILE /// relative to `dirfd`. +#[allow(deprecated)] fn enable_verity_on_copy( dirfd: impl AsFd, fd: BorrowedFd, @@ -303,6 +327,8 @@ pub fn ensure_verity_equal( } #[cfg(test)] +// Keeps testing the deprecated functions until they're removed. +#[allow(deprecated)] mod tests { use std::{collections::BTreeSet, io::Write, time::Duration}; diff --git a/crates/composefs/src/isolation.rs b/crates/composefs/src/isolation.rs new file mode 100644 index 00000000..c13ae3f4 --- /dev/null +++ b/crates/composefs/src/isolation.rs @@ -0,0 +1,289 @@ +//! Threads with a private file descriptor table. +//! +//! Linux has no `O_CLOFORK`: when one thread calls `fork()`, the child +//! gets a copy of every file descriptor in the process, including +//! writable ones that another thread opened a moment earlier. Until +//! the child calls `exec()` (and `O_CLOEXEC` takes effect), those +//! duplicates keep files open for writing, which for example makes +//! `FS_IOC_ENABLE_VERITY` fail with `ETXTBSY`. +//! +//! [`spawn_isolated`] starts a thread that calls `unshare(CLONE_FILES)` +//! and so gets its own copy of the descriptor table. `fork()` only +//! copies the table of the thread that calls it, so descriptors opened +//! inside the isolated thread (or in threads it spawns, which share its +//! table) can never leak into children forked by other threads. + +#![allow(unsafe_code)] + +use std::io; +use std::os::fd::{AsRawFd, FromRawFd, OwnedFd, RawFd}; +use std::thread::JoinHandle; + +use rustix::thread::UnshareFlags; + +/// Spawn a thread with a private file descriptor table and run `f` in it. +/// +/// `fds` are moved into the new thread's table and handed to `f`; their +/// copies in the shared table are closed before this function returns. +/// All other descriptors the new thread inherited are closed, except +/// for standard input, output and error, so that the thread doesn't +/// keep the rest of the process's files, sockets and pipes open. +/// +/// Descriptors are opaque numbers that only mean something in the +/// table they came from. `f` is a plain function pointer and `arg` +/// must be `Copy`, so neither can carry descriptors from the shared +/// table. Descriptors can still be exchanged safely by passing them +/// over a Unix socket with `SCM_RIGHTS`, where the kernel translates +/// them. +/// +/// Inside the isolated thread, `/proc/self/fd` describes the table of +/// the thread group leader; use `/proc/thread-self/fd` instead. +/// +/// # Safety +/// +/// The compiler can't stop `f` from reaching descriptors through +/// process-wide state. `f`, and every thread it spawns, must not: +/// +/// - use descriptors stored in statics, `&'static` references or +/// library-internal caches, such as a logger's output, the `/proc` +/// handle cached by `rustix::procfs`, or a random number generator's +/// `/dev/urandom` handle. Those were closed here (the standard streams +/// excepted), so their numbers may now refer to unrelated files. In +/// particular, don't log; +/// - store descriptors it opens in such state, where other threads +/// would find them. +/// +/// Standard input, output and error stay open as copies of the +/// process's own, so writing to them (as the default panic hook does) +/// is fine, but they aren't private to the thread. +pub(crate) unsafe fn spawn_isolated( + name: String, + fds: Vec, + arg: A, + f: fn(Vec, A), +) -> io::Result> { + let raw: Vec = fds.iter().map(AsRawFd::as_raw_fd).collect(); + let (tx, rx) = std::sync::mpsc::sync_channel::>(1); + + let handle = std::thread::Builder::new().name(name).spawn(move || { + // SAFETY: after this call the thread has a private copy of the + // descriptor table. The only descriptors it uses afterwards are + // the ones it takes ownership of below and the ones it opens + // itself, which our caller guarantees for `f`. + if let Err(e) = unsafe { rustix::thread::unshare_unsafe(UnshareFlags::FILES) } { + let _ = tx.send(Err(e.into())); + return; + } + + let closed = close_inherited(&raw); + + // SAFETY: the parent keeps `fds` open until we report back, so + // these numbers were valid when the table was copied. In this + // private table nothing else owns them. + let owned: Vec = raw + .iter() + .map(|&fd| unsafe { OwnedFd::from_raw_fd(fd) }) + .collect(); + + if let Err(e) = closed { + let _ = tx.send(Err(e)); + return; + } + let _ = tx.send(Ok(())); + drop(tx); + + f(owned, arg); + })?; + + let result = rx + .recv() + .unwrap_or_else(|_| Err(io::Error::other("isolated thread exited during setup"))); + // These are the copies in the shared table; the thread has its own. + drop(fds); + + match result { + Ok(()) => Ok(handle), + Err(e) => { + let _ = handle.join(); + Err(e) + } + } +} + +/// Close every descriptor in the current (private) table except the +/// standard streams and `keep`. +fn close_inherited(keep: &[RawFd]) -> io::Result<()> { + // Keep 0-2 open: otherwise a later open() could reuse those numbers + // and stray writes to stderr would land in our files. + let mut keep: Vec = [0, 1, 2] + .into_iter() + .chain(keep.iter().map(|&fd| fd as u32)) + .collect(); + keep.sort_unstable(); + keep.dedup(); + + let mut gaps = Vec::new(); + for pair in keep.windows(2) { + if pair[1] > pair[0] + 1 { + gaps.push((pair[0] + 1, pair[1] - 1)); + } + } + gaps.push((keep[keep.len() - 1] + 1, u32::MAX)); + + for &(first, last) in &gaps { + match close_range(first, last) { + Ok(()) => {} + Err(e) if e.raw_os_error() == Some(libc::ENOSYS) => { + return close_inherited_procfs(&keep); + } + Err(e) => return Err(e), + } + } + Ok(()) +} + +// rustix doesn't provide close_range() yet. +fn close_range(first: u32, last: u32) -> io::Result<()> { + // SAFETY: the syscall takes plain integers. Closing descriptors is + // fine because this thread's table is private and nothing in it + // owns them other than the ones we kept. + let r = unsafe { libc::syscall(libc::SYS_close_range, first, last, 0u32) }; + if r < 0 { + Err(io::Error::last_os_error()) + } else { + Ok(()) + } +} + +/// Fallback for kernels without `close_range` (before 5.9). +fn close_inherited_procfs(keep: &[u32]) -> io::Result<()> { + let dir = std::fs::read_dir("/proc/thread-self/fd")?; + let mut fds = Vec::new(); + for entry in dir { + if let Some(fd) = entry? + .file_name() + .to_str() + .and_then(|s| s.parse::().ok()) + { + fds.push(fd); + } + } + // The ReadDir has been dropped, so its own descriptor is gone and + // any number left in `fds` that still exists is an inherited one. + for fd in fds { + if keep.binary_search(&fd).is_err() { + // SAFETY: as in close_range(); EBADF (e.g. for the closed + // directory descriptor) is harmless. + unsafe { libc::close(fd as RawFd) }; + } + } + Ok(()) +} + +#[cfg(test)] +mod tests { + use std::os::fd::AsFd; + + use rustix::fs::{Mode, OFlags}; + + use super::*; + + fn same_file(a: impl AsFd, b: impl AsFd) -> bool { + let a = rustix::fs::fstat(a).unwrap(); + let b = rustix::fs::fstat(b).unwrap(); + (a.st_dev, a.st_ino) == (b.st_dev, b.st_ino) + } + + #[test] + fn fds_are_moved_and_private() { + let file = tempfile::tempfile().unwrap(); + let (tx_sock, rx_sock) = rustix::net::socketpair( + rustix::net::AddressFamily::UNIX, + rustix::net::SocketType::SEQPACKET, + rustix::net::SocketFlags::CLOEXEC, + None, + ) + .unwrap(); + let file_fd = OwnedFd::from(file.try_clone().unwrap()); + let moved_raw = file_fd.as_raw_fd(); + + fn body(fds: Vec, moved_raw: RawFd) { + let [file, sock]: [OwnedFd; 2] = fds.try_into().unwrap(); + assert_eq!(file.as_raw_fd(), moved_raw); + // Something opened here must not show up in the shared table. + let private = + rustix::fs::open("/", OFlags::RDONLY | OFlags::CLOEXEC, Mode::empty()).unwrap(); + let n = private.as_raw_fd().to_ne_bytes(); + rustix::net::send(&sock, &n, rustix::net::SendFlags::empty()).unwrap(); + // Keep `private` open until the parent has checked. + let mut buf = [0u8; 1]; + rustix::net::recv(&sock, &mut buf, rustix::net::RecvFlags::empty()).unwrap(); + drop(file); + } + + // SAFETY: `body` only uses the descriptors it is given. + let handle = unsafe { + spawn_isolated( + "isolated-test".into(), + vec![file_fd, tx_sock], + moved_raw, + body, + ) + } + .unwrap(); + + let mut buf = [0u8; 4]; + rustix::net::recv(&rx_sock, &mut buf, rustix::net::RecvFlags::empty()).unwrap(); + let private_raw = RawFd::from_ne_bytes(buf); + + // Either the number is unused in our table, or it is some other + // file of ours; it is never the directory opened over there. + match rustix::io::fcntl_getfd(unsafe { std::os::fd::BorrowedFd::borrow_raw(private_raw) }) { + Err(rustix::io::Errno::BADF) => {} + Ok(_) => { + let ours = unsafe { std::os::fd::BorrowedFd::borrow_raw(private_raw) }; + let root = + rustix::fs::open("/", OFlags::RDONLY | OFlags::CLOEXEC, Mode::empty()).unwrap(); + assert!(private_raw == root.as_raw_fd() || !same_file(ours, &root)); + } + Err(e) => panic!("{e}"), + } + + // The moved descriptor was closed in our table (the number may + // have been reused by `root` above, but not for `file`). + if rustix::io::fcntl_getfd(unsafe { std::os::fd::BorrowedFd::borrow_raw(moved_raw) }) + .is_ok() + { + let fd = unsafe { std::os::fd::BorrowedFd::borrow_raw(moved_raw) }; + assert!(!same_file(fd, &file)); + } + + rustix::net::send(&rx_sock, &[0], rustix::net::SendFlags::empty()).unwrap(); + handle.join().unwrap(); + } + + #[test] + fn inherited_fds_are_closed() { + let file = tempfile::tempfile().unwrap(); + let raw = file.as_raw_fd(); + + fn body(_fds: Vec, raw: RawFd) { + let fd = unsafe { std::os::fd::BorrowedFd::borrow_raw(raw) }; + assert_eq!( + rustix::io::fcntl_getfd(fd).unwrap_err(), + rustix::io::Errno::BADF + ); + // stderr stays usable + let stderr = unsafe { std::os::fd::BorrowedFd::borrow_raw(2) }; + rustix::io::fcntl_getfd(stderr).unwrap(); + } + + // SAFETY: `body` only checks that `raw` is closed in its table. + unsafe { spawn_isolated("isolated-test".into(), vec![], raw, body) } + .unwrap() + .join() + .unwrap(); + // Still open for us. + rustix::fs::fstat(&file).unwrap(); + } +} diff --git a/crates/composefs/src/lib.rs b/crates/composefs/src/lib.rs index 74611f07..ac43d7a6 100644 --- a/crates/composefs/src/lib.rs +++ b/crates/composefs/src/lib.rs @@ -48,7 +48,10 @@ //! filesystem with a hardlink to a text dumpfile and parsing it back. //! - [`dumpfile_parse::Entry`] demonstrates parsing and serializing one entry. -#![forbid(unsafe_code)] +// Unsafe code is only allowed where it is opted in with +// `#[allow(unsafe_code)]` (module-wide or on a single item) and every +// use is justified. +#![deny(unsafe_code)] // This is a library: emit diagnostics via the `log` crate (or return them), // never by writing to the process's stdout/stderr. Genuinely-intentional // exceptions carry a local `#[allow]` with justification. Test code is exempt. @@ -61,6 +64,7 @@ pub mod erofs; pub mod filesystem_ops; pub mod fs; pub mod fsverity; +pub(crate) mod isolation; pub mod mount; pub mod mountcompat; pub mod progress; diff --git a/crates/composefs/src/repository.rs b/crates/composefs/src/repository.rs index 1f4a580d..af01ff10 100644 --- a/crates/composefs/src/repository.rs +++ b/crates/composefs/src/repository.rs @@ -92,7 +92,7 @@ use std::{ ffi::{CStr, CString, OsStr, OsString}, fmt, fs::{File, canonicalize}, - io::{BufRead, Read, Write}, + io::{Read, Write}, os::{ fd::{AsFd, BorrowedFd, OwnedFd}, unix::ffi::OsStrExt, @@ -110,9 +110,8 @@ use fn_error_context::context; use once_cell::sync::OnceCell; use rustix::{ fs::{ - Access, AtFlags, CWD, Dir, FileType, FlockOperation, Mode, OFlags, RenameFlags, - StatVfsMountFlags, accessat, flock, fstatvfs, linkat, mkdirat, openat, readlinkat, - renameat_with, statat, syncfs, unlinkat, + Access, AtFlags, CWD, Dir, FileType, FlockOperation, Mode, OFlags, StatVfsMountFlags, + accessat, flock, fstatvfs, linkat, mkdirat, openat, readlinkat, statat, syncfs, unlinkat, }, io::{Errno, Result as ErrnoResult}, }; @@ -120,14 +119,15 @@ use rustix::{ use crate::{ erofs::format::{FormatConfig, FormatVersion}, fsverity::{ - Algorithm, CompareVerityError, DEFAULT_LG_BLOCKSIZE, EnableVerityError, FsVerityHashValue, - FsVerityHasher, MeasureVerityError, compute_verity, enable_verity_maybe_copy, + Algorithm, CompareVerityError, CopyMethod, DEFAULT_LG_BLOCKSIZE, EnableVerityError, + FinishError, FinishedFile, FsVerityHashValue, FsVerityHasher, FsVerityHelper, + MeasureVerityError, Published, VerityFile, VerityWriter, compute_verity, enable_verity_raw, ensure_verity_equal, has_verity, measure_verity, measure_verity_opt, }, mount::{MountOptions, VerityRequirement, composefs_fsmount, mount_at}, shared_internals::IO_BUF_CAPACITY, splitstream::{SplitStreamReader, SplitStreamWriter}, - util::{ErrnoFilter, generate_tmpname, proc_self_fd, reopen_tmpfile_ro, replace_symlinkat}, + util::{ErrnoFilter, generate_tmpname, proc_self_fd, replace_symlinkat}, }; /// The filename used for repository metadata. @@ -629,37 +629,6 @@ pub(crate) fn read_repo_metadata(repo_fd: &impl AsFd) -> Result Result> { - let copy = match algorithm { - Algorithm::Sha256 { .. } => { - enable_verity_maybe_copy::(dirfd, fd) - .context("enabling verity (sha256)")? - } - Algorithm::Sha512 { .. } => { - enable_verity_maybe_copy::(dirfd, fd) - .context("enabling verity (sha512)")? - } - }; - if let Some(copy) = © { - // enable_verity_maybe_copy() leaves syncing the copy to the caller. - rustix::fs::fsync(copy).context("syncing verity-enabled copy")?; - } - Ok(copy) -} - /// Remove algorithm-specific data from a repository directory. /// /// Deletes `streams/`, `images/`, and `meta.json` but preserves @@ -707,8 +676,8 @@ fn repo_format_config_from_meta(meta: &RepoMetadata) -> FormatConfig { /// Write `meta.json` into a repository directory fd. /// -/// This atomically writes (via O_TMPFILE + linkat) the metadata file. -/// It will fail if the file already exists. +/// The file is created atomically and only given its name once it is +/// complete. It will fail if the file already exists. /// /// If `enable_verity` is true, fs-verity is enabled on `meta.json` /// before linking it into place. This signals to future @@ -721,34 +690,76 @@ pub(crate) fn write_repo_metadata( enable_verity: bool, ) -> Result<()> { let data = meta.to_json()?; - let verity = enable_verity.then_some(&meta.algorithm); - - // Try O_TMPFILE for atomic creation - match openat( - repo_fd, - ".", - OFlags::WRONLY | OFlags::TMPFILE | OFlags::CLOEXEC, - Mode::from_raw_mode(0o644), - ) { - Ok(fd) => publish_repo_metadata(repo_fd, File::from(fd), None, &data, verity), - // Fallback: no O_TMPFILE support on this filesystem. - Err(Errno::OPNOTSUPP | Errno::NOSYS) => write_repo_metadata_named(repo_fd, &data, verity), - Err(e) => Err(e).context("creating tmpfile for meta.json"), + match enable_verity { + true => write_repo_metadata_verity(repo_fd, &data, meta.algorithm, false), + false => write_repo_metadata_plain(repo_fd, &data, false), } } -/// Fallback for [`write_repo_metadata`] on filesystems that don't -/// support O_TMPFILE. +/// Write `meta.json` with fs-verity enabled, via an [`FsVerityHelper`]. /// -/// The metadata is staged under a temporary name and only given its -/// final name once it is complete, so a failure along the way (notably -/// a failure to enable fs-verity) never leaves behind a `meta.json` -/// that later opens would mistake for a finished one. -fn write_repo_metadata_named( +/// `force_named` skips `O_TMPFILE`, for testing the fallback. +fn write_repo_metadata_verity( repo_fd: &impl AsFd, data: &[u8], - verity: Option<&Algorithm>, + algorithm: Algorithm, + force_named: bool, ) -> Result<()> { + // There's no repository (and so no long-lived helper) yet, and this + // happens once per repository, so a helper just for this is fine. + let builder = FsVerityHelper::builder().max_workers(1); + #[cfg(any(test, feature = "test"))] + let builder = builder.force_named_tmpfile(force_named); + #[cfg(not(any(test, feature = "test")))] + let _ = force_named; + let helper = builder.start().context("starting fs-verity helper")?; + + let mut writer = helper + .create_with_algorithm(repo_fd, data.len() as u64, algorithm) + .context("creating meta.json")?; + writer.write_all(data).context("writing meta.json")?; + let file = writer.finish().context("enabling verity on meta.json")?; + rustix::fs::fsync(&file).context("syncing meta.json")?; + match file + .publish(repo_fd, REPO_METADATA_FILENAME) + .context("linking meta.json into repository")? + { + Published::Created => Ok(()), + Published::AlreadyExists => Err(Errno::EXIST).context("linking meta.json into repository"), + } +} + +/// Write `meta.json` without fs-verity. +/// +/// Uses an anonymous O_TMPFILE, or (if the filesystem doesn't support +/// that, or `force_named` is set) a temporary name, so that a failure +/// along the way never leaves behind a `meta.json` that later opens +/// would mistake for a finished one. +fn write_repo_metadata_plain(repo_fd: &impl AsFd, data: &[u8], force_named: bool) -> Result<()> { + if !force_named { + match openat( + repo_fd, + ".", + OFlags::WRONLY | OFlags::TMPFILE | OFlags::CLOEXEC, + Mode::from_raw_mode(0o644), + ) { + Ok(fd) => { + let file = File::from(fd); + fill_repo_metadata(&file, data)?; + return linkat( + CWD, + proc_self_fd(&file), + repo_fd, + REPO_METADATA_FILENAME, + AtFlags::SYMLINK_FOLLOW, + ) + .context("linking meta.json into repository"); + } + Err(Errno::OPNOTSUPP | Errno::NOSYS) => {} + Err(e) => return Err(e).context("creating tmpfile for meta.json"), + } + } + let name = generate_tmpname(".tmp-meta-"); let fd = openat( repo_fd, @@ -757,7 +768,16 @@ fn write_repo_metadata_named( Mode::from_raw_mode(0o644), ) .context("creating temporary file for meta.json")?; - let result = publish_repo_metadata(repo_fd, File::from(fd), Some(&name), data, verity); + let result = fill_repo_metadata(&File::from(fd), data).and_then(|()| { + linkat( + repo_fd, + &name, + repo_fd, + REPO_METADATA_FILENAME, + AtFlags::empty(), + ) + .context("linking meta.json into repository") + }); // On success meta.json is a second link to the same file; on failure // the staging file is of no further use. if let Err(e) = unlinkat(repo_fd, &name, AtFlags::empty()) { @@ -766,51 +786,10 @@ fn write_repo_metadata_named( result } -/// Write `data` to the staging file `file`, optionally enable fs-verity -/// on it, and link the result into the repository as `meta.json`. -/// -/// `tmpname` is the name of `file` in `repo_fd`, or `None` if it is an -/// anonymous O_TMPFILE. The link fails if `meta.json` already exists. -fn publish_repo_metadata( - repo_fd: &impl AsFd, - mut file: File, - tmpname: Option<&str>, - data: &[u8], - verity: Option<&Algorithm>, -) -> Result<()> { +fn fill_repo_metadata(mut file: &File, data: &[u8]) -> Result<()> { file.write_all(data) .context("writing metadata to tmpfile")?; - file.sync_all().context("syncing metadata tmpfile")?; - - let ro_fd = reopen_tmpfile_ro(file).context("re-opening tmpfile read-only")?; - - let copy = match verity { - Some(algorithm) => enable_verity_for_algorithm(repo_fd, ro_fd.as_fd(), algorithm) - .context("enabling verity on meta.json")?, - None => None, - }; - - match (copy, tmpname) { - // The staging file itself is what we publish, and it has a name. - (None, Some(name)) => linkat( - repo_fd, - name, - repo_fd, - REPO_METADATA_FILENAME, - AtFlags::empty(), - ), - // Otherwise publish by descriptor: either the anonymous staging - // file, or the copy that verity was enabled on. In the latter - // case the staging file has no verity and must not be linked. - (copy, _) => linkat( - CWD, - proc_self_fd(copy.as_ref().unwrap_or(&ro_fd)), - repo_fd, - REPO_METADATA_FILENAME, - AtFlags::SYMLINK_FOLLOW, - ), - } - .context("linking meta.json into repository") + file.sync_all().context("syncing metadata tmpfile") } /// Infer repository metadata by examining existing objects. @@ -934,100 +913,75 @@ pub enum ObjectStoreMethod { AlreadyPresent, } -/// A staging file for object data, created either as an anonymous -/// O_TMPFILE (preferred: no directory entry, no cleanup needed) or, -/// as a fallback for filesystems that don't support O_TMPFILE (e.g. -/// some fuse-overlayfs configurations), a regular file with a -/// temporary random name in the objects directory that must be -/// explicitly renamed into place or unlinked. -pub(crate) enum ObjectTmpfile { - /// An anonymous O_TMPFILE fd with no directory entry. - Anonymous(OwnedFd), - /// A regular file with a temporary name, used when O_TMPFILE isn't - /// supported by the underlying filesystem. - Named { fd: OwnedFd, name: String }, +/// Writes the contents of a new object. +/// +/// Created by [`Repository::create_object`]. Write exactly the size given +/// there, via [`Write`], [`tokio::io::AsyncWrite`] or +/// [`pipe_mut`](Self::pipe_mut), then call [`finish`](Self::finish). +/// Dropping the writer without finishing discards the object. +#[derive(Debug)] +pub struct ObjectWriter<'r, ObjectID: FsVerityHashValue> { + repo: &'r Repository, + writer: VerityWriter, } -impl ObjectTmpfile { - /// Duplicate the underlying fd, e.g. for writing to it while keeping - /// `self` around to track the fallback name (if any). - fn try_clone(&self) -> std::io::Result { - match self { - ObjectTmpfile::Anonymous(fd) => fd.try_clone(), - ObjectTmpfile::Named { fd, .. } => fd.try_clone(), - } +impl ObjectWriter<'_, ObjectID> { + /// Return the write end of the pipe that feeds the object; see + /// [`VerityWriter::pipe_mut`]. + pub fn pipe_mut(&mut self) -> std::io::Result<&mut std::io::PipeWriter> { + self.writer.pipe_mut() } - /// The fallback temporary name, if this is a [`Named`](Self::Named) tmpfile. - fn name(&self) -> Option<&str> { - match self { - ObjectTmpfile::Anonymous(_) => None, - ObjectTmpfile::Named { name, .. } => Some(name), - } - } - - /// Build a [`NamedTmpfileGuard`] tracking this tmpfile's fallback name - /// (a no-op guard for [`Anonymous`](Self::Anonymous) tmpfiles), to - /// protect it from leaking until cleanup responsibility is explicitly - /// handed off (e.g. to [`Repository::link_tmpfile_as_object`]). - fn guard<'a>(&self, objects_dir: &'a OwnedFd) -> NamedTmpfileGuard<'a> { - NamedTmpfileGuard::new(objects_dir, self.name().map(str::to_owned)) + /// Finish writing: enable fs-verity, compute the object ID and link + /// the object into the repository. + /// + /// This blocks while the kernel reads the file to compute the digest. + #[context("Finishing object")] + pub fn finish(self) -> Result<(ObjectID, ObjectStoreMethod)> { + self.repo.publish_object(self.writer.finish(), None) } - /// Consume `self`, returning the underlying file. Callers that need - /// the fallback name after this point should retrieve it via - /// [`name`](Self::name) beforehand. - fn into_file(self) -> File { - match self { - ObjectTmpfile::Anonymous(fd) => File::from(fd), - ObjectTmpfile::Named { fd, .. } => File::from(fd), - } + /// Like [`finish`](Self::finish), but waits for fs-verity to be + /// enabled on tokio's blocking pool. + pub async fn finish_async(self) -> Result<(ObjectID, ObjectStoreMethod)> { + let result = self.writer.finish_async().await; + self.repo + .publish_object(result, None) + .context("Finishing object") } } -impl AsFd for ObjectTmpfile { - fn as_fd(&self) -> BorrowedFd<'_> { - match self { - ObjectTmpfile::Anonymous(fd) => fd.as_fd(), - ObjectTmpfile::Named { fd, .. } => fd.as_fd(), - } +impl Write for ObjectWriter<'_, ObjectID> { + fn write(&mut self, buf: &[u8]) -> std::io::Result { + self.writer.write(buf) } -} - -/// RAII guard that unlinks a fallback (named) object tmpfile on drop, -/// unless disarmed first. -/// -/// Used to avoid leaking the temporary file if an error occurs between -/// its creation and the point where responsibility for it is handed off -/// to [`Repository::link_tmpfile_as_object`], which either renames it -/// into place or unlinks it itself (on dedup). -pub(crate) struct NamedTmpfileGuard<'a> { - dirfd: &'a OwnedFd, - name: Option, -} -impl<'a> NamedTmpfileGuard<'a> { - fn new(dirfd: &'a OwnedFd, name: Option) -> Self { - Self { dirfd, name } + fn flush(&mut self) -> std::io::Result<()> { + self.writer.flush() } +} - /// The fallback temp name being guarded, if any. - fn name(&self) -> Option<&str> { - self.name.as_deref() +impl tokio::io::AsyncWrite for ObjectWriter<'_, ObjectID> { + fn poll_write( + self: std::pin::Pin<&mut Self>, + cx: &mut std::task::Context<'_>, + buf: &[u8], + ) -> std::task::Poll> { + std::pin::Pin::new(&mut self.get_mut().writer).poll_write(cx, buf) } - /// Disarm the guard and return the name, transferring cleanup - /// responsibility to the caller. - fn disarm(mut self) -> Option { - self.name.take() + fn poll_flush( + self: std::pin::Pin<&mut Self>, + cx: &mut std::task::Context<'_>, + ) -> std::task::Poll> { + std::pin::Pin::new(&mut self.get_mut().writer).poll_flush(cx) } -} -impl Drop for NamedTmpfileGuard<'_> { - fn drop(&mut self) { - if let Some(name) = &self.name { - let _ = unlinkat(self.dirfd, name, AtFlags::empty()); - } + fn poll_shutdown( + self: std::pin::Pin<&mut Self>, + cx: &mut std::task::Context<'_>, + ) -> std::task::Poll> { + std::pin::Pin::new(&mut self.get_mut().writer).poll_shutdown(cx) } } @@ -1143,6 +1097,8 @@ pub struct Repository { /// Set via [`set_write_concurrency`](Self::set_write_concurrency) before the semaphore /// is first used; if `None`, defaults to [`available_parallelism`]. write_concurrency: Option, + /// Creates object files with fs-verity enabled; started on first use. + verity_helper: OnceCell, /// Whether fs-verity is required, and whether that was detected from /// `meta.json` or chosen by the caller. Read through /// [`is_insecure`](Self::is_insecure). @@ -1156,10 +1112,10 @@ pub struct Repository { /// written before #[repr(C)] was added to SplitstreamHeader. #[cfg(any(test, feature = "test"))] write_old_splitstream_format: std::sync::atomic::AtomicBool, - /// When true, [`Repository::create_object_tmpfile_impl`] skips the - /// O_TMPFILE attempt and goes straight to the named-tmpfile fallback. - /// Used to test that fallback path without needing a real - /// O_TMPFILE-incapable filesystem. + /// When true, object files skip the O_TMPFILE attempt and go + /// straight to the named-tmpfile fallback. Used to test that + /// fallback path without needing a real O_TMPFILE-incapable + /// filesystem. #[cfg(any(test, feature = "test"))] force_named_tmpfile: std::sync::atomic::AtomicBool, _data: std::marker::PhantomData, @@ -1457,20 +1413,41 @@ impl Repository { .load(std::sync::atomic::Ordering::Relaxed) } - /// Force object tmpfile creation to use the named-tmpfile fallback - /// (as if O_TMPFILE were unsupported), for testing. + /// Force object files to use the named-tmpfile fallback (as if + /// O_TMPFILE were unsupported), for testing. Must be called before + /// the first object is written. #[cfg(any(test, feature = "test"))] pub fn set_force_named_tmpfile(&self, enabled: bool) { self.force_named_tmpfile .store(enabled, std::sync::atomic::Ordering::Relaxed); } - /// Whether object tmpfile creation should skip straight to the - /// named-tmpfile fallback. - #[cfg(any(test, feature = "test"))] - fn force_named_tmpfile(&self) -> bool { - self.force_named_tmpfile - .load(std::sync::atomic::Ordering::Relaxed) + /// The helper that creates object files with fs-verity enabled, + /// started on first use with one worker per allowed concurrent write + /// (see [`set_write_concurrency`](Self::set_write_concurrency)). + fn verity_helper(&self) -> Result<&FsVerityHelper> { + self.verity_helper + .get_or_try_init(|| { + let workers = self + .write_concurrency + .unwrap_or_else(|| available_parallelism().map(|n| n.get()).unwrap_or(4)); + // Insecure repositories name objects by a digest computed + // in userspace where the kernel can't; computing it while + // copying saves reading the file again. The repository + // can be made insecure after the helper is started, so + // always ask: it only costs anything where verity doesn't + // work, and then writing a secure repository fails anyway. + let builder = FsVerityHelper::builder() + .max_workers(workers) + .userspace_digest(true); + #[cfg(any(test, feature = "test"))] + let builder = builder.force_named_tmpfile( + self.force_named_tmpfile + .load(std::sync::atomic::Ordering::Relaxed), + ); + builder.start() + }) + .context("Starting fs-verity helper") } /// Return the objects directory. @@ -1487,8 +1464,12 @@ impl Repository { /// Override the maximum number of concurrent object writes. /// - /// Must be called before the first use of [`write_semaphore`](Self::write_semaphore); - /// has no effect if the semaphore has already been initialized. + /// This also sets the number of worker threads that create object + /// files (see [`create_object`](Self::create_object)). + /// + /// Must be called before the first use of [`write_semaphore`](Self::write_semaphore) + /// and before the first object is written; has no effect on whatever + /// has already been initialized. pub fn set_write_concurrency(&mut self, n: usize) { // Guard: the semaphore is lazily initialized on first use. If it's // already been initialized, this call has no effect. Callers must @@ -1647,6 +1628,7 @@ impl Repository { images: OnceCell::new(), write_semaphore: OnceCell::new(), write_concurrency: None, + verity_helper: OnceCell::new(), integrity: IntegrityPolicy::Detected(VerityState::from_enabled(has_verity)), metadata, erofs_version_override: None, @@ -1848,84 +1830,62 @@ impl Repository { tokio::task::spawn_blocking(move || self_.ensure_object_impl(&data, &writable)).await? } - /// Import an object by streaming from a file descriptor into a - /// tmpfile, without buffering the entire file in memory. + /// Import an object from a file descriptor, reflinking it if the + /// filesystem allows and copying it in the kernel otherwise. /// - /// In insecure mode the verity digest is computed while copying, - /// avoiding a second read pass. + /// `source` must be a regular file of `size` bytes; it is read from + /// the start regardless of its file offset. #[context("Ensuring object from file descriptor")] pub(crate) fn ensure_object_from_fd(&self, source: OwnedFd, size: u64) -> Result { - self.ensure_object_from_reader(File::from(source), size) + let _writable = self.ensure_writable_token()?; + let result = + self.verity_helper()? + .create_from_fd::(self.objects_dir()?, &source, size); + Ok(self.publish_object(result, None)?.0) } /// Ensures that the given data is stored as an object in the repository, /// reading from any source that implements [`Read`]. - /// - /// In insecure mode, the fs-verity digest is computed while copying, - /// avoiding a second read pass. #[context("Ensuring object from reader")] pub fn ensure_object_from_reader(&self, mut source: impl Read, size: u64) -> Result { - let writable = self.ensure_writable_token()?; - let objects_dir = self.objects_dir().context("Getting objects directory")?; - let tmpfile = self.create_object_tmpfile_impl(&writable)?; - // Protects the fallback tmpfile (if any) from leaking if we bail - // out (via `?`) before handing its name off to - // `link_tmpfile_as_object` / `finalize_object_tmpfile_impl`. - let guard = tmpfile.guard(objects_dir); - - if self.is_insecure() { - let mut hasher = FsVerityHasher::::new(); - let mut src = std::io::BufReader::with_capacity(IO_BUF_CAPACITY, &mut source); - let mut dst = File::from(tmpfile.try_clone()?); - - loop { - let buf = src.fill_buf()?; - if buf.is_empty() { - break; - } - hasher.write_all(buf)?; - dst.write_all(buf)?; - let n = buf.len(); - src.consume(n); - } - drop(dst); - - let id = hasher.digest(); - let ro_fd = reopen_tmpfile_ro(tmpfile.into_file()) - .context("Re-opening tmpfile as read-only")?; - let (id, _method) = - self.link_tmpfile_as_object(objects_dir, &ro_fd, &id, size, guard)?; - Ok(id) - } else { - // Secure mode: let std::io::copy use copy_file_range for - // potential reflinks, then finalize_object_tmpfile_impl - // enables kernel verity and measures the digest. - let mut dst = File::from(tmpfile.try_clone()?); - let copied = std::io::copy(&mut source, &mut dst)?; - ensure!(copied == size, "Expected {size} bytes, got {copied}"); - drop(dst); - - let (id, _method) = - self.finalize_object_tmpfile_impl(tmpfile, guard, size, &writable)?; - Ok(id) - } + let mut writer = self.create_object(size)?; + // Copying into the pipe itself lets std::io::copy() splice() + // from files and sockets. + std::io::copy(&mut source, writer.pipe_mut()?).context("Copying object data")?; + Ok(writer.finish()?.0) } - /// Create an O_TMPFILE in the objects directory for streaming writes. + /// Start writing a new object of `size` bytes. /// - /// Returns the file descriptor for writing. The caller should write data to this fd, - /// then call [`finalize_object_tmpfile`](Self::finalize_object_tmpfile) to compute - /// the verity digest, enable fs-verity, and link the file into the objects directory. + /// Write the data to the returned [`ObjectWriter`], then call + /// [`ObjectWriter::finish`] to enable fs-verity, compute the object + /// ID and link the object into the repository. /// - /// Unlike the crate-internal streaming ingestion path, this public API - /// always uses O_TMPFILE with no fallback: callers (e.g. `cfsctl`) are - /// expected to hold a real anonymous tmpfile fd. - #[context("Creating object tmpfile")] - pub fn create_object_tmpfile(&self) -> Result { + /// Each unfinished writer occupies one of the repository's + /// [write slots](Self::set_write_concurrency), and data for a writer + /// beyond that limit isn't read until a slot frees up. So a thread + /// must not write to one object while holding that many unfinished + /// others, or it deadlocks; [`write_semaphore`](Self::write_semaphore) + /// helps avoid that. + #[context("Creating object")] + pub fn create_object(&self, size: u64) -> Result> { let _writable = self.ensure_writable_token()?; - let objects_dir = self - .objects_dir() - .context("Getting objects directory for tmpfile creation")?; + let writer = self + .verity_helper()? + .create::(self.objects_dir()?, size)?; + Ok(ObjectWriter { repo: self, writer }) + } + + /// Create an anonymous read-write temporary file in the repository's + /// filesystem, for scratch data that won't become an object. + /// + /// Use [`create_object`](Self::create_object) for objects instead: + /// fs-verity can't be enabled on a file that had a writable + /// descriptor like this one, which forked children may inherit. + #[context("Creating scratch tmpfile")] + pub fn create_scratch_tmpfile(&self) -> Result { + let _writable = self.ensure_writable_token()?; + let objects_dir = self.objects_dir()?; let fd = openat( objects_dir, ".", @@ -1936,64 +1896,6 @@ impl Repository { Ok(fd) } - /// Create a staging file for object data in the objects directory. - /// - /// Prefers an anonymous O_TMPFILE (no directory entry, no cleanup - /// needed if the caller bails out). Falls back to a regular file - /// with a random temporary name when the underlying filesystem - /// doesn't support O_TMPFILE (`EOPNOTSUPP`), e.g. some fuse-overlayfs - /// configurations used in CI (see bootc-dev/bootc#2340). - #[context("Creating object tmpfile")] - pub(crate) fn create_object_tmpfile_impl( - &self, - _writable: &WritableRepo, - ) -> Result { - let objects_dir = self - .objects_dir() - .context("Getting objects directory for tmpfile creation")?; - - #[cfg(any(test, feature = "test"))] - if self.force_named_tmpfile() { - return self.create_named_object_tmpfile(objects_dir); - } - - match openat( - objects_dir, - ".", - OFlags::RDWR | OFlags::TMPFILE | OFlags::CLOEXEC, - Mode::from_raw_mode(0o644), - ) { - Ok(fd) => Ok(ObjectTmpfile::Anonymous(fd)), - Err(Errno::OPNOTSUPP) => self.create_named_object_tmpfile(objects_dir), - Err(e) => Err(e).context("Opening temp file in objects directory")?, - } - } - - /// Fallback for [`create_object_tmpfile_impl`](Self::create_object_tmpfile_impl): - /// create a regular file with a random temporary name in the objects - /// directory, for use on filesystems that don't support O_TMPFILE. - fn create_named_object_tmpfile(&self, objects_dir: &OwnedFd) -> Result { - // A handful of retries is plenty: collisions on a 12-character - // random alphanumeric suffix are astronomically unlikely; this - // just guards against a freak collision. - for _ in 0..16 { - let name = generate_tmpname(".tmp-object-"); - match openat( - objects_dir, - &name, - OFlags::CREATE | OFlags::EXCL | OFlags::RDWR | OFlags::CLOEXEC, - Mode::from_raw_mode(0o644), - ) { - Ok(fd) => return Ok(ObjectTmpfile::Named { fd, name }), - Err(Errno::EXIST) => continue, - Err(e) => { - return Err(e).context("Creating fallback temp file in objects directory")?; - } - } - } - bail!("failed to create a uniquely-named fallback object tmpfile after 16 attempts") - } - /// Ensure an object exists by reflinking or hardlinking from a source file. /// /// The fallback chain is: reflink -> hardlink -> copy. @@ -2047,50 +1949,29 @@ impl Repository { allow_copy: bool, ctx: &mut ImportContext, ) -> Result<(ObjectID, ObjectStoreMethod)> { - use rustix::fs::{fstat, ioctl_ficlone}; + use rustix::fs::fstat; - let writable = self.ensure_writable_token()?; + let _writable = self.ensure_writable_token()?; + let helper = self.verity_helper()?; + let objects_dir = self.objects_dir()?; // Determine the source and destination device IDs so we can look up // whether this particular filesystem pair supports reflinks. let src_dev = fstat(src)?.st_dev; - let dst_dev = fstat(self.objects_dir()?)?.st_dev; + let dst_dev = fstat(objects_dir)?.st_dev; // Try reflink first, unless a previous call on this device pair // already discovered that FICLONE is unsupported. if !ctx.is_reflink_unsupported(src_dev, dst_dev) { - let objects_dir = self.objects_dir()?; - let tmpfile = self.create_object_tmpfile_impl(&writable)?; - let guard = tmpfile.guard(objects_dir); - - match ioctl_ficlone(&tmpfile, src) { - Ok(()) => { - // Reflink succeeded — verify size matches - let stat = fstat(&tmpfile)?; - anyhow::ensure!( - stat.st_size as u64 == size, - "Reflink size mismatch: expected {}, got {}", - size, - stat.st_size - ); - - let (object_id, method) = - self.finalize_object_tmpfile_impl(tmpfile, guard, size, &writable)?; - let method = match method { - ObjectStoreMethod::Copied => ObjectStoreMethod::Reflinked, - other => other, - }; - return Ok((object_id, method)); - } - Err(Errno::OPNOTSUPP | Errno::XDEV) => { + match helper.reflink_from_fd::(objects_dir, src, size) { + Err(FinishError::ReflinkUnsupported) => { // Record for this device pair so subsequent calls skip. ctx.mark_reflink_unsupported(src_dev, dst_dev); - drop(tmpfile); - // `guard` drops here too, cleaning up the fallback - // tmpfile (if any) since it was never used. } - Err(e) => { - return Err(e).context("Reflinking source file to objects directory")?; + result => { + return self + .publish_object(result, None) + .context("Reflinking source file to objects directory"); } } } @@ -2118,21 +1999,10 @@ impl Repository { } } - // Final fallback: copy data into a new tmpfile. - let objects_dir = self.objects_dir()?; - let tmpfile = self.create_object_tmpfile_impl(&writable)?; - let guard = tmpfile.guard(objects_dir); - { - use std::io::{Seek, SeekFrom}; - let mut dst = File::from(tmpfile.try_clone()?); - let mut src_clone = src.try_clone()?; - src_clone.seek(SeekFrom::Start(0))?; - std::io::copy(&mut src_clone, &mut dst)?; - } - - let (object_id, method) = - self.finalize_object_tmpfile_impl(tmpfile, guard, size, &writable)?; - Ok((object_id, method)) + // Final fallback: copy the data. + let result = helper.create_from_fd::(objects_dir, src, size); + self.publish_object(result, None) + .context("Copying source file to objects directory") } /// Try to hardlink a source file directly into the objects directory. @@ -2147,7 +2017,6 @@ impl Repository { src: &std::fs::File, size: u64, ) -> Result<(ObjectID, ObjectStoreMethod)> { - use crate::fsverity::enable_verity_with_retry; use rustix::thread::{CapabilitySet, capabilities}; // AT_EMPTY_PATH linkat requires CAP_DAC_READ_SEARCH. Check upfront @@ -2168,7 +2037,9 @@ impl Repository { // This is safe because the caller (bootc/containers-storage) owns the // source files and they are immutable image data. // AlreadyEnabled is fine — the file was already verity-protected. - let verity_enabled = match enable_verity_with_retry::(src) { + // The file isn't ours, so if something holds it open for writing + // that's an error (and the caller can fall back to copying). + let verity_enabled = match enable_verity_raw::(src) { Ok(()) => true, Err(EnableVerityError::AlreadyEnabled) => true, Err(EnableVerityError::FilesystemNotSupported) if self.is_insecure() => false, @@ -2217,163 +2088,76 @@ impl Repository { } } - /// Finalize a tmpfile as an object. - /// - /// This method should be called from a blocking context (e.g., `spawn_blocking`) - /// as it performs synchronous I/O operations. + /// Link a file from the fs-verity helper into the objects directory. /// - /// This method: - /// 1. Re-opens the file as read-only - /// 2. Enables fs-verity on the file (kernel computes digest) - /// 3. Reads the digest from the kernel - /// 4. Checks if object already exists (deduplication) - /// 5. Links the file into the objects directory - /// - /// By letting the kernel compute the digest during verity enable, we avoid - /// reading the file an extra time in userspace. - #[context("Finalizing object tempfile")] - pub fn finalize_object_tmpfile( - &self, - file: File, - size: u64, - ) -> Result<(ObjectID, ObjectStoreMethod)> { - let writable = self.ensure_writable_token()?; - let objects_dir = self - .objects_dir() - .context("Getting objects directory for finalization")?; - // The public API only ever hands us an anonymous O_TMPFILE (created - // via `create_object_tmpfile`), so there's no fallback name to track. - let guard = NamedTmpfileGuard::new(objects_dir, None); - self.finalize_object_tmpfile_impl( - ObjectTmpfile::Anonymous(file.into()), - guard, - size, - &writable, - ) - } - - /// Like [`finalize_object_tmpfile`](Self::finalize_object_tmpfile), but - /// also accepts the [`ObjectTmpfile`] wrapper produced by - /// [`create_object_tmpfile_impl`](Self::create_object_tmpfile_impl) - /// along with the [`NamedTmpfileGuard`] that's been protecting it since - /// creation, so the fallback temp name (for filesystems that don't - /// support O_TMPFILE) stays tracked and cleaned up on error. - #[context("Finalizing object tempfile")] - pub(crate) fn finalize_object_tmpfile_impl( + /// Without fs-verity support the file is only accepted for insecure + /// repositories, named by a digest computed in userspace. If an + /// object with the same digest and size already exists, the new file + /// is discarded and `AlreadyPresent` is returned. If `expected` is + /// given, the digest must match it. + #[context("Storing new object")] + fn publish_object( &self, - tmpfile: ObjectTmpfile, - guard: NamedTmpfileGuard<'_>, - size: u64, - _writable: &WritableRepo, + result: Result, + expected: Option<&ObjectID>, ) -> Result<(ObjectID, ObjectStoreMethod)> { - // Get objects_dir early since we may need it for verity copy. - let objects_dir = self - .objects_dir() - .context("Getting objects directory for finalization")?; - - let ro_fd = reopen_tmpfile_ro(tmpfile.into_file()) - .context("Re-opening tmpfile as read-only for verity")?; - - // Enable verity - the kernel reads the file and computes the digest. - // Use enable_verity_maybe_copy to handle the case where forked processes - // have inherited writable fds to this file. - let (ro_fd, verity_enabled) = - match enable_verity_maybe_copy::(objects_dir, ro_fd.as_fd()) { - Ok(None) => (ro_fd, true), - Ok(Some(new_fd)) => (new_fd, true), - Err(EnableVerityError::FilesystemNotSupported) if self.is_insecure() => { - (ro_fd, false) - } - Err(EnableVerityError::AlreadyEnabled) => (ro_fd, true), - Err(other) => return Err(other).context("Enabling verity on tmpfile")?, - }; - - // Get the digest - either from kernel (fast) or compute in userspace (fallback) - let id: ObjectID = if verity_enabled { - measure_verity(&ro_fd).context("Measuring verity digest")? - } else { - // Insecure mode: compute digest in userspace from ro_fd - let mut reader = std::io::BufReader::new(File::from( - ro_fd - .try_clone() - .context("Cloning fd for digest computation")?, - )); - Self::compute_verity_digest(&mut reader) - .context("Computing verity digest in insecure mode")? + let objects_dir = self.objects_dir()?; + let (file, id) = match result { + Ok(file) => { + let id = file + .digest::() + .context("fs-verity digest has the wrong algorithm")?; + (FinishedFile::Verity(file), id) + } + Err(FinishError::NotSupported(file)) if self.is_insecure() => { + let id = match file.userspace_digest::() { + Some(id) => id, + None => { + let fd = file + .as_fd() + .try_clone_to_owned() + .context("Cloning fd for digest computation")?; + let mut reader = + std::io::BufReader::with_capacity(IO_BUF_CAPACITY, File::from(fd)); + Self::compute_verity_digest(&mut reader)? + } + }; + (FinishedFile::Unverified(file), id) + } + Err(e) => return Err(e).context("Enabling fs-verity on new object"), }; + if let Some(expected) = expected { + ensure!( + &id == expected, + "Object digest mismatch: expected {}, got {}", + expected.to_hex(), + id.to_hex() + ); + } - self.link_tmpfile_as_object(objects_dir, &ro_fd, &id, size, guard) - } - - /// Link a read-only tmpfile into the objects directory with dedup check. - /// - /// If an object with the same digest and size already exists, the - /// tmpfile is discarded and `AlreadyPresent` is returned. - /// - /// `guard` protects the fallback (named) tmpfile, if any, from leaking: - /// it stays armed for the whole function and is only disarmed once the - /// tmpfile has actually been put in place, so any early return (e.g. - /// from `ensure_dir_at` failing) or the dedup/race paths below all - /// still clean it up via `Drop`. - /// - /// When there's a fallback name, the new object is put in place with - /// an atomic `renameat` (`RENAME_NOREPLACE`) instead of the - /// `linkat`-via-`/proc/self/fd` trick used for anonymous tmpfiles. If a - /// racing writer completes first for the same digest, the rename fails - /// with `EEXIST`; that's fine, we just discard our tmpfile and report - /// `AlreadyPresent`. - fn link_tmpfile_as_object( - &self, - objects_dir: &OwnedFd, - ro_fd: &impl AsFd, - id: &ObjectID, - size: u64, - guard: NamedTmpfileGuard, - ) -> Result<(ObjectID, ObjectStoreMethod)> { let path = id.to_object_pathname(); - - match statat(objects_dir, &path, AtFlags::empty()) { - Ok(stat) if stat.st_size as u64 == size => { - // `guard` drops here, discarding our now-redundant tmpfile. - return Ok((id.clone(), ObjectStoreMethod::AlreadyPresent)); - } - _ => {} + let size = rustix::fs::fstat(file.as_fd()) + .context("Checking new object size")? + .st_size; + if let Ok(stat) = statat(objects_dir, &path, AtFlags::empty()) + && stat.st_size == size + { + // Dropping `file` discards it. + return Ok((id, ObjectStoreMethod::AlreadyPresent)); } - let parent_dir = id.to_object_dir(); - ensure_dir_at(objects_dir, &parent_dir, Mode::from_raw_mode(0o755)) + ensure_dir_at(objects_dir, &id.to_object_dir(), Mode::from_raw_mode(0o755)) .context("creating object parent directory")?; - - if let Some(name) = guard.name() { - return match renameat_with( - objects_dir, - name, - objects_dir, - &path, - RenameFlags::NOREPLACE, - ) { - Ok(()) => { - // Successfully in place; nothing left to clean up. - guard.disarm(); - Ok((id.clone(), ObjectStoreMethod::Copied)) - } - // Lost a race to a concurrent writer for the same digest; - // `guard` drops here, discarding our tmpfile. - Err(Errno::EXIST) => Ok((id.clone(), ObjectStoreMethod::AlreadyPresent)), - Err(e) => Err(e).context("Renaming tmpfile into objects directory")?, - }; - } - - match linkat( - CWD, - proc_self_fd(ro_fd), - objects_dir, - &path, - AtFlags::SYMLINK_FOLLOW, - ) { - Ok(()) => Ok((id.clone(), ObjectStoreMethod::Copied)), - Err(Errno::EXIST) => Ok((id.clone(), ObjectStoreMethod::AlreadyPresent)), - Err(e) => Err(e).context("Linking tmpfile into objects directory")?, + let method = match file.copy_method() { + CopyMethod::Reflinked => ObjectStoreMethod::Reflinked, + CopyMethod::Written | CopyMethod::Copied => ObjectStoreMethod::Copied, + }; + match file + .publish(objects_dir, &path) + .context("Linking new object into objects directory")? + { + Published::Created => Ok((id, method)), + Published::AlreadyExists => Ok((id, ObjectStoreMethod::AlreadyPresent)), } } @@ -2429,18 +2213,30 @@ impl Repository { Err(CompareVerityError::Measure(MeasureVerityError::VerityMissing)) if self.is_insecure() => { - match enable_verity_maybe_copy::(dirfd, fd.as_fd()) { - // The object is open for writing somewhere, so - // verity went onto a copy. The copy only - // serves to check the content here and is then - // dropped on purpose: swapping it in would - // replace the inode whose flock tells gc() that - // an image is mounted. The object stays without - // verity, which an insecure repository allows. - Ok(Some(copy)) => ensure_verity_equal(©, id) - .context("Verifying verity after enabling (copied)")?, - Ok(None) => ensure_verity_equal(&fd, id) - .context("Verifying verity after enabling (original)")?, + match enable_verity_raw::(&fd) { + Ok(()) | Err(EnableVerityError::AlreadyEnabled) => { + ensure_verity_equal(&fd, id) + .context("Verifying verity after enabling")? + } + // The object is open for writing somewhere (e.g. + // in a forked child that hasn't exec'd yet). It + // stays without verity, which an insecure + // repository allows; check its content instead. + // Replacing it with a fresh copy isn't an option: + // that would replace the inode whose flock tells + // gc() that an image is mounted. + Err(EnableVerityError::FileOpenedForWrite) => { + let mut reader = std::io::BufReader::with_capacity( + IO_BUF_CAPACITY, + File::from(fd.try_clone()?), + ); + let found = Self::compute_verity_digest(&mut reader)?; + ensure!( + &found == id, + "Existing object has digest {}", + found.to_hex() + ); + } Err(other) => { Err(other).context("Enabling verity on existing object")? } @@ -2461,46 +2257,14 @@ impl Repository { } } - let fd = ensure_dir_and_openat(dirfd, &id.to_object_dir(), OFlags::RDWR | OFlags::TMPFILE) - .with_context(|| "Creating tempfile in object subdirectory")?; - let mut file = File::from(fd); - file.write_all(data).context("Writing data to tmpfile")?; // NB: We should do fdatasync() or fsync() here, but doing this for each file forces the // creation of a massive number of journal commits and is a performance disaster. We need // to coordinate this at a higher level. See .write_stream(). - let ro_fd = reopen_tmpfile_ro(file).context("Re-opening file as read-only for verity")?; - - let ro_fd = match enable_verity_maybe_copy::(dirfd, ro_fd.as_fd()) { - Ok(maybe_fd) => { - let ro_fd = maybe_fd.unwrap_or(ro_fd); - match ensure_verity_equal(&ro_fd, id) { - Ok(()) => ro_fd, - Err(CompareVerityError::Measure( - MeasureVerityError::VerityMissing - | MeasureVerityError::FilesystemNotSupported, - )) if self.is_insecure() => ro_fd, - Err(other) => Err(other).context("Double-checking verity digest")?, - } - } - Err(EnableVerityError::FilesystemNotSupported) if self.is_insecure() => ro_fd, - Err(other) => Err(other).context("Enabling verity digest")?, - }; - - match linkat( - CWD, - proc_self_fd(&ro_fd), - dirfd, - path, - AtFlags::SYMLINK_FOLLOW, - ) { - Ok(()) => {} - Err(Errno::EXIST) => { - // TODO: strictly, we should measure the newly-appeared file - } - Err(other) => { - return Err(other).context("Linking created object file"); - } - } + let mut writer = self + .verity_helper()? + .create::(dirfd, data.len() as u64)?; + writer.write_all(data).context("Writing object data")?; + self.publish_object(writer.finish(), Some(id))?; Ok(()) } @@ -5427,8 +5191,8 @@ mod tests { } /// On filesystems without O_TMPFILE support (e.g. some fuse-overlayfs - /// configurations, see bootc-dev/bootc#2340), `create_object_tmpfile_impl` - /// must fall back to a named temp file that gets renamed into place (or + /// configurations, see bootc-dev/bootc#2340), object files must fall + /// back to a named temp file that gets renamed into place (or /// unlinked on dedup) instead of erroring out. #[test] fn test_ensure_object_from_reader_named_tmpfile_fallback() -> Result<()> { @@ -5455,6 +5219,150 @@ mod tests { Ok(()) } + /// Regression test: with the named-tmpfile fallback, a writable + /// descriptor inherited by a forked child used to make verity go onto + /// a copy while the original, verity-less file was renamed into + /// place. Objects in a secure repository must always have verity. + #[test] + fn test_named_tmpfile_objects_have_verity_while_forking() -> Result<()> { + use composefs_ioctls::test_utils_pub::CommandExt as _; + use std::sync::atomic::{AtomicBool, Ordering}; + + let tmp = tempdir(); + let (repo, _) = Repository::::init_path( + CWD, + tmp.path().join("repo"), + RepositoryConfig::new(Algorithm::SHA512), + )?; + assert!(!repo.is_insecure()); + repo.set_force_named_tmpfile(true); + + let stop = Arc::new(AtomicBool::new(false)); + let forkers: Vec<_> = (0..4) + .map(|_| { + let stop = Arc::clone(&stop); + std::thread::spawn(move || { + while !stop.load(Ordering::Relaxed) { + let _ = std::process::Command::new("true") + .pre_exec_sleep(std::time::Duration::from_millis(5)) + .status(); + } + }) + }) + .collect(); + + let result = (0..100u32).try_for_each(|i| -> Result<()> { + let data = generate_test_data(16 * 1024, i as u8); + let id = repo.ensure_object_from_reader(data.as_slice(), data.len() as u64)?; + let fd = repo.open_object(&id)?; + assert_eq!(measure_verity::(&fd)?, id); + Ok(()) + }); + + stop.store(true, Ordering::Relaxed); + for forker in forkers { + forker.join().unwrap(); + } + result?; + assert!(stray_tmpfiles_in_objects(&tmp)?.is_empty()); + Ok(()) + } + + #[tokio::test] + async fn test_create_object_async() -> Result<()> { + let tmp = tempdir(); + let repo = create_test_repo(&tmp.path().join("repo"))?; + let data = generate_test_data(256 * 1024, 0x5A); + + let mut writer = repo.create_object(data.len() as u64)?; + tokio::io::AsyncWriteExt::write_all(&mut writer, &data).await?; + let (id, method) = writer.finish_async().await?; + assert_eq!(id, compute_verity::(&data)); + assert_eq!(method, ObjectStoreMethod::Copied); + assert_eq!(repo.read_object(&id)?, data); + + let mut writer = repo.create_object(data.len() as u64)?; + tokio::io::AsyncWriteExt::write_all(&mut writer, &data).await?; + let (_, method) = writer.finish_async().await?; + assert_eq!(method, ObjectStoreMethod::AlreadyPresent); + Ok(()) + } + + #[test] + fn test_create_object_size_mismatch() -> Result<()> { + let tmp = tempdir(); + let repo = create_test_repo(&tmp.path().join("repo"))?; + let data = generate_test_data(4096, 0x11); + + let err = repo + .ensure_object_from_reader(data.as_slice(), 4000) + .unwrap_err(); + assert!( + matches!( + err.root_cause().downcast_ref::(), + Some(FinishError::SizeMismatch { + expected: 4000, + actual: 4096 + }) + ), + "{err:#}" + ); + + // A dropped writer leaves nothing behind either. + let mut writer = repo.create_object(100)?; + writer.write_all(b"partial")?; + drop(writer); + assert!(stray_tmpfiles_in_objects(&tmp)?.is_empty()); + Ok(()) + } + + #[test] + fn test_create_scratch_tmpfile() -> Result<()> { + use std::io::{Read as _, Seek as _}; + + let tmp = tempdir(); + let repo = create_test_repo(&tmp.path().join("repo"))?; + let mut file = File::from(repo.create_scratch_tmpfile()?); + file.write_all(b"scratch")?; + file.rewind()?; + let mut contents = String::new(); + file.read_to_string(&mut contents)?; + assert_eq!(contents, "scratch"); + assert!( + std::fs::read_dir(tmp.path().join("repo/objects"))? + .next() + .is_none() + ); + Ok(()) + } + + /// In an insecure repository, an existing object without verity gets + /// it enabled when stored again; but if it is open for writing + /// somewhere, it is left as it is and only its content is checked. + #[test] + fn test_existing_object_open_for_writing() -> Result<()> { + let tmp = tempdir(); + let repo = create_test_repo(&tmp.path().join("repo"))?; + let data = generate_test_data(8192, 0x77); + let id = compute_verity::(&data); + + let objects = tmp.path().join("repo/objects"); + let path = objects.join(id.to_object_pathname()); + std::fs::create_dir_all(path.parent().unwrap())?; + std::fs::write(&path, &data)?; + let writer = std::fs::OpenOptions::new().write(true).open(&path)?; + + assert_eq!(repo.ensure_object(&data)?, id); + assert!(measure_verity_opt::(&File::open(&path)?)?.is_none()); + + // Wrong content is caught. + std::fs::write(&path, generate_test_data(8192, 0x78))?; + assert!(repo.ensure_object(&data).is_err()); + + drop(writer); + Ok(()) + } + // ==================== Fsck Tests ==================== #[tokio::test] @@ -6255,90 +6163,67 @@ mod tests { Ok(names) } - /// If the staging file for `meta.json` is still open for writing - /// elsewhere when verity is enabled (as happens when another thread - /// forks and the child has not yet exec'd), verity ends up on a copy. - /// That copy, not the staging file, must become `meta.json`: otherwise - /// a strict init silently produces a repository without verity. + fn open_dir(path: &Path) -> Result { + Ok(openat( + CWD, + path, + OFlags::RDONLY | OFlags::DIRECTORY | OFlags::CLOEXEC, + Mode::empty(), + )?) + } + + /// A strict `meta.json` must end up with fs-verity, with or without + /// O_TMPFILE, and leave nothing else behind. #[test] - fn test_meta_json_verity_with_busy_staging_file() -> Result<()> { + fn test_meta_json_verity() -> Result<()> { let meta = RepoMetadata::new(Algorithm::SHA512); let data = meta.to_json()?; - for tmpname in [None, Some(".tmp-meta-test")] { + for force_named in [false, true] { let tmp = tempdir(); - let repo_fd = openat( - CWD, - tmp.path(), - OFlags::RDONLY | OFlags::DIRECTORY | OFlags::CLOEXEC, - Mode::empty(), - )?; - let staging = match tmpname { - None => openat( - &repo_fd, - ".", - OFlags::WRONLY | OFlags::TMPFILE | OFlags::CLOEXEC, - Mode::from_raw_mode(0o644), - )?, - Some(name) => openat( - &repo_fd, - name, - OFlags::WRONLY | OFlags::CREATE | OFlags::EXCL | OFlags::CLOEXEC, - Mode::from_raw_mode(0o644), - )?, - }; - let staging = File::from(staging); - // Stand in for the forked child holding an inherited descriptor. - let writer = staging.try_clone()?; - - publish_repo_metadata(&repo_fd, staging, tmpname, &data, Some(&meta.algorithm))?; - drop(writer); + let repo_fd = open_dir(tmp.path())?; + write_repo_metadata_verity(&repo_fd, &data, meta.algorithm, force_named)?; assert_eq!( Repository::::read_and_probe_metadata(&repo_fd)?, (meta.clone(), true), - "staging name {tmpname:?}" + "force_named {force_named}" ); + assert_eq!(names_in(&tmp)?, [REPO_METADATA_FILENAME]); let repo = Repository::::open_path(&repo_fd, ".")?; - assert!(!repo.is_insecure(), "staging name {tmpname:?}"); + assert!(!repo.is_insecure(), "force_named {force_named}"); } Ok(()) } - /// The fallback for filesystems without O_TMPFILE stages `meta.json` - /// under a temporary name, which must not outlive the call. + /// An existing meta.json is never replaced, and a failed attempt + /// cleans up after itself. #[test] - fn test_meta_json_named_fallback() -> Result<()> { + fn test_meta_json_exists() -> Result<()> { let tmp = tempdir(); - let repo_fd = openat( - CWD, - tmp.path(), - OFlags::RDONLY | OFlags::DIRECTORY | OFlags::CLOEXEC, - Mode::empty(), - )?; + let repo_fd = open_dir(tmp.path())?; let meta = RepoMetadata::new(Algorithm::SHA512); - let data = meta.to_json()?; + write_repo_metadata(&repo_fd, &meta, true)?; - write_repo_metadata_named(&repo_fd, &data, Some(&meta.algorithm))?; - assert_eq!( - Repository::::read_and_probe_metadata(&repo_fd)?, - (meta.clone(), true) - ); - assert_eq!(names_in(&tmp)?, [REPO_METADATA_FILENAME]); - - // An existing meta.json is never replaced, and the failed attempt - // cleans up after itself. let other = RepoMetadata::new(Algorithm::SHA256).to_json()?; - let err = write_repo_metadata_named(&repo_fd, &other, None).unwrap_err(); - assert_eq!( - err.root_cause().downcast_ref::(), - Some(&Errno::EXIST) - ); - assert_eq!( - Repository::::read_and_probe_metadata(&repo_fd)?, - (meta.clone(), true) - ); - assert_eq!(names_in(&tmp)?, [REPO_METADATA_FILENAME]); + for force_named in [false, true] { + let errors = [ + write_repo_metadata_plain(&repo_fd, &other, force_named).unwrap_err(), + write_repo_metadata_verity(&repo_fd, &other, Algorithm::SHA256, force_named) + .unwrap_err(), + ]; + for err in errors { + assert_eq!( + err.root_cause().downcast_ref::(), + Some(&Errno::EXIST) + ); + } + assert_eq!( + Repository::::read_and_probe_metadata(&repo_fd)?, + (meta.clone(), true) + ); + assert_eq!(names_in(&tmp)?, [REPO_METADATA_FILENAME]); + } Ok(()) } @@ -6353,21 +6238,16 @@ mod tests { // /dev/shm is a tmpfs: O_TMPFILE works there, fs-verity does not. let tmp = TempDir::with_prefix_in("composefs-test-", "/dev/shm")?; - let repo_fd = openat( - CWD, - tmp.path(), - OFlags::RDONLY | OFlags::DIRECTORY | OFlags::CLOEXEC, - Mode::empty(), - )?; + let repo_fd = open_dir(tmp.path())?; assert!(write_repo_metadata(&repo_fd, &meta, true).is_err()); assert!(names_in(&tmp)?.is_empty()); - assert!(write_repo_metadata_named(&repo_fd, &data, Some(&meta.algorithm)).is_err()); + assert!(write_repo_metadata_verity(&repo_fd, &data, meta.algorithm, true).is_err()); assert!(names_in(&tmp)?.is_empty()); - // Without the verity requirement both succeed. - write_repo_metadata_named(&repo_fd, &data, None)?; + // Without the verity requirement it succeeds. + write_repo_metadata_plain(&repo_fd, &data, true)?; assert_eq!(names_in(&tmp)?, [REPO_METADATA_FILENAME]); Ok(()) } diff --git a/crates/composefs/src/util.rs b/crates/composefs/src/util.rs index 521ac53f..92c8c33c 100644 --- a/crates/composefs/src/util.rs +++ b/crates/composefs/src/util.rs @@ -54,40 +54,6 @@ pub(crate) fn proc_self_fd(fd: impl AsFd) -> String { format!("/proc/self/fd/{}", fd.as_fd().as_raw_fd()) } -/// Re-open a writable file as read-only, consuming the original. -/// -/// This is the standard preparation step before enabling fs-verity: -/// the kernel requires that no writable file descriptors exist for -/// the inode. The re-open goes through `/proc/self/fd` so it works -/// on anonymous O_TMPFILE inodes that have no directory entry yet. -pub(crate) fn reopen_tmpfile_ro(file: std::fs::File) -> std::io::Result { - let path = proc_self_fd(&file); - let ro = rustix::fs::open( - &*path, - rustix::fs::OFlags::RDONLY | rustix::fs::OFlags::CLOEXEC, - rustix::fs::Mode::empty(), - )?; - drop(file); - Ok(ro) -} - -/// Create an anonymous O_TMPFILE in the given directory. -/// -/// The returned fd is open read-write and has no directory entry — it is -/// invisible in the filesystem until explicitly linked via `linkat`. To make -/// it permanent, reopen it read-only via [`reopen_tmpfile_ro`] and then use -/// `linkat` through `/proc/self/fd`. -pub(crate) fn create_tmpfile_in( - dirfd: impl rustix::fd::AsFd, -) -> rustix::io::Result { - rustix::fs::openat( - dirfd, - ".", - rustix::fs::OFlags::RDWR | rustix::fs::OFlags::TMPFILE | rustix::fs::OFlags::CLOEXEC, - rustix::fs::Mode::from_raw_mode(0o644), - ) -} - /// This function reads the exact amount of bytes required to fill the buffer, possibly performing /// multiple reads to do so (and also retrying if required to deal with EINTR). ///