Skip to contentSkip to conversation

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

PATCH In review Xiubo Li 4 participants 9 messages Apr 18, 2022 – Apr 25, 2022 Ceph client

lore
Share & tools
lore

Xiubo Li

author
#0 lore raw

PATCH v2 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 | 4 ++++
 1 file changed, 4 insertions(+)
fs/ceph/caps.c +4 -0
index 69af17df59be..c70fd747c914 100644
@@ -2333,6 +2333,8 @@ 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 (!s)
+					continue;
 				if (unlikely(s->s_mds >= max_sessions)) {
 					spin_unlock(&ci->i_unsafe_lock);
 					for (i = 0; i < max_sessions; i++) {
@@ -2353,6 +2355,8 @@ 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 (!s)
+					continue;
 				if (unlikely(s->s_mds >= max_sessions)) {
 					spin_unlock(&ci->i_unsafe_lock);
 					for (i = 0; i < max_sessions; i++) {
Signature · 1 line
2.36.0.rc1
Reviewed-by on v2 1/1
#1 lore raw
Quoted 24 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 | 4 ++++
 1 file changed, 4 insertions(+)
fs/ceph/caps.c +2 -0
index 69af17df59be..c70fd747c914 100644
@@ -2333,6 +2333,8 @@ 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 (!s)
+					continue;
 				if (unlikely(s->s_mds >= max_sessions)) {
 					spin_unlock(&ci->i_unsafe_lock);
 					for (i = 0; i < max_sessions; i++) {

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

Quoted 8 lines
fs/ceph/caps.c +2 -0
@@ -2353,6 +2355,8 @@ 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 (!s)
+					continue;
 				if (unlikely(s->s_mds >= max_sessions)) {
 					spin_unlock(&ci->i_unsafe_lock);
 					for (i = 0; i < max_sessions; i++) {
Reviewed-by on v2 1/1
#2 lore raw
In reply to Xiubo Li
Quoted 34 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 | 4 ++++
 1 file changed, 4 insertions(+)
fs/ceph/caps.c +4 -0
index 69af17df59be..c70fd747c914 100644
@@ -2333,6 +2333,8 @@ 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 (!s)
+					continue;
 				if (unlikely(s->s_mds >= max_sessions)) {
 					spin_unlock(&ci->i_unsafe_lock);
 					for (i = 0; i < max_sessions; i++) {
@@ -2353,6 +2355,8 @@ 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 (!s)
+					continue;
 				if (unlikely(s->s_mds >= max_sessions)) {
 					spin_unlock(&ci->i_unsafe_lock);
 					for (i = 0; i < max_sessions; i++) {
Signature · 1 line
2.36.0.rc1

Thanks Xiubo!

Signature · 1 line
Aaron Tomlin
Tested-by on v2 1/1
#3 lore raw
In reply to Xiubo Li
Quoted 34 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 | 4 ++++
 1 file changed, 4 insertions(+)
fs/ceph/caps.c +4 -0
index 69af17df59be..c70fd747c914 100644
@@ -2333,6 +2333,8 @@ 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 (!s)
+					continue;
 				if (unlikely(s->s_mds >= max_sessions)) {
 					spin_unlock(&ci->i_unsafe_lock);
 					for (i = 0; i < max_sessions; i++) {
@@ -2353,6 +2355,8 @@ 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 (!s)
+					continue;
 				if (unlikely(s->s_mds >= max_sessions)) {
 					spin_unlock(&ci->i_unsafe_lock);
 					for (i = 0; i < max_sessions; i++) {
Signature · 1 line
2.36.0.rc1
Signature · 1 line
Aaron Tomlin
author
#4 lore raw
Quoted 28 lines · Aaron Tomlin
Quoted 27 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 | 4 ++++
  1 file changed, 4 insertions(+)
fs/ceph/caps.c +2 -0
index 69af17df59be..c70fd747c914 100644
@@ -2333,6 +2333,8 @@ 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 (!s)
+					continue;
  				if (unlikely(s->s_mds >= max_sessions)) {
  					spin_unlock(&ci->i_unsafe_lock);
  					for (i = 0; i < max_sessions; i++) {
@@ -2353,6 +2355,8 @@ 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;
Quoted 9 lines
Quoted 7 lines
fs/ceph/caps.c +2 -0
+				if (!s)
+					continue;
  				if (unlikely(s->s_mds >= max_sessions)) {
  					spin_unlock(&ci->i_unsafe_lock);
  					for (i = 0; i < max_sessions; i++) {
Signature · 1 line
2.36.0.rc1

Hi Aaron,

Thanks very much for you testing.

BTW, did you test this by using Livepatch or something else ?

-- Xiubo

#5 lore raw
Quoted 1 line · Xiubo Li

Hi Aaron,

Hi Xiubo,

Quoted 1 line

Thanks very much for you testing.

No problem!

Quoted 1 line

BTW, did you test this by using Livepatch or something else ?

I mostly followed your suggestion here [1] by modifying/or patching the kernel to increase the race window so that unsafe_request_wait() may more reliably see a newly registered request with an unprepared session pointer i.e. 'req->r_session == NULL'.

Indeed, with this patch we simply skip such a request while traversing the Ceph inode's unsafe directory list in the context of unsafe_request_wait().

[1]: https://tracker.ceph.com/issues/55329

Kind regards,

Signature · 1 line
Aaron Tomlin
author
#6 lore raw
Quoted 7 lines · Aaron Tomlin
Quoted 1 line · Xiubo Li

Hi Aaron,

Hi Xiubo,

Quoted 1 line

Thanks very much for you testing.

No problem!

Quoted 8 lines
Quoted 1 line

BTW, did you test this by using Livepatch or something else ?

I mostly followed your suggestion here [1] by modifying/or patching the kernel to increase the race window so that unsafe_request_wait() may more reliably see a newly registered request with an unprepared session pointer i.e. 'req->r_session == NULL'.

Indeed, with this patch we simply skip such a request while traversing the Ceph inode's unsafe directory list in the context of unsafe_request_wait().

Okay, cool.

Thanks again for your help Aaron :-)

-- Xiubo

Quoted 4 lines

[1]: https://tracker.ceph.com/issues/55329

Kind regards,

maintainer
#7 lore raw
Quoted 16 lines · Xiubo Li
Quoted 13 lines · Aaron Tomlin
Quoted 1 line · Xiubo Li

Hi Aaron,

Hi Xiubo,

Quoted 1 line

Thanks very much for you testing.

No problem!

Quoted 1 line

BTW, did you test this by using Livepatch or something else ?

I mostly followed your suggestion here [1] by modifying/or patching the kernel to increase the race window so that unsafe_request_wait() may more reliably see a newly registered request with an unprepared session pointer i.e. 'req->r_session == NULL'.

Quoted 15 lines
Quoted 2 lines

Indeed, with this patch we simply skip such a request while traversing the Ceph inode's unsafe directory list in the context of unsafe_request_wait().

Okay, cool.

Thanks again for your help Aaron :-)

-- Xiubo

Quoted 4 lines

[1]: https://tracker.ceph.com/issues/55329

Kind regards,

I went ahead and marked this for stable (it's already queued for -rc5).

Thanks,

Ilya

author
#8 lore raw
Quoted 22 lines · Ilya Dryomov
Quoted 21 lines · Xiubo Li
Quoted 7 lines · Aaron Tomlin
Quoted 1 line · Xiubo Li

Hi Aaron,

Hi Xiubo,

Quoted 1 line

Thanks very much for you testing.

No problem!

Quoted 8 lines
Quoted 1 line

BTW, did you test this by using Livepatch or something else ?

I mostly followed your suggestion here [1] by modifying/or patching the kernel to increase the race window so that unsafe_request_wait() may more reliably see a newly registered request with an unprepared session pointer i.e. 'req->r_session == NULL'.

Indeed, with this patch we simply skip such a request while traversing the Ceph inode's unsafe directory list in the context of unsafe_request_wait().

Okay, cool.

Thanks again for your help Aaron :-)

Quoted 8 lines
Quoted 7 lines

-- Xiubo

Quoted 4 lines

[1]: https://tracker.ceph.com/issues/55329

Kind regards,

I went ahead and marked this for stable (it's already queued for -rc5).

Sure, thanks Ilya.

-- Xiubo

Quoted 4 lines

Thanks,

Ilya