Blob Blame Raw
From mboxrd@z Thu Jan  1 00:00:00 1970
Return-Path: <>
X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on
X-Spam-Status: No, score=-15.0 required=3.0
	autolearn=ham autolearn_force=no version=3.4.0
Received: from ( [])
	by (Postfix) with ESMTP id 0CE63C4CEC5
	for <>; Thu, 12 Sep 2019 13:30:40 +0000 (UTC)
Received: from ( [])
	by (Postfix) with ESMTP id DC0B020CC7
	for <>; Thu, 12 Sep 2019 13:30:39 +0000 (UTC)
Received: ( by via listexpand
        id S1732192AbfILNaj (ORCPT <rfc822;>);
        Thu, 12 Sep 2019 09:30:39 -0400
Received: from ([]:52278 "EHLO"
        rhost-flags-OK-OK-OK-OK) by with ESMTP
        id S1731687AbfILNaj (ORCPT <rfc822;>);
        Thu, 12 Sep 2019 09:30:39 -0400
Received: from ( [])
        (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits))
        (No client certificate requested)
        by (Postfix) with ESMTPS id 97CC359465
        for <>; Thu, 12 Sep 2019 13:30:38 +0000 (UTC)
Received: by with SMTP id c8so13609684qtd.20
        for <>; Thu, 12 Sep 2019 06:30:38 -0700 (PDT)
X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed;; s=20161025;
X-Gm-Message-State: APjAAAXpWx500L+bZRH8M7OzuSb0aBlsvvjaBYCGvSkzojpa2nRWjtk0
X-Received: by 2002:ac8:7b2e:: with SMTP id l14mr8094193qtu.11.1568295037636;
        Thu, 12 Sep 2019 06:30:37 -0700 (PDT)
X-Google-Smtp-Source: APXvYqzybFpoaFyGZXafGEdtHCL3XllpHltaXggcIZEb7De49V/kJzm1pU6vpg1gN8HtgnB3cilLuA==
X-Received: by 2002:ac8:7b2e:: with SMTP id l14mr8094176qtu.11.1568295037442;
        Thu, 12 Sep 2019 06:30:37 -0700 (PDT)
Received: from localhost.localdomain ([])
        by with ESMTPSA id h68sm11848865qkd.35.2019.
        (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256);
        Thu, 12 Sep 2019 06:30:36 -0700 (PDT)
From:   Jonathan Lebon <>
Cc:     Jonathan Lebon <>,
        Victor Kamensky <>
Subject: [PATCH v2] selinux: allow labeling before policy is loaded
Date:   Thu, 12 Sep 2019 09:30:07 -0400
Message-Id: <>
X-Mailer: git-send-email 2.21.0
MIME-Version: 1.0
Content-Transfer-Encoding: 8bit
Precedence: bulk
List-ID: <>
Archived-At: <>
List-Archive: <>
List-Post: <>

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

Co-developed-by: Victor Kamensky <>
Signed-off-by: Victor Kamensky <>
Signed-off-by: Jonathan Lebon <>



  - return early in selinux_inode_setxattr if policy hasn't been loaded


 security/selinux/hooks.c | 12 ++++++++++++
 1 file changed, 12 insertions(+)

diff --git a/security/selinux/hooks.c b/security/selinux/hooks.c
index 94de51628..dbe96c707 100644
--- a/security/selinux/hooks.c
+++ b/security/selinux/hooks.c
@@ -3142,6 +3142,9 @@ static int selinux_inode_setxattr(struct dentry *dentry, const char *name,
 		return dentry_has_perm(current_cred(), dentry, FILE__SETATTR);
+	if (!selinux_state.initialized)
+		return (inode_owner_or_capable(inode) ? 0 : -EPERM);
 	sbsec = inode->i_sb->s_security;
 	if (!(sbsec->flags & SBLABEL_MNT))
 		return -EOPNOTSUPP;
@@ -3225,6 +3228,15 @@ static void selinux_inode_post_setxattr(struct dentry *dentry, const char *name,
+	if (!selinux_state.initialized) {
+		/* If we haven't even been initialized, then we can't validate
+		 * against a policy, so leave the label as invalid. It may
+		 * resolve to a valid label on the next revalidation try if
+		 * we've since initialized.
+		 */
+		return;
+	}
 	rc = security_context_to_sid_force(&selinux_state, value, size,
 	if (rc) {