Received: by 2002:a25:824b:0:0:0:0:0 with SMTP id d11csp5849492ybn; Sun, 29 Sep 2019 07:03:17 -0700 (PDT) X-Google-Smtp-Source: APXvYqxWyY8jRpgAbHrT+rpxgZdvNU6Q1HhEpExdccx+Z77ijJjvwYKRg8dYH2Nob3BbbqhB5KPp X-Received: by 2002:a17:906:27cf:: with SMTP id k15mr15376602ejc.150.1569765797746; Sun, 29 Sep 2019 07:03:17 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1569765797; cv=none; d=google.com; s=arc-20160816; b=Lr6UsIiJK9qiAgRdj3JYlZHZOq/XJDfaAWuo1jLDnxj6E50CPl3MrZrgOrYixrQyUX hG30HfdRcjxd4U5iKC2moLzXJlcsJtKC5ZnmTv8j1WVh/lrBf62mUFm7HquNmxD0HfkU B2mx+VXu9RNXFmmgLuHzpgQzEOVVOQtP5/H7Vv43dV5U7BuQY76CTSnEZ0CPSpgkRtRI OxsTiXWO0yMY/963AxP9CUj4V/xSIK/m//Bw9f4rKbAxQPCGFdYFXtGn/K/9SoR8abrl SDwBUlnUl1aWf2H21JMJMdinu7t+6rpZs6RwU446eShzMtVL2adA92grnZOROvESmMDL bFLw== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:sender:content-transfer-encoding:mime-version :user-agent:references:in-reply-to:message-id:date:subject:cc:to :from:dkim-signature; bh=VTJ7+hzZj9SvmYuseSM8wAz5JpvErlSoKY7U4iii/F4=; b=EZyzJIML5Dfj5mRqZagwbk5UCpGjOOUHAHr7LmhAb6VHYEh+Q0PRus5dRAO4nEBMvg CvNR6MlW03KltC0iHfYJ4fSBMkj/FEzYxXqLN4HBnzgZXZLE/tKujHNL+RyzgjShuhFk MdD4Fm1OvD39hJzD4V4v+7pBVl4fmQig8Pf1SIEfgLmNH+rOdpYVPDIllVkOZfEASf0r 9bMUAcL1JlAkNpcWZMwIisDAzlEos60BbKKy+IKl6e+1NGJOJKb+IPTDrUk0vS8CaRWu 1Dt+d1BYFQK4qOO3p5XvxufeC+liUy4z8HuaDPR8Sdv1c0i/ixrq0zMJZ1Yf9QJufAbO /UrQ== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@kernel.org header.s=default header.b=b2yDbYC1; spf=pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org Return-Path: Received: from vger.kernel.org (vger.kernel.org. [209.132.180.67]) by mx.google.com with ESMTP id o49si5357998edc.261.2019.09.29.07.02.53; Sun, 29 Sep 2019 07:03:17 -0700 (PDT) Received-SPF: pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) client-ip=209.132.180.67; Authentication-Results: mx.google.com; dkim=pass header.i=@kernel.org header.s=default header.b=b2yDbYC1; spf=pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1729464AbfI2OBm (ORCPT + 99 others); Sun, 29 Sep 2019 10:01:42 -0400 Received: from mail.kernel.org ([198.145.29.99]:43974 "EHLO mail.kernel.org" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1730064AbfI2OBk (ORCPT ); Sun, 29 Sep 2019 10:01:40 -0400 Received: from localhost (83-86-89-107.cable.dynamic.v4.ziggo.nl [83.86.89.107]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by mail.kernel.org (Postfix) with ESMTPSA id 04B8B2082F; Sun, 29 Sep 2019 14:01:37 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=default; t=1569765698; bh=a0PbLUSHOQeGFwj/sxkijENQgTYWHyWj7f+/YAYp85s=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=b2yDbYC1wWQ0FajWDkSQmjwV+CufsvJM0xV7SaHMgGo8r3Xx4nHtMznRq+u4c9Jap PV4cnleT4JF2UghOv5oUlnb1BQAoqO9V7MnibZW23Gcgzzst790dIw6vVkm+lauV1t bD2fjtUg/rU8nXMOTZipZoQkF5phjYeMU7U1Zw5s= From: Greg Kroah-Hartman To: linux-kernel@vger.kernel.org Cc: Greg Kroah-Hartman , stable@vger.kernel.org, Chao Yu , Jaegeuk Kim , Sasha Levin Subject: [PATCH 5.2 33/45] f2fs: fix to do sanity check on segment bitmap of LFS curseg Date: Sun, 29 Sep 2019 15:56:01 +0200 Message-Id: <20190929135032.339459211@linuxfoundation.org> X-Mailer: git-send-email 2.23.0 In-Reply-To: <20190929135024.387033930@linuxfoundation.org> References: <20190929135024.387033930@linuxfoundation.org> User-Agent: quilt/0.66 MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Sender: linux-kernel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org From: Chao Yu [ Upstream commit c854f4d681365498f53ba07843a16423625aa7e9 ] As Jungyeon Reported in bugzilla: https://bugzilla.kernel.org/show_bug.cgi?id=203233 - Reproduces gcc poc_13.c ./run.sh f2fs - Kernel messages F2FS-fs (sdb): Bitmap was wrongly set, blk:4608 kernel BUG at fs/f2fs/segment.c:2133! RIP: 0010:update_sit_entry+0x35d/0x3e0 Call Trace: f2fs_allocate_data_block+0x16c/0x5a0 do_write_page+0x57/0x100 f2fs_do_write_node_page+0x33/0xa0 __write_node_page+0x270/0x4e0 f2fs_sync_node_pages+0x5df/0x670 f2fs_write_checkpoint+0x364/0x13a0 f2fs_sync_fs+0xa3/0x130 f2fs_do_sync_file+0x1a6/0x810 do_fsync+0x33/0x60 __x64_sys_fsync+0xb/0x10 do_syscall_64+0x43/0x110 entry_SYSCALL_64_after_hwframe+0x44/0xa9 The testcase fails because that, in fuzzed image, current segment was allocated with LFS type, its .next_blkoff should point to an unused block address, but actually, its bitmap shows it's not. So during allocation, f2fs crash when setting bitmap. Introducing sanity_check_curseg() to check such inconsistence of current in-used segment. Signed-off-by: Chao Yu Signed-off-by: Jaegeuk Kim Signed-off-by: Sasha Levin --- fs/f2fs/segment.c | 39 +++++++++++++++++++++++++++++++++++++++ 1 file changed, 39 insertions(+) diff --git a/fs/f2fs/segment.c b/fs/f2fs/segment.c index 291f7106537c7..0a6be0212e390 100644 --- a/fs/f2fs/segment.c +++ b/fs/f2fs/segment.c @@ -4242,6 +4242,41 @@ static int build_dirty_segmap(struct f2fs_sb_info *sbi) return init_victim_secmap(sbi); } +static int sanity_check_curseg(struct f2fs_sb_info *sbi) +{ + int i; + + /* + * In LFS/SSR curseg, .next_blkoff should point to an unused blkaddr; + * In LFS curseg, all blkaddr after .next_blkoff should be unused. + */ + for (i = 0; i < NO_CHECK_TYPE; i++) { + struct curseg_info *curseg = CURSEG_I(sbi, i); + struct seg_entry *se = get_seg_entry(sbi, curseg->segno); + unsigned int blkofs = curseg->next_blkoff; + + if (f2fs_test_bit(blkofs, se->cur_valid_map)) + goto out; + + if (curseg->alloc_type == SSR) + continue; + + for (blkofs += 1; blkofs < sbi->blocks_per_seg; blkofs++) { + if (!f2fs_test_bit(blkofs, se->cur_valid_map)) + continue; +out: + f2fs_msg(sbi->sb, KERN_ERR, + "Current segment's next free block offset is " + "inconsistent with bitmap, logtype:%u, " + "segno:%u, type:%u, next_blkoff:%u, blkofs:%u", + i, curseg->segno, curseg->alloc_type, + curseg->next_blkoff, blkofs); + return -EINVAL; + } + } + return 0; +} + /* * Update min, max modified time for cost-benefit GC algorithm */ @@ -4337,6 +4372,10 @@ int f2fs_build_segment_manager(struct f2fs_sb_info *sbi) if (err) return err; + err = sanity_check_curseg(sbi); + if (err) + return err; + init_min_max_mtime(sbi); return 0; } -- 2.20.1