run-tests.sh 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516
  1. #!/bin/sh
  2. # unbound-sec test suite.
  3. #
  4. # Runs fully inside Docker: this script is executed in a docker:cli container
  5. # with the Docker socket mounted; every assertion targets containers.
  6. #
  7. # Usage:
  8. # ./test/run.sh (host wrapper; recommended)
  9. # or directly in a docker:cli container with the socket + repo mounted
  10. # at the same absolute path as on the host (REPO_PATH set).
  11. set -u
  12. IMAGE="unbound-sec:test"
  13. C1="ubsec-t1" # main hardened container
  14. C2="ubsec-t2" # valid drop-in container
  15. C3="ubsec-t3" # broken drop-in container
  16. C4="ubsec-t4" # identity control container
  17. C5="ubsec-t5" # anchor persistence container
  18. C6="ubsec-t6" # DNSSEC negative-test unbound (forward-zone)
  19. RESP="ubsec-t6-resp" # static DNS responder for the negative test
  20. NET="ubsec-testnet"
  21. # The compose template is tested under a DEDICATED project name so the test
  22. # never touches a production deployment of the same template on this daemon
  23. # (its own container, network, and volume unbound-sec-test_unbound_anchor).
  24. PROJ_TEST="unbound-sec-test"
  25. C6_CONT="${PROJ_TEST}-unbound-1" # compose auto-name for the test project
  26. # Fixtures must use HOST paths (bind mounts are resolved by the daemon).
  27. # run.sh sets REPO_PATH to the host repo path and pre-creates the fixtures.
  28. CFG1="${REPO_PATH:-$(pwd)}/test/fixture-config-ok"
  29. CFG2="${REPO_PATH:-$(pwd)}/test/fixture-config-bad"
  30. ANCHOR_DIR="${REPO_PATH:-$(pwd)}/test/fixture-anchor"
  31. PASS=0
  32. FAIL=0
  33. NOTVERIFIED=0 # checks that could not be run (infrastructure, e.g. timeout)
  34. say() { printf '%s\n' "$*"; }
  35. ok() { PASS=$((PASS+1)); say " [PASS] $1"; }
  36. bad() { FAIL=$((FAIL+1)); say " [FAIL] $1"; }
  37. # A security probe that could not be executed (timeout / network) — NOT a
  38. # content finding, but must be visible in the result.
  39. nv() { NOTVERIFIED=$((NOTVERIFIED+1)); say " [NOT VERIFIED] $1"; }
  40. # Resolve a container's IP on $NET so probes can address it directly
  41. # (avoids depending on Docker's embedded DNS inside the probe container).
  42. ip_of() { docker inspect -f '{{(index .NetworkSettings.Networks "'"$NET"').IPAddress}}' "$1" 2>/dev/null; }
  43. # Test-only compose invocation (dedicated project + test-image override;
  44. # see test/compose.test.yml). A function (not an array) for POSIX sh.
  45. compose_test() {
  46. (cd "${REPO_PATH:-$(pwd)}" && docker compose -p "$PROJ_TEST" \
  47. -f docker-compose.yml -f test/compose.test.yml "$@")
  48. }
  49. cleanup() {
  50. # Remove ONLY resources this test run created. The fixed container name
  51. # "unbound" is gone from the template, so we no longer rm a name that a
  52. # production deployment might use.
  53. docker rm -f "$C1" "$C2" "$C3" "$C4" "$C5" "$C6" "$RESP" >/dev/null 2>&1
  54. compose_test down --remove-orphans --volumes >/dev/null 2>&1
  55. docker network rm "$NET" >/dev/null 2>&1
  56. docker network rm "ubsec-negnet" >/dev/null 2>&1
  57. rm -rf "$(pwd)/test/fixture-identity" 2>/dev/null
  58. rm -rf "$(pwd)/test/fixture-broken-fwd" 2>/dev/null
  59. rm -f "$(pwd)/test/.broken-zone.bin" 2>/dev/null
  60. rm -rf "$ANCHOR_DIR" 2>/dev/null
  61. rm -f "$(pwd)/test/build.log" 2>/dev/null
  62. }
  63. trap cleanup EXIT
  64. say "== 0. cleanup old artifacts =="
  65. cleanup
  66. say "== 1. build image =="
  67. BUILDLOG="$(pwd)/test/build.log"
  68. # CACHEBUST forces the src stages (SHA256 + GPG verification) to re-run on
  69. # every test run so the build log always contains fresh evidence; the slow
  70. # compile stages stay cached.
  71. if docker build --build-arg CACHEBUST="$(date +%s)" -t "$IMAGE" . >"$BUILDLOG" 2>&1; then
  72. ok "docker build"
  73. # NOTE: we deliberately do NOT tag unbound-sec:latest here. The compose
  74. # template is exercised through test/compose.test.yml, which pins the test
  75. # image — so the production tag is left untouched.
  76. else
  77. bad "docker build"
  78. tail -15 "$BUILDLOG"
  79. say "ABORT: build failed"; exit 1
  80. fi
  81. say "== 2. start hardened container =="
  82. docker network create "$NET" >/dev/null
  83. docker run -d --name "$C1" --network "$NET" \
  84. -p 5053:5053/udp -p 5053:5053/tcp \
  85. --read-only \
  86. --tmpfs /tmp --tmpfs "/run/unbound:uid=1500,gid=1500" \
  87. --tmpfs "/var/unbound:uid=1500,gid=1500" \
  88. --security-opt no-new-privileges:true \
  89. --cap-drop ALL \
  90. "$IMAGE" >/dev/null
  91. sleep 3
  92. if [ "$(docker inspect -f '{{.State.Running}}' "$C1" 2>/dev/null)" = "true" ]; then
  93. ok "container running"
  94. else
  95. bad "container running"; docker logs "$C1" 2>&1 | tail -5
  96. fi
  97. say "== 3. healthcheck =="
  98. healthy="no"
  99. i=0
  100. # WSL/9p container startup can be slow; give it up to 120s.
  101. while [ $i -lt 20 ]; do
  102. h="$(docker inspect -f '{{.State.Health.Status}}' "$C1" 2>/dev/null)"
  103. [ "$h" = "healthy" ] && healthy="yes" && break
  104. sleep 6
  105. i=$((i+1))
  106. done
  107. if [ "$healthy" = "yes" ]; then
  108. ok "healthcheck healthy"
  109. else
  110. bad "healthcheck healthy (status=$h)"
  111. fi
  112. say "== 4. version =="
  113. v="$(docker exec "$C1" unbound -V 2>/dev/null | head -1)"
  114. case "$v" in
  115. *1.26.2*) ok "unbound version 1.26.2 ($v)";;
  116. *) bad "unbound version 1.26.2 (got: $v)";;
  117. esac
  118. say "== 5. DNS resolution =="
  119. if docker exec "$C1" drill -p 5053 example.com @127.0.0.1 2>/dev/null | grep -q "example.com"; then
  120. ok "drill resolves example.com"
  121. else
  122. bad "drill resolves example.com"
  123. fi
  124. say "== 6. DNSSEC (correct EDNS-DO query) =="
  125. # Primary check: drill -D sets the EDNS DO bit; a validating resolver must
  126. # answer with the AD (authenticated data) bit set. Loopback via docker exec
  127. # is reliable (no external overlay-network dependency).
  128. ad_out="$(docker exec "$C1" drill -p 5053 -D example.com @127.0.0.1 2>/dev/null)"
  129. if printf '%s' "$ad_out" | grep -q "rcode: NOERROR" \
  130. && printf '%s' "$ad_out" | grep -qE 'flags:.*\bad\b' \
  131. && printf '%s' "$ad_out" | grep -q "example.com"; then
  132. ok "drill -D: signed zone answers with AD bit (EDNS DO)"
  133. else
  134. bad "drill -D: signed zone answers with AD bit (EDNS DO)"
  135. printf '%s\n' "$ad_out" | sed 's/^/ /'
  136. fi
  137. # Secondary check: raw EDNS-0 DO probe from an external container (also
  138. # verifies the no-DO -> no-AD behavior). The probe distinguishes content
  139. # deviations (exit 1 -> FAIL) from infrastructure timeouts (exit 2 -> NOT
  140. # VERIFIED). The loopback check above is the deterministic primary proof.
  141. probe_out="$(docker run --rm --network "$NET" -v "$(pwd)/test/dns-check.py:/probe.py:ro" python:alpine python /probe.py "$(ip_of "$C1")" 5053 2>&1)"
  142. probe_rc=$?
  143. case $probe_rc in
  144. 0) ok "external EDNS-DO probe (AD=1 with DO, AD=0 without)";;
  145. 1) bad "external EDNS-DO probe: CONTENT deviation"
  146. printf '%s\n' "$probe_out" | sed 's/^/ /';;
  147. *) nv "external EDNS-DO probe (timeout; loopback check is the proof)"
  148. printf '%s\n' "$probe_out" | sed 's/^/ /';;
  149. esac
  150. say "== 7. non-root user =="
  151. uid="$(docker exec "$C1" sh -c 'id -u' 2>/dev/null)"
  152. if [ "$uid" = "1500" ]; then
  153. ok "runs as uid 1500"
  154. else
  155. bad "runs as uid 1500 (got: $uid)"
  156. fi
  157. say "== 8. capabilities =="
  158. capeff="$(docker exec "$C1" sh -c 'grep ^CapEff /proc/self/status' 2>/dev/null | awk '{print $2}')"
  159. if [ "$capeff" = "0000000000000000" ]; then
  160. ok "CapEff = 0"
  161. else
  162. bad "CapEff = 0 (got: $capeff)"
  163. fi
  164. say "== 9. no SUID binaries =="
  165. suid="$(docker exec "$C1" sh -c 'find / -perm -4000 -type f 2>/dev/null' 2>/dev/null)"
  166. if [ -z "$suid" ]; then
  167. ok "no SUID-root binaries"
  168. else
  169. bad "no SUID-root binaries (found: $suid)"
  170. fi
  171. say "== 10. read-only root filesystem =="
  172. if docker exec "$C1" sh -c 'touch /usr/writetest 2>/dev/null'; then
  173. bad "root fs is read-only (write to /usr succeeded)"
  174. else
  175. ok "root fs is read-only"
  176. fi
  177. if docker exec "$C1" sh -c 'touch /run/unbound/writetest && rm /run/unbound/writetest' 2>/dev/null; then
  178. ok "/run/unbound writable (tmpfs)"
  179. else
  180. bad "/run/unbound writable (tmpfs)"
  181. fi
  182. say "== 11. no-new-privileges + seccomp =="
  183. nnp="$(docker exec "$C1" sh -c 'grep ^NoNewPrivs /proc/self/status' 2>/dev/null | awk '{print $2}')"
  184. if [ "$nnp" = "1" ]; then
  185. ok "NoNewPrivs = 1"
  186. else
  187. bad "NoNewPrivs = 1 (got: $nnp)"
  188. fi
  189. sc="$(docker exec "$C1" sh -c 'grep ^Seccomp: /proc/self/status' 2>/dev/null | awk '{print $2}')"
  190. if [ "$sc" = "2" ]; then
  191. ok "seccomp filter active"
  192. else
  193. bad "seccomp filter active (got: $sc)"
  194. fi
  195. say "== 12. hide-identity / hide-version (CHAOS) =="
  196. # CHAOS id.server/version.server probes. The Python probe distinguishes
  197. # content deviations (exit 1 -> FAIL, e.g. an identity string leaked) from
  198. # infrastructure timeouts (exit 2 -> NOT VERIFIED). A leaked identity is a
  199. # real finding and MUST fail the run; a network hiccup is visible but not a
  200. # failure. The config value is asserted separately below (deterministic).
  201. id_out="$(docker run --rm --network "$NET" -v "$(pwd)/test/chaos-check.py:/probe.py:ro" python:alpine python /probe.py "$(ip_of "$C1")" 5053 id.server. empty 2>&1)"
  202. id_rc=$?
  203. case $id_rc in
  204. 0) ok "hide-identity: id.server. answer is empty";;
  205. 1) bad "hide-identity: id.server. answer is empty (CONTENT deviation)"
  206. printf '%s\n' "$id_out" | sed 's/^/ /';;
  207. *) nv "hide-identity CHAOS probe (timeout; config asserted below)"
  208. printf '%s\n' "$id_out" | sed 's/^/ /';;
  209. esac
  210. # Config-level assertion (deterministic): the base config must set it.
  211. if docker exec "$C1" grep -q "hide-identity: yes" /etc/unbound/unbound.conf 2>/dev/null; then
  212. ok "config: hide-identity: yes in base config"
  213. else
  214. bad "config: hide-identity: yes in base config"
  215. fi
  216. # Control: same image with hide-identity switched off must reveal the identity.
  217. CFG_ID="${REPO_PATH:-$(pwd)}/test/fixture-identity"
  218. mkdir -p "$CFG_ID"
  219. printf 'server:\n hide-identity: no\n' > "$CFG_ID/00-identity.conf"
  220. docker run -d --name "$C4" --network "$NET" -v "$CFG_ID:/config:ro" "$IMAGE" >/dev/null
  221. sleep 3
  222. id2_out="$(docker run --rm --network "$NET" -v "$(pwd)/test/chaos-check.py:/probe.py:ro" python:alpine python /probe.py "$(ip_of "$C4")" 5053 id.server. unbound-sec 2>&1)"
  223. id2_rc=$?
  224. case $id2_rc in
  225. 0) ok "identity control: hide-identity:no reveals identity";;
  226. 1) bad "identity control: hide-identity:no reveals identity (CONTENT deviation)"
  227. printf '%s\n' "$id2_out" | sed 's/^/ /';;
  228. *) nv "identity-control CHAOS probe (timeout)"
  229. printf '%s\n' "$id2_out" | sed 's/^/ /';;
  230. esac
  231. say "== 13. drop-in config include =="
  232. # Fixtures are created on the host by test/run.sh (host paths required).
  233. if [ ! -f "$CFG1/99-test.conf" ] || [ ! -f "$CFG2/99-broken.conf" ]; then
  234. bad "test fixtures missing (run via test/run.sh)"
  235. fi
  236. docker run -d --name "$C2" --network "$NET" -v "$CFG1:/config:ro" "$IMAGE" >/dev/null
  237. sleep 3
  238. if [ "$(docker inspect -f '{{.State.Running}}' "$C2" 2>/dev/null)" = "true" ]; then
  239. ok "valid drop-in config accepted (include works)"
  240. else
  241. bad "valid drop-in config accepted"
  242. fi
  243. docker run -d --name "$C3" --network "$NET" -v "$CFG2:/config:ro" "$IMAGE" >/dev/null
  244. sleep 3
  245. if [ "$(docker inspect -f '{{.State.Running}}' "$C3" 2>/dev/null)" = "false" ]; then
  246. ok "broken drop-in config rejects start (checkconf)"
  247. else
  248. bad "broken drop-in config rejects start"
  249. fi
  250. say "== 14. package attack surface =="
  251. has_curl="$(docker exec "$C1" sh -c 'test -x /usr/bin/curl && echo yes' 2>/dev/null)"
  252. if [ "$has_curl" = "yes" ]; then
  253. bad "curl not in runtime image"
  254. else
  255. ok "no curl in runtime image"
  256. fi
  257. has_shadow="$(docker exec "$C1" sh -c 'test -x /usr/bin/chsh && echo yes' 2>/dev/null)"
  258. if [ "$has_shadow" = "yes" ]; then
  259. bad "shadow package not removed"
  260. else
  261. ok "shadow package removed"
  262. fi
  263. say "== 15. supply chain: GPG + SHA256 of source tarballs =="
  264. sigs="$(grep -c "Good signature" "$BUILDLOG" 2>/dev/null)"
  265. if [ "$sigs" -ge 2 ]; then
  266. ok "GPG: Good signature x$sigs (unbound + ldns tarballs)"
  267. else
  268. bad "GPG: Good signature (found: $sigs, need >= 2)"
  269. fi
  270. if grep -E "unbound\.tar\.gz: OK|ldns\.tar\.gz: OK" "$BUILDLOG" >/dev/null 2>&1; then
  271. ok "SHA256: tarball checksums OK"
  272. else
  273. bad "SHA256: tarball checksums OK"
  274. fi
  275. say "== 16. binary hardening: PIE =="
  276. # ELF e_type: 3 = ET_DYN (PIE/shared). Read from the final binary itself,
  277. # so this works even on cached builds.
  278. etype="$(docker exec "$C1" sh -c 'od -An -N32 -td2 /usr/sbin/unbound' 2>/dev/null | awk 'NR==2{print $1}')"
  279. if [ "$etype" = "3" ]; then
  280. ok "unbound binary is PIE (ELF e_type=ET_DYN)"
  281. else
  282. bad "unbound binary is PIE (ELF e_type=$etype, expected 3)"
  283. fi
  284. say "== 17. features compiled in (configure line) =="
  285. confline="$(docker exec "$C1" unbound -V 2>/dev/null | grep -i 'configure line')"
  286. for flag in --enable-subnet --enable-tfo-server --enable-tfo-client --enable-pie --enable-relro-now; do
  287. if printf '%s' "$confline" | grep -q -- "$flag"; then
  288. ok "compiled with $flag"
  289. else
  290. bad "compiled with $flag"
  291. fi
  292. done
  293. say "== 18. RFC5011 anchor: initialized once, not reset on restart =="
  294. # Deterministic entrypoint test (unbound stubbed so it cannot rewrite the
  295. # file): the entrypoint must (a) initialize /var/unbound/root.key from the
  296. # package anchor when it is missing, and (b) NOT overwrite an existing file.
  297. # The old bug (unconditional `cp -f`) reset learned RFC5011 state on every
  298. # start.
  299. STUB='mkdir -p /tmp/stub && printf "#!/bin/sh\nexit 0\n" > /tmp/stub/unbound && chmod +x /tmp/stub/unbound && PATH=/tmp/stub:$PATH sh /entrypoint.sh'
  300. rm -rf "$ANCHOR_DIR"
  301. mkdir -p "$ANCHOR_DIR"
  302. chmod 777 "$ANCHOR_DIR"
  303. h_pkg="$(docker run --rm --entrypoint cat "$IMAGE" /usr/share/dnssec-root/trusted-key.key 2>/dev/null | sha256sum | cut -d' ' -f1)"
  304. # (a) missing -> initialized from the package anchor
  305. docker run --rm --entrypoint sh -v "$ANCHOR_DIR:/var/unbound" "$IMAGE" -c "$STUB" >/dev/null 2>&1
  306. h_init="$(sha256sum "$ANCHOR_DIR/root.key" 2>/dev/null | cut -d' ' -f1)"
  307. if [ -n "$h_init" ] && [ "$h_init" = "$h_pkg" ]; then
  308. ok "entrypoint initializes anchor from package when missing"
  309. else
  310. bad "entrypoint initializes anchor from package when missing (init=$h_init pkg=$h_pkg)"
  311. fi
  312. # (b) existing -> kept, not reset (RFC5011 state must survive)
  313. printf '\n; sentinel: entrypoint must not overwrite this file\n' >> "$ANCHOR_DIR/root.key"
  314. h_sentinel="$(sha256sum "$ANCHOR_DIR/root.key" 2>/dev/null | cut -d' ' -f1)"
  315. docker run --rm --entrypoint sh -v "$ANCHOR_DIR:/var/unbound" "$IMAGE" -c "$STUB" >/dev/null 2>&1
  316. h_now="$(sha256sum "$ANCHOR_DIR/root.key" 2>/dev/null | cut -d' ' -f1)"
  317. if [ "$h_now" = "$h_sentinel" ]; then
  318. ok "entrypoint keeps existing anchor (no cp -f reset)"
  319. else
  320. bad "entrypoint keeps existing anchor (now=$h_now sentinel=$h_sentinel pkg=$h_pkg)"
  321. fi
  322. # Integration: real unbound, DNSSEC validates, RFC5011 state persisted to
  323. # the file, and still validating after a restart.
  324. rm -rf "$ANCHOR_DIR"; mkdir -p "$ANCHOR_DIR"; chmod 777 "$ANCHOR_DIR"
  325. docker run -d --name "$C5" --network "$NET" \
  326. -v "$ANCHOR_DIR:/var/unbound" "$IMAGE" >/dev/null
  327. ready="no"; i=0
  328. while [ $i -lt 10 ]; do
  329. if docker exec "$C5" drill -p 5053 unbound.net @127.0.0.1 >/dev/null 2>&1; then
  330. ready="yes"; break
  331. fi
  332. sleep 2; i=$((i+1))
  333. done
  334. [ "$ready" = "yes" ] || bad "resolver ready (initial)"
  335. # unbound writes its RFC5011 state into the anchor file (autotrust format)
  336. sleep 3
  337. if grep -q "autotrust trust anchor file" "$ANCHOR_DIR/root.key" 2>/dev/null; then
  338. ok "unbound persists RFC5011 state into anchor file"
  339. else
  340. bad "unbound persists RFC5011 state into anchor file"
  341. fi
  342. p1_out="$(docker exec "$C5" drill -p 5053 -D example.com @127.0.0.1 2>/dev/null)"
  343. if printf '%s' "$p1_out" | grep -q "rcode: NOERROR" \
  344. && printf '%s' "$p1_out" | grep -qE 'flags:.*\bad\b'; then
  345. ok "DNSSEC AD=1 (initial, drill -D loopback)"
  346. else
  347. bad "DNSSEC AD=1 (initial, drill -D loopback)"
  348. printf '%s\n' "$p1_out" | sed 's/^/ /'
  349. fi
  350. docker restart "$C5" >/dev/null 2>&1
  351. ready="no"; i=0
  352. while [ $i -lt 10 ]; do
  353. if docker exec "$C5" drill -p 5053 unbound.net @127.0.0.1 >/dev/null 2>&1; then
  354. ready="yes"; break
  355. fi
  356. sleep 2; i=$((i+1))
  357. done
  358. [ "$ready" = "yes" ] || bad "resolver ready after restart"
  359. if [ -f "$ANCHOR_DIR/root.key" ]; then
  360. ok "anchor file persists after restart"
  361. else
  362. bad "anchor file persists after restart"
  363. fi
  364. p2_out="$(docker exec "$C5" drill -p 5053 -D example.com @127.0.0.1 2>/dev/null)"
  365. if printf '%s' "$p2_out" | grep -q "rcode: NOERROR" \
  366. && printf '%s' "$p2_out" | grep -qE 'flags:.*\bad\b'; then
  367. ok "DNSSEC AD=1 after restart (drill -D loopback)"
  368. else
  369. bad "DNSSEC AD=1 after restart (drill -D loopback)"
  370. printf '%s\n' "$p2_out" | sed 's/^/ /'
  371. fi
  372. say "== 19. compose template starts (no recursive include) =="
  373. # Regression test for the audit P1 finding: the template must start cleanly
  374. # with the ACTUAL docker-compose.yml (old version mounted ./config over
  375. # itself -> recursive include -> start aborted).
  376. #
  377. # It runs under the dedicated test project (PROJ_TEST) with the test-image
  378. # override, so it creates only its own container/network/volume and never
  379. # touches a production deployment of the template.
  380. if compose_test up -d >/dev/null 2>&1; then
  381. hc="no"
  382. i=0
  383. while [ $i -lt 20 ]; do
  384. h="$(docker inspect -f '{{.State.Health.Status}}' "$C6_CONT" 2>/dev/null)"
  385. [ "$h" = "healthy" ] && hc="yes" && break
  386. sleep 6
  387. i=$((i+1))
  388. done
  389. if [ "$hc" = "yes" ]; then
  390. ok "compose template starts + healthy (no ports, conf.d + anchor volume)"
  391. else
  392. bad "compose template starts + healthy (status=$h)"
  393. docker logs "$C6_CONT" 2>&1 | tail -5 | sed 's/^/ /'
  394. fi
  395. else
  396. bad "compose template starts"
  397. compose_test up -d 2>&1 | tail -5 | sed 's/^/ /'
  398. fi
  399. compose_test down --remove-orphans --volumes >/dev/null 2>&1
  400. say "== 20. DNSSEC negative test: invalid signature -> SERVFAIL =="
  401. # Controlled zone "broken.example" served by a static responder: a
  402. # well-formed RRSIG whose DNSKEY cannot be anchored in the trust chain (no
  403. # parent DS). A validating resolver must reject it with SERVFAIL (without
  404. # CD). This catches regressions like val-permissive-mode: yes, which would
  405. # still validate good answers but let broken ones through.
  406. #
  407. # 1) generate the zone packet (fresh RRSIG timestamps) in a python container
  408. # 2) start a static UDP responder serving it
  409. # 3) start a test unbound with a forward-zone to the responder
  410. # 4) query www.broken.example A with DO -> expect SERVFAIL (rcode=2)
  411. #
  412. # RESP and C6 get their OWN network (not $NET), so they can't be affected by
  413. # the other test containers and the test stays deterministic.
  414. REPO="${REPO_PATH:-$(pwd)}"
  415. NEG_NET="ubsec-negnet"
  416. docker network create "$NEG_NET" >/dev/null 2>&1
  417. BZBIN="$(pwd)/test/.broken-zone.bin"
  418. # Remove any stale BZBIN first: if a previous run left a DIRECTORY there (Docker
  419. # creates a dir when mounting a missing file), the generator would fail with
  420. # IsADirectoryError.
  421. rm -rf "$BZBIN"
  422. docker run --rm -v "$REPO/test/make-broken-zone.py:/m.py:ro" -v "$(pwd)/test:/o" \
  423. python:alpine sh -c "pip install -q dnspython cryptography 2>/dev/null; python /m.py /o/.broken-zone.bin" >/dev/null 2>&1
  424. if [ ! -f "$BZBIN" ]; then
  425. bad "generate broken.example zone packet"
  426. else
  427. ok "generate broken.example zone packet"
  428. fi
  429. docker run -d --name "$RESP" --network "$NEG_NET" \
  430. -v "$BZBIN:/p.bin:ro" -v "$REPO/test/broken-zone-responder.py:/r.py:ro" \
  431. python:alpine python /r.py /p.bin 5353 >/dev/null
  432. # Get the responder's IP from ITS OWN log (it prints its container IP). This
  433. # is more reliable than `docker inspect` from the host, which can be empty
  434. # while the container is starting on a slow WSL daemon.
  435. RESP_IP=""; i=0
  436. while [ $i -lt 15 ]; do
  437. RESP_IP="$(docker logs "$RESP" 2>&1 | grep -oE 'responder ip=[0-9.]+' | head -1 | cut -d= -f2)"
  438. [ -n "$RESP_IP" ] && [ "$RESP_IP" != "0.0.0.0" ] && break
  439. sleep 2; i=$((i+1))
  440. done
  441. # Fallback to docker inspect if the log didn't yield an IP.
  442. if [ -z "$RESP_IP" ] || [ "$RESP_IP" = "0.0.0.0" ]; then
  443. RESP_IP="$(docker inspect -f '{{(index .NetworkSettings.Networks "'"$NEG_NET"').IPAddress}}' "$RESP" 2>/dev/null)"
  444. fi
  445. [ -n "$RESP_IP" ] || { bad "negative-test responder has an IP (got empty)"
  446. docker logs "$RESP" 2>&1 | tail -3 | sed 's/^/ /'
  447. say " networks: $(docker inspect -f '{{json .NetworkSettings.Networks}}' "$RESP" 2>/dev/null)"
  448. say " state: $(docker inspect -f '{{.State.Running}} exit={{.State.ExitCode}}' "$RESP" 2>/dev/null)"; }
  449. FWD_DIR="$(pwd)/test/fixture-broken-fwd"
  450. rm -rf "$FWD_DIR"; mkdir -p "$FWD_DIR"; chmod 755 "$FWD_DIR"
  451. printf 'forward-zone:\n name: "broken.example"\n forward-addr: %s@5353\n' "$RESP_IP" > "$FWD_DIR/99-broken.conf"
  452. chmod 644 "$FWD_DIR/99-broken.conf"
  453. docker run -d --name "$C6" --network "$NEG_NET" -v "$FWD_DIR:/config:ro" "$IMAGE" >/dev/null
  454. # wait for readiness: unbound is ready when it answers the (broken) query at
  455. # all — a SERVFAIL is a valid response, so we check for ANY rcode, not a
  456. # successful (exit 0) drill.
  457. ready="no"; i=0
  458. while [ $i -lt 15 ]; do
  459. if docker exec "$C6" drill -p 5053 -D www.broken.example A @127.0.0.1 2>/dev/null | grep -q "rcode:"; then
  460. ready="yes"; break
  461. fi
  462. sleep 2; i=$((i+1))
  463. done
  464. [ "$ready" = "yes" ] || bad "negative-test resolver ready"
  465. sf_out="$(docker exec "$C6" drill -p 5053 -D www.broken.example A @127.0.0.1 2>/dev/null)"
  466. if printf '%s' "$sf_out" | grep -q "rcode: SERVFAIL"; then
  467. ok "invalid signature -> SERVFAIL (no CD; validator rejects unanchorable DNSKEY)"
  468. else
  469. bad "invalid signature -> SERVFAIL (got: $(printf '%s' "$sf_out" | grep rcode | head -1))"
  470. printf '%s\n' "$sf_out" | sed 's/^/ /'
  471. fi
  472. rm -rf "$FWD_DIR"
  473. say ""
  474. say "=================================================="
  475. say "RESULT: $PASS passed, $FAIL failed, $NOTVERIFIED not verified"
  476. if [ "$NOTVERIFIED" -gt 0 ]; then
  477. say "(not verified = infrastructure timeout, not a content finding; re-run to clear)"
  478. fi
  479. say "=================================================="
  480. # A content finding (FAIL) fails the run. Not-verified checks are visible but
  481. # do not fail the run on their own (they are infrastructure, re-run to clear).
  482. [ $FAIL -eq 0 ]