fix(unionreader): honor the io.ReaderAt contract in readerAtAdapter (#5186)

`readerAtAdapter.ReadAt` seeks and then issues a single `Read`, which breaks the
`io.ReaderAt` contract in both directions against the squashfs reader it exists
to wrap:

- `squashfs.File.Read` copies against the decompressed block length but advances
  its block cursor by the nominal block size, so a block that decompresses short
  silently stops copying and returns fewer bytes with a nil error. `ReadAt`
  forbids that, and callers rely on it: anything decoding a fixed-size structure
  off the result gets zero padding it has no way to detect and parses it as real
  data. The GraalVM PE export table and the UPX block reader both size a buffer
  from a header field and then ignore `n` entirely, so a crafted image drives
  them straight through the padding.

- a read landing exactly on the end of the file returns a *full* buffer paired
  with `io.EOF`. `bytes.Reader.ReadAt` returns nil there, and the callers that
  treat any error as fatal were written against that, so squashfs-resident
  binaries sized near a read boundary were being skipped outright.

`io.ReadFull` normalizes both: it fills the buffer across short reads, and it
clears the error once the buffer is full. A genuinely short tail is reported as
`io.EOF`, which is what `ReadAt` implementations return at the end of a file, and
what the buffering branch of `GetUnionReader` already returns.

Affects squashfs-backed sources (snaps), so in practice the binary catalogers
reading structure out of executables.

Signed-off-by: Alex Goodman <wagoodman@users.noreply.github.com>
This commit is contained in:
Alex Goodman
2026-08-14 14:09:10 -04:00
committed by GitHub
parent d63c774fa9
commit 04a6fa1b41
2 changed files with 124 additions and 3 deletions
+12 -2
View File
@@ -120,9 +120,19 @@ func (r *readerAtAdapter) ReadAt(p []byte, off int64) (n int, err error) {
return 0, err
}
n, err = r.ReadSeekCloser.Read(p) // read from that absolute position
// io.ReaderAt requires len(p) bytes or a non-nil error, and the underlying reader honors neither half of
// that: a squashfs block that decompresses short returns fewer bytes with err == nil, and a read landing
// exactly on the end of the file returns a full buffer alongside io.EOF. io.ReadFull normalizes both.
n, err = io.ReadFull(r.ReadSeekCloser, p)
// ReadAt reports a short read at the end of the file as io.EOF. compared by identity on purpose:
// io.ReadFull returns this sentinel bare, and errors.Is would also downgrade a wrapped one from the
// underlying reader, turning genuine stream corruption into a benign end of file
if err == io.ErrUnexpectedEOF {
err = io.EOF
}
// restore the position for the stateful read/seek operations
// restore the position for the stateful read/seek operations. a read error wins over a restore failure:
// callers compare against io.EOF, and masking it would strand them without the data they did get
if restoreErr := r.restorePosition(currentPos); restoreErr != nil {
if err == nil {
err = restoreErr