messaging.c 14.0 KB
Newer Older
1 2 3
/**
 * eCryptfs: Linux filesystem encryption layer
 *
4
 * Copyright (C) 2004-2008 International Business Machines Corp.
5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21
 *   Author(s): Michael A. Halcrow <mhalcrow@us.ibm.com>
 *		Tyler Hicks <tyhicks@ou.edu>
 *
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public License version
 * 2 as published by the Free Software Foundation.
 *
 * This program is distributed in the hope that it will be useful, but
 * WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA
 * 02111-1307, USA.
 */
A
Alexey Dobriyan 已提交
22
#include <linux/sched.h>
23
#include <linux/slab.h>
24 25
#include <linux/user_namespace.h>
#include <linux/nsproxy.h>
26 27
#include "ecryptfs_kernel.h"

28 29 30
static LIST_HEAD(ecryptfs_msg_ctx_free_list);
static LIST_HEAD(ecryptfs_msg_ctx_alloc_list);
static struct mutex ecryptfs_msg_ctx_lists_mux;
31

32 33
static struct hlist_head *ecryptfs_daemon_hash;
struct mutex ecryptfs_daemon_hash_mux;
34
static int ecryptfs_hash_bits;
35
#define ecryptfs_current_euid_hash(uid) \
36
	hash_long((unsigned long)from_kuid(&init_user_ns, current_euid()), ecryptfs_hash_bits)
37

38
static u32 ecryptfs_msg_counter;
39
static struct ecryptfs_msg_ctx *ecryptfs_msg_ctx_arr;
40 41 42 43 44 45

/**
 * ecryptfs_acquire_free_msg_ctx
 * @msg_ctx: The context that was acquired from the free list
 *
 * Acquires a context element from the free list and locks the mutex
46 47 48 49
 * on the context.  Sets the msg_ctx task to current.  Returns zero on
 * success; non-zero on error or upon failure to acquire a free
 * context element.  Must be called with ecryptfs_msg_ctx_lists_mux
 * held.
50 51 52 53 54 55 56
 */
static int ecryptfs_acquire_free_msg_ctx(struct ecryptfs_msg_ctx **msg_ctx)
{
	struct list_head *p;
	int rc;

	if (list_empty(&ecryptfs_msg_ctx_free_list)) {
57 58 59 60 61
		printk(KERN_WARNING "%s: The eCryptfs free "
		       "context list is empty.  It may be helpful to "
		       "specify the ecryptfs_message_buf_len "
		       "parameter to be greater than the current "
		       "value of [%d]\n", __func__, ecryptfs_message_buf_len);
62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81
		rc = -ENOMEM;
		goto out;
	}
	list_for_each(p, &ecryptfs_msg_ctx_free_list) {
		*msg_ctx = list_entry(p, struct ecryptfs_msg_ctx, node);
		if (mutex_trylock(&(*msg_ctx)->mux)) {
			(*msg_ctx)->task = current;
			rc = 0;
			goto out;
		}
	}
	rc = -ENOMEM;
out:
	return rc;
}

/**
 * ecryptfs_msg_ctx_free_to_alloc
 * @msg_ctx: The context to move from the free list to the alloc list
 *
82
 * Must be called with ecryptfs_msg_ctx_lists_mux held.
83 84 85 86 87 88 89 90 91 92 93 94
 */
static void ecryptfs_msg_ctx_free_to_alloc(struct ecryptfs_msg_ctx *msg_ctx)
{
	list_move(&msg_ctx->node, &ecryptfs_msg_ctx_alloc_list);
	msg_ctx->state = ECRYPTFS_MSG_CTX_STATE_PENDING;
	msg_ctx->counter = ++ecryptfs_msg_counter;
}

/**
 * ecryptfs_msg_ctx_alloc_to_free
 * @msg_ctx: The context to move from the alloc list to the free list
 *
95
 * Must be called with ecryptfs_msg_ctx_lists_mux held.
96
 */
97
void ecryptfs_msg_ctx_alloc_to_free(struct ecryptfs_msg_ctx *msg_ctx)
98 99 100 101
{
	list_move(&(msg_ctx->node), &ecryptfs_msg_ctx_free_list);
	if (msg_ctx->msg)
		kfree(msg_ctx->msg);
102
	msg_ctx->msg = NULL;
103 104 105 106
	msg_ctx->state = ECRYPTFS_MSG_CTX_STATE_FREE;
}

/**
107 108
 * ecryptfs_find_daemon_by_euid
 * @daemon: If return value is zero, points to the desired daemon pointer
109
 *
110 111
 * Must be called with ecryptfs_daemon_hash_mux held.
 *
112
 * Search the hash list for the current effective user id.
113 114
 *
 * Returns zero if the user id exists in the list; non-zero otherwise.
115
 */
116
int ecryptfs_find_daemon_by_euid(struct ecryptfs_daemon **daemon)
117 118 119 120
{
	struct hlist_node *elem;
	int rc;

121
	hlist_for_each_entry(*daemon, elem,
122 123
			    &ecryptfs_daemon_hash[ecryptfs_current_euid_hash()],
			    euid_chain) {
124
		if (uid_eq((*daemon)->file->f_cred->euid, current_euid())) {
125 126 127 128 129 130 131 132 133
			rc = 0;
			goto out;
		}
	}
	rc = -EINVAL;
out:
	return rc;
}

134 135 136
/**
 * ecryptfs_spawn_daemon - Create and initialize a new daemon struct
 * @daemon: Pointer to set to newly allocated daemon struct
137
 * @file: File used when opening /dev/ecryptfs
138 139 140 141 142 143 144
 *
 * Must be called ceremoniously while in possession of
 * ecryptfs_sacred_daemon_hash_mux
 *
 * Returns zero on success; non-zero otherwise
 */
int
145
ecryptfs_spawn_daemon(struct ecryptfs_daemon **daemon, struct file *file)
146 147 148 149 150 151
{
	int rc = 0;

	(*daemon) = kzalloc(sizeof(**daemon), GFP_KERNEL);
	if (!(*daemon)) {
		rc = -ENOMEM;
M
Michael Halcrow 已提交
152
		printk(KERN_ERR "%s: Failed to allocate [%zd] bytes of "
153 154 155
		       "GFP_KERNEL memory\n", __func__, sizeof(**daemon));
		goto out;
	}
156
	(*daemon)->file = file;
157 158 159 160 161
	mutex_init(&(*daemon)->mux);
	INIT_LIST_HEAD(&(*daemon)->msg_ctx_out_queue);
	init_waitqueue_head(&(*daemon)->wait);
	(*daemon)->num_queued_msg_ctx = 0;
	hlist_add_head(&(*daemon)->euid_chain,
162
		       &ecryptfs_daemon_hash[ecryptfs_current_euid_hash()]);
163
out:
164 165 166
	return rc;
}

167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194
/**
 * ecryptfs_exorcise_daemon - Destroy the daemon struct
 *
 * Must be called ceremoniously while in possession of
 * ecryptfs_daemon_hash_mux and the daemon's own mux.
 */
int ecryptfs_exorcise_daemon(struct ecryptfs_daemon *daemon)
{
	struct ecryptfs_msg_ctx *msg_ctx, *msg_ctx_tmp;
	int rc = 0;

	mutex_lock(&daemon->mux);
	if ((daemon->flags & ECRYPTFS_DAEMON_IN_READ)
	    || (daemon->flags & ECRYPTFS_DAEMON_IN_POLL)) {
		rc = -EBUSY;
		mutex_unlock(&daemon->mux);
		goto out;
	}
	list_for_each_entry_safe(msg_ctx, msg_ctx_tmp,
				 &daemon->msg_ctx_out_queue, daemon_out_list) {
		list_del(&msg_ctx->daemon_out_list);
		daemon->num_queued_msg_ctx--;
		printk(KERN_WARNING "%s: Warning: dropping message that is in "
		       "the out queue of a dying daemon\n", __func__);
		ecryptfs_msg_ctx_alloc_to_free(msg_ctx);
	}
	hlist_del(&daemon->euid_chain);
	mutex_unlock(&daemon->mux);
J
Johannes Weiner 已提交
195
	kzfree(daemon);
196
out:
197 198 199 200 201 202
	return rc;
}

/**
 * ecryptfs_process_reponse
 * @msg: The ecryptfs message received; the caller should sanity check
203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218
 *       msg->data_len and free the memory
 * @seq: The sequence number of the message; must match the sequence
 *       number for the existing message context waiting for this
 *       response
 *
 * Processes a response message after sending an operation request to
 * userspace. Some other process is awaiting this response. Before
 * sending out its first communications, the other process allocated a
 * msg_ctx from the ecryptfs_msg_ctx_arr at a particular index. The
 * response message contains this index so that we can copy over the
 * response message into the msg_ctx that the process holds a
 * reference to. The other process is going to wake up, check to see
 * that msg_ctx->state == ECRYPTFS_MSG_CTX_STATE_DONE, and then
 * proceed to read off and process the response message. Returns zero
 * upon delivery to desired context element; non-zero upon delivery
 * failure or error.
219
 *
220
 * Returns zero on success; non-zero otherwise
221
 */
222 223
int ecryptfs_process_response(struct ecryptfs_daemon *daemon,
			      struct ecryptfs_message *msg, u32 seq)
224 225
{
	struct ecryptfs_msg_ctx *msg_ctx;
226
	size_t msg_size;
227 228 229 230
	int rc;

	if (msg->index >= ecryptfs_message_buf_len) {
		rc = -EINVAL;
231 232 233 234
		printk(KERN_ERR "%s: Attempt to reference "
		       "context buffer at index [%d]; maximum "
		       "allowable is [%d]\n", __func__, msg->index,
		       (ecryptfs_message_buf_len - 1));
235 236 237 238 239 240
		goto out;
	}
	msg_ctx = &ecryptfs_msg_ctx_arr[msg->index];
	mutex_lock(&msg_ctx->mux);
	if (msg_ctx->state != ECRYPTFS_MSG_CTX_STATE_PENDING) {
		rc = -EINVAL;
241 242
		printk(KERN_WARNING "%s: Desired context element is not "
		       "pending a response\n", __func__);
243 244 245
		goto unlock;
	} else if (msg_ctx->counter != seq) {
		rc = -EINVAL;
246 247 248
		printk(KERN_WARNING "%s: Invalid message sequence; "
		       "expected [%d]; received [%d]\n", __func__,
		       msg_ctx->counter, seq);
249 250
		goto unlock;
	}
251
	msg_size = (sizeof(*msg) + msg->data_len);
252 253 254
	msg_ctx->msg = kmalloc(msg_size, GFP_KERNEL);
	if (!msg_ctx->msg) {
		rc = -ENOMEM;
M
Michael Halcrow 已提交
255
		printk(KERN_ERR "%s: Failed to allocate [%zd] bytes of "
256
		       "GFP_KERNEL memory\n", __func__, msg_size);
257 258 259 260 261
		goto unlock;
	}
	memcpy(msg_ctx->msg, msg, msg_size);
	msg_ctx->state = ECRYPTFS_MSG_CTX_STATE_DONE;
	wake_up_process(msg_ctx->task);
262
	rc = 0;
263 264 265 266 267 268 269
unlock:
	mutex_unlock(&msg_ctx->mux);
out:
	return rc;
}

/**
270
 * ecryptfs_send_message_locked
271 272 273
 * @data: The data to send
 * @data_len: The length of data
 * @msg_ctx: The message context allocated for the send
274 275 276 277
 *
 * Must be called with ecryptfs_daemon_hash_mux held.
 *
 * Returns zero on success; non-zero otherwise
278
 */
279
static int
T
Tyler Hicks 已提交
280 281
ecryptfs_send_message_locked(char *data, int data_len, u8 msg_type,
			     struct ecryptfs_msg_ctx **msg_ctx)
282
{
283
	struct ecryptfs_daemon *daemon;
284 285
	int rc;

286
	rc = ecryptfs_find_daemon_by_euid(&daemon);
287
	if (rc || !daemon) {
288 289 290 291 292 293 294
		rc = -ENOTCONN;
		goto out;
	}
	mutex_lock(&ecryptfs_msg_ctx_lists_mux);
	rc = ecryptfs_acquire_free_msg_ctx(msg_ctx);
	if (rc) {
		mutex_unlock(&ecryptfs_msg_ctx_lists_mux);
295 296
		printk(KERN_WARNING "%s: Could not claim a free "
		       "context element\n", __func__);
297 298 299 300 301
		goto out;
	}
	ecryptfs_msg_ctx_free_to_alloc(*msg_ctx);
	mutex_unlock(&(*msg_ctx)->mux);
	mutex_unlock(&ecryptfs_msg_ctx_lists_mux);
T
Tyler Hicks 已提交
302 303
	rc = ecryptfs_send_miscdev(data, data_len, *msg_ctx, msg_type, 0,
				   daemon);
304 305 306
	if (rc)
		printk(KERN_ERR "%s: Error attempting to send message to "
		       "userspace daemon; rc = [%d]\n", __func__, rc);
307 308 309 310
out:
	return rc;
}

311 312 313 314 315 316 317 318 319 320
/**
 * ecryptfs_send_message
 * @data: The data to send
 * @data_len: The length of data
 * @msg_ctx: The message context allocated for the send
 *
 * Grabs ecryptfs_daemon_hash_mux.
 *
 * Returns zero on success; non-zero otherwise
 */
T
Tyler Hicks 已提交
321
int ecryptfs_send_message(char *data, int data_len,
322 323 324 325 326
			  struct ecryptfs_msg_ctx **msg_ctx)
{
	int rc;

	mutex_lock(&ecryptfs_daemon_hash_mux);
T
Tyler Hicks 已提交
327 328
	rc = ecryptfs_send_message_locked(data, data_len, ECRYPTFS_MSG_REQUEST,
					  msg_ctx);
329 330 331 332
	mutex_unlock(&ecryptfs_daemon_hash_mux);
	return rc;
}

333 334 335 336 337 338 339 340 341
/**
 * ecryptfs_wait_for_response
 * @msg_ctx: The context that was assigned when sending a message
 * @msg: The incoming message from userspace; not set if rc != 0
 *
 * Sleeps until awaken by ecryptfs_receive_message or until the amount
 * of time exceeds ecryptfs_message_wait_timeout.  If zero is
 * returned, msg will point to a valid message from userspace; a
 * non-zero value is returned upon failure to receive a message or an
342
 * error occurs. Callee must free @msg on success.
343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370
 */
int ecryptfs_wait_for_response(struct ecryptfs_msg_ctx *msg_ctx,
			       struct ecryptfs_message **msg)
{
	signed long timeout = ecryptfs_message_wait_timeout * HZ;
	int rc = 0;

sleep:
	timeout = schedule_timeout_interruptible(timeout);
	mutex_lock(&ecryptfs_msg_ctx_lists_mux);
	mutex_lock(&msg_ctx->mux);
	if (msg_ctx->state != ECRYPTFS_MSG_CTX_STATE_DONE) {
		if (timeout) {
			mutex_unlock(&msg_ctx->mux);
			mutex_unlock(&ecryptfs_msg_ctx_lists_mux);
			goto sleep;
		}
		rc = -ENOMSG;
	} else {
		*msg = msg_ctx->msg;
		msg_ctx->msg = NULL;
	}
	ecryptfs_msg_ctx_alloc_to_free(msg_ctx);
	mutex_unlock(&msg_ctx->mux);
	mutex_unlock(&ecryptfs_msg_ctx_lists_mux);
	return rc;
}

