Commit Graph

41 Commits

Author SHA1 Message Date
Maximilian Bosch
aaeeef5b6c
stdenv: fix custom hardening settings when using __structuredAttrs = true;
Replaces / Closes #353131

A while ago `postgresql` switched to using structured attrs[1]. In the
PR it was reported that this made postgresql notably slower when
importing SQL dumps[2].

After a bit of debugging it turned out that the hardening was entirely
missing and the following combination of settings was the culprit:

    hardeningEnable = [ "pie" ];
    __structuredAttrs = true;

I.e. the combination of custom hardening settings and structured attrs.

What happened here is that internally the default and enabled hardening
flags get written into `NIX_HARDENING_ENABLE`. However, the value is a list
and the setting is not in the `env` section. This means that in the
structured-attrs case we get something like

    declare -ax NIX_HARDENING_ENABLE=([0]="bindnow" [1]="format" [2]="fortify" [3]="fortify3" [4]="pic" [5]="relro" [6]="stackprotector" [7]="strictoverflow" [8]="zerocallusedregs" [9]="pie")

i.e. an actual array rather than a string with all hardening flags being
space-separated which is what the hardening code of the cc-wrapper
expects[3].

This only happens if `hardeningEnable` or `hardeningDisable` are
explicitly set by a derivation: if none of those are set,
`NIX_HARDENING_ENABLE` won't be set by `stdenv.mkDerivation` and the
default hardening flags are configured by the setup hook of the
cc-wrapper[4].

In other words, this _only_ applies to derivations that have both custom
hardening settings _and_ `__structuredAttrs = true;`.

All values of `NIX_HARDENING_ENABLE` are well-known, so we don't have to
worry about escaping issues. Just forcing it to a string by
concatenating the list everytime solves the issue without additional
issues like eval errors when inheriting `env` from a structuredAttrs
derivation[5]. The price we're paying is a full rebuild.

[1] https://github.com/NixOS/nixpkgs/pull/294504
[2] https://github.com/NixOS/nixpkgs/pull/294504#issuecomment-2451482522
[3] cf3e5d3744/pkgs/build-support/cc-wrapper/add-hardening.sh (L9)
[4] cf3e5d3744/pkgs/build-support/cc-wrapper/setup-hook.sh (L114)
[5] 1e84a7fb95
2024-11-02 22:30:01 +01:00
github-actions[bot]
336ffcf0e4
Merge staging-next into staging 2024-10-31 18:04:59 +00:00
Artturin
5f2d5d77da tests.cc-wrapper.default: reduce rebuild amount from 429 to 319
On native the `emulator` is now `execline` which adds 110 more dependencies.
2024-10-28 00:50:16 +02:00
Emily
04ba4d1a3b bintools-wrapper: fix late‐bound command references
This ports 985072e2ce to bintools-wrapper.

Co-authored-by: Artturin <Artturin@artturin.com>
Co-authored-by: IlyaNiklyaev <ilya.niklyaev@gmail.com>
2024-10-27 00:32:46 +03:00
Artturin
62f2fc1ae5 tests.cc-wrapper: Add NIX_CC_USE_RESPONSE_FILE check 2024-10-21 21:41:12 +03:00
Artturin
8de11b252d tests.cc-wrapper: Run CC CXX with an empty path
Ensure our wrappers hardcode all the necessary tools
2024-10-18 23:45:22 +03:00
Artturin
e0464e4788 treewide: replace stdenv.is with stdenv.hostPlatform.is
In preparation for the deprecation of `stdenv.isX`.

These shorthands are not conducive to cross-compilation because they
hide the platforms.

Darwin might get cross-compilation for which the continued usage of `stdenv.isDarwin` will get in the way

One example of why this is bad and especially affects compiler packages
https://www.github.com/NixOS/nixpkgs/pull/343059

There are too many files to go through manually but a treewide should
get users thinking when they see a `hostPlatform.isX` in a place where it
doesn't make sense.

