Compare commits

...
Author SHA1 Message Date
Théophane Hufschmitt d4c6722332 Add a test for CVE-2022-24765
NixOS VM test since handling different users without that is bound to be
tricky
2023-06-21 17:11:13 +02:00
Théophane Hufschmitt e5a211d77e Add an owner check when searching for the flake root
When searching up the filesystem for the root of the flake (the
directory that contains the `flake.nix`), don’t go anywhere up if we
encounter a directory owned by a different user, as otherwise this other
user could craft an arbitrary flake, potentially causing bad stuff to
happen (shouldn’t in most cases since all it could do is run sanboxed
builds, but there’s probably a lot of edge-cases that would make this
very undesirable).

This is to fix Nix’s equivalent of CVE-2022-24765

This check is intentionnally not applied to the exact directory
specified since:
1. It’s up to the user to not point to an untrusted input
2. In multi-user Nix installations, that would prevent from using a
   flake in the Nix store (since it’s owned by root and not the current
   user)

Fix #6408
2023-06-19 15:01:12 +02:00
4 changed files with 77 additions and 5 deletions

No files matched your search

+3 -1
View File
@@ -1,3 +1,5 @@
# Release X.Y (202?-??-??)
- [`nix-channel`](../command-ref/nix-channel.md) now supports a `--list-generations` subcommand
* When searching upwards for the root of the flake, Nix doesn’t consider
anything that’s not owned by the current user anymore as it’s a
security hazard.
+2
View File
@@ -598,6 +598,8 @@
["i686-linux" "x86_64-linux"]
(system: runNixOSTestFor system ./tests/nixos/setuid.nix);
tests.cve-2022-24765 = runNixOSTestFor "x86_64-linux" ./tests/nixos/cve-2022-24765.nix;
# Make sure that nix-env still produces the exact same result
# on a particular version of Nixpkgs.
+8 -4
View File
@@ -127,6 +127,8 @@ std::pair<FlakeRef, std::string> parseFlakeRefWithFragment(
// Save device to detect filesystem boundary
dev_t device = lstat(path).st_dev;
// Fix for the Nix equivalent of CVE-2022-24765
uid_t currentUser = geteuid();
bool found = false;
while (path != "/") {
if (pathExists(path + "/flake.nix")) {
@@ -134,11 +136,13 @@ std::pair<FlakeRef, std::string> parseFlakeRefWithFragment(
break;
} else if (pathExists(path + "/.git"))
throw Error("path '%s' is not part of a flake (neither it nor its parent directories contain a 'flake.nix' file)", path);
else {
if (lstat(path).st_dev != device)
throw Error("unable to find a flake before encountering filesystem boundary at '%s'", path);
}
path = dirOf(path);
auto pathStat = lstat(path);
if (pathStat.st_dev != device)
throw Error("unable to find a flake before encountering filesystem boundary at '%s'", path);
if (pathStat.st_uid != currentUser) {
throw Error("unable to find a flake before encountering a directory not owned by us at '%s'", path);
}
}
if (!found)
throw BadURL("could not find a flake.nix file");
+64
View File
@@ -0,0 +1,64 @@
{ lib, config, nixpkgs, ... }:
let
pkgs = config.nodes.machine.nixpkgs.pkgs;
flakeDotNix = pkgs.writeTextFile {
name = "flake.nix";
destination = "/flake.nix";
text = ''
{
outputs = { self }: { foo = 1; };
}
'';
};
in
{
name = "cve-2022-24765";
nodes.machine =
{ config, lib, pkgs, ... }:
{
virtualisation.writableStore = true;
virtualisation.additionalPaths = [ flakeDotNix ];
nix.settings.experimental-features = [ "nix-command" "flakes" ];
users.users.user1.isNormalUser = true;
users.users.user2.isNormalUser = true;
};
testScript = { nodes }: ''
# fmt: off
start_all()
# Evaluating a flake in the store. Should just work
machine.succeed('nix eval ${flakeDotNix}#foo')
# Create the following hierarchy:
#
# /flakeRoot (owned by `user1`)
# /flake.nix (owned by `user1`)
# /subdir (owned by `user2`)
#
machine.execute(r"""
mkdir -p /flakeRoot/subdir
chown -R user1 /flakeRoot
cp -r ${flakeDotNix}/flake.nix /flakeRoot
chown -R user2 /flakeRoot/subdir
""".strip())
# Evaluating `/flakeRoot#foo` should work even if we're not `user1` since
# we've specified the exact path
machine.succeed("su user1 -c 'nix eval /flakeRoot\#foo'")
machine.succeed("su user2 -c 'nix eval /flakeRoot\#foo'")
# But when we have to resort to searching upwards in the tree, then Nix
# should error out if we're not `user1` since the `flake.nix` isn't owned
# by us
machine.succeed("su user1 -c 'nix eval /flakeRoot/subdir\#foo'")
inSubdirStdout = machine.fail(
"su user2 -c 'nix eval /flakeRoot/subdir\#foo' 2>&1"
)
print(inSubdirStdout)
assert "owned by us" in inSubdirStdout
'';
}