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.6 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, URIBL_BLOCKED,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 26ECFC433E1 for ; Fri, 7 Aug 2020 22:50:30 +0000 (UTC) Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by mail.kernel.org (Postfix) with ESMTP id EA5A42224D for ; Fri, 7 Aug 2020 22:50:29 +0000 (UTC) Authentication-Results: mail.kernel.org; dkim=pass (2048-bit key) header.d=google.com header.i=@google.com header.b="SZFP3fsZ" Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1726388AbgHGWu2 (ORCPT ); Fri, 7 Aug 2020 18:50:28 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:37672 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1726335AbgHGWuY (ORCPT ); Fri, 7 Aug 2020 18:50:24 -0400 Received: from mail-qt1-x84a.google.com (mail-qt1-x84a.google.com [IPv6:2607:f8b0:4864:20::84a]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 003F0C061A28 for ; Fri, 7 Aug 2020 15:50:23 -0700 (PDT) Received: by mail-qt1-x84a.google.com with SMTP id h10so2780186qtc.4 for ; Fri, 07 Aug 2020 15:50:23 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20161025; h=date:in-reply-to:message-id:mime-version:references:subject:from:to :cc; bh=5LUPe8BT+jHh/tZlanYghI1SrJggIU5nJyl+FoUY+zw=; b=SZFP3fsZ2jQjHSSnMI+Z8i/5NI90k34O3AvqbW76psCndf30+itZlYlkH6wYGU2eQ+ UZgQhQmVZQ5Dw01H1xwXtPGwvxMhqcENTM3IFDc+4tgm2eln9Oa0+F2DQLUFVoZAmkFL 4yAqkSB8i45M+hEZ4VheqrsQf6zjEib8GMM2TIQpo+a6Oxlmled6yX5X0jDpQ1sqz2bd w+5f0XoFNB/0/IKFQM/69VGaaI/Aji0zwITUHJdzeKzsvSVe6CUM3bFZL+9PtZP5tmcl x+4dJpKNpBZQdmC8lh1JTHPEbzbwyoGFOlz+cpmLUekQsWUSEnlMmcre9w4x+5gq05NA Eluw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:date:in-reply-to:message-id:mime-version :references:subject:from:to:cc; bh=5LUPe8BT+jHh/tZlanYghI1SrJggIU5nJyl+FoUY+zw=; b=BMgxOG9FmXjb1XkGnKxe/FplHtrI6pZdhn4Eq3YPOyShOVJfy2KQpNwiw0ZwTu3aNy PG0rAeqbA0TlVP5ITvSD/ShnxTbsODPkiNSRi68nUqfU6i7E4JICIu+idToNT7B0TT8X BR7UjPIzFxlaHFHKRxpQbyGl0eDi1FmEBi+CnslboZSA8NrKHrlEg8HccDhMhUIpkwnO cwwEEtYmglmJUMUhGBrWAB74LTFAXC6Ue6T2Zull7vwpYb8usEbakooSTUcMQtWuVjA6 oBLz+SEytZ/MGWXL43x1qJwD81z7og+7LtnuRvg47m56lUceuEoSbpKL9Ud+Mdgf5i7x pb2A== X-Gm-Message-State: AOAM5302q9RRUu8fiZombM2i7gGxAr9h3LAoCxqGsHzGm8fVyMA47Z9v bh2rH6xzoFxMg4d6/YCS9AgUttTXqDWHIYMrWQ== X-Google-Smtp-Source: ABdhPJytkJJ1Im9EoucEn8GQ70mfklaLWFe9apw9X6eW8WrUOXqtG852zHNYsyhsZg0X1RHy8lmIHoP6OW3uJ5F2tw== X-Received: by 2002:a0c:fdeb:: with SMTP id m11mr17231144qvu.103.1596840623013; Fri, 07 Aug 2020 15:50:23 -0700 (PDT) Date: Fri, 7 Aug 2020 15:49:40 -0700 In-Reply-To: <20200807224941.3440722-1-lokeshgidra@google.com> Message-Id: <20200807224941.3440722-3-lokeshgidra@google.com> Mime-Version: 1.0 References: <20200807224941.3440722-1-lokeshgidra@google.com> X-Mailer: git-send-email 2.28.0.236.gb10cc79966-goog Subject: [PATCH v6 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" Sender: linux-kernel-owner@vger.kernel.org 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 _secure() anon_inodes functions. 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 Acked-by: Casey Schaufler Acked-by: Stephen Smalley Cc: Al Viro Cc: Andrew Morton Signed-off-by: James Morris --- 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 ca901025802a..5b403ad44aad 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_state.initialized)) + 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); @@ -6993,6 +7045,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.236.gb10cc79966-goog