From 3aa4b880d9e02a87e912d7eab7616e7a276729cd Mon Sep 17 00:00:00 2001 From: Sami Fouad Date: Wed, 11 Nov 2026 15:12:10 -0600 Subject: [PATCH 1/8] Layer 2: check query text that isn't UTF-8 instead of skipping it The database handler read the SQL argument as UTF-8 and ran the query unchecked when it wasn't, so one invalid byte anywhere in a query (a comment is enough) skipped layer 2 for every route. Read it with the invalid bytes replaced: such a byte is never ASCII, so the statement's structure reads as the server reads it. --- ext/php/src/db.rs | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) diff --git a/ext/php/src/db.rs b/ext/php/src/db.rs index 56874ec..5a94277 100644 --- a/ext/php/src/db.rs +++ b/ext/php/src/db.rs @@ -212,7 +212,12 @@ unsafe fn check(ex: *mut zend_execute_data, hook: &Hook) -> Then { if !l2.enabled || l2.app.is_none() || l2.mode == Mode::Off { return Then::Run; } - let Some(text) = (unsafe { vault::arg(ex, hook.sql_arg) }).and_then(Zval::str).map(str::to_owned) else { + // Read as UTF-8 with any invalid byte replaced: a byte that is not UTF-8 is never ASCII, so + // the statement's structure (keywords, quotes, comments) reads as the server reads it. Taking + // only valid UTF-8 skipped the check for any query with one such byte in it, a comment + // included (#94). + let Some(text) = (unsafe { vault::arg(ex, hook.sql_arg) }).and_then(Zval::zend_str).map(|s| String::from_utf8_lossy(s.as_bytes()).into_owned()) + else { // No SQL argument (mysqli_stmt without a query, a non-string): nothing to decide. return Then::Run; }; From f355a4a7b10d4d626f3f40f328787c7e06369e00 Mon Sep 17 00:00:00 2001 From: Sami Fouad Date: Wed, 11 Nov 2026 15:43:00 -0600 Subject: [PATCH 2/8] Layer 3: the database gateway for isolated plugins (#94) An isolated plugin's $wpdb in its child is a gateway to the php-fpm worker: each query crosses the request socket as a Q frame while the worker waits for the plugin's load or a call, and the stand-in runs it on the worker's real connection with $wpdb->query(). While it does, the stand-in's frames are the plugin, so layer 2's own caller rule charges the query to it exactly as unisolated: same check, same verdict, same audit line. The child holds no connection and no credential; the slug comes from the channel, never from the child. The worker admits each query first: dbQueries per request, size, UTF-8, and no change to the statement time cap. dbResultBytes caps an answer; the call's remaining timeoutMs caps each statement on the server (max_statement_time / max_execution_time). A transaction left open by a child that dies is rolled back. Tests: ext-layer3 gets a database probe (dbDelta table, every $wpdb method, own options, layer 2's refusals with the unisolated lines, the caps, the child's memory and environment scanned for the DB password) and ext-microvm runs it in the guest. --- ext/README.md | 82 ++++- ext/config/src/lib.rs | 33 +- ext/config/src/tests.rs | 13 + ext/db/src/tests.rs | 11 + ext/layer3/src/gateway.rs | 185 ++++++++++++ ext/layer3/src/lib.rs | 2 + ext/layer3/src/wire.rs | 48 ++- ext/php/src/attribution.rs | 4 +- ext/php/src/layer3.rs | 336 ++++++++++++++++++--- ext/php/src/layer3/helper.php | 107 +++++-- ext/php/src/layer3/stub.php | 88 +++++- ext/securecore.schema.json | 17 ++ test/ext-layer3.sh | 197 +++++++++++- test/ext-microvm.sh | 29 +- test/ext/layer3-memscan.php | 55 ++++ test/ext/layer3-plugin/sc-l3-neighbour.php | 9 + test/ext/layer3-plugin/sc-l3db.php | 190 ++++++++++++ 17 files changed, 1313 insertions(+), 93 deletions(-) create mode 100644 ext/layer3/src/gateway.rs create mode 100644 test/ext/layer3-memscan.php create mode 100644 test/ext/layer3-plugin/sc-l3db.php diff --git a/ext/README.md b/ext/README.md index b8099cb..07e9b15 100644 --- a/ext/README.md +++ b/ext/README.md @@ -390,7 +390,9 @@ by `mysqli_query`. `select_db` and `change_user` are refused for a component (`U re-login), except `select_db` of the site's own `DB_NAME` (exactly): `wpdb::db_connect()` runs it on every reconnect, so refusing it killed a `$wpdb->check_connection()` a plugin's call triggered; `set_charset` to an unsafe charset is refused for everyone. Every connection is checked, -not just `$wpdb`'s. +not just `$wpdb`'s. The text is read as UTF-8 with any invalid byte replaced (such a byte is +never ASCII, so the statement's structure reads as the server reads it); until #94 a query with +one such byte anywhere, a comment included, was not checked at all. ### What a refusal does @@ -1051,6 +1053,61 @@ extension. The wiring is [`php/src/layer3.rs`](php/src/layer3.rs); the parts tha caller rule) return `plugin:` for everything in the child, whatever the stack shows, so layer 2's checks there all charge the isolated plugin. +### The database (#94) + +The child has no database: no connection, no credential (no host, user, password or database +name; its environment is empty and the helper never reads the vault). Its `$wpdb` is a gateway to +the php-fpm worker, which runs each query on its own connection, under layer 2: + +1. **In the child,** `$wpdb->query()` runs the plugin's own `query` filters, then sends the SQL + on the request socket ([`gateway.rs`](layer3/src/gateway.rs): a `Q\n` frame) in place + of the answer the worker is waiting for (to the plugin's load or to a call). Everything else + is wpdb's own code on top of `query()`: `get_results`, `get_row`, `get_var`, `get_col`, + `insert`, `update`, `delete`, `replace`, `prepare` (escaped exactly as `mysqli_real_escape_string` + escapes on a utf8mb4 connection, so the worker receives the text the plugin would send + unisolated), `dbDelta`, `get_charset_collate()`. The answer sets `last_result`, `num_rows`, + `rows_affected`, `insert_id`, `last_error` and the column names (`get_col_info('name')`). The + prefix and table names are wpdb's (`set_prefix`); the charset, collation, `db_version()` and + `db_server_info()` come from the worker's `$wpdb` with the hello. +2. **In the worker,** the extension reads the frame and decides first what only layer 3 decides + (`gateway::admit`): the request's query cap, the text's size (16 MiB) and UTF-8, and no change to + the statement time cap. Then the stand-in takes the text (`securecore_layer3_db_begin`) and + runs it with `$wpdb->query()` on the real connection. While it does, the stand-in's own frames + (compiled under the name `securecore-layer3:`, which only the extension uses) are + `plugin:`, so **layer 2's own caller rule** (`attribution::db_caller`, then `db::check`) + charges the query to the plugin exactly as it charges that plugin's `$wpdb->query()` in the + worker: the same function, the same verdict, the same line + (`layer2 enforce plugin: mysqli_query core-table select wp_users`). The slug is the one + the worker opened the channel for; nothing the child sends names a plugin. Other plugins' + `query` filters run as they would, and SQL they run is charged to them. +3. The worker answers with what its `$wpdb` saw (PHP-serialized) and goes back to waiting. + +A refusal is the native failure: `false`, with layer 2's error in `last_error`. What the child's +core code does on the plugin's behalf (`get_option()` of an option missing from the snapshot, +`update_option()`, transients) is charged to the plugin, never to core: the child can't prove +which of its frames are core's. So a non-autoloaded core option needs `options:read` from an +isolated plugin, where the same `get_option()` in the worker is core's. + +| cap | where | what happens | +|---|---|---| +| `limits.dbQueries` (default 1000) | the worker, per request and plugin (its load and its calls) | each query past it fails (`last_error`: `over the dbQueries cap`); one line per request | +| `limits.dbResultBytes` (default 4 MiB, at most 16 MiB) | the worker, per answer | a larger answer fails instead (the query ran); a line | +| `limits.timeoutMs` | the worker, per call, queries included | the call's remaining time caps each statement on the server (`SET SESSION max_statement_time` on MariaDB, `max_execution_time` on MySQL, SELECTs only there), set once per call that queries and restored to the session's own value after; past the deadline the child is killed as for any call. A plugin's query naming either variable is refused | + +**Transactions** are the plugin's statements on the worker's connection, as unisolated. If the +child dies (timeout, memory, a crash) after a `START TRANSACTION`/`BEGIN`/`SET autocommit = 0` +it didn't end, the worker rolls back (with a line), so core's later writes in the request don't +join it. + +**Cost.** One query is one round trip on the request socket plus PHP serialize/unserialize of the +answer, on top of the query itself. `test/ext-layer3.sh` times a one-row `get_row(prepare(…))` 50 +times in the plugin, median of 7 pages: on a 4-thread Linux box shared with other builds, ~317 µs +isolated against ~155 µs unisolated in a quiet run, so **about +160 µs per query** (the isolated +figure held at 316 µs in a second run; the unisolated one swung to 551 µs with other builds +running, so treat the delta as rough until `bench/` measures it). A call that queries adds the two +`SET SESSION` statements and, once per request, the read of the session's value. Not yet tuned +(the column names are fetched for every SELECT; rows cross PHP-serialized). + ### Limits | | where | what happens | @@ -1067,9 +1124,12 @@ renders empty, an action does nothing. The first part covers what it proves end to end (`test/ext-layer3.sh`): a plugin that adds content through filters, actions and shortcodes, on the front end and the dashboard (Hello Dolly). Not yet: -- **The database.** An isolated plugin's queries are refused (`layer3 plugin: database - refused …`, once per query text). It sees core's options from the snapshot; its own options read - as unset. Next: the #42 gateway, the child's queries run by the worker under E4's policy. +- **The database's edges** ([above](#the-database-94)). Column metadata other than names + (`get_col_info('type')`, …) does not cross. Escaping assumes backslash escapes on an + ASCII-compatible connection (layer 2 refuses the charsets and `sql_mode`s where that differs, + but not a server whose global `sql_mode` has `NO_BACKSLASH_ESCAPES`). A plugin's own mysqli or + PDO connection still fails in the child (no network). An isolated plugin's `query` filter does + not see its own queries in the worker (a re-entered call passes through, with a line). - **Request state.** The child does not see the request: no current user, main query, `$post` or `$_GET`/`$_POST`/cookies. Next: replay them, as the spike's `setup_request_state` did. - **Objects across the socket.** Arguments and return values cross PHP-serialized with objects @@ -1190,7 +1250,19 @@ WordPress boot in a new zygote, on the request that brings it. `timeoutMs` is killed (logged) and too much memory ends the child (logged), while each page still renders. With layer 2 off the kernel alone still stops the write and the reads. The revert proof: with layer 3 off the probe runs in the worker, every probe case goes through, the loop holds the - page and the memory probe kills it. + page and the memory probe kills it. The database gateway (#94), with a database probe + (`test/ext/layer3-plugin/sc-l3db.php`, isolated): it makes its own table with `dbDelta` and uses + it through every `$wpdb` method (escaping, `insert_id`, `rows_affected`, a rolled-back + transaction, column names, a failing query's `last_error`), keeps a counter in its own option + that persists across requests and lands in the site's database, and reads the site's and core's + options, at load and in a call; layer 2 refuses a core table without a grant, a refused + statement and another plugin's table, and its lines are exactly those the same plugin gets + unisolated; the gateway refuses SQL that isn't UTF-8 and a change to the time cap; the caps trip + (dbQueries once-logged, dbResultBytes, a 5 s statement cut at timeoutMs and gone from the + server); the child's environment is empty and a dump of its memory holds no DB password and no + salt. Revert proofs: with layer 2 off the refused queries run through the gateway; unisolated, + nothing caps the queries and the statement holds the page for 5 s; in the worker the same memory + dump does hold the password. It also times a one-row query isolated and unisolated. - `DOCKER_CONTEXT=colima bench/layer3.sh` (after ext-config.sh): time to first byte of a post through the probe's filter and shortcode, isolated vs in the worker, alternating, 7 rounds. - `DOCKER_CONTEXT=colima research/db/compat.sh` (after ext-config.sh): Classic Editor, Yoast and diff --git a/ext/config/src/lib.rs b/ext/config/src/lib.rs index 7b28020..38937b6 100644 --- a/ext/config/src/lib.rs +++ b/ext/config/src/lib.rs @@ -446,14 +446,31 @@ pub struct Limits { /// Memory limit for one isolated child. #[serde(default = "default_memory_mb")] pub memory_mb: NonZeroU32, + /// Database queries one isolated component may send through the gateway in one request + /// (its load and every call together). Past it each query fails, with a line. + #[serde(default = "default_db_queries")] + pub db_queries: NonZeroU32, + /// The largest answer to one gateway query that crosses back to the child (its rows, + /// serialized), in bytes; at most 16777216 (16 MiB). A larger one fails, with a line. + #[serde(default = "default_db_result_bytes")] + #[schemars(range(min = 1, max = 16_777_216))] + pub db_result_bytes: NonZeroU32, } impl Default for Limits { fn default() -> Self { - Limits { timeout_ms: default_timeout_ms(), memory_mb: default_memory_mb() } + Limits { + timeout_ms: default_timeout_ms(), + memory_mb: default_memory_mb(), + db_queries: default_db_queries(), + db_result_bytes: default_db_result_bytes(), + } } } +/// The ceiling on `layer3.limits.dbResultBytes`: half a wire frame, so an answer always fits. +pub const MAX_DB_RESULT_BYTES: u32 = 16 << 20; + impl Layer2 { /// The vault in force (E3): `vault`, when layer 2 is enabled. The vault isn't a policy, so /// `mode` does not gate it; the real values are read once at startup regardless. @@ -470,6 +487,14 @@ fn default_memory_mb() -> NonZeroU32 { NonZeroU32::new(256).unwrap_or(NonZeroU32::MIN) } +fn default_db_queries() -> NonZeroU32 { + NonZeroU32::new(1000).unwrap_or(NonZeroU32::MIN) +} + +fn default_db_result_bytes() -> NonZeroU32 { + NonZeroU32::new(4 << 20).unwrap_or(NonZeroU32::MIN) +} + impl Config { /// Parses and validates a file's bytes. pub fn from_slice(bytes: &[u8]) -> Result { @@ -495,6 +520,12 @@ impl Config { .into(), )); } + if self.layer3.limits.db_result_bytes.get() > MAX_DB_RESULT_BYTES { + return Err(Problem::Invalid(format!( + "layer3.limits.dbResultBytes is {}: at most {MAX_DB_RESULT_BYTES}", + self.layer3.limits.db_result_bytes + ))); + } for c in &self.layer1.network.connect { if c.socket.is_some() && c.address.is_some() { return Err(Problem::Invalid(format!( diff --git a/ext/config/src/tests.rs b/ext/config/src/tests.rs index f79a486..e95f03a 100644 --- a/ext/config/src/tests.rs +++ b/ext/config/src/tests.rs @@ -56,6 +56,19 @@ fn wordpress_example_parses_with_every_field() { assert_eq!(c.layer3.isolate, ["contact-form-7"]); assert_eq!(c.layer3.limits.timeout_ms.get(), 5000); assert_eq!(c.layer3.limits.memory_mb.get(), 256); + assert_eq!(c.layer3.limits.db_queries.get(), 1000); + assert_eq!(c.layer3.limits.db_result_bytes.get(), 4 << 20); +} + +#[test] +fn the_database_gateway_caps_parse_and_are_bounded() { + let l3 = |limits: &str| format!(r#"{{"layer1":{{"isolation":"off"}},"layer2":{{"enabled":false}},"layer3":{{"enabled":true,"limits":{limits}}}}}"#); + let c = parse(&l3(r#"{"dbQueries":20,"dbResultBytes":65536}"#)).expect("caps"); + assert_eq!((c.layer3.limits.db_queries.get(), c.layer3.limits.db_result_bytes.get()), (20, 65536)); + assert!(parse(&l3(r#"{"dbResultBytes":16777216}"#)).is_ok()); + let e = rejected(&l3(r#"{"dbResultBytes":16777217}"#)); + assert!(e.contains("dbResultBytes is 16777217: at most 16777216"), "{e}"); + assert!(rejected(&l3(r#"{"dbQueries":0}"#)).contains("nonzero")); } #[test] diff --git a/ext/db/src/tests.rs b/ext/db/src/tests.rs index 98f63e0..99dd235 100644 --- a/ext/db/src/tests.rs +++ b/ext/db/src/tests.rs @@ -362,6 +362,17 @@ fn core_is_unrestricted_but_for_the_allowlist() { assert_eq!(core("this is not a query"), Decision::AllowLogged("unparseable")); assert_eq!(verdict(&plain(), "this is not a query"), Decision::Refuse("unparseable")); assert_eq!(core("PREPARE s FROM 'SELECT 1'"), Decision::Refuse("prepare")); + // Layer 3's gateway (#94) caps each statement's time on the worker's connection with these, + // as core, around an isolated plugin's queries. + for sql in [ + "SELECT @@SESSION.max_statement_time", + "SET SESSION max_statement_time = 1.500", + "SET SESSION max_statement_time = 0.000000", + "SELECT @@SESSION.max_execution_time", + "SET SESSION max_execution_time = 1500", + ] { + assert_eq!(core(sql), Decision::Allow, "{sql}"); + } } // --- the WordPress shapes the parser must take (a component in place of core) ------------------- diff --git a/ext/layer3/src/gateway.rs b/ext/layer3/src/gateway.rs new file mode 100644 index 0000000..19f62d7 --- /dev/null +++ b/ext/layer3/src/gateway.rs @@ -0,0 +1,185 @@ +//! Layer 3's database gateway (addiscorp/securecore#94): what crosses the request socket when an +//! isolated plugin's `$wpdb` runs a query. The child holds no connection and no credential; its +//! `$wpdb` sends the SQL text to the php-fpm worker, which runs it on its real connection, under +//! layer 2's policy, charged to the plugin the worker opened the channel for. +//! +//! - **Child → worker:** a [`QUERY`] frame, `Q\n` and the SQL text, in place of an answer, while +//! the worker waits for the plugin's load or a call. The worker answers each one with one frame +//! (PHP-serialized: the return value, the rows, `insert_id`, `rows_affected`, `last_error`) and +//! goes back to waiting for the answer it asked for. +//! - **The worker decides what to admit** ([`admit`]), before PHP sees the text: the per-request +//! query cap, the size of the text, UTF-8 (layer 2 reads the text as UTF-8), and no change to +//! the statement time cap the worker sets for the call. Everything else is layer 2's. +//! - **The child never picks who it is:** the worker charges the query to the slug its channel +//! was opened for. Nothing in a query frame names a plugin. + +/// A child's database query frame: this tag, then the SQL text. +pub const QUERY: &[u8] = b"Q\n"; + +/// The longest SQL text the gateway takes (MySQL's and MariaDB's default `max_allowed_packet`). +pub const MAX_SQL: usize = 16 << 20; + +/// A query frame for `sql`. +pub fn encode_query(sql: &[u8]) -> Vec { + let mut out = Vec::with_capacity(QUERY.len() + sql.len()); + out.extend_from_slice(QUERY); + out.extend_from_slice(sql); + out +} + +/// The SQL text of a query frame; `None` for any other frame (an answer). +pub fn decode_query(frame: &[u8]) -> Option<&[u8]> { + frame.strip_prefix(QUERY) +} + +/// Why the worker refused a query before running it. Each fails the query in the child, as a +/// failed query does (`false`, with `last_error` set), and writes one line. +#[derive(Debug, Clone, PartialEq, Eq)] +pub enum Refusal { + /// The request's `dbQueries` are used up. + Queries(u32), + /// The text is over [`MAX_SQL`]. + TooLong(usize), + /// The text is not UTF-8, so layer 2 could not read it. + NotUtf8, + /// The text names `max_statement_time` / `max_execution_time`: the cap the worker sets on the + /// connection for the call's time is not the plugin's to change. + TimeCap, +} + +impl Refusal { + /// The reason, for the log line and the child's `last_error`. + pub fn reason(&self) -> String { + match self { + Refusal::Queries(n) => format!("over the dbQueries cap of {n} per request"), + Refusal::TooLong(n) => format!("the query is {n} bytes, over {MAX_SQL}"), + Refusal::NotUtf8 => "the query is not UTF-8".to_owned(), + Refusal::TimeCap => "it changes the statement time cap".to_owned(), + } + } + + /// Logged once per request and plugin, not per query: a loop past the cap would flood the log. + pub fn once_per_request(&self) -> bool { + matches!(self, Refusal::Queries(_)) + } +} + +/// Whether the worker runs `sql`, the plugin's `sent`-th query of the request (counting from 0), +/// under a cap of `cap` queries. `Ok` is the text, for PHP. +pub fn admit(sql: &[u8], sent: u32, cap: u32) -> Result<&str, Refusal> { + if sent >= cap { + return Err(Refusal::Queries(cap)); + } + if sql.len() > MAX_SQL { + return Err(Refusal::TooLong(sql.len())); + } + let text = std::str::from_utf8(sql).map_err(|_| Refusal::NotUtf8)?; + if touches_time_cap(text) { + return Err(Refusal::TimeCap); + } + Ok(text) +} + +fn touches_time_cap(sql: &str) -> bool { + let lower = sql.to_ascii_lowercase(); + lower.contains("max_statement_time") || lower.contains("max_execution_time") +} + +/// What a statement does to a transaction on the connection: `Some(true)` opens one, +/// `Some(false)` ends it, `None` neither. The worker rolls back a transaction a plugin left open +/// when its child dies, so core's later writes in the request don't join it. +pub fn transaction_effect(sql: &str) -> Option { + let words: Vec = sql + .trim_start() + .split(|c: char| c.is_whitespace() || c == ';') + .filter(|w| !w.is_empty()) + .take(3) + .map(str::to_ascii_uppercase) + .collect(); + let w = |i: usize| words.get(i).map_or("", String::as_str); + match (w(0), w(1)) { + ("BEGIN", _) | ("START", "TRANSACTION") => Some(true), + // ROLLBACK TO [SAVEPOINT] x keeps the transaction open. + ("ROLLBACK", "TO") => None, + ("COMMIT" | "ROLLBACK", _) => Some(false), + ("SET", _) => { + let compact: String = sql.chars().filter(|c| !c.is_whitespace()).collect::().to_ascii_uppercase(); + if compact.contains("AUTOCOMMIT=0") || compact.contains("AUTOCOMMIT=OFF") { + Some(true) + } else if compact.contains("AUTOCOMMIT=1") || compact.contains("AUTOCOMMIT=ON") { + Some(false) + } else { + None + } + } + _ => None, + } +} + +/// The worker's answer for a query it refused or could not run: what a failed `$wpdb` query +/// leaves (`false`, and `message` as `last_error`), PHP-serialized as the worker's PHP answers. +pub fn failed_answer(message: &str) -> Vec { + format!(r#"a:2:{{s:3:"ret";b:0;s:5:"error";s:{}:"{message}";}}"#, message.len()).into_bytes() +} + +#[cfg(test)] +mod tests { + use super::*; + + #[test] + fn query_frames_round_trip_and_answers_are_not_queries() { + let f = encode_query(b"SELECT 1"); + assert_eq!(decode_query(&f), Some(&b"SELECT 1"[..])); + assert_eq!(decode_query(b"a:3:{}"), None, "a serialized answer is never a query"); + assert_eq!(decode_query(b"Q"), None); + assert_eq!(decode_query(b"Q\n"), Some(&b""[..])); + } + + #[test] + fn admit_counts_sizes_and_reads_utf8() { + assert_eq!(admit(b"SELECT 1", 0, 1), Ok("SELECT 1")); + assert_eq!(admit(b"SELECT 1", 1, 1), Err(Refusal::Queries(1))); + assert_eq!(admit(b"SELECT 1", 7, 3), Err(Refusal::Queries(3))); + let long = vec![b'x'; MAX_SQL + 1]; + assert_eq!(admit(&long, 0, 10), Err(Refusal::TooLong(MAX_SQL + 1))); + assert_eq!(admit(&long[..MAX_SQL], 0, 10).map(str::len), Ok(MAX_SQL)); + // Layer 2 reads text: a byte that is not UTF-8 (hidden in a comment) is refused here. + assert_eq!(admit(b"SELECT LOAD_FILE('/etc/passwd') /* \xff */", 0, 10), Err(Refusal::NotUtf8)); + assert_eq!(admit("SELECT 'é'".as_bytes(), 0, 10), Ok("SELECT 'é'")); + } + + #[test] + fn the_time_cap_is_not_the_plugins() { + for sql in [ + "SET SESSION max_statement_time = 0", + "set max_execution_time=0", + "SET STATEMENT MAX_STATEMENT_TIME=100 FOR SELECT SLEEP(100)", + "SELECT @@max_statement_time", + ] { + assert_eq!(admit(sql.as_bytes(), 0, 10), Err(Refusal::TimeCap), "{sql}"); + } + assert!(admit(b"SET SESSION sql_mode = 'TRADITIONAL'", 0, 10).is_ok()); + } + + #[test] + fn transactions_are_tracked_by_their_control_statements() { + assert_eq!(transaction_effect("START TRANSACTION"), Some(true)); + assert_eq!(transaction_effect(" begin;"), Some(true)); + assert_eq!(transaction_effect("start transaction read only"), Some(true)); + assert_eq!(transaction_effect("COMMIT"), Some(false)); + assert_eq!(transaction_effect("rollback"), Some(false)); + assert_eq!(transaction_effect("ROLLBACK TO SAVEPOINT a"), None); + assert_eq!(transaction_effect("SET autocommit = 0"), Some(true)); + assert_eq!(transaction_effect("SET autocommit=1"), Some(false)); + assert_eq!(transaction_effect("SELECT 'BEGIN'"), None); + assert_eq!(transaction_effect("INSERT INTO t VALUES (1)"), None); + } + + #[test] + fn a_failed_answer_is_valid_php_serialize() { + let a = failed_answer("securecore: nope"); + assert_eq!(a, br#"a:2:{s:3:"ret";b:0;s:5:"error";s:16:"securecore: nope";}"#); + // The byte length, not the character count, as PHP's serialize() writes it. + assert!(String::from_utf8(failed_answer("é")).unwrap().contains(r#"s:2:"é""#)); + } +} diff --git a/ext/layer3/src/lib.rs b/ext/layer3/src/lib.rs index 9724426..b38ec17 100644 --- a/ext/layer3/src/lib.rs +++ b/ext/layer3/src/lib.rs @@ -6,7 +6,9 @@ //! never a secret; #47). //! - [`wire`] (Linux): the control socket, the per-request socket and their messages. //! - [`child`] (Linux): the child's sandbox (gelada) and rlimits. +//! - [`gateway`]: the database gateway's frames and the worker's admission rules (#94). +pub mod gateway; pub mod options; #[cfg(target_os = "linux")] diff --git a/ext/layer3/src/wire.rs b/ext/layer3/src/wire.rs index cc899e7..78b261e 100644 --- a/ext/layer3/src/wire.rs +++ b/ext/layer3/src/wire.rs @@ -248,13 +248,39 @@ pub struct Hello { pub snapshot_sha: String, /// sha256 of the worker's live securecore.json: the child reloads when it differs. pub config_sha: String, + /// The worker's connection facts the child's `$wpdb` reports without a connection (no + /// credential: no host, user, password or database name). + pub db: DbFacts, } -const HELLO_MAGIC: &str = "securecore-layer3/1"; +/// What `$wpdb` knows about its connection that WordPress and plugins read without querying: +/// `$wpdb->charset` / `collate` (`get_charset_collate()`, dbDelta), `db_version()` (`has_cap()`) +/// and `db_server_info()`. +#[derive(Debug, Clone, Default, PartialEq, Eq)] +pub struct DbFacts { + pub charset: String, + pub collate: String, + pub version: String, + pub server_info: String, +} + +const HELLO_MAGIC: &str = "securecore-layer3/2"; impl Hello { pub fn encode(&self) -> Result, String> { - let fields = [&self.slug, &self.file, &self.abspath, &self.prefix, &self.snapshot_sha, &self.config_sha]; + let d = &self.db; + let fields = [ + &self.slug, + &self.file, + &self.abspath, + &self.prefix, + &self.snapshot_sha, + &self.config_sha, + &d.charset, + &d.collate, + &d.version, + &d.server_info, + ]; if let Some(bad) = fields.iter().find(|f| f.contains('\n') || f.contains('\0')) { return Err(format!("layer3: a hello field contains a newline or NUL: {bad:?}")); } @@ -273,7 +299,15 @@ impl Hello { return Err("layer3: not a hello".into()); } let mut next = || lines.next().map(str::to_owned).ok_or_else(|| "layer3: hello is short".to_owned()); - let hello = Hello { slug: next()?, file: next()?, abspath: next()?, prefix: next()?, snapshot_sha: next()?, config_sha: next()? }; + let hello = Hello { + slug: next()?, + file: next()?, + abspath: next()?, + prefix: next()?, + snapshot_sha: next()?, + config_sha: next()?, + db: DbFacts { charset: next()?, collate: next()?, version: next()?, server_info: next()? }, + }; if lines.next().is_some() { return Err("layer3: hello has extra lines".into()); } @@ -323,8 +357,16 @@ mod tests { prefix: "wp_".into(), snapshot_sha: "ab".repeat(32), config_sha: "cd".repeat(32), + db: DbFacts { + charset: "utf8mb4".into(), + collate: "utf8mb4_unicode_520_ci".into(), + version: "11.4.4".into(), + server_info: "11.4.4-MariaDB-ubu2404".into(), + }, }; assert_eq!(Hello::decode(&h.encode().unwrap()).unwrap(), h); + let bad = Hello { db: DbFacts { server_info: "x\ny".into(), ..h.db.clone() }, ..h.clone() }; + assert!(bad.encode().is_err(), "a newline in the db facts is refused too"); let bad = Hello { slug: "a\nb".into(), ..h.clone() }; assert!(bad.encode().is_err()); assert!(Hello::decode(b"other\n").is_err()); diff --git a/ext/php/src/attribution.rs b/ext/php/src/attribution.rs index 976ed1b..76bafc1 100644 --- a/ext/php/src/attribution.rs +++ b/ext/php/src/attribution.rs @@ -174,7 +174,9 @@ pub fn db_caller(root: &Path, from: *const zend_execute_data) -> Component { let frames: Vec> = named .iter() .map(|f| securecore_db::caller::Frame { - component: map.frame(&layout, now, f.file), + // Layer 3's database gateway (#94): the stand-in running an isolated plugin's query + // is that plugin, so its query is charged as the plugin's own `$wpdb` call would be. + component: crate::layer3::gateway_component(f.file).unwrap_or_else(|| map.frame(&layout, now, f.file)), file: f.file, function: f.function.as_deref(), db_layer: f.db_layer, diff --git a/ext/php/src/layer3.rs b/ext/php/src/layer3.rs index 11cf63b..47bceca 100644 --- a/ext/php/src/layer3.rs +++ b/ext/php/src/layer3.rs @@ -39,7 +39,7 @@ use ext_php_rs::types::{ZendHashTable, ZendStr, Zval}; use securecore_attribution::Component; use securecore_config::Loaded; use securecore_layer3::child::{self, Plan}; -use securecore_layer3::options; +use securecore_layer3::{gateway, options}; use securecore_layer3::wire::{self, Hello}; use crate::{LOG_NOTICE, LOG_WARNING, attribution, log}; @@ -197,14 +197,66 @@ fn close_from(first: RawFd) { /// its callbacks pass through for the rest of the request. struct Channel { sock: OwnedFd, + /// The child's pid, from its ready frame (0 while it loads). pid: i32, + /// What the worker is waiting for from the child, and until when. + waiting: Option, + /// The query the child sent, until the stand-in takes it ([`securecore_layer3_db_begin`]). + pending: Option, + /// The stand-in is running this channel's query: a call into the plugin passes through + /// meanwhile (the child is waiting for the answer, not for a call). + busy: bool, + /// Queries sent through the gateway this request (`layer3.limits.dbQueries`). + queries: u32, + /// The query cap's line was written (once per request). + cap_logged: bool, + /// The plugin's last transaction-control statement left a transaction open. + transaction: bool, +} + +#[derive(Debug, Clone, Copy)] +struct Waiting { + deadline: Instant, + phase: Phase, +} + +#[derive(Debug, Clone, Copy)] +enum Phase { + /// The plugin is loading: the answer is the ready frame. + Load, + /// A call to callback `id`: the answer is its result. + Call(i64), } static CHANNELS: Mutex>>> = Mutex::new(None); +/// Slugs whose query the stand-in is running now (a stack: a query's `query` filter may call +/// another isolated plugin that queries in turn). Read by [`gateway_component`]. +static GATEWAY: Mutex> = Mutex::new(Vec::new()); +/// Slugs whose child died with a transaction open: the stand-in rolls it back. +static ROLLBACK: Mutex> = Mutex::new(Vec::new()); + +/// The name the stand-in for `slug` is compiled under (its frames' filename). +fn stub_name(slug: &str) -> String { + format!("{STUB_PREFIX}{slug}") +} +const STUB_PREFIX: &str = "securecore-layer3:"; + +/// The database gateway's attribution (#94): a frame of the stand-in for an isolated plugin +/// whose child's query the stand-in is running now is that plugin. So the query is charged to +/// the plugin by layer 2's own caller rule ([`crate::attribution::db_caller`]), exactly as its +/// `$wpdb->query()` would be in the worker; code that a filter on the query runs is charged to +/// itself, as ever. Only the extension compiles code under that name, and the slug is the one the +/// worker opened the channel for: nothing the child sends names it. +pub(crate) fn gateway_component(file: &str) -> Option { + let slug = file.strip_prefix(STUB_PREFIX)?; + lock(&GATEWAY).iter().any(|s| s == slug).then(|| Component::Plugin(slug.to_owned())) +} /// At request end: close every child's socket; each child exits. pub(crate) fn request_shutdown() { lock(&CHANNELS).take(); + lock(&GATEWAY).clear(); + lock(&ROLLBACK).clear(); } /// Whether the `zend_compile_file` wrapper is needed for layer 3: a worker with plugins to @@ -268,7 +320,7 @@ fn php_quote(s: &str) -> String { fn stub(slug: &str, file: &str) -> *mut zend_op_array { let body = STUB_PHP.trim_start().strip_prefix(" Zval { - let mut out = Zval::new(); +pub fn securecore_layer3_open(slug: String, file: String, abspath: String, prefix: String, snapshot: &Zval, db: Vec) -> Zval { + let out = Zval::new(); let Some(live) = crate::live_arc() else { return out }; if role() != Role::Fpm || !live.config.layer3.enabled || !live.config.layer3.isolate.contains(&slug) { return out; @@ -311,6 +365,7 @@ pub fn securecore_layer3_open(slug: String, file: String, abspath: String, prefi } let Some(&control) = CONTROL.get() else { return out }; let snapshot = snapshot.zend_str().map(|s| s.as_bytes().to_vec()).unwrap_or_default(); + let fact = |i: usize| db.get(i).cloned().unwrap_or_default(); let hello = Hello { slug: slug.clone(), file, @@ -318,22 +373,34 @@ pub fn securecore_layer3_open(slug: String, file: String, abspath: String, prefi prefix, snapshot_sha: securecore_config::sha256_hex(&snapshot), config_sha: live.sha256.clone(), + db: wire::DbFacts { charset: fact(0), collate: fact(1), version: fact(2), server_info: fact(3) }, }; let timeout = Duration::from_millis(u64::from(live.config.layer3.limits.timeout_ms.get())); match open(control, &hello, &snapshot, timeout) { - Ok((channel, payload)) => { - channels.insert(slug, Some(channel)); - out.set_binary(payload); + Ok((sock, first, deadline)) => { + let channel = Channel { + sock, + pid: 0, + waiting: Some(Waiting { deadline, phase: Phase::Load }), + pending: None, + busy: false, + queries: 0, + cap_logged: false, + transaction: false, + }; + let slot = channels.entry(slug.clone()).or_insert(Some(channel)); + pump(&slug, slot, Some(first)) } Err(e) => { log(LOG_WARNING, &format!("layer3 plugin:{slug} not loaded: {e}")); channels.insert(slug, None); + out } } - out } -fn open(control: RawFd, hello: &Hello, snapshot: &[u8], timeout: Duration) -> Result<(Channel, Vec), String> { +/// The hand-off: the request socket, the first frame after it and the deadline for the load. +fn open(control: RawFd, hello: &Hello, snapshot: &[u8], timeout: Duration) -> Result<(OwnedFd, Vec, Instant), String> { let (ours, theirs) = wire::socketpair(libc::SOCK_STREAM).map_err(|e| format!("socketpair: {e}"))?; // SAFETY: the control socket, open for the process's life. let control = unsafe { BorrowedFd::borrow_raw(control) }; @@ -344,53 +411,225 @@ fn open(control: RawFd, hello: &Hello, snapshot: &[u8], timeout: Duration) -> Re if e.kind() == io::ErrorKind::TimedOut { format!("timed out waiting for {what}") } else { format!("{what}: {e}") } }; wire::write_frame(ours.as_fd(), &hello.encode()?, Some(start + HANDSHAKE)).map_err(|e| fail("the helper", e))?; - let mut frame = wire::read_frame(ours.as_fd(), Some(start + HANDSHAKE + timeout)).map_err(|e| fail("the child", e))?; + let mut deadline = start + HANDSHAKE + timeout; + let mut frame = wire::read_frame(ours.as_fd(), Some(deadline)).map_err(|e| fail("the child", e))?; if frame.as_deref() == Some(wire::NEED_SNAPSHOT) { wire::write_frame(ours.as_fd(), snapshot, Some(Instant::now() + HANDSHAKE)).map_err(|e| fail("the helper", e))?; - frame = wire::read_frame(ours.as_fd(), Some(Instant::now() + BOOT_ALLOWANCE + timeout)).map_err(|e| fail("the helper to boot", e))?; + deadline = Instant::now() + BOOT_ALLOWANCE + timeout; + frame = wire::read_frame(ours.as_fd(), Some(deadline)).map_err(|e| fail("the helper to boot", e))?; } let frame = frame.ok_or("the helper closed the request")?; - if let Some(error) = frame.strip_prefix(wire::ERROR) { - return Err(String::from_utf8_lossy(error).into_owned()); - } - let (pid, payload) = wire::decode_ready(&frame).ok_or("the child's answer is not a ready frame")?; - Ok((Channel { sock: ours, pid }, payload.to_vec())) + Ok((ours, frame, deadline)) } /// The worker's side of one call into an isolated plugin's callback `id`: the child's answer -/// (PHP-serialized), or null when the call failed (and the child is gone). +/// (PHP-serialized); or a query the callback sent through the gateway (an array: run it, then +/// [`securecore_layer3_db_reply`]); or null when the call failed (and the child is gone), or the +/// plugin is waiting on its own query (a `query` filter of its own, re-entered). #[php_function] #[php(name = "securecore_layer3_call")] pub fn securecore_layer3_call(slug: String, id: i64, args: &Zval) -> Zval { - let mut out = Zval::new(); + let out = Zval::new(); let timeout_ms = crate::with_live(|l| l.config.layer3.limits.timeout_ms.get()).unwrap_or(5000); let mut channels = lock(&CHANNELS); let Some(slot) = channels.as_mut().and_then(|c| c.get_mut(&slug)) else { return out }; - let Some(channel) = slot.as_ref() else { return out }; + let Some(channel) = slot.as_mut() else { return out }; + if channel.busy || channel.waiting.is_some() { + log(LOG_NOTICE, &format!("layer3 plugin:{slug} call {id} passed through: the plugin is waiting on its own query")); + return out; + } let mut body = format!("{id}\n").into_bytes(); body.extend_from_slice(args.zend_str().map_or(&b""[..], |s| s.as_bytes())); let deadline = Instant::now() + Duration::from_millis(u64::from(timeout_ms)); - let answer = wire::write_frame(channel.sock.as_fd(), &body, Some(deadline)).and_then(|()| wire::read_frame(channel.sock.as_fd(), Some(deadline))); - match answer { - Ok(Some(frame)) => out.set_binary(frame), - Ok(None) => { - log(LOG_WARNING, &format!("layer3 plugin:{slug} child {} ended during call {id}", channel.pid)); - *slot = None; + channel.waiting = Some(Waiting { deadline, phase: Phase::Call(id) }); + if let Err(e) = wire::write_frame(channel.sock.as_fd(), &body, Some(deadline)) { + fail(&slug, slot, Failure::Io(e)); + return out; + } + pump(&slug, slot, None) +} + +/// Why a channel ended mid-exchange. +enum Failure { + /// The child closed its socket (it exited, or died). + Ended, + /// The deadline passed. + TimedOut, + Io(io::Error), +} + +/// Ends `slot`'s channel after a failed exchange, with the line for it; a child past its deadline +/// is killed. A transaction the plugin left open is noted for the stand-in to roll back. +fn fail(slug: &str, slot: &mut Option, failure: Failure) { + let Some(ch) = slot.take() else { return }; + let phase = ch.waiting.map_or(Phase::Load, |w| w.phase); + let timeout_ms = crate::with_live(|l| l.config.layer3.limits.timeout_ms.get()).unwrap_or(5000); + let pid = ch.pid; + let line = match (phase, &failure) { + (Phase::Load, Failure::Ended) => "not loaded: the child closed the request".to_owned(), + (Phase::Load, Failure::TimedOut) => "not loaded: timed out waiting for the child".to_owned(), + (Phase::Load, Failure::Io(e)) => format!("not loaded: the child: {e}"), + (Phase::Call(id), Failure::Ended) => format!("child {pid} ended during call {id}"), + (Phase::Call(id), Failure::TimedOut) => format!("call {id} timed out after {timeout_ms} ms; child {pid} killed"), + (Phase::Call(id), Failure::Io(e)) => format!("child {pid} failed during call {id}: {e}"), + }; + log(LOG_WARNING, &format!("layer3 plugin:{slug} {line}")); + if matches!(failure, Failure::TimedOut) + && pid > 0 + && let Some(&control) = CONTROL.get() + { + // SAFETY: the control socket, open for the process's life. + let _ = wire::send_packet(unsafe { BorrowedFd::borrow_raw(control) }, &wire::encode_kill(pid), None); + } + if ch.transaction { + lock(&ROLLBACK).push(slug.to_owned()); + } +} + +/// Reads the child's frames until the answer the channel waits for. A query frame on the way is +/// admitted ([`gateway::admit`]) and handed to the stand-in (an array with the milliseconds left +/// before the deadline), or answered as refused without PHP seeing it. Returns the answer (a +/// string), a query (an array), or null when the channel failed. +fn pump(slug: &str, slot: &mut Option, mut first: Option>) -> Zval { + let mut out = Zval::new(); + let cap = crate::with_live(|l| l.config.layer3.limits.db_queries.get()).unwrap_or(1000); + loop { + let Some(ch) = slot.as_mut() else { return out }; + let Some(w) = ch.waiting else { return out }; + let frame = match first.take().map(|f| Ok(Some(f))).unwrap_or_else(|| wire::read_frame(ch.sock.as_fd(), Some(w.deadline))) { + Ok(Some(f)) => f, + Ok(None) => { + fail(slug, slot, Failure::Ended); + return out; + } + Err(e) if e.kind() == io::ErrorKind::TimedOut => { + fail(slug, slot, Failure::TimedOut); + return out; + } + Err(e) => { + fail(slug, slot, Failure::Io(e)); + return out; + } + }; + if let Some(sql) = gateway::decode_query(&frame) { + match gateway::admit(sql, ch.queries, cap) { + Ok(text) => { + ch.queries += 1; + if let Some(open) = gateway::transaction_effect(text) { + ch.transaction = open; + } + ch.pending = Some(text.to_owned()); + let left = w.deadline.saturating_duration_since(Instant::now()); + let mut q = ZendHashTable::new(); + let ms = i64::try_from(left.as_millis()).unwrap_or(i64::MAX).max(1); + if q.insert("ms", ms).is_err() { + return out; + } + out.set_hashtable(q); + return out; + } + Err(refusal) => { + if !refusal.once_per_request() || !ch.cap_logged { + log(LOG_WARNING, &format!("layer3 plugin:{slug} database refused: {}", refusal.reason())); + ch.cap_logged |= refusal.once_per_request(); + } + let answer = gateway::failed_answer(&format!("securecore: layer3 refused this query ({})", refusal.reason())); + if let Err(e) = wire::write_frame(ch.sock.as_fd(), &answer, Some(w.deadline)) { + let failure = if e.kind() == io::ErrorKind::TimedOut { Failure::TimedOut } else { Failure::Io(e) }; + fail(slug, slot, failure); + return out; + } + continue; + } + } } - Err(e) if e.kind() == io::ErrorKind::TimedOut => { - log(LOG_WARNING, &format!("layer3 plugin:{slug} call {id} timed out after {timeout_ms} ms; child {} killed", channel.pid)); - if let Some(&control) = CONTROL.get() { - // SAFETY: the control socket, open for the process's life. - let _ = wire::send_packet(unsafe { BorrowedFd::borrow_raw(control) }, &wire::encode_kill(channel.pid), None); + // The answer the channel was waiting for. + ch.waiting = None; + match w.phase { + Phase::Call(_) => out.set_binary(frame), + Phase::Load => { + if let Some(error) = frame.strip_prefix(wire::ERROR) { + log(LOG_WARNING, &format!("layer3 plugin:{slug} not loaded: {}", String::from_utf8_lossy(error))); + *slot = None; + } else if let Some((pid, payload)) = wire::decode_ready(&frame) { + ch.pid = pid; + out.set_binary(payload.to_vec()); + } else { + log(LOG_WARNING, &format!("layer3 plugin:{slug} not loaded: the child's answer is not a ready frame")); + *slot = None; + } } - *slot = None; } - Err(e) => { - log(LOG_WARNING, &format!("layer3 plugin:{slug} child {} failed during call {id}: {e}", channel.pid)); - *slot = None; + return out; + } +} + +/// The stand-in takes the query the child sent (the text), and runs it now: until +/// [`securecore_layer3_db_reply`], its frames are the plugin's ([`gateway_component`]). Null when +/// there is none. +#[php_function] +#[php(name = "securecore_layer3_db_begin")] +pub fn securecore_layer3_db_begin(slug: String) -> Option { + let mut channels = lock(&CHANNELS); + let ch = channels.as_mut()?.get_mut(&slug)?.as_mut()?; + let sql = ch.pending.take()?; + ch.busy = true; + lock(&GATEWAY).push(slug); + Some(sql) +} + +/// The stand-in's answer to the child's query (PHP-serialized). Sends it (or a refusal, when it +/// is over `layer3.limits.dbResultBytes` or the deadline passed), then waits on as +/// [`securecore_layer3_call`] does: the answer, the next query, or null. +#[php_function] +#[php(name = "securecore_layer3_db_reply")] +pub fn securecore_layer3_db_reply(slug: String, answer: &Zval) -> Zval { + let out = Zval::new(); + let cap = crate::with_live(|l| l.config.layer3.limits.db_result_bytes.get()).unwrap_or(4 << 20); + { + let mut gateway = lock(&GATEWAY); + if let Some(i) = gateway.iter().rposition(|s| *s == slug) { + gateway.remove(i); } } - out + let mut channels = lock(&CHANNELS); + let Some(slot) = channels.as_mut().and_then(|c| c.get_mut(&slug)) else { return out }; + let Some(ch) = slot.as_mut() else { return out }; + if !ch.busy { + return out; + } + ch.busy = false; + let Some(w) = ch.waiting else { return out }; + if Instant::now() >= w.deadline { + fail(&slug, slot, Failure::TimedOut); + return out; + } + let bytes = answer.zend_str().map_or(&b""[..], |s| s.as_bytes()); + let refused; + let bytes = if bytes.len() > cap as usize { + log(LOG_WARNING, &format!("layer3 plugin:{slug} database answer of {} bytes refused: over the dbResultBytes cap of {cap}", bytes.len())); + refused = gateway::failed_answer(&format!("securecore: layer3 refused this answer (over the dbResultBytes cap of {cap})")); + &refused[..] + } else { + bytes + }; + if let Err(e) = wire::write_frame(ch.sock.as_fd(), bytes, Some(w.deadline)) { + let failure = if e.kind() == io::ErrorKind::TimedOut { Failure::TimedOut } else { Failure::Io(e) }; + fail(&slug, slot, failure); + return out; + } + pump(&slug, slot, None) +} + +/// Whether the plugin's child died with a transaction open on the worker's connection (once): +/// the stand-in then rolls it back, so core's later writes don't join it. +#[php_function] +#[php(name = "securecore_layer3_take_rollback")] +pub fn securecore_layer3_take_rollback(slug: String) -> bool { + let mut owed = lock(&ROLLBACK); + let before = owed.len(); + owed.retain(|s| *s != slug); + owed.len() != before } // --- the helper: supervisor --------------------------------------------------------------------- @@ -694,6 +933,10 @@ fn become_child(hello: &Hello, _sock: RawFd) -> Result, Stri info.insert("slug", hello.slug.as_str()).map_err(|e| e.to_string())?; info.insert("file", hello.file.as_str()).map_err(|e| e.to_string())?; info.insert("memory_mb", i64::from(memory_mb)).map_err(|e| e.to_string())?; + let d = &hello.db; + for (k, v) in [("charset", &d.charset), ("collate", &d.collate), ("db_version", &d.version), ("db_server_info", &d.server_info)] { + info.insert(k, v.as_str()).map_err(|e| e.to_string())?; + } Ok(info) } @@ -729,6 +972,21 @@ pub fn securecore_layer3_send(payload: &Zval) -> bool { child_sock(|s| wire::write_frame(s, bytes, None).is_ok()).unwrap_or(false) } +/// In a child: one query through the database gateway. Sends the SQL to the worker, which runs it +/// on its connection under layer 2, charged to this plugin; returns the worker's answer +/// (PHP-serialized), or null when the worker is gone. +#[php_function] +#[php(name = "securecore_layer3_db")] +pub fn securecore_layer3_db(sql: &Zval) -> Zval { + let mut out = Zval::new(); + let frame = gateway::encode_query(sql.zend_str().map_or(&b""[..], |s| s.as_bytes())); + let answer = child_sock(|s| wire::write_frame(s, &frame, None).and_then(|()| wire::read_frame(s, None))); + if let Some(Ok(Some(a))) = answer { + out.set_binary(a); + } + out +} + /// The child's next call from the worker, or null when the worker's request is over. #[php_function] #[php(name = "securecore_layer3_recv")] @@ -746,6 +1004,10 @@ pub(crate) fn register(module: ModuleBuilder) -> ModuleBuilder { .function(wrap_function!(securecore_layer3_snapshot_names)) .function(wrap_function!(securecore_layer3_open)) .function(wrap_function!(securecore_layer3_call)) + .function(wrap_function!(securecore_layer3_db_begin)) + .function(wrap_function!(securecore_layer3_db_reply)) + .function(wrap_function!(securecore_layer3_take_rollback)) + .function(wrap_function!(securecore_layer3_db)) .function(wrap_function!(securecore_layer3_helper)) .function(wrap_function!(securecore_layer3_zygote)) .function(wrap_function!(securecore_layer3_ready)) diff --git a/ext/php/src/layer3/helper.php b/ext/php/src/layer3/helper.php index 1836361..42209c4 100644 --- a/ext/php/src/layer3/helper.php +++ b/ext/php/src/layer3/helper.php @@ -19,22 +19,28 @@ final class State { - /** True once forked into a child: the database is closed to it. */ + /** True once forked into a child: $wpdb is the database gateway from then on. */ public static bool $child = false; public static string $slug = ''; /** @var array id => the plugin's callback */ public static array $callbacks = []; public static int $next = 1; - /** @var array queries already logged as refused in this child */ - public static array $refused = []; } -/** A $wpdb with no connection: during the zygote's boot it serves wp_load_alloptions() from the - * snapshot and answers everything else empty; in a child it refuses every query. */ +/** A $wpdb with no connection (and no credential). During the zygote's boot it serves + * wp_load_alloptions() from the snapshot and answers everything else empty. In a child it is + * the database gateway (#94): each query goes to the php-fpm worker, which runs it on its own + * connection under layer 2's policy, charged to this plugin, and answers with what its $wpdb + * saw. Everything else (get_results, get_row, get_var, get_col, prepare, insert, update, + * delete, replace, dbDelta) is wpdb's own code, on top of query(). */ function make_wpdb(string $prefix, array $options): \wpdb { return new class($prefix, $options) extends \wpdb { private array $snapshot; + private string $server_info = '8.0.0'; + private string $version = '8.0.0'; + /** @var list the last result's column names (get_col_info('name')) */ + private array $col_names = []; public function __construct(string $prefix, array $options) { @@ -46,16 +52,75 @@ public function __construct(string $prefix, array $options) $this->snapshot = $options; } + /** In the child: the worker's connection facts (never a credential). */ + public function become_gateway(array $facts): void + { + $this->charset = (string) ($facts['charset'] ?? ''); + $this->collate = (string) ($facts['collate'] ?? ''); + $this->version = (string) ($facts['db_version'] ?? '') ?: '8.0.0'; + $this->server_info = (string) ($facts['db_server_info'] ?? '') ?: $this->version; + $this->snapshot = []; + } + public function query($query) { + if (!State::$child) { + $this->flush(); + $this->last_query = (string) $query; + return false; + } + // As wpdb::query(): the plugin's own `query` filters run here, the worker's there. + $query = apply_filters('query', $query); + if (!$query) { + $this->insert_id = 0; + return false; + } $this->flush(); + $this->func_call = "\$db->query(\"$query\")"; $this->last_query = (string) $query; - if (State::$child) { - refused_query((string) $query); + ++$this->num_queries; + $answer = \securecore_layer3_db((string) $query); + $a = is_string($answer) ? unserialize($answer, ['allowed_classes' => false]) : null; + if (!is_array($a)) { + $this->insert_id = 0; + $this->last_error = 'securecore: the database gateway did not answer'; + return false; + } + $this->last_error = (string) ($a['error'] ?? ''); + $this->rows_affected = (int) ($a['affected'] ?? 0); + // Only an INSERT/REPLACE sets it, as in wpdb::query(). + if (array_key_exists('insert_id', $a)) $this->insert_id = (int) $a['insert_id']; + $this->col_names = is_array($a['cols'] ?? null) ? array_values($a['cols']) : []; + $keys = is_array($a['keys'] ?? null) ? array_values($a['keys']) : []; + $this->last_result = []; + foreach (is_array($a['rows'] ?? null) ? $a['rows'] : [] as $row) { + if (!is_array($row) || count($row) !== count($keys)) continue; + $this->last_result[] = (object) array_combine($keys, array_values($row)); } - return false; + $this->num_rows = (int) ($a['num_rows'] ?? count($this->last_result)); + $ret = $a['ret'] ?? false; + return is_int($ret) || is_bool($ret) ? $ret : false; + } + + /** Escapes as mysqli_real_escape_string() does on a utf8mb4 (any ASCII-compatible) + * connection, so the worker receives the same text the plugin would send unisolated. + * Layer 2 refuses the charsets where this would differ. */ + public function _real_escape($data) + { + if (!is_scalar($data)) return ''; + $escaped = strtr((string) $data, ["\\" => "\\\\", "\0" => "\\0", "\n" => "\\n", "\r" => "\\r", "'" => "\\'", '"' => '\\"', "\x1a" => "\\Z"]); + return $this->add_placeholder_escape($escaped); + } + + public function get_col_info($info_type = 'name', $col_offset = -1) + { + // Names only: the rest of mysqli's field metadata does not cross (see ext/README.md). + if ($info_type !== 'name' || $this->col_names === []) return null; + return $col_offset === -1 ? $this->col_names : ($this->col_names[$col_offset] ?? null); } + protected function load_col_info() {} + public function get_results($query = null, $output = OBJECT) { if (!State::$child && is_string($query) && stripos($query, 'option_name') !== false && stripos($query, 'autoload') !== false) { @@ -68,32 +133,13 @@ public function get_results($query = null, $output = OBJECT) return parent::get_results($query, $output); } - public function _real_escape($data) - { - if (!is_scalar($data)) return ''; - return $this->add_placeholder_escape(addslashes((string) $data)); - } - - public function db_version() { return '8.0.0'; } - public function db_server_info() { return '8.0.0'; } - public function get_col_info($info_type = 'name', $col_offset = -1) { return []; } + public function db_version() { return $this->version; } + public function db_server_info() { return $this->server_info; } public function check_connection($allow_bail = true) { return true; } public function close() { return true; } - - /** The child keeps none of the snapshot (#47): it only carried the boot. */ - public function drop_snapshot(): void { $this->snapshot = []; } }; } -function refused_query(string $sql): void -{ - $sql = preg_replace('/\s+/', ' ', trim($sql)); - if (strlen($sql) > 160) $sql = substr($sql, 0, 160) . '...'; - if (isset(State::$refused[$sql])) return; - State::$refused[$sql] = true; - error_log('securecore: layer3 plugin:' . State::$slug . ' database refused (not yet available to an isolated plugin): ' . $sql); -} - /** Objects and resources don't cross to the worker yet: they become null. */ function plain($v, int $depth = 0) { @@ -207,7 +253,8 @@ function serve(array $before, string $out, ?string $error): void $request = \securecore_layer3_zygote(); State::$child = true; State::$slug = $request['slug']; -$GLOBALS['wpdb']->drop_snapshot(); +// The child keeps none of the snapshot (#47): it only carried the boot. +$GLOBALS['wpdb']->become_gateway($request); @ini_set('memory_limit', $request['memory_mb'] . 'M'); // The plugin loads at global scope, as wp-settings.php loads it (its top-level variables are // globals), with its output captured. diff --git a/ext/php/src/layer3/stub.php b/ext/php/src/layer3/stub.php index 373df64..846ce8f 100644 --- a/ext/php/src/layer3/stub.php +++ b/ext/php/src/layer3/stub.php @@ -5,6 +5,12 @@ // It asks the helper for a child that loads the plugin, and mirrors what the plugin registered // there (hooks and shortcodes) as callbacks that forward each call to the child. // +// The database gateway (#94): while the plugin loads or a call runs, the child may send queries +// instead of its answer. Each one comes back here as an array; $run takes its text from the +// extension and runs it on this worker's $wpdb, where layer 2 checks it as the plugin's own +// (the extension charges this file's frames to the plugin while it runs), and the answer goes +// back to the child. The call's remaining time caps each statement on the server. +// // If the child can't be had, or a call fails (timeout, memory, a crash), the page goes on: a // filter passes its value through unchanged, a shortcode renders empty, an action does nothing. (static function (string $slug, string $file): void { @@ -16,14 +22,85 @@ } return $v; }; + // One query from the child, on this worker's connection: what $wpdb saw, PHP-serialized. + $run = static function () use ($slug): string { + global $wpdb; + $sql = \securecore_layer3_db_begin($slug); + if (!is_string($sql)) { + return serialize(['ret' => false, 'error' => 'securecore: layer3 had no query to run']); + } + $ret = false; + $cols = []; + try { + $ret = $wpdb->query($sql); + $error = (string) $wpdb->last_error; + if ($ret !== false && ($wpdb->result ?? null) instanceof \mysqli_result) { + $cols = (array) $wpdb->get_col_info('name'); + } + } catch (\Throwable $e) { + $ret = false; + $error = 'securecore: ' . get_class($e) . ': ' . $e->getMessage(); + } + $keys = []; + $rows = []; + foreach (is_array($wpdb->last_result) ? $wpdb->last_result : [] as $row) { + $v = is_object($row) ? get_object_vars($row) : []; + if ($keys === []) $keys = array_keys($v); + $rows[] = array_values($v); + } + $a = [ + 'ret' => is_int($ret) || is_bool($ret) ? $ret : false, + 'error' => $error, + 'keys' => $keys, + 'rows' => $rows, + 'cols' => $cols, + 'num_rows' => count($rows), + ]; + // As wpdb::query() sets them: rows_affected for a write, insert_id for an INSERT/REPLACE. + if (preg_match('/^\s*(insert|delete|update|replace)\s/i', $sql)) $a['affected'] = (int) $wpdb->rows_affected; + if (preg_match('/^\s*(insert|replace)\s/i', $sql)) $a['insert_id'] = (int) $wpdb->insert_id; + return serialize($a); + }; + // The statement time cap: the call's remaining milliseconds while it queries, then the + // session's own value again (read once per request). + $before = null; + $cap = static function (?int $ms) use (&$before): void { + global $wpdb; + $var = stripos((string) $wpdb->db_server_info(), 'mariadb') !== false ? 'max_statement_time' : 'max_execution_time'; + $before ??= (string) $wpdb->get_var("SELECT @@SESSION.$var"); + if ($ms === null) { + $value = is_numeric($before) ? $before : '0'; + } else { + $value = $var === 'max_statement_time' ? sprintf('%.3F', $ms / 1000) : (string) $ms; + } + $wpdb->query("SET SESSION $var = $value"); + }; + // Waits out the child's queries: the answer (a string), or null. + $pump = static function ($answer) use ($slug, $run, $cap) { + global $wpdb; + $capped = false; + while (is_array($answer)) { + if (!$capped) { + $capped = true; + $cap(max(1, (int) ($answer['ms'] ?? 1))); + } + $answer = \securecore_layer3_db_reply($slug, $run()); + } + if ($capped) $cap(null); + if (!is_string($answer) && \securecore_layer3_take_rollback($slug)) { + $wpdb->query('ROLLBACK'); + error_log("securecore: layer3 plugin:$slug rolled back the transaction its child left open"); + } + return $answer; + }; $register = null; - $register = static function (array $regs) use ($slug, $plain, &$register): void { + $register = static function (array $regs) use ($slug, $plain, $pump, &$register): void { foreach ($regs as $r) { if (!is_array($r) || !isset($r['kind'], $r['name'], $r['id'])) continue; $id = (int) $r['id']; $kind = (string) $r['kind']; - $forward = static function (...$args) use ($slug, $id, $kind, $plain, &$register) { - $answer = \securecore_layer3_call($slug, $id, serialize($plain($args))); + $forward = static function (...$args) use ($slug, $id, $kind, $plain, $pump, &$register) { + $answer = $pump(\securecore_layer3_call($slug, $id, serialize($plain($args)))); $a = is_string($answer) ? unserialize($answer, ['allowed_classes' => false]) : null; if (!is_array($a)) { return $kind === 'shortcode' ? '' : ($args[0] ?? null); @@ -42,11 +119,14 @@ } } }; + global $wpdb; $prefix = (string) ($GLOBALS['table_prefix'] ?? 'wp_'); // The helper boots from WordPress's own options only (#47): the list is the extension's. $names = \securecore_layer3_snapshot_names(ABSPATH, $prefix, (string) get_option('stylesheet')); $snapshot = serialize(array_intersect_key(wp_load_alloptions(), array_flip($names))); - $ready = \securecore_layer3_open($slug, $file, ABSPATH, $prefix, $snapshot); + // What the child's $wpdb reports without a connection; never a credential. + $facts = [(string) $wpdb->charset, (string) $wpdb->collate, (string) $wpdb->db_version(), (string) $wpdb->db_server_info()]; + $ready = $pump(\securecore_layer3_open($slug, $file, ABSPATH, $prefix, $snapshot, $facts)); $a = is_string($ready) ? unserialize($ready, ['allowed_classes' => false]) : null; if (!is_array($a)) return; if (is_string($a['out'] ?? null) && $a['out'] !== '') echo $a['out']; diff --git a/ext/securecore.schema.json b/ext/securecore.schema.json index 196ba29..d3042cd 100644 --- a/ext/securecore.schema.json +++ b/ext/securecore.schema.json @@ -277,6 +277,8 @@ "limits": { "$ref": "#/$defs/Limits", "default": { + "dbQueries": 1000, + "dbResultBytes": 4194304, "memoryMb": 256, "timeoutMs": 5000 } @@ -301,6 +303,21 @@ "Limits": { "type": "object", "properties": { + "dbQueries": { + "description": "Database queries one isolated component may send through the gateway in one request\n(its load and every call together). Past it each query fails, with a line.", + "type": "integer", + "format": "uint32", + "default": 1000, + "minimum": 1 + }, + "dbResultBytes": { + "description": "The largest answer to one gateway query that crosses back to the child (its rows,\nserialized), in bytes; at most 16777216 (16 MiB). A larger one fails, with a line.", + "type": "integer", + "format": "uint32", + "default": 4194304, + "maximum": 16777216, + "minimum": 1 + }, "memoryMb": { "description": "Memory limit for one isolated child.", "type": "integer", diff --git a/test/ext-layer3.sh b/test/ext-layer3.sh index c58565e..a8cad1e 100755 --- a/test/ext-layer3.sh +++ b/test/ext-layer3.sh @@ -10,10 +10,19 @@ # code is not in the worker, and a stray include of its files there is refused; Hello Dolly # renders on the dashboard from its child; the helper runs as the pool user with an empty # environment; a core option change re-boots the helper (the child sees the new value). +# 1b. the database gateway (#94), with the database probe sc-l3db (isolated): its own table by +# dbDelta through every $wpdb method, its own option persisting across requests into the +# site's database, the site's and core's options; layer 2's refusals (a core table, a +# statement, another plugin's table) with layer 2's lines; the gateway's own refusals (not +# UTF-8, the time cap); the caps (queries, result bytes, a statement cut at timeoutMs); no DB +# password in the child's environment or memory; the cost of a query. # 2. the limits: a write outside its folder, a read of wp-config.php / securecore.json and a # network connect are stopped (and the write logged); a too-long loop is killed at timeoutMs # and too much memory ends the child, each logged, while the page still renders. # 3. layer 2 off, layer 3 on: the kernel alone (gelada) still stops the write and the reads. +# With layer 2 off, the database probe's refused queries run through the gateway. +# 3b. the database probe unisolated (layer 3 off, layer 2 enforce): layer 2's lines are exactly +# the isolated plugin's; SQL that isn't UTF-8 is refused by layer 2 itself; no cap applies. # 4. the revert proof, layer 3 off (layer 2 log): the probe runs in the worker, every probe # case goes through, the loop holds the page for its 8 s and the memory probe kills the page. # Exits non-zero on any failure. Writes .tmp/ext-layer3/. Restores examples/wordpress.json and @@ -69,19 +78,58 @@ compose cp test/ext/layer3-plugin/sc-l3probe.php "php:$probe/sc-l3probe.php" >>" compose cp test/ext/layer3-plugin/inc.php "php:$probe/inc.php" >>"$dir/cp.log" 2>&1 compose cp test/ext/layer3-plugin/sc-l3-neighbour.php "php:$content/plugins/sc-l3-neighbour.php" >>"$dir/cp.log" 2>&1 in_php "rm -f $content/uploads/sc-l3-up.txt $content/uploads/sc-l3probe/w.txt $probe/w.txt; chown -R www-data:www-data $probe $content/uploads" -wp plugin activate sc-l3probe sc-l3-neighbour hello >/dev/null +# The database probe (#94), isolated: its own table, its own options, the caps and the secrets. +in_php "rm -rf $content/plugins/sc-l3db.php $content/uploads/sc-l3db && mkdir -p $content/uploads/sc-l3db && chown www-data:www-data $content/uploads/sc-l3db" +compose cp test/ext/layer3-plugin/sc-l3db.php "php:$content/plugins/sc-l3db.php" >>"$dir/cp.log" 2>&1 +# db_exec : as the site's database user, on the db container. +db_exec() { compose exec -T db mariadb -uwordpress -pwordpress-db-password -N wordpress -e "$1" 2>>"$dir/db.err"; } +db_exec "DROP TABLE IF EXISTS wp_sc_l3db_items; CREATE TABLE IF NOT EXISTS wp_sc_l3_neighbour_data (id int); DELETE FROM wp_options WHERE option_name LIKE 'sc\\_l3db\\_%' OR option_name LIKE '\\_transient\\_%sc\\_l3db%'" +wp option add sc_l3db_greeting hello-from-wp --autoload=no >/dev/null +wp plugin activate sc-l3probe sc-l3-neighbour hello sc-l3db >/dev/null post() { wp post create --post_status=publish --post_title="$1" --post_content="$2" --porcelain; } +# Pages, not posts: the front page lists posts, and these must run only when asked. +page() { wp post create --post_type=page --post_status=publish --post_title="$1" --post_content="$2" --porcelain; } p_main=$(post "L3 main" 'SC-L3-BODY [sc_l3 say="hello"]') p_loop=$(post "L3 loop" '[sc_l3_loop] SC-L3-AFTER-LOOP') p_mem=$(post "L3 memory" '[sc_l3_memory] SC-L3-AFTER-MEMORY') p_probe=$(post "L3 probe" '[sc_l3_probe] SC-L3-AFTER-PROBE') -[ -n "$p_main" ] && [ -n "$p_probe" ] || { echo "FAIL could not create the test posts (see $dir/wp.err)"; exit 1; } +d_rw=$(page "L3 db rw" '[sc_l3db_rw]') +d_l2=$(page "L3 db layer 2" '[sc_l3db_l2]') +d_l3=$(page "L3 db gateway" '[sc_l3db_l3]') +d_many=$(page "L3 db many" '[sc_l3db_many n=80] SC-L3DB-AFTER') +d_big=$(page "L3 db big" '[sc_l3db_big]') +d_sleep=$(page "L3 db sleep" '[sc_l3db_sleep] SC-L3DB-AFTER-SLEEP') +d_secrets=$(page "L3 db secrets" '[sc_l3db_secrets]') +d_time=$(page "L3 db time" '[sc_l3db_time n=50]') +d_txn=$(page "L3 db txn" '[sc_l3db_txn_dies] SC-L3DB-AFTER-TXN') +# scan_secrets : fetch the secrets page into $dir/ and, while the process serving its +# call waits, read that process's environment and memory from outside (as root with +# CAP_SYS_PTRACE in the php container: a layer 3 child isn't dumpable) into $dir/.scan, +# counting the DB password, a salt and (the control) "WordPress". +scan_secrets() { + f=$content/uploads/sc-l3db + in_php "rm -f $f/pid $f/scanned" + compose exec --privileged -T php sh -c "i=0; until [ -s $f/pid ] || [ \$i -ge 250 ]; do sleep 0.02; i=\$((i + 1)); done + [ -s $f/pid ] && php -- \"\$(cat $f/pid)\" wordpress-db-password sc-test-auth-key WordPress; touch $f/scanned" \ + < test/ext/layer3-memscan.php > "$dir/$1.scan" 2>&1 & + scanner=$! + get "$1" "/?page_id=$d_secrets" + wait "$scanner" +} +scan_val() { sed -n "s/^$2=//p" "$dir/$1.scan" | head -1; } +scan_is() { v=$(scan_val "$1" "$2"); if [ "$v" = "$3" ]; then pass "$4"; else fail "$4" "$2 is '$v', want '$3'"; fi; } +# cost : the median of 7 timings of the time page (µs per query, in the plugin). +cost() { + for i in 1 2 3 4 5 6 7; do get "$1-$i" "/?page_id=$d_time"; verdict "$1-$i" us_per_query; done | sort -n | sed -n 4p +} +[ -n "$p_main" ] && [ -n "$p_probe" ] && [ -n "$d_secrets" ] || { echo "FAIL could not create the test posts (see $dir/wp.err)"; exit 1; } cleanup() { - wp plugin deactivate sc-l3probe sc-l3-neighbour hello >/dev/null 2>&1 - for p in $p_main $p_loop $p_mem $p_probe; do wp post delete "$p" --force >/dev/null 2>&1; done + wp plugin deactivate sc-l3probe sc-l3-neighbour hello sc-l3db >/dev/null 2>&1 + for p in $p_main $p_loop $p_mem $p_probe $d_rw $d_l2 $d_l3 $d_many $d_big $d_sleep $d_secrets $d_time $d_txn; do wp post delete "$p" --force >/dev/null 2>&1; done wp option update blogname SecureCore >/dev/null 2>&1 - in_php "rm -rf $probe $content/plugins/sc-l3-neighbour.php $content/uploads/sc-l3probe $content/uploads/sc-l3-up.txt" + db_exec "DROP TABLE IF EXISTS wp_sc_l3db_items, wp_sc_l3_neighbour_data; DELETE FROM wp_options WHERE option_name LIKE 'sc\\_l3db\\_%' OR option_name LIKE '\\_transient\\_%sc\\_l3db%' OR option_name = 'sc_l3_neighbour_mark'" + in_php "rm -rf $probe $content/plugins/sc-l3-neighbour.php $content/plugins/sc-l3db.php $content/uploads/sc-l3db $content/uploads/sc-l3probe $content/uploads/sc-l3-up.txt" compose cp ext/examples/wordpress.json "php:$conf" >/dev/null 2>&1 compose restart php >/dev/null 2>&1 wait_200 || echo "FAIL php-fpm did not come back after cleanup" @@ -101,15 +149,16 @@ cfg() { # $1 name, $2 layer2 mode, $3 layer3 enabled }, "layer3": { "enabled": $3, - "isolate": ["sc-l3probe", "hello"], - "limits": { "timeoutMs": 1500, "memoryMb": 64 }, - "write": { "sc-l3probe": ["wp-content/uploads/sc-l3probe"] } + "isolate": ["sc-l3probe", "hello", "sc-l3db"], + "limits": { "timeoutMs": 1500, "memoryMb": 64, "dbQueries": 60, "dbResultBytes": 65536 }, + "write": { "sc-l3probe": ["wp-content/uploads/sc-l3probe"], "sc-l3db": ["wp-content/uploads/sc-l3db"] } } } JSON } cfg on enforce true cfg l2off off true +cfg unisolated enforce false cfg revert log false # use : install it and restart php-fpm (the helper starts with php-fpm), wait for 200. @@ -162,6 +211,91 @@ get renamed "/?p=$p_main" contains renamed 'blogname=SC-L3-Renamed' "on: after a core option change the child sees the new value (re-booted)" wp option update blogname SecureCore >/dev/null +# --- 1b. the database gateway (#94) ------------------------------------------------------------ +echo "# the database gateway (layer 3 on, layer 2 enforce)" +T=$(now); sleep 1 +get dbrw "/?page_id=$d_rw" +get dbrw2 "/?page_id=$d_rw" +is "$(status_of dbrw)" 200 "db: the page renders" +want dbrw isolated 1 "db: the plugin ran in its child" +for c in dbdelta insert get_row get_row_array update replace insert_id_next get_var get_col get_results get_col_info get_results_k \ + transaction delete bad_query_error last_error_clears counter_written transient; do + want dbrw "$c" OK "db" +done +want dbrw prefix wp_ "db" +want dbrw options_table wp_options "db" +want dbrw charset utf8mb4 "db: the worker's charset" +want dbrw greeting hello-from-wp "db: an option the site set, read in a call" +want dbrw loaded_greeting hello-from-wp "db: and while the plugin loads" +want dbrw blogname SecureCore "db: a core option" +c1=$(verdict dbrw counter); c2=$(verdict dbrw2 counter) +is "$c2" "$((c1 + 1))" "db: its own option persisted from one request (child) to the next" +is "$(db_exec "SELECT option_value FROM wp_options WHERE option_name = 'sc_l3db_counter'")" "$((c2 + 1))" "db: the option is in the site's database" +is "$(db_exec 'SELECT name FROM wp_sc_l3db_items')" replaced "db: the plugin's table, made by dbDelta, holds its row in the site's database" +hasnt "$T" "layer2 enforce plugin:sc-l3db" "db: layer 2 refused nothing of its own table and options" +hasnt "$T" "layer3 plugin:sc-l3db database refused" "db: the gateway refused nothing" +cost_isolated=$(cost tiso) +case "$cost_isolated" in [0-9]*) pass "db: a one-row query isolated costs $cost_isolated µs (median of 7 × 50)" ;; *) fail "db: the cost page" "'$cost_isolated'" ;; esac +# Layer 2's refusals, with the lines a non-isolated plugin gets (compared in section 3b). +T=$(now); sleep 1 +get dbl2 "/?page_id=$d_l2" +for c in core-table statement cross-plugin; do want dbl2 "$c" BLOCKED "db refusals"; done +want dbl2 core-table-error securecore "db refusals: last_error is layer 2's" +has "$T" "layer2 enforce plugin:sc-l3db mysqli_query core-table select wp_users" "db refusals: a core table without a grant, charged to the plugin" +has "$T" "layer2 enforce plugin:sc-l3db mysqli_query server-file select -" "db refusals: a refused statement" +has "$T" "layer2 enforce plugin:sc-l3db mysqli_query table select wp_sc_l3_neighbour_data" "db refusals: another plugin's table" +sleep 1 +phplog "$T" | grep -o 'securecore: layer2 [a-z]* plugin:sc-l3db .*' | sort -u > "$dir/l2-isolated.lines" +get dbl3 "/?page_id=$d_l3" +want dbl3 not-utf8 BLOCKED "db gateway" +want dbl3 not-utf8-direct none "db gateway: the child has no connection of its own" +want dbl3 time-cap BLOCKED "db gateway" +has "$T" "layer3 plugin:sc-l3db database refused: the query is not UTF-8" "db gateway: SQL that isn't UTF-8 is refused, logged" +has "$T" "layer3 plugin:sc-l3db database refused: it changes the statement time cap" "db gateway: a change to the time cap is refused, logged" +# The caps (dbQueries 60, dbResultBytes 65536, timeoutMs 1500). +T=$(now); sleep 1 +get many "/?page_id=$d_many" +ran=$(verdict many ran) +if [ -n "$ran" ] && [ "$ran" -lt 60 ] && [ "$ran" -ge 50 ]; then pass "caps: $ran of 80 queries ran under dbQueries 60"; else fail "caps: the query cap stopped the loop" "ran '$ran'"; fi +want many error dbQueries "caps" +contains many SC-L3DB-AFTER "caps: the page goes on" +sleep 1 +is "$(phplog "$T" | grep -c 'layer3 plugin:sc-l3db database refused: over the dbQueries cap of 60 per request')" 1 "caps: the query cap is logged once per request" +get big "/?page_id=$d_big" +want big big BLOCKED "caps" +want big error dbResultBytes "caps" +has "$T" "over the dbResultBytes cap of 65536" "caps: the result cap is logged" +get sleep "/?page_id=$d_sleep" +is "$(status_of sleep)" 200 "caps: the 5 s statement's page renders" +contains sleep SC-L3DB-AFTER-SLEEP "caps: the rest of the page is there" +lacks sleep "slept=" "caps: the call did not finish" +if [ "$(secs_of sleep)" -lt 4 ]; then pass "caps: the 5 s statement was cut at timeoutMs ($(cut -d' ' -f2 "$dir/sleep.meta") s)"; else fail "caps: the 5 s statement was cut at timeoutMs" "$(cut -d' ' -f2 "$dir/sleep.meta") s"; fi +has "$T" "layer3 plugin:sc-l3db call" "caps: the timed-out call is logged" +is "$(db_exec "SELECT COUNT(*) FROM information_schema.processlist WHERE info LIKE 'SELECT SLEEP(5)%'")" 0 "caps: the statement is not running on the server any more" +# A child killed with a transaction open: the worker rolls it back (its write never lands). +T=$(now); sleep 1 +mark=m$$x$(date +%s) +get txn "/?page_id=$d_txn&sc_l3_mark=$mark" +contains txn SC-L3DB-AFTER-TXN "transactions: the page goes on after the child is killed" +lacks txn "survived=" "transactions: the call did not finish" +has "$T" "layer3 plugin:sc-l3db rolled back the transaction its child left open" "transactions: the worker rolled back what the dead child left open" +is "$(db_exec "SELECT COUNT(*) FROM wp_sc_l3db_items WHERE name = 'orphan'")" 0 "transactions: the dead child's write is not in the database" +is "$(db_exec "SELECT option_value FROM wp_options WHERE option_name = 'sc_l3_neighbour_mark'")" "$mark" \ + "transactions: a later write in the same request (another plugin's) committed, not swallowed by the open transaction" +# The secrets: the child has no database password, in its environment or its memory. +scan_secrets secrets +want secrets isolated 1 "secrets" +want secrets scanned yes "secrets: the process was scanned while it ran the call" +want secrets getenv 0 "secrets: getenv() is empty" +want secrets env_superglobal 0 "secrets: \$_ENV is empty" +want secrets db_password_const undefined "secrets: no DB_PASSWORD constant" +scan_is secrets environ 0 "secrets: the child's environment (/proc//environ) is empty" +bytes=$(scan_val secrets bytes) +if [ -n "$bytes" ] && [ "$bytes" -gt 4000000 ]; then pass "secrets: read $bytes bytes of the child's memory"; else fail "secrets: read the child's memory" "'$bytes' bytes"; fi +scan_is secrets found_0 0 "secrets: the DB password is nowhere in the child's memory" +scan_is secrets found_1 0 "secrets: nor a salt" +if [ "$(scan_val secrets found_2)" -gt 0 ] 2>/dev/null; then pass "secrets: control: the scan finds WordPress's strings in it"; else fail "secrets: control: the scan finds WordPress's strings" "$(scan_val secrets found_2)"; fi + # --- 2. the limits --------------------------------------------------------------------------- echo "# limits (timeoutMs 1500, memoryMb 64, write: uploads/sc-l3probe)" T=$(now); sleep 1 @@ -198,6 +332,46 @@ contains probe2 'probe-not-in-worker' "l2off: the probe is still not in the work want probe2 write-own OK "l2off" for c in write-uploads write-plugin-dir read-config read-securecore-json net-db; do want probe2 "$c" BLOCKED "l2off"; done hasnt "$T" "write-outside" "l2off: no layer 2 file line (the kernel stopped it)" +# The gateway runs what layer 2 allows: with layer 2 off, the refused queries run. +get dbl2off "/?page_id=$d_l2" +want dbl2off isolated 1 "l2off: db" +for c in core-table statement cross-plugin; do want dbl2off "$c" OK "l2off: db (the refusals were layer 2's)"; done +get dbrw3 "/?page_id=$d_rw" +want dbrw3 get_results OK "l2off: db" + +# --- 3b. the same plugin unisolated: the same verdicts and lines ------------------------------ +echo "# the database probe unisolated (layer 3 off, layer 2 enforce)" +use unisolated +T=$(now); sleep 1 +get udbl2 "/?page_id=$d_l2" +want udbl2 isolated 0 "unisolated: the probe runs in the worker" +for c in core-table statement cross-plugin; do want udbl2 "$c" BLOCKED "unisolated"; done +sleep 1 +phplog "$T" | grep -o 'securecore: layer2 [a-z]* plugin:sc-l3db .*' | sort -u > "$dir/l2-unisolated.lines" +if [ -s "$dir/l2-isolated.lines" ] && cmp -s "$dir/l2-isolated.lines" "$dir/l2-unisolated.lines"; then + pass "layer 2's lines for the isolated plugin are the unisolated plugin's ($(wc -l < "$dir/l2-isolated.lines") distinct)" +else + fail "layer 2's lines for the isolated plugin are the unisolated plugin's" "$(diff "$dir/l2-isolated.lines" "$dir/l2-unisolated.lines" | tr '\n' ' ')" +fi +get udbrw "/?page_id=$d_rw" +for c in dbdelta insert get_row update replace get_var get_col get_results transaction delete counter_written; do want udbrw "$c" OK "unisolated: db"; done +cost_worker=$(cost tuni) +case "$cost_worker" in + [0-9]*) pass "db: the same query unisolated costs $cost_worker µs; the gateway adds $(echo "$cost_isolated - $cost_worker" | awk '{ printf "%.1f", $1 - $3 }') µs per query" ;; + *) fail "db: the cost page unisolated" "'$cost_worker'" ;; +esac +# Layer 2 reads SQL that is not UTF-8 too (it was skipped before #94): refused unisolated as well. +T=$(now); sleep 1 +get udbl3 "/?page_id=$d_l3" +want udbl3 not-utf8-direct BLOCKED "unisolated: SQL that isn't UTF-8, straight to mysqli" +has "$T" "layer2 enforce plugin:sc-l3db mysqli_query protected-column select wp_users" "unisolated: layer 2 refuses it on what it says" +# The caps' revert: in the worker nothing caps the plugin's queries. +get umany "/?page_id=$d_many" +want umany ran 80 "unisolated: no query cap" +get ubig "/?page_id=$d_big" +want ubig big OK "unisolated: no result cap" +get usleep "/?page_id=$d_sleep" +if [ "$(secs_of usleep)" -ge 5 ]; then pass "unisolated: the 5 s statement held the page ($(cut -d' ' -f2 "$dir/usleep.meta") s)"; else fail "unisolated: the 5 s statement held the page" "$(cut -d' ' -f2 "$dir/usleep.meta") s"; fi # --- 4. revert: layer 3 off -------------------------------------------------------------------- echo "# revert: layer 3 off (layer 2 log)" @@ -213,6 +387,13 @@ contains rloop "loop-finished" "revert: the loop ran to the end" if [ "$(secs_of rloop)" -ge 7 ]; then pass "revert: the loop held the page for $(cut -d' ' -f2 "$dir/rloop.meta") s"; else fail "revert: the loop held the page for 8 s" "$(cut -d' ' -f2 "$dir/rloop.meta") s"; fi get rmem "/?p=$p_mem" if [ "$(status_of rmem)" != 200 ] || ! grep -qF SC-L3-AFTER-MEMORY "$dir/rmem"; then pass "revert: too much memory kills the page ($(status_of rmem))"; else fail "revert: too much memory kills the page" "200 with content"; fi +# The secrets' control: the same scan of the worker serving the call does find the DB password. +scan_secrets rsecrets +want rsecrets isolated 0 "revert: the secrets probe runs in the worker" +n=$(scan_val rsecrets found_0) +if [ -n "$n" ] && [ "$n" -gt 0 ]; then pass "revert: the worker's memory holds the DB password ($n times), so the scan finds it"; else fail "revert: the worker's memory holds the DB password" "'$n'"; fi +get rdbl2 "/?page_id=$d_l2" +for c in core-table statement cross-plugin; do want rdbl2 "$c" OK "revert (layer 2 log): db"; done echo if [ "$fails" -eq 0 ]; then echo "all ext-layer3 checks passed"; else echo "$fails check(s) failed"; fi diff --git a/test/ext-microvm.sh b/test/ext-microvm.sh index 8cc0924..4dd01aa 100755 --- a/test/ext-microvm.sh +++ b/test/ext-microvm.sh @@ -121,6 +121,16 @@ done wp plugin install classic-editor wordpress-seo contact-form-7 --activate >/dev/null || die "plugin install" site=$(docker volume inspect -f '{{.Mountpoint}}' "${project}_wp") [ -f "$site/wp-load.php" ] || die "the site volume is not at $site" +# Layer 3's database gateway (#94) in the guest: test/ext-layer3.sh's database probe, isolated +# beside Contact Form 7, on a page of its own. +cp test/ext/layer3-plugin/sc-l3db.php "$site/wp-content/plugins/sc-l3db.php" +compose exec -T db mariadb -uwordpress -pwordpress-db-password wordpress \ + -e "DROP TABLE IF EXISTS wp_sc_l3db_items; DELETE FROM wp_options WHERE option_name LIKE 'sc\\_l3db\\_%' OR option_name LIKE '\\_transient\\_%sc\\_l3db%'" 2>/dev/null +wp option add sc_l3db_greeting hello-from-wp --autoload=no >/dev/null +wp plugin activate sc-l3db >/dev/null || die "activating the database probe" +for p in $(wp post list --post_type=page --title="L3 db rw" --field=ID); do wp post delete "$p" --force >/dev/null; done +d_rw=$(wp post create --post_type=page --post_status=publish --post_title="L3 db rw" --post_content='[sc_l3db_rw]' --porcelain) +[ -n "$d_rw" ] || die "the database probe's page" # MariaDB's root by unix_socket (as Debian's packages set it up): whoever connects to the socket # as uid 0 is the database's root, with no password. db_root "ALTER USER 'root'@'localhost' IDENTIFIED VIA unix_socket OR mysql_native_password USING PASSWORD('root-db-password')" > /dev/null \ @@ -202,6 +212,7 @@ conf() { sed -e "s|{ \"port\": 3306 }|{ \"port\": 3306, \"socket\": \"$mysqld/mysqld.sock\" }, { \"port\": 4444, \"socket\": \"$peer/peer.sock\" }|" \ -e "s/\"isolation\": \"auto\"/\"isolation\": \"$1\"/" \ -e 's#"hosts": \["api.wordpress.org", "downloads.wordpress.org"\]#"hosts": ["api.wordpress.org", "downloads.wordpress.org", "127.0.0.1"]#' \ + -e 's#"isolate": \["contact-form-7"\]#"isolate": ["contact-form-7", "sc-l3db"]#' \ ext/examples/wordpress.json } peer=$sockdir/peer @@ -501,11 +512,21 @@ if [ "$started" = 1 ]; then vget /sc-status.php > "$dir/status.first" is "$(sed -n 's/^loaded=//p;s/^layer2_mode=//p' "$dir/status.first" | tr '\n' /)" "1/enforce/" "the extension is loaded in the guest, layer 2 enforcing" is "$(sed -n 's/^layer1=//p;s/^layer2=//p;s/^layer3=//p' "$dir/status.first" | tr '\n' /)" "1/1/1/" "all three layers on in the guest's config" - # Contact Form 7's isolated child runs (its database calls are refused, as on the host); on a - # kernel without Landlock the child fails closed instead ("child refused to start"). - has "$dir/first.log" "securecore: layer3 plugin:contact-form-7 database refused (not yet available to an isolated plugin)" \ - "layer 3's isolated child runs in the guest (Landlock in the guest kernel)" + # Layer 3 in the guest: the database probe's isolated child creates its table and writes and + # reads it through the gateway (the guest's php-fpm runs each query on its own connection, to + # MariaDB through the host's socket); on a kernel without Landlock the child fails closed + # instead ("child refused to start"). + vget "/?page_id=$d_rw" > "$dir/db.first" + dbv() { sed -n "s/^$1=//p" "$dir/db.first" | head -1; } + is "$(dbv isolated)" 1 "layer 3's isolated child runs in the guest (Landlock in the guest kernel)" + for c in dbdelta insert get_row update replace get_var get_col get_results transaction delete counter_written; do + is "$(dbv "$c")" OK "the guest's isolated plugin: $c through the database gateway" + done + is "$(dbv greeting)" hello-from-wp "the guest's isolated plugin reads the site's option" + is "$(compose exec -T db mariadb -uwordpress -pwordpress-db-password -N wordpress -e 'SELECT name FROM wp_sc_l3db_items' 2>/dev/null)" replaced \ + "its table, made by dbDelta in the guest, holds its row in the host's MariaDB" lacks "$dir/first.log" "child refused to start" "no isolated child failed closed" + lacks "$dir/first.log" "layer3 plugin:sc-l3db database refused" "the gateway refused none of its queries" has "$dir/first.log" "securecore: layer2 enforce plugin:wordpress-seo" "layer 2 refuses in the guest as on the host (Yoast's tables)" is "$(grep -c '^NOTICE\|^\[' "$dir/first.log")" 0 "every line from the guest is marked 'guest: ' in the launcher's log" # A second start message, straight to the guest's control port: refused. diff --git a/test/ext/layer3-memscan.php b/test/ext/layer3-memscan.php new file mode 100644 index 0000000..291fd5d --- /dev/null +++ b/test/ext/layer3-memscan.php @@ -0,0 +1,55 @@ + bytes= found_= +// php -- ... < test/ext/layer3-memscan.php +$pid = (int) ($argv[1] ?? 0); +$needles = array_slice($argv, 2); +if ($pid <= 0 || $needles === []) { + fwrite(STDERR, "usage: php -- ...\n"); + exit(2); +} +$env = @file_get_contents("/proc/$pid/environ"); +echo 'environ=', is_string($env) ? count(array_filter(explode("\0", $env), 'strlen')) : 'unreadable', "\n"; +$found = array_fill(0, count($needles), 0); +$maps = @file("/proc/$pid/maps", FILE_IGNORE_NEW_LINES); +$mem = @fopen("/proc/$pid/mem", 'rb'); +$bytes = 0; +if (is_array($maps) && $mem) { + stream_set_read_buffer($mem, 0); + // Each chunk overlaps the last by the longest needle, so a needle across a boundary counts. + $overlap = max(array_map('strlen', $needles)) - 1; + foreach ($maps as $line) { + $f = preg_split('/\s+/', $line, 6); + $path = $f[5] ?? ''; + if (($f[1][0] ?? '') !== 'r' || ($path !== '' && $path[0] === '/') || in_array($path, ['[vvar]', '[vsyscall]', '[vdso]', '[vvar_vclock]'], true)) { + continue; + } + [$lo, $hi] = array_map('hexdec', explode('-', $f[0])); + $tail = ''; + // fread() on /proc returns what one read() gave (often 8 KiB): advance by what came. + for ($at = $lo; $at < $hi; $at += strlen($chunk)) { + if (fseek($mem, $at) !== 0) break; + $chunk = @fread($mem, min(1 << 20, $hi - $at)); + if (!is_string($chunk) || $chunk === '') break; + $bytes += strlen($chunk); + $hay = $tail . $chunk; + foreach ($needles as $i => $n) { + // Counted in the new bytes only (an occurrence wholly inside $tail was counted). + $off = 0; + while (($p = strpos($hay, $n, $off)) !== false) { + if ($p + strlen($n) > strlen($tail)) $found[$i]++; + $off = $p + 1; + } + } + $tail = $overlap > 0 ? substr($hay, -$overlap) : ''; + } + } +} +echo 'bytes=', $bytes, "\n"; +foreach ($found as $i => $c) { + echo "found_$i=$c\n"; +} diff --git a/test/ext/layer3-plugin/sc-l3-neighbour.php b/test/ext/layer3-plugin/sc-l3-neighbour.php index cafe4e3..64a5195 100644 --- a/test/ext/layer3-plugin/sc-l3-neighbour.php +++ b/test/ext/layer3-plugin/sc-l3-neighbour.php @@ -17,3 +17,12 @@ } return $content . '

