Linux kernel mirror (for testing) git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git
kernel os linux

ext4: refactor and move ext4_ioctl_get_encryption_pwsalt()

This patch move code for FS_IOC_GET_ENCRYPTION_PWSALT case into
ext4's crypto.c file, i.e. ext4_ioctl_get_encryption_pwsalt()
and uuid_is_zero(). This is mostly refactoring logic and should
not affect any functionality change.

Suggested-by: Eric Biggers <ebiggers@google.com>
Reviewed-by: Eric Biggers <ebiggers@google.com>
Signed-off-by: Ritesh Harjani <ritesh.list@gmail.com>
Link: https://lore.kernel.org/r/5af98b17152a96b245b4f7d2dfb8607fc93e36aa.1652595565.git.ritesh.list@gmail.com
Signed-off-by: Theodore Ts'o <tytso@mit.edu>

authored by

Ritesh Harjani and committed by
Theodore Ts'o
72f63f4a 3030b59c

+64 -57
+54
fs/ext4/crypto.c
··· 1 1 // SPDX-License-Identifier: GPL-2.0 2 2 3 3 #include <linux/quotaops.h> 4 + #include <linux/uuid.h> 4 5 5 6 #include "ext4.h" 6 7 #include "xattr.h" ··· 70 69 kfree(fname->cf_name.name); 71 70 fname->cf_name.name = NULL; 72 71 #endif 72 + } 73 + 74 + static bool uuid_is_zero(__u8 u[16]) 75 + { 76 + int i; 77 + 78 + for (i = 0; i < 16; i++) 79 + if (u[i]) 80 + return false; 81 + return true; 82 + } 83 + 84 + int ext4_ioctl_get_encryption_pwsalt(struct file *filp, void __user *arg) 85 + { 86 + struct super_block *sb = file_inode(filp)->i_sb; 87 + struct ext4_sb_info *sbi = EXT4_SB(sb); 88 + int err, err2; 89 + handle_t *handle; 90 + 91 + if (!ext4_has_feature_encrypt(sb)) 92 + return -EOPNOTSUPP; 93 + 94 + if (uuid_is_zero(sbi->s_es->s_encrypt_pw_salt)) { 95 + err = mnt_want_write_file(filp); 96 + if (err) 97 + return err; 98 + handle = ext4_journal_start_sb(sb, EXT4_HT_MISC, 1); 99 + if (IS_ERR(handle)) { 100 + err = PTR_ERR(handle); 101 + goto pwsalt_err_exit; 102 + } 103 + err = ext4_journal_get_write_access(handle, sb, sbi->s_sbh, 104 + EXT4_JTR_NONE); 105 + if (err) 106 + goto pwsalt_err_journal; 107 + lock_buffer(sbi->s_sbh); 108 + generate_random_uuid(sbi->s_es->s_encrypt_pw_salt); 109 + ext4_superblock_csum_set(sb); 110 + unlock_buffer(sbi->s_sbh); 111 + err = ext4_handle_dirty_metadata(handle, NULL, sbi->s_sbh); 112 + pwsalt_err_journal: 113 + err2 = ext4_journal_stop(handle); 114 + if (err2 && !err) 115 + err = err2; 116 + pwsalt_err_exit: 117 + mnt_drop_write_file(filp); 118 + if (err) 119 + return err; 120 + } 121 + 122 + if (copy_to_user(arg, sbi->s_es->s_encrypt_pw_salt, 16)) 123 + return -EFAULT; 124 + return 0; 73 125 } 74 126 75 127 static int ext4_get_context(struct inode *inode, void *ctx, size_t len)
+8
fs/ext4/ext4.h
··· 2745 2745 2746 2746 void ext4_fname_free_filename(struct ext4_filename *fname); 2747 2747 2748 + int ext4_ioctl_get_encryption_pwsalt(struct file *filp, void __user *arg); 2749 + 2748 2750 #else /* !CONFIG_FS_ENCRYPTION */ 2749 2751 static inline int ext4_fname_setup_filename(struct inode *dir, 2750 2752 const struct qstr *iname, ··· 2778 2776 kfree(fname->cf_name.name); 2779 2777 fname->cf_name.name = NULL; 2780 2778 #endif 2779 + } 2780 + 2781 + static inline int ext4_ioctl_get_encryption_pwsalt(struct file *filp, 2782 + void __user *arg) 2783 + { 2784 + return -EOPNOTSUPP; 2781 2785 } 2782 2786 #endif /* !CONFIG_FS_ENCRYPTION */ 2783 2787
+2 -57
fs/ext4/ioctl.c
··· 16 16 #include <linux/file.h> 17 17 #include <linux/quotaops.h> 18 18 #include <linux/random.h> 19 - #include <linux/uuid.h> 20 19 #include <linux/uaccess.h> 21 20 #include <linux/delay.h> 22 21 #include <linux/iversion.h> ··· 502 503 iput(inode_bl); 503 504 return err; 504 505 } 505 - 506 - #ifdef CONFIG_FS_ENCRYPTION 507 - static int uuid_is_zero(__u8 u[16]) 508 - { 509 - int i; 510 - 511 - for (i = 0; i < 16; i++) 512 - if (u[i]) 513 - return 0; 514 - return 1; 515 - } 516 - #endif 517 506 518 507 /* 519 508 * If immutable is set and we are not clearing it, we're not allowed to change ··· 1419 1432 return -EOPNOTSUPP; 1420 1433 return fscrypt_ioctl_set_policy(filp, (const void __user *)arg); 1421 1434 1422 - case FS_IOC_GET_ENCRYPTION_PWSALT: { 1423 - #ifdef CONFIG_FS_ENCRYPTION 1424 - int err, err2; 1425 - struct ext4_sb_info *sbi = EXT4_SB(sb); 1426 - handle_t *handle; 1435 + case FS_IOC_GET_ENCRYPTION_PWSALT: 1436 + return ext4_ioctl_get_encryption_pwsalt(filp, (void __user *)arg); 1427 1437 1428 - if (!ext4_has_feature_encrypt(sb)) 1429 - return -EOPNOTSUPP; 1430 - if (uuid_is_zero(sbi->s_es->s_encrypt_pw_salt)) { 1431 - err = mnt_want_write_file(filp); 1432 - if (err) 1433 - return err; 1434 - handle = ext4_journal_start_sb(sb, EXT4_HT_MISC, 1); 1435 - if (IS_ERR(handle)) { 1436 - err = PTR_ERR(handle); 1437 - goto pwsalt_err_exit; 1438 - } 1439 - err = ext4_journal_get_write_access(handle, sb, 1440 - sbi->s_sbh, 1441 - EXT4_JTR_NONE); 1442 - if (err) 1443 - goto pwsalt_err_journal; 1444 - lock_buffer(sbi->s_sbh); 1445 - generate_random_uuid(sbi->s_es->s_encrypt_pw_salt); 1446 - ext4_superblock_csum_set(sb); 1447 - unlock_buffer(sbi->s_sbh); 1448 - err = ext4_handle_dirty_metadata(handle, NULL, 1449 - sbi->s_sbh); 1450 - pwsalt_err_journal: 1451 - err2 = ext4_journal_stop(handle); 1452 - if (err2 && !err) 1453 - err = err2; 1454 - pwsalt_err_exit: 1455 - mnt_drop_write_file(filp); 1456 - if (err) 1457 - return err; 1458 - } 1459 - if (copy_to_user((void __user *) arg, 1460 - sbi->s_es->s_encrypt_pw_salt, 16)) 1461 - return -EFAULT; 1462 - return 0; 1463 - #else 1464 - return -EOPNOTSUPP; 1465 - #endif 1466 - } 1467 1438 case FS_IOC_GET_ENCRYPTION_POLICY: 1468 1439 if (!ext4_has_feature_encrypt(sb)) 1469 1440 return -EOPNOTSUPP;