Received: by 2002:a05:6a10:22f:0:0:0:0 with SMTP id 15csp3755022pxk; Tue, 29 Sep 2020 05:35:42 -0700 (PDT) X-Google-Smtp-Source: ABdhPJzRgLVCr+751JFHdMkCPF84ZZRXF+zLgz/Nz7oZUDEP49WjSomnCeoQysDd9CjhfAF+2KdP X-Received: by 2002:a05:6402:515:: with SMTP id m21mr3091505edv.348.1601382942295; Tue, 29 Sep 2020 05:35:42 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1601382942; cv=none; d=google.com; s=arc-20160816; b=CCLrDTIOfx/uVIGSWuPb48wjyPJhk5L8S0lGajynu8j7CcRW3lK1BTaPzgOh2rTRS4 Bm17nyRJ5FVuJVWK39u4IpKdpGVQjHngWxDPt/wNHflvmvR3shirb8jQAvezk1UhidLs nzmjbBhdaiU1Q4uLbDJ7xAvLMw/ioY63Z3laKdSLeXIhF877Pc3oPTfk6C40MpY1EOm3 /NK5up70GfZuzWfhY0T+yDonxz/IaHHRyfdfG7BQPCNR5n14gmElWNaedO1FbY5a2OSH LGWTMtSzmDfb9pgZu1TDsBPgdznuh/wZoIpcpu3pgUCLm8bZS0tEZ4l1p6hBpPQUR9re feIA== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:content-transfer-encoding:mime-version :user-agent:references:in-reply-to:message-id:date:subject:cc:to :from:dkim-signature; bh=EIXQU09xcIc3p+agFIkiPR7rrSxENJpeCAEcsTOS6VE=; b=UHdFk32bSLmzdzttUFbuuMRs2toCjin7hJuR/WG2I+1XoIqFdYr6KcpOJV3wcc+C/9 TL8j0woElscfa7Us9ch9YN09RmwiRe5OmapykDaQ8Ku4ByAFhEqc0htRCvzEBINwAuxS tbCJNn8PFXgnR+2PimAcVfJ/89V1V77PCHsZDgdYCMjO9bj0P0aGEFA9jQ1+MnE1rHSZ xLf827VRpM+bEj15WKDD4wXC6qNSSeO/GsrTF2P2MfM7PwRDX+OOXHXMeZUwo9n+d3DA P7+MR7VbBcS69tX8E16EbmO3fJGPuR7oCdBPj/3V4pBR5NdkP5O4gXJ5Nz98XNArL+ND tkVw== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@kernel.org header.s=default header.b="GvF/Qdwx"; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=fail (p=NONE sp=NONE dis=NONE) header.from=linuxfoundation.org Return-Path: Received: from vger.kernel.org (vger.kernel.org. [23.128.96.18]) by mx.google.com with ESMTP id k1si2855415edx.88.2020.09.29.05.35.19; Tue, 29 Sep 2020 05:35:42 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) client-ip=23.128.96.18; Authentication-Results: mx.google.com; dkim=pass header.i=@kernel.org header.s=default header.b="GvF/Qdwx"; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=fail (p=NONE sp=NONE dis=NONE) header.from=linuxfoundation.org Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1733098AbgI2MeX (ORCPT + 99 others); Tue, 29 Sep 2020 08:34:23 -0400 Received: from mail.kernel.org ([198.145.29.99]:37022 "EHLO mail.kernel.org" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1729623AbgI2LW4 (ORCPT ); Tue, 29 Sep 2020 07:22:56 -0400 Received: from localhost (83-86-74-64.cable.dynamic.v4.ziggo.nl [83.86.74.64]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by mail.kernel.org (Postfix) with ESMTPSA id 8283123A75; Tue, 29 Sep 2020 11:20:23 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=default; t=1601378424; bh=Ck6fc4SttpI6j9QYpn6//UUfsHchD6fGEC7acmbMEqY=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=GvF/QdwxhClWOHonn6PsOIby/7efV2c0hZWpwXSH7Fpb5zYFKSGUhxuncIwxRx6Dr OWwSeEwEZa+X1PpBt/no1E3hZRsBKVpjp83LtYoKB/IQ8EhsVXuIN5Qq979YYs+KPo KgYG3SUg9Wsn1xBAa8bY/Asw/qhJtCbMbhV/uNDM= From: Greg Kroah-Hartman To: linux-kernel@vger.kernel.org Cc: Greg Kroah-Hartman , stable@vger.kernel.org, Victor Kamensky , Jonathan Lebon , Paul Moore , Sasha Levin Subject: [PATCH 4.19 001/245] selinux: allow labeling before policy is loaded Date: Tue, 29 Sep 2020 12:57:32 +0200 Message-Id: <20200929105947.056525770@linuxfoundation.org> X-Mailer: git-send-email 2.28.0 In-Reply-To: <20200929105946.978650816@linuxfoundation.org> References: <20200929105946.978650816@linuxfoundation.org> User-Agent: quilt/0.66 X-stable: review X-Patchwork-Hint: ignore MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org From: Jonathan Lebon [ Upstream commit 3e3e24b42043eceb97ed834102c2d094dfd7aaa6 ] 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] https://github.com/coreos/fedora-coreos-tracker/issues/94 [3] https://www.spinics.net/lists/linux-initramfs/msg04593.html Co-developed-by: Victor Kamensky Signed-off-by: Victor Kamensky Signed-off-by: Jonathan Lebon Signed-off-by: Paul Moore Signed-off-by: Sasha Levin --- security/selinux/hooks.c | 12 ++++++++++++ 1 file changed, 12 insertions(+) diff --git a/security/selinux/hooks.c b/security/selinux/hooks.c index 452254fd89f87..250b725f5754c 100644 --- a/security/selinux/hooks.c +++ b/security/selinux/hooks.c @@ -3304,6 +3304,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; @@ -3387,6 +3390,15 @@ static void selinux_inode_post_setxattr(struct dentry *dentry, const char *name, return; } + 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, &newsid); if (rc) { -- 2.25.1