Skip to contentSkip to conversation

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

PATCH Superseded M. Mohan Kumar 3 participants 17 messages Mar 9, 2011 – Mar 11, 2011 Kbuild

v8 of 12 Superseded Latest is v12 →

lore
Share & tools
lore
#0 lore raw

v8 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.

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.

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 (11):
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: Chroot environment for other functions

 Makefile.objs                     |    1 +
 configure                         |    1 +
 hw/9pfs/virtio-9p-chroot-worker.c |  306 ++++++++++++++++++++++++++++++++
 hw/9pfs/virtio-9p-chroot.c        |  104 +++++++++++
 hw/9pfs/virtio-9p-chroot.h        |   47 +++++
 hw/9pfs/virtio-9p-local.c         |  355 ++++++++++++++++++++++++++++--------
 hw/9pfs/virtio-9p.c               |   25 +++
 hw/file-op-9p.h                   |    4 +
 osdep.c                           |   32 ++++
 qemu-common.h                     |    2 +
 10 files changed, 798 insertions(+), 79 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.3.4
#1 lore raw

PATCH v8 1/11 Implement qemu_read_full

Add qemu_read_full function

---
 osdep.c       |   32 ++++++++++++++++++++++++++++++++
 qemu-common.h |    2 ++
 2 files changed, 34 insertions(+), 0 deletions(-)
osdep.c +32 -0
index 327583b..8d84a88 100644
@@ -127,6 +127,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 40dad52..325b16a 100644
@@ -207,6 +207,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.3.4
#2 lore raw

PATCH v8 2/11 virtio-9p: Enable CONFIG_THREAD if CONFIG_VIRTFS is enabled

In reply to M. Mohan Kumar

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 2560357..9eddd38 100755
@@ -2767,6 +2767,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.3.4
#3 lore raw

PATCH v8 3/11 virtio-9p: Provide chroot worker side interfaces

In reply to M. Mohan Kumar

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.

---
 Makefile.objs                     |    1 +
 hw/9pfs/virtio-9p-chroot-worker.c |  183 +++++++++++++++++++++++++++++++++++++
 hw/9pfs/virtio-9p-chroot.h        |   36 +++++++
 hw/9pfs/virtio-9p.c               |   25 +++++
 hw/file-op-9p.h                   |    4 +
 5 files changed, 249 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 6a9f765..44891c1 100644
@@ -283,6 +283,7 @@ hw-obj-$(CONFIG_SOUND) += $(sound-obj-y)
 9pfs-nested-$(CONFIG_VIRTFS) = virtio-9p-debug.o
 9pfs-nested-$(CONFIG_VIRTFS) +=  virtio-9p-local.o virtio-9p-xattr.o
 9pfs-nested-$(CONFIG_VIRTFS) +=   virtio-9p-xattr-user.o virtio-9p-posix-acl.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)): CFLAGS +=  -I$(SRC_PATH)/hw/
hw/9pfs/virtio-9p-chroot-worker.c new file +183 -0

183 lines not on this page · raw message

hw/9pfs/virtio-9p-chroot.h new file +36 -0
new file mode 100644
index 0000000..c05e05b8
@@ -0,0 +1,36 @@
+#ifndef _QEMU_VIRTIO_9P_CHROOT_H
+#define _QEMU_VIRTIO_9P_CHROOT_H
+
+/* types for V9fsFileObjectRequest */
+#define T_OPEN      1
+
+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.c +25 -0
index 08c0399..4dea542 100644
@@ -14,10 +14,12 @@
 #include "virtio.h"
 #include "pc.h"
 #include "qemu_socket.h"
+#include "qerror.h"
 #include "virtio-9p.h"
 #include "fsdev/qemu-fsdev.h"
 #include "virtio-9p-debug.h"
 #include "virtio-9p-xattr.h"
+#include "virtio-9p-chroot.h"
 
 int debug_9p_pdu;
 static void v9fs_reclaim_fd(V9fsState *s);
@@ -3925,5 +3927,28 @@ VirtIODevice *virtio_9p_init(DeviceState *dev, V9fsConf *conf)
                         s->tag_len;
     s->vdev.get_config = virtio_9p_get_config;
 
