model-format: VerifyPolicy (stamp directory and archive attestation) replaces the process-wide stamp registry
verify_stamp::set_global/global and set_attested_archive/attested_archive are gone. The operator binary builds a VerifyPolicy from its flags and the loaders receive it: VerifiedGgufFile::open_with and VerifiedKarPleArtifact::verify_under take it; open, verify and verify_sparse keep their signatures and hash in full (the default policy vouches for nothing), which is what a process that never set the globals did (STRA-338 S13).
This commit is contained in:
@@ -82,3 +82,4 @@ mod admits_flash_metadata_and_rejects;
|
||||
mod buffered_cursor_preserves_offsets_parsin;
|
||||
mod empty_owned_tensor_validation_preserves;
|
||||
mod original_first_source_fence_retains;
|
||||
mod stamp_policy_stands_in_for_the_hash;
|
||||
|
||||
@@ -0,0 +1,47 @@
|
||||
//! The verification stamps are an injected `VerifyPolicy`, not a process-wide registry: the same open hashes the file
|
||||
//! under the default policy and trusts a stamp of the policy it is given.
|
||||
#![cfg(unix)]
|
||||
|
||||
use super::{OwnedFixture, VerifiedGgufFile};
|
||||
use crate::verify_stamp::{FileIdentity, VerifyPolicy, VerifyStamps};
|
||||
use sha2::Digest;
|
||||
use std::os::unix::fs::PermissionsExt;
|
||||
|
||||
/// A stamp for `digest` on the fixture file as it stands now, in a private directory of its own.
|
||||
fn stamped(fixture: &OwnedFixture, digest: &[u8; 32]) -> VerifyPolicy {
|
||||
let dir = fixture.directory.join("stamps");
|
||||
std::fs::create_dir(&dir).unwrap();
|
||||
std::fs::set_permissions(&dir, std::fs::Permissions::from_mode(0o700)).unwrap();
|
||||
let stamps = VerifyStamps::new(&dir, false);
|
||||
let identity = FileIdentity::of(&std::fs::metadata(&fixture.path).unwrap()).unwrap();
|
||||
stamps.record("gguf", digest, &identity).unwrap();
|
||||
VerifyPolicy::new(Some(stamps), false)
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn a_stamp_of_the_policy_stands_in_for_the_hash_and_the_default_policy_hashes() {
|
||||
let fixture = OwnedFixture::new();
|
||||
// not the digest of the file: only a stamp can make this open succeed
|
||||
let digest = [0xab; 32];
|
||||
let pinned = format!("sha256:{}", "ab".repeat(32));
|
||||
let policy = stamped(&fixture, &digest);
|
||||
assert!(VerifiedGgufFile::open_with(&fixture.path, &pinned, &policy).is_ok(), "the stamp is trusted");
|
||||
for hashed in [VerifiedGgufFile::open(&fixture.path, &pinned), {
|
||||
VerifiedGgufFile::open_with(&fixture.path, &pinned, &VerifyPolicy::default())
|
||||
}] {
|
||||
assert!(hashed.unwrap_err().to_string().contains("SHA256 mismatch"), "no policy: the file is hashed");
|
||||
}
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn a_full_hash_records_a_stamp_in_the_policy_it_was_given() {
|
||||
let fixture = OwnedFixture::new();
|
||||
let dir = fixture.directory.join("stamps");
|
||||
std::fs::create_dir(&dir).unwrap();
|
||||
std::fs::set_permissions(&dir, std::fs::Permissions::from_mode(0o700)).unwrap();
|
||||
let policy = VerifyPolicy::new(Some(VerifyStamps::new(&dir, false)), false);
|
||||
VerifiedGgufFile::open_with(&fixture.path, &fixture.digest(), &policy).unwrap();
|
||||
let identity = FileIdentity::of(&std::fs::metadata(&fixture.path).unwrap()).unwrap();
|
||||
let digest: [u8; 32] = sha2::Sha256::digest(&fixture.bytes).into();
|
||||
assert!(policy.stamps().unwrap().trusts("gguf", &digest, &identity));
|
||||
}
|
||||
@@ -6,18 +6,25 @@ use super::{
|
||||
VerifiedGgufFile, ensure_size, open_regular_file, parse_artifact_digest, read_file_span_into, read_index_file,
|
||||
read_metadata_snapshot, read_split_metadata_snapshot, sha256_pipelined, stream_file_span,
|
||||
};
|
||||
use crate::verify_stamp::VerifyPolicy;
|
||||
|
||||
/// `VerifiedGgufFile` as a `WeightSource` (private fields: the impl must live below `gguf`).
|
||||
mod port;
|
||||
|
||||
impl VerifiedGgufFile {
|
||||
/// Verify `sha256:<64 hex digits>` and strictly index the same opened file.
|
||||
/// Verify `sha256:<64 hex digits>` and strictly index the same opened file; the whole file is hashed.
|
||||
/// The opened file's contents must remain stable until this handle is dropped.
|
||||
pub fn open(path: &Path, expected_digest: &str) -> Result<Self> {
|
||||
Self::open_with(path, expected_digest, &VerifyPolicy::default())
|
||||
}
|
||||
|
||||
/// [`Self::open`], except that a verification stamp of `policy` for this very file in this very state stands in
|
||||
/// for the hash (and a full hash records one).
|
||||
pub fn open_with(path: &Path, expected_digest: &str, policy: &VerifyPolicy) -> Result<Self> {
|
||||
let expected = parse_artifact_digest(expected_digest)?;
|
||||
let (mut file, metadata) = open_regular_file(path)?;
|
||||
let size = metadata.len();
|
||||
let actual = verify_digest(&file, &metadata, &expected)?;
|
||||
let actual = verify_digest(&file, &metadata, (&expected, policy))?;
|
||||
let index = read_index_file(&mut file, size, true)?;
|
||||
ensure_size(&file, size, "GGUF source size changed during indexing")?;
|
||||
Ok(Self { file, index, artifact_sha256: actual, resident_source_id: None })
|
||||
@@ -161,10 +168,14 @@ impl VerifiedGgufFile {
|
||||
}
|
||||
|
||||
/// Hash the whole file against `expected`, or trust a verification stamp for this very file in this very state.
|
||||
fn verify_digest(file: &File, metadata: &std::fs::Metadata, expected: &[u8; 32]) -> Result<[u8; 32]> {
|
||||
fn verify_digest(
|
||||
file: &File,
|
||||
metadata: &std::fs::Metadata,
|
||||
(expected, policy): (&[u8; 32], &VerifyPolicy),
|
||||
) -> Result<[u8; 32]> {
|
||||
let size = metadata.len();
|
||||
let identity = crate::verify_stamp::FileIdentity::of(metadata);
|
||||
let stamps = crate::verify_stamp::global();
|
||||
let stamps = policy.stamps();
|
||||
// A stamp skips the hash only for this very file in this very state, keyed by the pinned digest.
|
||||
let trusted = matches!((stamps, identity.as_ref()), (Some(s), Some(id)) if s.trusts("gguf", expected, id));
|
||||
let actual: [u8; 32] = if trusted { *expected } else { sha256_pipelined(file, size)? };
|
||||
|
||||
@@ -10,11 +10,21 @@ use super::{
|
||||
};
|
||||
|
||||
mod verify;
|
||||
use crate::verify_stamp::VerifyPolicy;
|
||||
use verify::verify_archive;
|
||||
|
||||
impl VerifiedKarPleArtifact {
|
||||
pub fn verify(source: KarImmutableSource, profile: KarPleProfile) -> Result<Self, KarError> {
|
||||
Self::verify_with(source, profile, None)
|
||||
Self::verify_with(source, profile, (None, &VerifyPolicy::default()))
|
||||
}
|
||||
/// [`Self::verify`], except that a stamp of `policy` for this very file in this very state, or the parent's
|
||||
/// attestation of an unlinked sealed memfd, stands in for the whole-archive SHA-256 and payload CRC pass.
|
||||
pub fn verify_under(
|
||||
source: KarImmutableSource,
|
||||
profile: KarPleProfile,
|
||||
policy: &VerifyPolicy,
|
||||
) -> Result<Self, KarError> {
|
||||
Self::verify_with(source, profile, (None, policy))
|
||||
}
|
||||
/// `verify` for a sealed memfd that holds only the bytes the serve path reads (holes elsewhere, same file offsets
|
||||
/// and
|
||||
@@ -27,15 +37,17 @@ impl VerifiedKarPleArtifact {
|
||||
profile: KarPleProfile,
|
||||
extents_sha256: &[u8; 32],
|
||||
) -> Result<Self, KarError> {
|
||||
Self::verify_with(source, profile, Some(extents_sha256))
|
||||
Self::verify_with(source, profile, (Some(extents_sha256), &VerifyPolicy::default()))
|
||||
}
|
||||
/// `how`: the pinned digest of a sparse copy's extents (`None`: a whole archive) and what vouches for the file.
|
||||
pub(super) fn verify_with(
|
||||
source: KarImmutableSource,
|
||||
profile: KarPleProfile,
|
||||
sparse_pin: Option<&[u8; 32]>,
|
||||
how: (Option<&[u8; 32]>, &VerifyPolicy),
|
||||
) -> Result<Self, KarError> {
|
||||
let (sparse_pin, policy) = how;
|
||||
let mut reader = source.inspection;
|
||||
let checked = verify_archive(&mut reader, &profile, sparse_pin)?;
|
||||
let checked = verify_archive(&mut reader, &profile, (sparse_pin, policy))?;
|
||||
Ok(Self {
|
||||
reader,
|
||||
profile,
|
||||
|
||||
@@ -7,6 +7,7 @@ use super::{
|
||||
};
|
||||
|
||||
type FileIdentity = crate::verify_stamp::FileIdentity;
|
||||
type VerifyPolicy = crate::verify_stamp::VerifyPolicy;
|
||||
|
||||
/// What a successful verification leaves behind.
|
||||
pub(super) struct Checked {
|
||||
@@ -18,9 +19,9 @@ pub(super) struct Checked {
|
||||
}
|
||||
|
||||
/// How the archive is vouched for: a stamp for this very file, or the parent's attestation of a memfd.
|
||||
struct Admission {
|
||||
struct Admission<'p> {
|
||||
identity: Option<FileIdentity>,
|
||||
stamps: Option<&'static crate::verify_stamp::VerifyStamps>,
|
||||
stamps: Option<&'p crate::verify_stamp::VerifyStamps>,
|
||||
attested_memfd: bool,
|
||||
stamped: bool,
|
||||
}
|
||||
@@ -29,14 +30,14 @@ fn current_identity(reader: &KarInspection) -> Option<FileIdentity> {
|
||||
reader.file.metadata().ok().and_then(|m| FileIdentity::of(&m))
|
||||
}
|
||||
|
||||
impl Admission {
|
||||
fn decide(reader: &KarInspection, profile: &KarPleProfile) -> Self {
|
||||
impl<'p> Admission<'p> {
|
||||
fn decide(reader: &KarInspection, profile: &KarPleProfile, policy: &'p VerifyPolicy) -> Self {
|
||||
// A stamp for exactly this file in exactly this state (see verify_stamp.rs) stands in for the full
|
||||
// SHA-256 + CRC pass; a sealed memfd has no stable identity and always takes the full pass.
|
||||
let identity = current_identity(reader);
|
||||
let stamps = crate::verify_stamp::global();
|
||||
// An unlinked sealed memfd: only if the parent attested the copy (see `set_attested_archive`).
|
||||
let attested_memfd = identity.is_none() && crate::verify_stamp::attested_archive() && is_unlinked(&reader.file);
|
||||
let stamps = policy.stamps();
|
||||
// An unlinked sealed memfd: only if the parent attested the copy (see `VerifyPolicy::new`).
|
||||
let attested_memfd = identity.is_none() && policy.archive_attested() && is_unlinked(&reader.file);
|
||||
let stamped = match (stamps, identity.as_ref()) {
|
||||
(Some(s), Some(id)) => s.trusts("kar", &profile.spec.archive_sha256, id),
|
||||
_ => false,
|
||||
@@ -89,10 +90,10 @@ impl Admission {
|
||||
pub(super) fn verify_archive(
|
||||
reader: &mut KarInspection,
|
||||
profile: &KarPleProfile,
|
||||
sparse_pin: Option<&[u8; 32]>,
|
||||
(sparse_pin, policy): (Option<&[u8; 32]>, &VerifyPolicy),
|
||||
) -> Result<Checked, KarError> {
|
||||
check_archive_identity(reader, profile)?;
|
||||
let admission = Admission::decide(reader, profile);
|
||||
let admission = Admission::decide(reader, profile, policy);
|
||||
admission.full_pass(reader, profile, sparse_pin.is_some())?;
|
||||
let (parts, books, book_at_list) = verified_parts(reader, profile)?;
|
||||
let serve_extents = normalize_extents(
|
||||
|
||||
@@ -14,7 +14,7 @@
|
||||
//!
|
||||
//! What a stamp does not defend against: someone who can write both the stamp directory and the model file and
|
||||
//! forge the identity fields. That actor already owns the service account, which also owns the archive the engine
|
||||
//! maps; the stamp directory is created 0700 by the same account.
|
||||
//! maps; the stamp directory is created 0700 by the same account.
|
||||
use std::{
|
||||
fmt::Write as _,
|
||||
fs::{self, File, Metadata},
|
||||
@@ -58,30 +58,33 @@ impl FileIdentity {
|
||||
}
|
||||
}
|
||||
|
||||
static GLOBAL: std::sync::OnceLock<VerifyStamps> = std::sync::OnceLock::new();
|
||||
|
||||
/// Process-wide stamps, set once by the operator binary (`leleka-inference --verify-stamps DIR`). Without it
|
||||
/// nothing is ever skipped.
|
||||
pub fn set_global(stamps: VerifyStamps) -> bool {
|
||||
GLOBAL.set(stamps).is_ok()
|
||||
/// How a start vouches for the files it verifies: the stamp directory (`leleka-inference --verify-stamps DIR`) and the
|
||||
/// parent's attestation of the sealed archive (`--kar-attested-by-stamp`). The operator binary builds it from its
|
||||
/// flags once and hands it to the loaders with the sources; `Default` vouches for nothing, so every file is hashed in
|
||||
/// full.
|
||||
#[derive(Clone, Debug, Default)]
|
||||
pub struct VerifyPolicy {
|
||||
stamps: Option<VerifyStamps>,
|
||||
archive_attested: bool,
|
||||
}
|
||||
|
||||
pub fn global() -> Option<&'static VerifyStamps> {
|
||||
GLOBAL.get()
|
||||
}
|
||||
impl VerifyPolicy {
|
||||
/// `archive_attested`: the parent process (`leleka run`) copied the sealed archive from a file whose stamp
|
||||
/// (device, inode, size, mtime, ctime of the source, kind `kar-source`) matched, and checked that the source did
|
||||
/// not change while it was copied. An unlinked sealed memfd has no identity of its own, so the engine takes the
|
||||
/// parent's word for the content instead of hashing 55 GB again. Never set by default.
|
||||
pub fn new(stamps: Option<VerifyStamps>, archive_attested: bool) -> Self {
|
||||
Self { stamps, archive_attested }
|
||||
}
|
||||
|
||||
static ATTESTED_ARCHIVE: std::sync::atomic::AtomicBool = std::sync::atomic::AtomicBool::new(false);
|
||||
/// The stamps that may stand in for a full hash; `None`: nothing is ever skipped.
|
||||
pub fn stamps(&self) -> Option<&VerifyStamps> {
|
||||
self.stamps.as_ref()
|
||||
}
|
||||
|
||||
/// `leleka-inference --kar-attested-by-stamp`: the parent process (`leleka run`) copied the sealed archive from a
|
||||
/// file whose stamp (device, inode, size, mtime, ctime of the source, kind `kar-source`) matched, and checked that
|
||||
/// the source did not change while it was copied. An unlinked sealed memfd has no identity of its own, so the
|
||||
/// engine takes the parent's word for the content instead of hashing 55 GB again. Never set by default.
|
||||
pub fn set_attested_archive() {
|
||||
ATTESTED_ARCHIVE.store(true, std::sync::atomic::Ordering::Release);
|
||||
}
|
||||
|
||||
pub fn attested_archive() -> bool {
|
||||
ATTESTED_ARCHIVE.load(std::sync::atomic::Ordering::Acquire)
|
||||
pub fn archive_attested(&self) -> bool {
|
||||
self.archive_attested
|
||||
}
|
||||
}
|
||||
|
||||
/// SHA-256 of a whole file, read ahead by a second thread while the first hashes.
|
||||
@@ -291,6 +294,18 @@ mod tests {
|
||||
fs::remove_dir_all(root).unwrap();
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn the_default_policy_vouches_for_nothing() {
|
||||
let policy = VerifyPolicy::default();
|
||||
assert!(policy.stamps().is_none());
|
||||
assert!(!policy.archive_attested());
|
||||
let (root, _file, stamps) = setup("policy");
|
||||
let policy = VerifyPolicy::new(Some(stamps), true);
|
||||
assert_eq!(policy.stamps().map(VerifyStamps::dir), Some(root.join("stamps").as_path()));
|
||||
assert!(policy.archive_attested());
|
||||
fs::remove_dir_all(root).unwrap();
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn a_missing_directory_means_no_stamps() {
|
||||
let stamps = VerifyStamps::new("/nonexistent/leleka/stamps", false);
|
||||
|
||||
Reference in New Issue
Block a user