Patchwork [V5,6/8] virtio-9p: Support for creating special files

login
register
mail settings
Submitter Mohan Kumar M
Date Feb. 16, 2011, 12:23 p.m.
Message ID <1297858995-24676-7-git-send-email-mohan@in.ibm.com>
Download mbox | patch
Permalink /patch/83361/
State New
Headers show

Comments

Mohan Kumar M - Feb. 16, 2011, 12:23 p.m.
Add both server and client side interfaces to create special files
(directory, device nodes, links and symbolic links)

Signed-off-by: M. Mohan Kumar <mohan@in.ibm.com>
---
 hw/9pfs/virtio-9p-chroot-clnt.c |   26 ++++++++++
 hw/9pfs/virtio-9p-chroot-sv.c   |   59 ++++++++++++++++++++++
 hw/9pfs/virtio-9p-chroot.h      |    2 +
 hw/9pfs/virtio-9p-local.c       |  104 +++++++++++++++++++++++++++------------
 4 files changed, 160 insertions(+), 31 deletions(-)
Stefan Hajnoczi - Feb. 17, 2011, 10:49 a.m.
On Wed, Feb 16, 2011 at 12:23 PM, M. Mohan Kumar <mohan@in.ibm.com> wrote:
> +    switch (type) {
> +    case T_MKNOD:
> +        request.data.type = T_MKNOD;
> +        break;
> +    case T_MKDIR:
> +        request.data.type = T_MKDIR;
> +        break;
> +    case T_SYMLINK:
> +        request.data.type = T_SYMLINK;
> +        break;
> +    case T_LINK:
> +        request.data.type = T_LINK;
> +        break;
> +    }

Or just request.data.type = type?

