//nefariousplan

Refund Breaks The Invariant

A state variable maintains its invariant on the spend path. The refund path writes it without enforcing that invariant, and a downstream validator's arithmetic that presumed the invariant wraps.

A process maintains a bounded state variable whose value a downstream consumer reads through arithmetic that treats the bound as a precondition, not a thing to verify. Subtraction against a maximum. Indexing by ceiling - counter. A credit delta computed as allowed - used and then reused by further arithmetic, pointer math, or a bounds check whose correctness argument is one sentence: the counter never leaves the admitted range.

Multiple writers reach the variable. One is the primary accounting path. It debits the variable by what it is committing to spend, and the debit is the act of reserving capacity. The writer cannot post a value the ceiling disallows because its own logic is the ceiling. The other writer is a refund, a cleanup, a release. It restores an amount a prior spend reserved against the ceiling, and its local correctness argument is that adding the amount back cannot push the variable above a value the ceiling has already approved.

The refund's argument is only sound when the primary writer's commit actually ran. Attackers reach the refund along a cleanup route that fires without the spend ever having completed: a stream reset mid-queue, a connection aborted before the frame flushed, a transaction rolled back after the ledger entry was reserved but before it was posted. The refund restores a reservation the ceiling never saw. The counter lands above its documented maximum. The consumer's arithmetic wraps.

Mechanism

Three things have to be true for the pattern to exist.

First, a state variable whose value a downstream consumer reads through arithmetic that presumes an invariant. The reader does not check the invariant; it treats the invariant as a type-level property of the variable. In NGINX CVE-2026-123007, ngx_http_v2_state_window_update computes the admitted delta as NGX_HTTP_V2_MAX_WINDOW - h2c->send_window, and the subtraction is only well-defined while send_window stays at or below NGX_HTTP_V2_MAX_WINDOW. The result is reused by further arithmetic, interpreted later as an (off_t) byte count, and ultimately passed as a length into a vectorised memcpy. The arithmetic's result is correct exactly on the range the invariant describes, and silently wrong on any input the invariant forbids.

Second, a primary writer whose debit logic holds the invariant. The writer deducts what it is about to commit from the shared variable, and the deduction happens inside the path that commits the spend. From the primary writer's local correctness argument, the invariant is unviolable: it reserves what it is about to use, and the ceiling is what gates the reservation. In the NGINX exhibit, ngx_http_v2_send_chain reserves credit before the frame leaves the queue. The author of that function does not install a ceiling check after the deduction because the deduction is the ceiling. A reviewer who audits only ngx_http_v2_send_chain concludes the counter cannot exceed the maximum.

Third, a refund writer whose add-back logic depends on the primary writer having actually committed. The refund exists because the primary writer may fail to flush: the connection may die, the stream may reset, the operation may be cancelled before the reserved capacity is consumed. Its purpose is to return reservations to the pool. Its local correctness argument is that the amount it is restoring was reserved by a prior writer who respected the ceiling, so adding the amount back cannot exceed a value the ceiling has already approved. The argument is sound exactly when the prior commit happened. The argument has no defence against the case where the refund reaches the variable along a cleanup route the primary writer never traversed with its matching debit.

Attackers deliver that case. In the NGINX exhibit, the operator sends a SETTINGS_INITIAL_WINDOW_SIZE the protocol disallows; the connection resets; ngx_http_v2_filter_cleanup walks every queued DATA frame and refunds frame_size back onto send_window for each one; the flushes never happened. The counter climbs past NGX_HTTP_V2_MAX_WINDOW. The validator's subtraction underflows into an enormous positive delta; a tailored final WINDOW_UPDATE drives the field across the signed midpoint; ngx_http_v2_send_chain reads it as (off_t) and passes a negative limit into glibc's AVX memcpy.

The pattern repeats across any accounting ledger with reservable capacity: token buckets, flow-control windows, connection credits, pool budgets, transaction locks. The structural condition is that one counter carries two meanings the code never separated. The counter the consumer reads is "capacity already granted to the peer," which has a ceiling. The counter the refund writes is "pending reservations that may still return to the pool," which does not have the same ceiling because a reservation that was never spent is not capacity granted to anybody. One variable is being asked to serve both accounting roles. The primary writer stays correct because its role is the consumer's role. The refund writer stays correct against its own role but writes to the consumer's variable.

The fix is not a predicate at the refund site. A check at the refund that rejects writes which would push the counter past the ceiling closes the demonstrated chain and leaves the pattern in place: the counter now rejects legitimate refunds whenever an attacker has already engineered the state near the ceiling, converting the write-path bug into a liveness bug. The structural fix is a ledger column. A second variable accumulates pending reservations without needing to respect the ceiling the consumer reads, and a reconciliation step moves the amount from the refund ledger to the consumer ledger only along the code path that would have committed the primary writer's spend. The NGINX patch adds send_queued for exactly this purpose: the refund writes to send_queued, the validator reads send_window, and the two are reconciled by the code path that would have debited the spend. The audit-readable scope of the patch is one new field. The pattern it closes is the collapse of two accounting roles into one counter.

