NGINX / Docker / C

We were getting NGINX running again on localhost around the same time we needed to update it in production. I wanted to see how the rewrite bug worked, so I wrote a small C example.

In Inside PHP's 2026 Memory Bugs, I looked at what happens inside PHP when it's handling a request. This time the problem starts one step earlier. NGINX is putting together a new URL, and under certain conditions the result takes up more space than it set aside. That's the bug known as NGINX Rift (CVE-2026-42945).

Which release fixed Rift?

F5 disclosed Rift in May 2026. It's a heap buffer overflow in ngx_http_rewrite_module. By the time we updated in November, NGINX had released more security fixes, including ones for HTTP/2 and its DNS resolver. We went straight to the November stable release.

Release pointStableMainline
First upstream Open Source releases fixing Rift, May 20261.30.11.31.0
Upstream targets after the November 18 updates, checked November 211.30.71.31.9

The NGINX security advisories list the fixes by branch, and F5's Rift advisory covers affected products and workarounds. If you installed NGINX through a Linux distribution, check that distribution's security tracker too. It may ship the fix under an older upstream version number.

Source links: Rift and the ten November CVEs

May: Rift. See CVE-2026-42945 and the F5 advisory. The November 18 release note lists the ten additional fixes below. The affected versions vary by issue.

For fixed and affected ranges, check the upstream security index; each entry also links to its F5 advisory. The NGINX 1.30.7 release records the stable update we deployed.

What happens before PHP sees the request?

Say a browser asks for /shop/42, and the app expects /store?item=42. A rewrite can make that change inside NGINX, before PHP sees the request. The NGINX worker builds the new address in memory and uses it to continue handling the request.

A browser request goes to an NGINX worker, which builds the rewritten address in memory before forwarding to the app.
Figure 1. The NGINX worker rewrites the address before it passes the request to the app.
Watch: the request moves through NGINX (7-second loop)
Animated paper tiles travel from the browser through NGINX and then to the app server.
Each tile in the Cairo animation represents a request.

The master process loads the config and manages the workers. A worker can serve a file itself or send the request to an app. In our stack it talks to PHP-FPM over FastCGI. The buffer involved in Rift belongs to that NGINX worker. The NGINX beginner's guide has a good overview of this setup.

How the byte count goes wrong

One way to build a string is to count the bytes first, allocate the buffer once, and then fill it. You can see why the first pass has to know what the second pass will do. Adding a separator or encoding a character can make the result longer.

An output plan feeds the size pass and the write pass. The writer can commit output only if it fits the actual remaining capacity.
Figure 2. A simplified view of a two-pass writer. The first pass counts the bytes. The second checks the space available before copying.
StageWhat must stay true
MeasureCount the bytes the finished string will use.
ReserveAllocate enough space for the output and any terminator.
WriteCheck the remaining space before copying.

In Rift, the rewrite module allocated a buffer that turned out to be too small. The write went past the end of that allocation. The next byte can still be on the same writable memory page, so the worker might corrupt nearby data and keep running for a while before anything visibly goes wrong. F5 also lists possible code execution under certain conditions.

I'd have both passes use the same rules for building the output and check the available space before each copy. The example below checks by subtraction, which also avoids overflowing an integer while adding up lengths.

Run it: six bytes reserved, seven needed

Here's a small C example. I set aside six bytes: four for shop, one for a label, and one for the zero byte that ends a C string. Then I try to append 42, which is two bytes. The result, shop42, needs seven bytes including the terminator. In a six-byte buffer the letters and digits fit, and the terminator would go one byte past the end.

Six cells cover the planned string. A second label byte shifts the terminator to a seventh cell outside the allocated object.
Figure 3. Four prefix bytes, two label bytes, one terminator. The proposed output crosses the allocation boundary by one byte. The example detects that before writing anything.
Watch: the write crosses the boundary (9-second loop)
The animation fills six reserved cells with letters, then shows four additional bytes outside the marked boundary.
The short uses ten bytes in a six-byte buffer to make the spill visible. The C demo below uses seven in six and stops before copying.

The program allocates both sizes and tries the same append on each. The space check leaves the six-byte buffer unchanged and lets the seven-byte buffer hold shop42. The strings and numbers are made up for this example.

