1 // SPDX-License-Identifier: GPL-2.0+
3 * Copyright (C) 2019 Microsoft Corporation
5 * Author: Lakshmi Ramasubramanian (nramas@linux.microsoft.com)
7 * File: ima_queue_keys.c
8 * Enables deferred processing of keys
11 #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
13 #include <linux/workqueue.h>
14 #include <keys/asymmetric-type.h>
18 * Flag to indicate whether a key can be processed
19 * right away or should be queued for processing later.
21 static bool ima_process_keys;
24 * To synchronize access to the list of keys that need to be measured
26 static DEFINE_MUTEX(ima_keys_lock);
27 static LIST_HEAD(ima_keys);
30 * If custom IMA policy is not loaded then keys queued up
31 * for measurement should be freed. This worker is used
32 * for handling this scenario.
34 static long ima_key_queue_timeout = 300000; /* 5 Minutes */
35 static void ima_keys_handler(struct work_struct *work);
36 static DECLARE_DELAYED_WORK(ima_keys_delayed_work, ima_keys_handler);
37 static bool timer_expired;
40 * This worker function frees keys that may still be
41 * queued up in case custom IMA policy was not loaded.
43 static void ima_keys_handler(struct work_struct *work)
46 ima_process_queued_keys();
50 * This function sets up a worker to free queued keys in case
51 * custom IMA policy was never loaded.
53 void ima_init_key_queue(void)
55 schedule_delayed_work(&ima_keys_delayed_work,
56 msecs_to_jiffies(ima_key_queue_timeout));
59 static void ima_free_key_entry(struct ima_key_entry *entry)
62 kfree(entry->payload);
63 kfree(entry->keyring_name);
68 static struct ima_key_entry *ima_alloc_key_entry(struct key *keyring,
73 struct ima_key_entry *entry;
75 entry = kzalloc(sizeof(*entry), GFP_KERNEL);
77 entry->payload = kmemdup(payload, payload_len, GFP_KERNEL);
78 entry->keyring_name = kstrdup(keyring->description,
80 entry->payload_len = payload_len;
83 if ((entry == NULL) || (entry->payload == NULL) ||
84 (entry->keyring_name == NULL)) {
89 INIT_LIST_HEAD(&entry->list);
93 ima_free_key_entry(entry);
100 bool ima_queue_key(struct key *keyring, const void *payload,
104 struct ima_key_entry *entry;
106 entry = ima_alloc_key_entry(keyring, payload, payload_len);
110 mutex_lock(&ima_keys_lock);
111 if (!ima_process_keys) {
112 list_add_tail(&entry->list, &ima_keys);
115 mutex_unlock(&ima_keys_lock);
118 ima_free_key_entry(entry);
124 * ima_process_queued_keys() - process keys queued for measurement
126 * This function sets ima_process_keys to true and processes queued keys.
127 * From here on keys will be processed right away (not queued).
129 void ima_process_queued_keys(void)
131 struct ima_key_entry *entry, *tmp;
132 bool process = false;
134 if (ima_process_keys)
138 * Since ima_process_keys is set to true, any new key will be
139 * processed immediately and not be queued to ima_keys list.
140 * First one setting the ima_process_keys flag to true will
141 * process the queued keys.
143 mutex_lock(&ima_keys_lock);
144 if (!ima_process_keys) {
145 ima_process_keys = true;
148 mutex_unlock(&ima_keys_lock);
154 cancel_delayed_work_sync(&ima_keys_delayed_work);
156 list_for_each_entry_safe(entry, tmp, &ima_keys, list) {
158 process_buffer_measurement(entry->payload,
162 entry->keyring_name);
163 list_del(&entry->list);
164 ima_free_key_entry(entry);
168 inline bool ima_should_queue_key(void)
170 return !ima_process_keys;