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.5 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 9FD13C56202 for ; Fri, 6 Nov 2020 15:56:46 +0000 (UTC) Received: from kanga.kvack.org (kanga.kvack.org [205.233.56.17]) by mail.kernel.org (Postfix) with ESMTP id EBA3E2072E for ; Fri, 6 Nov 2020 15:56:45 +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="ffl1DjsL" DMARC-Filter: OpenDMARC Filter v1.3.2 mail.kernel.org EBA3E2072E 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 7B5D96B0036; Fri, 6 Nov 2020 10:56:45 -0500 (EST) Received: by kanga.kvack.org (Postfix, from userid 40) id 78A906B0072; Fri, 6 Nov 2020 10:56:45 -0500 (EST) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 6A0776B0073; Fri, 6 Nov 2020 10:56:45 -0500 (EST) X-Delivered-To: linux-mm@kvack.org Received: from forelay.hostedemail.com (smtprelay0233.hostedemail.com [216.40.44.233]) by kanga.kvack.org (Postfix) with ESMTP id 3BB876B0036 for ; Fri, 6 Nov 2020 10:56:45 -0500 (EST) Received: from smtpin16.hostedemail.com (10.5.19.251.rfc1918.com [10.5.19.251]) by forelay02.hostedemail.com (Postfix) with ESMTP id D730D3489 for ; Fri, 6 Nov 2020 15:56:44 +0000 (UTC) X-FDA: 77454446328.16.wound04_1508d06272d3 Received: from filter.hostedemail.com (10.5.16.251.rfc1918.com [10.5.16.251]) by smtpin16.hostedemail.com (Postfix) with ESMTP id B1196100E6903 for ; Fri, 6 Nov 2020 15:56:44 +0000 (UTC) X-HE-Tag: wound04_1508d06272d3 X-Filterd-Recvd-Size: 8220 Received: from mail-yb1-f201.google.com (mail-yb1-f201.google.com [209.85.219.201]) by imf09.hostedemail.com (Postfix) with ESMTP for ; Fri, 6 Nov 2020 15:56:43 +0000 (UTC) Received: by mail-yb1-f201.google.com with SMTP id k7so2097962ybm.13 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=ermM3jYkMw84aWnueQcff+Zz+MO6s1u/w+bqf12at+w9WG+rXyxMdJKakVOupCCYCt 17a2OsPDgcSaI0KErV3gMm41K86GhaR9Sms3HGANQb6UOg7uFeyi5bfF5XVH2V/g+Tc4 OeD1wBLCHIwM5/YoKt8TFjqaGwxmrQLHzXTZTlRHjvR2Rd+ezoEHCReLiY7rYf/rLsEl LHfglpy/sH/aJFhQq5MPHb8dOWu6XqD6KbrXcK8Tc5kXXShmDaHQSINWFOoJmBxods3v /Lzk8C05vGhfoX/HJnl7O5aZIPxrQEJ9ovlSClMuB0Ju8LjVFR6ClOaIzNyfH7t7RRIo djqw== X-Gm-Message-State: AOAM531y/0jFr5H/9qD7DKDmch01nsFQV/ZgHw1UBh7jeaehvC9zLMyR 7q7WpnjVTBHV0sn2SRvMaK2tpmvfUbpY/Ronaw== X-Google-Smtp-Source: ABdhPJyfDOocb7mOy/FefZ5Q9p5psuC3ih70I08ET9g06WqySi1lWVaasXmP4ckhtBQ08WiLsg2lyvOog2OeNS5ykA== 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" 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 | 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