Received: by 2002:a05:6a10:22f:0:0:0:0 with SMTP id 15csp660620pxk; Wed, 23 Sep 2020 12:35:10 -0700 (PDT) X-Google-Smtp-Source: ABdhPJxt6NVnjk++vIKP5EdQKlCXHGn6Hdm51gxouYgNMIElanhnlpGQTnSahyljmVU9HlMXgB0y X-Received: by 2002:a05:6402:1acf:: with SMTP id ba15mr925407edb.305.1600889710475; Wed, 23 Sep 2020 12:35:10 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1600889710; cv=none; d=google.com; s=arc-20160816; b=LNO4nDyQqck0xTD7SzL5mP2/DCbg7kK5QnUlv+PL7TF4i/XUYerCs7EUoT/AVHeIeM mYpuW16Mohk3XtrYtfQe8ybk52i5cJ7HkpA/AHMbuZW1uWZV8Xfhg/pBhiIBpvGjtd3I ofbw7RCOrfELt7iWrxrmKN+zXVPWChA+jtdsO4oLuGKehtXAEIsKTwgNrRfJuwE6Vgsb FYc/q1n5GBzDmLfkF+4HVxbOD7HB4ZvLDABu/uKMuWDoM39Sk4t4QS76vI0P1f5sPtnS eQKz8semWwx8Vcfw0/APNmZ2pcSb6bxLozhjS36LwHmhTbiUxiKKPfBVAyyo/2Io31l8 /Xpg== 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=fQ+u0i/drpA23IAmEKGg6Wevn+USA3eu5o+lEdrjmF4=; b=bYCRoGqvou9FEV7WZtFLk5+8iGVMCjHsOlCTBayuJpT/I+mFkTFwRmfuuZpLCmwVn7 VFC0MAaoMDUN/iY9k6na07nm7518s/E+DNgN9dCSy80VmcLvMatjgw63wWWlZMOrDqV6 i12AsN6rnrA6IvXtUTI3W5gCjPoQNYKi9XQXA5zXIeWexiMDHW3QVbFTQ9i9q3ucEXJc v4JSbB6H12O9+f5xSSWDKR+x0B6mY9vGfkx5dpIoBHeF87oV7euaukPvc2J+YiZCwj7G dnssyar9EbHXkq6gAWT0jbuOH+RvC5pLlu/htTEf4aHz4Hd8sLUO76z1O+xmWuwEWfN/ EbPg== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@google.com header.s=20161025 header.b=TIdpodC0; 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 r26si709002eja.484.2020.09.23.12.34.47; Wed, 23 Sep 2020 12:35:10 -0700 (PDT) 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=TIdpodC0; 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 S1726709AbgIWTdl (ORCPT + 99 others); Wed, 23 Sep 2020 15:33:41 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:52280 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1726581AbgIWTdh (ORCPT ); Wed, 23 Sep 2020 15:33:37 -0400 Received: from mail-qk1-x749.google.com (mail-qk1-x749.google.com [IPv6:2607:f8b0:4864:20::749]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 982D0C0613D8 for ; Wed, 23 Sep 2020 12:33:35 -0700 (PDT) Received: by mail-qk1-x749.google.com with SMTP id y17so462617qkf.15 for ; Wed, 23 Sep 2020 12:33:35 -0700 (PDT) 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=fQ+u0i/drpA23IAmEKGg6Wevn+USA3eu5o+lEdrjmF4=; b=TIdpodC0D5kyjhJ1OZ7/V6D71iGyyC+HLo84qVjmXf8+4UJyNWUBTtl0ecWQ3zK+dV guYsIXdfN5+aiBC9DDeEjR6X/qv2GIaj6lcLOwkEwduU90tc1JbE4KgYLZXUkEAPeyAG WjOkifXoEnYentlROfgUmP6q/1E+r9O8WbUxuQ8UcUY7rAhdYtTo1VtAqnN5RwuMEqDZ DT//vVhP6XQwsUEBqLjW37sy0wNnFalh/YTZG4MIx4+UIE/yvnOeS6Y8a6rKm1R4Jt0W Algpv/HVcMI2g5z0uon3qJviJNl8ygMJK4nWi7beRhpvadJu1jlPVw55BL2bICpo5/me D7Mw== 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=fQ+u0i/drpA23IAmEKGg6Wevn+USA3eu5o+lEdrjmF4=; b=ENz+wE8adspDY8+IQgpr2g0d9i5TF+5d9TVXtGoErv7GPJKpbHfSC4LsGuwD55no1L PSMTjxGG5fP8mvwVSoWql6s1+vSXEMlFq7MXlB0Mu4iFn2kWnSP6GCuqa25uCgR9rJq6 lYN1u6YI+oQ2sbzTlPPpuvz5mKXr7G08N1GrYdAiOoc69h+dGlscsnYDtGURzfBvElZ9 Vh3BFxcX8wd9HdBdwevrIxclokyYFquR7Pk60UHYLY2pWr+I4WizAM2w1nWWI4ObsOmv DFNGPbjdzD/gyvu/OAA02GoIpdh5uJMSiSkXosHbm1RsH81UZLf2r9k4AiQACKcOV2dR Uo3g== X-Gm-Message-State: AOAM532oL9kCI+7JFqA+rC56B32O7zAjPv4aUqRSyeJ6Zw+A+xc0iQOq Hfr1FvcmANm2UyRkTx8tGmdZ1+dY4ujAH/vUeA== 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:ad4:5387:: with SMTP id i7mr1603818qvv.43.1600889614631; Wed, 23 Sep 2020 12:33:34 -0700 (PDT) Date: Wed, 23 Sep 2020 12:33:23 -0700 In-Reply-To: <20200923193324.3090160-1-lokeshgidra@google.com> Message-Id: <20200923193324.3090160-3-lokeshgidra@google.com> Mime-Version: 1.0 References: <20200923193324.3090160-1-lokeshgidra@google.com> X-Mailer: git-send-email 2.28.0.681.g6f77f65b4e-goog Subject: [PATCH v9 2/3] Teach SELinux about anonymous inodes From: Lokesh Gidra To: 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, Daniel Colascione , Andrew Morton 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 patch 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 Cc: Al Viro Cc: Andrew Morton --- 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 a340986aa92e..7b22c3112583 100644 --- a/security/selinux/hooks.c +++ b/security/selinux/hooks.c @@ -2926,6 +2926,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); @@ -6987,6 +7039,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.28.0.681.g6f77f65b4e-goog