Skip to contentSkip to conversation

LSM: switch to blocking policy update notifiers

PATCH Superseded Janne Karhunen 5 participants 13 messages Jun 5, 2019 – Jun 9, 2019

Subsystems (6)

v1 of 2 Superseded Latest is v2 →

lore
Share & tools
lore
#0 lore raw

PATCH 1/2 LSM: switch to blocking policy update notifiers

Atomic policy updaters are not very useful as they cannot usually perform the policy updates on their own. Since it seems that there is no strict need for the atomicity, switch to the blocking variant. While doing so, rename the functions accordingly.

---
 drivers/infiniband/core/device.c |  6 +++---
 include/linux/security.h         |  6 +++---
 security/security.c              | 23 +++++++++++++----------
 security/selinux/hooks.c         |  2 +-
 security/selinux/selinuxfs.c     |  2 +-
 5 files changed, 21 insertions(+), 18 deletions(-)
drivers/infiniband/core/device.c +3 -3
index 78dc07c6ac4b..61c0c93a2e73 100644
@@ -2499,7 +2499,7 @@ static int __init ib_core_init(void)
 		goto err_mad;
 	}
 
-	ret = register_lsm_notifier(&ibdev_lsm_nb);
+	ret = register_blocking_lsm_notifier(&ibdev_lsm_nb);
 	if (ret) {
 		pr_warn("Couldn't register LSM notifier. ret %d\n", ret);
 		goto err_sa;
@@ -2518,7 +2518,7 @@ static int __init ib_core_init(void)
 	return 0;
 
 err_compat:
-	unregister_lsm_notifier(&ibdev_lsm_nb);
+	unregister_blocking_lsm_notifier(&ibdev_lsm_nb);
 err_sa:
 	ib_sa_cleanup();
 err_mad:
@@ -2544,7 +2544,7 @@ static void __exit ib_core_cleanup(void)
 	nldev_exit();
 	rdma_nl_unregister(RDMA_NL_LS);
 	unregister_pernet_device(&rdma_dev_net_ops);
-	unregister_lsm_notifier(&ibdev_lsm_nb);
+	unregister_blocking_lsm_notifier(&ibdev_lsm_nb);
 	ib_sa_cleanup();
 	ib_mad_cleanup();
 	addr_cleanup();
include/linux/security.h +3 -3
index 659071c2e57c..fc655fbe44ad 100644
@@ -189,9 +189,9 @@ static inline const char *kernel_load_data_id_str(enum kernel_load_data_id id)
 
 #ifdef CONFIG_SECURITY
 
-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);
+int call_blocking_lsm_notifier(enum lsm_event event, void *data);
+int register_blocking_lsm_notifier(struct notifier_block *nb);
+int unregister_blocking_lsm_notifier(struct notifier_block *nb);
 
 /* prototypes */
 extern int security_init(void);
security/security.c +13 -10
index c01a88f65ad8..6bfc7636ddb7 100644
@@ -39,7 +39,7 @@
 #define LSM_COUNT (__end_lsm_info - __start_lsm_info)
 
 struct security_hook_heads security_hook_heads __lsm_ro_after_init;
-static ATOMIC_NOTIFIER_HEAD(lsm_notifier_chain);
+static BLOCKING_NOTIFIER_HEAD(blocking_lsm_notifier_chain);
 
 static struct kmem_cache *lsm_file_cache;
 static struct kmem_cache *lsm_inode_cache;
@@ -430,23 +430,26 @@ void __init security_add_hooks(struct security_hook_list *hooks, int count,
 		panic("%s - Cannot get early memory.\n", __func__);
 }
 
-int call_lsm_notifier(enum lsm_event event, void *data)
+int call_blocking_lsm_notifier(enum lsm_event event, void *data)
 {
-	return atomic_notifier_call_chain(&lsm_notifier_chain, event, data);
+	return blocking_notifier_call_chain(&blocking_lsm_notifier_chain,
+					    event, data);
 }
-EXPORT_SYMBOL(call_lsm_notifier);
+EXPORT_SYMBOL(call_blocking_lsm_notifier);
 
-int register_lsm_notifier(struct notifier_block *nb)
+int register_blocking_lsm_notifier(struct notifier_block *nb)
 {
-	return atomic_notifier_chain_register(&lsm_notifier_chain, nb);
+	return blocking_notifier_chain_register(&blocking_lsm_notifier_chain,
+						nb);
 }
-EXPORT_SYMBOL(register_lsm_notifier);
+EXPORT_SYMBOL(register_blocking_lsm_notifier);
 
