Received: by 2002:a05:6a10:2785:0:0:0:0 with SMTP id ia5csp579018pxb; Fri, 8 Jan 2021 12:20:51 -0800 (PST) X-Google-Smtp-Source: ABdhPJxxaLaWi6s591sr19TMPdLJ6zpaIUkixWcN051Md73hrcFjiNYQ5DEBnaKteLihICz17UOJ X-Received: by 2002:a17:906:1955:: with SMTP id b21mr3764495eje.236.1610137251703; Fri, 08 Jan 2021 12:20:51 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1610137251; cv=none; d=google.com; s=arc-20160816; b=HO1VCfWlDdz0xSme1BmV+zv3Uvp474TR8pXJFqONLPRk1XLlrmbOkNyRCW8LVU4PD8 wCDne6eQzZxyvBr8ZIHg+5OLo9h0m8jgbNE/zFrCAUdVp+EpTpmWF0QU8p5RcbbEcTwJ 1HnbGPRZoqDAdTc+u47E77IbL/6l5Net5Id2eCmRECNS3nHCm7BZ7AkRE8ogJmfF54z6 BPe0ke3WG0sb7TC5sOx0FOn2aUdmDAopoiiqYrSe9SNeqSa6nHoMgCQgpIwUVxgo1sDt /lY56u2uKvVH+fpFfKGdmtRitssNWZaihhdrGidonQWNq+M7ER/bWgj0jnKEQMXW8pl8 ieig== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:cc:to:subject:message-id:date:from:in-reply-to :references:mime-version:dkim-signature; bh=EwrOgDTg96KrzDvgtinwO514AtzHfwPfpJcfahxEYVI=; b=vypeYOIQss0IwAuAakHfdLAvjL4DaJJ1UMjJPLJV2aOHwq0OTl2xauwJH5nBTerQqv HmoULowNFWxLvdCXpF6uJg3TZ/koCltVzccZaEQEEM3XqhdCX3GNwZKeGOXprF6nx8vk YzM4NWpQtVgfb5KLDDY49Sn3PNZztHFGZXaD8ChRgE9DBRBx5RcAY9mi1/s6kDJ8xQVq 8BNdmDtiHT+y7OxP1iiwSVmPySpRGX5qsnhFn8jEm8H3Yg9A9PNPSyCaY3kuViP1xXKP 5X29so8ie59crrHkhgpehxPdHgZi7RsrqHQgvBVego1mzbjX+mpujmNZ81eRlcgaiGNM kiDQ== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@google.com header.s=20161025 header.b=aGlRquYV; 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=REJECT sp=REJECT dis=NONE) header.from=google.com Return-Path: Received: from vger.kernel.org (vger.kernel.org. [23.128.96.18]) by mx.google.com with ESMTP id oc24si3934841ejb.367.2021.01.08.12.20.27; Fri, 08 Jan 2021 12:20:51 -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=@google.com header.s=20161025 header.b=aGlRquYV; 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=REJECT sp=REJECT dis=NONE) header.from=google.com Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1729534AbhAHUSR (ORCPT + 99 others); Fri, 8 Jan 2021 15:18:17 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:52782 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1729504AbhAHUSH (ORCPT ); Fri, 8 Jan 2021 15:18:07 -0500 Received: from mail-ej1-x62c.google.com (mail-ej1-x62c.google.com [IPv6:2a00:1450:4864:20::62c]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 89E36C0612EA for ; Fri, 8 Jan 2021 12:17:26 -0800 (PST) Received: by mail-ej1-x62c.google.com with SMTP id n26so16159396eju.6 for ; Fri, 08 Jan 2021 12:17:26 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20161025; h=mime-version:references:in-reply-to:from:date:message-id:subject:to :cc; bh=EwrOgDTg96KrzDvgtinwO514AtzHfwPfpJcfahxEYVI=; b=aGlRquYV7bBoqWOfhg/mBYEuzggSozf1JOKAipPHB6CHoFm9cW3lprBEnleXHV82pE r5stn0vhAinJiv33KPHJ5w2fLw5Icm3NYqX8LQ0wAt21tWNTl6vudMYutWVrDrufXsKL 5FFs6hddoUdt10NDUrvs+56xFAfqToaAXTQent1IQQntDeUhmZ/QkLQcxbY/QmNj0Dca 4weEE7eCLoFXkAgKjbAxDWd7Haie/lzKOV1kXZl5EnV7TrZ3SpiwDi9RMToSkT0It6C6 HcFCJqGsfukio8J9XbsSYIhGB9QtbX2JDlLjWpn94zM1pIQkBdL0pEHk7skG8iTc9zyH kVbw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:mime-version:references:in-reply-to:from:date :message-id:subject:to:cc; bh=EwrOgDTg96KrzDvgtinwO514AtzHfwPfpJcfahxEYVI=; b=HfT4TNV3/quOXodkHgSfQljXD8c1D9Ocx/zCp6foa2mlvAx4jxpi8Kxb6uXkZILPSF gPx/XhCz7OA7CVQZzgUSgbuD1gtDB4yTZssZwAnD7GiMDWXb6z16wKRCi4+mt443CmSa IU4fvBoUolvx9pK77X+OKSm1aEV2OIb6MxzypalRh4srCC1iLs24RDvn3zbWMk5O7moj peJvuKPcuUBX0+e2X6lKX3bawcyczzixgdbXl708uV5dCbjHmkrMybOVQeW8Omfhh3MR sXKJJBLMOC4lN5SW7KhMadp4wNLM3z0Cwg6WmXwRxDjXsxVruiSHQWKldLd2tErhWBLH wMMw== X-Gm-Message-State: AOAM531a6WccrZ+a6McU0QsKCyy5xvux45J6UyQa09eB2b8zRRnmZe9M veSjhbxLvCepKs3vuRuzkOPdCI30M0LFC+cxY/eT8g== X-Received: by 2002:a17:907:101c:: with SMTP id ox28mr3462266ejb.201.1610137044856; Fri, 08 Jan 2021 12:17:24 -0800 (PST) MIME-Version: 1.0 References: <20201112015359.1103333-1-lokeshgidra@google.com> <20201112015359.1103333-4-lokeshgidra@google.com> In-Reply-To: From: Lokesh Gidra Date: Fri, 8 Jan 2021 12:17:13 -0800 Message-ID: Subject: Re: [PATCH v13 3/4] selinux: teach SELinux about anonymous inodes To: Stephen Smalley Cc: Paul Moore , Andrea Arcangeli , Alexander Viro , James Morris , Casey Schaufler , Eric Biggers , "Serge E. Hallyn" , Eric Paris , Daniel Colascione , Kees Cook , "Eric W. Biederman" , KP Singh , David Howells , Anders Roxell , Sami Tolvanen , Matthew Garrett , Aaron Goidel , Randy Dunlap , "Joel Fernandes (Google)" , YueHaibing , Christian Brauner , Alexei Starovoitov , Alexey Budankov , Adrian Reber , Aleksa Sarai , Linux FS Devel , linux-kernel , LSM List , SElinux list , Kalesh Singh , Calin Juravle , Suren Baghdasaryan , Jeffrey Vander Stoep , "Cc: Android Kernel" , "open list:MEMORY MANAGEMENT" , Andrew Morton , Christoph Hellwig , Daniel Colascione Content-Type: text/plain; charset="UTF-8" Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org On Fri, Jan 8, 2021 at 11:35 AM Stephen Smalley wrote: > > On Wed, Jan 6, 2021 at 10:03 PM Paul Moore wrote: > > > > On Wed, Nov 11, 2020 at 8:54 PM Lokesh Gidra wrote: > > > From: Daniel Colascione > > > > > > This change uses the anon_inodes and LSM infrastructure introduced in > > > the previous patches to give SELinux the ability to control > > > anonymous-inode files that are created using the new > > > anon_inode_getfd_secure() function. > > > > > > A SELinux policy author detects and controls these anonymous inodes by > > > adding a name-based type_transition rule that assigns a new security > > > type to anonymous-inode files created in some domain. The name used > > > for the name-based transition is the name associated with the > > > anonymous inode for file listings --- e.g., "[userfaultfd]" or > > > "[perf_event]". > > > > > > Example: > > > > > > type uffd_t; > > > type_transition sysadm_t sysadm_t : anon_inode uffd_t "[userfaultfd]"; > > > allow sysadm_t uffd_t:anon_inode { create }; > > > > > > (The next patch in this series is necessary for making userfaultfd > > > support this new interface. The example above is just > > > for exposition.) > > > > > > Signed-off-by: Daniel Colascione > > > Signed-off-by: Lokesh Gidra > > > --- > > > security/selinux/hooks.c | 56 +++++++++++++++++++++++++++++ > > > security/selinux/include/classmap.h | 2 ++ > > > 2 files changed, 58 insertions(+) > > > > > > diff --git a/security/selinux/hooks.c b/security/selinux/hooks.c > > > index 6b1826fc3658..d092aa512868 100644 > > > --- a/security/selinux/hooks.c > > > +++ b/security/selinux/hooks.c > > > @@ -2927,6 +2927,61 @@ 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 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(!selinux_initialized(&selinux_state))) > > > + return 0; > > > + > > > + 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 (context_isec->initialized != LABEL_INITIALIZED) > > > + return -EACCES; Stephen, as per your explanation below, is this check also problematic? I mean is it possible that /dev/kvm context_inode may not have its label initialized? If so, then v12 of the patch series can be used as is. Otherwise, I will send the next version which rollbacks v14 and v13, except for this check. Kindly confirm. > > > + > > > + isec->sclass = context_isec->sclass; > > > > Taking the object class directly from the context_inode is > > interesting, and I suspect problematic. In the case below where no > > context_inode is supplied the object class is set to > > SECCLASS_ANON_INODE, which is correct, but when a context_inode is > > supplied there is no guarantee that the object class will be set to > > SECCLASS_ANON_INODE. This could both pose a problem for policy > > writers (how do you distinguish the anon inode from other normal file > > inodes in this case?) as well as an outright fault later in this > > function when we try to check the ANON_INODE__CREATE on an object > > other than a SECCLASS_ANON_INODE object. > > > > It works in the userfaultfd case because the context_inode is > > originally created with this function so the object class is correctly > > set to SECCLASS_ANON_INODE, but can we always guarantee that to be the > > case? Do we ever need or want to support using a context_inode that > > is not SECCLASS_ANON_INODE? > > Sorry, I haven't been following this. IIRC, the original reason for > passing a context_inode was to support the /dev/kvm or similar use > cases where the driver is creating anonymous inodes to represent > specific objects/interfaces derived from the device node and we want > to be able to control subsequent ioctl operations on those anonymous > inodes in the same manner as for the device node. For example, ioctl > operations on /dev/kvm can end up returning file descriptors for > anonymous inodes representing a specific VM or VCPU or similar. If we > propagate the security class and SID from the /dev/kvm inode (the > context inode) to the new anonymous inode, we can write a single > policy rule over all ioctl operations related to /dev/kvm. That's > also why we used the FILE__CREATE permission here originally; that was > also intentional. All the file-related classes including anon_inode > inherit a common set of file permissions including create and thus we > often use the FILE__ in common code when checking > permission against any potentially derived class. Thanks a lot for the explanation.