[PATCH 4.18 117/197] selinux: cleanup dentry and inodes on error in selinuxfs

From: Greg Kroah-Hartman
Date: Thu Sep 13 2018 - 10:02:27 EST


4.18-stable review patch. If anyone has any objections, please let me know.

------------------

From: nixiaoming <nixiaoming@xxxxxxxxxx>

[ Upstream commit 7e4237faa7213c1cc1d0aa65a44c67ba4729ce9f ]

If the resource requested by d_alloc_name is not added to the linked
list through d_add, then dput needs to be called to release the
subsequent abnormal branch to avoid resource leakage.

Add missing dput to selinuxfs.c

Signed-off-by: nixiaoming <nixiaoming@xxxxxxxxxx>
[PM: tweak the subject line]
Signed-off-by: Paul Moore <paul@xxxxxxxxxxxxxx>
Signed-off-by: Sasha Levin <alexander.levin@xxxxxxxxxxxxx>
Signed-off-by: Greg Kroah-Hartman <gregkh@xxxxxxxxxxxxxxxxxxx>
---
security/selinux/selinuxfs.c | 33 +++++++++++++++++++++++++--------
1 file changed, 25 insertions(+), 8 deletions(-)

--- a/security/selinux/selinuxfs.c
+++ b/security/selinux/selinuxfs.c
@@ -1365,13 +1365,18 @@ static int sel_make_bools(struct selinux

ret = -ENOMEM;
inode = sel_make_inode(dir->d_sb, S_IFREG | S_IRUGO | S_IWUSR);
- if (!inode)
+ if (!inode) {
+ dput(dentry);
goto out;
+ }

ret = -ENAMETOOLONG;
len = snprintf(page, PAGE_SIZE, "/%s/%s", BOOL_DIR_NAME, names[i]);
- if (len >= PAGE_SIZE)
+ if (len >= PAGE_SIZE) {
+ dput(dentry);
+ iput(inode);
goto out;
+ }

isec = (struct inode_security_struct *)inode->i_security;
ret = security_genfs_sid(fsi->state, "selinuxfs", page,
@@ -1586,8 +1591,10 @@ static int sel_make_avc_files(struct den
return -ENOMEM;

inode = sel_make_inode(dir->d_sb, S_IFREG|files[i].mode);
- if (!inode)
+ if (!inode) {
+ dput(dentry);
return -ENOMEM;
+ }

inode->i_fop = files[i].ops;
inode->i_ino = ++fsi->last_ino;
@@ -1632,8 +1639,10 @@ static int sel_make_initcon_files(struct
return -ENOMEM;

inode = sel_make_inode(dir->d_sb, S_IFREG|S_IRUGO);
- if (!inode)
+ if (!inode) {
+ dput(dentry);
return -ENOMEM;
+ }

inode->i_fop = &sel_initcon_ops;
inode->i_ino = i|SEL_INITCON_INO_OFFSET;
@@ -1733,8 +1742,10 @@ static int sel_make_perm_files(char *obj

rc = -ENOMEM;
inode = sel_make_inode(dir->d_sb, S_IFREG|S_IRUGO);
- if (!inode)
+ if (!inode) {
+ dput(dentry);
goto out;
+ }

inode->i_fop = &sel_perm_ops;
/* i+1 since perm values are 1-indexed */
@@ -1763,8 +1774,10 @@ static int sel_make_class_dir_entries(ch
return -ENOMEM;

inode = sel_make_inode(dir->d_sb, S_IFREG|S_IRUGO);
- if (!inode)
+ if (!inode) {
+ dput(dentry);
return -ENOMEM;
+ }

inode->i_fop = &sel_class_ops;
inode->i_ino = sel_class_to_ino(index);
@@ -1838,8 +1851,10 @@ static int sel_make_policycap(struct sel
return -ENOMEM;

inode = sel_make_inode(fsi->sb, S_IFREG | 0444);
- if (inode == NULL)
+ if (inode == NULL) {
+ dput(dentry);
return -ENOMEM;
+ }

inode->i_fop = &sel_policycap_ops;
inode->i_ino = iter | SEL_POLICYCAP_INO_OFFSET;
@@ -1932,8 +1947,10 @@ static int sel_fill_super(struct super_b

ret = -ENOMEM;
inode = sel_make_inode(sb, S_IFCHR | S_IRUGO | S_IWUGO);
- if (!inode)
+ if (!inode) {
+ dput(dentry);
goto err;
+ }

inode->i_ino = ++fsi->last_ino;
isec = (struct inode_security_struct *)inode->i_security;