From 11e4ed2bf9c5b7600bb62198ac609ca138642a03 Mon Sep 17 00:00:00 2001 From: Alessandro Arzilli Date: Fri, 29 Jan 2021 22:39:04 +0100 Subject: [PATCH] proc/core: off-by-one error reading ELF core files (#2333) core.(*splicedMemory).ReadMemory checked the entry interval erroneously when dealing with contiguous entries. --- pkg/proc/core/core.go | 2 +- pkg/proc/core/core_test.go | 50 ++++++++++++++++++++++++++++++++++++++ 2 files changed, 51 insertions(+), 1 deletion(-) diff --git a/pkg/proc/core/core.go b/pkg/proc/core/core.go index 0406d045..4e357ac5 100644 --- a/pkg/proc/core/core.go +++ b/pkg/proc/core/core.go @@ -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 } diff --git a/pkg/proc/core/core_test.go b/pkg/proc/core/core_test.go index 5373163a..7c218ed8 100644 --- a/pkg/proc/core/core_test.go +++ b/pkg/proc/core/core_test.go @@ -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 {