projects/linux·blob master · refs

461 lines · 13.3 KiB

1// SPDX-License-Identifier: GPL-2.0-only
2/*
3 * eCryptfs: Linux filesystem encryption layer
4 *
5 * Copyright (C) 2004-2008 International Business Machines Corp.
6 * Author(s): Michael A. Halcrow <[email protected]>
7 * Tyler Hicks <[email protected]>
8 */
9#include <linux/overflow.h>
10#include <linux/sched.h>
11#include <linux/slab.h>
12#include <linux/user_namespace.h>
13#include <linux/nsproxy.h>
14#include "ecryptfs_kernel.h"
15
16static LIST_HEAD(ecryptfs_msg_ctx_free_list);
17static LIST_HEAD(ecryptfs_msg_ctx_alloc_list);
18static DEFINE_MUTEX(ecryptfs_msg_ctx_lists_mux);
19
20static struct hlist_head *ecryptfs_daemon_hash;
21DEFINE_MUTEX(ecryptfs_daemon_hash_mux);
22static int ecryptfs_hash_bits;
23#define ecryptfs_current_euid_hash(uid) \
24 hash_long((unsigned long)from_kuid(&init_user_ns, current_euid()), ecryptfs_hash_bits)
25
26static u32 ecryptfs_msg_counter;
27static struct ecryptfs_msg_ctx *ecryptfs_msg_ctx_arr;
28
29/**
30 * ecryptfs_acquire_free_msg_ctx
31 * @msg_ctx: The context that was acquired from the free list
32 *
33 * Acquires a context element from the free list and locks the mutex
34 * on the context. Sets the msg_ctx task to current. Returns zero on
35 * success; non-zero on error or upon failure to acquire a free
36 * context element. Must be called with ecryptfs_msg_ctx_lists_mux
37 * held.
38 */
39static int ecryptfs_acquire_free_msg_ctx(struct ecryptfs_msg_ctx **msg_ctx)
40{
41 struct list_head *p;
42 int rc;
43
44 if (list_empty(&ecryptfs_msg_ctx_free_list)) {
45 printk(KERN_WARNING "%s: The eCryptfs free "
46 "context list is empty. It may be helpful to "
47 "specify the ecryptfs_message_buf_len "
48 "parameter to be greater than the current "
49 "value of [%d]\n", __func__, ecryptfs_message_buf_len);
50 rc = -ENOMEM;
51 goto out;
52 }
53 list_for_each(p, &ecryptfs_msg_ctx_free_list) {
54 *msg_ctx = list_entry(p, struct ecryptfs_msg_ctx, node);
55 if (mutex_trylock(&(*msg_ctx)->mux)) {
56 (*msg_ctx)->task = current;
57 rc = 0;
58 goto out;
59 }
60 }
61 rc = -ENOMEM;
62out:
63 return rc;
64}
65
66/**
67 * ecryptfs_msg_ctx_free_to_alloc
68 * @msg_ctx: The context to move from the free list to the alloc list
69 *
70 * Must be called with ecryptfs_msg_ctx_lists_mux held.
71 */
72static void ecryptfs_msg_ctx_free_to_alloc(struct ecryptfs_msg_ctx *msg_ctx)
73{
74 list_move(&msg_ctx->node, &ecryptfs_msg_ctx_alloc_list);
75 msg_ctx->state = ECRYPTFS_MSG_CTX_STATE_PENDING;
76 msg_ctx->counter = ++ecryptfs_msg_counter;
77}
78
79/**
80 * ecryptfs_msg_ctx_alloc_to_free
81 * @msg_ctx: The context to move from the alloc list to the free list
82 *
83 * Must be called with ecryptfs_msg_ctx_lists_mux held.
84 */
85void ecryptfs_msg_ctx_alloc_to_free(struct ecryptfs_msg_ctx *msg_ctx)
86{
87 list_move(&(msg_ctx->node), &ecryptfs_msg_ctx_free_list);
88 kfree(msg_ctx->msg);
89 msg_ctx->msg = NULL;
90 msg_ctx->state = ECRYPTFS_MSG_CTX_STATE_FREE;
91}
92
93/**
94 * ecryptfs_find_daemon_by_euid
95 * @daemon: If return value is zero, points to the desired daemon pointer
96 *
97 * Must be called with ecryptfs_daemon_hash_mux held.
98 *
99 * Search the hash list for the current effective user id.
100 *
101 * Returns zero if the user id exists in the list; non-zero otherwise.
102 */
103int ecryptfs_find_daemon_by_euid(struct ecryptfs_daemon **daemon)
104{
105 int rc;
106
107 hlist_for_each_entry(*daemon,
108 &ecryptfs_daemon_hash[ecryptfs_current_euid_hash()],
109 euid_chain) {
110 if (uid_eq((*daemon)->file->f_cred->euid, current_euid())) {
111 rc = 0;
112 goto out;
113 }
114 }
115 rc = -EINVAL;
116out:
117 return rc;
118}
119
120/**
121 * ecryptfs_spawn_daemon - Create and initialize a new daemon struct
122 * @daemon: Pointer to set to newly allocated daemon struct
123 * @file: File used when opening /dev/ecryptfs
124 *
125 * Must be called ceremoniously while in possession of
126 * ecryptfs_sacred_daemon_hash_mux
127 *
128 * Returns zero on success; non-zero otherwise
129 */
130int
131ecryptfs_spawn_daemon(struct ecryptfs_daemon **daemon, struct file *file)
132{
133 int rc = 0;
134
135 (*daemon) = kzalloc_obj(**daemon);
136 if (!(*daemon)) {
137 rc = -ENOMEM;
138 goto out;
139 }
140 (*daemon)->file = file;
141 mutex_init(&(*daemon)->mux);
142 INIT_LIST_HEAD(&(*daemon)->msg_ctx_out_queue);
143 init_waitqueue_head(&(*daemon)->wait);
144 (*daemon)->num_queued_msg_ctx = 0;
145 hlist_add_head(&(*daemon)->euid_chain,
146 &ecryptfs_daemon_hash[ecryptfs_current_euid_hash()]);
147out:
148 return rc;
149}
150
151/*
152 * ecryptfs_exorcise_daemon - Destroy the daemon struct
153 *
154 * Must be called ceremoniously while in possession of
155 * ecryptfs_daemon_hash_mux and the daemon's own mux.
156 */
157int ecryptfs_exorcise_daemon(struct ecryptfs_daemon *daemon)
158{
159 struct ecryptfs_msg_ctx *msg_ctx, *msg_ctx_tmp;
160 int rc = 0;
161
162 mutex_lock(&daemon->mux);
163 if ((daemon->flags & ECRYPTFS_DAEMON_IN_READ)
164 || (daemon->flags & ECRYPTFS_DAEMON_IN_POLL)) {
165 rc = -EBUSY;
166 mutex_unlock(&daemon->mux);
167 goto out;
168 }
169 mutex_lock(&ecryptfs_msg_ctx_lists_mux);
170 list_for_each_entry_safe(msg_ctx, msg_ctx_tmp,
171 &daemon->msg_ctx_out_queue, daemon_out_list) {
172 list_del(&msg_ctx->daemon_out_list);
173 daemon->num_queued_msg_ctx--;
174 printk(KERN_WARNING "%s: Warning: dropping message that is in "
175 "the out queue of a dying daemon\n", __func__);
176 ecryptfs_msg_ctx_alloc_to_free(msg_ctx);
177 }
178 mutex_unlock(&ecryptfs_msg_ctx_lists_mux);
179 hlist_del(&daemon->euid_chain);
180 mutex_unlock(&daemon->mux);
181 kfree_sensitive(daemon);
182out:
183 return rc;
184}
185
186/**
187 * ecryptfs_process_response
188 * @daemon: eCryptfs daemon object
189 * @msg: The ecryptfs message received; the caller should sanity check
190 * msg->data_len and free the memory
191 * @seq: The sequence number of the message; must match the sequence
192 * number for the existing message context waiting for this
193 * response
194 *
195 * Processes a response message after sending an operation request to
196 * userspace. Some other process is awaiting this response. Before
197 * sending out its first communications, the other process allocated a
198 * msg_ctx from the ecryptfs_msg_ctx_arr at a particular index. The
199 * response message contains this index so that we can copy over the
200 * response message into the msg_ctx that the process holds a
201 * reference to. The other process is going to wake up, check to see
202 * that msg_ctx->state == ECRYPTFS_MSG_CTX_STATE_DONE, and then
203 * proceed to read off and process the response message. Returns zero
204 * upon delivery to desired context element; non-zero upon delivery
205 * failure or error.
206 *
207 * Returns zero on success; non-zero otherwise
208 */
209int ecryptfs_process_response(struct ecryptfs_daemon *daemon,
210 struct ecryptfs_message *msg, u32 seq)
211{
212 struct ecryptfs_msg_ctx *msg_ctx;
213 size_t msg_size;
214 int rc;
215
216 if (msg->index >= ecryptfs_message_buf_len) {
217 rc = -EINVAL;
218 printk(KERN_ERR "%s: Attempt to reference "
219 "context buffer at index [%d]; maximum "
220 "allowable is [%d]\n", __func__, msg->index,
221 (ecryptfs_message_buf_len - 1));
222 goto out;
223 }
224 msg_ctx = &ecryptfs_msg_ctx_arr[msg->index];
225 mutex_lock(&msg_ctx->mux);
226 if (msg_ctx->state != ECRYPTFS_MSG_CTX_STATE_PENDING) {
227 rc = -EINVAL;
228 printk(KERN_WARNING "%s: Desired context element is not "
229 "pending a response\n", __func__);
230 goto unlock;
231 } else if (msg_ctx->counter != seq) {
232 rc = -EINVAL;
233 printk(KERN_WARNING "%s: Invalid message sequence; "
234 "expected [%d]; received [%d]\n", __func__,
235 msg_ctx->counter, seq);
236 goto unlock;
237 }
238 msg_size = struct_size(msg, data, msg->data_len);
239 msg_ctx->msg = kmemdup(msg, msg_size, GFP_KERNEL);
240 if (!msg_ctx->msg) {
241 rc = -ENOMEM;
242 goto unlock;
243 }
244 msg_ctx->state = ECRYPTFS_MSG_CTX_STATE_DONE;
245 wake_up_process(msg_ctx->task);
246 rc = 0;
247unlock:
248 mutex_unlock(&msg_ctx->mux);
249out:
250 return rc;
251}
252
253/**
254 * ecryptfs_send_message_locked
255 * @data: The data to send
256 * @data_len: The length of data
257 * @msg_type: Type of message
258 * @msg_ctx: The message context allocated for the send
259 *
260 * Must be called with ecryptfs_daemon_hash_mux held.
261 *
262 * Returns zero on success; non-zero otherwise
263 */
264static int
265ecryptfs_send_message_locked(char *data, int data_len, u8 msg_type,
266 struct ecryptfs_msg_ctx **msg_ctx)
267{
268 struct ecryptfs_daemon *daemon;
269 int rc;
270
271 rc = ecryptfs_find_daemon_by_euid(&daemon);
272 if (rc) {
273 rc = -ENOTCONN;
274 goto out;
275 }
276 mutex_lock(&ecryptfs_msg_ctx_lists_mux);
277 rc = ecryptfs_acquire_free_msg_ctx(msg_ctx);
278 if (rc) {
279 mutex_unlock(&ecryptfs_msg_ctx_lists_mux);
280 printk(KERN_WARNING "%s: Could not claim a free "
281 "context element\n", __func__);
282 goto out;
283 }
284 ecryptfs_msg_ctx_free_to_alloc(*msg_ctx);
285 mutex_unlock(&(*msg_ctx)->mux);
286 mutex_unlock(&ecryptfs_msg_ctx_lists_mux);
287 rc = ecryptfs_send_miscdev(data, data_len, *msg_ctx, msg_type, 0,
288 daemon);
289 if (rc) {
290 printk(KERN_ERR "%s: Error attempting to send message to "
291 "userspace daemon; rc = [%d]\n", __func__, rc);
292 mutex_lock(&ecryptfs_msg_ctx_lists_mux);
293 mutex_lock(&(*msg_ctx)->mux);
294 ecryptfs_msg_ctx_alloc_to_free(*msg_ctx);
295 mutex_unlock(&(*msg_ctx)->mux);
296 mutex_unlock(&ecryptfs_msg_ctx_lists_mux);
297 *msg_ctx = NULL;
298 }
299out:
300 return rc;
301}
302
303/**
304 * ecryptfs_send_message
305 * @data: The data to send
306 * @data_len: The length of data
307 * @msg_ctx: The message context allocated for the send
308 *
309 * Grabs ecryptfs_daemon_hash_mux.
310 *
311 * Returns zero on success; non-zero otherwise
312 */
313int ecryptfs_send_message(char *data, int data_len,
314 struct ecryptfs_msg_ctx **msg_ctx)
315{
316 int rc;
317
318 mutex_lock(&ecryptfs_daemon_hash_mux);
319 rc = ecryptfs_send_message_locked(data, data_len, ECRYPTFS_MSG_REQUEST,
320 msg_ctx);
321 mutex_unlock(&ecryptfs_daemon_hash_mux);
322 return rc;
323}
324
325/**
326 * ecryptfs_wait_for_response
327 * @msg_ctx: The context that was assigned when sending a message
328 * @msg: The incoming message from userspace; not set if rc != 0
329 *
330 * Sleeps until awaken by ecryptfs_receive_message or until the amount
331 * of time exceeds ecryptfs_message_wait_timeout. If zero is
332 * returned, msg will point to a valid message from userspace; a
333 * non-zero value is returned upon failure to receive a message or an
334 * error occurs. Callee must free @msg on success.
335 */
336int ecryptfs_wait_for_response(struct ecryptfs_msg_ctx *msg_ctx,
337 struct ecryptfs_message **msg)
338{
339 signed long timeout = ecryptfs_message_wait_timeout * HZ;
340 int rc = 0;
341
342sleep:
343 timeout = schedule_timeout_interruptible(timeout);
344 mutex_lock(&ecryptfs_msg_ctx_lists_mux);
345 mutex_lock(&msg_ctx->mux);
346 if (msg_ctx->state != ECRYPTFS_MSG_CTX_STATE_DONE) {
347 if (timeout) {
348 mutex_unlock(&msg_ctx->mux);
349 mutex_unlock(&ecryptfs_msg_ctx_lists_mux);
350 goto sleep;
351 }
352 rc = -ENOMSG;
353 } else {
354 *msg = msg_ctx->msg;
355 msg_ctx->msg = NULL;
356 }
357 ecryptfs_msg_ctx_alloc_to_free(msg_ctx);
358 mutex_unlock(&msg_ctx->mux);
359 mutex_unlock(&ecryptfs_msg_ctx_lists_mux);
360 return rc;
361}
362
363int __init ecryptfs_init_messaging(void)
364{
365 int i;
366 int rc = 0;
367
368 if (ecryptfs_number_of_users > ECRYPTFS_MAX_NUM_USERS) {
369 ecryptfs_number_of_users = ECRYPTFS_MAX_NUM_USERS;
370 printk(KERN_WARNING "%s: Specified number of users is "
371 "too large, defaulting to [%d] users\n", __func__,
372 ecryptfs_number_of_users);
373 }
374 mutex_lock(&ecryptfs_daemon_hash_mux);
375 ecryptfs_hash_bits = 1;
376 while (ecryptfs_number_of_users >> ecryptfs_hash_bits)
377 ecryptfs_hash_bits++;
378 ecryptfs_daemon_hash = kmalloc((sizeof(struct hlist_head)
379 * (1 << ecryptfs_hash_bits)),
380 GFP_KERNEL);
381 if (!ecryptfs_daemon_hash) {
382 rc = -ENOMEM;
383 mutex_unlock(&ecryptfs_daemon_hash_mux);
384 goto out;
385 }
386 for (i = 0; i < (1 << ecryptfs_hash_bits); i++)
387 INIT_HLIST_HEAD(&ecryptfs_daemon_hash[i]);
388 mutex_unlock(&ecryptfs_daemon_hash_mux);
389 ecryptfs_msg_ctx_arr = kmalloc((sizeof(struct ecryptfs_msg_ctx)
390 * ecryptfs_message_buf_len),
391 GFP_KERNEL);
392 if (!ecryptfs_msg_ctx_arr) {
393 kfree(ecryptfs_daemon_hash);
394 rc = -ENOMEM;
395 goto out;
396 }
397 mutex_lock(&ecryptfs_msg_ctx_lists_mux);
398 ecryptfs_msg_counter = 0;
399 for (i = 0; i < ecryptfs_message_buf_len; i++) {
400 INIT_LIST_HEAD(&ecryptfs_msg_ctx_arr[i].node);
401 INIT_LIST_HEAD(&ecryptfs_msg_ctx_arr[i].daemon_out_list);
402 mutex_init(&ecryptfs_msg_ctx_arr[i].mux);
403 mutex_lock(&ecryptfs_msg_ctx_arr[i].mux);
404 ecryptfs_msg_ctx_arr[i].index = i;
405 ecryptfs_msg_ctx_arr[i].state = ECRYPTFS_MSG_CTX_STATE_FREE;
406 ecryptfs_msg_ctx_arr[i].counter = 0;
407 ecryptfs_msg_ctx_arr[i].task = NULL;
408 ecryptfs_msg_ctx_arr[i].msg = NULL;
409 list_add_tail(&ecryptfs_msg_ctx_arr[i].node,
410 &ecryptfs_msg_ctx_free_list);
411 mutex_unlock(&ecryptfs_msg_ctx_arr[i].mux);
412 }
413 mutex_unlock(&ecryptfs_msg_ctx_lists_mux);
414 rc = ecryptfs_init_ecryptfs_miscdev();
415 if (rc)
416 ecryptfs_release_messaging();
417out:
418 return rc;
419}
420
421void ecryptfs_release_messaging(void)
422{
423 if (ecryptfs_msg_ctx_arr) {
424 int i;
425
426 mutex_lock(&ecryptfs_msg_ctx_lists_mux);
427 for (i = 0; i < ecryptfs_message_buf_len; i++) {
428 mutex_lock(&ecryptfs_msg_ctx_arr[i].mux);
429 kfree(ecryptfs_msg_ctx_arr[i].msg);
430 mutex_unlock(&ecryptfs_msg_ctx_arr[i].mux);
431 }
432 kfree(ecryptfs_msg_ctx_arr);
433 mutex_unlock(&ecryptfs_msg_ctx_lists_mux);
434 }
435 if (ecryptfs_daemon_hash) {
436 struct ecryptfs_daemon *daemon;
437 struct hlist_node *n;
438 int i;
439
440 mutex_lock(&ecryptfs_daemon_hash_mux);
441 for (i = 0; i < (1 << ecryptfs_hash_bits); i++) {
442 int rc;
443
444 hlist_for_each_entry_safe(daemon, n,
445 &ecryptfs_daemon_hash[i],
446 euid_chain) {
447 rc = ecryptfs_exorcise_daemon(daemon);
448 if (rc)
449 printk(KERN_ERR "%s: Error whilst "
450 "attempting to destroy daemon; "
451 "rc = [%d]. Dazed and confused, "
452 "but trying to continue.\n",
453 __func__, rc);
454 }
455 }
456 kfree(ecryptfs_daemon_hash);
457 mutex_unlock(&ecryptfs_daemon_hash_mux);
458 }
459 ecryptfs_destroy_ecryptfs_miscdev();
460 return;
461}