LoadBucket and InlinePage cast unaligned on-disk bytes without copy or bounds check
Summary
InBucket is two uint64s and requires 8-byte alignment. LoadBucket (internal/common/utils.go), InBucket.InlinePage (internal/common/bucket.go), and the surgeon xray walker (internal/surgeon/xray.go) cast the value of a leaf element in place:
return (*InBucket)(unsafe.Pointer(&buf[0]))
The value of a leaf element begins at pos + ksize. Whenever an odd-length key is stored, the payload starts on an unaligned address. Casting &buf[0] to *InBucket is flagged by checkptr and causes SIGBUS on architectures that fault unaligned 64-bit access (e.g. 32-bit ARM, MIPS, SPARC). Furthermore, neither LoadBucket nor InlinePage checks that the buffer is long enough to contain the headers, risking out-of-bounds reads.
Proposed Fix
- Decode
InBucketby copy into a stack buffer usingbinary.LittleEndianwith bounds validation. - Allocate inline pages into 8-byte-aligned backing storage if unaligned.
- Nil-check bucket headers in
xray.go.
A Pull Request addressing this issue is submitted concurrently.
Source: etcd-io/bbolt