Received: by 2002:a25:1506:0:0:0:0:0 with SMTP id 6csp1262526ybv; Thu, 13 Feb 2020 19:27:34 -0800 (PST) X-Google-Smtp-Source: APXvYqwbW0Oe/qjAIR7YhSN8Thiw4NF9Fp+mPr4W+RQc8Xnif1ZuX+DLwrvJZr6M6mjxK7dTbbet X-Received: by 2002:a9d:7590:: with SMTP id s16mr567434otk.89.1581650854288; Thu, 13 Feb 2020 19:27:34 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1581650854; cv=none; d=google.com; s=arc-20160816; b=asIDv4n/lO7j5KOVxCB2+wJTVxcOjsA56RoFnT2+n88vmFhYlFNQKkjc8B5+XQsLoP UF84m37fbjHug3wYe5SpPpuXKdGKoXA8ZsdO/TExNlcAveNN7fOqgUiH+CtaON+kW8Am KKz5NttQ0XkAT/9N2jvaFskGcvLKtHwJVwO0tbwmJCiB7JpZRwff/09lw7AA8JvCoub1 cFvxxrTq0REE0MSOJ1cSH1j6s0U51cs0VsWZ0OtmjP+atjlwWf3ndjYDGtOLQjRshe37 Ne7Ko3Hfm15s1OpopHez4xvLvL9eNR53F5O4wKU68ixmkCiYPJMa3VPMxtTFJ1/NCndD Q2Yw== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:sender:cc:to:from:subject:references :mime-version:message-id:in-reply-to:date:dkim-signature; bh=fKf7Qy7FXarC79v0tJzZ3+R2Ba3CQFsER7P1EGsqsHY=; b=Tqn4rtwkxLkBZBZNPxJzooqxc/plQ5+Gd++sMGR8ivv0PG+HJbfwSX8Ym/OaFNQhuc 4bnIY6parLebwEJieP4WiQel/tnXo478cyVu27Dwd99+mGgP3Cu6D+egwE2NlGt4BZD7 h7kM9nV27YLKcPgyB5JD2D81QUymdYzxy2skmlcDeZtanDRZGaNh+aZTBQt5/zLRuz+Z +P5bJQpw//68JuGVjn2PuijG7m1lBKvzG2wNQ/CftOgUM3WEl2mi97Qiqa/zaM74V1tS fdhvfUTWZMVe6PvByX5g0gfG6n7BiJXSrgIUjtW/weYZZDHOP6atmSITvODMUAYdOBva QVug== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@google.com header.s=20161025 header.b=m8Bsd5O0; spf=pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 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. [209.132.180.67]) by mx.google.com with ESMTP id w4si2200228otq.144.2020.02.13.19.27.22; Thu, 13 Feb 2020 19:27:34 -0800 (PST) Received-SPF: pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) client-ip=209.132.180.67; Authentication-Results: mx.google.com; dkim=pass header.i=@google.com header.s=20161025 header.b=m8Bsd5O0; spf=pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 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 S1728576AbgBND1G (ORCPT + 99 others); Thu, 13 Feb 2020 22:27:06 -0500 Received: from mail-pl1-f202.google.com ([209.85.214.202]:52288 "EHLO mail-pl1-f202.google.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1728638AbgBND1B (ORCPT ); Thu, 13 Feb 2020 22:27:01 -0500 Received: by mail-pl1-f202.google.com with SMTP id c19so4377045plz.19 for ; Thu, 13 Feb 2020 19:27:01 -0800 (PST) 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=fKf7Qy7FXarC79v0tJzZ3+R2Ba3CQFsER7P1EGsqsHY=; b=m8Bsd5O0fYTKXemGllMkb7FkRhDEMVPj/GtwwVndphe2KtvZwDgPMvOndFJ1Me9mR6 qchUsPUoP5EMZyWtPHuhw/3WRcCKJ9AJpFVvSqDmZYbjLUufy2ZPyY2E1CWK6dvjq+TW 5/FD9kOtqSSGOtpUCyyqsW5UppSVE95es/30cP5meRBUMbYnjjkHCprogKIm8kn7EnNc DUPF38vxu4j3W4ZB28OXlCzWb7otenUgLwLYUb7x9KbAP937VsvwyYvAUPc+BmGY4m0W 6c3h1C3PFYeapH9/kxLyE5PkmDq3X8R+UfxQj585upTsNCWPx7ZVgF57E1OsvjcEU5Sn xSKA== 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=fKf7Qy7FXarC79v0tJzZ3+R2Ba3CQFsER7P1EGsqsHY=; b=AzdtX6PRl6tidNewzPoWW/dZcfK0cSvJJF8qqZLAdVDU8NHmhC6SkDzdJy0AeHrFLV mZJe/c3gNTyU4J0zA9LnrUiNEsVtuHz9PKMfLYzxtCLangq0pD/IkoVGWokBpACgLfUe q/k6GkcBJXVrjNjt25SYxMdPZMdbb2knU0Bc5xTyao5b7lFSME55SF4Gpco2fL7sF2AU l0ESrzAKZbekKwPyh7C0XuAzdPJ+QViNUaOYm3Yu4u6yfNfFqjRNRMGY48Sjx3HHWjRy kXy1c+u4fNannP5svquQAsygN+S/8ALKqZdFZ8ogh8czAx7Xj48blMl8fW4PiQjEceah FirA== X-Gm-Message-State: APjAAAVeOBzupjKk32D5QFrZ/khfNi3AWthybtlM64VME6znZuOO55vf y3SpTIBcWihZQJZuchMU1WKTLrVBr6s= X-Received: by 2002:a63:f0a:: with SMTP id e10mr1105943pgl.402.1581650821043; Thu, 13 Feb 2020 19:27:01 -0800 (PST) Date: Thu, 13 Feb 2020 19:26:34 -0800 In-Reply-To: <20200214032635.75434-1-dancol@google.com> Message-Id: <20200214032635.75434-3-dancol@google.com> Mime-Version: 1.0 References: <20200211225547.235083-1-dancol@google.com> <20200214032635.75434-1-dancol@google.com> X-Mailer: git-send-email 2.25.0.265.gbab2e86ba0-goog Subject: [PATCH 2/3] Teach SELinux about anonymous inodes From: Daniel Colascione To: timmurray@google.com, selinux@vger.kernel.org, linux-security-module@vger.kernel.org, linux-fsdevel@vger.kernel.org, linux-kernel@vger.kernel.org, kvm@vger.kernel.org, viro@zeniv.linux.org.uk, paul@paul-moore.com, nnk@google.com, sds@tycho.nsa.gov, lokeshgidra@google.com Cc: Daniel Colascione 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 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 : file uffd_t "[userfaultfd]"; allow sysadm_t uffd_t:file { 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 --- security/selinux/hooks.c | 57 ++++++++++++++++++++++++++++++++++++++++ 1 file changed, 57 insertions(+) diff --git a/security/selinux/hooks.c b/security/selinux/hooks.c index 1659b59fb5d7..6de0892620b3 100644 --- a/security/selinux/hooks.c +++ b/security/selinux/hooks.c @@ -2915,6 +2915,62 @@ 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 file_operations *fops, + 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(IS_PRIVATE(inode))) + return 0; + + 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); + if (IS_ERR(context_isec)) + return PTR_ERR(context_isec); + isec->sid = context_isec->sid; + } else { + rc = security_transition_sid( + &selinux_state, tsec->sid, tsec->sid, + SECCLASS_FILE, 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); @@ -6923,6 +6979,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), -- 2.25.0.265.gbab2e86ba0-goog