linux-kernel.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
* [PATCH] ksmbd: prevent memory leak on error return
@ 2023-11-08  8:15 Zongmin Zhou
  2023-11-08 10:36 ` Namjae Jeon
  0 siblings, 1 reply; 8+ messages in thread
From: Zongmin Zhou @ 2023-11-08  8:15 UTC (permalink / raw)
  To: linkinjeon, sfrench
  Cc: senozhatsky, tom, linux-cifs, linux-kernel, Zongmin Zhou,
	kernel test robot, Dan Carpenter, Zongmin Zhou

When allocated memory for 'new' failed,just return
will cause memory leak of 'ar'.

Fixes: 1819a9042999 ("ksmbd: reorganize ksmbd_iov_pin_rsp()")

Reported-by: kernel test robot <lkp@intel.com>
Reported-by: Dan Carpenter <error27@gmail.com>
Closes: https://lore.kernel.org/r/202311031837.H3yo7JVl-lkp@intel.com/
Signed-off-by: Zongmin Zhou<zhouzongmin@kylinos.cn>
---
 fs/smb/server/ksmbd_work.c | 4 +++-
 1 file changed, 3 insertions(+), 1 deletion(-)

diff --git a/fs/smb/server/ksmbd_work.c b/fs/smb/server/ksmbd_work.c
index a2ed441e837a..dbbef686e160 100644
--- a/fs/smb/server/ksmbd_work.c
+++ b/fs/smb/server/ksmbd_work.c
@@ -123,8 +123,10 @@ static int __ksmbd_iov_pin_rsp(struct ksmbd_work *work, void *ib, int len,
 		new = krealloc(work->iov,
 			       sizeof(struct kvec) * work->iov_alloc_cnt,
 			       GFP_KERNEL | __GFP_ZERO);
-		if (!new)
+		if (!new) {
+			kfree(ar);
 			return -ENOMEM;
+		}
 		work->iov = new;
 	}
 
-- 
2.34.1


^ permalink raw reply related	[flat|nested] 8+ messages in thread

* Re: [PATCH] ksmbd: prevent memory leak on error return
  2023-11-08  8:15 [PATCH] ksmbd: prevent memory leak on error return Zongmin Zhou
@ 2023-11-08 10:36 ` Namjae Jeon
  2023-11-09  1:17   ` [PATCH v2] " Zongmin Zhou
  0 siblings, 1 reply; 8+ messages in thread
From: Namjae Jeon @ 2023-11-08 10:36 UTC (permalink / raw)
  To: Zongmin Zhou
  Cc: sfrench, senozhatsky, tom, linux-cifs, linux-kernel,
	kernel test robot, Dan Carpenter, Zongmin Zhou

2023-11-08 17:15 GMT+09:00, Zongmin Zhou <min_halo@163.com>:
> When allocated memory for 'new' failed,just return
> will cause memory leak of 'ar'.
>
> Fixes: 1819a9042999 ("ksmbd: reorganize ksmbd_iov_pin_rsp()")
>
> Reported-by: kernel test robot <lkp@intel.com>
> Reported-by: Dan Carpenter <error27@gmail.com>
> Closes: https://lore.kernel.org/r/202311031837.H3yo7JVl-lkp@intel.com/
> Signed-off-by: Zongmin Zhou<zhouzongmin@kylinos.cn>
> ---
>  fs/smb/server/ksmbd_work.c | 4 +++-
>  1 file changed, 3 insertions(+), 1 deletion(-)
>
> diff --git a/fs/smb/server/ksmbd_work.c b/fs/smb/server/ksmbd_work.c
> index a2ed441e837a..dbbef686e160 100644
> --- a/fs/smb/server/ksmbd_work.c
> +++ b/fs/smb/server/ksmbd_work.c
> @@ -123,8 +123,10 @@ static int __ksmbd_iov_pin_rsp(struct ksmbd_work *work,
> void *ib, int len,
>  		new = krealloc(work->iov,
>  			       sizeof(struct kvec) * work->iov_alloc_cnt,
>  			       GFP_KERNEL | __GFP_ZERO);
> -		if (!new)
> +		if (!new) {
> +			kfree(ar);
Looks good to me:)
Can you add the below code to rollback ->iov_alloc_cnt ?
                       work->iov_alloc_cnt -= 4;
Thanks!
>  			return -ENOMEM;
> +		}
>  		work->iov = new;
>  	}
>
> --
> 2.34.1
>
>
>

^ permalink raw reply	[flat|nested] 8+ messages in thread

* [PATCH v2] ksmbd: prevent memory leak on error return
  2023-11-08 10:36 ` Namjae Jeon
