Skip to contentSkip to conversation

virtio-9p: Use chroot to safely access files in passthrough security model

PATCH Superseded M. Mohan Kumar 3 participants 19 messages Jun 24, 2011 – Jul 25, 2011 Kbuild

v11 of 12 Superseded Latest is v12 →

lore
Share & tools
lore
#0 lore raw

v11 cover virtio-9p: Use chroot to safely access files in passthrough security model

In passthrough security model, following symbolic links in the server side could result in TOCTTOU vulnerabilities.
(http://en.wikipedia.org/wiki/Time-of-check-to-time-of-use)

This patchset resolves this issue by creating a dedicated process which chroots into the share path and all file object access is done in the chroot environment.

This patchset implements chroot enviroment, provides necessary functions that can be used by the passthrough function calls.

This patchset is rebased on top of 9p coroutines patches posted to qemu-devel list
http://lists.nongnu.org/archive/html/qemu-devel/2011-05/msg02796.html

Changes from version V10:
* Added support to do lstat and readlink from chroot process
* Fixed an issue with dealing fds when qemu process reached maxfds limit

Changes from version V9:
* Error handling in special file object creation in virtio-9p-local.c

Changes from version V8:
* Make chmod and chown also operate under chroot process
* Check for invalid path requests, minor cleanups

Changes from version V7:
* Add two chroot methods remove and rename
* Minor cleanups like consolidating functions

Changes from version V6:
* Send only fd/errno in socket operations instead of FdInfo structure
* Minor cleanups

Changes from version V5:
* Return errno on failure instead of setting errno
* Minor cleanups like updated comments, enable CONFIG_THREAD if CONFIG_VIRTFS is enabled

Changes from version V4:
* Avoid using malloc/free inside chroot process
* Seperate chroot server and client functions

Changes from version V3
* Return EIO incase of socket read/write fail instead of exiting
* Changed data types as suggested by Blue Swirl
* Chroot process reports error through qemu process

Changes from version V2
* Treat socket IO errors as fatal, ie qemu will exit
* Split patchset based on chroot side (server) and qemu side(client) functionalities

M. Mohan Kumar (15):
Implement qemu_read_full
virtio-9p: Enable CONFIG_THREAD if CONFIG_VIRTFS is enabled
virtio-9p: Provide chroot worker side interfaces
virtio-9p: Add qemu side interfaces for chroot environment
virtio-9p: Add support to open a file in chroot environment
virtio-9p: Create support in chroot environment
virtio-9p: Support for creating special files
virtio-9p: Add support for removing file or directory
virtio-9p: Add support to rename
virtio-9p: Move file post creation changes to none security model
virtio-9p: Add support for chmod
virtio-9p: Add support for chown
virtio-9p: Chroot environment for other functions
virtio-9p: Add stat functionality to chroot
virtio-9p: Add readlink support to chroot

 Makefile.objs                     |    1 +
 configure                         |    1 +
 fsdev/file-op-9p.h                |    3 +
 hw/9pfs/virtio-9p-chroot-worker.c |  418 +++++++++++++++++++++++++++++++++++++
 hw/9pfs/virtio-9p-chroot.c        |  174 +++++++++++++++
 hw/9pfs/virtio-9p-chroot.h        |   54 +++++
 hw/9pfs/virtio-9p-device.c        |   24 ++
 hw/9pfs/virtio-9p-local.c         |  248 ++++++++++++++++++----
 osdep.c                           |   32 +++
 qemu-common.h                     |    2 +
 10 files changed, 910 insertions(+), 47 deletions(-)
 create mode 100644 hw/9pfs/virtio-9p-chroot-worker.c
 create mode 100644 hw/9pfs/virtio-9p-chroot.c
 create mode 100644 hw/9pfs/virtio-9p-chroot.h
Signature · 1 line
1.7.5.1
#1 lore raw

PATCH v11 1/15 Implement qemu_read_full

From: "M. Mohan Kumar" <[email protected]>

---
 osdep.c       |   32 ++++++++++++++++++++++++++++++++
 qemu-common.h |    2 ++
 2 files changed, 34 insertions(+), 0 deletions(-)
osdep.c +32 -0
index 56e6963..5a4d670 100644
@@ -126,6 +126,38 @@ ssize_t qemu_write_full(int fd, const void *buf, size_t count)
 }
 
 /*
+ * A variant of read(2) which handles interrupted read.
+ * Simlar to qemu_write_full function
+ *
+ * Return the number of bytes read.
+ *
+ * This function does not work with non-blocking fd's.
+ * errno is set if fewer than `count' bytes are read because of any
+ * error
+ */
+ssize_t qemu_read_full(int fd, void *buf, size_t count)
+{
+    ssize_t ret = 0;
+    ssize_t total = 0;
+
+    while (count) {
+        ret = read(fd, buf, count);
+        if (ret <= 0) {
+            if (errno == EINTR) {
+                continue;
+            }
+            break;
+        }
+
+        count -= ret;
+        buf += ret;
+        total += ret;
+    }
+
+    return total;
+}
+
+/*
  * Opens a socket with FD_CLOEXEC set
  */
 int qemu_socket(int domain, int type, int protocol)
qemu-common.h +2 -0
index 39fabc9..ea74ead 100644
@@ -216,6 +216,8 @@ void qemu_mutex_unlock_iothread(void);
 int qemu_open(const char *name, int flags, ...);
 ssize_t qemu_write_full(int fd, const void *buf, size_t count)
     QEMU_WARN_UNUSED_RESULT;
+ssize_t qemu_read_full(int fd, void *buf, size_t count)
+    QEMU_WARN_UNUSED_RESULT;
 void qemu_set_cloexec(int fd);
 
 #ifndef _WIN32
Signature · 1 line
1.7.5.4
#2 lore raw

PATCH v11 2/15 virtio-9p: Enable CONFIG_THREAD if CONFIG_VIRTFS is enabled

In reply to M. Mohan Kumar

From: "M. Mohan Kumar" <[email protected]>

9p Chroot environment needs APIs defined in qemu-thread.c, so enable CONFIG_THREAD if virtfs is enabled

---
 configure |    1 +
 1 files changed, 1 insertions(+), 0 deletions(-)
configure +1 -0
index df63403..888ac87 100755
@@ -2964,6 +2964,7 @@ fi
 if test "$linux" = "yes" ; then
   if test "$attr" = "yes" ; then
     echo "CONFIG_VIRTFS=y" >> $config_host_mak
+    echo "CONFIG_THREAD=y" >> $config_host_mak
   fi
 fi
 if test "$blobs" = "yes" ; then
Signature · 1 line
1.7.5.4
#3 lore raw

PATCH v11 3/15 virtio-9p: Provide chroot worker side interfaces

In reply to M. Mohan Kumar

From: "M. Mohan Kumar" <[email protected]>

Implement chroot worker side interfaces like sending the file descriptor to qemu process, reading the object request from socket etc. Also add chroot main function and other helper routines.

[[email protected]: Do not send fd as part of data, instead a special value is sent as part of data when fd is sent as part of ancillary data]

---
 Makefile.objs                     |    1 +
 fsdev/file-op-9p.h                |    3 +
 hw/9pfs/virtio-9p-chroot-worker.c |  193 +++++++++++++++++++++++++++++++++++++
 hw/9pfs/virtio-9p-chroot.h        |   39 ++++++++
 hw/9pfs/virtio-9p-device.c        |   24 +++++
 5 files changed, 260 insertions(+), 0 deletions(-)
 create mode 100644 hw/9pfs/virtio-9p-chroot-worker.c
 create mode 100644 hw/9pfs/virtio-9p-chroot.h
