Re: [PATCH v4 08/10] IMA: Defined functions to queue and dequeue keys for measurement

From: Lakshmi Ramasubramanian
Date: Wed Nov 06 2019 - 18:52:14 EST



On 11/6/2019 2:44 PM, Mimi Zohar wrote:

+int ima_queue_or_process_key_for_measurement(struct key *keyring,
+ struct key *key)
+{
+ int rc = 0;
+ struct ima_measure_key_entry *entry = NULL;
+ const struct public_key *pk;
+
+ if (key->type != &key_type_asymmetric)
+ return 0;
+
+ mutex_lock(&ima_measure_keys_mutex);


Unless the key is being queued, there's no reason to take the lock.

Reason the lock is taken even in the case the key is not queued is to avoid the following race condition:

=> ima_init() sets ima_initialized flag and calls the dequeue function

=> If IMA hook checks ima_initialized flag outside the lock and sees the flag is not set, it will call the queue function.

=> If the above two steps race, the key could get added to the queue after ima_init() has processed the queued keys.

That's the reason I named the function called by the IMA hook to ima_queue_or_process_key_for_measurement().

But I can make the following change:

=> IMA hook checks the flag.
=> If it is set, process key immediately
=> If the flag is not set, call ima_queue_or_process_key_for_measurement()

ima_queue_or_process_key_for_measurement() will do the following:

=> With the lock held check ima_initialized flag
=> If true release the lock and call process_buffer_measurement()
=> If false, queue the key and then release the lock

Would that be acceptable?

Measuring the key should be done in ima_post_key_create_or_update()
unless, it is being deferred. ÂPlease update the function name to
reflect this.

Just wanted to confirm:
Rename ima_post_key_create_or_update() to a more appropriate name?

Another reason for doing all key related operations in ima_queue_or_process_key_for_measurement() is to isolate key related code in a separate C file and build it if and only if the CONFIG dependencies are met.

With respect to loading custom policy, I will take a look at how to handle that case. Thanks for pointing that out.

Mimi

thanks,
-lakshmi