From patchwork Thu Apr 15 14:10:55 2010 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: "Aneesh Kumar K.V" X-Patchwork-Id: 50262 Return-Path: X-Original-To: incoming@patchwork.ozlabs.org Delivered-To: patchwork-incoming@bilbo.ozlabs.org Received: from lists.gnu.org (lists.gnu.org [199.232.76.165]) (using TLSv1 with cipher DHE-RSA-AES256-SHA (256/256 bits)) (Client did not present a certificate) by ozlabs.org (Postfix) with ESMTPS id B42C9B7C8E for ; Fri, 16 Apr 2010 01:01:32 +1000 (EST) Received: from localhost ([127.0.0.1]:44194 helo=lists.gnu.org) by lists.gnu.org with esmtp (Exim 4.43) id 1O2QYr-0004jX-SO for incoming@patchwork.ozlabs.org; Thu, 15 Apr 2010 11:00:57 -0400 Received: from mailman by lists.gnu.org with tmda-scanned (Exim 4.43) id 1O2Ptb-0003aL-SR for qemu-devel@nongnu.org; Thu, 15 Apr 2010 10:18:20 -0400 Received: from [140.186.70.92] (port=50377 helo=eggs.gnu.org) by lists.gnu.org with esmtp (Exim 4.43) id 1O2PtS-0003H8-Rs for qemu-devel@nongnu.org; Thu, 15 Apr 2010 10:18:18 -0400 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.69) (envelope-from ) id 1O2Pmv-0000GU-2Z for qemu-devel@nongnu.org; Thu, 15 Apr 2010 10:11:34 -0400 Received: from e28smtp02.in.ibm.com ([122.248.162.2]:57174) by eggs.gnu.org with esmtp (Exim 4.69) (envelope-from ) id 1O2Pmt-0000FD-6b for qemu-devel@nongnu.org; Thu, 15 Apr 2010 10:11:24 -0400 Received: from d28relay03.in.ibm.com (d28relay03.in.ibm.com [9.184.220.60]) by e28smtp02.in.ibm.com (8.14.3/8.13.1) with ESMTP id o3FEBKko032133 for ; Thu, 15 Apr 2010 19:41:20 +0530 Received: from d28av01.in.ibm.com (d28av01.in.ibm.com [9.184.220.63]) by d28relay03.in.ibm.com (8.13.8/8.13.8/NCO v10.0) with ESMTP id o3FEBKY73031162 for ; Thu, 15 Apr 2010 19:41:20 +0530 Received: from d28av01.in.ibm.com (loopback [127.0.0.1]) by d28av01.in.ibm.com (8.14.3/8.13.1/NCO v10.0 AVout) with ESMTP id o3FEBKPd030491 for ; Thu, 15 Apr 2010 19:41:20 +0530 Received: from skywalker.in.ibm.com ([9.77.204.187]) by d28av01.in.ibm.com (8.14.3/8.13.1/NCO v10.0 AVin) with ESMTP id o3FEBDa9030329; Thu, 15 Apr 2010 19:41:19 +0530 From: "Aneesh Kumar K.V" To: qemu-devel@nongnu.org Date: Thu, 15 Apr 2010 19:40:55 +0530 Message-Id: <1271340671-19558-6-git-send-email-aneesh.kumar@linux.vnet.ibm.com> X-Mailer: git-send-email 1.7.0.4.360.g11766c In-Reply-To: <1271340671-19558-1-git-send-email-aneesh.kumar@linux.vnet.ibm.com> References: <1271340671-19558-1-git-send-email-aneesh.kumar@linux.vnet.ibm.com> X-detected-operating-system: by eggs.gnu.org: GNU/Linux 2.6, seldom 2.4 (older, 4) Cc: ericvh@gmail.com, aliguori@us.ibm.com, aneesh.kumar@linux.vnet.ibm.com Subject: [Qemu-devel] [PATCH -V5 05/21] virtio-9p: Add minimal set of FileOperations X-BeenThere: qemu-devel@nongnu.org X-Mailman-Version: 2.1.5 Precedence: list List-Id: qemu-devel.nongnu.org List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Sender: qemu-devel-bounces+incoming=patchwork.ozlabs.org@nongnu.org Errors-To: qemu-devel-bounces+incoming=patchwork.ozlabs.org@nongnu.org From: Anthony Liguori Add minimal set of FileOperations and the corresponding implementations for local fstype. These will be required for the FID management patches later on. [aneesh.kumar@linux.vnet.ibm.com: rpath fix ] Signed-off-by: Anthony Liguori Signed-off-by: Aneesh Kumar K.V --- hw/file-op-9p.h | 5 +++ hw/virtio-9p-local.c | 78 ++++++++++++++++++++++++++++++++++++++++++++++++++ hw/virtio-9p.c | 43 ++++++++++++++++++++++++++- 3 files changed, 124 insertions(+), 2 deletions(-) diff --git a/hw/file-op-9p.h b/hw/file-op-9p.h index 7cde63c..461df9e 100644 --- a/hw/file-op-9p.h +++ b/hw/file-op-9p.h @@ -27,6 +27,11 @@ typedef struct FsContext typedef struct FileOperations { + int (*lstat)(FsContext *, const char *, struct stat *); + ssize_t (*readlink)(FsContext *, const char *, char *, size_t); + int (*setuid)(FsContext *, uid_t); + int (*close)(FsContext *, int); + int (*closedir)(FsContext *, DIR *); void *opaque; } FileOperations; #endif diff --git a/hw/virtio-9p-local.c b/hw/virtio-9p-local.c index 379af60..880cd0a 100644 --- a/hw/virtio-9p-local.c +++ b/hw/virtio-9p-local.c @@ -12,6 +12,84 @@ */ #include "virtio.h" #include "virtio-9p.h" +#include +#include + +static const char *rpath(FsContext *ctx, const char *path) +{ + /* FIXME: so wrong... */ + static char buffer[4096]; + snprintf(buffer, sizeof(buffer), "%s/%s", ctx->fs_root, path); + return buffer; +} + +static int local_lstat(FsContext *ctx, const char *path, struct stat *stbuf) +{ + return lstat(rpath(ctx, path), stbuf); +} + +static int local_setuid(FsContext *ctx, uid_t uid) +{ + struct passwd *pw; + gid_t groups[33]; + int ngroups; + static uid_t cur_uid = -1; + + if (cur_uid == uid) { + return 0; + } + + if (setreuid(0, 0)) { + return -1; + } + + pw = getpwuid(uid); + if (pw == NULL) { + return -1; + } + + ngroups = 33; + if (getgrouplist(pw->pw_name, pw->pw_gid, groups, &ngroups) == -1) { + return -1; + } + + if (setgroups(ngroups, groups)) { + return -1; + } + + if (setregid(-1, pw->pw_gid)) { + return -1; + } + + if (setreuid(-1, uid)) { + return -1; + } + + cur_uid = uid; + + return 0; +} + +static ssize_t local_readlink(FsContext *ctx, const char *path, + char *buf, size_t bufsz) +{ + return readlink(rpath(ctx, path), buf, bufsz); +} + +static int local_close(FsContext *ctx, int fd) +{ + return close(fd); +} + +static int local_closedir(FsContext *ctx, DIR *dir) +{ + return closedir(dir); +} FileOperations local_ops = { + .lstat = local_lstat, + .setuid = local_setuid, + .readlink = local_readlink, + .close = local_close, + .closedir = local_closedir, }; diff --git a/hw/virtio-9p.c b/hw/virtio-9p.c index 08df084..528c9cc 100644 --- a/hw/virtio-9p.c +++ b/hw/virtio-9p.c @@ -21,6 +21,41 @@ int dotu = 1; int debug_9p_pdu; +static int v9fs_do_lstat(V9fsState *s, V9fsString *path, struct stat *stbuf) +{ + return s->ops->lstat(&s->ctx, path->data, stbuf); +} + +static int v9fs_do_setuid(V9fsState *s, uid_t uid) +{ + return s->ops->setuid(&s->ctx, uid); +} + +static ssize_t v9fs_do_readlink(V9fsState *s, V9fsString *path, V9fsString *buf) +{ + ssize_t len; + + buf->data = qemu_malloc(1024); + + len = s->ops->readlink(&s->ctx, path->data, buf->data, 1024 - 1); + if (len > -1) { + buf->size = len; + buf->data[len] = 0; + } + + return len; +} + +static int v9fs_do_close(V9fsState *s, int fd) +{ + return s->ops->close(&s->ctx, fd); +} + +static int v9fs_do_closedir(V9fsState *s, DIR *dir) +{ + return s->ops->closedir(&s->ctx, dir); +} + static void v9fs_string_init(V9fsString *str) { str->data = NULL; @@ -439,9 +474,13 @@ static void v9fs_dummy(V9fsState *s, V9fsPDU *pdu) (void) v9fs_string_sprintf; (void) v9fs_string_copy; (void) v9fs_string_size; - - + (void) v9fs_do_lstat; + (void) v9fs_do_setuid; + (void) v9fs_do_readlink; + (void) v9fs_do_close; + (void) v9fs_do_closedir; } + static void v9fs_version(V9fsState *s, V9fsPDU *pdu) { if (debug_9p_pdu) {