Orange Pi5 kernel

Deprecated Linux kernel 5.10.110 for OrangePi 5/5B/5+ boards

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