Received: by 2002:a05:6358:1087:b0:cb:c9d3:cd90 with SMTP id j7csp2452196rwi; Tue, 11 Oct 2022 08:58:04 -0700 (PDT) X-Google-Smtp-Source: AMsMyM44NkMPZrfa/dnuZpwcHBh3wA1QGP2+q+JZgLnK3BN9SZNxWsF1fHF4a5r/AHdur3WXXhT/ X-Received: by 2002:a05:6402:e01:b0:442:dd7e:f49d with SMTP id h1-20020a0564020e0100b00442dd7ef49dmr14892419edh.355.1665503884050; Tue, 11 Oct 2022 08:58:04 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1665503884; cv=none; d=google.com; s=arc-20160816; b=vK3DJiH8N+o4fm1dmLHdVMVOeRzDHMuA2K0aT6QwQKwt/SqOR72wxsjqdeCjQuNQsQ zS9Qm+B9p5p2ZIpOKmncKrQIX8Fnjxz5zQeuwK7UUWEHXLq9QhYXaklGtdILq1bj58yO udwkjASnZXi6H/P1RJnMHJUszsAWPZl5Ae+Vrz/pOlNt+Uaqf0thFR5QcQYmFilEmedH V7fMt/nWJLMontEsCfOc1k0bBRMCwxMa8KSZJRC4OCueKBf3s2sltEgwfFFAefjHpxrF CeWhzAtX+1I0+UbfSTcZ6iz/fIU9/40NkRCqW8w3GiWZ69jhV9Ag0nhHCK6tJ3mm8chv HZ5w== 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 :message-id:date:subject:cc:to:from:dkim-signature:dkim-signature; bh=BzpXv8ILApfxNxoDpsT73KKx7K6rQNTPNbrtke8IbdE=; b=Zk4+GLKnZ6pgHSezgit5soKbw9zKmv1cCz8K0xrEpA28c90/rT7Q3SzO38/dCEzk3T Pidt51MouatnljaEQ2XebgT73VREs//Mbmfeg8E0LpMmLl0jTQnAFfSPlRP8+3+36szA YueF9HeeuPs83h7io5xFZ3AvSahd6UxKjB65xgKZyO8cbGTIcbKlgpn5YBEcU3/sVDLn kGnRHRjg/EZQUEhBDY1ZH1gMDuudviCwY7OHiR/CyWiMuewh1uUy57IIHsb90fiKcY9+ UBqV7EJXxkgYWwNVQDghZwQYCyZK6OFUSJAx4uFvsVwwtX5maUuxVU1+2rH3iaaxvqp6 1K3Q== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@suse.de header.s=susede2_rsa header.b=q7KoTaoP; dkim=neutral (no key) header.i=@suse.de header.s=susede2_ed25519 header.b=ITJ3z81u; spf=pass (google.com: domain of linux-ext4-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-ext4-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=suse.de Return-Path: Received: from out1.vger.email (out1.vger.email. [2620:137:e000::1:20]) by mx.google.com with ESMTP id gt35-20020a1709072da300b0073d64fca704si16484204ejc.36.2022.10.11.08.57.40; Tue, 11 Oct 2022 08:58:04 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-ext4-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=@suse.de header.s=susede2_rsa header.b=q7KoTaoP; dkim=neutral (no key) header.i=@suse.de header.s=susede2_ed25519 header.b=ITJ3z81u; spf=pass (google.com: domain of linux-ext4-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-ext4-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=suse.de Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S230055AbiJKP5A (ORCPT + 99 others); Tue, 11 Oct 2022 11:57:00 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:48670 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S230078AbiJKP45 (ORCPT ); Tue, 11 Oct 2022 11:56:57 -0400 Received: from smtp-out2.suse.de (smtp-out2.suse.de [195.135.220.29]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 246CE32EDB; Tue, 11 Oct 2022 08:56:55 -0700 (PDT) Received: from imap2.suse-dmz.suse.de (imap2.suse-dmz.suse.de [192.168.254.74]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature ECDSA (P-521) server-digest SHA512) (No client certificate requested) by smtp-out2.suse.de (Postfix) with ESMTPS id B2AEF1F8B0; Tue, 11 Oct 2022 15:56:53 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=suse.de; s=susede2_rsa; t=1665503813; h=from:from:reply-to:date:date:message-id:message-id:to:to:cc:cc: mime-version:mime-version:content-type:content-type: content-transfer-encoding:content-transfer-encoding; bh=BzpXv8ILApfxNxoDpsT73KKx7K6rQNTPNbrtke8IbdE=; b=q7KoTaoPrNUqHpu3g+sxiU/YiIPTSMdtDECUwXFNLzdOaYTLxnLONnKE6Eze7EFvTkZrtl K6mt2gIRHICzwYi/Yv7xvWC6rDQ+vIVbvPJdgPPK1hJr09cTQ33tuOsWZQBAnE+To0u3+v +g+ZZc25kbowBu2eNZumsBJ2gFdnwfA= DKIM-Signature: v=1; a=ed25519-sha256; c=relaxed/relaxed; d=suse.de; s=susede2_ed25519; t=1665503813; h=from:from:reply-to:date:date:message-id:message-id:to:to:cc:cc: mime-version:mime-version:content-type:content-type: content-transfer-encoding:content-transfer-encoding; bh=BzpXv8ILApfxNxoDpsT73KKx7K6rQNTPNbrtke8IbdE=; b=ITJ3z81uGnfrZNDiFTVif3RqESm0gw4tIh5i2zvFM4Av1gVfAH6wrenz2hdpDWGnTATEtY PiX3BnV9dlSm+aDg== Received: from imap2.suse-dmz.suse.de (imap2.suse-dmz.suse.de [192.168.254.74]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature ECDSA (P-521) server-digest SHA512) (No client certificate requested) by imap2.suse-dmz.suse.de (Postfix) with ESMTPS id 3FCEA13AAC; Tue, 11 Oct 2022 15:56:53 +0000 (UTC) Received: from dovecot-director2.suse.de ([192.168.254.65]) by imap2.suse-dmz.suse.de with ESMTPSA id PG/RDEWSRWPEYwAAMHmgww (envelope-from ); Tue, 11 Oct 2022 15:56:53 +0000 Received: from localhost (brahms.olymp [local]) by brahms.olymp (OpenSMTPD) with ESMTPA id 2c98b2e4; Tue, 11 Oct 2022 15:57:49 +0000 (UTC) From: =?UTF-8?q?Lu=C3=ADs=20Henriques?= To: "Theodore Ts'o" , Andreas Dilger Cc: linux-ext4@vger.kernel.org, linux-kernel@vger.kernel.org, =?UTF-8?q?Lu=C3=ADs=20Henriques?= , stable@vger.kernel.org Subject: [PATCH] ext4: fix BUG_ON() when a directory entry has an invalid rec_len Date: Tue, 11 Oct 2022 16:57:45 +0100 Message-Id: <20221011155745.15264-1-lhenriques@suse.de> MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit X-Spam-Status: No, score=-4.4 required=5.0 tests=BAYES_00,DKIM_SIGNED, DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,RCVD_IN_DNSWL_MED,SPF_HELO_NONE, SPF_PASS 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-ext4@vger.kernel.org The rec_len field in the directory entry has to be a multiple of 4. A corrupted filesystem image can be used to hit a BUG_ON() in ext4_rec_len_to_disk(), called from make_indexed_dir(). ------------[ cut here ]------------ kernel BUG at fs/ext4/ext4.h:2413! ... RIP: 0010:make_indexed_dir+0x53f/0x5f0 ... Call Trace: ? add_dirent_to_buf+0x1b2/0x200 ext4_add_entry+0x36e/0x480 ext4_add_nondir+0x2b/0xc0 ext4_create+0x163/0x200 path_openat+0x635/0xe90 do_filp_open+0xb4/0x160 ? __create_object.isra.0+0x1de/0x3b0 ? _raw_spin_unlock+0x12/0x30 do_sys_openat2+0x91/0x150 __x64_sys_open+0x6c/0xa0 do_syscall_64+0x3c/0x80 entry_SYSCALL_64_after_hwframe+0x46/0xb0 This fix simply returns -EFSCORRUPTED when this happens. CC: stable@vger.kernel.org Link: https://bugzilla.kernel.org/show_bug.cgi?id=216540 Signed-off-by: Luís Henriques --- fs/ext4/namei.c | 12 +++++++++++- 1 file changed, 11 insertions(+), 1 deletion(-) diff --git a/fs/ext4/namei.c b/fs/ext4/namei.c index 3a31b662f661..06803292e394 100644 --- a/fs/ext4/namei.c +++ b/fs/ext4/namei.c @@ -2254,8 +2254,18 @@ static int make_indexed_dir(handle_t *handle, struct ext4_filename *fname, memset(de, 0, len); /* wipe old data */ de = (struct ext4_dir_entry_2 *) data2; top = data2 + len; - while ((char *)(de2 = ext4_next_entry(de, blocksize)) < top) + while ((char *)(de2 = ext4_next_entry(de, blocksize)) < top) { + if (de->rec_len & 3) { + EXT4_ERROR_INODE( + dir, + "rec_len for '..' not multiple of 4 (%u)", + de->rec_len); + brelse(bh2); + brelse(bh); + return -EFSCORRUPTED; + } de = de2; + } de->rec_len = ext4_rec_len_to_disk(data2 + (blocksize - csum_size) - (char *) de, blocksize);