From e5e7e5fad89d2b54939f4692d04f483e0220ea79 Mon Sep 17 00:00:00 2001 From: "minimal-foundry[bot]" <327719912+minimal-foundry[bot]@users.noreply.github.com> Date: Mon, 19 Oct 2026 20:39:57 +0000 Subject: [PATCH 1/6] feat(bep): append hash-chained min/v1 audit records Add the audit module. A record is built from a Decision alone, which has no field for a credential, an injected header or a body (BEP-039, BEP-040). Each record carries previous_hash, the SHA-256 of the previous line, or the all-zero hash first (BEP-048). The log opens append-only with mode 0600 (BEP-041). A failed append logs one warn line that names the path. Add tracing to the bep dependencies for that warn line. --- Cargo.lock | 1 + crates/bep/Cargo.toml | 1 + crates/bep/src/audit.rs | 552 ++++++++++++++++++++++++++++++++++++++++ crates/bep/src/lib.rs | 1 + 4 files changed, 555 insertions(+) create mode 100644 crates/bep/src/audit.rs diff --git a/Cargo.lock b/Cargo.lock index 3485bbe8cb..243968aa84 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -431,6 +431,7 @@ dependencies = [ "serde_json", "sha2 0.11.0", "thiserror 2.0.21", + "tracing", "zeroize", ] diff --git a/crates/bep/Cargo.toml b/crates/bep/Cargo.toml index 381c6bedcd..f3af0e02da 100644 --- a/crates/bep/Cargo.toml +++ b/crates/bep/Cargo.toml @@ -16,6 +16,7 @@ serde = { workspace = true } serde_json.workspace = true sha2.workspace = true thiserror.workspace = true +tracing.workspace = true zeroize.workspace = true [dev-dependencies] diff --git a/crates/bep/src/audit.rs b/crates/bep/src/audit.rs new file mode 100644 index 0000000000..b261ae6505 --- /dev/null +++ b/crates/bep/src/audit.rs @@ -0,0 +1,552 @@ +//! The proxy's audit log (BEP-039, BEP-040, BEP-041, BEP-048). +//! +//! The log is JSONL under the proxy's state directory: one line for each +//! decision, in the `min/v1` audit schema. The proxy is the log's only +//! writer. +//! +//! A record is built from a [`Decision`] alone. A `Decision` has no field +//! for a credential, an injected header value or a request body, so a record +//! cannot carry one (BEP-040). +//! +//! Each record carries `previous_hash`: the lowercase hex SHA-256 of the +//! previous record's line, as UTF-8 JSON bytes without the trailing newline. +//! The first record of a log carries the all-zero hash (BEP-048). The hash +//! covers the whole previous line, its own `previous_hash` included. The +//! chain functions ([`line_hash`], [`Record::from_decision`], +//! [`Record::to_line`]) are pure over bytes, and separate from the file +//! writer [`AuditLog`]. +//! +//! [`AuditLog::open`] opens the file for append only, never for write or +//! truncate, so it cannot modify or remove an existing record (BEP-041). + +use std::fs::{self, File, OpenOptions}; +use std::io::{self, Write as _}; +use std::path::{Path, PathBuf}; + +use serde::{Deserialize, Serialize}; +use sha2::{Digest as _, Sha256}; + +/// The audit schema every record names. +pub const SCHEMA: &str = "min/v1"; + +/// The identifier a record carries when the request names no member or +/// store identifier. +pub const NONE: &str = "none"; + +/// The resource and permission a record carries when the module maps none. +pub const MODULE_UNMAPPED: &str = "module_unmapped"; + +/// The `previous_hash` of the first record of a log. +pub const ZERO_HASH: &str = "0000000000000000000000000000000000000000000000000000000000000000"; + +/// What a record is about. `kind` tells a proxy decision from the other +/// events of a box's trail. +#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, Serialize, Deserialize)] +#[serde(rename_all = "snake_case")] +pub enum Kind { + /// The proxy admitted or refused a request. + Decision, + /// The client minted a member or a store handle. + Mint, + /// A live box was destroyed and its sealed values revoked. + Revocation, +} + +/// The outcome of a decision. +#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, Serialize, Deserialize)] +#[serde(rename_all = "snake_case")] +pub enum Outcome { + /// The proxy forwarded the request. + Admit, + /// The proxy refused the request. + Refuse, +} + +/// The resource and permission the module mapped the request to. +#[derive(Debug, Clone, PartialEq, Eq, Hash)] +pub enum Mapping { + /// The module mapped the request to this resource and permission. + Mapped { + /// The mapped resource. + resource: String, + /// The mapped permission. + permission: String, + }, + /// The module maps no resource for the request. + ModuleUnmapped, +} + +/// The BEP-039 field set of one decision, and nothing more. +#[derive(Debug, Clone, PartialEq, Eq, Hash)] +pub struct Decision { + /// What the record is about. + pub kind: Kind, + /// The subject box's id. + pub box_id: String, + /// The upstream authority, `host:port`. + pub authority: String, + /// The member or store identifier, or `None` when the request names + /// none (an off-module or unsealed request). + pub identifier: Option, + /// The mapped resource and permission. + pub mapping: Mapping, + /// Admit or refuse. + pub outcome: Outcome, + /// The marker, such as `off_module`, when the decision carries one. + pub marker: Option, +} + +/// One serialized audit record. The field order is the line's byte order. +#[derive(Debug, Clone, PartialEq, Eq, Hash, Serialize, Deserialize)] +pub struct Record { + /// Always [`SCHEMA`]. + pub schema: String, + /// What the record is about. + pub kind: Kind, + /// The subject box's id. + pub subject: String, + /// The acting identity. Empty on an un-enrolled host. + pub act: String, + /// The transaction id. Empty on an un-enrolled host. + pub txn: String, + /// The upstream authority. + pub upstream: String, + /// The member or store identifier, or [`NONE`]. + pub identifier: String, + /// The mapped resource, or [`MODULE_UNMAPPED`]. + pub resource: String, + /// The mapped permission, or [`MODULE_UNMAPPED`]. + pub permission: String, + /// Admit or refuse. + pub decision: Outcome, + /// The marker, or null. + pub marker: Option, + /// The hash of the previous record's line (BEP-048). + pub previous_hash: String, +} + +impl Record { + /// Builds the record for `decision`, chained to `previous_hash`. + pub fn from_decision(decision: &Decision, previous_hash: &str) -> Self { + let (resource, permission) = match &decision.mapping { + Mapping::Mapped { + resource, + permission, + } => (resource.clone(), permission.clone()), + Mapping::ModuleUnmapped => (MODULE_UNMAPPED.to_owned(), MODULE_UNMAPPED.to_owned()), + }; + Self { + schema: SCHEMA.to_owned(), + kind: decision.kind, + subject: decision.box_id.clone(), + act: String::new(), + txn: String::new(), + upstream: decision.authority.clone(), + identifier: decision + .identifier + .clone() + .unwrap_or_else(|| NONE.to_owned()), + resource, + permission, + decision: decision.outcome, + marker: decision.marker.clone(), + previous_hash: previous_hash.to_owned(), + } + } + + /// The record's line: compact JSON, without the trailing newline. + pub fn to_line(&self) -> String { + // A struct of strings and unit enums always serializes. + serde_json::to_string(self).expect("an audit record serializes") + } +} + +/// The lowercase hex SHA-256 of `line`, the bytes without the trailing +/// newline. +pub fn line_hash(line: &str) -> String { + let digest = Sha256::digest(line.as_bytes()); + let mut out = String::with_capacity(64); + for byte in digest { + out.push(char::from_digit(u32::from(byte >> 4), 16).unwrap_or('0')); + out.push(char::from_digit(u32::from(byte & 0xf), 16).unwrap_or('0')); + } + out +} + +/// The hash the next record of the log in `contents` carries: the hash of +/// its last line, or [`ZERO_HASH`] for an empty log. +pub fn head_hash(contents: &str) -> String { + contents + .lines() + .rev() + .find(|line| !line.is_empty()) + .map_or_else(|| ZERO_HASH.to_owned(), line_hash) +} + +/// The audit log, open for append only. +#[derive(Debug)] +pub struct AuditLog { + path: PathBuf, + file: File, + head: String, +} + +impl AuditLog { + /// Opens the log at `path` for append, and creates it and its parent + /// directory when absent. The next record chains to the log's last line. + pub fn open(path: impl AsRef) -> io::Result { + let path = path.as_ref().to_path_buf(); + if let Some(parent) = path.parent() { + fs::create_dir_all(parent)?; + } + let mut options = OpenOptions::new(); + options.append(true).create(true); + #[cfg(unix)] + { + use std::os::unix::fs::OpenOptionsExt as _; + options.mode(0o600); + } + let file = options.open(&path)?; + let head = head_hash(&fs::read_to_string(&path)?); + Ok(Self { path, file, head }) + } + + /// The log's path. + pub fn path(&self) -> &Path { + &self.path + } + + /// The hash the next record carries as `previous_hash`. + pub fn head(&self) -> &str { + &self.head + } + + /// Appends one record for `decision` and returns it. On failure the + /// head does not move, and one warn line names the path. + pub fn append(&mut self, decision: &Decision) -> io::Result { + let record = Record::from_decision(decision, &self.head); + let line = record.to_line(); + let mut bytes = Vec::with_capacity(line.len() + 1); + bytes.extend_from_slice(line.as_bytes()); + bytes.push(b'\n'); + if let Err(err) = self.file.write_all(&bytes).and_then(|()| self.file.flush()) { + tracing::warn!(path = %self.path.display(), error = %err, "audit append failed"); + return Err(err); + } + self.head = line_hash(&line); + Ok(record) + } +} + +#[cfg(test)] +mod tests { + use super::*; + use proptest::prelude::*; + use std::sync::atomic::{AtomicU32, Ordering}; + + struct TempDir(PathBuf); + + impl TempDir { + fn new() -> Self { + static NEXT: AtomicU32 = AtomicU32::new(0); + let dir = std::env::temp_dir().join(format!( + "bep-audit-{}-{}", + std::process::id(), + NEXT.fetch_add(1, Ordering::Relaxed) + )); + let _ = fs::remove_dir_all(&dir); + Self(dir) + } + + fn log(&self) -> PathBuf { + self.0.join("state").join("audit.jsonl") + } + } + + impl Drop for TempDir { + fn drop(&mut self) { + let _ = fs::remove_dir_all(&self.0); + } + } + + fn admit() -> Decision { + Decision { + kind: Kind::Decision, + box_id: "box-7f3a".into(), + authority: "api.github.com:443".into(), + identifier: Some("github:user-token".into()), + mapping: Mapping::Mapped { + resource: "repo:gominimal/minimal".into(), + permission: "contents:read".into(), + }, + outcome: Outcome::Admit, + marker: None, + } + } + + fn off_module() -> Decision { + Decision { + kind: Kind::Decision, + box_id: "box-7f3a".into(), + authority: "example.com:443".into(), + identifier: None, + mapping: Mapping::ModuleUnmapped, + outcome: Outcome::Refuse, + marker: Some("off_module".into()), + } + } + + fn lines(path: &Path) -> Vec { + fs::read_to_string(path) + .unwrap() + .lines() + .map(str::to_owned) + .collect() + } + + // BEP-039: one `min/v1` line for each admit or refuse, with the F12 + // fields and `act` and `txn` empty. + #[test] + fn every_decision_appends_one_audit_record() { + let dir = TempDir::new(); + let mut log = AuditLog::open(dir.log()).unwrap(); + let decisions = [admit(), off_module(), admit()]; + for (n, decision) in decisions.iter().enumerate() { + log.append(decision).unwrap(); + assert_eq!(lines(&dir.log()).len(), n + 1); + } + + let written = lines(&dir.log()); + let first: serde_json::Value = serde_json::from_str(&written[0]).unwrap(); + assert_eq!(first["schema"], "min/v1"); + assert_eq!(first["kind"], "decision"); + assert_eq!(first["subject"], "box-7f3a"); + assert_eq!(first["act"], ""); + assert_eq!(first["txn"], ""); + assert_eq!(first["upstream"], "api.github.com:443"); + assert_eq!(first["identifier"], "github:user-token"); + assert_eq!(first["resource"], "repo:gominimal/minimal"); + assert_eq!(first["permission"], "contents:read"); + assert_eq!(first["decision"], "admit"); + assert!(first["marker"].is_null()); + + let second: serde_json::Value = serde_json::from_str(&written[1]).unwrap(); + assert_eq!(second["upstream"], "example.com:443"); + assert_eq!(second["identifier"], "none"); + assert_eq!(second["resource"], "module_unmapped"); + assert_eq!(second["permission"], "module_unmapped"); + assert_eq!(second["decision"], "refuse"); + assert_eq!(second["marker"], "off_module"); + + let keys: Vec<_> = first.as_object().unwrap().keys().cloned().collect(); + let mut expected = [ + "schema", + "kind", + "subject", + "act", + "txn", + "upstream", + "identifier", + "resource", + "permission", + "decision", + "marker", + "previous_hash", + ] + .map(String::from) + .to_vec(); + expected.sort(); + let mut keys = keys; + keys.sort(); + assert_eq!(keys, expected); + } + + // BEP-048: the first record carries the all-zero hash, and each later + // one the SHA-256 of the previous line without its newline, across a + // reopen too. + #[test] + fn audit_record_carries_previous_hash() { + let dir = TempDir::new(); + let mut log = AuditLog::open(dir.log()).unwrap(); + assert_eq!(log.head(), ZERO_HASH); + log.append(&admit()).unwrap(); + log.append(&off_module()).unwrap(); + drop(log); + let mut log = AuditLog::open(dir.log()).unwrap(); + log.append(&admit()).unwrap(); + + let written = lines(&dir.log()); + assert_eq!(written.len(), 3); + let records: Vec = written + .iter() + .map(|line| serde_json::from_str(line).unwrap()) + .collect(); + assert_eq!(records[0].previous_hash, ZERO_HASH); + for n in 1..records.len() { + let expected: String = Sha256::digest(written[n - 1].as_bytes()) + .iter() + .map(|b| format!("{b:02x}")) + .collect(); + assert_eq!(records[n].previous_hash, expected); + } + assert_eq!(log.head(), line_hash(&written[2])); + // The line is the record's own serialization, byte for byte. + assert_eq!(records[2].to_line(), written[2]); + } + + // BEP-041: the descriptor carries O_APPEND and no truncate, and earlier + // records stay byte for byte across reopens. + #[test] + fn audit_log_is_opened_append_only() { + let dir = TempDir::new(); + let mut log = AuditLog::open(dir.log()).unwrap(); + log.append(&admit()).unwrap(); + log.append(&off_module()).unwrap(); + let before = fs::read(dir.log()).unwrap(); + + #[cfg(target_os = "linux")] + { + use std::os::fd::AsRawFd as _; + let fdinfo = + fs::read_to_string(format!("/proc/self/fdinfo/{}", log.file.as_raw_fd())).unwrap(); + let flags = fdinfo + .lines() + .find_map(|l| l.strip_prefix("flags:")) + .map(|f| u32::from_str_radix(f.trim(), 8).unwrap()) + .unwrap(); + const O_ACCMODE: u32 = 0o3; + const O_WRONLY: u32 = 0o1; + const O_APPEND: u32 = 0o2000; + assert_ne!(flags & O_APPEND, 0, "audit log is not opened O_APPEND"); + assert_eq!( + flags & O_ACCMODE, + O_WRONLY, + "audit log is readable or not write-only" + ); + } + drop(log); + + let mut log = AuditLog::open(dir.log()).unwrap(); + assert_eq!(fs::read(dir.log()).unwrap(), before, "open changed the log"); + log.append(&admit()).unwrap(); + let after = fs::read(dir.log()).unwrap(); + assert!(after.len() > before.len()); + assert_eq!(&after[..before.len()], &before[..]); + + #[cfg(unix)] + { + use std::os::unix::fs::PermissionsExt as _; + let mode = fs::metadata(dir.log()).unwrap().permissions().mode(); + assert_eq!(mode & 0o777, 0o600); + } + } + + // A request as the proxy sees it: the BEP-039 fields plus the secrets + // a record must never carry. + #[derive(Debug, Clone)] + struct Request { + decision: Decision, + credential: String, + injected_header: String, + body: Vec, + } + + fn field() -> impl Strategy { + "[a-z0-9:./-]{1,24}" + } + + fn decision_strategy() -> impl Strategy { + ( + prop_oneof![ + Just(Kind::Decision), + Just(Kind::Mint), + Just(Kind::Revocation) + ], + field(), + field(), + proptest::option::of(field()), + proptest::option::of((field(), field())), + any::(), + proptest::option::of(field()), + ) + .prop_map( + |(kind, box_id, authority, identifier, mapping, admit, marker)| Decision { + kind, + box_id, + authority, + identifier, + mapping: mapping.map_or(Mapping::ModuleUnmapped, |(resource, permission)| { + Mapping::Mapped { + resource, + permission, + } + }), + outcome: if admit { + Outcome::Admit + } else { + Outcome::Refuse + }, + marker, + }, + ) + } + + fn request_strategy() -> impl Strategy { + ( + decision_strategy(), + "ghu_[A-Z]{36}", + "Bearer [A-Z]{32}", + proptest::collection::vec(any::(), 0..256), + ) + .prop_map(|(decision, credential, injected_header, body)| Request { + decision, + credential, + injected_header, + body, + }) + } + + proptest! { + // BEP-040: two requests that differ only in body give the same record + // apart from the chain field, and no record carries the credential, + // the injected header value or the body. + #[test] + fn prop_audit_records_never_contain_secrets( + request in request_strategy(), + other_body in proptest::collection::vec(any::(), 0..256), + other_credential in "ghu_[A-Z]{36}", + ) { + let dir = TempDir::new(); + let mut log = AuditLog::open(dir.log()).unwrap(); + let other = Request { + body: other_body, + credential: other_credential, + ..request.clone() + }; + let first = log.append(&request.decision).unwrap(); + let second = log.append(&other.decision).unwrap(); + prop_assert_eq!( + Record { previous_hash: String::new(), ..first }, + Record { previous_hash: String::new(), ..second } + ); + + let contents = fs::read_to_string(dir.log()).unwrap(); + for secret in [ + &request.credential, + &request.injected_header, + &other.credential, + ] { + prop_assert!(!contents.contains(secret.as_str())); + } + // The secrets' alphabet is upper case, and every BEP-039 field + // here is lower case, so any upper-case run of 32 came from one. + prop_assert!(!contents.contains("ghu_")); + prop_assert!(!contents.contains("Bearer")); + for body in [&request.body, &other.body] { + if body.len() >= 16 { + prop_assert!(!contents.as_bytes().windows(body.len()).any(|w| w == &body[..])); + } + } + } + } +} diff --git a/crates/bep/src/lib.rs b/crates/bep/src/lib.rs index 06771c0361..aadf9175b7 100644 --- a/crates/bep/src/lib.rs +++ b/crates/bep/src/lib.rs @@ -11,5 +11,6 @@ //! and the current set. A member sealed under an old host-set version still //! opens. +pub mod audit; pub mod module; pub mod seal; From 7296ce9d25710194eb35b6c812d092d05543d4e6 Mon Sep 17 00:00:00 2001 From: "minimal-foundry[bot]" <327719912+minimal-foundry[bot]@users.noreply.github.com> Date: Mon, 19 Oct 2026 22:58:13 +0000 Subject: [PATCH 2/6] fix(bep): name the audit schema and guard the log against torn lines Records now carry "min/v1/audit", as other min/v1 documents name their type. A failed append cuts its partial bytes, open cuts a torn tail, and open takes an exclusive lock and refuses a symlink. --- crates/bep/src/audit.rs | 124 ++++++++++++++++++++++++++++++++++++++-- 1 file changed, 118 insertions(+), 6 deletions(-) diff --git a/crates/bep/src/audit.rs b/crates/bep/src/audit.rs index b261ae6505..5225e764f8 100644 --- a/crates/bep/src/audit.rs +++ b/crates/bep/src/audit.rs @@ -18,6 +18,10 @@ //! //! [`AuditLog::open`] opens the file for append only, never for write or //! truncate, so it cannot modify or remove an existing record (BEP-041). +//! The only bytes the log ever cuts are a torn tail: the part of a line +//! that a failed or interrupted append left without its newline. Such bytes +//! never formed a record. [`AuditLog::open`] also takes an exclusive lock, +//! so a second writer on the same path cannot fork the chain. use std::fs::{self, File, OpenOptions}; use std::io::{self, Write as _}; @@ -27,7 +31,7 @@ use serde::{Deserialize, Serialize}; use sha2::{Digest as _, Sha256}; /// The audit schema every record names. -pub const SCHEMA: &str = "min/v1"; +pub const SCHEMA: &str = "min/v1/audit"; /// The identifier a record carries when the request names no member or /// store identifier. @@ -189,16 +193,35 @@ pub struct AuditLog { path: PathBuf, file: File, head: String, + /// The log's length after the last complete record. + len: u64, + /// Set when a failed append left bytes the log was not able to cut. + poisoned: bool, } impl AuditLog { /// Opens the log at `path` for append, and creates it and its parent /// directory when absent. The next record chains to the log's last line. + /// + /// The call fails when `path` is a symlink, or when another `AuditLog` + /// holds the log open. A torn last line, which an interrupted append + /// left without its newline, is cut off. pub fn open(path: impl AsRef) -> io::Result { let path = path.as_ref().to_path_buf(); if let Some(parent) = path.parent() { fs::create_dir_all(parent)?; } + match fs::symlink_metadata(&path) { + Ok(meta) if meta.file_type().is_symlink() => { + return Err(io::Error::new( + io::ErrorKind::InvalidInput, + format!("audit log {} is a symlink", path.display()), + )); + } + Ok(_) => {} + Err(err) if err.kind() == io::ErrorKind::NotFound => {} + Err(err) => return Err(err), + } let mut options = OpenOptions::new(); options.append(true).create(true); #[cfg(unix)] @@ -207,8 +230,35 @@ impl AuditLog { options.mode(0o600); } let file = options.open(&path)?; - let head = head_hash(&fs::read_to_string(&path)?); - Ok(Self { path, file, head }) + // One writer per log: a second one would chain to the same head. + file.try_lock().map_err(|err| match err { + fs::TryLockError::WouldBlock => io::Error::new( + io::ErrorKind::WouldBlock, + format!("audit log {} is held by another writer", path.display()), + ), + fs::TryLockError::Error(err) => err, + })?; + let contents = fs::read(&path)?; + let complete = contents + .iter() + .rposition(|&b| b == b'\n') + .map_or(0, |n| n + 1); + if complete < contents.len() { + tracing::warn!( + path = %path.display(), + torn_bytes = contents.len() - complete, + "audit log ends in a torn line, cutting it off" + ); + file.set_len(complete as u64)?; + } + let head = head_hash(&String::from_utf8_lossy(&contents[..complete])); + Ok(Self { + path, + file, + head, + len: complete as u64, + poisoned: false, + }) } /// The log's path. @@ -222,17 +272,44 @@ impl AuditLog { } /// Appends one record for `decision` and returns it. On failure the - /// head does not move, and one warn line names the path. + /// head does not move, the partial bytes are cut off, and one warn line + /// names the path. When the cut fails too, every later append fails. pub fn append(&mut self, decision: &Decision) -> io::Result { + if self.poisoned { + return Err(io::Error::other(format!( + "audit log {} holds a torn line, reopen it", + self.path.display() + ))); + } let record = Record::from_decision(decision, &self.head); let line = record.to_line(); let mut bytes = Vec::with_capacity(line.len() + 1); bytes.extend_from_slice(line.as_bytes()); bytes.push(b'\n'); - if let Err(err) = self.file.write_all(&bytes).and_then(|()| self.file.flush()) { + // One write call, so a short write shows as a short count. + let written = self.file.write(&bytes).and_then(|n| { + if n == bytes.len() { + self.file.flush() + } else { + Err(io::Error::new( + io::ErrorKind::WriteZero, + format!("short write: {n} of {} bytes", bytes.len()), + )) + } + }); + if let Err(err) = written { tracing::warn!(path = %self.path.display(), error = %err, "audit append failed"); + if let Err(cut) = self.file.set_len(self.len) { + tracing::warn!( + path = %self.path.display(), + error = %cut, + "audit log keeps a torn line, later appends fail until reopen" + ); + self.poisoned = true; + } return Err(err); } + self.len += bytes.len() as u64; self.head = line_hash(&line); Ok(record) } @@ -318,7 +395,7 @@ mod tests { let written = lines(&dir.log()); let first: serde_json::Value = serde_json::from_str(&written[0]).unwrap(); - assert_eq!(first["schema"], "min/v1"); + assert_eq!(first["schema"], "min/v1/audit"); assert_eq!(first["kind"], "decision"); assert_eq!(first["subject"], "box-7f3a"); assert_eq!(first["act"], ""); @@ -394,6 +471,41 @@ mod tests { assert_eq!(records[2].to_line(), written[2]); } + // BEP-048: a torn tail from an interrupted append is cut off on open, + // so the next record starts on its own line and chains to the last + // complete one. + #[test] + fn audit_log_cuts_a_torn_last_line() { + let dir = TempDir::new(); + let mut log = AuditLog::open(dir.log()).unwrap(); + log.append(&admit()).unwrap(); + let head = log.head().to_owned(); + drop(log); + let mut file = OpenOptions::new().append(true).open(dir.log()).unwrap(); + file.write_all(b"{\"schema\":\"min/v1/au").unwrap(); + drop(file); + + let mut log = AuditLog::open(dir.log()).unwrap(); + assert_eq!(log.head(), head); + log.append(&off_module()).unwrap(); + let written = lines(&dir.log()); + assert_eq!(written.len(), 2); + let second: Record = serde_json::from_str(&written[1]).unwrap(); + assert_eq!(second.previous_hash, line_hash(&written[0])); + } + + // BEP-048: a second writer on the same log would fork the chain, so + // open refuses it while the first one holds the log. + #[test] + fn audit_log_refuses_a_second_writer() { + let dir = TempDir::new(); + let log = AuditLog::open(dir.log()).unwrap(); + let err = AuditLog::open(dir.log()).unwrap_err(); + assert_eq!(err.kind(), io::ErrorKind::WouldBlock); + drop(log); + AuditLog::open(dir.log()).unwrap(); + } + // BEP-041: the descriptor carries O_APPEND and no truncate, and earlier // records stay byte for byte across reopens. #[test] From c1c04e92b2fb1849e64f31db6149407babfa96ec Mon Sep 17 00:00:00 2001 From: "minimal-foundry[bot]" <327719912+minimal-foundry[bot]@users.noreply.github.com> Date: Mon, 19 Oct 2026 23:05:21 +0000 Subject: [PATCH 3/6] fix(bep): check the opened audit log, hash raw line bytes Check the open file against the path's own entry after the open, so a symlink swapped in before the open is refused. Hash the last line's raw bytes on open, so a non-UTF-8 line chains as it is on disk. State plainly that Decision strings are free text. --- crates/bep/src/audit.rs | 92 ++++++++++++++++++++++++++++++++--------- 1 file changed, 73 insertions(+), 19 deletions(-) diff --git a/crates/bep/src/audit.rs b/crates/bep/src/audit.rs index 5225e764f8..3385829514 100644 --- a/crates/bep/src/audit.rs +++ b/crates/bep/src/audit.rs @@ -5,8 +5,12 @@ //! writer. //! //! A record is built from a [`Decision`] alone. A `Decision` has no field -//! for a credential, an injected header value or a request body, so a record -//! cannot carry one (BEP-040). +//! for a credential, an injected header value or a request body (BEP-040). +//! Its string fields are free text, so the type alone does not stop a +//! caller from putting a secret in one. The proxy fills them from the box +//! id, the request authority and the module's mapping, and never from a +//! sealed value or a header. A charset or length check cannot tell a token +//! from an identifier, so this module does not try. //! //! Each record carries `previous_hash`: the lowercase hex SHA-256 of the //! previous record's line, as UTF-8 JSON bytes without the trailing newline. @@ -167,8 +171,8 @@ impl Record { /// The lowercase hex SHA-256 of `line`, the bytes without the trailing /// newline. -pub fn line_hash(line: &str) -> String { - let digest = Sha256::digest(line.as_bytes()); +pub fn line_hash(line: impl AsRef<[u8]>) -> String { + let digest = Sha256::digest(line.as_ref()); let mut out = String::with_capacity(64); for byte in digest { out.push(char::from_digit(u32::from(byte >> 4), 16).unwrap_or('0')); @@ -178,15 +182,46 @@ pub fn line_hash(line: &str) -> String { } /// The hash the next record of the log in `contents` carries: the hash of -/// its last line, or [`ZERO_HASH`] for an empty log. -pub fn head_hash(contents: &str) -> String { +/// its last line, or [`ZERO_HASH`] for an empty log. The hash covers the +/// raw bytes of the line, so a line that is not valid UTF-8 hashes as it +/// is on disk. +pub fn head_hash(contents: impl AsRef<[u8]>) -> String { contents - .lines() + .as_ref() + .split(|&b| b == b'\n') .rev() + .map(|line| line.strip_suffix(b"\r").unwrap_or(line)) .find(|line| !line.is_empty()) .map_or_else(|| ZERO_HASH.to_owned(), line_hash) } +/// Fails unless `file` is a regular file and is the entry at `path` itself, +/// not a file that a symlink at `path` leads to. +fn ensure_own_file(path: &Path, file: &File) -> io::Result<()> { + let refuse = |why: &str| { + Err(io::Error::new( + io::ErrorKind::InvalidInput, + format!("audit log {} {why}", path.display()), + )) + }; + let entry = fs::symlink_metadata(path)?; + if entry.file_type().is_symlink() { + return refuse("is a symlink"); + } + let opened = file.metadata()?; + if !opened.is_file() { + return refuse("is not a regular file"); + } + #[cfg(unix)] + { + use std::os::unix::fs::MetadataExt as _; + if (entry.dev(), entry.ino()) != (opened.dev(), opened.ino()) { + return refuse("changed while it was opened"); + } + } + Ok(()) +} + /// The audit log, open for append only. #[derive(Debug)] pub struct AuditLog { @@ -211,17 +246,6 @@ impl AuditLog { if let Some(parent) = path.parent() { fs::create_dir_all(parent)?; } - match fs::symlink_metadata(&path) { - Ok(meta) if meta.file_type().is_symlink() => { - return Err(io::Error::new( - io::ErrorKind::InvalidInput, - format!("audit log {} is a symlink", path.display()), - )); - } - Ok(_) => {} - Err(err) if err.kind() == io::ErrorKind::NotFound => {} - Err(err) => return Err(err), - } let mut options = OpenOptions::new(); options.append(true).create(true); #[cfg(unix)] @@ -230,6 +254,9 @@ impl AuditLog { options.mode(0o600); } let file = options.open(&path)?; + // Checked after the open, against the open file, so a link swapped + // in between a check and the open cannot slip through. + ensure_own_file(&path, &file)?; // One writer per log: a second one would chain to the same head. file.try_lock().map_err(|err| match err { fs::TryLockError::WouldBlock => io::Error::new( @@ -251,7 +278,7 @@ impl AuditLog { ); file.set_len(complete as u64)?; } - let head = head_hash(&String::from_utf8_lossy(&contents[..complete])); + let head = head_hash(&contents[..complete]); Ok(Self { path, file, @@ -469,6 +496,20 @@ mod tests { assert_eq!(log.head(), line_hash(&written[2])); // The line is the record's own serialization, byte for byte. assert_eq!(records[2].to_line(), written[2]); + drop(log); + + // A last line that is not UTF-8 chains by its raw bytes. + let raw = b"{\"subject\":\"\xff\xfe\"}"; + let mut file = OpenOptions::new().append(true).open(dir.log()).unwrap(); + file.write_all(raw).unwrap(); + file.write_all(b"\n").unwrap(); + drop(file); + let log = AuditLog::open(dir.log()).unwrap(); + let expected: String = Sha256::digest(raw) + .iter() + .map(|b| format!("{b:02x}")) + .collect(); + assert_eq!(log.head(), expected); } // BEP-048: a torn tail from an interrupted append is cut off on open, @@ -551,6 +592,19 @@ mod tests { let mode = fs::metadata(dir.log()).unwrap().permissions().mode(); assert_eq!(mode & 0o777, 0o600); } + drop(log); + + // A symlink at the log's path is refused, and its target is kept. + #[cfg(unix)] + { + let target = dir.0.join("elsewhere"); + fs::write(&target, b"keep\n").unwrap(); + let link = dir.0.join("state").join("linked.jsonl"); + std::os::unix::fs::symlink(&target, &link).unwrap(); + let err = AuditLog::open(&link).unwrap_err(); + assert_eq!(err.kind(), io::ErrorKind::InvalidInput); + assert_eq!(fs::read(&target).unwrap(), b"keep\n"); + } } // A request as the proxy sees it: the BEP-039 fields plus the secrets From 974985eb39b625285681070fdf950dedad7511c2 Mon Sep 17 00:00:00 2001 From: "minimal-foundry[bot]" <327719912+minimal-foundry[bot]@users.noreply.github.com> Date: Mon, 19 Oct 2026 23:45:56 +0000 Subject: [PATCH 4/6] fix(bep): refuse a non-file audit path before open, bound fields Check the path with symlink_metadata before the open too, so a symlink target is never created and a FIFO never blocks the open. Cut each record string field to MAX_FIELD_BYTES at a char boundary. --- crates/bep/src/audit.rs | 74 +++++++++++++++++++++++++++++++++++++---- 1 file changed, 67 insertions(+), 7 deletions(-) diff --git a/crates/bep/src/audit.rs b/crates/bep/src/audit.rs index 3385829514..cbe61575c1 100644 --- a/crates/bep/src/audit.rs +++ b/crates/bep/src/audit.rs @@ -10,7 +10,8 @@ //! caller from putting a secret in one. The proxy fills them from the box //! id, the request authority and the module's mapping, and never from a //! sealed value or a header. A charset or length check cannot tell a token -//! from an identifier, so this module does not try. +//! from an identifier, so this module does not try. It does cut each field +//! to [`MAX_FIELD_BYTES`], so no one field can make a record large. //! //! Each record carries `previous_hash`: the lowercase hex SHA-256 of the //! previous record's line, as UTF-8 JSON bytes without the trailing newline. @@ -44,6 +45,20 @@ pub const NONE: &str = "none"; /// The resource and permission a record carries when the module maps none. pub const MODULE_UNMAPPED: &str = "module_unmapped"; +/// The most bytes a record keeps of one string field. A longer value is +/// cut at a character boundary, so a box that controls the request +/// authority cannot make one record arbitrarily large. +pub const MAX_FIELD_BYTES: usize = 512; + +/// `value`, cut to at most [`MAX_FIELD_BYTES`] at a character boundary. +fn bounded(value: &str) -> String { + let mut end = value.len().min(MAX_FIELD_BYTES); + while !value.is_char_boundary(end) { + end -= 1; + } + value[..end].to_owned() +} + /// The `previous_hash` of the first record of a log. pub const ZERO_HASH: &str = "0000000000000000000000000000000000000000000000000000000000000000"; @@ -140,24 +155,24 @@ impl Record { Mapping::Mapped { resource, permission, - } => (resource.clone(), permission.clone()), + } => (bounded(resource), bounded(permission)), Mapping::ModuleUnmapped => (MODULE_UNMAPPED.to_owned(), MODULE_UNMAPPED.to_owned()), }; Self { schema: SCHEMA.to_owned(), kind: decision.kind, - subject: decision.box_id.clone(), + subject: bounded(&decision.box_id), act: String::new(), txn: String::new(), - upstream: decision.authority.clone(), + upstream: bounded(&decision.authority), identifier: decision .identifier - .clone() - .unwrap_or_else(|| NONE.to_owned()), + .as_deref() + .map_or_else(|| NONE.to_owned(), bounded), resource, permission, decision: decision.outcome, - marker: decision.marker.clone(), + marker: decision.marker.as_deref().map(bounded), previous_hash: previous_hash.to_owned(), } } @@ -246,6 +261,26 @@ impl AuditLog { if let Some(parent) = path.parent() { fs::create_dir_all(parent)?; } + // Refused before the open as well, so a symlink's target is never + // created and a FIFO never blocks the open. The check after the + // open still catches an entry swapped in between the two. + match fs::symlink_metadata(&path) { + Ok(entry) if entry.file_type().is_symlink() => { + return Err(io::Error::new( + io::ErrorKind::InvalidInput, + format!("audit log {} is a symlink", path.display()), + )); + } + Ok(entry) if !entry.is_file() => { + return Err(io::Error::new( + io::ErrorKind::InvalidInput, + format!("audit log {} is not a regular file", path.display()), + )); + } + Ok(_) => {} + Err(err) if err.kind() == io::ErrorKind::NotFound => {} + Err(err) => return Err(err), + } let mut options = OpenOptions::new(); options.append(true).create(true); #[cfg(unix)] @@ -434,6 +469,17 @@ mod tests { assert_eq!(first["decision"], "admit"); assert!(first["marker"].is_null()); + // A long free-text field is cut to its bound at a char boundary. + let long = Decision { + authority: "é".repeat(MAX_FIELD_BYTES), + identifier: Some("x".repeat(MAX_FIELD_BYTES * 4)), + ..admit() + }; + let record = Record::from_decision(&long, ZERO_HASH); + assert_eq!(record.upstream.len(), MAX_FIELD_BYTES); + assert!(record.upstream.chars().all(|c| c == 'é')); + assert_eq!(record.identifier.len(), MAX_FIELD_BYTES); + let second: serde_json::Value = serde_json::from_str(&written[1]).unwrap(); assert_eq!(second["upstream"], "example.com:443"); assert_eq!(second["identifier"], "none"); @@ -604,6 +650,20 @@ mod tests { let err = AuditLog::open(&link).unwrap_err(); assert_eq!(err.kind(), io::ErrorKind::InvalidInput); assert_eq!(fs::read(&target).unwrap(), b"keep\n"); + + // A dangling symlink is refused, and its target is not created. + let missing = dir.0.join("missing"); + let dangling = dir.0.join("state").join("dangling.jsonl"); + std::os::unix::fs::symlink(&missing, &dangling).unwrap(); + let err = AuditLog::open(&dangling).unwrap_err(); + assert_eq!(err.kind(), io::ErrorKind::InvalidInput); + assert!(!missing.exists(), "open created the symlink's target"); + + // A directory at the path is not a regular file. + let not_file = dir.0.join("state").join("dir.jsonl"); + fs::create_dir(¬_file).unwrap(); + let err = AuditLog::open(¬_file).unwrap_err(); + assert_eq!(err.kind(), io::ErrorKind::InvalidInput); } } From 76af227f0a2427b07a073a210d973c0b36a2ca68 Mon Sep 17 00:00:00 2001 From: "minimal-foundry[bot]" <327719912+minimal-foundry[bot]@users.noreply.github.com> Date: Mon, 19 Oct 2026 23:53:00 +0000 Subject: [PATCH 5/6] fix(bep): read the audit head through the held file, mark cut fields The head hash now comes from the locked descriptor, not a second open of the path. A field over the bound ends in a marker and the full hash. --- crates/bep/src/audit.rs | 82 +++++++++++++++++++++++++++++++++++++---- 1 file changed, 74 insertions(+), 8 deletions(-) diff --git a/crates/bep/src/audit.rs b/crates/bep/src/audit.rs index cbe61575c1..513e632b90 100644 --- a/crates/bep/src/audit.rs +++ b/crates/bep/src/audit.rs @@ -11,7 +11,8 @@ //! id, the request authority and the module's mapping, and never from a //! sealed value or a header. A charset or length check cannot tell a token //! from an identifier, so this module does not try. It does cut each field -//! to [`MAX_FIELD_BYTES`], so no one field can make a record large. +//! to [`MAX_FIELD_BYTES`], so no one field can make a record large. A cut +//! field ends in [`CUT_MARKER`] and the hash of the full value. //! //! Each record carries `previous_hash`: the lowercase hex SHA-256 of the //! previous record's line, as UTF-8 JSON bytes without the trailing newline. @@ -50,13 +51,27 @@ pub const MODULE_UNMAPPED: &str = "module_unmapped"; /// authority cannot make one record arbitrarily large. pub const MAX_FIELD_BYTES: usize = 512; -/// `value`, cut to at most [`MAX_FIELD_BYTES`] at a character boundary. +/// What a cut field ends in, before the hex SHA-256 of the full value. +pub const CUT_MARKER: &str = "…sha256:"; + +/// `value` when it fits in [`MAX_FIELD_BYTES`]. A longer value is cut at a +/// character boundary and ends in [`CUT_MARKER`] and the hex SHA-256 of the +/// full value, so the record shows the cut and two long values with the +/// same prefix stay distinct. The result is never longer than the bound. fn bounded(value: &str) -> String { - let mut end = value.len().min(MAX_FIELD_BYTES); + if value.len() <= MAX_FIELD_BYTES { + return value.to_owned(); + } + let digest = line_hash(value); + let mut end = MAX_FIELD_BYTES - CUT_MARKER.len() - digest.len(); while !value.is_char_boundary(end) { end -= 1; } - value[..end].to_owned() + let mut out = String::with_capacity(MAX_FIELD_BYTES); + out.push_str(&value[..end]); + out.push_str(CUT_MARKER); + out.push_str(&digest); + out } /// The `previous_hash` of the first record of a log. @@ -237,6 +252,37 @@ fn ensure_own_file(path: &Path, file: &File) -> io::Result<()> { Ok(()) } +/// The bytes of the file that `file` holds open, read through a second, +/// read-only handle on the same file and not through `path`. An entry that +/// is swapped in at `path` after the open cannot supply the head hash. +fn read_opened(path: &Path, file: &File) -> io::Result> { + use std::io::Read as _; + // `/proc/self/fd/N` leads to the open file itself, whatever is at + // `path` now. Elsewhere the path is opened again, and the device and + // inode check below refuses a different file. + #[cfg(target_os = "linux")] + let mut reader = { + use std::os::fd::AsRawFd as _; + File::open(format!("/proc/self/fd/{}", file.as_raw_fd()))? + }; + #[cfg(not(target_os = "linux"))] + let mut reader = File::open(path)?; + #[cfg(unix)] + { + use std::os::unix::fs::MetadataExt as _; + let (held, read) = (file.metadata()?, reader.metadata()?); + if (held.dev(), held.ino()) != (read.dev(), read.ino()) { + return Err(io::Error::new( + io::ErrorKind::InvalidInput, + format!("audit log {} changed while it was opened", path.display()), + )); + } + } + let mut contents = Vec::new(); + reader.read_to_end(&mut contents)?; + Ok(contents) +} + /// The audit log, open for append only. #[derive(Debug)] pub struct AuditLog { @@ -300,7 +346,7 @@ impl AuditLog { ), fs::TryLockError::Error(err) => err, })?; - let contents = fs::read(&path)?; + let contents = read_opened(&path, &file)?; let complete = contents .iter() .rposition(|&b| b == b'\n') @@ -476,9 +522,29 @@ mod tests { ..admit() }; let record = Record::from_decision(&long, ZERO_HASH); - assert_eq!(record.upstream.len(), MAX_FIELD_BYTES); - assert!(record.upstream.chars().all(|c| c == 'é')); - assert_eq!(record.identifier.len(), MAX_FIELD_BYTES); + assert!(record.upstream.len() <= MAX_FIELD_BYTES); + let (kept, digest) = record.upstream.split_once(CUT_MARKER).unwrap(); + assert!(!kept.is_empty() && kept.chars().all(|c| c == 'é')); + assert_eq!(digest, line_hash(&long.authority)); + assert!(record.identifier.len() <= MAX_FIELD_BYTES); + assert!(record.identifier.ends_with(&format!( + "{CUT_MARKER}{}", + line_hash(long.identifier.as_ref().unwrap()) + ))); + // Two long values with the same prefix give distinct records. + let other = Decision { + identifier: Some(format!("{}y", "x".repeat(MAX_FIELD_BYTES * 4))), + ..long.clone() + }; + let other = Record::from_decision(&other, ZERO_HASH); + assert_ne!(other.identifier, record.identifier); + // A value at the bound is kept whole, with no marker. + let exact = Decision { + authority: "a".repeat(MAX_FIELD_BYTES), + ..admit() + }; + let exact = Record::from_decision(&exact, ZERO_HASH); + assert_eq!(exact.upstream, "a".repeat(MAX_FIELD_BYTES)); let second: serde_json::Value = serde_json::from_str(&written[1]).unwrap(); assert_eq!(second["upstream"], "example.com:443"); From a82526f55352e30b2d8d908c5abd9cbd431fc88f Mon Sep 17 00:00:00 2001 From: "minimal-foundry[bot]" <327719912+minimal-foundry[bot]@users.noreply.github.com> Date: Tue, 20 Oct 2026 00:28:18 +0000 Subject: [PATCH 6/6] fix(bep): read only the audit tail, tighten log and dir modes Open finds the head from the end of the log, so its cost does not grow with the log. Without /proc the reader falls back to the path and the inode check. Created directories get 0700 and an existing log is set to 0600 through the held descriptor. --- crates/bep/src/audit.rs | 136 ++++++++++++++++++++++++++++++++-------- 1 file changed, 111 insertions(+), 25 deletions(-) diff --git a/crates/bep/src/audit.rs b/crates/bep/src/audit.rs index 513e632b90..ab1126db95 100644 --- a/crates/bep/src/audit.rs +++ b/crates/bep/src/audit.rs @@ -252,21 +252,23 @@ fn ensure_own_file(path: &Path, file: &File) -> io::Result<()> { Ok(()) } -/// The bytes of the file that `file` holds open, read through a second, -/// read-only handle on the same file and not through `path`. An entry that -/// is swapped in at `path` after the open cannot supply the head hash. -fn read_opened(path: &Path, file: &File) -> io::Result> { - use std::io::Read as _; +/// A second, read-only handle on the file that `file` holds open. The +/// handle never leads to an entry swapped in at `path` after the open. +fn reader_for(path: &Path, file: &File) -> io::Result { // `/proc/self/fd/N` leads to the open file itself, whatever is at - // `path` now. Elsewhere the path is opened again, and the device and - // inode check below refuses a different file. + // `path` now. Without `/proc`, and on other systems, the path is opened + // again, and the device and inode check below refuses a different file. #[cfg(target_os = "linux")] - let mut reader = { + let reader = { use std::os::fd::AsRawFd as _; - File::open(format!("/proc/self/fd/{}", file.as_raw_fd()))? + match File::open(format!("/proc/self/fd/{}", file.as_raw_fd())) { + Ok(reader) => reader, + Err(err) if err.kind() == io::ErrorKind::NotFound => File::open(path)?, + Err(err) => return Err(err), + } }; #[cfg(not(target_os = "linux"))] - let mut reader = File::open(path)?; + let reader = File::open(path)?; #[cfg(unix)] { use std::os::unix::fs::MetadataExt as _; @@ -278,9 +280,41 @@ fn read_opened(path: &Path, file: &File) -> io::Result> { )); } } - let mut contents = Vec::new(); - reader.read_to_end(&mut contents)?; - Ok(contents) + Ok(reader) +} + +/// How many bytes [`read_tail`] reads from the end of the log at a time. +const TAIL_CHUNK: u64 = 16 * 1024; + +/// The end of the log that `reader` reads, enough of it to find the last +/// complete line. Returns the offset where the bytes start, and the bytes. +/// The read stops at the first chunk that holds a newline before the last +/// non-empty complete line, so its cost does not grow with the log. +fn read_tail(mut reader: &File) -> io::Result<(u64, Vec)> { + use std::io::{Read as _, Seek as _, SeekFrom}; + let len = reader.metadata()?.len(); + let mut start = len; + let mut tail: Vec = Vec::new(); + while start > 0 { + let step = start.min(TAIL_CHUNK); + start -= step; + let mut chunk = vec![0; step as usize]; + reader.seek(SeekFrom::Start(start))?; + reader.read_exact(&mut chunk)?; + chunk.extend_from_slice(&tail); + tail = chunk; + if let Some(last) = tail.iter().rposition(|&b| b == b'\n') { + let complete = &tail[..last]; + let trimmed = complete + .iter() + .rposition(|&b| b != b'\n' && b != b'\r') + .map_or(&complete[..0], |n| &complete[..=n]); + if trimmed.contains(&b'\n') { + break; + } + } + } + Ok((start, tail)) } /// The audit log, open for append only. @@ -297,7 +331,10 @@ pub struct AuditLog { impl AuditLog { /// Opens the log at `path` for append, and creates it and its parent - /// directory when absent. The next record chains to the log's last line. + /// directory when absent. A directory it creates gets mode 0700, and the + /// log gets mode 0600, also when it exists already. The next record + /// chains to the log's last line, which open finds from the end of the + /// file without a read of the whole log. /// /// The call fails when `path` is a symlink, or when another `AuditLog` /// holds the log open. A torn last line, which an interrupted append @@ -305,7 +342,15 @@ impl AuditLog { pub fn open(path: impl AsRef) -> io::Result { let path = path.as_ref().to_path_buf(); if let Some(parent) = path.parent() { - fs::create_dir_all(parent)?; + // Directories this call creates are the proxy's alone. + let mut dirs = fs::DirBuilder::new(); + dirs.recursive(true); + #[cfg(unix)] + { + use std::os::unix::fs::DirBuilderExt as _; + dirs.mode(0o700); + } + dirs.create(parent)?; } // Refused before the open as well, so a symlink's target is never // created and a FIFO never blocks the open. The check after the @@ -346,25 +391,32 @@ impl AuditLog { ), fs::TryLockError::Error(err) => err, })?; - let contents = read_opened(&path, &file)?; - let complete = contents - .iter() - .rposition(|&b| b == b'\n') - .map_or(0, |n| n + 1); - if complete < contents.len() { + // An existing log keeps the mode it was created with, so tighten it + // through the held descriptor. + #[cfg(unix)] + { + use std::os::unix::fs::PermissionsExt as _; + if file.metadata()?.permissions().mode() & 0o777 != 0o600 { + file.set_permissions(fs::Permissions::from_mode(0o600))?; + } + } + let (start, tail) = read_tail(&reader_for(&path, &file)?)?; + let complete = tail.iter().rposition(|&b| b == b'\n').map_or(0, |n| n + 1); + let len = start + complete as u64; + if complete < tail.len() { tracing::warn!( path = %path.display(), - torn_bytes = contents.len() - complete, + torn_bytes = tail.len() - complete, "audit log ends in a torn line, cutting it off" ); - file.set_len(complete as u64)?; + file.set_len(len)?; } - let head = head_hash(&contents[..complete]); + let head = head_hash(&tail[..complete]); Ok(Self { path, file, head, - len: complete as u64, + len, poisoned: false, }) } @@ -647,6 +699,30 @@ mod tests { assert_eq!(second.previous_hash, line_hash(&written[0])); } + // BEP-048: open finds the head from the end of the log, also when the + // last line starts before the last read chunk. + #[test] + fn audit_log_finds_the_head_past_one_chunk() { + let dir = TempDir::new(); + let mut log = AuditLog::open(dir.log()).unwrap(); + while fs::metadata(dir.log()).unwrap().len() < 3 * TAIL_CHUNK { + log.append(&admit()).unwrap(); + } + let head = log.head().to_owned(); + drop(log); + assert_eq!(AuditLog::open(dir.log()).unwrap().head(), head); + + // A torn tail longer than one chunk is cut off too. + let before = fs::read(dir.log()).unwrap(); + let mut file = OpenOptions::new().append(true).open(dir.log()).unwrap(); + file.write_all(&vec![b'x'; 2 * TAIL_CHUNK as usize]) + .unwrap(); + drop(file); + let log = AuditLog::open(dir.log()).unwrap(); + assert_eq!(log.head(), head); + assert_eq!(fs::read(dir.log()).unwrap(), before); + } + // BEP-048: a second writer on the same log would fork the chain, so // open refuses it while the first one holds the log. #[test] @@ -703,6 +779,16 @@ mod tests { use std::os::unix::fs::PermissionsExt as _; let mode = fs::metadata(dir.log()).unwrap().permissions().mode(); assert_eq!(mode & 0o777, 0o600); + let state = dir.log().parent().unwrap().to_path_buf(); + let mode = fs::metadata(&state).unwrap().permissions().mode(); + assert_eq!(mode & 0o777, 0o700, "open made a wider state directory"); + + // An existing log with a wider mode is tightened on open. + drop(log); + fs::set_permissions(dir.log(), fs::Permissions::from_mode(0o644)).unwrap(); + log = AuditLog::open(dir.log()).unwrap(); + let mode = fs::metadata(dir.log()).unwrap().permissions().mode(); + assert_eq!(mode & 0o777, 0o600, "open kept a wider log mode"); } drop(log);