ima: Fix IMA mishandling of LSM based rule during
Subsystems (5)
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
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
@@ -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
@@ -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
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.
--- security/integrity/ima/ima_policy.c | 34 ++++++++++++++++++++++------- 1 file changed, 26 insertions(+), 8 deletions(-)
security/integrity/ima/ima_policy.c +26 -8
@@ -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
Mimi Zohar
maintainerRe: [PATCH v2 1/2] ima: use the lsm policy update notifier
In reply to GUO ZihuaQuoted 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 thecall_rcu()function to resolve the freeing issue. To do that, we would needs to include arcu_headmember in our rule, as well as wrap the call toima_lsm_free_rule()into arcu_callback_ttype callback function.Original patch message is as follows:
commit
b169424551930a9325f700f502802f4d515194e5Author: Janne Karhunen <[email protected]> Date: Fri Jun 14 15:20:15 2019 +0300Don'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")Signed-off-by Janne Karhunen <[email protected]>Signed-off-by Mimi Zohar <[email protected]>Cc [email protected] #4.19.ySigned-off-by GUO Zihua <[email protected]>
There was quite a bit of discussion regarding converting the atomic notifier to blocking, but this backport doesn't make that change.
Mimi
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 thecall_rcu()function to resolve the freeing issue. To do that, we would needs to include arcu_headmember in our rule, as well as wrap the call toima_lsm_free_rule()into arcu_callback_ttype callback function.Original patch message is as follows:
commit
b169424551930a9325f700f502802f4d515194e5Author: Janne Karhunen <[email protected]> Date: Fri Jun 14 15:20:15 2019 +0300Don'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")Signed-off-by Janne Karhunen <[email protected]>Signed-off-by Mimi Zohar <[email protected]>
Quoted 8 lines
Quoted 2 lines
Cc [email protected] #4.19.ySigned-off-by GUO Zihua <[email protected]>There was quite a bit of discussion regarding converting the atomic notifier to blocking, but this backport doesn't make that change.
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