Minimize The Admission Of Containers Sharing The Host
More Info:
Containers sharing the host PID namespace can view and interact with all processes on the node. Their admission should be restricted via Pod Security Admission policies.
Risk Level
High
Address
Security
Compliance Standards
- CIS AKS
Triage and Remediation
- Remediation
Remediation
Manual Steps
-
Identify pods using
hostPIDacross all namespaces
Run on: any machine withkubectlaccesskubectl get pods --all-namespaces -o jsonpath='{range .items[?(@.spec.hostPID==true)]}{.metadata.namespace}{"\t"}{.metadata.name}{"\n"}{end}'Save the list and, for each pod found, inspect who runs it and why:
kubectl get pod POD_NAME -n NAMESPACE -o yaml -
Decide which workloads (if any) truly require
hostPID
For each pod withhostPID: true, review its purpose with the owning team (e.g., node monitoring/diagnostics agents may have a requirement; most app workloads do not).- If
hostPIDis not strictly required, plan to remove it from the pod spec. - If it is required, note the namespace, pod name, and service account for later RBAC tightening:
kubectl get pod POD_NAME -n NAMESPACE -o jsonpath='{.spec.serviceAccountName}{"\n"}'
- If
-
Harden namespaces with Pod Security Admission labels
Decide which namespaces must be strictly protected (nohostPIDallowed) and which, if any, can host exceptional workloads.- To enforce restricted (disallowing
hostPID) on a namespace:kubectl label --overwrite ns NAMESPACE pod-security.kubernetes.io/enforce=restricted - To enable baseline warnings cluster-wide (helps detect issues without blocking):
kubectl label --overwrite ns --all pod-security.kubernetes.io/warn=baseline
Avoid placing
hostPID-dependent workloads intorestrictednamespaces. - To enforce restricted (disallowing
-
Tighten RBAC for namespaces that legitimately use
hostPID
For any namespace that truly needshostPID, ensure only a dedicated service account can create such pods:- Identify who can create pods now:
kubectl auth can-i create pods --as USER_OR_SA -n NAMESPACEkubectl get role,rolebinding,clusterrole,clusterrolebinding -n NAMESPACE
- Based on this output, reduce
create/updatepermissions onpodsso that only the specific service account(s) used by the approvedhostPIDworkloads retain that ability.
- Identify who can create pods now:
-
Refactor or remove non-essential
hostPIDusage
For pods wherehostPIDis not required, update their manifests or Helm charts to removehostPID: truefrom.specand re-deploy. Use your normal deployment mechanism (kubectl apply, GitOps, or Helm) and ensure that application behavior remains correct without host PID access. -
Verify the policy and current state
Run on: any machine withkubectlaccess- Confirm no remaining
hostPIDpods except in approved namespaces:kubectl get pods --all-namespaces -o jsonpath='{range .items[?(@.spec.hostPID==true)]}{.metadata.namespace}{"\t"}{.metadata.name}{"\n"}{end}' - Confirm Pod Security Admission labels are set as intended:
kubectl get ns --show-labels
- Optionally, attempt to create a test pod with
hostPID: truein arestrictednamespace and ensure it is rejected.
- Confirm no remaining
Using kubectl
Using kubectl
1. List pods that request hostPID: true
Run on: any machine with kubectl access.
kubectl get pods -A -o=jsonpath='{range .items[?(@.spec.hostPID==true)]}{.metadata.namespace}{"\t"}{.metadata.name}{"\n"}{end}'
Problem indication:
- Any line of output means that pod is running with
hostPID: trueand must be manually reviewed to confirm if this is strictly necessary.
For more detail on those pods:
kubectl get pods -A \
-o custom-columns='NAMESPACE:.metadata.namespace,NAME:.metadata.name,HOSTPID:.spec.hostPID'
2. Inspect a specific pod’s spec
For each pod identified above:
kubectl get pod POD_NAME -n NAMESPACE -o yaml
Problem indication:
spec.hostPID: truewithout a clear, documented operational need.- Pods using default service accounts or broadly scoped service accounts when
hostPID: trueis set.
3. Check Pod Security Admission labels on namespaces
List PSA labels for all namespaces:
kubectl get ns \
-o custom-columns='NAME:.metadata.name,ENFORCE:.metadata.labels.pod-security\.kubernetes\.io/enforce,WARN:.metadata.labels.pod-security\.kubernetes\.io/warn' \
| sort
Problem indication:
- Namespaces lacking
pod-security.kubernetes.io/enforce=restrictedwherehostPIDshould generally be disallowed. - Sensitive or multi-tenant namespaces with
ENFORCEunset or set toprivileged/baseline. - Namespaces allowing
hostPIDbut not clearly segregated for that purpose.
To inspect labels for a single namespace:
kubectl get ns NAMESPACE --show-labels
4. Review RBAC for namespaces that legitimately need hostPID
First, list service accounts in a namespace that is allowed to use hostPID:
kubectl get sa -n NAMESPACE
Then list rolebindings and clusterrolebindings referencing those service accounts:
kubectl get rolebinding,clusterrolebinding -A -o yaml | grep -B5 -A10 "kind: ServiceAccount"
Problem indication:
- Service accounts used by
hostPIDpods are bound to roles that are overly broad or reused widely across other workloads. - No clear, dedicated service account for
hostPIDworkloads (e.g., usingdefaultservice account).
Automation
#!/usr/bin/env bash
# Report use of hostPID and Pod Security Admission labels across all namespaces.
# Run on: any machine with kubectl access and current context set.
set -euo pipefail
echo "=== Pod Security Admission labels per namespace ==="
kubectl get ns \
-o custom-columns=NAME:.metadata.name,ENFORCE:.metadata.labels.pod-security\\.kubernetes\\.io/enforce,WARN:.metadata.labels.pod-security\\.kubernetes\\.io/warn \
| sed 's/<none>//g'
cat <<'EOF'
Interpretation:
- ENFORCE=restricted: namespace is enforcing restricted Pod Security level (good).
- ENFORCE missing or set to baseline/privileged: hostPID is more likely to be allowed (review).
- WARN=baseline (or other): only warnings are emitted; enforcement still depends on ENFORCE.
Namespaces without ENFORCE=restricted should be reviewed, especially if they are not
intended for privileged workloads.
EOF
echo "=== Pods using hostPID=true (all namespaces) ==="
kubectl get pods --all-namespaces -o json \
| jq -r '
.items[]
| {
ns: .metadata.namespace,
pod: .metadata.name,
hostPID: .spec.hostPID,
sa: .spec.serviceAccountName
}
| select(.hostPID == true)
| "\(.ns)\t\(.pod)\t\(.sa)\t\(.hostPID)"' \
| awk 'BEGIN { print "NAMESPACE\tPOD\tSERVICEACCOUNT\tHOSTPID" }1'
cat <<'EOF'
Interpretation:
- Any line listed here is a pod that has hostPID=true.
- For each such pod, verify:
- The namespace is expected to allow hostPID.
- The namespace has Pod Security Admission labels intentionally set (or intentionally absent).
- The SERVICEACCOUNT is tightly scoped via RBAC and not used broadly.
Potential problems:
- Namespaces that are not critical/privileged but have pods with HOSTPID=true.
- Namespaces without ENFORCE=restricted that do not have a clear operational need
for looser policies.
- Service accounts used by hostPID pods that are shared with other workloads.
EOF
Problem indicators in the script output:
- Any non-system namespace (or any namespace you do not explicitly trust for privileged workloads) that:
- Lacks
ENFORCE=restricted, and/or - Contains pods listed with
HOSTPID=true.
- Lacks
- Service accounts shown in the hostPID list that are also used widely by non-privileged pods.