-int unregister_lsm_notifier(struct notifier_block *nb)
+int unregister_blocking_lsm_notifier(struct notifier_block *nb)
 {
-	return atomic_notifier_chain_unregister(&lsm_notifier_chain, nb);
+	return blocking_notifier_chain_unregister(&blocking_lsm_notifier_chain,
+						  nb);
 }
-EXPORT_SYMBOL(unregister_lsm_notifier);
+EXPORT_SYMBOL(unregister_blocking_lsm_notifier);
 
 /**
  * lsm_cred_alloc - allocate a composite cred blob
security/selinux/hooks.c +1 -1
index c61787b15f27..c1e37018c8eb 100644
@@ -197,7 +197,7 @@ static int selinux_lsm_notifier_avc_callback(u32 event)
 {
 	if (event == AVC_CALLBACK_RESET) {
 		sel_ib_pkey_flush();
-		call_lsm_notifier(LSM_POLICY_CHANGE, NULL);
+		call_blocking_lsm_notifier(LSM_POLICY_CHANGE, NULL);
 	}
 
 	return 0;
security/selinux/selinuxfs.c +1 -1
index 145ee62f205a..1e2e3e4b5fdb 100644
@@ -180,7 +180,7 @@ static ssize_t sel_write_enforce(struct file *file, const char __user *buf,
 		selnl_notify_setenforce(new_value);
 		selinux_status_update_setenforce(state, new_value);
 		if (!new_value)
-			call_lsm_notifier(LSM_POLICY_CHANGE, NULL);
+			call_blocking_lsm_notifier(LSM_POLICY_CHANGE, NULL);
 	}
 	length = count;
 out:
Signature · 1 line
2.17.1
#1 lore raw

PATCH 2/2 ima: use the lsm policy update notifier

Don't do lazy policy updates while running the rule matching, run the updates as they happen.

Depends on commit 2d1d5cee66d1 ("LSM: switch to blocking policy update notifiers")

---
 security/integrity/ima/ima.h        |   2 +
 security/integrity/ima/ima_main.c   |   8 ++
 security/integrity/ima/ima_policy.c | 124 +++++++++++++++++++++++-----
 3 files changed, 114 insertions(+), 20 deletions(-)
security/integrity/ima/ima.h +2 -0
index d213e835c498..2203451862d4 100644
@@ -154,6 +154,8 @@ unsigned long ima_get_binary_runtime_size(void);
 int ima_init_template(void);
 void ima_init_template_list(void);
 int __init ima_init_digests(void);
+int ima_lsm_policy_change(struct notifier_block *nb, unsigned long event,
+			  void *lsm_data);
 
 /*
  * used to protect h_table and sha_table
security/integrity/ima/ima_main.c +8 -0
index f16353b5097e..9e3ea8a3f2db 100644
@@ -43,6 +43,10 @@ int ima_appraise;
 int ima_hash_algo = HASH_ALGO_SHA1;
 static int hash_setup_done;
 
+static struct notifier_block ima_lsm_policy_notifier = {
+	.notifier_call = ima_lsm_policy_change,
+};
+
 static int __init hash_setup(char *str)
 {
 	struct ima_template_desc *template_desc = ima_template_desc_current();
@@ -621,6 +625,10 @@ static int __init init_ima(void)
 		error = ima_init();
 	}
 
+	error = register_blocking_lsm_notifier(&ima_lsm_policy_notifier);
+	if (error)
+		pr_warn("Couldn't register LSM notifier, error %d\n", error);
+
 	if (!error)
 		ima_update_policy_flag();
 
security/integrity/ima/ima_policy.c +104 -20

157 lines not on this page · raw message

Signature · 1 line
2.17.1
#4 lore raw
In reply to Janne Karhunen
Quoted 51 lines · Janne Karhunen

Atomic policy updaters are not very useful as they cannot usually perform the policy updates on their own. Since it seems that there is no strict need for the atomicity, switch to the blocking variant. While doing so, rename the functions accordingly.

---
 drivers/infiniband/core/device.c |  6 +++---
 include/linux/security.h         |  6 +++---
 security/security.c              | 23 +++++++++++++----------
 security/selinux/hooks.c         |  2 +-
 security/selinux/selinuxfs.c     |  2 +-
 5 files changed, 21 insertions(+), 18 deletions(-)
drivers/infiniband/core/device.c +3 -3
index 78dc07c6ac4b..61c0c93a2e73 100644
@@ -2499,7 +2499,7 @@ static int __init ib_core_init(void)
 		goto err_mad;
 	}
 
-	ret = register_lsm_notifier(&ibdev_lsm_nb);
+	ret = register_blocking_lsm_notifier(&ibdev_lsm_nb);
 	if (ret) {
 		pr_warn("Couldn't register LSM notifier. ret %d\n", ret);
 		goto err_sa;
@@ -2518,7 +2518,7 @@ static int __init ib_core_init(void)
 	return 0;
 
 err_compat:
-	unregister_lsm_notifier(&ibdev_lsm_nb);
+	unregister_blocking_lsm_notifier(&ibdev_lsm_nb);
 err_sa:
 	ib_sa_cleanup();
 err_mad:
@@ -2544,7 +2544,7 @@ static void __exit ib_core_cleanup(void)
 	nldev_exit();
 	rdma_nl_unregister(RDMA_NL_LS);
 	unregister_pernet_device(&rdma_dev_net_ops);
-	unregister_lsm_notifier(&ibdev_lsm_nb);
+	unregister_blocking_lsm_notifier(&ibdev_lsm_nb);
 	ib_sa_cleanup();
 	ib_mad_cleanup();
 	addr_cleanup();
include/linux/security.h +0 -0
index 659071c2e57c..fc655fbe44ad 100644

include/linux/security.h static inline const char *kernel_load_data_id_str(enum kernel_load_data_id id) · line 194

Quoted 9 lines
include/linux/security.h +3 -3
@@ -189,9 +189,9 @@ static inline const char *kernel_load_data_id_str(enum kernel_load_data_id id)
 
 #ifdef CONFIG_SECURITY
 
-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);
+int call_blocking_lsm_notifier(enum lsm_event event, void *data);
+int register_blocking_lsm_notifier(struct notifier_block *nb);
+int unregister_blocking_lsm_notifier(struct notifier_block *nb);

Why is it important to change the names of these hooks? It's not like you had call_atomic_lsm_notifier() before. It seems like a lot of unnecessary code churn.

Quoted 78 lines
include/linux/security.h +0 -0
 /* prototypes */
 extern int security_init(void);
