1237fead6SMichael Halcrow /** 2237fead6SMichael Halcrow * eCryptfs: Linux filesystem encryption layer 3237fead6SMichael Halcrow * 4237fead6SMichael Halcrow * Copyright (C) 1997-2004 Erez Zadok 5237fead6SMichael Halcrow * Copyright (C) 2001-2004 Stony Brook University 6dd2a3b7aSMichael Halcrow * Copyright (C) 2004-2007 International Business Machines Corp. 7237fead6SMichael Halcrow * Author(s): Michael A. Halcrow <mahalcro@us.ibm.com> 8237fead6SMichael Halcrow * Michael C. Thompson <mcthomps@us.ibm.com> 9237fead6SMichael Halcrow * 10237fead6SMichael Halcrow * This program is free software; you can redistribute it and/or 11237fead6SMichael Halcrow * modify it under the terms of the GNU General Public License as 12237fead6SMichael Halcrow * published by the Free Software Foundation; either version 2 of the 13237fead6SMichael Halcrow * License, or (at your option) any later version. 14237fead6SMichael Halcrow * 15237fead6SMichael Halcrow * This program is distributed in the hope that it will be useful, but 16237fead6SMichael Halcrow * WITHOUT ANY WARRANTY; without even the implied warranty of 17237fead6SMichael Halcrow * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 18237fead6SMichael Halcrow * General Public License for more details. 19237fead6SMichael Halcrow * 20237fead6SMichael Halcrow * You should have received a copy of the GNU General Public License 21237fead6SMichael Halcrow * along with this program; if not, write to the Free Software 22237fead6SMichael Halcrow * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 23237fead6SMichael Halcrow * 02111-1307, USA. 24237fead6SMichael Halcrow */ 25237fead6SMichael Halcrow 263095e8e3SHerbert Xu #include <crypto/hash.h> 273095e8e3SHerbert Xu #include <crypto/skcipher.h> 28237fead6SMichael Halcrow #include <linux/fs.h> 29237fead6SMichael Halcrow #include <linux/mount.h> 30237fead6SMichael Halcrow #include <linux/pagemap.h> 31237fead6SMichael Halcrow #include <linux/random.h> 32237fead6SMichael Halcrow #include <linux/compiler.h> 33237fead6SMichael Halcrow #include <linux/key.h> 34237fead6SMichael Halcrow #include <linux/namei.h> 35237fead6SMichael Halcrow #include <linux/file.h> 36237fead6SMichael Halcrow #include <linux/scatterlist.h> 375a0e3ad6STejun Heo #include <linux/slab.h> 3829335c6aSHarvey Harrison #include <asm/unaligned.h> 39*02f9876eSJérémy Lefaure #include <linux/kernel.h> 40237fead6SMichael Halcrow #include "ecryptfs_kernel.h" 41237fead6SMichael Halcrow 4200a69940STyler Hicks #define DECRYPT 0 4300a69940STyler Hicks #define ENCRYPT 1 44237fead6SMichael Halcrow 45237fead6SMichael Halcrow /** 46237fead6SMichael Halcrow * ecryptfs_from_hex 47237fead6SMichael Halcrow * @dst: Buffer to take the bytes from src hex; must be at least of 48237fead6SMichael Halcrow * size (src_size / 2) 495f9f2c2aSWei Yuan * @src: Buffer to be converted from a hex string representation to raw value 50237fead6SMichael Halcrow * @dst_size: size of dst buffer, or number of hex characters pairs to convert 51237fead6SMichael Halcrow */ 52237fead6SMichael Halcrow void ecryptfs_from_hex(char *dst, char *src, int dst_size) 53237fead6SMichael Halcrow { 54237fead6SMichael Halcrow int x; 55237fead6SMichael Halcrow char tmp[3] = { 0, }; 56237fead6SMichael Halcrow 57237fead6SMichael Halcrow for (x = 0; x < dst_size; x++) { 58237fead6SMichael Halcrow tmp[0] = src[x * 2]; 59237fead6SMichael Halcrow tmp[1] = src[x * 2 + 1]; 60237fead6SMichael Halcrow dst[x] = (unsigned char)simple_strtol(tmp, NULL, 16); 61237fead6SMichael Halcrow } 62237fead6SMichael Halcrow } 63237fead6SMichael Halcrow 643095e8e3SHerbert Xu static int ecryptfs_hash_digest(struct crypto_shash *tfm, 653095e8e3SHerbert Xu char *src, int len, char *dst) 663095e8e3SHerbert Xu { 673095e8e3SHerbert Xu SHASH_DESC_ON_STACK(desc, tfm); 683095e8e3SHerbert Xu int err; 693095e8e3SHerbert Xu 703095e8e3SHerbert Xu desc->tfm = tfm; 713095e8e3SHerbert Xu desc->flags = CRYPTO_TFM_REQ_MAY_SLEEP; 723095e8e3SHerbert Xu err = crypto_shash_digest(desc, src, len, dst); 733095e8e3SHerbert Xu shash_desc_zero(desc); 743095e8e3SHerbert Xu return err; 753095e8e3SHerbert Xu } 763095e8e3SHerbert Xu 77237fead6SMichael Halcrow /** 78237fead6SMichael Halcrow * ecryptfs_calculate_md5 - calculates the md5 of @src 79237fead6SMichael Halcrow * @dst: Pointer to 16 bytes of allocated memory 80237fead6SMichael Halcrow * @crypt_stat: Pointer to crypt_stat struct for the current inode 81237fead6SMichael Halcrow * @src: Data to be md5'd 82237fead6SMichael Halcrow * @len: Length of @src 83237fead6SMichael Halcrow * 84237fead6SMichael Halcrow * Uses the allocated crypto context that crypt_stat references to 85237fead6SMichael Halcrow * generate the MD5 sum of the contents of src. 86237fead6SMichael Halcrow */ 87237fead6SMichael Halcrow static int ecryptfs_calculate_md5(char *dst, 88237fead6SMichael Halcrow struct ecryptfs_crypt_stat *crypt_stat, 89237fead6SMichael Halcrow char *src, int len) 90237fead6SMichael Halcrow { 913095e8e3SHerbert Xu struct crypto_shash *tfm; 92565d9724SMichael Halcrow int rc = 0; 93237fead6SMichael Halcrow 943095e8e3SHerbert Xu tfm = crypt_stat->hash_tfm; 953095e8e3SHerbert Xu rc = ecryptfs_hash_digest(tfm, src, len, dst); 968a29f2b0SMichael Halcrow if (rc) { 978a29f2b0SMichael Halcrow printk(KERN_ERR 983095e8e3SHerbert Xu "%s: Error computing crypto hash; rc = [%d]\n", 9918d1dbf1SHarvey Harrison __func__, rc); 1008a29f2b0SMichael Halcrow goto out; 1018a29f2b0SMichael Halcrow } 102237fead6SMichael Halcrow out: 103237fead6SMichael Halcrow return rc; 104237fead6SMichael Halcrow } 105237fead6SMichael Halcrow 106cd9d67dfSMichael Halcrow static int ecryptfs_crypto_api_algify_cipher_name(char **algified_name, 1078bba066fSMichael Halcrow char *cipher_name, 1088bba066fSMichael Halcrow char *chaining_modifier) 1098bba066fSMichael Halcrow { 1108bba066fSMichael Halcrow int cipher_name_len = strlen(cipher_name); 1118bba066fSMichael Halcrow int chaining_modifier_len = strlen(chaining_modifier); 1128bba066fSMichael Halcrow int algified_name_len; 1138bba066fSMichael Halcrow int rc; 1148bba066fSMichael Halcrow 1158bba066fSMichael Halcrow algified_name_len = (chaining_modifier_len + cipher_name_len + 3); 1168bba066fSMichael Halcrow (*algified_name) = kmalloc(algified_name_len, GFP_KERNEL); 1177bd473fcSMichael Halcrow if (!(*algified_name)) { 1188bba066fSMichael Halcrow rc = -ENOMEM; 1198bba066fSMichael Halcrow goto out; 1208bba066fSMichael Halcrow } 1218bba066fSMichael Halcrow snprintf((*algified_name), algified_name_len, "%s(%s)", 1228bba066fSMichael Halcrow chaining_modifier, cipher_name); 1238bba066fSMichael Halcrow rc = 0; 1248bba066fSMichael Halcrow out: 1258bba066fSMichael Halcrow return rc; 1268bba066fSMichael Halcrow } 1278bba066fSMichael Halcrow 128237fead6SMichael Halcrow /** 129237fead6SMichael Halcrow * ecryptfs_derive_iv 130237fead6SMichael Halcrow * @iv: destination for the derived iv vale 131237fead6SMichael Halcrow * @crypt_stat: Pointer to crypt_stat struct for the current inode 132d6a13c17SMichael Halcrow * @offset: Offset of the extent whose IV we are to derive 133237fead6SMichael Halcrow * 134237fead6SMichael Halcrow * Generate the initialization vector from the given root IV and page 135237fead6SMichael Halcrow * offset. 136237fead6SMichael Halcrow * 137237fead6SMichael Halcrow * Returns zero on success; non-zero on error. 138237fead6SMichael Halcrow */ 139a34f60f7SMichael Halcrow int ecryptfs_derive_iv(char *iv, struct ecryptfs_crypt_stat *crypt_stat, 140d6a13c17SMichael Halcrow loff_t offset) 141237fead6SMichael Halcrow { 142237fead6SMichael Halcrow int rc = 0; 143237fead6SMichael Halcrow char dst[MD5_DIGEST_SIZE]; 144237fead6SMichael Halcrow char src[ECRYPTFS_MAX_IV_BYTES + 16]; 145237fead6SMichael Halcrow 146237fead6SMichael Halcrow if (unlikely(ecryptfs_verbosity > 0)) { 147237fead6SMichael Halcrow ecryptfs_printk(KERN_DEBUG, "root iv:\n"); 148237fead6SMichael Halcrow ecryptfs_dump_hex(crypt_stat->root_iv, crypt_stat->iv_bytes); 149237fead6SMichael Halcrow } 150237fead6SMichael Halcrow /* TODO: It is probably secure to just cast the least 151237fead6SMichael Halcrow * significant bits of the root IV into an unsigned long and 152237fead6SMichael Halcrow * add the offset to that rather than go through all this 153237fead6SMichael Halcrow * hashing business. -Halcrow */ 154237fead6SMichael Halcrow memcpy(src, crypt_stat->root_iv, crypt_stat->iv_bytes); 155237fead6SMichael Halcrow memset((src + crypt_stat->iv_bytes), 0, 16); 156d6a13c17SMichael Halcrow snprintf((src + crypt_stat->iv_bytes), 16, "%lld", offset); 157237fead6SMichael Halcrow if (unlikely(ecryptfs_verbosity > 0)) { 158237fead6SMichael Halcrow ecryptfs_printk(KERN_DEBUG, "source:\n"); 159237fead6SMichael Halcrow ecryptfs_dump_hex(src, (crypt_stat->iv_bytes + 16)); 160237fead6SMichael Halcrow } 161237fead6SMichael Halcrow rc = ecryptfs_calculate_md5(dst, crypt_stat, src, 162237fead6SMichael Halcrow (crypt_stat->iv_bytes + 16)); 163237fead6SMichael Halcrow if (rc) { 164237fead6SMichael Halcrow ecryptfs_printk(KERN_WARNING, "Error attempting to compute " 165237fead6SMichael Halcrow "MD5 while generating IV for a page\n"); 166237fead6SMichael Halcrow goto out; 167237fead6SMichael Halcrow } 168237fead6SMichael Halcrow memcpy(iv, dst, crypt_stat->iv_bytes); 169237fead6SMichael Halcrow if (unlikely(ecryptfs_verbosity > 0)) { 170237fead6SMichael Halcrow ecryptfs_printk(KERN_DEBUG, "derived iv:\n"); 171237fead6SMichael Halcrow ecryptfs_dump_hex(iv, crypt_stat->iv_bytes); 172237fead6SMichael Halcrow } 173237fead6SMichael Halcrow out: 174237fead6SMichael Halcrow return rc; 175237fead6SMichael Halcrow } 176237fead6SMichael Halcrow 177237fead6SMichael Halcrow /** 178237fead6SMichael Halcrow * ecryptfs_init_crypt_stat 179237fead6SMichael Halcrow * @crypt_stat: Pointer to the crypt_stat struct to initialize. 180237fead6SMichael Halcrow * 181237fead6SMichael Halcrow * Initialize the crypt_stat structure. 182237fead6SMichael Halcrow */ 183e81f3340SHerbert Xu int ecryptfs_init_crypt_stat(struct ecryptfs_crypt_stat *crypt_stat) 184237fead6SMichael Halcrow { 185e81f3340SHerbert Xu struct crypto_shash *tfm; 186e81f3340SHerbert Xu int rc; 187e81f3340SHerbert Xu 188e81f3340SHerbert Xu tfm = crypto_alloc_shash(ECRYPTFS_DEFAULT_HASH, 0, 0); 189e81f3340SHerbert Xu if (IS_ERR(tfm)) { 190e81f3340SHerbert Xu rc = PTR_ERR(tfm); 191e81f3340SHerbert Xu ecryptfs_printk(KERN_ERR, "Error attempting to " 192e81f3340SHerbert Xu "allocate crypto context; rc = [%d]\n", 193e81f3340SHerbert Xu rc); 194e81f3340SHerbert Xu return rc; 195e81f3340SHerbert Xu } 196e81f3340SHerbert Xu 197237fead6SMichael Halcrow memset((void *)crypt_stat, 0, sizeof(struct ecryptfs_crypt_stat)); 198f4aad16aSMichael Halcrow INIT_LIST_HEAD(&crypt_stat->keysig_list); 199f4aad16aSMichael Halcrow mutex_init(&crypt_stat->keysig_list_mutex); 200237fead6SMichael Halcrow mutex_init(&crypt_stat->cs_mutex); 201237fead6SMichael Halcrow mutex_init(&crypt_stat->cs_tfm_mutex); 202e81f3340SHerbert Xu crypt_stat->hash_tfm = tfm; 203e2bd99ecSMichael Halcrow crypt_stat->flags |= ECRYPTFS_STRUCT_INITIALIZED; 204e81f3340SHerbert Xu 205e81f3340SHerbert Xu return 0; 206237fead6SMichael Halcrow } 207237fead6SMichael Halcrow 208237fead6SMichael Halcrow /** 209fcd12835SMichael Halcrow * ecryptfs_destroy_crypt_stat 210237fead6SMichael Halcrow * @crypt_stat: Pointer to the crypt_stat struct to initialize. 211237fead6SMichael Halcrow * 212237fead6SMichael Halcrow * Releases all memory associated with a crypt_stat struct. 213237fead6SMichael Halcrow */ 214fcd12835SMichael Halcrow void ecryptfs_destroy_crypt_stat(struct ecryptfs_crypt_stat *crypt_stat) 215237fead6SMichael Halcrow { 216f4aad16aSMichael Halcrow struct ecryptfs_key_sig *key_sig, *key_sig_tmp; 217f4aad16aSMichael Halcrow 2183095e8e3SHerbert Xu crypto_free_skcipher(crypt_stat->tfm); 2193095e8e3SHerbert Xu crypto_free_shash(crypt_stat->hash_tfm); 220f4aad16aSMichael Halcrow list_for_each_entry_safe(key_sig, key_sig_tmp, 221f4aad16aSMichael Halcrow &crypt_stat->keysig_list, crypt_stat_list) { 222f4aad16aSMichael Halcrow list_del(&key_sig->crypt_stat_list); 223f4aad16aSMichael Halcrow kmem_cache_free(ecryptfs_key_sig_cache, key_sig); 224f4aad16aSMichael Halcrow } 225237fead6SMichael Halcrow memset(crypt_stat, 0, sizeof(struct ecryptfs_crypt_stat)); 226237fead6SMichael Halcrow } 227237fead6SMichael Halcrow 228fcd12835SMichael Halcrow void ecryptfs_destroy_mount_crypt_stat( 229237fead6SMichael Halcrow struct ecryptfs_mount_crypt_stat *mount_crypt_stat) 230237fead6SMichael Halcrow { 231f4aad16aSMichael Halcrow struct ecryptfs_global_auth_tok *auth_tok, *auth_tok_tmp; 232f4aad16aSMichael Halcrow 233f4aad16aSMichael Halcrow if (!(mount_crypt_stat->flags & ECRYPTFS_MOUNT_CRYPT_STAT_INITIALIZED)) 234f4aad16aSMichael Halcrow return; 235f4aad16aSMichael Halcrow mutex_lock(&mount_crypt_stat->global_auth_tok_list_mutex); 236f4aad16aSMichael Halcrow list_for_each_entry_safe(auth_tok, auth_tok_tmp, 237f4aad16aSMichael Halcrow &mount_crypt_stat->global_auth_tok_list, 238f4aad16aSMichael Halcrow mount_crypt_stat_list) { 239f4aad16aSMichael Halcrow list_del(&auth_tok->mount_crypt_stat_list); 2400dad87fcSMarkus Elfring if (!(auth_tok->flags & ECRYPTFS_AUTH_TOK_INVALID)) 241f4aad16aSMichael Halcrow key_put(auth_tok->global_auth_tok_key); 242f4aad16aSMichael Halcrow kmem_cache_free(ecryptfs_global_auth_tok_cache, auth_tok); 243f4aad16aSMichael Halcrow } 244f4aad16aSMichael Halcrow mutex_unlock(&mount_crypt_stat->global_auth_tok_list_mutex); 245237fead6SMichael Halcrow memset(mount_crypt_stat, 0, sizeof(struct ecryptfs_mount_crypt_stat)); 246237fead6SMichael Halcrow } 247237fead6SMichael Halcrow 248237fead6SMichael Halcrow /** 249237fead6SMichael Halcrow * virt_to_scatterlist 250237fead6SMichael Halcrow * @addr: Virtual address 251237fead6SMichael Halcrow * @size: Size of data; should be an even multiple of the block size 252237fead6SMichael Halcrow * @sg: Pointer to scatterlist array; set to NULL to obtain only 253237fead6SMichael Halcrow * the number of scatterlist structs required in array 254237fead6SMichael Halcrow * @sg_size: Max array size 255237fead6SMichael Halcrow * 256237fead6SMichael Halcrow * Fills in a scatterlist array with page references for a passed 257237fead6SMichael Halcrow * virtual address. 258237fead6SMichael Halcrow * 259237fead6SMichael Halcrow * Returns the number of scatterlist structs in array used 260237fead6SMichael Halcrow */ 261237fead6SMichael Halcrow int virt_to_scatterlist(const void *addr, int size, struct scatterlist *sg, 262237fead6SMichael Halcrow int sg_size) 263237fead6SMichael Halcrow { 264237fead6SMichael Halcrow int i = 0; 265237fead6SMichael Halcrow struct page *pg; 266237fead6SMichael Halcrow int offset; 267237fead6SMichael Halcrow int remainder_of_page; 268237fead6SMichael Halcrow 26968e3f5ddSHerbert Xu sg_init_table(sg, sg_size); 27068e3f5ddSHerbert Xu 271237fead6SMichael Halcrow while (size > 0 && i < sg_size) { 272237fead6SMichael Halcrow pg = virt_to_page(addr); 273237fead6SMichael Halcrow offset = offset_in_page(addr); 274642f1490SJens Axboe sg_set_page(&sg[i], pg, 0, offset); 27509cbfeafSKirill A. Shutemov remainder_of_page = PAGE_SIZE - offset; 276237fead6SMichael Halcrow if (size >= remainder_of_page) { 277237fead6SMichael Halcrow sg[i].length = remainder_of_page; 278237fead6SMichael Halcrow addr += remainder_of_page; 279237fead6SMichael Halcrow size -= remainder_of_page; 280237fead6SMichael Halcrow } else { 281237fead6SMichael Halcrow sg[i].length = size; 282237fead6SMichael Halcrow addr += size; 283237fead6SMichael Halcrow size = 0; 284237fead6SMichael Halcrow } 285237fead6SMichael Halcrow i++; 286237fead6SMichael Halcrow } 287237fead6SMichael Halcrow if (size > 0) 288237fead6SMichael Halcrow return -ENOMEM; 289237fead6SMichael Halcrow return i; 290237fead6SMichael Halcrow } 291237fead6SMichael Halcrow 2924dfea4f0STyler Hicks struct extent_crypt_result { 2934dfea4f0STyler Hicks struct completion completion; 2944dfea4f0STyler Hicks int rc; 2954dfea4f0STyler Hicks }; 2964dfea4f0STyler Hicks 2974dfea4f0STyler Hicks static void extent_crypt_complete(struct crypto_async_request *req, int rc) 2984dfea4f0STyler Hicks { 2994dfea4f0STyler Hicks struct extent_crypt_result *ecr = req->data; 3004dfea4f0STyler Hicks 3014dfea4f0STyler Hicks if (rc == -EINPROGRESS) 3024dfea4f0STyler Hicks return; 3034dfea4f0STyler Hicks 3044dfea4f0STyler Hicks ecr->rc = rc; 3054dfea4f0STyler Hicks complete(&ecr->completion); 3064dfea4f0STyler Hicks } 3074dfea4f0STyler Hicks 308237fead6SMichael Halcrow /** 30900a69940STyler Hicks * crypt_scatterlist 310237fead6SMichael Halcrow * @crypt_stat: Pointer to the crypt_stat struct to initialize. 3110df5ed65STyler Hicks * @dst_sg: Destination of the data after performing the crypto operation 31200a69940STyler Hicks * @src_sg: Data to be encrypted or decrypted 31300a69940STyler Hicks * @size: Length of data 31400a69940STyler Hicks * @iv: IV to use 31500a69940STyler Hicks * @op: ENCRYPT or DECRYPT to indicate the desired operation 316237fead6SMichael Halcrow * 31700a69940STyler Hicks * Returns the number of bytes encrypted or decrypted; negative value on error 318237fead6SMichael Halcrow */ 31900a69940STyler Hicks static int crypt_scatterlist(struct ecryptfs_crypt_stat *crypt_stat, 3200df5ed65STyler Hicks struct scatterlist *dst_sg, 321237fead6SMichael Halcrow struct scatterlist *src_sg, int size, 32200a69940STyler Hicks unsigned char *iv, int op) 323237fead6SMichael Halcrow { 3243095e8e3SHerbert Xu struct skcipher_request *req = NULL; 3254dfea4f0STyler Hicks struct extent_crypt_result ecr; 326237fead6SMichael Halcrow int rc = 0; 327237fead6SMichael Halcrow 328237fead6SMichael Halcrow BUG_ON(!crypt_stat || !crypt_stat->tfm 329e2bd99ecSMichael Halcrow || !(crypt_stat->flags & ECRYPTFS_STRUCT_INITIALIZED)); 330237fead6SMichael Halcrow if (unlikely(ecryptfs_verbosity > 0)) { 331f24b3887STyler Hicks ecryptfs_printk(KERN_DEBUG, "Key size [%zd]; key:\n", 332237fead6SMichael Halcrow crypt_stat->key_size); 333237fead6SMichael Halcrow ecryptfs_dump_hex(crypt_stat->key, 334237fead6SMichael Halcrow crypt_stat->key_size); 335237fead6SMichael Halcrow } 3364dfea4f0STyler Hicks 3374dfea4f0STyler Hicks init_completion(&ecr.completion); 3384dfea4f0STyler Hicks 339237fead6SMichael Halcrow mutex_lock(&crypt_stat->cs_tfm_mutex); 3403095e8e3SHerbert Xu req = skcipher_request_alloc(crypt_stat->tfm, GFP_NOFS); 3414dfea4f0STyler Hicks if (!req) { 3424dfea4f0STyler Hicks mutex_unlock(&crypt_stat->cs_tfm_mutex); 3434dfea4f0STyler Hicks rc = -ENOMEM; 3444dfea4f0STyler Hicks goto out; 3458e3a6f16STrevor Highland } 3464dfea4f0STyler Hicks 3473095e8e3SHerbert Xu skcipher_request_set_callback(req, 3484dfea4f0STyler Hicks CRYPTO_TFM_REQ_MAY_BACKLOG | CRYPTO_TFM_REQ_MAY_SLEEP, 3494dfea4f0STyler Hicks extent_crypt_complete, &ecr); 3504dfea4f0STyler Hicks /* Consider doing this once, when the file is opened */ 3514dfea4f0STyler Hicks if (!(crypt_stat->flags & ECRYPTFS_KEY_SET)) { 3523095e8e3SHerbert Xu rc = crypto_skcipher_setkey(crypt_stat->tfm, crypt_stat->key, 3534dfea4f0STyler Hicks crypt_stat->key_size); 354237fead6SMichael Halcrow if (rc) { 3554dfea4f0STyler Hicks ecryptfs_printk(KERN_ERR, 3564dfea4f0STyler Hicks "Error setting key; rc = [%d]\n", 357237fead6SMichael Halcrow rc); 358237fead6SMichael Halcrow mutex_unlock(&crypt_stat->cs_tfm_mutex); 359237fead6SMichael Halcrow rc = -EINVAL; 360237fead6SMichael Halcrow goto out; 361237fead6SMichael Halcrow } 3624dfea4f0STyler Hicks crypt_stat->flags |= ECRYPTFS_KEY_SET; 3634dfea4f0STyler Hicks } 364237fead6SMichael Halcrow mutex_unlock(&crypt_stat->cs_tfm_mutex); 3653095e8e3SHerbert Xu skcipher_request_set_crypt(req, src_sg, dst_sg, size, iv); 3663095e8e3SHerbert Xu rc = op == ENCRYPT ? crypto_skcipher_encrypt(req) : 3673095e8e3SHerbert Xu crypto_skcipher_decrypt(req); 3684dfea4f0STyler Hicks if (rc == -EINPROGRESS || rc == -EBUSY) { 3694dfea4f0STyler Hicks struct extent_crypt_result *ecr = req->base.data; 3704dfea4f0STyler Hicks 3714dfea4f0STyler Hicks wait_for_completion(&ecr->completion); 3724dfea4f0STyler Hicks rc = ecr->rc; 37316735d02SWolfram Sang reinit_completion(&ecr->completion); 3744dfea4f0STyler Hicks } 375237fead6SMichael Halcrow out: 3763095e8e3SHerbert Xu skcipher_request_free(req); 377237fead6SMichael Halcrow return rc; 378237fead6SMichael Halcrow } 379237fead6SMichael Halcrow 380237fead6SMichael Halcrow /** 38124d15266STyler Hicks * lower_offset_for_page 382237fead6SMichael Halcrow * 3830216f7f7SMichael Halcrow * Convert an eCryptfs page index into a lower byte offset 384237fead6SMichael Halcrow */ 38524d15266STyler Hicks static loff_t lower_offset_for_page(struct ecryptfs_crypt_stat *crypt_stat, 38624d15266STyler Hicks struct page *page) 387237fead6SMichael Halcrow { 38824d15266STyler Hicks return ecryptfs_lower_header_size(crypt_stat) + 38909cbfeafSKirill A. Shutemov ((loff_t)page->index << PAGE_SHIFT); 3900216f7f7SMichael Halcrow } 391237fead6SMichael Halcrow 3920216f7f7SMichael Halcrow /** 393d78de618STyler Hicks * crypt_extent 3940216f7f7SMichael Halcrow * @crypt_stat: crypt_stat containing cryptographic context for the 3950216f7f7SMichael Halcrow * encryption operation 3960df5ed65STyler Hicks * @dst_page: The page to write the result into 397d78de618STyler Hicks * @src_page: The page to read from 3980216f7f7SMichael Halcrow * @extent_offset: Page extent offset for use in generating IV 399d78de618STyler Hicks * @op: ENCRYPT or DECRYPT to indicate the desired operation 4000216f7f7SMichael Halcrow * 401d78de618STyler Hicks * Encrypts or decrypts one extent of data. 4020216f7f7SMichael Halcrow * 4030216f7f7SMichael Halcrow * Return zero on success; non-zero otherwise 4040216f7f7SMichael Halcrow */ 4050df5ed65STyler Hicks static int crypt_extent(struct ecryptfs_crypt_stat *crypt_stat, 4060df5ed65STyler Hicks struct page *dst_page, 407d78de618STyler Hicks struct page *src_page, 408d78de618STyler Hicks unsigned long extent_offset, int op) 4090216f7f7SMichael Halcrow { 410d78de618STyler Hicks pgoff_t page_index = op == ENCRYPT ? src_page->index : dst_page->index; 411d6a13c17SMichael Halcrow loff_t extent_base; 4120216f7f7SMichael Halcrow char extent_iv[ECRYPTFS_MAX_IV_BYTES]; 413406c93dfSTyler Hicks struct scatterlist src_sg, dst_sg; 414406c93dfSTyler Hicks size_t extent_size = crypt_stat->extent_size; 4150216f7f7SMichael Halcrow int rc; 4160216f7f7SMichael Halcrow 41709cbfeafSKirill A. Shutemov extent_base = (((loff_t)page_index) * (PAGE_SIZE / extent_size)); 418237fead6SMichael Halcrow rc = ecryptfs_derive_iv(extent_iv, crypt_stat, 4190216f7f7SMichael Halcrow (extent_base + extent_offset)); 420237fead6SMichael Halcrow if (rc) { 421888d57bbSJoe Perches ecryptfs_printk(KERN_ERR, "Error attempting to derive IV for " 422888d57bbSJoe Perches "extent [0x%.16llx]; rc = [%d]\n", 423888d57bbSJoe Perches (unsigned long long)(extent_base + extent_offset), rc); 424237fead6SMichael Halcrow goto out; 425237fead6SMichael Halcrow } 426406c93dfSTyler Hicks 427406c93dfSTyler Hicks sg_init_table(&src_sg, 1); 428406c93dfSTyler Hicks sg_init_table(&dst_sg, 1); 429406c93dfSTyler Hicks 430406c93dfSTyler Hicks sg_set_page(&src_sg, src_page, extent_size, 431406c93dfSTyler Hicks extent_offset * extent_size); 432406c93dfSTyler Hicks sg_set_page(&dst_sg, dst_page, extent_size, 433406c93dfSTyler Hicks extent_offset * extent_size); 434406c93dfSTyler Hicks 435406c93dfSTyler Hicks rc = crypt_scatterlist(crypt_stat, &dst_sg, &src_sg, extent_size, 436406c93dfSTyler Hicks extent_iv, op); 4370216f7f7SMichael Halcrow if (rc < 0) { 438d78de618STyler Hicks printk(KERN_ERR "%s: Error attempting to crypt page with " 439d78de618STyler Hicks "page_index = [%ld], extent_offset = [%ld]; " 440d78de618STyler Hicks "rc = [%d]\n", __func__, page_index, extent_offset, rc); 4410216f7f7SMichael Halcrow goto out; 4420216f7f7SMichael Halcrow } 4430216f7f7SMichael Halcrow rc = 0; 4440216f7f7SMichael Halcrow out: 4450216f7f7SMichael Halcrow return rc; 4460216f7f7SMichael Halcrow } 4470216f7f7SMichael Halcrow 4480216f7f7SMichael Halcrow /** 4490216f7f7SMichael Halcrow * ecryptfs_encrypt_page 4500216f7f7SMichael Halcrow * @page: Page mapped from the eCryptfs inode for the file; contains 4510216f7f7SMichael Halcrow * decrypted content that needs to be encrypted (to a temporary 4520216f7f7SMichael Halcrow * page; not in place) and written out to the lower file 4530216f7f7SMichael Halcrow * 4540216f7f7SMichael Halcrow * Encrypt an eCryptfs page. This is done on a per-extent basis. Note 4550216f7f7SMichael Halcrow * that eCryptfs pages may straddle the lower pages -- for instance, 4560216f7f7SMichael Halcrow * if the file was created on a machine with an 8K page size 4570216f7f7SMichael Halcrow * (resulting in an 8K header), and then the file is copied onto a 4580216f7f7SMichael Halcrow * host with a 32K page size, then when reading page 0 of the eCryptfs 4590216f7f7SMichael Halcrow * file, 24K of page 0 of the lower file will be read and decrypted, 4600216f7f7SMichael Halcrow * and then 8K of page 1 of the lower file will be read and decrypted. 4610216f7f7SMichael Halcrow * 4620216f7f7SMichael Halcrow * Returns zero on success; negative on error 4630216f7f7SMichael Halcrow */ 4640216f7f7SMichael Halcrow int ecryptfs_encrypt_page(struct page *page) 4650216f7f7SMichael Halcrow { 4660216f7f7SMichael Halcrow struct inode *ecryptfs_inode; 4670216f7f7SMichael Halcrow struct ecryptfs_crypt_stat *crypt_stat; 4687fcba054SEric Sandeen char *enc_extent_virt; 4697fcba054SEric Sandeen struct page *enc_extent_page = NULL; 4700216f7f7SMichael Halcrow loff_t extent_offset; 4710f896176STyler Hicks loff_t lower_offset; 4720216f7f7SMichael Halcrow int rc = 0; 4730216f7f7SMichael Halcrow 4740216f7f7SMichael Halcrow ecryptfs_inode = page->mapping->host; 4750216f7f7SMichael Halcrow crypt_stat = 4760216f7f7SMichael Halcrow &(ecryptfs_inode_to_private(ecryptfs_inode)->crypt_stat); 47713a791b4STyler Hicks BUG_ON(!(crypt_stat->flags & ECRYPTFS_ENCRYPTED)); 4787fcba054SEric Sandeen enc_extent_page = alloc_page(GFP_USER); 4797fcba054SEric Sandeen if (!enc_extent_page) { 4800216f7f7SMichael Halcrow rc = -ENOMEM; 4810216f7f7SMichael Halcrow ecryptfs_printk(KERN_ERR, "Error allocating memory for " 4820216f7f7SMichael Halcrow "encrypted extent\n"); 4830216f7f7SMichael Halcrow goto out; 4840216f7f7SMichael Halcrow } 4850f896176STyler Hicks 4860216f7f7SMichael Halcrow for (extent_offset = 0; 48709cbfeafSKirill A. Shutemov extent_offset < (PAGE_SIZE / crypt_stat->extent_size); 4880216f7f7SMichael Halcrow extent_offset++) { 4890df5ed65STyler Hicks rc = crypt_extent(crypt_stat, enc_extent_page, page, 490d78de618STyler Hicks extent_offset, ENCRYPT); 4910216f7f7SMichael Halcrow if (rc) { 4920216f7f7SMichael Halcrow printk(KERN_ERR "%s: Error encrypting extent; " 49318d1dbf1SHarvey Harrison "rc = [%d]\n", __func__, rc); 4940216f7f7SMichael Halcrow goto out; 4950216f7f7SMichael Halcrow } 4960216f7f7SMichael Halcrow } 4970f896176STyler Hicks 49824d15266STyler Hicks lower_offset = lower_offset_for_page(crypt_stat, page); 4990f896176STyler Hicks enc_extent_virt = kmap(enc_extent_page); 5000f896176STyler Hicks rc = ecryptfs_write_lower(ecryptfs_inode, enc_extent_virt, lower_offset, 50109cbfeafSKirill A. Shutemov PAGE_SIZE); 5027fcba054SEric Sandeen kunmap(enc_extent_page); 5030216f7f7SMichael Halcrow if (rc < 0) { 5040f896176STyler Hicks ecryptfs_printk(KERN_ERR, 5050f896176STyler Hicks "Error attempting to write lower page; rc = [%d]\n", 5060216f7f7SMichael Halcrow rc); 5070216f7f7SMichael Halcrow goto out; 5080216f7f7SMichael Halcrow } 5090216f7f7SMichael Halcrow rc = 0; 510237fead6SMichael Halcrow out: 511237fead6SMichael Halcrow if (enc_extent_page) { 512237fead6SMichael Halcrow __free_page(enc_extent_page); 513237fead6SMichael Halcrow } 514237fead6SMichael Halcrow return rc; 515237fead6SMichael Halcrow } 516237fead6SMichael Halcrow 517237fead6SMichael Halcrow /** 518237fead6SMichael Halcrow * ecryptfs_decrypt_page 5190216f7f7SMichael Halcrow * @page: Page mapped from the eCryptfs inode for the file; data read 5200216f7f7SMichael Halcrow * and decrypted from the lower file will be written into this 5210216f7f7SMichael Halcrow * page 522237fead6SMichael Halcrow * 523237fead6SMichael Halcrow * Decrypt an eCryptfs page. This is done on a per-extent basis. Note 524237fead6SMichael Halcrow * that eCryptfs pages may straddle the lower pages -- for instance, 525237fead6SMichael Halcrow * if the file was created on a machine with an 8K page size 526237fead6SMichael Halcrow * (resulting in an 8K header), and then the file is copied onto a 527237fead6SMichael Halcrow * host with a 32K page size, then when reading page 0 of the eCryptfs 528237fead6SMichael Halcrow * file, 24K of page 0 of the lower file will be read and decrypted, 529237fead6SMichael Halcrow * and then 8K of page 1 of the lower file will be read and decrypted. 530237fead6SMichael Halcrow * 531237fead6SMichael Halcrow * Returns zero on success; negative on error 532237fead6SMichael Halcrow */ 5330216f7f7SMichael Halcrow int ecryptfs_decrypt_page(struct page *page) 534237fead6SMichael Halcrow { 5350216f7f7SMichael Halcrow struct inode *ecryptfs_inode; 536237fead6SMichael Halcrow struct ecryptfs_crypt_stat *crypt_stat; 5379c6043f4STyler Hicks char *page_virt; 5380216f7f7SMichael Halcrow unsigned long extent_offset; 5390f896176STyler Hicks loff_t lower_offset; 540237fead6SMichael Halcrow int rc = 0; 541237fead6SMichael Halcrow 5420216f7f7SMichael Halcrow ecryptfs_inode = page->mapping->host; 5430216f7f7SMichael Halcrow crypt_stat = 5440216f7f7SMichael Halcrow &(ecryptfs_inode_to_private(ecryptfs_inode)->crypt_stat); 54513a791b4STyler Hicks BUG_ON(!(crypt_stat->flags & ECRYPTFS_ENCRYPTED)); 5460f896176STyler Hicks 54724d15266STyler Hicks lower_offset = lower_offset_for_page(crypt_stat, page); 5489c6043f4STyler Hicks page_virt = kmap(page); 54909cbfeafSKirill A. Shutemov rc = ecryptfs_read_lower(page_virt, lower_offset, PAGE_SIZE, 5500f896176STyler Hicks ecryptfs_inode); 5519c6043f4STyler Hicks kunmap(page); 5520f896176STyler Hicks if (rc < 0) { 5530f896176STyler Hicks ecryptfs_printk(KERN_ERR, 5540f896176STyler Hicks "Error attempting to read lower page; rc = [%d]\n", 5550f896176STyler Hicks rc); 55616a72c45SMichael Halcrow goto out; 557237fead6SMichael Halcrow } 5580f896176STyler Hicks 5590216f7f7SMichael Halcrow for (extent_offset = 0; 56009cbfeafSKirill A. Shutemov extent_offset < (PAGE_SIZE / crypt_stat->extent_size); 5610216f7f7SMichael Halcrow extent_offset++) { 5620df5ed65STyler Hicks rc = crypt_extent(crypt_stat, page, page, 563d78de618STyler Hicks extent_offset, DECRYPT); 5640216f7f7SMichael Halcrow if (rc) { 5650216f7f7SMichael Halcrow printk(KERN_ERR "%s: Error encrypting extent; " 56618d1dbf1SHarvey Harrison "rc = [%d]\n", __func__, rc); 56716a72c45SMichael Halcrow goto out; 568237fead6SMichael Halcrow } 569237fead6SMichael Halcrow } 570237fead6SMichael Halcrow out: 571237fead6SMichael Halcrow return rc; 572237fead6SMichael Halcrow } 573237fead6SMichael Halcrow 574237fead6SMichael Halcrow #define ECRYPTFS_MAX_SCATTERLIST_LEN 4 575237fead6SMichael Halcrow 576237fead6SMichael Halcrow /** 577237fead6SMichael Halcrow * ecryptfs_init_crypt_ctx 578421f91d2SUwe Kleine-König * @crypt_stat: Uninitialized crypt stats structure 579237fead6SMichael Halcrow * 580237fead6SMichael Halcrow * Initialize the crypto context. 581237fead6SMichael Halcrow * 582237fead6SMichael Halcrow * TODO: Performance: Keep a cache of initialized cipher contexts; 583237fead6SMichael Halcrow * only init if needed 584237fead6SMichael Halcrow */ 585237fead6SMichael Halcrow int ecryptfs_init_crypt_ctx(struct ecryptfs_crypt_stat *crypt_stat) 586237fead6SMichael Halcrow { 5878bba066fSMichael Halcrow char *full_alg_name; 588237fead6SMichael Halcrow int rc = -EINVAL; 589237fead6SMichael Halcrow 590237fead6SMichael Halcrow ecryptfs_printk(KERN_DEBUG, 591237fead6SMichael Halcrow "Initializing cipher [%s]; strlen = [%d]; " 592f24b3887STyler Hicks "key_size_bits = [%zd]\n", 593237fead6SMichael Halcrow crypt_stat->cipher, (int)strlen(crypt_stat->cipher), 594237fead6SMichael Halcrow crypt_stat->key_size << 3); 595cb69f36bSKees Cook mutex_lock(&crypt_stat->cs_tfm_mutex); 596237fead6SMichael Halcrow if (crypt_stat->tfm) { 597237fead6SMichael Halcrow rc = 0; 598cb69f36bSKees Cook goto out_unlock; 599237fead6SMichael Halcrow } 6008bba066fSMichael Halcrow rc = ecryptfs_crypto_api_algify_cipher_name(&full_alg_name, 6018bba066fSMichael Halcrow crypt_stat->cipher, "cbc"); 6028bba066fSMichael Halcrow if (rc) 603c8161f64SEric Sandeen goto out_unlock; 6043095e8e3SHerbert Xu crypt_stat->tfm = crypto_alloc_skcipher(full_alg_name, 0, 0); 605de88777eSAkinobu Mita if (IS_ERR(crypt_stat->tfm)) { 606de88777eSAkinobu Mita rc = PTR_ERR(crypt_stat->tfm); 607b0105eaeSTyler Hicks crypt_stat->tfm = NULL; 608237fead6SMichael Halcrow ecryptfs_printk(KERN_ERR, "cryptfs: init_crypt_ctx(): " 609237fead6SMichael Halcrow "Error initializing cipher [%s]\n", 610cb69f36bSKees Cook full_alg_name); 611cb69f36bSKees Cook goto out_free; 612237fead6SMichael Halcrow } 6133095e8e3SHerbert Xu crypto_skcipher_set_flags(crypt_stat->tfm, CRYPTO_TFM_REQ_WEAK_KEY); 614237fead6SMichael Halcrow rc = 0; 615cb69f36bSKees Cook out_free: 616cb69f36bSKees Cook kfree(full_alg_name); 617c8161f64SEric Sandeen out_unlock: 618c8161f64SEric Sandeen mutex_unlock(&crypt_stat->cs_tfm_mutex); 619237fead6SMichael Halcrow return rc; 620237fead6SMichael Halcrow } 621237fead6SMichael Halcrow 622237fead6SMichael Halcrow static void set_extent_mask_and_shift(struct ecryptfs_crypt_stat *crypt_stat) 623237fead6SMichael Halcrow { 624237fead6SMichael Halcrow int extent_size_tmp; 625237fead6SMichael Halcrow 626237fead6SMichael Halcrow crypt_stat->extent_mask = 0xFFFFFFFF; 627237fead6SMichael Halcrow crypt_stat->extent_shift = 0; 628237fead6SMichael Halcrow if (crypt_stat->extent_size == 0) 629237fead6SMichael Halcrow return; 630237fead6SMichael Halcrow extent_size_tmp = crypt_stat->extent_size; 631237fead6SMichael Halcrow while ((extent_size_tmp & 0x01) == 0) { 632237fead6SMichael Halcrow extent_size_tmp >>= 1; 633237fead6SMichael Halcrow crypt_stat->extent_mask <<= 1; 634237fead6SMichael Halcrow crypt_stat->extent_shift++; 635237fead6SMichael Halcrow } 636237fead6SMichael Halcrow } 637237fead6SMichael Halcrow 638237fead6SMichael Halcrow void ecryptfs_set_default_sizes(struct ecryptfs_crypt_stat *crypt_stat) 639237fead6SMichael Halcrow { 640237fead6SMichael Halcrow /* Default values; may be overwritten as we are parsing the 641237fead6SMichael Halcrow * packets. */ 642237fead6SMichael Halcrow crypt_stat->extent_size = ECRYPTFS_DEFAULT_EXTENT_SIZE; 643237fead6SMichael Halcrow set_extent_mask_and_shift(crypt_stat); 644237fead6SMichael Halcrow crypt_stat->iv_bytes = ECRYPTFS_DEFAULT_IV_BYTES; 645dd2a3b7aSMichael Halcrow if (crypt_stat->flags & ECRYPTFS_METADATA_IN_XATTR) 646fa3ef1cbSTyler Hicks crypt_stat->metadata_size = ECRYPTFS_MINIMUM_HEADER_EXTENT_SIZE; 64745eaab79SMichael Halcrow else { 64809cbfeafSKirill A. Shutemov if (PAGE_SIZE <= ECRYPTFS_MINIMUM_HEADER_EXTENT_SIZE) 649fa3ef1cbSTyler Hicks crypt_stat->metadata_size = 650cc11beffSMichael Halcrow ECRYPTFS_MINIMUM_HEADER_EXTENT_SIZE; 651dd2a3b7aSMichael Halcrow else 65209cbfeafSKirill A. Shutemov crypt_stat->metadata_size = PAGE_SIZE; 65345eaab79SMichael Halcrow } 654237fead6SMichael Halcrow } 655237fead6SMichael Halcrow 656237fead6SMichael Halcrow /** 657237fead6SMichael Halcrow * ecryptfs_compute_root_iv 658237fead6SMichael Halcrow * @crypt_stats 659237fead6SMichael Halcrow * 660237fead6SMichael Halcrow * On error, sets the root IV to all 0's. 661237fead6SMichael Halcrow */ 662237fead6SMichael Halcrow int ecryptfs_compute_root_iv(struct ecryptfs_crypt_stat *crypt_stat) 663237fead6SMichael Halcrow { 664237fead6SMichael Halcrow int rc = 0; 665237fead6SMichael Halcrow char dst[MD5_DIGEST_SIZE]; 666237fead6SMichael Halcrow 667237fead6SMichael Halcrow BUG_ON(crypt_stat->iv_bytes > MD5_DIGEST_SIZE); 668237fead6SMichael Halcrow BUG_ON(crypt_stat->iv_bytes <= 0); 669e2bd99ecSMichael Halcrow if (!(crypt_stat->flags & ECRYPTFS_KEY_VALID)) { 670237fead6SMichael Halcrow rc = -EINVAL; 671237fead6SMichael Halcrow ecryptfs_printk(KERN_WARNING, "Session key not valid; " 672237fead6SMichael Halcrow "cannot generate root IV\n"); 673237fead6SMichael Halcrow goto out; 674237fead6SMichael Halcrow } 675237fead6SMichael Halcrow rc = ecryptfs_calculate_md5(dst, crypt_stat, crypt_stat->key, 676237fead6SMichael Halcrow crypt_stat->key_size); 677237fead6SMichael Halcrow if (rc) { 678237fead6SMichael Halcrow ecryptfs_printk(KERN_WARNING, "Error attempting to compute " 679237fead6SMichael Halcrow "MD5 while generating root IV\n"); 680237fead6SMichael Halcrow goto out; 681237fead6SMichael Halcrow } 682237fead6SMichael Halcrow memcpy(crypt_stat->root_iv, dst, crypt_stat->iv_bytes); 683237fead6SMichael Halcrow out: 684237fead6SMichael Halcrow if (rc) { 685237fead6SMichael Halcrow memset(crypt_stat->root_iv, 0, crypt_stat->iv_bytes); 686e2bd99ecSMichael Halcrow crypt_stat->flags |= ECRYPTFS_SECURITY_WARNING; 687237fead6SMichael Halcrow } 688237fead6SMichael Halcrow return rc; 689237fead6SMichael Halcrow } 690237fead6SMichael Halcrow 691237fead6SMichael Halcrow static void ecryptfs_generate_new_key(struct ecryptfs_crypt_stat *crypt_stat) 692237fead6SMichael Halcrow { 693237fead6SMichael Halcrow get_random_bytes(crypt_stat->key, crypt_stat->key_size); 694e2bd99ecSMichael Halcrow crypt_stat->flags |= ECRYPTFS_KEY_VALID; 695237fead6SMichael Halcrow ecryptfs_compute_root_iv(crypt_stat); 696237fead6SMichael Halcrow if (unlikely(ecryptfs_verbosity > 0)) { 697237fead6SMichael Halcrow ecryptfs_printk(KERN_DEBUG, "Generated new session key:\n"); 698237fead6SMichael Halcrow ecryptfs_dump_hex(crypt_stat->key, 699237fead6SMichael Halcrow crypt_stat->key_size); 700237fead6SMichael Halcrow } 701237fead6SMichael Halcrow } 702237fead6SMichael Halcrow 703237fead6SMichael Halcrow /** 70417398957SMichael Halcrow * ecryptfs_copy_mount_wide_flags_to_inode_flags 70522e78fafSMichael Halcrow * @crypt_stat: The inode's cryptographic context 70622e78fafSMichael Halcrow * @mount_crypt_stat: The mount point's cryptographic context 70717398957SMichael Halcrow * 70817398957SMichael Halcrow * This function propagates the mount-wide flags to individual inode 70917398957SMichael Halcrow * flags. 71017398957SMichael Halcrow */ 71117398957SMichael Halcrow static void ecryptfs_copy_mount_wide_flags_to_inode_flags( 71217398957SMichael Halcrow struct ecryptfs_crypt_stat *crypt_stat, 71317398957SMichael Halcrow struct ecryptfs_mount_crypt_stat *mount_crypt_stat) 71417398957SMichael Halcrow { 71517398957SMichael Halcrow if (mount_crypt_stat->flags & ECRYPTFS_XATTR_METADATA_ENABLED) 71617398957SMichael Halcrow crypt_stat->flags |= ECRYPTFS_METADATA_IN_XATTR; 71717398957SMichael Halcrow if (mount_crypt_stat->flags & ECRYPTFS_ENCRYPTED_VIEW_ENABLED) 71817398957SMichael Halcrow crypt_stat->flags |= ECRYPTFS_VIEW_AS_ENCRYPTED; 719addd65adSMichael Halcrow if (mount_crypt_stat->flags & ECRYPTFS_GLOBAL_ENCRYPT_FILENAMES) { 720addd65adSMichael Halcrow crypt_stat->flags |= ECRYPTFS_ENCRYPT_FILENAMES; 721addd65adSMichael Halcrow if (mount_crypt_stat->flags 722addd65adSMichael Halcrow & ECRYPTFS_GLOBAL_ENCFN_USE_MOUNT_FNEK) 723addd65adSMichael Halcrow crypt_stat->flags |= ECRYPTFS_ENCFN_USE_MOUNT_FNEK; 724addd65adSMichael Halcrow else if (mount_crypt_stat->flags 725addd65adSMichael Halcrow & ECRYPTFS_GLOBAL_ENCFN_USE_FEK) 726addd65adSMichael Halcrow crypt_stat->flags |= ECRYPTFS_ENCFN_USE_FEK; 727addd65adSMichael Halcrow } 72817398957SMichael Halcrow } 72917398957SMichael Halcrow 730f4aad16aSMichael Halcrow static int ecryptfs_copy_mount_wide_sigs_to_inode_sigs( 731f4aad16aSMichael Halcrow struct ecryptfs_crypt_stat *crypt_stat, 732f4aad16aSMichael Halcrow struct ecryptfs_mount_crypt_stat *mount_crypt_stat) 733f4aad16aSMichael Halcrow { 734f4aad16aSMichael Halcrow struct ecryptfs_global_auth_tok *global_auth_tok; 735f4aad16aSMichael Halcrow int rc = 0; 736f4aad16aSMichael Halcrow 737aa06117fSRoland Dreier mutex_lock(&crypt_stat->keysig_list_mutex); 738f4aad16aSMichael Halcrow mutex_lock(&mount_crypt_stat->global_auth_tok_list_mutex); 739aa06117fSRoland Dreier 740f4aad16aSMichael Halcrow list_for_each_entry(global_auth_tok, 741f4aad16aSMichael Halcrow &mount_crypt_stat->global_auth_tok_list, 742f4aad16aSMichael Halcrow mount_crypt_stat_list) { 74384814d64STyler Hicks if (global_auth_tok->flags & ECRYPTFS_AUTH_TOK_FNEK) 74484814d64STyler Hicks continue; 745f4aad16aSMichael Halcrow rc = ecryptfs_add_keysig(crypt_stat, global_auth_tok->sig); 746f4aad16aSMichael Halcrow if (rc) { 747f4aad16aSMichael Halcrow printk(KERN_ERR "Error adding keysig; rc = [%d]\n", rc); 748f4aad16aSMichael Halcrow goto out; 749f4aad16aSMichael Halcrow } 750f4aad16aSMichael Halcrow } 751aa06117fSRoland Dreier 752f4aad16aSMichael Halcrow out: 753aa06117fSRoland Dreier mutex_unlock(&mount_crypt_stat->global_auth_tok_list_mutex); 754aa06117fSRoland Dreier mutex_unlock(&crypt_stat->keysig_list_mutex); 755f4aad16aSMichael Halcrow return rc; 756f4aad16aSMichael Halcrow } 757f4aad16aSMichael Halcrow 75817398957SMichael Halcrow /** 759237fead6SMichael Halcrow * ecryptfs_set_default_crypt_stat_vals 76022e78fafSMichael Halcrow * @crypt_stat: The inode's cryptographic context 76122e78fafSMichael Halcrow * @mount_crypt_stat: The mount point's cryptographic context 762237fead6SMichael Halcrow * 763237fead6SMichael Halcrow * Default values in the event that policy does not override them. 764237fead6SMichael Halcrow */ 765237fead6SMichael Halcrow static void ecryptfs_set_default_crypt_stat_vals( 766237fead6SMichael Halcrow struct ecryptfs_crypt_stat *crypt_stat, 767237fead6SMichael Halcrow struct ecryptfs_mount_crypt_stat *mount_crypt_stat) 768237fead6SMichael Halcrow { 76917398957SMichael Halcrow ecryptfs_copy_mount_wide_flags_to_inode_flags(crypt_stat, 77017398957SMichael Halcrow mount_crypt_stat); 771237fead6SMichael Halcrow ecryptfs_set_default_sizes(crypt_stat); 772237fead6SMichael Halcrow strcpy(crypt_stat->cipher, ECRYPTFS_DEFAULT_CIPHER); 773237fead6SMichael Halcrow crypt_stat->key_size = ECRYPTFS_DEFAULT_KEY_BYTES; 774e2bd99ecSMichael Halcrow crypt_stat->flags &= ~(ECRYPTFS_KEY_VALID); 775237fead6SMichael Halcrow crypt_stat->file_version = ECRYPTFS_FILE_VERSION; 776237fead6SMichael Halcrow crypt_stat->mount_crypt_stat = mount_crypt_stat; 777237fead6SMichael Halcrow } 778237fead6SMichael Halcrow 779237fead6SMichael Halcrow /** 780237fead6SMichael Halcrow * ecryptfs_new_file_context 781b59db43aSTyler Hicks * @ecryptfs_inode: The eCryptfs inode 782237fead6SMichael Halcrow * 783237fead6SMichael Halcrow * If the crypto context for the file has not yet been established, 784237fead6SMichael Halcrow * this is where we do that. Establishing a new crypto context 785237fead6SMichael Halcrow * involves the following decisions: 786237fead6SMichael Halcrow * - What cipher to use? 787237fead6SMichael Halcrow * - What set of authentication tokens to use? 788237fead6SMichael Halcrow * Here we just worry about getting enough information into the 789237fead6SMichael Halcrow * authentication tokens so that we know that they are available. 790237fead6SMichael Halcrow * We associate the available authentication tokens with the new file 791237fead6SMichael Halcrow * via the set of signatures in the crypt_stat struct. Later, when 792237fead6SMichael Halcrow * the headers are actually written out, we may again defer to 793237fead6SMichael Halcrow * userspace to perform the encryption of the session key; for the 794237fead6SMichael Halcrow * foreseeable future, this will be the case with public key packets. 795237fead6SMichael Halcrow * 796237fead6SMichael Halcrow * Returns zero on success; non-zero otherwise 797237fead6SMichael Halcrow */ 798b59db43aSTyler Hicks int ecryptfs_new_file_context(struct inode *ecryptfs_inode) 799237fead6SMichael Halcrow { 800237fead6SMichael Halcrow struct ecryptfs_crypt_stat *crypt_stat = 801b59db43aSTyler Hicks &ecryptfs_inode_to_private(ecryptfs_inode)->crypt_stat; 802237fead6SMichael Halcrow struct ecryptfs_mount_crypt_stat *mount_crypt_stat = 803237fead6SMichael Halcrow &ecryptfs_superblock_to_private( 804b59db43aSTyler Hicks ecryptfs_inode->i_sb)->mount_crypt_stat; 805237fead6SMichael Halcrow int cipher_name_len; 806f4aad16aSMichael Halcrow int rc = 0; 807237fead6SMichael Halcrow 808237fead6SMichael Halcrow ecryptfs_set_default_crypt_stat_vals(crypt_stat, mount_crypt_stat); 809af655dc6SMichael Halcrow crypt_stat->flags |= (ECRYPTFS_ENCRYPTED | ECRYPTFS_KEY_VALID); 81017398957SMichael Halcrow ecryptfs_copy_mount_wide_flags_to_inode_flags(crypt_stat, 81117398957SMichael Halcrow mount_crypt_stat); 812f4aad16aSMichael Halcrow rc = ecryptfs_copy_mount_wide_sigs_to_inode_sigs(crypt_stat, 813f4aad16aSMichael Halcrow mount_crypt_stat); 814f4aad16aSMichael Halcrow if (rc) { 815f4aad16aSMichael Halcrow printk(KERN_ERR "Error attempting to copy mount-wide key sigs " 816f4aad16aSMichael Halcrow "to the inode key sigs; rc = [%d]\n", rc); 817f4aad16aSMichael Halcrow goto out; 818f4aad16aSMichael Halcrow } 819237fead6SMichael Halcrow cipher_name_len = 820237fead6SMichael Halcrow strlen(mount_crypt_stat->global_default_cipher_name); 821237fead6SMichael Halcrow memcpy(crypt_stat->cipher, 822237fead6SMichael Halcrow mount_crypt_stat->global_default_cipher_name, 823237fead6SMichael Halcrow cipher_name_len); 824237fead6SMichael Halcrow crypt_stat->cipher[cipher_name_len] = '\0'; 825237fead6SMichael Halcrow crypt_stat->key_size = 826237fead6SMichael Halcrow mount_crypt_stat->global_default_cipher_key_size; 827237fead6SMichael Halcrow ecryptfs_generate_new_key(crypt_stat); 828237fead6SMichael Halcrow rc = ecryptfs_init_crypt_ctx(crypt_stat); 829237fead6SMichael Halcrow if (rc) 830237fead6SMichael Halcrow ecryptfs_printk(KERN_ERR, "Error initializing cryptographic " 831237fead6SMichael Halcrow "context for cipher [%s]: rc = [%d]\n", 832237fead6SMichael Halcrow crypt_stat->cipher, rc); 833f4aad16aSMichael Halcrow out: 834237fead6SMichael Halcrow return rc; 835237fead6SMichael Halcrow } 836237fead6SMichael Halcrow 837237fead6SMichael Halcrow /** 8387a86617eSTyler Hicks * ecryptfs_validate_marker - check for the ecryptfs marker 839237fead6SMichael Halcrow * @data: The data block in which to check 840237fead6SMichael Halcrow * 8417a86617eSTyler Hicks * Returns zero if marker found; -EINVAL if not found 842237fead6SMichael Halcrow */ 8437a86617eSTyler Hicks static int ecryptfs_validate_marker(char *data) 844237fead6SMichael Halcrow { 845237fead6SMichael Halcrow u32 m_1, m_2; 846237fead6SMichael Halcrow 84729335c6aSHarvey Harrison m_1 = get_unaligned_be32(data); 84829335c6aSHarvey Harrison m_2 = get_unaligned_be32(data + 4); 849237fead6SMichael Halcrow if ((m_1 ^ MAGIC_ECRYPTFS_MARKER) == m_2) 8507a86617eSTyler Hicks return 0; 851237fead6SMichael Halcrow ecryptfs_printk(KERN_DEBUG, "m_1 = [0x%.8x]; m_2 = [0x%.8x]; " 852237fead6SMichael Halcrow "MAGIC_ECRYPTFS_MARKER = [0x%.8x]\n", m_1, m_2, 853237fead6SMichael Halcrow MAGIC_ECRYPTFS_MARKER); 854237fead6SMichael Halcrow ecryptfs_printk(KERN_DEBUG, "(m_1 ^ MAGIC_ECRYPTFS_MARKER) = " 855237fead6SMichael Halcrow "[0x%.8x]\n", (m_1 ^ MAGIC_ECRYPTFS_MARKER)); 8567a86617eSTyler Hicks return -EINVAL; 857237fead6SMichael Halcrow } 858237fead6SMichael Halcrow 859237fead6SMichael Halcrow struct ecryptfs_flag_map_elem { 860237fead6SMichael Halcrow u32 file_flag; 861237fead6SMichael Halcrow u32 local_flag; 862237fead6SMichael Halcrow }; 863237fead6SMichael Halcrow 864237fead6SMichael Halcrow /* Add support for additional flags by adding elements here. */ 865237fead6SMichael Halcrow static struct ecryptfs_flag_map_elem ecryptfs_flag_map[] = { 866237fead6SMichael Halcrow {0x00000001, ECRYPTFS_ENABLE_HMAC}, 867dd2a3b7aSMichael Halcrow {0x00000002, ECRYPTFS_ENCRYPTED}, 868addd65adSMichael Halcrow {0x00000004, ECRYPTFS_METADATA_IN_XATTR}, 869addd65adSMichael Halcrow {0x00000008, ECRYPTFS_ENCRYPT_FILENAMES} 870237fead6SMichael Halcrow }; 871237fead6SMichael Halcrow 872237fead6SMichael Halcrow /** 873237fead6SMichael Halcrow * ecryptfs_process_flags 87422e78fafSMichael Halcrow * @crypt_stat: The cryptographic context 875237fead6SMichael Halcrow * @page_virt: Source data to be parsed 876237fead6SMichael Halcrow * @bytes_read: Updated with the number of bytes read 877237fead6SMichael Halcrow * 878237fead6SMichael Halcrow * Returns zero on success; non-zero if the flag set is invalid 879237fead6SMichael Halcrow */ 880237fead6SMichael Halcrow static int ecryptfs_process_flags(struct ecryptfs_crypt_stat *crypt_stat, 881237fead6SMichael Halcrow char *page_virt, int *bytes_read) 882237fead6SMichael Halcrow { 883237fead6SMichael Halcrow int rc = 0; 884237fead6SMichael Halcrow int i; 885237fead6SMichael Halcrow u32 flags; 886237fead6SMichael Halcrow 88729335c6aSHarvey Harrison flags = get_unaligned_be32(page_virt); 888*02f9876eSJérémy Lefaure for (i = 0; i < ARRAY_SIZE(ecryptfs_flag_map); i++) 889237fead6SMichael Halcrow if (flags & ecryptfs_flag_map[i].file_flag) { 890e2bd99ecSMichael Halcrow crypt_stat->flags |= ecryptfs_flag_map[i].local_flag; 891237fead6SMichael Halcrow } else 892e2bd99ecSMichael Halcrow crypt_stat->flags &= ~(ecryptfs_flag_map[i].local_flag); 893237fead6SMichael Halcrow /* Version is in top 8 bits of the 32-bit flag vector */ 894237fead6SMichael Halcrow crypt_stat->file_version = ((flags >> 24) & 0xFF); 895237fead6SMichael Halcrow (*bytes_read) = 4; 896237fead6SMichael Halcrow return rc; 897237fead6SMichael Halcrow } 898237fead6SMichael Halcrow 899237fead6SMichael Halcrow /** 900237fead6SMichael Halcrow * write_ecryptfs_marker 901237fead6SMichael Halcrow * @page_virt: The pointer to in a page to begin writing the marker 902237fead6SMichael Halcrow * @written: Number of bytes written 903237fead6SMichael Halcrow * 904237fead6SMichael Halcrow * Marker = 0x3c81b7f5 905237fead6SMichael Halcrow */ 906237fead6SMichael Halcrow static void write_ecryptfs_marker(char *page_virt, size_t *written) 907237fead6SMichael Halcrow { 908237fead6SMichael Halcrow u32 m_1, m_2; 909237fead6SMichael Halcrow 910237fead6SMichael Halcrow get_random_bytes(&m_1, (MAGIC_ECRYPTFS_MARKER_SIZE_BYTES / 2)); 911237fead6SMichael Halcrow m_2 = (m_1 ^ MAGIC_ECRYPTFS_MARKER); 91229335c6aSHarvey Harrison put_unaligned_be32(m_1, page_virt); 91329335c6aSHarvey Harrison page_virt += (MAGIC_ECRYPTFS_MARKER_SIZE_BYTES / 2); 91429335c6aSHarvey Harrison put_unaligned_be32(m_2, page_virt); 915237fead6SMichael Halcrow (*written) = MAGIC_ECRYPTFS_MARKER_SIZE_BYTES; 916237fead6SMichael Halcrow } 917237fead6SMichael Halcrow 918f4e60e6bSTyler Hicks void ecryptfs_write_crypt_stat_flags(char *page_virt, 919f4e60e6bSTyler Hicks struct ecryptfs_crypt_stat *crypt_stat, 920237fead6SMichael Halcrow size_t *written) 921237fead6SMichael Halcrow { 922237fead6SMichael Halcrow u32 flags = 0; 923237fead6SMichael Halcrow int i; 924237fead6SMichael Halcrow 925*02f9876eSJérémy Lefaure for (i = 0; i < ARRAY_SIZE(ecryptfs_flag_map); i++) 926e2bd99ecSMichael Halcrow if (crypt_stat->flags & ecryptfs_flag_map[i].local_flag) 927237fead6SMichael Halcrow flags |= ecryptfs_flag_map[i].file_flag; 928237fead6SMichael Halcrow /* Version is in top 8 bits of the 32-bit flag vector */ 929237fead6SMichael Halcrow flags |= ((((u8)crypt_stat->file_version) << 24) & 0xFF000000); 93029335c6aSHarvey Harrison put_unaligned_be32(flags, page_virt); 931237fead6SMichael Halcrow (*written) = 4; 932237fead6SMichael Halcrow } 933237fead6SMichael Halcrow 934237fead6SMichael Halcrow struct ecryptfs_cipher_code_str_map_elem { 935237fead6SMichael Halcrow char cipher_str[16]; 93619e66a67STrevor Highland u8 cipher_code; 937237fead6SMichael Halcrow }; 938237fead6SMichael Halcrow 939237fead6SMichael Halcrow /* Add support for additional ciphers by adding elements here. The 94040f0fd37SChris J Arges * cipher_code is whatever OpenPGP applications use to identify the 941237fead6SMichael Halcrow * ciphers. List in order of probability. */ 942237fead6SMichael Halcrow static struct ecryptfs_cipher_code_str_map_elem 943237fead6SMichael Halcrow ecryptfs_cipher_code_str_map[] = { 944237fead6SMichael Halcrow {"aes",RFC2440_CIPHER_AES_128 }, 945237fead6SMichael Halcrow {"blowfish", RFC2440_CIPHER_BLOWFISH}, 946237fead6SMichael Halcrow {"des3_ede", RFC2440_CIPHER_DES3_EDE}, 947237fead6SMichael Halcrow {"cast5", RFC2440_CIPHER_CAST_5}, 948237fead6SMichael Halcrow {"twofish", RFC2440_CIPHER_TWOFISH}, 949237fead6SMichael Halcrow {"cast6", RFC2440_CIPHER_CAST_6}, 950237fead6SMichael Halcrow {"aes", RFC2440_CIPHER_AES_192}, 951237fead6SMichael Halcrow {"aes", RFC2440_CIPHER_AES_256} 952237fead6SMichael Halcrow }; 953237fead6SMichael Halcrow 954237fead6SMichael Halcrow /** 955237fead6SMichael Halcrow * ecryptfs_code_for_cipher_string 9569c79f34fSMichael Halcrow * @cipher_name: The string alias for the cipher 9579c79f34fSMichael Halcrow * @key_bytes: Length of key in bytes; used for AES code selection 958237fead6SMichael Halcrow * 959237fead6SMichael Halcrow * Returns zero on no match, or the cipher code on match 960237fead6SMichael Halcrow */ 9619c79f34fSMichael Halcrow u8 ecryptfs_code_for_cipher_string(char *cipher_name, size_t key_bytes) 962237fead6SMichael Halcrow { 963237fead6SMichael Halcrow int i; 96419e66a67STrevor Highland u8 code = 0; 965237fead6SMichael Halcrow struct ecryptfs_cipher_code_str_map_elem *map = 966237fead6SMichael Halcrow ecryptfs_cipher_code_str_map; 967237fead6SMichael Halcrow 9689c79f34fSMichael Halcrow if (strcmp(cipher_name, "aes") == 0) { 9699c79f34fSMichael Halcrow switch (key_bytes) { 970237fead6SMichael Halcrow case 16: 971237fead6SMichael Halcrow code = RFC2440_CIPHER_AES_128; 972237fead6SMichael Halcrow break; 973237fead6SMichael Halcrow case 24: 974237fead6SMichael Halcrow code = RFC2440_CIPHER_AES_192; 975237fead6SMichael Halcrow break; 976237fead6SMichael Halcrow case 32: 977237fead6SMichael Halcrow code = RFC2440_CIPHER_AES_256; 978237fead6SMichael Halcrow } 979237fead6SMichael Halcrow } else { 980237fead6SMichael Halcrow for (i = 0; i < ARRAY_SIZE(ecryptfs_cipher_code_str_map); i++) 9819c79f34fSMichael Halcrow if (strcmp(cipher_name, map[i].cipher_str) == 0) { 982237fead6SMichael Halcrow code = map[i].cipher_code; 983237fead6SMichael Halcrow break; 984237fead6SMichael Halcrow } 985237fead6SMichael Halcrow } 986237fead6SMichael Halcrow return code; 987237fead6SMichael Halcrow } 988237fead6SMichael Halcrow 989237fead6SMichael Halcrow /** 990237fead6SMichael Halcrow * ecryptfs_cipher_code_to_string 991237fead6SMichael Halcrow * @str: Destination to write out the cipher name 992237fead6SMichael Halcrow * @cipher_code: The code to convert to cipher name string 993237fead6SMichael Halcrow * 994237fead6SMichael Halcrow * Returns zero on success 995237fead6SMichael Halcrow */ 99619e66a67STrevor Highland int ecryptfs_cipher_code_to_string(char *str, u8 cipher_code) 997237fead6SMichael Halcrow { 998237fead6SMichael Halcrow int rc = 0; 999237fead6SMichael Halcrow int i; 1000237fead6SMichael Halcrow 1001237fead6SMichael Halcrow str[0] = '\0'; 1002237fead6SMichael Halcrow for (i = 0; i < ARRAY_SIZE(ecryptfs_cipher_code_str_map); i++) 1003237fead6SMichael Halcrow if (cipher_code == ecryptfs_cipher_code_str_map[i].cipher_code) 1004237fead6SMichael Halcrow strcpy(str, ecryptfs_cipher_code_str_map[i].cipher_str); 1005237fead6SMichael Halcrow if (str[0] == '\0') { 1006237fead6SMichael Halcrow ecryptfs_printk(KERN_WARNING, "Cipher code not recognized: " 1007237fead6SMichael Halcrow "[%d]\n", cipher_code); 1008237fead6SMichael Halcrow rc = -EINVAL; 1009237fead6SMichael Halcrow } 1010237fead6SMichael Halcrow return rc; 1011237fead6SMichael Halcrow } 1012237fead6SMichael Halcrow 1013778aeb42STyler Hicks int ecryptfs_read_and_validate_header_region(struct inode *inode) 1014dd2a3b7aSMichael Halcrow { 1015778aeb42STyler Hicks u8 file_size[ECRYPTFS_SIZE_AND_MARKER_BYTES]; 1016778aeb42STyler Hicks u8 *marker = file_size + ECRYPTFS_FILE_SIZE_BYTES; 1017dd2a3b7aSMichael Halcrow int rc; 1018dd2a3b7aSMichael Halcrow 1019778aeb42STyler Hicks rc = ecryptfs_read_lower(file_size, 0, ECRYPTFS_SIZE_AND_MARKER_BYTES, 1020778aeb42STyler Hicks inode); 1021778aeb42STyler Hicks if (rc < ECRYPTFS_SIZE_AND_MARKER_BYTES) 1022778aeb42STyler Hicks return rc >= 0 ? -EINVAL : rc; 1023778aeb42STyler Hicks rc = ecryptfs_validate_marker(marker); 1024778aeb42STyler Hicks if (!rc) 1025778aeb42STyler Hicks ecryptfs_i_size_init(file_size, inode); 1026dd2a3b7aSMichael Halcrow return rc; 1027dd2a3b7aSMichael Halcrow } 1028dd2a3b7aSMichael Halcrow 1029e77a56ddSMichael Halcrow void 1030e77a56ddSMichael Halcrow ecryptfs_write_header_metadata(char *virt, 1031e77a56ddSMichael Halcrow struct ecryptfs_crypt_stat *crypt_stat, 1032237fead6SMichael Halcrow size_t *written) 1033237fead6SMichael Halcrow { 1034237fead6SMichael Halcrow u32 header_extent_size; 1035237fead6SMichael Halcrow u16 num_header_extents_at_front; 1036237fead6SMichael Halcrow 103745eaab79SMichael Halcrow header_extent_size = (u32)crypt_stat->extent_size; 1038237fead6SMichael Halcrow num_header_extents_at_front = 1039fa3ef1cbSTyler Hicks (u16)(crypt_stat->metadata_size / crypt_stat->extent_size); 104029335c6aSHarvey Harrison put_unaligned_be32(header_extent_size, virt); 1041237fead6SMichael Halcrow virt += 4; 104229335c6aSHarvey Harrison put_unaligned_be16(num_header_extents_at_front, virt); 1043237fead6SMichael Halcrow (*written) = 6; 1044237fead6SMichael Halcrow } 1045237fead6SMichael Halcrow 104630632870STyler Hicks struct kmem_cache *ecryptfs_header_cache; 1047237fead6SMichael Halcrow 1048237fead6SMichael Halcrow /** 1049237fead6SMichael Halcrow * ecryptfs_write_headers_virt 105022e78fafSMichael Halcrow * @page_virt: The virtual address to write the headers to 105187b811c3SEric Sandeen * @max: The size of memory allocated at page_virt 105222e78fafSMichael Halcrow * @size: Set to the number of bytes written by this function 105322e78fafSMichael Halcrow * @crypt_stat: The cryptographic context 105422e78fafSMichael Halcrow * @ecryptfs_dentry: The eCryptfs dentry 1055237fead6SMichael Halcrow * 1056237fead6SMichael Halcrow * Format version: 1 1057237fead6SMichael Halcrow * 1058237fead6SMichael Halcrow * Header Extent: 1059237fead6SMichael Halcrow * Octets 0-7: Unencrypted file size (big-endian) 1060237fead6SMichael Halcrow * Octets 8-15: eCryptfs special marker 1061237fead6SMichael Halcrow * Octets 16-19: Flags 1062237fead6SMichael Halcrow * Octet 16: File format version number (between 0 and 255) 1063237fead6SMichael Halcrow * Octets 17-18: Reserved 1064237fead6SMichael Halcrow * Octet 19: Bit 1 (lsb): Reserved 1065237fead6SMichael Halcrow * Bit 2: Encrypted? 1066237fead6SMichael Halcrow * Bits 3-8: Reserved 1067237fead6SMichael Halcrow * Octets 20-23: Header extent size (big-endian) 1068237fead6SMichael Halcrow * Octets 24-25: Number of header extents at front of file 1069237fead6SMichael Halcrow * (big-endian) 1070237fead6SMichael Halcrow * Octet 26: Begin RFC 2440 authentication token packet set 1071237fead6SMichael Halcrow * Data Extent 0: 1072237fead6SMichael Halcrow * Lower data (CBC encrypted) 1073237fead6SMichael Halcrow * Data Extent 1: 1074237fead6SMichael Halcrow * Lower data (CBC encrypted) 1075237fead6SMichael Halcrow * ... 1076237fead6SMichael Halcrow * 1077237fead6SMichael Halcrow * Returns zero on success 1078237fead6SMichael Halcrow */ 107987b811c3SEric Sandeen static int ecryptfs_write_headers_virt(char *page_virt, size_t max, 108087b811c3SEric Sandeen size_t *size, 1081237fead6SMichael Halcrow struct ecryptfs_crypt_stat *crypt_stat, 1082237fead6SMichael Halcrow struct dentry *ecryptfs_dentry) 1083237fead6SMichael Halcrow { 1084237fead6SMichael Halcrow int rc; 1085237fead6SMichael Halcrow size_t written; 1086237fead6SMichael Halcrow size_t offset; 1087237fead6SMichael Halcrow 1088237fead6SMichael Halcrow offset = ECRYPTFS_FILE_SIZE_BYTES; 1089237fead6SMichael Halcrow write_ecryptfs_marker((page_virt + offset), &written); 1090237fead6SMichael Halcrow offset += written; 1091f4e60e6bSTyler Hicks ecryptfs_write_crypt_stat_flags((page_virt + offset), crypt_stat, 1092f4e60e6bSTyler Hicks &written); 1093237fead6SMichael Halcrow offset += written; 1094e77a56ddSMichael Halcrow ecryptfs_write_header_metadata((page_virt + offset), crypt_stat, 1095e77a56ddSMichael Halcrow &written); 1096237fead6SMichael Halcrow offset += written; 1097237fead6SMichael Halcrow rc = ecryptfs_generate_key_packet_set((page_virt + offset), crypt_stat, 1098237fead6SMichael Halcrow ecryptfs_dentry, &written, 109987b811c3SEric Sandeen max - offset); 1100237fead6SMichael Halcrow if (rc) 1101237fead6SMichael Halcrow ecryptfs_printk(KERN_WARNING, "Error generating key packet " 1102237fead6SMichael Halcrow "set; rc = [%d]\n", rc); 1103dd2a3b7aSMichael Halcrow if (size) { 1104dd2a3b7aSMichael Halcrow offset += written; 1105dd2a3b7aSMichael Halcrow *size = offset; 1106dd2a3b7aSMichael Halcrow } 1107dd2a3b7aSMichael Halcrow return rc; 1108dd2a3b7aSMichael Halcrow } 1109dd2a3b7aSMichael Halcrow 111022e78fafSMichael Halcrow static int 1111b59db43aSTyler Hicks ecryptfs_write_metadata_to_contents(struct inode *ecryptfs_inode, 11128faece5fSTyler Hicks char *virt, size_t virt_len) 1113dd2a3b7aSMichael Halcrow { 1114d7cdc5feSMichael Halcrow int rc; 1115dd2a3b7aSMichael Halcrow 1116b59db43aSTyler Hicks rc = ecryptfs_write_lower(ecryptfs_inode, virt, 11178faece5fSTyler Hicks 0, virt_len); 111896a7b9c2STyler Hicks if (rc < 0) 1119d7cdc5feSMichael Halcrow printk(KERN_ERR "%s: Error attempting to write header " 112096a7b9c2STyler Hicks "information to lower file; rc = [%d]\n", __func__, rc); 112196a7b9c2STyler Hicks else 112296a7b9c2STyler Hicks rc = 0; 112370456600SMichael Halcrow return rc; 1124dd2a3b7aSMichael Halcrow } 1125dd2a3b7aSMichael Halcrow 112622e78fafSMichael Halcrow static int 112722e78fafSMichael Halcrow ecryptfs_write_metadata_to_xattr(struct dentry *ecryptfs_dentry, 11283767e255SAl Viro struct inode *ecryptfs_inode, 1129dd2a3b7aSMichael Halcrow char *page_virt, size_t size) 1130dd2a3b7aSMichael Halcrow { 1131dd2a3b7aSMichael Halcrow int rc; 1132dd2a3b7aSMichael Halcrow 11333767e255SAl Viro rc = ecryptfs_setxattr(ecryptfs_dentry, ecryptfs_inode, 11343767e255SAl Viro ECRYPTFS_XATTR_NAME, page_virt, size, 0); 1135237fead6SMichael Halcrow return rc; 1136237fead6SMichael Halcrow } 1137237fead6SMichael Halcrow 11388faece5fSTyler Hicks static unsigned long ecryptfs_get_zeroed_pages(gfp_t gfp_mask, 11398faece5fSTyler Hicks unsigned int order) 11408faece5fSTyler Hicks { 11418faece5fSTyler Hicks struct page *page; 11428faece5fSTyler Hicks 11438faece5fSTyler Hicks page = alloc_pages(gfp_mask | __GFP_ZERO, order); 11448faece5fSTyler Hicks if (page) 11458faece5fSTyler Hicks return (unsigned long) page_address(page); 11468faece5fSTyler Hicks return 0; 11478faece5fSTyler Hicks } 11488faece5fSTyler Hicks 1149237fead6SMichael Halcrow /** 1150dd2a3b7aSMichael Halcrow * ecryptfs_write_metadata 1151b59db43aSTyler Hicks * @ecryptfs_dentry: The eCryptfs dentry, which should be negative 1152b59db43aSTyler Hicks * @ecryptfs_inode: The newly created eCryptfs inode 1153237fead6SMichael Halcrow * 1154237fead6SMichael Halcrow * Write the file headers out. This will likely involve a userspace 1155237fead6SMichael Halcrow * callout, in which the session key is encrypted with one or more 1156237fead6SMichael Halcrow * public keys and/or the passphrase necessary to do the encryption is 1157237fead6SMichael Halcrow * retrieved via a prompt. Exactly what happens at this point should 1158237fead6SMichael Halcrow * be policy-dependent. 1159237fead6SMichael Halcrow * 1160237fead6SMichael Halcrow * Returns zero on success; non-zero on error 1161237fead6SMichael Halcrow */ 1162b59db43aSTyler Hicks int ecryptfs_write_metadata(struct dentry *ecryptfs_dentry, 1163b59db43aSTyler Hicks struct inode *ecryptfs_inode) 1164237fead6SMichael Halcrow { 1165d7cdc5feSMichael Halcrow struct ecryptfs_crypt_stat *crypt_stat = 1166b59db43aSTyler Hicks &ecryptfs_inode_to_private(ecryptfs_inode)->crypt_stat; 11678faece5fSTyler Hicks unsigned int order; 1168cc11beffSMichael Halcrow char *virt; 11698faece5fSTyler Hicks size_t virt_len; 1170d7cdc5feSMichael Halcrow size_t size = 0; 1171237fead6SMichael Halcrow int rc = 0; 1172237fead6SMichael Halcrow 1173e2bd99ecSMichael Halcrow if (likely(crypt_stat->flags & ECRYPTFS_ENCRYPTED)) { 1174e2bd99ecSMichael Halcrow if (!(crypt_stat->flags & ECRYPTFS_KEY_VALID)) { 1175d7cdc5feSMichael Halcrow printk(KERN_ERR "Key is invalid; bailing out\n"); 1176237fead6SMichael Halcrow rc = -EINVAL; 1177237fead6SMichael Halcrow goto out; 1178237fead6SMichael Halcrow } 1179237fead6SMichael Halcrow } else { 1180cc11beffSMichael Halcrow printk(KERN_WARNING "%s: Encrypted flag not set\n", 118118d1dbf1SHarvey Harrison __func__); 1182237fead6SMichael Halcrow rc = -EINVAL; 1183237fead6SMichael Halcrow goto out; 1184237fead6SMichael Halcrow } 1185fa3ef1cbSTyler Hicks virt_len = crypt_stat->metadata_size; 11868faece5fSTyler Hicks order = get_order(virt_len); 1187237fead6SMichael Halcrow /* Released in this function */ 11888faece5fSTyler Hicks virt = (char *)ecryptfs_get_zeroed_pages(GFP_KERNEL, order); 1189cc11beffSMichael Halcrow if (!virt) { 119018d1dbf1SHarvey Harrison printk(KERN_ERR "%s: Out of memory\n", __func__); 1191237fead6SMichael Halcrow rc = -ENOMEM; 1192237fead6SMichael Halcrow goto out; 1193237fead6SMichael Halcrow } 1194bd4f0fe8STyler Hicks /* Zeroed page ensures the in-header unencrypted i_size is set to 0 */ 11958faece5fSTyler Hicks rc = ecryptfs_write_headers_virt(virt, virt_len, &size, crypt_stat, 11968faece5fSTyler Hicks ecryptfs_dentry); 1197237fead6SMichael Halcrow if (unlikely(rc)) { 1198cc11beffSMichael Halcrow printk(KERN_ERR "%s: Error whilst writing headers; rc = [%d]\n", 119918d1dbf1SHarvey Harrison __func__, rc); 1200237fead6SMichael Halcrow goto out_free; 1201237fead6SMichael Halcrow } 1202dd2a3b7aSMichael Halcrow if (crypt_stat->flags & ECRYPTFS_METADATA_IN_XATTR) 12033767e255SAl Viro rc = ecryptfs_write_metadata_to_xattr(ecryptfs_dentry, ecryptfs_inode, 12043767e255SAl Viro virt, size); 1205dd2a3b7aSMichael Halcrow else 1206b59db43aSTyler Hicks rc = ecryptfs_write_metadata_to_contents(ecryptfs_inode, virt, 12078faece5fSTyler Hicks virt_len); 1208dd2a3b7aSMichael Halcrow if (rc) { 1209cc11beffSMichael Halcrow printk(KERN_ERR "%s: Error writing metadata out to lower file; " 121018d1dbf1SHarvey Harrison "rc = [%d]\n", __func__, rc); 1211dd2a3b7aSMichael Halcrow goto out_free; 1212237fead6SMichael Halcrow } 1213237fead6SMichael Halcrow out_free: 12148faece5fSTyler Hicks free_pages((unsigned long)virt, order); 1215237fead6SMichael Halcrow out: 1216237fead6SMichael Halcrow return rc; 1217237fead6SMichael Halcrow } 1218237fead6SMichael Halcrow 1219dd2a3b7aSMichael Halcrow #define ECRYPTFS_DONT_VALIDATE_HEADER_SIZE 0 1220dd2a3b7aSMichael Halcrow #define ECRYPTFS_VALIDATE_HEADER_SIZE 1 1221237fead6SMichael Halcrow static int parse_header_metadata(struct ecryptfs_crypt_stat *crypt_stat, 1222dd2a3b7aSMichael Halcrow char *virt, int *bytes_read, 1223dd2a3b7aSMichael Halcrow int validate_header_size) 1224237fead6SMichael Halcrow { 1225237fead6SMichael Halcrow int rc = 0; 1226237fead6SMichael Halcrow u32 header_extent_size; 1227237fead6SMichael Halcrow u16 num_header_extents_at_front; 1228237fead6SMichael Halcrow 122929335c6aSHarvey Harrison header_extent_size = get_unaligned_be32(virt); 123029335c6aSHarvey Harrison virt += sizeof(__be32); 123129335c6aSHarvey Harrison num_header_extents_at_front = get_unaligned_be16(virt); 1232fa3ef1cbSTyler Hicks crypt_stat->metadata_size = (((size_t)num_header_extents_at_front 1233cc11beffSMichael Halcrow * (size_t)header_extent_size)); 123429335c6aSHarvey Harrison (*bytes_read) = (sizeof(__be32) + sizeof(__be16)); 1235dd2a3b7aSMichael Halcrow if ((validate_header_size == ECRYPTFS_VALIDATE_HEADER_SIZE) 1236fa3ef1cbSTyler Hicks && (crypt_stat->metadata_size 1237dd2a3b7aSMichael Halcrow < ECRYPTFS_MINIMUM_HEADER_EXTENT_SIZE)) { 1238237fead6SMichael Halcrow rc = -EINVAL; 1239cc11beffSMichael Halcrow printk(KERN_WARNING "Invalid header size: [%zd]\n", 1240fa3ef1cbSTyler Hicks crypt_stat->metadata_size); 1241237fead6SMichael Halcrow } 1242237fead6SMichael Halcrow return rc; 1243237fead6SMichael Halcrow } 1244237fead6SMichael Halcrow 1245237fead6SMichael Halcrow /** 1246237fead6SMichael Halcrow * set_default_header_data 124722e78fafSMichael Halcrow * @crypt_stat: The cryptographic context 1248237fead6SMichael Halcrow * 1249237fead6SMichael Halcrow * For version 0 file format; this function is only for backwards 1250237fead6SMichael Halcrow * compatibility for files created with the prior versions of 1251237fead6SMichael Halcrow * eCryptfs. 1252237fead6SMichael Halcrow */ 1253237fead6SMichael Halcrow static void set_default_header_data(struct ecryptfs_crypt_stat *crypt_stat) 1254237fead6SMichael Halcrow { 1255fa3ef1cbSTyler Hicks crypt_stat->metadata_size = ECRYPTFS_MINIMUM_HEADER_EXTENT_SIZE; 1256237fead6SMichael Halcrow } 1257237fead6SMichael Halcrow 12583aeb86eaSTyler Hicks void ecryptfs_i_size_init(const char *page_virt, struct inode *inode) 12593aeb86eaSTyler Hicks { 12603aeb86eaSTyler Hicks struct ecryptfs_mount_crypt_stat *mount_crypt_stat; 12613aeb86eaSTyler Hicks struct ecryptfs_crypt_stat *crypt_stat; 12623aeb86eaSTyler Hicks u64 file_size; 12633aeb86eaSTyler Hicks 12643aeb86eaSTyler Hicks crypt_stat = &ecryptfs_inode_to_private(inode)->crypt_stat; 12653aeb86eaSTyler Hicks mount_crypt_stat = 12663aeb86eaSTyler Hicks &ecryptfs_superblock_to_private(inode->i_sb)->mount_crypt_stat; 12673aeb86eaSTyler Hicks if (mount_crypt_stat->flags & ECRYPTFS_ENCRYPTED_VIEW_ENABLED) { 12683aeb86eaSTyler Hicks file_size = i_size_read(ecryptfs_inode_to_lower(inode)); 12693aeb86eaSTyler Hicks if (crypt_stat->flags & ECRYPTFS_METADATA_IN_XATTR) 12703aeb86eaSTyler Hicks file_size += crypt_stat->metadata_size; 12713aeb86eaSTyler Hicks } else 12723aeb86eaSTyler Hicks file_size = get_unaligned_be64(page_virt); 12733aeb86eaSTyler Hicks i_size_write(inode, (loff_t)file_size); 12743aeb86eaSTyler Hicks crypt_stat->flags |= ECRYPTFS_I_SIZE_INITIALIZED; 12753aeb86eaSTyler Hicks } 12763aeb86eaSTyler Hicks 1277237fead6SMichael Halcrow /** 1278237fead6SMichael Halcrow * ecryptfs_read_headers_virt 127922e78fafSMichael Halcrow * @page_virt: The virtual address into which to read the headers 128022e78fafSMichael Halcrow * @crypt_stat: The cryptographic context 128122e78fafSMichael Halcrow * @ecryptfs_dentry: The eCryptfs dentry 128222e78fafSMichael Halcrow * @validate_header_size: Whether to validate the header size while reading 1283237fead6SMichael Halcrow * 1284237fead6SMichael Halcrow * Read/parse the header data. The header format is detailed in the 1285237fead6SMichael Halcrow * comment block for the ecryptfs_write_headers_virt() function. 1286237fead6SMichael Halcrow * 1287237fead6SMichael Halcrow * Returns zero on success 1288237fead6SMichael Halcrow */ 1289237fead6SMichael Halcrow static int ecryptfs_read_headers_virt(char *page_virt, 1290237fead6SMichael Halcrow struct ecryptfs_crypt_stat *crypt_stat, 1291dd2a3b7aSMichael Halcrow struct dentry *ecryptfs_dentry, 1292dd2a3b7aSMichael Halcrow int validate_header_size) 1293237fead6SMichael Halcrow { 1294237fead6SMichael Halcrow int rc = 0; 1295237fead6SMichael Halcrow int offset; 1296237fead6SMichael Halcrow int bytes_read; 1297237fead6SMichael Halcrow 1298237fead6SMichael Halcrow ecryptfs_set_default_sizes(crypt_stat); 1299237fead6SMichael Halcrow crypt_stat->mount_crypt_stat = &ecryptfs_superblock_to_private( 1300237fead6SMichael Halcrow ecryptfs_dentry->d_sb)->mount_crypt_stat; 1301237fead6SMichael Halcrow offset = ECRYPTFS_FILE_SIZE_BYTES; 13027a86617eSTyler Hicks rc = ecryptfs_validate_marker(page_virt + offset); 13037a86617eSTyler Hicks if (rc) 1304237fead6SMichael Halcrow goto out; 13053aeb86eaSTyler Hicks if (!(crypt_stat->flags & ECRYPTFS_I_SIZE_INITIALIZED)) 13062b0143b5SDavid Howells ecryptfs_i_size_init(page_virt, d_inode(ecryptfs_dentry)); 1307237fead6SMichael Halcrow offset += MAGIC_ECRYPTFS_MARKER_SIZE_BYTES; 1308237fead6SMichael Halcrow rc = ecryptfs_process_flags(crypt_stat, (page_virt + offset), 1309237fead6SMichael Halcrow &bytes_read); 1310237fead6SMichael Halcrow if (rc) { 1311237fead6SMichael Halcrow ecryptfs_printk(KERN_WARNING, "Error processing flags\n"); 1312237fead6SMichael Halcrow goto out; 1313237fead6SMichael Halcrow } 1314237fead6SMichael Halcrow if (crypt_stat->file_version > ECRYPTFS_SUPPORTED_FILE_VERSION) { 1315237fead6SMichael Halcrow ecryptfs_printk(KERN_WARNING, "File version is [%d]; only " 1316237fead6SMichael Halcrow "file version [%d] is supported by this " 1317237fead6SMichael Halcrow "version of eCryptfs\n", 1318237fead6SMichael Halcrow crypt_stat->file_version, 1319237fead6SMichael Halcrow ECRYPTFS_SUPPORTED_FILE_VERSION); 1320237fead6SMichael Halcrow rc = -EINVAL; 1321237fead6SMichael Halcrow goto out; 1322237fead6SMichael Halcrow } 1323237fead6SMichael Halcrow offset += bytes_read; 1324237fead6SMichael Halcrow if (crypt_stat->file_version >= 1) { 1325237fead6SMichael Halcrow rc = parse_header_metadata(crypt_stat, (page_virt + offset), 1326dd2a3b7aSMichael Halcrow &bytes_read, validate_header_size); 1327237fead6SMichael Halcrow if (rc) { 1328237fead6SMichael Halcrow ecryptfs_printk(KERN_WARNING, "Error reading header " 1329237fead6SMichael Halcrow "metadata; rc = [%d]\n", rc); 1330237fead6SMichael Halcrow } 1331237fead6SMichael Halcrow offset += bytes_read; 1332237fead6SMichael Halcrow } else 1333237fead6SMichael Halcrow set_default_header_data(crypt_stat); 1334237fead6SMichael Halcrow rc = ecryptfs_parse_packet_set(crypt_stat, (page_virt + offset), 1335237fead6SMichael Halcrow ecryptfs_dentry); 1336237fead6SMichael Halcrow out: 1337237fead6SMichael Halcrow return rc; 1338237fead6SMichael Halcrow } 1339237fead6SMichael Halcrow 1340237fead6SMichael Halcrow /** 1341dd2a3b7aSMichael Halcrow * ecryptfs_read_xattr_region 134222e78fafSMichael Halcrow * @page_virt: The vitual address into which to read the xattr data 13432ed92554SMichael Halcrow * @ecryptfs_inode: The eCryptfs inode 1344dd2a3b7aSMichael Halcrow * 1345dd2a3b7aSMichael Halcrow * Attempts to read the crypto metadata from the extended attribute 1346dd2a3b7aSMichael Halcrow * region of the lower file. 134722e78fafSMichael Halcrow * 134822e78fafSMichael Halcrow * Returns zero on success; non-zero on error 1349dd2a3b7aSMichael Halcrow */ 1350d7cdc5feSMichael Halcrow int ecryptfs_read_xattr_region(char *page_virt, struct inode *ecryptfs_inode) 1351dd2a3b7aSMichael Halcrow { 1352d7cdc5feSMichael Halcrow struct dentry *lower_dentry = 1353b583043eSAl Viro ecryptfs_inode_to_private(ecryptfs_inode)->lower_file->f_path.dentry; 1354dd2a3b7aSMichael Halcrow ssize_t size; 1355dd2a3b7aSMichael Halcrow int rc = 0; 1356dd2a3b7aSMichael Halcrow 1357ce23e640SAl Viro size = ecryptfs_getxattr_lower(lower_dentry, 1358ce23e640SAl Viro ecryptfs_inode_to_lower(ecryptfs_inode), 1359ce23e640SAl Viro ECRYPTFS_XATTR_NAME, 1360dd2a3b7aSMichael Halcrow page_virt, ECRYPTFS_DEFAULT_EXTENT_SIZE); 1361dd2a3b7aSMichael Halcrow if (size < 0) { 136225bd8174SMichael Halcrow if (unlikely(ecryptfs_verbosity > 0)) 136325bd8174SMichael Halcrow printk(KERN_INFO "Error attempting to read the [%s] " 136425bd8174SMichael Halcrow "xattr from the lower file; return value = " 136525bd8174SMichael Halcrow "[%zd]\n", ECRYPTFS_XATTR_NAME, size); 1366dd2a3b7aSMichael Halcrow rc = -EINVAL; 1367dd2a3b7aSMichael Halcrow goto out; 1368dd2a3b7aSMichael Halcrow } 1369dd2a3b7aSMichael Halcrow out: 1370dd2a3b7aSMichael Halcrow return rc; 1371dd2a3b7aSMichael Halcrow } 1372dd2a3b7aSMichael Halcrow 1373778aeb42STyler Hicks int ecryptfs_read_and_validate_xattr_region(struct dentry *dentry, 13743b06b3ebSTyler Hicks struct inode *inode) 1375dd2a3b7aSMichael Halcrow { 1376778aeb42STyler Hicks u8 file_size[ECRYPTFS_SIZE_AND_MARKER_BYTES]; 1377778aeb42STyler Hicks u8 *marker = file_size + ECRYPTFS_FILE_SIZE_BYTES; 1378dd2a3b7aSMichael Halcrow int rc; 1379dd2a3b7aSMichael Halcrow 1380778aeb42STyler Hicks rc = ecryptfs_getxattr_lower(ecryptfs_dentry_to_lower(dentry), 1381ce23e640SAl Viro ecryptfs_inode_to_lower(inode), 1382778aeb42STyler Hicks ECRYPTFS_XATTR_NAME, file_size, 1383778aeb42STyler Hicks ECRYPTFS_SIZE_AND_MARKER_BYTES); 1384778aeb42STyler Hicks if (rc < ECRYPTFS_SIZE_AND_MARKER_BYTES) 1385778aeb42STyler Hicks return rc >= 0 ? -EINVAL : rc; 1386778aeb42STyler Hicks rc = ecryptfs_validate_marker(marker); 1387778aeb42STyler Hicks if (!rc) 1388778aeb42STyler Hicks ecryptfs_i_size_init(file_size, inode); 1389dd2a3b7aSMichael Halcrow return rc; 1390dd2a3b7aSMichael Halcrow } 1391dd2a3b7aSMichael Halcrow 1392dd2a3b7aSMichael Halcrow /** 1393dd2a3b7aSMichael Halcrow * ecryptfs_read_metadata 1394dd2a3b7aSMichael Halcrow * 1395dd2a3b7aSMichael Halcrow * Common entry point for reading file metadata. From here, we could 1396dd2a3b7aSMichael Halcrow * retrieve the header information from the header region of the file, 139740f0fd37SChris J Arges * the xattr region of the file, or some other repository that is 1398dd2a3b7aSMichael Halcrow * stored separately from the file itself. The current implementation 1399dd2a3b7aSMichael Halcrow * supports retrieving the metadata information from the file contents 1400dd2a3b7aSMichael Halcrow * and from the xattr region. 1401237fead6SMichael Halcrow * 1402237fead6SMichael Halcrow * Returns zero if valid headers found and parsed; non-zero otherwise 1403237fead6SMichael Halcrow */ 1404d7cdc5feSMichael Halcrow int ecryptfs_read_metadata(struct dentry *ecryptfs_dentry) 1405237fead6SMichael Halcrow { 1406bb450361STim Gardner int rc; 1407bb450361STim Gardner char *page_virt; 14082b0143b5SDavid Howells struct inode *ecryptfs_inode = d_inode(ecryptfs_dentry); 1409237fead6SMichael Halcrow struct ecryptfs_crypt_stat *crypt_stat = 1410d7cdc5feSMichael Halcrow &ecryptfs_inode_to_private(ecryptfs_inode)->crypt_stat; 1411e77a56ddSMichael Halcrow struct ecryptfs_mount_crypt_stat *mount_crypt_stat = 1412e77a56ddSMichael Halcrow &ecryptfs_superblock_to_private( 1413e77a56ddSMichael Halcrow ecryptfs_dentry->d_sb)->mount_crypt_stat; 1414237fead6SMichael Halcrow 1415e77a56ddSMichael Halcrow ecryptfs_copy_mount_wide_flags_to_inode_flags(crypt_stat, 1416e77a56ddSMichael Halcrow mount_crypt_stat); 1417237fead6SMichael Halcrow /* Read the first page from the underlying file */ 141830632870STyler Hicks page_virt = kmem_cache_alloc(ecryptfs_header_cache, GFP_USER); 1419237fead6SMichael Halcrow if (!page_virt) { 1420237fead6SMichael Halcrow rc = -ENOMEM; 1421237fead6SMichael Halcrow goto out; 1422237fead6SMichael Halcrow } 1423d7cdc5feSMichael Halcrow rc = ecryptfs_read_lower(page_virt, 0, crypt_stat->extent_size, 1424d7cdc5feSMichael Halcrow ecryptfs_inode); 142596a7b9c2STyler Hicks if (rc >= 0) 1426237fead6SMichael Halcrow rc = ecryptfs_read_headers_virt(page_virt, crypt_stat, 1427dd2a3b7aSMichael Halcrow ecryptfs_dentry, 1428dd2a3b7aSMichael Halcrow ECRYPTFS_VALIDATE_HEADER_SIZE); 1429dd2a3b7aSMichael Halcrow if (rc) { 1430bb450361STim Gardner /* metadata is not in the file header, so try xattrs */ 143109cbfeafSKirill A. Shutemov memset(page_virt, 0, PAGE_SIZE); 1432d7cdc5feSMichael Halcrow rc = ecryptfs_read_xattr_region(page_virt, ecryptfs_inode); 1433237fead6SMichael Halcrow if (rc) { 1434dd2a3b7aSMichael Halcrow printk(KERN_DEBUG "Valid eCryptfs headers not found in " 143530373dc0STim Gardner "file header region or xattr region, inode %lu\n", 143630373dc0STim Gardner ecryptfs_inode->i_ino); 1437237fead6SMichael Halcrow rc = -EINVAL; 1438dd2a3b7aSMichael Halcrow goto out; 1439dd2a3b7aSMichael Halcrow } 1440dd2a3b7aSMichael Halcrow rc = ecryptfs_read_headers_virt(page_virt, crypt_stat, 1441dd2a3b7aSMichael Halcrow ecryptfs_dentry, 1442dd2a3b7aSMichael Halcrow ECRYPTFS_DONT_VALIDATE_HEADER_SIZE); 1443dd2a3b7aSMichael Halcrow if (rc) { 1444dd2a3b7aSMichael Halcrow printk(KERN_DEBUG "Valid eCryptfs headers not found in " 144530373dc0STim Gardner "file xattr region either, inode %lu\n", 144630373dc0STim Gardner ecryptfs_inode->i_ino); 1447dd2a3b7aSMichael Halcrow rc = -EINVAL; 1448dd2a3b7aSMichael Halcrow } 1449dd2a3b7aSMichael Halcrow if (crypt_stat->mount_crypt_stat->flags 1450dd2a3b7aSMichael Halcrow & ECRYPTFS_XATTR_METADATA_ENABLED) { 1451dd2a3b7aSMichael Halcrow crypt_stat->flags |= ECRYPTFS_METADATA_IN_XATTR; 1452dd2a3b7aSMichael Halcrow } else { 1453dd2a3b7aSMichael Halcrow printk(KERN_WARNING "Attempt to access file with " 1454dd2a3b7aSMichael Halcrow "crypto metadata only in the extended attribute " 1455dd2a3b7aSMichael Halcrow "region, but eCryptfs was mounted without " 1456dd2a3b7aSMichael Halcrow "xattr support enabled. eCryptfs will not treat " 145730373dc0STim Gardner "this like an encrypted file, inode %lu\n", 145830373dc0STim Gardner ecryptfs_inode->i_ino); 1459dd2a3b7aSMichael Halcrow rc = -EINVAL; 1460dd2a3b7aSMichael Halcrow } 1461237fead6SMichael Halcrow } 1462237fead6SMichael Halcrow out: 1463237fead6SMichael Halcrow if (page_virt) { 146409cbfeafSKirill A. Shutemov memset(page_virt, 0, PAGE_SIZE); 146530632870STyler Hicks kmem_cache_free(ecryptfs_header_cache, page_virt); 1466237fead6SMichael Halcrow } 1467237fead6SMichael Halcrow return rc; 1468237fead6SMichael Halcrow } 1469237fead6SMichael Halcrow 1470237fead6SMichael Halcrow /** 147151ca58dcSMichael Halcrow * ecryptfs_encrypt_filename - encrypt filename 147251ca58dcSMichael Halcrow * 147351ca58dcSMichael Halcrow * CBC-encrypts the filename. We do not want to encrypt the same 147451ca58dcSMichael Halcrow * filename with the same key and IV, which may happen with hard 147551ca58dcSMichael Halcrow * links, so we prepend random bits to each filename. 147651ca58dcSMichael Halcrow * 147751ca58dcSMichael Halcrow * Returns zero on success; non-zero otherwise 147851ca58dcSMichael Halcrow */ 147951ca58dcSMichael Halcrow static int 148051ca58dcSMichael Halcrow ecryptfs_encrypt_filename(struct ecryptfs_filename *filename, 148151ca58dcSMichael Halcrow struct ecryptfs_mount_crypt_stat *mount_crypt_stat) 148251ca58dcSMichael Halcrow { 148351ca58dcSMichael Halcrow int rc = 0; 148451ca58dcSMichael Halcrow 148551ca58dcSMichael Halcrow filename->encrypted_filename = NULL; 148651ca58dcSMichael Halcrow filename->encrypted_filename_size = 0; 148797c31606SAl Viro if (mount_crypt_stat && (mount_crypt_stat->flags 148897c31606SAl Viro & ECRYPTFS_GLOBAL_ENCFN_USE_MOUNT_FNEK)) { 148951ca58dcSMichael Halcrow size_t packet_size; 149051ca58dcSMichael Halcrow size_t remaining_bytes; 149151ca58dcSMichael Halcrow 149251ca58dcSMichael Halcrow rc = ecryptfs_write_tag_70_packet( 149351ca58dcSMichael Halcrow NULL, NULL, 149451ca58dcSMichael Halcrow &filename->encrypted_filename_size, 149551ca58dcSMichael Halcrow mount_crypt_stat, NULL, 149651ca58dcSMichael Halcrow filename->filename_size); 149751ca58dcSMichael Halcrow if (rc) { 149851ca58dcSMichael Halcrow printk(KERN_ERR "%s: Error attempting to get packet " 149951ca58dcSMichael Halcrow "size for tag 72; rc = [%d]\n", __func__, 150051ca58dcSMichael Halcrow rc); 150151ca58dcSMichael Halcrow filename->encrypted_filename_size = 0; 150251ca58dcSMichael Halcrow goto out; 150351ca58dcSMichael Halcrow } 150451ca58dcSMichael Halcrow filename->encrypted_filename = 150551ca58dcSMichael Halcrow kmalloc(filename->encrypted_filename_size, GFP_KERNEL); 150651ca58dcSMichael Halcrow if (!filename->encrypted_filename) { 150751ca58dcSMichael Halcrow rc = -ENOMEM; 150851ca58dcSMichael Halcrow goto out; 150951ca58dcSMichael Halcrow } 151051ca58dcSMichael Halcrow remaining_bytes = filename->encrypted_filename_size; 151151ca58dcSMichael Halcrow rc = ecryptfs_write_tag_70_packet(filename->encrypted_filename, 151251ca58dcSMichael Halcrow &remaining_bytes, 151351ca58dcSMichael Halcrow &packet_size, 151451ca58dcSMichael Halcrow mount_crypt_stat, 151551ca58dcSMichael Halcrow filename->filename, 151651ca58dcSMichael Halcrow filename->filename_size); 151751ca58dcSMichael Halcrow if (rc) { 151851ca58dcSMichael Halcrow printk(KERN_ERR "%s: Error attempting to generate " 151951ca58dcSMichael Halcrow "tag 70 packet; rc = [%d]\n", __func__, 152051ca58dcSMichael Halcrow rc); 152151ca58dcSMichael Halcrow kfree(filename->encrypted_filename); 152251ca58dcSMichael Halcrow filename->encrypted_filename = NULL; 152351ca58dcSMichael Halcrow filename->encrypted_filename_size = 0; 152451ca58dcSMichael Halcrow goto out; 152551ca58dcSMichael Halcrow } 152651ca58dcSMichael Halcrow filename->encrypted_filename_size = packet_size; 152751ca58dcSMichael Halcrow } else { 152851ca58dcSMichael Halcrow printk(KERN_ERR "%s: No support for requested filename " 152951ca58dcSMichael Halcrow "encryption method in this release\n", __func__); 1530df6ad33bSTyler Hicks rc = -EOPNOTSUPP; 153151ca58dcSMichael Halcrow goto out; 153251ca58dcSMichael Halcrow } 153351ca58dcSMichael Halcrow out: 153451ca58dcSMichael Halcrow return rc; 153551ca58dcSMichael Halcrow } 153651ca58dcSMichael Halcrow 153751ca58dcSMichael Halcrow static int ecryptfs_copy_filename(char **copied_name, size_t *copied_name_size, 153851ca58dcSMichael Halcrow const char *name, size_t name_size) 153951ca58dcSMichael Halcrow { 154051ca58dcSMichael Halcrow int rc = 0; 154151ca58dcSMichael Halcrow 1542fd9fc842STyler Hicks (*copied_name) = kmalloc((name_size + 1), GFP_KERNEL); 154351ca58dcSMichael Halcrow if (!(*copied_name)) { 154451ca58dcSMichael Halcrow rc = -ENOMEM; 154551ca58dcSMichael Halcrow goto out; 154651ca58dcSMichael Halcrow } 154751ca58dcSMichael Halcrow memcpy((void *)(*copied_name), (void *)name, name_size); 154851ca58dcSMichael Halcrow (*copied_name)[(name_size)] = '\0'; /* Only for convenience 154951ca58dcSMichael Halcrow * in printing out the 155051ca58dcSMichael Halcrow * string in debug 155151ca58dcSMichael Halcrow * messages */ 1552fd9fc842STyler Hicks (*copied_name_size) = name_size; 155351ca58dcSMichael Halcrow out: 155451ca58dcSMichael Halcrow return rc; 155551ca58dcSMichael Halcrow } 155651ca58dcSMichael Halcrow 155751ca58dcSMichael Halcrow /** 1558f4aad16aSMichael Halcrow * ecryptfs_process_key_cipher - Perform key cipher initialization. 1559237fead6SMichael Halcrow * @key_tfm: Crypto context for key material, set by this function 1560e5d9cbdeSMichael Halcrow * @cipher_name: Name of the cipher 1561e5d9cbdeSMichael Halcrow * @key_size: Size of the key in bytes 1562237fead6SMichael Halcrow * 1563237fead6SMichael Halcrow * Returns zero on success. Any crypto_tfm structs allocated here 1564237fead6SMichael Halcrow * should be released by other functions, such as on a superblock put 1565237fead6SMichael Halcrow * event, regardless of whether this function succeeds for fails. 1566237fead6SMichael Halcrow */ 1567cd9d67dfSMichael Halcrow static int 15683095e8e3SHerbert Xu ecryptfs_process_key_cipher(struct crypto_skcipher **key_tfm, 1569f4aad16aSMichael Halcrow char *cipher_name, size_t *key_size) 1570237fead6SMichael Halcrow { 1571237fead6SMichael Halcrow char dummy_key[ECRYPTFS_MAX_KEY_BYTES]; 1572ece550f5SDan Carpenter char *full_alg_name = NULL; 1573237fead6SMichael Halcrow int rc; 1574237fead6SMichael Halcrow 1575e5d9cbdeSMichael Halcrow *key_tfm = NULL; 1576e5d9cbdeSMichael Halcrow if (*key_size > ECRYPTFS_MAX_KEY_BYTES) { 1577237fead6SMichael Halcrow rc = -EINVAL; 1578df261c52SMichael Halcrow printk(KERN_ERR "Requested key size is [%zd] bytes; maximum " 1579e5d9cbdeSMichael Halcrow "allowable is [%d]\n", *key_size, ECRYPTFS_MAX_KEY_BYTES); 1580237fead6SMichael Halcrow goto out; 1581237fead6SMichael Halcrow } 15828bba066fSMichael Halcrow rc = ecryptfs_crypto_api_algify_cipher_name(&full_alg_name, cipher_name, 15838bba066fSMichael Halcrow "ecb"); 15848bba066fSMichael Halcrow if (rc) 15858bba066fSMichael Halcrow goto out; 15863095e8e3SHerbert Xu *key_tfm = crypto_alloc_skcipher(full_alg_name, 0, CRYPTO_ALG_ASYNC); 15878bba066fSMichael Halcrow if (IS_ERR(*key_tfm)) { 15888bba066fSMichael Halcrow rc = PTR_ERR(*key_tfm); 1589237fead6SMichael Halcrow printk(KERN_ERR "Unable to allocate crypto cipher with name " 159038268498SDave Hansen "[%s]; rc = [%d]\n", full_alg_name, rc); 1591237fead6SMichael Halcrow goto out; 1592237fead6SMichael Halcrow } 15933095e8e3SHerbert Xu crypto_skcipher_set_flags(*key_tfm, CRYPTO_TFM_REQ_WEAK_KEY); 15943095e8e3SHerbert Xu if (*key_size == 0) 15953095e8e3SHerbert Xu *key_size = crypto_skcipher_default_keysize(*key_tfm); 1596e5d9cbdeSMichael Halcrow get_random_bytes(dummy_key, *key_size); 15973095e8e3SHerbert Xu rc = crypto_skcipher_setkey(*key_tfm, dummy_key, *key_size); 1598237fead6SMichael Halcrow if (rc) { 1599df261c52SMichael Halcrow printk(KERN_ERR "Error attempting to set key of size [%zd] for " 160038268498SDave Hansen "cipher [%s]; rc = [%d]\n", *key_size, full_alg_name, 160138268498SDave Hansen rc); 1602237fead6SMichael Halcrow rc = -EINVAL; 1603237fead6SMichael Halcrow goto out; 1604237fead6SMichael Halcrow } 1605237fead6SMichael Halcrow out: 1606ece550f5SDan Carpenter kfree(full_alg_name); 1607237fead6SMichael Halcrow return rc; 1608237fead6SMichael Halcrow } 1609f4aad16aSMichael Halcrow 1610f4aad16aSMichael Halcrow struct kmem_cache *ecryptfs_key_tfm_cache; 16117896b631SAdrian Bunk static struct list_head key_tfm_list; 1612af440f52SEric Sandeen struct mutex key_tfm_list_mutex; 1613f4aad16aSMichael Halcrow 16147371a382SJerome Marchand int __init ecryptfs_init_crypto(void) 1615f4aad16aSMichael Halcrow { 1616f4aad16aSMichael Halcrow mutex_init(&key_tfm_list_mutex); 1617f4aad16aSMichael Halcrow INIT_LIST_HEAD(&key_tfm_list); 1618f4aad16aSMichael Halcrow return 0; 1619f4aad16aSMichael Halcrow } 1620f4aad16aSMichael Halcrow 1621af440f52SEric Sandeen /** 1622af440f52SEric Sandeen * ecryptfs_destroy_crypto - free all cached key_tfms on key_tfm_list 1623af440f52SEric Sandeen * 1624af440f52SEric Sandeen * Called only at module unload time 1625af440f52SEric Sandeen */ 1626fcd12835SMichael Halcrow int ecryptfs_destroy_crypto(void) 1627f4aad16aSMichael Halcrow { 1628f4aad16aSMichael Halcrow struct ecryptfs_key_tfm *key_tfm, *key_tfm_tmp; 1629f4aad16aSMichael Halcrow 1630f4aad16aSMichael Halcrow mutex_lock(&key_tfm_list_mutex); 1631f4aad16aSMichael Halcrow list_for_each_entry_safe(key_tfm, key_tfm_tmp, &key_tfm_list, 1632f4aad16aSMichael Halcrow key_tfm_list) { 1633f4aad16aSMichael Halcrow list_del(&key_tfm->key_tfm_list); 16343095e8e3SHerbert Xu crypto_free_skcipher(key_tfm->key_tfm); 1635f4aad16aSMichael Halcrow kmem_cache_free(ecryptfs_key_tfm_cache, key_tfm); 1636f4aad16aSMichael Halcrow } 1637f4aad16aSMichael Halcrow mutex_unlock(&key_tfm_list_mutex); 1638f4aad16aSMichael Halcrow return 0; 1639f4aad16aSMichael Halcrow } 1640f4aad16aSMichael Halcrow 1641f4aad16aSMichael Halcrow int 1642f4aad16aSMichael Halcrow ecryptfs_add_new_key_tfm(struct ecryptfs_key_tfm **key_tfm, char *cipher_name, 1643f4aad16aSMichael Halcrow size_t key_size) 1644f4aad16aSMichael Halcrow { 1645f4aad16aSMichael Halcrow struct ecryptfs_key_tfm *tmp_tfm; 1646f4aad16aSMichael Halcrow int rc = 0; 1647f4aad16aSMichael Halcrow 1648af440f52SEric Sandeen BUG_ON(!mutex_is_locked(&key_tfm_list_mutex)); 1649af440f52SEric Sandeen 1650f4aad16aSMichael Halcrow tmp_tfm = kmem_cache_alloc(ecryptfs_key_tfm_cache, GFP_KERNEL); 16515032f360SMarkus Elfring if (key_tfm) 1652f4aad16aSMichael Halcrow (*key_tfm) = tmp_tfm; 1653f4aad16aSMichael Halcrow if (!tmp_tfm) { 1654f4aad16aSMichael Halcrow rc = -ENOMEM; 1655f4aad16aSMichael Halcrow goto out; 1656f4aad16aSMichael Halcrow } 1657f4aad16aSMichael Halcrow mutex_init(&tmp_tfm->key_tfm_mutex); 1658f4aad16aSMichael Halcrow strncpy(tmp_tfm->cipher_name, cipher_name, 1659f4aad16aSMichael Halcrow ECRYPTFS_MAX_CIPHER_NAME_SIZE); 1660b8862906SEric Sandeen tmp_tfm->cipher_name[ECRYPTFS_MAX_CIPHER_NAME_SIZE] = '\0'; 1661f4aad16aSMichael Halcrow tmp_tfm->key_size = key_size; 16625dda6992SMichael Halcrow rc = ecryptfs_process_key_cipher(&tmp_tfm->key_tfm, 1663f4aad16aSMichael Halcrow tmp_tfm->cipher_name, 16645dda6992SMichael Halcrow &tmp_tfm->key_size); 16655dda6992SMichael Halcrow if (rc) { 1666f4aad16aSMichael Halcrow printk(KERN_ERR "Error attempting to initialize key TFM " 1667f4aad16aSMichael Halcrow "cipher with name = [%s]; rc = [%d]\n", 1668f4aad16aSMichael Halcrow tmp_tfm->cipher_name, rc); 1669f4aad16aSMichael Halcrow kmem_cache_free(ecryptfs_key_tfm_cache, tmp_tfm); 16705032f360SMarkus Elfring if (key_tfm) 1671f4aad16aSMichael Halcrow (*key_tfm) = NULL; 1672f4aad16aSMichael Halcrow goto out; 1673f4aad16aSMichael Halcrow } 1674f4aad16aSMichael Halcrow list_add(&tmp_tfm->key_tfm_list, &key_tfm_list); 1675f4aad16aSMichael Halcrow out: 1676f4aad16aSMichael Halcrow return rc; 1677f4aad16aSMichael Halcrow } 1678f4aad16aSMichael Halcrow 1679af440f52SEric Sandeen /** 1680af440f52SEric Sandeen * ecryptfs_tfm_exists - Search for existing tfm for cipher_name. 1681af440f52SEric Sandeen * @cipher_name: the name of the cipher to search for 1682af440f52SEric Sandeen * @key_tfm: set to corresponding tfm if found 1683af440f52SEric Sandeen * 1684af440f52SEric Sandeen * Searches for cached key_tfm matching @cipher_name 1685af440f52SEric Sandeen * Must be called with &key_tfm_list_mutex held 1686af440f52SEric Sandeen * Returns 1 if found, with @key_tfm set 1687af440f52SEric Sandeen * Returns 0 if not found, with @key_tfm set to NULL 1688af440f52SEric Sandeen */ 1689af440f52SEric Sandeen int ecryptfs_tfm_exists(char *cipher_name, struct ecryptfs_key_tfm **key_tfm) 1690af440f52SEric Sandeen { 1691af440f52SEric Sandeen struct ecryptfs_key_tfm *tmp_key_tfm; 1692af440f52SEric Sandeen 1693af440f52SEric Sandeen BUG_ON(!mutex_is_locked(&key_tfm_list_mutex)); 1694af440f52SEric Sandeen 1695af440f52SEric Sandeen list_for_each_entry(tmp_key_tfm, &key_tfm_list, key_tfm_list) { 1696af440f52SEric Sandeen if (strcmp(tmp_key_tfm->cipher_name, cipher_name) == 0) { 1697af440f52SEric Sandeen if (key_tfm) 1698af440f52SEric Sandeen (*key_tfm) = tmp_key_tfm; 1699af440f52SEric Sandeen return 1; 1700af440f52SEric Sandeen } 1701af440f52SEric Sandeen } 1702af440f52SEric Sandeen if (key_tfm) 1703af440f52SEric Sandeen (*key_tfm) = NULL; 1704af440f52SEric Sandeen return 0; 1705af440f52SEric Sandeen } 1706af440f52SEric Sandeen 1707af440f52SEric Sandeen /** 1708af440f52SEric Sandeen * ecryptfs_get_tfm_and_mutex_for_cipher_name 1709af440f52SEric Sandeen * 1710af440f52SEric Sandeen * @tfm: set to cached tfm found, or new tfm created 1711af440f52SEric Sandeen * @tfm_mutex: set to mutex for cached tfm found, or new tfm created 1712af440f52SEric Sandeen * @cipher_name: the name of the cipher to search for and/or add 1713af440f52SEric Sandeen * 1714af440f52SEric Sandeen * Sets pointers to @tfm & @tfm_mutex matching @cipher_name. 1715af440f52SEric Sandeen * Searches for cached item first, and creates new if not found. 1716af440f52SEric Sandeen * Returns 0 on success, non-zero if adding new cipher failed 1717af440f52SEric Sandeen */ 17183095e8e3SHerbert Xu int ecryptfs_get_tfm_and_mutex_for_cipher_name(struct crypto_skcipher **tfm, 1719f4aad16aSMichael Halcrow struct mutex **tfm_mutex, 1720f4aad16aSMichael Halcrow char *cipher_name) 1721f4aad16aSMichael Halcrow { 1722f4aad16aSMichael Halcrow struct ecryptfs_key_tfm *key_tfm; 1723f4aad16aSMichael Halcrow int rc = 0; 1724f4aad16aSMichael Halcrow 1725f4aad16aSMichael Halcrow (*tfm) = NULL; 1726f4aad16aSMichael Halcrow (*tfm_mutex) = NULL; 1727af440f52SEric Sandeen 1728f4aad16aSMichael Halcrow mutex_lock(&key_tfm_list_mutex); 1729af440f52SEric Sandeen if (!ecryptfs_tfm_exists(cipher_name, &key_tfm)) { 17305dda6992SMichael Halcrow rc = ecryptfs_add_new_key_tfm(&key_tfm, cipher_name, 0); 17315dda6992SMichael Halcrow if (rc) { 1732af440f52SEric Sandeen printk(KERN_ERR "Error adding new key_tfm to list; " 1733af440f52SEric Sandeen "rc = [%d]\n", rc); 1734f4aad16aSMichael Halcrow goto out; 1735f4aad16aSMichael Halcrow } 1736af440f52SEric Sandeen } 1737f4aad16aSMichael Halcrow (*tfm) = key_tfm->key_tfm; 1738f4aad16aSMichael Halcrow (*tfm_mutex) = &key_tfm->key_tfm_mutex; 1739f4aad16aSMichael Halcrow out: 174071fd5179SCyrill Gorcunov mutex_unlock(&key_tfm_list_mutex); 1741f4aad16aSMichael Halcrow return rc; 1742f4aad16aSMichael Halcrow } 174351ca58dcSMichael Halcrow 174451ca58dcSMichael Halcrow /* 64 characters forming a 6-bit target field */ 174551ca58dcSMichael Halcrow static unsigned char *portable_filename_chars = ("-.0123456789ABCD" 174651ca58dcSMichael Halcrow "EFGHIJKLMNOPQRST" 174751ca58dcSMichael Halcrow "UVWXYZabcdefghij" 174851ca58dcSMichael Halcrow "klmnopqrstuvwxyz"); 174951ca58dcSMichael Halcrow 175051ca58dcSMichael Halcrow /* We could either offset on every reverse map or just pad some 0x00's 175151ca58dcSMichael Halcrow * at the front here */ 17520f751e64STyler Hicks static const unsigned char filename_rev_map[256] = { 175351ca58dcSMichael Halcrow 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, /* 7 */ 175451ca58dcSMichael Halcrow 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, /* 15 */ 175551ca58dcSMichael Halcrow 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, /* 23 */ 175651ca58dcSMichael Halcrow 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, /* 31 */ 175751ca58dcSMichael Halcrow 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, /* 39 */ 175851ca58dcSMichael Halcrow 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x01, 0x00, /* 47 */ 175951ca58dcSMichael Halcrow 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, /* 55 */ 176051ca58dcSMichael Halcrow 0x0A, 0x0B, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, /* 63 */ 176151ca58dcSMichael Halcrow 0x00, 0x0C, 0x0D, 0x0E, 0x0F, 0x10, 0x11, 0x12, /* 71 */ 176251ca58dcSMichael Halcrow 0x13, 0x14, 0x15, 0x16, 0x17, 0x18, 0x19, 0x1A, /* 79 */ 176351ca58dcSMichael Halcrow 0x1B, 0x1C, 0x1D, 0x1E, 0x1F, 0x20, 0x21, 0x22, /* 87 */ 176451ca58dcSMichael Halcrow 0x23, 0x24, 0x25, 0x00, 0x00, 0x00, 0x00, 0x00, /* 95 */ 176551ca58dcSMichael Halcrow 0x00, 0x26, 0x27, 0x28, 0x29, 0x2A, 0x2B, 0x2C, /* 103 */ 176651ca58dcSMichael Halcrow 0x2D, 0x2E, 0x2F, 0x30, 0x31, 0x32, 0x33, 0x34, /* 111 */ 176751ca58dcSMichael Halcrow 0x35, 0x36, 0x37, 0x38, 0x39, 0x3A, 0x3B, 0x3C, /* 119 */ 17680f751e64STyler Hicks 0x3D, 0x3E, 0x3F /* 123 - 255 initialized to 0x00 */ 176951ca58dcSMichael Halcrow }; 177051ca58dcSMichael Halcrow 177151ca58dcSMichael Halcrow /** 177251ca58dcSMichael Halcrow * ecryptfs_encode_for_filename 177351ca58dcSMichael Halcrow * @dst: Destination location for encoded filename 177451ca58dcSMichael Halcrow * @dst_size: Size of the encoded filename in bytes 177551ca58dcSMichael Halcrow * @src: Source location for the filename to encode 177651ca58dcSMichael Halcrow * @src_size: Size of the source in bytes 177751ca58dcSMichael Halcrow */ 177837028758SCong Ding static void ecryptfs_encode_for_filename(unsigned char *dst, size_t *dst_size, 177951ca58dcSMichael Halcrow unsigned char *src, size_t src_size) 178051ca58dcSMichael Halcrow { 178151ca58dcSMichael Halcrow size_t num_blocks; 178251ca58dcSMichael Halcrow size_t block_num = 0; 178351ca58dcSMichael Halcrow size_t dst_offset = 0; 178451ca58dcSMichael Halcrow unsigned char last_block[3]; 178551ca58dcSMichael Halcrow 178651ca58dcSMichael Halcrow if (src_size == 0) { 178751ca58dcSMichael Halcrow (*dst_size) = 0; 178851ca58dcSMichael Halcrow goto out; 178951ca58dcSMichael Halcrow } 179051ca58dcSMichael Halcrow num_blocks = (src_size / 3); 179151ca58dcSMichael Halcrow if ((src_size % 3) == 0) { 179251ca58dcSMichael Halcrow memcpy(last_block, (&src[src_size - 3]), 3); 179351ca58dcSMichael Halcrow } else { 179451ca58dcSMichael Halcrow num_blocks++; 179551ca58dcSMichael Halcrow last_block[2] = 0x00; 179651ca58dcSMichael Halcrow switch (src_size % 3) { 179751ca58dcSMichael Halcrow case 1: 179851ca58dcSMichael Halcrow last_block[0] = src[src_size - 1]; 179951ca58dcSMichael Halcrow last_block[1] = 0x00; 180051ca58dcSMichael Halcrow break; 180151ca58dcSMichael Halcrow case 2: 180251ca58dcSMichael Halcrow last_block[0] = src[src_size - 2]; 180351ca58dcSMichael Halcrow last_block[1] = src[src_size - 1]; 180451ca58dcSMichael Halcrow } 180551ca58dcSMichael Halcrow } 180651ca58dcSMichael Halcrow (*dst_size) = (num_blocks * 4); 180751ca58dcSMichael Halcrow if (!dst) 180851ca58dcSMichael Halcrow goto out; 180951ca58dcSMichael Halcrow while (block_num < num_blocks) { 181051ca58dcSMichael Halcrow unsigned char *src_block; 181151ca58dcSMichael Halcrow unsigned char dst_block[4]; 181251ca58dcSMichael Halcrow 181351ca58dcSMichael Halcrow if (block_num == (num_blocks - 1)) 181451ca58dcSMichael Halcrow src_block = last_block; 181551ca58dcSMichael Halcrow else 181651ca58dcSMichael Halcrow src_block = &src[block_num * 3]; 181751ca58dcSMichael Halcrow dst_block[0] = ((src_block[0] >> 2) & 0x3F); 181851ca58dcSMichael Halcrow dst_block[1] = (((src_block[0] << 4) & 0x30) 181951ca58dcSMichael Halcrow | ((src_block[1] >> 4) & 0x0F)); 182051ca58dcSMichael Halcrow dst_block[2] = (((src_block[1] << 2) & 0x3C) 182151ca58dcSMichael Halcrow | ((src_block[2] >> 6) & 0x03)); 182251ca58dcSMichael Halcrow dst_block[3] = (src_block[2] & 0x3F); 182351ca58dcSMichael Halcrow dst[dst_offset++] = portable_filename_chars[dst_block[0]]; 182451ca58dcSMichael Halcrow dst[dst_offset++] = portable_filename_chars[dst_block[1]]; 182551ca58dcSMichael Halcrow dst[dst_offset++] = portable_filename_chars[dst_block[2]]; 182651ca58dcSMichael Halcrow dst[dst_offset++] = portable_filename_chars[dst_block[3]]; 182751ca58dcSMichael Halcrow block_num++; 182851ca58dcSMichael Halcrow } 182951ca58dcSMichael Halcrow out: 183051ca58dcSMichael Halcrow return; 183151ca58dcSMichael Halcrow } 183251ca58dcSMichael Halcrow 18334a26620dSTyler Hicks static size_t ecryptfs_max_decoded_size(size_t encoded_size) 18344a26620dSTyler Hicks { 18354a26620dSTyler Hicks /* Not exact; conservatively long. Every block of 4 18364a26620dSTyler Hicks * encoded characters decodes into a block of 3 18374a26620dSTyler Hicks * decoded characters. This segment of code provides 18384a26620dSTyler Hicks * the caller with the maximum amount of allocated 18394a26620dSTyler Hicks * space that @dst will need to point to in a 18404a26620dSTyler Hicks * subsequent call. */ 18414a26620dSTyler Hicks return ((encoded_size + 1) * 3) / 4; 18424a26620dSTyler Hicks } 18434a26620dSTyler Hicks 184471c11c37SMichael Halcrow /** 184571c11c37SMichael Halcrow * ecryptfs_decode_from_filename 184671c11c37SMichael Halcrow * @dst: If NULL, this function only sets @dst_size and returns. If 184771c11c37SMichael Halcrow * non-NULL, this function decodes the encoded octets in @src 184871c11c37SMichael Halcrow * into the memory that @dst points to. 184971c11c37SMichael Halcrow * @dst_size: Set to the size of the decoded string. 185071c11c37SMichael Halcrow * @src: The encoded set of octets to decode. 185171c11c37SMichael Halcrow * @src_size: The size of the encoded set of octets to decode. 185271c11c37SMichael Halcrow */ 185371c11c37SMichael Halcrow static void 185471c11c37SMichael Halcrow ecryptfs_decode_from_filename(unsigned char *dst, size_t *dst_size, 185551ca58dcSMichael Halcrow const unsigned char *src, size_t src_size) 185651ca58dcSMichael Halcrow { 185751ca58dcSMichael Halcrow u8 current_bit_offset = 0; 185851ca58dcSMichael Halcrow size_t src_byte_offset = 0; 185951ca58dcSMichael Halcrow size_t dst_byte_offset = 0; 186051ca58dcSMichael Halcrow 18615032f360SMarkus Elfring if (!dst) { 18624a26620dSTyler Hicks (*dst_size) = ecryptfs_max_decoded_size(src_size); 186351ca58dcSMichael Halcrow goto out; 186451ca58dcSMichael Halcrow } 186551ca58dcSMichael Halcrow while (src_byte_offset < src_size) { 186651ca58dcSMichael Halcrow unsigned char src_byte = 186751ca58dcSMichael Halcrow filename_rev_map[(int)src[src_byte_offset]]; 186851ca58dcSMichael Halcrow 186951ca58dcSMichael Halcrow switch (current_bit_offset) { 187051ca58dcSMichael Halcrow case 0: 187151ca58dcSMichael Halcrow dst[dst_byte_offset] = (src_byte << 2); 187251ca58dcSMichael Halcrow current_bit_offset = 6; 187351ca58dcSMichael Halcrow break; 187451ca58dcSMichael Halcrow case 6: 187551ca58dcSMichael Halcrow dst[dst_byte_offset++] |= (src_byte >> 4); 187651ca58dcSMichael Halcrow dst[dst_byte_offset] = ((src_byte & 0xF) 187751ca58dcSMichael Halcrow << 4); 187851ca58dcSMichael Halcrow current_bit_offset = 4; 187951ca58dcSMichael Halcrow break; 188051ca58dcSMichael Halcrow case 4: 188151ca58dcSMichael Halcrow dst[dst_byte_offset++] |= (src_byte >> 2); 188251ca58dcSMichael Halcrow dst[dst_byte_offset] = (src_byte << 6); 188351ca58dcSMichael Halcrow current_bit_offset = 2; 188451ca58dcSMichael Halcrow break; 188551ca58dcSMichael Halcrow case 2: 188651ca58dcSMichael Halcrow dst[dst_byte_offset++] |= (src_byte); 188751ca58dcSMichael Halcrow current_bit_offset = 0; 188851ca58dcSMichael Halcrow break; 188951ca58dcSMichael Halcrow } 189051ca58dcSMichael Halcrow src_byte_offset++; 189151ca58dcSMichael Halcrow } 189251ca58dcSMichael Halcrow (*dst_size) = dst_byte_offset; 189351ca58dcSMichael Halcrow out: 189471c11c37SMichael Halcrow return; 189551ca58dcSMichael Halcrow } 189651ca58dcSMichael Halcrow 189751ca58dcSMichael Halcrow /** 189851ca58dcSMichael Halcrow * ecryptfs_encrypt_and_encode_filename - converts a plaintext file name to cipher text 189951ca58dcSMichael Halcrow * @crypt_stat: The crypt_stat struct associated with the file anem to encode 190051ca58dcSMichael Halcrow * @name: The plaintext name 190151ca58dcSMichael Halcrow * @length: The length of the plaintext 190251ca58dcSMichael Halcrow * @encoded_name: The encypted name 190351ca58dcSMichael Halcrow * 190451ca58dcSMichael Halcrow * Encrypts and encodes a filename into something that constitutes a 190551ca58dcSMichael Halcrow * valid filename for a filesystem, with printable characters. 190651ca58dcSMichael Halcrow * 190751ca58dcSMichael Halcrow * We assume that we have a properly initialized crypto context, 190851ca58dcSMichael Halcrow * pointed to by crypt_stat->tfm. 190951ca58dcSMichael Halcrow * 191051ca58dcSMichael Halcrow * Returns zero on success; non-zero on otherwise 191151ca58dcSMichael Halcrow */ 191251ca58dcSMichael Halcrow int ecryptfs_encrypt_and_encode_filename( 191351ca58dcSMichael Halcrow char **encoded_name, 191451ca58dcSMichael Halcrow size_t *encoded_name_size, 191551ca58dcSMichael Halcrow struct ecryptfs_mount_crypt_stat *mount_crypt_stat, 191651ca58dcSMichael Halcrow const char *name, size_t name_size) 191751ca58dcSMichael Halcrow { 191851ca58dcSMichael Halcrow size_t encoded_name_no_prefix_size; 191951ca58dcSMichael Halcrow int rc = 0; 192051ca58dcSMichael Halcrow 192151ca58dcSMichael Halcrow (*encoded_name) = NULL; 192251ca58dcSMichael Halcrow (*encoded_name_size) = 0; 192397c31606SAl Viro if (mount_crypt_stat && (mount_crypt_stat->flags 192497c31606SAl Viro & ECRYPTFS_GLOBAL_ENCRYPT_FILENAMES)) { 192551ca58dcSMichael Halcrow struct ecryptfs_filename *filename; 192651ca58dcSMichael Halcrow 192751ca58dcSMichael Halcrow filename = kzalloc(sizeof(*filename), GFP_KERNEL); 192851ca58dcSMichael Halcrow if (!filename) { 192951ca58dcSMichael Halcrow rc = -ENOMEM; 193051ca58dcSMichael Halcrow goto out; 193151ca58dcSMichael Halcrow } 193251ca58dcSMichael Halcrow filename->filename = (char *)name; 193351ca58dcSMichael Halcrow filename->filename_size = name_size; 193497c31606SAl Viro rc = ecryptfs_encrypt_filename(filename, mount_crypt_stat); 193551ca58dcSMichael Halcrow if (rc) { 193651ca58dcSMichael Halcrow printk(KERN_ERR "%s: Error attempting to encrypt " 193751ca58dcSMichael Halcrow "filename; rc = [%d]\n", __func__, rc); 193851ca58dcSMichael Halcrow kfree(filename); 193951ca58dcSMichael Halcrow goto out; 194051ca58dcSMichael Halcrow } 194151ca58dcSMichael Halcrow ecryptfs_encode_for_filename( 194251ca58dcSMichael Halcrow NULL, &encoded_name_no_prefix_size, 194351ca58dcSMichael Halcrow filename->encrypted_filename, 194451ca58dcSMichael Halcrow filename->encrypted_filename_size); 194597c31606SAl Viro if (mount_crypt_stat 194651ca58dcSMichael Halcrow && (mount_crypt_stat->flags 194797c31606SAl Viro & ECRYPTFS_GLOBAL_ENCFN_USE_MOUNT_FNEK)) 194851ca58dcSMichael Halcrow (*encoded_name_size) = 194951ca58dcSMichael Halcrow (ECRYPTFS_FNEK_ENCRYPTED_FILENAME_PREFIX_SIZE 195051ca58dcSMichael Halcrow + encoded_name_no_prefix_size); 195151ca58dcSMichael Halcrow else 195251ca58dcSMichael Halcrow (*encoded_name_size) = 195351ca58dcSMichael Halcrow (ECRYPTFS_FEK_ENCRYPTED_FILENAME_PREFIX_SIZE 195451ca58dcSMichael Halcrow + encoded_name_no_prefix_size); 195551ca58dcSMichael Halcrow (*encoded_name) = kmalloc((*encoded_name_size) + 1, GFP_KERNEL); 195651ca58dcSMichael Halcrow if (!(*encoded_name)) { 195751ca58dcSMichael Halcrow rc = -ENOMEM; 195851ca58dcSMichael Halcrow kfree(filename->encrypted_filename); 195951ca58dcSMichael Halcrow kfree(filename); 196051ca58dcSMichael Halcrow goto out; 196151ca58dcSMichael Halcrow } 196297c31606SAl Viro if (mount_crypt_stat 196351ca58dcSMichael Halcrow && (mount_crypt_stat->flags 196497c31606SAl Viro & ECRYPTFS_GLOBAL_ENCFN_USE_MOUNT_FNEK)) { 196551ca58dcSMichael Halcrow memcpy((*encoded_name), 196651ca58dcSMichael Halcrow ECRYPTFS_FNEK_ENCRYPTED_FILENAME_PREFIX, 196751ca58dcSMichael Halcrow ECRYPTFS_FNEK_ENCRYPTED_FILENAME_PREFIX_SIZE); 196851ca58dcSMichael Halcrow ecryptfs_encode_for_filename( 196951ca58dcSMichael Halcrow ((*encoded_name) 197051ca58dcSMichael Halcrow + ECRYPTFS_FNEK_ENCRYPTED_FILENAME_PREFIX_SIZE), 197151ca58dcSMichael Halcrow &encoded_name_no_prefix_size, 197251ca58dcSMichael Halcrow filename->encrypted_filename, 197351ca58dcSMichael Halcrow filename->encrypted_filename_size); 197451ca58dcSMichael Halcrow (*encoded_name_size) = 197551ca58dcSMichael Halcrow (ECRYPTFS_FNEK_ENCRYPTED_FILENAME_PREFIX_SIZE 197651ca58dcSMichael Halcrow + encoded_name_no_prefix_size); 197751ca58dcSMichael Halcrow (*encoded_name)[(*encoded_name_size)] = '\0'; 197851ca58dcSMichael Halcrow } else { 1979df6ad33bSTyler Hicks rc = -EOPNOTSUPP; 198051ca58dcSMichael Halcrow } 198151ca58dcSMichael Halcrow if (rc) { 198251ca58dcSMichael Halcrow printk(KERN_ERR "%s: Error attempting to encode " 198351ca58dcSMichael Halcrow "encrypted filename; rc = [%d]\n", __func__, 198451ca58dcSMichael Halcrow rc); 198551ca58dcSMichael Halcrow kfree((*encoded_name)); 198651ca58dcSMichael Halcrow (*encoded_name) = NULL; 198751ca58dcSMichael Halcrow (*encoded_name_size) = 0; 198851ca58dcSMichael Halcrow } 198951ca58dcSMichael Halcrow kfree(filename->encrypted_filename); 199051ca58dcSMichael Halcrow kfree(filename); 199151ca58dcSMichael Halcrow } else { 199251ca58dcSMichael Halcrow rc = ecryptfs_copy_filename(encoded_name, 199351ca58dcSMichael Halcrow encoded_name_size, 199451ca58dcSMichael Halcrow name, name_size); 199551ca58dcSMichael Halcrow } 199651ca58dcSMichael Halcrow out: 199751ca58dcSMichael Halcrow return rc; 199851ca58dcSMichael Halcrow } 199951ca58dcSMichael Halcrow 200051ca58dcSMichael Halcrow /** 200151ca58dcSMichael Halcrow * ecryptfs_decode_and_decrypt_filename - converts the encoded cipher text name to decoded plaintext 200251ca58dcSMichael Halcrow * @plaintext_name: The plaintext name 200351ca58dcSMichael Halcrow * @plaintext_name_size: The plaintext name size 200451ca58dcSMichael Halcrow * @ecryptfs_dir_dentry: eCryptfs directory dentry 200551ca58dcSMichael Halcrow * @name: The filename in cipher text 200651ca58dcSMichael Halcrow * @name_size: The cipher text name size 200751ca58dcSMichael Halcrow * 200851ca58dcSMichael Halcrow * Decrypts and decodes the filename. 200951ca58dcSMichael Halcrow * 201051ca58dcSMichael Halcrow * Returns zero on error; non-zero otherwise 201151ca58dcSMichael Halcrow */ 201251ca58dcSMichael Halcrow int ecryptfs_decode_and_decrypt_filename(char **plaintext_name, 201351ca58dcSMichael Halcrow size_t *plaintext_name_size, 20140747fdb2SAl Viro struct super_block *sb, 201551ca58dcSMichael Halcrow const char *name, size_t name_size) 201651ca58dcSMichael Halcrow { 20172aac0cf8STyler Hicks struct ecryptfs_mount_crypt_stat *mount_crypt_stat = 20180747fdb2SAl Viro &ecryptfs_superblock_to_private(sb)->mount_crypt_stat; 201951ca58dcSMichael Halcrow char *decoded_name; 202051ca58dcSMichael Halcrow size_t decoded_name_size; 202151ca58dcSMichael Halcrow size_t packet_size; 202251ca58dcSMichael Halcrow int rc = 0; 202351ca58dcSMichael Halcrow 20242aac0cf8STyler Hicks if ((mount_crypt_stat->flags & ECRYPTFS_GLOBAL_ENCRYPT_FILENAMES) 20252aac0cf8STyler Hicks && !(mount_crypt_stat->flags & ECRYPTFS_ENCRYPTED_VIEW_ENABLED) 20262aac0cf8STyler Hicks && (name_size > ECRYPTFS_FNEK_ENCRYPTED_FILENAME_PREFIX_SIZE) 202751ca58dcSMichael Halcrow && (strncmp(name, ECRYPTFS_FNEK_ENCRYPTED_FILENAME_PREFIX, 202851ca58dcSMichael Halcrow ECRYPTFS_FNEK_ENCRYPTED_FILENAME_PREFIX_SIZE) == 0)) { 202951ca58dcSMichael Halcrow const char *orig_name = name; 203051ca58dcSMichael Halcrow size_t orig_name_size = name_size; 203151ca58dcSMichael Halcrow 203251ca58dcSMichael Halcrow name += ECRYPTFS_FNEK_ENCRYPTED_FILENAME_PREFIX_SIZE; 203351ca58dcSMichael Halcrow name_size -= ECRYPTFS_FNEK_ENCRYPTED_FILENAME_PREFIX_SIZE; 203471c11c37SMichael Halcrow ecryptfs_decode_from_filename(NULL, &decoded_name_size, 203551ca58dcSMichael Halcrow name, name_size); 203651ca58dcSMichael Halcrow decoded_name = kmalloc(decoded_name_size, GFP_KERNEL); 203751ca58dcSMichael Halcrow if (!decoded_name) { 203851ca58dcSMichael Halcrow rc = -ENOMEM; 203951ca58dcSMichael Halcrow goto out; 204051ca58dcSMichael Halcrow } 204171c11c37SMichael Halcrow ecryptfs_decode_from_filename(decoded_name, &decoded_name_size, 204251ca58dcSMichael Halcrow name, name_size); 204351ca58dcSMichael Halcrow rc = ecryptfs_parse_tag_70_packet(plaintext_name, 204451ca58dcSMichael Halcrow plaintext_name_size, 204551ca58dcSMichael Halcrow &packet_size, 204651ca58dcSMichael Halcrow mount_crypt_stat, 204751ca58dcSMichael Halcrow decoded_name, 204851ca58dcSMichael Halcrow decoded_name_size); 204951ca58dcSMichael Halcrow if (rc) { 205051ca58dcSMichael Halcrow printk(KERN_INFO "%s: Could not parse tag 70 packet " 205151ca58dcSMichael Halcrow "from filename; copying through filename " 205251ca58dcSMichael Halcrow "as-is\n", __func__); 205351ca58dcSMichael Halcrow rc = ecryptfs_copy_filename(plaintext_name, 205451ca58dcSMichael Halcrow plaintext_name_size, 205551ca58dcSMichael Halcrow orig_name, orig_name_size); 205651ca58dcSMichael Halcrow goto out_free; 205751ca58dcSMichael Halcrow } 205851ca58dcSMichael Halcrow } else { 205951ca58dcSMichael Halcrow rc = ecryptfs_copy_filename(plaintext_name, 206051ca58dcSMichael Halcrow plaintext_name_size, 206151ca58dcSMichael Halcrow name, name_size); 206251ca58dcSMichael Halcrow goto out; 206351ca58dcSMichael Halcrow } 206451ca58dcSMichael Halcrow out_free: 206551ca58dcSMichael Halcrow kfree(decoded_name); 206651ca58dcSMichael Halcrow out: 206751ca58dcSMichael Halcrow return rc; 206851ca58dcSMichael Halcrow } 20694a26620dSTyler Hicks 20704a26620dSTyler Hicks #define ENC_NAME_MAX_BLOCKLEN_8_OR_16 143 20714a26620dSTyler Hicks 20724a26620dSTyler Hicks int ecryptfs_set_f_namelen(long *namelen, long lower_namelen, 20734a26620dSTyler Hicks struct ecryptfs_mount_crypt_stat *mount_crypt_stat) 20744a26620dSTyler Hicks { 20753095e8e3SHerbert Xu struct crypto_skcipher *tfm; 20764a26620dSTyler Hicks struct mutex *tfm_mutex; 20774a26620dSTyler Hicks size_t cipher_blocksize; 20784a26620dSTyler Hicks int rc; 20794a26620dSTyler Hicks 20804a26620dSTyler Hicks if (!(mount_crypt_stat->flags & ECRYPTFS_GLOBAL_ENCRYPT_FILENAMES)) { 20814a26620dSTyler Hicks (*namelen) = lower_namelen; 20824a26620dSTyler Hicks return 0; 20834a26620dSTyler Hicks } 20844a26620dSTyler Hicks 20853095e8e3SHerbert Xu rc = ecryptfs_get_tfm_and_mutex_for_cipher_name(&tfm, &tfm_mutex, 20864a26620dSTyler Hicks mount_crypt_stat->global_default_fn_cipher_name); 20874a26620dSTyler Hicks if (unlikely(rc)) { 20884a26620dSTyler Hicks (*namelen) = 0; 20894a26620dSTyler Hicks return rc; 20904a26620dSTyler Hicks } 20914a26620dSTyler Hicks 20924a26620dSTyler Hicks mutex_lock(tfm_mutex); 20933095e8e3SHerbert Xu cipher_blocksize = crypto_skcipher_blocksize(tfm); 20944a26620dSTyler Hicks mutex_unlock(tfm_mutex); 20954a26620dSTyler Hicks 20964a26620dSTyler Hicks /* Return an exact amount for the common cases */ 20974a26620dSTyler Hicks if (lower_namelen == NAME_MAX 20984a26620dSTyler Hicks && (cipher_blocksize == 8 || cipher_blocksize == 16)) { 20994a26620dSTyler Hicks (*namelen) = ENC_NAME_MAX_BLOCKLEN_8_OR_16; 21004a26620dSTyler Hicks return 0; 21014a26620dSTyler Hicks } 21024a26620dSTyler Hicks 21034a26620dSTyler Hicks /* Return a safe estimate for the uncommon cases */ 21044a26620dSTyler Hicks (*namelen) = lower_namelen; 21054a26620dSTyler Hicks (*namelen) -= ECRYPTFS_FNEK_ENCRYPTED_FILENAME_PREFIX_SIZE; 21064a26620dSTyler Hicks /* Since this is the max decoded size, subtract 1 "decoded block" len */ 21074a26620dSTyler Hicks (*namelen) = ecryptfs_max_decoded_size(*namelen) - 3; 21084a26620dSTyler Hicks (*namelen) -= ECRYPTFS_TAG_70_MAX_METADATA_SIZE; 21094a26620dSTyler Hicks (*namelen) -= ECRYPTFS_FILENAME_MIN_RANDOM_PREPEND_BYTES; 21104a26620dSTyler Hicks /* Worst case is that the filename is padded nearly a full block size */ 21114a26620dSTyler Hicks (*namelen) -= cipher_blocksize - 1; 21124a26620dSTyler Hicks 21134a26620dSTyler Hicks if ((*namelen) < 0) 21144a26620dSTyler Hicks (*namelen) = 0; 21154a26620dSTyler Hicks 21164a26620dSTyler Hicks return 0; 21174a26620dSTyler Hicks } 2118