Image runs on Docker, Permission denied on OpenShift: restricted-v2 and arbitrary UIDs
Our nginx front-end image ran fine on Docker and plain Kubernetes but crash-looped on OpenShift with mkdir Permission denied. The restricted-v2 SCC runs containers with a random UID in the root group. We fixed the image with group 0 ownership, g=u permissions and port 8080 instead of granting anyuid.
We moved our static front end from a managed Kubernetes cluster to a customer's OpenShift Container Platform 4.18 cluster. The same image, the same Deployment manifest minus the Ingress. On OpenShift the pod went straight into CrashLoopBackOff:
oc logs deploy/web
nginx: [warn] the "user" directive makes sense only if the master process runs with super-user privileges, ignored in /etc/nginx/nginx.conf:2
nginx: [emerg] mkdir() "/var/cache/nginx/client_temp" failed (13: Permission denied)
The image was built on nginx:1.27-alpine with our files copied into /usr/share/nginx/html and a server block listening on port 80.
Reproducing it
Every pod start failed identically. Running the image locally with docker run worked, and so did running it on the old cluster.
What we ruled out
A broken image push. The digest on OpenShift matched the one running on the old cluster.
Missing volume mounts. The path in the error is inside the image, not a volume.
SELinux on the node. No AVC denials were reported on the node, and the error came from inside the container's own filesystem.
Root cause
OpenShift admitted the pod under the default restricted-v2 Security Context Constraint. That SCC runs the container with a user ID allocated from the project's range, in our case 1000680000, and makes the process a member of the root group (GID 0). The official nginx image expects to start as root and drop to the nginx user, and its directories under /var/cache/nginx are owned by root with no group write permission. Our arbitrary, non-root UID could not create them. Even past that, binding to port 80 would have failed for a non-root process.
On Docker and on our old cluster the container ran as root, which is why the same image had always worked.
What is happening under the hood
Each OpenShift project gets annotations defining a UID range and supplemental group range:
oc get namespace shop-web -o yaml | grep sa.scc
openshift.io/sa.scc.mcs: s0:c26,c15
openshift.io/sa.scc.supplemental-groups: 1000680000/10000
openshift.io/sa.scc.uid-range: 1000680000/10000
Under restricted-v2, runAsUser must fall within that range; if the pod does not set one, OpenShift assigns the first UID of the range. The SCC also requires allowPrivilegeEscalation: false, drops all capabilities (only NET_BIND_SERVICE may be added back), enforces the RuntimeDefault seccomp profile, and assigns an fsGroup from the supplemental range so mounted volumes are group-writable. The USER line in the Dockerfile does not override any of this.
Inside the container:
oc rsh deploy/web id
uid=1000680000(1000680000) gid=0(root) groups=0(root),1000680000
The process does not know its UID in advance, but it is always in group 0. Red Hat's image guidelines build on that: make every directory the process writes owned by group 0 and give the group the same permissions as the owner. Then any UID the platform chooses can write.
Environment
Component | Version / value |
|---|---|
Platform | OpenShift Container Platform 4.18 |
SCC admitted |
|
Base image |
|
Previous platform | Managed Kubernetes 1.31, no Pod Security admission restrictions |
Exposure | OpenShift Route to Service port 8080 |
Minimal reproduction
oc new-project scc-demo
oc create deployment web --image=docker.io/library/nginx:1.27-alpine
oc logs deploy/web # mkdir() ... failed (13: Permission denied)
oc get pod -l app=web -o jsonpath='{.items[0].metadata.annotations.openshift\.io/scc}'
# restricted-v2
Who is affected
Any image that assumes it runs as root or as one specific UID: images that write to directories owned by a fixed user, that bind ports below 1024, or that chown files at startup. It also bites charts that set securityContext.runAsUser to a fixed value such as 101. Those pods are rejected at admission instead of crashing:
pods "web-6d9f7c5b8-" is forbidden: unable to validate against any security context constraint: [provider "anyuid": Forbidden: not usable by user or serviceaccount, provider restricted-v2: .spec.securityContext.runAsUser: Invalid value: 101: must be in the ranges: [1000680000, 1000689999], ...]
The fix
We changed the image so it works with any UID, rather than changing the cluster's policy to fit the image.
Dockerfile:
FROM nginx:1.27-alpine
COPY nginx.conf /etc/nginx/nginx.conf
COPY dist/ /usr/share/nginx/html/
# Directories nginx writes at runtime: owned by group 0, group gets owner permissions
RUN chgrp -R 0 /var/cache/nginx /usr/share/nginx/html \
&& chmod -R g=u /var/cache/nginx /usr/share/nginx/html
# Numeric non-root user: ignored by OpenShift, honoured by runAsNonRoot elsewhere
USER 1001
EXPOSE 8080
nginx.conf (relevant parts):
# no "user" directive: we never start as root
# pid and temp files go straight to /tmp, which is writable and may be a tmpfs
worker_processes auto;
pid /tmp/nginx.pid;
http {
client_body_temp_path /tmp/client_temp;
proxy_temp_path /tmp/proxy_temp;
fastcgi_temp_path /tmp/fastcgi_temp;
uwsgi_temp_path /tmp/uwsgi_temp;
scgi_temp_path /tmp/scgi_temp;
server {
listen 8080;
root /usr/share/nginx/html;
}
}
The manifest changed only the port, plus an explicit security context that matches what the SCC enforces, so it also passes Pod Security "restricted" on other clusters:
spec:
template:
spec:
securityContext:
runAsNonRoot: true
seccompProfile:
type: RuntimeDefault
containers:
- name: web
image: registry.example.com/shop/web:2.4.1
ports:
- containerPort: 8080
securityContext:
allowPrivilegeEscalation: false
capabilities:
drop: ["ALL"]
No runAsUser: OpenShift picks one from the range, and other clusters use the image's USER 1001.
Alternatives and trade-offs
Grant the
anyuidSCC to the service account. One command makes the original image run as root. It also gives every pod using that service account root inside the container, which the customer's security team would rightly reject. Not a fix.Use an unprivileged base image. The nginx project publishes
nginxinc/nginx-unprivileged, and Red Hat's UBI nginx images already support arbitrary UIDs. Switching bases is a good long-term option; we patched our image first to keep the change small.emptyDirvolumes for writable paths. MountingemptyDirat/var/cache/nginxand/tmpworks because OpenShift sets volume group ownership viafsGroup. It is a useful pattern combined withreadOnlyRootFilesystem: true.
How to test the fix
Locally, simulate OpenShift by running with a random UID in group 0:
docker build -t web:test .
docker run --rm -u 1000680000:0 -p 8080:8080 web:test &
curl -s -o /dev/null -w '%{http_code}\n' http://localhost:8080/ # 200
Then on the cluster:
oc set image deploy/web web=registry.example.com/shop/web:2.4.1
oc rollout status deploy/web
oc exec deploy/web -- id
oc get pod -l app=web -o jsonpath='{.items[0].metadata.annotations.openshift\.io/scc}'
The rollout completed, id showed the arbitrary UID, and the SCC was still restricted-v2.
Confirming in production
The Route served the site, pod restarts stayed at zero over the following days, and oc get events -n shop-web showed no admission or crash-related events. We also confirmed the image behaved identically on the old managed cluster, now running as UID 1001 instead of root.
Regression guard
CI now runs the container with docker run -u 1000680000:0 --read-only --tmpfs /tmp and curls it before pushing. Any change that writes outside /tmp or the group-writable paths, or needs root, fails the build. We also added a policy check that fails manifests containing a fixed runAsUser or privileged: true.
Security implications
The fix reduces the attack surface: the container no longer runs as root anywhere, capabilities are dropped, and privilege escalation is disabled. Group 0 membership is not root privilege; it only grants access to files explicitly owned by the root group, which is why the image should give group write access to its own runtime directories and nothing else. The tempting shortcut, anyuid, would have done the opposite and is the most common way OpenShift's default protections get switched off in practice.
Prevention checklist
Build images that run as any UID in group 0:
chgrp -R 0pluschmod -R g=uon writable paths.Listen on ports above 1024 and drop all capabilities.
Do not hard-code
runAsUserin manifests meant for OpenShift.Test images in CI with
-u <random-uid>:0and a read-only root filesystem.Treat requests for
anyuidas a design review, not a ticket.
References
OpenShift 4.18 Images: guidelines for creating images (support arbitrary user IDs): https://docs.redhat.com/en/documentation/openshift_container_platform/4.18/html/images/creating-images
Managing security context constraints: https://docs.redhat.com/en/documentation/openshift_container_platform/4.18/html/authentication_and_authorization/managing-pod-security-policies
Kubernetes Pod Security Standards: https://kubernetes.io/docs/concepts/security/pod-security-standards/
nginx core module (
pid,user) and temp paths: https://nginx.org/en/docs/ngx_core_module.html
Add a label
Embed this post
<iframe src="https://hibug.me/openshift-pod-permission-denied-arbitrary-uid" width="700" height="420" frameborder="0" loading="lazy"></iframe>
I am an application and cloud security engineer. Most of my day is spent with development teams: reviewing designs, untangling IAM policies, hardening AWS accounts and making security checks fast enough that nobody wants to skip them. I believe security works best when it is concrete. Instead of long lists of best practices, my posts look at one decision at a time: a policy that granted more than intended, a design reviewed before the first line of code, a deletion that did not delete. Each one ends with checks you can run on your own systems.
Comments
0No comments yet. Be the first to comment!
Sign In to post a comment.