Skip to content

Commit 0ae042f

Browse files
ianlancetaylorgopherbot
authored andcommitted
debug/elf: validate phentsize and shentsize
No test case because the problem can only happen for invalid data. Let the fuzzer find cases like this. Fixes #56129 Change-Id: I6c81933781384c5e2c8ba0fd99cec50455b9664a Reviewed-on: https://go-review.googlesource.com/c/go/+/441976 Reviewed-by: Ian Lance Taylor <[email protected]> Run-TryBot: Ian Lance Taylor <[email protected]> Reviewed-by: Than McIntosh <[email protected]> TryBot-Result: Gopher Robot <[email protected]> Reviewed-by: Joedian Reid <[email protected]> Auto-Submit: Ian Lance Taylor <[email protected]> Run-TryBot: Ian Lance Taylor <[email protected]>
1 parent 79d0d33 commit 0ae042f

File tree

1 file changed

+17
-0
lines changed

1 file changed

+17
-0
lines changed

src/debug/elf/file.go

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -344,6 +344,19 @@ func NewFile(r io.ReaderAt) (*File, error) {
344344
return nil, &FormatError{0, "invalid ELF shstrndx", shstrndx}
345345
}
346346

347+
var wantPhentsize, wantShentsize int
348+
switch f.Class {
349+
case ELFCLASS32:
350+
wantPhentsize = 8 * 4
351+
wantShentsize = 10 * 4
352+
case ELFCLASS64:
353+
wantPhentsize = 2*4 + 6*8
354+
wantShentsize = 4*4 + 6*8
355+
}
356+
if phnum > 0 && phentsize < wantPhentsize {
357+
return nil, &FormatError{0, "invalid ELF phentsize", phentsize}
358+
}
359+
347360
// Read program headers
348361
f.Progs = make([]*Prog, phnum)
349362
for i := 0; i < phnum; i++ {
@@ -439,6 +452,10 @@ func NewFile(r io.ReaderAt) (*File, error) {
439452
}
440453
}
441454

455+
if shnum > 0 && shentsize < wantShentsize {
456+
return nil, &FormatError{0, "invalid ELF shentsize", shentsize}
457+
}
458+
442459
// Read section headers
443460
f.Sections = make([]*Section, shnum)
444461
names := make([]uint32, shnum)

0 commit comments

Comments
 (0)