@ 2023-11-09  1:17   ` Zongmin Zhou
  2023-11-09 13:21     ` Namjae Jeon
  2023-11-19  9:14     ` Pierre Mariani
  0 siblings, 2 replies; 8+ messages in thread
From: Zongmin Zhou @ 2023-11-09  1:17 UTC (permalink / raw)
  To: linkinjeon
  Cc: linux-cifs, linux-kernel, senozhatsky, sfrench, tom,
	Zongmin Zhou, kernel test robot, Dan Carpenter, Zongmin Zhou

When allocated memory for 'new' failed,just return
will cause memory leak of 'ar'.

v2: rollback iov_alloc_cnt when allocate memory failed.

Fixes: 1819a9042999 ("ksmbd: reorganize ksmbd_iov_pin_rsp()")

Reported-by: kernel test robot <lkp@intel.com>
Reported-by: Dan Carpenter <error27@gmail.com>
Closes: https://lore.kernel.org/r/202311031837.H3yo7JVl-lkp@intel.com/
Signed-off-by: Zongmin Zhou<zhouzongmin@kylinos.cn>
---
 fs/smb/server/ksmbd_work.c | 5 ++++-
 1 file changed, 4 insertions(+), 1 deletion(-)

diff --git a/fs/smb/server/ksmbd_work.c b/fs/smb/server/ksmbd_work.c
index a2ed441e837a..44bce4c56daf 100644
--- a/fs/smb/server/ksmbd_work.c
+++ b/fs/smb/server/ksmbd_work.c
@@ -123,8 +123,11 @@ static int __ksmbd_iov_pin_rsp(struct ksmbd_work *work, void *ib, int len,
 		new = krealloc(work->iov,
 			       sizeof(struct kvec) * work->iov_alloc_cnt,
 			       GFP_KERNEL | __GFP_ZERO);
-		if (!new)
+		if (!new) {
+			kfree(ar);
+			work->iov_alloc_cnt -= 4;
 			return -ENOMEM;
+		}
 		work->iov = new;
 	}
 
-- 
2.34.1


^ permalink raw reply related	[flat|nested] 8+ messages in thread

* Re: [PATCH v2] ksmbd: prevent memory leak on error return
  2023-11-09  1:17   ` [PATCH v2] " Zongmin Zhou
@ 2023-11-09 13:21     ` Namjae Jeon
  2023-11-19  9:14     ` Pierre Mariani
  1 sibling, 0 replies; 8+ messages in thread
From: Namjae Jeon @ 2023-11-09 13:21 UTC (permalink / raw)
  To: Zongmin Zhou
  Cc: linux-cifs, linux-kernel, senozhatsky, sfrench, tom,
	kernel test robot, Dan Carpenter, Zongmin Zhou

2023-11-09 10:17 GMT+09:00, Zongmin Zhou <min_halo@163.com>:
> When allocated memory for 'new' failed,just return
> will cause memory leak of 'ar'.
>
> v2: rollback iov_alloc_cnt when allocate memory failed.
>
> Fixes: 1819a9042999 ("ksmbd: reorganize ksmbd_iov_pin_rsp()")
>
> Reported-by: kernel test robot <lkp@intel.com>
> Reported-by: Dan Carpenter <error27@gmail.com>
> Closes: https://lore.kernel.org/r/202311031837.H3yo7JVl-lkp@intel.com/
> Signed-off-by: Zongmin Zhou<zhouzongmin@kylinos.cn>
Acked-by: Namjae Jeon <linkinjeon@kernel.org>

Applied it #ksmbd-for-next-next.
Thanks for your patch.

^ permalink raw reply	[flat|nested] 8+ messages in thread

* Re: [PATCH v2] ksmbd: prevent memory leak on error return
  2023-11-09  1:17   ` [PATCH v2] " Zongmin Zhou
  2023-11-09 13:21     ` Namjae Jeon
