Re: [PATCH] IMA: pre-allocate keyrings string

From: Mimi Zohar
Date: Thu Jan 16 2020 - 07:57:00 EST


Hi Laskhmi,

On Wed, 2020-01-15 at 19:15 -0800, Lakshmi Ramasubramanian wrote:
> ima_match_keyring() is called while holding rcu read lock.
> Since this function executes in atmomic context, it should
> not call any function that can sleep (such as kstrdup()).

Good catch!

> This patch pre-allocates a buffer to hold the keyrings
> string read from the IMA policy and uses that to check
> the given keyring in ima_match_keyring().

(Reminder: this patch description line length is a bit short.
ÂAccording to ÂDocumentation/process/submitting-patches.rst, the patch
description line length should be line wrapped at 75 columns.)

>
> Signed-off-by: Lakshmi Ramasubramanian <nramas@xxxxxxxxxxxxxxxxxxx>
> Fixes: e9085e0ad38a ("IMA: Add support to limit measuring keys")
> ---


> @@ -1120,8 +1117,17 @@ static int ima_parse_rule(char *rule, struct ima_rule_entry *entry)
> result = -EINVAL;
> break;
> }
> +
> + ima_keyrings = kstrdup(args[0].from, GFP_KERNEL);
> + if (!ima_keyrings) {
> + result = -ENOMEM;
> + break;
> + }


This would work for a single "key" measurement rule, but not for
multiple rules, where the last "keyrings" string is shorter than the
previous ones.ÂÂFor example, in addition to the builtin trusted
keyrings, another rule could measure a keyring owned by a user.

measure func=KEY_CHECK template=ima-buf keyrings=.ima|.builtin_trusted_keys
measure func=KEY_CHECK uid=1000 template=ima-buf keyrings=_foo

Mimi

> entry->keyrings = kstrdup(args[0].from, GFP_KERNEL);
> if (!entry->keyrings) {
> + kfree(ima_keyrings);
> + ima_keyrings = NULL;
> result = -ENOMEM;
> break;
> }