ext4 encryption patches
Theodore Ts'o
maintainerPATCH v2 2/20 ext4 crypto: reserve codepoints used by the ext4 encryption feature
In reply to Theodore Ts'o--- fs/ext4/ext4.h | 25 +++++++++++++++++++------ 1 file changed, 19 insertions(+), 6 deletions(-)
fs/ext4/ext4.h +19 -6
@@ -364,7 +364,8 @@ struct flex_groups {#define EXT4_DIRTY_FL 0x00000100#define EXT4_COMPRBLK_FL 0x00000200/* One or more compressed clusters */ #define EXT4_NOCOMPR_FL 0x00000400/* Don't compress */ -#define EXT4_ECOMPR_FL 0x00000800/* Compression error */ +/* nb: was previously EXT2_ECOMPR_FL */ +#define EXT4_ENCRYPT_FL 0x00000800/* encrypted file */ /* End compression flags --- maybe not all used */ #define EXT4_INDEX_FL 0x00001000/* hash-indexed directory */ #define EXT4_IMAGIC_FL 0x00002000/* AFS directory */
@@ -417,11 +418,11 @@ enum {EXT4_INODE_APPEND =5 ,/* writes to file may only append */ EXT4_INODE_NODUMP =6 ,/* do not dump file */ EXT4_INODE_NOATIME =7 ,/* do not update atime */ -/* Reserved for compression usage... */ +/* Reserved for compression usage, co-opted for encryption usage */ EXT4_INODE_DIRTY =8 ,EXT4_INODE_COMPRBLK =9 ,/* One or more compressed clusters */ EXT4_INODE_NOCOMPR =10 ,/* Don't compress */ -EXT4_INODE_ECOMPR =11 ,/* Compression error */ +EXT4_INODE_ENCRYPT =11 ,/* Encrypted */ /* End compression flags --- maybe not all used */ EXT4_INODE_INDEX =12 ,/* hash-indexed directory */ EXT4_INODE_IMAGIC = 13, /* AFS directory */
@@ -466,7 +467,7 @@ static inline void ext4_check_flag_values(void)CHECK_FLAG_VALUE (DIRTY );CHECK_FLAG_VALUE (COMPRBLK );CHECK_FLAG_VALUE (NOCOMPR ); -CHECK_FLAG_VALUE (ECOMPR ); +CHECK_FLAG_VALUE (ENCRYPT );CHECK_FLAG_VALUE (INDEX );CHECK_FLAG_VALUE (IMAGIC );CHECK_FLAG_VALUE (JOURNAL_DATA );
@@ -581,6 +582,13 @@ enum {#define EXT4_FREE_BLOCKS_NOFREE_FIRST_CLUSTER 0x0010#define EXT4_FREE_BLOCKS_NOFREE_LAST_CLUSTER 0x0020 +/* Encryption algorithms */ +#define EXT4_ENCRYPTION_MODE_INVALID 0 +#define EXT4_ENCRYPTION_MODE_AES_256_XTS 1 +#define EXT4_ENCRYPTION_MODE_AES_256_GCM 2 +#define EXT4_ENCRYPTION_MODE_AES_256_CBC 3 +#define EXT4_ENCRYPTION_MODE_AES_256_CTS 4 +/* * ioctl commands */
@@ -1130,7 +1138,8 @@ struct ext4_super_block {__le32 s_raid_stripe_width;/* blocks on all data disks (N*stride)*/ __u8 s_log_groups_per_flex;/* FLEX_BG group size */ __u8 s_checksum_type;/* metadata checksum algorithm used */ -__le16 s_reserved_pad; +__u8 s_encryption_level;/* versioning level for encryption */ +__u8 s_reserved_pad;/* Padding to next 32bits */ __le64 s_kbytes_written;/* nr of lifetime kilobytes written */ __le32 s_snapshot_inum;/* Inode number of active snapshot */ __le32 s_snapshot_id;/* sequential ID of active snapshot */
@@ -1156,7 +1165,10 @@ struct ext4_super_block {__le32 s_grp_quota_inum;/* inode for tracking group quota */ __le32 s_overhead_clusters;/* overhead blocks/clusters in fs */ __le32 s_backup_bgs[2 ];/* groups with sparse_super2 SBs */ -__le32 s_reserved[106 ];/* Padding to the end of the block */ +__u8 s_encrypt_algos[4 ];/* Encryption algorithms in use */ +__u8 s_encrypt_pw_salt[16 ];/* Salt used for string2key algorithm */ +__le32 s_lpf_ino;/* Location of the lost+found inode */ +__le32 s_reserved[100 ];/* Padding to the end of the block */ __le32 s_checksum;/* crc32c(superblock) */ };
@@ -1537,6 +1549,7 @@ static inline void ext4_clear_state_flags(struct ext4_inode_info *ei)#define EXT4_FEATURE_INCOMPAT_BG_USE_META_CSUM 0x2000/* use crc32c for bg */ #define EXT4_FEATURE_INCOMPAT_LARGEDIR 0x4000/* >2GB or 3-lvl htree */ #define EXT4_FEATURE_INCOMPAT_INLINE_DATA 0x8000/* data in inode */ +#define EXT4_FEATURE_INCOMPAT_ENCRYPT 0x10000#define EXT2_FEATURE_COMPAT_SUPP EXT4_FEATURE_COMPAT_EXT_ATTR#define EXT2_FEATURE_INCOMPAT_SUPP (EXT4_FEATURE_INCOMPAT_FILETYPE| \
Signature · 1 line
2.3.0
Theodore Ts'o
maintainerPATCH v2 3/20 ext4 crypto: add ext4 encryption Kconfig
In reply to Theodore Ts'o--- fs/ext4/Kconfig | 17 +++++++++++++++++ 1 file changed, 17 insertions(+)
fs/ext4/Kconfig +17 -0
@@ -64,6 +64,23 @@ config EXT4_FS_SECURITY If you are not using a security module that requires using extended attributes for file security labels, say N. +config EXT4_FS_ENCRYPTION + bool "Ext4 Encryption" + depends on EXT4_FS + select CRYPTO_AES + select CRYPTO_CBC + select CRYPTO_ECB + select CRYPTO_XTS + select CRYPTO_CTS + select CRYPTO_SHA256 + select KEYS + select ENCRYPTED_KEYS + help + Enable encryption of ext4 files and directories. This + feature is similar to ecryptfs, but it is more memory + efficient since it avoids caching the encrypted and + decrypted pages in the page cache. + config EXT4_DEBUG bool "EXT4 debugging support" depends on EXT4_FS
Signature · 1 line
2.3.0
Theodore Ts'o
maintainerv2 cover ext4 encryption patches
Changes since the last version of the patches
* Filename encryption now uses ciphertext stealing mode, which is more space efficient
* Fixed a bug when deleting encrypted htree directories w/o the key available
* Cleaned up comments, copyrights, and whitespace
* The encryption feature is not advertised via /sys/fs/ext4/features
* Remove unneeded Kconfig dependencies (CTR, SHA1, SHA512, and HMAC)
* Cleaned up and refactored the file name and data path crypto code
* Support encrypted fast symlinks
* Lots of other cleanups suggested by reviewers (thanks, all!)
Note: this version of the patches requires e2fsprogs commit v1.42.12-522-ga672190 or newer.
Michael Halcrow (13):
ext4 crypto: export ext4_empty_dir()
ext4 crypto: add encryption xattr support
ext4 crypto: add encryption policy and password salt support ext4 crypto: add ext4 encryption facilities
ext4 crypto: add encryption key management facilities
ext4 crypto: inherit encryption policies on inode and directory create ext4 crypto: implement the ext4 encryption write path
ext4 crypto: implement the ext4 decryption read path
ext4 crypto: filename encryption facilities
ext4 crypto: insert encrypted filenames into a leaf directory block ext4 crypto: partial update to namei.c for fname crypto ext4 crypto: filename encryption modifications
ext4 crypto: enable filename encryption
Theodore Ts'o (7):
ext4 crypto: add ext4_mpage_readpages()
ext4 crypto: reserve codepoints used by the ext4 encryption feature ext4 crypto: add ext4 encryption Kconfig
ext4 crypto: validate context consistency on lookup
ext4 crypto: teach ext4_htree_store_dirent() to store decrypted
filenames
ext4 crypto: Add symlink encryption
ext4 crypto: enable encryption feature flag
fs/ext4/Kconfig | 17 ++ fs/ext4/Makefile | 4 +- fs/ext4/crypto.c | 500 ++++++++++++++++++++++++++++++++++ fs/ext4/crypto_fname.c | 709 ++++++++++++++++++++++++++++++++++++++++++++++++ fs/ext4/crypto_key.c | 165 +++++++++++ fs/ext4/crypto_policy.c | 183 +++++++++++++ fs/ext4/dir.c | 79 ++++-- fs/ext4/ext4.h | 177 +++++++++++- fs/ext4/ext4_crypto.h | 147 ++++++++++ fs/ext4/extents.c | 6 + fs/ext4/file.c | 19 +- fs/ext4/ialloc.c | 27 +- fs/ext4/inline.c | 14 +- fs/ext4/inode.c | 130 ++++++++- fs/ext4/ioctl.c | 85 ++++++ fs/ext4/namei.c | 563 ++++++++++++++++++++++++++++++++------ fs/ext4/page-io.c | 46 +++- fs/ext4/readpage.c | 328 ++++++++++++++++++++++ fs/ext4/super.c | 37 ++- fs/ext4/symlink.c | 109 +++++++- fs/ext4/xattr.h | 3 + 21 files changed, 3211 insertions(+), 137 deletions(-) create mode 100644 fs/ext4/crypto.c create mode 100644 fs/ext4/crypto_fname.c create mode 100644 fs/ext4/crypto_key.c create mode 100644 fs/ext4/crypto_policy.c create mode 100644 fs/ext4/ext4_crypto.h create mode 100644 fs/ext4/readpage.c
Signature · 1 line
2.3.0
Theodore Ts'o
maintainerPATCH v2 1/20 ext4 crypto: add ext4_mpage_readpages()
This takes code from fs/mpage.c and optimizes it for ext4. Its primary reason is to allow us to more easily add encryption to ext4's read path in an efficient manner.
--- fs/ext4/Makefile | 2 +- fs/ext4/ext4.h | 4 + fs/ext4/inode.c | 4 +- fs/ext4/readpage.c | 264 +++++++++++++++++++++++++++++++++++++++++++++++++++++ 4 files changed, 271 insertions(+), 3 deletions(-) create mode 100644 fs/ext4/readpage.c
fs/ext4/Makefile +1 -1
@@ -8,7 +8,7 @@ ext4-y := balloc.o bitmap.o dir.o file.o fsync.o ialloc.o inode.o page-io.o \ ioctl.o namei.o super.o symlink.o hash.o resize.o extents.o \ ext4_jbd2.o migrate.o mballoc.o block_validity.o move_extent.o \ mmp.o indirect.o extents_status.o xattr.o xattr_user.o \ - xattr_trusted.o inline.o + xattr_trusted.o inline.o readpage.o ext4-$(CONFIG_EXT4_FS_POSIX_ACL) += acl.o ext4-$(CONFIG_EXT4_FS_SECURITY) += xattr_security.o
fs/ext4/ext4.h +4 -0
@@ -2683,6 +2683,10 @@ static inline void ext4_set_de_type(struct super_block *sb, de->file_type = ext4_type_by_mode[(mode &S_IFMT )>>S_SHIFT ]; } +/* readpages.c */ +extern int ext4_mpage_readpages (struct address_space *mapping, +struct list_head *pages,struct page *page, +unsigned nr_pages);/* symlink.c */ extern const struct inode_operations ext4_symlink_inode_operations;
fs/ext4/inode.c +2 -2
@@ -2798,7 +2798,7 @@ static int ext4_readpage(struct file *file, struct page *page) ret =ext4_readpage_inline (inode, page);if (ret == -EAGAIN ) -return mpage_readpage (page, ext4_get_block); +return ext4_mpage_readpages (page->mapping,NULL , page,1 );return ret; }
@@ -2813,7 +2813,7 @@ ext4_readpages(struct file *file, struct address_space *mapping,if (ext4_has_inline_data (inode))return 0 ; -return mpage_readpages (mapping, pages, nr_pages, ext4_get_block); +return ext4_mpage_readpages (mapping, pages,NULL , nr_pages); } static void ext4_invalidatepage(struct page *page, unsigned int offset,
fs/ext4/readpage.c new file +264 -0
264 lines not on this page · raw message
Signature · 1 line
2.3.0
Theodore Ts'o
maintainerPATCH v2 4/20 ext4 crypto: export ext4_empty_dir()
In reply to Theodore Ts'oFrom: Michael Halcrow <[email protected]>
Required for future encryption xattr changes.
--- fs/ext4/ext4.h | 1 + fs/ext4/namei.c | 11 ++++++----- 2 files changed, 7 insertions(+), 5 deletions(-)
fs/ext4/ext4.h +1 -0
@@ -2187,6 +2187,7 @@ extern int ext4_generic_delete_entry(handle_t *handle,void *entry_buf, int buf_size, int csum_size); +extern int ext4_empty_dir (struct inode *inode);/* resize.c */ extern int ext4_group_add(struct super_block *sb,
fs/ext4/namei.c +6 -5
@@ -2450,7 +2450,7 @@ out_stop:/* * routine to check that the specified directory is empty (for rmdir) */ -static int empty_dir (struct inode *inode) +int ext4_empty_dir (struct inode *inode) {unsigned int offset;struct buffer_head *bh;
@@ -2718,7 +2718,7 @@ static int ext4_rmdir(struct inode *dir, struct dentry *dentry) goto end_rmdir; retval = -ENOTEMPTY ; -if (!empty_dir (inode)) +if (!ext4_empty_dir (inode)) goto end_rmdir; handle = ext4_journal_start(dir, EXT4_HT_DIR,
@@ -3272,7 +3272,7 @@ static int ext4_rename(struct inode *old_dir, struct dentry *old_dentry,if (S_ISDIR (old.inode->i_mode)) {if (new.inode) { retval = -ENOTEMPTY ; -if (!empty_dir (new.inode)) +if (!ext4_empty_dir (new.inode)) goto end_rename; }else { retval = -EMLINK ;
@@ -3346,8 +3346,9 @@ static int ext4_rename(struct inode *old_dir, struct dentry *old_dentry,ext4_dec_count (handle, old.dir);if (new.inode) { -/* checked empty_dir above, can't have another parent, -* ext4_dec_count() won't work for many-linked dirs */ +/* checked ext4_empty_dir above, can't have another +* parent, ext4_dec_count() won't work for many-linked +* dirs */ clear_nlink (new.inode); }else {ext4_inc_count (handle, new.dir);
Signature · 1 line
2.3.0
Theodore Ts'o
maintainerPATCH v2 5/20 ext4 crypto: add encryption xattr support
In reply to Theodore Ts'oFrom: Michael Halcrow <[email protected]>
--- fs/ext4/xattr.h | 3 +++ 1 file changed, 3 insertions(+)
fs/ext4/xattr.h +3 -0
@@ -23,6 +23,7 @@#define EXT4_XATTR_INDEX_SECURITY 6#define EXT4_XATTR_INDEX_SYSTEM 7#define EXT4_XATTR_INDEX_RICHACL 8 +#define EXT4_XATTR_INDEX_ENCRYPTION 9struct ext4_xattr_header {__le32 h_magic;/* magic number for identification */
@@ -98,6 +99,8 @@ extern const struct xattr_handler ext4_xattr_user_handler;extern const struct xattr_handler ext4_xattr_trusted_handler;extern const struct xattr_handler ext4_xattr_security_handler; +#define EXT4_XATTR_NAME_ENCRYPTION_CONTEXT "c" +extern ssize_t ext4_listxattr (struct dentry *,char *,size_t );extern int ext4_xattr_get (struct inode *,int ,const char *,void *,size_t );
Signature · 1 line
2.3.0
Theodore Ts'o
maintainerPATCH v2 9/20 ext4 crypto: validate context consistency on lookup
In reply to Theodore Ts'o--- fs/ext4/namei.c | 7 +++++++ 1 file changed, 7 insertions(+)
fs/ext4/namei.c +7 -0
@@ -1418,6 +1418,13 @@ static struct dentry *ext4_lookup(struct inode *dir, struct dentry *dentry, unsireturn ERR_PTR (-EIO ); } } +if (ext4_encrypted_inode (dir) && + !ext4_is_child_context_consistent_with_parent (dir, + dentry->d_inode)) { +printk (KERN_ERR "%s: Security warning: Inconsistent contexts\n" , + __func__); +return ERR_PTR (-EINVAL ); + }return d_splice_alias (inode, dentry); }
Signature · 1 line
2.3.0
Theodore Ts'o
maintainerPATCH v2 12/20 ext4 crypto: implement the ext4 decryption read path
In reply to Theodore Ts'oFrom: Michael Halcrow <[email protected]>
--- fs/ext4/file.c | 19 ++++++++++++++-- fs/ext4/inode.c | 8 +++++++ fs/ext4/readpage.c | 66 +++++++++++++++++++++++++++++++++++++++++++++++++++++- 3 files changed, 90 insertions(+), 3 deletions(-)
fs/ext4/file.c +17 -2
@@ -28,6 +28,7 @@#include <linux/pagevec.h> #include "ext4.h" #include "ext4_jbd2.h" +#include "ext4_crypto.h" #include "xattr.h" #include "acl.h"
@@ -200,6 +201,13 @@ static const struct vm_operations_struct ext4_file_vm_ops = {static int ext4_file_mmap (struct file *file,struct vm_area_struct *vma) { +struct inode *inode = file->f_mapping->host; + +if (ext4_encrypted_inode (inode)) { +int err =ext4_generate_encryption_key (inode); +if (err) +return 0 ; + }file_accessed (file); vma->vm_ops = &ext4_file_vm_ops;return 0 ;
@@ -212,6 +220,7 @@ static int ext4_file_open(struct inode * inode, struct file * filp)struct vfsmount *mnt = filp->f_path.mnt;struct path path;char buf[64 ], *cp; +int ret;if (unlikely (!(sbi->s_mount_flags &EXT4_MF_MNTDIR_SAMPLED ) && !(sb->s_flags &MS_RDONLY ))) {
@@ -250,11 +259,17 @@ static int ext4_file_open(struct inode * inode, struct file * filp)* writing and the journal is present */ if (filp->f_mode &FMODE_WRITE ) { -int ret =ext4_inode_attach_jinode (inode); + ret =ext4_inode_attach_jinode (inode);if (ret <0 )return ret; } -return dquot_file_open (inode, filp); + ret =dquot_file_open (inode, filp); +if (!ret &&ext4_encrypted_inode (inode)) { + ret =ext4_generate_encryption_key (inode); +if (ret) + ret = -EACCES ; + } +return ret; }/*
fs/ext4/inode.c +8 -0
@@ -39,6 +39,7 @@#include <linux/ratelimit.h> #include <linux/aio.h> #include <linux/bitops.h> +#include <linux/prefetch.h> #include "ext4_jbd2.h" #include "ext4_crypto.h"
@@ -3363,6 +3364,13 @@ static int ext4_block_zero_page_range(handle_t *handle,/* Uhhuh. Read error. Complain and punt. */ if (!buffer_uptodate (bh)) goto unlock; +if (S_ISREG (inode->i_mode) && +ext4_encrypted_inode (inode)) { +/* We expect the key to be set. */ +BUG_ON (!ext4_has_encryption_key (inode)); +BUG_ON (blocksize !=PAGE_CACHE_SIZE ); +WARN_ON_ONCE (ext4_decrypt_one (inode, page)); + } }if (ext4_should_journal_data (inode)) {BUFFER_TRACE (bh,"get write access" );
fs/ext4/readpage.c +65 -1
90 lines not on this page · raw message
Signature · 1 line
2.3.0
Theodore Ts'o
maintainerPATCH v2 14/20 ext4 crypto: teach ext4_htree_store_dirent() to store decrypted filenames
In reply to Theodore Ts'oFor encrypted directories, we need to pass in a separate parameter for the decrypted filename, since the directory entry contains the encrypted filename.
--- fs/ext4/dir.c | 15 ++++++++++----- fs/ext4/ext4.h | 5 +++-- fs/ext4/inline.c | 7 +++++-- fs/ext4/namei.c | 21 +++++++++++++++++---- 4 files changed, 35 insertions(+), 13 deletions(-)
fs/ext4/dir.c +10 -5
@@ -384,10 +384,15 @@ void ext4_htree_free_dir_info(struct dir_private_info *p)/* * Given a directory entry, enter it into the fname rb tree. +* +* When filename encryption is enabled, the dirent will hold the +* encrypted filename, while the htree will hold decrypted filename. +* The decrypted filename is passed in via ent_name. parameter. */ int ext4_htree_store_dirent (struct file *dir_file,__u32 hash,__u32 minor_hash, -struct ext4_dir_entry_2 *dirent) +struct ext4_dir_entry_2 *dirent, +struct ext4_str *ent_name) {struct rb_node **p, *parent =NULL ;struct fname *fname, *new_fn;
@@ -398,17 +403,17 @@ int ext4_htree_store_dirent(struct file *dir_file, __u32 hash, p = &info->root.rb_node;/* Create and allocate the fname structure */ - len =sizeof (struct fname ) + dirent->name_len +1 ; + len =sizeof (struct fname ) + ent_name->len +1 ; new_fn =kzalloc (len,GFP_KERNEL );if (!new_fn)return -ENOMEM ; new_fn->hash = hash; new_fn->minor_hash = minor_hash; new_fn->inode =le32_to_cpu (dirent->inode); - new_fn->name_len = dirent->name_len; + new_fn->name_len = ent_name->len; new_fn->file_type = dirent->file_type; -memcpy (new_fn->name, dirent->name, dirent->name_len); - new_fn->name[dirent->name_len] =0 ; +memcpy (new_fn->name, ent_name->name, ent_name->len); + new_fn->name[ent_name->len] =0 ;while (*p) { parent = *p;
fs/ext4/ext4.h +3 -2
@@ -2129,8 +2129,9 @@ extern int __ext4_check_dir_entry(const char *, unsigned int, struct inode *,unlikely (__ext4_check_dir_entry (__func__, __LINE__, (dir), (filp), \ (de), (bh), (buf), (size), (offset)))extern int ext4_htree_store_dirent (struct file *dir_file,__u32 hash, -__u32 minor_hash, -struct ext4_dir_entry_2 *dirent); +__u32 minor_hash, +struct ext4_dir_entry_2 *dirent, +struct ext4_str *ent_name);extern void ext4_htree_free_dir_info (struct dir_private_info *p); extern int ext4_find_dest_de(struct inode *dir, struct inode *inode, struct buffer_head *bh,
fs/ext4/inline.c +5 -2
@@ -1327,6 +1327,7 @@ int htree_inlinedir_to_tree(struct file *dir_file,struct ext4_iloc iloc;void *dir_buf =NULL ;struct ext4_dir_entry_2 fake; +struct ext4_str tmp_str; ret =ext4_get_inode_loc (inode, &iloc);if (ret)
@@ -1398,8 +1399,10 @@ int htree_inlinedir_to_tree(struct file *dir_file,continue ;if (de->inode ==0 )continue ; - err =ext4_htree_store_dirent (dir_file, - hinfo->hash, hinfo->minor_hash, de); + tmp_str.name = de->name; + tmp_str.len = de->name_len; + err =ext4_htree_store_dirent (dir_file, hinfo->hash, + hinfo->minor_hash, de, &tmp_str);if (err) { count = err; goto out;
fs/ext4/namei.c +17 -4
@@ -880,6 +880,7 @@ static int htree_dirblock_to_tree(struct file *dir_file,struct buffer_head *bh;struct ext4_dir_entry_2 *de, *top;int err =0 , count =0 ; +struct ext4_str tmp_str;dxtrace (printk (KERN_INFO "In htree dirblock_to_tree: block %lu\n" , (unsigned long )block));
@@ -906,8 +907,11 @@ static int htree_dirblock_to_tree(struct file *dir_file,continue ;if (de->inode ==0 )continue ; -if ((err =ext4_htree_store_dirent (dir_file, - hinfo->hash, hinfo->minor_hash, de)) !=0 ) { + tmp_str.name = de->name; + tmp_str.len = de->name_len; + err =ext4_htree_store_dirent (dir_file, + hinfo->hash, hinfo->minor_hash, de, &tmp_str); +if (err !=0 ) {brelse (bh);return err; }
@@ -937,6 +941,7 @@ int ext4_htree_fill_tree(struct file *dir_file, __u32 start_hash,int count =0 ;int ret, err;__u32 hashval; +struct ext4_str tmp_str;dxtrace (printk (KERN_DEBUG "In htree_fill_tree, start hash: %x:%x\n" , start_hash, start_minor_hash));
@@ -972,14 +977,22 @@ int ext4_htree_fill_tree(struct file *dir_file, __u32 start_hash,/* Add '.' and '..' from the htree header */ if (!start_hash && !start_minor_hash) { de = (struct ext4_dir_entry_2 *) frames[0 ].bh->b_data; -if ((err =ext4_htree_store_dirent (dir_file,0 ,0 , de)) !=0 ) + tmp_str.name = de->name; + tmp_str.len = de->name_len; + err =ext4_htree_store_dirent (dir_file,0 ,0 , + de, &tmp_str); +if (err !=0 ) goto errout; count++; }if (start_hash <2 || (start_hash ==2 && start_minor_hash==0 )) { de = (struct ext4_dir_entry_2 *) frames[0 ].bh->b_data; de =ext4_next_entry (de, dir->i_sb->s_blocksize); -if ((err =ext4_htree_store_dirent (dir_file,2 ,0 , de)) !=0 ) + tmp_str.name = de->name; + tmp_str.len = de->name_len; + err =ext4_htree_store_dirent (dir_file,2 ,0 , + de, &tmp_str); +if (err !=0 ) goto errout; count++; }
Signature · 1 line
2.3.0
Theodore Ts'o
maintainerPATCH v2 7/20 ext4 crypto: add ext4 encryption facilities
In reply to Theodore Ts'oFrom: Michael Halcrow <[email protected]>
On encrypt, we will re-assign the buffer_heads to point to a bounce page rather than the control_page (which is the original page to write that contains the plaintext). The block I/O occurs against the bounce page. On write completion, we re-assign the buffer_heads to the original plaintext page.
On decrypt, we will attach a read completion callback to the bio struct. This read completion will decrypt the read contents in-place prior to setting the page up-to-date.
The current encryption mode, AES-256-XTS, lacks cryptographic integrity. AES-256-GCM is in-plan, but we will need to devise a mechanism for handling the integrity data.
--- fs/ext4/Makefile | 2 +- fs/ext4/crypto.c | 500 ++++++++++++++++++++++++++++++++++++++++++++++++ fs/ext4/crypto_policy.c | 8 + fs/ext4/ext4.h | 51 +++++ fs/ext4/ext4_crypto.h | 55 ++++++ fs/ext4/super.c | 8 + 6 files changed, 623 insertions(+), 1 deletion(-) create mode 100644 fs/ext4/crypto.c
fs/ext4/Makefile +1 -1
@@ -12,4 +12,4 @@ ext4-y := balloc.o bitmap.o dir.o file.o fsync.o ialloc.o inode.o page-io.o \ ext4-$(CONFIG_EXT4_FS_POSIX_ACL) += acl.o ext4-$(CONFIG_EXT4_FS_SECURITY) += xattr_security.o -ext4-$(CONFIG_EXT4_FS_ENCRYPTION) += crypto_policy.o +ext4-$(CONFIG_EXT4_FS_ENCRYPTION) += crypto_policy.o crypto.o
fs/ext4/crypto.c new file +500 -0
500 lines not on this page · raw message
fs/ext4/crypto_policy.c +8 -0
@@ -52,6 +52,13 @@ static int ext4_create_encryption_context_from_policy( ctx.format =EXT4_ENCRYPTION_CONTEXT_FORMAT_V1 ;memcpy (ctx.master_key_descriptor, policy->master_key_descriptor,EXT4_KEY_DESCRIPTOR_SIZE ); +if (!ext4_valid_contents_enc_mode (policy->contents_encryption_mode)) { +printk (KERN_WARNING +"%s: Invalid contents encryption mode %d\n" , __func__, + policy->contents_encryption_mode); + res = -EINVAL ; + goto out; + } ctx.contents_encryption_mode = policy->contents_encryption_mode; ctx.filenames_encryption_mode = policy->filenames_encryption_mode;BUILD_BUG_ON (sizeof (ctx.nonce) !=EXT4_KEY_DERIVATION_NONCE_SIZE );
@@ -60,6 +67,7 @@ static int ext4_create_encryption_context_from_policy( res =ext4_xattr_set (inode,EXT4_XATTR_INDEX_ENCRYPTION ,EXT4_XATTR_NAME_ENCRYPTION_CONTEXT , &ctx,sizeof (ctx),0 ); +out:if (!res)ext4_set_inode_flag (inode,EXT4_INODE_ENCRYPT );return res;
fs/ext4/ext4.h +51 -0
75 lines not on this page · raw message
fs/ext4/ext4_crypto.h +55 -0
59 lines not on this page · raw message
fs/ext4/super.c +8 -0
@@ -893,6 +893,9 @@ static struct inode *ext4_alloc_inode(struct super_block *sb)atomic_set (&ei->i_ioend_count,0 );atomic_set (&ei->i_unwritten,0 );INIT_WORK (&ei->i_rsv_conversion_work, ext4_end_io_rsv_work); +#ifdef CONFIG_EXT4_FS_ENCRYPTION + ei->i_encryption_key.mode =EXT4_ENCRYPTION_MODE_INVALID ; +#endif return &ei->vfs_inode; }
@@ -3439,6 +3442,11 @@ static int ext4_fill_super(struct super_block *sb, void *data, int silent)if (sb->s_bdev->bd_part) sbi->s_sectors_written_start =part_stat_read (sb->s_bdev->bd_part, sectors[1 ]); +#ifdef CONFIG_EXT4_FS_ENCRYPTION +/* Modes of operations for file and directory encryption. */ + sbi->s_file_encryption_mode =EXT4_ENCRYPTION_MODE_AES_256_XTS ; + sbi->s_dir_encryption_mode =EXT4_ENCRYPTION_MODE_INVALID ; +#endif /* Cleanup superblock name */ for (cp = sb->s_id; (cp =strchr (cp,'/' ));)
Signature · 1 line
2.3.0
Theodore Ts'o
maintainerPATCH v2 8/20 ext4 crypto: add encryption key management facilities
In reply to Theodore Ts'oFrom: Michael Halcrow <[email protected]>
--- fs/ext4/Makefile | 2 +- fs/ext4/crypto_key.c | 162 ++++++++++++++++++++++++++++++++++++++++++++++++++ fs/ext4/ext4.h | 13 ++++ fs/ext4/ext4_crypto.h | 3 + 4 files changed, 179 insertions(+), 1 deletion(-) create mode 100644 fs/ext4/crypto_key.c
fs/ext4/Makefile +1 -1
@@ -12,4 +12,4 @@ ext4-y := balloc.o bitmap.o dir.o file.o fsync.o ialloc.o inode.o page-io.o \ ext4-$(CONFIG_EXT4_FS_POSIX_ACL) += acl.o ext4-$(CONFIG_EXT4_FS_SECURITY) += xattr_security.o -ext4-$(CONFIG_EXT4_FS_ENCRYPTION) += crypto_policy.o crypto.o +ext4-$(CONFIG_EXT4_FS_ENCRYPTION) += crypto_policy.o crypto.o crypto_key.o
fs/ext4/crypto_key.c new file +162 -0
162 lines not on this page · raw message
fs/ext4/ext4.h +13 -0
@@ -2065,6 +2065,19 @@ static inline int ext4_sb_has_crypto(struct super_block *sb) }#endif +/* crypto_key.c */ +int ext4_generate_encryption_key (struct inode *inode); + +#ifdef CONFIG_EXT4_FS_ENCRYPTION +int ext4_has_encryption_key (struct inode *inode); +#else +static inline int ext4_has_encryption_key (struct inode *inode) +{ +return 0 ; +} +#endif + +/* dir.c */ extern int __ext4_check_dir_entry(const char *, unsigned int, struct inode *, struct file *,
fs/ext4/ext4_crypto.h +3 -0
@@ -55,6 +55,9 @@ struct ext4_encryption_context {#define EXT4_AES_256_XTS_KEY_SIZE 64#define EXT4_MAX_KEY_SIZE 64 +#define EXT4_KEY_DESC_PREFIX "ext4:" +#define EXT4_KEY_DESC_PREFIX_SIZE 5 +struct ext4_encryption_key {uint32_t mode;char raw[EXT4_MAX_KEY_SIZE ];
Signature · 1 line
2.3.0
Theodore Ts'o
maintainerPATCH v2 11/20 ext4 crypto: implement the ext4 encryption write path
In reply to Theodore Ts'oFrom: Michael Halcrow <[email protected]>
Pulls block_write_begin() into fs/ext4/inode.c because it might need to do a low-level read of the existing data, in which case we need to decrypt it.
--- fs/ext4/extents.c | 6 +++ fs/ext4/ialloc.c | 5 +++ fs/ext4/inode.c | 113 +++++++++++++++++++++++++++++++++++++++++++++++++++++- fs/ext4/page-io.c | 46 +++++++++++++++++++--- 4 files changed, 164 insertions(+), 6 deletions(-)
fs/ext4/extents.c +6 -0
@@ -4922,6 +4922,12 @@ long ext4_fallocate(struct file *file, int mode, loff_t offset, loff_t len)ext4_lblk_t lblk;unsigned int blkbits = inode->i_blkbits; +/* +* TODO: We don't yet support fallocate with encrypted files. +*/ +if (ext4_encrypted_inode (inode)) +return -EOPNOTSUPP ; +/* Return error if mode is not supported */ if (mode & ~(FALLOC_FL_KEEP_SIZE |FALLOC_FL_PUNCH_HOLE |FALLOC_FL_COLLAPSE_RANGE |FALLOC_FL_ZERO_RANGE ))
fs/ext4/ialloc.c +5 -0
@@ -997,6 +997,11 @@ got: ei->i_block_group = group; ei->i_last_alloc_group = ~0 ; +/* If the directory encrypted, then we should encrypt the inode. */ +if ((S_ISDIR (mode) ||S_ISREG (mode) ||S_ISLNK (mode)) && +ext4_encrypted_inode (dir)) +ext4_set_inode_flag (inode,EXT4_INODE_ENCRYPT ); +ext4_set_inode_flags (inode);if (IS_DIRSYNC (inode))ext4_handle_sync (handle);
fs/ext4/inode.c +112 -1
154 lines not on this page · raw message
fs/ext4/page-io.c +41 -5
111 lines not on this page · raw message
Signature · 1 line
2.3.0
Theodore Ts'o
maintainerPATCH v2 13/20 ext4 crypto: filename encryption facilities
In reply to Theodore Ts'oFrom: Michael Halcrow <[email protected]>
--- fs/ext4/Makefile | 3 +- fs/ext4/crypto_fname.c | 709 ++++++++++++++++++++++++++++++++++++++++++++++++ fs/ext4/crypto_policy.c | 7 + fs/ext4/ext4.h | 41 +++ fs/ext4/ext4_crypto.h | 20 ++ 5 files changed, 779 insertions(+), 1 deletion(-) create mode 100644 fs/ext4/crypto_fname.c
fs/ext4/Makefile +2 -1
@@ -12,4 +12,5 @@ ext4-y := balloc.o bitmap.o dir.o file.o fsync.o ialloc.o inode.o page-io.o \ ext4-$(CONFIG_EXT4_FS_POSIX_ACL) += acl.o ext4-$(CONFIG_EXT4_FS_SECURITY) += xattr_security.o -ext4-$(CONFIG_EXT4_FS_ENCRYPTION) += crypto_policy.o crypto.o crypto_key.o +ext4-$(CONFIG_EXT4_FS_ENCRYPTION) += crypto_policy.o crypto.o \ + crypto_key.o crypto_fname.o
fs/ext4/crypto_fname.c new file +709 -0
709 lines not on this page · raw message
fs/ext4/crypto_policy.c +7 -0
@@ -59,6 +59,13 @@ static int ext4_create_encryption_context_from_policy( res = -EINVAL ; goto out; } +if (!ext4_valid_filenames_enc_mode (policy->filenames_encryption_mode)) { +printk (KERN_WARNING +"%s: Invalid filenames encryption mode %d\n" , __func__, + policy->filenames_encryption_mode); + res = -EINVAL ; + goto out; + } ctx.contents_encryption_mode = policy->contents_encryption_mode; ctx.filenames_encryption_mode = policy->filenames_encryption_mode;BUILD_BUG_ON (sizeof (ctx.nonce) !=EXT4_KEY_DERIVATION_NONCE_SIZE );
fs/ext4/ext4.h +41 -0
47 lines not on this page · raw message
fs/ext4/ext4_crypto.h +20 -0
@@ -104,4 +104,24 @@ static inline int ext4_encryption_key_size(int mode) return 0; } +#define EXT4_FNAME_NUM_SCATTER_ENTRIES 4 +#define EXT4_CRYPTO_BLOCK_SIZE 16 +#define EXT4_FNAME_CRYPTO_DIGEST_SIZE 32 + +struct ext4_str { +unsigned char *name; +u32 len; +}; + +struct ext4_fname_crypto_ctx { +u32 lim; +char tmp_buf[EXT4_CRYPTO_BLOCK_SIZE ]; +struct crypto_ablkcipher *ctfm; +struct crypto_hash *htfm; +struct page *workpage; +struct ext4_encryption_key key; +unsigned has_valid_key :1 ; +unsigned ctfm_key_is_ready :1 ; +}; +#endif /* _EXT4_CRYPTO_H */
Signature · 1 line
2.3.0
Theodore Ts'o
maintainerPATCH v2 15/20 ext4 crypto: insert encrypted filenames into a leaf directory block
In reply to Theodore Ts'oFrom: Michael Halcrow <[email protected]>
--- fs/ext4/ext4.h | 4 ++- fs/ext4/inline.c | 7 +++-- fs/ext4/namei.c | 81 +++++++++++++++++++++++++++++++++++++++++++++++++------- 3 files changed, 79 insertions(+), 13 deletions(-)
fs/ext4/ext4.h +3 -1
@@ -2138,9 +2138,11 @@ extern int ext4_find_dest_de(struct inode *dir, struct inode *inode, void *buf, int buf_size, const char *name, int namelen, struct ext4_dir_entry_2 **dest_de); -void ext4_insert_dentry (struct inode *inode, +int ext4_insert_dentry (struct inode *dir, +struct inode *inode,struct ext4_dir_entry_2 *de,int buf_size, +const struct qstr *iname,const char *name,int namelen);static inline void ext4_update_dx_flag (struct inode *inode) {
fs/ext4/inline.c +5 -2
@@ -11,11 +11,13 @@* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU General Public License for more details. */ + +#include <linux/fiemap.h> +#include "ext4_jbd2.h" #include "ext4.h" #include "xattr.h" #include "truncate.h" -#include <linux/fiemap.h> #define EXT4_XATTR_SYSTEM_DATA "data"#define EXT4_MIN_INLINE_DATA_SIZE ((sizeof(__le32) * EXT4_N_BLOCKS))
@@ -1014,7 +1016,8 @@ static int ext4_add_dirent_to_inline(handle_t *handle, err =ext4_journal_get_write_access (handle, iloc->bh);if (err)return err; -ext4_insert_dentry (inode, de, inline_size, name, namelen); +ext4_insert_dentry (dir, inode, de, inline_size, &dentry->d_name, + name, namelen);ext4_show_inline_dir (dir, iloc->bh, inline_start, inline_size);
fs/ext4/namei.c +71 -10
129 lines not on this page · raw message
Signature · 1 line
2.3.0
Theodore Ts'o
maintainerPATCH v2 16/20 ext4 crypto: partial update to namei.c for fname crypto
In reply to Theodore Ts'oFrom: Michael Halcrow <[email protected]>
Modifies dx_show_leaf and dx_probe to support fname encryption. Filename encryption not yet enabled.
--- fs/ext4/namei.c | 109 +++++++++++++++++++++++++++++++++++++++++++++++++++----- 1 file changed, 101 insertions(+), 8 deletions(-)
fs/ext4/namei.c +101 -8
@@ -588,8 +588,10 @@ struct statsunsigned bcount; }; -static struct stats dx_show_leaf (struct dx_hash_info *hinfo,struct ext4_dir_entry_2 *de, -int size,int show_names) +static struct stats dx_show_leaf (struct inode *dir, +struct dx_hash_info *hinfo, +struct ext4_dir_entry_2 *de, +int size,int show_names) {unsigned names =0 , space =0 ;char *base = (char *) de;
@@ -602,12 +604,80 @@ static struct stats dx_show_leaf(struct dx_hash_info *hinfo, struct ext4_dir_ent {if (show_names) { +#ifdef CONFIG_EXT4_FS_ENCRYPTION +int len; +char *name; +struct ext4_str fname_crypto_str + = {.name =NULL , .len =0 }; +struct ext4_fname_crypto_ctx *ctx =NULL ; +int res; + + name = de->name; + len = de->name_len; + ctx =ext4_get_fname_crypto_ctx (dir, +EXT4_NAME_LEN ); +if (IS_ERR (ctx)) { +printk (KERN_WARNING "Error acquiring" +" crypto ctxt--skipping crypto\n" ); + ctx =NULL ; + } +if (ctx ==NULL ) { +/* Directory is not encrypted */ +ext4fs_dirhash (de->name, + de->name_len, &h); +printk ("%*.s:(U)%x.%u " , len, + name, h.hash, + (unsigned ) ((char *) de + - base)); + }else { +/* Directory is encrypted */ + res =ext4_fname_crypto_alloc_buffer ( + ctx, de->name_len, + &fname_crypto_str); +if (res <0 ) { +printk (KERN_WARNING "Error " +"allocating crypto " +"buffer--skipping " +"crypto\n" ); +ext4_put_fname_crypto_ctx (&ctx); + ctx =NULL ; + } + res =ext4_fname_disk_to_usr (ctx, de, + &fname_crypto_str); +if (res <0 ) { +printk (KERN_WARNING "Error " +"converting filename " +"from disk to usr" +"\n" ); + name ="??" ; + len =2 ; + }else { + name = fname_crypto_str.name; + len = fname_crypto_str.len; + } + res =ext4_fname_disk_to_hash (ctx, de, + &h); +if (res <0 ) { +printk (KERN_WARNING "Error " +"converting filename " +"from disk to htree" +"\n" ); + h.hash =0xDEADBEEF ; + } +printk ("%*.s:(E)%x.%u " , len, name, + h.hash, (unsigned ) ((char *) de + - base)); +ext4_put_fname_crypto_ctx (&ctx); +ext4_fname_crypto_free_buffer ( + &fname_crypto_str); + } +#else int len = de->name_len;char *name = de->name; -while (len--)printk ("%c" , *name++);ext4fs_dirhash (de->name, de->name_len, &h); -printk (":%x.%u " , h.hash, +printk ("%*.s:%x.%u " , len, name, h.hash, (unsigned ) ((char *) de - base)); +#endif } space +=EXT4_DIR_REC_LEN (de->name_len); names++;
@@ -625,7 +695,6 @@ struct stats dx_show_entries(struct dx_hash_info *hinfo, struct inode *dir,unsigned count =dx_get_count (entries), names =0 , space =0 , i;unsigned bcount =0 ;struct buffer_head *bh; -int err;printk ("%i indexed blocks...\n" , count);for (i =0 ; i < count; i++, entries++) {
@@ -639,7 +708,8 @@ struct stats dx_show_entries(struct dx_hash_info *hinfo, struct inode *dir,continue ; stats = levels?dx_show_entries (hinfo, dir, ((struct dx_node *) bh->b_data)->entries, levels -1 ): -dx_show_leaf (hinfo, (struct ext4_dir_entry_2 *) bh->b_data, blocksize,0 ); +dx_show_leaf (dir, hinfo, (struct ext4_dir_entry_2 *) + bh->b_data, blocksize,0 ); names += stats.names; space += stats.space; bcount += stats.bcount;
@@ -689,8 +759,28 @@ dx_probe(const struct qstr *d_name, struct inode *dir,if (hinfo->hash_version <=DX_HASH_TEA ) hinfo->hash_version +=EXT4_SB (dir->i_sb)->s_hash_unsigned; hinfo->seed =EXT4_SB (dir->i_sb)->s_hash_seed; +#ifdef CONFIG_EXT4_FS_ENCRYPTION +if (d_name) { +struct ext4_fname_crypto_ctx *ctx =NULL ; +int res; + +/* Check if the directory is encrypted */ + ctx =ext4_get_fname_crypto_ctx (dir,EXT4_NAME_LEN ); +if (IS_ERR (ctx)) { + ret_err =ERR_PTR (PTR_ERR (ctx)); + goto fail; + } + res =ext4_fname_usr_to_hash (ctx, d_name, hinfo); +if (res <0 ) { + ret_err =ERR_PTR (res); + goto fail; + } +ext4_put_fname_crypto_ctx (&ctx); + } +#else if (d_name)ext4fs_dirhash (d_name->name, d_name->len, hinfo); +#endif hash = hinfo->hash;if (root->info.unused_flags &1 ) {
@@ -775,6 +865,7 @@ fail:brelse (frame->bh); frame--; } +if (ret_err ==ERR_PTR (ERR_BAD_DX_DIR ))ext4_warning (dir->i_sb, "Corrupt dir inode %lu, running e2fsck is "
@@ -1602,8 +1693,10 @@ static struct ext4_dir_entry_2 *do_split(handle_t *handle, struct inode *dir,initialize_dirent_tail (t, blocksize); } -dxtrace (dx_show_leaf (hinfo, (struct ext4_dir_entry_2 *) data1, blocksize,1 )); -dxtrace (dx_show_leaf (hinfo, (struct ext4_dir_entry_2 *) data2, blocksize,1 )); +dxtrace (dx_show_leaf (dir, hinfo, (struct ext4_dir_entry_2 *) data1, + blocksize,1 )); +dxtrace (dx_show_leaf (dir, hinfo, (struct ext4_dir_entry_2 *) data2, + blocksize,1 ));/* Which block gets the new entry? */ if (hinfo->hash >= hash2) {
Signature · 1 line
2.3.0
Theodore Ts'o
maintainerPATCH v2 19/20 ext4 crypto: Add symlink encryption
In reply to Theodore Ts'o--- fs/ext4/ext4.h | 1 + fs/ext4/ext4_crypto.h | 20 +++++++++ fs/ext4/inode.c | 5 ++- fs/ext4/namei.c | 75 ++++++++++++++++++++++++++-------- fs/ext4/symlink.c | 109 +++++++++++++++++++++++++++++++++++++++++++++++++- 5 files changed, 191 insertions(+), 19 deletions(-)
fs/ext4/ext4.h +1 -0
@@ -2220,6 +2220,7 @@ extern int ext4_group_add_blocks(handle_t *handle, struct super_block *sb,extern int ext4_trim_fs (struct super_block *,struct fstrim_range *);/* inode.c */ +int ext4_inode_is_fast_symlink (struct inode *inode);struct buffer_head *ext4_getblk (handle_t *,struct inode *,ext4_lblk_t ,int );struct buffer_head *ext4_bread (handle_t *,struct inode *,ext4_lblk_t ,int ); int ext4_get_block_write(struct inode *inode, sector_t iblock,
fs/ext4/ext4_crypto.h +20 -0
@@ -124,4 +124,24 @@ struct ext4_fname_crypto_ctx { unsigned ctfm_key_is_ready : 1; }; +/** +* For encrypted symlinks, the ciphertext length is stored at the beginning +* of the string in little-endian format. +*/ +struct ext4_encrypted_symlink_data { +__le16 len; +char encrypted_path[1 ]; +} __attribute__((__packed__ )); + +/** +* This function is used to calculate the disk space required to +* store a filename of length l in encrypted symlink format. +*/ +static inline u32 encrypted_symlink_data_len (u32 l) +{ +if (l <EXT4_CRYPTO_BLOCK_SIZE ) + l =EXT4_CRYPTO_BLOCK_SIZE ; +return (l +sizeof (struct ext4_encrypted_symlink_data ) -1 ); +} +#endif /* _EXT4_CRYPTO_H */
fs/ext4/inode.c +3 -2
@@ -143,7 +143,7 @@ static int ext4_meta_trans_blocks(struct inode *inode, int lblocks,/* * Test whether an inode is a fast symlink. */ -static int ext4_inode_is_fast_symlink (struct inode *inode) +int ext4_inode_is_fast_symlink (struct inode *inode) {int ea_blocks =EXT4_I (inode)->i_file_acl ?EXT4_CLUSTER_SIZE (inode->i_sb) >>9 :0 ;
@@ -4177,7 +4177,8 @@ struct inode *ext4_iget(struct super_block *sb, unsigned long ino) inode->i_op = &ext4_dir_inode_operations; inode->i_fop = &ext4_dir_operations; }else if (S_ISLNK (inode->i_mode)) { -if (ext4_inode_is_fast_symlink (inode)) { +if (ext4_inode_is_fast_symlink (inode) && + !ext4_encrypted_inode (inode)) { inode->i_op = &ext4_fast_symlink_inode_operations;nd_terminate_link (ei->i_data, inode->i_size,sizeof (ei->i_data) -1 );
fs/ext4/namei.c +59 -16
120 lines not on this page · raw message
fs/ext4/symlink.c +108 -1
132 lines not on this page · raw message
Signature · 1 line
2.3.0
Theodore Ts'o
maintainerPATCH v2 6/20 ext4 crypto: add encryption policy and password salt support
In reply to Theodore Ts'oFrom: Michael Halcrow <[email protected]>
--- fs/ext4/Makefile | 1 + fs/ext4/crypto_policy.c | 156 ++++++++++++++++++++++++++++++++++++++++++++++++ fs/ext4/ext4.h | 15 +++++ fs/ext4/ext4_crypto.h | 49 +++++++++++++++ fs/ext4/ioctl.c | 85 ++++++++++++++++++++++++++ 5 files changed, 306 insertions(+) create mode 100644 fs/ext4/crypto_policy.c create mode 100644 fs/ext4/ext4_crypto.h
fs/ext4/Makefile +1 -0
@@ -12,3 +12,4 @@ ext4-y := balloc.o bitmap.o dir.o file.o fsync.o ialloc.o inode.o page-io.o \ ext4-$(CONFIG_EXT4_FS_POSIX_ACL) += acl.o ext4-$(CONFIG_EXT4_FS_SECURITY) += xattr_security.o +ext4-$(CONFIG_EXT4_FS_ENCRYPTION) += crypto_policy.o
fs/ext4/crypto_policy.c new file +156 -0
156 lines not on this page · raw message
fs/ext4/ext4.h +15 -0
@@ -589,6 +589,8 @@ enum {#define EXT4_ENCRYPTION_MODE_AES_256_CBC 3#define EXT4_ENCRYPTION_MODE_AES_256_CTS 4 +#include "ext4_crypto.h" +/* * ioctl commands */
@@ -610,6 +612,9 @@ enum {#define EXT4_IOC_RESIZE_FS _IOW('f', 16, __u64)#define EXT4_IOC_SWAP_BOOT _IO('f', 17)#define EXT4_IOC_PRECACHE_EXTENTS _IO('f', 18) +#define EXT4_IOC_SET_ENCRYPTION_POLICY _IOR('f', 19, struct ext4_encryption_policy) +#define EXT4_IOC_GET_ENCRYPTION_PWSALT _IOW('f', 20, __u8[16]) +#define EXT4_IOC_GET_ENCRYPTION_POLICY _IOW('f', 21, struct ext4_encryption_policy)#if defined(__KERNEL__) && defined(CONFIG_COMPAT )/*
@@ -1999,6 +2004,16 @@ extern unsigned ext4_free_clusters_after_init(struct super_block *sb, struct ext4_group_desc *gdp);ext4_fsblk_t ext4_inode_to_goal_block (struct inode *); +/* crypto_policy.c */ +int ext4_is_child_context_consistent_with_parent (struct inode *parent, +struct inode *child); +int ext4_inherit_context (struct inode *parent,struct inode *child); +void ext4_to_hex (char *dst,char *src,size_t src_size); +int ext4_process_policy (const struct ext4_encryption_policy *policy, +struct inode *inode); +int ext4_get_policy (struct inode *inode, +struct ext4_encryption_policy *policy); +/* dir.c */ extern int __ext4_check_dir_entry(const char *, unsigned int, struct inode *, struct file *,
fs/ext4/ext4_crypto.h new file +49 -0
49 lines not on this page · raw message
fs/ext4/ioctl.c +85 -0
110 lines not on this page · raw message
Signature · 1 line
2.3.0
Theodore Ts'o
maintainerPATCH v2 10/20 ext4 crypto: inherit encryption policies on inode and directory create
In reply to Theodore Ts'oFrom: Michael Halcrow <[email protected]>
--- fs/ext4/namei.c | 16 ++++++++++++++++ 1 file changed, 16 insertions(+)
fs/ext4/namei.c +16 -0
@@ -48,6 +48,8 @@#define NAMEI_RA_BLOCKS 4#define NAMEI_RA_SIZE (NAMEI_RA_CHUNKS * NAMEI_RA_BLOCKS) +static int ext4_unlink (struct inode *dir,struct dentry *dentry); +static struct buffer_head *ext4_append (handle_t *handle,struct inode *inode,ext4_lblk_t *block)
@@ -2247,6 +2249,13 @@ retry: err =ext4_add_nondir (handle, dentry, inode);if (!err &&IS_DIRSYNC (dir))ext4_handle_sync (handle); +#ifdef CONFIG_EXT4_FS_ENCRYPTION +if (!err &&ext4_encrypted_inode (dir)) { + err =ext4_inherit_context (dir, inode); +if (err) +ext4_unlink (dir, dentry); + } +#endif }if (handle)ext4_journal_stop (handle);
@@ -2445,6 +2454,13 @@ out_clear_inode:d_instantiate (dentry, inode);if (IS_DIRSYNC (dir))ext4_handle_sync (handle); +#ifdef CONFIG_EXT4_FS_ENCRYPTION +if (ext4_encrypted_inode (dir)) { + err =ext4_inherit_context (dir, inode); +if (err) +ext4_unlink (dir, dentry); + } +#endif out_stop:if (handle)
Signature · 1 line
2.3.0
Theodore Ts'o
maintainerPATCH v2 17/20 ext4 crypto: filename encryption modifications
In reply to Theodore Ts'oFrom: Michael Halcrow <[email protected]>
Modifies htree_dirblock_to_tree, dx_make_map, ext4_match search_dir, and ext4_find_dest_de to support fname crypto. Filename encryption feature is not yet enabled at this patch.
--- fs/ext4/namei.c | 248 ++++++++++++++++++++++++++++++++++++++++++++++---------- 1 file changed, 204 insertions(+), 44 deletions(-)
fs/ext4/namei.c +204 -44
@@ -256,8 +256,9 @@ static struct dx_frame *dx_probe(const struct qstr *d_name,struct dx_hash_info *hinfo, struct dx_frame *frame);static void dx_release (struct dx_frame *frames); -static int dx_make_map (struct ext4_dir_entry_2 *de,unsigned blocksize, -struct dx_hash_info *hinfo,struct dx_map_entry map[]); +static int dx_make_map (struct inode *dir,struct ext4_dir_entry_2 *de, +unsigned blocksize,struct dx_hash_info *hinfo, +struct dx_map_entry map[]);static void dx_sort_map (struct dx_map_entry *map,unsigned count);static struct ext4_dir_entry_2 *dx_move_dirents (char *from,char *to,struct dx_map_entry *offsets,int count,unsigned blocksize);
@@ -971,7 +972,8 @@ static int htree_dirblock_to_tree(struct file *dir_file,struct buffer_head *bh;struct ext4_dir_entry_2 *de, *top;int err =0 , count =0 ; -struct ext4_str tmp_str; +struct ext4_fname_crypto_ctx *ctx =NULL ; +struct ext4_str fname_crypto_str = {.name =NULL , .len =0 }, tmp_str;dxtrace (printk (KERN_INFO "In htree dirblock_to_tree: block %lu\n" , (unsigned long )block));
@@ -983,6 +985,24 @@ static int htree_dirblock_to_tree(struct file *dir_file, top = (struct ext4_dir_entry_2 *) ((char *) de + dir->i_sb->s_blocksize -EXT4_DIR_REC_LEN (0 )); +#ifdef CONFIG_EXT4_FS_ENCRYPTION +/* Check if the directory is encrypted */ + ctx =ext4_get_fname_crypto_ctx (dir,EXT4_NAME_LEN ); +if (IS_ERR (ctx)) { + err =PTR_ERR (ctx); +brelse (bh); +return err; + } +if (ctx !=NULL ) { + err =ext4_fname_crypto_alloc_buffer (ctx,EXT4_NAME_LEN , + &fname_crypto_str); +if (err <0 ) { +ext4_put_fname_crypto_ctx (&ctx); +brelse (bh); +return err; + } + } +#endif for (; de < top; de =ext4_next_entry (de, dir->i_sb->s_blocksize)) { if (ext4_check_dir_entry(dir, NULL, de, bh, bh->b_data, bh->b_size,
@@ -991,24 +1011,52 @@ static int htree_dirblock_to_tree(struct file *dir_file,/* silently ignore the rest of the block */ break ; } +#ifdef CONFIG_EXT4_FS_ENCRYPTION + err =ext4_fname_disk_to_hash (ctx, de, hinfo); +if (err <0 ) { + count = err; + goto errout; + } +#else ext4fs_dirhash (de->name, de->name_len, hinfo); +#endif if ((hinfo->hash < start_hash) || ((hinfo->hash == start_hash) && (hinfo->minor_hash < start_minor_hash)))continue ;if (de->inode ==0 )continue ; - tmp_str.name = de->name; - tmp_str.len = de->name_len; - err =ext4_htree_store_dirent (dir_file, - hinfo->hash, hinfo->minor_hash, de, &tmp_str); +if (ctx ==NULL ) { +/* Directory is not encrypted */ + tmp_str.name = de->name; + tmp_str.len = de->name_len; + err =ext4_htree_store_dirent (dir_file, + hinfo->hash, hinfo->minor_hash, de, + &tmp_str); + }else { +/* Directory is encrypted */ + err =ext4_fname_disk_to_usr (ctx, de, + &fname_crypto_str); +if (err <0 ) { + count = err; + goto errout; + } + err =ext4_htree_store_dirent (dir_file, + hinfo->hash, hinfo->minor_hash, de, + &fname_crypto_str); + }if (err !=0 ) { -brelse (bh); -return err; + count = err; + goto errout; } count++; } +errout:brelse (bh); +#ifdef CONFIG_EXT4_FS_ENCRYPTION +ext4_put_fname_crypto_ctx (&ctx); +ext4_fname_crypto_free_buffer (&fname_crypto_str); +#endif return count; }
@@ -1141,17 +1189,33 @@ static inline int search_dirblock(struct buffer_head *bh,* Create map of hash values, offsets, and sizes, stored at end of block. * Returns number of entries mapped. */ -static int dx_make_map (struct ext4_dir_entry_2 *de,unsigned blocksize, -struct dx_hash_info *hinfo, +static int dx_make_map (struct inode *dir,struct ext4_dir_entry_2 *de, +unsigned blocksize,struct dx_hash_info *hinfo,struct dx_map_entry *map_tail) {int count =0 ;char *base = (char *) de;struct dx_hash_info h = *hinfo; +#ifdef CONFIG_EXT4_FS_ENCRYPTION +struct ext4_fname_crypto_ctx *ctx =NULL ; +int err; + + ctx =ext4_get_fname_crypto_ctx (dir,EXT4_NAME_LEN ); +if (IS_ERR (ctx)) +return PTR_ERR (ctx); +#endif while ((char *) de < base + blocksize) {if (de->name_len && de->inode) { +#ifdef CONFIG_EXT4_FS_ENCRYPTION + err =ext4_fname_disk_to_hash (ctx, de, &h); +if (err <0 ) { +ext4_put_fname_crypto_ctx (&ctx); +return err; + } +#else ext4fs_dirhash (de->name, de->name_len, &h); +#endif map_tail--; map_tail->hash = h.hash; map_tail->offs = ((char *) de - base)>>2 ;
@@ -1162,6 +1226,9 @@ static int dx_make_map(struct ext4_dir_entry_2 *de, unsigned blocksize,/* XXX: do we need to check rec_len == 0 case? -Chris */ de =ext4_next_entry (de, blocksize); } +#ifdef CONFIG_EXT4_FS_ENCRYPTION +ext4_put_fname_crypto_ctx (&ctx); +#endif return count; }
@@ -1212,57 +1279,107 @@ static void dx_insert_block(struct dx_frame *frame, u32 hash, ext4_lblk_t block)* `len <= EXT4_NAME_LEN' is guaranteed by caller. * `de != NULL' is guaranteed by caller. */ -static inline int ext4_match (int len,const char *const name, -struct ext4_dir_entry_2 * de) +static inline int ext4_match (struct ext4_fname_crypto_ctx *ctx, +struct ext4_str *fname_crypto_str, +int len,const char *const name, +struct ext4_dir_entry_2 *de) { -if (len != de->name_len) -return 0 ; +int res; +if (!de->inode)return 0 ; -return !memcmp (name, de->name, len); + +#ifdef CONFIG_EXT4_FS_ENCRYPTION +if (ctx) { +/* Directory is encrypted */ + res =ext4_fname_disk_to_usr (ctx, de, fname_crypto_str); +if (res <0 ) +return res; +if (len != res) +return 0 ; + res =memcmp (name, fname_crypto_str->name, len); +return (res ==0 ) ?1 :0 ; + } +#endif +if (len != de->name_len) +return 0 ; + res =memcmp (name, de->name, len); +return (res ==0 ) ?1 :0 ; }/* * Returns 0 if not found, -1 on failure, and 1 on success */ -int search_dir (struct buffer_head *bh, -char *search_buf, -int buf_size, -struct inode *dir, -const struct qstr *d_name, -unsigned int offset, -struct ext4_dir_entry_2 **res_dir) +int search_dir (struct buffer_head *bh,char *search_buf,int buf_size, +struct inode *dir,const struct qstr *d_name, +unsigned int offset,struct ext4_dir_entry_2 **res_dir) {struct ext4_dir_entry_2 * de;char * dlimit;int de_len;const char *name = d_name->name;int namelen = d_name->len; +struct ext4_fname_crypto_ctx *ctx =NULL ; +struct ext4_str fname_crypto_str = {.name =NULL , .len =0 }; +int res; + + ctx =ext4_get_fname_crypto_ctx (dir,EXT4_NAME_LEN ); +if (IS_ERR (ctx)) +return -1 ; + +if (ctx !=NULL ) { +/* Allocate buffer to hold maximum name length */ + res =ext4_fname_crypto_alloc_buffer (ctx,EXT4_NAME_LEN , + &fname_crypto_str); +if (res <0 ) { +ext4_put_fname_crypto_ctx (&ctx); +return -1 ; + } + } de = (struct ext4_dir_entry_2 *)search_buf; dlimit = search_buf + buf_size;while ((char *) de < dlimit) {/* this code is executed quadratically often */ /* do minimal checking `by hand' */ +if ((char *) de + de->name_len <= dlimit) { + res =ext4_match (ctx, &fname_crypto_str, namelen, + name, de); +if (res <0 ) { + res =-1 ; + goto return_result; + } +if (res >0 ) { +/* found a match - just to be sure, do +* a full check */ +if (ext4_check_dir_entry (dir,NULL , de, bh, + bh->b_data, + bh->b_size, offset)) { + res =-1 ; + goto return_result; + } + *res_dir = de; + res =1 ; + goto return_result; + } -if ((char *) de + namelen <= dlimit && -ext4_match (namelen, name, de)) { -/* found a match - just to be sure, do a full check */ -if (ext4_check_dir_entry (dir,NULL , de, bh, bh->b_data, - bh->b_size, offset)) -return -1 ; - *res_dir = de; -return 1 ; }/* prevent looping on a bad block */ de_len =ext4_rec_len_from_disk (de->rec_len, dir->i_sb->s_blocksize); -if (de_len <=0 ) -return -1 ; +if (de_len <=0 ) { + res =-1 ; + goto return_result; + } offset += de_len; de = (struct ext4_dir_entry_2 *) ((char *) de + de_len); } -return 0 ; + + res =0 ; +return_result: +ext4_put_fname_crypto_ctx (&ctx); +ext4_fname_crypto_free_buffer (&fname_crypto_str); +return res; } static int is_dx_internal_node(struct inode *dir, ext4_lblk_t block,
@@ -1451,6 +1568,9 @@ static struct buffer_head * ext4_dx_find_entry(struct inode *dir, const struct qext4_lblk_t block;int retval; +#ifdef CONFIG_EXT4_FS_ENCRYPTION + *res_dir =NULL ; +#endif frame =dx_probe (d_name, dir, &hinfo, frames);if (IS_ERR (frame))return (struct buffer_head *) frame;
@@ -1654,7 +1774,7 @@ static struct ext4_dir_entry_2 *do_split(handle_t *handle, struct inode *dir,/* create map in the end of data2 block */ map = (struct dx_map_entry *) (data2 + blocksize); - count =dx_make_map ((struct ext4_dir_entry_2 *) data1, + count =dx_make_map (dir, (struct ext4_dir_entry_2 *) data1, blocksize, hinfo, map); map -= count;dx_sort_map (map, count);
@@ -1677,7 +1797,8 @@ static struct ext4_dir_entry_2 *do_split(handle_t *handle, struct inode *dir, hash2, split, count-split));/* Fancy dance to stay within two buffers */ - de2 =dx_move_dirents (data1, data2, map + split, count - split, blocksize); + de2 =dx_move_dirents (data1, data2, map + split, count - split, + blocksize); de =dx_pack_dirents (data1, blocksize); de->rec_len = ext4_rec_len_to_disk(data1 + (blocksize - csum_size) - (char *) de,
@@ -1733,15 +1854,48 @@ int ext4_find_dest_de(struct inode *dir, struct inode *inode,int nlen, rlen;unsigned int offset =0 ;char *top; +struct ext4_fname_crypto_ctx *ctx =NULL ; +struct ext4_str fname_crypto_str = {.name =NULL , .len =0 }; +int res; + + ctx =ext4_get_fname_crypto_ctx (dir,EXT4_NAME_LEN ); +if (IS_ERR (ctx)) +return -1 ; + +if (ctx !=NULL ) { +/* Calculate record length needed to store the entry */ + res =ext4_fname_crypto_namelen_on_disk (ctx, namelen); +if (res <0 ) { +ext4_put_fname_crypto_ctx (&ctx); +return res; + } + reclen =EXT4_DIR_REC_LEN (res); + +/* Allocate buffer to hold maximum name length */ + res =ext4_fname_crypto_alloc_buffer (ctx,EXT4_NAME_LEN , + &fname_crypto_str); +if (res <0 ) { +ext4_put_fname_crypto_ctx (&ctx); +return -1 ; + } + } de = (struct ext4_dir_entry_2 *)buf; top = buf + buf_size - reclen;while ((char *) de <= top) {if (ext4_check_dir_entry (dir,NULL , de, bh, - buf, buf_size, offset)) -return -EIO ; -if (ext4_match (namelen, name, de)) -return -EEXIST ; + buf, buf_size, offset)) { + res = -EIO ; + goto return_result; + } +/* Provide crypto context and crypto buffer to ext4 match */ + res =ext4_match (ctx, &fname_crypto_str, namelen, name, de); +if (res <0 ) + goto return_result; +if (res >0 ) { + res = -EEXIST ; + goto return_result; + } nlen =EXT4_DIR_REC_LEN (de->name_len); rlen =ext4_rec_len_from_disk (de->rec_len, buf_size);if ((de->inode ? rlen - nlen : rlen) >= reclen)
@@ -1749,11 +1903,17 @@ int ext4_find_dest_de(struct inode *dir, struct inode *inode, de = (struct ext4_dir_entry_2 *)((char *)de + rlen); offset += rlen; } -if ((char *) de > top) -return -ENOSPC ; - *dest_de = de; -return 0 ; +if ((char *) de > top) + res = -ENOSPC ; +else { + *dest_de = de; + res =0 ; + } +return_result: +ext4_put_fname_crypto_ctx (&ctx); +ext4_fname_crypto_free_buffer (&fname_crypto_str); +return res; } int ext4_insert_dentry(struct inode *dir,
Signature · 1 line
2.3.0
Theodore Ts'o
maintainerPATCH v2 18/20 ext4 crypto: enable filename encryption
In reply to Theodore Ts'oFrom: Michael Halcrow <[email protected]>
--- fs/ext4/dir.c | 64 +++++++++++++++++++++++++++++++++++++++++++------------- fs/ext4/ialloc.c | 21 +++++++++++++++++-- 2 files changed, 68 insertions(+), 17 deletions(-)
fs/ext4/dir.c +49 -15
@@ -110,7 +110,10 @@ static int ext4_readdir(struct file *file, struct dir_context *ctx)int err;struct inode *inode =file_inode (file);struct super_block *sb = inode->i_sb; +struct buffer_head *bh =NULL ;int dir_has_error =0 ; +struct ext4_fname_crypto_ctx *enc_ctx =NULL ; +struct ext4_str fname_crypto_str = {.name =NULL , .len =0 };if (is_dx_dir (inode)) { err =ext4_dx_readdir (file, ctx);
@@ -127,17 +130,28 @@ static int ext4_readdir(struct file *file, struct dir_context *ctx)if (ext4_has_inline_data (inode)) {int has_inline_data =1 ; -int ret =ext4_read_inline_dir (file, ctx, + err =ext4_read_inline_dir (file, ctx, &has_inline_data);if (has_inline_data) -return ret; +return err; + } + + enc_ctx =ext4_get_fname_crypto_ctx (inode,EXT4_NAME_LEN ); +if (IS_ERR (enc_ctx)) +return PTR_ERR (enc_ctx); +if (enc_ctx) { + err =ext4_fname_crypto_alloc_buffer (enc_ctx,EXT4_NAME_LEN , + &fname_crypto_str); +if (err <0 ) { +ext4_put_fname_crypto_ctx (&enc_ctx); +return err; + } } offset = ctx->pos & (sb->s_blocksize -1 );while (ctx->pos < inode->i_size) {struct ext4_map_blocks map; -struct buffer_head *bh =NULL ; map.m_lblk = ctx->pos >>EXT4_BLOCK_SIZE_BITS (sb); map.m_len =1 ;
@@ -180,6 +194,7 @@ static int ext4_readdir(struct file *file, struct dir_context *ctx) (unsigned long long)ctx->pos); ctx->pos += sb->s_blocksize - offset;brelse (bh); + bh =NULL ;continue ; }set_buffer_verified (bh);
@@ -226,25 +241,44 @@ static int ext4_readdir(struct file *file, struct dir_context *ctx) offset +=ext4_rec_len_from_disk (de->rec_len, sb->s_blocksize);if (le32_to_cpu (de->inode)) { -if (!dir_emit (ctx, de->name, - de->name_len, -le32_to_cpu (de->inode), -get_dtype (sb, de->file_type))) { -brelse (bh); -return 0 ; +if (enc_ctx ==NULL ) { +/* Directory is not encrypted */ +if (!dir_emit (ctx, de->name, + de->name_len, +le32_to_cpu (de->inode), +get_dtype (sb, de->file_type))) + goto done; + }else { +/* Directory is encrypted */ + err =ext4_fname_disk_to_usr (enc_ctx, + de, &fname_crypto_str); +if (err <0 ) + goto errout; +if (!dir_emit (ctx, + fname_crypto_str.name, err, +le32_to_cpu (de->inode), +get_dtype (sb, de->file_type))) + goto done; } } ctx->pos +=ext4_rec_len_from_disk (de->rec_len, sb->s_blocksize); } - offset =0 ; +if ((ctx->pos < inode->i_size) && !dir_relax (inode)) + goto done;brelse (bh); -if (ctx->pos < inode->i_size) { -if (!dir_relax (inode)) -return 0 ; - } + bh =NULL ; + offset =0 ; } -return 0 ; +done: + err =0 ; +errout: +#ifdef CONFIG_EXT4_FS_ENCRYPTION +ext4_put_fname_crypto_ctx (&enc_ctx); +ext4_fname_crypto_free_buffer (&fname_crypto_str); +#endif +brelse (bh); +return err; }static inline int is_32bit_api (void )
fs/ext4/ialloc.c +18 -1
@@ -1034,11 +1034,28 @@ got:ext4_set_inode_state (inode,EXT4_STATE_NEW ); ei->i_extra_isize =EXT4_SB (sb)->s_want_extra_isize; - +#ifdef CONFIG_EXT4_FS_ENCRYPTION +if ((sbi->s_file_encryption_mode ==EXT4_ENCRYPTION_MODE_INVALID ) && + (sbi->s_dir_encryption_mode ==EXT4_ENCRYPTION_MODE_INVALID )) { + ei->i_inline_off =0 ; +if (EXT4_HAS_INCOMPAT_FEATURE (sb, +EXT4_FEATURE_INCOMPAT_INLINE_DATA )) +ext4_set_inode_state (inode, +EXT4_STATE_MAY_INLINE_DATA ); + }else { +/* Inline data and encryption are incompatible +* We turn off inline data since encryption is enabled */ + ei->i_inline_off =1 ; +if (EXT4_HAS_INCOMPAT_FEATURE (sb, +EXT4_FEATURE_INCOMPAT_INLINE_DATA )) +ext4_clear_inode_state (inode, +EXT4_STATE_MAY_INLINE_DATA ); + } +#else ei->i_inline_off =0 ;if (EXT4_HAS_INCOMPAT_FEATURE (sb,EXT4_FEATURE_INCOMPAT_INLINE_DATA ))ext4_set_inode_state (inode,EXT4_STATE_MAY_INLINE_DATA );
Theodore Ts'o
maintainerPATCH v2 20/20 ext4 crypto: enable encryption feature flag
In reply to Theodore Ts'oAlso add the test dummy encryption mode flag so we can more easily test the encryption patches using xfstests.
--- fs/ext4/crypto_key.c | 27 +++++++++++++++------------ fs/ext4/crypto_policy.c | 18 +++++++++++++++--- fs/ext4/ext4.h | 17 +++++++++++++---- fs/ext4/ialloc.c | 3 ++- fs/ext4/namei.c | 9 ++++++--- fs/ext4/super.c | 29 ++++++++++++++++++++++++++++- 6 files changed, 79 insertions(+), 24 deletions(-)
fs/ext4/crypto_key.c +15 -12
@@ -98,6 +98,7 @@ int ext4_generate_encryption_key(struct inode *inode)struct ext4_encryption_key *master_key;struct ext4_encryption_context ctx;struct user_key_payload *ukp; +struct ext4_sb_info *sbi =EXT4_SB (inode->i_sb);int res =ext4_xattr_get (inode,EXT4_XATTR_INDEX_ENCRYPTION ,EXT4_XATTR_NAME_ENCRYPTION_CONTEXT , &ctx,sizeof (ctx));
@@ -109,6 +110,20 @@ int ext4_generate_encryption_key(struct inode *inode) } res =0 ; +if (S_ISREG (inode->i_mode)) + crypt_key->mode = ctx.contents_encryption_mode; +else if (S_ISDIR (inode->i_mode) ||S_ISLNK (inode->i_mode)) + crypt_key->mode = ctx.filenames_encryption_mode; +else { +printk (KERN_ERR "ext4 crypto: Unsupported inode type.\n" ); +BUG (); + } + crypt_key->size =ext4_encryption_key_size (crypt_key->mode); +BUG_ON (!crypt_key->size); +if (DUMMY_ENCRYPTION_ENABLED (sbi)) { +memset (crypt_key->raw,0x42 ,EXT4_AES_256_XTS_KEY_SIZE ); + goto out; + }memcpy (full_key_descriptor,EXT4_KEY_DESC_PREFIX ,EXT4_KEY_DESC_PREFIX_SIZE ); sprintf(full_key_descriptor + EXT4_KEY_DESC_PREFIX_SIZE,
@@ -129,21 +144,9 @@ int ext4_generate_encryption_key(struct inode *inode) goto out; } master_key = (struct ext4_encryption_key *)ukp->data; - -if (S_ISREG (inode->i_mode)) - crypt_key->mode = ctx.contents_encryption_mode; -else if (S_ISDIR (inode->i_mode) ||S_ISLNK (inode->i_mode)) - crypt_key->mode = ctx.filenames_encryption_mode; -else { -printk (KERN_ERR "ext4 crypto: Unsupported inode type.\n" ); -BUG (); - } - crypt_key->size =ext4_encryption_key_size (crypt_key->mode); -BUG_ON (!crypt_key->size);BUILD_BUG_ON (EXT4_AES_128_ECB_KEY_SIZE !=EXT4_KEY_DERIVATION_NONCE_SIZE );BUG_ON (master_key->size !=EXT4_AES_256_XTS_KEY_SIZE ); -BUG_ON (crypt_key->size <EXT4_AES_256_CBC_KEY_SIZE ); res =ext4_derive_key_aes (ctx.nonce, master_key->raw, crypt_key->raw); out:if (keyring_key)
fs/ext4/crypto_policy.c +15 -3
@@ -158,13 +158,25 @@ int ext4_inherit_context(struct inode *parent, struct inode *child)EXT4_XATTR_NAME_ENCRYPTION_CONTEXT , &ctx, sizeof(ctx)); -if (res !=sizeof (ctx)) -return -ENOENT ; - +if (res !=sizeof (ctx)) { +if (DUMMY_ENCRYPTION_ENABLED (EXT4_SB (parent->i_sb))) { + ctx.format =EXT4_ENCRYPTION_CONTEXT_FORMAT_V1 ; + ctx.contents_encryption_mode = +EXT4_ENCRYPTION_MODE_AES_256_XTS ; + ctx.filenames_encryption_mode = +EXT4_ENCRYPTION_MODE_AES_256_CTS ; +memset (ctx.master_key_descriptor,0x42 , +EXT4_KEY_DESCRIPTOR_SIZE ); + res =0 ; + }else { + goto out; + } + }get_random_bytes (ctx.nonce,EXT4_KEY_DERIVATION_NONCE_SIZE ); res =ext4_xattr_set (child,EXT4_XATTR_INDEX_ENCRYPTION ,EXT4_XATTR_NAME_ENCRYPTION_CONTEXT , &ctx,sizeof (ctx),0 ); +out:if (!res)ext4_set_inode_flag (child,EXT4_INODE_ENCRYPT );return res;
fs/ext4/ext4.h +13 -4
@@ -1189,8 +1189,16 @@ struct ext4_super_block {/* * run-time mount flags */ -#define EXT4_MF_MNTDIR_SAMPLED 0x0001 -#define EXT4_MF_FS_ABORTED 0x0002/* Fatal error detected */ +#define EXT4_MF_MNTDIR_SAMPLED 0x0001 +#define EXT4_MF_FS_ABORTED 0x0002/* Fatal error detected */ +#define EXT4_MF_TEST_DUMMY_ENCRYPTION 0x0004 + +#ifdef CONFIG_EXT4_FS_ENCRYPTION +#define DUMMY_ENCRYPTION_ENABLED (sbi) (unlikely((sbi)->s_mount_flags & \ + EXT4_MF_TEST_DUMMY_ENCRYPTION)) +#else +#define DUMMY_ENCRYPTION_ENABLED (sbi) (0) +#endif /* Number of quota types we support */ #define EXT4_MAXQUOTAS 2
@@ -1601,8 +1609,9 @@ static inline int ext4_encrypted_inode(struct inode *inode) EXT4_FEATURE_INCOMPAT_EXTENTS| \ EXT4_FEATURE_INCOMPAT_64BIT| \ EXT4_FEATURE_INCOMPAT_FLEX_BG| \ - EXT4_FEATURE_INCOMPAT_MMP | \ - EXT4_FEATURE_INCOMPAT_INLINE_DATA) + EXT4_FEATURE_INCOMPAT_MMP | \ + EXT4_FEATURE_INCOMPAT_INLINE_DATA | \ + EXT4_FEATURE_INCOMPAT_ENCRYPT)#define EXT4_FEATURE_RO_COMPAT_SUPP (EXT4_FEATURE_RO_COMPAT_SPARSE_SUPER| \ EXT4_FEATURE_RO_COMPAT_LARGE_FILE| \ EXT4_FEATURE_RO_COMPAT_GDT_CSUM| \
fs/ext4/ialloc.c +2 -1
@@ -999,7 +999,8 @@ got:/* If the directory encrypted, then we should encrypt the inode. */ if ((S_ISDIR (mode) ||S_ISREG (mode) ||S_ISLNK (mode)) && -ext4_encrypted_inode (dir)) + (ext4_encrypted_inode (dir) || +DUMMY_ENCRYPTION_ENABLED (sbi)))ext4_set_inode_flag (inode,EXT4_INODE_ENCRYPT );ext4_set_inode_flags (inode);
fs/ext4/namei.c +6 -3
@@ -2577,7 +2577,8 @@ retry:if (!err &&IS_DIRSYNC (dir))ext4_handle_sync (handle);#ifdef CONFIG_EXT4_FS_ENCRYPTION -if (!err &&ext4_encrypted_inode (dir)) { +if (!err && (ext4_encrypted_inode (dir) || +DUMMY_ENCRYPTION_ENABLED (EXT4_SB (dir->i_sb)))) { err =ext4_inherit_context (dir, inode);if (err)ext4_unlink (dir, dentry);
@@ -2782,7 +2783,8 @@ out_clear_inode:if (IS_DIRSYNC (dir))ext4_handle_sync (handle);#ifdef CONFIG_EXT4_FS_ENCRYPTION -if (ext4_encrypted_inode (dir)) { +if (ext4_encrypted_inode (dir) || +DUMMY_ENCRYPTION_ENABLED (EXT4_SB (dir->i_sb))) { err =ext4_inherit_context (dir, inode);if (err)ext4_unlink (dir, dentry);
@@ -3187,7 +3189,8 @@ static int ext4_symlink(struct inode *dir, disk_link.len = len +1 ; disk_link.name = (char *) symname; - encryption_required =ext4_encrypted_inode (dir); + encryption_required = (ext4_encrypted_inode (dir) || +DUMMY_ENCRYPTION_ENABLED (EXT4_SB (dir->i_sb)));if (encryption_required) disk_link.len =encrypted_symlink_data_len (len) +1 ;if (disk_link.len > dir->i_sb->s_blocksize)
fs/ext4/super.c +28 -1
@@ -1136,7 +1136,7 @@ enum { Opt_commit, Opt_min_batch_time, Opt_max_batch_time, Opt_journal_dev, Opt_journal_path, Opt_journal_checksum, Opt_journal_async_commit, Opt_abort, Opt_data_journal, Opt_data_ordered, Opt_data_writeback, - Opt_data_err_abort, Opt_data_err_ignore, + Opt_data_err_abort, Opt_data_err_ignore, Opt_test_dummy_encryption, Opt_usrjquota, Opt_grpjquota, Opt_offusrjquota, Opt_offgrpjquota, Opt_jqfmt_vfsold, Opt_jqfmt_vfsv0, Opt_jqfmt_vfsv1, Opt_quota, Opt_noquota, Opt_barrier, Opt_nobarrier, Opt_err,
@@ -1223,6 +1223,7 @@ static const match_table_t tokens = { {Opt_init_itable, "init_itable"}, {Opt_noinit_itable, "noinit_itable"}, {Opt_max_dir_size_kb, "max_dir_size_kb=%u"}, + {Opt_test_dummy_encryption, "test_dummy_encryption"}, {Opt_removed, "check=none"}, /* mount option from ext2/3 */ {Opt_removed, "nocheck"}, /* mount option from ext2/3 */ {Opt_removed, "reservation"}, /* mount option from ext2/3 */
@@ -1423,6 +1424,7 @@ static const struct mount_opts { {Opt_jqfmt_vfsv0, QFMT_VFS_V0, MOPT_QFMT}, {Opt_jqfmt_vfsv1, QFMT_VFS_V1, MOPT_QFMT}, {Opt_max_dir_size_kb, 0, MOPT_GTE0}, + {Opt_test_dummy_encryption, 0, MOPT_GTE0}, {Opt_err, 0, 0} };
@@ -1593,6 +1595,15 @@ static int handle_mount_opt(struct super_block *sb, char *opt, int token, } *journal_ioprio =IOPRIO_PRIO_VALUE (IOPRIO_CLASS_BE , arg); + }else if (token == Opt_test_dummy_encryption) { +#ifdef CONFIG_EXT4_FS_ENCRYPTION + sbi->s_mount_flags |=EXT4_MF_TEST_DUMMY_ENCRYPTION ; +ext4_msg (sb,KERN_WARNING , +"Test dummy encryption mode enabled" ); +#else +ext4_msg (sb,KERN_WARNING , +"Test dummy encryption mount option ignored" ); +#endif }else if (m->flags &MOPT_DATAJ ) {if (is_remount) {if (!sbi->s_journal)
@@ -2685,11 +2696,13 @@ static struct attribute *ext4_attrs[] = {EXT4_INFO_ATTR (lazy_itable_init);EXT4_INFO_ATTR (batched_discard);EXT4_INFO_ATTR (meta_bg_resize); +EXT4_INFO_ATTR (encryption);static struct attribute *ext4_feat_attrs[] = {ATTR_LIST (lazy_itable_init),ATTR_LIST (batched_discard),ATTR_LIST (meta_bg_resize), +ATTR_LIST (encryption),NULL , };
@@ -3673,6 +3686,13 @@ static int ext4_fill_super(struct super_block *sb, void *data, int silent) goto failed_mount; } +if (EXT4_HAS_INCOMPAT_FEATURE (sb,EXT4_FEATURE_INCOMPAT_ENCRYPT ) && + es->s_encryption_level) { +ext4_msg (sb,KERN_ERR ,"Unsupported encryption level %d" , + es->s_encryption_level); + goto failed_mount; + } +if (sb->s_blocksize != blocksize) {/* Validate the filesystem blocksize */ if (!sb_set_blocksize (sb, blocksize)) {
@@ -4036,6 +4056,13 @@ no_journal: } } +if (unlikely (sbi->s_mount_flags &EXT4_MF_TEST_DUMMY_ENCRYPTION ) && + !(sb->s_flags &MS_RDONLY ) && + !EXT4_HAS_INCOMPAT_FEATURE (sb,EXT4_FEATURE_INCOMPAT_ENCRYPT )) { +EXT4_SET_INCOMPAT_FEATURE (sb,EXT4_FEATURE_INCOMPAT_ENCRYPT ); +ext4_commit_super (sb,1 ); + } +/* * Get the # of file system overhead blocks from the * superblock if present.
Signature · 1 line
2.3.0
Re: [PATCH-v2 08/20] ext4 crypto: add encryption key management facilities
In reply to Theodore Ts'oQuoted 6 lines · Theodore Ts'o
From: Michael Halcrow <[email protected]>
Change-Id I0cb5711a628554d3b05cefd15740d8346115cbaaSigned-off-by Michael Halcrow <[email protected]>Signed-off-by Ildar Muslukhov <[email protected]>Signed-off-by Theodore Ts'o <[email protected]>
Hi, hope that it is not too late for discussion minor things
I'm wondering whenever derivation key algo (AEC-CBC) is too weak because allow
attacker get master key once attacker find key for any inode in a filesystem. IMHO sane key derivation should be done via HMAC (HMACSHA256) or any other one direction procedure.
Read file connect w/o key.
Currently this is prohibited which breaks a lot of applications. For example my backup scenario looks like rsync like script which copy content of home directory to some archive. Script has no access to my keys, so encrypted files will be backed-up. This is bad. IMHO it is reasonable to allow to read content of the file even w/o key but return encrypted content (as it is stored on disk). This is very similar to what we do with filenames. AFAIU this will no break our security assumptions 'steal once' because even if attacker has access to several versions of the encrypted file this simply equals to several files with same ctx.nonce (effectively equals encrypted file with size = sum of all files)
If everybody are ok with that I'll send a draft patch.
Quoted 231 lines
--- fs/ext4/Makefile | 2 +- fs/ext4/crypto_key.c | 162 ++++++++++++++++++++++++++++++++++++++++++++++++++ fs/ext4/ext4.h | 13 ++++ fs/ext4/ext4_crypto.h | 3 + 4 files changed, 179 insertions(+), 1 deletion(-) create mode 100644 fs/ext4/crypto_key.cfs/ext4/Makefile +1
-1@@ -12,4 +12,4 @@ ext4-y := balloc.o bitmap.o dir.o file.o fsync.o ialloc.o inode.o page-io.o \ ext4-$(CONFIG_EXT4_FS_POSIX_ACL) += acl.o ext4-$(CONFIG_EXT4_FS_SECURITY) += xattr_security.o -ext4-$(CONFIG_EXT4_FS_ENCRYPTION) += crypto_policy.o crypto.o +ext4-$(CONFIG_EXT4_FS_ENCRYPTION) += crypto_policy.o crypto.o crypto_key.ofs/ext4/crypto_key.c new file +162
-0@@ -0,0 +1,162 @@ +/* +* linux/fs/ext4/crypto_key.c +* +* Copyright (C) 2015, Google, Inc. +* +* This contains encryption key functions for ext4 +* +* Written by Michael Halcrow, Ildar Muslukhov, and Uday Savagaonkar, 2015. +*/ + +#include <keys/encrypted-type.h> +#include <keys/user-type.h> +#include <linux/random.h> +#include <linux/scatterlist.h> +#include <uapi/linux/keyctl.h> + +#include "ext4.h" +#include "xattr.h" + +static void derive_crypt_complete (struct crypto_async_request *req,int rc) +{ +struct ext4_completion_result *ecr = req->data; + +if (rc == -EINPROGRESS ) +return ; + + ecr->res = rc; +complete (&ecr->completion); +} + +/** +* ext4_derive_key_aes() - Derive a key using AES-128-ECB +* @deriving_key: Encryption key used for derivatio. +* @source_key: Source key to which to apply derivation. +* @derived_key: Derived key. +* +* Return: Zero on success; non-zero otherwise. +*/ +static int ext4_derive_key_aes (char deriving_key[EXT4_AES_128_ECB_KEY_SIZE ], +char source_key[EXT4_AES_256_XTS_KEY_SIZE ], +char derived_key[EXT4_AES_256_XTS_KEY_SIZE ]) +{ +int res =0 ; +struct ablkcipher_request *req =NULL ; +DECLARE_EXT4_COMPLETION_RESULT (ecr); +struct scatterlist src_sg, dst_sg; +struct crypto_ablkcipher *tfm =crypto_alloc_ablkcipher ("ecb(aes)" ,0 , +0 ); + +if (IS_ERR (tfm)) { + res =PTR_ERR (tfm); + tfm =NULL ; + goto out; + } +crypto_ablkcipher_set_flags (tfm,CRYPTO_TFM_REQ_WEAK_KEY ); + req =ablkcipher_request_alloc (tfm,GFP_NOFS ); +if (!req) { + res = -ENOMEM ; + goto out; + } +ablkcipher_request_set_callback (req, +CRYPTO_TFM_REQ_MAY_BACKLOG |CRYPTO_TFM_REQ_MAY_SLEEP , + derive_crypt_complete, &ecr); + res =crypto_ablkcipher_setkey (tfm, deriving_key, +EXT4_AES_128_ECB_KEY_SIZE ); +if (res <0 ) + goto out; +sg_init_one (&src_sg, source_key,EXT4_AES_256_XTS_KEY_SIZE ); +sg_init_one (&dst_sg, derived_key,EXT4_AES_256_XTS_KEY_SIZE ); +ablkcipher_request_set_crypt (req, &src_sg, &dst_sg, +EXT4_AES_256_XTS_KEY_SIZE ,NULL ); + res =crypto_ablkcipher_encrypt (req); +if (res == -EINPROGRESS || res == -EBUSY ) { +BUG_ON (req->base.data != &ecr); +wait_for_completion (&ecr.completion); + res = ecr.res; + } + +out: +if (req) +ablkcipher_request_free (req); +if (tfm) +crypto_free_ablkcipher (tfm); +return res; +} + +/** +* ext4_generate_encryption_key() - generates an encryption key +* @inode: The inode to generate the encryption key for. +*/ +int ext4_generate_encryption_key (struct inode *inode) +{ +struct ext4_inode_info *ei =EXT4_I (inode); +struct ext4_encryption_key *crypt_key = &ei->i_encryption_key; +char full_key_descriptor[EXT4_KEY_DESC_PREFIX_SIZE + + (EXT4_KEY_DESCRIPTOR_SIZE *2 ) +1 ]; +struct key *keyring_key =NULL ; +struct ext4_encryption_key *master_key; +struct ext4_encryption_context ctx; +struct user_key_payload *ukp; +int res =ext4_xattr_get (inode,EXT4_XATTR_INDEX_ENCRYPTION , +EXT4_XATTR_NAME_ENCRYPTION_CONTEXT , + &ctx,sizeof (ctx)); + +if (res !=sizeof (ctx)) { +if (res >0 ) + res = -EINVAL ; + goto out; + } + res =0 ; + +memcpy (full_key_descriptor,EXT4_KEY_DESC_PREFIX , +EXT4_KEY_DESC_PREFIX_SIZE ); +sprintf (full_key_descriptor +EXT4_KEY_DESC_PREFIX_SIZE , +"%*phN" ,EXT4_KEY_DESCRIPTOR_SIZE , + ctx.master_key_descriptor); + full_key_descriptor[EXT4_KEY_DESC_PREFIX_SIZE + + (2 *EXT4_KEY_DESCRIPTOR_SIZE )] ='\0' ; + keyring_key =request_key (&key_type_logon, full_key_descriptor,NULL ); +if (IS_ERR (keyring_key)) { + res =PTR_ERR (keyring_key); + keyring_key =NULL ; + goto out; + } +BUG_ON (keyring_key->type != &key_type_logon); + ukp = ((struct user_key_payload *)keyring_key->payload.data); +if (ukp->datalen !=sizeof (struct ext4_encryption_key )) { + res = -EINVAL ; + goto out; + } + master_key = (struct ext4_encryption_key *)ukp->data; + +if (S_ISREG (inode->i_mode)) + crypt_key->mode = ctx.contents_encryption_mode; +else if (S_ISDIR (inode->i_mode) ||S_ISLNK (inode->i_mode)) + crypt_key->mode = ctx.filenames_encryption_mode; +else { +printk (KERN_ERR "ext4 crypto: Unsupported inode type.\n" ); +BUG (); + } + crypt_key->size =ext4_encryption_key_size (crypt_key->mode); +BUG_ON (!crypt_key->size); +BUILD_BUG_ON (EXT4_AES_128_ECB_KEY_SIZE != +EXT4_KEY_DERIVATION_NONCE_SIZE ); +BUG_ON (master_key->size !=EXT4_AES_256_XTS_KEY_SIZE ); +BUG_ON (crypt_key->size <EXT4_AES_256_CBC_KEY_SIZE ); + res =ext4_derive_key_aes (ctx.nonce, master_key->raw, crypt_key->raw); +out: +if (keyring_key) +key_put (keyring_key); +if (res <0 ) + crypt_key->mode =EXT4_ENCRYPTION_MODE_INVALID ; +return res; +} + +int ext4_has_encryption_key (struct inode *inode) +{ +struct ext4_inode_info *ei =EXT4_I (inode); +struct ext4_encryption_key *crypt_key = &ei->i_encryption_key; + +return (crypt_key->mode !=EXT4_ENCRYPTION_MODE_INVALID ); +}fs/ext4/ext4.h +13
-0@@ -2065,6 +2065,19 @@ static inline int ext4_sb_has_crypto(struct super_block *sb) }#endif +/* crypto_key.c */ +int ext4_generate_encryption_key (struct inode *inode); + +#ifdef CONFIG_EXT4_FS_ENCRYPTION +int ext4_has_encryption_key (struct inode *inode); +#else +static inline int ext4_has_encryption_key (struct inode *inode) +{ +return 0 ; +} +#endif + +/* dir.c */ extern int __ext4_check_dir_entry(const char *, unsigned int, struct inode *, struct file *,fs/ext4/ext4_crypto.h +3
-0@@ -55,6 +55,9 @@ struct ext4_encryption_context {#define EXT4_AES_256_XTS_KEY_SIZE 64#define EXT4_MAX_KEY_SIZE 64 +#define EXT4_KEY_DESC_PREFIX "ext4:" +#define EXT4_KEY_DESC_PREFIX_SIZE 5 +struct ext4_encryption_key {uint32_t mode;char raw[EXT4_MAX_KEY_SIZE ];Signature · 6 lines
2.3.0 -- To unsubscribe from this list: send the line "unsubscribe linux-ext4" in the body of a message to [email protected] More majordomo info at http://vger.kernel.org/majordomo-info.html
Theodore Ts'o
maintainerRe: [PATCH-v2 08/20] ext4 crypto: add encryption key management facilities
Quoted 4 lines · Dmitry Monakhov
I'm wondering whenever derivation key algo (AEC-CBC) is too weak because allow attacker get master key once attacker find key for any inode in a filesystem. IMHO sane key derivation should be done via HMAC (HMACSHA256) or any other one direction procedure.
That's not true. If the attacker finds the encryption key for an inode, and they have the nonce which is stored in file's extended attribute, what the attacker has is a single plaintext/ciphertext pair. That doesn't imply that they can get the master key; they would still need to do a brute force search on the keyspace try to find the master key.
There are certainly key derivations that do not use HMAC; for example, see RFC-4615, which we used as a model for what we are using.
Quoted 4 lines
Read file connect w/o key.
Currently this is prohibited which breaks a lot of applications. For example my backup scenario looks like rsync like script which copy content of home directory to some archive. Script has no
Quoted 8 lines
access to my keys, so encrypted files will be backed-up. This is bad. IMHO it is reasonable to allow to read content of the file even w/o key but return encrypted content (as it is stored on disk). This is very similar to what we do with filenames. AFAIU this will no break our security assumptions 'steal once' because even if attacker has access to several versions of the encrypted file this simply equals to several files with same ctx.nonce (effectively equals encrypted file with size = sum of all files)
If we let applications get the encrypted version of the files if the key is not available, I suspect _more_ applications would break, or at least behave in very surprising ways. The current behavior breaks exactly one class of applications --- backup applications.
The problem is that merely backing up the encrypted data isn't enough. It's also important to back up the encryption context, and there also has to be a way of restoring (or interpreting) the encryption context. So it's a lot more complicated, and in the future, the rules when an encryption context is allowed to be different from the parent directory's encryption context (if the parent directory is protected using encryption).
The other thing that is going to get complicated is that if one userid has access to the key, and the other userid does not, we will need to keep track of both the encrypted and unencrypted pages in the page cache. Currently we don't keep the encrypted version of the pages at all. Adding support for this is going to get messy (unless you want to only allow backup programs to use O_DIRECT).
So I can imagine a new open flag that says, "I'm a backup program, so I want to read the encrypted blocks if that is all I can get", and then also a way of exporting and importing encryption contexts, and deal with the encrypted vs. unencrypted page cache issue, I could imagine adding the ability to back up encrypted files w/o having access to the key. But it's a lot more complicated than it first appears.
- Ted
Theodore Ts'o
maintainerRe: [PATCH-v2 08/20] ext4 crypto: add encryption key management facilities
Quoted 7 lines · Theodore Ts'o
That's not true. If the attacker finds the encryption key for an inode, and they have the nonce which is stored in file's extended attribute, what the attacker has is a single plaintext/ciphertext pair. That doesn't imply that they can get the master key; they would still need to do a brute force search on the keyspace try to find the master key.
So an update, after conferring with Michael Halcrow, who set me straight. I was wrong, because I mixed up which was the deriving key and which was the source key. You're correct; if an attacker could get ahold of the per-file key, they could use the nonce to decrypt it and obtain the master key.
However, there are only two ways to determine the per-file key. The first is a ring 0 compromise, in which case it's likely they could get access to the master key, and the second is if there is a practical known-plaintext attack on AES, and the attacker has access to the block device --- and possibly a chosen-plaintext attack if the attacker can control what data is written to the file. But either way, if there is such a crypto-analytic attack on AES, then this is going to be least of the whole world's problems. :-)
There are alternatives, such as either using the master to encrypt the nonce and none+1: (AES-256-ENCRYPT(nonce) || AES-256-ENCRYPT (nonce+1)). But this will be 40% slower than what we are currently doing, which is to use AES-256 to encrypt the master key.
Or we could use an HMAC, which would be even slower yet, especially since many chips have AES acceleration, but few have SHA hardware acceleration.
So ultimately, the question is whether we want to make a change (with all of the versioning work we would need for backwards compatibility) that decreases performance, which will be especially noticeable for small files, to protect against a partial Ring 0 compromise when other Ring 0 compromise would make us be toast anyway.
- Ted
Re: [PATCH-v2 08/20] ext4 crypto: add encryption key management facilities
Quoted 15 lines · Theodore Ts'o
Quoted 7 lines · Theodore Ts'o
That's not true. If the attacker finds the encryption key for an inode, and they have the nonce which is stored in file's extended attribute, what the attacker has is a single plaintext/ciphertext pair. That doesn't imply that they can get the master key; they would still need to do a brute force search on the keyspace try to find the master key.
So an update, after conferring with Michael Halcrow, who set me straight. I was wrong, because I mixed up which was the deriving key and which was the source key. You're correct; if an attacker could get ahold of the per-file key, they could use the nonce to decrypt it and obtain the master key.
Quoted 8 lines
However, there are only two ways to determine the per-file key. The first is a ring 0 compromise, in which case it's likely they could get access to the master key, and the second is if there is a practical known-plaintext attack on AES, and the attacker has access to the block device --- and possibly a chosen-plaintext attack if the attacker can control what data is written to the file. But either way, if there is such a crypto-analytic attack on AES, then this is going to be least of the whole world's problems. :-)
Ohh. My knowledge in cryptography is very weak, but imagine same practical scenarios where attacker can find dozens of files with known content by using knowledge of inode attributes and environment. Let's consider user encrypted his encrypted chroot-environment, so attacker may try to compare file attributes (permission, size and directory nesting level) with files from distro repositories (rpm/deb)
For example let's comare two directories encrypted one and my /bin/
kvm-xfstests:~# ls -l /vdc/X/4BbchaihxJLF5D+gErB0DC/ | sort -k 5 -n | tail - -rwxr-xr-x 1 root root 202936 May 29 17:09 l51q60ZbBvtGnUl8a3y3yA -rwxr-xr-x 1 root root 219392 May 29 17:09 5NluBcuHcBAb6J6ByLUtBC -rwxr-xr-x 1 root root 235768 May 29 17:09 lrFAT0jlaLHwenJ2PqwiEA -rwxr-xr-x 1 root root 290816 May 29 17:09 P7A5KsxbBO4Dyv8ofxedhA -rwxr-xr-x 1 root root 309488 May 29 17:09 PeOSBWm54qDpEMCov6TqSC -rwxr-xr-x 1 root root 313584 May 29 17:09 TEGrdRgB2KxMFqysRtg6LB -rwxr-xr-x 1 root root 314560 May 29 17:09 e6waVwHbgdmx97A,CncgxD -rwxr-xr-x 1 root root 358072 May 29 17:09 Zz51PHoSv91wjUjn9sCypB -rwxr-xr-x 1 root root 538904 May 29 17:09 ulVnXayZZW0SdYp3fJe83B root@kvm-xfstests:~# ls -l /bin | sort -k 5 -n | tail -n10 -rwxr-xr-x 1 root root 202936 Oct 3 2014 grep -rwxr-xr-x 1 root root 219392 Dec 5 09:13 journalctl -rwxr-xr-x 1 root root 235768 Dec 5 09:13 systemd-inhibit -rwxr-xr-x 1 root root 280816 Dec 5 09:13 machinectl -rwxr-xr-x 1 root root 309488 Dec 5 09:13 loginctl -rwxr-xr-x 1 root root 313584 Dec 5 09:13 udevadm -rwxr-xr-x 1 root root 314560 Sep 5 2014 ip -rwxr-xr-x 1 root root 358072 Nov 8 2014 tar -rwxr-xr-x 1 root root 538904 Dec 5 09:13 systemctl -rwxr-xr-x 1 root root 1029624 Nov 12 2014 bash
This gives me as an attacker very good guess that l51q60ZbBvtGnUl8a3y3yA == grep and so on, So I have can try brute force attack on first block (But AFAIU it is not practical for AES-256) May be we can prevent this my tweak inode size if key is not available. For example allign i_size to fsblock which makes distro-based attack impractical. See patch attached.
Quoted 6 lines
There are alternatives, such as either using the master to encrypt the nonce and none+1: (AES-256-ENCRYPT(nonce) || AES-256-ENCRYPT (nonce+1)). But this will be 40% slower than what we are currently doing, which is to use AES-256 to encrypt the master key.
At least it would be reasonable to provide this as an mkfs/tune2fs option. I'll try to prepare patches.
Quoted 11 lines
Or we could use an HMAC, which would be even slower yet, especially since many chips have AES acceleration, but few have SHA hardware acceleration.
So ultimately, the question is whether we want to make a change (with all of the versioning work we would need for backwards compatibility) that decreases performance, which will be especially noticeable for small files, to protect against a partial Ring 0 compromise when other Ring 0 compromise would make us be toast anyway.
- Ted
fs/ext4/inode.c +4 -0
@@ -4790,6 +4790,10 @@ int ext4_getattr(struct vfsmount *mnt, struct dentry *dentry, inode =d_inode (dentry);generic_fillattr (inode, stat); +/* Tweak inode size for encrypted inodes */ +if (unlikely (ext4_encrypted_inode (inode) &&ext4_get_encryption_info (inode) == -ENOKEY )) + stat->size = (stat->size + stat->blksize -1 ) & ~(stat->blksize -1 ); +/* * If there is inline data in the inode, the inode will normally not * have data blocks allocated (it may have an external xattr block).
Re: [PATCH-v2 08/20] ext4 crypto: add encryption key management facilities
Quoted 88 lines · Dmitry Monakhov
Quoted 15 lines · Theodore Ts'o
Quoted 7 lines · Theodore Ts'o
That's not true. If the attacker finds the encryption key for an inode, and they have the nonce which is stored in file's extended attribute, what the attacker has is a single plaintext/ciphertext pair. That doesn't imply that they can get the master key; they would still need to do a brute force search on the keyspace try to find the master key.
So an update, after conferring with Michael Halcrow, who set me straight. I was wrong, because I mixed up which was the deriving key and which was the source key. You're correct; if an attacker could get ahold of the per-file key, they could use the nonce to decrypt it and obtain the master key.
Quoted 8 lines
However, there are only two ways to determine the per-file key. The first is a ring 0 compromise, in which case it's likely they could get access to the master key, and the second is if there is a practical known-plaintext attack on AES, and the attacker has access to the block device --- and possibly a chosen-plaintext attack if the attacker can control what data is written to the file. But either way, if there is such a crypto-analytic attack on AES, then this is going to be least of the whole world's problems. :-)
Ohh. My knowledge in cryptography is very weak, but imagine same practical scenarios where attacker can find dozens of files with known content by using knowledge of inode attributes and environment. Let's consider user encrypted his encrypted chroot-environment, so attacker may try to compare file attributes (permission, size and directory nesting level) with files from distro repositories (rpm/deb)
For example let's comare two directories encrypted one and my
/bin/kvm-xfstests:~# ls -l /vdc/X/4BbchaihxJLF5D+gErB0DC/ | sort -k 5 -n | tail - -rwxr-xr-x 1 root root 202936 May 29 17:09 l51q60ZbBvtGnUl8a3y3yA -rwxr-xr-x 1 root root 219392 May 29 17:09 5NluBcuHcBAb6J6ByLUtBC -rwxr-xr-x 1 root root 235768 May 29 17:09 lrFAT0jlaLHwenJ2PqwiEA -rwxr-xr-x 1 root root 290816 May 29 17:09 P7A5KsxbBO4Dyv8ofxedhA -rwxr-xr-x 1 root root 309488 May 29 17:09 PeOSBWm54qDpEMCov6TqSC -rwxr-xr-x 1 root root 313584 May 29 17:09 TEGrdRgB2KxMFqysRtg6LB -rwxr-xr-x 1 root root 314560 May 29 17:09 e6waVwHbgdmx97A,CncgxD -rwxr-xr-x 1 root root 358072 May 29 17:09 Zz51PHoSv91wjUjn9sCypB -rwxr-xr-x 1 root root 538904 May 29 17:09 ulVnXayZZW0SdYp3fJe83B root@kvm-xfstests:~# ls -l /bin | sort -k 5 -n | tail -n10 -rwxr-xr-x 1 root root 202936 Oct 3 2014 grep -rwxr-xr-x 1 root root 219392 Dec 5 09:13 journalctl -rwxr-xr-x 1 root root 235768 Dec 5 09:13 systemd-inhibit -rwxr-xr-x 1 root root 280816 Dec 5 09:13 machinectl -rwxr-xr-x 1 root root 309488 Dec 5 09:13 loginctl -rwxr-xr-x 1 root root 313584 Dec 5 09:13 udevadm -rwxr-xr-x 1 root root 314560 Sep 5 2014 ip -rwxr-xr-x 1 root root 358072 Nov 8 2014 tar -rwxr-xr-x 1 root root 538904 Dec 5 09:13 systemctl -rwxr-xr-x 1 root root 1029624 Nov 12 2014 bashThis gives me as an attacker very good guess that l51q60ZbBvtGnUl8a3y3yA == grep and so on, So I have can try brute force attack on first block (But AFAIU it is not practical for AES-256) May be we can prevent this my tweak inode size if key is not available. For example allign
i_sizeto fsblock which makes distro-based attack impractical. See patch attached.Quoted 6 lines
There are alternatives, such as either using the master to encrypt the nonce and none+1: (AES-256-ENCRYPT(nonce) || AES-256-ENCRYPT (nonce+1)). But this will be 40% slower than what we are currently doing, which is to use AES-256 to encrypt the master key.
At least it would be reasonable to provide this as an mkfs/tune2fs option. I'll try to prepare patches.
Quoted 11 lines
Or we could use an HMAC, which would be even slower yet, especially since many chips have AES acceleration, but few have SHA hardware acceleration.
So ultimately, the question is whether we want to make a change (with all of the versioning work we would need for backwards compatibility) that decreases performance, which will be especially noticeable for small files, to protect against a partial Ring 0 compromise when other Ring 0 compromise would make us be toast anyway.
- Ted
fs/ext4/inode.c +0
-0
fs/ext4/inode.c int ext4_getattr(struct vfsmount *mnt, struct dentry *dentry, · line 4796
Quoted 7 lines
fs/ext4/inode.c +4
-0@@ -4790,6 +4790,10 @@ int ext4_getattr(struct vfsmount *mnt, struct dentry *dentry, inode =d_inode (dentry);generic_fillattr (inode, stat); +/* Tweak inode size for encrypted inodes */ +if (unlikely (ext4_encrypted_inode (inode) &&ext4_get_encryption_info (inode) == -ENOKEY )) + stat->size = (stat->size + stat->blksize -1 ) & ~(stat->blksize -1 ); +
Hm. I've just realized that this patch is useless because once attacker has access to FS he likely also has access to block device so i_size is known for each inode because it is stored on disk unencrypted.
Quoted 3 lines
fs/ext4/inode.c +0
-0/* * If there is inline data in the inode, the inode will normally not * have data blocks allocated (it may have an external xattr block).
Theodore Ts'o
maintainerRe: [PATCH-v2 08/20] ext4 crypto: add encryption key management facilities
In reply to Dmitry MonakhovQuoted 6 lines · Dmitry Monakhov
This gives me as an attacker very good guess that l51q60ZbBvtGnUl8a3y3yA == grep and so on, So I have can try brute force attack on first block (But AFAIU it is not practical for AES-256) May be we can prevent this my tweak inode size if key is not available. For example allign
i_sizeto fsblock which makes distro-based attack impractical. See patch attached.
It's not practical for AES-128, let alone AES-256:
If you assume:
* Every person on the planet owns 10 computers.
* There are 7 billion people on the planet.
* Each of these computers can test 1 billion key combinations per second.
* On average, you can crack the key after testing 50% of the possibilities.
Then the earth's population can crack one AES-128 encryption key in 77,000,000,000,000,000,000,000,000 years![1]
AES-256 is 10^19 times harder. So take the 77,000,000,000,000,000,000,000,000 years and add another 19 zero's. :-)
The bottom line is trying to deny the attacker plaintext/ciphertext pairs really isn't worth the effort. It's assumed the attacker can do this, and it really doesn't bother me. After all, the the per-inode key is a completely random 256 bit key.
The much more concerning attack is one where the attacker tries to attack the user's passphrase by trying brute force the user's password. We're using a pbkdf2_sha512 with an iteration count of 65535, to try and slow down the brute force attack, but if the user is using the typically horrendous user-chosen password, it's still going to be the weakest link.
So the attacker will simply use a password link, try all lower-case passwords, all lower case passwords with a single digit, etc., etc., turn that into a master key, try to use the master key and the nonce to create the per-inode key, and then see if the resulting file or filename looks plausible. The fact that it will take 65535 iterations of SHA-512 per passphrase tried will slow the attacker down somewhat, but if the user uses a birthday, or their girlfriend's name, etc., it's not going to help enough.
Quoted 2 lines
At least it would be reasonable to provide this as an mkfs/tune2fs option.
I'd really rather not support adding extra complexity unless it's very clear what is the specific threat that we are protecting about, and we're clear that it is a valid threat in the context of the overall system. Otherwise we may be strengthening the titanium/steel door while ignoring the paper maiche walls that it is set in.
(Or see the image on slide #4 of:
http://kernsec.org/files/lss2014/Halcrow_EXT4_Encryption.pdf :-)
- Ted