Skip to contentSkip to conversation

Proxy FS driver for VirtFS

PATCH Superseded M. Mohan Kumar 2 participants 20 messages Nov 15, 2011 – Nov 17, 2011 Kbuild

v2 of 5 Superseded Latest is v5 →

lore
Share & tools
lore
#0 lore raw

v2 cover Proxy FS driver for VirtFS

Pass-through security model in QEMU 9p server needs root privilege to do few file operations (like chown, chmod to any mode/uid:gid). There are two issues in pass-through security model

1) TOCTTOU vulnerability: Following symbolic links in the server could provide access to files beyond 9p export path.

2) Running QEMU with root privilege could be a security issue.

To overcome above issues, following approach is used: A new filesytem type 'proxy' is introduced. Proxy FS uses chroot + socket combination for securing the vulnerability known with following symbolic links. Intention of adding a new filesystem type is to allow qemu to run in non-root mode, but doing privileged operations using socket IO.

Proxy helper(a stand alone binary part of qemu) is invoked with root privileges. Proxy helper chroots into 9p export path and creates a socket pair or a named socket based on the command line parameter. Qemu and proxy helper communicate using this socket. QEMU proxy fs driver sends filesystem request to proxy helper and receives the response from it.

Proxy helper is designed so that it can drop the root privilege but retaining capbilities that are needed for doing filesystem operations (like CAP_DAC_OVERRIDE, CAP_FOWNER etc)

M. Mohan Kumar (12):
hw/9pfs: Move pdu_marshal/unmarshal code to a seperate file hw/9pfs: Add new proxy filesystem driver
hw/9pfs: File system helper process for qemu 9p proxy FS hw/9pfs: Open and create files
hw/9pfs: Create other filesystem objects
hw/9pfs: Add stat/readlink/statfs for proxy FS hw/9pfs: File ownership and others
hw/9pfs: xattr interfaces in proxy filesystem driver hw/9pfs: Proxy getversion
hw/9pfs: Documentation changes related to proxy fs hw/9pfs: man page for proxy helper
hw/9pfs: Add support to use named socket for proxy FS

 Makefile                       |   15 +-
 Makefile.objs                  |    4 +-
 configure                      |   19 +
 fsdev/file-op-9p.h             |    3 +-
 fsdev/qemu-fsdev.c             |    1 +
 fsdev/qemu-fsdev.h             |    1 +
 fsdev/virtfs-proxy-helper.c    |  947 +++++++++++++++++++++++++++++++++
 fsdev/virtfs-proxy-helper.texi |   63 +++
 fsdev/virtio-9p-marshal.c      |  338 ++++++++++++
 fsdev/virtio-9p-marshal.h      |   87 +++
 hw/9pfs/virtio-9p-proxy.c      | 1123 ++++++++++++++++++++++++++++++++++++++++
 hw/9pfs/virtio-9p-proxy.h      |   80 +++
 hw/9pfs/virtio-9p.c            |  297 +-----------
 hw/9pfs/virtio-9p.h            |   85 +---
 qemu-config.c                  |   13 +
 qemu-options.hx                |   32 +-
 vl.c                           |   10 +-
 17 files changed, 2736 insertions(+), 382 deletions(-)
 create mode 100644 fsdev/virtfs-proxy-helper.c
 create mode 100644 fsdev/virtfs-proxy-helper.texi
 create mode 100644 fsdev/virtio-9p-marshal.c
 create mode 100644 fsdev/virtio-9p-marshal.h
 create mode 100644 hw/9pfs/virtio-9p-proxy.c
 create mode 100644 hw/9pfs/virtio-9p-proxy.h
Signature · 1 line
1.7.6
#1 lore raw

PATCH v2 1/12 hw/9pfs: Move pdu_marshal/unmarshal code to a seperate file

Move p9 marshaling/unmarshaling code to a separate file so that proxy filesytem driver can use these calls. Also made marshaling code generic to accept "struct iovec" instead of V9fsPDU.

---
 Makefile.objs             |    2 +-
 fsdev/virtio-9p-marshal.c |  338 +++++++++++++++++++++++++++++++++++++++++++++
 fsdev/virtio-9p-marshal.h |   87 ++++++++++++
 hw/9pfs/virtio-9p.c       |  297 +---------------------------------------
 hw/9pfs/virtio-9p.h       |   85 ++----------
 5 files changed, 440 insertions(+), 369 deletions(-)
 create mode 100644 fsdev/virtio-9p-marshal.c
 create mode 100644 fsdev/virtio-9p-marshal.h
Makefile.objs +1 -1
index d7a6539..c256fdc 100644
@@ -61,7 +61,7 @@ ifeq ($(CONFIG_VIRTIO)$(CONFIG_VIRTFS)$(CONFIG_PCI),yyy)
 # Lots of the fsdev/9pcode is pulled in by vl.c via qemu_fsdev_add.
 # only pull in the actual virtio-9p device if we also enabled virtio.
 CONFIG_REALLY_VIRTFS=y
-fsdev-nested-y = qemu-fsdev.o
+fsdev-nested-y = qemu-fsdev.o virtio-9p-marshal.o
 else
 fsdev-nested-y = qemu-fsdev-dummy.o
 endif
fsdev/virtio-9p-marshal.c new file +338 -0

338 lines not on this page · raw message

fsdev/virtio-9p-marshal.h new file +87 -0

87 lines not on this page · raw message

hw/9pfs/virtio-9p.c +4 -293

321 lines not on this page · raw message

hw/9pfs/virtio-9p.h +10 -75

121 lines not on this page · raw message

Signature · 1 line
1.7.6
#2 lore raw

PATCH v2 2/12 hw/9pfs: Add new proxy filesystem driver

In reply to M. Mohan Kumar

Add new proxy filesystem driver to add root privilege to qemu process. It needs a helper process to be started by root user.

Following command line can be used to utilize proxy filesystem driver -virtfs proxy,id=<id>,mount_tag=<tag>,socket_fd=<socket-fd>

---
 Makefile.objs             |    2 +-
 fsdev/file-op-9p.h        |    1 -
 fsdev/qemu-fsdev.c        |    1 +
 fsdev/qemu-fsdev.h        |    1 +
 fsdev/virtio-9p-marshal.h |    2 +-
 hw/9pfs/virtio-9p-proxy.c |  374 +++++++++++++++++++++++++++++++++++++++++++++
 hw/9pfs/virtio-9p-proxy.h |   10 ++
 qemu-config.c             |    6 +
 vl.c                      |    6 +-
 9 files changed, 399 insertions(+), 4 deletions(-)
 create mode 100644 hw/9pfs/virtio-9p-proxy.c
 create mode 100644 hw/9pfs/virtio-9p-proxy.h
Makefile.objs +1 -1
index c256fdc..8201202 100644
@@ -311,7 +311,7 @@ hw-obj-$(CONFIG_SOUND) += $(sound-obj-y)
 9pfs-nested-$(CONFIG_VIRTFS) += virtio-9p-xattr-user.o virtio-9p-posix-acl.o
 9pfs-nested-$(CONFIG_VIRTFS) += virtio-9p-coth.o cofs.o codir.o cofile.o
 9pfs-nested-$(CONFIG_VIRTFS) += coxattr.o virtio-9p-handle.o
-9pfs-nested-$(CONFIG_VIRTFS) += virtio-9p-synth.o
+9pfs-nested-$(CONFIG_VIRTFS) += virtio-9p-synth.o virtio-9p-proxy.o
 
 hw-obj-$(CONFIG_REALLY_VIRTFS) += $(addprefix 9pfs/, $(9pfs-nested-y))
 $(addprefix 9pfs/, $(9pfs-nested-y)): QEMU_CFLAGS+=$(GLIB_CFLAGS)
fsdev/file-op-9p.h +0 -1
index 22849c9..84e5375 100644
@@ -60,7 +60,6 @@ typedef struct extended_ops {
 
 #define V9FS_SEC_MASK               0x0000001C
 
-
 typedef struct FileOperations FileOperations;
 /*
  * Structure to store the various fsdev's passed through command line.
fsdev/qemu-fsdev.c +1 -0
index efbfea1..b31d116 100644
@@ -25,6 +25,7 @@ static FsDriverTable FsDrivers[] = {
     { .name = "local", .ops = &local_ops},
     { .name = "handle", .ops = &handle_ops},
     { .name = "synth", .ops = &synth_ops},
+    { .name = "proxy", .ops = &proxy_ops},
 };
 
 int qemu_fsdev_add(QemuOpts *opts)
fsdev/qemu-fsdev.h +1 -0
index 921452d..1af1f54 100644
@@ -44,4 +44,5 @@ FsDriverEntry *get_fsdev_fsentry(char *id);
 extern FileOperations local_ops;
 extern FileOperations handle_ops;
 extern FileOperations synth_ops;
+extern FileOperations proxy_ops;
 #endif
fsdev/virtio-9p-marshal.h +1 -1
index fe2d34b..45dba20 100644
@@ -30,7 +30,7 @@ typedef struct V9fsStat
     V9fsString muid;
     /* 9p2000.u */
     V9fsString extension;
-   int32_t n_uid;
+    int32_t n_uid;
     int32_t n_gid;
     int32_t n_muid;
 } V9fsStat;
hw/9pfs/virtio-9p-proxy.c new file +374 -0

374 lines not on this page · raw message

hw/9pfs/virtio-9p-proxy.h new file +10 -0
new file mode 100644
index 0000000..f5e1a02
@@ -0,0 +1,10 @@
+#ifndef _QEMU_VIRTIO_9P_PROXY_H
+#define _QEMU_VIRTIO_9P_PROXY_H
+
+#define BUFF_SZ (4 * 1024)
+
+typedef struct {
+    int type;
+    int size;
+} ProxyHeader;
+#endif
qemu-config.c +6 -0
index 597d7e1..33367fe 100644
@@ -183,6 +183,9 @@ QemuOptsList qemu_fsdev_opts = {
         }, {
             .name = "readonly",
             .type = QEMU_OPT_BOOL,
+        }, {
+            .name = "sock_fd",
+            .type = QEMU_OPT_NUMBER,
         },
 
         { /*End of list */ }
@@ -212,6 +215,9 @@ QemuOptsList qemu_virtfs_opts = {
         }, {
             .name = "readonly",
             .type = QEMU_OPT_BOOL,
+        }, {
+            .name = "sock_fd",
+            .type = QEMU_OPT_NUMBER,
         },
 
         { /*End of list */ }
vl.c +5 -1
index e3b2443..1feaf37 100644
@@ -2663,7 +2663,7 @@ int main(int argc, char **argv, char **envp)
             case QEMU_OPTION_virtfs: {
                 QemuOpts *fsdev;
                 QemuOpts *device;
-                const char *writeout;
+                const char *writeout, *sock_fd;
 
                 olist = qemu_find_opts("virtfs");
                 if (!olist) {
@@ -2703,6 +2703,10 @@ int main(int argc, char **argv, char **envp)
                 qemu_opt_set(fsdev, "path", qemu_opt_get(opts, "path"));
                 qemu_opt_set(fsdev, "security_model",
                              qemu_opt_get(opts, "security_model"));
+                sock_fd = qemu_opt_get(opts, "sock_fd");
+                if (sock_fd) {
+                    qemu_opt_set(fsdev, "sock_fd", sock_fd);
+                }
 
                 qemu_opt_set_bool(fsdev, "readonly",
                                 qemu_opt_get_bool(opts, "readonly", 0));
Signature · 1 line
1.7.6
#3 lore raw

PATCH v2 3/12 hw/9pfs: File system helper process for qemu 9p proxy FS

In reply to M. Mohan Kumar

Provide root privilege access to QEMU 9p proxy filesystem using socket communication.

Proxy helper is started by root user as:
~ # virtfs-proxy-helper -f|--fd <socket descriptor> -p|--path <path-to-share>

---
 Makefile                    |    3 +
 configure                   |   19 +++
 fsdev/virtfs-proxy-helper.c |  271 +++++++++++++++++++++++++++++++++++++++++++
 hw/9pfs/virtio-9p-proxy.h   |   10 ++
 4 files changed, 303 insertions(+), 0 deletions(-)
 create mode 100644 fsdev/virtfs-proxy-helper.c
Makefile +3 -0
index ba8d738..19b481a 100644
@@ -153,6 +153,9 @@ qemu-img$(EXESUF): qemu-img.o $(tools-obj-y) $(block-obj-y)
 qemu-nbd$(EXESUF): qemu-nbd.o $(tools-obj-y) $(block-obj-y)
 qemu-io$(EXESUF): qemu-io.o cmd.o $(tools-obj-y) $(block-obj-y)
 
+fsdev/virtfs-proxy-helper$(EXESUF): fsdev/virtfs-proxy-helper.o fsdev/virtio-9p-marshal.o
+fsdev/virtfs-proxy-helper$(EXESUF): LIBS += -lcap
+
 qemu-img-cmds.h: $(SRC_PATH)/qemu-img-cmds.hx
 	$(call quiet-command,sh $(SRC_PATH)/scripts/hxtool -h < $< > $@,"  GEN   $@")
 
configure +19 -0
index a6cf6d6..a2b55e8 100755
@@ -1873,6 +1873,22 @@ else
 fi
 
 ##########################################
+# libcap probe
+
+if test "$cap" != "no" ; then
+  cat > $TMPC <<EOF
+#include <stdio.h>
+#include <sys/capability.h>
+int main(void) { cap_t caps; caps = cap_init(); }
+EOF
+  if compile_prog "" "-lcap" ; then
+    cap=yes
+  else
+    cap=no
+  fi
+fi
+
+##########################################
 # pthread probe
 PTHREADLIBS_LIST="-pthread -lpthread -lpthreadGC2"
 
@@ -2662,6 +2678,9 @@ confdir=$sysconfdir$confsuffix
 tools=
 if test "$softmmu" = yes ; then
   tools="qemu-img\$(EXESUF) qemu-io\$(EXESUF) $tools"
+  if [ "$cap" = "yes" -a "$linux" = "yes" ] ; then
+      tools="$tools fsdev/virtfs-proxy-helper\$(EXESUF)"
+  fi
   if [ "$linux" = "yes" -o "$bsd" = "yes" -o "$solaris" = "yes" ] ; then
       tools="qemu-nbd\$(EXESUF) $tools"
     if [ "$guest_agent" = "yes" ]; then
fsdev/virtfs-proxy-helper.c new file +271 -0

271 lines not on this page · raw message

hw/9pfs/virtio-9p-proxy.h +10 -0
index f5e1a02..120e940 100644
@@ -3,6 +3,16 @@
 
 #define BUFF_SZ (4 * 1024)
 
+#define proxy_unmarshal(in_sg, in_elem, offset, fmt, args...) \
+    v9fs_unmarshal(in_sg, in_elem, offset, 0 /* convert */, fmt, ##args)
+#define proxy_marshal(out_sg, out_elem, offset, fmt, args...) \
+    v9fs_marshal(out_sg, out_elem, offset, 0 /* convert */, fmt, ##args)
+
+union MsgControl {
+    struct cmsghdr cmsg;
+    char control[CMSG_SPACE(sizeof(int))];
+};
+
 typedef struct {
     int type;
     int size;
Signature · 1 line
1.7.6
#4 lore raw

Re: [Qemu-devel] [PATCH V2 03/12] hw/9pfs: File system helper process for qemu 9p proxy FS

Quoted 24 lines · M. Mohan Kumar
fsdev/virtfs-proxy-helper.c new file +18 -0
new file mode 100644
index 0000000..69daf7c
@@ -0,0 +1,271 @@
+/*
+ * Helper for QEMU Proxy FS Driver
+ * Copyright IBM, Corp. 2011
+ *
+ * Authors:
+ * M. Mohan Kumar <[email protected]>
+ *
+ * This work is licensed under the terms of the GNU GPL, version 2. See
+ * the COPYING file in the top-level directory.
+ */
+#include <stdio.h>
+#include <sys/socket.h>
+#include <string.h>
+#include <sys/un.h>
+#include <limits.h>
+#include <signal.h>
+#include <errno.h>
+#include <stdlib.h>

fsdev/virtfs-proxy-helper.c line 28

Quoted 10 lines
fsdev/virtfs-proxy-helper.c new file +10 -0
+#include <sys/resource.h>
+#include <sys/stat.h>
+#include <getopt.h>
+#include <unistd.h>
+#include <syslog.h>
+#include <sys/prctl.h>
+#include <sys/capability.h>
+#include <sys/fsuid.h>
+#include <stdarg.h>
+#include "bswap.h"

Where is "bswap.h" used and why above <sys/socket.h>?

Quoted 4 lines
fsdev/virtfs-proxy-helper.c new file +4 -0
+#include <sys/socket.h>
+#include "qemu-common.h"
+#include "virtio-9p-marshal.h"
+#include "hw/9pfs/virtio-9p-proxy.h"

fsdev/virtfs-proxy-helper.c line 42

Quoted 10 lines
fsdev/virtfs-proxy-helper.c new file +10 -0
+
+#define PROGNAME "virtfs-proxy-helper"
+
+static struct option helper_opts[] = {
+    {"fd", required_argument, NULL, 'f'},
+    {"path", required_argument, NULL, 'p'},
+    {"nodaemon", no_argument, NULL, 'n'},
+};
+
+int is_daemon;

static?

Also, please use the bool type from <stdbool.h>, it makes it easier for readers who don't have to guess how the variable works (might be a bitfield or reference count too).

fsdev/virtfs-proxy-helper.c line 128

Quoted 10 lines
fsdev/virtfs-proxy-helper.c new file +10 -0
+static int socket_read(int sockfd, void *buff, ssize_t size)
+{
+    int retval;
+
+    do {
+        retval = read(sockfd, buff, size);
+    } while (retval < 0 && errno == EINTR);
+    if (retval != size) {
+        return -EIO;
+    }

Shouldn't this loop until size bytes have been read?

fsdev/virtfs-proxy-helper.c line 140

Quoted 12 lines
fsdev/virtfs-proxy-helper.c new file +12 -0
+    return retval;
+}
+
+static int socket_write(int sockfd, void *buff, ssize_t size)
+{
+    int retval;
+
+    do {
+        retval = write(sockfd, buff, size);
+    } while (retval < 0 && errno == EINTR);
+    if (retval != size) {
+        return -EIO;

We could pass the actual -errno here if retval < 0.

Quoted 13 lines
fsdev/virtfs-proxy-helper.c new file +13 -0
+    }
+    return retval;
+}
+
+static int read_request(int sockfd, struct iovec *iovec)
+{
+    int retval;
+    ProxyHeader header;
+
+    /* read the header */
+    retval = socket_read(sockfd, iovec->iov_base, sizeof(header));
+    if (retval != sizeof(header)) {
+        return -EIO;

fsdev/virtfs-proxy-helper.c line 163

Quoted 10 lines
fsdev/virtfs-proxy-helper.c new file +10 -0
+    }
+    /* unmarshal header */
+    proxy_unmarshal(iovec, 1, 0, "dd", &header.type, &header.size);
+    /* read the request */
+    retval = socket_read(sockfd, iovec->iov_base + sizeof(header), header.size);
+    if (retval != header.size) {
+        return -EIO;
+    }
+    return header.type;
+}

Size checks are missing and we're trusting what the client sends!

Quoted 39 lines
fsdev/virtfs-proxy-helper.c new file +39 -0
+
+static void usage(char *prog)
+{
+    fprintf(stderr, "usage: %s\n"
+            " -p|--path <path> 9p path to export\n"
+            " {-f|--fd <socket-descriptor>} socket file descriptor to be used\n"
+            " [-n|--nodaemon] Run as a normal program\n",
+            basename(prog));
+}
+
+static int process_requests(int sock)
+{
+    int type;
+    struct iovec iovec;
+
+    iovec.iov_base = g_malloc(BUFF_SZ);
+    iovec.iov_len = BUFF_SZ;
+    while (1) {
+        type = read_request(sock, &iovec);
+        if (type <= 0) {
+            goto error;
+        }
+    }
+    (void)socket_write;
+error:
+    g_free(iovec.iov_base);
+    return -1;
+}
+
+int main(int argc, char **argv)
+{
+    int sock;
+    char rpath[PATH_MAX];
+    struct stat stbuf;
+    int c, option_index;
+
+    is_daemon = 1;
+    rpath[0] = '\0';
+    sock = -1;

fsdev/virtfs-proxy-helper.c line 212

Quoted 10 lines
fsdev/virtfs-proxy-helper.c new file +10 -0
+    while (1) {
+        option_index = 0;
+        c = getopt_long(argc, argv, "p:nh?f:", helper_opts,
+                        &option_index);
+        if (c == -1) {
+            break;
+        }
+        switch (c) {
+        case 'p':
+            strcpy(rpath, optarg);

Buffer overflow. The whole thing would be simpler like this:

const char *rpath = "";
[...]

    case 'p':
        rpath = optarg;
        break;

fsdev/virtfs-proxy-helper.c line 224

Quoted 12 lines
fsdev/virtfs-proxy-helper.c new file +12 -0
+            break;
+        case 'n':
+            is_daemon = 0;
+            break;
+        case 'f':
+            sock = atoi(optarg);
+            break;
+        case '?':
+        case 'h':
+        default:
+            usage(argv[0]);
+            return -1;

The convention is for programs to exit with 1 (EXIT_FAILURE) on error.

fsdev/virtfs-proxy-helper.c line 237

Quoted 13 lines
fsdev/virtfs-proxy-helper.c new file +13 -0
+            break;
+        }
+    }
+
+    /* Parameter validation */
+    if (sock == -1 || rpath[0] == '\0') {
+        fprintf(stderr, "socket descriptor or path not specified\n");
+        usage(argv[0]);
+        return -1;
+    }
+
+    if (lstat(rpath, &stbuf) < 0) {
+        fprintf(stderr, "invalid path \"%s\" specified?\n", rpath);

sterror() would provide further details on what went wrong.

Quoted 41 lines
fsdev/virtfs-proxy-helper.c new file +34 -0
+        return -1;
+    }
+
+    if (!S_ISDIR(stbuf.st_mode)) {
+        fprintf(stderr, "specified path \"%s\" is not directory\n", rpath);
+        return -1;
+    }
+
+    if (is_daemon) {
+        if (daemon(0, 0) < 0) {
+            fprintf(stderr, "daemon call failed\n");
+            return -1;
+        }
+        openlog(PROGNAME, LOG_PID, LOG_DAEMON);
+    }
+
+    do_log(LOG_INFO, "Started");
+
+    if (chroot(rpath) < 0) {
+        do_perror("chroot");
+        goto error;
+    }
+    umask(0);
+
+    if (init_capabilities() < 0) {
+        goto error;
+    }
+
+    process_requests(sock);
+error:
+    do_log(LOG_INFO, "Done");
+    closelog();
+    return 0;
+}
hw/9pfs/virtio-9p-proxy.h +0 -0
index f5e1a02..120e940 100644
@@ -3,6 +3,16 @@
 
  #define BUFF_SZ (4 * 1024)

hw/9pfs/virtio-9p-proxy.h line 14

Quoted 10 lines
hw/9pfs/virtio-9p-proxy.h +9 -0
 
+#define proxy_unmarshal(in_sg, in_elem, offset, fmt, args...) \
+    v9fs_unmarshal(in_sg, in_elem, offset, 0 /* convert */, fmt, ##args)
+#define proxy_marshal(out_sg, out_elem, offset, fmt, args...) \
+    v9fs_marshal(out_sg, out_elem, offset, 0 /* convert */, fmt, ##args)
+
+union MsgControl {
+    struct cmsghdr cmsg;
+    char control[CMSG_SPACE(sizeof(int))];
+};

This union isn't used in this patch.

Stefan

#5 lore raw

Re: [Qemu-devel] [PATCH V2 03/12] hw/9pfs: File system helper process for qemu 9p proxy FS

Quoted 30 lines · Stefan Hajnoczi
Quoted 28 lines · M. Mohan Kumar
fsdev/virtfs-proxy-helper.c new file +22 -0
new file mode 100644
index 0000000..69daf7c
@@ -0,0 +1,271 @@
+/*
+ * Helper for QEMU Proxy FS Driver
+ * Copyright IBM, Corp. 2011
+ *
+ * Authors:
+ * M. Mohan Kumar<[email protected]>
+ *
+ * This work is licensed under the terms of the GNU GPL, version 2. See
+ * the COPYING file in the top-level directory.
+ */
+#include<stdio.h>
+#include<sys/socket.h>
+#include<string.h>
+#include<sys/un.h>
+#include<limits.h>
+#include<signal.h>
+#include<errno.h>
+#include<stdlib.h>
+#include<sys/resource.h>
+#include<sys/stat.h>
+#include<getopt.h>
+#include<unistd.h>
Quoted 9 lines

fsdev/virtfs-proxy-helper.c line 28

Quoted 7 lines
fsdev/virtfs-proxy-helper.c new file +6 -0
+#include<syslog.h>
+#include<sys/prctl.h>
+#include<sys/capability.h>
+#include<sys/fsuid.h>
+#include<stdarg.h>
+#include "bswap.h"

Where is "bswap.h" used and why above<sys/socket.h>?

I will fix it

Quoted 18 lines
Quoted 6 lines
fsdev/virtfs-proxy-helper.c new file +4 -0
+#include<sys/socket.h>
+#include "qemu-common.h"
+#include "virtio-9p-marshal.h"
+#include "hw/9pfs/virtio-9p-proxy.h"

fsdev/virtfs-proxy-helper.c line 42

Quoted 10 lines
fsdev/virtfs-proxy-helper.c new file +10 -0
+
+#define PROGNAME "virtfs-proxy-helper"
+
+static struct option helper_opts[] = {
+    {"fd", required_argument, NULL, 'f'},
+    {"path", required_argument, NULL, 'p'},
+    {"nodaemon", no_argument, NULL, 'n'},
+};
+
+int is_daemon;

static?

Quoted 5 lines

Also, please use the bool type from<stdbool.h>, it makes it easier for readers who don't have to guess how the variable works (might be a bitfield or reference count too).

I will fix it.

Quoted 4 lines
Quoted 4 lines
fsdev/virtfs-proxy-helper.c new file +4 -0
+static int socket_read(int sockfd, void *buff, ssize_t size)
+{
+    int retval;
+
Quoted 10 lines

fsdev/virtfs-proxy-helper.c line 128

Quoted 7 lines
fsdev/virtfs-proxy-helper.c new file +6 -0
+    do {
+        retval = read(sockfd, buff, size);
+    } while (retval<  0&&  errno == EINTR);
+    if (retval != size) {
+        return -EIO;
+    }

Shouldn't this loop until size bytes have been read?

Ok, I will fix this.

Quoted 6 lines
Quoted 6 lines
fsdev/virtfs-proxy-helper.c new file +6 -0
+    return retval;
+}
+
+static int socket_write(int sockfd, void *buff, ssize_t size)
+{
+    int retval;
Quoted 10 lines

fsdev/virtfs-proxy-helper.c line 140

Quoted 7 lines
fsdev/virtfs-proxy-helper.c new file +6 -0
+
+    do {
+        retval = write(sockfd, buff, size);
+    } while (retval<  0&&  errno == EINTR);
+    if (retval != size) {
+        return -EIO;

We could pass the actual -errno here if retval< 0.

Socket errors are treated fatal and the reason for failures are not used by the code. When ever there is socket error, helper exits.

Quoted 17 lines
Quoted 17 lines
fsdev/virtfs-proxy-helper.c new file +17 -0
+    }
+    return retval;
+}
+
+static int read_request(int sockfd, struct iovec *iovec)
+{
+    int retval;
+    ProxyHeader header;
+
+    /* read the header */
+    retval = socket_read(sockfd, iovec->iov_base, sizeof(header));
+    if (retval != sizeof(header)) {
+        return -EIO;
+    }
+    /* unmarshal header */
+    proxy_unmarshal(iovec, 1, 0, "dd",&header.type,&header.size);
+    /* read the request */
Quoted 9 lines

fsdev/virtfs-proxy-helper.c line 163

Quoted 7 lines
fsdev/virtfs-proxy-helper.c new file +6 -0
+    retval = socket_read(sockfd, iovec->iov_base + sizeof(header), header.size);
+    if (retval != header.size) {
+        return -EIO;
+    }
+    return header.type;
+}

Size checks are missing and we're trusting what the client sends!

Could you please elaborate?

Quoted 53 lines
Quoted 41 lines
fsdev/virtfs-proxy-helper.c new file +39 -0
+
+static void usage(char *prog)
+{
+    fprintf(stderr, "usage: %s\n"
+            " -p|--path<path>  9p path to export\n"
+            " {-f|--fd<socket-descriptor>} socket file descriptor to be used\n"
+            " [-n|--nodaemon] Run as a normal program\n",
+            basename(prog));
+}
+
+static int process_requests(int sock)
+{
+    int type;
+    struct iovec iovec;
+
+    iovec.iov_base = g_malloc(BUFF_SZ);
+    iovec.iov_len = BUFF_SZ;
+    while (1) {
+        type = read_request(sock,&iovec);
+        if (type<= 0) {
+            goto error;
+        }
+    }
+    (void)socket_write;
+error:
+    g_free(iovec.iov_base);
+    return -1;
+}
+
+int main(int argc, char **argv)
+{
+    int sock;
+    char rpath[PATH_MAX];
+    struct stat stbuf;
+    int c, option_index;
+
+    is_daemon = 1;
+    rpath[0] = '\0';
+    sock = -1;

fsdev/virtfs-proxy-helper.c line 212

Quoted 10 lines
fsdev/virtfs-proxy-helper.c new file +10 -0
+    while (1) {
+        option_index = 0;
+        c = getopt_long(argc, argv, "p:nh?f:", helper_opts,
+&option_index);
+        if (c == -1) {
+            break;
+        }
+        switch (c) {
+        case 'p':
+            strcpy(rpath, optarg);

Buffer overflow. The whole thing would be simpler like this:

Quoted 7 lines

const char *rpath = "";
[...]

     case 'p':
         rpath = optarg;
         break;

I will fix it

Quoted 13 lines

fsdev/virtfs-proxy-helper.c line 224

Quoted 13 lines
fsdev/virtfs-proxy-helper.c new file +12 -0
+            break;
+        case 'n':
+            is_daemon = 0;
+            break;
+        case 'f':
+            sock = atoi(optarg);
+            break;
+        case '?':
+        case 'h':
+        default:
+            usage(argv[0]);
+            return -1;
Quoted 3 lines

The convention is for programs to exit with 1 (EXIT_FAILURE) on error.

I will fix it.

Quoted 7 lines
Quoted 7 lines
fsdev/virtfs-proxy-helper.c new file +7 -0
+            break;
+        }
+    }
+
+    /* Parameter validation */
+    if (sock == -1 || rpath[0] == '\0') {
+        fprintf(stderr, "socket descriptor or path not specified\n");
Quoted 9 lines

fsdev/virtfs-proxy-helper.c line 237

Quoted 7 lines
fsdev/virtfs-proxy-helper.c new file +6 -0
+        usage(argv[0]);
+        return -1;
+    }
+
+    if (lstat(rpath,&stbuf)<  0) {
+        fprintf(stderr, "invalid path \"%s\" specified?\n", rpath);

sterror() would provide further details on what went wrong.

Ok

Quoted 43 lines
Quoted 43 lines
fsdev/virtfs-proxy-helper.c new file +34 -0
+        return -1;
+    }
+
+    if (!S_ISDIR(stbuf.st_mode)) {
+        fprintf(stderr, "specified path \"%s\" is not directory\n", rpath);
+        return -1;
+    }
+
+    if (is_daemon) {
+        if (daemon(0, 0)<  0) {
+            fprintf(stderr, "daemon call failed\n");
+            return -1;
+        }
+        openlog(PROGNAME, LOG_PID, LOG_DAEMON);
+    }
+
+    do_log(LOG_INFO, "Started");
+
+    if (chroot(rpath)<  0) {
+        do_perror("chroot");
+        goto error;
+    }
+    umask(0);
+
+    if (init_capabilities()<  0) {
+        goto error;
+    }
+
+    process_requests(sock);
+error:
+    do_log(LOG_INFO, "Done");
+    closelog();
+    return 0;
+}
hw/9pfs/virtio-9p-proxy.h +0 -0
index f5e1a02..120e940 100644
@@ -3,6 +3,16 @@
 
  #define BUFF_SZ (4 * 1024)
 
Quoted 13 lines

hw/9pfs/virtio-9p-proxy.h

Quoted 10 lines
hw/9pfs/virtio-9p-proxy.h +9 -0
+#define proxy_unmarshal(in_sg, in_elem, offset, fmt, args...) \
+    v9fs_unmarshal(in_sg, in_elem, offset, 0 /* convert */, fmt, ##args)
+#define proxy_marshal(out_sg, out_elem, offset, fmt, args...) \
+    v9fs_marshal(out_sg, out_elem, offset, 0 /* convert */, fmt, ##args)
+
+union MsgControl {
+    struct cmsghdr cmsg;
+    char control[CMSG_SPACE(sizeof(int))];
+};
 

This union isn't used in this patch.

I will fix it.

#6 lore raw

Re: [Qemu-devel] [PATCH V2 03/12] hw/9pfs: File system helper process for qemu 9p proxy FS

Quoted 16 lines · M. Mohan Kumar
Quoted 4 lines · Stefan Hajnoczi
Quoted 4 lines
fsdev/virtfs-proxy-helper.c new file +4 -0
+static int socket_write(int sockfd, void *buff, ssize_t size)
+{
+    int retval;
+
Quoted 10 lines
Quoted 6 lines

+    do {
+        retval = write(sockfd, buff, size);
+    } while (retval<  0&&  errno == EINTR);
+    if (retval != size) {
+        return -EIO;

We could pass the actual -errno here if retval<  0.

Quoted 2 lines

Socket errors are treated fatal and the reason for failures are not used by the code. When ever there is socket error, helper exits.

Exiting may be fine but errno or strerror(errno) needs to be printed on exit. Otherwise we need to strace it to figure out what happened - and the error exit may be hard to reproduce, so this is not attractive. Therefore it makes sense to return the full -errno and print it out when exiting.

Quoted 25 lines
Quoted 15 lines
Quoted 15 lines
fsdev/virtfs-proxy-helper.c new file +14 -0
+static int read_request(int sockfd, struct iovec *iovec)
+{
+    int retval;
+    ProxyHeader header;
+
+    /* read the header */
+    retval = socket_read(sockfd, iovec->iov_base, sizeof(header));
+    if (retval != sizeof(header)) {
+        return -EIO;
+    }
+    /* unmarshal header */
+    proxy_unmarshal(iovec, 1, 0, "dd",&header.type,&header.size);
+    /* read the request */
+    retval = socket_read(sockfd, iovec->iov_base + sizeof(header),

header.size);

Quoted 9 lines
Quoted 6 lines

+    if (retval != header.size) {
+        return -EIO;
+    }
+    return header.type;
+}

Size checks are missing and we're trusting what the client sends!

Quoted 1 line

Could you please elaborate?

This function allows the client to overflow the iovec buffer. Check what happens when header.size > 4096.

I'm alarmed by this because there are 9 more patches to review and this is just the foundation of the helper. If the helper isn't secure then isolating it from QEMU is useless since a malicious QEMU can take over the helper. Input needs to be validated.

Stefan

#7 lore raw

PATCH v2 4/12 hw/9pfs: Open and create files

In reply to M. Mohan Kumar

Add interfaces to open and create files for proxy file system driver.

---
 fsdev/virtfs-proxy-helper.c |  136 +++++++++++++++++++++++++++++++-
 hw/9pfs/virtio-9p-proxy.c   |  180 +++++++++++++++++++++++++++++++++++++++++--
 hw/9pfs/virtio-9p-proxy.h   |    9 ++
 3 files changed, 314 insertions(+), 11 deletions(-)
fsdev/virtfs-proxy-helper.c +131 -5
index 69daf7c..68d27f1 100644
@@ -30,6 +30,8 @@
 #include "qemu-common.h"
 #include "virtio-9p-marshal.h"
 #include "hw/9pfs/virtio-9p-proxy.h"
+#include "fsdev/virtio-9p-marshal.h"
+
 
 #define PROGNAME "virtfs-proxy-helper"
 
@@ -148,20 +150,125 @@ static int read_request(int sockfd, struct iovec *iovec)
     ProxyHeader header;
 
     /* read the header */
-    retval = socket_read(sockfd, iovec->iov_base, sizeof(header));
-    if (retval != sizeof(header)) {
+    retval = socket_read(sockfd, iovec->iov_base, HDR_SZ);
+    if (retval != HDR_SZ) {
         return -EIO;
     }
     /* unmarshal header */
     proxy_unmarshal(iovec, 1, 0, "dd", &header.type, &header.size);
     /* read the request */
-    retval = socket_read(sockfd, iovec->iov_base + sizeof(header), header.size);
+    retval = socket_read(sockfd, iovec->iov_base + HDR_SZ, header.size);
     if (retval != header.size) {
         return -EIO;
     }
     return header.type;
 }
 
+static void send_fd(int sockfd, int fd)
+{
+    struct msghdr msg = { };
+    struct iovec iov;
+    struct cmsghdr *cmsg;
+    int retval, data;
+    union MsgControl msg_control;
+
+    iov.iov_base = &data;
+    iov.iov_len = sizeof(data);
+
+    memset(&msg, 0, sizeof(msg));
+    msg.msg_iov = &iov;
+    msg.msg_iovlen = 1;
+    /* No ancillary data on error */
+    if (fd < 0) {
+        /*
+         * fd is really negative errno if the request failed. Or simply
+         * zero if the request is successful and it doesn't need a file
+         * descriptor.
+         */
+        data = fd;
+    } else {
+        data = V9FS_FD_VALID;
+        msg.msg_control = &msg_control;
+        msg.msg_controllen = sizeof(msg_control);
+
+        cmsg = &msg_control.cmsg;
+        cmsg->cmsg_len = CMSG_LEN(sizeof(fd));
+        cmsg->cmsg_level = SOL_SOCKET;
+        cmsg->cmsg_type = SCM_RIGHTS;
+        memcpy(CMSG_DATA(cmsg), &fd, sizeof(fd));
+    }
+
+    do {
+        retval = sendmsg(sockfd, &msg, 0);
+    } while (retval < 0 && errno == EINTR);
+    if (retval < 0) {
+        do_perror("sendmsg");
+        exit(1);
+    }
+    if (fd >= 0) {
+        close(fd);
+    }
+}
+
+/*
+ * from man 7 capabilities, section
+ * Effect of User ID Changes on Capabilities:
+ * 4. If the file system user ID is changed from 0 to nonzero (see setfsuid(2))
+ * then the following capabilities are cleared from the effective set:
+ * CAP_CHOWN, CAP_DAC_OVERRIDE, CAP_DAC_READ_SEARCH,  CAP_FOWNER, CAP_FSETID,
+ * CAP_LINUX_IMMUTABLE  (since  Linux 2.2.30), CAP_MAC_OVERRIDE, and CAP_MKNOD
+ * (since Linux 2.2.30). If the file system UID is changed from nonzero to 0,
+ * then any of these capabilities that are enabled in the permitted set
+ * are enabled in the effective set.
+ */
+static int setfsugid(int uid, int gid)
+{
+    setfsgid(gid);
+    setfsuid(uid);
+    return cap_set();
+}
+
+/*
+ * create a file and send fd on success
+ * return -errno on error
+ */
+static int do_create(struct iovec *iovec)
+{
+    V9fsString path;
+    int flags, fd, mode, uid, gid, cur_uid, cur_gid;
+    proxy_unmarshal(iovec, 1, HDR_SZ, "sdddd",
+                   &path, &flags, &mode, &uid, &gid);
+    cur_uid = geteuid();
+    cur_gid = getegid();
+    if (setfsugid(uid, gid) < 0) {
+        return -EPERM;
+    }
+    fd = open(path.data, flags, mode);
+    if (fd < 0) {
+        fd = -errno;
+    }
+    v9fs_string_free(&path);
+    setfsugid(cur_uid, cur_gid);
+    return fd;
+}
+
+/*
+ * open a file and send fd on success
+ * return -errno on error
+ */
+static int do_open(struct iovec *iovec)
+{
+    V9fsString path;
+    int flags, fd;
+    proxy_unmarshal(iovec, 1, HDR_SZ, "sd", &path, &flags);
+    fd = open(path.data, flags);
+    if (fd < 0) {
+        fd = -errno;
+    }
+    v9fs_string_free(&path);
+    return fd;
+}
+
 static void usage(char *prog)
 {
     fprintf(stderr, "usage: %s\n"
@@ -173,15 +280,34 @@ static void usage(char *prog)
 
 static int process_requests(int sock)
 {
-    int type;
+    int type, retval = 0;
     struct iovec iovec;
 
     iovec.iov_base = g_malloc(BUFF_SZ);
     iovec.iov_len = BUFF_SZ;
     while (1) {
         type = read_request(sock, &iovec);
-        if (type <= 0) {
+        switch (type) {
+        case T_OPEN:
+            retval = do_open(&iovec);
+            break;
+        case T_CREATE:
+            retval = do_create(&iovec);
+            break;
+        default:
+            goto error;
+            break;
+        }
+
+        /* Send response */
+        switch (type) {
+        case T_OPEN:
+        case T_CREATE:
+            send_fd(sock, retval);
+            break;
+        default:
             goto error;
+            break;
         }
     }
     (void)socket_write;
hw/9pfs/virtio-9p-proxy.c +174 -6

205 lines not on this page · raw message

hw/9pfs/virtio-9p-proxy.h +9 -0
index 120e940..f30e367 100644
@@ -2,6 +2,7 @@
 #define _QEMU_VIRTIO_9P_PROXY_H
 
 #define BUFF_SZ (4 * 1024)
+#define V9FS_FD_VALID INT_MAX
 
 #define proxy_unmarshal(in_sg, in_elem, offset, fmt, args...) \
     v9fs_unmarshal(in_sg, in_elem, offset, 0 /* convert */, fmt, ##args)
@@ -17,4 +18,12 @@ typedef struct {
     int type;
     int size;
 } ProxyHeader;
+
+#define HDR_SZ (sizeof(ProxyHeader))
+
+enum {
+    T_OPEN = 1,
+    T_CREATE,
+};
+
 #endif
Signature · 1 line
1.7.6
#8 lore raw

Re: [Qemu-devel] [PATCH V2 04/12] hw/9pfs: Open and create files

Quoted 11 lines · M. Mohan Kumar
fsdev/virtfs-proxy-helper.c +11 -0
+static void send_fd(int sockfd, int fd)
+{
+    struct msghdr msg = { };
+    struct iovec iov;
+    struct cmsghdr *cmsg;
+    int retval, data;
+    union MsgControl msg_control;
+
+    iov.iov_base = &data;
+    iov.iov_len = sizeof(data);
+

fsdev/virtfs-proxy-helper.c static int read_request(int sockfd, struct iovec *iovec) · line 187

Quoted 10 lines
fsdev/virtfs-proxy-helper.c +10 -0
+    memset(&msg, 0, sizeof(msg));
+    msg.msg_iov = &iov;
+    msg.msg_iovlen = 1;
+    /* No ancillary data on error */
+    if (fd < 0) {
+        /*
+         * fd is really negative errno if the request failed. Or simply
+         * zero if the request is successful and it doesn't need a file
+         * descriptor.
+         */

It cannot be zero because the if statement is fd < 0. The comment is confusing.

fsdev/virtfs-proxy-helper.c static int read_request(int sockfd, struct iovec *iovec) · line 240

Quoted 10 lines
fsdev/virtfs-proxy-helper.c +10 -0
+/*
+ * create a file and send fd on success
+ * return -errno on error
+ */
+static int do_create(struct iovec *iovec)
+{
+    V9fsString path;
+    int flags, fd, mode, uid, gid, cur_uid, cur_gid;
+    proxy_unmarshal(iovec, 1, HDR_SZ, "sdddd",
+                   &path, &flags, &mode, &uid, &gid);

Unmarshalling can fail if the iovec size does not match what the format string describes. We should fail here rather than continuing on. If execution continues some of the variables may be uninitialized.

Stefan

#9 lore raw

PATCH v2 5/12 hw/9pfs: Create other filesystem objects

In reply to M. Mohan Kumar

Add interfaces to create filesystem objects like directory, device nodes, symbolic links, links for proxy filesytem driver

---
 fsdev/virtfs-proxy-helper.c |  105 ++++++++++++++++++++++++--
 hw/9pfs/virtio-9p-proxy.c   |  173 +++++++++++++++++++++++++++++++++++++++----
 hw/9pfs/virtio-9p-proxy.h   |    8 ++-
 3 files changed, 261 insertions(+), 25 deletions(-)
fsdev/virtfs-proxy-helper.c +97 -8
index 68d27f1..377e91a 100644
@@ -210,6 +210,28 @@ static void send_fd(int sockfd, int fd)
     }
 }
 
+static int send_status(int sockfd, struct iovec *iovec, int status)
+{
+    int retval, msg_size;;
+    ProxyHeader header;
+
+    if (status < 0) {
+        header.type = T_ERROR;
+    } else {
+        header.type = T_SUCCESS;
+    }
+    header.size = sizeof(status);
+
+    /* marshal the return status */
+    msg_size = proxy_marshal(iovec, 1, 0, "ddd", header.type, header.size,
+                    status);
+    retval = socket_write(sockfd, iovec->iov_base, msg_size);
+    if (retval != msg_size) {
+        return -EIO;
+    }
+    return 0;
+}
+
 /*
  * from man 7 capabilities, section
  * Effect of User ID Changes on Capabilities:
@@ -229,6 +251,49 @@ static int setfsugid(int uid, int gid)
 }
 
 /*
+ * create a other filesystem objects and send 0 on success
+ * return -errno on error
+ */
+static int do_create_others(int type, struct iovec *iovec)
+{
+    dev_t rdev;
+    int retval = 0;
+    V9fsString oldpath, path;
+    int mode, uid, gid, cur_uid, cur_gid;
+    int offset = HDR_SZ;
+
+    cur_uid = geteuid();
+    cur_gid = getegid();
+
+    offset += proxy_unmarshal(iovec, 1, offset, "dd", &uid, &gid);
+    if (setfsugid(uid, gid) < 0) {
+        return -EPERM;
+    }
+    switch (type) {
+    case T_MKNOD:
+        proxy_unmarshal(iovec, 1, offset, "sdq", &path, &mode, &rdev);
+        retval = mknod(path.data, mode, rdev);
+        break;
+    case T_MKDIR:
+        proxy_unmarshal(iovec, 1, offset, "sd", &path, &mode);
+        retval = mkdir(path.data, mode);
+        break;
+    case T_SYMLINK:
+        proxy_unmarshal(iovec, 1, offset, "ss", &oldpath, &path);
+        retval = symlink(oldpath.data, path.data);
+        v9fs_string_free(&oldpath);
+        break;
+    }
+
+    if (retval < 0) {
+        retval = -errno;
+    }
+    v9fs_string_free(&path);
+    setfsugid(cur_uid, cur_gid);
+    return retval;
+}
+
+/*
  * create a file and send fd on success
  * return -errno on error
  */
@@ -281,18 +346,36 @@ static void usage(char *prog)
 static int process_requests(int sock)
 {
     int type, retval = 0;
-    struct iovec iovec;
+    V9fsString oldpath, path;
+    struct iovec in_iovec, out_iovec;
+
+    in_iovec.iov_base = g_malloc(BUFF_SZ);
+    in_iovec.iov_len = BUFF_SZ;
+    out_iovec.iov_base = g_malloc(BUFF_SZ);
+    out_iovec.iov_len = BUFF_SZ;
 
-    iovec.iov_base = g_malloc(BUFF_SZ);
-    iovec.iov_len = BUFF_SZ;
     while (1) {
-        type = read_request(sock, &iovec);
+        type = read_request(sock, &in_iovec);
         switch (type) {
         case T_OPEN:
-            retval = do_open(&iovec);
+            retval = do_open(&in_iovec);
             break;
         case T_CREATE:
-            retval = do_create(&iovec);
+            retval = do_create(&in_iovec);
+            break;
+        case T_MKNOD:
+        case T_MKDIR:
+        case T_SYMLINK:
+            retval = do_create_others(type, &in_iovec);
+            break;
+        case T_LINK:
+            proxy_unmarshal(&in_iovec, 1, HDR_SZ, "ss", &oldpath, &path);
+            retval = link(oldpath.data, path.data);
+            if (retval < 0) {
+                retval = -errno;
+            }
+            v9fs_string_free(&oldpath);
+            v9fs_string_free(&path);
             break;
         default:
             goto error;
@@ -305,14 +388,20 @@ static int process_requests(int sock)
         case T_CREATE:
             send_fd(sock, retval);
             break;
+        case T_MKNOD:
+        case T_MKDIR:
+        case T_SYMLINK:
+        case T_LINK:
+            send_status(sock, &out_iovec, retval);
+            break;
         default:
             goto error;
             break;
         }
     }
-    (void)socket_write;
 error:
-    g_free(iovec.iov_base);
+    g_free(in_iovec.iov_base);
+    g_free(out_iovec.iov_base);
     return -1;
 }
 
hw/9pfs/virtio-9p-proxy.c +157 -16

241 lines not on this page · raw message

hw/9pfs/virtio-9p-proxy.h +7 -1
index f30e367..32610e0 100644
@@ -22,8 +22,14 @@ typedef struct {
 #define HDR_SZ (sizeof(ProxyHeader))
 
 enum {
-    T_OPEN = 1,
+    T_SUCCESS = 0,
+    T_ERROR,
+    T_OPEN,
     T_CREATE,
+    T_MKNOD,
+    T_MKDIR,
+    T_SYMLINK,
+    T_LINK,
 };
 
 #endif
Signature · 1 line
1.7.6
#10 lore raw

PATCH v2 6/12 hw/9pfs: Add stat/readlink/statfs for proxy FS

In reply to M. Mohan Kumar
---
 fsdev/virtfs-proxy-helper.c |  165 ++++++++++++++++++++++++++++++++++++++++
 hw/9pfs/virtio-9p-proxy.c   |  174 +++++++++++++++++++++++++++++++++++++++++--
 hw/9pfs/virtio-9p-proxy.h   |   34 +++++++++
 3 files changed, 367 insertions(+), 6 deletions(-)
fsdev/virtfs-proxy-helper.c +165 -0
index 377e91a..eb33504 100644
@@ -27,6 +27,8 @@
 #include <stdarg.h>
 #include "bswap.h"
 #include <sys/socket.h>
+#include <sys/vfs.h>
+#include <sys/stat.h>
 #include "qemu-common.h"
 #include "virtio-9p-marshal.h"
 #include "hw/9pfs/virtio-9p-proxy.h"
@@ -251,6 +253,154 @@ static int setfsugid(int uid, int gid)
 }
 
 /*
+ * send response in two parts
+ * 1) ProxyHeader
+ * 2) Response or error status
+ * This function should be called with marshaling response
+ * send_response constructs header part and error part only.
+ * send response sends {ProxyHeader,Response} if the request was success
+ * otherwise sends {ProxyHeader,error status}
+ */
+static int send_response(int sock, struct iovec *iovec, int size)
+{
+    int retval;
+    ProxyHeader header;
+
+    if (size < 0) {
+        header.type = T_ERROR;
+        header.size = sizeof(size);
+        proxy_marshal(iovec, 1, HDR_SZ, "d", size);
+    } else {
+        header.type = T_SUCCESS;
+        header.size = size;
+    }
+
+    proxy_marshal(iovec, 1, 0, "dd", header.type, header.size);
+    retval = socket_write(sock, iovec->iov_base, header.size + HDR_SZ);
+    if (retval != header.size + HDR_SZ) {
+        return -EIO;
+    }
+    return 0;
+}
+
+static void stat_to_prstat(ProxyStat *pr_stat, struct stat *stat)
+{
+    memset(pr_stat, 0, sizeof(*pr_stat));
+    pr_stat->st_dev = stat->st_dev;
+    pr_stat->st_ino = stat->st_ino;
+    pr_stat->st_nlink = stat->st_nlink;
+    pr_stat->st_mode = stat->st_mode;
+    pr_stat->st_uid = stat->st_uid;
+    pr_stat->st_gid = stat->st_gid;
+    pr_stat->st_rdev = stat->st_rdev;
+    pr_stat->st_size = stat->st_size;
+    pr_stat->st_blksize = stat->st_blksize;
+    pr_stat->st_blocks = stat->st_blocks;
+    pr_stat->st_atim_sec = stat->st_atim.tv_sec;
+    pr_stat->st_atim_nsec = stat->st_atim.tv_nsec;
+    pr_stat->st_mtim_sec = stat->st_mtim.tv_sec;
+    pr_stat->st_mtim_nsec = stat->st_mtim.tv_nsec;
+    pr_stat->st_ctim_sec = stat->st_ctim.tv_sec;
+    pr_stat->st_ctim_nsec = stat->st_ctim.tv_nsec;
+}
+
+static void statfs_to_prstatfs(ProxyStatFS *pr_stfs, struct statfs *stfs)
+{
+    memset(pr_stfs, 0, sizeof(*pr_stfs));
+    pr_stfs->f_type = stfs->f_type;
+    pr_stfs->f_bsize = stfs->f_bsize;
+    pr_stfs->f_blocks = stfs->f_blocks;
+    pr_stfs->f_bfree = stfs->f_bfree;
+    pr_stfs->f_bavail = stfs->f_bavail;
+    pr_stfs->f_files = stfs->f_files;
+    pr_stfs->f_ffree = stfs->f_ffree;
+    pr_stfs->f_fsid[0] = stfs->f_fsid.__val[0];
+    pr_stfs->f_fsid[1] = stfs->f_fsid.__val[1];
+    pr_stfs->f_namelen = stfs->f_namelen;
+    pr_stfs->f_frsize = stfs->f_frsize;
+}
+
+/*
+ * Gets stat/statfs information and packs in out_iovec structure
+ * on success returns number of bytes packed in out_iovec struture
+ * otherwise returns -errno
+ */
+static int do_stat(int type, struct iovec *iovec, struct iovec *out_iovec)
+{
+    V9fsString path;
+    int retval = 0;
+
+    proxy_unmarshal(iovec, 1, HDR_SZ, "s", &path);
+
+    switch (type) {
+    case T_LSTAT: {
+        struct stat st_buf;
+        ProxyStat pr_stat;
+
+        retval = lstat(path.data, &st_buf);
+        if (retval < 0) {
+            retval = -errno;
+        } else {
+            stat_to_prstat(&pr_stat, &st_buf);
+            retval = proxy_marshal(out_iovec, 1, HDR_SZ,
+                            "qqqdddqqqqqqqqqq", pr_stat.st_dev,
+                            pr_stat.st_ino, pr_stat.st_nlink,
+                            pr_stat.st_mode, pr_stat.st_uid,
+                            pr_stat.st_gid, pr_stat.st_rdev,
+                            pr_stat.st_size, pr_stat.st_blksize,
+                            pr_stat.st_blocks,
+                            pr_stat.st_atim_sec, pr_stat.st_atim_nsec,
+                            pr_stat.st_mtim_sec, pr_stat.st_mtim_nsec,
+                            pr_stat.st_ctim_sec, pr_stat.st_ctim_nsec);
+        }
+        break;
+    }
+    case T_STATFS: {
+        struct statfs stfs_buf;
+        ProxyStatFS pr_stfs;
+
+        retval = statfs(path.data, &stfs_buf);
+        if (retval < 0) {
+            retval = -errno;
+        } else {
+            statfs_to_prstatfs(&pr_stfs, &stfs_buf);
+            retval = proxy_marshal(out_iovec, 1, HDR_SZ,
+                            "qqqqqqqqqqq", pr_stfs.f_type, pr_stfs.f_bsize,
+                            pr_stfs.f_blocks, pr_stfs.f_bfree, pr_stfs.f_bavail,
+                            pr_stfs.f_files, pr_stfs.f_ffree,
+                            pr_stfs.f_fsid[0], pr_stfs.f_fsid[1],
+                            pr_stfs.f_namelen, pr_stfs.f_frsize);
+        }
+        break;
+    }
+    }
+    v9fs_string_free(&path);
+    return retval;
+}
+
+static int do_readlink(struct iovec *iovec, struct iovec *out_iovec)
+{
+    V9fsString target, path;
+    int size, retval;
+    char *buffer;
+
+    proxy_unmarshal(iovec, 1, HDR_SZ, "sd", &path, &size);
+    buffer = g_malloc(size);
+    v9fs_string_init(&target);
+    retval = readlink(path.data, buffer, size);
+    if (retval > 0) {
+	    buffer[retval] = '\0';
+	    v9fs_string_sprintf(&target, "%s", buffer);
+        retval = proxy_marshal(out_iovec, 1, HDR_SZ, "s", &target);
+    } else {
+        retval = -errno;
+    }
+    v9fs_string_free(&target);
+    v9fs_string_free(&path);
+    return retval;
+}
+
+/*
  * create a other filesystem objects and send 0 on success
  * return -errno on error
  */
@@ -348,6 +498,7 @@ static int process_requests(int sock)
     int type, retval = 0;
     V9fsString oldpath, path;
     struct iovec in_iovec, out_iovec;
+    int size = 0;
 
     in_iovec.iov_base = g_malloc(BUFF_SZ);
     in_iovec.iov_len = BUFF_SZ;
@@ -377,6 +528,13 @@ static int process_requests(int sock)
             v9fs_string_free(&oldpath);
             v9fs_string_free(&path);
             break;
+        case T_LSTAT:
+        case T_STATFS:
+            size = do_stat(type, &in_iovec, &out_iovec);
+            break;
+        case T_READLINK:
+            size = do_readlink(&in_iovec, &out_iovec);
+            break;
         default:
             goto error;
             break;
@@ -394,6 +552,13 @@ static int process_requests(int sock)
         case T_LINK:
             send_status(sock, &out_iovec, retval);
             break;
+        case T_LSTAT:
+        case T_STATFS:
+        case T_READLINK:
+            if (send_response(sock, &out_iovec, size) < 0) {
+                goto error;
+            }
+            break;
         default:
             goto error;
             break;
hw/9pfs/virtio-9p-proxy.c +168 -6

215 lines not on this page · raw message

hw/9pfs/virtio-9p-proxy.h +34 -0
index 32610e0..578ca19 100644
@@ -30,6 +30,40 @@ enum {
     T_MKDIR,
     T_SYMLINK,
     T_LINK,
+    T_LSTAT,
+    T_READLINK,
+    T_STATFS,
 };
 
+typedef struct {
+    uint64_t st_dev;
+    uint64_t st_ino;
+    uint64_t st_nlink;
+    uint32_t st_mode;
+    uint32_t st_uid;
+    uint32_t st_gid;
+    uint64_t st_rdev;
+    uint64_t st_size;
+    uint64_t st_blksize;
+    uint64_t st_blocks;
+    uint64_t st_atim_sec;
+    uint64_t st_atim_nsec;
+    uint64_t st_mtim_sec;
+    uint64_t st_mtim_nsec;
+    uint64_t st_ctim_sec;
+    uint64_t st_ctim_nsec;
+} ProxyStat;
+
+typedef struct {
+    uint64_t f_type;
+    uint64_t f_bsize;
+    uint64_t f_blocks;
+    uint64_t f_bfree;
+    uint64_t f_bavail;
+    uint64_t f_files;
+    uint64_t f_ffree;
+    uint64_t f_fsid[2];
+    uint64_t f_namelen;
+    uint64_t f_frsize;
+} ProxyStatFS;
 #endif
Signature · 1 line
1.7.6
#11 lore raw

PATCH v2 7/12 hw/9pfs: File ownership and others

In reply to M. Mohan Kumar

Add file ownership interfaces like chmod/chown, utime update, rename, remove and truncating files for proxy FS

---
 Makefile                    |    2 +-
 fsdev/virtfs-proxy-helper.c |   66 +++++++++++++++++++++
 hw/9pfs/virtio-9p-proxy.c   |  134 +++++++++++++++++++++++++++++++++++++++----
 hw/9pfs/virtio-9p-proxy.h   |    6 ++
 4 files changed, 195 insertions(+), 13 deletions(-)
Makefile +1 -1
index 19b481a..378ee4d 100644
@@ -153,7 +153,7 @@ qemu-img$(EXESUF): qemu-img.o $(tools-obj-y) $(block-obj-y)
 qemu-nbd$(EXESUF): qemu-nbd.o $(tools-obj-y) $(block-obj-y)
 qemu-io$(EXESUF): qemu-io.o cmd.o $(tools-obj-y) $(block-obj-y)
 
-fsdev/virtfs-proxy-helper$(EXESUF): fsdev/virtfs-proxy-helper.o fsdev/virtio-9p-marshal.o
+fsdev/virtfs-proxy-helper$(EXESUF): fsdev/virtfs-proxy-helper.o fsdev/virtio-9p-marshal.o oslib-posix.o
 fsdev/virtfs-proxy-helper$(EXESUF): LIBS += -lcap
 
 qemu-img-cmds.h: $(SRC_PATH)/qemu-img-cmds.hx
fsdev/virtfs-proxy-helper.c +66 -0

84 lines not on this page · raw message

hw/9pfs/virtio-9p-proxy.c +122 -12

182 lines not on this page · raw message

hw/9pfs/virtio-9p-proxy.h +6 -0
index 578ca19..ef8a0f3 100644
@@ -33,6 +33,12 @@ enum {
     T_LSTAT,
     T_READLINK,
     T_STATFS,
+    T_CHMOD,
+    T_CHOWN,
+    T_TRUNCATE,
+    T_UTIME,
+    T_RENAME,
+    T_REMOVE,
 };
 
 typedef struct {
Signature · 1 line
1.7.6
#12 lore raw

PATCH v2 8/12 hw/9pfs: xattr interfaces in proxy filesystem driver

In reply to M. Mohan Kumar

Add xattr support for proxy FS

---
 fsdev/virtfs-proxy-helper.c |   78 ++++++++++++++++++++++++++++-
 hw/9pfs/virtio-9p-proxy.c   |  119 +++++++++++++++++++++++++++++++++++++++----
 hw/9pfs/virtio-9p-proxy.h   |    4 ++
 3 files changed, 190 insertions(+), 11 deletions(-)
fsdev/virtfs-proxy-helper.c +77 -1
index ded0ead..ccd7ed8 100644
@@ -29,6 +29,7 @@
 #include <sys/socket.h>
 #include <sys/vfs.h>
 #include <sys/stat.h>
+#include <attr/xattr.h>
 #include "qemu-common.h"
 #include "virtio-9p-marshal.h"
 #include "hw/9pfs/virtio-9p-proxy.h"
@@ -283,6 +284,50 @@ static int send_response(int sock, struct iovec *iovec, int size)
     return 0;
 }
 
+static int do_getxattr(int type, struct iovec *iovec, struct iovec *out_iovec)
+{
+    int size = 0, offset, retval;
+    V9fsString path, name, xattr;
+
+    v9fs_string_init(&xattr);
+
+    offset = HDR_SZ;
+    offset += proxy_unmarshal(iovec, 1, HDR_SZ, "ds", &size, &path);
+    if (size) {
+        xattr.data = g_malloc(size);
+        xattr.size = size;
+    }
+    switch (type) {
+    case T_LGETXATTR:
+        proxy_unmarshal(iovec, 1, offset, "s", &name);
+        retval = lgetxattr(path.data, name.data, xattr.data, size);
+        if (retval < 0) {
+            retval = -errno;
+            v9fs_string_free(&name);
+            goto error;
+        }
+    	v9fs_string_free(&name);
+        break;
+    case T_LLISTXATTR:
+        retval = llistxattr(path.data, xattr.data, size);
+        if (retval < 0) {
+            retval = -errno;
+            goto error;
+        }
+        break;
+    }
+
+    if (!size) {
+        proxy_marshal(out_iovec, 1, HDR_SZ, "d", retval);
+        retval = sizeof(retval);
+    } else {
+        retval = proxy_marshal(out_iovec, 1, HDR_SZ, "s", &xattr);
+    }
+error:
+    v9fs_string_free(&path);
+    return retval;
+}
+
 static void stat_to_prstat(ProxyStat *pr_stat, struct stat *stat)
 {
     memset(pr_stat, 0, sizeof(*pr_stat));
@@ -499,9 +544,10 @@ static int process_requests(int sock)
     int mode, uid, gid;
     struct timespec spec[2];
     int type, retval = 0;
+    V9fsString name, value;
     V9fsString oldpath, path;
     struct iovec in_iovec, out_iovec;
-    int size = 0;
+    int size = 0, flags;
 
     in_iovec.iov_base = g_malloc(BUFF_SZ);
     in_iovec.iov_len = BUFF_SZ;
@@ -595,6 +641,32 @@ static int process_requests(int sock)
             }
             v9fs_string_free(&path);
             break;
+        case T_LGETXATTR:
+        case T_LLISTXATTR:
+            size = do_getxattr(type, &in_iovec, &out_iovec);
+            break;
+        case T_LSETXATTR:
+            proxy_unmarshal(&in_iovec, 1, HDR_SZ,
+                           "sssdd", &path, &name, &value, &size,
+                           &flags);
+            retval = lsetxattr(path.data, name.data, value.data, size, flags);
+            if (retval < 0) {
+                retval = -errno;
+            }
+            v9fs_string_free(&path);
+            v9fs_string_free(&name);
+            v9fs_string_free(&value);
+            break;
+        case T_LREMOVEXATTR:
+            proxy_unmarshal(&in_iovec, 1,
+                           HDR_SZ, "ss", &path, &name);
+            retval = lremovexattr(path.data, name.data);
+            if (retval < 0) {
+                retval = -errno;
+            }
+            v9fs_string_free(&path);
+            v9fs_string_free(&name);
+            break;
         default:
             goto error;
             break;
@@ -616,11 +688,15 @@ static int process_requests(int sock)
         case T_UTIME:
         case T_RENAME:
         case T_REMOVE:
+        case T_LSETXATTR:
+        case T_LREMOVEXATTR:
             send_status(sock, &out_iovec, retval);
             break;
         case T_LSTAT:
         case T_STATFS:
         case T_READLINK:
+        case T_LGETXATTR:
+        case T_LLISTXATTR:
             if (send_response(sock, &out_iovec, size) < 0) {
                 goto error;
             }
hw/9pfs/virtio-9p-proxy.c +109 -10

176 lines not on this page · raw message

hw/9pfs/virtio-9p-proxy.h +4 -0
index ef8a0f3..4c691a8 100644
@@ -39,6 +39,10 @@ enum {
     T_UTIME,
     T_RENAME,
     T_REMOVE,
+    T_LGETXATTR,
+    T_LLISTXATTR,
+    T_LSETXATTR,
+    T_LREMOVEXATTR,
 };
 
 typedef struct {
Signature · 1 line
1.7.6
#13 lore raw

PATCH v2 9/12 hw/9pfs: Proxy getversion

In reply to M. Mohan Kumar

Add proxy getversion to get generation number

---
 fsdev/virtfs-proxy-helper.c |   74 +++++++++++++++++++++++++++++++++++++++++++
 hw/9pfs/virtio-9p-proxy.c   |   31 ++++++++++++++++++
 hw/9pfs/virtio-9p-proxy.h   |    1 +
 3 files changed, 106 insertions(+), 0 deletions(-)
fsdev/virtfs-proxy-helper.c +74 -0
index ccd7ed8..917bb26 100644
@@ -30,6 +30,11 @@
 #include <sys/vfs.h>
 #include <sys/stat.h>
 #include <attr/xattr.h>
+#include <sys/ioctl.h>
+#include <linux/fs.h>
+#ifdef CONFIG_LINUX_MAGIC_H
+#include <linux/magic.h>
+#endif
 #include "qemu-common.h"
 #include "virtio-9p-marshal.h"
 #include "hw/9pfs/virtio-9p-proxy.h"
@@ -38,6 +43,19 @@
 
 #define PROGNAME "virtfs-proxy-helper"
 
+#ifndef XFS_SUPER_MAGIC
+#define XFS_SUPER_MAGIC  0x58465342
+#endif
+#ifndef EXT2_SUPER_MAGIC
+#define EXT2_SUPER_MAGIC 0xEF53
+#endif
+#ifndef REISERFS_SUPER_MAGIC
+#define REISERFS_SUPER_MAGIC 0x52654973
+#endif
+#ifndef BTRFS_SUPER_MAGIC
+#define BTRFS_SUPER_MAGIC 0x9123683E
+#endif
+
 static struct option helper_opts[] = {
     {"fd", required_argument, NULL, 'f'},
     {"path", required_argument, NULL, 'p'},
@@ -45,6 +63,7 @@ static struct option helper_opts[] = {
 };
 
 int is_daemon;
+int get_version; /* IOC getversion IOCTL supported */
 
 static void do_perror(const char *string)
 {
@@ -284,6 +303,42 @@ static int send_response(int sock, struct iovec *iovec, int size)
     return 0;
 }
 
+/*
+ * gets generation number
+ * returns -errno on failure and sizeof(generation number) on success
+ */
+static int do_getversion(struct iovec *iovec, struct iovec *out_iovec)
+{
+    int fd, retval;
+    uint64_t version;
+    V9fsString path;
+
+    retval = sizeof(version);
+    /* no need to issue ioctl */
+    if (!get_version) {
+        version = 0;
+        proxy_marshal(out_iovec, 1, HDR_SZ, "q", version);
+        return retval;
+    }
+
+    proxy_unmarshal(iovec, 1, HDR_SZ, "s", &path);
+
+    fd = open(path.data, O_RDONLY);
+    if (fd < 0) {
+        retval = -errno;
+        goto done;
+    }
+    if (ioctl(fd, FS_IOC_GETVERSION, &version) < 0) {
+        retval = -errno;
+    } else {
+        proxy_marshal(out_iovec, 1, HDR_SZ, "q", version);
+    }
+    close(fd);
+done:
+    v9fs_string_free(&path);
+    return retval;
+}
+
 static int do_getxattr(int type, struct iovec *iovec, struct iovec *out_iovec)
 {
     int size = 0, offset, retval;
@@ -667,6 +722,9 @@ static int process_requests(int sock)
             v9fs_string_free(&path);
             v9fs_string_free(&name);
             break;
+        case T_GETVERSION:
+            size = do_getversion(&in_iovec, &out_iovec);
+            break;
         default:
             goto error;
             break;
@@ -697,6 +755,7 @@ static int process_requests(int sock)
         case T_READLINK:
         case T_LGETXATTR:
         case T_LLISTXATTR:
+        case T_GETVERSION:
             if (send_response(sock, &out_iovec, size) < 0) {
                 goto error;
             }
@@ -718,6 +777,8 @@ int main(int argc, char **argv)
     char rpath[PATH_MAX];
     struct stat stbuf;
     int c, option_index;
+    int retval;
+    struct statfs st_fs;
 
     is_daemon = 1;
     rpath[0] = '\0';
@@ -775,6 +836,19 @@ int main(int argc, char **argv)
 
     do_log(LOG_INFO, "Started");
 
+    /* check whether underlying FS support IOC_GETVERSION */
+    retval = statfs(rpath, &st_fs);
+    if (!retval) {
+        switch (st_fs.f_type) {
+        case EXT2_SUPER_MAGIC:
+        case BTRFS_SUPER_MAGIC:
+        case REISERFS_SUPER_MAGIC:
+        case XFS_SUPER_MAGIC:
+            get_version = 1;
+            break;
+        }
+    }
+
     if (chroot(rpath) < 0) {
         do_perror("chroot");
         goto error;
hw/9pfs/virtio-9p-proxy.c +31 -0
index f672ac3..bc63835 100644
@@ -209,6 +209,9 @@ static int v9fs_receive_response(V9fsProxy *proxy, int type,
         }
         break;
     }
+    case T_GETVERSION:
+        proxy_unmarshal(reply, 1, HDR_SZ, "q", response);
+        break;
     default:
         *sock_error = 1;
         return -1;
@@ -450,6 +453,13 @@ static int v9fs_request(V9fsProxy *proxy, int type,
         proxy_marshal(iovec, 1, 0, "dd", header.type, header.size);
         header.size += HDR_SZ;
         break;
+    case T_GETVERSION:
+        path = va_arg(ap, V9fsString *);
+        header.size = proxy_marshal(iovec, 1, HDR_SZ, "s", path);
+        header.type = T_GETVERSION;
+        proxy_marshal(iovec, 1, 0, "dd", header.type, header.size);
+        header.size += HDR_SZ;
+        break;
     default:
         error_report("Invalid type %d\n", type);
         va_end(ap);
@@ -497,6 +507,7 @@ static int v9fs_request(V9fsProxy *proxy, int type,
     case T_STATFS:
     case T_LGETXATTR:
     case T_LLISTXATTR:
+    case T_GETVERSION:
         retval = v9fs_receive_response(proxy, type, &sock_error, size,
                         response);
         if (sock_error) {
@@ -978,6 +989,25 @@ static int proxy_unlinkat(FsContext *ctx, V9fsPath *dir,
     return ret;
 }
 
+static int proxy_ioc_getversion(FsContext *fs_ctx, V9fsPath *path,
+                                mode_t st_mode, uint64_t *st_gen)
+{
+    int err;
+
+    /* Do not try to open special files like device nodes, fifos etc
+     * we can get fd for regular files and directories only
+     */
+    if (!S_ISREG(st_mode) && !S_ISDIR(st_mode)) {
+        return 0;
+    }
+    err = v9fs_request(fs_ctx->private, T_GETVERSION, st_gen, "s", path);
+    if (err < 0) {
+        errno = -err;
+        err = -1;
+    }
+    return err;
+}
+
 static int proxy_parse_opts(QemuOpts *opts, struct FsDriverEntry *fs)
 {
     const char *sock_fd = qemu_opt_get(opts, "sock_fd");
@@ -1012,6 +1042,7 @@ static int proxy_init(FsContext *ctx)
     qemu_mutex_init(&proxy->mutex);
 
     ctx->export_flags |= V9FS_PATHNAME_FSCONTEXT;
+    ctx->exops.get_st_gen = proxy_ioc_getversion;
     return 0;
 }
 
hw/9pfs/virtio-9p-proxy.h +1 -0
index 4c691a8..37c260b 100644
@@ -43,6 +43,7 @@ enum {
     T_LLISTXATTR,
     T_LSETXATTR,
     T_LREMOVEXATTR,
+    T_GETVERSION,
 };
 
 typedef struct {
Signature · 1 line
1.7.6
#14 lore raw

PATCH v2 10/12 hw/9pfs: Documentation changes related to proxy fs

In reply to M. Mohan Kumar
---
 qemu-options.hx |   25 ++++++++++++++++---------
 1 files changed, 16 insertions(+), 9 deletions(-)
qemu-options.hx +16 -9
index 681eaf1..cde17ed 100644
@@ -530,19 +530,19 @@ DEFHEADING()
 DEFHEADING(File system options:)
 
 DEF("fsdev", HAS_ARG, QEMU_OPTION_fsdev,
-    "-fsdev fsdriver,id=id,path=path,[security_model={mapped|passthrough|none}]\n"
-    "       [,writeout=immediate][,readonly]\n",
+    "-fsdev fsdriver,id=id[,path=path,][security_model={mapped|passthrough|none}]\n"
+    " [,writeout=immediate][,readonly][,sock_fd=sock_fd]\n",
     QEMU_ARCH_ALL)
 
 STEXI
 
-@item -fsdev @var{fsdriver},id=@var{id},path=@var{path},[security_model=@var{security_model}][,writeout=@var{writeout}][,readonly]
+@item -fsdev @var{fsdriver},id=@var{id},path=@var{path},[security_model=@var{security_model}][,writeout=@var{writeout}][,readonly][,sock_fd=@var{sock_fd}]
 @findex -fsdev
 Define a new file system device. Valid options are:
 @table @option
 @item @var{fsdriver}
 This option specifies the fs driver backend to use.
-Currently "local" and "handle" file system drivers are supported.
+Currently "local", "handle" and "proxy" file system drivers are supported.
 @item id=@var{id}
 Specifies identifier for this device
 @item path=@var{path}
@@ -559,7 +559,7 @@ file attributes. Directories exported by this security model cannot
 interact with other unix tools. "none" security model is same as
 passthrough except the sever won't report failures if it fails to
 set file attributes like ownership. Security model is mandatory
-only for local fsdriver. Other fsdrivers (like handle) don't take
+only for local fsdriver. Other fsdrivers (like handle, proxy) don't take
 security model as a parameter.
 @item writeout=@var{writeout}
 This is an optional argument. The only supported value is "immediate".
@@ -569,6 +569,10 @@ reported as written by the storage subsystem.
 @item readonly
 Enables exporting 9p share as a readonly mount for guests. By default
 read-write access is given.
+@item sock_fd=@var{sock_fd}
+Enables proxy filesystem driver to use passed socket descriptor for
+communicating with virtfs-proxy-helper. Usually a helper like libvirt
+will create socketpair and pass one of the fds as sock_fd
 @end table
 
 -fsdev option is used along with -device driver "virtio-9p-pci".
@@ -589,19 +593,19 @@ DEFHEADING(Virtual File system pass-through options:)
 
 DEF("virtfs", HAS_ARG, QEMU_OPTION_virtfs,
     "-virtfs local,path=path,mount_tag=tag,security_model=[mapped|passthrough|none]\n"
-    "        [,writeout=immediate][,readonly]\n",
+    "        [,writeout=immediate][,readonly][,sock_fd=sock_fd]\n",
     QEMU_ARCH_ALL)
 
 STEXI
 
-@item -virtfs @var{fsdriver},path=@var{path},mount_tag=@var{mount_tag},security_model=@var{security_model}[,writeout=@var{writeout}][,readonly]
+@item -virtfs @var{fsdriver}[,path=@var{path}],mount_tag=@var{mount_tag}[,security_model=@var{security_model}][,writeout=@var{writeout}][,readonly][,sock_fd=@var{sock_fd}]
 @findex -virtfs
 
 The general form of a Virtual File system pass-through options are:
 @table @option
 @item @var{fsdriver}
 This option specifies the fs driver backend to use.
-Currently "local" and "handle" file system drivers are supported.
+Currently "local", "handle" and "proxy" file system drivers are supported.
 @item id=@var{id}
 Specifies identifier for this device
 @item path=@var{path}
@@ -618,7 +622,7 @@ file attributes. Directories exported by this security model cannot
 interact with other unix tools. "none" security model is same as
 passthrough except the sever won't report failures if it fails to
 set file attributes like ownership. Security model is mandatory only
-for local fsdriver. Other fsdrivers (like handle) don't take security
+for local fsdriver. Other fsdrivers (like handle, proxy) don't take security
 model as a parameter.
 @item writeout=@var{writeout}
 This is an optional argument. The only supported value is "immediate".
@@ -628,6 +632,9 @@ reported as written by the storage subsystem.
 @item readonly
 Enables exporting 9p share as a readonly mount for guests. By default
 read-write access is given.
+@item sock_fd
+Enables proxy filesystem driver to use passed 'sock_fd' as the socket
+descriptor for interfacing with virtfs-proxy-helper
 @end table
 ETEXI
 
Signature · 1 line
1.7.6
#15 lore raw

PATCH v2 11/12 hw/9pfs: man page for proxy helper

In reply to M. Mohan Kumar
---
 Makefile                       |   12 +++++++-
 fsdev/virtfs-proxy-helper.texi |   59 ++++++++++++++++++++++++++++++++++++++++
 2 files changed, 70 insertions(+), 1 deletions(-)
 create mode 100644 fsdev/virtfs-proxy-helper.texi
Makefile +11 -1
index 378ee4d..29ae332 100644
@@ -37,6 +37,7 @@ LIBS+=-lz $(LIBS_TOOLS)
 
 ifdef BUILD_DOCS
 DOCS=qemu-doc.html qemu-tech.html qemu.1 qemu-img.1 qemu-nbd.8 QMP/qmp-commands.txt
+DOCS+=fsdev/virtfs-proxy-helper.1
 else
 DOCS=
 endif
@@ -280,7 +281,10 @@ ifdef CONFIG_POSIX
 	$(INSTALL_DIR) "$(DESTDIR)$(mandir)/man8"
 	$(INSTALL_DATA) qemu-nbd.8 "$(DESTDIR)$(mandir)/man8"
 endif
-
+ifdef CONFIG_VIRTFS
+	$(INSTALL_DIR) "$(DESTDIR)$(mandir)/man1"
+	$(INSTALL_DATA) fsdev/virtfs-proxy-helper.1 "$(DESTDIR)$(mandir)/man1"
+endif
 install-sysconfig:
 	$(INSTALL_DIR) "$(DESTDIR)$(sysconfdir)/qemu"
 	$(INSTALL_DATA) $(SRC_PATH)/sysconfigs/target/target-x86_64.conf "$(DESTDIR)$(sysconfdir)/qemu"
@@ -358,6 +362,12 @@ qemu-img.1: qemu-img.texi qemu-img-cmds.texi
 	  pod2man --section=1 --center=" " --release=" " qemu-img.pod > $@, \
 	  "  GEN   $@")
 
+fsdev/virtfs-proxy-helper.1: fsdev/virtfs-proxy-helper.texi
+	$(call quiet-command, \
+	  perl -Ww -- $(SRC_PATH)/scripts/texi2pod.pl $< fsdev/virtfs-proxy-helper.pod && \
+	  pod2man --section=1 --center=" " --release=" " fsdev/virtfs-proxy-helper.pod > $@, \
+	  "  GEN   $@")
+
 qemu-nbd.8: qemu-nbd.texi
 	$(call quiet-command, \
 	  perl -Ww -- $(SRC_PATH)/scripts/texi2pod.pl $< qemu-nbd.pod && \
fsdev/virtfs-proxy-helper.texi new file +59 -0

59 lines not on this page · raw message

Signature · 1 line
1.7.6
#16 lore raw
In reply to M. Mohan Kumar

Changes from previous version:

1) Communication between qemu and helper process is similar to 9p way of packing elements (pdu marshaling).

Quoted 66 lines · M. Mohan Kumar

Pass-through security model in QEMU 9p server needs root privilege to do few file operations (like chown, chmod to any mode/uid:gid). There are two issues in pass-through security model

1) TOCTTOU vulnerability: Following symbolic links in the server could provide access to files beyond 9p export path.

2) Running QEMU with root privilege could be a security issue.

To overcome above issues, following approach is used: A new filesytem type 'proxy' is introduced. Proxy FS uses chroot + socket combination for securing the vulnerability known with following symbolic links. Intention of adding a new filesystem type is to allow qemu to run in non-root mode, but doing privileged operations using socket IO.

Proxy helper(a stand alone binary part of qemu) is invoked with root privileges. Proxy helper chroots into 9p export path and creates a socket pair or a named socket based on the command line parameter. Qemu and proxy helper communicate using this socket. QEMU proxy fs driver sends filesystem request to proxy helper and receives the response from it.

Proxy helper is designed so that it can drop the root privilege but retaining capbilities that are needed for doing filesystem operations (like CAP_DAC_OVERRIDE, CAP_FOWNER etc)

M. Mohan Kumar (12):
hw/9pfs: Move pdu_marshal/unmarshal code to a seperate file hw/9pfs: Add new proxy filesystem driver
hw/9pfs: File system helper process for qemu 9p proxy FS hw/9pfs: Open and create files
hw/9pfs: Create other filesystem objects
hw/9pfs: Add stat/readlink/statfs for proxy FS hw/9pfs: File ownership and others
hw/9pfs: xattr interfaces in proxy filesystem driver hw/9pfs: Proxy getversion
hw/9pfs: Documentation changes related to proxy fs hw/9pfs: man page for proxy helper
hw/9pfs: Add support to use named socket for proxy FS

  Makefile                       |   15 +-
  Makefile.objs                  |    4 +-
  configure                      |   19 +
  fsdev/file-op-9p.h             |    3 +-
  fsdev/qemu-fsdev.c             |    1 +
  fsdev/qemu-fsdev.h             |    1 +
  fsdev/virtfs-proxy-helper.c    |  947 +++++++++++++++++++++++++++++++++
  fsdev/virtfs-proxy-helper.texi |   63 +++
  fsdev/virtio-9p-marshal.c      |  338 ++++++++++++
  fsdev/virtio-9p-marshal.h      |   87 +++
  hw/9pfs/virtio-9p-proxy.c      | 1123 ++++++++++++++++++++++++++++++++++++++++
  hw/9pfs/virtio-9p-proxy.h      |   80 +++
  hw/9pfs/virtio-9p.c            |  297 +-----------
  hw/9pfs/virtio-9p.h            |   85 +---
  qemu-config.c                  |   13 +
  qemu-options.hx                |   32 +-
  vl.c                           |   10 +-
  17 files changed, 2736 insertions(+), 382 deletions(-)

create mode 100644 fsdev/virtfs-proxy-helper.c create mode 100644 fsdev/virtfs-proxy-helper.texi create mode 100644 fsdev/virtio-9p-marshal.c create mode 100644 fsdev/virtio-9p-marshal.h create mode 100644 hw/9pfs/virtio-9p-proxy.c create mode 100644 hw/9pfs/virtio-9p-proxy.h

#17 lore raw
Quoted 5 lines · M. Mohan Kumar

Changes from previous version:

1) Communication between qemu and helper process is similar to 9p way of packing
elements (pdu marshaling).

There is code I haven't reviewed yet but I think it will change as you add input validation, so I will wait for v3.

Stefan

#18 lore raw

PATCH v2 12/12 hw/9pfs: Add support to use named socket for proxy FS

In reply to M. Mohan Kumar

Add option to use named socket for communicating between proxy helper and qemu proxy FS. Access to socket can be given by using command line options -u and -g. We can achive the same using a shell script over qemu and virtfs-proxy-helper using exec fd<><socket-path>, and then passing that fd as argument to qemu and virtfs-proxy-helper. Also having a server like virtfs-proxy-helper listening on a pathname without any authentication is little bit scary. So we have to decide whether this patch is really needed.

---
 fsdev/file-op-9p.h             |    2 +
 fsdev/virtfs-proxy-helper.c    |   88 ++++++++++++++++++++++++++++++++++++++--
 fsdev/virtfs-proxy-helper.texi |    4 ++
 hw/9pfs/virtio-9p-proxy.c      |   52 ++++++++++++++++++++---
 qemu-config.c                  |    7 +++
 qemu-options.hx                |   15 +++++--
 vl.c                           |    6 ++-
 7 files changed, 158 insertions(+), 16 deletions(-)
fsdev/file-op-9p.h +2 -0
index 84e5375..ac98e10 100644
@@ -57,6 +57,8 @@ typedef struct extended_ops {
  */
 #define V9FS_SM_NONE                0x00000010
 #define V9FS_RDONLY                 0x00000020
+#define V9FS_PROXY_SOCK_FD          0x00000040
+#define V9FS_PROXY_SOCK_NAME        0x00000080
 
 #define V9FS_SEC_MASK               0x0000001C
 
fsdev/virtfs-proxy-helper.c +84 -4

139 lines not on this page · raw message

fsdev/virtfs-proxy-helper.texi +4 -0
index 3816382..faa0434 100644
@@ -46,6 +46,10 @@ Path to export for proxy filesystem driver
 Use given file descriptor as socket descriptor for communicating with
 qemu proxy fs drier. Usually a helper like libvirt will create
 socketpair and pass one of the fds as parameter to -f|--fd
+@item -s|--socket socket-file
+Creates named socket file for communicating with qemu proxy fs driver
+@item -u|--uid uid -g|--gid gid
+uid:gid combination to give access to named socket file
 @item -n|--nodaemon
 Run as a normal program. By default program will run in daemon mode
 @end table
hw/9pfs/virtio-9p-proxy.c +45 -7

70 lines not on this page · raw message

qemu-config.c +7 -0
index 33367fe..48093ca 100644
@@ -183,6 +183,10 @@ QemuOptsList qemu_fsdev_opts = {
         }, {
             .name = "readonly",
             .type = QEMU_OPT_BOOL,
+
+        }, {
+            .name = "socket",
+            .type = QEMU_OPT_STRING,
         }, {
             .name = "sock_fd",
             .type = QEMU_OPT_NUMBER,
@@ -216,6 +220,9 @@ QemuOptsList qemu_virtfs_opts = {
             .name = "readonly",
             .type = QEMU_OPT_BOOL,
         }, {
+            .name = "socket",
+            .type = QEMU_OPT_STRING,
+        }, {
             .name = "sock_fd",
             .type = QEMU_OPT_NUMBER,
         },
qemu-options.hx +11 -4
index cde17ed..a686f87 100644
@@ -531,12 +531,12 @@ DEFHEADING(File system options:)
 
 DEF("fsdev", HAS_ARG, QEMU_OPTION_fsdev,
     "-fsdev fsdriver,id=id[,path=path,][security_model={mapped|passthrough|none}]\n"
-    " [,writeout=immediate][,readonly][,sock_fd=sock_fd]\n",
+    " [,writeout=immediate][,readonly][,socket=socket|sock_fd=sock_fd]\n",
     QEMU_ARCH_ALL)
 
 STEXI
 
-@item -fsdev @var{fsdriver},id=@var{id},path=@var{path},[security_model=@var{security_model}][,writeout=@var{writeout}][,readonly][,sock_fd=@var{sock_fd}]
+@item -fsdev @var{fsdriver},id=@var{id},path=@var{path},[security_model=@var{security_model}][,writeout=@var{writeout}][,readonly][,socket=@var{socket}|sock_fd=@var{sock_fd}]
 @findex -fsdev
 Define a new file system device. Valid options are:
 @table @option
@@ -569,6 +569,9 @@ reported as written by the storage subsystem.
 @item readonly
 Enables exporting 9p share as a readonly mount for guests. By default
 read-write access is given.
+@item socket=@var{socket}
+Enables proxy filesystem driver to use passed socket file for communicating
+with virtfs-proxy-helper
 @item sock_fd=@var{sock_fd}
 Enables proxy filesystem driver to use passed socket descriptor for
 communicating with virtfs-proxy-helper. Usually a helper like libvirt
@@ -593,12 +596,12 @@ DEFHEADING(Virtual File system pass-through options:)
 
 DEF("virtfs", HAS_ARG, QEMU_OPTION_virtfs,
     "-virtfs local,path=path,mount_tag=tag,security_model=[mapped|passthrough|none]\n"
-    "        [,writeout=immediate][,readonly][,sock_fd=sock_fd]\n",
+    "        [,writeout=immediate][,readonly][,socket=socket|sock_fd=sock_fd]\n",
     QEMU_ARCH_ALL)
 
 STEXI
 
-@item -virtfs @var{fsdriver}[,path=@var{path}],mount_tag=@var{mount_tag}[,security_model=@var{security_model}][,writeout=@var{writeout}][,readonly][,sock_fd=@var{sock_fd}]
+@item -virtfs @var{fsdriver}[,path=@var{path}],mount_tag=@var{mount_tag}[,security_model=@var{security_model}][,writeout=@var{writeout}][,readonly][,socket=@var{socket}|sock_fd=@var{sock_fd}]
 @findex -virtfs
 
 The general form of a Virtual File system pass-through options are:
@@ -632,6 +635,10 @@ reported as written by the storage subsystem.
 @item readonly
 Enables exporting 9p share as a readonly mount for guests. By default
 read-write access is given.
+@item socket=@var{socket}
+Enables proxy filesystem driver to use passed socket file for
+communicating with virtfs-proxy-helper. Usually a helper like libvirt
+will create socketpair and pass one of the fds as sock_fd
 @item sock_fd
 Enables proxy filesystem driver to use passed 'sock_fd' as the socket
 descriptor for interfacing with virtfs-proxy-helper
vl.c +5 -1
index 1feaf37..697f92a 100644
@@ -2663,7 +2663,7 @@ int main(int argc, char **argv, char **envp)
             case QEMU_OPTION_virtfs: {
                 QemuOpts *fsdev;
                 QemuOpts *device;
-                const char *writeout, *sock_fd;
+                const char *writeout, *sock_fd, *socket;
 
                 olist = qemu_find_opts("virtfs");
                 if (!olist) {
@@ -2703,6 +2703,10 @@ int main(int argc, char **argv, char **envp)
                 qemu_opt_set(fsdev, "path", qemu_opt_get(opts, "path"));
                 qemu_opt_set(fsdev, "security_model",
                              qemu_opt_get(opts, "security_model"));
+                socket = qemu_opt_get(opts, "socket");
+                if (socket) {
+                    qemu_opt_set(fsdev, "socket", socket);
+                }
                 sock_fd = qemu_opt_get(opts, "sock_fd");
                 if (sock_fd) {
                     qemu_opt_set(fsdev, "sock_fd", sock_fd);
Signature · 1 line
1.7.6
#19 lore raw

cover Proxy FS driver for VirtFS

In reply to M. Mohan Kumar

Pass-through security model in QEMU 9p server needs root privilege to do few file operations (like chown, chmod to any mode/uid:gid). There are two issues in pass-through security model

1) TOCTTOU vulnerability: Following symbolic links in the server could provide access to files beyond 9p export path.

2) Running QEMU with root privilege could be a security issue.

To overcome above issues, following approach is used: A new filesytem type 'proxy' is introduced. Proxy FS uses chroot + socket combination for securing the vulnerability known with following symbolic links. Intention of adding a new filesystem type is to allow qemu to run in non-root mode, but doing privileged operations using socket IO.

Proxy helper(a stand alone binary part of qemu) is invoked with root privileges. Proxy helper chroots into 9p export path and creates a socket pair or a named socket based on the command line parameter. Qemu and proxy helper communicate using this socket. QEMU proxy fs driver sends filesystem request to proxy helper and receives the response from it.

Proxy helper is designed so that it can drop the root privilege but retaining capbilities that are needed for doing filesystem operations (like CAP_DAC_OVERRIDE, CAP_FOWNER etc)

M. Mohan Kumar (12):
hw/9pfs: Move pdu_marshal/unmarshal code to a seperate file hw/9pfs: Add new proxy filesystem driver
hw/9pfs: File system helper process for qemu 9p proxy FS hw/9pfs: Open and create files
hw/9pfs: Create other filesystem objects
hw/9pfs: Add stat/readlink/statfs for proxy FS hw/9pfs: File ownership and others
hw/9pfs: xattr interfaces in proxy filesystem driver hw/9pfs: Proxy getversion
hw/9pfs: Documentation changes related to proxy fs hw/9pfs: man page for proxy helper
hw/9pfs: Add support to use named socket for proxy FS

 Makefile                       |   15 +-
 Makefile.objs                  |    4 +-
 configure                      |   19 +
 fsdev/file-op-9p.h             |    3 +-
 fsdev/qemu-fsdev.c             |    1 +
 fsdev/qemu-fsdev.h             |    1 +
 fsdev/virtfs-proxy-helper.c    |  947 +++++++++++++++++++++++++++++++++
 fsdev/virtfs-proxy-helper.texi |   63 +++
 fsdev/virtio-9p-marshal.c      |  338 ++++++++++++
 fsdev/virtio-9p-marshal.h      |   87 +++
 hw/9pfs/virtio-9p-proxy.c      | 1123 ++++++++++++++++++++++++++++++++++++++++
 hw/9pfs/virtio-9p-proxy.h      |   80 +++
 hw/9pfs/virtio-9p.c            |  297 +-----------
 hw/9pfs/virtio-9p.h            |   85 +---
 qemu-config.c                  |   13 +
 qemu-options.hx                |   32 +-
 vl.c                           |   10 +-
 17 files changed, 2736 insertions(+), 382 deletions(-)
 create mode 100644 fsdev/virtfs-proxy-helper.c
 create mode 100644 fsdev/virtfs-proxy-helper.texi
 create mode 100644 fsdev/virtio-9p-marshal.c
 create mode 100644 fsdev/virtio-9p-marshal.h
 create mode 100644 hw/9pfs/virtio-9p-proxy.c
 create mode 100644 hw/9pfs/virtio-9p-proxy.h
Signature · 1 line
1.7.6