This is a note to let you know that I've just added the patch titled capabilities: fix potential memleak on error path from vfs_getxattr_alloc() to the 5.4-stable tree which can be found at: http://www.kernel.org/git/?p=linux/kernel/git/stable/stable-queue.git;a=summary The filename of the patch is: capabilities-fix-potential-memleak-on-error-path-from-vfs_getxattr_alloc.patch and it can be found in the queue-5.4 subdirectory. If you, or anyone else, feels it should not be added to the stable tree, please let <stable@xxxxxxxxxxxxxxx> know about it. >From 8cf0a1bc12870d148ae830a4ba88cfdf0e879cee Mon Sep 17 00:00:00 2001 From: Gaosheng Cui <cuigaosheng1@xxxxxxxxxx> Date: Tue, 25 Oct 2022 21:33:57 +0800 Subject: capabilities: fix potential memleak on error path from vfs_getxattr_alloc() From: Gaosheng Cui <cuigaosheng1@xxxxxxxxxx> commit 8cf0a1bc12870d148ae830a4ba88cfdf0e879cee 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@xxxxxxxxxxxxxxx Fixes: 8db6c34f1dbc ("Introduce v3 namespaced file capabilities") Signed-off-by: Gaosheng Cui <cuigaosheng1@xxxxxxxxxx> Acked-by: Serge Hallyn <serge@xxxxxxxxxx> [PM: subject line and backtrace tweaks] Signed-off-by: Paul Moore <paul@xxxxxxxxxxxxxx> Signed-off-by: Greg Kroah-Hartman <gregkh@xxxxxxxxxxxxxxxxxxx> --- security/commoncap.c | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) --- a/security/commoncap.c +++ b/security/commoncap.c @@ -391,8 +391,10 @@ int cap_inode_getsecurity(struct inode * &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; Patches currently in stable-queue which might be from cuigaosheng1@xxxxxxxxxx are queue-5.4/capabilities-fix-potential-memleak-on-error-path-from-vfs_getxattr_alloc.patch queue-5.4/net-mdio-fix-undefined-behavior-in-bit-shift-for-__m.patch