瀏覽代碼

test: fix DNSSEC/identity probes (raw CHAOS query, host paths, tmpfs uid/gid); verify 19/19 green, 0 CVEs

Bob 1 月之前
父節點
當前提交
32de976950
共有 9 個文件被更改,包括 142 次插入 和 24 次删除
  1. 1 0
      .gitignore
  2. 9 0
      CHANGELOG.md
  3. 5 4
      README.md
  4. 1 1
      docker-compose.yml
  5. 2 1
      entrypoint.sh
  6. 56 0
      test/chaos-check.py
  7. 1 1
      test/dns-check.py
  8. 43 17
      test/run-tests.sh
  9. 24 0
      test/run.sh

+ 1 - 0
.gitignore

@@ -5,6 +5,7 @@
 # test fixtures (generated at test run)
 test/fixture-config-ok/
 test/fixture-config-bad/
+test/fixture-identity/
 
 # editor/OS
 .vscode/

+ 9 - 0
CHANGELOG.md

@@ -32,3 +32,12 @@ All notable changes to this project are documented here.
 - CVE-2026-58055 (nghttp2) + CVE-2026-85091 (zlib): library stack removed
   (dns-root-hints dropped; Unbound has root servers built in)
 - SUID-root binaries removed (shadow package dropped from runtime)
+
+### Verified
+- Trivy 0.76.1 scan of the built image: **0 CVEs** (CRITICAL/HIGH/MEDIUM/LOW),
+  vs. 4 HIGH + 4 MEDIUM for crazymax/unbound:latest
+- Full test suite: **19/19 passed** (build, healthcheck, version 1.26.2, DNS,
+  DNSSEC AD-bit, uid 1500, CapEff=0, no SUID, read-only root fs, tmpfs
+  run-dir, NoNewPrivs, seccomp, hide-identity + control, drop-in include
+  accept/reject, no curl, no shadow)
+- Image size: 70.4 MB vs. 93.8 MB (crazymax/unbound)

+ 5 - 4
README.md

@@ -94,13 +94,14 @@ The whole test suite runs inside Docker (runner = `docker:cli` container with
 the socket mounted; all assertions target containers):
 
 ```sh
-./test/run-tests.sh
+./test/run.sh
 ```
 
 Covers: build, healthcheck, version pin, DNS resolution, DNSSEC AD-bit
-verification, non-root uid, CapEff=0, no SUID binaries, read-only root fs,
-no-new-privileges, seccomp, hide-identity, drop-in include (accept + reject),
-attack surface (no curl, no shadow).
+verification (raw DO-bit queries), non-root uid, CapEff=0, no SUID binaries,
+read-only root fs, no-new-privileges, seccomp, hide-identity (with control
+container proving the identity is revealed when the flag is off), drop-in
+include (accept + reject), attack surface (no curl, no shadow).
 
 ## Migrate from crazymax/unbound
 

+ 1 - 1
docker-compose.yml

@@ -25,7 +25,7 @@ services:
     read_only: true
     tmpfs:
       - /tmp
-      - /run/unbound
+      - "/run/unbound:uid=1500,gid=1500"
     security_opt:
       - no-new-privileges:true
     cap_drop:

+ 2 - 1
entrypoint.sh

@@ -4,7 +4,8 @@ set -e
 # Copy the static root trust anchor into the writable run-dir.
 # unbound updates auto-trust-anchor-file in place (RFC5011 rollover), so it
 # must live on a writable filesystem (tmpfs when running with read_only).
-cp -f /etc/unbound/root.key /run/unbound/root.key
+# Anchor ships in the dnssec-root package at /usr/share/dnssec-root/.
+cp -f /usr/share/dnssec-root/trusted-key.key /run/unbound/root.key
 
 # Fail fast on broken drop-in configs from /config before starting.
 unbound-checkconf /etc/unbound/unbound.conf

+ 56 - 0
test/chaos-check.py

@@ -0,0 +1,56 @@
+# Python container probe: CHAOS TXT query (identity/version leak check).
+# Usage: python chaos-check.py <server> <port> <name> <expect>
+#   expect: "empty" -> answer must contain no TXT data
+#   expect: <string> -> answer must contain a TXT with this substring
+import socket
+import struct
+import sys
+
+server = sys.argv[1]
+port = int(sys.argv[2])
+name = sys.argv[3]
+expect = sys.argv[4]
+
+header = struct.pack(">HHHHHH", 0x1234, 0x0100, 1, 0, 0, 0)
+qname = b"".join(bytes([len(p)]) + p.encode() for p in name.rstrip(".").split(".")) + b"\x00"
+q = header + qname + struct.pack(">HH", 16, 3)  # type TXT, class CHAOS
+
+
+def skip_name(buf, pos):
+    while True:
+        ln = buf[pos]
+        if ln == 0:
+            return pos + 1
+        if ln & 0xC0 == 0xC0:
+            return pos + 2
+        pos += 1 + ln
+
+
+s = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
+s.settimeout(10)
+s.sendto(q, (server, port))
+data, _ = s.recvfrom(4096)
+s.close()
+
+an = struct.unpack(">H", data[6:8])[0]
+pos = 12 + len(qname) + 4  # header + question
+texts = []
+for _ in range(an):
+    pos = skip_name(data, pos)
+    rtype = struct.unpack(">H", data[pos : pos + 2])[0]
+    rdlen = struct.unpack(">H", data[pos + 8 : pos + 10])[0]
+    rdata = data[pos + 10 : pos + 10 + rdlen]
+    pos += 10 + rdlen
+    if rtype == 16:  # TXT
+        p = 0
+        while p < len(rdata):
+            ln = rdata[p]
+            texts.append(rdata[p + 1 : p + 1 + ln].decode(errors="replace"))
+            p += 1 + ln
+
+if expect == "empty":
+    good = len(texts) == 0
+else:
+    good = any(expect in t for t in texts)
+print(f"{name} CHAOS TXT: texts={texts} -> {'PASS' if good else 'FAIL'}")
+sys.exit(0 if good else 1)

+ 1 - 1
test/dns-check.py

@@ -11,7 +11,7 @@ port = int(sys.argv[2]) if len(sys.argv) > 2 else 5053
 def query(name, do_bit):
     flags = 0x0100 | (0x0020 if do_bit else 0)  # RD (+ DO)
     header = struct.pack(">HHHHHH", 0x1234, flags, 1, 0, 0, 0)
-    qname = b"".join(bytes([len(p)]) + p.encode() for p in name.split(".")) + b"\x00"
+    qname = b"".join(bytes([len(p)]) + p.encode() for p in name.rstrip(".").split(".")) + b"\x00"
     q = header + qname + struct.pack(">HH", 1, 1)
     s = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
     s.settimeout(10)

+ 43 - 17
test/run-tests.sh

@@ -4,9 +4,10 @@
 # Runs fully inside Docker: this script is executed in a docker:cli container
 # with the Docker socket mounted; every assertion targets containers.
 #
-# Usage (from repo root):
-#   docker run --rm -v /var/run/docker.sock:/var/run/docker.sock \
-#     -v "$(pwd):/work" -w /work docker:cli sh test/run-tests.sh
+# Usage:
+#   ./test/run.sh   (host wrapper; recommended)
+#   or directly in a docker:cli container with the socket + repo mounted
+#   at the same absolute path as on the host (REPO_PATH set).
 
 set -u
 
@@ -14,9 +15,12 @@ IMAGE="unbound-sec:test"
 C1="ubsec-t1"          # main hardened container
 C2="ubsec-t2"          # valid drop-in container
 C3="ubsec-t3"          # broken drop-in container
+C4="ubsec-t4"          # identity control container
 NET="ubsec-testnet"
-CFG1="$(pwd)/test/fixture-config-ok"
-CFG2="$(pwd)/test/fixture-config-bad"
+# Fixtures must use HOST paths (bind mounts are resolved by the daemon).
+# run.sh sets REPO_PATH to the host repo path and pre-creates the fixtures.
+CFG1="${REPO_PATH:-$(pwd)}/test/fixture-config-ok"
+CFG2="${REPO_PATH:-$(pwd)}/test/fixture-config-bad"
 PASS=0
 FAIL=0
 
@@ -25,8 +29,9 @@ ok()   { PASS=$((PASS+1)); say "  [PASS] $1"; }
 bad()  { FAIL=$((FAIL+1)); say "  [FAIL] $1"; }
 
 cleanup() {
-  docker rm -f "$C1" "$C2" "$C3" >/dev/null 2>&1
+  docker rm -f "$C1" "$C2" "$C3" "$C4" >/dev/null 2>&1
   docker network rm "$NET" >/dev/null 2>&1
+  rm -rf "$(pwd)/test/fixture-identity" 2>/dev/null
 }
 trap cleanup EXIT
 
@@ -46,7 +51,7 @@ docker network create "$NET" >/dev/null
 docker run -d --name "$C1" --network "$NET" \
   -p 5053:5053/udp -p 5053:5053/tcp \
   --read-only \
-  --tmpfs /tmp --tmpfs /run/unbound \
+  --tmpfs /tmp --tmpfs "/run/unbound:uid=1500,gid=1500" \
   --security-opt no-new-privileges:true \
   --cap-drop ALL \
   "$IMAGE" >/dev/null
@@ -88,8 +93,11 @@ else
 fi
 
 say "== 6. DNSSEC (AD bit, DO query) =="
