Skip to content

Commit 62043a6

Browse files
ianlancetaylorgopherbot
authored andcommitted
internal/xcoff: use saferio to allocate slices
No test case because the problem can only happen for invalid data. Let the fuzzer find cases like this. For #47653 Fixes #58754 Change-Id: Ic3ef58b204b946f8bff80310d4c8dfcbb2939a1c Reviewed-on: https://go-review.googlesource.com/c/go/+/471678 Auto-Submit: Ian Lance Taylor <[email protected]> Reviewed-by: Ian Lance Taylor <[email protected]> Reviewed-by: Dmitri Shuralyov <[email protected]> Run-TryBot: Ian Lance Taylor <[email protected]> TryBot-Result: Gopher Robot <[email protected]> Run-TryBot: Ian Lance Taylor <[email protected]> Reviewed-by: Dmitri Shuralyov <[email protected]> Auto-Submit: Ian Lance Taylor <[email protected]>
1 parent d61ae9d commit 62043a6

File tree

1 file changed

+27
-16
lines changed

1 file changed

+27
-16
lines changed

src/internal/xcoff/file.go

Lines changed: 27 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -225,7 +225,11 @@ func NewFile(r io.ReaderAt) (*File, error) {
225225
if _, err := sr.Seek(int64(hdrsz)+int64(opthdr), io.SeekStart); err != nil {
226226
return nil, err
227227
}
228-
f.Sections = make([]*Section, nscns)
228+
c := saferio.SliceCap((**Section)(nil), uint64(nscns))
229+
if c < 0 {
230+
return nil, fmt.Errorf("too many XCOFF sections (%d)", nscns)
231+
}
232+
f.Sections = make([]*Section, 0, c)
229233
for i := 0; i < int(nscns); i++ {
230234
var scnptr uint64
231235
s := new(Section)
@@ -261,7 +265,7 @@ func NewFile(r io.ReaderAt) (*File, error) {
261265
}
262266
s.sr = io.NewSectionReader(r2, int64(scnptr), int64(s.Size))
263267
s.ReaderAt = s.sr
264-
f.Sections[i] = s
268+
f.Sections = append(f.Sections, s)
265269
}
266270

267271
// Symbol map needed by relocation
@@ -388,52 +392,59 @@ func NewFile(r io.ReaderAt) (*File, error) {
388392

389393
// Read relocations
390394
// Only for .data or .text section
391-
for _, sect := range f.Sections {
395+
for sectNum, sect := range f.Sections {
392396
if sect.Type != STYP_TEXT && sect.Type != STYP_DATA {
393397
continue
394398
}
395-
sect.Relocs = make([]Reloc, sect.Nreloc)
396399
if sect.Relptr == 0 {
397400
continue
398401
}
402+
c := saferio.SliceCap((*Reloc)(nil), uint64(sect.Nreloc))
403+
if c < 0 {
404+
return nil, fmt.Errorf("too many relocs (%d) for section %d", sect.Nreloc, sectNum)
405+
}
406+
sect.Relocs = make([]Reloc, 0, c)
399407
if _, err := sr.Seek(int64(sect.Relptr), io.SeekStart); err != nil {
400408
return nil, err
401409
}
402410
for i := uint32(0); i < sect.Nreloc; i++ {
411+
var reloc Reloc
403412
switch f.TargetMachine {
404413
case U802TOCMAGIC:
405414
rel := new(Reloc32)
406415
if err := binary.Read(sr, binary.BigEndian, rel); err != nil {
407416
return nil, err
408417
}
409-
sect.Relocs[i].VirtualAddress = uint64(rel.Rvaddr)
410-
sect.Relocs[i].Symbol = idxToSym[int(rel.Rsymndx)]
411-
sect.Relocs[i].Type = rel.Rtype
412-
sect.Relocs[i].Length = rel.Rsize&0x3F + 1
418+
reloc.VirtualAddress = uint64(rel.Rvaddr)
419+
reloc.Symbol = idxToSym[int(rel.Rsymndx)]
420+
reloc.Type = rel.Rtype
421+
reloc.Length = rel.Rsize&0x3F + 1
413422

414423
if rel.Rsize&0x80 != 0 {
415-
sect.Relocs[i].Signed = true
424+
reloc.Signed = true
416425
}
417426
if rel.Rsize&0x40 != 0 {
418-
sect.Relocs[i].InstructionFixed = true
427+
reloc.InstructionFixed = true
419428
}
420429

421430
case U64_TOCMAGIC:
422431
rel := new(Reloc64)
423432
if err := binary.Read(sr, binary.BigEndian, rel); err != nil {
424433
return nil, err
425434
}
426-
sect.Relocs[i].VirtualAddress = rel.Rvaddr
427-
sect.Relocs[i].Symbol = idxToSym[int(rel.Rsymndx)]
428-
sect.Relocs[i].Type = rel.Rtype
429-
sect.Relocs[i].Length = rel.Rsize&0x3F + 1
435+
reloc.VirtualAddress = rel.Rvaddr
436+
reloc.Symbol = idxToSym[int(rel.Rsymndx)]
437+
reloc.Type = rel.Rtype
438+
reloc.Length = rel.Rsize&0x3F + 1
430439
if rel.Rsize&0x80 != 0 {
431-
sect.Relocs[i].Signed = true
440+
reloc.Signed = true
432441
}
433442
if rel.Rsize&0x40 != 0 {
434-
sect.Relocs[i].InstructionFixed = true
443+
reloc.InstructionFixed = true
435444
}
436445
}
446+
447+
sect.Relocs = append(sect.Relocs, reloc)
437448
}
438449
}
439450

0 commit comments

Comments
 (0)