Michael Halcrow | 237fead | 2006-10-04 02:16:22 -0700 | [diff] [blame] | 1 | /** |
| 2 | * eCryptfs: Linux filesystem encryption layer |
| 3 | * |
| 4 | * Copyright (C) 1997-2004 Erez Zadok |
| 5 | * Copyright (C) 2001-2004 Stony Brook University |
Michael Halcrow | dd2a3b7 | 2007-02-12 00:53:46 -0800 | [diff] [blame] | 6 | * Copyright (C) 2004-2007 International Business Machines Corp. |
Michael Halcrow | 237fead | 2006-10-04 02:16:22 -0700 | [diff] [blame] | 7 | * Author(s): Michael A. Halcrow <mahalcro@us.ibm.com> |
| 8 | * Michael C. Thompson <mcthomps@us.ibm.com> |
| 9 | * |
| 10 | * This program is free software; you can redistribute it and/or |
| 11 | * modify it under the terms of the GNU General Public License as |
| 12 | * published by the Free Software Foundation; either version 2 of the |
| 13 | * License, or (at your option) any later version. |
| 14 | * |
| 15 | * This program is distributed in the hope that it will be useful, but |
| 16 | * WITHOUT ANY WARRANTY; without even the implied warranty of |
| 17 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
| 18 | * General Public License for more details. |
| 19 | * |
| 20 | * You should have received a copy of the GNU General Public License |
| 21 | * along with this program; if not, write to the Free Software |
| 22 | * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA |
| 23 | * 02111-1307, USA. |
| 24 | */ |
| 25 | |
| 26 | #include <linux/fs.h> |
| 27 | #include <linux/mount.h> |
| 28 | #include <linux/pagemap.h> |
| 29 | #include <linux/random.h> |
| 30 | #include <linux/compiler.h> |
| 31 | #include <linux/key.h> |
| 32 | #include <linux/namei.h> |
| 33 | #include <linux/crypto.h> |
| 34 | #include <linux/file.h> |
| 35 | #include <linux/scatterlist.h> |
| 36 | #include "ecryptfs_kernel.h" |
| 37 | |
| 38 | static int |
| 39 | ecryptfs_decrypt_page_offset(struct ecryptfs_crypt_stat *crypt_stat, |
| 40 | struct page *dst_page, int dst_offset, |
| 41 | struct page *src_page, int src_offset, int size, |
| 42 | unsigned char *iv); |
| 43 | static int |
| 44 | ecryptfs_encrypt_page_offset(struct ecryptfs_crypt_stat *crypt_stat, |
| 45 | struct page *dst_page, int dst_offset, |
| 46 | struct page *src_page, int src_offset, int size, |
| 47 | unsigned char *iv); |
| 48 | |
| 49 | /** |
| 50 | * ecryptfs_to_hex |
| 51 | * @dst: Buffer to take hex character representation of contents of |
| 52 | * src; must be at least of size (src_size * 2) |
| 53 | * @src: Buffer to be converted to a hex string respresentation |
| 54 | * @src_size: number of bytes to convert |
| 55 | */ |
| 56 | void ecryptfs_to_hex(char *dst, char *src, size_t src_size) |
| 57 | { |
| 58 | int x; |
| 59 | |
| 60 | for (x = 0; x < src_size; x++) |
| 61 | sprintf(&dst[x * 2], "%.2x", (unsigned char)src[x]); |
| 62 | } |
| 63 | |
| 64 | /** |
| 65 | * ecryptfs_from_hex |
| 66 | * @dst: Buffer to take the bytes from src hex; must be at least of |
| 67 | * size (src_size / 2) |
| 68 | * @src: Buffer to be converted from a hex string respresentation to raw value |
| 69 | * @dst_size: size of dst buffer, or number of hex characters pairs to convert |
| 70 | */ |
| 71 | void ecryptfs_from_hex(char *dst, char *src, int dst_size) |
| 72 | { |
| 73 | int x; |
| 74 | char tmp[3] = { 0, }; |
| 75 | |
| 76 | for (x = 0; x < dst_size; x++) { |
| 77 | tmp[0] = src[x * 2]; |
| 78 | tmp[1] = src[x * 2 + 1]; |
| 79 | dst[x] = (unsigned char)simple_strtol(tmp, NULL, 16); |
| 80 | } |
| 81 | } |
| 82 | |
| 83 | /** |
| 84 | * ecryptfs_calculate_md5 - calculates the md5 of @src |
| 85 | * @dst: Pointer to 16 bytes of allocated memory |
| 86 | * @crypt_stat: Pointer to crypt_stat struct for the current inode |
| 87 | * @src: Data to be md5'd |
| 88 | * @len: Length of @src |
| 89 | * |
| 90 | * Uses the allocated crypto context that crypt_stat references to |
| 91 | * generate the MD5 sum of the contents of src. |
| 92 | */ |
| 93 | static int ecryptfs_calculate_md5(char *dst, |
| 94 | struct ecryptfs_crypt_stat *crypt_stat, |
| 95 | char *src, int len) |
| 96 | { |
Michael Halcrow | 237fead | 2006-10-04 02:16:22 -0700 | [diff] [blame] | 97 | struct scatterlist sg; |
Michael Halcrow | 565d972 | 2006-10-30 22:07:17 -0800 | [diff] [blame] | 98 | struct hash_desc desc = { |
| 99 | .tfm = crypt_stat->hash_tfm, |
| 100 | .flags = CRYPTO_TFM_REQ_MAY_SLEEP |
| 101 | }; |
| 102 | int rc = 0; |
Michael Halcrow | 237fead | 2006-10-04 02:16:22 -0700 | [diff] [blame] | 103 | |
Michael Halcrow | 565d972 | 2006-10-30 22:07:17 -0800 | [diff] [blame] | 104 | mutex_lock(&crypt_stat->cs_hash_tfm_mutex); |
Michael Halcrow | 237fead | 2006-10-04 02:16:22 -0700 | [diff] [blame] | 105 | sg_init_one(&sg, (u8 *)src, len); |
Michael Halcrow | 565d972 | 2006-10-30 22:07:17 -0800 | [diff] [blame] | 106 | if (!desc.tfm) { |
| 107 | desc.tfm = crypto_alloc_hash(ECRYPTFS_DEFAULT_HASH, 0, |
| 108 | CRYPTO_ALG_ASYNC); |
| 109 | if (IS_ERR(desc.tfm)) { |
| 110 | rc = PTR_ERR(desc.tfm); |
Michael Halcrow | 237fead | 2006-10-04 02:16:22 -0700 | [diff] [blame] | 111 | ecryptfs_printk(KERN_ERR, "Error attempting to " |
Michael Halcrow | 565d972 | 2006-10-30 22:07:17 -0800 | [diff] [blame] | 112 | "allocate crypto context; rc = [%d]\n", |
| 113 | rc); |
Michael Halcrow | 237fead | 2006-10-04 02:16:22 -0700 | [diff] [blame] | 114 | goto out; |
| 115 | } |
Michael Halcrow | 565d972 | 2006-10-30 22:07:17 -0800 | [diff] [blame] | 116 | crypt_stat->hash_tfm = desc.tfm; |
Michael Halcrow | 237fead | 2006-10-04 02:16:22 -0700 | [diff] [blame] | 117 | } |
Michael Halcrow | 565d972 | 2006-10-30 22:07:17 -0800 | [diff] [blame] | 118 | crypto_hash_init(&desc); |
| 119 | crypto_hash_update(&desc, &sg, len); |
| 120 | crypto_hash_final(&desc, dst); |
| 121 | mutex_unlock(&crypt_stat->cs_hash_tfm_mutex); |
Michael Halcrow | 237fead | 2006-10-04 02:16:22 -0700 | [diff] [blame] | 122 | out: |
| 123 | return rc; |
| 124 | } |
| 125 | |
Michael Halcrow | cd9d67d | 2007-10-16 01:28:04 -0700 | [diff] [blame] | 126 | static int ecryptfs_crypto_api_algify_cipher_name(char **algified_name, |
| 127 | char *cipher_name, |
| 128 | char *chaining_modifier) |
Michael Halcrow | 8bba066 | 2006-10-30 22:07:18 -0800 | [diff] [blame] | 129 | { |
| 130 | int cipher_name_len = strlen(cipher_name); |
| 131 | int chaining_modifier_len = strlen(chaining_modifier); |
| 132 | int algified_name_len; |
| 133 | int rc; |
| 134 | |
| 135 | algified_name_len = (chaining_modifier_len + cipher_name_len + 3); |
| 136 | (*algified_name) = kmalloc(algified_name_len, GFP_KERNEL); |
Michael Halcrow | 7bd473f | 2006-11-02 22:06:56 -0800 | [diff] [blame] | 137 | if (!(*algified_name)) { |
Michael Halcrow | 8bba066 | 2006-10-30 22:07:18 -0800 | [diff] [blame] | 138 | rc = -ENOMEM; |
| 139 | goto out; |
| 140 | } |
| 141 | snprintf((*algified_name), algified_name_len, "%s(%s)", |
| 142 | chaining_modifier, cipher_name); |
| 143 | rc = 0; |
| 144 | out: |
| 145 | return rc; |
| 146 | } |
| 147 | |
Michael Halcrow | 237fead | 2006-10-04 02:16:22 -0700 | [diff] [blame] | 148 | /** |
| 149 | * ecryptfs_derive_iv |
| 150 | * @iv: destination for the derived iv vale |
| 151 | * @crypt_stat: Pointer to crypt_stat struct for the current inode |
| 152 | * @offset: Offset of the page whose's iv we are to derive |
| 153 | * |
| 154 | * Generate the initialization vector from the given root IV and page |
| 155 | * offset. |
| 156 | * |
| 157 | * Returns zero on success; non-zero on error. |
| 158 | */ |
| 159 | static int ecryptfs_derive_iv(char *iv, struct ecryptfs_crypt_stat *crypt_stat, |
| 160 | pgoff_t offset) |
| 161 | { |
| 162 | int rc = 0; |
| 163 | char dst[MD5_DIGEST_SIZE]; |
| 164 | char src[ECRYPTFS_MAX_IV_BYTES + 16]; |
| 165 | |
| 166 | if (unlikely(ecryptfs_verbosity > 0)) { |
| 167 | ecryptfs_printk(KERN_DEBUG, "root iv:\n"); |
| 168 | ecryptfs_dump_hex(crypt_stat->root_iv, crypt_stat->iv_bytes); |
| 169 | } |
| 170 | /* TODO: It is probably secure to just cast the least |
| 171 | * significant bits of the root IV into an unsigned long and |
| 172 | * add the offset to that rather than go through all this |
| 173 | * hashing business. -Halcrow */ |
| 174 | memcpy(src, crypt_stat->root_iv, crypt_stat->iv_bytes); |
| 175 | memset((src + crypt_stat->iv_bytes), 0, 16); |
| 176 | snprintf((src + crypt_stat->iv_bytes), 16, "%ld", offset); |
| 177 | if (unlikely(ecryptfs_verbosity > 0)) { |
| 178 | ecryptfs_printk(KERN_DEBUG, "source:\n"); |
| 179 | ecryptfs_dump_hex(src, (crypt_stat->iv_bytes + 16)); |
| 180 | } |
| 181 | rc = ecryptfs_calculate_md5(dst, crypt_stat, src, |
| 182 | (crypt_stat->iv_bytes + 16)); |
| 183 | if (rc) { |
| 184 | ecryptfs_printk(KERN_WARNING, "Error attempting to compute " |
| 185 | "MD5 while generating IV for a page\n"); |
| 186 | goto out; |
| 187 | } |
| 188 | memcpy(iv, dst, crypt_stat->iv_bytes); |
| 189 | if (unlikely(ecryptfs_verbosity > 0)) { |
| 190 | ecryptfs_printk(KERN_DEBUG, "derived iv:\n"); |
| 191 | ecryptfs_dump_hex(iv, crypt_stat->iv_bytes); |
| 192 | } |
| 193 | out: |
| 194 | return rc; |
| 195 | } |
| 196 | |
| 197 | /** |
| 198 | * ecryptfs_init_crypt_stat |
| 199 | * @crypt_stat: Pointer to the crypt_stat struct to initialize. |
| 200 | * |
| 201 | * Initialize the crypt_stat structure. |
| 202 | */ |
| 203 | void |
| 204 | ecryptfs_init_crypt_stat(struct ecryptfs_crypt_stat *crypt_stat) |
| 205 | { |
| 206 | memset((void *)crypt_stat, 0, sizeof(struct ecryptfs_crypt_stat)); |
Michael Halcrow | f4aad16 | 2007-10-16 01:27:53 -0700 | [diff] [blame] | 207 | INIT_LIST_HEAD(&crypt_stat->keysig_list); |
| 208 | mutex_init(&crypt_stat->keysig_list_mutex); |
Michael Halcrow | 237fead | 2006-10-04 02:16:22 -0700 | [diff] [blame] | 209 | mutex_init(&crypt_stat->cs_mutex); |
| 210 | mutex_init(&crypt_stat->cs_tfm_mutex); |
Michael Halcrow | 565d972 | 2006-10-30 22:07:17 -0800 | [diff] [blame] | 211 | mutex_init(&crypt_stat->cs_hash_tfm_mutex); |
Michael Halcrow | e2bd99e | 2007-02-12 00:53:49 -0800 | [diff] [blame] | 212 | crypt_stat->flags |= ECRYPTFS_STRUCT_INITIALIZED; |
Michael Halcrow | 237fead | 2006-10-04 02:16:22 -0700 | [diff] [blame] | 213 | } |
| 214 | |
| 215 | /** |
Michael Halcrow | fcd1283 | 2007-10-16 01:28:01 -0700 | [diff] [blame] | 216 | * ecryptfs_destroy_crypt_stat |
Michael Halcrow | 237fead | 2006-10-04 02:16:22 -0700 | [diff] [blame] | 217 | * @crypt_stat: Pointer to the crypt_stat struct to initialize. |
| 218 | * |
| 219 | * Releases all memory associated with a crypt_stat struct. |
| 220 | */ |
Michael Halcrow | fcd1283 | 2007-10-16 01:28:01 -0700 | [diff] [blame] | 221 | void ecryptfs_destroy_crypt_stat(struct ecryptfs_crypt_stat *crypt_stat) |
Michael Halcrow | 237fead | 2006-10-04 02:16:22 -0700 | [diff] [blame] | 222 | { |
Michael Halcrow | f4aad16 | 2007-10-16 01:27:53 -0700 | [diff] [blame] | 223 | struct ecryptfs_key_sig *key_sig, *key_sig_tmp; |
| 224 | |
Michael Halcrow | 237fead | 2006-10-04 02:16:22 -0700 | [diff] [blame] | 225 | if (crypt_stat->tfm) |
Michael Halcrow | 8bba066 | 2006-10-30 22:07:18 -0800 | [diff] [blame] | 226 | crypto_free_blkcipher(crypt_stat->tfm); |
Michael Halcrow | 565d972 | 2006-10-30 22:07:17 -0800 | [diff] [blame] | 227 | if (crypt_stat->hash_tfm) |
| 228 | crypto_free_hash(crypt_stat->hash_tfm); |
Michael Halcrow | f4aad16 | 2007-10-16 01:27:53 -0700 | [diff] [blame] | 229 | mutex_lock(&crypt_stat->keysig_list_mutex); |
| 230 | list_for_each_entry_safe(key_sig, key_sig_tmp, |
| 231 | &crypt_stat->keysig_list, crypt_stat_list) { |
| 232 | list_del(&key_sig->crypt_stat_list); |
| 233 | kmem_cache_free(ecryptfs_key_sig_cache, key_sig); |
| 234 | } |
| 235 | mutex_unlock(&crypt_stat->keysig_list_mutex); |
Michael Halcrow | 237fead | 2006-10-04 02:16:22 -0700 | [diff] [blame] | 236 | memset(crypt_stat, 0, sizeof(struct ecryptfs_crypt_stat)); |
| 237 | } |
| 238 | |
Michael Halcrow | fcd1283 | 2007-10-16 01:28:01 -0700 | [diff] [blame] | 239 | void ecryptfs_destroy_mount_crypt_stat( |
Michael Halcrow | 237fead | 2006-10-04 02:16:22 -0700 | [diff] [blame] | 240 | struct ecryptfs_mount_crypt_stat *mount_crypt_stat) |
| 241 | { |
Michael Halcrow | f4aad16 | 2007-10-16 01:27:53 -0700 | [diff] [blame] | 242 | struct ecryptfs_global_auth_tok *auth_tok, *auth_tok_tmp; |
| 243 | |
| 244 | if (!(mount_crypt_stat->flags & ECRYPTFS_MOUNT_CRYPT_STAT_INITIALIZED)) |
| 245 | return; |
| 246 | mutex_lock(&mount_crypt_stat->global_auth_tok_list_mutex); |
| 247 | list_for_each_entry_safe(auth_tok, auth_tok_tmp, |
| 248 | &mount_crypt_stat->global_auth_tok_list, |
| 249 | mount_crypt_stat_list) { |
| 250 | list_del(&auth_tok->mount_crypt_stat_list); |
| 251 | mount_crypt_stat->num_global_auth_toks--; |
| 252 | if (auth_tok->global_auth_tok_key |
| 253 | && !(auth_tok->flags & ECRYPTFS_AUTH_TOK_INVALID)) |
| 254 | key_put(auth_tok->global_auth_tok_key); |
| 255 | kmem_cache_free(ecryptfs_global_auth_tok_cache, auth_tok); |
| 256 | } |
| 257 | mutex_unlock(&mount_crypt_stat->global_auth_tok_list_mutex); |
Michael Halcrow | 237fead | 2006-10-04 02:16:22 -0700 | [diff] [blame] | 258 | memset(mount_crypt_stat, 0, sizeof(struct ecryptfs_mount_crypt_stat)); |
| 259 | } |
| 260 | |
| 261 | /** |
| 262 | * virt_to_scatterlist |
| 263 | * @addr: Virtual address |
| 264 | * @size: Size of data; should be an even multiple of the block size |
| 265 | * @sg: Pointer to scatterlist array; set to NULL to obtain only |
| 266 | * the number of scatterlist structs required in array |
| 267 | * @sg_size: Max array size |
| 268 | * |
| 269 | * Fills in a scatterlist array with page references for a passed |
| 270 | * virtual address. |
| 271 | * |
| 272 | * Returns the number of scatterlist structs in array used |
| 273 | */ |
| 274 | int virt_to_scatterlist(const void *addr, int size, struct scatterlist *sg, |
| 275 | int sg_size) |
| 276 | { |
| 277 | int i = 0; |
| 278 | struct page *pg; |
| 279 | int offset; |
| 280 | int remainder_of_page; |
| 281 | |
| 282 | while (size > 0 && i < sg_size) { |
| 283 | pg = virt_to_page(addr); |
| 284 | offset = offset_in_page(addr); |
| 285 | if (sg) { |
| 286 | sg[i].page = pg; |
| 287 | sg[i].offset = offset; |
| 288 | } |
| 289 | remainder_of_page = PAGE_CACHE_SIZE - offset; |
| 290 | if (size >= remainder_of_page) { |
| 291 | if (sg) |
| 292 | sg[i].length = remainder_of_page; |
| 293 | addr += remainder_of_page; |
| 294 | size -= remainder_of_page; |
| 295 | } else { |
| 296 | if (sg) |
| 297 | sg[i].length = size; |
| 298 | addr += size; |
| 299 | size = 0; |
| 300 | } |
| 301 | i++; |
| 302 | } |
| 303 | if (size > 0) |
| 304 | return -ENOMEM; |
| 305 | return i; |
| 306 | } |
| 307 | |
| 308 | /** |
| 309 | * encrypt_scatterlist |
| 310 | * @crypt_stat: Pointer to the crypt_stat struct to initialize. |
| 311 | * @dest_sg: Destination of encrypted data |
| 312 | * @src_sg: Data to be encrypted |
| 313 | * @size: Length of data to be encrypted |
| 314 | * @iv: iv to use during encryption |
| 315 | * |
| 316 | * Returns the number of bytes encrypted; negative value on error |
| 317 | */ |
| 318 | static int encrypt_scatterlist(struct ecryptfs_crypt_stat *crypt_stat, |
| 319 | struct scatterlist *dest_sg, |
| 320 | struct scatterlist *src_sg, int size, |
| 321 | unsigned char *iv) |
| 322 | { |
Michael Halcrow | 8bba066 | 2006-10-30 22:07:18 -0800 | [diff] [blame] | 323 | struct blkcipher_desc desc = { |
| 324 | .tfm = crypt_stat->tfm, |
| 325 | .info = iv, |
| 326 | .flags = CRYPTO_TFM_REQ_MAY_SLEEP |
| 327 | }; |
Michael Halcrow | 237fead | 2006-10-04 02:16:22 -0700 | [diff] [blame] | 328 | int rc = 0; |
| 329 | |
| 330 | BUG_ON(!crypt_stat || !crypt_stat->tfm |
Michael Halcrow | e2bd99e | 2007-02-12 00:53:49 -0800 | [diff] [blame] | 331 | || !(crypt_stat->flags & ECRYPTFS_STRUCT_INITIALIZED)); |
Michael Halcrow | 237fead | 2006-10-04 02:16:22 -0700 | [diff] [blame] | 332 | if (unlikely(ecryptfs_verbosity > 0)) { |
| 333 | ecryptfs_printk(KERN_DEBUG, "Key size [%d]; key:\n", |
| 334 | crypt_stat->key_size); |
| 335 | ecryptfs_dump_hex(crypt_stat->key, |
| 336 | crypt_stat->key_size); |
| 337 | } |
| 338 | /* Consider doing this once, when the file is opened */ |
| 339 | mutex_lock(&crypt_stat->cs_tfm_mutex); |
Michael Halcrow | 8bba066 | 2006-10-30 22:07:18 -0800 | [diff] [blame] | 340 | rc = crypto_blkcipher_setkey(crypt_stat->tfm, crypt_stat->key, |
| 341 | crypt_stat->key_size); |
Michael Halcrow | 237fead | 2006-10-04 02:16:22 -0700 | [diff] [blame] | 342 | if (rc) { |
| 343 | ecryptfs_printk(KERN_ERR, "Error setting key; rc = [%d]\n", |
| 344 | rc); |
| 345 | mutex_unlock(&crypt_stat->cs_tfm_mutex); |
| 346 | rc = -EINVAL; |
| 347 | goto out; |
| 348 | } |
| 349 | ecryptfs_printk(KERN_DEBUG, "Encrypting [%d] bytes.\n", size); |
Michael Halcrow | 8bba066 | 2006-10-30 22:07:18 -0800 | [diff] [blame] | 350 | crypto_blkcipher_encrypt_iv(&desc, dest_sg, src_sg, size); |
Michael Halcrow | 237fead | 2006-10-04 02:16:22 -0700 | [diff] [blame] | 351 | mutex_unlock(&crypt_stat->cs_tfm_mutex); |
| 352 | out: |
| 353 | return rc; |
| 354 | } |
| 355 | |
| 356 | static void |
| 357 | ecryptfs_extent_to_lwr_pg_idx_and_offset(unsigned long *lower_page_idx, |
| 358 | int *byte_offset, |
| 359 | struct ecryptfs_crypt_stat *crypt_stat, |
| 360 | unsigned long extent_num) |
| 361 | { |
| 362 | unsigned long lower_extent_num; |
| 363 | int extents_occupied_by_headers_at_front; |
| 364 | int bytes_occupied_by_headers_at_front; |
| 365 | int extent_offset; |
| 366 | int extents_per_page; |
| 367 | |
| 368 | bytes_occupied_by_headers_at_front = |
Michael Halcrow | 45eaab7 | 2007-10-16 01:28:05 -0700 | [diff] [blame] | 369 | (crypt_stat->extent_size |
| 370 | * crypt_stat->num_header_extents_at_front); |
Michael Halcrow | 237fead | 2006-10-04 02:16:22 -0700 | [diff] [blame] | 371 | extents_occupied_by_headers_at_front = |
| 372 | ( bytes_occupied_by_headers_at_front |
| 373 | / crypt_stat->extent_size ); |
| 374 | lower_extent_num = extents_occupied_by_headers_at_front + extent_num; |
| 375 | extents_per_page = PAGE_CACHE_SIZE / crypt_stat->extent_size; |
| 376 | (*lower_page_idx) = lower_extent_num / extents_per_page; |
| 377 | extent_offset = lower_extent_num % extents_per_page; |
| 378 | (*byte_offset) = extent_offset * crypt_stat->extent_size; |
Michael Halcrow | 45eaab7 | 2007-10-16 01:28:05 -0700 | [diff] [blame] | 379 | ecryptfs_printk(KERN_DEBUG, " * crypt_stat->extent_size = " |
| 380 | "[%d]\n", crypt_stat->extent_size); |
Michael Halcrow | 237fead | 2006-10-04 02:16:22 -0700 | [diff] [blame] | 381 | ecryptfs_printk(KERN_DEBUG, " * crypt_stat->" |
| 382 | "num_header_extents_at_front = [%d]\n", |
| 383 | crypt_stat->num_header_extents_at_front); |
| 384 | ecryptfs_printk(KERN_DEBUG, " * extents_occupied_by_headers_at_" |
| 385 | "front = [%d]\n", extents_occupied_by_headers_at_front); |
| 386 | ecryptfs_printk(KERN_DEBUG, " * lower_extent_num = [0x%.16x]\n", |
| 387 | lower_extent_num); |
| 388 | ecryptfs_printk(KERN_DEBUG, " * extents_per_page = [%d]\n", |
| 389 | extents_per_page); |
| 390 | ecryptfs_printk(KERN_DEBUG, " * (*lower_page_idx) = [0x%.16x]\n", |
| 391 | (*lower_page_idx)); |
| 392 | ecryptfs_printk(KERN_DEBUG, " * extent_offset = [%d]\n", |
| 393 | extent_offset); |
| 394 | ecryptfs_printk(KERN_DEBUG, " * (*byte_offset) = [%d]\n", |
| 395 | (*byte_offset)); |
| 396 | } |
| 397 | |
| 398 | static int ecryptfs_write_out_page(struct ecryptfs_page_crypt_context *ctx, |
| 399 | struct page *lower_page, |
| 400 | struct inode *lower_inode, |
| 401 | int byte_offset_in_page, int bytes_to_write) |
| 402 | { |
| 403 | int rc = 0; |
| 404 | |
| 405 | if (ctx->mode == ECRYPTFS_PREPARE_COMMIT_MODE) { |
| 406 | rc = ecryptfs_commit_lower_page(lower_page, lower_inode, |
| 407 | ctx->param.lower_file, |
| 408 | byte_offset_in_page, |
| 409 | bytes_to_write); |
| 410 | if (rc) { |
| 411 | ecryptfs_printk(KERN_ERR, "Error calling lower " |
| 412 | "commit; rc = [%d]\n", rc); |
| 413 | goto out; |
| 414 | } |
| 415 | } else { |
| 416 | rc = ecryptfs_writepage_and_release_lower_page(lower_page, |
| 417 | lower_inode, |
| 418 | ctx->param.wbc); |
| 419 | if (rc) { |
| 420 | ecryptfs_printk(KERN_ERR, "Error calling lower " |
| 421 | "writepage(); rc = [%d]\n", rc); |
| 422 | goto out; |
| 423 | } |
| 424 | } |
| 425 | out: |
| 426 | return rc; |
| 427 | } |
| 428 | |
| 429 | static int ecryptfs_read_in_page(struct ecryptfs_page_crypt_context *ctx, |
| 430 | struct page **lower_page, |
| 431 | struct inode *lower_inode, |
| 432 | unsigned long lower_page_idx, |
| 433 | int byte_offset_in_page) |
| 434 | { |
| 435 | int rc = 0; |
| 436 | |
| 437 | if (ctx->mode == ECRYPTFS_PREPARE_COMMIT_MODE) { |
| 438 | /* TODO: Limit this to only the data extents that are |
| 439 | * needed */ |
| 440 | rc = ecryptfs_get_lower_page(lower_page, lower_inode, |
| 441 | ctx->param.lower_file, |
| 442 | lower_page_idx, |
| 443 | byte_offset_in_page, |
| 444 | (PAGE_CACHE_SIZE |
| 445 | - byte_offset_in_page)); |
| 446 | if (rc) { |
| 447 | ecryptfs_printk( |
| 448 | KERN_ERR, "Error attempting to grab, map, " |
| 449 | "and prepare_write lower page with index " |
| 450 | "[0x%.16x]; rc = [%d]\n", lower_page_idx, rc); |
| 451 | goto out; |
| 452 | } |
| 453 | } else { |
Michael Halcrow | 9d8b8ce | 2007-02-12 00:53:48 -0800 | [diff] [blame] | 454 | *lower_page = grab_cache_page(lower_inode->i_mapping, |
| 455 | lower_page_idx); |
| 456 | if (!(*lower_page)) { |
| 457 | rc = -EINVAL; |
Michael Halcrow | 237fead | 2006-10-04 02:16:22 -0700 | [diff] [blame] | 458 | ecryptfs_printk( |
| 459 | KERN_ERR, "Error attempting to grab and map " |
| 460 | "lower page with index [0x%.16x]; rc = [%d]\n", |
| 461 | lower_page_idx, rc); |
| 462 | goto out; |
| 463 | } |
| 464 | } |
| 465 | out: |
| 466 | return rc; |
| 467 | } |
| 468 | |
| 469 | /** |
Michael Halcrow | 0216f7f | 2007-10-16 01:28:08 -0700 | [diff] [blame^] | 470 | * ecryptfs_lower_offset_for_extent |
| 471 | * |
| 472 | * Convert an eCryptfs page index into a lower byte offset |
| 473 | */ |
| 474 | void ecryptfs_lower_offset_for_extent(loff_t *offset, loff_t extent_num, |
| 475 | struct ecryptfs_crypt_stat *crypt_stat) |
| 476 | { |
| 477 | (*offset) = ((crypt_stat->extent_size |
| 478 | * crypt_stat->num_header_extents_at_front) |
| 479 | + (crypt_stat->extent_size * extent_num)); |
| 480 | } |
| 481 | |
| 482 | /** |
| 483 | * ecryptfs_encrypt_extent |
| 484 | * @enc_extent_page: Allocated page into which to encrypt the data in |
| 485 | * @page |
| 486 | * @crypt_stat: crypt_stat containing cryptographic context for the |
| 487 | * encryption operation |
| 488 | * @page: Page containing plaintext data extent to encrypt |
| 489 | * @extent_offset: Page extent offset for use in generating IV |
| 490 | * |
| 491 | * Encrypts one extent of data. |
| 492 | * |
| 493 | * Return zero on success; non-zero otherwise |
| 494 | */ |
| 495 | static int ecryptfs_encrypt_extent(struct page *enc_extent_page, |
| 496 | struct ecryptfs_crypt_stat *crypt_stat, |
| 497 | struct page *page, |
| 498 | unsigned long extent_offset) |
| 499 | { |
| 500 | unsigned long extent_base; |
| 501 | char extent_iv[ECRYPTFS_MAX_IV_BYTES]; |
| 502 | int rc; |
| 503 | |
| 504 | extent_base = (page->index |
| 505 | * (PAGE_CACHE_SIZE / crypt_stat->extent_size)); |
| 506 | rc = ecryptfs_derive_iv(extent_iv, crypt_stat, |
| 507 | (extent_base + extent_offset)); |
| 508 | if (rc) { |
| 509 | ecryptfs_printk(KERN_ERR, "Error attempting to " |
| 510 | "derive IV for extent [0x%.16x]; " |
| 511 | "rc = [%d]\n", (extent_base + extent_offset), |
| 512 | rc); |
| 513 | goto out; |
| 514 | } |
| 515 | if (unlikely(ecryptfs_verbosity > 0)) { |
| 516 | ecryptfs_printk(KERN_DEBUG, "Encrypting extent " |
| 517 | "with iv:\n"); |
| 518 | ecryptfs_dump_hex(extent_iv, crypt_stat->iv_bytes); |
| 519 | ecryptfs_printk(KERN_DEBUG, "First 8 bytes before " |
| 520 | "encryption:\n"); |
| 521 | ecryptfs_dump_hex((char *) |
| 522 | (page_address(page) |
| 523 | + (extent_offset * crypt_stat->extent_size)), |
| 524 | 8); |
| 525 | } |
| 526 | rc = ecryptfs_encrypt_page_offset(crypt_stat, enc_extent_page, 0, |
| 527 | page, (extent_offset |
| 528 | * crypt_stat->extent_size), |
| 529 | crypt_stat->extent_size, extent_iv); |
| 530 | if (rc < 0) { |
| 531 | printk(KERN_ERR "%s: Error attempting to encrypt page with " |
| 532 | "page->index = [%ld], extent_offset = [%ld]; " |
| 533 | "rc = [%d]\n", __FUNCTION__, page->index, extent_offset, |
| 534 | rc); |
| 535 | goto out; |
| 536 | } |
| 537 | rc = 0; |
| 538 | if (unlikely(ecryptfs_verbosity > 0)) { |
| 539 | ecryptfs_printk(KERN_DEBUG, "Encrypt extent [0x%.16x]; " |
| 540 | "rc = [%d]\n", (extent_base + extent_offset), |
| 541 | rc); |
| 542 | ecryptfs_printk(KERN_DEBUG, "First 8 bytes after " |
| 543 | "encryption:\n"); |
| 544 | ecryptfs_dump_hex((char *)(page_address(enc_extent_page)), 8); |
| 545 | } |
| 546 | out: |
| 547 | return rc; |
| 548 | } |
| 549 | |
| 550 | /** |
Michael Halcrow | 237fead | 2006-10-04 02:16:22 -0700 | [diff] [blame] | 551 | * ecryptfs_encrypt_page |
Michael Halcrow | 0216f7f | 2007-10-16 01:28:08 -0700 | [diff] [blame^] | 552 | * @page: Page mapped from the eCryptfs inode for the file; contains |
| 553 | * decrypted content that needs to be encrypted (to a temporary |
| 554 | * page; not in place) and written out to the lower file |
Michael Halcrow | 237fead | 2006-10-04 02:16:22 -0700 | [diff] [blame] | 555 | * |
| 556 | * Encrypt an eCryptfs page. This is done on a per-extent basis. Note |
| 557 | * that eCryptfs pages may straddle the lower pages -- for instance, |
| 558 | * if the file was created on a machine with an 8K page size |
| 559 | * (resulting in an 8K header), and then the file is copied onto a |
| 560 | * host with a 32K page size, then when reading page 0 of the eCryptfs |
| 561 | * file, 24K of page 0 of the lower file will be read and decrypted, |
| 562 | * and then 8K of page 1 of the lower file will be read and decrypted. |
| 563 | * |
Michael Halcrow | 237fead | 2006-10-04 02:16:22 -0700 | [diff] [blame] | 564 | * Returns zero on success; negative on error |
| 565 | */ |
Michael Halcrow | 0216f7f | 2007-10-16 01:28:08 -0700 | [diff] [blame^] | 566 | int ecryptfs_encrypt_page(struct page *page) |
Michael Halcrow | 237fead | 2006-10-04 02:16:22 -0700 | [diff] [blame] | 567 | { |
Michael Halcrow | 0216f7f | 2007-10-16 01:28:08 -0700 | [diff] [blame^] | 568 | struct inode *ecryptfs_inode; |
Michael Halcrow | 237fead | 2006-10-04 02:16:22 -0700 | [diff] [blame] | 569 | struct ecryptfs_crypt_stat *crypt_stat; |
Michael Halcrow | 0216f7f | 2007-10-16 01:28:08 -0700 | [diff] [blame^] | 570 | char *enc_extent_virt = NULL; |
| 571 | struct page *enc_extent_page; |
| 572 | loff_t extent_offset; |
Michael Halcrow | 237fead | 2006-10-04 02:16:22 -0700 | [diff] [blame] | 573 | int rc = 0; |
Michael Halcrow | 237fead | 2006-10-04 02:16:22 -0700 | [diff] [blame] | 574 | |
Michael Halcrow | 0216f7f | 2007-10-16 01:28:08 -0700 | [diff] [blame^] | 575 | ecryptfs_inode = page->mapping->host; |
| 576 | crypt_stat = |
| 577 | &(ecryptfs_inode_to_private(ecryptfs_inode)->crypt_stat); |
Michael Halcrow | e2bd99e | 2007-02-12 00:53:49 -0800 | [diff] [blame] | 578 | if (!(crypt_stat->flags & ECRYPTFS_ENCRYPTED)) { |
Michael Halcrow | 0216f7f | 2007-10-16 01:28:08 -0700 | [diff] [blame^] | 579 | rc = ecryptfs_write_lower_page_segment(ecryptfs_inode, page, |
| 580 | 0, PAGE_CACHE_SIZE); |
Michael Halcrow | 237fead | 2006-10-04 02:16:22 -0700 | [diff] [blame] | 581 | if (rc) |
Michael Halcrow | 0216f7f | 2007-10-16 01:28:08 -0700 | [diff] [blame^] | 582 | printk(KERN_ERR "%s: Error attempting to copy " |
| 583 | "page at index [%ld]\n", __FUNCTION__, |
| 584 | page->index); |
Michael Halcrow | 237fead | 2006-10-04 02:16:22 -0700 | [diff] [blame] | 585 | goto out; |
| 586 | } |
Michael Halcrow | 0216f7f | 2007-10-16 01:28:08 -0700 | [diff] [blame^] | 587 | enc_extent_virt = kmalloc(PAGE_CACHE_SIZE, GFP_USER); |
| 588 | if (!enc_extent_virt) { |
| 589 | rc = -ENOMEM; |
| 590 | ecryptfs_printk(KERN_ERR, "Error allocating memory for " |
| 591 | "encrypted extent\n"); |
| 592 | goto out; |
| 593 | } |
| 594 | enc_extent_page = virt_to_page(enc_extent_virt); |
| 595 | for (extent_offset = 0; |
| 596 | extent_offset < (PAGE_CACHE_SIZE / crypt_stat->extent_size); |
| 597 | extent_offset++) { |
| 598 | loff_t offset; |
| 599 | |
| 600 | rc = ecryptfs_encrypt_extent(enc_extent_page, crypt_stat, page, |
| 601 | extent_offset); |
Michael Halcrow | 237fead | 2006-10-04 02:16:22 -0700 | [diff] [blame] | 602 | if (rc) { |
Michael Halcrow | 0216f7f | 2007-10-16 01:28:08 -0700 | [diff] [blame^] | 603 | printk(KERN_ERR "%s: Error encrypting extent; " |
| 604 | "rc = [%d]\n", __FUNCTION__, rc); |
Michael Halcrow | 237fead | 2006-10-04 02:16:22 -0700 | [diff] [blame] | 605 | goto out; |
| 606 | } |
Michael Halcrow | 0216f7f | 2007-10-16 01:28:08 -0700 | [diff] [blame^] | 607 | ecryptfs_lower_offset_for_extent( |
| 608 | &offset, ((page->index * (PAGE_CACHE_SIZE |
| 609 | / crypt_stat->extent_size)) |
| 610 | + extent_offset), crypt_stat); |
| 611 | rc = ecryptfs_write_lower(ecryptfs_inode, enc_extent_virt, |
| 612 | offset, crypt_stat->extent_size); |
| 613 | if (rc) { |
| 614 | ecryptfs_printk(KERN_ERR, "Error attempting " |
| 615 | "to write lower page; rc = [%d]" |
| 616 | "\n", rc); |
| 617 | goto out; |
Michael Halcrow | 237fead | 2006-10-04 02:16:22 -0700 | [diff] [blame] | 618 | } |
Michael Halcrow | 237fead | 2006-10-04 02:16:22 -0700 | [diff] [blame] | 619 | extent_offset++; |
| 620 | } |
Michael Halcrow | 0216f7f | 2007-10-16 01:28:08 -0700 | [diff] [blame^] | 621 | out: |
| 622 | kfree(enc_extent_virt); |
| 623 | return rc; |
| 624 | } |
| 625 | |
| 626 | static int ecryptfs_decrypt_extent(struct page *page, |
| 627 | struct ecryptfs_crypt_stat *crypt_stat, |
| 628 | struct page *enc_extent_page, |
| 629 | unsigned long extent_offset) |
| 630 | { |
| 631 | unsigned long extent_base; |
| 632 | char extent_iv[ECRYPTFS_MAX_IV_BYTES]; |
| 633 | int rc; |
| 634 | |
| 635 | extent_base = (page->index |
| 636 | * (PAGE_CACHE_SIZE / crypt_stat->extent_size)); |
| 637 | rc = ecryptfs_derive_iv(extent_iv, crypt_stat, |
| 638 | (extent_base + extent_offset)); |
Michael Halcrow | 237fead | 2006-10-04 02:16:22 -0700 | [diff] [blame] | 639 | if (rc) { |
Michael Halcrow | 0216f7f | 2007-10-16 01:28:08 -0700 | [diff] [blame^] | 640 | ecryptfs_printk(KERN_ERR, "Error attempting to " |
| 641 | "derive IV for extent [0x%.16x]; " |
| 642 | "rc = [%d]\n", (extent_base + extent_offset), |
| 643 | rc); |
| 644 | goto out; |
| 645 | } |
| 646 | if (unlikely(ecryptfs_verbosity > 0)) { |
| 647 | ecryptfs_printk(KERN_DEBUG, "Decrypting extent " |
| 648 | "with iv:\n"); |
| 649 | ecryptfs_dump_hex(extent_iv, crypt_stat->iv_bytes); |
| 650 | ecryptfs_printk(KERN_DEBUG, "First 8 bytes before " |
| 651 | "decryption:\n"); |
| 652 | ecryptfs_dump_hex((char *) |
| 653 | (page_address(enc_extent_page) |
| 654 | + (extent_offset * crypt_stat->extent_size)), |
| 655 | 8); |
| 656 | } |
| 657 | rc = ecryptfs_decrypt_page_offset(crypt_stat, page, |
| 658 | (extent_offset |
| 659 | * crypt_stat->extent_size), |
| 660 | enc_extent_page, 0, |
| 661 | crypt_stat->extent_size, extent_iv); |
| 662 | if (rc < 0) { |
| 663 | printk(KERN_ERR "%s: Error attempting to decrypt to page with " |
| 664 | "page->index = [%ld], extent_offset = [%ld]; " |
| 665 | "rc = [%d]\n", __FUNCTION__, page->index, extent_offset, |
| 666 | rc); |
| 667 | goto out; |
| 668 | } |
| 669 | rc = 0; |
| 670 | if (unlikely(ecryptfs_verbosity > 0)) { |
| 671 | ecryptfs_printk(KERN_DEBUG, "Decrypt extent [0x%.16x]; " |
| 672 | "rc = [%d]\n", (extent_base + extent_offset), |
| 673 | rc); |
| 674 | ecryptfs_printk(KERN_DEBUG, "First 8 bytes after " |
| 675 | "decryption:\n"); |
| 676 | ecryptfs_dump_hex((char *)(page_address(page) |
| 677 | + (extent_offset |
| 678 | * crypt_stat->extent_size)), 8); |
Michael Halcrow | 237fead | 2006-10-04 02:16:22 -0700 | [diff] [blame] | 679 | } |
| 680 | out: |
| 681 | return rc; |
| 682 | } |
| 683 | |
| 684 | /** |
| 685 | * ecryptfs_decrypt_page |
Michael Halcrow | 0216f7f | 2007-10-16 01:28:08 -0700 | [diff] [blame^] | 686 | * @page: Page mapped from the eCryptfs inode for the file; data read |
| 687 | * and decrypted from the lower file will be written into this |
| 688 | * page |
Michael Halcrow | 237fead | 2006-10-04 02:16:22 -0700 | [diff] [blame] | 689 | * |
| 690 | * Decrypt an eCryptfs page. This is done on a per-extent basis. Note |
| 691 | * that eCryptfs pages may straddle the lower pages -- for instance, |
| 692 | * if the file was created on a machine with an 8K page size |
| 693 | * (resulting in an 8K header), and then the file is copied onto a |
| 694 | * host with a 32K page size, then when reading page 0 of the eCryptfs |
| 695 | * file, 24K of page 0 of the lower file will be read and decrypted, |
| 696 | * and then 8K of page 1 of the lower file will be read and decrypted. |
| 697 | * |
| 698 | * Returns zero on success; negative on error |
| 699 | */ |
Michael Halcrow | 0216f7f | 2007-10-16 01:28:08 -0700 | [diff] [blame^] | 700 | int ecryptfs_decrypt_page(struct page *page) |
Michael Halcrow | 237fead | 2006-10-04 02:16:22 -0700 | [diff] [blame] | 701 | { |
Michael Halcrow | 0216f7f | 2007-10-16 01:28:08 -0700 | [diff] [blame^] | 702 | struct inode *ecryptfs_inode; |
Michael Halcrow | 237fead | 2006-10-04 02:16:22 -0700 | [diff] [blame] | 703 | struct ecryptfs_crypt_stat *crypt_stat; |
Michael Halcrow | 0216f7f | 2007-10-16 01:28:08 -0700 | [diff] [blame^] | 704 | char *enc_extent_virt = NULL; |
| 705 | struct page *enc_extent_page; |
| 706 | unsigned long extent_offset; |
Michael Halcrow | 237fead | 2006-10-04 02:16:22 -0700 | [diff] [blame] | 707 | int rc = 0; |
Michael Halcrow | 237fead | 2006-10-04 02:16:22 -0700 | [diff] [blame] | 708 | |
Michael Halcrow | 0216f7f | 2007-10-16 01:28:08 -0700 | [diff] [blame^] | 709 | ecryptfs_inode = page->mapping->host; |
| 710 | crypt_stat = |
| 711 | &(ecryptfs_inode_to_private(ecryptfs_inode)->crypt_stat); |
Michael Halcrow | e2bd99e | 2007-02-12 00:53:49 -0800 | [diff] [blame] | 712 | if (!(crypt_stat->flags & ECRYPTFS_ENCRYPTED)) { |
Michael Halcrow | 0216f7f | 2007-10-16 01:28:08 -0700 | [diff] [blame^] | 713 | rc = ecryptfs_read_lower_page_segment(page, page->index, 0, |
| 714 | PAGE_CACHE_SIZE, |
| 715 | ecryptfs_inode); |
Michael Halcrow | 237fead | 2006-10-04 02:16:22 -0700 | [diff] [blame] | 716 | if (rc) |
Michael Halcrow | 0216f7f | 2007-10-16 01:28:08 -0700 | [diff] [blame^] | 717 | printk(KERN_ERR "%s: Error attempting to copy " |
| 718 | "page at index [%ld]\n", __FUNCTION__, |
| 719 | page->index); |
| 720 | goto out_clear_uptodate; |
Michael Halcrow | 237fead | 2006-10-04 02:16:22 -0700 | [diff] [blame] | 721 | } |
Michael Halcrow | 0216f7f | 2007-10-16 01:28:08 -0700 | [diff] [blame^] | 722 | enc_extent_virt = kmalloc(PAGE_CACHE_SIZE, GFP_USER); |
| 723 | if (!enc_extent_virt) { |
Michael Halcrow | 237fead | 2006-10-04 02:16:22 -0700 | [diff] [blame] | 724 | rc = -ENOMEM; |
Michael Halcrow | 0216f7f | 2007-10-16 01:28:08 -0700 | [diff] [blame^] | 725 | ecryptfs_printk(KERN_ERR, "Error allocating memory for " |
| 726 | "encrypted extent\n"); |
| 727 | goto out_clear_uptodate; |
Michael Halcrow | 237fead | 2006-10-04 02:16:22 -0700 | [diff] [blame] | 728 | } |
Michael Halcrow | 0216f7f | 2007-10-16 01:28:08 -0700 | [diff] [blame^] | 729 | enc_extent_page = virt_to_page(enc_extent_virt); |
| 730 | for (extent_offset = 0; |
| 731 | extent_offset < (PAGE_CACHE_SIZE / crypt_stat->extent_size); |
| 732 | extent_offset++) { |
| 733 | loff_t offset; |
| 734 | |
| 735 | ecryptfs_lower_offset_for_extent( |
| 736 | &offset, ((page->index * (PAGE_CACHE_SIZE |
| 737 | / crypt_stat->extent_size)) |
| 738 | + extent_offset), crypt_stat); |
| 739 | rc = ecryptfs_read_lower(enc_extent_virt, offset, |
| 740 | crypt_stat->extent_size, |
| 741 | ecryptfs_inode); |
Michael Halcrow | 237fead | 2006-10-04 02:16:22 -0700 | [diff] [blame] | 742 | if (rc) { |
Michael Halcrow | 0216f7f | 2007-10-16 01:28:08 -0700 | [diff] [blame^] | 743 | ecryptfs_printk(KERN_ERR, "Error attempting " |
| 744 | "to read lower page; rc = [%d]" |
| 745 | "\n", rc); |
| 746 | goto out_clear_uptodate; |
Michael Halcrow | 237fead | 2006-10-04 02:16:22 -0700 | [diff] [blame] | 747 | } |
Michael Halcrow | 0216f7f | 2007-10-16 01:28:08 -0700 | [diff] [blame^] | 748 | rc = ecryptfs_decrypt_extent(page, crypt_stat, enc_extent_page, |
| 749 | extent_offset); |
| 750 | if (rc) { |
| 751 | printk(KERN_ERR "%s: Error encrypting extent; " |
| 752 | "rc = [%d]\n", __FUNCTION__, rc); |
| 753 | goto out_clear_uptodate; |
Michael Halcrow | 237fead | 2006-10-04 02:16:22 -0700 | [diff] [blame] | 754 | } |
| 755 | extent_offset++; |
| 756 | } |
Michael Halcrow | 0216f7f | 2007-10-16 01:28:08 -0700 | [diff] [blame^] | 757 | SetPageUptodate(page); |
| 758 | goto out; |
| 759 | out_clear_uptodate: |
| 760 | ClearPageUptodate(page); |
Michael Halcrow | 237fead | 2006-10-04 02:16:22 -0700 | [diff] [blame] | 761 | out: |
Michael Halcrow | 0216f7f | 2007-10-16 01:28:08 -0700 | [diff] [blame^] | 762 | kfree(enc_extent_virt); |
Michael Halcrow | 237fead | 2006-10-04 02:16:22 -0700 | [diff] [blame] | 763 | return rc; |
| 764 | } |
| 765 | |
| 766 | /** |
| 767 | * decrypt_scatterlist |
Michael Halcrow | 22e78fa | 2007-10-16 01:28:02 -0700 | [diff] [blame] | 768 | * @crypt_stat: Cryptographic context |
| 769 | * @dest_sg: The destination scatterlist to decrypt into |
| 770 | * @src_sg: The source scatterlist to decrypt from |
| 771 | * @size: The number of bytes to decrypt |
| 772 | * @iv: The initialization vector to use for the decryption |
Michael Halcrow | 237fead | 2006-10-04 02:16:22 -0700 | [diff] [blame] | 773 | * |
| 774 | * Returns the number of bytes decrypted; negative value on error |
| 775 | */ |
| 776 | static int decrypt_scatterlist(struct ecryptfs_crypt_stat *crypt_stat, |
| 777 | struct scatterlist *dest_sg, |
| 778 | struct scatterlist *src_sg, int size, |
| 779 | unsigned char *iv) |
| 780 | { |
Michael Halcrow | 8bba066 | 2006-10-30 22:07:18 -0800 | [diff] [blame] | 781 | struct blkcipher_desc desc = { |
| 782 | .tfm = crypt_stat->tfm, |
| 783 | .info = iv, |
| 784 | .flags = CRYPTO_TFM_REQ_MAY_SLEEP |
| 785 | }; |
Michael Halcrow | 237fead | 2006-10-04 02:16:22 -0700 | [diff] [blame] | 786 | int rc = 0; |
| 787 | |
| 788 | /* Consider doing this once, when the file is opened */ |
| 789 | mutex_lock(&crypt_stat->cs_tfm_mutex); |
Michael Halcrow | 8bba066 | 2006-10-30 22:07:18 -0800 | [diff] [blame] | 790 | rc = crypto_blkcipher_setkey(crypt_stat->tfm, crypt_stat->key, |
| 791 | crypt_stat->key_size); |
Michael Halcrow | 237fead | 2006-10-04 02:16:22 -0700 | [diff] [blame] | 792 | if (rc) { |
| 793 | ecryptfs_printk(KERN_ERR, "Error setting key; rc = [%d]\n", |
| 794 | rc); |
| 795 | mutex_unlock(&crypt_stat->cs_tfm_mutex); |
| 796 | rc = -EINVAL; |
| 797 | goto out; |
| 798 | } |
| 799 | ecryptfs_printk(KERN_DEBUG, "Decrypting [%d] bytes.\n", size); |
Michael Halcrow | 8bba066 | 2006-10-30 22:07:18 -0800 | [diff] [blame] | 800 | rc = crypto_blkcipher_decrypt_iv(&desc, dest_sg, src_sg, size); |
Michael Halcrow | 237fead | 2006-10-04 02:16:22 -0700 | [diff] [blame] | 801 | mutex_unlock(&crypt_stat->cs_tfm_mutex); |
| 802 | if (rc) { |
| 803 | ecryptfs_printk(KERN_ERR, "Error decrypting; rc = [%d]\n", |
| 804 | rc); |
| 805 | goto out; |
| 806 | } |
| 807 | rc = size; |
| 808 | out: |
| 809 | return rc; |
| 810 | } |
| 811 | |
| 812 | /** |
| 813 | * ecryptfs_encrypt_page_offset |
Michael Halcrow | 22e78fa | 2007-10-16 01:28:02 -0700 | [diff] [blame] | 814 | * @crypt_stat: The cryptographic context |
| 815 | * @dst_page: The page to encrypt into |
| 816 | * @dst_offset: The offset in the page to encrypt into |
| 817 | * @src_page: The page to encrypt from |
| 818 | * @src_offset: The offset in the page to encrypt from |
| 819 | * @size: The number of bytes to encrypt |
| 820 | * @iv: The initialization vector to use for the encryption |
Michael Halcrow | 237fead | 2006-10-04 02:16:22 -0700 | [diff] [blame] | 821 | * |
| 822 | * Returns the number of bytes encrypted |
| 823 | */ |
| 824 | static int |
| 825 | ecryptfs_encrypt_page_offset(struct ecryptfs_crypt_stat *crypt_stat, |
| 826 | struct page *dst_page, int dst_offset, |
| 827 | struct page *src_page, int src_offset, int size, |
| 828 | unsigned char *iv) |
| 829 | { |
| 830 | struct scatterlist src_sg, dst_sg; |
| 831 | |
| 832 | src_sg.page = src_page; |
| 833 | src_sg.offset = src_offset; |
| 834 | src_sg.length = size; |
| 835 | dst_sg.page = dst_page; |
| 836 | dst_sg.offset = dst_offset; |
| 837 | dst_sg.length = size; |
| 838 | return encrypt_scatterlist(crypt_stat, &dst_sg, &src_sg, size, iv); |
| 839 | } |
| 840 | |
| 841 | /** |
| 842 | * ecryptfs_decrypt_page_offset |
Michael Halcrow | 22e78fa | 2007-10-16 01:28:02 -0700 | [diff] [blame] | 843 | * @crypt_stat: The cryptographic context |
| 844 | * @dst_page: The page to decrypt into |
| 845 | * @dst_offset: The offset in the page to decrypt into |
| 846 | * @src_page: The page to decrypt from |
| 847 | * @src_offset: The offset in the page to decrypt from |
| 848 | * @size: The number of bytes to decrypt |
| 849 | * @iv: The initialization vector to use for the decryption |
Michael Halcrow | 237fead | 2006-10-04 02:16:22 -0700 | [diff] [blame] | 850 | * |
| 851 | * Returns the number of bytes decrypted |
| 852 | */ |
| 853 | static int |
| 854 | ecryptfs_decrypt_page_offset(struct ecryptfs_crypt_stat *crypt_stat, |
| 855 | struct page *dst_page, int dst_offset, |
| 856 | struct page *src_page, int src_offset, int size, |
| 857 | unsigned char *iv) |
| 858 | { |
| 859 | struct scatterlist src_sg, dst_sg; |
| 860 | |
| 861 | src_sg.page = src_page; |
| 862 | src_sg.offset = src_offset; |
| 863 | src_sg.length = size; |
| 864 | dst_sg.page = dst_page; |
| 865 | dst_sg.offset = dst_offset; |
| 866 | dst_sg.length = size; |
| 867 | return decrypt_scatterlist(crypt_stat, &dst_sg, &src_sg, size, iv); |
| 868 | } |
| 869 | |
| 870 | #define ECRYPTFS_MAX_SCATTERLIST_LEN 4 |
| 871 | |
| 872 | /** |
| 873 | * ecryptfs_init_crypt_ctx |
| 874 | * @crypt_stat: Uninitilized crypt stats structure |
| 875 | * |
| 876 | * Initialize the crypto context. |
| 877 | * |
| 878 | * TODO: Performance: Keep a cache of initialized cipher contexts; |
| 879 | * only init if needed |
| 880 | */ |
| 881 | int ecryptfs_init_crypt_ctx(struct ecryptfs_crypt_stat *crypt_stat) |
| 882 | { |
Michael Halcrow | 8bba066 | 2006-10-30 22:07:18 -0800 | [diff] [blame] | 883 | char *full_alg_name; |
Michael Halcrow | 237fead | 2006-10-04 02:16:22 -0700 | [diff] [blame] | 884 | int rc = -EINVAL; |
| 885 | |
| 886 | if (!crypt_stat->cipher) { |
| 887 | ecryptfs_printk(KERN_ERR, "No cipher specified\n"); |
| 888 | goto out; |
| 889 | } |
| 890 | ecryptfs_printk(KERN_DEBUG, |
| 891 | "Initializing cipher [%s]; strlen = [%d]; " |
| 892 | "key_size_bits = [%d]\n", |
| 893 | crypt_stat->cipher, (int)strlen(crypt_stat->cipher), |
| 894 | crypt_stat->key_size << 3); |
| 895 | if (crypt_stat->tfm) { |
| 896 | rc = 0; |
| 897 | goto out; |
| 898 | } |
| 899 | mutex_lock(&crypt_stat->cs_tfm_mutex); |
Michael Halcrow | 8bba066 | 2006-10-30 22:07:18 -0800 | [diff] [blame] | 900 | rc = ecryptfs_crypto_api_algify_cipher_name(&full_alg_name, |
| 901 | crypt_stat->cipher, "cbc"); |
| 902 | if (rc) |
| 903 | goto out; |
| 904 | crypt_stat->tfm = crypto_alloc_blkcipher(full_alg_name, 0, |
| 905 | CRYPTO_ALG_ASYNC); |
| 906 | kfree(full_alg_name); |
Akinobu Mita | de88777 | 2006-11-28 12:29:49 -0800 | [diff] [blame] | 907 | if (IS_ERR(crypt_stat->tfm)) { |
| 908 | rc = PTR_ERR(crypt_stat->tfm); |
Michael Halcrow | 237fead | 2006-10-04 02:16:22 -0700 | [diff] [blame] | 909 | ecryptfs_printk(KERN_ERR, "cryptfs: init_crypt_ctx(): " |
| 910 | "Error initializing cipher [%s]\n", |
| 911 | crypt_stat->cipher); |
Michael Halcrow | 8bba066 | 2006-10-30 22:07:18 -0800 | [diff] [blame] | 912 | mutex_unlock(&crypt_stat->cs_tfm_mutex); |
Michael Halcrow | 237fead | 2006-10-04 02:16:22 -0700 | [diff] [blame] | 913 | goto out; |
| 914 | } |
Herbert Xu | f1ddcaf | 2007-01-27 10:05:15 +1100 | [diff] [blame] | 915 | crypto_blkcipher_set_flags(crypt_stat->tfm, CRYPTO_TFM_REQ_WEAK_KEY); |
Michael Halcrow | 8bba066 | 2006-10-30 22:07:18 -0800 | [diff] [blame] | 916 | mutex_unlock(&crypt_stat->cs_tfm_mutex); |
Michael Halcrow | 237fead | 2006-10-04 02:16:22 -0700 | [diff] [blame] | 917 | rc = 0; |
| 918 | out: |
| 919 | return rc; |
| 920 | } |
| 921 | |
| 922 | static void set_extent_mask_and_shift(struct ecryptfs_crypt_stat *crypt_stat) |
| 923 | { |
| 924 | int extent_size_tmp; |
| 925 | |
| 926 | crypt_stat->extent_mask = 0xFFFFFFFF; |
| 927 | crypt_stat->extent_shift = 0; |
| 928 | if (crypt_stat->extent_size == 0) |
| 929 | return; |
| 930 | extent_size_tmp = crypt_stat->extent_size; |
| 931 | while ((extent_size_tmp & 0x01) == 0) { |
| 932 | extent_size_tmp >>= 1; |
| 933 | crypt_stat->extent_mask <<= 1; |
| 934 | crypt_stat->extent_shift++; |
| 935 | } |
| 936 | } |
| 937 | |
| 938 | void ecryptfs_set_default_sizes(struct ecryptfs_crypt_stat *crypt_stat) |
| 939 | { |
| 940 | /* Default values; may be overwritten as we are parsing the |
| 941 | * packets. */ |
| 942 | crypt_stat->extent_size = ECRYPTFS_DEFAULT_EXTENT_SIZE; |
| 943 | set_extent_mask_and_shift(crypt_stat); |
| 944 | crypt_stat->iv_bytes = ECRYPTFS_DEFAULT_IV_BYTES; |
Michael Halcrow | dd2a3b7 | 2007-02-12 00:53:46 -0800 | [diff] [blame] | 945 | if (crypt_stat->flags & ECRYPTFS_METADATA_IN_XATTR) |
| 946 | crypt_stat->num_header_extents_at_front = 0; |
Michael Halcrow | 45eaab7 | 2007-10-16 01:28:05 -0700 | [diff] [blame] | 947 | else { |
| 948 | if (PAGE_CACHE_SIZE <= ECRYPTFS_MINIMUM_HEADER_EXTENT_SIZE) |
| 949 | crypt_stat->num_header_extents_at_front = |
| 950 | (ECRYPTFS_MINIMUM_HEADER_EXTENT_SIZE |
| 951 | / crypt_stat->extent_size); |
| 952 | else |
| 953 | crypt_stat->num_header_extents_at_front = |
| 954 | (PAGE_CACHE_SIZE / crypt_stat->extent_size); |
| 955 | } |
Michael Halcrow | 237fead | 2006-10-04 02:16:22 -0700 | [diff] [blame] | 956 | } |
| 957 | |
| 958 | /** |
| 959 | * ecryptfs_compute_root_iv |
| 960 | * @crypt_stats |
| 961 | * |
| 962 | * On error, sets the root IV to all 0's. |
| 963 | */ |
| 964 | int ecryptfs_compute_root_iv(struct ecryptfs_crypt_stat *crypt_stat) |
| 965 | { |
| 966 | int rc = 0; |
| 967 | char dst[MD5_DIGEST_SIZE]; |
| 968 | |
| 969 | BUG_ON(crypt_stat->iv_bytes > MD5_DIGEST_SIZE); |
| 970 | BUG_ON(crypt_stat->iv_bytes <= 0); |
Michael Halcrow | e2bd99e | 2007-02-12 00:53:49 -0800 | [diff] [blame] | 971 | if (!(crypt_stat->flags & ECRYPTFS_KEY_VALID)) { |
Michael Halcrow | 237fead | 2006-10-04 02:16:22 -0700 | [diff] [blame] | 972 | rc = -EINVAL; |
| 973 | ecryptfs_printk(KERN_WARNING, "Session key not valid; " |
| 974 | "cannot generate root IV\n"); |
| 975 | goto out; |
| 976 | } |
| 977 | rc = ecryptfs_calculate_md5(dst, crypt_stat, crypt_stat->key, |
| 978 | crypt_stat->key_size); |
| 979 | if (rc) { |
| 980 | ecryptfs_printk(KERN_WARNING, "Error attempting to compute " |
| 981 | "MD5 while generating root IV\n"); |
| 982 | goto out; |
| 983 | } |
| 984 | memcpy(crypt_stat->root_iv, dst, crypt_stat->iv_bytes); |
| 985 | out: |
| 986 | if (rc) { |
| 987 | memset(crypt_stat->root_iv, 0, crypt_stat->iv_bytes); |
Michael Halcrow | e2bd99e | 2007-02-12 00:53:49 -0800 | [diff] [blame] | 988 | crypt_stat->flags |= ECRYPTFS_SECURITY_WARNING; |
Michael Halcrow | 237fead | 2006-10-04 02:16:22 -0700 | [diff] [blame] | 989 | } |
| 990 | return rc; |
| 991 | } |
| 992 | |
| 993 | static void ecryptfs_generate_new_key(struct ecryptfs_crypt_stat *crypt_stat) |
| 994 | { |
| 995 | get_random_bytes(crypt_stat->key, crypt_stat->key_size); |
Michael Halcrow | e2bd99e | 2007-02-12 00:53:49 -0800 | [diff] [blame] | 996 | crypt_stat->flags |= ECRYPTFS_KEY_VALID; |
Michael Halcrow | 237fead | 2006-10-04 02:16:22 -0700 | [diff] [blame] | 997 | ecryptfs_compute_root_iv(crypt_stat); |
| 998 | if (unlikely(ecryptfs_verbosity > 0)) { |
| 999 | ecryptfs_printk(KERN_DEBUG, "Generated new session key:\n"); |
| 1000 | ecryptfs_dump_hex(crypt_stat->key, |
| 1001 | crypt_stat->key_size); |
| 1002 | } |
| 1003 | } |
| 1004 | |
| 1005 | /** |
Michael Halcrow | 1739895 | 2007-02-12 00:53:45 -0800 | [diff] [blame] | 1006 | * ecryptfs_copy_mount_wide_flags_to_inode_flags |
Michael Halcrow | 22e78fa | 2007-10-16 01:28:02 -0700 | [diff] [blame] | 1007 | * @crypt_stat: The inode's cryptographic context |
| 1008 | * @mount_crypt_stat: The mount point's cryptographic context |
Michael Halcrow | 1739895 | 2007-02-12 00:53:45 -0800 | [diff] [blame] | 1009 | * |
| 1010 | * This function propagates the mount-wide flags to individual inode |
| 1011 | * flags. |
| 1012 | */ |
| 1013 | static void ecryptfs_copy_mount_wide_flags_to_inode_flags( |
| 1014 | struct ecryptfs_crypt_stat *crypt_stat, |
| 1015 | struct ecryptfs_mount_crypt_stat *mount_crypt_stat) |
| 1016 | { |
| 1017 | if (mount_crypt_stat->flags & ECRYPTFS_XATTR_METADATA_ENABLED) |
| 1018 | crypt_stat->flags |= ECRYPTFS_METADATA_IN_XATTR; |
| 1019 | if (mount_crypt_stat->flags & ECRYPTFS_ENCRYPTED_VIEW_ENABLED) |
| 1020 | crypt_stat->flags |= ECRYPTFS_VIEW_AS_ENCRYPTED; |
| 1021 | } |
| 1022 | |
Michael Halcrow | f4aad16 | 2007-10-16 01:27:53 -0700 | [diff] [blame] | 1023 | static int ecryptfs_copy_mount_wide_sigs_to_inode_sigs( |
| 1024 | struct ecryptfs_crypt_stat *crypt_stat, |
| 1025 | struct ecryptfs_mount_crypt_stat *mount_crypt_stat) |
| 1026 | { |
| 1027 | struct ecryptfs_global_auth_tok *global_auth_tok; |
| 1028 | int rc = 0; |
| 1029 | |
| 1030 | mutex_lock(&mount_crypt_stat->global_auth_tok_list_mutex); |
| 1031 | list_for_each_entry(global_auth_tok, |
| 1032 | &mount_crypt_stat->global_auth_tok_list, |
| 1033 | mount_crypt_stat_list) { |
| 1034 | rc = ecryptfs_add_keysig(crypt_stat, global_auth_tok->sig); |
| 1035 | if (rc) { |
| 1036 | printk(KERN_ERR "Error adding keysig; rc = [%d]\n", rc); |
| 1037 | mutex_unlock( |
| 1038 | &mount_crypt_stat->global_auth_tok_list_mutex); |
| 1039 | goto out; |
| 1040 | } |
| 1041 | } |
| 1042 | mutex_unlock(&mount_crypt_stat->global_auth_tok_list_mutex); |
| 1043 | out: |
| 1044 | return rc; |
| 1045 | } |
| 1046 | |
Michael Halcrow | 1739895 | 2007-02-12 00:53:45 -0800 | [diff] [blame] | 1047 | /** |
Michael Halcrow | 237fead | 2006-10-04 02:16:22 -0700 | [diff] [blame] | 1048 | * ecryptfs_set_default_crypt_stat_vals |
Michael Halcrow | 22e78fa | 2007-10-16 01:28:02 -0700 | [diff] [blame] | 1049 | * @crypt_stat: The inode's cryptographic context |
| 1050 | * @mount_crypt_stat: The mount point's cryptographic context |
Michael Halcrow | 237fead | 2006-10-04 02:16:22 -0700 | [diff] [blame] | 1051 | * |
| 1052 | * Default values in the event that policy does not override them. |
| 1053 | */ |
| 1054 | static void ecryptfs_set_default_crypt_stat_vals( |
| 1055 | struct ecryptfs_crypt_stat *crypt_stat, |
| 1056 | struct ecryptfs_mount_crypt_stat *mount_crypt_stat) |
| 1057 | { |
Michael Halcrow | 1739895 | 2007-02-12 00:53:45 -0800 | [diff] [blame] | 1058 | ecryptfs_copy_mount_wide_flags_to_inode_flags(crypt_stat, |
| 1059 | mount_crypt_stat); |
Michael Halcrow | 237fead | 2006-10-04 02:16:22 -0700 | [diff] [blame] | 1060 | ecryptfs_set_default_sizes(crypt_stat); |
| 1061 | strcpy(crypt_stat->cipher, ECRYPTFS_DEFAULT_CIPHER); |
| 1062 | crypt_stat->key_size = ECRYPTFS_DEFAULT_KEY_BYTES; |
Michael Halcrow | e2bd99e | 2007-02-12 00:53:49 -0800 | [diff] [blame] | 1063 | crypt_stat->flags &= ~(ECRYPTFS_KEY_VALID); |
Michael Halcrow | 237fead | 2006-10-04 02:16:22 -0700 | [diff] [blame] | 1064 | crypt_stat->file_version = ECRYPTFS_FILE_VERSION; |
| 1065 | crypt_stat->mount_crypt_stat = mount_crypt_stat; |
| 1066 | } |
| 1067 | |
| 1068 | /** |
| 1069 | * ecryptfs_new_file_context |
Michael Halcrow | 22e78fa | 2007-10-16 01:28:02 -0700 | [diff] [blame] | 1070 | * @ecryptfs_dentry: The eCryptfs dentry |
Michael Halcrow | 237fead | 2006-10-04 02:16:22 -0700 | [diff] [blame] | 1071 | * |
| 1072 | * If the crypto context for the file has not yet been established, |
| 1073 | * this is where we do that. Establishing a new crypto context |
| 1074 | * involves the following decisions: |
| 1075 | * - What cipher to use? |
| 1076 | * - What set of authentication tokens to use? |
| 1077 | * Here we just worry about getting enough information into the |
| 1078 | * authentication tokens so that we know that they are available. |
| 1079 | * We associate the available authentication tokens with the new file |
| 1080 | * via the set of signatures in the crypt_stat struct. Later, when |
| 1081 | * the headers are actually written out, we may again defer to |
| 1082 | * userspace to perform the encryption of the session key; for the |
| 1083 | * foreseeable future, this will be the case with public key packets. |
| 1084 | * |
| 1085 | * Returns zero on success; non-zero otherwise |
| 1086 | */ |
Michael Halcrow | 237fead | 2006-10-04 02:16:22 -0700 | [diff] [blame] | 1087 | int ecryptfs_new_file_context(struct dentry *ecryptfs_dentry) |
| 1088 | { |
Michael Halcrow | 237fead | 2006-10-04 02:16:22 -0700 | [diff] [blame] | 1089 | struct ecryptfs_crypt_stat *crypt_stat = |
| 1090 | &ecryptfs_inode_to_private(ecryptfs_dentry->d_inode)->crypt_stat; |
| 1091 | struct ecryptfs_mount_crypt_stat *mount_crypt_stat = |
| 1092 | &ecryptfs_superblock_to_private( |
| 1093 | ecryptfs_dentry->d_sb)->mount_crypt_stat; |
| 1094 | int cipher_name_len; |
Michael Halcrow | f4aad16 | 2007-10-16 01:27:53 -0700 | [diff] [blame] | 1095 | int rc = 0; |
Michael Halcrow | 237fead | 2006-10-04 02:16:22 -0700 | [diff] [blame] | 1096 | |
| 1097 | ecryptfs_set_default_crypt_stat_vals(crypt_stat, mount_crypt_stat); |
Michael Halcrow | af655dc | 2007-10-16 01:28:00 -0700 | [diff] [blame] | 1098 | crypt_stat->flags |= (ECRYPTFS_ENCRYPTED | ECRYPTFS_KEY_VALID); |
Michael Halcrow | f4aad16 | 2007-10-16 01:27:53 -0700 | [diff] [blame] | 1099 | ecryptfs_copy_mount_wide_flags_to_inode_flags(crypt_stat, |
| 1100 | mount_crypt_stat); |
| 1101 | rc = ecryptfs_copy_mount_wide_sigs_to_inode_sigs(crypt_stat, |
| 1102 | mount_crypt_stat); |
| 1103 | if (rc) { |
| 1104 | printk(KERN_ERR "Error attempting to copy mount-wide key sigs " |
| 1105 | "to the inode key sigs; rc = [%d]\n", rc); |
| 1106 | goto out; |
| 1107 | } |
| 1108 | cipher_name_len = |
| 1109 | strlen(mount_crypt_stat->global_default_cipher_name); |
| 1110 | memcpy(crypt_stat->cipher, |
| 1111 | mount_crypt_stat->global_default_cipher_name, |
| 1112 | cipher_name_len); |
| 1113 | crypt_stat->cipher[cipher_name_len] = '\0'; |
| 1114 | crypt_stat->key_size = |
| 1115 | mount_crypt_stat->global_default_cipher_key_size; |
| 1116 | ecryptfs_generate_new_key(crypt_stat); |
Michael Halcrow | 237fead | 2006-10-04 02:16:22 -0700 | [diff] [blame] | 1117 | rc = ecryptfs_init_crypt_ctx(crypt_stat); |
| 1118 | if (rc) |
| 1119 | ecryptfs_printk(KERN_ERR, "Error initializing cryptographic " |
| 1120 | "context for cipher [%s]: rc = [%d]\n", |
| 1121 | crypt_stat->cipher, rc); |
Michael Halcrow | f4aad16 | 2007-10-16 01:27:53 -0700 | [diff] [blame] | 1122 | out: |
Michael Halcrow | 237fead | 2006-10-04 02:16:22 -0700 | [diff] [blame] | 1123 | return rc; |
| 1124 | } |
| 1125 | |
| 1126 | /** |
| 1127 | * contains_ecryptfs_marker - check for the ecryptfs marker |
| 1128 | * @data: The data block in which to check |
| 1129 | * |
| 1130 | * Returns one if marker found; zero if not found |
| 1131 | */ |
Michael Halcrow | dd2a3b7 | 2007-02-12 00:53:46 -0800 | [diff] [blame] | 1132 | static int contains_ecryptfs_marker(char *data) |
Michael Halcrow | 237fead | 2006-10-04 02:16:22 -0700 | [diff] [blame] | 1133 | { |
| 1134 | u32 m_1, m_2; |
| 1135 | |
| 1136 | memcpy(&m_1, data, 4); |
| 1137 | m_1 = be32_to_cpu(m_1); |
| 1138 | memcpy(&m_2, (data + 4), 4); |
| 1139 | m_2 = be32_to_cpu(m_2); |
| 1140 | if ((m_1 ^ MAGIC_ECRYPTFS_MARKER) == m_2) |
| 1141 | return 1; |
| 1142 | ecryptfs_printk(KERN_DEBUG, "m_1 = [0x%.8x]; m_2 = [0x%.8x]; " |
| 1143 | "MAGIC_ECRYPTFS_MARKER = [0x%.8x]\n", m_1, m_2, |
| 1144 | MAGIC_ECRYPTFS_MARKER); |
| 1145 | ecryptfs_printk(KERN_DEBUG, "(m_1 ^ MAGIC_ECRYPTFS_MARKER) = " |
| 1146 | "[0x%.8x]\n", (m_1 ^ MAGIC_ECRYPTFS_MARKER)); |
| 1147 | return 0; |
| 1148 | } |
| 1149 | |
| 1150 | struct ecryptfs_flag_map_elem { |
| 1151 | u32 file_flag; |
| 1152 | u32 local_flag; |
| 1153 | }; |
| 1154 | |
| 1155 | /* Add support for additional flags by adding elements here. */ |
| 1156 | static struct ecryptfs_flag_map_elem ecryptfs_flag_map[] = { |
| 1157 | {0x00000001, ECRYPTFS_ENABLE_HMAC}, |
Michael Halcrow | dd2a3b7 | 2007-02-12 00:53:46 -0800 | [diff] [blame] | 1158 | {0x00000002, ECRYPTFS_ENCRYPTED}, |
| 1159 | {0x00000004, ECRYPTFS_METADATA_IN_XATTR} |
Michael Halcrow | 237fead | 2006-10-04 02:16:22 -0700 | [diff] [blame] | 1160 | }; |
| 1161 | |
| 1162 | /** |
| 1163 | * ecryptfs_process_flags |
Michael Halcrow | 22e78fa | 2007-10-16 01:28:02 -0700 | [diff] [blame] | 1164 | * @crypt_stat: The cryptographic context |
Michael Halcrow | 237fead | 2006-10-04 02:16:22 -0700 | [diff] [blame] | 1165 | * @page_virt: Source data to be parsed |
| 1166 | * @bytes_read: Updated with the number of bytes read |
| 1167 | * |
| 1168 | * Returns zero on success; non-zero if the flag set is invalid |
| 1169 | */ |
| 1170 | static int ecryptfs_process_flags(struct ecryptfs_crypt_stat *crypt_stat, |
| 1171 | char *page_virt, int *bytes_read) |
| 1172 | { |
| 1173 | int rc = 0; |
| 1174 | int i; |
| 1175 | u32 flags; |
| 1176 | |
| 1177 | memcpy(&flags, page_virt, 4); |
| 1178 | flags = be32_to_cpu(flags); |
| 1179 | for (i = 0; i < ((sizeof(ecryptfs_flag_map) |
| 1180 | / sizeof(struct ecryptfs_flag_map_elem))); i++) |
| 1181 | if (flags & ecryptfs_flag_map[i].file_flag) { |
Michael Halcrow | e2bd99e | 2007-02-12 00:53:49 -0800 | [diff] [blame] | 1182 | crypt_stat->flags |= ecryptfs_flag_map[i].local_flag; |
Michael Halcrow | 237fead | 2006-10-04 02:16:22 -0700 | [diff] [blame] | 1183 | } else |
Michael Halcrow | e2bd99e | 2007-02-12 00:53:49 -0800 | [diff] [blame] | 1184 | crypt_stat->flags &= ~(ecryptfs_flag_map[i].local_flag); |
Michael Halcrow | 237fead | 2006-10-04 02:16:22 -0700 | [diff] [blame] | 1185 | /* Version is in top 8 bits of the 32-bit flag vector */ |
| 1186 | crypt_stat->file_version = ((flags >> 24) & 0xFF); |
| 1187 | (*bytes_read) = 4; |
| 1188 | return rc; |
| 1189 | } |
| 1190 | |
| 1191 | /** |
| 1192 | * write_ecryptfs_marker |
| 1193 | * @page_virt: The pointer to in a page to begin writing the marker |
| 1194 | * @written: Number of bytes written |
| 1195 | * |
| 1196 | * Marker = 0x3c81b7f5 |
| 1197 | */ |
| 1198 | static void write_ecryptfs_marker(char *page_virt, size_t *written) |
| 1199 | { |
| 1200 | u32 m_1, m_2; |
| 1201 | |
| 1202 | get_random_bytes(&m_1, (MAGIC_ECRYPTFS_MARKER_SIZE_BYTES / 2)); |
| 1203 | m_2 = (m_1 ^ MAGIC_ECRYPTFS_MARKER); |
| 1204 | m_1 = cpu_to_be32(m_1); |
| 1205 | memcpy(page_virt, &m_1, (MAGIC_ECRYPTFS_MARKER_SIZE_BYTES / 2)); |
| 1206 | m_2 = cpu_to_be32(m_2); |
| 1207 | memcpy(page_virt + (MAGIC_ECRYPTFS_MARKER_SIZE_BYTES / 2), &m_2, |
| 1208 | (MAGIC_ECRYPTFS_MARKER_SIZE_BYTES / 2)); |
| 1209 | (*written) = MAGIC_ECRYPTFS_MARKER_SIZE_BYTES; |
| 1210 | } |
| 1211 | |
| 1212 | static void |
| 1213 | write_ecryptfs_flags(char *page_virt, struct ecryptfs_crypt_stat *crypt_stat, |
| 1214 | size_t *written) |
| 1215 | { |
| 1216 | u32 flags = 0; |
| 1217 | int i; |
| 1218 | |
| 1219 | for (i = 0; i < ((sizeof(ecryptfs_flag_map) |
| 1220 | / sizeof(struct ecryptfs_flag_map_elem))); i++) |
Michael Halcrow | e2bd99e | 2007-02-12 00:53:49 -0800 | [diff] [blame] | 1221 | if (crypt_stat->flags & ecryptfs_flag_map[i].local_flag) |
Michael Halcrow | 237fead | 2006-10-04 02:16:22 -0700 | [diff] [blame] | 1222 | flags |= ecryptfs_flag_map[i].file_flag; |
| 1223 | /* Version is in top 8 bits of the 32-bit flag vector */ |
| 1224 | flags |= ((((u8)crypt_stat->file_version) << 24) & 0xFF000000); |
| 1225 | flags = cpu_to_be32(flags); |
| 1226 | memcpy(page_virt, &flags, 4); |
| 1227 | (*written) = 4; |
| 1228 | } |
| 1229 | |
| 1230 | struct ecryptfs_cipher_code_str_map_elem { |
| 1231 | char cipher_str[16]; |
| 1232 | u16 cipher_code; |
| 1233 | }; |
| 1234 | |
| 1235 | /* Add support for additional ciphers by adding elements here. The |
| 1236 | * cipher_code is whatever OpenPGP applicatoins use to identify the |
| 1237 | * ciphers. List in order of probability. */ |
| 1238 | static struct ecryptfs_cipher_code_str_map_elem |
| 1239 | ecryptfs_cipher_code_str_map[] = { |
| 1240 | {"aes",RFC2440_CIPHER_AES_128 }, |
| 1241 | {"blowfish", RFC2440_CIPHER_BLOWFISH}, |
| 1242 | {"des3_ede", RFC2440_CIPHER_DES3_EDE}, |
| 1243 | {"cast5", RFC2440_CIPHER_CAST_5}, |
| 1244 | {"twofish", RFC2440_CIPHER_TWOFISH}, |
| 1245 | {"cast6", RFC2440_CIPHER_CAST_6}, |
| 1246 | {"aes", RFC2440_CIPHER_AES_192}, |
| 1247 | {"aes", RFC2440_CIPHER_AES_256} |
| 1248 | }; |
| 1249 | |
| 1250 | /** |
| 1251 | * ecryptfs_code_for_cipher_string |
Michael Halcrow | 22e78fa | 2007-10-16 01:28:02 -0700 | [diff] [blame] | 1252 | * @crypt_stat: The cryptographic context |
Michael Halcrow | 237fead | 2006-10-04 02:16:22 -0700 | [diff] [blame] | 1253 | * |
| 1254 | * Returns zero on no match, or the cipher code on match |
| 1255 | */ |
| 1256 | u16 ecryptfs_code_for_cipher_string(struct ecryptfs_crypt_stat *crypt_stat) |
| 1257 | { |
| 1258 | int i; |
| 1259 | u16 code = 0; |
| 1260 | struct ecryptfs_cipher_code_str_map_elem *map = |
| 1261 | ecryptfs_cipher_code_str_map; |
| 1262 | |
| 1263 | if (strcmp(crypt_stat->cipher, "aes") == 0) { |
| 1264 | switch (crypt_stat->key_size) { |
| 1265 | case 16: |
| 1266 | code = RFC2440_CIPHER_AES_128; |
| 1267 | break; |
| 1268 | case 24: |
| 1269 | code = RFC2440_CIPHER_AES_192; |
| 1270 | break; |
| 1271 | case 32: |
| 1272 | code = RFC2440_CIPHER_AES_256; |
| 1273 | } |
| 1274 | } else { |
| 1275 | for (i = 0; i < ARRAY_SIZE(ecryptfs_cipher_code_str_map); i++) |
| 1276 | if (strcmp(crypt_stat->cipher, map[i].cipher_str) == 0){ |
| 1277 | code = map[i].cipher_code; |
| 1278 | break; |
| 1279 | } |
| 1280 | } |
| 1281 | return code; |
| 1282 | } |
| 1283 | |
| 1284 | /** |
| 1285 | * ecryptfs_cipher_code_to_string |
| 1286 | * @str: Destination to write out the cipher name |
| 1287 | * @cipher_code: The code to convert to cipher name string |
| 1288 | * |
| 1289 | * Returns zero on success |
| 1290 | */ |
| 1291 | int ecryptfs_cipher_code_to_string(char *str, u16 cipher_code) |
| 1292 | { |
| 1293 | int rc = 0; |
| 1294 | int i; |
| 1295 | |
| 1296 | str[0] = '\0'; |
| 1297 | for (i = 0; i < ARRAY_SIZE(ecryptfs_cipher_code_str_map); i++) |
| 1298 | if (cipher_code == ecryptfs_cipher_code_str_map[i].cipher_code) |
| 1299 | strcpy(str, ecryptfs_cipher_code_str_map[i].cipher_str); |
| 1300 | if (str[0] == '\0') { |
| 1301 | ecryptfs_printk(KERN_WARNING, "Cipher code not recognized: " |
| 1302 | "[%d]\n", cipher_code); |
| 1303 | rc = -EINVAL; |
| 1304 | } |
| 1305 | return rc; |
| 1306 | } |
| 1307 | |
| 1308 | /** |
| 1309 | * ecryptfs_read_header_region |
Michael Halcrow | 22e78fa | 2007-10-16 01:28:02 -0700 | [diff] [blame] | 1310 | * @data: The virtual address to write header region data into |
| 1311 | * @dentry: The lower dentry |
| 1312 | * @mnt: The lower VFS mount |
Michael Halcrow | 237fead | 2006-10-04 02:16:22 -0700 | [diff] [blame] | 1313 | * |
| 1314 | * Returns zero on success; non-zero otherwise |
| 1315 | */ |
Michael Halcrow | dd2a3b7 | 2007-02-12 00:53:46 -0800 | [diff] [blame] | 1316 | static int ecryptfs_read_header_region(char *data, struct dentry *dentry, |
| 1317 | struct vfsmount *mnt) |
Michael Halcrow | 237fead | 2006-10-04 02:16:22 -0700 | [diff] [blame] | 1318 | { |
Michael Halcrow | 7ff1d74 | 2006-10-30 22:07:19 -0800 | [diff] [blame] | 1319 | struct file *lower_file; |
Michael Halcrow | 237fead | 2006-10-04 02:16:22 -0700 | [diff] [blame] | 1320 | mm_segment_t oldfs; |
| 1321 | int rc; |
| 1322 | |
Michael Halcrow | 5dda699 | 2007-10-16 01:28:06 -0700 | [diff] [blame] | 1323 | rc = ecryptfs_open_lower_file(&lower_file, dentry, mnt, O_RDONLY); |
| 1324 | if (rc) { |
Michael Halcrow | 7ff1d74 | 2006-10-30 22:07:19 -0800 | [diff] [blame] | 1325 | printk(KERN_ERR |
| 1326 | "Error opening lower_file to read header region\n"); |
Michael Halcrow | 237fead | 2006-10-04 02:16:22 -0700 | [diff] [blame] | 1327 | goto out; |
| 1328 | } |
Michael Halcrow | 7ff1d74 | 2006-10-30 22:07:19 -0800 | [diff] [blame] | 1329 | lower_file->f_pos = 0; |
Michael Halcrow | 237fead | 2006-10-04 02:16:22 -0700 | [diff] [blame] | 1330 | oldfs = get_fs(); |
| 1331 | set_fs(get_ds()); |
Michael Halcrow | 7ff1d74 | 2006-10-30 22:07:19 -0800 | [diff] [blame] | 1332 | rc = lower_file->f_op->read(lower_file, (char __user *)data, |
| 1333 | ECRYPTFS_DEFAULT_EXTENT_SIZE, &lower_file->f_pos); |
Michael Halcrow | 237fead | 2006-10-04 02:16:22 -0700 | [diff] [blame] | 1334 | set_fs(oldfs); |
Michael Halcrow | 5dda699 | 2007-10-16 01:28:06 -0700 | [diff] [blame] | 1335 | rc = ecryptfs_close_lower_file(lower_file); |
| 1336 | if (rc) { |
Michael Halcrow | 7ff1d74 | 2006-10-30 22:07:19 -0800 | [diff] [blame] | 1337 | printk(KERN_ERR "Error closing lower_file\n"); |
| 1338 | goto out; |
| 1339 | } |
Michael Halcrow | 237fead | 2006-10-04 02:16:22 -0700 | [diff] [blame] | 1340 | rc = 0; |
| 1341 | out: |
| 1342 | return rc; |
| 1343 | } |
| 1344 | |
Michael Halcrow | dd2a3b7 | 2007-02-12 00:53:46 -0800 | [diff] [blame] | 1345 | int ecryptfs_read_and_validate_header_region(char *data, struct dentry *dentry, |
| 1346 | struct vfsmount *mnt) |
| 1347 | { |
| 1348 | int rc; |
| 1349 | |
| 1350 | rc = ecryptfs_read_header_region(data, dentry, mnt); |
| 1351 | if (rc) |
| 1352 | goto out; |
| 1353 | if (!contains_ecryptfs_marker(data + ECRYPTFS_FILE_SIZE_BYTES)) |
| 1354 | rc = -EINVAL; |
| 1355 | out: |
| 1356 | return rc; |
| 1357 | } |
| 1358 | |
| 1359 | |
Michael Halcrow | e77a56d | 2007-02-12 00:53:47 -0800 | [diff] [blame] | 1360 | void |
| 1361 | ecryptfs_write_header_metadata(char *virt, |
| 1362 | struct ecryptfs_crypt_stat *crypt_stat, |
| 1363 | size_t *written) |
Michael Halcrow | 237fead | 2006-10-04 02:16:22 -0700 | [diff] [blame] | 1364 | { |
| 1365 | u32 header_extent_size; |
| 1366 | u16 num_header_extents_at_front; |
| 1367 | |
Michael Halcrow | 45eaab7 | 2007-10-16 01:28:05 -0700 | [diff] [blame] | 1368 | header_extent_size = (u32)crypt_stat->extent_size; |
Michael Halcrow | 237fead | 2006-10-04 02:16:22 -0700 | [diff] [blame] | 1369 | num_header_extents_at_front = |
| 1370 | (u16)crypt_stat->num_header_extents_at_front; |
| 1371 | header_extent_size = cpu_to_be32(header_extent_size); |
| 1372 | memcpy(virt, &header_extent_size, 4); |
| 1373 | virt += 4; |
| 1374 | num_header_extents_at_front = cpu_to_be16(num_header_extents_at_front); |
| 1375 | memcpy(virt, &num_header_extents_at_front, 2); |
| 1376 | (*written) = 6; |
| 1377 | } |
| 1378 | |
| 1379 | struct kmem_cache *ecryptfs_header_cache_0; |
| 1380 | struct kmem_cache *ecryptfs_header_cache_1; |
| 1381 | struct kmem_cache *ecryptfs_header_cache_2; |
| 1382 | |
| 1383 | /** |
| 1384 | * ecryptfs_write_headers_virt |
Michael Halcrow | 22e78fa | 2007-10-16 01:28:02 -0700 | [diff] [blame] | 1385 | * @page_virt: The virtual address to write the headers to |
| 1386 | * @size: Set to the number of bytes written by this function |
| 1387 | * @crypt_stat: The cryptographic context |
| 1388 | * @ecryptfs_dentry: The eCryptfs dentry |
Michael Halcrow | 237fead | 2006-10-04 02:16:22 -0700 | [diff] [blame] | 1389 | * |
| 1390 | * Format version: 1 |
| 1391 | * |
| 1392 | * Header Extent: |
| 1393 | * Octets 0-7: Unencrypted file size (big-endian) |
| 1394 | * Octets 8-15: eCryptfs special marker |
| 1395 | * Octets 16-19: Flags |
| 1396 | * Octet 16: File format version number (between 0 and 255) |
| 1397 | * Octets 17-18: Reserved |
| 1398 | * Octet 19: Bit 1 (lsb): Reserved |
| 1399 | * Bit 2: Encrypted? |
| 1400 | * Bits 3-8: Reserved |
| 1401 | * Octets 20-23: Header extent size (big-endian) |
| 1402 | * Octets 24-25: Number of header extents at front of file |
| 1403 | * (big-endian) |
| 1404 | * Octet 26: Begin RFC 2440 authentication token packet set |
| 1405 | * Data Extent 0: |
| 1406 | * Lower data (CBC encrypted) |
| 1407 | * Data Extent 1: |
| 1408 | * Lower data (CBC encrypted) |
| 1409 | * ... |
| 1410 | * |
| 1411 | * Returns zero on success |
| 1412 | */ |
Michael Halcrow | dd2a3b7 | 2007-02-12 00:53:46 -0800 | [diff] [blame] | 1413 | static int ecryptfs_write_headers_virt(char *page_virt, size_t *size, |
| 1414 | struct ecryptfs_crypt_stat *crypt_stat, |
| 1415 | struct dentry *ecryptfs_dentry) |
Michael Halcrow | 237fead | 2006-10-04 02:16:22 -0700 | [diff] [blame] | 1416 | { |
| 1417 | int rc; |
| 1418 | size_t written; |
| 1419 | size_t offset; |
| 1420 | |
| 1421 | offset = ECRYPTFS_FILE_SIZE_BYTES; |
| 1422 | write_ecryptfs_marker((page_virt + offset), &written); |
| 1423 | offset += written; |
| 1424 | write_ecryptfs_flags((page_virt + offset), crypt_stat, &written); |
| 1425 | offset += written; |
Michael Halcrow | e77a56d | 2007-02-12 00:53:47 -0800 | [diff] [blame] | 1426 | ecryptfs_write_header_metadata((page_virt + offset), crypt_stat, |
| 1427 | &written); |
Michael Halcrow | 237fead | 2006-10-04 02:16:22 -0700 | [diff] [blame] | 1428 | offset += written; |
| 1429 | rc = ecryptfs_generate_key_packet_set((page_virt + offset), crypt_stat, |
| 1430 | ecryptfs_dentry, &written, |
| 1431 | PAGE_CACHE_SIZE - offset); |
| 1432 | if (rc) |
| 1433 | ecryptfs_printk(KERN_WARNING, "Error generating key packet " |
| 1434 | "set; rc = [%d]\n", rc); |
Michael Halcrow | dd2a3b7 | 2007-02-12 00:53:46 -0800 | [diff] [blame] | 1435 | if (size) { |
| 1436 | offset += written; |
| 1437 | *size = offset; |
| 1438 | } |
| 1439 | return rc; |
| 1440 | } |
| 1441 | |
Michael Halcrow | 22e78fa | 2007-10-16 01:28:02 -0700 | [diff] [blame] | 1442 | static int |
| 1443 | ecryptfs_write_metadata_to_contents(struct ecryptfs_crypt_stat *crypt_stat, |
| 1444 | struct file *lower_file, char *page_virt) |
Michael Halcrow | dd2a3b7 | 2007-02-12 00:53:46 -0800 | [diff] [blame] | 1445 | { |
| 1446 | mm_segment_t oldfs; |
| 1447 | int current_header_page; |
| 1448 | int header_pages; |
Michael Halcrow | 7045660 | 2007-02-12 00:53:48 -0800 | [diff] [blame] | 1449 | ssize_t size; |
| 1450 | int rc = 0; |
Michael Halcrow | dd2a3b7 | 2007-02-12 00:53:46 -0800 | [diff] [blame] | 1451 | |
| 1452 | lower_file->f_pos = 0; |
| 1453 | oldfs = get_fs(); |
| 1454 | set_fs(get_ds()); |
Michael Halcrow | 7045660 | 2007-02-12 00:53:48 -0800 | [diff] [blame] | 1455 | size = vfs_write(lower_file, (char __user *)page_virt, PAGE_CACHE_SIZE, |
| 1456 | &lower_file->f_pos); |
| 1457 | if (size < 0) { |
| 1458 | rc = (int)size; |
| 1459 | printk(KERN_ERR "Error attempting to write lower page; " |
| 1460 | "rc = [%d]\n", rc); |
| 1461 | set_fs(oldfs); |
| 1462 | goto out; |
| 1463 | } |
Michael Halcrow | 45eaab7 | 2007-10-16 01:28:05 -0700 | [diff] [blame] | 1464 | header_pages = ((crypt_stat->extent_size |
Michael Halcrow | dd2a3b7 | 2007-02-12 00:53:46 -0800 | [diff] [blame] | 1465 | * crypt_stat->num_header_extents_at_front) |
| 1466 | / PAGE_CACHE_SIZE); |
| 1467 | memset(page_virt, 0, PAGE_CACHE_SIZE); |
| 1468 | current_header_page = 1; |
| 1469 | while (current_header_page < header_pages) { |
Michael Halcrow | 7045660 | 2007-02-12 00:53:48 -0800 | [diff] [blame] | 1470 | size = vfs_write(lower_file, (char __user *)page_virt, |
| 1471 | PAGE_CACHE_SIZE, &lower_file->f_pos); |
| 1472 | if (size < 0) { |
| 1473 | rc = (int)size; |
| 1474 | printk(KERN_ERR "Error attempting to write lower page; " |
| 1475 | "rc = [%d]\n", rc); |
| 1476 | set_fs(oldfs); |
| 1477 | goto out; |
| 1478 | } |
Michael Halcrow | dd2a3b7 | 2007-02-12 00:53:46 -0800 | [diff] [blame] | 1479 | current_header_page++; |
| 1480 | } |
| 1481 | set_fs(oldfs); |
Michael Halcrow | 7045660 | 2007-02-12 00:53:48 -0800 | [diff] [blame] | 1482 | out: |
| 1483 | return rc; |
Michael Halcrow | dd2a3b7 | 2007-02-12 00:53:46 -0800 | [diff] [blame] | 1484 | } |
| 1485 | |
Michael Halcrow | 22e78fa | 2007-10-16 01:28:02 -0700 | [diff] [blame] | 1486 | static int |
| 1487 | ecryptfs_write_metadata_to_xattr(struct dentry *ecryptfs_dentry, |
| 1488 | struct ecryptfs_crypt_stat *crypt_stat, |
| 1489 | char *page_virt, size_t size) |
Michael Halcrow | dd2a3b7 | 2007-02-12 00:53:46 -0800 | [diff] [blame] | 1490 | { |
| 1491 | int rc; |
| 1492 | |
| 1493 | rc = ecryptfs_setxattr(ecryptfs_dentry, ECRYPTFS_XATTR_NAME, page_virt, |
| 1494 | size, 0); |
Michael Halcrow | 237fead | 2006-10-04 02:16:22 -0700 | [diff] [blame] | 1495 | return rc; |
| 1496 | } |
| 1497 | |
| 1498 | /** |
Michael Halcrow | dd2a3b7 | 2007-02-12 00:53:46 -0800 | [diff] [blame] | 1499 | * ecryptfs_write_metadata |
Michael Halcrow | 22e78fa | 2007-10-16 01:28:02 -0700 | [diff] [blame] | 1500 | * @ecryptfs_dentry: The eCryptfs dentry |
Michael Halcrow | 237fead | 2006-10-04 02:16:22 -0700 | [diff] [blame] | 1501 | * @lower_file: The lower file struct, which was returned from dentry_open |
| 1502 | * |
| 1503 | * Write the file headers out. This will likely involve a userspace |
| 1504 | * callout, in which the session key is encrypted with one or more |
| 1505 | * public keys and/or the passphrase necessary to do the encryption is |
| 1506 | * retrieved via a prompt. Exactly what happens at this point should |
| 1507 | * be policy-dependent. |
| 1508 | * |
| 1509 | * Returns zero on success; non-zero on error |
| 1510 | */ |
Michael Halcrow | dd2a3b7 | 2007-02-12 00:53:46 -0800 | [diff] [blame] | 1511 | int ecryptfs_write_metadata(struct dentry *ecryptfs_dentry, |
| 1512 | struct file *lower_file) |
Michael Halcrow | 237fead | 2006-10-04 02:16:22 -0700 | [diff] [blame] | 1513 | { |
Michael Halcrow | 237fead | 2006-10-04 02:16:22 -0700 | [diff] [blame] | 1514 | struct ecryptfs_crypt_stat *crypt_stat; |
| 1515 | char *page_virt; |
Michael Halcrow | dd2a3b7 | 2007-02-12 00:53:46 -0800 | [diff] [blame] | 1516 | size_t size; |
Michael Halcrow | 237fead | 2006-10-04 02:16:22 -0700 | [diff] [blame] | 1517 | int rc = 0; |
| 1518 | |
| 1519 | crypt_stat = &ecryptfs_inode_to_private( |
| 1520 | ecryptfs_dentry->d_inode)->crypt_stat; |
Michael Halcrow | e2bd99e | 2007-02-12 00:53:49 -0800 | [diff] [blame] | 1521 | if (likely(crypt_stat->flags & ECRYPTFS_ENCRYPTED)) { |
| 1522 | if (!(crypt_stat->flags & ECRYPTFS_KEY_VALID)) { |
Michael Halcrow | 237fead | 2006-10-04 02:16:22 -0700 | [diff] [blame] | 1523 | ecryptfs_printk(KERN_DEBUG, "Key is " |
| 1524 | "invalid; bailing out\n"); |
| 1525 | rc = -EINVAL; |
| 1526 | goto out; |
| 1527 | } |
| 1528 | } else { |
| 1529 | rc = -EINVAL; |
| 1530 | ecryptfs_printk(KERN_WARNING, |
| 1531 | "Called with crypt_stat->encrypted == 0\n"); |
| 1532 | goto out; |
| 1533 | } |
| 1534 | /* Released in this function */ |
Robert P. J. Day | c376222 | 2007-02-10 01:45:03 -0800 | [diff] [blame] | 1535 | page_virt = kmem_cache_zalloc(ecryptfs_header_cache_0, GFP_USER); |
Michael Halcrow | 237fead | 2006-10-04 02:16:22 -0700 | [diff] [blame] | 1536 | if (!page_virt) { |
| 1537 | ecryptfs_printk(KERN_ERR, "Out of memory\n"); |
| 1538 | rc = -ENOMEM; |
| 1539 | goto out; |
| 1540 | } |
Michael Halcrow | dd2a3b7 | 2007-02-12 00:53:46 -0800 | [diff] [blame] | 1541 | rc = ecryptfs_write_headers_virt(page_virt, &size, crypt_stat, |
| 1542 | ecryptfs_dentry); |
Michael Halcrow | 237fead | 2006-10-04 02:16:22 -0700 | [diff] [blame] | 1543 | if (unlikely(rc)) { |
| 1544 | ecryptfs_printk(KERN_ERR, "Error whilst writing headers\n"); |
| 1545 | memset(page_virt, 0, PAGE_CACHE_SIZE); |
| 1546 | goto out_free; |
| 1547 | } |
Michael Halcrow | dd2a3b7 | 2007-02-12 00:53:46 -0800 | [diff] [blame] | 1548 | if (crypt_stat->flags & ECRYPTFS_METADATA_IN_XATTR) |
| 1549 | rc = ecryptfs_write_metadata_to_xattr(ecryptfs_dentry, |
| 1550 | crypt_stat, page_virt, |
| 1551 | size); |
| 1552 | else |
| 1553 | rc = ecryptfs_write_metadata_to_contents(crypt_stat, lower_file, |
| 1554 | page_virt); |
| 1555 | if (rc) { |
| 1556 | printk(KERN_ERR "Error writing metadata out to lower file; " |
| 1557 | "rc = [%d]\n", rc); |
| 1558 | goto out_free; |
Michael Halcrow | 237fead | 2006-10-04 02:16:22 -0700 | [diff] [blame] | 1559 | } |
Michael Halcrow | 237fead | 2006-10-04 02:16:22 -0700 | [diff] [blame] | 1560 | out_free: |
| 1561 | kmem_cache_free(ecryptfs_header_cache_0, page_virt); |
| 1562 | out: |
| 1563 | return rc; |
| 1564 | } |
| 1565 | |
Michael Halcrow | dd2a3b7 | 2007-02-12 00:53:46 -0800 | [diff] [blame] | 1566 | #define ECRYPTFS_DONT_VALIDATE_HEADER_SIZE 0 |
| 1567 | #define ECRYPTFS_VALIDATE_HEADER_SIZE 1 |
Michael Halcrow | 237fead | 2006-10-04 02:16:22 -0700 | [diff] [blame] | 1568 | static int parse_header_metadata(struct ecryptfs_crypt_stat *crypt_stat, |
Michael Halcrow | dd2a3b7 | 2007-02-12 00:53:46 -0800 | [diff] [blame] | 1569 | char *virt, int *bytes_read, |
| 1570 | int validate_header_size) |
Michael Halcrow | 237fead | 2006-10-04 02:16:22 -0700 | [diff] [blame] | 1571 | { |
| 1572 | int rc = 0; |
| 1573 | u32 header_extent_size; |
| 1574 | u16 num_header_extents_at_front; |
| 1575 | |
| 1576 | memcpy(&header_extent_size, virt, 4); |
| 1577 | header_extent_size = be32_to_cpu(header_extent_size); |
| 1578 | virt += 4; |
| 1579 | memcpy(&num_header_extents_at_front, virt, 2); |
| 1580 | num_header_extents_at_front = be16_to_cpu(num_header_extents_at_front); |
Michael Halcrow | 237fead | 2006-10-04 02:16:22 -0700 | [diff] [blame] | 1581 | crypt_stat->num_header_extents_at_front = |
| 1582 | (int)num_header_extents_at_front; |
Michael Halcrow | 45eaab7 | 2007-10-16 01:28:05 -0700 | [diff] [blame] | 1583 | (*bytes_read) = (sizeof(u32) + sizeof(u16)); |
Michael Halcrow | dd2a3b7 | 2007-02-12 00:53:46 -0800 | [diff] [blame] | 1584 | if ((validate_header_size == ECRYPTFS_VALIDATE_HEADER_SIZE) |
Michael Halcrow | 45eaab7 | 2007-10-16 01:28:05 -0700 | [diff] [blame] | 1585 | && ((crypt_stat->extent_size |
Michael Halcrow | dd2a3b7 | 2007-02-12 00:53:46 -0800 | [diff] [blame] | 1586 | * crypt_stat->num_header_extents_at_front) |
| 1587 | < ECRYPTFS_MINIMUM_HEADER_EXTENT_SIZE)) { |
Michael Halcrow | 237fead | 2006-10-04 02:16:22 -0700 | [diff] [blame] | 1588 | rc = -EINVAL; |
Michael Halcrow | 45eaab7 | 2007-10-16 01:28:05 -0700 | [diff] [blame] | 1589 | printk(KERN_WARNING "Invalid number of header extents: [%zd]\n", |
| 1590 | crypt_stat->num_header_extents_at_front); |
Michael Halcrow | 237fead | 2006-10-04 02:16:22 -0700 | [diff] [blame] | 1591 | } |
| 1592 | return rc; |
| 1593 | } |
| 1594 | |
| 1595 | /** |
| 1596 | * set_default_header_data |
Michael Halcrow | 22e78fa | 2007-10-16 01:28:02 -0700 | [diff] [blame] | 1597 | * @crypt_stat: The cryptographic context |
Michael Halcrow | 237fead | 2006-10-04 02:16:22 -0700 | [diff] [blame] | 1598 | * |
| 1599 | * For version 0 file format; this function is only for backwards |
| 1600 | * compatibility for files created with the prior versions of |
| 1601 | * eCryptfs. |
| 1602 | */ |
| 1603 | static void set_default_header_data(struct ecryptfs_crypt_stat *crypt_stat) |
| 1604 | { |
Michael Halcrow | 45eaab7 | 2007-10-16 01:28:05 -0700 | [diff] [blame] | 1605 | crypt_stat->num_header_extents_at_front = 2; |
Michael Halcrow | 237fead | 2006-10-04 02:16:22 -0700 | [diff] [blame] | 1606 | } |
| 1607 | |
| 1608 | /** |
| 1609 | * ecryptfs_read_headers_virt |
Michael Halcrow | 22e78fa | 2007-10-16 01:28:02 -0700 | [diff] [blame] | 1610 | * @page_virt: The virtual address into which to read the headers |
| 1611 | * @crypt_stat: The cryptographic context |
| 1612 | * @ecryptfs_dentry: The eCryptfs dentry |
| 1613 | * @validate_header_size: Whether to validate the header size while reading |
Michael Halcrow | 237fead | 2006-10-04 02:16:22 -0700 | [diff] [blame] | 1614 | * |
| 1615 | * Read/parse the header data. The header format is detailed in the |
| 1616 | * comment block for the ecryptfs_write_headers_virt() function. |
| 1617 | * |
| 1618 | * Returns zero on success |
| 1619 | */ |
| 1620 | static int ecryptfs_read_headers_virt(char *page_virt, |
| 1621 | struct ecryptfs_crypt_stat *crypt_stat, |
Michael Halcrow | dd2a3b7 | 2007-02-12 00:53:46 -0800 | [diff] [blame] | 1622 | struct dentry *ecryptfs_dentry, |
| 1623 | int validate_header_size) |
Michael Halcrow | 237fead | 2006-10-04 02:16:22 -0700 | [diff] [blame] | 1624 | { |
| 1625 | int rc = 0; |
| 1626 | int offset; |
| 1627 | int bytes_read; |
| 1628 | |
| 1629 | ecryptfs_set_default_sizes(crypt_stat); |
| 1630 | crypt_stat->mount_crypt_stat = &ecryptfs_superblock_to_private( |
| 1631 | ecryptfs_dentry->d_sb)->mount_crypt_stat; |
| 1632 | offset = ECRYPTFS_FILE_SIZE_BYTES; |
| 1633 | rc = contains_ecryptfs_marker(page_virt + offset); |
| 1634 | if (rc == 0) { |
| 1635 | rc = -EINVAL; |
| 1636 | goto out; |
| 1637 | } |
| 1638 | offset += MAGIC_ECRYPTFS_MARKER_SIZE_BYTES; |
| 1639 | rc = ecryptfs_process_flags(crypt_stat, (page_virt + offset), |
| 1640 | &bytes_read); |
| 1641 | if (rc) { |
| 1642 | ecryptfs_printk(KERN_WARNING, "Error processing flags\n"); |
| 1643 | goto out; |
| 1644 | } |
| 1645 | if (crypt_stat->file_version > ECRYPTFS_SUPPORTED_FILE_VERSION) { |
| 1646 | ecryptfs_printk(KERN_WARNING, "File version is [%d]; only " |
| 1647 | "file version [%d] is supported by this " |
| 1648 | "version of eCryptfs\n", |
| 1649 | crypt_stat->file_version, |
| 1650 | ECRYPTFS_SUPPORTED_FILE_VERSION); |
| 1651 | rc = -EINVAL; |
| 1652 | goto out; |
| 1653 | } |
| 1654 | offset += bytes_read; |
| 1655 | if (crypt_stat->file_version >= 1) { |
| 1656 | rc = parse_header_metadata(crypt_stat, (page_virt + offset), |
Michael Halcrow | dd2a3b7 | 2007-02-12 00:53:46 -0800 | [diff] [blame] | 1657 | &bytes_read, validate_header_size); |
Michael Halcrow | 237fead | 2006-10-04 02:16:22 -0700 | [diff] [blame] | 1658 | if (rc) { |
| 1659 | ecryptfs_printk(KERN_WARNING, "Error reading header " |
| 1660 | "metadata; rc = [%d]\n", rc); |
| 1661 | } |
| 1662 | offset += bytes_read; |
| 1663 | } else |
| 1664 | set_default_header_data(crypt_stat); |
| 1665 | rc = ecryptfs_parse_packet_set(crypt_stat, (page_virt + offset), |
| 1666 | ecryptfs_dentry); |
| 1667 | out: |
| 1668 | return rc; |
| 1669 | } |
| 1670 | |
| 1671 | /** |
Michael Halcrow | dd2a3b7 | 2007-02-12 00:53:46 -0800 | [diff] [blame] | 1672 | * ecryptfs_read_xattr_region |
Michael Halcrow | 22e78fa | 2007-10-16 01:28:02 -0700 | [diff] [blame] | 1673 | * @page_virt: The vitual address into which to read the xattr data |
| 1674 | * @ecryptfs_dentry: The eCryptfs dentry |
Michael Halcrow | dd2a3b7 | 2007-02-12 00:53:46 -0800 | [diff] [blame] | 1675 | * |
| 1676 | * Attempts to read the crypto metadata from the extended attribute |
| 1677 | * region of the lower file. |
Michael Halcrow | 22e78fa | 2007-10-16 01:28:02 -0700 | [diff] [blame] | 1678 | * |
| 1679 | * Returns zero on success; non-zero on error |
Michael Halcrow | dd2a3b7 | 2007-02-12 00:53:46 -0800 | [diff] [blame] | 1680 | */ |
| 1681 | int ecryptfs_read_xattr_region(char *page_virt, struct dentry *ecryptfs_dentry) |
| 1682 | { |
| 1683 | ssize_t size; |
| 1684 | int rc = 0; |
| 1685 | |
| 1686 | size = ecryptfs_getxattr(ecryptfs_dentry, ECRYPTFS_XATTR_NAME, |
| 1687 | page_virt, ECRYPTFS_DEFAULT_EXTENT_SIZE); |
| 1688 | if (size < 0) { |
| 1689 | printk(KERN_DEBUG "Error attempting to read the [%s] " |
| 1690 | "xattr from the lower file; return value = [%zd]\n", |
| 1691 | ECRYPTFS_XATTR_NAME, size); |
| 1692 | rc = -EINVAL; |
| 1693 | goto out; |
| 1694 | } |
| 1695 | out: |
| 1696 | return rc; |
| 1697 | } |
| 1698 | |
| 1699 | int ecryptfs_read_and_validate_xattr_region(char *page_virt, |
| 1700 | struct dentry *ecryptfs_dentry) |
| 1701 | { |
| 1702 | int rc; |
| 1703 | |
| 1704 | rc = ecryptfs_read_xattr_region(page_virt, ecryptfs_dentry); |
| 1705 | if (rc) |
| 1706 | goto out; |
| 1707 | if (!contains_ecryptfs_marker(page_virt + ECRYPTFS_FILE_SIZE_BYTES)) { |
| 1708 | printk(KERN_WARNING "Valid data found in [%s] xattr, but " |
| 1709 | "the marker is invalid\n", ECRYPTFS_XATTR_NAME); |
| 1710 | rc = -EINVAL; |
| 1711 | } |
| 1712 | out: |
| 1713 | return rc; |
| 1714 | } |
| 1715 | |
| 1716 | /** |
| 1717 | * ecryptfs_read_metadata |
Michael Halcrow | 22e78fa | 2007-10-16 01:28:02 -0700 | [diff] [blame] | 1718 | * @ecryptfs_dentry: The eCryptfs dentry |
| 1719 | * @lower_file: The lower file from which to read the metadata |
Michael Halcrow | dd2a3b7 | 2007-02-12 00:53:46 -0800 | [diff] [blame] | 1720 | * |
| 1721 | * Common entry point for reading file metadata. From here, we could |
| 1722 | * retrieve the header information from the header region of the file, |
| 1723 | * the xattr region of the file, or some other repostory that is |
| 1724 | * stored separately from the file itself. The current implementation |
| 1725 | * supports retrieving the metadata information from the file contents |
| 1726 | * and from the xattr region. |
Michael Halcrow | 237fead | 2006-10-04 02:16:22 -0700 | [diff] [blame] | 1727 | * |
| 1728 | * Returns zero if valid headers found and parsed; non-zero otherwise |
| 1729 | */ |
Michael Halcrow | dd2a3b7 | 2007-02-12 00:53:46 -0800 | [diff] [blame] | 1730 | int ecryptfs_read_metadata(struct dentry *ecryptfs_dentry, |
| 1731 | struct file *lower_file) |
Michael Halcrow | 237fead | 2006-10-04 02:16:22 -0700 | [diff] [blame] | 1732 | { |
| 1733 | int rc = 0; |
| 1734 | char *page_virt = NULL; |
| 1735 | mm_segment_t oldfs; |
| 1736 | ssize_t bytes_read; |
| 1737 | struct ecryptfs_crypt_stat *crypt_stat = |
| 1738 | &ecryptfs_inode_to_private(ecryptfs_dentry->d_inode)->crypt_stat; |
Michael Halcrow | e77a56d | 2007-02-12 00:53:47 -0800 | [diff] [blame] | 1739 | struct ecryptfs_mount_crypt_stat *mount_crypt_stat = |
| 1740 | &ecryptfs_superblock_to_private( |
| 1741 | ecryptfs_dentry->d_sb)->mount_crypt_stat; |
Michael Halcrow | 237fead | 2006-10-04 02:16:22 -0700 | [diff] [blame] | 1742 | |
Michael Halcrow | e77a56d | 2007-02-12 00:53:47 -0800 | [diff] [blame] | 1743 | ecryptfs_copy_mount_wide_flags_to_inode_flags(crypt_stat, |
| 1744 | mount_crypt_stat); |
Michael Halcrow | 237fead | 2006-10-04 02:16:22 -0700 | [diff] [blame] | 1745 | /* Read the first page from the underlying file */ |
Christoph Lameter | f7267c0 | 2006-12-06 20:33:15 -0800 | [diff] [blame] | 1746 | page_virt = kmem_cache_alloc(ecryptfs_header_cache_1, GFP_USER); |
Michael Halcrow | 237fead | 2006-10-04 02:16:22 -0700 | [diff] [blame] | 1747 | if (!page_virt) { |
| 1748 | rc = -ENOMEM; |
| 1749 | ecryptfs_printk(KERN_ERR, "Unable to allocate page_virt\n"); |
| 1750 | goto out; |
| 1751 | } |
| 1752 | lower_file->f_pos = 0; |
| 1753 | oldfs = get_fs(); |
| 1754 | set_fs(get_ds()); |
| 1755 | bytes_read = lower_file->f_op->read(lower_file, |
| 1756 | (char __user *)page_virt, |
| 1757 | ECRYPTFS_DEFAULT_EXTENT_SIZE, |
| 1758 | &lower_file->f_pos); |
| 1759 | set_fs(oldfs); |
| 1760 | if (bytes_read != ECRYPTFS_DEFAULT_EXTENT_SIZE) { |
| 1761 | rc = -EINVAL; |
| 1762 | goto out; |
| 1763 | } |
| 1764 | rc = ecryptfs_read_headers_virt(page_virt, crypt_stat, |
Michael Halcrow | dd2a3b7 | 2007-02-12 00:53:46 -0800 | [diff] [blame] | 1765 | ecryptfs_dentry, |
| 1766 | ECRYPTFS_VALIDATE_HEADER_SIZE); |
Michael Halcrow | 237fead | 2006-10-04 02:16:22 -0700 | [diff] [blame] | 1767 | if (rc) { |
Michael Halcrow | dd2a3b7 | 2007-02-12 00:53:46 -0800 | [diff] [blame] | 1768 | rc = ecryptfs_read_xattr_region(page_virt, |
| 1769 | ecryptfs_dentry); |
| 1770 | if (rc) { |
| 1771 | printk(KERN_DEBUG "Valid eCryptfs headers not found in " |
| 1772 | "file header region or xattr region\n"); |
| 1773 | rc = -EINVAL; |
| 1774 | goto out; |
| 1775 | } |
| 1776 | rc = ecryptfs_read_headers_virt(page_virt, crypt_stat, |
| 1777 | ecryptfs_dentry, |
| 1778 | ECRYPTFS_DONT_VALIDATE_HEADER_SIZE); |
| 1779 | if (rc) { |
| 1780 | printk(KERN_DEBUG "Valid eCryptfs headers not found in " |
| 1781 | "file xattr region either\n"); |
| 1782 | rc = -EINVAL; |
| 1783 | } |
| 1784 | if (crypt_stat->mount_crypt_stat->flags |
| 1785 | & ECRYPTFS_XATTR_METADATA_ENABLED) { |
| 1786 | crypt_stat->flags |= ECRYPTFS_METADATA_IN_XATTR; |
| 1787 | } else { |
| 1788 | printk(KERN_WARNING "Attempt to access file with " |
| 1789 | "crypto metadata only in the extended attribute " |
| 1790 | "region, but eCryptfs was mounted without " |
| 1791 | "xattr support enabled. eCryptfs will not treat " |
| 1792 | "this like an encrypted file.\n"); |
| 1793 | rc = -EINVAL; |
| 1794 | } |
Michael Halcrow | 237fead | 2006-10-04 02:16:22 -0700 | [diff] [blame] | 1795 | } |
| 1796 | out: |
| 1797 | if (page_virt) { |
| 1798 | memset(page_virt, 0, PAGE_CACHE_SIZE); |
| 1799 | kmem_cache_free(ecryptfs_header_cache_1, page_virt); |
| 1800 | } |
| 1801 | return rc; |
| 1802 | } |
| 1803 | |
| 1804 | /** |
| 1805 | * ecryptfs_encode_filename - converts a plaintext file name to cipher text |
| 1806 | * @crypt_stat: The crypt_stat struct associated with the file anem to encode |
| 1807 | * @name: The plaintext name |
| 1808 | * @length: The length of the plaintext |
| 1809 | * @encoded_name: The encypted name |
| 1810 | * |
| 1811 | * Encrypts and encodes a filename into something that constitutes a |
| 1812 | * valid filename for a filesystem, with printable characters. |
| 1813 | * |
| 1814 | * We assume that we have a properly initialized crypto context, |
| 1815 | * pointed to by crypt_stat->tfm. |
| 1816 | * |
| 1817 | * TODO: Implement filename decoding and decryption here, in place of |
| 1818 | * memcpy. We are keeping the framework around for now to (1) |
| 1819 | * facilitate testing of the components needed to implement filename |
| 1820 | * encryption and (2) to provide a code base from which other |
| 1821 | * developers in the community can easily implement this feature. |
| 1822 | * |
| 1823 | * Returns the length of encoded filename; negative if error |
| 1824 | */ |
| 1825 | int |
| 1826 | ecryptfs_encode_filename(struct ecryptfs_crypt_stat *crypt_stat, |
| 1827 | const char *name, int length, char **encoded_name) |
| 1828 | { |
| 1829 | int error = 0; |
| 1830 | |
| 1831 | (*encoded_name) = kmalloc(length + 2, GFP_KERNEL); |
| 1832 | if (!(*encoded_name)) { |
| 1833 | error = -ENOMEM; |
| 1834 | goto out; |
| 1835 | } |
| 1836 | /* TODO: Filename encryption is a scheduled feature for a |
| 1837 | * future version of eCryptfs. This function is here only for |
| 1838 | * the purpose of providing a framework for other developers |
| 1839 | * to easily implement filename encryption. Hint: Replace this |
| 1840 | * memcpy() with a call to encrypt and encode the |
| 1841 | * filename, the set the length accordingly. */ |
| 1842 | memcpy((void *)(*encoded_name), (void *)name, length); |
| 1843 | (*encoded_name)[length] = '\0'; |
| 1844 | error = length + 1; |
| 1845 | out: |
| 1846 | return error; |
| 1847 | } |
| 1848 | |
| 1849 | /** |
| 1850 | * ecryptfs_decode_filename - converts the cipher text name to plaintext |
| 1851 | * @crypt_stat: The crypt_stat struct associated with the file |
| 1852 | * @name: The filename in cipher text |
| 1853 | * @length: The length of the cipher text name |
| 1854 | * @decrypted_name: The plaintext name |
| 1855 | * |
| 1856 | * Decodes and decrypts the filename. |
| 1857 | * |
| 1858 | * We assume that we have a properly initialized crypto context, |
| 1859 | * pointed to by crypt_stat->tfm. |
| 1860 | * |
| 1861 | * TODO: Implement filename decoding and decryption here, in place of |
| 1862 | * memcpy. We are keeping the framework around for now to (1) |
| 1863 | * facilitate testing of the components needed to implement filename |
| 1864 | * encryption and (2) to provide a code base from which other |
| 1865 | * developers in the community can easily implement this feature. |
| 1866 | * |
| 1867 | * Returns the length of decoded filename; negative if error |
| 1868 | */ |
| 1869 | int |
| 1870 | ecryptfs_decode_filename(struct ecryptfs_crypt_stat *crypt_stat, |
| 1871 | const char *name, int length, char **decrypted_name) |
| 1872 | { |
| 1873 | int error = 0; |
| 1874 | |
| 1875 | (*decrypted_name) = kmalloc(length + 2, GFP_KERNEL); |
| 1876 | if (!(*decrypted_name)) { |
| 1877 | error = -ENOMEM; |
| 1878 | goto out; |
| 1879 | } |
| 1880 | /* TODO: Filename encryption is a scheduled feature for a |
| 1881 | * future version of eCryptfs. This function is here only for |
| 1882 | * the purpose of providing a framework for other developers |
| 1883 | * to easily implement filename encryption. Hint: Replace this |
| 1884 | * memcpy() with a call to decode and decrypt the |
| 1885 | * filename, the set the length accordingly. */ |
| 1886 | memcpy((void *)(*decrypted_name), (void *)name, length); |
| 1887 | (*decrypted_name)[length + 1] = '\0'; /* Only for convenience |
| 1888 | * in printing out the |
| 1889 | * string in debug |
| 1890 | * messages */ |
| 1891 | error = length; |
| 1892 | out: |
| 1893 | return error; |
| 1894 | } |
| 1895 | |
| 1896 | /** |
Michael Halcrow | f4aad16 | 2007-10-16 01:27:53 -0700 | [diff] [blame] | 1897 | * ecryptfs_process_key_cipher - Perform key cipher initialization. |
Michael Halcrow | 237fead | 2006-10-04 02:16:22 -0700 | [diff] [blame] | 1898 | * @key_tfm: Crypto context for key material, set by this function |
Michael Halcrow | e5d9cbd | 2006-10-30 22:07:16 -0800 | [diff] [blame] | 1899 | * @cipher_name: Name of the cipher |
| 1900 | * @key_size: Size of the key in bytes |
Michael Halcrow | 237fead | 2006-10-04 02:16:22 -0700 | [diff] [blame] | 1901 | * |
| 1902 | * Returns zero on success. Any crypto_tfm structs allocated here |
| 1903 | * should be released by other functions, such as on a superblock put |
| 1904 | * event, regardless of whether this function succeeds for fails. |
| 1905 | */ |
Michael Halcrow | cd9d67d | 2007-10-16 01:28:04 -0700 | [diff] [blame] | 1906 | static int |
Michael Halcrow | f4aad16 | 2007-10-16 01:27:53 -0700 | [diff] [blame] | 1907 | ecryptfs_process_key_cipher(struct crypto_blkcipher **key_tfm, |
| 1908 | char *cipher_name, size_t *key_size) |
Michael Halcrow | 237fead | 2006-10-04 02:16:22 -0700 | [diff] [blame] | 1909 | { |
| 1910 | char dummy_key[ECRYPTFS_MAX_KEY_BYTES]; |
Michael Halcrow | 8bba066 | 2006-10-30 22:07:18 -0800 | [diff] [blame] | 1911 | char *full_alg_name; |
Michael Halcrow | 237fead | 2006-10-04 02:16:22 -0700 | [diff] [blame] | 1912 | int rc; |
| 1913 | |
Michael Halcrow | e5d9cbd | 2006-10-30 22:07:16 -0800 | [diff] [blame] | 1914 | *key_tfm = NULL; |
| 1915 | if (*key_size > ECRYPTFS_MAX_KEY_BYTES) { |
Michael Halcrow | 237fead | 2006-10-04 02:16:22 -0700 | [diff] [blame] | 1916 | rc = -EINVAL; |
| 1917 | printk(KERN_ERR "Requested key size is [%Zd] bytes; maximum " |
Michael Halcrow | e5d9cbd | 2006-10-30 22:07:16 -0800 | [diff] [blame] | 1918 | "allowable is [%d]\n", *key_size, ECRYPTFS_MAX_KEY_BYTES); |
Michael Halcrow | 237fead | 2006-10-04 02:16:22 -0700 | [diff] [blame] | 1919 | goto out; |
| 1920 | } |
Michael Halcrow | 8bba066 | 2006-10-30 22:07:18 -0800 | [diff] [blame] | 1921 | rc = ecryptfs_crypto_api_algify_cipher_name(&full_alg_name, cipher_name, |
| 1922 | "ecb"); |
| 1923 | if (rc) |
| 1924 | goto out; |
| 1925 | *key_tfm = crypto_alloc_blkcipher(full_alg_name, 0, CRYPTO_ALG_ASYNC); |
| 1926 | kfree(full_alg_name); |
| 1927 | if (IS_ERR(*key_tfm)) { |
| 1928 | rc = PTR_ERR(*key_tfm); |
Michael Halcrow | 237fead | 2006-10-04 02:16:22 -0700 | [diff] [blame] | 1929 | printk(KERN_ERR "Unable to allocate crypto cipher with name " |
Michael Halcrow | 8bba066 | 2006-10-30 22:07:18 -0800 | [diff] [blame] | 1930 | "[%s]; rc = [%d]\n", cipher_name, rc); |
Michael Halcrow | 237fead | 2006-10-04 02:16:22 -0700 | [diff] [blame] | 1931 | goto out; |
| 1932 | } |
Michael Halcrow | 8bba066 | 2006-10-30 22:07:18 -0800 | [diff] [blame] | 1933 | crypto_blkcipher_set_flags(*key_tfm, CRYPTO_TFM_REQ_WEAK_KEY); |
| 1934 | if (*key_size == 0) { |
| 1935 | struct blkcipher_alg *alg = crypto_blkcipher_alg(*key_tfm); |
| 1936 | |
| 1937 | *key_size = alg->max_keysize; |
| 1938 | } |
Michael Halcrow | e5d9cbd | 2006-10-30 22:07:16 -0800 | [diff] [blame] | 1939 | get_random_bytes(dummy_key, *key_size); |
Michael Halcrow | 8bba066 | 2006-10-30 22:07:18 -0800 | [diff] [blame] | 1940 | rc = crypto_blkcipher_setkey(*key_tfm, dummy_key, *key_size); |
Michael Halcrow | 237fead | 2006-10-04 02:16:22 -0700 | [diff] [blame] | 1941 | if (rc) { |
| 1942 | printk(KERN_ERR "Error attempting to set key of size [%Zd] for " |
Michael Halcrow | e5d9cbd | 2006-10-30 22:07:16 -0800 | [diff] [blame] | 1943 | "cipher [%s]; rc = [%d]\n", *key_size, cipher_name, rc); |
Michael Halcrow | 237fead | 2006-10-04 02:16:22 -0700 | [diff] [blame] | 1944 | rc = -EINVAL; |
| 1945 | goto out; |
| 1946 | } |
| 1947 | out: |
| 1948 | return rc; |
| 1949 | } |
Michael Halcrow | f4aad16 | 2007-10-16 01:27:53 -0700 | [diff] [blame] | 1950 | |
| 1951 | struct kmem_cache *ecryptfs_key_tfm_cache; |
| 1952 | struct list_head key_tfm_list; |
| 1953 | struct mutex key_tfm_list_mutex; |
| 1954 | |
| 1955 | int ecryptfs_init_crypto(void) |
| 1956 | { |
| 1957 | mutex_init(&key_tfm_list_mutex); |
| 1958 | INIT_LIST_HEAD(&key_tfm_list); |
| 1959 | return 0; |
| 1960 | } |
| 1961 | |
Michael Halcrow | fcd1283 | 2007-10-16 01:28:01 -0700 | [diff] [blame] | 1962 | int ecryptfs_destroy_crypto(void) |
Michael Halcrow | f4aad16 | 2007-10-16 01:27:53 -0700 | [diff] [blame] | 1963 | { |
| 1964 | struct ecryptfs_key_tfm *key_tfm, *key_tfm_tmp; |
| 1965 | |
| 1966 | mutex_lock(&key_tfm_list_mutex); |
| 1967 | list_for_each_entry_safe(key_tfm, key_tfm_tmp, &key_tfm_list, |
| 1968 | key_tfm_list) { |
| 1969 | list_del(&key_tfm->key_tfm_list); |
| 1970 | if (key_tfm->key_tfm) |
| 1971 | crypto_free_blkcipher(key_tfm->key_tfm); |
| 1972 | kmem_cache_free(ecryptfs_key_tfm_cache, key_tfm); |
| 1973 | } |
| 1974 | mutex_unlock(&key_tfm_list_mutex); |
| 1975 | return 0; |
| 1976 | } |
| 1977 | |
| 1978 | int |
| 1979 | ecryptfs_add_new_key_tfm(struct ecryptfs_key_tfm **key_tfm, char *cipher_name, |
| 1980 | size_t key_size) |
| 1981 | { |
| 1982 | struct ecryptfs_key_tfm *tmp_tfm; |
| 1983 | int rc = 0; |
| 1984 | |
| 1985 | tmp_tfm = kmem_cache_alloc(ecryptfs_key_tfm_cache, GFP_KERNEL); |
| 1986 | if (key_tfm != NULL) |
| 1987 | (*key_tfm) = tmp_tfm; |
| 1988 | if (!tmp_tfm) { |
| 1989 | rc = -ENOMEM; |
| 1990 | printk(KERN_ERR "Error attempting to allocate from " |
| 1991 | "ecryptfs_key_tfm_cache\n"); |
| 1992 | goto out; |
| 1993 | } |
| 1994 | mutex_init(&tmp_tfm->key_tfm_mutex); |
| 1995 | strncpy(tmp_tfm->cipher_name, cipher_name, |
| 1996 | ECRYPTFS_MAX_CIPHER_NAME_SIZE); |
| 1997 | tmp_tfm->key_size = key_size; |
Michael Halcrow | 5dda699 | 2007-10-16 01:28:06 -0700 | [diff] [blame] | 1998 | rc = ecryptfs_process_key_cipher(&tmp_tfm->key_tfm, |
| 1999 | tmp_tfm->cipher_name, |
| 2000 | &tmp_tfm->key_size); |
| 2001 | if (rc) { |
Michael Halcrow | f4aad16 | 2007-10-16 01:27:53 -0700 | [diff] [blame] | 2002 | printk(KERN_ERR "Error attempting to initialize key TFM " |
| 2003 | "cipher with name = [%s]; rc = [%d]\n", |
| 2004 | tmp_tfm->cipher_name, rc); |
| 2005 | kmem_cache_free(ecryptfs_key_tfm_cache, tmp_tfm); |
| 2006 | if (key_tfm != NULL) |
| 2007 | (*key_tfm) = NULL; |
| 2008 | goto out; |
| 2009 | } |
| 2010 | mutex_lock(&key_tfm_list_mutex); |
| 2011 | list_add(&tmp_tfm->key_tfm_list, &key_tfm_list); |
| 2012 | mutex_unlock(&key_tfm_list_mutex); |
| 2013 | out: |
| 2014 | return rc; |
| 2015 | } |
| 2016 | |
| 2017 | int ecryptfs_get_tfm_and_mutex_for_cipher_name(struct crypto_blkcipher **tfm, |
| 2018 | struct mutex **tfm_mutex, |
| 2019 | char *cipher_name) |
| 2020 | { |
| 2021 | struct ecryptfs_key_tfm *key_tfm; |
| 2022 | int rc = 0; |
| 2023 | |
| 2024 | (*tfm) = NULL; |
| 2025 | (*tfm_mutex) = NULL; |
| 2026 | mutex_lock(&key_tfm_list_mutex); |
| 2027 | list_for_each_entry(key_tfm, &key_tfm_list, key_tfm_list) { |
| 2028 | if (strcmp(key_tfm->cipher_name, cipher_name) == 0) { |
| 2029 | (*tfm) = key_tfm->key_tfm; |
| 2030 | (*tfm_mutex) = &key_tfm->key_tfm_mutex; |
| 2031 | mutex_unlock(&key_tfm_list_mutex); |
| 2032 | goto out; |
| 2033 | } |
| 2034 | } |
| 2035 | mutex_unlock(&key_tfm_list_mutex); |
Michael Halcrow | 5dda699 | 2007-10-16 01:28:06 -0700 | [diff] [blame] | 2036 | rc = ecryptfs_add_new_key_tfm(&key_tfm, cipher_name, 0); |
| 2037 | if (rc) { |
Michael Halcrow | f4aad16 | 2007-10-16 01:27:53 -0700 | [diff] [blame] | 2038 | printk(KERN_ERR "Error adding new key_tfm to list; rc = [%d]\n", |
| 2039 | rc); |
| 2040 | goto out; |
| 2041 | } |
| 2042 | (*tfm) = key_tfm->key_tfm; |
| 2043 | (*tfm_mutex) = &key_tfm->key_tfm_mutex; |
| 2044 | out: |
| 2045 | return rc; |
| 2046 | } |