namei: don't count symlinks twice when a lookup restarts
PATCH 1/1 namei: don't count symlinks twice when a lookup restarts
Since commit 2788cc47f459 ("Don't reset ->total_link_count on nested calls of vfs_path_lookup()"), only set_nameidata() sets ->total_link_count, once per lookup. When a walk in RCU mode fails with -ECHILD, filename_lookup(), __filename_parentat(), do_file_open() and do_file_open_root() retry it in ref-walk mode with the same nameidata, and the retry re-counts every link the abandoned walk had already followed. The LOOKUP_REVAL retry after -ESTALE does the same.
So a path that crosses n symlinks fails with -ELOOP if the RCU walk got through more than MAXSYMLINKS - n of them before giving up, which is possible for any n > MAXSYMLINKS / 2. A mount or umount anywhere on the system can make an RCU walk give up, since it changes mount_lock's sequence count, so whether such a lookup fails depends on unrelated activity. Opening the first link of a chain for 3 s, in a 4-CPU KVM guest, while another process mounted and unmounted a tmpfs in its own mount namespace:
v7.4-rc4 with this patch links opens -ELOOP opens -ELOOP 20 1,350,902 0 1,109,467 0 21 1,352,363 259,737 1,036,744 0 40 947,012 449,441 571,202 0 41 973,308 973,308 852,776 852,776
Without the mount loop, a 40-link chain never failed: 0 of 1.1 million openat2() calls on a v7.3 host. The reproducer is below.
The fix is simple: restart the count in path_init(), which begins every attempt, at the value set_nameidata() saved, if such a value exists. This avoids the DoS risk 2788cc47f459 fixed while fixing the incorrect -ELOOP returns.
---
// SPDX-License-Identifier: GPL-2.0
//
// Spurious ELOOP from a symlink chain under MAXSYMLINKS, while mounts happen // elsewhere on the system.
//
// cc -O2 -Wall -o eloop-retry eloop-retry.c
// ./eloop-retry 40 3 # chain of 40 links, open for 3 seconds
//
// It makes l1 -> l2 -> ... -> lN -> f in a fresh temporary directory. A child // enters its own user and mount namespace and mounts and unmounts a tmpfs // there in a loop. The parent opens l1 over and over, and counts the opens // that fail with ELOOP. With N <= 20 none should, and with N > 40 all must. // With 21 <= N <= 40, an unpatched kernel fails some: a walk that leaves RCU // mode with -ECHILD is retried without resetting nd->total_link_count, so the // links the first walk followed are counted twice.
#define _GNU_SOURCE
#include <errno.h>
#include <fcntl.h>
#include <sched.h>
#include <signal.h>
#include <stdio.h>
#include <stdlib.h>
#include <sys/mount.h>
#include <sys/stat.h>
#include <sys/wait.h>
#include <time.h>
#include <unistd.h>
static void die(const char *what)
{
perror(what);
exit(1);
}int main(int argc, char **argv)
{
int links = argc > 1 ? atoi(argv[1]) : 40;
int seconds = argc > 2 ? atoi(argv[2]) : 3;
char dir[] = "/tmp/eloop-retry.XXXXXX", name[32], target[32];
long opens = 0, eloop = 0, other = 0;
struct timespec now, end;
pid_t churn;
if (links < 1 || links > 100 || seconds < 1) {
fprintf(stderr, "usage: %s [links 1-100] [seconds]\n", argv[0]);
return 2;
}
if (!mkdtemp(dir) || chdir(dir) < 0)
die(dir);
if (close(open("f", O_CREAT | O_WRONLY, 0644)) < 0)
die("f");
for (int i = 1; i <= links; i++) {
snprintf(name, sizeof(name), "l%d", i);
if (i < links)
snprintf(target, sizeof(target), "l%d", i + 1);
else
snprintf(target, sizeof(target), "f");
if (symlink(target, name) < 0)
die(name);
}
if (mkdir("mnt", 0755) < 0)
die("mnt");
churn = fork();
if (churn < 0)
die("fork");
if (churn == 0) {
if (unshare(CLONE_NEWUSER | CLONE_NEWNS) < 0)
die("unshare");
for (;;) {
if (mount("t", "mnt", "tmpfs", 0, NULL) < 0)
die("mount");
if (umount("mnt") < 0)
die("umount");
}
}
clock_gettime(CLOCK_MONOTONIC, &end);
end.tv_sec += seconds;
do {
int fd = open("l1", O_RDONLY);
opens++;
if (fd >= 0)
close(fd);
else if (errno == ELOOP)
eloop++;
else
other++;
clock_gettime(CLOCK_MONOTONIC, &now);
} while (now.tv_sec < end.tv_sec ||
(now.tv_sec == end.tv_sec && now.tv_nsec < end.tv_nsec));
kill(churn, SIGKILL);
waitpid(churn, NULL, 0);
printf("%d links: %ld opens, %ld ELOOP, %ld other errors\n",
links, opens, eloop, other);
return eloop != 0;
}fs/namei.c | 7 +++++++ 1 file changed, 7 insertions(+)
fs/namei.c +7 -0
@@ -2699,6 +2699,13 @@ static const char *path_init(struct nameidata *nd, unsigned flags) nd->flags = flags; nd->state |=ND_JUMPED ; +/* +* A retry in ref-walk mode after -ECHILD, or with LOOKUP_REVAL after +* -ESTALE, starts the walk over. Start the link count from where we +* left off, so we don't double count. +*/ + nd->total_link_count = nd->saved ? nd->saved->total_link_count :0 ; + nd->m_seq =__read_seqcount_begin (&mount_lock.seqcount); nd->r_seq =__read_seqcount_begin (&rename_lock.seqcount);smp_rmb ();
Signature · 1 line
2.53.0
Al Viro
maintainerQuoted 8 lines · Michael Greenberg
So a path that crosses n symlinks fails with
-ELOOPif the RCU walk got through more than MAXSYMLINKS - n of them before giving up, which is possible for any n > MAXSYMLINKS / 2. A mount or umount anywhere on the system can make an RCU walk give up, since it changes mount_lock's sequence count, so whether such a lookup fails depends on unrelated activity. Opening the first link of a chain for 3 s, in a 4-CPU KVM guest, while another process mounted and unmounted a tmpfs in its own mount namespace:
You don't need mount to kick you out of RCU mode - just walk through /proc/self/root/... at given step of the chain and you'll get deterministic behaviour; no races needed at all.
Bug is real, but I'm not sure I like your solution - duplication between path_init() and __set_nameidata() is confusing. We can't just drop that part of __set_nameidata() - it's possible to get from set_nameidata() to restore_nameidata() without ever reaching path_init(), e.g. in do_file_open() if alloc_empty_file() fails, so it can't be dealt with that way, but...
Let me think a bit; if we go with your variant, we'll need a very clear comment along the lines of "it's not really redundant - removing either will break things" added in both places.
Al Viro
maintainerQuoted 21 lines · Al Viro
Quoted 8 lines · Michael Greenberg
So a path that crosses n symlinks fails with
-ELOOPif the RCU walk got through more than MAXSYMLINKS - n of them before giving up, which is possible for any n > MAXSYMLINKS / 2. A mount or umount anywhere on the system can make an RCU walk give up, since it changes mount_lock's sequence count, so whether such a lookup fails depends on unrelated activity. Opening the first link of a chain for 3 s, in a 4-CPU KVM guest, while another process mounted and unmounted a tmpfs in its own mount namespace:You don't need mount to kick you out of RCU mode - just walk through
/proc/self/root/... at given step of the chain and you'll get deterministic behaviour; no races needed at all.Bug is real, but I'm not sure I like your solution - duplication between
path_init()and__set_nameidata()is confusing. We can't just drop that part of__set_nameidata()- it's possible to get fromset_nameidata()torestore_nameidata()without ever reachingpath_init(), e.g. indo_file_open()ifalloc_empty_file()fails, so it can't be dealt with that way, but...
Quoted 3 lines
Let me think a bit; if we go with your variant, we'll need a very clear comment along the lines of "it's not really redundant - removing either will break things" added in both places.
One possibility would be
p->total_link_count = -1; in __set_nameidata(), same change as in your variant in path_init() and if (unlikely(old) && now->total_link_count > 0) old->total_link_count = now->total_link_count;
in restore_nameidata(). Not sure I like that variant, though...
One thing to keep in mind is that reserve_stack() is not the only place where we bump that thing - follow_automount() is another and there we don't have nameidata on hand...