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;
45 uid_t euid
= current_euid();
48 mutex_lock(&ecryptfs_daemon_hash_mux
);
49 /* TODO: Just use file->private_data? */
50 rc
= ecryptfs_find_daemon_by_euid(&daemon
, euid
, current_user_ns());
51 BUG_ON(rc
|| !daemon
);
52 mutex_lock(&daemon
->mux
);
53 mutex_unlock(&ecryptfs_daemon_hash_mux
);
54 if (daemon
->flags
& ECRYPTFS_DAEMON_ZOMBIE
) {
55 printk(KERN_WARNING
"%s: Attempt to poll on zombified "
56 "daemon\n", __func__
);
57 goto out_unlock_daemon
;
59 if (daemon
->flags
& ECRYPTFS_DAEMON_IN_READ
)
60 goto out_unlock_daemon
;
61 if (daemon
->flags
& ECRYPTFS_DAEMON_IN_POLL
)
62 goto out_unlock_daemon
;
63 daemon
->flags
|= ECRYPTFS_DAEMON_IN_POLL
;
64 mutex_unlock(&daemon
->mux
);
65 poll_wait(file
, &daemon
->wait
, pt
);
66 mutex_lock(&daemon
->mux
);
67 if (!list_empty(&daemon
->msg_ctx_out_queue
))
68 mask
|= POLLIN
| POLLRDNORM
;
70 daemon
->flags
&= ~ECRYPTFS_DAEMON_IN_POLL
;
71 mutex_unlock(&daemon
->mux
);
76 * ecryptfs_miscdev_open
77 * @inode: inode of miscdev handle (ignored)
78 * @file: file for miscdev handle (ignored)
80 * Returns zero on success; non-zero otherwise
83 ecryptfs_miscdev_open(struct inode
*inode
, struct file
*file
)
85 struct ecryptfs_daemon
*daemon
= NULL
;
86 uid_t euid
= current_euid();
89 mutex_lock(&ecryptfs_daemon_hash_mux
);
90 rc
= try_module_get(THIS_MODULE
);
93 printk(KERN_ERR
"%s: Error attempting to increment module use "
94 "count; rc = [%d]\n", __func__
, rc
);
95 goto out_unlock_daemon_list
;
97 rc
= ecryptfs_find_daemon_by_euid(&daemon
, euid
, current_user_ns());
99 rc
= ecryptfs_spawn_daemon(&daemon
, euid
, current_user_ns(),
102 printk(KERN_ERR
"%s: Error attempting to spawn daemon; "
103 "rc = [%d]\n", __func__
, rc
);
104 goto out_module_put_unlock_daemon_list
;
107 mutex_lock(&daemon
->mux
);
108 if (daemon
->pid
!= task_pid(current
)) {
110 printk(KERN_ERR
"%s: pid [0x%p] has registered with euid [%d], "
111 "but pid [0x%p] has attempted to open the handle "
112 "instead\n", __func__
, daemon
->pid
, daemon
->euid
,
114 goto out_unlock_daemon
;
116 if (daemon
->flags
& ECRYPTFS_DAEMON_MISCDEV_OPEN
) {
118 printk(KERN_ERR
"%s: Miscellaneous device handle may only be "
119 "opened once per daemon; pid [0x%p] already has this "
120 "handle open\n", __func__
, daemon
->pid
);
121 goto out_unlock_daemon
;
123 daemon
->flags
|= ECRYPTFS_DAEMON_MISCDEV_OPEN
;
124 atomic_inc(&ecryptfs_num_miscdev_opens
);
126 mutex_unlock(&daemon
->mux
);
127 out_module_put_unlock_daemon_list
:
129 module_put(THIS_MODULE
);
130 out_unlock_daemon_list
:
131 mutex_unlock(&ecryptfs_daemon_hash_mux
);
136 * ecryptfs_miscdev_release
137 * @inode: inode of fs/ecryptfs/euid handle (ignored)
138 * @file: file for fs/ecryptfs/euid handle (ignored)
140 * This keeps the daemon registered until the daemon sends another
141 * ioctl to fs/ecryptfs/ctl or until the kernel module unregisters.
143 * Returns zero on success; non-zero otherwise
146 ecryptfs_miscdev_release(struct inode
*inode
, struct file
*file
)
148 struct ecryptfs_daemon
*daemon
= NULL
;
149 uid_t euid
= current_euid();
152 mutex_lock(&ecryptfs_daemon_hash_mux
);
153 rc
= ecryptfs_find_daemon_by_euid(&daemon
, euid
, current_user_ns());
154 BUG_ON(rc
|| !daemon
);
155 mutex_lock(&daemon
->mux
);
156 BUG_ON(daemon
->pid
!= task_pid(current
));
157 BUG_ON(!(daemon
->flags
& ECRYPTFS_DAEMON_MISCDEV_OPEN
));
158 daemon
->flags
&= ~ECRYPTFS_DAEMON_MISCDEV_OPEN
;
159 atomic_dec(&ecryptfs_num_miscdev_opens
);
160 mutex_unlock(&daemon
->mux
);
161 rc
= ecryptfs_exorcise_daemon(daemon
);
163 printk(KERN_CRIT
"%s: Fatal error whilst attempting to "
164 "shut down daemon; rc = [%d]. Please report this "
165 "bug.\n", __func__
, rc
);
168 module_put(THIS_MODULE
);
169 mutex_unlock(&ecryptfs_daemon_hash_mux
);
174 * ecryptfs_send_miscdev
175 * @data: Data to send to daemon; may be NULL
176 * @data_size: Amount of data to send to daemon
177 * @msg_ctx: Message context, which is used to handle the reply. If
178 * this is NULL, then we do not expect a reply.
179 * @msg_type: Type of message
180 * @msg_flags: Flags for message
181 * @daemon: eCryptfs daemon object
183 * Add msg_ctx to queue and then, if it exists, notify the blocked
184 * miscdevess about the data being available. Must be called with
185 * ecryptfs_daemon_hash_mux held.
187 * Returns zero on success; non-zero otherwise
189 int ecryptfs_send_miscdev(char *data
, size_t data_size
,
190 struct ecryptfs_msg_ctx
*msg_ctx
, u8 msg_type
,
191 u16 msg_flags
, struct ecryptfs_daemon
*daemon
)
195 mutex_lock(&msg_ctx
->mux
);
196 msg_ctx
->msg
= kmalloc((sizeof(*msg_ctx
->msg
) + data_size
),
200 printk(KERN_ERR
"%s: Out of memory whilst attempting "
201 "to kmalloc(%zd, GFP_KERNEL)\n", __func__
,
202 (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
;
208 memcpy(msg_ctx
->msg
->data
, data
, data_size
);
209 msg_ctx
->msg_size
= (sizeof(*msg_ctx
->msg
) + data_size
);
210 mutex_lock(&daemon
->mux
);
211 list_add_tail(&msg_ctx
->daemon_out_list
, &daemon
->msg_ctx_out_queue
);
212 daemon
->num_queued_msg_ctx
++;
213 wake_up_interruptible(&daemon
->wait
);
214 mutex_unlock(&daemon
->mux
);
216 mutex_unlock(&msg_ctx
->mux
);
221 * ecryptfs_miscdev_read - format and send message from queue
222 * @file: fs/ecryptfs/euid miscdevfs handle (ignored)
223 * @buf: User buffer into which to copy the next message on the daemon queue
224 * @count: Amount of space available in @buf
225 * @ppos: Offset in file (ignored)
227 * Pulls the most recent message from the daemon queue, formats it for
228 * being sent via a miscdevfs handle, and copies it into @buf
230 * Returns the number of bytes copied into the user buffer
233 ecryptfs_miscdev_read(struct file
*file
, char __user
*buf
, size_t count
,
236 struct ecryptfs_daemon
*daemon
;
237 struct ecryptfs_msg_ctx
*msg_ctx
;
238 size_t packet_length_size
;
239 char packet_length
[3];
242 uid_t euid
= current_euid();
245 mutex_lock(&ecryptfs_daemon_hash_mux
);
246 /* TODO: Just use file->private_data? */
247 rc
= ecryptfs_find_daemon_by_euid(&daemon
, euid
, current_user_ns());
248 BUG_ON(rc
|| !daemon
);
249 mutex_lock(&daemon
->mux
);
250 if (daemon
->flags
& ECRYPTFS_DAEMON_ZOMBIE
) {
252 mutex_unlock(&ecryptfs_daemon_hash_mux
);
253 printk(KERN_WARNING
"%s: Attempt to read from zombified "
254 "daemon\n", __func__
);
255 goto out_unlock_daemon
;
257 if (daemon
->flags
& ECRYPTFS_DAEMON_IN_READ
) {
259 mutex_unlock(&ecryptfs_daemon_hash_mux
);
260 goto out_unlock_daemon
;
262 /* This daemon will not go away so long as this flag is set */
263 daemon
->flags
|= ECRYPTFS_DAEMON_IN_READ
;
264 mutex_unlock(&ecryptfs_daemon_hash_mux
);
266 if (list_empty(&daemon
->msg_ctx_out_queue
)) {
267 mutex_unlock(&daemon
->mux
);
268 rc
= wait_event_interruptible(
269 daemon
->wait
, !list_empty(&daemon
->msg_ctx_out_queue
));
270 mutex_lock(&daemon
->mux
);
273 goto out_unlock_daemon
;
276 if (daemon
->flags
& ECRYPTFS_DAEMON_ZOMBIE
) {
278 goto out_unlock_daemon
;
280 if (list_empty(&daemon
->msg_ctx_out_queue
)) {
281 /* Something else jumped in since the
282 * wait_event_interruptable() and removed the
283 * message from the queue; try again */
286 BUG_ON(euid
!= daemon
->euid
);
287 BUG_ON(current_user_ns() != daemon
->user_ns
);
288 BUG_ON(task_pid(current
) != 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 if (put_user(msg_ctx
->type
, buf
))
325 goto out_unlock_msg_ctx
;
326 if (put_user(cpu_to_be32(msg_ctx
->counter
), (__be32 __user
*)(buf
+ 1)))
327 goto out_unlock_msg_ctx
;
330 if (copy_to_user(&buf
[i
], packet_length
, packet_length_size
))
331 goto out_unlock_msg_ctx
;
332 i
+= packet_length_size
;
333 if (copy_to_user(&buf
[i
], msg_ctx
->msg
, msg_ctx
->msg_size
))
334 goto out_unlock_msg_ctx
;
335 i
+= msg_ctx
->msg_size
;
338 list_del(&msg_ctx
->daemon_out_list
);
341 /* We do not expect a reply from the userspace daemon for any
342 * message type other than ECRYPTFS_MSG_REQUEST */
343 if (msg_ctx
->type
!= ECRYPTFS_MSG_REQUEST
)
344 ecryptfs_msg_ctx_alloc_to_free(msg_ctx
);
346 mutex_unlock(&msg_ctx
->mux
);
348 daemon
->flags
&= ~ECRYPTFS_DAEMON_IN_READ
;
349 mutex_unlock(&daemon
->mux
);
354 * ecryptfs_miscdev_response - miscdevess response to message previously sent to daemon
355 * @data: Bytes comprising struct ecryptfs_message
356 * @data_size: sizeof(struct ecryptfs_message) + data len
357 * @euid: Effective user id of miscdevess sending the miscdev response
358 * @user_ns: The namespace in which @euid applies
359 * @pid: Miscdevess id of miscdevess sending the miscdev response
360 * @seq: Sequence number for miscdev response packet
362 * Returns zero on success; non-zero otherwise
364 static int ecryptfs_miscdev_response(char *data
, size_t data_size
,
365 uid_t euid
, struct user_namespace
*user_ns
,
366 struct pid
*pid
, u32 seq
)
368 struct ecryptfs_message
*msg
= (struct ecryptfs_message
*)data
;
371 if ((sizeof(*msg
) + msg
->data_len
) != data_size
) {
372 printk(KERN_WARNING
"%s: (sizeof(*msg) + msg->data_len) = "
373 "[%zd]; data_size = [%zd]. Invalid packet.\n", __func__
,
374 (sizeof(*msg
) + msg
->data_len
), data_size
);
378 rc
= ecryptfs_process_response(msg
, euid
, user_ns
, pid
, seq
);
381 "Error processing response message; rc = [%d]\n", rc
);
387 * ecryptfs_miscdev_write - handle write to daemon miscdev handle
388 * @file: File for misc dev handle (ignored)
389 * @buf: Buffer containing user data
390 * @count: Amount of data in @buf
391 * @ppos: Pointer to offset in file (ignored)
393 * miscdevfs packet format:
395 * Octets 1-4: network byte order msg_ctx->counter (0's for non-response)
396 * Octets 5-N0: Size of struct ecryptfs_message to follow
397 * Octets N0-N1: struct ecryptfs_message (including data)
399 * Returns the number of bytes read from @buf
402 ecryptfs_miscdev_write(struct file
*file
, const char __user
*buf
,
403 size_t count
, loff_t
*ppos
)
407 size_t packet_size
, packet_size_length
, i
;
410 uid_t euid
= current_euid();
416 data
= memdup_user(buf
, count
);
418 printk(KERN_ERR
"%s: memdup_user returned error [%ld]\n",
419 __func__
, PTR_ERR(data
));
425 case ECRYPTFS_MSG_RESPONSE
:
426 if (count
< (1 + 4 + 1 + sizeof(struct ecryptfs_message
))) {
427 printk(KERN_WARNING
"%s: Minimum acceptable packet "
428 "size is [%zd], but amount of data written is "
429 "only [%zd]. Discarding response packet.\n",
431 (1 + 4 + 1 + sizeof(struct ecryptfs_message
)),
435 memcpy(&counter_nbo
, &data
[i
], 4);
436 seq
= be32_to_cpu(counter_nbo
);
438 rc
= ecryptfs_parse_packet_length(&data
[i
], &packet_size
,
439 &packet_size_length
);
441 printk(KERN_WARNING
"%s: Error parsing packet length; "
442 "rc = [%d]\n", __func__
, rc
);
445 i
+= packet_size_length
;
446 if ((1 + 4 + packet_size_length
+ packet_size
) != count
) {
447 printk(KERN_WARNING
"%s: (1 + packet_size_length([%zd])"
448 " + packet_size([%zd]))([%zd]) != "
449 "count([%zd]). Invalid packet format.\n",
450 __func__
, packet_size_length
, packet_size
,
451 (1 + packet_size_length
+ packet_size
), count
);
454 rc
= ecryptfs_miscdev_response(&data
[i
], packet_size
,
455 euid
, current_user_ns(),
456 task_pid(current
), seq
);
458 printk(KERN_WARNING
"%s: Failed to deliver miscdev "
459 "response to requesting operation; rc = [%d]\n",
462 case ECRYPTFS_MSG_HELO
:
463 case ECRYPTFS_MSG_QUIT
:
466 ecryptfs_printk(KERN_WARNING
, "Dropping miscdev "
467 "message of unrecognized type [%d]\n",
478 static const struct file_operations ecryptfs_miscdev_fops
= {
479 .open
= ecryptfs_miscdev_open
,
480 .poll
= ecryptfs_miscdev_poll
,
481 .read
= ecryptfs_miscdev_read
,
482 .write
= ecryptfs_miscdev_write
,
483 .release
= ecryptfs_miscdev_release
,
486 static struct miscdevice ecryptfs_miscdev
= {
487 .minor
= MISC_DYNAMIC_MINOR
,
489 .fops
= &ecryptfs_miscdev_fops
493 * ecryptfs_init_ecryptfs_miscdev
495 * Messages sent to the userspace daemon from the kernel are placed on
496 * a queue associated with the daemon. The next read against the
497 * miscdev handle by that daemon will return the oldest message placed
498 * on the message queue for the daemon.
500 * Returns zero on success; non-zero otherwise
502 int ecryptfs_init_ecryptfs_miscdev(void)
506 atomic_set(&ecryptfs_num_miscdev_opens
, 0);
507 rc
= misc_register(&ecryptfs_miscdev
);
509 printk(KERN_ERR
"%s: Failed to register miscellaneous device "
510 "for communications with userspace daemons; rc = [%d]\n",
516 * ecryptfs_destroy_ecryptfs_miscdev
518 * All of the daemons must be exorcised prior to calling this
521 void ecryptfs_destroy_ecryptfs_miscdev(void)
523 BUG_ON(atomic_read(&ecryptfs_num_miscdev_opens
) != 0);
524 misc_deregister(&ecryptfs_miscdev
);