* [PATCH v2] security: commoncap: fix potential memleak on error path from vfs_getxattr_alloc
@ 2022-10-25 13:33 Gaosheng Cui
2022-10-26 0:11 ` Serge E. Hallyn
2022-10-28 10:54 ` Paul Moore
0 siblings, 2 replies; 3+ messages in thread
From: Gaosheng Cui @ 2022-10-25 13:33 UTC (permalink / raw)
To: serge, paul, jmorris, ebiederm, brauner, cuigaosheng1
Cc: linux-security-module
In cap_inode_getsecurity(), we will use vfs_getxattr_alloc() to
complete the memory allocation of tmpbuf, if we have completed
the memory allocation of tmpbuf, but failed to call handler->get(...),
there will be a memleak in below logic:
|-- ret = (int)vfs_getxattr_alloc(mnt_userns, ...) <-- alloc for tmpbuf
|-- value = krealloc(*xattr_value, error + 1, flags) <-- alloc memory
|-- error = handler->get(handler, ...) <-- if error
|-- *xattr_value = value <-- xattr_value is &tmpbuf <-- memory leak
So we will try to free(tmpbuf) after vfs_getxattr_alloc() fails to fix it.
Fixes: 8db6c34f1dbc ("Introduce v3 namespaced file capabilities")
Signed-off-by: Gaosheng Cui <cuigaosheng1@huawei.com>
---
v2:
- Update the Fixes tag, from 71bc356f93a1 to 8db6c34f1dbc. Thanks!
security/commoncap.c | 6 ++++--
1 file changed, 4 insertions(+), 2 deletions(-)
diff --git a/security/commoncap.c b/security/commoncap.c
index 5fc8986c3c77..bc751fa5adad 100644
--- a/security/commoncap.c
+++ b/security/commoncap.c
@@ -401,8 +401,10 @@ int cap_inode_getsecurity(struct user_namespace *mnt_userns,
&tmpbuf, size, GFP_NOFS);
dput(dentry);
- if (ret < 0 || !tmpbuf)
- return ret;
+ if (ret < 0 || !tmpbuf) {
+ size = ret;
+ goto out_free;
+ }
fs_ns = inode->i_sb->s_user_ns;
cap = (struct vfs_cap_data *) tmpbuf;
--
2.25.1
^ permalink raw reply related [flat|nested] 3+ messages in thread
* Re: [PATCH v2] security: commoncap: fix potential memleak on error path from vfs_getxattr_alloc
2022-10-25 13:33 [PATCH v2] security: commoncap: fix potential memleak on error path from vfs_getxattr_alloc Gaosheng Cui
@ 2022-10-26 0:11 ` Serge E. Hallyn
2022-10-28 10:54 ` Paul Moore
1 sibling, 0 replies; 3+ messages in thread
From: Serge E. Hallyn @ 2022-10-26 0:11 UTC (permalink / raw)
To: Gaosheng Cui
Cc: serge, paul, jmorris, ebiederm, brauner, linux-security-module
On Tue, Oct 25, 2022 at 09:33:57PM +0800, Gaosheng Cui wrote:
> In cap_inode_getsecurity(), we will use vfs_getxattr_alloc() to
> complete the memory allocation of tmpbuf, if we have completed
> the memory allocation of tmpbuf, but failed to call handler->get(...),
> there will be a memleak in below logic:
>
> |-- ret = (int)vfs_getxattr_alloc(mnt_userns, ...) <-- alloc for tmpbuf
> |-- value = krealloc(*xattr_value, error + 1, flags) <-- alloc memory
> |-- error = handler->get(handler, ...) <-- if error
> |-- *xattr_value = value <-- xattr_value is &tmpbuf <-- memory leak
>
> So we will try to free(tmpbuf) after vfs_getxattr_alloc() fails to fix it.
>
> Fixes: 8db6c34f1dbc ("Introduce v3 namespaced file capabilities")
Acked-by: Serge Hallyn <serge@hallyn.com>
> Signed-off-by: Gaosheng Cui <cuigaosheng1@huawei.com>
> ---
> v2:
> - Update the Fixes tag, from 71bc356f93a1 to 8db6c34f1dbc. Thanks!
> security/commoncap.c | 6 ++++--
> 1 file changed, 4 insertions(+), 2 deletions(-)
>
> diff --git a/security/commoncap.c b/security/commoncap.c
> index 5fc8986c3c77..bc751fa5adad 100644
> --- a/security/commoncap.c
> +++ b/security/commoncap.c
> @@ -401,8 +401,10 @@ int cap_inode_getsecurity(struct user_namespace *mnt_userns,
> &tmpbuf, size, GFP_NOFS);
> dput(dentry);
>
> - if (ret < 0 || !tmpbuf)
> - return ret;
> + if (ret < 0 || !tmpbuf) {
> + size = ret;
> + goto out_free;
> + }
>
> fs_ns = inode->i_sb->s_user_ns;
> cap = (struct vfs_cap_data *) tmpbuf;
> --
> 2.25.1
^ permalink raw reply [flat|nested] 3+ messages in thread
* Re: [PATCH v2] security: commoncap: fix potential memleak on error path from vfs_getxattr_alloc
2022-10-25 13:33 [PATCH v2] security: commoncap: fix potential memleak on error path from vfs_getxattr_alloc Gaosheng Cui
2022-10-26 0:11 ` Serge E. Hallyn
@ 2022-10-28 10:54 ` Paul Moore
1 sibling, 0 replies; 3+ messages in thread
From: Paul Moore @ 2022-10-28 10:54 UTC (permalink / raw)
To: Gaosheng Cui; +Cc: serge, jmorris, ebiederm, brauner, linux-security-module
On Tue, Oct 25, 2022 at 9:34 AM Gaosheng Cui <cuigaosheng1@huawei.com> wrote:
>
> In cap_inode_getsecurity(), we will use vfs_getxattr_alloc() to
> complete the memory allocation of tmpbuf, if we have completed
> the memory allocation of tmpbuf, but failed to call handler->get(...),
> there will be a memleak in below logic:
>
> |-- ret = (int)vfs_getxattr_alloc(mnt_userns, ...) <-- alloc for tmpbuf
> |-- value = krealloc(*xattr_value, error + 1, flags) <-- alloc memory
> |-- error = handler->get(handler, ...) <-- if error
> |-- *xattr_value = value <-- xattr_value is &tmpbuf <-- memory leak
>
> So we will try to free(tmpbuf) after vfs_getxattr_alloc() fails to fix it.
>
> Fixes: 8db6c34f1dbc ("Introduce v3 namespaced file capabilities")
> Signed-off-by: Gaosheng Cui <cuigaosheng1@huawei.com>
> ---
> v2:
> - Update the Fixes tag, from 71bc356f93a1 to 8db6c34f1dbc. Thanks!
> security/commoncap.c | 6 ++++--
> 1 file changed, 4 insertions(+), 2 deletions(-)
Merged into lsm/stable-6.1 and plan on sending it up to Linus early
next week (network connectivity permitting). Thanks!
--
paul-moore.com
^ permalink raw reply [flat|nested] 3+ messages in thread
end of thread, other threads:[~2022-10-28 10:55 UTC | newest]
Thread overview: 3+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2022-10-25 13:33 [PATCH v2] security: commoncap: fix potential memleak on error path from vfs_getxattr_alloc Gaosheng Cui
2022-10-26 0:11 ` Serge E. Hallyn
2022-10-28 10:54 ` Paul Moore
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).