Makefile.objs +1 -0
index e38ca15..588eae2 100644
@@ -304,6 +304,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-chroot-worker.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 +3 -0
index 5d088d4..66dac07 100644
@@ -19,6 +19,7 @@
 #include <sys/stat.h>
 #include <sys/uio.h>
 #include <sys/vfs.h>
+#include "qemu-thread.h"
 
 #define SM_LOCAL_MODE_BITS    0600
 #define SM_LOCAL_DIR_MODE_BITS    0700
@@ -63,6 +64,8 @@ typedef struct FsContext
     struct xattr_operations **xops;
     /* fs driver specific data */
     void *private;
+    QemuMutex chroot_mutex;
+    int chroot_socket;
 } FsContext;
 
 typedef struct V9fsPath {
hw/9pfs/virtio-9p-chroot-worker.c new file +193 -0

193 lines not on this page · raw message

hw/9pfs/virtio-9p-chroot.h new file +39 -0
new file mode 100644
index 0000000..c2a4a6e
@@ -0,0 +1,39 @@
+#ifndef _QEMU_VIRTIO_9P_CHROOT_H
+#define _QEMU_VIRTIO_9P_CHROOT_H
+
+#include "qemu_socket.h"
+/* types for V9fsFileObjectRequest */
+#define T_OPEN      1
+
+#define V9FS_FD_VALID INT_MAX
+
+union MsgControl {
+    struct cmsghdr cmsg;
+    char control[CMSG_SPACE(sizeof(int))];
+};
+
+struct V9fsFileObjectData
+{
+    int flags;
+    int mode;
+    uid_t uid;
+    gid_t gid;
+    dev_t dev;
+    int type;
+};
+
+struct V9fsFileObjectPath
+{
+    char path[PATH_MAX];
+    char old_path[PATH_MAX];
+};
+
+typedef struct V9fsFileObjectRequest
+{
+    struct V9fsFileObjectData data;
+    struct V9fsFileObjectPath path;
+} V9fsFileObjectRequest;
+
+int v9fs_chroot(FsContext *fs_ctx);
+
+#endif /* _QEMU_VIRTIO_9P_CHROOT_H */
hw/9pfs/virtio-9p-device.c +24 -0
index 0b40ba4..b302de7 100644
@@ -19,6 +19,8 @@
 #include "fsdev/qemu-fsdev.h"
 #include "virtio-9p-xattr.h"
 #include "virtio-9p-coth.h"
+#include "qemu-error.h"
+#include "virtio-9p-chroot.h"
 
 static uint32_t virtio_9p_get_features(VirtIODevice *vdev, uint32_t features)
 {
@@ -150,6 +152,28 @@ VirtIODevice *virtio_9p_init(DeviceState *dev, V9fsConf *conf)
         fprintf(stderr, "worker thread initialization failed\n");
         exit(1);
     }
+    if (s->ctx.fs_sm == SM_PASSTHROUGH) {
+        uint32_t code;
+        qemu_mutex_init(&s->ctx.chroot_mutex);
+        if (v9fs_chroot(&s->ctx) < 0) {
+            exit(1);
+        }
+
+        /*
+         * Chroot process sends 0 to indicate chroot process creation is
+         * successful
+         */
+        if (qemu_read_full(s->ctx.chroot_socket, &code,
+                                sizeof(code)) != sizeof(code)) {
+            error_report("chroot process creation failed");
+            exit(1);
+        }
+        if (code != 0) {
+            error_report("chroot system call failed: %s", strerror(code));
+            exit(1);
+        }
+    }
+
     return &s->vdev;
 }
 
Signature · 1 line
1.7.5.4
#4 lore raw

PATCH v11 4/15 virtio-9p: qemu interfaces for chroot environment

In reply to M. Mohan Kumar

From: "M. Mohan Kumar" <[email protected]>

QEMU side interfaces to communicate with chroot worker process.

[[email protected]: Handle when qemu process can not receive fd because it already reached max fds]

---
 Makefile.objs              |    2 +-
 hw/9pfs/virtio-9p-chroot.c |  103 ++++++++++++++++++++++++++++++++++++++++++++
 hw/9pfs/virtio-9p-chroot.h |    1 +
 3 files changed, 105 insertions(+), 1 deletions(-)
 create mode 100644 hw/9pfs/virtio-9p-chroot.c
Makefile.objs +1 -1
index 588eae2..9ff332f 100644
@@ -304,7 +304,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-chroot-worker.o
+9pfs-nested-$(CONFIG_VIRTFS) += virtio-9p-chroot-worker.o virtio-9p-chroot.o
 
 hw-obj-$(CONFIG_REALLY_VIRTFS) += $(addprefix 9pfs/, $(9pfs-nested-y))
 $(addprefix 9pfs/, $(9pfs-nested-y)): QEMU_CFLAGS+=$(GLIB_CFLAGS)
hw/9pfs/virtio-9p-chroot.c new file +103 -0

103 lines not on this page · raw message

hw/9pfs/virtio-9p-chroot.h +1 -0
index c2a4a6e..a817bcf 100644
@@ -35,5 +35,6 @@ typedef struct V9fsFileObjectRequest
 } V9fsFileObjectRequest;
 
 int v9fs_chroot(FsContext *fs_ctx);
+void chroot_dummy(void);
 
 #endif /* _QEMU_VIRTIO_9P_CHROOT_H */
Signature · 1 line
1.7.5.4
#5 lore raw

PATCH v11 5/15 virtio-9p: Support for opening a file in chroot environment

In reply to M. Mohan Kumar

From: "M. Mohan Kumar" <[email protected]>

This patch adds both chroot worker and qemu side support to open a file/ directory in the chroot environment

---
 hw/9pfs/virtio-9p-chroot.c |   29 ++++++++++++----
 hw/9pfs/virtio-9p-chroot.h |    2 +-
 hw/9pfs/virtio-9p-local.c  |   78 ++++++++++++++++++++++++++++++++++++++++++--
 3 files changed, 97 insertions(+), 12 deletions(-)
hw/9pfs/virtio-9p-chroot.c +21 -8
index 63de410..f5b3abc 100644
@@ -91,13 +91,26 @@ static int v9fs_write_request(int sockfd, V9fsFileObjectRequest *request)
     return 0;
 }
 
-/*
- * This patch adds v9fs_receivefd and v9fs_write_request functions,
- * but there is no caller. To avoid compiler warning message,
- * refer these two functions
- */
-void chroot_dummy(void)
+/* Return opened file descriptor on success or -errno on error */
+int v9fs_request(FsContext *fs_ctx, V9fsFileObjectRequest *request)
 {
-    (void)v9fs_receivefd;
-    (void)v9fs_write_request;
+    int fd, sock_error;
+    qemu_mutex_lock(&fs_ctx->chroot_mutex);
+    if (fs_ctx->chroot_socket == -1) {
+        goto error;
+    }
+    if (v9fs_write_request(fs_ctx->chroot_socket, request) < 0) {
+        goto error;
+    }
+    fd = v9fs_receivefd(fs_ctx->chroot_socket, &sock_error);
+    if (fd < 0 && sock_error) {
+        goto error;
+    }
+    qemu_mutex_unlock(&fs_ctx->chroot_mutex);
+    return fd;
+error:
+    close(fs_ctx->chroot_socket);
+    fs_ctx->chroot_socket = -1;
+    qemu_mutex_unlock(&fs_ctx->chroot_mutex);
+    return -EIO;
 }
