Add support for Encryption and Casefolding in F2FS
Subsystems (6)
cover Add support for Encryption and Casefolding in F2FS
These patches are on top of the f2fs dev branch
F2FS currently supports casefolding and encryption, but not at the same time. These patches aim to rectify that. In a later follow up, this will be added for Ext4 as well. I've included one ext4 patch from the previous set since it isn't in the f2fs branch, but is needed for the fscrypt changes.
The f2fs-tools changes have already been applied.
Since both fscrypt and casefolding require their own dentry operations, I've moved the responsibility of setting the dentry operations from fscrypt to the filesystems and provided helper functions that should work for most cases.
These are a follow-up to the previously sent patch set "[PATCH v12 0/4] Prepare for upcoming Casefolding/Encryption patches"
Daniel Rosenberg (5):
ext4: Use generic casefolding support
fscrypt: Export fscrypt_d_revalidate
libfs: Add generic function for setting dentry_ops
fscrypt: Have filesystems handle their d_ops
f2fs: Handle casefolding with Encryption
fs/crypto/fname.c | 7 ++--- fs/crypto/hooks.c | 1 - fs/ext4/dir.c | 67 ----------------------------------------- fs/ext4/ext4.h | 16 ---------- fs/ext4/hash.c | 2 +- fs/ext4/namei.c | 21 ++++++------- fs/ext4/super.c | 15 +++------ fs/f2fs/dir.c | 64 ++++++++++++++++++++++++++++++--------- fs/f2fs/f2fs.h | 11 +++---- fs/f2fs/hash.c | 11 ++++++- fs/f2fs/namei.c | 1 + fs/f2fs/recovery.c | 12 +++++++- fs/f2fs/super.c | 7 ----- fs/libfs.c | 49 ++++++++++++++++++++++++++++++ fs/ubifs/dir.c | 1 + include/linux/fs.h | 1 + include/linux/fscrypt.h | 6 ++-- 17 files changed, 148 insertions(+), 144 deletions(-)
Signature · 1 line
2.28.0.681.g6f77f65b4e-goog
PATCH 1/5 ext4: Use generic casefolding support
This switches ext4 over to the generic support provided in the previous patch.
Since casefolded dentries behave the same in ext4 and f2fs, we decrease the maintenance burden by unifying them, and any optimizations will immediately apply to both.
--- fs/ext4/dir.c | 64 ++----------------------------------------------- fs/ext4/ext4.h | 12 ---------- fs/ext4/hash.c | 2 +- fs/ext4/namei.c | 20 +++++++--------- fs/ext4/super.c | 12 +++++----- 5 files changed, 17 insertions(+), 93 deletions(-)
fs/ext4/dir.c +2 -62
@@ -669,68 +669,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 }; -const struct dentry *parent =READ_ONCE (dentry->d_parent); -const struct inode *inode =READ_ONCE (parent->d_inode); -char strbuf[DNAME_INLINE_LEN ]; - -if (!inode || !IS_CASEFOLDED (inode) || - !EXT4_SB (inode->i_sb)->s_encoding) { -if (len != name->len) -return -1 ; -return memcmp (str, name->name, len); - } - -/* -* If the dentry name is stored in-line, then it may be concurrently -* modified by a rename. If this happens, the VFS will eventually retry -* the lookup, so it doesn't matter what ->d_compare() returns. -* However, it's unsafe to call utf8_strncasecmp() with an unstable -* string. Therefore, we have to copy the name into a temporary buffer. -*/ -if (len <=DNAME_INLINE_LEN -1 ) { -memcpy (strbuf, str, len); - strbuf[len] =0 ; - qstr.name = strbuf; -/* prevent compiler from optimizing out the temporary buffer */ -barrier (); - } - -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; -const struct inode *inode =READ_ONCE (dentry->d_inode); -unsigned char *norm; -int len, ret =0 ; - -if (!inode || !IS_CASEFOLDED (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 = generic_ci_d_hash, + .d_compare = generic_ci_d_compare, };#endif
fs/ext4/ext4.h +0 -12
@@ -1411,14 +1411,6 @@ struct ext4_super_block {#define EXT4_ENC_UTF8_12_1 1 -/* -* Flags for ext4_sb_info.s_encoding_flags. -*/ -#define EXT4_ENC_STRICT_MODE_FL (1 << 0) - -#define ext4_has_strict_mode (sbi) \ - (sbi->s_encoding_flags & EXT4_ENC_STRICT_MODE_FL) -/* * fourth extended-fs super-block data in memory */
@@ -1468,10 +1460,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
@@ -1286,8 +1286,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)
@@ -1299,7 +1299,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_strict_encoding (sb))return -EINVAL ;if (name->len != entry->len)
@@ -1316,7 +1316,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 (!IS_CASEFOLDED (dir) || !dir->i_sb->s_encoding) { cf_name->name =NULL ;return ; }
@@ -1325,7 +1325,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 ) {
@@ -1362,7 +1362,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 (parent->i_sb->s_encoding &&IS_CASEFOLDED (parent)) {if (fname->cf_name.name) {struct qstr cf = {.name = fname->cf_name.name, .len = fname->cf_name.len};
@@ -2181,9 +2181,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 ;
@@ -2200,9 +2197,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_strict_encoding (sb) &&IS_CASEFOLDED (dir) && + sb->s_encoding &&utf8_validate (sb->s_encoding, &dentry->d_name))return -EINVAL ;#endif
fs/ext4/super.c +6 -6
@@ -1106,7 +1106,7 @@ static void ext4_put_super(struct super_block *sb)fs_put_dax (sbi->s_daxdev);fscrypt_free_dummy_context (&sbi->s_dummy_enc_ctx);#ifdef CONFIG_UNICODE -utf8_unload (sbi->s_encoding); +utf8_unload (sb->s_encoding);#endif kfree (sbi); }
@@ -4077,7 +4077,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;
@@ -4108,8 +4108,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
@@ -4720,7 +4720,7 @@ static int ext4_fill_super(struct super_block *sb, void *data, int silent) }#ifdef CONFIG_UNICODE -if (sbi->s_encoding) +if (sb->s_encoding) sb->s_d_op = &ext4_dentry_ops;#endif
@@ -4928,7 +4928,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
Signature · 1 line
2.28.0.681.g6f77f65b4e-goog
Eric Biggers
maintainerRe: [PATCH 1/5] ext4: Use generic casefolding support
Quoted 9 lines · Daniel Rosenberg
This switches ext4 over to the generic support provided in the previous patch.
Since casefolded dentries behave the same in ext4 and f2fs, we decrease the maintenance burden by unifying them, and any optimizations will immediately apply to both.
Signed-off-by Daniel Rosenberg <[email protected]>Reviewed-by Eric Biggers <[email protected]>
You could also add Gabriel's Reviewed-by from last time:
https://lkml.kernel.org/linux-fsdevel/[email protected]/
- Eric
Re: [PATCH 1/5] ext4: Use generic casefolding support
Quoted 8 lines · Eric Biggers
Quoted 7 lines · Daniel Rosenberg
This switches ext4 over to the generic support provided in the previous patch.
Since casefolded dentries behave the same in ext4 and f2fs, we decrease the maintenance burden by unifying them, and any optimizations will immediately apply to both.
Quoted 5 lines
Quoted 2 lines
Signed-off-by Daniel Rosenberg <[email protected]>Reviewed-by Eric Biggers <[email protected]>You could also add Gabriel's Reviewed-by from last time:
https://lkml.kernel.org/linux-fsdevel/[email protected]/
Yep, I was gonna say that. Assuming nothing changed from the last submission in the other series.
Thanks,
Signature · 1 line
Gabriel Krisman Bertazi
PATCH 2/5 fscrypt: Export fscrypt_d_revalidate
In reply to Daniel RosenbergThis is in preparation for shifting the responsibility of setting the dentry_operations to the filesystem, allowing it to maintain its own operations.
--- fs/crypto/fname.c | 3 ++- include/linux/fscrypt.h | 1 + 2 files changed, 3 insertions(+), 1 deletion(-)
fs/crypto/fname.c +2 -1
@@ -541,7 +541,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;
@@ -580,6 +580,7 @@ static int fscrypt_d_revalidate(struct dentry *dentry, unsigned int flags)return valid; } +EXPORT_SYMBOL_GPL (fscrypt_d_revalidate);const struct dentry_operations fscrypt_d_ops = { .d_revalidate = fscrypt_d_revalidate,
include/linux/fscrypt.h +1 -0
@@ -207,6 +207,7 @@ int fscrypt_fname_disk_to_usr(const struct inode *inode,bool fscrypt_match_name (const struct fscrypt_name *fname,const u8 *de_name,u32 de_name_len);u64 fscrypt_fname_siphash (const struct inode *dir,const struct qstr *name); +extern int fscrypt_d_revalidate (struct dentry *dentry,unsigned int flags);/* bio.c */ void fscrypt_decrypt_bio (struct bio *bio);
Signature · 1 line
2.28.0.681.g6f77f65b4e-goog
Eric Biggers
maintainerRe: [PATCH 2/5] fscrypt: Export fscrypt_d_revalidate
Quoted 36 lines · Daniel Rosenberg
This is in preparation for shifting the responsibility of setting the
dentry_operationsto the filesystem, allowing it to maintain its own operations.Signed-off-by Daniel Rosenberg <[email protected]>--- fs/crypto/fname.c | 3 ++- include/linux/fscrypt.h | 1 + 2 files changed, 3 insertions(+), 1 deletion(-)fs/crypto/fname.c +2
-1@@ -541,7 +541,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;@@ -580,6 +580,7 @@ static int fscrypt_d_revalidate(struct dentry *dentry, unsigned int flags)return valid; } +EXPORT_SYMBOL_GPL (fscrypt_d_revalidate);const struct dentry_operations fscrypt_d_ops = { .d_revalidate = fscrypt_d_revalidate,include/linux/fscrypt.h +0
-0
include/linux/fscrypt.h int fscrypt_fname_disk_to_usr(const struct inode *inode, · line 210
Quoted 4 lines
include/linux/fscrypt.h +1
-0@@ -207,6 +207,7 @@ int fscrypt_fname_disk_to_usr(const struct inode *inode,bool fscrypt_match_name (const struct fscrypt_name *fname,const u8 *de_name,u32 de_name_len);u64 fscrypt_fname_siphash (const struct inode *dir,const struct qstr *name); +extern int fscrypt_d_revalidate (struct dentry *dentry,unsigned int flags);
Please don't use 'extern' here.
Also FYI, Jeff Layton has sent this same patch as part of the ceph support for
fscrypt: https://lkml.kernel.org/linux-fscrypt/[email protected]
I'd like to apply one of them for 5.10 to get it out of the way for both patchsets, but I'd like for the commit message to mention both users.
- Eric
PATCH 3/5 libfs: Add generic function for setting dentry_ops
In reply to Daniel RosenbergThis adds a function to set dentry operations at lookup time that will work for both encrypted files and casefolded filenames.
A filesystem that supports both features simultaneously can use this function during lookup preperations to set up its dentry operations once fscrypt no longer does that itself.
Currently the casefolding dentry operation are always set because the feature is toggleable on empty directories. Since we don't know what set of functions we'll eventually need, and cannot change them later, we add just add them.
--- fs/libfs.c | 49 ++++++++++++++++++++++++++++++++++++++++++++++ include/linux/fs.h | 1 + 2 files changed, 50 insertions(+)
fs/libfs.c +49 -0
@@ -1449,4 +1449,53 @@ int generic_ci_d_hash(const struct dentry *dentry, struct qstr *str)return 0 ; }EXPORT_SYMBOL (generic_ci_d_hash); + +static const struct dentry_operations generic_ci_dentry_ops = { + .d_hash = generic_ci_d_hash, + .d_compare = generic_ci_d_compare, +}; +#endif + +#ifdef CONFIG_FS_ENCRYPTION +static const struct dentry_operations generic_encrypted_dentry_ops = { + .d_revalidate = fscrypt_d_revalidate, +}; +#endif + +#if IS_ENABLED (CONFIG_UNICODE ) &&IS_ENABLED (CONFIG_FS_ENCRYPTION ) +static const struct dentry_operations generic_encrypted_ci_dentry_ops = { + .d_hash = generic_ci_d_hash, + .d_compare = generic_ci_d_compare, + .d_revalidate = fscrypt_d_revalidate, +}; +#endif + +/** +* generic_set_encrypted_ci_d_ops - helper for setting d_ops for given dentry +* @dentry: dentry to set ops on +* +* This function sets the dentry ops for the given dentry to handle both +* casefolding and encryption of the dentry name. +*/ +void generic_set_encrypted_ci_d_ops (struct dentry *dentry) +{ +#ifdef CONFIG_FS_ENCRYPTION +if (dentry->d_flags &DCACHE_ENCRYPTED_NAME ) { +#ifdef CONFIG_UNICODE +if (dentry->d_sb->s_encoding) { +d_set_d_op (dentry, &generic_encrypted_ci_dentry_ops); +return ; + }#endif +d_set_d_op (dentry, &generic_encrypted_dentry_ops); +return ; + } +#endif +#ifdef CONFIG_UNICODE +if (dentry->d_sb->s_encoding) { +d_set_d_op (dentry, &generic_ci_dentry_ops); +return ; + } +#endif +} +EXPORT_SYMBOL (generic_set_encrypted_ci_d_ops);
include/linux/fs.h +1 -0
@@ -3277,6 +3277,7 @@ extern int generic_ci_d_hash(const struct dentry *dentry, struct qstr *str);extern int generic_ci_d_compare (const struct dentry *dentry,unsigned int len,const char *str,const struct qstr *name);#endif +extern void generic_set_encrypted_ci_d_ops (struct dentry *dentry);#ifdef CONFIG_MIGRATION extern int buffer_migrate_page(struct address_space *,
Signature · 1 line
2.28.0.681.g6f77f65b4e-goog
Eric Biggers
maintainerRe: [PATCH 3/5] libfs: Add generic function for setting dentry_ops
Quoted 2 lines · Daniel Rosenberg
This adds a function to set dentry operations at lookup time that will work for both encrypted files and casefolded filenames.
"encrypted files" => "encrypted filenames"
Quoted 4 lines
A filesystem that supports both features simultaneously can use this function during lookup preperations to set up its dentry operations once fscrypt no longer does that itself.
"preperations" => "preparations"
Quoted 5 lines
Currently the casefolding dentry operation are always set because the feature is toggleable on empty directories. Since we don't know what set of functions we'll eventually need, and cannot change them later, we add just add them.
"are always set" => "are always set if the filesystem defines an encoding"
Quoted 7 lines
+/**
+ *generic_set_encrypted_ci_d_ops- helper for settingd_opsfor given dentry
+ * @dentry: dentry to set ops on
+ *
+ * This function sets the dentry ops for the given dentry to handle both
+ * casefolding and encryption of the dentry name.
+ */
But it also seems that some of the information in the commit message should go into this comment so that it isn't lost. It's not clear to someone reading this code what "handling encryption of the dentry name" means (hint: it doesn't actually mean handling encryption...), and why setting the casefolding operations isn't conditional on IS_CASEFOLDED(dir).
- Eric
_______________________________________________
Linux-f2fs-devel mailing list
[email protected]
https://lists.sourceforge.net/lists/listinfo/linux-f2fs-devel
Re: [PATCH 3/5] libfs: Add generic function for setting dentry_ops
In reply to Daniel RosenbergQuoted 65 lines · Daniel Rosenberg
This adds a function to set dentry operations at lookup time that will work for both encrypted files and casefolded filenames.
A filesystem that supports both features simultaneously can use this function during lookup preperations to set up its dentry operations once fscrypt no longer does that itself.
Currently the casefolding dentry operation are always set because the feature is toggleable on empty directories. Since we don't know what set of functions we'll eventually need, and cannot change them later, we add just add them.
Signed-off-by Daniel Rosenberg <[email protected]>--- fs/libfs.c | 49 ++++++++++++++++++++++++++++++++++++++++++++++ include/linux/fs.h | 1 + 2 files changed, 50 insertions(+)fs/libfs.c +38
-0@@ -1449,4 +1449,53 @@ int generic_ci_d_hash(const struct dentry *dentry, struct qstr *str)return 0 ; }EXPORT_SYMBOL (generic_ci_d_hash); + +static const struct dentry_operations generic_ci_dentry_ops = { + .d_hash = generic_ci_d_hash, + .d_compare = generic_ci_d_compare, +}; +#endif + +#ifdef CONFIG_FS_ENCRYPTION +static const struct dentry_operations generic_encrypted_dentry_ops = { + .d_revalidate = fscrypt_d_revalidate, +}; +#endif + +#if IS_ENABLED (CONFIG_UNICODE ) &&IS_ENABLED (CONFIG_FS_ENCRYPTION ) +static const struct dentry_operations generic_encrypted_ci_dentry_ops = { + .d_hash = generic_ci_d_hash, + .d_compare = generic_ci_d_compare, + .d_revalidate = fscrypt_d_revalidate, +}; +#endif + +/** +* generic_set_encrypted_ci_d_ops - helper for setting d_ops for given dentry +* @dentry: dentry to set ops on +* +* This function sets the dentry ops for the given dentry to handle both +* casefolding and encryption of the dentry name. +*/ +void generic_set_encrypted_ci_d_ops (struct dentry *dentry) +{ +#ifdef CONFIG_FS_ENCRYPTION +if (dentry->d_flags &DCACHE_ENCRYPTED_NAME ) { +#ifdef CONFIG_UNICODE +if (dentry->d_sb->s_encoding) { +d_set_d_op (dentry, &generic_encrypted_ci_dentry_ops); +return ; + }#endif +d_set_d_op (dentry, &generic_encrypted_dentry_ops);
fs/libfs.c int generic_ci_d_hash(const struct dentry *dentry, struct qstr *str) · line 1500
Quoted 10 lines
fs/libfs.c +10
-0+ return; + } +#endif +#ifdef CONFIG_UNICODE +if (dentry->d_sb->s_encoding) { +d_set_d_op (dentry, &generic_ci_dentry_ops); +return ; + } +#endif +}
I think this is harder to read than necessary. What do you think about just splitting the three cases like the following:
void generic_set_encrypted_ci_d_ops(struct dentry *dentry) {
#if defined(CONFIG_FS_ENCRYPTION) && defined(CONFIG_UNICODE)
if (encoding && encryption) {
d_set_d_op(dentry, &generic_encrypted_ci_dentry_ops);
return;
}#endif
#if defined (CONFIG_FS_ENCRYPTION)
if (encryption) {
d_set_d_op(dentry, &generic_encrypted_dentry_ops);
return;
}#endif
#if defined (CONFIG_UNICODE)
if (encoding) {
d_set_d_op(dentry, &generic_ci_dentry_ops);
return;
}#endif
}
Quoted 13 lines
fs/libfs.c +1
-0+EXPORT_SYMBOL (generic_set_encrypted_ci_d_ops);include/linux/fs.h +1
-0@@ -3277,6 +3277,7 @@ extern int generic_ci_d_hash(const struct dentry *dentry, struct qstr *str);extern int generic_ci_d_compare (const struct dentry *dentry,unsigned int len,const char *str,const struct qstr *name);#endif +extern void generic_set_encrypted_ci_d_ops (struct dentry *dentry);#ifdef CONFIG_MIGRATION extern int buffer_migrate_page(struct address_space *,
Signature · 1 line
Gabriel Krisman Bertazi
PATCH 4/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.
Most filesystems can just use generic_set_encrypted_ci_d_ops, unless they have their own specific dentry operations as well. That operation will set the minimal d_ops required under the circumstances.
Since the fscrypt d_ops are set later on, we must set all d_ops there, since we cannot adjust those later on. This should not result in any change in behavior.
--- fs/crypto/fname.c | 4 ---- fs/crypto/hooks.c | 1 - fs/ext4/dir.c | 7 ------- fs/ext4/ext4.h | 4 ---- fs/ext4/namei.c | 1 + fs/ext4/super.c | 5 ----- fs/f2fs/dir.c | 7 ------- fs/f2fs/f2fs.h | 3 --- fs/f2fs/namei.c | 1 + fs/f2fs/super.c | 1 - fs/ubifs/dir.c | 1 + include/linux/fscrypt.h | 5 +++-- 12 files changed, 6 insertions(+), 34 deletions(-)
fs/crypto/fname.c +0 -4
@@ -581,7 +581,3 @@ int fscrypt_d_revalidate(struct dentry *dentry, unsigned int flags)return valid; }EXPORT_SYMBOL_GPL (fscrypt_d_revalidate); - -const struct dentry_operations fscrypt_d_ops = { - .d_revalidate = fscrypt_d_revalidate, -};
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 +0 -7
@@ -667,10 +667,3 @@ const struct file_operations ext4_dir_operations = { .open = ext4_dir_open, .release = ext4_release_dir, }; - -#ifdef CONFIG_UNICODE -const struct dentry_operations ext4_dentry_ops = { - .d_hash = generic_ci_d_hash, - .d_compare = generic_ci_d_compare, -}; -#endif
fs/ext4/ext4.h +0 -4
@@ -3247,10 +3247,6 @@ static inline void ext4_unlock_group(struct super_block *sb,/* dir.c */ extern const struct file_operations ext4_dir_operations; -#ifdef CONFIG_UNICODE -extern const struct dentry_operations ext4_dentry_ops; -#endif -/* file.c */ extern const struct inode_operations ext4_file_inode_operations;extern const struct file_operations ext4_file_operations;
fs/ext4/namei.c +1 -0
@@ -1615,6 +1615,7 @@ static struct buffer_head *ext4_lookup_entry(struct inode *dir,struct buffer_head *bh; err =ext4_fname_prepare_lookup (dir, dentry, &fname); +generic_set_encrypted_ci_d_ops (dentry);if (err == -ENOENT )return NULL ;if (err)
fs/ext4/super.c +0 -5
@@ -4719,11 +4719,6 @@ static int ext4_fill_super(struct super_block *sb, void *data, int silent) goto failed_mount4; } -#ifdef CONFIG_UNICODE -if (sb->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 +0 -7
@@ -1106,10 +1106,3 @@ const struct file_operations f2fs_dir_operations = { .compat_ioctl = f2fs_compat_ioctl,#endif }; - -#ifdef CONFIG_UNICODE -const struct dentry_operations f2fs_dentry_ops = { - .d_hash = generic_ci_d_hash, - .d_compare = generic_ci_d_compare, -}; -#endif
fs/f2fs/f2fs.h +0 -3
@@ -3774,9 +3774,6 @@ static inline void f2fs_update_sit_info(struct f2fs_sb_info *sbi) {}#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
@@ -492,6 +492,7 @@ static struct dentry *f2fs_lookup(struct inode *dir, struct dentry *dentry, } err =f2fs_prepare_lookup (dir, dentry, &fname); +generic_set_encrypted_ci_d_ops (dentry);if (err == -ENOENT ) goto out_splice;if (err)
fs/f2fs/super.c +0 -1
@@ -3410,7 +3410,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 +1 -0
@@ -209,6 +209,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); +generic_set_encrypted_ci_d_ops (dentry);if (err == -ENOENT )return d_splice_alias (NULL , dentry);if (err)
include/linux/fscrypt.h +3 -2
@@ -740,8 +740,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 its 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.28.0.681.g6f77f65b4e-goog
Re: [PATCH 4/5] fscrypt: Have filesystems handle their d_ops
Quoted 8 lines · Daniel Rosenberg
This 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_revalidateas appropriate.Most filesystems can just use
generic_set_encrypted_ci_d_ops, unless they have their own specific dentry operations as well. That operation will set the minimald_opsrequired under the circumstances.
Quoted 5 lines
Since the fscrypt
d_opsare set later on, we must set alld_opsthere, since we cannot adjust those later on. This should not result in any change in behavior.Signed-off-by Daniel Rosenberg <[email protected]>
Looks good,
PATCH 5/5 f2fs: Handle casefolding with Encryption
In reply to Daniel RosenbergExpand f2fs's casefolding support to include encrypted directories. To index casefolded+encrypted directories, we use the SipHash of the casefolded name, keyed by a key derived from the directory's fscrypt master key. This ensures that the dirhash doesn't leak information about the plaintext filenames.
Encryption keys are unavailable during roll-forward recovery, so we can't compute the dirhash when recovering a new dentry in an encrypted + casefolded directory. To avoid having to force a checkpoint when a new file is fsync'ed, store the dirhash on-disk appended to i_name.
This patch incorporates work by Eric Biggers <[email protected]> and Jaegeuk Kim <[email protected]>.
--- fs/f2fs/dir.c | 57 +++++++++++++++++++++++++++++++++++++++------- fs/f2fs/f2fs.h | 8 ++++--- fs/f2fs/hash.c | 11 ++++++++- fs/f2fs/recovery.c | 12 +++++++++- fs/f2fs/super.c | 6 ----- 5 files changed, 75 insertions(+), 19 deletions(-)
fs/f2fs/dir.c +49 -8
@@ -5,6 +5,7 @@* Copyright (c) 2012 Samsung Electronics Co., Ltd. * http://www.samsung.com/ */ +#include <asm/unaligned.h> #include <linux/fs.h> #include <linux/f2fs_fs.h> #include <linux/sched/signal.h>
@@ -218,9 +219,28 @@ static bool f2fs_match_ci_name(const struct inode *dir, const struct qstr *name, {const struct super_block *sb = dir->i_sb;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 res; +if (IS_ENCRYPTED (dir)) { +const struct fscrypt_str encrypted_name = +FSTR_INIT ((u8 *)de_name, de_name_len); + +if (WARN_ON_ONCE (!fscrypt_has_encryption_key (dir))) +return false; + + decrypted_name.name =kmalloc (de_name_len,GFP_KERNEL ); +if (!decrypted_name.name) +return false; + res =fscrypt_fname_disk_to_usr (dir,0 ,0 , &encrypted_name, + &decrypted_name); +if (res <0 ) + goto out; + entry.name = decrypted_name.name; + entry.len = decrypted_name.len; + } + res =utf8_strncasecmp_folded (um, name, &entry);if (res <0 ) {/*
@@ -228,9 +248,12 @@ static bool f2fs_match_ci_name(const struct inode *dir, const struct qstr *name,* fall back to treating them as opaque byte sequences. */ if (sb_has_strict_encoding (sb) || name->len != entry.len) -return false; - return !memcmp(name->name, entry.name, name->len); + res =1 ; +else + res = memcmp(name->name, entry.name, name->len); } +out: +kfree (decrypted_name.name);return res ==0 ; }#endif /* CONFIG_UNICODE */
@@ -455,17 +478,39 @@ void f2fs_set_link(struct inode *dir, struct f2fs_dir_entry *de,f2fs_put_page (page,1 ); } -static void init_dent_inode (const struct f2fs_filename *fname, +static void init_dent_inode (struct inode *dir,struct inode *inode, +const struct f2fs_filename *fname,struct page *ipage) {struct f2fs_inode *ri; +if (!fname)/* tmpfile case? */ +return ; +f2fs_wait_on_page_writeback (ipage,NODE , true, true);/* copy name info. to this inode page */ ri =F2FS_INODE (ipage); ri->i_namelen =cpu_to_le32 (fname->disk_name.len);memcpy (ri->i_name, fname->disk_name.name, fname->disk_name.len); +if (IS_ENCRYPTED (dir)) { +file_set_enc_name (inode); +/* +* Roll-forward recovery doesn't have encryption keys available, +* so it can't compute the dirhash for encrypted+casefolded +* filenames. Append it to i_name if possible. Else, disable +* roll-forward recovery of the dentry (i.e., make fsync'ing the +* file force a checkpoint) by setting LOST_PINO. +*/ +if (IS_CASEFOLDED (dir)) { +if (fname->disk_name.len +sizeof (f2fs_hash_t) <= +F2FS_NAME_LEN ) +put_unaligned (fname->hash, (f2fs_hash_t *) + &ri->i_name[fname->disk_name.len]); +else +file_lost_pino (inode); + } + }set_page_dirty (ipage); }
@@ -548,11 +593,7 @@ struct page *f2fs_init_inode_metadata(struct inode *inode, struct inode *dir,return page; } -if (fname) { -init_dent_inode (fname, page); -if (IS_ENCRYPTED (dir)) -file_set_enc_name (inode); - } +init_dent_inode (dir, inode, fname, page);/* * This file should be checkpointed during fsync.
fs/f2fs/f2fs.h +5 -3
@@ -533,9 +533,11 @@ struct f2fs_filename {#ifdef CONFIG_UNICODE /* * For casefolded directories: the casefolded name, but it's left NULL -* if the original name is not valid Unicode or if the filesystem is -* doing an internal operation where usr_fname is also NULL. In these -* cases we fall back to treating the name as an opaque byte sequence. +* if the original name is not valid Unicode, if the directory is both +* casefolded and encrypted and its encryption key is unavailable, or if +* the filesystem is doing an internal operation where usr_fname is also +* NULL. In all these cases we fall back to treating the name as an +* opaque byte sequence. */ struct fscrypt_str cf_name;#endif
fs/f2fs/hash.c +10 -1
@@ -111,7 +111,9 @@ void f2fs_hash_filename(const struct inode *dir, struct f2fs_filename *fname)* If the casefolded name is provided, hash it instead of the * on-disk name. If the casefolded name is *not* provided, that * should only be because the name wasn't valid Unicode, so fall -* back to treating the name as an opaque byte sequence. +* back to treating the name as an opaque byte sequence. Note +* that to handle encrypted directories, the fallback must use +* usr_fname (plaintext) rather than disk_name (ciphertext). */ WARN_ON_ONCE (!fname->usr_fname->name);if (fname->cf_name.name) {
@@ -121,6 +123,13 @@ void f2fs_hash_filename(const struct inode *dir, struct f2fs_filename *fname) name = fname->usr_fname->name; len = fname->usr_fname->len; } +if (IS_ENCRYPTED (dir)) { +struct qstr tmp =QSTR_INIT (name, len); + + fname->hash = +cpu_to_le32 (fscrypt_fname_siphash (dir, &tmp)); +return ; + } }#endif fname->hash =cpu_to_le32 (TEA_hash_name (name, len));
fs/f2fs/recovery.c +11 -1
@@ -5,6 +5,7 @@* Copyright (c) 2012 Samsung Electronics Co., Ltd. * http://www.samsung.com/ */ +#include <asm/unaligned.h> #include <linux/fs.h> #include <linux/f2fs_fs.h> #include "f2fs.h"
@@ -128,7 +129,16 @@ static int init_recovered_filename(const struct inode *dir, }/* Compute the hash of the filename */ -if (IS_CASEFOLDED (dir)) { +if (IS_ENCRYPTED (dir) &&IS_CASEFOLDED (dir)) { +/* +* In this case the hash isn't computable without the key, so it +* was saved on-disk. +*/ +if (fname->disk_name.len +sizeof (f2fs_hash_t) >F2FS_NAME_LEN ) +return -EINVAL ; + fname->hash =get_unaligned ((f2fs_hash_t *) + &raw_inode->i_name[fname->disk_name.len]); + }else if (IS_CASEFOLDED (dir)) { err =f2fs_init_casefolded_name (dir, fname);if (err)return err;
fs/f2fs/super.c +0 -6
@@ -3382,12 +3382,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.28.0.681.g6f77f65b4e-goog
Re: [PATCH 5/5] f2fs: Handle casefolding with Encryption
Quoted 11 lines · Daniel Rosenberg
Expand f2fs's casefolding support to include encrypted directories. To index casefolded+encrypted directories, we use the SipHash of the casefolded name, keyed by a key derived from the directory's fscrypt master key. This ensures that the dirhash doesn't leak information about the plaintext filenames.
Encryption keys are unavailable during roll-forward recovery, so we can't compute the dirhash when recovering a new dentry in an encrypted + casefolded directory. To avoid having to force a checkpoint when a new file is fsync'ed, store the dirhash on-disk appended to
i_name.
Quoted 7 lines
This patch incorporates work by Eric Biggers <[email protected]> and Jaegeuk Kim <[email protected]>.
Co-developed-by Eric Biggers <[email protected]>Signed-off-by Eric Biggers <[email protected]>Signed-off-by Daniel Rosenberg <[email protected]>---
Generally looks good. If it's needed, you can add:
(Though, some may claim I can't give Reviewed-by since this patch already has my Co-developed-by.)
One comment below, though:
Quoted 12 lines
fs/f2fs/dir.c +5
-0@@ -218,9 +219,28 @@ static bool f2fs_match_ci_name(const struct inode *dir, const struct qstr *name, {const struct super_block *sb = dir->i_sb;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 res; +if (IS_ENCRYPTED (dir)) { +const struct fscrypt_str encrypted_name = +FSTR_INIT ((u8 *)de_name, de_name_len); +
fs/f2fs/dir.c static bool f2fs_match_ci_name(const struct inode *dir, const struct qstr *name, · line 239
Quoted 10 lines
fs/f2fs/dir.c +10
-0+if (WARN_ON_ONCE (!fscrypt_has_encryption_key (dir))) +return false; + + decrypted_name.name =kmalloc (de_name_len,GFP_KERNEL ); +if (!decrypted_name.name) +return false; + res =fscrypt_fname_disk_to_usr (dir,0 ,0 , &encrypted_name, + &decrypted_name); +if (res <0 ) + goto out;
We probably should be passing up errors from here to f2fs_match_name(), then to f2fs_find_target_dentry(), then to f2fs_find_in_inline_dir() or find_in_block().
Ignoring the filename may be okay if fscrypt_fname_disk_to_usr() returns -EUCLEAN, indicating that it's invalid. However, if the error is -ENOMEM, either from the kmalloc() or from fscrypt_fname_disk_to_usr(), then the caller should receive an error rather than the filename being ignored.
- Eric
Eric Biggers
maintainerQuoted 18 lines · Daniel Rosenberg
These patches are on top of the f2fs dev branch
F2FS currently supports casefolding and encryption, but not at the same time. These patches aim to rectify that. In a later follow up, this will be added for Ext4 as well. I've included one ext4 patch from the previous set since it isn't in the f2fs branch, but is needed for the fscrypt changes.
The f2fs-tools changes have already been applied.
Since both fscrypt and casefolding require their own dentry operations, I've moved the responsibility of setting the dentry operations from fscrypt to the filesystems and provided helper functions that should work for most cases.
These are a follow-up to the previously sent patch set "[PATCH v12 0/4] Prepare for upcoming Casefolding/Encryption patches"
Quoted 6 lines
Daniel Rosenberg (5):
ext4: Use generic casefolding support
fscrypt: Exportfscrypt_d_revalidate
libfs: Add generic function for settingdentry_ops
fscrypt: Have filesystems handle theird_ops
f2fs: Handle casefolding with Encryption
Daniel, can you resend this for 5.11, with all remaining comments addressed? The first two patches made 5.10, but the others didn't.
- Eric