1*237fead6SMichael Halcrow /** 2*237fead6SMichael Halcrow * eCryptfs: Linux filesystem encryption layer 3*237fead6SMichael Halcrow * 4*237fead6SMichael Halcrow * Copyright (C) 1997-2004 Erez Zadok 5*237fead6SMichael Halcrow * Copyright (C) 2001-2004 Stony Brook University 6*237fead6SMichael Halcrow * Copyright (C) 2004-2006 International Business Machines Corp. 7*237fead6SMichael Halcrow * Author(s): Michael A. Halcrow <mahalcro@us.ibm.com> 8*237fead6SMichael Halcrow * Michael C. Thompson <mcthomps@us.ibm.com> 9*237fead6SMichael Halcrow * 10*237fead6SMichael Halcrow * This program is free software; you can redistribute it and/or 11*237fead6SMichael Halcrow * modify it under the terms of the GNU General Public License as 12*237fead6SMichael Halcrow * published by the Free Software Foundation; either version 2 of the 13*237fead6SMichael Halcrow * License, or (at your option) any later version. 14*237fead6SMichael Halcrow * 15*237fead6SMichael Halcrow * This program is distributed in the hope that it will be useful, but 16*237fead6SMichael Halcrow * WITHOUT ANY WARRANTY; without even the implied warranty of 17*237fead6SMichael Halcrow * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 18*237fead6SMichael Halcrow * General Public License for more details. 19*237fead6SMichael Halcrow * 20*237fead6SMichael Halcrow * You should have received a copy of the GNU General Public License 21*237fead6SMichael Halcrow * along with this program; if not, write to the Free Software 22*237fead6SMichael Halcrow * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 23*237fead6SMichael Halcrow * 02111-1307, USA. 24*237fead6SMichael Halcrow */ 25*237fead6SMichael Halcrow 26*237fead6SMichael Halcrow #include <linux/fs.h> 27*237fead6SMichael Halcrow #include <linux/mount.h> 28*237fead6SMichael Halcrow #include <linux/pagemap.h> 29*237fead6SMichael Halcrow #include <linux/random.h> 30*237fead6SMichael Halcrow #include <linux/compiler.h> 31*237fead6SMichael Halcrow #include <linux/key.h> 32*237fead6SMichael Halcrow #include <linux/namei.h> 33*237fead6SMichael Halcrow #include <linux/crypto.h> 34*237fead6SMichael Halcrow #include <linux/file.h> 35*237fead6SMichael Halcrow #include <linux/scatterlist.h> 36*237fead6SMichael Halcrow #include "ecryptfs_kernel.h" 37*237fead6SMichael Halcrow 38*237fead6SMichael Halcrow static int 39*237fead6SMichael Halcrow ecryptfs_decrypt_page_offset(struct ecryptfs_crypt_stat *crypt_stat, 40*237fead6SMichael Halcrow struct page *dst_page, int dst_offset, 41*237fead6SMichael Halcrow struct page *src_page, int src_offset, int size, 42*237fead6SMichael Halcrow unsigned char *iv); 43*237fead6SMichael Halcrow static int 44*237fead6SMichael Halcrow ecryptfs_encrypt_page_offset(struct ecryptfs_crypt_stat *crypt_stat, 45*237fead6SMichael Halcrow struct page *dst_page, int dst_offset, 46*237fead6SMichael Halcrow struct page *src_page, int src_offset, int size, 47*237fead6SMichael Halcrow unsigned char *iv); 48*237fead6SMichael Halcrow 49*237fead6SMichael Halcrow /** 50*237fead6SMichael Halcrow * ecryptfs_to_hex 51*237fead6SMichael Halcrow * @dst: Buffer to take hex character representation of contents of 52*237fead6SMichael Halcrow * src; must be at least of size (src_size * 2) 53*237fead6SMichael Halcrow * @src: Buffer to be converted to a hex string respresentation 54*237fead6SMichael Halcrow * @src_size: number of bytes to convert 55*237fead6SMichael Halcrow */ 56*237fead6SMichael Halcrow void ecryptfs_to_hex(char *dst, char *src, size_t src_size) 57*237fead6SMichael Halcrow { 58*237fead6SMichael Halcrow int x; 59*237fead6SMichael Halcrow 60*237fead6SMichael Halcrow for (x = 0; x < src_size; x++) 61*237fead6SMichael Halcrow sprintf(&dst[x * 2], "%.2x", (unsigned char)src[x]); 62*237fead6SMichael Halcrow } 63*237fead6SMichael Halcrow 64*237fead6SMichael Halcrow /** 65*237fead6SMichael Halcrow * ecryptfs_from_hex 66*237fead6SMichael Halcrow * @dst: Buffer to take the bytes from src hex; must be at least of 67*237fead6SMichael Halcrow * size (src_size / 2) 68*237fead6SMichael Halcrow * @src: Buffer to be converted from a hex string respresentation to raw value 69*237fead6SMichael Halcrow * @dst_size: size of dst buffer, or number of hex characters pairs to convert 70*237fead6SMichael Halcrow */ 71*237fead6SMichael Halcrow void ecryptfs_from_hex(char *dst, char *src, int dst_size) 72*237fead6SMichael Halcrow { 73*237fead6SMichael Halcrow int x; 74*237fead6SMichael Halcrow char tmp[3] = { 0, }; 75*237fead6SMichael Halcrow 76*237fead6SMichael Halcrow for (x = 0; x < dst_size; x++) { 77*237fead6SMichael Halcrow tmp[0] = src[x * 2]; 78*237fead6SMichael Halcrow tmp[1] = src[x * 2 + 1]; 79*237fead6SMichael Halcrow dst[x] = (unsigned char)simple_strtol(tmp, NULL, 16); 80*237fead6SMichael Halcrow } 81*237fead6SMichael Halcrow } 82*237fead6SMichael Halcrow 83*237fead6SMichael Halcrow /** 84*237fead6SMichael Halcrow * ecryptfs_calculate_md5 - calculates the md5 of @src 85*237fead6SMichael Halcrow * @dst: Pointer to 16 bytes of allocated memory 86*237fead6SMichael Halcrow * @crypt_stat: Pointer to crypt_stat struct for the current inode 87*237fead6SMichael Halcrow * @src: Data to be md5'd 88*237fead6SMichael Halcrow * @len: Length of @src 89*237fead6SMichael Halcrow * 90*237fead6SMichael Halcrow * Uses the allocated crypto context that crypt_stat references to 91*237fead6SMichael Halcrow * generate the MD5 sum of the contents of src. 92*237fead6SMichael Halcrow */ 93*237fead6SMichael Halcrow static int ecryptfs_calculate_md5(char *dst, 94*237fead6SMichael Halcrow struct ecryptfs_crypt_stat *crypt_stat, 95*237fead6SMichael Halcrow char *src, int len) 96*237fead6SMichael Halcrow { 97*237fead6SMichael Halcrow int rc = 0; 98*237fead6SMichael Halcrow struct scatterlist sg; 99*237fead6SMichael Halcrow 100*237fead6SMichael Halcrow mutex_lock(&crypt_stat->cs_md5_tfm_mutex); 101*237fead6SMichael Halcrow sg_init_one(&sg, (u8 *)src, len); 102*237fead6SMichael Halcrow if (!crypt_stat->md5_tfm) { 103*237fead6SMichael Halcrow crypt_stat->md5_tfm = 104*237fead6SMichael Halcrow crypto_alloc_tfm("md5", CRYPTO_TFM_REQ_MAY_SLEEP); 105*237fead6SMichael Halcrow if (!crypt_stat->md5_tfm) { 106*237fead6SMichael Halcrow rc = -ENOMEM; 107*237fead6SMichael Halcrow ecryptfs_printk(KERN_ERR, "Error attempting to " 108*237fead6SMichael Halcrow "allocate crypto context\n"); 109*237fead6SMichael Halcrow goto out; 110*237fead6SMichael Halcrow } 111*237fead6SMichael Halcrow } 112*237fead6SMichael Halcrow crypto_digest_init(crypt_stat->md5_tfm); 113*237fead6SMichael Halcrow crypto_digest_update(crypt_stat->md5_tfm, &sg, 1); 114*237fead6SMichael Halcrow crypto_digest_final(crypt_stat->md5_tfm, dst); 115*237fead6SMichael Halcrow mutex_unlock(&crypt_stat->cs_md5_tfm_mutex); 116*237fead6SMichael Halcrow out: 117*237fead6SMichael Halcrow return rc; 118*237fead6SMichael Halcrow } 119*237fead6SMichael Halcrow 120*237fead6SMichael Halcrow /** 121*237fead6SMichael Halcrow * ecryptfs_derive_iv 122*237fead6SMichael Halcrow * @iv: destination for the derived iv vale 123*237fead6SMichael Halcrow * @crypt_stat: Pointer to crypt_stat struct for the current inode 124*237fead6SMichael Halcrow * @offset: Offset of the page whose's iv we are to derive 125*237fead6SMichael Halcrow * 126*237fead6SMichael Halcrow * Generate the initialization vector from the given root IV and page 127*237fead6SMichael Halcrow * offset. 128*237fead6SMichael Halcrow * 129*237fead6SMichael Halcrow * Returns zero on success; non-zero on error. 130*237fead6SMichael Halcrow */ 131*237fead6SMichael Halcrow static int ecryptfs_derive_iv(char *iv, struct ecryptfs_crypt_stat *crypt_stat, 132*237fead6SMichael Halcrow pgoff_t offset) 133*237fead6SMichael Halcrow { 134*237fead6SMichael Halcrow int rc = 0; 135*237fead6SMichael Halcrow char dst[MD5_DIGEST_SIZE]; 136*237fead6SMichael Halcrow char src[ECRYPTFS_MAX_IV_BYTES + 16]; 137*237fead6SMichael Halcrow 138*237fead6SMichael Halcrow if (unlikely(ecryptfs_verbosity > 0)) { 139*237fead6SMichael Halcrow ecryptfs_printk(KERN_DEBUG, "root iv:\n"); 140*237fead6SMichael Halcrow ecryptfs_dump_hex(crypt_stat->root_iv, crypt_stat->iv_bytes); 141*237fead6SMichael Halcrow } 142*237fead6SMichael Halcrow /* TODO: It is probably secure to just cast the least 143*237fead6SMichael Halcrow * significant bits of the root IV into an unsigned long and 144*237fead6SMichael Halcrow * add the offset to that rather than go through all this 145*237fead6SMichael Halcrow * hashing business. -Halcrow */ 146*237fead6SMichael Halcrow memcpy(src, crypt_stat->root_iv, crypt_stat->iv_bytes); 147*237fead6SMichael Halcrow memset((src + crypt_stat->iv_bytes), 0, 16); 148*237fead6SMichael Halcrow snprintf((src + crypt_stat->iv_bytes), 16, "%ld", offset); 149*237fead6SMichael Halcrow if (unlikely(ecryptfs_verbosity > 0)) { 150*237fead6SMichael Halcrow ecryptfs_printk(KERN_DEBUG, "source:\n"); 151*237fead6SMichael Halcrow ecryptfs_dump_hex(src, (crypt_stat->iv_bytes + 16)); 152*237fead6SMichael Halcrow } 153*237fead6SMichael Halcrow rc = ecryptfs_calculate_md5(dst, crypt_stat, src, 154*237fead6SMichael Halcrow (crypt_stat->iv_bytes + 16)); 155*237fead6SMichael Halcrow if (rc) { 156*237fead6SMichael Halcrow ecryptfs_printk(KERN_WARNING, "Error attempting to compute " 157*237fead6SMichael Halcrow "MD5 while generating IV for a page\n"); 158*237fead6SMichael Halcrow goto out; 159*237fead6SMichael Halcrow } 160*237fead6SMichael Halcrow memcpy(iv, dst, crypt_stat->iv_bytes); 161*237fead6SMichael Halcrow if (unlikely(ecryptfs_verbosity > 0)) { 162*237fead6SMichael Halcrow ecryptfs_printk(KERN_DEBUG, "derived iv:\n"); 163*237fead6SMichael Halcrow ecryptfs_dump_hex(iv, crypt_stat->iv_bytes); 164*237fead6SMichael Halcrow } 165*237fead6SMichael Halcrow out: 166*237fead6SMichael Halcrow return rc; 167*237fead6SMichael Halcrow } 168*237fead6SMichael Halcrow 169*237fead6SMichael Halcrow /** 170*237fead6SMichael Halcrow * ecryptfs_init_crypt_stat 171*237fead6SMichael Halcrow * @crypt_stat: Pointer to the crypt_stat struct to initialize. 172*237fead6SMichael Halcrow * 173*237fead6SMichael Halcrow * Initialize the crypt_stat structure. 174*237fead6SMichael Halcrow */ 175*237fead6SMichael Halcrow void 176*237fead6SMichael Halcrow ecryptfs_init_crypt_stat(struct ecryptfs_crypt_stat *crypt_stat) 177*237fead6SMichael Halcrow { 178*237fead6SMichael Halcrow memset((void *)crypt_stat, 0, sizeof(struct ecryptfs_crypt_stat)); 179*237fead6SMichael Halcrow mutex_init(&crypt_stat->cs_mutex); 180*237fead6SMichael Halcrow mutex_init(&crypt_stat->cs_tfm_mutex); 181*237fead6SMichael Halcrow mutex_init(&crypt_stat->cs_md5_tfm_mutex); 182*237fead6SMichael Halcrow ECRYPTFS_SET_FLAG(crypt_stat->flags, ECRYPTFS_STRUCT_INITIALIZED); 183*237fead6SMichael Halcrow } 184*237fead6SMichael Halcrow 185*237fead6SMichael Halcrow /** 186*237fead6SMichael Halcrow * ecryptfs_destruct_crypt_stat 187*237fead6SMichael Halcrow * @crypt_stat: Pointer to the crypt_stat struct to initialize. 188*237fead6SMichael Halcrow * 189*237fead6SMichael Halcrow * Releases all memory associated with a crypt_stat struct. 190*237fead6SMichael Halcrow */ 191*237fead6SMichael Halcrow void ecryptfs_destruct_crypt_stat(struct ecryptfs_crypt_stat *crypt_stat) 192*237fead6SMichael Halcrow { 193*237fead6SMichael Halcrow if (crypt_stat->tfm) 194*237fead6SMichael Halcrow crypto_free_tfm(crypt_stat->tfm); 195*237fead6SMichael Halcrow if (crypt_stat->md5_tfm) 196*237fead6SMichael Halcrow crypto_free_tfm(crypt_stat->md5_tfm); 197*237fead6SMichael Halcrow memset(crypt_stat, 0, sizeof(struct ecryptfs_crypt_stat)); 198*237fead6SMichael Halcrow } 199*237fead6SMichael Halcrow 200*237fead6SMichael Halcrow void ecryptfs_destruct_mount_crypt_stat( 201*237fead6SMichael Halcrow struct ecryptfs_mount_crypt_stat *mount_crypt_stat) 202*237fead6SMichael Halcrow { 203*237fead6SMichael Halcrow if (mount_crypt_stat->global_auth_tok_key) 204*237fead6SMichael Halcrow key_put(mount_crypt_stat->global_auth_tok_key); 205*237fead6SMichael Halcrow if (mount_crypt_stat->global_key_tfm) 206*237fead6SMichael Halcrow crypto_free_tfm(mount_crypt_stat->global_key_tfm); 207*237fead6SMichael Halcrow memset(mount_crypt_stat, 0, sizeof(struct ecryptfs_mount_crypt_stat)); 208*237fead6SMichael Halcrow } 209*237fead6SMichael Halcrow 210*237fead6SMichael Halcrow /** 211*237fead6SMichael Halcrow * virt_to_scatterlist 212*237fead6SMichael Halcrow * @addr: Virtual address 213*237fead6SMichael Halcrow * @size: Size of data; should be an even multiple of the block size 214*237fead6SMichael Halcrow * @sg: Pointer to scatterlist array; set to NULL to obtain only 215*237fead6SMichael Halcrow * the number of scatterlist structs required in array 216*237fead6SMichael Halcrow * @sg_size: Max array size 217*237fead6SMichael Halcrow * 218*237fead6SMichael Halcrow * Fills in a scatterlist array with page references for a passed 219*237fead6SMichael Halcrow * virtual address. 220*237fead6SMichael Halcrow * 221*237fead6SMichael Halcrow * Returns the number of scatterlist structs in array used 222*237fead6SMichael Halcrow */ 223*237fead6SMichael Halcrow int virt_to_scatterlist(const void *addr, int size, struct scatterlist *sg, 224*237fead6SMichael Halcrow int sg_size) 225*237fead6SMichael Halcrow { 226*237fead6SMichael Halcrow int i = 0; 227*237fead6SMichael Halcrow struct page *pg; 228*237fead6SMichael Halcrow int offset; 229*237fead6SMichael Halcrow int remainder_of_page; 230*237fead6SMichael Halcrow 231*237fead6SMichael Halcrow while (size > 0 && i < sg_size) { 232*237fead6SMichael Halcrow pg = virt_to_page(addr); 233*237fead6SMichael Halcrow offset = offset_in_page(addr); 234*237fead6SMichael Halcrow if (sg) { 235*237fead6SMichael Halcrow sg[i].page = pg; 236*237fead6SMichael Halcrow sg[i].offset = offset; 237*237fead6SMichael Halcrow } 238*237fead6SMichael Halcrow remainder_of_page = PAGE_CACHE_SIZE - offset; 239*237fead6SMichael Halcrow if (size >= remainder_of_page) { 240*237fead6SMichael Halcrow if (sg) 241*237fead6SMichael Halcrow sg[i].length = remainder_of_page; 242*237fead6SMichael Halcrow addr += remainder_of_page; 243*237fead6SMichael Halcrow size -= remainder_of_page; 244*237fead6SMichael Halcrow } else { 245*237fead6SMichael Halcrow if (sg) 246*237fead6SMichael Halcrow sg[i].length = size; 247*237fead6SMichael Halcrow addr += size; 248*237fead6SMichael Halcrow size = 0; 249*237fead6SMichael Halcrow } 250*237fead6SMichael Halcrow i++; 251*237fead6SMichael Halcrow } 252*237fead6SMichael Halcrow if (size > 0) 253*237fead6SMichael Halcrow return -ENOMEM; 254*237fead6SMichael Halcrow return i; 255*237fead6SMichael Halcrow } 256*237fead6SMichael Halcrow 257*237fead6SMichael Halcrow /** 258*237fead6SMichael Halcrow * encrypt_scatterlist 259*237fead6SMichael Halcrow * @crypt_stat: Pointer to the crypt_stat struct to initialize. 260*237fead6SMichael Halcrow * @dest_sg: Destination of encrypted data 261*237fead6SMichael Halcrow * @src_sg: Data to be encrypted 262*237fead6SMichael Halcrow * @size: Length of data to be encrypted 263*237fead6SMichael Halcrow * @iv: iv to use during encryption 264*237fead6SMichael Halcrow * 265*237fead6SMichael Halcrow * Returns the number of bytes encrypted; negative value on error 266*237fead6SMichael Halcrow */ 267*237fead6SMichael Halcrow static int encrypt_scatterlist(struct ecryptfs_crypt_stat *crypt_stat, 268*237fead6SMichael Halcrow struct scatterlist *dest_sg, 269*237fead6SMichael Halcrow struct scatterlist *src_sg, int size, 270*237fead6SMichael Halcrow unsigned char *iv) 271*237fead6SMichael Halcrow { 272*237fead6SMichael Halcrow int rc = 0; 273*237fead6SMichael Halcrow 274*237fead6SMichael Halcrow BUG_ON(!crypt_stat || !crypt_stat->tfm 275*237fead6SMichael Halcrow || !ECRYPTFS_CHECK_FLAG(crypt_stat->flags, 276*237fead6SMichael Halcrow ECRYPTFS_STRUCT_INITIALIZED)); 277*237fead6SMichael Halcrow if (unlikely(ecryptfs_verbosity > 0)) { 278*237fead6SMichael Halcrow ecryptfs_printk(KERN_DEBUG, "Key size [%d]; key:\n", 279*237fead6SMichael Halcrow crypt_stat->key_size); 280*237fead6SMichael Halcrow ecryptfs_dump_hex(crypt_stat->key, 281*237fead6SMichael Halcrow crypt_stat->key_size); 282*237fead6SMichael Halcrow } 283*237fead6SMichael Halcrow /* Consider doing this once, when the file is opened */ 284*237fead6SMichael Halcrow mutex_lock(&crypt_stat->cs_tfm_mutex); 285*237fead6SMichael Halcrow rc = crypto_cipher_setkey(crypt_stat->tfm, crypt_stat->key, 286*237fead6SMichael Halcrow crypt_stat->key_size); 287*237fead6SMichael Halcrow if (rc) { 288*237fead6SMichael Halcrow ecryptfs_printk(KERN_ERR, "Error setting key; rc = [%d]\n", 289*237fead6SMichael Halcrow rc); 290*237fead6SMichael Halcrow mutex_unlock(&crypt_stat->cs_tfm_mutex); 291*237fead6SMichael Halcrow rc = -EINVAL; 292*237fead6SMichael Halcrow goto out; 293*237fead6SMichael Halcrow } 294*237fead6SMichael Halcrow ecryptfs_printk(KERN_DEBUG, "Encrypting [%d] bytes.\n", size); 295*237fead6SMichael Halcrow crypto_cipher_encrypt_iv(crypt_stat->tfm, dest_sg, src_sg, size, iv); 296*237fead6SMichael Halcrow mutex_unlock(&crypt_stat->cs_tfm_mutex); 297*237fead6SMichael Halcrow out: 298*237fead6SMichael Halcrow return rc; 299*237fead6SMichael Halcrow } 300*237fead6SMichael Halcrow 301*237fead6SMichael Halcrow static void 302*237fead6SMichael Halcrow ecryptfs_extent_to_lwr_pg_idx_and_offset(unsigned long *lower_page_idx, 303*237fead6SMichael Halcrow int *byte_offset, 304*237fead6SMichael Halcrow struct ecryptfs_crypt_stat *crypt_stat, 305*237fead6SMichael Halcrow unsigned long extent_num) 306*237fead6SMichael Halcrow { 307*237fead6SMichael Halcrow unsigned long lower_extent_num; 308*237fead6SMichael Halcrow int extents_occupied_by_headers_at_front; 309*237fead6SMichael Halcrow int bytes_occupied_by_headers_at_front; 310*237fead6SMichael Halcrow int extent_offset; 311*237fead6SMichael Halcrow int extents_per_page; 312*237fead6SMichael Halcrow 313*237fead6SMichael Halcrow bytes_occupied_by_headers_at_front = 314*237fead6SMichael Halcrow ( crypt_stat->header_extent_size 315*237fead6SMichael Halcrow * crypt_stat->num_header_extents_at_front ); 316*237fead6SMichael Halcrow extents_occupied_by_headers_at_front = 317*237fead6SMichael Halcrow ( bytes_occupied_by_headers_at_front 318*237fead6SMichael Halcrow / crypt_stat->extent_size ); 319*237fead6SMichael Halcrow lower_extent_num = extents_occupied_by_headers_at_front + extent_num; 320*237fead6SMichael Halcrow extents_per_page = PAGE_CACHE_SIZE / crypt_stat->extent_size; 321*237fead6SMichael Halcrow (*lower_page_idx) = lower_extent_num / extents_per_page; 322*237fead6SMichael Halcrow extent_offset = lower_extent_num % extents_per_page; 323*237fead6SMichael Halcrow (*byte_offset) = extent_offset * crypt_stat->extent_size; 324*237fead6SMichael Halcrow ecryptfs_printk(KERN_DEBUG, " * crypt_stat->header_extent_size = " 325*237fead6SMichael Halcrow "[%d]\n", crypt_stat->header_extent_size); 326*237fead6SMichael Halcrow ecryptfs_printk(KERN_DEBUG, " * crypt_stat->" 327*237fead6SMichael Halcrow "num_header_extents_at_front = [%d]\n", 328*237fead6SMichael Halcrow crypt_stat->num_header_extents_at_front); 329*237fead6SMichael Halcrow ecryptfs_printk(KERN_DEBUG, " * extents_occupied_by_headers_at_" 330*237fead6SMichael Halcrow "front = [%d]\n", extents_occupied_by_headers_at_front); 331*237fead6SMichael Halcrow ecryptfs_printk(KERN_DEBUG, " * lower_extent_num = [0x%.16x]\n", 332*237fead6SMichael Halcrow lower_extent_num); 333*237fead6SMichael Halcrow ecryptfs_printk(KERN_DEBUG, " * extents_per_page = [%d]\n", 334*237fead6SMichael Halcrow extents_per_page); 335*237fead6SMichael Halcrow ecryptfs_printk(KERN_DEBUG, " * (*lower_page_idx) = [0x%.16x]\n", 336*237fead6SMichael Halcrow (*lower_page_idx)); 337*237fead6SMichael Halcrow ecryptfs_printk(KERN_DEBUG, " * extent_offset = [%d]\n", 338*237fead6SMichael Halcrow extent_offset); 339*237fead6SMichael Halcrow ecryptfs_printk(KERN_DEBUG, " * (*byte_offset) = [%d]\n", 340*237fead6SMichael Halcrow (*byte_offset)); 341*237fead6SMichael Halcrow } 342*237fead6SMichael Halcrow 343*237fead6SMichael Halcrow static int ecryptfs_write_out_page(struct ecryptfs_page_crypt_context *ctx, 344*237fead6SMichael Halcrow struct page *lower_page, 345*237fead6SMichael Halcrow struct inode *lower_inode, 346*237fead6SMichael Halcrow int byte_offset_in_page, int bytes_to_write) 347*237fead6SMichael Halcrow { 348*237fead6SMichael Halcrow int rc = 0; 349*237fead6SMichael Halcrow 350*237fead6SMichael Halcrow if (ctx->mode == ECRYPTFS_PREPARE_COMMIT_MODE) { 351*237fead6SMichael Halcrow rc = ecryptfs_commit_lower_page(lower_page, lower_inode, 352*237fead6SMichael Halcrow ctx->param.lower_file, 353*237fead6SMichael Halcrow byte_offset_in_page, 354*237fead6SMichael Halcrow bytes_to_write); 355*237fead6SMichael Halcrow if (rc) { 356*237fead6SMichael Halcrow ecryptfs_printk(KERN_ERR, "Error calling lower " 357*237fead6SMichael Halcrow "commit; rc = [%d]\n", rc); 358*237fead6SMichael Halcrow goto out; 359*237fead6SMichael Halcrow } 360*237fead6SMichael Halcrow } else { 361*237fead6SMichael Halcrow rc = ecryptfs_writepage_and_release_lower_page(lower_page, 362*237fead6SMichael Halcrow lower_inode, 363*237fead6SMichael Halcrow ctx->param.wbc); 364*237fead6SMichael Halcrow if (rc) { 365*237fead6SMichael Halcrow ecryptfs_printk(KERN_ERR, "Error calling lower " 366*237fead6SMichael Halcrow "writepage(); rc = [%d]\n", rc); 367*237fead6SMichael Halcrow goto out; 368*237fead6SMichael Halcrow } 369*237fead6SMichael Halcrow } 370*237fead6SMichael Halcrow out: 371*237fead6SMichael Halcrow return rc; 372*237fead6SMichael Halcrow } 373*237fead6SMichael Halcrow 374*237fead6SMichael Halcrow static int ecryptfs_read_in_page(struct ecryptfs_page_crypt_context *ctx, 375*237fead6SMichael Halcrow struct page **lower_page, 376*237fead6SMichael Halcrow struct inode *lower_inode, 377*237fead6SMichael Halcrow unsigned long lower_page_idx, 378*237fead6SMichael Halcrow int byte_offset_in_page) 379*237fead6SMichael Halcrow { 380*237fead6SMichael Halcrow int rc = 0; 381*237fead6SMichael Halcrow 382*237fead6SMichael Halcrow if (ctx->mode == ECRYPTFS_PREPARE_COMMIT_MODE) { 383*237fead6SMichael Halcrow /* TODO: Limit this to only the data extents that are 384*237fead6SMichael Halcrow * needed */ 385*237fead6SMichael Halcrow rc = ecryptfs_get_lower_page(lower_page, lower_inode, 386*237fead6SMichael Halcrow ctx->param.lower_file, 387*237fead6SMichael Halcrow lower_page_idx, 388*237fead6SMichael Halcrow byte_offset_in_page, 389*237fead6SMichael Halcrow (PAGE_CACHE_SIZE 390*237fead6SMichael Halcrow - byte_offset_in_page)); 391*237fead6SMichael Halcrow if (rc) { 392*237fead6SMichael Halcrow ecryptfs_printk( 393*237fead6SMichael Halcrow KERN_ERR, "Error attempting to grab, map, " 394*237fead6SMichael Halcrow "and prepare_write lower page with index " 395*237fead6SMichael Halcrow "[0x%.16x]; rc = [%d]\n", lower_page_idx, rc); 396*237fead6SMichael Halcrow goto out; 397*237fead6SMichael Halcrow } 398*237fead6SMichael Halcrow } else { 399*237fead6SMichael Halcrow rc = ecryptfs_grab_and_map_lower_page(lower_page, NULL, 400*237fead6SMichael Halcrow lower_inode, 401*237fead6SMichael Halcrow lower_page_idx); 402*237fead6SMichael Halcrow if (rc) { 403*237fead6SMichael Halcrow ecryptfs_printk( 404*237fead6SMichael Halcrow KERN_ERR, "Error attempting to grab and map " 405*237fead6SMichael Halcrow "lower page with index [0x%.16x]; rc = [%d]\n", 406*237fead6SMichael Halcrow lower_page_idx, rc); 407*237fead6SMichael Halcrow goto out; 408*237fead6SMichael Halcrow } 409*237fead6SMichael Halcrow } 410*237fead6SMichael Halcrow out: 411*237fead6SMichael Halcrow return rc; 412*237fead6SMichael Halcrow } 413*237fead6SMichael Halcrow 414*237fead6SMichael Halcrow /** 415*237fead6SMichael Halcrow * ecryptfs_encrypt_page 416*237fead6SMichael Halcrow * @ctx: The context of the page 417*237fead6SMichael Halcrow * 418*237fead6SMichael Halcrow * Encrypt an eCryptfs page. This is done on a per-extent basis. Note 419*237fead6SMichael Halcrow * that eCryptfs pages may straddle the lower pages -- for instance, 420*237fead6SMichael Halcrow * if the file was created on a machine with an 8K page size 421*237fead6SMichael Halcrow * (resulting in an 8K header), and then the file is copied onto a 422*237fead6SMichael Halcrow * host with a 32K page size, then when reading page 0 of the eCryptfs 423*237fead6SMichael Halcrow * file, 24K of page 0 of the lower file will be read and decrypted, 424*237fead6SMichael Halcrow * and then 8K of page 1 of the lower file will be read and decrypted. 425*237fead6SMichael Halcrow * 426*237fead6SMichael Halcrow * The actual operations performed on each page depends on the 427*237fead6SMichael Halcrow * contents of the ecryptfs_page_crypt_context struct. 428*237fead6SMichael Halcrow * 429*237fead6SMichael Halcrow * Returns zero on success; negative on error 430*237fead6SMichael Halcrow */ 431*237fead6SMichael Halcrow int ecryptfs_encrypt_page(struct ecryptfs_page_crypt_context *ctx) 432*237fead6SMichael Halcrow { 433*237fead6SMichael Halcrow char extent_iv[ECRYPTFS_MAX_IV_BYTES]; 434*237fead6SMichael Halcrow unsigned long base_extent; 435*237fead6SMichael Halcrow unsigned long extent_offset = 0; 436*237fead6SMichael Halcrow unsigned long lower_page_idx = 0; 437*237fead6SMichael Halcrow unsigned long prior_lower_page_idx = 0; 438*237fead6SMichael Halcrow struct page *lower_page; 439*237fead6SMichael Halcrow struct inode *lower_inode; 440*237fead6SMichael Halcrow struct ecryptfs_inode_info *inode_info; 441*237fead6SMichael Halcrow struct ecryptfs_crypt_stat *crypt_stat; 442*237fead6SMichael Halcrow int rc = 0; 443*237fead6SMichael Halcrow int lower_byte_offset = 0; 444*237fead6SMichael Halcrow int orig_byte_offset = 0; 445*237fead6SMichael Halcrow int num_extents_per_page; 446*237fead6SMichael Halcrow #define ECRYPTFS_PAGE_STATE_UNREAD 0 447*237fead6SMichael Halcrow #define ECRYPTFS_PAGE_STATE_READ 1 448*237fead6SMichael Halcrow #define ECRYPTFS_PAGE_STATE_MODIFIED 2 449*237fead6SMichael Halcrow #define ECRYPTFS_PAGE_STATE_WRITTEN 3 450*237fead6SMichael Halcrow int page_state; 451*237fead6SMichael Halcrow 452*237fead6SMichael Halcrow lower_inode = ecryptfs_inode_to_lower(ctx->page->mapping->host); 453*237fead6SMichael Halcrow inode_info = ecryptfs_inode_to_private(ctx->page->mapping->host); 454*237fead6SMichael Halcrow crypt_stat = &inode_info->crypt_stat; 455*237fead6SMichael Halcrow if (!ECRYPTFS_CHECK_FLAG(crypt_stat->flags, ECRYPTFS_ENCRYPTED)) { 456*237fead6SMichael Halcrow rc = ecryptfs_copy_page_to_lower(ctx->page, lower_inode, 457*237fead6SMichael Halcrow ctx->param.lower_file); 458*237fead6SMichael Halcrow if (rc) 459*237fead6SMichael Halcrow ecryptfs_printk(KERN_ERR, "Error attempting to copy " 460*237fead6SMichael Halcrow "page at index [0x%.16x]\n", 461*237fead6SMichael Halcrow ctx->page->index); 462*237fead6SMichael Halcrow goto out; 463*237fead6SMichael Halcrow } 464*237fead6SMichael Halcrow num_extents_per_page = PAGE_CACHE_SIZE / crypt_stat->extent_size; 465*237fead6SMichael Halcrow base_extent = (ctx->page->index * num_extents_per_page); 466*237fead6SMichael Halcrow page_state = ECRYPTFS_PAGE_STATE_UNREAD; 467*237fead6SMichael Halcrow while (extent_offset < num_extents_per_page) { 468*237fead6SMichael Halcrow ecryptfs_extent_to_lwr_pg_idx_and_offset( 469*237fead6SMichael Halcrow &lower_page_idx, &lower_byte_offset, crypt_stat, 470*237fead6SMichael Halcrow (base_extent + extent_offset)); 471*237fead6SMichael Halcrow if (prior_lower_page_idx != lower_page_idx 472*237fead6SMichael Halcrow && page_state == ECRYPTFS_PAGE_STATE_MODIFIED) { 473*237fead6SMichael Halcrow rc = ecryptfs_write_out_page(ctx, lower_page, 474*237fead6SMichael Halcrow lower_inode, 475*237fead6SMichael Halcrow orig_byte_offset, 476*237fead6SMichael Halcrow (PAGE_CACHE_SIZE 477*237fead6SMichael Halcrow - orig_byte_offset)); 478*237fead6SMichael Halcrow if (rc) { 479*237fead6SMichael Halcrow ecryptfs_printk(KERN_ERR, "Error attempting " 480*237fead6SMichael Halcrow "to write out page; rc = [%d]" 481*237fead6SMichael Halcrow "\n", rc); 482*237fead6SMichael Halcrow goto out; 483*237fead6SMichael Halcrow } 484*237fead6SMichael Halcrow page_state = ECRYPTFS_PAGE_STATE_WRITTEN; 485*237fead6SMichael Halcrow } 486*237fead6SMichael Halcrow if (page_state == ECRYPTFS_PAGE_STATE_UNREAD 487*237fead6SMichael Halcrow || page_state == ECRYPTFS_PAGE_STATE_WRITTEN) { 488*237fead6SMichael Halcrow rc = ecryptfs_read_in_page(ctx, &lower_page, 489*237fead6SMichael Halcrow lower_inode, lower_page_idx, 490*237fead6SMichael Halcrow lower_byte_offset); 491*237fead6SMichael Halcrow if (rc) { 492*237fead6SMichael Halcrow ecryptfs_printk(KERN_ERR, "Error attempting " 493*237fead6SMichael Halcrow "to read in lower page with " 494*237fead6SMichael Halcrow "index [0x%.16x]; rc = [%d]\n", 495*237fead6SMichael Halcrow lower_page_idx, rc); 496*237fead6SMichael Halcrow goto out; 497*237fead6SMichael Halcrow } 498*237fead6SMichael Halcrow orig_byte_offset = lower_byte_offset; 499*237fead6SMichael Halcrow prior_lower_page_idx = lower_page_idx; 500*237fead6SMichael Halcrow page_state = ECRYPTFS_PAGE_STATE_READ; 501*237fead6SMichael Halcrow } 502*237fead6SMichael Halcrow BUG_ON(!(page_state == ECRYPTFS_PAGE_STATE_MODIFIED 503*237fead6SMichael Halcrow || page_state == ECRYPTFS_PAGE_STATE_READ)); 504*237fead6SMichael Halcrow rc = ecryptfs_derive_iv(extent_iv, crypt_stat, 505*237fead6SMichael Halcrow (base_extent + extent_offset)); 506*237fead6SMichael Halcrow if (rc) { 507*237fead6SMichael Halcrow ecryptfs_printk(KERN_ERR, "Error attempting to " 508*237fead6SMichael Halcrow "derive IV for extent [0x%.16x]; " 509*237fead6SMichael Halcrow "rc = [%d]\n", 510*237fead6SMichael Halcrow (base_extent + extent_offset), rc); 511*237fead6SMichael Halcrow goto out; 512*237fead6SMichael Halcrow } 513*237fead6SMichael Halcrow if (unlikely(ecryptfs_verbosity > 0)) { 514*237fead6SMichael Halcrow ecryptfs_printk(KERN_DEBUG, "Encrypting extent " 515*237fead6SMichael Halcrow "with iv:\n"); 516*237fead6SMichael Halcrow ecryptfs_dump_hex(extent_iv, crypt_stat->iv_bytes); 517*237fead6SMichael Halcrow ecryptfs_printk(KERN_DEBUG, "First 8 bytes before " 518*237fead6SMichael Halcrow "encryption:\n"); 519*237fead6SMichael Halcrow ecryptfs_dump_hex((char *) 520*237fead6SMichael Halcrow (page_address(ctx->page) 521*237fead6SMichael Halcrow + (extent_offset 522*237fead6SMichael Halcrow * crypt_stat->extent_size)), 8); 523*237fead6SMichael Halcrow } 524*237fead6SMichael Halcrow rc = ecryptfs_encrypt_page_offset( 525*237fead6SMichael Halcrow crypt_stat, lower_page, lower_byte_offset, ctx->page, 526*237fead6SMichael Halcrow (extent_offset * crypt_stat->extent_size), 527*237fead6SMichael Halcrow crypt_stat->extent_size, extent_iv); 528*237fead6SMichael Halcrow ecryptfs_printk(KERN_DEBUG, "Encrypt extent [0x%.16x]; " 529*237fead6SMichael Halcrow "rc = [%d]\n", 530*237fead6SMichael Halcrow (base_extent + extent_offset), rc); 531*237fead6SMichael Halcrow if (unlikely(ecryptfs_verbosity > 0)) { 532*237fead6SMichael Halcrow ecryptfs_printk(KERN_DEBUG, "First 8 bytes after " 533*237fead6SMichael Halcrow "encryption:\n"); 534*237fead6SMichael Halcrow ecryptfs_dump_hex((char *)(page_address(lower_page) 535*237fead6SMichael Halcrow + lower_byte_offset), 8); 536*237fead6SMichael Halcrow } 537*237fead6SMichael Halcrow page_state = ECRYPTFS_PAGE_STATE_MODIFIED; 538*237fead6SMichael Halcrow extent_offset++; 539*237fead6SMichael Halcrow } 540*237fead6SMichael Halcrow BUG_ON(orig_byte_offset != 0); 541*237fead6SMichael Halcrow rc = ecryptfs_write_out_page(ctx, lower_page, lower_inode, 0, 542*237fead6SMichael Halcrow (lower_byte_offset 543*237fead6SMichael Halcrow + crypt_stat->extent_size)); 544*237fead6SMichael Halcrow if (rc) { 545*237fead6SMichael Halcrow ecryptfs_printk(KERN_ERR, "Error attempting to write out " 546*237fead6SMichael Halcrow "page; rc = [%d]\n", rc); 547*237fead6SMichael Halcrow goto out; 548*237fead6SMichael Halcrow } 549*237fead6SMichael Halcrow out: 550*237fead6SMichael Halcrow return rc; 551*237fead6SMichael Halcrow } 552*237fead6SMichael Halcrow 553*237fead6SMichael Halcrow /** 554*237fead6SMichael Halcrow * ecryptfs_decrypt_page 555*237fead6SMichael Halcrow * @file: The ecryptfs file 556*237fead6SMichael Halcrow * @page: The page in ecryptfs to decrypt 557*237fead6SMichael Halcrow * 558*237fead6SMichael Halcrow * Decrypt an eCryptfs page. This is done on a per-extent basis. Note 559*237fead6SMichael Halcrow * that eCryptfs pages may straddle the lower pages -- for instance, 560*237fead6SMichael Halcrow * if the file was created on a machine with an 8K page size 561*237fead6SMichael Halcrow * (resulting in an 8K header), and then the file is copied onto a 562*237fead6SMichael Halcrow * host with a 32K page size, then when reading page 0 of the eCryptfs 563*237fead6SMichael Halcrow * file, 24K of page 0 of the lower file will be read and decrypted, 564*237fead6SMichael Halcrow * and then 8K of page 1 of the lower file will be read and decrypted. 565*237fead6SMichael Halcrow * 566*237fead6SMichael Halcrow * Returns zero on success; negative on error 567*237fead6SMichael Halcrow */ 568*237fead6SMichael Halcrow int ecryptfs_decrypt_page(struct file *file, struct page *page) 569*237fead6SMichael Halcrow { 570*237fead6SMichael Halcrow char extent_iv[ECRYPTFS_MAX_IV_BYTES]; 571*237fead6SMichael Halcrow unsigned long base_extent; 572*237fead6SMichael Halcrow unsigned long extent_offset = 0; 573*237fead6SMichael Halcrow unsigned long lower_page_idx = 0; 574*237fead6SMichael Halcrow unsigned long prior_lower_page_idx = 0; 575*237fead6SMichael Halcrow struct page *lower_page; 576*237fead6SMichael Halcrow char *lower_page_virt = NULL; 577*237fead6SMichael Halcrow struct inode *lower_inode; 578*237fead6SMichael Halcrow struct ecryptfs_crypt_stat *crypt_stat; 579*237fead6SMichael Halcrow int rc = 0; 580*237fead6SMichael Halcrow int byte_offset; 581*237fead6SMichael Halcrow int num_extents_per_page; 582*237fead6SMichael Halcrow int page_state; 583*237fead6SMichael Halcrow 584*237fead6SMichael Halcrow crypt_stat = &(ecryptfs_inode_to_private( 585*237fead6SMichael Halcrow page->mapping->host)->crypt_stat); 586*237fead6SMichael Halcrow lower_inode = ecryptfs_inode_to_lower(page->mapping->host); 587*237fead6SMichael Halcrow if (!ECRYPTFS_CHECK_FLAG(crypt_stat->flags, ECRYPTFS_ENCRYPTED)) { 588*237fead6SMichael Halcrow rc = ecryptfs_do_readpage(file, page, page->index); 589*237fead6SMichael Halcrow if (rc) 590*237fead6SMichael Halcrow ecryptfs_printk(KERN_ERR, "Error attempting to copy " 591*237fead6SMichael Halcrow "page at index [0x%.16x]\n", 592*237fead6SMichael Halcrow page->index); 593*237fead6SMichael Halcrow goto out; 594*237fead6SMichael Halcrow } 595*237fead6SMichael Halcrow num_extents_per_page = PAGE_CACHE_SIZE / crypt_stat->extent_size; 596*237fead6SMichael Halcrow base_extent = (page->index * num_extents_per_page); 597*237fead6SMichael Halcrow lower_page_virt = kmem_cache_alloc(ecryptfs_lower_page_cache, 598*237fead6SMichael Halcrow SLAB_KERNEL); 599*237fead6SMichael Halcrow if (!lower_page_virt) { 600*237fead6SMichael Halcrow rc = -ENOMEM; 601*237fead6SMichael Halcrow ecryptfs_printk(KERN_ERR, "Error getting page for encrypted " 602*237fead6SMichael Halcrow "lower page(s)\n"); 603*237fead6SMichael Halcrow goto out; 604*237fead6SMichael Halcrow } 605*237fead6SMichael Halcrow lower_page = virt_to_page(lower_page_virt); 606*237fead6SMichael Halcrow page_state = ECRYPTFS_PAGE_STATE_UNREAD; 607*237fead6SMichael Halcrow while (extent_offset < num_extents_per_page) { 608*237fead6SMichael Halcrow ecryptfs_extent_to_lwr_pg_idx_and_offset( 609*237fead6SMichael Halcrow &lower_page_idx, &byte_offset, crypt_stat, 610*237fead6SMichael Halcrow (base_extent + extent_offset)); 611*237fead6SMichael Halcrow if (prior_lower_page_idx != lower_page_idx 612*237fead6SMichael Halcrow || page_state == ECRYPTFS_PAGE_STATE_UNREAD) { 613*237fead6SMichael Halcrow rc = ecryptfs_do_readpage(file, lower_page, 614*237fead6SMichael Halcrow lower_page_idx); 615*237fead6SMichael Halcrow if (rc) { 616*237fead6SMichael Halcrow ecryptfs_printk(KERN_ERR, "Error reading " 617*237fead6SMichael Halcrow "lower encrypted page; rc = " 618*237fead6SMichael Halcrow "[%d]\n", rc); 619*237fead6SMichael Halcrow goto out; 620*237fead6SMichael Halcrow } 621*237fead6SMichael Halcrow prior_lower_page_idx = lower_page_idx; 622*237fead6SMichael Halcrow page_state = ECRYPTFS_PAGE_STATE_READ; 623*237fead6SMichael Halcrow } 624*237fead6SMichael Halcrow rc = ecryptfs_derive_iv(extent_iv, crypt_stat, 625*237fead6SMichael Halcrow (base_extent + extent_offset)); 626*237fead6SMichael Halcrow if (rc) { 627*237fead6SMichael Halcrow ecryptfs_printk(KERN_ERR, "Error attempting to " 628*237fead6SMichael Halcrow "derive IV for extent [0x%.16x]; rc = " 629*237fead6SMichael Halcrow "[%d]\n", 630*237fead6SMichael Halcrow (base_extent + extent_offset), rc); 631*237fead6SMichael Halcrow goto out; 632*237fead6SMichael Halcrow } 633*237fead6SMichael Halcrow if (unlikely(ecryptfs_verbosity > 0)) { 634*237fead6SMichael Halcrow ecryptfs_printk(KERN_DEBUG, "Decrypting extent " 635*237fead6SMichael Halcrow "with iv:\n"); 636*237fead6SMichael Halcrow ecryptfs_dump_hex(extent_iv, crypt_stat->iv_bytes); 637*237fead6SMichael Halcrow ecryptfs_printk(KERN_DEBUG, "First 8 bytes before " 638*237fead6SMichael Halcrow "decryption:\n"); 639*237fead6SMichael Halcrow ecryptfs_dump_hex((lower_page_virt + byte_offset), 8); 640*237fead6SMichael Halcrow } 641*237fead6SMichael Halcrow rc = ecryptfs_decrypt_page_offset(crypt_stat, page, 642*237fead6SMichael Halcrow (extent_offset 643*237fead6SMichael Halcrow * crypt_stat->extent_size), 644*237fead6SMichael Halcrow lower_page, byte_offset, 645*237fead6SMichael Halcrow crypt_stat->extent_size, 646*237fead6SMichael Halcrow extent_iv); 647*237fead6SMichael Halcrow if (rc != crypt_stat->extent_size) { 648*237fead6SMichael Halcrow ecryptfs_printk(KERN_ERR, "Error attempting to " 649*237fead6SMichael Halcrow "decrypt extent [0x%.16x]\n", 650*237fead6SMichael Halcrow (base_extent + extent_offset)); 651*237fead6SMichael Halcrow goto out; 652*237fead6SMichael Halcrow } 653*237fead6SMichael Halcrow rc = 0; 654*237fead6SMichael Halcrow if (unlikely(ecryptfs_verbosity > 0)) { 655*237fead6SMichael Halcrow ecryptfs_printk(KERN_DEBUG, "First 8 bytes after " 656*237fead6SMichael Halcrow "decryption:\n"); 657*237fead6SMichael Halcrow ecryptfs_dump_hex((char *)(page_address(page) 658*237fead6SMichael Halcrow + byte_offset), 8); 659*237fead6SMichael Halcrow } 660*237fead6SMichael Halcrow extent_offset++; 661*237fead6SMichael Halcrow } 662*237fead6SMichael Halcrow out: 663*237fead6SMichael Halcrow if (lower_page_virt) 664*237fead6SMichael Halcrow kmem_cache_free(ecryptfs_lower_page_cache, lower_page_virt); 665*237fead6SMichael Halcrow return rc; 666*237fead6SMichael Halcrow } 667*237fead6SMichael Halcrow 668*237fead6SMichael Halcrow /** 669*237fead6SMichael Halcrow * decrypt_scatterlist 670*237fead6SMichael Halcrow * 671*237fead6SMichael Halcrow * Returns the number of bytes decrypted; negative value on error 672*237fead6SMichael Halcrow */ 673*237fead6SMichael Halcrow static int decrypt_scatterlist(struct ecryptfs_crypt_stat *crypt_stat, 674*237fead6SMichael Halcrow struct scatterlist *dest_sg, 675*237fead6SMichael Halcrow struct scatterlist *src_sg, int size, 676*237fead6SMichael Halcrow unsigned char *iv) 677*237fead6SMichael Halcrow { 678*237fead6SMichael Halcrow int rc = 0; 679*237fead6SMichael Halcrow 680*237fead6SMichael Halcrow /* Consider doing this once, when the file is opened */ 681*237fead6SMichael Halcrow mutex_lock(&crypt_stat->cs_tfm_mutex); 682*237fead6SMichael Halcrow rc = crypto_cipher_setkey(crypt_stat->tfm, crypt_stat->key, 683*237fead6SMichael Halcrow crypt_stat->key_size); 684*237fead6SMichael Halcrow if (rc) { 685*237fead6SMichael Halcrow ecryptfs_printk(KERN_ERR, "Error setting key; rc = [%d]\n", 686*237fead6SMichael Halcrow rc); 687*237fead6SMichael Halcrow mutex_unlock(&crypt_stat->cs_tfm_mutex); 688*237fead6SMichael Halcrow rc = -EINVAL; 689*237fead6SMichael Halcrow goto out; 690*237fead6SMichael Halcrow } 691*237fead6SMichael Halcrow ecryptfs_printk(KERN_DEBUG, "Decrypting [%d] bytes.\n", size); 692*237fead6SMichael Halcrow rc = crypto_cipher_decrypt_iv(crypt_stat->tfm, dest_sg, src_sg, size, 693*237fead6SMichael Halcrow iv); 694*237fead6SMichael Halcrow mutex_unlock(&crypt_stat->cs_tfm_mutex); 695*237fead6SMichael Halcrow if (rc) { 696*237fead6SMichael Halcrow ecryptfs_printk(KERN_ERR, "Error decrypting; rc = [%d]\n", 697*237fead6SMichael Halcrow rc); 698*237fead6SMichael Halcrow goto out; 699*237fead6SMichael Halcrow } 700*237fead6SMichael Halcrow rc = size; 701*237fead6SMichael Halcrow out: 702*237fead6SMichael Halcrow return rc; 703*237fead6SMichael Halcrow } 704*237fead6SMichael Halcrow 705*237fead6SMichael Halcrow /** 706*237fead6SMichael Halcrow * ecryptfs_encrypt_page_offset 707*237fead6SMichael Halcrow * 708*237fead6SMichael Halcrow * Returns the number of bytes encrypted 709*237fead6SMichael Halcrow */ 710*237fead6SMichael Halcrow static int 711*237fead6SMichael Halcrow ecryptfs_encrypt_page_offset(struct ecryptfs_crypt_stat *crypt_stat, 712*237fead6SMichael Halcrow struct page *dst_page, int dst_offset, 713*237fead6SMichael Halcrow struct page *src_page, int src_offset, int size, 714*237fead6SMichael Halcrow unsigned char *iv) 715*237fead6SMichael Halcrow { 716*237fead6SMichael Halcrow struct scatterlist src_sg, dst_sg; 717*237fead6SMichael Halcrow 718*237fead6SMichael Halcrow src_sg.page = src_page; 719*237fead6SMichael Halcrow src_sg.offset = src_offset; 720*237fead6SMichael Halcrow src_sg.length = size; 721*237fead6SMichael Halcrow dst_sg.page = dst_page; 722*237fead6SMichael Halcrow dst_sg.offset = dst_offset; 723*237fead6SMichael Halcrow dst_sg.length = size; 724*237fead6SMichael Halcrow return encrypt_scatterlist(crypt_stat, &dst_sg, &src_sg, size, iv); 725*237fead6SMichael Halcrow } 726*237fead6SMichael Halcrow 727*237fead6SMichael Halcrow /** 728*237fead6SMichael Halcrow * ecryptfs_decrypt_page_offset 729*237fead6SMichael Halcrow * 730*237fead6SMichael Halcrow * Returns the number of bytes decrypted 731*237fead6SMichael Halcrow */ 732*237fead6SMichael Halcrow static int 733*237fead6SMichael Halcrow ecryptfs_decrypt_page_offset(struct ecryptfs_crypt_stat *crypt_stat, 734*237fead6SMichael Halcrow struct page *dst_page, int dst_offset, 735*237fead6SMichael Halcrow struct page *src_page, int src_offset, int size, 736*237fead6SMichael Halcrow unsigned char *iv) 737*237fead6SMichael Halcrow { 738*237fead6SMichael Halcrow struct scatterlist src_sg, dst_sg; 739*237fead6SMichael Halcrow 740*237fead6SMichael Halcrow src_sg.page = src_page; 741*237fead6SMichael Halcrow src_sg.offset = src_offset; 742*237fead6SMichael Halcrow src_sg.length = size; 743*237fead6SMichael Halcrow dst_sg.page = dst_page; 744*237fead6SMichael Halcrow dst_sg.offset = dst_offset; 745*237fead6SMichael Halcrow dst_sg.length = size; 746*237fead6SMichael Halcrow return decrypt_scatterlist(crypt_stat, &dst_sg, &src_sg, size, iv); 747*237fead6SMichael Halcrow } 748*237fead6SMichael Halcrow 749*237fead6SMichael Halcrow #define ECRYPTFS_MAX_SCATTERLIST_LEN 4 750*237fead6SMichael Halcrow 751*237fead6SMichael Halcrow /** 752*237fead6SMichael Halcrow * ecryptfs_init_crypt_ctx 753*237fead6SMichael Halcrow * @crypt_stat: Uninitilized crypt stats structure 754*237fead6SMichael Halcrow * 755*237fead6SMichael Halcrow * Initialize the crypto context. 756*237fead6SMichael Halcrow * 757*237fead6SMichael Halcrow * TODO: Performance: Keep a cache of initialized cipher contexts; 758*237fead6SMichael Halcrow * only init if needed 759*237fead6SMichael Halcrow */ 760*237fead6SMichael Halcrow int ecryptfs_init_crypt_ctx(struct ecryptfs_crypt_stat *crypt_stat) 761*237fead6SMichael Halcrow { 762*237fead6SMichael Halcrow int rc = -EINVAL; 763*237fead6SMichael Halcrow 764*237fead6SMichael Halcrow if (!crypt_stat->cipher) { 765*237fead6SMichael Halcrow ecryptfs_printk(KERN_ERR, "No cipher specified\n"); 766*237fead6SMichael Halcrow goto out; 767*237fead6SMichael Halcrow } 768*237fead6SMichael Halcrow ecryptfs_printk(KERN_DEBUG, 769*237fead6SMichael Halcrow "Initializing cipher [%s]; strlen = [%d]; " 770*237fead6SMichael Halcrow "key_size_bits = [%d]\n", 771*237fead6SMichael Halcrow crypt_stat->cipher, (int)strlen(crypt_stat->cipher), 772*237fead6SMichael Halcrow crypt_stat->key_size << 3); 773*237fead6SMichael Halcrow if (crypt_stat->tfm) { 774*237fead6SMichael Halcrow rc = 0; 775*237fead6SMichael Halcrow goto out; 776*237fead6SMichael Halcrow } 777*237fead6SMichael Halcrow mutex_lock(&crypt_stat->cs_tfm_mutex); 778*237fead6SMichael Halcrow crypt_stat->tfm = crypto_alloc_tfm(crypt_stat->cipher, 779*237fead6SMichael Halcrow ECRYPTFS_DEFAULT_CHAINING_MODE 780*237fead6SMichael Halcrow | CRYPTO_TFM_REQ_WEAK_KEY); 781*237fead6SMichael Halcrow mutex_unlock(&crypt_stat->cs_tfm_mutex); 782*237fead6SMichael Halcrow if (!crypt_stat->tfm) { 783*237fead6SMichael Halcrow ecryptfs_printk(KERN_ERR, "cryptfs: init_crypt_ctx(): " 784*237fead6SMichael Halcrow "Error initializing cipher [%s]\n", 785*237fead6SMichael Halcrow crypt_stat->cipher); 786*237fead6SMichael Halcrow goto out; 787*237fead6SMichael Halcrow } 788*237fead6SMichael Halcrow rc = 0; 789*237fead6SMichael Halcrow out: 790*237fead6SMichael Halcrow return rc; 791*237fead6SMichael Halcrow } 792*237fead6SMichael Halcrow 793*237fead6SMichael Halcrow static void set_extent_mask_and_shift(struct ecryptfs_crypt_stat *crypt_stat) 794*237fead6SMichael Halcrow { 795*237fead6SMichael Halcrow int extent_size_tmp; 796*237fead6SMichael Halcrow 797*237fead6SMichael Halcrow crypt_stat->extent_mask = 0xFFFFFFFF; 798*237fead6SMichael Halcrow crypt_stat->extent_shift = 0; 799*237fead6SMichael Halcrow if (crypt_stat->extent_size == 0) 800*237fead6SMichael Halcrow return; 801*237fead6SMichael Halcrow extent_size_tmp = crypt_stat->extent_size; 802*237fead6SMichael Halcrow while ((extent_size_tmp & 0x01) == 0) { 803*237fead6SMichael Halcrow extent_size_tmp >>= 1; 804*237fead6SMichael Halcrow crypt_stat->extent_mask <<= 1; 805*237fead6SMichael Halcrow crypt_stat->extent_shift++; 806*237fead6SMichael Halcrow } 807*237fead6SMichael Halcrow } 808*237fead6SMichael Halcrow 809*237fead6SMichael Halcrow void ecryptfs_set_default_sizes(struct ecryptfs_crypt_stat *crypt_stat) 810*237fead6SMichael Halcrow { 811*237fead6SMichael Halcrow /* Default values; may be overwritten as we are parsing the 812*237fead6SMichael Halcrow * packets. */ 813*237fead6SMichael Halcrow crypt_stat->extent_size = ECRYPTFS_DEFAULT_EXTENT_SIZE; 814*237fead6SMichael Halcrow set_extent_mask_and_shift(crypt_stat); 815*237fead6SMichael Halcrow crypt_stat->iv_bytes = ECRYPTFS_DEFAULT_IV_BYTES; 816*237fead6SMichael Halcrow if (PAGE_CACHE_SIZE <= ECRYPTFS_MINIMUM_HEADER_EXTENT_SIZE) { 817*237fead6SMichael Halcrow crypt_stat->header_extent_size = 818*237fead6SMichael Halcrow ECRYPTFS_MINIMUM_HEADER_EXTENT_SIZE; 819*237fead6SMichael Halcrow } else 820*237fead6SMichael Halcrow crypt_stat->header_extent_size = PAGE_CACHE_SIZE; 821*237fead6SMichael Halcrow crypt_stat->num_header_extents_at_front = 1; 822*237fead6SMichael Halcrow } 823*237fead6SMichael Halcrow 824*237fead6SMichael Halcrow /** 825*237fead6SMichael Halcrow * ecryptfs_compute_root_iv 826*237fead6SMichael Halcrow * @crypt_stats 827*237fead6SMichael Halcrow * 828*237fead6SMichael Halcrow * On error, sets the root IV to all 0's. 829*237fead6SMichael Halcrow */ 830*237fead6SMichael Halcrow int ecryptfs_compute_root_iv(struct ecryptfs_crypt_stat *crypt_stat) 831*237fead6SMichael Halcrow { 832*237fead6SMichael Halcrow int rc = 0; 833*237fead6SMichael Halcrow char dst[MD5_DIGEST_SIZE]; 834*237fead6SMichael Halcrow 835*237fead6SMichael Halcrow BUG_ON(crypt_stat->iv_bytes > MD5_DIGEST_SIZE); 836*237fead6SMichael Halcrow BUG_ON(crypt_stat->iv_bytes <= 0); 837*237fead6SMichael Halcrow if (!ECRYPTFS_CHECK_FLAG(crypt_stat->flags, ECRYPTFS_KEY_VALID)) { 838*237fead6SMichael Halcrow rc = -EINVAL; 839*237fead6SMichael Halcrow ecryptfs_printk(KERN_WARNING, "Session key not valid; " 840*237fead6SMichael Halcrow "cannot generate root IV\n"); 841*237fead6SMichael Halcrow goto out; 842*237fead6SMichael Halcrow } 843*237fead6SMichael Halcrow rc = ecryptfs_calculate_md5(dst, crypt_stat, crypt_stat->key, 844*237fead6SMichael Halcrow crypt_stat->key_size); 845*237fead6SMichael Halcrow if (rc) { 846*237fead6SMichael Halcrow ecryptfs_printk(KERN_WARNING, "Error attempting to compute " 847*237fead6SMichael Halcrow "MD5 while generating root IV\n"); 848*237fead6SMichael Halcrow goto out; 849*237fead6SMichael Halcrow } 850*237fead6SMichael Halcrow memcpy(crypt_stat->root_iv, dst, crypt_stat->iv_bytes); 851*237fead6SMichael Halcrow out: 852*237fead6SMichael Halcrow if (rc) { 853*237fead6SMichael Halcrow memset(crypt_stat->root_iv, 0, crypt_stat->iv_bytes); 854*237fead6SMichael Halcrow ECRYPTFS_SET_FLAG(crypt_stat->flags, 855*237fead6SMichael Halcrow ECRYPTFS_SECURITY_WARNING); 856*237fead6SMichael Halcrow } 857*237fead6SMichael Halcrow return rc; 858*237fead6SMichael Halcrow } 859*237fead6SMichael Halcrow 860*237fead6SMichael Halcrow static void ecryptfs_generate_new_key(struct ecryptfs_crypt_stat *crypt_stat) 861*237fead6SMichael Halcrow { 862*237fead6SMichael Halcrow get_random_bytes(crypt_stat->key, crypt_stat->key_size); 863*237fead6SMichael Halcrow ECRYPTFS_SET_FLAG(crypt_stat->flags, ECRYPTFS_KEY_VALID); 864*237fead6SMichael Halcrow ecryptfs_compute_root_iv(crypt_stat); 865*237fead6SMichael Halcrow if (unlikely(ecryptfs_verbosity > 0)) { 866*237fead6SMichael Halcrow ecryptfs_printk(KERN_DEBUG, "Generated new session key:\n"); 867*237fead6SMichael Halcrow ecryptfs_dump_hex(crypt_stat->key, 868*237fead6SMichael Halcrow crypt_stat->key_size); 869*237fead6SMichael Halcrow } 870*237fead6SMichael Halcrow } 871*237fead6SMichael Halcrow 872*237fead6SMichael Halcrow /** 873*237fead6SMichael Halcrow * ecryptfs_set_default_crypt_stat_vals 874*237fead6SMichael Halcrow * @crypt_stat 875*237fead6SMichael Halcrow * 876*237fead6SMichael Halcrow * Default values in the event that policy does not override them. 877*237fead6SMichael Halcrow */ 878*237fead6SMichael Halcrow static void ecryptfs_set_default_crypt_stat_vals( 879*237fead6SMichael Halcrow struct ecryptfs_crypt_stat *crypt_stat, 880*237fead6SMichael Halcrow struct ecryptfs_mount_crypt_stat *mount_crypt_stat) 881*237fead6SMichael Halcrow { 882*237fead6SMichael Halcrow ecryptfs_set_default_sizes(crypt_stat); 883*237fead6SMichael Halcrow strcpy(crypt_stat->cipher, ECRYPTFS_DEFAULT_CIPHER); 884*237fead6SMichael Halcrow crypt_stat->key_size = ECRYPTFS_DEFAULT_KEY_BYTES; 885*237fead6SMichael Halcrow ECRYPTFS_CLEAR_FLAG(crypt_stat->flags, ECRYPTFS_KEY_VALID); 886*237fead6SMichael Halcrow crypt_stat->file_version = ECRYPTFS_FILE_VERSION; 887*237fead6SMichael Halcrow crypt_stat->mount_crypt_stat = mount_crypt_stat; 888*237fead6SMichael Halcrow } 889*237fead6SMichael Halcrow 890*237fead6SMichael Halcrow /** 891*237fead6SMichael Halcrow * ecryptfs_new_file_context 892*237fead6SMichael Halcrow * @ecryptfs_dentry 893*237fead6SMichael Halcrow * 894*237fead6SMichael Halcrow * If the crypto context for the file has not yet been established, 895*237fead6SMichael Halcrow * this is where we do that. Establishing a new crypto context 896*237fead6SMichael Halcrow * involves the following decisions: 897*237fead6SMichael Halcrow * - What cipher to use? 898*237fead6SMichael Halcrow * - What set of authentication tokens to use? 899*237fead6SMichael Halcrow * Here we just worry about getting enough information into the 900*237fead6SMichael Halcrow * authentication tokens so that we know that they are available. 901*237fead6SMichael Halcrow * We associate the available authentication tokens with the new file 902*237fead6SMichael Halcrow * via the set of signatures in the crypt_stat struct. Later, when 903*237fead6SMichael Halcrow * the headers are actually written out, we may again defer to 904*237fead6SMichael Halcrow * userspace to perform the encryption of the session key; for the 905*237fead6SMichael Halcrow * foreseeable future, this will be the case with public key packets. 906*237fead6SMichael Halcrow * 907*237fead6SMichael Halcrow * Returns zero on success; non-zero otherwise 908*237fead6SMichael Halcrow */ 909*237fead6SMichael Halcrow /* Associate an authentication token(s) with the file */ 910*237fead6SMichael Halcrow int ecryptfs_new_file_context(struct dentry *ecryptfs_dentry) 911*237fead6SMichael Halcrow { 912*237fead6SMichael Halcrow int rc = 0; 913*237fead6SMichael Halcrow struct ecryptfs_crypt_stat *crypt_stat = 914*237fead6SMichael Halcrow &ecryptfs_inode_to_private(ecryptfs_dentry->d_inode)->crypt_stat; 915*237fead6SMichael Halcrow struct ecryptfs_mount_crypt_stat *mount_crypt_stat = 916*237fead6SMichael Halcrow &ecryptfs_superblock_to_private( 917*237fead6SMichael Halcrow ecryptfs_dentry->d_sb)->mount_crypt_stat; 918*237fead6SMichael Halcrow int cipher_name_len; 919*237fead6SMichael Halcrow 920*237fead6SMichael Halcrow ecryptfs_set_default_crypt_stat_vals(crypt_stat, mount_crypt_stat); 921*237fead6SMichael Halcrow /* See if there are mount crypt options */ 922*237fead6SMichael Halcrow if (mount_crypt_stat->global_auth_tok) { 923*237fead6SMichael Halcrow ecryptfs_printk(KERN_DEBUG, "Initializing context for new " 924*237fead6SMichael Halcrow "file using mount_crypt_stat\n"); 925*237fead6SMichael Halcrow ECRYPTFS_SET_FLAG(crypt_stat->flags, ECRYPTFS_ENCRYPTED); 926*237fead6SMichael Halcrow ECRYPTFS_SET_FLAG(crypt_stat->flags, ECRYPTFS_KEY_VALID); 927*237fead6SMichael Halcrow memcpy(crypt_stat->keysigs[crypt_stat->num_keysigs++], 928*237fead6SMichael Halcrow mount_crypt_stat->global_auth_tok_sig, 929*237fead6SMichael Halcrow ECRYPTFS_SIG_SIZE_HEX); 930*237fead6SMichael Halcrow cipher_name_len = 931*237fead6SMichael Halcrow strlen(mount_crypt_stat->global_default_cipher_name); 932*237fead6SMichael Halcrow memcpy(crypt_stat->cipher, 933*237fead6SMichael Halcrow mount_crypt_stat->global_default_cipher_name, 934*237fead6SMichael Halcrow cipher_name_len); 935*237fead6SMichael Halcrow crypt_stat->cipher[cipher_name_len] = '\0'; 936*237fead6SMichael Halcrow crypt_stat->key_size = 937*237fead6SMichael Halcrow mount_crypt_stat->global_default_cipher_key_size; 938*237fead6SMichael Halcrow ecryptfs_generate_new_key(crypt_stat); 939*237fead6SMichael Halcrow } else 940*237fead6SMichael Halcrow /* We should not encounter this scenario since we 941*237fead6SMichael Halcrow * should detect lack of global_auth_tok at mount time 942*237fead6SMichael Halcrow * TODO: Applies to 0.1 release only; remove in future 943*237fead6SMichael Halcrow * release */ 944*237fead6SMichael Halcrow BUG(); 945*237fead6SMichael Halcrow rc = ecryptfs_init_crypt_ctx(crypt_stat); 946*237fead6SMichael Halcrow if (rc) 947*237fead6SMichael Halcrow ecryptfs_printk(KERN_ERR, "Error initializing cryptographic " 948*237fead6SMichael Halcrow "context for cipher [%s]: rc = [%d]\n", 949*237fead6SMichael Halcrow crypt_stat->cipher, rc); 950*237fead6SMichael Halcrow return rc; 951*237fead6SMichael Halcrow } 952*237fead6SMichael Halcrow 953*237fead6SMichael Halcrow /** 954*237fead6SMichael Halcrow * contains_ecryptfs_marker - check for the ecryptfs marker 955*237fead6SMichael Halcrow * @data: The data block in which to check 956*237fead6SMichael Halcrow * 957*237fead6SMichael Halcrow * Returns one if marker found; zero if not found 958*237fead6SMichael Halcrow */ 959*237fead6SMichael Halcrow int contains_ecryptfs_marker(char *data) 960*237fead6SMichael Halcrow { 961*237fead6SMichael Halcrow u32 m_1, m_2; 962*237fead6SMichael Halcrow 963*237fead6SMichael Halcrow memcpy(&m_1, data, 4); 964*237fead6SMichael Halcrow m_1 = be32_to_cpu(m_1); 965*237fead6SMichael Halcrow memcpy(&m_2, (data + 4), 4); 966*237fead6SMichael Halcrow m_2 = be32_to_cpu(m_2); 967*237fead6SMichael Halcrow if ((m_1 ^ MAGIC_ECRYPTFS_MARKER) == m_2) 968*237fead6SMichael Halcrow return 1; 969*237fead6SMichael Halcrow ecryptfs_printk(KERN_DEBUG, "m_1 = [0x%.8x]; m_2 = [0x%.8x]; " 970*237fead6SMichael Halcrow "MAGIC_ECRYPTFS_MARKER = [0x%.8x]\n", m_1, m_2, 971*237fead6SMichael Halcrow MAGIC_ECRYPTFS_MARKER); 972*237fead6SMichael Halcrow ecryptfs_printk(KERN_DEBUG, "(m_1 ^ MAGIC_ECRYPTFS_MARKER) = " 973*237fead6SMichael Halcrow "[0x%.8x]\n", (m_1 ^ MAGIC_ECRYPTFS_MARKER)); 974*237fead6SMichael Halcrow return 0; 975*237fead6SMichael Halcrow } 976*237fead6SMichael Halcrow 977*237fead6SMichael Halcrow struct ecryptfs_flag_map_elem { 978*237fead6SMichael Halcrow u32 file_flag; 979*237fead6SMichael Halcrow u32 local_flag; 980*237fead6SMichael Halcrow }; 981*237fead6SMichael Halcrow 982*237fead6SMichael Halcrow /* Add support for additional flags by adding elements here. */ 983*237fead6SMichael Halcrow static struct ecryptfs_flag_map_elem ecryptfs_flag_map[] = { 984*237fead6SMichael Halcrow {0x00000001, ECRYPTFS_ENABLE_HMAC}, 985*237fead6SMichael Halcrow {0x00000002, ECRYPTFS_ENCRYPTED} 986*237fead6SMichael Halcrow }; 987*237fead6SMichael Halcrow 988*237fead6SMichael Halcrow /** 989*237fead6SMichael Halcrow * ecryptfs_process_flags 990*237fead6SMichael Halcrow * @crypt_stat 991*237fead6SMichael Halcrow * @page_virt: Source data to be parsed 992*237fead6SMichael Halcrow * @bytes_read: Updated with the number of bytes read 993*237fead6SMichael Halcrow * 994*237fead6SMichael Halcrow * Returns zero on success; non-zero if the flag set is invalid 995*237fead6SMichael Halcrow */ 996*237fead6SMichael Halcrow static int ecryptfs_process_flags(struct ecryptfs_crypt_stat *crypt_stat, 997*237fead6SMichael Halcrow char *page_virt, int *bytes_read) 998*237fead6SMichael Halcrow { 999*237fead6SMichael Halcrow int rc = 0; 1000*237fead6SMichael Halcrow int i; 1001*237fead6SMichael Halcrow u32 flags; 1002*237fead6SMichael Halcrow 1003*237fead6SMichael Halcrow memcpy(&flags, page_virt, 4); 1004*237fead6SMichael Halcrow flags = be32_to_cpu(flags); 1005*237fead6SMichael Halcrow for (i = 0; i < ((sizeof(ecryptfs_flag_map) 1006*237fead6SMichael Halcrow / sizeof(struct ecryptfs_flag_map_elem))); i++) 1007*237fead6SMichael Halcrow if (flags & ecryptfs_flag_map[i].file_flag) { 1008*237fead6SMichael Halcrow ECRYPTFS_SET_FLAG(crypt_stat->flags, 1009*237fead6SMichael Halcrow ecryptfs_flag_map[i].local_flag); 1010*237fead6SMichael Halcrow } else 1011*237fead6SMichael Halcrow ECRYPTFS_CLEAR_FLAG(crypt_stat->flags, 1012*237fead6SMichael Halcrow ecryptfs_flag_map[i].local_flag); 1013*237fead6SMichael Halcrow /* Version is in top 8 bits of the 32-bit flag vector */ 1014*237fead6SMichael Halcrow crypt_stat->file_version = ((flags >> 24) & 0xFF); 1015*237fead6SMichael Halcrow (*bytes_read) = 4; 1016*237fead6SMichael Halcrow return rc; 1017*237fead6SMichael Halcrow } 1018*237fead6SMichael Halcrow 1019*237fead6SMichael Halcrow /** 1020*237fead6SMichael Halcrow * write_ecryptfs_marker 1021*237fead6SMichael Halcrow * @page_virt: The pointer to in a page to begin writing the marker 1022*237fead6SMichael Halcrow * @written: Number of bytes written 1023*237fead6SMichael Halcrow * 1024*237fead6SMichael Halcrow * Marker = 0x3c81b7f5 1025*237fead6SMichael Halcrow */ 1026*237fead6SMichael Halcrow static void write_ecryptfs_marker(char *page_virt, size_t *written) 1027*237fead6SMichael Halcrow { 1028*237fead6SMichael Halcrow u32 m_1, m_2; 1029*237fead6SMichael Halcrow 1030*237fead6SMichael Halcrow get_random_bytes(&m_1, (MAGIC_ECRYPTFS_MARKER_SIZE_BYTES / 2)); 1031*237fead6SMichael Halcrow m_2 = (m_1 ^ MAGIC_ECRYPTFS_MARKER); 1032*237fead6SMichael Halcrow m_1 = cpu_to_be32(m_1); 1033*237fead6SMichael Halcrow memcpy(page_virt, &m_1, (MAGIC_ECRYPTFS_MARKER_SIZE_BYTES / 2)); 1034*237fead6SMichael Halcrow m_2 = cpu_to_be32(m_2); 1035*237fead6SMichael Halcrow memcpy(page_virt + (MAGIC_ECRYPTFS_MARKER_SIZE_BYTES / 2), &m_2, 1036*237fead6SMichael Halcrow (MAGIC_ECRYPTFS_MARKER_SIZE_BYTES / 2)); 1037*237fead6SMichael Halcrow (*written) = MAGIC_ECRYPTFS_MARKER_SIZE_BYTES; 1038*237fead6SMichael Halcrow } 1039*237fead6SMichael Halcrow 1040*237fead6SMichael Halcrow static void 1041*237fead6SMichael Halcrow write_ecryptfs_flags(char *page_virt, struct ecryptfs_crypt_stat *crypt_stat, 1042*237fead6SMichael Halcrow size_t *written) 1043*237fead6SMichael Halcrow { 1044*237fead6SMichael Halcrow u32 flags = 0; 1045*237fead6SMichael Halcrow int i; 1046*237fead6SMichael Halcrow 1047*237fead6SMichael Halcrow for (i = 0; i < ((sizeof(ecryptfs_flag_map) 1048*237fead6SMichael Halcrow / sizeof(struct ecryptfs_flag_map_elem))); i++) 1049*237fead6SMichael Halcrow if (ECRYPTFS_CHECK_FLAG(crypt_stat->flags, 1050*237fead6SMichael Halcrow ecryptfs_flag_map[i].local_flag)) 1051*237fead6SMichael Halcrow flags |= ecryptfs_flag_map[i].file_flag; 1052*237fead6SMichael Halcrow /* Version is in top 8 bits of the 32-bit flag vector */ 1053*237fead6SMichael Halcrow flags |= ((((u8)crypt_stat->file_version) << 24) & 0xFF000000); 1054*237fead6SMichael Halcrow flags = cpu_to_be32(flags); 1055*237fead6SMichael Halcrow memcpy(page_virt, &flags, 4); 1056*237fead6SMichael Halcrow (*written) = 4; 1057*237fead6SMichael Halcrow } 1058*237fead6SMichael Halcrow 1059*237fead6SMichael Halcrow struct ecryptfs_cipher_code_str_map_elem { 1060*237fead6SMichael Halcrow char cipher_str[16]; 1061*237fead6SMichael Halcrow u16 cipher_code; 1062*237fead6SMichael Halcrow }; 1063*237fead6SMichael Halcrow 1064*237fead6SMichael Halcrow /* Add support for additional ciphers by adding elements here. The 1065*237fead6SMichael Halcrow * cipher_code is whatever OpenPGP applicatoins use to identify the 1066*237fead6SMichael Halcrow * ciphers. List in order of probability. */ 1067*237fead6SMichael Halcrow static struct ecryptfs_cipher_code_str_map_elem 1068*237fead6SMichael Halcrow ecryptfs_cipher_code_str_map[] = { 1069*237fead6SMichael Halcrow {"aes",RFC2440_CIPHER_AES_128 }, 1070*237fead6SMichael Halcrow {"blowfish", RFC2440_CIPHER_BLOWFISH}, 1071*237fead6SMichael Halcrow {"des3_ede", RFC2440_CIPHER_DES3_EDE}, 1072*237fead6SMichael Halcrow {"cast5", RFC2440_CIPHER_CAST_5}, 1073*237fead6SMichael Halcrow {"twofish", RFC2440_CIPHER_TWOFISH}, 1074*237fead6SMichael Halcrow {"cast6", RFC2440_CIPHER_CAST_6}, 1075*237fead6SMichael Halcrow {"aes", RFC2440_CIPHER_AES_192}, 1076*237fead6SMichael Halcrow {"aes", RFC2440_CIPHER_AES_256} 1077*237fead6SMichael Halcrow }; 1078*237fead6SMichael Halcrow 1079*237fead6SMichael Halcrow /** 1080*237fead6SMichael Halcrow * ecryptfs_code_for_cipher_string 1081*237fead6SMichael Halcrow * @str: The string representing the cipher name 1082*237fead6SMichael Halcrow * 1083*237fead6SMichael Halcrow * Returns zero on no match, or the cipher code on match 1084*237fead6SMichael Halcrow */ 1085*237fead6SMichael Halcrow u16 ecryptfs_code_for_cipher_string(struct ecryptfs_crypt_stat *crypt_stat) 1086*237fead6SMichael Halcrow { 1087*237fead6SMichael Halcrow int i; 1088*237fead6SMichael Halcrow u16 code = 0; 1089*237fead6SMichael Halcrow struct ecryptfs_cipher_code_str_map_elem *map = 1090*237fead6SMichael Halcrow ecryptfs_cipher_code_str_map; 1091*237fead6SMichael Halcrow 1092*237fead6SMichael Halcrow if (strcmp(crypt_stat->cipher, "aes") == 0) { 1093*237fead6SMichael Halcrow switch (crypt_stat->key_size) { 1094*237fead6SMichael Halcrow case 16: 1095*237fead6SMichael Halcrow code = RFC2440_CIPHER_AES_128; 1096*237fead6SMichael Halcrow break; 1097*237fead6SMichael Halcrow case 24: 1098*237fead6SMichael Halcrow code = RFC2440_CIPHER_AES_192; 1099*237fead6SMichael Halcrow break; 1100*237fead6SMichael Halcrow case 32: 1101*237fead6SMichael Halcrow code = RFC2440_CIPHER_AES_256; 1102*237fead6SMichael Halcrow } 1103*237fead6SMichael Halcrow } else { 1104*237fead6SMichael Halcrow for (i = 0; i < ARRAY_SIZE(ecryptfs_cipher_code_str_map); i++) 1105*237fead6SMichael Halcrow if (strcmp(crypt_stat->cipher, map[i].cipher_str) == 0){ 1106*237fead6SMichael Halcrow code = map[i].cipher_code; 1107*237fead6SMichael Halcrow break; 1108*237fead6SMichael Halcrow } 1109*237fead6SMichael Halcrow } 1110*237fead6SMichael Halcrow return code; 1111*237fead6SMichael Halcrow } 1112*237fead6SMichael Halcrow 1113*237fead6SMichael Halcrow /** 1114*237fead6SMichael Halcrow * ecryptfs_cipher_code_to_string 1115*237fead6SMichael Halcrow * @str: Destination to write out the cipher name 1116*237fead6SMichael Halcrow * @cipher_code: The code to convert to cipher name string 1117*237fead6SMichael Halcrow * 1118*237fead6SMichael Halcrow * Returns zero on success 1119*237fead6SMichael Halcrow */ 1120*237fead6SMichael Halcrow int ecryptfs_cipher_code_to_string(char *str, u16 cipher_code) 1121*237fead6SMichael Halcrow { 1122*237fead6SMichael Halcrow int rc = 0; 1123*237fead6SMichael Halcrow int i; 1124*237fead6SMichael Halcrow 1125*237fead6SMichael Halcrow str[0] = '\0'; 1126*237fead6SMichael Halcrow for (i = 0; i < ARRAY_SIZE(ecryptfs_cipher_code_str_map); i++) 1127*237fead6SMichael Halcrow if (cipher_code == ecryptfs_cipher_code_str_map[i].cipher_code) 1128*237fead6SMichael Halcrow strcpy(str, ecryptfs_cipher_code_str_map[i].cipher_str); 1129*237fead6SMichael Halcrow if (str[0] == '\0') { 1130*237fead6SMichael Halcrow ecryptfs_printk(KERN_WARNING, "Cipher code not recognized: " 1131*237fead6SMichael Halcrow "[%d]\n", cipher_code); 1132*237fead6SMichael Halcrow rc = -EINVAL; 1133*237fead6SMichael Halcrow } 1134*237fead6SMichael Halcrow return rc; 1135*237fead6SMichael Halcrow } 1136*237fead6SMichael Halcrow 1137*237fead6SMichael Halcrow /** 1138*237fead6SMichael Halcrow * ecryptfs_read_header_region 1139*237fead6SMichael Halcrow * @data 1140*237fead6SMichael Halcrow * @dentry 1141*237fead6SMichael Halcrow * @nd 1142*237fead6SMichael Halcrow * 1143*237fead6SMichael Halcrow * Returns zero on success; non-zero otherwise 1144*237fead6SMichael Halcrow */ 1145*237fead6SMichael Halcrow int ecryptfs_read_header_region(char *data, struct dentry *dentry, 1146*237fead6SMichael Halcrow struct vfsmount *mnt) 1147*237fead6SMichael Halcrow { 1148*237fead6SMichael Halcrow struct file *file; 1149*237fead6SMichael Halcrow mm_segment_t oldfs; 1150*237fead6SMichael Halcrow int rc; 1151*237fead6SMichael Halcrow 1152*237fead6SMichael Halcrow mnt = mntget(mnt); 1153*237fead6SMichael Halcrow file = dentry_open(dentry, mnt, O_RDONLY); 1154*237fead6SMichael Halcrow if (IS_ERR(file)) { 1155*237fead6SMichael Halcrow ecryptfs_printk(KERN_DEBUG, "Error opening file to " 1156*237fead6SMichael Halcrow "read header region\n"); 1157*237fead6SMichael Halcrow mntput(mnt); 1158*237fead6SMichael Halcrow rc = PTR_ERR(file); 1159*237fead6SMichael Halcrow goto out; 1160*237fead6SMichael Halcrow } 1161*237fead6SMichael Halcrow file->f_pos = 0; 1162*237fead6SMichael Halcrow oldfs = get_fs(); 1163*237fead6SMichael Halcrow set_fs(get_ds()); 1164*237fead6SMichael Halcrow /* For releases 0.1 and 0.2, all of the header information 1165*237fead6SMichael Halcrow * fits in the first data extent-sized region. */ 1166*237fead6SMichael Halcrow rc = file->f_op->read(file, (char __user *)data, 1167*237fead6SMichael Halcrow ECRYPTFS_DEFAULT_EXTENT_SIZE, &file->f_pos); 1168*237fead6SMichael Halcrow set_fs(oldfs); 1169*237fead6SMichael Halcrow fput(file); 1170*237fead6SMichael Halcrow rc = 0; 1171*237fead6SMichael Halcrow out: 1172*237fead6SMichael Halcrow return rc; 1173*237fead6SMichael Halcrow } 1174*237fead6SMichael Halcrow 1175*237fead6SMichael Halcrow static void 1176*237fead6SMichael Halcrow write_header_metadata(char *virt, struct ecryptfs_crypt_stat *crypt_stat, 1177*237fead6SMichael Halcrow size_t *written) 1178*237fead6SMichael Halcrow { 1179*237fead6SMichael Halcrow u32 header_extent_size; 1180*237fead6SMichael Halcrow u16 num_header_extents_at_front; 1181*237fead6SMichael Halcrow 1182*237fead6SMichael Halcrow header_extent_size = (u32)crypt_stat->header_extent_size; 1183*237fead6SMichael Halcrow num_header_extents_at_front = 1184*237fead6SMichael Halcrow (u16)crypt_stat->num_header_extents_at_front; 1185*237fead6SMichael Halcrow header_extent_size = cpu_to_be32(header_extent_size); 1186*237fead6SMichael Halcrow memcpy(virt, &header_extent_size, 4); 1187*237fead6SMichael Halcrow virt += 4; 1188*237fead6SMichael Halcrow num_header_extents_at_front = cpu_to_be16(num_header_extents_at_front); 1189*237fead6SMichael Halcrow memcpy(virt, &num_header_extents_at_front, 2); 1190*237fead6SMichael Halcrow (*written) = 6; 1191*237fead6SMichael Halcrow } 1192*237fead6SMichael Halcrow 1193*237fead6SMichael Halcrow struct kmem_cache *ecryptfs_header_cache_0; 1194*237fead6SMichael Halcrow struct kmem_cache *ecryptfs_header_cache_1; 1195*237fead6SMichael Halcrow struct kmem_cache *ecryptfs_header_cache_2; 1196*237fead6SMichael Halcrow 1197*237fead6SMichael Halcrow /** 1198*237fead6SMichael Halcrow * ecryptfs_write_headers_virt 1199*237fead6SMichael Halcrow * @page_virt 1200*237fead6SMichael Halcrow * @crypt_stat 1201*237fead6SMichael Halcrow * @ecryptfs_dentry 1202*237fead6SMichael Halcrow * 1203*237fead6SMichael Halcrow * Format version: 1 1204*237fead6SMichael Halcrow * 1205*237fead6SMichael Halcrow * Header Extent: 1206*237fead6SMichael Halcrow * Octets 0-7: Unencrypted file size (big-endian) 1207*237fead6SMichael Halcrow * Octets 8-15: eCryptfs special marker 1208*237fead6SMichael Halcrow * Octets 16-19: Flags 1209*237fead6SMichael Halcrow * Octet 16: File format version number (between 0 and 255) 1210*237fead6SMichael Halcrow * Octets 17-18: Reserved 1211*237fead6SMichael Halcrow * Octet 19: Bit 1 (lsb): Reserved 1212*237fead6SMichael Halcrow * Bit 2: Encrypted? 1213*237fead6SMichael Halcrow * Bits 3-8: Reserved 1214*237fead6SMichael Halcrow * Octets 20-23: Header extent size (big-endian) 1215*237fead6SMichael Halcrow * Octets 24-25: Number of header extents at front of file 1216*237fead6SMichael Halcrow * (big-endian) 1217*237fead6SMichael Halcrow * Octet 26: Begin RFC 2440 authentication token packet set 1218*237fead6SMichael Halcrow * Data Extent 0: 1219*237fead6SMichael Halcrow * Lower data (CBC encrypted) 1220*237fead6SMichael Halcrow * Data Extent 1: 1221*237fead6SMichael Halcrow * Lower data (CBC encrypted) 1222*237fead6SMichael Halcrow * ... 1223*237fead6SMichael Halcrow * 1224*237fead6SMichael Halcrow * Returns zero on success 1225*237fead6SMichael Halcrow */ 1226*237fead6SMichael Halcrow int ecryptfs_write_headers_virt(char *page_virt, 1227*237fead6SMichael Halcrow struct ecryptfs_crypt_stat *crypt_stat, 1228*237fead6SMichael Halcrow struct dentry *ecryptfs_dentry) 1229*237fead6SMichael Halcrow { 1230*237fead6SMichael Halcrow int rc; 1231*237fead6SMichael Halcrow size_t written; 1232*237fead6SMichael Halcrow size_t offset; 1233*237fead6SMichael Halcrow 1234*237fead6SMichael Halcrow offset = ECRYPTFS_FILE_SIZE_BYTES; 1235*237fead6SMichael Halcrow write_ecryptfs_marker((page_virt + offset), &written); 1236*237fead6SMichael Halcrow offset += written; 1237*237fead6SMichael Halcrow write_ecryptfs_flags((page_virt + offset), crypt_stat, &written); 1238*237fead6SMichael Halcrow offset += written; 1239*237fead6SMichael Halcrow write_header_metadata((page_virt + offset), crypt_stat, &written); 1240*237fead6SMichael Halcrow offset += written; 1241*237fead6SMichael Halcrow rc = ecryptfs_generate_key_packet_set((page_virt + offset), crypt_stat, 1242*237fead6SMichael Halcrow ecryptfs_dentry, &written, 1243*237fead6SMichael Halcrow PAGE_CACHE_SIZE - offset); 1244*237fead6SMichael Halcrow if (rc) 1245*237fead6SMichael Halcrow ecryptfs_printk(KERN_WARNING, "Error generating key packet " 1246*237fead6SMichael Halcrow "set; rc = [%d]\n", rc); 1247*237fead6SMichael Halcrow return rc; 1248*237fead6SMichael Halcrow } 1249*237fead6SMichael Halcrow 1250*237fead6SMichael Halcrow /** 1251*237fead6SMichael Halcrow * ecryptfs_write_headers 1252*237fead6SMichael Halcrow * @lower_file: The lower file struct, which was returned from dentry_open 1253*237fead6SMichael Halcrow * 1254*237fead6SMichael Halcrow * Write the file headers out. This will likely involve a userspace 1255*237fead6SMichael Halcrow * callout, in which the session key is encrypted with one or more 1256*237fead6SMichael Halcrow * public keys and/or the passphrase necessary to do the encryption is 1257*237fead6SMichael Halcrow * retrieved via a prompt. Exactly what happens at this point should 1258*237fead6SMichael Halcrow * be policy-dependent. 1259*237fead6SMichael Halcrow * 1260*237fead6SMichael Halcrow * Returns zero on success; non-zero on error 1261*237fead6SMichael Halcrow */ 1262*237fead6SMichael Halcrow int ecryptfs_write_headers(struct dentry *ecryptfs_dentry, 1263*237fead6SMichael Halcrow struct file *lower_file) 1264*237fead6SMichael Halcrow { 1265*237fead6SMichael Halcrow mm_segment_t oldfs; 1266*237fead6SMichael Halcrow struct ecryptfs_crypt_stat *crypt_stat; 1267*237fead6SMichael Halcrow char *page_virt; 1268*237fead6SMichael Halcrow int current_header_page; 1269*237fead6SMichael Halcrow int header_pages; 1270*237fead6SMichael Halcrow int rc = 0; 1271*237fead6SMichael Halcrow 1272*237fead6SMichael Halcrow crypt_stat = &ecryptfs_inode_to_private( 1273*237fead6SMichael Halcrow ecryptfs_dentry->d_inode)->crypt_stat; 1274*237fead6SMichael Halcrow if (likely(ECRYPTFS_CHECK_FLAG(crypt_stat->flags, 1275*237fead6SMichael Halcrow ECRYPTFS_ENCRYPTED))) { 1276*237fead6SMichael Halcrow if (!ECRYPTFS_CHECK_FLAG(crypt_stat->flags, 1277*237fead6SMichael Halcrow ECRYPTFS_KEY_VALID)) { 1278*237fead6SMichael Halcrow ecryptfs_printk(KERN_DEBUG, "Key is " 1279*237fead6SMichael Halcrow "invalid; bailing out\n"); 1280*237fead6SMichael Halcrow rc = -EINVAL; 1281*237fead6SMichael Halcrow goto out; 1282*237fead6SMichael Halcrow } 1283*237fead6SMichael Halcrow } else { 1284*237fead6SMichael Halcrow rc = -EINVAL; 1285*237fead6SMichael Halcrow ecryptfs_printk(KERN_WARNING, 1286*237fead6SMichael Halcrow "Called with crypt_stat->encrypted == 0\n"); 1287*237fead6SMichael Halcrow goto out; 1288*237fead6SMichael Halcrow } 1289*237fead6SMichael Halcrow /* Released in this function */ 1290*237fead6SMichael Halcrow page_virt = kmem_cache_alloc(ecryptfs_header_cache_0, SLAB_USER); 1291*237fead6SMichael Halcrow if (!page_virt) { 1292*237fead6SMichael Halcrow ecryptfs_printk(KERN_ERR, "Out of memory\n"); 1293*237fead6SMichael Halcrow rc = -ENOMEM; 1294*237fead6SMichael Halcrow goto out; 1295*237fead6SMichael Halcrow } 1296*237fead6SMichael Halcrow memset(page_virt, 0, PAGE_CACHE_SIZE); 1297*237fead6SMichael Halcrow rc = ecryptfs_write_headers_virt(page_virt, crypt_stat, 1298*237fead6SMichael Halcrow ecryptfs_dentry); 1299*237fead6SMichael Halcrow if (unlikely(rc)) { 1300*237fead6SMichael Halcrow ecryptfs_printk(KERN_ERR, "Error whilst writing headers\n"); 1301*237fead6SMichael Halcrow memset(page_virt, 0, PAGE_CACHE_SIZE); 1302*237fead6SMichael Halcrow goto out_free; 1303*237fead6SMichael Halcrow } 1304*237fead6SMichael Halcrow ecryptfs_printk(KERN_DEBUG, 1305*237fead6SMichael Halcrow "Writing key packet set to underlying file\n"); 1306*237fead6SMichael Halcrow lower_file->f_pos = 0; 1307*237fead6SMichael Halcrow oldfs = get_fs(); 1308*237fead6SMichael Halcrow set_fs(get_ds()); 1309*237fead6SMichael Halcrow ecryptfs_printk(KERN_DEBUG, "Calling lower_file->f_op->" 1310*237fead6SMichael Halcrow "write() w/ header page; lower_file->f_pos = " 1311*237fead6SMichael Halcrow "[0x%.16x]\n", lower_file->f_pos); 1312*237fead6SMichael Halcrow lower_file->f_op->write(lower_file, (char __user *)page_virt, 1313*237fead6SMichael Halcrow PAGE_CACHE_SIZE, &lower_file->f_pos); 1314*237fead6SMichael Halcrow header_pages = ((crypt_stat->header_extent_size 1315*237fead6SMichael Halcrow * crypt_stat->num_header_extents_at_front) 1316*237fead6SMichael Halcrow / PAGE_CACHE_SIZE); 1317*237fead6SMichael Halcrow memset(page_virt, 0, PAGE_CACHE_SIZE); 1318*237fead6SMichael Halcrow current_header_page = 1; 1319*237fead6SMichael Halcrow while (current_header_page < header_pages) { 1320*237fead6SMichael Halcrow ecryptfs_printk(KERN_DEBUG, "Calling lower_file->f_op->" 1321*237fead6SMichael Halcrow "write() w/ zero'd page; lower_file->f_pos = " 1322*237fead6SMichael Halcrow "[0x%.16x]\n", lower_file->f_pos); 1323*237fead6SMichael Halcrow lower_file->f_op->write(lower_file, (char __user *)page_virt, 1324*237fead6SMichael Halcrow PAGE_CACHE_SIZE, &lower_file->f_pos); 1325*237fead6SMichael Halcrow current_header_page++; 1326*237fead6SMichael Halcrow } 1327*237fead6SMichael Halcrow set_fs(oldfs); 1328*237fead6SMichael Halcrow ecryptfs_printk(KERN_DEBUG, 1329*237fead6SMichael Halcrow "Done writing key packet set to underlying file.\n"); 1330*237fead6SMichael Halcrow out_free: 1331*237fead6SMichael Halcrow kmem_cache_free(ecryptfs_header_cache_0, page_virt); 1332*237fead6SMichael Halcrow out: 1333*237fead6SMichael Halcrow return rc; 1334*237fead6SMichael Halcrow } 1335*237fead6SMichael Halcrow 1336*237fead6SMichael Halcrow static int parse_header_metadata(struct ecryptfs_crypt_stat *crypt_stat, 1337*237fead6SMichael Halcrow char *virt, int *bytes_read) 1338*237fead6SMichael Halcrow { 1339*237fead6SMichael Halcrow int rc = 0; 1340*237fead6SMichael Halcrow u32 header_extent_size; 1341*237fead6SMichael Halcrow u16 num_header_extents_at_front; 1342*237fead6SMichael Halcrow 1343*237fead6SMichael Halcrow memcpy(&header_extent_size, virt, 4); 1344*237fead6SMichael Halcrow header_extent_size = be32_to_cpu(header_extent_size); 1345*237fead6SMichael Halcrow virt += 4; 1346*237fead6SMichael Halcrow memcpy(&num_header_extents_at_front, virt, 2); 1347*237fead6SMichael Halcrow num_header_extents_at_front = be16_to_cpu(num_header_extents_at_front); 1348*237fead6SMichael Halcrow crypt_stat->header_extent_size = (int)header_extent_size; 1349*237fead6SMichael Halcrow crypt_stat->num_header_extents_at_front = 1350*237fead6SMichael Halcrow (int)num_header_extents_at_front; 1351*237fead6SMichael Halcrow (*bytes_read) = 6; 1352*237fead6SMichael Halcrow if ((crypt_stat->header_extent_size 1353*237fead6SMichael Halcrow * crypt_stat->num_header_extents_at_front) 1354*237fead6SMichael Halcrow < ECRYPTFS_MINIMUM_HEADER_EXTENT_SIZE) { 1355*237fead6SMichael Halcrow rc = -EINVAL; 1356*237fead6SMichael Halcrow ecryptfs_printk(KERN_WARNING, "Invalid header extent size: " 1357*237fead6SMichael Halcrow "[%d]\n", crypt_stat->header_extent_size); 1358*237fead6SMichael Halcrow } 1359*237fead6SMichael Halcrow return rc; 1360*237fead6SMichael Halcrow } 1361*237fead6SMichael Halcrow 1362*237fead6SMichael Halcrow /** 1363*237fead6SMichael Halcrow * set_default_header_data 1364*237fead6SMichael Halcrow * 1365*237fead6SMichael Halcrow * For version 0 file format; this function is only for backwards 1366*237fead6SMichael Halcrow * compatibility for files created with the prior versions of 1367*237fead6SMichael Halcrow * eCryptfs. 1368*237fead6SMichael Halcrow */ 1369*237fead6SMichael Halcrow static void set_default_header_data(struct ecryptfs_crypt_stat *crypt_stat) 1370*237fead6SMichael Halcrow { 1371*237fead6SMichael Halcrow crypt_stat->header_extent_size = 4096; 1372*237fead6SMichael Halcrow crypt_stat->num_header_extents_at_front = 1; 1373*237fead6SMichael Halcrow } 1374*237fead6SMichael Halcrow 1375*237fead6SMichael Halcrow /** 1376*237fead6SMichael Halcrow * ecryptfs_read_headers_virt 1377*237fead6SMichael Halcrow * 1378*237fead6SMichael Halcrow * Read/parse the header data. The header format is detailed in the 1379*237fead6SMichael Halcrow * comment block for the ecryptfs_write_headers_virt() function. 1380*237fead6SMichael Halcrow * 1381*237fead6SMichael Halcrow * Returns zero on success 1382*237fead6SMichael Halcrow */ 1383*237fead6SMichael Halcrow static int ecryptfs_read_headers_virt(char *page_virt, 1384*237fead6SMichael Halcrow struct ecryptfs_crypt_stat *crypt_stat, 1385*237fead6SMichael Halcrow struct dentry *ecryptfs_dentry) 1386*237fead6SMichael Halcrow { 1387*237fead6SMichael Halcrow int rc = 0; 1388*237fead6SMichael Halcrow int offset; 1389*237fead6SMichael Halcrow int bytes_read; 1390*237fead6SMichael Halcrow 1391*237fead6SMichael Halcrow ecryptfs_set_default_sizes(crypt_stat); 1392*237fead6SMichael Halcrow crypt_stat->mount_crypt_stat = &ecryptfs_superblock_to_private( 1393*237fead6SMichael Halcrow ecryptfs_dentry->d_sb)->mount_crypt_stat; 1394*237fead6SMichael Halcrow offset = ECRYPTFS_FILE_SIZE_BYTES; 1395*237fead6SMichael Halcrow rc = contains_ecryptfs_marker(page_virt + offset); 1396*237fead6SMichael Halcrow if (rc == 0) { 1397*237fead6SMichael Halcrow rc = -EINVAL; 1398*237fead6SMichael Halcrow goto out; 1399*237fead6SMichael Halcrow } 1400*237fead6SMichael Halcrow offset += MAGIC_ECRYPTFS_MARKER_SIZE_BYTES; 1401*237fead6SMichael Halcrow rc = ecryptfs_process_flags(crypt_stat, (page_virt + offset), 1402*237fead6SMichael Halcrow &bytes_read); 1403*237fead6SMichael Halcrow if (rc) { 1404*237fead6SMichael Halcrow ecryptfs_printk(KERN_WARNING, "Error processing flags\n"); 1405*237fead6SMichael Halcrow goto out; 1406*237fead6SMichael Halcrow } 1407*237fead6SMichael Halcrow if (crypt_stat->file_version > ECRYPTFS_SUPPORTED_FILE_VERSION) { 1408*237fead6SMichael Halcrow ecryptfs_printk(KERN_WARNING, "File version is [%d]; only " 1409*237fead6SMichael Halcrow "file version [%d] is supported by this " 1410*237fead6SMichael Halcrow "version of eCryptfs\n", 1411*237fead6SMichael Halcrow crypt_stat->file_version, 1412*237fead6SMichael Halcrow ECRYPTFS_SUPPORTED_FILE_VERSION); 1413*237fead6SMichael Halcrow rc = -EINVAL; 1414*237fead6SMichael Halcrow goto out; 1415*237fead6SMichael Halcrow } 1416*237fead6SMichael Halcrow offset += bytes_read; 1417*237fead6SMichael Halcrow if (crypt_stat->file_version >= 1) { 1418*237fead6SMichael Halcrow rc = parse_header_metadata(crypt_stat, (page_virt + offset), 1419*237fead6SMichael Halcrow &bytes_read); 1420*237fead6SMichael Halcrow if (rc) { 1421*237fead6SMichael Halcrow ecryptfs_printk(KERN_WARNING, "Error reading header " 1422*237fead6SMichael Halcrow "metadata; rc = [%d]\n", rc); 1423*237fead6SMichael Halcrow } 1424*237fead6SMichael Halcrow offset += bytes_read; 1425*237fead6SMichael Halcrow } else 1426*237fead6SMichael Halcrow set_default_header_data(crypt_stat); 1427*237fead6SMichael Halcrow rc = ecryptfs_parse_packet_set(crypt_stat, (page_virt + offset), 1428*237fead6SMichael Halcrow ecryptfs_dentry); 1429*237fead6SMichael Halcrow out: 1430*237fead6SMichael Halcrow return rc; 1431*237fead6SMichael Halcrow } 1432*237fead6SMichael Halcrow 1433*237fead6SMichael Halcrow /** 1434*237fead6SMichael Halcrow * ecryptfs_read_headers 1435*237fead6SMichael Halcrow * 1436*237fead6SMichael Halcrow * Returns zero if valid headers found and parsed; non-zero otherwise 1437*237fead6SMichael Halcrow */ 1438*237fead6SMichael Halcrow int ecryptfs_read_headers(struct dentry *ecryptfs_dentry, 1439*237fead6SMichael Halcrow struct file *lower_file) 1440*237fead6SMichael Halcrow { 1441*237fead6SMichael Halcrow int rc = 0; 1442*237fead6SMichael Halcrow char *page_virt = NULL; 1443*237fead6SMichael Halcrow mm_segment_t oldfs; 1444*237fead6SMichael Halcrow ssize_t bytes_read; 1445*237fead6SMichael Halcrow struct ecryptfs_crypt_stat *crypt_stat = 1446*237fead6SMichael Halcrow &ecryptfs_inode_to_private(ecryptfs_dentry->d_inode)->crypt_stat; 1447*237fead6SMichael Halcrow 1448*237fead6SMichael Halcrow /* Read the first page from the underlying file */ 1449*237fead6SMichael Halcrow page_virt = kmem_cache_alloc(ecryptfs_header_cache_1, SLAB_USER); 1450*237fead6SMichael Halcrow if (!page_virt) { 1451*237fead6SMichael Halcrow rc = -ENOMEM; 1452*237fead6SMichael Halcrow ecryptfs_printk(KERN_ERR, "Unable to allocate page_virt\n"); 1453*237fead6SMichael Halcrow goto out; 1454*237fead6SMichael Halcrow } 1455*237fead6SMichael Halcrow lower_file->f_pos = 0; 1456*237fead6SMichael Halcrow oldfs = get_fs(); 1457*237fead6SMichael Halcrow set_fs(get_ds()); 1458*237fead6SMichael Halcrow bytes_read = lower_file->f_op->read(lower_file, 1459*237fead6SMichael Halcrow (char __user *)page_virt, 1460*237fead6SMichael Halcrow ECRYPTFS_DEFAULT_EXTENT_SIZE, 1461*237fead6SMichael Halcrow &lower_file->f_pos); 1462*237fead6SMichael Halcrow set_fs(oldfs); 1463*237fead6SMichael Halcrow if (bytes_read != ECRYPTFS_DEFAULT_EXTENT_SIZE) { 1464*237fead6SMichael Halcrow rc = -EINVAL; 1465*237fead6SMichael Halcrow goto out; 1466*237fead6SMichael Halcrow } 1467*237fead6SMichael Halcrow rc = ecryptfs_read_headers_virt(page_virt, crypt_stat, 1468*237fead6SMichael Halcrow ecryptfs_dentry); 1469*237fead6SMichael Halcrow if (rc) { 1470*237fead6SMichael Halcrow ecryptfs_printk(KERN_DEBUG, "Valid eCryptfs headers not " 1471*237fead6SMichael Halcrow "found\n"); 1472*237fead6SMichael Halcrow rc = -EINVAL; 1473*237fead6SMichael Halcrow } 1474*237fead6SMichael Halcrow out: 1475*237fead6SMichael Halcrow if (page_virt) { 1476*237fead6SMichael Halcrow memset(page_virt, 0, PAGE_CACHE_SIZE); 1477*237fead6SMichael Halcrow kmem_cache_free(ecryptfs_header_cache_1, page_virt); 1478*237fead6SMichael Halcrow } 1479*237fead6SMichael Halcrow return rc; 1480*237fead6SMichael Halcrow } 1481*237fead6SMichael Halcrow 1482*237fead6SMichael Halcrow /** 1483*237fead6SMichael Halcrow * ecryptfs_encode_filename - converts a plaintext file name to cipher text 1484*237fead6SMichael Halcrow * @crypt_stat: The crypt_stat struct associated with the file anem to encode 1485*237fead6SMichael Halcrow * @name: The plaintext name 1486*237fead6SMichael Halcrow * @length: The length of the plaintext 1487*237fead6SMichael Halcrow * @encoded_name: The encypted name 1488*237fead6SMichael Halcrow * 1489*237fead6SMichael Halcrow * Encrypts and encodes a filename into something that constitutes a 1490*237fead6SMichael Halcrow * valid filename for a filesystem, with printable characters. 1491*237fead6SMichael Halcrow * 1492*237fead6SMichael Halcrow * We assume that we have a properly initialized crypto context, 1493*237fead6SMichael Halcrow * pointed to by crypt_stat->tfm. 1494*237fead6SMichael Halcrow * 1495*237fead6SMichael Halcrow * TODO: Implement filename decoding and decryption here, in place of 1496*237fead6SMichael Halcrow * memcpy. We are keeping the framework around for now to (1) 1497*237fead6SMichael Halcrow * facilitate testing of the components needed to implement filename 1498*237fead6SMichael Halcrow * encryption and (2) to provide a code base from which other 1499*237fead6SMichael Halcrow * developers in the community can easily implement this feature. 1500*237fead6SMichael Halcrow * 1501*237fead6SMichael Halcrow * Returns the length of encoded filename; negative if error 1502*237fead6SMichael Halcrow */ 1503*237fead6SMichael Halcrow int 1504*237fead6SMichael Halcrow ecryptfs_encode_filename(struct ecryptfs_crypt_stat *crypt_stat, 1505*237fead6SMichael Halcrow const char *name, int length, char **encoded_name) 1506*237fead6SMichael Halcrow { 1507*237fead6SMichael Halcrow int error = 0; 1508*237fead6SMichael Halcrow 1509*237fead6SMichael Halcrow (*encoded_name) = kmalloc(length + 2, GFP_KERNEL); 1510*237fead6SMichael Halcrow if (!(*encoded_name)) { 1511*237fead6SMichael Halcrow error = -ENOMEM; 1512*237fead6SMichael Halcrow goto out; 1513*237fead6SMichael Halcrow } 1514*237fead6SMichael Halcrow /* TODO: Filename encryption is a scheduled feature for a 1515*237fead6SMichael Halcrow * future version of eCryptfs. This function is here only for 1516*237fead6SMichael Halcrow * the purpose of providing a framework for other developers 1517*237fead6SMichael Halcrow * to easily implement filename encryption. Hint: Replace this 1518*237fead6SMichael Halcrow * memcpy() with a call to encrypt and encode the 1519*237fead6SMichael Halcrow * filename, the set the length accordingly. */ 1520*237fead6SMichael Halcrow memcpy((void *)(*encoded_name), (void *)name, length); 1521*237fead6SMichael Halcrow (*encoded_name)[length] = '\0'; 1522*237fead6SMichael Halcrow error = length + 1; 1523*237fead6SMichael Halcrow out: 1524*237fead6SMichael Halcrow return error; 1525*237fead6SMichael Halcrow } 1526*237fead6SMichael Halcrow 1527*237fead6SMichael Halcrow /** 1528*237fead6SMichael Halcrow * ecryptfs_decode_filename - converts the cipher text name to plaintext 1529*237fead6SMichael Halcrow * @crypt_stat: The crypt_stat struct associated with the file 1530*237fead6SMichael Halcrow * @name: The filename in cipher text 1531*237fead6SMichael Halcrow * @length: The length of the cipher text name 1532*237fead6SMichael Halcrow * @decrypted_name: The plaintext name 1533*237fead6SMichael Halcrow * 1534*237fead6SMichael Halcrow * Decodes and decrypts the filename. 1535*237fead6SMichael Halcrow * 1536*237fead6SMichael Halcrow * We assume that we have a properly initialized crypto context, 1537*237fead6SMichael Halcrow * pointed to by crypt_stat->tfm. 1538*237fead6SMichael Halcrow * 1539*237fead6SMichael Halcrow * TODO: Implement filename decoding and decryption here, in place of 1540*237fead6SMichael Halcrow * memcpy. We are keeping the framework around for now to (1) 1541*237fead6SMichael Halcrow * facilitate testing of the components needed to implement filename 1542*237fead6SMichael Halcrow * encryption and (2) to provide a code base from which other 1543*237fead6SMichael Halcrow * developers in the community can easily implement this feature. 1544*237fead6SMichael Halcrow * 1545*237fead6SMichael Halcrow * Returns the length of decoded filename; negative if error 1546*237fead6SMichael Halcrow */ 1547*237fead6SMichael Halcrow int 1548*237fead6SMichael Halcrow ecryptfs_decode_filename(struct ecryptfs_crypt_stat *crypt_stat, 1549*237fead6SMichael Halcrow const char *name, int length, char **decrypted_name) 1550*237fead6SMichael Halcrow { 1551*237fead6SMichael Halcrow int error = 0; 1552*237fead6SMichael Halcrow 1553*237fead6SMichael Halcrow (*decrypted_name) = kmalloc(length + 2, GFP_KERNEL); 1554*237fead6SMichael Halcrow if (!(*decrypted_name)) { 1555*237fead6SMichael Halcrow error = -ENOMEM; 1556*237fead6SMichael Halcrow goto out; 1557*237fead6SMichael Halcrow } 1558*237fead6SMichael Halcrow /* TODO: Filename encryption is a scheduled feature for a 1559*237fead6SMichael Halcrow * future version of eCryptfs. This function is here only for 1560*237fead6SMichael Halcrow * the purpose of providing a framework for other developers 1561*237fead6SMichael Halcrow * to easily implement filename encryption. Hint: Replace this 1562*237fead6SMichael Halcrow * memcpy() with a call to decode and decrypt the 1563*237fead6SMichael Halcrow * filename, the set the length accordingly. */ 1564*237fead6SMichael Halcrow memcpy((void *)(*decrypted_name), (void *)name, length); 1565*237fead6SMichael Halcrow (*decrypted_name)[length + 1] = '\0'; /* Only for convenience 1566*237fead6SMichael Halcrow * in printing out the 1567*237fead6SMichael Halcrow * string in debug 1568*237fead6SMichael Halcrow * messages */ 1569*237fead6SMichael Halcrow error = length; 1570*237fead6SMichael Halcrow out: 1571*237fead6SMichael Halcrow return error; 1572*237fead6SMichael Halcrow } 1573*237fead6SMichael Halcrow 1574*237fead6SMichael Halcrow /** 1575*237fead6SMichael Halcrow * ecryptfs_process_cipher - Perform cipher initialization. 1576*237fead6SMichael Halcrow * @tfm: Crypto context set by this function 1577*237fead6SMichael Halcrow * @key_tfm: Crypto context for key material, set by this function 1578*237fead6SMichael Halcrow * @cipher_name: Name of the cipher. 1579*237fead6SMichael Halcrow * @key_size: Size of the key in bytes. 1580*237fead6SMichael Halcrow * 1581*237fead6SMichael Halcrow * Returns zero on success. Any crypto_tfm structs allocated here 1582*237fead6SMichael Halcrow * should be released by other functions, such as on a superblock put 1583*237fead6SMichael Halcrow * event, regardless of whether this function succeeds for fails. 1584*237fead6SMichael Halcrow */ 1585*237fead6SMichael Halcrow int 1586*237fead6SMichael Halcrow ecryptfs_process_cipher(struct crypto_tfm **tfm, struct crypto_tfm **key_tfm, 1587*237fead6SMichael Halcrow char *cipher_name, size_t key_size) 1588*237fead6SMichael Halcrow { 1589*237fead6SMichael Halcrow char dummy_key[ECRYPTFS_MAX_KEY_BYTES]; 1590*237fead6SMichael Halcrow int rc; 1591*237fead6SMichael Halcrow 1592*237fead6SMichael Halcrow *tfm = *key_tfm = NULL; 1593*237fead6SMichael Halcrow if (key_size > ECRYPTFS_MAX_KEY_BYTES) { 1594*237fead6SMichael Halcrow rc = -EINVAL; 1595*237fead6SMichael Halcrow printk(KERN_ERR "Requested key size is [%Zd] bytes; maximum " 1596*237fead6SMichael Halcrow "allowable is [%d]\n", key_size, ECRYPTFS_MAX_KEY_BYTES); 1597*237fead6SMichael Halcrow goto out; 1598*237fead6SMichael Halcrow } 1599*237fead6SMichael Halcrow *tfm = crypto_alloc_tfm(cipher_name, (ECRYPTFS_DEFAULT_CHAINING_MODE 1600*237fead6SMichael Halcrow | CRYPTO_TFM_REQ_WEAK_KEY)); 1601*237fead6SMichael Halcrow if (!(*tfm)) { 1602*237fead6SMichael Halcrow rc = -EINVAL; 1603*237fead6SMichael Halcrow printk(KERN_ERR "Unable to allocate crypto cipher with name " 1604*237fead6SMichael Halcrow "[%s]\n", cipher_name); 1605*237fead6SMichael Halcrow goto out; 1606*237fead6SMichael Halcrow } 1607*237fead6SMichael Halcrow *key_tfm = crypto_alloc_tfm(cipher_name, CRYPTO_TFM_REQ_WEAK_KEY); 1608*237fead6SMichael Halcrow if (!(*key_tfm)) { 1609*237fead6SMichael Halcrow rc = -EINVAL; 1610*237fead6SMichael Halcrow printk(KERN_ERR "Unable to allocate crypto cipher with name " 1611*237fead6SMichael Halcrow "[%s]\n", cipher_name); 1612*237fead6SMichael Halcrow goto out; 1613*237fead6SMichael Halcrow } 1614*237fead6SMichael Halcrow if (key_size < crypto_tfm_alg_min_keysize(*tfm)) { 1615*237fead6SMichael Halcrow rc = -EINVAL; 1616*237fead6SMichael Halcrow printk(KERN_ERR "Request key size is [%Zd]; minimum key size " 1617*237fead6SMichael Halcrow "supported by cipher [%s] is [%d]\n", key_size, 1618*237fead6SMichael Halcrow cipher_name, crypto_tfm_alg_min_keysize(*tfm)); 1619*237fead6SMichael Halcrow goto out; 1620*237fead6SMichael Halcrow } 1621*237fead6SMichael Halcrow if (key_size < crypto_tfm_alg_min_keysize(*key_tfm)) { 1622*237fead6SMichael Halcrow rc = -EINVAL; 1623*237fead6SMichael Halcrow printk(KERN_ERR "Request key size is [%Zd]; minimum key size " 1624*237fead6SMichael Halcrow "supported by cipher [%s] is [%d]\n", key_size, 1625*237fead6SMichael Halcrow cipher_name, crypto_tfm_alg_min_keysize(*key_tfm)); 1626*237fead6SMichael Halcrow goto out; 1627*237fead6SMichael Halcrow } 1628*237fead6SMichael Halcrow if (key_size > crypto_tfm_alg_max_keysize(*tfm)) { 1629*237fead6SMichael Halcrow rc = -EINVAL; 1630*237fead6SMichael Halcrow printk(KERN_ERR "Request key size is [%Zd]; maximum key size " 1631*237fead6SMichael Halcrow "supported by cipher [%s] is [%d]\n", key_size, 1632*237fead6SMichael Halcrow cipher_name, crypto_tfm_alg_min_keysize(*tfm)); 1633*237fead6SMichael Halcrow goto out; 1634*237fead6SMichael Halcrow } 1635*237fead6SMichael Halcrow if (key_size > crypto_tfm_alg_max_keysize(*key_tfm)) { 1636*237fead6SMichael Halcrow rc = -EINVAL; 1637*237fead6SMichael Halcrow printk(KERN_ERR "Request key size is [%Zd]; maximum key size " 1638*237fead6SMichael Halcrow "supported by cipher [%s] is [%d]\n", key_size, 1639*237fead6SMichael Halcrow cipher_name, crypto_tfm_alg_min_keysize(*key_tfm)); 1640*237fead6SMichael Halcrow goto out; 1641*237fead6SMichael Halcrow } 1642*237fead6SMichael Halcrow get_random_bytes(dummy_key, key_size); 1643*237fead6SMichael Halcrow rc = crypto_cipher_setkey(*tfm, dummy_key, key_size); 1644*237fead6SMichael Halcrow if (rc) { 1645*237fead6SMichael Halcrow printk(KERN_ERR "Error attempting to set key of size [%Zd] for " 1646*237fead6SMichael Halcrow "cipher [%s]; rc = [%d]\n", key_size, cipher_name, rc); 1647*237fead6SMichael Halcrow rc = -EINVAL; 1648*237fead6SMichael Halcrow goto out; 1649*237fead6SMichael Halcrow } 1650*237fead6SMichael Halcrow rc = crypto_cipher_setkey(*key_tfm, dummy_key, key_size); 1651*237fead6SMichael Halcrow if (rc) { 1652*237fead6SMichael Halcrow printk(KERN_ERR "Error attempting to set key of size [%Zd] for " 1653*237fead6SMichael Halcrow "cipher [%s]; rc = [%d]\n", key_size, cipher_name, rc); 1654*237fead6SMichael Halcrow rc = -EINVAL; 1655*237fead6SMichael Halcrow goto out; 1656*237fead6SMichael Halcrow } 1657*237fead6SMichael Halcrow out: 1658*237fead6SMichael Halcrow return rc; 1659*237fead6SMichael Halcrow } 1660