Skip to contentSkip to conversation

Backport handling -ESTALE policy update failure to 4.19

PATCH Superseded GUO Zihua 2 participants 8 messages Feb 16, 2023 – Feb 23, 2023

Subsystems (5)

v2 of 3 Superseded Latest is v3 →

lore
Share & tools
lore

GUO Zihua

author
#0 lore raw

v2 cover Backport handling -ESTALE policy update failure to 4.19

This series backports patches in order to resolve the issue discussed here:
https://lore.kernel.org/selinux/[email protected]/

This required backporting the non-blocking LSM policy update mechanism prerequisite patches. As well as bugfixes that follows.

c66f67414c1f ("IB/core: Don't register each MAD agent for LSM notifier") is merged as the prerequisite of 42df744c4166 ("LSM: switch to blocking policy update notifiers"). e144d6b26541 ("ima: Evaluate error in init_ima()") is merged as a follow up bugfix for b16942455193 ("ima:
use the lsm policy update notifier"). 483ec26eed42 ("ima: ima/lsm policy rule loading logic bug fixes") and 9ff8a616dfab ("ima: Have the LSM free its audit rule") is also followup bugfixes. The former would change the behavior of rule loading without fixing any criticial bug so I don't think it's necessary, while the latter has already been merged.

I've tested the patches against said issue and can confirm that the issue is fixed.

This is a re-send of the original patchset as the original patchset might have a faulty cover letter. The original patchset could be found here:
https://patchwork.kernel.org/project/linux-integrity/list/?series=709367

Change log:
v2: Fixed build issue and backport bugfix commits for backported
patches.

Daniel Jurgens (1):
IB/core: Don't register each MAD agent for LSM notifier

GUO Zihua (1):
ima: Handle -ESTALE returned by ima_filter_rule_match()

Janne Karhunen (2):
LSM: switch to blocking policy update notifiers
ima: use the lsm policy update notifier

Roberto Sassu (1):
ima: Evaluate error in init_ima()

 drivers/infiniband/core/core_priv.h |   5 +
 drivers/infiniband/core/device.c    |   5 +-
 drivers/infiniband/core/security.c  |  51 +++++-----
 include/linux/security.h            |  12 +--
 include/rdma/ib_mad.h               |   3 +-
 security/integrity/ima/ima.h        |   2 +
 security/integrity/ima/ima_main.c   |  11 ++
 security/integrity/ima/ima_policy.c | 151 ++++++++++++++++++++++------
 security/security.c                 |  23 +++--
 security/selinux/hooks.c            |   2 +-
 security/selinux/selinuxfs.c        |   2 +-
 11 files changed, 193 insertions(+), 74 deletions(-)
Signature · 1 line
2.17.1
author
#1 lore raw

PATCH v2 1/5 IB/core: Don't register each MAD agent for LSM notifier

From: Daniel Jurgens <[email protected]>

[ Upstream commit c66f67414c1f88554485bb2a0abf8b5c0d741de7 ]

When creating many MAD agents in a short period of time, receive packet processing can be delayed long enough to cause timeouts while new agents are being added to the atomic notifier chain with IRQs disabled. Notifier chain registration and unregstration is an O(n) operation. With large numbers of MAD agents being created and destroyed simultaneously the CPUs spend too much time with interrupts disabled.

Instead of each MAD agent registering for it's own LSM notification, maintain a list of agents internally and register once, this registration already existed for handling the PKeys. This list is write mostly, so a normal spin lock is used vs a read/write lock. All MAD agents must be checked, so a single list is used instead of breaking them down per device.

Notifier calls are done under rcu_read_lock, so there isn't a risk of similar packet timeouts while checking the MAD agents security settings when notified.

---
 drivers/infiniband/core/core_priv.h |  5 +++
 drivers/infiniband/core/device.c    |  1 +
 drivers/infiniband/core/security.c  | 51 ++++++++++++++++-------------
 include/rdma/ib_mad.h               |  3 +-
 4 files changed, 35 insertions(+), 25 deletions(-)
drivers/infiniband/core/core_priv.h +5 -0
index 77c7005c396c..8fd8a822d9b6 100644
@@ -227,6 +227,7 @@ int ib_mad_agent_security_setup(struct ib_mad_agent *agent,
 				enum ib_qp_type qp_type);
 void ib_mad_agent_security_cleanup(struct ib_mad_agent *agent);
 int ib_mad_enforce_security(struct ib_mad_agent_private *map, u16 pkey_index);
+void ib_mad_agent_security_change(void);
 #else
 static inline void ib_security_destroy_port_pkey_list(struct ib_device *device)
 {
@@ -292,6 +293,10 @@ static inline int ib_mad_enforce_security(struct ib_mad_agent_private *map,
 {
 	return 0;
 }
+
+static inline void ib_mad_agent_security_change(void)
+{
+}
 #endif
 
 struct ib_device *ib_device_get_by_index(u32 ifindex);
drivers/infiniband/core/device.c +1 -0
index ffd0f43e2129..9740614a108d 100644
@@ -417,6 +417,7 @@ static int ib_security_change(struct notifier_block *nb, unsigned long event,
 		return NOTIFY_DONE;
 
 	schedule_work(&ib_policy_change_work);
+	ib_mad_agent_security_change();
 
 	return NOTIFY_OK;
 }
drivers/infiniband/core/security.c +28 -23

97 lines not on this page · raw message

include/rdma/ib_mad.h +1 -2
index f6ba366051c7..69b838afe2cf 100644
@@ -610,8 +610,7 @@ struct ib_mad_agent {
 	u8			rmpp_version;
 	void			*security;
 	bool			smp_allowed;
-	bool			lsm_nb_reg;
-	struct notifier_block   lsm_nb;
+	struct list_head	mad_agent_sec_list;
 };
 
 /**
Signature · 1 line
2.17.1
author
#2 lore raw

PATCH v2 2/5 LSM: switch to blocking policy update notifiers

In reply to GUO Zihua

From: Janne Karhunen <[email protected]>

[ Upstream commit 42df744c4166af6959eda2df1ee5cde744d4a1c3 ]

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 |  4 ++--
 include/linux/security.h         | 12 ++++++------
 security/security.c              | 23 +++++++++++++----------
 security/selinux/hooks.c         |  2 +-
 security/selinux/selinuxfs.c     |  2 +-
 5 files changed, 23 insertions(+), 20 deletions(-)
drivers/infiniband/core/device.c +2 -2
index 9740614a108d..77e9c0c3c96b 100644
@@ -1208,7 +1208,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;
@@ -1244,7 +1244,7 @@ static void __exit ib_core_cleanup(void)
 	roce_gid_mgmt_cleanup();
 	nldev_exit();
 	rdma_nl_unregister(RDMA_NL_LS);
-	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 +6 -6
index 273877cf47bf..81e795c9c09a 100644
@@ -191,9 +191,9 @@ struct security_mnt_opts {
 	int num_mnt_opts;
 };
 
-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);
 
 static inline void security_init_mnt_opts(struct security_mnt_opts *opts)
 {
@@ -409,17 +409,17 @@ int security_inode_getsecctx(struct inode *inode, void **ctx, u32 *ctxlen);
 struct security_mnt_opts {
 };
 
-static inline int call_lsm_notifier(enum lsm_event event, void *data)
+static inline int call_blocking_lsm_notifier(enum lsm_event event, void *data)
 {
 	return 0;
 }
 
-static inline int register_lsm_notifier(struct notifier_block *nb)
+static inline int register_blocking_lsm_notifier(struct notifier_block *nb)
 {
 	return 0;
 }
 
-static inline  int unregister_lsm_notifier(struct notifier_block *nb)
+static inline  int unregister_blocking_lsm_notifier(struct notifier_block *nb)
 {
 	return 0;
 }
security/security.c +13 -10
index fc1410550b79..4d20a15f18b4 100644
@@ -38,7 +38,7 @@
 #define SECURITY_NAME_MAX	10
 
 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);
 
 char *lsm_names;
 /* Boot-time LSM user choice */
@@ -180,23 +180,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);
 
 /*
  * Hook list operation macros.
security/selinux/hooks.c +1 -1
index 41e24df986eb..77f2147709b1 100644
@@ -199,7 +199,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 60b3f16bb5c7..4f72d0998580 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
author
#3 lore raw

PATCH v2 3/5 ima: use the lsm policy update notifier

In reply to GUO Zihua

From: Janne Karhunen <[email protected]>

[ Upstream commit b169424551930a9325f700f502802f4d515194e5 ]

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

Depends on commit f242064c5df3 ("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 | 118 ++++++++++++++++++++++------
 3 files changed, 105 insertions(+), 23 deletions(-)
security/integrity/ima/ima.h +2 -0
index e2916b115b93..dc564ed9a790 100644
@@ -154,6 +154,8 @@ int ima_measurements_show(struct seq_file *m, void *v);
 unsigned long ima_get_binary_runtime_size(void);
 int ima_init_template(void);
 void ima_init_template_list(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 2d31921fbda4..55681872c6ce 100644
@@ -41,6 +41,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();
@@ -553,6 +557,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 +95 -23

163 lines not on this page · raw message

Signature · 1 line
2.17.1
author
#4 lore raw

PATCH v2 4/5 ima: Evaluate error in init_ima()

In reply to GUO Zihua

From: Roberto Sassu <[email protected]>

[ Upstream commit e144d6b265415ddbdc54b3f17f4f95133effa5a8 ]

Evaluate error in init_ima() before register_blocking_lsm_notifier() and return if not zero.

Fixes b16942455193 ("ima: use the lsm policy update notifier")
---
 security/integrity/ima/ima_main.c | 3 +++
 1 file changed, 3 insertions(+)
security/integrity/ima/ima_main.c +3 -0
index 55681872c6ce..fcaaf2c2ba4b 100644
@@ -557,6 +557,9 @@ static int __init init_ima(void)
 		error = ima_init();
 	}
 
+	if (error)
+		return error;
+
 	error = register_blocking_lsm_notifier(&ima_lsm_policy_notifier);
 	if (error)
 		pr_warn("Couldn't register LSM notifier, error %d\n", error);
Signature · 1 line
2.17.1
author
#5 lore raw

PATCH v2 5/5 ima: Handle -ESTALE returned by ima_filter_rule_match()

In reply to GUO Zihua

[ Upstream commit c7423dbdbc9ecef7fff5239d144cad4b9887f4de ]

IMA relies on the blocking LSM policy notifier callback to update the LSM based IMA policy rules.

When SELinux update its policies, IMA would be notified and starts updating all its lsm rules one-by-one. During this time, -ESTALE would be returned by ima_filter_rule_match() if it is called with a LSM rule that has not yet been updated. In ima_match_rules(), -ESTALE is not handled, and the LSM rule is considered a match, causing extra files to be measured by IMA.

Fix it by re-initializing a temporary rule if -ESTALE is returned by ima_filter_rule_match(). The origin rule in the rule list would be updated by the LSM policy notifier callback.

Fixes b16942455193 ("ima: use the lsm policy update notifier")
---
 security/integrity/ima/ima_policy.c | 37 ++++++++++++++++++++++-------
 1 file changed, 29 insertions(+), 8 deletions(-)
security/integrity/ima/ima_policy.c +29 -8
index 5a15524bca4c..9d500a06c710 100644
@@ -375,6 +375,9 @@ static bool ima_match_rules(struct ima_rule_entry *rule, struct inode *inode,
 			    enum ima_hooks func, int mask)
 {
 	int i;
+	bool result = false;
+	struct ima_rule_entry *lsm_rule = rule;
+	bool rule_reinitialized = false;
 
 	if ((rule->flags & IMA_FUNC) &&
 	    (rule->func != func && func != POST_SETATTR))
@@ -413,35 +416,53 @@ static bool ima_match_rules(struct ima_rule_entry *rule, struct inode *inode,
 		int rc = 0;
 		u32 osid;
 
-		if (!rule->lsm[i].rule)
+		if (!lsm_rule->lsm[i].rule)
 			continue;
 
+retry:
 		switch (i) {
 		case LSM_OBJ_USER:
 		case LSM_OBJ_ROLE:
 		case LSM_OBJ_TYPE:
 			security_inode_getsecid(inode, &osid);
 			rc = security_filter_rule_match(osid,
-							rule->lsm[i].type,
+							lsm_rule->lsm[i].type,
 							Audit_equal,
-							rule->lsm[i].rule,
+							lsm_rule->lsm[i].rule,
 							NULL);
 			break;
 		case LSM_SUBJ_USER:
 		case LSM_SUBJ_ROLE:
 		case LSM_SUBJ_TYPE:
 			rc = security_filter_rule_match(secid,
-							rule->lsm[i].type,
+							lsm_rule->lsm[i].type,
 							Audit_equal,
-							rule->lsm[i].rule,
+							lsm_rule->lsm[i].rule,
 							NULL);
 		default:
 			break;
 		}
-		if (!rc)
-			return false;
+
+		if (rc == -ESTALE && !rule_reinitialized) {
+			lsm_rule = ima_lsm_copy_rule(rule);
+			if (lsm_rule) {
+				rule_reinitialized = true;
+				goto retry;
+			}
+		}
+		if (!rc) {
+			result = false;
+			goto out;
+		}
+	}
+	result = true;
+
+out:
+	if (rule_reinitialized) {
+		ima_lsm_free_rule(lsm_rule);
+		kfree(lsm_rule);
 	}
-	return true;
+	return result;
 }
 
 /*
Signature · 1 line
2.17.1
maintainer
#6 lore raw
In reply to GUO Zihua

Hi Scott,

Quoted 5 lines · GUO Zihua

This series backports patches in order to resolve the issue discussed here:
https://lore.kernel.org/selinux/[email protected]/

This required backporting the non-blocking LSM policy update mechanism prerequisite patches. As well as bugfixes that follows.

For ease of reading, the above sentence should end with a colon and be followed with the list of commits.

Quoted 9 lines

66f67414c1f ("IB/core: Don't register each MAD agent for LSM notifier") is merged as the prerequisite of 42df744c4166 ("LSM: switch to blocking policy update notifiers").

e144d6b26541 ("ima: Evaluate error in
init_ima()") is merged as a follow up bugfix for b16942455193 ("ima:
use the lsm policy update notifier").

Quoted 5 lines

483ec26eed42 ("ima: ima/lsm policy
rule loading logic bug fixes") and 9ff8a616dfab ("ima: Have the LSM free its audit rule") is also followup bugfixes. The former would change the behavior of rule loading without fixing any criticial bug so I don't think it's necessary, while the latter has already been merged.

Prior to the non-blocking LSM notifier was upstreamed, a custom IMA policy with LSM based policy rules could not be loaded until the LSM policy had been initialized. Commit 483ec26eed42 ("ima: ima/lsm policy rule loading logic bug fixes") reverts the unintended change in behavior.

Quoted 8 lines

I've tested the patches against said issue and can confirm that the issue is fixed.

This is a re-send of the original patchset as the original patchset might have a faulty cover letter. The original patchset could be found here:
https://patchwork.kernel.org/project/linux-integrity/list/?series=709367

In addition to a "faulty cover letter", included in this version additional patches are being backported.

Probably better to drop this comment or to include a "Link:" to the mailing list discussion, as described in Documentation/process/submitting-patches.

Quoted 31 lines

Change log:
v2: Fixed build issue and backport bugfix commits for backported
patches.

Daniel Jurgens (1):
IB/core: Don't register each MAD agent for LSM notifier

GUO Zihua (1):
ima: Handle -ESTALE returned by ima_filter_rule_match()

Janne Karhunen (2):
LSM: switch to blocking policy update notifiers
ima: use the lsm policy update notifier

Roberto Sassu (1):
ima: Evaluate error in init_ima()

 drivers/infiniband/core/core_priv.h |   5 +
 drivers/infiniband/core/device.c    |   5 +-
 drivers/infiniband/core/security.c  |  51 +++++-----
 include/linux/security.h            |  12 +--
 include/rdma/ib_mad.h               |   3 +-
 security/integrity/ima/ima.h        |   2 +
 security/integrity/ima/ima_main.c   |  11 ++
 security/integrity/ima/ima_policy.c | 151 ++++++++++++++++++++++------
 security/security.c                 |  23 +++--
 security/selinux/hooks.c            |   2 +-
 security/selinux/selinuxfs.c        |   2 +-
 11 files changed, 193 insertions(+), 74 deletions(-)
Signature · 3 lines
thanks,

Mimi
#7 lore raw
Quoted 11 lines · Mimi Zohar

Hi Scott,

Quoted 5 lines · GUO Zihua

This series backports patches in order to resolve the issue discussed here:
https://lore.kernel.org/selinux/[email protected]/

This required backporting the non-blocking LSM policy update mechanism prerequisite patches. As well as bugfixes that follows.

For ease of reading, the above sentence should end with a colon and be followed with the list of commits.

Sure thing. Will make the change.

Quoted 15 lines
Quoted 9 lines

66f67414c1f ("IB/core: Don't register each MAD agent for LSM notifier") is merged as the prerequisite of 42df744c4166 ("LSM: switch to blocking policy update notifiers").

e144d6b26541 ("ima: Evaluate error in
init_ima()") is merged as a follow up bugfix for b16942455193 ("ima:
use the lsm policy update notifier").

Quoted 5 lines

483ec26eed42 ("ima: ima/lsm policy
rule loading logic bug fixes") and 9ff8a616dfab ("ima: Have the LSM free its audit rule") is also followup bugfixes. The former would change the behavior of rule loading without fixing any criticial bug so I don't think it's necessary, while the latter has already been merged.

Quoted 5 lines

Prior to the non-blocking LSM notifier was upstreamed, a custom IMA policy with LSM based policy rules could not be loaded until the LSM policy had been initialized. Commit 483ec26eed42 ("ima: ima/lsm policy rule loading logic bug fixes") reverts the unintended change in behavior.

Right. I thought that commit was an "feature addition" rather than a fix. I'll include that in the backport.

Quoted 10 lines
Quoted 9 lines

I've tested the patches against said issue and can confirm that the issue is fixed.

This is a re-send of the original patchset as the original patchset might have a faulty cover letter. The original patchset could be found here:
https://patchwork.kernel.org/project/linux-integrity/list/?series=709367

Quoted 6 lines

In addition to a "faulty cover letter", included in this version additional patches are being backported.

Probably better to drop this comment or to include a "Link:" to the mailing list discussion, as described in Documentation/process/submitting-patches.

Got it.

Quoted 32 lines
Quoted 32 lines

Change log:
v2: Fixed build issue and backport bugfix commits for backported
patches.

Daniel Jurgens (1):
IB/core: Don't register each MAD agent for LSM notifier

GUO Zihua (1):
ima: Handle -ESTALE returned by ima_filter_rule_match()

Janne Karhunen (2):
LSM: switch to blocking policy update notifiers
ima: use the lsm policy update notifier

Roberto Sassu (1):
ima: Evaluate error in init_ima()

 drivers/infiniband/core/core_priv.h |   5 +
 drivers/infiniband/core/device.c    |   5 +-
 drivers/infiniband/core/security.c  |  51 +++++-----
 include/linux/security.h            |  12 +--
 include/rdma/ib_mad.h               |   3 +-
 security/integrity/ima/ima.h        |   2 +
 security/integrity/ima/ima_main.c   |  11 ++
 security/integrity/ima/ima_policy.c | 151 ++++++++++++++++++++++------
 security/security.c                 |  23 +++--
 security/selinux/hooks.c            |   2 +-
 security/selinux/selinuxfs.c        |   2 +-
 11 files changed, 193 insertions(+), 74 deletions(-)
Signature · 2 lines
Best
GUO Zihua