We want to have a GitLab pipeline that allows all teams (even none-trusted ones) to run a “build” job. There is a few issues with that regarding security that we need confirmation on. Intuitively, the answer should all be yes, but we are not sure whether docker is made to support these untrusted use-cases, so our team is very hesitant. Unfortunately, we could not find a definitive answer to these questions
Scenario
In this scenario, we control the command to build, but not what is build and not the Dockerfile itself. They push to a GitLab repository, and we clone it to a shallow copy of the repository on this commit. Hence, the sources are under control of the adversary, yet they correspond to the definitely correct branch / commit.
After this checkout, we simply force docker build . on the repository sources, and tag it with the commit hash. We then push it to our registry. Hence, the built images and tags are correct and an adversary can for example not push an image to the master tag or the commit hash at HEAD for the master branch.
Note that the Dockerfile itself is under control of the adversary. Also, the docker build is docker-in-docker, so requires a privileged container, as we plan to use Kubernetes executor in the best case.
Security implications of…
… docker build
The first question that we are asking ourselves is, are there security implications to running docker build on arbitrary Dockerfiles? Yes, an adversary can run arbitrary code on our system, but it should all be neatly sandboxed(?), and a simple timeout on the build should prevent building forever.
Does the context (which is set to the cloned repository) restrict filesystem activity correctly? Can the Dockerfile somehow control the system’s filesystem? Is there something with symbolic links etc that we should be aware of? Are there any other attack vectors, or would any vulnerability in this regard just be a CVE as it is behavior that is not intended?
… caching
Given that we want our pipelines to be somewhat fast, we want to use caching for our developer branches. Can the adversary somehow poison our cache even though we are just running docker build .? Do we have to disable caching completely or is this safe to use in a such untrusted environment?
Thank you for looking at this and helping us out:)