* [PATCH] namei: don't count symlinks twice when a lookup restarts
@ 2026-11-23 19:44 Michael Greenberg
0 siblings, 0 replies; only message in thread
From: Michael Greenberg @ 2026-11-23 19:44 UTC (permalink / raw)
To: Alexander Viro, Christian Brauner; +Cc: Jan Kara, linux-fsdevel, mgreenbe
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.
Fixes: 2788cc47f459 ("Don't reset ->total_link_count on nested calls of vfs_path_lookup()")
Cc: stable@vger.kernel.org
Link: https://github.com/rrnewton/hermit/issues/5045
Signed-off-by: Michael Greenberg <michael@greenberg.science>
---
// 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(+)
diff --git a/fs/namei.c b/fs/namei.c
index 925aeb63bfa1..5f39d42ea6be 100644
--- a/fs/namei.c
+++ b/fs/namei.c
@@ -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();
--
2.53.0
^ permalink raw reply related [flat|nested] only message in thread
only message in thread, other threads:[~2026-11-23 19:44 UTC | newest]
Thread overview: (only message) (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2026-11-23 19:44 [PATCH] namei: don't count symlinks twice when a lookup restarts Michael Greenberg
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox