proc/core: off-by-one error reading ELF core files (#2333)

core.(*splicedMemory).ReadMemory checked the entry interval
erroneously when dealing with contiguous entries.
This commit is contained in:
Alessandro Arzilli 2021-01-29 22:39:04 +01:00 committed by GitHub
parent f19d5e5c13
commit 11e4ed2bf9
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 51 additions and 1 deletions

@ -95,7 +95,7 @@ func (r *splicedMemory) Add(reader proc.MemoryReader, off, length uint64) {
func (r *splicedMemory) ReadMemory(buf []byte, addr uint64) (n int, err error) {
started := false
for _, entry := range r.readers {
if entry.offset+entry.length < addr {
if entry.offset+entry.length <= addr {
if !started {
continue
}

@ -146,6 +146,56 @@ func TestSplicedReader(t *testing.T) {
}
})
}
// Test some ReadMemory errors
mem := &splicedMemory{}
for _, region := range []region{
{[]byte{0xa1, 0xa2, 0xa3, 0xa4}, 0x1000, 4},
{[]byte{0xb1, 0xb2, 0xb3, 0xb4}, 0x1004, 4},
{[]byte{0xc1, 0xc2, 0xc3, 0xc4}, 0x1010, 4},
} {
r := bytes.NewReader(region.data)
mem.Add(&offsetReaderAt{r, region.off}, region.off, region.length)
}
got := make([]byte, 4)
// Read before the first mapping
_, err := mem.ReadMemory(got, 0x900)
if err == nil || !strings.HasPrefix(err.Error(), "error while reading spliced memory at 0x900") {
t.Errorf("Read before the start of memory didn't fail (or wrong error): %v", err)
}
// Read after the last mapping
_, err = mem.ReadMemory(got, 0x1100)
if err == nil || (err.Error() != "offset 4352 did not match any regions") {
t.Errorf("Read after the end of memory didn't fail (or wrong error): %v", err)
}
// Read at the start of the first entry
_, err = mem.ReadMemory(got, 0x1000)
if err != nil || !bytes.Equal(got, []byte{0xa1, 0xa2, 0xa3, 0xa4}) {
t.Errorf("Reading at the start of the first entry: %v %#x", err, got)
}
// Read at the start of the second entry
_, err = mem.ReadMemory(got, 0x1004)
if err != nil || !bytes.Equal(got, []byte{0xb1, 0xb2, 0xb3, 0xb4}) {
t.Errorf("Reading at the start of the second entry: %v %#x", err, got)
}
// Read straddling entries 1 and 2
_, err = mem.ReadMemory(got, 0x1002)
if err != nil || !bytes.Equal(got, []byte{0xa3, 0xa4, 0xb1, 0xb2}) {
t.Errorf("Straddled read of the second entry: %v %#x", err, got)
}
// Read past the end of the second entry
_, err = mem.ReadMemory(got, 0x1007)
if err == nil || !strings.HasPrefix(err.Error(), "error while reading spliced memory at 0x1008") {
t.Errorf("Read into gap: %v", err)
}
}
func withCoreFile(t *testing.T, name, args string) *proc.Target {