[PATCH v2] security: commoncap: fix potential memleak on error path from vfs_getxattr_alloc

Paul Moore paul at paul-moore.com
Fri Oct 28 10:54:48 UTC 2022


On Tue, Oct 25, 2022 at 9:34 AM Gaosheng Cui <cuigaosheng1 at 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 at 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



More information about the Linux-security-module-archive mailing list