History

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.

Gaosheng Cui committed 4 years ago

Commit 8cf0a1b1 parent 9abf2311 file · +4 −2Browse files

Unified · Split

security/commoncap.c+4−2
@@ -401,8 +401,10 @@ int cap_inode_getsecurity(struct user_namespace *mnt_userns, · 400 unmodified lines
401 &tmpbuf, size, GFP_NOFS);401 &tmpbuf, size, GFP_NOFS);
402 dput(dentry);402 dput(dentry);
403 403
404 if (ret < 0 || !tmpbuf)404 if (ret < 0 || !tmpbuf) {
405 return ret;405 size = ret;
406 goto out_free;
407 }
406 408
407 fs_ns = inode->i_sb->s_user_ns;409 fs_ns = inode->i_sb->s_user_ns;
408 cap = (struct vfs_cap_data *) tmpbuf;410 cap = (struct vfs_cap_data *) tmpbuf;