fleetlint Policy for a fleet of repositories
v0.10
Getting started

containers/non-root-user

Containers do not run as root

Severitywarning
Tiersall
Stacksany
Scoperoot
Kindexpr
Sourcelibrary v0.7.0
Selected byfleetlint-recommended@v0.7.0

Requirement

The final stage of every Dockerfile or Containerfile (including *.Dockerfile) ends with a non-root USER, or builds on a base image that is non-root by default (:nonroot tags, Chainguard images other than -dev) without switching back to root. Files under .devcontainer/ are exempt.

Why

A process that is root in the container is one kernel or runtime bug away from root on the host, and can rewrite everything in its own image.

Applies when

tracked().exists(p, !p.startsWith(".devcontainer/")
  && p.matches("(^|/)((Dockerfile|Containerfile)[^/]*|[^/]+\\.(Dockerfile|Containerfile|dockerfile|containerfile))$"))

Check

For each item in:

tracked().filter(p, !p.startsWith(".devcontainer/")
  && p.matches("(^|/)((Dockerfile|Containerfile)[^/]*|[^/]+\\.(Dockerfile|Containerfile|dockerfile|containerfile))$")
  && !p.endsWith(".dockerignore") && !p.endsWith(".containerignore"))
[lines(item).filter(l, l.matches("(?i)^\\s*(FROM|USER)\\s"))].exists(ins, ins.size() > 0
  && [ins[ins.size() - 1]].exists(last,
    (last.matches("(?i)^\\s*USER\\s+\\S+") && !last.matches("(?i)^\\s*USER\\s+(root|0)(:\\S+)?\\s*$"))
    || (last.matches("(?i)^\\s*FROM\\s+(--\\S+\\s+)*\\S*(:nonroot|-nonroot|cgr\\.dev/)")
        && !last.matches("(?i)^\\s*FROM\\s+(--\\S+\\s+)*\\S*-dev(\\s|@|$)"))))

Fix

Create a user in the final stage and add USER <name> before the entrypoint.

Agent instruction: In the final stage add a dedicated user and a USER instruction before ENTRYPOINT/CMD; make sure files the process writes are owned by that user.

Sources


Generated by fleetlint docs from catalog v0.7.0.