Review COPY file ownership and write permissions

Grant the runtime account only necessary data-write permissions and protect application code.

Description

COPY --chown sets who owns copied files. Whether the application can change them also depends on file modes, parent directories and runtime mounts.

Potential impact

A process that can rewrite its own code or configuration can alter future behavior. It still needs write access to its data.

Remediation

Have a separate account own the application code or mount it read-only. Test with the runtime account and deployed mounts. It should be able to read or execute the code and write only to designated data paths.

Examples

Both examples run Python as appuser. Check the modes of the files copied from the build context.

Before

dockerfile
FROM python:3.12-slim

RUN useradd -ms /bin/bash appuser
COPY --chown=appuser:appuser app /app
WORKDIR /app
USER appuser
CMD ["python", "app.py"]

After

dockerfile
FROM python:3.12-slim

RUN useradd -ms /bin/bash appuser
COPY app /app
WORKDIR /app
USER appuser
CMD ["python", "app.py"]

The first copy assigns /app to appuser. The second uses the default root ownership. Check that appuser cannot modify or replace the code in either layout.

References