From patchwork Mon Jan 30 12:11:46 2017 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Greg Kurz X-Patchwork-Id: 721428 Return-Path: X-Original-To: incoming@patchwork.ozlabs.org Delivered-To: patchwork-incoming@bilbo.ozlabs.org Received: from lists.gnu.org (lists.gnu.org [IPv6:2001:4830:134:3::11]) (using TLSv1 with cipher AES256-SHA (256/256 bits)) (No client certificate requested) by ozlabs.org (Postfix) with ESMTPS id 3vBpYJ3ZTTz9s3T for ; Mon, 30 Jan 2017 23:27:00 +1100 (AEDT) Received: from localhost ([::1]:59939 helo=lists.gnu.org) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1cYB2v-0003Td-UM for incoming@patchwork.ozlabs.org; Mon, 30 Jan 2017 07:26:57 -0500 Received: from eggs.gnu.org ([2001:4830:134:3::10]:45934) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1cYAoR-0006a7-C3 for qemu-devel@nongnu.org; Mon, 30 Jan 2017 07:12:00 -0500 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1cYAoO-0000J8-6o for qemu-devel@nongnu.org; Mon, 30 Jan 2017 07:11:59 -0500 Received: from mx0a-001b2d01.pphosted.com ([148.163.156.1]:59216) by eggs.gnu.org with esmtps (TLS1.0:RSA_AES_256_CBC_SHA1:32) (Exim 4.71) (envelope-from ) id 1cYAoN-0000J3-T5 for qemu-devel@nongnu.org; Mon, 30 Jan 2017 07:11:56 -0500 Received: from pps.filterd (m0098396.ppops.net [127.0.0.1]) by mx0a-001b2d01.pphosted.com (8.16.0.20/8.16.0.20) with SMTP id v0UC8r1J054393 for ; Mon, 30 Jan 2017 07:11:54 -0500 Received: from e17.ny.us.ibm.com (e17.ny.us.ibm.com [129.33.205.207]) by mx0a-001b2d01.pphosted.com with ESMTP id 28a37q5gjy-1 (version=TLSv1.2 cipher=AES256-SHA bits=256 verify=NOT) for ; Mon, 30 Jan 2017 07:11:54 -0500 Received: from localhost by e17.ny.us.ibm.com with IBM ESMTP SMTP Gateway: Authorized Use Only! Violators will be prosecuted for from ; Mon, 30 Jan 2017 07:11:53 -0500 Received: from d01dlp01.pok.ibm.com (9.56.250.166) by e17.ny.us.ibm.com (146.89.104.204) with IBM ESMTP SMTP Gateway: Authorized Use Only! Violators will be prosecuted; Mon, 30 Jan 2017 07:11:49 -0500 Received: from b01cxnp23032.gho.pok.ibm.com (b01cxnp23032.gho.pok.ibm.com [9.57.198.27]) by d01dlp01.pok.ibm.com (Postfix) with ESMTP id B6BC238C8039; Mon, 30 Jan 2017 07:11:49 -0500 (EST) Received: from b01ledav004.gho.pok.ibm.com (b01ledav004.gho.pok.ibm.com [9.57.199.109]) by b01cxnp23032.gho.pok.ibm.com (8.14.9/8.14.9/NCO v10.0) with ESMTP id v0UCBm5b8323346; Mon, 30 Jan 2017 12:11:48 GMT Received: from b01ledav004.gho.pok.ibm.com (unknown [127.0.0.1]) by IMSVA (Postfix) with ESMTP id 7342111204B; Mon, 30 Jan 2017 07:11:48 -0500 (EST) Received: from bahia.lan (unknown [9.164.157.254]) by b01ledav004.gho.pok.ibm.com (Postfix) with ESMTP id 57243112051; Mon, 30 Jan 2017 07:11:47 -0500 (EST) From: Greg Kurz To: qemu-devel@nongnu.org Date: Mon, 30 Jan 2017 13:11:46 +0100 In-Reply-To: <148577817618.10533.9740628265078537215.stgit@bahia.lan> References: <148577817618.10533.9740628265078537215.stgit@bahia.lan> User-Agent: StGit/0.17.1-20-gc0b1b-dirty MIME-Version: 1.0 X-TM-AS-GCONF: 00 X-Content-Scanned: Fidelis XPS MAILER x-cbid: 17013012-0040-0000-0000-0000027E43FE X-IBM-SpamModules-Scores: X-IBM-SpamModules-Versions: BY=3.00006524; HX=3.00000240; KW=3.00000007; PH=3.00000004; SC=3.00000201; SDB=6.00814715; UDB=6.00397662; IPR=6.00592156; BA=6.00005099; NDR=6.00000001; ZLA=6.00000005; ZF=6.00000009; ZB=6.00000000; ZP=6.00000000; ZH=6.00000000; ZU=6.00000002; MB=3.00014106; XFM=3.00000011; UTC=2017-01-30 12:11:51 X-IBM-AV-DETECTION: SAVI=unused REMOTE=unused XFE=unused x-cbparentid: 17013012-0041-0000-0000-000006714949 Message-Id: <148577830646.10533.15016303134930167352.stgit@bahia.lan> X-Proofpoint-Virus-Version: vendor=fsecure engine=2.50.10432:, , definitions=2017-01-30_08:, , signatures=0 X-Proofpoint-Spam-Details: rule=outbound_notspam policy=outbound score=0 spamscore=0 suspectscore=3 malwarescore=0 phishscore=0 adultscore=0 bulkscore=0 classifier=spam adjust=0 reason=mlx scancount=1 engine=8.0.1-1612050000 definitions=main-1701300122 X-detected-operating-system: by eggs.gnu.org: GNU/Linux 3.x [generic] [fuzzy] X-Received-From: 148.163.156.1 Subject: [Qemu-devel] [PATCH RFC 17/36] 9pfs: local: keep a file descriptor on the shared folder X-BeenThere: qemu-devel@nongnu.org X-Mailman-Version: 2.1.21 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Cc: "Aneesh Kumar K.V" , jannh@google.com, Greg Kurz , ppandit@redhat.com Errors-To: qemu-devel-bounces+incoming=patchwork.ozlabs.org@nongnu.org Sender: "Qemu-devel" This patch opens the shared folder and caches the file descriptor, so that it can be used to do symlink-safe path walk. Since nothing prevents several QEMU instances to pass overlapping export paths to -fsdev, we also make sure that the export path doesn't traverse a symlink either. Signed-off-by: Greg Kurz --- hw/9pfs/9p-local.c | 38 ++++++++++++++++++++++++++++++++++++-- 1 file changed, 36 insertions(+), 2 deletions(-) diff --git a/hw/9pfs/9p-local.c b/hw/9pfs/9p-local.c index d3c6ccf30b53..8a1d52cd6c2a 100644 --- a/hw/9pfs/9p-local.c +++ b/hw/9pfs/9p-local.c @@ -14,6 +14,7 @@ #include "qemu/osdep.h" #include "9p.h" #include "9p-xattr.h" +#include "9p-util.h" #include "fsdev/qemu-fsdev.h" /* local_ops */ #include #include @@ -43,6 +44,10 @@ #define BTRFS_SUPER_MAGIC 0x9123683E #endif +struct local_data { + int mountfd; +}; + #define VIRTFS_META_DIR ".virtfs_metadata" static char *local_mapped_attr_path(FsContext *ctx, const char *path) @@ -1573,13 +1578,28 @@ static int local_ioc_getversion(FsContext *ctx, V9fsPath *path, static int local_init(FsContext *ctx) { struct statfs stbuf; + struct local_data *data = g_malloc(sizeof(*data)); + int rootfd; + + rootfd = open("/", O_DIRECTORY | O_RDONLY); + if (rootfd == -1) { + goto err; + } + + data->mountfd = openat_nofollow(rootfd, ctx->fs_root, + O_DIRECTORY | O_RDONLY, 0); + close_preserve_errno(rootfd); + if (data->mountfd == -1) { + goto err; + } #ifdef FS_IOC_GETVERSION /* * use ioc_getversion only if the iocl is definied */ - if (statfs(ctx->fs_root, &stbuf) < 0) { - return -1; + if (fstatfs(data->mountfd, &stbuf) < 0) { + close_preserve_errno(data->mountfd); + goto err; } switch (stbuf.f_type) { case EXT2_SUPER_MAGIC: @@ -1606,7 +1626,20 @@ static int local_init(FsContext *ctx) } ctx->export_flags |= V9FS_PATHNAME_FSCONTEXT; + ctx->private = data; return 0; + +err: + g_free(data); + return -1; +} + +static void local_cleanup(FsContext *ctx) +{ + struct local_data *data = ctx->private; + + close(data->mountfd); + g_free(data); } static int local_parse_opts(QemuOpts *opts, struct FsDriverEntry *fse) @@ -1649,6 +1682,7 @@ static int local_parse_opts(QemuOpts *opts, struct FsDriverEntry *fse) FileOperations local_ops = { .parse_opts = local_parse_opts, .init = local_init, + .cleanup = local_cleanup, .lstat = local_lstat, .readlink = local_readlink, .close = local_close,