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 #include <linux/user_namespace.h>
12 #include <linux/workqueue.h>
13 #include <keys/asymmetric-type.h>
17 * Flag to indicate whether a key can be processed
18 * right away or should be queued for processing later.
20 static bool ima_process_keys;
23 * To synchronize access to the list of keys that need to be measured
25 static DEFINE_MUTEX(ima_keys_lock);
26 static LIST_HEAD(ima_keys);
29 * If custom IMA policy is not loaded then keys queued up
30 * for measurement should be freed. This worker is used
31 * for handling this scenario.
33 static long ima_key_queue_timeout = 300000; /* 5 Minutes */
34 static void ima_keys_handler(struct work_struct *work);
35 static DECLARE_DELAYED_WORK(ima_keys_delayed_work, ima_keys_handler);
36 static bool timer_expired;
39 * This worker function frees keys that may still be
40 * queued up in case custom IMA policy was not loaded.
42 static void ima_keys_handler(struct work_struct *work)
45 ima_process_queued_keys();
49 * This function sets up a worker to free queued keys in case
50 * custom IMA policy was never loaded.
52 void ima_init_key_queue(void)
54 schedule_delayed_work(&ima_keys_delayed_work,
55 msecs_to_jiffies(ima_key_queue_timeout));
58 static void ima_free_key_entry(struct ima_key_entry *entry)
61 kfree(entry->payload);
62 kfree(entry->keyring_name);
67 static struct ima_key_entry *ima_alloc_key_entry(struct key *keyring,
72 const char *audit_cause = "ENOMEM";
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 integrity_audit_message(AUDIT_INTEGRITY_PCR, NULL,
95 func_measure_str(KEY_CHECK),
96 audit_cause, rc, 0, rc);
97 ima_free_key_entry(entry);
104 bool ima_queue_key(struct key *keyring, const void *payload,
108 struct ima_key_entry *entry;
110 entry = ima_alloc_key_entry(keyring, payload, payload_len);
114 mutex_lock(&ima_keys_lock);
115 if (!ima_process_keys) {
116 list_add_tail(&entry->list, &ima_keys);
119 mutex_unlock(&ima_keys_lock);
122 ima_free_key_entry(entry);
128 * ima_process_queued_keys() - process keys queued for measurement
130 * This function sets ima_process_keys to true and processes queued keys.
131 * From here on keys will be processed right away (not queued).
133 void ima_process_queued_keys(void)
135 struct ima_key_entry *entry, *tmp;
136 bool process = false;
138 if (ima_process_keys)
142 * Since ima_process_keys is set to true, any new key will be
143 * processed immediately and not be queued to ima_keys list.
144 * First one setting the ima_process_keys flag to true will
145 * process the queued keys.
147 mutex_lock(&ima_keys_lock);
148 if (!ima_process_keys) {
149 ima_process_keys = true;
152 mutex_unlock(&ima_keys_lock);
158 cancel_delayed_work_sync(&ima_keys_delayed_work);
160 list_for_each_entry_safe(entry, tmp, &ima_keys, list) {
162 process_buffer_measurement(&init_user_ns, NULL,
169 list_del(&entry->list);
170 ima_free_key_entry(entry);
174 inline bool ima_should_queue_key(void)
176 return !ima_process_keys;