Skip to content

Fix 2 — Writability fallback (lines 36-47)#69

Open
nicolasva wants to merge 2 commits into
ruby:masterfrom
nicolasva:fix_second_tmpdir
Open

Fix 2 — Writability fallback (lines 36-47)#69
nicolasva wants to merge 2 commits into
ruby:masterfrom
nicolasva:fix_second_tmpdir

Conversation

@nicolasva

@nicolasva nicolasva commented Mar 17, 2026

Copy link
Copy Markdown

reference : rails/rails#56997

Problem

In some containerized environments (Kubernetes with read-only root filesystem + emptyDir volumes), File.writable? (which uses the access(2) syscall) can return
false even though the directory is actually writable via mounted volumes.

Solution

Added a fallback to stat.writable? when File.writable? fails.

Changes

lib/tmpdir.rb:
when !File.writable?(dir)
# ... existing comment ...
#
# However, in some container environments (e.g. Kubernetes with read-only root filesystem
# and emptyDir volumes), File.writable? may return false even though the directory is
# actually writable via mounted volumes. Fall back to stat.writable? in that case.
if stat.writable?
warn "#{name}: File.writable? reports not writable but file mode bits suggest writable, using anyway: #{dir}"
break dir
end
warn "#{name} is not writable: #{dir}"

test/test_tmpdir.rb — 2 new tests:
Test: test_writable_fallback_to_stat
Description: Simulates a container environment where File.writable? returns false but stat.writable? returns true. Verifies the directory is accepted with a
warning.
────────────────────────────────────────
Test: test_writable_fallback_both_fail
Description: Verifies that a truly non-writable directory (chmod 0555) is properly rejected.
Test results

12 tests, 51 assertions, 100% passed

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=1
Or in the Kubernetes deployment manifest:

env:
name: RUBY_TMPDIR_ALLOW_WORLD_WRITABLE
value: "1"

@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:

Kubernetes with read-only root filesystem + emptyDir volumes: The security context may restrict access checks at the syscall level, but the mounted volume is
still writable
SELinux/AppArmor policies: Security modules can affect access(2) results without actually blocking write operations
NFS mounts with root squashing or specific export options: As you mentioned, NFS can exhibit similar behavior
User namespaces in containers: UID mapping can cause access(2) to return incorrect results
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.
The actual open()/write() syscalls can succeed even when access() says no.

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
help. @rhenium

@ioquatix ioquatix force-pushed the fix_second_tmpdir branch from 1aa9004 to d2ec6e2 Compare June 21, 2026 11:01

Copilot AI left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

This PR adjusts Dir.tmpdir selection to better handle containerized environments where File.writable? (access(2)) can report a false negative, by adding a fallback acceptance path based on stat.writable?, and adds tests covering the new behavior.

Changes:

  • Add a fallback in Dir.tmpdir to accept a candidate directory when File.writable? is false but stat.writable? indicates it should be writable, emitting a warning.
  • Add tests for (1) the fallback acceptance-with-warning path and (2) the rejection path when both checks indicate non-writable.

Reviewed changes

Copilot reviewed 2 out of 2 changed files in this pull request and generated 2 comments.

File Description
lib/tmpdir.rb Adds the new “writability fallback” behavior and warning during tmpdir candidate selection.
test/test_tmpdir.rb Adds regression tests to validate the fallback and non-writable rejection behavior.

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

Comment thread lib/tmpdir.rb
Comment on lines +42 to +45
if stat.writable?
warn "#{name}: File.writable? reports not writable but file mode bits suggest writable, using anyway: #{dir}"
break dir
end
Comment thread test/test_tmpdir.rb

def test_writable_fallback_both_fail
omit "no meaning on this platform" if /mswin|mingw/ =~ RUBY_PLATFORM
omit "root can write to any directory" if Process.uid == 0
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants