eCryptfs: enhancing eCryptfs to be used with external crypto engine
PATCH 1/1 eCryptfs: enhancing eCryptfs to be used with external crypto engine
From: Andrey Markovytch <[email protected]>
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
@@ -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
@@ -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
@@ -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
@@ -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
@@ -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
@@ -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
fs/ecryptfs/caches_utils.c line 2
Quoted 4 lines · Andrey Markovytch
+++
b/fs/ecryptfs/caches_utils.cfs/ecryptfs/caches_utils.c new file +2
-0@@ -0,0 +1,78 @@ +/* +* Copyright (c) 2015, The Linux Foundation. All rights reserved.
Really? This looks like copy and paste from core code that defintively was not written by the Linux Foundation. In addition this patch comes from Qualcomm so something very fishy is going on here, which if I'd call copyrght fraud if I'd want to be be mean.
Please a) stop pointlessly copy and pasting code and b) have a word with your lawyers on how to attribute code both that your wrote and which has been copy and pasted.
Quoted 11 lines · Andrey Markovytch
fs/ecryptfs/caches_utils.c line 2
Quoted 4 lines
+++
b/fs/ecryptfs/caches_utils.cfs/ecryptfs/caches_utils.c new file +2
-0@@ -0,0 +1,78 @@ +/* +* Copyright (c) 2015, The Linux Foundation. All rights reserved. Really? This looks like copy and paste from core code that defintively was not written by the Linux Foundation. In addition this patch comes from Qualcomm so something very fishy is going on here, which if I'd call copyrght fraud if I'd want to be be mean.
Quoted 4 lines
Please a) stop pointlessly copy and pasting code and b) have a word with your lawyers on how to attribute code both that your wrote and which has been copy and pasted.
Hi Christoph,
Regarding the license, after double checking this, it seems that there has been a mistake and we unproperly attributed Linux copyright for the code that actually came from open source files having a different license. I appologise for that, I am checking this internally and will update patch with proper license.
As for the 'pointless copy paste', the code was taken from files where those functions are declared as internal static, we had no choise but to copy paste them, other option would be alternating the original code and make those 2 functions public which seems unjustified in this case.
Hello Andrey!
Quoted 11 lines · Andrey Markovytch
From: Andrey Markovytch <[email protected]>
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"
What is "HW/external mode"? There's no description in the commit message and ecryptfs_register_to_events() does not have a caller so I'm not sure of the purpose. Please provide more context.
Despite not yet understanding the purpose of this patch, I think that I can safely say that "aes-xts" is not an appropriate mount option to use when enabling this mode. eCryptfs may support XTS mode one day, using the Crypto API, so "HW/external mode" should not own the mount option.
Tyler
Quoted 1,659 lines
Signed-off-by Lina Zarivach <[email protected]>Signed-off-by Andrey Markovytch <[email protected]>--- 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.cfs/ecryptfs/Makefile +2
-2@@ -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.ofs/ecryptfs/caches_utils.c new file +78
-0@@ -0,0 +1,78 @@ +/* +* Copyright (c) 2015, The Linux Foundation. All rights reserved. +* +* This program is free software; you can redistribute it and/or modify +* it under the terms of the GNU General Public License version 2 and +* only version 2 as published by the Free Software Foundation. +* +* This program is distributed in the hope that it will be useful, +* but WITHOUT ANY WARRANTY; without even the implied warranty of +* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +* GNU General Public License for more details. +*/ + +#include <linux/kernel.h> +#include <linux/fs.h> +#include <linux/spinlock.h> +#include <linux/pagemap.h> +#include <linux/pagevec.h> + +#include "../internal.h" + +void ecryptfs_drop_pagecache_sb (struct super_block *sb,void *unused) +{ +struct inode *inode, *toput_inode =NULL ; + +spin_lock (&sb->s_inode_list_lock); +list_for_each_entry (inode, &sb->s_inodes, i_sb_list) { +spin_lock (&inode->i_lock); +if ((inode->i_state & (I_FREEING |I_WILL_FREE |I_NEW )) || + (inode->i_mapping->nrpages ==0 )) { +spin_unlock (&inode->i_lock); +continue ; + } +__iget (inode); +spin_unlock (&inode->i_lock); +spin_unlock (&sb->s_inode_list_lock); + +invalidate_mapping_pages (inode->i_mapping,0 ,-1 ); +iput (toput_inode); + toput_inode = inode; + +spin_lock (&sb->s_inode_list_lock); + } +spin_unlock (&sb->s_inode_list_lock); +iput (toput_inode); +} + +void clean_inode_pages (struct address_space *mapping, +pgoff_t start,pgoff_t end) +{ +struct pagevec pvec; +pgoff_t index = start; +int i; + +pagevec_init (&pvec,0 ); +while (index <= end &&pagevec_lookup (&pvec, mapping, index, +min (end - index, + (pgoff_t )PAGEVEC_SIZE -1 ) +1 )) { +for (i =0 ; i <pagevec_count (&pvec); i++) { +struct page *page = pvec.pages[i]; + +/* We rely upon deletion +* not changing page->index +*/ + index = page->index; +if (index > end) +break ; +if (!trylock_page (page)) +continue ; +WARN_ON (page->index != index); +zero_user (page,0 ,PAGE_CACHE_SIZE ); +unlock_page (page); + } +pagevec_release (&pvec); +cond_resched (); + index++; + } +}fs/ecryptfs/crypto.c +173
-27@@ -35,6 +35,7 @@#include <linux/scatterlist.h> #include <linux/slab.h> #include <asm/unaligned.h> +#include <linux/ecryptfs.h> #include "ecryptfs_kernel.h" #define DECRYPT 0@@ -350,9 +351,9 @@ static int crypt_scatterlist(struct ecryptfs_crypt_stat *crypt_stat, || !(crypt_stat->flags & ECRYPTFS_STRUCT_INITIALIZED));if (unlikely (ecryptfs_verbosity >0 )) {ecryptfs_printk (KERN_DEBUG ,"Key size [%zd]; key:\n" , - crypt_stat->key_size); +ecryptfs_get_key_size_to_enc_data (crypt_stat));ecryptfs_dump_hex (crypt_stat->key, - crypt_stat->key_size); +ecryptfs_get_key_size_to_enc_data (crypt_stat)); }init_completion (&ecr.completion);@@ -371,7 +372,7 @@ static int crypt_scatterlist(struct ecryptfs_crypt_stat *crypt_stat,/* Consider doing this once, when the file is opened */ if (!(crypt_stat->flags &ECRYPTFS_KEY_SET )) { rc =crypto_ablkcipher_setkey (crypt_stat->tfm, crypt_stat->key, - crypt_stat->key_size); +ecryptfs_get_key_size_to_enc_data (crypt_stat));if (rc) { ecryptfs_printk(KERN_ERR, "Error setting key; rc = [%d]\n",@@ -466,6 +467,31 @@ out:return rc; } +static void init_ecryption_parameters (bool *hw_crypt,bool *cipher_supported, +struct ecryptfs_crypt_stat *crypt_stat) +{ +unsigned char final[2 *ECRYPTFS_MAX_CIPHER_NAME_SIZE +1 ]; + +if (!hw_crypt || !cipher_supported) +return ; + + *cipher_supported = false; + *hw_crypt = false; + +if (get_events () &&get_events ()->is_cipher_supported_cb) { + *cipher_supported = +get_events ()->is_cipher_supported_cb ( +ecryptfs_get_full_cipher (crypt_stat->cipher, + crypt_stat->cipher_mode, final,sizeof (final))); +if (*cipher_supported) { +/* we should apply external algorythm +* assume that is_hw_crypt() cbck is supplied +*/ + *hw_crypt =get_events ()->is_hw_crypt_cb (); + } + } +} +/** * ecryptfs_encrypt_page * @page: Page mapped from the eCryptfs inode for the file; contains @@ -491,11 +517,18 @@ int ecryptfs_encrypt_page(struct page *page)loff_t extent_offset;loff_t lower_offset;int rc =0 ; +bool is_hw_crypt; +bool is_cipher_supported; + ecryptfs_inode = page->mapping->host; crypt_stat = &(ecryptfs_inode_to_private (ecryptfs_inode)->crypt_stat);BUG_ON (!(crypt_stat->flags &ECRYPTFS_ENCRYPTED )); + +init_ecryption_parameters (&is_hw_crypt, + &is_cipher_supported, crypt_stat); + enc_extent_page =alloc_page (GFP_USER );if (!enc_extent_page) { rc = -ENOMEM ;@@ -503,24 +536,51 @@ int ecryptfs_encrypt_page(struct page *page) "encrypted extent\n"); goto out; } - -for (extent_offset =0 ; - extent_offset < (PAGE_CACHE_SIZE / crypt_stat->extent_size); - extent_offset++) { - rc =crypt_extent (crypt_stat, enc_extent_page, page, - extent_offset,ENCRYPT ); -if (rc) { -printk (KERN_ERR "%s: Error encrypting extent; " -"rc = [%d]\n" , __func__, rc); - goto out; - } +if (is_hw_crypt) { +/* no need for encryption */ + }else { +for (extent_offset =0 ; + extent_offset < + (PAGE_CACHE_SIZE / crypt_stat->extent_size); + extent_offset++) { + +if (is_cipher_supported) { +if (!get_events ()->encrypt_cb) { + rc = -EPERM ; + goto out; + } + rc =get_events ()->encrypt_cb (page, + enc_extent_page, +ecryptfs_inode_to_lower ( + ecryptfs_inode), + extent_offset); + }else { + rc =crypt_extent (crypt_stat, + enc_extent_page, page, + extent_offset,ENCRYPT ); + } +if (rc) { +ecryptfs_printk (KERN_ERR , +"%s: Error encrypting; rc = [%d]\n" , + __func__, rc); + goto out; + } + } } lower_offset =lower_offset_for_page (crypt_stat, page); - enc_extent_virt =kmap (enc_extent_page); +if (is_hw_crypt) + enc_extent_virt =kmap (page); +else + enc_extent_virt =kmap (enc_extent_page); + rc =ecryptfs_write_lower (ecryptfs_inode, enc_extent_virt, lower_offset,PAGE_CACHE_SIZE ); -kunmap (enc_extent_page); +if (!is_hw_crypt) +kunmap (enc_extent_page); +else +kunmap (page); +if (rc <0 ) { ecryptfs_printk(KERN_ERR, "Error attempting to write lower page; rc = [%d]\n",@@ -559,6 +619,8 @@ int ecryptfs_decrypt_page(struct page *page)unsigned long extent_offset;loff_t lower_offset;int rc =0 ; +bool is_cipher_supported; +bool is_hw_crypt; ecryptfs_inode = page->mapping->host; crypt_stat =@@ -577,13 +639,33 @@ int ecryptfs_decrypt_page(struct page *page) goto out; } +init_ecryption_parameters (&is_hw_crypt, + &is_cipher_supported, crypt_stat); + +if (is_hw_crypt) { + rc =0 ; +return rc; + } +for (extent_offset =0 ; extent_offset < (PAGE_CACHE_SIZE / crypt_stat->extent_size); extent_offset++) { - rc =crypt_extent (crypt_stat, page, page, +if (is_cipher_supported) { +if (!get_events ()->decrypt_cb) { + rc = -EPERM ; + goto out; + } + + rc =get_events ()->decrypt_cb (page, page, +ecryptfs_inode_to_lower (ecryptfs_inode), + extent_offset); + + }else + rc =crypt_extent (crypt_stat, page, page, extent_offset,DECRYPT ); +if (rc) { -printk (KERN_ERR "%s: Error encrypting extent; " +ecryptfs_printk (KERN_ERR ,"%s: Error decrypting extent;" "rc = [%d]\n" , __func__, rc); goto out; }@@ -612,7 +694,7 @@ int ecryptfs_init_crypt_ctx(struct ecryptfs_crypt_stat *crypt_stat)"Initializing cipher [%s]; strlen = [%d]; " "key_size_bits = [%zd]\n" , crypt_stat->cipher, (int )strlen (crypt_stat->cipher), - crypt_stat->key_size << 3); + ecryptfs_get_key_size_to_enc_data(crypt_stat) << 3);mutex_lock (&crypt_stat->cs_tfm_mutex);if (crypt_stat->tfm) { rc =0 ;@@ -694,7 +776,7 @@ int ecryptfs_compute_root_iv(struct ecryptfs_crypt_stat *crypt_stat) goto out; } rc =ecryptfs_calculate_md5 (dst, crypt_stat, crypt_stat->key, - crypt_stat->key_size); +ecryptfs_get_key_size_to_enc_data (crypt_stat));if (rc) {ecryptfs_printk (KERN_WARNING ,"Error attempting to compute " "MD5 while generating root IV\n" );@@ -721,6 +803,35 @@ static void ecryptfs_generate_new_key(struct ecryptfs_crypt_stat *crypt_stat) } } +static int ecryptfs_generate_new_salt (struct ecryptfs_crypt_stat *crypt_stat) +{ +size_t salt_size =0 ; +unsigned char final[2 *ECRYPTFS_MAX_CIPHER_NAME_SIZE +1 ]; + + salt_size =ecryptfs_get_salt_size_for_cipher ( +ecryptfs_get_full_cipher (crypt_stat->cipher, + crypt_stat->cipher_mode, + final,sizeof (final))); + +if (salt_size ==0 ) +return 0 ; + +if (!ecryptfs_check_space_for_salt (crypt_stat->key_size, salt_size)) { +ecryptfs_printk (KERN_WARNING ,"not enough space for salt\n" ); + crypt_stat->flags |=ECRYPTFS_SECURITY_WARNING ; +return -EINVAL ; + } + +get_random_bytes (crypt_stat->key + crypt_stat->key_size, salt_size); +if (unlikely (ecryptfs_verbosity >0 )) { +ecryptfs_printk (KERN_DEBUG ,"Generated new session salt:\n" ); +ecryptfs_dump_hex (crypt_stat->key + crypt_stat->key_size, + salt_size); + } + +return 0 ; +} +/** * ecryptfs_copy_mount_wide_flags_to_inode_flags * @crypt_stat: The inode's cryptographic context @@ -823,7 +934,6 @@ int ecryptfs_new_file_context(struct inode *ecryptfs_inode)struct ecryptfs_mount_crypt_stat *mount_crypt_stat = &ecryptfs_superblock_to_private ( ecryptfs_inode->i_sb)->mount_crypt_stat; -int cipher_name_len;int rc =0 ;ecryptfs_set_default_crypt_stat_vals (crypt_stat, mount_crypt_stat);@@ -837,15 +947,19 @@ int ecryptfs_new_file_context(struct inode *ecryptfs_inode) "to the inode key sigs; rc = [%d]\n", rc); goto out; } - cipher_name_len = -strlen (mount_crypt_stat->global_default_cipher_name); -memcpy (crypt_stat->cipher, +strlcpy (crypt_stat->cipher, mount_crypt_stat->global_default_cipher_name, - cipher_name_len); - crypt_stat->cipher[cipher_name_len] ='\0' ; +sizeof (crypt_stat->cipher)); + +strlcpy (crypt_stat->cipher_mode, + mount_crypt_stat->global_default_cipher_mode, +sizeof (crypt_stat->cipher_mode)); + crypt_stat->key_size = mount_crypt_stat->global_default_cipher_key_size;ecryptfs_generate_new_key (crypt_stat); +ecryptfs_generate_new_salt (crypt_stat); + rc =ecryptfs_init_crypt_ctx (crypt_stat);if (rc) ecryptfs_printk(KERN_ERR, "Error initializing cryptographic "@@ -971,7 +1085,8 @@ ecryptfs_cipher_code_str_map[] = { {"twofish", RFC2440_CIPHER_TWOFISH}, {"cast6", RFC2440_CIPHER_CAST_6}, {"aes", RFC2440_CIPHER_AES_192}, - {"aes", RFC2440_CIPHER_AES_256} + {"aes", RFC2440_CIPHER_AES_256}, + {"aes_xts", RFC2440_CIPHER_AES_XTS_256} };/** @@ -999,6 +1114,11 @@ u8 ecryptfs_code_for_cipher_string(char *cipher_name, size_t key_bytes)case 32 : code =RFC2440_CIPHER_AES_256 ; } + }else if (strcmp (cipher_name,"aes_xts" ) ==0 ) { +switch (key_bytes) { +case 32 : + code =RFC2440_CIPHER_AES_XTS_256 ; + } }else {for (i =0 ; i <ARRAY_SIZE (ecryptfs_cipher_code_str_map); i++)if (strcmp (cipher_name, map[i].cipher_str) ==0 ) {@@ -1038,9 +1158,24 @@ int ecryptfs_read_and_validate_header_region(struct inode *inode)u8 file_size[ECRYPTFS_SIZE_AND_MARKER_BYTES ];u8 *marker = file_size +ECRYPTFS_FILE_SIZE_BYTES ;int rc; +unsigned int ra_pages_org; +struct file *lower_file =NULL ; + +if (!inode) +return -EIO ; + lower_file =ecryptfs_inode_to_private (inode)->lower_file; +if (!lower_file) +return -EIO ; + +/*disable read a head mechanism for a while */ + ra_pages_org = lower_file->f_ra.ra_pages; + lower_file->f_ra.ra_pages =0 ; rc =ecryptfs_read_lower (file_size,0 ,ECRYPTFS_SIZE_AND_MARKER_BYTES , inode); + lower_file->f_ra.ra_pages = ra_pages_org; +/* restore read a head mechanism */ +if (rc <ECRYPTFS_SIZE_AND_MARKER_BYTES )return rc >=0 ? -EINVAL : rc; rc =ecryptfs_validate_marker (marker);@@ -1430,6 +1565,11 @@ int ecryptfs_read_metadata(struct dentry *ecryptfs_dentry)struct ecryptfs_mount_crypt_stat *mount_crypt_stat = &ecryptfs_superblock_to_private ( ecryptfs_dentry->d_sb)->mount_crypt_stat; +unsigned int ra_pages_org; +struct file *lower_file = +ecryptfs_inode_to_private (ecryptfs_inode)->lower_file; +if (!lower_file) +return -EIO ;ecryptfs_copy_mount_wide_flags_to_inode_flags (crypt_stat, mount_crypt_stat);@@ -1441,8 +1581,14 @@ int ecryptfs_read_metadata(struct dentry *ecryptfs_dentry) __func__); goto out; } +/*disable read a head mechanism */ + ra_pages_org = lower_file->f_ra.ra_pages; + lower_file->f_ra.ra_pages =0 ; + rc =ecryptfs_read_lower (page_virt,0 , crypt_stat->extent_size, ecryptfs_inode); + lower_file->f_ra.ra_pages = ra_pages_org;/* restore it back */ +if (rc >=0 ) rc = ecryptfs_read_headers_virt(page_virt, crypt_stat, ecryptfs_dentry,fs/ecryptfs/debug.c +13
-0@@ -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@@ -245,6 +245,7 @@ struct ecryptfs_crypt_stat {struct mutex cs_tfm_mutex;struct mutex cs_hash_tfm_mutex;struct mutex cs_mutex; +unsigned char cipher_mode[ECRYPTFS_MAX_CIPHER_NAME_SIZE +1 ]; };/* inode private data. */ @@ -267,6 +268,8 @@ struct ecryptfs_dentry_info { }; }; + +/** * ecryptfs_global_auth_tok - A key used to encrypt all new files under the mountpoint * @flags: Status flags @@ -345,6 +348,8 @@ struct ecryptfs_mount_crypt_stat {unsigned char global_default_fn_cipher_name[ECRYPTFS_MAX_CIPHER_NAME_SIZE +1 ];char global_default_fnek_sig[ECRYPTFS_SIG_SIZE_HEX +1 ]; +unsigned char global_default_cipher_mode[ECRYPTFS_MAX_CIPHER_NAME_SIZE + +1 ]; };/* superblock private data. */ @@ -527,6 +532,53 @@ ecryptfs_dentry_to_lower_path(struct dentry *dentry)return &((struct ecryptfs_dentry_info *)dentry->d_fsdata)->lower_path; } +/** +* Given a cipher and mode strings, the function +* concatenates them to create a new string of +* <cipher>_<mode> format. +*/ +static inline unsigned char *ecryptfs_get_full_cipher ( +unsigned char *cipher,unsigned char *mode, +unsigned char *final,size_t final_size) +{ +memset (final,0 , final_size); + +if (strlen (mode) >0 ) { +snprintf (final, final_size,"%s_%s" , cipher, mode); +return final; + } + +return cipher; +} + +/** +* Given a <cipher>[_<mode>] formatted string, the function +* extracts cipher string and/or mode string. +* Note: the passed cipher and/or mode strings will be null-terminated. +*/ +static inline void ecryptfs_parse_full_cipher ( +char *s,char *cipher,char *mode) +{ +char input[2 *ECRYPTFS_MAX_CIPHER_NAME_SIZE +1 +1 ]; +/* +1 for '_'; +1 for '\0' */ +char *p; +char *input_p = input; + +if (s ==NULL || cipher ==NULL ) +return ; + +memset (input,0 ,sizeof (input)); +strlcpy (input, s,sizeof (input)); + + p =strsep (&input_p,"_" ); +strlcpy (cipher, p,ECRYPTFS_MAX_CIPHER_NAME_SIZE +1 ); + + +/* check if mode is specified */ +if (input_p !=NULL && mode !=NULL ) +strlcpy (mode, input_p,ECRYPTFS_MAX_CIPHER_NAME_SIZE +1 ); +} + #define ecryptfs_printk(type, fmt, arg...) \ __ecryptfs_printk(type "%s: " fmt, __func__, ## arg);__printf (1 ,2 )@@ -575,6 +627,7 @@ int ecryptfs_encrypt_and_encode_filename( const char *name, size_t name_size);struct dentry *ecryptfs_lower_dentry (struct dentry *this_dentry);void ecryptfs_dump_hex (char *data,int bytes); +void ecryptfs_dump_salt_hex (char *data,int key_size,char *cipher);int virt_to_scatterlist (const void *addr,int size,struct scatterlist *sg,int sg_size);int ecryptfs_compute_root_iv (struct ecryptfs_crypt_stat *crypt_stat);@@ -718,4 +771,29 @@ int ecryptfs_set_f_namelen(long *namelen, long lower_namelen,int ecryptfs_derive_iv (char *iv,struct ecryptfs_crypt_stat *crypt_stat,loff_t offset); +void clean_inode_pages (struct address_space *mapping, +pgoff_t start,pgoff_t end); + +void ecryptfs_drop_pagecache_sb (struct super_block *sb,void *unused); + +void ecryptfs_free_events (void ); + +void ecryptfs_freepage (struct page *page); + +struct ecryptfs_events *get_events (void ); + +size_t ecryptfs_get_salt_size_for_cipher (const char *cipher); + +size_t ecryptfs_get_key_size_to_enc_data ( +struct ecryptfs_crypt_stat *crypt_stat); + +size_t ecryptfs_get_key_size_to_store_key ( +struct ecryptfs_crypt_stat *crypt_stat); + +size_t ecryptfs_get_key_size_to_restore_key (size_t stored_key_size, +const char *cipher); + +bool ecryptfs_check_space_for_salt (const size_t key_size, +const size_t salt_size); +#endif /* #ifndef ECRYPTFS_KERNEL_H */ fs/ecryptfs/events.c new file +361
-0@@ -0,0 +1,361 @@ +/** +* eCryptfs: Linux filesystem encryption layer +* Copyright (c) 2015, The Linux Foundation. All rights reserved. +* +* This program is free software; you can redistribute it and/or modify +* it under the terms of the GNU General Public License version 2 and +* only version 2 as published by the Free Software Foundation. +* +* This program is distributed in the hope that it will be useful, +* but WITHOUT ANY WARRANTY; without even the implied warranty of +* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +* GNU General Public License for more details. +*/ + +#include <linux/string.h> +#include <linux/ecryptfs.h> +#include <linux/mutex.h> +#include <linux/types.h> +#include <linux/slab.h> +#include <linux/pagemap.h> +#include <linux/random.h> +#include "ecryptfs_kernel.h" + +static DEFINE_MUTEX (events_mutex); +static struct ecryptfs_events *events_ptr; +static int handle; + +void ecryptfs_free_events (void ) +{ +mutex_lock (&events_mutex); +if (events_ptr !=NULL ) { +kfree (events_ptr); + events_ptr =NULL ; + } + +mutex_unlock (&events_mutex); +} + +/** +* Register to ecryptfs events, by passing callback +* functions to be called upon events occurrence. +* The function returns a handle to be passed +* to unregister function. +*/ +int ecryptfs_register_to_events (struct ecryptfs_events *ops) +{ +int ret_value =0 ; + +if (!ops) +return -EINVAL ; + +mutex_lock (&events_mutex); + +if (events_ptr !=NULL ) { +ecryptfs_printk (KERN_ERR , +"already registered!\n" ); + ret_value = -EPERM ; + goto out; + } + events_ptr = +kzalloc (sizeof (struct ecryptfs_events ),GFP_KERNEL ); + +if (!events_ptr) { +ecryptfs_printk (KERN_ERR ,"malloc failure\n" ); + ret_value = -ENOMEM ; + goto out; + } +/* copy the callbacks */ + events_ptr->open_cb = ops->open_cb; + events_ptr->release_cb = ops->release_cb; + events_ptr->encrypt_cb = ops->encrypt_cb; + events_ptr->decrypt_cb = ops->decrypt_cb; + events_ptr->is_cipher_supported_cb = + ops->is_cipher_supported_cb; + events_ptr->is_hw_crypt_cb = ops->is_hw_crypt_cb; + events_ptr->get_salt_key_size_cb = ops->get_salt_key_size_cb; + +get_random_bytes (&handle,sizeof (handle)); + ret_value = handle; + +out: +mutex_unlock (&events_mutex); +return ret_value; +} + +/** +* Unregister from ecryptfs events. +*/ +int ecryptfs_unregister_from_events (int user_handle) +{ +int ret_value =0 ; + +mutex_lock (&events_mutex); + +if (!events_ptr) { + ret_value = -EINVAL ; + goto out; + } +if (user_handle != handle) { + ret_value =ECRYPTFS_INVALID_EVENTS_HANDLE ; + goto out; + } + +kfree (events_ptr); + events_ptr =NULL ; + +out: +mutex_unlock (&events_mutex); +return ret_value; +} + +/** +* This function decides whether the passed file offset +* belongs to ecryptfs metadata or not. +* The caller must pass ecryptfs data, which was received in one +* of the callback invocations. +*/ +bool ecryptfs_is_page_in_metadata (void *data,pgoff_t offset) +{ + +struct ecryptfs_crypt_stat *stat =NULL ; +bool ret = true; + +if (!data) { +ecryptfs_printk (KERN_ERR ,"ecryptfs_is_page_in_metadata: invalid data parameter\n" ); + ret = false; + goto end; + } + stat = (struct ecryptfs_crypt_stat *)data; + +if (stat->flags &ECRYPTFS_METADATA_IN_XATTR ) { + ret = false; + goto end; + } + +if (offset >= (stat->metadata_size/PAGE_CACHE_SIZE )) { + ret = false; + goto end; + } +end: +return ret; +} + +/** +* Given two ecryptfs data, the function +* decides whether they are equal. +*/ +inline bool ecryptfs_is_data_equal (void *data1,void *data2) +{ +/* pointer comparison*/ +return data1 == data2; +} + +/** +* Given ecryptfs data, the function +* returns appropriate key size. +*/ +size_t ecryptfs_get_key_size (void *data) +{ + +struct ecryptfs_crypt_stat *stat =NULL ; + +if (!data) +return 0 ; + + stat = (struct ecryptfs_crypt_stat *)data; +return stat->key_size; +} + +/** +* Given ecryptfs data, the function +* returns appropriate salt size. +* +* !!! crypt_stat cipher name and mode must be initialized +*/ +size_t ecryptfs_get_salt_size (void *data) +{ +struct ecryptfs_crypt_stat *stat =NULL ; +unsigned char final[2 *ECRYPTFS_MAX_CIPHER_NAME_SIZE +1 ]; + +if (!data) { +ecryptfs_printk (KERN_ERR , +"ecryptfs_get_salt_size: invalid data parameter\n" ); +return 0 ; + } + + stat = (struct ecryptfs_crypt_stat *)data; +return ecryptfs_get_salt_size_for_cipher ( +ecryptfs_get_full_cipher (stat->cipher, + stat->cipher_mode, + final,sizeof (final))); + +} + +/** +* Given ecryptfs data, the function +* returns appropriate cipher. +*/ +const unsigned char *ecryptfs_get_cipher (void *data) +{ +unsigned char final[2 *ECRYPTFS_MAX_CIPHER_NAME_SIZE +1 ]; +struct ecryptfs_crypt_stat *stat =NULL ; + +if (!data) { +ecryptfs_printk (KERN_ERR , +"ecryptfs_get_cipher: invalid data parameter\n" ); +return NULL ; + } + stat = (struct ecryptfs_crypt_stat *)data; +return ecryptfs_get_full_cipher (stat->cipher, stat->cipher_mode, + final,sizeof (final)); +} + +/** +* Given ecryptfs data, the function +* returns file encryption key. +*/ +const unsigned char *ecryptfs_get_key (void *data) +{ + +struct ecryptfs_crypt_stat *stat =NULL ; + +if (!data) { +ecryptfs_printk (KERN_ERR , +"ecryptfs_get_key: invalid data parameter\n" ); +return NULL ; + } + stat = (struct ecryptfs_crypt_stat *)data; +return stat->key; +} + +/** +* Given ecryptfs data, the function +* returns file encryption salt. +*/ +const unsigned char *ecryptfs_get_salt (void *data) +{ +struct ecryptfs_crypt_stat *stat =NULL ; + +if (!data) { +ecryptfs_printk (KERN_ERR , +"ecryptfs_get_salt: invalid data parameter\n" ); +return NULL ; + } + stat = (struct ecryptfs_crypt_stat *)data; +return stat->key +ecryptfs_get_salt_size (data); +} + +/** +* Returns ecryptfs events pointer +*/ +inline struct ecryptfs_events *get_events (void ) +{ +return events_ptr; +} + +/** +* If external crypto module requires salt in addition to key, +* we store it as part of key array (if there is enough space) +* Checks whether a salt key can fit into array allocated for +* regular key +*/ +bool ecryptfs_check_space_for_salt (const size_t key_size, +const size_t salt_size) +{ +if ((salt_size + key_size) >ECRYPTFS_MAX_KEY_BYTES ) +return false; + +return true; +} + +/* +* If there is salt that is used by external crypto module, it is stored +* in the same array where regular key is. Salt is going to be used by +* external crypto module only, so for all internal crypto operations salt +* should be ignored. +* +* Get key size in cases where it is going to be used for data encryption +* or for all other general purposes +*/ +size_t ecryptfs_get_key_size_to_enc_data ( +struct ecryptfs_crypt_stat *crypt_stat) +{ +if (!crypt_stat) +return 0 ; + +return crypt_stat->key_size; +} + +/* +* If there is salt that is used by external crypto module, it is stored +* in the same array where regular key is. Salt is going to be used by +* external crypto module only, but we still need to save and restore it +* (in encrypted form) as part of ecryptfs header along with the regular +* key. +* +* Get key size in cases where it is going to be stored persistently +* +* !!! crypt_stat cipher name and mode must be initialized +*/ +size_t ecryptfs_get_key_size_to_store_key ( +struct ecryptfs_crypt_stat *crypt_stat) +{ +size_t salt_size =0 ; + +if (!crypt_stat) +return 0 ; + + salt_size =ecryptfs_get_salt_size (crypt_stat); + +if (!ecryptfs_check_space_for_salt (crypt_stat->key_size, salt_size)) { +ecryptfs_printk (KERN_WARNING , +"ecryptfs_get_key_size_to_store_key: not enough space for salt\n" ); +return crypt_stat->key_size; + } + +return crypt_stat->key_size + salt_size; +} + +/* +* If there is salt that is used by external crypto module, it is stored +* in the same array where regular key is. Salt is going to be used by +* external crypto module only, but we still need to save and restore it +* (in encrypted form) as part of ecryptfs header along with the regular +* key. +* +* Get key size in cases where it is going to be restored from storage +* +* !!! crypt_stat cipher name and mode must be initialized +*/ +size_t ecryptfs_get_key_size_to_restore_key (size_t stored_key_size, +const char *cipher) +{ +size_t salt_size =0 ; + +if (!cipher) +return 0 ; + + salt_size =ecryptfs_get_salt_size_for_cipher (cipher); + +if (salt_size >= stored_key_size) { +ecryptfs_printk (KERN_WARNING , +"ecryptfs_get_key_size_to_restore_key: salt %zu >= stred size %zu\n" , + salt_size, stored_key_size); + +return stored_key_size; + } + +return stored_key_size - salt_size; +} + +/** +* Given cipher, the function returns appropriate salt size. +*/ +size_t ecryptfs_get_salt_size_for_cipher (const char *cipher) +{ +if (!get_events () || !(get_events ()->get_salt_key_size_cb)) +return 0 ; + +return get_events ()->get_salt_key_size_cb (cipher); +}fs/ecryptfs/file.c +36
-0@@ -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@@ -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@@ -315,7 +315,8 @@ write_tag_66_packet(char *signature, u8 cipher_code,* | File Encryption Key Size | 1 or 2 bytes | * | File Encryption Key | arbitrary | */ - data_len = (5 +ECRYPTFS_SIG_SIZE_HEX + crypt_stat->key_size); + data_len = (5 +ECRYPTFS_SIG_SIZE_HEX + +ecryptfs_get_key_size_to_store_key (crypt_stat)); *packet =kmalloc (data_len,GFP_KERNEL ); message = *packet;if (!message) {@@ -335,8 +336,9 @@ write_tag_66_packet(char *signature, u8 cipher_code,memcpy (&message[i], signature,ECRYPTFS_SIG_SIZE_HEX ); i +=ECRYPTFS_SIG_SIZE_HEX ;/* The encrypted key includes 1 byte cipher code and 2 byte checksum */ - rc =ecryptfs_write_packet_length (&message[i], crypt_stat->key_size +3 , - &packet_size_len); + rc =ecryptfs_write_packet_length (&message[i], +ecryptfs_get_key_size_to_store_key (crypt_stat) +3 , + &packet_size_len);if (rc) {ecryptfs_printk (KERN_ERR ,"Error generating tag 66 packet " "header; cannot generate packet length\n" );@@ -344,9 +346,10 @@ write_tag_66_packet(char *signature, u8 cipher_code, } i += packet_size_len; message[i++] = cipher_code; -memcpy (&message[i], crypt_stat->key, crypt_stat->key_size); - i += crypt_stat->key_size; -for (j =0 ; j < crypt_stat->key_size; j++) +memcpy (&message[i], crypt_stat->key, +ecryptfs_get_key_size_to_store_key (crypt_stat)); + i +=ecryptfs_get_key_size_to_store_key (crypt_stat); +for (j =0 ; j <ecryptfs_get_key_size_to_store_key (crypt_stat); j++) checksum += crypt_stat->key[j]; message[i++] = (checksum /256 ) %256 ; message[i++] = (checksum %256 );@@ -918,6 +921,7 @@ ecryptfs_parse_tag_70_packet(char **filename, size_t *filename_size,struct ecryptfs_parse_tag_70_packet_silly_stack *s;struct key *auth_tok_key =NULL ;int rc =0 ; +char full_cipher[ECRYPTFS_MAX_CIPHER_NAME_SIZE ]; (*packet_size) =0 ; (*filename_size) =0 ;@@ -977,12 +981,13 @@ ecryptfs_parse_tag_70_packet(char **filename, size_t *filename_size, s->fnek_sig_hex[ECRYPTFS_SIG_SIZE_HEX ] ='\0' ; (*packet_size) +=ECRYPTFS_SIG_SIZE ; s->cipher_code = data[(*packet_size)++]; - rc =ecryptfs_cipher_code_to_string (s->cipher_string, s->cipher_code); + rc =ecryptfs_cipher_code_to_string (full_cipher, s->cipher_code);if (rc) {printk (KERN_WARNING "%s: Cipher code [%d] is invalid\n" , __func__, s->cipher_code); goto out; } +ecryptfs_parse_full_cipher (full_cipher, s->cipher_string,0 ); rc =ecryptfs_find_auth_tok_for_sig (&auth_tok_key, &s->auth_tok, mount_crypt_stat, s->fnek_sig_hex);@@ -1151,6 +1156,7 @@ decrypt_pki_encrypted_session_key(struct ecryptfs_auth_tok *auth_tok,char *payload =NULL ;size_t payload_len =0 ;int rc; +char full_cipher[ECRYPTFS_MAX_CIPHER_NAME_SIZE ]; rc =ecryptfs_get_auth_tok_sig (&auth_tok_sig, auth_tok);if (rc) {@@ -1184,21 +1190,31 @@ decrypt_pki_encrypted_session_key(struct ecryptfs_auth_tok *auth_tok, rc); goto out; } - auth_tok->session_key.flags |=ECRYPTFS_CONTAINS_DECRYPTED_KEY ; -memcpy (crypt_stat->key, auth_tok->session_key.decrypted_key, - auth_tok->session_key.decrypted_key_size); - crypt_stat->key_size = auth_tok->session_key.decrypted_key_size; - rc =ecryptfs_cipher_code_to_string (crypt_stat->cipher, cipher_code); + + rc =ecryptfs_cipher_code_to_string (full_cipher, cipher_code);if (rc) {ecryptfs_printk (KERN_ERR ,"Cipher code [%d] is invalid\n" , cipher_code) - goto out; + goto out; } + + auth_tok->session_key.flags |=ECRYPTFS_CONTAINS_DECRYPTED_KEY ; +memcpy (crypt_stat->key, auth_tok->session_key.decrypted_key, + auth_tok->session_key.decrypted_key_size); + crypt_stat->key_size =ecryptfs_get_key_size_to_restore_key ( + auth_tok->session_key.decrypted_key_size, full_cipher); + +ecryptfs_parse_full_cipher (full_cipher, + crypt_stat->cipher, crypt_stat->cipher_mode); + crypt_stat->flags |=ECRYPTFS_KEY_VALID ;if (ecryptfs_verbosity >0 ) {ecryptfs_printk (KERN_DEBUG ,"Decrypted session key:\n" );ecryptfs_dump_hex (crypt_stat->key, crypt_stat->key_size); + +ecryptfs_dump_salt_hex (crypt_stat->key, crypt_stat->key_size, + full_cipher); } out:kfree (msg);@@ -1380,6 +1396,7 @@ parse_tag_3_packet(struct ecryptfs_crypt_stat *crypt_stat,struct ecryptfs_auth_tok_list_item *auth_tok_list_item;size_t length_size;int rc =0 ; +char full_cipher[ECRYPTFS_MAX_CIPHER_NAME_SIZE ]; (*packet_size) =0 ; (*new_auth_tok) =NULL ;@@ -1453,10 +1470,13 @@ parse_tag_3_packet(struct ecryptfs_crypt_stat *crypt_stat, rc = -EINVAL ; goto out_free; } - rc =ecryptfs_cipher_code_to_string (crypt_stat->cipher, + rc =ecryptfs_cipher_code_to_string (full_cipher, (u16 )data[(*packet_size)]);if (rc) goto out_free; +ecryptfs_parse_full_cipher (full_cipher, + crypt_stat->cipher, crypt_stat->cipher_mode); +/* A little extra work to differentiate among the AES key * sizes; see RFC2440 */ switch (data[(*packet_size)++]) {@@ -1465,7 +1485,10 @@ parse_tag_3_packet(struct ecryptfs_crypt_stat *crypt_stat,break ;default : crypt_stat->key_size = - (*new_auth_tok)->session_key.encrypted_key_size; +ecryptfs_get_key_size_to_restore_key ( + (*new_auth_tok)->session_key.encrypted_key_size, + full_cipher); + } rc =ecryptfs_init_crypt_ctx (crypt_stat);if (rc)@@ -1664,6 +1687,8 @@ static int decrypt_passphrase_encrypted_session_key(struct ecryptfs_auth_tok *auth_tok, struct ecryptfs_crypt_stat *crypt_stat) { + + unsigned char final[2*ECRYPTFS_MAX_CIPHER_NAME_SIZE+1];struct scatterlist dst_sg[2 ];struct scatterlist src_sg[2 ];struct mutex *tfm_mutex;@@ -1713,7 +1738,7 @@ decrypt_passphrase_encrypted_session_key(struct ecryptfs_auth_tok *auth_tok,mutex_lock (tfm_mutex); rc =crypto_blkcipher_setkey ( desc.tfm, auth_tok->token.password.session_key_encryption_key, - crypt_stat->key_size); + auth_tok->token.password.session_key_encryption_key_bytes);if (unlikely (rc <0 )) {mutex_unlock (tfm_mutex);printk (KERN_ERR "Error setting key for crypto context\n" );@@ -1736,6 +1761,10 @@ decrypt_passphrase_encrypted_session_key(struct ecryptfs_auth_tok *auth_tok, crypt_stat->key_size);ecryptfs_dump_hex (crypt_stat->key, crypt_stat->key_size); +ecryptfs_dump_salt_hex (crypt_stat->key, crypt_stat->key_size, +ecryptfs_get_full_cipher (crypt_stat->cipher, + crypt_stat->cipher_mode, + final,sizeof (final))); } out:return rc;@@ -1972,12 +2001,17 @@ pki_encrypt_session_key(struct key *auth_tok_key,size_t payload_len =0 ;struct ecryptfs_message *msg;int rc; +unsigned char final[2 *ECRYPTFS_MAX_CIPHER_NAME_SIZE +1 ]; rc =write_tag_66_packet (auth_tok->token.private_key.signature, -ecryptfs_code_for_cipher_string ( - crypt_stat->cipher, - crypt_stat->key_size), - crypt_stat, &payload, &payload_len); +ecryptfs_code_for_cipher_string ( +ecryptfs_get_full_cipher ( + crypt_stat->cipher, + crypt_stat->cipher_mode, + final,sizeof (final)), +ecryptfs_get_key_size_to_enc_data ( + crypt_stat)), + crypt_stat, &payload, &payload_len);up_write (&(auth_tok_key->sem));key_put (auth_tok_key);if (rc) {@@ -2035,7 +2069,7 @@ write_tag_1_packet(char *dest, size_t *remaining_bytes,ecryptfs_from_hex (key_rec->sig, auth_tok->token.private_key.signature,ECRYPTFS_SIG_SIZE ); encrypted_session_key_valid =0 ; -for (i =0 ; i < crypt_stat->key_size; i++) +for (i =0 ; i <ecryptfs_get_key_size_to_store_key (crypt_stat); i++) encrypted_session_key_valid |= auth_tok->session_key.encrypted_key[i];if (encrypted_session_key_valid) {@@ -2189,6 +2223,7 @@ write_tag_3_packet(char *dest, size_t *remaining_bytes,u8 cipher_code;size_t packet_size_length;size_t max_packet_size; +unsigned char final[2 *ECRYPTFS_MAX_CIPHER_NAME_SIZE +1 ];struct ecryptfs_mount_crypt_stat *mount_crypt_stat = crypt_stat->mount_crypt_stat;struct blkcipher_desc desc = {@@ -2221,13 +2256,14 @@ write_tag_3_packet(char *dest, size_t *remaining_bytes, mount_crypt_stat->global_default_cipher_key_size;if (auth_tok->session_key.encrypted_key_size ==0 ) auth_tok->session_key.encrypted_key_size = - crypt_stat->key_size; +ecryptfs_get_key_size_to_store_key (crypt_stat);if (crypt_stat->key_size ==24 &&strcmp ("aes" , crypt_stat->cipher) ==0 ) {memset ((crypt_stat->key +24 ),0 ,8 ); auth_tok->session_key.encrypted_key_size =32 ; }else - auth_tok->session_key.encrypted_key_size = crypt_stat->key_size; + auth_tok->session_key.encrypted_key_size = +ecryptfs_get_key_size_to_store_key (crypt_stat); key_rec->enc_key_size = auth_tok->session_key.encrypted_key_size; encrypted_session_key_valid =0 ;@@ -2251,8 +2287,8 @@ write_tag_3_packet(char *dest, size_t *remaining_bytes, auth_tok->token.password. session_key_encryption_key_bytes);memcpy (session_key_encryption_key, - auth_tok->token.password.session_key_encryption_key, - crypt_stat->key_size); + auth_tok->token.password.session_key_encryption_key, + auth_tok->token.password.session_key_encryption_key_bytes);ecryptfs_printk (KERN_DEBUG ,"Cached session key encryption key:\n" );if (ecryptfs_verbosity >0 )@@ -2285,7 +2321,7 @@ write_tag_3_packet(char *dest, size_t *remaining_bytes, }mutex_lock (tfm_mutex); rc =crypto_blkcipher_setkey (desc.tfm, session_key_encryption_key, - crypt_stat->key_size); + auth_tok->token.password.session_key_encryption_key_bytes);if (rc <0 ) {mutex_unlock (tfm_mutex); ecryptfs_printk(KERN_ERR, "Error setting key for crypto "@@ -2294,7 +2330,12 @@ write_tag_3_packet(char *dest, size_t *remaining_bytes, } rc =0 ;ecryptfs_printk (KERN_DEBUG ,"Encrypting [%zd] bytes of the key\n" , - crypt_stat->key_size); + crypt_stat->key_size); +ecryptfs_printk (KERN_DEBUG ,"Encrypting [%zd] bytes of the salt key\n" , +ecryptfs_get_salt_size_for_cipher ( +ecryptfs_get_full_cipher (crypt_stat->cipher, + crypt_stat->cipher_mode, + final,sizeof (final)))); rc =crypto_blkcipher_encrypt (&desc, dst_sg, src_sg, (*key_rec).enc_key_size);mutex_unlock (tfm_mutex);@@ -2343,8 +2384,10 @@ encrypted_session_key_set: dest[(*packet_size)++] =0x04 ;/* version 4 */ /* TODO: Break from RFC2440 so that arbitrary ciphers can be * specified with strings */ - cipher_code =ecryptfs_code_for_cipher_string (crypt_stat->cipher, - crypt_stat->key_size); + cipher_code =ecryptfs_code_for_cipher_string ( +ecryptfs_get_full_cipher (crypt_stat->cipher, + crypt_stat->cipher_mode, final,sizeof (final)), + crypt_stat->key_size);if (cipher_code ==0 ) {ecryptfs_printk (KERN_WARNING ,"Unable to generate code for " "cipher [%s]\n" , crypt_stat->cipher);fs/ecryptfs/main.c +50
-10@@ -165,7 +165,13 @@ void ecryptfs_put_lower_file(struct inode *inode)fput (inode_info->lower_file); inode_info->lower_file =NULL ;mutex_unlock (&inode_info->lower_file_mutex); + +if (get_events () &&get_events ()->release_cb) +get_events ()->release_cb ( +ecryptfs_inode_to_lower (inode)); } + + }enum { ecryptfs_opt_sig, ecryptfs_opt_ecryptfs_sig,@@ -266,6 +272,7 @@ static int ecryptfs_parse_options(struct ecryptfs_sb_info *sbi, char *options,int cipher_key_bytes_set =0 ;int fn_cipher_key_bytes;int fn_cipher_key_bytes_set =0 ; +size_t salt_size =0 ;struct ecryptfs_mount_crypt_stat *mount_crypt_stat = &sbi->mount_crypt_stat;substring_t args[MAX_OPT_ARGS ];@@ -280,6 +287,7 @@ static int ecryptfs_parse_options(struct ecryptfs_sb_info *sbi, char *options,char *cipher_key_bytes_src;char *fn_cipher_key_bytes_src;u8 cipher_code; +unsigned char final[2 *ECRYPTFS_MAX_CIPHER_NAME_SIZE +1 ]; *check_ruid =0 ;@@ -309,12 +317,14 @@ static int ecryptfs_parse_options(struct ecryptfs_sb_info *sbi, char *options,case ecryptfs_opt_ecryptfs_cipher: cipher_name_src = args[0 ].from; cipher_name_dst = - mount_crypt_stat-> - global_default_cipher_name; -strncpy (cipher_name_dst, cipher_name_src, -ECRYPTFS_MAX_CIPHER_NAME_SIZE ); - cipher_name_dst[ECRYPTFS_MAX_CIPHER_NAME_SIZE ] ='\0' ; + mount_crypt_stat->global_default_cipher_name; + +ecryptfs_parse_full_cipher (cipher_name_src, + mount_crypt_stat->global_default_cipher_name, + mount_crypt_stat->global_default_cipher_mode); + cipher_name_set =1 ; +break ;case ecryptfs_opt_ecryptfs_key_bytes: cipher_key_bytes_src = args[0 ].from;@@ -411,24 +421,50 @@ static int ecryptfs_parse_options(struct ecryptfs_sb_info *sbi, char *options,strcpy (mount_crypt_stat->global_default_cipher_name,ECRYPTFS_DEFAULT_CIPHER ); } +if ((mount_crypt_stat->flags &ECRYPTFS_GLOBAL_ENCRYPT_FILENAMES ) && !fn_cipher_name_set)strcpy (mount_crypt_stat->global_default_fn_cipher_name, mount_crypt_stat->global_default_cipher_name); -if (!cipher_key_bytes_set) + +if (cipher_key_bytes_set) { + + salt_size =ecryptfs_get_salt_size_for_cipher ( +ecryptfs_get_full_cipher ( + mount_crypt_stat->global_default_cipher_name, + mount_crypt_stat->global_default_cipher_mode, + final,sizeof (final))); + +if (!ecryptfs_check_space_for_salt ( + mount_crypt_stat->global_default_cipher_key_size, + salt_size)) { +ecryptfs_printk ( +KERN_WARNING , +"eCryptfs internal error: no space for salt" ); + } + }else mount_crypt_stat->global_default_cipher_key_size =0 ; +if ((mount_crypt_stat->flags &ECRYPTFS_GLOBAL_ENCRYPT_FILENAMES ) && !fn_cipher_key_bytes_set) mount_crypt_stat->global_default_fn_cipher_key_bytes = mount_crypt_stat->global_default_cipher_key_size; cipher_code =ecryptfs_code_for_cipher_string ( - 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)), mount_crypt_stat->global_default_cipher_key_size);if (!cipher_code) { -ecryptfs_printk (KERN_ERR , -"eCryptfs doesn't support cipher: %s" , - mount_crypt_stat->global_default_cipher_name); +ecryptfs_printk ( +KERN_ERR , +"eCryptfs doesn't support cipher: %s and key size %zu" , +ecryptfs_get_full_cipher ( + mount_crypt_stat->global_default_cipher_name, + mount_crypt_stat->global_default_cipher_mode, + final,sizeof (final)), + mount_crypt_stat->global_default_cipher_key_size); rc = -EINVAL ; goto out; }@@ -488,6 +524,7 @@ static struct file_system_type ecryptfs_fs_type;* @dev_name: The path to mount over * @raw_data: The options passed into the kernel */ +static struct dentry *ecryptfs_mount (struct file_system_type *fs_type,int flags,const char *dev_name,void *raw_data) {@@ -557,6 +594,8 @@ static struct dentry *ecryptfs_mount(struct file_system_type *fs_type, int flagsecryptfs_set_superblock_lower (s, path.dentry->d_sb); +ecryptfs_drop_pagecache_sb (ecryptfs_superblock_to_lower (s),NULL ); +/** * Set the POSIX ACL flag based on whether they're enabled in the lower * mount. @@ -894,6 +933,7 @@ static void __exit ecryptfs_exit(void)do_sysfs_unregistration ();unregister_filesystem (&ecryptfs_fs_type);ecryptfs_free_kmem_caches (); +ecryptfs_free_events (); }MODULE_AUTHOR ("Michael A. Halcrow <[email protected]>" );fs/ecryptfs/mmap.c +6
-0@@ -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@@ -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@@ -1,6 +1,9 @@#ifndef _LINUX_ECRYPTFS_H#define _LINUX_ECRYPTFS_H +struct inode ; +struct page ; +/* Version verification for shared data structures w/ userspace */ #define ECRYPTFS_VERSION_MAJOR 0x00#define ECRYPTFS_VERSION_MINOR 0x04@@ -41,6 +44,7 @@#define RFC2440_CIPHER_AES_256 0x09#define RFC2440_CIPHER_TWOFISH 0x0a#define RFC2440_CIPHER_CAST_6 0x0b +#define RFC2440_CIPHER_AES_XTS_256 0x0c#define RFC2440_CIPHER_RSA 0x01@@ -102,4 +106,47 @@ struct ecryptfs_auth_tok { } token; } __attribute__ ((packed)); +#define ECRYPTFS_INVALID_EVENTS_HANDLE -1 + +/** +* ecryptfs_events struct represents a partial interface +* towards ecryptfs module. If registered to ecryptfs events, +* one can receive push notifications. +* A first callback received from ecryptfs will probably be +* about file opening (open_cb), +* in which ecryptfs passes its ecryptfs_data for future usage. +* This data represents a file and must be passed in every query functions +* such as ecryptfs_get_key_size(), ecryptfs_get_cipher() etc. +*/ +struct ecryptfs_events { +bool (*is_cipher_supported_cb)(const char *cipher); +void (*open_cb)(struct inode *inode,void *ecrytpfs_data); +void (*release_cb)(struct inode *inode); +int (*encrypt_cb)(struct page *in_page,struct page *out_page, +struct inode *inode,unsigned long extent_offset); +int (*decrypt_cb)(struct page *in_page,struct page *out_page, +struct inode *inode,unsigned long extent_offset); +bool (*is_hw_crypt_cb)(void ); +size_t (*get_salt_key_size_cb)(const char *cipher); +}; + + +int ecryptfs_register_to_events (struct ecryptfs_events *ops); + +int ecryptfs_unregister_from_events (int user_handle); + +const unsigned char *ecryptfs_get_key (void *ecrytpfs_data); + +size_t ecryptfs_get_key_size (void *ecrytpfs_data); + +const unsigned char *ecryptfs_get_salt (void *ecrytpfs_data); + +size_t ecryptfs_get_salt_size (void *ecrytpfs_data); + +const unsigned char *ecryptfs_get_cipher (void *ecrytpfs_data); + +bool ecryptfs_is_page_in_metadata (void *ecrytpfs_data,pgoff_t offset); + +bool ecryptfs_is_data_equal (void *ecrytpfs_data1,void *ecrytpfs_data2); +#endif /* _LINUX_ECRYPTFS_H */ 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
Hello, Tyler
I'll try to provide more detailed explanation, should it be satisfactory enough I will update the patch description.
The problem with current eCryptfs is that it has total control on how and when the encryption is performed and this control can't be altered. One example when this can be a problem is when we want to utilize an underlying inline HW encryption engine which allows encrypting blocks 'on the fly' as they are being written to the storage. In such a case relevant blocks just need to be marked as 'should be encrypted'. No actual encryption should be done by eCryptfs as it will be much slower.
The provided framework allows transferring this control (if needed) to some external module which will do the encryption itfelf or just mark the appropriate blocks.
There is no caller for ecryptfs_register_to_events() since this change only provides framework, it doesn't provide the module itself, the module could be HW dependent.
Regarding the mounting option, it merely serves as example of new cipher mode that can be served by registered module.
There is a special callback function that should be implemented by the registered module that tells whether a particular cipher is supported by it :is_cipher_supported_cb()
The mounting option itself is not necessary, I can remove it
Quoted 1,761 lines
Hello Andrey!
Quoted 11 lines · Andrey Markovytch
From: Andrey Markovytch <[email protected]>
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"What is "HW/external mode"? There's no description in the commit message and
ecryptfs_register_to_events()does not have a caller so I'm not sure of the purpose. Please provide more context.Despite not yet understanding the purpose of this patch, I think that I can safely say that "aes-xts" is not an appropriate mount option to use when enabling this mode. eCryptfs may support XTS mode one day, using the Crypto API, so "HW/external mode" should not own the mount option.
Tyler
Quoted 1,734 lines
Signed-off-by Lina Zarivach <[email protected]>Signed-off-by Andrey Markovytch <[email protected]>--- 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.cfs/ecryptfs/Makefile +2
-2@@ -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.oecryptfs-$(CONFIG_ECRYPT_FS_MESSAGING) += messaging.o miscdev.o
fs/ecryptfs/caches_utils.c new file +78
-0@@ -0,0 +1,78 @@ +/* +* Copyright (c) 2015, The Linux Foundation. All rights reserved. +* +* This program is free software; you can redistribute it and/or modify +* it under the terms of the GNU General Public License version 2 and +* only version 2 as published by the Free Software Foundation. +* +* This program is distributed in the hope that it will be useful, +* but WITHOUT ANY WARRANTY; without even the implied warranty of +* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +* GNU General Public License for more details. +*/ + +#include <linux/kernel.h> +#include <linux/fs.h> +#include <linux/spinlock.h> +#include <linux/pagemap.h> +#include <linux/pagevec.h> + +#include "../internal.h" + +void ecryptfs_drop_pagecache_sb (struct super_block *sb,void *unused) +{ +struct inode *inode, *toput_inode =NULL ; + +spin_lock (&sb->s_inode_list_lock); +list_for_each_entry (inode, &sb->s_inodes, i_sb_list) { +spin_lock (&inode->i_lock); +if ((inode->i_state & (I_FREEING |I_WILL_FREE |I_NEW )) || + (inode->i_mapping->nrpages ==0 )) { +spin_unlock (&inode->i_lock); +continue ; + } +__iget (inode); +spin_unlock (&inode->i_lock); +spin_unlock (&sb->s_inode_list_lock); + +invalidate_mapping_pages (inode->i_mapping,0 ,-1 ); +iput (toput_inode); + toput_inode = inode; + +spin_lock (&sb->s_inode_list_lock); + } +spin_unlock (&sb->s_inode_list_lock); +iput (toput_inode); +} + +void clean_inode_pages (struct address_space *mapping, +pgoff_t start,pgoff_t end) +{ +struct pagevec pvec; +pgoff_t index = start; +int i; + +pagevec_init (&pvec,0 ); +while (index <= end &&pagevec_lookup (&pvec, mapping, index, +min (end - index, + (pgoff_t )PAGEVEC_SIZE -1 ) +1 )) { +for (i =0 ; i <pagevec_count (&pvec); i++) { +struct page *page = pvec.pages[i]; + +/* We rely upon deletion +* not changing page->index +*/ + index = page->index; +if (index > end) +break ; +if (!trylock_page (page)) +continue ; +WARN_ON (page->index != index); +zero_user (page,0 ,PAGE_CACHE_SIZE ); +unlock_page (page); + } +pagevec_release (&pvec); +cond_resched (); + index++; + } +}fs/ecryptfs/crypto.c +3
-2@@ -35,6 +35,7 @@#include <linux/scatterlist.h> #include <linux/slab.h> #include <asm/unaligned.h> +#include <linux/ecryptfs.h> #include "ecryptfs_kernel.h" #define DECRYPT 0@@ -350,9 +351,9 @@ static int crypt_scatterlist(struct ecryptfs_crypt_stat *crypt_stat, || !(crypt_stat->flags & ECRYPTFS_STRUCT_INITIALIZED));if (unlikely (ecryptfs_verbosity >0 )) {ecryptfs_printk (KERN_DEBUG ,"Key size [%zd]; key:\n" , - crypt_stat->key_size); +ecryptfs_get_key_size_to_enc_data (crypt_stat));ecryptfs_dump_hex (crypt_stat->key, - crypt_stat->key_size); +ecryptfs_get_key_size_to_enc_data (crypt_stat)); }fs/ecryptfs/crypto.c +1
-1init_completion (&ecr.completion);@@ -371,7 +372,7 @@ static int crypt_scatterlist(struct ecryptfs_crypt_stat *crypt_stat,/* Consider doing this once, when the file is opened */ if (!(crypt_stat->flags & ECRYPTFS_KEY_SET)) { rc =crypto_ablkcipher_setkey (crypt_stat->tfm, crypt_stat->key, - crypt_stat->key_size); +ecryptfs_get_key_size_to_enc_data (crypt_stat));if (rc) { ecryptfs_printk(KERN_ERR,fs/ecryptfs/crypto.c +25
-0"Error setting key; rc = [%d]\n",@@ -466,6 +467,31 @@ out:return rc; } +static void init_ecryption_parameters (bool *hw_crypt,bool *cipher_supported, +struct ecryptfs_crypt_stat *crypt_stat) +{ +unsigned char final[2 *ECRYPTFS_MAX_CIPHER_NAME_SIZE +1 ]; + +if (!hw_crypt || !cipher_supported) +return ; + + *cipher_supported = false; + *hw_crypt = false; + +if (get_events () &&get_events ()->is_cipher_supported_cb) { + *cipher_supported = +get_events ()->is_cipher_supported_cb ( +ecryptfs_get_full_cipher (crypt_stat->cipher, + crypt_stat->cipher_mode, final,sizeof (final))); +if (*cipher_supported) { +/* we should apply external algorythm +* assume that is_hw_crypt() cbck is supplied +*/ + *hw_crypt =get_events ()->is_hw_crypt_cb (); + } + } +} +/** * ecryptfs_encrypt_page * @page: Page mapped from the eCryptfs inode for the file; contains
fs/ecryptfs/crypto.c +47
-13@@ -491,11 +517,18 @@ int ecryptfs_encrypt_page(struct page *page)loff_t extent_offset;loff_t lower_offset;int rc =0 ; +bool is_hw_crypt; +bool is_cipher_supported; + ecryptfs_inode = page->mapping->host; crypt_stat = &(ecryptfs_inode_to_private (ecryptfs_inode)->crypt_stat);BUG_ON (!(crypt_stat->flags &ECRYPTFS_ENCRYPTED )); + +init_ecryption_parameters (&is_hw_crypt, + &is_cipher_supported, crypt_stat); + enc_extent_page =alloc_page (GFP_USER );if (!enc_extent_page) { rc = -ENOMEM ;@@ -503,24 +536,51 @@ int ecryptfs_encrypt_page(struct page *page) "encrypted extent\n"); goto out; } - -for (extent_offset =0 ; - extent_offset < (PAGE_CACHE_SIZE / crypt_stat->extent_size); - extent_offset++) { - rc =crypt_extent (crypt_stat, enc_extent_page, page, - extent_offset,ENCRYPT ); -if (rc) { -printk (KERN_ERR "%s: Error encrypting extent; " -"rc = [%d]\n" , __func__, rc); - goto out; - } +if (is_hw_crypt) { +/* no need for encryption */ + }else { +for (extent_offset =0 ; + extent_offset < + (PAGE_CACHE_SIZE / crypt_stat->extent_size); + extent_offset++) { + +if (is_cipher_supported) { +if (!get_events ()->encrypt_cb) { + rc = -EPERM ; + goto out; + } + rc =get_events ()->encrypt_cb (page, + enc_extent_page, +ecryptfs_inode_to_lower ( + ecryptfs_inode), + extent_offset); + }else { + rc =crypt_extent (crypt_stat, + enc_extent_page, page, + extent_offset,ENCRYPT ); + } +if (rc) { +ecryptfs_printk (KERN_ERR , +"%s: Error encrypting; rc = [%d]\n" , + __func__, rc); + goto out; + } + } } lower_offset =lower_offset_for_page (crypt_stat, page); - enc_extent_virt =kmap (enc_extent_page); +if (is_hw_crypt) + enc_extent_virt =kmap (page); +else + enc_extent_virt =kmap (enc_extent_page); + rc =ecryptfs_write_lower (ecryptfs_inode, enc_extent_virt, lower_offset,PAGE_CACHE_SIZE ); -kunmap (enc_extent_page); +if (!is_hw_crypt) +kunmap (enc_extent_page); +else +kunmap (page); +if (rc <0 ) { ecryptfs_printk(KERN_ERR,fs/ecryptfs/crypto.c +25
-3"Error attempting to write lower page; rc = [%d]\n",@@ -559,6 +619,8 @@ int ecryptfs_decrypt_page(struct page *page)unsigned long extent_offset;loff_t lower_offset;int rc =0 ; +bool is_cipher_supported; +bool is_hw_crypt; ecryptfs_inode = page->mapping->host; crypt_stat =@@ -577,13 +639,33 @@ int ecryptfs_decrypt_page(struct page *page) goto out; } +init_ecryption_parameters (&is_hw_crypt, + &is_cipher_supported, crypt_stat); + +if (is_hw_crypt) { + rc =0 ; +return rc; + } +for (extent_offset =0 ; extent_offset < (PAGE_CACHE_SIZE / crypt_stat->extent_size); extent_offset++) { - rc =crypt_extent (crypt_stat, page, page, +if (is_cipher_supported) { +if (!get_events ()->decrypt_cb) { + rc = -EPERM ; + goto out; + } + + rc =get_events ()->decrypt_cb (page, page, +ecryptfs_inode_to_lower (ecryptfs_inode), + extent_offset); + + }else + rc =crypt_extent (crypt_stat, page, page, extent_offset,DECRYPT ); +if (rc) { -printk (KERN_ERR "%s: Error encrypting extent; " +ecryptfs_printk (KERN_ERR ,"%s: Error decrypting extent;" "rc = [%d]\n" , __func__, rc); goto out; }@@ -612,7 +694,7 @@ int ecryptfs_init_crypt_ctx(struct ecryptfs_crypt_stat *crypt_stat)"Initializing cipher [%s]; strlen = [%d]; " "key_size_bits = [%zd]\n" , crypt_stat->cipher, (int )strlen (crypt_stat->cipher), - crypt_stat->key_size << 3); + ecryptfs_get_key_size_to_enc_data(crypt_stat) << 3);mutex_lock (&crypt_stat->cs_tfm_mutex);if (crypt_stat->tfm) {fs/ecryptfs/crypto.c +1
-1rc =0 ;@@ -694,7 +776,7 @@ int ecryptfs_compute_root_iv(struct ecryptfs_crypt_stat *crypt_stat) goto out; } rc =ecryptfs_calculate_md5 (dst, crypt_stat, crypt_stat->key, - crypt_stat->key_size); +ecryptfs_get_key_size_to_enc_data (crypt_stat));if (rc) { ecryptfs_printk(KERN_WARNING, "Error attempting to compute "fs/ecryptfs/crypto.c +29
-0"MD5 while generating root IV\n");@@ -721,6 +803,35 @@ static void ecryptfs_generate_new_key(struct ecryptfs_crypt_stat *crypt_stat) } } +static int ecryptfs_generate_new_salt (struct ecryptfs_crypt_stat *crypt_stat) +{ +size_t salt_size =0 ; +unsigned char final[2 *ECRYPTFS_MAX_CIPHER_NAME_SIZE +1 ]; + + salt_size =ecryptfs_get_salt_size_for_cipher ( +ecryptfs_get_full_cipher (crypt_stat->cipher, + crypt_stat->cipher_mode, + final,sizeof (final))); + +if (salt_size ==0 ) +return 0 ; + +if (!ecryptfs_check_space_for_salt (crypt_stat->key_size, salt_size)) { +ecryptfs_printk (KERN_WARNING ,"not enough space for salt\n" ); + crypt_stat->flags |=ECRYPTFS_SECURITY_WARNING ; +return -EINVAL ; + } + +get_random_bytes (crypt_stat->key + crypt_stat->key_size, salt_size); +if (unlikely (ecryptfs_verbosity >0 )) { +ecryptfs_printk (KERN_DEBUG ,"Generated new session salt:\n" ); +ecryptfs_dump_hex (crypt_stat->key + crypt_stat->key_size, + salt_size); + } + +return 0 ; +} +/** *
ecryptfs_copy_mount_wide_flags_to_inode_flags
* @crypt_stat: The inode's cryptographic contextfs/ecryptfs/crypto.c +0
-1@@ -823,7 +934,6 @@ int ecryptfs_new_file_context(struct inode *ecryptfs_inode)struct ecryptfs_mount_crypt_stat *mount_crypt_stat = &ecryptfs_superblock_to_private ( ecryptfs_inode->i_sb)->mount_crypt_stat; -int cipher_name_len;int rc =0 ;fs/ecryptfs/crypto.c +9
-5ecryptfs_set_default_crypt_stat_vals (crypt_stat, mount_crypt_stat);@@ -837,15 +947,19 @@ int ecryptfs_new_file_context(struct inode *ecryptfs_inode) "to the inode key sigs; rc = [%d]\n", rc); goto out; } - cipher_name_len = -strlen (mount_crypt_stat->global_default_cipher_name); -memcpy (crypt_stat->cipher, +strlcpy (crypt_stat->cipher, mount_crypt_stat->global_default_cipher_name, - cipher_name_len); - crypt_stat->cipher[cipher_name_len] ='\0' ; +sizeof (crypt_stat->cipher)); + +strlcpy (crypt_stat->cipher_mode, + mount_crypt_stat->global_default_cipher_mode, +sizeof (crypt_stat->cipher_mode)); + crypt_stat->key_size = mount_crypt_stat->global_default_cipher_key_size;ecryptfs_generate_new_key (crypt_stat); +ecryptfs_generate_new_salt (crypt_stat); + rc =ecryptfs_init_crypt_ctx (crypt_stat);if (rc)fs/ecryptfs/crypto.c +7
-1ecryptfs_printk(KERN_ERR, "Error initializing cryptographic "@@ -971,7 +1085,8 @@ ecryptfs_cipher_code_str_map[] = { {"twofish", RFC2440_CIPHER_TWOFISH}, {"cast6", RFC2440_CIPHER_CAST_6}, {"aes", RFC2440_CIPHER_AES_192}, - {"aes", RFC2440_CIPHER_AES_256} + {"aes", RFC2440_CIPHER_AES_256}, + {"aes_xts", RFC2440_CIPHER_AES_XTS_256} };/** @@ -999,6 +1114,11 @@ u8 ecryptfs_code_for_cipher_string(char *cipher_name, size_t key_bytes)case 32 : code =RFC2440_CIPHER_AES_256 ; } + }else if (strcmp (cipher_name,"aes_xts" ) ==0 ) { +switch (key_bytes) { +case 32 : + code =RFC2440_CIPHER_AES_XTS_256 ; + } }else {for (i =0 ; i <ARRAY_SIZE (ecryptfs_cipher_code_str_map); i++)fs/ecryptfs/crypto.c +15
-0if (strcmp (cipher_name, map[i].cipher_str) ==0 ) {@@ -1038,9 +1158,24 @@ int ecryptfs_read_and_validate_header_region(struct inode *inode) u8 file_size[ECRYPTFS_SIZE_AND_MARKER_BYTES];u8 *marker = file_size +ECRYPTFS_FILE_SIZE_BYTES ;int rc; +unsigned int ra_pages_org; +struct file *lower_file =NULL ; + +if (!inode) +return -EIO ; + lower_file =ecryptfs_inode_to_private (inode)->lower_file; +if (!lower_file) +return -EIO ; + +/*disable read a head mechanism for a while */ + ra_pages_org = lower_file->f_ra.ra_pages; + lower_file->f_ra.ra_pages =0 ; rc =ecryptfs_read_lower (file_size,0 ,ECRYPTFS_SIZE_AND_MARKER_BYTES , inode); + lower_file->f_ra.ra_pages = ra_pages_org; +/* restore read a head mechanism */ +if (rc <ECRYPTFS_SIZE_AND_MARKER_BYTES )return rc >=0 ? -EINVAL : rc;fs/ecryptfs/crypto.c +5
-0rc =ecryptfs_validate_marker (marker);@@ -1430,6 +1565,11 @@ int ecryptfs_read_metadata(struct dentry *ecryptfs_dentry)struct ecryptfs_mount_crypt_stat *mount_crypt_stat = &ecryptfs_superblock_to_private ( ecryptfs_dentry->d_sb)->mount_crypt_stat; +unsigned int ra_pages_org; +struct file *lower_file = +ecryptfs_inode_to_private (ecryptfs_inode)->lower_file; +if (!lower_file) +return -EIO ; ecryptfs_copy_mount_wide_flags_to_inode_flags(crypt_stat,fs/ecryptfs/crypto.c +6
-0mount_crypt_stat);@@ -1441,8 +1581,14 @@ int ecryptfs_read_metadata(struct dentry *ecryptfs_dentry) __func__); goto out; } +/*disable read a head mechanism */ + ra_pages_org = lower_file->f_ra.ra_pages; + lower_file->f_ra.ra_pages =0 ; + rc =ecryptfs_read_lower (page_virt,0 , crypt_stat->extent_size, ecryptfs_inode); + lower_file->f_ra.ra_pages = ra_pages_org;/* restore it back */ +if (rc >=0 ) rc = ecryptfs_read_headers_virt(page_virt, crypt_stat,fs/ecryptfs/crypto.c +0
-0ecryptfs_dentry,fs/ecryptfs/debug.c +13
-0@@ -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 +3
-0@@ -245,6 +245,7 @@ struct ecryptfs_crypt_stat {struct mutex cs_tfm_mutex;struct mutex cs_hash_tfm_mutex;struct mutex cs_mutex; +unsigned char cipher_mode[ECRYPTFS_MAX_CIPHER_NAME_SIZE +1 ]; };/* inode private data. */ @@ -267,6 +268,8 @@ struct ecryptfs_dentry_info { }; }; + +/** * ecryptfs_global_auth_tok - A key used to encrypt all new files under the mountpoint
* @flags: Status flagsfs/ecryptfs/ecryptfs_kernel.h +49
-0@@ -345,6 +348,8 @@ struct ecryptfs_mount_crypt_stat {unsigned char global_default_fn_cipher_name[ECRYPTFS_MAX_CIPHER_NAME_SIZE +1 ];char global_default_fnek_sig[ECRYPTFS_SIG_SIZE_HEX +1 ]; +unsigned char global_default_cipher_mode[ECRYPTFS_MAX_CIPHER_NAME_SIZE + +1 ]; };/* superblock private data. */ @@ -527,6 +532,53 @@ ecryptfs_dentry_to_lower_path(struct dentry *dentry)return &((struct ecryptfs_dentry_info *)dentry->d_fsdata)->lower_path; } +/** +* Given a cipher and mode strings, the function +* concatenates them to create a new string of +* <cipher>_<mode> format. +*/ +static inline unsigned char *ecryptfs_get_full_cipher ( +unsigned char *cipher,unsigned char *mode, +unsigned char *final,size_t final_size) +{ +memset (final,0 , final_size); + +if (strlen (mode) >0 ) { +snprintf (final, final_size,"%s_%s" , cipher, mode); +return final; + } + +return cipher; +} + +/** +* Given a <cipher>[_<mode>] formatted string, the function +* extracts cipher string and/or mode string. +* Note: the passed cipher and/or mode strings will be null-terminated. +*/ +static inline void ecryptfs_parse_full_cipher ( +char *s,char *cipher,char *mode) +{ +char input[2 *ECRYPTFS_MAX_CIPHER_NAME_SIZE +1 +1 ]; +/* +1 for '_'; +1 for '\0' */ +char *p; +char *input_p = input; + +if (s ==NULL || cipher ==NULL ) +return ; + +memset (input,0 ,sizeof (input)); +strlcpy (input, s,sizeof (input)); + + p =strsep (&input_p,"_" ); +strlcpy (cipher, p,ECRYPTFS_MAX_CIPHER_NAME_SIZE +1 ); + + +/* check if mode is specified */ +if (input_p !=NULL && mode !=NULL ) +strlcpy (mode, input_p,ECRYPTFS_MAX_CIPHER_NAME_SIZE +1 ); +} + #define ecryptfs_printk(type, fmt, arg...) \ __ecryptfs_printk(type "%s: " fmt, __func__, ## arg);
__printf(1, 2)fs/ecryptfs/ecryptfs_kernel.h +1
-0@@ -575,6 +627,7 @@ int ecryptfs_encrypt_and_encode_filename( const char *name, size_t name_size);struct dentry *ecryptfs_lower_dentry (struct dentry *this_dentry);void ecryptfs_dump_hex (char *data,int bytes); +void ecryptfs_dump_salt_hex (char *data,int key_size,char *cipher);int virt_to_scatterlist (const void *addr,int size,struct scatterlist *sg,int sg_size);int
ecryptfs_compute_root_iv(struct ecryptfs_crypt_stat *crypt_stat);fs/ecryptfs/ecryptfs_kernel.h +25
-0@@ -718,4 +771,29 @@ int ecryptfs_set_f_namelen(long *namelen, long lower_namelen, int ecryptfs_derive_iv(char *iv, struct ecryptfs_crypt_stat *crypt_stat, loff_t offset); +void clean_inode_pages (struct address_space *mapping, +pgoff_t start,pgoff_t end); + +void ecryptfs_drop_pagecache_sb (struct super_block *sb,void *unused); + +void ecryptfs_free_events (void ); + +void ecryptfs_freepage (struct page *page); + +struct ecryptfs_events *get_events (void ); + +size_t ecryptfs_get_salt_size_for_cipher (const char *cipher); + +size_t ecryptfs_get_key_size_to_enc_data ( +struct ecryptfs_crypt_stat *crypt_stat); + +size_t ecryptfs_get_key_size_to_store_key ( +struct ecryptfs_crypt_stat *crypt_stat); + +size_t ecryptfs_get_key_size_to_restore_key (size_t stored_key_size, +const char *cipher); + +bool ecryptfs_check_space_for_salt (const size_t key_size, +const size_t salt_size); +#endif /* #ifndef ECRYPTFS_KERNEL_H */ fs/ecryptfs/events.c new file +125
-0@@ -0,0 +1,361 @@ +/** +* eCryptfs: Linux filesystem encryption layer +* Copyright (c) 2015, The Linux Foundation. All rights reserved. +* +* This program is free software; you can redistribute it and/or modify +* it under the terms of the GNU General Public License version 2 and +* only version 2 as published by the Free Software Foundation. +* +* This program is distributed in the hope that it will be useful, +* but WITHOUT ANY WARRANTY; without even the implied warranty of +* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +* GNU General Public License for more details. +*/ + +#include <linux/string.h> +#include <linux/ecryptfs.h> +#include <linux/mutex.h> +#include <linux/types.h> +#include <linux/slab.h> +#include <linux/pagemap.h> +#include <linux/random.h> +#include "ecryptfs_kernel.h" + +static DEFINE_MUTEX (events_mutex); +static struct ecryptfs_events *events_ptr; +static int handle; + +void ecryptfs_free_events (void ) +{ +mutex_lock (&events_mutex); +if (events_ptr !=NULL ) { +kfree (events_ptr); + events_ptr =NULL ; + } + +mutex_unlock (&events_mutex); +} + +/** +* Register to ecryptfs events, by passing callback +* functions to be called upon events occurrence. +* The function returns a handle to be passed +* to unregister function. +*/ +int ecryptfs_register_to_events (struct ecryptfs_events *ops) +{ +int ret_value =0 ; + +if (!ops) +return -EINVAL ; + +mutex_lock (&events_mutex); + +if (events_ptr !=NULL ) { +ecryptfs_printk (KERN_ERR , +"already registered!\n" ); + ret_value = -EPERM ; + goto out; + } + events_ptr = +kzalloc (sizeof (struct ecryptfs_events ),GFP_KERNEL ); + +if (!events_ptr) { +ecryptfs_printk (KERN_ERR ,"malloc failure\n" ); + ret_value = -ENOMEM ; + goto out; + } +/* copy the callbacks */ + events_ptr->open_cb = ops->open_cb; + events_ptr->release_cb = ops->release_cb; + events_ptr->encrypt_cb = ops->encrypt_cb; + events_ptr->decrypt_cb = ops->decrypt_cb; + events_ptr->is_cipher_supported_cb = + ops->is_cipher_supported_cb; + events_ptr->is_hw_crypt_cb = ops->is_hw_crypt_cb; + events_ptr->get_salt_key_size_cb = ops->get_salt_key_size_cb; + +get_random_bytes (&handle,sizeof (handle)); + ret_value = handle; + +out: +mutex_unlock (&events_mutex); +return ret_value; +} + +/** +* Unregister from ecryptfs events. +*/ +int ecryptfs_unregister_from_events (int user_handle) +{ +int ret_value =0 ; + +mutex_lock (&events_mutex); + +if (!events_ptr) { + ret_value = -EINVAL ; + goto out; + } +if (user_handle != handle) { + ret_value =ECRYPTFS_INVALID_EVENTS_HANDLE ; + goto out; + } + +kfree (events_ptr); + events_ptr =NULL ; + +out: +mutex_unlock (&events_mutex); +return ret_value; +} + +/** +* This function decides whether the passed file offset +* belongs to ecryptfs metadata or not. +* The caller must pass ecryptfs data, which was received in one +* of the callback invocations. +*/ +bool ecryptfs_is_page_in_metadata (void *data,pgoff_t offset) +{ + +struct ecryptfs_crypt_stat *stat =NULL ; +bool ret = true; + +if (!data) { + ecryptfs_printk(KERN_ERR, "ecryptfs_is_page_in_metadata: invalid dataparameter\n");
fs/ecryptfs/events.c new file +148
-0+ ret = false; + goto end; + } + stat = (struct ecryptfs_crypt_stat *)data; + +if (stat->flags &ECRYPTFS_METADATA_IN_XATTR ) { + ret = false; + goto end; + } + +if (offset >= (stat->metadata_size/PAGE_CACHE_SIZE )) { + ret = false; + goto end; + } +end: +return ret; +} + +/** +* Given two ecryptfs data, the function +* decides whether they are equal. +*/ +inline bool ecryptfs_is_data_equal (void *data1,void *data2) +{ +/* pointer comparison*/ +return data1 == data2; +} + +/** +* Given ecryptfs data, the function +* returns appropriate key size. +*/ +size_t ecryptfs_get_key_size (void *data) +{ + +struct ecryptfs_crypt_stat *stat =NULL ; + +if (!data) +return 0 ; + + stat = (struct ecryptfs_crypt_stat *)data; +return stat->key_size; +} + +/** +* Given ecryptfs data, the function +* returns appropriate salt size. +* +* !!! crypt_stat cipher name and mode must be initialized +*/ +size_t ecryptfs_get_salt_size (void *data) +{ +struct ecryptfs_crypt_stat *stat =NULL ; +unsigned char final[2 *ECRYPTFS_MAX_CIPHER_NAME_SIZE +1 ]; + +if (!data) { +ecryptfs_printk (KERN_ERR , +"ecryptfs_get_salt_size: invalid data parameter\n" ); +return 0 ; + } + + stat = (struct ecryptfs_crypt_stat *)data; +return ecryptfs_get_salt_size_for_cipher ( +ecryptfs_get_full_cipher (stat->cipher, + stat->cipher_mode, + final,sizeof (final))); + +} + +/** +* Given ecryptfs data, the function +* returns appropriate cipher. +*/ +const unsigned char *ecryptfs_get_cipher (void *data) +{ +unsigned char final[2 *ECRYPTFS_MAX_CIPHER_NAME_SIZE +1 ]; +struct ecryptfs_crypt_stat *stat =NULL ; + +if (!data) { +ecryptfs_printk (KERN_ERR , +"ecryptfs_get_cipher: invalid data parameter\n" ); +return NULL ; + } + stat = (struct ecryptfs_crypt_stat *)data; +return ecryptfs_get_full_cipher (stat->cipher, stat->cipher_mode, + final,sizeof (final)); +} + +/** +* Given ecryptfs data, the function +* returns file encryption key. +*/ +const unsigned char *ecryptfs_get_key (void *data) +{ + +struct ecryptfs_crypt_stat *stat =NULL ; + +if (!data) { +ecryptfs_printk (KERN_ERR , +"ecryptfs_get_key: invalid data parameter\n" ); +return NULL ; + } + stat = (struct ecryptfs_crypt_stat *)data; +return stat->key; +} + +/** +* Given ecryptfs data, the function +* returns file encryption salt. +*/ +const unsigned char *ecryptfs_get_salt (void *data) +{ +struct ecryptfs_crypt_stat *stat =NULL ; + +if (!data) { +ecryptfs_printk (KERN_ERR , +"ecryptfs_get_salt: invalid data parameter\n" ); +return NULL ; + } + stat = (struct ecryptfs_crypt_stat *)data; +return stat->key +ecryptfs_get_salt_size (data); +} + +/** +* Returns ecryptfs events pointer +*/ +inline struct ecryptfs_events *get_events (void ) +{ +return events_ptr; +} + +/** +* If external crypto module requires salt in addition to key, +* we store it as part of key array (if there is enough space) +* Checks whether a salt key can fit into array allocated for +* regular key +*/ +bool ecryptfs_check_space_for_salt (const size_t key_size, +const size_t salt_size) +{ +if ((salt_size + key_size) >ECRYPTFS_MAX_KEY_BYTES ) +return false; + +return true; +} + +/* +* If there is salt that is used by external crypto module, it is stored
+ * in the same array where regular key is. Salt is going to be used by
+ * external crypto module only, so for all internal crypto operations salt
+ * should be ignored.
+ *
+ * Get key size in cases where it is going to be used for data encryption
+ * or for all other general purposes
+ */fs/ecryptfs/events.c new file +11
-0+size_t ecryptfs_get_key_size_to_enc_data ( +struct ecryptfs_crypt_stat *crypt_stat) +{ +if (!crypt_stat) +return 0 ; + +return crypt_stat->key_size; +} + +/* +* If there is salt that is used by external crypto module, it is stored
+ * in the same array where regular key is. Salt is going to be used by
+ * external crypto module only, but we still need to save and restore it
+ * (in encrypted form) as part of ecryptfs header along with the regular
+ * key.
+ *
+ * Get key size in cases where it is going to be stored persistently
+ *
+ * !!!crypt_statcipher name and mode must be initialized
+ */fs/ecryptfs/events.c new file +21
-0+size_t ecryptfs_get_key_size_to_store_key ( +struct ecryptfs_crypt_stat *crypt_stat) +{ +size_t salt_size =0 ; + +if (!crypt_stat) +return 0 ; + + salt_size =ecryptfs_get_salt_size (crypt_stat); + +if (!ecryptfs_check_space_for_salt (crypt_stat->key_size, salt_size)) { +ecryptfs_printk (KERN_WARNING , +"ecryptfs_get_key_size_to_store_key: not enough space for salt\n" ); +return crypt_stat->key_size; + } + +return crypt_stat->key_size + salt_size; +} + +/* +* If there is salt that is used by external crypto module, it is stored
+ * in the same array where regular key is. Salt is going to be used by
+ * external crypto module only, but we still need to save and restore it
+ * (in encrypted form) as part of ecryptfs header along with the regular
+ * key.
+ *
+ * Get key size in cases where it is going to be restored from storage
+ *
+ * !!!crypt_statcipher name and mode must be initialized
+ */fs/ecryptfs/events.c new file +13
-0+size_t ecryptfs_get_key_size_to_restore_key (size_t stored_key_size, +const char *cipher) +{ +size_t salt_size =0 ; + +if (!cipher) +return 0 ; + + salt_size =ecryptfs_get_salt_size_for_cipher (cipher); + +if (salt_size >= stored_key_size) { + ecryptfs_printk(KERN_WARNING, + "ecryptfs_get_key_size_to_restore_key: salt %zu >= stred size%zu\n",
fs/ecryptfs/events.c new file +18
-0+ salt_size, stored_key_size); + +return stored_key_size; + } + +return stored_key_size - salt_size; +} + +/** +* Given cipher, the function returns appropriate salt size. +*/ +size_t ecryptfs_get_salt_size_for_cipher (const char *cipher) +{ +if (!get_events () || !(get_events ()->get_salt_key_size_cb)) +return 0 ; + +return get_events ()->get_salt_key_size_cb (cipher); +}fs/ecryptfs/file.c +4
-0@@ -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() */ fs/ecryptfs/file.c +19
-0struct 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:fs/ecryptfs/file.c +13
-0ecryptfs_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 +3
-0@@ -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,fs/ecryptfs/inode.c +1
-0mode);@@ -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 */ fs/ecryptfs/inode.c +7
-0rc =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:fs/ecryptfs/inode.c +0
-0return rc;fs/ecryptfs/keystore.c +5
-3@@ -315,7 +315,8 @@ write_tag_66_packet(char *signature, u8 cipher_code,* | File Encryption Key Size | 1 or 2 bytes | * | File Encryption Key | arbitrary | */ - data_len = (5 +ECRYPTFS_SIG_SIZE_HEX + crypt_stat->key_size); + data_len = (5 +ECRYPTFS_SIG_SIZE_HEX + +ecryptfs_get_key_size_to_store_key (crypt_stat)); *packet =kmalloc (data_len,GFP_KERNEL ); message = *packet;if (!message) {@@ -335,8 +336,9 @@ write_tag_66_packet(char *signature, u8 cipher_code,memcpy (&message[i], signature,ECRYPTFS_SIG_SIZE_HEX ); i +=ECRYPTFS_SIG_SIZE_HEX ;/* The encrypted key includes 1 byte cipher code and 2 byte checksum */ - rc =ecryptfs_write_packet_length (&message[i], crypt_stat->key_size +3 , - &packet_size_len); + rc =ecryptfs_write_packet_length (&message[i], +ecryptfs_get_key_size_to_store_key (crypt_stat) +3 , + &packet_size_len);if (rc) {fs/ecryptfs/keystore.c +4
-3ecryptfs_printk (KERN_ERR ,"Error generating tag 66 packet " "header; cannot generate packet length\n" );@@ -344,9 +346,10 @@ write_tag_66_packet(char *signature, u8 cipher_code, } i += packet_size_len; message[i++] = cipher_code; -memcpy (&message[i], crypt_stat->key, crypt_stat->key_size); - i += crypt_stat->key_size; -for (j =0 ; j < crypt_stat->key_size; j++) +memcpy (&message[i], crypt_stat->key, +ecryptfs_get_key_size_to_store_key (crypt_stat)); + i +=ecryptfs_get_key_size_to_store_key (crypt_stat); +for (j =0 ; j <ecryptfs_get_key_size_to_store_key (crypt_stat); j++) checksum += crypt_stat->key[j]; message[i++] = (checksum /256 ) %256 ;fs/ecryptfs/keystore.c +1
-0message[i++] = (checksum %256 );@@ -918,6 +921,7 @@ ecryptfs_parse_tag_70_packet(char **filename, size_t *filename_size, struct ecryptfs_parse_tag_70_packet_silly_stack *s;struct key *auth_tok_key =NULL ;int rc =0 ; +char full_cipher[ECRYPTFS_MAX_CIPHER_NAME_SIZE ]; (*packet_size) =0 ;fs/ecryptfs/keystore.c +2
-1(*filename_size) =0 ;@@ -977,12 +981,13 @@ ecryptfs_parse_tag_70_packet(char **filename,size_t *filename_size, s->fnek_sig_hex[ECRYPTFS_SIG_SIZE_HEX] = '\0'; (*packet_size) +=ECRYPTFS_SIG_SIZE ; s->cipher_code = data[(*packet_size)++]; - rc =ecryptfs_cipher_code_to_string (s->cipher_string, s->cipher_code); + rc =ecryptfs_cipher_code_to_string (full_cipher, s->cipher_code);if (rc) {printk (KERN_WARNING "%s: Cipher code [%d] is invalid\n" , __func__, s->cipher_code); goto out; } +ecryptfs_parse_full_cipher (full_cipher, s->cipher_string,0 ); rc = ecryptfs_find_auth_tok_for_sig(&auth_tok_key, &s->auth_tok, mount_crypt_stat,fs/ecryptfs/keystore.c +1
-0s->fnek_sig_hex);@@ -1151,6 +1156,7 @@ decrypt_pki_encrypted_session_key(struct ecryptfs_auth_tok *auth_tok, char *payload =NULL ;size_t payload_len =0 ;int rc; +char full_cipher[ECRYPTFS_MAX_CIPHER_NAME_SIZE ]; rc =ecryptfs_get_auth_tok_sig (&auth_tok_sig, auth_tok);fs/ecryptfs/keystore.c +16
-6if (rc) {@@ -1184,21 +1190,31 @@ decrypt_pki_encrypted_session_key(structecryptfs_auth_tok *auth_tok, rc); goto out; } - auth_tok->session_key.flags |=ECRYPTFS_CONTAINS_DECRYPTED_KEY ; -memcpy (crypt_stat->key, auth_tok->session_key.decrypted_key, - auth_tok->session_key.decrypted_key_size); - crypt_stat->key_size = auth_tok->session_key.decrypted_key_size; - rc =ecryptfs_cipher_code_to_string (crypt_stat->cipher, cipher_code); + + rc =ecryptfs_cipher_code_to_string (full_cipher, cipher_code);if (rc) {ecryptfs_printk (KERN_ERR ,"Cipher code [%d] is invalid\n" , cipher_code) - goto out; + goto out; } + + auth_tok->session_key.flags |=ECRYPTFS_CONTAINS_DECRYPTED_KEY ; +memcpy (crypt_stat->key, auth_tok->session_key.decrypted_key, + auth_tok->session_key.decrypted_key_size); + crypt_stat->key_size =ecryptfs_get_key_size_to_restore_key ( + auth_tok->session_key.decrypted_key_size, full_cipher); + +ecryptfs_parse_full_cipher (full_cipher, + crypt_stat->cipher, crypt_stat->cipher_mode); + crypt_stat->flags |=ECRYPTFS_KEY_VALID ;if (ecryptfs_verbosity >0 ) {ecryptfs_printk (KERN_DEBUG ,"Decrypted session key:\n" );ecryptfs_dump_hex (crypt_stat->key, crypt_stat->key_size); + +ecryptfs_dump_salt_hex (crypt_stat->key, crypt_stat->key_size, + full_cipher); } out:fs/ecryptfs/keystore.c +1
-0kfree (msg);@@ -1380,6 +1396,7 @@ parse_tag_3_packet(struct ecryptfs_crypt_stat *crypt_stat, struct ecryptfs_auth_tok_list_item *auth_tok_list_item;size_t length_size;int rc =0 ; +char full_cipher[ECRYPTFS_MAX_CIPHER_NAME_SIZE ]; (*packet_size) =0 ;fs/ecryptfs/keystore.c +4
-1(*new_auth_tok) =NULL ;@@ -1453,10 +1470,13 @@ parse_tag_3_packet(struct ecryptfs_crypt_stat *crypt_stat, rc = -EINVAL ; goto out_free; } - rc =ecryptfs_cipher_code_to_string (crypt_stat->cipher, + rc =ecryptfs_cipher_code_to_string (full_cipher, (u16 )data[(*packet_size)]);if (rc) goto out_free; +ecryptfs_parse_full_cipher (full_cipher, + crypt_stat->cipher, crypt_stat->cipher_mode); +/* A little extra work to differentiate among the AES key * sizes; see RFC2440 */ fs/ecryptfs/keystore.c +4
-1switch (data[(*packet_size)++]) {@@ -1465,7 +1485,10 @@ parse_tag_3_packet(struct ecryptfs_crypt_stat *crypt_stat, break;default : crypt_stat->key_size = - (*new_auth_tok)->session_key.encrypted_key_size; +ecryptfs_get_key_size_to_restore_key ( + (*new_auth_tok)->session_key.encrypted_key_size, + full_cipher); + } rc =ecryptfs_init_crypt_ctx (crypt_stat);fs/ecryptfs/keystore.c +2
-0if (rc)@@ -1664,6 +1687,8 @@ static intdecrypt_passphrase_encrypted_session_key (struct ecryptfs_auth_tok *auth_tok, struct ecryptfs_crypt_stat *crypt_stat) { + + unsigned char final[2*ECRYPTFS_MAX_CIPHER_NAME_SIZE+1];struct scatterlist dst_sg[2 ];struct scatterlist src_sg[2 ];fs/ecryptfs/keystore.c +1
-1struct mutex *tfm_mutex;@@ -1713,7 +1738,7 @@ decrypt_passphrase_encrypted_session_key(structecryptfs_auth_tok *auth_tok,mutex_lock (tfm_mutex ); rc =crypto_blkcipher_setkey ( desc.tfm, auth_tok->token.password.session_key_encryption_key, - crypt_stat->key_size); + auth_tok->token.password.session_key_encryption_key_bytes);if (unlikely (rc <0 )) {mutex_unlock (tfm_mutex);fs/ecryptfs/keystore.c +4
-0printk (KERN_ERR "Error setting key for crypto context\n" );@@ -1736,6 +1761,10 @@ decrypt_passphrase_encrypted_session_key(struct ecryptfs_auth_tok *auth_tok, crypt_stat->key_size);ecryptfs_dump_hex (crypt_stat->key, crypt_stat->key_size); +ecryptfs_dump_salt_hex (crypt_stat->key, crypt_stat->key_size, +ecryptfs_get_full_cipher (crypt_stat->cipher, + crypt_stat->cipher_mode, + final,sizeof (final))); } out:fs/ecryptfs/keystore.c +9
-4return rc;@@ -1972,12 +2001,17 @@ pki_encrypt_session_key(struct key *auth_tok_key, size_t payload_len = 0;struct ecryptfs_message *msg;int rc; +unsigned char final[2 *ECRYPTFS_MAX_CIPHER_NAME_SIZE +1 ]; rc =write_tag_66_packet (auth_tok->token.private_key.signature, -ecryptfs_code_for_cipher_string ( - crypt_stat->cipher, - crypt_stat->key_size), - crypt_stat, &payload, &payload_len); +ecryptfs_code_for_cipher_string ( +ecryptfs_get_full_cipher ( + crypt_stat->cipher, + crypt_stat->cipher_mode, + final,sizeof (final)), +ecryptfs_get_key_size_to_enc_data ( + crypt_stat)), + crypt_stat, &payload, &payload_len);up_write (&(auth_tok_key->sem));key_put (auth_tok_key);fs/ecryptfs/keystore.c +1
-1if (rc) {@@ -2035,7 +2069,7 @@ write_tag_1_packet(char *dest, size_t *remaining_bytes,ecryptfs_from_hex (key_rec->sig, auth_tok->token.private_key.signature,ECRYPTFS_SIG_SIZE ); encrypted_session_key_valid =0 ; -for (i =0 ; i < crypt_stat->key_size; i++) +for (i =0 ; i <ecryptfs_get_key_size_to_store_key (crypt_stat); i++) encrypted_session_key_valid |= auth_tok->session_key.encrypted_key[i];fs/ecryptfs/keystore.c +1
-0if (encrypted_session_key_valid) {@@ -2189,6 +2223,7 @@ write_tag_3_packet(char *dest, size_t *remaining_bytes, u8 cipher_code;size_t packet_size_length;size_t max_packet_size; +unsigned char final[2 *ECRYPTFS_MAX_CIPHER_NAME_SIZE +1 ];struct ecryptfs_mount_crypt_stat *mount_crypt_stat = crypt_stat->mount_crypt_stat;fs/ecryptfs/keystore.c +3
-2struct blkcipher_desc desc = {@@ -2221,13 +2256,14 @@ write_tag_3_packet(char *dest, size_t *remaining_bytes, mount_crypt_stat->global_default_cipher_key_size;if (auth_tok->session_key.encrypted_key_size ==0 ) auth_tok->session_key.encrypted_key_size = - crypt_stat->key_size; +ecryptfs_get_key_size_to_store_key (crypt_stat);if (crypt_stat->key_size ==24 &&strcmp ("aes" , crypt_stat->cipher) ==0 ) {memset ((crypt_stat->key +24 ),0 ,8 ); auth_tok->session_key.encrypted_key_size =32 ; }else - auth_tok->session_key.encrypted_key_size = crypt_stat->key_size; + auth_tok->session_key.encrypted_key_size = +ecryptfs_get_key_size_to_store_key (crypt_stat); key_rec->enc_key_size = auth_tok->session_key.encrypted_key_size;fs/ecryptfs/keystore.c +2
-2encrypted_session_key_valid =0 ;@@ -2251,8 +2287,8 @@ write_tag_3_packet(char *dest, size_t *remaining_bytes, auth_tok->token.password. session_key_encryption_key_bytes);memcpy (session_key_encryption_key, - auth_tok->token.password.session_key_encryption_key, - crypt_stat->key_size); + auth_tok->token.password.session_key_encryption_key, + auth_tok->token.password.session_key_encryption_key_bytes);ecryptfs_printk (KERN_DEBUG ,"Cached session key encryption key:\n" );fs/ecryptfs/keystore.c +1
-1if (ecryptfs_verbosity >0 )@@ -2285,7 +2321,7 @@ write_tag_3_packet(char *dest, size_t *remaining_bytes, }mutex_lock (tfm_mutex); rc =crypto_blkcipher_setkey (desc.tfm, session_key_encryption_key, - crypt_stat->key_size); + auth_tok->token.password.session_key_encryption_key_bytes);if (rc <0 ) {mutex_unlock (tfm_mutex);fs/ecryptfs/keystore.c +6
-1ecryptfs_printk(KERN_ERR, "Error setting key for crypto "@@ -2294,7 +2330,12 @@ write_tag_3_packet(char *dest, size_t *remaining_bytes, } rc =0 ;ecryptfs_printk (KERN_DEBUG ,"Encrypting [%zd] bytes of the key\n" , - crypt_stat->key_size); + crypt_stat->key_size); +ecryptfs_printk (KERN_DEBUG ,"Encrypting [%zd] bytes of the salt key\n", +ecryptfs_get_salt_size_for_cipher ( +ecryptfs_get_full_cipher (crypt_stat->cipher, + crypt_stat->cipher_mode, + final,sizeof (final)))); rc = crypto_blkcipher_encrypt(&desc, dst_sg, src_sg,fs/ecryptfs/keystore.c +4
-2(*key_rec).enc_key_size);mutex_unlock (tfm_mutex);@@ -2343,8 +2384,10 @@ encrypted_session_key_set: dest[(*packet_size)++] =0x04 ;/* version 4 */ /* TODO: Break from RFC2440 so that arbitrary ciphers can be * specified with strings */ - cipher_code =ecryptfs_code_for_cipher_string (crypt_stat->cipher, - crypt_stat->key_size); + cipher_code =ecryptfs_code_for_cipher_string ( +ecryptfs_get_full_cipher (crypt_stat->cipher, + crypt_stat->cipher_mode, final,sizeof (final)), + crypt_stat->key_size);if (cipher_code ==0 ) {ecryptfs_printk (KERN_WARNING ,"Unable to generate code for " "cipher [%s]\n" , crypt_stat->cipher);fs/ecryptfs/main.c +7
-0@@ -165,7 +165,13 @@ void ecryptfs_put_lower_file(struct inode *inode)fput (inode_info->lower_file); inode_info->lower_file =NULL ;mutex_unlock (&inode_info->lower_file_mutex); + +if (get_events () &&get_events ()->release_cb) +get_events ()->release_cb ( +ecryptfs_inode_to_lower (inode)); } + + }enum { ecryptfs_opt_sig, ecryptfs_opt_ecryptfs_sig,@@ -266,6 +272,7 @@ static int ecryptfs_parse_options(struct ecryptfs_sb_info *sbi, char *options, int cipher_key_bytes_set = 0;int fn_cipher_key_bytes;int fn_cipher_key_bytes_set =0 ; +size_t salt_size =0 ;struct ecryptfs_mount_crypt_stat *mount_crypt_stat = &sbi->mount_crypt_stat;fs/ecryptfs/main.c +8
-5substring_t args[MAX_OPT_ARGS ];@@ -280,6 +287,7 @@ static int ecryptfs_parse_options(struct ecryptfs_sb_info *sbi, char *options, char *cipher_key_bytes_src;char *fn_cipher_key_bytes_src;u8 cipher_code; +unsigned char final[2 *ECRYPTFS_MAX_CIPHER_NAME_SIZE +1 ]; *check_ruid =0 ;@@ -309,12 +317,14 @@ static int ecryptfs_parse_options(struct ecryptfs_sb_info *sbi, char *options, case ecryptfs_opt_ecryptfs_cipher: cipher_name_src = args[0 ].from; cipher_name_dst = - mount_crypt_stat-> - global_default_cipher_name; -strncpy (cipher_name_dst, cipher_name_src, -ECRYPTFS_MAX_CIPHER_NAME_SIZE ); - cipher_name_dst[ECRYPTFS_MAX_CIPHER_NAME_SIZE ] ='\0' ; + mount_crypt_stat->global_default_cipher_name; + +ecryptfs_parse_full_cipher (cipher_name_src, + mount_crypt_stat->global_default_cipher_name, + mount_crypt_stat->global_default_cipher_mode); + cipher_name_set =1 ; +break ;case ecryptfs_opt_ecryptfs_key_bytes:fs/ecryptfs/main.c +31
-5cipher_key_bytes_src = args[0 ].from;@@ -411,24 +421,50 @@ static int ecryptfs_parse_options(struct ecryptfs_sb_info *sbi, char *options,strcpy (mount_crypt_stat->global_default_cipher_name,ECRYPTFS_DEFAULT_CIPHER ); } +if ((mount_crypt_stat->flags &ECRYPTFS_GLOBAL_ENCRYPT_FILENAMES ) && !fn_cipher_name_set)strcpy (mount_crypt_stat->global_default_fn_cipher_name, mount_crypt_stat->global_default_cipher_name); -if (!cipher_key_bytes_set) + +if (cipher_key_bytes_set) { + + salt_size =ecryptfs_get_salt_size_for_cipher ( +ecryptfs_get_full_cipher ( + mount_crypt_stat->global_default_cipher_name, + mount_crypt_stat->global_default_cipher_mode, + final,sizeof (final))); + +if (!ecryptfs_check_space_for_salt ( + mount_crypt_stat->global_default_cipher_key_size, + salt_size)) { +ecryptfs_printk ( +KERN_WARNING , +"eCryptfs internal error: no space for salt" ); + } + }else mount_crypt_stat->global_default_cipher_key_size =0 ; +if ((mount_crypt_stat->flags &ECRYPTFS_GLOBAL_ENCRYPT_FILENAMES ) && !fn_cipher_key_bytes_set) mount_crypt_stat->global_default_fn_cipher_key_bytes = mount_crypt_stat->global_default_cipher_key_size; cipher_code =ecryptfs_code_for_cipher_string ( - 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)), mount_crypt_stat->global_default_cipher_key_size);if (!cipher_code) { -ecryptfs_printk (KERN_ERR , -"eCryptfs doesn't support cipher: %s" , - mount_crypt_stat->global_default_cipher_name); +ecryptfs_printk ( +KERN_ERR , +"eCryptfs doesn't support cipher: %s and key size %zu" , +ecryptfs_get_full_cipher ( + mount_crypt_stat->global_default_cipher_name, + mount_crypt_stat->global_default_cipher_mode, + final,sizeof (final)), + mount_crypt_stat->global_default_cipher_key_size); rc = -EINVAL ; goto out;fs/ecryptfs/main.c +1
-0}@@ -488,6 +524,7 @@ static struct file_system_type ecryptfs_fs_type;* @dev_name: The path to mount over * @raw_data: The options passed into the kernel */ +static struct dentry *ecryptfs_mount (struct file_system_type *fs_type,int flags,const char *dev_name,void *raw_data){
fs/ecryptfs/main.c +0
-0@@ -557,6 +594,8 @@ static struct dentry *ecryptfs_mount(struct
file_system_type*fs_type, int flagsfs/ecryptfs/main.c +2
-0ecryptfs_set_superblock_lower (s, path.dentry->d_sb); +ecryptfs_drop_pagecache_sb (ecryptfs_superblock_to_lower (s),NULL ); +/** * Set the POSIX ACL flag based on whether they're enabled in the lower
fs/ecryptfs/main.c +1
-0* mount. @@ -894,6 +933,7 @@ static void __exit ecryptfs_exit(void)do_sysfs_unregistration ();unregister_filesystem (&ecryptfs_fs_type);ecryptfs_free_kmem_caches (); +ecryptfs_free_events (); }MODULE_AUTHOR ("Michael A. Halcrow <[email protected]>" );fs/ecryptfs/mmap.c +6
-0@@ -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 +6
-0@@ -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); + }/**
fs/ecryptfs/super.c +3
-0@@ -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);fs/ecryptfs/super.c +4
-1list_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)fs/ecryptfs/super.c +0
-0seq_printf(m, ",ecryptfs_key_bytes=%zd",include/linux/ecryptfs.h +14
-0@@ -1,6 +1,9 @@#ifndef _LINUX_ECRYPTFS_H#define _LINUX_ECRYPTFS_H +struct inode ; +struct page ; +/* Version verification for shared data structures w/ userspace */ #define ECRYPTFS_VERSION_MAJOR 0x00#define ECRYPTFS_VERSION_MINOR 0x04@@ -41,6 +44,7 @@#define RFC2440_CIPHER_AES_256 0x09#define RFC2440_CIPHER_TWOFISH 0x0a#define RFC2440_CIPHER_CAST_6 0x0b +#define RFC2440_CIPHER_AES_XTS_256 0x0c#define RFC2440_CIPHER_RSA 0x01@@ -102,4 +106,47 @@ struct ecryptfs_auth_tok { } token; } __attribute__ ((packed)); +#define ECRYPTFS_INVALID_EVENTS_HANDLE -1 + +/** +* ecryptfs_events struct represents a partial interface +* towards ecryptfs module. If registered to ecryptfs events, +* one can receive push notifications. +* A first callback received from ecryptfs will probably be +* about file opening (open_cb), +* in which ecryptfs passes its ecryptfs_data for future usage. +* This data represents a file and must be passed in every query functions
+ * such asecryptfs_get_key_size(),ecryptfs_get_cipher()etc.
+ */include/linux/ecryptfs.h +30
-0+struct ecryptfs_events { +bool (*is_cipher_supported_cb)(const char *cipher); +void (*open_cb)(struct inode *inode,void *ecrytpfs_data); +void (*release_cb)(struct inode *inode); +int (*encrypt_cb)(struct page *in_page,struct page *out_page, +struct inode *inode,unsigned long extent_offset); +int (*decrypt_cb)(struct page *in_page,struct page *out_page, +struct inode *inode,unsigned long extent_offset); +bool (*is_hw_crypt_cb)(void ); +size_t (*get_salt_key_size_cb)(const char *cipher); +}; + + +int ecryptfs_register_to_events (struct ecryptfs_events *ops); + +int ecryptfs_unregister_from_events (int user_handle); + +const unsigned char *ecryptfs_get_key (void *ecrytpfs_data); + +size_t ecryptfs_get_key_size (void *ecrytpfs_data); + +const unsigned char *ecryptfs_get_salt (void *ecrytpfs_data); + +size_t ecryptfs_get_salt_size (void *ecrytpfs_data); + +const unsigned char *ecryptfs_get_cipher (void *ecrytpfs_data); + +bool ecryptfs_is_page_in_metadata (void *ecrytpfs_data,pgoff_t offset); + +bool ecryptfs_is_data_equal(void *ecrytpfs_data1, void
*ecrytpfs_data2);
+
#endif /*_LINUX_ECRYPTFS_H*/
--
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
Quoted 5 lines · On 2015-11-09 20:56:02, [email protected] wrote:
Hello, Tyler
I'll try to provide more detailed explanation, should it be satisfactory enough I will update the patch description.
Quoted 7 lines
The problem with current eCryptfs is that it has total control on how and when the encryption is performed and this control can't be altered. One example when this can be a problem is when we want to utilize an underlying inline HW encryption engine which allows encrypting blocks 'on the fly' as they are being written to the storage. In such a case relevant blocks just need to be marked as 'should be encrypted'. No actual encryption should be done by eCryptfs as it will be much slower.
Is this a hardware crypto accelerator? If so, why not create a crypto api driver so all subsystems can take advantage of the acceleration instead of baking support into individual subsystems?
Quoted 7 lines
The provided framework allows transferring this control (if needed) to some external module which will do the encryption itfelf or just mark the appropriate blocks.
There is no caller for
ecryptfs_register_to_events()since this change only provides framework, it doesn't provide the module itself, the module could be HW dependent.
Will the code that you plan to call ecryptfs_register_to_events() be upstream? If so, have you posted it?
Tyler
Quoted 1,771 lines
Regarding the mounting option, it merely serves as example of new cipher mode that can be served by registered module.
There is a special callback function that should be implemented by the registered module that tells whether a particular cipher is supported by it :is_cipher_supported_cb()The mounting option itself is not necessary, I can remove it
Quoted 1,761 lines
Hello Andrey!
Quoted 11 lines · Andrey Markovytch
From: Andrey Markovytch <[email protected]>
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"What is "HW/external mode"? There's no description in the commit message and
ecryptfs_register_to_events()does not have a caller so I'm not sure of the purpose. Please provide more context.Despite not yet understanding the purpose of this patch, I think that I can safely say that "aes-xts" is not an appropriate mount option to use when enabling this mode. eCryptfs may support XTS mode one day, using the Crypto API, so "HW/external mode" should not own the mount option.
Tyler
Quoted 1,734 lines
Signed-off-by Lina Zarivach <[email protected]>Signed-off-by Andrey Markovytch <[email protected]>--- 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.cfs/ecryptfs/Makefile +2
-2@@ -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.oecryptfs-$(CONFIG_ECRYPT_FS_MESSAGING) += messaging.o miscdev.o
fs/ecryptfs/caches_utils.c new file +78
-0@@ -0,0 +1,78 @@ +/* +* Copyright (c) 2015, The Linux Foundation. All rights reserved. +* +* This program is free software; you can redistribute it and/or modify +* it under the terms of the GNU General Public License version 2 and +* only version 2 as published by the Free Software Foundation. +* +* This program is distributed in the hope that it will be useful, +* but WITHOUT ANY WARRANTY; without even the implied warranty of +* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +* GNU General Public License for more details. +*/ + +#include <linux/kernel.h> +#include <linux/fs.h> +#include <linux/spinlock.h> +#include <linux/pagemap.h> +#include <linux/pagevec.h> + +#include "../internal.h" + +void ecryptfs_drop_pagecache_sb (struct super_block *sb,void *unused) +{ +struct inode *inode, *toput_inode =NULL ; + +spin_lock (&sb->s_inode_list_lock); +list_for_each_entry (inode, &sb->s_inodes, i_sb_list) { +spin_lock (&inode->i_lock); +if ((inode->i_state & (I_FREEING |I_WILL_FREE |I_NEW )) || + (inode->i_mapping->nrpages ==0 )) { +spin_unlock (&inode->i_lock); +continue ; + } +__iget (inode); +spin_unlock (&inode->i_lock); +spin_unlock (&sb->s_inode_list_lock); + +invalidate_mapping_pages (inode->i_mapping,0 ,-1 ); +iput (toput_inode); + toput_inode = inode; + +spin_lock (&sb->s_inode_list_lock); + } +spin_unlock (&sb->s_inode_list_lock); +iput (toput_inode); +} + +void clean_inode_pages (struct address_space *mapping, +pgoff_t start,pgoff_t end) +{ +struct pagevec pvec; +pgoff_t index = start; +int i; + +pagevec_init (&pvec,0 ); +while (index <= end &&pagevec_lookup (&pvec, mapping, index, +min (end - index, + (pgoff_t )PAGEVEC_SIZE -1 ) +1 )) { +for (i =0 ; i <pagevec_count (&pvec); i++) { +struct page *page = pvec.pages[i]; + +/* We rely upon deletion +* not changing page->index +*/ + index = page->index; +if (index > end) +break ; +if (!trylock_page (page)) +continue ; +WARN_ON (page->index != index); +zero_user (page,0 ,PAGE_CACHE_SIZE ); +unlock_page (page); + } +pagevec_release (&pvec); +cond_resched (); + index++; + } +}fs/ecryptfs/crypto.c +3
-2@@ -35,6 +35,7 @@#include <linux/scatterlist.h> #include <linux/slab.h> #include <asm/unaligned.h> +#include <linux/ecryptfs.h> #include "ecryptfs_kernel.h" #define DECRYPT 0@@ -350,9 +351,9 @@ static int crypt_scatterlist(struct ecryptfs_crypt_stat *crypt_stat, || !(crypt_stat->flags & ECRYPTFS_STRUCT_INITIALIZED));if (unlikely (ecryptfs_verbosity >0 )) {ecryptfs_printk (KERN_DEBUG ,"Key size [%zd]; key:\n" , - crypt_stat->key_size); +ecryptfs_get_key_size_to_enc_data (crypt_stat));ecryptfs_dump_hex (crypt_stat->key, - crypt_stat->key_size); +ecryptfs_get_key_size_to_enc_data (crypt_stat)); }fs/ecryptfs/crypto.c +1
-1init_completion (&ecr.completion);@@ -371,7 +372,7 @@ static int crypt_scatterlist(struct ecryptfs_crypt_stat *crypt_stat,/* Consider doing this once, when the file is opened */ if (!(crypt_stat->flags & ECRYPTFS_KEY_SET)) { rc =crypto_ablkcipher_setkey (crypt_stat->tfm, crypt_stat->key, - crypt_stat->key_size); +ecryptfs_get_key_size_to_enc_data (crypt_stat));if (rc) { ecryptfs_printk(KERN_ERR,fs/ecryptfs/crypto.c +25
-0"Error setting key; rc = [%d]\n",@@ -466,6 +467,31 @@ out:return rc; } +static void init_ecryption_parameters (bool *hw_crypt,bool *cipher_supported, +struct ecryptfs_crypt_stat *crypt_stat) +{ +unsigned char final[2 *ECRYPTFS_MAX_CIPHER_NAME_SIZE +1 ]; + +if (!hw_crypt || !cipher_supported) +return ; + + *cipher_supported = false; + *hw_crypt = false; + +if (get_events () &&get_events ()->is_cipher_supported_cb) { + *cipher_supported = +get_events ()->is_cipher_supported_cb ( +ecryptfs_get_full_cipher (crypt_stat->cipher, + crypt_stat->cipher_mode, final,sizeof (final))); +if (*cipher_supported) { +/* we should apply external algorythm +* assume that is_hw_crypt() cbck is supplied +*/ + *hw_crypt =get_events ()->is_hw_crypt_cb (); + } + } +} +/** * ecryptfs_encrypt_page * @page: Page mapped from the eCryptfs inode for the file; contains
fs/ecryptfs/crypto.c +47
-13@@ -491,11 +517,18 @@ int ecryptfs_encrypt_page(struct page *page)loff_t extent_offset;loff_t lower_offset;int rc =0 ; +bool is_hw_crypt; +bool is_cipher_supported; + ecryptfs_inode = page->mapping->host; crypt_stat = &(ecryptfs_inode_to_private (ecryptfs_inode)->crypt_stat);BUG_ON (!(crypt_stat->flags &ECRYPTFS_ENCRYPTED )); + +init_ecryption_parameters (&is_hw_crypt, + &is_cipher_supported, crypt_stat); + enc_extent_page =alloc_page (GFP_USER );if (!enc_extent_page) { rc = -ENOMEM ;@@ -503,24 +536,51 @@ int ecryptfs_encrypt_page(struct page *page) "encrypted extent\n"); goto out; } - -for (extent_offset =0 ; - extent_offset < (PAGE_CACHE_SIZE / crypt_stat->extent_size); - extent_offset++) { - rc =crypt_extent (crypt_stat, enc_extent_page, page, - extent_offset,ENCRYPT ); -if (rc) { -printk (KERN_ERR "%s: Error encrypting extent; " -"rc = [%d]\n" , __func__, rc); - goto out; - } +if (is_hw_crypt) { +/* no need for encryption */ + }else { +for (extent_offset =0 ; + extent_offset < + (PAGE_CACHE_SIZE / crypt_stat->extent_size); + extent_offset++) { + +if (is_cipher_supported) { +if (!get_events ()->encrypt_cb) { + rc = -EPERM ; + goto out; + } + rc =get_events ()->encrypt_cb (page, + enc_extent_page, +ecryptfs_inode_to_lower ( + ecryptfs_inode), + extent_offset); + }else { + rc =crypt_extent (crypt_stat, + enc_extent_page, page, + extent_offset,ENCRYPT ); + } +if (rc) { +ecryptfs_printk (KERN_ERR , +"%s: Error encrypting; rc = [%d]\n" , + __func__, rc); + goto out; + } + } } lower_offset =lower_offset_for_page (crypt_stat, page); - enc_extent_virt =kmap (enc_extent_page); +if (is_hw_crypt) + enc_extent_virt =kmap (page); +else + enc_extent_virt =kmap (enc_extent_page); + rc =ecryptfs_write_lower (ecryptfs_inode, enc_extent_virt, lower_offset,PAGE_CACHE_SIZE ); -kunmap (enc_extent_page); +if (!is_hw_crypt) +kunmap (enc_extent_page); +else +kunmap (page); +if (rc <0 ) { ecryptfs_printk(KERN_ERR,fs/ecryptfs/crypto.c +25
-3"Error attempting to write lower page; rc = [%d]\n",@@ -559,6 +619,8 @@ int ecryptfs_decrypt_page(struct page *page)unsigned long extent_offset;loff_t lower_offset;int rc =0 ; +bool is_cipher_supported; +bool is_hw_crypt; ecryptfs_inode = page->mapping->host; crypt_stat =@@ -577,13 +639,33 @@ int ecryptfs_decrypt_page(struct page *page) goto out; } +init_ecryption_parameters (&is_hw_crypt, + &is_cipher_supported, crypt_stat); + +if (is_hw_crypt) { + rc =0 ; +return rc; + } +for (extent_offset =0 ; extent_offset < (PAGE_CACHE_SIZE / crypt_stat->extent_size); extent_offset++) { - rc =crypt_extent (crypt_stat, page, page, +if (is_cipher_supported) { +if (!get_events ()->decrypt_cb) { + rc = -EPERM ; + goto out; + } + + rc =get_events ()->decrypt_cb (page, page, +ecryptfs_inode_to_lower (ecryptfs_inode), + extent_offset); + + }else + rc =crypt_extent (crypt_stat, page, page, extent_offset,DECRYPT ); +if (rc) { -printk (KERN_ERR "%s: Error encrypting extent; " +ecryptfs_printk (KERN_ERR ,"%s: Error decrypting extent;" "rc = [%d]\n" , __func__, rc); goto out; }@@ -612,7 +694,7 @@ int ecryptfs_init_crypt_ctx(struct ecryptfs_crypt_stat *crypt_stat)"Initializing cipher [%s]; strlen = [%d]; " "key_size_bits = [%zd]\n" , crypt_stat->cipher, (int )strlen (crypt_stat->cipher), - crypt_stat->key_size << 3); + ecryptfs_get_key_size_to_enc_data(crypt_stat) << 3);mutex_lock (&crypt_stat->cs_tfm_mutex);if (crypt_stat->tfm) {fs/ecryptfs/crypto.c +1
-1rc =0 ;@@ -694,7 +776,7 @@ int ecryptfs_compute_root_iv(struct ecryptfs_crypt_stat *crypt_stat) goto out; } rc =ecryptfs_calculate_md5 (dst, crypt_stat, crypt_stat->key, - crypt_stat->key_size); +ecryptfs_get_key_size_to_enc_data (crypt_stat));if (rc) { ecryptfs_printk(KERN_WARNING, "Error attempting to compute "fs/ecryptfs/crypto.c +29
-0"MD5 while generating root IV\n");@@ -721,6 +803,35 @@ static void ecryptfs_generate_new_key(struct ecryptfs_crypt_stat *crypt_stat) } } +static int ecryptfs_generate_new_salt (struct ecryptfs_crypt_stat *crypt_stat) +{ +size_t salt_size =0 ; +unsigned char final[2 *ECRYPTFS_MAX_CIPHER_NAME_SIZE +1 ]; + + salt_size =ecryptfs_get_salt_size_for_cipher ( +ecryptfs_get_full_cipher (crypt_stat->cipher, + crypt_stat->cipher_mode, + final,sizeof (final))); + +if (salt_size ==0 ) +return 0 ; + +if (!ecryptfs_check_space_for_salt (crypt_stat->key_size, salt_size)) { +ecryptfs_printk (KERN_WARNING ,"not enough space for salt\n" ); + crypt_stat->flags |=ECRYPTFS_SECURITY_WARNING ; +return -EINVAL ; + } + +get_random_bytes (crypt_stat->key + crypt_stat->key_size, salt_size); +if (unlikely (ecryptfs_verbosity >0 )) { +ecryptfs_printk (KERN_DEBUG ,"Generated new session salt:\n" ); +ecryptfs_dump_hex (crypt_stat->key + crypt_stat->key_size, + salt_size); + } + +return 0 ; +} +/** *
ecryptfs_copy_mount_wide_flags_to_inode_flags
* @crypt_stat: The inode's cryptographic contextfs/ecryptfs/crypto.c +0
-1@@ -823,7 +934,6 @@ int ecryptfs_new_file_context(struct inode *ecryptfs_inode)struct ecryptfs_mount_crypt_stat *mount_crypt_stat = &ecryptfs_superblock_to_private ( ecryptfs_inode->i_sb)->mount_crypt_stat; -int cipher_name_len;int rc =0 ;fs/ecryptfs/crypto.c +9
-5ecryptfs_set_default_crypt_stat_vals (crypt_stat, mount_crypt_stat);@@ -837,15 +947,19 @@ int ecryptfs_new_file_context(struct inode *ecryptfs_inode) "to the inode key sigs; rc = [%d]\n", rc); goto out; } - cipher_name_len = -strlen (mount_crypt_stat->global_default_cipher_name); -memcpy (crypt_stat->cipher, +strlcpy (crypt_stat->cipher, mount_crypt_stat->global_default_cipher_name, - cipher_name_len); - crypt_stat->cipher[cipher_name_len] ='\0' ; +sizeof (crypt_stat->cipher)); + +strlcpy (crypt_stat->cipher_mode, + mount_crypt_stat->global_default_cipher_mode, +sizeof (crypt_stat->cipher_mode)); + crypt_stat->key_size = mount_crypt_stat->global_default_cipher_key_size;ecryptfs_generate_new_key (crypt_stat); +ecryptfs_generate_new_salt (crypt_stat); + rc =ecryptfs_init_crypt_ctx (crypt_stat);if (rc)fs/ecryptfs/crypto.c +7
-1ecryptfs_printk(KERN_ERR, "Error initializing cryptographic "@@ -971,7 +1085,8 @@ ecryptfs_cipher_code_str_map[] = { {"twofish", RFC2440_CIPHER_TWOFISH}, {"cast6", RFC2440_CIPHER_CAST_6}, {"aes", RFC2440_CIPHER_AES_192}, - {"aes", RFC2440_CIPHER_AES_256} + {"aes", RFC2440_CIPHER_AES_256}, + {"aes_xts", RFC2440_CIPHER_AES_XTS_256} };/** @@ -999,6 +1114,11 @@ u8 ecryptfs_code_for_cipher_string(char *cipher_name, size_t key_bytes)case 32 : code =RFC2440_CIPHER_AES_256 ; } + }else if (strcmp (cipher_name,"aes_xts" ) ==0 ) { +switch (key_bytes) { +case 32 : + code =RFC2440_CIPHER_AES_XTS_256 ; + } }else {for (i =0 ; i <ARRAY_SIZE (ecryptfs_cipher_code_str_map); i++)fs/ecryptfs/crypto.c +15
-0if (strcmp (cipher_name, map[i].cipher_str) ==0 ) {@@ -1038,9 +1158,24 @@ int ecryptfs_read_and_validate_header_region(struct inode *inode) u8 file_size[ECRYPTFS_SIZE_AND_MARKER_BYTES];u8 *marker = file_size +ECRYPTFS_FILE_SIZE_BYTES ;int rc; +unsigned int ra_pages_org; +struct file *lower_file =NULL ; + +if (!inode) +return -EIO ; + lower_file =ecryptfs_inode_to_private (inode)->lower_file; +if (!lower_file) +return -EIO ; + +/*disable read a head mechanism for a while */ + ra_pages_org = lower_file->f_ra.ra_pages; + lower_file->f_ra.ra_pages =0 ; rc =ecryptfs_read_lower (file_size,0 ,ECRYPTFS_SIZE_AND_MARKER_BYTES , inode); + lower_file->f_ra.ra_pages = ra_pages_org; +/* restore read a head mechanism */ +if (rc <ECRYPTFS_SIZE_AND_MARKER_BYTES )return rc >=0 ? -EINVAL : rc;fs/ecryptfs/crypto.c +5
-0rc =ecryptfs_validate_marker (marker);@@ -1430,6 +1565,11 @@ int ecryptfs_read_metadata(struct dentry *ecryptfs_dentry)struct ecryptfs_mount_crypt_stat *mount_crypt_stat = &ecryptfs_superblock_to_private ( ecryptfs_dentry->d_sb)->mount_crypt_stat; +unsigned int ra_pages_org; +struct file *lower_file = +ecryptfs_inode_to_private (ecryptfs_inode)->lower_file; +if (!lower_file) +return -EIO ; ecryptfs_copy_mount_wide_flags_to_inode_flags(crypt_stat,fs/ecryptfs/crypto.c +6
-0mount_crypt_stat);@@ -1441,8 +1581,14 @@ int ecryptfs_read_metadata(struct dentry *ecryptfs_dentry) __func__); goto out; } +/*disable read a head mechanism */ + ra_pages_org = lower_file->f_ra.ra_pages; + lower_file->f_ra.ra_pages =0 ; + rc =ecryptfs_read_lower (page_virt,0 , crypt_stat->extent_size, ecryptfs_inode); + lower_file->f_ra.ra_pages = ra_pages_org;/* restore it back */ +if (rc >=0 ) rc = ecryptfs_read_headers_virt(page_virt, crypt_stat,fs/ecryptfs/crypto.c +0
-0ecryptfs_dentry,fs/ecryptfs/debug.c +13
-0@@ -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 +3
-0@@ -245,6 +245,7 @@ struct ecryptfs_crypt_stat {struct mutex cs_tfm_mutex;struct mutex cs_hash_tfm_mutex;struct mutex cs_mutex; +unsigned char cipher_mode[ECRYPTFS_MAX_CIPHER_NAME_SIZE +1 ]; };/* inode private data. */ @@ -267,6 +268,8 @@ struct ecryptfs_dentry_info { }; }; + +/** * ecryptfs_global_auth_tok - A key used to encrypt all new files under the mountpoint
* @flags: Status flagsfs/ecryptfs/ecryptfs_kernel.h +49
-0@@ -345,6 +348,8 @@ struct ecryptfs_mount_crypt_stat {unsigned char global_default_fn_cipher_name[ECRYPTFS_MAX_CIPHER_NAME_SIZE +1 ];char global_default_fnek_sig[ECRYPTFS_SIG_SIZE_HEX +1 ]; +unsigned char global_default_cipher_mode[ECRYPTFS_MAX_CIPHER_NAME_SIZE + +1 ]; };/* superblock private data. */ @@ -527,6 +532,53 @@ ecryptfs_dentry_to_lower_path(struct dentry *dentry)return &((struct ecryptfs_dentry_info *)dentry->d_fsdata)->lower_path; } +/** +* Given a cipher and mode strings, the function +* concatenates them to create a new string of +* <cipher>_<mode> format. +*/ +static inline unsigned char *ecryptfs_get_full_cipher ( +unsigned char *cipher,unsigned char *mode, +unsigned char *final,size_t final_size) +{ +memset (final,0 , final_size); + +if (strlen (mode) >0 ) { +snprintf (final, final_size,"%s_%s" , cipher, mode); +return final; + } + +return cipher; +} + +/** +* Given a <cipher>[_<mode>] formatted string, the function +* extracts cipher string and/or mode string. +* Note: the passed cipher and/or mode strings will be null-terminated. +*/ +static inline void ecryptfs_parse_full_cipher ( +char *s,char *cipher,char *mode) +{ +char input[2 *ECRYPTFS_MAX_CIPHER_NAME_SIZE +1 +1 ]; +/* +1 for '_'; +1 for '\0' */ +char *p; +char *input_p = input; + +if (s ==NULL || cipher ==NULL ) +return ; + +memset (input,0 ,sizeof (input)); +strlcpy (input, s,sizeof (input)); + + p =strsep (&input_p,"_" ); +strlcpy (cipher, p,ECRYPTFS_MAX_CIPHER_NAME_SIZE +1 ); + + +/* check if mode is specified */ +if (input_p !=NULL && mode !=NULL ) +strlcpy (mode, input_p,ECRYPTFS_MAX_CIPHER_NAME_SIZE +1 ); +} + #define ecryptfs_printk(type, fmt, arg...) \ __ecryptfs_printk(type "%s: " fmt, __func__, ## arg);
__printf(1, 2)fs/ecryptfs/ecryptfs_kernel.h +1
-0@@ -575,6 +627,7 @@ int ecryptfs_encrypt_and_encode_filename( const char *name, size_t name_size);struct dentry *ecryptfs_lower_dentry (struct dentry *this_dentry);void ecryptfs_dump_hex (char *data,int bytes); +void ecryptfs_dump_salt_hex (char *data,int key_size,char *cipher);int virt_to_scatterlist (const void *addr,int size,struct scatterlist *sg,int sg_size);int
ecryptfs_compute_root_iv(struct ecryptfs_crypt_stat *crypt_stat);fs/ecryptfs/ecryptfs_kernel.h +25
-0@@ -718,4 +771,29 @@ int ecryptfs_set_f_namelen(long *namelen, long lower_namelen, int ecryptfs_derive_iv(char *iv, struct ecryptfs_crypt_stat *crypt_stat, loff_t offset); +void clean_inode_pages (struct address_space *mapping, +pgoff_t start,pgoff_t end); + +void ecryptfs_drop_pagecache_sb (struct super_block *sb,void *unused); + +void ecryptfs_free_events (void ); + +void ecryptfs_freepage (struct page *page); + +struct ecryptfs_events *get_events (void ); + +size_t ecryptfs_get_salt_size_for_cipher (const char *cipher); + +size_t ecryptfs_get_key_size_to_enc_data ( +struct ecryptfs_crypt_stat *crypt_stat); + +size_t ecryptfs_get_key_size_to_store_key ( +struct ecryptfs_crypt_stat *crypt_stat); + +size_t ecryptfs_get_key_size_to_restore_key (size_t stored_key_size, +const char *cipher); + +bool ecryptfs_check_space_for_salt (const size_t key_size, +const size_t salt_size); +#endif /* #ifndef ECRYPTFS_KERNEL_H */ fs/ecryptfs/events.c new file +125
-0@@ -0,0 +1,361 @@ +/** +* eCryptfs: Linux filesystem encryption layer +* Copyright (c) 2015, The Linux Foundation. All rights reserved. +* +* This program is free software; you can redistribute it and/or modify +* it under the terms of the GNU General Public License version 2 and +* only version 2 as published by the Free Software Foundation. +* +* This program is distributed in the hope that it will be useful, +* but WITHOUT ANY WARRANTY; without even the implied warranty of +* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +* GNU General Public License for more details. +*/ + +#include <linux/string.h> +#include <linux/ecryptfs.h> +#include <linux/mutex.h> +#include <linux/types.h> +#include <linux/slab.h> +#include <linux/pagemap.h> +#include <linux/random.h> +#include "ecryptfs_kernel.h" + +static DEFINE_MUTEX (events_mutex); +static struct ecryptfs_events *events_ptr; +static int handle; + +void ecryptfs_free_events (void ) +{ +mutex_lock (&events_mutex); +if (events_ptr !=NULL ) { +kfree (events_ptr); + events_ptr =NULL ; + } + +mutex_unlock (&events_mutex); +} + +/** +* Register to ecryptfs events, by passing callback +* functions to be called upon events occurrence. +* The function returns a handle to be passed +* to unregister function. +*/ +int ecryptfs_register_to_events (struct ecryptfs_events *ops) +{ +int ret_value =0 ; + +if (!ops) +return -EINVAL ; + +mutex_lock (&events_mutex); + +if (events_ptr !=NULL ) { +ecryptfs_printk (KERN_ERR , +"already registered!\n" ); + ret_value = -EPERM ; + goto out; + } + events_ptr = +kzalloc (sizeof (struct ecryptfs_events ),GFP_KERNEL ); + +if (!events_ptr) { +ecryptfs_printk (KERN_ERR ,"malloc failure\n" ); + ret_value = -ENOMEM ; + goto out; + } +/* copy the callbacks */ + events_ptr->open_cb = ops->open_cb; + events_ptr->release_cb = ops->release_cb; + events_ptr->encrypt_cb = ops->encrypt_cb; + events_ptr->decrypt_cb = ops->decrypt_cb; + events_ptr->is_cipher_supported_cb = + ops->is_cipher_supported_cb; + events_ptr->is_hw_crypt_cb = ops->is_hw_crypt_cb; + events_ptr->get_salt_key_size_cb = ops->get_salt_key_size_cb; + +get_random_bytes (&handle,sizeof (handle)); + ret_value = handle; + +out: +mutex_unlock (&events_mutex); +return ret_value; +} + +/** +* Unregister from ecryptfs events. +*/ +int ecryptfs_unregister_from_events (int user_handle) +{ +int ret_value =0 ; + +mutex_lock (&events_mutex); + +if (!events_ptr) { + ret_value = -EINVAL ; + goto out; + } +if (user_handle != handle) { + ret_value =ECRYPTFS_INVALID_EVENTS_HANDLE ; + goto out; + } + +kfree (events_ptr); + events_ptr =NULL ; + +out: +mutex_unlock (&events_mutex); +return ret_value; +} + +/** +* This function decides whether the passed file offset +* belongs to ecryptfs metadata or not. +* The caller must pass ecryptfs data, which was received in one +* of the callback invocations. +*/ +bool ecryptfs_is_page_in_metadata (void *data,pgoff_t offset) +{ + +struct ecryptfs_crypt_stat *stat =NULL ; +bool ret = true; + +if (!data) { + ecryptfs_printk(KERN_ERR, "ecryptfs_is_page_in_metadata: invalid dataparameter\n");
fs/ecryptfs/events.c new file +148
-0+ ret = false; + goto end; + } + stat = (struct ecryptfs_crypt_stat *)data; + +if (stat->flags &ECRYPTFS_METADATA_IN_XATTR ) { + ret = false; + goto end; + } + +if (offset >= (stat->metadata_size/PAGE_CACHE_SIZE )) { + ret = false; + goto end; + } +end: +return ret; +} + +/** +* Given two ecryptfs data, the function +* decides whether they are equal. +*/ +inline bool ecryptfs_is_data_equal (void *data1,void *data2) +{ +/* pointer comparison*/ +return data1 == data2; +} + +/** +* Given ecryptfs data, the function +* returns appropriate key size. +*/ +size_t ecryptfs_get_key_size (void *data) +{ + +struct ecryptfs_crypt_stat *stat =NULL ; + +if (!data) +return 0 ; + + stat = (struct ecryptfs_crypt_stat *)data; +return stat->key_size; +} + +/** +* Given ecryptfs data, the function +* returns appropriate salt size. +* +* !!! crypt_stat cipher name and mode must be initialized +*/ +size_t ecryptfs_get_salt_size (void *data) +{ +struct ecryptfs_crypt_stat *stat =NULL ; +unsigned char final[2 *ECRYPTFS_MAX_CIPHER_NAME_SIZE +1 ]; + +if (!data) { +ecryptfs_printk (KERN_ERR , +"ecryptfs_get_salt_size: invalid data parameter\n" ); +return 0 ; + } + + stat = (struct ecryptfs_crypt_stat *)data; +return ecryptfs_get_salt_size_for_cipher ( +ecryptfs_get_full_cipher (stat->cipher, + stat->cipher_mode, + final,sizeof (final))); + +} + +/** +* Given ecryptfs data, the function +* returns appropriate cipher. +*/ +const unsigned char *ecryptfs_get_cipher (void *data) +{ +unsigned char final[2 *ECRYPTFS_MAX_CIPHER_NAME_SIZE +1 ]; +struct ecryptfs_crypt_stat *stat =NULL ; + +if (!data) { +ecryptfs_printk (KERN_ERR , +"ecryptfs_get_cipher: invalid data parameter\n" ); +return NULL ; + } + stat = (struct ecryptfs_crypt_stat *)data; +return ecryptfs_get_full_cipher (stat->cipher, stat->cipher_mode, + final,sizeof (final)); +} + +/** +* Given ecryptfs data, the function +* returns file encryption key. +*/ +const unsigned char *ecryptfs_get_key (void *data) +{ + +struct ecryptfs_crypt_stat *stat =NULL ; + +if (!data) { +ecryptfs_printk (KERN_ERR , +"ecryptfs_get_key: invalid data parameter\n" ); +return NULL ; + } + stat = (struct ecryptfs_crypt_stat *)data; +return stat->key; +} + +/** +* Given ecryptfs data, the function +* returns file encryption salt. +*/ +const unsigned char *ecryptfs_get_salt (void *data) +{ +struct ecryptfs_crypt_stat *stat =NULL ; + +if (!data) { +ecryptfs_printk (KERN_ERR , +"ecryptfs_get_salt: invalid data parameter\n" ); +return NULL ; + } + stat = (struct ecryptfs_crypt_stat *)data; +return stat->key +ecryptfs_get_salt_size (data); +} + +/** +* Returns ecryptfs events pointer +*/ +inline struct ecryptfs_events *get_events (void ) +{ +return events_ptr; +} + +/** +* If external crypto module requires salt in addition to key, +* we store it as part of key array (if there is enough space) +* Checks whether a salt key can fit into array allocated for +* regular key +*/ +bool ecryptfs_check_space_for_salt (const size_t key_size, +const size_t salt_size) +{ +if ((salt_size + key_size) >ECRYPTFS_MAX_KEY_BYTES ) +return false; + +return true; +} + +/* +* If there is salt that is used by external crypto module, it is stored
+ * in the same array where regular key is. Salt is going to be used by
+ * external crypto module only, so for all internal crypto operations salt
+ * should be ignored.
+ *
+ * Get key size in cases where it is going to be used for data encryption
+ * or for all other general purposes
+ */fs/ecryptfs/events.c new file +11
-0+size_t ecryptfs_get_key_size_to_enc_data ( +struct ecryptfs_crypt_stat *crypt_stat) +{ +if (!crypt_stat) +return 0 ; + +return crypt_stat->key_size; +} + +/* +* If there is salt that is used by external crypto module, it is stored
+ * in the same array where regular key is. Salt is going to be used by
+ * external crypto module only, but we still need to save and restore it
+ * (in encrypted form) as part of ecryptfs header along with the regular
+ * key.
+ *
+ * Get key size in cases where it is going to be stored persistently
+ *
+ * !!!crypt_statcipher name and mode must be initialized
+ */fs/ecryptfs/events.c new file +21
-0+size_t ecryptfs_get_key_size_to_store_key ( +struct ecryptfs_crypt_stat *crypt_stat) +{ +size_t salt_size =0 ; + +if (!crypt_stat) +return 0 ; + + salt_size =ecryptfs_get_salt_size (crypt_stat); + +if (!ecryptfs_check_space_for_salt (crypt_stat->key_size, salt_size)) { +ecryptfs_printk (KERN_WARNING , +"ecryptfs_get_key_size_to_store_key: not enough space for salt\n" ); +return crypt_stat->key_size; + } + +return crypt_stat->key_size + salt_size; +} + +/* +* If there is salt that is used by external crypto module, it is stored
+ * in the same array where regular key is. Salt is going to be used by
+ * external crypto module only, but we still need to save and restore it
+ * (in encrypted form) as part of ecryptfs header along with the regular
+ * key.
+ *
+ * Get key size in cases where it is going to be restored from storage
+ *
+ * !!!crypt_statcipher name and mode must be initialized
+ */fs/ecryptfs/events.c new file +13
-0+size_t ecryptfs_get_key_size_to_restore_key (size_t stored_key_size, +const char *cipher) +{ +size_t salt_size =0 ; + +if (!cipher) +return 0 ; + + salt_size =ecryptfs_get_salt_size_for_cipher (cipher); + +if (salt_size >= stored_key_size) { + ecryptfs_printk(KERN_WARNING, + "ecryptfs_get_key_size_to_restore_key: salt %zu >= stred size%zu\n",
fs/ecryptfs/events.c new file +18
-0+ salt_size, stored_key_size); + +return stored_key_size; + } + +return stored_key_size - salt_size; +} + +/** +* Given cipher, the function returns appropriate salt size. +*/ +size_t ecryptfs_get_salt_size_for_cipher (const char *cipher) +{ +if (!get_events () || !(get_events ()->get_salt_key_size_cb)) +return 0 ; + +return get_events ()->get_salt_key_size_cb (cipher); +}fs/ecryptfs/file.c +4
-0@@ -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() */ fs/ecryptfs/file.c +19
-0struct 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:fs/ecryptfs/file.c +13
-0ecryptfs_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 +3
-0@@ -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,fs/ecryptfs/inode.c +1
-0mode);@@ -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 */ fs/ecryptfs/inode.c +7
-0rc =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:fs/ecryptfs/inode.c +0
-0return rc;fs/ecryptfs/keystore.c +5
-3@@ -315,7 +315,8 @@ write_tag_66_packet(char *signature, u8 cipher_code,* | File Encryption Key Size | 1 or 2 bytes | * | File Encryption Key | arbitrary | */ - data_len = (5 +ECRYPTFS_SIG_SIZE_HEX + crypt_stat->key_size); + data_len = (5 +ECRYPTFS_SIG_SIZE_HEX + +ecryptfs_get_key_size_to_store_key (crypt_stat)); *packet =kmalloc (data_len,GFP_KERNEL ); message = *packet;if (!message) {@@ -335,8 +336,9 @@ write_tag_66_packet(char *signature, u8 cipher_code,memcpy (&message[i], signature,ECRYPTFS_SIG_SIZE_HEX ); i +=ECRYPTFS_SIG_SIZE_HEX ;/* The encrypted key includes 1 byte cipher code and 2 byte checksum */ - rc =ecryptfs_write_packet_length (&message[i], crypt_stat->key_size +3 , - &packet_size_len); + rc =ecryptfs_write_packet_length (&message[i], +ecryptfs_get_key_size_to_store_key (crypt_stat) +3 , + &packet_size_len);if (rc) {fs/ecryptfs/keystore.c +4
-3ecryptfs_printk (KERN_ERR ,"Error generating tag 66 packet " "header; cannot generate packet length\n" );@@ -344,9 +346,10 @@ write_tag_66_packet(char *signature, u8 cipher_code, } i += packet_size_len; message[i++] = cipher_code; -memcpy (&message[i], crypt_stat->key, crypt_stat->key_size); - i += crypt_stat->key_size; -for (j =0 ; j < crypt_stat->key_size; j++) +memcpy (&message[i], crypt_stat->key, +ecryptfs_get_key_size_to_store_key (crypt_stat)); + i +=ecryptfs_get_key_size_to_store_key (crypt_stat); +for (j =0 ; j <ecryptfs_get_key_size_to_store_key (crypt_stat); j++) checksum += crypt_stat->key[j]; message[i++] = (checksum /256 ) %256 ;fs/ecryptfs/keystore.c +1
-0message[i++] = (checksum %256 );@@ -918,6 +921,7 @@ ecryptfs_parse_tag_70_packet(char **filename, size_t *filename_size, struct ecryptfs_parse_tag_70_packet_silly_stack *s;struct key *auth_tok_key =NULL ;int rc =0 ; +char full_cipher[ECRYPTFS_MAX_CIPHER_NAME_SIZE ]; (*packet_size) =0 ;fs/ecryptfs/keystore.c +2
-1(*filename_size) =0 ;@@ -977,12 +981,13 @@ ecryptfs_parse_tag_70_packet(char **filename,size_t *filename_size, s->fnek_sig_hex[ECRYPTFS_SIG_SIZE_HEX] = '\0'; (*packet_size) +=ECRYPTFS_SIG_SIZE ; s->cipher_code = data[(*packet_size)++]; - rc =ecryptfs_cipher_code_to_string (s->cipher_string, s->cipher_code); + rc =ecryptfs_cipher_code_to_string (full_cipher, s->cipher_code);if (rc) {printk (KERN_WARNING "%s: Cipher code [%d] is invalid\n" , __func__, s->cipher_code); goto out; } +ecryptfs_parse_full_cipher (full_cipher, s->cipher_string,0 ); rc = ecryptfs_find_auth_tok_for_sig(&auth_tok_key, &s->auth_tok, mount_crypt_stat,fs/ecryptfs/keystore.c +1
-0s->fnek_sig_hex);@@ -1151,6 +1156,7 @@ decrypt_pki_encrypted_session_key(struct ecryptfs_auth_tok *auth_tok, char *payload =NULL ;size_t payload_len =0 ;int rc; +char full_cipher[ECRYPTFS_MAX_CIPHER_NAME_SIZE ]; rc =ecryptfs_get_auth_tok_sig (&auth_tok_sig, auth_tok);fs/ecryptfs/keystore.c +16
-6if (rc) {@@ -1184,21 +1190,31 @@ decrypt_pki_encrypted_session_key(structecryptfs_auth_tok *auth_tok, rc); goto out; } - auth_tok->session_key.flags |=ECRYPTFS_CONTAINS_DECRYPTED_KEY ; -memcpy (crypt_stat->key, auth_tok->session_key.decrypted_key, - auth_tok->session_key.decrypted_key_size); - crypt_stat->key_size = auth_tok->session_key.decrypted_key_size; - rc =ecryptfs_cipher_code_to_string (crypt_stat->cipher, cipher_code); + + rc =ecryptfs_cipher_code_to_string (full_cipher, cipher_code);if (rc) {ecryptfs_printk (KERN_ERR ,"Cipher code [%d] is invalid\n" , cipher_code) - goto out; + goto out; } + + auth_tok->session_key.flags |=ECRYPTFS_CONTAINS_DECRYPTED_KEY ; +memcpy (crypt_stat->key, auth_tok->session_key.decrypted_key, + auth_tok->session_key.decrypted_key_size); + crypt_stat->key_size =ecryptfs_get_key_size_to_restore_key ( + auth_tok->session_key.decrypted_key_size, full_cipher); + +ecryptfs_parse_full_cipher (full_cipher, + crypt_stat->cipher, crypt_stat->cipher_mode); + crypt_stat->flags |=ECRYPTFS_KEY_VALID ;if (ecryptfs_verbosity >0 ) {ecryptfs_printk (KERN_DEBUG ,"Decrypted session key:\n" );ecryptfs_dump_hex (crypt_stat->key, crypt_stat->key_size); + +ecryptfs_dump_salt_hex (crypt_stat->key, crypt_stat->key_size, + full_cipher); } out:fs/ecryptfs/keystore.c +1
-0kfree (msg);@@ -1380,6 +1396,7 @@ parse_tag_3_packet(struct ecryptfs_crypt_stat *crypt_stat, struct ecryptfs_auth_tok_list_item *auth_tok_list_item;size_t length_size;int rc =0 ; +char full_cipher[ECRYPTFS_MAX_CIPHER_NAME_SIZE ]; (*packet_size) =0 ;fs/ecryptfs/keystore.c +4
-1(*new_auth_tok) =NULL ;@@ -1453,10 +1470,13 @@ parse_tag_3_packet(struct ecryptfs_crypt_stat *crypt_stat, rc = -EINVAL ; goto out_free; } - rc =ecryptfs_cipher_code_to_string (crypt_stat->cipher, + rc =ecryptfs_cipher_code_to_string (full_cipher, (u16 )data[(*packet_size)]);if (rc) goto out_free; +ecryptfs_parse_full_cipher (full_cipher, + crypt_stat->cipher, crypt_stat->cipher_mode); +/* A little extra work to differentiate among the AES key * sizes; see RFC2440 */ fs/ecryptfs/keystore.c +4
-1switch (data[(*packet_size)++]) {@@ -1465,7 +1485,10 @@ parse_tag_3_packet(struct ecryptfs_crypt_stat *crypt_stat, break;default : crypt_stat->key_size = - (*new_auth_tok)->session_key.encrypted_key_size; +ecryptfs_get_key_size_to_restore_key ( + (*new_auth_tok)->session_key.encrypted_key_size, + full_cipher); + } rc =ecryptfs_init_crypt_ctx (crypt_stat);fs/ecryptfs/keystore.c +2
-0if (rc)@@ -1664,6 +1687,8 @@ static intdecrypt_passphrase_encrypted_session_key (struct ecryptfs_auth_tok *auth_tok, struct ecryptfs_crypt_stat *crypt_stat) { + + unsigned char final[2*ECRYPTFS_MAX_CIPHER_NAME_SIZE+1];struct scatterlist dst_sg[2 ];struct scatterlist src_sg[2 ];fs/ecryptfs/keystore.c +1
-1struct mutex *tfm_mutex;@@ -1713,7 +1738,7 @@ decrypt_passphrase_encrypted_session_key(structecryptfs_auth_tok *auth_tok,mutex_lock (tfm_mutex ); rc =crypto_blkcipher_setkey ( desc.tfm, auth_tok->token.password.session_key_encryption_key, - crypt_stat->key_size); + auth_tok->token.password.session_key_encryption_key_bytes);if (unlikely (rc <0 )) {mutex_unlock (tfm_mutex);fs/ecryptfs/keystore.c +4
-0printk (KERN_ERR "Error setting key for crypto context\n" );@@ -1736,6 +1761,10 @@ decrypt_passphrase_encrypted_session_key(struct ecryptfs_auth_tok *auth_tok, crypt_stat->key_size);ecryptfs_dump_hex (crypt_stat->key, crypt_stat->key_size); +ecryptfs_dump_salt_hex (crypt_stat->key, crypt_stat->key_size, +ecryptfs_get_full_cipher (crypt_stat->cipher, + crypt_stat->cipher_mode, + final,sizeof (final))); } out:fs/ecryptfs/keystore.c +9
-4return rc;@@ -1972,12 +2001,17 @@ pki_encrypt_session_key(struct key *auth_tok_key, size_t payload_len = 0;struct ecryptfs_message *msg;int rc; +unsigned char final[2 *ECRYPTFS_MAX_CIPHER_NAME_SIZE +1 ]; rc =write_tag_66_packet (auth_tok->token.private_key.signature, -ecryptfs_code_for_cipher_string ( - crypt_stat->cipher, - crypt_stat->key_size), - crypt_stat, &payload, &payload_len); +ecryptfs_code_for_cipher_string ( +ecryptfs_get_full_cipher ( + crypt_stat->cipher, + crypt_stat->cipher_mode, + final,sizeof (final)), +ecryptfs_get_key_size_to_enc_data ( + crypt_stat)), + crypt_stat, &payload, &payload_len);up_write (&(auth_tok_key->sem));key_put (auth_tok_key);fs/ecryptfs/keystore.c +1
-1if (rc) {@@ -2035,7 +2069,7 @@ write_tag_1_packet(char *dest, size_t *remaining_bytes,ecryptfs_from_hex (key_rec->sig, auth_tok->token.private_key.signature,ECRYPTFS_SIG_SIZE ); encrypted_session_key_valid =0 ; -for (i =0 ; i < crypt_stat->key_size; i++) +for (i =0 ; i <ecryptfs_get_key_size_to_store_key (crypt_stat); i++) encrypted_session_key_valid |= auth_tok->session_key.encrypted_key[i];fs/ecryptfs/keystore.c +1
-0if (encrypted_session_key_valid) {@@ -2189,6 +2223,7 @@ write_tag_3_packet(char *dest, size_t *remaining_bytes, u8 cipher_code;size_t packet_size_length;size_t max_packet_size; +unsigned char final[2 *ECRYPTFS_MAX_CIPHER_NAME_SIZE +1 ];struct ecryptfs_mount_crypt_stat *mount_crypt_stat = crypt_stat->mount_crypt_stat;fs/ecryptfs/keystore.c +3
-2struct blkcipher_desc desc = {@@ -2221,13 +2256,14 @@ write_tag_3_packet(char *dest, size_t *remaining_bytes, mount_crypt_stat->global_default_cipher_key_size;if (auth_tok->session_key.encrypted_key_size ==0 ) auth_tok->session_key.encrypted_key_size = - crypt_stat->key_size; +ecryptfs_get_key_size_to_store_key (crypt_stat);if (crypt_stat->key_size ==24 &&strcmp ("aes" , crypt_stat->cipher) ==0 ) {memset ((crypt_stat->key +24 ),0 ,8 ); auth_tok->session_key.encrypted_key_size =32 ; }else - auth_tok->session_key.encrypted_key_size = crypt_stat->key_size; + auth_tok->session_key.encrypted_key_size = +ecryptfs_get_key_size_to_store_key (crypt_stat); key_rec->enc_key_size = auth_tok->session_key.encrypted_key_size;fs/ecryptfs/keystore.c +2
-2encrypted_session_key_valid =0 ;@@ -2251,8 +2287,8 @@ write_tag_3_packet(char *dest, size_t *remaining_bytes, auth_tok->token.password. session_key_encryption_key_bytes);memcpy (session_key_encryption_key, - auth_tok->token.password.session_key_encryption_key, - crypt_stat->key_size); + auth_tok->token.password.session_key_encryption_key, + auth_tok->token.password.session_key_encryption_key_bytes);ecryptfs_printk (KERN_DEBUG ,"Cached session key encryption key:\n" );fs/ecryptfs/keystore.c +1
-1if (ecryptfs_verbosity >0 )@@ -2285,7 +2321,7 @@ write_tag_3_packet(char *dest, size_t *remaining_bytes, }mutex_lock (tfm_mutex); rc =crypto_blkcipher_setkey (desc.tfm, session_key_encryption_key, - crypt_stat->key_size); + auth_tok->token.password.session_key_encryption_key_bytes);if (rc <0 ) {mutex_unlock (tfm_mutex);fs/ecryptfs/keystore.c +6
-1ecryptfs_printk(KERN_ERR, "Error setting key for crypto "@@ -2294,7 +2330,12 @@ write_tag_3_packet(char *dest, size_t *remaining_bytes, } rc =0 ;ecryptfs_printk (KERN_DEBUG ,"Encrypting [%zd] bytes of the key\n" , - crypt_stat->key_size); + crypt_stat->key_size); +ecryptfs_printk (KERN_DEBUG ,"Encrypting [%zd] bytes of the salt key\n", +ecryptfs_get_salt_size_for_cipher ( +ecryptfs_get_full_cipher (crypt_stat->cipher, + crypt_stat->cipher_mode, + final,sizeof (final)))); rc = crypto_blkcipher_encrypt(&desc, dst_sg, src_sg,fs/ecryptfs/keystore.c +4
-2(*key_rec).enc_key_size);mutex_unlock (tfm_mutex);@@ -2343,8 +2384,10 @@ encrypted_session_key_set: dest[(*packet_size)++] =0x04 ;/* version 4 */ /* TODO: Break from RFC2440 so that arbitrary ciphers can be * specified with strings */ - cipher_code =ecryptfs_code_for_cipher_string (crypt_stat->cipher, - crypt_stat->key_size); + cipher_code =ecryptfs_code_for_cipher_string ( +ecryptfs_get_full_cipher (crypt_stat->cipher, + crypt_stat->cipher_mode, final,sizeof (final)), + crypt_stat->key_size);if (cipher_code ==0 ) {ecryptfs_printk (KERN_WARNING ,"Unable to generate code for " "cipher [%s]\n" , crypt_stat->cipher);fs/ecryptfs/main.c +7
-0@@ -165,7 +165,13 @@ void ecryptfs_put_lower_file(struct inode *inode)fput (inode_info->lower_file); inode_info->lower_file =NULL ;mutex_unlock (&inode_info->lower_file_mutex); + +if (get_events () &&get_events ()->release_cb) +get_events ()->release_cb ( +ecryptfs_inode_to_lower (inode)); } + + }enum { ecryptfs_opt_sig, ecryptfs_opt_ecryptfs_sig,@@ -266,6 +272,7 @@ static int ecryptfs_parse_options(struct ecryptfs_sb_info *sbi, char *options, int cipher_key_bytes_set = 0;int fn_cipher_key_bytes;int fn_cipher_key_bytes_set =0 ; +size_t salt_size =0 ;struct ecryptfs_mount_crypt_stat *mount_crypt_stat = &sbi->mount_crypt_stat;fs/ecryptfs/main.c +8
-5substring_t args[MAX_OPT_ARGS ];@@ -280,6 +287,7 @@ static int ecryptfs_parse_options(struct ecryptfs_sb_info *sbi, char *options, char *cipher_key_bytes_src;char *fn_cipher_key_bytes_src;u8 cipher_code; +unsigned char final[2 *ECRYPTFS_MAX_CIPHER_NAME_SIZE +1 ]; *check_ruid =0 ;@@ -309,12 +317,14 @@ static int ecryptfs_parse_options(struct ecryptfs_sb_info *sbi, char *options, case ecryptfs_opt_ecryptfs_cipher: cipher_name_src = args[0 ].from; cipher_name_dst = - mount_crypt_stat-> - global_default_cipher_name; -strncpy (cipher_name_dst, cipher_name_src, -ECRYPTFS_MAX_CIPHER_NAME_SIZE ); - cipher_name_dst[ECRYPTFS_MAX_CIPHER_NAME_SIZE ] ='\0' ; + mount_crypt_stat->global_default_cipher_name; + +ecryptfs_parse_full_cipher (cipher_name_src, + mount_crypt_stat->global_default_cipher_name, + mount_crypt_stat->global_default_cipher_mode); + cipher_name_set =1 ; +break ;case ecryptfs_opt_ecryptfs_key_bytes:fs/ecryptfs/main.c +31
-5cipher_key_bytes_src = args[0 ].from;@@ -411,24 +421,50 @@ static int ecryptfs_parse_options(struct ecryptfs_sb_info *sbi, char *options,strcpy (mount_crypt_stat->global_default_cipher_name,ECRYPTFS_DEFAULT_CIPHER ); } +if ((mount_crypt_stat->flags &ECRYPTFS_GLOBAL_ENCRYPT_FILENAMES ) && !fn_cipher_name_set)strcpy (mount_crypt_stat->global_default_fn_cipher_name, mount_crypt_stat->global_default_cipher_name); -if (!cipher_key_bytes_set) + +if (cipher_key_bytes_set) { + + salt_size =ecryptfs_get_salt_size_for_cipher ( +ecryptfs_get_full_cipher ( + mount_crypt_stat->global_default_cipher_name, + mount_crypt_stat->global_default_cipher_mode, + final,sizeof (final))); + +if (!ecryptfs_check_space_for_salt ( + mount_crypt_stat->global_default_cipher_key_size, + salt_size)) { +ecryptfs_printk ( +KERN_WARNING , +"eCryptfs internal error: no space for salt" ); + } + }else mount_crypt_stat->global_default_cipher_key_size =0 ; +if ((mount_crypt_stat->flags &ECRYPTFS_GLOBAL_ENCRYPT_FILENAMES ) && !fn_cipher_key_bytes_set) mount_crypt_stat->global_default_fn_cipher_key_bytes = mount_crypt_stat->global_default_cipher_key_size; cipher_code =ecryptfs_code_for_cipher_string ( - 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)), mount_crypt_stat->global_default_cipher_key_size);if (!cipher_code) { -ecryptfs_printk (KERN_ERR , -"eCryptfs doesn't support cipher: %s" , - mount_crypt_stat->global_default_cipher_name); +ecryptfs_printk ( +KERN_ERR , +"eCryptfs doesn't support cipher: %s and key size %zu" , +ecryptfs_get_full_cipher ( + mount_crypt_stat->global_default_cipher_name, + mount_crypt_stat->global_default_cipher_mode, + final,sizeof (final)), + mount_crypt_stat->global_default_cipher_key_size); rc = -EINVAL ; goto out;fs/ecryptfs/main.c +1
-0}@@ -488,6 +524,7 @@ static struct file_system_type ecryptfs_fs_type;* @dev_name: The path to mount over * @raw_data: The options passed into the kernel */ +static struct dentry *ecryptfs_mount (struct file_system_type *fs_type,int flags,const char *dev_name,void *raw_data){
fs/ecryptfs/main.c +0
-0@@ -557,6 +594,8 @@ static struct dentry *ecryptfs_mount(struct
file_system_type*fs_type, int flagsfs/ecryptfs/main.c +2
-0ecryptfs_set_superblock_lower (s, path.dentry->d_sb); +ecryptfs_drop_pagecache_sb (ecryptfs_superblock_to_lower (s),NULL ); +/** * Set the POSIX ACL flag based on whether they're enabled in the lower
fs/ecryptfs/main.c +1
-0* mount. @@ -894,6 +933,7 @@ static void __exit ecryptfs_exit(void)do_sysfs_unregistration ();unregister_filesystem (&ecryptfs_fs_type);ecryptfs_free_kmem_caches (); +ecryptfs_free_events (); }MODULE_AUTHOR ("Michael A. Halcrow <[email protected]>" );fs/ecryptfs/mmap.c +6
-0@@ -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 +6
-0@@ -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); + }/**
fs/ecryptfs/super.c +3
-0@@ -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);fs/ecryptfs/super.c +4
-1list_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)fs/ecryptfs/super.c +0
-0seq_printf(m, ",ecryptfs_key_bytes=%zd",include/linux/ecryptfs.h +14
-0@@ -1,6 +1,9 @@#ifndef _LINUX_ECRYPTFS_H#define _LINUX_ECRYPTFS_H +struct inode ; +struct page ; +/* Version verification for shared data structures w/ userspace */ #define ECRYPTFS_VERSION_MAJOR 0x00#define ECRYPTFS_VERSION_MINOR 0x04@@ -41,6 +44,7 @@#define RFC2440_CIPHER_AES_256 0x09#define RFC2440_CIPHER_TWOFISH 0x0a#define RFC2440_CIPHER_CAST_6 0x0b +#define RFC2440_CIPHER_AES_XTS_256 0x0c#define RFC2440_CIPHER_RSA 0x01@@ -102,4 +106,47 @@ struct ecryptfs_auth_tok { } token; } __attribute__ ((packed)); +#define ECRYPTFS_INVALID_EVENTS_HANDLE -1 + +/** +* ecryptfs_events struct represents a partial interface +* towards ecryptfs module. If registered to ecryptfs events, +* one can receive push notifications. +* A first callback received from ecryptfs will probably be +* about file opening (open_cb), +* in which ecryptfs passes its ecryptfs_data for future usage. +* This data represents a file and must be passed in every query functions
+ * such asecryptfs_get_key_size(),ecryptfs_get_cipher()etc.
+ */include/linux/ecryptfs.h +30
-0+struct ecryptfs_events { +bool (*is_cipher_supported_cb)(const char *cipher); +void (*open_cb)(struct inode *inode,void *ecrytpfs_data); +void (*release_cb)(struct inode *inode); +int (*encrypt_cb)(struct page *in_page,struct page *out_page, +struct inode *inode,unsigned long extent_offset); +int (*decrypt_cb)(struct page *in_page,struct page *out_page, +struct inode *inode,unsigned long extent_offset); +bool (*is_hw_crypt_cb)(void ); +size_t (*get_salt_key_size_cb)(const char *cipher); +}; + + +int ecryptfs_register_to_events (struct ecryptfs_events *ops); + +int ecryptfs_unregister_from_events (int user_handle); + +const unsigned char *ecryptfs_get_key (void *ecrytpfs_data); + +size_t ecryptfs_get_key_size (void *ecrytpfs_data); + +const unsigned char *ecryptfs_get_salt (void *ecrytpfs_data); + +size_t ecryptfs_get_salt_size (void *ecrytpfs_data); + +const unsigned char *ecryptfs_get_cipher (void *ecrytpfs_data); + +bool ecryptfs_is_page_in_metadata (void *ecrytpfs_data,pgoff_t offset); + +bool ecryptfs_is_data_equal(void *ecrytpfs_data1, void
*ecrytpfs_data2);
+
#endif /*_LINUX_ECRYPTFS_H*/
--
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
This is a hardware inline accelerator, meaning that it operates on much lower layer, block layer and device driver layer. The HW encrypts plain requests sent from block layer directly, thus doing it much more efficiently rather than using crypto API.
In order to use such HW efficiently with eCryptfs, eCryptfs encryption has to be canceled and it will need to call for external module instead that will do the correct marking for the blocks to distinguish between those that need to be encrypted by the HW from those that do not need.
We are considering posting the code that will call ecryptfs_register_to_events() as a separate patch, but haven't done so yet. It is HW specific.
Currently we are only proposing framework change so that it can allow for external modules to be connected
Quoted 1,860 lines · On 2015-11-09 20:56:02, [email protected] wrote:
Quoted 5 lines
Hello, Tyler
I'll try to provide more detailed explanation, should it be satisfactory enough I will update the patch description.
Quoted 10 lines
The problem with current eCryptfs is that it has total control on how and
when the encryption is performed and this control can't be altered. One example when this can be a problem is when we want to utilize an underlying inline HW encryption engine which allows encrypting blocks 'on
the fly' as they are being written to the storage. In such a case relevant
blocks just need to be marked as 'should be encrypted'. No actual encryption should be done by eCryptfs as it will be much slower.Is this a hardware crypto accelerator? If so, why not create a crypto api driver so all subsystems can take advantage of the acceleration instead of baking support into individual subsystems?
Quoted 9 lines
The provided framework allows transferring this control (if needed) to some external module which will do the encryption itfelf or just mark the
appropriate blocks.There is no caller for
ecryptfs_register_to_events()since this change only provides framework, it doesn't provide the module itself, the module
could be HW dependent.Will the code that you plan to call
ecryptfs_register_to_events()be upstream? If so, have you posted it?Tyler
Quoted 1,823 lines
Regarding the mounting option, it merely serves as example of new cipher mode that can be served by registered module.
There is a special callback function that should be implemented by the registered module that tells whether a particular cipher is supported by it :is_cipher_supported_cb()The mounting option itself is not necessary, I can remove it
Quoted 10 lines
Hello Andrey!
Quoted 7 lines · Andrey Markovytch
From: Andrey Markovytch <[email protected]>
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
Quoted 2 lines
Quoted 2 lines
the inline HW engine.
Additional cipher option was introduced to support the HW/externalmode
Quoted 4 lines
Quoted 2 lines
under that name of "aes-xts". If no external module has registered to support this cipher, eCryptfs will fall back to the usual "aes"
What is "HW/external mode"? There's no description in the commit
message
Quoted 1 line
and
ecryptfs_register_to_events()does not have a caller so I'm notsure
Quoted 3 lines
of the purpose. Please provide more context.
Despite not yet understanding the purpose of this patch, I think that
I
Quoted 1 line
can safely say that "aes-xts" is not an appropriate mount option to
use
Quoted 42 lines
when enabling this mode. eCryptfs may support XTS mode one day, using the Crypto API, so "HW/external mode" should not own the mount option.
Tyler
Quoted 37 lines
Signed-off-by Lina Zarivach <[email protected]>Signed-off-by Andrey Markovytch <[email protected]>--- 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.cfs/ecryptfs/Makefile +2
-2@@ -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.oQuoted 11 lines
fs/ecryptfs/caches_utils.c line 4
Quoted 11 lines
ecryptfs-$(CONFIG_ECRYPT_FS_MESSAGING) += messaging.o miscdev.o
fs/ecryptfs/caches_utils.c new file +4
-0@@ -0,0 +1,78 @@ +/* +* Copyright (c) 2015, The Linux Foundation. All rights reserved. +* +* This program is free software; you can redistribute it and/or modify
Quoted 1 line
fs/ecryptfs/caches_utils.c line 5
Quoted 1 line
fs/ecryptfs/caches_utils.c new file +1
-0+* it under the terms of the GNU General Public License version 2 and
Quoted 9 lines
Quoted 9 lines
fs/ecryptfs/caches_utils.c new file +9
-0+* only version 2 as published by the Free Software Foundation. +* +* This program is distributed in the hope that it will be useful, +* but WITHOUT ANY WARRANTY; without even the implied warranty of +* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +* GNU General Public License for more details. +*/ + +#include <linux/kernel.h> Quoted 8 lines
fs/ecryptfs/caches_utils.c
Quoted 8 lines
fs/ecryptfs/caches_utils.c new file +8
-0+#include <linux/fs.h> +#include <linux/spinlock.h> +#include <linux/pagemap.h> +#include <linux/pagevec.h> + +#include "../internal.h" + +void ecryptfs_drop_pagecache_sb(struct super_block *sb, void*unused)
Quoted 279 lines
Quoted 279 lines
fs/ecryptfs/caches_utils.c new file +56
-0+{ +struct inode *inode, *toput_inode =NULL ; + +spin_lock (&sb->s_inode_list_lock); +list_for_each_entry (inode, &sb->s_inodes, i_sb_list) { +spin_lock (&inode->i_lock); +if ((inode->i_state & (I_FREEING |I_WILL_FREE |I_NEW )) || + (inode->i_mapping->nrpages ==0 )) { +spin_unlock (&inode->i_lock); +continue ; + } +__iget (inode); +spin_unlock (&inode->i_lock); +spin_unlock (&sb->s_inode_list_lock); + +invalidate_mapping_pages (inode->i_mapping,0 ,-1 ); +iput (toput_inode); + toput_inode = inode; + +spin_lock (&sb->s_inode_list_lock); + } +spin_unlock (&sb->s_inode_list_lock); +iput (toput_inode); +} + +void clean_inode_pages (struct address_space *mapping, +pgoff_t start,pgoff_t end) +{ +struct pagevec pvec; +pgoff_t index = start; +int i; + +pagevec_init (&pvec,0 ); +while (index <= end &&pagevec_lookup (&pvec, mapping, index, +min (end - index, + (pgoff_t )PAGEVEC_SIZE -1 ) +1 )) { +for (i =0 ; i <pagevec_count (&pvec); i++) { +struct page *page = pvec.pages[i]; + +/* We rely upon deletion +* not changing page->index +*/ + index = page->index; +if (index > end) +break ; +if (!trylock_page (page)) +continue ; +WARN_ON (page->index != index); +zero_user (page,0 ,PAGE_CACHE_SIZE ); +unlock_page (page); + } +pagevec_release (&pvec); +cond_resched (); + index++; + } +}fs/ecryptfs/crypto.c +3
-2@@ -35,6 +35,7 @@#include <linux/scatterlist.h> #include <linux/slab.h> #include <asm/unaligned.h> +#include <linux/ecryptfs.h> #include "ecryptfs_kernel.h" #define DECRYPT 0@@ -350,9 +351,9 @@ static int crypt_scatterlist(struct ecryptfs_crypt_stat *crypt_stat, || !(crypt_stat->flags & ECRYPTFS_STRUCT_INITIALIZED));if (unlikely (ecryptfs_verbosity >0 )) {ecryptfs_printk (KERN_DEBUG ,"Key size [%zd]; key:\n" , - crypt_stat->key_size); +ecryptfs_get_key_size_to_enc_data (crypt_stat));ecryptfs_dump_hex (crypt_stat->key, - crypt_stat->key_size); +ecryptfs_get_key_size_to_enc_data (crypt_stat)); }fs/ecryptfs/crypto.c +1
-1init_completion (&ecr.completion);@@ -371,7 +372,7 @@ static int crypt_scatterlist(struct ecryptfs_crypt_stat *crypt_stat,/* Consider doing this once, when the file is opened */ if (!(crypt_stat->flags & ECRYPTFS_KEY_SET)) { rc =crypto_ablkcipher_setkey (crypt_stat->tfm, crypt_stat->key, - crypt_stat->key_size); +ecryptfs_get_key_size_to_enc_data (crypt_stat));if (rc) { ecryptfs_printk(KERN_ERR,fs/ecryptfs/crypto.c +25
-0"Error setting key; rc = [%d]\n",@@ -466,6 +467,31 @@ out:return rc; } +static void init_ecryption_parameters (bool *hw_crypt,bool *cipher_supported, +struct ecryptfs_crypt_stat *crypt_stat) +{ +unsigned char final[2 *ECRYPTFS_MAX_CIPHER_NAME_SIZE +1 ]; + +if (!hw_crypt || !cipher_supported) +return ; + + *cipher_supported = false; + *hw_crypt = false; + +if (get_events () &&get_events ()->is_cipher_supported_cb) { + *cipher_supported = +get_events ()->is_cipher_supported_cb ( +ecryptfs_get_full_cipher (crypt_stat->cipher, + crypt_stat->cipher_mode, final,sizeof (final))); +if (*cipher_supported) { +/* we should apply external algorythm +* assume that is_hw_crypt() cbck is supplied +*/ + *hw_crypt =get_events ()->is_hw_crypt_cb (); + } + } +} +/** * ecryptfs_encrypt_page * @page: Page mapped from the eCryptfs inode for the file; contains
fs/ecryptfs/crypto.c +47
-13@@ -491,11 +517,18 @@ int ecryptfs_encrypt_page(struct page *page)loff_t extent_offset;loff_t lower_offset;int rc =0 ; +bool is_hw_crypt; +bool is_cipher_supported; + ecryptfs_inode = page->mapping->host; crypt_stat = &(ecryptfs_inode_to_private (ecryptfs_inode)->crypt_stat);BUG_ON (!(crypt_stat->flags &ECRYPTFS_ENCRYPTED )); + +init_ecryption_parameters (&is_hw_crypt, + &is_cipher_supported, crypt_stat); + enc_extent_page =alloc_page (GFP_USER );if (!enc_extent_page) { rc = -ENOMEM ;@@ -503,24 +536,51 @@ int ecryptfs_encrypt_page(struct page *page) "encrypted extent\n"); goto out; } - -for (extent_offset =0 ; - extent_offset < (PAGE_CACHE_SIZE / crypt_stat->extent_size); - extent_offset++) { - rc =crypt_extent (crypt_stat, enc_extent_page, page, - extent_offset,ENCRYPT ); -if (rc) { -printk (KERN_ERR "%s: Error encrypting extent; " -"rc = [%d]\n" , __func__, rc); - goto out; - } +if (is_hw_crypt) { +/* no need for encryption */ + }else { +for (extent_offset =0 ; + extent_offset < + (PAGE_CACHE_SIZE / crypt_stat->extent_size); + extent_offset++) { + +if (is_cipher_supported) { +if (!get_events ()->encrypt_cb) { + rc = -EPERM ; + goto out; + } + rc =get_events ()->encrypt_cb (page, + enc_extent_page, +ecryptfs_inode_to_lower ( + ecryptfs_inode), + extent_offset); + }else { + rc =crypt_extent (crypt_stat, + enc_extent_page, page, + extent_offset,ENCRYPT ); + } +if (rc) { +ecryptfs_printk (KERN_ERR , +"%s: Error encrypting; rc = [%d]\n" , + __func__, rc); + goto out; + } + } } lower_offset =lower_offset_for_page (crypt_stat, page); - enc_extent_virt =kmap (enc_extent_page); +if (is_hw_crypt) + enc_extent_virt =kmap (page); +else + enc_extent_virt =kmap (enc_extent_page); + rc =ecryptfs_write_lower (ecryptfs_inode, enc_extent_virt, lower_offset,PAGE_CACHE_SIZE ); -kunmap (enc_extent_page); +if (!is_hw_crypt) +kunmap (enc_extent_page); +else +kunmap (page); +if (rc <0 ) { ecryptfs_printk(KERN_ERR,fs/ecryptfs/crypto.c +25
-3"Error attempting to write lower page; rc = [%d]\n",@@ -559,6 +619,8 @@ int ecryptfs_decrypt_page(struct page *page)unsigned long extent_offset;loff_t lower_offset;int rc =0 ; +bool is_cipher_supported; +bool is_hw_crypt; ecryptfs_inode = page->mapping->host; crypt_stat =@@ -577,13 +639,33 @@ int ecryptfs_decrypt_page(struct page *page) goto out; } +init_ecryption_parameters (&is_hw_crypt, + &is_cipher_supported, crypt_stat); + +if (is_hw_crypt) { + rc =0 ; +return rc; + } +for (extent_offset =0 ; extent_offset < (PAGE_CACHE_SIZE / crypt_stat->extent_size); extent_offset++) { - rc =crypt_extent (crypt_stat, page, page, +if (is_cipher_supported) { +if (!get_events ()->decrypt_cb) { + rc = -EPERM ; + goto out; + } + + rc =get_events ()->decrypt_cb (page, page, +ecryptfs_inode_to_lower (ecryptfs_inode), + extent_offset); + + }else + rc =crypt_extent (crypt_stat, page, page, extent_offset,DECRYPT ); +if (rc) { -printk (KERN_ERR "%s: Error encrypting extent; " +ecryptfs_printk (KERN_ERR ,"%s: Error decrypting extent;" "rc = [%d]\n" , __func__, rc); goto out; }@@ -612,7 +694,7 @@ int ecryptfs_init_crypt_ctx(struct ecryptfs_crypt_stat *crypt_stat)"Initializing cipher [%s]; strlen = [%d]; " "key_size_bits = [%zd]\n" , crypt_stat->cipher, (int )strlen (crypt_stat->cipher), - crypt_stat->key_size << 3); + ecryptfs_get_key_size_to_enc_data(crypt_stat) << 3);mutex_lock (&crypt_stat->cs_tfm_mutex);if (crypt_stat->tfm) {fs/ecryptfs/crypto.c +1
-1rc =0 ;@@ -694,7 +776,7 @@ int ecryptfs_compute_root_iv(struct ecryptfs_crypt_stat *crypt_stat) goto out; } rc =ecryptfs_calculate_md5 (dst, crypt_stat, crypt_stat->key, - crypt_stat->key_size); +ecryptfs_get_key_size_to_enc_data (crypt_stat));if (rc) { ecryptfs_printk(KERN_WARNING, "Error attempting to compute "fs/ecryptfs/crypto.c +0
-0"MD5 while generating root IV\n");@@ -721,6 +803,35 @@ static void ecryptfs_generate_new_key(struct ecryptfs_crypt_stat *crypt_stat) } }Quoted 15 lines
Quoted 5 lines
fs/ecryptfs/crypto.c +4
-0+static int ecryptfs_generate_new_salt (struct ecryptfs_crypt_stat *crypt_stat) +{ +size_t salt_size =0 ; +unsigned char final[2 *ECRYPTFS_MAX_CIPHER_NAME_SIZE +1 ];fs/ecryptfs/crypto.c static void ecryptfs_generate_new_key(struct ecryptfs_crypt_stat *crypt_stat)
Quoted 10 lines
fs/ecryptfs/crypto.c +10
-0+ + salt_size =ecryptfs_get_salt_size_for_cipher ( +ecryptfs_get_full_cipher (crypt_stat->cipher, + crypt_stat->cipher_mode, + final,sizeof (final))); + +if (salt_size ==0 ) +return 0 ; + + if (!ecryptfs_check_space_for_salt(crypt_stat->key_size,
salt_size)) {Quoted 6 lines
fs/ecryptfs/crypto.c static void ecryptfs_generate_new_key(struct ecryptfs_crypt_stat *crypt_stat)
Quoted 6 lines
fs/ecryptfs/crypto.c +6
-0+ecryptfs_printk (KERN_WARNING ,"not enough space for salt\n" ); + crypt_stat->flags |=ECRYPTFS_SECURITY_WARNING ; +return -EINVAL ; + } + + get_random_bytes(crypt_stat->key + crypt_stat->key_size,
salt_size);Quoted 88 lines
Quoted 88 lines
fs/ecryptfs/crypto.c +9
-0+if (unlikely (ecryptfs_verbosity >0 )) { +ecryptfs_printk (KERN_DEBUG ,"Generated new session salt:\n" ); +ecryptfs_dump_hex (crypt_stat->key + crypt_stat->key_size, + salt_size); + } + +return 0 ; +} +/** *
ecryptfs_copy_mount_wide_flags_to_inode_flags
* @crypt_stat: The inode's cryptographic contextfs/ecryptfs/crypto.c +0
-1@@ -823,7 +934,6 @@ int ecryptfs_new_file_context(struct inode *ecryptfs_inode)struct ecryptfs_mount_crypt_stat *mount_crypt_stat = &ecryptfs_superblock_to_private ( ecryptfs_inode->i_sb)->mount_crypt_stat; -int cipher_name_len;int rc =0 ;fs/ecryptfs/crypto.c +9
-5ecryptfs_set_default_crypt_stat_vals (crypt_stat, mount_crypt_stat);@@ -837,15 +947,19 @@ int ecryptfs_new_file_context(struct inode *ecryptfs_inode) "to the inode key sigs; rc = [%d]\n", rc); goto out; } - cipher_name_len = -strlen (mount_crypt_stat->global_default_cipher_name); -memcpy (crypt_stat->cipher, +strlcpy (crypt_stat->cipher, mount_crypt_stat->global_default_cipher_name, - cipher_name_len); - crypt_stat->cipher[cipher_name_len] ='\0' ; +sizeof (crypt_stat->cipher)); + +strlcpy (crypt_stat->cipher_mode, + mount_crypt_stat->global_default_cipher_mode, +sizeof (crypt_stat->cipher_mode)); + crypt_stat->key_size = mount_crypt_stat->global_default_cipher_key_size;ecryptfs_generate_new_key (crypt_stat); +ecryptfs_generate_new_salt (crypt_stat); + rc =ecryptfs_init_crypt_ctx (crypt_stat);if (rc)fs/ecryptfs/crypto.c +7
-1ecryptfs_printk(KERN_ERR, "Error initializing cryptographic "@@ -971,7 +1085,8 @@ ecryptfs_cipher_code_str_map[] = { {"twofish", RFC2440_CIPHER_TWOFISH}, {"cast6", RFC2440_CIPHER_CAST_6}, {"aes", RFC2440_CIPHER_AES_192}, - {"aes", RFC2440_CIPHER_AES_256} + {"aes", RFC2440_CIPHER_AES_256}, + {"aes_xts", RFC2440_CIPHER_AES_XTS_256} };/** @@ -999,6 +1114,11 @@ u8 ecryptfs_code_for_cipher_string(char *cipher_name, size_t key_bytes)case 32 : code =RFC2440_CIPHER_AES_256 ; } + }else if (strcmp (cipher_name,"aes_xts" ) ==0 ) { +switch (key_bytes) { +case 32 : + code =RFC2440_CIPHER_AES_XTS_256 ; + } }else {for (i =0 ; i <ARRAY_SIZE (ecryptfs_cipher_code_str_map); i++)fs/ecryptfs/crypto.c +12
-0if (strcmp (cipher_name, map[i].cipher_str) ==0 ) {@@ -1038,9 +1158,24 @@ int ecryptfs_read_and_validate_header_region(struct inode *inode) u8 file_size[ECRYPTFS_SIZE_AND_MARKER_BYTES];u8 *marker = file_size +ECRYPTFS_FILE_SIZE_BYTES ;int rc; +unsigned int ra_pages_org; +struct file *lower_file =NULL ; + +if (!inode) +return -EIO ; + lower_file =ecryptfs_inode_to_private (inode)->lower_file; +if (!lower_file) +return -EIO ; + +/*disable read a head mechanism for a while */ + ra_pages_org = lower_file->f_ra.ra_pages; + lower_file->f_ra.ra_pages =0 ;Quoted 1 line
fs/ecryptfs/crypto.c int · line 1175
Quoted 1 line
fs/ecryptfs/crypto.c +0
-0rc = ecryptfs_read_lower(file_size, 0,
ECRYPTFS_SIZE_AND_MARKER_BYTES,Quoted 74 lines
Quoted 74 lines
fs/ecryptfs/crypto.c +3
-0inode); + lower_file->f_ra.ra_pages = ra_pages_org; +/* restore read a head mechanism */ +if (rc <ECRYPTFS_SIZE_AND_MARKER_BYTES )return rc >=0 ? -EINVAL : rc;fs/ecryptfs/crypto.c +5
-0rc =ecryptfs_validate_marker (marker);@@ -1430,6 +1565,11 @@ int ecryptfs_read_metadata(struct dentry *ecryptfs_dentry)struct ecryptfs_mount_crypt_stat *mount_crypt_stat = &ecryptfs_superblock_to_private ( ecryptfs_dentry->d_sb)->mount_crypt_stat; +unsigned int ra_pages_org; +struct file *lower_file = +ecryptfs_inode_to_private (ecryptfs_inode)->lower_file; +if (!lower_file) +return -EIO ; ecryptfs_copy_mount_wide_flags_to_inode_flags(crypt_stat,fs/ecryptfs/crypto.c +6
-0mount_crypt_stat);@@ -1441,8 +1581,14 @@ int ecryptfs_read_metadata(struct dentry *ecryptfs_dentry) __func__); goto out; } +/*disable read a head mechanism */ + ra_pages_org = lower_file->f_ra.ra_pages; + lower_file->f_ra.ra_pages =0 ; + rc =ecryptfs_read_lower (page_virt,0 , crypt_stat->extent_size, ecryptfs_inode); + lower_file->f_ra.ra_pages = ra_pages_org;/* restore it back */ +if (rc >=0 ) rc = ecryptfs_read_headers_virt(page_virt, crypt_stat,fs/ecryptfs/crypto.c +0
-0ecryptfs_dentry,fs/ecryptfs/debug.c +13
-0@@ -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 +1
-0@@ -245,6 +245,7 @@ struct ecryptfs_crypt_stat {struct mutex cs_tfm_mutex;struct mutex cs_hash_tfm_mutex;struct mutex cs_mutex; +unsigned char cipher_mode[ECRYPTFS_MAX_CIPHER_NAME_SIZE +1 ]; };/* inode private data. */ @@ -267,6 +268,8 @@ struct ecryptfs_dentry_info { }; };Quoted 4 lines
fs/ecryptfs/ecryptfs_kernel.h struct ecryptfs_dentry_info { · line 274
Quoted 4 lines
fs/ecryptfs/ecryptfs_kernel.h +2
-0+ +/** * ecryptfs_global_auth_tok - A key used to encrypt all new files under
Quoted 7 lines
fs/ecryptfs/ecryptfs_kernel.h struct ecryptfs_mount_crypt_stat { · line 351
Quoted 7 lines
the mountpoint
* @flags: Status flagsfs/ecryptfs/ecryptfs_kernel.h +1
-0@@ -345,6 +348,8 @@ struct ecryptfs_mount_crypt_stat {unsigned char global_default_fn_cipher_name[ECRYPTFS_MAX_CIPHER_NAME_SIZE +1 ];char global_default_fnek_sig[ECRYPTFS_SIG_SIZE_HEX +1 ]; +unsigned char global_default_cipher_mode[ECRYPTFS_MAX_CIPHER_NAME_SIZE
Quoted 7 lines
fs/ecryptfs/ecryptfs_kernel.h ecryptfs_dentry_to_lower_path(struct dentry · line 533
Quoted 7 lines
fs/ecryptfs/ecryptfs_kernel.h +1
-0+ + 1]; };/* superblock private data. */ @@ -527,6 +532,53 @@ ecryptfs_dentry_to_lower_path(struct dentry *dentry) return &((struct ecryptfs_dentry_info*)dentry->d_fsdata)->lower_path;
Quoted 17 lines
Quoted 17 lines
fs/ecryptfs/ecryptfs_kernel.h +15
-0} +/** +* Given a cipher and mode strings, the function +* concatenates them to create a new string of +* <cipher>_<mode> format. +*/ +static inline unsigned char *ecryptfs_get_full_cipher ( +unsigned char *cipher,unsigned char *mode, +unsigned char *final,size_t final_size) +{ +memset (final,0 , final_size); + +if (strlen (mode) >0 ) { +snprintf (final, final_size,"%s_%s" , cipher, mode); +return final; + }Quoted 8 lines
fs/ecryptfs/ecryptfs_kernel.h ecryptfs_dentry_to_lower_path(struct dentry *dentry)
Quoted 8 lines
fs/ecryptfs/ecryptfs_kernel.h +8
-0+ +return cipher; +} + +/** +* Given a <cipher>[_<mode>] formatted string, the function +* extracts cipher string and/or mode string. +* Note: the passed cipher and/or mode strings will be null-terminated.
Quoted 25 lines
Quoted 25 lines
fs/ecryptfs/ecryptfs_kernel.h +24
-0+*/ +static inline void ecryptfs_parse_full_cipher ( +char *s,char *cipher,char *mode) +{ +char input[2 *ECRYPTFS_MAX_CIPHER_NAME_SIZE +1 +1 ]; +/* +1 for '_'; +1 for '\0' */ +char *p; +char *input_p = input; + +if (s ==NULL || cipher ==NULL ) +return ; + +memset (input,0 ,sizeof (input)); +strlcpy (input, s,sizeof (input)); + + p =strsep (&input_p,"_" ); +strlcpy (cipher, p,ECRYPTFS_MAX_CIPHER_NAME_SIZE +1 ); + + +/* check if mode is specified */ +if (input_p !=NULL && mode !=NULL ) +strlcpy (mode, input_p,ECRYPTFS_MAX_CIPHER_NAME_SIZE +1 ); +} + #define ecryptfs_printk(type, fmt, arg...) \Quoted 8 lines
fs/ecryptfs/ecryptfs_kernel.h int ecryptfs_encrypt_and_encode_filename( · line 631
Quoted 8 lines
fs/ecryptfs/ecryptfs_kernel.h +0
-0__ecryptfs_printk(type "%s: " fmt, __func__, ## arg);
__printf(1, 2)fs/ecryptfs/ecryptfs_kernel.h +1
-0@@ -575,6 +627,7 @@ int ecryptfs_encrypt_and_encode_filename( const char *name, size_t name_size);struct dentry *ecryptfs_lower_dentry (struct dentry *this_dentry);void ecryptfs_dump_hex (char *data,int bytes); +void ecryptfs_dump_salt_hex (char *data,int key_size,char *cipher); int virt_to_scatterlist(const void *addr, int size, structscatterlist
Quoted 3 lines
Quoted 3 lines
fs/ecryptfs/ecryptfs_kernel.h +0
-0*sg, int sg_size);int ecryptfs_compute_root_iv(struct
ecryptfs_crypt_stat
*crypt_stat);Quoted 10 lines
fs/ecryptfs/ecryptfs_kernel.h int ecryptfs_set_f_namelen(long *namelen, long
Quoted 10 lines
fs/ecryptfs/ecryptfs_kernel.h +4
-0@@ -718,4 +771,29 @@ int ecryptfs_set_f_namelen(long *namelen, long lower_namelen,int ecryptfs_derive_iv (char *iv,struct ecryptfs_crypt_stat *crypt_stat,loff_t offset); +void clean_inode_pages (struct address_space *mapping, +pgoff_t start,pgoff_t end); + +void ecryptfs_drop_pagecache_sb(struct super_block *sb, void*unused);
Quoted 25 lines
Quoted 25 lines
fs/ecryptfs/ecryptfs_kernel.h +21
-0+ +void ecryptfs_free_events (void ); + +void ecryptfs_freepage (struct page *page); + +struct ecryptfs_events *get_events (void ); + +size_t ecryptfs_get_salt_size_for_cipher (const char *cipher); + +size_t ecryptfs_get_key_size_to_enc_data ( +struct ecryptfs_crypt_stat *crypt_stat); + +size_t ecryptfs_get_key_size_to_store_key ( +struct ecryptfs_crypt_stat *crypt_stat); + +size_t ecryptfs_get_key_size_to_restore_key (size_t stored_key_size, +const char *cipher); + +bool ecryptfs_check_space_for_salt (const size_t key_size, +const size_t salt_size); +#endif /* #ifndef ECRYPTFS_KERNEL_H */ fs/ecryptfs/events.c new file +0
-0Quoted 8 lines
fs/ecryptfs/events.c line 5
Quoted 8 lines
fs/ecryptfs/events.c new file +5
-0@@ -0,0 +1,361 @@ +/** +* eCryptfs: Linux filesystem encryption layer +* Copyright (c) 2015, The Linux Foundation. All rights reserved. +* +* This program is free software; you can redistribute it and/or modify
Quoted 1 line
fs/ecryptfs/events.c line 6
Quoted 1 line
fs/ecryptfs/events.c new file +1
-0+* it under the terms of the GNU General Public License version 2 and
Quoted 111 lines
Quoted 111 lines
fs/ecryptfs/events.c new file +111
-0+* only version 2 as published by the Free Software Foundation. +* +* This program is distributed in the hope that it will be useful, +* but WITHOUT ANY WARRANTY; without even the implied warranty of +* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +* GNU General Public License for more details. +*/ + +#include <linux/string.h> +#include <linux/ecryptfs.h> +#include <linux/mutex.h> +#include <linux/types.h> +#include <linux/slab.h> +#include <linux/pagemap.h> +#include <linux/random.h> +#include "ecryptfs_kernel.h" + +static DEFINE_MUTEX (events_mutex); +static struct ecryptfs_events *events_ptr; +static int handle; + +void ecryptfs_free_events (void ) +{ +mutex_lock (&events_mutex); +if (events_ptr !=NULL ) { +kfree (events_ptr); + events_ptr =NULL ; + } + +mutex_unlock (&events_mutex); +} + +/** +* Register to ecryptfs events, by passing callback +* functions to be called upon events occurrence. +* The function returns a handle to be passed +* to unregister function. +*/ +int ecryptfs_register_to_events (struct ecryptfs_events *ops) +{ +int ret_value =0 ; + +if (!ops) +return -EINVAL ; + +mutex_lock (&events_mutex); + +if (events_ptr !=NULL ) { +ecryptfs_printk (KERN_ERR , +"already registered!\n" ); + ret_value = -EPERM ; + goto out; + } + events_ptr = +kzalloc (sizeof (struct ecryptfs_events ),GFP_KERNEL ); + +if (!events_ptr) { +ecryptfs_printk (KERN_ERR ,"malloc failure\n" ); + ret_value = -ENOMEM ; + goto out; + } +/* copy the callbacks */ + events_ptr->open_cb = ops->open_cb; + events_ptr->release_cb = ops->release_cb; + events_ptr->encrypt_cb = ops->encrypt_cb; + events_ptr->decrypt_cb = ops->decrypt_cb; + events_ptr->is_cipher_supported_cb = + ops->is_cipher_supported_cb; + events_ptr->is_hw_crypt_cb = ops->is_hw_crypt_cb; + events_ptr->get_salt_key_size_cb = ops->get_salt_key_size_cb; + +get_random_bytes (&handle,sizeof (handle)); + ret_value = handle; + +out: +mutex_unlock (&events_mutex); +return ret_value; +} + +/** +* Unregister from ecryptfs events. +*/ +int ecryptfs_unregister_from_events (int user_handle) +{ +int ret_value =0 ; + +mutex_lock (&events_mutex); + +if (!events_ptr) { + ret_value = -EINVAL ; + goto out; + } +if (user_handle != handle) { + ret_value =ECRYPTFS_INVALID_EVENTS_HANDLE ; + goto out; + } + +kfree (events_ptr); + events_ptr =NULL ; + +out: +mutex_unlock (&events_mutex); +return ret_value; +} + +/** +* This function decides whether the passed file offset +* belongs to ecryptfs metadata or not. +* The caller must pass ecryptfs data, which was received in one +* of the callback invocations. +*/ Quoted 8 lines
fs/ecryptfs/events.c
Quoted 8 lines
fs/ecryptfs/events.c new file +8
-0+bool ecryptfs_is_page_in_metadata (void *data,pgoff_t offset) +{ + +struct ecryptfs_crypt_stat *stat =NULL ; +bool ret = true; + +if (!data) { + ecryptfs_printk(KERN_ERR, "ecryptfs_is_page_in_metadata: invaliddata
Quoted 143 lines
Quoted 143 lines
parameter\n");
fs/ecryptfs/events.c new file +142
-0+ ret = false; + goto end; + } + stat = (struct ecryptfs_crypt_stat *)data; + +if (stat->flags &ECRYPTFS_METADATA_IN_XATTR ) { + ret = false; + goto end; + } + +if (offset >= (stat->metadata_size/PAGE_CACHE_SIZE )) { + ret = false; + goto end; + } +end: +return ret; +} + +/** +* Given two ecryptfs data, the function +* decides whether they are equal. +*/ +inline bool ecryptfs_is_data_equal (void *data1,void *data2) +{ +/* pointer comparison*/ +return data1 == data2; +} + +/** +* Given ecryptfs data, the function +* returns appropriate key size. +*/ +size_t ecryptfs_get_key_size (void *data) +{ + +struct ecryptfs_crypt_stat *stat =NULL ; + +if (!data) +return 0 ; + + stat = (struct ecryptfs_crypt_stat *)data; +return stat->key_size; +} + +/** +* Given ecryptfs data, the function +* returns appropriate salt size. +* +* !!! crypt_stat cipher name and mode must be initialized +*/ +size_t ecryptfs_get_salt_size (void *data) +{ +struct ecryptfs_crypt_stat *stat =NULL ; +unsigned char final[2 *ECRYPTFS_MAX_CIPHER_NAME_SIZE +1 ]; + +if (!data) { +ecryptfs_printk (KERN_ERR , +"ecryptfs_get_salt_size: invalid data parameter\n" ); +return 0 ; + } + + stat = (struct ecryptfs_crypt_stat *)data; +return ecryptfs_get_salt_size_for_cipher ( +ecryptfs_get_full_cipher (stat->cipher, + stat->cipher_mode, + final,sizeof (final))); + +} + +/** +* Given ecryptfs data, the function +* returns appropriate cipher. +*/ +const unsigned char *ecryptfs_get_cipher (void *data) +{ +unsigned char final[2 *ECRYPTFS_MAX_CIPHER_NAME_SIZE +1 ]; +struct ecryptfs_crypt_stat *stat =NULL ; + +if (!data) { +ecryptfs_printk (KERN_ERR , +"ecryptfs_get_cipher: invalid data parameter\n" ); +return NULL ; + } + stat = (struct ecryptfs_crypt_stat *)data; +return ecryptfs_get_full_cipher (stat->cipher, stat->cipher_mode, + final,sizeof (final)); +} + +/** +* Given ecryptfs data, the function +* returns file encryption key. +*/ +const unsigned char *ecryptfs_get_key (void *data) +{ + +struct ecryptfs_crypt_stat *stat =NULL ; + +if (!data) { +ecryptfs_printk (KERN_ERR , +"ecryptfs_get_key: invalid data parameter\n" ); +return NULL ; + } + stat = (struct ecryptfs_crypt_stat *)data; +return stat->key; +} + +/** +* Given ecryptfs data, the function +* returns file encryption salt. +*/ +const unsigned char *ecryptfs_get_salt (void *data) +{ +struct ecryptfs_crypt_stat *stat =NULL ; + +if (!data) { +ecryptfs_printk (KERN_ERR , +"ecryptfs_get_salt: invalid data parameter\n" ); +return NULL ; + } + stat = (struct ecryptfs_crypt_stat *)data; +return stat->key +ecryptfs_get_salt_size (data); +} + +/** +* Returns ecryptfs events pointer +*/ +inline struct ecryptfs_events *get_events (void ) +{ +return events_ptr; +} + +/** +* If external crypto module requires salt in addition to key, +* we store it as part of key array (if there is enough space) +* Checks whether a salt key can fit into array allocated for +* regular key +*/ +bool ecryptfs_check_space_for_salt (const size_t key_size, +const size_t salt_size) +{ +if ((salt_size + key_size) >ECRYPTFS_MAX_KEY_BYTES ) +return false;Quoted 8 lines
Quoted 8 lines
fs/ecryptfs/events.c new file +6
-0+ +return true; +} + +/* +* If there is salt that is used by external crypto module, it is stored
+ * in the same array where regular key is. Salt is going to be usedby
Quoted 1 line
Quoted 1 line
+ * external crypto module only, so for all internal crypto
operations
Quoted 12 lines
Quoted 12 lines
salt
+ * should be ignored.
+ *
+ * Get key size in cases where it is going to be used for data encryption
+ * or for all other general purposes
+ */fs/ecryptfs/events.c new file +5
-0+size_t ecryptfs_get_key_size_to_enc_data ( +struct ecryptfs_crypt_stat *crypt_stat) +{ +if (!crypt_stat) +return 0 ;Quoted 8 lines
Quoted 8 lines
fs/ecryptfs/events.c new file +6
-0+ +return crypt_stat->key_size; +} + +/* +* If there is salt that is used by external crypto module, it is stored
+ * in the same array where regular key is. Salt is going to be usedby
Quoted 1 line
Quoted 1 line
+ * external crypto module only, but we still need to save and
restore
Quoted 12 lines
Quoted 12 lines
it
+ * (in encrypted form) as part of ecryptfs header along with the regular
+ * key.
+ *
+ * Get key size in cases where it is going to be stored persistently
+ *
+ * !!!crypt_statcipher name and mode must be initialized
+ */fs/ecryptfs/events.c new file +3
-0+size_t ecryptfs_get_key_size_to_store_key ( +struct ecryptfs_crypt_stat *crypt_stat) +{Quoted 8 lines
fs/ecryptfs/events.c
Quoted 8 lines
fs/ecryptfs/events.c new file +8
-0+size_t salt_size =0 ; + +if (!crypt_stat) +return 0 ; + + salt_size =ecryptfs_get_salt_size (crypt_stat); + + if (!ecryptfs_check_space_for_salt(crypt_stat->key_size,
salt_size)) {Quoted 2 lines
fs/ecryptfs/events.c
Quoted 2 lines
fs/ecryptfs/events.c new file +2
-0+ ecryptfs_printk(KERN_WARNING, + "ecryptfs_get_key_size_to_store_key: not enough space forsalt\n");
Quoted 10 lines
Quoted 10 lines
fs/ecryptfs/events.c new file +8
-0+return crypt_stat->key_size; + } + +return crypt_stat->key_size + salt_size; +} + +/* +* If there is salt that is used by external crypto module, it is stored
+ * in the same array where regular key is. Salt is going to be usedby
Quoted 1 line
Quoted 1 line
+ * external crypto module only, but we still need to save and
restore
Quoted 6 lines
Quoted 6 lines
it
+ * (in encrypted form) as part of ecryptfs header along with the regular
+ * key.
+ *
+ * Get key size in cases where it is going to be restored fromstorage
Quoted 46 lines
Quoted 46 lines
+ *
+ * !!!crypt_statcipher name and mode must be initialized
+ */fs/ecryptfs/events.c new file +13
-0+size_t ecryptfs_get_key_size_to_restore_key (size_t stored_key_size, +const char *cipher) +{ +size_t salt_size =0 ; + +if (!cipher) +return 0 ; + + salt_size =ecryptfs_get_salt_size_for_cipher (cipher); + +if (salt_size >= stored_key_size) { + ecryptfs_printk(KERN_WARNING, + "ecryptfs_get_key_size_to_restore_key: salt %zu >= stred size%zu\n",
fs/ecryptfs/events.c new file +18
-0+ salt_size, stored_key_size); + +return stored_key_size; + } + +return stored_key_size - salt_size; +} + +/** +* Given cipher, the function returns appropriate salt size. +*/ +size_t ecryptfs_get_salt_size_for_cipher (const char *cipher) +{ +if (!get_events () || !(get_events ()->get_salt_key_size_cb)) +return 0 ; + +return get_events ()->get_salt_key_size_cb (cipher); +}fs/ecryptfs/file.c +1
-0@@ -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" Quoted 2 lines
fs/ecryptfs/file.c static int ecryptfs_open(struct inode *inode,
Quoted 2 lines
fs/ecryptfs/file.c +0
-0/** @@ -184,6 +185,9 @@ static int ecryptfs_open(struct inode *inode,struct
Quoted 46 lines
Quoted 46 lines
fs/ecryptfs/file.c +3
-0file *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() */ fs/ecryptfs/file.c +19
-0struct 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:fs/ecryptfs/file.c +0
-0ecryptfs_put_lower_file (inode);@@ -261,9 +284,22 @@ static int ecryptfs_flush(struct file *file, fl_owner_t td)Quoted 16 lines
Quoted 6 lines
fs/ecryptfs/file.c +4
-0static int ecryptfs_release (struct inode *inode,struct file *file) { + +int ret; + + ret =vfs_fsync (file, false);fs/ecryptfs/file.c static int ecryptfs_flush(struct file *file, fl_owner_t td)
Quoted 10 lines
fs/ecryptfs/file.c +7
-0+ +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);
Quoted 24 lines
Quoted 24 lines
fs/ecryptfs/file.c +2
-0+truncate_inode_pages (inode->i_mapping,0 ); +truncate_inode_pages (ecryptfs_inode_to_lower (inode)->i_mapping,0 );return 0 ; }fs/ecryptfs/inode.c +3
-0@@ -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;Quoted 1 line
fs/ecryptfs/inode.c out: · line 275
Quoted 1 line
fs/ecryptfs/inode.c +0
-0ecryptfs_inode = ecryptfs_do_create(directory_inode,
ecryptfs_dentry,Quoted 9 lines
Quoted 9 lines
fs/ecryptfs/inode.c +1
-0mode);@@ -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 */ Quoted 8 lines
fs/ecryptfs/inode.c ecryptfs_create(struct inode *directory_inode, · line 297
Quoted 8 lines
fs/ecryptfs/inode.c +2
-0rc =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;
Quoted 5 lines
Quoted 5 lines
fs/ecryptfs/inode.c +5
-0+if (get_events () &&get_events ()->open_cb) +get_events ()->open_cb ( +ecryptfs_inode_to_lower (ecryptfs_inode), + crypt_stat); +Quoted 8 lines
fs/ecryptfs/keystore.c write_tag_66_packet(char *signature, u8
Quoted 8 lines
fs/ecryptfs/inode.c +0
-0d_instantiate (ecryptfs_dentry, ecryptfs_inode); out:fs/ecryptfs/inode.c +0
-0return rc;fs/ecryptfs/keystore.c +0
-0@@ -315,7 +315,8 @@ write_tag_66_packet(char *signature, u8
cipher_code,Quoted 10 lines
fs/ecryptfs/keystore.c write_tag_66_packet(char *signature, u8
Quoted 10 lines
fs/ecryptfs/keystore.c +2
-1* | File Encryption Key Size | 1 or 2 bytes | * | File Encryption Key | arbitrary | */ - data_len = (5 +ECRYPTFS_SIG_SIZE_HEX + crypt_stat->key_size); + data_len = (5 +ECRYPTFS_SIG_SIZE_HEX + +ecryptfs_get_key_size_to_store_key (crypt_stat)); *packet =kmalloc (data_len,GFP_KERNEL ); message = *packet;if (!message) {@@ -335,8 +336,9 @@ write_tag_66_packet(char *signature, u8
cipher_code,Quoted 3 lines
fs/ecryptfs/keystore.c write_tag_66_packet(char *signature, u8 cipher_code,
Quoted 3 lines
fs/ecryptfs/keystore.c +0
-0memcpy (&message[i], signature,ECRYPTFS_SIG_SIZE_HEX ); i +=ECRYPTFS_SIG_SIZE_HEX ;/* The encrypted key includes 1 byte cipher code and 2 byte checksum
Quoted 2 lines
fs/ecryptfs/keystore.c write_tag_66_packet(char *signature, u8 · line 339
Quoted 2 lines
fs/ecryptfs/keystore.c +0
-1*/ - rc = ecryptfs_write_packet_length(&message[i], crypt_stat->key_size+
Quoted 12 lines
Quoted 12 lines
fs/ecryptfs/keystore.c +3
-13 , - &packet_size_len); + rc =ecryptfs_write_packet_length (&message[i], +ecryptfs_get_key_size_to_store_key (crypt_stat) +3 , + &packet_size_len);if (rc) {fs/ecryptfs/keystore.c +0
-0ecryptfs_printk (KERN_ERR ,"Error generating tag 66 packet " "header; cannot generate packet length\n" );@@ -344,9 +346,10 @@ write_tag_66_packet(char *signature, u8 cipher_code, } i += packet_size_len;Quoted 8 lines
fs/ecryptfs/keystore.c write_tag_66_packet(char *signature, u8 cipher_code,
Quoted 8 lines
fs/ecryptfs/keystore.c +4
-3message[i++] = cipher_code; -memcpy (&message[i], crypt_stat->key, crypt_stat->key_size); - i += crypt_stat->key_size; -for (j =0 ; j < crypt_stat->key_size; j++) +memcpy (&message[i], crypt_stat->key, +ecryptfs_get_key_size_to_store_key (crypt_stat)); + i +=ecryptfs_get_key_size_to_store_key (crypt_stat); + for (j = 0; j < ecryptfs_get_key_size_to_store_key(crypt_stat);j++)
Quoted 4 lines
fs/ecryptfs/keystore.c ecryptfs_parse_tag_70_packet(char **filename,
Quoted 4 lines
fs/ecryptfs/keystore.c +0
-0checksum += crypt_stat->key[j]; message[i++] = (checksum /256 ) %256 ;fs/ecryptfs/keystore.c +0
-0message[i++] = (checksum %256 );@@ -918,6 +921,7 @@ ecryptfs_parse_tag_70_packet(char **filename,
size_tQuoted 6 lines
Quoted 6 lines
fs/ecryptfs/keystore.c +1
-0*filename_size, struct ecryptfs_parse_tag_70_packet_silly_stack *s;struct key *auth_tok_key =NULL ;int rc =0 ; +char full_cipher[ECRYPTFS_MAX_CIPHER_NAME_SIZE ];Quoted 8 lines
fs/ecryptfs/keystore.c ecryptfs_parse_tag_70_packet(char **filename, · line 981
Quoted 8 lines
fs/ecryptfs/keystore.c +0
-0(*packet_size) =0 ;fs/ecryptfs/keystore.c +0
-1(*filename_size) =0 ;@@ -977,12 +981,13 @@ ecryptfs_parse_tag_70_packet(char **filename,size_t *filename_size, s->fnek_sig_hex[ECRYPTFS_SIG_SIZE_HEX] = '\0'; (*packet_size) +=ECRYPTFS_SIG_SIZE ; s->cipher_code = data[(*packet_size)++]; - rc = ecryptfs_cipher_code_to_string(s->cipher_string,
s->cipher_code);Quoted 17 lines
Quoted 17 lines
fs/ecryptfs/keystore.c +2
-0+ rc =ecryptfs_cipher_code_to_string (full_cipher, s->cipher_code);if (rc) {printk (KERN_WARNING "%s: Cipher code [%d] is invalid\n" , __func__, s->cipher_code); goto out; } +ecryptfs_parse_full_cipher (full_cipher, s->cipher_string,0 ); rc = ecryptfs_find_auth_tok_for_sig(&auth_tok_key, &s->auth_tok, mount_crypt_stat,fs/ecryptfs/keystore.c +1
-0s->fnek_sig_hex);@@ -1151,6 +1156,7 @@ decrypt_pki_encrypted_session_key(struct ecryptfs_auth_tok *auth_tok, char *payload =NULL ;size_t payload_len =0 ;int rc; +char full_cipher[ECRYPTFS_MAX_CIPHER_NAME_SIZE ];Quoted 12 lines
fs/ecryptfs/keystore.c decrypt_pki_encrypted_session_key(struct · line 1192
Quoted 12 lines
fs/ecryptfs/keystore.c +0
-0rc =ecryptfs_get_auth_tok_sig (&auth_tok_sig, auth_tok);fs/ecryptfs/keystore.c +0
-5if (rc) {@@ -1184,21 +1190,31 @@ decrypt_pki_encrypted_session_key(structecryptfs_auth_tok *auth_tok, rc); goto out; } - auth_tok->session_key.flags |=ECRYPTFS_CONTAINS_DECRYPTED_KEY ; -memcpy (crypt_stat->key, auth_tok->session_key.decrypted_key, - auth_tok->session_key.decrypted_key_size); - crypt_stat->key_size = auth_tok->session_key.decrypted_key_size; - rc = ecryptfs_cipher_code_to_string(crypt_stat->cipher,
cipher_code);Quoted 80 lines
Quoted 80 lines
fs/ecryptfs/keystore.c +16
-1+ + rc =ecryptfs_cipher_code_to_string (full_cipher, cipher_code);if (rc) {ecryptfs_printk (KERN_ERR ,"Cipher code [%d] is invalid\n" , cipher_code) - goto out; + goto out; } + + auth_tok->session_key.flags |=ECRYPTFS_CONTAINS_DECRYPTED_KEY ; +memcpy (crypt_stat->key, auth_tok->session_key.decrypted_key, + auth_tok->session_key.decrypted_key_size); + crypt_stat->key_size =ecryptfs_get_key_size_to_restore_key ( + auth_tok->session_key.decrypted_key_size, full_cipher); + +ecryptfs_parse_full_cipher (full_cipher, + crypt_stat->cipher, crypt_stat->cipher_mode); + crypt_stat->flags |=ECRYPTFS_KEY_VALID ;if (ecryptfs_verbosity >0 ) {ecryptfs_printk (KERN_DEBUG ,"Decrypted session key:\n" );ecryptfs_dump_hex (crypt_stat->key, crypt_stat->key_size); + +ecryptfs_dump_salt_hex (crypt_stat->key, crypt_stat->key_size, + full_cipher); } out:fs/ecryptfs/keystore.c +1
-0kfree (msg);@@ -1380,6 +1396,7 @@ parse_tag_3_packet(struct ecryptfs_crypt_stat *crypt_stat, struct ecryptfs_auth_tok_list_item *auth_tok_list_item;size_t length_size;int rc =0 ; +char full_cipher[ECRYPTFS_MAX_CIPHER_NAME_SIZE ]; (*packet_size) =0 ;fs/ecryptfs/keystore.c +4
-1(*new_auth_tok) =NULL ;@@ -1453,10 +1470,13 @@ parse_tag_3_packet(struct ecryptfs_crypt_stat *crypt_stat, rc = -EINVAL ; goto out_free; } - rc =ecryptfs_cipher_code_to_string (crypt_stat->cipher, + rc =ecryptfs_cipher_code_to_string (full_cipher, (u16 )data[(*packet_size)]);if (rc) goto out_free; +ecryptfs_parse_full_cipher (full_cipher, + crypt_stat->cipher, crypt_stat->cipher_mode); +/* A little extra work to differentiate among the AES key * sizes; see RFC2440 */ fs/ecryptfs/keystore.c +4
-1switch (data[(*packet_size)++]) {@@ -1465,7 +1485,10 @@ parse_tag_3_packet(struct ecryptfs_crypt_stat *crypt_stat, break;default : crypt_stat->key_size = - (*new_auth_tok)->session_key.encrypted_key_size; +ecryptfs_get_key_size_to_restore_key ( + (*new_auth_tok)->session_key.encrypted_key_size, + full_cipher); + } rc =ecryptfs_init_crypt_ctx (crypt_stat);fs/ecryptfs/keystore.c +2
-0if (rc)@@ -1664,6 +1687,8 @@ static intdecrypt_passphrase_encrypted_session_key (struct ecryptfs_auth_tok *auth_tok, struct ecryptfs_crypt_stat *crypt_stat) { + + unsigned char final[2*ECRYPTFS_MAX_CIPHER_NAME_SIZE+1];struct scatterlist dst_sg[2 ];struct scatterlist src_sg[2 ];fs/ecryptfs/keystore.c +0
-0struct mutex *tfm_mutex;@@ -1713,7 +1738,7 @@ decrypt_passphrase_encrypted_session_key(structecryptfs_auth_tok *auth_tok,mutex_lock (tfm_mutex );Quoted 8 lines
fs/ecryptfs/keystore.c
Quoted 8 lines
fs/ecryptfs/keystore.c +1
-1rc =crypto_blkcipher_setkey ( desc.tfm, auth_tok->token.password.session_key_encryption_key, - crypt_stat->key_size); + auth_tok->token.password.session_key_encryption_key_bytes);if (unlikely (rc <0 )) {mutex_unlock (tfm_mutex);fs/ecryptfs/keystore.c +0
-0printk (KERN_ERR "Error setting key for crypto context\n" );@@ -1736,6 +1761,10 @@decrypt_passphrase_encrypted_session_key(struct
Quoted 29 lines
Quoted 29 lines
fs/ecryptfs/keystore.c +4
-0ecryptfs_auth_tok *auth_tok, crypt_stat->key_size);ecryptfs_dump_hex (crypt_stat->key, crypt_stat->key_size); +ecryptfs_dump_salt_hex (crypt_stat->key, crypt_stat->key_size, +ecryptfs_get_full_cipher (crypt_stat->cipher, + crypt_stat->cipher_mode, + final,sizeof (final))); } out:fs/ecryptfs/keystore.c +7
-4return rc;@@ -1972,12 +2001,17 @@ pki_encrypt_session_key(struct key *auth_tok_key, size_t payload_len = 0;struct ecryptfs_message *msg;int rc; +unsigned char final[2 *ECRYPTFS_MAX_CIPHER_NAME_SIZE +1 ]; rc = write_tag_66_packet(auth_tok->token.private_key.signature, -ecryptfs_code_for_cipher_string ( - crypt_stat->cipher, - crypt_stat->key_size), - crypt_stat, &payload, &payload_len); + ecryptfs_code_for_cipher_string( + ecryptfs_get_full_cipher( + crypt_stat->cipher, + crypt_stat->cipher_mode, + final, sizeof(final)), + ecryptfs_get_key_size_to_enc_data(Quoted 8 lines
fs/ecryptfs/keystore.c write_tag_1_packet(char *dest, size_t · line 2070
Quoted 8 lines
fs/ecryptfs/keystore.c +2
-0+ crypt_stat)), + crypt_stat, &payload, &payload_len);up_write (&(auth_tok_key->sem));key_put (auth_tok_key);fs/ecryptfs/keystore.c +0
-0if (rc) {@@ -2035,7 +2069,7 @@ write_tag_1_packet(char *dest, size_t *remaining_bytes, ecryptfs_from_hex(key_rec->sig,
auth_tok->token.private_key.signature,Quoted 4 lines
fs/ecryptfs/keystore.c write_tag_1_packet(char *dest, size_t *remaining_bytes,
Quoted 4 lines
fs/ecryptfs/keystore.c +1
-1ECRYPTFS_SIG_SIZE); encrypted_session_key_valid =0 ; -for (i =0 ; i < crypt_stat->key_size; i++) + for (i = 0; i < ecryptfs_get_key_size_to_store_key(crypt_stat);i++)
Quoted 78 lines
Quoted 78 lines
fs/ecryptfs/keystore.c +0
-0encrypted_session_key_valid |= auth_tok->session_key.encrypted_key[i];fs/ecryptfs/keystore.c +1
-0if (encrypted_session_key_valid) {@@ -2189,6 +2223,7 @@ write_tag_3_packet(char *dest, size_t *remaining_bytes, u8 cipher_code;size_t packet_size_length;size_t max_packet_size; +unsigned char final[2 *ECRYPTFS_MAX_CIPHER_NAME_SIZE +1 ];struct ecryptfs_mount_crypt_stat *mount_crypt_stat = crypt_stat->mount_crypt_stat;fs/ecryptfs/keystore.c +3
-2struct blkcipher_desc desc = {@@ -2221,13 +2256,14 @@ write_tag_3_packet(char *dest, size_t *remaining_bytes, mount_crypt_stat->global_default_cipher_key_size;if (auth_tok->session_key.encrypted_key_size ==0 ) auth_tok->session_key.encrypted_key_size = - crypt_stat->key_size; +ecryptfs_get_key_size_to_store_key (crypt_stat);if (crypt_stat->key_size ==24 &&strcmp ("aes" , crypt_stat->cipher) ==0 ) {memset ((crypt_stat->key +24 ),0 ,8 ); auth_tok->session_key.encrypted_key_size =32 ; }else - auth_tok->session_key.encrypted_key_size = crypt_stat->key_size; + auth_tok->session_key.encrypted_key_size = +ecryptfs_get_key_size_to_store_key (crypt_stat); key_rec->enc_key_size = auth_tok->session_key.encrypted_key_size;fs/ecryptfs/keystore.c +2
-2encrypted_session_key_valid =0 ;@@ -2251,8 +2287,8 @@ write_tag_3_packet(char *dest, size_t *remaining_bytes, auth_tok->token.password. session_key_encryption_key_bytes);memcpy (session_key_encryption_key, - auth_tok->token.password.session_key_encryption_key, - crypt_stat->key_size); + auth_tok->token.password.session_key_encryption_key, + auth_tok->token.password.session_key_encryption_key_bytes);ecryptfs_printk (KERN_DEBUG ,"Cached session key encryption key:\n" );fs/ecryptfs/keystore.c +1
-1if (ecryptfs_verbosity >0 )@@ -2285,7 +2321,7 @@ write_tag_3_packet(char *dest, size_t *remaining_bytes, }mutex_lock (tfm_mutex); rc =crypto_blkcipher_setkey (desc.tfm, session_key_encryption_key, - crypt_stat->key_size); + auth_tok->token.password.session_key_encryption_key_bytes);if (rc <0 ) {mutex_unlock (tfm_mutex);fs/ecryptfs/keystore.c +6
-1ecryptfs_printk(KERN_ERR, "Error setting key for crypto "@@ -2294,7 +2330,12 @@ write_tag_3_packet(char *dest, size_t *remaining_bytes, } rc =0 ;ecryptfs_printk (KERN_DEBUG ,"Encrypting [%zd] bytes of the key\n" , - crypt_stat->key_size); + crypt_stat->key_size); +ecryptfs_printk (KERN_DEBUG ,"Encrypting [%zd] bytes of the salt key\n", +ecryptfs_get_salt_size_for_cipher ( +ecryptfs_get_full_cipher (crypt_stat->cipher, + crypt_stat->cipher_mode, + final,sizeof (final)))); rc = crypto_blkcipher_encrypt(&desc, dst_sg, src_sg,fs/ecryptfs/keystore.c +4
-2(*key_rec).enc_key_size);mutex_unlock (tfm_mutex);@@ -2343,8 +2384,10 @@ encrypted_session_key_set: dest[(*packet_size)++] =0x04 ;/* version 4 */ /* TODO: Break from RFC2440 so that arbitrary ciphers can be * specified with strings */ - cipher_code =ecryptfs_code_for_cipher_string (crypt_stat->cipher, - crypt_stat->key_size); + cipher_code =ecryptfs_code_for_cipher_string ( +ecryptfs_get_full_cipher (crypt_stat->cipher, + crypt_stat->cipher_mode, final,sizeof (final)), + crypt_stat->key_size);Quoted 8 lines
fs/ecryptfs/main.c void ecryptfs_put_lower_file(struct inode
Quoted 8 lines
fs/ecryptfs/keystore.c +0
-0if (cipher_code ==0 ) {ecryptfs_printk (KERN_WARNING ,"Unable to generate code for " "cipher [%s]\n" , crypt_stat->cipher);fs/ecryptfs/main.c +0
-0@@ -165,7 +165,13 @@ void ecryptfs_put_lower_file(struct inode*inode)
Quoted 107 lines
Quoted 107 lines
fs/ecryptfs/main.c +7
-0fput (inode_info->lower_file); inode_info->lower_file =NULL ;mutex_unlock (&inode_info->lower_file_mutex); + +if (get_events () &&get_events ()->release_cb) +get_events ()->release_cb ( +ecryptfs_inode_to_lower (inode)); } + + }enum { ecryptfs_opt_sig, ecryptfs_opt_ecryptfs_sig,@@ -266,6 +272,7 @@ static int ecryptfs_parse_options(struct ecryptfs_sb_info *sbi, char *options, int cipher_key_bytes_set = 0;int fn_cipher_key_bytes;int fn_cipher_key_bytes_set =0 ; +size_t salt_size =0 ;struct ecryptfs_mount_crypt_stat *mount_crypt_stat = &sbi->mount_crypt_stat;fs/ecryptfs/main.c +8
-5substring_t args[MAX_OPT_ARGS ];@@ -280,6 +287,7 @@ static int ecryptfs_parse_options(struct ecryptfs_sb_info *sbi, char *options, char *cipher_key_bytes_src;char *fn_cipher_key_bytes_src;u8 cipher_code; +unsigned char final[2 *ECRYPTFS_MAX_CIPHER_NAME_SIZE +1 ]; *check_ruid =0 ;@@ -309,12 +317,14 @@ static int ecryptfs_parse_options(struct ecryptfs_sb_info *sbi, char *options, case ecryptfs_opt_ecryptfs_cipher: cipher_name_src = args[0 ].from; cipher_name_dst = - mount_crypt_stat-> - global_default_cipher_name; -strncpy (cipher_name_dst, cipher_name_src, -ECRYPTFS_MAX_CIPHER_NAME_SIZE ); - cipher_name_dst[ECRYPTFS_MAX_CIPHER_NAME_SIZE ] ='\0' ; + mount_crypt_stat->global_default_cipher_name; + +ecryptfs_parse_full_cipher (cipher_name_src, + mount_crypt_stat->global_default_cipher_name, + mount_crypt_stat->global_default_cipher_mode); + cipher_name_set =1 ; +break ;case ecryptfs_opt_ecryptfs_key_bytes:fs/ecryptfs/main.c +31
-5cipher_key_bytes_src = args[0 ].from;@@ -411,24 +421,50 @@ static int ecryptfs_parse_options(struct ecryptfs_sb_info *sbi, char *options,strcpy (mount_crypt_stat->global_default_cipher_name,ECRYPTFS_DEFAULT_CIPHER ); } +if ((mount_crypt_stat->flags &ECRYPTFS_GLOBAL_ENCRYPT_FILENAMES ) && !fn_cipher_name_set)strcpy (mount_crypt_stat->global_default_fn_cipher_name, mount_crypt_stat->global_default_cipher_name); -if (!cipher_key_bytes_set) + +if (cipher_key_bytes_set) { + + salt_size =ecryptfs_get_salt_size_for_cipher ( +ecryptfs_get_full_cipher ( + mount_crypt_stat->global_default_cipher_name, + mount_crypt_stat->global_default_cipher_mode, + final,sizeof (final))); + +if (!ecryptfs_check_space_for_salt ( + mount_crypt_stat->global_default_cipher_key_size, + salt_size)) { +ecryptfs_printk ( +KERN_WARNING , +"eCryptfs internal error: no space for salt" ); + } + }else mount_crypt_stat->global_default_cipher_key_size =0 ; +if ((mount_crypt_stat->flags &ECRYPTFS_GLOBAL_ENCRYPT_FILENAMES ) && !fn_cipher_key_bytes_set) mount_crypt_stat->global_default_fn_cipher_key_bytes = mount_crypt_stat->global_default_cipher_key_size; cipher_code =ecryptfs_code_for_cipher_string ( - 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)), mount_crypt_stat->global_default_cipher_key_size);if (!cipher_code) { -ecryptfs_printk (KERN_ERR , -"eCryptfs doesn't support cipher: %s" , - mount_crypt_stat->global_default_cipher_name); +ecryptfs_printk ( +KERN_ERR , +"eCryptfs doesn't support cipher: %s and key size %zu" , +ecryptfs_get_full_cipher ( + mount_crypt_stat->global_default_cipher_name, + mount_crypt_stat->global_default_cipher_mode, + final,sizeof (final)), + mount_crypt_stat->global_default_cipher_key_size); rc = -EINVAL ;Quoted 8 lines
fs/ecryptfs/main.c static struct file_system_type ecryptfs_fs_type; · line 528
Quoted 8 lines
fs/ecryptfs/main.c +0
-0goto out;fs/ecryptfs/main.c +1
-0}@@ -488,6 +524,7 @@ static struct file_system_type ecryptfs_fs_type;* @dev_name: The path to mount over * @raw_data: The options passed into the kernel */ + static struct dentry *ecryptfs_mount(struct file_system_type
*fs_type,Quoted 21 lines
Quoted 21 lines
fs/ecryptfs/main.c +0
-0int flags, const char *dev_name, void *raw_data){
fs/ecryptfs/main.c +0
-0@@ -557,6 +594,8 @@ static struct dentry *ecryptfs_mount(struct
file_system_type*fs_type, int flagsfs/ecryptfs/main.c +2
-0ecryptfs_set_superblock_lower (s, path.dentry->d_sb); +ecryptfs_drop_pagecache_sb (ecryptfs_superblock_to_lower (s),NULL ); +/** * Set the POSIX ACL flag based on whether they're enabled in the lower
fs/ecryptfs/main.c +1
-0* mount. @@ -894,6 +933,7 @@ static void __exit ecryptfs_exit(void)do_sysfs_unregistration ();unregister_filesystem (&ecryptfs_fs_type);ecryptfs_free_kmem_caches (); +ecryptfs_free_events (); }Quoted 6 lines
fs/ecryptfs/mmap.c static sector_t ecryptfs_bmap(struct
Quoted 6 lines
fs/ecryptfs/main.c +0
-0MODULE_AUTHOR ("Michael A. Halcrow <[email protected]>" );fs/ecryptfs/mmap.c +0
-0@@ -552,10 +552,16 @@ static sector_t ecryptfs_bmap(struct
address_spaceQuoted 14 lines
Quoted 14 lines
fs/ecryptfs/mmap.c +5
-0*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,Quoted 8 lines
fs/ecryptfs/super.c static void ecryptfs_i_callback(struct rcu_head
Quoted 8 lines
fs/ecryptfs/mmap.c +1
-0.bmap = ecryptfs_bmap, + .freepage = ecryptfs_freepage,};
fs/ecryptfs/super.c +0
-0@@ -69,6 +69,9 @@ static void ecryptfs_i_callback(struct rcu_head*head)
Quoted 22 lines
Quoted 22 lines
fs/ecryptfs/super.c +6
-0{ 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); + }Quoted 2 lines
fs/ecryptfs/super.c static int ecryptfs_show_options(struct seq_file
Quoted 2 lines
/**
fs/ecryptfs/super.c +0
-0@@ -149,6 +155,9 @@ static int ecryptfs_show_options(struct seq_file*m,
Quoted 82 lines
Quoted 82 lines
fs/ecryptfs/super.c +3
-0struct 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);fs/ecryptfs/super.c +4
-1list_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)fs/ecryptfs/super.c +0
-0seq_printf(m, ",ecryptfs_key_bytes=%zd",include/linux/ecryptfs.h +14
-0@@ -1,6 +1,9 @@#ifndef _LINUX_ECRYPTFS_H#define _LINUX_ECRYPTFS_H +struct inode ; +struct page ; +/* Version verification for shared data structures w/ userspace */ #define ECRYPTFS_VERSION_MAJOR 0x00#define ECRYPTFS_VERSION_MINOR 0x04@@ -41,6 +44,7 @@#define RFC2440_CIPHER_AES_256 0x09#define RFC2440_CIPHER_TWOFISH 0x0a#define RFC2440_CIPHER_CAST_6 0x0b +#define RFC2440_CIPHER_AES_XTS_256 0x0c#define RFC2440_CIPHER_RSA 0x01@@ -102,4 +106,47 @@ struct ecryptfs_auth_tok { } token; } __attribute__ ((packed)); +#define ECRYPTFS_INVALID_EVENTS_HANDLE -1 + +/** +* ecryptfs_events struct represents a partial interface +* towards ecryptfs module. If registered to ecryptfs events, +* one can receive push notifications. +* A first callback received from ecryptfs will probably be +* about file opening (open_cb), +* in which ecryptfs passes its ecryptfs_data for future usage. +* This data represents a file and must be passed in every query functions
+ * such asecryptfs_get_key_size(),ecryptfs_get_cipher()etc.
+ */include/linux/ecryptfs.h +20
-0+struct ecryptfs_events { +bool (*is_cipher_supported_cb)(const char *cipher); +void (*open_cb)(struct inode *inode,void *ecrytpfs_data); +void (*release_cb)(struct inode *inode); +int (*encrypt_cb)(struct page *in_page,struct page *out_page, +struct inode *inode,unsigned long extent_offset); +int (*decrypt_cb)(struct page *in_page,struct page *out_page, +struct inode *inode,unsigned long extent_offset); +bool (*is_hw_crypt_cb)(void ); +size_t (*get_salt_key_size_cb)(const char *cipher); +}; + + +int ecryptfs_register_to_events (struct ecryptfs_events *ops); + +int ecryptfs_unregister_from_events (int user_handle); + +const unsigned char *ecryptfs_get_key (void *ecrytpfs_data); + +size_t ecryptfs_get_key_size (void *ecrytpfs_data);Quoted 8 lines
include/linux/ecryptfs.h struct ecryptfs_auth_tok {
Quoted 8 lines
include/linux/ecryptfs.h +8
-0+ +const unsigned char *ecryptfs_get_salt (void *ecrytpfs_data); + +size_t ecryptfs_get_salt_size (void *ecrytpfs_data); + +const unsigned char *ecryptfs_get_cipher (void *ecrytpfs_data); + +bool ecryptfs_is_page_in_metadata(void *ecrytpfs_data, pgoff_toffset);
Quoted 12 lines
Quoted 11 lines
include/linux/ecryptfs.h +2
-0+ +bool ecryptfs_is_data_equal(void *ecrytpfs_data1, void
*ecrytpfs_data2);
+
#endif /*_LINUX_ECRYPTFS_H*/
--
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
Hi Michael,
First of all, the original mechanism is still there and is used by default, unless someone registers for external module and then indeed the encryption is replaced for ciphers that this module decides to support. Currently the only suggestion is to extend the framework that will allow such modules to be added in the future and of course each such module will have to be audited and follow all the requirements that you mentioned. As on what this module is supposed to do, please see my responses to Tyler.
Quoted 1,894 lines
This is a hardware inline accelerator, meaning that it operates on much lower layer, block layer and device driver layer. The HW encrypts plain requests sent from block layer directly, thus doing it much more efficiently rather than using crypto API.
In order to use such HW efficiently with eCryptfs, eCryptfs encryption has to be canceled and it will need to call for external module instead that will do the correct marking for the blocks to distinguish between those that need to be encrypted by the HW from those that do not need.We are considering posting the code that will call
ecryptfs_register_to_events()as a separate patch, but haven't done so yet. It is HW specific.
Currently we are only proposing framework change so that it can allow for external modules to be connectedQuoted 32 lines · On 2015-11-09 20:56:02, [email protected] wrote:
Quoted 6 lines
Hello, Tyler
I'll try to provide more detailed explanation, should it be satisfactory
enough I will update the patch description.Quoted 10 lines
The problem with current eCryptfs is that it has total control on how and
when the encryption is performed and this control can't be altered. One example when this can be a problem is when we want to utilize an underlying inline HW encryption engine which allows encrypting blocks 'on
the fly' as they are being written to the storage. In such a case relevant
blocks just need to be marked as 'should be encrypted'. No actual encryption should be done by eCryptfs as it will be much slower.Is this a hardware crypto accelerator? If so, why not create a crypto api driver so all subsystems can take advantage of the acceleration instead of baking support into individual subsystems?
Quoted 9 lines
The provided framework allows transferring this control (if needed) to some external module which will do the encryption itfelf or just mark the
appropriate blocks.There is no caller for
ecryptfs_register_to_events()since this change only provides framework, it doesn't provide the module itself, the module
could be HW dependent.Quoted 1,841 lines
Will the code that you plan to call
ecryptfs_register_to_events()be upstream? If so, have you posted it?Tyler
Quoted 1,835 lines
Regarding the mounting option, it merely serves as example of new cipher
mode that can be served by registered module.
There is a special callback function that should be implemented by the registered module that tells whether a particular cipher is supported by
it :is_cipher_supported_cb()The mounting option itself is not necessary, I can remove it
Quoted 10 lines
Hello Andrey!
Quoted 7 lines · Andrey Markovytch
From: Andrey Markovytch <[email protected]>
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
Quoted 2 lines
Quoted 2 lines
the inline HW engine.
Additional cipher option was introduced to support the HW/externalmode
Quoted 4 lines
Quoted 2 lines
under that name of "aes-xts". If no external module has registered to support this cipher, eCryptfs will fall back to the usual "aes"
What is "HW/external mode"? There's no description in the commit
message
Quoted 1 line
and
ecryptfs_register_to_events()does not have a caller so I'm notsure
Quoted 3 lines
of the purpose. Please provide more context.
Despite not yet understanding the purpose of this patch, I think that
I
Quoted 1 line
can safely say that "aes-xts" is not an appropriate mount option to
use
Quoted 2 lines
when enabling this mode. eCryptfs may support XTS mode one day, using the Crypto API, so "HW/external mode" should not own the mount
option.
Quoted 40 lines
Tyler
Quoted 37 lines
Signed-off-by Lina Zarivach <[email protected]>Signed-off-by Andrey Markovytch <[email protected]>--- 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.cfs/ecryptfs/Makefile +2
-2@@ -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.oQuoted 11 lines
fs/ecryptfs/caches_utils.c line 4
Quoted 11 lines
ecryptfs-$(CONFIG_ECRYPT_FS_MESSAGING) += messaging.o miscdev.o
fs/ecryptfs/caches_utils.c new file +4
-0@@ -0,0 +1,78 @@ +/* +* Copyright (c) 2015, The Linux Foundation. All rights reserved. +* +* This program is free software; you can redistribute it and/or modify
Quoted 1 line
fs/ecryptfs/caches_utils.c line 5
Quoted 1 line
fs/ecryptfs/caches_utils.c new file +1
-0+* it under the terms of the GNU General Public License version 2 and
Quoted 9 lines
Quoted 9 lines
fs/ecryptfs/caches_utils.c new file +9
-0+* only version 2 as published by the Free Software Foundation. +* +* This program is distributed in the hope that it will be useful, +* but WITHOUT ANY WARRANTY; without even the implied warranty of +* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +* GNU General Public License for more details. +*/ + +#include <linux/kernel.h> Quoted 8 lines
fs/ecryptfs/caches_utils.c
Quoted 8 lines
fs/ecryptfs/caches_utils.c new file +8
-0+#include <linux/fs.h> +#include <linux/spinlock.h> +#include <linux/pagemap.h> +#include <linux/pagevec.h> + +#include "../internal.h" + +void ecryptfs_drop_pagecache_sb(struct super_block *sb, void*unused)
Quoted 116 lines
Quoted 116 lines
fs/ecryptfs/caches_utils.c new file +56
-0+{ +struct inode *inode, *toput_inode =NULL ; + +spin_lock (&sb->s_inode_list_lock); +list_for_each_entry (inode, &sb->s_inodes, i_sb_list) { +spin_lock (&inode->i_lock); +if ((inode->i_state & (I_FREEING |I_WILL_FREE |I_NEW )) || + (inode->i_mapping->nrpages ==0 )) { +spin_unlock (&inode->i_lock); +continue ; + } +__iget (inode); +spin_unlock (&inode->i_lock); +spin_unlock (&sb->s_inode_list_lock); + +invalidate_mapping_pages (inode->i_mapping,0 ,-1 ); +iput (toput_inode); + toput_inode = inode; + +spin_lock (&sb->s_inode_list_lock); + } +spin_unlock (&sb->s_inode_list_lock); +iput (toput_inode); +} + +void clean_inode_pages (struct address_space *mapping, +pgoff_t start,pgoff_t end) +{ +struct pagevec pvec; +pgoff_t index = start; +int i; + +pagevec_init (&pvec,0 ); +while (index <= end &&pagevec_lookup (&pvec, mapping, index, +min (end - index, + (pgoff_t )PAGEVEC_SIZE -1 ) +1 )) { +for (i =0 ; i <pagevec_count (&pvec); i++) { +struct page *page = pvec.pages[i]; + +/* We rely upon deletion +* not changing page->index +*/ + index = page->index; +if (index > end) +break ; +if (!trylock_page (page)) +continue ; +WARN_ON (page->index != index); +zero_user (page,0 ,PAGE_CACHE_SIZE ); +unlock_page (page); + } +pagevec_release (&pvec); +cond_resched (); + index++; + } +}fs/ecryptfs/crypto.c +3
-2@@ -35,6 +35,7 @@#include <linux/scatterlist.h> #include <linux/slab.h> #include <asm/unaligned.h> +#include <linux/ecryptfs.h> #include "ecryptfs_kernel.h" #define DECRYPT 0@@ -350,9 +351,9 @@ static int crypt_scatterlist(struct ecryptfs_crypt_stat *crypt_stat, || !(crypt_stat->flags & ECRYPTFS_STRUCT_INITIALIZED));if (unlikely (ecryptfs_verbosity >0 )) {ecryptfs_printk (KERN_DEBUG ,"Key size [%zd]; key:\n" , - crypt_stat->key_size); +ecryptfs_get_key_size_to_enc_data (crypt_stat));ecryptfs_dump_hex (crypt_stat->key, - crypt_stat->key_size); +ecryptfs_get_key_size_to_enc_data (crypt_stat)); }fs/ecryptfs/crypto.c +1
-1init_completion (&ecr.completion);@@ -371,7 +372,7 @@ static int crypt_scatterlist(struct ecryptfs_crypt_stat *crypt_stat,/* Consider doing this once, when the file is opened */ if (!(crypt_stat->flags & ECRYPTFS_KEY_SET)) { rc =crypto_ablkcipher_setkey (crypt_stat->tfm, crypt_stat->key, - crypt_stat->key_size); +ecryptfs_get_key_size_to_enc_data (crypt_stat));if (rc) { ecryptfs_printk(KERN_ERR,fs/ecryptfs/crypto.c +20
-0"Error setting key; rc = [%d]\n",@@ -466,6 +467,31 @@ out:return rc; } +static void init_ecryption_parameters (bool *hw_crypt,bool *cipher_supported, +struct ecryptfs_crypt_stat *crypt_stat) +{ +unsigned char final[2 *ECRYPTFS_MAX_CIPHER_NAME_SIZE +1 ]; + +if (!hw_crypt || !cipher_supported) +return ; + + *cipher_supported = false; + *hw_crypt = false; + +if (get_events () &&get_events ()->is_cipher_supported_cb) { + *cipher_supported = +get_events ()->is_cipher_supported_cb ( +ecryptfs_get_full_cipher (crypt_stat->cipher, + crypt_stat->cipher_mode, final,sizeof (final))); +if (*cipher_supported) { +/* we should apply external algorythm +* assume that is_hw_crypt() cbck is supplied +*/ Quoted 8 lines
Quoted 8 lines
fs/ecryptfs/crypto.c +5
-0+ *hw_crypt =get_events ()->is_hw_crypt_cb (); + } + } +} +/** * ecryptfs_encrypt_page * @page: Page mapped from the eCryptfs inode for the file;
contains
Quoted 155 lines
Quoted 155 lines
fs/ecryptfs/crypto.c +47
-13@@ -491,11 +517,18 @@ int ecryptfs_encrypt_page(struct page *page)loff_t extent_offset;loff_t lower_offset;int rc =0 ; +bool is_hw_crypt; +bool is_cipher_supported; + ecryptfs_inode = page->mapping->host; crypt_stat = &(ecryptfs_inode_to_private (ecryptfs_inode)->crypt_stat);BUG_ON (!(crypt_stat->flags &ECRYPTFS_ENCRYPTED )); + +init_ecryption_parameters (&is_hw_crypt, + &is_cipher_supported, crypt_stat); + enc_extent_page =alloc_page (GFP_USER );if (!enc_extent_page) { rc = -ENOMEM ;@@ -503,24 +536,51 @@ int ecryptfs_encrypt_page(struct page *page) "encrypted extent\n"); goto out; } - -for (extent_offset =0 ; - extent_offset < (PAGE_CACHE_SIZE / crypt_stat->extent_size); - extent_offset++) { - rc =crypt_extent (crypt_stat, enc_extent_page, page, - extent_offset,ENCRYPT ); -if (rc) { -printk (KERN_ERR "%s: Error encrypting extent; " -"rc = [%d]\n" , __func__, rc); - goto out; - } +if (is_hw_crypt) { +/* no need for encryption */ + }else { +for (extent_offset =0 ; + extent_offset < + (PAGE_CACHE_SIZE / crypt_stat->extent_size); + extent_offset++) { + +if (is_cipher_supported) { +if (!get_events ()->encrypt_cb) { + rc = -EPERM ; + goto out; + } + rc =get_events ()->encrypt_cb (page, + enc_extent_page, +ecryptfs_inode_to_lower ( + ecryptfs_inode), + extent_offset); + }else { + rc =crypt_extent (crypt_stat, + enc_extent_page, page, + extent_offset,ENCRYPT ); + } +if (rc) { +ecryptfs_printk (KERN_ERR , +"%s: Error encrypting; rc = [%d]\n" , + __func__, rc); + goto out; + } + } } lower_offset =lower_offset_for_page (crypt_stat, page); - enc_extent_virt =kmap (enc_extent_page); +if (is_hw_crypt) + enc_extent_virt =kmap (page); +else + enc_extent_virt =kmap (enc_extent_page); + rc =ecryptfs_write_lower (ecryptfs_inode, enc_extent_virt, lower_offset,PAGE_CACHE_SIZE ); -kunmap (enc_extent_page); +if (!is_hw_crypt) +kunmap (enc_extent_page); +else +kunmap (page); +if (rc <0 ) { ecryptfs_printk(KERN_ERR,fs/ecryptfs/crypto.c +25
-3"Error attempting to write lower page; rc = [%d]\n",@@ -559,6 +619,8 @@ int ecryptfs_decrypt_page(struct page *page)unsigned long extent_offset;loff_t lower_offset;int rc =0 ; +bool is_cipher_supported; +bool is_hw_crypt; ecryptfs_inode = page->mapping->host; crypt_stat =@@ -577,13 +639,33 @@ int ecryptfs_decrypt_page(struct page *page) goto out; } +init_ecryption_parameters (&is_hw_crypt, + &is_cipher_supported, crypt_stat); + +if (is_hw_crypt) { + rc =0 ; +return rc; + } +for (extent_offset =0 ; extent_offset < (PAGE_CACHE_SIZE / crypt_stat->extent_size); extent_offset++) { - rc =crypt_extent (crypt_stat, page, page, +if (is_cipher_supported) { +if (!get_events ()->decrypt_cb) { + rc = -EPERM ; + goto out; + } + + rc =get_events ()->decrypt_cb (page, page, +ecryptfs_inode_to_lower (ecryptfs_inode), + extent_offset); + + }else + rc =crypt_extent (crypt_stat, page, page, extent_offset,DECRYPT ); +if (rc) { -printk (KERN_ERR "%s: Error encrypting extent; " +ecryptfs_printk (KERN_ERR ,"%s: Error decrypting extent;" "rc = [%d]\n" , __func__, rc); goto out; }@@ -612,7 +694,7 @@ int ecryptfs_init_crypt_ctx(struct ecryptfs_crypt_stat *crypt_stat)"Initializing cipher [%s]; strlen = [%d]; " "key_size_bits = [%zd]\n" , crypt_stat->cipher, (int )strlen (crypt_stat->cipher), - crypt_stat->key_size << 3); + ecryptfs_get_key_size_to_enc_data(crypt_stat) << 3);mutex_lock (&crypt_stat->cs_tfm_mutex);if (crypt_stat->tfm) {fs/ecryptfs/crypto.c +1
-1rc =0 ;@@ -694,7 +776,7 @@ int ecryptfs_compute_root_iv(struct ecryptfs_crypt_stat *crypt_stat) goto out; } rc =ecryptfs_calculate_md5 (dst, crypt_stat, crypt_stat->key, - crypt_stat->key_size); +ecryptfs_get_key_size_to_enc_data (crypt_stat));if (rc) { ecryptfs_printk(KERN_WARNING, "Error attempting to compute "fs/ecryptfs/crypto.c +0
-0"MD5 while generating root IV\n");@@ -721,6 +803,35 @@ static void ecryptfs_generate_new_key(struct ecryptfs_crypt_stat *crypt_stat) } }Quoted 15 lines
Quoted 5 lines
fs/ecryptfs/crypto.c +4
-0+static int ecryptfs_generate_new_salt (struct ecryptfs_crypt_stat *crypt_stat) +{ +size_t salt_size =0 ; +unsigned char final[2 *ECRYPTFS_MAX_CIPHER_NAME_SIZE +1 ];fs/ecryptfs/crypto.c static void ecryptfs_generate_new_key(struct ecryptfs_crypt_stat *crypt_stat)
Quoted 10 lines
fs/ecryptfs/crypto.c +10
-0+ + salt_size =ecryptfs_get_salt_size_for_cipher ( +ecryptfs_get_full_cipher (crypt_stat->cipher, + crypt_stat->cipher_mode, + final,sizeof (final))); + +if (salt_size ==0 ) +return 0 ; + + if (!ecryptfs_check_space_for_salt(crypt_stat->key_size,
salt_size)) {Quoted 6 lines
fs/ecryptfs/crypto.c static void ecryptfs_generate_new_key(struct ecryptfs_crypt_stat *crypt_stat)
Quoted 6 lines
fs/ecryptfs/crypto.c +6
-0+ecryptfs_printk (KERN_WARNING ,"not enough space for salt\n" ); + crypt_stat->flags |=ECRYPTFS_SECURITY_WARNING ; +return -EINVAL ; + } + + get_random_bytes(crypt_stat->key + crypt_stat->key_size,
salt_size);Quoted 20 lines
Quoted 20 lines
fs/ecryptfs/crypto.c +9
-0+if (unlikely (ecryptfs_verbosity >0 )) { +ecryptfs_printk (KERN_DEBUG ,"Generated new session salt:\n" ); +ecryptfs_dump_hex (crypt_stat->key + crypt_stat->key_size, + salt_size); + } + +return 0 ; +} +/** *
ecryptfs_copy_mount_wide_flags_to_inode_flags
* @crypt_stat: The inode's cryptographic contextfs/ecryptfs/crypto.c +0
-1@@ -823,7 +934,6 @@ int ecryptfs_new_file_context(struct inode *ecryptfs_inode)struct ecryptfs_mount_crypt_stat *mount_crypt_stat = &ecryptfs_superblock_to_private ( ecryptfs_inode->i_sb)->mount_crypt_stat; -int cipher_name_len;int rc =0 ;Quoted 1 line
Quoted 1 line
ecryptfs_set_default_crypt_stat_vals(crypt_stat,
mount_crypt_stat);Quoted 67 lines
Quoted 67 lines
fs/ecryptfs/crypto.c +9
-5@@ -837,15 +947,19 @@ int ecryptfs_new_file_context(struct inode *ecryptfs_inode) "to the inode key sigs; rc = [%d]\n", rc); goto out; } - cipher_name_len = -strlen (mount_crypt_stat->global_default_cipher_name); -memcpy (crypt_stat->cipher, +strlcpy (crypt_stat->cipher, mount_crypt_stat->global_default_cipher_name, - cipher_name_len); - crypt_stat->cipher[cipher_name_len] ='\0' ; +sizeof (crypt_stat->cipher)); + +strlcpy (crypt_stat->cipher_mode, + mount_crypt_stat->global_default_cipher_mode, +sizeof (crypt_stat->cipher_mode)); + crypt_stat->key_size = mount_crypt_stat->global_default_cipher_key_size;ecryptfs_generate_new_key (crypt_stat); +ecryptfs_generate_new_salt (crypt_stat); + rc =ecryptfs_init_crypt_ctx (crypt_stat);if (rc)fs/ecryptfs/crypto.c +7
-1ecryptfs_printk(KERN_ERR, "Error initializing cryptographic "@@ -971,7 +1085,8 @@ ecryptfs_cipher_code_str_map[] = { {"twofish", RFC2440_CIPHER_TWOFISH}, {"cast6", RFC2440_CIPHER_CAST_6}, {"aes", RFC2440_CIPHER_AES_192}, - {"aes", RFC2440_CIPHER_AES_256} + {"aes", RFC2440_CIPHER_AES_256}, + {"aes_xts", RFC2440_CIPHER_AES_XTS_256} };/** @@ -999,6 +1114,11 @@ u8 ecryptfs_code_for_cipher_string(char *cipher_name, size_t key_bytes)case 32 : code =RFC2440_CIPHER_AES_256 ; } + }else if (strcmp (cipher_name,"aes_xts" ) ==0 ) { +switch (key_bytes) { +case 32 : + code =RFC2440_CIPHER_AES_XTS_256 ; + } }else {for (i =0 ; i <ARRAY_SIZE (ecryptfs_cipher_code_str_map); i++)fs/ecryptfs/crypto.c +12
-0if (strcmp (cipher_name, map[i].cipher_str) ==0 ) {@@ -1038,9 +1158,24 @@ int ecryptfs_read_and_validate_header_region(struct inode *inode) u8 file_size[ECRYPTFS_SIZE_AND_MARKER_BYTES];u8 *marker = file_size +ECRYPTFS_FILE_SIZE_BYTES ;int rc; +unsigned int ra_pages_org; +struct file *lower_file =NULL ; + +if (!inode) +return -EIO ; + lower_file =ecryptfs_inode_to_private (inode)->lower_file; +if (!lower_file) +return -EIO ; + +/*disable read a head mechanism for a while */ + ra_pages_org = lower_file->f_ra.ra_pages; + lower_file->f_ra.ra_pages =0 ;Quoted 1 line
fs/ecryptfs/crypto.c int · line 1175
Quoted 1 line
fs/ecryptfs/crypto.c +0
-0rc = ecryptfs_read_lower(file_size, 0,
ECRYPTFS_SIZE_AND_MARKER_BYTES,Quoted 74 lines
Quoted 74 lines
fs/ecryptfs/crypto.c +3
-0inode); + lower_file->f_ra.ra_pages = ra_pages_org; +/* restore read a head mechanism */ +if (rc <ECRYPTFS_SIZE_AND_MARKER_BYTES )return rc >=0 ? -EINVAL : rc;fs/ecryptfs/crypto.c +5
-0rc =ecryptfs_validate_marker (marker);@@ -1430,6 +1565,11 @@ int ecryptfs_read_metadata(struct dentry *ecryptfs_dentry)struct ecryptfs_mount_crypt_stat *mount_crypt_stat = &ecryptfs_superblock_to_private ( ecryptfs_dentry->d_sb)->mount_crypt_stat; +unsigned int ra_pages_org; +struct file *lower_file = +ecryptfs_inode_to_private (ecryptfs_inode)->lower_file; +if (!lower_file) +return -EIO ; ecryptfs_copy_mount_wide_flags_to_inode_flags(crypt_stat,fs/ecryptfs/crypto.c +6
-0mount_crypt_stat);@@ -1441,8 +1581,14 @@ int ecryptfs_read_metadata(struct dentry *ecryptfs_dentry) __func__); goto out; } +/*disable read a head mechanism */ + ra_pages_org = lower_file->f_ra.ra_pages; + lower_file->f_ra.ra_pages =0 ; + rc =ecryptfs_read_lower (page_virt,0 , crypt_stat->extent_size, ecryptfs_inode); + lower_file->f_ra.ra_pages = ra_pages_org;/* restore it back */ +if (rc >=0 ) rc = ecryptfs_read_headers_virt(page_virt, crypt_stat,fs/ecryptfs/crypto.c +0
-0ecryptfs_dentry,fs/ecryptfs/debug.c +13
-0@@ -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 +1
-0@@ -245,6 +245,7 @@ struct ecryptfs_crypt_stat {struct mutex cs_tfm_mutex;struct mutex cs_hash_tfm_mutex;struct mutex cs_mutex; +unsigned char cipher_mode[ECRYPTFS_MAX_CIPHER_NAME_SIZE +1 ]; };/* inode private data. */ @@ -267,6 +268,8 @@ struct ecryptfs_dentry_info { }; };Quoted 4 lines
fs/ecryptfs/ecryptfs_kernel.h struct ecryptfs_dentry_info { · line 274
Quoted 4 lines
fs/ecryptfs/ecryptfs_kernel.h +2
-0+ +/** * ecryptfs_global_auth_tok - A key used to encrypt all new files under
Quoted 7 lines
fs/ecryptfs/ecryptfs_kernel.h struct ecryptfs_mount_crypt_stat { · line 351
Quoted 7 lines
the mountpoint
* @flags: Status flagsfs/ecryptfs/ecryptfs_kernel.h +1
-0@@ -345,6 +348,8 @@ struct ecryptfs_mount_crypt_stat {unsigned char global_default_fn_cipher_name[ECRYPTFS_MAX_CIPHER_NAME_SIZE +1 ];char global_default_fnek_sig[ECRYPTFS_SIG_SIZE_HEX +1 ]; +unsigned char global_default_cipher_mode[ECRYPTFS_MAX_CIPHER_NAME_SIZE
Quoted 7 lines
fs/ecryptfs/ecryptfs_kernel.h ecryptfs_dentry_to_lower_path(struct dentry · line 533
Quoted 7 lines
fs/ecryptfs/ecryptfs_kernel.h +1
-0+ + 1]; };/* superblock private data. */ @@ -527,6 +532,53 @@ ecryptfs_dentry_to_lower_path(struct dentry *dentry) return &((struct ecryptfs_dentry_info*)dentry->d_fsdata)->lower_path;
Quoted 17 lines
Quoted 17 lines
fs/ecryptfs/ecryptfs_kernel.h +15
-0} +/** +* Given a cipher and mode strings, the function +* concatenates them to create a new string of +* <cipher>_<mode> format. +*/ +static inline unsigned char *ecryptfs_get_full_cipher ( +unsigned char *cipher,unsigned char *mode, +unsigned char *final,size_t final_size) +{ +memset (final,0 , final_size); + +if (strlen (mode) >0 ) { +snprintf (final, final_size,"%s_%s" , cipher, mode); +return final; + }Quoted 8 lines
fs/ecryptfs/ecryptfs_kernel.h ecryptfs_dentry_to_lower_path(struct dentry *dentry)
Quoted 8 lines
fs/ecryptfs/ecryptfs_kernel.h +8
-0+ +return cipher; +} + +/** +* Given a <cipher>[_<mode>] formatted string, the function +* extracts cipher string and/or mode string. +* Note: the passed cipher and/or mode strings will be null-terminated.
Quoted 24 lines
Quoted 24 lines
fs/ecryptfs/ecryptfs_kernel.h +24
-0+*/ +static inline void ecryptfs_parse_full_cipher ( +char *s,char *cipher,char *mode) +{ +char input[2 *ECRYPTFS_MAX_CIPHER_NAME_SIZE +1 +1 ]; +/* +1 for '_'; +1 for '\0' */ +char *p; +char *input_p = input; + +if (s ==NULL || cipher ==NULL ) +return ; + +memset (input,0 ,sizeof (input)); +strlcpy (input, s,sizeof (input)); + + p =strsep (&input_p,"_" ); +strlcpy (cipher, p,ECRYPTFS_MAX_CIPHER_NAME_SIZE +1 ); + + +/* check if mode is specified */ +if (input_p !=NULL && mode !=NULL ) +strlcpy (mode, input_p,ECRYPTFS_MAX_CIPHER_NAME_SIZE +1 ); +} +Quoted 8 lines
fs/ecryptfs/ecryptfs_kernel.h int ecryptfs_encrypt_and_encode_filename( · line 630
Quoted 8 lines
fs/ecryptfs/ecryptfs_kernel.h +0
-0#define ecryptfs_printk(type, fmt, arg...) \ __ecryptfs_printk(type "%s: " fmt, __func__, ## arg);
__printf(1, 2)fs/ecryptfs/ecryptfs_kernel.h +1
-0@@ -575,6 +627,7 @@ int ecryptfs_encrypt_and_encode_filename( const char *name, size_t name_size);struct dentry *ecryptfs_lower_dentry (struct dentry *this_dentry);void ecryptfs_dump_hex (char *data,int bytes); +void ecryptfs_dump_salt_hex(char *data, int key_size, char*cipher);
Quoted 1 line
fs/ecryptfs/ecryptfs_kernel.h int ecryptfs_encrypt_and_encode_filename( · line 631
Quoted 1 line
fs/ecryptfs/ecryptfs_kernel.h +0
-0int virt_to_scatterlist(const void *addr, int size, structscatterlist
Quoted 3 lines
Quoted 3 lines
fs/ecryptfs/ecryptfs_kernel.h +0
-0*sg, int sg_size);int ecryptfs_compute_root_iv(struct
ecryptfs_crypt_stat
*crypt_stat);Quoted 10 lines
fs/ecryptfs/ecryptfs_kernel.h int ecryptfs_set_f_namelen(long *namelen, long
Quoted 10 lines
fs/ecryptfs/ecryptfs_kernel.h +4
-0@@ -718,4 +771,29 @@ int ecryptfs_set_f_namelen(long *namelen, long lower_namelen,int ecryptfs_derive_iv (char *iv,struct ecryptfs_crypt_stat *crypt_stat,loff_t offset); +void clean_inode_pages (struct address_space *mapping, +pgoff_t start,pgoff_t end); + +void ecryptfs_drop_pagecache_sb(struct super_block *sb, void*unused);
Quoted 25 lines
Quoted 25 lines
fs/ecryptfs/ecryptfs_kernel.h +21
-0+ +void ecryptfs_free_events (void ); + +void ecryptfs_freepage (struct page *page); + +struct ecryptfs_events *get_events (void ); + +size_t ecryptfs_get_salt_size_for_cipher (const char *cipher); + +size_t ecryptfs_get_key_size_to_enc_data ( +struct ecryptfs_crypt_stat *crypt_stat); + +size_t ecryptfs_get_key_size_to_store_key ( +struct ecryptfs_crypt_stat *crypt_stat); + +size_t ecryptfs_get_key_size_to_restore_key (size_t stored_key_size, +const char *cipher); + +bool ecryptfs_check_space_for_salt (const size_t key_size, +const size_t salt_size); +#endif /* #ifndef ECRYPTFS_KERNEL_H */ fs/ecryptfs/events.c new file +0
-0Quoted 8 lines
fs/ecryptfs/events.c line 5
Quoted 8 lines
fs/ecryptfs/events.c new file +5
-0@@ -0,0 +1,361 @@ +/** +* eCryptfs: Linux filesystem encryption layer +* Copyright (c) 2015, The Linux Foundation. All rights reserved. +* +* This program is free software; you can redistribute it and/or modify
Quoted 1 line
fs/ecryptfs/events.c line 6
Quoted 1 line
fs/ecryptfs/events.c new file +1
-0+* it under the terms of the GNU General Public License version 2 and
Quoted 111 lines
Quoted 111 lines
fs/ecryptfs/events.c new file +111
-0+* only version 2 as published by the Free Software Foundation. +* +* This program is distributed in the hope that it will be useful, +* but WITHOUT ANY WARRANTY; without even the implied warranty of +* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +* GNU General Public License for more details. +*/ + +#include <linux/string.h> +#include <linux/ecryptfs.h> +#include <linux/mutex.h> +#include <linux/types.h> +#include <linux/slab.h> +#include <linux/pagemap.h> +#include <linux/random.h> +#include "ecryptfs_kernel.h" + +static DEFINE_MUTEX (events_mutex); +static struct ecryptfs_events *events_ptr; +static int handle; + +void ecryptfs_free_events (void ) +{ +mutex_lock (&events_mutex); +if (events_ptr !=NULL ) { +kfree (events_ptr); + events_ptr =NULL ; + } + +mutex_unlock (&events_mutex); +} + +/** +* Register to ecryptfs events, by passing callback +* functions to be called upon events occurrence. +* The function returns a handle to be passed +* to unregister function. +*/ +int ecryptfs_register_to_events (struct ecryptfs_events *ops) +{ +int ret_value =0 ; + +if (!ops) +return -EINVAL ; + +mutex_lock (&events_mutex); + +if (events_ptr !=NULL ) { +ecryptfs_printk (KERN_ERR , +"already registered!\n" ); + ret_value = -EPERM ; + goto out; + } + events_ptr = +kzalloc (sizeof (struct ecryptfs_events ),GFP_KERNEL ); + +if (!events_ptr) { +ecryptfs_printk (KERN_ERR ,"malloc failure\n" ); + ret_value = -ENOMEM ; + goto out; + } +/* copy the callbacks */ + events_ptr->open_cb = ops->open_cb; + events_ptr->release_cb = ops->release_cb; + events_ptr->encrypt_cb = ops->encrypt_cb; + events_ptr->decrypt_cb = ops->decrypt_cb; + events_ptr->is_cipher_supported_cb = + ops->is_cipher_supported_cb; + events_ptr->is_hw_crypt_cb = ops->is_hw_crypt_cb; + events_ptr->get_salt_key_size_cb = ops->get_salt_key_size_cb; + +get_random_bytes (&handle,sizeof (handle)); + ret_value = handle; + +out: +mutex_unlock (&events_mutex); +return ret_value; +} + +/** +* Unregister from ecryptfs events. +*/ +int ecryptfs_unregister_from_events (int user_handle) +{ +int ret_value =0 ; + +mutex_lock (&events_mutex); + +if (!events_ptr) { + ret_value = -EINVAL ; + goto out; + } +if (user_handle != handle) { + ret_value =ECRYPTFS_INVALID_EVENTS_HANDLE ; + goto out; + } + +kfree (events_ptr); + events_ptr =NULL ; + +out: +mutex_unlock (&events_mutex); +return ret_value; +} + +/** +* This function decides whether the passed file offset +* belongs to ecryptfs metadata or not. +* The caller must pass ecryptfs data, which was received in one +* of the callback invocations. +*/ Quoted 8 lines
fs/ecryptfs/events.c
Quoted 8 lines
fs/ecryptfs/events.c new file +8
-0+bool ecryptfs_is_page_in_metadata (void *data,pgoff_t offset) +{ + +struct ecryptfs_crypt_stat *stat =NULL ; +bool ret = true; + +if (!data) { + ecryptfs_printk(KERN_ERR, "ecryptfs_is_page_in_metadata: invaliddata
Quoted 143 lines
Quoted 143 lines
parameter\n");
fs/ecryptfs/events.c new file +142
-0+ ret = false; + goto end; + } + stat = (struct ecryptfs_crypt_stat *)data; + +if (stat->flags &ECRYPTFS_METADATA_IN_XATTR ) { + ret = false; + goto end; + } + +if (offset >= (stat->metadata_size/PAGE_CACHE_SIZE )) { + ret = false; + goto end; + } +end: +return ret; +} + +/** +* Given two ecryptfs data, the function +* decides whether they are equal. +*/ +inline bool ecryptfs_is_data_equal (void *data1,void *data2) +{ +/* pointer comparison*/ +return data1 == data2; +} + +/** +* Given ecryptfs data, the function +* returns appropriate key size. +*/ +size_t ecryptfs_get_key_size (void *data) +{ + +struct ecryptfs_crypt_stat *stat =NULL ; + +if (!data) +return 0 ; + + stat = (struct ecryptfs_crypt_stat *)data; +return stat->key_size; +} + +/** +* Given ecryptfs data, the function +* returns appropriate salt size. +* +* !!! crypt_stat cipher name and mode must be initialized +*/ +size_t ecryptfs_get_salt_size (void *data) +{ +struct ecryptfs_crypt_stat *stat =NULL ; +unsigned char final[2 *ECRYPTFS_MAX_CIPHER_NAME_SIZE +1 ]; + +if (!data) { +ecryptfs_printk (KERN_ERR , +"ecryptfs_get_salt_size: invalid data parameter\n" ); +return 0 ; + } + + stat = (struct ecryptfs_crypt_stat *)data; +return ecryptfs_get_salt_size_for_cipher ( +ecryptfs_get_full_cipher (stat->cipher, + stat->cipher_mode, + final,sizeof (final))); + +} + +/** +* Given ecryptfs data, the function +* returns appropriate cipher. +*/ +const unsigned char *ecryptfs_get_cipher (void *data) +{ +unsigned char final[2 *ECRYPTFS_MAX_CIPHER_NAME_SIZE +1 ]; +struct ecryptfs_crypt_stat *stat =NULL ; + +if (!data) { +ecryptfs_printk (KERN_ERR , +"ecryptfs_get_cipher: invalid data parameter\n" ); +return NULL ; + } + stat = (struct ecryptfs_crypt_stat *)data; +return ecryptfs_get_full_cipher (stat->cipher, stat->cipher_mode, + final,sizeof (final)); +} + +/** +* Given ecryptfs data, the function +* returns file encryption key. +*/ +const unsigned char *ecryptfs_get_key (void *data) +{ + +struct ecryptfs_crypt_stat *stat =NULL ; + +if (!data) { +ecryptfs_printk (KERN_ERR , +"ecryptfs_get_key: invalid data parameter\n" ); +return NULL ; + } + stat = (struct ecryptfs_crypt_stat *)data; +return stat->key; +} + +/** +* Given ecryptfs data, the function +* returns file encryption salt. +*/ +const unsigned char *ecryptfs_get_salt (void *data) +{ +struct ecryptfs_crypt_stat *stat =NULL ; + +if (!data) { +ecryptfs_printk (KERN_ERR , +"ecryptfs_get_salt: invalid data parameter\n" ); +return NULL ; + } + stat = (struct ecryptfs_crypt_stat *)data; +return stat->key +ecryptfs_get_salt_size (data); +} + +/** +* Returns ecryptfs events pointer +*/ +inline struct ecryptfs_events *get_events (void ) +{ +return events_ptr; +} + +/** +* If external crypto module requires salt in addition to key, +* we store it as part of key array (if there is enough space) +* Checks whether a salt key can fit into array allocated for +* regular key +*/ +bool ecryptfs_check_space_for_salt (const size_t key_size, +const size_t salt_size) +{ +if ((salt_size + key_size) >ECRYPTFS_MAX_KEY_BYTES ) +return false;Quoted 8 lines
Quoted 8 lines
fs/ecryptfs/events.c new file +6
-0+ +return true; +} + +/* +* If there is salt that is used by external crypto module, it is stored
+ * in the same array where regular key is. Salt is going to be usedby
Quoted 1 line
Quoted 1 line
+ * external crypto module only, so for all internal crypto
operations
Quoted 12 lines
Quoted 12 lines
salt
+ * should be ignored.
+ *
+ * Get key size in cases where it is going to be used for data encryption
+ * or for all other general purposes
+ */fs/ecryptfs/events.c new file +5
-0+size_t ecryptfs_get_key_size_to_enc_data ( +struct ecryptfs_crypt_stat *crypt_stat) +{ +if (!crypt_stat) +return 0 ;Quoted 8 lines
Quoted 8 lines
fs/ecryptfs/events.c new file +6
-0+ +return crypt_stat->key_size; +} + +/* +* If there is salt that is used by external crypto module, it is stored
+ * in the same array where regular key is. Salt is going to be usedby
Quoted 1 line
Quoted 1 line
+ * external crypto module only, but we still need to save and
restore
Quoted 6 lines
Quoted 6 lines
it
+ * (in encrypted form) as part of ecryptfs header along with the regular
+ * key.
+ *
+ * Get key size in cases where it is going to be storedpersistently
Quoted 6 lines
Quoted 6 lines
+ *
+ * !!!crypt_statcipher name and mode must be initialized
+ */fs/ecryptfs/events.c new file +3
-0+size_t ecryptfs_get_key_size_to_store_key ( +struct ecryptfs_crypt_stat *crypt_stat) +{Quoted 8 lines
fs/ecryptfs/events.c
Quoted 8 lines
fs/ecryptfs/events.c new file +8
-0+size_t salt_size =0 ; + +if (!crypt_stat) +return 0 ; + + salt_size =ecryptfs_get_salt_size (crypt_stat); + + if (!ecryptfs_check_space_for_salt(crypt_stat->key_size,
salt_size)) {Quoted 2 lines
fs/ecryptfs/events.c
Quoted 2 lines
fs/ecryptfs/events.c new file +2
-0+ ecryptfs_printk(KERN_WARNING, + "ecryptfs_get_key_size_to_store_key: not enough space forsalt\n");
Quoted 10 lines
Quoted 10 lines
fs/ecryptfs/events.c new file +8
-0+return crypt_stat->key_size; + } + +return crypt_stat->key_size + salt_size; +} + +/* +* If there is salt that is used by external crypto module, it is stored
+ * in the same array where regular key is. Salt is going to be usedby
Quoted 1 line
Quoted 1 line
+ * external crypto module only, but we still need to save and
restore
Quoted 6 lines
Quoted 6 lines
it
+ * (in encrypted form) as part of ecryptfs header along with the regular
+ * key.
+ *
+ * Get key size in cases where it is going to be restored fromstorage
Quoted 46 lines
Quoted 46 lines
+ *
+ * !!!crypt_statcipher name and mode must be initialized
+ */fs/ecryptfs/events.c new file +13
-0+size_t ecryptfs_get_key_size_to_restore_key (size_t stored_key_size, +const char *cipher) +{ +size_t salt_size =0 ; + +if (!cipher) +return 0 ; + + salt_size =ecryptfs_get_salt_size_for_cipher (cipher); + +if (salt_size >= stored_key_size) { + ecryptfs_printk(KERN_WARNING, + "ecryptfs_get_key_size_to_restore_key: salt %zu >= stred size%zu\n",
fs/ecryptfs/events.c new file +18
-0+ salt_size, stored_key_size); + +return stored_key_size; + } + +return stored_key_size - salt_size; +} + +/** +* Given cipher, the function returns appropriate salt size. +*/ +size_t ecryptfs_get_salt_size_for_cipher (const char *cipher) +{ +if (!get_events () || !(get_events ()->get_salt_key_size_cb)) +return 0 ; + +return get_events ()->get_salt_key_size_cb (cipher); +}fs/ecryptfs/file.c +1
-0@@ -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" Quoted 2 lines
fs/ecryptfs/file.c static int ecryptfs_open(struct inode *inode,
Quoted 2 lines
fs/ecryptfs/file.c +0
-0/** @@ -184,6 +185,9 @@ static int ecryptfs_open(struct inode *inode,struct
Quoted 46 lines
Quoted 46 lines
fs/ecryptfs/file.c +3
-0file *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() */ fs/ecryptfs/file.c +19
-0struct 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:fs/ecryptfs/file.c +0
-0ecryptfs_put_lower_file (inode);@@ -261,9 +284,22 @@ static int ecryptfs_flush(struct file *file, fl_owner_t td)Quoted 16 lines
Quoted 6 lines
fs/ecryptfs/file.c +4
-0static int ecryptfs_release (struct inode *inode,struct file *file) { + +int ret; + + ret =vfs_fsync (file, false);fs/ecryptfs/file.c static int ecryptfs_flush(struct file *file, fl_owner_t td)
Quoted 10 lines
fs/ecryptfs/file.c +7
-0+ +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);
Quoted 2 lines
fs/ecryptfs/file.c static int ecryptfs_flush(struct file *file, · line 303
Quoted 2 lines
fs/ecryptfs/file.c +2
-0+truncate_inode_pages (inode->i_mapping,0 ); + truncate_inode_pages(ecryptfs_inode_to_lower(inode)->i_mapping,0);
Quoted 22 lines
Quoted 22 lines
fs/ecryptfs/file.c +0
-0return 0 ; }fs/ecryptfs/inode.c +3
-0@@ -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;Quoted 1 line
fs/ecryptfs/inode.c out: · line 275
Quoted 1 line
fs/ecryptfs/inode.c +0
-0ecryptfs_inode = ecryptfs_do_create(directory_inode,
ecryptfs_dentry,Quoted 9 lines
Quoted 9 lines
fs/ecryptfs/inode.c +1
-0mode);@@ -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 */ Quoted 8 lines
fs/ecryptfs/inode.c ecryptfs_create(struct inode *directory_inode, · line 297
Quoted 8 lines
fs/ecryptfs/inode.c +2
-0rc =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;
Quoted 5 lines
Quoted 5 lines
fs/ecryptfs/inode.c +5
-0+if (get_events () &&get_events ()->open_cb) +get_events ()->open_cb ( +ecryptfs_inode_to_lower (ecryptfs_inode), + crypt_stat); +Quoted 8 lines
fs/ecryptfs/keystore.c write_tag_66_packet(char *signature, u8
Quoted 8 lines
fs/ecryptfs/inode.c +0
-0d_instantiate (ecryptfs_dentry, ecryptfs_inode); out:fs/ecryptfs/inode.c +0
-0return rc;fs/ecryptfs/keystore.c +0
-0@@ -315,7 +315,8 @@ write_tag_66_packet(char *signature, u8
cipher_code,Quoted 10 lines
fs/ecryptfs/keystore.c write_tag_66_packet(char *signature, u8
Quoted 10 lines
fs/ecryptfs/keystore.c +2
-1* | File Encryption Key Size | 1 or 2 bytes | * | File Encryption Key | arbitrary | */ - data_len = (5 +ECRYPTFS_SIG_SIZE_HEX + crypt_stat->key_size); + data_len = (5 +ECRYPTFS_SIG_SIZE_HEX + +ecryptfs_get_key_size_to_store_key (crypt_stat)); *packet =kmalloc (data_len,GFP_KERNEL ); message = *packet;if (!message) {@@ -335,8 +336,9 @@ write_tag_66_packet(char *signature, u8
cipher_code,Quoted 3 lines
fs/ecryptfs/keystore.c write_tag_66_packet(char *signature, u8 cipher_code,
Quoted 3 lines
fs/ecryptfs/keystore.c +0
-0memcpy (&message[i], signature,ECRYPTFS_SIG_SIZE_HEX ); i +=ECRYPTFS_SIG_SIZE_HEX ;/* The encrypted key includes 1 byte cipher code and 2 byte checksum
Quoted 2 lines
fs/ecryptfs/keystore.c write_tag_66_packet(char *signature, u8 · line 339
Quoted 2 lines
fs/ecryptfs/keystore.c +0
-1*/ - rc = ecryptfs_write_packet_length(&message[i],
crypt_stat->key_size
+Quoted 12 lines
Quoted 12 lines
fs/ecryptfs/keystore.c +3
-13 , - &packet_size_len); + rc =ecryptfs_write_packet_length (&message[i], +ecryptfs_get_key_size_to_store_key (crypt_stat) +3 , + &packet_size_len);if (rc) {fs/ecryptfs/keystore.c +0
-0ecryptfs_printk (KERN_ERR ,"Error generating tag 66 packet " "header; cannot generate packet length\n" );@@ -344,9 +346,10 @@ write_tag_66_packet(char *signature, u8 cipher_code, } i += packet_size_len;Quoted 8 lines
fs/ecryptfs/keystore.c write_tag_66_packet(char *signature, u8 cipher_code,
Quoted 8 lines
fs/ecryptfs/keystore.c +4
-3message[i++] = cipher_code; -memcpy (&message[i], crypt_stat->key, crypt_stat->key_size); - i += crypt_stat->key_size; -for (j =0 ; j < crypt_stat->key_size; j++) +memcpy (&message[i], crypt_stat->key, +ecryptfs_get_key_size_to_store_key (crypt_stat)); + i +=ecryptfs_get_key_size_to_store_key (crypt_stat); + for (j = 0; j < ecryptfs_get_key_size_to_store_key(crypt_stat);j++)
Quoted 4 lines
fs/ecryptfs/keystore.c ecryptfs_parse_tag_70_packet(char **filename,
Quoted 4 lines
fs/ecryptfs/keystore.c +0
-0checksum += crypt_stat->key[j]; message[i++] = (checksum /256 ) %256 ;fs/ecryptfs/keystore.c +0
-0message[i++] = (checksum %256 );@@ -918,6 +921,7 @@ ecryptfs_parse_tag_70_packet(char **filename,
size_tQuoted 6 lines
Quoted 6 lines
fs/ecryptfs/keystore.c +1
-0*filename_size, struct ecryptfs_parse_tag_70_packet_silly_stack *s;struct key *auth_tok_key =NULL ;int rc =0 ; +char full_cipher[ECRYPTFS_MAX_CIPHER_NAME_SIZE ];Quoted 8 lines
fs/ecryptfs/keystore.c ecryptfs_parse_tag_70_packet(char **filename, · line 981
Quoted 8 lines
fs/ecryptfs/keystore.c +0
-0(*packet_size) =0 ;fs/ecryptfs/keystore.c +0
-1(*filename_size) =0 ;@@ -977,12 +981,13 @@ ecryptfs_parse_tag_70_packet(char **filename,size_t *filename_size, s->fnek_sig_hex[ECRYPTFS_SIG_SIZE_HEX] = '\0'; (*packet_size) +=ECRYPTFS_SIG_SIZE ; s->cipher_code = data[(*packet_size)++]; - rc = ecryptfs_cipher_code_to_string(s->cipher_string,
s->cipher_code);Quoted 17 lines
Quoted 17 lines
fs/ecryptfs/keystore.c +2
-0+ rc =ecryptfs_cipher_code_to_string (full_cipher, s->cipher_code);if (rc) {printk (KERN_WARNING "%s: Cipher code [%d] is invalid\n" , __func__, s->cipher_code); goto out; } +ecryptfs_parse_full_cipher (full_cipher, s->cipher_string,0 ); rc = ecryptfs_find_auth_tok_for_sig(&auth_tok_key, &s->auth_tok, mount_crypt_stat,fs/ecryptfs/keystore.c +1
-0s->fnek_sig_hex);@@ -1151,6 +1156,7 @@ decrypt_pki_encrypted_session_key(struct ecryptfs_auth_tok *auth_tok, char *payload =NULL ;size_t payload_len =0 ;int rc; +char full_cipher[ECRYPTFS_MAX_CIPHER_NAME_SIZE ];Quoted 12 lines
fs/ecryptfs/keystore.c decrypt_pki_encrypted_session_key(struct · line 1192
Quoted 12 lines
fs/ecryptfs/keystore.c +0
-0rc =ecryptfs_get_auth_tok_sig (&auth_tok_sig, auth_tok);fs/ecryptfs/keystore.c +0
-5if (rc) {@@ -1184,21 +1190,31 @@ decrypt_pki_encrypted_session_key(structecryptfs_auth_tok *auth_tok, rc); goto out; } - auth_tok->session_key.flags |=ECRYPTFS_CONTAINS_DECRYPTED_KEY ; -memcpy (crypt_stat->key, auth_tok->session_key.decrypted_key, - auth_tok->session_key.decrypted_key_size); - crypt_stat->key_size = auth_tok->session_key.decrypted_key_size; - rc = ecryptfs_cipher_code_to_string(crypt_stat->cipher,
cipher_code);Quoted 36 lines
Quoted 36 lines
fs/ecryptfs/keystore.c +16
-1+ + rc =ecryptfs_cipher_code_to_string (full_cipher, cipher_code);if (rc) {ecryptfs_printk (KERN_ERR ,"Cipher code [%d] is invalid\n" , cipher_code) - goto out; + goto out; } + + auth_tok->session_key.flags |=ECRYPTFS_CONTAINS_DECRYPTED_KEY ; +memcpy (crypt_stat->key, auth_tok->session_key.decrypted_key, + auth_tok->session_key.decrypted_key_size); + crypt_stat->key_size =ecryptfs_get_key_size_to_restore_key ( + auth_tok->session_key.decrypted_key_size, full_cipher); + +ecryptfs_parse_full_cipher (full_cipher, + crypt_stat->cipher, crypt_stat->cipher_mode); + crypt_stat->flags |=ECRYPTFS_KEY_VALID ;if (ecryptfs_verbosity >0 ) {ecryptfs_printk (KERN_DEBUG ,"Decrypted session key:\n" );ecryptfs_dump_hex (crypt_stat->key, crypt_stat->key_size); + +ecryptfs_dump_salt_hex (crypt_stat->key, crypt_stat->key_size, + full_cipher); } out:fs/ecryptfs/keystore.c +1
-0kfree (msg);@@ -1380,6 +1396,7 @@ parse_tag_3_packet(struct ecryptfs_crypt_stat *crypt_stat, struct ecryptfs_auth_tok_list_item *auth_tok_list_item;size_t length_size;int rc =0 ; +char full_cipher[ECRYPTFS_MAX_CIPHER_NAME_SIZE ];Quoted 3 lines
fs/ecryptfs/keystore.c parse_tag_3_packet(struct
Quoted 3 lines
fs/ecryptfs/keystore.c +0
-0(*packet_size) =0 ;fs/ecryptfs/keystore.c +0
-0(*new_auth_tok) =NULL ;@@ -1453,10 +1470,13 @@ parse_tag_3_packet(struct
ecryptfs_crypt_statQuoted 31 lines
Quoted 31 lines
fs/ecryptfs/keystore.c +4
-1*crypt_stat, rc = -EINVAL ; goto out_free; } - rc =ecryptfs_cipher_code_to_string (crypt_stat->cipher, + rc =ecryptfs_cipher_code_to_string (full_cipher, (u16 )data[(*packet_size)]);if (rc) goto out_free; +ecryptfs_parse_full_cipher (full_cipher, + crypt_stat->cipher, crypt_stat->cipher_mode); +/* A little extra work to differentiate among the AES key * sizes; see RFC2440 */ fs/ecryptfs/keystore.c +4
-1switch (data[(*packet_size)++]) {@@ -1465,7 +1485,10 @@ parse_tag_3_packet(struct ecryptfs_crypt_stat *crypt_stat, break;default : crypt_stat->key_size = - (*new_auth_tok)->session_key.encrypted_key_size; +ecryptfs_get_key_size_to_restore_key ( + (*new_auth_tok)->session_key.encrypted_key_size, + full_cipher); + } rc =ecryptfs_init_crypt_ctx (crypt_stat);fs/ecryptfs/keystore.c +0
-0if (rc)@@ -1664,6 +1687,8 @@ static intdecrypt_passphrase_encrypted_session_key (struct ecryptfs_auth_tok *auth_tok,Quoted 8 lines
fs/ecryptfs/keystore.c
Quoted 8 lines
fs/ecryptfs/keystore.c +2
-0struct ecryptfs_crypt_stat *crypt_stat) { + +unsigned char final[2 *ECRYPTFS_MAX_CIPHER_NAME_SIZE +1 ];struct scatterlist dst_sg[2 ];struct scatterlist src_sg[2 ];fs/ecryptfs/keystore.c +0
-0struct mutex *tfm_mutex;@@ -1713,7 +1738,7 @@decrypt_passphrase_encrypted_session_key(struct
Quoted 10 lines
fs/ecryptfs/keystore.c
Quoted 10 lines
fs/ecryptfs/keystore.c +1
-1ecryptfs_auth_tok *auth_tok,mutex_lock (tfm_mutex ); rc =crypto_blkcipher_setkey ( desc.tfm, auth_tok->token.password.session_key_encryption_key, - crypt_stat->key_size); + auth_tok->token.password.session_key_encryption_key_bytes);if (unlikely (rc <0 )) {mutex_unlock (tfm_mutex);fs/ecryptfs/keystore.c +0
-0printk (KERN_ERR "Error setting key for crypto context\n" );@@ -1736,6 +1761,10 @@decrypt_passphrase_encrypted_session_key(struct
Quoted 29 lines
Quoted 29 lines
fs/ecryptfs/keystore.c +4
-0ecryptfs_auth_tok *auth_tok, crypt_stat->key_size);ecryptfs_dump_hex (crypt_stat->key, crypt_stat->key_size); +ecryptfs_dump_salt_hex (crypt_stat->key, crypt_stat->key_size, +ecryptfs_get_full_cipher (crypt_stat->cipher, + crypt_stat->cipher_mode, + final,sizeof (final))); } out:fs/ecryptfs/keystore.c +7
-4return rc;@@ -1972,12 +2001,17 @@ pki_encrypt_session_key(struct key *auth_tok_key, size_t payload_len = 0;struct ecryptfs_message *msg;int rc; +unsigned char final[2 *ECRYPTFS_MAX_CIPHER_NAME_SIZE +1 ]; rc = write_tag_66_packet(auth_tok->token.private_key.signature, -ecryptfs_code_for_cipher_string ( - crypt_stat->cipher, - crypt_stat->key_size), - crypt_stat, &payload, &payload_len); + ecryptfs_code_for_cipher_string( + ecryptfs_get_full_cipher( + crypt_stat->cipher, + crypt_stat->cipher_mode, + final, sizeof(final)), + ecryptfs_get_key_size_to_enc_data(Quoted 8 lines
fs/ecryptfs/keystore.c write_tag_1_packet(char *dest, size_t · line 2070
Quoted 8 lines
fs/ecryptfs/keystore.c +2
-0+ crypt_stat)), + crypt_stat, &payload, &payload_len);up_write (&(auth_tok_key->sem));key_put (auth_tok_key);fs/ecryptfs/keystore.c +0
-0if (rc) {@@ -2035,7 +2069,7 @@ write_tag_1_packet(char *dest, size_t *remaining_bytes, ecryptfs_from_hex(key_rec->sig,
auth_tok->token.private_key.signature,Quoted 4 lines
fs/ecryptfs/keystore.c write_tag_1_packet(char *dest, size_t *remaining_bytes,
Quoted 4 lines
fs/ecryptfs/keystore.c +1
-1ECRYPTFS_SIG_SIZE); encrypted_session_key_valid =0 ; -for (i =0 ; i < crypt_stat->key_size; i++) + for (i = 0; i < ecryptfs_get_key_size_to_store_key(crypt_stat);i++)
Quoted 78 lines
Quoted 78 lines
fs/ecryptfs/keystore.c +0
-0encrypted_session_key_valid |= auth_tok->session_key.encrypted_key[i];fs/ecryptfs/keystore.c +1
-0if (encrypted_session_key_valid) {@@ -2189,6 +2223,7 @@ write_tag_3_packet(char *dest, size_t *remaining_bytes, u8 cipher_code;size_t packet_size_length;size_t max_packet_size; +unsigned char final[2 *ECRYPTFS_MAX_CIPHER_NAME_SIZE +1 ];struct ecryptfs_mount_crypt_stat *mount_crypt_stat = crypt_stat->mount_crypt_stat;fs/ecryptfs/keystore.c +3
-2struct blkcipher_desc desc = {@@ -2221,13 +2256,14 @@ write_tag_3_packet(char *dest, size_t *remaining_bytes, mount_crypt_stat->global_default_cipher_key_size;if (auth_tok->session_key.encrypted_key_size ==0 ) auth_tok->session_key.encrypted_key_size = - crypt_stat->key_size; +ecryptfs_get_key_size_to_store_key (crypt_stat);if (crypt_stat->key_size ==24 &&strcmp ("aes" , crypt_stat->cipher) ==0 ) {memset ((crypt_stat->key +24 ),0 ,8 ); auth_tok->session_key.encrypted_key_size =32 ; }else - auth_tok->session_key.encrypted_key_size = crypt_stat->key_size; + auth_tok->session_key.encrypted_key_size = +ecryptfs_get_key_size_to_store_key (crypt_stat); key_rec->enc_key_size = auth_tok->session_key.encrypted_key_size;fs/ecryptfs/keystore.c +2
-2encrypted_session_key_valid =0 ;@@ -2251,8 +2287,8 @@ write_tag_3_packet(char *dest, size_t *remaining_bytes, auth_tok->token.password. session_key_encryption_key_bytes);memcpy (session_key_encryption_key, - auth_tok->token.password.session_key_encryption_key, - crypt_stat->key_size); + auth_tok->token.password.session_key_encryption_key, + auth_tok->token.password.session_key_encryption_key_bytes);ecryptfs_printk (KERN_DEBUG ,"Cached session key encryption key:\n" );fs/ecryptfs/keystore.c +1
-1if (ecryptfs_verbosity >0 )@@ -2285,7 +2321,7 @@ write_tag_3_packet(char *dest, size_t *remaining_bytes, }mutex_lock (tfm_mutex); rc =crypto_blkcipher_setkey (desc.tfm, session_key_encryption_key, - crypt_stat->key_size); + auth_tok->token.password.session_key_encryption_key_bytes);if (rc <0 ) {mutex_unlock (tfm_mutex);fs/ecryptfs/keystore.c +6
-1ecryptfs_printk(KERN_ERR, "Error setting key for crypto "@@ -2294,7 +2330,12 @@ write_tag_3_packet(char *dest, size_t *remaining_bytes, } rc =0 ;ecryptfs_printk (KERN_DEBUG ,"Encrypting [%zd] bytes of the key\n" , - crypt_stat->key_size); + crypt_stat->key_size); +ecryptfs_printk (KERN_DEBUG ,"Encrypting [%zd] bytes of the salt key\n", +ecryptfs_get_salt_size_for_cipher ( +ecryptfs_get_full_cipher (crypt_stat->cipher, + crypt_stat->cipher_mode, + final,sizeof (final)))); rc = crypto_blkcipher_encrypt(&desc, dst_sg, src_sg,fs/ecryptfs/keystore.c +4
-2(*key_rec).enc_key_size);mutex_unlock (tfm_mutex);@@ -2343,8 +2384,10 @@ encrypted_session_key_set: dest[(*packet_size)++] =0x04 ;/* version 4 */ /* TODO: Break from RFC2440 so that arbitrary ciphers can be * specified with strings */ - cipher_code =ecryptfs_code_for_cipher_string (crypt_stat->cipher, - crypt_stat->key_size); + cipher_code =ecryptfs_code_for_cipher_string ( +ecryptfs_get_full_cipher (crypt_stat->cipher, + crypt_stat->cipher_mode, final,sizeof (final)), + crypt_stat->key_size);Quoted 8 lines
fs/ecryptfs/main.c void ecryptfs_put_lower_file(struct inode
Quoted 8 lines
fs/ecryptfs/keystore.c +0
-0if (cipher_code ==0 ) {ecryptfs_printk (KERN_WARNING ,"Unable to generate code for " "cipher [%s]\n" , crypt_stat->cipher);fs/ecryptfs/main.c +0
-0@@ -165,7 +165,13 @@ void ecryptfs_put_lower_file(struct inode*inode)
Quoted 107 lines
Quoted 107 lines
fs/ecryptfs/main.c +7
-0fput (inode_info->lower_file); inode_info->lower_file =NULL ;mutex_unlock (&inode_info->lower_file_mutex); + +if (get_events () &&get_events ()->release_cb) +get_events ()->release_cb ( +ecryptfs_inode_to_lower (inode)); } + + }enum { ecryptfs_opt_sig, ecryptfs_opt_ecryptfs_sig,@@ -266,6 +272,7 @@ static int ecryptfs_parse_options(struct ecryptfs_sb_info *sbi, char *options, int cipher_key_bytes_set = 0;int fn_cipher_key_bytes;int fn_cipher_key_bytes_set =0 ; +size_t salt_size =0 ;struct ecryptfs_mount_crypt_stat *mount_crypt_stat = &sbi->mount_crypt_stat;fs/ecryptfs/main.c +8
-5substring_t args[MAX_OPT_ARGS ];@@ -280,6 +287,7 @@ static int ecryptfs_parse_options(struct ecryptfs_sb_info *sbi, char *options, char *cipher_key_bytes_src;char *fn_cipher_key_bytes_src;u8 cipher_code; +unsigned char final[2 *ECRYPTFS_MAX_CIPHER_NAME_SIZE +1 ]; *check_ruid =0 ;@@ -309,12 +317,14 @@ static int ecryptfs_parse_options(struct ecryptfs_sb_info *sbi, char *options, case ecryptfs_opt_ecryptfs_cipher: cipher_name_src = args[0 ].from; cipher_name_dst = - mount_crypt_stat-> - global_default_cipher_name; -strncpy (cipher_name_dst, cipher_name_src, -ECRYPTFS_MAX_CIPHER_NAME_SIZE ); - cipher_name_dst[ECRYPTFS_MAX_CIPHER_NAME_SIZE ] ='\0' ; + mount_crypt_stat->global_default_cipher_name; + +ecryptfs_parse_full_cipher (cipher_name_src, + mount_crypt_stat->global_default_cipher_name, + mount_crypt_stat->global_default_cipher_mode); + cipher_name_set =1 ; +break ;case ecryptfs_opt_ecryptfs_key_bytes:fs/ecryptfs/main.c +31
-5cipher_key_bytes_src = args[0 ].from;@@ -411,24 +421,50 @@ static int ecryptfs_parse_options(struct ecryptfs_sb_info *sbi, char *options,strcpy (mount_crypt_stat->global_default_cipher_name,ECRYPTFS_DEFAULT_CIPHER ); } +if ((mount_crypt_stat->flags &ECRYPTFS_GLOBAL_ENCRYPT_FILENAMES ) && !fn_cipher_name_set)strcpy (mount_crypt_stat->global_default_fn_cipher_name, mount_crypt_stat->global_default_cipher_name); -if (!cipher_key_bytes_set) + +if (cipher_key_bytes_set) { + + salt_size =ecryptfs_get_salt_size_for_cipher ( +ecryptfs_get_full_cipher ( + mount_crypt_stat->global_default_cipher_name, + mount_crypt_stat->global_default_cipher_mode, + final,sizeof (final))); + +if (!ecryptfs_check_space_for_salt ( + mount_crypt_stat->global_default_cipher_key_size, + salt_size)) { +ecryptfs_printk ( +KERN_WARNING , +"eCryptfs internal error: no space for salt" ); + } + }else mount_crypt_stat->global_default_cipher_key_size =0 ; +if ((mount_crypt_stat->flags &ECRYPTFS_GLOBAL_ENCRYPT_FILENAMES ) && !fn_cipher_key_bytes_set) mount_crypt_stat->global_default_fn_cipher_key_bytes = mount_crypt_stat->global_default_cipher_key_size; cipher_code =ecryptfs_code_for_cipher_string ( - 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)), mount_crypt_stat->global_default_cipher_key_size);if (!cipher_code) { -ecryptfs_printk (KERN_ERR , -"eCryptfs doesn't support cipher: %s" , - mount_crypt_stat->global_default_cipher_name); +ecryptfs_printk ( +KERN_ERR , +"eCryptfs doesn't support cipher: %s and key size %zu" , +ecryptfs_get_full_cipher ( + mount_crypt_stat->global_default_cipher_name, + mount_crypt_stat->global_default_cipher_mode, + final,sizeof (final)), + mount_crypt_stat->global_default_cipher_key_size); rc = -EINVAL ;Quoted 8 lines
fs/ecryptfs/main.c static struct file_system_type ecryptfs_fs_type; · line 528
Quoted 8 lines
fs/ecryptfs/main.c +0
-0goto out;fs/ecryptfs/main.c +1
-0}@@ -488,6 +524,7 @@ static struct file_system_type ecryptfs_fs_type;* @dev_name: The path to mount over * @raw_data: The options passed into the kernel */ + static struct dentry *ecryptfs_mount(struct file_system_type
*fs_type,Quoted 21 lines
Quoted 21 lines
fs/ecryptfs/main.c +0
-0int flags, const char *dev_name, void *raw_data){
fs/ecryptfs/main.c +0
-0@@ -557,6 +594,8 @@ static struct dentry *ecryptfs_mount(struct
file_system_type*fs_type, int flagsfs/ecryptfs/main.c +2
-0ecryptfs_set_superblock_lower (s, path.dentry->d_sb); +ecryptfs_drop_pagecache_sb (ecryptfs_superblock_to_lower (s),NULL ); +/** * Set the POSIX ACL flag based on whether they're enabled in the lower
fs/ecryptfs/main.c +1
-0* mount. @@ -894,6 +933,7 @@ static void __exit ecryptfs_exit(void)do_sysfs_unregistration ();unregister_filesystem (&ecryptfs_fs_type);ecryptfs_free_kmem_caches (); +ecryptfs_free_events (); }Quoted 6 lines
fs/ecryptfs/mmap.c static sector_t ecryptfs_bmap(struct
Quoted 6 lines
fs/ecryptfs/main.c +0
-0MODULE_AUTHOR ("Michael A. Halcrow <[email protected]>" );fs/ecryptfs/mmap.c +0
-0@@ -552,10 +552,16 @@ static sector_t ecryptfs_bmap(struct
address_spaceQuoted 14 lines
Quoted 14 lines
fs/ecryptfs/mmap.c +5
-0*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,Quoted 8 lines
fs/ecryptfs/super.c static void ecryptfs_i_callback(struct rcu_head
Quoted 8 lines
fs/ecryptfs/mmap.c +1
-0.bmap = ecryptfs_bmap, + .freepage = ecryptfs_freepage,};
fs/ecryptfs/super.c +0
-0@@ -69,6 +69,9 @@ static void ecryptfs_i_callback(struct rcu_head*head)
Quoted 22 lines
Quoted 22 lines
fs/ecryptfs/super.c +6
-0{ 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); + }Quoted 2 lines
fs/ecryptfs/super.c static int ecryptfs_show_options(struct seq_file
Quoted 2 lines
/**
fs/ecryptfs/super.c +0
-0@@ -149,6 +155,9 @@ static int ecryptfs_show_options(struct seq_file*m,
Quoted 11 lines
fs/ecryptfs/super.c static int ecryptfs_show_options(struct
Quoted 11 lines
fs/ecryptfs/super.c +3
-0struct 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);fs/ecryptfs/super.c +0
-0list_for_each_entry(walker,@@ -162,7 +171,10 @@ static int ecryptfs_show_options(struct
seq_fileQuoted 71 lines
Quoted 71 lines
fs/ecryptfs/super.c +4
-1*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)fs/ecryptfs/super.c +0
-0seq_printf(m, ",ecryptfs_key_bytes=%zd",include/linux/ecryptfs.h +14
-0@@ -1,6 +1,9 @@#ifndef _LINUX_ECRYPTFS_H#define _LINUX_ECRYPTFS_H +struct inode ; +struct page ; +/* Version verification for shared data structures w/ userspace */ #define ECRYPTFS_VERSION_MAJOR 0x00#define ECRYPTFS_VERSION_MINOR 0x04@@ -41,6 +44,7 @@#define RFC2440_CIPHER_AES_256 0x09#define RFC2440_CIPHER_TWOFISH 0x0a#define RFC2440_CIPHER_CAST_6 0x0b +#define RFC2440_CIPHER_AES_XTS_256 0x0c#define RFC2440_CIPHER_RSA 0x01@@ -102,4 +106,47 @@ struct ecryptfs_auth_tok { } token; } __attribute__ ((packed)); +#define ECRYPTFS_INVALID_EVENTS_HANDLE -1 + +/** +* ecryptfs_events struct represents a partial interface +* towards ecryptfs module. If registered to ecryptfs events, +* one can receive push notifications. +* A first callback received from ecryptfs will probably be +* about file opening (open_cb), +* in which ecryptfs passes its ecryptfs_data for future usage. +* This data represents a file and must be passed in every query functions
+ * such asecryptfs_get_key_size(),ecryptfs_get_cipher()etc.
+ */include/linux/ecryptfs.h +20
-0+struct ecryptfs_events { +bool (*is_cipher_supported_cb)(const char *cipher); +void (*open_cb)(struct inode *inode,void *ecrytpfs_data); +void (*release_cb)(struct inode *inode); +int (*encrypt_cb)(struct page *in_page,struct page *out_page, +struct inode *inode,unsigned long extent_offset); +int (*decrypt_cb)(struct page *in_page,struct page *out_page, +struct inode *inode,unsigned long extent_offset); +bool (*is_hw_crypt_cb)(void ); +size_t (*get_salt_key_size_cb)(const char *cipher); +}; + + +int ecryptfs_register_to_events (struct ecryptfs_events *ops); + +int ecryptfs_unregister_from_events (int user_handle); + +const unsigned char *ecryptfs_get_key (void *ecrytpfs_data); + +size_t ecryptfs_get_key_size (void *ecrytpfs_data);Quoted 8 lines
include/linux/ecryptfs.h struct ecryptfs_auth_tok {
Quoted 8 lines
include/linux/ecryptfs.h +8
-0+ +const unsigned char *ecryptfs_get_salt (void *ecrytpfs_data); + +size_t ecryptfs_get_salt_size (void *ecrytpfs_data); + +const unsigned char *ecryptfs_get_cipher (void *ecrytpfs_data); + +bool ecryptfs_is_page_in_metadata(void *ecrytpfs_data, pgoff_toffset);
Quoted 12 lines
Quoted 11 lines
include/linux/ecryptfs.h +2
-0+ +bool ecryptfs_is_data_equal(void *ecrytpfs_data1, void
*ecrytpfs_data2);
+
#endif /*_LINUX_ECRYPTFS_H*/
--
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--
To unsubscribe from this list: send the line "unsubscribe ecryptfs" in the body of a message to [email protected]
More majordomo info at http://vger.kernel.org/majordomo-info.html
Quoted 4 lines · On 2015-11-10 15:20:59, [email protected] wrote:
This is a hardware inline accelerator, meaning that it operates on much lower layer, block layer and device driver layer. The HW encrypts plain requests sent from block layer directly, thus doing it much more efficiently rather than using crypto API.
I feel like basing this on eCryptfs is an odd choice. The overhead of setting up an eCryptfs mount (and requiring CAP_SYS_ADMIN) and the duplication of page cache and dentry cache entries (for upper and lower caches) seems like considerable baggage for such a nifty, new hardware feature.
Quoted 10 lines
In order to use such HW efficiently with eCryptfs, eCryptfs encryption has to be canceled and it will need to call for external module instead that will do the correct marking for the blocks to distinguish between those that need to be encrypted by the HW from those that do not need.
We are considering posting the code that will call
ecryptfs_register_to_events()as a separate patch, but haven't done so yet. It is HW specific.
Currently we are only proposing framework change so that it can allow for external modules to be connected
In that case, I cannot accept this patch. I will have no way to test the external module functionality and will not be able to make changes to that area of the code because it will not be possible for me to fix anything that I've broken. I won't even be able to know if I've broken anything.
If you are able to upstream the external module code, then I'll do a proper review of this patch. I should note that I've only glanced at the patch but if feels like it should be broken up into smaller, easier to review patches before it can be properly reviewed.
Tyler
Quoted 1,862 lines
Quoted 1,861 lines · On 2015-11-09 20:56:02, [email protected] wrote:
Quoted 5 lines
Hello, Tyler
I'll try to provide more detailed explanation, should it be satisfactory enough I will update the patch description.
Quoted 10 lines
The problem with current eCryptfs is that it has total control on how and
when the encryption is performed and this control can't be altered. One example when this can be a problem is when we want to utilize an underlying inline HW encryption engine which allows encrypting blocks 'on
the fly' as they are being written to the storage. In such a case relevant
blocks just need to be marked as 'should be encrypted'. No actual encryption should be done by eCryptfs as it will be much slower.Is this a hardware crypto accelerator? If so, why not create a crypto api driver so all subsystems can take advantage of the acceleration instead of baking support into individual subsystems?
Quoted 9 lines
The provided framework allows transferring this control (if needed) to some external module which will do the encryption itfelf or just mark the
appropriate blocks.There is no caller for
ecryptfs_register_to_events()since this change only provides framework, it doesn't provide the module itself, the module
could be HW dependent.Will the code that you plan to call
ecryptfs_register_to_events()be upstream? If so, have you posted it?Tyler
Quoted 1,823 lines
Regarding the mounting option, it merely serves as example of new cipher mode that can be served by registered module.
There is a special callback function that should be implemented by the registered module that tells whether a particular cipher is supported by it :is_cipher_supported_cb()The mounting option itself is not necessary, I can remove it
Quoted 10 lines
Hello Andrey!
Quoted 7 lines · Andrey Markovytch
From: Andrey Markovytch <[email protected]>
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
Quoted 2 lines
Quoted 2 lines
the inline HW engine.
Additional cipher option was introduced to support the HW/externalmode
Quoted 4 lines
Quoted 2 lines
under that name of "aes-xts". If no external module has registered to support this cipher, eCryptfs will fall back to the usual "aes"
What is "HW/external mode"? There's no description in the commit
message
Quoted 1 line
and
ecryptfs_register_to_events()does not have a caller so I'm notsure
Quoted 3 lines
of the purpose. Please provide more context.
Despite not yet understanding the purpose of this patch, I think that
I
Quoted 1 line
can safely say that "aes-xts" is not an appropriate mount option to
use
Quoted 42 lines
when enabling this mode. eCryptfs may support XTS mode one day, using the Crypto API, so "HW/external mode" should not own the mount option.
Tyler
Quoted 37 lines
Signed-off-by Lina Zarivach <[email protected]>Signed-off-by Andrey Markovytch <[email protected]>--- 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.cfs/ecryptfs/Makefile +2
-2@@ -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.oQuoted 11 lines
fs/ecryptfs/caches_utils.c line 4
Quoted 11 lines
ecryptfs-$(CONFIG_ECRYPT_FS_MESSAGING) += messaging.o miscdev.o
fs/ecryptfs/caches_utils.c new file +4
-0@@ -0,0 +1,78 @@ +/* +* Copyright (c) 2015, The Linux Foundation. All rights reserved. +* +* This program is free software; you can redistribute it and/or modify
Quoted 1 line
fs/ecryptfs/caches_utils.c line 5
Quoted 1 line
fs/ecryptfs/caches_utils.c new file +1
-0+* it under the terms of the GNU General Public License version 2 and
Quoted 9 lines
Quoted 9 lines
fs/ecryptfs/caches_utils.c new file +9
-0+* only version 2 as published by the Free Software Foundation. +* +* This program is distributed in the hope that it will be useful, +* but WITHOUT ANY WARRANTY; without even the implied warranty of +* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +* GNU General Public License for more details. +*/ + +#include <linux/kernel.h> Quoted 8 lines
fs/ecryptfs/caches_utils.c
Quoted 8 lines
fs/ecryptfs/caches_utils.c new file +8
-0+#include <linux/fs.h> +#include <linux/spinlock.h> +#include <linux/pagemap.h> +#include <linux/pagevec.h> + +#include "../internal.h" + +void ecryptfs_drop_pagecache_sb(struct super_block *sb, void*unused)
Quoted 279 lines
Quoted 279 lines
fs/ecryptfs/caches_utils.c new file +56
-0+{ +struct inode *inode, *toput_inode =NULL ; + +spin_lock (&sb->s_inode_list_lock); +list_for_each_entry (inode, &sb->s_inodes, i_sb_list) { +spin_lock (&inode->i_lock); +if ((inode->i_state & (I_FREEING |I_WILL_FREE |I_NEW )) || + (inode->i_mapping->nrpages ==0 )) { +spin_unlock (&inode->i_lock); +continue ; + } +__iget (inode); +spin_unlock (&inode->i_lock); +spin_unlock (&sb->s_inode_list_lock); + +invalidate_mapping_pages (inode->i_mapping,0 ,-1 ); +iput (toput_inode); + toput_inode = inode; + +spin_lock (&sb->s_inode_list_lock); + } +spin_unlock (&sb->s_inode_list_lock); +iput (toput_inode); +} + +void clean_inode_pages (struct address_space *mapping, +pgoff_t start,pgoff_t end) +{ +struct pagevec pvec; +pgoff_t index = start; +int i; + +pagevec_init (&pvec,0 ); +while (index <= end &&pagevec_lookup (&pvec, mapping, index, +min (end - index, + (pgoff_t )PAGEVEC_SIZE -1 ) +1 )) { +for (i =0 ; i <pagevec_count (&pvec); i++) { +struct page *page = pvec.pages[i]; + +/* We rely upon deletion +* not changing page->index +*/ + index = page->index; +if (index > end) +break ; +if (!trylock_page (page)) +continue ; +WARN_ON (page->index != index); +zero_user (page,0 ,PAGE_CACHE_SIZE ); +unlock_page (page); + } +pagevec_release (&pvec); +cond_resched (); + index++; + } +}fs/ecryptfs/crypto.c +3
-2@@ -35,6 +35,7 @@#include <linux/scatterlist.h> #include <linux/slab.h> #include <asm/unaligned.h> +#include <linux/ecryptfs.h> #include "ecryptfs_kernel.h" #define DECRYPT 0@@ -350,9 +351,9 @@ static int crypt_scatterlist(struct ecryptfs_crypt_stat *crypt_stat, || !(crypt_stat->flags & ECRYPTFS_STRUCT_INITIALIZED));if (unlikely (ecryptfs_verbosity >0 )) {ecryptfs_printk (KERN_DEBUG ,"Key size [%zd]; key:\n" , - crypt_stat->key_size); +ecryptfs_get_key_size_to_enc_data (crypt_stat));ecryptfs_dump_hex (crypt_stat->key, - crypt_stat->key_size); +ecryptfs_get_key_size_to_enc_data (crypt_stat)); }fs/ecryptfs/crypto.c +1
-1init_completion (&ecr.completion);@@ -371,7 +372,7 @@ static int crypt_scatterlist(struct ecryptfs_crypt_stat *crypt_stat,/* Consider doing this once, when the file is opened */ if (!(crypt_stat->flags & ECRYPTFS_KEY_SET)) { rc =crypto_ablkcipher_setkey (crypt_stat->tfm, crypt_stat->key, - crypt_stat->key_size); +ecryptfs_get_key_size_to_enc_data (crypt_stat));if (rc) { ecryptfs_printk(KERN_ERR,fs/ecryptfs/crypto.c +25
-0"Error setting key; rc = [%d]\n",@@ -466,6 +467,31 @@ out:return rc; } +static void init_ecryption_parameters (bool *hw_crypt,bool *cipher_supported, +struct ecryptfs_crypt_stat *crypt_stat) +{ +unsigned char final[2 *ECRYPTFS_MAX_CIPHER_NAME_SIZE +1 ]; + +if (!hw_crypt || !cipher_supported) +return ; + + *cipher_supported = false; + *hw_crypt = false; + +if (get_events () &&get_events ()->is_cipher_supported_cb) { + *cipher_supported = +get_events ()->is_cipher_supported_cb ( +ecryptfs_get_full_cipher (crypt_stat->cipher, + crypt_stat->cipher_mode, final,sizeof (final))); +if (*cipher_supported) { +/* we should apply external algorythm +* assume that is_hw_crypt() cbck is supplied +*/ + *hw_crypt =get_events ()->is_hw_crypt_cb (); + } + } +} +/** * ecryptfs_encrypt_page * @page: Page mapped from the eCryptfs inode for the file; contains
fs/ecryptfs/crypto.c +47
-13@@ -491,11 +517,18 @@ int ecryptfs_encrypt_page(struct page *page)loff_t extent_offset;loff_t lower_offset;int rc =0 ; +bool is_hw_crypt; +bool is_cipher_supported; + ecryptfs_inode = page->mapping->host; crypt_stat = &(ecryptfs_inode_to_private (ecryptfs_inode)->crypt_stat);BUG_ON (!(crypt_stat->flags &ECRYPTFS_ENCRYPTED )); + +init_ecryption_parameters (&is_hw_crypt, + &is_cipher_supported, crypt_stat); + enc_extent_page =alloc_page (GFP_USER );if (!enc_extent_page) { rc = -ENOMEM ;@@ -503,24 +536,51 @@ int ecryptfs_encrypt_page(struct page *page) "encrypted extent\n"); goto out; } - -for (extent_offset =0 ; - extent_offset < (PAGE_CACHE_SIZE / crypt_stat->extent_size); - extent_offset++) { - rc =crypt_extent (crypt_stat, enc_extent_page, page, - extent_offset,ENCRYPT ); -if (rc) { -printk (KERN_ERR "%s: Error encrypting extent; " -"rc = [%d]\n" , __func__, rc); - goto out; - } +if (is_hw_crypt) { +/* no need for encryption */ + }else { +for (extent_offset =0 ; + extent_offset < + (PAGE_CACHE_SIZE / crypt_stat->extent_size); + extent_offset++) { + +if (is_cipher_supported) { +if (!get_events ()->encrypt_cb) { + rc = -EPERM ; + goto out; + } + rc =get_events ()->encrypt_cb (page, + enc_extent_page, +ecryptfs_inode_to_lower ( + ecryptfs_inode), + extent_offset); + }else { + rc =crypt_extent (crypt_stat, + enc_extent_page, page, + extent_offset,ENCRYPT ); + } +if (rc) { +ecryptfs_printk (KERN_ERR , +"%s: Error encrypting; rc = [%d]\n" , + __func__, rc); + goto out; + } + } } lower_offset =lower_offset_for_page (crypt_stat, page); - enc_extent_virt =kmap (enc_extent_page); +if (is_hw_crypt) + enc_extent_virt =kmap (page); +else + enc_extent_virt =kmap (enc_extent_page); + rc =ecryptfs_write_lower (ecryptfs_inode, enc_extent_virt, lower_offset,PAGE_CACHE_SIZE ); -kunmap (enc_extent_page); +if (!is_hw_crypt) +kunmap (enc_extent_page); +else +kunmap (page); +if (rc <0 ) { ecryptfs_printk(KERN_ERR,fs/ecryptfs/crypto.c +25
-3"Error attempting to write lower page; rc = [%d]\n",@@ -559,6 +619,8 @@ int ecryptfs_decrypt_page(struct page *page)unsigned long extent_offset;loff_t lower_offset;int rc =0 ; +bool is_cipher_supported; +bool is_hw_crypt; ecryptfs_inode = page->mapping->host; crypt_stat =@@ -577,13 +639,33 @@ int ecryptfs_decrypt_page(struct page *page) goto out; } +init_ecryption_parameters (&is_hw_crypt, + &is_cipher_supported, crypt_stat); + +if (is_hw_crypt) { + rc =0 ; +return rc; + } +for (extent_offset =0 ; extent_offset < (PAGE_CACHE_SIZE / crypt_stat->extent_size); extent_offset++) { - rc =crypt_extent (crypt_stat, page, page, +if (is_cipher_supported) { +if (!get_events ()->decrypt_cb) { + rc = -EPERM ; + goto out; + } + + rc =get_events ()->decrypt_cb (page, page, +ecryptfs_inode_to_lower (ecryptfs_inode), + extent_offset); + + }else + rc =crypt_extent (crypt_stat, page, page, extent_offset,DECRYPT ); +if (rc) { -printk (KERN_ERR "%s: Error encrypting extent; " +ecryptfs_printk (KERN_ERR ,"%s: Error decrypting extent;" "rc = [%d]\n" , __func__, rc); goto out; }@@ -612,7 +694,7 @@ int ecryptfs_init_crypt_ctx(struct ecryptfs_crypt_stat *crypt_stat)"Initializing cipher [%s]; strlen = [%d]; " "key_size_bits = [%zd]\n" , crypt_stat->cipher, (int )strlen (crypt_stat->cipher), - crypt_stat->key_size << 3); + ecryptfs_get_key_size_to_enc_data(crypt_stat) << 3);mutex_lock (&crypt_stat->cs_tfm_mutex);if (crypt_stat->tfm) {fs/ecryptfs/crypto.c +1
-1rc =0 ;@@ -694,7 +776,7 @@ int ecryptfs_compute_root_iv(struct ecryptfs_crypt_stat *crypt_stat) goto out; } rc =ecryptfs_calculate_md5 (dst, crypt_stat, crypt_stat->key, - crypt_stat->key_size); +ecryptfs_get_key_size_to_enc_data (crypt_stat));if (rc) { ecryptfs_printk(KERN_WARNING, "Error attempting to compute "fs/ecryptfs/crypto.c +0
-0"MD5 while generating root IV\n");@@ -721,6 +803,35 @@ static void ecryptfs_generate_new_key(struct ecryptfs_crypt_stat *crypt_stat) } }Quoted 15 lines
Quoted 5 lines
fs/ecryptfs/crypto.c +4
-0+static int ecryptfs_generate_new_salt (struct ecryptfs_crypt_stat *crypt_stat) +{ +size_t salt_size =0 ; +unsigned char final[2 *ECRYPTFS_MAX_CIPHER_NAME_SIZE +1 ];fs/ecryptfs/crypto.c static void ecryptfs_generate_new_key(struct ecryptfs_crypt_stat *crypt_stat)
Quoted 10 lines
fs/ecryptfs/crypto.c +10
-0+ + salt_size =ecryptfs_get_salt_size_for_cipher ( +ecryptfs_get_full_cipher (crypt_stat->cipher, + crypt_stat->cipher_mode, + final,sizeof (final))); + +if (salt_size ==0 ) +return 0 ; + + if (!ecryptfs_check_space_for_salt(crypt_stat->key_size,
salt_size)) {Quoted 6 lines
fs/ecryptfs/crypto.c static void ecryptfs_generate_new_key(struct ecryptfs_crypt_stat *crypt_stat)
Quoted 6 lines
fs/ecryptfs/crypto.c +6
-0+ecryptfs_printk (KERN_WARNING ,"not enough space for salt\n" ); + crypt_stat->flags |=ECRYPTFS_SECURITY_WARNING ; +return -EINVAL ; + } + + get_random_bytes(crypt_stat->key + crypt_stat->key_size,
salt_size);Quoted 88 lines
Quoted 88 lines
fs/ecryptfs/crypto.c +9
-0+if (unlikely (ecryptfs_verbosity >0 )) { +ecryptfs_printk (KERN_DEBUG ,"Generated new session salt:\n" ); +ecryptfs_dump_hex (crypt_stat->key + crypt_stat->key_size, + salt_size); + } + +return 0 ; +} +/** *
ecryptfs_copy_mount_wide_flags_to_inode_flags
* @crypt_stat: The inode's cryptographic contextfs/ecryptfs/crypto.c +0
-1@@ -823,7 +934,6 @@ int ecryptfs_new_file_context(struct inode *ecryptfs_inode)struct ecryptfs_mount_crypt_stat *mount_crypt_stat = &ecryptfs_superblock_to_private ( ecryptfs_inode->i_sb)->mount_crypt_stat; -int cipher_name_len;int rc =0 ;fs/ecryptfs/crypto.c +9
-5ecryptfs_set_default_crypt_stat_vals (crypt_stat, mount_crypt_stat);@@ -837,15 +947,19 @@ int ecryptfs_new_file_context(struct inode *ecryptfs_inode) "to the inode key sigs; rc = [%d]\n", rc); goto out; } - cipher_name_len = -strlen (mount_crypt_stat->global_default_cipher_name); -memcpy (crypt_stat->cipher, +strlcpy (crypt_stat->cipher, mount_crypt_stat->global_default_cipher_name, - cipher_name_len); - crypt_stat->cipher[cipher_name_len] ='\0' ; +sizeof (crypt_stat->cipher)); + +strlcpy (crypt_stat->cipher_mode, + mount_crypt_stat->global_default_cipher_mode, +sizeof (crypt_stat->cipher_mode)); + crypt_stat->key_size = mount_crypt_stat->global_default_cipher_key_size;ecryptfs_generate_new_key (crypt_stat); +ecryptfs_generate_new_salt (crypt_stat); + rc =ecryptfs_init_crypt_ctx (crypt_stat);if (rc)fs/ecryptfs/crypto.c +7
-1ecryptfs_printk(KERN_ERR, "Error initializing cryptographic "@@ -971,7 +1085,8 @@ ecryptfs_cipher_code_str_map[] = { {"twofish", RFC2440_CIPHER_TWOFISH}, {"cast6", RFC2440_CIPHER_CAST_6}, {"aes", RFC2440_CIPHER_AES_192}, - {"aes", RFC2440_CIPHER_AES_256} + {"aes", RFC2440_CIPHER_AES_256}, + {"aes_xts", RFC2440_CIPHER_AES_XTS_256} };/** @@ -999,6 +1114,11 @@ u8 ecryptfs_code_for_cipher_string(char *cipher_name, size_t key_bytes)case 32 : code =RFC2440_CIPHER_AES_256 ; } + }else if (strcmp (cipher_name,"aes_xts" ) ==0 ) { +switch (key_bytes) { +case 32 : + code =RFC2440_CIPHER_AES_XTS_256 ; + } }else {for (i =0 ; i <ARRAY_SIZE (ecryptfs_cipher_code_str_map); i++)fs/ecryptfs/crypto.c +12
-0if (strcmp (cipher_name, map[i].cipher_str) ==0 ) {@@ -1038,9 +1158,24 @@ int ecryptfs_read_and_validate_header_region(struct inode *inode) u8 file_size[ECRYPTFS_SIZE_AND_MARKER_BYTES];u8 *marker = file_size +ECRYPTFS_FILE_SIZE_BYTES ;int rc; +unsigned int ra_pages_org; +struct file *lower_file =NULL ; + +if (!inode) +return -EIO ; + lower_file =ecryptfs_inode_to_private (inode)->lower_file; +if (!lower_file) +return -EIO ; + +/*disable read a head mechanism for a while */ + ra_pages_org = lower_file->f_ra.ra_pages; + lower_file->f_ra.ra_pages =0 ;Quoted 1 line
fs/ecryptfs/crypto.c int · line 1175
Quoted 1 line
fs/ecryptfs/crypto.c +0
-0rc = ecryptfs_read_lower(file_size, 0,
ECRYPTFS_SIZE_AND_MARKER_BYTES,Quoted 74 lines
Quoted 74 lines
fs/ecryptfs/crypto.c +3
-0inode); + lower_file->f_ra.ra_pages = ra_pages_org; +/* restore read a head mechanism */ +if (rc <ECRYPTFS_SIZE_AND_MARKER_BYTES )return rc >=0 ? -EINVAL : rc;fs/ecryptfs/crypto.c +5
-0rc =ecryptfs_validate_marker (marker);@@ -1430,6 +1565,11 @@ int ecryptfs_read_metadata(struct dentry *ecryptfs_dentry)struct ecryptfs_mount_crypt_stat *mount_crypt_stat = &ecryptfs_superblock_to_private ( ecryptfs_dentry->d_sb)->mount_crypt_stat; +unsigned int ra_pages_org; +struct file *lower_file = +ecryptfs_inode_to_private (ecryptfs_inode)->lower_file; +if (!lower_file) +return -EIO ; ecryptfs_copy_mount_wide_flags_to_inode_flags(crypt_stat,fs/ecryptfs/crypto.c +6
-0mount_crypt_stat);@@ -1441,8 +1581,14 @@ int ecryptfs_read_metadata(struct dentry *ecryptfs_dentry) __func__); goto out; } +/*disable read a head mechanism */ + ra_pages_org = lower_file->f_ra.ra_pages; + lower_file->f_ra.ra_pages =0 ; + rc =ecryptfs_read_lower (page_virt,0 , crypt_stat->extent_size, ecryptfs_inode); + lower_file->f_ra.ra_pages = ra_pages_org;/* restore it back */ +if (rc >=0 ) rc = ecryptfs_read_headers_virt(page_virt, crypt_stat,fs/ecryptfs/crypto.c +0
-0ecryptfs_dentry,fs/ecryptfs/debug.c +13
-0@@ -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 +1
-0@@ -245,6 +245,7 @@ struct ecryptfs_crypt_stat {struct mutex cs_tfm_mutex;struct mutex cs_hash_tfm_mutex;struct mutex cs_mutex; +unsigned char cipher_mode[ECRYPTFS_MAX_CIPHER_NAME_SIZE +1 ]; };/* inode private data. */ @@ -267,6 +268,8 @@ struct ecryptfs_dentry_info { }; };Quoted 4 lines
fs/ecryptfs/ecryptfs_kernel.h struct ecryptfs_dentry_info { · line 274
Quoted 4 lines
fs/ecryptfs/ecryptfs_kernel.h +2
-0+ +/** * ecryptfs_global_auth_tok - A key used to encrypt all new files under
Quoted 7 lines
fs/ecryptfs/ecryptfs_kernel.h struct ecryptfs_mount_crypt_stat { · line 351
Quoted 7 lines
the mountpoint
* @flags: Status flagsfs/ecryptfs/ecryptfs_kernel.h +1
-0@@ -345,6 +348,8 @@ struct ecryptfs_mount_crypt_stat {unsigned char global_default_fn_cipher_name[ECRYPTFS_MAX_CIPHER_NAME_SIZE +1 ];char global_default_fnek_sig[ECRYPTFS_SIG_SIZE_HEX +1 ]; +unsigned char global_default_cipher_mode[ECRYPTFS_MAX_CIPHER_NAME_SIZE
Quoted 7 lines
fs/ecryptfs/ecryptfs_kernel.h ecryptfs_dentry_to_lower_path(struct dentry · line 533
Quoted 7 lines
fs/ecryptfs/ecryptfs_kernel.h +1
-0+ + 1]; };/* superblock private data. */ @@ -527,6 +532,53 @@ ecryptfs_dentry_to_lower_path(struct dentry *dentry) return &((struct ecryptfs_dentry_info*)dentry->d_fsdata)->lower_path;
Quoted 17 lines
Quoted 17 lines
fs/ecryptfs/ecryptfs_kernel.h +15
-0} +/** +* Given a cipher and mode strings, the function +* concatenates them to create a new string of +* <cipher>_<mode> format. +*/ +static inline unsigned char *ecryptfs_get_full_cipher ( +unsigned char *cipher,unsigned char *mode, +unsigned char *final,size_t final_size) +{ +memset (final,0 , final_size); + +if (strlen (mode) >0 ) { +snprintf (final, final_size,"%s_%s" , cipher, mode); +return final; + }Quoted 8 lines
fs/ecryptfs/ecryptfs_kernel.h ecryptfs_dentry_to_lower_path(struct dentry *dentry)
Quoted 8 lines
fs/ecryptfs/ecryptfs_kernel.h +8
-0+ +return cipher; +} + +/** +* Given a <cipher>[_<mode>] formatted string, the function +* extracts cipher string and/or mode string. +* Note: the passed cipher and/or mode strings will be null-terminated.
Quoted 25 lines
Quoted 25 lines
fs/ecryptfs/ecryptfs_kernel.h +24
-0+*/ +static inline void ecryptfs_parse_full_cipher ( +char *s,char *cipher,char *mode) +{ +char input[2 *ECRYPTFS_MAX_CIPHER_NAME_SIZE +1 +1 ]; +/* +1 for '_'; +1 for '\0' */ +char *p; +char *input_p = input; + +if (s ==NULL || cipher ==NULL ) +return ; + +memset (input,0 ,sizeof (input)); +strlcpy (input, s,sizeof (input)); + + p =strsep (&input_p,"_" ); +strlcpy (cipher, p,ECRYPTFS_MAX_CIPHER_NAME_SIZE +1 ); + + +/* check if mode is specified */ +if (input_p !=NULL && mode !=NULL ) +strlcpy (mode, input_p,ECRYPTFS_MAX_CIPHER_NAME_SIZE +1 ); +} + #define ecryptfs_printk(type, fmt, arg...) \Quoted 8 lines
fs/ecryptfs/ecryptfs_kernel.h int ecryptfs_encrypt_and_encode_filename( · line 631
Quoted 8 lines
fs/ecryptfs/ecryptfs_kernel.h +0
-0__ecryptfs_printk(type "%s: " fmt, __func__, ## arg);
__printf(1, 2)fs/ecryptfs/ecryptfs_kernel.h +1
-0@@ -575,6 +627,7 @@ int ecryptfs_encrypt_and_encode_filename( const char *name, size_t name_size);struct dentry *ecryptfs_lower_dentry (struct dentry *this_dentry);void ecryptfs_dump_hex (char *data,int bytes); +void ecryptfs_dump_salt_hex (char *data,int key_size,char *cipher); int virt_to_scatterlist(const void *addr, int size, structscatterlist
Quoted 3 lines
Quoted 3 lines
fs/ecryptfs/ecryptfs_kernel.h +0
-0*sg, int sg_size);int ecryptfs_compute_root_iv(struct
ecryptfs_crypt_stat
*crypt_stat);Quoted 10 lines
fs/ecryptfs/ecryptfs_kernel.h int ecryptfs_set_f_namelen(long *namelen, long
Quoted 10 lines
fs/ecryptfs/ecryptfs_kernel.h +4
-0@@ -718,4 +771,29 @@ int ecryptfs_set_f_namelen(long *namelen, long lower_namelen,int ecryptfs_derive_iv (char *iv,struct ecryptfs_crypt_stat *crypt_stat,loff_t offset); +void clean_inode_pages (struct address_space *mapping, +pgoff_t start,pgoff_t end); + +void ecryptfs_drop_pagecache_sb(struct super_block *sb, void*unused);
Quoted 25 lines
Quoted 25 lines
fs/ecryptfs/ecryptfs_kernel.h +21
-0+ +void ecryptfs_free_events (void ); + +void ecryptfs_freepage (struct page *page); + +struct ecryptfs_events *get_events (void ); + +size_t ecryptfs_get_salt_size_for_cipher (const char *cipher); + +size_t ecryptfs_get_key_size_to_enc_data ( +struct ecryptfs_crypt_stat *crypt_stat); + +size_t ecryptfs_get_key_size_to_store_key ( +struct ecryptfs_crypt_stat *crypt_stat); + +size_t ecryptfs_get_key_size_to_restore_key (size_t stored_key_size, +const char *cipher); + +bool ecryptfs_check_space_for_salt (const size_t key_size, +const size_t salt_size); +#endif /* #ifndef ECRYPTFS_KERNEL_H */ fs/ecryptfs/events.c new file +0
-0Quoted 8 lines
fs/ecryptfs/events.c line 5
Quoted 8 lines
fs/ecryptfs/events.c new file +5
-0@@ -0,0 +1,361 @@ +/** +* eCryptfs: Linux filesystem encryption layer +* Copyright (c) 2015, The Linux Foundation. All rights reserved. +* +* This program is free software; you can redistribute it and/or modify
Quoted 1 line
fs/ecryptfs/events.c line 6
Quoted 1 line
fs/ecryptfs/events.c new file +1
-0+* it under the terms of the GNU General Public License version 2 and
Quoted 111 lines
Quoted 111 lines
fs/ecryptfs/events.c new file +111
-0+* only version 2 as published by the Free Software Foundation. +* +* This program is distributed in the hope that it will be useful, +* but WITHOUT ANY WARRANTY; without even the implied warranty of +* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +* GNU General Public License for more details. +*/ + +#include <linux/string.h> +#include <linux/ecryptfs.h> +#include <linux/mutex.h> +#include <linux/types.h> +#include <linux/slab.h> +#include <linux/pagemap.h> +#include <linux/random.h> +#include "ecryptfs_kernel.h" + +static DEFINE_MUTEX (events_mutex); +static struct ecryptfs_events *events_ptr; +static int handle; + +void ecryptfs_free_events (void ) +{ +mutex_lock (&events_mutex); +if (events_ptr !=NULL ) { +kfree (events_ptr); + events_ptr =NULL ; + } + +mutex_unlock (&events_mutex); +} + +/** +* Register to ecryptfs events, by passing callback +* functions to be called upon events occurrence. +* The function returns a handle to be passed +* to unregister function. +*/ +int ecryptfs_register_to_events (struct ecryptfs_events *ops) +{ +int ret_value =0 ; + +if (!ops) +return -EINVAL ; + +mutex_lock (&events_mutex); + +if (events_ptr !=NULL ) { +ecryptfs_printk (KERN_ERR , +"already registered!\n" ); + ret_value = -EPERM ; + goto out; + } + events_ptr = +kzalloc (sizeof (struct ecryptfs_events ),GFP_KERNEL ); + +if (!events_ptr) { +ecryptfs_printk (KERN_ERR ,"malloc failure\n" ); + ret_value = -ENOMEM ; + goto out; + } +/* copy the callbacks */ + events_ptr->open_cb = ops->open_cb; + events_ptr->release_cb = ops->release_cb; + events_ptr->encrypt_cb = ops->encrypt_cb; + events_ptr->decrypt_cb = ops->decrypt_cb; + events_ptr->is_cipher_supported_cb = + ops->is_cipher_supported_cb; + events_ptr->is_hw_crypt_cb = ops->is_hw_crypt_cb; + events_ptr->get_salt_key_size_cb = ops->get_salt_key_size_cb; + +get_random_bytes (&handle,sizeof (handle)); + ret_value = handle; + +out: +mutex_unlock (&events_mutex); +return ret_value; +} + +/** +* Unregister from ecryptfs events. +*/ +int ecryptfs_unregister_from_events (int user_handle) +{ +int ret_value =0 ; + +mutex_lock (&events_mutex); + +if (!events_ptr) { + ret_value = -EINVAL ; + goto out; + } +if (user_handle != handle) { + ret_value =ECRYPTFS_INVALID_EVENTS_HANDLE ; + goto out; + } + +kfree (events_ptr); + events_ptr =NULL ; + +out: +mutex_unlock (&events_mutex); +return ret_value; +} + +/** +* This function decides whether the passed file offset +* belongs to ecryptfs metadata or not. +* The caller must pass ecryptfs data, which was received in one +* of the callback invocations. +*/ Quoted 8 lines
fs/ecryptfs/events.c
Quoted 8 lines
fs/ecryptfs/events.c new file +8
-0+bool ecryptfs_is_page_in_metadata (void *data,pgoff_t offset) +{ + +struct ecryptfs_crypt_stat *stat =NULL ; +bool ret = true; + +if (!data) { + ecryptfs_printk(KERN_ERR, "ecryptfs_is_page_in_metadata: invaliddata
Quoted 143 lines
Quoted 143 lines
parameter\n");
fs/ecryptfs/events.c new file +142
-0+ ret = false; + goto end; + } + stat = (struct ecryptfs_crypt_stat *)data; + +if (stat->flags &ECRYPTFS_METADATA_IN_XATTR ) { + ret = false; + goto end; + } + +if (offset >= (stat->metadata_size/PAGE_CACHE_SIZE )) { + ret = false; + goto end; + } +end: +return ret; +} + +/** +* Given two ecryptfs data, the function +* decides whether they are equal. +*/ +inline bool ecryptfs_is_data_equal (void *data1,void *data2) +{ +/* pointer comparison*/ +return data1 == data2; +} + +/** +* Given ecryptfs data, the function +* returns appropriate key size. +*/ +size_t ecryptfs_get_key_size (void *data) +{ + +struct ecryptfs_crypt_stat *stat =NULL ; + +if (!data) +return 0 ; + + stat = (struct ecryptfs_crypt_stat *)data; +return stat->key_size; +} + +/** +* Given ecryptfs data, the function +* returns appropriate salt size. +* +* !!! crypt_stat cipher name and mode must be initialized +*/ +size_t ecryptfs_get_salt_size (void *data) +{ +struct ecryptfs_crypt_stat *stat =NULL ; +unsigned char final[2 *ECRYPTFS_MAX_CIPHER_NAME_SIZE +1 ]; + +if (!data) { +ecryptfs_printk (KERN_ERR , +"ecryptfs_get_salt_size: invalid data parameter\n" ); +return 0 ; + } + + stat = (struct ecryptfs_crypt_stat *)data; +return ecryptfs_get_salt_size_for_cipher ( +ecryptfs_get_full_cipher (stat->cipher, + stat->cipher_mode, + final,sizeof (final))); + +} + +/** +* Given ecryptfs data, the function +* returns appropriate cipher. +*/ +const unsigned char *ecryptfs_get_cipher (void *data) +{ +unsigned char final[2 *ECRYPTFS_MAX_CIPHER_NAME_SIZE +1 ]; +struct ecryptfs_crypt_stat *stat =NULL ; + +if (!data) { +ecryptfs_printk (KERN_ERR , +"ecryptfs_get_cipher: invalid data parameter\n" ); +return NULL ; + } + stat = (struct ecryptfs_crypt_stat *)data; +return ecryptfs_get_full_cipher (stat->cipher, stat->cipher_mode, + final,sizeof (final)); +} + +/** +* Given ecryptfs data, the function +* returns file encryption key. +*/ +const unsigned char *ecryptfs_get_key (void *data) +{ + +struct ecryptfs_crypt_stat *stat =NULL ; + +if (!data) { +ecryptfs_printk (KERN_ERR , +"ecryptfs_get_key: invalid data parameter\n" ); +return NULL ; + } + stat = (struct ecryptfs_crypt_stat *)data; +return stat->key; +} + +/** +* Given ecryptfs data, the function +* returns file encryption salt. +*/ +const unsigned char *ecryptfs_get_salt (void *data) +{ +struct ecryptfs_crypt_stat *stat =NULL ; + +if (!data) { +ecryptfs_printk (KERN_ERR , +"ecryptfs_get_salt: invalid data parameter\n" ); +return NULL ; + } + stat = (struct ecryptfs_crypt_stat *)data; +return stat->key +ecryptfs_get_salt_size (data); +} + +/** +* Returns ecryptfs events pointer +*/ +inline struct ecryptfs_events *get_events (void ) +{ +return events_ptr; +} + +/** +* If external crypto module requires salt in addition to key, +* we store it as part of key array (if there is enough space) +* Checks whether a salt key can fit into array allocated for +* regular key +*/ +bool ecryptfs_check_space_for_salt (const size_t key_size, +const size_t salt_size) +{ +if ((salt_size + key_size) >ECRYPTFS_MAX_KEY_BYTES ) +return false;Quoted 8 lines
Quoted 8 lines
fs/ecryptfs/events.c new file +6
-0+ +return true; +} + +/* +* If there is salt that is used by external crypto module, it is stored
+ * in the same array where regular key is. Salt is going to be usedby
Quoted 1 line
Quoted 1 line
+ * external crypto module only, so for all internal crypto
operations
Quoted 12 lines
Quoted 12 lines
salt
+ * should be ignored.
+ *
+ * Get key size in cases where it is going to be used for data encryption
+ * or for all other general purposes
+ */fs/ecryptfs/events.c new file +5
-0+size_t ecryptfs_get_key_size_to_enc_data ( +struct ecryptfs_crypt_stat *crypt_stat) +{ +if (!crypt_stat) +return 0 ;Quoted 8 lines
Quoted 8 lines
fs/ecryptfs/events.c new file +6
-0+ +return crypt_stat->key_size; +} + +/* +* If there is salt that is used by external crypto module, it is stored
+ * in the same array where regular key is. Salt is going to be usedby
Quoted 1 line
Quoted 1 line
+ * external crypto module only, but we still need to save and
restore
Quoted 12 lines
Quoted 12 lines
it
+ * (in encrypted form) as part of ecryptfs header along with the regular
+ * key.
+ *
+ * Get key size in cases where it is going to be stored persistently
+ *
+ * !!!crypt_statcipher name and mode must be initialized
+ */fs/ecryptfs/events.c new file +3
-0+size_t ecryptfs_get_key_size_to_store_key ( +struct ecryptfs_crypt_stat *crypt_stat) +{Quoted 8 lines
fs/ecryptfs/events.c
Quoted 8 lines
fs/ecryptfs/events.c new file +8
-0+size_t salt_size =0 ; + +if (!crypt_stat) +return 0 ; + + salt_size =ecryptfs_get_salt_size (crypt_stat); + + if (!ecryptfs_check_space_for_salt(crypt_stat->key_size,
salt_size)) {Quoted 2 lines
fs/ecryptfs/events.c
Quoted 2 lines
fs/ecryptfs/events.c new file +2
-0+ ecryptfs_printk(KERN_WARNING, + "ecryptfs_get_key_size_to_store_key: not enough space forsalt\n");
Quoted 10 lines
Quoted 10 lines
fs/ecryptfs/events.c new file +8
-0+return crypt_stat->key_size; + } + +return crypt_stat->key_size + salt_size; +} + +/* +* If there is salt that is used by external crypto module, it is stored
+ * in the same array where regular key is. Salt is going to be usedby
Quoted 1 line
Quoted 1 line
+ * external crypto module only, but we still need to save and
restore
Quoted 6 lines
Quoted 6 lines
it
+ * (in encrypted form) as part of ecryptfs header along with the regular
+ * key.
+ *
+ * Get key size in cases where it is going to be restored fromstorage
Quoted 46 lines
Quoted 46 lines
+ *
+ * !!!crypt_statcipher name and mode must be initialized
+ */fs/ecryptfs/events.c new file +13
-0+size_t ecryptfs_get_key_size_to_restore_key (size_t stored_key_size, +const char *cipher) +{ +size_t salt_size =0 ; + +if (!cipher) +return 0 ; + + salt_size =ecryptfs_get_salt_size_for_cipher (cipher); + +if (salt_size >= stored_key_size) { + ecryptfs_printk(KERN_WARNING, + "ecryptfs_get_key_size_to_restore_key: salt %zu >= stred size%zu\n",
fs/ecryptfs/events.c new file +18
-0+ salt_size, stored_key_size); + +return stored_key_size; + } + +return stored_key_size - salt_size; +} + +/** +* Given cipher, the function returns appropriate salt size. +*/ +size_t ecryptfs_get_salt_size_for_cipher (const char *cipher) +{ +if (!get_events () || !(get_events ()->get_salt_key_size_cb)) +return 0 ; + +return get_events ()->get_salt_key_size_cb (cipher); +}fs/ecryptfs/file.c +1
-0@@ -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" Quoted 2 lines
fs/ecryptfs/file.c static int ecryptfs_open(struct inode *inode,
Quoted 2 lines
fs/ecryptfs/file.c +0
-0/** @@ -184,6 +185,9 @@ static int ecryptfs_open(struct inode *inode,struct
Quoted 46 lines
Quoted 46 lines
fs/ecryptfs/file.c +3
-0file *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() */ fs/ecryptfs/file.c +19
-0struct 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:fs/ecryptfs/file.c +0
-0ecryptfs_put_lower_file (inode);@@ -261,9 +284,22 @@ static int ecryptfs_flush(struct file *file, fl_owner_t td)Quoted 16 lines
Quoted 6 lines
fs/ecryptfs/file.c +4
-0static int ecryptfs_release (struct inode *inode,struct file *file) { + +int ret; + + ret =vfs_fsync (file, false);fs/ecryptfs/file.c static int ecryptfs_flush(struct file *file, fl_owner_t td)
Quoted 10 lines
fs/ecryptfs/file.c +7
-0+ +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);
Quoted 24 lines
Quoted 24 lines
fs/ecryptfs/file.c +2
-0+truncate_inode_pages (inode->i_mapping,0 ); +truncate_inode_pages (ecryptfs_inode_to_lower (inode)->i_mapping,0 );return 0 ; }fs/ecryptfs/inode.c +3
-0@@ -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;Quoted 1 line
fs/ecryptfs/inode.c out: · line 275
Quoted 1 line
fs/ecryptfs/inode.c +0
-0ecryptfs_inode = ecryptfs_do_create(directory_inode,
ecryptfs_dentry,Quoted 9 lines
Quoted 9 lines
fs/ecryptfs/inode.c +1
-0mode);@@ -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 */ Quoted 8 lines
fs/ecryptfs/inode.c ecryptfs_create(struct inode *directory_inode, · line 297
Quoted 8 lines
fs/ecryptfs/inode.c +2
-0rc =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;
Quoted 5 lines
Quoted 5 lines
fs/ecryptfs/inode.c +5
-0+if (get_events () &&get_events ()->open_cb) +get_events ()->open_cb ( +ecryptfs_inode_to_lower (ecryptfs_inode), + crypt_stat); +Quoted 8 lines
fs/ecryptfs/keystore.c write_tag_66_packet(char *signature, u8
Quoted 8 lines
fs/ecryptfs/inode.c +0
-0d_instantiate (ecryptfs_dentry, ecryptfs_inode); out:fs/ecryptfs/inode.c +0
-0return rc;fs/ecryptfs/keystore.c +0
-0@@ -315,7 +315,8 @@ write_tag_66_packet(char *signature, u8
cipher_code,Quoted 10 lines
fs/ecryptfs/keystore.c write_tag_66_packet(char *signature, u8
Quoted 10 lines
fs/ecryptfs/keystore.c +2
-1* | File Encryption Key Size | 1 or 2 bytes | * | File Encryption Key | arbitrary | */ - data_len = (5 +ECRYPTFS_SIG_SIZE_HEX + crypt_stat->key_size); + data_len = (5 +ECRYPTFS_SIG_SIZE_HEX + +ecryptfs_get_key_size_to_store_key (crypt_stat)); *packet =kmalloc (data_len,GFP_KERNEL ); message = *packet;if (!message) {@@ -335,8 +336,9 @@ write_tag_66_packet(char *signature, u8
cipher_code,Quoted 3 lines
fs/ecryptfs/keystore.c write_tag_66_packet(char *signature, u8 cipher_code,
Quoted 3 lines
fs/ecryptfs/keystore.c +0
-0memcpy (&message[i], signature,ECRYPTFS_SIG_SIZE_HEX ); i +=ECRYPTFS_SIG_SIZE_HEX ;/* The encrypted key includes 1 byte cipher code and 2 byte checksum
Quoted 2 lines
fs/ecryptfs/keystore.c write_tag_66_packet(char *signature, u8 · line 339
Quoted 2 lines
fs/ecryptfs/keystore.c +0
-1*/ - rc = ecryptfs_write_packet_length(&message[i], crypt_stat->key_size+
Quoted 12 lines
Quoted 12 lines
fs/ecryptfs/keystore.c +3
-13 , - &packet_size_len); + rc =ecryptfs_write_packet_length (&message[i], +ecryptfs_get_key_size_to_store_key (crypt_stat) +3 , + &packet_size_len);if (rc) {fs/ecryptfs/keystore.c +0
-0ecryptfs_printk (KERN_ERR ,"Error generating tag 66 packet " "header; cannot generate packet length\n" );@@ -344,9 +346,10 @@ write_tag_66_packet(char *signature, u8 cipher_code, } i += packet_size_len;Quoted 8 lines
fs/ecryptfs/keystore.c write_tag_66_packet(char *signature, u8 cipher_code,
Quoted 8 lines
fs/ecryptfs/keystore.c +4
-3message[i++] = cipher_code; -memcpy (&message[i], crypt_stat->key, crypt_stat->key_size); - i += crypt_stat->key_size; -for (j =0 ; j < crypt_stat->key_size; j++) +memcpy (&message[i], crypt_stat->key, +ecryptfs_get_key_size_to_store_key (crypt_stat)); + i +=ecryptfs_get_key_size_to_store_key (crypt_stat); + for (j = 0; j < ecryptfs_get_key_size_to_store_key(crypt_stat);j++)
Quoted 4 lines
fs/ecryptfs/keystore.c ecryptfs_parse_tag_70_packet(char **filename,
Quoted 4 lines
fs/ecryptfs/keystore.c +0
-0checksum += crypt_stat->key[j]; message[i++] = (checksum /256 ) %256 ;fs/ecryptfs/keystore.c +0
-0message[i++] = (checksum %256 );@@ -918,6 +921,7 @@ ecryptfs_parse_tag_70_packet(char **filename,
size_tQuoted 6 lines
Quoted 6 lines
fs/ecryptfs/keystore.c +1
-0*filename_size, struct ecryptfs_parse_tag_70_packet_silly_stack *s;struct key *auth_tok_key =NULL ;int rc =0 ; +char full_cipher[ECRYPTFS_MAX_CIPHER_NAME_SIZE ];Quoted 8 lines
fs/ecryptfs/keystore.c ecryptfs_parse_tag_70_packet(char **filename, · line 981
Quoted 8 lines
fs/ecryptfs/keystore.c +0
-0(*packet_size) =0 ;fs/ecryptfs/keystore.c +0
-1(*filename_size) =0 ;@@ -977,12 +981,13 @@ ecryptfs_parse_tag_70_packet(char **filename,size_t *filename_size, s->fnek_sig_hex[ECRYPTFS_SIG_SIZE_HEX] = '\0'; (*packet_size) +=ECRYPTFS_SIG_SIZE ; s->cipher_code = data[(*packet_size)++]; - rc = ecryptfs_cipher_code_to_string(s->cipher_string,
s->cipher_code);Quoted 17 lines
Quoted 17 lines
fs/ecryptfs/keystore.c +2
-0+ rc =ecryptfs_cipher_code_to_string (full_cipher, s->cipher_code);if (rc) {printk (KERN_WARNING "%s: Cipher code [%d] is invalid\n" , __func__, s->cipher_code); goto out; } +ecryptfs_parse_full_cipher (full_cipher, s->cipher_string,0 ); rc = ecryptfs_find_auth_tok_for_sig(&auth_tok_key, &s->auth_tok, mount_crypt_stat,fs/ecryptfs/keystore.c +1
-0s->fnek_sig_hex);@@ -1151,6 +1156,7 @@ decrypt_pki_encrypted_session_key(struct ecryptfs_auth_tok *auth_tok, char *payload =NULL ;size_t payload_len =0 ;int rc; +char full_cipher[ECRYPTFS_MAX_CIPHER_NAME_SIZE ];Quoted 12 lines
fs/ecryptfs/keystore.c decrypt_pki_encrypted_session_key(struct · line 1192
Quoted 12 lines
fs/ecryptfs/keystore.c +0
-0rc =ecryptfs_get_auth_tok_sig (&auth_tok_sig, auth_tok);fs/ecryptfs/keystore.c +0
-5if (rc) {@@ -1184,21 +1190,31 @@ decrypt_pki_encrypted_session_key(structecryptfs_auth_tok *auth_tok, rc); goto out; } - auth_tok->session_key.flags |=ECRYPTFS_CONTAINS_DECRYPTED_KEY ; -memcpy (crypt_stat->key, auth_tok->session_key.decrypted_key, - auth_tok->session_key.decrypted_key_size); - crypt_stat->key_size = auth_tok->session_key.decrypted_key_size; - rc = ecryptfs_cipher_code_to_string(crypt_stat->cipher,
cipher_code);Quoted 80 lines
Quoted 80 lines
fs/ecryptfs/keystore.c +16
-1+ + rc =ecryptfs_cipher_code_to_string (full_cipher, cipher_code);if (rc) {ecryptfs_printk (KERN_ERR ,"Cipher code [%d] is invalid\n" , cipher_code) - goto out; + goto out; } + + auth_tok->session_key.flags |=ECRYPTFS_CONTAINS_DECRYPTED_KEY ; +memcpy (crypt_stat->key, auth_tok->session_key.decrypted_key, + auth_tok->session_key.decrypted_key_size); + crypt_stat->key_size =ecryptfs_get_key_size_to_restore_key ( + auth_tok->session_key.decrypted_key_size, full_cipher); + +ecryptfs_parse_full_cipher (full_cipher, + crypt_stat->cipher, crypt_stat->cipher_mode); + crypt_stat->flags |=ECRYPTFS_KEY_VALID ;if (ecryptfs_verbosity >0 ) {ecryptfs_printk (KERN_DEBUG ,"Decrypted session key:\n" );ecryptfs_dump_hex (crypt_stat->key, crypt_stat->key_size); + +ecryptfs_dump_salt_hex (crypt_stat->key, crypt_stat->key_size, + full_cipher); } out:fs/ecryptfs/keystore.c +1
-0kfree (msg);@@ -1380,6 +1396,7 @@ parse_tag_3_packet(struct ecryptfs_crypt_stat *crypt_stat, struct ecryptfs_auth_tok_list_item *auth_tok_list_item;size_t length_size;int rc =0 ; +char full_cipher[ECRYPTFS_MAX_CIPHER_NAME_SIZE ]; (*packet_size) =0 ;fs/ecryptfs/keystore.c +4
-1(*new_auth_tok) =NULL ;@@ -1453,10 +1470,13 @@ parse_tag_3_packet(struct ecryptfs_crypt_stat *crypt_stat, rc = -EINVAL ; goto out_free; } - rc =ecryptfs_cipher_code_to_string (crypt_stat->cipher, + rc =ecryptfs_cipher_code_to_string (full_cipher, (u16 )data[(*packet_size)]);if (rc) goto out_free; +ecryptfs_parse_full_cipher (full_cipher, + crypt_stat->cipher, crypt_stat->cipher_mode); +/* A little extra work to differentiate among the AES key * sizes; see RFC2440 */ fs/ecryptfs/keystore.c +4
-1switch (data[(*packet_size)++]) {@@ -1465,7 +1485,10 @@ parse_tag_3_packet(struct ecryptfs_crypt_stat *crypt_stat, break;default : crypt_stat->key_size = - (*new_auth_tok)->session_key.encrypted_key_size; +ecryptfs_get_key_size_to_restore_key ( + (*new_auth_tok)->session_key.encrypted_key_size, + full_cipher); + } rc =ecryptfs_init_crypt_ctx (crypt_stat);fs/ecryptfs/keystore.c +2
-0if (rc)@@ -1664,6 +1687,8 @@ static intdecrypt_passphrase_encrypted_session_key (struct ecryptfs_auth_tok *auth_tok, struct ecryptfs_crypt_stat *crypt_stat) { + + unsigned char final[2*ECRYPTFS_MAX_CIPHER_NAME_SIZE+1];struct scatterlist dst_sg[2 ];struct scatterlist src_sg[2 ];fs/ecryptfs/keystore.c +0
-0struct mutex *tfm_mutex;@@ -1713,7 +1738,7 @@ decrypt_passphrase_encrypted_session_key(structecryptfs_auth_tok *auth_tok,mutex_lock (tfm_mutex );Quoted 8 lines
fs/ecryptfs/keystore.c
Quoted 8 lines
fs/ecryptfs/keystore.c +1
-1rc =crypto_blkcipher_setkey ( desc.tfm, auth_tok->token.password.session_key_encryption_key, - crypt_stat->key_size); + auth_tok->token.password.session_key_encryption_key_bytes);if (unlikely (rc <0 )) {mutex_unlock (tfm_mutex);fs/ecryptfs/keystore.c +0
-0printk (KERN_ERR "Error setting key for crypto context\n" );@@ -1736,6 +1761,10 @@decrypt_passphrase_encrypted_session_key(struct
Quoted 29 lines
Quoted 29 lines
fs/ecryptfs/keystore.c +4
-0ecryptfs_auth_tok *auth_tok, crypt_stat->key_size);ecryptfs_dump_hex (crypt_stat->key, crypt_stat->key_size); +ecryptfs_dump_salt_hex (crypt_stat->key, crypt_stat->key_size, +ecryptfs_get_full_cipher (crypt_stat->cipher, + crypt_stat->cipher_mode, + final,sizeof (final))); } out:fs/ecryptfs/keystore.c +7
-4return rc;@@ -1972,12 +2001,17 @@ pki_encrypt_session_key(struct key *auth_tok_key, size_t payload_len = 0;struct ecryptfs_message *msg;int rc; +unsigned char final[2 *ECRYPTFS_MAX_CIPHER_NAME_SIZE +1 ]; rc = write_tag_66_packet(auth_tok->token.private_key.signature, -ecryptfs_code_for_cipher_string ( - crypt_stat->cipher, - crypt_stat->key_size), - crypt_stat, &payload, &payload_len); + ecryptfs_code_for_cipher_string( + ecryptfs_get_full_cipher( + crypt_stat->cipher, + crypt_stat->cipher_mode, + final, sizeof(final)), + ecryptfs_get_key_size_to_enc_data(Quoted 8 lines
fs/ecryptfs/keystore.c write_tag_1_packet(char *dest, size_t · line 2070
Quoted 8 lines
fs/ecryptfs/keystore.c +2
-0+ crypt_stat)), + crypt_stat, &payload, &payload_len);up_write (&(auth_tok_key->sem));key_put (auth_tok_key);fs/ecryptfs/keystore.c +0
-0if (rc) {@@ -2035,7 +2069,7 @@ write_tag_1_packet(char *dest, size_t *remaining_bytes, ecryptfs_from_hex(key_rec->sig,
auth_tok->token.private_key.signature,Quoted 4 lines
fs/ecryptfs/keystore.c write_tag_1_packet(char *dest, size_t *remaining_bytes,
Quoted 4 lines
fs/ecryptfs/keystore.c +1
-1ECRYPTFS_SIG_SIZE); encrypted_session_key_valid =0 ; -for (i =0 ; i < crypt_stat->key_size; i++) + for (i = 0; i < ecryptfs_get_key_size_to_store_key(crypt_stat);i++)
Quoted 78 lines
Quoted 78 lines
fs/ecryptfs/keystore.c +0
-0encrypted_session_key_valid |= auth_tok->session_key.encrypted_key[i];fs/ecryptfs/keystore.c +1
-0if (encrypted_session_key_valid) {@@ -2189,6 +2223,7 @@ write_tag_3_packet(char *dest, size_t *remaining_bytes, u8 cipher_code;size_t packet_size_length;size_t max_packet_size; +unsigned char final[2 *ECRYPTFS_MAX_CIPHER_NAME_SIZE +1 ];struct ecryptfs_mount_crypt_stat *mount_crypt_stat = crypt_stat->mount_crypt_stat;fs/ecryptfs/keystore.c +3
-2struct blkcipher_desc desc = {@@ -2221,13 +2256,14 @@ write_tag_3_packet(char *dest, size_t *remaining_bytes, mount_crypt_stat->global_default_cipher_key_size;if (auth_tok->session_key.encrypted_key_size ==0 ) auth_tok->session_key.encrypted_key_size = - crypt_stat->key_size; +ecryptfs_get_key_size_to_store_key (crypt_stat);if (crypt_stat->key_size ==24 &&strcmp ("aes" , crypt_stat->cipher) ==0 ) {memset ((crypt_stat->key +24 ),0 ,8 ); auth_tok->session_key.encrypted_key_size =32 ; }else - auth_tok->session_key.encrypted_key_size = crypt_stat->key_size; + auth_tok->session_key.encrypted_key_size = +ecryptfs_get_key_size_to_store_key (crypt_stat); key_rec->enc_key_size = auth_tok->session_key.encrypted_key_size;fs/ecryptfs/keystore.c +2
-2encrypted_session_key_valid =0 ;@@ -2251,8 +2287,8 @@ write_tag_3_packet(char *dest, size_t *remaining_bytes, auth_tok->token.password. session_key_encryption_key_bytes);memcpy (session_key_encryption_key, - auth_tok->token.password.session_key_encryption_key, - crypt_stat->key_size); + auth_tok->token.password.session_key_encryption_key, + auth_tok->token.password.session_key_encryption_key_bytes);ecryptfs_printk (KERN_DEBUG ,"Cached session key encryption key:\n" );fs/ecryptfs/keystore.c +1
-1if (ecryptfs_verbosity >0 )@@ -2285,7 +2321,7 @@ write_tag_3_packet(char *dest, size_t *remaining_bytes, }mutex_lock (tfm_mutex); rc =crypto_blkcipher_setkey (desc.tfm, session_key_encryption_key, - crypt_stat->key_size); + auth_tok->token.password.session_key_encryption_key_bytes);if (rc <0 ) {mutex_unlock (tfm_mutex);fs/ecryptfs/keystore.c +6
-1ecryptfs_printk(KERN_ERR, "Error setting key for crypto "@@ -2294,7 +2330,12 @@ write_tag_3_packet(char *dest, size_t *remaining_bytes, } rc =0 ;ecryptfs_printk (KERN_DEBUG ,"Encrypting [%zd] bytes of the key\n" , - crypt_stat->key_size); + crypt_stat->key_size); +ecryptfs_printk (KERN_DEBUG ,"Encrypting [%zd] bytes of the salt key\n", +ecryptfs_get_salt_size_for_cipher ( +ecryptfs_get_full_cipher (crypt_stat->cipher, + crypt_stat->cipher_mode, + final,sizeof (final)))); rc = crypto_blkcipher_encrypt(&desc, dst_sg, src_sg,fs/ecryptfs/keystore.c +4
-2(*key_rec).enc_key_size);mutex_unlock (tfm_mutex);@@ -2343,8 +2384,10 @@ encrypted_session_key_set: dest[(*packet_size)++] =0x04 ;/* version 4 */ /* TODO: Break from RFC2440 so that arbitrary ciphers can be * specified with strings */ - cipher_code =ecryptfs_code_for_cipher_string (crypt_stat->cipher, - crypt_stat->key_size); + cipher_code =ecryptfs_code_for_cipher_string ( +ecryptfs_get_full_cipher (crypt_stat->cipher, + crypt_stat->cipher_mode, final,sizeof (final)), + crypt_stat->key_size);Quoted 8 lines
fs/ecryptfs/main.c void ecryptfs_put_lower_file(struct inode
Quoted 8 lines
fs/ecryptfs/keystore.c +0
-0if (cipher_code ==0 ) {ecryptfs_printk (KERN_WARNING ,"Unable to generate code for " "cipher [%s]\n" , crypt_stat->cipher);fs/ecryptfs/main.c +0
-0@@ -165,7 +165,13 @@ void ecryptfs_put_lower_file(struct inode*inode)
Quoted 107 lines
Quoted 107 lines
fs/ecryptfs/main.c +7
-0fput (inode_info->lower_file); inode_info->lower_file =NULL ;mutex_unlock (&inode_info->lower_file_mutex); + +if (get_events () &&get_events ()->release_cb) +get_events ()->release_cb ( +ecryptfs_inode_to_lower (inode)); } + + }enum { ecryptfs_opt_sig, ecryptfs_opt_ecryptfs_sig,@@ -266,6 +272,7 @@ static int ecryptfs_parse_options(struct ecryptfs_sb_info *sbi, char *options, int cipher_key_bytes_set = 0;int fn_cipher_key_bytes;int fn_cipher_key_bytes_set =0 ; +size_t salt_size =0 ;struct ecryptfs_mount_crypt_stat *mount_crypt_stat = &sbi->mount_crypt_stat;fs/ecryptfs/main.c +8
-5substring_t args[MAX_OPT_ARGS ];@@ -280,6 +287,7 @@ static int ecryptfs_parse_options(struct ecryptfs_sb_info *sbi, char *options, char *cipher_key_bytes_src;char *fn_cipher_key_bytes_src;u8 cipher_code; +unsigned char final[2 *ECRYPTFS_MAX_CIPHER_NAME_SIZE +1 ]; *check_ruid =0 ;@@ -309,12 +317,14 @@ static int ecryptfs_parse_options(struct ecryptfs_sb_info *sbi, char *options, case ecryptfs_opt_ecryptfs_cipher: cipher_name_src = args[0 ].from; cipher_name_dst = - mount_crypt_stat-> - global_default_cipher_name; -strncpy (cipher_name_dst, cipher_name_src, -ECRYPTFS_MAX_CIPHER_NAME_SIZE ); - cipher_name_dst[ECRYPTFS_MAX_CIPHER_NAME_SIZE ] ='\0' ; + mount_crypt_stat->global_default_cipher_name; + +ecryptfs_parse_full_cipher (cipher_name_src, + mount_crypt_stat->global_default_cipher_name, + mount_crypt_stat->global_default_cipher_mode); + cipher_name_set =1 ; +break ;case ecryptfs_opt_ecryptfs_key_bytes:fs/ecryptfs/main.c +31
-5cipher_key_bytes_src = args[0 ].from;@@ -411,24 +421,50 @@ static int ecryptfs_parse_options(struct ecryptfs_sb_info *sbi, char *options,strcpy (mount_crypt_stat->global_default_cipher_name,ECRYPTFS_DEFAULT_CIPHER ); } +if ((mount_crypt_stat->flags &ECRYPTFS_GLOBAL_ENCRYPT_FILENAMES ) && !fn_cipher_name_set)strcpy (mount_crypt_stat->global_default_fn_cipher_name, mount_crypt_stat->global_default_cipher_name); -if (!cipher_key_bytes_set) + +if (cipher_key_bytes_set) { + + salt_size =ecryptfs_get_salt_size_for_cipher ( +ecryptfs_get_full_cipher ( + mount_crypt_stat->global_default_cipher_name, + mount_crypt_stat->global_default_cipher_mode, + final,sizeof (final))); + +if (!ecryptfs_check_space_for_salt ( + mount_crypt_stat->global_default_cipher_key_size, + salt_size)) { +ecryptfs_printk ( +KERN_WARNING , +"eCryptfs internal error: no space for salt" ); + } + }else mount_crypt_stat->global_default_cipher_key_size =0 ; +if ((mount_crypt_stat->flags &ECRYPTFS_GLOBAL_ENCRYPT_FILENAMES ) && !fn_cipher_key_bytes_set) mount_crypt_stat->global_default_fn_cipher_key_bytes = mount_crypt_stat->global_default_cipher_key_size; cipher_code =ecryptfs_code_for_cipher_string ( - 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)), mount_crypt_stat->global_default_cipher_key_size);if (!cipher_code) { -ecryptfs_printk (KERN_ERR , -"eCryptfs doesn't support cipher: %s" , - mount_crypt_stat->global_default_cipher_name); +ecryptfs_printk ( +KERN_ERR , +"eCryptfs doesn't support cipher: %s and key size %zu" , +ecryptfs_get_full_cipher ( + mount_crypt_stat->global_default_cipher_name, + mount_crypt_stat->global_default_cipher_mode, + final,sizeof (final)), + mount_crypt_stat->global_default_cipher_key_size); rc = -EINVAL ;Quoted 8 lines
fs/ecryptfs/main.c static struct file_system_type ecryptfs_fs_type; · line 528
Quoted 8 lines
fs/ecryptfs/main.c +0
-0goto out;fs/ecryptfs/main.c +1
-0}@@ -488,6 +524,7 @@ static struct file_system_type ecryptfs_fs_type;* @dev_name: The path to mount over * @raw_data: The options passed into the kernel */ + static struct dentry *ecryptfs_mount(struct file_system_type
*fs_type,Quoted 21 lines
Quoted 21 lines
fs/ecryptfs/main.c +0
-0int flags, const char *dev_name, void *raw_data){
fs/ecryptfs/main.c +0
-0@@ -557,6 +594,8 @@ static struct dentry *ecryptfs_mount(struct
file_system_type*fs_type, int flagsfs/ecryptfs/main.c +2
-0ecryptfs_set_superblock_lower (s, path.dentry->d_sb); +ecryptfs_drop_pagecache_sb (ecryptfs_superblock_to_lower (s),NULL ); +/** * Set the POSIX ACL flag based on whether they're enabled in the lower
fs/ecryptfs/main.c +1
-0* mount. @@ -894,6 +933,7 @@ static void __exit ecryptfs_exit(void)do_sysfs_unregistration ();unregister_filesystem (&ecryptfs_fs_type);ecryptfs_free_kmem_caches (); +ecryptfs_free_events (); }Quoted 6 lines
fs/ecryptfs/mmap.c static sector_t ecryptfs_bmap(struct
Quoted 6 lines
fs/ecryptfs/main.c +0
-0MODULE_AUTHOR ("Michael A. Halcrow <[email protected]>" );fs/ecryptfs/mmap.c +0
-0@@ -552,10 +552,16 @@ static sector_t ecryptfs_bmap(struct
address_spaceQuoted 14 lines
Quoted 14 lines
fs/ecryptfs/mmap.c +5
-0*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,Quoted 8 lines
fs/ecryptfs/super.c static void ecryptfs_i_callback(struct rcu_head
Quoted 8 lines
fs/ecryptfs/mmap.c +1
-0.bmap = ecryptfs_bmap, + .freepage = ecryptfs_freepage,};
fs/ecryptfs/super.c +0
-0@@ -69,6 +69,9 @@ static void ecryptfs_i_callback(struct rcu_head*head)
Quoted 22 lines
Quoted 22 lines
fs/ecryptfs/super.c +6
-0{ 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); + }Quoted 2 lines
fs/ecryptfs/super.c static int ecryptfs_show_options(struct seq_file
Quoted 2 lines
/**
fs/ecryptfs/super.c +0
-0@@ -149,6 +155,9 @@ static int ecryptfs_show_options(struct seq_file*m,
Quoted 82 lines
Quoted 82 lines
fs/ecryptfs/super.c +3
-0struct 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);fs/ecryptfs/super.c +4
-1list_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)fs/ecryptfs/super.c +0
-0seq_printf(m, ",ecryptfs_key_bytes=%zd",include/linux/ecryptfs.h +14
-0@@ -1,6 +1,9 @@#ifndef _LINUX_ECRYPTFS_H#define _LINUX_ECRYPTFS_H +struct inode ; +struct page ; +/* Version verification for shared data structures w/ userspace */ #define ECRYPTFS_VERSION_MAJOR 0x00#define ECRYPTFS_VERSION_MINOR 0x04@@ -41,6 +44,7 @@#define RFC2440_CIPHER_AES_256 0x09#define RFC2440_CIPHER_TWOFISH 0x0a#define RFC2440_CIPHER_CAST_6 0x0b +#define RFC2440_CIPHER_AES_XTS_256 0x0c#define RFC2440_CIPHER_RSA 0x01@@ -102,4 +106,47 @@ struct ecryptfs_auth_tok { } token; } __attribute__ ((packed)); +#define ECRYPTFS_INVALID_EVENTS_HANDLE -1 + +/** +* ecryptfs_events struct represents a partial interface +* towards ecryptfs module. If registered to ecryptfs events, +* one can receive push notifications. +* A first callback received from ecryptfs will probably be +* about file opening (open_cb), +* in which ecryptfs passes its ecryptfs_data for future usage. +* This data represents a file and must be passed in every query functions
+ * such asecryptfs_get_key_size(),ecryptfs_get_cipher()etc.
+ */include/linux/ecryptfs.h +20
-0+struct ecryptfs_events { +bool (*is_cipher_supported_cb)(const char *cipher); +void (*open_cb)(struct inode *inode,void *ecrytpfs_data); +void (*release_cb)(struct inode *inode); +int (*encrypt_cb)(struct page *in_page,struct page *out_page, +struct inode *inode,unsigned long extent_offset); +int (*decrypt_cb)(struct page *in_page,struct page *out_page, +struct inode *inode,unsigned long extent_offset); +bool (*is_hw_crypt_cb)(void ); +size_t (*get_salt_key_size_cb)(const char *cipher); +}; + + +int ecryptfs_register_to_events (struct ecryptfs_events *ops); + +int ecryptfs_unregister_from_events (int user_handle); + +const unsigned char *ecryptfs_get_key (void *ecrytpfs_data); + +size_t ecryptfs_get_key_size (void *ecrytpfs_data);Quoted 8 lines
include/linux/ecryptfs.h struct ecryptfs_auth_tok {
Quoted 8 lines
include/linux/ecryptfs.h +8
-0+ +const unsigned char *ecryptfs_get_salt (void *ecrytpfs_data); + +size_t ecryptfs_get_salt_size (void *ecrytpfs_data); + +const unsigned char *ecryptfs_get_cipher (void *ecrytpfs_data); + +bool ecryptfs_is_page_in_metadata(void *ecrytpfs_data, pgoff_toffset);
Quoted 12 lines
Quoted 11 lines
include/linux/ecryptfs.h +2
-0+ +bool ecryptfs_is_data_equal(void *ecrytpfs_data1, void
*ecrytpfs_data2);
+
#endif /*_LINUX_ECRYPTFS_H*/
--
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
Quoted 7 lines · On 2015-11-10 15:20:59, [email protected] wrote:
Quoted 4 lines
This is a hardware inline accelerator, meaning that it operates on much lower layer, block layer and device driver layer. The HW encrypts plain requests sent from block layer directly, thus doing it much more efficiently rather than using crypto API.
Quoted 6 lines
I feel like basing this on eCryptfs is an odd choice. The overhead of setting up an eCryptfs mount (and requiring
CAP_SYS_ADMIN) and the duplication of page cache and dentry cache entries (for upper and lower caches) seems like considerable baggage for such a nifty, new hardware feature.
First of all, one of the leading companies on the mobile market uses eCryptfs as a solution for their file-based-encryption feature and they use it along with HW crypto engine. I believe we will soon see interest from additional companies.
Secondly, eCryptfs is convenient in many ways for implementing file-based-encryption and there are not so many good alternatives as of today. You are right regarding overhead, page duplication, etc., however enabling eCryptfs to work with HW encryption still makes it a very efficient solution. Also, it is not just inline HW crypto engine, any HW crypto engine operates more efficiently while working on long chunks of data. Our suggested solution uses the existing block layer (that is part of the standard Linux storage stack) feature that gathers number of block to into one request and then encrypt/decrypt the data, whereas eCryptfs can only perform the crypto operation on single pages. This feature was tested on our platforms and demonstrated very significance performance improvement comparing to existing SW based solutions
Quoted 19 lines
Quoted 6 lines
In order to use such HW efficiently with eCryptfs, eCryptfs encryption has
to be canceled and it will need to call for external module instead that will do the correct marking for the blocks to distinguish between those that need to be encrypted by the HW from those that do not need.Quoted 6 lines
We are considering posting the code that will call
ecryptfs_register_to_events()as a separate patch, but haven't done so yet. It is HW specific.
Currently we are only proposing framework change so that it can allow for
external modules to be connectedIn that case, I cannot accept this patch. I will have no way to test the external module functionality and will not be able to make changes to that area of the code because it will not be possible for me to fix anything that I've broken. I won't even be able to know if I've broken anything.
Quoted 6 lines
If you are able to upstream the external module code, then I'll do a proper review of this patch. I should note that I've only glanced at the patch but if feels like it should be broken up into smaller, easier to review patches before it can be properly reviewed.
Tyler
We can upstream the external module code, however as I mentioned this module is specific for our HW, so you won't be able to test it either. However we can pretty easily turn it into some dummy module for testing purposes only that will just do prints instead of HW specific calls. Would this be sufficient ? If so, where in project tree should I put it ?
Quoted 1,918 lines
Quoted 1,917 lines
Quoted 6 lines · On 2015-11-09 20:56:02, [email protected] wrote:
Quoted 3 lines
Hello, Tyler
I'll try to provide more detailed explanation, should it be
satisfactory
Quoted 5 lines
Quoted 5 lines
enough I will update the patch description.
The problem with current eCryptfs is that it has total control on how and
when the encryption is performed and this control can't be altered.One
Quoted 8 lines
Quoted 7 lines
example when this can be a problem is when we want to utilize an underlying inline HW encryption engine which allows encrypting blocks 'on
the fly' as they are being written to the storage. In such a case relevant
blocks just need to be marked as 'should be encrypted'. No actual encryption should be done by eCryptfs as it will be much slower.Quoted 5 lines
Is this a hardware crypto accelerator? If so, why not create a crypto api driver so all subsystems can take advantage of the acceleration instead of baking support into individual subsystems?
Quoted 1 line
The provided framework allows transferring this control (if needed)
to
Quoted 5 lines
Quoted 5 lines
some external module which will do the encryption itfelf or just mark the
appropriate blocks.There is no caller for
ecryptfs_register_to_events()since thischange
Quoted 10 lines
Quoted 3 lines
only provides framework, it doesn't provide the module itself, the module
could be HW dependent.Will the code that you plan to call
ecryptfs_register_to_events()be upstream? If so, have you posted it?Tyler
Quoted 1 line
Regarding the mounting option, it merely serves as example of new
cipher
Quoted 2 lines
Quoted 2 lines
mode that can be served by registered module.
There is a special callback function that should be implemented bythe
Quoted 1 line
Quoted 1 line
registered module that tells whether a particular cipher is supported
by
Quoted 5 lines
Quoted 5 lines
it :
is_cipher_supported_cb()The mounting option itself is not necessary, I can remove it
Quoted 8 lines
Quoted 8 lines
Quoted 8 lines
Hello Andrey!
Quoted 5 lines · Andrey Markovytch
From: Andrey Markovytch <[email protected]>
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
Quoted 1 line
Quoted 1 line
Quoted 1 line
Quoted 1 line
and provide alternative encryption mechanism and also decide
whether
Quoted 1 line
Quoted 1 line
Quoted 1 line
Quoted 1 line
encryption should be performed at all, or deferred to a later
stage
Quoted 7 lines
Quoted 7 lines
via
Quoted 2 lines
Quoted 2 lines
the inline HW engine.
Additional cipher option was introduced to support the HW/externalmode
Quoted 3 lines
Quoted 2 lines
under that name of "aes-xts". If no external module has registered to support this cipher, eCryptfs will fall back to the usual "aes"
Quoted 7 lines
Quoted 7 lines
Quoted 1 line
What is "HW/external mode"? There's no description in the commit
message
Quoted 1 line
and
ecryptfs_register_to_events()does not have a caller so I'm notsure
Quoted 3 lines
of the purpose. Please provide more context.
Despite not yet understanding the purpose of this patch, I think
that
Quoted 4 lines
Quoted 4 lines
I
Quoted 1 line
can safely say that "aes-xts" is not an appropriate mount option to
use
Quoted 1 line
when enabling this mode. eCryptfs may support XTS mode one day,
using
Quoted 1 line
Quoted 1 line
Quoted 1 line
the Crypto API, so "HW/external mode" should not own the mount
option.
Quoted 40 lines
Quoted 40 lines
Quoted 40 lines
Tyler
Quoted 37 lines
Signed-off-by Lina Zarivach <[email protected]>Signed-off-by Andrey Markovytch <[email protected]>--- 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.cfs/ecryptfs/Makefile +2
-2@@ -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.oQuoted 2 lines
Quoted 2 lines
Quoted 2 lines
Review
Quoted 2 lines
ecryptfs-$(CONFIG_ECRYPT_FS_MESSAGING) += messaging.o miscdev.o
(quoted diff) +0
-0
b/fs/ecryptfs/caches_utils.cQuoted 10 lines
Quoted 10 lines
Quoted 9 lines
fs/ecryptfs/caches_utils.c line 4
Quoted 9 lines
new file mode 100644
index 0000000..c599c96fs/ecryptfs/caches_utils.c new file +4
-0@@ -0,0 +1,78 @@ +/* +* Copyright (c) 2015, The Linux Foundation. All rights reserved. +* +* This program is free software; you can redistribute it and/or modify
Quoted 5 lines
Quoted 5 lines
Quoted 1 line
fs/ecryptfs/caches_utils.c line 5
Quoted 1 line
fs/ecryptfs/caches_utils.c new file +1
-0+* it under the terms of the GNU General Public License version 2 and
Quoted 3 lines
Quoted 3 lines
fs/ecryptfs/caches_utils.c new file +3
-0+* only version 2 as published by the Free Software Foundation. +* +* This program is distributed in the hope that it will be useful,
Quoted 131 lines
Quoted 131 lines
Quoted 6 lines
Quoted 6 lines
fs/ecryptfs/caches_utils.c new file +6
-0+* but WITHOUT ANY WARRANTY; without even the implied warranty of +* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +* GNU General Public License for more details. +*/ + +#include <linux/kernel.h> Quoted 8 lines
fs/ecryptfs/caches_utils.c
Quoted 8 lines
fs/ecryptfs/caches_utils.c new file +8
-0+#include <linux/fs.h> +#include <linux/spinlock.h> +#include <linux/pagemap.h> +#include <linux/pagevec.h> + +#include "../internal.h" + +void ecryptfs_drop_pagecache_sb(struct super_block *sb, void*unused)
Quoted 116 lines
Quoted 116 lines
fs/ecryptfs/caches_utils.c new file +56
-0+{ +struct inode *inode, *toput_inode =NULL ; + +spin_lock (&sb->s_inode_list_lock); +list_for_each_entry (inode, &sb->s_inodes, i_sb_list) { +spin_lock (&inode->i_lock); +if ((inode->i_state & (I_FREEING |I_WILL_FREE |I_NEW )) || + (inode->i_mapping->nrpages ==0 )) { +spin_unlock (&inode->i_lock); +continue ; + } +__iget (inode); +spin_unlock (&inode->i_lock); +spin_unlock (&sb->s_inode_list_lock); + +invalidate_mapping_pages (inode->i_mapping,0 ,-1 ); +iput (toput_inode); + toput_inode = inode; + +spin_lock (&sb->s_inode_list_lock); + } +spin_unlock (&sb->s_inode_list_lock); +iput (toput_inode); +} + +void clean_inode_pages (struct address_space *mapping, +pgoff_t start,pgoff_t end) +{ +struct pagevec pvec; +pgoff_t index = start; +int i; + +pagevec_init (&pvec,0 ); +while (index <= end &&pagevec_lookup (&pvec, mapping, index, +min (end - index, + (pgoff_t )PAGEVEC_SIZE -1 ) +1 )) { +for (i =0 ; i <pagevec_count (&pvec); i++) { +struct page *page = pvec.pages[i]; + +/* We rely upon deletion +* not changing page->index +*/ + index = page->index; +if (index > end) +break ; +if (!trylock_page (page)) +continue ; +WARN_ON (page->index != index); +zero_user (page,0 ,PAGE_CACHE_SIZE ); +unlock_page (page); + } +pagevec_release (&pvec); +cond_resched (); + index++; + } +}fs/ecryptfs/crypto.c +3
-2@@ -35,6 +35,7 @@#include <linux/scatterlist.h> #include <linux/slab.h> #include <asm/unaligned.h> +#include <linux/ecryptfs.h> #include "ecryptfs_kernel.h" #define DECRYPT 0@@ -350,9 +351,9 @@ static int crypt_scatterlist(struct ecryptfs_crypt_stat *crypt_stat, || !(crypt_stat->flags & ECRYPTFS_STRUCT_INITIALIZED));if (unlikely (ecryptfs_verbosity >0 )) {ecryptfs_printk (KERN_DEBUG ,"Key size [%zd]; key:\n" , - crypt_stat->key_size); +ecryptfs_get_key_size_to_enc_data (crypt_stat));ecryptfs_dump_hex (crypt_stat->key, - crypt_stat->key_size); +ecryptfs_get_key_size_to_enc_data (crypt_stat)); }fs/ecryptfs/crypto.c +1
-1init_completion (&ecr.completion);@@ -371,7 +372,7 @@ static int crypt_scatterlist(struct ecryptfs_crypt_stat *crypt_stat,/* Consider doing this once, when the file is opened */ if (!(crypt_stat->flags & ECRYPTFS_KEY_SET)) { rc =crypto_ablkcipher_setkey (crypt_stat->tfm, crypt_stat->key, - crypt_stat->key_size); +ecryptfs_get_key_size_to_enc_data (crypt_stat));if (rc) { ecryptfs_printk(KERN_ERR,fs/ecryptfs/crypto.c +20
-0"Error setting key; rc = [%d]\n",@@ -466,6 +467,31 @@ out:return rc; } +static void init_ecryption_parameters (bool *hw_crypt,bool *cipher_supported, +struct ecryptfs_crypt_stat *crypt_stat) +{ +unsigned char final[2 *ECRYPTFS_MAX_CIPHER_NAME_SIZE +1 ]; + +if (!hw_crypt || !cipher_supported) +return ; + + *cipher_supported = false; + *hw_crypt = false; + +if (get_events () &&get_events ()->is_cipher_supported_cb) { + *cipher_supported = +get_events ()->is_cipher_supported_cb ( +ecryptfs_get_full_cipher (crypt_stat->cipher, + crypt_stat->cipher_mode, final,sizeof (final))); +if (*cipher_supported) { +/* we should apply external algorythm +* assume that is_hw_crypt() cbck is supplied +*/ Quoted 8 lines
Quoted 8 lines
Quoted 8 lines
Quoted 8 lines
fs/ecryptfs/crypto.c +5
-0+ *hw_crypt =get_events ()->is_hw_crypt_cb (); + } + } +} +/** * ecryptfs_encrypt_page * @page: Page mapped from the eCryptfs inode for the file;
contains
Quoted 18 lines
Quoted 18 lines
Quoted 18 lines
Quoted 18 lines
fs/ecryptfs/crypto.c +7
-0@@ -491,11 +517,18 @@ int ecryptfs_encrypt_page(struct page *page)loff_t extent_offset;loff_t lower_offset;int rc =0 ; +bool is_hw_crypt; +bool is_cipher_supported; + ecryptfs_inode = page->mapping->host; crypt_stat = &(ecryptfs_inode_to_private (ecryptfs_inode)->crypt_stat);BUG_ON (!(crypt_stat->flags &ECRYPTFS_ENCRYPTED )); + +init_ecryption_parameters (&is_hw_crypt, + &is_cipher_supported, crypt_stat); + enc_extent_page =alloc_page (GFP_USER );if (!enc_extent_page) {Quoted 8 lines
Quoted 8 lines
Quoted 8 lines
fs/ecryptfs/crypto.c int ecryptfs_encrypt_page(struct page *page) · line 508
Quoted 8 lines
fs/ecryptfs/crypto.c +0
-3rc = -ENOMEM ;@@ -503,24 +536,51 @@ int ecryptfs_encrypt_page(struct page *page) "encrypted extent\n"); goto out; } - - for (extent_offset = 0; - extent_offset < (PAGE_CACHE_SIZE /
crypt_stat->extent_size);Quoted 72 lines
Quoted 72 lines
Quoted 72 lines
Quoted 72 lines
fs/ecryptfs/crypto.c +40
-10- extent_offset++) { - rc =crypt_extent (crypt_stat, enc_extent_page, page, - extent_offset,ENCRYPT ); -if (rc) { -printk (KERN_ERR "%s: Error encrypting extent; " -"rc = [%d]\n" , __func__, rc); - goto out; - } +if (is_hw_crypt) { +/* no need for encryption */ + }else { +for (extent_offset =0 ; + extent_offset < + (PAGE_CACHE_SIZE / crypt_stat->extent_size); + extent_offset++) { + +if (is_cipher_supported) { +if (!get_events ()->encrypt_cb) { + rc = -EPERM ; + goto out; + } + rc =get_events ()->encrypt_cb (page, + enc_extent_page, +ecryptfs_inode_to_lower ( + ecryptfs_inode), + extent_offset); + }else { + rc =crypt_extent (crypt_stat, + enc_extent_page, page, + extent_offset,ENCRYPT ); + } +if (rc) { +ecryptfs_printk (KERN_ERR , +"%s: Error encrypting; rc = [%d]\n" , + __func__, rc); + goto out; + } + } } lower_offset =lower_offset_for_page (crypt_stat, page); - enc_extent_virt =kmap (enc_extent_page); +if (is_hw_crypt) + enc_extent_virt =kmap (page); +else + enc_extent_virt =kmap (enc_extent_page); + rc =ecryptfs_write_lower (ecryptfs_inode, enc_extent_virt, lower_offset,PAGE_CACHE_SIZE ); -kunmap (enc_extent_page); +if (!is_hw_crypt) +kunmap (enc_extent_page); +else +kunmap (page); +if (rc <0 ) { ecryptfs_printk(KERN_ERR,fs/ecryptfs/crypto.c +2
-0"Error attempting to write lower page; rc = [%d]\n",@@ -559,6 +619,8 @@ int ecryptfs_decrypt_page(struct page *page)unsigned long extent_offset;loff_t lower_offset;int rc =0 ; +bool is_cipher_supported; +bool is_hw_crypt; ecryptfs_inode = page->mapping->host; crypt_stat =@@ -577,13 +639,33 @@ int ecryptfs_decrypt_page(struct page *page) goto out; }Quoted 10 lines
Quoted 10 lines
Quoted 10 lines
fs/ecryptfs/crypto.c int ecryptfs_decrypt_page(struct page *page)
Quoted 10 lines
fs/ecryptfs/crypto.c +8
-0+init_ecryption_parameters (&is_hw_crypt, + &is_cipher_supported, crypt_stat); + +if (is_hw_crypt) { + rc =0 ; +return rc; + } + for (extent_offset = 0; extent_offset < (PAGE_CACHE_SIZE /
crypt_stat->extent_size);Quoted 90 lines
Quoted 90 lines
Quoted 47 lines
Quoted 47 lines
fs/ecryptfs/crypto.c +15
-3extent_offset++) { - rc =crypt_extent (crypt_stat, page, page, +if (is_cipher_supported) { +if (!get_events ()->decrypt_cb) { + rc = -EPERM ; + goto out; + } + + rc =get_events ()->decrypt_cb (page, page, +ecryptfs_inode_to_lower (ecryptfs_inode), + extent_offset); + + }else + rc =crypt_extent (crypt_stat, page, page, extent_offset,DECRYPT ); +if (rc) { -printk (KERN_ERR "%s: Error encrypting extent; " +ecryptfs_printk (KERN_ERR ,"%s: Error decrypting extent;" "rc = [%d]\n" , __func__, rc); goto out; }@@ -612,7 +694,7 @@ int ecryptfs_init_crypt_ctx(struct ecryptfs_crypt_stat *crypt_stat)"Initializing cipher [%s]; strlen = [%d]; " "key_size_bits = [%zd]\n" , crypt_stat->cipher, (int )strlen (crypt_stat->cipher), - crypt_stat->key_size << 3); + ecryptfs_get_key_size_to_enc_data(crypt_stat) << 3);mutex_lock (&crypt_stat->cs_tfm_mutex);if (crypt_stat->tfm) {fs/ecryptfs/crypto.c +1
-1rc =0 ;@@ -694,7 +776,7 @@ int ecryptfs_compute_root_iv(struct ecryptfs_crypt_stat *crypt_stat) goto out; } rc =ecryptfs_calculate_md5 (dst, crypt_stat, crypt_stat->key, - crypt_stat->key_size); +ecryptfs_get_key_size_to_enc_data (crypt_stat));if (rc) { ecryptfs_printk(KERN_WARNING, "Error attempting to compute "fs/ecryptfs/crypto.c +0
-0"MD5 while generating root IV\n");@@ -721,6 +803,35 @@ static void ecryptfs_generate_new_key(struct ecryptfs_crypt_stat *crypt_stat) } }Quoted 15 lines
Quoted 5 lines
fs/ecryptfs/crypto.c +4
-0+static int ecryptfs_generate_new_salt (struct ecryptfs_crypt_stat *crypt_stat) +{ +size_t salt_size =0 ; +unsigned char final[2 *ECRYPTFS_MAX_CIPHER_NAME_SIZE +1 ];fs/ecryptfs/crypto.c static void ecryptfs_generate_new_key(struct ecryptfs_crypt_stat *crypt_stat)
Quoted 10 lines
fs/ecryptfs/crypto.c +10
-0+ + salt_size =ecryptfs_get_salt_size_for_cipher ( +ecryptfs_get_full_cipher (crypt_stat->cipher, + crypt_stat->cipher_mode, + final,sizeof (final))); + +if (salt_size ==0 ) +return 0 ; + + if (!ecryptfs_check_space_for_salt(crypt_stat->key_size,
salt_size)) {Quoted 6 lines
fs/ecryptfs/crypto.c static void ecryptfs_generate_new_key(struct ecryptfs_crypt_stat *crypt_stat)
Quoted 6 lines
fs/ecryptfs/crypto.c +6
-0+ecryptfs_printk (KERN_WARNING ,"not enough space for salt\n" ); + crypt_stat->flags |=ECRYPTFS_SECURITY_WARNING ; +return -EINVAL ; + } + + get_random_bytes(crypt_stat->key + crypt_stat->key_size,
salt_size);Quoted 20 lines
Quoted 20 lines
fs/ecryptfs/crypto.c +9
-0+if (unlikely (ecryptfs_verbosity >0 )) { +ecryptfs_printk (KERN_DEBUG ,"Generated new session salt:\n" ); +ecryptfs_dump_hex (crypt_stat->key + crypt_stat->key_size, + salt_size); + } + +return 0 ; +} +/** *
ecryptfs_copy_mount_wide_flags_to_inode_flags
* @crypt_stat: The inode's cryptographic contextfs/ecryptfs/crypto.c +0
-1@@ -823,7 +934,6 @@ int ecryptfs_new_file_context(struct inode *ecryptfs_inode)struct ecryptfs_mount_crypt_stat *mount_crypt_stat = &ecryptfs_superblock_to_private ( ecryptfs_inode->i_sb)->mount_crypt_stat; -int cipher_name_len;int rc =0 ;Quoted 1 line
Quoted 1 line
Quoted 1 line
Quoted 1 line
ecryptfs_set_default_crypt_stat_vals(crypt_stat,
mount_crypt_stat);Quoted 113 lines
Quoted 113 lines
Quoted 67 lines
Quoted 67 lines
fs/ecryptfs/crypto.c +9
-5@@ -837,15 +947,19 @@ int ecryptfs_new_file_context(struct inode *ecryptfs_inode) "to the inode key sigs; rc = [%d]\n", rc); goto out; } - cipher_name_len = -strlen (mount_crypt_stat->global_default_cipher_name); -memcpy (crypt_stat->cipher, +strlcpy (crypt_stat->cipher, mount_crypt_stat->global_default_cipher_name, - cipher_name_len); - crypt_stat->cipher[cipher_name_len] ='\0' ; +sizeof (crypt_stat->cipher)); + +strlcpy (crypt_stat->cipher_mode, + mount_crypt_stat->global_default_cipher_mode, +sizeof (crypt_stat->cipher_mode)); + crypt_stat->key_size = mount_crypt_stat->global_default_cipher_key_size;ecryptfs_generate_new_key (crypt_stat); +ecryptfs_generate_new_salt (crypt_stat); + rc =ecryptfs_init_crypt_ctx (crypt_stat);if (rc)fs/ecryptfs/crypto.c +7
-1ecryptfs_printk(KERN_ERR, "Error initializing cryptographic "@@ -971,7 +1085,8 @@ ecryptfs_cipher_code_str_map[] = { {"twofish", RFC2440_CIPHER_TWOFISH}, {"cast6", RFC2440_CIPHER_CAST_6}, {"aes", RFC2440_CIPHER_AES_192}, - {"aes", RFC2440_CIPHER_AES_256} + {"aes", RFC2440_CIPHER_AES_256}, + {"aes_xts", RFC2440_CIPHER_AES_XTS_256} };/** @@ -999,6 +1114,11 @@ u8 ecryptfs_code_for_cipher_string(char *cipher_name, size_t key_bytes)case 32 : code =RFC2440_CIPHER_AES_256 ; } + }else if (strcmp (cipher_name,"aes_xts" ) ==0 ) { +switch (key_bytes) { +case 32 : + code =RFC2440_CIPHER_AES_XTS_256 ; + } }else {for (i =0 ; i <ARRAY_SIZE (ecryptfs_cipher_code_str_map); i++)fs/ecryptfs/crypto.c +12
-0if (strcmp (cipher_name, map[i].cipher_str) ==0 ) {@@ -1038,9 +1158,24 @@ int ecryptfs_read_and_validate_header_region(struct inode *inode) u8 file_size[ECRYPTFS_SIZE_AND_MARKER_BYTES];u8 *marker = file_size +ECRYPTFS_FILE_SIZE_BYTES ;int rc; +unsigned int ra_pages_org; +struct file *lower_file =NULL ; + +if (!inode) +return -EIO ; + lower_file =ecryptfs_inode_to_private (inode)->lower_file; +if (!lower_file) +return -EIO ; + +/*disable read a head mechanism for a while */ + ra_pages_org = lower_file->f_ra.ra_pages; + lower_file->f_ra.ra_pages =0 ;Quoted 1 line
fs/ecryptfs/crypto.c int · line 1175
Quoted 1 line
fs/ecryptfs/crypto.c +0
-0rc = ecryptfs_read_lower(file_size, 0,
ECRYPTFS_SIZE_AND_MARKER_BYTES,Quoted 44 lines
Quoted 44 lines
fs/ecryptfs/crypto.c +3
-0inode); + lower_file->f_ra.ra_pages = ra_pages_org; +/* restore read a head mechanism */ +if (rc <ECRYPTFS_SIZE_AND_MARKER_BYTES )return rc >=0 ? -EINVAL : rc;fs/ecryptfs/crypto.c +5
-0rc =ecryptfs_validate_marker (marker);@@ -1430,6 +1565,11 @@ int ecryptfs_read_metadata(struct dentry *ecryptfs_dentry)struct ecryptfs_mount_crypt_stat *mount_crypt_stat = &ecryptfs_superblock_to_private ( ecryptfs_dentry->d_sb)->mount_crypt_stat; +unsigned int ra_pages_org; +struct file *lower_file = +ecryptfs_inode_to_private (ecryptfs_inode)->lower_file; +if (!lower_file) +return -EIO ; ecryptfs_copy_mount_wide_flags_to_inode_flags(crypt_stat,fs/ecryptfs/crypto.c +6
-0mount_crypt_stat);@@ -1441,8 +1581,14 @@ int ecryptfs_read_metadata(struct dentry *ecryptfs_dentry) __func__); goto out; } +/*disable read a head mechanism */ + ra_pages_org = lower_file->f_ra.ra_pages; + lower_file->f_ra.ra_pages =0 ; + rc =ecryptfs_read_lower (page_virt,0 , crypt_stat->extent_size, ecryptfs_inode); + lower_file->f_ra.ra_pages = ra_pages_org;/* restore it back */ +if (rc >=0 ) rc = ecryptfs_read_headers_virt(page_virt, crypt_stat,fs/ecryptfs/crypto.c +0
-0ecryptfs_dentry,fs/ecryptfs/debug.c +0
-0@@ -119,3 +119,16 @@ void ecryptfs_dump_hex(char *data, int bytes)printk ("\n" ); }Quoted 1 line
Quoted 1 line
Quoted 1 line
fs/ecryptfs/debug.c void ecryptfs_dump_hex(char *data, int bytes) · line 122
Quoted 1 line
fs/ecryptfs/debug.c +1
-0+void ecryptfs_dump_salt_hex(char *data, int key_size, char*cipher)
Quoted 100 lines
Quoted 100 lines
Quoted 29 lines
Quoted 29 lines
fs/ecryptfs/debug.c +12
-0+{ +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 +1
-0@@ -245,6 +245,7 @@ struct ecryptfs_crypt_stat {struct mutex cs_tfm_mutex;struct mutex cs_hash_tfm_mutex;struct mutex cs_mutex; +unsigned char cipher_mode[ECRYPTFS_MAX_CIPHER_NAME_SIZE +1 ]; };/* inode private data. */ @@ -267,6 +268,8 @@ struct ecryptfs_dentry_info { }; };Quoted 4 lines
fs/ecryptfs/ecryptfs_kernel.h struct ecryptfs_dentry_info { · line 274
Quoted 4 lines
fs/ecryptfs/ecryptfs_kernel.h +2
-0+ +/** * ecryptfs_global_auth_tok - A key used to encrypt all new files under
Quoted 7 lines
fs/ecryptfs/ecryptfs_kernel.h struct ecryptfs_mount_crypt_stat { · line 351
Quoted 7 lines
the mountpoint
* @flags: Status flagsfs/ecryptfs/ecryptfs_kernel.h +1
-0@@ -345,6 +348,8 @@ struct ecryptfs_mount_crypt_stat {unsigned char global_default_fn_cipher_name[ECRYPTFS_MAX_CIPHER_NAME_SIZE +1 ];char global_default_fnek_sig[ECRYPTFS_SIG_SIZE_HEX +1 ]; +unsigned char global_default_cipher_mode[ECRYPTFS_MAX_CIPHER_NAME_SIZE
Quoted 7 lines
fs/ecryptfs/ecryptfs_kernel.h ecryptfs_dentry_to_lower_path(struct dentry · line 533
Quoted 7 lines
fs/ecryptfs/ecryptfs_kernel.h +1
-0+ + 1]; };/* superblock private data. */ @@ -527,6 +532,53 @@ ecryptfs_dentry_to_lower_path(struct dentry *dentry) return &((struct ecryptfs_dentry_info*)dentry->d_fsdata)->lower_path;
Quoted 17 lines
Quoted 17 lines
fs/ecryptfs/ecryptfs_kernel.h +15
-0} +/** +* Given a cipher and mode strings, the function +* concatenates them to create a new string of +* <cipher>_<mode> format. +*/ +static inline unsigned char *ecryptfs_get_full_cipher ( +unsigned char *cipher,unsigned char *mode, +unsigned char *final,size_t final_size) +{ +memset (final,0 , final_size); + +if (strlen (mode) >0 ) { +snprintf (final, final_size,"%s_%s" , cipher, mode); +return final; + }Quoted 8 lines
fs/ecryptfs/ecryptfs_kernel.h ecryptfs_dentry_to_lower_path(struct dentry *dentry)
Quoted 8 lines
fs/ecryptfs/ecryptfs_kernel.h +8
-0+ +return cipher; +} + +/** +* Given a <cipher>[_<mode>] formatted string, the function +* extracts cipher string and/or mode string. +* Note: the passed cipher and/or mode strings will be null-terminated.
Quoted 24 lines
Quoted 24 lines
fs/ecryptfs/ecryptfs_kernel.h +24
-0+*/ +static inline void ecryptfs_parse_full_cipher ( +char *s,char *cipher,char *mode) +{ +char input[2 *ECRYPTFS_MAX_CIPHER_NAME_SIZE +1 +1 ]; +/* +1 for '_'; +1 for '\0' */ +char *p; +char *input_p = input; + +if (s ==NULL || cipher ==NULL ) +return ; + +memset (input,0 ,sizeof (input)); +strlcpy (input, s,sizeof (input)); + + p =strsep (&input_p,"_" ); +strlcpy (cipher, p,ECRYPTFS_MAX_CIPHER_NAME_SIZE +1 ); + + +/* check if mode is specified */ +if (input_p !=NULL && mode !=NULL ) +strlcpy (mode, input_p,ECRYPTFS_MAX_CIPHER_NAME_SIZE +1 ); +} +Quoted 8 lines
Quoted 8 lines
Quoted 8 lines
fs/ecryptfs/ecryptfs_kernel.h int ecryptfs_encrypt_and_encode_filename( · line 630
Quoted 8 lines
fs/ecryptfs/ecryptfs_kernel.h +0
-0#define ecryptfs_printk(type, fmt, arg...) \ __ecryptfs_printk(type "%s: " fmt, __func__, ## arg);
__printf(1, 2)fs/ecryptfs/ecryptfs_kernel.h +1
-0@@ -575,6 +627,7 @@ int ecryptfs_encrypt_and_encode_filename( const char *name, size_t name_size);struct dentry *ecryptfs_lower_dentry (struct dentry *this_dentry);void ecryptfs_dump_hex (char *data,int bytes); +void ecryptfs_dump_salt_hex(char *data, int key_size, char*cipher);
Quoted 7 lines
Quoted 7 lines
Quoted 1 line
fs/ecryptfs/ecryptfs_kernel.h int ecryptfs_encrypt_and_encode_filename( · line 631
Quoted 1 line
fs/ecryptfs/ecryptfs_kernel.h +0
-0int virt_to_scatterlist(const void *addr, int size, structscatterlist
Quoted 3 lines
Quoted 3 lines
fs/ecryptfs/ecryptfs_kernel.h +0
-0*sg, int sg_size);int ecryptfs_compute_root_iv(struct
ecryptfs_crypt_stat
*crypt_stat);Quoted 1 line
fs/ecryptfs/ecryptfs_kernel.h int ecryptfs_set_f_namelen(long *namelen,
Quoted 1 line
fs/ecryptfs/ecryptfs_kernel.h +0
-0@@ -718,4 +771,29 @@ int ecryptfs_set_f_namelen(long *namelen,long
Quoted 10 lines
Quoted 10 lines
Quoted 9 lines
fs/ecryptfs/ecryptfs_kernel.h int ecryptfs_set_f_namelen(long *namelen, long lower_namelen,
Quoted 9 lines
fs/ecryptfs/ecryptfs_kernel.h +4
-0lower_namelen,int ecryptfs_derive_iv (char *iv,struct ecryptfs_crypt_stat *crypt_stat,loff_t offset); +void clean_inode_pages (struct address_space *mapping, +pgoff_t start,pgoff_t end); + +void ecryptfs_drop_pagecache_sb(struct super_block *sb, void*unused);
Quoted 16 lines
Quoted 8 lines
Quoted 8 lines
Quoted 8 lines
fs/ecryptfs/ecryptfs_kernel.h +8
-0+ +void ecryptfs_free_events (void ); + +void ecryptfs_freepage (struct page *page); + +struct ecryptfs_events *get_events (void ); + +size_t ecryptfs_get_salt_size_for_cipher (const char *cipher);Quoted 8 lines
Quoted 8 lines
fs/ecryptfs/ecryptfs_kernel.h int ecryptfs_set_f_namelen(long *namelen, long lower_namelen,
Quoted 8 lines
fs/ecryptfs/ecryptfs_kernel.h +8
-0+ +size_t ecryptfs_get_key_size_to_enc_data ( +struct ecryptfs_crypt_stat *crypt_stat); + +size_t ecryptfs_get_key_size_to_store_key ( +struct ecryptfs_crypt_stat *crypt_stat); + +size_t ecryptfs_get_key_size_to_restore_key(size_t
stored_key_size,Quoted 18 lines
Quoted 18 lines
Quoted 9 lines
Quoted 9 lines
fs/ecryptfs/ecryptfs_kernel.h +5
-0+ const char *cipher); + +bool ecryptfs_check_space_for_salt (const size_t key_size, +const size_t salt_size); +#endif /* #ifndef ECRYPTFS_KERNEL_H */ fs/ecryptfs/events.c new file +0
-0Quoted 8 lines
fs/ecryptfs/events.c line 5
Quoted 8 lines
fs/ecryptfs/events.c new file +5
-0@@ -0,0 +1,361 @@ +/** +* eCryptfs: Linux filesystem encryption layer +* Copyright (c) 2015, The Linux Foundation. All rights reserved. +* +* This program is free software; you can redistribute it and/or modify
Quoted 5 lines
Quoted 5 lines
Quoted 1 line
fs/ecryptfs/events.c line 6
Quoted 1 line
fs/ecryptfs/events.c new file +1
-0+* it under the terms of the GNU General Public License version 2 and
Quoted 3 lines
Quoted 3 lines
fs/ecryptfs/events.c new file +3
-0+* only version 2 as published by the Free Software Foundation. +* +* This program is distributed in the hope that it will be useful,
Quoted 108 lines
Quoted 108 lines
Quoted 108 lines
Quoted 108 lines
fs/ecryptfs/events.c new file +108
-0+* but WITHOUT ANY WARRANTY; without even the implied warranty of +* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +* GNU General Public License for more details. +*/ + +#include <linux/string.h> +#include <linux/ecryptfs.h> +#include <linux/mutex.h> +#include <linux/types.h> +#include <linux/slab.h> +#include <linux/pagemap.h> +#include <linux/random.h> +#include "ecryptfs_kernel.h" + +static DEFINE_MUTEX (events_mutex); +static struct ecryptfs_events *events_ptr; +static int handle; + +void ecryptfs_free_events (void ) +{ +mutex_lock (&events_mutex); +if (events_ptr !=NULL ) { +kfree (events_ptr); + events_ptr =NULL ; + } + +mutex_unlock (&events_mutex); +} + +/** +* Register to ecryptfs events, by passing callback +* functions to be called upon events occurrence. +* The function returns a handle to be passed +* to unregister function. +*/ +int ecryptfs_register_to_events (struct ecryptfs_events *ops) +{ +int ret_value =0 ; + +if (!ops) +return -EINVAL ; + +mutex_lock (&events_mutex); + +if (events_ptr !=NULL ) { +ecryptfs_printk (KERN_ERR , +"already registered!\n" ); + ret_value = -EPERM ; + goto out; + } + events_ptr = +kzalloc (sizeof (struct ecryptfs_events ),GFP_KERNEL ); + +if (!events_ptr) { +ecryptfs_printk (KERN_ERR ,"malloc failure\n" ); + ret_value = -ENOMEM ; + goto out; + } +/* copy the callbacks */ + events_ptr->open_cb = ops->open_cb; + events_ptr->release_cb = ops->release_cb; + events_ptr->encrypt_cb = ops->encrypt_cb; + events_ptr->decrypt_cb = ops->decrypt_cb; + events_ptr->is_cipher_supported_cb = + ops->is_cipher_supported_cb; + events_ptr->is_hw_crypt_cb = ops->is_hw_crypt_cb; + events_ptr->get_salt_key_size_cb = ops->get_salt_key_size_cb; + +get_random_bytes (&handle,sizeof (handle)); + ret_value = handle; + +out: +mutex_unlock (&events_mutex); +return ret_value; +} + +/** +* Unregister from ecryptfs events. +*/ +int ecryptfs_unregister_from_events (int user_handle) +{ +int ret_value =0 ; + +mutex_lock (&events_mutex); + +if (!events_ptr) { + ret_value = -EINVAL ; + goto out; + } +if (user_handle != handle) { + ret_value =ECRYPTFS_INVALID_EVENTS_HANDLE ; + goto out; + } + +kfree (events_ptr); + events_ptr =NULL ; + +out: +mutex_unlock (&events_mutex); +return ret_value; +} + +/** +* This function decides whether the passed file offset +* belongs to ecryptfs metadata or not. +* The caller must pass ecryptfs data, which was received in one +* of the callback invocations. +*/ Quoted 8 lines
Quoted 8 lines
Quoted 8 lines
fs/ecryptfs/events.c
Quoted 8 lines
fs/ecryptfs/events.c new file +8
-0+bool ecryptfs_is_page_in_metadata (void *data,pgoff_t offset) +{ + +struct ecryptfs_crypt_stat *stat =NULL ; +bool ret = true; + +if (!data) { + ecryptfs_printk(KERN_ERR, "ecryptfs_is_page_in_metadata:invalid
Quoted 144 lines
Quoted 144 lines
data
Quoted 143 lines
Quoted 143 lines
parameter\n");
fs/ecryptfs/events.c new file +142
-0+ ret = false; + goto end; + } + stat = (struct ecryptfs_crypt_stat *)data; + +if (stat->flags &ECRYPTFS_METADATA_IN_XATTR ) { + ret = false; + goto end; + } + +if (offset >= (stat->metadata_size/PAGE_CACHE_SIZE )) { + ret = false; + goto end; + } +end: +return ret; +} + +/** +* Given two ecryptfs data, the function +* decides whether they are equal. +*/ +inline bool ecryptfs_is_data_equal (void *data1,void *data2) +{ +/* pointer comparison*/ +return data1 == data2; +} + +/** +* Given ecryptfs data, the function +* returns appropriate key size. +*/ +size_t ecryptfs_get_key_size (void *data) +{ + +struct ecryptfs_crypt_stat *stat =NULL ; + +if (!data) +return 0 ; + + stat = (struct ecryptfs_crypt_stat *)data; +return stat->key_size; +} + +/** +* Given ecryptfs data, the function +* returns appropriate salt size. +* +* !!! crypt_stat cipher name and mode must be initialized +*/ +size_t ecryptfs_get_salt_size (void *data) +{ +struct ecryptfs_crypt_stat *stat =NULL ; +unsigned char final[2 *ECRYPTFS_MAX_CIPHER_NAME_SIZE +1 ]; + +if (!data) { +ecryptfs_printk (KERN_ERR , +"ecryptfs_get_salt_size: invalid data parameter\n" ); +return 0 ; + } + + stat = (struct ecryptfs_crypt_stat *)data; +return ecryptfs_get_salt_size_for_cipher ( +ecryptfs_get_full_cipher (stat->cipher, + stat->cipher_mode, + final,sizeof (final))); + +} + +/** +* Given ecryptfs data, the function +* returns appropriate cipher. +*/ +const unsigned char *ecryptfs_get_cipher (void *data) +{ +unsigned char final[2 *ECRYPTFS_MAX_CIPHER_NAME_SIZE +1 ]; +struct ecryptfs_crypt_stat *stat =NULL ; + +if (!data) { +ecryptfs_printk (KERN_ERR , +"ecryptfs_get_cipher: invalid data parameter\n" ); +return NULL ; + } + stat = (struct ecryptfs_crypt_stat *)data; +return ecryptfs_get_full_cipher (stat->cipher, stat->cipher_mode, + final,sizeof (final)); +} + +/** +* Given ecryptfs data, the function +* returns file encryption key. +*/ +const unsigned char *ecryptfs_get_key (void *data) +{ + +struct ecryptfs_crypt_stat *stat =NULL ; + +if (!data) { +ecryptfs_printk (KERN_ERR , +"ecryptfs_get_key: invalid data parameter\n" ); +return NULL ; + } + stat = (struct ecryptfs_crypt_stat *)data; +return stat->key; +} + +/** +* Given ecryptfs data, the function +* returns file encryption salt. +*/ +const unsigned char *ecryptfs_get_salt (void *data) +{ +struct ecryptfs_crypt_stat *stat =NULL ; + +if (!data) { +ecryptfs_printk (KERN_ERR , +"ecryptfs_get_salt: invalid data parameter\n" ); +return NULL ; + } + stat = (struct ecryptfs_crypt_stat *)data; +return stat->key +ecryptfs_get_salt_size (data); +} + +/** +* Returns ecryptfs events pointer +*/ +inline struct ecryptfs_events *get_events (void ) +{ +return events_ptr; +} + +/** +* If external crypto module requires salt in addition to key, +* we store it as part of key array (if there is enough space) +* Checks whether a salt key can fit into array allocated for +* regular key +*/ +bool ecryptfs_check_space_for_salt (const size_t key_size, +const size_t salt_size) +{ +if ((salt_size + key_size) >ECRYPTFS_MAX_KEY_BYTES ) +return false;Quoted 8 lines
Quoted 8 lines
Quoted 8 lines
Quoted 8 lines
fs/ecryptfs/events.c new file +6
-0+ +return true; +} + +/* +* If there is salt that is used by external crypto module, it is stored
+ * in the same array where regular key is. Salt is going to beused
Quoted 15 lines
Quoted 15 lines
by
Quoted 1 line
Quoted 1 line
+ * external crypto module only, so for all internal crypto
operations
Quoted 12 lines
Quoted 12 lines
salt
+ * should be ignored.
+ *
+ * Get key size in cases where it is going to be used for data encryption
+ * or for all other general purposes
+ */fs/ecryptfs/events.c new file +5
-0+size_t ecryptfs_get_key_size_to_enc_data ( +struct ecryptfs_crypt_stat *crypt_stat) +{ +if (!crypt_stat) +return 0 ;Quoted 8 lines
Quoted 8 lines
Quoted 8 lines
Quoted 8 lines
fs/ecryptfs/events.c new file +6
-0+ +return crypt_stat->key_size; +} + +/* +* If there is salt that is used by external crypto module, it is stored
+ * in the same array where regular key is. Salt is going to beused
Quoted 9 lines
Quoted 9 lines
by
Quoted 1 line
Quoted 1 line
+ * external crypto module only, but we still need to save and
restore
Quoted 6 lines
Quoted 6 lines
it
+ * (in encrypted form) as part of ecryptfs header along with the regular
+ * key.
+ *
+ * Get key size in cases where it is going to be storedpersistently
Quoted 18 lines
Quoted 18 lines
Quoted 6 lines
Quoted 6 lines
+ *
+ * !!!crypt_statcipher name and mode must be initialized
+ */fs/ecryptfs/events.c new file +3
-0+size_t ecryptfs_get_key_size_to_store_key ( +struct ecryptfs_crypt_stat *crypt_stat) +{Quoted 8 lines
fs/ecryptfs/events.c
Quoted 8 lines
fs/ecryptfs/events.c new file +8
-0+size_t salt_size =0 ; + +if (!crypt_stat) +return 0 ; + + salt_size =ecryptfs_get_salt_size (crypt_stat); + + if (!ecryptfs_check_space_for_salt(crypt_stat->key_size,
salt_size)) {Quoted 2 lines
fs/ecryptfs/events.c
Quoted 2 lines
fs/ecryptfs/events.c new file +2
-0+ ecryptfs_printk(KERN_WARNING, + "ecryptfs_get_key_size_to_store_key: not enough space forsalt\n");
Quoted 10 lines
Quoted 10 lines
Quoted 10 lines
Quoted 10 lines
fs/ecryptfs/events.c new file +8
-0+return crypt_stat->key_size; + } + +return crypt_stat->key_size + salt_size; +} + +/* +* If there is salt that is used by external crypto module, it is stored
+ * in the same array where regular key is. Salt is going to beused
Quoted 9 lines
Quoted 9 lines
by
Quoted 1 line
Quoted 1 line
+ * external crypto module only, but we still need to save and
restore
Quoted 6 lines
Quoted 6 lines
6 lines not shown · raw message
Quoted 5 lines
5 lines not shown · raw message
796 lines not shown · raw message
Part of this message is not shown here. View the raw message.
Quoted 14 lines · On Wed, Nov 11, 2015 at 12:03:35PM -0000, [email protected] wrote:
Quoted 7 lines · On 2015-11-10 15:20:59, [email protected] wrote:
Quoted 4 lines
This is a hardware inline accelerator, meaning that it operates on much lower layer, block layer and device driver layer. The HW encrypts plain requests sent from block layer directly, thus doing it much more efficiently rather than using crypto API.
Quoted 6 lines
I feel like basing this on eCryptfs is an odd choice. The overhead of setting up an eCryptfs mount (and requiring
CAP_SYS_ADMIN) and the duplication of page cache and dentry cache entries (for upper and lower caches) seems like considerable baggage for such a nifty, new hardware feature.
Quoted 8 lines
First of all, one of the leading companies on the mobile market uses eCryptfs as a solution for their file-based-encryption feature and they use it along with HW crypto engine. I believe we will soon see interest from additional companies.
Secondly, eCryptfs is convenient in many ways for implementing file-based-encryption and there are not so many good alternatives as of today.
EXT4 and F2FS both have native file-based encryption capabilities. They don't suffer from a lot of the overhead and page ballooning issues that eCryptfs has. Nobody should be putting development effort toward using eCryptfs on top of EXT4 or F2FS.
There's clear value in enabling inline hardware encryption on mobile platforms. However I don't think trying to wedge it in via a stacked file system is a good long-term strategy. We should be moving toward integrating inline hardware encryption into file systems that are deployed in those environments.
Given the amount of code duplication between EXT4 and F2FS encryption, we should also be thinking about more general support for encryption at the VFS/MM layer.
Quoted 1,967 lines
You are right regarding overhead, page duplication, etc., however enabling eCryptfs to work with HW encryption still makes it a very efficient solution. Also, it is not just inline HW crypto engine, any HW crypto engine operates more efficiently while working on long chunks of data. Our suggested solution uses the existing block layer (that is part of the standard Linux storage stack) feature that gathers number of block to into one request and then encrypt/decrypt the data, whereas eCryptfs can only perform the crypto operation on single pages. This feature was tested on our platforms and demonstrated very significance performance improvement comparing to existing SW based solutions
Quoted 19 lines
Quoted 6 lines
In order to use such HW efficiently with eCryptfs, eCryptfs encryption has
to be canceled and it will need to call for external module instead that will do the correct marking for the blocks to distinguish between those that need to be encrypted by the HW from those that do not need.Quoted 6 lines
We are considering posting the code that will call
ecryptfs_register_to_events()as a separate patch, but haven't done so yet. It is HW specific.
Currently we are only proposing framework change so that it can allow for
external modules to be connectedIn that case, I cannot accept this patch. I will have no way to test the external module functionality and will not be able to make changes to that area of the code because it will not be possible for me to fix anything that I've broken. I won't even be able to know if I've broken anything.
Quoted 6 lines
If you are able to upstream the external module code, then I'll do a proper review of this patch. I should note that I've only glanced at the patch but if feels like it should be broken up into smaller, easier to review patches before it can be properly reviewed.
Tyler
We can upstream the external module code, however as I mentioned this module is specific for our HW, so you won't be able to test it either. However we can pretty easily turn it into some dummy module for testing purposes only that will just do prints instead of HW specific calls. Would this be sufficient ? If so, where in project tree should I put it ?
Quoted 1,910 lines
Quoted 1,910 lines
Quoted 6 lines · On 2015-11-09 20:56:02, [email protected] wrote:
Quoted 3 lines
Hello, Tyler
I'll try to provide more detailed explanation, should it be
satisfactory
Quoted 5 lines
Quoted 5 lines
enough I will update the patch description.
The problem with current eCryptfs is that it has total control on how and
when the encryption is performed and this control can't be altered.One
Quoted 8 lines
Quoted 7 lines
example when this can be a problem is when we want to utilize an underlying inline HW encryption engine which allows encrypting blocks 'on
the fly' as they are being written to the storage. In such a case relevant
blocks just need to be marked as 'should be encrypted'. No actual encryption should be done by eCryptfs as it will be much slower.Quoted 5 lines
Is this a hardware crypto accelerator? If so, why not create a crypto api driver so all subsystems can take advantage of the acceleration instead of baking support into individual subsystems?
Quoted 1 line
The provided framework allows transferring this control (if needed)
to
Quoted 5 lines
Quoted 5 lines
some external module which will do the encryption itfelf or just mark the
appropriate blocks.There is no caller for
ecryptfs_register_to_events()since thischange
Quoted 10 lines
Quoted 3 lines
only provides framework, it doesn't provide the module itself, the module
could be HW dependent.Will the code that you plan to call
ecryptfs_register_to_events()be upstream? If so, have you posted it?Tyler
Quoted 1 line
Regarding the mounting option, it merely serves as example of new
cipher
Quoted 2 lines
Quoted 2 lines
mode that can be served by registered module.
There is a special callback function that should be implemented bythe
Quoted 1 line
Quoted 1 line
registered module that tells whether a particular cipher is supported
by
Quoted 5 lines
Quoted 5 lines
it :
is_cipher_supported_cb()The mounting option itself is not necessary, I can remove it
Quoted 8 lines
Quoted 8 lines
Quoted 8 lines
Hello Andrey!
Quoted 5 lines · Andrey Markovytch
From: Andrey Markovytch <[email protected]>
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
Quoted 1 line
Quoted 1 line
Quoted 1 line
Quoted 1 line
and provide alternative encryption mechanism and also decide
whether
Quoted 1 line
Quoted 1 line
Quoted 1 line
Quoted 1 line
encryption should be performed at all, or deferred to a later
stage
Quoted 7 lines
Quoted 7 lines
via
Quoted 2 lines
Quoted 2 lines
the inline HW engine.
Additional cipher option was introduced to support the HW/externalmode
Quoted 3 lines
Quoted 2 lines
under that name of "aes-xts". If no external module has registered to support this cipher, eCryptfs will fall back to the usual "aes"
Quoted 7 lines
Quoted 7 lines
Quoted 1 line
What is "HW/external mode"? There's no description in the commit
message
Quoted 1 line
and
ecryptfs_register_to_events()does not have a caller so I'm notsure
Quoted 3 lines
of the purpose. Please provide more context.
Despite not yet understanding the purpose of this patch, I think
that
Quoted 4 lines
Quoted 4 lines
I
Quoted 1 line
can safely say that "aes-xts" is not an appropriate mount option to
use
Quoted 1 line
when enabling this mode. eCryptfs may support XTS mode one day,
using
Quoted 1 line
Quoted 1 line
Quoted 1 line
the Crypto API, so "HW/external mode" should not own the mount
option.
Quoted 40 lines
Quoted 40 lines
Quoted 40 lines
Tyler
Quoted 37 lines
Signed-off-by Lina Zarivach <[email protected]>Signed-off-by Andrey Markovytch <[email protected]>--- 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.cfs/ecryptfs/Makefile +2
-2@@ -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.oQuoted 2 lines
Quoted 2 lines
Quoted 2 lines
Review
Quoted 2 lines
ecryptfs-$(CONFIG_ECRYPT_FS_MESSAGING) += messaging.o miscdev.o
(quoted diff) +0
-0
b/fs/ecryptfs/caches_utils.cQuoted 10 lines
Quoted 10 lines
Quoted 9 lines
fs/ecryptfs/caches_utils.c line 4
Quoted 9 lines
new file mode 100644
index 0000000..c599c96fs/ecryptfs/caches_utils.c new file +4
-0@@ -0,0 +1,78 @@ +/* +* Copyright (c) 2015, The Linux Foundation. All rights reserved. +* +* This program is free software; you can redistribute it and/or modify
Quoted 5 lines
Quoted 5 lines
Quoted 1 line
fs/ecryptfs/caches_utils.c line 5
Quoted 1 line
fs/ecryptfs/caches_utils.c new file +1
-0+* it under the terms of the GNU General Public License version 2 and
Quoted 3 lines
Quoted 3 lines
fs/ecryptfs/caches_utils.c new file +3
-0+* only version 2 as published by the Free Software Foundation. +* +* This program is distributed in the hope that it will be useful,
Quoted 131 lines
Quoted 131 lines
Quoted 6 lines
Quoted 6 lines
fs/ecryptfs/caches_utils.c new file +6
-0+* but WITHOUT ANY WARRANTY; without even the implied warranty of +* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +* GNU General Public License for more details. +*/ + +#include <linux/kernel.h> Quoted 8 lines
fs/ecryptfs/caches_utils.c line 22
Quoted 8 lines
fs/ecryptfs/caches_utils.c new file +8
-0+#include <linux/fs.h> +#include <linux/spinlock.h> +#include <linux/pagemap.h> +#include <linux/pagevec.h> + +#include "../internal.h" + +void ecryptfs_drop_pagecache_sb(struct super_block *sb, void*unused)
Quoted 116 lines
Quoted 116 lines
fs/ecryptfs/caches_utils.c new file +56
-0+{ +struct inode *inode, *toput_inode =NULL ; + +spin_lock (&sb->s_inode_list_lock); +list_for_each_entry (inode, &sb->s_inodes, i_sb_list) { +spin_lock (&inode->i_lock); +if ((inode->i_state & (I_FREEING |I_WILL_FREE |I_NEW )) || + (inode->i_mapping->nrpages ==0 )) { +spin_unlock (&inode->i_lock); +continue ; + } +__iget (inode); +spin_unlock (&inode->i_lock); +spin_unlock (&sb->s_inode_list_lock); + +invalidate_mapping_pages (inode->i_mapping,0 ,-1 ); +iput (toput_inode); + toput_inode = inode; + +spin_lock (&sb->s_inode_list_lock); + } +spin_unlock (&sb->s_inode_list_lock); +iput (toput_inode); +} + +void clean_inode_pages (struct address_space *mapping, +pgoff_t start,pgoff_t end) +{ +struct pagevec pvec; +pgoff_t index = start; +int i; + +pagevec_init (&pvec,0 ); +while (index <= end &&pagevec_lookup (&pvec, mapping, index, +min (end - index, + (pgoff_t )PAGEVEC_SIZE -1 ) +1 )) { +for (i =0 ; i <pagevec_count (&pvec); i++) { +struct page *page = pvec.pages[i]; + +/* We rely upon deletion +* not changing page->index +*/ + index = page->index; +if (index > end) +break ; +if (!trylock_page (page)) +continue ; +WARN_ON (page->index != index); +zero_user (page,0 ,PAGE_CACHE_SIZE ); +unlock_page (page); + } +pagevec_release (&pvec); +cond_resched (); + index++; + } +}fs/ecryptfs/crypto.c +3
-2@@ -35,6 +35,7 @@#include <linux/scatterlist.h> #include <linux/slab.h> #include <asm/unaligned.h> +#include <linux/ecryptfs.h> #include "ecryptfs_kernel.h" #define DECRYPT 0@@ -350,9 +351,9 @@ static int crypt_scatterlist(struct ecryptfs_crypt_stat *crypt_stat, || !(crypt_stat->flags & ECRYPTFS_STRUCT_INITIALIZED));if (unlikely (ecryptfs_verbosity >0 )) {ecryptfs_printk (KERN_DEBUG ,"Key size [%zd]; key:\n" , - crypt_stat->key_size); +ecryptfs_get_key_size_to_enc_data (crypt_stat));ecryptfs_dump_hex (crypt_stat->key, - crypt_stat->key_size); +ecryptfs_get_key_size_to_enc_data (crypt_stat)); }fs/ecryptfs/crypto.c +1
-1init_completion (&ecr.completion);@@ -371,7 +372,7 @@ static int crypt_scatterlist(struct ecryptfs_crypt_stat *crypt_stat,/* Consider doing this once, when the file is opened */ if (!(crypt_stat->flags & ECRYPTFS_KEY_SET)) { rc =crypto_ablkcipher_setkey (crypt_stat->tfm, crypt_stat->key, - crypt_stat->key_size); +ecryptfs_get_key_size_to_enc_data (crypt_stat));if (rc) { ecryptfs_printk(KERN_ERR,fs/ecryptfs/crypto.c +20
-0"Error setting key; rc = [%d]\n",@@ -466,6 +467,31 @@ out:return rc; } +static void init_ecryption_parameters (bool *hw_crypt,bool *cipher_supported, +struct ecryptfs_crypt_stat *crypt_stat) +{ +unsigned char final[2 *ECRYPTFS_MAX_CIPHER_NAME_SIZE +1 ]; + +if (!hw_crypt || !cipher_supported) +return ; + + *cipher_supported = false; + *hw_crypt = false; + +if (get_events () &&get_events ()->is_cipher_supported_cb) { + *cipher_supported = +get_events ()->is_cipher_supported_cb ( +ecryptfs_get_full_cipher (crypt_stat->cipher, + crypt_stat->cipher_mode, final,sizeof (final))); +if (*cipher_supported) { +/* we should apply external algorythm +* assume that is_hw_crypt() cbck is supplied +*/ Quoted 8 lines
Quoted 8 lines
Quoted 8 lines
Quoted 8 lines
fs/ecryptfs/crypto.c +5
-0+ *hw_crypt =get_events ()->is_hw_crypt_cb (); + } + } +} +/** * ecryptfs_encrypt_page * @page: Page mapped from the eCryptfs inode for the file;
contains
Quoted 18 lines
Quoted 18 lines
Quoted 18 lines
Quoted 18 lines
fs/ecryptfs/crypto.c +7
-0@@ -491,11 +517,18 @@ int ecryptfs_encrypt_page(struct page *page)loff_t extent_offset;loff_t lower_offset;int rc =0 ; +bool is_hw_crypt; +bool is_cipher_supported; + ecryptfs_inode = page->mapping->host; crypt_stat = &(ecryptfs_inode_to_private (ecryptfs_inode)->crypt_stat);BUG_ON (!(crypt_stat->flags &ECRYPTFS_ENCRYPTED )); + +init_ecryption_parameters (&is_hw_crypt, + &is_cipher_supported, crypt_stat); + enc_extent_page =alloc_page (GFP_USER );if (!enc_extent_page) {Quoted 8 lines
Quoted 8 lines
Quoted 8 lines
fs/ecryptfs/crypto.c int ecryptfs_encrypt_page(struct page *page) · line 508
Quoted 8 lines
fs/ecryptfs/crypto.c +0
-3rc = -ENOMEM ;@@ -503,24 +536,51 @@ int ecryptfs_encrypt_page(struct page *page) "encrypted extent\n"); goto out; } - - for (extent_offset = 0; - extent_offset < (PAGE_CACHE_SIZE /
crypt_stat->extent_size);Quoted 72 lines
Quoted 72 lines
Quoted 72 lines
Quoted 72 lines
fs/ecryptfs/crypto.c +40
-10- extent_offset++) { - rc =crypt_extent (crypt_stat, enc_extent_page, page, - extent_offset,ENCRYPT ); -if (rc) { -printk (KERN_ERR "%s: Error encrypting extent; " -"rc = [%d]\n" , __func__, rc); - goto out; - } +if (is_hw_crypt) { +/* no need for encryption */ + }else { +for (extent_offset =0 ; + extent_offset < + (PAGE_CACHE_SIZE / crypt_stat->extent_size); + extent_offset++) { + +if (is_cipher_supported) { +if (!get_events ()->encrypt_cb) { + rc = -EPERM ; + goto out; + } + rc =get_events ()->encrypt_cb (page, + enc_extent_page, +ecryptfs_inode_to_lower ( + ecryptfs_inode), + extent_offset); + }else { + rc =crypt_extent (crypt_stat, + enc_extent_page, page, + extent_offset,ENCRYPT ); + } +if (rc) { +ecryptfs_printk (KERN_ERR , +"%s: Error encrypting; rc = [%d]\n" , + __func__, rc); + goto out; + } + } } lower_offset =lower_offset_for_page (crypt_stat, page); - enc_extent_virt =kmap (enc_extent_page); +if (is_hw_crypt) + enc_extent_virt =kmap (page); +else + enc_extent_virt =kmap (enc_extent_page); + rc =ecryptfs_write_lower (ecryptfs_inode, enc_extent_virt, lower_offset,PAGE_CACHE_SIZE ); -kunmap (enc_extent_page); +if (!is_hw_crypt) +kunmap (enc_extent_page); +else +kunmap (page); +if (rc <0 ) { ecryptfs_printk(KERN_ERR,fs/ecryptfs/crypto.c +2
-0"Error attempting to write lower page; rc = [%d]\n",@@ -559,6 +619,8 @@ int ecryptfs_decrypt_page(struct page *page)unsigned long extent_offset;loff_t lower_offset;int rc =0 ; +bool is_cipher_supported; +bool is_hw_crypt; ecryptfs_inode = page->mapping->host; crypt_stat =@@ -577,13 +639,33 @@ int ecryptfs_decrypt_page(struct page *page) goto out; }Quoted 10 lines
Quoted 10 lines
Quoted 10 lines
fs/ecryptfs/crypto.c int ecryptfs_decrypt_page(struct page *page) · line 651
Quoted 10 lines
fs/ecryptfs/crypto.c +8
-0+init_ecryption_parameters (&is_hw_crypt, + &is_cipher_supported, crypt_stat); + +if (is_hw_crypt) { + rc =0 ; +return rc; + } + for (extent_offset = 0; extent_offset < (PAGE_CACHE_SIZE /
crypt_stat->extent_size);Quoted 90 lines
Quoted 90 lines
Quoted 47 lines
Quoted 47 lines
fs/ecryptfs/crypto.c +15
-3extent_offset++) { - rc =crypt_extent (crypt_stat, page, page, +if (is_cipher_supported) { +if (!get_events ()->decrypt_cb) { + rc = -EPERM ; + goto out; + } + + rc =get_events ()->decrypt_cb (page, page, +ecryptfs_inode_to_lower (ecryptfs_inode), + extent_offset); + + }else + rc =crypt_extent (crypt_stat, page, page, extent_offset,DECRYPT ); +if (rc) { -printk (KERN_ERR "%s: Error encrypting extent; " +ecryptfs_printk (KERN_ERR ,"%s: Error decrypting extent;" "rc = [%d]\n" , __func__, rc); goto out; }@@ -612,7 +694,7 @@ int ecryptfs_init_crypt_ctx(struct ecryptfs_crypt_stat *crypt_stat)"Initializing cipher [%s]; strlen = [%d]; " "key_size_bits = [%zd]\n" , crypt_stat->cipher, (int )strlen (crypt_stat->cipher), - crypt_stat->key_size << 3); + ecryptfs_get_key_size_to_enc_data(crypt_stat) << 3);mutex_lock (&crypt_stat->cs_tfm_mutex);if (crypt_stat->tfm) {fs/ecryptfs/crypto.c +1
-1rc =0 ;@@ -694,7 +776,7 @@ int ecryptfs_compute_root_iv(struct ecryptfs_crypt_stat *crypt_stat) goto out; } rc =ecryptfs_calculate_md5 (dst, crypt_stat, crypt_stat->key, - crypt_stat->key_size); +ecryptfs_get_key_size_to_enc_data (crypt_stat));if (rc) { ecryptfs_printk(KERN_WARNING, "Error attempting to compute "fs/ecryptfs/crypto.c +0
-0"MD5 while generating root IV\n");@@ -721,6 +803,35 @@ static void ecryptfs_generate_new_key(struct ecryptfs_crypt_stat *crypt_stat) } }Quoted 15 lines
Quoted 5 lines
fs/ecryptfs/crypto.c +4
-0+static int ecryptfs_generate_new_salt (struct ecryptfs_crypt_stat *crypt_stat) +{ +size_t salt_size =0 ; +unsigned char final[2 *ECRYPTFS_MAX_CIPHER_NAME_SIZE +1 ];fs/ecryptfs/crypto.c static void ecryptfs_generate_new_key(struct · line 821
Quoted 10 lines
fs/ecryptfs/crypto.c +10
-0+ + salt_size =ecryptfs_get_salt_size_for_cipher ( +ecryptfs_get_full_cipher (crypt_stat->cipher, + crypt_stat->cipher_mode, + final,sizeof (final))); + +if (salt_size ==0 ) +return 0 ; + + if (!ecryptfs_check_space_for_salt(crypt_stat->key_size,
salt_size)) {Quoted 6 lines
fs/ecryptfs/crypto.c static void ecryptfs_generate_new_key(struct · line 827
Quoted 6 lines
fs/ecryptfs/crypto.c +6
-0+ecryptfs_printk (KERN_WARNING ,"not enough space for salt\n" ); + crypt_stat->flags |=ECRYPTFS_SECURITY_WARNING ; +return -EINVAL ; + } + + get_random_bytes(crypt_stat->key + crypt_stat->key_size,
salt_size);Quoted 20 lines
Quoted 20 lines
fs/ecryptfs/crypto.c +9
-0+if (unlikely (ecryptfs_verbosity >0 )) { +ecryptfs_printk (KERN_DEBUG ,"Generated new session salt:\n" ); +ecryptfs_dump_hex (crypt_stat->key + crypt_stat->key_size, + salt_size); + } + +return 0 ; +} +/** *
ecryptfs_copy_mount_wide_flags_to_inode_flags
* @crypt_stat: The inode's cryptographic contextfs/ecryptfs/crypto.c +0
-1@@ -823,7 +934,6 @@ int ecryptfs_new_file_context(struct inode *ecryptfs_inode)struct ecryptfs_mount_crypt_stat *mount_crypt_stat = &ecryptfs_superblock_to_private ( ecryptfs_inode->i_sb)->mount_crypt_stat; -int cipher_name_len;int rc =0 ;Quoted 1 line
Quoted 1 line
Quoted 1 line
Quoted 1 line
ecryptfs_set_default_crypt_stat_vals(crypt_stat,
mount_crypt_stat);Quoted 113 lines
Quoted 113 lines
Quoted 67 lines
Quoted 67 lines
fs/ecryptfs/crypto.c +9
-5@@ -837,15 +947,19 @@ int ecryptfs_new_file_context(struct inode *ecryptfs_inode) "to the inode key sigs; rc = [%d]\n", rc); goto out; } - cipher_name_len = -strlen (mount_crypt_stat->global_default_cipher_name); -memcpy (crypt_stat->cipher, +strlcpy (crypt_stat->cipher, mount_crypt_stat->global_default_cipher_name, - cipher_name_len); - crypt_stat->cipher[cipher_name_len] ='\0' ; +sizeof (crypt_stat->cipher)); + +strlcpy (crypt_stat->cipher_mode, + mount_crypt_stat->global_default_cipher_mode, +sizeof (crypt_stat->cipher_mode)); + crypt_stat->key_size = mount_crypt_stat->global_default_cipher_key_size;ecryptfs_generate_new_key (crypt_stat); +ecryptfs_generate_new_salt (crypt_stat); + rc =ecryptfs_init_crypt_ctx (crypt_stat);if (rc)fs/ecryptfs/crypto.c +7
-1ecryptfs_printk(KERN_ERR, "Error initializing cryptographic "@@ -971,7 +1085,8 @@ ecryptfs_cipher_code_str_map[] = { {"twofish", RFC2440_CIPHER_TWOFISH}, {"cast6", RFC2440_CIPHER_CAST_6}, {"aes", RFC2440_CIPHER_AES_192}, - {"aes", RFC2440_CIPHER_AES_256} + {"aes", RFC2440_CIPHER_AES_256}, + {"aes_xts", RFC2440_CIPHER_AES_XTS_256} };/** @@ -999,6 +1114,11 @@ u8 ecryptfs_code_for_cipher_string(char *cipher_name, size_t key_bytes)case 32 : code =RFC2440_CIPHER_AES_256 ; } + }else if (strcmp (cipher_name,"aes_xts" ) ==0 ) { +switch (key_bytes) { +case 32 : + code =RFC2440_CIPHER_AES_XTS_256 ; + } }else {for (i =0 ; i <ARRAY_SIZE (ecryptfs_cipher_code_str_map); i++)fs/ecryptfs/crypto.c +12
-0if (strcmp (cipher_name, map[i].cipher_str) ==0 ) {@@ -1038,9 +1158,24 @@ int ecryptfs_read_and_validate_header_region(struct inode *inode) u8 file_size[ECRYPTFS_SIZE_AND_MARKER_BYTES];u8 *marker = file_size +ECRYPTFS_FILE_SIZE_BYTES ;int rc; +unsigned int ra_pages_org; +struct file *lower_file =NULL ; + +if (!inode) +return -EIO ; + lower_file =ecryptfs_inode_to_private (inode)->lower_file; +if (!lower_file) +return -EIO ; + +/*disable read a head mechanism for a while */ + ra_pages_org = lower_file->f_ra.ra_pages; + lower_file->f_ra.ra_pages =0 ;Quoted 1 line
fs/ecryptfs/crypto.c int · line 1175
Quoted 1 line
fs/ecryptfs/crypto.c +0
-0rc = ecryptfs_read_lower(file_size, 0,
ECRYPTFS_SIZE_AND_MARKER_BYTES,Quoted 44 lines
Quoted 44 lines
fs/ecryptfs/crypto.c +3
-0inode); + lower_file->f_ra.ra_pages = ra_pages_org; +/* restore read a head mechanism */ +if (rc <ECRYPTFS_SIZE_AND_MARKER_BYTES )return rc >=0 ? -EINVAL : rc;fs/ecryptfs/crypto.c +5
-0rc =ecryptfs_validate_marker (marker);@@ -1430,6 +1565,11 @@ int ecryptfs_read_metadata(struct dentry *ecryptfs_dentry)struct ecryptfs_mount_crypt_stat *mount_crypt_stat = &ecryptfs_superblock_to_private ( ecryptfs_dentry->d_sb)->mount_crypt_stat; +unsigned int ra_pages_org; +struct file *lower_file = +ecryptfs_inode_to_private (ecryptfs_inode)->lower_file; +if (!lower_file) +return -EIO ; ecryptfs_copy_mount_wide_flags_to_inode_flags(crypt_stat,fs/ecryptfs/crypto.c +6
-0mount_crypt_stat);@@ -1441,8 +1581,14 @@ int ecryptfs_read_metadata(struct dentry *ecryptfs_dentry) __func__); goto out; } +/*disable read a head mechanism */ + ra_pages_org = lower_file->f_ra.ra_pages; + lower_file->f_ra.ra_pages =0 ; + rc =ecryptfs_read_lower (page_virt,0 , crypt_stat->extent_size, ecryptfs_inode); + lower_file->f_ra.ra_pages = ra_pages_org;/* restore it back */ +if (rc >=0 ) rc = ecryptfs_read_headers_virt(page_virt, crypt_stat,fs/ecryptfs/crypto.c +0
-0ecryptfs_dentry,fs/ecryptfs/debug.c +0
-0@@ -119,3 +119,16 @@ void ecryptfs_dump_hex(char *data, int bytes)printk ("\n" ); }Quoted 1 line
Quoted 1 line
Quoted 1 line
fs/ecryptfs/debug.c void ecryptfs_dump_hex(char *data, int bytes) · line 122
Quoted 1 line
fs/ecryptfs/debug.c +1
-0+void ecryptfs_dump_salt_hex(char *data, int key_size, char*cipher)
Quoted 100 lines
Quoted 100 lines
Quoted 29 lines
Quoted 29 lines
fs/ecryptfs/debug.c +12
-0+{ +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 +1
-0@@ -245,6 +245,7 @@ struct ecryptfs_crypt_stat {struct mutex cs_tfm_mutex;struct mutex cs_hash_tfm_mutex;struct mutex cs_mutex; +unsigned char cipher_mode[ECRYPTFS_MAX_CIPHER_NAME_SIZE +1 ]; };/* inode private data. */ @@ -267,6 +268,8 @@ struct ecryptfs_dentry_info { }; };Quoted 4 lines
fs/ecryptfs/ecryptfs_kernel.h struct ecryptfs_dentry_info { · line 274
Quoted 4 lines
fs/ecryptfs/ecryptfs_kernel.h +2
-0+ +/** * ecryptfs_global_auth_tok - A key used to encrypt all new files under
Quoted 7 lines
fs/ecryptfs/ecryptfs_kernel.h struct ecryptfs_mount_crypt_stat { · line 351
Quoted 7 lines
the mountpoint
* @flags: Status flagsfs/ecryptfs/ecryptfs_kernel.h +1
-0@@ -345,6 +348,8 @@ struct ecryptfs_mount_crypt_stat {unsigned char global_default_fn_cipher_name[ECRYPTFS_MAX_CIPHER_NAME_SIZE +1 ];char global_default_fnek_sig[ECRYPTFS_SIG_SIZE_HEX +1 ]; +unsigned char global_default_cipher_mode[ECRYPTFS_MAX_CIPHER_NAME_SIZE
Quoted 7 lines
fs/ecryptfs/ecryptfs_kernel.h ecryptfs_dentry_to_lower_path(struct dentry · line 533
Quoted 7 lines
fs/ecryptfs/ecryptfs_kernel.h +1
-0+ + 1]; };/* superblock private data. */ @@ -527,6 +532,53 @@ ecryptfs_dentry_to_lower_path(struct dentry *dentry) return &((struct ecryptfs_dentry_info*)dentry->d_fsdata)->lower_path;
Quoted 17 lines
Quoted 17 lines
fs/ecryptfs/ecryptfs_kernel.h +15
-0} +/** +* Given a cipher and mode strings, the function +* concatenates them to create a new string of +* <cipher>_<mode> format. +*/ +static inline unsigned char *ecryptfs_get_full_cipher ( +unsigned char *cipher,unsigned char *mode, +unsigned char *final,size_t final_size) +{ +memset (final,0 , final_size); + +if (strlen (mode) >0 ) { +snprintf (final, final_size,"%s_%s" , cipher, mode); +return final; + }Quoted 8 lines
fs/ecryptfs/ecryptfs_kernel.h ecryptfs_dentry_to_lower_path(struct dentry · line 558
Quoted 8 lines
fs/ecryptfs/ecryptfs_kernel.h +8
-0+ +return cipher; +} + +/** +* Given a <cipher>[_<mode>] formatted string, the function +* extracts cipher string and/or mode string. +* Note: the passed cipher and/or mode strings will be null-terminated.
Quoted 24 lines
Quoted 24 lines
fs/ecryptfs/ecryptfs_kernel.h +24
-0+*/ +static inline void ecryptfs_parse_full_cipher ( +char *s,char *cipher,char *mode) +{ +char input[2 *ECRYPTFS_MAX_CIPHER_NAME_SIZE +1 +1 ]; +/* +1 for '_'; +1 for '\0' */ +char *p; +char *input_p = input; + +if (s ==NULL || cipher ==NULL ) +return ; + +memset (input,0 ,sizeof (input)); +strlcpy (input, s,sizeof (input)); + + p =strsep (&input_p,"_" ); +strlcpy (cipher, p,ECRYPTFS_MAX_CIPHER_NAME_SIZE +1 ); + + +/* check if mode is specified */ +if (input_p !=NULL && mode !=NULL ) +strlcpy (mode, input_p,ECRYPTFS_MAX_CIPHER_NAME_SIZE +1 ); +} +Quoted 8 lines
Quoted 8 lines
Quoted 8 lines
fs/ecryptfs/ecryptfs_kernel.h int ecryptfs_encrypt_and_encode_filename( · line 630
Quoted 8 lines
fs/ecryptfs/ecryptfs_kernel.h +0
-0#define ecryptfs_printk(type, fmt, arg...) \ __ecryptfs_printk(type "%s: " fmt, __func__, ## arg);
__printf(1, 2)fs/ecryptfs/ecryptfs_kernel.h +1
-0@@ -575,6 +627,7 @@ int ecryptfs_encrypt_and_encode_filename( const char *name, size_t name_size);struct dentry *ecryptfs_lower_dentry (struct dentry *this_dentry);void ecryptfs_dump_hex (char *data,int bytes); +void ecryptfs_dump_salt_hex(char *data, int key_size, char*cipher);
Quoted 7 lines
Quoted 7 lines
Quoted 1 line
fs/ecryptfs/ecryptfs_kernel.h int ecryptfs_encrypt_and_encode_filename( · line 631
Quoted 1 line
fs/ecryptfs/ecryptfs_kernel.h +0
-0int virt_to_scatterlist(const void *addr, int size, structscatterlist
Quoted 3 lines
Quoted 3 lines
fs/ecryptfs/ecryptfs_kernel.h +0
-0*sg, int sg_size);int ecryptfs_compute_root_iv(struct
ecryptfs_crypt_stat
*crypt_stat);Quoted 1 line
fs/ecryptfs/ecryptfs_kernel.h int ecryptfs_set_f_namelen(long *namelen,
Quoted 1 line
fs/ecryptfs/ecryptfs_kernel.h +0
-0@@ -718,4 +771,29 @@ int ecryptfs_set_f_namelen(long *namelen,long
Quoted 10 lines
Quoted 10 lines
Quoted 9 lines
fs/ecryptfs/ecryptfs_kernel.h int ecryptfs_set_f_namelen(long *namelen,
Quoted 9 lines
fs/ecryptfs/ecryptfs_kernel.h +4
-0lower_namelen,int ecryptfs_derive_iv (char *iv,struct ecryptfs_crypt_stat *crypt_stat,loff_t offset); +void clean_inode_pages (struct address_space *mapping, +pgoff_t start,pgoff_t end); + +void ecryptfs_drop_pagecache_sb(struct super_block *sb, void*unused);
Quoted 16 lines
Quoted 8 lines
Quoted 8 lines
Quoted 8 lines
fs/ecryptfs/ecryptfs_kernel.h +8
-0+ +void ecryptfs_free_events (void ); + +void ecryptfs_freepage (struct page *page); + +struct ecryptfs_events *get_events (void ); + +size_t ecryptfs_get_salt_size_for_cipher (const char *cipher);Quoted 8 lines
Quoted 8 lines
fs/ecryptfs/ecryptfs_kernel.h
Quoted 8 lines
fs/ecryptfs/ecryptfs_kernel.h +8
-0+ +size_t ecryptfs_get_key_size_to_enc_data ( +struct ecryptfs_crypt_stat *crypt_stat); + +size_t ecryptfs_get_key_size_to_store_key ( +struct ecryptfs_crypt_stat *crypt_stat); + +size_t ecryptfs_get_key_size_to_restore_key(size_t
stored_key_size,Quoted 18 lines
Quoted 18 lines
Quoted 9 lines
Quoted 9 lines
fs/ecryptfs/ecryptfs_kernel.h +5
-0+ const char *cipher); + +bool ecryptfs_check_space_for_salt (const size_t key_size, +const size_t salt_size); +#endif /* #ifndef ECRYPTFS_KERNEL_H */ fs/ecryptfs/events.c new file +0
-0Quoted 8 lines
fs/ecryptfs/events.c line 5
Quoted 8 lines
fs/ecryptfs/events.c new file +5
-0@@ -0,0 +1,361 @@ +/** +* eCryptfs: Linux filesystem encryption layer +* Copyright (c) 2015, The Linux Foundation. All rights reserved. +* +* This program is free software; you can redistribute it and/or modify
Quoted 5 lines
Quoted 5 lines
Quoted 1 line
fs/ecryptfs/events.c line 6
Quoted 1 line
fs/ecryptfs/events.c new file +1
-0+* it under the terms of the GNU General Public License version 2 and
Quoted 3 lines
Quoted 3 lines
fs/ecryptfs/events.c new file +3
-0+* only version 2 as published by the Free Software Foundation. +* +* This program is distributed in the hope that it will be useful,
Quoted 108 lines
Quoted 108 lines
Quoted 108 lines
Quoted 108 lines
fs/ecryptfs/events.c new file +108
-0+* but WITHOUT ANY WARRANTY; without even the implied warranty of +* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +* GNU General Public License for more details. +*/ + +#include <linux/string.h> +#include <linux/ecryptfs.h> +#include <linux/mutex.h> +#include <linux/types.h> +#include <linux/slab.h> +#include <linux/pagemap.h> +#include <linux/random.h> +#include "ecryptfs_kernel.h" + +static DEFINE_MUTEX (events_mutex); +static struct ecryptfs_events *events_ptr; +static int handle; + +void ecryptfs_free_events (void ) +{ +mutex_lock (&events_mutex); +if (events_ptr !=NULL ) { +kfree (events_ptr); + events_ptr =NULL ; + } + +mutex_unlock (&events_mutex); +} + +/** +* Register to ecryptfs events, by passing callback +* functions to be called upon events occurrence. +* The function returns a handle to be passed +* to unregister function. +*/ +int ecryptfs_register_to_events (struct ecryptfs_events *ops) +{ +int ret_value =0 ; + +if (!ops) +return -EINVAL ; + +mutex_lock (&events_mutex); + +if (events_ptr !=NULL ) { +ecryptfs_printk (KERN_ERR , +"already registered!\n" ); + ret_value = -EPERM ; + goto out; + } + events_ptr = +kzalloc (sizeof (struct ecryptfs_events ),GFP_KERNEL ); + +if (!events_ptr) { +ecryptfs_printk (KERN_ERR ,"malloc failure\n" ); + ret_value = -ENOMEM ; + goto out; + } +/* copy the callbacks */ + events_ptr->open_cb = ops->open_cb; + events_ptr->release_cb = ops->release_cb; + events_ptr->encrypt_cb = ops->encrypt_cb; + events_ptr->decrypt_cb = ops->decrypt_cb; + events_ptr->is_cipher_supported_cb = + ops->is_cipher_supported_cb; + events_ptr->is_hw_crypt_cb = ops->is_hw_crypt_cb; + events_ptr->get_salt_key_size_cb = ops->get_salt_key_size_cb; + +get_random_bytes (&handle,sizeof (handle)); + ret_value = handle; + +out: +mutex_unlock (&events_mutex); +return ret_value; +} + +/** +* Unregister from ecryptfs events. +*/ +int ecryptfs_unregister_from_events (int user_handle) +{ +int ret_value =0 ; + +mutex_lock (&events_mutex); + +if (!events_ptr) { + ret_value = -EINVAL ; + goto out; + } +if (user_handle != handle) { + ret_value =ECRYPTFS_INVALID_EVENTS_HANDLE ; + goto out; + } + +kfree (events_ptr); + events_ptr =NULL ; + +out: +mutex_unlock (&events_mutex); +return ret_value; +} + +/** +* This function decides whether the passed file offset +* belongs to ecryptfs metadata or not. +* The caller must pass ecryptfs data, which was received in one +* of the callback invocations. +*/ Quoted 8 lines
Quoted 8 lines
Quoted 8 lines
fs/ecryptfs/events.c line 125
Quoted 8 lines
fs/ecryptfs/events.c new file +8
-0+bool ecryptfs_is_page_in_metadata (void *data,pgoff_t offset) +{ + +struct ecryptfs_crypt_stat *stat =NULL ; +bool ret = true; + +if (!data) { + ecryptfs_printk(KERN_ERR, "ecryptfs_is_page_in_metadata:invalid
Quoted 144 lines
Quoted 144 lines
data
Quoted 143 lines
Quoted 143 lines
parameter\n");
fs/ecryptfs/events.c new file +142
-0+ ret = false; + goto end; + } + stat = (struct ecryptfs_crypt_stat *)data; + +if (stat->flags &ECRYPTFS_METADATA_IN_XATTR ) { + ret = false; + goto end; + } + +if (offset >= (stat->metadata_size/PAGE_CACHE_SIZE )) { + ret = false; + goto end; + } +end: +return ret; +} + +/** +* Given two ecryptfs data, the function +* decides whether they are equal. +*/ +inline bool ecryptfs_is_data_equal (void *data1,void *data2) +{ +/* pointer comparison*/ +return data1 == data2; +} + +/** +* Given ecryptfs data, the function +* returns appropriate key size. +*/ +size_t ecryptfs_get_key_size (void *data) +{ + +struct ecryptfs_crypt_stat *stat =NULL ; + +if (!data) +return 0 ; + + stat = (struct ecryptfs_crypt_stat *)data; +return stat->key_size; +} + +/** +* Given ecryptfs data, the function +* returns appropriate salt size. +* +* !!! crypt_stat cipher name and mode must be initialized +*/ +size_t ecryptfs_get_salt_size (void *data) +{ +struct ecryptfs_crypt_stat *stat =NULL ; +unsigned char final[2 *ECRYPTFS_MAX_CIPHER_NAME_SIZE +1 ]; + +if (!data) { +ecryptfs_printk (KERN_ERR , +"ecryptfs_get_salt_size: invalid data parameter\n" ); +return 0 ; + } + + stat = (struct ecryptfs_crypt_stat *)data; +return ecryptfs_get_salt_size_for_cipher ( +ecryptfs_get_full_cipher (stat->cipher, + stat->cipher_mode, + final,sizeof (final))); + +} + +/** +* Given ecryptfs data, the function +* returns appropriate cipher. +*/ +const unsigned char *ecryptfs_get_cipher (void *data) +{ +unsigned char final[2 *ECRYPTFS_MAX_CIPHER_NAME_SIZE +1 ]; +struct ecryptfs_crypt_stat *stat =NULL ; + +if (!data) { +ecryptfs_printk (KERN_ERR , +"ecryptfs_get_cipher: invalid data parameter\n" ); +return NULL ; + } + stat = (struct ecryptfs_crypt_stat *)data; +return ecryptfs_get_full_cipher (stat->cipher, stat->cipher_mode, + final,sizeof (final)); +} + +/** +* Given ecryptfs data, the function +* returns file encryption key. +*/ +const unsigned char *ecryptfs_get_key (void *data) +{ + +struct ecryptfs_crypt_stat *stat =NULL ; + +if (!data) { +ecryptfs_printk (KERN_ERR , +"ecryptfs_get_key: invalid data parameter\n" ); +return NULL ; + } + stat = (struct ecryptfs_crypt_stat *)data; +return stat->key; +} + +/** +* Given ecryptfs data, the function +* returns file encryption salt. +*/ +const unsigned char *ecryptfs_get_salt (void *data) +{ +struct ecryptfs_crypt_stat *stat =NULL ; + +if (!data) { +ecryptfs_printk (KERN_ERR , +"ecryptfs_get_salt: invalid data parameter\n" ); +return NULL ; + } + stat = (struct ecryptfs_crypt_stat *)data; +return stat->key +ecryptfs_get_salt_size (data); +} + +/** +* Returns ecryptfs events pointer +*/ +inline struct ecryptfs_events *get_events (void ) +{ +return events_ptr; +} + +/** +* If external crypto module requires salt in addition to key, +* we store it as part of key array (if there is enough space) +* Checks whether a salt key can fit into array allocated for +* regular key +*/ +bool ecryptfs_check_space_for_salt (const size_t key_size, +const size_t salt_size) +{ +if ((salt_size + key_size) >ECRYPTFS_MAX_KEY_BYTES ) +return false;Quoted 8 lines
Quoted 8 lines
Quoted 8 lines
Quoted 8 lines
fs/ecryptfs/events.c new file +6
-0+ +return true; +} + +/* +* If there is salt that is used by external crypto module, it is stored
+ * in the same array where regular key is. Salt is going to beused
Quoted 15 lines
Quoted 15 lines
by
Quoted 1 line
Quoted 1 line
+ * external crypto module only, so for all internal crypto
operations
Quoted 12 lines
Quoted 12 lines
salt
+ * should be ignored.
+ *
+ * Get key size in cases where it is going to be used for data encryption
+ * or for all other general purposes
+ */fs/ecryptfs/events.c new file +5
-0+size_t ecryptfs_get_key_size_to_enc_data ( +struct ecryptfs_crypt_stat *crypt_stat) +{ +if (!crypt_stat) +return 0 ;Quoted 8 lines
Quoted 8 lines
Quoted 8 lines
Quoted 8 lines
fs/ecryptfs/events.c new file +6
-0+ +return crypt_stat->key_size; +} + +/* +* If there is salt that is used by external crypto module, it is stored
+ * in the same array where regular key is. Salt is going to beused
Quoted 9 lines
Quoted 9 lines
by
Quoted 1 line
Quoted 1 line
+ * external crypto module only, but we still need to save and
restore
Quoted 6 lines
Quoted 6 lines
it
+ * (in encrypted form) as part of ecryptfs header along with the regular
+ * key.
+ *
+ * Get key size in cases where it is going to be storedpersistently
Quoted 18 lines
Quoted 18 lines
Quoted 6 lines
Quoted 6 lines
+ *
+ * !!!crypt_statcipher name and mode must be initialized
+ */fs/ecryptfs/events.c new file +3
-0+size_t ecryptfs_get_key_size_to_store_key ( +struct ecryptfs_crypt_stat *crypt_stat) +{Quoted 8 lines
fs/ecryptfs/events.c
Quoted 8 lines
fs/ecryptfs/events.c new file +8
-0+size_t salt_size =0 ; + +if (!crypt_stat) +return 0 ; + + salt_size =ecryptfs_get_salt_size (crypt_stat); + + if (!ecryptfs_check_space_for_salt(crypt_stat->key_size,
salt_size)) {Quoted 2 lines
fs/ecryptfs/events.c
Quoted 2 lines
fs/ecryptfs/events.c new file +2
-0+ ecryptfs_printk(KERN_WARNING, + "ecryptfs_get_key_size_to_store_key: not enough space forsalt\n");
Quoted 10 lines
Quoted 10 lines
Quoted 10 lines
Quoted 10 lines
fs/ecryptfs/events.c new file +8
-0+return crypt_stat->key_size; + } + +return crypt_stat->key_size + salt_size; +} + +/* +* If there is salt that is used by external crypto module, it is stored
+ * in the same array where regular key is. Salt is going to beused
Quoted 9 lines
Quoted 9 lines
by
Quoted 1 line
Quoted 1 line
+ * external crypto module only, but we still need to save and
restore
Quoted 6 lines
Quoted 6 lines
6 lines not shown · raw message
Quoted 5 lines
5 lines not shown · raw message
790 lines not shown · raw message
Quoted 8 lines
7 lines not shown · raw message
4 lines not shown · raw message
Part of this message is not shown here. View the raw message.