Skip to contentSkip to conversation

ceph: do not touch cap when trimming the caps

PATCH In review [email protected] 1 participant 2 messages Apr 14, 2023

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

PATCH 1/1 ceph: do not touch cap when trimming the caps

From: Xiubo Li <[email protected]>

When trimming the caps it maybe queued to release in the next loop, and just after the 'session->s_cap_lock' lock is released the 'session->s_cap_iterator' will be set to NULL and the cap also has been removed from 'session->s_caps' list, then the '__touch_cap()' could continue and add the cap back to the 'session->s_caps' list.

That means this cap could be iterated twice to call 'trim_caps_cb()' and the second time will trigger use-after-free bug.

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

---
 fs/ceph/caps.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
fs/ceph/caps.c +1 -1
index cf29e395af23..186c9818ab0d 100644
@@ -846,7 +846,7 @@ static void __touch_cap(struct ceph_cap *cap)
 	struct ceph_mds_session *s = cap->session;
 
 	spin_lock(&s->s_cap_lock);
-	if (!s->s_cap_iterator) {
+	if (!s->s_cap_iterator && !list_empty(&cap->session_caps) && !cap->queue_release) {
 		dout("__touch_cap %p cap %p mds%d\n", &cap->ci->netfs.inode, cap,
 		     s->s_mds);
 		list_move_tail(&cap->session_caps, &s->s_caps);
Signature · 1 line
2.39.1
author
#1 lore raw
Quoted 23 lines · On 4/14/23 10:41, [email protected] wrote:

From: Xiubo Li <[email protected]>

When trimming the caps it maybe queued to release in the next loop, and just after the 'session->s_cap_lock' lock is released the 'session->s_cap_iterator' will be set to NULL and the cap also has been removed from 'session->s_caps' list, then the '__touch_cap()' could continue and add the cap back to the 'session->s_caps' list.

That means this cap could be iterated twice to call 'trim_caps_cb()' and the second time will trigger use-after-free bug.

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

---
  fs/ceph/caps.c | 2 +-
  1 file changed, 1 insertion(+), 1 deletion(-)
fs/ceph/caps.c +0 -0
index cf29e395af23..186c9818ab0d 100644

fs/ceph/caps.c static void __touch_cap(struct ceph_cap *cap) · line 849

Quoted 5 lines
fs/ceph/caps.c +1 -1
@@ -846,7 +846,7 @@ static void __touch_cap(struct ceph_cap *cap)
  	struct ceph_mds_session *s = cap->session;
 
  	spin_lock(&s->s_cap_lock);
-	if (!s->s_cap_iterator) {
+	if (!s->s_cap_iterator && !list_empty(&cap->session_caps) && !cap->queue_release) {

Comment it myself.

The s_cap_iterator will always be true during trimming the caps. So this check here is incorrect.

Quoted 3 lines
fs/ceph/caps.c +0 -0
  		dout("__touch_cap %p cap %p mds%d\n", &cap->ci->netfs.inode, cap,
  		     s->s_mds);
  		list_move_tail(&cap->session_caps, &s->s_caps);