From patchwork Thu Mar 31 08:21:40 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: 89046 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 CC3BEB6F77 for ; Thu, 31 Mar 2011 19:49:23 +1100 (EST) Received: from localhost ([127.0.0.1]:56635 helo=lists.gnu.org) by lists.gnu.org with esmtp (Exim 4.43) id 1Q5DYz-0002GB-Ja for incoming@patchwork.ozlabs.org; Thu, 31 Mar 2011 04:49:10 -0400 Received: from [140.186.70.92] (port=53032 helo=eggs.gnu.org) by lists.gnu.org with esmtp (Exim 4.43) id 1Q5DFw-00089J-9i for qemu-devel@nongnu.org; Thu, 31 Mar 2011 04:29:30 -0400 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1Q5DFr-0007D5-5z for qemu-devel@nongnu.org; Thu, 31 Mar 2011 04:29:24 -0400 Received: from e28smtp08.in.ibm.com ([122.248.162.8]:50608) by eggs.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1Q5DFq-0007Bz-1m for qemu-devel@nongnu.org; Thu, 31 Mar 2011 04:29:23 -0400 Received: from d28relay03.in.ibm.com (d28relay03.in.ibm.com [9.184.220.60]) by e28smtp08.in.ibm.com (8.14.4/8.13.1) with ESMTP id p2V7NGmB029949 for ; Thu, 31 Mar 2011 12:53:16 +0530 Received: from d28av04.in.ibm.com (d28av04.in.ibm.com [9.184.220.66]) by d28relay03.in.ibm.com (8.13.8/8.13.8/NCO v10.0) with ESMTP id p2V8LgMV4173846 for ; Thu, 31 Mar 2011 13:51:42 +0530 Received: from d28av04.in.ibm.com (loopback [127.0.0.1]) by d28av04.in.ibm.com (8.14.4/8.13.1/NCO v10.0 AVout) with ESMTP id p2V8Lf0C002912 for ; Thu, 31 Mar 2011 19:21:41 +1100 Received: from explorer.in.ibm.com ([9.124.35.24]) by d28av04.in.ibm.com (8.14.4/8.13.1/NCO v10.0 AVin) with ESMTP id p2V8LeD0002810; Thu, 31 Mar 2011 19:21:41 +1100 From: "M. Mohan Kumar" To: qemu-devel@nongnu.org, Stefan Hajnoczi Date: Thu, 31 Mar 2011 13:51:40 +0530 Message-Id: <1301559700-6742-14-git-send-email-mohan@in.ibm.com> X-Mailer: git-send-email 1.7.4 In-Reply-To: <1301559700-6742-1-git-send-email-mohan@in.ibm.com> References: <1301559700-6742-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: 122.248.162.8 Cc: Subject: [Qemu-devel] [V10 PATCH 13/13] virtio-9p: Chroot environment for other functions 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 chroot functionality for systemcalls that can operate on a file using relative directory file descriptor. Signed-off-by: M. Mohan Kumar --- hw/9pfs/virtio-9p-local.c | 125 +++++++++++++++++++++++++++++++++++++++++---- 1 files changed, 115 insertions(+), 10 deletions(-) diff --git a/hw/9pfs/virtio-9p-local.c b/hw/9pfs/virtio-9p-local.c index def0c62..fa5c88f 100644 --- a/hw/9pfs/virtio-9p-local.c +++ b/hw/9pfs/virtio-9p-local.c @@ -22,6 +22,8 @@ #include #include #include +#include +#include "qemu-error.h" /* Helper routine to fill V9fsFileObjectRequest structure */ static int fill_fileobjectrequest(V9fsFileObjectRequest *request, @@ -80,14 +82,42 @@ 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) +{ + int fd; + char *dpath; + char *last_component; + + /* path can not contain ".." */ + last_component = strrchr(path, '/'); + if (last_component && !strcmp(last_component, "/..")) { + error_report("9p path request contains \"..\": %s\n", path); + errno = EFAULT; + return -1; + } + dpath = qemu_strdup(path); + fd = passthrough_request(fs_ctx, NULL, dirname(dpath), 0, NULL, T_OPEN); + 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; @@ -109,6 +139,26 @@ 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); + if (err < 0) { + errno = serrno; + } + } else { + err = lstat(rpath(fs_ctx, path), stbuf); } return err; } @@ -173,9 +223,26 @@ 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); + if (tsize < 0) { + errno = serrno; + } } return tsize; } @@ -501,7 +568,25 @@ 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); + if (retval < 0) { + serrno = errno; + } + close(fd); + if (retval < 0) { + errno = serrno; + } + return retval; + } else { + return truncate(rpath(ctx, path), size); + } } static int local_rename(FsContext *ctx, const char *oldpath, @@ -541,10 +626,30 @@ static int local_chown(FsContext *fs_ctx, const char *path, FsCred *credp) 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, serrno = 0; + 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); + if (retval < 0) { + serrno = errno; + } + close(fd); + if (retval < 0) { + errno = serrno; + } + return retval; + } else { + return utimensat(AT_FDCWD, rpath(fs_ctx, path), buf, + AT_SYMLINK_NOFOLLOW); + } } static int local_remove(FsContext *ctx, const char *path)