run-tests.sh 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409
  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. NET="ubsec-testnet"
  19. # Fixtures must use HOST paths (bind mounts are resolved by the daemon).
  20. # run.sh sets REPO_PATH to the host repo path and pre-creates the fixtures.
  21. CFG1="${REPO_PATH:-$(pwd)}/test/fixture-config-ok"
  22. CFG2="${REPO_PATH:-$(pwd)}/test/fixture-config-bad"
  23. ANCHOR_DIR="${REPO_PATH:-$(pwd)}/test/fixture-anchor"
  24. PASS=0
  25. FAIL=0
  26. say() { printf '%s\n' "$*"; }
  27. ok() { PASS=$((PASS+1)); say " [PASS] $1"; }
  28. bad() { FAIL=$((FAIL+1)); say " [FAIL] $1"; }
  29. # Resolve a container's IP on $NET so probes can address it directly
  30. # (avoids depending on Docker's embedded DNS inside the probe container).
  31. ip_of() { docker inspect -f '{{(index .NetworkSettings.Networks "'"$NET"').IPAddress}}' "$1" 2>/dev/null; }
  32. cleanup() {
  33. docker rm -f "$C1" "$C2" "$C3" "$C4" "$C5" >/dev/null 2>&1
  34. docker rm -f unbound >/dev/null 2>&1
  35. (cd "${REPO_PATH:-$(pwd)}" && docker compose -f docker-compose.yml down --remove-orphans --volumes >/dev/null 2>&1)
  36. docker network rm "$NET" >/dev/null 2>&1
  37. rm -rf "$(pwd)/test/fixture-identity" 2>/dev/null
  38. rm -rf "$ANCHOR_DIR" 2>/dev/null
  39. rm -f "$(pwd)/test/build.log" 2>/dev/null
  40. }
  41. trap cleanup EXIT
  42. say "== 0. cleanup old artifacts =="
  43. cleanup
  44. say "== 1. build image =="
  45. BUILDLOG="$(pwd)/test/build.log"
  46. # CACHEBUST forces the src stages (SHA256 + GPG verification) to re-run on
  47. # every test run so the build log always contains fresh evidence; the slow
  48. # compile stages stay cached.
  49. if docker build --build-arg CACHEBUST="$(date +%s)" -t "$IMAGE" . >"$BUILDLOG" 2>&1; then
  50. ok "docker build"
  51. # The compose template references unbound-sec:latest (the production
  52. # tag from build.sh); tag the test image accordingly so test 19 can use
  53. # it without pulling from a registry.
  54. docker tag "$IMAGE" unbound-sec:latest >/dev/null 2>&1
  55. else
  56. bad "docker build"
  57. tail -15 "$BUILDLOG"
  58. say "ABORT: build failed"; exit 1
  59. fi
  60. say "== 2. start hardened container =="
  61. docker network create "$NET" >/dev/null
  62. docker run -d --name "$C1" --network "$NET" \
  63. -p 5053:5053/udp -p 5053:5053/tcp \
  64. --read-only \
  65. --tmpfs /tmp --tmpfs "/run/unbound:uid=1500,gid=1500" \
  66. --tmpfs "/var/unbound:uid=1500,gid=1500" \
  67. --security-opt no-new-privileges:true \
  68. --cap-drop ALL \
  69. "$IMAGE" >/dev/null
  70. sleep 3
  71. if [ "$(docker inspect -f '{{.State.Running}}' "$C1" 2>/dev/null)" = "true" ]; then
  72. ok "container running"
  73. else
  74. bad "container running"; docker logs "$C1" 2>&1 | tail -5
  75. fi
  76. say "== 3. healthcheck =="
  77. healthy="no"
  78. i=0
  79. # WSL/9p container startup can be slow; give it up to 120s.
  80. while [ $i -lt 20 ]; do
  81. h="$(docker inspect -f '{{.State.Health.Status}}' "$C1" 2>/dev/null)"
  82. [ "$h" = "healthy" ] && healthy="yes" && break
  83. sleep 6
  84. i=$((i+1))
  85. done
  86. if [ "$healthy" = "yes" ]; then
  87. ok "healthcheck healthy"
  88. else
  89. bad "healthcheck healthy (status=$h)"
  90. fi
  91. say "== 4. version =="
  92. v="$(docker exec "$C1" unbound -V 2>/dev/null | head -1)"
  93. case "$v" in
  94. *1.26.2*) ok "unbound version 1.26.2 ($v)";;
  95. *) bad "unbound version 1.26.2 (got: $v)";;
  96. esac
  97. say "== 5. DNS resolution =="
  98. if docker exec "$C1" drill -p 5053 example.com @127.0.0.1 2>/dev/null | grep -q "example.com"; then
  99. ok "drill resolves example.com"
  100. else
  101. bad "drill resolves example.com"
  102. fi
  103. say "== 6. DNSSEC (correct EDNS-DO query) =="
  104. # Primary check: drill -D sets the EDNS DO bit; a validating resolver must
  105. # answer with the AD (authenticated data) bit set. Loopback via docker exec
  106. # is reliable (no external overlay-network dependency).
  107. ad_out="$(docker exec "$C1" drill -p 5053 -D example.com @127.0.0.1 2>/dev/null)"
  108. if printf '%s' "$ad_out" | grep -q "rcode: NOERROR" \
  109. && printf '%s' "$ad_out" | grep -qE 'flags:.*\bad\b' \
  110. && printf '%s' "$ad_out" | grep -q "example.com"; then
  111. ok "drill -D: signed zone answers with AD bit (EDNS DO)"
  112. else
  113. bad "drill -D: signed zone answers with AD bit (EDNS DO)"
  114. printf '%s\n' "$ad_out" | sed 's/^/ /'
  115. fi
  116. # Secondary check: raw EDNS-0 DO probe from an external container (also
  117. # verifies the no-DO -> no-AD behavior). Best-effort: the WSL overlay
  118. # network can hiccup, so a timeout here is not treated as a validation
  119. # failure (the loopback check above already proved validation).
  120. 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)"
  121. probe_rc=$?
  122. if [ $probe_rc -ne 0 ] || printf '%s' "$probe_out" | grep -q "FAIL"; then
  123. say " [warn] external EDNS-DO probe flaky (loopback check passed):"
  124. printf '%s\n' "$probe_out" | sed 's/^/ /'
  125. else
  126. ok "external EDNS-DO probe (AD=1 with DO, AD=0 without)"
  127. fi
  128. say "== 7. non-root user =="
  129. uid="$(docker exec "$C1" sh -c 'id -u' 2>/dev/null)"
  130. if [ "$uid" = "1500" ]; then
  131. ok "runs as uid 1500"
  132. else
  133. bad "runs as uid 1500 (got: $uid)"
  134. fi
  135. say "== 8. capabilities =="
  136. capeff="$(docker exec "$C1" sh -c 'grep ^CapEff /proc/self/status' 2>/dev/null | awk '{print $2}')"
  137. if [ "$capeff" = "0000000000000000" ]; then
  138. ok "CapEff = 0"
  139. else
  140. bad "CapEff = 0 (got: $capeff)"
  141. fi
  142. say "== 9. no SUID binaries =="
  143. suid="$(docker exec "$C1" sh -c 'find / -perm -4000 -type f 2>/dev/null' 2>/dev/null)"
  144. if [ -z "$suid" ]; then
  145. ok "no SUID-root binaries"
  146. else
  147. bad "no SUID-root binaries (found: $suid)"
  148. fi
  149. say "== 10. read-only root filesystem =="
  150. if docker exec "$C1" sh -c 'touch /usr/writetest 2>/dev/null'; then
  151. bad "root fs is read-only (write to /usr succeeded)"
  152. else
  153. ok "root fs is read-only"
  154. fi
  155. if docker exec "$C1" sh -c 'touch /run/unbound/writetest && rm /run/unbound/writetest' 2>/dev/null; then
  156. ok "/run/unbound writable (tmpfs)"
  157. else
  158. bad "/run/unbound writable (tmpfs)"
  159. fi
  160. say "== 11. no-new-privileges + seccomp =="
  161. nnp="$(docker exec "$C1" sh -c 'grep ^NoNewPrivs /proc/self/status' 2>/dev/null | awk '{print $2}')"
  162. if [ "$nnp" = "1" ]; then
  163. ok "NoNewPrivs = 1"
  164. else
  165. bad "NoNewPrivs = 1 (got: $nnp)"
  166. fi
  167. sc="$(docker exec "$C1" sh -c 'grep ^Seccomp: /proc/self/status' 2>/dev/null | awk '{print $2}')"
  168. if [ "$sc" = "2" ]; then
  169. ok "seccomp filter active"
  170. else
  171. bad "seccomp filter active (got: $sc)"
  172. fi
  173. say "== 12. hide-identity / hide-version (CHAOS) =="
  174. # CHAOS id.server/version.server probes. The only reliable CHAOS query tool
  175. # here is the Python probe (drill/unbound-host in this image can't set the
  176. # CHAOS class). It runs from a side container over the Docker overlay
  177. # network, which can hiccup on WSL — so a timeout is reported as a warning,
  178. # not a failure (the config value is asserted separately below).
  179. 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)"
  180. id_rc=$?
  181. if [ $id_rc -ne 0 ] || printf '%s' "$id_out" | grep -q "FAIL"; then
  182. say " [warn] hide-identity CHAOS probe flaky/failed (WSL overlay):"
  183. printf '%s\n' "$id_out" | sed 's/^/ /'
  184. else
  185. ok "hide-identity: id.server. answer is empty"
  186. fi
  187. # Config-level assertion (deterministic): the base config must set it.
  188. if docker exec "$C1" grep -q "hide-identity: yes" /etc/unbound/unbound.conf 2>/dev/null; then
  189. ok "config: hide-identity: yes in base config"
  190. else
  191. bad "config: hide-identity: yes in base config"
  192. fi
  193. # Control: same image with hide-identity switched off must reveal the identity.
  194. CFG_ID="${REPO_PATH:-$(pwd)}/test/fixture-identity"
  195. mkdir -p "$CFG_ID"
  196. printf 'server:\n hide-identity: no\n' > "$CFG_ID/00-identity.conf"
  197. docker run -d --name "$C4" --network "$NET" -v "$CFG_ID:/config:ro" "$IMAGE" >/dev/null
  198. sleep 3
  199. 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)"
  200. id2_rc=$?
  201. if [ $id2_rc -ne 0 ] || printf '%s' "$id2_out" | grep -q "FAIL"; then
  202. say " [warn] identity-control CHAOS probe flaky/failed (WSL overlay):"
  203. printf '%s\n' "$id2_out" | sed 's/^/ /'
  204. else
  205. ok "identity control: hide-identity:no reveals identity"
  206. fi
  207. say "== 13. drop-in config include =="
  208. # Fixtures are created on the host by test/run.sh (host paths required).
  209. if [ ! -f "$CFG1/99-test.conf" ] || [ ! -f "$CFG2/99-broken.conf" ]; then
  210. bad "test fixtures missing (run via test/run.sh)"
  211. fi
  212. docker run -d --name "$C2" --network "$NET" -v "$CFG1:/config:ro" "$IMAGE" >/dev/null
  213. sleep 3
  214. if [ "$(docker inspect -f '{{.State.Running}}' "$C2" 2>/dev/null)" = "true" ]; then
  215. ok "valid drop-in config accepted (include works)"
  216. else
  217. bad "valid drop-in config accepted"
  218. fi
  219. docker run -d --name "$C3" --network "$NET" -v "$CFG2:/config:ro" "$IMAGE" >/dev/null
  220. sleep 3
  221. if [ "$(docker inspect -f '{{.State.Running}}' "$C3" 2>/dev/null)" = "false" ]; then
  222. ok "broken drop-in config rejects start (checkconf)"
  223. else
  224. bad "broken drop-in config rejects start"
  225. fi
  226. say "== 14. package attack surface =="
  227. has_curl="$(docker exec "$C1" sh -c 'test -x /usr/bin/curl && echo yes' 2>/dev/null)"
  228. if [ "$has_curl" = "yes" ]; then
  229. bad "curl not in runtime image"
  230. else
  231. ok "no curl in runtime image"
  232. fi
  233. has_shadow="$(docker exec "$C1" sh -c 'test -x /usr/bin/chsh && echo yes' 2>/dev/null)"
  234. if [ "$has_shadow" = "yes" ]; then
  235. bad "shadow package not removed"
  236. else
  237. ok "shadow package removed"
  238. fi
  239. say "== 15. supply chain: GPG + SHA256 of source tarballs =="
  240. sigs="$(grep -c "Good signature" "$BUILDLOG" 2>/dev/null)"
  241. if [ "$sigs" -ge 2 ]; then
  242. ok "GPG: Good signature x$sigs (unbound + ldns tarballs)"
  243. else
  244. bad "GPG: Good signature (found: $sigs, need >= 2)"
  245. fi
  246. if grep -E "unbound\.tar\.gz: OK|ldns\.tar\.gz: OK" "$BUILDLOG" >/dev/null 2>&1; then
  247. ok "SHA256: tarball checksums OK"
  248. else
  249. bad "SHA256: tarball checksums OK"
  250. fi
  251. say "== 16. binary hardening: PIE =="
  252. # ELF e_type: 3 = ET_DYN (PIE/shared). Read from the final binary itself,
  253. # so this works even on cached builds.
  254. etype="$(docker exec "$C1" sh -c 'od -An -N32 -td2 /usr/sbin/unbound' 2>/dev/null | awk 'NR==2{print $1}')"
  255. if [ "$etype" = "3" ]; then
  256. ok "unbound binary is PIE (ELF e_type=ET_DYN)"
  257. else
  258. bad "unbound binary is PIE (ELF e_type=$etype, expected 3)"
  259. fi
  260. say "== 17. features compiled in (configure line) =="
  261. confline="$(docker exec "$C1" unbound -V 2>/dev/null | grep -i 'configure line')"
  262. for flag in --enable-subnet --enable-tfo-server --enable-tfo-client --enable-pie --enable-relro-now; do
  263. if printf '%s' "$confline" | grep -q -- "$flag"; then
  264. ok "compiled with $flag"
  265. else
  266. bad "compiled with $flag"
  267. fi
  268. done
  269. say "== 18. RFC5011 anchor: initialized once, not reset on restart =="
  270. # Deterministic entrypoint test (unbound stubbed so it cannot rewrite the
  271. # file): the entrypoint must (a) initialize /var/unbound/root.key from the
  272. # package anchor when it is missing, and (b) NOT overwrite an existing file.
  273. # The old bug (unconditional `cp -f`) reset learned RFC5011 state on every
  274. # start.
  275. 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'
  276. rm -rf "$ANCHOR_DIR"
  277. mkdir -p "$ANCHOR_DIR"
  278. chmod 777 "$ANCHOR_DIR"
  279. h_pkg="$(docker run --rm --entrypoint cat "$IMAGE" /usr/share/dnssec-root/trusted-key.key 2>/dev/null | sha256sum | cut -d' ' -f1)"
  280. # (a) missing -> initialized from the package anchor
  281. docker run --rm --entrypoint sh -v "$ANCHOR_DIR:/var/unbound" "$IMAGE" -c "$STUB" >/dev/null 2>&1
  282. h_init="$(sha256sum "$ANCHOR_DIR/root.key" 2>/dev/null | cut -d' ' -f1)"
  283. if [ -n "$h_init" ] && [ "$h_init" = "$h_pkg" ]; then
  284. ok "entrypoint initializes anchor from package when missing"
  285. else
  286. bad "entrypoint initializes anchor from package when missing (init=$h_init pkg=$h_pkg)"
  287. fi
  288. # (b) existing -> kept, not reset (RFC5011 state must survive)
  289. printf '\n; sentinel: entrypoint must not overwrite this file\n' >> "$ANCHOR_DIR/root.key"
  290. h_sentinel="$(sha256sum "$ANCHOR_DIR/root.key" 2>/dev/null | cut -d' ' -f1)"
  291. docker run --rm --entrypoint sh -v "$ANCHOR_DIR:/var/unbound" "$IMAGE" -c "$STUB" >/dev/null 2>&1
  292. h_now="$(sha256sum "$ANCHOR_DIR/root.key" 2>/dev/null | cut -d' ' -f1)"
  293. if [ "$h_now" = "$h_sentinel" ]; then
  294. ok "entrypoint keeps existing anchor (no cp -f reset)"
  295. else
  296. bad "entrypoint keeps existing anchor (now=$h_now sentinel=$h_sentinel pkg=$h_pkg)"
  297. fi
  298. # Integration: real unbound, DNSSEC validates, RFC5011 state persisted to
  299. # the file, and still validating after a restart.
  300. rm -rf "$ANCHOR_DIR"; mkdir -p "$ANCHOR_DIR"; chmod 777 "$ANCHOR_DIR"
  301. docker run -d --name "$C5" --network "$NET" \
  302. -v "$ANCHOR_DIR:/var/unbound" "$IMAGE" >/dev/null
  303. ready="no"; i=0
  304. while [ $i -lt 10 ]; do
  305. if docker exec "$C5" drill -p 5053 unbound.net @127.0.0.1 >/dev/null 2>&1; then
  306. ready="yes"; break
  307. fi
  308. sleep 2; i=$((i+1))
  309. done
  310. [ "$ready" = "yes" ] || bad "resolver ready (initial)"
  311. # unbound writes its RFC5011 state into the anchor file (autotrust format)
  312. sleep 3
  313. if grep -q "autotrust trust anchor file" "$ANCHOR_DIR/root.key" 2>/dev/null; then
  314. ok "unbound persists RFC5011 state into anchor file"
  315. else
  316. bad "unbound persists RFC5011 state into anchor file"
  317. fi
  318. p1_out="$(docker exec "$C5" drill -p 5053 -D example.com @127.0.0.1 2>/dev/null)"
  319. if printf '%s' "$p1_out" | grep -q "rcode: NOERROR" \
  320. && printf '%s' "$p1_out" | grep -qE 'flags:.*\bad\b'; then
  321. ok "DNSSEC AD=1 (initial, drill -D loopback)"
  322. else
  323. bad "DNSSEC AD=1 (initial, drill -D loopback)"
  324. printf '%s\n' "$p1_out" | sed 's/^/ /'
  325. fi
  326. docker restart "$C5" >/dev/null 2>&1
  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 after restart"
  335. if [ -f "$ANCHOR_DIR/root.key" ]; then
  336. ok "anchor file persists after restart"
  337. else
  338. bad "anchor file persists after restart"
  339. fi
  340. p2_out="$(docker exec "$C5" drill -p 5053 -D example.com @127.0.0.1 2>/dev/null)"
  341. if printf '%s' "$p2_out" | grep -q "rcode: NOERROR" \
  342. && printf '%s' "$p2_out" | grep -qE 'flags:.*\bad\b'; then
  343. ok "DNSSEC AD=1 after restart (drill -D loopback)"
  344. else
  345. bad "DNSSEC AD=1 after restart (drill -D loopback)"
  346. printf '%s\n' "$p2_out" | sed 's/^/ /'
  347. fi
  348. say "== 19. compose template starts (no recursive include) =="
  349. # Regression test for the audit P1 finding: the template must start cleanly
  350. # with the ACTUAL docker-compose.yml (old version mounted ./config over
  351. # itself -> recursive include -> start aborted).
  352. REPO="${REPO_PATH:-$(pwd)}"
  353. if (cd "$REPO" && docker compose -f docker-compose.yml up -d >/dev/null 2>&1); then
  354. hc="no"
  355. i=0
  356. while [ $i -lt 20 ]; do
  357. h="$(docker inspect -f '{{.State.Health.Status}}' unbound 2>/dev/null)"
  358. [ "$h" = "healthy" ] && hc="yes" && break
  359. sleep 6
  360. i=$((i+1))
  361. done
  362. if [ "$hc" = "yes" ]; then
  363. ok "compose template starts + healthy (no ports, conf.d + anchor volume)"
  364. else
  365. bad "compose template starts + healthy (status=$h)"
  366. docker logs unbound 2>&1 | tail -5 | sed 's/^/ /'
  367. fi
  368. else
  369. bad "compose template starts"
  370. (cd "$REPO" && docker compose -f docker-compose.yml up -d 2>&1 | tail -5) | sed 's/^/ /'
  371. fi
  372. (cd "$REPO" && docker compose -f docker-compose.yml down --remove-orphans --volumes >/dev/null 2>&1)
  373. say ""
  374. say "=================================================="
  375. say "RESULT: $PASS passed, $FAIL failed"
  376. say "=================================================="
  377. [ $FAIL -eq 0 ]