@ 2023-11-19  9:14     ` Pierre Mariani
  2023-11-19 14:17       ` Namjae Jeon
  1 sibling, 1 reply; 8+ messages in thread
From: Pierre Mariani @ 2023-11-19  9:14 UTC (permalink / raw)
  To: Zongmin Zhou, linkinjeon
  Cc: linux-cifs, linux-kernel, senozhatsky, sfrench, tom,
	kernel test robot, Dan Carpenter, Zongmin Zhou

On 11/8/2023 5:17 PM, Zongmin Zhou wrote:
> When allocated memory for 'new' failed,just return
> will cause memory leak of 'ar'.
> 
> v2: rollback iov_alloc_cnt when allocate memory failed.
> 
> Fixes: 1819a9042999 ("ksmbd: reorganize ksmbd_iov_pin_rsp()")
> 
> Reported-by: kernel test robot <lkp@intel.com>
> Reported-by: Dan Carpenter <error27@gmail.com>
> Closes: https://lore.kernel.org/r/202311031837.H3yo7JVl-lkp@intel.com/
> Signed-off-by: Zongmin Zhou<zhouzongmin@kylinos.cn>
> ---
>  fs/smb/server/ksmbd_work.c | 5 ++++-
>  1 file changed, 4 insertions(+), 1 deletion(-)
> 
> diff --git a/fs/smb/server/ksmbd_work.c b/fs/smb/server/ksmbd_work.c
> index a2ed441e837a..44bce4c56daf 100644
> --- a/fs/smb/server/ksmbd_work.c
> +++ b/fs/smb/server/ksmbd_work.c
> @@ -123,8 +123,11 @@ static int __ksmbd_iov_pin_rsp(struct ksmbd_work *work, void *ib, int len,
>  		new = krealloc(work->iov,
>  			       sizeof(struct kvec) * work->iov_alloc_cnt,
>  			       GFP_KERNEL | __GFP_ZERO);
> -		if (!new)
> +		if (!new) {
> +			kfree(ar);
> +			work->iov_alloc_cnt -= 4;
>  			return -ENOMEM;
> +		}
>  		work->iov = new;
>  	}
>  

A few lines above, ar is allocated inside the 'if (aux_size)' block.
If aux_size is falsy, isn't it possible that ar will be NULL hence
we should have 'if (ar) kfree(ar);'?  

^ permalink raw reply	[flat|nested] 8+ messages in thread

* Re: [PATCH v2] ksmbd: prevent memory leak on error return
  2023-11-19  9:14     ` Pierre Mariani
@ 2023-11-19 14:17       ` Namjae Jeon
  2023-11-20  1:33         ` Zongmin Zhou
  0 siblings, 1 reply; 8+ messages in thread
From: Namjae Jeon @ 2023-11-19 14:17 UTC (permalink / raw)
  To: Pierre Mariani
  Cc: Zongmin Zhou, linux-cifs, linux-kernel, senozhatsky, sfrench,
	tom, kernel test robot, Dan Carpenter, Zongmin Zhou

2023-11-19 18:14 GMT+09:00, Pierre Mariani <pierre.mariani@gmail.com>:
> On 11/8/2023 5:17 PM, Zongmin Zhou wrote:
>> When allocated memory for 'new' failed,just return
>> will cause memory leak of 'ar'.
>>
>> v2: rollback iov_alloc_cnt when allocate memory failed.
>>
>> Fixes: 1819a9042999 ("ksmbd: reorganize ksmbd_iov_pin_rsp()")
>>
>> Reported-by: kernel test robot <lkp@intel.com>
>> Reported-by: Dan Carpenter <error27@gmail.com>
>> Closes: https://lore.kernel.org/r/202311031837.H3yo7JVl-lkp@intel.com/
>> Signed-off-by: Zongmin Zhou<zhouzongmin@kylinos.cn>
>> ---
>>  fs/smb/server/ksmbd_work.c | 5 ++++-
>>  1 file changed, 4 insertions(+), 1 deletion(-)
>>
>> diff --git a/fs/smb/server/ksmbd_work.c b/fs/smb/server/ksmbd_work.c
>> index a2ed441e837a..44bce4c56daf 100644
>> --- a/fs/smb/server/ksmbd_work.c
>> +++ b/fs/smb/server/ksmbd_work.c
>> @@ -123,8 +123,11 @@ static int __ksmbd_iov_pin_rsp(struct ksmbd_work
>> *work, void *ib, int len,
>>  		new = krealloc(work->iov,
>>  			       sizeof(struct kvec) * work->iov_alloc_cnt,
>>  			       GFP_KERNEL | __GFP_ZERO);
>> -		if (!new)
>> +		if (!new) {
>> +			kfree(ar);
>> +			work->iov_alloc_cnt -= 4;
>>  			return -ENOMEM;
>> +		}
>>  		work->iov = new;
>>  	}
>>
>
> A few lines above, ar is allocated inside the 'if (aux_size)' block.
> If aux_size is falsy, isn't it possible that ar will be NULL hence
> we should have 'if (ar) kfree(ar);'?
We need to initialize ar to NULL on that case. And Passing a NULL
pointer to kfree is safe, So NULL check before kfree() is not needed.

Thanks.
>

^ permalink raw reply	[flat|nested] 8+ messages in thread

* Re: [PATCH v2] ksmbd: prevent memory leak on error return
  2023-11-19 14:17       ` Namjae Jeon
