Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Remove pie from GOFLAGS #2237

Merged
merged 1 commit into from May 23, 2023
Merged

Remove pie from GOFLAGS #2237

merged 1 commit into from May 23, 2023

Conversation

luhring
Copy link
Contributor

@luhring luhring commented May 22, 2023

We don't think this setting has a worthwhile benefit to the Go binaries we're building. cc: @joshrwolf @kaniini

Signed-off-by: Dan Luhring <dluhring@chainguard.dev>
@luhring luhring requested a review from a team as a code owner May 22, 2023 18:16
@luhring luhring requested review from rawlingsj and kaniini May 22, 2023 18:16
@kaniini kaniini added this pull request to the merge queue May 23, 2023
Merged via the queue into wolfi-dev:main with commit e7a1108 May 23, 2023
5 checks passed
@mattmoor
Copy link
Contributor

I am curious about this because this feels like a security feature that let's the loader randomize the binary layout in memory.
We enable a variety of security flags in our C/C++ toolchain, so I'm curious why we think there isn't a benefit for Go?

I am also curious what this costs us in terms of downside to have on (I see a few comments about -race not working).

@luhring
Copy link
Contributor Author

luhring commented May 29, 2023

After consulting with my trusted companion, ChatGPT, I agree with @mattmoor that this is worth further thought.

Yes, there is a security benefit to using the -pie (Position Independent Executable) flag when building Go programs, or any other type of program for that matter.

Position Independent Executables (PIEs) are executables that can be loaded at any memory address without affecting the program's functionality. This makes it harder for attackers to exploit memory corruption vulnerabilities, such as buffer overflows or code injection attacks, as the exact memory addresses of critical functions and data are not known in advance.

By using the -pie flag during the compilation process, you enable the creation of Position Independent Executables. This flag tells the linker to generate an executable that can be loaded at any address in memory. It includes the necessary relocation information in the binary so that the operating system can adjust the memory addresses during runtime.

The benefits of using PIEs include:

  1. Address Space Layout Randomization (ASLR): ASLR is a security technique that randomizes the memory layout of an executable, making it difficult for attackers to predict the location of critical functions and data. PIEs are a prerequisite for effective ASLR implementation.

  2. Mitigation of memory corruption attacks: PIEs make it more challenging for attackers to exploit memory corruption vulnerabilities, as they cannot rely on fixed memory addresses for their malicious code or data.

  3. Defense-in-depth: Using PIEs is an additional security measure that complements other security mechanisms like stack canaries, non-executable memory (NX), and Address Sanitizer (ASan). It adds an extra layer of protection to your executable.

While PIEs enhance security, it's important to note that they are not a silver bullet and do not guarantee complete protection against all types of attacks. They are just one piece of the security puzzle, and it's crucial to follow other best practices like proper input validation, secure coding techniques, and regular security audits to ensure the overall security of your Go programs.

@joshrwolf
Copy link
Contributor

joshrwolf commented Sep 19, 2023

lifting this out of slack for future context:

the impetus for this change was -pie resulted in dynamically linked go binaries. we found this while dealing with the (sadly) many apps that like to mv/cp things around init-containers.

I agree with luhring that we probably overcorrected making -pie opt-in, and it's worth reconsidering.

there seem to be ways to have -pie and eat it too (static builds), which might be something we consider as well:

# https://stackoverflow.com/questions/64019336/go-compile-to-static-binary-with-pie
go build  -ldflags '-linkmode external -s -w -extldflags "--static-pie"' -buildmode=pie -tags 'osusergo,netgo,static_build' -o /hello hello.go

@kaniini
Copy link
Collaborator

kaniini commented Sep 19, 2023

We are likely to start building dynamic Go binaries for other reasons, namely using libcrypto for cryptography (so we can do a single build and support FIPS mode in the usual way).

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

4 participants