From 8e46c489d0a1a2741e7a4973006fa73000c94cd0 Mon Sep 17 00:00:00 2001 From: adisbladis Date: Fri, 8 May 2026 14:55:04 +1200 Subject: [PATCH] stdenv.mkDerivation: Optimise hardening flag handling --- pkgs/stdenv/generic/make-derivation.nix | 55 ++++++++++++------------- 1 file changed, 27 insertions(+), 28 deletions(-) diff --git a/pkgs/stdenv/generic/make-derivation.nix b/pkgs/stdenv/generic/make-derivation.nix index 7df90e856846..c5e72872d23d 100644 --- a/pkgs/stdenv/generic/make-derivation.nix +++ b/pkgs/stdenv/generic/make-derivation.nix @@ -41,7 +41,6 @@ let optional optionalString optionals - pipe remove seq splitString @@ -54,6 +53,7 @@ let unsafeGetAttrPos warnIf zipAttrsWith + any ; inherit (lib.generators) toPretty; @@ -175,10 +175,6 @@ let "zerocallusedregs" ]; - concretizeFlagImplications = - flag: impliesFlags: list: - if elem flag list then (list ++ impliesFlags) else list; - removedOrReplacedAttrNames = [ "checkInputs" "installCheckInputs" @@ -462,11 +458,6 @@ let actualValue; outputs' = if separateDebugInfo' then outputs ++ [ "debug" ] else outputs; - # hardeningDisable additionally supports "all". - erroneousHardeningFlags = subtractLists knownHardeningFlags ( - hardeningEnable ++ remove "all" hardeningDisable - ); - checkDependencyList = checkDependencyList' [ ]; checkDependencyList' = positions: name: deps: @@ -488,9 +479,19 @@ let concatMapStrings (ix: "element ${toString ix} of ") ([ index ] ++ positions) }${name} for ${attrs.name or attrs.pname}" ) 1 deps) deps; + + isErroneous = flag: !elem flag knownHardeningFlags; in - if erroneousHardeningFlags != [ ] then + if + # Check if any hardening flag is erroneous + any isErroneous hardeningEnable || any (flag: flag != "all" && isErroneous flag) hardeningDisable + then abort ( + let + erroneousHardeningFlags = subtractLists knownHardeningFlags ( + hardeningEnable ++ remove "all" hardeningDisable + ); + in "mkDerivation was called with unsupported hardening flags: " + toPretty { } { inherit @@ -733,23 +734,21 @@ let else null } = - let - enabledHardeningOptions = - if elem "all" hardeningDisable then - [ ] - else - subtractLists (unique ( - pipe hardeningDisable [ - # disabling fortify implies fortify3 should also be disabled - (concretizeFlagImplications "fortify" [ "fortify3" ]) - # disabling strictflexarrays1 implies strictflexarrays3 should also be disabled - (concretizeFlagImplications "strictflexarrays1" [ "strictflexarrays3" ]) - # disabling libcxxhardeningfast implies libcxxhardeningextensive should also be disabled - (concretizeFlagImplications "libcxxhardeningfast" [ "libcxxhardeningextensive" ]) - ] - )) (defaultHardeningFlags ++ hardeningEnable); - in - concatStringsSep " " enabledHardeningOptions; + concatStringsSep " " ( + if elem "all" hardeningDisable then + [ ] + else + filter ( + flag: + !(elem flag hardeningDisable) + # disabling fortify implies fortify3 should also be disabled + && (flag == "fortify3" -> !elem "fortify" hardeningDisable) + # disabling strictflexarrays1 implies strictflexarrays3 should also be disabled + && (flag == "strictflexarrays3" -> !elem "strictflexarrays1" hardeningDisable) + # disabling libcxxhardeningfast implies libcxxhardeningextensive should also be disabled + && (flag == "libcxxhardeningextensive" -> !elem "libcxxhardeningfast" hardeningDisable) + ) (defaultHardeningFlags ++ hardeningEnable) + ); # TODO: remove platform condition # Enabling this check could be a breaking change as it requires to edit nix.conf