Default Container Behavior
Many container images run as root by default, and Kubernetes allows this unless explicitly configured otherwise through securityContext settings.
Kubernetes containers running as root (uid 0) present a significant security risk, as any code execution vulnerability within the container immediately grants root privileges. This enables container breakout attempts, filesystem tampering, lateral movement, and broader cluster compromise.
# VULNERABLE: Deployment without security context - runs as root
apiVersion: apps/v1
kind: Deployment
metadata:
name: vulnerable-app
namespace: production
spec:
replicas: 3
selector:
matchLabels:
app: vulnerable-app
template:
metadata:
labels:
app: vulnerable-app
spec:
# VULNERABLE: No security context specified
containers:
- name: web-server
image: nginx:1.21
ports:
- containerPort: 80
# VULNERABLE: Container will run as root (uid 0)
- name: app-container
image: my-app:latest
ports:
- containerPort: 8080
# VULNERABLE: No security restrictions# SECURE: Deployment with runAsNonRoot security context
apiVersion: apps/v1
kind: Deployment
metadata:
name: secure-app
labels:
app: secure-app
spec:
replicas: 3
selector:
matchLabels:
app: secure-app
template:
metadata:
labels:
app: secure-app
spec:
# SECURE: Pod-level security context
securityContext:
runAsNonRoot: true # SECURE: Prevent root execution
runAsUser: 1000
fsGroup: 2000
containers:
- name: web-server
image: nginx:1.21-alpine
ports:
- containerPort: 8080
# SECURE: Container-level security context
securityContext:
runAsNonRoot: true # SECURE: Prevent root execution
runAsUser: 101
allowPrivilegeEscalation: false
capabilities:
drop:
- ALL
- name: app-container
image: my-app:latest
ports:
- containerPort: 8080
# SECURE: Application container security
securityContext:
runAsNonRoot: true # SECURE: Prevent root execution
runAsUser: 1001
allowPrivilegeEscalation: false
capabilities:
drop:
- ALLThe vulnerable example shows a Kubernetes deployment without securityContext configurations, allowing containers to run as root with full privileges. The secure alternative implements comprehensive security contexts with runAsNonRoot: true, specific user IDs, read-only root filesystems, dropped capabilities, and additional hardening measures like seccomp profiles and resource limits.
Many container images run as root by default, and Kubernetes allows this unless explicitly configured otherwise through securityContext settings.
Sourcery automatically identifies remote code execution (rce) due to root containers in kubernetes workloads and many other security issues in your codebase.