CRED: Wrap task credential accesses in the eCryptFS filesystem
[deliverable/linux.git] / fs / ecryptfs / messaging.c
CommitLineData
88b4a07e
MH
1/**
2 * eCryptfs: Linux filesystem encryption layer
3 *
f66e883e 4 * Copyright (C) 2004-2008 International Business Machines Corp.
88b4a07e
MH
5 * Author(s): Michael A. Halcrow <mhalcrow@us.ibm.com>
6 * Tyler Hicks <tyhicks@ou.edu>
7 *
8 * This program is free software; you can redistribute it and/or
9 * modify it under the terms of the GNU General Public License version
10 * 2 as published by the Free Software Foundation.
11 *
12 * This program is distributed in the hope that it will be useful, but
13 * WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 * General Public License for more details.
16 *
17 * You should have received a copy of the GNU General Public License
18 * along with this program; if not, write to the Free Software
19 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA
20 * 02111-1307, USA.
21 */
e8edc6e0 22#include <linux/sched.h>
6a3fd92e
MH
23#include <linux/user_namespace.h>
24#include <linux/nsproxy.h>
88b4a07e
MH
25#include "ecryptfs_kernel.h"
26
dd2a3b7a
MH
27static LIST_HEAD(ecryptfs_msg_ctx_free_list);
28static LIST_HEAD(ecryptfs_msg_ctx_alloc_list);
29static struct mutex ecryptfs_msg_ctx_lists_mux;
88b4a07e 30
f66e883e
MH
31static struct hlist_head *ecryptfs_daemon_hash;
32struct mutex ecryptfs_daemon_hash_mux;
dd2a3b7a
MH
33static int ecryptfs_hash_buckets;
34#define ecryptfs_uid_hash(uid) \
35 hash_long((unsigned long)uid, ecryptfs_hash_buckets)
88b4a07e 36
f66e883e 37static u32 ecryptfs_msg_counter;
dd2a3b7a 38static struct ecryptfs_msg_ctx *ecryptfs_msg_ctx_arr;
88b4a07e
MH
39
40/**
41 * ecryptfs_acquire_free_msg_ctx
42 * @msg_ctx: The context that was acquired from the free list
43 *
44 * Acquires a context element from the free list and locks the mutex
f66e883e
MH
45 * on the context. Sets the msg_ctx task to current. Returns zero on
46 * success; non-zero on error or upon failure to acquire a free
47 * context element. Must be called with ecryptfs_msg_ctx_lists_mux
48 * held.
88b4a07e
MH
49 */
50static int ecryptfs_acquire_free_msg_ctx(struct ecryptfs_msg_ctx **msg_ctx)
51{
52 struct list_head *p;
53 int rc;
54
55 if (list_empty(&ecryptfs_msg_ctx_free_list)) {
f66e883e
MH
56 printk(KERN_WARNING "%s: The eCryptfs free "
57 "context list is empty. It may be helpful to "
58 "specify the ecryptfs_message_buf_len "
59 "parameter to be greater than the current "
60 "value of [%d]\n", __func__, ecryptfs_message_buf_len);
88b4a07e
MH
61 rc = -ENOMEM;
62 goto out;
63 }
64 list_for_each(p, &ecryptfs_msg_ctx_free_list) {
65 *msg_ctx = list_entry(p, struct ecryptfs_msg_ctx, node);
66 if (mutex_trylock(&(*msg_ctx)->mux)) {
67 (*msg_ctx)->task = current;
68 rc = 0;
69 goto out;
70 }
71 }
72 rc = -ENOMEM;
73out:
74 return rc;
75}
76
77/**
78 * ecryptfs_msg_ctx_free_to_alloc
79 * @msg_ctx: The context to move from the free list to the alloc list
80 *
f66e883e 81 * Must be called with ecryptfs_msg_ctx_lists_mux held.
88b4a07e
MH
82 */
83static void ecryptfs_msg_ctx_free_to_alloc(struct ecryptfs_msg_ctx *msg_ctx)
84{
85 list_move(&msg_ctx->node, &ecryptfs_msg_ctx_alloc_list);
86 msg_ctx->state = ECRYPTFS_MSG_CTX_STATE_PENDING;
87 msg_ctx->counter = ++ecryptfs_msg_counter;
88}
89
90/**
91 * ecryptfs_msg_ctx_alloc_to_free
92 * @msg_ctx: The context to move from the alloc list to the free list
93 *
f66e883e 94 * Must be called with ecryptfs_msg_ctx_lists_mux held.
88b4a07e 95 */
f66e883e 96void ecryptfs_msg_ctx_alloc_to_free(struct ecryptfs_msg_ctx *msg_ctx)
88b4a07e
MH
97{
98 list_move(&(msg_ctx->node), &ecryptfs_msg_ctx_free_list);
99 if (msg_ctx->msg)
100 kfree(msg_ctx->msg);
f66e883e 101 msg_ctx->msg = NULL;
88b4a07e
MH
102 msg_ctx->state = ECRYPTFS_MSG_CTX_STATE_FREE;
103}
104
105/**
f66e883e
MH
106 * ecryptfs_find_daemon_by_euid
107 * @euid: The effective user id which maps to the desired daemon id
6a3fd92e 108 * @user_ns: The namespace in which @euid applies
f66e883e 109 * @daemon: If return value is zero, points to the desired daemon pointer
88b4a07e 110 *
f66e883e
MH
111 * Must be called with ecryptfs_daemon_hash_mux held.
112 *
113 * Search the hash list for the given user id.
114 *
115 * Returns zero if the user id exists in the list; non-zero otherwise.
88b4a07e 116 */
6a3fd92e
MH
117int ecryptfs_find_daemon_by_euid(struct ecryptfs_daemon **daemon, uid_t euid,
118 struct user_namespace *user_ns)
88b4a07e
MH
119{
120 struct hlist_node *elem;
121 int rc;
122
f66e883e
MH
123 hlist_for_each_entry(*daemon, elem,
124 &ecryptfs_daemon_hash[ecryptfs_uid_hash(euid)],
125 euid_chain) {
6a3fd92e 126 if ((*daemon)->euid == euid && (*daemon)->user_ns == user_ns) {
88b4a07e
MH
127 rc = 0;
128 goto out;
129 }
130 }
131 rc = -EINVAL;
132out:
133 return rc;
134}
135
f66e883e 136static int
624ae528
TH
137ecryptfs_send_message_locked(char *data, int data_len, u8 msg_type,
138 struct ecryptfs_msg_ctx **msg_ctx);
f66e883e
MH
139
140/**
141 * ecryptfs_send_raw_message
f66e883e
MH
142 * @msg_type: Message type
143 * @daemon: Daemon struct for recipient of message
144 *
145 * A raw message is one that does not include an ecryptfs_message
146 * struct. It simply has a type.
147 *
148 * Must be called with ecryptfs_daemon_hash_mux held.
149 *
150 * Returns zero on success; non-zero otherwise
151 */
624ae528 152static int ecryptfs_send_raw_message(u8 msg_type,
f66e883e 153 struct ecryptfs_daemon *daemon)
88b4a07e 154{
f66e883e 155 struct ecryptfs_msg_ctx *msg_ctx;
88b4a07e
MH
156 int rc;
157
624ae528
TH
158 rc = ecryptfs_send_message_locked(NULL, 0, msg_type, &msg_ctx);
159 if (rc) {
160 printk(KERN_ERR "%s: Error whilst attempting to send "
161 "message to ecryptfsd; rc = [%d]\n", __func__, rc);
162 goto out;
88b4a07e 163 }
624ae528
TH
164 /* Raw messages are logically context-free (e.g., no
165 * reply is expected), so we set the state of the
166 * ecryptfs_msg_ctx object to indicate that it should
167 * be freed as soon as the message is sent. */
168 mutex_lock(&msg_ctx->mux);
169 msg_ctx->state = ECRYPTFS_MSG_CTX_STATE_NO_REPLY;
170 mutex_unlock(&msg_ctx->mux);
f66e883e
MH
171out:
172 return rc;
173}
174
175/**
176 * ecryptfs_spawn_daemon - Create and initialize a new daemon struct
177 * @daemon: Pointer to set to newly allocated daemon struct
178 * @euid: Effective user id for the daemon
6a3fd92e 179 * @user_ns: The namespace in which @euid applies
f66e883e
MH
180 * @pid: Process id for the daemon
181 *
182 * Must be called ceremoniously while in possession of
183 * ecryptfs_sacred_daemon_hash_mux
184 *
185 * Returns zero on success; non-zero otherwise
186 */
187int
6a3fd92e
MH
188ecryptfs_spawn_daemon(struct ecryptfs_daemon **daemon, uid_t euid,
189 struct user_namespace *user_ns, struct pid *pid)
f66e883e
MH
190{
191 int rc = 0;
192
193 (*daemon) = kzalloc(sizeof(**daemon), GFP_KERNEL);
194 if (!(*daemon)) {
195 rc = -ENOMEM;
196 printk(KERN_ERR "%s: Failed to allocate [%Zd] bytes of "
197 "GFP_KERNEL memory\n", __func__, sizeof(**daemon));
198 goto out;
199 }
200 (*daemon)->euid = euid;
6a3fd92e
MH
201 (*daemon)->user_ns = get_user_ns(user_ns);
202 (*daemon)->pid = get_pid(pid);
f66e883e
MH
203 (*daemon)->task = current;
204 mutex_init(&(*daemon)->mux);
205 INIT_LIST_HEAD(&(*daemon)->msg_ctx_out_queue);
206 init_waitqueue_head(&(*daemon)->wait);
207 (*daemon)->num_queued_msg_ctx = 0;
208 hlist_add_head(&(*daemon)->euid_chain,
209 &ecryptfs_daemon_hash[ecryptfs_uid_hash(euid)]);
210out:
88b4a07e
MH
211 return rc;
212}
213
214/**
215 * ecryptfs_process_helo
f66e883e 216 * @euid: The user ID owner of the message
6a3fd92e 217 * @user_ns: The namespace in which @euid applies
88b4a07e
MH
218 * @pid: The process ID for the userspace program that sent the
219 * message
220 *
f66e883e 221 * Adds the euid and pid values to the daemon euid hash. If an euid
88b4a07e 222 * already has a daemon pid registered, the daemon will be
f66e883e
MH
223 * unregistered before the new daemon is put into the hash list.
224 * Returns zero after adding a new daemon to the hash list;
88b4a07e
MH
225 * non-zero otherwise.
226 */
624ae528
TH
227int ecryptfs_process_helo(uid_t euid, struct user_namespace *user_ns,
228 struct pid *pid)
88b4a07e 229{
f66e883e
MH
230 struct ecryptfs_daemon *new_daemon;
231 struct ecryptfs_daemon *old_daemon;
88b4a07e
MH
232 int rc;
233
f66e883e 234 mutex_lock(&ecryptfs_daemon_hash_mux);
6a3fd92e 235 rc = ecryptfs_find_daemon_by_euid(&old_daemon, euid, user_ns);
f66e883e 236 if (rc != 0) {
88b4a07e 237 printk(KERN_WARNING "Received request from user [%d] "
6a3fd92e
MH
238 "to register daemon [0x%p]; unregistering daemon "
239 "[0x%p]\n", euid, pid, old_daemon->pid);
624ae528 240 rc = ecryptfs_send_raw_message(ECRYPTFS_MSG_QUIT, old_daemon);
88b4a07e
MH
241 if (rc)
242 printk(KERN_WARNING "Failed to send QUIT "
6a3fd92e 243 "message to daemon [0x%p]; rc = [%d]\n",
f66e883e
MH
244 old_daemon->pid, rc);
245 hlist_del(&old_daemon->euid_chain);
246 kfree(old_daemon);
88b4a07e 247 }
6a3fd92e 248 rc = ecryptfs_spawn_daemon(&new_daemon, euid, user_ns, pid);
f66e883e
MH
249 if (rc)
250 printk(KERN_ERR "%s: The gods are displeased with this attempt "
6a3fd92e
MH
251 "to create a new daemon object for euid [%d]; pid "
252 "[0x%p]; rc = [%d]\n", __func__, euid, pid, rc);
f66e883e
MH
253 mutex_unlock(&ecryptfs_daemon_hash_mux);
254 return rc;
255}
256
257/**
258 * ecryptfs_exorcise_daemon - Destroy the daemon struct
259 *
260 * Must be called ceremoniously while in possession of
261 * ecryptfs_daemon_hash_mux and the daemon's own mux.
262 */
263int ecryptfs_exorcise_daemon(struct ecryptfs_daemon *daemon)
264{
265 struct ecryptfs_msg_ctx *msg_ctx, *msg_ctx_tmp;
266 int rc = 0;
267
268 mutex_lock(&daemon->mux);
269 if ((daemon->flags & ECRYPTFS_DAEMON_IN_READ)
270 || (daemon->flags & ECRYPTFS_DAEMON_IN_POLL)) {
271 rc = -EBUSY;
272 printk(KERN_WARNING "%s: Attempt to destroy daemon with pid "
6a3fd92e 273 "[0x%p], but it is in the midst of a read or a poll\n",
f66e883e
MH
274 __func__, daemon->pid);
275 mutex_unlock(&daemon->mux);
276 goto out;
277 }
278 list_for_each_entry_safe(msg_ctx, msg_ctx_tmp,
279 &daemon->msg_ctx_out_queue, daemon_out_list) {
280 list_del(&msg_ctx->daemon_out_list);
281 daemon->num_queued_msg_ctx--;
282 printk(KERN_WARNING "%s: Warning: dropping message that is in "
283 "the out queue of a dying daemon\n", __func__);
284 ecryptfs_msg_ctx_alloc_to_free(msg_ctx);
285 }
286 hlist_del(&daemon->euid_chain);
287 if (daemon->task)
288 wake_up_process(daemon->task);
6a3fd92e
MH
289 if (daemon->pid)
290 put_pid(daemon->pid);
291 if (daemon->user_ns)
292 put_user_ns(daemon->user_ns);
f66e883e
MH
293 mutex_unlock(&daemon->mux);
294 memset(daemon, 0, sizeof(*daemon));
295 kfree(daemon);
296out:
88b4a07e
MH
297 return rc;
298}
299
300/**
301 * ecryptfs_process_quit
f66e883e 302 * @euid: The user ID owner of the message
6a3fd92e 303 * @user_ns: The namespace in which @euid applies
88b4a07e
MH
304 * @pid: The process ID for the userspace program that sent the
305 * message
306 *
f66e883e 307 * Deletes the corresponding daemon for the given euid and pid, if
88b4a07e 308 * it is the registered that is requesting the deletion. Returns zero
f66e883e 309 * after deleting the desired daemon; non-zero otherwise.
88b4a07e 310 */
6a3fd92e
MH
311int ecryptfs_process_quit(uid_t euid, struct user_namespace *user_ns,
312 struct pid *pid)
88b4a07e 313{
f66e883e 314 struct ecryptfs_daemon *daemon;
88b4a07e
MH
315 int rc;
316
f66e883e 317 mutex_lock(&ecryptfs_daemon_hash_mux);
6a3fd92e 318 rc = ecryptfs_find_daemon_by_euid(&daemon, euid, user_ns);
f66e883e 319 if (rc || !daemon) {
88b4a07e 320 rc = -EINVAL;
f66e883e 321 printk(KERN_ERR "Received request from user [%d] to "
6a3fd92e 322 "unregister unrecognized daemon [0x%p]\n", euid, pid);
f66e883e 323 goto out_unlock;
88b4a07e 324 }
f66e883e
MH
325 rc = ecryptfs_exorcise_daemon(daemon);
326out_unlock:
327 mutex_unlock(&ecryptfs_daemon_hash_mux);
88b4a07e
MH
328 return rc;
329}
330
331/**
332 * ecryptfs_process_reponse
333 * @msg: The ecryptfs message received; the caller should sanity check
f66e883e 334 * msg->data_len and free the memory
88b4a07e
MH
335 * @pid: The process ID of the userspace application that sent the
336 * message
f66e883e
MH
337 * @seq: The sequence number of the message; must match the sequence
338 * number for the existing message context waiting for this
339 * response
340 *
341 * Processes a response message after sending an operation request to
342 * userspace. Some other process is awaiting this response. Before
343 * sending out its first communications, the other process allocated a
344 * msg_ctx from the ecryptfs_msg_ctx_arr at a particular index. The
345 * response message contains this index so that we can copy over the
346 * response message into the msg_ctx that the process holds a
347 * reference to. The other process is going to wake up, check to see
348 * that msg_ctx->state == ECRYPTFS_MSG_CTX_STATE_DONE, and then
349 * proceed to read off and process the response message. Returns zero
350 * upon delivery to desired context element; non-zero upon delivery
351 * failure or error.
88b4a07e 352 *
f66e883e 353 * Returns zero on success; non-zero otherwise
88b4a07e 354 */
f66e883e 355int ecryptfs_process_response(struct ecryptfs_message *msg, uid_t euid,
6a3fd92e
MH
356 struct user_namespace *user_ns, struct pid *pid,
357 u32 seq)
88b4a07e 358{
f66e883e 359 struct ecryptfs_daemon *daemon;
88b4a07e 360 struct ecryptfs_msg_ctx *msg_ctx;
f66e883e 361 size_t msg_size;
6a3fd92e
MH
362 struct nsproxy *nsproxy;
363 struct user_namespace *current_user_ns;
4eea0353 364 uid_t ctx_euid;
88b4a07e
MH
365 int rc;
366
367 if (msg->index >= ecryptfs_message_buf_len) {
368 rc = -EINVAL;
f66e883e
MH
369 printk(KERN_ERR "%s: Attempt to reference "
370 "context buffer at index [%d]; maximum "
371 "allowable is [%d]\n", __func__, msg->index,
372 (ecryptfs_message_buf_len - 1));
88b4a07e
MH
373 goto out;
374 }
375 msg_ctx = &ecryptfs_msg_ctx_arr[msg->index];
376 mutex_lock(&msg_ctx->mux);
f66e883e 377 mutex_lock(&ecryptfs_daemon_hash_mux);
6a3fd92e
MH
378 rcu_read_lock();
379 nsproxy = task_nsproxy(msg_ctx->task);
380 if (nsproxy == NULL) {
381 rc = -EBADMSG;
382 printk(KERN_ERR "%s: Receiving process is a zombie. Dropping "
383 "message.\n", __func__);
384 rcu_read_unlock();
385 mutex_unlock(&ecryptfs_daemon_hash_mux);
386 goto wake_up;
387 }
388 current_user_ns = nsproxy->user_ns;
4eea0353
DH
389 ctx_euid = task_euid(msg_ctx->task);
390 rc = ecryptfs_find_daemon_by_euid(&daemon, ctx_euid, current_user_ns);
6a3fd92e 391 rcu_read_unlock();
f66e883e
MH
392 mutex_unlock(&ecryptfs_daemon_hash_mux);
393 if (rc) {
88b4a07e 394 rc = -EBADMSG;
f66e883e 395 printk(KERN_WARNING "%s: User [%d] received a "
6a3fd92e 396 "message response from process [0x%p] but does "
f66e883e 397 "not have a registered daemon\n", __func__,
4eea0353 398 ctx_euid, pid);
88b4a07e
MH
399 goto wake_up;
400 }
4eea0353 401 if (ctx_euid != euid) {
dddfa461 402 rc = -EBADMSG;
f66e883e
MH
403 printk(KERN_WARNING "%s: Received message from user "
404 "[%d]; expected message from user [%d]\n", __func__,
4eea0353 405 euid, ctx_euid);
dddfa461
MH
406 goto unlock;
407 }
6a3fd92e
MH
408 if (current_user_ns != user_ns) {
409 rc = -EBADMSG;
410 printk(KERN_WARNING "%s: Received message from user_ns "
411 "[0x%p]; expected message from user_ns [0x%p]\n",
412 __func__, user_ns, nsproxy->user_ns);
413 goto unlock;
414 }
f66e883e 415 if (daemon->pid != pid) {
88b4a07e 416 rc = -EBADMSG;
f66e883e 417 printk(KERN_ERR "%s: User [%d] sent a message response "
6a3fd92e 418 "from an unrecognized process [0x%p]\n",
4eea0353 419 __func__, ctx_euid, pid);
88b4a07e
MH
420 goto unlock;
421 }
422 if (msg_ctx->state != ECRYPTFS_MSG_CTX_STATE_PENDING) {
423 rc = -EINVAL;
f66e883e
MH
424 printk(KERN_WARNING "%s: Desired context element is not "
425 "pending a response\n", __func__);
88b4a07e
MH
426 goto unlock;
427 } else if (msg_ctx->counter != seq) {
428 rc = -EINVAL;
f66e883e
MH
429 printk(KERN_WARNING "%s: Invalid message sequence; "
430 "expected [%d]; received [%d]\n", __func__,
431 msg_ctx->counter, seq);
88b4a07e
MH
432 goto unlock;
433 }
f66e883e 434 msg_size = (sizeof(*msg) + msg->data_len);
88b4a07e
MH
435 msg_ctx->msg = kmalloc(msg_size, GFP_KERNEL);
436 if (!msg_ctx->msg) {
437 rc = -ENOMEM;
f66e883e
MH
438 printk(KERN_ERR "%s: Failed to allocate [%Zd] bytes of "
439 "GFP_KERNEL memory\n", __func__, msg_size);
88b4a07e
MH
440 goto unlock;
441 }
442 memcpy(msg_ctx->msg, msg, msg_size);
443 msg_ctx->state = ECRYPTFS_MSG_CTX_STATE_DONE;
444 rc = 0;
445wake_up:
446 wake_up_process(msg_ctx->task);
447unlock:
448 mutex_unlock(&msg_ctx->mux);
449out:
450 return rc;
451}
452
453/**
f66e883e 454 * ecryptfs_send_message_locked
88b4a07e
MH
455 * @data: The data to send
456 * @data_len: The length of data
457 * @msg_ctx: The message context allocated for the send
f66e883e
MH
458 *
459 * Must be called with ecryptfs_daemon_hash_mux held.
460 *
461 * Returns zero on success; non-zero otherwise
88b4a07e 462 */
f66e883e 463static int
624ae528
TH
464ecryptfs_send_message_locked(char *data, int data_len, u8 msg_type,
465 struct ecryptfs_msg_ctx **msg_ctx)
88b4a07e 466{
f66e883e 467 struct ecryptfs_daemon *daemon;
4eea0353 468 uid_t euid = current_euid();
88b4a07e
MH
469 int rc;
470
4eea0353 471 rc = ecryptfs_find_daemon_by_euid(&daemon, euid,
6a3fd92e 472 current->nsproxy->user_ns);
f66e883e 473 if (rc || !daemon) {
88b4a07e 474 rc = -ENOTCONN;
f66e883e 475 printk(KERN_ERR "%s: User [%d] does not have a daemon "
4eea0353 476 "registered\n", __func__, euid);
88b4a07e
MH
477 goto out;
478 }
88b4a07e
MH
479 mutex_lock(&ecryptfs_msg_ctx_lists_mux);
480 rc = ecryptfs_acquire_free_msg_ctx(msg_ctx);
481 if (rc) {
482 mutex_unlock(&ecryptfs_msg_ctx_lists_mux);
f66e883e
MH
483 printk(KERN_WARNING "%s: Could not claim a free "
484 "context element\n", __func__);
88b4a07e
MH
485 goto out;
486 }
487 ecryptfs_msg_ctx_free_to_alloc(*msg_ctx);
488 mutex_unlock(&(*msg_ctx)->mux);
489 mutex_unlock(&ecryptfs_msg_ctx_lists_mux);
624ae528
TH
490 rc = ecryptfs_send_miscdev(data, data_len, *msg_ctx, msg_type, 0,
491 daemon);
f66e883e
MH
492 if (rc)
493 printk(KERN_ERR "%s: Error attempting to send message to "
494 "userspace daemon; rc = [%d]\n", __func__, rc);
88b4a07e
MH
495out:
496 return rc;
497}
498
f66e883e
MH
499/**
500 * ecryptfs_send_message
f66e883e
MH
501 * @data: The data to send
502 * @data_len: The length of data
503 * @msg_ctx: The message context allocated for the send
504 *
505 * Grabs ecryptfs_daemon_hash_mux.
506 *
507 * Returns zero on success; non-zero otherwise
508 */
624ae528 509int ecryptfs_send_message(char *data, int data_len,
f66e883e
MH
510 struct ecryptfs_msg_ctx **msg_ctx)
511{
512 int rc;
513
514 mutex_lock(&ecryptfs_daemon_hash_mux);
624ae528
TH
515 rc = ecryptfs_send_message_locked(data, data_len, ECRYPTFS_MSG_REQUEST,
516 msg_ctx);
f66e883e
MH
517 mutex_unlock(&ecryptfs_daemon_hash_mux);
518 return rc;
519}
520
88b4a07e
MH
521/**
522 * ecryptfs_wait_for_response
523 * @msg_ctx: The context that was assigned when sending a message
524 * @msg: The incoming message from userspace; not set if rc != 0
525 *
526 * Sleeps until awaken by ecryptfs_receive_message or until the amount
527 * of time exceeds ecryptfs_message_wait_timeout. If zero is
528 * returned, msg will point to a valid message from userspace; a
529 * non-zero value is returned upon failure to receive a message or an
f66e883e 530 * error occurs. Callee must free @msg on success.
88b4a07e
MH
531 */
532int ecryptfs_wait_for_response(struct ecryptfs_msg_ctx *msg_ctx,
533 struct ecryptfs_message **msg)
534{
535 signed long timeout = ecryptfs_message_wait_timeout * HZ;
536 int rc = 0;
537
538sleep:
539 timeout = schedule_timeout_interruptible(timeout);
540 mutex_lock(&ecryptfs_msg_ctx_lists_mux);
541 mutex_lock(&msg_ctx->mux);
542 if (msg_ctx->state != ECRYPTFS_MSG_CTX_STATE_DONE) {
543 if (timeout) {
544 mutex_unlock(&msg_ctx->mux);
545 mutex_unlock(&ecryptfs_msg_ctx_lists_mux);
546 goto sleep;
547 }
548 rc = -ENOMSG;
549 } else {
550 *msg = msg_ctx->msg;
551 msg_ctx->msg = NULL;
552 }
553 ecryptfs_msg_ctx_alloc_to_free(msg_ctx);
554 mutex_unlock(&msg_ctx->mux);
555 mutex_unlock(&ecryptfs_msg_ctx_lists_mux);
556 return rc;
557}
558
624ae528 559int ecryptfs_init_messaging(void)
88b4a07e
MH
560{
561 int i;
562 int rc = 0;
563
564 if (ecryptfs_number_of_users > ECRYPTFS_MAX_NUM_USERS) {
565 ecryptfs_number_of_users = ECRYPTFS_MAX_NUM_USERS;
f66e883e
MH
566 printk(KERN_WARNING "%s: Specified number of users is "
567 "too large, defaulting to [%d] users\n", __func__,
568 ecryptfs_number_of_users);
88b4a07e 569 }
f66e883e
MH
570 mutex_init(&ecryptfs_daemon_hash_mux);
571 mutex_lock(&ecryptfs_daemon_hash_mux);
5dda6992
MH
572 ecryptfs_hash_buckets = 1;
573 while (ecryptfs_number_of_users >> ecryptfs_hash_buckets)
574 ecryptfs_hash_buckets++;
f66e883e
MH
575 ecryptfs_daemon_hash = kmalloc((sizeof(struct hlist_head)
576 * ecryptfs_hash_buckets), GFP_KERNEL);
577 if (!ecryptfs_daemon_hash) {
88b4a07e 578 rc = -ENOMEM;
f66e883e
MH
579 printk(KERN_ERR "%s: Failed to allocate memory\n", __func__);
580 mutex_unlock(&ecryptfs_daemon_hash_mux);
88b4a07e
MH
581 goto out;
582 }
583 for (i = 0; i < ecryptfs_hash_buckets; i++)
f66e883e
MH
584 INIT_HLIST_HEAD(&ecryptfs_daemon_hash[i]);
585 mutex_unlock(&ecryptfs_daemon_hash_mux);
88b4a07e 586 ecryptfs_msg_ctx_arr = kmalloc((sizeof(struct ecryptfs_msg_ctx)
f66e883e
MH
587 * ecryptfs_message_buf_len),
588 GFP_KERNEL);
88b4a07e
MH
589 if (!ecryptfs_msg_ctx_arr) {
590 rc = -ENOMEM;
f66e883e 591 printk(KERN_ERR "%s: Failed to allocate memory\n", __func__);
88b4a07e
MH
592 goto out;
593 }
594 mutex_init(&ecryptfs_msg_ctx_lists_mux);
595 mutex_lock(&ecryptfs_msg_ctx_lists_mux);
596 ecryptfs_msg_counter = 0;
597 for (i = 0; i < ecryptfs_message_buf_len; i++) {
598 INIT_LIST_HEAD(&ecryptfs_msg_ctx_arr[i].node);
f66e883e 599 INIT_LIST_HEAD(&ecryptfs_msg_ctx_arr[i].daemon_out_list);
88b4a07e
MH
600 mutex_init(&ecryptfs_msg_ctx_arr[i].mux);
601 mutex_lock(&ecryptfs_msg_ctx_arr[i].mux);
602 ecryptfs_msg_ctx_arr[i].index = i;
603 ecryptfs_msg_ctx_arr[i].state = ECRYPTFS_MSG_CTX_STATE_FREE;
604 ecryptfs_msg_ctx_arr[i].counter = 0;
605 ecryptfs_msg_ctx_arr[i].task = NULL;
606 ecryptfs_msg_ctx_arr[i].msg = NULL;
607 list_add_tail(&ecryptfs_msg_ctx_arr[i].node,
608 &ecryptfs_msg_ctx_free_list);
609 mutex_unlock(&ecryptfs_msg_ctx_arr[i].mux);
610 }
611 mutex_unlock(&ecryptfs_msg_ctx_lists_mux);
624ae528
TH
612 rc = ecryptfs_init_ecryptfs_miscdev();
613 if (rc)
614 ecryptfs_release_messaging();
88b4a07e
MH
615out:
616 return rc;
617}
618
624ae528 619void ecryptfs_release_messaging(void)
88b4a07e
MH
620{
621 if (ecryptfs_msg_ctx_arr) {
622 int i;
623
624 mutex_lock(&ecryptfs_msg_ctx_lists_mux);
625 for (i = 0; i < ecryptfs_message_buf_len; i++) {
626 mutex_lock(&ecryptfs_msg_ctx_arr[i].mux);
627 if (ecryptfs_msg_ctx_arr[i].msg)
628 kfree(ecryptfs_msg_ctx_arr[i].msg);
629 mutex_unlock(&ecryptfs_msg_ctx_arr[i].mux);
630 }
631 kfree(ecryptfs_msg_ctx_arr);
632 mutex_unlock(&ecryptfs_msg_ctx_lists_mux);
633 }
f66e883e 634 if (ecryptfs_daemon_hash) {
88b4a07e 635 struct hlist_node *elem;
f66e883e 636 struct ecryptfs_daemon *daemon;
88b4a07e
MH
637 int i;
638
f66e883e 639 mutex_lock(&ecryptfs_daemon_hash_mux);
88b4a07e 640 for (i = 0; i < ecryptfs_hash_buckets; i++) {
f66e883e
MH
641 int rc;
642
643 hlist_for_each_entry(daemon, elem,
644 &ecryptfs_daemon_hash[i],
645 euid_chain) {
646 rc = ecryptfs_exorcise_daemon(daemon);
647 if (rc)
648 printk(KERN_ERR "%s: Error whilst "
649 "attempting to destroy daemon; "
650 "rc = [%d]. Dazed and confused, "
651 "but trying to continue.\n",
652 __func__, rc);
88b4a07e
MH
653 }
654 }
f66e883e
MH
655 kfree(ecryptfs_daemon_hash);
656 mutex_unlock(&ecryptfs_daemon_hash_mux);
88b4a07e 657 }
624ae528 658 ecryptfs_destroy_ecryptfs_miscdev();
88b4a07e
MH
659 return;
660}
This page took 0.445584 seconds and 5 git commands to generate.