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/workqueue.h>
12 #include <keys/asymmetric-type.h>
16 * Flag to indicate whether a key can be processed
17 * right away or should be queued for processing later.
19 static bool ima_process_keys;
22 * To synchronize access to the list of keys that need to be measured
24 static DEFINE_MUTEX(ima_keys_lock);
25 static LIST_HEAD(ima_keys);
28 * If custom IMA policy is not loaded then keys queued up
29 * for measurement should be freed. This worker is used
30 * for handling this scenario.
32 static long ima_key_queue_timeout = 300000; /* 5 Minutes */
33 static void ima_keys_handler(struct work_struct *work);
34 static DECLARE_DELAYED_WORK(ima_keys_delayed_work, ima_keys_handler);
35 static bool timer_expired;
38 * This worker function frees keys that may still be
39 * queued up in case custom IMA policy was not loaded.
41 static void ima_keys_handler(struct work_struct *work)
44 ima_process_queued_keys();
48 * This function sets up a worker to free queued keys in case
49 * custom IMA policy was never loaded.
51 void ima_init_key_queue(void)
53 schedule_delayed_work(&ima_keys_delayed_work,
54 msecs_to_jiffies(ima_key_queue_timeout));
57 static void ima_free_key_entry(struct ima_key_entry *entry)
60 kfree(entry->payload);
61 kfree(entry->keyring_name);
66 static struct ima_key_entry *ima_alloc_key_entry(struct key *keyring,
71 const char *audit_cause = "ENOMEM";
72 struct ima_key_entry *entry;
74 entry = kzalloc(sizeof(*entry), GFP_KERNEL);
76 entry->payload = kmemdup(payload, payload_len, GFP_KERNEL);
77 entry->keyring_name = kstrdup(keyring->description,
79 entry->payload_len = payload_len;
82 if ((entry == NULL) || (entry->payload == NULL) ||
83 (entry->keyring_name == NULL)) {
88 INIT_LIST_HEAD(&entry->list);
92 integrity_audit_message(AUDIT_INTEGRITY_PCR, NULL,
94 func_measure_str(KEY_CHECK),
95 audit_cause, rc, 0, rc);
96 ima_free_key_entry(entry);
103 bool ima_queue_key(struct key *keyring, const void *payload,
107 struct ima_key_entry *entry;
109 entry = ima_alloc_key_entry(keyring, payload, payload_len);
113 mutex_lock(&ima_keys_lock);
114 if (!ima_process_keys) {
115 list_add_tail(&entry->list, &ima_keys);
118 mutex_unlock(&ima_keys_lock);
121 ima_free_key_entry(entry);
127 * ima_process_queued_keys() - process keys queued for measurement
129 * This function sets ima_process_keys to true and processes queued keys.
130 * From here on keys will be processed right away (not queued).
132 void ima_process_queued_keys(void)
134 struct ima_key_entry *entry, *tmp;
135 bool process = false;
137 if (ima_process_keys)
141 * Since ima_process_keys is set to true, any new key will be
142 * processed immediately and not be queued to ima_keys list.
143 * First one setting the ima_process_keys flag to true will
144 * process the queued keys.
146 mutex_lock(&ima_keys_lock);
147 if (!ima_process_keys) {
148 ima_process_keys = true;
151 mutex_unlock(&ima_keys_lock);
157 cancel_delayed_work_sync(&ima_keys_delayed_work);
159 list_for_each_entry_safe(entry, tmp, &ima_keys, list) {
161 process_buffer_measurement(NULL, entry->payload,
165 entry->keyring_name);
166 list_del(&entry->list);
167 ima_free_key_entry(entry);
171 inline bool ima_should_queue_key(void)
173 return !ima_process_keys;