Skip to contentSkip to conversation

ceph: fix possible NULL pointer dereference for req->r_session

PATCH Superseded Xiubo Li 2 participants 3 messages Apr 14, 2022 – Apr 18, 2022 Ceph client

v1 of 2 Superseded Latest is v2 →

lore
Share & tools
lore

Xiubo Li

author
#0 lore raw

PATCH 1/1 ceph: fix possible NULL pointer dereference for req->r_session

The request will be inserted into the ci->i_unsafe_dirops before assigning the req->r_session, so it's possible that we will hit NULL pointer dereference bug here.

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

---
 fs/ceph/caps.c | 8 ++++----
 1 file changed, 4 insertions(+), 4 deletions(-)
fs/ceph/caps.c +4 -4
index 69af17df59be..6a9bf58478c8 100644
@@ -2333,7 +2333,7 @@ static int unsafe_request_wait(struct inode *inode)
 			list_for_each_entry(req, &ci->i_unsafe_dirops,
 					    r_unsafe_dir_item) {
 				s = req->r_session;
-				if (unlikely(s->s_mds >= max_sessions)) {
+				if (unlikely(s && s->s_mds >= max_sessions)) {
 					spin_unlock(&ci->i_unsafe_lock);
 					for (i = 0; i < max_sessions; i++) {
 						s = sessions[i];
@@ -2343,7 +2343,7 @@ static int unsafe_request_wait(struct inode *inode)
 					kfree(sessions);
 					goto retry;
 				}
-				if (!sessions[s->s_mds]) {
+				if (s && !sessions[s->s_mds]) {
 					s = ceph_get_mds_session(s);
 					sessions[s->s_mds] = s;
 				}
@@ -2353,7 +2353,7 @@ static int unsafe_request_wait(struct inode *inode)
 			list_for_each_entry(req, &ci->i_unsafe_iops,
 					    r_unsafe_target_item) {
 				s = req->r_session;
-				if (unlikely(s->s_mds >= max_sessions)) {
+				if (unlikely(s && s->s_mds >= max_sessions)) {
 					spin_unlock(&ci->i_unsafe_lock);
 					for (i = 0; i < max_sessions; i++) {
 						s = sessions[i];
@@ -2363,7 +2363,7 @@ static int unsafe_request_wait(struct inode *inode)
 					kfree(sessions);
 					goto retry;
 				}
-				if (!sessions[s->s_mds]) {
+				if (s && !sessions[s->s_mds]) {
 					s = ceph_get_mds_session(s);
 					sessions[s->s_mds] = s;
 				}
Signature · 1 line
2.36.0.rc1
#1 lore raw
Quoted 42 lines · Xiubo Li

The request will be inserted into the ci->i_unsafe_dirops before assigning the req->r_session, so it's possible that we will hit NULL pointer dereference bug here.

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

---
 fs/ceph/caps.c | 8 ++++----
 1 file changed, 4 insertions(+), 4 deletions(-)
fs/ceph/caps.c +3 -3
index 69af17df59be..6a9bf58478c8 100644
@@ -2333,7 +2333,7 @@ static int unsafe_request_wait(struct inode *inode)
 			list_for_each_entry(req, &ci->i_unsafe_dirops,
 					    r_unsafe_dir_item) {
 				s = req->r_session;
-				if (unlikely(s->s_mds >= max_sessions)) {
+				if (unlikely(s && s->s_mds >= max_sessions)) {
 					spin_unlock(&ci->i_unsafe_lock);
 					for (i = 0; i < max_sessions; i++) {
 						s = sessions[i];
@@ -2343,7 +2343,7 @@ static int unsafe_request_wait(struct inode *inode)
 					kfree(sessions);
 					goto retry;
 				}
-				if (!sessions[s->s_mds]) {
+				if (s && !sessions[s->s_mds]) {
 					s = ceph_get_mds_session(s);
 					sessions[s->s_mds] = s;
 				}
@@ -2353,7 +2353,7 @@ static int unsafe_request_wait(struct inode *inode)
 			list_for_each_entry(req, &ci->i_unsafe_iops,
 					    r_unsafe_target_item) {
 				s = req->r_session;
-				if (unlikely(s->s_mds >= max_sessions)) {
+				if (unlikely(s && s->s_mds >= max_sessions)) {
 					spin_unlock(&ci->i_unsafe_lock);
 					for (i = 0; i < max_sessions; i++) {
 						s = sessions[i];

fs/ceph/caps.c static int unsafe_request_wait(struct inode *inode) · line 2369

Quoted 8 lines
fs/ceph/caps.c +1 -1
@@ -2363,7 +2363,7 @@ static int unsafe_request_wait(struct inode *inode)
 					kfree(sessions);
 					goto retry;
 				}
-				if (!sessions[s->s_mds]) {
+				if (s && !sessions[s->s_mds]) {
 					s = ceph_get_mds_session(s);
 					sessions[s->s_mds] = s;
 				}

Good catch. I think it'd be cleaner to just do this in each loop though, to keep the if conditions simpler:

    s = req->r_session;
    if (!s)
	    continue;

The bug and fix look real though.

Signature · 1 line
Jeff Layton <[email protected]>
author
#2 lore raw
Quoted 54 lines · Jeff Layton
Quoted 42 lines · Xiubo Li

The request will be inserted into the ci->i_unsafe_dirops before assigning the req->r_session, so it's possible that we will hit NULL pointer dereference bug here.

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

---
  fs/ceph/caps.c | 8 ++++----
  1 file changed, 4 insertions(+), 4 deletions(-)
fs/ceph/caps.c +3 -3
index 69af17df59be..6a9bf58478c8 100644
@@ -2333,7 +2333,7 @@ static int unsafe_request_wait(struct inode *inode)
  			list_for_each_entry(req, &ci->i_unsafe_dirops,
  					    r_unsafe_dir_item) {
  				s = req->r_session;
-				if (unlikely(s->s_mds >= max_sessions)) {
+				if (unlikely(s && s->s_mds >= max_sessions)) {
  					spin_unlock(&ci->i_unsafe_lock);
  					for (i = 0; i < max_sessions; i++) {
  						s = sessions[i];
@@ -2343,7 +2343,7 @@ static int unsafe_request_wait(struct inode *inode)
  					kfree(sessions);
  					goto retry;
  				}
-				if (!sessions[s->s_mds]) {
+				if (s && !sessions[s->s_mds]) {
  					s = ceph_get_mds_session(s);
  					sessions[s->s_mds] = s;
  				}
@@ -2353,7 +2353,7 @@ static int unsafe_request_wait(struct inode *inode)
  			list_for_each_entry(req, &ci->i_unsafe_iops,
  					    r_unsafe_target_item) {
  				s = req->r_session;
-				if (unlikely(s->s_mds >= max_sessions)) {
+				if (unlikely(s && s->s_mds >= max_sessions)) {
  					spin_unlock(&ci->i_unsafe_lock);
  					for (i = 0; i < max_sessions; i++) {
  						s = sessions[i];

fs/ceph/caps.c static int unsafe_request_wait(struct inode *inode) · line 2369

Quoted 8 lines
fs/ceph/caps.c +1 -1
@@ -2363,7 +2363,7 @@ static int unsafe_request_wait(struct inode *inode)
  					kfree(sessions);
  					goto retry;
  				}
-				if (!sessions[s->s_mds]) {
+				if (s && !sessions[s->s_mds]) {
  					s = ceph_get_mds_session(s);
  					sessions[s->s_mds] = s;
  				}

Good catch. I think it'd be cleaner to just do this in each loop though, to keep the if conditions simpler:

Quoted 3 lines
     s = req->r_session;
     if (!s)
	    continue;

Sure, will send the second version.

-- Xiubo

Quoted 1 line

The bug and fix look real though.