1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
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/wait.h>
28#include <linux/module.h>
29#include "ecryptfs_kernel.h"
30
31static atomic_t ecryptfs_num_miscdev_opens;
32
33
34
35
36
37
38
39
40static unsigned int
41ecryptfs_miscdev_poll(struct file *file, poll_table *pt)
42{
43 struct ecryptfs_daemon *daemon;
44 unsigned int mask = 0;
45 uid_t euid = current_euid();
46 int rc;
47
48 mutex_lock(&ecryptfs_daemon_hash_mux);
49
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;
58 }
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;
69out_unlock_daemon:
70 daemon->flags &= ~ECRYPTFS_DAEMON_IN_POLL;
71 mutex_unlock(&daemon->mux);
72 return mask;
73}
74
75
76
77
78
79
80
81
82static int
83ecryptfs_miscdev_open(struct inode *inode, struct file *file)
84{
85 struct ecryptfs_daemon *daemon = NULL;
86 uid_t euid = current_euid();
87 int rc;
88
89 mutex_lock(&ecryptfs_daemon_hash_mux);
90 rc = try_module_get(THIS_MODULE);
91 if (rc == 0) {
92 rc = -EIO;
93 printk(KERN_ERR "%s: Error attempting to increment module use "
94 "count; rc = [%d]\n", __func__, rc);
95 goto out_unlock_daemon_list;
96 }
97 rc = ecryptfs_find_daemon_by_euid(&daemon, euid, current_user_ns());
98 if (rc || !daemon) {
99 rc = ecryptfs_spawn_daemon(&daemon, euid, current_user_ns(),
100 task_pid(current));
101 if (rc) {
102 printk(KERN_ERR "%s: Error attempting to spawn daemon; "
103 "rc = [%d]\n", __func__, rc);
104 goto out_module_put_unlock_daemon_list;
105 }
106 }
107 mutex_lock(&daemon->mux);
108 if (daemon->pid != task_pid(current)) {
109 rc = -EINVAL;
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,
113 task_pid(current));
114 goto out_unlock_daemon;
115 }
116 if (daemon->flags & ECRYPTFS_DAEMON_MISCDEV_OPEN) {
117 rc = -EBUSY;
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;
122 }
123 daemon->flags |= ECRYPTFS_DAEMON_MISCDEV_OPEN;
124 atomic_inc(&ecryptfs_num_miscdev_opens);
125out_unlock_daemon:
126 mutex_unlock(&daemon->mux);
127out_module_put_unlock_daemon_list:
128 if (rc)
129 module_put(THIS_MODULE);
130out_unlock_daemon_list:
131 mutex_unlock(&ecryptfs_daemon_hash_mux);
132 return rc;
133}
134
135
136
137
138
139
140
141
142
143
144
145static int
146ecryptfs_miscdev_release(struct inode *inode, struct file *file)
147{
148 struct ecryptfs_daemon *daemon = NULL;
149 uid_t euid = current_euid();
150 int rc;
151
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);
162 if (rc) {
163 printk(KERN_CRIT "%s: Fatal error whilst attempting to "
164 "shut down daemon; rc = [%d]. Please report this "
165 "bug.\n", __func__, rc);
166 BUG();
167 }
168 module_put(THIS_MODULE);
169 mutex_unlock(&ecryptfs_daemon_hash_mux);
170 return rc;
171}
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189int 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)
192{
193 int rc = 0;
194
195 mutex_lock(&msg_ctx->mux);
196 msg_ctx->msg = kmalloc((sizeof(*msg_ctx->msg) + data_size),
197 GFP_KERNEL);
198 if (!msg_ctx->msg) {
199 rc = -ENOMEM;
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));
203 goto out_unlock;
204 }
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);
215out_unlock:
216 mutex_unlock(&msg_ctx->mux);
217 return rc;
218}
219
220
221
222
223
224
225
226
227
228
229
230
231
232static ssize_t
233ecryptfs_miscdev_read(struct file *file, char __user *buf, size_t count,
234 loff_t *ppos)
235{
236 struct ecryptfs_daemon *daemon;
237 struct ecryptfs_msg_ctx *msg_ctx;
238 size_t packet_length_size;
239 char packet_length[3];
240 size_t i;
241 size_t total_length;
242 uid_t euid = current_euid();
243 int rc;
244
245 mutex_lock(&ecryptfs_daemon_hash_mux);
246
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) {
251 rc = 0;
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;
256 }
257 if (daemon->flags & ECRYPTFS_DAEMON_IN_READ) {
258 rc = 0;
259 mutex_unlock(&ecryptfs_daemon_hash_mux);
260 goto out_unlock_daemon;
261 }
262
263 daemon->flags |= ECRYPTFS_DAEMON_IN_READ;
264 mutex_unlock(&ecryptfs_daemon_hash_mux);
265check_list:
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);
271 if (rc < 0) {
272 rc = 0;
273 goto out_unlock_daemon;
274 }
275 }
276 if (daemon->flags & ECRYPTFS_DAEMON_ZOMBIE) {
277 rc = 0;
278 goto out_unlock_daemon;
279 }
280 if (list_empty(&daemon->msg_ctx_out_queue)) {
281
282
283
284 goto check_list;
285 }
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);
291 BUG_ON(!msg_ctx);
292 mutex_lock(&msg_ctx->mux);
293 if (msg_ctx->msg) {
294 rc = ecryptfs_write_packet_length(packet_length,
295 msg_ctx->msg_size,
296 &packet_length_size);
297 if (rc) {
298 rc = 0;
299 printk(KERN_WARNING "%s: Error writing packet length; "
300 "rc = [%d]\n", __func__, rc);
301 goto out_unlock_msg_ctx;
302 }
303 } else {
304 packet_length_size = 0;
305 msg_ctx->msg_size = 0;
306 }
307
308
309
310
311
312
313
314
315 total_length = (1 + 4 + packet_length_size + msg_ctx->msg_size);
316 if (count < total_length) {
317 rc = 0;
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;
322 }
323 rc = -EFAULT;
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;
328 i = 5;
329 if (msg_ctx->msg) {
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;
336 }
337 rc = i;
338 list_del(&msg_ctx->daemon_out_list);
339 kfree(msg_ctx->msg);
340 msg_ctx->msg = NULL;
341
342
343 if (msg_ctx->type != ECRYPTFS_MSG_REQUEST)
344 ecryptfs_msg_ctx_alloc_to_free(msg_ctx);
345out_unlock_msg_ctx:
346 mutex_unlock(&msg_ctx->mux);
347out_unlock_daemon:
348 daemon->flags &= ~ECRYPTFS_DAEMON_IN_READ;
349 mutex_unlock(&daemon->mux);
350 return rc;
351}
352
353
354
355
356
357
358
359
360
361
362
363
364static 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)
367{
368 struct ecryptfs_message *msg = (struct ecryptfs_message *)data;
369 int rc;
370
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);
375 rc = -EINVAL;
376 goto out;
377 }
378 rc = ecryptfs_process_response(msg, euid, user_ns, pid, seq);
379 if (rc)
380 printk(KERN_ERR
381 "Error processing response message; rc = [%d]\n", rc);
382out:
383 return rc;
384}
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401static ssize_t
402ecryptfs_miscdev_write(struct file *file, const char __user *buf,
403 size_t count, loff_t *ppos)
404{
405 __be32 counter_nbo;
406 u32 seq;
407 size_t packet_size, packet_size_length, i;
408 ssize_t sz = 0;
409 char *data;
410 uid_t euid = current_euid();
411 int rc;
412
413 if (count == 0)
414 goto out;
415
416 data = memdup_user(buf, count);
417 if (IS_ERR(data)) {
418 printk(KERN_ERR "%s: memdup_user returned error [%ld]\n",
419 __func__, PTR_ERR(data));
420 goto out;
421 }
422 sz = count;
423 i = 0;
424 switch (data[i++]) {
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",
430 __func__,
431 (1 + 4 + 1 + sizeof(struct ecryptfs_message)),
432 count);
433 goto out_free;
434 }
435 memcpy(&counter_nbo, &data[i], 4);
436 seq = be32_to_cpu(counter_nbo);
437 i += 4;
438 rc = ecryptfs_parse_packet_length(&data[i], &packet_size,
439 &packet_size_length);
440 if (rc) {
441 printk(KERN_WARNING "%s: Error parsing packet length; "
442 "rc = [%d]\n", __func__, rc);
443 goto out_free;
444 }
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);
452 goto out_free;
453 }
454 rc = ecryptfs_miscdev_response(&data[i], packet_size,
455 euid, current_user_ns(),
456 task_pid(current), seq);
457 if (rc)
458 printk(KERN_WARNING "%s: Failed to deliver miscdev "
459 "response to requesting operation; rc = [%d]\n",
460 __func__, rc);
461 break;
462 case ECRYPTFS_MSG_HELO:
463 case ECRYPTFS_MSG_QUIT:
464 break;
465 default:
466 ecryptfs_printk(KERN_WARNING, "Dropping miscdev "
467 "message of unrecognized type [%d]\n",
468 data[0]);
469 break;
470 }
471out_free:
472 kfree(data);
473out:
474 return sz;
475}
476
477
478static 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,
484};
485
486static struct miscdevice ecryptfs_miscdev = {
487 .minor = MISC_DYNAMIC_MINOR,
488 .name = "ecryptfs",
489 .fops = &ecryptfs_miscdev_fops
490};
491
492
493
494
495
496
497
498
499
500
501
502int ecryptfs_init_ecryptfs_miscdev(void)
503{
504 int rc;
505
506 atomic_set(&ecryptfs_num_miscdev_opens, 0);
507 rc = misc_register(&ecryptfs_miscdev);
508 if (rc)
509 printk(KERN_ERR "%s: Failed to register miscellaneous device "
510 "for communications with userspace daemons; rc = [%d]\n",
511 __func__, rc);
512 return rc;
513}
514
515
516
517
518
519
520
521void ecryptfs_destroy_ecryptfs_miscdev(void)
522{
523 BUG_ON(atomic_read(&ecryptfs_num_miscdev_opens) != 0);
524 misc_deregister(&ecryptfs_miscdev);
525}
526