security/security.c +13 -10
index c01a88f65ad8..6bfc7636ddb7 100644
@@ -39,7 +39,7 @@
 #define LSM_COUNT (__end_lsm_info - __start_lsm_info)
 
 struct security_hook_heads security_hook_heads __lsm_ro_after_init;
-static ATOMIC_NOTIFIER_HEAD(lsm_notifier_chain);
+static BLOCKING_NOTIFIER_HEAD(blocking_lsm_notifier_chain);
 
 static struct kmem_cache *lsm_file_cache;
 static struct kmem_cache *lsm_inode_cache;
@@ -430,23 +430,26 @@ void __init security_add_hooks(struct security_hook_list *hooks, int count,
 		panic("%s - Cannot get early memory.\n", __func__);
 }
 
-int call_lsm_notifier(enum lsm_event event, void *data)
+int call_blocking_lsm_notifier(enum lsm_event event, void *data)
 {
-	return atomic_notifier_call_chain(&lsm_notifier_chain, event, data);
+	return blocking_notifier_call_chain(&blocking_lsm_notifier_chain,
+					    event, data);
 }
-EXPORT_SYMBOL(call_lsm_notifier);
+EXPORT_SYMBOL(call_blocking_lsm_notifier);
 
-int register_lsm_notifier(struct notifier_block *nb)
+int register_blocking_lsm_notifier(struct notifier_block *nb)
 {
-	return atomic_notifier_chain_register(&lsm_notifier_chain, nb);
+	return blocking_notifier_chain_register(&blocking_lsm_notifier_chain,
+						nb);
 }
-EXPORT_SYMBOL(register_lsm_notifier);
+EXPORT_SYMBOL(register_blocking_lsm_notifier);
 
-int unregister_lsm_notifier(struct notifier_block *nb)
+int unregister_blocking_lsm_notifier(struct notifier_block *nb)
 {
-	return atomic_notifier_chain_unregister(&lsm_notifier_chain, nb);
+	return blocking_notifier_chain_unregister(&blocking_lsm_notifier_chain,
+						  nb);
 }
