From patchwork Tue Mar 22 03:00:00 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Gabriel Krisman Bertazi X-Patchwork-Id: 1608010 Return-Path: X-Original-To: incoming@patchwork.ozlabs.org Delivered-To: patchwork-incoming@bilbo.ozlabs.org Authentication-Results: bilbo.ozlabs.org; dkim=pass (2048-bit key; unprotected) header.d=collabora.com header.i=@collabora.com header.a=rsa-sha256 header.s=mail header.b=VMV50pMK; dkim-atps=neutral Received: from gandalf.ozlabs.org (gandalf.ozlabs.org [150.107.74.76]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (4096 bits) server-digest SHA256) (No client certificate requested) by bilbo.ozlabs.org (Postfix) with ESMTPS id 4KMx7H5fHvz9s5V for ; Tue, 22 Mar 2022 14:00:27 +1100 (AEDT) Received: from gandalf.ozlabs.org (mail.ozlabs.org [IPv6:2404:9400:2221:ea00::3]) by gandalf.ozlabs.org (Postfix) with ESMTP id 4KMx7D44vbz4xMW for ; Tue, 22 Mar 2022 14:00:24 +1100 (AEDT) Received: by gandalf.ozlabs.org (Postfix) id 4KMx7D41ryz4xXV; Tue, 22 Mar 2022 14:00:24 +1100 (AEDT) Delivered-To: patchwork-incoming@ozlabs.org Authentication-Results: gandalf.ozlabs.org; spf=pass (sender SPF authorized) smtp.mailfrom=vger.kernel.org (client-ip=2620:137:e000::1:20; helo=out1.vger.email; envelope-from=linux-ext4-owner@vger.kernel.org; receiver=) Authentication-Results: gandalf.ozlabs.org; dkim=pass (2048-bit key; unprotected) header.d=collabora.com header.i=@collabora.com header.a=rsa-sha256 header.s=mail header.b=VMV50pMK; dkim-atps=neutral Received: from out1.vger.email (out1.vger.email [IPv6:2620:137:e000::1:20]) by gandalf.ozlabs.org (Postfix) with ESMTP id 4KMx7D3Vz0z4xMW for ; Tue, 22 Mar 2022 14:00:24 +1100 (AEDT) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S235739AbiCVDBt (ORCPT ); Mon, 21 Mar 2022 23:01:49 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:51418 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S235718AbiCVDBo (ORCPT ); Mon, 21 Mar 2022 23:01:44 -0400 Received: from bhuna.collabora.co.uk (bhuna.collabora.co.uk [IPv6:2a00:1098:0:82:1000:25:2eeb:e3e3]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 347361FE564 for ; Mon, 21 Mar 2022 20:00:18 -0700 (PDT) Received: from [127.0.0.1] (localhost [127.0.0.1]) (Authenticated sender: krisman) with ESMTPSA id D50E21F41640 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=collabora.com; s=mail; t=1647918017; bh=9Gvy4Q6vMFRJVdm/DEWf0uzjdE0FiEbo+TJFE9FDuYU=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=VMV50pMKCOUEBV9F8w1natXIZcuNjlbcR3UM3GnBQmr+rctPJVftlmJ61y+Q/9o36 qxDrzhwuGOvsOrwhXazSgmiotx6ItY7SwDOY+NrW1Lc+UbP6kiauvClDcAHSxKpkqC v03jRcqQe3/EU3mIzXaly2aiG7iyuPvwWH3p0v+yo6LhGC0Hx2MlMKtVpsRfTrr8ol Tj4IYT3NBE4diGFtWVxkQDNingJNyI9jq/lqMpIRcVB+4GSQ0njPj/XSWwEmHW7k4q B8tdhIsi74ylyJjx7kWqGZsj2po1CkrPtC/SWpc1ADWakFLm9WNX8aqyYAF26Z9oGm jxE7QVicwDqQA== From: Gabriel Krisman Bertazi To: tytso@mit.edu Cc: ebiggers@kernel.org, jaegeuk@kernel.org, linux-ext4@vger.kernel.org, Gabriel Krisman Bertazi , kernel@collabora.com Subject: [PATCH 1/5] ext4: Match the f2fs ci_compare implementation Date: Mon, 21 Mar 2022 23:00:00 -0400 Message-Id: <20220322030004.148560-2-krisman@collabora.com> X-Mailer: git-send-email 2.35.1 In-Reply-To: <20220322030004.148560-1-krisman@collabora.com> References: <20220322030004.148560-1-krisman@collabora.com> MIME-Version: 1.0 X-Spam-Status: No, score=-2.1 required=5.0 tests=BAYES_00,DKIM_SIGNED, DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,SPF_HELO_PASS,SPF_PASS, T_SCC_BODY_TEXT_LINE,UNPARSEABLE_RELAY 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 ext4_ci_compare originally follows utf8_*_strcmp, which means return zero on match. This means that every usage of that in ext4 negates the return. Turn it into a predicate function, let it follow the kernel convention and return true on match, which means it's now the same as its f2fs counterpart and can be extracted into generic code. This change also makes it more obvious that we are ignoring error handling in ext4_match, which can occur since casefolding support (bad utf8 name due to disk corruption on strict mode causes -EINVAL) and casefold+encryption (-ENOMEM). For now, keep the behavior. It is handled by the following patches. While we are there, change the comment to the kernel-doc style. Signed-off-by: Gabriel Krisman Bertazi --- fs/ext4/namei.c | 62 +++++++++++++++++++++++++++++++++---------------- 1 file changed, 42 insertions(+), 20 deletions(-) diff --git a/fs/ext4/namei.c b/fs/ext4/namei.c index 8cf0a924a49b..24ea3bb446d0 100644 --- a/fs/ext4/namei.c +++ b/fs/ext4/namei.c @@ -1318,13 +1318,20 @@ static void dx_insert_block(struct dx_frame *frame, u32 hash, ext4_lblk_t block) } #if IS_ENABLED(CONFIG_UNICODE) -/* +/** + * ext4_ci_compare() - Match (case-insensitive) a name with a dirent. + * @parent: Inode of the parent of the dentry. + * @name: name under lookup. + * @de_name: Dirent name. + * @de_name_len: dirent name length. + * @quick: whether @name is already casefolded. + * * Test whether a case-insensitive directory entry matches the filename - * being searched for. If quick is set, assume the name being looked up - * is already in the casefolded form. + * being searched. If quick is set, the @name being looked up is + * already in the casefolded form. * - * Returns: 0 if the directory entry matches, more than 0 if it - * doesn't match or less than zero on error. + * Return: > 0 if the directory entry matches, 0 if it doesn't match, or + * < 0 on error. */ static int ext4_ci_compare(const struct inode *parent, const struct qstr *name, u8 *de_name, size_t de_name_len, bool quick) @@ -1333,7 +1340,7 @@ static int ext4_ci_compare(const struct inode *parent, const struct qstr *name, const struct unicode_map *um = sb->s_encoding; struct fscrypt_str decrypted_name = FSTR_INIT(NULL, de_name_len); struct qstr entry = QSTR_INIT(de_name, de_name_len); - int ret; + int ret, match = false; if (IS_ENCRYPTED(parent)) { const struct fscrypt_str encrypted_name = @@ -1354,20 +1361,22 @@ static int ext4_ci_compare(const struct inode *parent, const struct qstr *name, ret = utf8_strncasecmp_folded(um, name, &entry); else ret = utf8_strncasecmp(um, name, &entry); - if (ret < 0) { - /* Handle invalid character sequence as either an error - * or as an opaque byte sequence. + + if (!ret) + match = true; + else if (ret < 0 && !sb_has_strict_encoding(sb)) { + /* + * In non-strict mode, fallback to a byte comparison if + * the names have invalid characters. */ - if (sb_has_strict_encoding(sb)) - ret = -EINVAL; - else if (name->len != entry.len) - ret = 1; - else - ret = !!memcmp(name->name, entry.name, entry.len); + ret = 0; + match = ((name->len == entry.len) && + !memcmp(name->name, entry.name, entry.len)); } + out: kfree(decrypted_name.name); - return ret; + return (ret >= 0) ? match : ret; } int ext4_fname_setup_ci_filename(struct inode *dir, const struct qstr *iname, @@ -1418,6 +1427,7 @@ static bool ext4_match(struct inode *parent, struct ext4_dir_entry_2 *de) { struct fscrypt_name f; + int ret; if (!de->inode) return false; @@ -1442,11 +1452,23 @@ static bool ext4_match(struct inode *parent, return false; } } - return !ext4_ci_compare(parent, &cf, de->name, - de->name_len, true); + ret = ext4_ci_compare(parent, &cf, de->name, + de->name_len, true); + } else { + ret = ext4_ci_compare(parent, fname->usr_fname, + de->name, de->name_len, false); } - return !ext4_ci_compare(parent, fname->usr_fname, de->name, - de->name_len, false); + + if (ret < 0) { + /* + * Treat comparison errors as not a match. The + * only case where it happens is on a disk + * corruption or ENOMEM. + */ + return false; + } + return ret; + } #endif From patchwork Tue Mar 22 03:00:01 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Gabriel Krisman Bertazi X-Patchwork-Id: 1608009 Return-Path: X-Original-To: incoming@patchwork.ozlabs.org Delivered-To: patchwork-incoming@bilbo.ozlabs.org Authentication-Results: bilbo.ozlabs.org; dkim=pass (2048-bit key; unprotected) header.d=collabora.com header.i=@collabora.com header.a=rsa-sha256 header.s=mail header.b=YicSGx3a; dkim-atps=neutral Received: from gandalf.ozlabs.org (gandalf.ozlabs.org [150.107.74.76]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (4096 bits) server-digest SHA256) (No client certificate requested) by bilbo.ozlabs.org (Postfix) with ESMTPS id 4KMx7H5JCbz9s3q for ; Tue, 22 Mar 2022 14:00:27 +1100 (AEDT) Received: from gandalf.ozlabs.org (gandalf.ozlabs.org [150.107.74.76]) by gandalf.ozlabs.org (Postfix) with ESMTP id 4KMx7G1Bv4z4xXV for ; Tue, 22 Mar 2022 14:00:26 +1100 (AEDT) Received: by gandalf.ozlabs.org (Postfix) id 4KMx7G10QFz4xc3; Tue, 22 Mar 2022 14:00:26 +1100 (AEDT) Delivered-To: patchwork-incoming@ozlabs.org Authentication-Results: gandalf.ozlabs.org; spf=pass (sender SPF authorized) smtp.mailfrom=vger.kernel.org (client-ip=2620:137:e000::1:20; helo=out1.vger.email; envelope-from=linux-ext4-owner@vger.kernel.org; receiver=) Authentication-Results: gandalf.ozlabs.org; dkim=pass (2048-bit key; unprotected) header.d=collabora.com header.i=@collabora.com header.a=rsa-sha256 header.s=mail header.b=YicSGx3a; dkim-atps=neutral Received: from out1.vger.email (out1.vger.email [IPv6:2620:137:e000::1:20]) by gandalf.ozlabs.org (Postfix) with ESMTP id 4KMx7G0YlRz4xXV for ; Tue, 22 Mar 2022 14:00:26 +1100 (AEDT) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S235745AbiCVDBt (ORCPT ); Mon, 21 Mar 2022 23:01:49 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:51682 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S235734AbiCVDBs (ORCPT ); Mon, 21 Mar 2022 23:01:48 -0400 Received: from bhuna.collabora.co.uk (bhuna.collabora.co.uk [IPv6:2a00:1098:0:82:1000:25:2eeb:e3e3]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 304172016A3 for ; Mon, 21 Mar 2022 20:00:22 -0700 (PDT) Received: from [127.0.0.1] (localhost [127.0.0.1]) (Authenticated sender: krisman) with ESMTPSA id CAAD91F41054 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=collabora.com; s=mail; t=1647918021; bh=uuxMaXOl1WidwsQtHu97JSPFDBNZ+gJKbzzoXAH1l74=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=YicSGx3aq5KT9RaGCU57tX1lG1qisOCcdOn+aHLdWw0ARxlLTt7aOknlvEq7RGRs5 IQaqhb9FVBA/Tf4yzE6iAvu0lc9HJRJrG7p72yGYmlf0lE8mUYs7iSUJk+hjsjVHGa XuNzMyWjIYc0AmfIm8HD+lOhfV2fLWHABmgeXc+Sf49/laF4zHvSiNcMBPa5y377TO MIK234Ycro3TRMctnsrjZuHKBbrzYI8YJiQaxoZVXWgdpVZ40EHk+j0dh5tyutK8O/ k0DVyk1c/qQ5UZj9dhrU+RB09JTQuxuAGqVNvf3h2eeOx6LsDL+nj/mBwpJjuP91/U gkDRYU/qK9t9Q== From: Gabriel Krisman Bertazi To: tytso@mit.edu Cc: ebiggers@kernel.org, jaegeuk@kernel.org, linux-ext4@vger.kernel.org, Gabriel Krisman Bertazi , kernel@collabora.com Subject: [PATCH 2/5] ext4: Simplify the handling of chached insensitive names Date: Mon, 21 Mar 2022 23:00:01 -0400 Message-Id: <20220322030004.148560-3-krisman@collabora.com> X-Mailer: git-send-email 2.35.1 In-Reply-To: <20220322030004.148560-1-krisman@collabora.com> References: <20220322030004.148560-1-krisman@collabora.com> MIME-Version: 1.0 X-Spam-Status: No, score=-2.1 required=5.0 tests=BAYES_00,DKIM_SIGNED, DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,SPF_HELO_PASS,SPF_PASS, T_SCC_BODY_TEXT_LINE,UNPARSEABLE_RELAY 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 Keeping it as qstr avoids the unnecessary conversion in ext4_match Signed-off-by: Gabriel Krisman Bertazi --- fs/ext4/ext4.h | 2 +- fs/ext4/namei.c | 23 +++++++++++------------ 2 files changed, 12 insertions(+), 13 deletions(-) diff --git a/fs/ext4/ext4.h b/fs/ext4/ext4.h index bcd3b9bf8069..46e729ce7b35 100644 --- a/fs/ext4/ext4.h +++ b/fs/ext4/ext4.h @@ -2484,7 +2484,7 @@ struct ext4_filename { struct fscrypt_str crypto_buf; #endif #if IS_ENABLED(CONFIG_UNICODE) - struct fscrypt_str cf_name; + struct qstr cf_name; #endif }; diff --git a/fs/ext4/namei.c b/fs/ext4/namei.c index 24ea3bb446d0..8976e5a28c73 100644 --- a/fs/ext4/namei.c +++ b/fs/ext4/namei.c @@ -1382,28 +1382,29 @@ static int ext4_ci_compare(const struct inode *parent, const struct qstr *name, int ext4_fname_setup_ci_filename(struct inode *dir, const struct qstr *iname, struct ext4_filename *name) { - struct fscrypt_str *cf_name = &name->cf_name; + struct qstr *cf_name = &name->cf_name; + unsigned char *buf; struct dx_hash_info *hinfo = &name->hinfo; int len; if (!IS_CASEFOLDED(dir) || !dir->i_sb->s_encoding || (IS_ENCRYPTED(dir) && !fscrypt_has_encryption_key(dir))) { - cf_name->name = NULL; + name->cf_name.name = NULL; return 0; } - cf_name->name = kmalloc(EXT4_NAME_LEN, GFP_NOFS); - if (!cf_name->name) + buf = kmalloc(EXT4_NAME_LEN, GFP_NOFS); + if (!buf) return -ENOMEM; - len = utf8_casefold(dir->i_sb->s_encoding, - iname, cf_name->name, - EXT4_NAME_LEN); + len = utf8_casefold(dir->i_sb->s_encoding, iname, buf, EXT4_NAME_LEN); if (len <= 0) { - kfree(cf_name->name); - cf_name->name = NULL; + kfree(buf); + buf = NULL; } + cf_name->name = buf; cf_name->len = (unsigned) len; + if (!IS_ENCRYPTED(dir)) return 0; @@ -1442,8 +1443,6 @@ static bool ext4_match(struct inode *parent, if (parent->i_sb->s_encoding && IS_CASEFOLDED(parent) && (!IS_ENCRYPTED(parent) || fscrypt_has_encryption_key(parent))) { if (fname->cf_name.name) { - struct qstr cf = {.name = fname->cf_name.name, - .len = fname->cf_name.len}; if (IS_ENCRYPTED(parent)) { if (fname->hinfo.hash != EXT4_DIRENT_HASH(de) || fname->hinfo.minor_hash != @@ -1452,7 +1451,7 @@ static bool ext4_match(struct inode *parent, return false; } } - ret = ext4_ci_compare(parent, &cf, de->name, + ret = ext4_ci_compare(parent, &fname->cf_name, de->name, de->name_len, true); } else { ret = ext4_ci_compare(parent, fname->usr_fname, From patchwork Tue Mar 22 03:00:02 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Gabriel Krisman Bertazi X-Patchwork-Id: 1608011 Return-Path: X-Original-To: incoming@patchwork.ozlabs.org Delivered-To: patchwork-incoming@bilbo.ozlabs.org Authentication-Results: bilbo.ozlabs.org; dkim=pass (2048-bit key; unprotected) header.d=collabora.com header.i=@collabora.com header.a=rsa-sha256 header.s=mail header.b=ajAmRddI; dkim-atps=neutral Received: from gandalf.ozlabs.org (gandalf.ozlabs.org [150.107.74.76]) (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 bilbo.ozlabs.org (Postfix) with ESMTPS id 4KMx7T4DNfz9s3q for ; Tue, 22 Mar 2022 14:00:37 +1100 (AEDT) Received: from gandalf.ozlabs.org (mail.ozlabs.org [IPv6:2404:9400:2221:ea00::3]) by gandalf.ozlabs.org (Postfix) with ESMTP id 4KMx7T3rMKz4xc3 for ; Tue, 22 Mar 2022 14:00:37 +1100 (AEDT) Received: by gandalf.ozlabs.org (Postfix) id 4KMx7T3nG4z4xcq; Tue, 22 Mar 2022 14:00:37 +1100 (AEDT) Delivered-To: patchwork-incoming@ozlabs.org Authentication-Results: gandalf.ozlabs.org; spf=pass (sender SPF authorized) smtp.mailfrom=vger.kernel.org (client-ip=2620:137:e000::1:20; helo=out1.vger.email; envelope-from=linux-ext4-owner@vger.kernel.org; receiver=) Authentication-Results: gandalf.ozlabs.org; dkim=pass (2048-bit key; unprotected) header.d=collabora.com header.i=@collabora.com header.a=rsa-sha256 header.s=mail header.b=ajAmRddI; dkim-atps=neutral Received: from out1.vger.email (out1.vger.email [IPv6:2620:137:e000::1:20]) by gandalf.ozlabs.org (Postfix) with ESMTP id 4KMx7T3bZMz4xc3 for ; Tue, 22 Mar 2022 14:00:37 +1100 (AEDT) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S235744AbiCVDCB (ORCPT ); Mon, 21 Mar 2022 23:02:01 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:51926 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S235756AbiCVDBw (ORCPT ); Mon, 21 Mar 2022 23:01:52 -0400 Received: from bhuna.collabora.co.uk (bhuna.collabora.co.uk [46.235.227.227]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id E4AF62042BE for ; Mon, 21 Mar 2022 20:00:25 -0700 (PDT) Received: from [127.0.0.1] (localhost [127.0.0.1]) (Authenticated sender: krisman) with ESMTPSA id 5A31F1F41054 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=collabora.com; s=mail; t=1647918024; bh=Ne9gHo1W22hoN0fs0VlYikMKOzIWbakWkN+3Rnc2qZ0=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=ajAmRddIRpaT3hRRyoN/i4QKBs2ffsWPtOmaFq5o441NXaYeRZWstfV22b8lXqZhP HyJRV3f2AuMoHCYbBQzOHIWq9JefW2d5NbVDdZh0swSb6H79I70SBY0oclJUXjfhWy +IDf2KDgTzDgq0Mz0gMVjg3rlId8li+wPmcdg6oD+M6lKds1y7jjEbaK59PwD60p0d Z9kNun68XzRquy9b7EMdUuuHv8WCmU7nLBldSU29cKyaG/+kKxFWGlBTkkZIgdUxCF DpiBQWJ3yjfClipogNkTGU9NuWAesphaW5dz3jsAKg5r92CAzQEpfCwT962mG0H2Xb cBgzybZ0/oAAA== From: Gabriel Krisman Bertazi To: tytso@mit.edu Cc: ebiggers@kernel.org, jaegeuk@kernel.org, linux-ext4@vger.kernel.org, Gabriel Krisman Bertazi , kernel@collabora.com Subject: [PATCH 3/5] ext4: Implement ci comparison using fscrypt_name Date: Mon, 21 Mar 2022 23:00:02 -0400 Message-Id: <20220322030004.148560-4-krisman@collabora.com> X-Mailer: git-send-email 2.35.1 In-Reply-To: <20220322030004.148560-1-krisman@collabora.com> References: <20220322030004.148560-1-krisman@collabora.com> MIME-Version: 1.0 X-Spam-Status: No, score=-2.1 required=5.0 tests=BAYES_00,DKIM_SIGNED, DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,SPF_HELO_PASS,SPF_PASS, T_SCC_BODY_TEXT_LINE,UNPARSEABLE_RELAY 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 By using fscrypt_name here, we can hide most of the caching casefold logic from ext4. The condition in ext4_match is now quite redundant, but this is addressed in the next patch. Signed-off-by: Gabriel Krisman Bertazi --- fs/ext4/namei.c | 26 ++++++++++++-------------- include/linux/fscrypt.h | 4 ++++ 2 files changed, 16 insertions(+), 14 deletions(-) diff --git a/fs/ext4/namei.c b/fs/ext4/namei.c index 8976e5a28c73..71b4b05fae89 100644 --- a/fs/ext4/namei.c +++ b/fs/ext4/namei.c @@ -1321,10 +1321,9 @@ static void dx_insert_block(struct dx_frame *frame, u32 hash, ext4_lblk_t block) /** * ext4_ci_compare() - Match (case-insensitive) a name with a dirent. * @parent: Inode of the parent of the dentry. - * @name: name under lookup. + * @fname: name under lookup. * @de_name: Dirent name. * @de_name_len: dirent name length. - * @quick: whether @name is already casefolded. * * Test whether a case-insensitive directory entry matches the filename * being searched. If quick is set, the @name being looked up is @@ -1333,8 +1332,9 @@ static void dx_insert_block(struct dx_frame *frame, u32 hash, ext4_lblk_t block) * Return: > 0 if the directory entry matches, 0 if it doesn't match, or * < 0 on error. */ -static int ext4_ci_compare(const struct inode *parent, const struct qstr *name, - u8 *de_name, size_t de_name_len, bool quick) +static int ext4_ci_compare(const struct inode *parent, + const struct fscrypt_name *fname, + u8 *de_name, size_t de_name_len) { const struct super_block *sb = parent->i_sb; const struct unicode_map *um = sb->s_encoding; @@ -1357,10 +1357,10 @@ static int ext4_ci_compare(const struct inode *parent, const struct qstr *name, entry.len = decrypted_name.len; } - if (quick) - ret = utf8_strncasecmp_folded(um, name, &entry); + if (fname->cf_name.name) + ret = utf8_strncasecmp_folded(um, &fname->cf_name, &entry); else - ret = utf8_strncasecmp(um, name, &entry); + ret = utf8_strncasecmp(um, fname->usr_fname, &entry); if (!ret) match = true; @@ -1370,8 +1370,8 @@ static int ext4_ci_compare(const struct inode *parent, const struct qstr *name, * the names have invalid characters. */ ret = 0; - match = ((name->len == entry.len) && - !memcmp(name->name, entry.name, entry.len)); + match = ((fname->usr_fname->len == entry.len) && + !memcmp(fname->usr_fname->name, entry.name, entry.len)); } out: @@ -1440,6 +1440,8 @@ static bool ext4_match(struct inode *parent, #endif #if IS_ENABLED(CONFIG_UNICODE) + f.cf_name = fname->cf_name; + if (parent->i_sb->s_encoding && IS_CASEFOLDED(parent) && (!IS_ENCRYPTED(parent) || fscrypt_has_encryption_key(parent))) { if (fname->cf_name.name) { @@ -1451,13 +1453,9 @@ static bool ext4_match(struct inode *parent, return false; } } - ret = ext4_ci_compare(parent, &fname->cf_name, de->name, - de->name_len, true); - } else { - ret = ext4_ci_compare(parent, fname->usr_fname, - de->name, de->name_len, false); } + ret = ext4_ci_compare(parent, &f, de->name, de->name_len); if (ret < 0) { /* * Treat comparison errors as not a match. The diff --git a/include/linux/fscrypt.h b/include/linux/fscrypt.h index 91ea9477e9bd..5dc4b3c805e4 100644 --- a/include/linux/fscrypt.h +++ b/include/linux/fscrypt.h @@ -36,6 +36,10 @@ struct fscrypt_name { u32 minor_hash; struct fscrypt_str crypto_buf; bool is_nokey_name; + +#ifdef CONFIG_UNICODE + struct qstr cf_name; +#endif }; #define FSTR_INIT(n, l) { .name = n, .len = l } From patchwork Tue Mar 22 03:00:03 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Gabriel Krisman Bertazi X-Patchwork-Id: 1608012 Return-Path: X-Original-To: incoming@patchwork.ozlabs.org Delivered-To: patchwork-incoming@bilbo.ozlabs.org Authentication-Results: bilbo.ozlabs.org; dkim=pass (2048-bit key; unprotected) header.d=collabora.com header.i=@collabora.com header.a=rsa-sha256 header.s=mail header.b=WbonEwN4; dkim-atps=neutral Received: from gandalf.ozlabs.org (gandalf.ozlabs.org [150.107.74.76]) (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 bilbo.ozlabs.org (Postfix) with ESMTPS id 4KMx7V5TwSz9s3q for ; Tue, 22 Mar 2022 14:00:38 +1100 (AEDT) Received: from gandalf.ozlabs.org (gandalf.ozlabs.org [150.107.74.76]) by gandalf.ozlabs.org (Postfix) with ESMTP id 4KMx7V52HRz4xc3 for ; Tue, 22 Mar 2022 14:00:38 +1100 (AEDT) Received: by gandalf.ozlabs.org (Postfix) id 4KMx7V4z55z4xcq; Tue, 22 Mar 2022 14:00:38 +1100 (AEDT) Delivered-To: patchwork-incoming@ozlabs.org Authentication-Results: gandalf.ozlabs.org; spf=pass (sender SPF authorized) smtp.mailfrom=vger.kernel.org (client-ip=2620:137:e000::1:20; helo=out1.vger.email; envelope-from=linux-ext4-owner@vger.kernel.org; receiver=) Authentication-Results: gandalf.ozlabs.org; dkim=pass (2048-bit key; unprotected) header.d=collabora.com header.i=@collabora.com header.a=rsa-sha256 header.s=mail header.b=WbonEwN4; dkim-atps=neutral Received: from out1.vger.email (out1.vger.email [IPv6:2620:137:e000::1:20]) by gandalf.ozlabs.org (Postfix) with ESMTP id 4KMx7V4vQpz4xc3 for ; Tue, 22 Mar 2022 14:00:38 +1100 (AEDT) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S235764AbiCVDCC (ORCPT ); Mon, 21 Mar 2022 23:02:02 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:52280 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S235759AbiCVDB4 (ORCPT ); Mon, 21 Mar 2022 23:01:56 -0400 Received: from bhuna.collabora.co.uk (bhuna.collabora.co.uk [IPv6:2a00:1098:0:82:1000:25:2eeb:e3e3]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id B97363CCCC3 for ; Mon, 21 Mar 2022 20:00:29 -0700 (PDT) Received: from [127.0.0.1] (localhost [127.0.0.1]) (Authenticated sender: krisman) with ESMTPSA id 4FE281F413D3 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=collabora.com; s=mail; t=1647918028; bh=kqkciCrqxPaGMYalJ3yGrHWLeiWSTcQ+ToF7JIySmPs=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=WbonEwN4BS3rmF3IbyukzEHZv97o/vcmyOCI9J1lOz9QzDLuF67Rw44UzhTBoytFN xpdEIwoZy9qe+Nd1AsjdwNv3YemErR5E1Z98rp2Z5OPdjohMw4sMapG7wWcU1PP7aS AxwELfHdpCX3KSKTho6xTlY0CNkMIXoxsd9GDK+FSOWeG1bkaSNCwYYqGrPrgtnMiN 375/wwPZo5XaBEr9wgns2pui15yMWg2q+VBZNd6+blmfT+w/0T1XbmbGlhH9MbBW1I 5eFUJiyfaw9F6yve3ycFpsKbP6f9QERxnCWt/TfQcFmAFRDY7RaQEoCDYLJauCit8E oGnQXGUPaEUqg== From: Gabriel Krisman Bertazi To: tytso@mit.edu Cc: ebiggers@kernel.org, jaegeuk@kernel.org, linux-ext4@vger.kernel.org, Gabriel Krisman Bertazi , kernel@collabora.com Subject: [PATCH 4/5] ext4: Simplify hash check on ext4_match Date: Mon, 21 Mar 2022 23:00:03 -0400 Message-Id: <20220322030004.148560-5-krisman@collabora.com> X-Mailer: git-send-email 2.35.1 In-Reply-To: <20220322030004.148560-1-krisman@collabora.com> References: <20220322030004.148560-1-krisman@collabora.com> MIME-Version: 1.0 X-Spam-Status: No, score=-2.1 required=5.0 tests=BAYES_00,DKIM_SIGNED, DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,SPF_HELO_PASS,SPF_PASS, T_SCC_BODY_TEXT_LINE,UNPARSEABLE_RELAY 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 existence of fname->cf_name.name requires s_encoding & IS_CASEFOLDED, therefore this can be simplified. Signed-off-by: Gabriel Krisman Bertazi --- fs/ext4/namei.c | 18 ++++++------------ 1 file changed, 6 insertions(+), 12 deletions(-) diff --git a/fs/ext4/namei.c b/fs/ext4/namei.c index 71b4b05fae89..8520115cd5c2 100644 --- a/fs/ext4/namei.c +++ b/fs/ext4/namei.c @@ -1442,19 +1442,13 @@ static bool ext4_match(struct inode *parent, #if IS_ENABLED(CONFIG_UNICODE) f.cf_name = fname->cf_name; - if (parent->i_sb->s_encoding && IS_CASEFOLDED(parent) && - (!IS_ENCRYPTED(parent) || fscrypt_has_encryption_key(parent))) { - if (fname->cf_name.name) { - if (IS_ENCRYPTED(parent)) { - if (fname->hinfo.hash != EXT4_DIRENT_HASH(de) || - fname->hinfo.minor_hash != - EXT4_DIRENT_MINOR_HASH(de)) { - - return false; - } - } - } + if (IS_ENCRYPTED(parent) && fname->cf_name.name) { + if (fname->hinfo.hash != EXT4_DIRENT_HASH(de) || + fname->hinfo.minor_hash != EXT4_DIRENT_MINOR_HASH(de)) + return false; + } + if (parent->i_sb->s_encoding && IS_CASEFOLDED(parent)) { ret = ext4_ci_compare(parent, &f, de->name, de->name_len); if (ret < 0) { /* From patchwork Tue Mar 22 03:00:04 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Gabriel Krisman Bertazi X-Patchwork-Id: 1608013 Return-Path: X-Original-To: incoming@patchwork.ozlabs.org Delivered-To: patchwork-incoming@bilbo.ozlabs.org Authentication-Results: bilbo.ozlabs.org; dkim=pass (2048-bit key; unprotected) header.d=collabora.com header.i=@collabora.com header.a=rsa-sha256 header.s=mail header.b=mT3VhGo0; dkim-atps=neutral Received: from gandalf.ozlabs.org (gandalf.ozlabs.org [150.107.74.76]) (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 bilbo.ozlabs.org (Postfix) with ESMTPS id 4KMx7W4Dxqz9s3q for ; Tue, 22 Mar 2022 14:00:39 +1100 (AEDT) Received: from gandalf.ozlabs.org (mail.ozlabs.org [IPv6:2404:9400:2221:ea00::3]) by gandalf.ozlabs.org (Postfix) with ESMTP id 4KMx7W3rCRz4xXV for ; Tue, 22 Mar 2022 14:00:39 +1100 (AEDT) Received: by gandalf.ozlabs.org (Postfix) id 4KMx7W3nQQz4xc3; Tue, 22 Mar 2022 14:00:39 +1100 (AEDT) Delivered-To: patchwork-incoming@ozlabs.org Authentication-Results: gandalf.ozlabs.org; spf=pass (sender SPF authorized) smtp.mailfrom=vger.kernel.org (client-ip=2620:137:e000::1:20; helo=out1.vger.email; envelope-from=linux-ext4-owner@vger.kernel.org; receiver=) Authentication-Results: gandalf.ozlabs.org; dkim=pass (2048-bit key; unprotected) header.d=collabora.com header.i=@collabora.com header.a=rsa-sha256 header.s=mail header.b=mT3VhGo0; dkim-atps=neutral Received: from out1.vger.email (out1.vger.email [IPv6:2620:137:e000::1:20]) by gandalf.ozlabs.org (Postfix) with ESMTP id 4KMx7W3jHyz4xXV for ; Tue, 22 Mar 2022 14:00:39 +1100 (AEDT) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S235758AbiCVDCE (ORCPT ); Mon, 21 Mar 2022 23:02:04 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:52502 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S235761AbiCVDB7 (ORCPT ); Mon, 21 Mar 2022 23:01:59 -0400 Received: from bhuna.collabora.co.uk (bhuna.collabora.co.uk [46.235.227.227]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 87C163CC079 for ; Mon, 21 Mar 2022 20:00:33 -0700 (PDT) Received: from [127.0.0.1] (localhost [127.0.0.1]) (Authenticated sender: krisman) with ESMTPSA id 25A7A1F41054 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=collabora.com; s=mail; t=1647918032; bh=Tx1VCof8d5xVQ2X1dQ+CXCV8k1m84bsTT2v1ZSmYgKQ=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=mT3VhGo0OF5Oje5JSQDFh9aLAuaR2yow24oQk1pUUyMeXaHREW9HzYr+kVs7zyoRP m8r00JXv8/J+1RCZRKiZJWRoDigsCneKVf1U7tw+TGytGjoO8p7A3LM0AzOYcSv0Vl LCbdgAkP/qX1m0wfWrUbNL2RyffdICIBQZAKxUQfmwzRT336Q1DeiXPEqBf1wGFE3q bFXETyhE2DjhzfGeiYxkymX5hxXjGSQY2QVDDQjiRPmpBCwjZ/HIoT3hHg+YGHslMo JHTUsqXwPjqdU5RK9CynA9Gx73UTQiapgWTxNODulHDfelRc5Z/B2rZ5/UkKr/y1L5 /aNt143Ng1yig== From: Gabriel Krisman Bertazi To: tytso@mit.edu Cc: ebiggers@kernel.org, jaegeuk@kernel.org, linux-ext4@vger.kernel.org, Gabriel Krisman Bertazi , kernel@collabora.com Subject: [PATCH 5/5] ext4: Log error when lookup of encoded dentry fails Date: Mon, 21 Mar 2022 23:00:04 -0400 Message-Id: <20220322030004.148560-6-krisman@collabora.com> X-Mailer: git-send-email 2.35.1 In-Reply-To: <20220322030004.148560-1-krisman@collabora.com> References: <20220322030004.148560-1-krisman@collabora.com> MIME-Version: 1.0 X-Spam-Status: No, score=-2.1 required=5.0 tests=BAYES_00,DKIM_SIGNED, DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,SPF_HELO_PASS,SPF_PASS, T_SCC_BODY_TEXT_LINE,UNPARSEABLE_RELAY 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 If the volume is in strict mode, ext4_ci_compare can report a broken encoding name. This will not trigger on a bad lookup, which is caught earlier, only if the actual disk name is bad. Signed-off-by: Gabriel Krisman Bertazi --- fs/ext4/namei.c | 3 +++ 1 file changed, 3 insertions(+) diff --git a/fs/ext4/namei.c b/fs/ext4/namei.c index 8520115cd5c2..c321c6fdb4ae 100644 --- a/fs/ext4/namei.c +++ b/fs/ext4/namei.c @@ -1456,6 +1456,9 @@ static bool ext4_match(struct inode *parent, * only case where it happens is on a disk * corruption or ENOMEM. */ + if (ret == -EINVAL) + EXT4_ERROR_INODE(parent, + "Bad encoded file in directory"); return false; } return ret;