Skip to content

Commit 409e873

Browse files
lxbszidryomov
authored andcommitted
ceph: fix use-after-free bug for inodes when flushing capsnaps
There is a race between capsnaps flush and removing the inode from 'mdsc->snap_flush_list' list: == Thread A == == Thread B == ceph_queue_cap_snap() -> allocate 'capsnapA' ->ihold('&ci->vfs_inode') ->add 'capsnapA' to 'ci->i_cap_snaps' ->add 'ci' to 'mdsc->snap_flush_list' ... == Thread C == ceph_flush_snaps() ->__ceph_flush_snaps() ->__send_flush_snap() handle_cap_flushsnap_ack() ->iput('&ci->vfs_inode') this also will release 'ci' ... == Thread D == ceph_handle_snap() ->flush_snaps() ->iterate 'mdsc->snap_flush_list' ->get the stale 'ci' ->remove 'ci' from ->ihold(&ci->vfs_inode) this 'mdsc->snap_flush_list' will WARNING To fix this we will increase the inode's i_count ref when adding 'ci' to the 'mdsc->snap_flush_list' list. [ idryomov: need_put int -> bool ] Cc: stable@vger.kernel.org Link: https://bugzilla.redhat.com/show_bug.cgi?id=2209299 Signed-off-by: Xiubo Li <xiubli@redhat.com> Reviewed-by: Milind Changire <mchangir@redhat.com> Reviewed-by: Ilya Dryomov <idryomov@gmail.com> Signed-off-by: Ilya Dryomov <idryomov@gmail.com>
1 parent 870611e commit 409e873

File tree

2 files changed

+9
-1
lines changed

2 files changed

+9
-1
lines changed

fs/ceph/caps.c

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1627,6 +1627,7 @@ void ceph_flush_snaps(struct ceph_inode_info *ci,
16271627
struct inode *inode = &ci->netfs.inode;
16281628
struct ceph_mds_client *mdsc = ceph_inode_to_client(inode)->mdsc;
16291629
struct ceph_mds_session *session = NULL;
1630+
bool need_put = false;
16301631
int mds;
16311632

16321633
dout("ceph_flush_snaps %p\n", inode);
@@ -1671,8 +1672,13 @@ void ceph_flush_snaps(struct ceph_inode_info *ci,
16711672
ceph_put_mds_session(session);
16721673
/* we flushed them all; remove this inode from the queue */
16731674
spin_lock(&mdsc->snap_flush_lock);
1675+
if (!list_empty(&ci->i_snap_flush_item))
1676+
need_put = true;
16741677
list_del_init(&ci->i_snap_flush_item);
16751678
spin_unlock(&mdsc->snap_flush_lock);
1679+
1680+
if (need_put)
1681+
iput(inode);
16761682
}
16771683

16781684
/*

fs/ceph/snap.c

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -693,8 +693,10 @@ int __ceph_finish_cap_snap(struct ceph_inode_info *ci,
693693
capsnap->size);
694694

695695
spin_lock(&mdsc->snap_flush_lock);
696-
if (list_empty(&ci->i_snap_flush_item))
696+
if (list_empty(&ci->i_snap_flush_item)) {
697+
ihold(inode);
697698
list_add_tail(&ci->i_snap_flush_item, &mdsc->snap_flush_list);
699+
}
698700
spin_unlock(&mdsc->snap_flush_lock);
699701
return 1; /* caller may want to ceph_flush_snaps */
700702
}

0 commit comments

Comments
 (0)