-EXPORT_SYMBOL(unregister_lsm_notifier);
+EXPORT_SYMBOL(unregister_blocking_lsm_notifier);
 
 /**
  * lsm_cred_alloc - allocate a composite cred blob
security/selinux/hooks.c +1 -1
index c61787b15f27..c1e37018c8eb 100644
@@ -197,7 +197,7 @@ static int selinux_lsm_notifier_avc_callback(u32 event)
 {
 	if (event == AVC_CALLBACK_RESET) {
 		sel_ib_pkey_flush();
-		call_lsm_notifier(LSM_POLICY_CHANGE, NULL);
+		call_blocking_lsm_notifier(LSM_POLICY_CHANGE, NULL);
 	}
 
 	return 0;
security/selinux/selinuxfs.c +1 -1
index 145ee62f205a..1e2e3e4b5fdb 100644
@@ -180,7 +180,7 @@ static ssize_t sel_write_enforce(struct file *file, const char __user *buf,
 		selnl_notify_setenforce(new_value);
 		selinux_status_update_setenforce(state, new_value);
 		if (!new_value)
-			call_lsm_notifier(LSM_POLICY_CHANGE, NULL);
+			call_blocking_lsm_notifier(LSM_POLICY_CHANGE, NULL);
 	}
 	length = count;
 out:
#5 lore raw
Quoted 10 lines · Casey Schaufler

include/linux/security.h static inline const char *kernel_load_data_id_str(enum kernel_load_data_id id) · line 194

Quoted 6 lines
include/linux/security.h +3 -3
-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);
+int call_blocking_lsm_notifier(enum lsm_event event, void *data);
+int register_blocking_lsm_notifier(struct notifier_block *nb);
+int unregister_blocking_lsm_notifier(struct notifier_block *nb);

Why is it important to change the names of these hooks? It's not like you had call_atomic_lsm_notifier() before. It seems like a lot of unnecessary code churn.

Paul was thinking there will eventually be two sets of notifiers (atomic and blocking) and this creates the clear separation. That's probably true, but it does indeed create a pretty big change that it is not really needed yet. I'm fine either way.

--
Janne

#6 lore raw
Quoted 11 lines · Janne Karhunen
Quoted 9 lines · Casey Schaufler

include/linux/security.h static inline const char *kernel_load_data_id_str(enum kernel_load_data_id id) · line 194

Quoted 6 lines
include/linux/security.h +3 -3
-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);
+int call_blocking_lsm_notifier(enum lsm_event event, void *data);
+int register_blocking_lsm_notifier(struct notifier_block *nb);
+int unregister_blocking_lsm_notifier(struct notifier_block *nb);

Why is it important to change the names of these hooks? It's not like you had call_atomic_lsm_notifier() before. It seems like a lot of unnecessary code churn.

Quoted 2 lines

Paul was thinking there will eventually be two sets of notifiers (atomic and blocking) and this creates the clear separation.

One hook with an added "bool blocking" argument, if that's the only difference?

Quoted 7 lines

That's
probably true, but it does indeed create a pretty big change that it is not really needed yet. I'm fine either way.

--
Janne

maintainer
#7 lore raw
Quoted 12 lines · Casey Schaufler
Quoted 11 lines · Janne Karhunen
Quoted 9 lines · Casey Schaufler

include/linux/security.h static inline const char *kernel_load_data_id_str(enum kernel_load_data_id id) · line 194

Quoted 6 lines
include/linux/security.h +3 -3
-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);
+int call_blocking_lsm_notifier(enum lsm_event event, void *data);
+int register_blocking_lsm_notifier(struct notifier_block *nb);
+int unregister_blocking_lsm_notifier(struct notifier_block *nb);

Why is it important to change the names of these hooks? It's not like you had call_atomic_lsm_notifier() before. It seems like a lot of unnecessary code churn.

Quoted 5 lines
Quoted 2 lines

Paul was thinking there will eventually be two sets of notifiers (atomic and blocking) and this creates the clear separation.

One hook with an added "bool blocking" argument, if that's the only difference?

I think there is value in keeping a similar convention to the notifier code on which this is based, see include/linux/notifier.h.

Signature · 2 lines
paul moore
www.paul-moore.com
maintainer Acked-by on v1 1/2
#12 lore raw
In reply to Janne Karhunen
Quoted 7 lines · Janne Karhunen

Atomic policy updaters are not very useful as they cannot usually perform the policy updates on their own. Since it seems that there is no strict need for the atomicity, switch to the blocking variant. While doing so, rename the functions accordingly.

Quoted 8 lines
---
 drivers/infiniband/core/device.c |  6 +++---
 include/linux/security.h         |  6 +++---
 security/security.c              | 23 +++++++++++++----------
 security/selinux/hooks.c         |  2 +-
 security/selinux/selinuxfs.c     |  2 +-
 5 files changed, 21 insertions(+), 18 deletions(-)
Quoted 126 lines
drivers/infiniband/core/device.c +3 -3
index 78dc07c6ac4b..61c0c93a2e73 100644
@@ -2499,7 +2499,7 @@ static int __init ib_core_init(void)
                goto err_mad;
        }
 
-       ret = register_lsm_notifier(&ibdev_lsm_nb);
+       ret = register_blocking_lsm_notifier(&ibdev_lsm_nb);
        if (ret) {
                pr_warn("Couldn't register LSM notifier. ret %d\n", ret);
                goto err_sa;
@@ -2518,7 +2518,7 @@ static int __init ib_core_init(void)
        return 0;
 
 err_compat:
-       unregister_lsm_notifier(&ibdev_lsm_nb);
+       unregister_blocking_lsm_notifier(&ibdev_lsm_nb);
 err_sa:
        ib_sa_cleanup();
 err_mad:
@@ -2544,7 +2544,7 @@ static void __exit ib_core_cleanup(void)
        nldev_exit();
        rdma_nl_unregister(RDMA_NL_LS);
        unregister_pernet_device(&rdma_dev_net_ops);
-       unregister_lsm_notifier(&ibdev_lsm_nb);
+       unregister_blocking_lsm_notifier(&ibdev_lsm_nb);
        ib_sa_cleanup();
        ib_mad_cleanup();
        addr_cleanup();
include/linux/security.h +3 -3
index 659071c2e57c..fc655fbe44ad 100644
@@ -189,9 +189,9 @@ static inline const char *kernel_load_data_id_str(enum kernel_load_data_id id)
 
 #ifdef CONFIG_SECURITY
 
-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);
+int call_blocking_lsm_notifier(enum lsm_event event, void *data);
+int register_blocking_lsm_notifier(struct notifier_block *nb);
+int unregister_blocking_lsm_notifier(struct notifier_block *nb);
 
 /* prototypes */
 extern int security_init(void);
