Received: by 2002:a6b:fb09:0:0:0:0:0 with SMTP id h9csp437931iog; Mon, 13 Jun 2022 06:02:15 -0700 (PDT) X-Google-Smtp-Source: ABdhPJz1ee5Kumra27dct5vg4IEFi+KfiuLE+Qzq/tShVYJQB6TiqT6/V6MdAbIAV2ssdsf0OW+v X-Received: by 2002:a63:1b26:0:b0:3fd:8db8:9602 with SMTP id b38-20020a631b26000000b003fd8db89602mr38884177pgb.239.1655125335416; Mon, 13 Jun 2022 06:02:15 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1655125335; cv=none; d=google.com; s=arc-20160816; b=UZA0QbLV/831mw+jKDpnpBgTYJ9BvQIj7SL3BFQEaGe/qwhqtSDGjLj1aLHYt5u5aC 7phZ6UnGbfb3/qZLAChOdPplc4YHWu10HERZp/Edsrlaro//wB3n7MTSJOGveVtsl3PY N8zD4a/QDQoxi1Y3uJyg4FkQBdVA7pPAIaNW+Tr/tFBFQwYR24vLhw/wHFeQkRReuSam NG4R3fQY4qpF/UA5eC5P5sLSn/E5oJy4SmnbtD7nrbSrfIN1SR/N5jcpL1eRltR965wd Rzf1LFCL/+BUiog8ot3sepTrIP+Alk3LXw4hQj9KNcuct9zMkRAoERg4X4b6dq9G39OG fkeQ== 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 :user-agent:references:in-reply-to:message-id:date:subject:cc:to :from:dkim-signature; bh=2wrsDtc02M7D6DEVg78f9rkx202UkfB8nPu+N0gjFL8=; b=h5DLXRYEMWHYkHHuT/iUMwjY+rc9HZnYTFOf8iZJFJZvPsVavhE7fmKlanwa9xOWEN OBiJWreODpOlvjxD6l/yzpbQocXbekKiTTmAvoVYUYqCpj138cFfLzcFM+QKSS6zIRTY /1/hGPj5hiMmjgpkvyXqu+rpd0w+9LfbnW9k5mZ0MWjGG4/5JE/pjToTwE6PVbug6+/Z I6i8uw33zC+c81xe7uxCqP52f0sOmVnIq/ZzrVrDn4FTfjMhiDBtS9oBhPyYNF/xYHdK SP4o5WDmYtPW6Vmievt6ZLpVF1jycvXcFoxUOiWX3oIZpoEYF/5Yqb7x39jL7cG0pCD5 afZw== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@linuxfoundation.org header.s=korg header.b=KmueuwyB; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=linuxfoundation.org Return-Path: Received: from out1.vger.email (out1.vger.email. [2620:137:e000::1:20]) by mx.google.com with ESMTP id s6-20020a63d046000000b003fe4da82aa6si9176696pgi.744.2022.06.13.06.02.00; Mon, 13 Jun 2022 06:02:15 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) client-ip=2620:137:e000::1:20; Authentication-Results: mx.google.com; dkim=pass header.i=@linuxfoundation.org header.s=korg header.b=KmueuwyB; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=linuxfoundation.org Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1353976AbiFML0p (ORCPT + 99 others); Mon, 13 Jun 2022 07:26:45 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:56286 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1353647AbiFMLTx (ORCPT ); Mon, 13 Jun 2022 07:19:53 -0400 Received: from dfw.source.kernel.org (dfw.source.kernel.org [139.178.84.217]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 3107B3B3D0; Mon, 13 Jun 2022 03:41:31 -0700 (PDT) Received: from smtp.kernel.org (relay.kernel.org [52.25.139.140]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by dfw.source.kernel.org (Postfix) with ESMTPS id A6C2C6120F; Mon, 13 Jun 2022 10:41:30 +0000 (UTC) Received: by smtp.kernel.org (Postfix) with ESMTPSA id B6E50C34114; Mon, 13 Jun 2022 10:41:29 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=linuxfoundation.org; s=korg; t=1655116890; bh=uhAikVW3+fGYdSEq4++jX+IFxCaKQkM/o+8pQs6LtkI=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=KmueuwyBtlBkKfKw0XaDWdf4aJ2aXcLQbUu7s29qDXdJkl6IkGM/JtDqUJUYto+p9 r6ITgWTtC2DjGMAoZJLQBUpj/Bwf7g34gWJRlXFYgcHLjWh8jkDGX9mB/1ISqb/+fv qAN9wImcPbKzmv2WdQyUW/len6vFpos7LZAEvn7Q= From: Greg Kroah-Hartman To: linux-kernel@vger.kernel.org Cc: Greg Kroah-Hartman , stable@vger.kernel.org, Ming Yan , Chao Yu , Jaegeuk Kim Subject: [PATCH 5.4 210/411] f2fs: fix to do sanity check on block address in f2fs_do_zero_range() Date: Mon, 13 Jun 2022 12:08:03 +0200 Message-Id: <20220613094934.953482107@linuxfoundation.org> X-Mailer: git-send-email 2.36.1 In-Reply-To: <20220613094928.482772422@linuxfoundation.org> References: <20220613094928.482772422@linuxfoundation.org> User-Agent: quilt/0.66 MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit X-Spam-Status: No, score=-8.3 required=5.0 tests=BAYES_00,DKIMWL_WL_HIGH, DKIM_SIGNED,DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,RCVD_IN_DNSWL_HI, SPF_HELO_NONE,SPF_PASS,T_SCC_BODY_TEXT_LINE autolearn=ham autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on lindbergh.monkeyblade.net Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org From: Chao Yu commit 25f8236213a91efdf708b9d77e9e51b6fc3e141c upstream. As Yanming reported in bugzilla: https://bugzilla.kernel.org/show_bug.cgi?id=215894 I have encountered a bug in F2FS file system in kernel v5.17. I have uploaded the system call sequence as case.c, and a fuzzed image can be found in google net disk The kernel should enable CONFIG_KASAN=y and CONFIG_KASAN_INLINE=y. You can reproduce the bug by running the following commands: kernel BUG at fs/f2fs/segment.c:2291! Call Trace: f2fs_invalidate_blocks+0x193/0x2d0 f2fs_fallocate+0x2593/0x4a70 vfs_fallocate+0x2a5/0xac0 ksys_fallocate+0x35/0x70 __x64_sys_fallocate+0x8e/0xf0 do_syscall_64+0x3b/0x90 entry_SYSCALL_64_after_hwframe+0x44/0xae The root cause is, after image was fuzzed, block mapping info in inode will be inconsistent with SIT table, so in f2fs_fallocate(), it will cause panic when updating SIT with invalid blkaddr. Let's fix the issue by adding sanity check on block address before updating SIT table with it. Cc: stable@vger.kernel.org Reported-by: Ming Yan Signed-off-by: Chao Yu Signed-off-by: Jaegeuk Kim Signed-off-by: Greg Kroah-Hartman --- fs/f2fs/file.c | 16 ++++++++++++---- 1 file changed, 12 insertions(+), 4 deletions(-) --- a/fs/f2fs/file.c +++ b/fs/f2fs/file.c @@ -1320,11 +1320,19 @@ static int f2fs_do_zero_range(struct dno ret = -ENOSPC; break; } - if (dn->data_blkaddr != NEW_ADDR) { - f2fs_invalidate_blocks(sbi, dn->data_blkaddr); - dn->data_blkaddr = NEW_ADDR; - f2fs_set_data_blkaddr(dn); + + if (dn->data_blkaddr == NEW_ADDR) + continue; + + if (!f2fs_is_valid_blkaddr(sbi, dn->data_blkaddr, + DATA_GENERIC_ENHANCE)) { + ret = -EFSCORRUPTED; + break; } + + f2fs_invalidate_blocks(sbi, dn->data_blkaddr); + dn->data_blkaddr = NEW_ADDR; + f2fs_set_data_blkaddr(dn); } f2fs_update_extent_cache_range(dn, start, 0, index - start);