hw/9pfs/virtio-9p-chroot.h +1 -1
index a817bcf..326238d 100644
@@ -35,6 +35,6 @@ typedef struct V9fsFileObjectRequest
 } V9fsFileObjectRequest;
 
 int v9fs_chroot(FsContext *fs_ctx);
-void chroot_dummy(void);
+int v9fs_request(FsContext *fs_ctx, V9fsFileObjectRequest *or);
 
 #endif /* _QEMU_VIRTIO_9P_CHROOT_H */
hw/9pfs/virtio-9p-local.c +75 -3

108 lines not on this page · raw message

Signature · 1 line
1.7.5.4
#6 lore raw

PATCH v11 6/15 virtio-9p: Create support in chroot environment

In reply to M. Mohan Kumar

From: "M. Mohan Kumar" <[email protected]>

Add both chroot worker & qemu side interfaces to create regular files in chroot environment

---
 hw/9pfs/virtio-9p-chroot-worker.c |   36 ++++++++++++++++++++++++++++++++++++
 hw/9pfs/virtio-9p-chroot.h        |    1 +
 hw/9pfs/virtio-9p-local.c         |    5 +++--
 3 files changed, 40 insertions(+), 2 deletions(-)
hw/9pfs/virtio-9p-chroot-worker.c +36 -0
index 40a54b3..581bfa95a 100644
@@ -93,6 +93,36 @@ static int chroot_do_open(V9fsFileObjectRequest *request)
     return fd;
 }
 
+/*
+ * Helper routine to create a file and return the file descriptor
+ */
+static int chroot_do_create(V9fsFileObjectRequest *request)
+{
+    uid_t cur_uid;
+    gid_t cur_gid;
+    int fd = -1;
+
+    cur_uid = geteuid();
+    cur_gid = getegid();
+
+    if (setfsuid(request->data.uid) < 0) {
+        return -errno;
+    }
+    if (setfsgid(request->data.gid) < 0) {
+        fd = -errno;
+        goto unset_uid;
+    }
+
+    fd = open(request->path.path, request->data.flags, request->data.mode);
+    if (fd < 0) {
+        fd = -errno;
+    }
+    setfsgid(cur_gid);
+unset_uid:
+    setfsuid(cur_uid);
+    return fd;
+}
+
 static void chroot_daemonize(int chroot_sock)
 {
     sigset_t sigset;
@@ -184,6 +214,12 @@ int v9fs_chroot(FsContext *fs_ctx)
                 valid_fd = 1;
             }
             break;
+        case T_CREATE:
+            retval = chroot_do_create(&request);
+            if (retval >= 0) {
+                valid_fd = 1;
+            }
+            break;
         default:
             retval = -1;
             break;
hw/9pfs/virtio-9p-chroot.h +1 -0
index 326238d..d5c3f37 100644
@@ -4,6 +4,7 @@
 #include "qemu_socket.h"
 /* types for V9fsFileObjectRequest */
 #define T_OPEN      1
+#define T_CREATE    2
 
 #define V9FS_FD_VALID INT_MAX
 
hw/9pfs/virtio-9p-local.c +3 -2
index 2ae4317..2ae08e8 100644
@@ -455,8 +455,7 @@ static int local_open2(FsContext *fs_ctx, V9fsPath *dir_path, const char *name,
             serrno = errno;
             goto err_end;
         }
-    } else if ((fs_ctx->fs_sm == SM_PASSTHROUGH) ||
-               (fs_ctx->fs_sm == SM_NONE)) {
+    } else if (fs_ctx->fs_sm == SM_NONE) {
         fd = open(rpath(fs_ctx, path, buffer), flags, credp->fc_mode);
         if (fd == -1) {
             err = fd;
@@ -467,6 +466,8 @@ static int local_open2(FsContext *fs_ctx, V9fsPath *dir_path, const char *name,
             serrno = errno;
             goto err_end;
         }
+    } else if (fs_ctx->fs_sm == SM_PASSTHROUGH) {
+        fd = passthrough_request(fs_ctx, NULL, path, flags, credp, T_CREATE);
     }
     err = fd;
     goto out;
Signature · 1 line
1.7.5.4
#7 lore raw

PATCH v11 7/15 virtio-9p: Creating special files in chroot environment

In reply to M. Mohan Kumar

From: "M. Mohan Kumar" <[email protected]>

Add both chroot worker and qemu side interfaces to create special files (directory, device nodes, links and symbolic links)

---
 hw/9pfs/virtio-9p-chroot-worker.c |   52 +++++++++++++++++++++++++++++++++++++
 hw/9pfs/virtio-9p-chroot.h        |    5 +++
 hw/9pfs/virtio-9p-local.c         |   37 +++++++++++++-------------
 3 files changed, 75 insertions(+), 19 deletions(-)
hw/9pfs/virtio-9p-chroot-worker.c +52 -0
index 581bfa95a..10f290d 100644
@@ -123,6 +123,52 @@ unset_uid:
     return fd;
 }
 
+/*
+ * Create directory, symbolic link, link, device node and regular files
+ * Similar to create, but it does not return the fd of created object
+ * Returns 0 as file descriptor on success and -errno on failure
+ */
+static int chroot_do_create_special(V9fsFileObjectRequest *request)
+{
+    int cur_uid, cur_gid;
+    int retval = -1;
+
+    cur_uid = geteuid();
+    cur_gid = getegid();
+
+    if (setfsuid(request->data.uid) < 0) {
+        return -errno;
+    }
+    if (setfsgid(request->data.gid) < 0) {
+        retval = -errno;
+        goto unset_uid;
+    }
+
+    switch (request->data.type) {
+    case T_MKDIR:
+        retval = mkdir(request->path.path, request->data.mode);
+        break;
+    case T_SYMLINK:
+        retval = symlink(request->path.old_path, request->path.path);
+        break;
+    case T_LINK:
+        retval = link(request->path.old_path, request->path.path);
+        break;
+    default:
+        retval = mknod(request->path.path, request->data.mode,
+                        request->data.dev);
+        break;
+    }
+
+    if (retval < 0) {
+        retval = -errno;
+    }
+    setfsgid(cur_gid);
+unset_uid:
+    setfsuid(cur_uid);
+    return retval;
+}
+
 static void chroot_daemonize(int chroot_sock)
 {
     sigset_t sigset;
@@ -220,6 +266,12 @@ int v9fs_chroot(FsContext *fs_ctx)
                 valid_fd = 1;
             }
             break;
+        case T_MKDIR:
+        case T_SYMLINK:
+        case T_LINK:
+        case T_MKNOD:
+            retval = chroot_do_create_special(&request);
+            break;
         default:
             retval = -1;
             break;
hw/9pfs/virtio-9p-chroot.h +5 -0
index d5c3f37..9075361 100644
@@ -5,6 +5,10 @@
 /* types for V9fsFileObjectRequest */
 #define T_OPEN      1
 #define T_CREATE    2
+#define T_MKDIR     3
+#define T_MKNOD     4
+#define T_SYMLINK   5
+#define T_LINK      6
 
 #define V9FS_FD_VALID INT_MAX
 
@@ -37,5 +41,6 @@ typedef struct V9fsFileObjectRequest
 
 int v9fs_chroot(FsContext *fs_ctx);
 int v9fs_request(FsContext *fs_ctx, V9fsFileObjectRequest *or);
+int v9fs_create_special(FsContext *fs_ctx, V9fsFileObjectRequest *request);
 
 #endif /* _QEMU_VIRTIO_9P_CHROOT_H */
hw/9pfs/virtio-9p-local.c +18 -19
index 2ae08e8..a1eff5f 100644
@@ -327,8 +327,7 @@ static int local_mknod(FsContext *fs_ctx, V9fsPath *dir_path,
             serrno = errno;
             goto err_end;
         }
