Received: by 2002:a05:6a10:16a7:0:0:0:0 with SMTP id gp39csp1369390pxb; Fri, 6 Nov 2020 07:58:48 -0800 (PST) X-Google-Smtp-Source: ABdhPJzzfea4x01ElZ1G6UN5R2Ri0s8w6w+3ktxDnLiN58gzklzoYnIsNKxnrRTK50Ip+nWHvvKb X-Received: by 2002:a17:906:374b:: with SMTP id e11mr2610442ejc.339.1604678327982; Fri, 06 Nov 2020 07:58:47 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1604678327; cv=none; d=google.com; s=arc-20160816; b=fbKx7AhJnzg4jdTx3E1eFGEOauRQKuGRz2raq/2vge4axwOldC58cnW3AYkt4tedna 2JTmj8kNN3HQRvIGGgYtygd5nsxEKdzbvzmFItsEG8AAuEBHxHCUUi5ZIZeNjFoPLCEb jAwrl40Z8TsGKsp8TMfeOwBnTcFhvrLLf2KX/y7wOWIXg1BxVkpuVr6DEKpCrJydnQOc klhcfwSEW2VdRLUuRwtuFJQoJS1lFlsuCCaJHa0FGusx4d7WejgZAdcYhE0yskmEbkuQ eTqIDigAyor4HIig0pwblNDkDJKZ1tvFuoSfgjRoONk9cmvywWKpQrlkii0DsPiiZEqN 6QAw== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:cc:to:from:subject:references:mime-version :message-id:in-reply-to:date:sender:dkim-signature; bh=L4KGFFWDHlU66vPQG585OdMG0AUk6bVr7qLzNQZAqk0=; b=oBfRtWVJ8C1LI0kO90DoqZwAB6l1LXFAGVdjbY5V/fs9PseSpoRpaq6xvU8KpPRFEY yL6ld3Y7otlqYvbYbioHMod6jzm2UzBSU1TgaVhb/yLB4QkRg/55JEA71fKVjqUXjRM7 n4GQQGTnN34hURuMy3e3aUmQARcfKsYLG/sSnC7BjxB/3zAmKGzn/t2FphGqysmWN6YK rCQbbPFU4c3eKODDu8oDX/Xw4ZqGZnDdJ/5nKbnV0CEw8AoYijAnsIQj6CkS3Rc8D+y8 BJ7dpPBeJUkiwIkvc79FGEEH557UuGOIRwmpJqL2Xw+na/DUd/tWRyWMfmQ04P8Rule1 SKCA== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@google.com header.s=20161025 header.b=ffl1DjsL; 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 mc11si1126556ejb.154.2020.11.06.07.58.24; Fri, 06 Nov 2020 07:58:47 -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=ffl1DjsL; 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 S1727744AbgKFP4r (ORCPT + 99 others); Fri, 6 Nov 2020 10:56:47 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:57468 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1727719AbgKFP4o (ORCPT ); Fri, 6 Nov 2020 10:56:44 -0500 Received: from mail-yb1-xb49.google.com (mail-yb1-xb49.google.com [IPv6:2607:f8b0:4864:20::b49]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id D0698C0613CF for ; Fri, 6 Nov 2020 07:56:43 -0800 (PST) Received: by mail-yb1-xb49.google.com with SMTP id w8so2136887ybj.14 for ; Fri, 06 Nov 2020 07:56:43 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20161025; h=sender:date:in-reply-to:message-id:mime-version:references:subject :from:to:cc; bh=L4KGFFWDHlU66vPQG585OdMG0AUk6bVr7qLzNQZAqk0=; b=ffl1DjsLtbx4Na6zWHq8zJ/fNOh3xFnqff+6leag11fesgjIHAPz7OMbIQHlSJDtsL vp1Hf866chPTwdiGFaezEgU/0zK/96RUqzjiFUGhXJAbltgNqmkuWbzq4GptwFEzYzZ0 +FHYk/rzrzQwtMx7FF3/5ctZf3gaa/xTSZKilLw6U7drImewFIUOwm5TI3Izl2dov6Ps S8xJ2FZDBpOefQEjDSUiTusb1rqoqHzmhLRdw9JRBHUKW+h2Go7C2NJHLRyM44RROrnI u2RiDTPBrn66PObViiWK3uwTe0MSK6s2ZbX10/FnIpl8VFQVEMJjpV5f4yQBAIAvoM5c s5xQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:sender:date:in-reply-to:message-id:mime-version :references:subject:from:to:cc; bh=L4KGFFWDHlU66vPQG585OdMG0AUk6bVr7qLzNQZAqk0=; b=BlE6zCYU3ZrSTzqv2mrfjn4BVk5BHNKQPHtUh9u5Pp0WYI2SkdjmOQNdfZaLQKQPcb vWvubktXi/KWXhxYwnHGc0R8rv/B9HPfwB+kugpqgMBJRFKPE4ESwWOIGfaiXZEKOkqb WdNI8NaElChwYrnBUzSWiBiW6CV13CvEBH2svLhyAeQmh5/Gk2JLCjKsh07lLBrrXg4W 6iRTVhimcHdjJ4p5YJQsrhegSwe5sy96d5BdEtoiWMk1lgVZDjwz2Ka3zA5g14GE1Fvb CiDJ+9PIf4e7mA/qVMjGysApssRsArUdX/l3IBjuGUXrozVT1qSML+z4MtVs5WrmacXR 3Oxg== X-Gm-Message-State: AOAM531yMNTJtTY01/7IujWTKUdpQS3dTY9FO3vLgD9gzUNhwgLe/Cx6 +JfKIsW+F+hrhfN89KIT69MoGkoBPcvp0BcS7Q== Sender: "lokeshgidra via sendgmr" X-Received: from lg.mtv.corp.google.com ([2620:15c:211:202:f693:9fff:fef4:29dd]) (user=lokeshgidra job=sendgmr) by 2002:a25:60d7:: with SMTP id u206mr3591699ybb.315.1604678202914; Fri, 06 Nov 2020 07:56:42 -0800 (PST) Date: Fri, 6 Nov 2020 07:56:25 -0800 In-Reply-To: <20201106155626.3395468-1-lokeshgidra@google.com> Message-Id: <20201106155626.3395468-4-lokeshgidra@google.com> Mime-Version: 1.0 References: <20201106155626.3395468-1-lokeshgidra@google.com> X-Mailer: git-send-email 2.29.1.341.ge80a0c044ae-goog Subject: [PATCH v12 3/4] selinux: teach SELinux about anonymous inodes From: Lokesh Gidra To: Andrea Arcangeli , Alexander Viro , James Morris , Stephen Smalley , Casey Schaufler , Eric Biggers Cc: "Serge E. Hallyn" , Paul Moore , Eric Paris , Lokesh Gidra , Daniel Colascione , Kees Cook , "Eric W. Biederman" , KP Singh , David Howells , Thomas Cedeno , 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-fsdevel@vger.kernel.org, linux-kernel@vger.kernel.org, linux-security-module@vger.kernel.org, selinux@vger.kernel.org, kaleshsingh@google.com, calin@google.com, surenb@google.com, nnk@google.com, jeffv@google.com, kernel-team@android.com, linux-mm@kvack.org, Andrew Morton , hch@infradead.org, Daniel Colascione Content-Type: text/plain; charset="UTF-8" Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org 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 | 53 +++++++++++++++++++++++++++++ security/selinux/include/classmap.h | 2 ++ 2 files changed, 55 insertions(+) diff --git a/security/selinux/hooks.c b/security/selinux/hooks.c index 6b1826fc3658..1c0adcdce7a8 100644 --- a/security/selinux/hooks.c +++ b/security/selinux/hooks.c @@ -2927,6 +2927,58 @@ 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); + isec->sclass = context_isec->sclass; + isec->sid = context_isec->sid; + } else { + isec->sclass = SECCLASS_ANON_INODE; + rc = security_transition_sid( + &selinux_state, tsec->sid, tsec->sid, + isec->sclass, name, &isec->sid); + if (rc) + return rc; + } + + isec->initialized = LABEL_INITIALIZED; + + /* + * Now that we've initialized security, check whether we're + * allowed to actually create this type of anonymous inode. + */ + + ad.type = LSM_AUDIT_DATA_INODE; + ad.u.inode = inode; + + return avc_has_perm(&selinux_state, + tsec->sid, + isec->sid, + isec->sclass, + FILE__CREATE, + &ad); +} + static int selinux_inode_create(struct inode *dir, struct dentry *dentry, umode_t mode) { return may_create(dir, dentry, SECCLASS_FILE); @@ -6992,6 +7044,7 @@ static struct security_hook_list selinux_hooks[] __lsm_ro_after_init = { LSM_HOOK_INIT(inode_free_security, selinux_inode_free_security), LSM_HOOK_INIT(inode_init_security, selinux_inode_init_security), + LSM_HOOK_INIT(inode_init_security_anon, selinux_inode_init_security_anon), LSM_HOOK_INIT(inode_create, selinux_inode_create), LSM_HOOK_INIT(inode_link, selinux_inode_link), LSM_HOOK_INIT(inode_unlink, selinux_inode_unlink), diff --git a/security/selinux/include/classmap.h b/security/selinux/include/classmap.h index 40cebde62856..ba2e01a6955c 100644 --- a/security/selinux/include/classmap.h +++ b/security/selinux/include/classmap.h @@ -249,6 +249,8 @@ struct security_class_mapping secclass_map[] = { {"open", "cpu", "kernel", "tracepoint", "read", "write"} }, { "lockdown", { "integrity", "confidentiality", NULL } }, + { "anon_inode", + { COMMON_FILE_PERMS, NULL } }, { NULL } }; -- 2.29.1.341.ge80a0c044ae-goog