blob: c6038bd60897334158441586261758c5f5e195e8 [file] [log] [blame]
Michael Halcrow88b4a072007-02-12 00:53:43 -08001/**
2 * eCryptfs: Linux filesystem encryption layer
3 *
Michael Halcrowf66e8832008-04-29 00:59:51 -07004 * Copyright (C) 2004-2008 International Business Machines Corp.
Michael Halcrow88b4a072007-02-12 00:53:43 -08005 * 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 Halcrowf66e8832008-04-29 00:59:51 -070029static struct hlist_head *ecryptfs_daemon_hash;
30struct mutex ecryptfs_daemon_hash_mux;
Michael Halcrowdd2a3b72007-02-12 00:53:46 -080031static 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 Halcrowf66e8832008-04-29 00:59:51 -070035static u32 ecryptfs_msg_counter;
Michael Halcrowdd2a3b72007-02-12 00:53:46 -080036static 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
Michael Halcrowf66e8832008-04-29 00:59:51 -070043 * 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 Halcrow88b4a072007-02-12 00:53:43 -080047 */
48static 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 Halcrowf66e8832008-04-29 00:59:51 -070054 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 Halcrow88b4a072007-02-12 00:53:43 -080059 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;
71out:
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 Halcrowf66e8832008-04-29 00:59:51 -070079 * Must be called with ecryptfs_msg_ctx_lists_mux held.
Michael Halcrow88b4a072007-02-12 00:53:43 -080080 */
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 *
Michael Halcrowf66e8832008-04-29 00:59:51 -070092 * Must be called with ecryptfs_msg_ctx_lists_mux held.
Michael Halcrow88b4a072007-02-12 00:53:43 -080093 */
Michael Halcrowf66e8832008-04-29 00:59:51 -070094void ecryptfs_msg_ctx_alloc_to_free(struct ecryptfs_msg_ctx *msg_ctx)
Michael Halcrow88b4a072007-02-12 00:53:43 -080095{
96 list_move(&(msg_ctx->node), &ecryptfs_msg_ctx_free_list);
97 if (msg_ctx->msg)
98 kfree(msg_ctx->msg);
Michael Halcrowf66e8832008-04-29 00:59:51 -070099 msg_ctx->msg = NULL;
Michael Halcrow88b4a072007-02-12 00:53:43 -0800100 msg_ctx->state = ECRYPTFS_MSG_CTX_STATE_FREE;
101}
102
103/**
Michael Halcrowf66e8832008-04-29 00:59:51 -0700104 * 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 Halcrow88b4a072007-02-12 00:53:43 -0800107 *
Michael Halcrowf66e8832008-04-29 00:59:51 -0700108 * 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 Halcrow88b4a072007-02-12 00:53:43 -0800113 */
Michael Halcrowf66e8832008-04-29 00:59:51 -0700114int ecryptfs_find_daemon_by_euid(struct ecryptfs_daemon **daemon, uid_t euid)
Michael Halcrow88b4a072007-02-12 00:53:43 -0800115{
116 struct hlist_node *elem;
117 int rc;
118
Michael Halcrowf66e8832008-04-29 00:59:51 -0700119 hlist_for_each_entry(*daemon, elem,
120 &ecryptfs_daemon_hash[ecryptfs_uid_hash(euid)],
121 euid_chain) {
122 if ((*daemon)->euid == euid) {
Michael Halcrow88b4a072007-02-12 00:53:43 -0800123 rc = 0;
124 goto out;
125 }
126 }
127 rc = -EINVAL;
128out:
129 return rc;
130}
131
Michael Halcrowf66e8832008-04-29 00:59:51 -0700132static int
133ecryptfs_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 */
149static int ecryptfs_send_raw_message(unsigned int transport, u8 msg_type,
150 struct ecryptfs_daemon *daemon)
Michael Halcrow88b4a072007-02-12 00:53:43 -0800151{
Michael Halcrowf66e8832008-04-29 00:59:51 -0700152 struct ecryptfs_msg_ctx *msg_ctx;
Michael Halcrow88b4a072007-02-12 00:53:43 -0800153 int rc;
154
155 switch(transport) {
156 case ECRYPTFS_TRANSPORT_NETLINK:
Michael Halcrowf66e8832008-04-29 00:59:51 -0700157 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 Halcrow88b4a072007-02-12 00:53:43 -0800175 break;
176 case ECRYPTFS_TRANSPORT_CONNECTOR:
177 case ECRYPTFS_TRANSPORT_RELAYFS:
178 default:
179 rc = -ENOSYS;
180 }
Michael Halcrowf66e8832008-04-29 00:59:51 -0700181out:
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 */
196int
197ecryptfs_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)]);
217out:
Michael Halcrow88b4a072007-02-12 00:53:43 -0800218 return rc;
219}
220
221/**
222 * ecryptfs_process_helo
223 * @transport: The underlying transport (netlink, etc.)
Michael Halcrowf66e8832008-04-29 00:59:51 -0700224 * @euid: The user ID owner of the message
Michael Halcrow88b4a072007-02-12 00:53:43 -0800225 * @pid: The process ID for the userspace program that sent the
226 * message
227 *
Michael Halcrowf66e8832008-04-29 00:59:51 -0700228 * Adds the euid and pid values to the daemon euid hash. If an euid
Michael Halcrow88b4a072007-02-12 00:53:43 -0800229 * already has a daemon pid registered, the daemon will be
Michael Halcrowf66e8832008-04-29 00:59:51 -0700230 * unregistered before the new daemon is put into the hash list.
231 * Returns zero after adding a new daemon to the hash list;
Michael Halcrow88b4a072007-02-12 00:53:43 -0800232 * non-zero otherwise.
233 */
Michael Halcrowf66e8832008-04-29 00:59:51 -0700234int ecryptfs_process_helo(unsigned int transport, uid_t euid, pid_t pid)
Michael Halcrow88b4a072007-02-12 00:53:43 -0800235{
Michael Halcrowf66e8832008-04-29 00:59:51 -0700236 struct ecryptfs_daemon *new_daemon;
237 struct ecryptfs_daemon *old_daemon;
Michael Halcrow88b4a072007-02-12 00:53:43 -0800238 int rc;
239
Michael Halcrowf66e8832008-04-29 00:59:51 -0700240 mutex_lock(&ecryptfs_daemon_hash_mux);
241 rc = ecryptfs_find_daemon_by_euid(&old_daemon, euid);
242 if (rc != 0) {
Michael Halcrow88b4a072007-02-12 00:53:43 -0800243 printk(KERN_WARNING "Received request from user [%d] "
244 "to register daemon [%d]; unregistering daemon "
Michael Halcrowf66e8832008-04-29 00:59:51 -0700245 "[%d]\n", euid, pid, old_daemon->pid);
246 rc = ecryptfs_send_raw_message(transport, ECRYPTFS_MSG_QUIT,
247 old_daemon);
Michael Halcrow88b4a072007-02-12 00:53:43 -0800248 if (rc)
249 printk(KERN_WARNING "Failed to send QUIT "
250 "message to daemon [%d]; rc = [%d]\n",
Michael Halcrowf66e8832008-04-29 00:59:51 -0700251 old_daemon->pid, rc);
252 hlist_del(&old_daemon->euid_chain);
253 kfree(old_daemon);
Michael Halcrow88b4a072007-02-12 00:53:43 -0800254 }
Michael Halcrowf66e8832008-04-29 00:59:51 -0700255 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 */
270int 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);
299out:
Michael Halcrow88b4a072007-02-12 00:53:43 -0800300 return rc;
301}
302
303/**
304 * ecryptfs_process_quit
Michael Halcrowf66e8832008-04-29 00:59:51 -0700305 * @euid: The user ID owner of the message
Michael Halcrow88b4a072007-02-12 00:53:43 -0800306 * @pid: The process ID for the userspace program that sent the
307 * message
308 *
Michael Halcrowf66e8832008-04-29 00:59:51 -0700309 * Deletes the corresponding daemon for the given euid and pid, if
Michael Halcrow88b4a072007-02-12 00:53:43 -0800310 * it is the registered that is requesting the deletion. Returns zero
Michael Halcrowf66e8832008-04-29 00:59:51 -0700311 * after deleting the desired daemon; non-zero otherwise.
Michael Halcrow88b4a072007-02-12 00:53:43 -0800312 */
Michael Halcrowf66e8832008-04-29 00:59:51 -0700313int ecryptfs_process_quit(uid_t euid, pid_t pid)
Michael Halcrow88b4a072007-02-12 00:53:43 -0800314{
Michael Halcrowf66e8832008-04-29 00:59:51 -0700315 struct ecryptfs_daemon *daemon;
Michael Halcrow88b4a072007-02-12 00:53:43 -0800316 int rc;
317
Michael Halcrowf66e8832008-04-29 00:59:51 -0700318 mutex_lock(&ecryptfs_daemon_hash_mux);
319 rc = ecryptfs_find_daemon_by_euid(&daemon, euid);
320 if (rc || !daemon) {
Michael Halcrow88b4a072007-02-12 00:53:43 -0800321 rc = -EINVAL;
Michael Halcrowf66e8832008-04-29 00:59:51 -0700322 printk(KERN_ERR "Received request from user [%d] to "
323 "unregister unrecognized daemon [%d]\n", euid, pid);
324 goto out_unlock;
Michael Halcrow88b4a072007-02-12 00:53:43 -0800325 }
Michael Halcrowf66e8832008-04-29 00:59:51 -0700326 rc = ecryptfs_exorcise_daemon(daemon);
327out_unlock:
328 mutex_unlock(&ecryptfs_daemon_hash_mux);
Michael Halcrow88b4a072007-02-12 00:53:43 -0800329 return rc;
330}
331
332/**
333 * ecryptfs_process_reponse
334 * @msg: The ecryptfs message received; the caller should sanity check
Michael Halcrowf66e8832008-04-29 00:59:51 -0700335 * msg->data_len and free the memory
Michael Halcrow88b4a072007-02-12 00:53:43 -0800336 * @pid: The process ID of the userspace application that sent the
337 * message
Michael Halcrowf66e8832008-04-29 00:59:51 -0700338 * @seq: The sequence number of the message; must match the sequence
339 * number for the existing message context waiting for this
340 * response
Michael Halcrow88b4a072007-02-12 00:53:43 -0800341 *
Michael Halcrowf66e8832008-04-29 00:59:51 -0700342 * 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 Halcrow88b4a072007-02-12 00:53:43 -0800355 */
Michael Halcrowf66e8832008-04-29 00:59:51 -0700356int ecryptfs_process_response(struct ecryptfs_message *msg, uid_t euid,
Michael Halcrowdddfa462007-02-12 00:53:44 -0800357 pid_t pid, u32 seq)
Michael Halcrow88b4a072007-02-12 00:53:43 -0800358{
Michael Halcrowf66e8832008-04-29 00:59:51 -0700359 struct ecryptfs_daemon *daemon;
Michael Halcrow88b4a072007-02-12 00:53:43 -0800360 struct ecryptfs_msg_ctx *msg_ctx;
Michael Halcrowf66e8832008-04-29 00:59:51 -0700361 size_t msg_size;
Michael Halcrow88b4a072007-02-12 00:53:43 -0800362 int rc;
363
364 if (msg->index >= ecryptfs_message_buf_len) {
365 rc = -EINVAL;
Michael Halcrowf66e8832008-04-29 00:59:51 -0700366 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 Halcrow88b4a072007-02-12 00:53:43 -0800370 goto out;
371 }
372 msg_ctx = &ecryptfs_msg_ctx_arr[msg->index];
373 mutex_lock(&msg_ctx->mux);
Michael Halcrowf66e8832008-04-29 00:59:51 -0700374 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 Halcrow88b4a072007-02-12 00:53:43 -0800378 rc = -EBADMSG;
Michael Halcrowf66e8832008-04-29 00:59:51 -0700379 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 Halcrow88b4a072007-02-12 00:53:43 -0800383 goto wake_up;
384 }
Michael Halcrowf66e8832008-04-29 00:59:51 -0700385 if (msg_ctx->task->euid != euid) {
Michael Halcrowdddfa462007-02-12 00:53:44 -0800386 rc = -EBADMSG;
Michael Halcrowf66e8832008-04-29 00:59:51 -0700387 printk(KERN_WARNING "%s: Received message from user "
388 "[%d]; expected message from user [%d]\n", __func__,
389 euid, msg_ctx->task->euid);
Michael Halcrowdddfa462007-02-12 00:53:44 -0800390 goto unlock;
391 }
Michael Halcrowf66e8832008-04-29 00:59:51 -0700392 if (daemon->pid != pid) {
Michael Halcrow88b4a072007-02-12 00:53:43 -0800393 rc = -EBADMSG;
Michael Halcrowf66e8832008-04-29 00:59:51 -0700394 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 Halcrow88b4a072007-02-12 00:53:43 -0800397 goto unlock;
398 }
399 if (msg_ctx->state != ECRYPTFS_MSG_CTX_STATE_PENDING) {
400 rc = -EINVAL;
Michael Halcrowf66e8832008-04-29 00:59:51 -0700401 printk(KERN_WARNING "%s: Desired context element is not "
402 "pending a response\n", __func__);
Michael Halcrow88b4a072007-02-12 00:53:43 -0800403 goto unlock;
404 } else if (msg_ctx->counter != seq) {
405 rc = -EINVAL;
Michael Halcrowf66e8832008-04-29 00:59:51 -0700406 printk(KERN_WARNING "%s: Invalid message sequence; "
407 "expected [%d]; received [%d]\n", __func__,
408 msg_ctx->counter, seq);
Michael Halcrow88b4a072007-02-12 00:53:43 -0800409 goto unlock;
410 }
Michael Halcrowf66e8832008-04-29 00:59:51 -0700411 msg_size = (sizeof(*msg) + msg->data_len);
Michael Halcrow88b4a072007-02-12 00:53:43 -0800412 msg_ctx->msg = kmalloc(msg_size, GFP_KERNEL);
413 if (!msg_ctx->msg) {
414 rc = -ENOMEM;
Michael Halcrowf66e8832008-04-29 00:59:51 -0700415 printk(KERN_ERR "%s: Failed to allocate [%Zd] bytes of "
416 "GFP_KERNEL memory\n", __func__, msg_size);
Michael Halcrow88b4a072007-02-12 00:53:43 -0800417 goto unlock;
418 }
419 memcpy(msg_ctx->msg, msg, msg_size);
420 msg_ctx->state = ECRYPTFS_MSG_CTX_STATE_DONE;
421 rc = 0;
422wake_up:
423 wake_up_process(msg_ctx->task);
424unlock:
425 mutex_unlock(&msg_ctx->mux);
426out:
427 return rc;
428}
429
430/**
Michael Halcrowf66e8832008-04-29 00:59:51 -0700431 * ecryptfs_send_message_locked
Michael Halcrow88b4a072007-02-12 00:53:43 -0800432 * @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 Halcrowf66e8832008-04-29 00:59:51 -0700437 *
438 * Must be called with ecryptfs_daemon_hash_mux held.
439 *
440 * Returns zero on success; non-zero otherwise
Michael Halcrow88b4a072007-02-12 00:53:43 -0800441 */
Michael Halcrowf66e8832008-04-29 00:59:51 -0700442static int
443ecryptfs_send_message_locked(unsigned int transport, char *data, int data_len,
444 u8 msg_type, struct ecryptfs_msg_ctx **msg_ctx)
Michael Halcrow88b4a072007-02-12 00:53:43 -0800445{
Michael Halcrowf66e8832008-04-29 00:59:51 -0700446 struct ecryptfs_daemon *daemon;
Michael Halcrow88b4a072007-02-12 00:53:43 -0800447 int rc;
448
Michael Halcrowf66e8832008-04-29 00:59:51 -0700449 rc = ecryptfs_find_daemon_by_euid(&daemon, current->euid);
450 if (rc || !daemon) {
Michael Halcrow88b4a072007-02-12 00:53:43 -0800451 rc = -ENOTCONN;
Michael Halcrowf66e8832008-04-29 00:59:51 -0700452 printk(KERN_ERR "%s: User [%d] does not have a daemon "
453 "registered\n", __func__, current->euid);
Michael Halcrow88b4a072007-02-12 00:53:43 -0800454 goto out;
455 }
Michael Halcrow88b4a072007-02-12 00:53:43 -0800456 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 Halcrowf66e8832008-04-29 00:59:51 -0700460 printk(KERN_WARNING "%s: Could not claim a free "
461 "context element\n", __func__);
Michael Halcrow88b4a072007-02-12 00:53:43 -0800462 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 Halcrowf66e8832008-04-29 00:59:51 -0700469 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 Halcrow88b4a072007-02-12 00:53:43 -0800475 break;
476 case ECRYPTFS_TRANSPORT_CONNECTOR:
477 case ECRYPTFS_TRANSPORT_RELAYFS:
478 default:
479 rc = -ENOSYS;
480 }
Michael Halcrowf66e8832008-04-29 00:59:51 -0700481 if (rc)
482 printk(KERN_ERR "%s: Error attempting to send message to "
483 "userspace daemon; rc = [%d]\n", __func__, rc);
Michael Halcrow88b4a072007-02-12 00:53:43 -0800484out:
485 return rc;
486}
487
488/**
Michael Halcrowf66e8832008-04-29 00:59:51 -0700489 * 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 */
500int 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 Halcrow88b4a072007-02-12 00:53:43 -0800513 * 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 Halcrowf66e8832008-04-29 00:59:51 -0700521 * error occurs. Callee must free @msg on success.
Michael Halcrow88b4a072007-02-12 00:53:43 -0800522 */
523int 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
529sleep:
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
550int 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 Halcrowf66e8832008-04-29 00:59:51 -0700557 printk(KERN_WARNING "%s: Specified number of users is "
558 "too large, defaulting to [%d] users\n", __func__,
559 ecryptfs_number_of_users);
Michael Halcrow88b4a072007-02-12 00:53:43 -0800560 }
Michael Halcrowf66e8832008-04-29 00:59:51 -0700561 mutex_init(&ecryptfs_daemon_hash_mux);
562 mutex_lock(&ecryptfs_daemon_hash_mux);
Michael Halcrow5dda6992007-10-16 01:28:06 -0700563 ecryptfs_hash_buckets = 1;
564 while (ecryptfs_number_of_users >> ecryptfs_hash_buckets)
565 ecryptfs_hash_buckets++;
Michael Halcrowf66e8832008-04-29 00:59:51 -0700566 ecryptfs_daemon_hash = kmalloc((sizeof(struct hlist_head)
567 * ecryptfs_hash_buckets), GFP_KERNEL);
568 if (!ecryptfs_daemon_hash) {
Michael Halcrow88b4a072007-02-12 00:53:43 -0800569 rc = -ENOMEM;
Michael Halcrowf66e8832008-04-29 00:59:51 -0700570 printk(KERN_ERR "%s: Failed to allocate memory\n", __func__);
571 mutex_unlock(&ecryptfs_daemon_hash_mux);
Michael Halcrow88b4a072007-02-12 00:53:43 -0800572 goto out;
573 }
574 for (i = 0; i < ecryptfs_hash_buckets; i++)
Michael Halcrowf66e8832008-04-29 00:59:51 -0700575 INIT_HLIST_HEAD(&ecryptfs_daemon_hash[i]);
576 mutex_unlock(&ecryptfs_daemon_hash_mux);
Michael Halcrow88b4a072007-02-12 00:53:43 -0800577 ecryptfs_msg_ctx_arr = kmalloc((sizeof(struct ecryptfs_msg_ctx)
Michael Halcrowf66e8832008-04-29 00:59:51 -0700578 * ecryptfs_message_buf_len),
579 GFP_KERNEL);
Michael Halcrow88b4a072007-02-12 00:53:43 -0800580 if (!ecryptfs_msg_ctx_arr) {
581 rc = -ENOMEM;
Michael Halcrowf66e8832008-04-29 00:59:51 -0700582 printk(KERN_ERR "%s: Failed to allocate memory\n", __func__);
Michael Halcrow88b4a072007-02-12 00:53:43 -0800583 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 Halcrowf66e8832008-04-29 00:59:51 -0700590 INIT_LIST_HEAD(&ecryptfs_msg_ctx_arr[i].daemon_out_list);
Michael Halcrow88b4a072007-02-12 00:53:43 -0800591 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 Halcrowf66e8832008-04-29 00:59:51 -0700609 case ECRYPTFS_TRANSPORT_MISCDEV:
610 rc = ecryptfs_init_ecryptfs_miscdev();
611 if (rc)
612 ecryptfs_release_messaging(transport);
613 break;
Michael Halcrow88b4a072007-02-12 00:53:43 -0800614 case ECRYPTFS_TRANSPORT_CONNECTOR:
615 case ECRYPTFS_TRANSPORT_RELAYFS:
616 default:
617 rc = -ENOSYS;
618 }
619out:
620 return rc;
621}
622
623void 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 Halcrowf66e8832008-04-29 00:59:51 -0700638 if (ecryptfs_daemon_hash) {
Michael Halcrow88b4a072007-02-12 00:53:43 -0800639 struct hlist_node *elem;
Michael Halcrowf66e8832008-04-29 00:59:51 -0700640 struct ecryptfs_daemon *daemon;
Michael Halcrow88b4a072007-02-12 00:53:43 -0800641 int i;
642
Michael Halcrowf66e8832008-04-29 00:59:51 -0700643 mutex_lock(&ecryptfs_daemon_hash_mux);
Michael Halcrow88b4a072007-02-12 00:53:43 -0800644 for (i = 0; i < ecryptfs_hash_buckets; i++) {
Michael Halcrowf66e8832008-04-29 00:59:51 -0700645 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 Halcrow88b4a072007-02-12 00:53:43 -0800657 }
658 }
Michael Halcrowf66e8832008-04-29 00:59:51 -0700659 kfree(ecryptfs_daemon_hash);
660 mutex_unlock(&ecryptfs_daemon_hash_mux);
Michael Halcrow88b4a072007-02-12 00:53:43 -0800661 }
662 switch(transport) {
663 case ECRYPTFS_TRANSPORT_NETLINK:
664 ecryptfs_release_netlink();
665 break;
Michael Halcrowf66e8832008-04-29 00:59:51 -0700666 case ECRYPTFS_TRANSPORT_MISCDEV:
667 ecryptfs_destroy_ecryptfs_miscdev();
668 break;
Michael Halcrow88b4a072007-02-12 00:53:43 -0800669 case ECRYPTFS_TRANSPORT_CONNECTOR:
670 case ECRYPTFS_TRANSPORT_RELAYFS:
671 default:
672 break;
673 }
674 return;
675}