From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org X-Spam-Level: X-Spam-Status: No, score=-19.7 required=3.0 tests=BAYES_00,DKIMWL_WL_HIGH, DKIM_SIGNED,DKIM_VALID,DKIM_VALID_AU,INCLUDES_CR_TRAILER,INCLUDES_PATCH, MAILING_LIST_MULTI,SPF_HELO_NONE,SPF_PASS,USER_AGENT_GIT autolearn=unavailable autolearn_force=no version=3.4.0 Received: from mail.kernel.org (mail.kernel.org [198.145.29.99]) by smtp.lore.kernel.org (Postfix) with ESMTP id 0BD6BC19F31 for ; Tue, 3 Aug 2021 11:44:35 +0000 (UTC) Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by mail.kernel.org (Postfix) with ESMTP id E85106105A for ; Tue, 3 Aug 2021 11:44:34 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S235874AbhHCLom (ORCPT ); Tue, 3 Aug 2021 07:44:42 -0400 Received: from mail.kernel.org ([198.145.29.99]:35016 "EHLO mail.kernel.org" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S235844AbhHCLoI (ORCPT ); Tue, 3 Aug 2021 07:44:08 -0400 Received: by mail.kernel.org (Postfix) with ESMTPSA id 9BD4360ED6; Tue, 3 Aug 2021 11:43:56 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1627991037; bh=juMD/gwYa8eKOqbZs0IYqt0t+hpwfZ8KU6t6B3GM0Iw=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=XiLbcF2Rdm1IXtDNpmMBNnALvIMeMOxgxyhx9FHIbicmjhnRQVa/LEOCBkUZuxL3O J7CK+ivfPgCoe7FHOleICphT/4+84dC7lwVi8/EdO9pyV5eBpl/SOk4pDJ0ozlKgZN WZ4EISUzKK5SoyEVXBKSzx0gu4cZhauBt1X2ojNP1vEDui3UwwuLUzdGFLaycQJF+Q 3EgKrwQqj1XlqfS+gj9ugSCzH6fnzUBJJFMSB5mbTHqJEmf8sBmJPgq2DrT2pHceqZ xaIXcFuwRiNq8XWJn9wUt239SB+KValGyqLmrCbccVmX82jULuPsKrBBxuI83u3Jaz KuiTrJUNDR0Sg== From: Sasha Levin To: linux-kernel@vger.kernel.org, stable@vger.kernel.org Cc: Shreyansh Chouhan , syzbot+c31a48e6702ccb3d64c9@syzkaller.appspotmail.com, Jan Kara , Sasha Levin , reiserfs-devel@vger.kernel.org Subject: [PATCH AUTOSEL 5.13 03/11] reiserfs: check directory items on read from disk Date: Tue, 3 Aug 2021 07:43:44 -0400 Message-Id: <20210803114352.2252544-3-sashal@kernel.org> X-Mailer: git-send-email 2.30.2 In-Reply-To: <20210803114352.2252544-1-sashal@kernel.org> References: <20210803114352.2252544-1-sashal@kernel.org> MIME-Version: 1.0 X-stable: review X-Patchwork-Hint: Ignore Content-Transfer-Encoding: 8bit Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org From: Shreyansh Chouhan [ Upstream commit 13d257503c0930010ef9eed78b689cec417ab741 ] While verifying the leaf item that we read from the disk, reiserfs doesn't check the directory items, this could cause a crash when we read a directory item from the disk that has an invalid deh_location. This patch adds a check to the directory items read from the disk that does a bounds check on deh_location for the directory entries. Any directory entry header with a directory entry offset greater than the item length is considered invalid. Link: https://lore.kernel.org/r/20210709152929.766363-1-chouhan.shreyansh630@gmail.com Reported-by: syzbot+c31a48e6702ccb3d64c9@syzkaller.appspotmail.com Signed-off-by: Shreyansh Chouhan Signed-off-by: Jan Kara Signed-off-by: Sasha Levin --- fs/reiserfs/stree.c | 31 ++++++++++++++++++++++++++----- 1 file changed, 26 insertions(+), 5 deletions(-) diff --git a/fs/reiserfs/stree.c b/fs/reiserfs/stree.c index 476a7ff49482..ef42729216d1 100644 --- a/fs/reiserfs/stree.c +++ b/fs/reiserfs/stree.c @@ -387,6 +387,24 @@ void pathrelse(struct treepath *search_path) search_path->path_length = ILLEGAL_PATH_ELEMENT_OFFSET; } +static int has_valid_deh_location(struct buffer_head *bh, struct item_head *ih) +{ + struct reiserfs_de_head *deh; + int i; + + deh = B_I_DEH(bh, ih); + for (i = 0; i < ih_entry_count(ih); i++) { + if (deh_location(&deh[i]) > ih_item_len(ih)) { + reiserfs_warning(NULL, "reiserfs-5094", + "directory entry location seems wrong %h", + &deh[i]); + return 0; + } + } + + return 1; +} + static int is_leaf(char *buf, int blocksize, struct buffer_head *bh) { struct block_head *blkh; @@ -454,11 +472,14 @@ static int is_leaf(char *buf, int blocksize, struct buffer_head *bh) "(second one): %h", ih); return 0; } - if (is_direntry_le_ih(ih) && (ih_item_len(ih) < (ih_entry_count(ih) * IH_SIZE))) { - reiserfs_warning(NULL, "reiserfs-5093", - "item entry count seems wrong %h", - ih); - return 0; + if (is_direntry_le_ih(ih)) { + if (ih_item_len(ih) < (ih_entry_count(ih) * IH_SIZE)) { + reiserfs_warning(NULL, "reiserfs-5093", + "item entry count seems wrong %h", + ih); + return 0; + } + return has_valid_deh_location(bh, ih); } prev_location = ih_location(ih); } -- 2.30.2