Skip to content

Commit dcc05cb

Browse files
sigvlucacome
authored andcommitted
Inherit NET_BIND_SERVICE from IC to Nginx (#3722)
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. (cherry picked from commit 5d56f71)
1 parent 69e071f commit dcc05cb

File tree

7 files changed

+42
-23
lines changed

7 files changed

+42
-23
lines changed

build/Dockerfile

Lines changed: 30 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,10 @@ FROM opentracing/nginx-opentracing:nginx-1.23.3-alpine as alpine-opentracing-lib
1313
FROM nginx:1.23.3 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.3-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 \
@@ -250,6 +255,11 @@ FROM common AS container
250255
LABEL org.nginx.kic.image.build.version="container"
251256

252257
COPY --link --from=builder --chown=101:0 /nginx-ingress /
258+
# root is required for `setcap` invocation
259+
USER 0
260+
RUN setcap 'cap_net_bind_service=+ep' /nginx-ingress && setcap -v 'cap_net_bind_service=+ep' /nginx-ingress
261+
# 101 is nginx, defined above
262+
USER 101
253263

254264

255265
############################################# Create image with nginx-ingress built locally #############################################
@@ -258,6 +268,11 @@ FROM common AS local
258268
LABEL org.nginx.kic.image.build.version="local"
259269

260270
COPY --link --chown=101:0 nginx-ingress /
271+
# root is required for `setcap` invocation
272+
USER 0
273+
RUN setcap 'cap_net_bind_service=+ep' /nginx-ingress && setcap -v 'cap_net_bind_service=+ep' /nginx-ingress
274+
# 101 is nginx, defined above
275+
USER 101
261276

262277

263278
############################################# Create image with nginx-ingress built by GoReleaser #############################################
@@ -267,6 +282,11 @@ ARG TARGETARCH
267282
LABEL org.nginx.kic.image.build.version="goreleaser"
268283

269284
COPY --link --chown=101:0 dist/kubernetes-ingress_linux_${TARGETARCH}*/nginx-ingress /
285+
# root is required for `setcap` invocation
286+
USER 0
287+
RUN setcap 'cap_net_bind_service=+ep' /nginx-ingress && setcap -v 'cap_net_bind_service=+ep' /nginx-ingress
288+
# 101 is nginx, defined above
289+
USER 101
270290

271291

272292
############################################# Create image with nginx-ingress built by GoReleaser for AWS Marketplace #############################################
@@ -277,6 +297,11 @@ ARG NAP_MODULES_AWS
277297
LABEL org.nginx.kic.image.build.version="aws"
278298

279299
COPY --link --chown=101:0 dist/aws*${NAP_MODULES_AWS}_linux_${TARGETARCH}*/nginx-ingress /
300+
# root is required for `setcap` invocation
301+
USER 0
302+
RUN setcap 'cap_net_bind_service=+ep' /nginx-ingress && setcap -v 'cap_net_bind_service=+ep' /nginx-ingress
303+
# 101 is nginx, defined above
304+
USER 101
280305

281306

282307
############################################# 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
@@ -45,9 +45,6 @@ spec:
4545
{{- if .Values.controller.readOnlyRootFilesystem }}
4646
fsGroup: 101 #nginx
4747
{{- end }}
48-
sysctls:
49-
- name: "net.ipv4.ip_unprivileged_port_start"
50-
value: "0"
5148
terminationGracePeriodSeconds: {{ .Values.controller.terminationGracePeriodSeconds }}
5249
{{- if .Values.controller.nodeSelector }}
5350
nodeSelector:
@@ -126,6 +123,8 @@ spec:
126123
capabilities:
127124
drop:
128125
- ALL
126+
add:
127+
- NET_BIND_SERVICE
129128
{{- if or .Values.controller.readOnlyRootFilesystem .Values.controller.volumeMounts }}
130129
volumeMounts:
131130
{{- end }}

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

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -83,9 +83,6 @@ spec:
8383
{{- if .Values.controller.readOnlyRootFilesystem }}
8484
fsGroup: 101 #nginx
8585
{{- end }}
86-
sysctls:
87-
- name: "net.ipv4.ip_unprivileged_port_start"
88-
value: "0"
8986
terminationGracePeriodSeconds: {{ .Values.controller.terminationGracePeriodSeconds }}
9087
hostNetwork: {{ .Values.controller.hostNetwork }}
9188
dnsPolicy: {{ .Values.controller.dnsPolicy }}
@@ -133,6 +130,8 @@ spec:
133130
capabilities:
134131
drop:
135132
- ALL
133+
add:
134+
- NET_BIND_SERVICE
136135
{{- if or .Values.controller.readOnlyRootFilesystem .Values.controller.volumeMounts }}
137136
volumeMounts:
138137
{{- end }}

0 commit comments

Comments
 (0)