Skip to content

vm: keep the special mode bits, and bound positional offsets - #975

Merged
tannevaled merged 8 commits into
mainfrom
positional-io-hardening
Nov 22, 2026
Merged

tannevaled merged 8 commits into
mainfrom
positional-io-hardening

Conversation

@tannevaled

Copy link
Copy Markdown
Contributor

Closes #972
Closes #973
Refs #970: this branch is stacked on it, so its diff includes #970's commits until that one merges. Merge #970 first.
Refs #974: one of its items, pwrite swallowing ENOSPC/EIO, is fixed here.

Refs #968, Refs #971: with #970, these three changes were the subject of the security and functionality audit that found both defects.

#972: setuid, setgid and sticky were lost

Ruby's 0o4000/0o2000/0o1000 are not Go's fs.ModeSetuid/Setgid/Sticky. File.chmod, File#chmod, FileUtils.chmod and chmod_R all passed the integer to os.Chmod, which kept only the nine permission bits. So File.chmod(0o1444, q) set 0o444.

  • chmodMode maps the three special bits (new file internal/vm/file_mode.go).

ioFlush and #pwrite share a retry that puts the owner-write bit back for the length of a write (withOwnerWrite). It saved Mode().Perm() and restored that, which lost the same bits.

  • It now restores only the owner-write bit over the mode the write left behind.
  • So the kernel clearing setuid/setgid on a write stands, as in MRI.
  • Restoring the mode read before the write would have put a setuid bit back on content that had just changed.

Measured with rubydiff against MRI 4.0.7 (arm64-darwin25):

mode MRI after pwrite / close rbgo before rbgo after
1444 1444 / 1444 444 / 444 1444 / 1444
4444 444 / 444 444 (chmod already lost it) 444 / 444
6755 755 / 755 755 (same) 755 / 755

Every row diverged before the fix, starting with File.stat right after File.chmod; every row agrees after it.

#973: positional offsets

#pwrite grew the in-memory buffer to off+len BEFORE writing by path. That happened even when the file was reachable, so:

  • f.pwrite("AB", 2**63-1) was a slice-bounds panic, surfaced as ArgumentError;
  • 2**62 went to the allocator (NoMemoryError).

MRI raises Errno::EFBIG: File too large @ rb_io_pwrite - <path>.

What changed:

  • The file is written first. Its refusal is raised with its own errno: EFBIG, and ENOSPC/EIO, which used to be swallowed into the buffer.
  • The buffer mirror opens a hole of at most 64 MiB (ioHoleMax). A reachable file still gets the hole on disk, as in MRI. When the path is gone, the buffer is the only copy, so a larger hole is Errno::EFBIG. This divergence is documented in the README row.
  • The #pread buffer fallback no longer computes off+n.
  • Offsets are coerced as NUM2OFFT: a Bignum is RangeError (it was TypeError), and offsets stay 64-bit on a 32-bit target, where they used to be clamped to 2**31-1.

rubydiff probe of 9 rows (huge offsets, Bignum offsets): every row diverged before, 0 after.

Tests

  • TestChmodKeepsTheSpecialBits covers every chmod path, plus pwrite and close through a read-only mode. The rows are pinned to MRI and skipped as root and on Windows.
  • TestPositionalOffsetsAreBounded has 6 rows: 3 pinned to MRI, 3 for this model's documented limits.
  • TestChmodModeKeepsTheSpecialBits and TestRaisePwriteErrKeepsTheErrno (unix only) are internal tests.
  • mutsweep: 4/4 new refusals caught, and 7/7 moved refusals in io_positional.go caught, all by an assertion and none by a panic.
  • The full internal/vm suite passes locally in 336 s.

Security notes

  • The owner-write retry never widens a mode. Afterwards the owner-write bit is the original one, and the other bits are what the kernel left.
  • A Ruby offset can no longer drive an allocation larger than 64 MiB plus the data.
  • Path-based access still has the time-of-check/time-of-use limit documented in vm: IO#pwrite and IO#pread reach the file, not the stream's buffer #970. Hardening it with os.SameFile would need the stream to keep the identity of the file it opened, which is a separate change.

🤖 Generated with Claude Code

tannevaled and others added 8 commits November 21, 2026 17:56
MRI's rb_io_pwrite and rb_io_pread are pwrite(2) and pread(2) on the
descriptor. rbgo's File streams are buffer-backed and wrote and read only
the buffer, so a pwrite was not on disk until a flush and a pread could
not see another writer.

Both now reach the regular file by its path:

