Skip to content

Commit

Permalink
Allow passing /dev/fuse file descriptor from parent process
Browse files Browse the repository at this point in the history
This adds support for a mode of operation in which a privileged parent
process opens `/dev/fuse` and takes care of mounting. The FUSE file
system daemon can then run as an unprivileged child that merely
processes requests on the FUSE file descriptor, which get passed using
the special `/dev/fd/%u` syntax for the mountpoint parameter.

The main benefit is that no privileged operations need to be performed
by the FUSE file system daemon itself directly or indirectly, so the
FUSE process can run with fully unprivileged and mechanisms like
securebits and no_new_privs can be used to prevent subprocesses from
re-acquiring privilege via setuid, fscaps, etc. This reduces risk in
case the FUSE file system gets exploited by malicious file system
data.

Below is an example that illustrates this. Note that I'm using shell
for presentation purposes, the expectation is that the parent process
will implement the equivalent of the `mount -i` and `capsh` commands.

```
\# example/hello can mount successfully with privilege
$ sudo sh -c "LD_LIBRARY_PATH=build/lib ./example/hello /mnt/tmp"
$ sudo cat /mnt/tmp/hello
Hello World!
$ sudo umount /mnt/tmp

\# example/hello fails to mount without privilege
$ sudo capsh --drop=all --secbits=0x2f -- -c 'LD_LIBRARY_PATH=build/lib ./example/hello -f /mnt/tmp'
fusermount3: mount failed: Operation not permitted

\# Passing FUSE file descriptor via /dev/fd/%u allows example/hello to work without privilege
$ sudo sh -c '
      exec 17<>/dev/fuse
      mount -i -o nodev,nosuid,noexec,fd=17,rootmode=40000,user_id=0,group_id=0 -t fuse hello /mnt/tmp
      capsh --drop=all --secbits=0x2f -- -c "LD_LIBRARY_PATH=build/lib example/hello /dev/fd/17"
    '
$ sudo cat /mnt/tmp/hello
Hello World!
$ sudo umount /mnt/tmp
```
  • Loading branch information
saittam authored and Nikratio committed Oct 9, 2018
1 parent 4a6a5da commit 64e1107
Show file tree
Hide file tree
Showing 5 changed files with 48 additions and 4 deletions.
4 changes: 4 additions & 0 deletions ChangeLog.rst
Expand Up @@ -8,6 +8,10 @@ Unreleased Changes
* The description of the FUSE_CAP_READDIRPLUS_AUTO flag has been
improved.

* Allow open `/dev/fuse` file descriptors to be passed via mountpoints of the
special format `/dev/fd/%u`. This allows mounting to be handled by the parent
so the FUSE filesystem process can run fully unprivileged.

libfuse 3.2.6 (2018-08-31)
==========================

Expand Down
27 changes: 24 additions & 3 deletions lib/fuse_lowlevel.c
Expand Up @@ -16,6 +16,7 @@
#include "fuse_kernel.h"
#include "fuse_opt.h"
#include "fuse_misc.h"
#include "mount_util.h"

#include <stdio.h>
#include <stdlib.h>
Expand Down Expand Up @@ -2891,6 +2892,24 @@ int fuse_session_mount(struct fuse_session *se, const char *mountpoint)
close(fd);
} while (fd >= 0 && fd <= 2);

/*
* To allow FUSE daemons to run without privileges, the caller may open
* /dev/fuse before launching the file system and pass on the file
* descriptor by specifying /dev/fd/N as the mount point. Note that the
* parent process takes care of performing the mount in this case.
*/
fd = fuse_mnt_parse_fuse_fd(mountpoint);
if (fd != -1) {
if (fcntl(fd, F_GETFD) == -1) {
fprintf(stderr,
"fuse: Invalid file descriptor /dev/fd/%u\n",
fd);
return -1;
}
se->fd = fd;
return 0;
}

/* Open channel */
fd = fuse_kern_mount(mountpoint, se->mo);
if (fd == -1)
Expand All @@ -2916,9 +2935,11 @@ int fuse_session_fd(struct fuse_session *se)

void fuse_session_unmount(struct fuse_session *se)
{
fuse_kern_unmount(se->mountpoint, se->fd);
free(se->mountpoint);
se->mountpoint = NULL;
if (se->mountpoint != NULL) {
fuse_kern_unmount(se->mountpoint, se->fd);
free(se->mountpoint);
se->mountpoint = NULL;
}
}

#ifdef linux
Expand Down
7 changes: 6 additions & 1 deletion lib/helper.c
Expand Up @@ -15,6 +15,7 @@
#include "fuse_misc.h"
#include "fuse_opt.h"
#include "fuse_lowlevel.h"
#include "mount_util.h"

#include <stdio.h>
#include <stdlib.h>
Expand Down Expand Up @@ -147,7 +148,11 @@ static int fuse_helper_opt_proc(void *data, const char *arg, int key,
switch (key) {
case FUSE_OPT_KEY_NONOPT:
if (!opts->mountpoint) {
char mountpoint[PATH_MAX];
if (fuse_mnt_parse_fuse_fd(arg) != -1) {
return fuse_opt_add_opt(&opts->mountpoint, arg);
}

char mountpoint[PATH_MAX] = "";
if (realpath(arg, mountpoint) == NULL) {
fprintf(stderr,
"fuse: bad mount point `%s': %s\n",
Expand Down
13 changes: 13 additions & 0 deletions lib/mount_util.c
Expand Up @@ -352,3 +352,16 @@ int fuse_mnt_check_fuseblk(void)
fclose(f);
return 0;
}

int fuse_mnt_parse_fuse_fd(const char *mountpoint)
{
int fd = -1;
int len = 0;

if (sscanf(mountpoint, "/dev/fd/%u%n", &fd, &len) == 1 &&
len == strlen(mountpoint)) {
return fd;
}

return -1;
}
1 change: 1 addition & 0 deletions lib/mount_util.h
Expand Up @@ -15,3 +15,4 @@ int fuse_mnt_umount(const char *progname, const char *abs_mnt,
const char *rel_mnt, int lazy);
char *fuse_mnt_resolve_path(const char *progname, const char *orig);
int fuse_mnt_check_fuseblk(void);
int fuse_mnt_parse_fuse_fd(const char *mountpoint);

0 comments on commit 64e1107

Please sign in to comment.