From: Michael Greenberg <michael@greenberg.science>
To: Alexander Viro <viro@zeniv.linux.org.uk>,
Christian Brauner <brauner@kernel.org>
Cc: Jan Kara <jack@suse.cz>,
linux-fsdevel@vger.kernel.org, mgreenbe@stevens.edu
Subject: [PATCH] namei: don't count symlinks twice when a lookup restarts
Date: Mon, 23 Nov 2026 14:44:28 -0500 [thread overview]
Message-ID: <87zeuz5o4j.fsf@hippogriff> (raw)
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
next reply other threads:[~2026-11-23 19:44 UTC|newest]
Thread overview: 4+ messages / expand[flat|nested] mbox.gz Atom feed top
2026-11-23 19:44 Michael Greenberg [this message]
2026-11-24 3:16 ` [PATCH] namei: don't count symlinks twice when a lookup restarts Al Viro
2026-11-24 3:52 ` Al Viro
2026-11-24 16:12 ` Michael Greenberg
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=87zeuz5o4j.fsf@hippogriff \
--to=michael@greenberg.science \
--cc=brauner@kernel.org \
--cc=jack@suse.cz \
--cc=linux-fsdevel@vger.kernel.org \
--cc=mgreenbe@stevens.edu \
--cc=viro@zeniv.linux.org.uk \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.