From patchwork Tue May 11 05:55:18 2021 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Patchwork-Submitter: Li Zhijian X-Patchwork-Id: 12249825 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org X-Spam-Level: X-Spam-Status: No, score=-16.8 required=3.0 tests=BAYES_00, HEADER_FROM_DIFFERENT_DOMAINS,INCLUDES_CR_TRAILER,INCLUDES_PATCH, MAILING_LIST_MULTI,SPF_HELO_NONE,SPF_PASS,URIBL_BLOCKED,USER_AGENT_GIT autolearn=ham autolearn_force=no version=3.4.0 Received: from mail.kernel.org (mail.kernel.org [198.145.29.99]) by smtp.lore.kernel.org (Postfix) with ESMTP id E1E4AC433ED for ; Tue, 11 May 2021 06:04:47 +0000 (UTC) Received: from lists.gnu.org (lists.gnu.org [209.51.188.17]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by mail.kernel.org (Postfix) with ESMTPS id 5E44561606 for ; Tue, 11 May 2021 06:04:47 +0000 (UTC) DMARC-Filter: OpenDMARC Filter v1.3.2 mail.kernel.org 5E44561606 Authentication-Results: mail.kernel.org; dmarc=none (p=none dis=none) header.from=cn.fujitsu.com Authentication-Results: mail.kernel.org; spf=pass smtp.mailfrom=qemu-devel-bounces+qemu-devel=archiver.kernel.org@nongnu.org Received: from localhost ([::1]:49830 helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1lgLVe-0006Mj-6C for qemu-devel@archiver.kernel.org; Tue, 11 May 2021 02:04:46 -0400 Received: from eggs.gnu.org ([2001:470:142:3::10]:35188) by lists.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1lgLNA-0004uD-P2; Tue, 11 May 2021 01:56:00 -0400 Received: from mail.cn.fujitsu.com ([183.91.158.132]:3603 helo=heian.cn.fujitsu.com) by eggs.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1lgLN7-0003UO-Qb; Tue, 11 May 2021 01:56:00 -0400 IronPort-HdrOrdr: A9a23:SMU+PK5N1yHmB7nhQQPXwPTXdLJyesId70hD6qkRc20wTiX8ra2TdZsguyMc9wx6ZJhNo7G90cq7MBbhHPxOkOos1N6ZNWGIhILCFvAB0WKN+V3dMhy73utc+IMlSKJmFeD3ZGIQse/KpCW+DPYsqePqzJyV X-IronPort-AV: E=Sophos;i="5.82,290,1613404800"; d="scan'208";a="108115337" Received: from unknown (HELO cn.fujitsu.com) ([10.167.33.5]) by heian.cn.fujitsu.com with ESMTP; 11 May 2021 13:55:50 +0800 Received: from G08CNEXMBPEKD04.g08.fujitsu.local (unknown [10.167.33.201]) by cn.fujitsu.com (Postfix) with ESMTP id 5E4734D0BA7A; Tue, 11 May 2021 13:55:49 +0800 (CST) Received: from G08CNEXCHPEKD08.g08.fujitsu.local (10.167.33.83) by G08CNEXMBPEKD04.g08.fujitsu.local (10.167.33.201) with Microsoft SMTP Server (TLS) id 15.0.1497.2; Tue, 11 May 2021 13:55:43 +0800 Received: from FNSTPC.g08.fujitsu.local (10.167.226.45) by G08CNEXCHPEKD08.g08.fujitsu.local (10.167.33.209) with Microsoft SMTP Server id 15.0.1497.2 via Frontend Transport; Tue, 11 May 2021 13:55:45 +0800 From: Li Zhijian To: , , , Subject: [PATCH v2] block: Improve backing file validation Date: Tue, 11 May 2021 13:55:18 +0800 Message-ID: <20210511055518.31876-1-lizhijian@cn.fujitsu.com> X-Mailer: git-send-email 2.30.2 MIME-Version: 1.0 X-yoursite-MailScanner-ID: 5E4734D0BA7A.AE945 X-yoursite-MailScanner: Found to be clean X-yoursite-MailScanner-From: lizhijian@fujitsu.com Received-SPF: neutral client-ip=183.91.158.132; envelope-from=lizhijian@fujitsu.com; helo=heian.cn.fujitsu.com X-Spam_score_int: -33 X-Spam_score: -3.4 X-Spam_bar: --- X-Spam_report: (-3.4 / 5.0 requ) BAYES_00=-1.9, RCVD_IN_DNSWL_MED=-2.3, SPF_HELO_NONE=0.001, SPF_NEUTRAL=0.779 autolearn=ham autolearn_force=no X-Spam_action: no action X-BeenThere: qemu-devel@nongnu.org X-Mailman-Version: 2.1.23 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Cc: qemu-devel@nongnu.org, Li Zhijian Errors-To: qemu-devel-bounces+qemu-devel=archiver.kernel.org@nongnu.org Sender: "Qemu-devel" Image below user cases: case 1: ``` $ qemu-img create -f raw source.raw 1G $ qemu-img create -f qcow2 -F raw -b source.raw ./source.raw qemu-img info source.raw image: source.raw file format: qcow2 virtual size: 193K (197120 bytes) disk size: 196K cluster_size: 65536 backing file: source.raw <<<<<< backing file format: raw Format specific information: compat: 1.1 lazy refcounts: false refcount bits: 16 corrupt: false ``` case 2: ``` $ qemu-img create -f raw source.raw 1G $ ln -sf source.raw destination.qcow2 $ qemu-img create -f qcow2 -F raw -b source.raw ./destination.qcow2 qemu-img info source.raw image: source.raw file format: qcow2 <<<<<< virtual size: 2.0G (2147483648 bytes) disk size: 196K cluster_size: 65536 backing file: source.raw backing file format: raw Format specific information: compat: 1.1 lazy refcounts: false refcount bits: 16 corrupt: false ``` Generally, we don't expect to corrupte the source.raw anyway, while actually it does. Here we check their inode number instead of file name. Suggested-by: Daniel P. Berrangé Signed-off-by: Li Zhijian --- v2: utilize stat() instead of realpath() (Daniel) Signed-off-by: Li Zhijian --- block.c | 39 ++++++++++++++++++++++++++++++++------- 1 file changed, 32 insertions(+), 7 deletions(-) diff --git a/block.c b/block.c index 9ad725d205..db4ae57959 100644 --- a/block.c +++ b/block.c @@ -6431,6 +6431,37 @@ bool bdrv_op_blocker_is_empty(BlockDriverState *bs) return true; } +static bool validate_backing_file(const char *filename, + const char *backing_file, Error **errp) +{ + struct stat filename_stat, backing_stat; + + if (backing_file[0] == '\0') { + error_setg(errp, "Expected backing file name, got empty string"); + goto out; + } + + /* check whether filename and backing_file are refering to the same file */ + if (stat(backing_file, &backing_stat) == -1) { + error_setg(errp, "Cannot stat backing file %s", backing_file); + goto out; + } + if (stat(filename, &filename_stat) == -1) { + /* Simply consider filename doesn't exist, no need to further check */ + return true; + } + if ((filename_stat.st_dev == backing_stat.st_dev) && + (filename_stat.st_ino == backing_stat.st_ino)) { + error_setg(errp, "Error: Trying to create an image with the " + "same filename as the backing file"); + goto out; + } + + return true; +out: + return false; +} + void bdrv_img_create(const char *filename, const char *fmt, const char *base_filename, const char *base_fmt, char *options, uint64_t img_size, int flags, bool quiet, @@ -6507,13 +6538,7 @@ void bdrv_img_create(const char *filename, const char *fmt, backing_file = qemu_opt_get(opts, BLOCK_OPT_BACKING_FILE); if (backing_file) { - if (!strcmp(filename, backing_file)) { - error_setg(errp, "Error: Trying to create an image with the " - "same filename as the backing file"); - goto out; - } - if (backing_file[0] == '\0') { - error_setg(errp, "Expected backing file name, got empty string"); + if (!validate_backing_file(filename, backing_file, errp)) { goto out; } }