Commit Graph
18095 Commits
Author SHA1 Message Date
Raito Bezarius 33122e79df libstore: weaken the top-level fallback temp dir to 0755 for macOS
Under macOS, the first level of directory has actually mode 0755 instead
of 0700 as macOS often do not possess the right primitives to chroot
inside of these directories, leading to
https://github.com/NixOS/nix/pull/11031.

Thanks to Emily for the heads-up on this type of matter.

Change-Id: I9d4e53717f61c9d573ff176f820610612804fbc3
Signed-off-by: Raito Bezarius <raito@lix.systems>
2025-06-28 17:37:43 +02:00
eldritch horrors ac80a11300 packaging: unbreak static builds
Change-Id: I84dbf66d2d4116c531384445a108d1eab7752ffb
2025-06-27 22:53:28 +02:00
Raito Bezarius fd35e86fc5 libstore: fallback on creating a safe space in the default tempdir
If `settings.buildDir` cannot be written to, because we are in a chroot
store, unprivileged or anything.

We can and should always gracefully fallback to a *secure* location
inside of /tmp, i.e. `/tmp/<a directory under 0700>/<our temporary
directory for build under 0700>/...`.

This does not reintroduce CVE-2025-52991 because we are creating a
directory in-between compared to creating only ONE level of directory.

Fixes #876.

Change-Id: Ie521202923f763225e1901ab1b9b6c6132aaf548
Signed-off-by: Raito Bezarius <raito@lix.systems>
2025-06-27 17:39:59 +00:00
eldritch horrors e356d54d7a libstore: don't delete already valid outputs after build
eagerly consider outputs as not needing deletion during output
registration rather than only doing so after registration. not
waiting for registration to succeed may keep store paths alive
in the file system if registration fails for some reason; that
seem preferrable to the possibility of having another instance
of this bug. since we only leave *good* outputs around there's
not much to worry about except maybe bit of wasted disk space.

fixes #883

Change-Id: I8c22c92e39b9e203f1061278f86cde19dc4474a4
2025-06-27 15:38:53 +02:00
eldritch horrors a0a00948df libstore: fix scratch output cleanup
the daemon must use real store paths, not virtual store paths. using
virtual paths may inadvertently delete paths in the system nix store
when a build was run on a redirected store as root, which isn't good

Change-Id: Id048b236bda0e0ab1f3be6ccba0ddc1de2a3e941
2025-06-27 15:38:53 +02:00
Wolfgang Walther 34696c65a2 libstore: fix race condition when creating state directories
Running (parallel?) nix in nix can lead to multiple instances trying
to create the state directories and failing on the createSymlink step,
because the link already exists.

`replaceSymlink` is already idempotent, so let's use that.

See also:
- https://github.com/NixOS/nix/pull/13368
- https://github.com/NixOS/nix/issues/2706

Change-Id: I7fadd0ce3c1ffcebc9d281c00e5b49c12af3d50b
2025-06-25 16:01:51 +00:00
Raito Bezariusandeldritch horrors 9a59106c17 libstore: NIX_REMOTE=auto tries the daemon socket *then* direct access
In the past, it tried direct access if it *could* [1] perform direct
access.

This solves a bunch of errors people had when they tried the cgroup
feature and their scripts did not pass NIX_REMOTE=daemon manually
(nixos-rebuild-ng, home-manager activation from a root systemd unit,
etc.)

To avoid looping infinitely while receiving daemon connections, we
forcibly change the store URI when forking for a subdaemon to do direct
access automatically, this doesn't break forward usecases where you
point a daemon to another socket because we only change NIX_REMOTE="",
NIX_REMOTE=daemon, NIX_REMOTE=auto to a local and direct access.

All these usecases would end up infinitely looping no matter what
settings are set, because we are also responsible for creating the
daemon socket.

[1]: this happened all the time if you were `root`.

Related: https://github.com/NixOS/nixpkgs/pull/415701
Change-Id: I783fc795a9c2ee25b3d9f44f453f8f94b063371f
Signed-off-by: Raito Bezarius <raito@lix.systems>
2025-06-25 14:59:58 +00:00
eldritch horrors e3caf98a8f libstore: wait for cgroups to die on cleanup
killing a cgroup via `cgroup.kill` is not synchronous, we need to give
the processes in the group some time to wake up and exit. due to a few
historical accidents in the codebase we cannot do this asycnhronously,
e.g. with a kj promise without creating yet more problems. we will, at
some point in the future, have to move cgroup management into the main
daemon rather than doing it with RAII wrappers within every subdaemon.

Change-Id: I03bf9060144b5737729f2b05c25771c674fd154c
2025-06-25 14:59:58 +00:00
Jade Lovelace 276add2cd7 repl: fix repl-overlays in pure eval mode
The reason this gets hit is because of the debugger in flakes. Otherwise
you never have a repl in pure mode anyway.

We evaluate the repl-overlay file in impure mode but this doesn't do
what one would initially expect.

Fixes: https://git.lix.systems/lix-project/lix/issues/777
Change-Id: I19b8ed2f5e9ce500b633b13301b42df69ab7deb3
2025-06-25 14:15:37 +00:00
Jade Lovelace 38850e59e1 repl-characterization: delete duplicate extra_data directory
idk how this mistake happened but it was really confusing to figure out
which one of these was right, so let's get rid of the impostor.

Change-Id: If3b6fb543e5976b1edad68fb143bfa994d1d6381
2025-06-24 22:20:28 +00:00
Raito Bezarius 42e2bd045c libstore/build: automatic clean up of unsuccessfully built scratch outputs
When a build fails, its scratch output paths are not cleaned up.

Until recently, this was deemed not a problem but as part of the effort
to harden the Nix builds and protect these paths against being part of a
staged attack (race conditions, etc.), we automatically cleanup after
failed builds.

Fixes CVE-2025-52992.

Change-Id: I58481b1cc83826298b9d80d37fecf81f117ccb09
Signed-off-by: Raito Bezarius <raito@lix.systems>
2025-06-24 10:49:49 +00:00
eldritch horrorsandRaito Bezarius 749afbbe99 libstore: don't default build-dir to temp-dir
if a build directory is accessible to other users it is possible to
smuggle data in and out of build directories. usually this ins only
a build purity problem, but in combination with other issues it can
be used to break out of a build sandbox. to prevent this we default
to using a subdirectory of nixStateDir (which is more restrictive).

Fixes CVE-2025-52991.

Change-Id: Iacfc9b50534de158618c815f9fb99d7dae1be4d0
2025-06-24 10:49:49 +00:00
a959290f41 libstore: use pasta for FODs if available
This allows using a userspace program, pasta, to handle comms between
the build sandbox, and the outside world; allowing for full isolation
including the network namespace, closing the "fixed-output derivation
talks to the host over an abstract domain socket" hole for good.

Fixes CVE-2025-46416.

Co-Authored-By: Puck Meerburg <puck@puckipedia.com>
Change-Id: Ifd499b7dbb3784600a6e842fede65fc031ff9f15
2025-06-24 10:49:49 +00:00
eldritch horrorsandRaito Bezarius e6b9f714ea libutil: add capability support to runProgram2
launching pasta to not run as root will ambient require capabilities.

Change-Id: I1dd2506a1fa3944a9d9062123ef8a74903c597ea
2025-06-24 10:49:49 +00:00
eldritch horrorsandRaito Bezarius 6f2b810b4a libutil: add generic redirections runProgram2
explicit stderr redirection makes mergeStderrToStdout unnecessary also.

Change-Id: I63de929e6dc53f6c5ceb2d43c2ce288bfc04d872
2025-06-24 10:49:49 +00:00
eldritch horrorsandRaito Bezarius 19930d423d libutil: make RunningProgram more useful
make it moveable, make it killable, and add a stdout fd accessor.

Change-Id: I2387cbe8ac67b899a322cd6c7d306ef9ea7abcd0
2025-06-24 10:49:49 +00:00
Raito Bezarius 11c5e3bbcc libutil: ensure that _deletePath does NOT use absolute paths with dirfds
When calling `_deletePath` with a parent file descriptor, `openat` is
made effective by using relative paths to the directory file descriptor.

To avoid the problem, the signature is changed to resist misuse with an
assert in the prologue of the function.

Fixes CVE-2025-46415.

Change-Id: I6b3fc766bad2afe54dc27d47d1df3873e188de96
Signed-off-by: Raito Bezarius <raito@lix.systems>
2025-06-24 10:49:49 +00:00
Raito Bezarius dceb9438d2 libstore: ensure that passAsFile is created in the original temp dir
This ensures that `passAsFile` data is created inside the expected
temporary build directory by `openat()` from the parent directory file
descriptor.

Fixes CVE-2025-52993.

Change-Id: Ie5273446c4a19403088d0389ae8e3f473af8879a
Signed-off-by: Raito Bezarius <raito@lix.systems>
2025-06-24 10:46:02 +00:00
Raito Bezariusandeldritch horrors 2d836357dc libutil: writeFile variant for file descriptors
`writeFile` lose its `sync` boolean flag to make things simpler.

A new `writeFileAndSync` function is created and all call sites are
converted to it.

Change-Id: Ib871a5283a9c047db1e4fe48a241506e4aab9192
Signed-off-by: Raito Bezarius <raito@lix.systems>
2025-06-24 00:28:09 +02:00
Raito Bezarius 10509774ed libstore: chown to builder variant for file descriptors
We use it immediately for the build temporary directory.

Change-Id: I180193c63a2b98721f5fb8e542c4e39c099bb947
Signed-off-by: Raito Bezarius <raito@lix.systems>
2025-06-23 16:57:23 +02:00
Raito Bezarius bcf1f27fec libstore: open build directory as a dirfd as well
We now keep around a proper AutoCloseFD around the temporary directory
which we plan to use for openat operations and avoiding the build
directory being swapped out while we are doing something else.

Change-Id: I18d387b0f123ebf2d20c6405cd47ebadc5505f2a
Signed-off-by: Raito Bezarius <raito@lix.systems>
2025-06-23 16:57:23 +02:00
Raito Bezarius c7976e63a3 libutil: guess or invent a path from file descriptors
This is useful for certain error recovery paths (no pun intended) that
does not thread through the original path name.

Change-Id: I2d800740cb4f9912e64c923120d3f977c58ccb7e
Signed-off-by: Raito Bezarius <raito@lix.systems>
2025-06-23 16:57:23 +02:00
Raito BezariusandMaximilian Bosch cd129186ea libstore/s3: fulfill with PutObjectOutcome instead of HeadObjectOutcome
This was probably a typo introduced in
7453e2979f.

Unfortunately, AWS SDK is so well made that this typo became an assert
error in production.

AWS Outcome constructors contains
```
            // Move error from other type of outcome
            template<typename RT, typename ET,
enable_if_t<!std::is_convertible<RT, R>::value &&

std::is_convertible<ET, E>::value, int> = 0>
```

which means that when:

* RT → R is not possible (e.g. PutObjectOutcome → HeadObjectOutcome)
* ET → E is possible (e.g. S3Error → S3Error)

Then, we will instantiate the error-moving outcome constructor which
asserts `!o.success`… Though, the original outcome indeed succeeded.

Change-Id: I3809514ae0648e8c02b0f93fa64d91115a091cd9
Co-authored-by: Maximilian Bosch <maximilian@mbosch.me>
Signed-off-by: Raito Bezarius <raito@lix.systems>
2025-06-22 19:45:53 +00:00
Teo Camarasu 2172683388 libstore: better error message when remote version is too old
We clarify that the *remote* daemon is too old. Otherwise it can be a bit confusing since you might have a local daemon as well, and it's not clear if the error is coming from the local or remote end