-    } else if ((fs_ctx->fs_sm == SM_PASSTHROUGH) ||
-               (fs_ctx->fs_sm == SM_NONE)) {
+    } else if (fs_ctx->fs_sm == SM_NONE) {
         err = mknod(rpath(fs_ctx, path, buffer), credp->fc_mode,
                 credp->fc_rdev);
         if (err == -1) {
@@ -339,6 +338,8 @@ static int local_mknod(FsContext *fs_ctx, V9fsPath *dir_path,
             serrno = errno;
             goto err_end;
         }
+    } else {
+        err = passthrough_request(fs_ctx, NULL, path, 0, credp, T_MKNOD);
     }
     goto out;
 
@@ -375,8 +376,7 @@ static int local_mkdir(FsContext *fs_ctx, V9fsPath *dir_path,
             serrno = errno;
             goto err_end;
         }
-    } else if ((fs_ctx->fs_sm == SM_PASSTHROUGH) ||
-               (fs_ctx->fs_sm == SM_NONE)) {
+    } else if (fs_ctx->fs_sm == SM_NONE) {
         err = mkdir(rpath(fs_ctx, path, buffer), credp->fc_mode);
         if (err == -1) {
             goto out;
@@ -386,6 +386,8 @@ static int local_mkdir(FsContext *fs_ctx, V9fsPath *dir_path,
             serrno = errno;
             goto err_end;
         }
+    } else {
+        err = passthrough_request(fs_ctx, NULL, path, 0, credp, T_MKDIR);
     }
     goto out;
 
@@ -524,25 +526,17 @@ static int local_symlink(FsContext *fs_ctx, const char *oldpath,
             serrno = errno;
             goto err_end;
         }
-    } else if ((fs_ctx->fs_sm == SM_PASSTHROUGH) ||
-               (fs_ctx->fs_sm == SM_NONE)) {
+    } else if (fs_ctx->fs_sm == SM_NONE) {
         err = symlink(oldpath, rpath(fs_ctx, newpath, buffer));
         if (err) {
             goto out;
         }
         err = lchown(rpath(fs_ctx, newpath, buffer), credp->fc_uid,
                      credp->fc_gid);
-        if (err == -1) {
-            /*
-             * If we fail to change ownership and if we are
-             * using security model none. Ignore the error
-             */
-            if (fs_ctx->fs_sm != SM_NONE) {
-                serrno = errno;
-                goto err_end;
-            } else
-                err = 0;
-        }
+        err = 0;
+    } else {
+        err = passthrough_request(fs_ctx, oldpath, newpath, 0, credp,
+                        T_SYMLINK);
     }
     goto out;
 
@@ -564,8 +558,13 @@ static int local_link(FsContext *ctx, V9fsPath *oldpath,
     v9fs_string_init(&newpath);
     v9fs_string_sprintf(&newpath, "%s/%s", dirpath->data, name);
 
-    ret = link(rpath(ctx, oldpath->data, buffer),
-               rpath(ctx, newpath.data, buffer1));
+    if (ctx->fs_sm == SM_PASSTHROUGH) {
+        ret = passthrough_request(ctx, oldpath->data, newpath.data, 0, NULL,
+                        T_LINK);
+    } else {
+        ret = link(rpath(ctx, oldpath->data, buffer),
+                rpath(ctx, newpath.data, buffer1));
+    }
     v9fs_string_free(&newpath);
     return ret;
 }
Signature · 1 line
1.7.5.4
#8 lore raw

PATCH v11 8/15 virtio-9p: Removing file or directory in chroot environment

In reply to M. Mohan Kumar

From: "M. Mohan Kumar" <[email protected]>

Support for removing file or directory in chroot environment. Add interfaces to remove file/directory in chroot worker and qemu side.

---
 hw/9pfs/virtio-9p-chroot-worker.c |   18 ++++++++++++++++++
 hw/9pfs/virtio-9p-chroot.h        |    1 +
 hw/9pfs/virtio-9p-local.c         |    4 ++++
 3 files changed, 23 insertions(+), 0 deletions(-)
hw/9pfs/virtio-9p-chroot-worker.c +18 -0
index 10f290d..f269c7b 100644
@@ -169,6 +169,21 @@ unset_uid:
     return retval;
 }
 
