From patchwork Wed Dec 14 13:20:25 2011 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Mohan Kumar M X-Patchwork-Id: 131375 Return-Path: X-Original-To: incoming@patchwork.ozlabs.org Delivered-To: patchwork-incoming@bilbo.ozlabs.org Received: from lists.gnu.org (lists.gnu.org [140.186.70.17]) (using TLSv1 with cipher AES256-SHA (256/256 bits)) (Client did not present a certificate) by ozlabs.org (Postfix) with ESMTPS id 9CCED1007D6 for ; Thu, 15 Dec 2011 00:22:27 +1100 (EST) Received: from localhost ([::1]:36283 helo=lists.gnu.org) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1Raomt-00028A-8H for incoming@patchwork.ozlabs.org; Wed, 14 Dec 2011 08:22:23 -0500 Received: from eggs.gnu.org ([140.186.70.92]:59791) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1Raolt-0000In-Q4 for qemu-devel@nongnu.org; Wed, 14 Dec 2011 08:21:23 -0500 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1Raoln-0003sT-U3 for qemu-devel@nongnu.org; Wed, 14 Dec 2011 08:21:21 -0500 Received: from e23smtp04.au.ibm.com ([202.81.31.146]:39188) by eggs.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1Raolm-0003ro-Lf for qemu-devel@nongnu.org; Wed, 14 Dec 2011 08:21:15 -0500 Received: from /spool/local by e23smtp04.au.ibm.com with IBM ESMTP SMTP Gateway: Authorized Use Only! Violators will be prosecuted for from ; Wed, 14 Dec 2011 13:08:11 +1000 Received: from d23relay04.au.ibm.com ([202.81.31.246]) by e23smtp04.au.ibm.com ([202.81.31.210]) with IBM ESMTP SMTP Gateway: Authorized Use Only! Violators will be prosecuted; Wed, 14 Dec 2011 13:07:45 +1000 Received: from d23av04.au.ibm.com (d23av04.au.ibm.com [9.190.235.139]) by d23relay04.au.ibm.com (8.13.8/8.13.8/NCO v10.0) with ESMTP id pBEDH08g1532082 for ; Thu, 15 Dec 2011 00:17:00 +1100 Received: from d23av04.au.ibm.com (loopback [127.0.0.1]) by d23av04.au.ibm.com (8.14.4/8.13.1/NCO v10.0 AVout) with ESMTP id pBEDKirp028784 for ; Thu, 15 Dec 2011 00:20:45 +1100 Received: from explorer.in.ibm.com (explorer.in.ibm.com [9.124.35.162]) by d23av04.au.ibm.com (8.14.4/8.13.1/NCO v10.0 AVin) with ESMTP id pBEDKY9O028542; Thu, 15 Dec 2011 00:20:43 +1100 From: "M. Mohan Kumar" To: qemu-devel@nongnu.org, aneesh.kumar@linux.vnet.ibm.com, stefanha@gmail.com Date: Wed, 14 Dec 2011 18:50:25 +0530 Message-Id: <1323868833-541-7-git-send-email-mohan@in.ibm.com> X-Mailer: git-send-email 1.7.6 In-Reply-To: <1323868833-541-1-git-send-email-mohan@in.ibm.com> References: <1323868833-541-1-git-send-email-mohan@in.ibm.com> x-cbid: 11121403-9264-0000-0000-000000665DCB X-detected-operating-system: by eggs.gnu.org: Genre and OS details not recognized. X-Received-From: 202.81.31.146 Cc: "M. Mohan Kumar" Subject: [Qemu-devel] [PATCH V5 06/14] hw/9pfs: Open and create files X-BeenThere: qemu-devel@nongnu.org X-Mailman-Version: 2.1.14 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: qemu-devel-bounces+incoming=patchwork.ozlabs.org@nongnu.org Sender: qemu-devel-bounces+incoming=patchwork.ozlabs.org@nongnu.org From: "M. Mohan Kumar" Add interfaces to open and create files for proxy file system driver. Signed-off-by: M. Mohan Kumar --- fsdev/virtfs-proxy-helper.c | 178 ++++++++++++++++++++++++++++++++++++++++- hw/9pfs/virtio-9p-proxy.c | 187 +++++++++++++++++++++++++++++++++++++++++-- hw/9pfs/virtio-9p-proxy.h | 11 +++ 3 files changed, 367 insertions(+), 9 deletions(-) diff --git a/fsdev/virtfs-proxy-helper.c b/fsdev/virtfs-proxy-helper.c index 5fc2fc4..c0935e3 100644 --- a/fsdev/virtfs-proxy-helper.c +++ b/fsdev/virtfs-proxy-helper.c @@ -9,6 +9,7 @@ * the COPYING file in the top-level directory. */ #include +#include #include #include #include @@ -27,6 +28,7 @@ #include "qemu-common.h" #include "virtio-9p-marshal.h" #include "hw/9pfs/virtio-9p-proxy.h" +#include "fsdev/virtio-9p-marshal.h" #define PROGNAME "virtfs-proxy-helper" @@ -187,6 +189,139 @@ static int read_request(int sockfd, struct iovec *iovec, ProxyHeader *header) return 0; } +static int send_fd(int sockfd, int fd) +{ + struct msghdr msg; + struct iovec iov; + int retval, data; + struct cmsghdr *cmsg; + union MsgControl msg_control; + + iov.iov_base = &data; + iov.iov_len = sizeof(data); + + memset(&msg, 0, sizeof(msg)); + msg.msg_iov = &iov; + msg.msg_iovlen = 1; + /* No ancillary data on error */ + if (fd < 0) { + /* fd is really negative errno if the request failed */ + data = fd; + } else { + data = V9FS_FD_VALID; + msg.msg_control = &msg_control; + msg.msg_controllen = sizeof(msg_control); + + cmsg = &msg_control.cmsg; + cmsg->cmsg_len = CMSG_LEN(sizeof(fd)); + cmsg->cmsg_level = SOL_SOCKET; + cmsg->cmsg_type = SCM_RIGHTS; + memcpy(CMSG_DATA(cmsg), &fd, sizeof(fd)); + } + + do { + retval = sendmsg(sockfd, &msg, 0); + } while (retval < 0 && errno == EINTR); + if (fd >= 0) { + close(fd); + } + if (retval < 0) { + return retval; + } + return 0; +} + +/* + * from man 7 capabilities, section + * Effect of User ID Changes on Capabilities: + * 4. If the file system user ID is changed from 0 to nonzero (see setfsuid(2)) + * then the following capabilities are cleared from the effective set: + * CAP_CHOWN, CAP_DAC_OVERRIDE, CAP_DAC_READ_SEARCH, CAP_FOWNER, CAP_FSETID, + * CAP_LINUX_IMMUTABLE (since Linux 2.2.30), CAP_MAC_OVERRIDE, and CAP_MKNOD + * (since Linux 2.2.30). If the file system UID is changed from nonzero to 0, + * then any of these capabilities that are enabled in the permitted set + * are enabled in the effective set. + */ +static int setfsugid(int uid, int gid) +{ + /* + * We still need DAC_OVERRIDE because we don't change + * supplementary group ids, and hence may be subjected DAC rules + */ + cap_value_t cap_list[] = { + CAP_DAC_OVERRIDE, + }; + + setfsgid(gid); + setfsuid(uid); + + if (uid != 0 || gid != 0) { + return do_cap_set(cap_list, ARRAY_SIZE(cap_list), 0); + } + return 0; +} + +/* + * create a file and send fd on success + * return -errno on error + */ +static int do_create(struct iovec *iovec) +{ + int ret; + V9fsString path; + int flags, mode, uid, gid, cur_uid, cur_gid; + + v9fs_string_init(&path); + ret = proxy_unmarshal(iovec, PROXY_HDR_SZ, "sdddd", + &path, &flags, &mode, &uid, &gid); + if (ret < 0) { + goto err_out; + } + cur_uid = geteuid(); + cur_gid = getegid(); + ret = setfsugid(uid, gid); + if (ret < 0) { + /* + * On failure reset back to the + * old uid/gid + */ + ret = -errno; + goto err_out; + } + ret = open(path.data, flags, mode); + if (ret < 0) { + ret = -errno; + } + +err_out: + setfsugid(cur_uid, cur_gid); + v9fs_string_free(&path); + return ret; +} + +/* + * open a file and send fd on success + * return -errno on error + */ +static int do_open(struct iovec *iovec) +{ + int flags, ret; + V9fsString path; + + v9fs_string_init(&path); + ret = proxy_unmarshal(iovec, PROXY_HDR_SZ, "sd", &path, &flags); + if (ret < 0) { + goto err_out; + } + ret = open(path.data, flags); + if (ret < 0) { + ret = -errno; + } +err_out: + v9fs_string_free(&path); + return ret; +} + static void usage(char *prog) { fprintf(stderr, "usage: %s\n" @@ -196,22 +331,59 @@ static void usage(char *prog) basename(prog)); } +static int process_reply(int sock, int type, int retval) +{ + switch (type) { + case T_OPEN: + case T_CREATE: + if (send_fd(sock, retval) < 0) { + return -1; + } + break; + default: + return -1; + break; + } + return 0; +} + static int process_requests(int sock) { - int retval; + int retval = 0; ProxyHeader header; struct iovec in_iovec; in_iovec.iov_base = g_malloc(PROXY_MAX_IO_SZ + PROXY_HDR_SZ); in_iovec.iov_len = PROXY_MAX_IO_SZ + PROXY_HDR_SZ; while (1) { + /* + * initialize the header type, so that we send + * response to proper request type. + */ + header.type = 0; retval = read_request(sock, &in_iovec, &header); if (retval < 0) { - goto error; + goto err_out; + } + + switch (header.type) { + case T_OPEN: + retval = do_open(&in_iovec); + break; + case T_CREATE: + retval = do_create(&in_iovec); + break; + default: + goto err_out; + break; + } + + if (process_reply(sock, header.type, retval) < 0) { + goto err_out; } } (void)socket_write; -error: +err_out: g_free(in_iovec.iov_base); return -1; } diff --git a/hw/9pfs/virtio-9p-proxy.c b/hw/9pfs/virtio-9p-proxy.c index 3e4dc68..b4accd4 100644 --- a/hw/9pfs/virtio-9p-proxy.c +++ b/hw/9pfs/virtio-9p-proxy.c @@ -22,6 +22,153 @@ typedef struct V9fsProxy { struct iovec iovec; } V9fsProxy; +/* + * Return received file descriptor on success in *status. + * errno is also returned on *status (which will be < 0) + * return < 0 on transport error. + */ +static int v9fs_receivefd(int sockfd, int *status) +{ + struct iovec iov; + struct msghdr msg; + struct cmsghdr *cmsg; + int retval, data, fd; + union MsgControl msg_control; + + iov.iov_base = &data; + iov.iov_len = sizeof(data); + + memset(&msg, 0, sizeof(msg)); + msg.msg_iov = &iov; + msg.msg_iovlen = 1; + msg.msg_control = &msg_control; + msg.msg_controllen = sizeof(msg_control); + + do { + retval = recvmsg(sockfd, &msg, 0); + } while (retval < 0 && errno == EINTR); + if (retval <= 0) { + return retval; + } + /* + * data is set to V9FS_FD_VALID, if ancillary data is sent. If this + * request doesn't need ancillary data (fd) or an error occurred, + * data is set to negative errno value. + */ + if (data != V9FS_FD_VALID) { + *status = data; + return 0; + } + /* + * File descriptor (fd) is sent in the ancillary data. Check if we + * indeed received it. One of the reasons to fail to receive it is if + * we exceeded the maximum number of file descriptors! + */ + for (cmsg = CMSG_FIRSTHDR(&msg); cmsg; cmsg = CMSG_NXTHDR(&msg, cmsg)) { + if (cmsg->cmsg_len != CMSG_LEN(sizeof(int)) || + cmsg->cmsg_level != SOL_SOCKET || + cmsg->cmsg_type != SCM_RIGHTS) { + continue; + } + fd = *((int *)CMSG_DATA(cmsg)); + *status = fd; + return 0; + } + *status = -ENFILE; /* Ancillary data sent but not received */ + return 0; +} + +/* + * Proxy->header and proxy->request written to socket by QEMU process. + * This request read by proxy helper process + * returns 0 on success and -errno on error + */ +static int v9fs_request(V9fsProxy *proxy, int type, + void *response, const char *fmt, ...) +{ + va_list ap; + int retval = 0; + V9fsString *path; + ProxyHeader header; + struct iovec *iovec = NULL; + int flags, mode, uid, gid; + + qemu_mutex_lock(&proxy->mutex); + + if (proxy->sockfd == -1) { + retval = -EIO; + goto err_out; + } + iovec = &proxy->iovec; + + va_start(ap, fmt); + switch (type) { + case T_OPEN: + path = va_arg(ap, V9fsString *); + flags = va_arg(ap, int); + retval = proxy_marshal(iovec, PROXY_HDR_SZ, "sd", path, flags); + if (retval > 0) { + header.size = retval; + header.type = T_OPEN; + } + break; + case T_CREATE: + path = va_arg(ap, V9fsString *); + flags = va_arg(ap, int); + mode = va_arg(ap, int); + uid = va_arg(ap, int); + gid = va_arg(ap, int); + retval = proxy_marshal(iovec, PROXY_HDR_SZ, "sdddd", path, + flags, mode, uid, gid); + if (retval > 0) { + header.size = retval; + header.type = T_CREATE; + } + break; + default: + error_report("Invalid type %d\n", type); + retval = -EINVAL; + break; + } + va_end(ap); + + if (retval < 0) { + goto err_out; + } + + /* marshal the header details */ + proxy_marshal(iovec, 0, "dd", header.type, header.size); + header.size += PROXY_HDR_SZ; + + retval = qemu_write_full(proxy->sockfd, iovec->iov_base, header.size); + if (retval != header.size) { + goto close_error; + } + + switch (type) { + case T_OPEN: + case T_CREATE: + /* + * A file descriptor is returned as response for + * T_OPEN,T_CREATE on success + */ + if (v9fs_receivefd(proxy->sockfd, &retval) < 0) { + goto close_error; + } + break; + } + +err_out: + qemu_mutex_unlock(&proxy->mutex); + return retval; + +close_error: + close(proxy->sockfd); + proxy->sockfd = -1; + qemu_mutex_unlock(&proxy->mutex); + return -EIO; +} + static int proxy_lstat(FsContext *fs_ctx, V9fsPath *fs_path, struct stat *stbuf) { errno = EOPNOTSUPP; @@ -48,16 +195,33 @@ static int proxy_closedir(FsContext *ctx, V9fsFidOpenState *fs) static int proxy_open(FsContext *ctx, V9fsPath *fs_path, int flags, V9fsFidOpenState *fs) { - fs->fd = -1; + fs->fd = v9fs_request(ctx->private, T_OPEN, NULL, "sd", fs_path, flags); + if (fs->fd < 0) { + errno = -fs->fd; + fs->fd = -1; + } return fs->fd; } static int proxy_opendir(FsContext *ctx, V9fsPath *fs_path, V9fsFidOpenState *fs) { + int serrno, fd; + fs->dir = NULL; - errno = EOPNOTSUPP; - return -1; + fd = v9fs_request(ctx->private, T_OPEN, NULL, "sd", fs_path, O_DIRECTORY); + if (fd < 0) { + errno = -fd; + return -1; + } + fs->dir = fdopendir(fd); + if (!fs->dir) { + serrno = errno; + close(fd); + errno = serrno; + return -1; + } + return 0; } static void proxy_rewinddir(FsContext *ctx, V9fsFidOpenState *fs) @@ -164,9 +328,20 @@ static int proxy_fstat(FsContext *fs_ctx, int fid_type, static int proxy_open2(FsContext *fs_ctx, V9fsPath *dir_path, const char *name, int flags, FsCred *credp, V9fsFidOpenState *fs) { - fs->fd = -1; - errno = EOPNOTSUPP; - return -1; + V9fsString fullname; + + v9fs_string_init(&fullname); + v9fs_string_sprintf(&fullname, "%s/%s", dir_path->data, name); + + fs->fd = v9fs_request(fs_ctx->private, T_CREATE, NULL, "sdddd", + &fullname, flags, credp->fc_mode, + credp->fc_uid, credp->fc_gid); + v9fs_string_free(&fullname); + if (fs->fd < 0) { + errno = -fs->fd; + fs->fd = -1; + } + return fs->fd; } diff --git a/hw/9pfs/virtio-9p-proxy.h b/hw/9pfs/virtio-9p-proxy.h index 97c5574..6172e5f 100644 --- a/hw/9pfs/virtio-9p-proxy.h +++ b/hw/9pfs/virtio-9p-proxy.h @@ -13,6 +13,7 @@ #define _QEMU_VIRTIO_9P_PROXY_H #define PROXY_MAX_IO_SZ (64 * 1024) +#define V9FS_FD_VALID INT_MAX /* * proxy iovec only support one element and @@ -23,6 +24,11 @@ #define proxy_marshal(out_sg, offset, fmt, args...) \ v9fs_marshal(out_sg, 1, offset, 0, fmt, ##args) +union MsgControl { + struct cmsghdr cmsg; + char control[CMSG_SPACE(sizeof(int))]; +}; + typedef struct { uint32_t type; uint32_t size; @@ -30,4 +36,9 @@ typedef struct { #define PROXY_HDR_SZ (sizeof(ProxyHeader)) +enum { + T_OPEN = 1, + T_CREATE, +}; + #endif