distroless icon indicating copy to clipboard operation
distroless copied to clipboard

Consider having the default distroless tags use non-root user by default

Open dprotaso opened this issue 7 years ago • 13 comments

Continuing the discussion from https://github.com/GoogleContainerTools/distroless/pull/368

dprotaso avatar Jun 19 '19 20:06 dprotaso

How about add java:8-nonroot and java:11-nonroot???

Renz2018 avatar Jun 24 '19 02:06 Renz2018

The switch to debian10 would actually be an opportunity to introduce this change, since it is going to break compatibility anyway?

evanj avatar Sep 16 '19 14:09 evanj

Please re-consider or at least document if you want to make nonroot the default.

To have a binary listen to a privileged port, you have to RUN setcap "cap_net_bind_service=+ep" $YOUR_BINARY. Due to https://github.com/moby/moby/issues/35699, COPY a file with capability from an image during multi-stage build won't work. The only workaround is to bundle setcap binary in your final image and run it in the build step.

See also rant: https://mobile.twitter.com/miekg/status/1095035943294259202

yegle avatar Nov 08 '19 08:11 yegle

@yegle it is an interesting Docker bug, but you can always put USER 0:0 if you really have to. Also most runtimes allow overriding the default user set in the container. What is the real issue in your use case?

chanseokoh avatar Nov 08 '19 13:11 chanseokoh

You'll have to bundle additional binary (setcap and it's shared objects) if:

  • you use :nonroot
  • your binary needs additional capability (e.g. listening to port <1024)

Which kind of defeated the purpose of using distroless (additional binary not used at runtime = added security risk etc.)

See https://github.com/yegle/stubby-docker/commit/e0baae16d3a8cee50c49a98b18f2b2cae772e932 for more context. The binary in the commit is a DNS server and have to listen to port 53. This commit doesn't actually work.

yegle avatar Nov 08 '19 15:11 yegle

Sorry, I still don't get it.

FROM gcr.io/distroless/base # default user is root

will basically be equivalent to

FROM gcr.io/distroless/base:nonroot
USER 0:0

How not using :nonroot alone solves the problem that you have to copy setcap? As you said, due to the bug, even if you use an image where the default user is root, the capabilities are not carried over. For example,

FROM debian:buster AS build_env
RUN setcap 'cap_net_bind_service=+ep' /bin/ls && getcap /bin/ls

FROM debian:buster
COPY --from=build_env /bin/ls /my-ls
$ docker build -t test .
Step 2/4 : RUN setcap 'cap_net_bind_service=+ep' /bin/ls && getcap /bin/ls
...
/bin/ls = cap_net_bind_service+ep

$ docker run --rm test getcap /my-ls
<no output>

I'm confused. Maybe I'm missing something? And the only difference between :nonroot and :latest is the default user set in the container image, which you can set whatever you want in Dockerfile, so I don't see any connection from :nonroot to the capability issue.

chanseokoh avatar Nov 08 '19 16:11 chanseokoh

I was saying for people who do want to utilize :nonroot image and run the process as non-root user, they have to bundle a setcap binary with their image.

In my example of running a DNS server in docker and provide the DNS service to other containers in the same Docker network, I have to use port 53. My option would be:

  1. Use base-debian10:latest, but it's not there (See https://github.com/GoogleContainerTools/distroless/issues/436)
  2. Use base-debian10:nonroot with USER 0:0, then again what's the point of using nonroot if you have to run the process as root anyway?
  3. Use base-debian10:debug, which is essentially option 1 but with additional shell
  4. Use base-debian10:nonroot, bundle a setcap binary, and set the capability when building the final image, so that you don't need to run the process as root.

Does this makes sense now...?

yegle avatar Nov 08 '19 17:11 yegle

Use base-debian10:nonroot with USER 0:0, then again what's the point of using nonroot if you have to run the process as root anyway?

The discussion here is about making :latest point to :nonroot (and probably :nonroot will be gone afterwards). This non-root image (already) subsumes the conceptual "root image"; they are just identical except some metadata about the image indicating runtimes should run the image with some particular user if not overridden. There's nothing you will lose when Distroless makes this switch. You'll have to use USER 0:0 for :latest (not for :nonroot) anyways after that.

And switching (or not switching) the default does not make any difference to your situation at all. You either have to (1) run your image as root; or (2) bundle setcap to do the shenanigans, regardless of what image you use. And I don't see this has any connection to the discussion around making this switch. Not making a switch doesn't allow you to resolve your issue as of now, and making a switch doesn't prevent you from doing what you have to do either.

After all, considering everything, I genuinely think this can't be an argument against making :latest point to :nonroot. For people who really need to run the image as root, they can do it easily with USER 0:0. And this is pretty much intended.

chanseokoh avatar Nov 08 '19 18:11 chanseokoh

Wait...What...

No I don't want to run my binary as root. I just want to use :nonroot, but want to be able to setcap. Yes this would be the same regardless which image I use, but the distroless image doesn't have package manager, does that mean I need to copy setcap and its shared objects from a burster image?

I'm totally fine with :latest being :nonroot. It would be more useful if you also have an image :nonroot-with-setcap, to make some people's life easier.

yegle avatar Nov 09 '19 19:11 yegle

I'm totally fine with :latest being :nonroot.

Cool! Looks like I can take this as a definite answer that you actually do not have any objection on this issue. I initially thought you were opposing, as you had asked for "reconsideration" on this matter. Switching the default won't affect your situation, after all.

It would be more useful if you also have an image :nonroot-with-setcap, to make some people's life easier.

Definitely, this is something conceivable, but to be completely honest, I think this isn't really the right direction for Distroless (or any other similar base images) to take and maintain it, for a number of reasons. But primarily, this setcap issue (https://github.com/moby/moby/issues/35699) should really be fixed at the root. Actually, looking at the bug, they seem to agree that this should be resolved at the Docker level in today's environment, and indeed, they are actively working on this (draft PR here) to resolve the issue.

I need to copy setcap and its shared objects from a burster image?

That is one way to do it. Another option I can think of is rebasing (https://github.com/google/go-containerregistry/blob/master/cmd/crane/rebase.md, https://github.com/google/image-rebase, or https://github.com/codahale/docker-rebase), and this may be an easier approach to some people. Another workaround I found is to import a tarball, and perhaps this may also prove easier. I believe many people have hit this issue, so there may be more workarounds, I don't know.

chanseokoh avatar Nov 09 '19 23:11 chanseokoh

Inquiry: Is it possible to, in an image or Dockerfile using Distroless, besides the ':nonroot' tag, also define a 'USER nonroot'? This is because, when scanning images with Trivy, it does not detect root, and as a consequence, it generates a HIGH vulnerability.

JonathanParra avatar Dec 19 '23 15:12 JonathanParra

I'm not sure I understand the problem? Can you elaborate? The users are in all images. The tag is just setting the default user.

loosebazooka avatar Dec 20 '23 15:12 loosebazooka

Sorry for reopening an old thread, but I've run into what I believe is the same issue as @JonathanParra. Our Trivy scan reports:

Dockerfile (dockerfile)

Tests: 20 (SUCCESSES: 19, FAILURES: 1)
Failures: 1 (HIGH: 1, CRITICAL: 0)

AVD-DS-0002 (HIGH): Specify at least 1 USER command in Dockerfile with non-root user as argument

For this container:

FROM gcr.io/distroless/nodejs24-debian12:nonroot

WORKDIR /app
COPY --from=build /app .

CMD ["app.js"]

It seems Trivy does not detect this as a non-root container. Or am I maybe missing something?

rudfoss-rr avatar Nov 14 '25 08:11 rudfoss-rr