Commit 6ccd0456 authored by Eric Paris's avatar Eric Paris Committed by James Morris

ima: handle multiple rules per write

Currently IMA will only accept one rule per write().  This patch allows IMA to
accept writes which contain multiple rules but only processes one rule per
write.  \n is used as the delimiter between rules.  IMA will return a short
write indicating that it only accepted up to the first \n.

This allows simple userspace utilities like cat to be used to load an IMA
policy instead of needing a special userspace utility that understood 'one
write per rule'
Signed-off-by: default avatarEric Paris <eparis@redhat.com>
Acked-by: default avatarMimi Zohar <zohar@us.ibm.com>
Signed-off-by: default avatarJames Morris <jmorris@namei.org>
parent a2000050
...@@ -135,7 +135,7 @@ enum ima_hooks { FILE_CHECK = 1, FILE_MMAP, BPRM_CHECK }; ...@@ -135,7 +135,7 @@ enum ima_hooks { FILE_CHECK = 1, FILE_MMAP, BPRM_CHECK };
int ima_match_policy(struct inode *inode, enum ima_hooks func, int mask); int ima_match_policy(struct inode *inode, enum ima_hooks func, int mask);
void ima_init_policy(void); void ima_init_policy(void);
void ima_update_policy(void); void ima_update_policy(void);
int ima_parse_add_rule(char *); ssize_t ima_parse_add_rule(char *);
void ima_delete_rules(void); void ima_delete_rules(void);
/* LSM based policy rules require audit */ /* LSM based policy rules require audit */
......
...@@ -243,32 +243,34 @@ static const struct file_operations ima_ascii_measurements_ops = { ...@@ -243,32 +243,34 @@ static const struct file_operations ima_ascii_measurements_ops = {
static ssize_t ima_write_policy(struct file *file, const char __user *buf, static ssize_t ima_write_policy(struct file *file, const char __user *buf,
size_t datalen, loff_t *ppos) size_t datalen, loff_t *ppos)
{ {
char *data; char *data = NULL;
int rc; ssize_t result;
if (datalen >= PAGE_SIZE) if (datalen >= PAGE_SIZE)
return -ENOMEM; datalen = PAGE_SIZE - 1;
if (*ppos != 0) {
/* No partial writes. */ /* No partial writes. */
return -EINVAL; result = -EINVAL;
} if (*ppos != 0)
goto out;
result = -ENOMEM;
data = kmalloc(datalen + 1, GFP_KERNEL); data = kmalloc(datalen + 1, GFP_KERNEL);
if (!data) if (!data)
return -ENOMEM; goto out;
if (copy_from_user(data, buf, datalen)) {
kfree(data);
return -EFAULT;
}
*(data + datalen) = '\0'; *(data + datalen) = '\0';
rc = ima_parse_add_rule(data);
if (rc < 0) {
datalen = -EINVAL;
valid_policy = 0;
}
result = -EFAULT;
if (copy_from_user(data, buf, datalen))
goto out;
result = ima_parse_add_rule(data);
out:
if (result < 0)
valid_policy = 0;
kfree(data); kfree(data);
return datalen; return result;
} }
static struct dentry *ima_dir; static struct dentry *ima_dir;
......
...@@ -261,7 +261,7 @@ static int ima_parse_rule(char *rule, struct ima_measure_rule_entry *entry) ...@@ -261,7 +261,7 @@ static int ima_parse_rule(char *rule, struct ima_measure_rule_entry *entry)
ab = audit_log_start(NULL, GFP_KERNEL, AUDIT_INTEGRITY_RULE); ab = audit_log_start(NULL, GFP_KERNEL, AUDIT_INTEGRITY_RULE);
entry->action = -1; entry->action = -1;
while ((p = strsep(&rule, " \n")) != NULL) { while ((p = strsep(&rule, " ")) != NULL) {
substring_t args[MAX_OPT_ARGS]; substring_t args[MAX_OPT_ARGS];
int token; int token;
unsigned long lnum; unsigned long lnum;
...@@ -269,7 +269,7 @@ static int ima_parse_rule(char *rule, struct ima_measure_rule_entry *entry) ...@@ -269,7 +269,7 @@ static int ima_parse_rule(char *rule, struct ima_measure_rule_entry *entry)
if (result < 0) if (result < 0)
break; break;
if (!*p) if (!*p)
continue; break;
token = match_token(p, policy_tokens, args); token = match_token(p, policy_tokens, args);
switch (token) { switch (token) {
case Opt_measure: case Opt_measure:
...@@ -373,7 +373,7 @@ static int ima_parse_rule(char *rule, struct ima_measure_rule_entry *entry) ...@@ -373,7 +373,7 @@ static int ima_parse_rule(char *rule, struct ima_measure_rule_entry *entry)
if (entry->action == UNKNOWN) if (entry->action == UNKNOWN)
result = -EINVAL; result = -EINVAL;
audit_log_format(ab, "res=%d", !result ? 0 : 1); audit_log_format(ab, "res=%d", !!result);
audit_log_end(ab); audit_log_end(ab);
return result; return result;
} }
...@@ -383,13 +383,14 @@ static int ima_parse_rule(char *rule, struct ima_measure_rule_entry *entry) ...@@ -383,13 +383,14 @@ static int ima_parse_rule(char *rule, struct ima_measure_rule_entry *entry)
* @rule - ima measurement policy rule * @rule - ima measurement policy rule
* *
* Uses a mutex to protect the policy list from multiple concurrent writers. * Uses a mutex to protect the policy list from multiple concurrent writers.
* Returns 0 on success, an error code on failure. * Returns the length of the rule parsed, an error code on failure
*/ */
int ima_parse_add_rule(char *rule) ssize_t ima_parse_add_rule(char *rule)
{ {
const char *op = "update_policy"; const char *op = "update_policy";
char *p;
struct ima_measure_rule_entry *entry; struct ima_measure_rule_entry *entry;
int result = 0; ssize_t result, len;
int audit_info = 0; int audit_info = 0;
/* Prevent installed policy from changing */ /* Prevent installed policy from changing */
...@@ -409,8 +410,11 @@ int ima_parse_add_rule(char *rule) ...@@ -409,8 +410,11 @@ int ima_parse_add_rule(char *rule)
INIT_LIST_HEAD(&entry->list); INIT_LIST_HEAD(&entry->list);
result = ima_parse_rule(rule, entry); p = strsep(&rule, "\n");
len = strlen(p) + 1;
result = ima_parse_rule(p, entry);
if (!result) { if (!result) {
result = len;
mutex_lock(&ima_measure_mutex); mutex_lock(&ima_measure_mutex);
list_add_tail(&entry->list, &measure_policy_rules); list_add_tail(&entry->list, &measure_policy_rules);
mutex_unlock(&ima_measure_mutex); mutex_unlock(&ima_measure_mutex);
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment