โฌข DragonFlyBSD Kernel Audit
DF-0925 / rawfuse.c
โ† back to finding โ†“ download raw
  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
/*
 * rawfuse.c โ€” Self-contained raw FUSE protocol daemon for DF-0925 PoC.
 *
 * Speaks the FUSE kernel ABI directly over /dev/fuse โ€” NO libfuse needed.
 * Serves a minimal filesystem with one regular file "target" at nodeid 100.
 *
 * Build:  cc -O2 -o rawfuse rawfuse.c
 *
 * Usage:  ./rawfuse /mnt/fuse
 *         (root: opens /dev/fuse, mounts, serves until killed)
 *
 * Protocol: read(2) /dev/fuse -> fuse_in_header + payload;
 *           write(2) /dev/fuse <- fuse_out_header + payload (match unique).
 */
#include <sys/types.h>
#include <sys/stat.h>
#include <sys/uio.h>
#include <sys/ioctl.h>

#include <errno.h>
#include <fcntl.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <signal.h>

/* ---- FUSE ABI structs (from fuse_abi.h, hardcoded for portability) ---- */

#define FUSE_KERNEL_VERSION 7
#define FUSE_KERNEL_MINOR_VERSION 28
#define FUSE_ROOT_ID 1

enum fuse_opcode {
    FUSE_LOOKUP     = 1,
    FUSE_FORGET     = 2,
    FUSE_GETATTR    = 3,
    FUSE_SETATTR    = 4,
    FUSE_STATFS     = 17,
    FUSE_INIT       = 26,
    FUSE_OPEN       = 14,
    FUSE_READ       = 15,
    FUSE_WRITE      = 16,
    FUSE_FLUSH      = 20,
    FUSE_RELEASE    = 18,
    FUSE_FSYNC      = 3, /* reuse */
    FUSE_OPENDIR    = 27,
    FUSE_READDIR    = 28,
    FUSE_RELEASEDIR = 29,
};

struct fuse_in_header {
    uint32_t len;
    uint32_t opcode;
    uint64_t unique;
    uint64_t nodeid;
    uint32_t uid;
    uint32_t gid;
    uint32_t pid;
    uint32_t padding;
};

struct fuse_out_header {
    uint32_t len;
    int32_t  error;
    uint64_t unique;
};

struct fuse_init_in {
    uint32_t major;
    uint32_t minor;
    uint32_t max_readahead;
    uint32_t flags;
};

struct fuse_init_out {
    uint32_t major;
    uint32_t minor;
    uint32_t max_readahead;
    uint32_t flags;
    uint16_t max_background;
    uint16_t congestion_threshold;
    uint32_t max_write;
    uint32_t time_gran;
    uint16_t max_pages;
    uint16_t padding;
    uint32_t unused[8];
};

struct fuse_attr {
    uint64_t ino;
    uint64_t size;
    uint64_t blocks;
    uint64_t atime;
    uint64_t mtime;
    uint64_t ctime;
    uint32_t atimensec;
    uint32_t mtimensec;
    uint32_t ctimensec;
    uint32_t mode;
    uint32_t nlink;
    uint32_t uid;
    uint32_t gid;
    uint32_t rdev;
    uint32_t blksize;
    uint32_t padding;
};

struct fuse_entry_out {
    uint64_t nodeid;
    uint64_t generation;
    uint64_t entry_valid;
    uint64_t attr_valid;
    uint32_t entry_valid_nsec;
    uint32_t attr_valid_nsec;
    struct fuse_attr attr;
};

struct fuse_attr_out {
    uint64_t attr_valid;
    uint32_t attr_valid_nsec;
    uint32_t dummy;
    struct fuse_attr attr;
};

struct fuse_kstatfs {
    uint64_t blocks;
    uint64_t bfree;
    uint64_t bavail;
    uint64_t files;
    uint64_t ffree;
    uint32_t bsize;
    uint32_t namelen;
    uint32_t frsize;
    uint32_t padding;
    uint32_t spare[6];
};

struct fuse_statfs_out {
    struct fuse_kstatfs st;
};

struct fuse_getattr_in {
    uint32_t getattr_flags;
    uint32_t dummy;
    uint64_t fh;
};

struct fuse_open_in {
    uint32_t flags;
    uint32_t unused;
};

struct fuse_open_out {
    uint64_t fh;
    uint32_t open_flags;
    uint32_t padding;
};

struct fuse_read_in {
    uint64_t fh;
    uint64_t offset;
    uint32_t size;
    uint32_t read_flags;
    uint64_t lock_owner;
    uint32_t flags;
    uint32_t padding;
};

struct fuse_release_in {
    uint64_t fh;
    uint32_t flags;
    uint32_t release_flags;
    uint64_t lock_owner;
};

/* fuse_mount_info from <vfs/fuse/fuse_mount.h> */
struct fuse_mount_info {
    int flags;
    int fd;
    int max_read;
    const char *subtype;
    const char *from;
};

#define FUSE_MOUNT_DEFAULT_PERMISSIONS 0x01
#define FUSE_MOUNT_ALLOW_OTHER         0x02

#define TARGET_INO  100
#define TARGET_NAME "target"

static volatile int g_running = 1;

static void
sighandler(int sig)
{
    (void)sig;
    g_running = 0;
}

/* Fill a fuse_attr for the given inode */
static void
fill_attr(struct fuse_attr *a, uint64_t ino)
{
    memset(a, 0, sizeof(*a));
    a->ino = ino;
    a->nlink = 1;
    a->uid = 0;
    a->gid = 0;
    a->blksize = 4096;
    if (ino == FUSE_ROOT_ID) {
        a->mode = 0040755;  /* dir */
        a->nlink = 2;
    } else {
        a->mode = 0100644;  /* regular file */
        a->size = 0;
    }
}

/* Write a reply: out_header + optional payload */
static int
write_reply(int fd, uint64_t unique, int error, const void *payload, size_t plen)
{
    struct {
        struct fuse_out_header ohd;
        char buf[8192];
    } resp;

    memset(&resp.ohd, 0, sizeof(resp.ohd));
    resp.ohd.unique = unique;
    resp.ohd.error = error;
    resp.ohd.len = sizeof(resp.ohd) + plen;
    if (plen > 0 && plen <= sizeof(resp.buf))
        memcpy(resp.buf, payload, plen);

    return write(fd, &resp, resp.ohd.len);
}

static int
write_err(int fd, uint64_t unique, int err)
{
    return write_reply(fd, unique, err, NULL, 0);
}