+/*
+ * Remove a file object
+ * Returns 0 on success and -errno on failure
+ */
+static int chroot_do_remove(V9fsFileObjectRequest *request)
+{
+    int retval;
+
+    retval = remove(request->path.path);
+    if (retval < 0) {
+        return -errno;
+    }
+    return 0;
+}
+
 static void chroot_daemonize(int chroot_sock)
 {
     sigset_t sigset;
@@ -272,6 +287,9 @@ int v9fs_chroot(FsContext *fs_ctx)
         case T_MKNOD:
             retval = chroot_do_create_special(&request);
             break;
+        case T_REMOVE:
+            retval = chroot_do_remove(&request);
+            break;
         default:
             retval = -1;
             break;
hw/9pfs/virtio-9p-chroot.h +1 -0
index 9075361..9d69739 100644
@@ -9,6 +9,7 @@
 #define T_MKNOD     4
 #define T_SYMLINK   5
 #define T_LINK      6
+#define T_REMOVE    7
 
 #define V9FS_FD_VALID INT_MAX
 
hw/9pfs/virtio-9p-local.c +4 -0
index a1eff5f..f2d66e1 100644
@@ -617,6 +617,10 @@ static int local_utimensat(FsContext *s, V9fsPath *fs_path,
 static int local_remove(FsContext *ctx, const char *path)
 {
     char buffer[PATH_MAX];
+
+    if (ctx->fs_sm == SM_PASSTHROUGH) {
+        return passthrough_request(ctx, NULL, path, 0, NULL, T_REMOVE);
+    }
     return remove(rpath(ctx, path, buffer));
 }
 
Signature · 1 line
1.7.5.4
#9 lore raw

PATCH v11 9/15 virtio-9p: Rename in chroot environment

In reply to M. Mohan Kumar

From: "M. Mohan Kumar" <[email protected]>

Support renaming a file or directory in chroot envirnoment. Add interfaces for renaming in chroot worker and qemu side.

---
 hw/9pfs/virtio-9p-chroot-worker.c |   17 +++++++++++++++++
 hw/9pfs/virtio-9p-chroot.h        |    1 +
 hw/9pfs/virtio-9p-local.c         |    3 +++
 3 files changed, 21 insertions(+), 0 deletions(-)
hw/9pfs/virtio-9p-chroot-worker.c +17 -0
index f269c7b..9bb94f2 100644
@@ -184,6 +184,20 @@ static int chroot_do_remove(V9fsFileObjectRequest *request)
     return 0;
 }
 
+/*
+ * Rename a file object
+ */
+static int chroot_do_rename(V9fsFileObjectRequest *request)
+{
+    int retval;
+
+    retval = rename(request->path.old_path, request->path.path);
+    if (retval < 0) {
+        return -errno;
+    }
+    return 0;
+}
+
 static void chroot_daemonize(int chroot_sock)
 {
     sigset_t sigset;
@@ -290,6 +304,9 @@ int v9fs_chroot(FsContext *fs_ctx)
         case T_REMOVE:
             retval = chroot_do_remove(&request);
             break;
+        case T_RENAME:
+            retval = chroot_do_rename(&request);
+            break;
         default:
             retval = -1;
             break;
hw/9pfs/virtio-9p-chroot.h +1 -0
index 9d69739..fda60af 100644
@@ -10,6 +10,7 @@
 #define T_SYMLINK   5
 #define T_LINK      6
 #define T_REMOVE    7
+#define T_RENAME    8
 
 #define V9FS_FD_VALID INT_MAX
 
hw/9pfs/virtio-9p-local.c +3 -0
index f2d66e1..864a464 100644
@@ -582,6 +582,9 @@ static int local_rename(FsContext *ctx, const char *oldpath,
 {
     char buffer[PATH_MAX], buffer1[PATH_MAX];
 
+    if (ctx->fs_sm == SM_PASSTHROUGH) {
+        return passthrough_request(ctx, oldpath, newpath, 0, NULL, T_RENAME);
+    }
     return rename(rpath(ctx, oldpath, buffer), rpath(ctx, newpath, buffer1));
 }
 
Signature · 1 line
1.7.5.4
#10 lore raw

PATCH v11 10/15 virtio-9p: Move file post creation changes to none security model

In reply to M. Mohan Kumar

From: "M. Mohan Kumar" <[email protected]>

After creating a file object, its permission and ownership details are updated as per 9p client's request for both passthrough and none security model. But with chrooted environment its not required for passthrough security model. Move all post file creation changes to none security model.

---
 hw/9pfs/virtio-9p-local.c |   25 +++++++++----------------
 1 files changed, 9 insertions(+), 16 deletions(-)
hw/9pfs/virtio-9p-local.c +9 -16
index 864a464..2645da0 100644
@@ -152,23 +152,16 @@ static int local_set_xattr(const char *path, FsCred *credp)
     return 0;
 }
 
-static int local_post_create_passthrough(FsContext *fs_ctx, const char *path,
-                                         FsCred *credp)
+static int local_post_create_none(const char *path, FsCred *credp)
 {
-    char buffer[PATH_MAX];
+    int retval;
 
-    if (chmod(rpath(fs_ctx, path, buffer), credp->fc_mode & 07777) < 0) {
+    if (chmod(path, credp->fc_mode & 07777) < 0) {
         return -1;
     }
-    if (lchown(rpath(fs_ctx, path, buffer), credp->fc_uid,
-                credp->fc_gid) < 0) {
-        /*
-         * If we fail to change ownership and if we are
-         * using security model none. Ignore the error
-         */
-        if (fs_ctx->fs_sm != SM_NONE) {
-            return -1;
-        }
+    retval = lchown(path, credp->fc_uid, credp->fc_gid);
+    if (retval < 0) {
+        /* Ignore return value for none security model */
     }
     return 0;
 }
@@ -333,7 +326,7 @@ static int local_mknod(FsContext *fs_ctx, V9fsPath *dir_path,
         if (err == -1) {
             goto out;
         }
-        err = local_post_create_passthrough(fs_ctx, path, credp);
+        err = local_post_create_none(buffer, credp);
         if (err == -1) {
             serrno = errno;
             goto err_end;
@@ -381,7 +374,7 @@ static int local_mkdir(FsContext *fs_ctx, V9fsPath *dir_path,
         if (err == -1) {
             goto out;
         }
-        err = local_post_create_passthrough(fs_ctx, path, credp);
+        err = local_post_create_none(buffer, credp);
         if (err == -1) {
             serrno = errno;
             goto err_end;
@@ -463,7 +456,7 @@ static int local_open2(FsContext *fs_ctx, V9fsPath *dir_path, const char *name,
             err = fd;
             goto out;
         }
-        err = local_post_create_passthrough(fs_ctx, path, credp);
+        err = local_post_create_none(buffer, credp);
         if (err == -1) {
             serrno = errno;
             goto err_end;
Signature · 1 line
1.7.5.4
#11 lore raw

PATCH v11 11/15 virtio-9p: chmod in chroot environment

In reply to M. Mohan Kumar

From: "M. Mohan Kumar" <[email protected]>

Add support to do chmod operation in chroot process.

---
 hw/9pfs/virtio-9p-chroot-worker.c |   18 ++++++++++++++++++
 hw/9pfs/virtio-9p-chroot.h        |    1 +
 hw/9pfs/virtio-9p-local.c         |    5 +++--
 3 files changed, 22 insertions(+), 2 deletions(-)
hw/9pfs/virtio-9p-chroot-worker.c +18 -0
index 9bb94f2..d297b50 100644
@@ -198,6 +198,21 @@ static int chroot_do_rename(V9fsFileObjectRequest *request)
     return 0;
 }
 
+/*
+ * Change permissions of a file object
+ * Returns 0 on success and -errno on failure
+ */
+static int chroot_do_chmod(V9fsFileObjectRequest *request)
+{
+    int retval;
+
+    retval = chmod(request->path.path, request->data.mode);
+    if (retval < 0) {
+        return -errno;
+    }
+    return 0;
+}
+
 static void chroot_daemonize(int chroot_sock)
 {
     sigset_t sigset;
@@ -307,6 +322,9 @@ int v9fs_chroot(FsContext *fs_ctx)
         case T_RENAME:
             retval = chroot_do_rename(&request);
             break;
+        case T_CHMOD:
+            retval = chroot_do_chmod(&request);
+            break;
         default:
             retval = -1;
             break;
hw/9pfs/virtio-9p-chroot.h +1 -0
index fda60af..fc7a134 100644
@@ -11,6 +11,7 @@
 #define T_LINK      6
 #define T_REMOVE    7
 #define T_RENAME    8
+#define T_CHMOD     9
 
 #define V9FS_FD_VALID INT_MAX
 
hw/9pfs/virtio-9p-local.c +3 -2
index 2645da0..2b22f5d 100644
@@ -288,9 +288,10 @@ static int local_chmod(FsContext *fs_ctx, V9fsPath *fs_path, FsCred *credp)
 
     if (fs_ctx->fs_sm == SM_MAPPED) {
         return local_set_xattr(rpath(fs_ctx, path, buffer), credp);
-    } else if ((fs_ctx->fs_sm == SM_PASSTHROUGH) ||
-               (fs_ctx->fs_sm == SM_NONE)) {
+    } else if (fs_ctx->fs_sm == SM_NONE) {
         return chmod(rpath(fs_ctx, path, buffer), credp->fc_mode);
+    } else {
+        return passthrough_request(fs_ctx, NULL, path, 0, credp, T_CHMOD);
     }
     return -1;
 }
Signature · 1 line
1.7.5.4
#12 lore raw

PATCH v11 12/15 virtio-9p: chown in chroot environment

In reply to M. Mohan Kumar

From: "M. Mohan Kumar" <[email protected]>

Add support to do chown in chroot process

---
 hw/9pfs/virtio-9p-chroot-worker.c |   18 ++++++++++++++++++
 hw/9pfs/virtio-9p-chroot.h        |    1 +
 hw/9pfs/virtio-9p-local.c         |    9 +++++----
 3 files changed, 24 insertions(+), 4 deletions(-)
hw/9pfs/virtio-9p-chroot-worker.c +18 -0
index d297b50..8ca4805 100644
@@ -213,6 +213,21 @@ static int chroot_do_chmod(V9fsFileObjectRequest *request)
     return 0;
 }
 
+/*
+ * Change ownership of a file object
+ * Returns 0 on success and -errno on failure
+ */
+static int chroot_do_chown(V9fsFileObjectRequest *request)
+{
+    int retval;
+
+    retval = lchown(request->path.path, request->data.uid, request->data.gid);
+    if (retval < 0) {
+        return -errno;
+    }
+    return 0;
+}
+
 static void chroot_daemonize(int chroot_sock)
 {
     sigset_t sigset;
@@ -325,6 +340,9 @@ int v9fs_chroot(FsContext *fs_ctx)
         case T_CHMOD:
             retval = chroot_do_chmod(&request);
             break;
+        case T_CHOWN:
+            retval = chroot_do_chown(&request);
+            break;
         default:
             retval = -1;
             break;
hw/9pfs/virtio-9p-chroot.h +1 -0
index fc7a134..07c6627 100644
@@ -12,6 +12,7 @@
 #define T_REMOVE    7
 #define T_RENAME    8
 #define T_CHMOD     9
+#define T_CHOWN     10
 
 #define V9FS_FD_VALID INT_MAX
 
hw/9pfs/virtio-9p-local.c +5 -4
index 2b22f5d..ea7d954 100644
@@ -587,16 +587,17 @@ static int local_chown(FsContext *fs_ctx, V9fsPath *fs_path, FsCred *credp)
     char buffer[PATH_MAX];
     char *path = fs_path->data;
 
-    if ((credp->fc_uid == -1 && credp->fc_gid == -1) ||
-            (fs_ctx->fs_sm == SM_PASSTHROUGH)) {
+    if (fs_ctx->fs_sm != SM_PASSTHROUGH &&
+            (credp->fc_uid == -1 && credp->fc_gid == -1)) {
         return lchown(rpath(fs_ctx, path, buffer), credp->fc_uid,
                 credp->fc_gid);
     } else if (fs_ctx->fs_sm == SM_MAPPED) {
         return local_set_xattr(rpath(fs_ctx, path, buffer), credp);
-    } else if ((fs_ctx->fs_sm == SM_PASSTHROUGH) ||
-               (fs_ctx->fs_sm == SM_NONE)) {
+    } else if (fs_ctx->fs_sm == SM_NONE) {
         return lchown(rpath(fs_ctx, path, buffer), credp->fc_uid,
                 credp->fc_gid);
+    } else if (fs_ctx->fs_sm == SM_PASSTHROUGH) {
+        return passthrough_request(fs_ctx, NULL, path, 0, credp, T_CHOWN);
     }
     return -1;
 }
Signature · 1 line
1.7.5.4
#13 lore raw

PATCH v11 13/15 virtio-9p: stat in chroot environment

In reply to M. Mohan Kumar

From: "M. Mohan Kumar" <[email protected]>

---
 hw/9pfs/virtio-9p-chroot-worker.c |   52 ++++++++++++++++++++++++++++++++-
 hw/9pfs/virtio-9p-chroot.c        |   59 ++++++++++++++++++++++++++++++++++++-
 hw/9pfs/virtio-9p-chroot.h        |    3 ++
 hw/9pfs/virtio-9p-local.c         |   30 +++++++++++++++++++
 4 files changed, 142 insertions(+), 2 deletions(-)
hw/9pfs/virtio-9p-chroot-worker.c +51 -1
index 8ca4805..c4ebb96 100644
@@ -66,6 +66,29 @@ static void chroot_sendfd(int sockfd, int fd, int fd_valid)
     }
 }
 
+/* Send special information and error status to qemu process */
+static void chroot_sendspecial(int sockfd, void *buff, int size, int error)
+{
+    int retval;
+
+    /* If there is an error, send NULL buff also set status to error */
+    if (error) {
+        memset(buff, 0, size);
+    }
+    do {
+        retval = send(sockfd, &error, sizeof(error), 0);
+    } while (retval < 0 && errno == EINTR);
+    if (retval < 0) {
+        _exit(1);
+    }
+    do {
+        retval = send(sockfd, buff, size, 0);
+    } while (retval < 0 && errno == EINTR);
+    if (retval < 0) {
+        _exit(1);
+    }
+}
+
 /* Read V9fsFileObjectRequest sent by QEMU process */
 static int chroot_read_request(int sockfd, V9fsFileObjectRequest *request)
 {
@@ -267,6 +290,7 @@ int v9fs_chroot(FsContext *fs_ctx)
     pid_t pid;
     uint32_t code;
     int retval, valid_fd;
+    char *buff;
 
     if (socketpair(PF_UNIX, SOCK_STREAM, 0, fd_pair) < 0) {
         error_report("socketpair %s", strerror(errno));
@@ -346,7 +370,33 @@ int v9fs_chroot(FsContext *fs_ctx)
         default:
             retval = -1;
             break;
+        case T_LSTAT:
+            buff = qemu_malloc(request.data.size);
+            retval = lstat(request.path.path, (struct stat *)buff);
+            if (retval  < 0) {
+                retval = -errno;
+            }
+            break;
+        }
+
+        /* Send the results */
+        switch (request.data.type) {
+        case T_OPEN:
+        case T_CREATE:
+        case T_MKDIR:
+        case T_SYMLINK:
+        case T_LINK:
+        case T_MKNOD:
+        case T_REMOVE:
+        case T_RENAME:
+        case T_CHMOD:
+        case T_CHOWN:
+            chroot_sendfd(chroot_sock, retval, valid_fd);
+            break;
+        case T_LSTAT:
+            chroot_sendspecial(chroot_sock, buff, request.data.size, retval);
+            qemu_free(buff);
+            break;
         }
-        chroot_sendfd(chroot_sock, retval, valid_fd);
     }
 }
hw/9pfs/virtio-9p-chroot.c +58 -1

74 lines not on this page · raw message

hw/9pfs/virtio-9p-chroot.h +3 -0
index 07c6627..9ed3f4d 100644
@@ -13,6 +13,7 @@
 #define T_RENAME    8
 #define T_CHMOD     9
 #define T_CHOWN     10
+#define T_LSTAT     11
 
 #define V9FS_FD_VALID INT_MAX
 
@@ -29,6 +30,7 @@ struct V9fsFileObjectData
     gid_t gid;
     dev_t dev;
     int type;
+    int size; /* for special requests */
 };
 
 struct V9fsFileObjectPath
@@ -45,6 +47,7 @@ typedef struct V9fsFileObjectRequest
 
 int v9fs_chroot(FsContext *fs_ctx);
 int v9fs_request(FsContext *fs_ctx, V9fsFileObjectRequest *or);
+int v9fs_special(FsContext *fs_ctx, V9fsFileObjectRequest *or, char *, int);
 int v9fs_create_special(FsContext *fs_ctx, V9fsFileObjectRequest *request);
 
 #endif /* _QEMU_VIRTIO_9P_CHROOT_H */
hw/9pfs/virtio-9p-local.c +30 -0
index ea7d954..eab0854 100644
@@ -81,12 +81,42 @@ static int passthrough_request(FsContext *fs_ctx, const char *old_path,
     return retval;
 }
 
+static int passthrough_special_request(FsContext *fs_ctx, const char *path,
+                char *buff, int size, int type)
+{
+    V9fsFileObjectRequest request;
+    int retval;
+
+    retval = fill_fileobjectrequest(&request, NULL, path, 0, NULL, type);
+    if (retval < 0) {
+        errno = -retval;
+        return -1;
+    }
+
+    request.data.size = size;
+    if (type == T_LSTAT) {
+        retval = v9fs_special(fs_ctx, &request, buff, size);
+    } else {
+            return -1;
+    }
+    if (retval < 0) {
+        errno = -retval;
+        retval = -1;
+    }
+    return retval;
+}
+
 static int local_lstat(FsContext *fs_ctx, V9fsPath *fs_path, struct stat *stbuf)
 {
     int err;
     char buffer[PATH_MAX];
     char *path = fs_path->data;
 
+    if (fs_ctx->fs_sm == SM_PASSTHROUGH) {
+        return passthrough_special_request(fs_ctx, path, (char *)stbuf,
+                        sizeof(*stbuf), T_LSTAT);
+    }
+
     err =  lstat(rpath(fs_ctx, path, buffer), stbuf);
     if (err) {
         return err;
Signature · 1 line
1.7.5.4
#14 lore raw

PATCH v11 14/15 virtio-9p: readlink in chroot environment

In reply to M. Mohan Kumar

From: "M. Mohan Kumar" <[email protected]>

---
 hw/9pfs/virtio-9p-chroot-worker.c |   17 ++++++++++++++---
 hw/9pfs/virtio-9p-chroot.h        |    1 +
 hw/9pfs/virtio-9p-local.c         |   14 ++++++++++++--
 3 files changed, 27 insertions(+), 5 deletions(-)
hw/9pfs/virtio-9p-chroot-worker.c +14 -3
index c4ebb96..0b87017 100644
@@ -367,9 +367,6 @@ int v9fs_chroot(FsContext *fs_ctx)
         case T_CHOWN:
             retval = chroot_do_chown(&request);
             break;
-        default:
-            retval = -1;
-            break;
         case T_LSTAT:
             buff = qemu_malloc(request.data.size);
             retval = lstat(request.path.path, (struct stat *)buff);
@@ -377,6 +374,19 @@ int v9fs_chroot(FsContext *fs_ctx)
                 retval = -errno;
             }
             break;
+        case T_READLINK:
+            buff = qemu_malloc(request.data.size);
+            retval = readlink(request.path.path, buff, request.data.size);
+            if (retval < 0) {
+                retval = -errno;
+            } else {
+                buff[retval] = '\0';
+                retval = 0;
+            }
+            break;
+        default:
+            retval = -1;
+            break;
         }
 
         /* Send the results */
@@ -394,6 +404,7 @@ int v9fs_chroot(FsContext *fs_ctx)
             chroot_sendfd(chroot_sock, retval, valid_fd);
             break;
         case T_LSTAT:
+        case T_READLINK:
             chroot_sendspecial(chroot_sock, buff, request.data.size, retval);
             qemu_free(buff);
             break;
hw/9pfs/virtio-9p-chroot.h +1 -0
index 9ed3f4d..ebf04b5 100644
@@ -14,6 +14,7 @@
 #define T_CHMOD     9
 #define T_CHOWN     10
 #define T_LSTAT     11
+#define T_READLINK  12
 
 #define V9FS_FD_VALID INT_MAX
 
hw/9pfs/virtio-9p-local.c +12 -2
index eab0854..e865cc8 100644
@@ -96,6 +96,12 @@ static int passthrough_special_request(FsContext *fs_ctx, const char *path,
     request.data.size = size;
     if (type == T_LSTAT) {
         retval = v9fs_special(fs_ctx, &request, buff, size);
+    } else if (type == T_READLINK) {
+        retval = v9fs_special(fs_ctx, &request, buff, size);
+        /* readlink needs to return the number of bytes placed in buff */
+        if (retval >= 0) {
+            retval = size;
+        }
     } else {
             return -1;
     }
@@ -203,6 +209,11 @@ static ssize_t local_readlink(FsContext *fs_ctx, V9fsPath *fs_path,
     char buffer[PATH_MAX];
     char *path = fs_path->data;
 
+    if (fs_ctx->fs_sm == SM_PASSTHROUGH) {
+        return passthrough_special_request(fs_ctx, path, buf, bufsz,
+                        T_READLINK);
+    }
+
     if (fs_ctx->fs_sm == SM_MAPPED) {
         int fd;
         fd = open(rpath(fs_ctx, path, buffer), O_RDONLY);
@@ -214,8 +225,7 @@ static ssize_t local_readlink(FsContext *fs_ctx, V9fsPath *fs_path,
         } while (tsize == -1 && errno == EINTR);
         close(fd);
         return tsize;
-    } else if ((fs_ctx->fs_sm == SM_PASSTHROUGH) ||
-               (fs_ctx->fs_sm == SM_NONE)) {
+    } else if (fs_ctx->fs_sm == SM_NONE) {
         tsize = readlink(rpath(fs_ctx, path, buffer), buf, bufsz);
     }
     return tsize;
Signature · 1 line
1.7.5.4
#15 lore raw

PATCH v11 15/15 virtio-9p: Chroot environment for other functions

In reply to M. Mohan Kumar

From: "M. Mohan Kumar" <[email protected]>

Add chroot functionality for system calls that can operate on a file using relative directory file descriptor.

---
 hw/9pfs/virtio-9p-local.c |   41 +++++++++++++++++++++++++++++++++++++++--
 1 files changed, 39 insertions(+), 2 deletions(-)
hw/9pfs/virtio-9p-local.c +39 -2
index e865cc8..5847d43 100644
@@ -608,7 +608,25 @@ static int local_truncate(FsContext *ctx, V9fsPath *fs_path, off_t size)
     char buffer[PATH_MAX];
     char *path = fs_path->data;
 
-    return truncate(rpath(ctx, path, buffer), size);
+    if (ctx->fs_sm == SM_PASSTHROUGH) {
+        int fd, retval, serrno;
+        fd = passthrough_request(ctx, NULL, path, O_RDWR, NULL, T_OPEN);
+        if (fd < 0) {
+            errno = -fd;
+            return -1;
+        }
+        retval = ftruncate(fd, size);
+        if (retval < 0) {
+            serrno = errno;
+        }
+        close(fd);
+        if (retval < 0) {
+            errno = serrno;
+        }
+        return retval;
+    } else {
+        return truncate(rpath(ctx, path, buffer), size);
+    }
 }
 
 static int local_rename(FsContext *ctx, const char *oldpath,
@@ -648,8 +666,27 @@ static int local_utimensat(FsContext *s, V9fsPath *fs_path,
     char buffer[PATH_MAX];
     char *path = fs_path->data;
 
-    return qemu_utimensat(AT_FDCWD, rpath(s, path, buffer), buf,
+    if (s->fs_sm == SM_PASSTHROUGH) {
+        int fd, retval, serrno = 0;
+        fd = passthrough_request(s, NULL, path,
+                        O_RDONLY | O_NONBLOCK | O_NOFOLLOW, NULL, T_OPEN);
+        if (fd < 0) {
+            errno = -fd;
+            return -1;
+        }
+        retval = futimens(fd, buf);
+        if (retval < 0) {
+            serrno = errno;
+        }
+        close(fd);
+        if (retval < 0) {
+            errno = serrno;
+        }
+        return retval;
+    } else {
+        return qemu_utimensat(AT_FDCWD, rpath(s, path, buffer), buf,
                           AT_SYMLINK_NOFOLLOW);
+    }
 }
 
 static int local_remove(FsContext *ctx, const char *path)
Signature · 1 line
1.7.5.4
#16 lore raw
In reply to M. Mohan Kumar
Quoted 10 lines · M. Mohan Kumar

In passthrough security model, following symbolic links in the server side could result in TOCTTOU vulnerabilities.
(http://en.wikipedia.org/wiki/Time-of-check-to-time-of-use)

This patchset resolves this issue by creating a dedicated process which chroots into the share path and all file object access is done in the chroot environment.

This patchset implements chroot enviroment, provides necessary functions that can be used by the passthrough function calls.

This could be interesting also for privilege separation. A helper process like this could access and reopen the image files etc. while the rest of QEMU could run in a jail.

Quoted 3 lines

This patchset is rebased on top of 9p coroutines patches posted to qemu-devel list
http://lists.nongnu.org/archive/html/qemu-devel/2011-05/msg02796.html

Aren't the coroutines running in same process, so don't they share the root? Also the coroutines are implemented in several OS dependent ways.

Quoted 75 lines

Changes from version V10:
* Added support to do lstat and readlink from chroot process
* Fixed an issue with dealing fds when qemu process reached maxfds limit

Changes from version V9:
* Error handling in special file object creation in virtio-9p-local.c

Changes from version V8:
* Make chmod and chown also operate under chroot process
* Check for invalid path requests, minor cleanups

Changes from version V7:
* Add two chroot methods remove and rename
* Minor cleanups like consolidating functions

Changes from version V6:
* Send only fd/errno in socket operations instead of FdInfo structure
* Minor cleanups

Changes from version V5:
* Return errno on failure instead of setting errno
* Minor cleanups like updated comments, enable CONFIG_THREAD if CONFIG_VIRTFS is enabled

Changes from version V4:
* Avoid using malloc/free inside chroot process
* Seperate chroot server and client functions

Changes from version V3
* Return EIO incase of socket read/write fail instead of exiting
* Changed data types as suggested by Blue Swirl
* Chroot process reports error through qemu process

Changes from version V2
* Treat socket IO errors as fatal, ie qemu will exit
* Split patchset based on chroot side (server) and qemu side(client) functionalities

M. Mohan Kumar (15):
 Implement qemu_read_full
 virtio-9p: Enable CONFIG_THREAD if CONFIG_VIRTFS is enabled
 virtio-9p: Provide chroot worker side interfaces
 virtio-9p: Add qemu side interfaces for chroot environment
 virtio-9p: Add support to open a file in chroot environment
 virtio-9p: Create support in chroot environment
 virtio-9p: Support for creating special files
 virtio-9p: Add support for removing file or directory
 virtio-9p: Add support to rename
 virtio-9p: Move file post creation changes to none security model
 virtio-9p: Add support for chmod
 virtio-9p: Add support for chown
 virtio-9p: Chroot environment for other functions
 virtio-9p: Add stat functionality to chroot
 virtio-9p: Add readlink support to chroot

 Makefile.objs                     |    1 +
 configure                         |    1 +
 fsdev/file-op-9p.h                |    3 + hw/9pfs/virtio-9p-chroot-worker.c |  418 +++++++++++++++++++++++++++++++++++++

 hw/9pfs/virtio-9p-chroot.c        |  174 +++++++++++++++
 hw/9pfs/virtio-9p-chroot.h        |   54 +++++
 hw/9pfs/virtio-9p-device.c        |   24 ++
 hw/9pfs/virtio-9p-local.c         |  248 ++++++++++++++++++----
 osdep.c                           |   32 +++
 qemu-common.h                     |    2 +
 10 files changed, 910 insertions(+), 47 deletions(-)

 create mode 100644 hw/9pfs/virtio-9p-chroot-worker.c create mode 100644 hw/9pfs/virtio-9p-chroot.c create mode 100644 hw/9pfs/virtio-9p-chroot.h

--
1.7.5.1

#17 lore raw
Quoted 9 lines · Blue Swirl
Quoted 8 lines · M. Mohan Kumar

In passthrough security model, following symbolic links in the server side could result in TOCTTOU vulnerabilities.
(http://en.wikipedia.org/wiki/Time-of-check-to-time-of-use)

This patchset resolves this issue by creating a dedicated process which chroots into the share path and all file object access is done in the chroot environment.

Quoted 6 lines
Quoted 2 lines

This patchset implements chroot enviroment, provides necessary functions that can be used by the passthrough function calls.

This could be interesting also for privilege separation. A helper process like this could access and reopen the image files etc. while the rest of QEMU could run in a jail.

As of now this is not generic helper, its meant for 9p export only. If needed we can add another process which can be a helper routine for qemu.

Quoted 9 lines
Quoted 4 lines

This patchset is rebased on top of 9p coroutines patches posted to qemu-devel list
http://lists.nongnu.org/archive/html/qemu-devel/2011-05/msg02796.html

Aren't the coroutines running in same process, so don't they share the root? Also the coroutines are implemented in several OS dependent ways.

Chroot is done in a forked process, so it won't affect others. I mentioned about co-routines because this patchset is rebased on top of coroutines patchset that are not yet part of mainline qemu.

Reviewed-by on the v11 series
#18 lore raw
In reply to M. Mohan Kumar
Quoted 70 lines · M. Mohan Kumar

In passthrough security model, following symbolic links in the server side could result in TOCTTOU vulnerabilities.
(http://en.wikipedia.org/wiki/Time-of-check-to-time-of-use)

This patchset resolves this issue by creating a dedicated process which chroots into the share path and all file object access is done in the chroot environment.

This patchset implements chroot enviroment, provides necessary functions that can be used by the passthrough function calls.

This patchset is rebased on top of 9p coroutines patches posted to qemu-devel list
http://lists.nongnu.org/archive/html/qemu-devel/2011-05/msg02796.html

Changes from version V10:
* Added support to do lstat and readlink from chroot process
* Fixed an issue with dealing fds when qemu process reached maxfds limit

Changes from version V9:
* Error handling in special file object creation in virtio-9p-local.c

Changes from version V8:
* Make chmod and chown also operate under chroot process
* Check for invalid path requests, minor cleanups

Changes from version V7:
* Add two chroot methods remove and rename
* Minor cleanups like consolidating functions

Changes from version V6:
* Send only fd/errno in socket operations instead of FdInfo structure
* Minor cleanups

Changes from version V5:
* Return errno on failure instead of setting errno
* Minor cleanups like updated comments, enable CONFIG_THREAD if CONFIG_VIRTFS is enabled

Changes from version V4:
* Avoid using malloc/free inside chroot process
* Seperate chroot server and client functions

Changes from version V3
* Return EIO incase of socket read/write fail instead of exiting
* Changed data types as suggested by Blue Swirl
* Chroot process reports error through qemu process

Changes from version V2
* Treat socket IO errors as fatal, ie qemu will exit
* Split patchset based on chroot side (server) and qemu side(client) functionalities

M. Mohan Kumar (15):
Implement qemu_read_full
virtio-9p: Enable CONFIG_THREAD if CONFIG_VIRTFS is enabled
virtio-9p: Provide chroot worker side interfaces
virtio-9p: Add qemu side interfaces for chroot environment
virtio-9p: Add support to open a file in chroot environment
virtio-9p: Create support in chroot environment
virtio-9p: Support for creating special files
virtio-9p: Add support for removing file or directory
virtio-9p: Add support to rename
virtio-9p: Move file post creation changes to none security model
virtio-9p: Add support for chmod
virtio-9p: Add support for chown
virtio-9p: Chroot environment for other functions
virtio-9p: Add stat functionality to chroot
virtio-9p: Add readlink support to chroot

Quoted 17 lines
 Makefile.objs                     |    1 +
 configure                         |    1 +
 fsdev/file-op-9p.h                |    3 +
 hw/9pfs/virtio-9p-chroot-worker.c |  418 +++++++++++++++++++++++++++++++++++++
 hw/9pfs/virtio-9p-chroot.c        |  174 +++++++++++++++
 hw/9pfs/virtio-9p-chroot.h        |   54 +++++
 hw/9pfs/virtio-9p-device.c        |   24 ++
 hw/9pfs/virtio-9p-local.c         |  248 ++++++++++++++++++----
 osdep.c                           |   32 +++
 qemu-common.h                     |    2 +
 10 files changed, 910 insertions(+), 47 deletions(-)
 create mode 100644 hw/9pfs/virtio-9p-chroot-worker.c
 create mode 100644 hw/9pfs/virtio-9p-chroot.c
 create mode 100644 hw/9pfs/virtio-9p-chroot.h
Signature · 1 line
1.7.5.1