Skip to content

Reduce init container privileges from privileged mode to capabilities-only #111

Closed

Description

@mrsabath

Problem

PR #106 changed the proxy-init container security context from capabilities-based to full privileged mode:

# Before (PR #106)
securityContext:
  capabilities:
    add:
    - NET_ADMIN
    - NET_RAW
  runAsNonRoot: false
  runAsUser: 0

# After (PR #106)
securityContext:
  privileged: true
  runAsNonRoot: false
  runAsUser: 0

Why this is a problem:

  1. Excessive permissions: privileged: true grants far more access than needed:

    • Access to all host devices (/dev/*)
    • Ability to load kernel modules
    • Bypass AppArmor/SELinux policies
    • Mount host filesystems
    • Modify kernel parameters beyond networking
  2. Security compliance: Many Kubernetes clusters have PodSecurityPolicies, PodSecurityStandards, or admission controllers that block privileged pods

  3. Deployment blockers: Organizations with security-hardened clusters may not be able to adopt AuthProxy with this requirement

Root cause: The change was made to support sysctl -w net.ipv4.conf.all.route_localnet=1, which requires write access to /proc/sys/. This is needed for Istio Ambient Mesh coexistence (see PR #106).

Proposed Solution

Split the init container into two separate containers with minimal privileges:

Option 1: Two Init Containers (Recommended)

initContainers:
  # Step 1: Set sysctl parameter (requires privileged, but is minimal/auditable)
  - name: sysctl-init
    image: busybox:latest
    command:
      - /bin/sh
      - -c
      - |
        echo "Setting route_localnet for Istio Ambient Mesh compatibility"
        sysctl -w net.ipv4.conf.all.route_localnet=1
        echo "Sysctl configuration complete"
    securityContext:
      privileged: true
      runAsUser: 0
      runAsNonRoot: false
    resources:
      limits:
        cpu: 10m
        memory: 16Mi
      requests:
        cpu: 10m
        memory: 16Mi

  # Step 2: Configure iptables (only needs network capabilities)
  - name: proxy-init
    image: localhost/proxy-init:latest
    imagePullPolicy: IfNotPresent
    securityContext:
      runAsUser: 0
      runAsNonRoot: false
      capabilities:
        add:
          - NET_ADMIN
          - NET_RAW
    env:
      - name: PROXY_UID
        value: "1337"
      - name: PROXY_PORT
        value: "15123"
      - name: INBOUND_PROXY_PORT
        value: "15124"
      - name: SSH_PORT
        value: "22"
    resources:
      limits:
        cpu: 100m
        memory: 64Mi
      requests:
        cpu: 10m
        memory: 32Mi

Benefits:

  • ✅ Minimizes privileged container scope (only sysctl operation)
  • ✅ iptables setup uses minimal capabilities
  • ✅ Easy to audit (sysctl container has 1 command)
  • ✅ Can be conditionally included (only needed for Istio Ambient)

Option 2: Pod-Level SecurityContext (If Supported)

Some Kubernetes distributions support setting sysctls via pod-level securityContext:

apiVersion: v1
kind: Pod
spec:
  securityContext:
    sysctls:
      - name: net.ipv4.conf.all.route_localnet
        value: "1"
  initContainers:
    - name: proxy-init
      securityContext:
        capabilities:
          add: [NET_ADMIN, NET_RAW]
        runAsUser: 0

Limitations:

  • ❌ Requires PodSecurityPolicy or cluster admission controller to allow unsafe sysctls
  • ❌ net.ipv4.conf.all.route_localnet is considered an "unsafe" sysctl by default
  • ❌ Cluster admin must whitelist it via --allowed-unsafe-sysctls kubelet flag

Verdict: Option 2 is cleaner but has cluster-level prerequisites. Use Option 1 for broader compatibility.

Option 3: Conditional Sysctl (Advanced)

Make route_localnet conditional based on Istio Ambient detection:

#!/bin/sh
# In init-iptables.sh

# Detect if Istio Ambient is present (check for ztunnel sockets)
if ss -tln | grep -q ':15008\|:15001\|:15006'; then
  echo "Istio Ambient Mesh detected, setting route_localnet"
  if ! sysctl -w net.ipv4.conf.all.route_localnet=1; then
    echo "WARNING: Failed to set route_localnet. Istio Ambient may not work correctly."
    echo "This requires privileged: true in the init container securityContext."
  fi
else
  echo "No Istio Ambient Mesh detected, skipping route_localnet"
fi

Then make privileged optional via annotation:

metadata:
  annotations:
    kagenti.io/istio-ambient: "true"  # Triggers privileged mode

Files to Update

Required Changes

  1. kagenti-webhook/internal/webhook/injector/container_builder.go

    • Modify BuildProxyInitContainer() to create two init containers OR make privileged conditional
    • Add logic to detect kagenti.io/istio-ambient annotation (if using Option 3)
  2. AuthBridge/AuthProxy/init-iptables.sh

    • Move sysctl command to a separate section with error handling
    • Add conditional logic for Istio detection (if using Option 3)
  3. Deployment YAMLs (update all):

    • AuthBridge/AuthProxy/k8s/auth-proxy-deployment.yaml
    • AuthBridge/k8s/authbridge-deployment.yaml
    • AuthBridge/k8s/authbridge-deployment-no-spiffe.yaml

Documentation Updates

  1. AuthBridge/AuthProxy/README.md

    • Add section explaining the two init containers
    • Document when route_localnet is needed (Istio Ambient only)
  2. AuthBridge/AuthProxy/quickstart/README.md

    • Update prerequisites section
    • Add note about PodSecurityPolicies

Testing Plan

Test 1: Non-Ambient Cluster (Capabilities Only)

# Deploy with only capabilities-based init container
kubectl apply -f authbridge-deployment.yaml

# Verify iptables rules are set
kubectl exec -it <pod> -c auth-proxy -- iptables-legacy-save

# Test inbound/outbound traffic
kubectl exec -it <caller-pod> -- curl http://auth-target:8081/protected

Expected: Should work without privileged mode

Test 2: Istio Ambient Cluster (With Sysctl)

# Install Istio with ambient mesh
istioctl install --set profile=ambient

# Label namespace for ambient
kubectl label namespace default istio.io/dataplane-mode=ambient

# Deploy AuthProxy with sysctl-init container
kubectl apply -f authbridge-deployment.yaml

# Verify route_localnet is set
kubectl exec -it <pod> -- sysctl net.ipv4.conf.all.route_localnet

# Verify ztunnel is present
kubectl exec -it <pod> -- ss -tln | grep 15008

# Test traffic through ambient mesh
kubectl exec -it <caller-pod> -- curl http://auth-target:8081/protected

Expected: Both sysctl and iptables work; traffic flows through ztunnel

Test 3: PodSecurityPolicy Enforcement

# Apply restrictive PSP
kubectl apply -f - <<EOF
apiVersion: policy/v1beta1
kind: PodSecurityPolicy
metadata:
  name: restricted
spec:
  privileged: false
  allowPrivilegeEscalation: false
  requiredDropCapabilities:
    - ALL
  allowedCapabilities:
    - NET_ADMIN
    - NET_RAW
EOF

# Try to deploy AuthProxy
kubectl apply -f authbridge-deployment.yaml

Expected (Option 1): Only sysctl-init should fail PSP (if PSP blocks privileged), but iptables init succeeds. Document this limitation.

Expected (current main): Entire deployment fails

Security Analysis

Current State (Privileged)

Attack Surface:
- Full device access
- Kernel module loading
- SELinux bypass
- Host filesystem access

Required For:
- sysctl (1 command)
- iptables (NET_ADMIN)

Proposed State (Option 1)

sysctl-init (privileged):
  Duration: ~100ms
  Commands: 1 (sysctl)
  Attack Surface: Full, but minimal time window

proxy-init (capabilities):
  Duration: ~500ms
  Commands: ~30 iptables rules
  Attack Surface: Network namespace only
  Required: NET_ADMIN, NET_RAW

Risk Reduction: ~80% of execution time now uses minimal privileges

References

Related Files (from PR #106)

Modified files requiring changes:

AuthBridge/AuthProxy/k8s/auth-proxy-deployment.yaml (line 383-390)
AuthBridge/k8s/authbridge-deployment-no-spiffe.yaml (line 862-867)
AuthBridge/k8s/authbridge-deployment.yaml (line 962-967)
kagenti-webhook/internal/webhook/injector/container_builder.go (line 442-458)

Sysctl command location:

AuthBridge/AuthProxy/init-iptables.sh (line 171)

Success Criteria

  • Init containers split into sysctl-init (privileged) and proxy-init (capabilities-only)
  • All deployment YAMLs updated
  • Webhook injector updated to inject both containers
  • Documentation updated with security notes
  • Tested in non-ambient cluster (should work with only proxy-init)
  • Tested in Istio Ambient cluster (both containers work)
  • Tested with PodSecurityPolicy (fails gracefully with clear error message)

Priority

High Priority — This blocks adoption in security-hardened environments.

Additional Context

This issue was identified during review of PR #106. While the privileged mode change was necessary to support Istio Ambient Mesh, it introduces a significant security expansion that can be mitigated by splitting the responsibilities across two init containers.

The recommended approach (Option 1) provides:

  • Backward compatibility: Works with or without Istio Ambient
  • Security isolation: Minimal privileged execution time
  • Auditability: Clear separation of concerns
  • Flexibility: Can be conditionally included based on deployment needs

Activity

  1. added
    enhancement
    good first issue
    on Feb 12, 2026
  2. huang195 commented on Feb 12, 2026

    @huang195
    Member

    @mrsabath the only concern I have with this approach is we're using alpine:latest image as our base image for the iptables init shell script, and this one is almost like the scratch image:

    docker.io/library/alpine     latest       1ab49c19c53e  2 weeks ago   8.99 MB
    

    It's 9MB big, whereas the busybox image is slightly smaller:

    docker.io/library/busybox    latest       cd9176cd36f9  16 months ago  4.35 MB
    

    I wonder if it's worth the effort given that we're going to be adding yet another init container into the mix, whereas we have another issue to minimize the number of init / sidecar containers overall.

  3. mrsabath commented on Feb 12, 2026

    @mrsabath
    MemberAuthor

    I think the key element of this issue is to reduce the privileges and that should be a focus

  4. huang195 commented on Feb 12, 2026

    @huang195
    Member

    Reducing attack surface and reducing privilege is generally a good idea. However, in this case, our init container has 1 script, which is the init-iptables.sh, and nothing else. There's no service we're exposing. I don't think creating an extra busybox container would provide us a benefit here.

  5. huang195 commented on Feb 12, 2026

    @huang195
    Member

    Here's a take from Claude:

    The issue is valid in principle — privileged: true is a broad grant, and splitting concerns is generally good security hygiene. However, there are practical concerns:
    
      Why the split may not be worth it right now:
    
      1. Minimal actual attack surface — The init container runs a single shell script (init-iptables.sh) and exits. There's no long-running service, no exposed ports, no user-facing interface. The privileged window is
       ~100-500ms during pod startup only.
      2. Adding complexity — A second init container means:
        - Another image to pull (busybox/alpine) adding startup latency
        - More webhook injector logic in container_builder.go
        - More deployment YAML to maintain
        - This conflicts with the goal of minimizing init/sidecar containers overall
      3. The sysctl-init container is still privileged anyway — Option 1 doesn't eliminate privileged: true, it just isolates it. Clusters with strict PSPs/PodSecurityStandards that block privileged containers will
      still block the deployment.
    
      What might be more impactful:
    
      - Option 3 (conditional sysctl) is actually the most interesting idea — only require privileged when Istio Ambient is detected, controlled via annotation. This way, non-ambient deployments don't need privileged
      at all.
      - Alternatively, move the sysctl into the same init-iptables.sh script with a flag/env var to conditionally execute it, keeping a single init container but making privileged: true optional based on the deployment
       scenario.
    
      Bottom line: I agree with your assessment in the comments. The real win would be making privileged conditional (only when Istio Ambient coexistence is needed), not splitting into two containers that both still
      need to exist in the ambient case. The effort is better spent on the container consolidation work.
    

    We could keep the issue as is if we want to do the conditional sysctl, but the benefit is marginal at best.

  6. huang195 commented on Feb 17, 2026

    @huang195
    Member

    As discussed on the call, another approach to this problem is to explore if we can get rid of the requirement giving root access to the init container in the first place. The problem is there is no capability to allow a container to run sysctl command. There might be some way of tweaking the iptables rules so we don't need to run the sysctl command in the first place.

  7. self-assigned this
    on Feb 17, 2026
  8. removed
    good first issue
    on Feb 17, 2026
  9. moved this from Backlog to In progress in Rossoctl Issue Prioritizationon Feb 17, 2026
  10. huang195 commented on Mar 22, 2026

    @huang195
    Member

    the issue has been resolved, no longer need root level privilege.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Labels

enhancement

Type

No type

Projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions