Michael Halcrow | 88b4a07 | 2007-02-12 00:53:43 -0800 | [diff] [blame] | 1 | /** |
| 2 | * eCryptfs: Linux filesystem encryption layer |
| 3 | * |
Michael Halcrow | f66e883 | 2008-04-29 00:59:51 -0700 | [diff] [blame] | 4 | * Copyright (C) 2004-2008 International Business Machines Corp. |
Michael Halcrow | 88b4a07 | 2007-02-12 00:53:43 -0800 | [diff] [blame] | 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 | */ |
Alexey Dobriyan | e8edc6e | 2007-05-21 01:22:52 +0400 | [diff] [blame] | 22 | #include <linux/sched.h> |
Michael Halcrow | 6a3fd92 | 2008-04-29 00:59:52 -0700 | [diff] [blame] | 23 | #include <linux/user_namespace.h> |
| 24 | #include <linux/nsproxy.h> |
Michael Halcrow | 88b4a07 | 2007-02-12 00:53:43 -0800 | [diff] [blame] | 25 | #include "ecryptfs_kernel.h" |
| 26 | |
Michael Halcrow | dd2a3b7 | 2007-02-12 00:53:46 -0800 | [diff] [blame] | 27 | static LIST_HEAD(ecryptfs_msg_ctx_free_list); |
| 28 | static LIST_HEAD(ecryptfs_msg_ctx_alloc_list); |
| 29 | static struct mutex ecryptfs_msg_ctx_lists_mux; |
Michael Halcrow | 88b4a07 | 2007-02-12 00:53:43 -0800 | [diff] [blame] | 30 | |
Michael Halcrow | f66e883 | 2008-04-29 00:59:51 -0700 | [diff] [blame] | 31 | static struct hlist_head *ecryptfs_daemon_hash; |
| 32 | struct mutex ecryptfs_daemon_hash_mux; |
Michael Halcrow | dd2a3b7 | 2007-02-12 00:53:46 -0800 | [diff] [blame] | 33 | static int ecryptfs_hash_buckets; |
| 34 | #define ecryptfs_uid_hash(uid) \ |
| 35 | hash_long((unsigned long)uid, ecryptfs_hash_buckets) |
Michael Halcrow | 88b4a07 | 2007-02-12 00:53:43 -0800 | [diff] [blame] | 36 | |
Michael Halcrow | f66e883 | 2008-04-29 00:59:51 -0700 | [diff] [blame] | 37 | static u32 ecryptfs_msg_counter; |
Michael Halcrow | dd2a3b7 | 2007-02-12 00:53:46 -0800 | [diff] [blame] | 38 | static struct ecryptfs_msg_ctx *ecryptfs_msg_ctx_arr; |
Michael Halcrow | 88b4a07 | 2007-02-12 00:53:43 -0800 | [diff] [blame] | 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 |
Michael Halcrow | f66e883 | 2008-04-29 00:59:51 -0700 | [diff] [blame] | 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. |
Michael Halcrow | 88b4a07 | 2007-02-12 00:53:43 -0800 | [diff] [blame] | 49 | */ |
| 50 | static 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)) { |
Michael Halcrow | f66e883 | 2008-04-29 00:59:51 -0700 | [diff] [blame] | 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); |
Michael Halcrow | 88b4a07 | 2007-02-12 00:53:43 -0800 | [diff] [blame] | 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; |
| 73 | out: |
| 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 | * |
Michael Halcrow | f66e883 | 2008-04-29 00:59:51 -0700 | [diff] [blame] | 81 | * Must be called with ecryptfs_msg_ctx_lists_mux held. |
Michael Halcrow | 88b4a07 | 2007-02-12 00:53:43 -0800 | [diff] [blame] | 82 | */ |
| 83 | static 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 | * |
Michael Halcrow | f66e883 | 2008-04-29 00:59:51 -0700 | [diff] [blame] | 94 | * Must be called with ecryptfs_msg_ctx_lists_mux held. |
Michael Halcrow | 88b4a07 | 2007-02-12 00:53:43 -0800 | [diff] [blame] | 95 | */ |
Michael Halcrow | f66e883 | 2008-04-29 00:59:51 -0700 | [diff] [blame] | 96 | void ecryptfs_msg_ctx_alloc_to_free(struct ecryptfs_msg_ctx *msg_ctx) |
Michael Halcrow | 88b4a07 | 2007-02-12 00:53:43 -0800 | [diff] [blame] | 97 | { |
| 98 | list_move(&(msg_ctx->node), &ecryptfs_msg_ctx_free_list); |
| 99 | if (msg_ctx->msg) |
| 100 | kfree(msg_ctx->msg); |
Michael Halcrow | f66e883 | 2008-04-29 00:59:51 -0700 | [diff] [blame] | 101 | msg_ctx->msg = NULL; |
Michael Halcrow | 88b4a07 | 2007-02-12 00:53:43 -0800 | [diff] [blame] | 102 | msg_ctx->state = ECRYPTFS_MSG_CTX_STATE_FREE; |
| 103 | } |
| 104 | |
| 105 | /** |
Michael Halcrow | f66e883 | 2008-04-29 00:59:51 -0700 | [diff] [blame] | 106 | * ecryptfs_find_daemon_by_euid |
| 107 | * @euid: The effective user id which maps to the desired daemon id |
Michael Halcrow | 6a3fd92 | 2008-04-29 00:59:52 -0700 | [diff] [blame] | 108 | * @user_ns: The namespace in which @euid applies |
Michael Halcrow | f66e883 | 2008-04-29 00:59:51 -0700 | [diff] [blame] | 109 | * @daemon: If return value is zero, points to the desired daemon pointer |
Michael Halcrow | 88b4a07 | 2007-02-12 00:53:43 -0800 | [diff] [blame] | 110 | * |
Michael Halcrow | f66e883 | 2008-04-29 00:59:51 -0700 | [diff] [blame] | 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. |
Michael Halcrow | 88b4a07 | 2007-02-12 00:53:43 -0800 | [diff] [blame] | 116 | */ |
Michael Halcrow | 6a3fd92 | 2008-04-29 00:59:52 -0700 | [diff] [blame] | 117 | int ecryptfs_find_daemon_by_euid(struct ecryptfs_daemon **daemon, uid_t euid, |
| 118 | struct user_namespace *user_ns) |
Michael Halcrow | 88b4a07 | 2007-02-12 00:53:43 -0800 | [diff] [blame] | 119 | { |
| 120 | struct hlist_node *elem; |
| 121 | int rc; |
| 122 | |
Michael Halcrow | f66e883 | 2008-04-29 00:59:51 -0700 | [diff] [blame] | 123 | hlist_for_each_entry(*daemon, elem, |
| 124 | &ecryptfs_daemon_hash[ecryptfs_uid_hash(euid)], |
| 125 | euid_chain) { |
Michael Halcrow | 6a3fd92 | 2008-04-29 00:59:52 -0700 | [diff] [blame] | 126 | if ((*daemon)->euid == euid && (*daemon)->user_ns == user_ns) { |
Michael Halcrow | 88b4a07 | 2007-02-12 00:53:43 -0800 | [diff] [blame] | 127 | rc = 0; |
| 128 | goto out; |
| 129 | } |
| 130 | } |
| 131 | rc = -EINVAL; |
| 132 | out: |
| 133 | return rc; |
| 134 | } |
| 135 | |
Michael Halcrow | f66e883 | 2008-04-29 00:59:51 -0700 | [diff] [blame] | 136 | static int |
Tyler Hicks | 624ae52 | 2008-10-15 22:02:51 -0700 | [diff] [blame] | 137 | ecryptfs_send_message_locked(char *data, int data_len, u8 msg_type, |
| 138 | struct ecryptfs_msg_ctx **msg_ctx); |
Michael Halcrow | f66e883 | 2008-04-29 00:59:51 -0700 | [diff] [blame] | 139 | |
| 140 | /** |
| 141 | * ecryptfs_send_raw_message |
Michael Halcrow | f66e883 | 2008-04-29 00:59:51 -0700 | [diff] [blame] | 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 | */ |
Tyler Hicks | 624ae52 | 2008-10-15 22:02:51 -0700 | [diff] [blame] | 152 | static int ecryptfs_send_raw_message(u8 msg_type, |
Michael Halcrow | f66e883 | 2008-04-29 00:59:51 -0700 | [diff] [blame] | 153 | struct ecryptfs_daemon *daemon) |
Michael Halcrow | 88b4a07 | 2007-02-12 00:53:43 -0800 | [diff] [blame] | 154 | { |
Michael Halcrow | f66e883 | 2008-04-29 00:59:51 -0700 | [diff] [blame] | 155 | struct ecryptfs_msg_ctx *msg_ctx; |
Michael Halcrow | 88b4a07 | 2007-02-12 00:53:43 -0800 | [diff] [blame] | 156 | int rc; |
| 157 | |
Tyler Hicks | 624ae52 | 2008-10-15 22:02:51 -0700 | [diff] [blame] | 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; |
Michael Halcrow | 88b4a07 | 2007-02-12 00:53:43 -0800 | [diff] [blame] | 163 | } |
Tyler Hicks | 624ae52 | 2008-10-15 22:02:51 -0700 | [diff] [blame] | 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); |
Michael Halcrow | f66e883 | 2008-04-29 00:59:51 -0700 | [diff] [blame] | 171 | out: |
| 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 |
Michael Halcrow | 6a3fd92 | 2008-04-29 00:59:52 -0700 | [diff] [blame] | 179 | * @user_ns: The namespace in which @euid applies |
Michael Halcrow | f66e883 | 2008-04-29 00:59:51 -0700 | [diff] [blame] | 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 | */ |
| 187 | int |
Michael Halcrow | 6a3fd92 | 2008-04-29 00:59:52 -0700 | [diff] [blame] | 188 | ecryptfs_spawn_daemon(struct ecryptfs_daemon **daemon, uid_t euid, |
| 189 | struct user_namespace *user_ns, struct pid *pid) |
Michael Halcrow | f66e883 | 2008-04-29 00:59:51 -0700 | [diff] [blame] | 190 | { |
| 191 | int rc = 0; |
| 192 | |
| 193 | (*daemon) = kzalloc(sizeof(**daemon), GFP_KERNEL); |
| 194 | if (!(*daemon)) { |
| 195 | rc = -ENOMEM; |
Michael Halcrow | df261c5 | 2009-01-06 14:42:02 -0800 | [diff] [blame] | 196 | printk(KERN_ERR "%s: Failed to allocate [%zd] bytes of " |
Michael Halcrow | f66e883 | 2008-04-29 00:59:51 -0700 | [diff] [blame] | 197 | "GFP_KERNEL memory\n", __func__, sizeof(**daemon)); |
| 198 | goto out; |
| 199 | } |
| 200 | (*daemon)->euid = euid; |
Michael Halcrow | 6a3fd92 | 2008-04-29 00:59:52 -0700 | [diff] [blame] | 201 | (*daemon)->user_ns = get_user_ns(user_ns); |
| 202 | (*daemon)->pid = get_pid(pid); |
Michael Halcrow | f66e883 | 2008-04-29 00:59:51 -0700 | [diff] [blame] | 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)]); |
| 210 | out: |
Michael Halcrow | 88b4a07 | 2007-02-12 00:53:43 -0800 | [diff] [blame] | 211 | return rc; |
| 212 | } |
| 213 | |
| 214 | /** |
| 215 | * ecryptfs_process_helo |
Michael Halcrow | f66e883 | 2008-04-29 00:59:51 -0700 | [diff] [blame] | 216 | * @euid: The user ID owner of the message |
Michael Halcrow | 6a3fd92 | 2008-04-29 00:59:52 -0700 | [diff] [blame] | 217 | * @user_ns: The namespace in which @euid applies |
Michael Halcrow | 88b4a07 | 2007-02-12 00:53:43 -0800 | [diff] [blame] | 218 | * @pid: The process ID for the userspace program that sent the |
| 219 | * message |
| 220 | * |
Michael Halcrow | f66e883 | 2008-04-29 00:59:51 -0700 | [diff] [blame] | 221 | * Adds the euid and pid values to the daemon euid hash. If an euid |
Michael Halcrow | 88b4a07 | 2007-02-12 00:53:43 -0800 | [diff] [blame] | 222 | * already has a daemon pid registered, the daemon will be |
Michael Halcrow | f66e883 | 2008-04-29 00:59:51 -0700 | [diff] [blame] | 223 | * unregistered before the new daemon is put into the hash list. |
| 224 | * Returns zero after adding a new daemon to the hash list; |
Michael Halcrow | 88b4a07 | 2007-02-12 00:53:43 -0800 | [diff] [blame] | 225 | * non-zero otherwise. |
| 226 | */ |
Tyler Hicks | 624ae52 | 2008-10-15 22:02:51 -0700 | [diff] [blame] | 227 | int ecryptfs_process_helo(uid_t euid, struct user_namespace *user_ns, |
| 228 | struct pid *pid) |
Michael Halcrow | 88b4a07 | 2007-02-12 00:53:43 -0800 | [diff] [blame] | 229 | { |
Michael Halcrow | f66e883 | 2008-04-29 00:59:51 -0700 | [diff] [blame] | 230 | struct ecryptfs_daemon *new_daemon; |
| 231 | struct ecryptfs_daemon *old_daemon; |
Michael Halcrow | 88b4a07 | 2007-02-12 00:53:43 -0800 | [diff] [blame] | 232 | int rc; |
| 233 | |
Michael Halcrow | f66e883 | 2008-04-29 00:59:51 -0700 | [diff] [blame] | 234 | mutex_lock(&ecryptfs_daemon_hash_mux); |
Michael Halcrow | 6a3fd92 | 2008-04-29 00:59:52 -0700 | [diff] [blame] | 235 | rc = ecryptfs_find_daemon_by_euid(&old_daemon, euid, user_ns); |
Michael Halcrow | f66e883 | 2008-04-29 00:59:51 -0700 | [diff] [blame] | 236 | if (rc != 0) { |
Michael Halcrow | 88b4a07 | 2007-02-12 00:53:43 -0800 | [diff] [blame] | 237 | printk(KERN_WARNING "Received request from user [%d] " |
Michael Halcrow | 6a3fd92 | 2008-04-29 00:59:52 -0700 | [diff] [blame] | 238 | "to register daemon [0x%p]; unregistering daemon " |
| 239 | "[0x%p]\n", euid, pid, old_daemon->pid); |
Tyler Hicks | 624ae52 | 2008-10-15 22:02:51 -0700 | [diff] [blame] | 240 | rc = ecryptfs_send_raw_message(ECRYPTFS_MSG_QUIT, old_daemon); |
Michael Halcrow | 88b4a07 | 2007-02-12 00:53:43 -0800 | [diff] [blame] | 241 | if (rc) |
| 242 | printk(KERN_WARNING "Failed to send QUIT " |
Michael Halcrow | 6a3fd92 | 2008-04-29 00:59:52 -0700 | [diff] [blame] | 243 | "message to daemon [0x%p]; rc = [%d]\n", |
Michael Halcrow | f66e883 | 2008-04-29 00:59:51 -0700 | [diff] [blame] | 244 | old_daemon->pid, rc); |
| 245 | hlist_del(&old_daemon->euid_chain); |
| 246 | kfree(old_daemon); |
Michael Halcrow | 88b4a07 | 2007-02-12 00:53:43 -0800 | [diff] [blame] | 247 | } |
Michael Halcrow | 6a3fd92 | 2008-04-29 00:59:52 -0700 | [diff] [blame] | 248 | rc = ecryptfs_spawn_daemon(&new_daemon, euid, user_ns, pid); |
Michael Halcrow | f66e883 | 2008-04-29 00:59:51 -0700 | [diff] [blame] | 249 | if (rc) |
| 250 | printk(KERN_ERR "%s: The gods are displeased with this attempt " |
Michael Halcrow | 6a3fd92 | 2008-04-29 00:59:52 -0700 | [diff] [blame] | 251 | "to create a new daemon object for euid [%d]; pid " |
| 252 | "[0x%p]; rc = [%d]\n", __func__, euid, pid, rc); |
Michael Halcrow | f66e883 | 2008-04-29 00:59:51 -0700 | [diff] [blame] | 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 | */ |
| 263 | int 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 " |
Michael Halcrow | 6a3fd92 | 2008-04-29 00:59:52 -0700 | [diff] [blame] | 273 | "[0x%p], but it is in the midst of a read or a poll\n", |
Michael Halcrow | f66e883 | 2008-04-29 00:59:51 -0700 | [diff] [blame] | 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); |
Michael Halcrow | 6a3fd92 | 2008-04-29 00:59:52 -0700 | [diff] [blame] | 289 | if (daemon->pid) |
| 290 | put_pid(daemon->pid); |
| 291 | if (daemon->user_ns) |
| 292 | put_user_ns(daemon->user_ns); |
Michael Halcrow | f66e883 | 2008-04-29 00:59:51 -0700 | [diff] [blame] | 293 | mutex_unlock(&daemon->mux); |
| 294 | memset(daemon, 0, sizeof(*daemon)); |
| 295 | kfree(daemon); |
| 296 | out: |
Michael Halcrow | 88b4a07 | 2007-02-12 00:53:43 -0800 | [diff] [blame] | 297 | return rc; |
| 298 | } |
| 299 | |
| 300 | /** |
| 301 | * ecryptfs_process_quit |
Michael Halcrow | f66e883 | 2008-04-29 00:59:51 -0700 | [diff] [blame] | 302 | * @euid: The user ID owner of the message |
Michael Halcrow | 6a3fd92 | 2008-04-29 00:59:52 -0700 | [diff] [blame] | 303 | * @user_ns: The namespace in which @euid applies |
Michael Halcrow | 88b4a07 | 2007-02-12 00:53:43 -0800 | [diff] [blame] | 304 | * @pid: The process ID for the userspace program that sent the |
| 305 | * message |
| 306 | * |
Michael Halcrow | f66e883 | 2008-04-29 00:59:51 -0700 | [diff] [blame] | 307 | * Deletes the corresponding daemon for the given euid and pid, if |
Michael Halcrow | 88b4a07 | 2007-02-12 00:53:43 -0800 | [diff] [blame] | 308 | * it is the registered that is requesting the deletion. Returns zero |
Michael Halcrow | f66e883 | 2008-04-29 00:59:51 -0700 | [diff] [blame] | 309 | * after deleting the desired daemon; non-zero otherwise. |
Michael Halcrow | 88b4a07 | 2007-02-12 00:53:43 -0800 | [diff] [blame] | 310 | */ |
Michael Halcrow | 6a3fd92 | 2008-04-29 00:59:52 -0700 | [diff] [blame] | 311 | int ecryptfs_process_quit(uid_t euid, struct user_namespace *user_ns, |
| 312 | struct pid *pid) |
Michael Halcrow | 88b4a07 | 2007-02-12 00:53:43 -0800 | [diff] [blame] | 313 | { |
Michael Halcrow | f66e883 | 2008-04-29 00:59:51 -0700 | [diff] [blame] | 314 | struct ecryptfs_daemon *daemon; |
Michael Halcrow | 88b4a07 | 2007-02-12 00:53:43 -0800 | [diff] [blame] | 315 | int rc; |
| 316 | |
Michael Halcrow | f66e883 | 2008-04-29 00:59:51 -0700 | [diff] [blame] | 317 | mutex_lock(&ecryptfs_daemon_hash_mux); |
Michael Halcrow | 6a3fd92 | 2008-04-29 00:59:52 -0700 | [diff] [blame] | 318 | rc = ecryptfs_find_daemon_by_euid(&daemon, euid, user_ns); |
Michael Halcrow | f66e883 | 2008-04-29 00:59:51 -0700 | [diff] [blame] | 319 | if (rc || !daemon) { |
Michael Halcrow | 88b4a07 | 2007-02-12 00:53:43 -0800 | [diff] [blame] | 320 | rc = -EINVAL; |
Michael Halcrow | f66e883 | 2008-04-29 00:59:51 -0700 | [diff] [blame] | 321 | printk(KERN_ERR "Received request from user [%d] to " |
Michael Halcrow | 6a3fd92 | 2008-04-29 00:59:52 -0700 | [diff] [blame] | 322 | "unregister unrecognized daemon [0x%p]\n", euid, pid); |
Michael Halcrow | f66e883 | 2008-04-29 00:59:51 -0700 | [diff] [blame] | 323 | goto out_unlock; |
Michael Halcrow | 88b4a07 | 2007-02-12 00:53:43 -0800 | [diff] [blame] | 324 | } |
Michael Halcrow | f66e883 | 2008-04-29 00:59:51 -0700 | [diff] [blame] | 325 | rc = ecryptfs_exorcise_daemon(daemon); |
| 326 | out_unlock: |
| 327 | mutex_unlock(&ecryptfs_daemon_hash_mux); |
Michael Halcrow | 88b4a07 | 2007-02-12 00:53:43 -0800 | [diff] [blame] | 328 | return rc; |
| 329 | } |
| 330 | |
| 331 | /** |
| 332 | * ecryptfs_process_reponse |
| 333 | * @msg: The ecryptfs message received; the caller should sanity check |
Michael Halcrow | f66e883 | 2008-04-29 00:59:51 -0700 | [diff] [blame] | 334 | * msg->data_len and free the memory |
Michael Halcrow | 88b4a07 | 2007-02-12 00:53:43 -0800 | [diff] [blame] | 335 | * @pid: The process ID of the userspace application that sent the |
| 336 | * message |
Michael Halcrow | f66e883 | 2008-04-29 00:59:51 -0700 | [diff] [blame] | 337 | * @seq: The sequence number of the message; must match the sequence |
| 338 | * number for the existing message context waiting for this |
| 339 | * response |
Michael Halcrow | 88b4a07 | 2007-02-12 00:53:43 -0800 | [diff] [blame] | 340 | * |
Michael Halcrow | f66e883 | 2008-04-29 00:59:51 -0700 | [diff] [blame] | 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. |
| 352 | * |
| 353 | * Returns zero on success; non-zero otherwise |
Michael Halcrow | 88b4a07 | 2007-02-12 00:53:43 -0800 | [diff] [blame] | 354 | */ |
Michael Halcrow | f66e883 | 2008-04-29 00:59:51 -0700 | [diff] [blame] | 355 | int ecryptfs_process_response(struct ecryptfs_message *msg, uid_t euid, |
Michael Halcrow | 6a3fd92 | 2008-04-29 00:59:52 -0700 | [diff] [blame] | 356 | struct user_namespace *user_ns, struct pid *pid, |
| 357 | u32 seq) |
Michael Halcrow | 88b4a07 | 2007-02-12 00:53:43 -0800 | [diff] [blame] | 358 | { |
Michael Halcrow | f66e883 | 2008-04-29 00:59:51 -0700 | [diff] [blame] | 359 | struct ecryptfs_daemon *daemon; |
Michael Halcrow | 88b4a07 | 2007-02-12 00:53:43 -0800 | [diff] [blame] | 360 | struct ecryptfs_msg_ctx *msg_ctx; |
Michael Halcrow | f66e883 | 2008-04-29 00:59:51 -0700 | [diff] [blame] | 361 | size_t msg_size; |
Michael Halcrow | 6a3fd92 | 2008-04-29 00:59:52 -0700 | [diff] [blame] | 362 | struct nsproxy *nsproxy; |
Serge Hallyn | 18b6e04 | 2008-10-15 16:38:45 -0500 | [diff] [blame] | 363 | struct user_namespace *tsk_user_ns; |
David Howells | 4eea035 | 2008-11-14 10:38:49 +1100 | [diff] [blame] | 364 | uid_t ctx_euid; |
Michael Halcrow | 88b4a07 | 2007-02-12 00:53:43 -0800 | [diff] [blame] | 365 | int rc; |
| 366 | |
| 367 | if (msg->index >= ecryptfs_message_buf_len) { |
| 368 | rc = -EINVAL; |
Michael Halcrow | f66e883 | 2008-04-29 00:59:51 -0700 | [diff] [blame] | 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)); |
Michael Halcrow | 88b4a07 | 2007-02-12 00:53:43 -0800 | [diff] [blame] | 373 | goto out; |
| 374 | } |
| 375 | msg_ctx = &ecryptfs_msg_ctx_arr[msg->index]; |
| 376 | mutex_lock(&msg_ctx->mux); |
Michael Halcrow | f66e883 | 2008-04-29 00:59:51 -0700 | [diff] [blame] | 377 | mutex_lock(&ecryptfs_daemon_hash_mux); |
Michael Halcrow | 6a3fd92 | 2008-04-29 00:59:52 -0700 | [diff] [blame] | 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 | } |
Serge Hallyn | 18b6e04 | 2008-10-15 16:38:45 -0500 | [diff] [blame] | 388 | tsk_user_ns = __task_cred(msg_ctx->task)->user->user_ns; |
David Howells | 4eea035 | 2008-11-14 10:38:49 +1100 | [diff] [blame] | 389 | ctx_euid = task_euid(msg_ctx->task); |
Serge Hallyn | 18b6e04 | 2008-10-15 16:38:45 -0500 | [diff] [blame] | 390 | rc = ecryptfs_find_daemon_by_euid(&daemon, ctx_euid, tsk_user_ns); |
Michael Halcrow | 6a3fd92 | 2008-04-29 00:59:52 -0700 | [diff] [blame] | 391 | rcu_read_unlock(); |
Michael Halcrow | f66e883 | 2008-04-29 00:59:51 -0700 | [diff] [blame] | 392 | mutex_unlock(&ecryptfs_daemon_hash_mux); |
| 393 | if (rc) { |
Michael Halcrow | 88b4a07 | 2007-02-12 00:53:43 -0800 | [diff] [blame] | 394 | rc = -EBADMSG; |
Michael Halcrow | f66e883 | 2008-04-29 00:59:51 -0700 | [diff] [blame] | 395 | printk(KERN_WARNING "%s: User [%d] received a " |
Michael Halcrow | 6a3fd92 | 2008-04-29 00:59:52 -0700 | [diff] [blame] | 396 | "message response from process [0x%p] but does " |
Michael Halcrow | f66e883 | 2008-04-29 00:59:51 -0700 | [diff] [blame] | 397 | "not have a registered daemon\n", __func__, |
David Howells | 4eea035 | 2008-11-14 10:38:49 +1100 | [diff] [blame] | 398 | ctx_euid, pid); |
Michael Halcrow | 88b4a07 | 2007-02-12 00:53:43 -0800 | [diff] [blame] | 399 | goto wake_up; |
| 400 | } |
David Howells | 4eea035 | 2008-11-14 10:38:49 +1100 | [diff] [blame] | 401 | if (ctx_euid != euid) { |
Michael Halcrow | dddfa46 | 2007-02-12 00:53:44 -0800 | [diff] [blame] | 402 | rc = -EBADMSG; |
Michael Halcrow | f66e883 | 2008-04-29 00:59:51 -0700 | [diff] [blame] | 403 | printk(KERN_WARNING "%s: Received message from user " |
| 404 | "[%d]; expected message from user [%d]\n", __func__, |
David Howells | 4eea035 | 2008-11-14 10:38:49 +1100 | [diff] [blame] | 405 | euid, ctx_euid); |
Michael Halcrow | dddfa46 | 2007-02-12 00:53:44 -0800 | [diff] [blame] | 406 | goto unlock; |
| 407 | } |
Serge Hallyn | 18b6e04 | 2008-10-15 16:38:45 -0500 | [diff] [blame] | 408 | if (tsk_user_ns != user_ns) { |
Michael Halcrow | 6a3fd92 | 2008-04-29 00:59:52 -0700 | [diff] [blame] | 409 | rc = -EBADMSG; |
| 410 | printk(KERN_WARNING "%s: Received message from user_ns " |
| 411 | "[0x%p]; expected message from user_ns [0x%p]\n", |
Serge Hallyn | 18b6e04 | 2008-10-15 16:38:45 -0500 | [diff] [blame] | 412 | __func__, user_ns, tsk_user_ns); |
Michael Halcrow | 6a3fd92 | 2008-04-29 00:59:52 -0700 | [diff] [blame] | 413 | goto unlock; |
| 414 | } |
Michael Halcrow | f66e883 | 2008-04-29 00:59:51 -0700 | [diff] [blame] | 415 | if (daemon->pid != pid) { |
Michael Halcrow | 88b4a07 | 2007-02-12 00:53:43 -0800 | [diff] [blame] | 416 | rc = -EBADMSG; |
Michael Halcrow | f66e883 | 2008-04-29 00:59:51 -0700 | [diff] [blame] | 417 | printk(KERN_ERR "%s: User [%d] sent a message response " |
Michael Halcrow | 6a3fd92 | 2008-04-29 00:59:52 -0700 | [diff] [blame] | 418 | "from an unrecognized process [0x%p]\n", |
David Howells | 4eea035 | 2008-11-14 10:38:49 +1100 | [diff] [blame] | 419 | __func__, ctx_euid, pid); |
Michael Halcrow | 88b4a07 | 2007-02-12 00:53:43 -0800 | [diff] [blame] | 420 | goto unlock; |
| 421 | } |
| 422 | if (msg_ctx->state != ECRYPTFS_MSG_CTX_STATE_PENDING) { |
| 423 | rc = -EINVAL; |
Michael Halcrow | f66e883 | 2008-04-29 00:59:51 -0700 | [diff] [blame] | 424 | printk(KERN_WARNING "%s: Desired context element is not " |
| 425 | "pending a response\n", __func__); |
Michael Halcrow | 88b4a07 | 2007-02-12 00:53:43 -0800 | [diff] [blame] | 426 | goto unlock; |
| 427 | } else if (msg_ctx->counter != seq) { |
| 428 | rc = -EINVAL; |
Michael Halcrow | f66e883 | 2008-04-29 00:59:51 -0700 | [diff] [blame] | 429 | printk(KERN_WARNING "%s: Invalid message sequence; " |
| 430 | "expected [%d]; received [%d]\n", __func__, |
| 431 | msg_ctx->counter, seq); |
Michael Halcrow | 88b4a07 | 2007-02-12 00:53:43 -0800 | [diff] [blame] | 432 | goto unlock; |
| 433 | } |
Michael Halcrow | f66e883 | 2008-04-29 00:59:51 -0700 | [diff] [blame] | 434 | msg_size = (sizeof(*msg) + msg->data_len); |
Michael Halcrow | 88b4a07 | 2007-02-12 00:53:43 -0800 | [diff] [blame] | 435 | msg_ctx->msg = kmalloc(msg_size, GFP_KERNEL); |
| 436 | if (!msg_ctx->msg) { |
| 437 | rc = -ENOMEM; |
Michael Halcrow | df261c5 | 2009-01-06 14:42:02 -0800 | [diff] [blame] | 438 | printk(KERN_ERR "%s: Failed to allocate [%zd] bytes of " |
Michael Halcrow | f66e883 | 2008-04-29 00:59:51 -0700 | [diff] [blame] | 439 | "GFP_KERNEL memory\n", __func__, msg_size); |
Michael Halcrow | 88b4a07 | 2007-02-12 00:53:43 -0800 | [diff] [blame] | 440 | goto unlock; |
| 441 | } |
| 442 | memcpy(msg_ctx->msg, msg, msg_size); |
| 443 | msg_ctx->state = ECRYPTFS_MSG_CTX_STATE_DONE; |
| 444 | rc = 0; |
| 445 | wake_up: |
| 446 | wake_up_process(msg_ctx->task); |
| 447 | unlock: |
| 448 | mutex_unlock(&msg_ctx->mux); |
| 449 | out: |
| 450 | return rc; |
| 451 | } |
| 452 | |
| 453 | /** |
Michael Halcrow | f66e883 | 2008-04-29 00:59:51 -0700 | [diff] [blame] | 454 | * ecryptfs_send_message_locked |
Michael Halcrow | 88b4a07 | 2007-02-12 00:53:43 -0800 | [diff] [blame] | 455 | * @data: The data to send |
| 456 | * @data_len: The length of data |
| 457 | * @msg_ctx: The message context allocated for the send |
Michael Halcrow | f66e883 | 2008-04-29 00:59:51 -0700 | [diff] [blame] | 458 | * |
| 459 | * Must be called with ecryptfs_daemon_hash_mux held. |
| 460 | * |
| 461 | * Returns zero on success; non-zero otherwise |
Michael Halcrow | 88b4a07 | 2007-02-12 00:53:43 -0800 | [diff] [blame] | 462 | */ |
Michael Halcrow | f66e883 | 2008-04-29 00:59:51 -0700 | [diff] [blame] | 463 | static int |
Tyler Hicks | 624ae52 | 2008-10-15 22:02:51 -0700 | [diff] [blame] | 464 | ecryptfs_send_message_locked(char *data, int data_len, u8 msg_type, |
| 465 | struct ecryptfs_msg_ctx **msg_ctx) |
Michael Halcrow | 88b4a07 | 2007-02-12 00:53:43 -0800 | [diff] [blame] | 466 | { |
Michael Halcrow | f66e883 | 2008-04-29 00:59:51 -0700 | [diff] [blame] | 467 | struct ecryptfs_daemon *daemon; |
David Howells | 4eea035 | 2008-11-14 10:38:49 +1100 | [diff] [blame] | 468 | uid_t euid = current_euid(); |
Michael Halcrow | 88b4a07 | 2007-02-12 00:53:43 -0800 | [diff] [blame] | 469 | int rc; |
| 470 | |
Serge Hallyn | 18b6e04 | 2008-10-15 16:38:45 -0500 | [diff] [blame] | 471 | rc = ecryptfs_find_daemon_by_euid(&daemon, euid, current_user_ns()); |
Michael Halcrow | f66e883 | 2008-04-29 00:59:51 -0700 | [diff] [blame] | 472 | if (rc || !daemon) { |
Michael Halcrow | 88b4a07 | 2007-02-12 00:53:43 -0800 | [diff] [blame] | 473 | rc = -ENOTCONN; |
Michael Halcrow | f66e883 | 2008-04-29 00:59:51 -0700 | [diff] [blame] | 474 | printk(KERN_ERR "%s: User [%d] does not have a daemon " |
David Howells | 4eea035 | 2008-11-14 10:38:49 +1100 | [diff] [blame] | 475 | "registered\n", __func__, euid); |
Michael Halcrow | 88b4a07 | 2007-02-12 00:53:43 -0800 | [diff] [blame] | 476 | goto out; |
| 477 | } |
Michael Halcrow | 88b4a07 | 2007-02-12 00:53:43 -0800 | [diff] [blame] | 478 | mutex_lock(&ecryptfs_msg_ctx_lists_mux); |
| 479 | rc = ecryptfs_acquire_free_msg_ctx(msg_ctx); |
| 480 | if (rc) { |
| 481 | mutex_unlock(&ecryptfs_msg_ctx_lists_mux); |
Michael Halcrow | f66e883 | 2008-04-29 00:59:51 -0700 | [diff] [blame] | 482 | printk(KERN_WARNING "%s: Could not claim a free " |
| 483 | "context element\n", __func__); |
Michael Halcrow | 88b4a07 | 2007-02-12 00:53:43 -0800 | [diff] [blame] | 484 | goto out; |
| 485 | } |
| 486 | ecryptfs_msg_ctx_free_to_alloc(*msg_ctx); |
| 487 | mutex_unlock(&(*msg_ctx)->mux); |
| 488 | mutex_unlock(&ecryptfs_msg_ctx_lists_mux); |
Tyler Hicks | 624ae52 | 2008-10-15 22:02:51 -0700 | [diff] [blame] | 489 | rc = ecryptfs_send_miscdev(data, data_len, *msg_ctx, msg_type, 0, |
| 490 | daemon); |
Michael Halcrow | f66e883 | 2008-04-29 00:59:51 -0700 | [diff] [blame] | 491 | if (rc) |
| 492 | printk(KERN_ERR "%s: Error attempting to send message to " |
| 493 | "userspace daemon; rc = [%d]\n", __func__, rc); |
Michael Halcrow | 88b4a07 | 2007-02-12 00:53:43 -0800 | [diff] [blame] | 494 | out: |
| 495 | return rc; |
| 496 | } |
| 497 | |
| 498 | /** |
Michael Halcrow | f66e883 | 2008-04-29 00:59:51 -0700 | [diff] [blame] | 499 | * ecryptfs_send_message |
Michael Halcrow | f66e883 | 2008-04-29 00:59:51 -0700 | [diff] [blame] | 500 | * @data: The data to send |
| 501 | * @data_len: The length of data |
| 502 | * @msg_ctx: The message context allocated for the send |
| 503 | * |
| 504 | * Grabs ecryptfs_daemon_hash_mux. |
| 505 | * |
| 506 | * Returns zero on success; non-zero otherwise |
| 507 | */ |
Tyler Hicks | 624ae52 | 2008-10-15 22:02:51 -0700 | [diff] [blame] | 508 | int ecryptfs_send_message(char *data, int data_len, |
Michael Halcrow | f66e883 | 2008-04-29 00:59:51 -0700 | [diff] [blame] | 509 | struct ecryptfs_msg_ctx **msg_ctx) |
| 510 | { |
| 511 | int rc; |
| 512 | |
| 513 | mutex_lock(&ecryptfs_daemon_hash_mux); |
Tyler Hicks | 624ae52 | 2008-10-15 22:02:51 -0700 | [diff] [blame] | 514 | rc = ecryptfs_send_message_locked(data, data_len, ECRYPTFS_MSG_REQUEST, |
| 515 | msg_ctx); |
Michael Halcrow | f66e883 | 2008-04-29 00:59:51 -0700 | [diff] [blame] | 516 | mutex_unlock(&ecryptfs_daemon_hash_mux); |
| 517 | return rc; |
| 518 | } |
| 519 | |
| 520 | /** |
Michael Halcrow | 88b4a07 | 2007-02-12 00:53:43 -0800 | [diff] [blame] | 521 | * ecryptfs_wait_for_response |
| 522 | * @msg_ctx: The context that was assigned when sending a message |
| 523 | * @msg: The incoming message from userspace; not set if rc != 0 |
| 524 | * |
| 525 | * Sleeps until awaken by ecryptfs_receive_message or until the amount |
| 526 | * of time exceeds ecryptfs_message_wait_timeout. If zero is |
| 527 | * returned, msg will point to a valid message from userspace; a |
| 528 | * non-zero value is returned upon failure to receive a message or an |
Michael Halcrow | f66e883 | 2008-04-29 00:59:51 -0700 | [diff] [blame] | 529 | * error occurs. Callee must free @msg on success. |
Michael Halcrow | 88b4a07 | 2007-02-12 00:53:43 -0800 | [diff] [blame] | 530 | */ |
| 531 | int ecryptfs_wait_for_response(struct ecryptfs_msg_ctx *msg_ctx, |
| 532 | struct ecryptfs_message **msg) |
| 533 | { |
| 534 | signed long timeout = ecryptfs_message_wait_timeout * HZ; |
| 535 | int rc = 0; |
| 536 | |
| 537 | sleep: |
| 538 | timeout = schedule_timeout_interruptible(timeout); |
| 539 | mutex_lock(&ecryptfs_msg_ctx_lists_mux); |
| 540 | mutex_lock(&msg_ctx->mux); |
| 541 | if (msg_ctx->state != ECRYPTFS_MSG_CTX_STATE_DONE) { |
| 542 | if (timeout) { |
| 543 | mutex_unlock(&msg_ctx->mux); |
| 544 | mutex_unlock(&ecryptfs_msg_ctx_lists_mux); |
| 545 | goto sleep; |
| 546 | } |
| 547 | rc = -ENOMSG; |
| 548 | } else { |
| 549 | *msg = msg_ctx->msg; |
| 550 | msg_ctx->msg = NULL; |
| 551 | } |
| 552 | ecryptfs_msg_ctx_alloc_to_free(msg_ctx); |
| 553 | mutex_unlock(&msg_ctx->mux); |
| 554 | mutex_unlock(&ecryptfs_msg_ctx_lists_mux); |
| 555 | return rc; |
| 556 | } |
| 557 | |
Tyler Hicks | 624ae52 | 2008-10-15 22:02:51 -0700 | [diff] [blame] | 558 | int ecryptfs_init_messaging(void) |
Michael Halcrow | 88b4a07 | 2007-02-12 00:53:43 -0800 | [diff] [blame] | 559 | { |
| 560 | int i; |
| 561 | int rc = 0; |
| 562 | |
| 563 | if (ecryptfs_number_of_users > ECRYPTFS_MAX_NUM_USERS) { |
| 564 | ecryptfs_number_of_users = ECRYPTFS_MAX_NUM_USERS; |
Michael Halcrow | f66e883 | 2008-04-29 00:59:51 -0700 | [diff] [blame] | 565 | printk(KERN_WARNING "%s: Specified number of users is " |
| 566 | "too large, defaulting to [%d] users\n", __func__, |
| 567 | ecryptfs_number_of_users); |
Michael Halcrow | 88b4a07 | 2007-02-12 00:53:43 -0800 | [diff] [blame] | 568 | } |
Michael Halcrow | f66e883 | 2008-04-29 00:59:51 -0700 | [diff] [blame] | 569 | mutex_init(&ecryptfs_daemon_hash_mux); |
| 570 | mutex_lock(&ecryptfs_daemon_hash_mux); |
Michael Halcrow | 5dda699 | 2007-10-16 01:28:06 -0700 | [diff] [blame] | 571 | ecryptfs_hash_buckets = 1; |
| 572 | while (ecryptfs_number_of_users >> ecryptfs_hash_buckets) |
| 573 | ecryptfs_hash_buckets++; |
Michael Halcrow | f66e883 | 2008-04-29 00:59:51 -0700 | [diff] [blame] | 574 | ecryptfs_daemon_hash = kmalloc((sizeof(struct hlist_head) |
| 575 | * ecryptfs_hash_buckets), GFP_KERNEL); |
| 576 | if (!ecryptfs_daemon_hash) { |
Michael Halcrow | 88b4a07 | 2007-02-12 00:53:43 -0800 | [diff] [blame] | 577 | rc = -ENOMEM; |
Michael Halcrow | f66e883 | 2008-04-29 00:59:51 -0700 | [diff] [blame] | 578 | printk(KERN_ERR "%s: Failed to allocate memory\n", __func__); |
| 579 | mutex_unlock(&ecryptfs_daemon_hash_mux); |
Michael Halcrow | 88b4a07 | 2007-02-12 00:53:43 -0800 | [diff] [blame] | 580 | goto out; |
| 581 | } |
| 582 | for (i = 0; i < ecryptfs_hash_buckets; i++) |
Michael Halcrow | f66e883 | 2008-04-29 00:59:51 -0700 | [diff] [blame] | 583 | INIT_HLIST_HEAD(&ecryptfs_daemon_hash[i]); |
| 584 | mutex_unlock(&ecryptfs_daemon_hash_mux); |
Michael Halcrow | 88b4a07 | 2007-02-12 00:53:43 -0800 | [diff] [blame] | 585 | ecryptfs_msg_ctx_arr = kmalloc((sizeof(struct ecryptfs_msg_ctx) |
Michael Halcrow | f66e883 | 2008-04-29 00:59:51 -0700 | [diff] [blame] | 586 | * ecryptfs_message_buf_len), |
| 587 | GFP_KERNEL); |
Michael Halcrow | 88b4a07 | 2007-02-12 00:53:43 -0800 | [diff] [blame] | 588 | if (!ecryptfs_msg_ctx_arr) { |
| 589 | rc = -ENOMEM; |
Michael Halcrow | f66e883 | 2008-04-29 00:59:51 -0700 | [diff] [blame] | 590 | printk(KERN_ERR "%s: Failed to allocate memory\n", __func__); |
Michael Halcrow | 88b4a07 | 2007-02-12 00:53:43 -0800 | [diff] [blame] | 591 | goto out; |
| 592 | } |
| 593 | mutex_init(&ecryptfs_msg_ctx_lists_mux); |
| 594 | mutex_lock(&ecryptfs_msg_ctx_lists_mux); |
| 595 | ecryptfs_msg_counter = 0; |
| 596 | for (i = 0; i < ecryptfs_message_buf_len; i++) { |
| 597 | INIT_LIST_HEAD(&ecryptfs_msg_ctx_arr[i].node); |
Michael Halcrow | f66e883 | 2008-04-29 00:59:51 -0700 | [diff] [blame] | 598 | INIT_LIST_HEAD(&ecryptfs_msg_ctx_arr[i].daemon_out_list); |
Michael Halcrow | 88b4a07 | 2007-02-12 00:53:43 -0800 | [diff] [blame] | 599 | mutex_init(&ecryptfs_msg_ctx_arr[i].mux); |
| 600 | mutex_lock(&ecryptfs_msg_ctx_arr[i].mux); |
| 601 | ecryptfs_msg_ctx_arr[i].index = i; |
| 602 | ecryptfs_msg_ctx_arr[i].state = ECRYPTFS_MSG_CTX_STATE_FREE; |
| 603 | ecryptfs_msg_ctx_arr[i].counter = 0; |
| 604 | ecryptfs_msg_ctx_arr[i].task = NULL; |
| 605 | ecryptfs_msg_ctx_arr[i].msg = NULL; |
| 606 | list_add_tail(&ecryptfs_msg_ctx_arr[i].node, |
| 607 | &ecryptfs_msg_ctx_free_list); |
| 608 | mutex_unlock(&ecryptfs_msg_ctx_arr[i].mux); |
| 609 | } |
| 610 | mutex_unlock(&ecryptfs_msg_ctx_lists_mux); |
Tyler Hicks | 624ae52 | 2008-10-15 22:02:51 -0700 | [diff] [blame] | 611 | rc = ecryptfs_init_ecryptfs_miscdev(); |
| 612 | if (rc) |
| 613 | ecryptfs_release_messaging(); |
Michael Halcrow | 88b4a07 | 2007-02-12 00:53:43 -0800 | [diff] [blame] | 614 | out: |
| 615 | return rc; |
| 616 | } |
| 617 | |
Tyler Hicks | 624ae52 | 2008-10-15 22:02:51 -0700 | [diff] [blame] | 618 | void ecryptfs_release_messaging(void) |
Michael Halcrow | 88b4a07 | 2007-02-12 00:53:43 -0800 | [diff] [blame] | 619 | { |
| 620 | if (ecryptfs_msg_ctx_arr) { |
| 621 | int i; |
| 622 | |
| 623 | mutex_lock(&ecryptfs_msg_ctx_lists_mux); |
| 624 | for (i = 0; i < ecryptfs_message_buf_len; i++) { |
| 625 | mutex_lock(&ecryptfs_msg_ctx_arr[i].mux); |
| 626 | if (ecryptfs_msg_ctx_arr[i].msg) |
| 627 | kfree(ecryptfs_msg_ctx_arr[i].msg); |
| 628 | mutex_unlock(&ecryptfs_msg_ctx_arr[i].mux); |
| 629 | } |
| 630 | kfree(ecryptfs_msg_ctx_arr); |
| 631 | mutex_unlock(&ecryptfs_msg_ctx_lists_mux); |
| 632 | } |
Michael Halcrow | f66e883 | 2008-04-29 00:59:51 -0700 | [diff] [blame] | 633 | if (ecryptfs_daemon_hash) { |
Michael Halcrow | 88b4a07 | 2007-02-12 00:53:43 -0800 | [diff] [blame] | 634 | struct hlist_node *elem; |
Michael Halcrow | f66e883 | 2008-04-29 00:59:51 -0700 | [diff] [blame] | 635 | struct ecryptfs_daemon *daemon; |
Michael Halcrow | 88b4a07 | 2007-02-12 00:53:43 -0800 | [diff] [blame] | 636 | int i; |
| 637 | |
Michael Halcrow | f66e883 | 2008-04-29 00:59:51 -0700 | [diff] [blame] | 638 | mutex_lock(&ecryptfs_daemon_hash_mux); |
Michael Halcrow | 88b4a07 | 2007-02-12 00:53:43 -0800 | [diff] [blame] | 639 | for (i = 0; i < ecryptfs_hash_buckets; i++) { |
Michael Halcrow | f66e883 | 2008-04-29 00:59:51 -0700 | [diff] [blame] | 640 | int rc; |
| 641 | |
| 642 | hlist_for_each_entry(daemon, elem, |
| 643 | &ecryptfs_daemon_hash[i], |
| 644 | euid_chain) { |
| 645 | rc = ecryptfs_exorcise_daemon(daemon); |
| 646 | if (rc) |
| 647 | printk(KERN_ERR "%s: Error whilst " |
| 648 | "attempting to destroy daemon; " |
| 649 | "rc = [%d]. Dazed and confused, " |
| 650 | "but trying to continue.\n", |
| 651 | __func__, rc); |
Michael Halcrow | 88b4a07 | 2007-02-12 00:53:43 -0800 | [diff] [blame] | 652 | } |
| 653 | } |
Michael Halcrow | f66e883 | 2008-04-29 00:59:51 -0700 | [diff] [blame] | 654 | kfree(ecryptfs_daemon_hash); |
| 655 | mutex_unlock(&ecryptfs_daemon_hash_mux); |
Michael Halcrow | 88b4a07 | 2007-02-12 00:53:43 -0800 | [diff] [blame] | 656 | } |
Tyler Hicks | 624ae52 | 2008-10-15 22:02:51 -0700 | [diff] [blame] | 657 | ecryptfs_destroy_ecryptfs_miscdev(); |
Michael Halcrow | 88b4a07 | 2007-02-12 00:53:43 -0800 | [diff] [blame] | 658 | return; |
| 659 | } |