diff mbox series

[v3] ksmbd: fix possible memory leak in smb2_lock()

Message ID 20230207080728.15725-1-hbh25y@gmail.com (mailing list archive)
State New, archived
Headers show
Series [v3] ksmbd: fix possible memory leak in smb2_lock() | expand

Commit Message

Hangyu Hua Feb. 7, 2023, 8:07 a.m. UTC
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")
Signed-off-by: Hangyu Hua <hbh25y@gmail.com>
---

v2: avoid NULL pointer dereference in set_close_state_blocked_works()
v3: avoid race condition between smb2_lock() and smb2_cancel()

 fs/ksmbd/smb2pdu.c   | 23 ++++++++++++++---------
 fs/ksmbd/vfs_cache.c |  2 ++
 2 files changed, 16 insertions(+), 9 deletions(-)

Comments

Namjae Jeon Feb. 13, 2023, 11:35 p.m. UTC | #1
2023-02-07 17:07 GMT+09:00, Hangyu Hua <hbh25y@gmail.com>:
> 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")
> Signed-off-by: Hangyu Hua <hbh25y@gmail.com>
> ---
>
> v2: avoid NULL pointer dereference in set_close_state_blocked_works()
> v3: avoid race condition between smb2_lock() and smb2_cancel()
>
>  fs/ksmbd/smb2pdu.c   | 23 ++++++++++++++---------
>  fs/ksmbd/vfs_cache.c |  2 ++
>  2 files changed, 16 insertions(+), 9 deletions(-)
>
> diff --git a/fs/ksmbd/smb2pdu.c b/fs/ksmbd/smb2pdu.c
> index d681f91947d9..f4079518eaf6 100644
> --- a/fs/ksmbd/smb2pdu.c
> +++ b/fs/ksmbd/smb2pdu.c
> @@ -6644,7 +6644,7 @@ int smb2_cancel(struct ksmbd_work *work)
>  	struct ksmbd_conn *conn = work->conn;
>  	struct smb2_hdr *hdr = smb2_get_msg(work->request_buf);
>  	struct smb2_hdr *chdr;
> -	struct ksmbd_work *cancel_work = NULL, *iter;
> +	struct ksmbd_work *iter;
>  	struct list_head *command_list;
>
>  	ksmbd_debug(SMB, "smb2 cancel called on mid %llu, async flags 0x%x\n",
> @@ -6666,7 +6666,9 @@ int smb2_cancel(struct ksmbd_work *work)
>  				    "smb2 with AsyncId %llu cancelled command = 0x%x\n",
>  				    le64_to_cpu(hdr->Id.AsyncId),
>  				    le16_to_cpu(chdr->Command));
> -			cancel_work = iter;
> +			iter->state = KSMBD_WORK_CANCELLED;
> +			if (iter->cancel_fn)
> +				iter->cancel_fn(iter->cancel_argv);
>  			break;
>  		}
>  		spin_unlock(&conn->request_lock);
> @@ -6685,18 +6687,12 @@ int smb2_cancel(struct ksmbd_work *work)
>  				    "smb2 with mid %llu cancelled command = 0x%x\n",
>  				    le64_to_cpu(hdr->MessageId),
>  				    le16_to_cpu(chdr->Command));
> -			cancel_work = iter;
> +			iter->state = KSMBD_WORK_CANCELLED;
>  			break;
>  		}
>  		spin_unlock(&conn->request_lock);
>  	}
>
> -	if (cancel_work) {
> -		cancel_work->state = KSMBD_WORK_CANCELLED;
> -		if (cancel_work->cancel_fn)
> -			cancel_work->cancel_fn(cancel_work->cancel_argv);
> -	}
> -
>  	/* For SMB2_CANCEL command itself send no response*/
>  	work->send_no_response = 1;
>  	return 0;
> @@ -7050,6 +7046,7 @@ int smb2_lock(struct ksmbd_work *work)
>  						      smb2_remove_blocked_lock,
>  						      argv);
>  				if (rc) {
> +					kfree(argv);
>  					err = -ENOMEM;
>  					goto out;
>  				}
> @@ -7061,6 +7058,10 @@ int smb2_lock(struct ksmbd_work *work)
>
>  				ksmbd_vfs_posix_lock_wait(flock);
>
> +				spin_lock(&work->conn->request_lock);
> +				list_del_init(&work->async_request_entry);
It is called again in ksmbd_conn_try_dequeue_request().
> +				spin_unlock(&work->conn->request_lock);
> +
diff mbox series

