Received: by 2002:a05:6a10:2785:0:0:0:0 with SMTP id ia5csp600625pxb; Fri, 8 Jan 2021 13:00:30 -0800 (PST) X-Google-Smtp-Source: ABdhPJxghwlNM9Ig/nm9cogi3y+F3Xsu5XiYi2bug27synMrDrjmz4C9MDLQr9R0kY4rDciwp4TL X-Received: by 2002:a17:906:f0c9:: with SMTP id dk9mr3841496ejb.51.1610139630167; Fri, 08 Jan 2021 13:00:30 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1610139630; cv=none; d=google.com; s=arc-20160816; b=QCtqveNKS5g/yvlT3O0lghJR+zJv36zOXtqy2DH+Ye9iA0m43JTqiaMgE3v3IbBLl9 HtGqw546bpcC1a+SD9uQEySzkxEk9zTVGcLXlz14S1/Kq4Q8KureIPvKn4SlP0LVVvOz pfH2/HQ/LO+nSuWA5X+DpRn6Z/A/oU7bnFXz3OVsVY4wWEqu/6jNJ4z3jwGvE6hQEcMV nJ7SUVBxZ8rO1t7GVxHCUB9gyKQM8pjlXAWPQkQQsb+XZVLolr9F61d/dQUoVhNE3cq2 P/qeAUc+4JU2jCnjoV73LxfwtZaoHOZCv2UY9BYewCJknBBDC3cNL12qbRzRHhbmToXe sYwA== 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=khq1BhJKP5TVovoHPldpcRP4cKiwHOG+v7RRKqn8j58=; b=0oLYDT5UKaC3F6md20oFD7p+Fa1jb/vpfmbznmUczWnNrboeRJHIh8N+S1rl9sF2QZ WLthDY8Opp8lAHXdjjq0IDQl3oa7WShyNNGDVj3f8T5YcCZtqn1TcpsTqpF1nd2qM260 KYwxRFOyDpPjbbxhs5ekpNRBQCEpFaLRvG7UzRR7kPOrv0U+lP/YsJG5CeMqMtTkngo2 OHXrFaVa28+psiPYo358gxpZMYIT1Km54vWz5+s6sq0WrBE9yiw0++IvJuH0Ix9NXdFx PzGrNzOp1mx2jJ/xkAXfEwkYV+kc2eU8kyUkHJXj60Q7rr4xgL03NvBD7KyTK2POhGmO MYmA== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@paul-moore-com.20150623.gappssmtp.com header.s=20150623 header.b=obA6xuhm; 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 Return-Path: Received: from vger.kernel.org (vger.kernel.org. [23.128.96.18]) by mx.google.com with ESMTP id cq10si3808813edb.140.2021.01.08.13.00.06; Fri, 08 Jan 2021 13:00:30 -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=@paul-moore-com.20150623.gappssmtp.com header.s=20150623 header.b=obA6xuhm; 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 Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1729422AbhAHU7a (ORCPT + 99 others); Fri, 8 Jan 2021 15:59:30 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:59234 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1727003AbhAHU7a (ORCPT ); Fri, 8 Jan 2021 15:59:30 -0500 Received: from mail-ej1-x62d.google.com (mail-ej1-x62d.google.com [IPv6:2a00:1450:4864:20::62d]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 6C563C061380 for ; Fri, 8 Jan 2021 12:58:49 -0800 (PST) Received: by mail-ej1-x62d.google.com with SMTP id x16so16286079ejj.7 for ; Fri, 08 Jan 2021 12:58:49 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=paul-moore-com.20150623.gappssmtp.com; s=20150623; h=mime-version:references:in-reply-to:from:date:message-id:subject:to :cc; bh=khq1BhJKP5TVovoHPldpcRP4cKiwHOG+v7RRKqn8j58=; b=obA6xuhmm9kV3+sXrUgxMMsfFga8AGTLVYwR5OX5nJ2XlW6dNnOubwbilqkj0TMDh8 T6qAuoauDw+XXUxxElzoTtfFf8VMi/L3cL9wpra8fzRz/wdI150FS4zxBD61HcPZy1vR A2E91WGbe5uDVTG7ZQVJvENnGfVNZ4Y8niHT1pIza7xgove9CGQ4CF6CSrurFmwazb2q 865Ias4cNka3b/TIVm4UJ4SWpJxjm3Az8lE+E/Pn+5qvhwWyelWJb62Lx+uMZ3jLbsH6 /OEoKonYp9djudJ6iqMYJ5mJil8vbsCZWb0PG4tjs6RI5UTLM9ZDwq3LR5ukjgANpAYY OBoA== 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=khq1BhJKP5TVovoHPldpcRP4cKiwHOG+v7RRKqn8j58=; b=kixJHW5nyEN+tHo74aCq4mB3nDuLZ9+B+z0K2zwxEg/B1phDNV34S4i1XQbWZNQUEV wL/sxfR3LrLpSXBT1oDyLUn489nk2XNPu7HCEoa1VMLF9fwiZBHkSLAVQGxA99NKHdC/ gSIhsZSjNi735qJLftz+UyCDCKA9sle+X3bQSY9wy6mXniSOvurq0xW2sg8uuiHoNZ90 PFu7oRLJOF3OlBxIcTj+b+yukQGDzSBDQNARZFCQT9zt1Vvc+RgjrTKL5P1HfETEKRXw 5lJkta0ajXFt9lJsmYQVMUUFBSLOWeQT14ITV9uj9Gk+Ru1kjTrzM+UT8I/cS100zpqr u/ug== X-Gm-Message-State: AOAM533enGL2Pw2oMEL0KyCmdDsCIp8uoeI5K8g9mP95U/TjrgRP+8f8 IIUExGW9nJQ7zhet+WGgeu9FVo5a+Vj5YoQauVPv X-Received: by 2002:a17:906:aec6:: with SMTP id me6mr3822560ejb.542.1610139527942; Fri, 08 Jan 2021 12:58:47 -0800 (PST) MIME-Version: 1.0 References: <20201112015359.1103333-1-lokeshgidra@google.com> <20201112015359.1103333-4-lokeshgidra@google.com> In-Reply-To: From: Paul Moore Date: Fri, 8 Jan 2021 15:58:36 -0500 Message-ID: Subject: Re: [PATCH v13 3/4] selinux: teach SELinux about anonymous inodes To: Stephen Smalley Cc: Lokesh Gidra , 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 , kaleshsingh@google.com, Calin Juravle , Suren Baghdasaryan , Jeffrey Vander Stoep , kernel-team@android.com, linux-mm@kvack.org, 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 2:35 PM 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; > > > + > > > + 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. Thanks for the background, and the /dev/kvm example, that is what I was missing. > 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. Yes, if all of the anonymous inodes are not going to fall into the anon_inode object class then FILE__CREATE makes the most sense. Thanks Stephen. -- paul moore www.paul-moore.com