7 ms·
Compiler Options Hardening Guide for C and C++
- klysm 2y agoIt would be really nice if we had a versioning scheme that enabled developers to get secure by default and opt into performance tradeoffs
- vkaku 2y agoIt is hard that people have to remember these options on a per-compiler basis. I'd rather prefer people use easy to remember flags like `-O2` than the word soup mentioned here. Compiler writers should revisit their option matrices and come up with easy defaults today. Disclaimer: Used to work on the GCC code for option handling back in the day. Options like -O2 map to a whole bunch of individual options, and people only needed to remember adding -O2, which corresponded to different values in every era and yet subjectively meant: decently optimized code.
- teo_zero 2y ago> I'd rather prefer people use easy to remember flags Like -fhardened?
- vkaku 2y agoSure. -f is technically machine independent. -m should be used when having it implemented as machine dependent options. So if you are telling me all these security features are only developed without requiring to implement per machine level support then it makes sense.
- dapperdrake 2y agoThe interactions between different optimization passes may have surprising consequences. Endless loops are technically undefined behavior, can be dropped, except for their assembly jump tag entry point, and collide with the next function's assembly jump tag. All because of UB. Huge headache. Try debugging that. And interaction loops on games are sometimes endlessly waiting for input.
- teo_zero 2y agoSorry, I'm not sure I understand your point. Are you complaining because it has a leading -f instead of a different spelling?
- dailykoder 2y agoNice, thank you! Saved this. Mastering GCC compiler options feels harder than mastering C++ UB.
- dapperdrake 2y agoSuccinct.
- dapperdrake 2y agoSee also: https://c9x.me/compile/bib/ubc.pdf https://c9x.me/compile/bib/ubc.pdf via https://c9x.me/compile/bib/ https://c9x.me/compile/bib/ Related HN discussion: https://news.ycombinator.com/item?id=26925314 https://news.ycombinator.com/item?id=26925314
- dapperdrake 2y agoRelated: Rob Pike on programming style, especially his note in include files: http://doc.cat-v.org/bell_labs/pikestyle http://doc.cat-v.org/bell_labs/pikestyle See also: SQLites amalgamation. Others (iirc Philippe Gaultier) have called this a Unity build: https://sqlite.org/amalgamation.html https://sqlite.org/amalgamation.html Rob Pike on systems software research: http://doc.cat-v.org/bell_labs/utah2000/utah2000.html http://doc.cat-v.org/bell_labs/utah2000/utah2000.html EDIT: typo
- z_open 2y agoHis opinions on include files have fallen out of favor because compiling is faster and it adds needless work. Are there organizations that still do this? All the style guides I've seen do not.
- dapperdrake 2y agoIf your filesystem and disks are fast enough, then maybe Rob's assumptions don't apply.
- ryandrake 2y agoI still adhere to this for personal hobby projects, more out of a sense of craftsmanship than anything practical at this point.
- csb6 2y agoI believe clang and gcc avoid reading in and re-processing include files that are already included, so his advice is unnecessary and creates a lot of maintenance burden, especially for C++ where a lot more code is in header files. It may still be useful for old compilers, though.
- kevin_thibedeau 2y agoThey recognize include guards and skip any further inclusions for those cases. There are scenarios where you may want multiple inclusion and you can still have that.
- dapperdrake 2y agoFurther reading: Fabien Sanglard in driving compilers: https://fabiensanglard.net/dc/ https://fabiensanglard.net/dc/ GNU binutils with their own take on how to process static archives (libfoo.a) https://sourceware.org/bugzilla/show_bug.cgi?id=32006 https://sourceware.org/bugzilla/show_bug.cgi?id=32006 Linkers: Mold: https://news.ycombinator.com/item?id=26233244 https://news.ycombinator.com/item?id=26233244 Wild: https://news.ycombinator.com/item?id=42814683 https://news.ycombinator.com/item?id=42814683 List of FOSS C linkers: GNU ld GNU gold LLVM lld mold (by LLVM lld author) wild EDIT: typesetting
- plainOldText 2y agoThe 5-part series by Fabien Sanglard is really good. Thanks for sharing!
- MITSardine 2y agoIf my C++ project is a simple utility supposed to take some files, crunch numbers, and spit out results, is there still the possibility it can be used for nefarious purposes?
- rramadass 2y agoIt depends on what exactly your program does and equally important, where it is deployed and used. Security is a matter of degree based on context i.e. there are levels of Security. It is not a all or nothing proposition. If your program is going to be used for some non-critical work internally you don't have to bother much about attack surface/vectors etc. Just use some standard "healthy" compiler options and you are good. If you would like to know more on this subject, i recommend reading the classic The Art of Software Security Assessment: Identifying and Preventing Software Vulnerabilities by Mark Dowd et al.
- thfuran 2y agoHow does it get its input files? Where does it run? What's the output used for?
- kibwen 2y agoIt doesn't matter what the tool does, what matters is 1) whether it is ever exposed to untrusted input, 2) what permissions it has. If you don't ever expose something to untrusted input, then you're probably fine. But be VERY careful, because you should defensively consider anything downloaded off the internet to be untrusted input. As for permissions, if you run a tool inside of a sandbox inside of a virtual machine on an airgapped computer inside a Faraday cage six stories underground, then you're probably fine.
- duped 2y agoRead/write access to a filesystem is a pretty large surface area for attack, so yes.
- tuananh 2y agoWolfi OS (by Chainguard) is one of a few decided to adopt openssf compiler options https://github.com/wolfi-dev/os/blob/main/openssf-compiler-options/etc/clang/common.cfg https://github.com/wolfi-dev/os/blob/main/openssf-compiler-o...
- rurban 2y agoNo, he does not. He skipped most warnings
- derriz 2y agoSane defaults should be table stakes for toolchains but C++ has "history". All significant C++ code-bases and projects I've worked on have had 10s of lines (if not screens) of compiler and linker options - a maintenance nightmare particularly with stuff related to optimization. This stuff is so brittle, who knows when (with which release of the compiler or linker) a particular combination of optimization flags were actually beneficial? How do you regression test this stuff? So everyone is afraid to touch this stuff. Other compiled languages have similar issues but none to the extent of C++ that I've experienced.
- motorest 2y ago> Sane defaults should be table stakes for toolchains but C++ has "history". Yes, it has. By "history" you actually mean "production software that is expected to not break just because someone upgrades a compiler". Yes, C++ does have a lot of that. > All significant C++ code-bases and projects I've worked on have had 10s of lines (if not screens) of compiler and linker options - a maintenance nightmare particularly with stuff related to optimization. No, not really. That is definitely not the norm, at all. I can tell you as a matter of fact that release builds of some production software that's even a household name is built with only a couple of basic custom compiler flags, such as specifying the exact version of the target language. Moreover, if your project uses a build system such as CMake and your team is able to spend 5 minutes reading an onboarding guide onto modern CMake, you do not even need or care to set compiler flags. You set a few high-level target properties and you never look at it ever again.
- rowanG077 2y ago> Yes, it has. By "history" you actually mean "production software that is expected to not break just because someone upgrades a compiler". Yes, C++ does have a lot of that. I disagree. Disproportionately in my career random C and C++ code bases failed to build because some new warning was introduced. And this is precisely because compiler options are so bad in that a lot of projects do Wall, Wextra and Werror. Also the way undefined behavior is exploited means that you don't really know of your software that worked fine 10 years ago will actually work fine today, unless you have exhaustive tests.
- fuhsnn 2y agoIf you are taking notes, add `-fzero-init-padding-bits=all` to the list, without this flag, GCC 15 onwards will not zero-initialize a full union if you wrote pre-C23 style ={0} and the largest member is not the first one. `-ftrivial-auto-var-init` cannot help this case. https://godbolt.org/z/7zKccfnea https://godbolt.org/z/7zKccfnea
- stabbles 2y ago> The keyword $ORIGIN in rpath is expanded by the dynamic loader to the path of the directory where the object is found, which may be set by an attacker (e.g., via hard links) to a directory with a malicious dependency. On Linux, the fs.protected_hardlinks sysctl can help prevent this attack. This has nothing to do with hardlinks, the same applies to symlinks. On linux the status quo is that the dynamic loader finds the library by symlink, the convention is `libfoo.so.x -> libfoo.so.a.b.c` where `x` is the ABI version and `a.b.c` the full version. But if `libfoo.so.x -> /absolute/path/libfoo.so.a.b.c` and it has `$ORIGIN/libbar.so.y` in DT_NEEDED, those are resolved relative to the dir of the symlink, not to realpath of the symlink. That makes sense, cause it would be a lot of startup overhead to lstat every path component of every library that uses $ORIGIN. I don't see the point of including this gotcha in a security overview to be honest.
- grandempire 2y ago> Our threat model is that all software developers make mistakes, and sometimes those mistakes lead to vulnerabilities That’s not a threat model. What are the attackers going to do if there are vulnerabilities in your executable? Is it connected to a web server? Does it have access to privileged resources?
- steveklabnik 2y agoThey're using it in the sense of "the scope of this document covers this scenario," so the answer to all of your questions are out of scope.
- javier_e06 2y agoLast week a build broke because there was space after the Wl, some-linker-option The Warning messages can't be very challenging to decipher. Most importantly: Are the warnings show-stoppers? Not in part of my pay grade. There is a pragma to ignore specific warnings. This is "#pragma GCC diagnostic ignore "some-compiler-warning" which is useful when dealing with several versions of the GCC compiler. Yes, it happens.
- ryandrake 2y ago> Most importantly: Are the warnings show-stoppers? Not in part of my pay grade. The best places (code quality wise) I've ever worked were the strictest on compiler warnings. Turn on all warnings, turn on extra warnings, treat warnings as errors, and forbid disabling warnings via #pragma. The absolute worst was the one where compiling the software using the compiler's default warning level produced a deluge of 40,000 warnings, and the culture was to disable warnings when they became annoying (vs. you know, fixing them). My philosophy: Compilers don't issue warnings for fun. Every one of them is a potential problem and they are almost always worth fixing. I also adhere to this in my personal hobby projects, too. It can be challenging when integrating with third party libraries, where the library maintainer doesn't care as much. I once submitted a patch to an open source project I won't name here, which fixed a bunch of warnings that seem to be only present in macOS builds (XCode's defaults tend to be quite strict). The response was not to merge it because "I don't regularly do macOS builds, and besides, they're just warnings." Alright, bro, sorry I tried to help.
- mid-kid 2y agoWhile all of these are very useful, you'll find that a lot of these are already enabled by default in many distributions of the gcc compiler. Sometimes they're embedded in the compiler itself through a patch or configure flag, and sometimes they're added through CFLAGS variables during the compilation of distribution packages. I can only really speak of gentoo, but here's a non-exhaustive list: * -fPIE is enabled with --enable-default-pie in GCC's ./configure script * -fstack-protector-strong is enabled with --enable-default-ssp in GCC's ./configure script * -Wl,-z,relro is enabled with --enable-relro in Binutils' ./configure script * -Wp,-D_FORTIFY_SOURCE=2, -fstack-clash-protection, -Wl,-z,now and -fcf-protection=full are enabled by default through patches to GCC in Gentoo. * -Wl,--as-needed is enabled through the default LDFLAGS For reference, here's the default compiler flags for a few other distributions. Note that these don't include GCC patches: * Arch Linux: https://gitlab.archlinux.org/archlinux/packaging/packages/pacman/-/blob/main/makepkg.conf?ref_type=heads#L43 https://gitlab.archlinux.org/archlinux/packaging/packages/pa... * Alpine Linux: https://gitlab.alpinelinux.org/alpine/abuild/-/blob/master/default.conf?ref_type=heads#L1 https://gitlab.alpinelinux.org/alpine/abuild/-/blob/master/d... * Debian: It's a tiny bit more obscure, but running `dpkg-buildflags` on a fresh container returns the following: CFLAGS=-g -O2 -Werror=implicit-function-declaration -ffile-prefix-map=/home/<myuser>=. -fstack-protector-strong -fstack-clash-protection -Wformat -Werror=format-security -fcf-protection
- jpfr 2y agoMost of these are implicit with -fhardened. https://gcc.gnu.org/onlinedocs/gcc/Instrumentation-Options.html#index-fhardened https://gcc.gnu.org/onlinedocs/gcc/Instrumentation-Options.h...
- dapperdrake 2y agoFinally.
- westurner 2y agoFrom https://news.ycombinator.com/item?id=38505448 https://news.ycombinator.com/item?id=38505448 : > There are default gcc and/or clang compiler flags in distros' default build tools; e.g. `make` specifies additional default compiler flags (that e.g. cmake, ninja, gn, or bazel/buck/pants may not also specify for you). Is there a good reference for comparing these compile-time build flags and their defaults with Make, CMake, Ninja Build, and other build systems, on each platform and architecture? From https://news.ycombinator.com/item?id=41306658 https://news.ycombinator.com/item?id=41306658 : > From "Building optimized packages for conda-forge and PyPI" at EuroSciPy 2024: https://pretalx.com/euroscipy-2024/talk/JXB79J/ https://pretalx.com/euroscipy-2024/talk/JXB79J/ : >> Since some time, conda-forge defines multiple "cpu-levels". These are defined for sse, avx2, avx512 or ARM Neon. On the client-side the maximum CPU level is detected and the best available package is then installed. This opens the doors for highly optimized packages on conda-forge that support the latest CPU features. But those are per-arch performance flags, not security flags.