From patchwork Thu Apr 28 18:31:37 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Jan Kara X-Patchwork-Id: 567472 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by smtp.lore.kernel.org (Postfix) with ESMTP id 0C83EC4332F for ; Thu, 28 Apr 2022 18:31:53 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1351084AbiD1SfG (ORCPT ); Thu, 28 Apr 2022 14:35:06 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:37152 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1351072AbiD1SfD (ORCPT ); Thu, 28 Apr 2022 14:35:03 -0400 Received: from smtp-out1.suse.de (smtp-out1.suse.de [195.135.220.28]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 3CFBFB7C70; Thu, 28 Apr 2022 11:31:46 -0700 (PDT) Received: from relay2.suse.de (relay2.suse.de [149.44.160.134]) by smtp-out1.suse.de (Postfix) with ESMTP id D0C14210EC; Thu, 28 Apr 2022 18:31:44 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=suse.cz; s=susede2_rsa; t=1651170704; h=from:from:reply-to:date:date:message-id:message-id:to:to:cc:cc: mime-version:mime-version: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=rU76PoxE+Dj3VF/hkhaMNzLeRpB+c1clmNsGmixXrr4=; b=Xw0Ai7h8/C6a2xgH6RDFcke4aBdVjuPJ55ZmIJoTFIW35HWn2Lc8lilXK+6w4hCTlLMYlM vtPjNRaNM5ctYtkhHHCWkG354jp/wSmVir+HpBZZU8s91R1/nsOakzgJMkFmD/GdBufX+U aSlm5Fd7xrUMclHxfsJns721AdXzY3M= DKIM-Signature: v=1; a=ed25519-sha256; c=relaxed/relaxed; d=suse.cz; s=susede2_ed25519; t=1651170704; h=from:from:reply-to:date:date:message-id:message-id:to:to:cc:cc: mime-version:mime-version: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=rU76PoxE+Dj3VF/hkhaMNzLeRpB+c1clmNsGmixXrr4=; b=dIB/cbWreIPwvACR5AdBpdEtPLJ+K9r1zaHC5vKjB5cHteHoE6j40hJoqDjD7uDTjyDYQP 70nnDMZ8+AbWUkBA== Received: from quack3.suse.cz (unknown [10.100.224.230]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by relay2.suse.de (Postfix) with ESMTPS id A46CD2C141; Thu, 28 Apr 2022 18:31:44 +0000 (UTC) Received: by quack3.suse.cz (Postfix, from userid 1000) id BC45AA0622; Thu, 28 Apr 2022 20:31:43 +0200 (CEST) From: Jan Kara To: Ted Tso Cc: , Jan Kara , stable@vger.kernel.org Subject: [PATCH 1/2] ext4: Verify dir block before splitting it Date: Thu, 28 Apr 2022 20:31:37 +0200 Message-Id: <20220428183143.5439-1-jack@suse.cz> X-Mailer: git-send-email 2.34.1 In-Reply-To: <20220428180355.15209-1-jack@suse.cz> References: <20220428180355.15209-1-jack@suse.cz> MIME-Version: 1.0 X-Developer-Signature: v=1; a=openpgp-sha256; l=3086; h=from:subject; bh=8mXnUEkGGuPlpirARPzTAkZsUnz11/VA6efu36V6Ork=; b=owEBbQGS/pANAwAIAZydqgc/ZEDZAcsmYgBiat2IqEaQFRgF0jJ2Onet/bqhrxxRgMfCeLkXiJ0H DnG7ztqJATMEAAEIAB0WIQSrWdEr1p4yirVVKBycnaoHP2RA2QUCYmrdiAAKCRCcnaoHP2RA2QvfB/ 9P4y4qOXprD2e+0Cku4ijsKVcOr+hGQYcJ3mUvKqNPI2KLcDnV7RDSBsemg80pfRodYWlnt8feA8gd EQwdDdrfpx3j3Lc8mOz6RYWmb1A5aZJfgGBzu1pP8YTDHoBUdK+mJzq2/78csbDidukGFbajLe2WYB 0wHwl6rDXsqQRmpnbaJENm7wNds6jSgB07cpGIbJJaTi1owZTgwdjBW+aLmBXjgbjjZK1jaz7YnGA+ ZZPGnX8hfjn9I+jrY6UI37lKYo5gihMTBT0a6gVU7wos9AWZ31AhG78hRIGVBBfASVPJbmM3wIocIn 6VLsfxY1bHDFuIRlIMSnF4YDznzBTO X-Developer-Key: i=jack@suse.cz; a=openpgp; fpr=93C6099A142276A28BBE35D815BC833443038D8C Precedence: bulk List-ID: X-Mailing-List: stable@vger.kernel.org Before splitting a directory block verify its directory entries are sane so that the splitting code does not access memory it should not. CC: stable@vger.kernel.org Signed-off-by: Jan Kara --- fs/ext4/namei.c | 30 ++++++++++++++++++++---------- 1 file changed, 20 insertions(+), 10 deletions(-) diff --git a/fs/ext4/namei.c b/fs/ext4/namei.c index 767b4bfe39c3..ca6ee9940599 100644 --- a/fs/ext4/namei.c +++ b/fs/ext4/namei.c @@ -277,9 +277,9 @@ static struct dx_frame *dx_probe(struct ext4_filename *fname, struct dx_hash_info *hinfo, struct dx_frame *frame); static void dx_release(struct dx_frame *frames); -static int dx_make_map(struct inode *dir, struct ext4_dir_entry_2 *de, - unsigned blocksize, struct dx_hash_info *hinfo, - struct dx_map_entry map[]); +static int dx_make_map(struct inode *dir, struct buffer_head *bh, + struct dx_hash_info *hinfo, + struct dx_map_entry *map_tail); static void dx_sort_map(struct dx_map_entry *map, unsigned count); static struct ext4_dir_entry_2 *dx_move_dirents(struct inode *dir, char *from, char *to, struct dx_map_entry *offsets, @@ -1249,15 +1249,23 @@ static inline int search_dirblock(struct buffer_head *bh, * Create map of hash values, offsets, and sizes, stored at end of block. * Returns number of entries mapped. */ -static int dx_make_map(struct inode *dir, struct ext4_dir_entry_2 *de, - unsigned blocksize, struct dx_hash_info *hinfo, +static int dx_make_map(struct inode *dir, struct buffer_head *bh, + struct dx_hash_info *hinfo, struct dx_map_entry *map_tail) { int count = 0; - char *base = (char *) de; + struct ext4_dir_entry_2 *de = (struct ext4_dir_entry_2 *)bh->b_data; + unsigned int buflen = bh->b_size; + char *base = bh->b_data; struct dx_hash_info h = *hinfo; - while ((char *) de < base + blocksize) { + if (ext4_has_metadata_csum(dir->i_sb)) + buflen -= sizeof(struct ext4_dir_entry_tail); + + while ((char *) de < base + buflen) { + if (ext4_check_dir_entry(dir, NULL, de, bh, base, buflen, + ((char *)de) - base)) + return -EFSCORRUPTED; if (de->name_len && de->inode) { if (ext4_hash_in_dirent(dir)) h.hash = EXT4_DIRENT_HASH(de); @@ -1270,7 +1278,6 @@ static int dx_make_map(struct inode *dir, struct ext4_dir_entry_2 *de, count++; cond_resched(); } - /* XXX: do we need to check rec_len == 0 case? -Chris */ de = ext4_next_entry(de, blocksize); } return count; @@ -1943,8 +1950,11 @@ static struct ext4_dir_entry_2 *do_split(handle_t *handle, struct inode *dir, /* create map in the end of data2 block */ map = (struct dx_map_entry *) (data2 + blocksize); - count = dx_make_map(dir, (struct ext4_dir_entry_2 *) data1, - blocksize, hinfo, map); + count = dx_make_map(dir, *bh, hinfo, map); + if (count < 0) { + err = count; + goto journal_error; + } map -= count; dx_sort_map(map, count); /* Ensure that neither split block is over half full */ From patchwork Thu Apr 28 18:31:38 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Jan Kara X-Patchwork-Id: 567757 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by smtp.lore.kernel.org (Postfix) with ESMTP id DA6FDC433FE for ; Thu, 28 Apr 2022 18:31:52 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1351073AbiD1SfF (ORCPT ); Thu, 28 Apr 2022 14:35:05 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:37080 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1351083AbiD1SfC (ORCPT ); Thu, 28 Apr 2022 14:35:02 -0400 Received: from smtp-out1.suse.de (smtp-out1.suse.de [195.135.220.28]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 3FF4BBC858; Thu, 28 Apr 2022 11:31:46 -0700 (PDT) Received: from relay2.suse.de (relay2.suse.de [149.44.160.134]) by smtp-out1.suse.de (Postfix) with ESMTP id ED3B92186F; Thu, 28 Apr 2022 18:31:44 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=suse.cz; s=susede2_rsa; t=1651170704; h=from:from:reply-to:date:date:message-id:message-id:to:to:cc:cc: mime-version:mime-version: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=753xZx2E3bh3yXyUeggW3d/Fh8b+RnXgCb38nNJCS+c=; b=rKvBhXjN1pTYEj7qICuvDSf+7Noo2UDDU06HQjCPyFaLREGlJSuW2LaXSNhuaRbpw+mAb1 NDXtdMJHDiWx2N0qHwpN9QiHeUAeZJbjKyxpZaqnXtJ2PGynPXV9/iMqH+0gyOuo+01+RL Xi/0wxpTYBZ5XwWMO4O67y5lsYGH4LM= DKIM-Signature: v=1; a=ed25519-sha256; c=relaxed/relaxed; d=suse.cz; s=susede2_ed25519; t=1651170704; h=from:from:reply-to:date:date:message-id:message-id:to:to:cc:cc: mime-version:mime-version: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=753xZx2E3bh3yXyUeggW3d/Fh8b+RnXgCb38nNJCS+c=; b=uEypsh7I+tYaEgGQecB/uBdf0PYIFveETZz1o8DsT7ifzbyAQEmbjE3oWo3UtVSh/H8Oyg JE2bMkZu6fAH8uDQ== Received: from quack3.suse.cz (unknown [10.100.224.230]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by relay2.suse.de (Postfix) with ESMTPS id C0B342C143; Thu, 28 Apr 2022 18:31:44 +0000 (UTC) Received: by quack3.suse.cz (Postfix, from userid 1000) id C2A79A0623; Thu, 28 Apr 2022 20:31:43 +0200 (CEST) From: Jan Kara To: Ted Tso Cc: , Jan Kara , stable@vger.kernel.org Subject: [PATCH 2/2] ext4: Avoid cycles in directory h-tree Date: Thu, 28 Apr 2022 20:31:38 +0200 Message-Id: <20220428183143.5439-2-jack@suse.cz> X-Mailer: git-send-email 2.34.1 In-Reply-To: <20220428180355.15209-1-jack@suse.cz> References: <20220428180355.15209-1-jack@suse.cz> MIME-Version: 1.0 X-Developer-Signature: v=1; a=openpgp-sha256; l=2654; h=from:subject; bh=RxClARObkvuG8R3IW72U/utffsswWMvIdqciBenEwxc=; b=owEBbQGS/pANAwAIAZydqgc/ZEDZAcsmYgBiat2JvcGzSpul1ayzLA3SFdYV59YLvaWlsi54FO8I jsxnPrKJATMEAAEIAB0WIQSrWdEr1p4yirVVKBycnaoHP2RA2QUCYmrdiQAKCRCcnaoHP2RA2dFeB/ 42mKlrgOINYupTuh56tuxuJbVC2N1y+PpKf+laD3iDBbcJOCMHMHoUX526mdI1NP8S91hFaL4lHO0s PBixhLNFm7pZfFDgNQhzdJA7YKFxjo7BHajNiG36a5JvADYYY7eES6MnyO7+8uwjxJ1+UT6y0q0pdm eU2MzI/PRhDxd+krQRHLA6t2NodugaCe3UpVMOsxfVoIxEhxfe7FDkrrGsX5tWSNoVh8w+pR7WC6dS J2xDrXmy8siELj1Iz9iD9virdptmaUQmKgjIken9quGtgqH2Vm2jOmDIzFO3yTt2nurTeIAGaTZ+6b BKrbr2YPnh6wUIdDUjobnOmT0UMxPT X-Developer-Key: i=jack@suse.cz; a=openpgp; fpr=93C6099A142276A28BBE35D815BC833443038D8C Precedence: bulk List-ID: X-Mailing-List: stable@vger.kernel.org A maliciously corrupted filesystem can contain cycles in the h-tree stored inside a directory. That can easily lead to the kernel corrupting tree nodes that were already verified under its hands while doing a node split and consequently accessing unallocated memory. Fix the problem by verifying traversed block numbers are unique. CC: stable@vger.kernel.org Signed-off-by: Jan Kara --- fs/ext4/namei.c | 24 ++++++++++++++++++++---- 1 file changed, 20 insertions(+), 4 deletions(-) diff --git a/fs/ext4/namei.c b/fs/ext4/namei.c index ca6ee9940599..06441ad6104d 100644 --- a/fs/ext4/namei.c +++ b/fs/ext4/namei.c @@ -777,12 +777,14 @@ static struct dx_frame * dx_probe(struct ext4_filename *fname, struct inode *dir, struct dx_hash_info *hinfo, struct dx_frame *frame_in) { - unsigned count, indirect; + unsigned count, indirect, level, i; struct dx_entry *at, *entries, *p, *q, *m; struct dx_root *root; struct dx_frame *frame = frame_in; struct dx_frame *ret_err = ERR_PTR(ERR_BAD_DX_DIR); u32 hash; + ext4_lblk_t block; + ext4_lblk_t blocks[EXT4_HTREE_LEVEL]; memset(frame_in, 0, EXT4_HTREE_LEVEL * sizeof(frame_in[0])); frame->bh = ext4_read_dirblock(dir, 0, INDEX); @@ -854,6 +856,8 @@ dx_probe(struct ext4_filename *fname, struct inode *dir, } dxtrace(printk("Look up %x", hash)); + level = 0; + blocks[0] = 0; while (1) { count = dx_get_count(entries); if (!count || count > dx_get_limit(entries)) { @@ -882,15 +886,27 @@ dx_probe(struct ext4_filename *fname, struct inode *dir, dx_get_block(at))); frame->entries = entries; frame->at = at; - if (!indirect--) + + block = dx_get_block(at); + for (i = 0; i <= level; i++) { + if (blocks[i] == block) { + ext4_warning_inode(dir, + "dx entry: tree cycle block %u points back to block %u", + blocks[level], block); + goto fail; + } + } + blocks[++level] = block; + if (level > indirect) return frame; frame++; - frame->bh = ext4_read_dirblock(dir, dx_get_block(at), INDEX); + frame->bh = ext4_read_dirblock(dir, block, INDEX); if (IS_ERR(frame->bh)) { ret_err = (struct dx_frame *) frame->bh; frame->bh = NULL; goto fail; } + entries = ((struct dx_node *) frame->bh->b_data)->entries; if (dx_get_limit(entries) != dx_node_limit(dir)) { @@ -1278,7 +1294,7 @@ static int dx_make_map(struct inode *dir, struct buffer_head *bh, count++; cond_resched(); } - de = ext4_next_entry(de, blocksize); + de = ext4_next_entry(de, dir->i_sb->s_blocksize); } return count; }