fscrypt: add some higher-level helper functions
Subsystems (6)
v2 cover fscrypt: add some higher-level helper functions
From: Eric Biggers <[email protected]>
This series prepares to reduce code duplication among ext4, f2fs, and ubifs by introducing a S_ENCRYPTED inode flag (so we don't have to call back into the filesystem to test the filesystem-specific inode flag), then introducing new helper functions that are called at the beginning of the open, link, rename, lookup, and setattr operations.
In the future we maybe should even call these new helpers from the VFS so that each individual filesystem doesn't have to do it. But that's not possible currently because fs/crypto/ can be built as a module.
The patches to switch the filesystems over to use the helper functions were included in v1 of this patchset. They are not included now since I'm planning to get them picked up by the individual filesystem maintainers after this goes in.
Changes since v1:
- Added Dave Chinner's patch to make fscrypt.h include fscrypt_supp.h or fscrypt_notsupp.h, then reorganized the helpers so that most are defined in fscrypt.h.
- As noted above, for now omitted the patches to switch the individual filesystems over to the new helpers.
Dave Chinner (1):
fscrypt: clean up include file mess
Eric Biggers (10):
fs, fscrypt: add an S_ENCRYPTED inode flag
fscrypt: switch from ->is_encrypted() to IS_ENCRYPTED()
fscrypt: remove ->is_encrypted()
fscrypt: remove unneeded empty fscrypt_operations structs
fscrypt: new helper function - fscrypt_require_key()
fscrypt: new helper function - fscrypt_file_open()
fscrypt: new helper function - fscrypt_prepare_link()
fscrypt: new helper function - fscrypt_prepare_rename()
fscrypt: new helper function - fscrypt_prepare_lookup()
fscrypt: new helper function - fscrypt_prepare_setattr()
fs/crypto/Makefile | 2 +- fs/crypto/crypto.c | 2 +- fs/crypto/fname.c | 3 +- fs/crypto/fscrypt_private.h | 3 +- fs/crypto/hooks.c | 112 +++++++++++++++ fs/crypto/keyinfo.c | 2 +- fs/crypto/policy.c | 6 +- fs/ext4/ext4.h | 8 +- fs/ext4/inode.c | 7 +- fs/ext4/super.c | 15 +- fs/f2fs/f2fs.h | 9 +- fs/f2fs/inode.c | 5 +- fs/f2fs/super.c | 7 +- fs/ubifs/crypto.c | 1 - fs/ubifs/ioctl.c | 5 +- fs/ubifs/super.c | 8 +- fs/ubifs/ubifs.h | 18 +-- fs/ubifs/xattr.c | 1 + include/linux/fs.h | 2 + include/linux/fscrypt.h | 293 ++++++++++++++++++++++++++++++++++++++++ include/linux/fscrypt_common.h | 141 ------------------- include/linux/fscrypt_notsupp.h | 39 +++++- include/linux/fscrypt_supp.h | 17 ++- 23 files changed, 505 insertions(+), 201 deletions(-) create mode 100644 fs/crypto/hooks.c create mode 100644 include/linux/fscrypt.h delete mode 100644 include/linux/fscrypt_common.h
Signature · 1 line
2.14.2.920.gcf0c67979c-goog
PATCH v2 1/11 fscrypt: clean up include file mess
From: Dave Chinner <[email protected]>
Filesystems have to include different header files based on whether they are compiled with encryption support or not. That's nasty and messy.
Instead, rationalise the headers so we have a single include fscrypt.h and let it decide what internal implementation to include based on the __FS_HAS_ENCRYPTION define. Filesystems set __FS_HAS_ENCRYPTION to 1 before including linux/fscrypt.h if they are built with encryption support. Otherwise, they must set __FS_HAS_ENCRYPTION to 0.
Add guards to prevent fscrypt_supp.h and fscrypt_notsupp.h from being directly included by filesystems.
---
fs/crypto/fscrypt_private.h | 3 +-
fs/ext4/ext4.h | 8 ++----
fs/f2fs/f2fs.h | 8 ++----
fs/ubifs/ubifs.h | 9 +++---
include/linux/{fscrypt_common.h => fscrypt.h} | 41 ++++++++++++++++++---------
include/linux/fscrypt_notsupp.h | 7 +++--
include/linux/fscrypt_supp.h | 7 +++--
7 files changed, 49 insertions(+), 34 deletions(-)
rename include/linux/{fscrypt_common.h => fscrypt.h} (80%)fs/crypto/fscrypt_private.h +2 -1
@@ -11,7 +11,8 @@#ifndef _FSCRYPT_PRIVATE_H#define _FSCRYPT_PRIVATE_H -#include <linux/fscrypt_supp.h> +#define __FS_HAS_ENCRYPTION 1 +#include <linux/fscrypt.h> #include <crypto/hash.h> /* Encryption parameters */
fs/ext4/ext4.h +3 -5
@@ -33,17 +33,15 @@#include <linux/percpu_counter.h> #include <linux/ratelimit.h> #include <crypto/hash.h> -#ifdef CONFIG_EXT4_FS_ENCRYPTION -#include <linux/fscrypt_supp.h> -#else -#include <linux/fscrypt_notsupp.h> -#endif #include <linux/falloc.h> #include <linux/percpu-rwsem.h> #ifdef __KERNEL__#include <linux/compat.h> #endif +#define __FS_HAS_ENCRYPTION IS_ENABLED(CONFIG_EXT4_FS_ENCRYPTION) +#include <linux/fscrypt.h> +/* * The fourth extended filesystem constants/structures */
fs/f2fs/f2fs.h +3 -5
@@ -23,13 +23,11 @@#include <linux/bio.h> #include <linux/blkdev.h> #include <linux/quotaops.h> -#ifdef CONFIG_F2FS_FS_ENCRYPTION -#include <linux/fscrypt_supp.h> -#else -#include <linux/fscrypt_notsupp.h> -#endif #include <crypto/hash.h> +#define __FS_HAS_ENCRYPTION IS_ENABLED(CONFIG_F2FS_FS_ENCRYPTION) +#include <linux/fscrypt.h> +#ifdef CONFIG_F2FS_CHECK_FS #define f2fs_bug_on (sbi, condition) BUG_ON(condition)#else
fs/ubifs/ubifs.h +4 -5
@@ -38,12 +38,11 @@#include <linux/backing-dev.h> #include <linux/security.h> #include <linux/xattr.h> -#ifdef CONFIG_UBIFS_FS_ENCRYPTION -#include <linux/fscrypt_supp.h> -#else -#include <linux/fscrypt_notsupp.h> -#endif #include <linux/random.h> + +#define __FS_HAS_ENCRYPTION IS_ENABLED(CONFIG_UBIFS_FS_ENCRYPTION) +#include <linux/fscrypt.h> +#include "ubifs-media.h" /* Version of this UBIFS implementation */
include/linux/fscrypt.h renamed from include/linux/fscrypt_common.h +28 -13
65 lines not on this page · raw message
include/linux/fscrypt_notsupp.h +5 -2
@@ -3,13 +3,16 @@* * This stubs out the fscrypt functions for filesystems configured without * encryption support. +* +* Do not include this file directly. Use fscrypt.h instead! */ +#ifndef _LINUX_FSCRYPT_H +#error "Incorrect include of linux/fscrypt_notsupp.h!" +#endif #ifndef _LINUX_FSCRYPT_NOTSUPP_H#define _LINUX_FSCRYPT_NOTSUPP_H -#include <linux/fscrypt_common.h> -/* crypto.c */ static inline struct fscrypt_ctx *fscrypt_get_ctx (const struct inode *inode,gfp_t gfp_flags)
include/linux/fscrypt_supp.h +4 -3
@@ -1,14 +1,15 @@/* * fscrypt_supp.h * -* This is included by filesystems configured with encryption support. +* Do not include this file directly. Use fscrypt.h instead! */ +#ifndef _LINUX_FSCRYPT_H +#error "Incorrect include of linux/fscrypt_supp.h!" +#endif #ifndef _LINUX_FSCRYPT_SUPP_H#define _LINUX_FSCRYPT_SUPP_H -#include <linux/fscrypt_common.h> -/* crypto.c */ extern struct kmem_cache *fscrypt_info_cachep;extern struct fscrypt_ctx *fscrypt_get_ctx (const struct inode *,gfp_t );
Signature · 1 line
2.14.2.920.gcf0c67979c-goog
PATCH v2 2/11 fs, fscrypt: add an S_ENCRYPTED inode flag
In reply to Eric BiggersFrom: Eric Biggers <[email protected]>
Introduce a flag S_ENCRYPTED which can be set in ->i_flags to indicate that the inode is encrypted using the fscrypt (fs/crypto/) mechanism.
Checking this flag will give the same information that inode->i_sb->s_cop->is_encrypted(inode) currently does, but will be more efficient. This will be useful for adding higher-level helper functions for filesystems to use. For example we'll be able to replace this:
if (ext4_encrypted_inode(inode)) {
ret = fscrypt_get_encryption_info(inode);
if (ret)
return ret;
if (!fscrypt_has_encryption_key(inode))
return -ENOKEY;
}with this:
ret = fscrypt_require_key(inode); if (ret) return ret;
... since we'll be able to retain the fast path for unencrypted files as a single flag check, using an inline function. This wasn't possible before because we'd have had to frequently call through the ->i_sb->s_cop->is_encrypted function pointer, even when the encryption support was disabled or not being used.
Note: we don't define S_ENCRYPTED to 0 if CONFIG_FS_ENCRYPTION is disabled because we want to continue to return an error if an encrypted file is accessed without encryption support, rather than pretending that it is unencrypted.
--- fs/ext4/inode.c | 7 +++++-- fs/ext4/super.c | 8 ++++++-- fs/f2fs/f2fs.h | 1 + fs/f2fs/inode.c | 5 ++++- fs/ubifs/ioctl.c | 5 ++++- fs/ubifs/xattr.c | 1 + include/linux/fs.h | 2 ++ 7 files changed, 23 insertions(+), 6 deletions(-)
fs/ext4/inode.c +5 -2
@@ -4589,10 +4589,13 @@ void ext4_set_inode_flags(struct inode *inode) new_fl |=S_DIRSYNC ;if (test_opt (inode->i_sb,DAX ) &&S_ISREG (inode->i_mode) && !ext4_should_journal_data (inode) && !ext4_has_inline_data (inode) && - !ext4_encrypted_inode (inode)) + !(flags &EXT4_ENCRYPT_FL )) new_fl |=S_DAX ; +if (flags &EXT4_ENCRYPT_FL ) + new_fl |=S_ENCRYPTED ;inode_set_flags (inode, new_fl, -S_SYNC |S_APPEND |S_IMMUTABLE |S_NOATIME |S_DIRSYNC |S_DAX ); +S_SYNC |S_APPEND |S_IMMUTABLE |S_NOATIME |S_DIRSYNC |S_DAX | +S_ENCRYPTED ); } static blkcnt_t ext4_inode_blocks(struct ext4_inode *raw_inode,
fs/ext4/super.c +6 -2
@@ -1181,7 +1181,8 @@ static int ext4_set_context(struct inode *inode, const void *ctx, size_t len,ext4_clear_inode_state (inode,EXT4_STATE_MAY_INLINE_DATA );/* -* Update inode->i_flags - e.g. S_DAX may get disabled +* Update inode->i_flags - S_ENCRYPTED will be enabled, +* S_DAX may be disabled */ ext4_set_inode_flags (inode); }
@@ -1206,7 +1207,10 @@ static int ext4_set_context(struct inode *inode, const void *ctx, size_t len, ctx, len, 0);if (!res) {ext4_set_inode_flag (inode,EXT4_INODE_ENCRYPT ); -/* Update inode->i_flags - e.g. S_DAX may get disabled */ +/* +* Update inode->i_flags - S_ENCRYPTED will be enabled, +* S_DAX may be disabled +*/ ext4_set_inode_flags (inode); res =ext4_mark_inode_dirty (handle, inode);if (res)
fs/f2fs/f2fs.h +1 -0
@@ -2947,6 +2947,7 @@ static inline void f2fs_set_encrypted_inode(struct inode *inode) {#ifdef CONFIG_F2FS_FS_ENCRYPTION file_set_encrypt (inode); + inode->i_flags |=S_ENCRYPTED ;#endif }
fs/f2fs/inode.c +4 -1
@@ -43,8 +43,11 @@ void f2fs_set_inode_flags(struct inode *inode) new_fl |=S_NOATIME ;if (flags &FS_DIRSYNC_FL ) new_fl |=S_DIRSYNC ; +if (f2fs_encrypted_inode (inode)) + new_fl |=S_ENCRYPTED ;inode_set_flags (inode, new_fl, -S_SYNC |S_APPEND |S_IMMUTABLE |S_NOATIME |S_DIRSYNC ); +S_SYNC |S_APPEND |S_IMMUTABLE |S_NOATIME |S_DIRSYNC | +S_ENCRYPTED ); }static void __get_inode_rdev (struct inode *inode,struct f2fs_inode *ri)
fs/ubifs/ioctl.c +4 -1
@@ -38,7 +38,8 @@ void ubifs_set_inode_flags(struct inode *inode) {unsigned int flags =ubifs_inode (inode)->flags; - inode->i_flags &= ~(S_SYNC |S_APPEND |S_IMMUTABLE |S_DIRSYNC ); + inode->i_flags &= ~(S_SYNC |S_APPEND |S_IMMUTABLE |S_DIRSYNC | +S_ENCRYPTED );if (flags &UBIFS_SYNC_FL ) inode->i_flags |=S_SYNC ;if (flags &UBIFS_APPEND_FL )
@@ -47,6 +48,8 @@ void ubifs_set_inode_flags(struct inode *inode) inode->i_flags |=S_IMMUTABLE ;if (flags &UBIFS_DIRSYNC_FL ) inode->i_flags |=S_DIRSYNC ; +if (flags &UBIFS_CRYPT_FL ) + inode->i_flags |=S_ENCRYPTED ; }/*
fs/ubifs/xattr.c +1 -0
@@ -170,6 +170,7 @@ static int create_xattr(struct ubifs_info *c, struct inode *host, err =ubifs_jnl_update (c, host, nm, inode,0 ,1 );if (err) goto out_cancel; +ubifs_set_inode_flags (host);mutex_unlock (&host_ui->ui_mutex);ubifs_release_budget (c, &req);
include/linux/fs.h +2 -0
@@ -1853,6 +1853,7 @@ struct super_operations {#else #define S_DAX 0/* Make all the DAX code disappear */ #endif +#define S_ENCRYPTED 16384/* Encrypted file (using fs/crypto/) */ /* * Note that nosuid etc flags are inode-specific: setting some file-system
@@ -1892,6 +1893,7 @@ static inline bool sb_rdonly(const struct super_block *sb) { return sb->s_flags#define IS_AUTOMOUNT (inode) ((inode)->i_flags & S_AUTOMOUNT)#define IS_NOSEC (inode) ((inode)->i_flags & S_NOSEC)#define IS_DAX (inode) ((inode)->i_flags & S_DAX) +#define IS_ENCRYPTED (inode) ((inode)->i_flags & S_ENCRYPTED)#define IS_WHITEOUT (inode) (S_ISCHR(inode->i_mode) && \ (inode)->i_rdev == WHITEOUT_DEV)
Signature · 6 lines
2.14.2.920.gcf0c67979c-goog ------------------------------------------------------------------------------ Check out the vibrant tech community on one of the world's most engaging tech sites, Slashdot.org! http://sdm.link/slashdot
PATCH v2 3/11 fscrypt: switch from ->is_encrypted() to IS_ENCRYPTED()
In reply to Eric BiggersFrom: Eric Biggers <[email protected]>
IS_ENCRYPTED() now gives the same information as i_sb->s_cop->is_encrypted() but is more efficient, since IS_ENCRYPTED() is just a simple flag check. Prepare to remove ->is_encrypted() by switching all callers to IS_ENCRYPTED().
--- fs/crypto/crypto.c | 2 +- fs/crypto/fname.c | 3 +-- fs/crypto/keyinfo.c | 2 +- fs/crypto/policy.c | 6 +++--- include/linux/fscrypt_notsupp.h | 2 +- 5 files changed, 7 insertions(+), 8 deletions(-)
fs/crypto/crypto.c +1 -1
@@ -340,7 +340,7 @@ static int fscrypt_d_revalidate(struct dentry *dentry, unsigned int flags)return -ECHILD ; dir =dget_parent (dentry); -if (!d_inode (dir)->i_sb->s_cop->is_encrypted (d_inode (dir))) { +if (!IS_ENCRYPTED (d_inode (dir))) {dput (dir);return 0 ; }
fs/crypto/fname.c +1 -2
@@ -382,8 +382,7 @@ int fscrypt_setup_filename(struct inode *dir, const struct qstr *iname,memset (fname,0 ,sizeof (struct fscrypt_name )); fname->usr_fname = iname; -if (!dir->i_sb->s_cop->is_encrypted (dir) || -fscrypt_is_dot_dotdot (iname)) { +if (!IS_ENCRYPTED (dir) ||fscrypt_is_dot_dotdot (iname)) { fname->disk_name.name = (unsigned char *)iname->name; fname->disk_name.len = iname->len;return 0 ;
fs/crypto/keyinfo.c +1 -1
@@ -268,7 +268,7 @@ int fscrypt_get_encryption_info(struct inode *inode) res = inode->i_sb->s_cop->get_context (inode, &ctx,sizeof (ctx));if (res <0 ) {if (!fscrypt_dummy_context_enabled (inode) || - inode->i_sb->s_cop->is_encrypted (inode)) +IS_ENCRYPTED (inode))return res;/* Fake up a context for an unencrypted directory */ memset (&ctx,0 ,sizeof (ctx));
fs/crypto/policy.c +3 -3
@@ -109,7 +109,7 @@ int fscrypt_ioctl_get_policy(struct file *filp, void __user *arg)struct fscrypt_policy policy;int res; -if (!inode->i_sb->s_cop->is_encrypted (inode)) +if (!IS_ENCRYPTED (inode))return -ENODATA ; res = inode->i_sb->s_cop->get_context (inode, &ctx,sizeof (ctx));
@@ -166,11 +166,11 @@ int fscrypt_has_permitted_context(struct inode *parent, struct inode *child)return 1 ;/* No restrictions if the parent directory is unencrypted */ -if (!cops->is_encrypted (parent)) +if (!IS_ENCRYPTED (parent))return 1 ;/* Encrypted directories must not contain unencrypted files */ -if (!cops->is_encrypted (child)) +if (!IS_ENCRYPTED (child))return 0 ;/*
include/linux/fscrypt_notsupp.h +1 -1
@@ -100,7 +100,7 @@ static inline int fscrypt_setup_filename(struct inode *dir,const struct qstr *iname, int lookup, struct fscrypt_name *fname) { - if (dir->i_sb->s_cop->is_encrypted(dir)) + if (IS_ENCRYPTED(dir)) return -EOPNOTSUPP; memset(fname, 0, sizeof(struct fscrypt_name));
Signature · 1 line
2.14.2.920.gcf0c67979c-goog
PATCH v2 4/11 fscrypt: remove ->is_encrypted()
In reply to Eric BiggersFrom: Eric Biggers <[email protected]>
Now that all callers of fscrypt_operations.is_encrypted() have been switched to IS_ENCRYPTED(), remove ->is_encrypted().
--- fs/ext4/super.c | 2 -- fs/f2fs/super.c | 2 -- fs/ubifs/crypto.c | 1 - fs/ubifs/super.c | 1 - fs/ubifs/ubifs.h | 9 ++------- include/linux/fscrypt.h | 1 - 6 files changed, 2 insertions(+), 14 deletions(-)
fs/ext4/super.c +0 -2
@@ -1241,13 +1241,11 @@ static const struct fscrypt_operations ext4_cryptops = { .get_context = ext4_get_context, .set_context = ext4_set_context, .dummy_context = ext4_dummy_context, - .is_encrypted = ext4_encrypted_inode, .empty_dir = ext4_empty_dir, .max_namelen = ext4_max_namelen, };#else static const struct fscrypt_operations ext4_cryptops = { - .is_encrypted = ext4_encrypted_inode, };#endif
fs/f2fs/super.c +0 -2
@@ -1594,13 +1594,11 @@ static const struct fscrypt_operations f2fs_cryptops = { .key_prefix = "f2fs:", .get_context = f2fs_get_context, .set_context = f2fs_set_context, - .is_encrypted = f2fs_encrypted_inode, .empty_dir = f2fs_empty_dir, .max_namelen = f2fs_max_namelen, };#else static const struct fscrypt_operations f2fs_cryptops = { - .is_encrypted = f2fs_encrypted_inode, };#endif
fs/ubifs/crypto.c +0 -1
@@ -87,7 +87,6 @@ const struct fscrypt_operations ubifs_crypt_operations = { .key_prefix = "ubifs:", .get_context = ubifs_crypt_get_context, .set_context = ubifs_crypt_set_context, - .is_encrypted = __ubifs_crypt_is_encrypted, .empty_dir = ubifs_crypt_empty_dir, .max_namelen = ubifs_crypt_max_namelen, };
fs/ubifs/super.c +0 -1
@@ -2009,7 +2009,6 @@ static struct ubifs_info *alloc_ubifs_info(struct ubi_volume_desc *ubi)#ifndef CONFIG_UBIFS_FS_ENCRYPTION const struct fscrypt_operations ubifs_crypt_operations = { - .is_encrypted = __ubifs_crypt_is_encrypted, };#endif
fs/ubifs/ubifs.h +2 -7
@@ -1834,18 +1834,13 @@ int ubifs_decrypt(const struct inode *inode, struct ubifs_data_node *dn,extern const struct fscrypt_operations ubifs_crypt_operations; -static inline bool __ubifs_crypt_is_encrypted (struct inode *inode) +static inline bool ubifs_crypt_is_encrypted (const struct inode *inode) { -struct ubifs_inode *ui =ubifs_inode (inode); +const struct ubifs_inode *ui =ubifs_inode (inode);return ui->flags &UBIFS_CRYPT_FL ; } -static inline bool ubifs_crypt_is_encrypted (const struct inode *inode) -{ -return __ubifs_crypt_is_encrypted ((struct inode *)inode); -} -/* Normal UBIFS messages */ __printf(2, 3)void ubifs_msg (const struct ubifs_info *c,const char *fmt, ...);
include/linux/fscrypt.h +0 -1
@@ -81,7 +81,6 @@ struct fscrypt_operations {int (*get_context)(struct inode *,void *,size_t );int (*set_context)(struct inode *,const void *,size_t ,void *);bool (*dummy_context)(struct inode *); -bool (*is_encrypted)(struct inode *);bool (*empty_dir)(struct inode *);unsigned (*max_namelen)(struct inode *); };
Signature · 1 line
2.14.2.920.gcf0c67979c-goog
PATCH v2 5/11 fscrypt: remove unneeded empty fscrypt_operations structs
In reply to Eric BiggersFrom: Eric Biggers <[email protected]>
In the case where a filesystem has been configured without encryption support, there is no longer any need to initialize ->s_cop at all, since none of the methods are ever called.
--- fs/ext4/super.c | 5 ++--- fs/f2fs/super.c | 5 ++--- fs/ubifs/super.c | 7 ++----- 3 files changed, 6 insertions(+), 11 deletions(-)
fs/ext4/super.c +2 -3
@@ -1244,9 +1244,6 @@ static const struct fscrypt_operations ext4_cryptops = { .empty_dir = ext4_empty_dir, .max_namelen = ext4_max_namelen, }; -#else -static const struct fscrypt_operations ext4_cryptops = { -};#endif #ifdef CONFIG_QUOTA
@@ -3998,7 +3995,9 @@ static int ext4_fill_super(struct super_block *sb, void *data, int silent) sb->s_op = &ext4_sops; sb->s_export_op = &ext4_export_ops; sb->s_xattr = ext4_xattr_handlers; +#ifdef CONFIG_EXT4_FS_ENCRYPTION sb->s_cop = &ext4_cryptops; +#endif #ifdef CONFIG_QUOTA sb->dq_op = &ext4_quota_operations;if (ext4_has_feature_quota (sb))
fs/f2fs/super.c +2 -3
@@ -1597,9 +1597,6 @@ static const struct fscrypt_operations f2fs_cryptops = { .empty_dir = f2fs_empty_dir, .max_namelen = f2fs_max_namelen, }; -#else -static const struct fscrypt_operations f2fs_cryptops = { -};#endif static struct inode *f2fs_nfs_get_inode(struct super_block *sb,
@@ -2318,7 +2315,9 @@ static int f2fs_fill_super(struct super_block *sb, void *data, int silent)#endif sb->s_op = &f2fs_sops; +#ifdef CONFIG_F2FS_FS_ENCRYPTION sb->s_cop = &f2fs_cryptops; +#endif sb->s_xattr = f2fs_xattr_handlers; sb->s_export_op = &f2fs_export_ops; sb->s_magic =F2FS_SUPER_MAGIC ;
fs/ubifs/super.c +2 -5
@@ -2007,11 +2007,6 @@ static struct ubifs_info *alloc_ubifs_info(struct ubi_volume_desc *ubi)return c; } -#ifndef CONFIG_UBIFS_FS_ENCRYPTION -const struct fscrypt_operations ubifs_crypt_operations = { -}; -#endif -static int ubifs_fill_super (struct super_block *sb,void *data,int silent) {struct ubifs_info *c = sb->s_fs_info;
@@ -2054,7 +2049,9 @@ static int ubifs_fill_super(struct super_block *sb, void *data, int silent) sb->s_maxbytes = c->max_inode_sz =MAX_LFS_FILESIZE ; sb->s_op = &ubifs_super_operations; sb->s_xattr = ubifs_xattr_handlers; +#ifdef CONFIG_UBIFS_FS_ENCRYPTION sb->s_cop = &ubifs_crypt_operations; +#endif mutex_lock (&c->umount_mutex); err =mount_ubifs (c);
Signature · 1 line
2.14.2.920.gcf0c67979c-goog
PATCH v2 6/11 fscrypt: new helper function - fscrypt_require_key()
In reply to Eric BiggersFrom: Eric Biggers <[email protected]>
Add a helper function which checks if an inode is encrypted, and if so, tries to set up its encryption key. This is a pattern which is duplicated in multiple places in each of ext4, f2fs, and ubifs --- for example, when a regular file is asked to be opened or truncated.
--- include/linux/fscrypt.h | 25 +++++++++++++++++++++++++ 1 file changed, 25 insertions(+)
include/linux/fscrypt.h +25 -0
@@ -151,5 +151,30 @@ static inline bool fscrypt_has_encryption_key(const struct inode *inode)#include <linux/fscrypt_notsupp.h> #endif /* __FS_HAS_ENCRYPTION */ +/** +* fscrypt_require_key - require an inode's encryption key +* @inode: the inode we need the key for +* +* If the inode is encrypted, set up its encryption key if not already done. +* Then require that the key be present and return -ENOKEY otherwise. +* +* No locks are needed, and the key will live as long as the struct inode --- so +* it won't go away from under you. +* +* Return: 0 on success, -ENOKEY if the key is missing, or another -errno code +* if a problem occurred while setting up the encryption key. +*/ +static inline int fscrypt_require_key (struct inode *inode) +{ +if (IS_ENCRYPTED (inode)) { +int err =fscrypt_get_encryption_info (inode); + +if (err) +return err; +if (!fscrypt_has_encryption_key (inode)) +return -ENOKEY ; + } +return 0 ; +}#endif /* _LINUX_FSCRYPT_H */
Signature · 1 line
2.14.2.920.gcf0c67979c-goog
PATCH v2 7/11 fscrypt: new helper function - fscrypt_file_open()
In reply to Eric BiggersFrom: Eric Biggers <[email protected]>
Add a helper function which prepares to open a regular file which may be encrypted. It handles setting up the file's encryption key, then checking that the file's encryption policy matches that of its parent directory (if the parent directory is encrypted). It may be set as the ->open() method or it can be called from another ->open() method.
--- fs/crypto/Makefile | 2 +- fs/crypto/hooks.c | 49 +++++++++++++++++++++++++++++++++++++++++ include/linux/fscrypt_notsupp.h | 9 ++++++++ include/linux/fscrypt_supp.h | 3 +++ 4 files changed, 62 insertions(+), 1 deletion(-) create mode 100644 fs/crypto/hooks.c
fs/crypto/Makefile +1 -1
@@ -1,4 +1,4 @@ obj-$(CONFIG_FS_ENCRYPTION) += fscrypto.o -fscrypto-y := crypto.o fname.o policy.o keyinfo.o +fscrypto-y := crypto.o fname.o hooks.o keyinfo.o policy.o fscrypto-$(CONFIG_BLOCK) += bio.o
fs/crypto/hooks.c new file +49 -0
49 lines not on this page · raw message
include/linux/fscrypt_notsupp.h +9 -0
@@ -177,4 +177,13 @@ static inline int fscrypt_zeroout_range(const struct inode *inode, pgoff_t lblk, return -EOPNOTSUPP; } +/* hooks.c */ + +static inline int fscrypt_file_open (struct inode *inode,struct file *filp) +{ +if (IS_ENCRYPTED (inode)) +return -EOPNOTSUPP ; +return 0 ; +} +#endif /* _LINUX_FSCRYPT_NOTSUPP_H */
include/linux/fscrypt_supp.h +3 -0
@@ -143,4 +143,7 @@ extern void fscrypt_pullback_bio_page(struct page **, bool);extern int fscrypt_zeroout_range (const struct inode *,pgoff_t ,sector_t ,unsigned int ); +/* hooks.c */ +extern int fscrypt_file_open (struct inode *inode,struct file *filp); +#endif /* _LINUX_FSCRYPT_SUPP_H */
Signature · 6 lines
2.14.2.920.gcf0c67979c-goog ------------------------------------------------------------------------------ Check out the vibrant tech community on one of the world's most engaging tech sites, Slashdot.org! http://sdm.link/slashdot
PATCH v2 8/11 fscrypt: new helper function - fscrypt_prepare_link()
In reply to Eric BiggersFrom: Eric Biggers <[email protected]>
Introduce a helper function which prepares to link an inode into a possibly-encrypted directory. It handles setting up the target directory's encryption key, then verifying that the link won't violate the constraint that all files in an encrypted directory tree use the same encryption policy.
--- fs/crypto/hooks.c | 15 +++++++++++++++ include/linux/fscrypt.h | 27 +++++++++++++++++++++++++++ include/linux/fscrypt_notsupp.h | 6 ++++++ include/linux/fscrypt_supp.h | 1 + 4 files changed, 49 insertions(+)
fs/crypto/hooks.c +15 -0
@@ -47,3 +47,18 @@ int fscrypt_file_open(struct inode *inode, struct file *filp)return err; }EXPORT_SYMBOL_GPL (fscrypt_file_open); + +int __fscrypt_prepare_link (struct inode *inode,struct inode *dir) +{ +int err; + + err =fscrypt_require_key (dir); +if (err) +return err; + +if (!fscrypt_has_permitted_context (dir, inode)) +return -EPERM ; + +return 0 ; +} +EXPORT_SYMBOL_GPL (__fscrypt_prepare_link);
include/linux/fscrypt.h +27 -0
@@ -177,4 +177,31 @@ static inline int fscrypt_require_key(struct inode *inode) return 0; } +/** +* fscrypt_prepare_link - prepare to link an inode into a possibly-encrypted directory +* @old_dentry: an existing dentry for the inode being linked +* @dir: the target directory +* @dentry: negative dentry for the target filename +* +* A new link can only be added to an encrypted directory if the directory's +* encryption key is available --- since otherwise we'd have no way to encrypt +* the filename. Therefore, we first set up the directory's encryption key (if +* not already done) and return an error if it's unavailable. +* +* We also verify that the link will not violate the constraint that all files +* in an encrypted directory tree use the same encryption policy. +* +* Return: 0 on success, -ENOKEY if the directory's encryption key is missing, +* -EPERM if the link would result in an inconsistent encryption policy, or +* another -errno code. +*/ +static inline int fscrypt_prepare_link (struct dentry *old_dentry, +struct inode *dir, +struct dentry *dentry) +{ +if (IS_ENCRYPTED (dir)) +return __fscrypt_prepare_link (d_inode (old_dentry), dir); +return 0 ; +} +#endif /* _LINUX_FSCRYPT_H */
include/linux/fscrypt_notsupp.h +6 -0
@@ -186,4 +186,10 @@ static inline int fscrypt_file_open(struct inode *inode, struct file *filp) return 0; } +static inline int __fscrypt_prepare_link (struct inode *inode, +struct inode *dir) +{ +return -EOPNOTSUPP ; +} +#endif /* _LINUX_FSCRYPT_NOTSUPP_H */
include/linux/fscrypt_supp.h +1 -0
@@ -145,5 +145,6 @@ extern int fscrypt_zeroout_range(const struct inode *, pgoff_t, sector_t,/* hooks.c */ extern int fscrypt_file_open (struct inode *inode,struct file *filp); +extern int __fscrypt_prepare_link (struct inode *inode,struct inode *dir);#endif /* _LINUX_FSCRYPT_SUPP_H */
Signature · 6 lines
2.14.2.920.gcf0c67979c-goog ------------------------------------------------------------------------------ Check out the vibrant tech community on one of the world's most engaging tech sites, Slashdot.org! http://sdm.link/slashdot
PATCH v2 9/11 fscrypt: new helper function - fscrypt_prepare_rename()
In reply to Eric BiggersFrom: Eric Biggers <[email protected]>
Introduce a helper function which prepares to rename a file into a possibly encrypted directory. It handles loading the encryption keys for the source and target directories if needed, and it handles enforcing that if the target directory (and the source directory for a cross-rename) is encrypted, then the file being moved into the directory has the same encryption policy as its containing directory.
--- fs/crypto/hooks.c | 30 ++++++++++++++++++++++++++++++ include/linux/fscrypt.h | 33 +++++++++++++++++++++++++++++++++ include/linux/fscrypt_notsupp.h | 9 +++++++++ include/linux/fscrypt_supp.h | 5 +++++ 4 files changed, 77 insertions(+)
fs/crypto/hooks.c +30 -0
@@ -62,3 +62,33 @@ int __fscrypt_prepare_link(struct inode *inode, struct inode *dir)return 0 ; }EXPORT_SYMBOL_GPL (__fscrypt_prepare_link); + +int __fscrypt_prepare_rename (struct inode *old_dir,struct dentry *old_dentry, +struct inode *new_dir,struct dentry *new_dentry, +unsigned int flags) +{ +int err; + + err =fscrypt_require_key (old_dir); +if (err) +return err; + + err =fscrypt_require_key (new_dir); +if (err) +return err; + +if (old_dir != new_dir) { +if (IS_ENCRYPTED (new_dir) && + !fscrypt_has_permitted_context (new_dir, +d_inode (old_dentry))) +return -EPERM ; + +if ((flags &RENAME_EXCHANGE ) && +IS_ENCRYPTED (old_dir) && + !fscrypt_has_permitted_context (old_dir, +d_inode (new_dentry))) +return -EPERM ; + } +return 0 ; +} +EXPORT_SYMBOL_GPL (__fscrypt_prepare_rename);
include/linux/fscrypt.h +33 -0
@@ -204,4 +204,37 @@ static inline int fscrypt_prepare_link(struct dentry *old_dentry, return 0; } +/** +* fscrypt_prepare_rename - prepare for a rename between possibly-encrypted directories +* @old_dir: source directory +* @old_dentry: dentry for source file +* @new_dir: target directory +* @new_dentry: dentry for target location (may be negative unless exchanging) +* @flags: rename flags (we care at least about %RENAME_EXCHANGE) +* +* Prepare for ->rename() where the source and/or target directories may be +* encrypted. A new link can only be added to an encrypted directory if the +* directory's encryption key is available --- since otherwise we'd have no way +* to encrypt the filename. A rename to an existing name, on the other hand, +* *is* cryptographically possible without the key. However, we take the more +* conservative approach and just forbid all no-key renames. +* +* We also verify that the rename will not violate the constraint that all files +* in an encrypted directory tree use the same encryption policy. +* +* Return: 0 on success, -ENOKEY if an encryption key is missing, -EPERM if the +* rename would cause inconsistent encryption policies, or another -errno code. +*/ +static inline int fscrypt_prepare_rename (struct inode *old_dir, +struct dentry *old_dentry, +struct inode *new_dir, +struct dentry *new_dentry, +unsigned int flags) +{ +if (IS_ENCRYPTED (old_dir) ||IS_ENCRYPTED (new_dir)) +return __fscrypt_prepare_rename (old_dir, old_dentry, + new_dir, new_dentry, flags); +return 0 ; +} +#endif /* _LINUX_FSCRYPT_H */
include/linux/fscrypt_notsupp.h +9 -0
@@ -192,4 +192,13 @@ static inline int __fscrypt_prepare_link(struct inode *inode, return -EOPNOTSUPP; } +static inline int __fscrypt_prepare_rename (struct inode *old_dir, +struct dentry *old_dentry, +struct inode *new_dir, +struct dentry *new_dentry, +unsigned int flags) +{ +return -EOPNOTSUPP ; +} +#endif /* _LINUX_FSCRYPT_NOTSUPP_H */
include/linux/fscrypt_supp.h +5 -0
@@ -146,5 +146,10 @@ extern int fscrypt_zeroout_range(const struct inode *, pgoff_t, sector_t,/* hooks.c */ extern int fscrypt_file_open (struct inode *inode,struct file *filp);extern int __fscrypt_prepare_link (struct inode *inode,struct inode *dir); +extern int __fscrypt_prepare_rename (struct inode *old_dir, +struct dentry *old_dentry, +struct inode *new_dir, +struct dentry *new_dentry, +unsigned int flags);#endif /* _LINUX_FSCRYPT_SUPP_H */
Signature · 6 lines
2.14.2.920.gcf0c67979c-goog ------------------------------------------------------------------------------ Check out the vibrant tech community on one of the world's most engaging tech sites, Slashdot.org! http://sdm.link/slashdot
PATCH v2 10/11 fscrypt: new helper function - fscrypt_prepare_lookup()
In reply to Eric BiggersFrom: Eric Biggers <[email protected]>
Introduce a helper function which prepares to look up the given dentry in the given directory. If the directory is encrypted, it handles loading the directory's encryption key, setting the dentry's ->d_op to fscrypt_d_ops, and setting DCACHE_ENCRYPTED_WITH_KEY if the directory's encryption key is available.
Note: once all filesystems switch over to this, we'll be able to move fscrypt_d_ops and fscrypt_set_encrypted_dentry() to fscrypt_private.h.
--- fs/crypto/hooks.c | 18 ++++++++++++++++++ include/linux/fscrypt.h | 28 ++++++++++++++++++++++++++++ include/linux/fscrypt_notsupp.h | 6 ++++++ include/linux/fscrypt_supp.h | 1 + 4 files changed, 53 insertions(+)
fs/crypto/hooks.c +18 -0
@@ -92,3 +92,21 @@ int __fscrypt_prepare_rename(struct inode *old_dir, struct dentry *old_dentry,return 0 ; }EXPORT_SYMBOL_GPL (__fscrypt_prepare_rename); + +int __fscrypt_prepare_lookup (struct inode *dir,struct dentry *dentry) +{ +int err =fscrypt_get_encryption_info (dir); + +if (err) +return err; + +if (fscrypt_has_encryption_key (dir)) { +spin_lock (&dentry->d_lock); + dentry->d_flags |=DCACHE_ENCRYPTED_WITH_KEY ; +spin_unlock (&dentry->d_lock); + } + +d_set_d_op (dentry, &fscrypt_d_ops); +return 0 ; +} +EXPORT_SYMBOL_GPL (__fscrypt_prepare_lookup);
include/linux/fscrypt.h +28 -0
@@ -237,4 +237,32 @@ static inline int fscrypt_prepare_rename(struct inode *old_dir, return 0; } +/** +* fscrypt_prepare_lookup - prepare to lookup a name in a possibly-encrypted directory +* @dir: directory being searched +* @dentry: filename being looked up +* @flags: lookup flags +* +* Prepare for ->lookup() in a directory which may be encrypted. Lookups can be +* done with or without the directory's encryption key; without the key, +* 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. +* +* To allow invalidating stale dentries if the directory's encryption key is +* added later, we also install a custom ->d_revalidate() method and use the +* DCACHE_ENCRYPTED_WITH_KEY flag to indicate whether a given dentry is a +* plaintext name (flag set) or a ciphertext name (flag cleared). +* +* Return: 0 on success, -errno if a problem occurred while setting up the +* encryption key +*/ +static inline int fscrypt_prepare_lookup (struct inode *dir, +struct dentry *dentry, +unsigned int flags) +{ +if (IS_ENCRYPTED (dir)) +return __fscrypt_prepare_lookup (dir, dentry); +return 0 ; +} +#endif /* _LINUX_FSCRYPT_H */
include/linux/fscrypt_notsupp.h +6 -0
@@ -201,4 +201,10 @@ static inline int __fscrypt_prepare_rename(struct inode *old_dir, return -EOPNOTSUPP; } +static inline int __fscrypt_prepare_lookup (struct inode *dir, +struct dentry *dentry) +{ +return -EOPNOTSUPP ; +} +#endif /* _LINUX_FSCRYPT_NOTSUPP_H */
include/linux/fscrypt_supp.h +1 -0
@@ -151,5 +151,6 @@ extern int __fscrypt_prepare_rename(struct inode *old_dir,struct inode *new_dir, struct dentry *new_dentry, unsigned int flags); +extern int __fscrypt_prepare_lookup (struct inode *dir,struct dentry *dentry);#endif /* _LINUX_FSCRYPT_SUPP_H */
Signature · 1 line
2.14.2.920.gcf0c67979c-goog
PATCH v2 11/11 fscrypt: new helper function - fscrypt_prepare_setattr()
In reply to Eric BiggersFrom: Eric Biggers <[email protected]>
Introduce a helper function for filesystems to call when processing ->setattr() on a possibly-encrypted inode. It handles enforcing that an encrypted file can only be truncated if its encryption key is available.
--- include/linux/fscrypt.h | 25 +++++++++++++++++++++++++ 1 file changed, 25 insertions(+)
include/linux/fscrypt.h +25 -0
@@ -265,4 +265,29 @@ static inline int fscrypt_prepare_lookup(struct inode *dir, return 0; } +/** +* fscrypt_prepare_setattr - prepare to change a possibly-encrypted inode's attributes +* @dentry: dentry through which the inode is being changed +* @attr: attributes to change +* +* Prepare for ->setattr() on a possibly-encrypted inode. On an encrypted file, +* most attribute changes are allowed even without the encryption key. However, +* without the encryption key we do have to forbid truncates. This is needed +* because the size being truncated to may not be a multiple of the filesystem +* block size, and in that case we'd have to decrypt the final block, zero the +* portion past i_size, and re-encrypt it. (We *could* allow truncating to a +* filesystem block boundary, but it's simpler to just forbid all truncates --- +* and we already forbid all other contents modifications without the key.) +* +* Return: 0 on success, -ENOKEY if the key is missing, or another -errno code +* if a problem occurred while setting up the encryption key. +*/ +static inline int fscrypt_prepare_setattr (struct dentry *dentry, +struct iattr *attr) +{ +if (attr->ia_valid &ATTR_SIZE ) +return fscrypt_require_key (d_inode (dentry)); +return 0 ; +} +#endif /* _LINUX_FSCRYPT_H */
Signature · 1 line
2.14.2.920.gcf0c67979c-goog
Theodore Ts'o
maintainerQuoted 8 lines · Eric Biggers
From: Eric Biggers <[email protected]>
This series prepares to reduce code duplication among ext4, f2fs, and ubifs by introducing a
S_ENCRYPTEDinode flag (so we don't have to call back into the filesystem to test the filesystem-specific inode flag), then introducing new helper functions that are called at the beginning of the open, link, rename, lookup, and setattr operations.
Quoted 8 lines
In the future we maybe should even call these new helpers from the VFS so that each individual filesystem doesn't have to do it. But that's not possible currently because
fs/crypto/can be built as a module.The patches to switch the filesystems over to use the helper functions were included in v1 of this patchset. They are not included now since I'm planning to get them picked up by the individual filesystem maintainers after this goes in.
Thanks, I've applied these patches plus the ones for ext4 on the fscrypt.git tree.
The only concern I have is that in fscrypt_file_name() the warning prints just the inode numbers, but not the block device (it replaced an ext4_warning() call which automatically printed the block device). I'll add a patch to fix that up in the next day or two.
- Ted
------------------------------------------------------------------------------
Check out the vibrant tech community on one of the world's most engaging tech sites, Slashdot.org! http://sdm.link/slashdot