Skip to content

Commit

Permalink
selinux: fix inode_doinit_with_dentry() LABEL_INVALID error handling
Browse files Browse the repository at this point in the history
[ Upstream commit 200ea5a ]

A previous fix, commit 83370b3 ("selinux: fix error initialization
in inode_doinit_with_dentry()"), changed how failures were handled
before a SELinux policy was loaded.  Unfortunately that patch was
potentially problematic for two reasons: it set the isec->initialized
state without holding a lock, and it didn't set the inode's SELinux
label to the "default" for the particular filesystem.  The later can
be a problem if/when a later attempt to revalidate the inode fails
and SELinux reverts to the existing inode label.

This patch should restore the default inode labeling that existed
before the original fix, without affecting the LABEL_INVALID marking
such that revalidation will still be attempted in the future.

Fixes: 83370b3 ("selinux: fix error initialization in inode_doinit_with_dentry()")
Reported-by: Sven Schnelle <svens@linux.ibm.com>
Tested-by: Sven Schnelle <svens@linux.ibm.com>
Reviewed-by: Ondrej Mosnacek <omosnace@redhat.com>
Signed-off-by: Paul Moore <paul@paul-moore.com>
Signed-off-by: Sasha Levin <sashal@kernel.org>
  • Loading branch information
pcmoore authored and gregkh committed Dec 30, 2020
1 parent ae54a6d commit c807042
Showing 1 changed file with 13 additions and 18 deletions.
31 changes: 13 additions & 18 deletions security/selinux/hooks.c
Expand Up @@ -1499,13 +1499,7 @@ static int inode_doinit_with_dentry(struct inode *inode, struct dentry *opt_dent
* inode_doinit with a dentry, before these inodes could
* be used again by userspace.
*/
isec->initialized = LABEL_INVALID;
/*
* There is nothing useful to jump to the "out"
* label, except a needless spin lock/unlock
* cycle.
*/
return 0;
goto out_invalid;
}

rc = inode_doinit_use_xattr(inode, dentry, sbsec->def_sid,
Expand Down Expand Up @@ -1559,15 +1553,8 @@ static int inode_doinit_with_dentry(struct inode *inode, struct dentry *opt_dent
* inode_doinit() with a dentry, before these inodes
* could be used again by userspace.
*/
if (!dentry) {
isec->initialized = LABEL_INVALID;
/*
* There is nothing useful to jump to the "out"
* label, except a needless spin lock/unlock
* cycle.
*/
return 0;
}
if (!dentry)
goto out_invalid;
rc = selinux_genfs_get_sid(dentry, sclass,
sbsec->flags, &sid);
if (rc) {
Expand All @@ -1592,18 +1579,26 @@ static int inode_doinit_with_dentry(struct inode *inode, struct dentry *opt_dent
out:
spin_lock(&isec->lock);
if (isec->initialized == LABEL_PENDING) {
if (!sid || rc) {
if (rc) {
isec->initialized = LABEL_INVALID;
goto out_unlock;
}

isec->initialized = LABEL_INITIALIZED;
isec->sid = sid;
}

out_unlock:
spin_unlock(&isec->lock);
return rc;

out_invalid:
spin_lock(&isec->lock);
if (isec->initialized == LABEL_PENDING) {
isec->initialized = LABEL_INVALID;
isec->sid = sid;
}
spin_unlock(&isec->lock);
return 0;
}

/* Convert a Linux signal to an access vector. */
Expand Down

0 comments on commit c807042

Please sign in to comment.