Skip to content

runtime: feature request: Support for ASLR with the heap #27583

Closed

Description

@lattera

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

Activity

  1. randall77 commented on Sep 9, 2018

    @randall77
    Contributor
  2. changed the title [-]Feature request: Support for ASLR with the heap[/-] [+]runtime: feature request: Support for ASLR with the heap[/+] on Sep 9, 2018
  3. added
    NeedsInvestigationSomeone must examine and confirm this is a valid issue and not a duplicate of an existing one.
    on Sep 9, 2018
  4. added this to the Go1.12 milestone on Sep 9, 2018
  5. modified the milestones: Go1.12, Go1.13 on Jan 8, 2019
  6. modified the milestones: Go1.13, Go1.14 on Jun 3, 2019
  7. modified the milestones: Go1.14, Backlog on Oct 9, 2019
  8. lattera commented on Dec 29, 2021

    @lattera
    Author

    Just checking in. Any movement on this? I'm still seeing that the heap is mapped at a fixed address (0xc000000000)

  9. ianlancetaylor commented on Dec 29, 2021

    @ianlancetaylor
    Contributor

    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=pie doesn't really have anything to do with a randomly placed heap, despite their both being based on the same underlying idea.

  10. lattera commented on Dec 29, 2021

    @lattera
    Author

    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.)

  11. ianlancetaylor commented on Dec 29, 2021

    @ianlancetaylor
    Contributor

    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?

  12. lattera commented on Dec 29, 2021

    @lattera
    Author

    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.

  13. 5 remaining items

  14. aclements commented on Mar 7, 2022

    @aclements
    Member

    @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.

  15. walterjwhite commented on Mar 7, 2022

    @walterjwhite
  16. gopherbot commented on Apr 28, 2025

    @gopherbot
    Contributor

    Change https://go.dev/cl/668635 mentions this issue: runtime: add randomization to arena allocation hints

  17. rolandshoemaker commented on May 5, 2025

    @rolandshoemaker
    Member

    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:

    • We take a random uint64 (e.g. from bootstrapRand, after randinit has been called), and truncate it to the number of heap address bits available (e.g. 48). This is our initial base address.
    • We align the base address to the heap arena size (e.g. 64MB).
    • We set the base of the first arena to this address and map it.
    • We set the bottom zero bits (present from alignment, e.g. 25 bits) to another random value (i.e. by taking another random uint64, truncating it to 25 bits, right shifting it 48-25 bits, and OR'ing it with the base address), and align it to the page size (e.g. 8MB). This can also be done by just setting the bits between the heap arena alignment bit and the page alignment bit to random bits, e.g. bits 25-38 (with slightly more complex bit math).
    • We set mheap_.curArena.base to the randomized page aligned base address and mark all the memory between the heap aligned address and the page aligned address as allocated in mheap_.pages. This address space will be marked PROT_NONE, meaning we waste some amount of address space, but not the actual memory itself.

    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 - 14 bits 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

  18. gopherbot commented on May 29, 2025

    @gopherbot
    Contributor

    Change https://go.dev/cl/674835 mentions this issue: runtime: randomize heap base address

  19. anacrolix commented on May 30, 2025

    @anacrolix
    Contributor

    I tried https://go.dev/cl/674835 to address fatal error: too many address space collisions for -race mode but no dice. I also made sure to use llvm from homebrew and avoid any forced PIE stuff I could find.

    > file myexe
    myexe: Mach-O 64-bit executable arm64
    

    I don't know if there's any other way I can check why it wouldn't work.

  20. rolandshoemaker commented on May 30, 2025

    @rolandshoemaker
    Member

    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 collisions issue 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.

  21. added a commit that references this issue on Jul 24, 2025
    6669aa3
  22. locked and limited conversation to collaborators on Jul 24, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    FrozenDueToAgeNeedsInvestigationSomeone must examine and confirm this is a valid issue and not a duplicate of an existing one.compiler/runtimeIssues related to the Go compiler and/or runtime.

    Type

    No type

    Projects

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions