Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Use composefs crate for fsverity digest reading #24

Closed
wants to merge 3 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 7 additions & 4 deletions .github/workflows/rust.yml
Original file line number Diff line number Diff line change
Expand Up @@ -10,17 +10,20 @@ env:
CARGO_TERM_COLOR: always

jobs:
build:

build-fedora:
runs-on: ubuntu-24.04
container:
image: quay.io/fedora/fedora:41
options: "--privileged --pid=host -v /var/tmp:/var/tmp -v /:/run/host"

steps:
- run: dnf -y install cargo clippy composefs-devel e2fsprogs
- name: Enable fs-verity on /
run: sudo tune2fs -O verity $(findmnt -vno SOURCE /)
run: tune2fs -O verity $(findmnt -vno SOURCE /run/host)
- uses: actions/checkout@v4
- name: Build
run: cargo build --verbose
- name: Clippy
run: cargo clippy
- name: Run tests
run: cargo test --verbose
run: env CFS_TEST_TMPDIR=/run/host/var/tmp cargo test --verbose
1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ anyhow = { version = "1.0.89", features = ["backtrace"] }
async-compression = { version = "0.4.17", features = ["tokio", "gzip", "zstd"] }
clap = { version = "4.5.19", features = ["derive"] }
containers-image-proxy = "0.7.0"
composefs = "0.1.3"
hex = "0.4.3"
indicatif = { version = "0.17.8", features = ["tokio"] }
oci-spec = "0.7.0"
Expand Down
40 changes: 7 additions & 33 deletions src/fsverity/ioctl.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ use std::os::fd::AsFd;
use anyhow::Result;
use rustix::ioctl;

use super::FsVerityHashValue;
use super::{FsVerityHashValue, Sha256HashValue};

// See /usr/include/linux/fsverity.h
#[repr(C)]
Expand Down Expand Up @@ -43,36 +43,10 @@ pub fn fs_ioc_enable_verity<F: AsFd, H: FsVerityHashValue>(fd: F) -> Result<()>
Ok(())
}

#[repr(C)]
pub struct FsVerityDigest<F> {
digest_algorithm: u16,
digest_size: u16,
digest: F,
}

// #define FS_IOC_MEASURE_VERITY _IORW('f', 134, struct fsverity_digest)
type FsIocMeasureVerity = ioctl::ReadWriteOpcode<b'f', 134, FsVerityDigest<()>>;

pub fn fs_ioc_measure_verity<F: AsFd, H: FsVerityHashValue>(fd: F) -> Result<H> {
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

That said this code is quite elegant and nice; the generics usage for sha256/sha512 is cool. But OTOH there are no plans to use anything but sha256 for composefs.

What we could do though is move some of this "reimplement composefs core in Rust" into the composefs git repo under the composefs crate under an optional feature flag or something - might be especially useful to "cross test" i.e. verify the C and Rust codepaths do the same thing.

let digest_size = std::mem::size_of::<H>() as u16;
let digest_algorithm = H::ALGORITHM as u16;

let mut digest = FsVerityDigest::<H> {
digest_algorithm,
digest_size,
digest: H::EMPTY,
};

unsafe {
ioctl::ioctl(
fd,
ioctl::Updater::<FsIocMeasureVerity, FsVerityDigest<H>>::new(&mut digest),
)?;
}

if digest.digest_algorithm != digest_algorithm || digest.digest_size != digest_size {
Err(std::io::Error::from(std::io::ErrorKind::InvalidData))?
} else {
Ok(digest.digest)
}
pub fn fs_ioc_measure_verity<F: AsFd>(fd: F) -> Result<Sha256HashValue> {
let mut digest = composefs::fsverity::Digest::new();
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Also looking at this, perhaps we should drop the Digest wrapper and do what you have here and just have it write into an array. Definitely the compile-time known size is better than what we have in .get() at the moment.

OTOH, and this relates to a larger topic...I feel like in practice we end up converting to hex in many places anyways, so we may as well canonicalize to e.g. this Sha256Digest instead.

composefs::fsverity::fsverity_digest_from_fd(fd.as_fd(), &mut digest)?;
let mut r = Sha256HashValue::EMPTY;
r.copy_from_slice(digest.get());
Ok(r)
}
7 changes: 5 additions & 2 deletions tests/repo.rs
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,10 @@ fn example_layer() -> Result<Vec<u8>> {
Ok(builder.into_inner()?)
}

fn home_var_tmp() -> Result<PathBuf> {
fn test_global_tmpdir() -> Result<PathBuf> {
if let Some(path) = std::env::var_os("CFS_TEST_TMPDIR") {
return Ok(path.into());
}
// We can't use /tmp because that's usually a tmpfs (no fsverity)
// We also can't use /var/tmp because it's an overlayfs in toolbox (no fsverity)
// So let's try something in the user's homedir?
Expand All @@ -41,7 +44,7 @@ fn test_layer() -> Result<()> {
context.update(&layer);
let layer_id: [u8; 32] = context.finalize().into();

let tmpfile = tempfile::TempDir::with_prefix_in("composefs-test-", home_var_tmp()?)?;
let tmpfile = tempfile::TempDir::with_prefix_in("composefs-test-", test_global_tmpdir()?)?;
let repo = Repository::open_path(tmpfile.path().to_path_buf())?;
let id = oci::import_layer(&repo, &layer_id, Some("name"), &mut layer.as_slice())?;

Expand Down