Repository navigation
Description
Please answer these questions before submitting your issue. Thanks!
What version of Go are you using (go version)?
go version go1.11 freebsd/amd64
Does this issue reproduce with the latest release?
Yes
What operating system and processor architecture are you using (go env)?
GOARCH="amd64"
GOBIN=""
GOCACHE="/home/shawn/.cache/go-build"
GOEXE=""
GOFLAGS=""
GOHOSTARCH="amd64"
GOHOSTOS="freebsd"
GOOS="freebsd"
GOPATH="/home/shawn/go"
GOPROXY=""
GORACE=""
GOROOT="/usr/local/go"
GOTMPDIR=""
GOTOOLDIR="/usr/local/go/pkg/tool/freebsd_amd64"
GCCGO="gccgo"
CC="clang"
CXX="clang++"
CGO_ENABLED="1"
GOMOD=""
CGO_CFLAGS="-g -O2"
CGO_CPPFLAGS=""
CGO_CXXFLAGS="-g -O2"
CGO_FFLAGS="-g -O2"
CGO_LDFLAGS="-g -O2"
PKG_CONFIG="pkg-config"
GOGCCFLAGS="-fPIC -m64 -pthread -fno-caret-diagnostics -Qunused-arguments -fmessage-length=0 -fdebug-prefix-map=/tmp/go-build851003260=/tmp/go-build -gno-record-gcc-switches"
What did you do?
Run a go application
What did you expect to see?
The heap mapped at a non-fixed location.
What did you see instead?
The heap mapped at a fixed location.
More information:
I've read through a previous discussion in 2012 about golang's use of deterministic memory allocations here: https://groups.google.com/forum/#!msg/golang-nuts/Jd9tlNc6jUE/qp2oyfEEfjQJ
Back then, -buildmode=pie didn't exist back then, so I figured a revisit of the topic might be warranted.
Go 1.11 introduced -buildmode=pie for FreeBSD, which HardenedBSD is able to take advantage of, given HBSD's PaX ASLR implementation. HardenedBSD is a hardened derivative of FreeBSD.
When applying -buildmode=pie with gitea, I wanted to verify that golang was able to take full advantage of HardenedBSD's robust ASLR implementation. I noticed a fixed mapping at 0xc000000000. I restarted the process, looked at the memory mappings again (procstat -v $PID) and saw 0xc000000000 again. Grepping through the golang 1.11 codebase revealed that Go allocates its heap at a fixed address, starting at 0xc000000000.
With the heap being allocated at a fixed mapping, golang applications are still not able to really utilize ASLR. Thus, -buildmode=pie is moot.
Given that the reason to implement PIE support is to take advantage of ASLR, I'm curious if perhaps updating the heap management code for ASLR support was overlooked. Or perhaps it wasn't, and this is deliberate. Or perhaps a totally different reason. Either way, could someone shed some light on this?
I submitted a post on the golang-nuts mailing list, asking the same questions. It can be found here: https://groups.google.com/d/msg/golang-nuts/pAGcmRA_V5s/2NnOPDEcBgAJ
Metadata
Metadata
Assignees
Labels
Type
Projects
- StatusShow more project fieldsDone
Activity
randall77 commentedon Sep 9, 2018
@aclements
[-]Feature request: Support for ASLR with the heap[/-][+]runtime: feature request: Support for ASLR with the heap[/+]lattera commentedon Dec 29, 2021
Just checking in. Any movement on this? I'm still seeing that the heap is mapped at a fixed address (
0xc000000000)ianlancetaylor commentedon Dec 29, 2021
Nothing has changed.
A randomly placed heap requires completely different code from support for Position Independent Executables. And fixed location of executable code is riskier than fixed location of non-executable data. I say this not to say that a randomly placed heap is a bad idea, but merely to observe that
-buildmode=piedoesn't really have anything to do with a randomly placed heap, despite their both being based on the same underlying idea.lattera commentedon Dec 29, 2021
I'm of the opinion that non-executable data is still executable (read: data-only attacks causing the code to take different code branches.) Fuzzers show this concept in action. Machines are weird. ;-)
In order for ASLR to be effective, everything's gotta be randomized. My original statement from years back about PIE is only tangentially related. It just doesn't make sense to support PIE without also randomizing the heap. (Not that I'm arguing to remove existing PIE support--the opposite: to include heap randomization.)
ianlancetaylor commentedon Dec 29, 2021
Since Go programs are always multi-threaded, the contents of the heap are already somewhat randomized. I can't agree that it doesn't make sense to support PIE without randomizing the heap address.
But I'm not opposed to this, it's just that somebody has to do the work. Want to volunteer?
lattera commentedon Dec 29, 2021
I'd love to, but my spare time's focused on HardenedBSD. What I can do, though, is ping the HardenedBSD community to see if anyone's interested in volunteering. I'll report back if I can find a volunteer.
5 remaining items
aclements commentedon Mar 7, 2022
@walterjwhite , mind filing a new issue? It looks like you have some compatibility issue between the Go runtime and ASLR, while this issue is about enabling randomization of the Go heap, which is not about correctness.
walterjwhite commentedon Mar 7, 2022
#51523
gopherbot commentedon Apr 28, 2025
Change https://go.dev/cl/668635 mentions this issue:
runtime: add randomization to arena allocation hintsrolandshoemaker commentedon May 5, 2025
For mixed Go/C binaries, heap base address randomization seems like a reasonable feature, especially when ASLR is already being used to prevent abuse of the C heap.
It seems like the general consensus is, at least for 64 bit platforms, that 32 bits of entropy is a baseline.
We use anywhere between 40 and 64 bit heap addresses, depending on the architecture. When we allocate heap arenas we align them to 4MB or 64MB chunks (22 or 26 bits of alignment), depending on the platform, and when we allocate pages within these arenas we align them to 8KB chunks (13 bits of alignment).
If we consider the "base" heap address to be the address of the first byte of the first page in the first heap arena, the alignment needs restrict our ability to incorporate entropy, since we need at least 14 "fixed" bits. On (most) 64 bit platforms, this would leave us with 34 bits that can be used for randomization.
On runtime initialization we currently generate a list of heap arena hints that start at a fixed address. If we were to randomize the base heap address, we could simply randomize the first arena hint.
There would be a couple of complexities to this. First, we'd need to make it possible to call randinit before mallocinit in order to generate useful random values. Currently randinit needs to do some allocations, but this could plausibly be fixed. Secondly, we'd need to randomize the base address while still maintaining alignment of both the arena, and pages.
It seems alignment could be maintained relatively simply. One approach would be as follows:
Our final "base address" (the first byte of the first page of the first heap arena) then becomes our randomized page aligned address.
This approach gives us
heap address bits - 14bits of entropy. On most 64 bit platforms that gives us 34 bits of entropy, and on most 32 bit platforms this gives us 18 bits of entropy.cc @9muir @mknyszek
gopherbot commentedon May 29, 2025
Change https://go.dev/cl/674835 mentions this issue:
runtime: randomize heap base addressanacrolix commentedon May 30, 2025
I tried https://go.dev/cl/674835 to address
fatal error: too many address space collisions for -race modebut no dice. I also made sure to use llvm from homebrew and avoid any forced PIE stuff I could find.I don't know if there's any other way I can check why it wouldn't work.
rolandshoemaker commentedon May 30, 2025
In CL674835 I made it so you cannot enable heap base randomization when using race mode, since the TSAN runtime requires the heap be in a very specific region (
[0x00c000000000, 0x00e000000000)), and because I'm not hugely convinced that using these two things together makes a ton of sense.I suspect the
too many address space collisionsissue is orthogonal to the general purpose of heap base randomization, likely the ASLR of the C portion of the program is just stamping over the address space the TSAN runtime is expecting to use? I'm not sure there is anything we can do about this on our end.runtime: randomize heap base address