feat: add approval bound full maintenance script console and executors
Rust development slice / linux (push) Failing after 6s
Rust development slice / linux (push) Failing after 6s
This commit is contained in:
@@ -0,0 +1,91 @@
|
||||
//! Root observes its own *running* executable. An agent-readable configured
|
||||
//! pathname or a build claim is never substituted for this descriptor.
|
||||
use anyhow::{Result, ensure};
|
||||
use openssl::sha::Sha256;
|
||||
use std::{
|
||||
fs::File,
|
||||
io::{Read, Seek, SeekFrom},
|
||||
os::unix::fs::MetadataExt,
|
||||
};
|
||||
|
||||
pub struct RunningImage {
|
||||
file: File,
|
||||
expected: String,
|
||||
}
|
||||
impl RunningImage {
|
||||
pub fn current(expected: &str) -> Result<Self> {
|
||||
let mut value = Self {
|
||||
file: File::open("/proc/self/exe")?,
|
||||
expected: expected.into(),
|
||||
};
|
||||
value.verify()?;
|
||||
Ok(value)
|
||||
}
|
||||
pub fn verify(&mut self) -> Result<String> {
|
||||
let before = self.file.metadata()?;
|
||||
ensure!(
|
||||
before.is_file()
|
||||
&& before.uid() == 0
|
||||
&& before.mode() & 0o022 == 0
|
||||
&& before.len() > 0
|
||||
&& before.len() <= 128 * 1024 * 1024,
|
||||
"RUNNING_HELPER_IMAGE_METADATA"
|
||||
);
|
||||
let identity = |m: &std::fs::Metadata| {
|
||||
(
|
||||
m.dev(),
|
||||
m.ino(),
|
||||
m.len(),
|
||||
m.uid(),
|
||||
m.mode(),
|
||||
m.mtime(),
|
||||
m.mtime_nsec(),
|
||||
m.ctime(),
|
||||
m.ctime_nsec(),
|
||||
)
|
||||
};
|
||||
self.file.seek(SeekFrom::Start(0))?;
|
||||
let mut hash = Sha256::new();
|
||||
let mut buffer = [0u8; 65536];
|
||||
let mut length = 0u64;
|
||||
loop {
|
||||
let n = self.file.read(&mut buffer)?;
|
||||
if n == 0 {
|
||||
break;
|
||||
}
|
||||
length += n as u64;
|
||||
ensure!(
|
||||
length <= before.len(),
|
||||
"RUNNING_HELPER_IMAGE_LENGTH_CHANGED"
|
||||
);
|
||||
hash.update(&buffer[..n]);
|
||||
}
|
||||
let actual = hex::encode(hash.finish());
|
||||
ensure!(
|
||||
length == before.len()
|
||||
&& identity(&before) == identity(&self.file.metadata()?)
|
||||
&& actual == self.expected,
|
||||
"RUNNING_HELPER_IMAGE_PIN_MISMATCH"
|
||||
);
|
||||
Ok(actual)
|
||||
}
|
||||
}
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use super::*;
|
||||
#[test]
|
||||
fn actual_running_image_is_independent_of_a_claimed_path_or_digest() {
|
||||
let metadata = std::fs::metadata("/proc/self/exe").unwrap();
|
||||
let bytes = std::fs::read("/proc/self/exe").unwrap();
|
||||
let actual = hex::encode(openssl::sha::sha256(&bytes));
|
||||
if metadata.uid() == 0 && metadata.mode() & 0o022 == 0 {
|
||||
let mut image = RunningImage::current(&actual).unwrap();
|
||||
assert_eq!(image.verify().unwrap(), actual);
|
||||
} else {
|
||||
// An untrusted-owned/writable executable must fail even if its
|
||||
// content hash is correct. This is a tested denial, never a skip.
|
||||
assert!(RunningImage::current(&actual).is_err());
|
||||
}
|
||||
assert!(RunningImage::current(&"00".repeat(32)).is_err());
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user