Skip to content

Commit b7316f5

Browse files
jlebongregkh
authored andcommitted
selinux: allow labeling before policy is loaded
[ Upstream commit 3e3e24b ] Currently, the SELinux LSM prevents one from setting the `security.selinux` xattr on an inode without a policy first being loaded. However, this restriction is problematic: it makes it impossible to have newly created files with the correct label before actually loading the policy. This is relevant in distributions like Fedora, where the policy is loaded by systemd shortly after pivoting out of the initrd. In such instances, all files created prior to pivoting will be unlabeled. One then has to relabel them after pivoting, an operation which inherently races with other processes trying to access those same files. Going further, there are use cases for creating the entire root filesystem on first boot from the initrd (e.g. Container Linux supports this today[1], and we'd like to support it in Fedora CoreOS as well[2]). One can imagine doing this in two ways: at the block device level (e.g. laying down a disk image), or at the filesystem level. In the former, labeling can simply be part of the image. But even in the latter scenario, one still really wants to be able to set the right labels when populating the new filesystem. This patch enables this by changing behaviour in the following two ways: 1. allow `setxattr` if we're not initialized 2. don't try to set the in-core inode SID if we're not initialized; instead leave it as `LABEL_INVALID` so that revalidation may be attempted at a later time Note the first hunk of this patch is mostly the same as a previously discussed one[3], though it was part of a larger series which wasn't accepted. [1] https://coreos.com/os/docs/latest/root-filesystem-placement.html [2] coreos/fedora-coreos-tracker#94 [3] https://www.spinics.net/lists/linux-initramfs/msg04593.html Co-developed-by: Victor Kamensky <kamensky@cisco.com> Signed-off-by: Victor Kamensky <kamensky@cisco.com> Signed-off-by: Jonathan Lebon <jlebon@redhat.com> Signed-off-by: Paul Moore <paul@paul-moore.com> Signed-off-by: Sasha Levin <sashal@kernel.org>
1 parent e14f1f8 commit b7316f5

1 file changed

Lines changed: 12 additions & 0 deletions

File tree

‎security/selinux/hooks.c‎

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3156,6 +3156,9 @@ static int selinux_inode_setxattr(struct dentry *dentry, const char *name,
31563156
return dentry_has_perm(current_cred(), dentry, FILE__SETATTR);
31573157
}
31583158

3159+
if (!selinux_state.initialized)
3160+
return (inode_owner_or_capable(inode) ? 0 : -EPERM);
3161+
31593162
sbsec = inode->i_sb->s_security;
31603163
if (!(sbsec->flags & SBLABEL_MNT))
31613164
return -EOPNOTSUPP;
@@ -3239,6 +3242,15 @@ static void selinux_inode_post_setxattr(struct dentry *dentry, const char *name,
32393242
return;
32403243
}
32413244

3245+
if (!selinux_state.initialized) {
3246+
/* If we haven't even been initialized, then we can't validate
3247+
* against a policy, so leave the label as invalid. It may
3248+
* resolve to a valid label on the next revalidation try if
3249+
* we've since initialized.
3250+
*/
3251+
return;
3252+
}
3253+
32423254
rc = security_context_to_sid_force(&selinux_state, value, size,
32433255
&newsid);
32443256
if (rc) {

0 commit comments

Comments
 (0)