Re: [PATCH] ceph: fix slab-use-after-free of capsnap in ceph_put_wrbuffer_cap_refs()
From: Xiubo Li
Date: Wed Nov 11 2026 - 21:05:44 EST
On ... zhangdaolong wrote:
> 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(). ...
Hi Daolong,
Thanks for your patch.
Reproduced on testing with KASAN and the doutc in
ceph_put_wrbuffer_cap_refs() enabled: dirty a file under a snapshotted
dir, rmdir the snapshot (snapc seq bump without new snaps, so the
cap_snap is queued with need_flush == false), then sync. Every
writeback completion that drops the cap_snap gave two
slab-use-after-free reports (4-byte read of dirty_pages and 8-byte
read of ->context) on the ceph_cap_snap cache, freed by
ceph_try_drop_cap_snap(). With this patch the same 30 drops give no
KASAN report.
Reviewed-by: Xiubo Li <xiubo.li@xxxxxxxxx>
Tested-by: Xiubo Li <xiubo.li@xxxxxxxxx>
Thanks
- Xiubo
zhangdaolong <dlzhangak@xxxxxxxxxxxxxx> 于2026年11月11日周三 08:40写道:
>
> 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
>