fix: retain complete histories with bounded journal capacity and native recovery
Rust development slice / linux (push) Failing after 6s
Rust development slice / linux (push) Failing after 6s
This commit is contained in:
@@ -204,6 +204,17 @@ pub fn lock_state(dir: &Path, uid: u32) -> Result<fs::File> {
|
||||
f.try_lock_exclusive()?;
|
||||
Ok(f)
|
||||
}
|
||||
/// Finite 256 MiB bound for durable agent/helper history. Unexpected page sizes
|
||||
/// are refused rather than silently multiplying the byte limit. No compaction
|
||||
/// or tombstone deletion occurs here; SQLITE_FULL must still fail closed.
|
||||
pub fn bounded_journal_capacity(db: &rusqlite::Connection) -> Result<()> {
|
||||
let page_size: u32 = db.query_row("PRAGMA page_size", [], |r| r.get(0))?;
|
||||
ensure!(page_size == 4096, "JOURNAL_PAGE_SIZE_UNSUPPORTED");
|
||||
let maximum: u32 = db.query_row("PRAGMA max_page_count=65536", [], |r| r.get(0))?;
|
||||
ensure!(maximum == 65536, "JOURNAL_STORAGE_CAPACITY_BLOCKED");
|
||||
Ok(())
|
||||
}
|
||||
|
||||
pub fn open_ledger(dir: &Path, uid: u32) -> Result<rusqlite::Connection> {
|
||||
use std::os::unix::fs::OpenOptionsExt;
|
||||
let path = dir.join("consumption.sqlite");
|
||||
@@ -218,7 +229,8 @@ pub fn open_ledger(dir: &Path, uid: u32) -> Result<rusqlite::Connection> {
|
||||
f.sync_all()?;
|
||||
private_path(&path, uid, false)?;
|
||||
let db = rusqlite::Connection::open(path)?;
|
||||
db.execute_batch("PRAGMA synchronous=FULL; PRAGMA journal_mode=DELETE; PRAGMA max_page_count=16384;
|
||||
bounded_journal_capacity(&db)?;
|
||||
db.execute_batch("PRAGMA synchronous=FULL; PRAGMA journal_mode=DELETE;
|
||||
CREATE TABLE IF NOT EXISTS consumed(grant_id TEXT PRIMARY KEY, agent TEXT NOT NULL, generation INTEGER NOT NULL,
|
||||
job TEXT NOT NULL, attempt TEXT NOT NULL, result BLOB, UNIQUE(agent,generation,job,attempt));
|
||||
CREATE TABLE IF NOT EXISTS execution_evidence(grant_id TEXT PRIMARY KEY, start BLOB NOT NULL, outcome BLOB);")?;
|
||||
|
||||
@@ -472,7 +472,30 @@ fn handle(
|
||||
stream.write_all(&bytes)?;
|
||||
Ok(())
|
||||
}
|
||||
fn safe_error_code(error: &anyhow::Error) -> String {
|
||||
if error.chain().any(|cause| {
|
||||
matches!(cause.downcast_ref::<rusqlite::Error>(),
|
||||
Some(rusqlite::Error::SqliteFailure(code, _)) if code.code == rusqlite::ErrorCode::DiskFull)
|
||||
}) {
|
||||
return "JOURNAL_STORAGE_CAPACITY_BLOCKED".into();
|
||||
}
|
||||
let text = error.to_string();
|
||||
if text.len() <= 96
|
||||
&& text
|
||||
.bytes()
|
||||
.all(|b| b.is_ascii_uppercase() || b.is_ascii_digit() || b == b'_')
|
||||
{
|
||||
text
|
||||
} else {
|
||||
"HELPER_VALIDATION_OR_IO_FAILURE".into()
|
||||
}
|
||||
}
|
||||
|
||||
fn main() -> Result<()> {
|
||||
run().map_err(|error| anyhow::anyhow!(safe_error_code(&error)))
|
||||
}
|
||||
|
||||
fn run() -> Result<()> {
|
||||
let args: Vec<_> = std::env::args_os().skip(1).collect();
|
||||
if args == [std::ffi::OsString::from("--help")] {
|
||||
println!(
|
||||
@@ -607,16 +630,7 @@ fn main() -> Result<()> {
|
||||
&mut sync,
|
||||
&sync_clock,
|
||||
) {
|
||||
let text = error.to_string();
|
||||
let code = if text.len() <= 96
|
||||
&& text
|
||||
.bytes()
|
||||
.all(|b| b.is_ascii_uppercase() || b.is_ascii_digit() || b == b'_')
|
||||
{
|
||||
text.as_str()
|
||||
} else {
|
||||
"HELPER_VALIDATION_OR_IO_FAILURE"
|
||||
};
|
||||
let code = safe_error_code(&error);
|
||||
eprintln!("helper request rejected: {code}");
|
||||
}
|
||||
}
|
||||
@@ -627,6 +641,41 @@ fn main() -> Result<()> {
|
||||
mod tests {
|
||||
use super::*;
|
||||
#[test]
|
||||
fn sqlite_full_is_sanitized_and_failed_transaction_preserves_history() {
|
||||
let db = rusqlite::Connection::open_in_memory().unwrap();
|
||||
rmm_privileged_helper::bounded_journal_capacity(&db).unwrap();
|
||||
db.execute_batch("CREATE TABLE witness(value BLOB);INSERT INTO witness VALUES('retained');PRAGMA max_page_count=4;").unwrap();
|
||||
db.execute_batch("BEGIN;INSERT INTO witness VALUES('must roll back');")
|
||||
.unwrap();
|
||||
let error = db
|
||||
.execute("INSERT INTO witness VALUES(zeroblob(1048576))", [])
|
||||
.unwrap_err();
|
||||
let wrapped = anyhow::Error::new(error).context("private SQL context must never leak");
|
||||
assert_eq!(
|
||||
safe_error_code(&wrapped),
|
||||
"JOURNAL_STORAGE_CAPACITY_BLOCKED"
|
||||
);
|
||||
assert_eq!(
|
||||
db.query_row("SELECT count(*) FROM witness", [], |r| r.get::<_, u32>(0))
|
||||
.unwrap(),
|
||||
1
|
||||
);
|
||||
assert_eq!(
|
||||
db.query_row("PRAGMA integrity_check", [], |r| r.get::<_, String>(0))
|
||||
.unwrap(),
|
||||
"ok"
|
||||
);
|
||||
assert_eq!(
|
||||
db.query_row("PRAGMA max_page_count", [], |r| r.get::<_, u32>(0))
|
||||
.unwrap(),
|
||||
4
|
||||
);
|
||||
assert_eq!(
|
||||
safe_error_code(&anyhow::anyhow!("private SQL text")),
|
||||
"HELPER_VALIDATION_OR_IO_FAILURE"
|
||||
);
|
||||
}
|
||||
#[test]
|
||||
fn apt_only_profile_denies_scripts_services_reboot_and_wrong_shared_state() {
|
||||
let id = uuid::Uuid::new_v4();
|
||||
let mut config:Config=serde_json::from_value(serde_json::json!({"profile":"apt_only",
|
||||
|
||||
@@ -85,6 +85,57 @@ fn directory() -> std::path::PathBuf {
|
||||
path
|
||||
}
|
||||
#[test]
|
||||
fn journal_capacity_is_finite_and_retains_original_consumption_on_reopen() {
|
||||
let dir = directory();
|
||||
let uid = unsafe { libc::geteuid() };
|
||||
let mut db = open_ledger(&dir, uid).unwrap();
|
||||
let (binding, request, _, now) = fixture();
|
||||
let (_, permit) = binding.verify(&request, now).unwrap();
|
||||
consume(&mut db, &permit).unwrap();
|
||||
save_start(&db, &permit, &request).unwrap();
|
||||
let original: Vec<u8> = db
|
||||
.query_row("SELECT start FROM execution_evidence", [], |r| r.get(0))
|
||||
.unwrap();
|
||||
db.execute_batch("PRAGMA max_page_count=16384").unwrap();
|
||||
drop(db);
|
||||
let mut db = open_ledger(&dir, uid).unwrap();
|
||||
assert_eq!(
|
||||
db.query_row("PRAGMA page_size", [], |r| r.get::<_, u32>(0))
|
||||
.unwrap(),
|
||||
4096
|
||||
);
|
||||
assert_eq!(
|
||||
db.query_row("PRAGMA max_page_count", [], |r| r.get::<_, u32>(0))
|
||||
.unwrap(),
|
||||
65536
|
||||
);
|
||||
let preserved: (String, Option<Vec<u8>>, Vec<u8>) = db.query_row("SELECT c.grant_id,c.result,e.start FROM consumed c JOIN execution_evidence e USING(grant_id)", [], |r|Ok((r.get(0)?,r.get(1)?,r.get(2)?))).unwrap();
|
||||
assert_eq!(preserved, (permit.grant_id.to_string(), None, original));
|
||||
assert!(consume(&mut db, &permit).is_err());
|
||||
drop(db);
|
||||
fs::remove_dir_all(dir).unwrap();
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn journal_capacity_rejects_unexpected_page_size_without_rewriting_history() {
|
||||
let dir = directory();
|
||||
let db = rusqlite::Connection::open(dir.join("odd.sqlite")).unwrap();
|
||||
db.execute_batch("PRAGMA page_size=8192;CREATE TABLE witness(value TEXT);INSERT INTO witness VALUES('retained');").unwrap();
|
||||
assert!(bounded_journal_capacity(&db).is_err());
|
||||
assert_eq!(
|
||||
db.query_row("PRAGMA page_size", [], |r| r.get::<_, u32>(0))
|
||||
.unwrap(),
|
||||
8192
|
||||
);
|
||||
assert_eq!(
|
||||
db.query_row("SELECT value FROM witness", [], |r| r.get::<_, String>(0))
|
||||
.unwrap(),
|
||||
"retained"
|
||||
);
|
||||
drop(db);
|
||||
fs::remove_dir_all(dir).unwrap();
|
||||
}
|
||||
#[test]
|
||||
fn exact_signature_and_every_permit_binding() {
|
||||
let (binding, request, key, now) = fixture();
|
||||
assert!(binding.verify(&request, now).is_ok());
|
||||
|
||||
Reference in New Issue
Block a user