diff --git a/README.md b/README.md index ee1c55d..8c3bd95 100644 --- a/README.md +++ b/README.md @@ -18,3 +18,4 @@ Where possible, detections are written to be fleet-policy compatible - known una | Directory | What it detects | |---|---| | `CVE-2026-114938/` | JVMs exposed to the HotSpot x86-64 AVX-512 Base64 decode heap overwrite (CVE-2026-114938, JDK-8392044) | +| `nginx2root/` | nginx exposed to the five "nginx2root" bugs fixed in nginx 1.31.9 and 1.30.7 (CVE-2026-123007, CVE-2026-123633, CVE-2026-121810, CVE-2026-123634, CVE-2026-113425) | diff --git a/nginx2root/README.md b/nginx2root/README.md new file mode 100644 index 0000000..45ee7c3 --- /dev/null +++ b/nginx2root/README.md @@ -0,0 +1,348 @@ +# nginx2root detection (CVE-2026-123007, CVE-2026-123633, CVE-2026-121810, CVE-2026-123634, CVE-2026-113425) + +Fleet checks for the five nginx vulnerabilities disclosed on 2026-11-18 as +"nginx2root". Each one lets an unauthenticated remote attacker run code as the +nginx worker process. All five are fixed in nginx 1.31.9 and 1.30.7. + +| CVE | What it is | Affected nginx versions | +|---|---|---| +| CVE-2026-123007 | HTTP/2 flow-control window overflow | 1.9.5 to 1.31.8 | +| CVE-2026-123633 | HTTP/2 use-after-free | 1.19.7 to 1.31.8 | +| CVE-2026-121810 | HTTP/3 use-after-free | 1.25.0 to 1.31.8 | +| CVE-2026-123634 | DNS resolver use-after-free | 0.6.18 to 1.31.8 | +| CVE-2026-113425 | Mail proxy use-after-free | 0.5.33 to 1.31.8 | + +Quick start, on the host you want to check: + +```sh +sudo ./linux/detect-nginx2root.sh +``` + +The last line of the output is `impacted`, `not impacted` or `unknown`. Above +it is one status line for each CVE. + +The scripts are read-only. They do not run nginx, do not reload it, send it no +traffic, and change nothing on the host. + +## Directory contents + +Run the examples below from this directory. + +| File | Purpose | +|---|---| +| `linux/detect-nginx2root.sh` | Host detector (bash 3.2+) | +| `linux/ssh-nginx2root.sh` | Fleet run over plain SSH | +| `linux/ansible-nginx2root.yml` | Fleet run through Ansible | +| `linux/k8s-nginx2root.yaml` | Fleet run as a privileged Kubernetes DaemonSet | +| `linux/k8s-exec-nginx2root.sh` | Fleet run with `kubectl exec` into each container | + +The fleet wrappers find the detector next to themselves, so keep the files +together. The `tests/` folder holds the maintainers' test harness; it is not +needed to run any script. + +## What counts as impacted + +An nginx is impacted by a CVE when all three conditions hold for it: + +1. **Version.** The nginx version is in that CVE's affected range (table + above). Versions 1.31.9 and later, and 1.30.7 and later 1.30.x, are fixed. + - OpenResty is judged on its nginx core: `openresty/1.27.1.2` is nginx + 1.27.1. + - Tengine is judged on the nginx version it reports. + - Angie, freenginx and other forks with their own version numbers are + `unknown`. Check the vendor's advisory. + - NGINX Plus is judged on the nginx version in its version string. Confirm + the result against the F5 advisory, which lists the fixed Plus releases. +2. **No fix recorded for the package.** Linux distributions patch nginx + without changing its version number, so an old version is not proof of an + unpatched host. The detector treats a CVE as fixed when: + - the package changelog names that CVE (dpkg and rpm packages), or + - you have listed the package in `NGINX2ROOT_FIXED_PKGS` (see Usage). + + If neither applies but the binary itself carries one of the fixes, the + result is `unknown` rather than `impacted`, with "probably a backport" in + the version line. +3. **Configuration.** The affected feature is switched on: + +| CVE | Switched on when | +|---|---| +| CVE-2026-123007 | HTTP/2 is enabled: `http2` on a `listen` directive, or `http2 on;` in a server or inherited from `http {}`. | +| CVE-2026-123633 | HTTP/2 is enabled and requests are passed to a backend: `proxy_pass`, `grpc_pass`, `fastcgi_pass`, `uwsgi_pass`, `scgi_pass` or `memcached_pass`. | +| CVE-2026-121810 | A `listen ... quic` directive (HTTP/3) and requests passed to a backend as above. | +| CVE-2026-123634 | A `resolver` directive, a `*_pass` target that contains a variable, and a directive that creates subrequests: `mirror`, `ssi on`, `auth_request`, `add_before_body` or `add_after_body`. | +| CVE-2026-113425 | A `mail {}` block with at least one `server`. | + +Lua and njs code can open backend connections, look up names through the +`resolver` and make subrequests, and none of that can be read from the +configuration. Where a CVE's directives are only partly present and Lua or +njs handlers are configured, that CVE is reported `unknown`, not +`not impacted`. The same goes for CVE-2026-123634 when `resolver` and a +variable `*_pass` are present without a subrequest directive. + +A feature that is compiled out (for example no `--with-http_v2_module`) rules +its CVE out regardless of the configuration. + +The detector reads all of this without running nginx: + +- The version and build options are read from the nginx binary. For a running + nginx that is the binary in memory, so the answer is right even when the + file on disk was replaced by an upgrade and nginx was not restarted. +- The configuration is read from disk, starting at the file the running nginx + was started with and following every `include`. Comments, quoted strings and + Lua code blocks are skipped. +- For containers, files are read through `/proc/PID/root`. Nothing is run + inside the container. + +The configuration on disk is what nginx loads at its next reload. If someone +edited it without reloading, the running configuration may differ. + +## Output + +``` +nginx2root check: the five CVEs fixed in nginx 1.31.9 and 1.30.7 +Each nginx gets a status per CVE: impacted, not impacted or unknown (could not check). +host: Linux x86_64 + +/usr/sbin/nginx (pid 4159, container web-6c9f7d8b5-x2x7q) + version: nginx/1.24.0 (Ubuntu), package nginx=1.24.0-2ubuntu7.19: affected range; a backport cannot be seen in a debug build + CVE-2026-123007: impacted (http/2 enabled: listen 8443 ssl http2) + CVE-2026-123633: impacted (http/2 with proxy_pass http://127.0.0.1:9) + CVE-2026-121810: not impacted (not affected before 1.25.0) + CVE-2026-123634: not impacted (no resolver in http{}) + CVE-2026-113425: not impacted (no mail{} servers) + => impacted (CVE-2026-123007, CVE-2026-123633) + +host kernel 6.8.0-87-generic (the separate, unpatched root step; not part of the verdict) + sctp: reachable: sctp is not loaded but loads on demand (no "install sctp /bin/false" in modprobe.d) + +this host +CVE-2026-123007: impacted +CVE-2026-123633: impacted +CVE-2026-121810: not impacted +CVE-2026-123634: not impacted +CVE-2026-113425: not impacted + +impacted +``` + +Each nginx is one block: its version, a status for each CVE with the reason +in brackets, and its own result after `=>`. A status is one of: + +- `impacted`: all three conditions hold. +- `not impacted`: at least one condition rules it out. The reason says which. +- `unknown`: something could not be checked. The reason says what. + +The title line names the binary and where it was found: + +- `pid N`: a running nginx. +- `container `: a running nginx in a container. The name is the pod name + in Kubernetes and the short container ID in Docker. +- `chroot`: a running nginx in a chroot. +- `host`: a running nginx on the node itself, seen from the DaemonSet. +- `installed, not running`: found on disk. +- `-b`: a binary passed on the command line. + +After the blocks, `this host` gives one line per CVE with the worst status +found in any nginx, and the last line is the overall verdict. `-q` prints only +those five lines and the verdict. + +A block may also carry a note: + +- **"the binary was replaced on disk after this nginx started"**: nginx was + upgraded but not restarted. The result describes the old code that is still + running. Restart nginx; a reload is not enough. +- **"N old worker(s) still shutting down"**: workers left over from a reload + keep the previous configuration until their connections close. +- **"part of the configuration could not be read or parsed"**: a + `not impacted` would be a guess, so the status is `unknown` instead. + +The `host kernel` block is extra information. Running code as the nginx +worker is the nginx bug; becoming root needs a second, separate bug in the +Linux kernel's SCTP code, which had no fix on 2026-11-18. The line says +whether SCTP can be reached on this kernel. It does not change the verdict. + +## Exit codes + +| Code | Last line | Meaning | +|---|---|---| +| 0 | `not impacted` | no nginx is impacted by any of the five CVEs, or no nginx was found | +| 1 | `impacted` | at least one nginx is impacted by at least one CVE | +| 2 | `unknown` | at least one nginx could not be evaluated and none is impacted | +| 3 | (usage) | bad arguments | + +## Usage + +```sh +sudo ./linux/detect-nginx2root.sh # every running and installed nginx +sudo ./linux/detect-nginx2root.sh -q # five status lines and the verdict +sudo ./linux/detect-nginx2root.sh --no-disk # running nginx only +./linux/detect-nginx2root.sh --no-procs --no-disk -b /opt/app/sbin/nginx -c /opt/app/conf/nginx.conf +./linux/detect-nginx2root.sh --no-procs --no-disk -r /mnt/image -b /usr/sbin/nginx +``` + +| Option | Effect | +|---|---| +| `-q`, `--quiet` | print only the five status lines and the verdict | +| `-v`, `--verbose` | print diagnostic detail | +| `-b`, `--binary PATH` | also check this nginx binary (repeatable) | +| `-c`, `--conf PATH` | configuration file for binaries that are not running | +| `-r`, `--root DIR` | `-b` and `-c` are paths inside `DIR`, an unpacked image or a mounted disk | +| `--no-procs` | skip running processes | +| `--no-disk` | skip installed binaries on disk | +| `--no-kernel` | skip the `host kernel` block | + +Run as root. Without root the script cannot read nginx processes owned by +other users (nginx usually starts as root) or anything in a container, and +those come out `unknown`. + +As of 2026-11-18 no distribution had shipped a fixed package, so none is +built in. When yours does, either the package changelog names the CVEs and +the detector picks that up by itself, or you tell it: + +```sh +sudo NGINX2ROOT_FIXED_PKGS="nginx=1.24.0-2ubuntu7.20" ./linux/detect-nginx2root.sh +``` + +- `NGINX2ROOT_FIXED_PKGS` takes `name=version` entries, space separated. Use + the package name and version shown in the detector's version line. An exact + match is `not impacted`. For Debian and Ubuntu packages, a newer installed + version matches too. +- `NGINX2ROOT_FIXED_IN` replaces the first fixed nginx releases (default + `"1.30.7 1.31.9"`). The highest entry also covers every later release line. + +The fleet wrappers pass these settings on to the hosts they check. Each +section under Fleet runs shows how. + +## Coverage + +| | Covered | +|---|---| +| Running nginx, with the `-c` and `-p` it was started with | yes | +| nginx in containers (Docker, containerd, Kubernetes pods) and chroots | yes, as root | +| `nginx`, `openresty`, `angie` and `tengine` on PATH and in common install directories | yes | +| nginx upgraded on disk but not restarted | yes, the running copy is checked | +| Backports recorded in a dpkg changelog (Debian, Ubuntu) | yes | +| Backports recorded in an rpm changelog (RHEL family, SUSE) | yes, where the `rpm` command is available to the script | +| Backports in Alpine packages | no changelog on disk; use `NGINX2ROOT_FIXED_PKGS` | +| Unpacked images and mounted disks | yes, with `-r` | + +Not covered: + +- Windows and macOS. +- Directives passed with `-g` on the nginx command line. +- Configuration that exists only in memory, and anything Lua or njs code does + at run time. See the note under the configuration table for how that is + reported. +- Third-party modules that open backend connections or create subrequests. +- Stopped containers and images. Unpack one and use `-r`. +- Managed services that run nginx for you, such as a cloud load balancer or a + CDN. Check those vendors' advisories. +- Signs of an attack. These scripts check exposure, not compromise. + +The configuration check looks at each nginx as a whole, not at each virtual +server. HTTP/2 on one server and `proxy_pass` on another counts for +CVE-2026-123633. That leans toward `impacted`. + +## Fleet runs + +Wrappers that run `detect-nginx2root.sh` on many hosts and roll up the +verdicts. Each one keeps the detector's exit-code contract. + +### SSH + +`ssh-nginx2root.sh` needs no Ansible and installs nothing: the detector is +piped to each host's bash over stdin and run there, so nothing is copied to +or left on the target. + +```sh +sh linux/ssh-nginx2root.sh hosts.txt results # one host or user@host per line +PARALLEL=20 sh linux/ssh-nginx2root.sh hosts.txt +DETECT_ARGS="-q" sh linux/ssh-nginx2root.sh hosts.txt # save just the status lines +SUDO="" sh linux/ssh-nginx2root.sh hosts.txt # run as the login user (less coverage) +NGINX2ROOT_FIXED_PKGS="nginx=1.24.0-2ubuntu7.20" sh linux/ssh-nginx2root.sh hosts.txt +``` + +It needs key-based SSH and, for full coverage, passwordless sudo and bash on +the targets. `NGINX2ROOT_FIXED_PKGS` and `NGINX2ROOT_FIXED_IN`, when set, are +passed to every host. Each host's output goes to `results/.txt`. At the end it +prints the hosts by verdict and the impacted hosts for each CVE. It exits `1` +if any host is impacted, else `2` if any host is unknown or unreachable, else +`0`. + +### Ansible + +`ansible-nginx2root.yml` runs the detector on every host in the inventory. +No agent is installed: Ansible copies the detector to a temp path, runs it, +and removes it. + +```sh +ansible-playbook -i inventory linux/ansible-nginx2root.yml +ansible-playbook -i inventory linux/ansible-nginx2root.yml -e detect_args="-q" +ansible-playbook -i inventory linux/ansible-nginx2root.yml -e fixed_pkgs="nginx=1.24.0-2ubuntu7.20" +``` + +`fixed_pkgs` sets `NGINX2ROOT_FIXED_PKGS` on every host. +Each host's output is saved to `./results/.txt`, and a host list per +verdict prints at the end. Exit codes `1` (impacted) and `2` (unknown) are +verdicts and do not fail the play; `3` (usage error) does. + +### Kubernetes + +Two methods, both running the same detector: + +- `k8s-nginx2root.yaml` is a DaemonSet. One pod per node with `hostPID` and + `privileged`, so the detector sees every nginx on the node, including + ingress controllers and nginx in other pods. Nothing is copied into or run + inside workloads. The detector is mounted from a ConfigMap: + + ```sh + kubectl create namespace nginx2root + kubectl -n nginx2root create configmap detect --from-file=linux/detect-nginx2root.sh + kubectl apply -f linux/k8s-nginx2root.yaml + kubectl -n nginx2root rollout status daemonset/nginx2root + kubectl -n nginx2root logs -l app=nginx2root --tail=-1 --prefix --max-log-requests=500 + kubectl delete namespace nginx2root + ``` + + The pod is privileged by design. Review the manifest before applying it and + delete the namespace when you have your results. Each block names the pod, + and each pod's log is the result for its node. To list fixed packages, fill + in the `NGINX2ROOT_FIXED_PKGS` value in the manifest. + +- `k8s-exec-nginx2root.sh` is for clusters that forbid privileged pods. It + runs the detector inside every running container with `kubectl exec`. It + needs only the right to list pods and exec into them. Containers without + bash are reported `unknown`; use the DaemonSet for those. + `NGINX2ROOT_FIXED_PKGS` and `NGINX2ROOT_FIXED_IN`, when set, are passed to + every container. + + ```sh + sh linux/k8s-exec-nginx2root.sh > results.txt # all namespaces + sh linux/k8s-exec-nginx2root.sh -n ingress > results.txt # one namespace + ``` + +## If a host is impacted + +1. **Upgrade** nginx to 1.31.9 or 1.30.7 or later, or to the package your + distribution lists as fixing these CVEs. For OpenResty and other forks, + take the vendor release that includes these fixes. For NGINX Plus, follow + the F5 advisory. +2. **Restart nginx.** A reload does not replace the running binary. Run the + detector again afterwards: it tells you if an old copy is still running. +3. **If you cannot upgrade yet**, switch off the feature named in the status + line. This blocks the known attack path; it is not a fix. + + | CVE | Temporary mitigation | + |---|---| + | CVE-2026-123007, CVE-2026-123633 | Turn HTTP/2 off: remove `http2` from every `listen` directive and, on nginx 1.25.1 and later, set `http2 off;`. Clients fall back to HTTP/1.1; native gRPC clients stop working. | + | CVE-2026-121810 | Remove the `listen ... quic` directives and stop advertising HTTP/3 in `Alt-Svc`. | + | CVE-2026-123634 | Point `*_pass` at a fixed `upstream` group instead of a variable, so no DNS lookup happens during a request. | + | CVE-2026-113425 | Keep untrusted clients away from the mail proxy: bind it to loopback or restrict it with a firewall. | + + After a configuration change, reload nginx and run the detector again. Old + workers keep the previous configuration until they exit, and the detector + notes when any are left. +4. **Kernel.** On hosts that do not use SCTP, add `install sctp /bin/false` to + a file under `/etc/modprobe.d/`. This stops the module from loading. It + does not unload a module that is already loaded and has no effect where + SCTP is built into the kernel. diff --git a/nginx2root/linux/ansible-nginx2root.yml b/nginx2root/linux/ansible-nginx2root.yml new file mode 100644 index 0000000..76fcfc9 --- /dev/null +++ b/nginx2root/linux/ansible-nginx2root.yml @@ -0,0 +1,65 @@ +--- + +- name: nginx2root host detection + hosts: all + gather_facts: false + become: true + vars: + detect_script: "{{ playbook_dir }}/detect-nginx2root.sh" + detect_args: "" + fixed_pkgs: "" + results_dir: "{{ playbook_dir }}/results" + + tasks: + - name: Run the detector (copied, run, and removed by Ansible; nothing installed) + ansible.builtin.script: "{{ detect_script }} {{ detect_args }}" + args: + executable: bash + environment: + NGINX2ROOT_FIXED_PKGS: "{{ fixed_pkgs }}" + register: detect + changed_when: false + failed_when: detect.rc not in [0, 1, 2] + + - name: Derive this host's verdict from the exit code + ansible.builtin.set_fact: + nginx2root_verdict: "{{ 'impacted' if detect.rc == 1 else ('unknown' if detect.rc == 2 else 'not impacted') }}" + + - name: Create the results folder on the control node + ansible.builtin.file: + path: "{{ results_dir }}" + state: directory + mode: "0755" + delegate_to: localhost + become: false + run_once: true + + - name: Save this host's detector output on the control node + ansible.builtin.copy: + content: "{{ detect.stdout }}" + dest: "{{ results_dir }}/{{ inventory_hostname }}.txt" + mode: "0644" + delegate_to: localhost + become: false + changed_when: false + + - name: Print the fleet summary + run_once: true + delegate_to: localhost + become: false + vars: + pairs: >- + {{ ansible_play_hosts_all + | zip(ansible_play_hosts_all + | map('extract', hostvars, 'nginx2root_verdict') + | map('default', 'no result') + | list) + | list }} + ansible.builtin.debug: + msg: + - "nginx2root fleet detection (CVE-2026-123007, -123633, -121810, -123634, -113425)" + - "impacted: {{ pairs | selectattr('1', 'equalto', 'impacted') | map(attribute='0') | list }}" + - "unknown: {{ pairs | selectattr('1', 'equalto', 'unknown') | map(attribute='0') | list }}" + - "not impacted: {{ pairs | selectattr('1', 'equalto', 'not impacted') | map(attribute='0') | list }}" + - "no result: {{ pairs | selectattr('1', 'equalto', 'no result') | map(attribute='0') | list }}" + - "per-host output saved under {{ results_dir }}/" diff --git a/nginx2root/linux/detect-nginx2root.sh b/nginx2root/linux/detect-nginx2root.sh new file mode 100755 index 0000000..01deee2 --- /dev/null +++ b/nginx2root/linux/detect-nginx2root.sh @@ -0,0 +1,1027 @@ +#!/usr/bin/env bash +set -u +set -f + +FIXED_IN="${NGINX2ROOT_FIXED_IN-1.30.7 1.31.9}" +FIXED_PKGS="${NGINX2ROOT_FIXED_PKGS:-}" + +ID1=CVE-2026-123007 +ID2=CVE-2026-123633 +ID3=CVE-2026-121810 +ID4=CVE-2026-123634 +ID5=CVE-2026-113425 + +QUIET=0 +VERBOSE=0 +SCAN_PROCS=1 +SCAN_DISK=1 +SCAN_KERNEL=1 +EXTRA_BINS="" +CONF_OPT="" +ROOT_OPT="" + +usage() { + cat <<'EOF' +usage: detect-nginx2root.sh [-q] [-v] [-b /path/to/nginx]... [-c /path/to/nginx.conf] + [-r /path/to/rootfs] [--no-procs] [--no-disk] [--no-kernel] + + -q, --quiet print only the per-CVE status lines and the verdict + -v, --verbose print diagnostic detail + -b, --binary PATH also check this nginx binary (repeatable) + -c, --conf PATH configuration file to use for binaries that are not + running (those from -b and from the disk scan) + -r, --root DIR -b and -c paths are inside DIR, an unpacked image or a + mounted disk; packages and includes are read from there + --no-procs skip running processes + --no-disk skip installed binaries on disk + --no-kernel skip the host kernel SCTP line + +Exit: 0 not impacted, 1 impacted, 2 unknown, 3 usage error. +Env: NGINX2ROOT_FIXED_IN="1.30.7 1.31.9" first fixed nginx releases + NGINX2ROOT_FIXED_PKGS="name=version ..." packages confirmed as fixed +EOF +} + +while [ $# -gt 0 ]; do + case "$1" in + -q|--quiet) QUIET=1 ;; + -v|--verbose) VERBOSE=1 ;; + -b|--binary) + [ $# -ge 2 ] || { usage >&2; exit 3; } + EXTRA_BINS="$EXTRA_BINS$2 +" + shift ;; + -c|--conf) + [ $# -ge 2 ] || { usage >&2; exit 3; } + CONF_OPT="$2" + shift ;; + -r|--root) + [ $# -ge 2 ] || { usage >&2; exit 3; } + ROOT_OPT="${2%/}" + [ -d "$ROOT_OPT/" ] || { echo "not a directory: $2" >&2; exit 3; } + case "$ROOT_OPT" in /*) ;; *) ROOT_OPT="$PWD/$ROOT_OPT" ;; esac + shift ;; + --no-procs) SCAN_PROCS=0 ;; + --no-disk) SCAN_DISK=0 ;; + --no-kernel) SCAN_KERNEL=0 ;; + -h|--help) usage; exit 0 ;; + *) echo "unknown argument: $1" >&2; usage >&2; exit 3 ;; + esac + shift +done + +if [ -n "$CONF_OPT" ] && [ -z "$ROOT_OPT" ]; then + case "$CONF_OPT" in /*) ;; *) CONF_OPT="$PWD/$CONF_OPT" ;; esac +fi + +TMP=$(mktemp -d 2>/dev/null || mktemp -d -t nginx2root) || { echo unknown; exit 2; } +trap 'rm -rf "$TMP"' EXIT +: > "$TMP/cands" +: > "$TMP/procs" +: > "$TMP/running" +: > "$TMP/draining" + +N_IMPACTED=0 +N_UNKNOWN=0 +N_OK=0 + +H1=ok +H2=ok +H3=ok +H4=ok +H5=ok + +TAB=$(printf '\t') +SEP=$(printf '\037') +NL=' +' + +GREP_A="" +if printf 'x\n' | grep -a x >/dev/null 2>&1; then GREP_A="-a"; fi + +say() { [ "$QUIET" -eq 1 ] || printf '%s\n' "$*"; } +dbg() { [ "$VERBOSE" -eq 1 ] && printf 'debug: %s\n' "$*" >&2; return 0; } + +tally() { + case "$1" in + impacted) N_IMPACTED=$((N_IMPACTED + 1)) ;; + unknown) N_UNKNOWN=$((N_UNKNOWN + 1)) ;; + *) N_OK=$((N_OK + 1)) ;; + esac +} + +word() { + case "$1" in + impacted) echo "impacted" ;; + ok) echo "not impacted" ;; + unknown) echo "unknown" ;; + *) echo "not checked" ;; + esac +} + +worse() { + case "$1:$2" in + impacted:*|*:impacted) echo impacted ;; + unknown:*|*:unknown) echo unknown ;; + *) echo ok ;; + esac +} + +bump() { + case "$1" in + 1) H1=$(worse "$H1" "$2") ;; + 2) H2=$(worse "$H2" "$2") ;; + 3) H3=$(worse "$H3" "$2") ;; + 4) H4=$(worse "$H4" "$2") ;; + 5) H5=$(worse "$H5" "$2") ;; + esac +} + +bump_all() { + bump 1 "$1"; bump 2 "$1"; bump 3 "$1"; bump 4 "$1"; bump 5 "$1" +} + +line() { say " $1: $(word "$2")${3:+ ($3)}"; } + +result_line() { + case "$1" in + impacted) say " => impacted${2:+ ($2)}" ;; + ok) say " => not impacted" ;; + *) say " => unknown" ;; + esac +} + +single() { + say "" + say "$1" + say " $2" + result_line unknown + tally unknown + bump_all unknown +} + +header() { + say "nginx2root check: the five CVEs fixed in nginx 1.31.9 and 1.30.7" + say "Each nginx gets a status per CVE: impacted, not impacted or unknown (could not check)." +} + +finish() { + local v + if [ "$N_IMPACTED" -gt 0 ]; then v=impacted + elif [ "$N_UNKNOWN" -gt 0 ]; then v=unknown + else v="not impacted" + fi + [ $((N_IMPACTED + N_UNKNOWN + N_OK)) -eq 0 ] && { say ""; say "no nginx found"; } + say "" + say "this host" + printf '%s: %s\n' "$ID1" "$(word "$H1")" + printf '%s: %s\n' "$ID2" "$(word "$H2")" + printf '%s: %s\n' "$ID3" "$(word "$H3")" + printf '%s: %s\n' "$ID4" "$(word "$H4")" + printf '%s: %s\n' "$ID5" "$(word "$H5")" + say "" + echo "$v" + case "$v" in + impacted) exit 1 ;; + unknown) exit 2 ;; + esac + exit 0 +} + +clean() { printf '%s' "$1" | LC_ALL=C tr -cd '\040-\176' | cut -c1-160; } + +ver_key() { + local v IFS=. + v=$(printf '%s' "$1" | sed 's/[^0-9.].*$//') + set -- $v + echo $(( 10#${1:-0} * 1000000 + 10#${2:-0} * 1000 + 10#${3:-0} )) +} + +is_fixed() { + local k=$1 f fk top=0 + for f in $FIXED_IN; do + fk=$(ver_key "$f") + [ $((fk / 1000)) -gt "$top" ] && top=$((fk / 1000)) + if [ $((fk / 1000)) -eq $((k / 1000)) ] && [ "$k" -ge "$fk" ]; then + return 0 + fi + done + [ "$top" -gt 0 ] && [ $((k / 1000)) -gt "$top" ] +} + +LOW1=$(ver_key 1.9.5) +LOW2=$(ver_key 1.19.7) +LOW3=$(ver_key 1.25.0) +LOW4=$(ver_key 0.6.18) +LOW5=$(ver_key 0.5.33) + +resolve_in() { + local root=$1 rest=$2 cur="" comp t n=0 + while [ -n "$rest" ]; do + comp=${rest%%/*} + if [ "$comp" = "$rest" ]; then rest=""; else rest=${rest#*/}; fi + case "$comp" in + ''|.) continue ;; + ..) cur=${cur%/*}; continue ;; + esac + if [ -L "$root$cur/$comp" ]; then + n=$((n + 1)) + [ "$n" -gt 40 ] && return 1 + t=$(readlink "$root$cur/$comp") || return 1 + case "$t" in /*) cur="" ;; esac + rest="$t${rest:+/$rest}" + else + cur="$cur/$comp" + fi + done + printf '%s\n' "${cur:-/}" +} + +pkg_of() { + local root=$1 path=$2 l name ver + [ -n "$path" ] || return 0 + if [ -d "$root/var/lib/dpkg/info" ] && [ -r "$root/var/lib/dpkg/status" ]; then + set +f + l=$(grep -lxF -- "$path" "$root"/var/lib/dpkg/info/*.list 2>/dev/null) + set -f + l=${l%%"$NL"*} + if [ -n "$l" ]; then + name=${l##*/}; name=${name%.list}; name=${name%%:*} + ver=$(awk -v p="$name" '$1 == "Package:" { n = $2 } $1 == "Version:" && n == p { print $2; exit }' \ + "$root/var/lib/dpkg/status" 2>/dev/null) + if [ -n "$ver" ]; then printf 'dpkg %s=%s\n' "$name" "$ver"; return 0; fi + fi + fi + if [ -r "$root/lib/apk/db/installed" ]; then + l=$(awk -v t="${path#/}" ' + { k = substr($0, 1, 2); v = substr($0, 3) } + k == "P:" { p = v } k == "V:" { ver = v } k == "F:" { d = v } + k == "R:" && d "/" v == t { print p "=" ver; exit }' "$root/lib/apk/db/installed" 2>/dev/null) + if [ -n "$l" ]; then printf 'apk %s\n' "$l"; return 0; fi + fi + if command -v rpm >/dev/null 2>&1 && + { [ -d "$root/var/lib/rpm" ] || [ -d "$root/usr/lib/sysimage/rpm" ]; }; then + if [ -n "$root" ]; then + l=$(rpm --root "$root" -qf --qf '%{NAME}=%{VERSION}-%{RELEASE}\n' "$path" 2>/dev/null) || l="" + else + l=$(rpm -qf --qf '%{NAME}=%{VERSION}-%{RELEASE}\n' "$path" 2>/dev/null) || l="" + fi + l=${l%%"$NL"*} + case "$l" in + *" "*|"") ;; + *=*) printf 'rpm %s\n' "$l"; return 0 ;; + esac + fi + return 0 +} + +pkg_cves() { + local root=$1 kind=$2 name=$3 f text="" got=0 id out="" + case "$kind" in + dpkg) + f=$(resolve_in "$root" "/usr/share/doc/$name/changelog.Debian.gz") || f="" + if [ -n "$f" ] && [ -r "$root$f" ] && command -v gzip >/dev/null 2>&1; then + text=$(gzip -dc < "$root$f" 2>/dev/null | grep -o -E 'CVE-2026-[0-9]+' 2>/dev/null) && got=1 + [ -n "$text" ] || got=1 + fi ;; + rpm) + if command -v rpm >/dev/null 2>&1; then + if [ -n "$root" ]; then + text=$(rpm --root "$root" -q --changelog "$name" 2>/dev/null) && got=1 + else + text=$(rpm -q --changelog "$name" 2>/dev/null) && got=1 + fi + fi ;; + esac + if [ "$got" -eq 0 ]; then echo "-"; return 0; fi + for id in "$ID1" "$ID2" "$ID3" "$ID4" "$ID5"; do + case "$text" in *"$id"*) out="$out $id" ;; esac + done + echo "${out# }" +} + +pkg_fixed() { + local kind=$1 pkg=$2 e + [ -n "$pkg" ] || return 1 + for e in $FIXED_PKGS; do + [ "${e%%=*}" = "${pkg%%=*}" ] || continue + [ "${e#*=}" = "${pkg#*=}" ] && return 0 + if [ "$kind" = dpkg ] && command -v dpkg >/dev/null 2>&1 && + dpkg --compare-versions "${pkg#*=}" ge "${e#*=}" 2>/dev/null; then + return 0 + fi + done + return 1 +} + +bin_info() { + local f=$1 s l rest + B_VERLINE=""; B_PRODUCT=""; B_VERSION=""; B_BUILD=""; B_ALT="" + B_CONFARGS=""; B_MARK_OLD=0; B_MARK_NEW=0 + [ -r "$f" ] || return 1 + s=$(tr '\0' '\n' < "$f" 2>/dev/null | LC_ALL=C grep $GREP_A -o -E \ + '[A-Za-z]+ version: [A-Za-z]+/[0-9][0-9.]*[ -~]*|configure arguments:[ -~]*|could not cancel %V resolving|mail auth http block write|smtp writing blocked' \ + 2>/dev/null) || s="" + while IFS= read -r l; do + case "$l" in + "could not cancel %V resolving") B_MARK_OLD=1 ;; + "mail auth http block write"|"smtp writing blocked") B_MARK_NEW=1 ;; + "configure arguments:"*) [ -n "$B_CONFARGS" ] || B_CONFARGS=" ${l#configure arguments:} " ;; + "nginx version: "*) B_VERLINE=$l ;; + *" version: "*) + [ -n "$B_ALT" ] || B_ALT=${l#* version: } + [ -n "$B_VERLINE" ] || B_VERLINE=$l ;; + esac + done < 1) s = s US + s = s w + } + print s + if (t == "{" && n > 0 && A[1] ~ /_by_lua_block$/) { raw = 1; rawdepth = 1 } + n = 0 + } + BEGIN { US = sprintf("%c", 31); q = ""; lq = ""; n = 0 } + { + if (!raw && !cmt && q == "" && !intok && $0 ~ /^[ \t]*(#.*)?\r?$/) next + line = $0 "\n" + len = length(line) + for (i = 1; i <= len; i++) { + c = substr(line, i, 1) + if (raw) { + if (lq != "") { + if (lesc) lesc = 0 + else if (c == "\\") lesc = 1 + else if (c == lq || c == "\n") lq = "" + continue + } + if (llong) { + if (c == "]" && substr(line, i + 1, 1) == "]") { llong = 0; i++ } + continue + } + if (lcmt) { if (c == "\n") lcmt = 0; continue } + if (c == "-" && substr(line, i + 1, 1) == "-") { + if (substr(line, i + 2, 2) == "[[") { llong = 1; i += 3 } else lcmt = 1 + continue + } + if (c == "[" && substr(line, i + 1, 1) == "[") { llong = 1; i++; continue } + if (c == "\"" || c == "\047") { lq = c; continue } + if (c == "{") rawdepth++ + else if (c == "}") { rawdepth--; if (rawdepth == 0) { raw = 0; print "}\t" } } + continue + } + if (cmt) { if (c == "\n") cmt = 0; continue } + if (q != "") { + if (esc) { tok = tok c; esc = 0 } + else if (c == "\\") esc = 1 + else if (c == q) { q = ""; A[++n] = tok; tok = "" } + else tok = tok c + continue + } + if (intok) { + if (esc) { tok = tok c; esc = 0; continue } + if (c == "\\") { esc = 1; continue } + if (c == "{" && var) { tok = tok c; var = 0; continue } + var = (c == "$") + if (c == " " || c == "\t" || c == "\n" || c == "\r") { A[++n] = tok; tok = ""; intok = 0; continue } + if (c == ";") { A[++n] = tok; tok = ""; intok = 0; emit(";"); continue } + if (c == "{") { A[++n] = tok; tok = ""; intok = 0; emit("{"); continue } + tok = tok c + continue + } + if (c == " " || c == "\t" || c == "\n" || c == "\r") continue + if (c == "#") { cmt = 1; continue } + if (c == ";") { if (n > 0) emit(";"); continue } + if (c == "{") { emit("{"); continue } + if (c == "}") { n = 0; print "}\t"; continue } + if (c == "\"" || c == "\047") { q = c; tok = ""; esc = 0; continue } + intok = 1; tok = ""; esc = 0; var = 0 + if (c == "\\") esc = 1 + else { tok = c; var = (c == "$") } + } + } + END { if (q != "" || raw || intok || n > 0) print "!\t" }' "$1" 2>/dev/null +} + +emit_conf() { + local root=$1 p=$2 cdir=$3 depth=$4 f t incs w n pat prev + if [ "$depth" -gt 12 ] || [ "$CONF_FILES" -gt 4000 ]; then CONF_PARTIAL=1; return; fi + if [ -n "$root" ]; then + p=$(resolve_in "$root" "$p") || { CONF_PARTIAL=1; return; } + fi + f="$root$p" + if [ ! -f "$f" ] || [ ! -r "$f" ]; then + CONF_PARTIAL=1 + dbg "cannot read $p" + return + fi + CONF_FILES=$((CONF_FILES + 1)) + CONF_SEQ=$((CONF_SEQ + 1)) + t="$TMP/tok.$CONF_SEQ" + tokenize "$f" > "$t" || CONF_PARTIAL=1 + incs=$(grep -n "^;${TAB}include${SEP}" "$t" 2>/dev/null) + if [ -z "$incs" ]; then + cat "$t" + rm -f "$t" + return + fi + prev=1 + while IFS= read -r w; do + [ -n "$w" ] || continue + n=${w%%:*} + pat=${w#*"$SEP"} + pat=${pat%%"$SEP"*} + [ "$n" -gt "$prev" ] && sed -n "${prev},$((n - 1))p" "$t" + expand_include "$root" "$cdir" "$pat" $((depth + 1)) + prev=$((n + 1)) + done < 60) s = substr(s, 1, 57) "..." + return s + } + function setf(k, v) { if (!(k in F)) F[k] = short(v) } + function arg(i) { return (i <= n) ? A[i] : "" } + function directive(isblock, name, rest, i, top) { + if (n == 0) { + if (isblock) S[++depth] = "{" + return + } + name = A[1] + rest = name + for (i = 2; i <= n; i++) rest = rest " " A[i] + top = (depth >= 1) ? S[1] : "main" + if (name == "include") C["unexpanded"]++ + if (top == "http") { + if (name == "listen") { + for (i = 2; i <= n; i++) { + if (A[i] == "http2") setf("h2", rest) + if (A[i] == "quic" || A[i] == "http3") setf("quic", rest) + } + } else if (name == "http2") { + if (depth == 1) { hv = arg(2); hrest = rest } + else if (depth == 2 && S[2] == "server") { sv[ns] = arg(2); srest[ns] = rest } + } else if (name ~ /^(proxy|grpc|fastcgi|uwsgi|scgi|memcached)_pass$/) { + setf("pass", rest) + if (index(rest, "$")) setf("varpass", rest) + } else if (name == "resolver") { + setf("resolver", rest) + } else if ((name == "mirror" && arg(2) != "off") || (name == "ssi" && arg(2) == "on") || + (name == "auth_request" && arg(2) != "off") || + name == "add_before_body" || name == "add_after_body") { + setf("subreq", rest) + } else if (name ~ /_by_lua(_block|_file)?$/ || name ~ /^js_(content|access|header_filter|body_filter|set|periodic)$/) { + setf("script", name) + } + if (isblock && name == "server" && depth == 1) { ns++; sv[ns] = "" } + } else if (top == "mail") { + if (isblock && name == "server") C["mailservers"]++ + if (name == "auth_http") setf("authhttp", rest) + } + if (isblock) S[++depth] = name + } + BEGIN { FS = "\t"; US = sprintf("%c", 31); depth = 0; ns = 0; hv = "" } + $1 == "!" { C["lexerr"]++; next } + $1 == "}" { if (depth == 0) C["lexerr"]++; else depth--; next } + $1 == ";" || $1 == "{" { + n = split($2, A, US) + directive($1 == "{") + } + END { + if (depth != 0) C["lexerr"]++ + if (!("h2" in F)) { + for (s = 1; s <= ns; s++) { + if (sv[s] == "on") { setf("h2", srest[s]); break } + if (sv[s] == "" && hv == "on") { setf("h2", hrest " in http{}"); break } + } + } + for (k in F) print k "\t" F[k] + for (k in C) print k "\t" C[k] + print "done\t1" + }' +} + +cve() { + local idx=$1 id=$2 low=$3 lowtxt=$4 cs=$5 ct=$6 st + if [ "$A_KIND" = upstream ] && [ "$A_KEY" -lt "$low" ]; then + st=ok; ct="not affected before $lowtxt" + elif [ "$cs" = ok ]; then + st=ok + else + case " $A_PKGCVES " in + *" $id "*) st=ok; ct="fix named in the package changelog" ;; + *) + if [ "$A_BASE" = unknown ] || [ "$cs" = unknown ]; then st=unknown; else st=impacted; fi ;; + esac + fi + line "$id" "$st" "$ct" + bump "$idx" "$st" + case "$st" in + impacted) R_IMP=1; HITS="$HITS${HITS:+, }$id" ;; + unknown) R_UNK=1 ;; + esac +} + +print_notes() { + local l + [ -n "$1" ] || return 0 + while IFS= read -r l; do + [ -n "$l" ] && say " note: $l" + done < "$TMP/conf" + cfg=ok + if [ "$CONF_FILES" -eq 0 ]; then + cfg=unreadable + else + conf_facts < "$TMP/conf" > "$TMP/facts" 2>/dev/null + while IFS="$TAB" read -r k v; do + case "$k" in + h2) F_h2=$v ;; + quic) F_quic=$v ;; + pass) F_pass=$v ;; + varpass) F_varpass=$v ;; + resolver) F_resolver=$v ;; + subreq) F_subreq=$v ;; + script) F_script=$v ;; + authhttp) F_authhttp=$v ;; + mailservers) F_mail=$v ;; + unexpanded) F_unexp=$v ;; + lexerr) F_lexerr=$v ;; + done) F_done=$v ;; + esac + done < "$TMP/facts" + if [ "$CONF_PARTIAL" -eq 1 ] || [ "$F_unexp" != 0 ] || [ "$F_lexerr" != 0 ] || [ "$F_done" != 1 ]; then cfg=partial; fi + fi + dbg "$title: conf=$conf files=$CONF_FILES cfg=$cfg h2=[$F_h2] quic=[$F_quic] pass=[$F_pass] varpass=[$F_varpass] resolver=[$F_resolver] subreq=[$F_subreq] script=[$F_script] mail=$F_mail" + + none=ok + if [ "$cfg" = partial ]; then none=unknown; part=", but the configuration was only partly read"; fi + conf=$(clean "$conf") + + if ! built ' --with-http_v2_module '; then c1=ok; t1="http_v2 module not built" + elif [ "$cfg" = unreadable ]; then c1=unknown; t1="cannot read $conf" + elif [ -n "$F_h2" ]; then c1=impacted; t1="http/2 enabled: $F_h2" + else c1=$none; t1="http/2 not enabled$part" + fi + + if ! built ' --with-http_v2_module '; then c2=ok; t2="http_v2 module not built" + elif [ "$cfg" = unreadable ]; then c2=unknown; t2="cannot read $conf" + elif [ -z "$F_h2" ]; then c2=$none; t2="http/2 not enabled$part" + elif [ -n "$F_pass" ]; then c2=impacted; t2="http/2 with $F_pass" + elif [ -n "$F_script" ]; then c2=unknown; t2="http/2 on, no *_pass, but $F_script may open connections" + else c2=$none; t2="http/2 on, nothing passed upstream$part" + fi + + if ! built ' --with-http_v3_module '; then c3=ok; t3="http_v3 module not built" + elif [ "$cfg" = unreadable ]; then c3=unknown; t3="cannot read $conf" + elif [ -z "$F_quic" ]; then c3=$none; t3="no quic listener$part" + elif [ -n "$F_pass" ]; then c3=impacted; t3="$F_quic, with $F_pass" + elif [ -n "$F_script" ]; then c3=unknown; t3="$F_quic, no *_pass, but $F_script may open connections" + else c3=$none; t3="quic on, nothing passed upstream$part" + fi + + if [ "$cfg" = unreadable ]; then c4=unknown; t4="cannot read $conf" + elif [ -z "$F_resolver" ]; then c4=$none; t4="no resolver in http{}$part" + elif [ -z "$F_varpass" ] && [ -n "$F_script" ]; then + c4=unknown; t4="$F_resolver; $F_script code may resolve names and make subrequests" + elif [ -z "$F_varpass" ]; then c4=$none; t4="resolver set, no *_pass with a variable$part" + elif [ -n "$F_subreq" ]; then c4=impacted; t4="$F_resolver, $F_varpass, $F_subreq" + else c4=unknown; t4="resolver and $F_varpass; no mirror/ssi/auth_request seen, other subrequest sources not ruled out" + fi + + if ! built ' --with-mail(=dynamic)? '; then c5=ok; t5="mail module not built" + elif [ "$cfg" = unreadable ]; then c5=unknown; t5="cannot read $conf" + elif [ "$F_mail" != 0 ]; then + c5=impacted; t5="mail{} with $F_mail server(s)" + [ -n "$F_authhttp" ] && t5="$t5, $F_authhttp" + else c5=$none; t5="no mail{} servers$part" + fi + + HITS="" + R_IMP=0 + R_UNK=0 + cve 1 "$ID1" "$LOW1" 1.9.5 "$c1" "$t1" + cve 2 "$ID2" "$LOW2" 1.19.7 "$c2" "$t2" + cve 3 "$ID3" "$LOW3" 1.25.0 "$c3" "$t3" + cve 4 "$ID4" "$LOW4" 0.6.18 "$c4" "$t4" + cve 5 "$ID5" "$LOW5" 0.5.33 "$c5" "$t5" + + [ "$cfg" = partial ] && notes="${notes:+$notes$NL}part of the configuration could not be read or parsed; run as root, or check it by hand" + print_notes "$notes" + + if [ "$R_IMP" -eq 1 ]; then + result_line impacted "$HITS"; tally impacted + elif [ "$R_UNK" -eq 1 ]; then + result_line unknown; tally unknown + else + result_line ok; tally ok + fi +} + +resolve() { + readlink -f "$1" 2>/dev/null || realpath "$1" 2>/dev/null || printf '%s\n' "$1" +} + +add_cand() { + local r + { [ -f "$1" ] && [ -x "$1" ]; } || return 0 + r=$(resolve "$1") + grep -qxF -- "$r" "$TMP/cands" || printf '%s\n' "$r" >> "$TMP/cands" +} + +discover_disk() { + local n p + for n in nginx openresty angie tengine freenginx; do + p=$(command -v "$n" 2>/dev/null) || p="" + [ -n "$p" ] && add_cand "$p" + done + set +f + for p in /usr/sbin/nginx /usr/local/sbin/nginx /usr/local/nginx/sbin/nginx \ + /usr/local/openresty/nginx/sbin/nginx /usr/local/openresty/bin/openresty \ + /usr/sbin/angie /usr/local/angie/sbin/angie \ + /usr/sbin/tengine /usr/local/tengine/sbin/nginx \ + /opt/zimbra/common/sbin/nginx \ + /opt/*/sbin/nginx /opt/*/nginx/sbin/nginx /opt/*/*/sbin/nginx \ + /usr/local/*/sbin/nginx /usr/local/*/nginx/sbin/nginx; do + add_cand "$p" + done + set -f +} + +proc_line() { + printf '%s%s%s%s%s%s%s%s%s%s%s%s%s%s%s\n' "$1" "$SEP" "$2" "$SEP" "$3" "$SEP" "$4" "$SEP" \ + "$5" "$SEP" "$6" "$SEP" "$7" "$SEP" "$8" >> "$TMP/procs" +} + +scan_procs_linux() { + local d pid cmd exe args tok prev popt copt root ns self_ns init_ns rootlink binpath tag note st cwd where hn replaced + self_ns=$(readlink /proc/self/ns/mnt 2>/dev/null) || self_ns="" + init_ns=$(readlink /proc/1/ns/mnt 2>/dev/null) || init_ns="" + set +f + for d in /proc/[0-9]*; do + set -f + pid=${d#/proc/} + [ "$pid" = "$$" ] && continue + [ -r "$d/cmdline" ] || continue + cmd=$(tr '\0' ' ' < "$d/cmdline" 2>/dev/null) || continue + [ -n "$cmd" ] || continue + exe=$(readlink "$d/exe" 2>/dev/null) || exe="" + case "$cmd" in + "nginx: master process "*|"angie: master process "*|"tengine: master process "*) + args=${cmd#*: master process } ;; + *": worker process is shutting down"*) + st=$(cat "$d/stat" 2>/dev/null) || st="" + st=${st##*) } + set -- $st + [ $# -ge 2 ] && printf '%s\n' "$2" >> "$TMP/draining" + continue ;; + *) + case "${exe% (deleted)}" in + */nginx|*/nginx-debug|*/openresty|*/angie|*/tengine|*/freenginx) ;; + *) continue ;; + esac + case "$cmd" in *": "*" process"*) continue ;; esac + case " $cmd " in *" -t "*|*" -T "*|*" -s "*|*" -v "*|*" -V "*|*" -h "*|*" -? "*) continue ;; esac + args=$cmd ;; + esac + + popt=""; copt=""; prev=""; tag="" + for tok in $args; do + [ -n "$tag" ] || tag=$tok + case "$prev" in + -c) copt=$tok ;; + -p) popt=$tok ;; + esac + prev=$tok + done + tag=$(clean "$tag") + + if [ -z "$exe" ] || [ ! -r "$d/exe" ]; then + single "$tag (pid $pid)" "cannot read /proc/$pid/exe, run as root" + continue + fi + binpath=${exe% (deleted)} + + root="" + note="" + where="" + replaced=0 + ns=$(readlink "$d/ns/mnt" 2>/dev/null) || ns="" + rootlink=$(readlink "$d/root" 2>/dev/null) || rootlink=/ + if [ -n "$ns" ] && [ -n "$self_ns" ] && [ "$ns" != "$self_ns" ]; then + root="$d/root" + if [ "$ns" = "$init_ns" ]; then + where=", host" + else + hn="" + if [ -r "$root/etc/hostname" ]; then IFS= read -r hn < "$root/etc/hostname" || true; fi + hn=$(printf '%s' "$hn" | tr -cd 'A-Za-z0-9._-' | cut -c1-80) + where=", container${hn:+ $hn}" + fi + elif [ "$rootlink" != / ]; then + root="$d/root" + where=", chroot" + elif [ "$exe" = "$binpath" ]; then + printf '%s\n' "$binpath" >> "$TMP/running" + fi + tag="$binpath (pid $pid$where)" + if [ -n "$root" ] && [ ! -d "$root/" ]; then + single "$(clean "$tag")" "cannot read /proc/$pid/root, run as root" + continue + fi + case "$popt" in + ""|/*) ;; + *) cwd=$(readlink "$d/cwd" 2>/dev/null) || cwd="" + popt="$cwd/$popt" ;; + esac + if [ "$exe" != "$binpath" ]; then + replaced=1 + note="the binary was replaced on disk after this nginx started; the result is for the running copy. Restart nginx (a reload is not enough) to run the new one" + fi + proc_line "$pid" "$root" "$(clean "$tag")" "$binpath" "$popt" "$copt" "$replaced" "$note" + done + set -f +} + +kernel_sctp() { + local kroot="" rel a b txt f found="" + [ "$(uname -s)" = Linux ] || return 0 + a=$(readlink /proc/1/ns/mnt 2>/dev/null) || a="" + b=$(readlink /proc/self/ns/mnt 2>/dev/null) || b="" + if [ -n "$a" ] && [ -n "$b" ] && [ "$a" != "$b" ] && [ -d /proc/1/root/etc ]; then + kroot=/proc/1/root + fi + rel=$(uname -r) + set +f + for f in "$kroot"/etc/modprobe.d/*.conf "$kroot"/run/modprobe.d/*.conf \ + "$kroot"/usr/lib/modprobe.d/*.conf "$kroot"/lib/modprobe.d/*.conf; do + [ -r "$f" ] || continue + if grep -qE '^[[:space:]]*install[[:space:]]+sctp[[:space:]]+(/usr)?/bin/(false|true)' "$f" 2>/dev/null; then + found=${f#"$kroot"} + break + fi + done + set -f + if grep -q '^sctp ' /proc/modules 2>/dev/null; then + txt="reachable: the sctp module is loaded" + [ -n "$found" ] && txt="$txt ($found blocks loading, but does not unload it)" + elif [ -d /sys/module/sctp ]; then + txt="reachable: sctp is built into this kernel" + elif [ -n "$found" ]; then + txt="blocked: sctp is not loaded and $found stops it loading" + else + set +f + set -- "$kroot"/lib/modules/"$rel"/kernel/net/sctp/sctp.ko* + set -f + if [ -e "$1" ]; then + txt="reachable: sctp is not loaded but loads on demand (no \"install sctp /bin/false\" in modprobe.d)" + elif [ -d "$kroot/lib/modules/$rel" ]; then + txt="not present: no sctp module for this kernel" + else + txt="unknown: sctp is not loaded, and this kernel's modules cannot be seen from here" + fi + fi + say "" + say "host kernel $rel (the separate, unpatched root step; not part of the verdict)" + say " sctp: $txt" +} + +HOST_OS=$(uname -s) +HOST_ARCH=$(uname -m) +IS_ROOT=0 +[ "$(id -u)" = 0 ] && IS_ROOT=1 + +header +say "host: $HOST_OS $HOST_ARCH" + +[ "$SCAN_DISK" -eq 1 ] && discover_disk +while IFS= read -r b; do + [ -n "$b" ] || continue + if [ -n "$ROOT_OPT" ]; then + r=$(resolve_in "$ROOT_OPT" "$b") || r="" + if [ -n "$r" ] && [ -f "$ROOT_OPT$r" ]; then + printf 'extra:%s\n' "$r" >> "$TMP/cands" + else + printf 'missing:%s\n' "$b" >> "$TMP/cands" + fi + elif [ -f "$b" ]; then + r=$(resolve "$b") + grep -qxF -- "$r" "$TMP/cands" || printf 'extra:%s\n' "$r" >> "$TMP/cands" + else + printf 'missing:%s\n' "$b" >> "$TMP/cands" + fi +done </dev/null) || drain=0 + if [ "$drain" -gt 0 ]; then + note="${note:+$note$NL}$drain old worker(s) still shutting down; they keep the previous configuration until they exit" + fi + analyze "$tag" "/proc/$pid/exe" "$root" "$binpath" "$popt" "$copt" "$note" "$replaced" +done < "$TMP/procs" + +while IFS= read -r b; do + [ -n "$b" ] || continue + case "$b" in + missing:*) single "${b#missing:} (-b)" "not a file"; continue ;; + extra:*) + b=${b#extra:} + if [ -n "$ROOT_OPT" ]; then + analyze "$b (-b, root $ROOT_OPT)" "$ROOT_OPT$b" "$ROOT_OPT" "$b" "" "$CONF_OPT" "" 0 + continue + fi + t="$b (-b)" ;; + *) + grep -qxF -- "$b" "$TMP/running" && continue + bin_info "$b" || continue + [ -n "$B_VERLINE" ] || continue + t="$b (installed, not running)" ;; + esac + analyze "$t" "$b" "" "$b" "" "$CONF_OPT" "" 0 +done < "$TMP/cands" + +[ "$SCAN_KERNEL" -eq 1 ] && kernel_sctp + +finish diff --git a/nginx2root/linux/k8s-exec-nginx2root.sh b/nginx2root/linux/k8s-exec-nginx2root.sh new file mode 100755 index 0000000..235e6c9 --- /dev/null +++ b/nginx2root/linux/k8s-exec-nginx2root.sh @@ -0,0 +1,42 @@ +#!/bin/sh +set -u + +SCRIPT=${DETECT:-$(dirname "$0")/detect-nginx2root.sh} +[ -f "$SCRIPT" ] || { echo "detector not found at $SCRIPT (set DETECT=/path/to/detect-nginx2root.sh)" >&2; exit 3; } +[ $# -gt 0 ] || set -- -A + +NL=' +' +LIST=$(kubectl get pods "$@" --field-selector=status.phase=Running \ + -o go-template='{{range .items}}{{$ns := .metadata.namespace}}{{$pod := .metadata.name}}{{$node := .spec.nodeName}}{{range .spec.containers}}{{$ns}} {{$pod}} {{.name}} {{$node}}{{"\n"}}{{end}}{{end}}') || exit 3 + +imp=0; unk=0; ok=0 +IFS=$NL +for line in $LIST; do + IFS=' ' + set -- $line + ns=$1; pod=$2; ctr=$3; node=$4 + printf '=== %s/%s/%s (node %s) ===\n' "$ns" "$pod" "$ctr" "$node" + if kubectl exec -n "$ns" "$pod" -c "$ctr" -- sh -c 'command -v bash >/dev/null 2>&1' >/dev/null 2>&1; then + out=$(kubectl exec -i -n "$ns" "$pod" -c "$ctr" -- \ + env "NGINX2ROOT_FIXED_PKGS=${NGINX2ROOT_FIXED_PKGS:-}" "NGINX2ROOT_FIXED_IN=${NGINX2ROOT_FIXED_IN-1.30.7 1.31.9}" \ + bash -s -- --no-kernel < "$SCRIPT" 2>/dev/null) + printf '%s\n' "$out" + case "$(printf '%s\n' "$out" | tail -1)" in + impacted) imp=$((imp + 1)) ;; + "not impacted") ok=$((ok + 1)) ;; + *) unk=$((unk + 1)) ;; + esac + else + echo "unknown: no bash in this container; use the DaemonSet (k8s-nginx2root.yaml)" + unk=$((unk + 1)) + fi + IFS=$NL +done +IFS=' ' + +echo "---" +echo "nginx2root containers: $imp impacted, $unk unknown, $ok not impacted" +[ "$imp" -gt 0 ] && exit 1 +[ "$unk" -gt 0 ] && exit 2 +exit 0 diff --git a/nginx2root/linux/k8s-nginx2root.yaml b/nginx2root/linux/k8s-nginx2root.yaml new file mode 100644 index 0000000..7a704e3 --- /dev/null +++ b/nginx2root/linux/k8s-nginx2root.yaml @@ -0,0 +1,53 @@ +apiVersion: apps/v1 +kind: DaemonSet +metadata: + name: nginx2root + namespace: nginx2root +spec: + selector: + matchLabels: + app: nginx2root + template: + metadata: + labels: + app: nginx2root + spec: + hostPID: true + nodeSelector: + kubernetes.io/os: linux + tolerations: + - operator: Exists + terminationGracePeriodSeconds: 1 + containers: + - name: detect + image: debian:stable-slim + securityContext: + privileged: true + env: + - name: NODE_NAME + valueFrom: + fieldRef: + fieldPath: spec.nodeName + - name: NGINX2ROOT_FIXED_PKGS + value: "" + command: ["/bin/sh", "-c"] + args: + - | + echo "=== node: ${NODE_NAME} ===" + bash /scripts/detect-nginx2root.sh --no-disk || true + echo "=== end node: ${NODE_NAME} ===" + sleep infinity + resources: + requests: + cpu: 50m + memory: 64Mi + limits: + memory: 512Mi + volumeMounts: + - name: detect + mountPath: /scripts + readOnly: true + volumes: + - name: detect + configMap: + name: detect diff --git a/nginx2root/linux/ssh-nginx2root.sh b/nginx2root/linux/ssh-nginx2root.sh new file mode 100755 index 0000000..5478ea8 --- /dev/null +++ b/nginx2root/linux/ssh-nginx2root.sh @@ -0,0 +1,83 @@ +#!/bin/sh +set -u + +SCRIPT=${DETECT:-$(dirname "$0")/detect-nginx2root.sh} +HOSTS=${1:?usage: ssh-nginx2root.sh hosts.txt [results_dir]} +OUT=${2:-results} +PARALLEL=${PARALLEL:-10} +SUDO=${SUDO-sudo -n} +DETECT_ARGS=${DETECT_ARGS:-} + +[ -f "$SCRIPT" ] || { echo "detector not found at $SCRIPT (set DETECT=/path/to/detect-nginx2root.sh)" >&2; exit 3; } +[ -f "$HOSTS" ] || { echo "hosts file not found: $HOSTS" >&2; exit 3; } +mkdir -p "$OUT" || exit 3 +rm -f "$OUT"/.status.* 2>/dev/null + +safe() { printf '%s' "$1" | tr -c 'A-Za-z0-9._@-' '_'; } + +ENVP="" +for v in NGINX2ROOT_FIXED_PKGS NGINX2ROOT_FIXED_IN; do + isset="" + val="" + eval "isset=\${$v+yes}; val=\${$v-}" + [ "${isset:-}" = yes ] || continue + case "$val" in + *[!A-Za-z0-9\ ._:+~=-]*) echo "$v has characters that cannot be passed over SSH" >&2; exit 3 ;; + esac + ENVP="$ENVP $v='$val'" +done +[ -n "$ENVP" ] && ENVP="env$ENVP" + +one() { + h=$1 + s=$(safe "$h") + f="$OUT/$s.txt" + if ssh -o BatchMode=yes -o ConnectTimeout=15 "$h" "$SUDO $ENVP bash -s -- $DETECT_ARGS" < "$SCRIPT" > "$f" 2>"$f.err"; then + rc=0 + else + rc=$? + fi + case "$rc" in + 0) v="not impacted" ;; + 1) v="impacted" ;; + 2) v="unknown" ;; + 3) v="error (usage)" ;; + 255) v="error (ssh/connection)" ;; + *) v="error (rc=$rc)" ;; + esac + printf '%s\t%s\n' "$v" "$h" > "$OUT/.status.$s" + grep -E '^CVE-[0-9]+-[0-9]+: ' "$f" 2>/dev/null | while IFS= read -r l; do + printf '%s\t%s\t%s\n' "${l%%:*}" "${l#*: }" "$h" + done > "$OUT/.cve.$s" + [ -s "$f.err" ] || rm -f "$f.err" +} + +n=0 +while IFS= read -r h || [ -n "$h" ]; do + case "$h" in ''|\#*) continue ;; esac + one "$h" & + n=$((n + 1)) + [ $((n % PARALLEL)) -eq 0 ] && wait +done < "$HOSTS" +wait + +cat "$OUT"/.status.* 2>/dev/null > "$OUT/.all" || true +cat "$OUT"/.cve.* 2>/dev/null > "$OUT/.cves" || true +echo "nginx2root fleet detection ($n host(s))" +for v in impacted unknown "not impacted"; do + hs=$(awk -F'\t' -v V="$v" '$1==V{print $2}' "$OUT/.all" 2>/dev/null | sort | tr '\n' ' ') + printf ' %-13s %s\n' "$v:" "${hs:-(none)}" +done +errs=$(awk -F'\t' '$1 ~ /^error/{printf "%s(%s) ", $2, $1}' "$OUT/.all" 2>/dev/null) +[ -n "$errs" ] && printf ' %-13s %s\n' "error:" "$errs" +echo "impacted hosts by CVE" +for c in CVE-2026-123007 CVE-2026-123633 CVE-2026-121810 CVE-2026-123634 CVE-2026-113425; do + hs=$(awk -F'\t' -v C="$c" '$1==C && $2=="impacted"{print $3}' "$OUT/.cves" 2>/dev/null | sort | tr '\n' ' ') + printf ' %s: %s\n' "$c" "${hs:-(none)}" +done +echo "per-host output in $OUT/" +rm -f "$OUT"/.status.* "$OUT"/.cve.* 2>/dev/null + +if awk -F'\t' '$1=="impacted"{f=1} END{exit !f}' "$OUT/.all" 2>/dev/null; then exit 1; fi +if awk -F'\t' '$1=="unknown" || $1 ~ /^error/{f=1} END{exit !f}' "$OUT/.all" 2>/dev/null; then exit 2; fi +exit 0 diff --git a/nginx2root/linux/tests/run-tests.sh b/nginx2root/linux/tests/run-tests.sh new file mode 100755 index 0000000..725bb8b --- /dev/null +++ b/nginx2root/linux/tests/run-tests.sh @@ -0,0 +1,1023 @@ +#!/usr/bin/env bash +set -u + +HERE=$(cd "$(dirname "$0")" && pwd) +S="$HERE/../detect-nginx2root.sh" +REAL_VULN="" +REAL_FIXED="" +IMAGE="" +SCAN_IMAGE="" +while [ $# -gt 0 ]; do + case "$1" in + --detector) S=$2; shift ;; + --real-vulnerable) REAL_VULN=$2; shift ;; + --real-fixed) REAL_FIXED=$2; shift ;; + --container-image) IMAGE=$2; shift ;; + --scan-image) SCAN_IMAGE=$2; shift ;; + *) echo "unknown argument: $1" >&2; exit 3 ;; + esac + shift +done +[ -n "$SCAN_IMAGE" ] || SCAN_IMAGE=$IMAGE + +PASS=0 +FAIL=0 +WORK=$(mktemp -d) +PIDS="" +CONTAINERS="" + +cleanup() { + local p + for p in $PIDS; do + pkill -9 -P "$p" 2>/dev/null + kill -9 "$p" 2>/dev/null + done + for p in $CONTAINERS; do docker rm -f "$p" >/dev/null 2>&1; done + rm -rf "$WORK" +} +trap cleanup EXIT + +verdict() { + case "$1" in + 0) echo "not impacted" ;; + 1) echo "impacted" ;; + 2) echo "unknown" ;; + *) echo "" ;; + esac +} + +run() { + OUT=$("$@" 2>&1) + CODE=$? + LAST=$(printf '%s\n' "$OUT" | sed '/^[[:space:]]*$/d' | tail -1) +} + +has() { + local c + for c in "$@"; do + case "$c" in + =*) printf '%s\n' "$OUT" | grep -qxF -- "${c#=}" || return 1 ;; + !*) printf '%s\n' "$OUT" | grep -qF -- "${c#!}" && return 1 ;; + *) printf '%s\n' "$OUT" | grep -qF -- "$c" || return 1 ;; + esac + done + return 0 +} + +report() { + if [ "$1" -eq 1 ]; then + PASS=$((PASS + 1)) + echo "PASS $2" + else + FAIL=$((FAIL + 1)) + echo "FAIL $2 ($3)" + printf '%s\n' "$OUT" | sed 's/^/ | /' + fi +} + +check() { + local name=$1 want=$2 ok=1 n + shift 2 + [ "$CODE" = "$want" ] || ok=0 + if [ "$want" != 3 ]; then + [ "$LAST" = "$(verdict "$want")" ] || ok=0 + n=$(printf '%s\n' "$OUT" | grep -cE '^CVE-2026-[0-9]+: (impacted|not impacted|unknown)$') + [ "$n" = 5 ] || ok=0 + fi + has "$@" || ok=0 + if printf '%s\n' "$OUT" | grep -qE 'Broken pipe|write error|syntax error|unbound variable'; then ok=0; fi + report "$ok" "$name" "want exit $want '$(verdict "$want")' with [$*]; got exit $CODE" +} + +block() { + OUT=$(printf '%s\n' "$OUT" | awk -v p="(pid $1" 'index($0, p) { f = 1 } f && /^$/ { exit } f') +} + +check_block() { + local name=$1 ok=1 + shift + [ -n "$OUT" ] || ok=0 + has "$@" || ok=0 + report "$ok" "$name" "want a block with [$*]" +} + +ALL="--prefix=/usr/local/nginx --conf-path=/etc/nginx/nginx.conf --with-http_ssl_module --with-http_v2_module --with-http_v3_module --with-mail --with-mail_ssl_module --with-stream" + +mkbin() { + local f=$1 ver=$2 args=$3 m + shift 3 + { + printf '\177ELF\0\0junk\0' + printf '%s\n\0' "$ver" + [ "$args" = none ] || printf 'configure arguments: %s\n\0' "$args" + for m in "$@"; do + case "$m" in + old) printf 'could not cancel %%V resolving\0' ;; + new) printf 'mail auth http block write\0' ;; + esac + done + printf 'tail\0' + } > "$f" + chmod 755 "$f" +} + +det() { + local b=$1 c=$2 + shift 2 + run bash "$S" --no-procs --no-disk --no-kernel -b "$b" -c "$c" "$@" +} + +echo "bash: $BASH_VERSION" +echo "detector: $S" + +B="$WORK/bin" +C="$WORK/conf" +mkdir -p "$B" "$C" + +cat > "$C/h2.conf" <<'EOF' +events {} +http { + server { + listen 443 ssl http2; + location / { proxy_pass http://127.0.0.1:9000; } + } +} +EOF +cat > "$C/static.conf" <<'EOF' +events {} +http { server { listen 80; root /srv; } } +EOF +cat > "$C/quic.conf" <<'EOF' +events {} +http { + server { + listen 443 quic reuseport; + listen 443 ssl; + location / { proxy_pass http://127.0.0.1:9000; } + } +} +EOF + +echo "## 1. version logic" + +mkbin "$B/v1318" "nginx version: nginx/1.31.8" "$ALL" old +det "$B/v1318" "$C/h2.conf" +check "1.31.8 with http/2 and proxy_pass is impacted" 1 \ + "=CVE-2026-123007: impacted" "=CVE-2026-123633: impacted" "=CVE-2026-121810: not impacted" \ + "=CVE-2026-123634: not impacted" "=CVE-2026-113425: not impacted" \ + "unpatched resolver code in the binary" " => impacted (CVE-2026-123007, CVE-2026-123633)" + +mkbin "$B/v1319" "nginx version: nginx/1.31.9" "$ALL" +det "$B/v1319" "$C/h2.conf" +check "1.31.9 is fixed" 0 "has the fixes" "=CVE-2026-123007: not impacted" "!impacted (CVE" + +mkbin "$B/v1307" "nginx version: nginx/1.30.7" "$ALL" +det "$B/v1307" "$C/h2.conf" +check "1.30.7 is fixed" 0 "has the fixes" + +mkbin "$B/v13012" "nginx version: nginx/1.30.12" "$ALL" +det "$B/v13012" "$C/h2.conf" +check "1.30.12 is fixed" 0 "has the fixes" + +mkbin "$B/v1306" "nginx version: nginx/1.30.6" "$ALL" old +det "$B/v1306" "$C/h2.conf" +check "1.30.6 is impacted" 1 "=CVE-2026-123007: impacted" + +mkbin "$B/v1320" "nginx version: nginx/1.32.0" "$ALL" +det "$B/v1320" "$C/h2.conf" +check "1.32.0, a later branch, is fixed" 0 "has the fixes" + +mkbin "$B/v194" "nginx version: nginx/1.9.4" "$ALL" old +det "$B/v194" "$C/h2.conf" +check "1.9.4 predates both http/2 bugs" 0 "CVE-2026-123007: not impacted (not affected before 1.9.5)" \ + "CVE-2026-123633: not impacted (not affected before 1.19.7)" + +mkbin "$B/v1196" "nginx version: nginx/1.19.6" "$ALL" old +det "$B/v1196" "$C/h2.conf" +check "1.19.6 has the window bug but not the http/2 use-after-free" 1 \ + "=CVE-2026-123007: impacted" "=CVE-2026-123633: not impacted" "not affected before 1.19.7" + +mkbin "$B/v1249" "nginx version: nginx/1.24.9" "$ALL" old +det "$B/v1249" "$C/quic.conf" +check "1.24.9 predates the http/3 bug" 0 "CVE-2026-121810: not impacted (not affected before 1.25.0)" + +mkbin "$B/v1250" "nginx version: nginx/1.25.0" "$ALL" old +det "$B/v1250" "$C/quic.conf" +check "1.25.0 with quic and proxy_pass is impacted" 1 "=CVE-2026-121810: impacted" "=CVE-2026-123007: not impacted" + +mkbin "$B/v0530" "nginx version: nginx/0.5.30" none old +det "$B/v0530" "$C/h2.conf" +check "0.5.30 is older than every affected release" 0 "older than every affected release" + +mkbin "$B/backport" "nginx version: nginx/1.26.2" "$ALL" +det "$B/backport" "$C/h2.conf" +check "affected version without the unpatched string is unknown" 2 \ + "probably a backport" "=CVE-2026-123007: unknown" "=CVE-2026-121810: not impacted" + +mkbin "$B/debug" "nginx version: nginx/1.24.0 (Ubuntu)" "$ALL --with-debug" old +det "$B/debug" "$C/h2.conf" +check "debug build in the affected range is impacted" 1 "nginx/1.24.0 (Ubuntu)" \ + "a backport cannot be seen in a debug build" "=CVE-2026-123007: impacted" + +mkbin "$B/debugfixed" "nginx version: nginx/1.24.0 (Ubuntu)" "$ALL --with-debug" old new +det "$B/debugfixed" "$C/h2.conf" +check "debug build with a patched-only string is unknown" 2 "probably a backport" + +mkbin "$B/noargs" "nginx version: nginx/1.24.0" none old +det "$B/noargs" "$C/h2.conf" +check "unreadable build options stay impacted" 1 "build options unreadable" + +mkbin "$B/openresty" "nginx version: openresty/1.27.1.2" "$ALL" old +det "$B/openresty" "$C/h2.conf" +check "openresty is judged on its nginx core" 1 "openresty/1.27.1.2" "=CVE-2026-123007: impacted" + +mkbin "$B/openrestyfixed" "nginx version: openresty/1.31.9.1" "$ALL" +det "$B/openrestyfixed" "$C/h2.conf" +check "openresty on a fixed core" 0 "has the fixes" + +mkbin "$B/angie" "Angie version: Angie/1.8.0" "$ALL" old +det "$B/angie" "$C/h2.conf" +check "a fork with its own numbering is unknown" 2 "Angie/1.8.0" "fork with its own version numbers" \ + "=CVE-2026-123007: unknown" +det "$B/angie" "$C/static.conf" +check "a fork with nothing enabled is not impacted" 0 "=CVE-2026-123007: not impacted" + +{ + printf '\177ELF\0' + printf 'Tengine version: Tengine/3.1.0\n\0nginx version: nginx/1.24.0\n\0' + printf 'configure arguments: %s\n\0could not cancel %%V resolving\0' "$ALL" +} > "$B/tengine" +det "$B/tengine" "$C/h2.conf" +check "tengine is judged on the nginx version it reports" 1 "nginx/1.24.0 [Tengine/3.1.0]" + +mkbin "$B/plus" "nginx version: nginx/1.29.3 (nginx-plus-r36)" "$ALL" old +det "$B/plus" "$C/h2.conf" +check "NGINX Plus points at the F5 advisory" 1 "NGINX Plus, see the F5 advisory" + +printf 'not nginx at all\n' > "$B/other" +det "$B/other" "$C/h2.conf" +check "a file without a version string is unknown" 2 "no nginx version string" "=CVE-2026-113425: unknown" + +det "$B/does-not-exist" "$C/h2.conf" +check "a missing -b file is unknown" 2 "not a file" + +run bash "$S" --bogus +check "bad argument" 3 +run bash "$S" -b +check "missing option value" 3 +run bash "$S" -r "$WORK/no-such-dir" -b /x +check "missing --root directory" 3 + +run env NGINX2ROOT_FIXED_IN="1.24.1" bash "$S" --no-procs --no-disk --no-kernel -b "$B/debug" -c "$C/h2.conf" +check "NGINX2ROOT_FIXED_IN below the version" 1 +mkbin "$B/v1245" "nginx version: nginx/1.24.5" "$ALL" old +run env NGINX2ROOT_FIXED_IN="1.24.1" bash "$S" --no-procs --no-disk --no-kernel -b "$B/v1245" -c "$C/h2.conf" +check "NGINX2ROOT_FIXED_IN at or above the entry" 0 "has the fixes" + +det "$B/v1318" "$C/h2.conf" -q +n=$(printf '%s\n' "$OUT" | wc -l) +ok=1; [ "$n" -eq 6 ] || ok=0 +[ "$CODE" = 1 ] && [ "$LAST" = impacted ] || ok=0 +has "=CVE-2026-123007: impacted" "=CVE-2026-113425: not impacted" || ok=0 +report "$ok" "-q prints the five status lines and the verdict" "got $n lines, exit $CODE" + +det "$B/v1318" "$C/h2.conf" -b "$B/v1319" +check "two binaries: the worst state wins" 1 "=CVE-2026-123007: impacted" +n=$(printf '%s\n' "$OUT" | grep -c '^ => ') +report "$([ "$n" -eq 2 ] && echo 1 || echo 0)" "two binaries give two blocks" "got $n" + +echo "## 2. configuration logic" +V="$B/v1318" + +det "$V" "$C/static.conf" +check "plain site is not impacted" 0 "http/2 not enabled" "no quic listener" "no resolver in http{}" "no mail{} servers" + +cat > "$C/h2on.conf" <<'EOF' +events {} +http { server { listen 443 ssl; http2 on; } } +EOF +det "$V" "$C/h2on.conf" +check "http2 on in a server" 1 "CVE-2026-123007: impacted (http/2 enabled: http2 on)" \ + "=CVE-2026-123633: not impacted" "nothing passed upstream" + +cat > "$C/h2inherit.conf" <<'EOF' +events {} +http { + http2 on; + server { listen 443 ssl; } +} +EOF +det "$V" "$C/h2inherit.conf" +check "http2 on inherited from http{}" 1 "http2 on in http{}" + +cat > "$C/h2alloff.conf" <<'EOF' +events {} +http { + http2 on; + server { listen 443 ssl; http2 off; } + server { listen 8443 ssl; http2 off; } +} +EOF +det "$V" "$C/h2alloff.conf" +check "http2 on in http{} but off in every server" 0 "http/2 not enabled" + +cat > "$C/h2oneoff.conf" <<'EOF' +events {} +http { + http2 on; + server { listen 443 ssl; http2 off; } + server { listen 8443 ssl; } +} +EOF +det "$V" "$C/h2oneoff.conf" +check "http2 on in http{}, off in one server only" 1 "=CVE-2026-123007: impacted" + +cat > "$C/h2noservers.conf" <<'EOF' +events {} +http { http2 on; } +EOF +det "$V" "$C/h2noservers.conf" +check "http2 on with no server" 0 + +for p in grpc_pass fastcgi_pass uwsgi_pass scgi_pass memcached_pass; do + cat > "$C/pass.conf" < "$C/quicstatic.conf" <<'EOF' +events {} +http { server { listen 443 quic; listen 443 ssl; root /srv; } } +EOF +det "$V" "$C/quicstatic.conf" +check "quic with nothing passed upstream" 0 "quic on, nothing passed upstream" + +mkbin "$B/noh2" "nginx version: nginx/1.31.8" "--prefix=/usr/local/nginx --with-http_ssl_module --with-mail" old +det "$B/noh2" "$C/h2.conf" +check "http_v2 module not built" 0 "http_v2 module not built" "http_v3 module not built" + +cat > "$C/resolver.conf" <<'EOF' +events {} +http { + resolver 10.0.0.2 valid=30s; + server { + listen 80; + location / { mirror /mirror; proxy_pass http://$host; } + location = /mirror { internal; proxy_pass http://$host$request_uri; } + } +} +EOF +det "$V" "$C/resolver.conf" +check "resolver, variable proxy_pass and mirror" 1 "=CVE-2026-123634: impacted" \ + 'resolver 10.0.0.2 valid=30s, proxy_pass http://$host, mirror /mirror' "=CVE-2026-123007: not impacted" + +cat > "$C/resolvernosub.conf" <<'EOF' +events {} +http { + resolver 10.0.0.2; + server { listen 80; location / { mirror off; proxy_pass http://$host; } } +} +EOF +det "$V" "$C/resolvernosub.conf" +check "resolver and variable proxy_pass without a subrequest directive" 2 \ + "=CVE-2026-123634: unknown" "other subrequest sources not ruled out" + +cat > "$C/resolverfixed.conf" <<'EOF' +events {} +http { + resolver 10.0.0.2; + server { listen 80; location / { mirror /m; proxy_pass http://backend; } } +} +EOF +det "$V" "$C/resolverfixed.conf" +check "resolver with a fixed proxy_pass" 0 "resolver set, no *_pass with a variable" + +cat > "$C/resolverstapling.conf" <<'EOF' +events {} +http { + resolver 10.0.0.2; + server { listen 443 ssl; ssl_stapling on; ssl_stapling_verify on; root /srv; } +} +EOF +det "$V" "$C/resolverstapling.conf" +check "resolver used only for OCSP stapling" 0 "=CVE-2026-123634: not impacted" + +cat > "$C/resolvernjs.conf" <<'EOF' +events {} +http { + resolver 10.0.0.2; + js_import main from app.js; + server { listen 80; location / { js_content main.handler; } } +} +EOF +det "$V" "$C/resolvernjs.conf" +check "resolver with an njs handler and no variable proxy_pass is unknown" 2 \ + "=CVE-2026-123634: unknown" "js_content code may resolve names and make subrequests" + +cat > "$C/resolverlua.conf" <<'EOF' +events {} +http { + resolver 10.0.0.2; + server { listen 80; location / { content_by_lua_block { ngx.say("x") } } } +} +EOF +det "$V" "$C/resolverlua.conf" +check "resolver with a Lua handler and no variable proxy_pass is unknown" 2 \ + "=CVE-2026-123634: unknown" "content_by_lua_block code may resolve names" + +for d in "ssi on" "auth_request /auth" "add_before_body /hdr" "add_after_body /ftr"; do + cat > "$C/sub.conf" < "$C/mail.conf" <<'EOF' +events {} +mail { + auth_http 127.0.0.1:9000/auth; + server { listen 143; protocol imap; } + server { listen 25; protocol smtp; } +} +http { server { listen 80; } } +EOF +det "$V" "$C/mail.conf" +check "mail proxy" 1 "=CVE-2026-113425: impacted" "mail{} with 2 server(s), auth_http 127.0.0.1:9000/auth" \ + "=CVE-2026-123007: not impacted" + +mkbin "$B/nomail" "nginx version: nginx/1.31.8" "--prefix=/usr/local/nginx --with-http_v2_module" old +det "$B/nomail" "$C/mail.conf" +check "mail module not built" 0 "mail module not built" + +mkbin "$B/dynmail" "nginx version: nginx/1.31.8" "--prefix=/usr/local/nginx --with-mail=dynamic --with-http_v2_module" old +det "$B/dynmail" "$C/mail.conf" +check "mail built as a dynamic module" 1 "=CVE-2026-113425: impacted" + +cat > "$C/mailempty.conf" <<'EOF' +events {} +mail { auth_http 127.0.0.1:9000/auth; } +EOF +det "$V" "$C/mailempty.conf" +check "mail{} without a server" 0 "no mail{} servers" + +cat > "$C/stream.conf" <<'EOF' +events {} +stream { + server { listen 8443 http2; proxy_pass 10.0.0.5:443; } +} +http { server { listen 443 ssl http2; root /srv; } } +EOF +det "$V" "$C/stream.conf" +check "stream{} directives are not http ones" 1 "=CVE-2026-123007: impacted" "=CVE-2026-123633: not impacted" + +cat > "$C/comments.conf" <<'EOF' +events {} +http { + # listen 443 ssl http2; + server { + listen 80; # http2 on; + location /a { return 200 "listen 443 ssl http2; http2 on; proxy_pass x;"; } + location /b { set $x 'http2 on; } mail { server {'; } + #http2 on; + } +} +# mail { server { listen 25; } } +EOF +det "$V" "$C/comments.conf" +check "comments and quoted strings are not directives" 0 "http/2 not enabled" "no mail{} servers" + +cat > "$C/escapes.conf" <<'EOF' +events {} +http { + map $uri $x { "~^/a{2}" 1; default 0; } + server { + listen 80; + location ~ \.php$ { return 200 "a\"b; listen 443 http2;"; } + location ~* "^/x{1,3}/" { proxy_set_header X-A "v;{}#"; } + location /v { proxy_set_header Host ${host}; } + } + server { listen 443 ssl http2; } +} +EOF +det "$V" "$C/escapes.conf" +check "regexes, escapes and \${var} do not derail the tokenizer" 1 "listen 443 ssl http2" "!partly read" + +cat > "$C/lua.conf" <<'EOF' +events {} +http { + server { + listen 80; + location /lua { + content_by_lua_block { + -- a comment with a brace } and a quote " + local t = { a = "}", b = '{' } + local n = #t + local s = [[ listen 443 ssl http2; } ]] + --[[ long comment + http2 on; } ]] + ngx.say("mail { server { listen 25; } }") + } + } + } + server { listen 8443 ssl http2; } +} +EOF +det "$V" "$C/lua.conf" +check "Lua block bodies are skipped, directives after them are read" 1 \ + "http/2 enabled: listen 8443 ssl http2" "=CVE-2026-113425: not impacted" "!partly read" \ + "=CVE-2026-123633: unknown" "content_by_lua_block may open connections" + +cat > "$C/luaonly.conf" <<'EOF' +events {} +http { + server { + listen 80; + location / { content_by_lua_block { ngx.say("listen 443 ssl http2;") } } + } +} +EOF +det "$V" "$C/luaonly.conf" +check "text inside Lua does not enable anything" 0 "http/2 not enabled" + +mkdir -p "$C/inc/conf.d" "$C/inc/sites" "$C/inc/snippets" +cat > "$C/inc/nginx.conf" <<'EOF' +events {} +http { + include mime.types; + include conf.d/*.conf; + include "sites/site one.conf"; +} +EOF +printf 'types { text/html html; }\n' > "$C/inc/mime.types" +printf 'server { listen 80; include snippets/tls.conf; }\n' > "$C/inc/conf.d/a.conf" +printf 'server { listen 81; }\n' > "$C/inc/conf.d/b.conf" +printf 'server { listen 443 ssl http2; }\n' > "$C/inc/conf.d/ignored.conf.disabled" +printf 'location / { proxy_pass http://app; }\n' > "$C/inc/sites/site one.conf" +printf 'listen 8443 ssl http2;\n' > "$C/inc/snippets/tls.conf" +det "$V" "$C/inc/nginx.conf" +check "includes: wildcard, nested, quoted, relative to the main config" 1 \ + "http/2 enabled: listen 8443 ssl http2" "!partly read" +rm "$C/inc/snippets/tls.conf" +det "$V" "$C/inc/nginx.conf" +check "a missing include makes a negative unknown" 2 "=CVE-2026-123007: unknown" "partly read" \ + "part of the configuration could not be read" +printf 'listen 8443 ssl http2;\n' > "$C/inc/conf.d/c.conf" +sed -i 's/listen 81;/listen 81 ssl http2;/' "$C/inc/conf.d/b.conf" +det "$V" "$C/inc/nginx.conf" +check "a missing include does not hide what was found" 1 "=CVE-2026-123007: impacted" + +cat > "$C/midline.conf" < "$C/static-extra.conf" +det "$V" "$C/midline.conf" +check "an include that shares its line with other directives" 1 "http/2 enabled: listen 8443 ssl http2" "!partly read" + +det "$V" "$C/no-such.conf" +check "an unreadable config is unknown" 2 "cannot read $C/no-such.conf" "=CVE-2026-123007: unknown" + +printf 'events {}\r\nhttp {\r\n server {\r\n listen 443 ssl http2;\r\n }\r\n}\r\n' > "$C/crlf.conf" +det "$V" "$C/crlf.conf" +check "CRLF line endings" 1 "=CVE-2026-123007: impacted" + +cat > "$C/unbalanced.conf" <<'EOF' +events {} +http { server { listen 80; +EOF +det "$V" "$C/unbalanced.conf" +check "unbalanced braces make a negative unknown" 2 "partly read" + +{ + echo 'events {}' + echo 'http {' + i=0 + while [ "$i" -lt 4000 ]; do + echo " server { listen 80; server_name s$i.example; location / { root /srv/$i; } # c" + echo " }" + i=$((i + 1)) + done + echo ' server { listen 443 ssl http2; location / { proxy_pass http://app; } }' + echo '}' +} > "$C/big.conf" +t0=$(date +%s) +det "$V" "$C/big.conf" +t1=$(date +%s) +check "an 8000 line config" 1 "=CVE-2026-123633: impacted" +report "$([ $((t1 - t0)) -lt 60 ] && echo 1 || echo 0)" "an 8000 line config is read in under a minute" "took $((t1 - t0))s" + +OLDPWD_T=$PWD +cd "$C" || exit 1 +run bash "$S" --no-procs --no-disk --no-kernel -b "$V" -c h2.conf +cd "$OLDPWD_T" || exit 1 +check "a relative -c is relative to the current directory" 1 "=CVE-2026-123007: impacted" + +echo "## 3. --root, packages and changelogs" +R="$WORK/rootfs" +mkdir -p "$R/usr/sbin" "$R/etc/nginx/conf.d" "$R/data" "$R/var/lib/dpkg/info" "$R/usr/share/doc/nginx-core" +mkbin "$R/usr/sbin/nginx" "nginx version: nginx/1.24.0 (Ubuntu)" "$ALL --with-debug" old +cat > "$R/etc/nginx/nginx.conf" <<'EOF' +events {} +http { include /etc/nginx/conf.d/*.conf; } +EOF +ln -s /data/site.conf "$R/etc/nginx/conf.d/site.conf" +printf 'server { listen 443 ssl http2; location / { proxy_pass http://app; } }\n' > "$R/data/site.conf" +printf '/usr/sbin/nginx\n/etc/nginx\n' > "$R/var/lib/dpkg/info/nginx-core.list" +cat > "$R/var/lib/dpkg/status" <<'EOF' +Package: libc6 +Status: install ok installed +Version: 2.39-0ubuntu8 + +Package: nginx-core +Status: install ok installed +Maintainer: Ubuntu Developers +Version: 1.24.0-2ubuntu7.5 +EOF +rdet() { run bash "$S" --no-procs --no-disk --no-kernel -r "$R" -b /usr/sbin/nginx; } + +rdet +check "--root: package, default config path, absolute symlink kept inside the root" 1 \ + "package nginx-core=1.24.0-2ubuntu7.5" "http/2 enabled: listen 443 ssl http2" "!partly read" + +run env NGINX2ROOT_FIXED_PKGS="other=1 nginx-core=1.24.0-2ubuntu7.5" bash "$S" --no-procs --no-disk --no-kernel -r "$R" -b /usr/sbin/nginx +check "NGINX2ROOT_FIXED_PKGS exact match" 0 "package listed as fixed" + +run env NGINX2ROOT_FIXED_PKGS="nginx-core=1.24.0-2ubuntu7.9" bash "$S" --no-procs --no-disk --no-kernel -r "$R" -b /usr/sbin/nginx +check "NGINX2ROOT_FIXED_PKGS newer than installed" 1 + +if command -v dpkg >/dev/null 2>&1; then + run env NGINX2ROOT_FIXED_PKGS="nginx-core=1.24.0-2ubuntu7.3" bash "$S" --no-procs --no-disk --no-kernel -r "$R" -b /usr/sbin/nginx + check "NGINX2ROOT_FIXED_PKGS older than installed (dpkg ordering)" 0 "package listed as fixed" +else + echo "SKIP dpkg ordering (no dpkg here)" +fi + +printf 'nginx (1.24.0-2ubuntu7.5) noble-security; urgency=medium\n\n * SECURITY UPDATE: HTTP/2 window overflow\n - CVE-2026-123007\n' \ + | gzip > "$R/usr/share/doc/nginx-core/changelog.Debian.gz" +rdet +check "changelog names one CVE: only that one is cleared" 1 \ + "CVE-2026-123007: not impacted (fix named in the package changelog)" "=CVE-2026-123633: impacted" \ + "package changelog names CVE-2026-123007" + +printf 'nginx (1.24.0-2ubuntu7.6) noble-security; urgency=medium\n\n * SECURITY UPDATE\n - CVE-2026-123007 CVE-2026-123633 CVE-2026-121810\n - CVE-2026-123634\n - CVE-2026-113425\n' \ + | gzip > "$R/usr/share/doc/nginx-core/changelog.Debian.gz" +rdet +check "changelog names all five" 0 "fix named in the package changelog" + +printf 'nginx (1.24.0-2ubuntu7.5) noble; urgency=medium\n\n * CVE-2025-23419 and nothing else\n' \ + | gzip > "$R/usr/share/doc/nginx-core/changelog.Debian.gz" +rdet +check "changelog without these CVEs clears nothing" 1 "=CVE-2026-123007: impacted" "!package changelog names" + +rm -rf "$R/usr/share/doc/nginx-core" +mkdir -p "$R/usr/share/doc/nginx-common" +ln -s nginx-common "$R/usr/share/doc/nginx-core" +printf 'nginx (1.24.0-2ubuntu7.6) noble-security\n * CVE-2026-123007 CVE-2026-123633\n' \ + | gzip > "$R/usr/share/doc/nginx-common/changelog.Debian.gz" +rdet +check "changelog found through a linked doc directory" 0 "=CVE-2026-123633: not impacted" + +run bash "$S" --no-procs --no-disk --no-kernel -r "$R" -b /usr/sbin/missing +check "--root with a missing binary" 2 "not a file" + +A="$WORK/alpine" +mkdir -p "$A/usr/sbin" "$A/etc/nginx" "$A/lib/apk/db" +mkbin "$A/usr/sbin/nginx" "nginx version: nginx/1.26.2" "$ALL" old +cp "$C/h2.conf" "$A/etc/nginx/nginx.conf" +cat > "$A/lib/apk/db/installed" <<'EOF' +P:musl +V:1.2.5-r0 +F:lib +R:ld-musl-x86_64.so.1 + +P:nginx +V:1.26.2-r0 +m:Jakub Jirutka +F:etc/nginx +R:nginx.conf +F:usr/sbin +R:nginx +EOF +run bash "$S" --no-procs --no-disk --no-kernel -r "$A" -b /usr/sbin/nginx +check "apk package database" 1 "package nginx=1.26.2-r0" +run env NGINX2ROOT_FIXED_PKGS="nginx=1.26.2-r0" bash "$S" --no-procs --no-disk --no-kernel -r "$A" -b /usr/sbin/nginx +check "apk package listed as fixed" 0 "package listed as fixed" + +echo "## 4. running processes" +if [ "$(uname -s)" = Linux ]; then + P="$WORK/proc" + mkdir -p "$P/sbin" "$P/sbin2" "$P/pfx/conf" "$P/pfx2/conf" + cp "$C/h2.conf" "$P/pfx/conf/nginx.conf" + cp "$C/mail.conf" "$P/pfx2/conf/alt.conf" + cp "$C/static.conf" "$P/static.conf" + mkfifo "$P/fifo" + mkfake() { + cp "$(command -v bash)" "$1" + { + printf '\0nginx version: nginx/1.31.8\n\0' + printf 'configure arguments: --prefix=%s --conf-path=%s/conf/nginx.conf --with-http_v2_module --with-http_v3_module --with-mail\n\0' "$P/pfx" "$P/pfx" + printf 'could not cancel %%V resolving\0' + } >> "$1" + chmod 755 "$1" + } + FAKE="$P/sbin/nginx" + mkfake "$FAKE" + printf 'read -r _ < "$1"\n' > "$P/idle.sh" + cat > "$P/master.sh" <<'EOF' +( exec -a "nginx: worker process" "$N2R_FAKE" "$N2R_DIR/idle.sh" "$1" ) & +( exec -a "nginx: worker process is shutting down" "$N2R_FAKE" "$N2R_DIR/idle.sh" "$1" ) & +read -r _ < "$1" +EOF + start_master() { + local f=$1 a=$2 i + ( N2R_FAKE=$f N2R_DIR=$P exec -a "nginx: master process $f $a" "$f" "$P/master.sh" "$P/fifo" ) & + MPID=$! + PIDS="$PIDS $MPID" + for i in 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20; do + [ "$(pgrep -P "$MPID" 2>/dev/null | wc -l)" -ge 2 ] && return 0 + sleep 0.25 + done + echo "warning: master $MPID has no workers yet" + } + stop_master() { pkill -9 -P "$1" 2>/dev/null; kill -9 "$1" 2>/dev/null; wait "$1" 2>/dev/null; } + scan() { run bash "$S" --no-disk --no-kernel "$@"; } + + start_master "$FAKE" "-c $P/pfx/conf/nginx.conf" + scan + ALLOUT=$OUT + n=$(printf '%s\n' "$ALLOUT" | grep -c "^$FAKE (pid ") + report "$([ "$n" -eq 1 ] && echo 1 || echo 0)" "one block for the master, none for its workers" "got $n blocks" + block "$MPID" + check_block "titled master: binary from /proc, config from -c" "$FAKE (pid $MPID)" \ + "version: nginx/1.31.8" "CVE-2026-123007: impacted" "CVE-2026-123633: impacted" " => impacted" \ + "note: 1 old worker(s) still shutting down" "!replaced on disk" + stop_master "$MPID" + + start_master "$FAKE" "-p $P/pfx2 -c conf/alt.conf -g daemon off;" + scan + block "$MPID" + check_block "master with -p and a relative -c" "CVE-2026-113425: impacted" "CVE-2026-123007: not impacted" + stop_master "$MPID" + + start_master "$FAKE" "" + scan + block "$MPID" + check_block "master without -c uses the built-in config path" "CVE-2026-123007: impacted" + stop_master "$MPID" + + FAKE2="$P/sbin2/nginx" + mkfake "$FAKE2" + start_master "$FAKE2" "-c $P/pfx/conf/nginx.conf" + rm -f "$FAKE2" + scan + block "$MPID" + check_block "binary replaced on disk: the running copy is still read" "$FAKE2 (pid $MPID)" \ + "CVE-2026-123007: impacted" "note: the binary was replaced on disk" "Restart nginx" + stop_master "$MPID" + + "$FAKE" "$P/idle.sh" "$P/fifo" & + SPID=$! + PIDS="$PIDS $SPID" + sleep 0.5 + scan + block "$SPID" + check_block "untitled process (master_process off)" "$FAKE (pid $SPID)" "CVE-2026-123007: impacted" + kill -9 "$SPID" 2>/dev/null; wait "$SPID" 2>/dev/null + + "$FAKE" "$P/idle.sh" "$P/fifo" -t & + SPID=$! + PIDS="$PIDS $SPID" + sleep 0.5 + scan + block "$SPID" + report "$([ -z "$OUT" ] && echo 1 || echo 0)" "a one-shot nginx -t is not reported" "it was" + kill -9 "$SPID" 2>/dev/null; wait "$SPID" 2>/dev/null + + start_master "$FAKE" "-c $P/static.conf" + scan -b "$FAKE" -c "$P/pfx/conf/nginx.conf" + block "$MPID" + check_block "a running master keeps its own config when -c is given for -b" "CVE-2026-123007: not impacted" + stop_master "$MPID" + + run bash "$S" + ok=1 + case "$CODE" in 0|1|2) ;; *) ok=0 ;; esac + [ "$LAST" = "$(verdict "$CODE")" ] || ok=0 + has "host kernel" "sctp:" "this host" || ok=0 + report "$ok" "default full run: consistent exit code, verdict and kernel line" "exit $CODE, last '$LAST'" +else + echo "SKIP process tests (not Linux)" +fi + +echo "## 5. busybox tools" +if command -v busybox >/dev/null 2>&1; then + BB="$WORK/bb" + mkdir -p "$BB" + for t in awk grep sed tr cut cat readlink mktemp gzip head; do + busybox "$t" --help >/dev/null 2>&1 || true + ln -s "$(command -v busybox)" "$BB/$t" + done + bdet() { local b=$1 c=$2; shift 2; run env PATH="$BB:$PATH" bash "$S" --no-procs --no-disk --no-kernel -b "$b" -c "$c" "$@"; } + bdet "$V" "$C/h2.conf" + check "busybox: version and http/2" 1 "version: nginx/1.31.8" "=CVE-2026-123633: impacted" + bdet "$V" "$C/lua.conf" + check "busybox: Lua block" 1 "http/2 enabled: listen 8443 ssl http2" "=CVE-2026-113425: not impacted" + bdet "$V" "$C/escapes.conf" + check "busybox: escapes" 1 "!partly read" + bdet "$V" "$C/inc/nginx.conf" + check "busybox: includes" 1 "=CVE-2026-123007: impacted" + bdet "$V" "$C/mail.conf" + check "busybox: mail" 1 "=CVE-2026-113425: impacted" + bdet "$B/backport" "$C/h2.conf" + check "busybox: marker" 2 "probably a backport" + run env PATH="$BB:$PATH" bash "$S" --no-procs --no-disk --no-kernel -r "$R" -b /usr/sbin/nginx + check "busybox: --root, dpkg and gzip changelog" 0 "package nginx-core=1.24.0-2ubuntu7.5" "=CVE-2026-123633: not impacted" + run env PATH="$BB:$PATH" bash "$S" --no-procs --no-disk --no-kernel -r "$A" -b /usr/sbin/nginx + check "busybox: apk database" 1 "package nginx=1.26.2-r0" +else + echo "SKIP busybox tests (no busybox here)" +fi + +echo "## 6. real nginx binaries" +start_real() { + local bin=$1 d=$2 port=$3 i + mkdir -p "$d/conf" "$d/logs" "$d/tmp" + cat > "$d/conf/nginx.conf" </dev/null 2>&1 & + RPID=$! + PIDS="$PIDS $RPID" + for i in 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20; do + [ -s "$d/nginx.pid" ] && return 0 + sleep 0.25 + done + echo "warning: $bin did not start"; cat "$d/logs/error.log" 2>/dev/null +} +if [ -n "$REAL_VULN" ] && [ -x "$REAL_VULN" ]; then + want=$("$REAL_VULN" -v 2>&1 | sed 's/^nginx version: //') + start_real "$REAL_VULN" "$WORK/real1" 18471 + run bash "$S" --no-disk --no-kernel + block "$RPID" + check_block "real unpatched nginx, running" "(pid $RPID)" "version: $want" \ + "unpatched resolver code in the binary" "CVE-2026-123007: impacted (http/2 enabled: http2 on)" \ + "CVE-2026-123633: impacted" "CVE-2026-113425: not impacted" " => impacted" + kill "$RPID" 2>/dev/null; wait "$RPID" 2>/dev/null + det "$REAL_VULN" "$C/mail.conf" + check "real unpatched nginx, mail config" 1 "version: $want" "=CVE-2026-113425: impacted" +else + echo "SKIP real unpatched nginx (--real-vulnerable not given)" +fi +if [ -n "$REAL_FIXED" ] && [ -x "$REAL_FIXED" ]; then + want=$("$REAL_FIXED" -v 2>&1 | sed 's/^nginx version: //') + start_real "$REAL_FIXED" "$WORK/real2" 18472 + run bash "$S" --no-disk --no-kernel + block "$RPID" + check_block "real fixed nginx, running" "(pid $RPID)" "version: $want" "has the fixes" " => not impacted" + kill "$RPID" 2>/dev/null; wait "$RPID" 2>/dev/null + run env NGINX2ROOT_FIXED_IN="" bash "$S" --no-procs --no-disk --no-kernel -b "$REAL_FIXED" -c "$C/h2.conf" + check "real fixed nginx carries the patch marker" 2 "probably a backport" +else + echo "SKIP real fixed nginx (--real-fixed not given)" +fi + +echo "## 7. containers" +if [ -n "$IMAGE" ] && command -v docker >/dev/null 2>&1 && docker info >/dev/null 2>&1; then + CN="n2rt-$$" + cscan() { + run docker run --rm -i --privileged --pid=host "$@" "$SCAN_IMAGE" bash -s -- --no-disk < "$S" + } + pkgv=$(docker run --rm "$IMAGE" sh -c "dpkg-query -W -f='\${Version}' nginx 2>/dev/null" 2>/dev/null) + imgver=$(docker run --rm "$IMAGE" nginx -v 2>&1 | sed 's/^nginx version: //') + + docker run -d --name "$CN-stock" --hostname "$CN-stock" "$IMAGE" >/dev/null + CONTAINERS="$CONTAINERS $CN-stock" + docker create --name "$CN-h2" --hostname "$CN-h2" "$IMAGE" >/dev/null + CONTAINERS="$CONTAINERS $CN-h2" + cat > "$WORK/h2c.conf" <<'EOF' +server { + listen 8443 http2; + location / { proxy_pass http://127.0.0.1:9; } +} +EOF + docker cp "$WORK/h2c.conf" "$CN-h2:/etc/nginx/conf.d/h2c.conf" >/dev/null + docker start "$CN-h2" >/dev/null + sleep 2 + p1=$(docker inspect -f '{{.State.Pid}}' "$CN-stock") + p2=$(docker inspect -f '{{.State.Pid}}' "$CN-h2") + + cscan + FULL=$OUT + has "host kernel" "sctp:" + report "$([ $? -eq 0 ] && echo 1 || echo 0)" "container scan prints the kernel line" "missing" + OUT=$FULL; block "$p1" + check_block "stock container: read through /proc/PID/root, nothing enabled" \ + "/usr/sbin/nginx (pid $p1, container $CN-stock)" "version: $imgver" \ + "CVE-2026-123007: not impacted" " => not impacted" + [ -n "$pkgv" ] && { OUT=$FULL; block "$p1"; check_block "stock container: package from its dpkg database" "package nginx=$pkgv"; } + OUT=$FULL; block "$p2" + check_block "container with http/2 and proxy_pass" "(pid $p2, container $CN-h2)" \ + "CVE-2026-123007: impacted (http/2 enabled: listen 8443 http2)" "CVE-2026-123633: impacted" " => impacted" + + if [ -n "$pkgv" ]; then + cscan -e NGINX2ROOT_FIXED_PKGS="nginx=$pkgv" + block "$p2" + check_block "container package listed as fixed" "package listed as fixed" " => not impacted" + fi + + docker exec "$CN-h2" sh -c 'cp /usr/sbin/nginx /tmp/nginx && chmod 755 /tmp/nginx' >/dev/null 2>&1 + docker exec -d "$CN-h2" /tmp/nginx -c /etc/nginx/nginx.conf -g 'daemon off; pid /tmp/n.pid;' >/dev/null 2>&1 + before=$(docker exec "$CN-h2" sh -c 'ls /tmp | wc -l') + cscan + after=$(docker exec "$CN-h2" sh -c 'ls /tmp | wc -l') + report "$([ "$before" = "$after" ] && echo 1 || echo 0)" "scan leaves the container's /tmp untouched" "$before files before, $after after" + for c in "$CN-stock" "$CN-h2"; do docker rm -f "$c" >/dev/null 2>&1; done +else + echo "SKIP container tests (--container-image not given, or no docker)" +fi + +echo "## 8. ssh wrapper" +SSHW="$(dirname "$S")/ssh-nginx2root.sh" +if [ -f "$SSHW" ]; then + FB="$WORK/fakebin" + mkdir -p "$FB" + cat > "$FB/ssh" <&2; exit 255 ;; +esac +eval "exec \$cmd --no-procs --no-disk --no-kernel \"\\\$@\"" +EOF + chmod 755 "$FB/ssh" + printf '# fleet\nweb1\nweb2\n\nmail1\nodd1\npkg1\ndown1\n' > "$WORK/hosts.txt" + sshw() { run env PATH="$FB:$PATH" SUDO="" DETECT="$S" "$@"; } + + sshw sh "$SSHW" "$WORK/hosts.txt" "$WORK/results" + ok=1 + [ "$CODE" = 1 ] || ok=0 + has "nginx2root fleet detection (6 host(s))" "impacted: mail1 pkg1 web1" "unknown: odd1" \ + "not impacted: web2" "down1(error (ssh/connection))" \ + "CVE-2026-123007: pkg1 web1" "CVE-2026-123633: pkg1 web1" "CVE-2026-121810: (none)" \ + "CVE-2026-113425: mail1" || ok=0 + [ -s "$WORK/results/web1.txt" ] && grep -q '^impacted$' "$WORK/results/web1.txt" || ok=0 + report "$ok" "ssh wrapper: per-host files, roll-up by verdict and by CVE, exit 1" "exit $CODE" + + sshw NGINX2ROOT_FIXED_PKGS="other=1 nginx=1.26.2-r0" sh "$SSHW" "$WORK/hosts.txt" "$WORK/results-b" + ok=1 + has "impacted: mail1 web1" "not impacted: pkg1 web2" "CVE-2026-123007: web1" || ok=0 + grep -q 'package listed as fixed' "$WORK/results-b/pkg1.txt" || ok=0 + report "$ok" "ssh wrapper: NGINX2ROOT_FIXED_PKGS reaches the hosts" "exit $CODE" + + sshw NGINX2ROOT_FIXED_IN="1.31.8" sh "$SSHW" "$WORK/hosts.txt" "$WORK/results-c" + has "not impacted: mail1 web1 web2" + report "$([ $? -eq 0 ] && echo 1 || echo 0)" "ssh wrapper: NGINX2ROOT_FIXED_IN reaches the hosts" "exit $CODE" + + sshw NGINX2ROOT_FIXED_PKGS='nginx=1; touch /tmp/x' sh "$SSHW" "$WORK/hosts.txt" "$WORK/results-d" + report "$([ "$CODE" = 3 ] && echo 1 || echo 0)" "ssh wrapper: refuses a value it cannot pass safely" "exit $CODE" + + printf 'web2\n' > "$WORK/hosts2.txt" + sshw sh "$SSHW" "$WORK/hosts2.txt" "$WORK/results2" + report "$([ "$CODE" = 0 ] && echo 1 || echo 0)" "ssh wrapper: all hosts clean gives exit 0" "exit $CODE" + + printf 'web2\ndown1\n' > "$WORK/hosts3.txt" + sshw sh "$SSHW" "$WORK/hosts3.txt" "$WORK/results3" + report "$([ "$CODE" = 2 ] && echo 1 || echo 0)" "ssh wrapper: an unreachable host gives exit 2" "exit $CODE" +else + echo "SKIP ssh wrapper (not next to the detector)" +fi + +echo +echo "passed: $PASS failed: $FAIL" +[ "$FAIL" -eq 0 ]