[PATCH] ceph: fix slab-use-after-free of capsnap in ceph_put_wrbuffer_cap_refs()

From: zhangdaolong

Date: Wed Nov 11 2026 - 02:43:59 EST


In ceph_put_wrbuffer_cap_refs(), when capsnap->dirty_pages drops to 0
and !capsnap->writing, ceph_try_drop_cap_snap() may drop the last
reference on capsnap and free it via ceph_put_cap_snap(). However, the
subsequent doutc() statement still dereferences capsnap->context->seq
and capsnap->dirty_pages, triggering a KASAN slab-use-after-free when
debug logging is enabled.

Fix this by emitting the doutc() log message before calling
ceph_try_drop_cap_snap().

Fixes: 860560904962 ("ceph: avoid sending unnessesary FLUSHSNAP message")
Reported-by: Shuangpeng Bai <shuangpeng.kernel@xxxxxxxxx>
Closes: https://tracker.ceph.com/issues/77923
Signed-off-by: zhangdaolong <dlzhangak@xxxxxxxxxxxxxx>
---
fs/ceph/caps.c | 21 +++++++++++----------
1 file changed, 11 insertions(+), 10 deletions(-)

diff --git a/fs/ceph/caps.c b/fs/ceph/caps.c
index 4b37d9ffdf7f..2f9f334276d1 100644
--- a/fs/ceph/caps.c
+++ b/fs/ceph/caps.c
@@ -3393,23 +3393,24 @@ void ceph_put_wrbuffer_cap_refs(struct ceph_inode_info *ci, int nr,
}

capsnap->dirty_pages -= nr;
- if (capsnap->dirty_pages == 0) {
+ if (capsnap->dirty_pages == 0)
complete_capsnap = true;
- if (!capsnap->writing) {
- if (ceph_try_drop_cap_snap(ci, capsnap)) {
- put++;
- } else {
- set_bit(CEPH_I_FLUSH_SNAPS_BIT, &ci->i_ceph_flags);
- flush_snaps = true;
- }
- }
- }
+
doutc(cl, "%p %llx.%llx cap_snap %p snap %lld %d/%d -> %d/%d %s%s\n",
inode, ceph_vinop(inode), capsnap, capsnap->context->seq,
ci->i_wrbuffer_ref+nr, capsnap->dirty_pages + nr,
ci->i_wrbuffer_ref, capsnap->dirty_pages,
last ? " (wrbuffer last)" : "",
complete_capsnap ? " (complete capsnap)" : "");
+
+ if (complete_capsnap && !capsnap->writing) {
+ if (ceph_try_drop_cap_snap(ci, capsnap)) {
+ put++;
+ } else {
+ set_bit(CEPH_I_FLUSH_SNAPS_BIT, &ci->i_ceph_flags);
+ flush_snaps = true;
+ }
+ }
}

unlock:
--
2.48.1