Dockerfile 8.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209
  1. # unbound-sec: security-hardened Unbound Docker image (crazymax/unbound replacement)
  2. #
  3. # NOTE: No `# syntax=docker/dockerfile:1` directive on purpose — it would force
  4. # BuildKit to pull the docker/dockerfile:1 frontend image on every build, which
  5. # fails on offline/WSL builds even when all base images are cached. The Dockerfile
  6. # uses no advanced syntax features, so the built-in BuildKit frontend suffices.
  7. #
  8. # Security deltas vs. crazymax/unbound:
  9. # - Unbound 1.26.2 (security release; fixes CVE-2026-97206, CVE-2026-96405,
  10. # CVE-2026-109125, CVE-2026-50106, CVE-2026-35501)
  11. # - no dns-root-hints package -> no curl/nghttp2/zlib/c-ares/brotli in runtime
  12. # (fixes CVE-2026-58055, CVE-2026-85091; unbound has root servers built in)
  13. # - current Alpine 3.24 expat 2.9.0 (fixes CVE-2026-77214, CVE-2026-93990,
  14. # CVE-2026-102633)
  15. # - shadow package removed after user creation -> no SUID-root binaries
  16. # - hide-identity / hide-version enabled (no version leak via CHAOS queries)
  17. #
  18. # Improvements adopted from the 10/2026 comparison with other unbound images
  19. # (mvance/unbound, klutchell/unbound, madnuttah/unbound — see README):
  20. # - source tarballs: SHA256 check + GPG signature (NLnet Labs release key G2)
  21. # - base images pinned by digest (reproducible builds)
  22. # - binary hardening: --enable-pie --enable-relro-now
  23. # - features: --enable-subnet (EDNS Client Subnet), TFO server + client
  24. ARG UNBOUND_VERSION=1.26.2
  25. ARG UNBOUND_SHA256=d8ee7aa6bbb3ff1be7d9e57637d0d0ec2f18273a8efff1a0afe2c665858b96f7
  26. ARG LDNS_VERSION=1.9.2
  27. ARG LDNS_SHA256=b524fa21994b6e834200ceb8c27f1b84bda5982fe35706f058196c079db94d5d
  28. # NLnet Labs release key G2 — independently verified fingerprint
  29. # (checked 2026-10-23 against nlnetlabs.nl/downloads/keys/releases-g2.asc).
  30. # The imported key must carry this fingerprint; otherwise the build fails.
  31. ARG GNUPG_FINGERPRINT=231018690C4D903EF419146AA144323DEAACDF45
  32. ARG XX_VERSION=1.9.0
  33. ARG XX_DIGEST=sha256:c64defb9ed5a91eacb37f96ccc3d4cd72521c4bd18d5442905b95e2226b0e707
  34. ARG ALPINE_VERSION=3.24
  35. ARG ALPINE_DIGEST=sha256:71e9b7bae160ba167fa17414401b94cbcdaf3c32feeab9298bd794a2a0614c35
  36. FROM --platform=$BUILDPLATFORM tonistiigi/xx:${XX_VERSION}@${XX_DIGEST} AS xx
  37. FROM --platform=$BUILDPLATFORM alpine:${ALPINE_VERSION}@${ALPINE_DIGEST} AS base
  38. COPY --from=xx / /
  39. RUN apk --update --no-cache add binutils clang curl file gnupg make pkgconf tar tree xz
  40. FROM base AS base-build
  41. ENV XX_CC_PREFER_LINKER=ld
  42. ARG TARGETPLATFORM
  43. RUN xx-apk --no-cache add gcc g++ expat-dev hiredis hiredis-dev libevent-dev libcap libpcap-dev openssl-dev perl \
  44. && xx-clang --setup-target-triple
  45. FROM base AS unbound-src
  46. WORKDIR /src/unbound
  47. ARG UNBOUND_VERSION
  48. ARG UNBOUND_SHA256
  49. ARG GNUPG_FINGERPRINT
  50. # CACHEBUST: test suite passes a timestamp so integrity verification (SHA256 +
  51. # GPG) re-runs on every test run even when the build stages are cached.
  52. # Default empty: plain builds (no --build-arg) must not trip `set -u`.
  53. ARG CACHEBUST=""
  54. # Retry flags for downloads: DNS on the build host can be flaky
  55. # (observed 2026-10-23: WSL/Windows resolver intermittently fails on
  56. # nlnetlabs.nl). --retry-all-errors covers DNS failures too (curl >= 7.71).
  57. RUN set -eux \
  58. && echo "cachebust=$CACHEBUST" \
  59. && curl -sSL --retry 3 --retry-delay 2 --retry-all-errors --max-time 120 \
  60. "https://unbound.net/downloads/unbound-${UNBOUND_VERSION}.tar.gz" -o unbound.tar.gz \
  61. && curl -sSL --retry 3 --retry-delay 2 --retry-all-errors --max-time 60 \
  62. "https://unbound.net/downloads/unbound-${UNBOUND_VERSION}.tar.gz.asc" -o unbound.tar.gz.asc \
  63. && echo "${UNBOUND_SHA256} unbound.tar.gz" | sha256sum -c - \
  64. && export GNUPGHOME="$(mktemp -d)" \
  65. && curl -sSL --retry 3 --retry-delay 2 --retry-all-errors --max-time 60 \
  66. "https://nlnetlabs.nl/downloads/keys/releases-g2.asc" -o "$GNUPGHOME/releases-g2.asc" \
  67. && gpg --no-tty --import "$GNUPGHOME/releases-g2.asc" \
  68. && gpg --no-tty --list-keys --with-colons \
  69. | awk -F: '/^fpr:/{print $10}' \
  70. | grep -qx "$GNUPG_FINGERPRINT" \
  71. && gpg --batch --verify --status-fd 1 unbound.tar.gz.asc unbound.tar.gz 2>/dev/null \
  72. | grep '^\[GNUPG:\] VALIDSIG' | awk '{print $3}' \
  73. | grep -qx "$GNUPG_FINGERPRINT" \
  74. && echo "GPG-VALIDSIG unbound.tar.gz signer=$GNUPG_FINGERPRINT" \
  75. && tar xzf unbound.tar.gz --strip 1
  76. FROM base AS ldns-src
  77. WORKDIR /src/ldns
  78. ARG LDNS_VERSION
  79. ARG LDNS_SHA256
  80. ARG GNUPG_FINGERPRINT
  81. ARG CACHEBUST=""
  82. RUN set -eux \
  83. && echo "cachebust=$CACHEBUST" \
  84. && curl -sSL --retry 3 --retry-delay 2 --retry-all-errors --max-time 120 \
  85. "https://nlnetlabs.nl/downloads/ldns/ldns-${LDNS_VERSION}.tar.gz" -o ldns.tar.gz \
  86. && curl -sSL --retry 3 --retry-delay 2 --retry-all-errors --max-time 60 \
  87. "https://nlnetlabs.nl/downloads/ldns/ldns-${LDNS_VERSION}.tar.gz.asc" -o ldns.tar.gz.asc \
  88. && echo "${LDNS_SHA256} ldns.tar.gz" | sha256sum -c - \
  89. && export GNUPGHOME="$(mktemp -d)" \
  90. && curl -sSL --retry 3 --retry-delay 2 --retry-all-errors --max-time 60 \
  91. "https://nlnetlabs.nl/downloads/keys/releases-g2.asc" -o "$GNUPGHOME/releases-g2.asc" \
  92. && gpg --no-tty --import "$GNUPGHOME/releases-g2.asc" \
  93. && gpg --no-tty --list-keys --with-colons \
  94. | awk -F: '/^fpr:/{print $10}' \
  95. | grep -qx "$GNUPG_FINGERPRINT" \
  96. && gpg --batch --verify --status-fd 1 ldns.tar.gz.asc ldns.tar.gz 2>/dev/null \
  97. | grep '^\[GNUPG:\] VALIDSIG' | awk '{print $3}' \
  98. | grep -qx "$GNUPG_FINGERPRINT" \
  99. && echo "GPG-VALIDSIG ldns.tar.gz signer=$GNUPG_FINGERPRINT" \
  100. && tar xzf ldns.tar.gz --strip 1
  101. FROM base-build AS unbound-build
  102. WORKDIR /src/unbound
  103. RUN --mount=type=bind,from=unbound-src,source=/src/unbound,target=.,rw <<EOT
  104. set -ex
  105. CC=xx-clang CXX=xx-clang++ ./configure \
  106. --host=$(xx-clang --print-target-triple) \
  107. --prefix=/usr \
  108. --sysconfdir=/etc \
  109. --mandir=/usr/share/man \
  110. --localstatedir=/var \
  111. --with-chroot-dir="" \
  112. --with-pidfile=/var/run/unbound/unbound.pid \
  113. --with-run-dir=/var/run/unbound \
  114. --with-username="" \
  115. --disable-flto \
  116. --disable-rpath \
  117. --disable-shared \
  118. --enable-cachedb \
  119. --enable-event-api \
  120. --enable-subnet \
  121. --enable-tfo-server \
  122. --enable-tfo-client \
  123. --enable-pie \
  124. --enable-relro-now \
  125. --with-pthreads \
  126. --with-libhiredis=$(xx-info sysroot)usr \
  127. --with-libexpat=$(xx-info sysroot)usr \
  128. --with-libevent=$(xx-info sysroot)usr \
  129. --with-ssl=$(xx-info sysroot)usr
  130. make DESTDIR=/out install
  131. make DESTDIR=/out unbound-event-install
  132. install -Dm755 contrib/update-anchor.sh /out/usr/share/unbound/update-anchor.sh
  133. xx-verify /out/usr/sbin/unbound
  134. xx-verify /out/usr/sbin/unbound-anchor
  135. xx-verify /out/usr/sbin/unbound-checkconf
  136. xx-verify /out/usr/sbin/unbound-control
  137. xx-verify /out/usr/sbin/unbound-host
  138. # build-log evidence for the test suite (asserts "pie executable")
  139. file /out/usr/sbin/unbound
  140. EOT
  141. FROM base-build AS ldns-build
  142. WORKDIR /src/ldns
  143. RUN --mount=type=bind,from=ldns-src,source=/src/ldns,target=.,rw <<EOT
  144. set -ex
  145. CC=xx-clang CXX=xx-clang++ CPPFLAGS=-I/src/ldns/ldns ./configure \
  146. --host=$(xx-clang --print-target-triple) \
  147. --prefix=/usr \
  148. --sysconfdir=/etc \
  149. --mandir=/usr/share/man \
  150. --infodir=/usr/share/info \
  151. --localstatedir=/var \
  152. --disable-gost \
  153. --disable-rpath \
  154. --disable-shared \
  155. --with-drill \
  156. --with-ssl=$(xx-info sysroot)usr
  157. make DESTDIR=/out install
  158. xx-verify /out/usr/bin/drill
  159. file /out/usr/bin/drill
  160. EOT
  161. FROM alpine:${ALPINE_VERSION}@${ALPINE_DIGEST}
  162. COPY --from=unbound-build /out /
  163. COPY --from=ldns-build /out /
  164. # Runtime deps only. No dns-root-hints (would pull curl+nghttp2+zlib; unbound
  165. # has root servers built in), no shadow (only needed at build for adduser).
  166. RUN apk --update --no-cache add \
  167. ca-certificates \
  168. dnssec-root \
  169. expat \
  170. hiredis \
  171. libevent \
  172. libpcap \
  173. openssl \
  174. && mkdir -p /run/unbound /var/unbound \
  175. && unbound -V \
  176. && ldns-config --version
  177. COPY entrypoint.sh /entrypoint.sh
  178. COPY config/unbound.conf /etc/unbound/unbound.conf
  179. RUN chmod 755 /entrypoint.sh \
  180. && mkdir -p /config \
  181. && addgroup -g 1500 unbound \
  182. && adduser -D -H -u 1500 -G unbound -s /bin/sh unbound \
  183. && chown -R unbound:unbound /etc/unbound /run/unbound /var/unbound \
  184. && apk del --no-cache shadow \
  185. && rm -rf /tmp/* /var/cache/apk/*
  186. USER unbound
  187. EXPOSE 5053/tcp
  188. EXPOSE 5053/udp
  189. VOLUME ["/config", "/var/unbound"]
  190. HEALTHCHECK --interval=30s --timeout=10s \
  191. CMD drill -p 5053 unbound.net @127.0.0.1
  192. ENTRYPOINT ["sh", "/entrypoint.sh"]
  193. CMD [""]