Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: mem cache out of range panic caused by overflow #3761

Merged
merged 1 commit into from
Jul 1, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 10 additions & 1 deletion pkg/proc/mem.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,12 @@ type memCache struct {
}

func (m *memCache) contains(addr uint64, size int) bool {
return addr >= m.cacheAddr && addr <= (m.cacheAddr+uint64(len(m.cache)-size))
end := addr + uint64(size)
if end < addr {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This branch is never exercised by any test. Can you add a case for it?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done, please take a look again.

// overflow
return false
}
return addr >= m.cacheAddr && end <= m.cacheAddr+uint64(len(m.cache))
}

func (m *memCache) ReadMemory(data []byte, addr uint64) (n int, err error) {
Expand Down Expand Up @@ -69,6 +74,10 @@ func cacheMemory(mem MemoryReadWriter, addr uint64, size int) MemoryReadWriter {
if size <= 0 {
return mem
}
if addr+uint64(size) < addr {
// overflow
return mem
}
switch cacheMem := mem.(type) {
case *memCache:
if cacheMem.contains(addr, size) {
Expand Down
20 changes: 20 additions & 0 deletions pkg/proc/proc_general_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,26 @@ func TestIssue554(t *testing.T) {
}
}

func TestIssue3760(t *testing.T) {
// unsigned integer overflow if len(m.cache) < size
mem := memCache{true, 0x20, make([]byte, 100), nil}
if mem.contains(0x20, 200) {
t.Fatalf("should be false")
}
// test overflow of end addr
mem = memCache{true, 0xfffffffffffffff0, make([]byte, 15), nil}
if !mem.contains(0xfffffffffffffff0, 15) {
t.Fatalf("should contain it")
}
if mem.contains(0xfffffffffffffff0, 16) {
t.Fatalf("shoud be false")
}
cm := cacheMemory(nil, 0xffffffffffffffff, 1)
if cm != nil {
t.Fatalf("should be nil")
}
}

type dummyMem struct {
t *testing.T
mem []byte
Expand Down