While building untrusted Dockerfiles isn't too safe, it's unexpected that environment variables can be easily leaked without an exploit of some kind.
It's a common task to build containers in CI pipelines, but it's extremely unsafe to pass a Docker socket into the CI environment. Instead, tools like kaniko don't actually create nested containers, but support building Dockerfiles by building everything in userspace. Other tools such as buildkit and buildah support a rootless mode that creates user namespaces but has less isolation than a traditional container.
In CI environments, sensitive data is often stored in environment variables (at a minimum, often Docker push credentials). Unexpectedly, some rootless/unprivileged build tools allow access to environment variables of their host environment. Via /proc/$PPID/environ
or ps auxe
, some tools can expose environment variables. Additionally, most unprivileged tools allow access to command line arguments of all parent processes inside the container, but it is less likely for sensitive data to be present here.
Most of the above is due to how containers and processes work in Linux. Any process can access the command line arguments of other processes. If the process has the same owner, /proc/*/environ
allows accessing environment variables. Most privileged Docker runners will use a separate PID namespace to fully isolate processes. Unprivileged tools cannot do this, and instead often create a user namespace but no PID namespace. If the rootless tool is careful to not pass any sensitive data into the user namespace this can be a reasonably secure design.
- kaniko – ❌ No isolation, all environment variables can be accessed
- Mostly unavoidable by it's design of not using nested containers. The security section of their README clearly says "kaniko by itself does not make it safe to run untrusted builds inside your cluster, or anywhere else." By design kaniko does not provide any namespace, process, or mount isolation.
- buildkit (rootless) – ✅ Isolated namespace for RUN commands prevents accessing environment variables
- img – ✅ Isolated namespace for RUN commands prevents accessing environment variables
- Buildah 1.21.0 and Podman 3.2.1 with
BUILDAH_ISOLATION=chroot
– Being fixed. ❌ CVE-2021-3602 environment variables were accidentally passed inside isolated namespace allowing access to environment variables.- ✅ Fixed in buildah v1.21.3 or later, see GHSA-7638-r9r3-rmjj
- orca-build – I couldn't get it it's rootless mode to work inside a container
git clone https://gist.github.com/bburky/30a66ab6fd0cfc79dcd8e7810b06ff26 rootless-build
cd rootless-build
bash hack.sh
See comments inline in the Dockerfile and shell script below.