From mboxrd@z Thu Jan 1 00:00:00 1970 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=-20.4 required=3.0 tests=BAYES_00,DKIMWL_WL_MED, DKIM_SIGNED,DKIM_VALID,DKIM_VALID_AU,HEADER_FROM_DIFFERENT_DOMAINS, INCLUDES_PATCH,MAILING_LIST_MULTI,SIGNED_OFF_BY,SPF_HELO_NONE,SPF_PASS, USER_AGENT_GIT,USER_IN_DEF_DKIM_WL autolearn=unavailable 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 6A785C56201 for ; Thu, 12 Nov 2020 05:34:08 +0000 (UTC) Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by mail.kernel.org (Postfix) with ESMTP id 0705720825 for ; Thu, 12 Nov 2020 05:34:07 +0000 (UTC) Authentication-Results: mail.kernel.org; dkim=pass (2048-bit key) header.d=google.com header.i=@google.com header.b="Mz0/RepG" Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1728650AbgKLFeG (ORCPT ); Thu, 12 Nov 2020 00:34:06 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:55274 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1728032AbgKLByz (ORCPT ); Wed, 11 Nov 2020 20:54:55 -0500 Received: from mail-yb1-xb4a.google.com (mail-yb1-xb4a.google.com [IPv6:2607:f8b0:4864:20::b4a]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 51EC6C0401C1 for ; Wed, 11 Nov 2020 17:54:15 -0800 (PST) Received: by mail-yb1-xb4a.google.com with SMTP id u37so4277467ybi.15 for ; Wed, 11 Nov 2020 17:54:15 -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=EKdfgxKmgqyWOrEoFelO0xo4xUprR4kVgmmY5QtMneA=; b=Mz0/RepGCaX9VPHW6LKYUdkGuWwDxdRh602UW+erQ9khnSph5lnBFq+8u9tT4GDxaS ckxAoj4jIZb/X+96/D7EN4p3mRLqrhrRnE3AxR4nd3qOi6FK1e/Z6XUKmdBv0V52oHoi fnRSEC7tE+8O3CSNKd8dbNMYYBVswtN2GpJKmuRoeXfgAY2LAq6yldLOmIxmMhutphYm j4fhBnd140Pi1Ckyo33E5c9jKyWfJ37HDd6ORnuKewANEJvZfovjkYwOABiS+IlQOpEn QIjD4OMXTdg/iDGYGT+UyppMyuf4rtDyeeBOjhzp9egpqH7d6TWO6igQsuhW95l5hcfa uSuw== 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=EKdfgxKmgqyWOrEoFelO0xo4xUprR4kVgmmY5QtMneA=; b=rIaelFh0y76SV/lesZkFZNF9v85iAd/C7WgzEX7PjMsFxGjB2LM6261W1vEJXGrFOv XPSbSPi4mKXMjnuTLmBQ0hKnGQDuwJ1eqOY+7xNkLqhARkA2eWj3VBf7hKrPiNbMjH65 CcE5mju5u29O3Mg8gV9+ui//YiSyCdbQy/WRx9ofd/aBjFZZ426nKAIJvms5VDoCLZ5H c35fksrI4Sb6QzXq2IUapoyEV77qqesl42PZD+gSL6dSl2PHFvynXJc16hS3av8lrapw sqFs+dz2FpMI03a3wAVr1a4LjP6HTkqTcHN0tRYmdlyduwLZEOnhW2cVfT4e8N2s8KgO +EIg== X-Gm-Message-State: AOAM533ac99YXiOjApQ2kzNegLiKapYtZQ8gcshJ/djX4zg02GUH1Rly guJa6Q59cOTPBpm6WgFlr2xZq9SPxXFDN5AgFQ== X-Google-Smtp-Source: ABdhPJzFSv/Xdn7GGdy2MUG7BhSbdefjZ9hVJ+MOvRURjTp6rSFxYr2XLTCAi7JOZcqE2J6h4zH58KP3k6BneY9sdA== 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:bc42:: with SMTP id d2mr6737406ybk.461.1605146054397; Wed, 11 Nov 2020 17:54:14 -0800 (PST) Date: Wed, 11 Nov 2020 17:53:58 -0800 In-Reply-To: <20201112015359.1103333-1-lokeshgidra@google.com> Message-Id: <20201112015359.1103333-4-lokeshgidra@google.com> Mime-Version: 1.0 References: <20201112015359.1103333-1-lokeshgidra@google.com> X-Mailer: git-send-email 2.29.2.299.gdc1121823c-goog Subject: [PATCH v13 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 , 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, 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 | 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; + 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, + ANON_INODE__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 +7047,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.2.299.gdc1121823c-goog From mboxrd@z Thu Jan 1 00:00:00 1970 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=-12.6 required=3.0 tests=BAYES_00, DKIM_ADSP_CUSTOM_MED,DKIM_INVALID,DKIM_SIGNED,HEADER_FROM_DIFFERENT_DOMAINS, INCLUDES_PATCH,MAILING_LIST_MULTI,SIGNED_OFF_BY,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 8A1E3C56201 for ; Thu, 12 Nov 2020 01:54:17 +0000 (UTC) Received: from kanga.kvack.org (kanga.kvack.org [205.233.56.17]) by mail.kernel.org (Postfix) with ESMTP id 0925F206CA for ; Thu, 12 Nov 2020 01:54:16 +0000 (UTC) Authentication-Results: mail.kernel.org; dkim=fail reason="signature verification failed" (2048-bit key) header.d=google.com header.i=@google.com header.b="Mz0/RepG" DMARC-Filter: OpenDMARC Filter v1.3.2 mail.kernel.org 0925F206CA 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 8E0CA6B0071; Wed, 11 Nov 2020 20:54:16 -0500 (EST) Received: by kanga.kvack.org (Postfix, from userid 40) id 840206B0072; Wed, 11 Nov 2020 20:54:16 -0500 (EST) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 6B90F6B0073; Wed, 11 Nov 2020 20:54:16 -0500 (EST) X-Delivered-To: linux-mm@kvack.org Received: from forelay.hostedemail.com (smtprelay0037.hostedemail.com [216.40.44.37]) by kanga.kvack.org (Postfix) with ESMTP id 339E26B0071 for ; Wed, 11 Nov 2020 20:54:16 -0500 (EST) Received: from smtpin06.hostedemail.com (10.5.19.251.rfc1918.com [10.5.19.251]) by forelay03.hostedemail.com (Postfix) with ESMTP id D610E8249980 for ; Thu, 12 Nov 2020 01:54:15 +0000 (UTC) X-FDA: 77474096070.06.bat16_0f0731327302 Received: from filter.hostedemail.com (10.5.16.251.rfc1918.com [10.5.16.251]) by smtpin06.hostedemail.com (Postfix) with ESMTP id B54A11003CA90 for ; Thu, 12 Nov 2020 01:54:15 +0000 (UTC) X-HE-Tag: bat16_0f0731327302 X-Filterd-Recvd-Size: 8246 Received: from mail-yb1-f201.google.com (mail-yb1-f201.google.com [209.85.219.201]) by imf11.hostedemail.com (Postfix) with ESMTP for ; Thu, 12 Nov 2020 01:54:15 +0000 (UTC) Received: by mail-yb1-f201.google.com with SMTP id b189so4295083ybh.5 for ; Wed, 11 Nov 2020 17:54:15 -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=EKdfgxKmgqyWOrEoFelO0xo4xUprR4kVgmmY5QtMneA=; b=Mz0/RepGCaX9VPHW6LKYUdkGuWwDxdRh602UW+erQ9khnSph5lnBFq+8u9tT4GDxaS ckxAoj4jIZb/X+96/D7EN4p3mRLqrhrRnE3AxR4nd3qOi6FK1e/Z6XUKmdBv0V52oHoi fnRSEC7tE+8O3CSNKd8dbNMYYBVswtN2GpJKmuRoeXfgAY2LAq6yldLOmIxmMhutphYm j4fhBnd140Pi1Ckyo33E5c9jKyWfJ37HDd6ORnuKewANEJvZfovjkYwOABiS+IlQOpEn QIjD4OMXTdg/iDGYGT+UyppMyuf4rtDyeeBOjhzp9egpqH7d6TWO6igQsuhW95l5hcfa uSuw== 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=EKdfgxKmgqyWOrEoFelO0xo4xUprR4kVgmmY5QtMneA=; b=A9ALF4LXgkFatYIEzinDFST7EBQvIjYARQrqyZzUyMh/pn9Ob0i80cynJrE1V6UWdE B+vAoof056OBQ2x9UiXTi5horc5mx7J/KCjdGhrztuScNje2McCA2efySStEtuq7ib9b Z+ehTWmBeCS+kUQYEGg5fGrJ1uFe1ot1zQo/oUpgnP8cbL7mPg3NazaAhtTMFxls2c+P Yrk5CvQqxkO/rD2o2v+hD3UtEyn4p0cHtJBGHslMPtFQ7GO8NhW6OxcCp2lHYRGJsDCK 3lz/15Kb1+cTgRGflhx8ZEBaI2uqcGeKVns8cCpf0rY+hrqxSVx2XctBaNndOiOLopTG CD0Q== X-Gm-Message-State: AOAM532X+7qP296j3flmQ6k3L9h2zCU8k0ahem8mBv6I1ME1UteM6SID wqIyipEtMo4X4y9c89EZ/bfA6DKqzgL77Ppccg== X-Google-Smtp-Source: ABdhPJzFSv/Xdn7GGdy2MUG7BhSbdefjZ9hVJ+MOvRURjTp6rSFxYr2XLTCAi7JOZcqE2J6h4zH58KP3k6BneY9sdA== X-Received: from lg.mtv.corp.google.com ([2620:15c:211:202:f693:9fff:fef4:29dd]) (user=lokeshgidra job=sendgmr) by 2002:a25:bc42:: with SMTP id d2mr6737406ybk.461.1605146054397; Wed, 11 Nov 2020 17:54:14 -0800 (PST) Date: Wed, 11 Nov 2020 17:53:58 -0800 In-Reply-To: <20201112015359.1103333-1-lokeshgidra@google.com> Message-Id: <20201112015359.1103333-4-lokeshgidra@google.com> Mime-Version: 1.0 References: <20201112015359.1103333-1-lokeshgidra@google.com> X-Mailer: git-send-email 2.29.2.299.gdc1121823c-goog Subject: [PATCH v13 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 , 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, 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" 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 | 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; + 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, + ANON_INODE__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 +7047,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.2.299.gdc1121823c-goog