Skip to content

Commit a8bd2d3

Browse files
committed
Inherit NET_BIND_SERVICE from IC to Nginx
8be0144: Rework port binding logic without privileges caused issues for host networking configurations. The Kubernetes documentation states that the `net.*` sysctls can be used with container networking, which was misinterpreted. This commit reverts the change, bringing back NET_BIND_SERVICE to the Nginx process, as well as reverts the libcap package removal done in a later commit. In order to avoid privilege escalation being re-introduced, the IC process is also receiving NET_BIND_SERVICE, so that it can be inherited over to Nginx. This change aims to restore host networking as functional for the Helm chart. A future change is recommended to harden security for the IC process (to drop the capability after executing Nginx) as well as Nginx itself (to drop the capability after binding). OBS! To use a 3.1.0 image, you should manually install the `setcap` binary and add `+ep` on `/nginx-ingress` and `+eip` on `nginx` binary.
1 parent 9d57002 commit a8bd2d3

File tree

7 files changed

+39
-24
lines changed

7 files changed

+39
-24
lines changed

build/Dockerfile

Lines changed: 27 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,10 @@ FROM opentracing/nginx-opentracing:nginx-1.23.4-alpine as alpine-opentracing-lib
1313
FROM nginx:1.23.4 AS debian
1414