security/security.c +13 -10
index c01a88f65ad8..6bfc7636ddb7 100644
@@ -39,7 +39,7 @@
 #define LSM_COUNT (__end_lsm_info - __start_lsm_info)
 
 struct security_hook_heads security_hook_heads __lsm_ro_after_init;
-static ATOMIC_NOTIFIER_HEAD(lsm_notifier_chain);
+static BLOCKING_NOTIFIER_HEAD(blocking_lsm_notifier_chain);
 
 static struct kmem_cache *lsm_file_cache;
 static struct kmem_cache *lsm_inode_cache;
@@ -430,23 +430,26 @@ void __init security_add_hooks(struct security_hook_list *hooks, int count,
                panic("%s - Cannot get early memory.\n", __func__);
 }
 
-int call_lsm_notifier(enum lsm_event event, void *data)
+int call_blocking_lsm_notifier(enum lsm_event event, void *data)
 {
-       return atomic_notifier_call_chain(&lsm_notifier_chain, event, data);
+       return blocking_notifier_call_chain(&blocking_lsm_notifier_chain,
+                                           event, data);
 }
-EXPORT_SYMBOL(call_lsm_notifier);
+EXPORT_SYMBOL(call_blocking_lsm_notifier);
 
-int register_lsm_notifier(struct notifier_block *nb)
+int register_blocking_lsm_notifier(struct notifier_block *nb)
 {
-       return atomic_notifier_chain_register(&lsm_notifier_chain, nb);
+       return blocking_notifier_chain_register(&blocking_lsm_notifier_chain,
+                                               nb);
 }
-EXPORT_SYMBOL(register_lsm_notifier);
+EXPORT_SYMBOL(register_blocking_lsm_notifier);
 
-int unregister_lsm_notifier(struct notifier_block *nb)
+int unregister_blocking_lsm_notifier(struct notifier_block *nb)
 {
-       return atomic_notifier_chain_unregister(&lsm_notifier_chain, nb);
+       return blocking_notifier_chain_unregister(&blocking_lsm_notifier_chain,
+                                                 nb);
 }
