2 * eCryptfs: Linux filesystem encryption layer
4 * Copyright (C) 2008 International Business Machines Corp.
5 * Author(s): Michael A. Halcrow <mhalcrow@us.ibm.com>
7 * This program is free software; you can redistribute it and/or
8 * modify it under the terms of the GNU General Public License version
9 * 2 as published by the Free Software Foundation.
11 * This program is distributed in the hope that it will be useful, but
12 * WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * General Public License for more details.
16 * You should have received a copy of the GNU General Public License
17 * along with this program; if not, write to the Free Software
18 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA
23 #include <linux/hash.h>
24 #include <linux/random.h>
25 #include <linux/miscdevice.h>
26 #include <linux/poll.h>
27 #include <linux/wait.h>
28 #include <linux/module.h>
29 #include "ecryptfs_kernel.h"
31 static atomic_t ecryptfs_num_miscdev_opens;
34 * ecryptfs_miscdev_poll
35 * @file: dev file (ignored)
36 * @pt: dev poll table (ignored)
38 * Returns the poll mask
41 ecryptfs_miscdev_poll(struct file *file, poll_table *pt)
43 struct ecryptfs_daemon *daemon;
44 unsigned int mask = 0;
47 mutex_lock(&ecryptfs_daemon_hash_mux);
48 /* TODO: Just use file->private_data? */
49 rc = ecryptfs_find_daemon_by_euid(&daemon, current->euid);
50 BUG_ON(rc || !daemon);
51 mutex_lock(&daemon->mux);
52 mutex_unlock(&ecryptfs_daemon_hash_mux);
53 if (daemon->flags & ECRYPTFS_DAEMON_ZOMBIE) {
54 printk(KERN_WARNING "%s: Attempt to poll on zombified "
55 "daemon\n", __func__);
56 goto out_unlock_daemon;
58 if (daemon->flags & ECRYPTFS_DAEMON_IN_READ)
59 goto out_unlock_daemon;
60 if (daemon->flags & ECRYPTFS_DAEMON_IN_POLL)
61 goto out_unlock_daemon;
62 daemon->flags |= ECRYPTFS_DAEMON_IN_POLL;
63 mutex_unlock(&daemon->mux);
64 poll_wait(file, &daemon->wait, pt);
65 mutex_lock(&daemon->mux);
66 if (!list_empty(&daemon->msg_ctx_out_queue))
67 mask |= POLLIN | POLLRDNORM;
69 daemon->flags &= ~ECRYPTFS_DAEMON_IN_POLL;
70 mutex_unlock(&daemon->mux);
75 * ecryptfs_miscdev_open
76 * @inode: inode of miscdev handle (ignored)
77 * @file: file for miscdev handle (ignored)
79 * Returns zero on success; non-zero otherwise
82 ecryptfs_miscdev_open(struct inode *inode, struct file *file)
84 struct ecryptfs_daemon *daemon = NULL;
87 mutex_lock(&ecryptfs_daemon_hash_mux);
88 rc = try_module_get(THIS_MODULE);
91 printk(KERN_ERR "%s: Error attempting to increment module use "
92 "count; rc = [%d]\n", __func__, rc);
93 goto out_unlock_daemon_list;
95 rc = ecryptfs_find_daemon_by_euid(&daemon, current->euid);
97 rc = ecryptfs_spawn_daemon(&daemon, current->euid,
100 printk(KERN_ERR "%s: Error attempting to spawn daemon; "
101 "rc = [%d]\n", __func__, rc);
102 goto out_module_put_unlock_daemon_list;
105 mutex_lock(&daemon->mux);
106 if (daemon->pid != current->pid) {
108 printk(KERN_ERR "%s: pid [%d] has registered with euid [%d], "
109 "but pid [%d] has attempted to open the handle "
110 "instead\n", __func__, daemon->pid, daemon->euid,
112 goto out_unlock_daemon;
114 if (daemon->flags & ECRYPTFS_DAEMON_MISCDEV_OPEN) {
116 printk(KERN_ERR "%s: Miscellaneous device handle may only be "
117 "opened once per daemon; pid [%d] already has this "
118 "handle open\n", __func__, daemon->pid);
119 goto out_unlock_daemon;
121 daemon->flags |= ECRYPTFS_DAEMON_MISCDEV_OPEN;
122 atomic_inc(&ecryptfs_num_miscdev_opens);
124 mutex_unlock(&daemon->mux);
125 out_module_put_unlock_daemon_list:
127 module_put(THIS_MODULE);
128 out_unlock_daemon_list:
129 mutex_unlock(&ecryptfs_daemon_hash_mux);
134 * ecryptfs_miscdev_release
135 * @inode: inode of fs/ecryptfs/euid handle (ignored)
136 * @file: file for fs/ecryptfs/euid handle (ignored)
138 * This keeps the daemon registered until the daemon sends another
139 * ioctl to fs/ecryptfs/ctl or until the kernel module unregisters.
141 * Returns zero on success; non-zero otherwise
144 ecryptfs_miscdev_release(struct inode *inode, struct file *file)
146 struct ecryptfs_daemon *daemon = NULL;
149 mutex_lock(&ecryptfs_daemon_hash_mux);
150 rc = ecryptfs_find_daemon_by_euid(&daemon, current->euid);
151 BUG_ON(rc || !daemon);
152 mutex_lock(&daemon->mux);
153 BUG_ON(daemon->pid != current->pid);
154 BUG_ON(!(daemon->flags & ECRYPTFS_DAEMON_MISCDEV_OPEN));
155 daemon->flags &= ~ECRYPTFS_DAEMON_MISCDEV_OPEN;
156 atomic_dec(&ecryptfs_num_miscdev_opens);
157 mutex_unlock(&daemon->mux);
158 rc = ecryptfs_exorcise_daemon(daemon);
160 printk(KERN_CRIT "%s: Fatal error whilst attempting to "
161 "shut down daemon; rc = [%d]. Please report this "
162 "bug.\n", __func__, rc);
165 module_put(THIS_MODULE);
166 mutex_unlock(&ecryptfs_daemon_hash_mux);
171 * ecryptfs_send_miscdev
172 * @data: Data to send to daemon; may be NULL
173 * @data_size: Amount of data to send to daemon
174 * @msg_ctx: Message context, which is used to handle the reply. If
175 * this is NULL, then we do not expect a reply.
176 * @msg_type: Type of message
177 * @msg_flags: Flags for message
178 * @daemon: eCryptfs daemon object
180 * Add msg_ctx to queue and then, if it exists, notify the blocked
181 * miscdevess about the data being available. Must be called with
182 * ecryptfs_daemon_hash_mux held.
184 * Returns zero on success; non-zero otherwise
186 int ecryptfs_send_miscdev(char *data, size_t data_size,
187 struct ecryptfs_msg_ctx *msg_ctx, u8 msg_type,
188 u16 msg_flags, struct ecryptfs_daemon *daemon)
192 mutex_lock(&msg_ctx->mux);
194 msg_ctx->msg = kmalloc((sizeof(*msg_ctx->msg) + data_size),
198 printk(KERN_ERR "%s: Out of memory whilst attempting "
199 "to kmalloc(%Zd, GFP_KERNEL)\n", __func__,
200 (sizeof(*msg_ctx->msg) + data_size));
205 msg_ctx->msg->index = msg_ctx->index;
206 msg_ctx->msg->data_len = data_size;
207 msg_ctx->type = msg_type;
209 memcpy(msg_ctx->msg->data, data, data_size);
210 msg_ctx->msg_size = (sizeof(*msg_ctx->msg) + data_size);
212 msg_ctx->msg_size = 0;
213 mutex_lock(&daemon->mux);
214 list_add_tail(&msg_ctx->daemon_out_list, &daemon->msg_ctx_out_queue);
215 daemon->num_queued_msg_ctx++;
216 wake_up_interruptible(&daemon->wait);
217 mutex_unlock(&daemon->mux);
219 mutex_unlock(&msg_ctx->mux);
224 * ecryptfs_miscdev_read - format and send message from queue
225 * @file: fs/ecryptfs/euid miscdevfs handle (ignored)
226 * @buf: User buffer into which to copy the next message on the daemon queue
227 * @count: Amount of space available in @buf
228 * @ppos: Offset in file (ignored)
230 * Pulls the most recent message from the daemon queue, formats it for
231 * being sent via a miscdevfs handle, and copies it into @buf
233 * Returns the number of bytes copied into the user buffer
236 ecryptfs_miscdev_read(struct file *file, char __user *buf, size_t count,
239 struct ecryptfs_daemon *daemon;
240 struct ecryptfs_msg_ctx *msg_ctx;
241 size_t packet_length_size;
243 char packet_length[3];
248 mutex_lock(&ecryptfs_daemon_hash_mux);
249 /* TODO: Just use file->private_data? */
250 rc = ecryptfs_find_daemon_by_euid(&daemon, current->euid);
251 BUG_ON(rc || !daemon);
252 mutex_lock(&daemon->mux);
253 if (daemon->flags & ECRYPTFS_DAEMON_ZOMBIE) {
255 printk(KERN_WARNING "%s: Attempt to read from zombified "
256 "daemon\n", __func__);
257 goto out_unlock_daemon;
259 if (daemon->flags & ECRYPTFS_DAEMON_IN_READ) {
261 goto out_unlock_daemon;
263 /* This daemon will not go away so long as this flag is set */
264 daemon->flags |= ECRYPTFS_DAEMON_IN_READ;
265 mutex_unlock(&ecryptfs_daemon_hash_mux);
267 if (list_empty(&daemon->msg_ctx_out_queue)) {
268 mutex_unlock(&daemon->mux);
269 rc = wait_event_interruptible(
270 daemon->wait, !list_empty(&daemon->msg_ctx_out_queue));
271 mutex_lock(&daemon->mux);
274 goto out_unlock_daemon;
277 if (daemon->flags & ECRYPTFS_DAEMON_ZOMBIE) {
279 goto out_unlock_daemon;
281 if (list_empty(&daemon->msg_ctx_out_queue)) {
282 /* Something else jumped in since the
283 * wait_event_interruptable() and removed the
284 * message from the queue; try again */
287 BUG_ON(current->euid != daemon->euid);
288 BUG_ON(current->pid != daemon->pid);
289 msg_ctx = list_first_entry(&daemon->msg_ctx_out_queue,
290 struct ecryptfs_msg_ctx, daemon_out_list);
292 mutex_lock(&msg_ctx->mux);
294 rc = ecryptfs_write_packet_length(packet_length,
296 &packet_length_size);
299 printk(KERN_WARNING "%s: Error writing packet length; "
300 "rc = [%d]\n", __func__, rc);
301 goto out_unlock_msg_ctx;
304 packet_length_size = 0;
305 msg_ctx->msg_size = 0;
307 /* miscdevfs packet format:
309 * Octets 1-4: network byte order msg_ctx->counter
310 * Octets 5-N0: Size of struct ecryptfs_message to follow
311 * Octets N0-N1: struct ecryptfs_message (including data)
313 * Octets 5-N1 not written if the packet type does not
314 * include a message */
315 total_length = (1 + 4 + packet_length_size + msg_ctx->msg_size);
316 if (count < total_length) {
318 printk(KERN_WARNING "%s: Only given user buffer of "
319 "size [%Zd], but we need [%Zd] to read the "
320 "pending message\n", __func__, count, total_length);
321 goto out_unlock_msg_ctx;
324 buf[i++] = msg_ctx->type;
325 counter_nbo = cpu_to_be32(msg_ctx->counter);
326 memcpy(&buf[i], (char *)&counter_nbo, 4);
329 memcpy(&buf[i], packet_length, packet_length_size);
330 i += packet_length_size;
331 rc = copy_to_user(&buf[i], msg_ctx->msg, msg_ctx->msg_size);
333 printk(KERN_ERR "%s: copy_to_user returned error "
334 "[%d]\n", __func__, rc);
335 goto out_unlock_msg_ctx;
337 i += msg_ctx->msg_size;
340 list_del(&msg_ctx->daemon_out_list);
343 /* We do not expect a reply from the userspace daemon for any
344 * message type other than ECRYPTFS_MSG_REQUEST */
345 if (msg_ctx->type != ECRYPTFS_MSG_REQUEST)
346 ecryptfs_msg_ctx_alloc_to_free(msg_ctx);
348 mutex_unlock(&msg_ctx->mux);
350 daemon->flags &= ~ECRYPTFS_DAEMON_IN_READ;
351 mutex_unlock(&daemon->mux);
356 * ecryptfs_miscdev_helo
357 * @euid: effective user id of miscdevess sending helo packet
358 * @pid: miscdevess id of miscdevess sending helo packet
360 * Returns zero on success; non-zero otherwise
362 static int ecryptfs_miscdev_helo(uid_t uid, pid_t pid)
366 rc = ecryptfs_process_helo(ECRYPTFS_TRANSPORT_MISCDEV, uid, pid);
368 printk(KERN_WARNING "Error processing HELO; rc = [%d]\n", rc);
373 * ecryptfs_miscdev_quit
374 * @euid: effective user id of miscdevess sending quit packet
375 * @pid: miscdevess id of miscdevess sending quit packet
377 * Returns zero on success; non-zero otherwise
379 static int ecryptfs_miscdev_quit(uid_t euid, pid_t pid)
383 rc = ecryptfs_process_quit(euid, pid);
386 "Error processing QUIT message; rc = [%d]\n", rc);
391 * ecryptfs_miscdev_response - miscdevess response to message previously sent to daemon
392 * @data: Bytes comprising struct ecryptfs_message
393 * @data_size: sizeof(struct ecryptfs_message) + data len
394 * @euid: Effective user id of miscdevess sending the miscdev response
395 * @pid: Miscdevess id of miscdevess sending the miscdev response
396 * @seq: Sequence number for miscdev response packet
398 * Returns zero on success; non-zero otherwise
400 static int ecryptfs_miscdev_response(char *data, size_t data_size,
401 uid_t euid, pid_t pid, u32 seq)
403 struct ecryptfs_message *msg = (struct ecryptfs_message *)data;
406 if ((sizeof(*msg) + msg->data_len) != data_size) {
407 printk(KERN_WARNING "%s: (sizeof(*msg) + msg->data_len) = "
408 "[%Zd]; data_size = [%Zd]. Invalid packet.\n", __func__,
409 (sizeof(*msg) + msg->data_len), data_size);
413 rc = ecryptfs_process_response(msg, euid, pid, seq);
416 "Error processing response message; rc = [%d]\n", rc);
422 * ecryptfs_miscdev_write - handle write to daemon miscdev handle
423 * @file: File for misc dev handle (ignored)
424 * @buf: Buffer containing user data
425 * @count: Amount of data in @buf
426 * @ppos: Pointer to offset in file (ignored)
428 * miscdevfs packet format:
430 * Octets 1-4: network byte order msg_ctx->counter (0's for non-response)
431 * Octets 5-N0: Size of struct ecryptfs_message to follow
432 * Octets N0-N1: struct ecryptfs_message (including data)
434 * Returns the number of bytes read from @buf
437 ecryptfs_miscdev_write(struct file *file, const char __user *buf,
438 size_t count, loff_t *ppos)
440 u32 counter_nbo, seq;
441 size_t packet_size, packet_size_length, i;
448 data = kmalloc(count, GFP_KERNEL);
450 printk(KERN_ERR "%s: Out of memory whilst attempting to "
451 "kmalloc([%Zd], GFP_KERNEL)\n", __func__, count);
454 rc = copy_from_user(data, buf, count);
456 printk(KERN_ERR "%s: copy_from_user returned error [%d]\n",
463 case ECRYPTFS_MSG_RESPONSE:
464 if (count < (1 + 4 + 1 + sizeof(struct ecryptfs_message))) {
465 printk(KERN_WARNING "%s: Minimum acceptable packet "
466 "size is [%Zd], but amount of data written is "
467 "only [%Zd]. Discarding response packet.\n",
469 (1 + 4 + 1 + sizeof(struct ecryptfs_message)),
473 memcpy((char *)&counter_nbo, &data[i], 4);
474 seq = be32_to_cpu(counter_nbo);
476 rc = ecryptfs_parse_packet_length(&data[i], &packet_size,
477 &packet_size_length);
479 printk(KERN_WARNING "%s: Error parsing packet length; "
480 "rc = [%d]\n", __func__, rc);
483 i += packet_size_length;
484 if ((1 + 4 + packet_size_length + packet_size) != count) {
485 printk(KERN_WARNING "%s: (1 + packet_size_length([%Zd])"
486 " + packet_size([%Zd]))([%Zd]) != "
487 "count([%Zd]). Invalid packet format.\n",
488 __func__, packet_size_length, packet_size,
489 (1 + packet_size_length + packet_size), count);
492 rc = ecryptfs_miscdev_response(&data[i], packet_size,
496 printk(KERN_WARNING "%s: Failed to deliver miscdev "
497 "response to requesting operation; rc = [%d]\n",
500 case ECRYPTFS_MSG_HELO:
501 rc = ecryptfs_miscdev_helo(current->euid, current->pid);
503 printk(KERN_ERR "%s: Error attempting to process "
504 "helo from pid [%d]; rc = [%d]\n", __func__,
509 case ECRYPTFS_MSG_QUIT:
510 rc = ecryptfs_miscdev_quit(current->euid, current->pid);
512 printk(KERN_ERR "%s: Error attempting to process "
513 "quit from pid [%d]; rc = [%d]\n", __func__,
519 ecryptfs_printk(KERN_WARNING, "Dropping miscdev "
520 "message of unrecognized type [%d]\n",
531 static const struct file_operations ecryptfs_miscdev_fops = {
532 .open = ecryptfs_miscdev_open,
533 .poll = ecryptfs_miscdev_poll,
534 .read = ecryptfs_miscdev_read,
535 .write = ecryptfs_miscdev_write,
536 .release = ecryptfs_miscdev_release,
539 static struct miscdevice ecryptfs_miscdev = {
540 .minor = MISC_DYNAMIC_MINOR,
542 .fops = &ecryptfs_miscdev_fops
546 * ecryptfs_init_ecryptfs_miscdev
548 * Messages sent to the userspace daemon from the kernel are placed on
549 * a queue associated with the daemon. The next read against the
550 * miscdev handle by that daemon will return the oldest message placed
551 * on the message queue for the daemon.
553 * Returns zero on success; non-zero otherwise
555 int ecryptfs_init_ecryptfs_miscdev(void)
559 atomic_set(&ecryptfs_num_miscdev_opens, 0);
560 mutex_lock(&ecryptfs_daemon_hash_mux);
561 rc = misc_register(&ecryptfs_miscdev);
563 printk(KERN_ERR "%s: Failed to register miscellaneous device "
564 "for communications with userspace daemons; rc = [%d]\n",
566 mutex_unlock(&ecryptfs_daemon_hash_mux);
571 * ecryptfs_destroy_ecryptfs_miscdev
573 * All of the daemons must be exorcised prior to calling this
576 void ecryptfs_destroy_ecryptfs_miscdev(void)
578 BUG_ON(atomic_read(&ecryptfs_num_miscdev_opens) != 0);
579 misc_deregister(&ecryptfs_miscdev);