Security Resources

⌘K
  1. Home
  2. Security Resources
  3. Secure Code Explain
  4. Kubernetes Pod Security Standards Hardening

Kubernetes Pod Security Standards Hardening

Here is an example of a Kubernetes Deployment manifest that lacks security contexts, allowing privilege escalation, host filesystem modification, and root execution:

🥺 Vulnerable Code

# Vulnerable: Kubernetes Deployment without securityContext or resource boundaries
apiVersion: apps/v1
kind: Deployment
metadata:
  name: payment-service
  namespace: production
spec:
  replicas: 3
  selector:
    matchLabels:
      app: payment-service
  template:
    metadata:
      labels:
        app: payment-service
    spec:
      containers:
      - name: payment-api
        image: internal-registry.company.com/payment:v1.2.0
        ports:
        - containerPort: 8080
        # Insecure: Missing securityContext allows root, writable rootfs, and privilege escalation
        # Insecure: Missing resource limits enables host resource exhaustion (DoS)

Without an explicit securityContext, Kubernetes defaults to running containers with the image's default user (frequently root). Furthermore, allowPrivilegeEscalation defaults to true, allowing processes to gain more privileges than their parent process via setuid binaries. The root filesystem remains fully writable, allowing attackers who gain remote code execution to download malware or modify application code. Finally, omitting resource limits allows a compromised or leaking pod to starve other node workloads.

😎 Secure Code

Here is a hardened version compliant with Kubernetes Pod Security Standards (Restricted profile):

# Secure: Hardened Kubernetes Deployment adhering to Restricted Pod Security Standard
apiVersion: apps/v1
kind: Deployment
metadata:
  name: payment-service
  namespace: production
spec:
  replicas: 3
  selector:
    matchLabels:
      app: payment-service
  template:
    metadata:
      labels:
        app: payment-service
    spec:
      # Pod-level security context
      securityContext:
        runAsNonRoot: true
        runAsUser: 10001
        runAsGroup: 10001
        fsGroup: 10001
        seccompProfile:
          type: RuntimeDefault
      containers:
      - name: payment-api
        image: internal-registry.company.com/payment:v1.2.0
        ports:
        - containerPort: 8080
        # Container-level security context
        securityContext:
          allowPrivilegeEscalation: false
          readOnlyRootFilesystem: true
          capabilities:
            drop:
            - ALL
        resources:
          limits:
            cpu: "500m"
            memory: "512Mi"
          requests:
            cpu: "100m"
            memory: "128Mi"
        volumeMounts:
        - name: tmp-volume
          mountPath: /tmp
      volumes:
      - name: tmp-volume
        emptyDir:
          medium: Memory
          sizeLimit: 64Mi

The hardened manifest enforces the Kubernetes Restricted security profile: runAsNonRoot: true and runAsUser: 10001 guarantee execution as an unprivileged user; allowPrivilegeEscalation: false prevents setuid elevation; readOnlyRootFilesystem: true blocks any modifications to disk (ephemeral scratch space is provided via an in-memory /tmp volume); capabilities.drop: [ALL] strips all default Linux kernel capabilities; and seccompProfile: RuntimeDefault restricts dangerous system calls.