From patchwork Tue Jan 6 09:30:44 2015 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Qu Wenruo X-Patchwork-Id: 5572261 Return-Path: X-Original-To: patchwork-linux-btrfs@patchwork.kernel.org Delivered-To: patchwork-parsemail@patchwork2.web.kernel.org Received: from mail.kernel.org (mail.kernel.org [198.145.29.136]) by patchwork2.web.kernel.org (Postfix) with ESMTP id 90DFABF6C3 for ; Tue, 6 Jan 2015 09:30:57 +0000 (UTC) Received: from mail.kernel.org (localhost [127.0.0.1]) by mail.kernel.org (Postfix) with ESMTP id B382C201BC for ; Tue, 6 Jan 2015 09:30:56 +0000 (UTC) Received: from vger.kernel.org (vger.kernel.org [209.132.180.67]) by mail.kernel.org (Postfix) with ESMTP id B4BCF20166 for ; Tue, 6 Jan 2015 09:30:55 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1753969AbbAFJau (ORCPT ); Tue, 6 Jan 2015 04:30:50 -0500 Received: from cn.fujitsu.com ([59.151.112.132]:13782 "EHLO heian.cn.fujitsu.com" rhost-flags-OK-FAIL-OK-FAIL) by vger.kernel.org with ESMTP id S1751510AbbAFJar (ORCPT ); Tue, 6 Jan 2015 04:30:47 -0500 X-IronPort-AV: E=Sophos;i="5.04,848,1406563200"; d="scan'208";a="55604424" Received: from unknown (HELO edo.cn.fujitsu.com) ([10.167.33.5]) by heian.cn.fujitsu.com with ESMTP; 06 Jan 2015 17:27:21 +0800 Received: from G08CNEXCHPEKD02.g08.fujitsu.local (localhost.localdomain [127.0.0.1]) by edo.cn.fujitsu.com (8.14.3/8.13.1) with ESMTP id t069UDBg016362 for ; Tue, 6 Jan 2015 17:30:13 +0800 Received: from localhost.localdomain (10.167.226.33) by G08CNEXCHPEKD02.g08.fujitsu.local (10.167.33.89) with Microsoft SMTP Server (TLS) id 14.3.181.6; Tue, 6 Jan 2015 17:30:50 +0800 From: Qu Wenruo To: Subject: [PATCH v2] btrfs-progs: Fix a buffer overflow causing segfault in fstests/btrfs/069 Date: Tue, 6 Jan 2015 17:30:44 +0800 Message-ID: <1420536644-12152-1-git-send-email-quwenruo@cn.fujitsu.com> X-Mailer: git-send-email 2.2.1 MIME-Version: 1.0 X-Originating-IP: [10.167.226.33] Sender: linux-btrfs-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-btrfs@vger.kernel.org X-Spam-Status: No, score=-6.9 required=5.0 tests=BAYES_00, RCVD_IN_DNSWL_HI, T_RP_MATCHES_RCVD, UNPARSEABLE_RELAY autolearn=ham version=3.3.1 X-Spam-Checker-Version: SpamAssassin 3.3.1 (2010-03-16) on mail.kernel.org X-Virus-Scanned: ClamAV using ClamSMTP The newly introduced search_chunk_tree_for_fs_info() won't count devid 0 in fi_arg->num_devices, which will cause buffer overflow since later get_device_info() will fill di_args with devid. This can be trigger by fstests/btrfs/069 and any operations needs to iterate over all the devices like 'fi show' or 'dev stat' while replacing. The fix is do an extra probe specifically for devid 0 after search_chunk_tree_for_fs_info() and change num_devices if needed. Reported-by: Tsutomu Itoh Signed-off-by: Qu Wenruo Signed-off-by: Gui Hecheng --- utils.c | 17 +++++++++++++++++ 1 file changed, 17 insertions(+) diff --git a/utils.c b/utils.c index af0a8fe..6581568 100644 --- a/utils.c +++ b/utils.c @@ -1934,8 +1934,10 @@ int get_fs_info(char *path, struct btrfs_ioctl_fs_info_args *fi_args, int ret = 0; int ndevs = 0; int i = 0; + int replacing = 0; struct btrfs_fs_devices *fs_devices_mnt = NULL; struct btrfs_ioctl_dev_info_args *di_args; + struct btrfs_ioctl_dev_info_args tmp; char mp[BTRFS_PATH_NAME_MAX + 1]; DIR *dirstream = NULL; @@ -2003,6 +2005,19 @@ int get_fs_info(char *path, struct btrfs_ioctl_fs_info_args *fi_args, ret = search_chunk_tree_for_fs_info(fd, fi_args); if (ret) goto out; + + /* + * search_chunk_tree_for_fs_info() will lacks the devid 0 + * so manual probe for it here. + */ + ret = get_device_info(fd, 0, &tmp); + if (!ret) { + fi_args->num_devices++; + ndevs++; + replacing = 1; + if (i == 0) + i++; + } } if (!fi_args->num_devices) @@ -2014,6 +2029,8 @@ int get_fs_info(char *path, struct btrfs_ioctl_fs_info_args *fi_args, goto out; } + if (replacing) + memcpy(di_args, &tmp, sizeof(tmp)); for (; i <= fi_args->max_id; ++i) { ret = get_device_info(fd, i, &di_args[ndevs]); if (ret == -ENODEV)