- pwrite writes its own range with WriteAt and nothing else. The stream's
  other pending writes stay pending; MRI does the same (disk
  "01234AB789", not "QQ234AB789"). On success it no longer marks the
  buffer dirty. A permission refusal is retried with the owner-write bit
  restored, as ioFlush does, so a stream created 0444 writes through its
  grant. Before, those bytes never reached the disk.
- pread flushes this stream's pending writes first, as rb_io_pread does,
  then reads with ReadAt. The allocation is bounded by the file's size,
  not by the length Ruby asked for, so f.pread(2**40, 0) returns at once
  (MRI 4.0.7 did not answer within 30 s).

A stream with no reachable regular file (a StringIO, a pipe, a popen
stream, a device, an unlinked path) falls back to the buffer, as before.

Measured with rubydiff against MRI 4.0.7: the probe diverged on 6 of 11
lines before this change and on none after.
TestPositionalIOReachesTheFile pins those rows.

Found while measuring: closing a writable stream that was never written to
rewrites the file from its buffer (#969). That is a separate defect and is
not touched here.

Closes #949

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
…s it

mutsweep on the first commit caught 5 of 12 mutants; 7 survived. This
commit fixes them in two ways.

The code is restructured so that no line is a mutant nobody can tell
apart:
- The permission retry is now `if errors.Is(err, ErrPermission)`, and the
  0444 row holds it. The old `if !errors.Is(...) return` was a fast path
  whose mutant only repeated a successful write.
- preadFile reads with io.ReadAll over an io.SectionReader instead of
  Stat-then-allocate. That removes an `f.Stat` failure on an open file
  that nothing can provoke, and the allocation stays bounded by what the
  file holds.
- ioPositionalPath lets os.Stat("") refuse an empty path, instead of a
  second guard for the same case.

Package-internal tests cover the remaining refusals: a pipe or popen
stream over a regular path, a directory, a missing path, a retry whose
mode cannot be read, a write that fails after a successful open, and the
EOF / error / data answers of preadFile.

Refs #949

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
When os.Stat cannot read the mode, writeAtWithOwnerWrite must return the
original refusal. Its mutant was caught only by a nil-pointer panic. The
test now recovers that panic and fails with an assertion, and mutsweep
reports the line "caught", not "caught by PANIC".

Refs #949

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
CI's per-function coverage ratchet failed on both POSIX lanes:

- defIOSeekable 99.1%: the buffer's own end-of-file check in #pread is
  reached only when the file cannot be read by path, and no test did
  that. Two rows now do, each matching MRI 4.0.7 under rubydiff: a pread
  on a path made unreadable after open (falls back to the buffer, "234"),
  and a pread past the end of an unlinked file (EOFError).
- writeAtWithOwnerWrite 88.9%: the "chmod failed" branch could not be
  provoked by a test, and it was redundant. When the mode cannot be
  changed, the retried write is refused for permission again and that
  refusal is returned. The branch is gone; the chmod's error is ignored
  with that stated.

Refs #949

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
#972: Ruby's 0o4000/0o2000/0o1000 are not Go's fs.ModeSetuid/Setgid/Sticky,
so File.chmod, File#chmod, FileUtils.chmod and chmod_R passed the integer to
os.Chmod and set the nine permission bits alone: File.chmod(0o1444, q) gave
0o444. chmodMode maps them. The owner-write retry that ioFlush and #pwrite
share (withOwnerWrite) saved Mode().Perm() and restored it, losing the same
bits; it now restores only the owner-write bit over the mode the write left,
so the kernel's clearing of setuid/setgid on a write stands, as in MRI 4.0.7
(1444 kept, 4444 -> 444, 6755 -> 755).

#973: #pwrite grew the in-memory buffer to off+len BEFORE writing by path,
so f.pwrite("AB", 2**63-1) was a slice-bounds panic raised as ArgumentError
and 2**62 reached the allocator, even on a reachable file. Now the file is
written first and its refusal raised with its errno (EFBIG, ENOSPC, EIO were
swallowed into the buffer); the buffer mirror opens at most a 64 MiB hole,
and when the path is gone and the hole is larger, #pwrite raises EFBIG. The
#pread fallback no longer forms off+n. Offsets are NUM2OFFT: a Bignum is
RangeError, and they stay 64-bit on a 32-bit target.

Both probes agree with MRI 4.0.7 under rubydiff (they diverged on every
row before).

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@tannevaled
tannevaled merged commit b3d23b9 into main Nov 22, 2026
26 checks passed
@tannevaled
tannevaled deleted the positional-io-hardening branch November 22, 2026 09:15
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

1 participant