ima: measure userspace policy writes before parsing

When a signed policy is not mandatory, userspace can write IMA policy rules
directly to the securityfs policy file:

echo -e "measure func=BPRM_CHECK mask=MAY_EXEC\n" \
        "audit func=BPRM_CHECK mask=MAY_EXEC\n" \
     > /sys/kernel/security/ima/policy

or by cat'ing the entire IMA custom policy file:

cat ima-policy-file > /sys/kernel/security/ima/policy

Because these rules originate from userspace and cross the userspace/kernel
trust boundary, measure the raw write buffer before parsing, regardless of
whether the new policy will be accepted or not. This can be caught when
'measure func=POLICY_CHECK' is enabled (e.g., ima_policy=tcb). The
measurement template is forced to ima-buf.
This follows the "measure & load" paradigm, exposing potential bugs in
the policy code and detecting attempts to corrupt IMA. It also completes
the POLICY_CHECK hook, which already measures partial policy load by file.

To verify the template data hash value, convert the buffer policy data
to binary:
grep "ima_policy_written" \
	/sys/kernel/security/integrity/ima/ascii_runtime_measurements | \
	tail -1 | cut -d' ' -f 6 | xxd -r -p | sha256sum

Suggested-by: Roberto Sassu <roberto.sassu@huawei.com>
Signed-off-by: Enrico Bravi <enrico.bravi@polito.it>
Signed-off-by: Mimi Zohar <zohar@linux.ibm.com>
This commit is contained in:
Enrico Bravi
2026-07-13 08:49:04 -04:00
committed by Mimi Zohar
parent 12ca6fae79
commit 1da739feb3
4 changed files with 30 additions and 0 deletions
+1
View File
@@ -470,6 +470,7 @@ void *ima_policy_next(struct seq_file *m, void *v, loff_t *pos);
void ima_policy_stop(struct seq_file *m, void *v);
int ima_policy_show(struct seq_file *m, void *v);
void ima_measure_loaded_policy(void);
int ima_measure_raw_policy(const char *buf, size_t buf_len);
/* Appraise integrity measurements */
#define IMA_APPRAISE_ENFORCE 0x01
+1
View File
@@ -598,6 +598,7 @@ static ssize_t ima_write_policy(struct file *file, const char __user *buf,
1, 0);
result = -EACCES;
} else {
ima_measure_raw_policy(data, datalen);
result = ima_parse_add_rule(data);
}
mutex_unlock(&ima_write_mutex);
+22
View File
@@ -1221,6 +1221,28 @@ int ima_measure_critical_data(const char *event_label,
}
EXPORT_SYMBOL_GPL(ima_measure_critical_data);
/**
* ima_measure_raw_policy - Measure the raw policy write buffer
* @buf: pointer to the buffer containing the raw policy data
* @buf_len: size of the buffer
*
* Measure the raw policy buffer sent to the IMA policy securityfs file. The
* buffer is written from userspace, and the measurement is performed before
* parsing it. This measurement includes any data written on the policy file
* such as malformed policy rules and comments.
*
* Return 0 on success, a negative value otherwise.
*/
int ima_measure_raw_policy(const char *buf, size_t buf_len)
{
if (!buf || !buf_len)
return -EINVAL;
return process_buffer_measurement(&nop_mnt_idmap, NULL, buf, buf_len,
"ima_policy_written", POLICY_CHECK,
0, NULL, false, NULL, 0);
}
#ifdef CONFIG_INTEGRITY_ASYMMETRIC_KEYS
/**
+6
View File
@@ -542,6 +542,8 @@ static bool ima_match_rule_data(struct ima_rule_entry *rule,
opt_list = rule->label;
break;
case POLICY_CHECK:
return true;
default:
return false;
}
@@ -588,6 +590,10 @@ static bool ima_match_rules(struct ima_rule_entry *rule,
return false;
switch (func) {
case POLICY_CHECK:
if (inode)
break;
fallthrough;
case KEY_CHECK:
case CRITICAL_DATA:
return ((rule->func == func) &&