```
fd --type f "\.nix" | xargs sd --fixed-strings "stdenv.is" "stdenv.hostPlatform.is"
fd --type f "\.nix" | xargs sd --fixed-strings "stdenv'.is" "stdenv'.hostPlatform.is"
fd --type f "\.nix" | xargs sd --fixed-strings "clangStdenv.is" "clangStdenv.hostPlatform.is"
fd --type f "\.nix" | xargs sd --fixed-strings "gccStdenv.is" "gccStdenv.hostPlatform.is"
fd --type f "\.nix" | xargs sd --fixed-strings "stdenvNoCC.is" "stdenvNoCC.hostPlatform.is"
fd --type f "\.nix" | xargs sd --fixed-strings "inherit (stdenv) is" "inherit (stdenv.hostPlatform) is"
fd --type f "\.nix" | xargs sd --fixed-strings "buildStdenv.is" "buildStdenv.hostPlatform.is"
fd --type f "\.nix" | xargs sd --fixed-strings "effectiveStdenv.is" "effectiveStdenv.hostPlatform.is"
fd --type f "\.nix" | xargs sd --fixed-strings "originalStdenv.is" "originalStdenv.hostPlatform.is"
```
2024-09-25 00:04:37 +03:00
Robert Scott
2e0d7e230a cc-wrapper hardeningFlags tests: fix stdenvUnsupport-based tests
these were not updated to understand
hardeningUnsupportedFlagsByTargetPlatform when it was added
causing more tests to fail for clang than otherwise would
2024-07-15 19:28:11 +01:00
Robert Scott
38b580b21a cc-wrapper hardeningFlags tests: add tests for stackclashprotection 2024-07-15 19:28:02 +01:00
Robert Scott
ec8d29ab82 cc-wrapper hardeningFlags tests: fix expected behaviour in corner cases
also use fortify1-based tests in some places that it may allow us
to better test the behaviour of toolchains that only support that
2024-07-12 22:30:18 +01:00
Peter Waller
3c03811683 llvmPackages_*.libcxx: include libcxxabi within libcxx
Key test case: nixpkgs#pkgsStatic.pkgsLLVM.ncurses

Prior to this patch, this fails with errors such as:

```
error: undefined symbol: __cxa_throw
```

I think this is a reasonable solution because in #292043, libcxxabi was
'merged into libcxx', however, the commit message suggests that only
dynamic linking was accounted for, because it says:

```
* linux/freebsd `libc++.so` is a linker script `LINK(libc++.so.1, -lc++abi)` making `-lc++` sufficient.
```

Whereas, I found that if I tried linking a "hello world" C++ program
with a static hostPlatform, it failed unless -lc++abi was passed.

Signed-off-by: Peter Waller <p@pwaller.net>
2024-05-10 17:22:20 +02:00
Artturin
dabfaef444 tests.cc-wrapper: add atomics test
`-std=c++17` is for clang 5
2023-11-16 00:23:56 +02:00
Artturin
42f3292616 tests.cc-wrapper.supported: add test for cxxabi header
`#include <cxxabi.h>`

`/nix/store/02wpjmp2zjjxz13z7g599mniwi25zkcy-libcxxabi-16.0.6-dev/include/cxxabi.h:20:10: fatal error: '__cxxabi_config.h' file not found`
2023-09-18 06:43:36 +05:30
Artturin
535447e289 tests.cc-wrapper: show command output on different line
this is much easier to read.
2023-09-07 22:36:16 +03:00
Artturin
1b6c3aed63 tests.cc-wrapper: show more prominently what cc is being tested
Show what libc is used, otherwise there's indication what libc is being
used. Ex in gccMultiStdenv.
2023-09-07 22:36:15 +03:00
Robert Scott
e0f6367446 cc-wrapper, binutils-wrapper: add tests hardening-flags-handling
most tests use debian-devscripts' hardening-check, so only work on
ELF systems and can only detect a limited subset of flags.

