Skip to contentSkip to conversation

eCryptfs: enhancing eCryptfs to be used with external crypto engine

PATCH New Andrey Markovytch 2 participants 3 messages Nov 8, 2015 Ecrypt file system

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 eCryptfs: enhancing eCryptfs to be used with external crypto engine

Currently eCryptfs is responsible for page encryption/decryption. This approach will not work when there is HW inline encryption. The proposed change allows external module to register with eCryptfs and provide alternative encryption mechanism and also decide whether encryption should be performed at all, or deferred to a later stage via the inline HW engine.
Additional cipher option was introduced to support the HW/external mode under that name of "aes-xts". If no external module has registered to support this cipher, eCryptfs will fall back to the usual "aes"

---
 fs/ecryptfs/Makefile          |   4 +-
 fs/ecryptfs/caches_utils.c    |  78 +++++++++
 fs/ecryptfs/crypto.c          | 200 +++++++++++++++++++----
 fs/ecryptfs/debug.c           |  13 ++
 fs/ecryptfs/ecryptfs_kernel.h |  78 +++++++++
 fs/ecryptfs/events.c          | 361 ++++++++++++++++++++++++++++++++++++++++++
 fs/ecryptfs/file.c            |  36 +++++
 fs/ecryptfs/inode.c           |  11 ++
 fs/ecryptfs/keystore.c        | 101 ++++++++----
 fs/ecryptfs/main.c            |  60 +++++--
 fs/ecryptfs/mmap.c            |   6 +
 fs/ecryptfs/super.c           |  14 +-
 include/linux/ecryptfs.h      |  47 ++++++
 13 files changed, 940 insertions(+), 69 deletions(-)
 create mode 100644 fs/ecryptfs/caches_utils.c
 create mode 100644 fs/ecryptfs/events.c
fs/ecryptfs/Makefile +2 -2
index 49678a6..995719c 100644
@@ -4,7 +4,7 @@
 
 obj-$(CONFIG_ECRYPT_FS) += ecryptfs.o
 
-ecryptfs-y := dentry.o file.o inode.o main.o super.o mmap.o read_write.o \
-	      crypto.o keystore.o kthread.o debug.o
+ecryptfs-y := dentry.o file.o inode.o main.o super.o mmap.o read_write.o events.o \
+	      crypto.o keystore.o kthread.o debug.o caches_utils.o
 
 ecryptfs-$(CONFIG_ECRYPT_FS_MESSAGING) += messaging.o miscdev.o
fs/ecryptfs/caches_utils.c new file +78 -0

78 lines not on this page · raw message

fs/ecryptfs/crypto.c +173 -27

333 lines not on this page · raw message

fs/ecryptfs/debug.c +13 -0
index 3d2bdf5..2b60137 100644
@@ -119,3 +119,16 @@ void ecryptfs_dump_hex(char *data, int bytes)
 		printk("\n");
 }
 
+void ecryptfs_dump_salt_hex(char *data, int key_size, char *cipher)
+{
+	size_t salt_size = ecryptfs_get_salt_size_for_cipher(cipher);
+
+	if (salt_size == 0)
+		return;
+
+	if (!ecryptfs_check_space_for_salt(key_size, salt_size))
+		return;
+
+	ecryptfs_printk(KERN_DEBUG, "Decrypted session salt key:\n");
+	ecryptfs_dump_hex(data + key_size, salt_size);
+}
fs/ecryptfs/ecryptfs_kernel.h +78 -0

112 lines not on this page · raw message

fs/ecryptfs/events.c new file +361 -0

361 lines not on this page · raw message

fs/ecryptfs/file.c +36 -0
index feef8a9..c346c9e 100644
@@ -31,6 +31,7 @@
 #include <linux/security.h>
 #include <linux/compat.h>
 #include <linux/fs_stack.h>
+#include <linux/ecryptfs.h>
 #include "ecryptfs_kernel.h"
 
 /**
@@ -184,6 +185,9 @@ static int ecryptfs_open(struct inode *inode, struct file *file)
 	int rc = 0;
 	struct ecryptfs_crypt_stat *crypt_stat = NULL;
 	struct dentry *ecryptfs_dentry = file->f_path.dentry;
+	int ret;
+
+
 	/* Private value of ecryptfs_dentry allocated in
 	 * ecryptfs_lookup() */
 	struct ecryptfs_file_info *file_info;
