Kubernetes workload mounts a sensitive OS directory

Mounting sensitive host directories through hostPath broadens access to node information and files.

Description

Reserve sensitive hostPath mounts, such as the node’s /etc, /proc, /var, /root or root directory, for system agents that need them. Give ordinary apps ConfigMaps for configuration, Secrets for sensitive values and suitable PVCs for persistent data.

Potential impact

Even a read-only mount can expose node logs, configuration or credentials. Write access may also allow changes to host files used by other workloads.

Remediation

Limit exceptions to approved workloads and the smallest path they need. Use read-only access where possible. When replacing storage, migrate the data and permissions and verify the application uses the new path.

Examples

The examples show storage for two different applications.

Before

yaml
apiVersion: v1
kind: Pod
metadata:
  name: web-server-pod
spec:
  volumes:
    - name: nginx-host-config
      hostPath:
        path: /etc/nginx
  containers:
    - name: nginx-container
      image: nginx
      volumeMounts:
        - mountPath: /etc/nginx
          name: nginx-host-config

The nginx Pod mounts the node’s /etc/nginx at the same path in the container. Its access to host configuration depends on file permissions.

After

yaml
apiVersion: v1
kind: Pod
metadata:
  name: redis-empty-dir
spec:
  containers:
    - name: redis
      image: redis
      volumeMounts:
        - name: redis-storage
          mountPath: /data/redis
  volumes:
    - name: redis-storage
      emptyDir: {}

The Redis Pod mounts an emptyDir at /data/redis. Configure Redis to write there. Its data is deleted when the Pod is removed from the node, so use persistent storage for data you need to retain.

References