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

When compiling compilers, make their defaults secure #261

Merged
merged 4 commits into from Nov 4, 2023

Conversation

david-a-wheeler
Copy link
Contributor

No description provided.

Signed-off-by: David A. Wheeler <dwheeler@dwheeler.com>
@david-a-wheeler
Copy link
Contributor Author

@eslerm @thomasnyman - comments?

@kees
Copy link

kees commented Oct 18, 2023

Perhaps refer to Ubuntu's compiler flag wiki page, as this is what Ubuntu already does. (I'm not aware of any other distro that enables hardening options in their compiler, unfortunately.)
https://wiki.ubuntu.com/ToolChain/CompilerFlags

@eslerm
Copy link

eslerm commented Oct 18, 2023

The Ubuntu compiler flag wiki page is out of date when it comes to PIE (flags are not set, but compiler is built with --enable-default-pie). I'll be addressing this.

@setharnold
Copy link

The Ubuntu compiler flag wiki page is out of date when it comes to PIE (flags are not set, but compiler is built with --enable-default-pie). I'll be addressing this.

Depending upon the context of the discussion, either one might be appropriate. A developer looking for the 'best' options to use in their build might want the -fPIE or -fPIC options but someone packaging GCC might want the --enable-default-pie compiler option.

@siddhesh
Copy link
Contributor

Perhaps refer to Ubuntu's compiler flag wiki page, as this is what Ubuntu already does. (I'm not aware of any other distro that enables hardening options in their compiler, unfortunately.) https://wiki.ubuntu.com/ToolChain/CompilerFlags

I believe Gentoo also defaults to enabling many hardening features in their toolchain.

@eslerm
Copy link

eslerm commented Oct 18, 2023

clang sets --enable-default-pie as a default on Linux now:

CMake -DCLANG_DEFAULT_PIE_ON_LINUX=ON is now the default. This is used by
linux-gnu systems to decide whether -fPIE -pie is the default (instead of
-fno-pic -no-pie). This matches GCC installations on many Linux distros.
Note: linux-android and linux-musl always default to -fPIE -pie, ignoring
this variable. -DCLANG_DEFAULT_PIE_ON_LINUX will be removed in the future.

However, gcc itself does not.

Signed-off-by: David A. Wheeler <dwheeler@dwheeler.com>
Copy link
Contributor

@gkunz gkunz left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM expect for the small nit-pick.

@thomasnyman
Copy link
Contributor

I can understand the rationale for including this, but I'm not so sure about whether the introductory section (which is already rather long) is the best place for this. I'm mindful of the feedback we have received regarding the amount of prose.

I'm also not a big fan of jumping straight into specific options in this section (apart from the TLDR).

Would this be a better fit as either a separate section or appendix with the options in a table for consistency?

@david-a-wheeler
Copy link
Contributor Author

Let's move it later into its own section. Most people don't compile their own compilers.

@thesamesam
Copy link
Contributor

thesamesam commented Oct 28, 2023

Perhaps refer to Ubuntu's compiler flag wiki page, as this is what Ubuntu already does. (I'm not aware of any other distro that enables hardening options in their compiler, unfortunately.) wiki.ubuntu.com/ToolChain/CompilerFlags

I believe Gentoo also defaults to enabling many hardening features in their toolchain.

You can see what we do at https://wiki.gentoo.org/wiki/Hardened/Toolchain#Changes. We don't set any of that via flags-per-package, instead via either patches or configure arguments to GCC or Binutils. For Clang, we use config files in /etc/clang as upstream are trying to move towards that rather than adding many CMake configuration options for stuff like this (we barely got them to add PIE).

I'm happy to help describe what we do and also document how others can do the same, as well as anything else you think I can help with.

@jvoisin and I have also been working on https://github.com/jvoisin/compiler-flags-distro.

Signed-off-by: David A. Wheeler <dwheeler@dwheeler.com>
Signed-off-by: David A. Wheeler <dwheeler@dwheeler.com>
@david-a-wheeler
Copy link
Contributor Author

I think this tweaked version covers the gist: If you control compiling the compiler, make the defaults the secure ones.

That said, many people aren't recompiling their compilers, so we need to give them guidance for their cases. If they include lots of options in their builds, the code is likely to work with those options elsewhere too :-).

Copy link
Contributor

@thesamesam thesamesam left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We should talk about doing more here in future, but the guidance here is solid and there's no reason not to just add more stuff later.

Thanks!

@david-a-wheeler
Copy link
Contributor Author

Ok. I'll merge this now, we can add more later. Thank you everyone!

@david-a-wheeler david-a-wheeler merged commit 45a1eb5 into main Nov 4, 2023
5 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

8 participants