eCryptfs: Fix lockdep warning in miscdev operations
[deliverable/linux.git] / fs / ecryptfs / miscdev.c
1 /**
2 * eCryptfs: Linux filesystem encryption layer
3 *
4 * Copyright (C) 2008 International Business Machines Corp.
5 * Author(s): Michael A. Halcrow <mhalcrow@us.ibm.com>
6 *
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.
10 *
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.
15 *
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
19 * 02111-1307, USA.
20 */
21
22 #include <linux/fs.h>
23 #include <linux/hash.h>
24 #include <linux/random.h>
25 #include <linux/miscdevice.h>
26 #include <linux/poll.h>
27 #include <linux/slab.h>
28 #include <linux/wait.h>
29 #include <linux/module.h>
30 #include "ecryptfs_kernel.h"
31
32 static atomic_t ecryptfs_num_miscdev_opens;
33
34 /**
35 * ecryptfs_miscdev_poll
36 * @file: dev file (ignored)
37 * @pt: dev poll table (ignored)
38 *
39 * Returns the poll mask
40 */
41 static unsigned int
42 ecryptfs_miscdev_poll(struct file *file, poll_table *pt)
43 {
44 struct ecryptfs_daemon *daemon;
45 unsigned int mask = 0;
46 uid_t euid = current_euid();
47 int rc;
48
49 mutex_lock(&ecryptfs_daemon_hash_mux);
50 /* TODO: Just use file->private_data? */
51 rc = ecryptfs_find_daemon_by_euid(&daemon, euid, current_user_ns());
52 BUG_ON(rc || !daemon);
53 mutex_lock(&daemon->mux);
54 mutex_unlock(&ecryptfs_daemon_hash_mux);
55 if (daemon->flags & ECRYPTFS_DAEMON_ZOMBIE) {
56 printk(KERN_WARNING "%s: Attempt to poll on zombified "
57 "daemon\n", __func__);
58 goto out_unlock_daemon;
59 }
60 if (daemon->flags & ECRYPTFS_DAEMON_IN_READ)
61 goto out_unlock_daemon;
62 if (daemon->flags & ECRYPTFS_DAEMON_IN_POLL)
63 goto out_unlock_daemon;
64 daemon->flags |= ECRYPTFS_DAEMON_IN_POLL;
65 mutex_unlock(&daemon->mux);
66 poll_wait(file, &daemon->wait, pt);
67 mutex_lock(&daemon->mux);
68 if (!list_empty(&daemon->msg_ctx_out_queue))
69 mask |= POLLIN | POLLRDNORM;
70 out_unlock_daemon:
71 daemon->flags &= ~ECRYPTFS_DAEMON_IN_POLL;
72 mutex_unlock(&daemon->mux);
73 return mask;
74 }
75
76 /**
77 * ecryptfs_miscdev_open
78 * @inode: inode of miscdev handle (ignored)
79 * @file: file for miscdev handle (ignored)
80 *
81 * Returns zero on success; non-zero otherwise
82 */
83 static int
84 ecryptfs_miscdev_open(struct inode *inode, struct file *file)
85 {
86 struct ecryptfs_daemon *daemon = NULL;
87 uid_t euid = current_euid();
88 int rc;
89
90 mutex_lock(&ecryptfs_daemon_hash_mux);
91 rc = try_module_get(THIS_MODULE);
92 if (rc == 0) {
93 rc = -EIO;
94 printk(KERN_ERR "%s: Error attempting to increment module use "
95 "count; rc = [%d]\n", __func__, rc);
96 goto out_unlock_daemon_list;
97 }
98 rc = ecryptfs_find_daemon_by_euid(&daemon, euid, current_user_ns());
99 if (rc || !daemon) {
100 rc = ecryptfs_spawn_daemon(&daemon, euid, current_user_ns(),
101 task_pid(current));
102 if (rc) {
103 printk(KERN_ERR "%s: Error attempting to spawn daemon; "
104 "rc = [%d]\n", __func__, rc);
105 goto out_module_put_unlock_daemon_list;
106 }
107 }
108 mutex_lock(&daemon->mux);
109 if (daemon->pid != task_pid(current)) {
110 rc = -EINVAL;
111 printk(KERN_ERR "%s: pid [0x%p] has registered with euid [%d], "
112 "but pid [0x%p] has attempted to open the handle "
113 "instead\n", __func__, daemon->pid, daemon->euid,
114 task_pid(current));
115 goto out_unlock_daemon;
116 }
117 if (daemon->flags & ECRYPTFS_DAEMON_MISCDEV_OPEN) {
118 rc = -EBUSY;
119 printk(KERN_ERR "%s: Miscellaneous device handle may only be "
120 "opened once per daemon; pid [0x%p] already has this "
121 "handle open\n", __func__, daemon->pid);
122 goto out_unlock_daemon;
123 }
124 daemon->flags |= ECRYPTFS_DAEMON_MISCDEV_OPEN;
125 atomic_inc(&ecryptfs_num_miscdev_opens);
126 out_unlock_daemon:
127 mutex_unlock(&daemon->mux);
128 out_module_put_unlock_daemon_list:
129 if (rc)
130 module_put(THIS_MODULE);
131 out_unlock_daemon_list:
132 mutex_unlock(&ecryptfs_daemon_hash_mux);
133 return rc;
134 }
135
136 /**
137 * ecryptfs_miscdev_release
138 * @inode: inode of fs/ecryptfs/euid handle (ignored)
139 * @file: file for fs/ecryptfs/euid handle (ignored)
140 *
141 * This keeps the daemon registered until the daemon sends another
142 * ioctl to fs/ecryptfs/ctl or until the kernel module unregisters.
143 *
144 * Returns zero on success; non-zero otherwise
145 */
146 static int
147 ecryptfs_miscdev_release(struct inode *inode, struct file *file)
148 {
149 struct ecryptfs_daemon *daemon = NULL;
150 uid_t euid = current_euid();
151 int rc;
152
153 mutex_lock(&ecryptfs_daemon_hash_mux);
154 rc = ecryptfs_find_daemon_by_euid(&daemon, euid, current_user_ns());
155 BUG_ON(rc || !daemon);
156 mutex_lock(&daemon->mux);
157 BUG_ON(daemon->pid != task_pid(current));
158 BUG_ON(!(daemon->flags & ECRYPTFS_DAEMON_MISCDEV_OPEN));
159 daemon->flags &= ~ECRYPTFS_DAEMON_MISCDEV_OPEN;
160 atomic_dec(&ecryptfs_num_miscdev_opens);
161 mutex_unlock(&daemon->mux);
162 rc = ecryptfs_exorcise_daemon(daemon);
163 if (rc) {
164 printk(KERN_CRIT "%s: Fatal error whilst attempting to "
165 "shut down daemon; rc = [%d]. Please report this "
166 "bug.\n", __func__, rc);
167 BUG();
168 }
169 module_put(THIS_MODULE);
170 mutex_unlock(&ecryptfs_daemon_hash_mux);
171 return rc;
172 }
173
174 /**
175 * ecryptfs_send_miscdev
176 * @data: Data to send to daemon; may be NULL
177 * @data_size: Amount of data to send to daemon
178 * @msg_ctx: Message context, which is used to handle the reply. If
179 * this is NULL, then we do not expect a reply.
180 * @msg_type: Type of message
181 * @msg_flags: Flags for message
182 * @daemon: eCryptfs daemon object
183 *
184 * Add msg_ctx to queue and then, if it exists, notify the blocked
185 * miscdevess about the data being available. Must be called with
186 * ecryptfs_daemon_hash_mux held.
187 *
188 * Returns zero on success; non-zero otherwise
189 */
190 int ecryptfs_send_miscdev(char *data, size_t data_size,
191 struct ecryptfs_msg_ctx *msg_ctx, u8 msg_type,
192 u16 msg_flags, struct ecryptfs_daemon *daemon)
193 {
194 struct ecryptfs_message *msg;
195
196 msg = kmalloc((sizeof(*msg) + data_size), GFP_KERNEL);
197 if (!msg) {
198 printk(KERN_ERR "%s: Out of memory whilst attempting "
199 "to kmalloc(%zd, GFP_KERNEL)\n", __func__,
200 (sizeof(*msg) + data_size));
201 return -ENOMEM;
202 }
203
204 mutex_lock(&msg_ctx->mux);
205 msg_ctx->msg = msg;
206 msg_ctx->msg->index = msg_ctx->index;
207 msg_ctx->msg->data_len = data_size;
208 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);
211 list_add_tail(&msg_ctx->daemon_out_list, &daemon->msg_ctx_out_queue);
212 mutex_unlock(&msg_ctx->mux);
213
214 mutex_lock(&daemon->mux);
215 daemon->num_queued_msg_ctx++;
216 wake_up_interruptible(&daemon->wait);
217 mutex_unlock(&daemon->mux);
218
219 return 0;
220 }
221
222 /*
223 * miscdevfs packet format:
224 * Octet 0: Type
225 * Octets 1-4: network byte order msg_ctx->counter
226 * Octets 5-N0: Size of struct ecryptfs_message to follow
227 * Octets N0-N1: struct ecryptfs_message (including data)
228 *
229 * Octets 5-N1 not written if the packet type does not include a message
230 */
231 #define PKT_TYPE_SIZE 1
232 #define PKT_CTR_SIZE 4
233 #define MIN_NON_MSG_PKT_SIZE (PKT_TYPE_SIZE + PKT_CTR_SIZE)
234 #define MIN_MSG_PKT_SIZE (PKT_TYPE_SIZE + PKT_CTR_SIZE \
235 + ECRYPTFS_MIN_PKT_LEN_SIZE)
236 /* 4 + ECRYPTFS_MAX_ENCRYPTED_KEY_BYTES comes from tag 65 packet format */
237 #define MAX_MSG_PKT_SIZE (PKT_TYPE_SIZE + PKT_CTR_SIZE \
238 + ECRYPTFS_MAX_PKT_LEN_SIZE \
239 + sizeof(struct ecryptfs_message) \
240 + 4 + ECRYPTFS_MAX_ENCRYPTED_KEY_BYTES)
241 #define PKT_TYPE_OFFSET 0
242 #define PKT_CTR_OFFSET PKT_TYPE_SIZE
243 #define PKT_LEN_OFFSET (PKT_TYPE_SIZE + PKT_CTR_SIZE)
244
245 /**
246 * ecryptfs_miscdev_read - format and send message from queue
247 * @file: fs/ecryptfs/euid miscdevfs handle (ignored)
248 * @buf: User buffer into which to copy the next message on the daemon queue
249 * @count: Amount of space available in @buf
250 * @ppos: Offset in file (ignored)
251 *
252 * Pulls the most recent message from the daemon queue, formats it for
253 * being sent via a miscdevfs handle, and copies it into @buf
254 *
255 * Returns the number of bytes copied into the user buffer
256 */
257 static ssize_t
258 ecryptfs_miscdev_read(struct file *file, char __user *buf, size_t count,
259 loff_t *ppos)
260 {
261 struct ecryptfs_daemon *daemon;
262 struct ecryptfs_msg_ctx *msg_ctx;
263 size_t packet_length_size;
264 char packet_length[ECRYPTFS_MAX_PKT_LEN_SIZE];
265 size_t i;
266 size_t total_length;
267 uid_t euid = current_euid();
268 int rc;
269
270 mutex_lock(&ecryptfs_daemon_hash_mux);
271 /* TODO: Just use file->private_data? */
272 rc = ecryptfs_find_daemon_by_euid(&daemon, euid, current_user_ns());
273 BUG_ON(rc || !daemon);
274 mutex_lock(&daemon->mux);
275 if (daemon->flags & ECRYPTFS_DAEMON_ZOMBIE) {
276 rc = 0;
277 mutex_unlock(&ecryptfs_daemon_hash_mux);
278 printk(KERN_WARNING "%s: Attempt to read from zombified "
279 "daemon\n", __func__);
280 goto out_unlock_daemon;
281 }
282 if (daemon->flags & ECRYPTFS_DAEMON_IN_READ) {
283 rc = 0;
284 mutex_unlock(&ecryptfs_daemon_hash_mux);
285 goto out_unlock_daemon;
286 }
287 /* This daemon will not go away so long as this flag is set */
288 daemon->flags |= ECRYPTFS_DAEMON_IN_READ;
289 mutex_unlock(&ecryptfs_daemon_hash_mux);
290 check_list:
291 if (list_empty(&daemon->msg_ctx_out_queue)) {
292 mutex_unlock(&daemon->mux);
293 rc = wait_event_interruptible(
294 daemon->wait, !list_empty(&daemon->msg_ctx_out_queue));
295 mutex_lock(&daemon->mux);
296 if (rc < 0) {
297 rc = 0;
298 goto out_unlock_daemon;
299 }
300 }
301 if (daemon->flags & ECRYPTFS_DAEMON_ZOMBIE) {
302 rc = 0;
303 goto out_unlock_daemon;
304 }
305 if (list_empty(&daemon->msg_ctx_out_queue)) {
306 /* Something else jumped in since the
307 * wait_event_interruptable() and removed the
308 * message from the queue; try again */
309 goto check_list;
310 }
311 BUG_ON(euid != daemon->euid);
312 BUG_ON(current_user_ns() != daemon->user_ns);
313 BUG_ON(task_pid(current) != daemon->pid);
314 msg_ctx = list_first_entry(&daemon->msg_ctx_out_queue,
315 struct ecryptfs_msg_ctx, daemon_out_list);
316 BUG_ON(!msg_ctx);
317 mutex_lock(&msg_ctx->mux);
318 if (msg_ctx->msg) {
319 rc = ecryptfs_write_packet_length(packet_length,
320 msg_ctx->msg_size,
321 &packet_length_size);
322 if (rc) {
323 rc = 0;
324 printk(KERN_WARNING "%s: Error writing packet length; "
325 "rc = [%d]\n", __func__, rc);
326 goto out_unlock_msg_ctx;
327 }
328 } else {
329 packet_length_size = 0;
330 msg_ctx->msg_size = 0;
331 }
332 total_length = (PKT_TYPE_SIZE + PKT_CTR_SIZE + packet_length_size
333 + msg_ctx->msg_size);
334 if (count < total_length) {
335 rc = 0;
336 printk(KERN_WARNING "%s: Only given user buffer of "
337 "size [%zd], but we need [%zd] to read the "
338 "pending message\n", __func__, count, total_length);
339 goto out_unlock_msg_ctx;
340 }
341 rc = -EFAULT;
342 if (put_user(msg_ctx->type, buf))
343 goto out_unlock_msg_ctx;
344 if (put_user(cpu_to_be32(msg_ctx->counter),
345 (__be32 __user *)(&buf[PKT_CTR_OFFSET])))
346 goto out_unlock_msg_ctx;
347 i = PKT_TYPE_SIZE + PKT_CTR_SIZE;
348 if (msg_ctx->msg) {
349 if (copy_to_user(&buf[i], packet_length, packet_length_size))
350 goto out_unlock_msg_ctx;
351 i += packet_length_size;
352 if (copy_to_user(&buf[i], msg_ctx->msg, msg_ctx->msg_size))
353 goto out_unlock_msg_ctx;
354 i += msg_ctx->msg_size;
355 }
356 rc = i;
357 list_del(&msg_ctx->daemon_out_list);
358 kfree(msg_ctx->msg);
359 msg_ctx->msg = NULL;
360 /* We do not expect a reply from the userspace daemon for any
361 * message type other than ECRYPTFS_MSG_REQUEST */
362 if (msg_ctx->type != ECRYPTFS_MSG_REQUEST)
363 ecryptfs_msg_ctx_alloc_to_free(msg_ctx);
364 out_unlock_msg_ctx:
365 mutex_unlock(&msg_ctx->mux);
366 out_unlock_daemon:
367 daemon->flags &= ~ECRYPTFS_DAEMON_IN_READ;
368 mutex_unlock(&daemon->mux);
369 return rc;
370 }
371
372 /**
373 * ecryptfs_miscdev_response - miscdevess response to message previously sent to daemon
374 * @data: Bytes comprising struct ecryptfs_message
375 * @data_size: sizeof(struct ecryptfs_message) + data len
376 * @euid: Effective user id of miscdevess sending the miscdev response
377 * @user_ns: The namespace in which @euid applies
378 * @pid: Miscdevess id of miscdevess sending the miscdev response
379 * @seq: Sequence number for miscdev response packet
380 *
381 * Returns zero on success; non-zero otherwise
382 */
383 static int ecryptfs_miscdev_response(char *data, size_t data_size,
384 uid_t euid, struct user_namespace *user_ns,
385 struct pid *pid, u32 seq)
386 {
387 struct ecryptfs_message *msg = (struct ecryptfs_message *)data;
388 int rc;
389
390 if ((sizeof(*msg) + msg->data_len) != data_size) {
391 printk(KERN_WARNING "%s: (sizeof(*msg) + msg->data_len) = "
392 "[%zd]; data_size = [%zd]. Invalid packet.\n", __func__,
393 (sizeof(*msg) + msg->data_len), data_size);
394 rc = -EINVAL;
395 goto out;
396 }
397 rc = ecryptfs_process_response(msg, euid, user_ns, pid, seq);
398 if (rc)
399 printk(KERN_ERR
400 "Error processing response message; rc = [%d]\n", rc);
401 out:
402 return rc;
403 }
404
405 /**
406 * ecryptfs_miscdev_write - handle write to daemon miscdev handle
407 * @file: File for misc dev handle (ignored)
408 * @buf: Buffer containing user data
409 * @count: Amount of data in @buf
410 * @ppos: Pointer to offset in file (ignored)
411 *
412 * Returns the number of bytes read from @buf
413 */
414 static ssize_t
415 ecryptfs_miscdev_write(struct file *file, const char __user *buf,
416 size_t count, loff_t *ppos)
417 {
418 __be32 counter_nbo;
419 u32 seq;
420 size_t packet_size, packet_size_length;
421 char *data;
422 uid_t euid = current_euid();
423 unsigned char packet_size_peek[ECRYPTFS_MAX_PKT_LEN_SIZE];
424 ssize_t rc;
425
426 if (count == 0) {
427 return 0;
428 } else if (count == MIN_NON_MSG_PKT_SIZE) {
429 /* Likely a harmless MSG_HELO or MSG_QUIT - no packet length */
430 goto memdup;
431 } else if (count < MIN_MSG_PKT_SIZE || count > MAX_MSG_PKT_SIZE) {
432 printk(KERN_WARNING "%s: Acceptable packet size range is "
433 "[%d-%zu], but amount of data written is [%zu].",
434 __func__, MIN_MSG_PKT_SIZE, MAX_MSG_PKT_SIZE, count);
435 return -EINVAL;
436 }
437
438 if (copy_from_user(packet_size_peek, &buf[PKT_LEN_OFFSET],
439 sizeof(packet_size_peek))) {
440 printk(KERN_WARNING "%s: Error while inspecting packet size\n",
441 __func__);
442 return -EFAULT;
443 }
444
445 rc = ecryptfs_parse_packet_length(packet_size_peek, &packet_size,
446 &packet_size_length);
447 if (rc) {
448 printk(KERN_WARNING "%s: Error parsing packet length; "
449 "rc = [%zd]\n", __func__, rc);
450 return rc;
451 }
452
453 if ((PKT_TYPE_SIZE + PKT_CTR_SIZE + packet_size_length + packet_size)
454 != count) {
455 printk(KERN_WARNING "%s: Invalid packet size [%zu]\n", __func__,
456 packet_size);
457 return -EINVAL;
458 }
459
460 memdup:
461 data = memdup_user(buf, count);
462 if (IS_ERR(data)) {
463 printk(KERN_ERR "%s: memdup_user returned error [%ld]\n",
464 __func__, PTR_ERR(data));
465 return PTR_ERR(data);
466 }
467 switch (data[PKT_TYPE_OFFSET]) {
468 case ECRYPTFS_MSG_RESPONSE:
469 if (count < (MIN_MSG_PKT_SIZE
470 + sizeof(struct ecryptfs_message))) {
471 printk(KERN_WARNING "%s: Minimum acceptable packet "
472 "size is [%zd], but amount of data written is "
473 "only [%zd]. Discarding response packet.\n",
474 __func__,
475 (MIN_MSG_PKT_SIZE
476 + sizeof(struct ecryptfs_message)), count);
477 rc = -EINVAL;
478 goto out_free;
479 }
480 memcpy(&counter_nbo, &data[PKT_CTR_OFFSET], PKT_CTR_SIZE);
481 seq = be32_to_cpu(counter_nbo);
482 rc = ecryptfs_miscdev_response(
483 &data[PKT_LEN_OFFSET + packet_size_length],
484 packet_size, euid, current_user_ns(),
485 task_pid(current), seq);
486 if (rc) {
487 printk(KERN_WARNING "%s: Failed to deliver miscdev "
488 "response to requesting operation; rc = [%zd]\n",
489 __func__, rc);
490 goto out_free;
491 }
492 break;
493 case ECRYPTFS_MSG_HELO:
494 case ECRYPTFS_MSG_QUIT:
495 break;
496 default:
497 ecryptfs_printk(KERN_WARNING, "Dropping miscdev "
498 "message of unrecognized type [%d]\n",
499 data[0]);
500 rc = -EINVAL;
501 goto out_free;
502 }
503 rc = count;
504 out_free:
505 kfree(data);
506 return rc;
507 }
508
509
510 static const struct file_operations ecryptfs_miscdev_fops = {
511 .open = ecryptfs_miscdev_open,
512 .poll = ecryptfs_miscdev_poll,
513 .read = ecryptfs_miscdev_read,
514 .write = ecryptfs_miscdev_write,
515 .release = ecryptfs_miscdev_release,
516 .llseek = noop_llseek,
517 };
518
519 static struct miscdevice ecryptfs_miscdev = {
520 .minor = MISC_DYNAMIC_MINOR,
521 .name = "ecryptfs",
522 .fops = &ecryptfs_miscdev_fops
523 };
524
525 /**
526 * ecryptfs_init_ecryptfs_miscdev
527 *
528 * Messages sent to the userspace daemon from the kernel are placed on
529 * a queue associated with the daemon. The next read against the
530 * miscdev handle by that daemon will return the oldest message placed
531 * on the message queue for the daemon.
532 *
533 * Returns zero on success; non-zero otherwise
534 */
535 int __init ecryptfs_init_ecryptfs_miscdev(void)
536 {
537 int rc;
538
539 atomic_set(&ecryptfs_num_miscdev_opens, 0);
540 rc = misc_register(&ecryptfs_miscdev);
541 if (rc)
542 printk(KERN_ERR "%s: Failed to register miscellaneous device "
543 "for communications with userspace daemons; rc = [%d]\n",
544 __func__, rc);
545 return rc;
546 }
547
548 /**
549 * ecryptfs_destroy_ecryptfs_miscdev
550 *
551 * All of the daemons must be exorcised prior to calling this
552 * function.
553 */
554 void ecryptfs_destroy_ecryptfs_miscdev(void)
555 {
556 BUG_ON(atomic_read(&ecryptfs_num_miscdev_opens) != 0);
557 misc_deregister(&ecryptfs_miscdev);
558 }
This page took 0.042622 seconds and 5 git commands to generate.