> +    if (oldpath) {
> +        request.data.oldpath_len = strlen(oldpath);
> +        strcpy(request.path.old_path, oldpath);

It's not obvious that this strcpy() is safe.

> +    }
> +    retval = v9fs_create_special(fs_ctx, &request, &error);
> +    if (retval < 0) {
> +        errno = error;
> +        return 0;

This looks suspicious.  Should the return value be negative on error?

> +        char *tmp = qemu_strdup(rpath(fs_ctx, oldpath));
> +        if (tmp == NULL) {

QEMU strdup never returns NULL.

Stefan
Mohan Kumar M - Feb. 18, 2011, 5:58 a.m.
On Thursday 17 February 2011 4:19:17 pm Stefan Hajnoczi wrote:
> On Wed, Feb 16, 2011 at 12:23 PM, M. Mohan Kumar <mohan@in.ibm.com> wrote:
> > +    switch (type) {
> > +    case T_MKNOD:
> > +        request.data.type = T_MKNOD;
> > +        break;
> > +    case T_MKDIR:
> > +        request.data.type = T_MKDIR;
> > +        break;
> > +    case T_SYMLINK:
> > +        request.data.type = T_SYMLINK;
> > +        break;
> > +    case T_LINK:
> > +        request.data.type = T_LINK;
> > +        break;
> > +    }
> 
> Or just request.data.type = type?
>
Oops, I will modify.
 
> > +    if (oldpath) {
> > +        request.data.oldpath_len = strlen(oldpath);
> > +        strcpy(request.path.old_path, oldpath);
> 
> It's not obvious that this strcpy() is safe.
> 

I will change the code.

> > +    }
> > +    retval = v9fs_create_special(fs_ctx, &request, &error);
> > +    if (retval < 0) {
> > +        errno = error;
> > +        return 0;
> 
> This looks suspicious.  Should the return value be negative on error?
Yes, it should return negative on error, I will update.

> 
> > +        char *tmp = qemu_strdup(rpath(fs_ctx, oldpath));
> > +        if (tmp == NULL) {
> 
> QEMU strdup never returns NULL.
Ok.
> 
> Stefan

----
M. Mohan Kumar

Patch

diff --git a/hw/9pfs/virtio-9p-chroot-clnt.c b/hw/9pfs/virtio-9p-chroot-clnt.c
index 7f72add..30366d3 100644
--- a/hw/9pfs/virtio-9p-chroot-clnt.c
+++ b/hw/9pfs/virtio-9p-chroot-clnt.c
@@ -108,3 +108,29 @@  unlock:
     qemu_mutex_unlock(&fs_ctx->chroot_mutex);
     return fd;
 }
+
+int v9fs_create_special(FsContext *fs_ctx,
+                V9fsFileObjectRequest *request, int *error)
+{
+    int fd;
+    qemu_mutex_lock(&fs_ctx->chroot_mutex);
+    if (fs_ctx->chroot_ioerror) {
+        *error = EIO;
+        fd = -1;
+        goto unlock;
+    } else {
+        *error = 0;
+    }
+    v9fs_write_request(fs_ctx->chroot_socket, request);
+    fd = v9fs_receivefd(fs_ctx->chroot_socket, error);
+    if (fd == -EIO && *error == EIO) {
+        fs_ctx->chroot_ioerror = 1;
+    }
+unlock:
+    qemu_mutex_unlock(&fs_ctx->chroot_mutex);
+    if (*error) {
+        return -1;
+    } else {
+        return 0;
+    }
+}
diff --git a/hw/9pfs/virtio-9p-chroot-sv.c b/hw/9pfs/virtio-9p-chroot-sv.c
index bab5f7c..e3d8843 100644
--- a/hw/9pfs/virtio-9p-chroot-sv.c
+++ b/hw/9pfs/virtio-9p-chroot-sv.c
@@ -123,6 +123,59 @@  unset_uid:
     setfsuid(cur_uid);
 }
 
+/*
+ * 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 on success, returns errno on failure
+ */
+static void chroot_do_create_special(V9fsFileObjectRequest *request,
+                FdInfo *fd_info)
+{
+    int cur_uid, cur_gid;
+
+    cur_uid = geteuid();
+    cur_gid = getegid();
+
+    fd_info->fi_fd = -1;
+    fd_info->fi_error = 0;
+
+    if (setfsuid(request->data.uid) < 0) {
+        fd_info->fi_error = errno;
+        return;
+    }
+    if (setfsgid(request->data.gid) < 0) {
+        fd_info->fi_error = errno;
+        goto unset_uid;
+    }
+
+    switch (request->data.type) {
+    case T_MKDIR:
+        fd_info->fi_fd = mkdir(request->path.path, request->data.mode);
+        break;
+    case T_SYMLINK:
+        fd_info->fi_fd = symlink(request->path.old_path, request->path.path);
+        break;
+    case T_LINK:
+        fd_info->fi_fd = link(request->path.old_path, request->path.path);
+        break;
+    default:
+        fd_info->fi_fd = mknod(request->path.path, request->data.mode,
+                        request->data.dev);
+        break;
+    }
+
+    if (fd_info->fi_fd < 0) {
+        fd_info->fi_error = errno;
+    } else {
+        fd_info->fi_error = 0;
+        fd_info->fi_fd = 0;
+    }
+
+    setfsgid(cur_gid);
+unset_uid:
+    setfsuid(cur_uid);
+}
+
 static int chroot_daemonize(int chroot_sock)
 {
     sigset_t sigset;
@@ -226,6 +279,12 @@  int v9fs_chroot(FsContext *fs_ctx)
         case T_CREATE:
             chroot_do_create(&request, &fd_info);
             break;
+        case T_MKDIR:
+        case T_SYMLINK:
+        case T_LINK:
+        case T_MKNOD:
+            chroot_do_create_special(&request, &fd_info);
+            break;
         default:
             fd_info.fi_fd = 0;
             fd_info.fi_error = EIO;
diff --git a/hw/9pfs/virtio-9p-chroot.h b/hw/9pfs/virtio-9p-chroot.h
index 057dcde..7ae1389 100644
--- a/hw/9pfs/virtio-9p-chroot.h
+++ b/hw/9pfs/virtio-9p-chroot.h
@@ -54,5 +54,7 @@  typedef struct V9fsFileObjectRequest
 
 int v9fs_chroot(FsContext *fs_ctx);
 int v9fs_request(FsContext *fs_ctx, V9fsFileObjectRequest *or, int *error);
+int v9fs_create_special(FsContext *fs_ctx,
+                V9fsFileObjectRequest *request, int *error);
 
 #endif /* _QEMU_VIRTIO_9P_CHROOT_H */
diff --git a/hw/9pfs/virtio-9p-local.c b/hw/9pfs/virtio-9p-local.c
index 51911dd..9975ed1 100644
--- a/hw/9pfs/virtio-9p-local.c
+++ b/hw/9pfs/virtio-9p-local.c
@@ -69,6 +69,39 @@  static int passthrough_create(FsContext *fs_ctx, const char *path, int flags,
     return fd;
 }
 
+static int passthrough_create_special(FsContext *fs_ctx, const char *oldpath,
+                const char *path, FsCred *credp, int type)
+{
+    V9fsFileObjectRequest request;
+    int retval, error = 0;
+
+    fill_request(&request, path, credp);
+    switch (type) {
+    case T_MKNOD:
+        request.data.type = T_MKNOD;
+        break;
+    case T_MKDIR:
+        request.data.type = T_MKDIR;
+        break;
+    case T_SYMLINK:
+        request.data.type = T_SYMLINK;
+        break;
+    case T_LINK:
+        request.data.type = T_LINK;
+        break;
+    }
+    if (oldpath) {
+        request.data.oldpath_len = strlen(oldpath);
+        strcpy(request.path.old_path, oldpath);
+    }
+    retval = v9fs_create_special(fs_ctx, &request, &error);
+    if (retval < 0) {
+        errno = error;
+        return 0;
+    }
+    return retval;
+}
+
 static int local_lstat(FsContext *fs_ctx, const char *path, struct stat *stbuf)
 {
     int err;
@@ -286,8 +319,7 @@  static int local_mknod(FsContext *fs_ctx, const char *path, FsCred *credp)
             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), credp->fc_mode, credp->fc_rdev);
         if (err == -1) {
             return err;
@@ -297,6 +329,12 @@  static int local_mknod(FsContext *fs_ctx, const char *path, FsCred *credp)
             serrno = errno;
             goto err_end;
         }
+    } else if (fs_ctx->fs_sm == SM_PASSTHROUGH) {
+        err = passthrough_create_special(fs_ctx, NULL, path, credp, T_MKNOD);
+        if (err < 0) {
+            serrno = errno;
+            goto err_end;
+        }
     }
     return err;
 
@@ -323,8 +361,7 @@  static int local_mkdir(FsContext *fs_ctx, const char *path, FsCred *credp)
             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), credp->fc_mode);
         if (err == -1) {
             return err;
@@ -334,6 +371,12 @@  static int local_mkdir(FsContext *fs_ctx, const char *path, FsCred *credp)
             serrno = errno;
             goto err_end;
         }
