kubernetes: Compromised container can trigger OOM with `kubectl cp`

A compromised container can trigger the kubectl cp command to use an arbitrarily large amount of memory.

The kubectl cp command is typically executed by admins from computers outside of the Kubernetes cluster. This means that a compromised container inside the Kubernetes cluster can trigger “out of memory” errors on the admin computer outside of the Kubernetes cluster.

Kubernetes Version: 1.28.9

Repro steps: The malicious actor configures a container with a bogus /usr/bin/test file. This can be done either by modifying the container image before execution, or via the following kubectl exec command:

kubectl exec -ti normal-pod-l44rc – sh -c ‘mv /usr/bin/test /usr/bin/test.old ; printf “#!/bin/sh\ncat /dev/zero” > /usr/bin/test ; chmod +x /usr/bin/test’

An oblivious admin attempts to use the kubectl cp command:

touch myfile ; kubectl cp myfile normal-pod-l44rc:/myfile

The kubectl cp command executes the bogus /usr/bin/test script in the container and retrieves its stdout/stderr without any upper limit for the buffer size: https://github.com/kubernetes/kubernetes/blob/master/staging/src/k8s.io/kubectl/pkg/cmd/cp/cp.go#L270 The stdout/stderr buffers are not used by kubectl cp since /usr/bin/test does not actually output anything useful. But it keeps stdout/stderr in memory, allowing the bogus /usr/bin/test script to grow memory usage on the admin’s computer.

Impact The “top” command shows that kubectl quickly takes a lot of CPU and memory:

PID USER PR NI VIRT RES SHR S %CPU %MEM TIME+ COMMAND 2 55060 alban 20 0 9678712 4.1g 33152 S 42.9 26.2 0:33.16 kubectl

If continues to run, it would consume all the memory on the admin’s computer. If the admin’s computer has a reasonably fast internet connection, this grows quite fast because the zeros from /dev/zero can be compressed very easily.

Reported by @alban

About this issue

  • Original URL
  • State: closed
  • Created 2 years ago
  • Comments: 16 (16 by maintainers)

Commits related to this issue

Most upvoted comments

This seems like another example of why we need https://github.com/kubernetes/enhancements/issues/4582

@ritazh any objections to close as is?

No objection. Thank you @ardaguclu and sig cli for the fix.

This issue was resolved in https://github.com/kubernetes/kubernetes/pull/126652

FYI @ @alban

/close

will tar command have the same/similar problem?