rewrite_capacity.c / complete exampleC
#include <assert.h>
#include <stdbool.h>
#include <stddef.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

/* Append a byte span and leave room for the C string terminator. */
static bool append_span(char *dst, size_t capacity, size_t *used,
                        const char *src, size_t length)
{
    if (*used >= capacity || length >= capacity - *used) {
        return false;
    }

    memmove(dst + *used, src, length);
    *used += length;
    dst[*used] = '\0';
    return true;
}

int main(void)
{
    const char prefix[] = "shop";
    const char emitted[] = "42";
    const size_t planned = sizeof prefix + 1;
    const size_t needed = sizeof prefix + sizeof emitted - 1;
    char *small = malloc(planned);
    char *right = malloc(needed);
    if (!small || !right) {
        free(small);
        free(right);
        return EXIT_FAILURE;
    }

    memcpy(small, prefix, sizeof prefix);
    memcpy(right, prefix, sizeof prefix);
    size_t used = sizeof prefix - 1;
    printf("planned: %zu bytes; actual needed: %zu bytes (both include NUL)\n",
           planned, needed);
    bool ok = append_span(small, planned, &used, emitted, sizeof emitted - 1);
    assert(!ok && used == 4 && strcmp(small, "shop") == 0);
    puts("Mismatched output rejected; destination unchanged.");

    ok = append_span(right, needed, &used, emitted, sizeof emitted - 1);
    assert(ok && used == 6 && strcmp(right, "shop42") == 0);
    printf("Correctly sized buffer accepts the same output: %s\n", right);
    assert(!append_span(right, needed, &used, "!", 1));
    assert(!append_span(right, 0, &used, "", 0));
    assert(append_span(right, needed, &used, "", 0));
    puts("Full buffer, zero capacity, and empty append checks passed.");
    free(right);
    free(small);
}
Build and run with memory checksShell
cc -std=c11 -Wall -Wextra -Werror -g \
  -fsanitize=address,undefined rewrite_capacity.c -o rewrite_capacity
./rewrite_capacity
  1. Check the current length. If used is at or past capacity, return before calculating capacity - used. The subtraction uses unsigned values.
  2. Leave room for the zero byte. The six-byte buffer has two slots left, and the append needs three. The seven-byte buffer has exactly three slots.
  3. Check what changed. After the first call the buffer still holds shop and used is still four. After the second call the result is shop42.

For a real two-pass writer I'd test an empty string, an exact fit, and a buffer one byte too small. I'd also test an input whose encoded form changes length and compare the estimate with the number of bytes written.

What happens if a worker crashes?

The master can start a replacement worker. Requests that were already in progress on the crashed worker can still fail. I'd line up the worker exit times with request errors, then check whether a reload or an upstream outage happened around the same time. A restart alone leaves the cause open.

Address Space Layout Randomization (ASLR) makes memory addresses harder to predict. It's worth keeping enabled alongside the update, since the patch is what fixes this out-of-bounds write.

How we verified the upgrade

  1. Check where you're running NGINX, including the public proxy, utility containers, and any ingress. For NGINX Plus or a third-party package, use that vendor's patch guidance.
  2. Pin and pull the image you want. We used the official image at nginx:1.30.7-alpine and tested the new image with the deployed config before recreating the container.
  3. With the same Compose files, run docker compose exec -T nginx nginx -v to check the version and docker compose exec -T nginx nginx -t to check the config. Then load a PHP-backed page and a static asset.
  4. If you need time before upgrading, check the F5 advisory for temporary measures that apply to your version.
The new image passed its configuration test, the running container reported NGINX 1.30.7, and real requests succeeded over HTTPS.
Figure 4. We checked the config, the version inside the container, and the sites over HTTPS.

On localhost we had two startup problems to sort out first. NGINX couldn't find PHP-FPM, and another service already had port 8090. We made the admin port opt-in and had Compose wait for PHP-FPM to start listening.

On the production server we recreated just the NGINX container. It reported nginx/1.30.7, and ShivasNotes, both Antsand homepages, and an existing article all returned HTTP 200.

Read next on ShivasNotes

For more illustrated systems write-ups and runnable examples, subscribe to the ShivasNotes newsletter.