Dockerfile 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188
  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. ARG XX_VERSION=1.9.0
  26. ARG XX_DIGEST=sha256:c64defb9ed5a91eacb37f96ccc3d4cd72521c4bd18d5442905b95e2226b0e707
  27. ARG ALPINE_VERSION=3.24
  28. ARG ALPINE_DIGEST=sha256:71e9b7bae160ba167fa17414401b94cbcdaf3c32feeab9298bd794a2a0614c35
  29. FROM --platform=$BUILDPLATFORM tonistiigi/xx:${XX_VERSION}@${XX_DIGEST} AS xx
  30. FROM --platform=$BUILDPLATFORM alpine:${ALPINE_VERSION}@${ALPINE_DIGEST} AS base
  31. COPY --from=xx / /
  32. RUN apk --update --no-cache add binutils clang curl file gnupg make pkgconf tar tree xz
  33. FROM base AS base-build
  34. ENV XX_CC_PREFER_LINKER=ld
  35. ARG TARGETPLATFORM
  36. RUN xx-apk --no-cache add gcc g++ expat-dev hiredis hiredis-dev libevent-dev libcap libpcap-dev openssl-dev perl \
  37. && xx-clang --setup-target-triple
  38. FROM base AS unbound-src
  39. WORKDIR /src/unbound
  40. ARG UNBOUND_VERSION
  41. ARG UNBOUND_SHA256
  42. # CACHEBUST: test suite passes a timestamp so integrity verification (SHA256 +
  43. # GPG) re-runs on every test run even when the build stages are cached.
  44. # Default empty: plain builds (no --build-arg) must not trip `set -u`.
  45. ARG CACHEBUST=""
  46. # Retry flags for downloads: DNS on the build host can be flaky
  47. # (observed 2026-10-23: WSL/Windows resolver intermittently fails on
  48. # nlnetlabs.nl). --retry-all-errors covers DNS failures too (curl >= 7.71).
  49. RUN set -eux \
  50. && echo "cachebust=$CACHEBUST" \
  51. && curl -sSL --retry 3 --retry-delay 2 --retry-all-errors --max-time 120 \
  52. "https://unbound.net/downloads/unbound-${UNBOUND_VERSION}.tar.gz" -o unbound.tar.gz \
  53. && curl -sSL --retry 3 --retry-delay 2 --retry-all-errors --max-time 60 \
  54. "https://unbound.net/downloads/unbound-${UNBOUND_VERSION}.tar.gz.asc" -o unbound.tar.gz.asc \
  55. && echo "${UNBOUND_SHA256} unbound.tar.gz" | sha256sum -c - \
  56. && export GNUPGHOME="$(mktemp -d)" \
  57. && curl -sSL --retry 3 --retry-delay 2 --retry-all-errors --max-time 60 \
  58. "https://nlnetlabs.nl/downloads/keys/releases-g2.asc" -o "$GNUPGHOME/releases-g2.asc" \
  59. && gpg --no-tty --import "$GNUPGHOME/releases-g2.asc" \
  60. && gpg --batch --verify unbound.tar.gz.asc unbound.tar.gz \
  61. && tar xzf unbound.tar.gz --strip 1
  62. FROM base AS ldns-src
  63. WORKDIR /src/ldns
  64. ARG LDNS_VERSION
  65. ARG LDNS_SHA256
  66. ARG CACHEBUST=""
  67. RUN set -eux \
  68. && echo "cachebust=$CACHEBUST" \
  69. && curl -sSL --retry 3 --retry-delay 2 --retry-all-errors --max-time 120 \
  70. "https://nlnetlabs.nl/downloads/ldns/ldns-${LDNS_VERSION}.tar.gz" -o ldns.tar.gz \
  71. && curl -sSL --retry 3 --retry-delay 2 --retry-all-errors --max-time 60 \
  72. "https://nlnetlabs.nl/downloads/ldns/ldns-${LDNS_VERSION}.tar.gz.asc" -o ldns.tar.gz.asc \
  73. && echo "${LDNS_SHA256} ldns.tar.gz" | sha256sum -c - \
  74. && export GNUPGHOME="$(mktemp -d)" \
  75. && curl -sSL --retry 3 --retry-delay 2 --retry-all-errors --max-time 60 \
  76. "https://nlnetlabs.nl/downloads/keys/releases-g2.asc" -o "$GNUPGHOME/releases-g2.asc" \
  77. && gpg --no-tty --import "$GNUPGHOME/releases-g2.asc" \
  78. && gpg --batch --verify ldns.tar.gz.asc ldns.tar.gz \
  79. && tar xzf ldns.tar.gz --strip 1
  80. FROM base-build AS unbound-build
  81. WORKDIR /src/unbound
  82. RUN --mount=type=bind,from=unbound-src,source=/src/unbound,target=.,rw <<EOT
  83. set -ex
  84. CC=xx-clang CXX=xx-clang++ ./configure \
  85. --host=$(xx-clang --print-target-triple) \
  86. --prefix=/usr \
  87. --sysconfdir=/etc \
  88. --mandir=/usr/share/man \
  89. --localstatedir=/var \
  90. --with-chroot-dir="" \
  91. --with-pidfile=/var/run/unbound/unbound.pid \
  92. --with-run-dir=/var/run/unbound \
  93. --with-username="" \
  94. --disable-flto \
  95. --disable-rpath \
  96. --disable-shared \
  97. --enable-cachedb \
  98. --enable-event-api \
  99. --enable-subnet \
  100. --enable-tfo-server \
  101. --enable-tfo-client \
  102. --enable-pie \
  103. --enable-relro-now \
  104. --with-pthreads \
  105. --with-libhiredis=$(xx-info sysroot)usr \
  106. --with-libexpat=$(xx-info sysroot)usr \
  107. --with-libevent=$(xx-info sysroot)usr \
  108. --with-ssl=$(xx-info sysroot)usr
  109. make DESTDIR=/out install
  110. make DESTDIR=/out unbound-event-install
  111. install -Dm755 contrib/update-anchor.sh /out/usr/share/unbound/update-anchor.sh
  112. xx-verify /out/usr/sbin/unbound
  113. xx-verify /out/usr/sbin/unbound-anchor
  114. xx-verify /out/usr/sbin/unbound-checkconf
  115. xx-verify /out/usr/sbin/unbound-control
  116. xx-verify /out/usr/sbin/unbound-host
  117. # build-log evidence for the test suite (asserts "pie executable")
  118. file /out/usr/sbin/unbound
  119. EOT
  120. FROM base-build AS ldns-build
  121. WORKDIR /src/ldns
  122. RUN --mount=type=bind,from=ldns-src,source=/src/ldns,target=.,rw <<EOT
  123. set -ex
  124. CC=xx-clang CXX=xx-clang++ CPPFLAGS=-I/src/ldns/ldns ./configure \
  125. --host=$(xx-clang --print-target-triple) \
  126. --prefix=/usr \
  127. --sysconfdir=/etc \
  128. --mandir=/usr/share/man \
  129. --infodir=/usr/share/info \
  130. --localstatedir=/var \
  131. --disable-gost \
  132. --disable-rpath \
  133. --disable-shared \
  134. --with-drill \
  135. --with-ssl=$(xx-info sysroot)usr
  136. make DESTDIR=/out install
  137. xx-verify /out/usr/bin/drill
  138. file /out/usr/bin/drill
  139. EOT
  140. FROM alpine:${ALPINE_VERSION}@${ALPINE_DIGEST}
  141. COPY --from=unbound-build /out /
  142. COPY --from=ldns-build /out /
  143. # Runtime deps only. No dns-root-hints (would pull curl+nghttp2+zlib; unbound
  144. # has root servers built in), no shadow (only needed at build for adduser).
  145. RUN apk --update --no-cache add \
  146. ca-certificates \
  147. dnssec-root \
  148. expat \
  149. hiredis \
  150. libevent \
  151. libpcap \
  152. openssl \
  153. && mkdir -p /run/unbound \
  154. && unbound -V \
  155. && ldns-config --version
  156. COPY entrypoint.sh /entrypoint.sh
  157. COPY config/unbound.conf /etc/unbound/unbound.conf
  158. RUN chmod 755 /entrypoint.sh \
  159. && mkdir -p /config \
  160. && addgroup -g 1500 unbound \
  161. && adduser -D -H -u 1500 -G unbound -s /bin/sh unbound \
  162. && chown -R unbound:unbound /etc/unbound /run/unbound \
  163. && apk del --no-cache shadow \
  164. && rm -rf /tmp/* /var/cache/apk/*
  165. USER unbound
  166. EXPOSE 5053/tcp
  167. EXPOSE 5053/udp
  168. VOLUME ["/config"]
  169. HEALTHCHECK --interval=30s --timeout=10s \
  170. CMD drill -p 5053 unbound.net @127.0.0.1
  171. ENTRYPOINT ["sh", "/entrypoint.sh"]
  172. CMD [""]