diff --git a/beyond-pg-init/src/env_relay.rs b/beyond-pg-init/src/env_relay.rs index 637ed9a..7b41d59 100644 --- a/beyond-pg-init/src/env_relay.rs +++ b/beyond-pg-init/src/env_relay.rs @@ -1,10 +1,13 @@ -//! The database's password comes from the sealed env relay, never from MMDS. +//! The database's password comes from the sealed env relay, never from MMDS, and it arrives only as a SCRAM-SHA-256 verifier. //! //! The password is generated by the node's postgres driver (the platform-generated secrets of the Beyond repo), sealed to the tenant's node key -//! and stored by the API as a sealed variable: nothing central ever holds it. PID 1 asks for it over the substrate vsock it already owns, the way -//! every guest asks its driver for its env, and writes it into the metadata file the supervisor reads, before the supervisor starts. The -//! supervisor's `post_start` then runs `ALTER ROLE postgres PASSWORD` on EVERY start, so a data directory that carries another instance's role -//! hash (a fork, a restore) takes this instance's value. +//! and stored by the API as a sealed variable: nothing central ever holds it. The driver derives a verifier from it on the node (RFC 5802/7677, +//! `SCRAM-SHA-256$4096:$:`, a fresh salt each delivery) and hands the guest only that: this VM can check a login and +//! cannot present the password. PID 1 asks for it over the substrate vsock it already owns, the way every guest asks its driver for its env, and +//! writes it into the metadata file the supervisor reads, before the supervisor starts. initdb takes the verifier as the superuser's password +//! as it is, and the supervisor's `post_start` runs `ALTER ROLE postgres PASSWORD ''` on EVERY start (PostgreSQL stores a verifier +//! as given), so a data directory that carries another instance's role hash (a fork, a restore) takes this instance's value. A value that is +//! not a verifier (a driver from before this) is never kept, written or used: the ask repeats and, past [`WAIT`], init exits. //! //! Wire (`rustlib/wire` `env.rs` and `rustlib/vsock-protocol` in the Beyond repo, mirrored here so this repo builds standalone and pinned by the //! tests below): the ask is an `AppMessage` (0x20) whose bytes are `[0xE1]`; the answer is an `AppMessage` whose bytes are `[0xE2][msgpack @@ -31,6 +34,8 @@ pub const ASK_EVERY: Duration = Duration::from_secs(1); pub const WAIT: Duration = Duration::from_secs(300); /// The one variable the supervisor needs from the relay. pub const PASSWORD_KEY: &str = "POSTGRES_PASSWORD"; +/// What a SCRAM-SHA-256 verifier starts with (PostgreSQL's `rolpassword` form). +pub const VERIFIER_PREFIX: &str = "SCRAM-SHA-256$"; #[derive(Serialize, Deserialize)] struct AppMessage { @@ -48,7 +53,7 @@ struct EnvPayload { /// What the wait ended with. #[derive(Debug, PartialEq, Eq)] pub enum Outcome { - /// The relay answered with a password. + /// The relay answered with the password's verifier. Password(String), /// There is no substrate (no AF_VSOCK): the metadata file is used as it is. NoSubstrate, @@ -95,8 +100,8 @@ pub fn ask_frame() -> Vec { frame } -/// An inbound `AppMessage` payload (the msgpack after the type byte). If it is an env answer holding the password, keep it and wake the -/// waiter. Anything else (another primitive message, a malformed or empty answer) is ignored: the ask repeats. +/// An inbound `AppMessage` payload (the msgpack after the type byte). If it is an env answer holding the password's verifier, keep it and wake +/// the waiter; a password that is not a verifier is dropped here, so no plaintext is ever held. Anything else (another primitive message, a malformed or empty answer) is ignored: the ask repeats. pub fn offer(app_payload: &[u8]) { let Ok(msg) = rmp_serde::from_slice::(app_payload) else { return; @@ -110,7 +115,7 @@ pub fn offer(app_payload: &[u8]) { let Ok(env) = rmp_serde::from_slice::(rest) else { return; }; - if let Some(pw) = env.vars.get(PASSWORD_KEY).filter(|p| !p.is_empty()) { + if let Some(pw) = env.vars.get(PASSWORD_KEY).filter(|p| p.starts_with(VERIFIER_PREFIX)) { state().password = Some(pw.clone()); CV.notify_all(); } @@ -204,15 +209,17 @@ mod tests { .unwrap(); offer(&foreign); foreign.clear(); + // A plaintext password (a driver from before verifiers) is never kept: the wait times out and init exits. + offer(&answer(&[(PASSWORD_KEY, "s3cret")])); + assert!(needs_password()); assert_eq!(wait(Duration::from_millis(20)), Outcome::TimedOut); - let t = std::thread::spawn(|| { + let verifier = format!("{VERIFIER_PREFIX}4096:c2FsdA==$a:b"); + let v2 = verifier.clone(); + let t = std::thread::spawn(move || { std::thread::sleep(Duration::from_millis(30)); - offer(&answer(&[(PASSWORD_KEY, "s3cret")])); + offer(&answer(&[(PASSWORD_KEY, &v2)])); }); - assert_eq!( - wait(Duration::from_secs(5)), - Outcome::Password("s3cret".into()) - ); + assert_eq!(wait(Duration::from_secs(5)), Outcome::Password(verifier)); t.join().unwrap(); assert!(!needs_password()); *state() = State::default(); diff --git a/tests/supervisor.rs b/tests/supervisor.rs index 2c2e3f8..489ddee 100644 --- a/tests/supervisor.rs +++ b/tests/supervisor.rs @@ -669,6 +669,64 @@ fn supervisor_lifecycle_primary() { ); } +/// The VM is handed only a SCRAM-SHA-256 verifier of the superuser's password (the Beyond node derives it; see `env_relay`). initdb takes it +/// as given and `ALTER ROLE postgres PASSWORD` on every start sets it again; a client that knows the ORIGINAL password logs in over SCRAM, a wrong +/// one does not, and the container holds no copy of the original in its metadata or environment. +#[test] +#[ignore = "requires Docker + musl target + beyond-pg-test:latest image"] +fn supervisor_with_only_a_verifier_accepts_the_original_password_over_scram() { + // PostgreSQL 17's own `rolpassword` for the password below (docker postgres:17, `SET password_encryption='scram-sha-256'`). + const PASSWORD: &str = "correct horse battery"; + const VERIFIER: &str = "SCRAM-SHA-256$4096:cbHdwWtOk0dwNZAaQVTR9g==$NYFMnqzwzMV7CTFaCfBP5J3y41xmTmDJPDLE96pN3/o=:P8VydXaJRrINE2cDxQRh2CZobwRK3EbticDXin423Nc="; + let _guard = DOCKER.lock().unwrap_or_else(|e| e.into_inner()); + let harness = SupervisorHarness::new().expect("Docker, the musl target and beyond-pg-test:latest are required"); + let container = harness.start(&[("POSTGRES_PASSWORD", VERIFIER)]); + if !container.wait_postgres_ready(Duration::from_secs(90)) { + eprintln!("logs:\n{}", container.logs()); + panic!("Postgres never became ready"); + } + assert!(container.wait_post_start_done(Duration::from_secs(30)), "post-start did not complete\n{}", container.logs()); + + let login = |password: &str| { + container.exec(&[ + "env", + &format!("PGPASSWORD={password}"), + "psql", + "-h", + "127.0.0.1", + "-p", + "5433", + "-U", + "postgres", + "-d", + "postgres", + "-tAc", + "SELECT 'scram-login-ok'", + ]) + }; + let ok = login(PASSWORD); + assert!( + ok.status.success() && String::from_utf8_lossy(&ok.stdout).trim() == "scram-login-ok", + "the original password did not log in: {}\n{}", + String::from_utf8_lossy(&ok.stderr), + container.logs() + ); + assert!(!login("not the password").status.success(), "a wrong password logged in"); + + // The server stores exactly the verifier it was handed, and the original is nowhere in the container's metadata or environment. + let stored = container.exec(&[ + "gosu", "postgres", "psql", "-h", "/var/run/postgresql", "-p", "5433", "-tAc", + "SELECT rolpassword FROM pg_authid WHERE rolname='postgres'", + ]); + assert_eq!(String::from_utf8_lossy(&stored.stdout).trim(), VERIFIER); + let leak = container.exec(&[ + "sh", "-c", + &format!("grep -rqF '{PASSWORD}' /run/mmds /proc/1/environ /proc/self/environ 2>/dev/null; echo $?"), + ]); + assert_eq!(String::from_utf8_lossy(&leak.stdout).trim(), "1", "the original password is in the container's metadata or environment"); + let _ = container.stop(Duration::from_secs(30)); +} + /// Warm-start: a busy box that cold-restarts must come back at its pre-restart /// pooler size, not a single worker — the post-restart reconnect storm is peak /// handshake churn, the worst moment to be under-provisioned. children.json is