lix/src
Alyssa Ross c05e20daa1
Fix long paths permanently breaking GC
Suppose I have a path /nix/store/[hash]-[name]/a/a/a/a/a/[...]/a,
long enough that everything after "/nix/store/" is longer than 4096
(MAX_PATH) bytes.

Nix will happily allow such a path to be inserted into the store,
because it doesn't look at all the nested structure.  It just cares
about the /nix/store/[hash]-[name] part.  But, when the path is deleted,
we encounter a problem.  Nix will move the path to /nix/store/trash, but
then when it's trying to recursively delete the trash directory, it will
at some point try to unlink
/nix/store/trash/[hash]-[name]/a/a/a/a/a/[...]/a.  This will fail,
because the path is too long.  After this has failed, any store deletion
operation will never work again, because Nix needs to delete the trash
directory before recreating it to move new things to it.  (I assume this
is because otherwise a path being deleted could already exist in the
trash, and then moving it would fail.)

This means that if I can trick somebody into just fetching a tarball
containing a path of the right length, they won't be able to delete
store paths or garbage collect ever again, until the offending path is
manually removed from /nix/store/trash.  (And even fixing this manually
is quite difficult if you don't understand the issue, because the
absolute path that Nix says it failed to remove is also too long for
rm(1).)

This patch fixes the issue by making Nix's recursive delete operation
use unlinkat(2).  This function takes a relative path and a directory
file descriptor.  We ensure that the relative path is always just the
name of the directory entry, and therefore its length will never exceed
255 bytes.  This means that it will never even come close to AX_PATH,
and Nix will therefore be able to handle removing arbitrarily deep
directory hierachies.

Since the directory file descriptor is used for recursion after being
used in readDirectory, I made a variant of readDirectory that takes an
already open directory stream, to avoid the directory being opened
multiple times.  As we have seen from this issue, the less we have to
interact with paths, the better, and so it's good to reuse file
descriptors where possible.

I left _deletePath as succeeding even if the parent directory doesn't
exist, even though that feels wrong to me, because without that early
return, the linux-sandbox test failed.

Reported-by: Alyssa Ross <hi@alyssa.is>
Thanks-to: Puck Meerburg <puck@puckipedia.com>
Tested-by: Puck Meerburg <puck@puckipedia.com>
Reviewed-by: Puck Meerburg <puck@puckipedia.com>
2020-04-27 20:50:17 +00:00
..
build-remote Remove global -I flags 2020-03-30 15:30:19 +02:00
cpptoml bump cpptoml to v0.1.1 2019-05-29 17:01:39 +08:00
error-demo comments 2020-04-08 09:56:10 -06:00
libexpr Merge pull request #3502 from NixOS/more-pos 2020-04-18 14:05:21 +02:00
libfetchers Path fetcher: Fix store path name 2020-04-22 15:27:06 +02:00
libmain Merge pull request #3458 from zimbatm/nix-user-conf-dir 2020-04-15 13:00:28 +02:00
libstore Replace select() with poll() to allow waiting on more than FD_SETSIZE fds 2020-04-21 16:21:28 -07:00
libutil Fix long paths permanently breaking GC 2020-04-27 20:50:17 +00:00
nix Use Logger::stdout() 2020-04-16 18:14:01 +02:00
nix-build Remove global -I flags 2020-03-30 15:30:19 +02:00
nix-channel after flake rebase 2020-04-08 22:26:57 +02:00
nix-collect-garbage Remove global -I flags 2020-03-30 15:30:19 +02:00
nix-copy-closure Remove global -I flags 2020-03-30 15:30:19 +02:00
nix-daemon Remove global -I flags 2020-03-30 15:30:19 +02:00
nix-env nix-env: Refuse to operate on a new-style profile 2020-04-03 20:14:34 +02:00
nix-instantiate Remove global -I flags 2020-03-30 15:30:19 +02:00
nix-prefetch-url datatransfer.{cc,hh} -> filetransfer.{cc,hh} 2020-04-08 22:26:57 +02:00
nix-store Remove global -I flags 2020-03-30 15:30:19 +02:00
resolve-system-dependencies Fix macOS build 2020-03-30 17:00:40 +02:00