Skip to content

Commit

Permalink
ima: use atomic bit operations to protect policy update interface
Browse files Browse the repository at this point in the history
The current implementation uses an atomic counter to provide exclusive
access to the sysfs 'policy' entry to update the IMA policy. While it is
highly unlikely, the usage of a counter might potentially allow another
process to overflow the counter, open the interface and insert additional
rules into the policy being loaded.

This patch replaces using an atomic counter with atomic bit operations
which is more reliable and a widely used method to provide exclusive access.

As bit operation keep the interface locked after successful update, it makes
it unnecessary to verify if the default policy was set or not during parsing
and interface closing. This patch also removes that code.

Changes in v3:
* move audit log message to ima_relead_policy() to report successful and
  unsuccessful result
* unnecessary comment removed

Changes in v2:
* keep interface locked after successful policy load as in original design
* remove sysfs entry as in original design

Signed-off-by: Dmitry Kasatkin <d.kasatkin@samsung.com>
Signed-off-by: Mimi Zohar <zohar@linux.vnet.ibm.com>
  • Loading branch information
Dmitry Kasatkin authored and Mimi Zohar committed Oct 12, 2014
1 parent 7178784 commit 0716abb
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 28 deletions.
23 changes: 16 additions & 7 deletions security/integrity/ima/ima_fs.c
Original file line number Diff line number Diff line change
Expand Up @@ -288,7 +288,12 @@ static struct dentry *runtime_measurements_count;
static struct dentry *violations;
static struct dentry *ima_policy;

static atomic_t policy_opencount = ATOMIC_INIT(1);
enum ima_fs_flags {
IMA_FS_BUSY,
};

static unsigned long ima_fs_flags;

/*
* ima_open_policy: sequentialize access to the policy file
*/
Expand All @@ -297,9 +302,9 @@ static int ima_open_policy(struct inode *inode, struct file *filp)
/* No point in being allowed to open it if you aren't going to write */
if (!(filp->f_flags & O_WRONLY))
return -EACCES;
if (atomic_dec_and_test(&policy_opencount))
return 0;
return -EBUSY;
if (test_and_set_bit(IMA_FS_BUSY, &ima_fs_flags))
return -EBUSY;
return 0;
}

/*
Expand All @@ -311,12 +316,16 @@ static int ima_open_policy(struct inode *inode, struct file *filp)
*/
static int ima_release_policy(struct inode *inode, struct file *file)
{
pr_info("IMA: policy update %s\n",
valid_policy ? "completed" : "failed");
const char *cause = valid_policy ? "completed" : "failed";

pr_info("IMA: policy update %s\n", cause);
integrity_audit_msg(AUDIT_INTEGRITY_STATUS, NULL, NULL,
"policy_update", cause, !valid_policy, 0);

if (!valid_policy) {
ima_delete_rules();
valid_policy = 1;
atomic_set(&policy_opencount, 1);
clear_bit(IMA_FS_BUSY, &ima_fs_flags);
return 0;
}
ima_update_policy();
Expand Down
23 changes: 2 additions & 21 deletions security/integrity/ima/ima_policy.c
Original file line number Diff line number Diff line change
Expand Up @@ -356,19 +356,8 @@ void __init ima_init_policy(void)
*/
void ima_update_policy(void)
{
static const char op[] = "policy_update";
const char *cause = "already-exists";
int result = 1;
int audit_info = 0;

if (ima_rules == &ima_default_rules) {
ima_rules = &ima_policy_rules;
ima_update_policy_flag();
cause = "complete";
result = 0;
}
integrity_audit_msg(AUDIT_INTEGRITY_STATUS, NULL,
NULL, op, cause, result, audit_info);
ima_rules = &ima_policy_rules;
ima_update_policy_flag();
}

enum {
Expand Down Expand Up @@ -686,14 +675,6 @@ ssize_t ima_parse_add_rule(char *rule)
ssize_t result, len;
int audit_info = 0;

/* Prevent installed policy from changing */
if (ima_rules != &ima_default_rules) {
integrity_audit_msg(AUDIT_INTEGRITY_STATUS, NULL,
NULL, op, "already-exists",
-EACCES, audit_info);
return -EACCES;
}

p = strsep(&rule, "\n");
len = strlen(p) + 1;
p += strspn(p, " \t");
Expand Down

0 comments on commit 0716abb

Please sign in to comment.