diff mbox series

ksmbd: fix memory leak in parse_lease_state()

Message ID 20250430031623.744547-1-wangzhaolong1@huawei.com
State New
Headers show
Series ksmbd: fix memory leak in parse_lease_state() | expand

Commit Message

Wang Zhaolong April 30, 2025, 3:16 a.m. UTC
The previous patch that added bounds check for create lease context
introduced a memory leak. When the bounds check fails, the function
returns NULL without freeing the previously allocated lease_ctx_info
structure.

This patch fixes the issue by adding kfree(lreq) before returning NULL
in both boundary check cases.

Fixes: bab703ed8472 ("ksmbd: add bounds check for create lease context")
Signed-off-by: Wang Zhaolong <wangzhaolong1@huawei.com>
---
 fs/smb/server/oplock.c | 7 +++++--
 1 file changed, 5 insertions(+), 2 deletions(-)

Comments

Namjae Jeon April 30, 2025, 5:43 a.m. UTC | #1
On Wed, Apr 30, 2025 at 12:16 PM Wang Zhaolong <wangzhaolong1@huawei.com> wrote:
>
> The previous patch that added bounds check for create lease context
> introduced a memory leak. When the bounds check fails, the function
> returns NULL without freeing the previously allocated lease_ctx_info
> structure.
>
> This patch fixes the issue by adding kfree(lreq) before returning NULL
> in both boundary check cases.
>
> Fixes: bab703ed8472 ("ksmbd: add bounds check for create lease context")
> Signed-off-by: Wang Zhaolong <wangzhaolong1@huawei.com>
Applied it to #ksmbd-for-next-next.
Thanks!
diff mbox series

Patch

diff --git a/fs/smb/server/oplock.c b/fs/smb/server/oplock.c
index 81a29857b1e3..03f606afad93 100644
--- a/fs/smb/server/oplock.c
+++ b/fs/smb/server/oplock.c
@@ -1494,11 +1494,11 @@  struct lease_ctx_info *parse_lease_state(void *open_req)
 	if (sizeof(struct lease_context_v2) == le32_to_cpu(cc->DataLength)) {
 		struct create_lease_v2 *lc = (struct create_lease_v2 *)cc;
 
 		if (le16_to_cpu(cc->DataOffset) + le32_to_cpu(cc->DataLength) <
 		    sizeof(struct create_lease_v2) - 4)
-			return NULL;
+			goto err_out;
 
 		memcpy(lreq->lease_key, lc->lcontext.LeaseKey, SMB2_LEASE_KEY_SIZE);
 		lreq->req_state = lc->lcontext.LeaseState;
 		lreq->flags = lc->lcontext.LeaseFlags;
 		lreq->epoch = lc->lcontext.Epoch;
@@ -1510,19 +1510,22 @@  struct lease_ctx_info *parse_lease_state(void *open_req)
 	} else {
 		struct create_lease *lc = (struct create_lease *)cc;
 
 		if (le16_to_cpu(cc->DataOffset) + le32_to_cpu(cc->DataLength) <
 		    sizeof(struct create_lease))
-			return NULL;
+			goto err_out;
 
 		memcpy(lreq->lease_key, lc->lcontext.LeaseKey, SMB2_LEASE_KEY_SIZE);
 		lreq->req_state = lc->lcontext.LeaseState;
 		lreq->flags = lc->lcontext.LeaseFlags;
 		lreq->duration = lc->lcontext.LeaseDuration;
 		lreq->version = 1;
 	}
 	return lreq;
+err_out:
+	kfree(lreq);
+	return NULL;
 }
 
 /**
  * smb2_find_context_vals() - find a particular context info in open request
  * @open_req:	buffer containing smb2 file open(create) request