ITISFoundation / osparc-simcore

🐼 osparc-simcore simulation framework
https://osparc.io
MIT License
43 stars 27 forks source link

Review security of services mounting docker socket #3423

Open pcrespov opened 1 year ago

pcrespov commented 1 year ago

We should have a well defined procedure to guarantee the security of any service with access to the docker daemon.

SEE comment:

If you do this, and I think it is necessary, please make sure that this service is properly isolated. It should not be on a network with other plattform services. There should be no way to execute code inside this container or to manipulate the container via REST requests

_Originally posted by @mrnicegyu11 in https://github.com/ITISFoundation/osparc-simcore/pull/3364#discussion_r987823199_

pcrespov commented 5 months ago

Chatty feedback summary:

Granting a container access to the Docker socket is a powerful capability, essentially allowing the container to control the Docker daemon, and by extension, to manage other containers on the same host. This can introduce significant security risks, especially if the container or the application inside it becomes compromised. However, there are scenarios where such access is required for legitimate purposes. Here are several strategies to safely provide Docker socket access to a container, particularly when running the application as a non-root user:

1. Use Docker-in-Docker (DinD)

Docker-in-Docker involves running a Docker daemon inside a Docker container. This is achieved by running the container with a Docker daemon and then binding this inner daemon to a different socket or port. This approach isolates the inner Docker environment from the host Docker daemon but introduces complexity and overhead.

2. Docker Socket Proxy

A Docker socket proxy sits between your application and the Docker socket, filtering requests to the Docker API and allowing only a safe subset of actions. This approach can significantly mitigate security risks by restricting what actions can be performed through the Docker socket.

3. Mount the Docker Socket with Group Control

By mounting the Docker socket into the container (-v /var/run/docker.sock:/var/run/docker.sock) and controlling access through Unix groups, you can limit which users (or containers) can interact with Docker.

  1. Create a Unix group on the host with access to the Docker socket.
  2. Add the non-root user inside the container to this group.
  3. Ensure the Docker socket on the host is accessible to this group.

This method requires careful management of group permissions and user IDs inside and outside the container.

4. Use a Dedicated Service Account

Create a dedicated service account on the host with specific permissions to interact with Docker. Use this account exclusively for containers requiring Docker access. This strategy involves more nuanced control at the host level and may require additional tools or scripts to manage permissions and capabilities.

5. Employ Container Orchestration Security Contexts

When using container orchestration tools like Kubernetes, you can define security contexts for your pods. These contexts allow you to manage permissions and capabilities, including Docker socket access, in a more controlled and declarative manner.

General Security Considerations

Choosing the right strategy depends on your specific use case, security requirements, and operational environment. Combining these strategies with robust security practices can help mitigate the risks associated with Docker socket access.