From patchwork Fri Dec 15 06:26:14 2017 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Yunlong Song X-Patchwork-Id: 10113993 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 53B6760327 for ; Fri, 15 Dec 2017 06:29:27 +0000 (UTC) Received: from mail.wl.linuxfoundation.org (localhost [127.0.0.1]) by mail.wl.linuxfoundation.org (Postfix) with ESMTP id 473FB29E82 for ; Fri, 15 Dec 2017 06:29:27 +0000 (UTC) Received: by mail.wl.linuxfoundation.org (Postfix, from userid 486) id 3C18C29E93; Fri, 15 Dec 2017 06:29:27 +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=unavailable 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 3D24929E82 for ; Fri, 15 Dec 2017 06:29:26 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1753916AbdLOG0f (ORCPT ); Fri, 15 Dec 2017 01:26:35 -0500 Received: from szxga04-in.huawei.com ([45.249.212.190]:11941 "EHLO szxga04-in.huawei.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1753173AbdLOG0d (ORCPT ); Fri, 15 Dec 2017 01:26:33 -0500 Received: from 172.30.72.59 (EHLO DGGEMS411-HUB.china.huawei.com) ([172.30.72.59]) by dggrg04-dlp.huawei.com (MOS 4.4.6-GA FastPath queued) with ESMTP id DMB11269; Fri, 15 Dec 2017 14:26:29 +0800 (CST) Received: from huawei.com (10.107.193.250) by DGGEMS411-HUB.china.huawei.com (10.3.19.211) with Microsoft SMTP Server id 14.3.361.1; Fri, 15 Dec 2017 14:26:19 +0800 From: Yunlong Song To: , , , , CC: , , , , , , Subject: [PATCH] fsck.f2fs: check nid range before use to avoid segmentation fault Date: Fri, 15 Dec 2017 14:26:14 +0800 Message-ID: <1513319174-114303-1-git-send-email-yunlong.song@huawei.com> X-Mailer: git-send-email 1.8.5.2 MIME-Version: 1.0 X-Originating-IP: [10.107.193.250] X-CFilter-Loop: Reflected X-Mirapoint-Virus-RAPID-Raw: score=unknown(0), refid=str=0001.0A020204.5A336B16.002F, ss=1, re=0.000, recu=0.000, reip=0.000, cl=1, cld=1, fgs=0, ip=0.0.0.0, so=2014-11-16 11:51:01, dmn=2013-03-21 17:37:32 X-Mirapoint-Loop-Id: 7a4d0efa5d72c112994f820f0d6c029b Sender: linux-fsdevel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-fsdevel@vger.kernel.org X-Virus-Scanned: ClamAV using ClamSMTP Signed-off-by: Yunlong Song --- fsck/fsck.c | 11 ++++++++++- 1 file changed, 10 insertions(+), 1 deletion(-) diff --git a/fsck/fsck.c b/fsck/fsck.c index 11b8b0b..2212aa3 100644 --- a/fsck/fsck.c +++ b/fsck/fsck.c @@ -14,6 +14,15 @@ char *tree_mark; uint32_t tree_mark_size = 256; +static inline int check_nid_range(struct f2fs_sb_info *sbi, nid_t nid) +{ + if (nid < F2FS_ROOT_INO(sbi)) + return -EINVAL; + if (nid >= NM_I(sbi)->max_nid) + return -EINVAL; + return 0; +} + int f2fs_set_main_bitmap(struct f2fs_sb_info *sbi, u32 blk, int type) { struct f2fs_fsck *fsck = F2FS_FSCK(sbi); @@ -740,7 +749,7 @@ void fsck_chk_inode_blk(struct f2fs_sb_info *sbi, u32 nid, for (idx = 0; idx < 5; idx++) { u32 nid = le32_to_cpu(node_blk->i.i_nid[idx]); - if (nid != 0) { + if (nid != 0 && !check_nid_range(sbi, nid)) { struct node_info ni; get_node_info(sbi, nid, &ni);