Skip to contentSkip to conversation

ceph: increment refcount before usage

PATCH In review [email protected] 3 participants 4 messages Feb 1, 2024 – Feb 2, 2024 Ceph client

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 ceph: increment refcount before usage

From: Rishabh Dave <[email protected]>

In fs/ceph/caps.c, in "encode_cap_msg()", "use after free" error was caught by KASAN at this line - 'ceph_buffer_get(arg->xattr_buf);'. This implies before the refcount could be increment here, it was freed.

In same file, in "handle_cap_grant()" refcount is decremented by this line - "ceph_buffer_put(ci->i_xattrs.blob);". It appears that a race occurred and resource was freed by the latter line before the former line
could increment it.

encode_cap_msg() is called by __send_cap() and __send_cap() is called by ceph_check_caps() after calling __prep_cap(). __prep_cap() is where "arg->xattr_buf" is assigned to "ci->i_xattrs.blob" . This is the spot where the refcount must be increased to prevent "use after free" error.

URL: https://tracker.ceph.com/issues/59259

---
 fs/ceph/caps.c | 3 ++-
 1 file changed, 2 insertions(+), 1 deletion(-)
fs/ceph/caps.c +2 -1
index 5501c86b337d..0ca7dce48172 100644
@@ -1452,7 +1452,7 @@ static void __prep_cap(struct cap_msg_args *arg, struct ceph_cap *cap,
 	if (flushing & CEPH_CAP_XATTR_EXCL) {
 		arg->old_xattr_buf = __ceph_build_xattrs_blob(ci);
 		arg->xattr_version = ci->i_xattrs.version;
-		arg->xattr_buf = ci->i_xattrs.blob;
+		arg->xattr_buf = ceph_buffer_get(ci->i_xattrs.blob);
 	} else {
 		arg->xattr_buf = NULL;
 		arg->old_xattr_buf = NULL;
@@ -1553,6 +1553,7 @@ static void __send_cap(struct cap_msg_args *arg, struct ceph_inode_info *ci)
 	encode_cap_msg(msg, arg);
 	ceph_con_send(&arg->session->s_con, msg);
 	ceph_buffer_put(arg->old_xattr_buf);
+	ceph_buffer_put(arg->xattr_buf);
 	if (arg->wake)
 		wake_up_all(&ci->i_cap_wq);
 }
Signature · 1 line
2.43.0
Reviewed-by on v1 1/1
#1 lore raw
Quoted 37 lines · On Thu, 2024-02-01 at 17:07 +0530, [email protected] wrote:

From: Rishabh Dave <[email protected]>

In fs/ceph/caps.c, in "encode_cap_msg()", "use after free" error was caught by KASAN at this line - 'ceph_buffer_get(arg->xattr_buf);'. This implies before the refcount could be increment here, it was freed.

In same file, in "handle_cap_grant()" refcount is decremented by this line - "ceph_buffer_put(ci->i_xattrs.blob);". It appears that a race occurred and resource was freed by the latter line before the former line
could increment it.

encode_cap_msg() is called by __send_cap() and __send_cap() is called by ceph_check_caps() after calling __prep_cap(). __prep_cap() is where "arg->xattr_buf" is assigned to "ci->i_xattrs.blob" . This is the spot where the refcount must be increased to prevent "use after free" error.

URL: https://tracker.ceph.com/issues/59259

---
 fs/ceph/caps.c | 3 ++-
 1 file changed, 2 insertions(+), 1 deletion(-)
fs/ceph/caps.c +1 -1
index 5501c86b337d..0ca7dce48172 100644
@@ -1452,7 +1452,7 @@ static void __prep_cap(struct cap_msg_args *arg, struct ceph_cap *cap,
 	if (flushing & CEPH_CAP_XATTR_EXCL) {
 		arg->old_xattr_buf = __ceph_build_xattrs_blob(ci);
 		arg->xattr_version = ci->i_xattrs.version;
-		arg->xattr_buf = ci->i_xattrs.blob;
+		arg->xattr_buf = ceph_buffer_get(ci->i_xattrs.blob);
 	} else {
 		arg->xattr_buf = NULL;
 		arg->old_xattr_buf = NULL;

fs/ceph/caps.c static void __send_cap(struct cap_msg_args *arg, struct ceph_inode_info *ci) · line 1559

Quoted 7 lines
fs/ceph/caps.c +1 -0
@@ -1553,6 +1553,7 @@ static void __send_cap(struct cap_msg_args *arg, struct ceph_inode_info *ci)
 	encode_cap_msg(msg, arg);
 	ceph_con_send(&arg->session->s_con, msg);
 	ceph_buffer_put(arg->old_xattr_buf);
+	ceph_buffer_put(arg->xattr_buf);
 	if (arg->wake)
 		wake_up_all(&ci->i_cap_wq);
 }

Nice catch!

Reviewed-by
#2 lore raw
Quoted 37 lines · Jeff Layton
Quoted 36 lines · On Thu, 2024-02-01 at 17:07 +0530, [email protected] wrote:

From: Rishabh Dave <[email protected]>

In fs/ceph/caps.c, in "encode_cap_msg()", "use after free" error was caught by KASAN at this line - 'ceph_buffer_get(arg->xattr_buf);'. This implies before the refcount could be increment here, it was freed.

In same file, in "handle_cap_grant()" refcount is decremented by this line - "ceph_buffer_put(ci->i_xattrs.blob);". It appears that a race occurred and resource was freed by the latter line before the former line
could increment it.

encode_cap_msg() is called by __send_cap() and __send_cap() is called by ceph_check_caps() after calling __prep_cap(). __prep_cap() is where "arg->xattr_buf" is assigned to "ci->i_xattrs.blob" . This is the spot where the refcount must be increased to prevent "use after free" error.

URL: https://tracker.ceph.com/issues/59259

---
  fs/ceph/caps.c | 3 ++-
  1 file changed, 2 insertions(+), 1 deletion(-)
fs/ceph/caps.c +1 -1
index 5501c86b337d..0ca7dce48172 100644
@@ -1452,7 +1452,7 @@ static void __prep_cap(struct cap_msg_args *arg, struct ceph_cap *cap,
  	if (flushing & CEPH_CAP_XATTR_EXCL) {
  		arg->old_xattr_buf = __ceph_build_xattrs_blob(ci);
  		arg->xattr_version = ci->i_xattrs.version;
-		arg->xattr_buf = ci->i_xattrs.blob;
+		arg->xattr_buf = ceph_buffer_get(ci->i_xattrs.blob);
  	} else {
  		arg->xattr_buf = NULL;
  		arg->old_xattr_buf = NULL;
Quoted 8 lines

fs/ceph/caps.c static void __send_cap(struct cap_msg_args *arg, struct ceph_inode_info *ci) · line 1559

Quoted 8 lines
fs/ceph/caps.c +1 -0
@@ -1553,6 +1553,7 @@ static void __send_cap(struct cap_msg_args *arg, struct ceph_inode_info *ci)
  	encode_cap_msg(msg, arg);
  	ceph_con_send(&arg->session->s_con, msg);
  	ceph_buffer_put(arg->old_xattr_buf);
+	ceph_buffer_put(arg->xattr_buf);
  	if (arg->wake)
  		wake_up_all(&ci->i_cap_wq);
  }

Looks good to me.

Quoted 4 lines

Nice catch!

Thanks Jeff.

- Xiubo

#3 lore raw
In reply to [email protected]
Quoted 37 lines · On 2/1/24 19:37, [email protected] wrote:

From: Rishabh Dave <[email protected]>

In fs/ceph/caps.c, in "encode_cap_msg()", "use after free" error was caught by KASAN at this line - 'ceph_buffer_get(arg->xattr_buf);'. This implies before the refcount could be increment here, it was freed.

In same file, in "handle_cap_grant()" refcount is decremented by this line - "ceph_buffer_put(ci->i_xattrs.blob);". It appears that a race occurred and resource was freed by the latter line before the former line
could increment it.

encode_cap_msg() is called by __send_cap() and __send_cap() is called by ceph_check_caps() after calling __prep_cap(). __prep_cap() is where "arg->xattr_buf" is assigned to "ci->i_xattrs.blob" . This is the spot where the refcount must be increased to prevent "use after free" error.

URL: https://tracker.ceph.com/issues/59259

---
  fs/ceph/caps.c | 3 ++-
  1 file changed, 2 insertions(+), 1 deletion(-)
fs/ceph/caps.c +1 -1
index 5501c86b337d..0ca7dce48172 100644
@@ -1452,7 +1452,7 @@ static void __prep_cap(struct cap_msg_args *arg, struct ceph_cap *cap,
  	if (flushing & CEPH_CAP_XATTR_EXCL) {
  		arg->old_xattr_buf = __ceph_build_xattrs_blob(ci);
  		arg->xattr_version = ci->i_xattrs.version;
-		arg->xattr_buf = ci->i_xattrs.blob;
+		arg->xattr_buf = ceph_buffer_get(ci->i_xattrs.blob);
  	} else {
  		arg->xattr_buf = NULL;
  		arg->old_xattr_buf = NULL;

fs/ceph/caps.c static void __send_cap(struct cap_msg_args *arg, struct ceph_inode_info *ci) · line 1559

Quoted 7 lines
fs/ceph/caps.c +1 -0
@@ -1553,6 +1553,7 @@ static void __send_cap(struct cap_msg_args *arg, struct ceph_inode_info *ci)
  	encode_cap_msg(msg, arg);
  	ceph_con_send(&arg->session->s_con, msg);
  	ceph_buffer_put(arg->old_xattr_buf);
+	ceph_buffer_put(arg->xattr_buf);
  	if (arg->wake)
  		wake_up_all(&ci->i_cap_wq);
  }

Applied to the ceph-client's 'testing' branch and will run the tests.

Thanks

- Xiubo