Skip to content

Commit 573828e

Browse files
authored
Fix port binding with reduced privileges (#3574)
Problem: The nginx deployment was using extra privileges in order to bind to privileged ports (<1024). This included `allowPrivilegeEscalation` and `NET_BIND_SERVICE`. Sometimes this could cause issues in some secure environments. Solution: Remove these extra privileges and take advantage of `sysctls` to lower the allowed port range for the pod its defined on.
1 parent a523d21 commit 573828e

File tree

5 files changed

+11
-17
lines changed

5 files changed

+11
-17
lines changed

build/Dockerfile.nginx

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -16,15 +16,10 @@ RUN --mount=type=bind,from=nginx-files,src=nginx_signing.rsa.pub,target=/etc/apk
1616
printf "%s\n" "https://packages.nginx.org/nginx-agent/alpine/v$(egrep -o '^[0-9]+\.[0-9]+' /etc/alpine-release)/main" >> /etc/apk/repositories \
1717
&& apk add --no-cache nginx-agent=${NGINX_AGENT_VERSION#v}
1818

19-
RUN apk add --no-cache libcap bash \
19+
RUN apk add --no-cache bash \
2020
&& mkdir -p /usr/lib/nginx/modules \
21-
&& setcap 'cap_net_bind_service=+ep' /usr/sbin/nginx \
22-
&& setcap -v 'cap_net_bind_service=+ep' /usr/sbin/nginx \
23-
&& setcap 'cap_net_bind_service=+ep' /usr/sbin/nginx-debug \
24-
&& setcap -v 'cap_net_bind_service=+ep' /usr/sbin/nginx-debug \
2521
# Update packages for CVE-2025-32414 and CVE-2025-32415
2622
&& apk --no-cache upgrade libxml2 \
27-
&& apk del libcap \
2823
# forward request and error logs to docker log collector
2924
&& ln -sf /dev/stdout /var/log/nginx/access.log \
3025
&& ln -sf /dev/stderr /var/log/nginx/error.log

build/Dockerfile.nginxplus

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -22,13 +22,8 @@ RUN --mount=type=secret,id=nginx-repo.crt,dst=/etc/apk/cert.pem,mode=0644 \
2222
&& printf "%s\n" "https://pkgs.nginx.com/nginx-agent/alpine/v$(egrep -o '^[0-9]+\.[0-9]+' /etc/alpine-release)/main" >> /etc/apk/repositories \
2323
&& apk add --no-cache nginx-plus nginx-plus-module-njs nginx-plus-module-otel nginx-agent=${NGINX_AGENT_VERSION#v}
2424

25-
RUN apk add --no-cache libcap bash \
25+
RUN apk add --no-cache bash \
2626
&& mkdir -p /usr/lib/nginx/modules \
27-
&& setcap 'cap_net_bind_service=+ep' /usr/sbin/nginx \
28-
&& setcap -v 'cap_net_bind_service=+ep' /usr/sbin/nginx \
29-
&& setcap 'cap_net_bind_service=+ep' /usr/sbin/nginx-debug \
30-
&& setcap -v 'cap_net_bind_service=+ep' /usr/sbin/nginx-debug \
31-
&& apk del libcap \
3227
# forward request and error logs to docker log collector
3328
&& ln -sf /dev/stdout /var/log/nginx/access.log \
3429
&& ln -sf /dev/stderr /var/log/nginx/error.log

charts/nginx-gateway-fabric/templates/scc.yaml

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,7 @@ metadata:
4444
name: {{ include "nginx-gateway.scc-name" . }}-nginx
4545
labels:
4646
{{- include "nginx-gateway.labels" . | nindent 4 }}
47+
allowPrivilegeEscalation: false
4748
allowHostDirVolumePlugin: false
4849
allowHostIPC: false
4950
allowHostNetwork: false
@@ -69,8 +70,6 @@ seLinuxContext:
6970
type: MustRunAs
7071
seccompProfiles:
7172
- runtime/default
72-
allowedCapabilities:
73-
- NET_BIND_SERVICE
7473
requiredDropCapabilities:
7574
- ALL
7675
volumes:

deploy/openshift/deploy.yaml

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -529,9 +529,8 @@ allowHostIPC: false
529529
allowHostNetwork: false
530530
allowHostPID: false
531531
allowHostPorts: false
532+
allowPrivilegeEscalation: false
532533
allowPrivilegedContainer: false
533-
allowedCapabilities:
534-
- NET_BIND_SERVICE
535534
apiVersion: security.openshift.io/v1
536535
fsGroup:
537536
ranges:

internal/controller/provisioner/objects.go

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -617,8 +617,8 @@ func (p *NginxProvisioner) buildNginxPodTemplateSpec(
617617
ImagePullPolicy: pullPolicy,
618618
Ports: containerPorts,
619619
SecurityContext: &corev1.SecurityContext{
620+
AllowPrivilegeEscalation: helpers.GetPointer(false),
620621
Capabilities: &corev1.Capabilities{
621-
Add: []corev1.Capability{"NET_BIND_SERVICE"},
622622
Drop: []corev1.Capability{"ALL"},
623623
},
624624
ReadOnlyRootFilesystem: helpers.GetPointer(true),
@@ -691,6 +691,12 @@ func (p *NginxProvisioner) buildNginxPodTemplateSpec(
691691
SecurityContext: &corev1.PodSecurityContext{
692692
FSGroup: helpers.GetPointer[int64](1001),
693693
RunAsNonRoot: helpers.GetPointer(true),
694+
Sysctls: []corev1.Sysctl{
695+
{
696+
Name: "net.ipv4.ip_unprivileged_port_start",
697+
Value: "0",
698+
},
699+
},
694700
},
695701
Volumes: []corev1.Volume{
696702
{

0 commit comments

Comments
 (0)