Change-Id: I17344c6f59bd7e0e62960c0025184d72ec3f012b
2025-06-22 15:40:52 +00:00
Maximilian Bosch 242a228124 libutil: close file handle in async NAR parser
This bit us while upgrading Hydra[1]: when all the data was read into
the hashing sink while receinving NAR contents, the hash was never
created which lead to a test failing because file size was correct, but
the hash was std::nullopt.

[1] https://git.lix.systems/lix-project/hydra/src/commit/7a0dae579b53b4b96a829263b160c6dc9f42ce75/src/hydra-queue-runner/nar-extractor.cc#L70-L73

Change-Id: Ie71b5f1f17c926a2ab95fb2aabf23c7a575ff70b
2025-06-21 13:52:43 +02:00
Maximilian Bosch 3a6414760e libcmd: error if first argument for --arg/--argstr isn't an a valid identifier
Step two for #496.

The idea is to allow `nix-build --arg config.allowUnfree true` do the
right thing in the future. However, that's a breaking change since
people might be relying on the ability to set `"config.allowUnfree"` as
attribute-name when auto-calling a Nix-expression.

As a first step, a warning got introduced in 2.92, the next step is now
to reject this usage in 2.94 and await feedback if any so that we can do
the change in a future Lix release.

Change-Id: I6e38fafe26e234204f5bba2a3a4c1da10f80e5f2
2025-06-20 22:40:14 +02:00
Maximilian Bosch e23bed5e64 libutil: expose the functionality of Lix's exception handler
This introduces three new things:

* `handleException` which prints out exception details and its stack
  trace.
* `handleExceptionWithAsyncTrace` which does the same, but also prints
  the async trace if any.
* `LIX_BLOCK_ON` which is awaits a promise and adds an exception trace
  if an exception got thrown, similar to `LIX_TRY_AWAIT`. However, this
  is not supposed to be used in async functions, but on callsites of
  `aio.blockOn()` which is especially useful for Hydra[1].

For `LIX_BLOCK_ON` I had to introduce another function because there's
apparently no way to implement all of it in a macro: on macros with
compound statements the return value must be a trivial expression at the
bottom, i.e. no `try`/`catch`. Now, returning the value from the
`try`-block requires the variable to be defined up-front, but for that
we'd need to know the type-name. Hence the construction with a
template-function being invoked by a macro that injects the current
source-location.

[1] https://git.lix.systems/lix-project/hydra/pulls/52

Change-Id: I56cc92c94f7e8f0be5d4dc5a7d8cb21a92e776ef
2025-06-20 18:14:38 +02:00
Commentator2.0 35c3bfdacb tests/functional2/lang: require all files to be used
Added an additional check that all files present within a folder must be
used/referenced. Otherwise an InvalidLangTest will be created.
This ensures that there weren't any mishaps while migrating tests
resulting in files being ignored and hence some tests not being run.

Fixes: #852

Change-Id: Ie096c5670bc20325ba72c7d6ce33c06667c66ab1
2025-06-20 11:36:13 +02:00
Commentator2.0 d1afc83676 tests/functional2/lang: improve toml design
Redesigns the test.toml to use a list instead of a directory
additionally it is now possible to do toml and matrix tests on singular
files as well as on a subset of files.

Fixes: #851

Change-Id: If8635109c6274f406ad68fe35315b9125f45f67d
2025-06-20 11:36:13 +02:00
Commentator2.0 f2eb920e46 tests/functional2/lang: improve assertion failure message
Currently when a lang test fails, (or any snapshot assertion for that
matter) the error message is rather bulky.
This is due to both sides being printed fully, using escaped newlines
(i.e. everything is one line)

This is awful to read and check what the actual difference is. Also
there is no indication that one can update the golden files using the
cli flag.

This commit changes the error message when comparing snapshots against
something
a list of lines is shown, where the output differed. An additional note
about how to update the files automatically was added too

Change-Id: Ibedcf48018c27f924b807fbd42362fb608d27441
2025-06-20 11:26:03 +02:00
eldritch horrors 877b0d7121 libstore: asyncify Store::queryMissing
we no longer use thread pools for querying missing derivations. this
binds queryMissing to a single thread for now, but query performance
is still greatly improved. we may want to optimize the store code in
the near future too though since queryMissing is now fully cpu bound

