blob: a96d341d154d0d0082047ca291632bd4002a7ac9 [file] [log] [blame]
Michael Halcrow88b4a072007-02-12 00:53:43 -08001/**
2 * eCryptfs: Linux filesystem encryption layer
3 *
4 * Copyright (C) 2004-2006 International Business Machines Corp.
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 Dobriyane8edc6e2007-05-21 01:22:52 +040022#include <linux/sched.h>
Michael Halcrow88b4a072007-02-12 00:53:43 -080023#include "ecryptfs_kernel.h"
24
Michael Halcrowdd2a3b72007-02-12 00:53:46 -080025static LIST_HEAD(ecryptfs_msg_ctx_free_list);
26static LIST_HEAD(ecryptfs_msg_ctx_alloc_list);
27static struct mutex ecryptfs_msg_ctx_lists_mux;
Michael Halcrow88b4a072007-02-12 00:53:43 -080028
Michael Halcrowdd2a3b72007-02-12 00:53:46 -080029static struct hlist_head *ecryptfs_daemon_id_hash;
30static struct mutex ecryptfs_daemon_id_hash_mux;
31static int ecryptfs_hash_buckets;
32#define ecryptfs_uid_hash(uid) \
33 hash_long((unsigned long)uid, ecryptfs_hash_buckets)
Michael Halcrow88b4a072007-02-12 00:53:43 -080034
Michael Halcrowdd2a3b72007-02-12 00:53:46 -080035static unsigned int ecryptfs_msg_counter;
36static struct ecryptfs_msg_ctx *ecryptfs_msg_ctx_arr;
Michael Halcrow88b4a072007-02-12 00:53:43 -080037
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
43 * on the context. Returns zero on success; non-zero on error or upon
44 * failure to acquire a free context element. Be sure to lock the
45 * list mutex before calling.
46 */
47static int ecryptfs_acquire_free_msg_ctx(struct ecryptfs_msg_ctx **msg_ctx)
48{
49 struct list_head *p;
50 int rc;
51
52 if (list_empty(&ecryptfs_msg_ctx_free_list)) {
53 ecryptfs_printk(KERN_WARNING, "The eCryptfs free "
54 "context list is empty. It may be helpful to "
55 "specify the ecryptfs_message_buf_len "
56 "parameter to be greater than the current "
57 "value of [%d]\n", ecryptfs_message_buf_len);
58 rc = -ENOMEM;
59 goto out;
60 }
61 list_for_each(p, &ecryptfs_msg_ctx_free_list) {
62 *msg_ctx = list_entry(p, struct ecryptfs_msg_ctx, node);
63 if (mutex_trylock(&(*msg_ctx)->mux)) {
64 (*msg_ctx)->task = current;
65 rc = 0;
66 goto out;
67 }
68 }
69 rc = -ENOMEM;
70out:
71 return rc;
72}
73
74/**
75 * ecryptfs_msg_ctx_free_to_alloc
76 * @msg_ctx: The context to move from the free list to the alloc list
77 *
78 * Be sure to lock the list mutex and the context mutex before
79 * calling.
80 */
81static 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 *
92 * Be sure to lock the list mutex and the context mutex before
93 * calling.
94 */
95static void ecryptfs_msg_ctx_alloc_to_free(struct ecryptfs_msg_ctx *msg_ctx)
96{
97 list_move(&(msg_ctx->node), &ecryptfs_msg_ctx_free_list);
98 if (msg_ctx->msg)
99 kfree(msg_ctx->msg);
100 msg_ctx->state = ECRYPTFS_MSG_CTX_STATE_FREE;
101}
102
103/**
104 * ecryptfs_find_daemon_id
105 * @uid: The user id which maps to the desired daemon id
106 * @id: If return value is zero, points to the desired daemon id
107 * pointer
108 *
109 * Search the hash list for the given user id. Returns zero if the
110 * user id exists in the list; non-zero otherwise. The daemon id hash
111 * mutex should be held before calling this function.
112 */
113static int ecryptfs_find_daemon_id(uid_t uid, struct ecryptfs_daemon_id **id)
114{
115 struct hlist_node *elem;
116 int rc;
117
118 hlist_for_each_entry(*id, elem,
119 &ecryptfs_daemon_id_hash[ecryptfs_uid_hash(uid)],
120 id_chain) {
121 if ((*id)->uid == uid) {
122 rc = 0;
123 goto out;
124 }
125 }
126 rc = -EINVAL;
127out:
128 return rc;
129}
130
131static int ecryptfs_send_raw_message(unsigned int transport, u16 msg_type,
132 pid_t pid)
133{
134 int rc;
135
136 switch(transport) {
137 case ECRYPTFS_TRANSPORT_NETLINK:
138 rc = ecryptfs_send_netlink(NULL, 0, NULL, msg_type, 0, pid);
139 break;
140 case ECRYPTFS_TRANSPORT_CONNECTOR:
141 case ECRYPTFS_TRANSPORT_RELAYFS:
142 default:
143 rc = -ENOSYS;
144 }
145 return rc;
146}
147
148/**
149 * ecryptfs_process_helo
150 * @transport: The underlying transport (netlink, etc.)
151 * @uid: The user ID owner of the message
152 * @pid: The process ID for the userspace program that sent the
153 * message
154 *
155 * Adds the uid and pid values to the daemon id hash. If a uid
156 * already has a daemon pid registered, the daemon will be
157 * unregistered before the new daemon id is put into the hash list.
158 * Returns zero after adding a new daemon id to the hash list;
159 * non-zero otherwise.
160 */
161int ecryptfs_process_helo(unsigned int transport, uid_t uid, pid_t pid)
162{
163 struct ecryptfs_daemon_id *new_id;
164 struct ecryptfs_daemon_id *old_id;
165 int rc;
166
167 mutex_lock(&ecryptfs_daemon_id_hash_mux);
168 new_id = kmalloc(sizeof(*new_id), GFP_KERNEL);
169 if (!new_id) {
170 rc = -ENOMEM;
171 ecryptfs_printk(KERN_ERR, "Failed to allocate memory; unable "
Thomas Hisch008983d2007-02-16 01:28:41 -0800172 "to register daemon [%d] for user [%d]\n",
173 pid, uid);
Michael Halcrow88b4a072007-02-12 00:53:43 -0800174 goto unlock;
175 }
176 if (!ecryptfs_find_daemon_id(uid, &old_id)) {
177 printk(KERN_WARNING "Received request from user [%d] "
178 "to register daemon [%d]; unregistering daemon "
179 "[%d]\n", uid, pid, old_id->pid);
180 hlist_del(&old_id->id_chain);
181 rc = ecryptfs_send_raw_message(transport, ECRYPTFS_NLMSG_QUIT,
182 old_id->pid);
183 if (rc)
184 printk(KERN_WARNING "Failed to send QUIT "
185 "message to daemon [%d]; rc = [%d]\n",
186 old_id->pid, rc);
187 kfree(old_id);
188 }
189 new_id->uid = uid;
190 new_id->pid = pid;
191 hlist_add_head(&new_id->id_chain,
192 &ecryptfs_daemon_id_hash[ecryptfs_uid_hash(uid)]);
193 rc = 0;
194unlock:
195 mutex_unlock(&ecryptfs_daemon_id_hash_mux);
196 return rc;
197}
198
199/**
200 * ecryptfs_process_quit
201 * @uid: The user ID owner of the message
202 * @pid: The process ID for the userspace program that sent the
203 * message
204 *
205 * Deletes the corresponding daemon id for the given uid and pid, if
206 * it is the registered that is requesting the deletion. Returns zero
207 * after deleting the desired daemon id; non-zero otherwise.
208 */
209int ecryptfs_process_quit(uid_t uid, pid_t pid)
210{
211 struct ecryptfs_daemon_id *id;
212 int rc;
213
214 mutex_lock(&ecryptfs_daemon_id_hash_mux);
215 if (ecryptfs_find_daemon_id(uid, &id)) {
216 rc = -EINVAL;
217 ecryptfs_printk(KERN_ERR, "Received request from user [%d] to "
218 "unregister unrecognized daemon [%d]\n", uid,
219 pid);
220 goto unlock;
221 }
222 if (id->pid != pid) {
223 rc = -EINVAL;
224 ecryptfs_printk(KERN_WARNING, "Received request from user [%d] "
225 "with pid [%d] to unregister daemon [%d]\n",
226 uid, pid, id->pid);
227 goto unlock;
228 }
229 hlist_del(&id->id_chain);
230 kfree(id);
231 rc = 0;
232unlock:
233 mutex_unlock(&ecryptfs_daemon_id_hash_mux);
234 return rc;
235}
236
237/**
238 * ecryptfs_process_reponse
239 * @msg: The ecryptfs message received; the caller should sanity check
240 * msg->data_len
241 * @pid: The process ID of the userspace application that sent the
242 * message
243 * @seq: The sequence number of the message
244 *
245 * Processes a response message after sending a operation request to
246 * userspace. Returns zero upon delivery to desired context element;
247 * non-zero upon delivery failure or error.
248 */
Michael Halcrowdddfa462007-02-12 00:53:44 -0800249int ecryptfs_process_response(struct ecryptfs_message *msg, uid_t uid,
250 pid_t pid, u32 seq)
Michael Halcrow88b4a072007-02-12 00:53:43 -0800251{
252 struct ecryptfs_daemon_id *id;
253 struct ecryptfs_msg_ctx *msg_ctx;
254 int msg_size;
255 int rc;
256
257 if (msg->index >= ecryptfs_message_buf_len) {
258 rc = -EINVAL;
259 ecryptfs_printk(KERN_ERR, "Attempt to reference "
260 "context buffer at index [%d]; maximum "
261 "allowable is [%d]\n", msg->index,
262 (ecryptfs_message_buf_len - 1));
263 goto out;
264 }
265 msg_ctx = &ecryptfs_msg_ctx_arr[msg->index];
266 mutex_lock(&msg_ctx->mux);
267 if (ecryptfs_find_daemon_id(msg_ctx->task->euid, &id)) {
268 rc = -EBADMSG;
269 ecryptfs_printk(KERN_WARNING, "User [%d] received a "
270 "message response from process [%d] but does "
271 "not have a registered daemon\n",
272 msg_ctx->task->euid, pid);
273 goto wake_up;
274 }
Michael Halcrowdddfa462007-02-12 00:53:44 -0800275 if (msg_ctx->task->euid != uid) {
276 rc = -EBADMSG;
277 ecryptfs_printk(KERN_WARNING, "Received message from user "
278 "[%d]; expected message from user [%d]\n",
279 uid, msg_ctx->task->euid);
280 goto unlock;
281 }
Michael Halcrow88b4a072007-02-12 00:53:43 -0800282 if (id->pid != pid) {
283 rc = -EBADMSG;
284 ecryptfs_printk(KERN_ERR, "User [%d] received a "
285 "message response from an unrecognized "
286 "process [%d]\n", msg_ctx->task->euid, pid);
287 goto unlock;
288 }
289 if (msg_ctx->state != ECRYPTFS_MSG_CTX_STATE_PENDING) {
290 rc = -EINVAL;
291 ecryptfs_printk(KERN_WARNING, "Desired context element is not "
292 "pending a response\n");
293 goto unlock;
294 } else if (msg_ctx->counter != seq) {
295 rc = -EINVAL;
296 ecryptfs_printk(KERN_WARNING, "Invalid message sequence; "
297 "expected [%d]; received [%d]\n",
298 msg_ctx->counter, seq);
299 goto unlock;
300 }
301 msg_size = sizeof(*msg) + msg->data_len;
302 msg_ctx->msg = kmalloc(msg_size, GFP_KERNEL);
303 if (!msg_ctx->msg) {
304 rc = -ENOMEM;
305 ecryptfs_printk(KERN_ERR, "Failed to allocate memory\n");
306 goto unlock;
307 }
308 memcpy(msg_ctx->msg, msg, msg_size);
309 msg_ctx->state = ECRYPTFS_MSG_CTX_STATE_DONE;
310 rc = 0;
311wake_up:
312 wake_up_process(msg_ctx->task);
313unlock:
314 mutex_unlock(&msg_ctx->mux);
315out:
316 return rc;
317}
318
319/**
320 * ecryptfs_send_message
321 * @transport: The transport over which to send the message (i.e.,
322 * netlink)
323 * @data: The data to send
324 * @data_len: The length of data
325 * @msg_ctx: The message context allocated for the send
326 */
327int ecryptfs_send_message(unsigned int transport, char *data, int data_len,
328 struct ecryptfs_msg_ctx **msg_ctx)
329{
330 struct ecryptfs_daemon_id *id;
331 int rc;
332
333 mutex_lock(&ecryptfs_daemon_id_hash_mux);
334 if (ecryptfs_find_daemon_id(current->euid, &id)) {
335 mutex_unlock(&ecryptfs_daemon_id_hash_mux);
336 rc = -ENOTCONN;
337 ecryptfs_printk(KERN_ERR, "User [%d] does not have a daemon "
338 "registered\n", current->euid);
339 goto out;
340 }
341 mutex_unlock(&ecryptfs_daemon_id_hash_mux);
342 mutex_lock(&ecryptfs_msg_ctx_lists_mux);
343 rc = ecryptfs_acquire_free_msg_ctx(msg_ctx);
344 if (rc) {
345 mutex_unlock(&ecryptfs_msg_ctx_lists_mux);
346 ecryptfs_printk(KERN_WARNING, "Could not claim a free "
347 "context element\n");
348 goto out;
349 }
350 ecryptfs_msg_ctx_free_to_alloc(*msg_ctx);
351 mutex_unlock(&(*msg_ctx)->mux);
352 mutex_unlock(&ecryptfs_msg_ctx_lists_mux);
353 switch (transport) {
354 case ECRYPTFS_TRANSPORT_NETLINK:
355 rc = ecryptfs_send_netlink(data, data_len, *msg_ctx,
356 ECRYPTFS_NLMSG_REQUEST, 0, id->pid);
357 break;
358 case ECRYPTFS_TRANSPORT_CONNECTOR:
359 case ECRYPTFS_TRANSPORT_RELAYFS:
360 default:
361 rc = -ENOSYS;
362 }
363 if (rc) {
364 printk(KERN_ERR "Error attempting to send message to userspace "
365 "daemon; rc = [%d]\n", rc);
366 }
367out:
368 return rc;
369}
370
371/**
372 * ecryptfs_wait_for_response
373 * @msg_ctx: The context that was assigned when sending a message
374 * @msg: The incoming message from userspace; not set if rc != 0
375 *
376 * Sleeps until awaken by ecryptfs_receive_message or until the amount
377 * of time exceeds ecryptfs_message_wait_timeout. If zero is
378 * returned, msg will point to a valid message from userspace; a
379 * non-zero value is returned upon failure to receive a message or an
380 * error occurs.
381 */
382int ecryptfs_wait_for_response(struct ecryptfs_msg_ctx *msg_ctx,
383 struct ecryptfs_message **msg)
384{
385 signed long timeout = ecryptfs_message_wait_timeout * HZ;
386 int rc = 0;
387
388sleep:
389 timeout = schedule_timeout_interruptible(timeout);
390 mutex_lock(&ecryptfs_msg_ctx_lists_mux);
391 mutex_lock(&msg_ctx->mux);
392 if (msg_ctx->state != ECRYPTFS_MSG_CTX_STATE_DONE) {
393 if (timeout) {
394 mutex_unlock(&msg_ctx->mux);
395 mutex_unlock(&ecryptfs_msg_ctx_lists_mux);
396 goto sleep;
397 }
398 rc = -ENOMSG;
399 } else {
400 *msg = msg_ctx->msg;
401 msg_ctx->msg = NULL;
402 }
403 ecryptfs_msg_ctx_alloc_to_free(msg_ctx);
404 mutex_unlock(&msg_ctx->mux);
405 mutex_unlock(&ecryptfs_msg_ctx_lists_mux);
406 return rc;
407}
408
409int ecryptfs_init_messaging(unsigned int transport)
410{
411 int i;
412 int rc = 0;
413
414 if (ecryptfs_number_of_users > ECRYPTFS_MAX_NUM_USERS) {
415 ecryptfs_number_of_users = ECRYPTFS_MAX_NUM_USERS;
416 ecryptfs_printk(KERN_WARNING, "Specified number of users is "
417 "too large, defaulting to [%d] users\n",
418 ecryptfs_number_of_users);
419 }
420 mutex_init(&ecryptfs_daemon_id_hash_mux);
421 mutex_lock(&ecryptfs_daemon_id_hash_mux);
Michael Halcrow5dda6992007-10-16 01:28:06 -0700422 ecryptfs_hash_buckets = 1;
423 while (ecryptfs_number_of_users >> ecryptfs_hash_buckets)
424 ecryptfs_hash_buckets++;
Michael Halcrow88b4a072007-02-12 00:53:43 -0800425 ecryptfs_daemon_id_hash = kmalloc(sizeof(struct hlist_head)
426 * ecryptfs_hash_buckets, GFP_KERNEL);
427 if (!ecryptfs_daemon_id_hash) {
428 rc = -ENOMEM;
429 ecryptfs_printk(KERN_ERR, "Failed to allocate memory\n");
430 goto out;
431 }
432 for (i = 0; i < ecryptfs_hash_buckets; i++)
433 INIT_HLIST_HEAD(&ecryptfs_daemon_id_hash[i]);
434 mutex_unlock(&ecryptfs_daemon_id_hash_mux);
435
436 ecryptfs_msg_ctx_arr = kmalloc((sizeof(struct ecryptfs_msg_ctx)
437 * ecryptfs_message_buf_len), GFP_KERNEL);
438 if (!ecryptfs_msg_ctx_arr) {
439 rc = -ENOMEM;
440 ecryptfs_printk(KERN_ERR, "Failed to allocate memory\n");
441 goto out;
442 }
443 mutex_init(&ecryptfs_msg_ctx_lists_mux);
444 mutex_lock(&ecryptfs_msg_ctx_lists_mux);
445 ecryptfs_msg_counter = 0;
446 for (i = 0; i < ecryptfs_message_buf_len; i++) {
447 INIT_LIST_HEAD(&ecryptfs_msg_ctx_arr[i].node);
448 mutex_init(&ecryptfs_msg_ctx_arr[i].mux);
449 mutex_lock(&ecryptfs_msg_ctx_arr[i].mux);
450 ecryptfs_msg_ctx_arr[i].index = i;
451 ecryptfs_msg_ctx_arr[i].state = ECRYPTFS_MSG_CTX_STATE_FREE;
452 ecryptfs_msg_ctx_arr[i].counter = 0;
453 ecryptfs_msg_ctx_arr[i].task = NULL;
454 ecryptfs_msg_ctx_arr[i].msg = NULL;
455 list_add_tail(&ecryptfs_msg_ctx_arr[i].node,
456 &ecryptfs_msg_ctx_free_list);
457 mutex_unlock(&ecryptfs_msg_ctx_arr[i].mux);
458 }
459 mutex_unlock(&ecryptfs_msg_ctx_lists_mux);
460 switch(transport) {
461 case ECRYPTFS_TRANSPORT_NETLINK:
462 rc = ecryptfs_init_netlink();
463 if (rc)
464 ecryptfs_release_messaging(transport);
465 break;
466 case ECRYPTFS_TRANSPORT_CONNECTOR:
467 case ECRYPTFS_TRANSPORT_RELAYFS:
468 default:
469 rc = -ENOSYS;
470 }
471out:
472 return rc;
473}
474
475void ecryptfs_release_messaging(unsigned int transport)
476{
477 if (ecryptfs_msg_ctx_arr) {
478 int i;
479
480 mutex_lock(&ecryptfs_msg_ctx_lists_mux);
481 for (i = 0; i < ecryptfs_message_buf_len; i++) {
482 mutex_lock(&ecryptfs_msg_ctx_arr[i].mux);
483 if (ecryptfs_msg_ctx_arr[i].msg)
484 kfree(ecryptfs_msg_ctx_arr[i].msg);
485 mutex_unlock(&ecryptfs_msg_ctx_arr[i].mux);
486 }
487 kfree(ecryptfs_msg_ctx_arr);
488 mutex_unlock(&ecryptfs_msg_ctx_lists_mux);
489 }
490 if (ecryptfs_daemon_id_hash) {
491 struct hlist_node *elem;
492 struct ecryptfs_daemon_id *id;
493 int i;
494
495 mutex_lock(&ecryptfs_daemon_id_hash_mux);
496 for (i = 0; i < ecryptfs_hash_buckets; i++) {
497 hlist_for_each_entry(id, elem,
498 &ecryptfs_daemon_id_hash[i],
499 id_chain) {
500 hlist_del(elem);
501 kfree(id);
502 }
503 }
504 kfree(ecryptfs_daemon_id_hash);
505 mutex_unlock(&ecryptfs_daemon_id_hash_mux);
506 }
507 switch(transport) {
508 case ECRYPTFS_TRANSPORT_NETLINK:
509 ecryptfs_release_netlink();
510 break;
511 case ECRYPTFS_TRANSPORT_CONNECTOR:
512 case ECRYPTFS_TRANSPORT_RELAYFS:
513 default:
514 break;
515 }
516 return;
517}