@@ -231,12 +235,31 @@ static int ecryptfs_open(struct inode *inode, struct file *file)
 		rc = 0;
 		goto out;
 	}
+
 	rc = read_or_initialize_metadata(ecryptfs_dentry);
 	if (rc)
 		goto out_put;
 	ecryptfs_printk(KERN_DEBUG, "inode w/ addr = [0x%p], i_ino = "
 			"[0x%.16lx] size: [0x%.16llx]\n", inode, inode->i_ino,
 			(unsigned long long)i_size_read(inode));
+
+	if (get_events() && get_events()->open_cb) {
+
+		ret = vfs_fsync(file, false);
+
+		if (ret)
+			ecryptfs_printk(KERN_ERR,
+				"failed to sync file ret = %d.\n", ret);
+
+		get_events()->open_cb(ecryptfs_inode_to_lower(inode),
+			crypt_stat);
+
+		if (crypt_stat->flags & ECRYPTFS_METADATA_IN_XATTR) {
+			truncate_inode_pages(inode->i_mapping, 0);
+			truncate_inode_pages(
+				ecryptfs_inode_to_lower(inode)->i_mapping, 0);
+		}
+	}
 	goto out;
 out_put:
 	ecryptfs_put_lower_file(inode);
@@ -261,9 +284,22 @@ static int ecryptfs_flush(struct file *file, fl_owner_t td)
 
 static int ecryptfs_release(struct inode *inode, struct file *file)
 {
+
+	int ret;
+
+	ret = vfs_fsync(file, false);
+
+	if (ret)
+		pr_err("failed to sync file ret = %d.\n", ret);
+
 	ecryptfs_put_lower_file(inode);
 	kmem_cache_free(ecryptfs_file_info_cache,
 			ecryptfs_file_to_private(file));
+
+	clean_inode_pages(inode->i_mapping, 0, -1);
+	clean_inode_pages(ecryptfs_inode_to_lower(inode)->i_mapping, 0, -1);
+	truncate_inode_pages(inode->i_mapping, 0);
+	truncate_inode_pages(ecryptfs_inode_to_lower(inode)->i_mapping, 0);
 	return 0;
 }
 
fs/ecryptfs/inode.c +11 -0
index 3c4db11..e0d72e7 100644
@@ -261,12 +261,15 @@ out:
  *
  * Returns zero on success; non-zero on error condition
  */
+
+
 static int
 ecryptfs_create(struct inode *directory_inode, struct dentry *ecryptfs_dentry,
 		umode_t mode, bool excl)
 {
 	struct inode *ecryptfs_inode;
 	int rc;
+	struct ecryptfs_crypt_stat *crypt_stat;
 
 	ecryptfs_inode = ecryptfs_do_create(directory_inode, ecryptfs_dentry,
 					    mode);
@@ -276,6 +279,7 @@ ecryptfs_create(struct inode *directory_inode, struct dentry *ecryptfs_dentry,
 		rc = PTR_ERR(ecryptfs_inode);
 		goto out;
 	}
+
 	/* At this point, a file exists on "disk"; we need to make sure
 	 * that this on disk file is prepared to be an ecryptfs file */
 	rc = ecryptfs_initialize_file(ecryptfs_dentry, ecryptfs_inode);
@@ -288,6 +292,13 @@ ecryptfs_create(struct inode *directory_inode, struct dentry *ecryptfs_dentry,
 		goto out;
 	}
 	unlock_new_inode(ecryptfs_inode);
+
+	crypt_stat = &ecryptfs_inode_to_private(ecryptfs_inode)->crypt_stat;
+	if (get_events() && get_events()->open_cb)
+		get_events()->open_cb(
+				ecryptfs_inode_to_lower(ecryptfs_inode),
+					crypt_stat);
+
 	d_instantiate(ecryptfs_dentry, ecryptfs_inode);
 out:
 	return rc;
fs/ecryptfs/keystore.c +72 -29

251 lines not on this page · raw message

fs/ecryptfs/main.c +50 -10

123 lines not on this page · raw message

fs/ecryptfs/mmap.c +6 -0
index caba848..bdbc72d 100644
@@ -552,10 +552,16 @@ static sector_t ecryptfs_bmap(struct address_space *mapping, sector_t block)
 	return rc;
 }
 
