* [PATCH] security: commoncap: fix potential memleak on error path from vfs_getxattr_alloc
@ 2022-10-25 10:45 Gaosheng Cui
2022-10-25 13:04 ` Christian Brauner
0 siblings, 1 reply; 4+ messages in thread
From: Gaosheng Cui @ 2022-10-25 10:45 UTC (permalink / raw)
To: serge, paul, jmorris, 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: 71bc356f93a1 ("commoncap: handle idmapped mounts")
Signed-off-by: Gaosheng Cui <cuigaosheng1@huawei.com>
---
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] 4+ messages in thread
* Re: [PATCH] security: commoncap: fix potential memleak on error path from vfs_getxattr_alloc
2022-10-25 10:45 [PATCH] security: commoncap: fix potential memleak on error path from vfs_getxattr_alloc Gaosheng Cui
@ 2022-10-25 13:04 ` Christian Brauner
2022-10-25 13:45 ` Seth Forshee
2022-10-25 13:51 ` cuigaosheng
0 siblings, 2 replies; 4+ messages in thread
From: Christian Brauner @ 2022-10-25 13:04 UTC (permalink / raw)
To: Gaosheng Cui; +Cc: serge, paul, jmorris, linux-security-module
On Tue, Oct 25, 2022 at 06:45:44PM +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.
Hey Gaosheng,
>
> Fixes: 71bc356f93a1 ("commoncap: handle idmapped mounts")
The Fixes: tag is wrong afaict. The control flow isn't changed in any
way by the referenced commit.
The logic changed the last time with
82e5d8cc768b ("security: commoncap: fix -Wstringop-overread warning")
but even that commit wouldn't have introduced the bug. It would've been
introduced by 8db6c34f1dbc ("Introduce v3 namespaced file
capabilities"). So update the Fixes: tag with that reference, please.
Otherwise I think you in principle have a point. Not sure if we have any
filesystem that in practice would fail after permission checks have
already passed with the first call to ->get() but then fail with the
correct size passed in the second ->get() invocation. Sounds super
unlikely but not impossible.
^ permalink raw reply [flat|nested] 4+ messages in thread
* Re: [PATCH] security: commoncap: fix potential memleak on error path from vfs_getxattr_alloc
2022-10-25 13:04 ` Christian Brauner
@ 2022-10-25 13:45 ` Seth Forshee
2022-10-25 13:51 ` cuigaosheng
1 sibling, 0 replies; 4+ messages in thread
From: Seth Forshee @ 2022-10-25 13:45 UTC (permalink / raw)
To: Christian Brauner
Cc: Gaosheng Cui, serge, paul, jmorris, linux-security-module
On Tue, Oct 25, 2022 at 03:04:59PM +0200, Christian Brauner wrote:
> On Tue, Oct 25, 2022 at 06:45:44PM +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.
>
> Hey Gaosheng,
>
> >
> > Fixes: 71bc356f93a1 ("commoncap: handle idmapped mounts")
>
> The Fixes: tag is wrong afaict. The control flow isn't changed in any
> way by the referenced commit.
>
> The logic changed the last time with
> 82e5d8cc768b ("security: commoncap: fix -Wstringop-overread warning")
> but even that commit wouldn't have introduced the bug. It would've been
> introduced by 8db6c34f1dbc ("Introduce v3 namespaced file
> capabilities"). So update the Fixes: tag with that reference, please.
>
> Otherwise I think you in principle have a point. Not sure if we have any
> filesystem that in practice would fail after permission checks have
> already passed with the first call to ->get() but then fail with the
> correct size passed in the second ->get() invocation. Sounds super
> unlikely but not impossible.
Looks like several other callers have the same problem --
evm_is_immutable(), evm_xattr_change(), ima_eventevmsig_init(). And
ima_read_xattr() expects the caller to handle it which seems potentially
problematic, though currently only current caller does handle it
properly.
The documention comment for vfs_getxattr_alloc() needs a warning that
the caller needs to free memory even if an error is returned, because
that is very counterintuitive. It probably should have been named
vfs_getxattr_realloc() ...
Seth
^ permalink raw reply [flat|nested] 4+ messages in thread
* Re: [PATCH] security: commoncap: fix potential memleak on error path from vfs_getxattr_alloc
2022-10-25 13:04 ` Christian Brauner
2022-10-25 13:45 ` Seth Forshee
@ 2022-10-25 13:51 ` cuigaosheng
1 sibling, 0 replies; 4+ messages in thread
From: cuigaosheng @ 2022-10-25 13:51 UTC (permalink / raw)
To: Christian Brauner; +Cc: serge, paul, jmorris, linux-security-module
> The Fixes: tag is wrong afaict. The control flow isn't changed in any
> way by the referenced commit.
Thanks for taking time to review this patch, I have update the fixes tags.
link: https://patchwork.kernel.org/project/linux-security-module/patch/20221025133357.2404086-1-cuigaosheng1@huawei.com/
> Otherwise I think you in principle have a point. Not sure if we have any
> filesystem that in practice would fail after permission checks have
> already passed with the first call to ->get() but then fail with the
> correct size passed in the second ->get() invocation. Sounds super
> unlikely but not impossible.
This problem was discovered when I was reading the code, we can build the fault
scenario by hard coding, but the probability of this problem occurring during
use should be very small, I thought we can fix it, so I submit the patch,
thanks again!
On 2022/10/25 21:04, Christian Brauner wrote:
> On Tue, Oct 25, 2022 at 06:45:44PM +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.
> Hey Gaosheng,
>
>> Fixes: 71bc356f93a1 ("commoncap: handle idmapped mounts")
> The Fixes: tag is wrong afaict. The control flow isn't changed in any
> way by the referenced commit.
>
> The logic changed the last time with
> 82e5d8cc768b ("security: commoncap: fix -Wstringop-overread warning")
> but even that commit wouldn't have introduced the bug. It would've been
> introduced by 8db6c34f1dbc ("Introduce v3 namespaced file
> capabilities"). So update the Fixes: tag with that reference, please.
>
> Otherwise I think you in principle have a point. Not sure if we have any
> filesystem that in practice would fail after permission checks have
> already passed with the first call to ->get() but then fail with the
> correct size passed in the second ->get() invocation. Sounds super
> unlikely but not impossible.
> .
^ permalink raw reply [flat|nested] 4+ messages in thread
end of thread, other threads:[~2022-10-25 13:51 UTC | newest]
Thread overview: 4+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2022-10-25 10:45 [PATCH] security: commoncap: fix potential memleak on error path from vfs_getxattr_alloc Gaosheng Cui
2022-10-25 13:04 ` Christian Brauner
2022-10-25 13:45 ` Seth Forshee
2022-10-25 13:51 ` cuigaosheng
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).