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