Repository navigation
vm: keep the special mode bits, and bound positional offsets - #975
Merged
Merged
Conversation
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>
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
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,
pwriteswallowing 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/0o1000are not Go'sfs.ModeSetuid/Setgid/Sticky.File.chmod,File#chmod,FileUtils.chmodandchmod_Rall passed the integer toos.Chmod, which kept only the nine permission bits. SoFile.chmod(0o1444, q)set0o444.chmodModemaps the three special bits (new fileinternal/vm/file_mode.go).ioFlushand#pwriteshare a retry that puts the owner-write bit back for the length of a write (withOwnerWrite). It savedMode().Perm()and restored that, which lost the same bits.Measured with
rubydiffagainst MRI 4.0.7 (arm64-darwin25):Every row diverged before the fix, starting with
File.statright afterFile.chmod; every row agrees after it.#973: positional offsets
#pwritegrew the in-memory buffer tooff+lenBEFORE writing by path. That happened even when the file was reachable, so:f.pwrite("AB", 2**63-1)was a slice-bounds panic, surfaced asArgumentError;2**62went to the allocator (NoMemoryError).MRI raises
Errno::EFBIG: File too large @ rb_io_pwrite - <path>.What changed:
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 isErrno::EFBIG. This divergence is documented in the README row.#preadbuffer fallback no longer computesoff+n.RangeError(it wasTypeError), and offsets stay 64-bit on a 32-bit target, where they used to be clamped to 2**31-1.rubydiffprobe of 9 rows (huge offsets, Bignum offsets): every row diverged before, 0 after.Tests
TestChmodKeepsTheSpecialBitscovers 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.TestPositionalOffsetsAreBoundedhas 6 rows: 3 pinned to MRI, 3 for this model's documented limits.TestChmodModeKeepsTheSpecialBitsandTestRaisePwriteErrKeepsTheErrno(unix only) are internal tests.io_positional.gocaught, all by an assertion and none by a panic.internal/vmsuite passes locally in 336 s.Security notes
os.SameFilewould need the stream to keep the identity of the file it opened, which is a separate change.🤖 Generated with Claude Code