* [PATCH] namei: don't count symlinks twice when a lookup restarts
@ 2026-11-23 19:44 Michael Greenberg
2026-11-24 3:16 ` Al Viro
0 siblings, 1 reply; 4+ messages 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] 4+ messages in thread* Re: [PATCH] namei: don't count symlinks twice when a lookup restarts
2026-11-23 19:44 [PATCH] namei: don't count symlinks twice when a lookup restarts Michael Greenberg
@ 2026-11-24 3:16 ` Al Viro
2026-11-24 3:52 ` Al Viro
0 siblings, 1 reply; 4+ messages in thread
From: Al Viro @ 2026-11-24 3:16 UTC (permalink / raw)
To: Michael Greenberg; +Cc: Christian Brauner, Jan Kara, linux-fsdevel, mgreenbe
On Mon, Nov 23, 2026 at 02:44:28PM -0500, Michael Greenberg wrote:
> 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:
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.
^ permalink raw reply [flat|nested] 4+ messages in thread
* Re: [PATCH] namei: don't count symlinks twice when a lookup restarts
2026-11-24 3:16 ` Al Viro
@ 2026-11-24 3:52 ` Al Viro
2026-11-24 16:12 ` Michael Greenberg
0 siblings, 1 reply; 4+ messages in thread
From: Al Viro @ 2026-11-24 3:52 UTC (permalink / raw)
To: Michael Greenberg; +Cc: Christian Brauner, Jan Kara, linux-fsdevel, mgreenbe
On Tue, Nov 24, 2026 at 03:16:49AM +0000, Al Viro wrote:
> On Mon, Nov 23, 2026 at 02:44:28PM -0500, Michael Greenberg wrote:
> > 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:
>
> 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.
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...
^ permalink raw reply [flat|nested] 4+ messages in thread
* Re: [PATCH] namei: don't count symlinks twice when a lookup restarts
2026-11-24 3:52 ` Al Viro
@ 2026-11-24 16:12 ` Michael Greenberg
0 siblings, 0 replies; 4+ messages in thread
From: Michael Greenberg @ 2026-11-24 16:12 UTC (permalink / raw)
To: Al Viro; +Cc: Christian Brauner, Jan Kara, linux-fsdevel
On 2026-11-24 at 03:52:45 AM, Al Viro wrote:
> On Tue, Nov 24, 2026 at 03:16:49AM +0000, Al Viro wrote:
>> On Mon, Nov 23, 2026 at 02:44:28PM -0500, Michael Greenberg wrote:
>> > 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:
>>
>> 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.
The racy mount loop is how I hit the bug. A deterministic reproducer
would be much nicer, but I tried to build one and couldn't trigger the
bug.
It seems that proc's get_link (proc_pid_get_link()) returns -ECHILD in
RCU mode, and then pick_link() calls try_to_unlazy(), which could only
fail (as far as I can tell) with a concurrent change, like a mount. When
it succeeds, we continue in ref-walk mode with a correct count and no
restart.
Here's a run on an unpatched 7.3 host, with the proc link at positions 1,
10, 19, and last.
Links ELOOP (out of 1000 calls)
21, 30, 40 0
41 1000
I get the same results through /proc/self/root, /proc/<pid>/root, and
/proc/thread-self/root. Code is at the bottom... maybe I messed up, or
maybe you meant a different approach? The only other path I found that
returns -ECHILD to the caller without trying to unlazy itself is
security_inode_follow_link() in pick_link(), the SELinux invalid-label
case in inode_security_rcu().
>> 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...
I'm happy with either solution, though I think it's a bit clearer to
have just one line in `path_init` (rather than an invariant and sentinel
value to keep track of).
>> 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.
Here's a draft of comments for my patch.
In __set_nameidata():
/*
* path_init() sets this again at the start of every attempt, which
* is not redundant: a caller can reach restore_nameidata() without
* calling path_init(), e.g. do_file_open() when alloc_empty_file()
* fails, and restore_nameidata() copies this count back to the
* outer lookup. Without this assignment, restore_nameidata() would
* copy back an uninitialized count; without the one in path_init(),
* links are counted twice when a lookup restarts.
*/
p->total_link_count = old ? old->total_link_count : 0;
In path_init():
/*
* Each attempt (the first, the ref-walk retry after -ECHILD, the
* LOOKUP_REVAL retry after -ESTALE) starts counting links again from
* the count of the lookup this one is nested in, if any, so that
* links an abandoned attempt followed are not counted twice. This
* repeats the assignment in __set_nameidata() on purpose; see the
* comment there.
*/
nd->total_link_count = nd->saved ? nd->saved->total_link_count : 0;
Would you like a v2 with these comments added? Alternatively, I'd be
happy to send a patch with your -1 approach (and similar comments in
__set_nameidata() and restore_nameidata()).
> 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...
I don't _think_ this is an issue for follow_automount(), which receives
&nd->total_link_count (from handle_mounts(); follow_down() passes NULL),
which path_init() resets.
Overall, we make three kinds of attempts:
- RCU mode
- ref-walk, after -ECHILD
- ref-walk with LOOKUP_REVAL, after -ESTALE
With the patch, each of these either uses the outer lookup's count or 0,
yielding -ELOOP after 40. An unlucky lookup that makes all three
attempts will follow up to 120 links (40 per attempt), but no successful
path will have more than 40. Before the patch, we stopped at 40 but
paths of 21 to 40 links could fail with -ELOOP early.
Michael
---
#!/usr/bin/python3 -I
#
# try to get a premature ELOOP using proc's root magic link
#
# Usage: proc-eloop.py DIR (chains are built in fresh directories in DIR)
import errno, os, sys, tempfile
PREFIXES = {
"self/root": ("/proc/self/root", 2), # /proc/self is a link
"pid/root": (f"/proc/{os.getpid()}/root", 1),
"thread-self/root": ("/proc/thread-self/root", 2), # /proc/thread-self is a link
}
def trial(total, magic, prefix, tries=1000):
path, cost = PREFIXES[prefix]
d = tempfile.mkdtemp(dir=sys.argv[1])
open(os.path.join(d, "f"), "w").close()
plain = total - cost
for i in range(1, plain + 1):
nxt = f"l{i + 1}" if i < plain else "f"
target = f"{path}{d}/{nxt}" if i == magic else nxt
os.symlink(target, os.path.join(d, f"l{i}"))
eloop = 0
for _ in range(tries):
try:
os.close(os.open(os.path.join(d, "l1"), os.O_RDONLY))
except OSError as e:
if e.errno != errno.ELOOP:
raise
eloop += 1
return eloop
print(os.uname().release)
for prefix in PREFIXES:
for total in (21, 30, 40, 41):
cells = []
for magic in (1, 10, 19, total - PREFIXES[prefix][1]):
cells.append(f"@{magic}:{trial(total, magic, prefix)}")
print(f"{prefix:16} {total} links, ELOOP of 1000:", " ".join(cells))
^ permalink raw reply [flat|nested] 4+ messages in thread
end of thread, other threads:[~2026-11-24 16:12 UTC | newest]
Thread overview: 4+ messages (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
2026-11-24 3:16 ` Al Viro
2026-11-24 3:52 ` Al Viro
2026-11-24 16:12 ` Michael Greenberg
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox