Merge branch 'upstream' of git://ftp.linux-mips.org/pub/scm/upstream-linus
[pandora-kernel.git] / fs / ecryptfs / messaging.c
1 /**
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  */
22
23 #include "ecryptfs_kernel.h"
24
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;
28
29 static struct hlist_head *ecryptfs_daemon_id_hash;
30 static struct mutex ecryptfs_daemon_id_hash_mux;
31 static int ecryptfs_hash_buckets;
32 #define ecryptfs_uid_hash(uid) \
33         hash_long((unsigned long)uid, ecryptfs_hash_buckets)
34
35 static unsigned int ecryptfs_msg_counter;
36 static struct ecryptfs_msg_ctx *ecryptfs_msg_ctx_arr;
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
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  */
47 static 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;
70 out:
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  */
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  *
92  * Be sure to lock the list mutex and the context mutex before
93  * calling.
94  */
95 static 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  */
113 static 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;
127 out:
128         return rc;
129 }
130
131 static 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  */
161 int 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 "
172                                 "to register daemon [%d] for user\n", pid, uid);
173                 goto unlock;
174         }
175         if (!ecryptfs_find_daemon_id(uid, &old_id)) {
176                 printk(KERN_WARNING "Received request from user [%d] "
177                        "to register daemon [%d]; unregistering daemon "
178                        "[%d]\n", uid, pid, old_id->pid);
179                 hlist_del(&old_id->id_chain);
180                 rc = ecryptfs_send_raw_message(transport, ECRYPTFS_NLMSG_QUIT,
181                                                old_id->pid);
182                 if (rc)
183                         printk(KERN_WARNING "Failed to send QUIT "
184                                "message to daemon [%d]; rc = [%d]\n",
185                                old_id->pid, rc);
186                 kfree(old_id);
187         }
188         new_id->uid = uid;
189         new_id->pid = pid;
190         hlist_add_head(&new_id->id_chain,
191                        &ecryptfs_daemon_id_hash[ecryptfs_uid_hash(uid)]);
192         rc = 0;
193 unlock:
194         mutex_unlock(&ecryptfs_daemon_id_hash_mux);
195         return rc;
196 }
197
198 /**
199  * ecryptfs_process_quit
200  * @uid: The user ID owner of the message
201  * @pid: The process ID for the userspace program that sent the
202  *       message
203  *
204  * Deletes the corresponding daemon id for the given uid and pid, if
205  * it is the registered that is requesting the deletion. Returns zero
206  * after deleting the desired daemon id; non-zero otherwise.
207  */
208 int ecryptfs_process_quit(uid_t uid, pid_t pid)
209 {
210         struct ecryptfs_daemon_id *id;
211         int rc;
212
213         mutex_lock(&ecryptfs_daemon_id_hash_mux);
214         if (ecryptfs_find_daemon_id(uid, &id)) {
215                 rc = -EINVAL;
216                 ecryptfs_printk(KERN_ERR, "Received request from user [%d] to "
217                                 "unregister unrecognized daemon [%d]\n", uid,
218                                 pid);
219                 goto unlock;
220         }
221         if (id->pid != pid) {
222                 rc = -EINVAL;
223                 ecryptfs_printk(KERN_WARNING, "Received request from user [%d] "
224                                 "with pid [%d] to unregister daemon [%d]\n",
225                                 uid, pid, id->pid);
226                 goto unlock;
227         }
228         hlist_del(&id->id_chain);
229         kfree(id);
230         rc = 0;
231 unlock:
232         mutex_unlock(&ecryptfs_daemon_id_hash_mux);
233         return rc;
234 }
235
236 /**
237  * ecryptfs_process_reponse
238  * @msg: The ecryptfs message received; the caller should sanity check
239  *       msg->data_len
240  * @pid: The process ID of the userspace application that sent the
241  *       message
242  * @seq: The sequence number of the message
243  *
244  * Processes a response message after sending a operation request to
245  * userspace. Returns zero upon delivery to desired context element;
246  * non-zero upon delivery failure or error.
247  */
248 int ecryptfs_process_response(struct ecryptfs_message *msg, uid_t uid,
249                               pid_t pid, u32 seq)
250 {
251         struct ecryptfs_daemon_id *id;
252         struct ecryptfs_msg_ctx *msg_ctx;
253         int msg_size;
254         int rc;
255
256         if (msg->index >= ecryptfs_message_buf_len) {
257                 rc = -EINVAL;
258                 ecryptfs_printk(KERN_ERR, "Attempt to reference "
259                                 "context buffer at index [%d]; maximum "
260                                 "allowable is [%d]\n", msg->index,
261                                 (ecryptfs_message_buf_len - 1));
262                 goto out;
263         }
264         msg_ctx = &ecryptfs_msg_ctx_arr[msg->index];
265         mutex_lock(&msg_ctx->mux);
266         if (ecryptfs_find_daemon_id(msg_ctx->task->euid, &id)) {
267                 rc = -EBADMSG;
268                 ecryptfs_printk(KERN_WARNING, "User [%d] received a "
269                                 "message response from process [%d] but does "
270                                 "not have a registered daemon\n",
271                                 msg_ctx->task->euid, pid);
272                 goto wake_up;
273         }
274         if (msg_ctx->task->euid != uid) {
275                 rc = -EBADMSG;
276                 ecryptfs_printk(KERN_WARNING, "Received message from user "
277                                 "[%d]; expected message from user [%d]\n",
278                                 uid, msg_ctx->task->euid);
279                 goto unlock;
280         }
281         if (id->pid != pid) {
282                 rc = -EBADMSG;
283                 ecryptfs_printk(KERN_ERR, "User [%d] received a "
284                                 "message response from an unrecognized "
285                                 "process [%d]\n", msg_ctx->task->euid, pid);
286                 goto unlock;
287         }
288         if (msg_ctx->state != ECRYPTFS_MSG_CTX_STATE_PENDING) {
289                 rc = -EINVAL;
290                 ecryptfs_printk(KERN_WARNING, "Desired context element is not "
291                                 "pending a response\n");
292                 goto unlock;
293         } else if (msg_ctx->counter != seq) {
294                 rc = -EINVAL;
295                 ecryptfs_printk(KERN_WARNING, "Invalid message sequence; "
296                                 "expected [%d]; received [%d]\n",
297                                 msg_ctx->counter, seq);
298                 goto unlock;
299         }
300         msg_size = sizeof(*msg) + msg->data_len;
301         msg_ctx->msg = kmalloc(msg_size, GFP_KERNEL);
302         if (!msg_ctx->msg) {
303                 rc = -ENOMEM;
304                 ecryptfs_printk(KERN_ERR, "Failed to allocate memory\n");
305                 goto unlock;
306         }
307         memcpy(msg_ctx->msg, msg, msg_size);
308         msg_ctx->state = ECRYPTFS_MSG_CTX_STATE_DONE;
309         rc = 0;
310 wake_up:
311         wake_up_process(msg_ctx->task);
312 unlock:
313         mutex_unlock(&msg_ctx->mux);
314 out:
315         return rc;
316 }
317
318 /**
319  * ecryptfs_send_message
320  * @transport: The transport over which to send the message (i.e.,
321  *             netlink)
322  * @data: The data to send
323  * @data_len: The length of data
324  * @msg_ctx: The message context allocated for the send
325  */
326 int ecryptfs_send_message(unsigned int transport, char *data, int data_len,
327                           struct ecryptfs_msg_ctx **msg_ctx)
328 {
329         struct ecryptfs_daemon_id *id;
330         int rc;
331
332         mutex_lock(&ecryptfs_daemon_id_hash_mux);
333         if (ecryptfs_find_daemon_id(current->euid, &id)) {
334                 mutex_unlock(&ecryptfs_daemon_id_hash_mux);
335                 rc = -ENOTCONN;
336                 ecryptfs_printk(KERN_ERR, "User [%d] does not have a daemon "
337                                 "registered\n", current->euid);
338                 goto out;
339         }
340         mutex_unlock(&ecryptfs_daemon_id_hash_mux);
341         mutex_lock(&ecryptfs_msg_ctx_lists_mux);
342         rc = ecryptfs_acquire_free_msg_ctx(msg_ctx);
343         if (rc) {
344                 mutex_unlock(&ecryptfs_msg_ctx_lists_mux);
345                 ecryptfs_printk(KERN_WARNING, "Could not claim a free "
346                                 "context element\n");
347                 goto out;
348         }
349         ecryptfs_msg_ctx_free_to_alloc(*msg_ctx);
350         mutex_unlock(&(*msg_ctx)->mux);
351         mutex_unlock(&ecryptfs_msg_ctx_lists_mux);
352         switch (transport) {
353         case ECRYPTFS_TRANSPORT_NETLINK:
354                 rc = ecryptfs_send_netlink(data, data_len, *msg_ctx,
355                                            ECRYPTFS_NLMSG_REQUEST, 0, id->pid);
356                 break;
357         case ECRYPTFS_TRANSPORT_CONNECTOR:
358         case ECRYPTFS_TRANSPORT_RELAYFS:
359         default:
360                 rc = -ENOSYS;
361         }
362         if (rc) {
363                 printk(KERN_ERR "Error attempting to send message to userspace "
364                        "daemon; rc = [%d]\n", rc);
365         }
366 out:
367         return rc;
368 }
369
370 /**
371  * ecryptfs_wait_for_response
372  * @msg_ctx: The context that was assigned when sending a message
373  * @msg: The incoming message from userspace; not set if rc != 0
374  *
375  * Sleeps until awaken by ecryptfs_receive_message or until the amount
376  * of time exceeds ecryptfs_message_wait_timeout.  If zero is
377  * returned, msg will point to a valid message from userspace; a
378  * non-zero value is returned upon failure to receive a message or an
379  * error occurs.
380  */
381 int ecryptfs_wait_for_response(struct ecryptfs_msg_ctx *msg_ctx,
382                                struct ecryptfs_message **msg)
383 {
384         signed long timeout = ecryptfs_message_wait_timeout * HZ;
385         int rc = 0;
386
387 sleep:
388         timeout = schedule_timeout_interruptible(timeout);
389         mutex_lock(&ecryptfs_msg_ctx_lists_mux);
390         mutex_lock(&msg_ctx->mux);
391         if (msg_ctx->state != ECRYPTFS_MSG_CTX_STATE_DONE) {
392                 if (timeout) {
393                         mutex_unlock(&msg_ctx->mux);
394                         mutex_unlock(&ecryptfs_msg_ctx_lists_mux);
395                         goto sleep;
396                 }
397                 rc = -ENOMSG;
398         } else {
399                 *msg = msg_ctx->msg;
400                 msg_ctx->msg = NULL;
401         }
402         ecryptfs_msg_ctx_alloc_to_free(msg_ctx);
403         mutex_unlock(&msg_ctx->mux);
404         mutex_unlock(&ecryptfs_msg_ctx_lists_mux);
405         return rc;
406 }
407
408 int ecryptfs_init_messaging(unsigned int transport)
409 {
410         int i;
411         int rc = 0;
412
413         if (ecryptfs_number_of_users > ECRYPTFS_MAX_NUM_USERS) {
414                 ecryptfs_number_of_users = ECRYPTFS_MAX_NUM_USERS;
415                 ecryptfs_printk(KERN_WARNING, "Specified number of users is "
416                                 "too large, defaulting to [%d] users\n",
417                                 ecryptfs_number_of_users);
418         }
419         mutex_init(&ecryptfs_daemon_id_hash_mux);
420         mutex_lock(&ecryptfs_daemon_id_hash_mux);
421         ecryptfs_hash_buckets = 0;
422         while (ecryptfs_number_of_users >> ++ecryptfs_hash_buckets);
423         ecryptfs_daemon_id_hash = kmalloc(sizeof(struct hlist_head)
424                                           * ecryptfs_hash_buckets, GFP_KERNEL);
425         if (!ecryptfs_daemon_id_hash) {
426                 rc = -ENOMEM;
427                 ecryptfs_printk(KERN_ERR, "Failed to allocate memory\n");
428                 goto out;
429         }
430         for (i = 0; i < ecryptfs_hash_buckets; i++)
431                 INIT_HLIST_HEAD(&ecryptfs_daemon_id_hash[i]);
432         mutex_unlock(&ecryptfs_daemon_id_hash_mux);
433
434         ecryptfs_msg_ctx_arr = kmalloc((sizeof(struct ecryptfs_msg_ctx)
435                                       * ecryptfs_message_buf_len), GFP_KERNEL);
436         if (!ecryptfs_msg_ctx_arr) {
437                 rc = -ENOMEM;
438                 ecryptfs_printk(KERN_ERR, "Failed to allocate memory\n");
439                 goto out;
440         }
441         mutex_init(&ecryptfs_msg_ctx_lists_mux);
442         mutex_lock(&ecryptfs_msg_ctx_lists_mux);
443         ecryptfs_msg_counter = 0;
444         for (i = 0; i < ecryptfs_message_buf_len; i++) {
445                 INIT_LIST_HEAD(&ecryptfs_msg_ctx_arr[i].node);
446                 mutex_init(&ecryptfs_msg_ctx_arr[i].mux);
447                 mutex_lock(&ecryptfs_msg_ctx_arr[i].mux);
448                 ecryptfs_msg_ctx_arr[i].index = i;
449                 ecryptfs_msg_ctx_arr[i].state = ECRYPTFS_MSG_CTX_STATE_FREE;
450                 ecryptfs_msg_ctx_arr[i].counter = 0;
451                 ecryptfs_msg_ctx_arr[i].task = NULL;
452                 ecryptfs_msg_ctx_arr[i].msg = NULL;
453                 list_add_tail(&ecryptfs_msg_ctx_arr[i].node,
454                               &ecryptfs_msg_ctx_free_list);
455                 mutex_unlock(&ecryptfs_msg_ctx_arr[i].mux);
456         }
457         mutex_unlock(&ecryptfs_msg_ctx_lists_mux);
458         switch(transport) {
459         case ECRYPTFS_TRANSPORT_NETLINK:
460                 rc = ecryptfs_init_netlink();
461                 if (rc)
462                         ecryptfs_release_messaging(transport);
463                 break;
464         case ECRYPTFS_TRANSPORT_CONNECTOR:
465         case ECRYPTFS_TRANSPORT_RELAYFS:
466         default:
467                 rc = -ENOSYS;
468         }
469 out:
470         return rc;
471 }
472
473 void ecryptfs_release_messaging(unsigned int transport)
474 {
475         if (ecryptfs_msg_ctx_arr) {
476                 int i;
477
478                 mutex_lock(&ecryptfs_msg_ctx_lists_mux);
479                 for (i = 0; i < ecryptfs_message_buf_len; i++) {
480                         mutex_lock(&ecryptfs_msg_ctx_arr[i].mux);
481                         if (ecryptfs_msg_ctx_arr[i].msg)
482                                 kfree(ecryptfs_msg_ctx_arr[i].msg);
483                         mutex_unlock(&ecryptfs_msg_ctx_arr[i].mux);
484                 }
485                 kfree(ecryptfs_msg_ctx_arr);
486                 mutex_unlock(&ecryptfs_msg_ctx_lists_mux);
487         }
488         if (ecryptfs_daemon_id_hash) {
489                 struct hlist_node *elem;
490                 struct ecryptfs_daemon_id *id;
491                 int i;
492
493                 mutex_lock(&ecryptfs_daemon_id_hash_mux);
494                 for (i = 0; i < ecryptfs_hash_buckets; i++) {
495                         hlist_for_each_entry(id, elem,
496                                              &ecryptfs_daemon_id_hash[i],
497                                              id_chain) {
498                                 hlist_del(elem);
499                                 kfree(id);
500                         }
501                 }
502                 kfree(ecryptfs_daemon_id_hash);
503                 mutex_unlock(&ecryptfs_daemon_id_hash_mux);
504         }
505         switch(transport) {
506         case ECRYPTFS_TRANSPORT_NETLINK:
507                 ecryptfs_release_netlink();
508                 break;
509         case ECRYPTFS_TRANSPORT_CONNECTOR:
510         case ECRYPTFS_TRANSPORT_RELAYFS:
511         default:
512                 break;
513         }
514         return;
515 }