Allow bypassing world-writable sticky bit check in Dir.tmpdir for container environments#67
Allow bypassing world-writable sticky bit check in Dir.tmpdir for container environments#67nicolasva wants to merge 1 commit intoruby:masterfrom
Conversation
…tainer environments #16419
|
This looks like two separate changes and should be split into two PRs.
I don't have an opinion on whether it should be configurable, but the current behavior appears to date back to Ruby 2.0.0 released in 2013: 6dbb585
/cc @KJTsanaktsidis This is basically a revert of 47eeddd, which was merged to
Could you elaborate more on the environment where this occurs? I think I've seen a similar situation in the past with an NFS mount on Linux (not related to |
|
@rhenium this problem is describe here : rails/rails#56997 You're right, I'll split this into a separate PR. To answer your question about when File.writable? (which uses access(2)) can return false even though the directory is actually writable: This can occur in containerized environments with:
The issue is that access(2) checks permissions based on the real UID/GID and may not account for all the kernel mechanisms that ultimately determine writability. I don't have a specific reproducible environment to share right now, but I can try to create a minimal Kubernetes manifest that demonstrates this if that would |
reference/issue : rails/rails#56997
We fixed two issues in [lib/tmpdir.rb]:
Fix 1 — Sticky bit check bypass (lines 48-58 and 128-132)
The existing check rejects any world-writable directory (0777) without the sticky bit. Kubernetes emptyDir volumes are mounted as 0777 without sticky bit by default. We now skip this check when:
Process.uid == 0 — root is not restricted by sticky bit on Linux anyway
ENV["RUBY_TMPDIR_ALLOW_WORLD_WRITABLE"] is set to 1, true, or yes
This applies to both Dir.tmpdir and the parent directory check in Dir.mktmpdir.
Fix 2 — Writability fallback (lines 36-47)
Ruby 3.4 changed stat.writable? to File.writable? (which calls the kernel's access(2) syscall). In some container environments, access(2) can return false even though the directory IS writable (due to security modules, mounted volumes, etc.). We now fall back to stat.writable? — if the mode bits say writable, we accept the directory with a warning.
Test added in [test/test_tmpdir.rb]: test_world_writable_allowed_by_env verifies that a 0777 directory without sticky bit is accepted when the env var is set.
Usage for K8s users
In the Dockerfile:
ENV RUBY_TMPDIR_ALLOW_WORLD_WRITABLE=1Or in the Kubernetes deployment manifest:
env:
name: RUBY_TMPDIR_ALLOW_WORLD_WRITABLEvalue: "1"