+    if (s->ctx.fs_sm == SM_PASSTHROUGH) {
+        uint32_t code;
+        qemu_mutex_init(&s->ctx.chroot_mutex);
+        s->ctx.chroot_ioerror = 0;
+        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;
 }
hw/file-op-9p.h +4 -0
index 126e60e..b52b432 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
 
@@ -55,6 +56,9 @@ typedef struct FsContext
     SecModel fs_sm;
     uid_t uid;
     struct xattr_operations **xops;
+    QemuMutex chroot_mutex;
+    int chroot_socket;
+    int chroot_ioerror;
 } FsContext;
 
 void cred_init(FsCred *);
Signature · 1 line
1.7.3.4
#4 lore raw

PATCH v8 4/11 virtio-9p: Add qemu side interfaces for chroot environment

In reply to M. Mohan Kumar

QEMU side interfaces to communicate with chroot worker process.

---
 Makefile.objs              |    2 +-
 hw/9pfs/virtio-9p-chroot.c |   92 ++++++++++++++++++++++++++++++++++++++++++++
 hw/9pfs/virtio-9p-chroot.h |    1 +
 3 files changed, 94 insertions(+), 1 deletions(-)
 create mode 100644 hw/9pfs/virtio-9p-chroot.c
Makefile.objs +1 -1
index 44891c1..6610de9 100644
@@ -283,7 +283,7 @@ hw-obj-$(CONFIG_SOUND) += $(sound-obj-y)
 9pfs-nested-$(CONFIG_VIRTFS) = virtio-9p-debug.o
 9pfs-nested-$(CONFIG_VIRTFS) +=  virtio-9p-local.o virtio-9p-xattr.o
 9pfs-nested-$(CONFIG_VIRTFS) +=   virtio-9p-xattr-user.o virtio-9p-posix-acl.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)): CFLAGS +=  -I$(SRC_PATH)/hw/
hw/9pfs/virtio-9p-chroot.c new file +92 -0

92 lines not on this page · raw message

hw/9pfs/virtio-9p-chroot.h +1 -0
index c05e05b8..a218f95 100644
@@ -32,5 +32,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.3.4
#5 lore raw

PATCH v8 5/11 virtio-9p: Add support to open a file in chroot environment

In reply to M. Mohan Kumar

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 |   28 ++++++++++++-----
 hw/9pfs/virtio-9p-chroot.h |    2 +-
 hw/9pfs/virtio-9p-local.c  |   70 +++++++++++++++++++++++++++++++++++++++++--
 3 files changed, 87 insertions(+), 13 deletions(-)
hw/9pfs/virtio-9p-chroot.c +20 -8
index 4aa3b43..0c91db6 100644
@@ -80,13 +80,25 @@ 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 callers. 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_ioerror) {
+        fd = -EIO;
+        goto unlock;
+    }
+    if (v9fs_write_request(fs_ctx->chroot_socket, request) < 0) {
+        fs_ctx->chroot_ioerror = 1;
+        fd = -EIO;
+        goto unlock;
+    }
+    fd = v9fs_receivefd(fs_ctx->chroot_socket, &sock_error);
+    if (fd < 0 && sock_error) {
+        fs_ctx->chroot_ioerror = 1;
+    }
+unlock:
+    qemu_mutex_unlock(&fs_ctx->chroot_mutex);
+    return fd;
 }
hw/9pfs/virtio-9p-chroot.h +1 -1
index a218f95..ceb858d 100644
@@ -32,6 +32,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 +66 -4

92 lines not on this page · raw message

Signature · 1 line
1.7.3.4
#6 lore raw

[Qemu-devel] Re: [V8 PATCH 05/11] virtio-9p: Add support to open a file in chroot environment

Quoted 16 lines · M. Mohan Kumar
hw/9pfs/virtio-9p-chroot.c +2 -0
+/* 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_ioerror) {
+        fd = -EIO;
+        goto unlock;
+    }
+    if (v9fs_write_request(fs_ctx->chroot_socket, request) < 0) {
+        fs_ctx->chroot_ioerror = 1;
+        fd = -EIO;
+        goto unlock;
+    }

Quoted 8 lines

+    fd = v9fs_receivefd(fs_ctx->chroot_socket, &sock_error);
+    if (fd < 0 && sock_error) {
+        fs_ctx->chroot_ioerror = 1;
+    }
+unlock:
+    qemu_mutex_unlock(&fs_ctx->chroot_mutex);
+    return fd;
 }

If the socket is broken why not just close it? Right now we're keeping the file descriptor and have an additional chroot_ioerror variable to keep track of the fact that we don't want to touch the socket.

hw/9pfs/virtio-9p-local.c line 33

Quoted 8 lines
hw/9pfs/virtio-9p-local.c +8 -0
+/* Helper routine to fill V9fsFileObjectRequest structure */
+static int fill_fileobjectrequest(V9fsFileObjectRequest *request,
+                const char *path, FsCred *credp)
+{
+    if (strlen(path) >= PATH_MAX) {
+        return -ENAMETOOLONG;
+    }
+    memset(request, 0, sizeof(*request));

Perhaps remove this since request is a big struct (two PATH_MAX buffers) or is it necessary?

Quoted 8 lines

+    strcpy(request->path.path, path);
+    if (credp) {
+        request->data.mode = credp->fc_mode;
+        request->data.uid = credp->fc_uid;
+        request->data.gid = credp->fc_gid;
+        request->data.dev = credp->fc_rdev;
+    }
+    return 0;

hw/9pfs/virtio-9p-local.c line 50

Quoted 9 lines
hw/9pfs/virtio-9p-local.c +9 -0
+}
+
+static int passthrough_request(FsContext *fs_ctx, const char *old_path,
+                const char *path, int flags, FsCred *credp, int type)
+{
+    V9fsFileObjectRequest request;
+    int retval;
+
+    retval = fill_fileobjectrequest(&request, path, credp);

This function could also handle old_path, flags, and type. It seems to only fill half the request struct at the moment.

Quoted 7 lines

+    if (retval < 0) {
+        errno = -retval;
+        return -1;
+    }
+    if (old_path) {
+        if (strlen(old_path) >= PATH_MAX) {
+            errno = -ENAMETOOLONG;

errno = ENAMETOOLONG;

Stefan

#7 lore raw

PATCH v8 6/11 virtio-9p: Create support in chroot environment

In reply to M. Mohan Kumar

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 e7bc6c2..cdcb303 100644
@@ -85,6 +85,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;
@@ -174,6 +204,12 @@ int v9fs_chroot(FsContext *fs_ctx)
                 valid_fd = 1;
             }
             break;
+        case T_CREATE:
+            fd = chroot_do_create(&request);
+            if (fd >= 0) {
+                valid_fd = 1;
+            }
+            break;
         default:
             fd = -1;
             break;
hw/9pfs/virtio-9p-chroot.h +1 -0
index ceb858d..298d5b5 100644
@@ -3,6 +3,7 @@
 
 /* types for V9fsFileObjectRequest */
 #define T_OPEN      1
+#define T_CREATE    2
 
 union MsgControl {
     struct cmsghdr cmsg;
hw/9pfs/virtio-9p-local.c +3 -2
index 9ba1644..625d29c 100644
@@ -394,8 +394,7 @@ static int local_open2(FsContext *fs_ctx, const char *path, int flags,
             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), flags, credp->fc_mode);
         if (fd == -1) {
             return fd;
@@ -405,6 +404,8 @@ static int local_open2(FsContext *fs_ctx, const char *path, int flags,
             serrno = errno;
             goto err_end;
         }
+    } else if (fs_ctx->fs_sm == SM_PASSTHROUGH) {
+        fd = passthrough_request(fs_ctx, NULL, path, flags, credp, T_CREATE);
     }
     return fd;
 
Signature · 1 line
1.7.3.4
#8 lore raw

PATCH v8 7/11 virtio-9p: Support for creating special files

In reply to M. Mohan Kumar

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         |   71 +++++++++++++++++++++----------------
 3 files changed, 97 insertions(+), 31 deletions(-)
hw/9pfs/virtio-9p-chroot-worker.c +52 -0
index cdcb303..fcc0be1 100644
@@ -115,6 +115,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;
@@ -210,6 +256,12 @@ int v9fs_chroot(FsContext *fs_ctx)
                 valid_fd = 1;
             }
             break;
+        case T_MKDIR:
+        case T_SYMLINK:
+        case T_LINK:
+        case T_MKNOD:
+            fd = chroot_do_create_special(&request);
+            break;
         default:
             fd = -1;
             break;
hw/9pfs/virtio-9p-chroot.h +5 -0
index 298d5b5..b2db276 100644
@@ -4,6 +4,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
 
 union MsgControl {
     struct cmsghdr cmsg;
@@ -34,5 +38,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 +40 -31

115 lines not on this page · raw message

Signature · 1 line
1.7.3.4
#9 lore raw

PATCH v8 8/11 virtio-9p: Add support for removing file or directory

In reply to M. Mohan Kumar

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 |   34 ++++++++++++++++++++++++++--------
 hw/9pfs/virtio-9p-chroot.h        |    1 +
 hw/9pfs/virtio-9p-local.c         |    6 +++++-
 3 files changed, 32 insertions(+), 9 deletions(-)
hw/9pfs/virtio-9p-chroot-worker.c +26 -8
index fcc0be1..40e572c 100644
@@ -161,6 +161,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;
@@ -199,7 +214,7 @@ int v9fs_chroot(FsContext *fs_ctx)
     V9fsFileObjectRequest request;
     pid_t pid;
     uint32_t code;
-    int fd, valid_fd;
+    int retval, valid_fd;
 
     if (socketpair(PF_UNIX, SOCK_STREAM, 0, fd_pair) < 0) {
         error_report("socketpair %s", strerror(errno));
@@ -245,14 +260,14 @@ int v9fs_chroot(FsContext *fs_ctx)
         }
         switch (request.data.type) {
         case T_OPEN:
-            fd = chroot_do_open(&request);
-            if (fd >= 0) {
+            retval = chroot_do_open(&request);
+            if (retval >= 0) {
                 valid_fd = 1;
             }
             break;
         case T_CREATE:
-            fd = chroot_do_create(&request);
-            if (fd >= 0) {
+            retval = chroot_do_create(&request);
+            if (retval >= 0) {
                 valid_fd = 1;
             }
             break;
@@ -260,12 +275,15 @@ int v9fs_chroot(FsContext *fs_ctx)
         case T_SYMLINK:
         case T_LINK:
         case T_MKNOD:
-            fd = chroot_do_create_special(&request);
+            retval = chroot_do_create_special(&request);
+            break;
+        case T_REMOVE:
+            retval = chroot_do_remove(&request);
             break;
         default:
-            fd = -1;
+            retval = -1;
             break;
         }
-        chroot_sendfd(chroot_sock, fd, valid_fd);
+        chroot_sendfd(chroot_sock, retval, valid_fd);
     }
 }
hw/9pfs/virtio-9p-chroot.h +1 -0
index b2db276..2d7984a 100644
@@ -8,6 +8,7 @@
 #define T_MKNOD     4
 #define T_SYMLINK   5
 #define T_LINK      6
+#define T_REMOVE    7
 
 union MsgControl {
     struct cmsghdr cmsg;
hw/9pfs/virtio-9p-local.c +5 -1
index e7d39c0..cdbaf6a 100644
@@ -559,7 +559,11 @@ static int local_utimensat(FsContext *s, const char *path,
 
 static int local_remove(FsContext *ctx, const char *path)
 {
-    return remove(rpath(ctx, path));
+    if (ctx->fs_sm == SM_PASSTHROUGH) {
+        return passthrough_request(ctx, NULL, path, 0, NULL, T_REMOVE);
+    } else {
+        return remove(rpath(ctx, path));
+    }
 }
 
 static int local_fsync(FsContext *ctx, int fd, int datasync)
Signature · 1 line
1.7.3.4
#10 lore raw

PATCH v8 9/11 virtio-9p: Add support to rename

In reply to M. Mohan Kumar

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         |   26 +++++++++++++++-----------
 3 files changed, 33 insertions(+), 11 deletions(-)
hw/9pfs/virtio-9p-chroot-worker.c +17 -0
index 40e572c..353aeed 100644
@@ -176,6 +176,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;
@@ -280,6 +294,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 2d7984a..e8251b4 100644
@@ -9,6 +9,7 @@
 #define T_SYMLINK   5
 #define T_LINK      6
 #define T_REMOVE    7
+#define T_RENAME    8
 
 union MsgControl {
     struct cmsghdr cmsg;
hw/9pfs/virtio-9p-local.c +15 -11
index cdbaf6a..cf4792b 100644
@@ -519,22 +519,26 @@ static int local_truncate(FsContext *ctx, const char *path, off_t size)
 static int local_rename(FsContext *ctx, const char *oldpath,
                         const char *newpath)
 {
-    char *tmp;
-    int err;
-
-    tmp = qemu_strdup(rpath(ctx, oldpath));
+    int err, serrno;
 
-    err = rename(tmp, rpath(ctx, newpath));
-    if (err == -1) {
-        int serrno = errno;
-        qemu_free(tmp);
-        errno = serrno;
+    if (ctx->fs_sm == SM_PASSTHROUGH) {
+        err = passthrough_request(ctx, oldpath, newpath, 0, NULL, T_RENAME);
+        if (err == -1) {
+            serrno = errno;
+        }
     } else {
+        char *tmp;
+        tmp = qemu_strdup(rpath(ctx, oldpath));
+        err = rename(tmp, rpath(ctx, newpath));
+        if (err == -1) {
+            serrno = errno;
+        }
         qemu_free(tmp);
     }
-
+    if (err == -1) {
+        errno = serrno;
+    }
     return err;
-
 }
 
 static int local_chown(FsContext *fs_ctx, const char *path, FsCred *credp)
Signature · 1 line
1.7.3.4
#11 lore raw

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

In reply to M. Mohan Kumar

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 |   19 ++++++-------------
 1 files changed, 6 insertions(+), 13 deletions(-)
hw/9pfs/virtio-9p-local.c +6 -13
index cf4792b..1670e33 100644
@@ -137,21 +137,14 @@ static int local_set_xattr(const char *path, FsCred *credp)
     return 0;
 }
 
-static int local_post_create_passthrough(FsContext *fs_ctx, const char *path,
+static int local_post_create_none(FsContext *fs_ctx, const char *path,
         FsCred *credp)
 {
+    int retval;
     if (chmod(rpath(fs_ctx, path), credp->fc_mode & 07777) < 0) {
         return -1;
     }
-    if (lchown(rpath(fs_ctx, path), 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(rpath(fs_ctx, path), credp->fc_uid, credp->fc_gid);
     return 0;
 }
 
@@ -292,7 +285,7 @@ static int local_mknod(FsContext *fs_ctx, const char *path, FsCred *credp)
         if (err == -1) {
             return err;
         }
-        err = local_post_create_passthrough(fs_ctx, path, credp);
+        err = local_post_create_none(fs_ctx, path, credp);
         if (err == -1) {
             serrno = errno;
             goto err_end;
@@ -334,7 +327,7 @@ static int local_mkdir(FsContext *fs_ctx, const char *path, FsCred *credp)
         if (err == -1) {
             return err;
         }
-        err = local_post_create_passthrough(fs_ctx, path, credp);
+        err = local_post_create_none(fs_ctx, path, credp);
         if (err == -1) {
             serrno = errno;
             goto err_end;
@@ -409,7 +402,7 @@ static int local_open2(FsContext *fs_ctx, const char *path, int flags,
         if (fd == -1) {
             return fd;
         }
-        err = local_post_create_passthrough(fs_ctx, path, credp);
+        err = local_post_create_none(fs_ctx, path, credp);
         if (err == -1) {
             serrno = errno;
             goto err_end;
Signature · 1 line
1.7.3.4
#12 lore raw

PATCH v8 11/11 virtio-9p: Chroot environment for other functions

In reply to M. Mohan Kumar

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

---
 hw/9pfs/virtio-9p-local.c |  156 ++++++++++++++++++++++++++++++++++++++++-----
 1 files changed, 139 insertions(+), 17 deletions(-)
hw/9pfs/virtio-9p-local.c +139 -17
index 1670e33..0157eb8 100644
@@ -22,6 +22,7 @@
 #include <sys/socket.h>
 #include <sys/un.h>
 #include <attr/xattr.h>
+#include <libgen.h>
 
 /* Helper routine to fill V9fsFileObjectRequest structure */
 static int fill_fileobjectrequest(V9fsFileObjectRequest *request,
@@ -70,14 +71,41 @@ static int passthrough_request(FsContext *fs_ctx, const char *old_path,
     return retval;
 }
 
+/*
+ * Returns file descriptor of dirname(path)
+ * This fd can be used by *at functions
+ */
+static int get_dirfd(FsContext *fs_ctx, const char *path)
+{
+    V9fsFileObjectRequest request;
+    int fd;
+    char *dpath = qemu_strdup(path);
+
+    fd = fill_fileobjectrequest(&request, dirname(dpath), NULL);
+    if (fd < 0) {
+        qemu_free(dpath);
+        errno = -fd;
+        return -1;
+    }
+    request.data.type = T_OPEN;
+    fd = v9fs_request(fs_ctx, &request);
+    qemu_free(dpath);
+    if (fd < 0) {
+        errno = -fd;
+        fd = -1;
+    }
+    return fd;
+}
+
 static int local_lstat(FsContext *fs_ctx, const char *path, struct stat *stbuf)
 {
     int err;
-    err =  lstat(rpath(fs_ctx, path), stbuf);
-    if (err) {
-        return err;
-    }
+
     if (fs_ctx->fs_sm == SM_MAPPED) {
+        err =  lstat(rpath(fs_ctx, path), stbuf);
+        if (err) {
+            return err;
+        }
         /* Actual credentials are part of extended attrs */
         uid_t tmp_uid;
         gid_t tmp_gid;
@@ -99,6 +127,27 @@ static int local_lstat(FsContext *fs_ctx, const char *path, struct stat *stbuf)
                         sizeof(dev_t)) > 0) {
                 stbuf->st_rdev = tmp_dev;
         }
+    } else if (fs_ctx->fs_sm == SM_PASSTHROUGH) {
+        int pfd, serrno = 0;
+        char *tmp_path;
+
+        pfd = get_dirfd(fs_ctx, path);
+        if (pfd < 0) {
+            return -1;
+        }
+        tmp_path = qemu_strdup(path);
+        err = fstatat(pfd, basename(tmp_path), stbuf, AT_SYMLINK_NOFOLLOW);
+        if (err < 0) {
+            serrno = errno;
+        }
+        close(pfd);
+        qemu_free(tmp_path);
+        errno = serrno;
+    } else {
+        err =  lstat(rpath(fs_ctx, path), stbuf);
+        if (err) {
+            return err;
+        }
     }
     return err;
 }
@@ -163,9 +212,23 @@ static ssize_t local_readlink(FsContext *fs_ctx, const char *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), buf, bufsz);
+    } else if (fs_ctx->fs_sm == SM_PASSTHROUGH) {
+        int pfd, serrno = 0;
+        char *tmp_path;
+        pfd = get_dirfd(fs_ctx, path);
+        if (pfd < 0) {
+            return -1;
+        }
+        tmp_path = qemu_strdup(path);
+        tsize = readlinkat(pfd, basename(tmp_path), buf, bufsz);
+        if (tsize < 0) {
+            serrno = errno;
+        }
+        close(pfd);
+        qemu_free(tmp_path);
+        errno = serrno;
     }
     return tsize;
 }
@@ -257,8 +320,23 @@ static int local_chmod(FsContext *fs_ctx, const char *path, FsCred *credp)
 {
     if (fs_ctx->fs_sm == SM_MAPPED) {
         return local_set_xattr(rpath(fs_ctx, path), credp);
-    } else if ((fs_ctx->fs_sm == SM_PASSTHROUGH) ||
-               (fs_ctx->fs_sm == SM_NONE)) {
+    } else if (fs_ctx->fs_sm == SM_PASSTHROUGH) {
+        int pfd, err, serrno = 0;
+        char *tmp_path;
+        pfd = get_dirfd(fs_ctx, path);
+        if (pfd < 0) {
+            return -1;
+        }
+        tmp_path = qemu_strdup(path);
+        err = fchmodat(pfd, basename(tmp_path), credp->fc_mode, 0);
+        if (err == -1) {
+            serrno = errno;
+        }
+        qemu_free(tmp_path);
+        close(pfd);
+        errno = serrno;
+        return err;
+    } else if (fs_ctx->fs_sm == SM_NONE) {
         return chmod(rpath(fs_ctx, path), credp->fc_mode);
     }
     return -1;
@@ -506,7 +584,21 @@ static int local_link(FsContext *fs_ctx, const char *oldpath,
 
 static int local_truncate(FsContext *ctx, const char *path, off_t size)
 {
-    return truncate(rpath(ctx, path), 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);
+        serrno = errno;
+        close(fd);
+        errno = serrno;
+        return retval;
+    } else {
+        return truncate(rpath(ctx, path), size);
+    }
 }
 
 static int local_rename(FsContext *ctx, const char *oldpath,
@@ -536,22 +628,52 @@ static int local_rename(FsContext *ctx, const char *oldpath,
 
 static int local_chown(FsContext *fs_ctx, const char *path, FsCred *credp)
 {
-    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), credp->fc_uid, credp->fc_gid);
+    } else if (fs_ctx->fs_sm == SM_NONE) {
         return lchown(rpath(fs_ctx, path), credp->fc_uid, credp->fc_gid);
     } else if (fs_ctx->fs_sm == SM_MAPPED) {
         return local_set_xattr(rpath(fs_ctx, path), credp);
-    } else if ((fs_ctx->fs_sm == SM_PASSTHROUGH) ||
-               (fs_ctx->fs_sm == SM_NONE)) {
-        return lchown(rpath(fs_ctx, path), credp->fc_uid, credp->fc_gid);
+    } else if (fs_ctx->fs_sm == SM_PASSTHROUGH) {
+        int pfd, err, serrno = 0;
+        char *old_path;
+        pfd = get_dirfd(fs_ctx, path);
+        if (pfd < 0) {
+            return -1;
+        }
+        old_path = qemu_strdup(path);
+        err = fchownat(pfd, basename(old_path), credp->fc_uid, credp->fc_gid,
+                        AT_SYMLINK_NOFOLLOW);
+        if (err == -1) {
+            serrno = errno;
+        }
+        qemu_free(old_path);
+        close(pfd);
+        errno = serrno;
+        return err;
     }
     return -1;
 }
 
-static int local_utimensat(FsContext *s, const char *path,
-                           const struct timespec *buf)
+static int local_utimensat(FsContext *fs_ctx, const char *path,
+                const struct timespec *buf)
 {
-    return qemu_utimensat(AT_FDCWD, rpath(s, path), buf, AT_SYMLINK_NOFOLLOW);
+    if (fs_ctx->fs_sm == SM_PASSTHROUGH) {
+        int fd, retval;
+        fd = passthrough_request(fs_ctx, NULL, path,
+                        O_RDONLY | O_NONBLOCK | O_NOFOLLOW, NULL, T_OPEN);
+        if (fd < 0) {
+            errno = -fd;
+            return -1;
+        }
+        retval = futimens(fd, buf);
+        close(fd);
+        return retval;
+    } else {
+        return utimensat(AT_FDCWD, rpath(fs_ctx, path), buf,
+                        AT_SYMLINK_NOFOLLOW);
+    }
 }
 
 static int local_remove(FsContext *ctx, const char *path)
Signature · 1 line
1.7.3.4
#13 lore raw

[Qemu-devel] Re: [V8 PATCH 11/11] virtio-9p: Chroot environment for other functions

Quoted 2 lines · M. Mohan Kumar

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

I suspect the relative directory approach is broken and escapes the chroot. Here's why:

The request is local_chmod(fs_ctx, "/..", credp). dirname("/..") is "/" and basename("..") is "..".

I'm not 100% sure of the semantics but I suspect that chmodat(dir_fd, "..", ...) does not honor the chroot since your current task is not inside the chroot. If so, then you can manipulate the parent directory of the chroot using some of the operations added in this patch.

The safe solution is to perform all operations inside the chroot. This will require extending the chroot socket protocol.

Stefan

#14 lore raw

Re: [Qemu-devel] Re: [V8 PATCH 11/11] virtio-9p: Chroot environment for other functions

Quoted 9 lines · Stefan Hajnoczi
Quoted 2 lines · M. Mohan Kumar

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

I suspect the relative directory approach is broken and escapes the chroot. Here's why:

The request is local_chmod(fs_ctx, "/..", credp). dirname("/..") is "/" and basename("..") is "..".

We should never receive protocol operations with relative path. Client should always resolve to full path and send the request. If the client is malicious this scenario can be be possible.. but in that case it is fine to fail the operation.

Thanks,
JV

Quoted 11 lines

I'm not 100% sure of the semantics but I suspect that chmodat(dir_fd, "..", ...) does not honor the chroot since your current task is not inside the chroot. If so, then you can manipulate the parent directory of the chroot using some of the operations added in this patch.

The safe solution is to perform all operations inside the chroot. This will require extending the chroot socket protocol.

Stefan

#15 lore raw

Re: [Qemu-devel] Re: [V8 PATCH 11/11] virtio-9p: Chroot environment for other functions

Quoted 8 lines · Venkateswararao Jujjuri (JV)
Quoted 7 lines · Stefan Hajnoczi
Quoted 2 lines · M. Mohan Kumar

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

I suspect the relative directory approach is broken and escapes the chroot.  Here's why:

Quoted 7 lines
Quoted 2 lines

The request is local_chmod(fs_ctx, "/..", credp).  dirname("/..") is "/" and basename("..") is "..".

We should never receive protocol operations with relative path. Client should always resolve to full path and send the request. If the client is malicious this scenario can be be possible.. but in that case it is fine to fail the operation.

What I haven't audited yet is whether symlinks can be abused in any of these *at(2) operations.

The *at(2) approach seems like a shortcut to avoid implementing individual chroot protocol requests/responses for stat(2) and friends.
But it carries the risk that if we don't use NOFOLLOW then we can be
tricked into escaping the "chroot" because we're performing the operation outside the chroot.

I'll take a look later today to make sure all operations safe traverse paths outside the chroot.

Stefan

#16 lore raw

Re: [Qemu-devel] Re: [V8 PATCH 11/11] virtio-9p: Chroot environment for other functions

Quoted 13 lines · Stefan Hajnoczi
Quoted 11 lines · Venkateswararao Jujjuri (JV)
Quoted 9 lines · Stefan Hajnoczi
Quoted 2 lines · M. Mohan Kumar

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

I suspect the relative directory approach is broken and escapes the chroot. Here's why:

The request is local_chmod(fs_ctx, "/..", credp). dirname("/..") is "/" and basename("..") is "..".

Quoted 7 lines
Quoted 4 lines

We should never receive protocol operations with relative path. Client should always resolve to full path and send the request. If the client is malicious this scenario can be be possible.. but in that case it is fine to fail the operation.

What I haven't audited yet is whether symlinks can be abused in any of these *at(2) operations.

Reading symlink sends only contents to the client. So a symlink can contain anything.
But when the fully populated path comes we avoid the potential symlink issue by opening
the entire dir in chrooted process.

Quoted 6 lines

The *at(2) approach seems like a shortcut to avoid implementing individual chroot protocol requests/responses for stat(2) and friends.
But it carries the risk that if we don't use NOFOLLOW then we can be
tricked into escaping the "chroot" because we're performing the operation outside the chroot.

I would agree with your observation. With *at(2) we need the following:

1. The path should never have ".." May be we can check it early enough and fail.
2. Get the pfd from the chroot_thread
3. use NO_FOLLOW.

If we do all three then we are fool prof.

Quoted 3 lines

I'll take a look later today to make sure all operations safe traverse paths outside the chroot.

If we move all the operations to chroot, we are essentially serializing all operations.
But the code looks lot cleaner though.

- JV

Quoted 3 lines

Stefan