static void
serve_fuse(int fd)
{
    unsigned char inbuf[65536];
    ssize_t n;

    while (g_running) {
        n = read(fd, inbuf, sizeof(inbuf));
        if (n < 0) {
            if (errno == EINTR) continue;
            perror("read /dev/fuse");
            break;
        }
        if (n < (ssize_t)sizeof(struct fuse_in_header))
            continue;

        struct fuse_in_header *ih = (struct fuse_in_header *)inbuf;
        char *name = (char *)(inbuf + sizeof(*ih));
        uint64_t unique = ih->unique;

        switch (ih->opcode) {
        case FUSE_INIT: {
            struct fuse_init_out io;
            memset(&io, 0, sizeof(io));
            io.major = FUSE_KERNEL_VERSION;
            io.minor = FUSE_KERNEL_MINOR_VERSION;
            io.max_readahead = 4096;
            io.max_write = 4096;
            write_reply(fd, unique, 0, &io, sizeof(io));
            break;
        }
        case FUSE_STATFS: {
            struct fuse_statfs_out so;
            memset(&so, 0, sizeof(so));
            so.st.blocks = 1000000;
            so.st.bfree = 500000;
            so.st.bavail = 500000;
            so.st.files = 100;
            so.st.ffree = 50;
            so.st.bsize = 4096;
            so.st.namelen = 255;
            so.st.frsize = 4096;
            write_reply(fd, unique, 0, &so, sizeof(so));
            break;
        }
        case FUSE_LOOKUP: {
            /* name is null-terminated in the request */
            if (name && strcmp(name, TARGET_NAME) == 0) {
                struct fuse_entry_out eo;
                memset(&eo, 0, sizeof(eo));
                eo.nodeid = TARGET_INO;
                eo.generation = 1;
                eo.entry_valid = 1;
                eo.attr_valid = 1;
                fill_attr(&eo.attr, TARGET_INO);
                write_reply(fd, unique, 0, &eo, sizeof(eo));
            } else if (name && (strcmp(name, ".") == 0 ||
                                strcmp(name, "..") == 0)) {
                struct fuse_entry_out eo;
                memset(&eo, 0, sizeof(eo));
                eo.nodeid = FUSE_ROOT_ID;
                eo.generation = 1;
                eo.entry_valid = 1;
                eo.attr_valid = 1;
                fill_attr(&eo.attr, FUSE_ROOT_ID);
                write_reply(fd, unique, 0, &eo, sizeof(eo));
            } else {
                write_err(fd, unique, -ENOENT);
            }
            break;
        }
        case FUSE_GETATTR: {
            struct fuse_attr_out ao;
            memset(&ao, 0, sizeof(ao));
            ao.attr_valid = 1;
            fill_attr(&ao.attr, ih->nodeid);
            write_reply(fd, unique, 0, &ao, sizeof(ao));
            break;
        }
        case FUSE_OPEN:
        case FUSE_OPENDIR: {
            struct fuse_open_out oo;
            memset(&oo, 0, sizeof(oo));
            oo.fh = ih->nodeid;
            write_reply(fd, unique, 0, &oo, sizeof(oo));
            break;
        }
        case FUSE_READ: {
            /* Return empty data */
            write_reply(fd, unique, 0, NULL, 0);
            break;
        }
        case FUSE_READDIR: {
            /* Return empty dir */
            write_reply(fd, unique, 0, NULL, 0);
            break;
        }
        case FUSE_RELEASE:
        case FUSE_RELEASEDIR:
        case FUSE_FLUSH:
            write_reply(fd, unique, 0, NULL, 0);
            break;
        case FUSE_FORGET:
            /* No reply */
            break;
        default:
            write_err(fd, unique, -ENOSYS);
            break;
        }
    }
}

int
main(int argc, char **argv)
{
    const char *mntpt;
    int fusefd;
    pid_t pid;

    if (argc < 2) {
        fprintf(stderr, "usage: %s <mountpoint>\n", argv[0]);
        return 1;
    }
    mntpt = argv[1];

    signal(SIGTERM, sighandler);
    signal(SIGINT, sighandler);

    /* Open /dev/fuse โ€” creates the per-open fuse_mount context */
    fusefd = open("/dev/fuse", O_RDWR);
    if (fusefd < 0) {
        perror("open /dev/fuse");
        fprintf(stderr, "Is the fuse module loaded? (kldload fuse)\n");
        return 1;
    }

    /* Fork: child serves FUSE protocol, parent mounts */
    pid = fork();
    if (pid < 0) {
        perror("fork");
        return 1;
    }

    if (pid == 0) {
        /* Child: serve FUSE requests on fusefd */
        serve_fuse(fusefd);
        _exit(0);
    }

    /* Parent: mount the filesystem using the fd */
    struct fuse_mount_info args;
    memset(&args, 0, sizeof(args));
    args.fd = fusefd;
    args.from = "/dev/fuse";
    args.flags = 0;

    if (mount("fuse", mntpt, 0, &args) < 0) {
        perror("mount fuse");
        kill(pid, SIGTERM);
        return 1;
    }

    fprintf(stderr, "rawfuse: mounted %s (fusefd=%d, server pid=%d)\n",
            mntpt, fusefd, pid);

    /* Wait for signal; child keeps serving */
    /* The child process holds the fd and serves forever.
     * Parent can exit โ€” the mount stays up as long as the child runs. */
    /* Actually, keep parent alive so we can unmount cleanly */
    int status;
    waitpid(pid, &status, 0);

    /* Unmount */
    unmount(mntpt, 0);
    fprintf(stderr, "rawfuse: unmounted %s\n", mntpt);

    return 0;
}