ima: check ima-policy's path
Hi, I was reading the function ima_write_policy in ima/ima_fs.c when I find the issue:
Quoted 15 lines
static
ssize_tima_write_policy(struct file *file, const char__user*buf,
size_tdatalen,loff_t*ppos){ ... if (data[0] == '/') { result = ima_read_policy(data); } else if (ima_appraise & IMA_APPRAISE_POLICY) { pr_err("signed policy file (specified as an absolute pathname) required\n"); integrity_audit_msg(AUDIT_INTEGRITY_STATUS, NULL, NULL, "policy_update", "signed policy required", 1, 0); ... return result; }
For the absolute path written by the user, we only check the prefix "/". Actually, we can echo an illegal path to the /sys/kernel/security/ima/policy, e.g. "/\rtest: ddddddddddddddddddd" to inject some logs into dmesg.
Then ima_read_policy is called to return error:
Quoted 11 lines
static
ssize_tima_read_policy(char *path){ ... rc = kernel_read_file_from_path(path, 0, &data, INT_MAX, NULL, READING_POLICY); if (rc < 0) { pr_err("Unable to open file: %s (%d)", path, rc); return rc; } ... }
In ima_read_policy, the illegal path would be logged into dmesg like this:
Quoted 5 lines
...
test: ddddddddddddddddddd (-2)/
test: ddddddddddddddddddd (-2)/
test: ddddddddddddddddddd (-2)/
test: ddddddddddddddddddd (-2)/
I suggest that we should check the path in ima_write_policy to make sure it's a valid one, at least literally.
thanks,
Tianxing
Mimi Zohar
maintainerQuoted 48 lines · Tianxing Zhang
Hi, I was reading the function
ima_write_policyinima/ima_fs.cwhen I find the issue:Quoted 1 line
static
ssize_tima_write_policy(struct file *file, const char
__user*buf,Quoted 1 line
size_tdatalen,loff_t*ppos)
Quoted 7 lines
{ ... if (data[0] == '/') { result = ima_read_policy(data); } else if (ima_appraise & IMA_APPRAISE_POLICY) { pr_err("signed policy file (specified as anabsolute pathname) required\n");
Quoted 1 line
integrity_audit_msg(AUDIT_INTEGRITY_STATUS, NULL,
NULL,
Quoted 1 line
"
policy_update","signed policy required",
Quoted 4 lines
1, 0); ... return result; }For the absolute path written by the user, we only check the prefix "/". Actually, we can echo an illegal path to the
/sys/kernel/security/ima/policy, e.g. "/\rtest: ddddddddddddddddddd" to inject some logs into dmesg.Then
ima_read_policyis called to return error:Quoted 4 lines
static
ssize_tima_read_policy(char *path)
{
...
rc = kernel_read_file_from_path(path, 0, &data,INT_MAX,NULL,
Quoted 1 line
READING_POLICY);Quoted 6 lines
if (rc < 0) { pr_err("Unable to open file: %s (%d)", path, rc); return rc; } ... }In
ima_read_policy, the illegal path would be logged into dmesg like this:
Quoted 8 lines
Quoted 5 lines
...
test: ddddddddddddddddddd (-2)/
test: ddddddddddddddddddd (-2)/
test: ddddddddddddddddddd (-2)/
test: ddddddddddddddddddd (-2)/I suggest that we should check the path in
ima_write_policyto make sure it's a valid one, at least literally.
Sure. In the case that the path isn't valid, perhaps instead of removing the message entirely, limit the number of messages emitted using pr_err_once().
thanks,
Mimi