Received: by 2002:a6b:500f:0:0:0:0:0 with SMTP id e15csp2563547iob; Mon, 16 May 2022 00:32:45 -0700 (PDT) X-Google-Smtp-Source: ABdhPJx4Lxzj21W7saBACfBFODf8wT5IN3BqRwrZYwjl5Sp9dLQ/38Sjr01uhXyTKI1p07hfx2Ip X-Received: by 2002:a17:907:2ce3:b0:6f5:130e:d98f with SMTP id hz3-20020a1709072ce300b006f5130ed98fmr13672943ejc.706.1652686364804; Mon, 16 May 2022 00:32:44 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1652686364; cv=none; d=google.com; s=arc-20160816; b=Qcfnr3hqmNt/Wn3PM4oGlPGaSH7nJBxTr2mtj+2UCGufF6YfGrjTR5po7i7Vdo4Pv/ tWwZHo7/UJG0TXJbHK53gUduQ/QG2w398IOUe0t6rwrZ6pUbSUonhy3oji6o5pcsBFs8 R7mMIePO1ly32QDOvp4kl7j/22Gmfr1mRP/2wLdZWB6XaTZTEP2KDRohHGLr0WmVxdPl WJRMN5mDwJn3M7v99RnqFI0GIsoaLlv/35engi8vfp2hNGNGgzyax9U0R2VJ2mRhdeCr EufIvIOkAZQBoHoFACUCEzGg7uTbuu3N/fCRNBYziBeigJtQaEqvqRj1TEwMTy+DNdhw ZyYg== 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 :references:in-reply-to:message-id:date:subject:cc:to:from :dkim-signature; bh=s44YCbNakB8bQK/snGTGfzRHdvt2dIcw1qE5dHnQ1es=; b=uREJQR3BYU8OuN5V82HzWguEth3BqGeOcNQg9CdoK07pzfwVpBak1QM3WMVlCP7ZBO OuzOhE0WhS7XpwjuzuG/tjjCzROfEMNHgdLh5L26u95u0eHDj9mcsCcrg8td8GKvy3Ep Evogn3OyeBMUyTOJ1DHDR3b3nCddgScqyaUFxCgtMdGsmTAxh8Vn/+CKKZtID1P/hynV NM/cH1ng7JABzpO1quDTAqXNqGo2394YlZCcJtx0axo6y6dZuGJqrLtxNUaCWgdIC9XB t8tkn6LjNLRPUziIuuH00BVPeIxgRTjqSE2vORGctP8IV6XnHXcFvHGzX0jYfebQmi3v MHIA== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@gmail.com header.s=20210112 header.b=KCP5QTPq; 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=QUARANTINE dis=NONE) header.from=gmail.com Return-Path: Received: from out1.vger.email (out1.vger.email. [2620:137:e000::1:20]) by mx.google.com with ESMTP id dt21-20020a170907729500b006f37bde5a2csi10945966ejc.693.2022.05.16.00.32.11; Mon, 16 May 2022 00:32:44 -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=@gmail.com header.s=20210112 header.b=KCP5QTPq; 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=QUARANTINE dis=NONE) header.from=gmail.com Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S234587AbiENRXQ (ORCPT + 99 others); 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-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 Content-Transfer-Encoding: 8bit X-Spam-Status: No, score=-2.1 required=5.0 tests=BAYES_00,DKIM_SIGNED, DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,FREEMAIL_FROM, RCVD_IN_DNSWL_NONE,SPF_HELO_NONE,SPF_PASS,T_SCC_BODY_TEXT_LINE 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 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 --- 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; -- 2.31.1