From patchwork Tue Nov 8 21:06:19 2016 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Daniel Jurgens X-Patchwork-Id: 9418169 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 BCCA560459 for ; Tue, 8 Nov 2016 21:14:22 +0000 (UTC) Received: from mail.wl.linuxfoundation.org (localhost [127.0.0.1]) by mail.wl.linuxfoundation.org (Postfix) with ESMTP id ACE7128992 for ; Tue, 8 Nov 2016 21:14:22 +0000 (UTC) Received: by mail.wl.linuxfoundation.org (Postfix, from userid 486) id A131E28A81; Tue, 8 Nov 2016 21:14:22 +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=-4.2 required=2.0 tests=BAYES_00, RCVD_IN_DNSWL_MED, UNPARSEABLE_RELAY autolearn=unavailable version=3.3.1 Received: from emsm-gh1-uea11.nsa.gov (smtp.nsa.gov [8.44.101.9]) (using TLSv1.2 with cipher DHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by mail.wl.linuxfoundation.org (Postfix) with ESMTPS id C06A328992 for ; Tue, 8 Nov 2016 21:14:21 +0000 (UTC) X-IronPort-AV: E=Sophos;i="5.31,611,1473120000"; d="scan'208";a="606828" IronPort-PHdr: =?us-ascii?q?9a23=3Ab5qBChzXvzXk2cnXCy+O+j09IxM/srCxBDY+r6Qd?= =?us-ascii?q?0O0eIJqq85mqBkHD//Il1AaPBtSBraMbwLeG+4nbGkU4qa6bt34DdJEeHzQksu?= =?us-ascii?q?4x2zIaPcieFEfgJ+TrZSFpVO5LVVti4m3peRMNQJW2WVTerzWI4CIIHV2nbEwu?= =?us-ascii?q?d76zS9GZ0pT//tvx0qWbWx9Piju5bOE6BzSNhiKViPMrh5B/IL060BrDrygAUe?= =?us-ascii?q?1XwWR1OQDbxE6ktY+N5porzwB887JkrpYBAu3GePEzTaZZFysOKGco/4jwsh2F?= =?us-ascii?q?ShGAojMEW3gSugJBHgyA6Rb9RJq3uSz/8qJm1DKyIdz9TbdyXy+rqahsVlughT?= =?us-ascii?q?8OLT8i2HnekM1rlKZSqx/noAZwh8bYZIyZOv1kcovHcN8aTHYHVcFUE2RaD4q6?= =?us-ascii?q?KYsCAfYQFedes4T54VAJqF/2BQCoQvno0TJOnWPx1q4S3OEoEAWA1wslTPwUt3?= =?us-ascii?q?GBgNzzfIUPS++4yrKAmTnKafJb3TXV7YXMdhknpuHKVrV1J5mCgXIzHh/I2w3D?= =?us-ascii?q?4bfuOCmYg6FU62U=3D?= X-IPAS-Result: =?us-ascii?q?A2HdBABxPyJY/wHyM5BdHAEBBAEBCgEBFwEBBAEBCgEBgwQ?= =?us-ascii?q?BAQEBAR+BV6Q+llQliBtTAQEBAQEBAQECAQJfKIIzBAMTBYIQAgEDAQI3FCAJA?= =?us-ascii?q?gMDCQEBFwghCAgDAS0VEQcHCwUYBIg7BLRROAKLEQEBCAEBAQEjjy8RAYV8BY5?= =?us-ascii?q?Zi1aQSooNhgWRNlVWJBuDFhyBX3CFDXiBNQEBAQ?= Received: from unknown (HELO tarius.tycho.ncsc.mil) ([144.51.242.1]) by emsm-gh1-uea11.nsa.gov with ESMTP; 08 Nov 2016 21:14:19 +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 uA8LE74q003662; Tue, 8 Nov 2016 16:14:11 -0500 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 uA8LDOTe246550 for ; Tue, 8 Nov 2016 16:13:24 -0500 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 uA8LDO5M003590 for ; Tue, 8 Nov 2016 16:13:24 -0500 X-IronPort-Anti-Spam-Filtered: true X-IronPort-Anti-Spam-Result: A1B5BABRPyJY/4GlL8FdHAEBBAEBCgEBgy8BAQEBAYF2pD6SRIQXhiQCgmYBAgEBAQEBAmIohGICAQN3AhAYCDFXBxKIYLRRi0sBAQgCJZU9BY5Zi1aQSpASkTZVehuDFhyBXzw0hzoBAQE X-IPAS-Result: A1B5BABRPyJY/4GlL8FdHAEBBAEBCgEBgy8BAQEBAYF2pD6SRIQXhiQCgmYBAgEBAQEBAmIohGICAQN3AhAYCDFXBxKIYLRRi0sBAQgCJZU9BY5Zi1aQSpASkTZVehuDFhyBXzw0hzoBAQE X-IronPort-AV: E=Sophos;i="5.31,611,1473134400"; d="scan'208";a="5812124" 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; 08 Nov 2016 16:13:23 -0500 IronPort-PHdr: =?us-ascii?q?9a23=3Am0h3JhyC6t4J9xHXCy+O+j09IxM/srCxBDY+r6Qd?= =?us-ascii?q?0OITIJqq85mqBkHD//Il1AaPBtSBraMbwLeG+4nbGkU4qa6bt34DdJEeHzQksu?= =?us-ascii?q?4x2zIaPcieFEfgJ+TrZSFpVO5LVVti4m3peRMNQJW2WVTerzWI4CIIHV2nbEwu?= =?us-ascii?q?d76zS9GZ0pT//tvx0qWbWx9Piju5bOE6BzSNhiKViPMrh5B/IL060BrDrygAUe?= =?us-ascii?q?1XwWR1OQDbxE6ktY+YtaRu+CVIuv8n69UIEeCjJ/x5HvRkC2EtM3o0/9LDrh7e?= =?us-ascii?q?Vk6U4X9aVX8Z1lJQChXBxAnzQ5O0tyz9rOc70y6feYXtQascRSWp76AtTgThzi?= =?us-ascii?q?gAKXpx/XnekM1rpLpSuhO6vxh2yIOSZ5uacLJyeq7Rc9QASUJbU8pRXjAHCYS5?= =?us-ascii?q?K8MUAuMHe+ZVoZXsj1oIsRa6QwKrAaenyjZMwGXxxqA8yPgmGgLu0wkpHtZIu3?= =?us-ascii?q?PR//vvM6JHfemzhIvV1zrOaegejTv07onFdhIJpPyIWbt2dtqXwk4qQVCWxm6M?= =?us-ascii?q?oJDoamvGnt8GtHKWuq85Dbqi?= X-IronPort-Anti-Spam-Filtered: true X-IronPort-Anti-Spam-Result: =?us-ascii?q?A0F5BABxPyJY/4GlL8FdHAEBBAEBCgEBF?= =?us-ascii?q?wEBBAEBCgEBgwQBAQEBAYF2pD6SRIQXhiQCgmYBAQEBAQEBAQIBAl8ogjMEARU?= =?us-ascii?q?BBIIQAgEDdwIQGAgxVwcSiGC0UYtLAQEIAiWVPQWOWYtWkEqQEpE2VXobgxYcg?= =?us-ascii?q?V88NIVsgU4BAQE?= X-IPAS-Result: =?us-ascii?q?A0F5BABxPyJY/4GlL8FdHAEBBAEBCgEBFwEBBAEBCgEBgwQ?= =?us-ascii?q?BAQEBAYF2pD6SRIQXhiQCgmYBAQEBAQEBAQIBAl8ogjMEARUBBIIQAgEDdwIQG?= =?us-ascii?q?AgxVwcSiGC0UYtLAQEIAiWVPQWOWYtWkEqQEpE2VXobgxYcgV88NIVsgU4BAQE?= X-IronPort-AV: E=Sophos;i="5.31,611,1473120000"; d="scan'208";a="606792" Received: from mail-il-dmz.mellanox.com (HELO mellanox.co.il) ([193.47.165.129]) by emsm-gh1-uea11.nsa.gov with ESMTP; 08 Nov 2016 21:13:22 +0000 Received: from Internal Mail-Server by MTLPINE1 (envelope-from danielj@mellanox.com) with ESMTPS (AES256-SHA encrypted); 8 Nov 2016 23:06:42 +0200 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 uA8L6Se3009889; Tue, 8 Nov 2016 23:06:39 +0200 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 v4 3/9] selinux lsm IB/core: Implement LSM notification system Date: Tue, 8 Nov 2016 23:06:19 +0200 Message-Id: <1478639185-47521-4-git-send-email-danielj@mellanox.com> X-Mailer: git-send-email 1.7.1 In-Reply-To: <1478639185-47521-1-git-send-email-danielj@mellanox.com> References: <1478639185-47521-1-git-send-email-danielj@mellanox.com> X-Mailman-Approved-At: Tue, 08 Nov 2016 16:14:07 -0500 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: liranl@mellanox.com, leonro@mellanox.com, 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 generic notificaiton mechanism in the LSM. Interested consumers can register a callback with the LSM and security modules can produce events. Because access to Infiniband QPs are enforced in the setup phase of a connection security should be enforced again if the policy changes. Register infiniband devices for policy change notification and check all QPs on that device when the notification is received. Add a call to the notification mechanism from SELinux when the AVC cache changes or setenforce is cleared. issue: 736423 Change-Id: I9f5a13026c9219105ee5e0cd99edf8eabdafe946 Signed-off-by: Daniel Jurgens --- v2: - new patch that has the generic notification, replaces selinux and IB/core patches related to the ib_flush callback. Yuval Shaia and Paul Moore v3: - use notifier chains. Paul Moore v4: - Seperate avc callback for LSM notifier. Paul Moore Signed-off-by: Daniel Jurgens --- drivers/infiniband/core/device.c | 53 ++++++++++++++++++++++++++++++++++++++++ include/linux/security.h | 8 ++++++ security/security.c | 20 +++++++++++++++ security/selinux/hooks.c | 11 +++++++++ security/selinux/selinuxfs.c | 2 ++ 5 files changed, 94 insertions(+) diff --git a/drivers/infiniband/core/device.c b/drivers/infiniband/core/device.c index 5b42e83..7b6fd06 100644 --- a/drivers/infiniband/core/device.c +++ b/drivers/infiniband/core/device.c @@ -39,6 +39,8 @@ #include #include #include +#include +#include #include #include #include @@ -82,6 +84,14 @@ struct ib_client_data { static DEFINE_MUTEX(device_mutex); static DECLARE_RWSEM(lists_rwsem); +static int ib_security_change(struct notifier_block *nb, unsigned long event, + void *lsm_data); +static void ib_policy_change_task(struct work_struct *work); +static DECLARE_WORK(ib_policy_change_work, ib_policy_change_task); + +static struct notifier_block ibdev_lsm_nb = { + .notifier_call = ib_security_change, +}; static int ib_device_check_mandatory(struct ib_device *device) { @@ -344,6 +354,40 @@ static int setup_port_pkey_list(struct ib_device *device) return 0; } +static void ib_policy_change_task(struct work_struct *work) +{ + struct ib_device *dev; + + down_read(&lists_rwsem); + list_for_each_entry(dev, &device_list, core_list) { + int i; + + for (i = rdma_start_port(dev); i <= rdma_end_port(dev); i++) { + u64 sp; + int ret = ib_get_cached_subnet_prefix(dev, + i, + &sp); + + WARN_ONCE(ret, + "ib_get_cached_subnet_prefix err: %d, this should never happen here\n", + ret); + ib_security_cache_change(dev, i, sp); + } + } + up_read(&lists_rwsem); +} + +static int ib_security_change(struct notifier_block *nb, unsigned long event, + void *lsm_data) +{ + if (event != LSM_POLICY_CHANGE) + return NOTIFY_DONE; + + schedule_work(&ib_policy_change_work); + + return NOTIFY_OK; +} + /** * ib_register_device - Register an IB device with IB core * @device:Device to register @@ -1075,10 +1119,18 @@ static int __init ib_core_init(void) goto err_sa; } + ret = register_lsm_notifier(&ibdev_lsm_nb); + if (ret) { + pr_warn("Couldn't register LSM notifier. ret %d\n", ret); + goto err_ibnl_clients; + } + ib_cache_setup(); return 0; +err_ibnl_clients: + ib_remove_ibnl_clients(); err_sa: ib_sa_cleanup(); err_mad: @@ -1098,6 +1150,7 @@ static int __init ib_core_init(void) static void __exit ib_core_cleanup(void) { + unregister_lsm_notifier(&ibdev_lsm_nb); ib_cache_cleanup(); ib_remove_ibnl_clients(); ib_sa_cleanup(); diff --git a/include/linux/security.h b/include/linux/security.h index 342ca4c..d8c29cd 100644 --- a/include/linux/security.h +++ b/include/linux/security.h @@ -69,6 +69,14 @@ struct user_namespace; struct timezone; +enum lsm_event { + LSM_POLICY_CHANGE, +}; + +int call_lsm_notifier(enum lsm_event event, void *data); +int register_lsm_notifier(struct notifier_block *nb); +int unregister_lsm_notifier(struct notifier_block *nb); + /* These functions are in security/commoncap.c */ extern int cap_capable(const struct cred *cred, struct user_namespace *ns, int cap, int audit); diff --git a/security/security.c b/security/security.c index 7d3bf2f..40326d4 100644 --- a/security/security.c +++ b/security/security.c @@ -33,6 +33,8 @@ /* Maximum number of letters for an LSM name string */ #define SECURITY_NAME_MAX 10 +static ATOMIC_NOTIFIER_HEAD(lsm_notifier_chain); + /* Boot-time LSM user choice */ static __initdata char chosen_lsm[SECURITY_NAME_MAX + 1] = CONFIG_DEFAULT_SECURITY; @@ -98,6 +100,24 @@ int __init security_module_enable(const char *module) return !strcmp(module, chosen_lsm); } +int call_lsm_notifier(enum lsm_event event, void *data) +{ + return atomic_notifier_call_chain(&lsm_notifier_chain, event, data); +} +EXPORT_SYMBOL(call_lsm_notifier); + +int register_lsm_notifier(struct notifier_block *nb) +{ + return atomic_notifier_chain_register(&lsm_notifier_chain, nb); +} +EXPORT_SYMBOL(register_lsm_notifier); + +int unregister_lsm_notifier(struct notifier_block *nb) +{ + return atomic_notifier_chain_unregister(&lsm_notifier_chain, nb); +} +EXPORT_SYMBOL(unregister_lsm_notifier); + /* * Hook list operation macros. * diff --git a/security/selinux/hooks.c b/security/selinux/hooks.c index 09fd610..7578198 100644 --- a/security/selinux/hooks.c +++ b/security/selinux/hooks.c @@ -170,6 +170,14 @@ static int selinux_netcache_avc_callback(u32 event) return 0; } +static int selinux_lsm_notifier_avc_callback(u32 event) +{ + if (event == AVC_CALLBACK_RESET) { + call_lsm_notifier(LSM_POLICY_CHANGE, NULL); + } + return 0; +} + /* * initialise the security for the init task */ @@ -6325,6 +6333,9 @@ static __init int selinux_init(void) if (avc_add_callback(selinux_netcache_avc_callback, AVC_CALLBACK_RESET)) panic("SELinux: Unable to register AVC netcache callback\n"); + if (avc_add_callback(selinux_lsm_notifier_avc_callback, AVC_CALLBACK_RESET)) + panic("SELinux: Unable to register AVC LSM notifier callback\n"); + if (selinux_enforcing) printk(KERN_DEBUG "SELinux: Starting in enforcing mode\n"); else diff --git a/security/selinux/selinuxfs.c b/security/selinux/selinuxfs.c index 72c145d..d3f9192 100644 --- a/security/selinux/selinuxfs.c +++ b/security/selinux/selinuxfs.c @@ -177,6 +177,8 @@ static ssize_t sel_write_enforce(struct file *file, const char __user *buf, avc_ss_reset(0); selnl_notify_setenforce(selinux_enforcing); selinux_status_update_setenforce(selinux_enforcing); + if (!selinux_enforcing) + call_lsm_notifier(LSM_POLICY_CHANGE, NULL); } length = count; out: