Repository navigation
Layer 3: the database gateway for isolated plugins (#94) - #97
Conversation
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.
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.
- 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.
…ounds the worker's memory; strip only the database name from errors
Review fixesCommits e79572d and 22e43c0. Merges only; 3aa4b88 is untouched.
Revert run. The new Gates (on [xps], under the lock):
Cost. One query now costs ~325 µs isolated against ~153 µs unisolated: about +170 µs. Behaviour change: drop-ins. Gateway queries no longer go through -claude |
…ver mirrored, a session SET allowlist, ROLLBACK AND NO CHAIN, the MySQL rollback, a per-row result cost, cleanup in finally
…neighbour's LIMIT check reads its own table
Verification fixes (round 2)Commits ff3da44 and 2ad8d66 (the second is a test-only fix), on top of 22e43c0. Merges only.
Revert run: the new Gates on [xps], under the lock:
Cost: about +127 µs per query (288 µs isolated, 160 µs unisolated). -claude |
# Conflicts: # test/ext-layer3.sh
…e memory scans of the child and the worker)
|
After merging main into the branch (3f426c2, d6bd2d1). #88's
Two new checks are n/a on the microVM target: the memory scans of the child and the worker, because the scan reads CI after one check: the ext cell passed, guest reproducibility passed, and the microVM job was pending. -claude |
… test leg exists (#99)
|
Commit 2ce853f: the gateway refuses on MySQL for now. The worker reads
Why: on MySQL, Tests: a new unit test, Runs:
-claude |
An isolated plugin's
$wpdbnow works: in its child,$wpdbis a gateway to the php-fpm worker. The worker runs each query on its real connection, under layer 2's own check, charged to the plugin. Two commits:closes #94 (part of #53)
Design
What crosses the wire. The existing request socket and frames carry it:
Q\n<sql>frame (ext/layer3/src/gateway.rs). The child sends it in place of the answer the worker is waiting for, either to the plugin's load or to a call.ret,error(becomeslast_error);keys+rows(rows as value lists);cols(column names);num_rows;affected(for a write);insert_id(for an INSERT/REPLACE).$wpdbfacts: charset, collate,db_version()anddb_server_info(). It carries no host, user, password or database name.Where layer 2's checks run. Layer 2 runs in the worker, through the same functions as for a plugin that isn't isolated:
gateway::admit):dbQueriescap;max_statement_timeormax_execution_time.securecore_layer3_db_begin) and runs$wpdb->query($sql)on the real connection.attribution::db_callertreats the stand-in's own frames asplugin:<slug>(layer3::gateway_component). Those frames are compiled undersecurecore-layer3:<slug>, a name only the extension uses.db::checkgives the plugin the same verdict and writes the same line as for that plugin's own$wpdb->query().The child never picks the attribution. The slug is the key of the channel the worker opened; nothing in a query frame names a plugin. Other plugins'
queryfilters run as they always do, and any SQL they run is charged to them, not to the isolated plugin.The child side.
helper.php's connectionless$wpdboverridesquery(): it applies the plugin's ownqueryfilters, then goes through the gateway. Everything else is wpdb's own code on top of that:get_results,get_row,get_var,get_col;insert,update,delete,replace;prepare,dbDelta,get_charset_collate, transients._real_escapeescapes byte for byte asmysqli_real_escape_stringdoes on utf8mb4, so the worker gets the same text the plugin would send unisolated.Caps (
layer3.limits, added to the schema):dbQueriesdbResultBytestimeoutMsFor time, the stand-in sets
SET SESSION max_statement_time(MariaDB) ormax_execution_time(MySQL) to the call's remaining time, once per call that queries. Afterwards it restores the session's own value, which it reads once per request. A child past its deadline is killed as before.Transactions. If a child dies with a transaction open (START/BEGIN/
autocommit=0), the worker rolls it back and logs a line. Without that, core's later writes in the request would join the open transaction and be lost at disconnect.Layer 2 bug fixed (commit 1)
db::checkread the SQL argument withZval::str(), which gives up when the text isn't valid UTF-8. On that path the handler ran the query unchecked. So one\xffbyte, even inside a comment, skipped layer 2 on every route for a non-isolated plugin.Proven on main's extension:
ran (
not-utf8-direct is BLOCKED (got 'OK')). It now reads the text lossily: an invalid byte is never ASCII, so the statement's structure is what the server sees. The gateway refuses text that isn't UTF-8 outright.Evidence
All on [xps], project
scl3db; the microVM run used projectscl3dbmvm.docker build --target check(clippy-D warnings+cargo test, whole workspace)test/ext-config.shtest/ext-layer3.shtest/ext-attribution.sh,ext-enforce,ext-vault,ext-db,ext-hosts,ext-layer1test/ext-microvm.sh(KVM, jailed Firecracker guest, kernel frombench/microvm/kernel.sh)scripts/ext-build.sh 8.3 ntsscripts/guest-build.sh,test/release-check.sh 8.3 nts,test/promote-check.shClippy line, from the
checktarget:clippy --workspace --all-targets -D warnings, clean (rc=0).What ext-layer3 now proves (new fixture
test/ext/layer3-plugin/sc-l3db.php, isolated):dbDelta, then OKs forinsert,get_row(quotes, a backslash, a newline and a NUL round-trip),get_row(ARRAY_A),update(rows_affected),replace,insert_id,get_var,get_col,get_results(+OBJECT_K,num_rows),get_col_info('name'), a rolled-back transaction,delete, a bad query'slast_error, and transients.blognamefrom the snapshot.LOAD_FILE) and another plugin's table are each refused, withlast_errorfrom layer 2. The lines are identical to the unisolated run.dbQueries 60(the load used one), logged once;dbResultBytes 65536;SELECT SLEEP(5)is cut at 1.59 s (timeoutMs 1500) and no longer runs on the server.update_optionin the same request commits.getenv(),$_ENVand/proc/<pid>/environare empty, andDB_PASSWORDis undefined;test/ext/layer3-memscan.php), because a layer 3 child isn't dumpable after its uid change.Revert proofs
scl3dbmain; the new cap keys are dropped from the config, because main's parser rejects them): 46 FAIL. Every gateway case isBLOCKED, the layer 2 lines don't appear, no cap trips, the sleep finishes, andnot-utf8-directruns (the layer 2 bug). Sample:Cost per query
[sc_l3db_time]runs a one-rowget_row(prepare(…))50 times, measured in the plugin, and takes the median of 7 pages. Isolated it costs ~316 µs. In a quiet run the same query cost ~155 µs unisolated, so the gateway adds about +160 µs per query. The unisolated figure swung to 551 µs in a second run while other builds shared the box, so the delta is rough. A call that queries also adds twoSET SESSIONstatements. It isn't tuned yet: column names are fetched for every SELECT, and rows cross PHP-serialized.Covered / not covered
Covered:
$wpdbmethods above,prefixand the table properties,dbDelta, transactions;Not covered (in
ext/README.md"Not yet"):sql_modehasNO_BACKSLASH_ESCAPESwould differ.queryfilter doesn't see its own queries in the worker: a re-entered call passes through, with a line.get_option()of a core option missing from the snapshot) is charged to the plugin, not to core, because the child can't prove which of its frames are core's. Unisolated, the same call would be core's.securecore_layer3_db_replyand its siblings are PHP functions, so a non-isolated plugin in the worker could forge an answer to an isolated child. It can't run SQL as that plugin: the stand-in only runs text the child sent.Out of scope, still in #53's "not yet" list: request state, objects across the socket, salted pluggables and mail, outbound HTTP.
-claude