What distinguishes the class from its neighbours is where the enforcement disagreement lives. No validator function ran once at startup while the runtime rewrote the state behind it (Validated At Boot); there is no validator in the usual sense, and the state is not configuration. No predicate was written narrower than the dangerous range (Sign Bit Is Not Overflow, Limit Check As Equality); no predicate was written at the write site at all, because the write site's local reasoning precluded needing one. No two implementations of the same contract diverged on whether to import a shared check (Parallel Implementation Gap); the refund path and the primary path are complementary, not parallel, and no shared check exists to be imported. The discipline this pattern asks a reviewer to apply is to recognise, at any accounting site with reservable capacity, that the counter which the consumer reads and the counter which refunds write back to may be the same variable by accident.

Exhibits

CVE-2026-123007: NGINX's HTTP/2 Validator Subtracted From a Counter That Could Legally Exceed Its Maximum. The Fix Is the Ledger Column That Would Have Kept It Legal.. ngx_http_v2_state_window_update reads h2c->send_window through NGX_HTTP_V2_MAX_WINDOW - h2c->send_window. That subtraction is only safe while send_window stays at or below NGX_HTTP_V2_MAX_WINDOW. Three code paths write that field. Two of them hold the ceiling: ngx_http_v2_send_chain reserves credit before the frame leaves the queue, and the wire send does not touch the counter at all. The third path, ngx_http_v2_filter_cleanup, refunds the reserved frame_size back onto send_window when a stream dies before its queued DATA flushes. The refund does not check the ceiling, because at the spend site the ceiling could not be violated. The exploit reaches that third write deliberately, by queuing DATA and then forcing a stream reset through an illegal SETTINGS_INITIAL_WINDOW_SIZE. Once the refund pushes send_window above the maximum, the validator's subtraction underflows into an enormous allowed delta, and a tailored final WINDOW_UPDATE drives the field across the signed midpoint. ngx_http_v2_send_chain then reads the field as (off_t) and passes a negative limit into glibc's AVX memcpy. The patch does not harden the subtraction. It adds send_queued so the refund writes to a counter that does not have to respect the protocol maximum, and the validator reads the one that does.

Boundaries

Not Validated At Boot. That pattern names an explicit validator function that runs once at a lifecycle hook and a runtime mutation path that rewrites the validated state without re-invoking the function. The validator exists in the source, correctly implemented, invoked from exactly one site, and the fix is a helper every writer calls. Refund Breaks The Invariant has no validator in the usual sense. The invariant is held by the primary writer's own debit logic, as an emergent property of the writer reserving what it is about to spend. There is no function to relocate. The fix is not to call a validator from the refund path; the fix is to recognise that one counter is being asked to carry two accounting roles and allocate a second variable for the refund's role.

Not Sign Bit Is Not Overflow. That pattern names a predicate the author wrote to catch arithmetic overflow and installed as a comparison against zero, capturing one of overflow's two visible signatures while remaining silent on the other. The predicate exists. Refund Breaks The Invariant has no predicate at the write site at all, by design: the primary writer's local correctness argument precluded one, and the refund writer inherited that argument. If the fix is to widen the accumulator and compare against the contracted ceiling, the class is Sign Bit Is Not Overflow. If the fix is to add a second variable so the refund writes to a counter that does not have to respect the consumer's ceiling, the class is this one.

Not Limit Check As Equality. That pattern names a bounded counter checked against its ceiling with an equality (count == max - K) rather than an inequality (count >= max); the counter is monotonic, the comparator fires at exactly one value, and the fix is the inequality the equality was substituting for. Refund Breaks The Invariant lives at a different site: the counter is not monotonic (refunds move it up, spends move it down), the comparator does not exist at the write at all, and the fix does not rewrite a predicate. If the audit question is "which comparator shape is wrong," the class is Limit Check As Equality. If the audit question is "which writer's local reasoning disclaims the need for any comparator," the class is this one.

Not Parallel Implementation Gap. That pattern names two implementations of the same client-facing contract, where one imports a shared security check from a utility module and the other was written separately against the same contract and never imported it. The two surfaces are substitutable from the caller's perspective, and the pattern is a gap in which gated surface the client happens to hit. Refund Breaks The Invariant names a pair of writers that are complementary, not parallel. Spend is not refund; the two paths participate together in one accounting ledger, and no shared check exists for the refund to fail to import. The audit question is not "which surface did the caller reach" but "which cleanup route reached the refund without the matching spend."

Not every bounded-counter bug is this pattern. A counter whose writer forgot to check the bound at all, where no local correctness argument precluded the check and no sibling writer held the invariant implicitly, is an ordinary missing-check bug; the fix is to add the check and the pattern is absent. Refund Breaks The Invariant requires that the write site's local reasoning precluded the need for a predicate (the writer is restoring an amount a prior spend reserved under the ceiling), that the reasoning was sound against the lifecycle the writer was authored for, and that a cleanup route reaches the writer without the matching spend having committed. If removing the invariant assumption from the writer's reasoning is the first thing the fix does, the pattern is present. If the fix is to add a check the author simply forgot, the class is a plain missing-bound bug.

