]> Cypherpunks repositories - gostls13.git/commitdiff
cmd/link: issue error if elf header overruns
authorPaul E. Murphy <murp@ibm.com>
Mon, 5 Apr 2021 18:36:13 +0000 (13:36 -0500)
committerLynn Boger <laboger@linux.vnet.ibm.com>
Thu, 15 Apr 2021 14:58:38 +0000 (14:58 +0000)
This is probably unlikely in practice, but when debugging alignment
related issues on ppc64 using very small text section splits, the elf
header could grow beyond the preallocated space and quietly stomp
on the first few text sections.

Change-Id: Ided58aa0b1e60f9da4b3cb277e4ebafcee4ec693
Reviewed-on: https://go-review.googlesource.com/c/go/+/307430
Reviewed-by: Cherry Zhang <cherryyz@google.com>
Reviewed-by: Lynn Boger <laboger@linux.vnet.ibm.com>
Run-TryBot: Cherry Zhang <cherryyz@google.com>
TryBot-Result: Go Bot <gobot@golang.org>
Trust: Lynn Boger <laboger@linux.vnet.ibm.com>

src/cmd/link/internal/ld/elf.go

index b4af0931ccc892d7e19693aca86e334d770fd3de..3ba9f329ee62ee8f41fa3df9719957338e3fd7c8 100644 (file)
@@ -2194,6 +2194,12 @@ elfobj:
        if a > elfreserve {
                Errorf(nil, "ELFRESERVE too small: %d > %d with %d text sections", a, elfreserve, numtext)
        }
+
+       // Verify the amount of space allocated for the elf header is sufficient.  The file offsets are
+       // already computed in layout, so we could spill into another section.
+       if a > int64(HEADR) {
+               Errorf(nil, "HEADR too small: %d > %d with %d text sections", a, HEADR, numtext)
+       }
 }
 
 func elfadddynsym(ldr *loader.Loader, target *Target, syms *ArchSyms, s loader.Sym) {