1 // SPDX-License-Identifier: GPL-2.0+
2 /*
3  * Copyright (C) 2019 Microsoft Corporation
4  *
5  * Author: Lakshmi Ramasubramanian (nramas@linux.microsoft.com)
6  *
7  * File: ima_queue_keys.c
8  *       Enables deferred processing of keys
9  */
10 
11 #include <linux/workqueue.h>
12 #include <keys/asymmetric-type.h>
13 #include "ima.h"
14 
15 /*
16  * Flag to indicate whether a key can be processed
17  * right away or should be queued for processing later.
18  */
19 static bool ima_process_keys;
20 
21 /*
22  * To synchronize access to the list of keys that need to be measured
23  */
24 static DEFINE_MUTEX(ima_keys_lock);
25 static LIST_HEAD(ima_keys);
26 
27 /*
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.
31  */
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;
36 
37 /*
38  * This worker function frees keys that may still be
39  * queued up in case custom IMA policy was not loaded.
40  */
41 static void ima_keys_handler(struct work_struct *work)
42 {
43 	timer_expired = true;
44 	ima_process_queued_keys();
45 }
46 
47 /*
48  * This function sets up a worker to free queued keys in case
49  * custom IMA policy was never loaded.
50  */
51 void ima_init_key_queue(void)
52 {
53 	schedule_delayed_work(&ima_keys_delayed_work,
54 			      msecs_to_jiffies(ima_key_queue_timeout));
55 }
56 
57 static void ima_free_key_entry(struct ima_key_entry *entry)
58 {
59 	if (entry) {
60 		kfree(entry->payload);
61 		kfree(entry->keyring_name);
62 		kfree(entry);
63 	}
64 }
65 
66 static struct ima_key_entry *ima_alloc_key_entry(struct key *keyring,
67 						 const void *payload,
68 						 size_t payload_len)
69 {
70 	int rc = 0;
71 	struct ima_key_entry *entry;
72 
73 	entry = kzalloc(sizeof(*entry), GFP_KERNEL);
74 	if (entry) {
75 		entry->payload = kmemdup(payload, payload_len, GFP_KERNEL);
76 		entry->keyring_name = kstrdup(keyring->description,
77 					      GFP_KERNEL);
78 		entry->payload_len = payload_len;
79 	}
80 
81 	if ((entry == NULL) || (entry->payload == NULL) ||
82 	    (entry->keyring_name == NULL)) {
83 		rc = -ENOMEM;
84 		goto out;
85 	}
86 
87 	INIT_LIST_HEAD(&entry->list);
88 
89 out:
90 	if (rc) {
91 		ima_free_key_entry(entry);
92 		entry = NULL;
93 	}
94 
95 	return entry;
96 }
97 
98 bool ima_queue_key(struct key *keyring, const void *payload,
99 		   size_t payload_len)
100 {
101 	bool queued = false;
102 	struct ima_key_entry *entry;
103 
104 	entry = ima_alloc_key_entry(keyring, payload, payload_len);
105 	if (!entry)
106 		return false;
107 
108 	mutex_lock(&ima_keys_lock);
109 	if (!ima_process_keys) {
110 		list_add_tail(&entry->list, &ima_keys);
111 		queued = true;
112 	}
113 	mutex_unlock(&ima_keys_lock);
114 
115 	if (!queued)
116 		ima_free_key_entry(entry);
117 
118 	return queued;
119 }
120 
121 /*
122  * ima_process_queued_keys() - process keys queued for measurement
123  *
124  * This function sets ima_process_keys to true and processes queued keys.
125  * From here on keys will be processed right away (not queued).
126  */
127 void ima_process_queued_keys(void)
128 {
129 	struct ima_key_entry *entry, *tmp;
130 	bool process = false;
131 
132 	if (ima_process_keys)
133 		return;
134 
135 	/*
136 	 * Since ima_process_keys is set to true, any new key will be
137 	 * processed immediately and not be queued to ima_keys list.
138 	 * First one setting the ima_process_keys flag to true will
139 	 * process the queued keys.
140 	 */
141 	mutex_lock(&ima_keys_lock);
142 	if (!ima_process_keys) {
143 		ima_process_keys = true;
144 		process = true;
145 	}
146 	mutex_unlock(&ima_keys_lock);
147 
148 	if (!process)
149 		return;
150 
151 	if (!timer_expired)
152 		cancel_delayed_work_sync(&ima_keys_delayed_work);
153 
154 	list_for_each_entry_safe(entry, tmp, &ima_keys, list) {
155 		if (!timer_expired)
156 			process_buffer_measurement(entry->payload,
157 						   entry->payload_len,
158 						   entry->keyring_name,
159 						   KEY_CHECK, 0,
160 						   entry->keyring_name);
161 		list_del(&entry->list);
162 		ima_free_key_entry(entry);
163 	}
164 }
165 
166 inline bool ima_should_queue_key(void)
167 {
168 	return !ima_process_keys;
169 }
170