371
int __init ecryptfs_init_messaging(void)
372 373 374 375 376 377
{
	int i;
	int rc = 0;

	if (ecryptfs_number_of_users > ECRYPTFS_MAX_NUM_USERS) {
		ecryptfs_number_of_users = ECRYPTFS_MAX_NUM_USERS;
378 379 380
		printk(KERN_WARNING "%s: Specified number of users is "
		       "too large, defaulting to [%d] users\n", __func__,
		       ecryptfs_number_of_users);
381
	}
382 383
	mutex_init(&ecryptfs_daemon_hash_mux);
	mutex_lock(&ecryptfs_daemon_hash_mux);
384 385 386
	ecryptfs_hash_bits = 1;
	while (ecryptfs_number_of_users >> ecryptfs_hash_bits)
		ecryptfs_hash_bits++;
387
	ecryptfs_daemon_hash = kmalloc((sizeof(struct hlist_head)
388 389
					* (1 << ecryptfs_hash_bits)),
				       GFP_KERNEL);
390
	if (!ecryptfs_daemon_hash) {
391
		rc = -ENOMEM;
392 393
		printk(KERN_ERR "%s: Failed to allocate memory\n", __func__);
		mutex_unlock(&ecryptfs_daemon_hash_mux);
394 395
		goto out;
	}
396
	for (i = 0; i < (1 << ecryptfs_hash_bits); i++)
397 398
		INIT_HLIST_HEAD(&ecryptfs_daemon_hash[i]);
	mutex_unlock(&ecryptfs_daemon_hash_mux);
399
	ecryptfs_msg_ctx_arr = kmalloc((sizeof(struct ecryptfs_msg_ctx)
400 401
					* ecryptfs_message_buf_len),
				       GFP_KERNEL);
402 403
	if (!ecryptfs_msg_ctx_arr) {
		rc = -ENOMEM;
404
		printk(KERN_ERR "%s: Failed to allocate memory\n", __func__);
405 406 407 408 409 410 411
		goto out;
	}
	mutex_init(&ecryptfs_msg_ctx_lists_mux);
	mutex_lock(&ecryptfs_msg_ctx_lists_mux);
	ecryptfs_msg_counter = 0;
	for (i = 0; i < ecryptfs_message_buf_len; i++) {
		INIT_LIST_HEAD(&ecryptfs_msg_ctx_arr[i].node);
412
		INIT_LIST_HEAD(&ecryptfs_msg_ctx_arr[i].daemon_out_list);
413 414 415 416 417 418 419 420 421 422 423 424
		mutex_init(&ecryptfs_msg_ctx_arr[i].mux);
		mutex_lock(&ecryptfs_msg_ctx_arr[i].mux);
		ecryptfs_msg_ctx_arr[i].index = i;
		ecryptfs_msg_ctx_arr[i].state = ECRYPTFS_MSG_CTX_STATE_FREE;
		ecryptfs_msg_ctx_arr[i].counter = 0;
		ecryptfs_msg_ctx_arr[i].task = NULL;
		ecryptfs_msg_ctx_arr[i].msg = NULL;
		list_add_tail(&ecryptfs_msg_ctx_arr[i].node,
			      &ecryptfs_msg_ctx_free_list);
		mutex_unlock(&ecryptfs_msg_ctx_arr[i].mux);
	}
	mutex_unlock(&ecryptfs_msg_ctx_lists_mux);
T
Tyler Hicks 已提交
425 426 427
	rc = ecryptfs_init_ecryptfs_miscdev();
	if (rc)
		ecryptfs_release_messaging();
428 429 430 431
out:
	return rc;
}

T
Tyler Hicks 已提交
432
void ecryptfs_release_messaging(void)
433 434 435 436 437 438 439 440 441 442 443 444 445 446
{
	if (ecryptfs_msg_ctx_arr) {
		int i;

		mutex_lock(&ecryptfs_msg_ctx_lists_mux);
		for (i = 0; i < ecryptfs_message_buf_len; i++) {
			mutex_lock(&ecryptfs_msg_ctx_arr[i].mux);
			if (ecryptfs_msg_ctx_arr[i].msg)
				kfree(ecryptfs_msg_ctx_arr[i].msg);
			mutex_unlock(&ecryptfs_msg_ctx_arr[i].mux);
		}
		kfree(ecryptfs_msg_ctx_arr);
		mutex_unlock(&ecryptfs_msg_ctx_lists_mux);
	}
447
	if (ecryptfs_daemon_hash) {
448
		struct hlist_node *elem;
449
		struct ecryptfs_daemon *daemon;
450 451
		int i;

452
		mutex_lock(&ecryptfs_daemon_hash_mux);
453
		for (i = 0; i < (1 << ecryptfs_hash_bits); i++) {
454 455 456 457 458 459 460 461 462 463 464 465
			int rc;

			hlist_for_each_entry(daemon, elem,
					     &ecryptfs_daemon_hash[i],
					     euid_chain) {
				rc = ecryptfs_exorcise_daemon(daemon);
				if (rc)
					printk(KERN_ERR "%s: Error whilst "
					       "attempting to destroy daemon; "
					       "rc = [%d]. Dazed and confused, "
					       "but trying to continue.\n",
					       __func__, rc);
466 467
			}
		}
468 469
		kfree(ecryptfs_daemon_hash);
		mutex_unlock(&ecryptfs_daemon_hash_mux);
470
	}
T
Tyler Hicks 已提交
471
	ecryptfs_destroy_ecryptfs_miscdev();
472 473
	return;
}