[PATCH 2/3] Teach SELinux about anonymous inodes
Stephen Smalley
sds at tycho.nsa.gov
Fri Feb 14 16:39:40 UTC 2020
On 2/13/20 10:26 PM, Daniel Colascione wrote:
> diff --git a/security/selinux/hooks.c b/security/selinux/hooks.c
> index 1659b59fb5d7..6de0892620b3 100644
> --- a/security/selinux/hooks.c
> +++ b/security/selinux/hooks.c
> @@ -2915,6 +2915,62 @@ static int selinux_inode_init_security(struct inode *inode, struct inode *dir,
> return 0;
> }
>
> +static int selinux_inode_init_security_anon(struct inode *inode,
> + const struct qstr *name,
> + const struct file_operations *fops,
> + const struct inode *context_inode)
> +{
> + const struct task_security_struct *tsec = selinux_cred(current_cred());
> + struct common_audit_data ad;
> + struct inode_security_struct *isec;
> + int rc;
> +
> + if (unlikely(IS_PRIVATE(inode)))
> + return 0;
This is not possible since the caller clears S_PRIVATE before calling
and it would be a bug to call the hook on an inode that was intended to
be private, so we shouldn't check it here.
> +
> + if (unlikely(!selinux_state.initialized))
> + return 0;
Are we doing this to defer initialization until selinux_complete_init()
- that's normally why we bail in the !initialized case? Not entirely
sure what will happen in such a situation since we won't have the
context_inode or the allocating task information at that time, so we
certainly won't get the same result - probably they would all be labeled
with whatever anon_inodefs is assigned via genfscon or
SECINITSID_UNLABELED by default. If we instead just drop this test and
proceed, we'll inherit the context inode SID if specified or we'll call
security_transition_sid(), which in the !initialized case will just
return the tsid i.e. tsec->sid, so it will be labeled with the creating
task SID (SECINITSID_KERNEL prior to initialization). Then the
avc_has_perm() call will pass because everything gets allowed until
initialization. So you could drop this check and userfaultfds created
before policy load would get the kernel SID, or you can keep it and they
will get the unlabeled SID. Preference?
> +
> + isec = selinux_inode(inode);
> +
> + /*
> + * We only get here once per ephemeral inode. The inode has
> + * been initialized via inode_alloc_security but is otherwise
> + * untouched.
> + */
> +
> + if (context_inode) {
> + struct inode_security_struct *context_isec =
> + selinux_inode(context_inode);
> + if (IS_ERR(context_isec))
> + return PTR_ERR(context_isec);
This isn't possible AFAICT so you don't need to test for it or handle
it. In fact, even the test for NULL in selinux_inode() is bogus and
should get dropped AFAICT; we always allocate an inode security blob
even before policy load so it would be a bug if we ever had a NULL there.
> + isec->sid = context_isec->sid;
> + } else {
> + rc = security_transition_sid(
> + &selinux_state, tsec->sid, tsec->sid,
> + SECCLASS_FILE, name, &isec->sid);
> + if (rc)
> + return rc;
> + }
Since you switched to using security_transition_sid(), you are not using
the fops parameter anymore nor comparing with userfaultfd_fops, so you
could drop the parameter from the hook and leave the latter static in
the first patch.
That's assuming you are ok with having to define these type_transition
rules for the userfaultfd case instead of having your own separate
security class. Wondering how many different anon inode names/classes
there are in the kernel today and how much they change over time; for a
small, relatively stable set, separate classes might be ok; for a large,
dynamic set, type transitions should scale better. We might still need
to create a mapping table in SELinux from the names to some stable
identifier for the policy lookup if we can't rely on the names being stable.
More information about the Linux-security-module-archive
mailing list