Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 3 additions & 4 deletions .github/workflows/ext-pr.yml
Original file line number Diff line number Diff line change
Expand Up @@ -144,14 +144,13 @@ jobs:
- name: test/ext-microvm.sh
run: sudo -E env PATH="$PATH" TMPDIR="$PWD/.tmp" test/ext-microvm.sh
# The ext-* suites on the microVM target (#72, test/lib/target.sh): php-fpm in the guest,
# behind the stack's nginx. Each check that can't apply in a guest says n/a and why. Not
# here yet: ext-enforce, ext-db and ext-vault, whose audit-line checks intermittently miss
# lines the guest's php-fpm logged (#86); they run on this target by hand until it's fixed.
# behind the stack's nginx. Each check that can't apply in a guest says n/a and why. All
# eight suites: the guest's log lines reach the launcher's log, every one (#86).
- name: The ext-* suites on the microVM target (SC_TARGET=microvm)
run: |
set -u
rc=0
for s in config attribution layer3 layer1 hosts; do
for s in config attribution layer3 layer1 hosts enforce db vault; do
echo "::group::test/ext-$s.sh (microvm)"
sudo -E env PATH="$PATH" TMPDIR="$PWD/.tmp" SC_TARGET=microvm "test/ext-$s.sh" || { echo "::error::test/ext-$s.sh failed on the microVM target"; rc=1; }
echo "::endgroup::"
Expand Down
29 changes: 29 additions & 0 deletions docs/layer1-microvm.md
Original file line number Diff line number Diff line change
Expand Up @@ -822,6 +822,35 @@ site's settings (`WORDPRESS_DB_HOST`, …). Until #72 the test image baked them
the snapshot's `vm.mem` (as text or hex) and `vm.snap`, `console.log`, the launcher's log. It
also checks that a restored guest still reaches the database.

## The guest's log: what it guarantees, and against whom (#86)

php-fpm's lines (layer 2's audit lines among them) and the init's own come to the launcher on
vsock 1514, numbered and acknowledged. A line that can't be delivered is counted in the
launcher's own log ("N guest log lines lost"), never dropped in silence.

