Skip to contentSkip to conversation

ceph: fix potential use-after-free bug when trimming caps

PATCH New [email protected] 1 participant 3 messages Apr 19, 2023

Subsystems (2)
lore
Share & tools
lore
#0 lore raw

v4 cover ceph: fix potential use-after-free bug when trimming caps

From: Xiubo Li <[email protected]>

V4:
- swtich ci_node to mds

Xiubo Li (2):
ceph: export __get_cap_for_mds() helper
ceph: fix potential use-after-free bug when trimming caps

 fs/ceph/caps.c       |  2 +-
 fs/ceph/debugfs.c    | 18 +++++++----
 fs/ceph/mds_client.c | 72 ++++++++++++++++++++++++++++----------------
 fs/ceph/mds_client.h |  3 +-
 fs/ceph/super.h      |  2 ++
 5 files changed, 62 insertions(+), 35 deletions(-)
Signature · 1 line
2.39.2
#1 lore raw

PATCH v4 1/2 ceph: export __get_cap_for_mds() helper

From: Xiubo Li <[email protected]>

This is one helper without the ci->i_ceph_lock and will be used in another file.

Cc: [email protected]
URL: https://bugzilla.redhat.com/show_bug.cgi?id=2186264
URL: https://tracker.ceph.com/issues/43272

---
 fs/ceph/caps.c  | 2 +-
 fs/ceph/super.h | 2 ++
 2 files changed, 3 insertions(+), 1 deletion(-)
fs/ceph/caps.c +1 -1
index cf29e395af23..2732f46532ec 100644
@@ -432,7 +432,7 @@ void ceph_reservation_status(struct ceph_fs_client *fsc,
  *
  * Called with i_ceph_lock held.
  */
-static struct ceph_cap *__get_cap_for_mds(struct ceph_inode_info *ci, int mds)
+struct ceph_cap *__get_cap_for_mds(struct ceph_inode_info *ci, int mds)
 {
 	struct ceph_cap *cap;
 	struct rb_node *n = ci->i_caps.rb_node;
fs/ceph/super.h +2 -0
index a5e84f8b67ba..a226d36b3ecb 100644
@@ -1225,6 +1225,8 @@ extern void ceph_kick_flushing_caps(struct ceph_mds_client *mdsc,
 				    struct ceph_mds_session *session);
 void ceph_kick_flushing_inode_caps(struct ceph_mds_session *session,
 				   struct ceph_inode_info *ci);
+extern struct ceph_cap *__get_cap_for_mds(struct ceph_inode_info *ci,
+					  int mds);
 extern struct ceph_cap *ceph_get_cap_for_mds(struct ceph_inode_info *ci,
 					     int mds);
 extern void ceph_take_cap_refs(struct ceph_inode_info *ci, int caps,
Signature · 1 line
2.39.2
#2 lore raw

PATCH v4 2/2 ceph: fix potential use-after-free bug when trimming caps

In reply to [email protected]

From: Xiubo Li <[email protected]>

When trimming the caps and just after the 'session->s_cap_lock' is released in ceph_iterate_session_caps() the cap maybe removed by another thread, and when using the stale cap memory in the callbacks it will trigger use-after-free crash.

We need to check the existence of the cap just after the 'ci->i_ceph_lock' being acquired. And do nothing if it's already removed.

Cc: [email protected]
URL: https://bugzilla.redhat.com/show_bug.cgi?id=2186264
URL: https://tracker.ceph.com/issues/43272

---
 fs/ceph/debugfs.c    | 18 +++++++----
 fs/ceph/mds_client.c | 72 ++++++++++++++++++++++++++++----------------
 fs/ceph/mds_client.h |  3 +-
 3 files changed, 59 insertions(+), 34 deletions(-)
fs/ceph/debugfs.c +12 -6
index bec3c4549c07..3904333fa6c3 100644
@@ -248,14 +248,20 @@ static int metrics_caps_show(struct seq_file *s, void *p)
 	return 0;
 }
 
-static int caps_show_cb(struct inode *inode, struct ceph_cap *cap, void *p)
+static int caps_show_cb(struct inode *inode, int mds, void *p)
 {
+	struct ceph_inode_info *ci = ceph_inode(inode);
 	struct seq_file *s = p;
-
-	seq_printf(s, "0x%-17llx%-3d%-17s%-17s\n", ceph_ino(inode),
-		   cap->session->s_mds,
-		   ceph_cap_string(cap->issued),
-		   ceph_cap_string(cap->implemented));
+	struct ceph_cap *cap;
+
+	spin_lock(&ci->i_ceph_lock);
+	cap = __get_cap_for_mds(ci, mds);
+	if (cap)
+		seq_printf(s, "0x%-17llx%-3d%-17s%-17s\n", ceph_ino(inode),
+			   cap->session->s_mds,
+			   ceph_cap_string(cap->issued),
+			   ceph_cap_string(cap->implemented));
+	spin_unlock(&ci->i_ceph_lock);
 	return 0;
 }
 
fs/ceph/mds_client.c +46 -26

161 lines not on this page · raw message

fs/ceph/mds_client.h +1 -2
index 0f70ca3cdb21..c2e77e0306b4 100644
@@ -568,8 +568,7 @@ extern void ceph_flush_cap_releases(struct ceph_mds_client *mdsc,
 extern void ceph_queue_cap_reclaim_work(struct ceph_mds_client *mdsc);
 extern void ceph_reclaim_caps_nr(struct ceph_mds_client *mdsc, int nr);
 extern int ceph_iterate_session_caps(struct ceph_mds_session *session,
-				     int (*cb)(struct inode *,
-					       struct ceph_cap *, void *),
+				     int (*cb)(struct inode *, int mds, void *),
 				     void *arg);
 extern void ceph_mdsc_pre_umount(struct ceph_mds_client *mdsc);
 
Signature · 1 line
2.39.2