From: Michael Greenberg <michael@greenberg.science>
To: Al Viro <viro@zeniv.linux.org.uk>
Cc: Christian Brauner <brauner@kernel.org>, Jan Kara <jack@suse.cz>,
linux-fsdevel@vger.kernel.org
Subject: Re: [PATCH] namei: don't count symlinks twice when a lookup restarts
Date: Tue, 24 Nov 2026 11:12:06 -0500 [thread overview]
Message-ID: <87o6be5hux.fsf@hippogriff> (raw)
In-Reply-To: <20261124035245.GX989762@ZenIV>
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))
prev parent reply other threads:[~2026-11-24 16:12 UTC|newest]
Thread overview: 4+ messages / expand[flat|nested] mbox.gz Atom feed top
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 message]
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=87o6be5hux.fsf@hippogriff \
--to=michael@greenberg.science \
--cc=brauner@kernel.org \
--cc=jack@suse.cz \
--cc=linux-fsdevel@vger.kernel.org \
--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 a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox