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