Received: by 2002:a05:6a10:2785:0:0:0:0 with SMTP id ia5csp58845pxb; Thu, 7 Jan 2021 21:36:19 -0800 (PST) X-Google-Smtp-Source: ABdhPJwLNTtJNP0Sf8IxuepC+E+q59Xicd1LS4j1KHuce3rF4QvkhMcKQrqjEfKyCq6KoILPFXSA X-Received: by 2002:a05:6402:3048:: with SMTP id bu8mr4067007edb.49.1610084179402; Thu, 07 Jan 2021 21:36:19 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1610084179; cv=none; d=google.com; s=arc-20160816; b=XZNC4d4lYGyjpWi3WBYfMw/7A/F9AzAG/M3tUenB7QakNvbyep5bzxYO07t3ExuBAb CZXzCK8aaCJbMXuu4cAEUliisNrPmvpsYH3uSqPsn0VXMI8VQeNyBGXNtG/TYRX4msqA TV/Dn0YNv3hld9p1YAIbxD40IQQNHucuGGj1/XwZ269lgU/5GGz444XhFaLWfMeq0Hib UziMTjeu0lSvRJEcUHMN70d+ZRg4bk7zGFtPXg+Mbg1KGj83xAqQwEV4yrvNfLl/wFek HK+NLqr68oMqhv4h1IFXvy2XClDcs+Dh3KzRBpmp632R+I+mIgFXOETQ+Dhp/eDNF/1v hC/Q== 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=yvxLkMrnVxRSxC9KzTyVpuuqR+pCEABEK4TkVEGjmlM=; b=HTxHzK8c4zx4VOIR/Wv30CJpHBpsRbg2D4owsLm0C30XLvhnDngBzNCJDjoMjUlzJH NgeSnkOcrcZV5fNndfqFNfM/Mz6qKNO5vpv6OS0XB6ZGlOg+Hp0w/PRbM65kegDEOrkq 9BQGsUvCJ8IRWcvWc3odVgwFVaMOrj792cbdI77fQcWvGZpSmhG4VfRBikrxLdQqw3Jo 5okawBCUWr6NEmjbbCgOkVM/pY4Rql7TLlUZAy7oCjP1Z8fcvOv3gahP0vH8DHAXA5Fy ugF+FHV7HcnUuB84WImT4/r1+G1zRiMwQoDceWN2AvjCQifFthFndWcBLhDdipAC2sr5 RIRQ== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@google.com header.s=20161025 header.b=n8QszcMr; 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 n13si3237693eds.172.2021.01.07.21.35.55; Thu, 07 Jan 2021 21:36:19 -0800 (PST) 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=n8QszcMr; 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 S1727980AbhAHFeS (ORCPT + 99 others); Fri, 8 Jan 2021 00:34:18 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:56524 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1727926AbhAHFeR (ORCPT ); Fri, 8 Jan 2021 00:34:17 -0500 Received: from mail-qv1-xf49.google.com (mail-qv1-xf49.google.com [IPv6:2607:f8b0:4864:20::f49]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id EFC88C0612A1 for ; Thu, 7 Jan 2021 21:33:07 -0800 (PST) Received: by mail-qv1-xf49.google.com with SMTP id i20so7439522qvk.18 for ; Thu, 07 Jan 2021 21:33:07 -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=yvxLkMrnVxRSxC9KzTyVpuuqR+pCEABEK4TkVEGjmlM=; b=n8QszcMrbIG1zQA/diOQYNUs4mCdtg1DOV6tjUlQ2lnb4Xk+zOC4rmCdSfLa9SsHTD 1PprmyzinG1EOt3nJpebsLKdEdyvjZF1F2BghDq1S2ORAkTIkdEJwNxahywA9jft0xdc qoak145AZ6+a7UTh1S/Blc9JyXlsfKx5CdBSA3KY6OXoln7cuH1RKIP2mHO8XZNcHnx8 0pUcd4G6FGfYHob7nD7B/DTyCajiHZO9AAZ1DQA+d20psDVp27zaZ7Lv9bciaxH6jXfl TaDmcjn4rcZXDa5tf2lupNo7K2quDgoTMdE8NA+fLHyo30TSP/nFPac46ZWyAj1+rmzP vVjA== 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=yvxLkMrnVxRSxC9KzTyVpuuqR+pCEABEK4TkVEGjmlM=; b=Ts+9FurPsGVX2IIUdoThRKt5U3fil3AzoDge/8kPwIB2qDw3daLLmXJn7EQgjwDdj2 aNrsJ90V8kWQPGLUkWQzYQS4fLzXIGxLo1rmw+RKuOK4ivfiByb/Ai25JE8eSuLvaFYk 62wSetuPQQAAlPvco9cCkw+UX3H7/XvtkCaXaDDomwG8b4+ihmvWtFyCMQHB0uZtBwCO bxLF41YPlx65ZhEjQZUuma6MGFE77bpo8d+TAmBnxPypdcTCUu5Ghn2GantDdt+WbF+C 1f3+Gqs5Oo+BXoiJP2ivsDm5U+cHoJaJtt+y1E89KGzAjt1oBsZXpswQDFm1kDdAX2g8 okUQ== X-Gm-Message-State: AOAM530cJdzSLiQoIIJxWielnWbhZPcJYLdCu5xNFarRx0dPS1pEDDA6 D/ST0YLctpJZN9Avlg3FlfF5u/Yi/UqlPckq+Q== 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:a0c:a905:: with SMTP id y5mr1983204qva.55.1610083987068; Thu, 07 Jan 2021 21:33:07 -0800 (PST) Date: Thu, 7 Jan 2021 21:32:58 -0800 In-Reply-To: <20210108053259.726613-1-lokeshgidra@google.com> Message-Id: <20210108053259.726613-4-lokeshgidra@google.com> Mime-Version: 1.0 References: <20210108053259.726613-1-lokeshgidra@google.com> X-Mailer: git-send-email 2.30.0.284.gd98b1dd5eaa7-goog Subject: [PATCH v14 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 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 | 59 +++++++++++++++++++++++++++++ security/selinux/include/classmap.h | 2 + 2 files changed, 61 insertions(+) diff --git a/security/selinux/hooks.c b/security/selinux/hooks.c index 644b17ec9e63..8b4e155b2930 100644 --- a/security/selinux/hooks.c +++ b/security/selinux/hooks.c @@ -2934,6 +2933,63 @@ 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. + */ + isec->initialized = LABEL_INITIALIZED; + isec->sclass = SECCLASS_ANON_INODE; + + if (context_inode) { + struct inode_security_struct *context_isec = + selinux_inode(context_inode); + if (context_isec->initialized != LABEL_INITIALIZED) + return -EACCES; + if (context_isec->sclass != SECCLASS_ANON_INODE) { + pr_err("SELinux: initializing anonymous inode with non-anonymous inode"); + return -EACCES; + } + + isec->sid = context_isec->sid; + } else { + rc = security_transition_sid( + &selinux_state, tsec->sid, tsec->sid, + isec->sclass, name, &isec->sid); + if (rc) + return rc; + } + + /* + * 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); @@ -7000,6 +7057,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.30.0.284.gd98b1dd5eaa7-goog