-EXPORT_SYMBOL(unregister_lsm_notifier);
+EXPORT_SYMBOL(unregister_blocking_lsm_notifier);
 
 /**
  * lsm_cred_alloc - allocate a composite cred blob
security/selinux/hooks.c +1 -1
index c61787b15f27..c1e37018c8eb 100644
@@ -197,7 +197,7 @@ static int selinux_lsm_notifier_avc_callback(u32 event)
 {
        if (event == AVC_CALLBACK_RESET) {
                sel_ib_pkey_flush();
-               call_lsm_notifier(LSM_POLICY_CHANGE, NULL);
+               call_blocking_lsm_notifier(LSM_POLICY_CHANGE, NULL);
        }
 
        return 0;
security/selinux/selinuxfs.c +1 -2
index 145ee62f205a..1e2e3e4b5fdb 100644
@@ -180,7 +180,7 @@ static ssize_t sel_write_enforce(struct file *file, const char __user *buf,
                selnl_notify_setenforce(new_value);
                selinux_status_update_setenforce(state, new_value);
                if (!new_value)
-                       call_lsm_notifier(LSM_POLICY_CHANGE, NULL);
+                       call_blocking_lsm_notifier(LSM_POLICY_CHANGE, NULL);
        }
        length = count;
 out:
--

2.17.1

Signature · 2 lines
paul moore
www.paul-moore.com
maintainer
#2 lore raw

Re: [PATCH 2/2] ima: use the lsm policy update notifier

In reply to Janne Karhunen

Hi Janne,

Quoted 6 lines · Janne Karhunen

Don't do lazy policy updates while running the rule matching, run the updates as they happen.

Depends on commit 2d1d5cee66d1 ("LSM: switch to blocking policy update notifiers")

Thanks!  Just a couple of minor things.  Comments inline below.

Quoted 54 lines
---
 security/integrity/ima/ima.h        |   2 +
 security/integrity/ima/ima_main.c   |   8 ++
 security/integrity/ima/ima_policy.c | 124 +++++++++++++++++++++++-----
 3 files changed, 114 insertions(+), 20 deletions(-)
security/integrity/ima/ima.h +2 -0
index d213e835c498..2203451862d4 100644
@@ -154,6 +154,8 @@ unsigned long ima_get_binary_runtime_size(void);
 int ima_init_template(void);
 void ima_init_template_list(void);
 int __init ima_init_digests(void);
+int ima_lsm_policy_change(struct notifier_block *nb, unsigned long event,
+			  void *lsm_data);
 
 /*
  * used to protect h_table and sha_table
security/integrity/ima/ima_main.c +8 -0
index f16353b5097e..9e3ea8a3f2db 100644
@@ -43,6 +43,10 @@ int ima_appraise;
 int ima_hash_algo = HASH_ALGO_SHA1;
 static int hash_setup_done;
 
+static struct notifier_block ima_lsm_policy_notifier = {
+	.notifier_call = ima_lsm_policy_change,
+};
+
 static int __init hash_setup(char *str)
 {
 	struct ima_template_desc *template_desc = ima_template_desc_current();
@@ -621,6 +625,10 @@ static int __init init_ima(void)
 		error = ima_init();
 	}
 
+	error = register_blocking_lsm_notifier(&ima_lsm_policy_notifier);
+	if (error)
+		pr_warn("Couldn't register LSM notifier, error %d\n", error);
+
 	if (!error)
 		ima_update_policy_flag();
 
security/integrity/ima/ima_policy.c +1 -0
index 1cc822a59054..7129dc4cd396 100644
@@ -249,31 +249,121 @@ static int __init default_appraise_policy_setup(char *str)
 }
 __setup("ima_appraise_tcb", default_appraise_policy_setup);
 
+static void ima_lsm_free_rule(struct ima_rule_entry *entry)

security/integrity/ima/ima_policy.c static int __init default_appraise_policy_setup(char *str) · line 262

Quoted 10 lines
security/integrity/ima/ima_policy.c +10 -0
+{
+	int i;
+
+	for (i = 0; i < MAX_LSM_RULES; i++) {
+		kfree(entry->lsm[i].rule);
+		kfree(entry->lsm[i].args_p);
+	}
+	kfree(entry->fsname);
+	kfree(entry);
+}

Matthew's patch, which adds per policy template format support, adds a "template" field to entry.  In case anyone wants to backport this patch, it might be simpler to make the change as a separate patch.

Quoted 29 lines
security/integrity/ima/ima_policy.c +29 -0
+
+static struct ima_rule_entry *ima_lsm_copy_rule(struct ima_rule_entry *entry)
+{
+	struct ima_rule_entry *nentry;
+	int i, result;
+
+	nentry = kmalloc(sizeof(*nentry), GFP_KERNEL);
+	if (!nentry)
+		return NULL;
+
+	memcpy(nentry, entry, sizeof(*nentry));
+	nentry->fsname = NULL;
+	for (i = 0; i < MAX_LSM_RULES; i++) {
+		nentry->lsm[i].rule = NULL;
+		nentry->lsm[i].args_p = NULL;
+	}
+
+	if (entry->fsname) {
+		nentry->fsname = kstrdup(entry->fsname, GFP_KERNEL);
+		if (!nentry->fsname)
+			goto out_err;
+	}
+	for (i = 0; i < MAX_LSM_RULES; i++) {
+		if (!entry->lsm[i].rule)
+			continue;
+
+		nentry->lsm[i].type = entry->lsm[i].type;
+		nentry->lsm[i].args_p = kstrdup(entry->lsm[i].args_p,
+						GFP_KERNEL);

security/integrity/ima/ima_policy.c static int __init default_appraise_policy_setup(char *str) · line 302

Quoted 10 lines
security/integrity/ima/ima_policy.c +10 -0
+		if (!nentry->lsm[i].args_p)
+			goto out_err;
+
+		result = security_filter_rule_init(nentry->lsm[i].type,
+						   Audit_equal,
+						   nentry->lsm[i].args_p,
+						   &nentry->lsm[i].rule);
+		if (result == -EINVAL)
+			pr_warn("ima: rule for LSM \'%d\' is invalid\n",
+				entry->lsm[i].type);

If LSM labels can come and go, then perhaps instead of saying "invalid" say "undefined" or "missing".

Quoted 52 lines
security/integrity/ima/ima_policy.c +31 -13
+
+	}
+	return nentry;
+
+out_err:
+	ima_lsm_free_rule(nentry);
+	return NULL;
+}
+
+static int ima_lsm_update_rule(struct ima_rule_entry *entry)
+{
+	struct ima_rule_entry *nentry;
+
+	nentry = ima_lsm_copy_rule(entry);
+	if (!nentry)
+		return -ENOMEM;
+
+	list_replace_rcu(&entry->list, &nentry->list);
+	synchronize_rcu();
+	ima_lsm_free_rule(entry);
+
+	return 0;
+}
+
 /*
  * The LSM policy can be reloaded, leaving the IMA LSM based rules referring
  * to the old, stale LSM policy.  Update the IMA LSM based rules to reflect
- * the reloaded LSM policy.  We assume the rules still exist; and BUG_ON() if
- * they don't.
+ * the reloaded LSM policy.
  */
 static void ima_lsm_update_rules(void)
 {
-	struct ima_rule_entry *entry;
-	int result;
-	int i;
+	struct ima_rule_entry *entry, *e;
+	int i, result, needs_update;
 
-	list_for_each_entry(entry, &ima_policy_rules, list) {
+	list_for_each_entry_safe(entry, e, &ima_policy_rules, list) {
+		needs_update = 0;
 		for (i = 0; i < MAX_LSM_RULES; i++) {
-			if (!entry->lsm[i].rule)
-				continue;
-			result = security_filter_rule_init(entry->lsm[i].type,
-							   Audit_equal,
-							   entry->lsm[i].args_p,
-							   &entry->lsm[i].rule);
-			BUG_ON(!entry->lsm[i].rule);
+			if (entry->lsm[i].rule) {
+				needs_update = 1;

security/integrity/ima/ima_policy.c static int __init default_appraise_policy_setup(char *str) · line 351

Quoted 10 lines
security/integrity/ima/ima_policy.c +10 -0
+				break;
+			}
+		}
+		if (!needs_update)
+			continue;
+
+		result = ima_lsm_update_rule(entry);
+		if (result) {
+			pr_err("ima: lsm rule update error %d\n",
+				result);

No need for separate line.

Mimi

Quoted 43 lines
security/integrity/ima/ima_policy.c +12 -7
+			return;
 		}
 	}
 }
 
+int ima_lsm_policy_change(struct notifier_block *nb, unsigned long event,
+			  void *lsm_data)
+{
+	if (event != LSM_POLICY_CHANGE)
+		return NOTIFY_DONE;
+
+	ima_lsm_update_rules();
+	return NOTIFY_OK;
+}
+
 /**
  * ima_match_rules - determine whether an inode matches the measure rule.
  * @rule: a pointer to a rule
@@ -327,11 +417,10 @@ static bool ima_match_rules(struct ima_rule_entry *rule, struct inode *inode,
 	for (i = 0; i < MAX_LSM_RULES; i++) {
 		int rc = 0;
 		u32 osid;
-		int retried = 0;
 
 		if (!rule->lsm[i].rule)
 			continue;
-retry:
+
 		switch (i) {
 		case LSM_OBJ_USER:
 		case LSM_OBJ_ROLE:
@@ -352,11 +441,6 @@ static bool ima_match_rules(struct ima_rule_entry *rule, struct inode *inode,
 		default:
 			break;
 		}
-		if ((rc < 0) && (!retried)) {
-			retried = 1;
-			ima_lsm_update_rules();
-			goto retry;
-		}
 		if (!rc)
 			return false;
 	}
maintainer
#3 lore raw

Re: [PATCH 2/2] ima: use the lsm policy update notifier

Hi Janne,

One more comment below ...

Quoted 8 lines
Quoted 8 lines
security/integrity/ima/ima_policy.c +8 -0
+
+static struct ima_rule_entry *ima_lsm_copy_rule(struct ima_rule_entry *entry)
+{
+	struct ima_rule_entry *nentry;
+	int i, result;
+
+	nentry = kmalloc(sizeof(*nentry), GFP_KERNEL);
+	if (!nentry)
Quoted 8 lines

security/integrity/ima/ima_policy.c static int __init default_appraise_policy_setup(char *str) · line 279

Quoted 8 lines
security/integrity/ima/ima_policy.c +8 -0
+		return NULL;
+
+	memcpy(nentry, entry, sizeof(*nentry));
+	nentry->fsname = NULL;
+	for (i = 0; i < MAX_LSM_RULES; i++) {
+		nentry->lsm[i].rule = NULL;
+		nentry->lsm[i].args_p = NULL;
+	}

I don't think this loop is necessary.  Either use kzalloc() or move the initialization to inside the loop below.

Quoted 9 lines

security/integrity/ima/ima_policy.c static int __init default_appraise_policy_setup(char *str) · line 288

Quoted 9 lines
security/integrity/ima/ima_policy.c +9 -0
+
+	if (entry->fsname) {
+		nentry->fsname = kstrdup(entry->fsname, GFP_KERNEL);
+		if (!nentry->fsname)
+			goto out_err;
+	}
+	for (i = 0; i < MAX_LSM_RULES; i++) {
+		if (!entry->lsm[i].rule)
+			continue;

To here.

Quoted 6 lines

security/integrity/ima/ima_policy.c static int __init default_appraise_policy_setup(char *str) · line 294

Quoted 6 lines
security/integrity/ima/ima_policy.c +6 -0
+
+		nentry->lsm[i].type = entry->lsm[i].type;
+		nentry->lsm[i].args_p = kstrdup(entry->lsm[i].args_p,
+						GFP_KERNEL);
+		if (!nentry->lsm[i].args_p)
+			goto out_err;

If the memory allocation fails, then nentry will be freed anyway.

thanks,

Mimid

maintainer
#8 lore raw
In reply to Paul Moore
Quoted 9 lines · Paul Moore
Quoted 4 lines · Casey Schaufler

On 6/5/2019 9:51 AM, Janne Karhunen wrote:

One hook with an added "bool blocking" argument, if that's the only difference?

I think there is value in keeping a similar convention to the notifier code on which this is based, see include/linux/notifier.h.

Although this doesn't seem to be what other users in the kernel are doing. Probably the less code churn the better in this case.

Signature · 2 lines
James Morris
<[email protected]>
maintainer
#9 lore raw
Quoted 11 lines · James Morris
Quoted 8 lines · Paul Moore
Quoted 4 lines · Casey Schaufler

On 6/5/2019 9:51 AM, Janne Karhunen wrote:

One hook with an added "bool blocking" argument, if that's the only difference?

I think there is value in keeping a similar convention to the notifier code on which this is based, see include/linux/notifier.h.

Although this doesn't seem to be what other users in the kernel are doing.

How many of them potentially have the need for both blocking and non-blocking notifiers? I didn't go through the entire list of callers, but it seems all that I looked at used only one type. The simple fact that we started with one type of notifier for the LSM, and we are now switching to the other (and getting lucky that it is safe to do so for the existing callers) seems to lend some weight to the argument we may need both and adding "block"/"blocking"/etc. to the name has value.

Signature · 2 lines
paul moore
www.paul-moore.com
maintainer
#10 lore raw
Quoted 13 lines · Paul Moore
Quoted 11 lines · James Morris
Quoted 8 lines · Paul Moore
Quoted 4 lines · Casey Schaufler

On 6/5/2019 9:51 AM, Janne Karhunen wrote:

One hook with an added "bool blocking" argument, if that's the only difference?

I think there is value in keeping a similar convention to the notifier code on which this is based, see include/linux/notifier.h.

Although this doesn't seem to be what other users in the kernel are doing.

Quoted 8 lines

How many of them potentially have the need for both blocking and non-blocking notifiers? I didn't go through the entire list of callers, but it seems all that I looked at used only one type. The simple fact that we started with one type of notifier for the LSM, and we are now switching to the other (and getting lucky that it is safe to do so for the existing callers) seems to lend some weight to the argument we may need both and adding "block"/"blocking"/etc. to the name has value.

Fair enough.

Signature · 2 lines
James Morris
<[email protected]>
#11 lore raw
Quoted 7 lines · James Morris
Quoted 5 lines

simple fact that we started with one type of notifier for the LSM, and we are now switching to the other (and getting lucky that it is safe to do so for the existing callers) seems to lend some weight to the argument we may need both and adding "block"/"blocking"/etc. to the name has value.

Fair enough.

Ok, I take this to mean we have an agreement to go with this variant. I will post the fixes to the Mimi's findings on top of this one tomorrow.

--
Janne