Skip to contentSkip to conversation

ksmbd: fix possible memory leak in smb2_lock()

PATCH Superseded Hangyu Hua 2 participants 4 messages Feb 6, 2023 – Feb 7, 2023

Subsystems (2)

v2 of 5 Superseded Latest is v5 →

lore
Share & tools
lore

Hangyu Hua

author
#0 lore raw

PATCH v2 1/1 ksmbd: fix possible memory leak in smb2_lock()

argv needs to be free when setup_async_work fails or when the current process is woken up.

Fixes e2f34481b24d ("cifsd: add server-side procedures for SMB3")

---

v2: avoid NULL pointer dereference in set_close_state_blocked_works()

 fs/ksmbd/smb2pdu.c   | 5 +++++
 fs/ksmbd/vfs_cache.c | 2 ++
 2 files changed, 7 insertions(+)
fs/ksmbd/smb2pdu.c +5 -0
index d681f91947d9..177a24704021 100644
@@ -7050,6 +7050,7 @@ int smb2_lock(struct ksmbd_work *work)
 						      smb2_remove_blocked_lock,
 						      argv);
 				if (rc) {
+					kfree(argv);
 					err = -ENOMEM;
 					goto out;
 				}
@@ -7072,6 +7073,8 @@ int smb2_lock(struct ksmbd_work *work)
 						spin_lock(&fp->f_lock);
 						list_del(&work->fp_entry);
 						spin_unlock(&fp->f_lock);
+						kfree(argv);
+						work->cancel_fn = NULL;
 						rsp->hdr.Status =
 							STATUS_CANCELLED;
 						kfree(smb_lock);
@@ -7096,6 +7099,8 @@ int smb2_lock(struct ksmbd_work *work)
 				spin_lock(&fp->f_lock);
 				list_del(&work->fp_entry);
 				spin_unlock(&fp->f_lock);
+				kfree(argv);
+				work->cancel_fn = NULL;
 				goto retry;
 			} else if (!rc) {
 				spin_lock(&work->conn->llist_lock);
fs/ksmbd/vfs_cache.c +2 -0
index da9163b00350..eb95c16393b7 100644
@@ -372,6 +372,8 @@ static void set_close_state_blocked_works(struct ksmbd_file *fp)
 		list_del(&cancel_work->fp_entry);
 		cancel_work->state = KSMBD_WORK_CLOSED;
 		cancel_work->cancel_fn(cancel_work->cancel_argv);
+		kfree(cancel_work->cancel_argv);
+		cancel_work->cancel_fn = NULL;
 	}
 	spin_unlock(&fp->f_lock);
 }
Signature · 1 line
2.34.1
maintainer
#1 lore raw

2023-02-06 11:36 GMT+09:00, Hangyu Hua <[email protected]>:

Quoted 35 lines

argv needs to be free when setup_async_work fails or when the current process is woken up.

Fixes e2f34481b24d ("cifsd: add server-side procedures for SMB3")

---

v2: avoid NULL pointer dereference in set_close_state_blocked_works()

 fs/ksmbd/smb2pdu.c   | 5 +++++
 fs/ksmbd/vfs_cache.c | 2 ++
 2 files changed, 7 insertions(+)
fs/ksmbd/smb2pdu.c +3 -0
index d681f91947d9..177a24704021 100644
@@ -7050,6 +7050,7 @@ int smb2_lock(struct ksmbd_work *work)
 						      smb2_remove_blocked_lock,
 						      argv);
 				if (rc) {
+					kfree(argv);
 					err = -ENOMEM;
 					goto out;
 				}
@@ -7072,6 +7073,8 @@ int smb2_lock(struct ksmbd_work *work)
 						spin_lock(&fp->f_lock);
 						list_del(&work->fp_entry);
 						spin_unlock(&fp->f_lock);
+						kfree(argv);
+						work->cancel_fn = NULL;
 						rsp->hdr.Status =
 							STATUS_CANCELLED;
 						kfree(smb_lock);

fs/ksmbd/smb2pdu.c int smb2_lock(struct ksmbd_work *work) · line 7103