@ 2023-11-20  1:33         ` Zongmin Zhou
  2023-11-20  1:38           ` Namjae Jeon
  0 siblings, 1 reply; 8+ messages in thread
From: Zongmin Zhou @ 2023-11-20  1:33 UTC (permalink / raw)
  To: Namjae Jeon, Pierre Mariani
  Cc: Zongmin Zhou, linux-cifs, linux-kernel, senozhatsky, sfrench,
	tom, kernel test robot, Dan Carpenter


On 2023/11/19 22:17, Namjae Jeon wrote:
> 2023-11-19 18:14 GMT+09:00, Pierre Mariani <pierre.mariani@gmail.com>:
>> On 11/8/2023 5:17 PM, Zongmin Zhou wrote:
>>> When allocated memory for 'new' failed,just return
>>> will cause memory leak of 'ar'.
>>>
>>> v2: rollback iov_alloc_cnt when allocate memory failed.
>>>
>>> Fixes: 1819a9042999 ("ksmbd: reorganize ksmbd_iov_pin_rsp()")
>>>
>>> Reported-by: kernel test robot <lkp@intel.com>
>>> Reported-by: Dan Carpenter <error27@gmail.com>
>>> Closes: https://lore.kernel.org/r/202311031837.H3yo7JVl-lkp@intel.com/
>>> Signed-off-by: Zongmin Zhou<zhouzongmin@kylinos.cn>
>>> ---
>>>   fs/smb/server/ksmbd_work.c | 5 ++++-
>>>   1 file changed, 4 insertions(+), 1 deletion(-)
>>>
>>> diff --git a/fs/smb/server/ksmbd_work.c b/fs/smb/server/ksmbd_work.c
>>> index a2ed441e837a..44bce4c56daf 100644
>>> --- a/fs/smb/server/ksmbd_work.c
>>> +++ b/fs/smb/server/ksmbd_work.c
>>> @@ -123,8 +123,11 @@ static int __ksmbd_iov_pin_rsp(struct ksmbd_work
>>> *work, void *ib, int len,
>>>   		new = krealloc(work->iov,
>>>   			       sizeof(struct kvec) * work->iov_alloc_cnt,
>>>   			       GFP_KERNEL | __GFP_ZERO);
>>> -		if (!new)
>>> +		if (!new) {
>>> +			kfree(ar);
>>> +			work->iov_alloc_cnt -= 4;
>>>   			return -ENOMEM;
>>> +		}
>>>   		work->iov = new;
>>>   	}
>>>
>> A few lines above, ar is allocated inside the 'if (aux_size)' block.
>> If aux_size is falsy, isn't it possible that ar will be NULL hence
>> we should have 'if (ar) kfree(ar);'?
> We need to initialize ar to NULL on that case. And Passing a NULL
> pointer to kfree is safe, So NULL check before kfree() is not needed.
Yes, ar should be initialized to NULL to avoid the case of  aux_size 
will be false.
Since kfree(NULL) is safe.
Should I  send another patch for this?

