From patchwork Fri Mar 4 09:25:53 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: 85387 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 1F0F4B6EF7 for ; Fri, 4 Mar 2011 20:37:01 +1100 (EST) Received: from localhost ([127.0.0.1]:60838 helo=lists.gnu.org) by lists.gnu.org with esmtp (Exim 4.43) id 1PvRRR-0006VE-K6 for incoming@patchwork.ozlabs.org; Fri, 04 Mar 2011 04:36:57 -0500 Received: from [140.186.70.92] (port=53748 helo=eggs.gnu.org) by lists.gnu.org with esmtp (Exim 4.43) id 1PvRH2-00023b-0J for qemu-devel@nongnu.org; Fri, 04 Mar 2011 04:26:14 -0500 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1PvRGz-0003ep-8J for qemu-devel@nongnu.org; Fri, 04 Mar 2011 04:26:11 -0500 Received: from e23smtp04.au.ibm.com ([202.81.31.146]:56632) by eggs.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1PvRGy-0003eX-N2 for qemu-devel@nongnu.org; Fri, 04 Mar 2011 04:26:09 -0500 Received: from d23relay03.au.ibm.com (d23relay03.au.ibm.com [202.81.31.245]) by e23smtp04.au.ibm.com (8.14.4/8.13.1) with ESMTP id p249Ka6B023338 for ; Fri, 4 Mar 2011 20:20:36 +1100 Received: from d23av02.au.ibm.com (d23av02.au.ibm.com [9.190.235.138]) by d23relay03.au.ibm.com (8.13.8/8.13.8/NCO v10.0) with ESMTP id p249Q7bh1753220 for ; Fri, 4 Mar 2011 20:26:07 +1100 Received: from d23av02.au.ibm.com (loopback [127.0.0.1]) by d23av02.au.ibm.com (8.14.4/8.13.1/NCO v10.0 AVout) with ESMTP id p249Q7Hj023651 for ; Fri, 4 Mar 2011 20:26:07 +1100 Received: from explorer.in.ibm.com ([9.79.221.231]) by d23av02.au.ibm.com (8.14.4/8.13.1/NCO v10.0 AVin) with ESMTP id p249PvTX023421; Fri, 4 Mar 2011 20:26:06 +1100 From: "M. Mohan Kumar" To: qemu-devel@nongnu.org, Stefan Hajnoczi Date: Fri, 4 Mar 2011 14:55:53 +0530 Message-Id: <1299230756-1644-7-git-send-email-mohan@in.ibm.com> X-Mailer: git-send-email 1.7.3.4 In-Reply-To: <1299230756-1644-1-git-send-email-mohan@in.ibm.com> References: <1299230756-1644-1-git-send-email-mohan@in.ibm.com> X-detected-operating-system: by eggs.gnu.org: GNU/Linux 2.6, seldom 2.4 (older, 4) X-Received-From: 202.81.31.146 Cc: Subject: [Qemu-devel] [V7 PATCH 6/9] virtio-9p: Create support in chroot environment 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 Add both chroot worker & qemu side interfaces to create regular files in chroot environment Signed-off-by: M. Mohan Kumar --- hw/9pfs/virtio-9p-chroot-worker.c | 36 ++++++++++++++++++++++++++++++++++++ hw/9pfs/virtio-9p-local.c | 26 ++++++++++++++++++++++++-- 2 files changed, 60 insertions(+), 2 deletions(-) diff --git a/hw/9pfs/virtio-9p-chroot-worker.c b/hw/9pfs/virtio-9p-chroot-worker.c index e7bc6c2..cdcb303 100644 --- a/hw/9pfs/virtio-9p-chroot-worker.c +++ b/hw/9pfs/virtio-9p-chroot-worker.c @@ -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; diff --git a/hw/9pfs/virtio-9p-local.c b/hw/9pfs/virtio-9p-local.c index 8c187d7..aa8ebff 100644 --- a/hw/9pfs/virtio-9p-local.c +++ b/hw/9pfs/virtio-9p-local.c @@ -62,6 +62,27 @@ static int passthrough_open(FsContext *fs_ctx, const char *path, int flags) return fd; } +static int passthrough_create(FsContext *fs_ctx, const char *path, int flags, + FsCred *credp) +{ + V9fsFileObjectRequest request; + int fd; + + fd = fill_fileobjectrequest(&request, path, credp); + if (fd < 0) { + errno = -1; + return -1; + } + request.data.flags = flags; + request.data.type = T_CREATE; + fd = v9fs_request(fs_ctx, &request); + if (fd < 0) { + errno = -fd; + fd = -1; + } + return fd; +} + static int local_lstat(FsContext *fs_ctx, const char *path, struct stat *stbuf) { int err; @@ -386,8 +407,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; @@ -397,6 +417,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_create(fs_ctx, path, flags, credp); } return fd;