Received: by 2002:a05:6a10:22f:0:0:0:0 with SMTP id 15csp3437306pxk; Mon, 28 Sep 2020 18:25:50 -0700 (PDT) X-Google-Smtp-Source: ABdhPJy7JW6xnvib8mikkLtMD3oHAPITGw/b5PASZIkBlDZq5Wc+abWoCZzCs3IdjVEy8b+iWTW+ X-Received: by 2002:a50:f69a:: with SMTP id d26mr736227edn.21.1601342750472; Mon, 28 Sep 2020 18:25:50 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1601342750; cv=none; d=google.com; s=arc-20160816; b=rCOFE/h8qwacmeErPy/YyLoRs4o4oA9F+gUY+iO+d2OWPuI3W9q40MrIeVKIe2fUbB g/Y4VYCBLCXNpW35/5ARBA8JrPZLlDYVXOdzKIuy0g4wZr1KlnQlrzW22SOYNjgMP9xo E3oZGnlhZwQweAXQFjEpGKmDTYqjU1ipnoD+yt7RPMMEGL7cnjru67cxtOYlIkC2xsiq A4i2qKHjZTkm0trf24swBAPLcn2y5QW26QsjpNWSfv1fak4wgcHygc8K+7gpm9MVl622 yfGsUimuSzIbyjmAJ/DFhSH30618LF3c59rzErvZ+AQzGNq2ErGMszAVko5d9L44XSaZ ysYQ== 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=DFDFKCwe5Nwx/1hQRI/1ppM7YCmG6iWgQOjedu7ub70=; b=ADPcZIPLclewv/NuNvdU+/09M2aG5BX4EapTqwHiosb8Zwapj2/7rcKvVpxCs62y7H GKkiMqP352HbDvgUq8nNQF0BkK53GM4r+/mVjoE4d4GD8SCqcstpCyUKrTQL/nkdH8C2 Mxhr0BGSiyWMenJaFn7VqxTajvXm1OZqwjeGYTeXhjTeW6QB5TUVleGWxauU5UirT4n+ wrKu8lolMbI8zEUndqeMutCgmwbNkBU0s+dvQfLDEwo8B2VOuDJyZbRCEk5HHT54JGKg YhC/1/dvcDNFDAVZswU3v4pXd8RZe36/ob4VmcaSFE6gn823FGqVo3Ul9CpHFdnUrvk3 xJWg== 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 x20si1815518edv.348.2020.09.28.18.25.21; Mon, 28 Sep 2020 18:25:50 -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 S1727219AbgI2BXs (ORCPT + 99 others); Mon, 28 Sep 2020 21:23:48 -0400 Received: from szxga05-in.huawei.com ([45.249.212.191]:14696 "EHLO huawei.com" rhost-flags-OK-OK-OK-FAIL) by vger.kernel.org with ESMTP id S1725272AbgI2BXs (ORCPT ); Mon, 28 Sep 2020 21:23:48 -0400 Received: from DGGEMS406-HUB.china.huawei.com (unknown [172.30.72.58]) by Forcepoint Email with ESMTP id 3F2D3A213072299B3644; Tue, 29 Sep 2020 09:23:47 +0800 (CST) Received: from szvp000203569.huawei.com (10.120.216.130) by DGGEMS406-HUB.china.huawei.com (10.3.19.206) with Microsoft SMTP Server id 14.3.487.0; Tue, 29 Sep 2020 09:23:40 +0800 From: Chao Yu To: CC: , , , Chao Yu , Subject: [PATCH v2] f2fs: fix to do sanity check on segment/section count Date: Tue, 29 Sep 2020 09:23:34 +0800 Message-ID: <20200929012334.109708-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(). Reported-by: syzbot+481a3ffab50fed41dcc0@syzkaller.appspotmail.com Signed-off-by: Chao Yu --- v2: - add reported-by tag. 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