some extra tests actually execute fortify-protected programs and
should be slightly more universally applicable.
2023-09-02 15:01:54 +01:00
John Ericson
f4868c6c7d
Merge pull request #229754 from rrbutani/fix/cc-wrapper-extra-positional-args
Support `--` in `cc-wrapper`
2023-05-08 06:55:52 -04:00
Artturin
1d9f58c7a4 tests.cc-multilib: put error on separate line 2023-05-05 23:55:24 +03:00
Rahul Butani
40c914f1be
cc-wrapper-test: add tests for -- 2023-05-03 16:45:48 -05:00
Nick Cao
e419aa8283 cc-wrapper-test: add workaround for asan allocation error
Miminal program fail with address sanitizer error failed to allocate 0x0 (0) bytes of SetAlternateSignalStack
https://bugzilla.redhat.com/show_bug.cgi?id=1950244
2022-12-25 09:19:28 +01:00
Nick Cao
42cd6aebe4 cc-wrapper-test: do not test sanitizers when cross compiling 2022-12-25 09:19:28 +01:00
Nick Cao
8643dbc57e cc-wrapper-test: do not test sanitizers on darwin 2022-12-25 09:19:28 +01:00
Nick Cao
57ff6191af cc-wrapper-test: support cross compilers 2022-12-25 09:19:28 +01:00
Jörg Thalheim
166948d479 cc-wrapper: don't set rpath on static-pie executables 2021-05-23 17:38:17 +00:00
Pavol Rusnak
90f7338112
treewide: stdenv.lib -> lib 2021-01-24 01:49:49 +01:00
Jörg Thalheim
61bbbcd1af
bintools-wrapper: skip dynamic linker for static binaries 2020-12-27 16:42:11 +01:00
Vladimír Čunát
363175cd99
Revert "bintools-wrapper: skip dynamic linker for static binaries"
This reverts commit ccfd26ef14.

These toolchain changes are too problematic, so reverting for now; see
https://github.com/NixOS/nixpkgs/pull/107086#issuecomment-749196366
2020-12-21 22:27:48 +01:00
Jörg Thalheim
b39dd890ac
tests.cc-wrapper: disable static compilation macOS
macOS does not support this
2020-12-14 20:53:00 +01:00
Jörg Thalheim
ccfd26ef14 bintools-wrapper: skip dynamic linker for static binaries
Currently we set dynamic-linker unconditionally. This breaks
however some static binaries i.e. rust binaries linked against musl.
There is no reason we should set an elf interpreter for static binaries
hence this is skipped if `-static` or `-static-pie` is either passed to
our cc or ld wrapper.
2020-12-14 15:42:54 +00:00
Jörg Thalheim
96092dc936
stdenv: make -nostdinc work as intended
Right now we add glibc to search path also -nostdinc was provided,
which breaks projects providing their own gcc.
2020-07-23 08:39:46 +01:00
John Ericson
b3d957649f Merge remote-tracking branch 'upstream/master' into staging 2018-06-18 15:30:39 -04:00
John Ericson
e3b1937baf tests.cc-wrapper: Fix sanitizer condition
fc9644d4c9 accidentally enabled the
sanitizer tests for GCC on Darwin, when fixing that case was never
attempted. Also inverted the condition from broken to working for
clarity.
2018-06-18 15:05:57 -04:00
John Ericson
6e7e22da70 llvm 5: split out compiler-rt and remove libcxxabi dep
We already did them on non-mass-rebuild llvm 6. Also, this allows
simplifying the stdenv booting.

We were missing the libcxxabi dep in compile-rt in llvm 6, so fixed that
too.
2018-06-14 19:22:15 -04:00
John Ericson
fc9644d4c9 llvm 6: Fix libcxxabi impurity and darwin sanitizers 2018-06-14 19:19:54 -04:00
Orivej Desh
07ebb8bb79 tests.cc-wrapper: do not test sanitizers on darwin
They are not supported yet.

https://github.com/NixOS/nixpkgs/pull/41284#issuecomment-394977350
2018-06-06 08:01:13 +00:00
Benjamin Saunders
f24f6d7af3 tests.cc-wrapper: skip known-broken sanitizer cases 2018-05-30 18:48:42 -07:00
Benjamin Saunders
b0d0b1adfe clang_6: fix sanitizers under libstdc++ 2018-05-24 15:04:30 -07:00
Benjamin Saunders
46eeef1898 tests.cc-wrapper: verify building with sanitizers 2018-05-24 02:57:44 -04:00
Will Dietz
9d8f9b2e53 Add clang multilib variants (x64_64-only, 64/32bit), basic multilib tests 2017-12-05 07:17:14 -06:00
John Ericson
5aec3f96a2 treewide: Use *Platform.extensions 2017-09-13 11:16:10 -04:00
Daiderd Jordan
19c4673310
nixpkgs-tests: add basic test for cc-wrapper 2017-09-10 22:49:39 +02:00