Change-Id: I08a9c8cc199963ef5981572ca4a32d90dbdec028
2025-06-19 14:59:38 +00:00
eldritch horrors 2bfea5eefe libstore: use async streams in LegacySSHStore
this mirrors what have already done to the more modern wires.

Change-Id: I68b65bb400c889ba822386a9c280297c9ff4f740
2025-06-19 14:59:38 +00:00
eldritch horrors 02f61e7759 libstore: asyncify legacy ssh command/response
we intentionally omit writers for the new types we add for serialization
purposes since we do not plan to asyncify the legacy ssh server side. if
we ever change our mind we can extract these types into a header and add
writers as needed. due to the inevitable network overhead of the old ssh
wires we don't bother to optimize serialization too much and instead opt
to make the code more readable; the performance difference does not show
up in practice since network latency dominates the few nanoseconds spent
on extra promise allocations and awaits by a couple orders of magnitude.

Change-Id: Id3ee9a01f8bfa63fa23082fa07de5c673fd70883
2025-06-19 14:59:38 +00:00
eldritch horrors aae67feb19 libstore: make legacy ssh build settings a generator
that'll make sendCommand-ing the legacy protocol much easier.

Change-Id: I3193b306ab28c203fe50c404a15c45cf598ca7e7
2025-06-19 14:59:38 +00:00
eldritch horrors 508f476c18 libstore: don't crash when talking to old ssh:// remotes
protocol version 0x204 dates back to nix 2.0 in 2017. that's old enough
to not worry and drop the gratuitous assertion crash we see it instead.

Change-Id: I8cf23373d4daabccab61f1cbb670947479f0d2bc
2025-06-19 14:59:38 +00:00
Lily Ballard 20fed838a6 libcmd: replace @docroot@ when rendering markdown
Also replace links to `.md` files with the equivalent `.html` files.

Change-Id: Id0000000f267872d021985daf2d833a93ec06e66
2025-06-18 00:42:36 -07:00
Raito Bezarius 87d99da6ca libutil/cgroup: ensure that cleanup takes place even under interruptions
When Ctrl-C is sent to the workload, even across remote builds, the
whole process possess a global flag `_isInterrupted` which is checked in
certain filesystem operations, cancelling them, e.g. writeFile will
write nothing under interruption unwinding.

In addition, if any operation throws an exception before we `rmdir` the
cgroup, we may leave it hanging while we remove the state record.
Therefore, we put the final cleanup in a block.

In practice, reading statistics could lead to failures.

Control groups cleanups are critical though and should always be
performed.

Change-Id: I48fa87317b6a9f6663559bc8fa5f8a897f37011e
Signed-off-by: Raito Bezarius <raito@lix.systems>
2025-06-17 22:20:37 +02:00
eldritch horrors c3bc0d35dd libstore: use async streams in RemoteStore
this is a large step towards making RemoteStore a proper capnp rpc
interface, and it lets us get rid of the RemoteStore error handler
thread pool. this does mean we make six or more extra syscalls per
operation to set and clear socket non-blocking flags, but they are
pretty cheap compared to cross-thread wakeups and scheduling. once
we have real capnp rpc for store wires we can drop them again too.

Change-Id: I67dfebc8644a407cd4a8221ffcad02a938ac5abe
2025-06-17 15:25:32 +02:00
eldritch horrors 3f62905312 libstore: instantiate RemoteStore FdSources as needed
in the future we will want to instantiate either a sink, a source, both,
or streams, depending on how the fd is used. to do this we need to share
read buffers among sync and async readers. removing the FdSource we kept
in the connection also helps prove that we always use this buffer for io

Change-Id: Ib678e128ed6c4a07d6ce5ec1d3cde9eb3f5fc4ca
2025-06-17 14:34:05 +02:00
eldritch horrors 687ea19e6f libstore: drop pervasive RemoteStore send buffering
we don't need to double-buffer commands. only the subframe protocol
needs a buffered backing, and connection setup is special *anyway*.

