# CVE-2026-118383 — MongoDB pre-auth use-after-free in primary catch-up | | | |---|---| | **CVE** | CVE-2026-118383 | | **Component** | MongoDB server, replication (`ReplicationCoordinatorImpl`) | | **Affected** | 8.0.x before 8.0.37 (8.0.34 tested); 8.2 builds before the backport (8.2.12 tested) | | **Fixed in** | 8.0.37 (`23dfc54219`, `6111165c32`) | | **CWE** | CWE-416 — use-after-free | | **Trigger** | Unauthenticated `readConcern.afterOpTime` waiter poisoning + primary catch-up abort | | **Impact** | mongod crash / memory corruption in the replication coordinator | A `ReplicationCoordinatorImpl::CatchupState` object can free itself while one of its own methods is still running. The completion callback registered for the primary catch-up waiter captures a raw `this`, ignores the completion status, and calls `abort_inlock(kSucceeded)` from inside `abort_inlock()`. The inner call resets `_repl->_catchupState` and destroys the object; the outer frame then continues to read and write through the freed pointer. The waiter can be kept unfulfilled by an unauthenticated client. Two `readConcern.afterOpTime` waiters with adversarial opTime terms create an ordering cycle in `std::multimap`, so the real catch-up waiter is never reached by `setValueIf_inlock()`. The next heartbeat concludes "already caught up" and aborts catch-up, which destroys the unfulfilled waiter and re-enters `abort_inlock()`. Two server fixes shipped in 8.0.37: - `23dfc542199ab3b88d6a9420b6cd3f125a897620` — SERVER-138175. `CatchupState` becomes `enable_shared_from_this`, `_catchupState` becomes a `shared_ptr`, and the callback holds a `weak_ptr`, returns on non-OK status, and only concludes catch-up if it still owns the current state. - `6111165c32bfc74f197e2dfeb9480d0b52154d68` — SERVER-138119. Rejects client-supplied `readConcern.afterOpTime` unless the client holds `ActionType::internal`. ## Quick start ```bash # Vulnerable 8.0.34: deterministic double abort (UAF fingerprint) ./run.sh assisted # Fixed 8.0.37: same attack, no double abort MONGO_IMAGE=docker.io/mongodb/mongodb-community-server:8.0.37-ubi9 \ ./run.sh assisted # Unauthenticated afterOpTime acceptance, vulnerable vs fixed AUTH=1 ./run.sh control AUTH=1 MONGO_IMAGE=docker.io/mongodb/mongodb-community-server:8.0.37-ubi9 \ ./run.sh control ``` `run.sh ` tears down any previous replica set, starts three members, runs the PoC, captures the report and server logs under `results/`, and tears the set down again. A non-zero exit means the run did not reach an expected verdict. Use `./run.sh up` and `./run.sh poc ` to keep a set running for inspection. Requirements: Podman, `mongosh` (only used inside the MongoDB images), and Python 3.10+ if `poc.py` is run directly. The PoC itself has no third-party dependencies; `mongo_wire.py` implements BSON and OP_MSG from the standard library. ## Root cause Vulnerable code in `src/mongo/db/repl/replication_coordinator_impl.cpp`: ```cpp void ReplicationCoordinatorImpl::CatchupState::abort_inlock( PrimaryCatchUpConclusionReason reason) { ... if (reason != PrimaryCatchUpConclusionReason::kSucceeded && _waiter) { _repl->_lastAppliedOpTimeWaiterList.remove_inlock(_waiter); _waiter.reset(); // may synchronously run targetOpTimeCB } _repl->_enterDrainMode_inlock(); _repl->_catchupState.reset(); // destroys `this` } auto targetOpTimeCB = [this](Status status) { // status is ignored const auto myLastApplied = _repl->_getMyLastAppliedOpTime_inlock(); if (_targetOpTime <= myLastApplied) { ... abort_inlock(PrimaryCatchUpConclusionReason::kSucceeded); // re-enters } }; ``` Three conditions make the re-entry fatal: 1. The node is in primary catch-up with a registered `_waiter`. 2. The waiter is still unfulfilled even though `lastApplied >= _targetOpTime`. 3. Any abort with a reason other than `kSucceeded` destroys the waiter. The synchronous callback observes that the target is already applied and calls `abort_inlock(kSucceeded)` again. The second condition is reachable because `OpTime::operator<` is not a strict weak ordering when either side carries the legacy/uninitialized term `-1`: ```cpp inline bool operator<(const OpTime& rhs) const { if (_term == kUninitializedTerm || rhs._term == kUninitializedTerm) return _timestamp < rhs._timestamp; // timestamp only return std::tie(_term, _timestamp) < std::tie(rhs._term, rhs._timestamp); // term first } ``` Plant two waiters in this order: - `W1 = {ts: applied + 1s, term: -1}` — term omitted on the wire - `W2 = {ts: Timestamp(1, 1), term: 2^40}` With `W1` inserted first, the multimap order becomes `W2, W1, C` (where `C` is the catch-up waiter): `W2 < W1` by timestamp, `W1 < C` by timestamp, and `C < W2` by term. Iteration in `setValueIf_inlock()` starts at `W2`, whose key compares greater than any real lastApplied, so the loop stops before `C`. The poisoned ordering survives until the next heartbeat aborts catch-up. ## Attack surface `readConcern` is a generic command argument, and `waitForReadConcern` runs before command dispatch. The deprecated `afterOpTime` field was accepted from any connection, including unauthenticated clients and commands that require no privileges (`ping`, `hello`). The fixed jstest `jstests/auth/after_op_time_requires_internal.js` checks exactly that case. The waiter-poisoning primitive needs no credentials. The reproduction also drives the election that turns the poison into catch-up state; on an auth-enabled deployment that step requires replica-set control. The security boundary crossed by SERVER-138119 is that any connection could register waiters on a secondary's last-applied list. ### Why the control needs authentication `checkAuthForAfterOpTime()` asks for `ActionType::internal` on the cluster. When `security.authorization` is disabled, that action is effectively granted to every client, so even 8.0.37 accepts `afterOpTime` without a keyFile. The auth fix is only observable on an auth-enabled deployment. `AUTH=1` starts every member with a shared keyFile, bootstraps an admin user through the localhost exception, and leaves the attacker connection unauthenticated. `AUTH=1` supports `control` only. `assisted` and `natural` need the unauthenticated test commands used to hold the applier and are run with auth disabled. ## PoC modes - **assisted** — deterministic. Enables the test-only failpoint `pauseBatchApplicationAfterWritingOplogEntries` on n2, so its oplog is current (it can win votes) while `lastApplied` lags (so primary catch-up registers a waiter). Both poison waiters are planted through unauthenticated `ping` commands with `readConcern.afterOpTime`. This isolates the UAF logic from timing noise. - **natural** — no failpoint. Plants the poison waiters asynchronously, drives a multi-threaded write burst against the primary, and forces the election while n2 is behind. Timing-sensitive: on a fast disk n2 can catch up before the post-election heartbeat fixes the target. It has produced `DOUBLE_ABORT_UAF` with `N2_CPUS=0.25`, six writers, and an 8-second backlog, but `assisted` is the reliable mode. - **control** — one unauthenticated `ping` with `readConcern.afterOpTime` and a short `maxTimeMS`. Vulnerable builds accept the read concern and the command waits (code 50, timeout). Fixed builds with auth reject it immediately with code 13 (`Unauthorized`). ## Verified results | Target | Mode | Verdict | |---|---|---| | `mongo:8.0.34` | assisted | `DOUBLE_ABORT_UAF` | | `mongo:8.0.34` | gdb oracle | `UAF_CRASH` — SIGSEGV at `abort_inlock+0x1a0` | | `mongo:8.0.34` | control, `AUTH=1` | `VULNERABLE_ACCEPTS_UNAUTH_AFTEROPTIME` — code 50 after ~1 s | | `mongodb-community-server:8.0.37-ubi9` | assisted | `POISON_HELD_BUT_NO_DOUBLE_ABORT` | | `mongodb-community-server:8.0.37-ubi9` | control, `AUTH=1` | `FIXED_REJECTS_UNAUTH_AFTEROPTIME` — code 13, immediate | | `mongo:8.2.12` | assisted | `DOUBLE_ABORT_UAF` | The vulnerable double abort has a portable fingerprint: - `numCatchUpsSucceeded` **+1** and `numCatchUpsAlreadyCaughtUp` **+1** in one resume; - two `Exited primary catch-up mode` lines with the same timestamp. The fixed build shows only `numCatchUpsAlreadyCaughtUp +1` and a single exit line. Raw reports and matching server-log excerpts are in [`evidence/`](evidence/). ## Deterministic UAF oracle The release build often survives the UAF silently because the freed allocation is not reused immediately. `tools/uaf_run.sh` turns the double abort into a visible fault: 1. Runs `assisted` and stops it at a sync point right before the applier is resumed, while the poisoned catch-up waiter is live. 2. Starts a gdb sidecar in n2's PID namespace with `SYS_PTRACE`. 3. Breaks at `CatchupState::abort_inlock+0x1a0`, the instruction that reloads `this->_repl` after `_waiter.reset()`. 4. The first hit is the inner `kSucceeded` abort. On the second hit the object has already been freed; gdb makes its page `PROT_NONE` and single-steps the load, producing a SIGSEGV with `signalHeartbeatUpdate_inlock ← _handleHeartbeatResponse` below it. ```bash podman build -t localhost/cve118383-debug:latest \ -f containers/Debug.containerfile . mkdir -p build podman run --rm --entrypoint cat docker.io/library/mongo:8.0.34 \ /usr/bin/mongod > build/mongod-8.0.34 chmod +x build/mongod-8.0.34 ./tools/uaf_run.sh ``` The driver exits 0 only after `ORACLE: SIGSEGV at pc=...`. Set `MONGO_BIN` to use a different mongod binary. ## Repository layout ``` poc.py assisted / natural / control PoC mongo_wire.py dependency-free BSON + OP_MSG client run.sh replica-set orchestration and mode dispatch containers/Attacker.containerfile attacker client image containers/Debug.containerfile gdb image for the oracle tools/uaf_run.sh deterministic oracle driver tools/uaf_oracle.gdb breakpoint, mprotect, single-step oracle evidence/ archived reports and key server-log lines ``` ## Notes - `assisted` uses `enableTestCommands=1` and a test-only failpoint to hold the applier. A remote attacker cannot enable it; it removes timing noise while proving the UAF path. The poison waiters and the election trigger are real wire-protocol operations. - `natural` is a realistic but racy demonstration. Use it to observe the attack without failpoints, not as the primary evidence. - The official fixed image is `docker.io/mongodb/mongodb-community-server:8.0.37-ubi9`. Docker Hub `library/mongo` has no 8.0.37 tag. - 8.2.12 was the newest 8.2 image published at test time and still reproduced the double abort; the hotfix had not been backported to that tag. Binary-level UAF validation was performed on 8.0.34. ## Standalone exploit `python3 exploit/exploit.py` (exit 1 exploited, 0 safe).