Received: by 2002:a05:6a10:f347:0:0:0:0 with SMTP id d7csp9173812pxu; Mon, 28 Dec 2020 08:24:35 -0800 (PST) X-Google-Smtp-Source: ABdhPJyfVG77TDV30xVbVrWEqDIq8YstKPjBDMjo8VydGt6bmPkrPuZgP0sLpfN33xR6wvga+LS6 X-Received: by 2002:a17:907:111c:: with SMTP id qu28mr41830788ejb.540.1609172675505; Mon, 28 Dec 2020 08:24:35 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1609172675; cv=none; d=google.com; s=arc-20160816; b=Rc8Jgyik0mgV6R0KMkfx7EZNQstS6W10OLBO7wi8DTSmXc8q0+phs4aNh373v1u5Sy HTWUEWm+tZsFZLLpsMZDUgo5HqYf+xhFenReD3tLh7dbRsBMBLgZoNn545wnnAW6WWL0 3DlyJ3/LtY5keypv6HftOszzavZd/SRM2B9o983PiWamOOY0+t5zeoRiLa0zOunkjP10 GmKtfvsX4ZHXHlp47NcFBLLTYEHI8+aTqYvY7sNr2MjT47Csve8w2x4jUyZhSBFxJDbg HGfaanE7VZo5l9/xf+Vn2EV7L3589CVrv6W7CnFHJbnH2Fo1OLgO/1P2MzENCYe7KA2a X9fw== 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=Do657ghHSio61oU7s0THsLmaWNeqIbuj/Ax0+5wMunw=; b=Gcj4U6v4SvXJvWcDIAmS6aGcqfI6Ur90ZnqGLM9gxjnytpYhI+TuQ3aVpxWqHv6Pi+ pKFBWWpHlPpdYqDUDyQPZYQ5Fe6EPS/zpMBJQpcfRcKNUaXXL3bqGDgoiPnhNHnVH+Oc BDkUK3nIvMUVg47EGq/YYTEg4CaFApOGwJ+047u7IIeeXJh1hWGgDW4q+njQAw2PRC+D +6v2r9tb7jL+ule9xxTJLM7NgvYOXWR7zK/e6qnWOs+fitU3N/VPz468jkdgpVQ5WU1M QQXJQchb03SxaRrpYwrh1SUZluXk4Pq5AmrqJqT0t6fO8yHD3fWOqQvq2+ksJcR6CYDN EKOw== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@linuxfoundation.org header.s=korg header.b=eeS31qM+; 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=pass (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 v1si11432643ejd.298.2020.12.28.08.24.12; Mon, 28 Dec 2020 08:24:35 -0800 (PST) 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=@linuxfoundation.org header.s=korg header.b=eeS31qM+; 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=pass (p=NONE sp=NONE dis=NONE) header.from=linuxfoundation.org Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S2441002AbgL1QWT (ORCPT + 99 others); Mon, 28 Dec 2020 11:22:19 -0500 Received: from mail.kernel.org ([198.145.29.99]:39816 "EHLO mail.kernel.org" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1731995AbgL1NMQ (ORCPT ); Mon, 28 Dec 2020 08:12:16 -0500 Received: by mail.kernel.org (Postfix) with ESMTPSA id 1124B20776; Mon, 28 Dec 2020 13:11:59 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=linuxfoundation.org; s=korg; t=1609161120; bh=g/v+1rTa2gjUBjXl4NS7as3TpK2nuD+hQqywd1ongmw=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=eeS31qM+ptCUMB42JU+uGQx2grLT9yp/O0kEaMBCaRtrm5k6WAammGAKBTxVnRdgE ItX30zy8+it2+ZlTBeJLDxVu7nesEjL1cunckdHoJMAceEvfDr3gaCx1O2NCGISWAm cLJBuQklmSVD+2OFQRVRl3wZRiHLdBWwefIc9awM= From: Greg Kroah-Hartman To: linux-kernel@vger.kernel.org Cc: Greg Kroah-Hartman , stable@vger.kernel.org, Sven Schnelle , Ondrej Mosnacek , Paul Moore , Sasha Levin Subject: [PATCH 4.14 078/242] selinux: fix inode_doinit_with_dentry() LABEL_INVALID error handling Date: Mon, 28 Dec 2020 13:48:03 +0100 Message-Id: <20201228124908.524696329@linuxfoundation.org> X-Mailer: git-send-email 2.29.2 In-Reply-To: <20201228124904.654293249@linuxfoundation.org> References: <20201228124904.654293249@linuxfoundation.org> User-Agent: quilt/0.66 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: Paul Moore [ Upstream commit 200ea5a2292dc444a818b096ae6a32ba3caa51b9 ] A previous fix, commit 83370b31a915 ("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: 83370b31a915 ("selinux: fix error initialization in inode_doinit_with_dentry()") Reported-by: Sven Schnelle Tested-by: Sven Schnelle Reviewed-by: Ondrej Mosnacek Signed-off-by: Paul Moore Signed-off-by: Sasha Levin --- security/selinux/hooks.c | 31 +++++++++++++------------------ 1 file changed, 13 insertions(+), 18 deletions(-) diff --git a/security/selinux/hooks.c b/security/selinux/hooks.c index af8ddae0ddedb..895d369bc4103 100644 --- a/security/selinux/hooks.c +++ b/security/selinux/hooks.c @@ -1569,13 +1569,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; } len = INITCONTEXTLEN; @@ -1683,15 +1677,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); dput(dentry); @@ -1704,11 +1691,10 @@ 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; } @@ -1716,6 +1702,15 @@ out: 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. */ -- 2.27.0