fscrypt: symlink helpers and fscrypt.h cleanup
v2 cover fscrypt: symlink helpers and fscrypt.h cleanup
From: Eric Biggers <[email protected]>
This series introduces helper functions in fscrypt that filesystems can call to simplify handling of encrypted symlinks. The helpers also fix a couple subtle bugs, such as incorrectly rejecting symlinks that are very close to the maximum length, and accidentally leaving the "." and ".." symlink targets unencrypted (they need to be special in directory entries, but not in symlink targets).
Patches 1-9 are mostly cleanup to trim down fscrypt.h, moving stuff into more appropriate places depending on whether it is needed only internally by fscrypt, or only by filesystems supporting encryption, etc. I was going to send these out as a separate series, but there is a dependency because the symlink helpers depend on fscrypt_dummy_context_enabled() having been fixed to work correctly in the "notsupp" case.
Patch 10 introduces the helpers for ->symlink() and patch 11 introduces a helper for ->get_link().
The patches to switch ext4, f2fs, and ubifs over to use the helper functions were included in v1 of this patchset
(https://marc.info/?l=linux-ext4&m=151336000031869). They are not included now since I'm planning to get them picked up by the individual filesystem maintainers after this goes in.
Changed since v1:
- Fixed __fscrypt_prepare_symlink() to work correctly with test_dummy_encryption mode in the case where the directory is unencrypted and has not yet had its ->i_crypt_info set up.
Eric Biggers (11):
fscrypt: move fscrypt_has_encryption_key() to supp/notsupp headers
fscrypt: move fscrypt_control_page() to supp/notsupp headers
fscrypt: move fscrypt_info_cachep declaration to fscrypt_private.h
fscrypt: move fscrypt_ctx declaration to fscrypt_supp.h
fscrypt: split fscrypt_dummy_context_enabled() into supp/notsupp
versions
fscrypt: move fscrypt_operations declaration to fscrypt_supp.h
fscrypt: move fscrypt_valid_enc_modes() to fscrypt_private.h
fscrypt: move fscrypt_is_dot_dotdot() to fs/crypto/fname.c
fscrypt: trim down fscrypt.h includes
fscrypt: new helper functions for ->symlink()
fscrypt: new helper function - fscrypt_get_symlink()
fs/crypto/crypto.c | 1 + fs/crypto/fname.c | 20 ++++- fs/crypto/fscrypt_private.h | 19 +++++ fs/crypto/hooks.c | 163 +++++++++++++++++++++++++++++++++++++++ fs/crypto/keyinfo.c | 1 + include/linux/fscrypt.h | 165 ++++++++++++++++------------------------ include/linux/fscrypt_notsupp.h | 39 ++++++++++ include/linux/fscrypt_supp.h | 63 ++++++++++++++- 8 files changed, 369 insertions(+), 102 deletions(-)
Signature · 6 lines
2.16.0.rc0.223.g4a4ac83678-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 1/11 fscrypt: move fscrypt_has_encryption_key() to supp/notsupp headers
From: Eric Biggers <[email protected]>
fscrypt_has_encryption_key() is already split into two versions depending on whether the filesystem is being built with encryption support or not. Move them into the appropriate headers.
--- include/linux/fscrypt.h | 10 ---------- include/linux/fscrypt_notsupp.h | 5 +++++ include/linux/fscrypt_supp.h | 5 +++++ 3 files changed, 10 insertions(+), 10 deletions(-)
include/linux/fscrypt.h +0 -10
@@ -129,11 +129,6 @@ static inline struct page *fscrypt_control_page(struct page *page) return ((struct fscrypt_ctx *)page_private(page))->w.control_page; } -static inline bool fscrypt_has_encryption_key (const struct inode *inode) -{ -return (inode->i_crypt_info !=NULL ); -} -#include <linux/fscrypt_supp.h> #else /* !__FS_HAS_ENCRYPTION */
@@ -144,11 +139,6 @@ static inline struct page *fscrypt_control_page(struct page *page) return ERR_PTR(-EINVAL); } -static inline bool fscrypt_has_encryption_key (const struct inode *inode) -{ -return 0 ; -} -#include <linux/fscrypt_notsupp.h> #endif /* __FS_HAS_ENCRYPTION */
include/linux/fscrypt_notsupp.h +5 -0
@@ -14,6 +14,11 @@#ifndef _LINUX_FSCRYPT_NOTSUPP_H#define _LINUX_FSCRYPT_NOTSUPP_H +static inline bool fscrypt_has_encryption_key (const struct inode *inode) +{ +return false; +} +/* crypto.c */ static inline struct fscrypt_ctx *fscrypt_get_ctx (const struct inode *inode,gfp_t gfp_flags)
include/linux/fscrypt_supp.h +5 -0
@@ -11,6 +11,11 @@#ifndef _LINUX_FSCRYPT_SUPP_H#define _LINUX_FSCRYPT_SUPP_H +static inline bool fscrypt_has_encryption_key (const struct inode *inode) +{ +return (inode->i_crypt_info !=NULL ); +} +/* crypto.c */ extern struct kmem_cache *fscrypt_info_cachep;extern struct fscrypt_ctx *fscrypt_get_ctx (const struct inode *,gfp_t );
Signature · 6 lines
2.16.0.rc0.223.g4a4ac83678-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 2/11 fscrypt: move fscrypt_control_page() to supp/notsupp headers
In reply to Eric BiggersFrom: Eric Biggers <[email protected]>
fscrypt_control_page() is already split into two versions depending on whether the filesystem is being built with encryption support or not. Move them into the appropriate headers.
--- include/linux/fscrypt.h | 18 ++---------------- include/linux/fscrypt_notsupp.h | 5 +++++ include/linux/fscrypt_supp.h | 6 ++++++ 3 files changed, 13 insertions(+), 16 deletions(-)
include/linux/fscrypt.h +2 -16
@@ -123,24 +123,10 @@ static inline bool fscrypt_is_dot_dotdot(const struct qstr *str) }#if __FS_HAS_ENCRYPTION - -static inline struct page *fscrypt_control_page (struct page *page) -{ -return ((struct fscrypt_ctx *)page_private (page))->w.control_page; -} -#include <linux/fscrypt_supp.h> - -#else /* !__FS_HAS_ENCRYPTION */ - -static inline struct page *fscrypt_control_page (struct page *page) -{ -WARN_ON_ONCE (1 ); -return ERR_PTR (-EINVAL ); -} - +#else #include <linux/fscrypt_notsupp.h> -#endif /* __FS_HAS_ENCRYPTION */ +#endif /** * fscrypt_require_key - require an inode's encryption key
include/linux/fscrypt_notsupp.h +5 -0
@@ -48,6 +48,11 @@ static inline int fscrypt_decrypt_page(const struct inode *inode,return -EOPNOTSUPP ; } +static inline struct page *fscrypt_control_page (struct page *page) +{ +WARN_ON_ONCE (1 ); +return ERR_PTR (-EINVAL ); +}static inline void fscrypt_restore_control_page (struct page *page) {
include/linux/fscrypt_supp.h +6 -0
@@ -25,6 +25,12 @@ extern struct page *fscrypt_encrypt_page(const struct inode *, struct page *, u64, gfp_t);extern int fscrypt_decrypt_page (const struct inode *,struct page *,unsigned int ,unsigned int ,u64 ); + +static inline struct page *fscrypt_control_page (struct page *page) +{ +return ((struct fscrypt_ctx *)page_private (page))->w.control_page; +} +extern void fscrypt_restore_control_page (struct page *);extern const struct dentry_operations fscrypt_d_ops;
Signature · 1 line
2.16.0.rc0.223.g4a4ac83678-goog
PATCH v2 3/11 fscrypt: move fscrypt_info_cachep declaration to fscrypt_private.h
In reply to Eric BiggersFrom: Eric Biggers <[email protected]>
The fscrypt_info kmem_cache is internal to fscrypt; filesystems don't need to access it. So move its declaration into fscrypt_private.h.
--- fs/crypto/fscrypt_private.h | 1 + include/linux/fscrypt_supp.h | 1 - 2 files changed, 1 insertion(+), 1 deletion(-)
fs/crypto/fscrypt_private.h +1 -0
@@ -72,6 +72,7 @@ typedef enum {#define FS_CTX_HAS_BOUNCE_BUFFER_FL 0x00000002/* crypto.c */ +extern struct kmem_cache *fscrypt_info_cachep;extern int fscrypt_initialize (unsigned int cop_flags);extern struct workqueue_struct *fscrypt_read_workqueue; extern int fscrypt_do_page_crypto(const struct inode *inode,
include/linux/fscrypt_supp.h +0 -1
@@ -17,7 +17,6 @@ static inline bool fscrypt_has_encryption_key(const struct inode *inode) }/* crypto.c */ -extern struct kmem_cache *fscrypt_info_cachep;extern struct fscrypt_ctx *fscrypt_get_ctx (const struct inode *,gfp_t );extern void fscrypt_release_ctx (struct fscrypt_ctx *); extern struct page *fscrypt_encrypt_page(const struct inode *, struct page *,
Signature · 6 lines
2.16.0.rc0.223.g4a4ac83678-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 4/11 fscrypt: move fscrypt_ctx declaration to fscrypt_supp.h
In reply to Eric BiggersFrom: Eric Biggers <[email protected]>
Filesystems only ever access 'struct fscrypt_ctx' through fscrypt functions. But when a filesystem is built without encryption support, these functions are all stubbed out, so the declaration of fscrypt_ctx is unneeded. Therefore, move it from fscrypt.h to fscrypt_supp.h.
--- include/linux/fscrypt.h | 16 +--------------- include/linux/fscrypt_supp.h | 15 +++++++++++++++ 2 files changed, 16 insertions(+), 15 deletions(-)
include/linux/fscrypt.h +1 -15
@@ -24,23 +24,9 @@#define FS_CRYPTO_BLOCK_SIZE 16 +struct fscrypt_ctx ;struct fscrypt_info ; -struct fscrypt_ctx { -union { -struct { -struct page *bounce_page;/* Ciphertext page */ -struct page *control_page;/* Original page */ - } w; -struct { -struct bio *bio; -struct work_struct work; - } r; -struct list_head free_list;/* Free list */ - }; -u8 flags;/* Flags */ -}; -/** * For encrypted symlinks, the ciphertext length is stored at the beginning * of the string in little-endian format.
include/linux/fscrypt_supp.h +15 -0
@@ -11,6 +11,21 @@#ifndef _LINUX_FSCRYPT_SUPP_H#define _LINUX_FSCRYPT_SUPP_H +struct fscrypt_ctx { +union { +struct { +struct page *bounce_page;/* Ciphertext page */ +struct page *control_page;/* Original page */ + } w; +struct { +struct bio *bio; +struct work_struct work; + } r; +struct list_head free_list;/* Free list */ + }; +u8 flags;/* Flags */ +}; +static inline bool fscrypt_has_encryption_key (const struct inode *inode) {return (inode->i_crypt_info !=NULL );
Signature · 6 lines
2.16.0.rc0.223.g4a4ac83678-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 5/11 fscrypt: split fscrypt_dummy_context_enabled() into supp/notsupp versions
In reply to Eric BiggersFrom: Eric Biggers <[email protected]>
fscrypt_dummy_context_enabled() accesses ->s_cop, which now is only set when the filesystem is built with encryption support. This didn't actually matter because no filesystems called it. However, it will start being used soon, so fix it by moving it from fscrypt.h to fscrypt_supp.h and stubbing it out in fscrypt_notsupp.h.
--- include/linux/fscrypt.h | 8 -------- include/linux/fscrypt_notsupp.h | 5 +++++ include/linux/fscrypt_supp.h | 6 ++++++ 3 files changed, 11 insertions(+), 8 deletions(-)
include/linux/fscrypt.h +0 -8
@@ -75,14 +75,6 @@ struct fscrypt_operations {/* Maximum value for the third parameter of fscrypt_operations.set_context(). */ #define FSCRYPT_SET_CONTEXT_MAX_SIZE 28 -static inline bool fscrypt_dummy_context_enabled (struct inode *inode) -{ -if (inode->i_sb->s_cop->dummy_context && - inode->i_sb->s_cop->dummy_context (inode)) -return true; -return false; -} -static inline bool fscrypt_valid_enc_modes (u32 contents_mode,u32 filenames_mode) {
include/linux/fscrypt_notsupp.h +5 -0
@@ -19,6 +19,11 @@ static inline bool fscrypt_has_encryption_key(const struct inode *inode)return false; } +static inline bool fscrypt_dummy_context_enabled (struct inode *inode) +{ +return false; +} +/* crypto.c */ static inline struct fscrypt_ctx *fscrypt_get_ctx (const struct inode *inode,gfp_t gfp_flags)
include/linux/fscrypt_supp.h +6 -0
@@ -31,6 +31,12 @@ static inline bool fscrypt_has_encryption_key(const struct inode *inode)return (inode->i_crypt_info !=NULL ); } +static inline bool fscrypt_dummy_context_enabled (struct inode *inode) +{ +return inode->i_sb->s_cop->dummy_context && + inode->i_sb->s_cop->dummy_context (inode); +} +/* crypto.c */ extern struct fscrypt_ctx *fscrypt_get_ctx (const struct inode *,gfp_t );extern void fscrypt_release_ctx (struct fscrypt_ctx *);
Signature · 6 lines
2.16.0.rc0.223.g4a4ac83678-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 6/11 fscrypt: move fscrypt_operations declaration to fscrypt_supp.h
In reply to Eric BiggersFrom: Eric Biggers <[email protected]>
Filesystems now only define their fscrypt_operations when they are compiled with encryption support, so move the fscrypt_operations declaration from fscrypt.h to fscrypt_supp.h.
--- include/linux/fscrypt.h | 18 ------------------ include/linux/fscrypt_supp.h | 18 ++++++++++++++++++ 2 files changed, 18 insertions(+), 18 deletions(-)
include/linux/fscrypt.h +0 -18
@@ -54,24 +54,6 @@ struct fscrypt_name {#define fname_name (p) ((p)->disk_name.name)#define fname_len (p) ((p)->disk_name.len) -/* -* fscrypt superblock flags -*/ -#define FS_CFLG_OWN_PAGES (1U << 1) - -/* -* crypto opertions for filesystems -*/ -struct fscrypt_operations { -unsigned int flags; -const char *key_prefix; -int (*get_context)(struct inode *,void *,size_t ); -int (*set_context)(struct inode *,const void *,size_t ,void *); -bool (*dummy_context)(struct inode *); -bool (*empty_dir)(struct inode *); -unsigned (*max_namelen)(struct inode *); -}; -/* Maximum value for the third parameter of fscrypt_operations.set_context(). */ #define FSCRYPT_SET_CONTEXT_MAX_SIZE 28
include/linux/fscrypt_supp.h +18 -0
@@ -11,6 +11,24 @@ #ifndef _LINUX_FSCRYPT_SUPP_H #define _LINUX_FSCRYPT_SUPP_H +/* + * fscrypt superblock flags + */ +#define FS_CFLG_OWN_PAGES (1U << 1) + +/* + * crypto operations for filesystems + */ +struct fscrypt_operations { + unsigned int flags; + const char *key_prefix; + int (*get_context)(struct inode *, void *, size_t); + int (*set_context)(struct inode *, const void *, size_t, void *); + bool (*dummy_context)(struct inode *); + bool (*empty_dir)(struct inode *); + unsigned (*max_namelen)(struct inode *); +}; + struct fscrypt_ctx { union { struct {
Signature · 1 line
2.16.0.rc0.223.g4a4ac83678-goog
PATCH v2 7/11 fscrypt: move fscrypt_valid_enc_modes() to fscrypt_private.h
In reply to Eric BiggersFrom: Eric Biggers <[email protected]>
The encryption modes are validated by fs/crypto/, not by individual filesystems. Therefore, move fscrypt_valid_enc_modes() from fscrypt.h to fscrypt_private.h.
--- fs/crypto/fscrypt_private.h | 14 ++++++++++++++ include/linux/fscrypt.h | 14 -------------- 2 files changed, 14 insertions(+), 14 deletions(-)
fs/crypto/fscrypt_private.h +14 -0
@@ -71,6 +71,20 @@ typedef enum {#define FS_CTX_REQUIRES_FREE_ENCRYPT_FL 0x00000001#define FS_CTX_HAS_BOUNCE_BUFFER_FL 0x00000002 +static inline bool fscrypt_valid_enc_modes (u32 contents_mode, +u32 filenames_mode) +{ +if (contents_mode ==FS_ENCRYPTION_MODE_AES_128_CBC && + filenames_mode ==FS_ENCRYPTION_MODE_AES_128_CTS ) +return true; + +if (contents_mode ==FS_ENCRYPTION_MODE_AES_256_XTS && + filenames_mode ==FS_ENCRYPTION_MODE_AES_256_CTS ) +return true; + +return false; +} +/* crypto.c */ extern struct kmem_cache *fscrypt_info_cachep;extern int fscrypt_initialize (unsigned int cop_flags);
include/linux/fscrypt.h +0 -14
@@ -57,20 +57,6 @@ struct fscrypt_name {/* Maximum value for the third parameter of fscrypt_operations.set_context(). */ #define FSCRYPT_SET_CONTEXT_MAX_SIZE 28 -static inline bool fscrypt_valid_enc_modes (u32 contents_mode, -u32 filenames_mode) -{ -if (contents_mode ==FS_ENCRYPTION_MODE_AES_128_CBC && - filenames_mode ==FS_ENCRYPTION_MODE_AES_128_CTS ) -return true; - -if (contents_mode ==FS_ENCRYPTION_MODE_AES_256_XTS && - filenames_mode ==FS_ENCRYPTION_MODE_AES_256_CTS ) -return true; - -return false; -} -static inline bool fscrypt_is_dot_dotdot (const struct qstr *str) {if (str->len ==1 && str->name[0 ] =='.' )
Signature · 6 lines
2.16.0.rc0.223.g4a4ac83678-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: move fscrypt_is_dot_dotdot() to fs/crypto/fname.c
In reply to Eric BiggersFrom: Eric Biggers <[email protected]>
Only fs/crypto/fname.c cares about treating the "." and ".." filenames specially with regards to encryption, so move fscrypt_is_dot_dotdot() from fscrypt.h to there.
--- fs/crypto/fname.c | 11 +++++++++++ include/linux/fscrypt.h | 11 ----------- 2 files changed, 11 insertions(+), 11 deletions(-)
fs/crypto/fname.c +11 -0
@@ -15,6 +15,17 @@#include <linux/ratelimit.h> #include "fscrypt_private.h" +static inline bool fscrypt_is_dot_dotdot (const struct qstr *str) +{ +if (str->len ==1 && str->name[0 ] =='.' ) +return true; + +if (str->len ==2 && str->name[0 ] =='.' && str->name[1 ] =='.' ) +return true; + +return false; +} +/** * fname_encrypt() - encrypt a filename *
include/linux/fscrypt.h +0 -11
@@ -57,17 +57,6 @@ struct fscrypt_name {/* Maximum value for the third parameter of fscrypt_operations.set_context(). */ #define FSCRYPT_SET_CONTEXT_MAX_SIZE 28 -static inline bool fscrypt_is_dot_dotdot (const struct qstr *str) -{ -if (str->len ==1 && str->name[0 ] =='.' ) -return true; - -if (str->len ==2 && str->name[0 ] =='.' && str->name[1 ] =='.' ) -return true; - -return false; -} -#if __FS_HAS_ENCRYPTION#include <linux/fscrypt_supp.h> #else
Signature · 6 lines
2.16.0.rc0.223.g4a4ac83678-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: trim down fscrypt.h includes
In reply to Eric BiggersFrom: Eric Biggers <[email protected]>
fscrypt.h included way too many other headers, given that it is included by filesystems both with and without encryption support. Trim down the includes list by moving the needed includes into more appropriate places, and removing the unneeded ones.
--- fs/crypto/crypto.c | 1 + fs/crypto/fname.c | 1 + fs/crypto/keyinfo.c | 1 + include/linux/fscrypt.h | 6 ------ include/linux/fscrypt_supp.h | 3 +++ 5 files changed, 6 insertions(+), 6 deletions(-)
fs/crypto/crypto.c +1 -0
@@ -27,6 +27,7 @@#include <linux/dcache.h> #include <linux/namei.h> #include <crypto/aes.h> +#include <crypto/skcipher.h> #include "fscrypt_private.h" static unsigned int num_prealloc_crypto_pages =32 ;
fs/crypto/fname.c +1 -0
@@ -13,6 +13,7 @@#include <linux/scatterlist.h> #include <linux/ratelimit.h> +#include <crypto/skcipher.h> #include "fscrypt_private.h" static inline bool fscrypt_is_dot_dotdot (const struct qstr *str)
fs/crypto/keyinfo.c +1 -0
@@ -14,6 +14,7 @@#include <linux/ratelimit.h> #include <crypto/aes.h> #include <crypto/sha.h> +#include <crypto/skcipher.h> #include "fscrypt_private.h" static struct crypto_shash *essiv_hash_tfm;
include/linux/fscrypt.h +0 -6
@@ -14,13 +14,7 @@#ifndef _LINUX_FSCRYPT_H#define _LINUX_FSCRYPT_H -#include <linux/key.h> #include <linux/fs.h> -#include <linux/mm.h> -#include <linux/bio.h> -#include <linux/dcache.h> -#include <crypto/skcipher.h> -#include <uapi/linux/fs.h> #define FS_CRYPTO_BLOCK_SIZE 16
include/linux/fscrypt_supp.h +3 -0
@@ -11,6 +11,9 @@#ifndef _LINUX_FSCRYPT_SUPP_H#define _LINUX_FSCRYPT_SUPP_H +#include <linux/mm.h> +#include <linux/slab.h> +/* * fscrypt superblock flags */
Signature · 6 lines
2.16.0.rc0.223.g4a4ac83678-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 functions for ->symlink()
In reply to Eric BiggersFrom: Eric Biggers <[email protected]>
Currently, filesystems supporting fscrypt need to implement some tricky logic when creating encrypted symlinks, including handling a peculiar on-disk format (struct fscrypt_symlink_data) and correctly calculating the size of the encrypted symlink. Introduce helper functions to make things a bit easier:
- fscrypt_prepare_symlink() computes and validates the size the symlink target will require on-disk.
- fscrypt_encrypt_symlink() creates the encrypted target if needed.
The new helpers actually fix some subtle bugs. First, when checking whether the symlink target was too long, filesystems didn't account for the fact that the NUL padding is meant to be truncated if it would cause the maximum length to be exceeded, as is done for filenames in directories. Consequently users would receive ENAMETOOLONG when creating symlinks close to what is supposed to be the maximum length. For example, with EXT4 with a 4K block size, the maximum symlink target length in an encrypted directory is supposed to be 4093 bytes (in comparison to 4095 in an unencrypted directory), but in FS_POLICY_FLAGS_PAD_32-mode only up to 4064 bytes were accepted.
Second, symlink targets of "." and ".." were not being encrypted, even though they should be, as these names are special in *directory entries* but not in symlink targets. Fortunately, we can fix this simply by starting to encrypt them, as old kernels already accept them in encrypted form.
Third, the output string length the filesystems were providing when doing the actual encryption was incorrect, as it was forgotten to exclude 'sizeof(struct fscrypt_symlink_data)'. Fortunately though, this bug didn't make a difference.
--- fs/crypto/fname.c | 8 ++-- fs/crypto/fscrypt_private.h | 4 ++ fs/crypto/hooks.c | 90 +++++++++++++++++++++++++++++++++++++++++ include/linux/fscrypt.h | 64 +++++++++++++++++++++++++++++ include/linux/fscrypt_notsupp.h | 16 ++++++++ include/linux/fscrypt_supp.h | 6 +++ 6 files changed, 185 insertions(+), 3 deletions(-)
fs/crypto/fname.c +5 -3
@@ -34,8 +34,8 @@ static inline bool fscrypt_is_dot_dotdot(const struct qstr *str)* * Return: 0 on success, -errno on failure */ -static int fname_encrypt (struct inode *inode, -const struct qstr *iname,struct fscrypt_str *oname) +int fname_encrypt (struct inode *inode, +const struct qstr *iname,struct fscrypt_str *oname) {struct skcipher_request *req =NULL ;DECLARE_CRYPTO_WAIT (wait);
@@ -56,9 +56,11 @@ static int fname_encrypt(struct inode *inode,* Copy the filename to the output buffer for encrypting in-place and * pad it with the needed number of NUL bytes. */ +if (WARN_ON (oname->len < iname->len)) +return -ENOBUFS ; cryptlen = max_t(unsigned int, iname->len, FS_CRYPTO_BLOCK_SIZE); cryptlen =round_up (cryptlen, padding); - cryptlen =min (cryptlen, lim); + cryptlen =min3 (cryptlen, lim, oname->len);memcpy (oname->name, iname->name, iname->len);memset (oname->name + iname->len,0 , cryptlen - iname->len);
fs/crypto/fscrypt_private.h +4 -0
@@ -98,6 +98,10 @@ extern int fscrypt_do_page_crypto(const struct inode *inode,extern struct page *fscrypt_alloc_bounce_page (struct fscrypt_ctx *ctx,gfp_t gfp_flags); +/* fname.c */ +extern int fname_encrypt (struct inode *inode, +const struct qstr *iname,struct fscrypt_str *oname); +/* keyinfo.c */ extern void __exit fscrypt_essiv_cleanup(void);
fs/crypto/hooks.c +90 -0
93 lines not on this page · raw message
include/linux/fscrypt.h +64 -0
68 lines not on this page · raw message
include/linux/fscrypt_notsupp.h +16 -0
@@ -223,4 +223,20 @@ static inline int __fscrypt_prepare_lookup(struct inode *dir, return -EOPNOTSUPP; } +static inline int __fscrypt_prepare_symlink (struct inode *dir, +unsigned int len, +unsigned int max_len, +struct fscrypt_str *disk_link) +{ +return -EOPNOTSUPP ; +} + +static inline int __fscrypt_encrypt_symlink (struct inode *inode, +const char *target, +unsigned int len, +struct fscrypt_str *disk_link) +{ +return -EOPNOTSUPP ; +} +#endif /* _LINUX_FSCRYPT_NOTSUPP_H */
include/linux/fscrypt_supp.h +6 -0
@@ -205,5 +205,11 @@ extern int __fscrypt_prepare_rename(struct inode *old_dir,struct dentry *new_dentry, unsigned int flags);extern int __fscrypt_prepare_lookup (struct inode *dir,struct dentry *dentry); +extern int __fscrypt_prepare_symlink (struct inode *dir,unsigned int len, +unsigned int max_len, +struct fscrypt_str *disk_link); +extern int __fscrypt_encrypt_symlink (struct inode *inode,const char *target, +unsigned int len, +struct fscrypt_str *disk_link);#endif /* _LINUX_FSCRYPT_SUPP_H */
Signature · 6 lines
2.16.0.rc0.223.g4a4ac83678-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 11/11 fscrypt: new helper function - fscrypt_get_symlink()
In reply to Eric BiggersFrom: Eric Biggers <[email protected]>
Filesystems also have duplicate code to support ->get_link() on encrypted symlinks. Factor it out into a new function fscrypt_get_symlink(). It takes in the contents of the encrypted symlink on-disk and provides the target (decrypted or encoded) that should be returned from ->get_link().
--- fs/crypto/hooks.c | 73 +++++++++++++++++++++++++++++++++++++++++ include/linux/fscrypt_notsupp.h | 8 +++++ include/linux/fscrypt_supp.h | 3 ++ 3 files changed, 84 insertions(+)
fs/crypto/hooks.c +73 -0
@@ -200,3 +200,76 @@ int __fscrypt_encrypt_symlink(struct inode *inode, const char *target,return 0 ; }EXPORT_SYMBOL_GPL (__fscrypt_encrypt_symlink); + +/** +* fscrypt_get_symlink - get the target of an encrypted symlink +* @inode: the symlink inode +* @caddr: the on-disk contents of the symlink +* @max_size: size of @caddr buffer +* @done: if successful, will be set up to free the returned target +* +* If the symlink's encryption key is available, we decrypt its target. +* Otherwise, we encode its target for presentation. +* +* This may sleep, so the filesystem must have dropped out of RCU mode already. +* +* Return: the presentable symlink target or an ERR_PTR() +*/ +const char *fscrypt_get_symlink (struct inode *inode,const void *caddr, +unsigned int max_size, +struct delayed_call *done) +{ +const struct fscrypt_symlink_data *sd; +struct fscrypt_str cstr, pstr; +int err; + +/* This is for encrypted symlinks only */ +if (WARN_ON (!IS_ENCRYPTED (inode))) +return ERR_PTR (-EINVAL ); + +/* +* Try to set up the symlink's encryption key, but we can continue +* regardless of whether the key is available or not. +*/ + err =fscrypt_get_encryption_info (inode); +if (err) +return ERR_PTR (err); + +/* +* For historical reasons, encrypted symlink targets are prefixed with +* the ciphertext length, even though this is redundant with i_size. +*/ + +if (max_size <sizeof (*sd)) +return ERR_PTR (-EUCLEAN ); + sd = caddr; + cstr.name = (unsigned char *)sd->encrypted_path; + cstr.len =le16_to_cpu (sd->len); + +if (cstr.len ==0 ) +return ERR_PTR (-EUCLEAN ); + +if (cstr.len +sizeof (*sd) -1 > max_size) +return ERR_PTR (-EUCLEAN ); + + err =fscrypt_fname_alloc_buffer (inode, cstr.len, &pstr); +if (err) +return ERR_PTR (err); + + err =fscrypt_fname_disk_to_usr (inode,0 ,0 , &cstr, &pstr); +if (err) + goto err_kfree; + + err = -EUCLEAN ; +if (pstr.name[0 ] =='\0' ) + goto err_kfree; + + pstr.name[pstr.len] ='\0' ; +set_delayed_call (done, kfree_link, pstr.name); +return pstr.name; + +err_kfree: +kfree (pstr.name); +return ERR_PTR (err); +} +EXPORT_SYMBOL_GPL (fscrypt_get_symlink);
include/linux/fscrypt_notsupp.h +8 -0
@@ -239,4 +239,12 @@ static inline int __fscrypt_encrypt_symlink(struct inode *inode, return -EOPNOTSUPP; } +static inline const char *fscrypt_get_symlink (struct inode *inode, +const void *caddr, +unsigned int max_size, +struct delayed_call *done) +{ +return ERR_PTR (-EOPNOTSUPP ); +} +#endif /* _LINUX_FSCRYPT_NOTSUPP_H */
include/linux/fscrypt_supp.h +3 -0
@@ -211,5 +211,8 @@ extern int __fscrypt_prepare_symlink(struct inode *dir, unsigned int len,extern int __fscrypt_encrypt_symlink (struct inode *inode,const char *target,unsigned int len,struct fscrypt_str *disk_link); +extern const char *fscrypt_get_symlink (struct inode *inode,const void *caddr, +unsigned int max_size, +struct delayed_call *done);#endif /* _LINUX_FSCRYPT_SUPP_H */
Signature · 1 line
2.16.0.rc0.223.g4a4ac83678-goog
Jaegeuk Kim
maintainerRe: [PATCH v2 05/11] fscrypt: split fscrypt_dummy_context_enabled() into supp/notsupp versions
In reply to Eric BiggersQuoted 7 lines · Eric Biggers
From: Eric Biggers <[email protected]>
fscrypt_dummy_context_enabled()accesses ->s_cop, which now is only set when the filesystem is built with encryption support. This didn't actually matter because no filesystems called it. However, it will start being used soon, so fix it by moving it fromfscrypt.htofscrypt_supp.hand stubbing it out infscrypt_notsupp.h.
Ted, do we have a chance to get rid of this dummy_context? If there exists backward compatibility issue, please never mind tho.
Thanks,
Quoted 62 lines
Signed-off-by Eric Biggers <[email protected]>--- include/linux/fscrypt.h | 8 -------- include/linux/fscrypt_notsupp.h | 5 +++++ include/linux/fscrypt_supp.h | 6 ++++++ 3 files changed, 11 insertions(+), 8 deletions(-)include/linux/fscrypt.h +0
-8@@ -75,14 +75,6 @@ struct fscrypt_operations {/* Maximum value for the third parameter of fscrypt_operations.set_context(). */ #define FSCRYPT_SET_CONTEXT_MAX_SIZE 28 -static inline bool fscrypt_dummy_context_enabled (struct inode *inode) -{ -if (inode->i_sb->s_cop->dummy_context && - inode->i_sb->s_cop->dummy_context (inode)) -return true; -return false; -} -static inline bool fscrypt_valid_enc_modes (u32 contents_mode,u32 filenames_mode) {include/linux/fscrypt_notsupp.h +5
-0@@ -19,6 +19,11 @@ static inline bool fscrypt_has_encryption_key(const struct inode *inode)return false; } +static inline bool fscrypt_dummy_context_enabled (struct inode *inode) +{ +return false; +} +/* crypto.c */ static inline struct fscrypt_ctx *fscrypt_get_ctx (const struct inode *inode,gfp_t gfp_flags)include/linux/fscrypt_supp.h +6
-0@@ -31,6 +31,12 @@ static inline bool fscrypt_has_encryption_key(const struct inode *inode)return (inode->i_crypt_info !=NULL ); } +static inline bool fscrypt_dummy_context_enabled (struct inode *inode) +{ +return inode->i_sb->s_cop->dummy_context && + inode->i_sb->s_cop->dummy_context (inode); +} +/* crypto.c */ extern struct fscrypt_ctx *fscrypt_get_ctx (const struct inode *,gfp_t );extern void fscrypt_release_ctx (struct fscrypt_ctx *);Signature · 1 line
2.16.0.rc0.223.g4a4ac83678-goog
Re: [PATCH v2 05/11] fscrypt: split fscrypt_dummy_context_enabled() into supp/notsupp versions
Quoted 12 lines · Jaegeuk Kim
Quoted 7 lines · Eric Biggers
From: Eric Biggers <[email protected]>
fscrypt_dummy_context_enabled()accesses ->s_cop, which now is only set when the filesystem is built with encryption support. This didn't actually matter because no filesystems called it. However, it will start being used soon, so fix it by moving it fromfscrypt.htofscrypt_supp.hand stubbing it out infscrypt_notsupp.h.Ted, do we have a chance to get rid of this
dummy_context? If there exists backward compatibility issue, please never mind tho.
It's used to implement the test_dummy_encryption mount option for ext4, which is used by the 'ext4/encrypt' config for gce-xfstests. Its purpose is to cause all new files (directories, regular files, and symlinks) to be automatically encrypted with a default key, so that the encrypted I/O paths are tested more thoroughly than by just running the 'encrypt' group tests.
There are no backward compatibility concerns with changing or removing the test_dummy_encryption mount option; we just don't have a better solution yet.
Ideally, instead of using test_dummy_encryption we would encrypt the root directory of the filesystem immediately after it is formatted. However, that doesn't work for ext4 because the lost+found directory has to be located in the root directory, and must be unencrypted, and the lost+found directory entry must be unencrypted.
So I think getting rid of test_dummy_encryption depends on a solution to the lost+found problem.
An alternative would be to add support for "inherit-only" encryption policies, then set such a policy on the root directory of the filesystem. But, there haven't been any requests for such a feature yet outside of this specific testing-only use case, so I've been hesitant to add it.
Eric
------------------------------------------------------------------------------
Check out the vibrant tech community on one of the world's most engaging tech sites, Slashdot.org! http://sdm.link/slashdot
Jaegeuk Kim
maintainerRe: [PATCH v2 05/11] fscrypt: split fscrypt_dummy_context_enabled() into supp/notsupp versions
Quoted 23 lines · Eric Biggers
Quoted 12 lines · Jaegeuk Kim
Quoted 7 lines · Eric Biggers
From: Eric Biggers <[email protected]>
fscrypt_dummy_context_enabled()accesses ->s_cop, which now is only set when the filesystem is built with encryption support. This didn't actually matter because no filesystems called it. However, it will start being used soon, so fix it by moving it fromfscrypt.htofscrypt_supp.hand stubbing it out infscrypt_notsupp.h.Ted, do we have a chance to get rid of this
dummy_context? If there exists backward compatibility issue, please never mind tho.It's used to implement the
test_dummy_encryptionmount option for ext4, which is used by the 'ext4/encrypt' config for gce-xfstests. Its purpose is to cause all new files (directories, regular files, and symlinks) to be automatically encrypted with a default key, so that the encrypted I/O paths are tested more thoroughly than by just running the 'encrypt' group tests.There are no backward compatibility concerns with changing or removing the
test_dummy_encryptionmount option; we just don't have a better solution yet.
Quoted 8 lines
Ideally, instead of using
test_dummy_encryptionwe would encrypt the root directory of the filesystem immediately after it is formatted. However, that doesn't work for ext4 because the lost+found directory has to be located in the root directory, and must be unencrypted, and the lost+found directory entry must be unencrypted.So I think getting rid of
test_dummy_encryptiondepends on a solution to the lost+found problem.
Thank you for the explanation. Actually, I've got used to encrypt the root dir in f2fs for such the testing purpose, since it's indeed empty. If it's the only matter of lost_found in ext4, how about setting the encryption bit and dummy context in root inode by mke2fs or tune2fs?
Thanks,
Quoted 7 lines
An alternative would be to add support for "inherit-only" encryption policies, then set such a policy on the root directory of the filesystem. But, there haven't been any requests for such a feature yet outside of this specific testing-only use case, so I've been hesitant to add it.
Eric
Re: [PATCH v2 05/11] fscrypt: split fscrypt_dummy_context_enabled() into supp/notsupp versions
Quoted 30 lines · Jaegeuk Kim
Quoted 29 lines · Eric Biggers
Quoted 12 lines · Jaegeuk Kim
Quoted 7 lines · Eric Biggers
From: Eric Biggers <[email protected]>
fscrypt_dummy_context_enabled()accesses ->s_cop, which now is only set when the filesystem is built with encryption support. This didn't actually matter because no filesystems called it. However, it will start being used soon, so fix it by moving it fromfscrypt.htofscrypt_supp.hand stubbing it out infscrypt_notsupp.h.Ted, do we have a chance to get rid of this
dummy_context? If there exists backward compatibility issue, please never mind tho.It's used to implement the
test_dummy_encryptionmount option for ext4, which is used by the 'ext4/encrypt' config for gce-xfstests. Its purpose is to cause all new files (directories, regular files, and symlinks) to be automatically encrypted with a default key, so that the encrypted I/O paths are tested more thoroughly than by just running the 'encrypt' group tests.There are no backward compatibility concerns with changing or removing the
test_dummy_encryptionmount option; we just don't have a better solution yet.Ideally, instead of using
test_dummy_encryptionwe would encrypt the root directory of the filesystem immediately after it is formatted. However, that doesn't work for ext4 because the lost+found directory has to be located in the root directory, and must be unencrypted, and the lost+found directory entry must be unencrypted.
Quoted 8 lines
Quoted 2 lines
So I think getting rid of
test_dummy_encryptiondepends on a solution to the lost+found problem.Thank you for the explanation. Actually, I've got used to encrypt the root dir in f2fs for such the testing purpose, since it's indeed empty. If it's the only matter of
lost_foundin ext4, how about setting the encryption bit and dummy context in root inode by mke2fs or tune2fs?
You can't mark a nonempty directory as encrypted. First, you can't have an unencrypted file *name* in an encrypted directory, because then all the operations on that filename won't work, because the filesystem will think it is encrypted. Second, you cannot have an unencrypted file in an encrypted directory because that violates the constraint that all files in an encrypted directory are encrypted -- and by design that is verified on ->lookup().
So to get it to actually work in ext4 we'd need to add extra special-case logic around the lost+found directory -- probably allowing both the directory entry and directory itself to be unencrypted, despite existing in an encrypted directory. And I think that solution would be more complicated than the test_dummy_encryption mount option we have now.
It may be worth reconsidering if people start asking for the ability to encrypt the root directory for other reasons, though. (Although, that use case is largely redundant with dm-crypt.)
Eric
Jaegeuk Kim
maintainerRe: [PATCH v2 05/11] fscrypt: split fscrypt_dummy_context_enabled() into supp/notsupp versions
Quoted 47 lines · Eric Biggers
Quoted 30 lines · Jaegeuk Kim
Quoted 29 lines · Eric Biggers
Quoted 12 lines · Jaegeuk Kim
Quoted 7 lines · Eric Biggers
From: Eric Biggers <[email protected]>
fscrypt_dummy_context_enabled()accesses ->s_cop, which now is only set when the filesystem is built with encryption support. This didn't actually matter because no filesystems called it. However, it will start being used soon, so fix it by moving it fromfscrypt.htofscrypt_supp.hand stubbing it out infscrypt_notsupp.h.Ted, do we have a chance to get rid of this
dummy_context? If there exists backward compatibility issue, please never mind tho.It's used to implement the
test_dummy_encryptionmount option for ext4, which is used by the 'ext4/encrypt' config for gce-xfstests. Its purpose is to cause all new files (directories, regular files, and symlinks) to be automatically encrypted with a default key, so that the encrypted I/O paths are tested more thoroughly than by just running the 'encrypt' group tests.There are no backward compatibility concerns with changing or removing the
test_dummy_encryptionmount option; we just don't have a better solution yet.Ideally, instead of using
test_dummy_encryptionwe would encrypt the root directory of the filesystem immediately after it is formatted. However, that doesn't work for ext4 because the lost+found directory has to be located in the root directory, and must be unencrypted, and the lost+found directory entry must be unencrypted.Quoted 8 lines
Quoted 2 lines
So I think getting rid of
test_dummy_encryptiondepends on a solution to the lost+found problem.Thank you for the explanation. Actually, I've got used to encrypt the root dir in f2fs for such the testing purpose, since it's indeed empty. If it's the only matter of
lost_foundin ext4, how about setting the encryption bit and dummy context in root inode by mke2fs or tune2fs?You can't mark a nonempty directory as encrypted. First, you can't have an unencrypted file *name* in an encrypted directory, because then all the operations on that filename won't work, because the filesystem will think it is encrypted. Second, you cannot have an unencrypted file in an encrypted directory because that violates the constraint that all files in an encrypted directory are encrypted -- and by design that is verified on ->lookup().
Quoted 5 lines
So to get it to actually work in ext4 we'd need to add extra special-case logic around the lost+found directory -- probably allowing both the directory entry and directory itself to be unencrypted, despite existing in an encrypted directory. And I think that solution would be more complicated than the
test_dummy_encryptionmount option we have now.
Agreed that dummy'd be easy to go for now tho, doesn't it give any security concern at all, even only for ext4 testing purpose? Is there a chance to hack the mount option in runtime? BTW, it may be doable to build an encrypt root inode having encrypted dentry of lost_found, given dummy_context in mke2fs. I'm just curious whether or not it'd be worth to do.
Thanks,
Quoted 6 lines
It may be worth reconsidering if people start asking for the ability to encrypt the root directory for other reasons, though. (Although, that use case is largely redundant with dm-crypt.)
Eric
Theodore Ts'o
maintainerRe: [PATCH v2 05/11] fscrypt: split fscrypt_dummy_context_enabled() into supp/notsupp versions
Quoted 6 lines · Jaegeuk Kim
Agreed that dummy'd be easy to go for now tho, doesn't it give any security concern at all, even only for ext4 testing purpose? Is there a chance to hack the mount option in runtime? BTW, it may be doable to build an encrypt root inode having encrypted dentry of
lost_found, givendummy_contextin mke2fs. I'm just curious whether or not it'd be worth to do.
It requires root to manipulate the mount option; and if you don't trust root, you've got other problems.
The problem is that e2fsck needs to be able to repair a file system without having access to any of the file system keys. And so if there is an orphaned inode (e.g., an inode which is not attached to any directory), we need to put it somewhere, and that's /lost+found.
I've considered making lost+found a magic directory which isn't directly linked into the root directory, and have a magic namei lookup. There are downsides though; that would mean that the /lost+found filename has to be magic --- what to do if the user tries to create a lost+found directory, or rename the magic lost+found directory.
Ultimately, i decided it wasn't worth the semantic and implementational complexity, especially since none of the primary use cases (Android, Chromeos, etc.) needed an encrypted root directory.
- Ted
------------------------------------------------------------------------------
Check out the vibrant tech community on one of the world's most engaging tech sites, Slashdot.org! http://sdm.link/slashdot
Theodore Ts'o
maintainerI've taken the v2 version of this series, plus the ext4-specific patches to use the new symlink helpers (from the v1 patch series). After testing and validating them, I included the patches to convert f2fs and ubifs to use the symlink helpers, so I could also grab the cleanup patches from the V1 series.
Jaeguk, Richard, please let me know if you are OK with my taking the f2fs and ubifs changes in the fscrypt tree. It seems like it would keep things simpler if we did that.
Cheers,
- Ted
------------------------------------------------------------------------------
Check out the vibrant tech community on one of the world's most engaging tech sites, Slashdot.org! http://sdm.link/slashdot
Jaegeuk Kim
maintainerQuoted 9 lines · Theodore Ts'o
I've taken the v2 version of this series, plus the ext4-specific patches to use the new symlink helpers (from the v1 patch series). After testing and validating them, I included the patches to convert f2fs and ubifs to use the symlink helpers, so I could also grab the cleanup patches from the V1 series.
Jaeguk, Richard, please let me know if you are OK with my taking the f2fs and ubifs changes in the fscrypt tree. It seems like it would keep things simpler if we did that.
I'm fine with that.
Thanks,
Quoted 4 lines
Cheers,
- Ted