Dockerfile 7.7 KB

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