From patchwork Tue Oct 8 12:11:52 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Baokun Li X-Patchwork-Id: 1994234 Return-Path: X-Original-To: incoming@patchwork.ozlabs.org Delivered-To: patchwork-incoming@legolas.ozlabs.org Authentication-Results: legolas.ozlabs.org; spf=pass (sender SPF authorized) smtp.mailfrom=ozlabs.org (client-ip=2404:9400:2221:ea00::3; helo=mail.ozlabs.org; envelope-from=srs0=didc=re=vger.kernel.org=linux-ext4+bounces-4528-patchwork-incoming=ozlabs.org@ozlabs.org; receiver=patchwork.ozlabs.org) Received: from mail.ozlabs.org (mail.ozlabs.org [IPv6:2404:9400:2221:ea00::3]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature ECDSA (secp384r1) server-digest SHA384) (No client certificate requested) by legolas.ozlabs.org (Postfix) with ESMTPS id 4XNFLq0QvVz1xsn for ; Tue, 8 Oct 2024 23:14:27 +1100 (AEDT) Received: from mail.ozlabs.org (mail.ozlabs.org [IPv6:2404:9400:2221:ea00::3]) by gandalf.ozlabs.org (Postfix) with ESMTP id 4XNFLp2FFwz4x8Z for ; Tue, 8 Oct 2024 23:14:26 +1100 (AEDT) Received: by gandalf.ozlabs.org (Postfix) id 4XNFLp2CCqz4x8c; Tue, 8 Oct 2024 23:14:26 +1100 (AEDT) Delivered-To: patchwork-incoming@ozlabs.org Authentication-Results: gandalf.ozlabs.org; arc=pass smtp.remote-ip="2604:1380:45e3:2400::1" arc.chain=subspace.kernel.org ARC-Seal: i=2; a=rsa-sha256; d=ozlabs.org; s=201707; t=1728389666; cv=pass; b=GzyWZ46/+6PYl1osMdfNNfMIfr1Ym+ZBhSRo1QZXSQ9M7gqOddznvybvva86trOKsrEXooFxmdOF85Kn8A166dU+/aDQ2KoR1TwLVIg5+2z5QCTjmwwkFCqjpc+RzQl5mY8rLexADRATOhkCktdUp90gijWUoV/ytS5EFp5eG2fKV2DkYVKqtN+Zcf8dbZKovw4ClFs8Ls12ooZW/npWW+gEwWjmdHZ/ng8w0O3ingHjzQJUgwsJlPWTxwpWJLOE3phciHtfgnX1UK1UgLF/Ucs+piCETewLHaFyZnJVeLrxShVMCqFJwzQzMGdR9NgDbEY+wt5a+pQDxHmtk3591A== ARC-Message-Signature: i=2; a=rsa-sha256; d=ozlabs.org; s=201707; t=1728389666; c=relaxed/relaxed; bh=pJg6HbdYTzN6bnuDE3otEXfh4Wh7FPeYTJkyUr/2+VI=; h=From:To:Cc:Subject:Date:Message-Id:MIME-Version; b=gRgwYaYomYX0PxB6OxA7AjXRNG7/Rcs+U5NoPnMOVTZNX2qIAqucKYc5Ib2RYAdHyRB/ZeKmF+1HfM189eX6xpxerxdtW+0FPFMIK404LIB7U+S8uM6oNoqOx+fJkJKZxFmGicE3uvAep9dRe9T5TCNH8r1ARBSIKK0cGUHWsxGlbjGtKOPxicvtiZkmc5sdduMjgTW9UDSZovE6kifdLWC+s3Zl2H81k0EOehqvsQJgiZvT9hd7TkCc51dm+sTc60XVpfnE0hKduehiIwDd3QsYuRbDYl3BAyKdwOiafPSEa7Bjp7Ho5knXrd9k7NdAboK8Iv0XH42q7RisMmgoow== ARC-Authentication-Results: i=2; gandalf.ozlabs.org; dmarc=none (p=none dis=none) header.from=huaweicloud.com; spf=pass (client-ip=2604:1380:45e3:2400::1; helo=sv.mirrors.kernel.org; envelope-from=linux-ext4+bounces-4528-patchwork-incoming=ozlabs.org@vger.kernel.org; receiver=ozlabs.org) smtp.mailfrom=vger.kernel.org Authentication-Results: gandalf.ozlabs.org; dmarc=none (p=none dis=none) header.from=huaweicloud.com Authentication-Results: gandalf.ozlabs.org; spf=pass (sender SPF authorized) smtp.mailfrom=vger.kernel.org (client-ip=2604:1380:45e3:2400::1; helo=sv.mirrors.kernel.org; envelope-from=linux-ext4+bounces-4528-patchwork-incoming=ozlabs.org@vger.kernel.org; receiver=ozlabs.org) Received: from sv.mirrors.kernel.org (sv.mirrors.kernel.org [IPv6:2604:1380:45e3:2400::1]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (4096 bits)) (No client certificate requested) by gandalf.ozlabs.org (Postfix) with ESMTPS id 4XNFLn5NGyz4x8Z for ; Tue, 8 Oct 2024 23:14:25 +1100 (AEDT) Received: from smtp.subspace.kernel.org (wormhole.subspace.kernel.org [52.25.139.140]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by sv.mirrors.kernel.org (Postfix) with ESMTPS id 2D5E9281600 for ; Tue, 8 Oct 2024 12:14:25 +0000 (UTC) Received: from localhost.localdomain (localhost.localdomain [127.0.0.1]) by smtp.subspace.kernel.org (Postfix) with ESMTP id B2AA61DE3AE; Tue, 8 Oct 2024 12:13:49 +0000 (UTC) X-Original-To: linux-ext4@vger.kernel.org Received: from dggsgout11.his.huawei.com (dggsgout11.his.huawei.com [45.249.212.51]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 3AA291DE3C1; Tue, 8 Oct 2024 12:13:46 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=45.249.212.51 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1728389629; cv=none; b=T7jT0FIAUa1pS3moGx/HWirEsdZX3c2644kzLwskGuvSVtsXfxc8resrps3d3W/EkXZFtOx5O5x6pDWQFqwwjby239IcFkgFuadLU2hqmSS/QEuxY6r853mcv6wbZcn5akE09tKFq8fmCSCLvhutY8kybdCxV0BwN532iuICoLs= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1728389629; c=relaxed/simple; bh=wEbV/QzaN2bQIxKJnfGzPG4juvL36fx0LANBaOI3IZw=; h=From:To:Cc:Subject:Date:Message-Id:MIME-Version; b=DfU/jM89HwJN8M25lD7HfyeO4idVYwTJO1u+07SQNo+mdmeK9b1aXacQ6v64SgpWtpFFBJXb22/xdUCARKXZax212Eu/S9iZ3C39O6KqW7LJ/Ma+UTTcjrtuqGMHFY/Miwx0N4dZh8XT4EfV3OzVwdFPglvJtxImLXwylYbtAgk= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=none (p=none dis=none) header.from=huaweicloud.com; spf=none smtp.mailfrom=huaweicloud.com; arc=none smtp.client-ip=45.249.212.51 Authentication-Results: smtp.subspace.kernel.org; dmarc=none (p=none dis=none) header.from=huaweicloud.com Authentication-Results: smtp.subspace.kernel.org; spf=none smtp.mailfrom=huaweicloud.com Received: from mail.maildlp.com (unknown [172.19.163.235]) by dggsgout11.his.huawei.com (SkyGuard) with ESMTP id 4XNFKk5SPvz4f3k6W; Tue, 8 Oct 2024 20:13:30 +0800 (CST) Received: from mail02.huawei.com (unknown [10.116.40.128]) by mail.maildlp.com (Postfix) with ESMTP id 78B3F1A0568; Tue, 8 Oct 2024 20:13:42 +0800 (CST) Received: from huaweicloud.com (unknown [10.175.104.67]) by APP4 (Coremail) with SMTP id gCh0CgDH+8fzIQVnSZoUDg--.44667S4; Tue, 08 Oct 2024 20:13:41 +0800 (CST) From: libaokun@huaweicloud.com To: linux-ext4@vger.kernel.org Cc: tytso@mit.edu, adilger.kernel@dilger.ca, jack@suse.cz, linux-kernel@vger.kernel.org, yi.zhang@huawei.com, yangerkun@huawei.com, libaokun@huaweicloud.com, Baokun Li Subject: [PATCH] ext4: WARN if a full dir leaf block has only one dentry Date: Tue, 8 Oct 2024 20:11:52 +0800 Message-Id: <20241008121152.3771906-1-libaokun@huaweicloud.com> X-Mailer: git-send-email 2.39.2 Precedence: bulk X-Mailing-List: linux-ext4@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 X-CM-TRANSID: gCh0CgDH+8fzIQVnSZoUDg--.44667S4 X-Coremail-Antispam: 1UD129KB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xpfor3voofrz/1tbiAQAKBWcE7QQQOwAAsv From: Baokun Li The maximum length of a filename is 255 and the minimum block size is 1024, so it is always guaranteed that the number of entries is greater than or equal to 2 when do_split() is called. So unless ext4_dx_add_entry() and make_indexed_dir() or some other functions are buggy, 'split == 0' will not occur. Setting 'continued' to 0 in this case masks the problem that the file system has become corrupted, even though it prevents possible out-of-bounds access. Hence WARN_ON_ONCE() is used to check if 'split' is 0, and if it is then warns and returns an error to abort split. Suggested-by: Theodore Ts'o Link: https://lore.kernel.org/r/20240823160518.GA424729@mit.edu Signed-off-by: Baokun Li Reviewed-by: Jan Kara --- fs/ext4/namei.c | 14 ++++++++++++-- 1 file changed, 12 insertions(+), 2 deletions(-) diff --git a/fs/ext4/namei.c b/fs/ext4/namei.c index 790db7eac6c2..08d15cd2b594 100644 --- a/fs/ext4/namei.c +++ b/fs/ext4/namei.c @@ -2000,8 +2000,17 @@ static struct ext4_dir_entry_2 *do_split(handle_t *handle, struct inode *dir, else split = count/2; + if (WARN_ON_ONCE(split == 0)) { + /* Should never happen, but avoid out-of-bounds access below */ + ext4_error_inode_block(dir, (*bh)->b_blocknr, 0, + "bad indexed directory? hash=%08x:%08x count=%d move=%u", + hinfo->hash, hinfo->minor_hash, count, move); + err = -EFSCORRUPTED; + goto out; + } + hash2 = map[split].hash; - continued = split > 0 ? hash2 == map[split - 1].hash : 0; + continued = hash2 == map[split - 1].hash; dxtrace(printk(KERN_INFO "Split block %lu at %x, %i/%i\n", (unsigned long)dx_get_block(frame->at), hash2, split, count-split)); @@ -2043,10 +2052,11 @@ static struct ext4_dir_entry_2 *do_split(handle_t *handle, struct inode *dir, return de; journal_error: + ext4_std_error(dir->i_sb, err); +out: brelse(*bh); brelse(bh2); *bh = NULL; - ext4_std_error(dir->i_sb, err); return ERR_PTR(err); }