Change-Id: I596f2bf8e297c3c5dc2befae674deafcf559d9a9
2025-06-17 14:34:05 +02:00
eldritch horrors b0edb262b2 libutil: add bidirectional async fd streams
this may as well be called AsyncSocketStream since that will be what we
use it for, but hopefully it will not exist for long enough to need any
other socket functions to actually justify such highly specific naming.

Change-Id: Icf2fe88cf345405218e4b1bd440267e7f132f5c7
2025-06-17 14:34:05 +02:00
eldritch horrors 7b65d7c508 libutil: add buffered async streams
these will let us share async stream io buffers with sync sinks and sources.

Change-Id: If3149803a9e1fda62391399177da62f7522a811b
2025-06-17 14:34:05 +02:00
eldritch horrors 81d2d26c3f libutil: add async output stream type
we also extend AsyncInputStream with a drainInto variant to give async
output streams rough feature parity with sync sinks. we still will not
add serialization support to streams though, that's far too expensive.

Change-Id: I60d5ab43610c45a40ea8740470a5eafe68064aea
2025-06-17 14:34:05 +02:00
eldritch horrors fa116c96f7 perl: ensure that stores are destroyed after aio roots
otherwise stores containing async objects will cause crashes during
shutdown. currently there are no such stores, but that will change.

Change-Id: I05d46ba6831c641774edfe6aa99aa7d0de457429
2025-06-17 14:34:05 +02:00
eldritch horrors bc33c21b8a cli: remove static destruction from nix-store
store objects may hold on to network connections. if those connections
are async they're bound to the lifetime of the aio runtime, which ends
long before the static object destructors we need for nix-store today.

Change-Id: I4aa5466681a82f7e5008cc0b952fcba01d5b39d7
2025-06-17 14:34:05 +02:00
eldritch horrors 49e6147f95 libutil: remove unused Source::good
Change-Id: I8dcb725578e27415b60a01a16c10720e96a5371b
2025-06-17 14:34:05 +02:00
eldritch horrors e5c4de34c5 libstore: eagerly mark daemon connections as bad on local errors
do not rely on Source/Sink `good()` or delayed guessing about whether
an exception was thrown by the daemon or not. mark connections as bad
for all local errors happening while communication is ongoing instead,
and leave it valid only when an exception was provided by the remote.

we may drop connections a bit too eagerly now, but all cases in which
that happens were vulnerable to protocol desynchronization. there are
still a few windows for this to happen left, but those are unfixable.

Change-Id: Iefaa66c552092c436b9de77aa3f8e09f847a966e
2025-06-17 14:34:05 +02:00
eldritch horrors 37c17804df libstore: serialize wire messages into temp buffer
once we make our socket fds non-blocking we won't be able to easily use
plain FdSink for serialization. performance impact of using a temporary
buffer should be low since we don't send very many messages and even in
the simple local daemon case networking overhead is already quite high.

Change-Id: I550d73142570b7d2e7b0feb1bcc57d61e9b45178
2025-06-17 14:34:05 +02:00
eldritch horrors 6f64e1b133 libutil: make Fd{Sink,Source} io buffer shareable
we will need this during RemoteStore wire asyncification to be able to
use the old synchronous serializers. alternatively we could define all
serializers on the async types as well, but that'd be slow and far too
much unnecessarily duplicated code (that will be deleted soon anyway).

Change-Id: I6e4f334025844b808a697ddcd8f80ddcd8c3fc9c
2025-06-17 14:34:05 +02:00
eldritch horrors fc18a6d170 libutil: disallow Fd{Sink,Source} copy and move
it was never safe. both discarded the buffer of the source object,
possibly leading to silent data corruption. FdSource discarded the
fancy EOF error string as well, possibly causing bad error reports

Change-Id: Ib5c07986471b5af03d707230cd487259201952e9
2025-06-17 14:34:05 +02:00