From patchwork Thu Jun 23 19:52:51 2016 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Daniel Jurgens X-Patchwork-Id: 9195963 Return-Path: Received: from mail.wl.linuxfoundation.org (pdx-wl-mail.web.codeaurora.org [172.30.200.125]) by pdx-korg-patchwork.web.codeaurora.org (Postfix) with ESMTP id 9F5356075A for ; Thu, 23 Jun 2016 20:05:16 +0000 (UTC) Received: from mail.wl.linuxfoundation.org (localhost [127.0.0.1]) by mail.wl.linuxfoundation.org (Postfix) with ESMTP id 8E3962846B for ; Thu, 23 Jun 2016 20:05:16 +0000 (UTC) Received: by mail.wl.linuxfoundation.org (Postfix, from userid 486) id 8094828473; Thu, 23 Jun 2016 20:05:16 +0000 (UTC) X-Spam-Checker-Version: SpamAssassin 3.3.1 (2010-03-16) on pdx-wl-mail.web.codeaurora.org X-Spam-Level: X-Spam-Status: No, score=-1.9 required=2.0 tests=BAYES_00, UNPARSEABLE_RELAY autolearn=unavailable version=3.3.1 Received: from emsm-gh1-uea10.nsa.gov (emsm-gh1-uea10.nsa.gov [8.44.101.8]) by mail.wl.linuxfoundation.org (Postfix) with ESMTP id A936E2846B for ; Thu, 23 Jun 2016 20:05:15 +0000 (UTC) X-IronPort-AV: E=Sophos;i="5.26,518,1459814400"; d="scan'208";a="14966453" IronPort-PHdr: =?us-ascii?q?9a23=3AjicoDhCII+iGX88chSTrUyQJP3N1i/DPJgcQr6Af?= =?us-ascii?q?oPdwSP/8r8bcNUDSrc9gkEXOFd2CrakV06yO7Ou4BCQp2tWoiDg6aptCVhsI24?= =?us-ascii?q?09vjcLJ4q7M3D9N+PgdCcgHc5PBxdP9nC/NlVJSo6lPwWB6kO74TNaIBjjLw09?= =?us-ascii?q?fr2zQd+KyZzonLjvs7ToICxwzAKnZr1zKBjk5S7wjeIxxbVYF6Aq1xHSqWFJce?= =?us-ascii?q?kFjUlhJFaUggqurpzopM0rzj5U884F24YAFPyiPvdwcbsNBzQ8MnsqzNbsqAKG?= =?us-ascii?q?VgyLoHwGXSFeihNVByDd5Qz+G5L2tTH38OF63W3SJsHtZawlUjSlqaFwQVnnjz?= =?us-ascii?q?lDfzMh+XvWkeRohbhauwqlrhdyhYnOb8XdOP5zcq3aZ98yX2dNXs9NESdGB8f0?= =?us-ascii?q?dIYGCawBMOhEvqH8oUcDqV21AgzoTOPmznlSgWT727Ym1O8iOQ7D1Q0kWdkJtS?= =?us-ascii?q?f6ttLwYYsbV6iX0bPHwDPYJ6da0DH85YzKWhUspfWFW7VgN8HWzB98REv+klyM?= =?us-ascii?q?pNm9bHuu3eMXvj3etrJt?= X-IPAS-Result: =?us-ascii?q?A2HOBADUP2xX/wHyM5BdHAEBgyCBU7wsH4cyTAEBAQEBAQI?= =?us-ascii?q?CYieCMoIbAgQBAiQTFCALAwMJAQEXCCEICAMBLRURBwcLBRgEiA8ExysBCwEkj?= =?us-ascii?q?wYRAYV3BY15cIoWjjOJXIVGj35UgggcgU9riEuBNQEBAQ?= Received: from unknown (HELO tarius.tycho.ncsc.mil) ([144.51.242.1]) by emsm-gh1-uea10.nsa.gov with ESMTP; 23 Jun 2016 20:05:12 +0000 Received: from prometheus.infosec.tycho.ncsc.mil (prometheus [192.168.25.40]) by tarius.tycho.ncsc.mil (8.14.4/8.14.4) with ESMTP id u5NK5B3w005063; Thu, 23 Jun 2016 16:05:11 -0400 Received: from tarius.tycho.ncsc.mil (tarius.infosec.tycho.ncsc.mil [144.51.242.1]) by prometheus.infosec.tycho.ncsc.mil (8.15.2/8.15.2) with ESMTP id u5NJveCh212547 for ; Thu, 23 Jun 2016 15:57:40 -0400 Received: from goalie.tycho.ncsc.mil (goalie [144.51.242.250]) by tarius.tycho.ncsc.mil (8.14.4/8.14.4) with ESMTP id u5NJvdsq003324 for ; Thu, 23 Jun 2016 15:57:39 -0400 X-IronPort-Anti-Spam-Filtered: true X-IronPort-Anti-Spam-Result: A1AnBQDyPWxX/4GlL8FdHQGDIIFTuCuECYYYAoEvTAEBAQEBAWYnhE0CBCdSEBgIMVcHEog0xysBMJUPBY15cIoWjjOJXIVGj35UgggcgU85MooAAQEB X-IPAS-Result: A1AnBQDyPWxX/4GlL8FdHQGDIIFTuCuECYYYAoEvTAEBAQEBAWYnhE0CBCdSEBgIMVcHEog0xysBMJUPBY15cIoWjjOJXIVGj35UgggcgU85MooAAQEB X-IronPort-AV: E=Sophos;i="5.26,518,1459828800"; d="scan'208";a="5538178" Received: from emsm-gh1-uea11.corp.nsa.gov (HELO emsm-gh1-uea11.nsa.gov) ([10.208.41.37]) by goalie.tycho.ncsc.mil with ESMTP; 23 Jun 2016 15:57:08 -0400 IronPort-PHdr: =?us-ascii?q?9a23=3Awe0KjB9N9to8SP9uRHKM819IXTAuvvDOBiVQ1KB8?= =?us-ascii?q?2+0cTK2v8tzYMVDF4r011RmSDN2ds6wP0rKJ+4nbGkU4qa6bt34DdJEeHzQksu?= =?us-ascii?q?4x2zIaPcieFEfgJ+TrZSFpVO5LVVti4m3peRMNQJW2WVTerzWI4CIIHV2nbEwu?= =?us-ascii?q?d7yzQtCZ1pzon8mJuLTrKz1SgzS8Zb4gZD6Xli728vcsvI15N6wqwQHIqHYbM8?= =?us-ascii?q?5fxGdvOE7B102kvpT41NdZ/i9Ro/Ms8dJbGeW/JvxgDO8QMDNzOGAv5db0nQLE?= =?us-ascii?q?UBDK/XwYFGIMnV4AGwHY6DniV4r19y79sfBwniKdOIm+VrwvcSiz5KdsDhnzgW?= =?us-ascii?q?EIMCB9uGzPg9Zxl4pDqQigvAR7yoXZJoaPO742cqfQcd0dX2ZpRMtdVyVdRIi7?= =?us-ascii?q?as9HFOcIO6BYoofhtnMPqwezAU+nA+argj9Bgjns2rY31f89FgrF9AMlFtMK9n?= =?us-ascii?q?/TqYbbLqAXBMy8yuHq0C/MYvVNkWPx4Y7JfxQjifSBWbZ5d83Bj0IoElWW3R2r?= =?us-ascii?q?tYX5MmbNhaw2uG+B4r8lDLr3hg=3D=3D?= X-IronPort-Anti-Spam-Filtered: true X-IronPort-Anti-Spam-Result: =?us-ascii?q?A0EOBQCo7GpX/4GlL8FeHQGDIIFTuCaEC?= =?us-ascii?q?YYXAoEwTAEBAQEBAQICYieCMoIbAgQnUhAYCDFXBxKINMURATCVDwWNeHCKFY4?= =?us-ascii?q?viVuFRo98VIIIHIFPOTKKcQEBAQ?= X-IPAS-Result: =?us-ascii?q?A0EOBQCo7GpX/4GlL8FeHQGDIIFTuCaECYYXAoEwTAEBAQE?= =?us-ascii?q?BAQICYieCMoIbAgQnUhAYCDFXBxKINMURATCVDwWNeHCKFY4viVuFRo98VIIIH?= =?us-ascii?q?IFPOTKKcQEBAQ?= X-IronPort-AV: E=Sophos;i="5.26,518,1459814400"; d="scan'208";a="17160096" Received: from unknown (HELO mellanox.co.il) ([193.47.165.129]) by emsm-gh1-uea11.nsa.gov with ESMTP; 23 Jun 2016 19:56:26 +0000 Received: from Internal Mail-Server by MTLPINE1 (envelope-from danielj@mellanox.com) with ESMTPS (AES256-SHA encrypted); 23 Jun 2016 22:53:30 +0300 Received: from x-vnc01.mtx.labs.mlnx (x-vnc01.mtx.labs.mlnx [10.12.150.16]) by labmailer.mlnx (8.13.8/8.13.8) with ESMTP id u5NJr2DE029223; Thu, 23 Jun 2016 22:53:28 +0300 From: Dan Jurgens To: chrisw@sous-sol.org, paul@paul-moore.com, sds@tycho.nsa.gov, eparis@parisplace.org, dledford@redhat.com, sean.hefty@intel.com, hal.rosenstock@gmail.com Subject: [PATCH 05/12] selinux: Implement Infiniband PKey "Access" access vector Date: Thu, 23 Jun 2016 22:52:51 +0300 Message-Id: <1466711578-64398-6-git-send-email-danielj@mellanox.com> X-Mailer: git-send-email 1.7.1 In-Reply-To: <1466711578-64398-1-git-send-email-danielj@mellanox.com> References: <1466711578-64398-1-git-send-email-danielj@mellanox.com> X-Mailman-Approved-At: Thu, 23 Jun 2016 16:01:54 -0400 X-BeenThere: selinux@tycho.nsa.gov X-Mailman-Version: 2.1.20 Precedence: list List-Id: "Security-Enhanced Linux \(SELinux\) mailing list" List-Post: List-Help: Cc: linux-rdma@vger.kernel.org, linux-security-module@vger.kernel.org, selinux@tycho.nsa.gov MIME-Version: 1.0 Errors-To: selinux-bounces@tycho.nsa.gov Sender: "Selinux" X-Virus-Scanned: ClamAV using ClamSMTP From: Daniel Jurgens Add a type and access vector for PKeys. Implement the qp_pkey_access and mad_agent_pkey_access hooks to check that the caller has permission to access the PKey on the given subnet prefix. Add an interface to get the PKey SID. Walk the PKey ocontexts to find an entry for the given subnet prefix and pkey. Signed-off-by: Daniel Jurgens Reviewed-by: Eli Cohen --- include/linux/lsm_audit.h | 7 ++++ security/selinux/hooks.c | 41 ++++++++++++++++++++++++ security/selinux/include/classmap.h | 2 ++ security/selinux/include/initial_sid_to_string.h | 1 + security/selinux/include/security.h | 2 ++ security/selinux/ss/services.c | 41 ++++++++++++++++++++++++ 6 files changed, 94 insertions(+) diff --git a/include/linux/lsm_audit.h b/include/linux/lsm_audit.h index ffb9c9d..8ff7eae 100644 --- a/include/linux/lsm_audit.h +++ b/include/linux/lsm_audit.h @@ -45,6 +45,11 @@ struct lsm_ioctlop_audit { u16 cmd; }; +struct lsm_pkey_audit { + u64 subnet_prefix; + u16 pkey; +}; + /* Auxiliary data to use in generating the audit record. */ struct common_audit_data { char type; @@ -59,6 +64,7 @@ struct common_audit_data { #define LSM_AUDIT_DATA_INODE 9 #define LSM_AUDIT_DATA_DENTRY 10 #define LSM_AUDIT_DATA_IOCTL_OP 11 +#define LSM_AUDIT_DATA_PKEY 12 union { struct path path; struct dentry *dentry; @@ -75,6 +81,7 @@ struct common_audit_data { #endif char *kmod_name; struct lsm_ioctlop_audit *op; + struct lsm_pkey_audit *pkey; } u; /* this union contains LSM specific data */ union { diff --git a/security/selinux/hooks.c b/security/selinux/hooks.c index 4f13ea4..5a40b10 100644 --- a/security/selinux/hooks.c +++ b/security/selinux/hooks.c @@ -6018,6 +6018,44 @@ static void selinux_unregister_ib_flush_callback(void) mutex_unlock(&ib_flush_mutex); } +static int selinux_pkey_access(u64 subnet_prefix, u16 pkey_val, void *security) +{ + struct common_audit_data ad; + int err; + u32 sid = 0; + struct ib_security_struct *sec = security; + struct lsm_pkey_audit pkey; + + err = security_pkey_sid(subnet_prefix, pkey_val, &sid); + + if (err) + goto out; + + ad.type = LSM_AUDIT_DATA_PKEY; + pkey.subnet_prefix = subnet_prefix; + pkey.pkey = pkey_val; + ad.u.pkey = &pkey; + err = avc_has_perm(sec->sid, sid, + SECCLASS_INFINIBAND_PKEY, + INFINIBAND_PKEY__ACCESS, &ad); +out: + return err; +} + +static int selinux_ib_qp_pkey_access(u64 subnet_prefix, u16 pkey_val, + struct ib_qp_security *qp_sec) +{ + return selinux_pkey_access(subnet_prefix, pkey_val, + qp_sec->q_security); +} + +static int selinux_ib_mad_agent_pkey_access(u64 subnet_prefix, u16 pkey_val, + struct ib_mad_agent *mad_agent) +{ + return selinux_pkey_access(subnet_prefix, pkey_val, + mad_agent->m_security); +} + static int selinux_ib_qp_alloc_security(struct ib_qp_security *qp_sec) { struct ib_security_struct *sec; @@ -6248,6 +6286,9 @@ static struct security_hook_list selinux_hooks[] = { selinux_register_ib_flush_callback), LSM_HOOK_INIT(unregister_ib_flush_callback, selinux_unregister_ib_flush_callback), + LSM_HOOK_INIT(ib_qp_pkey_access, selinux_ib_qp_pkey_access), + LSM_HOOK_INIT(ib_mad_agent_pkey_access, + selinux_ib_mad_agent_pkey_access), LSM_HOOK_INIT(ib_qp_alloc_security, selinux_ib_qp_alloc_security), LSM_HOOK_INIT(ib_qp_free_security, diff --git a/security/selinux/include/classmap.h b/security/selinux/include/classmap.h index 1f1f4b2..d42dd4d 100644 --- a/security/selinux/include/classmap.h +++ b/security/selinux/include/classmap.h @@ -165,5 +165,7 @@ struct security_class_mapping secclass_map[] = { { COMMON_CAP_PERMS, NULL } }, { "cap2_userns", { COMMON_CAP2_PERMS, NULL } }, + { "infiniband_pkey", + { "access", NULL } }, { NULL } }; diff --git a/security/selinux/include/initial_sid_to_string.h b/security/selinux/include/initial_sid_to_string.h index a59b64e..8f2eefc 100644 --- a/security/selinux/include/initial_sid_to_string.h +++ b/security/selinux/include/initial_sid_to_string.h @@ -29,5 +29,6 @@ static const char *initial_sid_to_string[] = "policy", "scmp_packet", "devnull", + "pkey", }; diff --git a/security/selinux/include/security.h b/security/selinux/include/security.h index a7e6ed2..8f1a66e 100644 --- a/security/selinux/include/security.h +++ b/security/selinux/include/security.h @@ -180,6 +180,8 @@ int security_get_user_sids(u32 callsid, char *username, int security_port_sid(u8 protocol, u16 port, u32 *out_sid); +int security_pkey_sid(u64 subnet_prefix, u16 pkey_num, u32 *out_sid); + int security_netif_sid(char *name, u32 *if_sid); int security_node_sid(u16 domain, void *addr, u32 addrlen, diff --git a/security/selinux/ss/services.c b/security/selinux/ss/services.c index 89df646..49701a5 100644 --- a/security/selinux/ss/services.c +++ b/security/selinux/ss/services.c @@ -2229,6 +2229,47 @@ out: } /** + * security_pkey_sid - Obtain the SID for a pkey. + * @subnet_prefix: Subnet Prefix + * @pkey_num: pkey number + * @out_sid: security identifier + */ +int security_pkey_sid(u64 subnet_prefix, u16 pkey_num, u32 *out_sid) +{ + struct ocontext *c; + int rc = 0; + + read_lock(&policy_rwlock); + + c = policydb.ocontexts[OCON_PKEY]; + while (c) { + if (c->u.pkey.low_pkey <= pkey_num && + c->u.pkey.high_pkey >= pkey_num && + c->u.pkey.subnet_prefix == subnet_prefix) + break; + + c = c->next; + } + + if (c) { + if (!c->sid[0]) { + rc = sidtab_context_to_sid(&sidtab, + &c->context[0], + &c->sid[0]); + if (rc) + goto out; + } + *out_sid = c->sid[0]; + } else { + *out_sid = SECINITSID_PKEY; + } + +out: + read_unlock(&policy_rwlock); + return rc; +} + +/** * security_netif_sid - Obtain the SID for a network interface. * @name: interface name * @if_sid: interface SID