brewedbits.dev

Personal blog with guides and news on selfhosting, Linux and AI, from the Netherlands

nextDash nextdash --aboutYour bookmarks. Your terminal. checks your links knows your Docker host one container, no cloudLive demo →nextdash.cc ↗

Tip: --security-opt no-new-privileges stops a container process from gaining privileges

Security
~/tips/tip-docker-run-no-new-privileges

docker run --rm -it --security-opt no-new-privileges ubuntu bash

Blocks processes inside a container from gaining extra privileges through su, sudo or setuid binaries.

It is a single flag with no cost that I would put on nearly every container that does not need sudo inside. A process in the container can no longer climb to more privileges than it started with.

The docker run reference describes it like this: the option stops container processes from gaining new privileges, so commands that raise privileges such as su or sudo no longer work inside the container.

docker run --rm -it --security-opt no-new-privileges ubuntu bash

In Compose you write it under the service:

services:
  app:
    image: example/app:1.0
    security_opt:
      - no-new-privileges

The Compose reference says that for boolean options like this the value can be left out and the option counts as enabled, so no-new-privileges, no-new-privileges=true and no-new-privileges:true all mean the same.

Some images start as root on purpose and drop to a normal user through a setuid helper or an entrypoint that calls su-exec or gosu. After adding the flag, check that the container still starts and that its logs show no permission errors. If an image breaks, leave the flag off for that one.

It combines well with --cap-drop ALL. Add them one at a time, so you know which one caused a failure.

Further reading: docker run reference, Compose services: security_opt

More Security tips tcpdump -nn -r capture.pcap port 53 mkcert -install Strict-Transport-Security: max-age=0 Referrer-Policy: strict-origin-when-cross-origin wp super-admin list All Security tips (79) All topics

Link