Patch

diff --git a/fs/ksmbd/smb2pdu.c b/fs/ksmbd/smb2pdu.c
index d681f91947d9..f4079518eaf6 100644
--- a/fs/ksmbd/smb2pdu.c
+++ b/fs/ksmbd/smb2pdu.c
@@ -6644,7 +6644,7 @@  int smb2_cancel(struct ksmbd_work *work)
 	struct ksmbd_conn *conn = work->conn;
 	struct smb2_hdr *hdr = smb2_get_msg(work->request_buf);
 	struct smb2_hdr *chdr;
-	struct ksmbd_work *cancel_work = NULL, *iter;
+	struct ksmbd_work *iter;
 	struct list_head *command_list;
 
 	ksmbd_debug(SMB, "smb2 cancel called on mid %llu, async flags 0x%x\n",
@@ -6666,7 +6666,9 @@  int smb2_cancel(struct ksmbd_work *work)
 				    "smb2 with AsyncId %llu cancelled command = 0x%x\n",
 				    le64_to_cpu(hdr->Id.AsyncId),
 				    le16_to_cpu(chdr->Command));
-			cancel_work = iter;
+			iter->state = KSMBD_WORK_CANCELLED;
+			if (iter->cancel_fn)
+				iter->cancel_fn(iter->cancel_argv);
 			break;
 		}
 		spin_unlock(&conn->request_lock);
@@ -6685,18 +6687,12 @@  int smb2_cancel(struct ksmbd_work *work)
 				    "smb2 with mid %llu cancelled command = 0x%x\n",
 				    le64_to_cpu(hdr->MessageId),
 				    le16_to_cpu(chdr->Command));
-			cancel_work = iter;
+			iter->state = KSMBD_WORK_CANCELLED;
 			break;
 		}
 		spin_unlock(&conn->request_lock);
 	}
 
-	if (cancel_work) {
-		cancel_work->state = KSMBD_WORK_CANCELLED;
-		if (cancel_work->cancel_fn)
-			cancel_work->cancel_fn(cancel_work->cancel_argv);
-	}
-
 	/* For SMB2_CANCEL command itself send no response*/
 	work->send_no_response = 1;
 	return 0;
@@ -7050,6 +7046,7 @@  int smb2_lock(struct ksmbd_work *work)
 						      smb2_remove_blocked_lock,
 						      argv);
 				if (rc) {
+					kfree(argv);
 					err = -ENOMEM;
 					goto out;
 				}
@@ -7061,6 +7058,10 @@  int smb2_lock(struct ksmbd_work *work)
 
 				ksmbd_vfs_posix_lock_wait(flock);
 
+				spin_lock(&work->conn->request_lock);
+				list_del_init(&work->async_request_entry);
+				spin_unlock(&work->conn->request_lock);
+
 				if (work->state != KSMBD_WORK_ACTIVE) {
 					list_del(&smb_lock->llist);
 					spin_lock(&work->conn->llist_lock);
@@ -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);
+						work->cancel_fn = NULL;
+						kfree(argv);
 						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);
+				work->cancel_fn = NULL;
+				kfree(argv);
 				goto retry;
 			} else if (!rc) {
 				spin_lock(&work->conn->llist_lock);
diff --git a/fs/ksmbd/vfs_cache.c b/fs/ksmbd/vfs_cache.c
index da9163b00350..761a8aa540ce 100644
--- a/fs/ksmbd/vfs_cache.c
+++ b/fs/ksmbd/vfs_cache.c
@@ -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);
+		cancel_work->cancel_fn = NULL;
+		kfree(cancel_work->cancel_argv);
 	}
 	spin_unlock(&fp->f_lock);
 }