Skip to content

Commit 8cf0a1b

Browse files
gscuipcmoore
authored andcommitted
capabilities: fix potential memleak on error path from vfs_getxattr_alloc()
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: [email protected] Fixes: 8db6c34 ("Introduce v3 namespaced file capabilities") Signed-off-by: Gaosheng Cui <[email protected]> Acked-by: Serge Hallyn <[email protected]> [PM: subject line and backtrace tweaks] Signed-off-by: Paul Moore <[email protected]>
1 parent 9abf231 commit 8cf0a1b

File tree

1 file changed

+4
-2
lines changed

1 file changed

+4
-2
lines changed

security/commoncap.c

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -401,8 +401,10 @@ int cap_inode_getsecurity(struct user_namespace *mnt_userns,
401401
&tmpbuf, size, GFP_NOFS);
402402
dput(dentry);
403403

404-
if (ret < 0 || !tmpbuf)
405-
return ret;
404+
if (ret < 0 || !tmpbuf) {
405+
size = ret;
406+
goto out_free;
407+
}
406408

407409
fs_ns = inode->i_sb->s_user_ns;
408410
cap = (struct vfs_cap_data *) tmpbuf;

0 commit comments

Comments
 (0)