-
Notifications
You must be signed in to change notification settings - Fork 36
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
We can expand this in the future; for now I started by just binding the digest API, which we definitely need when doing things in Rust in the future. Signed-off-by: Colin Walters <[email protected]>
- Loading branch information
Showing
7 changed files
with
151 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,61 @@ | ||
//! # Bindings for computing fsverity | ||
//! | ||
//! This collection of APIs is for computing fsverity digests as | ||
//! used by composefs. | ||
|
||
use std::os::fd::{AsRawFd, BorrowedFd}; | ||
|
||
use composefs_sys::{map_result, LCFS_SHA256_DIGEST_LEN}; | ||
|
||
/// The binary composefs digest | ||
#[derive(Debug, PartialEq, Eq, Clone, Default)] | ||
pub struct Digest([u8; LCFS_SHA256_DIGEST_LEN]); | ||
|
||
impl Digest { | ||
/// Create an uninitialized digest. | ||
pub fn new() -> Self { | ||
Self::default() | ||
} | ||
|
||
/// Retrieve the digest bytes | ||
pub fn get(&self) -> &[u8; LCFS_SHA256_DIGEST_LEN] { | ||
&self.0 | ||
} | ||
} | ||
|
||
/// Compute the composefs fsverity digest from the provided file descriptor. | ||
#[allow(unsafe_code)] | ||
pub fn fsverity_digest_from_fd(fd: BorrowedFd, digest: &mut Digest) -> std::io::Result<()> { | ||
unsafe { | ||
map_result(composefs_sys::lcfs_compute_fsverity_from_fd( | ||
digest.0.as_mut_ptr(), | ||
fd.as_raw_fd(), | ||
)) | ||
} | ||
} | ||
|
||
#[cfg(test)] | ||
mod tests { | ||
use anyhow::Result; | ||
use std::io::{Seek, Write}; | ||
use std::os::fd::AsFd; | ||
|
||
use super::*; | ||
|
||
#[test] | ||
fn test_digest() -> Result<()> { | ||
let mut tf = tempfile::tempfile()?; | ||
tf.write_all(b"hello world")?; | ||
let mut digest = Digest::new(); | ||
tf.seek(std::io::SeekFrom::Start(0))?; | ||
fsverity_digest_from_fd(tf.as_fd(), &mut digest)?; | ||
assert_eq!( | ||
digest.get(), | ||
&[ | ||
30, 46, 170, 66, 2, 215, 80, 164, 17, 116, 238, 69, 73, 112, 185, 44, 27, 194, 249, | ||
37, 177, 227, 80, 118, 216, 199, 213, 245, 99, 98, 186, 100 | ||
] | ||
); | ||
Ok(()) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
[package] | ||
name = "composefs-sys" | ||
version = "0.1.0" | ||
edition = "2021" | ||
links = "composefs" | ||
build = "build.rs" | ||
|
||
[package.metadata.system-deps.composefs] | ||
name = "composefs" | ||
version = "1" | ||
|
||
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html | ||
|
||
[build-dependencies] | ||
system-deps = "6" | ||
|
||
[dev-dependencies] | ||
anyhow = "1" | ||
tempfile = "3" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
fn main() { | ||
if let Err(s) = system_deps::Config::new().probe() { | ||
println!("cargo:warning={s}"); | ||
std::process::exit(1); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,60 @@ | ||
//! # Bindings for libcomposefs | ||
//! | ||
//! This crate contains a few manually maintained system bindings for libcomposefs. | ||
pub const LCFS_SHA256_DIGEST_LEN: usize = 32; | ||
|
||
extern "C" { | ||
pub fn lcfs_compute_fsverity_from_fd( | ||
digest: *mut u8, | ||
fd: std::os::raw::c_int, | ||
) -> std::os::raw::c_int; | ||
pub fn lcfs_fd_enable_fsverity(fd: std::os::raw::c_int) -> std::os::raw::c_int; | ||
} | ||
|
||
/// Convert an integer return value into a `Result`. | ||
pub fn map_result(r: std::os::raw::c_int) -> std::io::Result<()> { | ||
match r { | ||
0 => Ok(()), | ||
_ => Err(std::io::Error::last_os_error()), | ||
} | ||
} | ||
|
||
#[cfg(test)] | ||
mod tests { | ||
use anyhow::Result; | ||
use std::io::{Seek, Write}; | ||
use std::os::fd::AsRawFd; | ||
|
||
use super::*; | ||
|
||
#[test] | ||
fn test_fd_enable_fsverity() -> Result<()> { | ||
// We can't require fsverity in our test suite, so just verify we can call the | ||
// function. | ||
let mut tf = tempfile::NamedTempFile::new()?; | ||
tf.write_all(b"hello")?; | ||
let tf = std::fs::File::open(tf.path())?; | ||
let _ = unsafe { lcfs_fd_enable_fsverity(tf.as_raw_fd()) }; | ||
Ok(()) | ||
} | ||
|
||
#[test] | ||
fn test_digest() -> Result<()> { | ||
unsafe { | ||
let mut tf = tempfile::tempfile()?; | ||
tf.write_all(b"hello world")?; | ||
let mut buf = [0u8; LCFS_SHA256_DIGEST_LEN]; | ||
tf.seek(std::io::SeekFrom::Start(0))?; | ||
let r = lcfs_compute_fsverity_from_fd(buf.as_mut_ptr(), tf.as_raw_fd()); | ||
assert_eq!(r, 0); | ||
assert_eq!( | ||
buf, | ||
[ | ||
30, 46, 170, 66, 2, 215, 80, 164, 17, 116, 238, 69, 73, 112, 185, 44, 27, 194, | ||
249, 37, 177, 227, 80, 118, 216, 199, 213, 245, 99, 98, 186, 100 | ||
] | ||
); | ||
Ok(()) | ||
} | ||
} | ||
} |