neighbour pid=' . getmypid() . ' ' . $where . $inc . '

'; }, 20); + +// ?sc_l3_mark=: after the content, a write on the worker's connection (its own option, through +// core's update_option): it lands only if no transaction is left open on that connection (#94). +add_filter('the_content', static function ($content) { + if (isset($_GET['sc_l3_mark'])) { + update_option('sc_l3_neighbour_mark', sanitize_key($_GET['sc_l3_mark']), false); + } + return $content; +}, 30); diff --git a/test/ext/layer3-plugin/sc-l3db.php b/test/ext/layer3-plugin/sc-l3db.php new file mode 100644 index 0000000..ad1c5f0 --- /dev/null +++ b/test/ext/layer3-plugin/sc-l3db.php @@ -0,0 +1,190 @@ + $v) { + $lines .= "\n$k=$v"; + } + return "
\n" . esc_html($lines) . "\n
"; +} + +/** OK when the query ran (did not return false), BLOCKED when it failed. */ +function sc_l3db_ran($ret): string +{ + return $ret === false ? 'BLOCKED' : 'OK'; +} + +// Its own table and options, through every $wpdb method. +add_shortcode('sc_l3db_rw', static function () { + global $wpdb; + $r = []; + $t = $wpdb->prefix . 'sc_l3db_items'; + require_once ABSPATH . 'wp-admin/includes/upgrade.php'; + dbDelta("CREATE TABLE $t ( + id bigint(20) unsigned NOT NULL AUTO_INCREMENT, + name varchar(64) NOT NULL DEFAULT '', + n int(11) NOT NULL DEFAULT 0, + PRIMARY KEY (id), + KEY name (name) +) " . $wpdb->get_charset_collate() . ';'); + $r['dbdelta'] = $wpdb->get_var($wpdb->prepare('SHOW TABLES LIKE %s', $t)) === $t ? 'OK' : 'BLOCKED'; + $r['prefix'] = $wpdb->prefix; + $r['options_table'] = $wpdb->options; + $r['charset'] = $wpdb->charset; + $wpdb->query("DELETE FROM $t"); + // Quotes, a backslash, a newline and a NUL: escaped here, as mysqli would. + $tricky = "a'b\"c\\d\ne\0f"; + $ins = $wpdb->insert($t, ['name' => $tricky, 'n' => 1], ['%s', '%d']); + $id = (int) $wpdb->insert_id; + $r['insert'] = $ins === 1 && $id > 0 ? 'OK' : 'BLOCKED'; + $row = $wpdb->get_row($wpdb->prepare("SELECT id, name, n FROM $t WHERE id = %d", $id)); + $r['get_row'] = is_object($row) && (int) $row->id === $id && $row->name === $tricky ? 'OK' : 'BLOCKED'; + $r['get_row_array'] = ($wpdb->get_row($wpdb->prepare("SELECT n FROM $t WHERE id = %d", $id), ARRAY_A)['n'] ?? null) === '1' ? 'OK' : 'BLOCKED'; + $up = $wpdb->update($t, ['n' => 2], ['id' => $id], ['%d'], ['%d']); + $r['update'] = $up === 1 && $wpdb->rows_affected === 1 ? 'OK' : 'BLOCKED'; + $rep = $wpdb->replace($t, ['id' => $id, 'name' => 'replaced', 'n' => 3], ['%d', '%s', '%d']); + $r['replace'] = $rep === 2 && $wpdb->rows_affected === 2 ? 'OK' : 'BLOCKED'; + $wpdb->insert($t, ['name' => 'second', 'n' => 4]); + $r['insert_id_next'] = (int) $wpdb->insert_id > $id ? 'OK' : 'BLOCKED'; + $r['get_var'] = $wpdb->get_var("SELECT COUNT(*) FROM $t") === '2' ? 'OK' : 'BLOCKED'; + $r['get_col'] = implode(',', $wpdb->get_col("SELECT name FROM $t ORDER BY n")) === 'replaced,second' ? 'OK' : 'BLOCKED'; + $all = $wpdb->get_results("SELECT name, n FROM $t ORDER BY n DESC"); + $r['get_results'] = is_array($all) && count($all) === 2 && $all[0]->name === 'second' && $wpdb->num_rows === 2 ? 'OK' : 'BLOCKED'; + $r['get_col_info'] = implode(',', (array) $wpdb->get_col_info('name')) === 'name,n' ? 'OK' : 'BLOCKED'; + $keyed = $wpdb->get_results("SELECT name, n FROM $t", OBJECT_K); + $r['get_results_k'] = isset($keyed['replaced'], $keyed['second']) ? 'OK' : 'BLOCKED'; + // A transaction rolled back leaves the table as it was. + $wpdb->query('START TRANSACTION'); + $wpdb->insert($t, ['name' => 'rolled', 'n' => 9]); + $wpdb->query('ROLLBACK'); + $r['transaction'] = $wpdb->get_var("SELECT COUNT(*) FROM $t WHERE name = 'rolled'") === '0' ? 'OK' : 'BLOCKED'; + $del = $wpdb->delete($t, ['name' => 'second'], ['%s']); + $r['delete'] = $del === 1 ? 'OK' : 'BLOCKED'; + $r['bad_query_error'] = $wpdb->query("SELECT no_such_column FROM $t") === false && str_contains($wpdb->last_error, 'no_such_column') ? 'OK' : 'BLOCKED'; + $wpdb->query("SELECT 1 FROM $t"); + $r['last_error_clears'] = $wpdb->last_error === '' ? 'OK' : 'BLOCKED'; + // Options: its own (a counter kept across requests), the site's, core's from the snapshot. + $c = (int) get_option('sc_l3db_counter', 0); + update_option('sc_l3db_counter', $c + 1, false); + $r['counter'] = $c; + $r['counter_written'] = (int) get_option('sc_l3db_counter') === $c + 1 ? 'OK' : 'BLOCKED'; + $r['greeting'] = get_option('sc_l3db_greeting', 'none'); + $r['loaded_greeting'] = $GLOBALS['sc_l3db_loaded'] ?? 'unset'; + $r['blogname'] = get_option('blogname'); + $r['transient'] = set_transient('sc_l3db_t', 'tv', 60) && get_transient('sc_l3db_t') === 'tv' ? 'OK' : 'BLOCKED'; + return sc_l3db_out($r); +}); + +// Layer 2's refusals: a core table without a grant, a refused statement, another plugin's table. +add_shortcode('sc_l3db_l2', static function () { + global $wpdb; + return sc_l3db_out([ + 'core-table' => sc_l3db_ran($wpdb->get_var("SELECT user_login FROM {$wpdb->users} LIMIT 1") ?? false), + 'statement' => sc_l3db_ran($wpdb->query("SELECT LOAD_FILE('/etc/hostname')")), + 'cross-plugin' => sc_l3db_ran($wpdb->query("SELECT COUNT(*) FROM {$wpdb->prefix}sc_l3_neighbour_data")), + 'core-table-error' => str_contains($wpdb->get_var("SELECT user_login FROM {$wpdb->users} LIMIT 1") ?? $wpdb->last_error, 'securecore') ? 'securecore' : 'other', + ]); +}); + +// What only the gateway refuses: SQL that is not UTF-8 (layer 2 reads text), and a change to the +// statement time cap. +add_shortcode('sc_l3db_l3', static function () { + global $wpdb; + $sql = "SELECT user_login, user_pass FROM {$wpdb->users} /* \xff */ LIMIT 1"; + return sc_l3db_out([ + 'not-utf8' => sc_l3db_ran($wpdb->query($sql)), + // Unisolated, straight to mysqli (wpdb would strip the byte first): layer 2's own check. + 'not-utf8-direct' => $wpdb->dbh instanceof mysqli ? sc_l3db_ran(@mysqli_query($wpdb->dbh, $sql)) : 'none', + 'time-cap' => sc_l3db_ran($wpdb->query('SET SESSION max_statement_time = 0')), + ]); +}); + +// A transaction left open by a child that dies: it opens one, writes, then spins past timeoutMs. +add_shortcode('sc_l3db_txn_dies', static function () { + global $wpdb; + $wpdb->query('START TRANSACTION'); + $wpdb->insert($wpdb->prefix . 'sc_l3db_items', ['name' => 'orphan', 'n' => 7]); + $until = microtime(true) + 3; + while (microtime(true) < $until) { + } + return sc_l3db_out(['survived' => 'yes']); +}); + +// The query cap: n queries in a row, how many ran. +add_shortcode('sc_l3db_many', static function ($atts) { + global $wpdb; + $n = (int) (shortcode_atts(['n' => 80], $atts)['n']); + $ran = 0; + for ($i = 0; $i < $n; $i++) { + if ($wpdb->get_var('SELECT 1') === '1') $ran++; + } + return sc_l3db_out(['ran' => $ran, 'error' => str_contains($wpdb->last_error, 'dbQueries') ? 'dbQueries' : 'other']); +}); + +// The cost: microseconds per query, n queries of one row each, timed in the plugin. +add_shortcode('sc_l3db_time', static function ($atts) { + global $wpdb; + $n = (int) (shortcode_atts(['n' => 50], $atts)['n']); + $t = $wpdb->prefix . 'sc_l3db_items'; + $wpdb->get_var("SELECT name FROM $t LIMIT 1"); + $start = hrtime(true); + for ($i = 0; $i < $n; $i++) { + $wpdb->get_row($wpdb->prepare("SELECT id, name, n FROM $t WHERE n >= %d LIMIT 1", $i % 3)); + } + return sc_l3db_out(['us_per_query' => round((hrtime(true) - $start) / 1000 / $n, 1)]); +}); + +// The result cap: one answer of 300 KB. +add_shortcode('sc_l3db_big', static function () { + global $wpdb; + $v = $wpdb->get_var("SELECT REPEAT('x', 300000)"); + return sc_l3db_out(['big' => is_string($v) && strlen($v) === 300000 ? 'OK' : 'BLOCKED', 'error' => str_contains($wpdb->last_error, 'dbResultBytes') ? 'dbResultBytes' : 'other']); +}); + +// The time cap: a 5 s statement. +add_shortcode('sc_l3db_sleep', static function () { + global $wpdb; + $t = microtime(true); + $wpdb->query('SELECT SLEEP(5)'); + return sc_l3db_out(['slept' => round(microtime(true) - $t, 1)]); +}); + +// The secrets: what PHP shows of the environment, then a pause for the test to read this +// process's environment and memory from outside (test/ext/layer3-memscan.php): it writes its pid +// into its folder and waits (at most 1.3 s) for the test's "scanned" file. A layer 3 child is not +// dumpable (it changed uid), so it can't read its own /proc files; the test can. This file never +// names the DB password. +add_shortcode('sc_l3db_secrets', static function () { + $r = []; + $r['getenv'] = count(getenv()); + $r['env_superglobal'] = count($_ENV); + $r['db_password_const'] = defined('DB_PASSWORD') ? 'defined' : 'undefined'; + $dir = WP_CONTENT_DIR . '/uploads/sc-l3db'; + @unlink("$dir/scanned"); + $r['pid_written'] = @file_put_contents("$dir/pid", (string) getmypid()) !== false ? 'OK' : 'BLOCKED'; + $until = microtime(true) + 1.3; + while (!file_exists("$dir/scanned") && microtime(true) < $until) { + usleep(20000); + clearstatcache(); + } + $r['scanned'] = file_exists("$dir/scanned") ? 'yes' : 'no'; + return sc_l3db_out($r); +}); From e79572d62f1abde39d9a74510be9f786d5eb8b71 Mon Sep 17 00:00:00 2001 From: Sami Fouad Date: Wed, 11 Nov 2026 16:48:13 -0600 Subject: [PATCH 3/8] Layer 3 gateway: review fixes (#97) - Transactions: after every exchange that queried, however it ended, the stand-in reads the connection's state from the server (@@autocommit, @@in_transaction or innodb_trx) instead of parsing SQL, and rolls back and restores autocommit when the plugin left either changed. A normal return no longer leaves core's later writes in the plugin's transaction. - dbResultBytes is applied while the rows are copied, not after the whole answer is serialized; the worker's copies are freed on a refusal. - The child gets the server error's code and text with the database name, user and host taken out. - The statement time cap is re-set to the time left before a query when the last setting is over 50 ms old. MySQL's SELECT-only limit is #99. - A unit test that read_frame refuses an oversized length prefix before allocating. --- ext/README.md | 24 ++++-- ext/db/src/tests.rs | 6 ++ ext/layer3/src/gateway.rs | 45 ----------- ext/layer3/src/wire.rs | 15 ++++ ext/php/src/layer3.rs | 34 ++------ ext/php/src/layer3/stub.php | 93 ++++++++++++++++++---- test/ext-layer3.sh | 45 ++++++++++- test/ext/layer3-plugin/sc-l3-neighbour.php | 4 + test/ext/layer3-plugin/sc-l3db.php | 38 +++++++++ 9 files changed, 206 insertions(+), 98 deletions(-) diff --git a/ext/README.md b/ext/README.md index 07e9b15..eb5ce41 100644 --- a/ext/README.md +++ b/ext/README.md @@ -1091,13 +1091,23 @@ isolated plugin, where the same `get_option()` in the worker is core's. | cap | where | what happens | |---|---|---| | `limits.dbQueries` (default 1000) | the worker, per request and plugin (its load and its calls) | each query past it fails (`last_error`: `over the dbQueries cap`); one line per request | -| `limits.dbResultBytes` (default 4 MiB, at most 16 MiB) | the worker, per answer | a larger answer fails instead (the query ran); a line | -| `limits.timeoutMs` | the worker, per call, queries included | the call's remaining time caps each statement on the server (`SET SESSION max_statement_time` on MariaDB, `max_execution_time` on MySQL, SELECTs only there), set once per call that queries and restored to the session's own value after; past the deadline the child is killed as for any call. A plugin's query naming either variable is refused | - -**Transactions** are the plugin's statements on the worker's connection, as unisolated. If the -child dies (timeout, memory, a crash) after a `START TRANSACTION`/`BEGIN`/`SET autocommit = 0` -it didn't end, the worker rolls back (with a line), so core's later writes in the request don't -join it. +| `limits.dbResultBytes` (default 4 MiB, at most 16 MiB) | the worker, per answer | the stand-in counts the bytes as it copies the rows and stops past the cap: the answer fails (the query ran) and the worker's copies are freed, with a line. The one copy `$wpdb` buffered (mysqli's) is the worker's own `memory_limit`'s to bound; the serialized answer is checked again on the way out | +| `limits.timeoutMs` | the worker, per call, queries included | the time left before the call's deadline caps each statement on the server (`SET SESSION max_statement_time` on MariaDB; `max_execution_time` on MySQL, which covers SELECTs only, #99), re-set before a query whenever the last setting is over 50 ms old (so no statement runs more than 50 ms past the deadline there), and the session's own value restored after; past the deadline the child is killed as for any call. A plugin's query naming either variable is refused | + +**Transactions.** The plugin's statements run on the worker's connection, as unisolated. After +every exchange that queried (the load or a call, however it ended: an answer, a timeout, a +crash), the stand-in asks the server for the connection's state (`@@autocommit` and +`@@in_transaction` on MariaDB; on MySQL `information_schema.innodb_trx` for the connection), never +the SQL text, which a comment, `SET autocommit = FALSE` or the like would hide. If the plugin left +a transaction open or autocommit off that wasn't before, it rolls back, turns autocommit on again +and writes `layer3 plugin: left a transaction open on the worker's connection: rolled back`, +so core's later writes in the request never join it and no row lock outlives the call. (A +transaction the plugin opens and ends within one call is its own, as unisolated; XA transactions +never open, layer 2 refuses `XA`.) + +**Errors.** The child gets the server's error code and text with the connection's database name, +user and host taken out (`[1146] Table 'wp_x' doesn't exist`, not `'.wp_x'`; a user +and host read `''@''`); layer 2's and layer 3's own refusals come through as they are. **Cost.** One query is one round trip on the request socket plus PHP serialize/unserialize of the answer, on top of the query itself. `test/ext-layer3.sh` times a one-row `get_row(prepare(…))` 50 diff --git a/ext/db/src/tests.rs b/ext/db/src/tests.rs index 99dd235..31e3da9 100644 --- a/ext/db/src/tests.rs +++ b/ext/db/src/tests.rs @@ -373,6 +373,12 @@ fn core_is_unrestricted_but_for_the_allowlist() { ] { assert_eq!(core(sql), Decision::Allow, "{sql}"); } + // ...and reads the connection's transaction state after each exchange, as core. + assert_eq!(core("SELECT @@autocommit, @@in_transaction"), Decision::Allow); + // XA transactions (which a plain ROLLBACK can't end) never open for a component. + for sql in ["XA START 'x'", "XA BEGIN 'x'"] { + assert!(matches!(verdict(&plain(), sql), Decision::Refuse(_)), "{sql}"); + } } // --- the WordPress shapes the parser must take (a component in place of core) ------------------- diff --git a/ext/layer3/src/gateway.rs b/ext/layer3/src/gateway.rs index 19f62d7..6e855a1 100644 --- a/ext/layer3/src/gateway.rs +++ b/ext/layer3/src/gateway.rs @@ -85,37 +85,6 @@ fn touches_time_cap(sql: &str) -> bool { lower.contains("max_statement_time") || lower.contains("max_execution_time") } -/// What a statement does to a transaction on the connection: `Some(true)` opens one, -/// `Some(false)` ends it, `None` neither. The worker rolls back a transaction a plugin left open -/// when its child dies, so core's later writes in the request don't join it. -pub fn transaction_effect(sql: &str) -> Option { - let words: Vec = sql - .trim_start() - .split(|c: char| c.is_whitespace() || c == ';') - .filter(|w| !w.is_empty()) - .take(3) - .map(str::to_ascii_uppercase) - .collect(); - let w = |i: usize| words.get(i).map_or("", String::as_str); - match (w(0), w(1)) { - ("BEGIN", _) | ("START", "TRANSACTION") => Some(true), - // ROLLBACK TO [SAVEPOINT] x keeps the transaction open. - ("ROLLBACK", "TO") => None, - ("COMMIT" | "ROLLBACK", _) => Some(false), - ("SET", _) => { - let compact: String = sql.chars().filter(|c| !c.is_whitespace()).collect::().to_ascii_uppercase(); - if compact.contains("AUTOCOMMIT=0") || compact.contains("AUTOCOMMIT=OFF") { - Some(true) - } else if compact.contains("AUTOCOMMIT=1") || compact.contains("AUTOCOMMIT=ON") { - Some(false) - } else { - None - } - } - _ => None, - } -} - /// The worker's answer for a query it refused or could not run: what a failed `$wpdb` query /// leaves (`false`, and `message` as `last_error`), PHP-serialized as the worker's PHP answers. pub fn failed_answer(message: &str) -> Vec { @@ -161,20 +130,6 @@ mod tests { assert!(admit(b"SET SESSION sql_mode = 'TRADITIONAL'", 0, 10).is_ok()); } - #[test] - fn transactions_are_tracked_by_their_control_statements() { - assert_eq!(transaction_effect("START TRANSACTION"), Some(true)); - assert_eq!(transaction_effect(" begin;"), Some(true)); - assert_eq!(transaction_effect("start transaction read only"), Some(true)); - assert_eq!(transaction_effect("COMMIT"), Some(false)); - assert_eq!(transaction_effect("rollback"), Some(false)); - assert_eq!(transaction_effect("ROLLBACK TO SAVEPOINT a"), None); - assert_eq!(transaction_effect("SET autocommit = 0"), Some(true)); - assert_eq!(transaction_effect("SET autocommit=1"), Some(false)); - assert_eq!(transaction_effect("SELECT 'BEGIN'"), None); - assert_eq!(transaction_effect("INSERT INTO t VALUES (1)"), None); - } - #[test] fn a_failed_answer_is_valid_php_serialize() { let a = failed_answer("securecore: nope"); diff --git a/ext/layer3/src/wire.rs b/ext/layer3/src/wire.rs index 78b261e..137ee71 100644 --- a/ext/layer3/src/wire.rs +++ b/ext/layer3/src/wire.rs @@ -384,6 +384,21 @@ mod tests { assert_eq!(decode_kill(b"R"), None); } + /// A hostile peer's length prefix is checked before anything is allocated or read: a header + /// claiming more than MAX_FRAME is refused at once, with its body never sent. + #[test] + fn an_oversized_frame_is_refused_from_its_header() { + let (a, b) = socketpair(libc::SOCK_STREAM).unwrap(); + let len = u32::try_from(MAX_FRAME + 1).unwrap(); + send_all(a.as_fd(), &len.to_be_bytes(), None).unwrap(); + let e = read_frame(b.as_fd(), Some(Instant::now() + Duration::from_secs(5))).unwrap_err(); + assert!(e.to_string().contains("over the"), "{e}"); + send_all(a.as_fd(), &u32::MAX.to_be_bytes(), None).unwrap(); + assert!(read_frame(b.as_fd(), Some(Instant::now() + Duration::from_secs(5))).is_err()); + // And a writer can't send one. + assert!(write_frame(a.as_fd(), &vec![0; MAX_FRAME + 1], None).is_err()); + } + #[test] fn frames_cross_a_socketpair_and_time_out() { let (a, b) = socketpair(libc::SOCK_STREAM).unwrap(); diff --git a/ext/php/src/layer3.rs b/ext/php/src/layer3.rs index 47bceca..a507e0b 100644 --- a/ext/php/src/layer3.rs +++ b/ext/php/src/layer3.rs @@ -210,8 +210,6 @@ struct Channel { queries: u32, /// The query cap's line was written (once per request). cap_logged: bool, - /// The plugin's last transaction-control statement left a transaction open. - transaction: bool, } #[derive(Debug, Clone, Copy)] @@ -232,8 +230,6 @@ static CHANNELS: Mutex>>> = Mutex::new(No /// Slugs whose query the stand-in is running now (a stack: a query's `query` filter may call /// another isolated plugin that queries in turn). Read by [`gateway_component`]. static GATEWAY: Mutex> = Mutex::new(Vec::new()); -/// Slugs whose child died with a transaction open: the stand-in rolls it back. -static ROLLBACK: Mutex> = Mutex::new(Vec::new()); /// The name the stand-in for `slug` is compiled under (its frames' filename). fn stub_name(slug: &str) -> String { @@ -256,7 +252,6 @@ pub(crate) fn gateway_component(file: &str) -> Option { pub(crate) fn request_shutdown() { lock(&CHANNELS).take(); lock(&GATEWAY).clear(); - lock(&ROLLBACK).clear(); } /// Whether the `zend_compile_file` wrapper is needed for layer 3: a worker with plugins to @@ -386,7 +381,6 @@ pub fn securecore_layer3_open(slug: String, file: String, abspath: String, prefi busy: false, queries: 0, cap_logged: false, - transaction: false, }; let slot = channels.entry(slug.clone()).or_insert(Some(channel)); pump(&slug, slot, Some(first)) @@ -459,7 +453,8 @@ enum Failure { } /// Ends `slot`'s channel after a failed exchange, with the line for it; a child past its deadline -/// is killed. A transaction the plugin left open is noted for the stand-in to roll back. +/// is killed. (A transaction the plugin left open is the stand-in's to roll back: it reads the +/// connection's state after every exchange.) fn fail(slug: &str, slot: &mut Option, failure: Failure) { let Some(ch) = slot.take() else { return }; let phase = ch.waiting.map_or(Phase::Load, |w| w.phase); @@ -481,9 +476,6 @@ fn fail(slug: &str, slot: &mut Option, failure: Failure) { // SAFETY: the control socket, open for the process's life. let _ = wire::send_packet(unsafe { BorrowedFd::borrow_raw(control) }, &wire::encode_kill(pid), None); } - if ch.transaction { - lock(&ROLLBACK).push(slug.to_owned()); - } } /// Reads the child's frames until the answer the channel waits for. A query frame on the way is @@ -492,7 +484,8 @@ fn fail(slug: &str, slot: &mut Option, failure: Failure) { /// string), a query (an array), or null when the channel failed. fn pump(slug: &str, slot: &mut Option, mut first: Option>) -> Zval { let mut out = Zval::new(); - let cap = crate::with_live(|l| l.config.layer3.limits.db_queries.get()).unwrap_or(1000); + let (cap, result_cap) = + crate::with_live(|l| (l.config.layer3.limits.db_queries.get(), l.config.layer3.limits.db_result_bytes.get())).unwrap_or((1000, 4 << 20)); loop { let Some(ch) = slot.as_mut() else { return out }; let Some(w) = ch.waiting else { return out }; @@ -515,14 +508,13 @@ fn pump(slug: &str, slot: &mut Option, mut first: Option>) -> Z match gateway::admit(sql, ch.queries, cap) { Ok(text) => { ch.queries += 1; - if let Some(open) = gateway::transaction_effect(text) { - ch.transaction = open; - } ch.pending = Some(text.to_owned()); let left = w.deadline.saturating_duration_since(Instant::now()); let mut q = ZendHashTable::new(); let ms = i64::try_from(left.as_millis()).unwrap_or(i64::MAX).max(1); - if q.insert("ms", ms).is_err() { + // The stand-in stops copying rows past dbResultBytes (the reply is checked + // again on the way out). + if q.insert("ms", ms).is_err() || q.insert("bytes", i64::from(result_cap)).is_err() { return out; } out.set_hashtable(q); @@ -621,17 +613,6 @@ pub fn securecore_layer3_db_reply(slug: String, answer: &Zval) -> Zval { pump(&slug, slot, None) } -/// Whether the plugin's child died with a transaction open on the worker's connection (once): -/// the stand-in then rolls it back, so core's later writes don't join it. -#[php_function] -#[php(name = "securecore_layer3_take_rollback")] -pub fn securecore_layer3_take_rollback(slug: String) -> bool { - let mut owed = lock(&ROLLBACK); - let before = owed.len(); - owed.retain(|s| *s != slug); - owed.len() != before -} - // --- the helper: supervisor --------------------------------------------------------------------- struct Zygote { @@ -1006,7 +987,6 @@ pub(crate) fn register(module: ModuleBuilder) -> ModuleBuilder { .function(wrap_function!(securecore_layer3_call)) .function(wrap_function!(securecore_layer3_db_begin)) .function(wrap_function!(securecore_layer3_db_reply)) - .function(wrap_function!(securecore_layer3_take_rollback)) .function(wrap_function!(securecore_layer3_db)) .function(wrap_function!(securecore_layer3_helper)) .function(wrap_function!(securecore_layer3_zygote)) diff --git a/ext/php/src/layer3/stub.php b/ext/php/src/layer3/stub.php index 846ce8f..40ddbad 100644 --- a/ext/php/src/layer3/stub.php +++ b/ext/php/src/layer3/stub.php @@ -22,8 +22,32 @@ } return $v; }; + $mariadb = static function (): bool { + global $wpdb; + return stripos((string) $wpdb->db_server_info(), 'mariadb') !== false; + }; + // The server's error for the child: its code and its text, with the connection's database + // name, user and host taken out (`'db.table'`, `'user'@'host'`), so the child learns no part + // of the credentials from an error. + $clean = static function (string $error, int $errno): string { + global $wpdb; + if ($error === '' || str_starts_with($error, 'securecore: ')) return $error; + $error = (string) preg_replace("/'[^']*'@'[^']*'/", "''@''", $error); + $facts = [ + defined('DB_NAME') ? (string) DB_NAME : (string) ($wpdb->dbname ?? ''), + defined('DB_USER') ? (string) DB_USER : (string) ($wpdb->dbuser ?? ''), + defined('DB_HOST') ? (string) DB_HOST : (string) ($wpdb->dbhost ?? ''), + ]; + foreach (array_unique(array_filter($facts, static fn ($f) => strlen($f) >= 2)) as $f) { + $error = str_ireplace([$f . '.', $f], ['', ''], $error); + } + return $errno > 0 ? "[$errno] $error" : $error; + }; // One query from the child, on this worker's connection: what $wpdb saw, PHP-serialized. - $run = static function () use ($slug): string { + // The rows are copied only up to $limit bytes (layer3.limits.dbResultBytes): past it the + // answer is a refusal, and the worker's copies are freed, so a result far over the cap never + // costs more than the one $wpdb buffered. + $run = static function (int $limit) use ($slug, $clean): string { global $wpdb; $sql = \securecore_layer3_db_begin($slug); if (!is_string($sql)) { @@ -31,26 +55,38 @@ } $ret = false; $cols = []; + $errno = 0; try { $ret = $wpdb->query($sql); $error = (string) $wpdb->last_error; + $dbh = $wpdb->dbh ?? null; + if ($error !== '' && $dbh instanceof \mysqli) $errno = (int) mysqli_errno($dbh); if ($ret !== false && ($wpdb->result ?? null) instanceof \mysqli_result) { $cols = (array) $wpdb->get_col_info('name'); } } catch (\Throwable $e) { $ret = false; - $error = 'securecore: ' . get_class($e) . ': ' . $e->getMessage(); + $error = 'securecore: ' . get_class($e); } $keys = []; $rows = []; + $bytes = 0; foreach (is_array($wpdb->last_result) ? $wpdb->last_result : [] as $row) { $v = is_object($row) ? get_object_vars($row) : []; if ($keys === []) $keys = array_keys($v); + // What serialize() writes for each value, near enough (its text and ~16 bytes). + foreach ($v as $x) $bytes += strlen((string) $x) + 16; + if ($limit > 0 && $bytes > $limit) { + unset($rows, $v); + $wpdb->flush(); + error_log("securecore: layer3 plugin:$slug database answer refused: over the dbResultBytes cap of $limit"); + return serialize(['ret' => false, 'error' => "securecore: layer3 refused this answer (over the dbResultBytes cap of $limit)"]); + } $rows[] = array_values($v); } $a = [ 'ret' => is_int($ret) || is_bool($ret) ? $ret : false, - 'error' => $error, + 'error' => $clean($error, $errno), 'keys' => $keys, 'rows' => $rows, 'cols' => $cols, @@ -59,14 +95,18 @@ // As wpdb::query() sets them: rows_affected for a write, insert_id for an INSERT/REPLACE. if (preg_match('/^\s*(insert|delete|update|replace)\s/i', $sql)) $a['affected'] = (int) $wpdb->rows_affected; if (preg_match('/^\s*(insert|replace)\s/i', $sql)) $a['insert_id'] = (int) $wpdb->insert_id; + if ($rows !== []) $wpdb->flush(); return serialize($a); }; - // The statement time cap: the call's remaining milliseconds while it queries, then the - // session's own value again (read once per request). + // The statement time cap: the time left before the call's deadline, set before a query + // whenever the last setting is more than 50 ms old (so a statement never runs more than 50 ms + // past the deadline on the server), then the session's own value again (read once per + // request). MariaDB's max_statement_time covers every statement; MySQL's max_execution_time + // only SELECTs (addiscorp/securecore#99). $before = null; - $cap = static function (?int $ms) use (&$before): void { + $cap = static function (?int $ms) use (&$before, $mariadb): void { global $wpdb; - $var = stripos((string) $wpdb->db_server_info(), 'mariadb') !== false ? 'max_statement_time' : 'max_execution_time'; + $var = $mariadb() ? 'max_statement_time' : 'max_execution_time'; $before ??= (string) $wpdb->get_var("SELECT @@SESSION.$var"); if ($ms === null) { $value = is_numeric($before) ? $before : '0'; @@ -75,21 +115,40 @@ } $wpdb->query("SET SESSION $var = $value"); }; - // Waits out the child's queries: the answer (a string), or null. - $pump = static function ($answer) use ($slug, $run, $cap) { + // The connection's transaction state, as the server reports it (never read from the SQL + // text): [autocommit on, a transaction open]. + $state = static function () use ($mariadb): array { global $wpdb; - $capped = false; + $open = $mariadb() + ? '@@in_transaction' + : '(SELECT COUNT(*) FROM information_schema.innodb_trx WHERE trx_mysql_thread_id = CONNECTION_ID())'; + $r = $wpdb->get_row("SELECT @@autocommit, $open", ARRAY_N); + return [(int) ($r[0] ?? 1) === 1, (int) ($r[1] ?? 0) > 0]; + }; + // Waits out the child's queries: the answer (a string), or null. However the exchange ended + // (an answer, a timeout, a crash), a transaction the plugin opened, or autocommit it turned + // off, is rolled back and restored, so core's later writes in the request never join it. + $pump = static function ($answer) use ($slug, $run, $cap, $state) { + global $wpdb; + $was = null; + $set_at = null; while (is_array($answer)) { - if (!$capped) { - $capped = true; + $was ??= $state(); + if ($set_at === null || microtime(true) - $set_at > 0.05) { $cap(max(1, (int) ($answer['ms'] ?? 1))); + $set_at = microtime(true); } - $answer = \securecore_layer3_db_reply($slug, $run()); + $answer = \securecore_layer3_db_reply($slug, $run((int) ($answer['bytes'] ?? 0))); } - if ($capped) $cap(null); - if (!is_string($answer) && \securecore_layer3_take_rollback($slug)) { - $wpdb->query('ROLLBACK'); - error_log("securecore: layer3 plugin:$slug rolled back the transaction its child left open"); + if ($was !== null) { + $cap(null); + [$autocommit, $open] = $state(); + if (($open && !$was[1]) || (!$autocommit && $was[0])) { + $wpdb->query('ROLLBACK'); + if (!$autocommit && $was[0]) $wpdb->query('SET autocommit = 1'); + error_log("securecore: layer3 plugin:$slug left " . ($open ? 'a transaction open' : 'autocommit off') + . " on the worker's connection: rolled back" . (!$autocommit && $was[0] ? ', autocommit restored' : '')); + } } return $answer; }; diff --git a/test/ext-layer3.sh b/test/ext-layer3.sh index a8cad1e..19d5767 100755 --- a/test/ext-layer3.sh +++ b/test/ext-layer3.sh @@ -102,6 +102,11 @@ d_sleep=$(page "L3 db sleep" '[sc_l3db_sleep] SC-L3DB-AFTER-SLEEP') d_secrets=$(page "L3 db secrets" '[sc_l3db_secrets]') d_time=$(page "L3 db time" '[sc_l3db_time n=50]') d_txn=$(page "L3 db txn" '[sc_l3db_txn_dies] SC-L3DB-AFTER-TXN') +d_txo=$(page "L3 db txn open" '[sc_l3db_txn_open] SC-L3DB-AFTER-TXO') +d_aco=$(page "L3 db autocommit off" '[sc_l3db_autocommit_off] SC-L3DB-AFTER-ACO') +d_huge=$(page "L3 db huge" '[sc_l3db_huge] SC-L3DB-AFTER-HUGE') +d_err=$(page "L3 db error" '[sc_l3db_err]') +d_sleeps=$(page "L3 db sleeps" '[sc_l3db_sleeps] SC-L3DB-AFTER-SLEEPS') # scan_secrets : fetch the secrets page into $dir/ and, while the process serving its # call waits, read that process's environment and memory from outside (as root with # CAP_SYS_PTRACE in the php container: a layer 3 child isn't dumpable) into $dir/.scan, @@ -126,7 +131,7 @@ cost() { cleanup() { wp plugin deactivate sc-l3probe sc-l3-neighbour hello sc-l3db >/dev/null 2>&1 - for p in $p_main $p_loop $p_mem $p_probe $d_rw $d_l2 $d_l3 $d_many $d_big $d_sleep $d_secrets $d_time $d_txn; do wp post delete "$p" --force >/dev/null 2>&1; done + for p in $p_main $p_loop $p_mem $p_probe $d_rw $d_l2 $d_l3 $d_many $d_big $d_sleep $d_secrets $d_time $d_txn $d_txo $d_aco $d_huge $d_err $d_sleeps; do wp post delete "$p" --force >/dev/null 2>&1; done wp option update blogname SecureCore >/dev/null 2>&1 db_exec "DROP TABLE IF EXISTS wp_sc_l3db_items, wp_sc_l3_neighbour_data; DELETE FROM wp_options WHERE option_name LIKE 'sc\\_l3db\\_%' OR option_name LIKE '\\_transient\\_%sc\\_l3db%' OR option_name = 'sc_l3_neighbour_mark'" in_php "rm -rf $probe $content/plugins/sc-l3-neighbour.php $content/plugins/sc-l3db.php $content/uploads/sc-l3db $content/uploads/sc-l3probe $content/uploads/sc-l3-up.txt" @@ -278,10 +283,46 @@ mark=m$$x$(date +%s) get txn "/?page_id=$d_txn&sc_l3_mark=$mark" contains txn SC-L3DB-AFTER-TXN "transactions: the page goes on after the child is killed" lacks txn "survived=" "transactions: the call did not finish" -has "$T" "layer3 plugin:sc-l3db rolled back the transaction its child left open" "transactions: the worker rolled back what the dead child left open" +has "$T" "layer3 plugin:sc-l3db left a transaction open on the worker's connection: rolled back" "transactions: the worker rolled back what the dead child left open" is "$(db_exec "SELECT COUNT(*) FROM wp_sc_l3db_items WHERE name = 'orphan'")" 0 "transactions: the dead child's write is not in the database" is "$(db_exec "SELECT option_value FROM wp_options WHERE option_name = 'sc_l3_neighbour_mark'")" "$mark" \ "transactions: a later write in the same request (another plugin's) committed, not swallowed by the open transaction" +# Left open on a normal return: a START TRANSACTION behind a comment, and autocommit turned off. +# Read from the server's state, not the SQL: rolled back, autocommit restored, core's write commits. +for c in txo:open1 aco:ac1; do + n=${c%%:*} row=${c#*:} + eval "pg=\$d_$n" + T=$(now); sleep 1 + mark=m$$${n}$(date +%s) + get "$n" "/?page_id=$pg&sc_l3_mark=$mark" + want "$n" returned yes "transactions ($n): the call returned normally" + has "$T" "layer3 plugin:sc-l3db left a transaction open on the worker's connection: rolled back" "transactions ($n): the worker rolled back what the plugin left open" + is "$(db_exec "SELECT COUNT(*) FROM wp_sc_l3db_items WHERE name = '$row'")" 0 "transactions ($n): the plugin's uncommitted row is not in the database" + is "$(db_exec "SELECT option_value FROM wp_options WHERE option_name = 'sc_l3_neighbour_mark'")" "$mark" \ + "transactions ($n): core's later write in the same request committed" +done +has "$T" "rolled back, autocommit restored" "transactions (aco): autocommit is restored" +# dbResultBytes against a result far over it (40 MiB, cap 64 KiB): refused while the rows are +# copied, and the worker stays within memory_limit. +get huge "/?page_id=$d_huge&sc_l3_peak=1" +is "$(status_of huge)" 200 "result cap: the 40 MiB result's page renders (no memory_limit fatal)" +contains huge SC-L3DB-AFTER-HUGE "result cap: the rest of the page is there" +want huge huge BLOCKED "result cap" +want huge error dbResultBytes "result cap" +peak=$(verdict huge peak) +if [ -n "$peak" ] && [ "$peak" -lt 120000000 ]; then pass "result cap: the worker's peak memory stayed at $peak bytes"; else fail "result cap: the worker's peak memory stayed under 120 MB" "'$peak'"; fi +# The error the child sees names neither the database nor its user. +get err "/?page_id=$d_err" +e=$(verdict err err) +case "$e" in *sc_l3db_nope*) pass "errors: the child sees the error ($e)" ;; *) fail "errors: the child sees the error" "'$e'" ;; esac +case "$e" in "[1146] "*) pass "errors: with its code" ;; *) fail "errors: with its code" "'$e'" ;; esac +case "$e" in *wordpress*) fail "errors: the database name is taken out" "'$e'" ;; *) pass "errors: the database name is taken out" ;; esac +# The statement cap is the time left: two 1 s statements in a 1.5 s call end at ~1.5 s, not 2. +get sleeps "/?page_id=$d_sleeps" +contains sleeps SC-L3DB-AFTER-SLEEPS "time cap: the page goes on" +lacks sleeps "slept=" "time cap: the call did not finish" +s=$(cut -d' ' -f2 "$dir/sleeps.meta") +if awk "BEGIN { exit !($s < 1.85) }"; then pass "time cap: the second statement was cut at the time left ($s s)"; else fail "time cap: the second statement was cut at the time left" "$s s"; fi # The secrets: the child has no database password, in its environment or its memory. scan_secrets secrets want secrets isolated 1 "secrets" diff --git a/test/ext/layer3-plugin/sc-l3-neighbour.php b/test/ext/layer3-plugin/sc-l3-neighbour.php index 64a5195..933bdfd 100644 --- a/test/ext/layer3-plugin/sc-l3-neighbour.php +++ b/test/ext/layer3-plugin/sc-l3-neighbour.php @@ -24,5 +24,9 @@ if (isset($_GET['sc_l3_mark'])) { update_option('sc_l3_neighbour_mark', sanitize_key($_GET['sc_l3_mark']), false); } + // ?sc_l3_peak=1: the worker's peak memory so far (the result cap's test). + if (isset($_GET['sc_l3_peak'])) { + $content .= "\n
\npeak=" . memory_get_peak_usage() . "\n
"; + } return $content; }, 30); diff --git a/test/ext/layer3-plugin/sc-l3db.php b/test/ext/layer3-plugin/sc-l3db.php index ad1c5f0..c7e0ce1 100644 --- a/test/ext/layer3-plugin/sc-l3db.php +++ b/test/ext/layer3-plugin/sc-l3db.php @@ -128,6 +128,44 @@ function sc_l3db_ran($ret): string return sc_l3db_out(['survived' => 'yes']); }); +// Transactions left open on a normal return: one opened behind a comment, one by turning +// autocommit off. Each writes a row and returns without committing. +add_shortcode('sc_l3db_txn_open', static function () { + global $wpdb; + $wpdb->query('/* hidden */ START TRANSACTION'); + $wpdb->insert($wpdb->prefix . 'sc_l3db_items', ['name' => 'open1', 'n' => 8]); + return sc_l3db_out(['returned' => 'yes']); +}); +add_shortcode('sc_l3db_autocommit_off', static function () { + global $wpdb; + $wpdb->query('SET autocommit = FALSE'); + $wpdb->insert($wpdb->prefix . 'sc_l3db_items', ['name' => 'ac1', 'n' => 8]); + return sc_l3db_out(['returned' => 'yes']); +}); + +// The result cap against a result far larger than it: 40 rows of 1 MiB. +add_shortcode('sc_l3db_huge', static function () { + global $wpdb; + $v = $wpdb->get_col("SELECT REPEAT('x', 1048576) FROM information_schema.COLLATIONS LIMIT 40"); + return sc_l3db_out(['huge' => count($v) === 40 ? 'OK' : 'BLOCKED', 'error' => str_contains($wpdb->last_error, 'dbResultBytes') ? 'dbResultBytes' : 'other']); +}); + +// A server error naming the database: what the child sees of it. +add_shortcode('sc_l3db_err', static function () { + global $wpdb; + $wpdb->query("SELECT * FROM {$wpdb->prefix}sc_l3db_nope"); + return sc_l3db_out(['err' => $wpdb->last_error]); +}); + +// Two 1 s statements in one call: the second must be capped at the time left, not at the time +// there was before the first. +add_shortcode('sc_l3db_sleeps', static function () { + global $wpdb; + $wpdb->query('SELECT SLEEP(1)'); + $wpdb->query('SELECT SLEEP(1)'); + return sc_l3db_out(['slept' => 'twice']); +}); + // The query cap: n queries in a row, how many ran. add_shortcode('sc_l3db_many', static function ($atts) { global $wpdb; From 22e43c063b6b2d0cbad822dc5f0389ddef7cd46f Mon Sep 17 00:00:00 2001 From: Sami Fouad Date: Wed, 11 Nov 2026 16:55:48 -0600 Subject: [PATCH 4/8] Layer 3 gateway: stream gateway results unbuffered so dbResultBytes bounds the worker's memory; strip only the database name from errors --- ext/README.md | 8 ++- ext/php/src/layer3/stub.php | 99 ++++++++++++++++++++++--------------- test/ext-layer3.sh | 2 +- 3 files changed, 65 insertions(+), 44 deletions(-) diff --git a/ext/README.md b/ext/README.md index eb5ce41..0a638b4 100644 --- a/ext/README.md +++ b/ext/README.md @@ -1072,7 +1072,11 @@ the php-fpm worker, which runs each query on its own connection, under layer 2: 2. **In the worker,** the extension reads the frame and decides first what only layer 3 decides (`gateway::admit`): the request's query cap, the text's size (16 MiB) and UTF-8, and no change to the statement time cap. Then the stand-in takes the text (`securecore_layer3_db_begin`) and - runs it with `$wpdb->query()` on the real connection. While it does, the stand-in's own frames + runs it as `$wpdb->query()` does: the worker's `query` filters, then `mysqli_query()` on + `$wpdb`'s own connection (the route and text layer 2 sees for the plugin's `$wpdb->query()` + unisolated), but unbuffered, so the rows are read one at a time (below; `$wpdb`'s own state, + `num_queries` and `SAVEQUERIES` don't see these queries, and a `db.php` drop-in's own routing + is not used). While it does, the stand-in's own frames (compiled under the name `securecore-layer3:`, which only the extension uses) are `plugin:`, so **layer 2's own caller rule** (`attribution::db_caller`, then `db::check`) charges the query to the plugin exactly as it charges that plugin's `$wpdb->query()` in the @@ -1091,7 +1095,7 @@ isolated plugin, where the same `get_option()` in the worker is core's. | cap | where | what happens | |---|---|---| | `limits.dbQueries` (default 1000) | the worker, per request and plugin (its load and its calls) | each query past it fails (`last_error`: `over the dbQueries cap`); one line per request | -| `limits.dbResultBytes` (default 4 MiB, at most 16 MiB) | the worker, per answer | the stand-in counts the bytes as it copies the rows and stops past the cap: the answer fails (the query ran) and the worker's copies are freed, with a line. The one copy `$wpdb` buffered (mysqli's) is the worker's own `memory_limit`'s to bound; the serialized answer is checked again on the way out | +| `limits.dbResultBytes` (default 4 MiB, at most 16 MiB) | the worker, per answer | the result is read unbuffered (`MYSQLI_USE_RESULT`) and the stand-in counts the bytes as it copies each row: past the cap the rest is drained unread and the answer fails (the query ran), with a line, so a result far over the cap never sits in the worker's memory. The serialized answer is checked again on the way out | | `limits.timeoutMs` | the worker, per call, queries included | the time left before the call's deadline caps each statement on the server (`SET SESSION max_statement_time` on MariaDB; `max_execution_time` on MySQL, which covers SELECTs only, #99), re-set before a query whenever the last setting is over 50 ms old (so no statement runs more than 50 ms past the deadline there), and the session's own value restored after; past the deadline the child is killed as for any call. A plugin's query naming either variable is refused | **Transactions.** The plugin's statements run on the worker's connection, as unisolated. After diff --git a/ext/php/src/layer3/stub.php b/ext/php/src/layer3/stub.php index 40ddbad..94d43af 100644 --- a/ext/php/src/layer3/stub.php +++ b/ext/php/src/layer3/stub.php @@ -33,69 +33,86 @@ global $wpdb; if ($error === '' || str_starts_with($error, 'securecore: ')) return $error; $error = (string) preg_replace("/'[^']*'@'[^']*'/", "''@''", $error); - $facts = [ - defined('DB_NAME') ? (string) DB_NAME : (string) ($wpdb->dbname ?? ''), - defined('DB_USER') ? (string) DB_USER : (string) ($wpdb->dbuser ?? ''), - defined('DB_HOST') ? (string) DB_HOST : (string) ($wpdb->dbhost ?? ''), - ]; - foreach (array_unique(array_filter($facts, static fn ($f) => strlen($f) >= 2)) as $f) { - $error = str_ireplace([$f . '.', $f], ['', ''], $error); + $db = defined('DB_NAME') ? (string) DB_NAME : (string) ($wpdb->dbname ?? ''); + if ($db !== '') { + // Where the server names it: 'db.table', `db`.table, 'db'. + $error = str_replace(["'$db.", "`$db`.", "'$db'", "`$db`"], ["'", '', "''", '``'], $error); } return $errno > 0 ? "[$errno] $error" : $error; }; - // One query from the child, on this worker's connection: what $wpdb saw, PHP-serialized. - // The rows are copied only up to $limit bytes (layer3.limits.dbResultBytes): past it the - // answer is a refusal, and the worker's copies are freed, so a result far over the cap never - // costs more than the one $wpdb buffered. + // One query from the child, on this worker's connection, PHP-serialized as wpdb::query() + // would leave it. It runs as $wpdb's own query does (the worker's `query` filters, then + // mysqli_query() on $wpdb's connection, which layer 2 checks with the same route and text), + // but unbuffered (MYSQLI_USE_RESULT): rows are read one at a time and copied only up to + // $limit bytes (layer3.limits.dbResultBytes). Past it the rest is drained unread and the + // answer is a refusal, so a result far over the cap never sits in the worker's memory. $run = static function (int $limit) use ($slug, $clean): string { global $wpdb; $sql = \securecore_layer3_db_begin($slug); if (!is_string($sql)) { return serialize(['ret' => false, 'error' => 'securecore: layer3 had no query to run']); } + $sql = (string) apply_filters('query', $sql); + $dbh = $wpdb->dbh ?? null; + if (!$dbh instanceof \mysqli && $wpdb->check_connection(false)) $dbh = $wpdb->dbh ?? null; + if ($sql === '' || !$dbh instanceof \mysqli) { + return serialize(['ret' => false, 'error' => $sql === '' ? '' : 'securecore: layer3: the worker has no database connection']); + } $ret = false; - $cols = []; + $error = ''; $errno = 0; + $keys = []; + $cols = []; + $rows = []; + $a = []; try { - $ret = $wpdb->query($sql); - $error = (string) $wpdb->last_error; - $dbh = $wpdb->dbh ?? null; - if ($error !== '' && $dbh instanceof \mysqli) $errno = (int) mysqli_errno($dbh); - if ($ret !== false && ($wpdb->result ?? null) instanceof \mysqli_result) { - $cols = (array) $wpdb->get_col_info('name'); - } + $res = mysqli_query($dbh, $sql, MYSQLI_USE_RESULT); } catch (\Throwable $e) { - $ret = false; - $error = 'securecore: ' . get_class($e); + $res = false; } - $keys = []; - $rows = []; - $bytes = 0; - foreach (is_array($wpdb->last_result) ? $wpdb->last_result : [] as $row) { - $v = is_object($row) ? get_object_vars($row) : []; - if ($keys === []) $keys = array_keys($v); - // What serialize() writes for each value, near enough (its text and ~16 bytes). - foreach ($v as $x) $bytes += strlen((string) $x) + 16; - if ($limit > 0 && $bytes > $limit) { - unset($rows, $v); - $wpdb->flush(); - error_log("securecore: layer3 plugin:$slug database answer refused: over the dbResultBytes cap of $limit"); - return serialize(['ret' => false, 'error' => "securecore: layer3 refused this answer (over the dbResultBytes cap of $limit)"]); + if ($res === false) { + $errno = (int) mysqli_errno($dbh); + $error = (string) mysqli_error($dbh); + } elseif ($res instanceof \mysqli_result) { + $cols = array_map(static fn ($f) => $f->name, mysqli_fetch_fields($res) ?: []); + $bytes = 0; + while (($row = mysqli_fetch_assoc($res)) !== null && $row !== false) { + if ($keys === []) $keys = array_keys($row); + // What serialize() writes for each value, near enough (its text and ~16 bytes). + foreach ($row as $x) $bytes += strlen((string) $x) + 16; + if ($limit > 0 && $bytes > $limit) { + $rows = []; + mysqli_free_result($res); + error_log("securecore: layer3 plugin:$slug database answer refused: over the dbResultBytes cap of $limit"); + return serialize(['ret' => false, 'error' => "securecore: layer3 refused this answer (over the dbResultBytes cap of $limit)"]); + } + $rows[] = array_values($row); + } + if (mysqli_errno($dbh)) { + $errno = (int) mysqli_errno($dbh); + $error = (string) mysqli_error($dbh); } - $rows[] = array_values($v); + mysqli_free_result($res); + $ret = $error === '' ? count($rows) : false; + } elseif (preg_match('/^\s*(create|alter|truncate|drop)\s/i', $sql)) { + $ret = true; + } elseif (preg_match('/^\s*(insert|delete|update|replace)\s/i', $sql)) { + // As wpdb::query() sets them: rows_affected for a write, insert_id for an INSERT/REPLACE. + $ret = (int) mysqli_affected_rows($dbh); + $a['affected'] = $ret; + if (preg_match('/^\s*(insert|replace)\s/i', $sql)) $a['insert_id'] = (int) mysqli_insert_id($dbh); + } else { + $ret = 0; } - $a = [ - 'ret' => is_int($ret) || is_bool($ret) ? $ret : false, + if ($error !== '' && preg_match('/^\s*(insert|replace)\s/i', $sql)) $a['insert_id'] = 0; + $a += [ + 'ret' => $ret, 'error' => $clean($error, $errno), 'keys' => $keys, 'rows' => $rows, 'cols' => $cols, 'num_rows' => count($rows), ]; - // As wpdb::query() sets them: rows_affected for a write, insert_id for an INSERT/REPLACE. - if (preg_match('/^\s*(insert|delete|update|replace)\s/i', $sql)) $a['affected'] = (int) $wpdb->rows_affected; - if (preg_match('/^\s*(insert|replace)\s/i', $sql)) $a['insert_id'] = (int) $wpdb->insert_id; - if ($rows !== []) $wpdb->flush(); return serialize($a); }; // The statement time cap: the time left before the call's deadline, set before a query diff --git a/test/ext-layer3.sh b/test/ext-layer3.sh index 19d5767..0f7be51 100755 --- a/test/ext-layer3.sh +++ b/test/ext-layer3.sh @@ -310,7 +310,7 @@ contains huge SC-L3DB-AFTER-HUGE "result cap: the rest of the page is there" want huge huge BLOCKED "result cap" want huge error dbResultBytes "result cap" peak=$(verdict huge peak) -if [ -n "$peak" ] && [ "$peak" -lt 120000000 ]; then pass "result cap: the worker's peak memory stayed at $peak bytes"; else fail "result cap: the worker's peak memory stayed under 120 MB" "'$peak'"; fi +if [ -n "$peak" ] && [ "$peak" -lt 40000000 ]; then pass "result cap: the worker's peak memory stayed at $peak bytes, under the 40 MiB result"; else fail "result cap: the worker's peak memory stayed under the 40 MiB result" "'$peak'"; fi # The error the child sees names neither the database nor its user. get err "/?page_id=$d_err" e=$(verdict err err) From ff3da44a623947bb1b97d551b0fee8fe826bfdab Mon Sep 17 00:00:00 2001 From: Sami Fouad Date: Wed, 11 Nov 2026 17:46:52 -0600 Subject: [PATCH 5/8] Layer 3 gateway: bookkeeping straight to mysqli, SQL-shaping hooks never mirrored, a session SET allowlist, ROLLBACK AND NO CHAIN, the MySQL rollback, a per-row result cost, cleanup in finally --- ext/README.md | 50 +++++- ext/db/src/tests.rs | 5 + ext/layer3/src/gateway.rs | 198 +++++++++++++++++++-- ext/php/src/layer3.rs | 53 +++++- ext/php/src/layer3/stub.php | 143 +++++++++++---- test/ext-layer3.sh | 53 +++++- test/ext/layer3-plugin/sc-l3-neighbour.php | 27 +++ test/ext/layer3-plugin/sc-l3db.php | 65 ++++++- 8 files changed, 525 insertions(+), 69 deletions(-) diff --git a/ext/README.md b/ext/README.md index 0a638b4..40c09f8 100644 --- a/ext/README.md +++ b/ext/README.md @@ -1095,19 +1095,51 @@ isolated plugin, where the same `get_option()` in the worker is core's. | cap | where | what happens | |---|---|---| | `limits.dbQueries` (default 1000) | the worker, per request and plugin (its load and its calls) | each query past it fails (`last_error`: `over the dbQueries cap`); one line per request | -| `limits.dbResultBytes` (default 4 MiB, at most 16 MiB) | the worker, per answer | the result is read unbuffered (`MYSQLI_USE_RESULT`) and the stand-in counts the bytes as it copies each row: past the cap the rest is drained unread and the answer fails (the query ran), with a line, so a result far over the cap never sits in the worker's memory. The serialized answer is checked again on the way out | +| `limits.dbResultBytes` (default 4 MiB, at most 16 MiB) | the worker, per answer | the result is read unbuffered (`MYSQLI_USE_RESULT`) and the stand-in counts the bytes as it copies each row (each row ~200 bytes, each value ~16 more plus its text: what it costs the worker, not only its serialized size, so a million one-byte rows count 200 MB): past the cap the rest is drained unread and the answer fails (the query ran), with a line, so a result far over the cap never sits in the worker's memory. The serialized answer is checked again on the way out | | `limits.timeoutMs` | the worker, per call, queries included | the time left before the call's deadline caps each statement on the server (`SET SESSION max_statement_time` on MariaDB; `max_execution_time` on MySQL, which covers SELECTs only, #99), re-set before a query whenever the last setting is over 50 ms old (so no statement runs more than 50 ms past the deadline there), and the session's own value restored after; past the deadline the child is killed as for any call. A plugin's query naming either variable is refused | **Transactions.** The plugin's statements run on the worker's connection, as unisolated. After every exchange that queried (the load or a call, however it ended: an answer, a timeout, a -crash), the stand-in asks the server for the connection's state (`@@autocommit` and -`@@in_transaction` on MariaDB; on MySQL `information_schema.innodb_trx` for the connection), never -the SQL text, which a comment, `SET autocommit = FALSE` or the like would hide. If the plugin left -a transaction open or autocommit off that wasn't before, it rolls back, turns autocommit on again -and writes `layer3 plugin: left a transaction open on the worker's connection: rolled back`, -so core's later writes in the request never join it and no row lock outlives the call. (A -transaction the plugin opens and ends within one call is its own, as unisolated; XA transactions -never open, layer 2 refuses `XA`.) +crash, an exception in the worker, in a `finally`), the stand-in puts the connection back: +- **MariaDB:** it asks the server for `@@autocommit` and `@@in_transaction` (never the SQL text, + which a comment would hide). If the plugin left a transaction open, or the state can't be read, + it runs `ROLLBACK AND NO CHAIN`, turns autocommit on, reads the state again, and replaces the + connection if it still isn't clean; with the line `layer3 plugin: left a transaction open + on the worker's connection: rolled back`. +- **MySQL:** an open transaction can't be seen without the PROCESS privilege (and InnoDB starts + one lazily), so whenever autocommit was on before the plugin's first query, the same + `ROLLBACK AND NO CHAIN` and `SET autocommit = 1` run unconditionally (a no-op when nothing is + open). A transaction core itself held open around a call into an isolated plugin would be + rolled back too; WordPress core holds none while rendering. + +So core's later writes in the request never join what the plugin left, and no row lock outlives +the call. A transaction the plugin opens and ends within one call is its own, as unisolated. XA +transactions never open (layer 2 refuses `XA`). + +**Bookkeeping never goes through filters.** The cap, the state reads, the rollback and `SET +autocommit` are `mysqli_query()` calls on the exchange's own connection, never `$wpdb->query()`, +whose `query` filters a plugin could use to answer "nothing open". + +**SQL-shaping hooks are never mirrored.** An isolated plugin's callbacks on hooks whose value is +SQL the worker runs (`query`, `dbdelta_*`, `incompatible_sql_modes`, WP_Query's `posts_*` and +`post_limits*`, the query classes' `*_clauses`, `*_pre_query`, `found_*_query`, `comment_feed_*`, +`getarchives_*`, `get_terms_fields`/`_orderby`, `get_meta_sql`, `get_date_sql`, `split_the_query`; +`gateway::shapes_sql`) are not mirrored into the worker, with a line (`layer3 plugin: hook + not mirrored: it shapes SQL the worker runs`). Mirrored, they would rewrite core's queries +from the child. They still run on the plugin's own queries in the child. + +**Session state is an allowlist.** Every `SET` persists into core's later queries on the +worker's connection, so the gateway refuses all but `SET NAMES` / `SET CHARACTER SET` to the +connection's own charset (with a collation of it), `SET [SESSION] group_concat_max_len = ` and +`SET [SESSION] sql_big_selects = 1|ON`. Refused: every other `SET` (`autocommit`, +`completion_type`, `sql_select_limit`, `transaction_read_only`, `sql_mode`, `time_zone`, `SET +TRANSACTION`, `SET @@…`, several assignments at once), also behind a comment, plus `COMMIT` / +`ROLLBACK … AND CHAIN` or `RELEASE`. Each refusal logs `layer3 plugin: database refused: it +changes the worker connection's session state`. + +**db.php drop-ins.** The gateway uses `$wpdb->dbh`, the drop-in's current connection for HyperDB +or LudicrousDB, for the plugin's query and for all bookkeeping after it. A drop-in whose `dbh` +isn't a mysqli (SQLite) gets a refusal: `the worker's $wpdb has no mysqli connection`. **Errors.** The child gets the server's error code and text with the connection's database name, user and host taken out (`[1146] Table 'wp_x' doesn't exist`, not `'.wp_x'`; a user diff --git a/ext/db/src/tests.rs b/ext/db/src/tests.rs index 31e3da9..0c3c4cc 100644 --- a/ext/db/src/tests.rs +++ b/ext/db/src/tests.rs @@ -375,6 +375,11 @@ fn core_is_unrestricted_but_for_the_allowlist() { } // ...and reads the connection's transaction state after each exchange, as core. assert_eq!(core("SELECT @@autocommit, @@in_transaction"), Decision::Allow); + // (`NO RELEASE` isn't in the parser's grammar; RELEASE would come only from a completion_type + // the gateway never lets a plugin set.) + for sql in ["ROLLBACK AND NO CHAIN", "SET autocommit = 1"] { + assert_eq!(core(sql), Decision::Allow, "{sql}"); + } // XA transactions (which a plain ROLLBACK can't end) never open for a component. for sql in ["XA START 'x'", "XA BEGIN 'x'"] { assert!(matches!(verdict(&plain(), sql), Decision::Refuse(_)), "{sql}"); diff --git a/ext/layer3/src/gateway.rs b/ext/layer3/src/gateway.rs index 6e855a1..7463cde 100644 --- a/ext/layer3/src/gateway.rs +++ b/ext/layer3/src/gateway.rs @@ -45,6 +45,10 @@ pub enum Refusal { /// The text names `max_statement_time` / `max_execution_time`: the cap the worker sets on the /// connection for the call's time is not the plugin's to change. TimeCap, + /// A `SET` outside [`allowed_set`], or a COMMIT / ROLLBACK that chains a new transaction or + /// releases the connection: session state persists into core's later queries on the worker's + /// connection. + SessionState, } impl Refusal { @@ -55,6 +59,7 @@ impl Refusal { Refusal::TooLong(n) => format!("the query is {n} bytes, over {MAX_SQL}"), Refusal::NotUtf8 => "the query is not UTF-8".to_owned(), Refusal::TimeCap => "it changes the statement time cap".to_owned(), + Refusal::SessionState => "it changes the worker connection's session state".to_owned(), } } @@ -65,8 +70,9 @@ impl Refusal { } /// Whether the worker runs `sql`, the plugin's `sent`-th query of the request (counting from 0), -/// under a cap of `cap` queries. `Ok` is the text, for PHP. -pub fn admit(sql: &[u8], sent: u32, cap: u32) -> Result<&str, Refusal> { +/// under a cap of `cap` queries, on a connection whose charset is `charset`. `Ok` is the text, +/// for PHP. +pub fn admit<'a>(sql: &'a [u8], sent: u32, cap: u32, charset: &str) -> Result<&'a str, Refusal> { if sent >= cap { return Err(Refusal::Queries(cap)); } @@ -77,9 +83,103 @@ pub fn admit(sql: &[u8], sent: u32, cap: u32) -> Result<&str, Refusal> { if touches_time_cap(text) { return Err(Refusal::TimeCap); } + let words = normalized(text); + match words.first().map(String::as_str) { + Some("SET") if !allowed_set(&words, charset) => return Err(Refusal::SessionState), + Some("COMMIT" | "ROLLBACK") if chains_or_releases(&words) => return Err(Refusal::SessionState), + _ => {} + } Ok(text) } +/// The SQL's words outside comments, uppercased, with `=` and `,` as words of their own and +/// quoted strings kept whole (`'utf8mb4'`). Comments (`/* */`, `-- `, `#`) are dropped, so one +/// can't hide the statement's first word. +pub fn normalized(sql: &str) -> Vec { + let b = sql.as_bytes(); + let mut words = Vec::new(); + let mut cur = String::new(); + let flush = |cur: &mut String, words: &mut Vec| { + if !cur.is_empty() { + words.push(std::mem::take(cur).to_ascii_uppercase()); + } + }; + let mut i = 0; + while i < b.len() { + let c = b[i]; + if c == b'/' && b.get(i + 1) == Some(&b'*') { + flush(&mut cur, &mut words); + i = sql[i + 2..].find("*/").map_or(b.len(), |e| i + 2 + e + 2); + } else if (c == b'-' && b.get(i + 1) == Some(&b'-') && b.get(i + 2).is_none_or(|n| n.is_ascii_whitespace())) || c == b'#' { + flush(&mut cur, &mut words); + i = sql[i..].find('\n').map_or(b.len(), |e| i + e + 1); + } else if c == b'\'' || c == b'"' || c == b'`' { + let end = (i + 1..b.len()).find(|&j| b[j] == c && b[j - 1] != b'\\').map_or(b.len(), |j| j + 1); + cur.push_str(&sql[i..end]); + i = end; + } else if c.is_ascii_whitespace() || c == b';' { + flush(&mut cur, &mut words); + i += 1; + } else if c == b'=' || c == b',' { + flush(&mut cur, &mut words); + words.push((c as char).to_string()); + i += 1; + } else { + let ch = sql[i..].chars().next().unwrap_or(' '); + cur.push(ch); + i += ch.len_utf8(); + } + } + flush(&mut cur, &mut words); + words +} + +/// The session SETs an isolated plugin may run (each persists into core's later queries, so the +/// list holds only what WordPress plugins actually send and what can't change how core's SQL +/// reads or runs): `SET NAMES` / `SET CHARACTER SET` to the connection's own charset (with a +/// collation of it), `SET [SESSION] group_concat_max_len = ` and `SET [SESSION] sql_big_selects +/// = 1|ON`. Every other SET is refused: `SET [SESSION] ` (sql_mode, autocommit, +/// completion_type, sql_select_limit, transaction_read_only, time_zone, …), `SET @@…`, `SET +/// TRANSACTION`, `SET STATEMENT`, several assignments at once. +pub fn allowed_set(words: &[String], charset: &str) -> bool { + let unq = |w: &str| w.trim_matches(['\'', '"', '`']).to_ascii_uppercase(); + let cs = charset.to_ascii_uppercase(); + let own = |w: &str| !cs.is_empty() && unq(w) == cs; + let own_collation = |w: &str| unq(w).strip_prefix(&format!("{cs}_")).is_some_and(|r| !r.is_empty()); + let w: Vec<&str> = words.iter().map(String::as_str).collect(); + let digits = |s: &str| !s.is_empty() && s.len() <= 12 && s.bytes().all(|b| b.is_ascii_digit()); + match w.as_slice() { + ["SET", "NAMES", c] | ["SET", "CHARACTER", "SET", c] => own(c), + ["SET", "NAMES", c, "COLLATE", k] => own(c) && own_collation(k), + ["SET", "SESSION", "GROUP_CONCAT_MAX_LEN", "=", n] | ["SET", "GROUP_CONCAT_MAX_LEN", "=", n] => digits(n), + ["SET", "SESSION", "SQL_BIG_SELECTS", "=", v] | ["SET", "SQL_BIG_SELECTS", "=", v] => matches!(*v, "1" | "ON"), + _ => false, + } +} + +/// Hooks whose value is SQL (or a clause of it, or rows standing in for a query's) that core then +/// runs or trusts, in the worker: never mirrored for an isolated plugin, or its callback would +/// rewrite core's queries (and the stand-in's own) from its child. It still runs on its own +/// queries in the child. `query`; `dbdelta_*`; `incompatible_sql_modes`; WP_Query's `posts_*` +/// and `post_limits*`; the query classes' `*_clauses`, `*_pre_query` and `found_*_query`; +/// `comment_feed_*`, `getarchives_*`, `get_terms_fields` / `_orderby`, `get_meta_sql`, +/// `get_date_sql`, `split_the_query`. +pub fn shapes_sql(hook: &str) -> bool { + let h = hook.to_ascii_lowercase(); + let h = h.as_str(); + matches!(h, "query" | "incompatible_sql_modes" | "get_meta_sql" | "get_date_sql" | "split_the_query" | "get_terms_fields" | "get_terms_orderby") + || ["dbdelta_", "posts_", "post_limits", "comment_feed_", "getarchives_"].iter().any(|p| h.starts_with(p)) + || h.ends_with("_clauses") + || h.ends_with("_pre_query") + || (h.starts_with("found_") && h.ends_with("_query")) +} + +/// A COMMIT / ROLLBACK `AND CHAIN` (opens a new transaction) or `RELEASE` (drops the worker's +/// connection). `AND NO CHAIN`, `NO RELEASE` and `ROLLBACK TO SAVEPOINT` are fine. +fn chains_or_releases(words: &[String]) -> bool { + words.iter().enumerate().any(|(i, w)| (w == "CHAIN" || w == "RELEASE") && (i == 0 || words[i - 1] != "NO")) +} + fn touches_time_cap(sql: &str) -> bool { let lower = sql.to_ascii_lowercase(); lower.contains("max_statement_time") || lower.contains("max_execution_time") @@ -106,17 +206,19 @@ mod tests { #[test] fn admit_counts_sizes_and_reads_utf8() { - assert_eq!(admit(b"SELECT 1", 0, 1), Ok("SELECT 1")); - assert_eq!(admit(b"SELECT 1", 1, 1), Err(Refusal::Queries(1))); - assert_eq!(admit(b"SELECT 1", 7, 3), Err(Refusal::Queries(3))); + assert_eq!(admit(b"SELECT 1", 0, 1, U), Ok("SELECT 1")); + assert_eq!(admit(b"SELECT 1", 1, 1, U), Err(Refusal::Queries(1))); + assert_eq!(admit(b"SELECT 1", 7, 3, U), Err(Refusal::Queries(3))); let long = vec![b'x'; MAX_SQL + 1]; - assert_eq!(admit(&long, 0, 10), Err(Refusal::TooLong(MAX_SQL + 1))); - assert_eq!(admit(&long[..MAX_SQL], 0, 10).map(str::len), Ok(MAX_SQL)); + assert_eq!(admit(&long, 0, 10, U), Err(Refusal::TooLong(MAX_SQL + 1))); + assert_eq!(admit(&long[..MAX_SQL], 0, 10, U).map(str::len), Ok(MAX_SQL)); // Layer 2 reads text: a byte that is not UTF-8 (hidden in a comment) is refused here. - assert_eq!(admit(b"SELECT LOAD_FILE('/etc/passwd') /* \xff */", 0, 10), Err(Refusal::NotUtf8)); - assert_eq!(admit("SELECT 'é'".as_bytes(), 0, 10), Ok("SELECT 'é'")); + assert_eq!(admit(b"SELECT LOAD_FILE('/etc/passwd') /* \xff */", 0, 10, U), Err(Refusal::NotUtf8)); + assert_eq!(admit("SELECT 'é'".as_bytes(), 0, 10, U), Ok("SELECT 'é'")); } + const U: &str = "utf8mb4"; + #[test] fn the_time_cap_is_not_the_plugins() { for sql in [ @@ -125,9 +227,83 @@ mod tests { "SET STATEMENT MAX_STATEMENT_TIME=100 FOR SELECT SLEEP(100)", "SELECT @@max_statement_time", ] { - assert_eq!(admit(sql.as_bytes(), 0, 10), Err(Refusal::TimeCap), "{sql}"); + assert_eq!(admit(sql.as_bytes(), 0, 10, U), Err(Refusal::TimeCap), "{sql}"); + } + } + + #[test] + fn session_state_is_an_allowlist() { + for sql in [ + "SET NAMES utf8mb4", + "set names 'utf8mb4' collate 'utf8mb4_unicode_520_ci'", + "SET CHARACTER SET utf8mb4", + "SET SESSION group_concat_max_len = 100000", + "SET group_concat_max_len=4096;", + "SET SQL_BIG_SELECTS=1", + "set session sql_big_selects = ON", + ] { + assert!(admit(sql.as_bytes(), 0, 10, U).is_ok(), "{sql}"); + } + for sql in [ + "SET SESSION completion_type = 'CHAIN'", + "SET completion_type=1", + "SET SESSION sql_select_limit = 1", + "SET SESSION transaction_read_only = 1", + "SET SESSION sql_mode = 'TRADITIONAL'", + "SET autocommit = 0", + "SET autocommit = FALSE", + "SET time_zone = '+05:00'", + "SET TRANSACTION ISOLATION LEVEL SERIALIZABLE", + "SET SESSION TRANSACTION READ ONLY", + "SET @@session.sql_select_limit = 1", + "SET NAMES latin1", + "SET NAMES utf8mb4 COLLATE latin1_swedish_ci", + "SET NAMES utf8mb4, sql_select_limit = 1", + "SET group_concat_max_len = 1, sql_select_limit = 1", + "/* hidden */ SET sql_select_limit = 1", + "-- note\nSET sql_select_limit = 1", + "# note\nset sql_select_limit=1", + "SET SQL_BIG_SELECTS = 0", + "COMMIT AND CHAIN", + "ROLLBACK AND CHAIN", + "commit work release", + "ROLLBACK /* x */ RELEASE", + ] { + assert_eq!(admit(sql.as_bytes(), 0, 10, U), Err(Refusal::SessionState), "{sql}"); + } + for sql in ["COMMIT", "ROLLBACK AND NO CHAIN NO RELEASE", "ROLLBACK TO SAVEPOINT a", "RELEASE SAVEPOINT a", "SELECT 'SET x = 1'"] { + assert!(admit(sql.as_bytes(), 0, 10, U).is_ok(), "{sql}"); + } + // No charset known: no SET NAMES either. + assert_eq!(admit(b"SET NAMES utf8mb4", 0, 10, ""), Err(Refusal::SessionState)); + } + + #[test] + fn sql_shaping_hooks_are_never_mirrored() { + for h in [ + "query", + "dbdelta_queries", + "dbdelta_create_queries", + "posts_where", + "posts_request", + "posts_clauses", + "post_limits_request", + "comments_clauses", + "terms_clauses", + "users_pre_query", + "posts_pre_query", + "found_posts_query", + "comment_feed_join", + "getarchives_where", + "get_meta_sql", + "incompatible_sql_modes", + "Query", + ] { + assert!(shapes_sql(h), "{h}"); + } + for h in ["the_content", "init", "wp_head", "the_title", "query_vars", "pre_get_posts", "shortcode_atts_x", "admin_notices"] { + assert!(!shapes_sql(h), "{h}"); } - assert!(admit(b"SET SESSION sql_mode = 'TRADITIONAL'", 0, 10).is_ok()); } #[test] diff --git a/ext/php/src/layer3.rs b/ext/php/src/layer3.rs index a507e0b..d67da6d 100644 --- a/ext/php/src/layer3.rs +++ b/ext/php/src/layer3.rs @@ -210,6 +210,8 @@ struct Channel { queries: u32, /// The query cap's line was written (once per request). cap_logged: bool, + /// The worker connection's charset (the only one `SET NAMES` may name, [`gateway::admit`]). + charset: String, } #[derive(Debug, Clone, Copy)] @@ -230,6 +232,8 @@ static CHANNELS: Mutex>>> = Mutex::new(No /// Slugs whose query the stand-in is running now (a stack: a query's `query` filter may call /// another isolated plugin that queries in turn). Read by [`gateway_component`]. static GATEWAY: Mutex> = Mutex::new(Vec::new()); +/// `slug\0hook` pairs whose not-mirrored line was written this request. +static UNMIRRORED: Mutex> = Mutex::new(Vec::new()); /// The name the stand-in for `slug` is compiled under (its frames' filename). fn stub_name(slug: &str) -> String { @@ -252,6 +256,7 @@ pub(crate) fn gateway_component(file: &str) -> Option { pub(crate) fn request_shutdown() { lock(&CHANNELS).take(); lock(&GATEWAY).clear(); + lock(&UNMIRRORED).clear(); } /// Whether the `zend_compile_file` wrapper is needed for layer 3: a worker with plugins to @@ -381,6 +386,7 @@ pub fn securecore_layer3_open(slug: String, file: String, abspath: String, prefi busy: false, queries: 0, cap_logged: false, + charset: hello.db.charset.clone(), }; let slot = channels.entry(slug.clone()).or_insert(Some(channel)); pump(&slug, slot, Some(first)) @@ -505,7 +511,7 @@ fn pump(slug: &str, slot: &mut Option, mut first: Option>) -> Z } }; if let Some(sql) = gateway::decode_query(&frame) { - match gateway::admit(sql, ch.queries, cap) { + match gateway::admit(sql, ch.queries, cap, &ch.charset) { Ok(text) => { ch.queries += 1; ch.pending = Some(text.to_owned()); @@ -570,6 +576,49 @@ pub fn securecore_layer3_db_begin(slug: String) -> Option { Some(sql) } +/// The end of the stand-in's exchange with `slug`'s child, however it ended (its `finally`): the +/// slug's frames stop being the plugin's, and a query whose answer never went back (an exception +/// in the worker while it ran) ends the channel, since the child is still waiting for it. +#[php_function] +#[php(name = "securecore_layer3_db_end")] +pub fn securecore_layer3_db_end(slug: String) { + { + let mut gateway = lock(&GATEWAY); + if let Some(i) = gateway.iter().rposition(|s| *s == slug) { + gateway.remove(i); + } + } + let mut channels = lock(&CHANNELS); + let Some(slot) = channels.as_mut().and_then(|c| c.get_mut(&slug)) else { return }; + if slot.as_ref().is_some_and(|ch| ch.busy) { + log(LOG_WARNING, &format!("layer3 plugin:{slug} exchange aborted: the worker failed while running its query")); + if let Some(ch) = slot.take() + && let Some(&control) = CONTROL.get() + && ch.pid > 0 + { + // SAFETY: the control socket, open for the process's life. + let _ = wire::send_packet(unsafe { BorrowedFd::borrow_raw(control) }, &wire::encode_kill(ch.pid), None); + } + } +} + +/// Whether the stand-in mirrors the isolated plugin's callback on `hook` in the worker: never for +/// a hook whose value is SQL core runs ([`gateway::shapes_sql`]), with a line once per request. +#[php_function] +#[php(name = "securecore_layer3_mirrorable")] +pub fn securecore_layer3_mirrorable(slug: String, hook: String) -> bool { + if !gateway::shapes_sql(&hook) { + return true; + } + let key = format!("{slug}\0{hook}"); + let mut seen = lock(&UNMIRRORED); + if !seen.contains(&key) { + log(LOG_NOTICE, &format!("layer3 plugin:{slug} hook {hook} not mirrored: it shapes SQL the worker runs")); + seen.push(key); + } + false +} + /// The stand-in's answer to the child's query (PHP-serialized). Sends it (or a refusal, when it /// is over `layer3.limits.dbResultBytes` or the deadline passed), then waits on as /// [`securecore_layer3_call`] does: the answer, the next query, or null. @@ -987,6 +1036,8 @@ pub(crate) fn register(module: ModuleBuilder) -> ModuleBuilder { .function(wrap_function!(securecore_layer3_call)) .function(wrap_function!(securecore_layer3_db_begin)) .function(wrap_function!(securecore_layer3_db_reply)) + .function(wrap_function!(securecore_layer3_db_end)) + .function(wrap_function!(securecore_layer3_mirrorable)) .function(wrap_function!(securecore_layer3_db)) .function(wrap_function!(securecore_layer3_helper)) .function(wrap_function!(securecore_layer3_zygote)) diff --git a/ext/php/src/layer3/stub.php b/ext/php/src/layer3/stub.php index 94d43af..d2ce1f9 100644 --- a/ext/php/src/layer3/stub.php +++ b/ext/php/src/layer3/stub.php @@ -46,17 +46,26 @@ // but unbuffered (MYSQLI_USE_RESULT): rows are read one at a time and copied only up to // $limit bytes (layer3.limits.dbResultBytes). Past it the rest is drained unread and the // answer is a refusal, so a result far over the cap never sits in the worker's memory. - $run = static function (int $limit) use ($slug, $clean): string { + // The worker's mysqli connection for an exchange, the one every query of it and every + // bookkeeping statement after it uses ($wpdb->dbh: a db.php drop-in's current connection + // too); null when $wpdb has none (an SQLite drop-in), and then the gateway refuses. + $conn = static function (): ?\mysqli { global $wpdb; + $dbh = $wpdb->dbh ?? null; + if (!$dbh instanceof \mysqli && !empty($wpdb->ready) && $wpdb->check_connection(false)) $dbh = $wpdb->dbh ?? null; + return $dbh instanceof \mysqli ? $dbh : null; + }; + $run = static function (?\mysqli $dbh, int $limit) use ($slug, $clean): string { $sql = \securecore_layer3_db_begin($slug); if (!is_string($sql)) { return serialize(['ret' => false, 'error' => 'securecore: layer3 had no query to run']); } + if ($dbh === null) { + return serialize(['ret' => false, 'error' => "securecore: layer3: the worker's \$wpdb has no mysqli connection (a db.php drop-in such as SQLite), so an isolated plugin can't use the database"]); + } $sql = (string) apply_filters('query', $sql); - $dbh = $wpdb->dbh ?? null; - if (!$dbh instanceof \mysqli && $wpdb->check_connection(false)) $dbh = $wpdb->dbh ?? null; - if ($sql === '' || !$dbh instanceof \mysqli) { - return serialize(['ret' => false, 'error' => $sql === '' ? '' : 'securecore: layer3: the worker has no database connection']); + if ($sql === '') { + return serialize(['ret' => false, 'error' => '']); } $ret = false; $error = ''; @@ -78,7 +87,10 @@ $bytes = 0; while (($row = mysqli_fetch_assoc($res)) !== null && $row !== false) { if ($keys === []) $keys = array_keys($row); - // What serialize() writes for each value, near enough (its text and ~16 bytes). + // What a row costs the worker while it's copied, not only what serialize() writes: + // ~200 bytes for the row's array, ~16 more per value, and the value's text. A + // million one-byte rows count 200 MB, not 17. + $bytes += 200; foreach ($row as $x) $bytes += strlen((string) $x) + 16; if ($limit > 0 && $bytes > $limit) { $rows = []; @@ -120,51 +132,106 @@ // past the deadline on the server), then the session's own value again (read once per // request). MariaDB's max_statement_time covers every statement; MySQL's max_execution_time // only SELECTs (addiscorp/securecore#99). + // + // Every bookkeeping statement (the cap, the state, the rollback) goes straight to mysqli on + // the exchange's connection: never through $wpdb->query() and its `query` filters, which a + // plugin could use to rewrite them (an isolated plugin's `query` callback isn't mirrored at + // all, below, but another plugin's would run there too). + $one = static function (\mysqli $dbh, string $sql): ?array { + try { + $r = mysqli_query($dbh, $sql); + } catch (\Throwable $e) { + return null; + } + if ($r instanceof \mysqli_result) { + $row = mysqli_fetch_row($r); + mysqli_free_result($r); + return is_array($row) ? $row : null; + } + return $r === true ? [] : null; + }; $before = null; - $cap = static function (?int $ms) use (&$before, $mariadb): void { - global $wpdb; + $cap = static function (\mysqli $dbh, ?int $ms) use (&$before, $mariadb, $one): void { $var = $mariadb() ? 'max_statement_time' : 'max_execution_time'; - $before ??= (string) $wpdb->get_var("SELECT @@SESSION.$var"); + $before ??= (string) ($one($dbh, "SELECT @@SESSION.$var")[0] ?? '0'); if ($ms === null) { $value = is_numeric($before) ? $before : '0'; } else { $value = $var === 'max_statement_time' ? sprintf('%.3F', $ms / 1000) : (string) $ms; } - $wpdb->query("SET SESSION $var = $value"); + $one($dbh, "SET SESSION $var = $value"); }; // The connection's transaction state, as the server reports it (never read from the SQL - // text): [autocommit on, a transaction open]. - $state = static function () use ($mariadb): array { + // text): [autocommit on, a transaction open (null: MySQL can't say)], or null when it can't + // be read (then it counts as open). MariaDB has @@in_transaction; on MySQL only autocommit is + // readable without the PROCESS privilege, and InnoDB starts a transaction lazily anyway. + $state = static function (\mysqli $dbh) use ($mariadb, $one): ?array { + $r = $one($dbh, $mariadb() ? 'SELECT @@autocommit, @@in_transaction' : 'SELECT @@autocommit'); + if ($r === null || !isset($r[0])) return null; + return [(int) $r[0] === 1, isset($r[1]) ? (int) $r[1] > 0 : null]; + }; + // After an exchange: put the connection back as it was. On MariaDB, a transaction the + // plugin left open or autocommit it turned off (or a state that can't be read) is rolled back + // and autocommit turned on, then the state read again; if it still isn't clean, the + // connection is replaced. On MySQL, where an open transaction can't be seen, the rollback + // runs whenever autocommit was on before the plugin's first query (a no-op if nothing is + // open). `AND NO CHAIN`: no completion_type turns the rollback into a new transaction (and the + // gateway refuses any change to it). + $restore = static function (\mysqli $dbh, ?array $was) use ($slug, $mariadb, $state, $one): void { global $wpdb; - $open = $mariadb() - ? '@@in_transaction' - : '(SELECT COUNT(*) FROM information_schema.innodb_trx WHERE trx_mysql_thread_id = CONNECTION_ID())'; - $r = $wpdb->get_row("SELECT @@autocommit, $open", ARRAY_N); - return [(int) ($r[0] ?? 1) === 1, (int) ($r[1] ?? 0) > 0]; + $was_ac = $was[0] ?? true; + $was_open = $was[1] ?? false; + $now = $state($dbh); + if ($mariadb()) { + $left = $now === null || ($now[1] && !$was_open) || (!$now[0] && $was_ac); + } else { + $left = $was_ac; + } + if (!$left) return; + $one($dbh, 'ROLLBACK AND NO CHAIN'); + if ($was_ac) $one($dbh, 'SET autocommit = 1'); + $after = $state($dbh); + $clean = $after !== null && ($after[1] ?? false) === false && ($after[0] || !$was_ac); + if ($mariadb() || $now === null || !$now[0]) { + $what = $now === null ? 'its state unreadable' : ($now[1] ? 'a transaction open' : 'autocommit off'); + error_log("securecore: layer3 plugin:$slug left $what on the worker's connection: rolled back" + . ($now !== null && !$now[0] && $was_ac ? ', autocommit restored' : '')); + } + if (!$clean) { + error_log("securecore: layer3 plugin:$slug: the worker's connection is still not clean after the rollback; reconnecting"); + $wpdb->close(); + $wpdb->db_connect(false); + } }; // Waits out the child's queries: the answer (a string), or null. However the exchange ended - // (an answer, a timeout, a crash), a transaction the plugin opened, or autocommit it turned - // off, is rolled back and restored, so core's later writes in the request never join it. - $pump = static function ($answer) use ($slug, $run, $cap, $state) { - global $wpdb; + // (an answer, a timeout, a crash, an exception in the worker), the slug's frames stop being + // the plugin's and the connection is put back ($restore), so core's later writes in the + // request never join what the plugin left. + $pump = static function ($answer) use ($slug, $run, $cap, $state, $restore, $conn) { + $dbh = null; $was = null; + $started = false; $set_at = null; - while (is_array($answer)) { - $was ??= $state(); - if ($set_at === null || microtime(true) - $set_at > 0.05) { - $cap(max(1, (int) ($answer['ms'] ?? 1))); - $set_at = microtime(true); + try { + while (is_array($answer)) { + if (!$started) { + $started = true; + $dbh = $conn(); + if ($dbh !== null) $was = $state($dbh); + } + if ($dbh !== null && ($set_at === null || microtime(true) - $set_at > 0.05)) { + $cap($dbh, max(1, (int) ($answer['ms'] ?? 1))); + $set_at = microtime(true); + } + $answer = \securecore_layer3_db_reply($slug, $run($dbh, (int) ($answer['bytes'] ?? 0))); } - $answer = \securecore_layer3_db_reply($slug, $run((int) ($answer['bytes'] ?? 0))); - } - if ($was !== null) { - $cap(null); - [$autocommit, $open] = $state(); - if (($open && !$was[1]) || (!$autocommit && $was[0])) { - $wpdb->query('ROLLBACK'); - if (!$autocommit && $was[0]) $wpdb->query('SET autocommit = 1'); - error_log("securecore: layer3 plugin:$slug left " . ($open ? 'a transaction open' : 'autocommit off') - . " on the worker's connection: rolled back" . (!$autocommit && $was[0] ? ', autocommit restored' : '')); + } finally { + if ($started) { + \securecore_layer3_db_end($slug); + if ($dbh !== null) { + $cap($dbh, null); + $restore($dbh, $was); + } } } return $answer; @@ -190,7 +257,9 @@ }; if ($kind === 'shortcode') { add_shortcode((string) $r['name'], $forward); - } else { + } elseif (\securecore_layer3_mirrorable($slug, (string) $r['name'])) { + // Never a hook whose value is SQL the worker runs (`query`, `posts_where`, …): + // from the child it would rewrite core's queries. add_filter((string) $r['name'], $forward, (int) ($r['priority'] ?? 10), (int) ($r['args'] ?? 1)); } } diff --git a/test/ext-layer3.sh b/test/ext-layer3.sh index 0f7be51..3456982 100755 --- a/test/ext-layer3.sh +++ b/test/ext-layer3.sh @@ -107,6 +107,10 @@ d_aco=$(page "L3 db autocommit off" '[sc_l3db_autocommit_off] SC-L3DB-AFTER-ACO' d_huge=$(page "L3 db huge" '[sc_l3db_huge] SC-L3DB-AFTER-HUGE') d_err=$(page "L3 db error" '[sc_l3db_err]') d_sleeps=$(page "L3 db sleeps" '[sc_l3db_sleeps] SC-L3DB-AFTER-SLEEPS') +d_chain=$(page "L3 db chain" '[sc_l3db_chain] SC-L3DB-AFTER-CHAIN') +d_session=$(page "L3 db session" '[sc_l3db_session]') +d_tiny=$(page "L3 db tiny" '[sc_l3db_tiny] SC-L3DB-AFTER-TINY') +d_throw=$(page "L3 db throw" '[sc_l3db_throw] SC-L3DB-AFTER-THROW') # scan_secrets : fetch the secrets page into $dir/ and, while the process serving its # call waits, read that process's environment and memory from outside (as root with # CAP_SYS_PTRACE in the php container: a layer 3 child isn't dumpable) into $dir/.scan, @@ -131,7 +135,7 @@ cost() { cleanup() { wp plugin deactivate sc-l3probe sc-l3-neighbour hello sc-l3db >/dev/null 2>&1 - for p in $p_main $p_loop $p_mem $p_probe $d_rw $d_l2 $d_l3 $d_many $d_big $d_sleep $d_secrets $d_time $d_txn $d_txo $d_aco $d_huge $d_err $d_sleeps; do wp post delete "$p" --force >/dev/null 2>&1; done + for p in $p_main $p_loop $p_mem $p_probe $d_rw $d_l2 $d_l3 $d_many $d_big $d_sleep $d_secrets $d_time $d_txn $d_txo $d_aco $d_huge $d_err $d_sleeps $d_chain $d_session $d_tiny $d_throw; do wp post delete "$p" --force >/dev/null 2>&1; done wp option update blogname SecureCore >/dev/null 2>&1 db_exec "DROP TABLE IF EXISTS wp_sc_l3db_items, wp_sc_l3_neighbour_data; DELETE FROM wp_options WHERE option_name LIKE 'sc\\_l3db\\_%' OR option_name LIKE '\\_transient\\_%sc\\_l3db%' OR option_name = 'sc_l3_neighbour_mark'" in_php "rm -rf $probe $content/plugins/sc-l3-neighbour.php $content/plugins/sc-l3db.php $content/uploads/sc-l3db $content/uploads/sc-l3probe $content/uploads/sc-l3-up.txt" @@ -155,7 +159,7 @@ cfg() { # $1 name, $2 layer2 mode, $3 layer3 enabled "layer3": { "enabled": $3, "isolate": ["sc-l3probe", "hello", "sc-l3db"], - "limits": { "timeoutMs": 1500, "memoryMb": 64, "dbQueries": 60, "dbResultBytes": 65536 }, + "limits": { "timeoutMs": 1500, "memoryMb": 64, "dbQueries": 60, "dbResultBytes": 4194304 }, "write": { "sc-l3probe": ["wp-content/uploads/sc-l3probe"], "sc-l3db": ["wp-content/uploads/sc-l3db"] } } } @@ -257,7 +261,7 @@ want dbl3 not-utf8-direct none "db gateway: the child has no connection of its o want dbl3 time-cap BLOCKED "db gateway" has "$T" "layer3 plugin:sc-l3db database refused: the query is not UTF-8" "db gateway: SQL that isn't UTF-8 is refused, logged" has "$T" "layer3 plugin:sc-l3db database refused: it changes the statement time cap" "db gateway: a change to the time cap is refused, logged" -# The caps (dbQueries 60, dbResultBytes 65536, timeoutMs 1500). +# The caps (dbQueries 60, dbResultBytes 4 MiB, timeoutMs 1500). T=$(now); sleep 1 get many "/?page_id=$d_many" ran=$(verdict many ran) @@ -269,7 +273,7 @@ is "$(phplog "$T" | grep -c 'layer3 plugin:sc-l3db database refused: over the db get big "/?page_id=$d_big" want big big BLOCKED "caps" want big error dbResultBytes "caps" -has "$T" "over the dbResultBytes cap of 65536" "caps: the result cap is logged" +has "$T" "over the dbResultBytes cap of 4194304" "caps: the result cap is logged" get sleep "/?page_id=$d_sleep" is "$(status_of sleep)" 200 "caps: the 5 s statement's page renders" contains sleep SC-L3DB-AFTER-SLEEP "caps: the rest of the page is there" @@ -289,19 +293,41 @@ is "$(db_exec "SELECT option_value FROM wp_options WHERE option_name = 'sc_l3_ne "transactions: a later write in the same request (another plugin's) committed, not swallowed by the open transaction" # Left open on a normal return: a START TRANSACTION behind a comment, and autocommit turned off. # Read from the server's state, not the SQL: rolled back, autocommit restored, core's write commits. -for c in txo:open1 aco:ac1; do +# (txo: behind a comment; chain: after trying completion_type = CHAIN; the plugin's own `query` +# filter answers the stand-in's state read with "nothing open" if it is ever mirrored; throw: a +# `query` filter in the worker throws while the plugin's query runs, caught around the shortcodes.) +for c in txo:open1 chain:chain1 throw:thrown1; do n=${c%%:*} row=${c#*:} eval "pg=\$d_$n" T=$(now); sleep 1 mark=m$$${n}$(date +%s) - get "$n" "/?page_id=$pg&sc_l3_mark=$mark" - want "$n" returned yes "transactions ($n): the call returned normally" + get "$n" "/?page_id=$pg&sc_l3_mark=$mark&sc_l3_throw=1&sc_l3_session=1" + [ "$n" = throw ] || want "$n" returned yes "transactions ($n): the call returned normally" has "$T" "layer3 plugin:sc-l3db left a transaction open on the worker's connection: rolled back" "transactions ($n): the worker rolled back what the plugin left open" is "$(db_exec "SELECT COUNT(*) FROM wp_sc_l3db_items WHERE name = '$row'")" 0 "transactions ($n): the plugin's uncommitted row is not in the database" is "$(db_exec "SELECT option_value FROM wp_options WHERE option_name = 'sc_l3_neighbour_mark'")" "$mark" \ "transactions ($n): core's later write in the same request committed" + is "$(verdict "$n" core_rows)" 5 "transactions ($n): core's next query on the connection is unaffected" done -has "$T" "rolled back, autocommit restored" "transactions (aco): autocommit is restored" +has "$T" "layer3 plugin:sc-l3db hook query not mirrored: it shapes SQL the worker runs" "hooks: the plugin's own query filter is never mirrored into the worker" +want chain completion_type BLOCKED "transactions (chain): completion_type is refused" +contains throw "caught=RuntimeException" "transactions (throw): the worker's exception reached the page's own catch" +has "$T" "layer3 plugin:sc-l3db exchange aborted: the worker failed while running its query" "transactions (throw): the aborted exchange ends the channel, with a line" +# autocommit off is refused now (session state), so the plugin's write is an ordinary committed one. +T=$(now); sleep 1 +mark=m$$aco$(date +%s) +get aco "/?page_id=$d_aco&sc_l3_mark=$mark" +want aco returned yes "transactions (aco): the call returned normally" +has "$T" "layer3 plugin:sc-l3db database refused: it changes the worker connection's session state" "transactions (aco): SET autocommit is refused" +is "$(db_exec "SELECT option_value FROM wp_options WHERE option_name = 'sc_l3_neighbour_mark'")" "$mark" "transactions (aco): core's later write committed" +# Session state: the allowlist, and core's next queries see the session as it was. +get sbase "/?page_id=$d_err&sc_l3_session=1" +get session "/?page_id=$d_session&sc_l3_session=1" +for c in names group_concat big_selects; do want session "$c" OK "session: allowed"; done +for c in completion_type select_limit read_only sql_mode autocommit time_zone isolation hidden names_other chain; do want session "$c" BLOCKED "session: refused"; done +base=$(verdict sbase session) +is "$(verdict session session)" "$base" "session: core's next query sees the session unchanged ($base)" +is "$(verdict session core_rows)" 5 "session: core's LIMIT 5 query returns 5 rows (no sql_select_limit)" # dbResultBytes against a result far over it (40 MiB, cap 64 KiB): refused while the rows are # copied, and the worker stays within memory_limit. get huge "/?page_id=$d_huge&sc_l3_peak=1" @@ -310,7 +336,16 @@ contains huge SC-L3DB-AFTER-HUGE "result cap: the rest of the page is there" want huge huge BLOCKED "result cap" want huge error dbResultBytes "result cap" peak=$(verdict huge peak) -if [ -n "$peak" ] && [ "$peak" -lt 40000000 ]; then pass "result cap: the worker's peak memory stayed at $peak bytes, under the 40 MiB result"; else fail "result cap: the worker's peak memory stayed under the 40 MiB result" "'$peak'"; fi +if [ -n "$peak" ] && [ "$peak" -lt 20000000 ]; then pass "result cap: the worker's peak memory stayed at $peak bytes, under the 40 MiB result"; else fail "result cap: the worker's peak memory stayed under 20 MB" "'$peak'"; fi +# A million one-byte rows under the 4 MiB cap: refused early (each row counts ~200 bytes), the +# worker's memory bounded. +get tiny "/?page_id=$d_tiny&sc_l3_peak=1" +is "$(status_of tiny)" 200 "result cap: the million-row page renders" +contains tiny SC-L3DB-AFTER-TINY "result cap: the rest of the page is there" +want tiny tiny BLOCKED "result cap: a million tiny rows" +want tiny error dbResultBytes "result cap: a million tiny rows" +peak=$(verdict tiny peak) +if [ -n "$peak" ] && [ "$peak" -lt 20000000 ]; then pass "result cap: the worker's peak memory for a million rows stayed at $peak bytes"; else fail "result cap: the worker's peak memory for a million rows stayed under 20 MB" "'$peak'"; fi # The error the child sees names neither the database nor its user. get err "/?page_id=$d_err" e=$(verdict err err) diff --git a/test/ext/layer3-plugin/sc-l3-neighbour.php b/test/ext/layer3-plugin/sc-l3-neighbour.php index 933bdfd..7d9d392 100644 --- a/test/ext/layer3-plugin/sc-l3-neighbour.php +++ b/test/ext/layer3-plugin/sc-l3-neighbour.php @@ -28,5 +28,32 @@ if (isset($_GET['sc_l3_peak'])) { $content .= "\n
\npeak=" . memory_get_peak_usage() . "\n
"; } + // ?sc_l3_session=1: the worker connection's session state as core's next queries see it. + if (isset($_GET['sc_l3_session'])) { + global $wpdb; + $s = $wpdb->get_row('SELECT @@sql_select_limit AS l, @@completion_type AS c, @@transaction_read_only AS r, @@autocommit AS a, @@time_zone AS z, @@sql_mode AS m', ARRAY_A); + $n = count((array) $wpdb->get_col("SELECT option_id FROM {$wpdb->options} LIMIT 5")); + $content .= "\n
\nsession=" . esc_html(implode('|', (array) $s)) . "\ncore_rows=$n\n
"; + } return $content; }, 30); + +// A `query` filter that throws on one marker query (an exception in the worker while it runs an +// isolated plugin's query), and ?sc_l3_throw=1 catching it around the shortcodes so the page +// goes on. +add_filter('query', static function ($q) { + if (is_string($q) && str_contains($q, 'sc_l3db_throw_marker')) { + throw new RuntimeException('sc-l3-neighbour: thrown from a query filter'); + } + return $q; +}); +if (isset($_GET['sc_l3_throw'])) { + remove_filter('the_content', 'do_shortcode', 11); + add_filter('the_content', static function ($content) { + try { + return do_shortcode($content); + } catch (\Throwable $e) { + return $content . "\n
\ncaught=" . get_class($e) . "\n
"; + } + }, 11); +} diff --git a/test/ext/layer3-plugin/sc-l3db.php b/test/ext/layer3-plugin/sc-l3db.php index c7e0ce1..dbbb373 100644 --- a/test/ext/layer3-plugin/sc-l3db.php +++ b/test/ext/layer3-plugin/sc-l3db.php @@ -8,6 +8,13 @@ exit; } +// A hostile `query` filter: it answers any query that reads the connection's transaction state +// with "nothing open". Mirrored into the worker, it would hide the transactions below from the +// stand-in's check; it must never be mirrored. +add_filter('query', static function ($q) { + return is_string($q) && stripos($q, '@@in_transaction') !== false ? 'SELECT 1, 0' : $q; +}); + // A query while the plugin loads (before its child reports ready): its own option. $GLOBALS['sc_l3db_loaded'] = get_option('sc_l3db_greeting', 'none'); @@ -143,6 +150,60 @@ function sc_l3db_ran($ret): string return sc_l3db_out(['returned' => 'yes']); }); +// completion_type = CHAIN would turn the stand-in's ROLLBACK into a new transaction: set it, +// then leave a transaction open. +add_shortcode('sc_l3db_chain', static function () { + global $wpdb; + $set = $wpdb->query("SET SESSION completion_type = 'CHAIN'"); + $wpdb->query('START TRANSACTION'); + $wpdb->insert($wpdb->prefix . 'sc_l3db_items', ['name' => 'chain1', 'n' => 8]); + return sc_l3db_out(['returned' => 'yes', 'completion_type' => sc_l3db_ran($set)]); +}); + +// Session state: what the gateway lets an isolated plugin SET, and what it refuses (each would +// persist into core's later queries on the worker's connection). +add_shortcode('sc_l3db_session', static function () { + global $wpdb; + $r = []; + foreach ([ + 'names' => 'SET NAMES utf8mb4', + 'group_concat' => 'SET SESSION group_concat_max_len = 100000', + 'big_selects' => 'SET SQL_BIG_SELECTS = 1', + 'completion_type' => "SET SESSION completion_type = 'CHAIN'", + 'select_limit' => 'SET SESSION sql_select_limit = 1', + 'read_only' => 'SET SESSION transaction_read_only = 1', + 'sql_mode' => "SET SESSION sql_mode = 'TRADITIONAL'", + 'autocommit' => 'SET autocommit = 0', + 'time_zone' => "SET time_zone = '+05:00'", + 'isolation' => 'SET TRANSACTION ISOLATION LEVEL SERIALIZABLE', + 'hidden' => '/* x */ SET sql_select_limit = 2', + 'names_other' => 'SET NAMES latin1', + 'chain' => 'COMMIT AND CHAIN', + ] as $k => $sql) { + $r[$k] = sc_l3db_ran($wpdb->query($sql)); + } + return sc_l3db_out($r); +}); + +// The result cap against a million tiny rows (one byte each): refused long before the cap's +// worth of serialized text, because each row costs the worker far more than its bytes. +add_shortcode('sc_l3db_tiny', static function () { + global $wpdb; + $c = 'information_schema.COLLATIONS'; + $v = $wpdb->get_col("SELECT 1 FROM $c a, $c b, $c c LIMIT 1000000"); + return sc_l3db_out(['tiny' => count($v) === 1000000 ? 'OK' : 'BLOCKED', 'error' => str_contains($wpdb->last_error, 'dbResultBytes') ? 'dbResultBytes' : 'other']); +}); + +// An exception in the worker while it runs this plugin's query (the neighbour's `query` filter +// throws on this one), with a transaction open: the stand-in still cleans up. +add_shortcode('sc_l3db_throw', static function () { + global $wpdb; + $wpdb->query('START TRANSACTION'); + $wpdb->insert($wpdb->prefix . 'sc_l3db_items', ['name' => 'thrown1', 'n' => 8]); + $wpdb->query("SELECT 'sc_l3db_throw_marker'"); + return sc_l3db_out(['returned' => 'yes']); +}); + // The result cap against a result far larger than it: 40 rows of 1 MiB. add_shortcode('sc_l3db_huge', static function () { global $wpdb; @@ -193,8 +254,8 @@ function sc_l3db_ran($ret): string // The result cap: one answer of 300 KB. add_shortcode('sc_l3db_big', static function () { global $wpdb; - $v = $wpdb->get_var("SELECT REPEAT('x', 300000)"); - return sc_l3db_out(['big' => is_string($v) && strlen($v) === 300000 ? 'OK' : 'BLOCKED', 'error' => str_contains($wpdb->last_error, 'dbResultBytes') ? 'dbResultBytes' : 'other']); + $v = $wpdb->get_var("SELECT REPEAT('x', 5000000)"); + return sc_l3db_out(['big' => is_string($v) && strlen($v) === 5000000 ? 'OK' : 'BLOCKED', 'error' => str_contains($wpdb->last_error, 'dbResultBytes') ? 'dbResultBytes' : 'other']); }); // The time cap: a 5 s statement. From 2ad8d66f0d9c38f85818578c497d446e2b06ee34 Mon Sep 17 00:00:00 2001 From: Sami Fouad Date: Wed, 11 Nov 2026 18:17:35 -0600 Subject: [PATCH 6/8] ext-layer3: the session check no longer overwrites the base URL; the neighbour's LIMIT check reads its own table --- test/ext-layer3.sh | 6 +++--- test/ext/layer3-plugin/sc-l3-neighbour.php | 4 +++- 2 files changed, 6 insertions(+), 4 deletions(-) diff --git a/test/ext-layer3.sh b/test/ext-layer3.sh index 3456982..194495b 100755 --- a/test/ext-layer3.sh +++ b/test/ext-layer3.sh @@ -83,7 +83,7 @@ in_php "rm -rf $content/plugins/sc-l3db.php $content/uploads/sc-l3db && mkdir -p compose cp test/ext/layer3-plugin/sc-l3db.php "php:$content/plugins/sc-l3db.php" >>"$dir/cp.log" 2>&1 # db_exec : as the site's database user, on the db container. db_exec() { compose exec -T db mariadb -uwordpress -pwordpress-db-password -N wordpress -e "$1" 2>>"$dir/db.err"; } -db_exec "DROP TABLE IF EXISTS wp_sc_l3db_items; CREATE TABLE IF NOT EXISTS wp_sc_l3_neighbour_data (id int); DELETE FROM wp_options WHERE option_name LIKE 'sc\\_l3db\\_%' OR option_name LIKE '\\_transient\\_%sc\\_l3db%'" +db_exec "DROP TABLE IF EXISTS wp_sc_l3db_items; DROP TABLE IF EXISTS wp_sc_l3_neighbour_data; CREATE TABLE wp_sc_l3_neighbour_data (id int); INSERT INTO wp_sc_l3_neighbour_data VALUES (1), (2), (3), (4), (5), (6); DELETE FROM wp_options WHERE option_name LIKE 'sc\\_l3db\\_%' OR option_name LIKE '\\_transient\\_%sc\\_l3db%'" wp option add sc_l3db_greeting hello-from-wp --autoload=no >/dev/null wp plugin activate sc-l3probe sc-l3-neighbour hello sc-l3db >/dev/null post() { wp post create --post_status=publish --post_title="$1" --post_content="$2" --porcelain; } @@ -325,8 +325,8 @@ get sbase "/?page_id=$d_err&sc_l3_session=1" get session "/?page_id=$d_session&sc_l3_session=1" for c in names group_concat big_selects; do want session "$c" OK "session: allowed"; done for c in completion_type select_limit read_only sql_mode autocommit time_zone isolation hidden names_other chain; do want session "$c" BLOCKED "session: refused"; done -base=$(verdict sbase session) -is "$(verdict session session)" "$base" "session: core's next query sees the session unchanged ($base)" +sess=$(verdict sbase session) +is "$(verdict session session)" "$sess" "session: core's next query sees the session unchanged ($sess)" is "$(verdict session core_rows)" 5 "session: core's LIMIT 5 query returns 5 rows (no sql_select_limit)" # dbResultBytes against a result far over it (40 MiB, cap 64 KiB): refused while the rows are # copied, and the worker stays within memory_limit. diff --git a/test/ext/layer3-plugin/sc-l3-neighbour.php b/test/ext/layer3-plugin/sc-l3-neighbour.php index 7d9d392..5aab59c 100644 --- a/test/ext/layer3-plugin/sc-l3-neighbour.php +++ b/test/ext/layer3-plugin/sc-l3-neighbour.php @@ -32,7 +32,9 @@ if (isset($_GET['sc_l3_session'])) { global $wpdb; $s = $wpdb->get_row('SELECT @@sql_select_limit AS l, @@completion_type AS c, @@transaction_read_only AS r, @@autocommit AS a, @@time_zone AS z, @@sql_mode AS m', ARRAY_A); - $n = count((array) $wpdb->get_col("SELECT option_id FROM {$wpdb->options} LIMIT 5")); + // Its own table (test/ext-layer3.sh puts six rows in it): LIMIT 5 is 5 rows unless a + // session sql_select_limit cut it. + $n = count((array) $wpdb->get_col("SELECT id FROM {$wpdb->prefix}sc_l3_neighbour_data LIMIT 5")); $content .= "\n
\nsession=" . esc_html(implode('|', (array) $s)) . "\ncore_rows=$n\n
"; } return $content; From d6b14a8e6eb54714b1f5ad97b228c9edf89ece55 Mon Sep 17 00:00:00 2001 From: Sami Fouad Date: Wed, 11 Nov 2026 19:10:03 -0600 Subject: [PATCH 7/8] na-expected: layer3 has two more n/a checks on the microVM target (the memory scans of the child and the worker) --- test/lib/na-expected.txt | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/test/lib/na-expected.txt b/test/lib/na-expected.txt index f2482c6..4df1d44 100644 --- a/test/lib/na-expected.txt +++ b/test/lib/na-expected.txt @@ -2,7 +2,7 @@ # na_summary fails a suite whose count differs. Change a number only with the checks it counts. config 5 attribution 0 -layer3 4 +layer3 6 layer1 18 hosts 59 enforce 18 From 2ce853fbf21f4d9dd868b8e41d65ccc93d81bff2 Mon Sep 17 00:00:00 2001 From: Sami Fouad Date: Wed, 11 Nov 2026 19:48:01 -0600 Subject: [PATCH 8/8] Layer 3 gateway: refuse on MySQL (anything not MariaDB) until a MySQL test leg exists (#99) --- ext/README.md | 13 +++++--- ext/layer3/src/gateway.rs | 64 +++++++++++++++++++++++++++++---------- ext/php/src/layer3.rs | 6 +++- 3 files changed, 61 insertions(+), 22 deletions(-) diff --git a/ext/README.md b/ext/README.md index e065db9..8c95627 100644 --- a/ext/README.md +++ b/ext/README.md @@ -1127,11 +1127,14 @@ crash, an exception in the worker, in a `finally`), the stand-in puts the connec it runs `ROLLBACK AND NO CHAIN`, turns autocommit on, reads the state again, and replaces the connection if it still isn't clean; with the line `layer3 plugin: left a transaction open on the worker's connection: rolled back`. -- **MySQL:** an open transaction can't be seen without the PROCESS privilege (and InnoDB starts - one lazily), so whenever autocommit was on before the plugin's first query, the same - `ROLLBACK AND NO CHAIN` and `SET autocommit = 1` run unconditionally (a no-op when nothing is - open). A transaction core itself held open around a call into an isolated plugin would be - rolled back too; WordPress core holds none while rendering. +- **MySQL (and anything else that isn't MariaDB): the gateway refuses.** An open transaction + can't be seen there without the PROCESS privilege (InnoDB starts one lazily, and `START + TRANSACTION` leaves `@@autocommit` as it was), so the worker couldn't put the connection back + without risking a transaction core itself opened around the call (WooCommerce does). Until + there's a MySQL test leg, every isolated plugin's query fails closed when the worker's + `db_server_info()` isn't MariaDB (read once per request, with the hello), with one line per + request: `layer3 plugin: database refused: the database gateway supports MariaDB only for + now (#99)`. The stand-in's MySQL rollback path stays, unreached, for that leg. So core's later writes in the request never join what the plugin left, and no row lock outlives the call. A transaction the plugin opens and ends within one call is its own, as unisolated. XA diff --git a/ext/layer3/src/gateway.rs b/ext/layer3/src/gateway.rs index 7463cde..bdd4bfd 100644 --- a/ext/layer3/src/gateway.rs +++ b/ext/layer3/src/gateway.rs @@ -49,6 +49,18 @@ pub enum Refusal { /// releases the connection: session state persists into core's later queries on the worker's /// connection. SessionState, + /// The worker's server isn't MariaDB ([`is_mariadb`]). On MySQL the worker can't see whether + /// a transaction is open (no `@@in_transaction`), so it couldn't put the connection back + /// without risking core's own transaction: the gateway fails closed until a MySQL test leg + /// exists (addiscorp/securecore#99). + NotMariaDb, +} + +/// Whether the worker's server is MariaDB, from `mysqli_get_server_info()` (`11.4.4-MariaDB-…`, +/// or `5.5.5-10.11.6-MariaDB-…` through the old replication prefix). Anything else (MySQL, +/// Percona, Aurora MySQL, TiDB, an empty string) is not. +pub fn is_mariadb(server_info: &str) -> bool { + server_info.to_ascii_lowercase().contains("mariadb") } impl Refusal { @@ -60,19 +72,23 @@ impl Refusal { Refusal::NotUtf8 => "the query is not UTF-8".to_owned(), Refusal::TimeCap => "it changes the statement time cap".to_owned(), Refusal::SessionState => "it changes the worker connection's session state".to_owned(), + Refusal::NotMariaDb => "the database gateway supports MariaDB only for now (#99)".to_owned(), } } /// Logged once per request and plugin, not per query: a loop past the cap would flood the log. pub fn once_per_request(&self) -> bool { - matches!(self, Refusal::Queries(_)) + matches!(self, Refusal::Queries(_) | Refusal::NotMariaDb) } } /// Whether the worker runs `sql`, the plugin's `sent`-th query of the request (counting from 0), -/// under a cap of `cap` queries, on a connection whose charset is `charset`. `Ok` is the text, -/// for PHP. -pub fn admit<'a>(sql: &'a [u8], sent: u32, cap: u32, charset: &str) -> Result<&'a str, Refusal> { +/// under a cap of `cap` queries, on a connection whose charset is `charset` and whose server is +/// MariaDB (`mariadb`, [`is_mariadb`]). `Ok` is the text, for PHP. +pub fn admit<'a>(sql: &'a [u8], sent: u32, cap: u32, charset: &str, mariadb: bool) -> Result<&'a str, Refusal> { + if !mariadb { + return Err(Refusal::NotMariaDb); + } if sent >= cap { return Err(Refusal::Queries(cap)); } @@ -206,15 +222,15 @@ mod tests { #[test] fn admit_counts_sizes_and_reads_utf8() { - assert_eq!(admit(b"SELECT 1", 0, 1, U), Ok("SELECT 1")); - assert_eq!(admit(b"SELECT 1", 1, 1, U), Err(Refusal::Queries(1))); - assert_eq!(admit(b"SELECT 1", 7, 3, U), Err(Refusal::Queries(3))); + assert_eq!(admit(b"SELECT 1", 0, 1, U, true), Ok("SELECT 1")); + assert_eq!(admit(b"SELECT 1", 1, 1, U, true), Err(Refusal::Queries(1))); + assert_eq!(admit(b"SELECT 1", 7, 3, U, true), Err(Refusal::Queries(3))); let long = vec![b'x'; MAX_SQL + 1]; - assert_eq!(admit(&long, 0, 10, U), Err(Refusal::TooLong(MAX_SQL + 1))); - assert_eq!(admit(&long[..MAX_SQL], 0, 10, U).map(str::len), Ok(MAX_SQL)); + assert_eq!(admit(&long, 0, 10, U, true), Err(Refusal::TooLong(MAX_SQL + 1))); + assert_eq!(admit(&long[..MAX_SQL], 0, 10, U, true).map(str::len), Ok(MAX_SQL)); // Layer 2 reads text: a byte that is not UTF-8 (hidden in a comment) is refused here. - assert_eq!(admit(b"SELECT LOAD_FILE('/etc/passwd') /* \xff */", 0, 10, U), Err(Refusal::NotUtf8)); - assert_eq!(admit("SELECT 'é'".as_bytes(), 0, 10, U), Ok("SELECT 'é'")); + assert_eq!(admit(b"SELECT LOAD_FILE('/etc/passwd') /* \xff */", 0, 10, U, true), Err(Refusal::NotUtf8)); + assert_eq!(admit("SELECT 'é'".as_bytes(), 0, 10, U, true), Ok("SELECT 'é'")); } const U: &str = "utf8mb4"; @@ -227,7 +243,7 @@ mod tests { "SET STATEMENT MAX_STATEMENT_TIME=100 FOR SELECT SLEEP(100)", "SELECT @@max_statement_time", ] { - assert_eq!(admit(sql.as_bytes(), 0, 10, U), Err(Refusal::TimeCap), "{sql}"); + assert_eq!(admit(sql.as_bytes(), 0, 10, U, true), Err(Refusal::TimeCap), "{sql}"); } } @@ -242,7 +258,7 @@ mod tests { "SET SQL_BIG_SELECTS=1", "set session sql_big_selects = ON", ] { - assert!(admit(sql.as_bytes(), 0, 10, U).is_ok(), "{sql}"); + assert!(admit(sql.as_bytes(), 0, 10, U, true).is_ok(), "{sql}"); } for sql in [ "SET SESSION completion_type = 'CHAIN'", @@ -269,13 +285,29 @@ mod tests { "commit work release", "ROLLBACK /* x */ RELEASE", ] { - assert_eq!(admit(sql.as_bytes(), 0, 10, U), Err(Refusal::SessionState), "{sql}"); + assert_eq!(admit(sql.as_bytes(), 0, 10, U, true), Err(Refusal::SessionState), "{sql}"); } for sql in ["COMMIT", "ROLLBACK AND NO CHAIN NO RELEASE", "ROLLBACK TO SAVEPOINT a", "RELEASE SAVEPOINT a", "SELECT 'SET x = 1'"] { - assert!(admit(sql.as_bytes(), 0, 10, U).is_ok(), "{sql}"); + assert!(admit(sql.as_bytes(), 0, 10, U, true).is_ok(), "{sql}"); } // No charset known: no SET NAMES either. - assert_eq!(admit(b"SET NAMES utf8mb4", 0, 10, ""), Err(Refusal::SessionState)); + assert_eq!(admit(b"SET NAMES utf8mb4", 0, 10, "", true), Err(Refusal::SessionState)); + } + + #[test] + fn the_gateway_is_mariadb_only_for_now() { + for s in ["11.4.4-MariaDB-ubu2404", "5.5.5-10.11.6-MariaDB-0+deb12u1", "10.6.18-mariadb-log"] { + assert!(is_mariadb(s), "{s}"); + } + for s in ["8.0.36", "8.4.0-0ubuntu0.24.04.1", "5.7.44-log", "8.0.32-24 Percona Server", "5.7.12-TiDB-v7.5.0", ""] { + assert!(!is_mariadb(s), "{s}"); + } + // On anything else every query is refused, with the one reason (logged once a request). + for sql in ["SELECT 1", "START TRANSACTION", "UPDATE wp_x SET a = 1"] { + assert_eq!(admit(sql.as_bytes(), 0, 10, U, false), Err(Refusal::NotMariaDb), "{sql}"); + } + assert_eq!(Refusal::NotMariaDb.reason(), "the database gateway supports MariaDB only for now (#99)"); + assert!(Refusal::NotMariaDb.once_per_request()); } #[test] diff --git a/ext/php/src/layer3.rs b/ext/php/src/layer3.rs index d67da6d..38a1a5f 100644 --- a/ext/php/src/layer3.rs +++ b/ext/php/src/layer3.rs @@ -212,6 +212,9 @@ struct Channel { cap_logged: bool, /// The worker connection's charset (the only one `SET NAMES` may name, [`gateway::admit`]). charset: String, + /// The worker's server is MariaDB, read once per request from the hello's facts + /// ([`gateway::is_mariadb`]); otherwise the gateway refuses every query. + mariadb: bool, } #[derive(Debug, Clone, Copy)] @@ -387,6 +390,7 @@ pub fn securecore_layer3_open(slug: String, file: String, abspath: String, prefi queries: 0, cap_logged: false, charset: hello.db.charset.clone(), + mariadb: gateway::is_mariadb(&hello.db.server_info), }; let slot = channels.entry(slug.clone()).or_insert(Some(channel)); pump(&slug, slot, Some(first)) @@ -511,7 +515,7 @@ fn pump(slug: &str, slot: &mut Option, mut first: Option>) -> Z } }; if let Some(sql) = gateway::decode_query(&frame) { - match gateway::admit(sql, ch.queries, cap, &ch.charset) { + match gateway::admit(sql, ch.queries, cap, &ch.charset, ch.mariadb) { Ok(text) => { ch.queries += 1; ch.pending = Some(text.to_owned());