+void ecryptfs_freepage(struct page *page)
+{
+	zero_user(page, 0, PAGE_CACHE_SIZE);
+}
+
 const struct address_space_operations ecryptfs_aops = {
 	.writepage = ecryptfs_writepage,
 	.readpage = ecryptfs_readpage,
 	.write_begin = ecryptfs_write_begin,
 	.write_end = ecryptfs_write_end,
 	.bmap = ecryptfs_bmap,
+	.freepage = ecryptfs_freepage,
 };
fs/ecryptfs/super.c +13 -1
index afa1b81..25e436d 100644
@@ -69,6 +69,9 @@ static void ecryptfs_i_callback(struct rcu_head *head)
 {
 	struct inode *inode = container_of(head, struct inode, i_rcu);
 	struct ecryptfs_inode_info *inode_info;
+	if (inode == NULL)
+		return;
+
 	inode_info = ecryptfs_inode_to_private(inode);
 
 	kmem_cache_free(ecryptfs_inode_info_cache, inode_info);
@@ -88,9 +91,12 @@ static void ecryptfs_destroy_inode(struct inode *inode)
 	struct ecryptfs_inode_info *inode_info;
 
 	inode_info = ecryptfs_inode_to_private(inode);
+
 	BUG_ON(inode_info->lower_file);
+
 	ecryptfs_destroy_crypt_stat(&inode_info->crypt_stat);
 	call_rcu(&inode->i_rcu, ecryptfs_i_callback);
+
 }
 
 /**
@@ -149,6 +155,9 @@ static int ecryptfs_show_options(struct seq_file *m, struct dentry *root)
 	struct ecryptfs_mount_crypt_stat *mount_crypt_stat =
 		&ecryptfs_superblock_to_private(sb)->mount_crypt_stat;
 	struct ecryptfs_global_auth_tok *walker;
+	unsigned char final[2*ECRYPTFS_MAX_CIPHER_NAME_SIZE+1];
+
+	memset(final, 0, sizeof(final));
 
 	mutex_lock(&mount_crypt_stat->global_auth_tok_list_mutex);
 	list_for_each_entry(walker,
@@ -162,7 +171,10 @@ static int ecryptfs_show_options(struct seq_file *m, struct dentry *root)
 	mutex_unlock(&mount_crypt_stat->global_auth_tok_list_mutex);
 
 	seq_printf(m, ",ecryptfs_cipher=%s",
-		mount_crypt_stat->global_default_cipher_name);
+			ecryptfs_get_full_cipher(
+				mount_crypt_stat->global_default_cipher_name,
+				mount_crypt_stat->global_default_cipher_mode,
+				final, sizeof(final)));
 
 	if (mount_crypt_stat->global_default_cipher_key_size)
 		seq_printf(m, ",ecryptfs_key_bytes=%zd",
include/linux/ecryptfs.h +47 -0

63 lines not on this page · raw message

Signature · 3 lines
Qualcomm Israel, on behalf of Qualcomm Innovation Center, Inc.
The Qualcomm Innovation Center, Inc. is a member of the Code Aurora Forum,
a Linux Foundation Collaborative Project
#1 lore raw
reproduce:

Hi Andrey,

[auto build test ERROR on: v4.3-rc7]
[also build test ERROR on: next-20151106]

url:    https://github.com/0day-ci/linux/commits/Andrey-Markovytch/eCryptfs-enhancing-eCryptfs-to-be-used-with-external-crypto-engine/20151108-161722
config: x86_64-randconfig-s0-11081650 (attached as .config)
reproduce:
        # save the attached .config to linux build tree
        make ARCH=x86_64 

All errors (new ones prefixed by >>):

>> ERROR: "__iget" [fs/ecryptfs/ecryptfs.ko] undefined!

---
0-DAY kernel test infrastructure Open Source Technology Center
https://lists.01.org/pipermail/kbuild-all Intel Corporation

#2 lore raw
In reply to Andrey Markovytch
reproduce:

Hi Andrey,

[auto build test ERROR on: v4.3-rc7]
[also build test ERROR on: next-20151106]

url:    https://github.com/0day-ci/linux/commits/Andrey-Markovytch/eCryptfs-enhancing-eCryptfs-to-be-used-with-external-crypto-engine/20151108-161722
config: i386-allmodconfig (attached as .config)
reproduce:
        # save the attached .config to linux build tree
        make ARCH=i386 

All errors (new ones prefixed by >>):

>> ERROR: "__iget" undefined!

---
0-DAY kernel test infrastructure Open Source Technology Center
https://lists.01.org/pipermail/kbuild-all Intel Corporation