Quoted 5 lines
fs/ksmbd/smb2pdu.c +2 -0
@@ -7096,6 +7099,8 @@ int smb2_lock(struct ksmbd_work *work)
 				spin_lock(&fp->f_lock);
 				list_del(&work->fp_entry);
 				spin_unlock(&fp->f_lock);
+				kfree(argv);
+				work->cancel_fn = NULL;

This doesn't seem so simple... You have to consider the racy issue between this change and smb2_cancel(). Also, how are you testing this patch?

Quoted 21 lines
fs/ksmbd/smb2pdu.c +0 -0
 				goto retry;
 			} else if (!rc) {
 				spin_lock(&work->conn->llist_lock);
fs/ksmbd/vfs_cache.c +2 -0
index da9163b00350..eb95c16393b7 100644
@@ -372,6 +372,8 @@ static void set_close_state_blocked_works(struct
 ksmbd_file *fp)
 		list_del(&cancel_work->fp_entry);
 		cancel_work->state = KSMBD_WORK_CLOSED;
 		cancel_work->cancel_fn(cancel_work->cancel_argv);
+		kfree(cancel_work->cancel_argv);
+		cancel_work->cancel_fn = NULL;
 	}
 	spin_unlock(&fp->f_lock);

}
--
2.34.1

author
#2 lore raw
Quoted 36 lines · Namjae Jeon

2023-02-06 11:36 GMT+09:00, Hangyu Hua <[email protected]>:

Quoted 35 lines

argv needs to be free when setup_async_work fails or when the current process is woken up.

Fixes e2f34481b24d ("cifsd: add server-side procedures for SMB3")

---

v2: avoid NULL pointer dereference in set_close_state_blocked_works()

  fs/ksmbd/smb2pdu.c   | 5 +++++
  fs/ksmbd/vfs_cache.c | 2 ++
  2 files changed, 7 insertions(+)
fs/ksmbd/smb2pdu.c +3 -0
index d681f91947d9..177a24704021 100644
@@ -7050,6 +7050,7 @@ int smb2_lock(struct ksmbd_work *work)
  						      smb2_remove_blocked_lock,
  						      argv);
  				if (rc) {
+					kfree(argv);
  					err = -ENOMEM;
  					goto out;
  				}
@@ -7072,6 +7073,8 @@ int smb2_lock(struct ksmbd_work *work)
  						spin_lock(&fp->f_lock);
  						list_del(&work->fp_entry);
  						spin_unlock(&fp->f_lock);
+						kfree(argv);
+						work->cancel_fn = NULL;
  						rsp->hdr.Status =
  							STATUS_CANCELLED;
  						kfree(smb_lock);
@@ -7096,6 +7099,8 @@ int smb2_lock(struct ksmbd_work *work)
Quoted 9 lines

fs/ksmbd/smb2pdu.c int smb2_lock(struct ksmbd_work *work) · line 7077

Quoted 5 lines
fs/ksmbd/smb2pdu.c +2 -0
  				spin_lock(&fp->f_lock);
  				list_del(&work->fp_entry);
  				spin_unlock(&fp->f_lock);
+				kfree(argv);
+				work->cancel_fn = NULL;

This doesn't seem so simple... You have to consider the racy issue between this change and smb2_cancel(). Also, how are you testing this patch?

Do you means a race condition between smb2_lock() and smb2_cancel()? This look like another bug. This problem exists even if this patch does not exist. If state becomes KSMBD_WORK_CANCELLED we shouldn't go retry.

But you are right. This patch will cause UAF in race condition. I think setting cancel_fn to NULL before releasing argv can fix this UAF. After this memory leak is solved we should use another patch to fix the race condition. What do you think?

Thanks,
Hangyu

Quoted 21 lines
Quoted 21 lines
fs/ksmbd/smb2pdu.c +0 -0
  				goto retry;
  			} else if (!rc) {
  				spin_lock(&work->conn->llist_lock);
fs/ksmbd/vfs_cache.c +2 -0
index da9163b00350..eb95c16393b7 100644
@@ -372,6 +372,8 @@ static void set_close_state_blocked_works(struct
 ksmbd_file *fp)
  		list_del(&cancel_work->fp_entry);
  		cancel_work->state = KSMBD_WORK_CLOSED;
  		cancel_work->cancel_fn(cancel_work->cancel_argv);
+		kfree(cancel_work->cancel_argv);
+		cancel_work->cancel_fn = NULL;
  	}
  	spin_unlock(&fp->f_lock);

}
--
2.34.1

maintainer
#3 lore raw

2023-02-07 12:22 GMT+09:00, Hangyu Hua <[email protected]>:

Quoted 47 lines · Namjae Jeon
Quoted 36 lines

2023-02-06 11:36 GMT+09:00, Hangyu Hua <[email protected]>:

Quoted 35 lines

argv needs to be free when setup_async_work fails or when the current process is woken up.

Fixes e2f34481b24d ("cifsd: add server-side procedures for SMB3")

---

v2: avoid NULL pointer dereference in set_close_state_blocked_works()

  fs/ksmbd/smb2pdu.c   | 5 +++++
  fs/ksmbd/vfs_cache.c | 2 ++
  2 files changed, 7 insertions(+)
fs/ksmbd/smb2pdu.c +3 -0
index d681f91947d9..177a24704021 100644
@@ -7050,6 +7050,7 @@ int smb2_lock(struct ksmbd_work *work)
  						      smb2_remove_blocked_lock,
  						      argv);
  				if (rc) {
+					kfree(argv);
  					err = -ENOMEM;
  					goto out;
  				}
@@ -7072,6 +7073,8 @@ int smb2_lock(struct ksmbd_work *work)
  						spin_lock(&fp->f_lock);
  						list_del(&work->fp_entry);
  						spin_unlock(&fp->f_lock);
+						kfree(argv);
+						work->cancel_fn = NULL;
  						rsp->hdr.Status =
  							STATUS_CANCELLED;
  						kfree(smb_lock);
@@ -7096,6 +7099,8 @@ int smb2_lock(struct ksmbd_work *work)
Quoted 9 lines

fs/ksmbd/smb2pdu.c int smb2_lock(struct ksmbd_work *work) · line 7077

Quoted 5 lines
fs/ksmbd/smb2pdu.c +2 -0
  				spin_lock(&fp->f_lock);
  				list_del(&work->fp_entry);
  				spin_unlock(&fp->f_lock);
+				kfree(argv);
+				work->cancel_fn = NULL;

This doesn't seem so simple... You have to consider the racy issue between this change and smb2_cancel(). Also, how are you testing this patch?

Quoted 6 lines

Do you means a race condition between smb2_lock() and smb2_cancel()? This look like another bug. This problem exists even if this patch does not exist. If state becomes KSMBD_WORK_CANCELLED we shouldn't go retry.

But you are right. This patch will cause UAF in race condition. I think setting cancel_fn to NULL before releasing argv can fix this UAF.

I don't know until I see your updated patch. It may be protected by lock.

Quoted 3 lines

After
this memory leak is solved we should use another patch to fix the race condition. What do you think?

It doesn't matter whether it's a single patch or a multi-patches, as long as this patch doesn't cause racy issue...

Thanks.

Quoted 26 lines

Thanks,
Hangyu

Quoted 21 lines
Quoted 21 lines
fs/ksmbd/smb2pdu.c +0 -0
  				goto retry;
  			} else if (!rc) {
  				spin_lock(&work->conn->llist_lock);
fs/ksmbd/vfs_cache.c +2 -0
index da9163b00350..eb95c16393b7 100644
@@ -372,6 +372,8 @@ static void set_close_state_blocked_works(struct
 ksmbd_file *fp)
  		list_del(&cancel_work->fp_entry);
  		cancel_work->state = KSMBD_WORK_CLOSED;
  		cancel_work->cancel_fn(cancel_work->cancel_argv);
+		kfree(cancel_work->cancel_argv);
+		cancel_work->cancel_fn = NULL;
  	}
  	spin_unlock(&fp->f_lock);

}
--
2.34.1