]> git.baikalelectronics.ru Git - kernel.git/commit
capabilities: fix potential memleak on error path from vfs_getxattr_alloc()
authorGaosheng Cui <cuigaosheng1@huawei.com>
Tue, 25 Oct 2022 13:33:57 +0000 (21:33 +0800)
committerGreg Kroah-Hartman <gregkh@linuxfoundation.org>
Thu, 10 Nov 2022 16:57:55 +0000 (17:57 +0100)
commitcc84335a0831d9539f7304176b61f4158ed21ff9
tree230d2f7d599d959531f05766d9a2298f5aa5b9f0
parentf10645e1c51cd38f051945643ef9b081b14d360b
capabilities: fix potential memleak on error path from vfs_getxattr_alloc()

commit a6bef972c70c1668f79c254e5c90d40d20b489c2 upstream.

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, ...)
    |           /* 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.

Cc: stable@vger.kernel.org
Fixes: 8e3c7cc45997 ("Introduce v3 namespaced file capabilities")
Signed-off-by: Gaosheng Cui <cuigaosheng1@huawei.com>
Acked-by: Serge Hallyn <serge@hallyn.com>
[PM: subject line and backtrace tweaks]
Signed-off-by: Paul Moore <paul@paul-moore.com>
Signed-off-by: Greg Kroah-Hartman <gregkh@linuxfoundation.org>
security/commoncap.c