From patchwork Tue Aug 30 03:29:32 2016 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Qu Wenruo X-Patchwork-Id: 9304777 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 E7B9860756 for ; Tue, 30 Aug 2016 03:29:49 +0000 (UTC) Received: from mail.wl.linuxfoundation.org (localhost [127.0.0.1]) by mail.wl.linuxfoundation.org (Postfix) with ESMTP id DD94328A71 for ; Tue, 30 Aug 2016 03:29:49 +0000 (UTC) Received: by mail.wl.linuxfoundation.org (Postfix, from userid 486) id CDB0D28A90; Tue, 30 Aug 2016 03:29:49 +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 vger.kernel.org (vger.kernel.org [209.132.180.67]) by mail.wl.linuxfoundation.org (Postfix) with ESMTP id 455AC28A71 for ; Tue, 30 Aug 2016 03:29:49 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1756246AbcH3D3p (ORCPT ); Mon, 29 Aug 2016 23:29:45 -0400 Received: from cn.fujitsu.com ([59.151.112.132]:46487 "EHLO heian.cn.fujitsu.com" rhost-flags-OK-FAIL-OK-FAIL) by vger.kernel.org with ESMTP id S1754321AbcH3D3p (ORCPT ); Mon, 29 Aug 2016 23:29:45 -0400 X-IronPort-AV: E=Sophos;i="5.22,518,1449504000"; d="scan'208";a="10410997" Received: from unknown (HELO cn.fujitsu.com) ([10.167.33.5]) by heian.cn.fujitsu.com with ESMTP; 30 Aug 2016 11:29:35 +0800 Received: from G08CNEXCHPEKD02.g08.fujitsu.local (unknown [10.167.33.83]) by cn.fujitsu.com (Postfix) with ESMTP id 8CEC342EB26E for ; Tue, 30 Aug 2016 11:29:35 +0800 (CST) Received: from localhost.localdomain (10.167.226.34) by G08CNEXCHPEKD02.g08.fujitsu.local (10.167.33.89) with Microsoft SMTP Server (TLS) id 14.3.279.2; Tue, 30 Aug 2016 11:29:34 +0800 From: Qu Wenruo To: Subject: [PATCH 1/2] btrfs-progs: fsck: Do early check for read_tree_block Date: Tue, 30 Aug 2016 11:29:32 +0800 Message-ID: <20160830032933.22194-1-quwenruo@cn.fujitsu.com> X-Mailer: git-send-email 2.9.3 MIME-Version: 1.0 X-Originating-IP: [10.167.226.34] X-yoursite-MailScanner-ID: 8CEC342EB26E.A1A66 X-yoursite-MailScanner: Found to be clean X-yoursite-MailScanner-From: quwenruo@cn.fujitsu.com Sender: linux-btrfs-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-btrfs@vger.kernel.org X-Virus-Scanned: ClamAV using ClamSMTP Although we have enhanced read_tree_block() from a lot of different aspects, it lacks the early bytenr/blocksize alignment check. And the lack of such check can lead to strange use-after-free bugs, due to the fact that alloc_extent_buffer() will free overlapping extent buffers, and allocate new eb for the usage. So we should not allow invalid bytenr/blocksize even passed to btrfs_find_create_tree_block(). This patch will add such check so we won't trigger use-after-free bug then. Reported-by: Lukas Lueg Signed-off-by: Qu Wenruo --- disk-io.c | 18 ++++++++++++++++++ 1 file changed, 18 insertions(+) diff --git a/disk-io.c b/disk-io.c index ca4578f..f5340c3 100644 --- a/disk-io.c +++ b/disk-io.c @@ -313,11 +313,29 @@ struct extent_buffer* read_tree_block_fs_info( int ret; struct extent_buffer *eb; u64 best_transid = 0; + u32 sectorsize = btrfs_super_sectorsize(fs_info->super_copy); + u32 nodesize = btrfs_super_nodesize(fs_info->super_copy); int mirror_num = 0; int good_mirror = 0; int num_copies; int ignore = 0; + /* + * Don't even try to create tree block for unaligned tree block + * bytenr. + * Such unaligned tree block will free overlapping extent buffer, + * causing use-after-free bugs for fuzzed images. + */ + if (!IS_ALIGNED(bytenr, sectorsize)) { + error("tree block bytenr %llu is not aligned to sectorsize %u", + bytenr, sectorsize); + return ERR_PTR(-EIO); + } + if (!IS_ALIGNED(blocksize, nodesize)) { + error("tree block size %u is not aligned to nodesize %u", + blocksize, nodesize); + return ERR_PTR(-EIO); + } eb = btrfs_find_create_tree_block(fs_info, bytenr, blocksize); if (!eb) return ERR_PTR(-ENOMEM);