Commit Graph

15001 Commits

Author SHA1 Message Date
Eelco Dolstra
8e75ad1995
Merge pull request #10609 from Ma27/backport-fchmodat2-fixes
[2.18] prohibit creating setuid/setgid binaries with fchmodat2
2024-04-25 10:27:59 +02:00
Maximilian Bosch
a75c34a2c9
Require at least libseccomp 2.5.5
Closes #10585

As it turns out, libseccomp maintains an internal syscall table and
validates each rule against it. This means that when using libseccomp
2.5.4 or older, one may pass `452` as syscall number against it, but
since it doesn't exist in the internal structure, `libseccomp` will refuse
to create a filter for that. This happens with nixpkgs-23.11, i.e. on
stable NixOS and when building Nix against the project's flake.

To work around that

* a backport of libseccomp 2.5.5 on upstream nixpkgs has been
  scheduled[1].

* the package now uses libseccomp 2.5.5 on its own already. This is to
  provide a quick fix since the correct fix for 23.11 is still a staging cycle
  away.

It must not be possible to build a Nix with an incompatible libseccomp
version (nothing can be built in a sandbox on Linux!), so configure.ac
rejects libseccomp if `__SNR_fchmodat2` is not defined.

We still need the compat header though since `SCMP_SYS(fchmodat2)`
internally transforms this into `__SNR_fchmodat2` which points to
`__NR_fchmodat2` from glibc 2.39, so it wouldn't build on glibc 2.38.
The updated syscall table from libseccomp 2.5.5 is NOT used for that
step, but used later, so we need both, our compat header and their
syscall table 🤷

[1] https://github.com/NixOS/nixpkgs/pull/306070

(cherry picked from commit 73918b0ae4)
2024-04-24 23:41:46 +02:00
Maximilian Bosch
39b08e3766
libstore/local-derivation-goal: prohibit creating setuid/setgid binaries
With Linux kernel >=6.6 & glibc 2.39 a `fchmodat2(2)` is available that
isn't filtered away by the libseccomp sandbox.

Being able to use this to bypass that restriction has surprising results
for some builds such as lxc[1]:

> With kernel ≥6.6 and glibc 2.39, lxc's install phase uses fchmodat2,
> which slips through 9b88e52846/src/libstore/build/local-derivation-goal.cc (L1650-L1663).
> The fixupPhase then uses fchmodat, which fails.
> With older kernel or glibc, setting the suid bit fails in the
> install phase, which is not treated as fatal, and then the
> fixup phase does not try to set it again.

Please note that there are still ways to bypass this sandbox[2] and this is
mostly a fix for the breaking builds.

This change works by creating a syscall filter for the `fchmodat2`
syscall (number 452 on most systems). The problem is that glibc 2.39
and seccomp 2.5.5 are needed to have the correct syscall number available
via `__NR_fchmodat2` / `__SNR_fchmodat2`, but this flake is still on
nixpkgs 23.11. To have this change everywhere and not dependent on the
glibc this package is built against, I added a header
"fchmodat2-compat.hh" that sets the syscall number based on the
architecture. On most platforms its 452 according to glibc with a few
exceptions:

    $ rg --pcre2 'define __NR_fchmodat2 (?!452)'
    sysdeps/unix/sysv/linux/x86_64/x32/arch-syscall.h
    58:#define __NR_fchmodat2 1073742276

    sysdeps/unix/sysv/linux/mips/mips64/n32/arch-syscall.h
    67:#define __NR_fchmodat2 6452

    sysdeps/unix/sysv/linux/mips/mips64/n64/arch-syscall.h
    62:#define __NR_fchmodat2 5452

    sysdeps/unix/sysv/linux/mips/mips32/arch-syscall.h
    70:#define __NR_fchmodat2 4452

    sysdeps/unix/sysv/linux/alpha/arch-syscall.h
    59:#define __NR_fchmodat2 562

I tested the change by adding the diff below as patch to
`pkgs/tools/package-management/nix/common.nix` & then built a VM from
the following config using my dirty nixpkgs master:

    {
      vm = { pkgs, ... }: {
        virtualisation.writableStore = true;
        virtualisation.memorySize = 8192;
        virtualisation.diskSize = 12 * 1024;
        nix.package = pkgs.nixVersions.nix_2_21;
      };
    }

The original issue can be triggered via

    nix build -L github:nixos/nixpkgs/d6dc19adbda4fd92fe9a332327a8113eaa843894#lxc \
      --extra-experimental-features 'nix-command flakes'

however the problem disappears with this patch applied.

Closes #10424

[1] https://github.com/NixOS/nixpkgs/issues/300635#issuecomment-2031073804
[2] https://github.com/NixOS/nixpkgs/issues/300635#issuecomment-2030844251

