Skip to content

Commit a756024

Browse files
Roberto SassuMimi Zohar
authored andcommitted
ima: added ima_policy_flag variable
This patch introduces the new variable 'ima_policy_flag', whose bits are set depending on the action of the current policy rules. Only the flags IMA_MEASURE, IMA_APPRAISE and IMA_AUDIT are set. The new variable will be used to improve performance by skipping the unnecessary execution of IMA code if the policy does not contain rules with the above actions. Changes in v6 (Roberto Sassu) * do not check 'ima_initialized' before calling ima_update_policy_flag() in ima_update_policy() (suggested by Dmitry) * calling ima_update_policy_flag() moved to init_ima to co-locate with ima_initialized (Dmitry) * add/revise comments (Mimi) Changes in v5 (Roberto Sassu) * reset IMA_APPRAISE flag in 'ima_policy_flag' if 'ima_appraise' is set to zero (reported by Dmitry) * update 'ima_policy_flag' only if IMA initialization is successful (suggested by Mimi and Dmitry) * check 'ima_policy_flag' instead of 'ima_initialized' (suggested by Mimi and Dmitry) Signed-off-by: Roberto Sassu <[email protected]> Signed-off-by: Dmitry Kasatkin <[email protected]> Signed-off-by: Mimi Zohar <[email protected]>
1 parent be39ffc commit a756024

File tree

4 files changed

+34
-5
lines changed

4 files changed

+34
-5
lines changed

security/integrity/ima/ima.h

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,9 @@ enum tpm_pcrs { TPM_PCR0 = 0, TPM_PCR8 = 8 };
4343
#define IMA_TEMPLATE_IMA_NAME "ima"
4444
#define IMA_TEMPLATE_IMA_FMT "d|n"
4545

46+
/* current content of the policy */
47+
extern int ima_policy_flag;
48+
4649
/* set during initialization */
4750
extern int ima_initialized;
4851
extern int ima_used_chip;
@@ -153,6 +156,7 @@ int ima_match_policy(struct inode *inode, enum ima_hooks func, int mask,
153156
int flags);
154157
void ima_init_policy(void);
155158
void ima_update_policy(void);
159+
void ima_update_policy_flag(void);
156160
ssize_t ima_parse_add_rule(char *);
157161
void ima_delete_rules(void);
158162

security/integrity/ima/ima_appraise.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -318,7 +318,7 @@ void ima_inode_post_setattr(struct dentry *dentry)
318318
struct integrity_iint_cache *iint;
319319
int must_appraise, rc;
320320

321-
if (!ima_initialized || !ima_appraise || !S_ISREG(inode->i_mode)
321+
if (!(ima_policy_flag & IMA_APPRAISE) || !S_ISREG(inode->i_mode)
322322
|| !inode->i_op->removexattr)
323323
return;
324324

@@ -356,7 +356,7 @@ static void ima_reset_appraise_flags(struct inode *inode, int digsig)
356356
{
357357
struct integrity_iint_cache *iint;
358358

359-
if (!ima_initialized || !ima_appraise || !S_ISREG(inode->i_mode))
359+
if (!(ima_policy_flag & IMA_APPRAISE) || !S_ISREG(inode->i_mode))
360360
return;
361361

362362
iint = integrity_iint_find(inode);

security/integrity/ima/ima_main.c

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -85,7 +85,7 @@ static void ima_rdwr_violation_check(struct file *file)
8585
char *pathbuf = NULL;
8686
const char *pathname;
8787

88-
if (!S_ISREG(inode->i_mode) || !ima_initialized)
88+
if (!S_ISREG(inode->i_mode) || !(ima_policy_flag & IMA_MEASURE))
8989
return;
9090

9191
if (mode & FMODE_WRITE) {
@@ -168,7 +168,7 @@ static int process_measurement(struct file *file, int mask, int function,
168168
struct evm_ima_xattr_data *xattr_value = NULL, **xattr_ptr = NULL;
169169
int xattr_len = 0;
170170

171-
if (!ima_initialized || !S_ISREG(inode->i_mode))
171+
if (!ima_policy_flag || !S_ISREG(inode->i_mode))
172172
return 0;
173173

174174
/* Return an IMA_MEASURE, IMA_APPRAISE, IMA_AUDIT action
@@ -334,8 +334,10 @@ static int __init init_ima(void)
334334

335335
hash_setup(CONFIG_IMA_DEFAULT_HASH);
336336
error = ima_init();
337-
if (!error)
337+
if (!error) {
338338
ima_initialized = 1;
339+
ima_update_policy_flag();
340+
}
339341
return error;
340342
}
341343

security/integrity/ima/ima_policy.c

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,8 @@
3535
#define DONT_APPRAISE 0x0008
3636
#define AUDIT 0x0040
3737

38+
int ima_policy_flag;
39+
3840
#define MAX_LSM_RULES 6
3941
enum lsm_rule_types { LSM_OBJ_USER, LSM_OBJ_ROLE, LSM_OBJ_TYPE,
4042
LSM_SUBJ_USER, LSM_SUBJ_ROLE, LSM_SUBJ_TYPE
@@ -295,6 +297,26 @@ int ima_match_policy(struct inode *inode, enum ima_hooks func, int mask,
295297
return action;
296298
}
297299

300+
/*
301+
* Initialize the ima_policy_flag variable based on the currently
302+
* loaded policy. Based on this flag, the decision to short circuit
303+
* out of a function or not call the function in the first place
304+
* can be made earlier.
305+
*/
306+
void ima_update_policy_flag(void)
307+
{
308+
struct ima_rule_entry *entry;
309+
310+
ima_policy_flag = 0;
311+
list_for_each_entry(entry, ima_rules, list) {
312+
if (entry->action & IMA_DO_MASK)
313+
ima_policy_flag |= entry->action;
314+
}
315+
316+
if (!ima_appraise)
317+
ima_policy_flag &= ~IMA_APPRAISE;
318+
}
319+
298320
/**
299321
* ima_init_policy - initialize the default measure rules.
300322
*
@@ -341,6 +363,7 @@ void ima_update_policy(void)
341363

342364
if (ima_rules == &ima_default_rules) {
343365
ima_rules = &ima_policy_rules;
366+
ima_update_policy_flag();
344367
cause = "complete";
345368
result = 0;
346369
}

0 commit comments

Comments
 (0)