-if docker run --rm --network "$NET" -v "$(pwd)/test/dns-check.py:/probe.py:ro" python:3.20-alpine python /probe.py unbound 5053 | grep -q "FAIL\|Traceback"; then
+probe_out="$(docker run --rm --network "$NET" -v "$(pwd)/test/dns-check.py:/probe.py:ro" python:alpine python /probe.py "$C1" 5053 2>&1)"
+probe_rc=$?
+if [ $probe_rc -ne 0 ] || printf '%s' "$probe_out" | grep -q "FAIL"; then
   bad "DNSSEC AD-bit probe"
+  printf '%s\n' "$probe_out" | sed 's/^/    /'
 else
   ok "DNSSEC AD-bit probe"
 fi
@@ -145,17 +153,35 @@ else
 fi
 
 say "== 12. hide-identity / hide-version =="
-idout="$(docker exec "$C1" drill -p 5053 id.server. CHAOS TXT @127.0.0.1 2>/dev/null | grep -oE '"[^"]*"' | tr -d '"')"
-case "$idout" in
-  *1.26*) bad "identity leaks version ($idout)";;
-  "")     bad "identity query returned nothing";;
-  *)      ok "identity does not leak version ($idout)";;
-esac
+id_out="$(docker run --rm --network "$NET" -v "$(pwd)/test/chaos-check.py:/probe.py:ro" python:alpine python /probe.py "$C1" 5053 id.server. empty 2>&1)"
+id_rc=$?
+if [ $id_rc -ne 0 ] || printf '%s' "$id_out" | grep -q "FAIL"; then
+  bad "hide-identity: id.server. answer is empty"
+  printf '%s\n' "$id_out" | sed 's/^/    /'
+else
+  ok "hide-identity: id.server. answer is empty"
+fi
+
+# Control: same image with hide-identity switched off must reveal the identity.
+CFG_ID="${REPO_PATH:-$(pwd)}/test/fixture-identity"
+mkdir -p "$CFG_ID"
+printf 'server:\n  hide-identity: no\n' > "$CFG_ID/00-identity.conf"
+docker run -d --name "$C4" --network "$NET" -v "$CFG_ID:/config:ro" "$IMAGE" >/dev/null
+sleep 3
+id2_out="$(docker run --rm --network "$NET" -v "$(pwd)/test/chaos-check.py:/probe.py:ro" python:alpine python /probe.py "$C4" 5053 id.server. unbound-sec 2>&1)"
+id2_rc=$?
+if [ $id2_rc -ne 0 ] || printf '%s' "$id2_out" | grep -q "FAIL"; then
+  bad "identity control: hide-identity:no reveals identity"
+  printf '%s\n' "$id2_out" | sed 's/^/    /'
+else
+  ok "identity control: hide-identity:no reveals identity"
+fi
 
 say "== 13. drop-in config include =="
-mkdir -p "$CFG1" "$CFG2"
-printf 'server:\n  verbosity: 2\n' > "$CFG1/99-test.conf"
-printf 'server:\n  this-is-not-a-valid-option: 1\n' > "$CFG2/99-broken.conf"
+# Fixtures are created on the host by test/run.sh (host paths required).
+if [ ! -f "$CFG1/99-test.conf" ] || [ ! -f "$CFG2/99-broken.conf" ]; then
+  bad "test fixtures missing (run via test/run.sh)"
+fi
 
 docker run -d --name "$C2" --network "$NET" -v "$CFG1:/config:ro" "$IMAGE" >/dev/null
 sleep 3

+ 24 - 0
test/run.sh

@@ -0,0 +1,24 @@
+#!/bin/sh
+# Host-side wrapper for the unbound-sec test suite.
+#
+# Creates test fixtures, then runs the suite inside a docker:cli container
+# (Docker socket mounted) so every test target is a container.
+#
+# Usage (from repo root): ./test/run.sh
+set -eu
+
+REPO="$(cd "$(dirname "$0")/.." && pwd)"
+
+rm -rf "$REPO/test/fixture-config-ok" "$REPO/test/fixture-config-bad"
+mkdir -p "$REPO/test/fixture-config-ok" "$REPO/test/fixture-config-bad"
+printf 'server:\n  verbosity: 2\n' > "$REPO/test/fixture-config-ok/99-test.conf"
+printf 'server:\n  this-is-not-a-valid-option: 1\n' > "$REPO/test/fixture-config-bad/99-broken.conf"
+
+# Mount the repo at the same absolute path inside the runner so that
+# bind mounts for the fixture dirs resolve on the host side.
+docker run --rm \
+  -v /var/run/docker.sock:/var/run/docker.sock \
+  -v "$REPO":"$REPO" \
+  -w "$REPO" \
+  -e REPO_PATH="$REPO" \
+  docker:cli sh test/run-tests.sh