ima: reject duplicate fsname= in a policy rule

ima_parse_rule() stores the fsname= value with kstrdup() but, unlike
fsmagic=, fs_subtype= and the other options, does not check whether the
rule already has one. A rule that names fsname= twice overwrites
entry->fsname and leaks the first string.

Reject the duplicate with -EINVAL, as fs_subtype= does. Such a rule used
to be accepted with the last fsname= taking effect; giving the option
twice was never meaningful, and the other options already reject it.

Writing the policy requires CAP_SYS_ADMIN, so the leak is limited to a
few bytes per malformed rule loaded by a privileged user.

Confirmed on an unmodified kernel, without error injection, on x86_64
under QEMU with kmemleak: loading 64 rules with a duplicated fsname=
left 64 unreferenced objects allocated by kstrdup() in ima_parse_rule()
(inlined into ima_parse_add_rule()); the same rules with a single
fsname= left none. With this patch the duplicate rules are rejected and
kmemleak reports nothing; rules with a single fsname= are still accepted.

Fixes: f1b08bbcbdaf ("ima: define a new policy condition based on the filesystem name")
Reported-by: Zijie Zhao <zijie4@illinois.edu>
Signed-off-by: Weidong Wang <weidongw@illinois.edu>
Reviewed-by: Roberto Sassu <roberto.sassu@huawei.com>
[zohar: removed AI tool attribution, changed Cc tag to credit Zijie Zhao
with contribution based on the AI attribution]
Signed-off-by: Mimi Zohar <zohar@linux.ibm.com>
1 file changed