From patchwork Thu Jul 14 22:56:45 2016 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Daniel Jurgens X-Patchwork-Id: 9231879 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 010C06075D for ; Fri, 15 Jul 2016 12:34:03 +0000 (UTC) Received: from mail.wl.linuxfoundation.org (localhost [127.0.0.1]) by mail.wl.linuxfoundation.org (Postfix) with ESMTP id E2B5D27F86 for ; Fri, 15 Jul 2016 12:34:02 +0000 (UTC) Received: by mail.wl.linuxfoundation.org (Postfix, from userid 486) id D5BF828324; Fri, 15 Jul 2016 12:34:02 +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=-2.6 required=2.0 tests=BAYES_00, RCVD_IN_DNSWL_LOW, 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 E469727F86 for ; Fri, 15 Jul 2016 12:34:01 +0000 (UTC) X-IronPort-AV: E=Sophos;i="5.28,368,1464652800"; d="scan'208";a="17673668" IronPort-PHdr: =?us-ascii?q?9a23=3AwcKwah89DOJpAP9uRHKM819IXTAuvvDOBiVQ1KB6?= =?us-ascii?q?0ugcTK2v8tzYMVDF4r011RmSDN2dt64P17WempujcFRI2YyGvnEGfc4EfD4+ou?= =?us-ascii?q?JSoTYdBtWYA1bwNv/gYn9yNs1DUFh44yPzahANS47AblHf6ke/8SQVUk2mc1Ek?= =?us-ascii?q?fqKpRsWO0Iye7KObw9XreQJGhT6wM/tZDS6dikHvjPQQmpZoMa0ryxHE8TNicu?= =?us-ascii?q?VSwn50dxrIx06vrvqq+NZf1wgY+7d4r48TGZj8ZLkyGLxEECx0dCct/sruuAXD?= =?us-ascii?q?QBaUoHoaXGINnxEOHxnO7Ba8WJzquW3/t+463SCbOcj/S/czQSij6KFwDwTljC?= =?us-ascii?q?YBKi5qzWfMl8YliatapA6m9Qdyx4/SfZHQLvdnY67GYfsGVGFBWYBXTCUHDYSi?= =?us-ascii?q?OJASBegMNvoNso/mulEVpgG/DwT/OOS6wT9Sh2Tp9bY9zv9kCgbCmgs6EJZGqH?= =?us-ascii?q?3JqP3tPboWFOWyy7PFiz7EarcewjvmwJTZeRAm5/eXVPR/dtSVgUQ3HhnElX2I?= =?us-ascii?q?oJbkJCuR3+8A9W+B4K4oWeaih2AgsQ1ZuDWjxs4wzILOg8Zd0V3N82N5zYgpNP?= =?us-ascii?q?W5TlJ2ZZivF54U/yGTM81sRdgvRXp0vy054rIDsJ+/OiMNzccJ3RnaPtCOdcCq?= =?us-ascii?q?/wjsWeCKaWN0jXRkdba9rxO/9UypzOrsEMKz1QAZ/WJ+jtDQuyVVhFTo4c+dR6?= =?us-ascii?q?44px+s?= X-IPAS-Result: =?us-ascii?q?A2EqBQBb14hX/wHyM5BcHAEBgyCBUrplH4cxTAEBAQEBAQI?= =?us-ascii?q?CYieCMgQDEIIUAgEDAQIkEwwIAh4JAgMDAQIGAQEXCCEICAMBLRURBwcLBRgEi?= =?us-ascii?q?A8Ev3MBAQEBBgIBJI8JEQGFdwWOCHGKKI5diWWFTpBvggscgU9rhR6BNQEBAQ?= Received: from unknown (HELO tarius.tycho.ncsc.mil) ([144.51.242.1]) by emsm-gh1-uea11.nsa.gov with ESMTP; 15 Jul 2016 12:33:54 +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 u6FCXYrl024363; Fri, 15 Jul 2016 08:33:42 -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 u6EMvdZI072599 for ; Thu, 14 Jul 2016 18:57:39 -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 u6EMvcG3030259 for ; Thu, 14 Jul 2016 18:57:38 -0400 X-IronPort-Anti-Spam-Filtered: true X-IronPort-Anti-Spam-Result: A1CeBACpF4hX/4GlL8FcHQGEcrZhhAqGGQKBewEBAQEBAWYnhF0CAQMnUAIQGAgxVwcSiDTAYgEwlRIFjgdxiieOW48vkBlUggkcgU85ModuAQEB X-IPAS-Result: A1CeBACpF4hX/4GlL8FcHQGEcrZhhAqGGQKBewEBAQEBAWYnhF0CAQMnUAIQGAgxVwcSiDTAYgEwlRIFjgdxiieOW48vkBlUggkcgU85ModuAQEB X-IronPort-AV: E=Sophos;i="5.28,365,1464667200"; d="scan'208";a="5582136" 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; 14 Jul 2016 18:57:38 -0400 IronPort-PHdr: =?us-ascii?q?9a23=3AOfIcrBHhfJ6QtQLe4fhkrZ1GYnF86YWxBRYc798d?= =?us-ascii?q?s5kLTJ74r8mwAkXT6L1XgUPTWs2DsrQf2rKQ7/urAzRIyK3CmUhKSIZLWR4BhJ?= =?us-ascii?q?detC0bK+nBN3fGKuX3ZTcxBsVIWQwt1Xi6NU9IBJS2PAWK8TWM5DIfUi/yKRBy?= =?us-ascii?q?brysXNWD14LrjKvoo9X6WEZhvHKFe7R8LRG7/036l/I9ps9cEJs30QbDuXBSeu?= =?us-ascii?q?5blitCLFOXmAvgtI/rpMYwuwwZgf8q9tZBXKPmZOx4COUAVHV1e1wyscbrrxjY?= =?us-ascii?q?UiOU4WYHF34RllxPGQeBpAnzQ5PZqiLnsq943y6AMIv9SrVwES+u9I93WRToj2?= =?us-ascii?q?EBLDd//2bJzoR9lKtBoQmJvxVlwpXMZIieObx5ZKyZNd8eSGZPWdxdfzZMDoO1?= =?us-ascii?q?c80ECO9Fdf1Vpo+7p1YAsQqWBA+wCeepwThNwjf21Kl8yeM6HAfdwAsrHPoBtX?= =?us-ascii?q?3VqJP+M6JBf/qyyfzkxDOLTe5M1jr788CcfhknoPyIX5p1fM7VwE8oB0XOiVDG?= =?us-ascii?q?+t+tBC+cyulY6zvT1OFnT+/6zjd/8ww=3D?= X-IronPort-Anti-Spam-Filtered: true X-IronPort-Anti-Spam-Result: =?us-ascii?q?A0GfBADVF4hX/4GlL8FcHQGEcrZhhAqGG?= =?us-ascii?q?QKBewEBAQEBAQICYieCMgQBEgGCEwIBAydQAhAYCDFXBxKINMBiATCVEgWOB3G?= =?us-ascii?q?KJ45bjy+QGVSCCRyBTzkyh24BAQE?= X-IPAS-Result: =?us-ascii?q?A0GfBADVF4hX/4GlL8FcHQGEcrZhhAqGGQKBewEBAQEBAQI?= =?us-ascii?q?CYieCMgQBEgGCEwIBAydQAhAYCDFXBxKINMBiATCVEgWOB3GKJ45bjy+QGVSCC?= =?us-ascii?q?RyBTzkyh24BAQE?= X-IronPort-AV: E=Sophos;i="5.28,365,1464652800"; d="scan'208";a="17666565" Received: from mail-il-dmz.mellanox.com (HELO mellanox.co.il) ([193.47.165.129]) by emsm-gh1-uea11.nsa.gov with ESMTP; 14 Jul 2016 22:57:37 +0000 Received: from Internal Mail-Server by MTLPINE1 (envelope-from danielj@mellanox.com) with ESMTPS (AES256-SHA encrypted); 15 Jul 2016 01:57:36 +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 u6EMv9u4030854; Fri, 15 Jul 2016 01:57:32 +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 v2 3/9] selinux lsm IB/core: Implement LSM notification system Date: Fri, 15 Jul 2016 01:56:45 +0300 Message-Id: <1468537011-20407-4-git-send-email-danielj@mellanox.com> X-Mailer: git-send-email 1.7.1 In-Reply-To: <1468537011-20407-1-git-send-email-danielj@mellanox.com> References: <1468537011-20407-1-git-send-email-danielj@mellanox.com> X-Mailman-Approved-At: Fri, 15 Jul 2016 08:13:30 -0400 X-BeenThere: selinux@tycho.nsa.gov X-Mailman-Version: 2.1.20 Precedence: list List-Id: "Security-Enhanced Linux \(SELinux\) mailing list" List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , 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 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 is from SELinux when the AVC cache changes. 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 --- drivers/infiniband/core/device.c | 46 ++++++++++++++++++++++++++++++ include/linux/security.h | 10 ++++++ security/security.c | 58 ++++++++++++++++++++++++++++++++++++++ security/selinux/hooks.c | 5 ++- security/selinux/selinuxfs.c | 2 + 5 files changed, 119 insertions(+), 2 deletions(-) diff --git a/drivers/infiniband/core/device.c b/drivers/infiniband/core/device.c index 5b42e83..3219a7a 100644 --- a/drivers/infiniband/core/device.c +++ b/drivers/infiniband/core/device.c @@ -39,6 +39,7 @@ #include #include #include +#include #include #include #include @@ -82,6 +83,10 @@ static LIST_HEAD(client_list); static DEFINE_MUTEX(device_mutex); static DECLARE_RWSEM(lists_rwsem); +static void ib_policy_change_task(struct work_struct *work); +static DECLARE_WORK(ib_policy_change_work, ib_policy_change_task); + +static u32 lsm_callback_id; static int ib_device_check_mandatory(struct ib_device *device) { @@ -344,6 +349,37 @@ 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 void ib_security_change(enum lsm_event event, void *ctx, void *lsm_data) +{ + if (event != LSM_POLICY_CHANGE) + return; + + schedule_work(&ib_policy_change_work); +} + /** * ib_register_device - Register an IB device with IB core * @device:Device to register @@ -1075,10 +1111,19 @@ static int __init ib_core_init(void) goto err_sa; } + ret = security_register_lsm_notifier(&ib_security_change, NULL, + &lsm_callback_id); + 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 +1143,7 @@ err: static void __exit ib_core_cleanup(void) { + security_unregister_lsm_notifier(lsm_callback_id); ib_cache_cleanup(); ib_remove_ibnl_clients(); ib_sa_cleanup(); diff --git a/include/linux/security.h b/include/linux/security.h index 33e23c4..bf53911 100644 --- a/include/linux/security.h +++ b/include/linux/security.h @@ -69,6 +69,16 @@ struct audit_krule; struct user_namespace; struct timezone; +enum lsm_event { + LSM_POLICY_CHANGE, +}; + +typedef void (*lsm_notifier)(enum lsm_event event, void *ctx, void *data); + +void security_lsm_notify(enum lsm_event event, void *data); +int security_register_lsm_notifier(lsm_notifier func, void *ctx, u32 *id); +void security_unregister_lsm_notifier(u32 id); + /* 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 234982d..1263c1d 100644 --- a/security/security.c +++ b/security/security.c @@ -33,6 +33,18 @@ /* Maximum number of letters for an LSM name string */ #define SECURITY_NAME_MAX 10 +struct lsm_notifier_entry { + u32 callback_id; + lsm_notifier func; + void *ctx; + struct list_head list; + struct rcu_head rcu; +}; + +static LIST_HEAD(lsm_notifier_list); +static DEFINE_SPINLOCK(lsm_notifier_lock); +static u32 next_callback_id; + /* Boot-time LSM user choice */ static __initdata char chosen_lsm[SECURITY_NAME_MAX + 1] = CONFIG_DEFAULT_SECURITY; @@ -98,6 +110,52 @@ int __init security_module_enable(const char *module) return !strcmp(module, chosen_lsm); } +void security_lsm_notify(enum lsm_event event, void *data) +{ + struct lsm_notifier_entry *entry; + + rcu_read_lock(); + list_for_each_entry_rcu(entry, &lsm_notifier_list, list) + entry->func(event, entry->ctx, data); + rcu_read_unlock(); +} +EXPORT_SYMBOL(security_lsm_notify); + +int security_register_lsm_notifier(lsm_notifier func, void *ctx, u32 *id) +{ + struct lsm_notifier_entry *entry; + + entry = kzalloc(sizeof(*entry), GFP_KERNEL); + if (!entry) + return -ENOMEM; + + entry->func = func; + entry->ctx = ctx; + + spin_lock(&lsm_notifier_lock); + entry->callback_id = next_callback_id++; + *id = entry->callback_id; + list_add_rcu(&entry->list, &lsm_notifier_list); + spin_unlock(&lsm_notifier_lock); + + return 0; +} +EXPORT_SYMBOL(security_register_lsm_notifier); + +void security_unregister_lsm_notifier(u32 id) +{ + struct lsm_notifier_entry *entry; + + list_for_each_entry_rcu(entry, &lsm_notifier_list, list) { + if (entry->callback_id == id) { + list_del_rcu(&entry->list); + kfree_rcu(entry, rcu); + break; + } + } +} +EXPORT_SYMBOL(security_unregister_lsm_notifier); + /* * Hook list operation macros. * diff --git a/security/selinux/hooks.c b/security/selinux/hooks.c index a86d537..a363202 100644 --- a/security/selinux/hooks.c +++ b/security/selinux/hooks.c @@ -159,13 +159,14 @@ static int selinux_peerlbl_enabled(void) return (selinux_policycap_alwaysnetwork || netlbl_enabled() || selinux_xfrm_enabled()); } -static int selinux_netcache_avc_callback(u32 event) +static int selinux_cache_avc_callback(u32 event) { if (event == AVC_CALLBACK_RESET) { sel_netif_flush(); sel_netnode_flush(); sel_netport_flush(); synchronize_net(); + security_lsm_notify(LSM_POLICY_CHANGE, NULL); } return 0; } @@ -6235,7 +6236,7 @@ static __init int selinux_init(void) security_add_hooks(selinux_hooks, ARRAY_SIZE(selinux_hooks)); - if (avc_add_callback(selinux_netcache_avc_callback, AVC_CALLBACK_RESET)) + if (avc_add_callback(selinux_cache_avc_callback, AVC_CALLBACK_RESET)) panic("SELinux: Unable to register AVC netcache callback\n"); if (selinux_enforcing) diff --git a/security/selinux/selinuxfs.c b/security/selinux/selinuxfs.c index 1b1fd27..adc09e7 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) + security_lsm_notify(LSM_POLICY_CHANGE, NULL); } length = count; out: