Support fof Casefolding and Encryption
Subsystems (8)
v6 cover Support fof Casefolding and Encryption
These patches are all on top of fscrypt's development branch
Ext4 and F2FS currently both support casefolding and encryption, but not at the same time. These patches aim to rectify that.
I moved the identical casefolding dcache operations for ext4 and f2fs into fs/unicode, as all filesystems using casefolded names will want them.
I've also adjust fscrypt to not set it's d_revalidate operation during it's prepare lookup, instead having the calling filesystem set it up. This is done to that the filesystem may have it's own dentry_operations.
For Ext4, since the hash for encrypted casefolded directory names cannot be computed without the key, we need to store the hash on disk. We only do so for encrypted and casefolded directories to avoid on disk format changes. Previously encryption and casefolding could not be on the same filesystem, and we're relaxing that requirement. F2fs is a bit more straightforward since it already stores hashes on disk.
I've updated the related tools with just enough to enable the feature. I still need to adjust ext4's fsck's, although without access to the keys, neither fsck will be able to verify the hashes of casefolded and encrypted names.
v6 changes:
Went back to using dentry_operations for casefolding. Provided standard implementations in fs/unicode, avoiding extra allocation in d_hash op. Moved fscrypt d_ops setting to be filesystem's responsibility to maintain compatibility with casefolding and overlayfs if casefolding is not used fixes some f2fs error handling
v4-5: patches submitted on fscrypt
v3 changes:
fscrypt patch only creates hash key if it will be needed. Rebased on top of fscrypt branch, reconstified match functions in ext4/f2fs
v2 changes:
fscrypt moved to separate thread to rebase on fscrypt dev branch addressed feedback, plus some minor fixes
Daniel Rosenberg (5):
unicode: Add standard casefolded d_ops
fscrypt: Have filesystems handle their d_ops
f2fs: Handle casefolding with Encryption
ext4: Hande casefolding with encryption
ext4: Optimize match for casefolded encrypted dirs
Documentation/filesystems/ext4/directory.rst | 27 ++ fs/crypto/fname.c | 7 +- fs/crypto/fscrypt_private.h | 1 - fs/crypto/hooks.c | 1 - fs/ext4/dir.c | 102 +++--- fs/ext4/ext4.h | 86 +++-- fs/ext4/hash.c | 26 +- fs/ext4/ialloc.c | 5 +- fs/ext4/inline.c | 41 ++- fs/ext4/namei.c | 325 ++++++++++++------- fs/ext4/super.c | 21 +- fs/f2fs/dir.c | 151 +++++---- fs/f2fs/f2fs.h | 16 +- fs/f2fs/hash.c | 25 +- fs/f2fs/inline.c | 9 +- fs/f2fs/namei.c | 1 + fs/f2fs/super.c | 17 +- fs/f2fs/sysfs.c | 8 +- fs/ubifs/dir.c | 18 + fs/unicode/utf8-core.c | 61 ++++ include/linux/fs.h | 10 + include/linux/fscrypt.h | 6 +- include/linux/unicode.h | 17 + 23 files changed, 644 insertions(+), 337 deletions(-)
Signature · 1 line
2.25.0.341.g760bfbb309-goog
PATCH v6 1/5 unicode: Add standard casefolded d_ops
This adds dentry_operations implementations for d_hash and d_compare which can be used by any filesystem using utf8 casefolding, unifying the existing ext4 and f2fs functions.
In order to do this, we add new variables to struct super_block and switch ext4 and f2fs over to those instead of maintaining their own versions.
--- fs/ext4/dir.c | 45 ++---------------------------- fs/ext4/ext4.h | 4 --- fs/ext4/hash.c | 2 +- fs/ext4/namei.c | 20 ++++++-------- fs/ext4/super.c | 10 +++---- fs/f2fs/dir.c | 60 +++++++--------------------------------- fs/f2fs/f2fs.h | 4 --- fs/f2fs/hash.c | 2 +- fs/f2fs/super.c | 10 +++---- fs/f2fs/sysfs.c | 8 +++--- fs/unicode/utf8-core.c | 61 +++++++++++++++++++++++++++++++++++++++++ include/linux/fs.h | 10 +++++++ include/linux/unicode.h | 17 ++++++++++++ 13 files changed, 124 insertions(+), 129 deletions(-)
fs/ext4/dir.c +2 -43
@@ -668,49 +668,8 @@ const struct file_operations ext4_dir_operations = { };#ifdef CONFIG_UNICODE -static int ext4_d_compare (const struct dentry *dentry,unsigned int len, -const char *str,const struct qstr *name) -{ -struct qstr qstr = {.name = str, .len = len }; -struct inode *inode = dentry->d_parent->d_inode; - -if (!IS_CASEFOLDED (inode) || !EXT4_SB (inode->i_sb)->s_encoding) { -if (len != name->len) -return -1 ; -return memcmp (str, name->name, len); - } - -return ext4_ci_compare (inode, name, &qstr, false); -} - -static int ext4_d_hash (const struct dentry *dentry,struct qstr *str) -{ -const struct ext4_sb_info *sbi =EXT4_SB (dentry->d_sb); -const struct unicode_map *um = sbi->s_encoding; -unsigned char *norm; -int len, ret =0 ; - -if (!IS_CASEFOLDED (dentry->d_inode) || !um) -return 0 ; - - norm =kmalloc (PATH_MAX ,GFP_ATOMIC ); -if (!norm) -return -ENOMEM ; - - len =utf8_casefold (um, str, norm,PATH_MAX ); -if (len <0 ) { -if (ext4_has_strict_mode (sbi)) - ret = -EINVAL ; - goto out; - } - str->hash =full_name_hash (dentry, norm, len); -out: -kfree (norm); -return ret; -} -const struct dentry_operations ext4_dentry_ops = { - .d_hash = ext4_d_hash, - .d_compare = ext4_d_compare, + .d_hash = utf8_ci_d_hash, + .d_compare = utf8_ci_d_compare, };#endif
fs/ext4/ext4.h +0 -4
@@ -1430,10 +1430,6 @@ struct ext4_sb_info {struct kobject s_kobj;struct completion s_kobj_unregister;struct super_block *s_sb; -#ifdef CONFIG_UNICODE -struct unicode_map *s_encoding; -__u16 s_encoding_flags; -#endif /* Journaling */ struct journal_s *s_journal;
fs/ext4/hash.c +1 -1
@@ -275,7 +275,7 @@ int ext4fs_dirhash(const struct inode *dir, const char *name, int len, struct dx_hash_info *hinfo) {#ifdef CONFIG_UNICODE -const struct unicode_map *um =EXT4_SB (dir->i_sb)->s_encoding; +const struct unicode_map *um = dir->i_sb->s_encoding;int r, dlen;unsigned char *buff;struct qstr qstr = {.name = name, .len = len };
fs/ext4/namei.c +8 -12
@@ -1282,8 +1282,8 @@ static void dx_insert_block(struct dx_frame *frame, u32 hash, ext4_lblk_t block)int ext4_ci_compare (const struct inode *parent,const struct qstr *name,const struct qstr *entry,bool quick) { -const struct ext4_sb_info *sbi =EXT4_SB (parent->i_sb); -const struct unicode_map *um = sbi->s_encoding; +const struct super_block *sb = parent->i_sb; +const struct unicode_map *um = sb->s_encoding;int ret;if (quick)
@@ -1295,7 +1295,7 @@ int ext4_ci_compare(const struct inode *parent, const struct qstr *name,/* Handle invalid character sequence as either an error * or as an opaque byte sequence. */ -if (ext4_has_strict_mode (sbi)) +if (sb_has_enc_strict_mode (sb))return -EINVAL ;if (name->len != entry->len)
@@ -1312,7 +1312,7 @@ void ext4_fname_setup_ci_filename(struct inode *dir, const struct qstr *iname, {int len; -if (!IS_CASEFOLDED (dir) || !EXT4_SB (dir->i_sb)->s_encoding) { +if (!needs_casefold (dir)) { cf_name->name =NULL ;return ; }
@@ -1321,7 +1321,7 @@ void ext4_fname_setup_ci_filename(struct inode *dir, const struct qstr *iname,if (!cf_name->name)return ; - len =utf8_casefold (EXT4_SB (dir->i_sb)->s_encoding, + len =utf8_casefold (dir->i_sb->s_encoding, iname, cf_name->name,EXT4_NAME_LEN );if (len <=0 ) {
@@ -1358,7 +1358,7 @@ static inline bool ext4_match(const struct inode *parent,#endif #ifdef CONFIG_UNICODE -if (EXT4_SB (parent->i_sb)->s_encoding &&IS_CASEFOLDED (parent)) { +if (needs_casefold (parent)) {if (fname->cf_name.name) {struct qstr cf = {.name = fname->cf_name.name, .len = fname->cf_name.len};
@@ -2164,9 +2164,6 @@ static int ext4_add_entry(handle_t *handle, struct dentry *dentry,struct buffer_head *bh =NULL ;struct ext4_dir_entry_2 *de;struct super_block *sb; -#ifdef CONFIG_UNICODE -struct ext4_sb_info *sbi; -#endif struct ext4_filename fname;int retval;int dx_fallback=0 ;
@@ -2183,9 +2180,8 @@ static int ext4_add_entry(handle_t *handle, struct dentry *dentry,return -EINVAL ;#ifdef CONFIG_UNICODE - sbi =EXT4_SB (sb); -if (ext4_has_strict_mode (sbi) &&IS_CASEFOLDED (dir) && - sbi->s_encoding &&utf8_validate (sbi->s_encoding, &dentry->d_name)) +if (sb_has_enc_strict_mode (sb) &&IS_CASEFOLDED (dir) && + sb->s_encoding &&utf8_validate (sb->s_encoding, &dentry->d_name))return -EINVAL ;#endif
fs/ext4/super.c +5 -5
@@ -1056,7 +1056,7 @@ static void ext4_put_super(struct super_block *sb)kfree (sbi->s_blockgroup_lock);fs_put_dax (sbi->s_daxdev);#ifdef CONFIG_UNICODE -utf8_unload (sbi->s_encoding); +utf8_unload (sb->s_encoding);#endif kfree (sbi); }
@@ -3850,7 +3850,7 @@ static int ext4_fill_super(struct super_block *sb, void *data, int silent) goto failed_mount;#ifdef CONFIG_UNICODE -if (ext4_has_feature_casefold (sb) && !sbi->s_encoding) { +if (ext4_has_feature_casefold (sb) && !sb->s_encoding) {const struct ext4_sb_encodings *encoding_info;struct unicode_map *encoding;__u16 encoding_flags;
@@ -3881,8 +3881,8 @@ static int ext4_fill_super(struct super_block *sb, void *data, int silent)"%s-%s with flags 0x%hx" , encoding_info->name, encoding_info->version?:"\b", encoding_flags); - sbi->s_encoding = encoding; - sbi->s_encoding_flags = encoding_flags; + sb->s_encoding = encoding; + sb->s_encoding_flags = encoding_flags; }#endif
@@ -4684,7 +4684,7 @@ static int ext4_fill_super(struct super_block *sb, void *data, int silent)crypto_free_shash (sbi->s_chksum_driver);#ifdef CONFIG_UNICODE -utf8_unload (sbi->s_encoding); +utf8_unload (sb->s_encoding);#endif #ifdef CONFIG_QUOTA
fs/f2fs/dir.c +10 -50
102 lines not on this page · raw message
fs/f2fs/f2fs.h +0 -4
@@ -1184,10 +1184,6 @@ struct f2fs_sb_info {int valid_super_block;/* valid super block no */ unsigned long s_flag;/* flags for sbi */ struct mutex writepages;/* mutex for writepages() */ -#ifdef CONFIG_UNICODE -struct unicode_map *s_encoding; -__u16 s_encoding_flags; -#endif #ifdef CONFIG_BLK_DEV_ZONED unsigned int blocks_per_blkz;/* F2FS blocks per zone */
fs/f2fs/hash.c +1 -1
@@ -110,7 +110,7 @@ f2fs_hash_t f2fs_dentry_hash(const struct inode *dir, {#ifdef CONFIG_UNICODE struct f2fs_sb_info *sbi =F2FS_SB (dir->i_sb); -const struct unicode_map *um = sbi->s_encoding; +const struct unicode_map *um = dir->i_sb->s_encoding;int r, dlen;unsigned char *buff;struct qstr folded;
fs/f2fs/super.c +5 -5
@@ -1144,7 +1144,7 @@ static void f2fs_put_super(struct super_block *sb)for (i =0 ; i <NR_PAGE_TYPE ; i++)kvfree (sbi->write_io[i]);#ifdef CONFIG_UNICODE -utf8_unload (sbi->s_encoding); +utf8_unload (sb->s_encoding);#endif kvfree (sbi); }
@@ -3136,7 +3136,7 @@ static int f2fs_scan_devices(struct f2fs_sb_info *sbi)static int f2fs_setup_casefold (struct f2fs_sb_info *sbi) {#ifdef CONFIG_UNICODE -if (f2fs_sb_has_casefold (sbi) && !sbi->s_encoding) { +if (f2fs_sb_has_casefold (sbi) && !sbi->sb->s_encoding) {const struct f2fs_sb_encodings *encoding_info;struct unicode_map *encoding;__u16 encoding_flags;
@@ -3167,8 +3167,8 @@ static int f2fs_setup_casefold(struct f2fs_sb_info *sbi)"%s-%s with flags 0x%hx" , encoding_info->name, encoding_info->version?:"\b", encoding_flags); - sbi->s_encoding = encoding; - sbi->s_encoding_flags = encoding_flags; + sbi->sb->s_encoding = encoding; + sbi->sb->s_encoding_flags = encoding_flags; sbi->sb->s_d_op = &f2fs_dentry_ops; }#else
@@ -3637,7 +3637,7 @@ static int f2fs_fill_super(struct super_block *sb, void *data, int silent)kvfree (sbi->write_io[i]);#ifdef CONFIG_UNICODE -utf8_unload (sbi->s_encoding); +utf8_unload (sb->s_encoding);#endif free_options: #ifdef CONFIG_QUOTA
fs/f2fs/sysfs.c +4 -4
@@ -88,10 +88,10 @@ static ssize_t encoding_show(struct f2fs_attr *a,#ifdef CONFIG_UNICODE if (f2fs_sb_has_casefold (sbi))return snprintf (buf,PAGE_SIZE ,"%s (%d.%d.%d)\n" , - sbi->s_encoding->charset, - (sbi->s_encoding->version >>16 ) &0xff , - (sbi->s_encoding->version >>8 ) &0xff , - sbi->s_encoding->version &0xff ); + sbi->sb->s_encoding->charset, + (sbi->sb->s_encoding->version >>16 ) &0xff , + (sbi->sb->s_encoding->version >>8 ) &0xff , + sbi->sb->s_encoding->version &0xff );#endif return snprintf (buf,PAGE_SIZE ,"(none)" ); }
fs/unicode/utf8-core.c +61 -0
71 lines not on this page · raw message
include/linux/fs.h +10 -0
@@ -1382,6 +1382,12 @@ extern int send_sigurg(struct fown_struct *fown);#define SB_ACTIVE (1<<30)#define SB_NOUSER (1<<31) +/* These flags relate to encoding and casefolding */ +#define SB_ENC_STRICT_MODE_FL (1 << 0) + +#define sb_has_enc_strict_mode (sb) \ + (sb->s_encoding_flags & SB_ENC_STRICT_MODE_FL) +/* * Umount options */
@@ -1449,6 +1455,10 @@ struct super_block {#endif #ifdef CONFIG_FS_VERITY const struct fsverity_operations *s_vop; +#endif +#ifdef CONFIG_UNICODE +struct unicode_map *s_encoding; +__u16 s_encoding_flags;#endif struct hlist_bl_head s_roots;/* alternate root dentries for NFS */ struct list_head s_mounts;/* list of mounts; _not_ for fs use */
include/linux/unicode.h +17 -0
@@ -4,6 +4,8 @@#include <linux/init.h> #include <linux/dcache.h> +#include <linux/fscrypt.h> +#include <linux/fs.h> struct unicode_map {const char *charset;
@@ -30,4 +32,19 @@ int utf8_casefold(const struct unicode_map *um, const struct qstr *str,struct unicode_map *utf8_load (const char *version);void utf8_unload (struct unicode_map *um); +int utf8_ci_d_hash (const struct dentry *dentry,struct qstr *str); +int utf8_ci_d_compare (const struct dentry *dentry,unsigned int len, +const char *str,const struct qstr *name); +#ifdef CONFIG_UNICODE +static inline bool needs_casefold (const struct inode *dir) +{ +return IS_CASEFOLDED (dir) && dir->i_sb->s_encoding && + (!IS_ENCRYPTED (dir) ||fscrypt_has_encryption_key (dir)); +} +#else +static inline bool needs_casefold (const struct inode *dir) +{ +return 0 ; +} +#endif #endif /* _LINUX_UNICODE_H */
Signature · 1 line
2.25.0.341.g760bfbb309-goog
Re: [PATCH v6 1/5] unicode: Add standard casefolded d_ops
Quoted 14 lines · Daniel Rosenberg
include/linux/unicode.h +2
-0@@ -4,6 +4,8 @@#include <linux/init.h> #include <linux/dcache.h> +#include <linux/fscrypt.h> +#include <linux/fs.h> struct unicode_map {const char *charset;
include/linux/unicode.h int utf8_casefold(const struct unicode_map *um, const struct qstr *str, · line 37
Quoted 6 lines
include/linux/unicode.h +3
-0@@ -30,4 +32,19 @@ int utf8_casefold(const struct unicode_map *um, const struct qstr *str,struct unicode_map *utf8_load (const char *version);void utf8_unload (struct unicode_map *um); +int utf8_ci_d_hash (const struct dentry *dentry,struct qstr *str); +int utf8_ci_d_compare (const struct dentry *dentry,unsigned int len, +const char *str,const struct qstr *name);
I don't think fs/unicode is the right place for these very specific filesystem functions, just because they happen to use unicode. It is an encoding library, it doesn't care about dentries, nor should know how to handle them. It exposes a simple api to manipulate and convert utf8 strings.
I saw change was after the desire to not have these functions polluting the VFS hot path, but that has nothing to do with placing them here.
Would libfs be better? or a casefolding library in fs/casefold.c?
Signature · 1 line
Gabriel Krisman Bertazi
Re: [PATCH v6 1/5] unicode: Add standard casefolded d_ops
Quoted 7 lines · Gabriel Krisman Bertazi
I don't think fs/unicode is the right place for these very specific filesystem functions, just because they happen to use unicode. It is an encoding library, it doesn't care about dentries, nor should know how to handle them. It exposes a simple api to manipulate and convert utf8 strings.
Quoted 8 lines
I saw change was after the desire to not have these functions polluting the VFS hot path, but that has nothing to do with placing them here.
Would libfs be better? or a casefolding library in
fs/casefold.c?--
Gabriel Krisman Bertazi
The hash function needs access to utf8ncursor, but apart from that, libfs would make sense. utf8ncursor is the only reason I have them here. How do you feel about exposing utf8cursor or something similar?
Re: [PATCH v6 1/5] unicode: Add standard casefolded d_ops
Quoted 15 lines · Daniel Rosenberg
Quoted 13 lines · Gabriel Krisman Bertazi
I don't think fs/unicode is the right place for these very specific filesystem functions, just because they happen to use unicode. It is an encoding library, it doesn't care about dentries, nor should know how to handle them. It exposes a simple api to manipulate and convert utf8 strings.
I saw change was after the desire to not have these functions polluting the VFS hot path, but that has nothing to do with placing them here.
Would libfs be better? or a casefolding library in
fs/casefold.c?
Quoted 6 lines
Quoted 2 lines
--
Gabriel Krisman BertaziThe hash function needs access to utf8ncursor, but apart from that, libfs would make sense. utf8ncursor is the only reason I have them here. How do you feel about exposing utf8cursor or something similar?
Hi,
It was designed to be an internal thing, but I'm ok with exposing it.
Signature · 1 line
Gabriel Krisman Bertazi
Re: [PATCH v6 1/5] unicode: Add standard casefolded d_ops
Quoted 9 lines · Gabriel Krisman Bertazi
Daniel Rosenberg <[email protected]> writes:
Hi,
It was designed to be an internal thing, but I'm ok with exposing it.
--
Gabriel Krisman Bertazi
We could also avoid exposing it by creating an iterator function that accepts a context struct with some actor function pointer, similar to iterate_dir. I'm currently reworking around that and moving the fs specific functions to libfs. I can move the generic op_set code there as well.
-Daniel Rosenberg
PATCH v6 2/5 fscrypt: Have filesystems handle their d_ops
In reply to Daniel RosenbergThis shifts the responsibility of setting up dentry operations from fscrypt to the individual filesystems, allowing them to have their own operations while still setting fscrypt's d_revalidate as appropriate.
---
Should the d_op setting functions go into fs/libfs.c?
fs/crypto/fname.c | 7 ++----- fs/crypto/fscrypt_private.h | 1 - fs/crypto/hooks.c | 1 - fs/ext4/dir.c | 38 ++++++++++++++++++++++++++++++++++++- fs/ext4/ext4.h | 1 + fs/ext4/namei.c | 1 + fs/ext4/super.c | 5 ----- fs/f2fs/dir.c | 38 ++++++++++++++++++++++++++++++++++++- fs/f2fs/f2fs.h | 4 +--- fs/f2fs/namei.c | 1 + fs/f2fs/super.c | 1 - fs/ubifs/dir.c | 18 ++++++++++++++++++ include/linux/fscrypt.h | 6 ++++-- 13 files changed, 102 insertions(+), 20 deletions(-)
fs/crypto/fname.c +2 -5
@@ -543,7 +543,7 @@ EXPORT_SYMBOL_GPL(fscrypt_fname_siphash);* Validate dentries in encrypted directories to make sure we aren't potentially * caching stale dentries after a key has been added. */ -static int fscrypt_d_revalidate (struct dentry *dentry,unsigned int flags) +int fscrypt_d_revalidate (struct dentry *dentry,unsigned int flags) {struct dentry *dir;int err;
@@ -582,7 +582,4 @@ static int fscrypt_d_revalidate(struct dentry *dentry, unsigned int flags)return valid; } - -const struct dentry_operations fscrypt_d_ops = { - .d_revalidate = fscrypt_d_revalidate, -}; +EXPORT_SYMBOL (fscrypt_d_revalidate);
fs/crypto/fscrypt_private.h +0 -1
@@ -250,7 +250,6 @@ extern int fscrypt_fname_encrypt(const struct inode *inode,extern bool fscrypt_fname_encrypted_size (const struct inode *inode,u32 orig_len,u32 max_len,u32 *encrypted_len_ret); -extern const struct dentry_operations fscrypt_d_ops;/* hkdf.c */
fs/crypto/hooks.c +0 -1
@@ -118,7 +118,6 @@ int __fscrypt_prepare_lookup(struct inode *dir, struct dentry *dentry,spin_lock (&dentry->d_lock); dentry->d_flags |=DCACHE_ENCRYPTED_NAME ;spin_unlock (&dentry->d_lock); -d_set_d_op (dentry, &fscrypt_d_ops); }return err; }
fs/ext4/dir.c +37 -1
@@ -668,8 +668,44 @@ const struct file_operations ext4_dir_operations = { };#ifdef CONFIG_UNICODE -const struct dentry_operations ext4_dentry_ops = { +static const struct dentry_operations ext4_ci_dentry_ops = { .d_hash = utf8_ci_d_hash, .d_compare = utf8_ci_d_compare, };#endif + +#ifdef CONFIG_FS_ENCRYPTION +static const struct dentry_operations ext4_encrypted_dentry_ops = { + .d_revalidate = fscrypt_d_revalidate, +}; +#endif + +#if IS_ENABLED (CONFIG_UNICODE ) &&IS_ENABLED (CONFIG_FS_ENCRYPTION ) +static const struct dentry_operations ext4_encrypted_ci_dentry_ops = { + .d_hash = utf8_ci_d_hash, + .d_compare = utf8_ci_d_compare, + .d_revalidate = fscrypt_d_revalidate, +}; +#endif + +void ext4_set_d_ops (struct inode *dir,struct dentry *dentry) +{ +#ifdef CONFIG_FS_ENCRYPTION +if (dentry->d_flags &DCACHE_ENCRYPTED_NAME ) { +#ifdef CONFIG_UNICODE +if (dir->i_sb->s_encoding) { +d_set_d_op (dentry, &ext4_encrypted_ci_dentry_ops); +return ; + } +#endif +d_set_d_op (dentry, &ext4_encrypted_dentry_ops); +return ; + } +#endif +#ifdef CONFIG_UNICODE +if (dir->i_sb->s_encoding) { +d_set_d_op (dentry, &ext4_ci_dentry_ops); +return ; + } +#endif +}
fs/ext4/ext4.h +1 -0
@@ -2494,6 +2494,7 @@ static inline unsigned char get_dtype(struct super_block *sb, int filetype) }extern int ext4_check_all_de (struct inode *dir,struct buffer_head *bh,void *buf,int buf_size); +void ext4_set_d_ops (struct inode *dir,struct dentry *dentry);/* fsync.c */ extern int ext4_sync_file (struct file *,loff_t ,loff_t ,int );
fs/ext4/namei.c +1 -0
@@ -1608,6 +1608,7 @@ static struct buffer_head *ext4_lookup_entry(struct inode *dir,struct buffer_head *bh; err =ext4_fname_prepare_lookup (dir, dentry, &fname); +ext4_set_d_ops (dir, dentry);if (err == -ENOENT )return NULL ;if (err)
fs/ext4/super.c +0 -5
@@ -4497,11 +4497,6 @@ static int ext4_fill_super(struct super_block *sb, void *data, int silent) goto failed_mount4; } -#ifdef CONFIG_UNICODE -if (sbi->s_encoding) - sb->s_d_op = &ext4_dentry_ops; -#endif - sb->s_root =d_make_root (root);if (!sb->s_root) {ext4_msg (sb,KERN_ERR ,"get root dentry failed" );
fs/f2fs/dir.c +37 -1
@@ -1065,8 +1065,44 @@ const struct file_operations f2fs_dir_operations = { };#ifdef CONFIG_UNICODE -const struct dentry_operations f2fs_dentry_ops = { +static const struct dentry_operations f2fs_ci_dentry_ops = { .d_hash = utf8_ci_d_hash, .d_compare = utf8_ci_d_compare, };#endif + +#ifdef CONFIG_FS_ENCRYPTION +static const struct dentry_operations f2fs_encrypted_dentry_ops = { + .d_revalidate = fscrypt_d_revalidate, +}; +#endif + +#if IS_ENABLED (CONFIG_UNICODE ) &&IS_ENABLED (CONFIG_FS_ENCRYPTION ) +static const struct dentry_operations f2fs_encrypted_ci_dentry_ops = { + .d_hash = utf8_ci_d_hash, + .d_compare = utf8_ci_d_compare, + .d_revalidate = fscrypt_d_revalidate, +}; +#endif + +void f2fs_set_d_ops (struct inode *dir,struct dentry *dentry) +{ +#ifdef CONFIG_FS_ENCRYPTION +if (dentry->d_flags &DCACHE_ENCRYPTED_NAME ) { +#ifdef CONFIG_UNICODE +if (dir->i_sb->s_encoding) { +d_set_d_op (dentry, &f2fs_encrypted_ci_dentry_ops); +return ; + } +#endif +d_set_d_op (dentry, &f2fs_encrypted_dentry_ops); +return ; + } +#endif +#ifdef CONFIG_UNICODE +if (dir->i_sb->s_encoding) { +d_set_d_op (dentry, &f2fs_ci_dentry_ops); +return ; + } +#endif +}
fs/f2fs/f2fs.h +1 -3
@@ -3017,6 +3017,7 @@ static inline int f2fs_add_link(struct dentry *dentry, struct inode *inode)return f2fs_do_add_link (d_inode (dentry->d_parent), &dentry->d_name, inode, inode->i_ino, inode->i_mode); } +void f2fs_set_d_ops (struct inode *dir,struct dentry *dentry);/* * super.c
@@ -3481,9 +3482,6 @@ static inline void f2fs_destroy_root_stats(void) { }#endif extern const struct file_operations f2fs_dir_operations; -#ifdef CONFIG_UNICODE -extern const struct dentry_operations f2fs_dentry_ops; -#endif extern const struct file_operations f2fs_file_operations;extern const struct inode_operations f2fs_file_inode_operations;extern const struct address_space_operations f2fs_dblock_aops;
fs/f2fs/namei.c +1 -0
@@ -443,6 +443,7 @@ static struct dentry *f2fs_lookup(struct inode *dir, struct dentry *dentry, } err =fscrypt_prepare_lookup (dir, dentry, &fname); +f2fs_set_d_ops (dir, dentry);if (err == -ENOENT ) goto out_splice;if (err)
fs/f2fs/super.c +0 -1
@@ -3169,7 +3169,6 @@ static int f2fs_setup_casefold(struct f2fs_sb_info *sbi) sbi->sb->s_encoding = encoding; sbi->sb->s_encoding_flags = encoding_flags; - sbi->sb->s_d_op = &f2fs_dentry_ops; }#else if (f2fs_sb_has_casefold (sbi)) {
fs/ubifs/dir.c +18 -0
@@ -196,6 +196,7 @@ static int dbg_check_name(const struct ubifs_info *c,return 0 ; } +static void ubifs_set_d_ops (struct inode *dir,struct dentry *dentry);static struct dentry *ubifs_lookup (struct inode *dir,struct dentry *dentry,unsigned int flags) {
@@ -209,6 +210,7 @@ static struct dentry *ubifs_lookup(struct inode *dir, struct dentry *dentry,dbg_gen ("'%pd' in dir ino %lu" , dentry, dir->i_ino); err =fscrypt_prepare_lookup (dir, dentry, &nm); +ubifs_set_d_ops (dir, dentry);if (err == -ENOENT )return d_splice_alias (NULL , dentry);if (err)
@@ -1655,3 +1657,19 @@ const struct file_operations ubifs_dir_operations = { .compat_ioctl = ubifs_compat_ioctl,#endif }; + +#ifdef CONFIG_FS_ENCRYPTION +static const struct dentry_operations ubifs_encrypted_dentry_ops = { + .d_revalidate = fscrypt_d_revalidate, +}; +#endif + +static void ubifs_set_d_ops (struct inode *dir,struct dentry *dentry) +{ +#ifdef CONFIG_FS_ENCRYPTION +if (dentry->d_flags &DCACHE_ENCRYPTED_NAME ) { +d_set_d_op (dentry, &ubifs_encrypted_dentry_ops); +return ; + } +#endif +}
include/linux/fscrypt.h +4 -2
@@ -134,6 +134,7 @@ static inline struct page *fscrypt_pagecache_page(struct page *bounce_page) }extern void fscrypt_free_bounce_page (struct page *bounce_page); +extern int fscrypt_d_revalidate (struct dentry *dentry,unsigned int flags);/* policy.c */ extern int fscrypt_ioctl_set_policy (struct file *,const void __user *);
@@ -595,8 +596,9 @@ static inline int fscrypt_prepare_rename(struct inode *old_dir,* filenames are presented in encrypted form. Therefore, we'll try to set up * the directory's encryption key, but even without it the lookup can continue. * -* This also installs a custom ->d_revalidate() method which will invalidate the -* dentry if it was created without the key and the key is later added. +* After calling this function, a filesystem should ensure that it's dentry +* operations contain fscrypt_d_revalidate if DCACHE_ENCRYPTED_NAME was set, +* so that the dentry can be invalidated if the key is later added. * * Return: 0 on success; -ENOENT if key is unavailable but the filename isn't a * correctly formed encoded ciphertext name, so a negative dentry should be
Signature · 1 line
2.25.0.341.g760bfbb309-goog
PATCH v6 3/5 f2fs: Handle casefolding with Encryption
In reply to Daniel RosenbergThis expands f2fs's casefolding support to include encrypted directories. For encrypted directories, we use the siphash of the casefolded name. This ensures there is no direct way to go from an unencrypted name to the stored hash on disk without knowledge of the encryption policy keys.
Additionally, we switch to using the vfs layer's casefolding support instead of storing this information inside of f2fs's private data.
--- fs/f2fs/dir.c | 65 ++++++++++++++++++++++++++++++++---------------- fs/f2fs/f2fs.h | 8 +++--- fs/f2fs/hash.c | 23 +++++++++++------ fs/f2fs/inline.c | 9 ++++--- fs/f2fs/super.c | 6 ----- 5 files changed, 68 insertions(+), 43 deletions(-)
fs/f2fs/dir.c +43 -22
@@ -112,30 +112,50 @@ static struct f2fs_dir_entry *find_in_block(struct inode *dir,* doesn't match or less than zero on error. */ int f2fs_ci_compare (const struct inode *parent,const struct qstr *name, -const struct qstr *entry,bool quick) +unsigned char *name2,size_t len,bool quick) {const struct super_block *sb = parent->i_sb;const struct unicode_map *um = sb->s_encoding; +const struct fscrypt_str crypt_entry =FSTR_INIT (name2, len); +struct fscrypt_str decrypted_entry; +struct qstr decrypted; +struct qstr entry =QSTR_INIT (name2, len); +struct qstr *tocheck;int ret; + decrypted_entry.name =NULL ; + +if (IS_ENCRYPTED (parent) &&fscrypt_has_encryption_key (parent)) { + decrypted_entry.name =kmalloc (len,GFP_ATOMIC ); + decrypted.name = decrypted_entry.name; + decrypted_entry.len = len; + decrypted.len = len; +if (!decrypted.name) +return -ENOMEM ; + ret =fscrypt_fname_disk_to_usr (parent,0 ,0 , &crypt_entry, + &decrypted_entry); +if (ret <0 ) + goto out; + } + tocheck = decrypted_entry.name ? &decrypted : &entry; +if (quick) - ret =utf8_strncasecmp_folded (um, name, entry); + ret =utf8_strncasecmp_folded (um, name, tocheck);else - ret =utf8_strncasecmp (um, name, entry); - + ret =utf8_strncasecmp (um, name, tocheck);if (ret <0 ) {/* Handle invalid character sequence as either an error * or as an opaque byte sequence. */ if (sb_has_enc_strict_mode (sb)) -return -EINVAL ; - -if (name->len != entry->len) -return 1 ; - -return !!memcmp (name->name, entry->name, name->len); + ret = -EINVAL ; +else if (name->len != len) + ret =1 ; +else + ret = !!memcmp (name->name, tocheck->name, len); } - +out: +kfree (decrypted_entry.name);return ret; }
@@ -173,24 +193,24 @@ static inline bool f2fs_match_name(struct f2fs_dentry_ptr *d, {#ifdef CONFIG_UNICODE struct inode *parent = d->inode; -struct super_block *sb = parent->i_sb; -struct qstr entry; +unsigned char *name; +int len;#endif if (de->hash_code != namehash)return false;#ifdef CONFIG_UNICODE - entry.name = d->filename[bit_pos]; - entry.len = de->name_len; + name = d->filename[bit_pos]; + len = de->name_len; -if (sb->s_encoding &&IS_CASEFOLDED (parent)) { +if (needs_casefold (parent)) {if (cf_str->name) {struct qstr cf = {.name = cf_str->name, .len = cf_str->len}; -return !f2fs_ci_compare (parent, &cf, &entry, true); +return !f2fs_ci_compare (parent, &cf, name, len, true); } -return !f2fs_ci_compare (parent, fname->usr_fname, &entry, +return !f2fs_ci_compare (parent, fname->usr_fname, name, len, false); }#endif
@@ -602,13 +622,13 @@ void f2fs_update_dentry(nid_t ino, umode_t mode, struct f2fs_dentry_ptr *d,int f2fs_add_regular_entry (struct inode *dir,const struct qstr *new_name,const struct qstr *orig_name, +f2fs_hash_t dentry_hash,struct inode *inode,nid_t ino,umode_t mode) {unsigned int bit_pos;unsigned int level;unsigned int current_depth;unsigned long bidx, block; -f2fs_hash_t dentry_hash;unsigned int nbucket, nblock;struct page *dentry_page =NULL ;struct f2fs_dentry_block *dentry_blk =NULL ;
@@ -618,7 +638,6 @@ int f2fs_add_regular_entry(struct inode *dir, const struct qstr *new_name, level =0 ; slots =GET_DENTRY_SLOTS (new_name->len); - dentry_hash =f2fs_dentry_hash (dir, new_name,NULL ); current_depth =F2FS_I (dir)->i_current_depth;if (F2FS_I (dir)->chash == dentry_hash) {
@@ -704,17 +723,19 @@ int f2fs_add_dentry(struct inode *dir, struct fscrypt_name *fname, struct inode *inode, nid_t ino, umode_t mode) { struct qstr new_name; +f2fs_hash_t dentry_hash;int err = -EAGAIN ; new_name.name =fname_name (fname); new_name.len =fname_len (fname);if (f2fs_has_inline_dentry (dir)) - err =f2fs_add_inline_entry (dir, &new_name, fname->usr_fname, + err =f2fs_add_inline_entry (dir, &new_name, fname, inode, ino, mode); + dentry_hash =f2fs_dentry_hash (dir, &new_name, fname);if (err == -EAGAIN ) err =f2fs_add_regular_entry (dir, &new_name, fname->usr_fname, - inode, ino, mode); + dentry_hash, inode, ino, mode);f2fs_update_time (F2FS_I_SB (dir),REQ_TIME );return err;
fs/f2fs/f2fs.h +4 -4
@@ -2967,7 +2967,7 @@ struct dentry *f2fs_get_parent(struct dentry *child);extern int f2fs_ci_compare (const struct inode *parent,const struct qstr *name, -const struct qstr *entry, +unsigned char *name2,size_t len,bool quick);/*
@@ -3001,7 +3001,7 @@ void f2fs_update_dentry(nid_t ino, umode_t mode, struct f2fs_dentry_ptr *d, const struct qstr *name, f2fs_hash_t name_hash, unsigned int bit_pos);int f2fs_add_regular_entry (struct inode *dir,const struct qstr *new_name, -const struct qstr *orig_name, +const struct qstr *orig_name,f2fs_hash_t dentry_hash,struct inode *inode,nid_t ino,umode_t mode);int f2fs_add_dentry (struct inode *dir,struct fscrypt_name *fname,struct inode *inode,nid_t ino,umode_t mode);
@@ -3035,7 +3035,7 @@ int f2fs_sanity_check_ckpt(struct f2fs_sb_info *sbi);* hash.c */ f2fs_hash_t f2fs_dentry_hash (const struct inode *dir, -const struct qstr *name_info,struct fscrypt_name *fname); +const struct qstr *name_info,const struct fscrypt_name *fname);/* * node.c
@@ -3511,7 +3511,7 @@ struct f2fs_dir_entry *f2fs_find_in_inline_dir(struct inode *dir,int f2fs_make_empty_inline_dir (struct inode *inode,struct inode *parent,struct page *ipage);int f2fs_add_inline_entry (struct inode *dir,const struct qstr *new_name, -const struct qstr *orig_name, +const struct fscrypt_name *fname,struct inode *inode,nid_t ino,umode_t mode); void f2fs_delete_inline_entry(struct f2fs_dir_entry *dentry, struct page *page, struct inode *dir,
fs/f2fs/hash.c +16 -7
@@ -68,8 +68,9 @@ static void str2hashbuf(const unsigned char *msg, size_t len, *buf++ = pad; } -static f2fs_hash_t __f2fs_dentry_hash (const struct qstr *name_info, -struct fscrypt_name *fname) +static f2fs_hash_t __f2fs_dentry_hash (const struct inode *dir, +const struct qstr *name_info, +const struct fscrypt_name *fname) {__u32 hash;f2fs_hash_t f2fs_hash;
@@ -85,6 +86,11 @@ static f2fs_hash_t __f2fs_dentry_hash(const struct qstr *name_info,if (is_dot_dotdot (name_info))return 0 ; +if (IS_CASEFOLDED (dir) &&IS_ENCRYPTED (dir)) { + f2fs_hash =fscrypt_fname_siphash (dir, name_info); +return f2fs_hash; + } +/* Initialize the default seed for the hash checksum functions */ buf[0 ] =0x67452301 ; buf[1 ] =0xefcdab89 ;
@@ -106,7 +112,7 @@ static f2fs_hash_t __f2fs_dentry_hash(const struct qstr *name_info, }f2fs_hash_t f2fs_dentry_hash (const struct inode *dir, -const struct qstr *name_info,struct fscrypt_name *fname) +const struct qstr *name_info,const struct fscrypt_name *fname) {#ifdef CONFIG_UNICODE struct f2fs_sb_info *sbi =F2FS_SB (dir->i_sb);
@@ -114,27 +120,30 @@ f2fs_hash_t f2fs_dentry_hash(const struct inode *dir,int r, dlen;unsigned char *buff;struct qstr folded; +const struct qstr *name = fname ? fname->usr_fname : name_info;if (!name_info->len || !IS_CASEFOLDED (dir)) goto opaque_seq; +if (IS_ENCRYPTED (dir) && !fscrypt_has_encryption_key (dir)) + goto opaque_seq; + buff =f2fs_kzalloc (sbi,sizeof (char ) *PATH_MAX ,GFP_KERNEL );if (!buff)return -ENOMEM ; - - dlen =utf8_casefold (um, name_info, buff,PATH_MAX ); + dlen =utf8_casefold (um, name, buff,PATH_MAX );if (dlen <0 ) {kvfree (buff); goto opaque_seq; } folded.name = buff; folded.len = dlen; - r =__f2fs_dentry_hash (&folded, fname); + r =__f2fs_dentry_hash (dir, &folded, fname);kvfree (buff);return r; opaque_seq:#endif -return __f2fs_dentry_hash (name_info, fname); +return __f2fs_dentry_hash (dir, name_info, fname); }
fs/f2fs/inline.c +5 -4
@@ -465,8 +465,8 @@ static int f2fs_add_inline_entries(struct inode *dir, void *inline_dentry) ino =le32_to_cpu (de->ino); fake_mode =f2fs_get_de_type (de) <<S_SHIFT ; - err =f2fs_add_regular_entry (dir, &new_name,NULL ,NULL , - ino, fake_mode); + err =f2fs_add_regular_entry (dir, &new_name,NULL , + de->hash_code,NULL , ino, fake_mode);if (err) goto punch_dentry_pages;
@@ -540,7 +540,7 @@ static int f2fs_convert_inline_dir(struct inode *dir, struct page *ipage, }int f2fs_add_inline_entry (struct inode *dir,const struct qstr *new_name, -const struct qstr *orig_name, +const struct fscrypt_name *fname,struct inode *inode,nid_t ino,umode_t mode) {struct f2fs_sb_info *sbi =F2FS_I_SB (dir);
@@ -551,6 +551,7 @@ int f2fs_add_inline_entry(struct inode *dir, const struct qstr *new_name,struct f2fs_dentry_ptr d;int slots =GET_DENTRY_SLOTS (new_name->len);struct page *page =NULL ; +const struct qstr *orig_name = fname->usr_fname;int err =0 ; ipage =f2fs_get_node_page (sbi, dir->i_ino);
@@ -581,7 +582,7 @@ int f2fs_add_inline_entry(struct inode *dir, const struct qstr *new_name,f2fs_wait_on_page_writeback (ipage,NODE , true, true); - name_hash =f2fs_dentry_hash (dir, new_name,NULL ); + name_hash =f2fs_dentry_hash (dir, new_name, fname);f2fs_update_dentry (ino, mode, &d, new_name, name_hash, bit_pos);set_page_dirty (ipage);
fs/f2fs/super.c +0 -6
@@ -3141,12 +3141,6 @@ static int f2fs_setup_casefold(struct f2fs_sb_info *sbi)struct unicode_map *encoding;__u16 encoding_flags; -if (f2fs_sb_has_encrypt (sbi)) { -f2fs_err (sbi, -"Can't mount with encoding and encryption" ); -return -EINVAL ; - } -if (f2fs_sb_read_encoding (sbi->raw_super, &encoding_info, &encoding_flags)) { f2fs_err(sbi,
Signature · 1 line
2.25.0.341.g760bfbb309-goog
PATCH v6 4/5 ext4: Hande casefolding with encryption
In reply to Daniel RosenbergThis adds support for encryption with casefolding.
Since the name on disk is case preserving, and also encrypted, we can no longer just recompute the hash on the fly. Additionally, to avoid leaking extra information from the hash of the unencrypted name, we use siphash via an fscrypt v2 policy.
The hash is stored at the end of the directory entry for all entries inside of an encrypted and casefolded directory apart from those that deal with '.' and '..'. This way, the change is backwards compatible with existing ext4 filesystems.
--- Documentation/filesystems/ext4/directory.rst | 27 ++ fs/ext4/dir.c | 27 +- fs/ext4/ext4.h | 64 +++- fs/ext4/hash.c | 24 +- fs/ext4/ialloc.c | 5 +- fs/ext4/inline.c | 41 +-- fs/ext4/namei.c | 291 +++++++++++++------ fs/ext4/super.c | 6 - 8 files changed, 343 insertions(+), 142 deletions(-)
Documentation/filesystems/ext4/directory.rst +27 -0
@@ -121,6 +121,31 @@ The directory file type is one of the following values: * - 0x7 - Symbolic link. +To support directories that are both encrypted and casefolded directories, we +must also include hash information in the directory entry. We append +``ext4_extended_dir_entry_2`` to ``ext4_dir_entry_2`` except for the entries +for dot and dotdot, which are kept the same. The structure follows immediately +after ``name`` and is included in the size listed by ``rec_len`` If a directory +entry uses this extension, it may be up to 271 bytes. + +.. list-table:: + :widths: 8 8 24 40 + :header-rows: 1 + + * - Offset + - Size + - Name + - Description + * - 0x0 + - \_\_le32 + - hash + - The hash of the directory name + * - 0x4 + - \_\_le32 + - minor\_hash + - The minor hash of the directory name + + In order to add checksums to these classic directory blocks, a phony ``struct ext4_dir_entry`` is placed at the end of each leaf block to hold the checksum. The directory entry is 12 bytes long. The inode
@@ -322,6 +347,8 @@ The directory hash is one of the following values: - Half MD4, unsigned. * - 0x5 - Tea, unsigned. + * - 0x6 + - Siphash. Interior nodes of an htree are recorded as ``struct dx_node``, which is also the full length of a data block:
fs/ext4/dir.c +17 -10
@@ -30,6 +30,8 @@#include "ext4.h" #include "xattr.h" +#define DOTDOT_OFFSET 12 +static int ext4_dx_readdir (struct file *,struct dir_context *);/**
@@ -67,22 +69,26 @@ int __ext4_check_dir_entry(const char *function, unsigned int line, struct inode *dir, struct file *filp, struct ext4_dir_entry_2 *de, struct buffer_head *bh, char *buf, int size, + ext4_lblk_t lblk, unsigned int offset) { const char *error_msg = NULL;const int rlen =ext4_rec_len_from_disk (de->rec_len, dir->i_sb->s_blocksize);const int next_offset = ((char *) de - buf) + rlen; +bool fake = (lblk ==0 ) && (offset <=DOTDOT_OFFSET ); -if (unlikely (rlen <EXT4_DIR_REC_LEN (1 ))) +if (unlikely (rlen <ext4_dir_rec_len (1 , fake ?NULL : dir))) error_msg ="rec_len is smaller than minimal" ;else if (unlikely (rlen %4 !=0 )) error_msg ="rec_len % 4 != 0" ; -else if (unlikely (rlen <EXT4_DIR_REC_LEN (de->name_len))) +else if (unlikely (rlen <ext4_dir_rec_len (de->name_len, + fake ?NULL : dir))) error_msg ="rec_len is too small for name_len" ;else if (unlikely (next_offset > size)) error_msg ="directory entry overrun" ; -else if (unlikely (next_offset > size -EXT4_DIR_REC_LEN (1 ) && +else if (unlikely (next_offset > size -ext4_dir_rec_len (1 , + fake ?NULL : dir) && next_offset != size)) error_msg ="directory entry too close to block end" ; else if (unlikely(le32_to_cpu(de->inode) >
@@ -94,15 +100,15 @@ int __ext4_check_dir_entry(const char *function, unsigned int line,if (filp)ext4_error_file (filp, function, line, bh->b_blocknr,"bad entry in directory: %s - offset=%u, " -"inode=%u, rec_len=%d, name_len=%d, size=%d" , +"inode=%u, rec_len=%d, lblk=%d, size=%d" , error_msg, offset,le32_to_cpu (de->inode), - rlen, de->name_len, size); + rlen, lblk, size);else ext4_error_inode (dir, function, line, bh->b_blocknr,"bad entry in directory: %s - offset=%u, " -"inode=%u, rec_len=%d, name_len=%d, size=%d" , +"inode=%u, rec_len=%d, lblk=%d, size=%d" , error_msg, offset,le32_to_cpu (de->inode), - rlen, de->name_len, size); + rlen, lblk, size);return 1 ; }
@@ -224,7 +230,8 @@ static int ext4_readdir(struct file *file, struct dir_context *ctx)* failure will be detected in the * dirent test below. */ if (ext4_rec_len_from_disk (de->rec_len, - sb->s_blocksize) <EXT4_DIR_REC_LEN (1 )) + sb->s_blocksize) <ext4_dir_rec_len (1 , + inode))break ; i +=ext4_rec_len_from_disk (de->rec_len, sb->s_blocksize);
@@ -240,7 +247,7 @@ static int ext4_readdir(struct file *file, struct dir_context *ctx) de = (struct ext4_dir_entry_2 *) (bh->b_data + offset);if (ext4_check_dir_entry (inode, file, de, bh, bh->b_data, bh->b_size, - offset)) { + map.m_lblk, offset)) {/* * On error, skip to the next block */
@@ -642,7 +649,7 @@ int ext4_check_all_de(struct inode *dir, struct buffer_head *bh, void *buf, top = buf + buf_size;while ((char *) de < top) {if (ext4_check_dir_entry (dir,NULL , de, bh, - buf, buf_size, offset)) + buf, buf_size,0 , offset))return -EFSCORRUPTED ; rlen =ext4_rec_len_from_disk (de->rec_len, buf_size); de = (struct ext4_dir_entry_2 *)((char *)de + rlen);
fs/ext4/ext4.h +56 -8
131 lines not on this page · raw message
fs/ext4/hash.c +20 -4
@@ -197,7 +197,7 @@ static void str2hashbuf_unsigned(const char *msg, int len, __u32 *buf, int num)* represented, and whether or not the returned hash is 32 bits or 64 * bits. 32 bit hashes will return 0 for the minor hash. */ -static int __ext4fs_dirhash (const char *name,int len, +static int __ext4fs_dirhash (const struct inode *dir,const char *name,int len,struct dx_hash_info *hinfo) {__u32 hash;
@@ -259,6 +259,22 @@ static int __ext4fs_dirhash(const char *name, int len, hash = buf[0 ]; minor_hash = buf[1 ];break ; +case DX_HASH_SIPHASH : + { +struct qstr qname =QSTR_INIT (name, len); +__u64 combined_hash; + +if (fscrypt_has_encryption_key (dir)) { + combined_hash =fscrypt_fname_siphash (dir, &qname); + }else { +ext4_warning_inode (dir,"Siphash requires key" ); +return -1 ; + } + + hash = (__u32)(combined_hash >>32 ); + minor_hash = (__u32 )combined_hash; +break ; + }default : hinfo->hash =0 ;return -1 ;
@@ -280,7 +296,7 @@ int ext4fs_dirhash(const struct inode *dir, const char *name, int len,unsigned char *buff;struct qstr qstr = {.name = name, .len = len }; -if (len &&IS_CASEFOLDED (dir) && um) { +if (len &&needs_casefold (dir) && um) { buff =kzalloc (sizeof (char ) *PATH_MAX ,GFP_KERNEL );if (!buff)return -ENOMEM ;
@@ -291,12 +307,12 @@ int ext4fs_dirhash(const struct inode *dir, const char *name, int len, goto opaque_seq; } - r =__ext4fs_dirhash (buff, dlen, hinfo); + r =__ext4fs_dirhash (dir, buff, dlen, hinfo);kfree (buff);return r; } opaque_seq: #endif -return __ext4fs_dirhash (name, len, hinfo); +return __ext4fs_dirhash (dir, name, len, hinfo); }
fs/ext4/ialloc.c +4 -1
@@ -448,7 +448,10 @@ static int find_group_orlov(struct super_block *sb, struct inode *parent,int ret =-1 ;if (qstr) { - hinfo.hash_version =DX_HASH_HALF_MD4 ; +if (ext4_hash_in_dirent (parent)) + hinfo.hash_version =DX_HASH_SIPHASH ; +else + hinfo.hash_version =DX_HASH_HALF_MD4 ; hinfo.seed = sbi->s_hash_seed;ext4fs_dirhash (parent, qstr->name, qstr->len, &hinfo); grp = hinfo.hash;
fs/ext4/inline.c +23 -18
130 lines not on this page · raw message
fs/ext4/namei.c +196 -95
806 lines not on this page · raw message
fs/ext4/super.c +0 -6
@@ -3855,12 +3855,6 @@ static int ext4_fill_super(struct super_block *sb, void *data, int silent)struct unicode_map *encoding;__u16 encoding_flags; -if (ext4_has_feature_encrypt (sb)) { -ext4_msg (sb,KERN_ERR , -"Can't mount with encoding and encryption" ); - goto failed_mount; - } -if (ext4_sb_read_encoding (es, &encoding_info, &encoding_flags)) { ext4_msg(sb, KERN_ERR,
Signature · 1 line
2.25.0.341.g760bfbb309-goog
PATCH v6 5/5 ext4: Optimize match for casefolded encrypted dirs
In reply to Daniel RosenbergMatching names with casefolded encrypting directories requires decrypting entries to confirm case since we are case preserving. We can avoid needing to decrypt if our hash values don't match.
--- fs/ext4/ext4.h | 17 ++++++++------- fs/ext4/namei.c | 55 ++++++++++++++++++++++++++----------------------- 2 files changed, 38 insertions(+), 34 deletions(-)
fs/ext4/ext4.h +9 -8
@@ -2393,9 +2393,9 @@ extern unsigned ext4_free_clusters_after_init(struct super_block *sb,ext4_fsblk_t ext4_inode_to_goal_block (struct inode *);#ifdef CONFIG_UNICODE -extern void ext4_fname_setup_ci_filename (struct inode *dir, +extern int ext4_fname_setup_ci_filename (struct inode *dir,const struct qstr *iname, -struct fscrypt_str *fname); +struct ext4_filename *fname);#endif #ifdef CONFIG_FS_ENCRYPTION
@@ -2426,9 +2426,9 @@ static inline int ext4_fname_setup_filename(struct inode *dir,ext4_fname_from_fscrypt_name (fname, &name);#ifdef CONFIG_UNICODE -ext4_fname_setup_ci_filename (dir, iname, &fname->cf_name); + err =ext4_fname_setup_ci_filename (dir, iname, fname);#endif -return 0 ; +return err; } static inline int ext4_fname_prepare_lookup(struct inode *dir,
@@ -2445,9 +2445,9 @@ static inline int ext4_fname_prepare_lookup(struct inode *dir,ext4_fname_from_fscrypt_name (fname, &name);#ifdef CONFIG_UNICODE -ext4_fname_setup_ci_filename (dir, &dentry->d_name, &fname->cf_name); + err =ext4_fname_setup_ci_filename (dir, &dentry->d_name, fname);#endif -return 0 ; +return err; }static inline void ext4_fname_free_filename (struct ext4_filename *fname)
@@ -2472,15 +2472,16 @@ static inline int ext4_fname_setup_filename(struct inode *dir,int lookup, struct ext4_filename *fname) { + int err = 0; fname->usr_fname = iname; fname->disk_name.name = (unsigned char *) iname->name; fname->disk_name.len = iname->len;#ifdef CONFIG_UNICODE -ext4_fname_setup_ci_filename (dir, iname, &fname->cf_name); + err =ext4_fname_setup_ci_filename (dir, iname, fname);#endif -return 0 ; +return err; } static inline int ext4_fname_prepare_lookup(struct inode *dir,
fs/ext4/namei.c +29 -26
106 lines not on this page · raw message
Signature · 1 line
2.25.0.341.g760bfbb309-goog