Received: by 2002:a05:6a10:6744:0:0:0:0 with SMTP id w4csp3476365pxu; Sun, 11 Oct 2020 11:03:46 -0700 (PDT) X-Google-Smtp-Source: ABdhPJyayPHTF5IY+mFKZiGR3GsflDbFa+/NDMvsGBbo0EdN3+vn3nMcElzyt/EIvPUOj7/Y+0pB X-Received: by 2002:a17:907:2677:: with SMTP id ci23mr23891394ejc.270.1602439426188; Sun, 11 Oct 2020 11:03:46 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1602439426; cv=none; d=google.com; s=arc-20160816; b=LH7Co31and5sxhY5+1eSG3U/zX2ytd/CtGNEobD3ZTy4A/9oqBanY659YZLhcXM2/R Cvz3o6LebViD17980VCl0Ei0lODDTHA60bmBS4GDVTbdLclAq4cGhEFOtC2ggblQtdA2 ZH4a3CP9o0FtfxkcMKDjtOHzTqOOHRYgMdOREsJKszJN/tv/X022k6Hj7uyVn5SrKO59 X19Ok156i+nYaHjh/paDrfiXK5lQyeSA2NHF48fEkzz9ljmseTc9YVexQfBNx6fX2jX9 0NFV7aGx159djkya3iVKIxwh7NntYkp4oebQnTaF+KIYUZexvXpnI9ceqYL8sXdKKrx6 akeA== 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=DS8C9e64g1Tr1aD0/k+FxGXbkI0cBOsw5SM2h09qao8=; b=psAZpPT+xh9nBd78Fl0StSTzaHEUCVkC80GowYhVxmQqxpdnPBInb3C1SfV0ik5De5 jJno5XPwnZ5hGfNlDaa68hUeUHyfDbWwIyyF5mFyglO9JmyJ4YhAs33SdleuhqLPouup 3tHH/fU+l+3ZyR3yOEWUAaowMD01l31vOa2kENhMhc56jO3GW0acE3ZzXDCJqzy5uiVM uU4AC/vSXp+1mXJ1mBkBG+0RQgdu/VuU6x77MS48u/pr8htj3e5ujK5YwhELOoAuH4je fzhvXk3oz1HI14XUfaq3+gku1+NJaS+aUYylY5INRK46otGkvU7ElMsq4u32sSQNWcry omtQ== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@google.com header.s=20161025 header.b=i2nS5xZN; 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 g16si10572880ejw.328.2020.10.11.11.03.23; Sun, 11 Oct 2020 11:03:46 -0700 (PDT) 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=i2nS5xZN; 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 S1729289AbgJKI3w (ORCPT + 99 others); Sun, 11 Oct 2020 04:29:52 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:44830 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1729266AbgJKI3r (ORCPT ); Sun, 11 Oct 2020 04:29:47 -0400 Received: from mail-qk1-x74a.google.com (mail-qk1-x74a.google.com [IPv6:2607:f8b0:4864:20::74a]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 3B73BC0613D8 for ; Sun, 11 Oct 2020 01:29:46 -0700 (PDT) Received: by mail-qk1-x74a.google.com with SMTP id w126so10611501qka.5 for ; Sun, 11 Oct 2020 01:29:46 -0700 (PDT) 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=DS8C9e64g1Tr1aD0/k+FxGXbkI0cBOsw5SM2h09qao8=; b=i2nS5xZN3nfP8mH5SMaFmKcZCLgNl6jV+ckgmOf/deX6W0Kz6oMqcAxTPqavTJEEI9 RfzCeiIdGXN5cW+Fa/g/uFhHb1LEiDDDvs0rnzKWJJTjfM+3yJQjtX2WRhABwBs/jSS8 gWUxuaeadJPIqV/QHGrNmqPFGqQMdWgidPtW/9s3rqjAx4R86Wv67I8GmbUN/RsoBIk0 0mb4AlqbThZzEot/APR+ZNF7IgaEO/KBN2MNGsSv5F+soTQkJwIFl2RotUHwAcSi/vSP iC4Jw4c5FsCqiqfW0sWaDT5POrJW/yS437Cq+wDRAxjAsCw7YEn96KEKVFuWyj78eyCU H07A== 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=DS8C9e64g1Tr1aD0/k+FxGXbkI0cBOsw5SM2h09qao8=; b=btinfhwMMHdDsa/hWw7pNH7EUSdIH/YfCBJF5pKmFp8xUZ38QBo4L7QuIJvROWqjjm qnk+DSYogTQHXvtPNdj2/Wdv4VT2Rvm0YfybWstgs1+GOdWyTnWvqy16W71YsAirU2TV w1UUaNwp96Q1LAGTjDKzXCFRgiD8H29OmExNKWgrfjUsA7ulESjm/ej/uhX9Vd46KFjv +4BfnXThHSFiUUpMc19M/zMeE6DP/VVVX4Ig7JvKMCDQCJC4R+bLbispV9ya/3Em/yz3 qVyVWnDaoMM3A93cehQwmS7ETDIyiRJ+eTJTaLsvuvRMwILIS5Dsyy5R3p5HpTcTOCAv YHtA== X-Gm-Message-State: AOAM532dkUapWImGklCYbeEel9iLWQGFc52hPRPWZsmCkxh5ISe7jtaC WmixyMGT/5o/PjPtqMGRvEjwYdOLIlyed4Tzcw== 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:8d08:: with SMTP id r8mr13787480qvb.35.1602404985271; Sun, 11 Oct 2020 01:29:45 -0700 (PDT) Date: Sun, 11 Oct 2020 01:29:35 -0700 In-Reply-To: <20201011082936.4131726-1-lokeshgidra@google.com> Message-Id: <20201011082936.4131726-3-lokeshgidra@google.com> Mime-Version: 1.0 References: <20201011082936.4131726-1-lokeshgidra@google.com> X-Mailer: git-send-email 2.28.0.1011.ga647a8990f-goog Subject: [PATCH v10 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" 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 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 Cc: Al Viro Cc: Andrew Morton --- 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 a340986aa92e..7b22c3112583 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_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); @@ -6987,6 +7039,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.1011.ga647a8990f-goog