Skip to content

client: keep dentry alive while unlinking trimmed cache entries - #69021

Merged
vshankar merged 2 commits into
ceph:mainfrom
salieri11:igolikov-bug-74625
Jul 1, 2026
Merged

vshankar merged 2 commits into
ceph:mainfrom
salieri11:igolikov-bug-74625

Conversation

@salieri11

Copy link
Copy Markdown
Contributor

Summary

  • hold a local DentryRef in Client::unlink() while tearing down a dentry
  • fix the shutdown/unmount trim path where a child trim can close_dir() and drop the extra pin on the parent dentry just before that parent is trimmed
  • prevent the stale-dentry/LRU state corruption behind the ceph_shutdown() crash reported in tracker#74625

Root cause

Client::unlink() was operating on a raw Dentry* across ref-dropping work.
In the shutdown path, trimming the last child of a cached directory can call
close_dir(), which removes the extra directory pin from the parent dentry.
If that parent dentry is selected for trimming next, Client::unlink() may
still use the same raw pointer after dn->unlink() while detaching it from
the dir and removing it from the LRU.

Keeping a local DentryRef for the whole unlink teardown makes the dentry
lifetime explicit and keeps it alive until the final dn->put().

Fixes: https://tracker.ceph.com/issues/74625

Contribution Guidelines

  • To sign and title your commits, please refer to Submitting Patches to Ceph.

  • If you are submitting a fix for a stable branch (e.g. "quincy"), please refer to Submitting Patches to Ceph - Backports for the proper workflow.

  • When filling out the below checklist, you may click boxes directly in the GitHub web UI. When entering or editing the entire PR message in the GitHub web UI editor, you may also select a checklist item by adding an x between the brackets: [x]. Spaces and capitalization matter when checking off items this way.

Checklist

  • Tracker (select at least one)
    • References tracker ticket
    • Very recent bug; references commit where it was introduced
    • New feature (ticket optional)
    • Doc update (no ticket needed)
    • Code cleanup (no ticket needed)
  • Component impact
    • Affects Dashboard, opened tracker ticket
    • Affects Orchestrator, opened tracker ticket
    • No impact that needs to be tracked
  • Documentation (select at least one)
    • Updates relevant documentation
    • No doc update is appropriate
  • Tests (select at least one)
Show available Jenkins commands

You must only issue one Jenkins command per-comment. Jenkins does not understand
comments with more than one command.

@salieri11
salieri11 requested review from a team as code owners May 20, 2026 12:29
@github-actions

Copy link
Copy Markdown

This pull request can no longer be automatically merged: a rebase is needed and changes have to be manually resolved

Comment thread src/client/Client.cc Resolved

@gregsfortytwo gregsfortytwo left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'll try to look at this some more to understand better, but I don't think this patch covers the full crash scope?

Comment thread src/client/Client.cc Resolved
Comment thread src/client/Client.cc Resolved
@gregsfortytwo

Copy link
Copy Markdown
Member

So with this patch as it stands, the next thing that will happen is that we run the dentry-handling code:

if (keepdentry) {
  dn->lease_mds = -1;
} else {
  ldout(cct, 15) << "unlink  removing '" << dn->name << "' dn " << dn << dendl;
  // unlink from dir                                                                    
  Dir *dir = dn->dir;
  dn->detach();
  // delete den                                                                         
  lru.lru_remove(dn);
  dn->put();
  if (dir->is_empty() && !keepdir)
    close_dir(dir);
}

This code assumes the dentry is still linked in to a directory and that dn->dir is not NULL. Which Client::close_dir() will do.

I don't think I understand why we're seeing this crash now and not before. If the given explanation is accurate, isn't this whole workflow completely broken? That doesn't seem likely given the age and stability of this part of the code?

We're talking about a "clone", but that's the dentry name itself and it doesn't immediately look like a snapshot dentry to me?

@vshankar

Copy link
Copy Markdown
Contributor

I don't think I understand why we're seeing this crash now and not before. If the given explanation is accurate, isn't this whole workflow completely broken? That doesn't seem likely given the age and stability of this part of the code?

That is my concern too. Did we just got lucky all these days that the client was able to survive using a deallocated object? I am going to run a wild grep in our teuthology fs suite history looking for the dentry ref counts during shutdown. Let me see what I get...

@salieri11

salieri11 commented May 27, 2026 •

Copy link
Copy Markdown
Contributor Author

So with this patch as it stands, the next thing that will happen is that we run the dentry-handling code:

if (keepdentry) {
  dn->lease_mds = -1;
} else {
  ldout(cct, 15) << "unlink  removing '" << dn->name << "' dn " << dn << dendl;
  // unlink from dir                                                                    
  Dir *dir = dn->dir;
  dn->detach();
  // delete den                                                                         
  lru.lru_remove(dn);
  dn->put();
  if (dir->is_empty() && !keepdir)
    close_dir(dir);
}