1515
RUN --mount=type=bind,from=opentracing-lib,target=/tmp/ot/ \
16-
cp -av /tmp/ot/usr/local/lib/libopentracing.so* /tmp/ot/usr/local/lib/libjaegertracing*so* /tmp/ot/usr/local/lib/libzipkin*so* /tmp/ot/usr/local/lib/libdd*so* /tmp/ot/usr/local/lib/libyaml*so* /usr/local/lib/ \
16+
apt-get update \
17+
&& apt-get install --no-install-recommends --no-install-suggests -y libcap2-bin \
18+
&& rm -rf /var/lib/apt/lists/* \
19+
&& cp -av /tmp/ot/usr/local/lib/libopentracing.so* /tmp/ot/usr/local/lib/libjaegertracing*so* /tmp/ot/usr/local/lib/libzipkin*so* /tmp/ot/usr/local/lib/libdd*so* /tmp/ot/usr/local/lib/libyaml*so* /usr/local/lib/ \
1720
&& cp -av /tmp/ot/usr/lib/nginx/modules/ngx_http_opentracing_module.so /usr/lib/nginx/modules/ \
1821
&& ldconfig
1922

@@ -22,7 +25,7 @@ RUN --mount=type=bind,from=opentracing-lib,target=/tmp/ot/ \
2225
FROM nginx:1.23.4-alpine AS alpine
2326

2427
RUN --mount=type=bind,from=alpine-opentracing-lib,target=/tmp/ot/ \
25-
apk add --no-cache libstdc++ \
28+
apk add --no-cache libcap libstdc++ \
2629
# temp fix for CVE-2023-23916
2730
&& apk upgrade --no-cache curl libcurl \
2831
&& cp -av /tmp/ot/usr/local/lib/libopentracing.so* /tmp/ot/usr/local/lib/libjaegertracing*so* /tmp/ot/usr/local/lib/libzipkin*so* /tmp/ot/usr/local/lib/libdd*so* /tmp/ot/usr/local/lib/libyaml*so* /usr/local/lib/ \
@@ -39,7 +42,7 @@ RUN --mount=type=secret,id=nginx-repo.crt,dst=/etc/apk/cert.pem,mode=0644 \
3942
--mount=type=bind,from=alpine-opentracing-lib,target=/tmp/ot/ \
4043
wget -nv -O /etc/apk/keys/nginx_signing.rsa.pub https://cs.nginx.com/static/keys/nginx_signing.rsa.pub \
4144
&& printf "%s\n" "https://pkgs.nginx.com/plus/${NGINX_PLUS_VERSION}/alpine/v$(grep -E -o '^[0-9]+\.[0-9]+' /etc/alpine-release)/main" >> /etc/apk/repositories \
42-
&& apk add --no-cache nginx-plus nginx-plus-module-njs nginx-plus-module-opentracing libcurl \
45+
&& apk add --no-cache nginx-plus nginx-plus-module-njs nginx-plus-module-opentracing libcap libcurl \
4346
&& cp -av /tmp/ot/usr/local/lib/libjaegertracing*so* /tmp/ot/usr/local/lib/libzipkin*so* /tmp/ot/usr/local/lib/libdd*so* /tmp/ot/usr/local/lib/libyaml*so* /usr/local/lib/ \
4447
&& ldconfig /usr/local/lib/
4548

@@ -62,7 +65,7 @@ RUN --mount=type=secret,id=nginx-repo.crt,dst=/etc/ssl/nginx/nginx-repo.crt,mode
6265
&& printf "%s\n" "Acquire::https::pkgs.nginx.com::User-Agent \"k8s-ic-$IC_VERSION${BUILD_OS##debian-plus}-apt\";" >> /etc/apt/apt.conf.d/90pkgs-nginx \
6366
&& printf "%s\n" "deb https://pkgs.nginx.com/plus/${NGINX_PLUS_VERSION}/debian ${DEBIAN_VERSION} nginx-plus" > /etc/apt/sources.list.d/nginx-plus.list \
6467
&& apt-get update \
65-
&& apt-get install --no-install-recommends --no-install-suggests -y nginx-plus nginx-plus-module-njs nginx-plus-module-opentracing libcurl4 \
68+
&& apt-get install --no-install-recommends --no-install-suggests -y nginx-plus nginx-plus-module-njs nginx-plus-module-opentracing libcap2-bin libcurl4 \
6669
&& apt-get purge --auto-remove -y apt-transport-https gnupg curl \
6770
&& cp -av /tmp/ot/usr/local/lib/libjaegertracing*so* /tmp/ot/usr/local/lib/libzipkin*so* /tmp/ot/usr/local/lib/libdd*so* /tmp/ot/usr/local/lib/libyaml*so* /usr/local/lib/ \
6871
&& ldconfig \
@@ -185,7 +188,7 @@ RUN --mount=type=secret,id=nginx-repo.crt,dst=/etc/ssl/nginx/nginx-repo.crt,mode
185188
# RUN update-ca-trust extract
186189

187190

188-
############################################# Create common files and permissions #############################################
191+
############################################# Create common files, permissions and setcap #############################################
189192
FROM ${BUILD_OS} as common
190193

191194
ARG BUILD_OS
@@ -208,6 +211,8 @@ RUN [ -n "${NAP_MODULES##*dos*}" ] && exit 0; mkdir -p /root/app_protect_dos /et
208211
&& chmod 777 /shared/cores /var/log/adm /var/run/adm /etc/app_protect_dos
209212

210213
RUN --mount=type=bind,target=/tmp mkdir -p /var/lib/nginx /etc/nginx/secrets /etc/nginx/stream-conf.d \
214+
&& setcap 'cap_net_bind_service=+eip' /usr/sbin/nginx 'cap_net_bind_service=+eip' /usr/sbin/nginx-debug \
215+
&& setcap -v 'cap_net_bind_service=+eip' /usr/sbin/nginx 'cap_net_bind_service=+eip' /usr/sbin/nginx-debug \
211216
&& [ -z "${BUILD_OS##*plus*}" ] && PLUS=-plus; cp -a /tmp/internal/configs/version1/nginx$PLUS.ingress.tmpl /tmp/internal/configs/version1/nginx$PLUS.tmpl \
212217
/tmp/internal/configs/version2/nginx$PLUS.virtualserver.tmpl /tmp/internal/configs/version2/nginx$PLUS.transportserver.tmpl / \
213218
&& chown -R 101:0 /etc/nginx /var/cache/nginx /var/lib/nginx /*.tmpl \
@@ -237,11 +242,12 @@ ARG IC_VERSION
237242
ARG TARGETARCH
238243

239244
WORKDIR /go/src/github.com/nginxinc/kubernetes-ingress/
240-
RUN apk add --no-cache git
245+
RUN apk add --no-cache git libcap
241246
RUN --mount=type=bind,target=/go/src/github.com/nginxinc/kubernetes-ingress/ --mount=type=cache,target=/root/.cache/go-build \
242247
go mod download
243248
RUN --mount=type=bind,target=/go/src/github.com/nginxinc/kubernetes-ingress/ --mount=type=cache,target=/root/.cache/go-build \
244249
CGO_ENABLED=0 GOOS=linux GOARCH=$TARGETARCH go build -trimpath -ldflags "-s -w -X main.version=${IC_VERSION}" -o /nginx-ingress github.com/nginxinc/kubernetes-ingress/cmd/nginx-ingress
250+
RUN --mount=type=bind,target=/tmp setcap 'cap_net_bind_service=+ep' /nginx-ingress && setcap -v 'cap_net_bind_service=+ep' /nginx-ingress
245251

246252

247253
############################################# Create image with nginx-ingress built in container #############################################
@@ -258,6 +264,11 @@ FROM common AS local
258264
LABEL org.nginx.kic.image.build.version="local"
259265

260266
COPY --link --chown=101:0 nginx-ingress /
267+
# root is required for `setcap` invocation
268+
USER 0
269+
RUN --mount=type=bind,target=/tmp setcap 'cap_net_bind_service=+ep' /nginx-ingress && setcap -v 'cap_net_bind_service=+ep' /nginx-ingress
270+
# 101 is nginx, defined above
271+
USER 101
261272

262273

263274
############################################# Create image with nginx-ingress built by GoReleaser #############################################
@@ -267,6 +278,11 @@ ARG TARGETARCH
267278
LABEL org.nginx.kic.image.build.version="goreleaser"
268279

269280
COPY --link --chown=101:0 dist/kubernetes-ingress_linux_${TARGETARCH}*/nginx-ingress /
281+
# root is required for `setcap` invocation
282+
USER 0
283+
RUN --mount=type=bind,target=/tmp setcap 'cap_net_bind_service=+ep' /nginx-ingress && setcap -v 'cap_net_bind_service=+ep' /nginx-ingress
284+
# 101 is nginx, defined above
285+
USER 101
270286

271287

272288
############################################# Create image with nginx-ingress built by GoReleaser for AWS Marketplace #############################################
@@ -277,6 +293,11 @@ ARG NAP_MODULES_AWS
277293
LABEL org.nginx.kic.image.build.version="aws"
278294

279295
COPY --link --chown=101:0 dist/aws*${NAP_MODULES_AWS}_linux_${TARGETARCH}*/nginx-ingress /
296+
# root is required for `setcap` invocation
297+
USER 0
298+
RUN --mount=type=bind,target=/tmp setcap 'cap_net_bind_service=+ep' /nginx-ingress && setcap -v 'cap_net_bind_service=+ep' /nginx-ingress
299+
# 101 is nginx, defined above
300+
USER 101
280301

281302

282303
############################################# Create image with nginx-ingress extracted from image on Docker Hub #############################################

deployments/daemon-set/nginx-ingress.yaml

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -23,9 +23,6 @@ spec:
2323
seccompProfile:
2424
type: RuntimeDefault
2525
# fsGroup: 101 #nginx
26-
sysctls:
27-
- name: "net.ipv4.ip_unprivileged_port_start"
28-
value: "0"
2926
# volumes:
3027
# - name: nginx-etc
3128
# emptyDir: {}
@@ -69,6 +66,8 @@ spec:
6966
capabilities:
7067
drop:
7168
- ALL
69+
add:
70+
- NET_BIND_SERVICE
7271
# volumeMounts:
7372
# - mountPath: /etc/nginx
7473
# name: nginx-etc

deployments/daemon-set/nginx-plus-ingress.yaml

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -23,9 +23,6 @@ spec:
2323
seccompProfile:
2424
type: RuntimeDefault
2525
# fsGroup: 101 #nginx
26-
sysctls:
27-
- name: "net.ipv4.ip_unprivileged_port_start"
28-
value: "0"
2926
# volumes:
3027
# - name: nginx-etc
3128
# emptyDir: {}
@@ -69,6 +66,8 @@ spec:
6966
capabilities:
7067
drop:
7168
- ALL
69+
add:
70+
- NET_BIND_SERVICE
7271
# volumeMounts:
7372
# - mountPath: /etc/nginx
7473
# name: nginx-etc

deployments/deployment/nginx-ingress.yaml

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -24,9 +24,6 @@ spec:
2424
seccompProfile:
2525
type: RuntimeDefault
2626
# fsGroup: 101 #nginx
27-
sysctls:
28-
- name: "net.ipv4.ip_unprivileged_port_start"
29-
value: "0"
3027
# volumes:
3128
# - name: nginx-etc
3229
# emptyDir: {}
@@ -69,6 +66,8 @@ spec:
6966
capabilities:
7067
drop:
7168
- ALL
69+
add:
70+
- NET_BIND_SERVICE
7271
# volumeMounts:
7372
# - mountPath: /etc/nginx
7473
# name: nginx-etc

deployments/deployment/nginx-plus-ingress.yaml

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -24,9 +24,6 @@ spec:
2424
seccompProfile:
2525
type: RuntimeDefault
2626
# fsGroup: 101 #nginx
27-
sysctls:
28-
- name: "net.ipv4.ip_unprivileged_port_start"
29-
value: "0"
3027
# volumes:
3128
# - name: nginx-etc
3229
# emptyDir: {}
@@ -71,6 +68,8 @@ spec:
7168
capabilities:
7269
drop:
7370
- ALL
71+
add:
72+
- NET_BIND_SERVICE
7473
# volumeMounts:
7574
# - mountPath: /etc/nginx
7675
# name: nginx-etc

deployments/helm-chart/templates/controller-daemonset.yaml

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -48,9 +48,6 @@ spec:
4848
{{- if .Values.controller.readOnlyRootFilesystem }}
4949
fsGroup: 101 #nginx
5050
{{- end }}
51-
sysctls:
52-
- name: "net.ipv4.ip_unprivileged_port_start"
53-
value: "0"
5451
terminationGracePeriodSeconds: {{ .Values.controller.terminationGracePeriodSeconds }}
5552
{{- if .Values.controller.nodeSelector }}
5653
nodeSelector:
@@ -135,6 +132,8 @@ spec:
135132
capabilities:
136133
drop:
137134
- ALL
135+
add:
136+
- NET_BIND_SERVICE
138137
{{- if or .Values.controller.readOnlyRootFilesystem .Values.nginxServiceMesh.enable .Values.controller.volumeMounts }}
139138
volumeMounts:
140139
{{- end }}

deployments/helm-chart/templates/controller-deployment.yaml

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -90,9 +90,6 @@ spec:
9090
{{- if .Values.controller.readOnlyRootFilesystem }}
9191
fsGroup: 101 #nginx
9292
{{- end }}
93-
sysctls:
94-
- name: "net.ipv4.ip_unprivileged_port_start"
95-
value: "0"
9693
terminationGracePeriodSeconds: {{ .Values.controller.terminationGracePeriodSeconds }}
9794
hostNetwork: {{ .Values.controller.hostNetwork }}
9895
dnsPolicy: {{ .Values.controller.dnsPolicy }}
@@ -140,6 +137,8 @@ spec:
140137
capabilities:
141138
drop:
142139
- ALL
140+
add:
141+
- NET_BIND_SERVICE
143142
{{- if or .Values.controller.readOnlyRootFilesystem .Values.nginxServiceMesh.enable .Values.controller.volumeMounts }}
144143
volumeMounts:
145144
{{- end }}

0 commit comments

Comments
 (0)