From patchwork Wed Apr 5 08:59:14 2017 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Lu Fengqi X-Patchwork-Id: 9663345 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 809A860353 for ; Wed, 5 Apr 2017 08:59:31 +0000 (UTC) Received: from mail.wl.linuxfoundation.org (localhost [127.0.0.1]) by mail.wl.linuxfoundation.org (Postfix) with ESMTP id 6E49C2858C for ; Wed, 5 Apr 2017 08:59:31 +0000 (UTC) Received: by mail.wl.linuxfoundation.org (Postfix, from userid 486) id 62B622859E; Wed, 5 Apr 2017 08:59:31 +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 CF56C28595 for ; Wed, 5 Apr 2017 08:59:30 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S932639AbdDEI7X (ORCPT ); Wed, 5 Apr 2017 04:59:23 -0400 Received: from cn.fujitsu.com ([59.151.112.132]:5881 "EHLO heian.cn.fujitsu.com" rhost-flags-OK-FAIL-OK-FAIL) by vger.kernel.org with ESMTP id S932080AbdDEI7V (ORCPT ); Wed, 5 Apr 2017 04:59:21 -0400 X-IronPort-AV: E=Sophos;i="5.22,518,1449504000"; d="scan'208";a="17375760" Received: from unknown (HELO cn.fujitsu.com) ([10.167.33.5]) by heian.cn.fujitsu.com with ESMTP; 05 Apr 2017 16:59:20 +0800 Received: from G08CNEXCHPEKD01.g08.fujitsu.local (unknown [10.167.33.80]) by cn.fujitsu.com (Postfix) with ESMTP id 30BEA49FD53E for ; Wed, 5 Apr 2017 16:59:18 +0800 (CST) Received: from lufq.5F (10.167.225.63) by G08CNEXCHPEKD01.g08.fujitsu.local (10.167.33.89) with Microsoft SMTP Server (TLS) id 14.3.319.2; Wed, 5 Apr 2017 16:59:17 +0800 From: Lu Fengqi To: Subject: [PATCH 1/2] btrfs-progs: dump-super: check array_size in print_sys_chunk_array Date: Wed, 5 Apr 2017 16:59:14 +0800 Message-ID: <20170405085915.21791-1-lufq.fnst@cn.fujitsu.com> X-Mailer: git-send-email 2.12.1 MIME-Version: 1.0 X-Originating-IP: [10.167.225.63] X-yoursite-MailScanner-ID: 30BEA49FD53E.AE9DB X-yoursite-MailScanner: Found to be clean X-yoursite-MailScanner-From: lufq.fnst@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 Without validation of array_size, the dump-super may lead to a bad memory access. Signed-off-by: Lu Fengqi --- cmds-inspect-dump-super.c | 15 +++++++++++---- 1 file changed, 11 insertions(+), 4 deletions(-) diff --git a/cmds-inspect-dump-super.c b/cmds-inspect-dump-super.c index ee2c8e3a..48b5219c 100644 --- a/cmds-inspect-dump-super.c +++ b/cmds-inspect-dump-super.c @@ -62,16 +62,23 @@ static void print_sys_chunk_array(struct btrfs_super_block *sb) struct btrfs_key key; int item; - buf = malloc(sizeof(*buf) + sizeof(*sb)); + buf = malloc(sizeof(*buf) + BTRFS_SUPER_INFO_SIZE); if (!buf) { error("not enough memory"); - goto out; + return; } - write_extent_buffer(buf, sb, 0, sizeof(*sb)); + write_extent_buffer(buf, sb, 0, BTRFS_SUPER_INFO_SIZE); array_size = btrfs_super_sys_array_size(sb); array_ptr = sb->sys_chunk_array; sb_array_offset = offsetof(struct btrfs_super_block, sys_chunk_array); + + if (array_size > BTRFS_SYSTEM_CHUNK_ARRAY_SIZE) { + error("sys_array_size %u shouldn't exceed %u bytes", + array_size, BTRFS_SYSTEM_CHUNK_ARRAY_SIZE); + goto out; + } + cur_offset = 0; item = 0; @@ -124,8 +131,8 @@ static void print_sys_chunk_array(struct btrfs_super_block *sb) item++; } - free(buf); out: + free(buf); return; out_short_read: