forked from fuse4x/kext
-
Notifications
You must be signed in to change notification settings - Fork 0
/
fuse_device.c
701 lines (538 loc) · 16.4 KB
/
fuse_device.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
/*
* Copyright (C) 2006-2008 Google. All Rights Reserved.
* Amit Singh <singh@>
*/
#include "fuse.h"
#include "fuse_kernel.h"
#include "fuse_device.h"
#include "fuse_ipc.h"
#include "fuse_internal.h"
#include "fuse_kludges.h"
#include "fuse_locking.h"
#include "fuse_nodehash.h"
#include "fuse_sysctl.h"
#include <stdbool.h>
#include <libkern/libkern.h>
#define TAILQ_FOREACH_SAFE(var, head, field, tvar) \
for ((var) = TAILQ_FIRST((head)); \
(var) && ((tvar) = TAILQ_NEXT((var), field), 1); \
(var) = (tvar))
static int fuse_cdev_major = -1;
static bool fuse_interface_available = false;
static struct fuse_device fuse_device_table[FUSE4X_NDEVICES];
#define FUSE_DEVICE_FROM_UNIT_FAST(u) (fuse_device_t)&(fuse_device_table[(u)])
/* Interface for VFS */
/* Doesn't need lock. */
fuse_device_t
fuse_device_get(dev_t dev)
{
int unit = minor(dev);
if ((unit < 0) || (unit >= FUSE4X_NDEVICES)) {
return NULL;
}
return FUSE_DEVICE_FROM_UNIT_FAST(unit);
}
/* Must be called under lock. */
__inline__
void
fuse_device_close_final(fuse_device_t fdev)
{
fdata_destroy(fdev->data);
fdev->data = NULL;
fdev->pid = -1;
}
/* Must be called under data->aw_mtx mutex */
static __inline__
void
fuse_reject_answers(struct fuse_data *data)
{
struct fuse_ticket *ftick;
fuse_lck_mtx_lock(data->aw_mtx);
while ((ftick = fuse_aw_pop(data))) {
fuse_lck_mtx_lock(ftick->tk_aw_mtx);
fticket_set_answered(ftick);
ftick->tk_aw_errno = ENOTCONN;
fuse_wakeup(ftick);
fuse_lck_mtx_unlock(ftick->tk_aw_mtx);
}
fuse_lck_mtx_unlock(data->aw_mtx);
}
/* /dev/fuseN implementation */
d_open_t fuse_device_open;
d_close_t fuse_device_close;
d_read_t fuse_device_read;
d_write_t fuse_device_write;
d_ioctl_t fuse_device_ioctl;
#if M_FUSE4X_ENABLE_DSELECT
d_select_t fuse_device_select;
#else
#define fuse_device_select (d_select_t*)enodev
#endif /* M_FUSE4X_ENABLE_DSELECT */
static struct cdevsw fuse_device_cdevsw = {
/* open */ fuse_device_open,
/* close */ fuse_device_close,
/* read */ fuse_device_read,
/* write */ fuse_device_write,
/* ioctl */ fuse_device_ioctl,
/* stop */ eno_stop,
/* reset */ eno_reset,
/* ttys */ NULL,
/* select */ fuse_device_select,
/* mmap */ eno_mmap,
/* strategy */ eno_strat,
/* getc */ eno_getc,
/* putc */ eno_putc,
/* flags */ D_TTY,
};
int
fuse_device_open(dev_t dev, __unused int flags, __unused int devtype,
struct proc *p)
{
int unit;
struct fuse_device *fdev;
struct fuse_data *fdata;
fuse_trace_printf_func();
if (!fuse_interface_available) {
return ENOENT;
}
unit = minor(dev);
if ((unit >= FUSE4X_NDEVICES) || (unit < 0)) {
fuse_lck_mtx_unlock(fuse_device_mutex);
return ENOENT;
}
fdev = FUSE_DEVICE_FROM_UNIT_FAST(unit);
if (!fdev) {
fuse_lck_mtx_unlock(fuse_device_mutex);
log("fuse4x: device found with no softc\n");
return ENXIO;
}
fuse_lck_mtx_lock(fuse_device_mutex);
if (fdev->usecount != 0) {
fuse_lck_mtx_unlock(fuse_device_mutex);
return EBUSY;
}
fdev->usecount++;
fuse_lck_mtx_lock(fdev->mtx);
fuse_lck_mtx_unlock(fuse_device_mutex);
/* Could block. */
fdata = fdata_alloc(p);
if (fdev->data) {
/*
* This slot isn't currently open by a user daemon. However, it was
* used earlier for a mount that's still lingering, even though the
* user daemon is dead.
*/
fuse_lck_mtx_lock(fuse_device_mutex);
fdev->usecount--;
fuse_lck_mtx_unlock(fdev->mtx);
fuse_lck_mtx_unlock(fuse_device_mutex);
fdata_destroy(fdata);
return EBUSY;
} else {
fdata->dataflags |= FSESS_OPENED;
fdata->fdev = fdev;
fdev->data = fdata;
fdev->pid = proc_pid(p);
}
fuse_lck_mtx_unlock(fdev->mtx);
return KERN_SUCCESS;
}
int
fuse_device_close(dev_t dev, __unused int flags, __unused int devtype,
__unused struct proc *p)
{
int unit;
struct fuse_device *fdev;
struct fuse_data *data;
fuse_trace_printf_func();
unit = minor(dev);
if (unit >= FUSE4X_NDEVICES) {
return ENOENT;
}
fdev = FUSE_DEVICE_FROM_UNIT_FAST(unit);
if (!fdev) {
return ENXIO;
}
data = fdev->data;
if (!data) {
panic("fuse4x: no device private data in device_close");
}
fdata_set_dead(data);
fuse_lck_mtx_lock(fdev->mtx);
data->dataflags &= ~FSESS_OPENED;
fuse_reject_answers(data);
#if M_FUSE4X_ENABLE_DSELECT
selwakeup((struct selinfo*)&data->d_rsel);
#endif /* M_FUSE4X_ENABLE_DSELECT */
if (!(data->dataflags & FSESS_MOUNTED)) {
/* We're not mounted. Can destroy mpdata. */
fuse_device_close_final(fdev);
}
fuse_lck_mtx_unlock(fdev->mtx);
fuse_lck_mtx_lock(fuse_device_mutex);
/*
* Even if usecount goes 0 here, at open time, we check if fdev->data
* is non-NULL (that is, a lingering mount). If so, we return EBUSY.
* We could make the usecount depend on both device-use and mount-state,
* but I think this is truer to reality, if a bit more complex to maintain.
*/
fdev->usecount--;
fuse_lck_mtx_unlock(fuse_device_mutex);
return KERN_SUCCESS;
}
int
fuse_device_read(dev_t dev, uio_t uio, int ioflag)
{
int i, err = 0;
size_t buflen[3];
void *buf[] = { NULL, NULL, NULL };
struct fuse_device *fdev;
struct fuse_data *data;
struct fuse_ticket *ftick;
fuse_trace_printf_func();
fdev = FUSE_DEVICE_FROM_UNIT_FAST(minor(dev));
if (!fdev) {
return ENXIO;
}
data = fdev->data;
fuse_lck_mtx_lock(data->ms_mtx);
/* The read loop (outgoing messages to the user daemon). */
again:
if (fdata_dead_get(data)) {
fuse_lck_mtx_unlock(data->ms_mtx);
return ENODEV;
}
if (!(ftick = fuse_ms_pop(data))) {
if (ioflag & FNONBLOCK) {
fuse_lck_mtx_unlock(data->ms_mtx);
return EAGAIN;
}
err = fuse_msleep(data, data->ms_mtx, PCATCH, "fu_msg", NULL);
if (err) {
fuse_lck_mtx_unlock(data->ms_mtx);
return (fdata_dead_get(data) ? ENODEV : err);
}
goto again;
}
fuse_lck_mtx_unlock(data->ms_mtx);
if (fdata_dead_get(data)) {
if (ftick) {
fuse_ticket_drop_invalid(ftick);
}
return ENODEV;
}
switch (ftick->tk_ms_type) {
case FT_M_FIOV:
buf[0] = ftick->tk_ms_fiov.base;
buflen[0] = ftick->tk_ms_fiov.len;
break;
case FT_M_BUF:
buf[0] = ftick->tk_ms_fiov.base;
buflen[0] = ftick->tk_ms_fiov.len;
buf[1] = ftick->tk_ms_bufdata;
buflen[1] = ftick->tk_ms_bufsize;
break;
default:
panic("fuse4x: unknown message type for ticket %p", ftick);
}
for (i = 0; buf[i]; i++) {
if (uio_resid(uio) < (user_ssize_t)buflen[i]) {
data->dataflags |= FSESS_DEAD;
err = ENODEV;
break;
}
err = uiomove(buf[i], (int)buflen[i], uio);
if (err) {
break;
}
}
/*
* XXX: Stop gap! I really need to finish interruption plumbing.
*/
if (fticket_answered(ftick)) {
err = EINTR;
}
/*
* The FORGET message is an example of a ticket that has explicitly
* been invalidated by the sender. The sender is not expecting or wanting
* a reply, so he sets the FT_INVALID bit in the ticket.
*/
fuse_ticket_drop_invalid(ftick);
return err;
}
int
fuse_device_write(dev_t dev, uio_t uio, __unused int ioflag)
{
int err = 0;
bool found = false;
struct fuse_device *fdev;
struct fuse_data *data;
struct fuse_ticket *ftick;
struct fuse_ticket *x_ftick;
struct fuse_out_header ohead;
fuse_trace_printf_func();
fdev = FUSE_DEVICE_FROM_UNIT_FAST(minor(dev));
if (!fdev) {
return ENXIO;
}
if (uio_resid(uio) < (user_ssize_t)sizeof(struct fuse_out_header)) {
log("fuse4x: Incorrect header size. Got %lld, expected at least %lu\n",
uio_resid(uio), sizeof(struct fuse_out_header));
return EINVAL;
}
if ((err = uiomove((caddr_t)&ohead, (int)sizeof(struct fuse_out_header), uio))) {
return err;
}
/* begin audit */
if (uio_resid(uio) + sizeof(struct fuse_out_header) != ohead.len) {
log("fuse4x: message body size does not match that in the header\n");
return EINVAL;
}
if (uio_resid(uio) && ohead.error) {
log("fuse4x: non-zero error for a message with a body\n");
return EINVAL;
}
ohead.error = -(ohead.error);
/* end audit */
data = fdev->data;
fuse_lck_mtx_lock(data->aw_mtx);
TAILQ_FOREACH_SAFE(ftick, &data->aw_head, tk_aw_link, x_ftick) {
if (ftick->tk_unique == ohead.unique) {
found = true;
fuse_aw_remove(ftick);
break;
}
}
fuse_lck_mtx_unlock(data->aw_mtx);
if (found) {
if (ftick->tk_aw_handler) {
memcpy(&ftick->tk_aw_ohead, &ohead, sizeof(ohead));
err = ftick->tk_aw_handler(ftick, uio);
} else {
fuse_ticket_drop(ftick);
return err;
}
} else {
/* ticket has no response handler */
}
return err;
}
int
fuse_devices_start(void)
{
int i = 0;
fuse_trace_printf_func();
bzero((void *)fuse_device_table, sizeof(fuse_device_table));
if ((fuse_cdev_major = cdevsw_add(-1, &fuse_device_cdevsw)) == -1) {
goto error;
}
for (i = 0; i < FUSE4X_NDEVICES; i++) {
dev_t dev = makedev(fuse_cdev_major, i);
fuse_device_table[i].cdev = devfs_make_node(
dev,
DEVFS_CHAR,
UID_ROOT,
GID_OPERATOR,
0666,
FUSE4X_DEVICE_BASENAME "%d",
i);
if (fuse_device_table[i].cdev == NULL) {
goto error;
}
fuse_device_table[i].data = NULL;
fuse_device_table[i].dev = dev;
fuse_device_table[i].pid = -1;
fuse_device_table[i].usecount = 0;
fuse_device_table[i].mtx = lck_mtx_alloc_init(fuse_lock_group,
fuse_lock_attr);
}
fuse_interface_available = true;
return KERN_SUCCESS;
error:
for (--i; i >= 0; i--) {
devfs_remove(fuse_device_table[i].cdev);
fuse_device_table[i].cdev = NULL;
fuse_device_table[i].dev = 0;
lck_mtx_free(fuse_device_table[i].mtx, fuse_lock_group);
}
(void)cdevsw_remove(fuse_cdev_major, &fuse_device_cdevsw);
fuse_cdev_major = -1;
return KERN_FAILURE;
}
int
fuse_devices_stop(void)
{
int i, ret;
fuse_trace_printf_func();
fuse_interface_available = false;
fuse_lck_mtx_lock(fuse_device_mutex);
if (fuse_cdev_major == -1) {
fuse_lck_mtx_unlock(fuse_device_mutex);
return KERN_SUCCESS;
}
for (i = 0; i < FUSE4X_NDEVICES; i++) {
char p_comm[MAXCOMLEN + 1] = { '?', '\0' };
if (fuse_device_table[i].usecount != 0) {
fuse_interface_available = true;
fuse_lck_mtx_unlock(fuse_device_mutex);
proc_name(fuse_device_table[i].pid, p_comm, MAXCOMLEN + 1);
log("fuse4x: /dev/fuse%d is still active (pid=%d %s)\n",
i, fuse_device_table[i].pid, p_comm);
return KERN_FAILURE;
}
if (fuse_device_table[i].data != NULL) {
fuse_interface_available = true;
fuse_lck_mtx_unlock(fuse_device_mutex);
proc_name(fuse_device_table[i].pid, p_comm, MAXCOMLEN + 1);
/* The pid can't possibly be active here. */
log("fuse4x: /dev/fuse%d has a lingering mount (pid=%d, %s)\n",
i, fuse_device_table[i].pid, p_comm);
return KERN_FAILURE;
}
}
/* No device is in use. */
for (i = 0; i < FUSE4X_NDEVICES; i++) {
devfs_remove(fuse_device_table[i].cdev);
lck_mtx_free(fuse_device_table[i].mtx, fuse_lock_group);
fuse_device_table[i].cdev = NULL;
fuse_device_table[i].dev = 0;
fuse_device_table[i].pid = -1;
fuse_device_table[i].mtx = NULL;
}
ret = cdevsw_remove(fuse_cdev_major, &fuse_device_cdevsw);
if (ret != fuse_cdev_major) {
log("fuse4x: fuse_cdev_major != return from cdevsw_remove()\n");
}
fuse_cdev_major = -1;
fuse_lck_mtx_unlock(fuse_device_mutex);
return KERN_SUCCESS;
}
/* Control/Debug Utilities */
int
fuse_device_ioctl(dev_t dev, u_long cmd, caddr_t udata,
__unused int flags, __unused proc_t proc)
{
int ret = EINVAL;
struct fuse_device *fdev;
struct fuse_data *data;
fuse_trace_printf_func();
fdev = FUSE_DEVICE_FROM_UNIT_FAST(minor(dev));
if (!fdev) {
return ENXIO;
}
data = fdev->data;
if (!data) {
return ENXIO;
}
switch (cmd) {
case FUSEDEVIOCSETDAEMONDEAD:
fdata_set_dead(data);
ret = 0;
break;
default:
break;
}
return ret;
}
#if M_FUSE4X_ENABLE_DSELECT
int
fuse_device_select(dev_t dev, int events, void *wql, struct proc *p)
{
int unit, revents = 0;
struct fuse_device *fdev;
struct fuse_data *data;
fuse_trace_printf_func();
unit = minor(dev);
if (unit >= FUSE4X_NDEVICES) {
return ENOENT;
}
fdev = FUSE_DEVICE_FROM_UNIT_FAST(unit);
if (!fdev) {
return ENXIO;
}
data = fdev->data;
if (!data) {
panic("fuse4x: no device private data in device_select");
}
if (events & (POLLIN | POLLRDNORM)) {
fuse_lck_mtx_lock(data->ms_mtx);
if (fdata_dead_get(data) || STAILQ_FIRST(&data->ms_head)) {
revents |= (events & (POLLIN | POLLRDNORM));
} else {
selrecord((proc_t)p, (struct selinfo*)&data->d_rsel, wql);
}
fuse_lck_mtx_unlock(data->ms_mtx);
}
if (events & (POLLOUT | POLLWRNORM)) {
revents |= (events & (POLLOUT | POLLWRNORM));
}
return revents;
}
#endif /* M_FUSE4X_ENABLE_DSELECT */
int
fuse_device_kill(int unit, struct proc *p)
{
int error = ENOENT;
struct fuse_device *fdev;
fuse_trace_printf_func();
if ((unit < 0) || (unit >= FUSE4X_NDEVICES)) {
return EINVAL;
}
fdev = FUSE_DEVICE_FROM_UNIT_FAST(unit);
if (!fdev) {
return ENOENT;
}
fuse_lck_mtx_lock(fdev->mtx);
struct fuse_data *fdata = fdev->data;
if (fdata) {
error = EPERM;
if (p) {
kauth_cred_t request_cred = kauth_cred_proc_ref(p);
if ((kauth_cred_getuid(request_cred) == 0) ||
(fuse_match_cred(fdata->daemoncred, request_cred) == 0)) {
/* The following can block. */
fdata_set_dead(fdata);
fuse_reject_answers(fdata);
error = 0;
}
kauth_cred_unref(&request_cred);
}
}
fuse_lck_mtx_unlock(fdev->mtx);
return error;
}
int
fuse_device_print_vnodes(int unit_flags, struct proc *p)
{
int error = ENOENT;
struct fuse_device *fdev;
int unit = unit_flags;
if ((unit < 0) || (unit >= FUSE4X_NDEVICES)) {
return EINVAL;
}
fdev = FUSE_DEVICE_FROM_UNIT_FAST(unit);
if (!fdev) {
return ENOENT;
}
fuse_lck_mtx_lock(fdev->mtx);
if (fdev->data) {
mount_t mp = fdev->data->mp;
if (vfs_busy(mp, LK_NOWAIT)) {
fuse_lck_mtx_unlock(fdev->mtx);
return EBUSY;
}
error = EPERM;
if (p) {
kauth_cred_t request_cred = kauth_cred_proc_ref(p);
if ((kauth_cred_getuid(request_cred) == 0) ||
(fuse_match_cred(fdev->data->daemoncred, request_cred) == 0)) {
fuse_internal_print_vnodes(fdev->data->mp);
error = 0;
}
kauth_cred_unref(&request_cred);
}
vfs_unbusy(mp);
}
fuse_lck_mtx_unlock(fdev->mtx);
return error;
}