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