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

debug/elf: don't check section size #33121

Open
gregory-m opened this issue Jul 15, 2019 · 3 comments
Open

debug/elf: don't check section size #33121

gregory-m opened this issue Jul 15, 2019 · 3 comments

Comments

@gregory-m
Copy link
Contributor

@gregory-m gregory-m commented Jul 15, 2019

What version of Go are you using (go version)?

go1.12.7
also reproducible on tip

Does this issue reproduce with the latest release?

yes

What did you do?

https://play.golang.org/p/_5bUxWBh14g

package main

import (
	"debug/elf"
	"fmt"
	"strings"
)

func main() {
	data := "\u007fELF\x02\x01\x010000000000000" +
		"\x010000000000000000000" +
		"\x02\x00\x00\x00\x00\x00\x00\x0000000000\x00\x00\x00\x00" +
		"000\x0000"

	_, err := elf.NewFile(strings.NewReader(data))
	if err != nil {
		fmt.Println(err)
	}
}

What did you expect to see?

no panic

What did you see instead?

panic: runtime error: makeslice: len out of range

goroutine 1 [running]:
debug/elf.(*Section).Data(0x44b2c0, 0x43e300, 0x7f6900, 0x206590, 0x109440, 0x7fb880, 0x0, 0x0)
	/usr/local/go/src/debug/elf/file.go:105 +0x40
debug/elf.NewFile(0x15d640, 0x43e2e0, 0x43e2e0, 0x1293, 0x432070, 0x11)
	/usr/local/go/src/debug/elf/file.go:448 +0x1660
main.main()
	/tmp/sandbox898898450/prog.go:15 +0xa0

Fund with https://github.com/dvyukov/go-fuzz

@odeke-em odeke-em changed the title debug/elf don't check section size debug/elf: don't check section size Jul 15, 2019
@odeke-em
Copy link
Member

@odeke-em odeke-em commented Jul 15, 2019

Thank you for this report @gregory-m!

Kindly paging @tklauser @heschik @aclements.

@ianlancetaylor
Copy link
Contributor

@ianlancetaylor ianlancetaylor commented Jul 15, 2019

I'm not really sure how hard debug/elf should work to handle invalid input files.

@gregory-m
Copy link
Contributor Author

@gregory-m gregory-m commented Jul 16, 2019

I have same question as @ianlancetaylor, because I have 2 more panics in different places.

lmb added a commit to cilium/ebpf that referenced this issue Nov 27, 2020
Fuzzing the ELF loader isn't very succesful since it keeps crashing
in debug/elf. Most of the time the culprit is a call to elf.Section.Data().
This method allocates a buffer with a size taken from the ELF, which
can lead to outlandishly large allocations. It's not clear how to validate
elf.Section.Size since the code that creates the section doesn't know
the total length of the ELF.

Instead, add a wrapper that catches panics due to ELF parsing, and turns
them into errors. This isn't a fool proof solution since the runtime
can still kill the process due to an OOM, but hopefully we will still
crash less overall.

See golang/go#33121
lmb added a commit to cilium/ebpf that referenced this issue Nov 27, 2020
Fuzzing the ELF loader isn't very succesful since it keeps crashing
in debug/elf. Most of the time the culprit is a call to elf.Section.Data().
This method allocates a buffer with a size taken from the ELF, which
can lead to outlandishly large allocations. It's not clear how to validate
elf.Section.Size since the code that creates the section doesn't know
the total length of the ELF.

Instead, add a wrapper that catches panics due to ELF parsing, and turns
them into errors. This isn't a fool proof solution since the runtime
can still kill the process due to an OOM, but hopefully we will still
crash less overall.

See golang/go#33121
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Linked pull requests

Successfully merging a pull request may close this issue.

None yet
3 participants