run-tests.sh 8.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259
  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. NET="ubsec-testnet"
  18. # Fixtures must use HOST paths (bind mounts are resolved by the daemon).
  19. # run.sh sets REPO_PATH to the host repo path and pre-creates the fixtures.
  20. CFG1="${REPO_PATH:-$(pwd)}/test/fixture-config-ok"
  21. CFG2="${REPO_PATH:-$(pwd)}/test/fixture-config-bad"
  22. PASS=0
  23. FAIL=0
  24. say() { printf '%s\n' "$*"; }
  25. ok() { PASS=$((PASS+1)); say " [PASS] $1"; }
  26. bad() { FAIL=$((FAIL+1)); say " [FAIL] $1"; }
  27. cleanup() {
  28. docker rm -f "$C1" "$C2" "$C3" "$C4" >/dev/null 2>&1
  29. docker network rm "$NET" >/dev/null 2>&1
  30. rm -rf "$(pwd)/test/fixture-identity" 2>/dev/null
  31. rm -f "$(pwd)/test/build.log" 2>/dev/null
  32. }
  33. trap cleanup EXIT
  34. say "== 0. cleanup old artifacts =="
  35. cleanup
  36. say "== 1. build image =="
  37. BUILDLOG="$(pwd)/test/build.log"
  38. # CACHEBUST forces the src stages (SHA256 + GPG verification) to re-run on
  39. # every test run so the build log always contains fresh evidence; the slow
  40. # compile stages stay cached.
  41. if docker build --build-arg CACHEBUST="$(date +%s)" -t "$IMAGE" . >"$BUILDLOG" 2>&1; then
  42. ok "docker build"
  43. else
  44. bad "docker build"
  45. tail -15 "$BUILDLOG"
  46. say "ABORT: build failed"; exit 1
  47. fi
  48. say "== 2. start hardened container =="
  49. docker network create "$NET" >/dev/null
  50. docker run -d --name "$C1" --network "$NET" \
  51. -p 5053:5053/udp -p 5053:5053/tcp \
  52. --read-only \
  53. --tmpfs /tmp --tmpfs "/run/unbound:uid=1500,gid=1500" \
  54. --security-opt no-new-privileges:true \
  55. --cap-drop ALL \
  56. "$IMAGE" >/dev/null
  57. sleep 3
  58. if [ "$(docker inspect -f '{{.State.Running}}' "$C1" 2>/dev/null)" = "true" ]; then
  59. ok "container running"
  60. else
  61. bad "container running"; docker logs "$C1" 2>&1 | tail -5
  62. fi
  63. say "== 3. healthcheck =="
  64. healthy="no"
  65. i=0
  66. while [ $i -lt 12 ]; do
  67. h="$(docker inspect -f '{{.State.Health.Status}}' "$C1" 2>/dev/null)"
  68. [ "$h" = "healthy" ] && healthy="yes" && break
  69. sleep 6
  70. i=$((i+1))
  71. done
  72. if [ "$healthy" = "yes" ]; then
  73. ok "healthcheck healthy"
  74. else
  75. bad "healthcheck healthy (status=$h)"
  76. fi
  77. say "== 4. version =="
  78. v="$(docker exec "$C1" unbound -V 2>/dev/null | head -1)"
  79. case "$v" in
  80. *1.26.2*) ok "unbound version 1.26.2 ($v)";;
  81. *) bad "unbound version 1.26.2 (got: $v)";;
  82. esac
  83. say "== 5. DNS resolution =="
  84. if docker exec "$C1" drill -p 5053 example.com @127.0.0.1 2>/dev/null | grep -q "example.com"; then
  85. ok "drill resolves example.com"
  86. else
  87. bad "drill resolves example.com"
  88. fi
  89. say "== 6. DNSSEC (AD bit, DO query) =="
  90. 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)"
  91. probe_rc=$?
  92. if [ $probe_rc -ne 0 ] || printf '%s' "$probe_out" | grep -q "FAIL"; then
  93. bad "DNSSEC AD-bit probe"
  94. printf '%s\n' "$probe_out" | sed 's/^/ /'
  95. else
  96. ok "DNSSEC AD-bit probe"
  97. fi
  98. say "== 7. non-root user =="
  99. uid="$(docker exec "$C1" sh -c 'id -u' 2>/dev/null)"
  100. if [ "$uid" = "1500" ]; then
  101. ok "runs as uid 1500"
  102. else
  103. bad "runs as uid 1500 (got: $uid)"
  104. fi
  105. say "== 8. capabilities =="
  106. capeff="$(docker exec "$C1" sh -c 'grep ^CapEff /proc/self/status' 2>/dev/null | awk '{print $2}')"
  107. if [ "$capeff" = "0000000000000000" ]; then
  108. ok "CapEff = 0"
  109. else
  110. bad "CapEff = 0 (got: $capeff)"
  111. fi
  112. say "== 9. no SUID binaries =="
  113. suid="$(docker exec "$C1" sh -c 'find / -perm -4000 -type f 2>/dev/null' 2>/dev/null)"
  114. if [ -z "$suid" ]; then
  115. ok "no SUID-root binaries"
  116. else
  117. bad "no SUID-root binaries (found: $suid)"
  118. fi
  119. say "== 10. read-only root filesystem =="
  120. if docker exec "$C1" sh -c 'touch /usr/writetest 2>/dev/null'; then
  121. bad "root fs is read-only (write to /usr succeeded)"
  122. else
  123. ok "root fs is read-only"
  124. fi
  125. if docker exec "$C1" sh -c 'touch /run/unbound/writetest && rm /run/unbound/writetest' 2>/dev/null; then
  126. ok "/run/unbound writable (tmpfs)"
  127. else
  128. bad "/run/unbound writable (tmpfs)"
  129. fi
  130. say "== 11. no-new-privileges + seccomp =="
  131. nnp="$(docker exec "$C1" sh -c 'grep ^NoNewPrivs /proc/self/status' 2>/dev/null | awk '{print $2}')"
  132. if [ "$nnp" = "1" ]; then
  133. ok "NoNewPrivs = 1"
  134. else
  135. bad "NoNewPrivs = 1 (got: $nnp)"
  136. fi
  137. sc="$(docker exec "$C1" sh -c 'grep ^Seccomp: /proc/self/status' 2>/dev/null | awk '{print $2}')"
  138. if [ "$sc" = "2" ]; then
  139. ok "seccomp filter active"
  140. else
  141. bad "seccomp filter active (got: $sc)"
  142. fi
  143. say "== 12. hide-identity / hide-version =="
  144. 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)"
  145. id_rc=$?
  146. if [ $id_rc -ne 0 ] || printf '%s' "$id_out" | grep -q "FAIL"; then
  147. bad "hide-identity: id.server. answer is empty"
  148. printf '%s\n' "$id_out" | sed 's/^/ /'
  149. else
  150. ok "hide-identity: id.server. answer is empty"
  151. fi
  152. # Control: same image with hide-identity switched off must reveal the identity.
  153. CFG_ID="${REPO_PATH:-$(pwd)}/test/fixture-identity"
  154. mkdir -p "$CFG_ID"
  155. printf 'server:\n hide-identity: no\n' > "$CFG_ID/00-identity.conf"
  156. docker run -d --name "$C4" --network "$NET" -v "$CFG_ID:/config:ro" "$IMAGE" >/dev/null
  157. sleep 3
  158. 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)"
  159. id2_rc=$?
  160. if [ $id2_rc -ne 0 ] || printf '%s' "$id2_out" | grep -q "FAIL"; then
  161. bad "identity control: hide-identity:no reveals identity"
  162. printf '%s\n' "$id2_out" | sed 's/^/ /'
  163. else
  164. ok "identity control: hide-identity:no reveals identity"
  165. fi
  166. say "== 13. drop-in config include =="
  167. # Fixtures are created on the host by test/run.sh (host paths required).
  168. if [ ! -f "$CFG1/99-test.conf" ] || [ ! -f "$CFG2/99-broken.conf" ]; then
  169. bad "test fixtures missing (run via test/run.sh)"
  170. fi
  171. docker run -d --name "$C2" --network "$NET" -v "$CFG1:/config:ro" "$IMAGE" >/dev/null
  172. sleep 3
  173. if [ "$(docker inspect -f '{{.State.Running}}' "$C2" 2>/dev/null)" = "true" ]; then
  174. ok "valid drop-in config accepted (include works)"
  175. else
  176. bad "valid drop-in config accepted"
  177. fi
  178. docker run -d --name "$C3" --network "$NET" -v "$CFG2:/config:ro" "$IMAGE" >/dev/null
  179. sleep 3
  180. if [ "$(docker inspect -f '{{.State.Running}}' "$C3" 2>/dev/null)" = "false" ]; then
  181. ok "broken drop-in config rejects start (checkconf)"
  182. else
  183. bad "broken drop-in config rejects start"
  184. fi
  185. say "== 14. package attack surface =="
  186. has_curl="$(docker exec "$C1" sh -c 'test -x /usr/bin/curl && echo yes' 2>/dev/null)"
  187. if [ "$has_curl" = "yes" ]; then
  188. bad "curl not in runtime image"
  189. else
  190. ok "no curl in runtime image"
  191. fi
  192. has_shadow="$(docker exec "$C1" sh -c 'test -x /usr/bin/chsh && echo yes' 2>/dev/null)"
  193. if [ "$has_shadow" = "yes" ]; then
  194. bad "shadow package not removed"
  195. else
  196. ok "shadow package removed"
  197. fi
  198. say "== 15. supply chain: GPG + SHA256 of source tarballs =="
  199. sigs="$(grep -c "Good signature" "$BUILDLOG" 2>/dev/null)"
  200. if [ "$sigs" -ge 2 ]; then
  201. ok "GPG: Good signature x$sigs (unbound + ldns tarballs)"
  202. else
  203. bad "GPG: Good signature (found: $sigs, need >= 2)"
  204. fi
  205. if grep -E "unbound\.tar\.gz: OK|ldns\.tar\.gz: OK" "$BUILDLOG" >/dev/null 2>&1; then
  206. ok "SHA256: tarball checksums OK"
  207. else
  208. bad "SHA256: tarball checksums OK"
  209. fi
  210. say "== 16. binary hardening: PIE =="
  211. # ELF e_type: 3 = ET_DYN (PIE/shared). Read from the final binary itself,
  212. # so this works even on cached builds.
  213. etype="$(docker exec "$C1" sh -c 'od -An -N32 -td2 /usr/sbin/unbound' 2>/dev/null | awk 'NR==2{print $1}')"
  214. if [ "$etype" = "3" ]; then
  215. ok "unbound binary is PIE (ELF e_type=ET_DYN)"
  216. else
  217. bad "unbound binary is PIE (ELF e_type=$etype, expected 3)"
  218. fi
  219. say "== 17. features compiled in (configure line) =="
  220. confline="$(docker exec "$C1" unbound -V 2>/dev/null | grep -i 'configure line')"
  221. for flag in --enable-subnet --enable-tfo-server --enable-tfo-client --enable-pie --enable-relro-now; do
  222. if printf '%s' "$confline" | grep -q -- "$flag"; then
  223. ok "compiled with $flag"
  224. else
  225. bad "compiled with $flag"
  226. fi
  227. done
  228. say ""
  229. say "=================================================="
  230. say "RESULT: $PASS passed, $FAIL failed"
  231. say "=================================================="
  232. [ $FAIL -eq 0 ]