Best regards!
> Thanks.

^ permalink raw reply	[flat|nested] 8+ messages in thread

* Re: [PATCH v2] ksmbd: prevent memory leak on error return
  2023-11-20  1:33         ` Zongmin Zhou
@ 2023-11-20  1:38           ` Namjae Jeon
  0 siblings, 0 replies; 8+ messages in thread
From: Namjae Jeon @ 2023-11-20  1:38 UTC (permalink / raw)
  To: Zongmin Zhou
  Cc: Pierre Mariani, Zongmin Zhou, linux-cifs, linux-kernel,
	senozhatsky, sfrench, tom, kernel test robot, Dan Carpenter

2023-11-20 10:33 GMT+09:00, Zongmin Zhou <zhouzongmin@kylinos.cn>:
>
> On 2023/11/19 22:17, Namjae Jeon wrote:
>> 2023-11-19 18:14 GMT+09:00, Pierre Mariani <pierre.mariani@gmail.com>:
>>> On 11/8/2023 5:17 PM, Zongmin Zhou wrote:
>>>> When allocated memory for 'new' failed,just return
>>>> will cause memory leak of 'ar'.
>>>>
>>>> v2: rollback iov_alloc_cnt when allocate memory failed.
>>>>
>>>> Fixes: 1819a9042999 ("ksmbd: reorganize ksmbd_iov_pin_rsp()")
>>>>
>>>> Reported-by: kernel test robot <lkp@intel.com>
>>>> Reported-by: Dan Carpenter <error27@gmail.com>
>>>> Closes: https://lore.kernel.org/r/202311031837.H3yo7JVl-lkp@intel.com/
>>>> Signed-off-by: Zongmin Zhou<zhouzongmin@kylinos.cn>
>>>> ---
>>>>   fs/smb/server/ksmbd_work.c | 5 ++++-
>>>>   1 file changed, 4 insertions(+), 1 deletion(-)
>>>>
>>>> diff --git a/fs/smb/server/ksmbd_work.c b/fs/smb/server/ksmbd_work.c
>>>> index a2ed441e837a..44bce4c56daf 100644
>>>> --- a/fs/smb/server/ksmbd_work.c
>>>> +++ b/fs/smb/server/ksmbd_work.c
>>>> @@ -123,8 +123,11 @@ static int __ksmbd_iov_pin_rsp(struct ksmbd_work
>>>> *work, void *ib, int len,
>>>>   		new = krealloc(work->iov,
>>>>   			       sizeof(struct kvec) * work->iov_alloc_cnt,
>>>>   			       GFP_KERNEL | __GFP_ZERO);
>>>> -		if (!new)
>>>> +		if (!new) {
>>>> +			kfree(ar);
>>>> +			work->iov_alloc_cnt -= 4;
>>>>   			return -ENOMEM;
>>>> +		}
>>>>   		work->iov = new;
>>>>   	}
>>>>
>>> A few lines above, ar is allocated inside the 'if (aux_size)' block.
>>> If aux_size is falsy, isn't it possible that ar will be NULL hence
>>> we should have 'if (ar) kfree(ar);'?
>> We need to initialize ar to NULL on that case. And Passing a NULL
>> pointer to kfree is safe, So NULL check before kfree() is not needed.
> Yes, ar should be initialized to NULL to avoid the case of  aux_size
> will be false.
> Since kfree(NULL) is safe.
> Should I  send another patch for this?
I would appreciate it if you could do that.

>
> Best regards!
>> Thanks.
>
>

^ permalink raw reply	[flat|nested] 8+ messages in thread

end of thread, other threads:[~2023-11-20  1:38 UTC | newest]

Thread overview: 8+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2023-11-08  8:15 [PATCH] ksmbd: prevent memory leak on error return Zongmin Zhou
2023-11-08 10:36 ` Namjae Jeon
2023-11-09  1:17   ` [PATCH v2] " Zongmin Zhou
2023-11-09 13:21     ` Namjae Jeon
2023-11-19  9:14     ` Pierre Mariani
2023-11-19 14:17       ` Namjae Jeon
2023-11-20  1:33         ` Zongmin Zhou
2023-11-20  1:38           ` Namjae Jeon

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).