Skip to content

IO#pwrite/#pread buffer fallback: huge offset overflows into a Go panic, large offset allocates the hole in memory #973

Open

Description

@tannevaled

Summary

When the file cannot be reached by path, IO#pwrite and IO#pread fall back to the stream's buffer, and that fallback computes off+len(data) in int.

  • A huge offset overflows the sum, and a Go runtime panic reaches Ruby as an ArgumentError.
  • A large but valid offset materialises the hole in memory, where MRI writes a sparse file.

Affected code:

  • internal/vm/io_descriptors.go ~:365-368 (the pwrite buffer fallback)
  • internal/vm/io_descriptors.go ~:350 (the pread buffer fallback)

These lines are pre-existing. The open pull request Refs #970 made them the fallback path only.

Reproduction (found by audit)

f = File.open("t.bin", "r+")
File.unlink("t.bin")      # forces the buffer fallback
f.pwrite("AB", 2**63-1)   # rbgo: ArgumentError "runtime error: slice bounds out of range"
f.pread(1, 2**63-1)       # rbgo: the same runtime panic message
f.pwrite("AB", 2**27)     # rbgo allocates ~128 MB; 2**40 would exhaust memory (inferred, not run)
  • MRI writes to the descriptor, which is sparse.
  • A Bignum offset raises TypeError in rbgo, where MRI raises RangeError.

Fix

  1. Check off > maxInt - len(data) before slicing and raise as MRI does (Errno::EFBIG or Errno::EINVAL from pwrite(2); measure which).
  2. Cap the fallback buffer growth with a stated limit, or refuse the operation when the path is gone rather than growing memory to the offset.
  3. Add bomb tests that assert a REFUSAL, run under GOMEMLIMIT.

Prior art: priorart "pwrite pread huge offset overflow slice bounds" at 2026-11-21 20:43 CET reported prior art; checked: priorart 'pwrite pread huge offset overflow slice bounds': 206 hits, none on offset overflow; #593 and #347 introduced pread/pwrite, #970 made this the fallback

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions