* [PATCH] cifs: fix memory leak of an allocated cifs_ntsd structure
@ 2019-01-07 17:15 Colin King
2019-01-08 9:42 ` Dan Carpenter
0 siblings, 1 reply; 4+ messages in thread
From: Colin King @ 2019-01-07 17:15 UTC (permalink / raw)
To: Steve French, linux-cifs, samba-technical; +Cc: kernel-janitors, linux-kernel
From: Colin Ian King <colin.king@canonical.com>
The call to SMB2_queary_acl can allocate memory to pntsd and also
return a failure via a call to SMB2_query_acl (and then query_info).
This occurs when query_info allocates the structure and then in
query_info the call to smb2_validate_and_copy_iov fails. Currently the
failure just returns without kfree'ing pntsd hence causing a memory
leak. Fix this by kfree'ing pntsd before returning.
Detected by CoverityScan, CID#1457059 ("Resource Leak")
Fixes: 2f1afe25997f ("cifs: Use smb 2 - 3 and cifsacl mount options getacl functions")
Signed-off-by: Colin Ian King <colin.king@canonical.com>
---
fs/cifs/smb2ops.c | 4 +++-
1 file changed, 3 insertions(+), 1 deletion(-)
diff --git a/fs/cifs/smb2ops.c b/fs/cifs/smb2ops.c
index cf7eb891804f..6d71958ad2cb 100644
--- a/fs/cifs/smb2ops.c
+++ b/fs/cifs/smb2ops.c
@@ -2238,8 +2238,10 @@ get_smb2_acl_by_fid(struct cifs_sb_info *cifs_sb,
cifs_put_tlink(tlink);
cifs_dbg(FYI, "%s: rc = %d ACL len %d\n", __func__, rc, *pacllen);
- if (rc)
+ if (rc) {
+ kfree(pntsd);
return ERR_PTR(rc);
+ }
return pntsd;
}
--
2.19.1
^ permalink raw reply related [flat|nested] 4+ messages in thread
* Re: [PATCH] cifs: fix memory leak of an allocated cifs_ntsd structure
2019-01-07 17:15 [PATCH] cifs: fix memory leak of an allocated cifs_ntsd structure Colin King
@ 2019-01-08 9:42 ` Dan Carpenter
2019-01-08 9:49 ` Colin Ian King
0 siblings, 1 reply; 4+ messages in thread
From: Dan Carpenter @ 2019-01-08 9:42 UTC (permalink / raw)
To: Colin King
Cc: Steve French, linux-cifs, samba-technical, kernel-janitors, linux-kernel
On Mon, Jan 07, 2019 at 05:15:15PM +0000, Colin King wrote:
> From: Colin Ian King <colin.king@canonical.com>
>
> The call to SMB2_queary_acl can allocate memory to pntsd and also
> return a failure via a call to SMB2_query_acl (and then query_info).
> This occurs when query_info allocates the structure and then in
> query_info the call to smb2_validate_and_copy_iov fails. Currently the
> failure just returns without kfree'ing pntsd hence causing a memory
> leak. Fix this by kfree'ing pntsd before returning.
>
> Detected by CoverityScan, CID#1457059 ("Resource Leak")
>
> Fixes: 2f1afe25997f ("cifs: Use smb 2 - 3 and cifsacl mount options getacl functions")
> Signed-off-by: Colin Ian King <colin.king@canonical.com>
> ---
> fs/cifs/smb2ops.c | 4 +++-
> 1 file changed, 3 insertions(+), 1 deletion(-)
>
> diff --git a/fs/cifs/smb2ops.c b/fs/cifs/smb2ops.c
> index cf7eb891804f..6d71958ad2cb 100644
> --- a/fs/cifs/smb2ops.c
> +++ b/fs/cifs/smb2ops.c
> @@ -2238,8 +2238,10 @@ get_smb2_acl_by_fid(struct cifs_sb_info *cifs_sb,
> cifs_put_tlink(tlink);
>
> cifs_dbg(FYI, "%s: rc = %d ACL len %d\n", __func__, rc, *pacllen);
> - if (rc)
> + if (rc) {
> + kfree(pntsd);
> return ERR_PTR(rc);
> + }
This is a layering violation. The memory was allocated in query_info()
so it should be freed there instead. Also if the kmalloc() fails in
query_info() then it should return -ENOMEM instead of success.
This only affects code which calls SMB2_query_acl(). There are two
callers. You have fixed one but the other is also buggy because we're
returning uninitialized memory in get_smb2_acl_by_path().
regards,
dan carpenter
^ permalink raw reply [flat|nested] 4+ messages in thread
* Re: [PATCH] cifs: fix memory leak of an allocated cifs_ntsd structure
2019-01-08 9:42 ` Dan Carpenter
@ 2019-01-08 9:49 ` Colin Ian King
2019-01-08 17:46 ` Steve French
0 siblings, 1 reply; 4+ messages in thread
From: Colin Ian King @ 2019-01-08 9:49 UTC (permalink / raw)
To: Dan Carpenter
Cc: Steve French, linux-cifs, samba-technical, kernel-janitors, linux-kernel
On 08/01/2019 09:42, Dan Carpenter wrote:
> On Mon, Jan 07, 2019 at 05:15:15PM +0000, Colin King wrote:
>> From: Colin Ian King <colin.king@canonical.com>
>>
>> The call to SMB2_queary_acl can allocate memory to pntsd and also
>> return a failure via a call to SMB2_query_acl (and then query_info).
>> This occurs when query_info allocates the structure and then in
>> query_info the call to smb2_validate_and_copy_iov fails. Currently the
>> failure just returns without kfree'ing pntsd hence causing a memory
>> leak. Fix this by kfree'ing pntsd before returning.
>>
>> Detected by CoverityScan, CID#1457059 ("Resource Leak")
>>
>> Fixes: 2f1afe25997f ("cifs: Use smb 2 - 3 and cifsacl mount options getacl functions")
>> Signed-off-by: Colin Ian King <colin.king@canonical.com>
>> ---
>> fs/cifs/smb2ops.c | 4 +++-
>> 1 file changed, 3 insertions(+), 1 deletion(-)
>>
>> diff --git a/fs/cifs/smb2ops.c b/fs/cifs/smb2ops.c
>> index cf7eb891804f..6d71958ad2cb 100644
>> --- a/fs/cifs/smb2ops.c
>> +++ b/fs/cifs/smb2ops.c
>> @@ -2238,8 +2238,10 @@ get_smb2_acl_by_fid(struct cifs_sb_info *cifs_sb,
>> cifs_put_tlink(tlink);
>>
>> cifs_dbg(FYI, "%s: rc = %d ACL len %d\n", __func__, rc, *pacllen);
>> - if (rc)
>> + if (rc) {
>> + kfree(pntsd);
>> return ERR_PTR(rc);
>> + }
>
> This is a layering violation. The memory was allocated in query_info()
> so it should be freed there instead. Also if the kmalloc() fails in
> query_info() then it should return -ENOMEM instead of success.
>
> This only affects code which calls SMB2_query_acl(). There are two
> callers. You have fixed one but the other is also buggy because we're
> returning uninitialized memory in get_smb2_acl_by_path().
>
> regards,
> dan carpenter
>
Eep. very true. NACK to my fixes and I'll sort out a correct fix later.
Colin
^ permalink raw reply [flat|nested] 4+ messages in thread
* Re: [PATCH] cifs: fix memory leak of an allocated cifs_ntsd structure
2019-01-08 9:49 ` Colin Ian King
@ 2019-01-08 17:46 ` Steve French
0 siblings, 0 replies; 4+ messages in thread
From: Steve French @ 2019-01-08 17:46 UTC (permalink / raw)
To: Colin Ian King
Cc: Dan Carpenter, Steve French, CIFS, samba-technical,
kernel-janitors, LKML
Yes - good point. Will back this out temporarily, and also update
Aurelien's (unrelated patch) to the later version
On Tue, Jan 8, 2019 at 3:52 AM Colin Ian King <colin.king@canonical.com> wrote:
>
> On 08/01/2019 09:42, Dan Carpenter wrote:
> > On Mon, Jan 07, 2019 at 05:15:15PM +0000, Colin King wrote:
> >> From: Colin Ian King <colin.king@canonical.com>
> >>
> >> The call to SMB2_queary_acl can allocate memory to pntsd and also
> >> return a failure via a call to SMB2_query_acl (and then query_info).
> >> This occurs when query_info allocates the structure and then in
> >> query_info the call to smb2_validate_and_copy_iov fails. Currently the
> >> failure just returns without kfree'ing pntsd hence causing a memory
> >> leak. Fix this by kfree'ing pntsd before returning.
> >>
> >> Detected by CoverityScan, CID#1457059 ("Resource Leak")
> >>
> >> Fixes: 2f1afe25997f ("cifs: Use smb 2 - 3 and cifsacl mount options getacl functions")
> >> Signed-off-by: Colin Ian King <colin.king@canonical.com>
> >> ---
> >> fs/cifs/smb2ops.c | 4 +++-
> >> 1 file changed, 3 insertions(+), 1 deletion(-)
> >>
> >> diff --git a/fs/cifs/smb2ops.c b/fs/cifs/smb2ops.c
> >> index cf7eb891804f..6d71958ad2cb 100644
> >> --- a/fs/cifs/smb2ops.c
> >> +++ b/fs/cifs/smb2ops.c
> >> @@ -2238,8 +2238,10 @@ get_smb2_acl_by_fid(struct cifs_sb_info *cifs_sb,
> >> cifs_put_tlink(tlink);
> >>
> >> cifs_dbg(FYI, "%s: rc = %d ACL len %d\n", __func__, rc, *pacllen);
> >> - if (rc)
> >> + if (rc) {
> >> + kfree(pntsd);
> >> return ERR_PTR(rc);
> >> + }
> >
> > This is a layering violation. The memory was allocated in query_info()
> > so it should be freed there instead. Also if the kmalloc() fails in
> > query_info() then it should return -ENOMEM instead of success.
> >
> > This only affects code which calls SMB2_query_acl(). There are two
> > callers. You have fixed one but the other is also buggy because we're
> > returning uninitialized memory in get_smb2_acl_by_path().
> >
> > regards,
> > dan carpenter
> >
>
> Eep. very true. NACK to my fixes and I'll sort out a correct fix later.
>
> Colin
--
Thanks,
Steve
^ permalink raw reply [flat|nested] 4+ messages in thread
end of thread, other threads:[~2019-01-08 17:46 UTC | newest]
Thread overview: 4+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2019-01-07 17:15 [PATCH] cifs: fix memory leak of an allocated cifs_ntsd structure Colin King
2019-01-08 9:42 ` Dan Carpenter
2019-01-08 9:49 ` Colin Ian King
2019-01-08 17:46 ` Steve French
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).