Skip to contentSkip to conversation

ima: Fix IMA mishandling of LSM based rule during

PATCH Superseded GUO Zihua 2 participants 5 messages Jan 3, 2023 – Jan 4, 2023

Subsystems (5)

v2 of 7 Superseded Latest is v7 →

lore
Share & tools
lore

GUO Zihua

author
#0 lore raw

v2 cover ima: Fix IMA mishandling of LSM based rule during

Backports the following two patches to fix the issue of IMA mishandling LSM based rule during LSM policy update, causing a file to match an unexpected rule.

Some changes were made to these patches, which was stated in the commit message of corresponding patch.

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

Janne Karhunen (1):
ima: use the lsm policy update notifier

 security/integrity/ima/ima.h        |   2 +
 security/integrity/ima/ima_main.c   |   8 ++
 security/integrity/ima/ima_policy.c | 153 +++++++++++++++++++++++-----
 3 files changed, 137 insertions(+), 26 deletions(-)
Signature · 1 line
2.17.1
author
#1 lore raw

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

From: Janne Karhunen <[email protected]>

[ Upstream commit b169424551930a9325f700f502802f4d515194e5 ]

This patch is backported to resolve the issue of IMA ignoreing LSM part of an LSM based rule. As the LSM notifier chain was an atomic notifier chain, we'll not be able to call synchronize_rcu() within our notifier handling function. Instead, we call the call_rcu() function to resolve the freeing issue. To do that, we would needs to include a rcu_head member in our rule, as well as wrap the call to ima_lsm_free_rule() into a rcu_callback_t type callback function.

Original patch message is as follows:

commit b169424551930a9325f700f502802f4d515194e5

Author: Janne Karhunen <[email protected]>
Date:   Fri Jun 14 15:20:15 2019 +0300

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 | 123 +++++++++++++++++++++++-----
 3 files changed, 113 insertions(+), 20 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..f461b3e2de00 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_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 +103 -20

162 lines not on this page · raw message

Signature · 1 line
2.17.1
author
#4 lore raw

PATCH v2 2/2 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")
Cc [email protected] # 4.19.y
---
 security/integrity/ima/ima_policy.c | 34 ++++++++++++++++++++++-------
 1 file changed, 26 insertions(+), 8 deletions(-)
security/integrity/ima/ima_policy.c +26 -8
index 086ff58f0669..4ed98eaf6cb8 100644
@@ -386,6 +386,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))
@@ -424,35 +427,50 @@ 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;
+		}
 	}
-	return true;
+	result = true;
+
+out:
+	if (rule_reinitialized)
+		ima_lsm_free_rule(lsm_rule);
+	return result;
 }
 
 /*
Signature · 1 line
2.17.1
maintainer
#2 lore raw

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

In reply to GUO Zihua
Quoted 21 lines · GUO Zihua

From: Janne Karhunen <[email protected]>

[ Upstream commit b169424551930a9325f700f502802f4d515194e5 ]

This patch is backported to resolve the issue of IMA ignoreing LSM part of an LSM based rule. As the LSM notifier chain was an atomic notifier chain, we'll not be able to call synchronize_rcu() within our notifier handling function. Instead, we call the call_rcu() function to resolve the freeing issue. To do that, we would needs to include a rcu_head member in our rule, as well as wrap the call to ima_lsm_free_rule() into a rcu_callback_t type callback function.

Original patch message is as follows:

commit b169424551930a9325f700f502802f4d515194e5

Author: Janne Karhunen <[email protected]>
Date:   Fri Jun 14 15:20:15 2019 +0300

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

Quoted 8 lines

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

There was quite a bit of discussion regarding converting the atomic notifier to blocking, but this backport doesn't make that change.

Refer to
https://lore.kernel.org/linux-integrity/CAHC9VhS=GsEVUmxtiV64o8G6i2nJpkzxzpyTADgN-vhV8pzZbg@mail.gmail.com/

Mimi

#3 lore raw

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

Quoted 28 lines · Mimi Zohar
Quoted 27 lines · GUO Zihua

From: Janne Karhunen <[email protected]>

[ Upstream commit b169424551930a9325f700f502802f4d515194e5 ]

This patch is backported to resolve the issue of IMA ignoreing LSM part of an LSM based rule. As the LSM notifier chain was an atomic notifier chain, we'll not be able to call synchronize_rcu() within our notifier handling function. Instead, we call the call_rcu() function to resolve the freeing issue. To do that, we would needs to include a rcu_head member in our rule, as well as wrap the call to ima_lsm_free_rule() into a rcu_callback_t type callback function.

Original patch message is as follows:

commit b169424551930a9325f700f502802f4d515194e5

Author: Janne Karhunen <[email protected]>
Date:   Fri Jun 14 15:20:15 2019 +0300

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")

Quoted 8 lines
Quoted 2 lines

There was quite a bit of discussion regarding converting the atomic notifier to blocking, but this backport doesn't make that change.

Refer to
https://lore.kernel.org/linux-integrity/CAHC9VhS=GsEVUmxtiV64o8G6i2nJpkzxzpyTADgN-vhV8pzZbg@mail.gmail.com/

Well it seems that the bug mentioned here is still valid on 4.19.y. Which is worrying. I'll try backporting the blocking notifier change as well.

Quoted 3 lines

Mimi

Signature · 2 lines
Best
GUO Zihua