cmd/link: use relro for internally linked openbsd executables
OpenBSD binaries are always dynamically linked when linking internally.
Go makes system calls through libc there so ld.so is always present to
apply relro. Enable it by adding a new condition in the UseRelro()
method.
In support of CL 641615 which adds .openbsd.randomdata to relro segment.
For #69781
diff --git a/src/cmd/link/elf_test.go b/src/cmd/link/elf_test.go
index 9b06d71..37da349 100644
--- a/src/cmd/link/elf_test.go
+++ b/src/cmd/link/elf_test.go
@@ -625,6 +625,15 @@
}
defer ef.Close()
+ inRelro := func(sec *elf.Section) bool {
+ for _, p := range ef.Progs {
+ if p.Type == elf.PT_GNU_RELRO && p.Vaddr <= sec.Addr && sec.Addr < p.Vaddr+p.Memsz {
+ return true
+ }
+ }
+ return false
+ }
+
// Find the first data-related section to verify segment placement
var firstDataSection *elf.Section
for _, sec := range ef.Sections {
@@ -636,7 +645,7 @@
isAlloc := sec.Flags&elf.SHF_ALLOC != 0
isTLS := sec.Flags&elf.SHF_TLS != 0
- if isWrite && !isExec && isAlloc && !isTLS {
+ if isWrite && !isExec && isAlloc && !isTLS && !inRelro(sec) {
if firstDataSection == nil || sec.Addr < firstDataSection.Addr {
firstDataSection = sec
}
diff --git a/src/cmd/link/internal/ld/target.go b/src/cmd/link/internal/ld/target.go
index d0ce99f..5e46f84 100644
--- a/src/cmd/link/internal/ld/target.go
+++ b/src/cmd/link/internal/ld/target.go
@@ -80,6 +80,13 @@
// On darwin/ARM64, everything is PIE.
return true
}
+ if t.HeadType == objabi.Hopenbsd && t.LinkMode == LinkInternal {
+ // On OpenBSD, internally linked binaries are always dynamically linked
+ // and ld.so is always present to apply relro.
+ // Externally linked binaries may be static with nothing to apply relro.
+ // Layout is therefore left alone.
+ return true
+ }
return t.linkShared || (t.HeadType == objabi.Haix && t.LinkMode == LinkExternal)
}
}
| Inspect html for hidden footers to help with email filtering. To unsubscribe visit settings. |
| Inspect html for hidden footers to help with email filtering. To unsubscribe visit settings. |
| Auto-Submit | +1 |
| Code-Review | +2 |
| Inspect html for hidden footers to help with email filtering. To unsubscribe visit settings. |
if t.HeadType == objabi.Hopenbsd && t.LinkMode == LinkInternal {AIUI we should be able to enable this unconditionally for OpenBSD - external linking should handle relro in all cases.
| Inspect html for hidden footers to help with email filtering. To unsubscribe visit settings. |
if t.HeadType == objabi.Hopenbsd && t.LinkMode == LinkInternal {AIUI we should be able to enable this unconditionally for OpenBSD - external linking should handle relro in all cases.
I've tested this on OpenBSD 7.9 (amd64). When linkmode is set to external (with -linkmode=external -extldflags=-static), the result is a static bin and ld.so is not used to apply relro. End result is the segment is left writable.
| Inspect html for hidden footers to help with email filtering. To unsubscribe visit settings. |