- **Only the init writes the log.** The launcher makes a fresh 32-byte log key for every boot
and every restore, and gives it to the init over the control port, which only the host can
reach. A connection to 1514 that doesn't present that key first, within a second, is refused,
and the refusals are counted in the launcher's log. A restored init's old key (the
snapshot's) is refused too. It is never let in, and is only counted apart.
- **Labels.** php-fpm's output (master and workers) is logged as `guest: …`. The init's own lines
go in a frame of their own and are logged as `guest-init: …`, which nothing php-fpm writes can
produce.
- **Reserved room for the init.** The init's lines have the last 256 KiB of the guest's 4 MiB
buffer to themselves, so php-fpm's lines can't crowd them out.
- **Escaping.** Control characters and the Unicode line separators in a guest line are escaped,
so one guest line is one launcher line.

**This holds against code that isn't root in the guest: PHP, plugins, and anything they run as
the pool user. It does not hold against root in the guest.** The lines are produced inside the
guest, by php-fpm's master and the init, and the key is in the init's memory. Code that becomes
root in the guest (a kernel exploit, say) can forge lines, drop them before they are numbered,
or send nothing at all. What the launcher still guarantees then is that such lines are marked
as the guest's (`guest:`/`guest-init:`) and never read as its own (`securecore-fpm:`), and
that a key learned in one restored guest is worthless in the next. An audit trail that must
survive a compromised guest has to be kept outside it: the host's egress, sync and database
logs are the launcher's own.

## Follow-up issues (not filed)

1. **mirab: Landlock and seccomp in the x86_64 guest kernel.** Add `SECURITY`,
Expand Down
34 changes: 30 additions & 4 deletions ext/config/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -627,10 +627,36 @@ fn root_of(path: &Path) -> PathBuf {

/// Reads, parses and validates the file at `path`.
pub fn load(path: &Path) -> Result<Loaded, LoadError> {
let err = |problem| LoadError { path: path.to_owned(), problem };
let bytes = fs::read(path).map_err(|e| err(Problem::Read(e)))?;
let config = Config::from_slice(&bytes).map_err(err)?;
Ok(Loaded { path: path.to_owned(), config, sha256: sha256_hex(&bytes), root: root_of(path) })
let bytes = fs::read(path).map_err(|e| LoadError { path: path.to_owned(), problem: Problem::Read(e) })?;
load_bytes(path, &bytes)
}

/// Parses and validates `bytes`, read from `path` by the caller (the launcher reads the file its
/// own careful way, addiscorp/securecore#90 review).
pub fn load_bytes(path: &Path, bytes: &[u8]) -> Result<Loaded, LoadError> {
let config = Config::from_slice(bytes).map_err(|problem| LoadError { path: path.to_owned(), problem })?;
Ok(Loaded { path: path.to_owned(), config, sha256: sha256_hex(bytes), root: root_of(path) })
}

impl LoadError {
/// The message without anything of the file's bytes in it: a JSON or schema error says
/// where (line and column) and what kind, never the token it met (serde's messages quote
/// it, and a file in the wrong place could hold anything). The rules the types can't
/// express ([`Problem::Invalid`]) are this crate's own words.
pub fn without_contents(&self) -> String {
match &self.problem {
Problem::Parse(e) => {
let kind = match e.classify() {
serde_json::error::Category::Syntax => "not valid JSON",
serde_json::error::Category::Eof => "the file ends too early",
serde_json::error::Category::Data => "a key or value securecore.schema.json doesn't take",
serde_json::error::Category::Io => "it could not be read",
};
format!("{}: invalid at line {} column {}: {kind}", self.path.display(), e.line(), e.column())
}
_ => self.to_string(),
}
}
}

pub fn sha256_hex(bytes: &[u8]) -> String {
Expand Down
14 changes: 11 additions & 3 deletions ext/fpm/guest/vsock-connect.c
Original file line number Diff line number Diff line change
Expand Up @@ -61,9 +61,17 @@ static void load_ports(void) {
if (!egress_host_port) negress = 0;
}

/* Read once, in php-fpm's master at load time: its workers are forks with clear_env, so the
* variable is gone by the time a worker connects. */
__attribute__((constructor)) static void init(void) { load_ports(); }
/* Read once, in php-fpm's master at load time, then taken out of the environment: the workers are
* forks of the master (the library and what it read stay mapped), and the image's pool has
* clear_env = no, so without this every program a PHP script starts would inherit the preload
* and the list of ports it forwards (#86 review). */
__attribute__((constructor)) static void init(void) {
load_ports();
unsetenv("LD_PRELOAD");
unsetenv("SC_VSOCK_PORTS");
unsetenv("SC_EGRESS_PORTS");
unsetenv("SC_EGRESS_HOST_PORT");
}

static int listed(unsigned short p) {
if (nports < 0) load_ports();
Expand Down
107 changes: 72 additions & 35 deletions ext/fpm/src/guest.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,9 @@
//! out php-fpm's connect to 127.0.0.1:<port> is a vsock connect to the host on that port
//! (libsc-vsock-connect.so, #70), for each port the host forwards (sc.ports); a unix
//! socket under /run (sc.sock) reaches the same port through a forwarder here
//! log php-fpm's stdout and stderr → vsock host:1514 (the serial console is too slow)
//! log php-fpm's stdout and stderr, and this init's own lines → vsock host:1514 (the serial
//! console is too slow), numbered and held until the host acknowledges them (#86,
//! [`crate::guestlog`])
//! ctl vsock 1515: the host's start message, sent after every cold boot and every restore:
//! the clock, then the writable disks to mount; and `sync` before it stops the VM
//! (see [`Ctl`])
Expand All @@ -21,7 +23,7 @@

use std::ffi::CString;
use std::fs;
use std::io::{self, BufRead, BufReader, Read, Write};
use std::io::{self, BufRead, BufReader, Write};
use std::net::TcpStream;
use std::os::fd::{AsRawFd, FromRawFd, OwnedFd};
use std::os::unix::net::UnixListener;
Expand Down Expand Up @@ -169,6 +171,10 @@ pub enum Ctl {
/// The handshake, alone on its connection before the start message: the host's protocol.
/// `ok` only when it is this init's own and the one it booted with (`sc.proto`).
Proto { version: u32 },
/// This VM's log key (hex, [`crate::guestlog::KEY_LEN`] bytes): the host takes the log only
/// from connections that present it (#86). Alone on its connection, at any time; only the
/// host can reach this port, and no other process in the guest learns the key.
LogKey { hex: String },
Done,
}

Expand All @@ -184,6 +190,7 @@ impl Ctl {
Ctl::Sync => "sync\n".into(),
Ctl::Reload => "reload\n".into(),
Ctl::Proto { version } => format!("proto {version}\n"),
Ctl::LogKey { hex } => format!("logkey {hex}\n"),
Ctl::Done => "done\n".into(),
}
}
Expand All @@ -206,6 +213,9 @@ impl Ctl {
["sync"] => Ok(Ctl::Sync),
["reload"] => Ok(Ctl::Reload),
["proto", v] => Ok(Ctl::Proto { version: v.parse().map_err(|_| format!("proto: {v}"))? }),
["logkey", hex] if hex.len() == 2 * crate::guestlog::KEY_LEN && hex.bytes().all(|b| b.is_ascii_hexdigit()) => Ok(Ctl::LogKey { hex: (*hex).into() }),
// Never echo it: it is a secret.
["logkey", ..] => Err("a malformed logkey line".into()),
["done"] => Ok(Ctl::Done),
// Never echo an env line: it carries a secret.
["env", ..] => Err("a malformed env line".into()),
Expand All @@ -229,7 +239,7 @@ pub fn env_name_ok(name: &str) -> bool {
/// Exactly one start message per boot (a snapshot is taken before it, so each restore takes its
/// own); after it only `sync` and `reload`, and nothing at all before it but a start.
pub fn allowed(lines: &[Ctl], started: bool) -> Result<(), String> {
if lines.iter().any(|c| matches!(c, Ctl::Proto { .. })) && lines.len() > 1 {
if lines.iter().any(|c| matches!(c, Ctl::Proto { .. } | Ctl::LogKey { .. })) && lines.len() > 1 {
return Err("the handshake comes alone".into());
}
let starts = lines.iter().any(Ctl::starts);
Expand Down Expand Up @@ -476,7 +486,8 @@ fn apply(ctl: &Ctl) -> io::Result<()> {
return Err(io::Error::new(e.kind(), format!("{device}: BLKFLSBUF: {e}")));
}
drop(dev);
mount(device, path, "ext4", libc::MS_NOSUID | libc::MS_NODEV, WRITABLE_MOUNT_DATA)?;
// noexec (#86 verification review): nothing written to a writable folder runs.
mount(device, path, "ext4", libc::MS_NOSUID | libc::MS_NODEV | libc::MS_NOEXEC, WRITABLE_MOUNT_DATA)?;
WRITABLE.lock().unwrap_or_else(|p| p.into_inner()).push(path.clone());
// Every file closed after writing is made durable at once, whatever the host's sync
// is doing (it may be stuck behind a host that doesn't answer).
Expand All @@ -488,18 +499,25 @@ fn apply(ctl: &Ctl) -> io::Result<()> {
});
Ok(())
}
Ctl::Tmpfs { path } => mount("tmpfs", path, "tmpfs", libc::MS_NOSUID | libc::MS_NODEV, "mode=1777"),
Ctl::Tmpfs { path } => mount("tmpfs", path, "tmpfs", libc::MS_NOSUID | libc::MS_NODEV | libc::MS_NOEXEC, "mode=1777"),
Ctl::Sync => {
// SAFETY: sync(2) takes nothing and can't fail.
unsafe { libc::sync() };
// And every change to a writable folder is on the host's copy of it (#72).
if WRITABLE.lock().unwrap_or_else(|p| p.into_inner()).is_empty() {
return Ok(());
if !WRITABLE.lock().unwrap_or_else(|p| p.into_inner()).is_empty() {
let drain = DRAIN.get().ok_or_else(|| io::Error::other("the folder sync never started"))?;
drain.wait(SYNC_DRAIN).map_err(|e| io::Error::other(format!("the writable folders did not sync to the host: {e}")))?;
}
let drain = DRAIN.get().ok_or_else(|| io::Error::other("the folder sync never started"))?;
drain.wait(SYNC_DRAIN).map_err(|e| io::Error::other(format!("the writable folders did not sync to the host: {e}")))
// The host stops the VM after a sync: every log line must be in its log by then, or
// counted there as lost (the reply is the launcher's to log).
LOG.drain(LOG_DRAIN).map_err(|n| io::Error::other(crate::guestlog::lost(n as u64, crate::guestlog::UNACKED_AT_STOP)))
}
// Checked before anything is applied ([`protocol_ok`]).
Ctl::LogKey { hex } => {
let key: crate::guestlog::Key = unhex(hex).try_into().map_err(|_| io::Error::other("logkey: not a key"))?;
LOG.set_key(key);
Ok(())
}
Ctl::Proto { .. } | Ctl::Done => Ok(()),
}
}
Expand Down Expand Up @@ -561,6 +579,11 @@ fn serve_ctl(conn: Vsock, peer_cid: u32) {
} else if !fpm_listening(Duration::from_secs(10)) {
failed = Some(format!("php-fpm with the delivered environment isn't listening on 127.0.0.1:{FPM_PORT}"));
}
} else if failed.is_none() && lines.contains(&Ctl::Reload) && !fpm_listening(Duration::from_secs(10)) {
// A reload is done once the fresh php-fpm takes requests, not once it is started: the
// host says "reloaded" on this answer, and sends requests on (found by #86's stress on
// GitHub's runners, where requests right after a reload met no listener).
failed = Some(format!("the reloaded php-fpm isn't listening on 127.0.0.1:{FPM_PORT}"));
}
// The host never shows a reply to a message that carried secrets, so the reason is logged
// here too (no message here ever holds a value: refused env lines are never echoed).
Expand All @@ -569,15 +592,30 @@ fn serve_ctl(conn: Vsock, peer_cid: u32) {
{
report(&format!("ctl: the start message was refused: {e}"));
}
// A refusal may end the launcher: the lines that explain it reach its log first.
if failed.is_some() {
let _ = LOG.drain(LOG_DRAIN);
}
let reply = failed.map_or_else(|| "ok\n".to_owned(), |e| format!("error {e}\n"));
let _ = out.write_all(reply.as_bytes());
}

/// Every line for the host's log (php-fpm's output and this init's own), held until the host
/// acknowledges it (#86): one numbered stream on one connection ([`crate::guestlog`]).
static LOG: crate::guestlog::Outbox = crate::guestlog::Outbox::new(crate::guestlog::OUTBOX_BYTES);
/// How long a `sync` waits for the host to have every log line (inside [`SYNC_TIMEOUT`]).
const LOG_DRAIN: Duration = Duration::from_secs(1);

/// The log's sender, once per boot (a restore resumes it with the snapshot).
fn start_log() {
thread::spawn(|| crate::guestlog::send(&LOG, || Vsock::connect(HOST_CID, LOG_PORT)));
}

/// One line to the host's log, from this init (the host marks every guest line as the guest's).
fn report(message: &str) {
if let Ok(mut c) = Vsock::connect(HOST_CID, LOG_PORT) {
let _ = c.write_all(format!("securecore-init: {message}\n").as_bytes());
}
// Its own kind of frame: the launcher logs it as `guest-init: `, which php-fpm's lines can't
// be (#86 verification review).
LOG.init_line(message.as_bytes());
}

/// Warns once a writable disk passes 90 % (and again past 98 %): beyond it, php-fpm's writes
Expand Down Expand Up @@ -608,28 +646,16 @@ fn watch_space() {
}
}

/// php-fpm's output to the host, reconnecting when the connection breaks (every restore
/// resets the guest's vsock connections).
fn pump_log(mut from: fs::File) {
let mut conn: Option<Vsock> = None;
let mut buf = vec![0u8; 64 * 1024];
loop {
let n = match from.read(&mut buf) {
Ok(0) | Err(_) => return,
Ok(n) => n,
};
for _ in 0..50 {
if conn.is_none() {
conn = Vsock::connect(HOST_CID, LOG_PORT).ok();
}
match conn.as_mut().map(|c| c.write_all(&buf[..n])) {
Some(Ok(())) => break,
_ => {
conn = None;
thread::sleep(Duration::from_millis(20));
}
}
/// php-fpm's output to the host's log, a line at a time ([`LOG`] delivers it). Never blocks on
/// the host: php-fpm's master writes every worker's lines to this pipe.
fn pump_log(from: fs::File) {
let mut r = BufReader::with_capacity(64 << 10, from);
let mut line = Vec::with_capacity(256);
while let Some(cut) = crate::guestlog::read_capped(&mut r, &mut line, crate::guestlog::MAX_LINE) {
if cut {
line.extend_from_slice(" …[cut]".as_bytes());
}
LOG.line(&line);
}
}

Expand Down Expand Up @@ -802,13 +828,15 @@ fn boot_guest() -> io::Result<u32> {
mount("proc", "/proc", "proc", flags | libc::MS_NOEXEC, "")?;
mount("sysfs", "/sys", "sysfs", flags | libc::MS_NOEXEC, "")?;
mount("devtmpfs", "/dev", "devtmpfs", libc::MS_NOSUID, "")?;
mount("tmpfs", "/tmp", "tmpfs", flags, "mode=1777")?;
// noexec: nothing a site writes there runs (#86 review).
mount("tmpfs", "/tmp", "tmpfs", flags | libc::MS_NOEXEC, "mode=1777")?;
mount("tmpfs", "/run", "tmpfs", flags, "mode=755")?;
let boot = parse_cmdline(&fs::read_to_string("/proc/cmdline")?).map_err(io::Error::other)?;
if let Some(t) = boot.time {
set_clock(t, 0)?;
}
mount("/dev/vdb", &boot.root, "ext4", libc::MS_RDONLY | flags, "noload")?;
// noexec too: php-fpm reads the site's PHP, nothing on the site disk ever runs as a program.
mount("/dev/vdb", &boot.root, "ext4", libc::MS_RDONLY | flags | libc::MS_NOEXEC, "noload")?;
loopback_up()?;
// The folder sync watches every folder of the writable disks (8192 by default here).
let _ = fs::write("/proc/sys/fs/inotify/max_user_watches", "65536");
Expand All @@ -818,6 +846,7 @@ fn boot_guest() -> io::Result<u32> {
// (`commit=1` on the mount). The defaults are 30 s and 5 s.
fs::write("/proc/sys/vm/dirty_expire_centisecs", "100")?;
fs::write("/proc/sys/vm/dirty_writeback_centisecs", "50")?;
start_log();
listen_forwards(&boot)?;
let pid = start_php_fpm(&boot)?;
FPM_MASTER.store(pid as i32, Ordering::SeqCst);
Expand Down Expand Up @@ -957,6 +986,14 @@ mod tests {
assert_eq!(allowed(&[Ctl::Sync], true), Ok(()));
assert_eq!(allowed(&[Ctl::Reload], true), Ok(()));
assert!(allowed(&[Ctl::Sync], false).is_err(), "nothing before the start");
// The log key (#86 review): at any time, alone, and never echoed when malformed.
let key = Ctl::LogKey { hex: "ab".repeat(crate::guestlog::KEY_LEN) };
assert_eq!(Ctl::parse(key.line().trim_end()), Ok(key.clone()));
assert_eq!(allowed(std::slice::from_ref(&key), false), Ok(()));
assert_eq!(allowed(std::slice::from_ref(&key), true), Ok(()));
assert!(allowed(&[key, Ctl::Sync], true).is_err(), "alone on its connection");
let e = Ctl::parse(&format!("logkey {}", "5ec7".repeat(8))).expect_err("short");
assert!(!e.contains("5ec7"), "{e}");
assert!(allowed(&[Ctl::Time { secs: 1, nanos: 0 }, Ctl::Reload], false).is_err());
}
}
Loading
Loading