News for hackers. Stuff that matters.
Re:Dockerfile Best Practices by KronisLV on Sunday January 03, 2021 @04:35AM (#25620729)
I find it somewhat curious, that the article starts with listing examples where root user is used to run containers (amongst other issues):

> Writing production-worthy Dockerfiles is, unfortunately, not as simple as you would imagine. Most Docker images in the wild fail here, and even professionals often[1] get[2] this[2] wrong[3].

And yet, in some of the linked URLs, people are presenting reasons for why that approach was used in particular, instead of the supposedly safer alternatives.

For example, https://github.com/caddyserver/caddy-docker/issues/104

> We actually originally did run as non-root by default, but simplicity we decided to drop that (see #24, and also #103 for some other related discussion).

> If your Dockerfile works for you, that's great. In most cases where users want to run as non-root, they also don't need to listen to :80/:443 in the container, so the setpcap magic isn't necessary at all.

> It's also worth noting that caddy is an official image, and as such needs to be similarly-shaped to other official images of the same type. At a quick glance, none of nginx, traefik, httpd, or haproxy support running as non-root out of the box either.

> Finally, it's worth considering why you want to run as non-root. What attack vectors are you trying to avoid? Container escape vulnerabilities are pretty much the only real risk, but anyone running a modern Docker version is immune to many of them. It's also worth considering user namespace remapping as a mitigation. In my experience the main reason for running as non-root is to pass compliance checks - not a bad reason, but it's also worth recognizing that non-compliance does not automatically equal decreased security (and vice-versa).

If larger projects, like Nginx, Traefik, Httpd and HAProxy were all creating containers like that, it makes you think about the reasoning behind it. Is it easier to just run containers as root and not worry about the permissions inside of the container? If so, wouldn't it really make more sense for the container runtime to have some sort of mechanisms in place to allow people to do what's easy within the containers while also making sure that it has no harmful impact outside of them?

Because to me it seems like people will continuously take the path of least resistance and from where i stand, it should be up to the creators of the container technologies to make sure that this path is safe by default.

Re:Dockerfile Best Practices by plasticxme on Sunday January 03, 2021 @11:02AM (#25622491)
Nginx and the like are starting to provide non-privileged versions of their container images.

Running as root is lazy and equals container escape, especially when running on anything other than scratch and read only file system.

The only reason Nginx and Traefik run as root is to bind to privileged ports (80,443). There is no reason to do that inside of a container, since you can remap exposed ports outside of the container.

Containers are not VMs and must be handled differently. You are always one RCE away from having your entire container platform compromised.

Re:Dockerfile Best Practices by ofrzeta on Sunday January 03, 2021 @01:00PM (#25623499)
You don't need root to open ports 80 and 443 but instead use CAP_NET_BIND_SERVICE that you can also grant to the container.
Re:Dockerfile Best Practices by plasticxme on Sunday January 03, 2021 @07:34PM (#25626716)
CAP_NET_BIND_SERVICE is a root privilege, a distinct one provided by kernel capabilities, granted to a process. In order to use it the container must be permitted to allow its processes to elevate their privileges.

If the container is running as root permitting it is redundant, since the kernel doesn’t filter root for kernel capabilities anyways.

If a privileged user sets CAP_NET_BIND_SERVICE on an executable binary using setpcap to allow a non-root user the ability in a container to bind to a privileged port, elevated privileges are still required for execve to create a process that is permitted to use the kernel capability. Think sudo but for processes.

The argument with containers is that binding to a privileged port isn’t necessary, so you shouldn’t do it. And by not doing it you improve your security posture.

Re:Dockerfile Best Practices by cpuguy83 on Sunday January 03, 2021 @01:56PM (#25624034)
Don't even need that on newer kernels and Docker 20.10: https://github.com/moby/moby/pull/41030
Re:Dockerfile Best Practices by zeroxfe on Sunday January 03, 2021 @07:09AM (#25621315)
> Is it easier to just run containers as root and not worry about the permissions inside of the container? If so, wouldn't it really make more sense for the container runtime to have some sort of mechanisms in place to allow people to do what's easy within the containers while also making sure that it has no harmful impact outside of them?

This is correct in principle, but very hard in practice. This is because kernel support for containers were kind of "tacked on" and more-or-less scattered across the code-base. And although they're getting a lot better, there's still no easy way to reason about their security. So a lot of the advice around permissions management and access control are a kind of defence-in-depth.

I'd love it if we get to a point where containers can make strong statements around security.

Re:Dockerfile Best Practices by turminal on Sunday January 03, 2021 @08:38AM (#25621683)
Before containers everyone used VMs. VMs had a much bigger overhead and were harder to manage. But they were also secure. Now everyone other docker tutorial explains containers as lightweight linux VMs, while in terms of security they are far from it.

I don't like the idea of running my (internal, not meant to face the internet) containers so that they are publicly accessible or running their processes as root. Yet this seems to be the default with docker and I'm sure a lot of people don't bother fixing that.

There is so much confusion regarding users and user namespaces. I think something needs to change in the way docker documents those things and also in the way defaults are chosen for various configuration options.

Re:Dockerfile Best Practices by imglorp on Sunday January 03, 2021 @08:57AM (#25621781)
I think it's better to say VMs are _more_ secure. You can never leave out the _more_.

Example: https://www.vmware.com/security/advisories.html

Re:Dockerfile Best Practices by cpuguy83 on Sunday January 03, 2021 @01:58PM (#25624051)
VM's offer different security. That is all.

It is more secure to run a process with seccomp filters than it is to run it without.

It is more secure to run a process with seccomp and vm isolation than just one or none of these.

Re:Dockerfile Best Practices by MaKey on Sunday January 03, 2021 @12:32PM (#25623247)
You can use VMs to isolate your containers, Amazon developed Firecracker [0] for that.

[0] https://firecracker-microvm.github.io/

Re:Dockerfile Best Practices by ArloL on Sunday January 03, 2021 @08:57AM (#25621777)
Were they really that significantly more secure? You still needed to do regular maintenance on the underlying image, etc. Same with Docker. The only big difference I see is that yes, breaking out of a container is easier than out of a VM. But are there any other significant vectors I should be aware of?
Re:Dockerfile Best Practices by plasticxme on Sunday January 03, 2021 @10:32AM (#25622296)
They were not more secure, just more isolated. The challenges are different.

Containers are just namespaced processes that share the same kernel as the host. A host has access to all container processes, uids, gids, file systems, and networks. Cgroups are used to limit resource access.

To run containers securely you need to understand how to protect running processes. You need to use unprivileged users where possible, drop all kernel capabilities not required, run Linux Security Modules (AppArmor, SELinux) to prevent processes from doing things they shouldn’t; and, run containers based on the smallest image possible, since a container should only have files that are absolutely required to run a process, and nothing more.

Even when you do it all right, in a multi tenant environment, it’s not safe to run all containers on the same hosts.

Re:Dockerfile Best Practices by ArloL on Sunday January 03, 2021 @02:07PM (#25624138)
So if I am understanding this correctly the challenges of setting up a secure linux VM and a container are more or less the same?

The point about multi-tenancy is absolutely understandable. Isn't this an old story from the PHP world with multi-tenancy? I think a good generalization is: don't run on multi-tenant systems if you do anything (!) critical (e.g. authentication or payments)?

But that of course disregards the fact that when people _can_ do something, they _will_ do it even though they shouldn't (like running E-Commerce systems in multi-tenant environments).

Another thought regarding isolation: aren't VMs essentially just running on one host as well? Is that why you said "VMs are _more_ isolated"?

Re:Dockerfile Best Practices by turminal on Sunday January 03, 2021 @10:48AM (#25622406)
That's a very important difference, because isolation and the associated increase in overall security of the system is a core purpose of any virtualization technology. Docker promises a lot here, but a lot of those promises remain unfulfilled in reality. Yes, containers are inherently easier to break out of than VMs, but even with that caveat there is room for improvement in container security. That alone is enough reasons for me not to be a big fan of docker in production.

But there are other vectors. With a VM you get a whole linux distribution, which of course increases the attack surface, but at the same time you also get much better isolation and that distribution's team of maintainers looking over your software, providing security patches, advisories, a simple way to update the system and so on. On the other hand there exist 'docker best practices' tutorials (not the posted one) that recommend not updating your base system at all in the name of reproducibility. Docker's solution to update management is manual image tagging and manual updates, possibly with help of external tooling. I don't think that's a good solution for that problem.

Imo the overall best solution is to run stuff in VMs and pick a lightweight distro for that.

Re:Dockerfile Best Practices by ArloL on Monday January 04, 2021 @07:17AM (#25630371)
Just to be sure, isn't a container a whole linux distribution as well depending on your base image? With the same distribution team, etc.?

That not updating part is of course just plain and simply bad advice.

What solutions for update management would you recommend in the VM space?

Re:Dockerfile Best Practices by rualca on Sunday January 03, 2021 @05:23AM (#25620906)
> It's also worth noting that caddy is an official image, and as such needs to be similarly-shaped to other official images of the same type. At a quick glance, none of nginx, traefik, httpd, or haproxy support running as non-root out of the box either.

That does not sound like an adequate excuse, does it?

I'm afraid none of your remarks makes a reasonable point. Even if you believe that running random stuff as root is ok, if you do not have any reason to do that then why should we mindlessly follow bad practices? If we aim at running a safe system.and there is absolutely zero drawback in following best practices, then why should we continue to make the mistake of intentionally using poor practices?

Re:Dockerfile Best Practices by KronisLV on Sunday January 03, 2021 @06:01AM (#25621056)
But that's the thing - if so many projects are using these bad practices, then doesn't that mean that there's something fishy going on i.e. it's difficult to do things the "proper" way and therefore most people simply don't? Why must we follow these best practices at all - why aren't they simply the default way of doing things, e.g. having to specify a user for the process in the container to run, instead of defaulting to root?

For example, look at SSL/TLS certificates - before Let's Encrypt ( https://letsencrypt.org/ ) and tools like either Certbot ( https://certbot.eff.org/ ) or even web servers like Caddy ( https://caddyserver.com/ ) which automate both provisioning and renewing certificates, people used to simply run HTTP. But now, it's easier than ever to use them for transport level security, and the stats seem to vaguely back this up, for example: https://www.welivesecurity.com/2018/09/03/majority-worlds-to...

Why should users inside of containers be any different? What are the factors that prevent safe defaults from being implemented? That's what i don't understand.

Disclaimer: i'm not advocating for running things as root, but rather my claim is that if things are hard to do, they simply won't be done unless absolutely necessary. Any tech vendor should acknowledge this and make sure that doing things the "right way" is as easy as possible.

Re:Dockerfile Best Practices by kortex on Sunday January 03, 2021 @12:36PM (#25623284)
I think the fishiness is exactly what your intuition is pointing to: root is easy, "proper" is not. There are very few applications where you need true root at runtime, and most can get by with correctly configured user space.

The issue is complexity and lack of appropriate abstractions.

The "Let's Encrypt" of dockerfile safety would be something that makes it trivial to 1) create a user 2) chmod/chown an appropriate spot on the fs, 3) ideally let the author defer these actions to always finalize the image in user mode. That way, you declare at the top that you will do these user actions, but RUN stays as root. Or just provide an SRUN, SCOPY,SADD directive which acts like running with sudo. Then, you can easily extend a layer or base image without being concerned with the details of how user space is implemented.

Also there is no standard or idiomatic protocol for setting up user space in a dockerfile.

Re:Dockerfile Best Practices by saurik on Sunday January 03, 2021 @05:53AM (#25621011)
To me the argument was pretty simple: if running software as non-root is important--and you clearly think it is--but it is even slightly annoying to write software in a container that doesn't run as root (as maybe it now has to have a special user and internally manage these users; the comment you responded to showed this with de facto evidence) then it seems pretty obvious this shouldn't be an abstraction that people who make containers have to deal with... let them write everything to run as root and then add a damned command line flag to the container runtime to run the container not as root. I don't use containers (I am too deep into security to pretend they are a security boundary and I know too much about toolchains to feel they solve actual deployment problems), so I am frankly shocked this isn't already how they work :(.
Re:Dockerfile Best Practices by cpuguy83 on Sunday January 03, 2021 @01:50PM (#25623981)
This is exactly how the work.

You either specify the user when creating the container (which can have certain implications, like being able to bind to < port 1024... solveable but still something to deal with) or you can for everything to run with a uid mapping such that uids in the container are mapped to higher uids on the host.

Re:Dockerfile Best Practices by LucidLynx on Sunday January 03, 2021 @05:28AM (#25620926)
It is definitely possible to run nginx as non-root in a container: https://www.rockyourcode.com/run-docker-nginx-as-non-root-us...

Also, all the sentences that begin with '>' were comments from https://github.com/caddyserver/caddy-docker/issues/104, and not from the user gbrindisi

Re:Dockerfile Best Practices by francislavoie on Sunday January 03, 2021 @11:51AM (#25622889)
It's also possible to run Caddy in Docker as non-root, but it requires jumping through some hoops when configuring Caddy, for example changing the HTTP and HTTPS ports in global configuration to something else, since those ports are necessary to use for ACME.

If the official Docker image did that, then most users would be very confused and we would get lots of support complaints. A cost-benefit analysis told us it was not worth the headache to run as non-root since the Caddy project values highly user experience.

Re:Dockerfile Best Practices by cpuguy83 on Sunday January 03, 2021 @01:53PM (#25624012)
I believe we setup containers in Docker 20.10 such that containers can bind to < 1024 by default without giving it cap_net_bind: https://github.com/moby/moby/pull/41030
Re:Dockerfile Best Practices by kitd on Sunday January 03, 2021 @07:08AM (#25621312)
Even if you believe that running random stuff as root is ok, if you do not have any reason to do that then why should we mindlessly follow bad practices?

But the argument is, should it be a bad practice?

Re:Dockerfile Best Practices by rualca on Sunday January 03, 2021 @11:53AM (#25622906)
> But the argument is, should it be a bad practice?

Are you really asking if running external-facing services as root should be considered a bad practice?

Re:Dockerfile Best Practices by kitd on Monday January 04, 2021 @02:34AM (#25628638)
It's a container. The point of Docker is to contain and encapsulate. Lock down Docker.

Maybe not a bad practice, but it should be redundant.