cc-wrapper: add glibcxxassertions hardening flag (#414987)

This commit is contained in:
Robert Scott
2025-08-29 20:20:39 +01:00
committed by GitHub
7 changed files with 49 additions and 1 deletions

View File

@@ -574,6 +574,9 @@
"strictflexarrays3": [ "strictflexarrays3": [
"index.html#strictflexarrays3" "index.html#strictflexarrays3"
], ],
"glibcxxassertions": [
"index.html#glibcxxassertions"
],
"tester-shfmt": [ "tester-shfmt": [
"index.html#tester-shfmt" "index.html#tester-shfmt"
], ],

View File

@@ -1688,6 +1688,12 @@ This should be turned off or fixed for build errors such as:
sorry, unimplemented: __builtin_clear_padding not supported for variable length aggregates 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} #### `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. 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.

View File

@@ -52,7 +52,7 @@ fi
if (( "${NIX_DEBUG:-0}" >= 1 )); then 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=() declare -A hardeningDisableMap=()
# Determine which flags were effectively disabled so we can report below. # Determine which flags were effectively disabled so we can report below.
@@ -111,6 +111,10 @@ for flag in "${!hardeningEnableMap[@]}"; do
if (( "${NIX_DEBUG:-0}" >= 1 )); then echo HARDENING: enabling pacret >&2; fi if (( "${NIX_DEBUG:-0}" >= 1 )); then echo HARDENING: enabling pacret >&2; fi
hardeningCFlagsBefore+=('-mbranch-protection=pac-ret') hardeningCFlagsBefore+=('-mbranch-protection=pac-ret')
;; ;;
glibcxxassertions)
if (( "${NIX_DEBUG:-0}" >= 1 )); then echo HARDENING: enabling glibcxxassertions >&2; fi
hardeningCFlagsBefore+=('-D_GLIBCXX_ASSERTIONS')
;;
stackprotector) stackprotector)
if (( "${NIX_DEBUG:-0}" >= 1 )); then echo HARDENING: enabling stackprotector >&2; fi if (( "${NIX_DEBUG:-0}" >= 1 )); then echo HARDENING: enabling stackprotector >&2; fi
hardeningCFlagsBefore+=('-fstack-protector-strong' '--param' 'ssp-buffer-size=4') hardeningCFlagsBefore+=('-fstack-protector-strong' '--param' 'ssp-buffer-size=4')

View File

@@ -178,6 +178,9 @@ stdenv.mkDerivation (finalAttrs: {
ZSTD_ROOT = zstd.dev; ZSTD_ROOT = zstd.dev;
}; };
# fails tests on glibc with this enabled
hardeningDisable = [ "glibcxxassertions" ];
preConfigure = '' preConfigure = ''
patchShebangs build-support/ patchShebangs build-support/
substituteInPlace "src/arrow/vendored/datetime/tz.cpp" \ substituteInPlace "src/arrow/vendored/datetime/tz.cpp" \

View File

@@ -154,6 +154,7 @@ let
"pie" "pie"
"relro" "relro"
"stackprotector" "stackprotector"
"glibcxxassertions"
"stackclashprotection" "stackclashprotection"
"strictoverflow" "strictoverflow"
"trivialautovarinit" "trivialautovarinit"

View File

@@ -4,6 +4,7 @@
runCommand, runCommand,
runCommandWith, runCommandWith,
runCommandCC, runCommandCC,
writeText,
bintools, bintools,
hello, hello,
debian-devscripts, debian-devscripts,
@@ -40,6 +41,17 @@ let
flexArrF2ExampleWithStdEnv = writeCBinWithStdenv ./flex-arrays-fortify-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 # for when we need a slightly more complicated program
helloWithStdEnv = helloWithStdEnv =
stdenv': env: stdenv': env:
@@ -502,6 +514,10 @@ nameDrvAfterAttrName (
hardeningEnable = [ "shadowstack" ]; hardeningEnable = [ "shadowstack" ];
}) false; }) false;
glibcxxassertionsExplicitEnabled = checkGlibcxxassertionsWithStdEnv true stdenv {
hardeningEnable = [ "glibcxxassertions" ];
};
bindNowExplicitDisabled = bindNowExplicitDisabled =
checkTestBin checkTestBin
(f2exampleWithStdEnv stdenv { (f2exampleWithStdEnv stdenv {
@@ -697,6 +713,10 @@ nameDrvAfterAttrName (
hardeningDisable = [ "shadowstack" ]; hardeningDisable = [ "shadowstack" ];
}) true; }) true;
glibcxxassertionsExplicitDisabled = checkGlibcxxassertionsWithStdEnv false stdenv {
hardeningDisable = [ "glibcxxassertions" ];
};
# most flags can't be "unsupported" by compiler alone and # most flags can't be "unsupported" by compiler alone and
# binutils doesn't have an accessible hardeningUnsupportedFlags # binutils doesn't have an accessible hardeningUnsupportedFlags
# mechanism, so can only test a couple of flags through altered # mechanism, so can only test a couple of flags through altered
@@ -897,6 +917,12 @@ nameDrvAfterAttrName (
expectFailure = true; expectFailure = true;
}; };
glibcxxassertionsStdenvUnsupp =
checkGlibcxxassertionsWithStdEnv false (stdenvUnsupport [ "glibcxxassertions" ])
{
hardeningEnable = [ "glibcxxassertions" ];
};
fortify3EnabledEnvEnablesFortify1 = fortify3EnabledEnvEnablesFortify1 =
checkTestBin checkTestBin
(f1exampleWithStdEnv stdenv { (f1exampleWithStdEnv stdenv {
@@ -1107,6 +1133,10 @@ nameDrvAfterAttrName (
allExplicitDisabledShadowStack = shadowStackTest (f1exampleWithStdEnv stdenv { allExplicitDisabledShadowStack = shadowStackTest (f1exampleWithStdEnv stdenv {
hardeningDisable = [ "all" ]; hardeningDisable = [ "all" ];
}) true; }) true;
glibcxxassertionsExplicitDisabled = checkGlibcxxassertionsWithStdEnv false stdenv {
hardeningDisable = [ "all" ];
};
} }
) )
) )

View File

@@ -164,6 +164,7 @@ self: super: {
"shadowstack" "shadowstack"
"nostrictaliasing" "nostrictaliasing"
"pacret" "pacret"
"glibcxxassertions"
"trivialautovarinit" "trivialautovarinit"
] ]
) super'.stdenv; ) super'.stdenv;