From patchwork Fri Jan 8 22:22:22 2021 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Lokesh Gidra X-Patchwork-Id: 12007655 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org X-Spam-Level: X-Spam-Status: No, score=-16.6 required=3.0 tests=BAYES_00, DKIM_ADSP_CUSTOM_MED,DKIM_INVALID,DKIM_SIGNED,HEADER_FROM_DIFFERENT_DOMAINS, INCLUDES_CR_TRAILER,INCLUDES_PATCH,MAILING_LIST_MULTI,SPF_HELO_NONE,SPF_PASS, USER_AGENT_GIT autolearn=ham autolearn_force=no version=3.4.0 Received: from mail.kernel.org (mail.kernel.org [198.145.29.99]) by smtp.lore.kernel.org (Postfix) with ESMTP id 78E07C433DB for ; Fri, 8 Jan 2021 22:22:34 +0000 (UTC) Received: from kanga.kvack.org (kanga.kvack.org [205.233.56.17]) by mail.kernel.org (Postfix) with ESMTP id 18CCE23A74 for ; Fri, 8 Jan 2021 22:22:34 +0000 (UTC) DMARC-Filter: OpenDMARC Filter v1.3.2 mail.kernel.org 18CCE23A74 Authentication-Results: mail.kernel.org; dmarc=fail (p=reject dis=none) header.from=google.com Authentication-Results: mail.kernel.org; spf=pass smtp.mailfrom=owner-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix) id 9CF948D01BA; Fri, 8 Jan 2021 17:22:33 -0500 (EST) Received: by kanga.kvack.org (Postfix, from userid 40) id 9A6198D01B9; Fri, 8 Jan 2021 17:22:33 -0500 (EST) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 8BC7D8D01BA; Fri, 8 Jan 2021 17:22:33 -0500 (EST) X-Delivered-To: linux-mm@kvack.org Received: from forelay.hostedemail.com (smtprelay0025.hostedemail.com [216.40.44.25]) by kanga.kvack.org (Postfix) with ESMTP id 76CA88D01B9 for ; Fri, 8 Jan 2021 17:22:33 -0500 (EST) Received: from smtpin05.hostedemail.com (10.5.19.251.rfc1918.com [10.5.19.251]) by forelay03.hostedemail.com (Postfix) with ESMTP id 397C6824556B for ; Fri, 8 Jan 2021 22:22:33 +0000 (UTC) X-FDA: 77684032986.05.sack66_170373f274f6 Received: from filter.hostedemail.com (10.5.16.251.rfc1918.com [10.5.16.251]) by smtpin05.hostedemail.com (Postfix) with ESMTP id 205101804176F for ; Fri, 8 Jan 2021 22:22:33 +0000 (UTC) X-HE-Tag: sack66_170373f274f6 X-Filterd-Recvd-Size: 8224 Received: from mail-pf1-f202.google.com (mail-pf1-f202.google.com [209.85.210.202]) by imf09.hostedemail.com (Postfix) with ESMTP for ; Fri, 8 Jan 2021 22:22:32 +0000 (UTC) Received: by mail-pf1-f202.google.com with SMTP id e4so7493146pfc.11 for ; Fri, 08 Jan 2021 14:22:32 -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=6MJ/Mtjmj+33/ewT428wH6PWq4EtZoCoS3wdjfPfGts=; b=YYA1E41BAWtjcDPOBAvpekyYblM+e/qSQgBAE3fG3J57Cpks8qgBce0gR9ieQoqLaZ 4c3I+dF72GHTzEhbVVK7I6bjXiIwriJd3i7MLXn+NjxcgsLgTX8/qZQPsJ6u11Lho/Wr Sqd2uqG1w4mI0eEaSxpdjdJlE2/ts+VwwbLvpQAHiG0e06iABStKdD7WPk3USIzKVX1o /5eyvtApXAJHhxniqyekk3niTJ4gze+1XcBW+PHFQ6YCp5TJONDi/GQRE2UAE/j7Wvzw AIqaUj6vuiTPqrfldlPxVhBvnsJXWxjy25n6o7UMy7nXjdoR7WWgOB+zPTtvQk1htEdp ilJQ== 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=6MJ/Mtjmj+33/ewT428wH6PWq4EtZoCoS3wdjfPfGts=; b=Ltw14Jtuuar2FSvxZyxRpwqpZkDH1JZHzAA9y51Fj6f2L4Ts/Afa75FU1uAvSgI12a wFEPkghJ4En2wQwlmpCra+3TGizUEau2KWwme5CcG5LZ8WGZv8A4Mgv4l/zQ+elDJvnA rdwcxMg+KkiUu2TSRzo3+8lDOxxDRbVPsp7I0Ehi9nQ3WFDQdrsIy2SVuLSR66Opsn31 /rZnsscI8NqWkeXCqBshT+d3n2QarZNDoDIq6qq1prfPnti5CwAFjV0tWYSX2AAh/p+Z 777hH+94L7qEQWU/3Qyul8xS2x1IcDEP0MFuD+o9J9S+7Ns/jAssMdNNuvvh8PhtCx1P n9xQ== X-Gm-Message-State: AOAM533v5Pxo8UkY05m0diYBvuQMWAtYfQoUsJQ1uUkgirP8e7phWpsz ggG9A7Syd9PE/DuSs26au1n0QPuff2h6xH6xpQ== X-Google-Smtp-Source: ABdhPJwY0lzUWT9L/TMGH6EZt5NcZ/C6wl43YButvA43Almybc2Om4XHqPpO7qc0fbJgM8W6mTJzZJLMBF1aThUl5w== X-Received: from lg.mtv.corp.google.com ([2620:15c:211:202:f693:9fff:fef4:29dd]) (user=lokeshgidra job=sendgmr) by 2002:a17:902:9a4a:b029:dc:435c:70ad with SMTP id x10-20020a1709029a4ab02900dc435c70admr8978943plv.77.1610144551518; Fri, 08 Jan 2021 14:22:31 -0800 (PST) Date: Fri, 8 Jan 2021 14:22:22 -0800 In-Reply-To: <20210108222223.952458-1-lokeshgidra@google.com> Message-Id: <20210108222223.952458-4-lokeshgidra@google.com> Mime-Version: 1.0 References: <20210108222223.952458-1-lokeshgidra@google.com> X-Mailer: git-send-email 2.30.0.284.gd98b1dd5eaa7-goog Subject: [PATCH v15 3/4] selinux: teach SELinux about anonymous inodes From: Lokesh Gidra To: Andrea Arcangeli , Alexander Viro , James Morris , Stephen Smalley , Casey Schaufler , Eric Biggers , Paul Moore Cc: "Serge E. Hallyn" , Eric Paris , Lokesh Gidra , Daniel Colascione , Kees Cook , "Eric W. Biederman" , KP Singh , David Howells , Anders Roxell , Sami Tolvanen , Matthew Garrett , Randy Dunlap , "Joel Fernandes (Google)" , YueHaibing , Christian Brauner , Alexei Starovoitov , 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, jeffv@google.com, kernel-team@android.com, linux-mm@kvack.org, Andrew Morton , hch@infradead.org X-Bogosity: Ham, tests=bogofilter, spamicity=0.000000, version=1.2.4 Sender: owner-linux-mm@kvack.org Precedence: bulk X-Loop: owner-majordomo@kvack.org List-ID: 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 | 57 +++++++++++++++++++++++++++++ security/selinux/include/classmap.h | 2 + 2 files changed, 59 insertions(+) diff --git a/security/selinux/hooks.c b/security/selinux/hooks.c index 644b17ec9e63..a5e12b2fabde 100644 --- a/security/selinux/hooks.c +++ b/security/selinux/hooks.c @@ -2934,6 +2934,62 @@ 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) { + pr_err("SELinux: context_inode is not initialized"); + return -EACCES; + } + + 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); @@ -7000,6 +7056,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 } };