Defender playbook

Enumerate every writer that touches a bounded state variable, and classify each by its local correctness argument. A writer whose argument is "I am deducting the amount I am about to use, so the deduction cannot exceed the ceiling the deduction enforces" is a primary accounting writer. A writer whose argument is "I am restoring an amount a prior writer already reserved under the ceiling, so my add-back cannot exceed a value the ceiling already approved" is a refund writer. For each refund writer, enumerate the cleanup routes that can reach it: reset paths, abort paths, connection teardown, session rollback, stream cancel, transaction retry. For each route, ask whether the primary writer's commit always ran before the refund restores the reservation. Any route for which the answer is no is the audit candidate, and the NGINX exhibit's cleanup walk over queued-but-unflushed DATA frames is the canonical shape.

Audit the consumers that read the state variable for arithmetic that presumes the invariant. Grep the variable for subtraction against a maximum (MAX - x), indexing into containers whose size is derived from the variable (buf[ceiling - x]), delta calculations that treat ceiling - variable as non-negative, casts from unsigned to signed that presume the high bit is clear. Any read site whose correctness argument begins "the variable is at most the ceiling" is a sink. If the sink's result is reused by further arithmetic, a memcpy length, a pointer derivation, or a bounds check, the primitive is a bounded or unbounded write. If the sink's result is passed to the wire, the primitive is an information leak. If the sink's result is converted to a signed type, the primitive is attacker control over the signed domain, which is what the NGINX exhibit uses to drive a negative length into glibc's AVX memcpy.

Fix with a ledger column, not a predicate. The refund site's reasoning is sound against the role it was written to fill: restoring the amount a cancelled reservation owes back to the pool. The error is that the pool and the capacity ledger share one variable. Allocate a second variable for the refund's role (send_queued in the NGINX patch, pending reservations in a token bucket, undisbursed credit in a flow-control window, escrowed capacity in a transaction cache), and reconcile it with the consumer's variable only along the code path that would have committed the primary writer's spend. A predicate at the refund site that rejects writes exceeding the ceiling closes this exhibit and leaves the next cleanup route, written by a different author against a different lifecycle event, to reproduce the pattern on the next variable the project grows that carries two accounting roles in one name.

Build the regression test from the cleanup path, not the spend path. The witness is a session in which a reservation is made and then a cleanup route executes without the commit ever running. For the NGINX exhibit, queue a DATA frame that reserves frame_size on send_window, force the stream reset through an illegal SETTINGS_INITIAL_WINDOW_SIZE, issue a WINDOW_UPDATE that tips send_window across the signed midpoint, and then send any payload that exercises ngx_http_v2_send_chain. The test asserts the subtraction never underflows, or equivalently that the refund path never produced a send_window value above NGX_HTTP_V2_MAX_WINDOW. A test suite that only exercises matched reserve-and-spend pairs catches no regressions here; the invariant breaks exactly on the mismatched case the suite was not written to cover.

Kinship

Validated At Boot. Cousin at the check-is-at-one-site layer. Validated At Boot names an explicit validator function that fires once at a lifecycle hook and does not fire against the runtime path that rewrites the state; the fix lifts the validator out of the lifecycle hook into a helper every writer calls before publishing. Refund Breaks The Invariant has no standalone validator at all. The invariant is held by the primary writer's own debit logic, and the refund writer inherited the invariant as a consequence of its local correctness argument. The two patterns share the shape that a check at one site does not cover the state at another, and they separate on whether the check is an explicit function (validated-at-boot) or an implicit property of a writer's accounting logic (refund-breaks-the-invariant). An audit under either lens asks the same first question: which sites write this state, and which of them run the invariant through enforcement.

Sign Bit Is Not Overflow. Sibling at the consumer-arithmetic layer. Both patterns end in a consumer whose arithmetic wraps on attacker-reachable input, and both repeat across any codebase that mixes signed accumulators with attacker-controlled numeric state. Sign Bit Is Not Overflow is about a predicate the author wrote to catch the wrap and installed as a comparison against zero, capturing one of overflow's two signatures. Refund Breaks The Invariant is about no predicate at the write at all, by design, because the writer's local reasoning precluded needing one; the wrap happens because a sibling writer violated the invariant the consumer's arithmetic presumed. A reviewer who sees a wrap at a consumer should audit upstream for either a narrow predicate (sign-bit) or a sibling writer whose logic broke the invariant the consumer presumed (refund-breaks). Both fixes can land in the same file and the two are different diffs: type widening in one, a second ledger variable in the other.

The counter the consumer read was the capacity granted to the peer; the counter the refund wrote to was the reservations the pool might still get back; the code called them both send_window.