(cherry picked from commit ba68045187)
2024-04-24 23:28:23 +02:00
Théophane Hufschmitt
38822ce6d7 Add a release note for the build-dir hardening 2024-04-22 18:37:54 -04:00
Théophane Hufschmitt
3481a9c41d Run the builds in a daemon-controled directory
Instead of running the builds under
`$TMPDIR/{unique-build-directory-owned-by-the-build-user}`, run them
under `$TMPDIR/{unique-build-directory-owned-by-the-daemon}/{subdir-owned-by-the-build-user}`
where the build directory is only readable and traversable by the daemon user.

This achieves two things:

1. It prevents builders from making their build directory world-readable
   (or even writeable), which would allow the outside world to interact
   with them.
2. It prevents external processes running as the build user (either
   because that somehow leaked, maybe as a consequence of 1., or because
   `build-users` isn't in use) from gaining access to the build
   directory.

fix: do not use unknown setting

tests: remove build-dir test
2024-04-22 18:37:52 -04:00
Théophane Hufschmitt
d24431dea2 Add a test for the user sandboxing
test: add user-sandboxing to hydraJobs
2024-04-22 09:56:49 -04:00
Valentin Gagarin
6f9d2e46fa
show Nix logo in the manual (#10445)
the location of files is hard-coded by mdBook.
there is also seems to be no way to define custom templates, therefore
all styling has to be done in the CSS override.

Co-authored-by: Robert Hensing <roberth@users.noreply.github.com>
2024-04-09 09:37:44 +00:00
Eelco Dolstra
76364a847d
Merge pull request #10327 from thufschmitt/allow-leading-dot-2.18
[Backport 2.18-maintenance] allow leading period
2024-03-26 18:08:58 +01:00
Robert Hensing
6e3548f866 Disallow store path names that are . or .. (plus opt. -)
As discussed in the maintainer meeting on 2024-01-29.

Mainly this is to avoid a situation where the name is parsed and
treated as a file name, mostly to protect users.
.-* and ..-* are also considered invalid because they might strip
on that separator to remove versions. Doesn't really work, but that's
what we decided, and I won't argue with it, because .-* probably
doesn't seem to have a real world application anyway.
We do still permit a 1-character name that's just "-", which still
poses a similar risk in such a situation. We can't start disallowing
trailing -, because a non-zero number of users will need it and we've
seen how annoying and painful such a change is.

What matters most is preventing a situation where . or .. can be
injected, and to just get this done.
2024-03-26 13:47:41 +01:00
Robert Hensing
80ea2dcc25 test: Generate distinct hashes
Gen::just is the constant generator. Don't just return that!
2024-03-26 13:47:41 +01:00
Robert Hensing
51332020c0 test: Generate distinct path names
Gen::just is the constant generator. Don't just return that!
2024-03-26 10:20:43 +00:00
Robert Hensing
752e8e4acd parseStorePath: Support leading period 2024-03-26 10:20:43 +00:00
Robert Hensing
379274fd7f Revert "StorePath: reject names starting with '.'"
This reverts commit 24bda0c7b3.
2024-03-26 10:20:43 +00:00
Eelco Dolstra
d137077271 Bump version 2024-03-07 15:03:40 +01:00
Eelco Dolstra
0bd62229cd Bump version 2024-03-07 13:10:53 +01:00
Eelco Dolstra
17a598e644
Merge pull request from GHSA-2ffj-w4mj-pg37
Sandbox escape 2.18
2024-03-07 11:56:24 +01:00
Tom Bereknyei
9e7065bef5 Add release notes
Co-authored-by: Theophane Hufschmitt <theophane.hufschmitt@tweag.io>
2024-03-07 09:33:44 +01:00
Tom Bereknyei
4bc5a3510f Copy the output of fixed-output derivations before registering them
It is possible to exfiltrate a file descriptor out of the build sandbox
of FODs, and use it to modify the store path after it has been
registered.
To avoid that issue, don't register the output of the build, but a copy
of it (that will be free of any leaked file descriptor).

Co-authored-by: Theophane Hufschmitt <theophane.hufschmitt@tweag.io>
Co-authored-by: Valentin Gagarin <valentin.gagarin@tweag.io>
2024-03-07 09:33:44 +01:00
Tom Bereknyei
f8d20e91a4 Add a NixOS test for the sandbox escape
Test that we can't leverage abstract unix domain sockets to leak file
descriptors out of the sandbox and modify the path after it has been
registered.

Co-authored-by: Theophane Hufschmitt <theophane.hufschmitt@tweag.io>
2024-03-07 09:33:40 +01:00
tomberek
ec177b98f3
Merge pull request #10170 from johnrichardrinehart/jrinehart/2_18-faster-flake-lock-parsing
2.18 faster flake lock parsing
2024-03-07 00:11:54 -05:00
Graham Dennis
7b237ebe5c Faster flake.lock parsing
This PR reduces the creation of short-lived basic_json objects while
parsing flake.lock files. For large flake.lock files (~1.5MB) I was
observing ~60s being spent for trivial nix build operations while
after this change it is now taking ~1.6s.

(cherry picked from commit 7fd0de38c6)
2024-03-06 17:57:15 -08:00
github-actions[bot]
afb55f36df
Fix bad_format_string error when builder stdout contains % (#10063)
(cherry picked from commit e2b6821ca0)

Co-authored-by: roblabla <unfiltered@roblab.la>
2024-02-22 23:12:29 +01:00
Eelco Dolstra
2a6a7aad3f
Merge pull request #10018 from NixOS/fix-18-build
[2.18] Fix the build
2024-02-15 11:00:13 +01:00
Théophane Hufschmitt
f36d4aefd3 Fix the build
bef68e53b9 (backport of
31ebc6028b) accidentally broke the build
because of a change in the constructor of `SourcePath` between 2.18 and
master. Fix that.
2024-02-15 09:50:12 +01:00
github-actions[bot]
2db5c5326b
fix location of _redirects file (#9956) 2024-02-07 11:44:06 +01:00
Eelco Dolstra
5f7f68e0a8
Merge pull request #9827 from NixOS/backport-9363-to-2.18-maintenance
[Backport 2.18-maintenance] Fix symlink handling
2024-01-22 14:30:17 +01:00
Eelco Dolstra
bef68e53b9 Fix symlink handling
This restores the symlink handling behaviour prior to
94812cca98.

Fixes #9298.

(cherry picked from commit 31ebc6028b)
2024-01-21 20:53:30 +00:00
John Ericson
60eb80593f
Merge pull request #9738 from shlevy/ifd-buildStore-2.18
(Backport #9661) Build IFD in the build store when using eval-store.
2024-01-11 10:06:20 -05:00
Shea Levy
ba48ab4b95
Merge branch '2.18-maintenance' into ifd-buildStore-2.18 2024-01-11 07:21:16 -05:00
Shea Levy
e7c2b35827
Build IFD in the build store when using eval-store.
Previously, IFDs would be built within the eval store, even though one
is typically using `--eval-store` precisely to *avoid* local builds.

Because the resulting Nix expression must be copied back to the eval
store in order to be imported, this requires the eval store to trust
the build store's signatures.

(cherry picked from commit c3942ef85f)
2024-01-11 06:34:27 -05:00
Shea Levy
be208d8e78
remote-store test: Break out IFD expression into a separate file
(cherry picked from commit 9cb287657b)
2024-01-11 06:30:02 -05:00
github-actions[bot]
4dd5171652
fix an old lost direct (#9717)
this part must have been moved quite a while ago, but apparently so far
no one noticed

(cherry picked from commit 6db805b3d1)

Co-authored-by: Valentin Gagarin <valentin.gagarin@tweag.io>
2024-01-08 20:57:39 +01:00
John Ericson
4a1c3762df
Merge pull request #9543 from NixOS/backport-9542-to-2.18-maintenance
[Backport 2.18-maintenance] Add missing `-pthread` for test support libraries
2023-12-05 19:26:18 -05:00
John Ericson
615bd655e5 Add missing -pthread for test support libraries
This is good in general (see how the other libraries also have long had
it, since 49fe9592a4) but in particular
needed to fix the NetBSD build.

(cherry picked from commit b23273f6a2)
2023-12-05 23:44:41 +00:00
John Ericson
211b6e1855
Merge pull request #9517 from NixOS/2.18-flatten-tests
[Backport 2.18-maintanence] Backport test source layout reorgs
2023-12-01 13:53:13 -05:00
John Ericson
f7f37035c8 Move tests to separate directories, and document
Today, with the tests inside a `tests` intermingled with the
corresponding library's source code, we have a few problems:

- We have to be careful that wildcards don't end up with tests being
  built as part of Nix proper, or test headers being installed as part
  of Nix proper.

- Tests in libraries but not executables is not right:

  - It means each executable runs the previous unit tests again, because
    it needs the libraries.

  - It doesn't work right on Windows, which doesn't want you to load a
    DLL just for the side global variable . It could be made to work
    with the dlopen equivalent, but that's gross!

This reorg solves these problems.

There is a remaining problem which is that sibbling headers (like
`hash.hh` the test header vs `hash.hh` the main `libnixutil` header) end
up shadowing each other. This PR doesn't solve that. That is left as
future work for a future PR.

Co-authored-by: Valentin Gagarin <valentin.gagarin@tweag.io>

(cherry picked from commit 91b6833686)
(cherry picked from commit a61e42adb5)
2023-12-01 13:05:03 -05:00
John Ericson
30dcc19d1f Put functional tests in tests/functional
I think it is bad for these reasons when `tests/` contains a mix of
functional and integration tests

 - Concepts is harder to understand, the documentation makes a good
   unit vs functional vs integration distinction, but when the
   integration tests are just two subdirs within `tests/` this is not
   clear.

 - Source filtering in the `flake.nix` is more complex. We need to
   filter out some of the dirs from `tests/`, rather than simply pick
   the dirs we want and take all of them. This is a good sign the
   structure of what we are trying to do is not matching the structure
   of the files.

With this change we have a clean:
```shell-session
$ git show 'HEAD:tests'
tree HEAD:tests

functional/
installer/
nixos/
```

(cherry picked from commit 68c81c7375)
2023-12-01 12:06:43 -05:00
John Ericson
7242521265
Merge pull request #9514 from NixOS/2.18-nixpkgs-bump
[Backport 2.18-maintainence] nixpkgs bump
2023-12-01 11:30:05 -05:00
Robert Hensing
016f936df6 flake.lock: Update
Flake lock file updates:

• Updated input 'nixpkgs':
    'github:NixOS/nixpkgs/31ed632c692e6a36cfc18083b88ece892f863ed4' (2023-09-21)
  → 'github:NixOS/nixpkgs/9ba29e2346bc542e9909d1021e8fd7d4b3f64db0' (2023-11-23)

(cherry picked from commit fe4f573d49)
(cherry picked from commit f01baf5f06
2023-12-01 11:08:15 -05:00
Robert Hensing
083b198bb7
Merge pull request #9471 from NixOS/backport-9459-to-2.18-maintenance
[Backport 2.18-maintenance] add path based redirects
2023-11-27 15:15:52 +01:00
Valentin Gagarin
8fb477a6e7 add path based redirects
up to now, those were managed outside of this repo, which as
unsurprisingly a real hassle to deal with if one wanted to prevent URLs
from breaking when moving pages around. this change removes a large part
of the friction involved in moving content in the Nix manual.

possible next steps for further automation:
- check for content that moved and warn if it's not reachable from
  links that were valid prior to a change
- create redirect rules automatically based on this information

(cherry picked from commit 2b7016cc56)
2023-11-27 13:29:00 +00:00
John Ericson
05b7e2dc57
Merge pull request #9436 from NixOS/backport-9431-to-2.18-maintenance
[Backport 2.18-maintenance] Add missing `-lrapidcheck` fixing build with shared lib
2023-11-22 09:35:24 -05:00
John Ericson
89a66633b7 Add missing -lrapidcheck fixing build with shared lib
https://github.com/NixOS/nixpkgs/pull/269064 makes rapidcheck be build
as a shared lib, but that broke Nix because the `-lrapidcheck` was
missing. This fixes that (and doesn't break Nix what the library is a
static archive as today).

(cherry picked from commit 46131567da)
2023-11-22 04:48:11 +00:00
Théophane Hufschmitt
fadad86276
Merge pull request #9406 from NixOS/backport-9390-to-2.18-maintenance
[Backport 2.18-maintenance] fetchTree: clarify docs for shallow flag
2023-11-20 15:38:25 +01:00
DavHau
23775b7364 fetchTree: clarify docs for shallow flag
(cherry picked from commit 796a7eb92d)
2023-11-20 14:16:03 +00:00
Robert Hensing
8e72a529da
Merge pull request #9366 from NixOS/backport-9187-to-2.18-maintenance
[2.18-maintenance] Backport #9187 apple-virt
2023-11-16 19:44:32 +01:00
Robert Hensing
af21431140 libstore: Add apple-virt to system features when available
I'm sure that we'll adjust the implementation over time, but this
at least discerns between an apple silicon bare metal machine and
a tart VM.

(cherry picked from commit 9277eb276b)
2023-11-16 18:59:06 +01:00
Eelco Dolstra
184a20ec04
Merge pull request #9334 from fricklerhandwerk/backport-2.18-fix-anchors
backport fix for the `--help` output to 2.18
2023-11-16 15:02:18 +01:00
Valentin Gagarin
a68bf15fe6 backport fix for the --help output 2023-11-10 19:22:42 +01:00
John Ericson
78fd621397
Merge pull request #9122 from NixOS/backport-9095-to-2.18-maintenance
[Backport 2.18-maintenance] StorePath: reject names starting with '.'
2023-10-09 10:27:51 -04:00