From patchwork Thu Aug 11 05:13:11 2016 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Prasad Pandit X-Patchwork-Id: 9274465 Return-Path: Received: from mail.wl.linuxfoundation.org (pdx-wl-mail.web.codeaurora.org [172.30.200.125]) by pdx-korg-patchwork.web.codeaurora.org (Postfix) with ESMTP id 9F81E60231 for ; Thu, 11 Aug 2016 05:13:42 +0000 (UTC) Received: from mail.wl.linuxfoundation.org (localhost [127.0.0.1]) by mail.wl.linuxfoundation.org (Postfix) with ESMTP id 862DE284AD for ; Thu, 11 Aug 2016 05:13:42 +0000 (UTC) Received: by mail.wl.linuxfoundation.org (Postfix, from userid 486) id 79888284E3; Thu, 11 Aug 2016 05:13:42 +0000 (UTC) X-Spam-Checker-Version: SpamAssassin 3.3.1 (2010-03-16) on pdx-wl-mail.web.codeaurora.org X-Spam-Level: X-Spam-Status: No, score=-6.9 required=2.0 tests=BAYES_00,RCVD_IN_DNSWL_HI autolearn=ham version=3.3.1 Received: from lists.gnu.org (lists.gnu.org [208.118.235.17]) (using TLSv1 with cipher AES256-SHA (256/256 bits)) (No client certificate requested) by mail.wl.linuxfoundation.org (Postfix) with ESMTPS id C1125284AD for ; Thu, 11 Aug 2016 05:13:40 +0000 (UTC) Received: from localhost ([::1]:45676 helo=lists.gnu.org) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1bXiJH-0008Q1-Fu for patchwork-qemu-devel@patchwork.kernel.org; Thu, 11 Aug 2016 01:13:39 -0400 Received: from eggs.gnu.org ([2001:4830:134:3::10]:58497) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1bXiJ2-0008EO-2B for qemu-devel@nongnu.org; Thu, 11 Aug 2016 01:13:25 -0400 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1bXiIx-0004HU-QL for qemu-devel@nongnu.org; Thu, 11 Aug 2016 01:13:22 -0400 Received: from mx1.redhat.com ([209.132.183.28]:37880) by eggs.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1bXiIx-0004HN-IH for qemu-devel@nongnu.org; Thu, 11 Aug 2016 01:13:19 -0400 Received: from int-mx10.intmail.prod.int.phx2.redhat.com (int-mx10.intmail.prod.int.phx2.redhat.com [10.5.11.23]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by mx1.redhat.com (Postfix) with ESMTPS id C079A4E32C; Thu, 11 Aug 2016 05:13:18 +0000 (UTC) Received: from javelin.localdomain (vpn1-5-177.sin2.redhat.com [10.67.5.177]) by int-mx10.intmail.prod.int.phx2.redhat.com (8.14.4/8.14.4) with ESMTP id u7B5DD7X018732 (version=TLSv1/SSLv3 cipher=DHE-RSA-AES256-GCM-SHA384 bits=256 verify=NO); Thu, 11 Aug 2016 01:13:15 -0400 From: P J P To: Qemu Developers Date: Thu, 11 Aug 2016 10:43:11 +0530 Message-Id: <1470892391-4917-1-git-send-email-ppandit@redhat.com> X-Scanned-By: MIMEDefang 2.68 on 10.5.11.23 X-Greylist: Sender IP whitelisted, not delayed by milter-greylist-4.5.16 (mx1.redhat.com [10.5.110.38]); Thu, 11 Aug 2016 05:13:18 +0000 (UTC) X-detected-operating-system: by eggs.gnu.org: GNU/Linux 2.2.x-3.x [generic] X-Received-From: 209.132.183.28 Subject: [Qemu-devel] [PATCH] 9pfs: add check for relative path 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: Prasad J Pandit , Greg Kurz , "Michael S. Tsirkin" , Felix Wilhelm , "Aneesh Kumar K.V" Errors-To: qemu-devel-bounces+patchwork-qemu-devel=patchwork.kernel.org@nongnu.org Sender: "Qemu-devel" X-Virus-Scanned: ClamAV using ClamSMTP From: Prasad J Pandit At various places in 9pfs back-end, it creates full path by concatenating two path strings. It could lead to a path traversal issue if one of the parameter was a relative path. Add check to avoid it. Reported-by: Felix Wilhelm Signed-off-by: Prasad J Pandit Acked-by: Greg Kurz --- hw/9pfs/9p-local.c | 31 +++++++++++++++++++++++++++---- 1 file changed, 27 insertions(+), 4 deletions(-) diff --git a/hw/9pfs/9p-local.c b/hw/9pfs/9p-local.c index 3f271fc..c20331a 100644 --- a/hw/9pfs/9p-local.c +++ b/hw/9pfs/9p-local.c @@ -493,6 +493,9 @@ static int local_mknod(FsContext *fs_ctx, V9fsPath *dir_path, char *buffer = NULL; v9fs_string_init(&fullname); + if (strstr(name, "../")) { + return err; + } v9fs_string_sprintf(&fullname, "%s/%s", dir_path->data, name); path = fullname.data; @@ -554,6 +557,9 @@ static int local_mkdir(FsContext *fs_ctx, V9fsPath *dir_path, char *buffer = NULL; v9fs_string_init(&fullname); + if (strstr(name, "../")) { + return err; + } v9fs_string_sprintf(&fullname, "%s/%s", dir_path->data, name); path = fullname.data; @@ -663,6 +669,9 @@ static int local_open2(FsContext *fs_ctx, V9fsPath *dir_path, const char *name, flags |= O_NOFOLLOW; v9fs_string_init(&fullname); + if (strstr(name, "../")) { + return err; + } v9fs_string_sprintf(&fullname, "%s/%s", dir_path->data, name); path = fullname.data; @@ -734,6 +743,9 @@ static int local_symlink(FsContext *fs_ctx, const char *oldpath, char *buffer = NULL; v9fs_string_init(&fullname); + if (strstr(name, "../")) { + return err; + } v9fs_string_sprintf(&fullname, "%s/%s", dir_path->data, name); newpath = fullname.data; @@ -830,11 +842,14 @@ out: static int local_link(FsContext *ctx, V9fsPath *oldpath, V9fsPath *dirpath, const char *name) { - int ret; + int ret = -1; V9fsString newpath; char *buffer, *buffer1; v9fs_string_init(&newpath); + if (strstr(name, "../")) { + return ret; + } v9fs_string_sprintf(&newpath, "%s/%s", dirpath->data, name); buffer = rpath(ctx, oldpath->data); @@ -1059,6 +1074,9 @@ static int local_lremovexattr(FsContext *ctx, V9fsPath *fs_path, static int local_name_to_path(FsContext *ctx, V9fsPath *dir_path, const char *name, V9fsPath *target) { + if (strstr(name, "../")) { + return -1; + } if (dir_path) { v9fs_string_sprintf((V9fsString *)target, "%s/%s", dir_path->data, name); @@ -1074,12 +1092,15 @@ static int local_renameat(FsContext *ctx, V9fsPath *olddir, const char *old_name, V9fsPath *newdir, const char *new_name) { - int ret; + int ret = -1; V9fsString old_full_name, new_full_name; v9fs_string_init(&old_full_name); v9fs_string_init(&new_full_name); + if (strstr(old_name, "../") || strstr(new_name, "../")) { + return ret; + } v9fs_string_sprintf(&old_full_name, "%s/%s", olddir->data, old_name); v9fs_string_sprintf(&new_full_name, "%s/%s", newdir->data, new_name); @@ -1092,12 +1113,14 @@ static int local_renameat(FsContext *ctx, V9fsPath *olddir, static int local_unlinkat(FsContext *ctx, V9fsPath *dir, const char *name, int flags) { - int ret; + int ret = -1; V9fsString fullname; char *buffer; v9fs_string_init(&fullname); - + if (strstr(name, "../")) { + return ret; + } v9fs_string_sprintf(&fullname, "%s/%s", dir->data, name); if (ctx->export_flags & V9FS_SM_MAPPED_FILE) { if (flags == AT_REMOVEDIR) {