From patchwork Sat May 14 17:22:46 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: "Ritesh Harjani (IBM)" X-Patchwork-Id: 12849900 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 5F8C3C433F5 for ; Sat, 14 May 2022 17:23:06 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S234589AbiENRXF (ORCPT ); Sat, 14 May 2022 13:23:05 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:34598 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S229447AbiENRXE (ORCPT ); Sat, 14 May 2022 13:23:04 -0400 Received: from mail-pf1-x433.google.com (mail-pf1-x433.google.com [IPv6:2607:f8b0:4864:20::433]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id ABBB8B47; Sat, 14 May 2022 10:23:03 -0700 (PDT) Received: by mail-pf1-x433.google.com with SMTP id c14so10419882pfn.2; Sat, 14 May 2022 10:23:03 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20210112; h=from:to:cc:subject:date:message-id:in-reply-to:references :mime-version:content-transfer-encoding; bh=ZtorijWtGeqsjcvr/gQHDZ+uw73+I7h2d4Z+6J5lltE=; b=EaRmZdEz1a3iBXZ0qItXLXRb5yNBcR20hNtRphT/SnDYr6IpSXEJq6tGAu9/mCzFo0 fpVBnU/LO4+TrtMxCPLwzik1kaiEQJS/DO/5w0J6kns8CnDBgzsC9mR5ZWeEYSDnOUU/ EGREgakMV1xq8bucDMBz/t5Av765fvTdHA99M7xrlliSf08W6f+JypCmnJ2Bi7G4tvLU eT5lHe4v7syX31K12m9ybFy/bTWJT3TUWsqNVawrD3fJ9TuJreQP3Xd9cW2ofmq4AZjG XymlQN/V2lqesaptq2ybpNwXP/80O2c5MWrWyUee5jf0bFPlDS9+HZgUu+b3y/CpTxbs BFwg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=x-gm-message-state:from:to:cc:subject:date:message-id:in-reply-to :references:mime-version:content-transfer-encoding; bh=ZtorijWtGeqsjcvr/gQHDZ+uw73+I7h2d4Z+6J5lltE=; b=2i6idzAELRnTCh9ZU5FogDx4QccX8F79d0q8xxLTeMUhP/NcxkfE6NZpQXBv9twr7j s7UMDN5/XG/wIH8WK92rkAwLsXdhddlcaC5nbdT5Enr7rijEsAstuslfb6IF0w60K4ww LMHkP3KLS9W4EHuL2NOeGxYclmmaefSHK0E0Xl+PkZlgdKAn0AMSv4KGj6Ktu7Yndps0 r55IcWC0lTT+cRQuV0rqb7YIL7Tg7JbSqi3UbFyIaN+gePIR3u3sCyH0Du/VrHkRlzgE TeL04waQsR28sRHX+bXjAYHyvh+T/kyc1lDy6BJ0apZiqPhvTx5gWgVjAyHQ3q3lJxf9 uArw== X-Gm-Message-State: AOAM531b2vkQ8QB26dHzWQZDhzjKDblYivtUx0/Acwh32Z4Fyt3d2jEC nU0LOXIoPafsBfgJdsXE4iIR30/noS0= X-Google-Smtp-Source: ABdhPJxiES+c1XmUcgTcard16jYTHrZT2FLjQ6ozl/SAM7vIGFP6zCF3g/LJqTd/ulBQRbFTp8wJYg== X-Received: by 2002:a63:4e62:0:b0:398:cb40:19b0 with SMTP id o34-20020a634e62000000b00398cb4019b0mr8650058pgl.445.1652548983117; Sat, 14 May 2022 10:23:03 -0700 (PDT) Received: from localhost ([2406:7400:63:532d:c4bb:97f7:b03d:2c53]) by smtp.gmail.com with ESMTPSA id a30-20020a62d41e000000b0050dc7628130sm3949814pfh.10.2022.05.14.10.23.02 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Sat, 14 May 2022 10:23:02 -0700 (PDT) From: Ritesh Harjani To: linux-ext4@vger.kernel.org Cc: linux-fscrypt@vger.kernel.org, Theodore Ts'o , Eric Biggers , Jan Kara , Ritesh Harjani Subject: [PATCHv2 1/3] ext4: Move ext4 crypto code to its own file crypto.c Date: Sat, 14 May 2022 22:52:46 +0530 Message-Id: <4f6b9ff4411ced6591f858119feb025300ecf918.1652539361.git.ritesh.list@gmail.com> X-Mailer: git-send-email 2.31.1 In-Reply-To: References: MIME-Version: 1.0 Precedence: bulk List-ID: X-Mailing-List: linux-fscrypt@vger.kernel.org This is to cleanup super.c file which has grown quite large. So, start moving ext4 crypto related code to where it should be in the first place i.e. fs/ext4/crypto.c Signed-off-by: Ritesh Harjani Reviewed-by: Eric Biggers --- fs/ext4/Makefile | 1 + fs/ext4/crypto.c | 127 +++++++++++++++++++++++++++++++++++++++++++++++ fs/ext4/ext4.h | 3 ++ fs/ext4/super.c | 122 --------------------------------------------- 4 files changed, 131 insertions(+), 122 deletions(-) create mode 100644 fs/ext4/crypto.c diff --git a/fs/ext4/Makefile b/fs/ext4/Makefile index 7d89142e1421..72206a292676 100644 --- a/fs/ext4/Makefile +++ b/fs/ext4/Makefile @@ -17,3 +17,4 @@ ext4-$(CONFIG_EXT4_FS_SECURITY) += xattr_security.o ext4-inode-test-objs += inode-test.o obj-$(CONFIG_EXT4_KUNIT_TESTS) += ext4-inode-test.o ext4-$(CONFIG_FS_VERITY) += verity.o +ext4-$(CONFIG_FS_ENCRYPTION) += crypto.o diff --git a/fs/ext4/crypto.c b/fs/ext4/crypto.c new file mode 100644 index 000000000000..e5413c0970ee --- /dev/null +++ b/fs/ext4/crypto.c @@ -0,0 +1,127 @@ +// SPDX-License-Identifier: GPL-2.0 + +#include + +#include "ext4.h" +#include "xattr.h" +#include "ext4_jbd2.h" + +static int ext4_get_context(struct inode *inode, void *ctx, size_t len) +{ + return ext4_xattr_get(inode, EXT4_XATTR_INDEX_ENCRYPTION, + EXT4_XATTR_NAME_ENCRYPTION_CONTEXT, ctx, len); +} + +static int ext4_set_context(struct inode *inode, const void *ctx, size_t len, + void *fs_data) +{ + handle_t *handle = fs_data; + int res, res2, credits, retries = 0; + + /* + * Encrypting the root directory is not allowed because e2fsck expects + * lost+found to exist and be unencrypted, and encrypting the root + * directory would imply encrypting the lost+found directory as well as + * the filename "lost+found" itself. + */ + if (inode->i_ino == EXT4_ROOT_INO) + return -EPERM; + + if (WARN_ON_ONCE(IS_DAX(inode) && i_size_read(inode))) + return -EINVAL; + + if (ext4_test_inode_flag(inode, EXT4_INODE_DAX)) + return -EOPNOTSUPP; + + res = ext4_convert_inline_data(inode); + if (res) + return res; + + /* + * If a journal handle was specified, then the encryption context is + * being set on a new inode via inheritance and is part of a larger + * transaction to create the inode. Otherwise the encryption context is + * being set on an existing inode in its own transaction. Only in the + * latter case should the "retry on ENOSPC" logic be used. + */ + + if (handle) { + res = ext4_xattr_set_handle(handle, inode, + EXT4_XATTR_INDEX_ENCRYPTION, + EXT4_XATTR_NAME_ENCRYPTION_CONTEXT, + ctx, len, 0); + if (!res) { + ext4_set_inode_flag(inode, EXT4_INODE_ENCRYPT); + ext4_clear_inode_state(inode, + EXT4_STATE_MAY_INLINE_DATA); + /* + * Update inode->i_flags - S_ENCRYPTED will be enabled, + * S_DAX may be disabled + */ + ext4_set_inode_flags(inode, false); + } + return res; + } + + res = dquot_initialize(inode); + if (res) + return res; +retry: + res = ext4_xattr_set_credits(inode, len, false /* is_create */, + &credits); + if (res) + return res; + + handle = ext4_journal_start(inode, EXT4_HT_MISC, credits); + if (IS_ERR(handle)) + return PTR_ERR(handle); + + res = ext4_xattr_set_handle(handle, inode, EXT4_XATTR_INDEX_ENCRYPTION, + EXT4_XATTR_NAME_ENCRYPTION_CONTEXT, + ctx, len, 0); + if (!res) { + ext4_set_inode_flag(inode, EXT4_INODE_ENCRYPT); + /* + * Update inode->i_flags - S_ENCRYPTED will be enabled, + * S_DAX may be disabled + */ + ext4_set_inode_flags(inode, false); + res = ext4_mark_inode_dirty(handle, inode); + if (res) + EXT4_ERROR_INODE(inode, "Failed to mark inode dirty"); + } + res2 = ext4_journal_stop(handle); + + if (res == -ENOSPC && ext4_should_retry_alloc(inode->i_sb, &retries)) + goto retry; + if (!res) + res = res2; + return res; +} + +static const union fscrypt_policy *ext4_get_dummy_policy(struct super_block *sb) +{ + return EXT4_SB(sb)->s_dummy_enc_policy.policy; +} + +static bool ext4_has_stable_inodes(struct super_block *sb) +{ + return ext4_has_feature_stable_inodes(sb); +} + +static void ext4_get_ino_and_lblk_bits(struct super_block *sb, + int *ino_bits_ret, int *lblk_bits_ret) +{ + *ino_bits_ret = 8 * sizeof(EXT4_SB(sb)->s_es->s_inodes_count); + *lblk_bits_ret = 8 * sizeof(ext4_lblk_t); +} + +const struct fscrypt_operations ext4_cryptops = { + .key_prefix = "ext4:", + .get_context = ext4_get_context, + .set_context = ext4_set_context, + .get_dummy_policy = ext4_get_dummy_policy, + .empty_dir = ext4_empty_dir, + .has_stable_inodes = ext4_has_stable_inodes, + .get_ino_and_lblk_bits = ext4_get_ino_and_lblk_bits, +}; diff --git a/fs/ext4/ext4.h b/fs/ext4/ext4.h index a743b1e3b89e..9100f0ba4a52 100644 --- a/fs/ext4/ext4.h +++ b/fs/ext4/ext4.h @@ -2731,6 +2731,9 @@ extern int ext4_fname_setup_ci_filename(struct inode *dir, struct ext4_filename *fname); #endif +/* ext4 encryption related stuff goes here crypto.c */ +extern const struct fscrypt_operations ext4_cryptops; + #ifdef CONFIG_FS_ENCRYPTION static inline void ext4_fname_from_fscrypt_name(struct ext4_filename *dst, const struct fscrypt_name *src) diff --git a/fs/ext4/super.c b/fs/ext4/super.c index 1847b46af808..e6cfd338712c 100644 --- a/fs/ext4/super.c +++ b/fs/ext4/super.c @@ -1492,128 +1492,6 @@ static int ext4_nfs_commit_metadata(struct inode *inode) return ext4_write_inode(inode, &wbc); } -#ifdef CONFIG_FS_ENCRYPTION -static int ext4_get_context(struct inode *inode, void *ctx, size_t len) -{ - return ext4_xattr_get(inode, EXT4_XATTR_INDEX_ENCRYPTION, - EXT4_XATTR_NAME_ENCRYPTION_CONTEXT, ctx, len); -} - -static int ext4_set_context(struct inode *inode, const void *ctx, size_t len, - void *fs_data) -{ - handle_t *handle = fs_data; - int res, res2, credits, retries = 0; - - /* - * Encrypting the root directory is not allowed because e2fsck expects - * lost+found to exist and be unencrypted, and encrypting the root - * directory would imply encrypting the lost+found directory as well as - * the filename "lost+found" itself. - */ - if (inode->i_ino == EXT4_ROOT_INO) - return -EPERM; - - if (WARN_ON_ONCE(IS_DAX(inode) && i_size_read(inode))) - return -EINVAL; - - if (ext4_test_inode_flag(inode, EXT4_INODE_DAX)) - return -EOPNOTSUPP; - - res = ext4_convert_inline_data(inode); - if (res) - return res; - - /* - * If a journal handle was specified, then the encryption context is - * being set on a new inode via inheritance and is part of a larger - * transaction to create the inode. Otherwise the encryption context is - * being set on an existing inode in its own transaction. Only in the - * latter case should the "retry on ENOSPC" logic be used. - */ - - if (handle) { - res = ext4_xattr_set_handle(handle, inode, - EXT4_XATTR_INDEX_ENCRYPTION, - EXT4_XATTR_NAME_ENCRYPTION_CONTEXT, - ctx, len, 0); - if (!res) { - ext4_set_inode_flag(inode, EXT4_INODE_ENCRYPT); - ext4_clear_inode_state(inode, - EXT4_STATE_MAY_INLINE_DATA); - /* - * Update inode->i_flags - S_ENCRYPTED will be enabled, - * S_DAX may be disabled - */ - ext4_set_inode_flags(inode, false); - } - return res; - } - - res = dquot_initialize(inode); - if (res) - return res; -retry: - res = ext4_xattr_set_credits(inode, len, false /* is_create */, - &credits); - if (res) - return res; - - handle = ext4_journal_start(inode, EXT4_HT_MISC, credits); - if (IS_ERR(handle)) - return PTR_ERR(handle); - - res = ext4_xattr_set_handle(handle, inode, EXT4_XATTR_INDEX_ENCRYPTION, - EXT4_XATTR_NAME_ENCRYPTION_CONTEXT, - ctx, len, 0); - if (!res) { - ext4_set_inode_flag(inode, EXT4_INODE_ENCRYPT); - /* - * Update inode->i_flags - S_ENCRYPTED will be enabled, - * S_DAX may be disabled - */ - ext4_set_inode_flags(inode, false); - res = ext4_mark_inode_dirty(handle, inode); - if (res) - EXT4_ERROR_INODE(inode, "Failed to mark inode dirty"); - } - res2 = ext4_journal_stop(handle); - - if (res == -ENOSPC && ext4_should_retry_alloc(inode->i_sb, &retries)) - goto retry; - if (!res) - res = res2; - return res; -} - -static const union fscrypt_policy *ext4_get_dummy_policy(struct super_block *sb) -{ - return EXT4_SB(sb)->s_dummy_enc_policy.policy; -} - -static bool ext4_has_stable_inodes(struct super_block *sb) -{ - return ext4_has_feature_stable_inodes(sb); -} - -static void ext4_get_ino_and_lblk_bits(struct super_block *sb, - int *ino_bits_ret, int *lblk_bits_ret) -{ - *ino_bits_ret = 8 * sizeof(EXT4_SB(sb)->s_es->s_inodes_count); - *lblk_bits_ret = 8 * sizeof(ext4_lblk_t); -} - -static const struct fscrypt_operations ext4_cryptops = { - .key_prefix = "ext4:", - .get_context = ext4_get_context, - .set_context = ext4_set_context, - .get_dummy_policy = ext4_get_dummy_policy, - .empty_dir = ext4_empty_dir, - .has_stable_inodes = ext4_has_stable_inodes, - .get_ino_and_lblk_bits = ext4_get_ino_and_lblk_bits, -}; -#endif - #ifdef CONFIG_QUOTA static const char * const quotatypes[] = INITQFNAMES; #define QTYPE2NAME(t) (quotatypes[t]) From patchwork Sat May 14 17:22:47 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: "Ritesh Harjani (IBM)" X-Patchwork-Id: 12849901 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 BE542C433EF for ; Sat, 14 May 2022 17:23:12 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S229447AbiENRXM (ORCPT ); Sat, 14 May 2022 13:23:12 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:34762 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S234604AbiENRXJ (ORCPT ); Sat, 14 May 2022 13:23:09 -0400 Received: from mail-pf1-x434.google.com (mail-pf1-x434.google.com [IPv6:2607:f8b0:4864:20::434]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id A7D68101EA; Sat, 14 May 2022 10:23:08 -0700 (PDT) Received: by mail-pf1-x434.google.com with SMTP id p8so10384322pfh.8; Sat, 14 May 2022 10:23:08 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20210112; h=from:to:cc:subject:date:message-id:in-reply-to:references :mime-version:content-transfer-encoding; bh=dQwy2TXFojnREtRP7UzV1zaEesI5jCcgp2eHJEFcnLg=; b=XLEM2AAshCIk0rTZg+IO7FhrgHD8k0etKL8VLAMPUatj/aOLL76zH54ss18P255jo0 wF9xL5lOIC1jH4l46uK6IsCzNI6Gq1boU93Edmrn3XtP8LrQrY3DvtoQ8qTb+OP9EeYs KqSm6mU/ZH05VXhs2KxxlDcg1AjoIKIr0gNGOFV/5vke6rb8IaOv9Cm85Ea3MPcHazZY tGel5Sl4XfwuBDktIeXC+01uZD0lhuWkDicMBitEMsLWLlh5fNRx8hvp5CKO7RmE+HNv oKCP1H/wtxD1g6aCgIqCmElxWCdo+ya/sleXtyKG3NtIlGLEELdExXTGzHPUcX9kQnAI rKuQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=x-gm-message-state:from:to:cc:subject:date:message-id:in-reply-to :references:mime-version:content-transfer-encoding; bh=dQwy2TXFojnREtRP7UzV1zaEesI5jCcgp2eHJEFcnLg=; b=QGcyqVFeQ6gW58Yzw3onLfNWnau9P+zC/qPVaOJ0JQ8YFCQIRZ+Lal5X0qX+5cZI02 EjZn0Eu6r4aA6JK8q0s68nNUox+b87MFLbjZ8HCtpx5aLNst2T7Wr7KiV3MkqqoTx6Eo Bn9Vf7fRwXQxWQulHNffYsCYjV8tsDIWnxTvyn0ZT+i8yIehrXJoGMhqbrblllc4Ass8 u+t0lJ2stxACPl2oohwOMmLp60loTz4ifOw3COC8vwecAjDykoNBq3YJaxmRdtLOr4wg YisRsTZfZxks4R7H2IFRLSlRa4KbaT33W2EtYNaNAPc45AdW/Rg4cixYX2mv3SOpaZ1b 39Mg== X-Gm-Message-State: AOAM5316btn06jsZxDt7TV+zvHeDmkTP4u+aRNC+IefmmRk9SAc9hWM1 ZynKsgH/bduWZvWmrfQW7zgK9PlZiOw= X-Google-Smtp-Source: ABdhPJxQyIZIPeOsePJp4Zk119skicFaVrOqRrF2qNeYS4XI9MvwYjSbaHnHOJdEdxjqGJzxXErYpg== X-Received: by 2002:a63:8b42:0:b0:3c6:2f31:2dfc with SMTP id j63-20020a638b42000000b003c62f312dfcmr8455776pge.285.1652548988106; Sat, 14 May 2022 10:23:08 -0700 (PDT) Received: from localhost ([2406:7400:63:532d:c4bb:97f7:b03d:2c53]) by smtp.gmail.com with ESMTPSA id r19-20020a170903021300b0015e8d4eb26esm3960538plh.184.2022.05.14.10.23.07 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Sat, 14 May 2022 10:23:07 -0700 (PDT) From: Ritesh Harjani To: linux-ext4@vger.kernel.org Cc: linux-fscrypt@vger.kernel.org, Theodore Ts'o , Eric Biggers , Jan Kara , Ritesh Harjani Subject: [PATCHv2 2/3] ext4: Cleanup function defs from ext4.h into crypto.c Date: Sat, 14 May 2022 22:52:47 +0530 Message-Id: <4120e61a1f68c225eb7a27a7a529fd0847270010.1652539361.git.ritesh.list@gmail.com> X-Mailer: git-send-email 2.31.1 In-Reply-To: References: MIME-Version: 1.0 Precedence: bulk List-ID: X-Mailing-List: linux-fscrypt@vger.kernel.org Some of these functions when CONFIG_FS_ENCRYPTION is enabled are not really inline (let compiler be the best judge of it). Remove inline and move them into crypto.c where they should be present. Signed-off-by: Ritesh Harjani Reviewed-by: Eric Biggers --- fs/ext4/crypto.c | 65 ++++++++++++++++++++++++++++++++++++++++++++ fs/ext4/ext4.h | 70 +++++------------------------------------------- 2 files changed, 71 insertions(+), 64 deletions(-) diff --git a/fs/ext4/crypto.c b/fs/ext4/crypto.c index e5413c0970ee..f8333927f0f6 100644 --- a/fs/ext4/crypto.c +++ b/fs/ext4/crypto.c @@ -6,6 +6,71 @@ #include "xattr.h" #include "ext4_jbd2.h" +static void ext4_fname_from_fscrypt_name(struct ext4_filename *dst, + const struct fscrypt_name *src) +{ + memset(dst, 0, sizeof(*dst)); + + dst->usr_fname = src->usr_fname; + dst->disk_name = src->disk_name; + dst->hinfo.hash = src->hash; + dst->hinfo.minor_hash = src->minor_hash; + dst->crypto_buf = src->crypto_buf; +} + +int ext4_fname_setup_filename(struct inode *dir, const struct qstr *iname, + int lookup, struct ext4_filename *fname) +{ + struct fscrypt_name name; + int err; + + err = fscrypt_setup_filename(dir, iname, lookup, &name); + if (err) + return err; + + ext4_fname_from_fscrypt_name(fname, &name); + +#if IS_ENABLED(CONFIG_UNICODE) + err = ext4_fname_setup_ci_filename(dir, iname, fname); +#endif + return err; +} + +int ext4_fname_prepare_lookup(struct inode *dir, struct dentry *dentry, + struct ext4_filename *fname) +{ + struct fscrypt_name name; + int err; + + err = fscrypt_prepare_lookup(dir, dentry, &name); + if (err) + return err; + + ext4_fname_from_fscrypt_name(fname, &name); + +#if IS_ENABLED(CONFIG_UNICODE) + err = ext4_fname_setup_ci_filename(dir, &dentry->d_name, fname); +#endif + return err; +} + +void ext4_fname_free_filename(struct ext4_filename *fname) +{ + struct fscrypt_name name; + + name.crypto_buf = fname->crypto_buf; + fscrypt_free_filename(&name); + + fname->crypto_buf.name = NULL; + fname->usr_fname = NULL; + fname->disk_name.name = NULL; + +#if IS_ENABLED(CONFIG_UNICODE) + kfree(fname->cf_name.name); + fname->cf_name.name = NULL; +#endif +} + static int ext4_get_context(struct inode *inode, void *ctx, size_t len) { return ext4_xattr_get(inode, EXT4_XATTR_INDEX_ENCRYPTION, diff --git a/fs/ext4/ext4.h b/fs/ext4/ext4.h index 9100f0ba4a52..11bb0d2ee2eb 100644 --- a/fs/ext4/ext4.h +++ b/fs/ext4/ext4.h @@ -2735,73 +2735,15 @@ extern int ext4_fname_setup_ci_filename(struct inode *dir, extern const struct fscrypt_operations ext4_cryptops; #ifdef CONFIG_FS_ENCRYPTION -static inline void ext4_fname_from_fscrypt_name(struct ext4_filename *dst, - const struct fscrypt_name *src) -{ - memset(dst, 0, sizeof(*dst)); - - dst->usr_fname = src->usr_fname; - dst->disk_name = src->disk_name; - dst->hinfo.hash = src->hash; - dst->hinfo.minor_hash = src->minor_hash; - dst->crypto_buf = src->crypto_buf; -} - -static inline int ext4_fname_setup_filename(struct inode *dir, - const struct qstr *iname, - int lookup, - struct ext4_filename *fname) -{ - struct fscrypt_name name; - int err; +int ext4_fname_setup_filename(struct inode *dir, + const struct qstr *iname, int lookup, + struct ext4_filename *fname); - err = fscrypt_setup_filename(dir, iname, lookup, &name); - if (err) - return err; +int ext4_fname_prepare_lookup(struct inode *dir, struct dentry *dentry, + struct ext4_filename *fname); - ext4_fname_from_fscrypt_name(fname, &name); +void ext4_fname_free_filename(struct ext4_filename *fname); -#if IS_ENABLED(CONFIG_UNICODE) - err = ext4_fname_setup_ci_filename(dir, iname, fname); -#endif - return err; -} - -static inline int ext4_fname_prepare_lookup(struct inode *dir, - struct dentry *dentry, - struct ext4_filename *fname) -{ - struct fscrypt_name name; - int err; - - err = fscrypt_prepare_lookup(dir, dentry, &name); - if (err) - return err; - - ext4_fname_from_fscrypt_name(fname, &name); - -#if IS_ENABLED(CONFIG_UNICODE) - err = ext4_fname_setup_ci_filename(dir, &dentry->d_name, fname); -#endif - return err; -} - -static inline void ext4_fname_free_filename(struct ext4_filename *fname) -{ - struct fscrypt_name name; - - name.crypto_buf = fname->crypto_buf; - fscrypt_free_filename(&name); - - fname->crypto_buf.name = NULL; - fname->usr_fname = NULL; - fname->disk_name.name = NULL; - -#if IS_ENABLED(CONFIG_UNICODE) - kfree(fname->cf_name.name); - fname->cf_name.name = NULL; -#endif -} #else /* !CONFIG_FS_ENCRYPTION */ static inline int ext4_fname_setup_filename(struct inode *dir, const struct qstr *iname, From patchwork Sat May 14 17:22:48 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: "Ritesh Harjani (IBM)" X-Patchwork-Id: 12849902 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 BAF7EC433EF for ; Sat, 14 May 2022 17:23:16 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S234604AbiENRXQ (ORCPT ); Sat, 14 May 2022 13:23:16 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:34922 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S234614AbiENRXO (ORCPT ); Sat, 14 May 2022 13:23:14 -0400 Received: from mail-pj1-x102b.google.com (mail-pj1-x102b.google.com [IPv6:2607:f8b0:4864:20::102b]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 747A0101EA; Sat, 14 May 2022 10:23:13 -0700 (PDT) Received: by mail-pj1-x102b.google.com with SMTP id l20-20020a17090a409400b001dd2a9d555bso10580888pjg.0; Sat, 14 May 2022 10:23:13 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20210112; h=from:to:cc:subject:date:message-id:in-reply-to:references :mime-version:content-transfer-encoding; bh=s44YCbNakB8bQK/snGTGfzRHdvt2dIcw1qE5dHnQ1es=; b=KCP5QTPq/b8iEYZKB6C1/wrxlm5IJ1DMcze64xkVI90k0d+dT/WcW3LZijibTNpk8s BHlUBjJaq7sSZuxmDpVdapvAtTxUE38/V8LBeV1iKhd/BSvUmfgOIm1WIYqyMV0vFo3P wvoZjbiStAe71lDZKZC+gTCTg6mGRhwc9UNnpsGmJ+hUARVfiz/KEamtjtuHQ2djfpH0 4RLxLHRs0l/xCElZVneFJmULtLmVrQCcmYn2qszHB9Sg/GJrE0v1r4O3cwyMWfdhH69k WAau5X2YiTCRtPPOWdQUPIOi49KB90t+R/qgz3J42B1fkfmFD2GCRyZ4PN0UEJDJaxXe 0ruQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=x-gm-message-state:from:to:cc:subject:date:message-id:in-reply-to :references:mime-version:content-transfer-encoding; bh=s44YCbNakB8bQK/snGTGfzRHdvt2dIcw1qE5dHnQ1es=; b=Qm6StK9euTt8TAFhDaRpGKrAG+1MnA7tumM5dGMSdZfccP44oQFSkxlxsaZZNa09sQ sXMIy/s7HeJJsG8XgdktFbpl/8Onr69GPTXYGSSMf0RnJ+pCLgs46a944kxqhoVKgPK7 7NvFXYtYP4njESKi1n7rSN1KOgXWp82EdaevUyXgqA5Lc72pjurBZelXfO8GZ78k5VJy eRP9MY1THHB8mUf9GQ5aWvdXdMar3ExAjIPQ1h8L0OVDhNFc1kuljFtIo7K9P0jA7eyM 92HYD+H7I4+N55voJ2S8hVBBAhinQYlMgkd/BlFlBkHldq8GAfmrEpwUinUg2jSXK6KJ xOFg== X-Gm-Message-State: AOAM532R1cbScoU+/ZGKzQhRGemq+ChDobnFZ/vdQWNHHDJb97qHuiNj LyZk8J/SEMV5+KRAWpH695Ly2dVcGyA= X-Google-Smtp-Source: ABdhPJxMOKEbcOBQuh6llD1+SNoXhu+eOYCaQMVJM/DcAF8cTWqxp3RsCfwq4wv1NbNpFzmWWIGbyA== X-Received: by 2002:a17:902:ba85:b0:161:53e9:c798 with SMTP id k5-20020a170902ba8500b0016153e9c798mr2925945pls.81.1652548992944; Sat, 14 May 2022 10:23:12 -0700 (PDT) Received: from localhost ([2406:7400:63:532d:c4bb:97f7:b03d:2c53]) by smtp.gmail.com with ESMTPSA id d134-20020a621d8c000000b0050dc762819dsm3801872pfd.119.2022.05.14.10.23.12 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Sat, 14 May 2022 10:23:12 -0700 (PDT) From: Ritesh Harjani To: linux-ext4@vger.kernel.org Cc: linux-fscrypt@vger.kernel.org, Theodore Ts'o , Eric Biggers , Jan Kara , Ritesh Harjani Subject: [PATCHv2 3/3] ext4: Refactor and move ext4_ioc_get_encryption_pwsalt() Date: Sat, 14 May 2022 22:52:48 +0530 Message-Id: <3256b969d6e858414f08e0ca2f5117e76fdc2057.1652539361.git.ritesh.list@gmail.com> X-Mailer: git-send-email 2.31.1 In-Reply-To: References: MIME-Version: 1.0 Precedence: bulk List-ID: X-Mailing-List: linux-fscrypt@vger.kernel.org This patch move code for FS_IOC_GET_ENCRYPTION_PWSALT case into ext4's crypto.c file, i.e. ext4_ioc_get_encryption_pwsalt() and uuid_is_zero(). This is mostly refactoring logic and should not affect any functionality change. Suggested-by: Eric Biggers Signed-off-by: Ritesh Harjani Reviewed-by: Eric Biggers --- fs/ext4/crypto.c | 53 +++++++++++++++++++++++++++++++++++++++++++ fs/ext4/ext4.h | 8 +++++++ fs/ext4/ioctl.c | 58 ++---------------------------------------------- 3 files changed, 63 insertions(+), 56 deletions(-) diff --git a/fs/ext4/crypto.c b/fs/ext4/crypto.c index f8333927f0f6..36bcfeecdb00 100644 --- a/fs/ext4/crypto.c +++ b/fs/ext4/crypto.c @@ -71,6 +71,59 @@ void ext4_fname_free_filename(struct ext4_filename *fname) #endif } +static bool uuid_is_zero(__u8 u[16]) +{ + int i; + + for (i = 0; i < 16; i++) + if (u[i]) + return false; + return true; +} + +int ext4_ioc_get_encryption_pwsalt(struct file *filp, void __user *arg) +{ + struct super_block *sb = file_inode(filp)->i_sb; + struct ext4_sb_info *sbi = EXT4_SB(sb); + int err, err2; + handle_t *handle; + + if (!ext4_has_feature_encrypt(sb)) + return -EOPNOTSUPP; + + if (uuid_is_zero(sbi->s_es->s_encrypt_pw_salt)) { + err = mnt_want_write_file(filp); + if (err) + return err; + handle = ext4_journal_start_sb(sb, EXT4_HT_MISC, 1); + if (IS_ERR(handle)) { + err = PTR_ERR(handle); + goto pwsalt_err_exit; + } + err = ext4_journal_get_write_access(handle, sb, sbi->s_sbh, + EXT4_JTR_NONE); + if (err) + goto pwsalt_err_journal; + lock_buffer(sbi->s_sbh); + generate_random_uuid(sbi->s_es->s_encrypt_pw_salt); + ext4_superblock_csum_set(sb); + unlock_buffer(sbi->s_sbh); + err = ext4_handle_dirty_metadata(handle, NULL, sbi->s_sbh); +pwsalt_err_journal: + err2 = ext4_journal_stop(handle); + if (err2 && !err) + err = err2; +pwsalt_err_exit: + mnt_drop_write_file(filp); + if (err) + return err; + } + + if (copy_to_user(arg, sbi->s_es->s_encrypt_pw_salt, 16)) + return -EFAULT; + return 0; +} + static int ext4_get_context(struct inode *inode, void *ctx, size_t len) { return ext4_xattr_get(inode, EXT4_XATTR_INDEX_ENCRYPTION, diff --git a/fs/ext4/ext4.h b/fs/ext4/ext4.h index 11bb0d2ee2eb..b61115fe7ffb 100644 --- a/fs/ext4/ext4.h +++ b/fs/ext4/ext4.h @@ -2744,6 +2744,8 @@ int ext4_fname_prepare_lookup(struct inode *dir, struct dentry *dentry, void ext4_fname_free_filename(struct ext4_filename *fname); +int ext4_ioc_get_encryption_pwsalt(struct file *filp, void __user *arg); + #else /* !CONFIG_FS_ENCRYPTION */ static inline int ext4_fname_setup_filename(struct inode *dir, const struct qstr *iname, @@ -2776,6 +2778,12 @@ static inline void ext4_fname_free_filename(struct ext4_filename *fname) fname->cf_name.name = NULL; #endif } + +static inline int ext4_ioc_get_encryption_pwsalt(struct file *filp, + void __user *arg) +{ + return -EOPNOTSUPP; +} #endif /* !CONFIG_FS_ENCRYPTION */ /* dir.c */ diff --git a/fs/ext4/ioctl.c b/fs/ext4/ioctl.c index ba44fa1be70a..044e65d44054 100644 --- a/fs/ext4/ioctl.c +++ b/fs/ext4/ioctl.c @@ -504,18 +504,6 @@ static long swap_inode_boot_loader(struct super_block *sb, return err; } -#ifdef CONFIG_FS_ENCRYPTION -static int uuid_is_zero(__u8 u[16]) -{ - int i; - - for (i = 0; i < 16; i++) - if (u[i]) - return 0; - return 1; -} -#endif - /* * If immutable is set and we are not clearing it, we're not allowed to change * anything else in the inode. Don't error out if we're only trying to set @@ -1432,51 +1420,9 @@ static long __ext4_ioctl(struct file *filp, unsigned int cmd, unsigned long arg) return -EOPNOTSUPP; return fscrypt_ioctl_set_policy(filp, (const void __user *)arg); - case FS_IOC_GET_ENCRYPTION_PWSALT: { -#ifdef CONFIG_FS_ENCRYPTION - int err, err2; - struct ext4_sb_info *sbi = EXT4_SB(sb); - handle_t *handle; + case FS_IOC_GET_ENCRYPTION_PWSALT: + return ext4_ioc_get_encryption_pwsalt(filp, (void __user *)arg); - if (!ext4_has_feature_encrypt(sb)) - return -EOPNOTSUPP; - if (uuid_is_zero(sbi->s_es->s_encrypt_pw_salt)) { - err = mnt_want_write_file(filp); - if (err) - return err; - handle = ext4_journal_start_sb(sb, EXT4_HT_MISC, 1); - if (IS_ERR(handle)) { - err = PTR_ERR(handle); - goto pwsalt_err_exit; - } - err = ext4_journal_get_write_access(handle, sb, - sbi->s_sbh, - EXT4_JTR_NONE); - if (err) - goto pwsalt_err_journal; - lock_buffer(sbi->s_sbh); - generate_random_uuid(sbi->s_es->s_encrypt_pw_salt); - ext4_superblock_csum_set(sb); - unlock_buffer(sbi->s_sbh); - err = ext4_handle_dirty_metadata(handle, NULL, - sbi->s_sbh); - pwsalt_err_journal: - err2 = ext4_journal_stop(handle); - if (err2 && !err) - err = err2; - pwsalt_err_exit: - mnt_drop_write_file(filp); - if (err) - return err; - } - if (copy_to_user((void __user *) arg, - sbi->s_es->s_encrypt_pw_salt, 16)) - return -EFAULT; - return 0; -#else - return -EOPNOTSUPP; -#endif - } case FS_IOC_GET_ENCRYPTION_POLICY: if (!ext4_has_feature_encrypt(sb)) return -EOPNOTSUPP;