cc-wrapper: add glibcxxassertions hardening flag

no platforms "unsupported" because we don't have a nice
mechanism for determining a platform's c++ lib and the flag
should be harmlessly ignored by a other c++ libs

Changed files
+46 -1
doc
pkgs
build-support
cc-wrapper
stdenv
test
cc-wrapper
top-level
+3
doc/redirects.json
···
"strictflexarrays3": [
"index.html#strictflexarrays3"
],
+
"glibcxxassertions": [
+
"index.html#glibcxxassertions"
+
],
"tester-shfmt": [
"index.html#tester-shfmt"
],
+6
doc/stdenv/stdenv.chapter.md
···
sorry, unimplemented: __builtin_clear_padding not supported for variable length aggregates
```
+
#### `glibcxxassertions` {#glibcxxassertions}
+
+
Adds the `-D_GLIBCXX_ASSERTIONS` compiler flag. This flag only has an effect on libstdc++ targets, and when defined, enables extra error checking in the form of precondition assertions, such as bounds checking in c++ strings and null pointer checks when dereferencing c++ smart pointers.
+
+
These checks may have an impact on performance in some cases.
+
#### `pacret` {#pacret}
This flag adds the `-mbranch-protection=pac-ret` compiler option on aarch64-linux targets. This uses ARM v8.3's Pointer Authentication feature to sign function return pointers before adding them to the stack. The pointer's authenticity is then validated before returning to its destination. This dramatically increases the difficulty of ROP exploitation techniques.
+5 -1
pkgs/build-support/cc-wrapper/add-hardening.sh
···
if (( "${NIX_DEBUG:-0}" >= 1 )); then
-
declare -a allHardeningFlags=(fortify fortify3 shadowstack stackprotector stackclashprotection nostrictaliasing pacret strictflexarrays1 strictflexarrays3 pie pic strictoverflow format trivialautovarinit zerocallusedregs)
+
declare -a allHardeningFlags=(fortify fortify3 shadowstack stackprotector stackclashprotection nostrictaliasing pacret strictflexarrays1 strictflexarrays3 pie pic strictoverflow glibcxxassertions format trivialautovarinit zerocallusedregs)
declare -A hardeningDisableMap=()
# Determine which flags were effectively disabled so we can report below.
···
pacret)
if (( "${NIX_DEBUG:-0}" >= 1 )); then echo HARDENING: enabling pacret >&2; fi
hardeningCFlagsBefore+=('-mbranch-protection=pac-ret')
+
;;
+
glibcxxassertions)
+
if (( "${NIX_DEBUG:-0}" >= 1 )); then echo HARDENING: enabling glibcxxassertions >&2; fi
+
hardeningCFlagsBefore+=('-D_GLIBCXX_ASSERTIONS')
;;
stackprotector)
if (( "${NIX_DEBUG:-0}" >= 1 )); then echo HARDENING: enabling stackprotector >&2; fi
+1
pkgs/stdenv/generic/make-derivation.nix
···
"pie"
"relro"
"stackprotector"
+
"glibcxxassertions"
"stackclashprotection"
"strictoverflow"
"trivialautovarinit"
+30
pkgs/test/cc-wrapper/hardening.nix
···
runCommand,
runCommandWith,
runCommandCC,
+
writeText,
bintools,
hello,
debian-devscripts,
···
f3exampleWithStdEnv = writeCBinWithStdenv ./fortify3-example.c;
flexArrF2ExampleWithStdEnv = writeCBinWithStdenv ./flex-arrays-fortify-example.c;
+
+
checkGlibcxxassertionsWithStdEnv =
+
expectDefined:
+
writeCBinWithStdenv (
+
writeText "main.cpp" ''
+
#if${if expectDefined then "n" else ""}def _GLIBCXX_ASSERTIONS
+
#error "Expected _GLIBCXX_ASSERTIONS to be ${if expectDefined then "" else "un"}defined"
+
#endif
+
int main() {}
+
''
+
);
# for when we need a slightly more complicated program
helloWithStdEnv =
···
hardeningEnable = [ "shadowstack" ];
}) false;
+
glibcxxassertionsExplicitEnabled = checkGlibcxxassertionsWithStdEnv true stdenv {
+
hardeningEnable = [ "glibcxxassertions" ];
+
};
+
bindNowExplicitDisabled =
checkTestBin
(f2exampleWithStdEnv stdenv {
···
hardeningDisable = [ "shadowstack" ];
}) true;
+
glibcxxassertionsExplicitDisabled = checkGlibcxxassertionsWithStdEnv false stdenv {
+
hardeningDisable = [ "glibcxxassertions" ];
+
};
+
# most flags can't be "unsupported" by compiler alone and
# binutils doesn't have an accessible hardeningUnsupportedFlags
# mechanism, so can only test a couple of flags through altered
···
expectFailure = true;
};
+
glibcxxassertionsStdenvUnsupp =
+
checkGlibcxxassertionsWithStdEnv false (stdenvUnsupport [ "glibcxxassertions" ])
+
{
+
hardeningEnable = [ "glibcxxassertions" ];
+
};
+
fortify3EnabledEnvEnablesFortify1 =
checkTestBin
(f1exampleWithStdEnv stdenv {
···
allExplicitDisabledShadowStack = shadowStackTest (f1exampleWithStdEnv stdenv {
hardeningDisable = [ "all" ];
}) true;
+
+
glibcxxassertionsExplicitDisabled = checkGlibcxxassertionsWithStdEnv false stdenv {
+
hardeningDisable = [ "all" ];
+
};
+1
pkgs/top-level/variants.nix
···
"shadowstack"
"nostrictaliasing"
"pacret"
+
"glibcxxassertions"
"trivialautovarinit"
]
) super'.stdenv;