+    } else if (fs_ctx->fs_sm == SM_PASSTHROUGH) {
+        err = passthrough_create_special(fs_ctx, NULL, path, credp, T_MKDIR);
+        if (err < 0) {
+            serrno = errno;
+            goto err_end;
+        }
     }
     return err;
 
@@ -451,23 +494,19 @@  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));
         if (err) {
             return err;
         }
         err = lchown(rpath(fs_ctx, newpath), 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 if (fs_ctx->fs_sm == SM_PASSTHROUGH) {
+        err = passthrough_create_special(fs_ctx, oldpath, newpath, credp,
+                        T_SYMLINK);
+        if (err < 0) {
+            serrno = errno;
+            goto err_end;
         }
     }
     return err;
@@ -478,24 +517,27 @@  err_end:
     return err;
 }
 
-static int local_link(FsContext *ctx, const char *oldpath, const char *newpath)
+static int local_link(FsContext *fs_ctx, const char *oldpath,
+                const char *newpath)
 {
-    char *tmp = qemu_strdup(rpath(ctx, oldpath));
     int err, serrno = 0;
 
-    if (tmp == NULL) {
-        return -ENOMEM;
-    }
-
-    err = link(tmp, rpath(ctx, newpath));
-    if (err == -1) {
-        serrno = errno;
-    }
-
-    qemu_free(tmp);
-
-    if (err == -1) {
-        errno = serrno;
+    if (fs_ctx->fs_sm == SM_PASSTHROUGH) {
+        err = passthrough_create_special(fs_ctx, oldpath, newpath, NULL,
+                        T_LINK);
+        if (err < 0) {
+            serrno = errno;
+        }
+    } else {
+        char *tmp = qemu_strdup(rpath(fs_ctx, oldpath));
+        if (tmp == NULL) {
+            return -ENOMEM;
+        }
+        err = link(tmp, rpath(fs_ctx, newpath));
+        if (err == -1) {
+            serrno = errno;
+        }
+        qemu_free(tmp);
     }
 
     return err;