This code assumes the dentry is still linked in to a directory and that dn->dir is not NULL. Which Client::close_dir() will do.

I don't think I understand why we're seeing this crash now and not before. If the given explanation is accurate, isn't this whole workflow completely broken? That doesn't seem likely given the age and stability of this part of the code?

We're talking about a "clone", but that's the dentry name itself and it doesn't immediately look like a snapshot dentry to me?

From my past experience, with jemalloc/tcmalloc, freed memory often retains its old values (not zeroed). So dn->lru still looks like a valid pointer,dn->lru_link still has list pointers that happen to work — the UAF succeeds silently. The crash only manifests when memory is recycled under pressure. I don't have another explanation for this behavior.

I can suggest adding ceph_assert(dn->ref > 0) to the Client::unlink after the if block — catches any scenario where dn->unlink() + DentryRef destructor mismatch somehow still drops ref to 0 (shouldn't happen with function-scoped DentryRef, but serves as safety net for future debugging? )

@salieri11

Copy link
Copy Markdown
Contributor Author

Another point is that Dentry::ref serves double duty — it controls both object lifetime and LRU eviction. The LRU should evict the least recently used item, but instead it walks the list skipping "pinned" items, because pinning and lifetime share the same counter. This means a cache eviction decision can accidentally destroy an object mid-operation?

The DentryRef fix makes the immediate UAF impossible. Longer term, eviction should be managed by a policy (e.g., "is this dentry idle?") rather than by the object's own refcount. And the dir/ll_ref pins should hold DentryRef instead of manual get()/put(), so pin symmetry is guaranteed by RAII.

@vshankar

Copy link
Copy Markdown
Contributor

So with this patch as it stands, the next thing that will happen is that we run the dentry-handling code:

if (keepdentry) {
  dn->lease_mds = -1;
} else {
  ldout(cct, 15) << "unlink  removing '" << dn->name << "' dn " << dn << dendl;
  // unlink from dir                                                                    
  Dir *dir = dn->dir;
  dn->detach();
  // delete den                                                                         
  lru.lru_remove(dn);
  dn->put();
  if (dir->is_empty() && !keepdir)
    close_dir(dir);
}

This code assumes the dentry is still linked in to a directory and that dn->dir is not NULL. Which Client::close_dir() will do.
I don't think I understand why we're seeing this crash now and not before. If the given explanation is accurate, isn't this whole workflow completely broken? That doesn't seem likely given the age and stability of this part of the code?
We're talking about a "clone", but that's the dentry name itself and it doesn't immediately look like a snapshot dentry to me?

From my past experience, with jemalloc/tcmalloc, freed memory often retains its old values (not zeroed). So dn->lru still looks like a valid pointer, dn->lru_link still has list pointers that happen to work — the UAF succeeds silently. The crash only manifests when memory is recycled under pressure. I don't have another explanation for this behavior.

I can suggest adding ceph_assert(dn->ref > 0) to the Client::unlink after the if block — catches any scenario where dn->unlink() + DentryRef destructor mismatch somehow still drops ref to 0 (shouldn't happen with function-scoped DentryRef, but serves as safety net for future debugging? )

I scrapped through our teuthology tests that exercises this code path and here is what is happening. E.g.: /a/choffman-2026-04-22_16:26:51-fs:fscrypt-main-distro-default-trial/169501/remote/trial108/log/ceph-mgr.x.log.gz

2026-04-22T16:51:17.120+0000 7ff7ce0bd640 15 client.5429 trim_dentry unlinking dn 8e52a303-5736-43f4-a1ae-eafdec34f9c5 in dir 0x100000005e7
2026-04-22T16:51:17.120+0000 7ff7ce0bd640 15 client.5429 unlink dir 0x561975ef7800 '8e52a303-5736-43f4-a1ae-eafdec34f9c5' dn 0x561975f18460 inode 0
2026-04-22T16:51:17.120+0000 7ff7ce0bd640 15 client.5429 unlink  removing '8e52a303-5736-43f4-a1ae-eafdec34f9c5' dn 0x561975f18460
2026-04-22T16:51:17.120+0000 7ff7ce0bd640 15 client.5429 close_dir dir 0x56197446b020 on 0x561975ef7800
2026-04-22T16:51:17.120+0000 7ff7ce0bd640 20 client.5429 put_inode on 0x100000005e7.head(faked_ino=0 nref=5 ll_ref=0 cap_refs={} open={} mode=40700 size=0/0 nlink=1 btime=2026-04-22T16:50:52.754881+0000 mtime=2026-04-22T16:50:57.065171+0000 ctime=2026-04-22T16:50:57.065171+0000 change_attr=2 caps=- COMPLETE parents=0x100000005e6.head["clone" ref=1 ino=0x100000005e7.head csg=1] 0x561975ef7800) n = 1

From above, when the last child under "clone" gets trimmed, the dout() for put_inode() has dentry list for clone with ref=1, followed by, trimming dentry(clone) itself:

2026-04-22T16:51:17.120+0000 7ff7ce0bd640 15 client.5429 unlink dir 0x561975ef7100 'clone' dn 0x561975f18380 inode 0x561975ef7800
2026-04-22T16:51:17.120+0000 7ff7ce0bd640 20 client.5429 put_inode on 0x100000005e7.head(faked_ino=0 nref=6 ll_ref=0 cap_refs={} open={} mode=40700 size=0/0 nlink=1 btime=2026-04-22T16:50:52.754881+0000 mtime=2026-04-22T16:50:57.065171+0000 ctime=2026-04-22T16:50:57.065171+0000 change_attr=2 caps=- COMPLETE 0x561975ef7800) n = 1
2026-04-22T16:51:17.120+0000 7ff7ce0bd640 20 client.5429 unlink  inode 0x561975ef7800 parents now
2026-04-22T16:51:17.120+0000 7ff7ce0bd640 15 client.5429 unlink  removing 'clone' dn 0x561975f18380
2026-04-22T16:51:17.120+0000 7ff7ce0bd640 15 client.5429 close_dir dir 0x56197421de60 on 0x561975ef7100

Pretty similar pattern, but no crash in this case.

@salieri11

Copy link
Copy Markdown
Contributor Author

I suggest to go with the function scope fix, it will not make it worse, but the assert I suggested may give us good hint next time this crash will hit

@vshankar

Copy link
Copy Markdown
Contributor

I can suggest adding ceph_assert(dn->ref > 0) to the Client::unlink after the if block — catches any scenario where dn->unlink() + DentryRef destructor mismatch somehow still drops ref to 0 (shouldn't happen with function-scoped DentryRef, but serves as safety net for future debugging? )

Do you have a change that I can put to test (without this change), so that when it hits a coredump is there for us to debug?

Comment thread src/client/Client.cc Resolved
@gregsfortytwo

Copy link
Copy Markdown
Member

I think diagnostics is the best we can do right now, so this seems like a good approach.

@dparmar18

Copy link
Copy Markdown
Contributor

So with this patch as it stands, the next thing that will happen is that we run the dentry-handling code:

if (keepdentry) {
  dn->lease_mds = -1;
} else {
  ldout(cct, 15) << "unlink  removing '" << dn->name << "' dn " << dn << dendl;
  // unlink from dir                                                                    
  Dir *dir = dn->dir;
  dn->detach();
  // delete den                                                                         
  lru.lru_remove(dn);
  dn->put();
  if (dir->is_empty() && !keepdir)
    close_dir(dir);
}

This code assumes the dentry is still linked in to a directory and that dn->dir is not NULL. Which Client::close_dir() will do.
I don't think I understand why we're seeing this crash now and not before. If the given explanation is accurate, isn't this whole workflow completely broken? That doesn't seem likely given the age and stability of this part of the code?
We're talking about a "clone", but that's the dentry name itself and it doesn't immediately look like a snapshot dentry to me?

From my past experience, with jemalloc/tcmalloc, freed memory often retains its old values (not zeroed). So dn->lru still looks like a valid pointer,dn->lru_link still has list pointers that happen to work — the UAF succeeds silently. The crash only manifests when memory is recycled under pressure. I don't have another explanation for this behavior.

Not sure about tcmalloc but IIRC then jemalloc provides a debugging tool call opt.junk (junk filling) that initialises freed/unused memory with a specific byte stream and on hitting a UAF it should segfault. Might help with debugging?

@gregsfortytwo

Copy link
Copy Markdown
Member

This code assumes the dentry is still linked in to a directory and that dn->dir is not NULL. Which Client::close_dir() will do.
I don't think I understand why we're seeing this crash now and not before. If the given explanation is accurate, isn't this whole workflow completely broken? That doesn't seem likely given the age and stability of this part of the code?
We're talking about a "clone", but that's the dentry name itself and it doesn't immediately look like a snapshot dentry to me?

From my past experience, with jemalloc/tcmalloc, freed memory often retains its old values (not zeroed). So dn->lru still looks like a valid pointer,dn->lru_link still has list pointers that happen to work — the UAF succeeds silently. The crash only manifests when memory is recycled under pressure. I don't have another explanation for this behavior.

Yeah, but Client::close_dir() is explicitly zeroing out the dn->dir pointer, so we won't be able to follow that and should bring segfaulting when we hit that dereference, if the crash is a result of running this unlink code after close_dir() runs.

@vshankar

Copy link
Copy Markdown
Contributor

This code assumes the dentry is still linked in to a directory and that dn->dir is not NULL. Which Client::close_dir() will do.
I don't think I understand why we're seeing this crash now and not before. If the given explanation is accurate, isn't this whole workflow completely broken? That doesn't seem likely given the age and stability of this part of the code?
We're talking about a "clone", but that's the dentry name itself and it doesn't immediately look like a snapshot dentry to me?

From my past experience, with jemalloc/tcmalloc, freed memory often retains its old values (not zeroed). So dn->lru still looks like a valid pointer,dn->lru_link still has list pointers that happen to work — the UAF succeeds silently. The crash only manifests when memory is recycled under pressure. I don't have another explanation for this behavior.

Yeah, but Client::close_dir() is explicitly zeroing out the dn->dir pointer, so we won't be able to follow that and should bring segfaulting when we hit that dereference, if the crash is a result of running this unlink code after close_dir() runs.

Yeh, I have been banging my head on this without being able to explain why there would be a premature deallocation of the dentry. The only explanation I have now is the absence of PR #60909 could be causing this issue. It seems related to some extent but I cannot yet say with 100% surety that it's caused by the missing PR in the branch where the crash was seen. @gregsfortytwo

@salieri11

Copy link
Copy Markdown
Contributor Author

We confirmed that PR #60909 (pin inode/dentry for O_DIRECTORY) is not in the 19.2.1 build where the crash was observed. That PR was merged 11 days after the v19.2.1 tag.

Without it, opening a directory does not call _ll_get(), so the dentry stays unpinned (ref=1) while inode->dir is still active. When trim_cache() evicts this dentry, Dentry::unlink() calls put() for the active dir pin, ref drops to 0, and the dentry is freed while Client::unlink() is still using it.

PR #60909 fixes the root cause — the pin was never taken. Our DentryRef patch here adds a safety net: even if a pin is missing for some other reason in the future, Client::unlink() will not corrupt the LRU by operating on a freed dentry.

I suggest we land both: PR #60909 as the backport fix, and this DentryRef guard as defense-in-depth so that missing pins produce an assert (ref > 1) instead of silent memory corruption.

@vshankar

vshankar commented Jun 1, 2026

Copy link
Copy Markdown
Contributor

jenkins retest this please

@vshankar

vshankar commented Jun 1, 2026

Copy link
Copy Markdown
Contributor

This PR has been added to main integration testing by vshankar started 2026-06-01-06:34.

@vshankar

vshankar commented Jun 3, 2026

Copy link
Copy Markdown
Contributor

jenkins retest this please

@vshankar vshankar left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@gregsfortytwo gregsfortytwo left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@salieri11's explanation seems good to me. But the code is still referring to close_dir() as a path to trouble. Let's update the code comment with our new understanding and then merge? :)

@vshankar

Copy link
Copy Markdown
Contributor

jenkins retest this please

@vshankar

Copy link
Copy Markdown
Contributor

jenkins test docs

1 similar comment
@vshankar

Copy link
Copy Markdown
Contributor

jenkins test docs

salieri11 added 2 commits July 1, 2026 11:04
Add ceph_assert(dn->ref > 0) after Dentry::unlink() in Client::unlink()
to catch the case where internal put() calls drop the dentry reference
to zero before we proceed to detach/lru_remove.

This turns a silent use-after-free into a clean assertion failure with
a full stack trace, making it possible to catch the exact conditions
in a test environment.

Signed-off-by: Igor Golikov <[email protected]>
Fixes: https://tracker.ceph.com/issues/74625
Hold a function-scoped DentryRef in Client::unlink() to keep the dentry
alive through the entire teardown sequence.

Without the O_DIRECTORY dentry pin (PR ceph#60909, merged after v19.2.1),
opening a directory does not call _ll_get(), so the dentry stays at
ref=1 while inode->dir is still active. When trim_cache() evicts this
dentry, Dentry::unlink() calls put() for the dir pin, ref drops to 0,
and the dentry is freed while Client::unlink() still needs it for
detach/lru_remove.

A function-scoped DentryRef guarantees the dentry survives until after
it has been properly removed from the dir and LRU, regardless of pin
state.

Signed-off-by: Igor Golikov <[email protected]>
Fixes: https://tracker.ceph.com/issues/74625
@vshankar

vshankar commented Jul 1, 2026

Copy link
Copy Markdown
Contributor

Force pushed to get jenkins test passing (hopefully).

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Projects

Archived in project

Development

Successfully merging this pull request may close these issues.

5 participants