Received: by 2002:a05:6a10:22f:0:0:0:0 with SMTP id 15csp2884961pxk; Mon, 28 Sep 2020 02:44:13 -0700 (PDT) X-Google-Smtp-Source: ABdhPJwUlzMt+TzsxWPqw1n2sstldXa64tdJJXUMsUOVg1oTuFYrv1BMCau75KnJMdncNsMIP0qM X-Received: by 2002:a17:906:3494:: with SMTP id g20mr789286ejb.486.1601286253608; Mon, 28 Sep 2020 02:44:13 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1601286253; cv=none; d=google.com; s=arc-20160816; b=OrZzgmZVXUNJuFpbbplYIDKbcih19W8ncQgP7q+RGFaNeSRcVtcrhna6qT7dCzNny7 EJksFWBZXnnUs8ydlb+gP/6e88sP3vM0heqMs5kqMQ96T0rtPkjw6nNmdtzM1yGw/w4o /0PpEXo2Si1qstADQXSM82EI1c1hYRqtMSCKAhON0ZD1Zf3SnrHgD2b/xIJQmBudLBG5 6i8g8c/Z1n8UvWBZKsMZGdBgwRvmO61Yub/djyBG9gOZxYynWcZ7ApYkOJ9o3u7WSBv5 erRBBcjVWmoAde+ySN1H1bQqArn0K8nzLYJO68J1P86abo0aD5/PmFyll09PJuKmNbxN a/nw== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:content-transfer-encoding:mime-version :message-id:date:subject:cc:to:from; bh=js14lOO4nyfb3f026jh74JvOlwcAonZ7mp7TqRtXs/w=; b=Klckd2T3q8qrOQwzG1Ds9XhQaNRO0LMQ1iXGqVY7AEjxF0jgj0frm9biV8B2+GI7dO E3BOgIlqcNh99O7ZxNKNrgQPnFLAoklG8sDBtDIWuf7muxE5cpfQTOtqRRB6uEnd+PzQ YrmVolufEakjPlG/pLN0w5PkzBBfBkvPCML5LR5lqk1mOhf7CIgsaA+kUWzkXOZqQP8R sgZsUn+yuMfhpLiBsnfPcqJFRc4i/EBPDRp5RpkuiXo0z+XIbVAmKNKNWOM4ntgoSX/I KZ+v5mwhgU6OQdDG8WGDFyJiJ57l48f595v86LUViy2bB6RuMKCjuQVjke3BDpJqGacO GgWA== ARC-Authentication-Results: i=1; mx.google.com; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org Return-Path: Received: from vger.kernel.org (vger.kernel.org. [23.128.96.18]) by mx.google.com with ESMTP id dj21si224057edb.310.2020.09.28.02.43.51; Mon, 28 Sep 2020 02:44:13 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) client-ip=23.128.96.18; Authentication-Results: mx.google.com; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1726601AbgI1Jmy (ORCPT + 99 others); Mon, 28 Sep 2020 05:42:54 -0400 Received: from szxga06-in.huawei.com ([45.249.212.32]:34162 "EHLO huawei.com" rhost-flags-OK-OK-OK-FAIL) by vger.kernel.org with ESMTP id S1726497AbgI1Jmy (ORCPT ); Mon, 28 Sep 2020 05:42:54 -0400 Received: from DGGEMS408-HUB.china.huawei.com (unknown [172.30.72.58]) by Forcepoint Email with ESMTP id 6A01EC669EBFA0EF986C; Mon, 28 Sep 2020 17:42:52 +0800 (CST) Received: from szvp000203569.huawei.com (10.120.216.130) by DGGEMS408-HUB.china.huawei.com (10.3.19.208) with Microsoft SMTP Server id 14.3.487.0; Mon, 28 Sep 2020 17:42:43 +0800 From: Chao Yu To: CC: , , , Chao Yu Subject: [PATCH] f2fs: fix to do sanity check on segment/section count Date: Mon, 28 Sep 2020 17:42:39 +0800 Message-ID: <20200928094239.66221-1-yuchao0@huawei.com> X-Mailer: git-send-email 2.26.2 MIME-Version: 1.0 Content-Transfer-Encoding: 7BIT Content-Type: text/plain; charset=US-ASCII X-Originating-IP: [10.120.216.130] X-CFilter-Loop: Reflected Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org As syzbot reported: BUG: KASAN: slab-out-of-bounds in init_min_max_mtime fs/f2fs/segment.c:4710 [inline] BUG: KASAN: slab-out-of-bounds in f2fs_build_segment_manager+0x9302/0xa6d0 fs/f2fs/segment.c:4792 Read of size 8 at addr ffff8880a1b934a8 by task syz-executor682/6878 CPU: 1 PID: 6878 Comm: syz-executor682 Not tainted 5.9.0-rc6-syzkaller #0 Hardware name: Google Google Compute Engine/Google Compute Engine, BIOS Google 01/01/2011 Call Trace: __dump_stack lib/dump_stack.c:77 [inline] dump_stack+0x198/0x1fd lib/dump_stack.c:118 print_address_description.constprop.0.cold+0xae/0x497 mm/kasan/report.c:383 __kasan_report mm/kasan/report.c:513 [inline] kasan_report.cold+0x1f/0x37 mm/kasan/report.c:530 init_min_max_mtime fs/f2fs/segment.c:4710 [inline] f2fs_build_segment_manager+0x9302/0xa6d0 fs/f2fs/segment.c:4792 f2fs_fill_super+0x381a/0x6e80 fs/f2fs/super.c:3633 mount_bdev+0x32e/0x3f0 fs/super.c:1417 legacy_get_tree+0x105/0x220 fs/fs_context.c:592 vfs_get_tree+0x89/0x2f0 fs/super.c:1547 do_new_mount fs/namespace.c:2875 [inline] path_mount+0x1387/0x20a0 fs/namespace.c:3192 do_mount fs/namespace.c:3205 [inline] __do_sys_mount fs/namespace.c:3413 [inline] __se_sys_mount fs/namespace.c:3390 [inline] __x64_sys_mount+0x27f/0x300 fs/namespace.c:3390 do_syscall_64+0x2d/0x70 arch/x86/entry/common.c:46 entry_SYSCALL_64_after_hwframe+0x44/0xa9 The root cause is: if segs_per_sec is larger than one, and segment count in last section is less than segs_per_sec, we will suffer out-of-boundary memory access on sit_i->sentries[] in init_min_max_mtime(). Fix this by adding sanity check among segment count, section count and segs_per_sec value in sanity_check_raw_super(). Signed-off-by: Chao Yu --- fs/f2fs/super.c | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/fs/f2fs/super.c b/fs/f2fs/super.c index a24980aa425d..79d5e0e2225a 100644 --- a/fs/f2fs/super.c +++ b/fs/f2fs/super.c @@ -2834,6 +2834,12 @@ static int sanity_check_raw_super(struct f2fs_sb_info *sbi, return -EFSCORRUPTED; } + if (segment_count_main != total_sections * segs_per_sec) { + f2fs_info(sbi, "Invalid segment/section count (%u != %u * %u)", + segment_count_main, total_sections, segs_per_sec); + return -EFSCORRUPTED; + } + if ((segment_count / segs_per_sec) < total_sections) { f2fs_info(sbi, "Small segment_count (%u < %u * %u)", segment_count, segs_per_sec, total_sections); -- 2.26.2