blob: 4f7d895919703df511938cdfdfaaa6330db716b1 [file] [log] [blame]
Michael Halcrow237fead2006-10-04 02:16:22 -07001/**
2 * eCryptfs: Linux filesystem encryption layer
3 *
4 * Copyright (C) 1997-2004 Erez Zadok
5 * Copyright (C) 2001-2004 Stony Brook University
Michael Halcrowdd2a3b72007-02-12 00:53:46 -08006 * Copyright (C) 2004-2007 International Business Machines Corp.
Michael Halcrow237fead2006-10-04 02:16:22 -07007 * 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
38static int
39ecryptfs_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);
43static int
44ecryptfs_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 */
56void 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 */
71void 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 */
93static int ecryptfs_calculate_md5(char *dst,
94 struct ecryptfs_crypt_stat *crypt_stat,
95 char *src, int len)
96{
Michael Halcrow237fead2006-10-04 02:16:22 -070097 struct scatterlist sg;
Michael Halcrow565d9722006-10-30 22:07:17 -080098 struct hash_desc desc = {
99 .tfm = crypt_stat->hash_tfm,
100 .flags = CRYPTO_TFM_REQ_MAY_SLEEP
101 };
102 int rc = 0;
Michael Halcrow237fead2006-10-04 02:16:22 -0700103
Michael Halcrow565d9722006-10-30 22:07:17 -0800104 mutex_lock(&crypt_stat->cs_hash_tfm_mutex);
Michael Halcrow237fead2006-10-04 02:16:22 -0700105 sg_init_one(&sg, (u8 *)src, len);
Michael Halcrow565d9722006-10-30 22:07:17 -0800106 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 Halcrow237fead2006-10-04 02:16:22 -0700111 ecryptfs_printk(KERN_ERR, "Error attempting to "
Michael Halcrow565d9722006-10-30 22:07:17 -0800112 "allocate crypto context; rc = [%d]\n",
113 rc);
Michael Halcrow237fead2006-10-04 02:16:22 -0700114 goto out;
115 }
Michael Halcrow565d9722006-10-30 22:07:17 -0800116 crypt_stat->hash_tfm = desc.tfm;
Michael Halcrow237fead2006-10-04 02:16:22 -0700117 }
Michael Halcrow565d9722006-10-30 22:07:17 -0800118 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 Halcrow237fead2006-10-04 02:16:22 -0700122out:
123 return rc;
124}
125
Michael Halcrow8bba0662006-10-30 22:07:18 -0800126int ecryptfs_crypto_api_algify_cipher_name(char **algified_name,
127 char *cipher_name,
128 char *chaining_modifier)
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 Halcrow7bd473f2006-11-02 22:06:56 -0800137 if (!(*algified_name)) {
Michael Halcrow8bba0662006-10-30 22:07:18 -0800138 rc = -ENOMEM;
139 goto out;
140 }
141 snprintf((*algified_name), algified_name_len, "%s(%s)",
142 chaining_modifier, cipher_name);
143 rc = 0;
144out:
145 return rc;
146}
147
Michael Halcrow237fead2006-10-04 02:16:22 -0700148/**
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 */
159static 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 }
193out:
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 */
203void
204ecryptfs_init_crypt_stat(struct ecryptfs_crypt_stat *crypt_stat)
205{
206 memset((void *)crypt_stat, 0, sizeof(struct ecryptfs_crypt_stat));
Michael Halcrowf4aad162007-10-16 01:27:53 -0700207 INIT_LIST_HEAD(&crypt_stat->keysig_list);
208 mutex_init(&crypt_stat->keysig_list_mutex);
Michael Halcrow237fead2006-10-04 02:16:22 -0700209 mutex_init(&crypt_stat->cs_mutex);
210 mutex_init(&crypt_stat->cs_tfm_mutex);
Michael Halcrow565d9722006-10-30 22:07:17 -0800211 mutex_init(&crypt_stat->cs_hash_tfm_mutex);
Michael Halcrowe2bd99e2007-02-12 00:53:49 -0800212 crypt_stat->flags |= ECRYPTFS_STRUCT_INITIALIZED;
Michael Halcrow237fead2006-10-04 02:16:22 -0700213}
214
215/**
216 * ecryptfs_destruct_crypt_stat
217 * @crypt_stat: Pointer to the crypt_stat struct to initialize.
218 *
219 * Releases all memory associated with a crypt_stat struct.
220 */
221void ecryptfs_destruct_crypt_stat(struct ecryptfs_crypt_stat *crypt_stat)
222{
Michael Halcrowf4aad162007-10-16 01:27:53 -0700223 struct ecryptfs_key_sig *key_sig, *key_sig_tmp;
224
Michael Halcrow237fead2006-10-04 02:16:22 -0700225 if (crypt_stat->tfm)
Michael Halcrow8bba0662006-10-30 22:07:18 -0800226 crypto_free_blkcipher(crypt_stat->tfm);
Michael Halcrow565d9722006-10-30 22:07:17 -0800227 if (crypt_stat->hash_tfm)
228 crypto_free_hash(crypt_stat->hash_tfm);
Michael Halcrowf4aad162007-10-16 01:27:53 -0700229 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 Halcrow237fead2006-10-04 02:16:22 -0700236 memset(crypt_stat, 0, sizeof(struct ecryptfs_crypt_stat));
237}
238
239void ecryptfs_destruct_mount_crypt_stat(
240 struct ecryptfs_mount_crypt_stat *mount_crypt_stat)
241{
Michael Halcrowf4aad162007-10-16 01:27:53 -0700242 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 Halcrow237fead2006-10-04 02:16:22 -0700258 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 */
274int 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 */
318static 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 Halcrow8bba0662006-10-30 22:07:18 -0800323 struct blkcipher_desc desc = {
324 .tfm = crypt_stat->tfm,
325 .info = iv,
326 .flags = CRYPTO_TFM_REQ_MAY_SLEEP
327 };
Michael Halcrow237fead2006-10-04 02:16:22 -0700328 int rc = 0;
329
330 BUG_ON(!crypt_stat || !crypt_stat->tfm
Michael Halcrowe2bd99e2007-02-12 00:53:49 -0800331 || !(crypt_stat->flags & ECRYPTFS_STRUCT_INITIALIZED));
Michael Halcrow237fead2006-10-04 02:16:22 -0700332 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 Halcrow8bba0662006-10-30 22:07:18 -0800340 rc = crypto_blkcipher_setkey(crypt_stat->tfm, crypt_stat->key,
341 crypt_stat->key_size);
Michael Halcrow237fead2006-10-04 02:16:22 -0700342 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 Halcrow8bba0662006-10-30 22:07:18 -0800350 crypto_blkcipher_encrypt_iv(&desc, dest_sg, src_sg, size);
Michael Halcrow237fead2006-10-04 02:16:22 -0700351 mutex_unlock(&crypt_stat->cs_tfm_mutex);
352out:
353 return rc;
354}
355
356static void
357ecryptfs_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 =
369 ( crypt_stat->header_extent_size
370 * crypt_stat->num_header_extents_at_front );
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;
379 ecryptfs_printk(KERN_DEBUG, " * crypt_stat->header_extent_size = "
380 "[%d]\n", crypt_stat->header_extent_size);
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
398static 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 }
425out:
426 return rc;
427}
428
429static 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 Halcrow9d8b8ce2007-02-12 00:53:48 -0800454 *lower_page = grab_cache_page(lower_inode->i_mapping,
455 lower_page_idx);
456 if (!(*lower_page)) {
457 rc = -EINVAL;
Michael Halcrow237fead2006-10-04 02:16:22 -0700458 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 }
465out:
466 return rc;
467}
468
469/**
470 * ecryptfs_encrypt_page
471 * @ctx: The context of the page
472 *
473 * Encrypt an eCryptfs page. This is done on a per-extent basis. Note
474 * that eCryptfs pages may straddle the lower pages -- for instance,
475 * if the file was created on a machine with an 8K page size
476 * (resulting in an 8K header), and then the file is copied onto a
477 * host with a 32K page size, then when reading page 0 of the eCryptfs
478 * file, 24K of page 0 of the lower file will be read and decrypted,
479 * and then 8K of page 1 of the lower file will be read and decrypted.
480 *
481 * The actual operations performed on each page depends on the
482 * contents of the ecryptfs_page_crypt_context struct.
483 *
484 * Returns zero on success; negative on error
485 */
486int ecryptfs_encrypt_page(struct ecryptfs_page_crypt_context *ctx)
487{
488 char extent_iv[ECRYPTFS_MAX_IV_BYTES];
489 unsigned long base_extent;
490 unsigned long extent_offset = 0;
491 unsigned long lower_page_idx = 0;
492 unsigned long prior_lower_page_idx = 0;
493 struct page *lower_page;
494 struct inode *lower_inode;
495 struct ecryptfs_inode_info *inode_info;
496 struct ecryptfs_crypt_stat *crypt_stat;
497 int rc = 0;
498 int lower_byte_offset = 0;
499 int orig_byte_offset = 0;
500 int num_extents_per_page;
501#define ECRYPTFS_PAGE_STATE_UNREAD 0
502#define ECRYPTFS_PAGE_STATE_READ 1
503#define ECRYPTFS_PAGE_STATE_MODIFIED 2
504#define ECRYPTFS_PAGE_STATE_WRITTEN 3
505 int page_state;
506
507 lower_inode = ecryptfs_inode_to_lower(ctx->page->mapping->host);
508 inode_info = ecryptfs_inode_to_private(ctx->page->mapping->host);
509 crypt_stat = &inode_info->crypt_stat;
Michael Halcrowe2bd99e2007-02-12 00:53:49 -0800510 if (!(crypt_stat->flags & ECRYPTFS_ENCRYPTED)) {
Michael Halcrow237fead2006-10-04 02:16:22 -0700511 rc = ecryptfs_copy_page_to_lower(ctx->page, lower_inode,
512 ctx->param.lower_file);
513 if (rc)
514 ecryptfs_printk(KERN_ERR, "Error attempting to copy "
515 "page at index [0x%.16x]\n",
516 ctx->page->index);
517 goto out;
518 }
519 num_extents_per_page = PAGE_CACHE_SIZE / crypt_stat->extent_size;
520 base_extent = (ctx->page->index * num_extents_per_page);
521 page_state = ECRYPTFS_PAGE_STATE_UNREAD;
522 while (extent_offset < num_extents_per_page) {
523 ecryptfs_extent_to_lwr_pg_idx_and_offset(
524 &lower_page_idx, &lower_byte_offset, crypt_stat,
525 (base_extent + extent_offset));
526 if (prior_lower_page_idx != lower_page_idx
527 && page_state == ECRYPTFS_PAGE_STATE_MODIFIED) {
528 rc = ecryptfs_write_out_page(ctx, lower_page,
529 lower_inode,
530 orig_byte_offset,
531 (PAGE_CACHE_SIZE
532 - orig_byte_offset));
533 if (rc) {
534 ecryptfs_printk(KERN_ERR, "Error attempting "
535 "to write out page; rc = [%d]"
536 "\n", rc);
537 goto out;
538 }
539 page_state = ECRYPTFS_PAGE_STATE_WRITTEN;
540 }
541 if (page_state == ECRYPTFS_PAGE_STATE_UNREAD
542 || page_state == ECRYPTFS_PAGE_STATE_WRITTEN) {
543 rc = ecryptfs_read_in_page(ctx, &lower_page,
544 lower_inode, lower_page_idx,
545 lower_byte_offset);
546 if (rc) {
547 ecryptfs_printk(KERN_ERR, "Error attempting "
548 "to read in lower page with "
549 "index [0x%.16x]; rc = [%d]\n",
550 lower_page_idx, rc);
551 goto out;
552 }
553 orig_byte_offset = lower_byte_offset;
554 prior_lower_page_idx = lower_page_idx;
555 page_state = ECRYPTFS_PAGE_STATE_READ;
556 }
557 BUG_ON(!(page_state == ECRYPTFS_PAGE_STATE_MODIFIED
558 || page_state == ECRYPTFS_PAGE_STATE_READ));
559 rc = ecryptfs_derive_iv(extent_iv, crypt_stat,
560 (base_extent + extent_offset));
561 if (rc) {
562 ecryptfs_printk(KERN_ERR, "Error attempting to "
563 "derive IV for extent [0x%.16x]; "
564 "rc = [%d]\n",
565 (base_extent + extent_offset), rc);
566 goto out;
567 }
568 if (unlikely(ecryptfs_verbosity > 0)) {
569 ecryptfs_printk(KERN_DEBUG, "Encrypting extent "
570 "with iv:\n");
571 ecryptfs_dump_hex(extent_iv, crypt_stat->iv_bytes);
572 ecryptfs_printk(KERN_DEBUG, "First 8 bytes before "
573 "encryption:\n");
574 ecryptfs_dump_hex((char *)
575 (page_address(ctx->page)
576 + (extent_offset
577 * crypt_stat->extent_size)), 8);
578 }
579 rc = ecryptfs_encrypt_page_offset(
580 crypt_stat, lower_page, lower_byte_offset, ctx->page,
581 (extent_offset * crypt_stat->extent_size),
582 crypt_stat->extent_size, extent_iv);
583 ecryptfs_printk(KERN_DEBUG, "Encrypt extent [0x%.16x]; "
584 "rc = [%d]\n",
585 (base_extent + extent_offset), rc);
586 if (unlikely(ecryptfs_verbosity > 0)) {
587 ecryptfs_printk(KERN_DEBUG, "First 8 bytes after "
588 "encryption:\n");
589 ecryptfs_dump_hex((char *)(page_address(lower_page)
590 + lower_byte_offset), 8);
591 }
592 page_state = ECRYPTFS_PAGE_STATE_MODIFIED;
593 extent_offset++;
594 }
595 BUG_ON(orig_byte_offset != 0);
596 rc = ecryptfs_write_out_page(ctx, lower_page, lower_inode, 0,
597 (lower_byte_offset
598 + crypt_stat->extent_size));
599 if (rc) {
600 ecryptfs_printk(KERN_ERR, "Error attempting to write out "
601 "page; rc = [%d]\n", rc);
602 goto out;
603 }
604out:
605 return rc;
606}
607
608/**
609 * ecryptfs_decrypt_page
610 * @file: The ecryptfs file
611 * @page: The page in ecryptfs to decrypt
612 *
613 * Decrypt an eCryptfs page. This is done on a per-extent basis. Note
614 * that eCryptfs pages may straddle the lower pages -- for instance,
615 * if the file was created on a machine with an 8K page size
616 * (resulting in an 8K header), and then the file is copied onto a
617 * host with a 32K page size, then when reading page 0 of the eCryptfs
618 * file, 24K of page 0 of the lower file will be read and decrypted,
619 * and then 8K of page 1 of the lower file will be read and decrypted.
620 *
621 * Returns zero on success; negative on error
622 */
623int ecryptfs_decrypt_page(struct file *file, struct page *page)
624{
625 char extent_iv[ECRYPTFS_MAX_IV_BYTES];
626 unsigned long base_extent;
627 unsigned long extent_offset = 0;
628 unsigned long lower_page_idx = 0;
629 unsigned long prior_lower_page_idx = 0;
630 struct page *lower_page;
631 char *lower_page_virt = NULL;
632 struct inode *lower_inode;
633 struct ecryptfs_crypt_stat *crypt_stat;
634 int rc = 0;
635 int byte_offset;
636 int num_extents_per_page;
637 int page_state;
638
639 crypt_stat = &(ecryptfs_inode_to_private(
640 page->mapping->host)->crypt_stat);
641 lower_inode = ecryptfs_inode_to_lower(page->mapping->host);
Michael Halcrowe2bd99e2007-02-12 00:53:49 -0800642 if (!(crypt_stat->flags & ECRYPTFS_ENCRYPTED)) {
Michael Halcrow237fead2006-10-04 02:16:22 -0700643 rc = ecryptfs_do_readpage(file, page, page->index);
644 if (rc)
645 ecryptfs_printk(KERN_ERR, "Error attempting to copy "
646 "page at index [0x%.16x]\n",
647 page->index);
648 goto out;
649 }
650 num_extents_per_page = PAGE_CACHE_SIZE / crypt_stat->extent_size;
651 base_extent = (page->index * num_extents_per_page);
652 lower_page_virt = kmem_cache_alloc(ecryptfs_lower_page_cache,
Christoph Lametere94b1762006-12-06 20:33:17 -0800653 GFP_KERNEL);
Michael Halcrow237fead2006-10-04 02:16:22 -0700654 if (!lower_page_virt) {
655 rc = -ENOMEM;
656 ecryptfs_printk(KERN_ERR, "Error getting page for encrypted "
657 "lower page(s)\n");
658 goto out;
659 }
660 lower_page = virt_to_page(lower_page_virt);
661 page_state = ECRYPTFS_PAGE_STATE_UNREAD;
662 while (extent_offset < num_extents_per_page) {
663 ecryptfs_extent_to_lwr_pg_idx_and_offset(
664 &lower_page_idx, &byte_offset, crypt_stat,
665 (base_extent + extent_offset));
666 if (prior_lower_page_idx != lower_page_idx
667 || page_state == ECRYPTFS_PAGE_STATE_UNREAD) {
668 rc = ecryptfs_do_readpage(file, lower_page,
669 lower_page_idx);
670 if (rc) {
671 ecryptfs_printk(KERN_ERR, "Error reading "
672 "lower encrypted page; rc = "
673 "[%d]\n", rc);
674 goto out;
675 }
676 prior_lower_page_idx = lower_page_idx;
677 page_state = ECRYPTFS_PAGE_STATE_READ;
678 }
679 rc = ecryptfs_derive_iv(extent_iv, crypt_stat,
680 (base_extent + extent_offset));
681 if (rc) {
682 ecryptfs_printk(KERN_ERR, "Error attempting to "
683 "derive IV for extent [0x%.16x]; rc = "
684 "[%d]\n",
685 (base_extent + extent_offset), rc);
686 goto out;
687 }
688 if (unlikely(ecryptfs_verbosity > 0)) {
689 ecryptfs_printk(KERN_DEBUG, "Decrypting extent "
690 "with iv:\n");
691 ecryptfs_dump_hex(extent_iv, crypt_stat->iv_bytes);
692 ecryptfs_printk(KERN_DEBUG, "First 8 bytes before "
693 "decryption:\n");
694 ecryptfs_dump_hex((lower_page_virt + byte_offset), 8);
695 }
696 rc = ecryptfs_decrypt_page_offset(crypt_stat, page,
697 (extent_offset
698 * crypt_stat->extent_size),
699 lower_page, byte_offset,
700 crypt_stat->extent_size,
701 extent_iv);
702 if (rc != crypt_stat->extent_size) {
703 ecryptfs_printk(KERN_ERR, "Error attempting to "
704 "decrypt extent [0x%.16x]\n",
705 (base_extent + extent_offset));
706 goto out;
707 }
708 rc = 0;
709 if (unlikely(ecryptfs_verbosity > 0)) {
710 ecryptfs_printk(KERN_DEBUG, "First 8 bytes after "
711 "decryption:\n");
712 ecryptfs_dump_hex((char *)(page_address(page)
713 + byte_offset), 8);
714 }
715 extent_offset++;
716 }
717out:
718 if (lower_page_virt)
719 kmem_cache_free(ecryptfs_lower_page_cache, lower_page_virt);
720 return rc;
721}
722
723/**
724 * decrypt_scatterlist
725 *
726 * Returns the number of bytes decrypted; negative value on error
727 */
728static int decrypt_scatterlist(struct ecryptfs_crypt_stat *crypt_stat,
729 struct scatterlist *dest_sg,
730 struct scatterlist *src_sg, int size,
731 unsigned char *iv)
732{
Michael Halcrow8bba0662006-10-30 22:07:18 -0800733 struct blkcipher_desc desc = {
734 .tfm = crypt_stat->tfm,
735 .info = iv,
736 .flags = CRYPTO_TFM_REQ_MAY_SLEEP
737 };
Michael Halcrow237fead2006-10-04 02:16:22 -0700738 int rc = 0;
739
740 /* Consider doing this once, when the file is opened */
741 mutex_lock(&crypt_stat->cs_tfm_mutex);
Michael Halcrow8bba0662006-10-30 22:07:18 -0800742 rc = crypto_blkcipher_setkey(crypt_stat->tfm, crypt_stat->key,
743 crypt_stat->key_size);
Michael Halcrow237fead2006-10-04 02:16:22 -0700744 if (rc) {
745 ecryptfs_printk(KERN_ERR, "Error setting key; rc = [%d]\n",
746 rc);
747 mutex_unlock(&crypt_stat->cs_tfm_mutex);
748 rc = -EINVAL;
749 goto out;
750 }
751 ecryptfs_printk(KERN_DEBUG, "Decrypting [%d] bytes.\n", size);
Michael Halcrow8bba0662006-10-30 22:07:18 -0800752 rc = crypto_blkcipher_decrypt_iv(&desc, dest_sg, src_sg, size);
Michael Halcrow237fead2006-10-04 02:16:22 -0700753 mutex_unlock(&crypt_stat->cs_tfm_mutex);
754 if (rc) {
755 ecryptfs_printk(KERN_ERR, "Error decrypting; rc = [%d]\n",
756 rc);
757 goto out;
758 }
759 rc = size;
760out:
761 return rc;
762}
763
764/**
765 * ecryptfs_encrypt_page_offset
766 *
767 * Returns the number of bytes encrypted
768 */
769static int
770ecryptfs_encrypt_page_offset(struct ecryptfs_crypt_stat *crypt_stat,
771 struct page *dst_page, int dst_offset,
772 struct page *src_page, int src_offset, int size,
773 unsigned char *iv)
774{
775 struct scatterlist src_sg, dst_sg;
776
777 src_sg.page = src_page;
778 src_sg.offset = src_offset;
779 src_sg.length = size;
780 dst_sg.page = dst_page;
781 dst_sg.offset = dst_offset;
782 dst_sg.length = size;
783 return encrypt_scatterlist(crypt_stat, &dst_sg, &src_sg, size, iv);
784}
785
786/**
787 * ecryptfs_decrypt_page_offset
788 *
789 * Returns the number of bytes decrypted
790 */
791static int
792ecryptfs_decrypt_page_offset(struct ecryptfs_crypt_stat *crypt_stat,
793 struct page *dst_page, int dst_offset,
794 struct page *src_page, int src_offset, int size,
795 unsigned char *iv)
796{
797 struct scatterlist src_sg, dst_sg;
798
799 src_sg.page = src_page;
800 src_sg.offset = src_offset;
801 src_sg.length = size;
802 dst_sg.page = dst_page;
803 dst_sg.offset = dst_offset;
804 dst_sg.length = size;
805 return decrypt_scatterlist(crypt_stat, &dst_sg, &src_sg, size, iv);
806}
807
808#define ECRYPTFS_MAX_SCATTERLIST_LEN 4
809
810/**
811 * ecryptfs_init_crypt_ctx
812 * @crypt_stat: Uninitilized crypt stats structure
813 *
814 * Initialize the crypto context.
815 *
816 * TODO: Performance: Keep a cache of initialized cipher contexts;
817 * only init if needed
818 */
819int ecryptfs_init_crypt_ctx(struct ecryptfs_crypt_stat *crypt_stat)
820{
Michael Halcrow8bba0662006-10-30 22:07:18 -0800821 char *full_alg_name;
Michael Halcrow237fead2006-10-04 02:16:22 -0700822 int rc = -EINVAL;
823
824 if (!crypt_stat->cipher) {
825 ecryptfs_printk(KERN_ERR, "No cipher specified\n");
826 goto out;
827 }
828 ecryptfs_printk(KERN_DEBUG,
829 "Initializing cipher [%s]; strlen = [%d]; "
830 "key_size_bits = [%d]\n",
831 crypt_stat->cipher, (int)strlen(crypt_stat->cipher),
832 crypt_stat->key_size << 3);
833 if (crypt_stat->tfm) {
834 rc = 0;
835 goto out;
836 }
837 mutex_lock(&crypt_stat->cs_tfm_mutex);
Michael Halcrow8bba0662006-10-30 22:07:18 -0800838 rc = ecryptfs_crypto_api_algify_cipher_name(&full_alg_name,
839 crypt_stat->cipher, "cbc");
840 if (rc)
841 goto out;
842 crypt_stat->tfm = crypto_alloc_blkcipher(full_alg_name, 0,
843 CRYPTO_ALG_ASYNC);
844 kfree(full_alg_name);
Akinobu Mitade887772006-11-28 12:29:49 -0800845 if (IS_ERR(crypt_stat->tfm)) {
846 rc = PTR_ERR(crypt_stat->tfm);
Michael Halcrow237fead2006-10-04 02:16:22 -0700847 ecryptfs_printk(KERN_ERR, "cryptfs: init_crypt_ctx(): "
848 "Error initializing cipher [%s]\n",
849 crypt_stat->cipher);
Michael Halcrow8bba0662006-10-30 22:07:18 -0800850 mutex_unlock(&crypt_stat->cs_tfm_mutex);
Michael Halcrow237fead2006-10-04 02:16:22 -0700851 goto out;
852 }
Herbert Xuf1ddcaf2007-01-27 10:05:15 +1100853 crypto_blkcipher_set_flags(crypt_stat->tfm, CRYPTO_TFM_REQ_WEAK_KEY);
Michael Halcrow8bba0662006-10-30 22:07:18 -0800854 mutex_unlock(&crypt_stat->cs_tfm_mutex);
Michael Halcrow237fead2006-10-04 02:16:22 -0700855 rc = 0;
856out:
857 return rc;
858}
859
860static void set_extent_mask_and_shift(struct ecryptfs_crypt_stat *crypt_stat)
861{
862 int extent_size_tmp;
863
864 crypt_stat->extent_mask = 0xFFFFFFFF;
865 crypt_stat->extent_shift = 0;
866 if (crypt_stat->extent_size == 0)
867 return;
868 extent_size_tmp = crypt_stat->extent_size;
869 while ((extent_size_tmp & 0x01) == 0) {
870 extent_size_tmp >>= 1;
871 crypt_stat->extent_mask <<= 1;
872 crypt_stat->extent_shift++;
873 }
874}
875
876void ecryptfs_set_default_sizes(struct ecryptfs_crypt_stat *crypt_stat)
877{
878 /* Default values; may be overwritten as we are parsing the
879 * packets. */
880 crypt_stat->extent_size = ECRYPTFS_DEFAULT_EXTENT_SIZE;
881 set_extent_mask_and_shift(crypt_stat);
882 crypt_stat->iv_bytes = ECRYPTFS_DEFAULT_IV_BYTES;
883 if (PAGE_CACHE_SIZE <= ECRYPTFS_MINIMUM_HEADER_EXTENT_SIZE) {
884 crypt_stat->header_extent_size =
885 ECRYPTFS_MINIMUM_HEADER_EXTENT_SIZE;
886 } else
887 crypt_stat->header_extent_size = PAGE_CACHE_SIZE;
Michael Halcrowdd2a3b72007-02-12 00:53:46 -0800888 if (crypt_stat->flags & ECRYPTFS_METADATA_IN_XATTR)
889 crypt_stat->num_header_extents_at_front = 0;
890 else
891 crypt_stat->num_header_extents_at_front = 1;
Michael Halcrow237fead2006-10-04 02:16:22 -0700892}
893
894/**
895 * ecryptfs_compute_root_iv
896 * @crypt_stats
897 *
898 * On error, sets the root IV to all 0's.
899 */
900int ecryptfs_compute_root_iv(struct ecryptfs_crypt_stat *crypt_stat)
901{
902 int rc = 0;
903 char dst[MD5_DIGEST_SIZE];
904
905 BUG_ON(crypt_stat->iv_bytes > MD5_DIGEST_SIZE);
906 BUG_ON(crypt_stat->iv_bytes <= 0);
Michael Halcrowe2bd99e2007-02-12 00:53:49 -0800907 if (!(crypt_stat->flags & ECRYPTFS_KEY_VALID)) {
Michael Halcrow237fead2006-10-04 02:16:22 -0700908 rc = -EINVAL;
909 ecryptfs_printk(KERN_WARNING, "Session key not valid; "
910 "cannot generate root IV\n");
911 goto out;
912 }
913 rc = ecryptfs_calculate_md5(dst, crypt_stat, crypt_stat->key,
914 crypt_stat->key_size);
915 if (rc) {
916 ecryptfs_printk(KERN_WARNING, "Error attempting to compute "
917 "MD5 while generating root IV\n");
918 goto out;
919 }
920 memcpy(crypt_stat->root_iv, dst, crypt_stat->iv_bytes);
921out:
922 if (rc) {
923 memset(crypt_stat->root_iv, 0, crypt_stat->iv_bytes);
Michael Halcrowe2bd99e2007-02-12 00:53:49 -0800924 crypt_stat->flags |= ECRYPTFS_SECURITY_WARNING;
Michael Halcrow237fead2006-10-04 02:16:22 -0700925 }
926 return rc;
927}
928
929static void ecryptfs_generate_new_key(struct ecryptfs_crypt_stat *crypt_stat)
930{
931 get_random_bytes(crypt_stat->key, crypt_stat->key_size);
Michael Halcrowe2bd99e2007-02-12 00:53:49 -0800932 crypt_stat->flags |= ECRYPTFS_KEY_VALID;
Michael Halcrow237fead2006-10-04 02:16:22 -0700933 ecryptfs_compute_root_iv(crypt_stat);
934 if (unlikely(ecryptfs_verbosity > 0)) {
935 ecryptfs_printk(KERN_DEBUG, "Generated new session key:\n");
936 ecryptfs_dump_hex(crypt_stat->key,
937 crypt_stat->key_size);
938 }
939}
940
941/**
Michael Halcrow17398952007-02-12 00:53:45 -0800942 * ecryptfs_copy_mount_wide_flags_to_inode_flags
943 *
944 * This function propagates the mount-wide flags to individual inode
945 * flags.
946 */
947static void ecryptfs_copy_mount_wide_flags_to_inode_flags(
948 struct ecryptfs_crypt_stat *crypt_stat,
949 struct ecryptfs_mount_crypt_stat *mount_crypt_stat)
950{
951 if (mount_crypt_stat->flags & ECRYPTFS_XATTR_METADATA_ENABLED)
952 crypt_stat->flags |= ECRYPTFS_METADATA_IN_XATTR;
953 if (mount_crypt_stat->flags & ECRYPTFS_ENCRYPTED_VIEW_ENABLED)
954 crypt_stat->flags |= ECRYPTFS_VIEW_AS_ENCRYPTED;
955}
956
Michael Halcrowf4aad162007-10-16 01:27:53 -0700957static int ecryptfs_copy_mount_wide_sigs_to_inode_sigs(
958 struct ecryptfs_crypt_stat *crypt_stat,
959 struct ecryptfs_mount_crypt_stat *mount_crypt_stat)
960{
961 struct ecryptfs_global_auth_tok *global_auth_tok;
962 int rc = 0;
963
964 mutex_lock(&mount_crypt_stat->global_auth_tok_list_mutex);
965 list_for_each_entry(global_auth_tok,
966 &mount_crypt_stat->global_auth_tok_list,
967 mount_crypt_stat_list) {
968 rc = ecryptfs_add_keysig(crypt_stat, global_auth_tok->sig);
969 if (rc) {
970 printk(KERN_ERR "Error adding keysig; rc = [%d]\n", rc);
971 mutex_unlock(
972 &mount_crypt_stat->global_auth_tok_list_mutex);
973 goto out;
974 }
975 }
976 mutex_unlock(&mount_crypt_stat->global_auth_tok_list_mutex);
977out:
978 return rc;
979}
980
Michael Halcrow17398952007-02-12 00:53:45 -0800981/**
Michael Halcrow237fead2006-10-04 02:16:22 -0700982 * ecryptfs_set_default_crypt_stat_vals
983 * @crypt_stat
984 *
985 * Default values in the event that policy does not override them.
986 */
987static void ecryptfs_set_default_crypt_stat_vals(
988 struct ecryptfs_crypt_stat *crypt_stat,
989 struct ecryptfs_mount_crypt_stat *mount_crypt_stat)
990{
Michael Halcrow17398952007-02-12 00:53:45 -0800991 ecryptfs_copy_mount_wide_flags_to_inode_flags(crypt_stat,
992 mount_crypt_stat);
Michael Halcrow237fead2006-10-04 02:16:22 -0700993 ecryptfs_set_default_sizes(crypt_stat);
994 strcpy(crypt_stat->cipher, ECRYPTFS_DEFAULT_CIPHER);
995 crypt_stat->key_size = ECRYPTFS_DEFAULT_KEY_BYTES;
Michael Halcrowe2bd99e2007-02-12 00:53:49 -0800996 crypt_stat->flags &= ~(ECRYPTFS_KEY_VALID);
Michael Halcrow237fead2006-10-04 02:16:22 -0700997 crypt_stat->file_version = ECRYPTFS_FILE_VERSION;
998 crypt_stat->mount_crypt_stat = mount_crypt_stat;
999}
1000
1001/**
1002 * ecryptfs_new_file_context
1003 * @ecryptfs_dentry
1004 *
1005 * If the crypto context for the file has not yet been established,
1006 * this is where we do that. Establishing a new crypto context
1007 * involves the following decisions:
1008 * - What cipher to use?
1009 * - What set of authentication tokens to use?
1010 * Here we just worry about getting enough information into the
1011 * authentication tokens so that we know that they are available.
1012 * We associate the available authentication tokens with the new file
1013 * via the set of signatures in the crypt_stat struct. Later, when
1014 * the headers are actually written out, we may again defer to
1015 * userspace to perform the encryption of the session key; for the
1016 * foreseeable future, this will be the case with public key packets.
1017 *
1018 * Returns zero on success; non-zero otherwise
1019 */
1020/* Associate an authentication token(s) with the file */
1021int ecryptfs_new_file_context(struct dentry *ecryptfs_dentry)
1022{
Michael Halcrow237fead2006-10-04 02:16:22 -07001023 struct ecryptfs_crypt_stat *crypt_stat =
1024 &ecryptfs_inode_to_private(ecryptfs_dentry->d_inode)->crypt_stat;
1025 struct ecryptfs_mount_crypt_stat *mount_crypt_stat =
1026 &ecryptfs_superblock_to_private(
1027 ecryptfs_dentry->d_sb)->mount_crypt_stat;
1028 int cipher_name_len;
Michael Halcrowf4aad162007-10-16 01:27:53 -07001029 int rc = 0;
Michael Halcrow237fead2006-10-04 02:16:22 -07001030
1031 ecryptfs_set_default_crypt_stat_vals(crypt_stat, mount_crypt_stat);
Michael Halcrowf4aad162007-10-16 01:27:53 -07001032 mutex_lock(&mount_crypt_stat->global_auth_tok_list_mutex);
1033 BUG_ON(mount_crypt_stat->num_global_auth_toks == 0);
1034 mutex_unlock(&mount_crypt_stat->global_auth_tok_list_mutex);
1035 crypt_stat->flags |= ECRYPTFS_ENCRYPTED;
1036 crypt_stat->flags |= ECRYPTFS_KEY_VALID;
1037 ecryptfs_copy_mount_wide_flags_to_inode_flags(crypt_stat,
1038 mount_crypt_stat);
1039 rc = ecryptfs_copy_mount_wide_sigs_to_inode_sigs(crypt_stat,
1040 mount_crypt_stat);
1041 if (rc) {
1042 printk(KERN_ERR "Error attempting to copy mount-wide key sigs "
1043 "to the inode key sigs; rc = [%d]\n", rc);
1044 goto out;
1045 }
1046 cipher_name_len =
1047 strlen(mount_crypt_stat->global_default_cipher_name);
1048 memcpy(crypt_stat->cipher,
1049 mount_crypt_stat->global_default_cipher_name,
1050 cipher_name_len);
1051 crypt_stat->cipher[cipher_name_len] = '\0';
1052 crypt_stat->key_size =
1053 mount_crypt_stat->global_default_cipher_key_size;
1054 ecryptfs_generate_new_key(crypt_stat);
Michael Halcrow237fead2006-10-04 02:16:22 -07001055 rc = ecryptfs_init_crypt_ctx(crypt_stat);
1056 if (rc)
1057 ecryptfs_printk(KERN_ERR, "Error initializing cryptographic "
1058 "context for cipher [%s]: rc = [%d]\n",
1059 crypt_stat->cipher, rc);
Michael Halcrowf4aad162007-10-16 01:27:53 -07001060out:
Michael Halcrow237fead2006-10-04 02:16:22 -07001061 return rc;
1062}
1063
1064/**
1065 * contains_ecryptfs_marker - check for the ecryptfs marker
1066 * @data: The data block in which to check
1067 *
1068 * Returns one if marker found; zero if not found
1069 */
Michael Halcrowdd2a3b72007-02-12 00:53:46 -08001070static int contains_ecryptfs_marker(char *data)
Michael Halcrow237fead2006-10-04 02:16:22 -07001071{
1072 u32 m_1, m_2;
1073
1074 memcpy(&m_1, data, 4);
1075 m_1 = be32_to_cpu(m_1);
1076 memcpy(&m_2, (data + 4), 4);
1077 m_2 = be32_to_cpu(m_2);
1078 if ((m_1 ^ MAGIC_ECRYPTFS_MARKER) == m_2)
1079 return 1;
1080 ecryptfs_printk(KERN_DEBUG, "m_1 = [0x%.8x]; m_2 = [0x%.8x]; "
1081 "MAGIC_ECRYPTFS_MARKER = [0x%.8x]\n", m_1, m_2,
1082 MAGIC_ECRYPTFS_MARKER);
1083 ecryptfs_printk(KERN_DEBUG, "(m_1 ^ MAGIC_ECRYPTFS_MARKER) = "
1084 "[0x%.8x]\n", (m_1 ^ MAGIC_ECRYPTFS_MARKER));
1085 return 0;
1086}
1087
1088struct ecryptfs_flag_map_elem {
1089 u32 file_flag;
1090 u32 local_flag;
1091};
1092
1093/* Add support for additional flags by adding elements here. */
1094static struct ecryptfs_flag_map_elem ecryptfs_flag_map[] = {
1095 {0x00000001, ECRYPTFS_ENABLE_HMAC},
Michael Halcrowdd2a3b72007-02-12 00:53:46 -08001096 {0x00000002, ECRYPTFS_ENCRYPTED},
1097 {0x00000004, ECRYPTFS_METADATA_IN_XATTR}
Michael Halcrow237fead2006-10-04 02:16:22 -07001098};
1099
1100/**
1101 * ecryptfs_process_flags
1102 * @crypt_stat
1103 * @page_virt: Source data to be parsed
1104 * @bytes_read: Updated with the number of bytes read
1105 *
1106 * Returns zero on success; non-zero if the flag set is invalid
1107 */
1108static int ecryptfs_process_flags(struct ecryptfs_crypt_stat *crypt_stat,
1109 char *page_virt, int *bytes_read)
1110{
1111 int rc = 0;
1112 int i;
1113 u32 flags;
1114
1115 memcpy(&flags, page_virt, 4);
1116 flags = be32_to_cpu(flags);
1117 for (i = 0; i < ((sizeof(ecryptfs_flag_map)
1118 / sizeof(struct ecryptfs_flag_map_elem))); i++)
1119 if (flags & ecryptfs_flag_map[i].file_flag) {
Michael Halcrowe2bd99e2007-02-12 00:53:49 -08001120 crypt_stat->flags |= ecryptfs_flag_map[i].local_flag;
Michael Halcrow237fead2006-10-04 02:16:22 -07001121 } else
Michael Halcrowe2bd99e2007-02-12 00:53:49 -08001122 crypt_stat->flags &= ~(ecryptfs_flag_map[i].local_flag);
Michael Halcrow237fead2006-10-04 02:16:22 -07001123 /* Version is in top 8 bits of the 32-bit flag vector */
1124 crypt_stat->file_version = ((flags >> 24) & 0xFF);
1125 (*bytes_read) = 4;
1126 return rc;
1127}
1128
1129/**
1130 * write_ecryptfs_marker
1131 * @page_virt: The pointer to in a page to begin writing the marker
1132 * @written: Number of bytes written
1133 *
1134 * Marker = 0x3c81b7f5
1135 */
1136static void write_ecryptfs_marker(char *page_virt, size_t *written)
1137{
1138 u32 m_1, m_2;
1139
1140 get_random_bytes(&m_1, (MAGIC_ECRYPTFS_MARKER_SIZE_BYTES / 2));
1141 m_2 = (m_1 ^ MAGIC_ECRYPTFS_MARKER);
1142 m_1 = cpu_to_be32(m_1);
1143 memcpy(page_virt, &m_1, (MAGIC_ECRYPTFS_MARKER_SIZE_BYTES / 2));
1144 m_2 = cpu_to_be32(m_2);
1145 memcpy(page_virt + (MAGIC_ECRYPTFS_MARKER_SIZE_BYTES / 2), &m_2,
1146 (MAGIC_ECRYPTFS_MARKER_SIZE_BYTES / 2));
1147 (*written) = MAGIC_ECRYPTFS_MARKER_SIZE_BYTES;
1148}
1149
1150static void
1151write_ecryptfs_flags(char *page_virt, struct ecryptfs_crypt_stat *crypt_stat,
1152 size_t *written)
1153{
1154 u32 flags = 0;
1155 int i;
1156
1157 for (i = 0; i < ((sizeof(ecryptfs_flag_map)
1158 / sizeof(struct ecryptfs_flag_map_elem))); i++)
Michael Halcrowe2bd99e2007-02-12 00:53:49 -08001159 if (crypt_stat->flags & ecryptfs_flag_map[i].local_flag)
Michael Halcrow237fead2006-10-04 02:16:22 -07001160 flags |= ecryptfs_flag_map[i].file_flag;
1161 /* Version is in top 8 bits of the 32-bit flag vector */
1162 flags |= ((((u8)crypt_stat->file_version) << 24) & 0xFF000000);
1163 flags = cpu_to_be32(flags);
1164 memcpy(page_virt, &flags, 4);
1165 (*written) = 4;
1166}
1167
1168struct ecryptfs_cipher_code_str_map_elem {
1169 char cipher_str[16];
1170 u16 cipher_code;
1171};
1172
1173/* Add support for additional ciphers by adding elements here. The
1174 * cipher_code is whatever OpenPGP applicatoins use to identify the
1175 * ciphers. List in order of probability. */
1176static struct ecryptfs_cipher_code_str_map_elem
1177ecryptfs_cipher_code_str_map[] = {
1178 {"aes",RFC2440_CIPHER_AES_128 },
1179 {"blowfish", RFC2440_CIPHER_BLOWFISH},
1180 {"des3_ede", RFC2440_CIPHER_DES3_EDE},
1181 {"cast5", RFC2440_CIPHER_CAST_5},
1182 {"twofish", RFC2440_CIPHER_TWOFISH},
1183 {"cast6", RFC2440_CIPHER_CAST_6},
1184 {"aes", RFC2440_CIPHER_AES_192},
1185 {"aes", RFC2440_CIPHER_AES_256}
1186};
1187
1188/**
1189 * ecryptfs_code_for_cipher_string
1190 * @str: The string representing the cipher name
1191 *
1192 * Returns zero on no match, or the cipher code on match
1193 */
1194u16 ecryptfs_code_for_cipher_string(struct ecryptfs_crypt_stat *crypt_stat)
1195{
1196 int i;
1197 u16 code = 0;
1198 struct ecryptfs_cipher_code_str_map_elem *map =
1199 ecryptfs_cipher_code_str_map;
1200
1201 if (strcmp(crypt_stat->cipher, "aes") == 0) {
1202 switch (crypt_stat->key_size) {
1203 case 16:
1204 code = RFC2440_CIPHER_AES_128;
1205 break;
1206 case 24:
1207 code = RFC2440_CIPHER_AES_192;
1208 break;
1209 case 32:
1210 code = RFC2440_CIPHER_AES_256;
1211 }
1212 } else {
1213 for (i = 0; i < ARRAY_SIZE(ecryptfs_cipher_code_str_map); i++)
1214 if (strcmp(crypt_stat->cipher, map[i].cipher_str) == 0){
1215 code = map[i].cipher_code;
1216 break;
1217 }
1218 }
1219 return code;
1220}
1221
1222/**
1223 * ecryptfs_cipher_code_to_string
1224 * @str: Destination to write out the cipher name
1225 * @cipher_code: The code to convert to cipher name string
1226 *
1227 * Returns zero on success
1228 */
1229int ecryptfs_cipher_code_to_string(char *str, u16 cipher_code)
1230{
1231 int rc = 0;
1232 int i;
1233
1234 str[0] = '\0';
1235 for (i = 0; i < ARRAY_SIZE(ecryptfs_cipher_code_str_map); i++)
1236 if (cipher_code == ecryptfs_cipher_code_str_map[i].cipher_code)
1237 strcpy(str, ecryptfs_cipher_code_str_map[i].cipher_str);
1238 if (str[0] == '\0') {
1239 ecryptfs_printk(KERN_WARNING, "Cipher code not recognized: "
1240 "[%d]\n", cipher_code);
1241 rc = -EINVAL;
1242 }
1243 return rc;
1244}
1245
1246/**
1247 * ecryptfs_read_header_region
1248 * @data
1249 * @dentry
1250 * @nd
1251 *
1252 * Returns zero on success; non-zero otherwise
1253 */
Michael Halcrowdd2a3b72007-02-12 00:53:46 -08001254static int ecryptfs_read_header_region(char *data, struct dentry *dentry,
1255 struct vfsmount *mnt)
Michael Halcrow237fead2006-10-04 02:16:22 -07001256{
Michael Halcrow7ff1d742006-10-30 22:07:19 -08001257 struct file *lower_file;
Michael Halcrow237fead2006-10-04 02:16:22 -07001258 mm_segment_t oldfs;
1259 int rc;
1260
Michael Halcrow7ff1d742006-10-30 22:07:19 -08001261 if ((rc = ecryptfs_open_lower_file(&lower_file, dentry, mnt,
1262 O_RDONLY))) {
1263 printk(KERN_ERR
1264 "Error opening lower_file to read header region\n");
Michael Halcrow237fead2006-10-04 02:16:22 -07001265 goto out;
1266 }
Michael Halcrow7ff1d742006-10-30 22:07:19 -08001267 lower_file->f_pos = 0;
Michael Halcrow237fead2006-10-04 02:16:22 -07001268 oldfs = get_fs();
1269 set_fs(get_ds());
1270 /* For releases 0.1 and 0.2, all of the header information
1271 * fits in the first data extent-sized region. */
Michael Halcrow7ff1d742006-10-30 22:07:19 -08001272 rc = lower_file->f_op->read(lower_file, (char __user *)data,
1273 ECRYPTFS_DEFAULT_EXTENT_SIZE, &lower_file->f_pos);
Michael Halcrow237fead2006-10-04 02:16:22 -07001274 set_fs(oldfs);
Michael Halcrow7ff1d742006-10-30 22:07:19 -08001275 if ((rc = ecryptfs_close_lower_file(lower_file))) {
1276 printk(KERN_ERR "Error closing lower_file\n");
1277 goto out;
1278 }
Michael Halcrow237fead2006-10-04 02:16:22 -07001279 rc = 0;
1280out:
1281 return rc;
1282}
1283
Michael Halcrowdd2a3b72007-02-12 00:53:46 -08001284int ecryptfs_read_and_validate_header_region(char *data, struct dentry *dentry,
1285 struct vfsmount *mnt)
1286{
1287 int rc;
1288
1289 rc = ecryptfs_read_header_region(data, dentry, mnt);
1290 if (rc)
1291 goto out;
1292 if (!contains_ecryptfs_marker(data + ECRYPTFS_FILE_SIZE_BYTES))
1293 rc = -EINVAL;
1294out:
1295 return rc;
1296}
1297
1298
Michael Halcrowe77a56d2007-02-12 00:53:47 -08001299void
1300ecryptfs_write_header_metadata(char *virt,
1301 struct ecryptfs_crypt_stat *crypt_stat,
1302 size_t *written)
Michael Halcrow237fead2006-10-04 02:16:22 -07001303{
1304 u32 header_extent_size;
1305 u16 num_header_extents_at_front;
1306
1307 header_extent_size = (u32)crypt_stat->header_extent_size;
1308 num_header_extents_at_front =
1309 (u16)crypt_stat->num_header_extents_at_front;
1310 header_extent_size = cpu_to_be32(header_extent_size);
1311 memcpy(virt, &header_extent_size, 4);
1312 virt += 4;
1313 num_header_extents_at_front = cpu_to_be16(num_header_extents_at_front);
1314 memcpy(virt, &num_header_extents_at_front, 2);
1315 (*written) = 6;
1316}
1317
1318struct kmem_cache *ecryptfs_header_cache_0;
1319struct kmem_cache *ecryptfs_header_cache_1;
1320struct kmem_cache *ecryptfs_header_cache_2;
1321
1322/**
1323 * ecryptfs_write_headers_virt
1324 * @page_virt
1325 * @crypt_stat
1326 * @ecryptfs_dentry
1327 *
1328 * Format version: 1
1329 *
1330 * Header Extent:
1331 * Octets 0-7: Unencrypted file size (big-endian)
1332 * Octets 8-15: eCryptfs special marker
1333 * Octets 16-19: Flags
1334 * Octet 16: File format version number (between 0 and 255)
1335 * Octets 17-18: Reserved
1336 * Octet 19: Bit 1 (lsb): Reserved
1337 * Bit 2: Encrypted?
1338 * Bits 3-8: Reserved
1339 * Octets 20-23: Header extent size (big-endian)
1340 * Octets 24-25: Number of header extents at front of file
1341 * (big-endian)
1342 * Octet 26: Begin RFC 2440 authentication token packet set
1343 * Data Extent 0:
1344 * Lower data (CBC encrypted)
1345 * Data Extent 1:
1346 * Lower data (CBC encrypted)
1347 * ...
1348 *
1349 * Returns zero on success
1350 */
Michael Halcrowdd2a3b72007-02-12 00:53:46 -08001351static int ecryptfs_write_headers_virt(char *page_virt, size_t *size,
1352 struct ecryptfs_crypt_stat *crypt_stat,
1353 struct dentry *ecryptfs_dentry)
Michael Halcrow237fead2006-10-04 02:16:22 -07001354{
1355 int rc;
1356 size_t written;
1357 size_t offset;
1358
1359 offset = ECRYPTFS_FILE_SIZE_BYTES;
1360 write_ecryptfs_marker((page_virt + offset), &written);
1361 offset += written;
1362 write_ecryptfs_flags((page_virt + offset), crypt_stat, &written);
1363 offset += written;
Michael Halcrowe77a56d2007-02-12 00:53:47 -08001364 ecryptfs_write_header_metadata((page_virt + offset), crypt_stat,
1365 &written);
Michael Halcrow237fead2006-10-04 02:16:22 -07001366 offset += written;
1367 rc = ecryptfs_generate_key_packet_set((page_virt + offset), crypt_stat,
1368 ecryptfs_dentry, &written,
1369 PAGE_CACHE_SIZE - offset);
1370 if (rc)
1371 ecryptfs_printk(KERN_WARNING, "Error generating key packet "
1372 "set; rc = [%d]\n", rc);
Michael Halcrowdd2a3b72007-02-12 00:53:46 -08001373 if (size) {
1374 offset += written;
1375 *size = offset;
1376 }
1377 return rc;
1378}
1379
1380static int ecryptfs_write_metadata_to_contents(struct ecryptfs_crypt_stat *crypt_stat,
1381 struct file *lower_file,
1382 char *page_virt)
1383{
1384 mm_segment_t oldfs;
1385 int current_header_page;
1386 int header_pages;
Michael Halcrow70456602007-02-12 00:53:48 -08001387 ssize_t size;
1388 int rc = 0;
Michael Halcrowdd2a3b72007-02-12 00:53:46 -08001389
1390 lower_file->f_pos = 0;
1391 oldfs = get_fs();
1392 set_fs(get_ds());
Michael Halcrow70456602007-02-12 00:53:48 -08001393 size = vfs_write(lower_file, (char __user *)page_virt, PAGE_CACHE_SIZE,
1394 &lower_file->f_pos);
1395 if (size < 0) {
1396 rc = (int)size;
1397 printk(KERN_ERR "Error attempting to write lower page; "
1398 "rc = [%d]\n", rc);
1399 set_fs(oldfs);
1400 goto out;
1401 }
Michael Halcrowdd2a3b72007-02-12 00:53:46 -08001402 header_pages = ((crypt_stat->header_extent_size
1403 * crypt_stat->num_header_extents_at_front)
1404 / PAGE_CACHE_SIZE);
1405 memset(page_virt, 0, PAGE_CACHE_SIZE);
1406 current_header_page = 1;
1407 while (current_header_page < header_pages) {
Michael Halcrow70456602007-02-12 00:53:48 -08001408 size = vfs_write(lower_file, (char __user *)page_virt,
1409 PAGE_CACHE_SIZE, &lower_file->f_pos);
1410 if (size < 0) {
1411 rc = (int)size;
1412 printk(KERN_ERR "Error attempting to write lower page; "
1413 "rc = [%d]\n", rc);
1414 set_fs(oldfs);
1415 goto out;
1416 }
Michael Halcrowdd2a3b72007-02-12 00:53:46 -08001417 current_header_page++;
1418 }
1419 set_fs(oldfs);
Michael Halcrow70456602007-02-12 00:53:48 -08001420out:
1421 return rc;
Michael Halcrowdd2a3b72007-02-12 00:53:46 -08001422}
1423
1424static int ecryptfs_write_metadata_to_xattr(struct dentry *ecryptfs_dentry,
1425 struct ecryptfs_crypt_stat *crypt_stat,
1426 char *page_virt, size_t size)
1427{
1428 int rc;
1429
1430 rc = ecryptfs_setxattr(ecryptfs_dentry, ECRYPTFS_XATTR_NAME, page_virt,
1431 size, 0);
Michael Halcrow237fead2006-10-04 02:16:22 -07001432 return rc;
1433}
1434
1435/**
Michael Halcrowdd2a3b72007-02-12 00:53:46 -08001436 * ecryptfs_write_metadata
Michael Halcrow237fead2006-10-04 02:16:22 -07001437 * @lower_file: The lower file struct, which was returned from dentry_open
1438 *
1439 * Write the file headers out. This will likely involve a userspace
1440 * callout, in which the session key is encrypted with one or more
1441 * public keys and/or the passphrase necessary to do the encryption is
1442 * retrieved via a prompt. Exactly what happens at this point should
1443 * be policy-dependent.
1444 *
1445 * Returns zero on success; non-zero on error
1446 */
Michael Halcrowdd2a3b72007-02-12 00:53:46 -08001447int ecryptfs_write_metadata(struct dentry *ecryptfs_dentry,
1448 struct file *lower_file)
Michael Halcrow237fead2006-10-04 02:16:22 -07001449{
Michael Halcrow237fead2006-10-04 02:16:22 -07001450 struct ecryptfs_crypt_stat *crypt_stat;
1451 char *page_virt;
Michael Halcrowdd2a3b72007-02-12 00:53:46 -08001452 size_t size;
Michael Halcrow237fead2006-10-04 02:16:22 -07001453 int rc = 0;
1454
1455 crypt_stat = &ecryptfs_inode_to_private(
1456 ecryptfs_dentry->d_inode)->crypt_stat;
Michael Halcrowe2bd99e2007-02-12 00:53:49 -08001457 if (likely(crypt_stat->flags & ECRYPTFS_ENCRYPTED)) {
1458 if (!(crypt_stat->flags & ECRYPTFS_KEY_VALID)) {
Michael Halcrow237fead2006-10-04 02:16:22 -07001459 ecryptfs_printk(KERN_DEBUG, "Key is "
1460 "invalid; bailing out\n");
1461 rc = -EINVAL;
1462 goto out;
1463 }
1464 } else {
1465 rc = -EINVAL;
1466 ecryptfs_printk(KERN_WARNING,
1467 "Called with crypt_stat->encrypted == 0\n");
1468 goto out;
1469 }
1470 /* Released in this function */
Robert P. J. Dayc3762222007-02-10 01:45:03 -08001471 page_virt = kmem_cache_zalloc(ecryptfs_header_cache_0, GFP_USER);
Michael Halcrow237fead2006-10-04 02:16:22 -07001472 if (!page_virt) {
1473 ecryptfs_printk(KERN_ERR, "Out of memory\n");
1474 rc = -ENOMEM;
1475 goto out;
1476 }
Michael Halcrowdd2a3b72007-02-12 00:53:46 -08001477 rc = ecryptfs_write_headers_virt(page_virt, &size, crypt_stat,
1478 ecryptfs_dentry);
Michael Halcrow237fead2006-10-04 02:16:22 -07001479 if (unlikely(rc)) {
1480 ecryptfs_printk(KERN_ERR, "Error whilst writing headers\n");
1481 memset(page_virt, 0, PAGE_CACHE_SIZE);
1482 goto out_free;
1483 }
Michael Halcrowdd2a3b72007-02-12 00:53:46 -08001484 if (crypt_stat->flags & ECRYPTFS_METADATA_IN_XATTR)
1485 rc = ecryptfs_write_metadata_to_xattr(ecryptfs_dentry,
1486 crypt_stat, page_virt,
1487 size);
1488 else
1489 rc = ecryptfs_write_metadata_to_contents(crypt_stat, lower_file,
1490 page_virt);
1491 if (rc) {
1492 printk(KERN_ERR "Error writing metadata out to lower file; "
1493 "rc = [%d]\n", rc);
1494 goto out_free;
Michael Halcrow237fead2006-10-04 02:16:22 -07001495 }
Michael Halcrow237fead2006-10-04 02:16:22 -07001496out_free:
1497 kmem_cache_free(ecryptfs_header_cache_0, page_virt);
1498out:
1499 return rc;
1500}
1501
Michael Halcrowdd2a3b72007-02-12 00:53:46 -08001502#define ECRYPTFS_DONT_VALIDATE_HEADER_SIZE 0
1503#define ECRYPTFS_VALIDATE_HEADER_SIZE 1
Michael Halcrow237fead2006-10-04 02:16:22 -07001504static int parse_header_metadata(struct ecryptfs_crypt_stat *crypt_stat,
Michael Halcrowdd2a3b72007-02-12 00:53:46 -08001505 char *virt, int *bytes_read,
1506 int validate_header_size)
Michael Halcrow237fead2006-10-04 02:16:22 -07001507{
1508 int rc = 0;
1509 u32 header_extent_size;
1510 u16 num_header_extents_at_front;
1511
1512 memcpy(&header_extent_size, virt, 4);
1513 header_extent_size = be32_to_cpu(header_extent_size);
1514 virt += 4;
1515 memcpy(&num_header_extents_at_front, virt, 2);
1516 num_header_extents_at_front = be16_to_cpu(num_header_extents_at_front);
1517 crypt_stat->header_extent_size = (int)header_extent_size;
1518 crypt_stat->num_header_extents_at_front =
1519 (int)num_header_extents_at_front;
1520 (*bytes_read) = 6;
Michael Halcrowdd2a3b72007-02-12 00:53:46 -08001521 if ((validate_header_size == ECRYPTFS_VALIDATE_HEADER_SIZE)
1522 && ((crypt_stat->header_extent_size
1523 * crypt_stat->num_header_extents_at_front)
1524 < ECRYPTFS_MINIMUM_HEADER_EXTENT_SIZE)) {
Michael Halcrow237fead2006-10-04 02:16:22 -07001525 rc = -EINVAL;
1526 ecryptfs_printk(KERN_WARNING, "Invalid header extent size: "
1527 "[%d]\n", crypt_stat->header_extent_size);
1528 }
1529 return rc;
1530}
1531
1532/**
1533 * set_default_header_data
1534 *
1535 * For version 0 file format; this function is only for backwards
1536 * compatibility for files created with the prior versions of
1537 * eCryptfs.
1538 */
1539static void set_default_header_data(struct ecryptfs_crypt_stat *crypt_stat)
1540{
1541 crypt_stat->header_extent_size = 4096;
1542 crypt_stat->num_header_extents_at_front = 1;
1543}
1544
1545/**
1546 * ecryptfs_read_headers_virt
1547 *
1548 * Read/parse the header data. The header format is detailed in the
1549 * comment block for the ecryptfs_write_headers_virt() function.
1550 *
1551 * Returns zero on success
1552 */
1553static int ecryptfs_read_headers_virt(char *page_virt,
1554 struct ecryptfs_crypt_stat *crypt_stat,
Michael Halcrowdd2a3b72007-02-12 00:53:46 -08001555 struct dentry *ecryptfs_dentry,
1556 int validate_header_size)
Michael Halcrow237fead2006-10-04 02:16:22 -07001557{
1558 int rc = 0;
1559 int offset;
1560 int bytes_read;
1561
1562 ecryptfs_set_default_sizes(crypt_stat);
1563 crypt_stat->mount_crypt_stat = &ecryptfs_superblock_to_private(
1564 ecryptfs_dentry->d_sb)->mount_crypt_stat;
1565 offset = ECRYPTFS_FILE_SIZE_BYTES;
1566 rc = contains_ecryptfs_marker(page_virt + offset);
1567 if (rc == 0) {
1568 rc = -EINVAL;
1569 goto out;
1570 }
1571 offset += MAGIC_ECRYPTFS_MARKER_SIZE_BYTES;
1572 rc = ecryptfs_process_flags(crypt_stat, (page_virt + offset),
1573 &bytes_read);
1574 if (rc) {
1575 ecryptfs_printk(KERN_WARNING, "Error processing flags\n");
1576 goto out;
1577 }
1578 if (crypt_stat->file_version > ECRYPTFS_SUPPORTED_FILE_VERSION) {
1579 ecryptfs_printk(KERN_WARNING, "File version is [%d]; only "
1580 "file version [%d] is supported by this "
1581 "version of eCryptfs\n",
1582 crypt_stat->file_version,
1583 ECRYPTFS_SUPPORTED_FILE_VERSION);
1584 rc = -EINVAL;
1585 goto out;
1586 }
1587 offset += bytes_read;
1588 if (crypt_stat->file_version >= 1) {
1589 rc = parse_header_metadata(crypt_stat, (page_virt + offset),
Michael Halcrowdd2a3b72007-02-12 00:53:46 -08001590 &bytes_read, validate_header_size);
Michael Halcrow237fead2006-10-04 02:16:22 -07001591 if (rc) {
1592 ecryptfs_printk(KERN_WARNING, "Error reading header "
1593 "metadata; rc = [%d]\n", rc);
1594 }
1595 offset += bytes_read;
1596 } else
1597 set_default_header_data(crypt_stat);
1598 rc = ecryptfs_parse_packet_set(crypt_stat, (page_virt + offset),
1599 ecryptfs_dentry);
1600out:
1601 return rc;
1602}
1603
1604/**
Michael Halcrowdd2a3b72007-02-12 00:53:46 -08001605 * ecryptfs_read_xattr_region
1606 *
1607 * Attempts to read the crypto metadata from the extended attribute
1608 * region of the lower file.
1609 */
1610int ecryptfs_read_xattr_region(char *page_virt, struct dentry *ecryptfs_dentry)
1611{
1612 ssize_t size;
1613 int rc = 0;
1614
1615 size = ecryptfs_getxattr(ecryptfs_dentry, ECRYPTFS_XATTR_NAME,
1616 page_virt, ECRYPTFS_DEFAULT_EXTENT_SIZE);
1617 if (size < 0) {
1618 printk(KERN_DEBUG "Error attempting to read the [%s] "
1619 "xattr from the lower file; return value = [%zd]\n",
1620 ECRYPTFS_XATTR_NAME, size);
1621 rc = -EINVAL;
1622 goto out;
1623 }
1624out:
1625 return rc;
1626}
1627
1628int ecryptfs_read_and_validate_xattr_region(char *page_virt,
1629 struct dentry *ecryptfs_dentry)
1630{
1631 int rc;
1632
1633 rc = ecryptfs_read_xattr_region(page_virt, ecryptfs_dentry);
1634 if (rc)
1635 goto out;
1636 if (!contains_ecryptfs_marker(page_virt + ECRYPTFS_FILE_SIZE_BYTES)) {
1637 printk(KERN_WARNING "Valid data found in [%s] xattr, but "
1638 "the marker is invalid\n", ECRYPTFS_XATTR_NAME);
1639 rc = -EINVAL;
1640 }
1641out:
1642 return rc;
1643}
1644
1645/**
1646 * ecryptfs_read_metadata
1647 *
1648 * Common entry point for reading file metadata. From here, we could
1649 * retrieve the header information from the header region of the file,
1650 * the xattr region of the file, or some other repostory that is
1651 * stored separately from the file itself. The current implementation
1652 * supports retrieving the metadata information from the file contents
1653 * and from the xattr region.
Michael Halcrow237fead2006-10-04 02:16:22 -07001654 *
1655 * Returns zero if valid headers found and parsed; non-zero otherwise
1656 */
Michael Halcrowdd2a3b72007-02-12 00:53:46 -08001657int ecryptfs_read_metadata(struct dentry *ecryptfs_dentry,
1658 struct file *lower_file)
Michael Halcrow237fead2006-10-04 02:16:22 -07001659{
1660 int rc = 0;
1661 char *page_virt = NULL;
1662 mm_segment_t oldfs;
1663 ssize_t bytes_read;
1664 struct ecryptfs_crypt_stat *crypt_stat =
1665 &ecryptfs_inode_to_private(ecryptfs_dentry->d_inode)->crypt_stat;
Michael Halcrowe77a56d2007-02-12 00:53:47 -08001666 struct ecryptfs_mount_crypt_stat *mount_crypt_stat =
1667 &ecryptfs_superblock_to_private(
1668 ecryptfs_dentry->d_sb)->mount_crypt_stat;
Michael Halcrow237fead2006-10-04 02:16:22 -07001669
Michael Halcrowe77a56d2007-02-12 00:53:47 -08001670 ecryptfs_copy_mount_wide_flags_to_inode_flags(crypt_stat,
1671 mount_crypt_stat);
Michael Halcrow237fead2006-10-04 02:16:22 -07001672 /* Read the first page from the underlying file */
Christoph Lameterf7267c02006-12-06 20:33:15 -08001673 page_virt = kmem_cache_alloc(ecryptfs_header_cache_1, GFP_USER);
Michael Halcrow237fead2006-10-04 02:16:22 -07001674 if (!page_virt) {
1675 rc = -ENOMEM;
1676 ecryptfs_printk(KERN_ERR, "Unable to allocate page_virt\n");
1677 goto out;
1678 }
1679 lower_file->f_pos = 0;
1680 oldfs = get_fs();
1681 set_fs(get_ds());
1682 bytes_read = lower_file->f_op->read(lower_file,
1683 (char __user *)page_virt,
1684 ECRYPTFS_DEFAULT_EXTENT_SIZE,
1685 &lower_file->f_pos);
1686 set_fs(oldfs);
1687 if (bytes_read != ECRYPTFS_DEFAULT_EXTENT_SIZE) {
1688 rc = -EINVAL;
1689 goto out;
1690 }
1691 rc = ecryptfs_read_headers_virt(page_virt, crypt_stat,
Michael Halcrowdd2a3b72007-02-12 00:53:46 -08001692 ecryptfs_dentry,
1693 ECRYPTFS_VALIDATE_HEADER_SIZE);
Michael Halcrow237fead2006-10-04 02:16:22 -07001694 if (rc) {
Michael Halcrowdd2a3b72007-02-12 00:53:46 -08001695 rc = ecryptfs_read_xattr_region(page_virt,
1696 ecryptfs_dentry);
1697 if (rc) {
1698 printk(KERN_DEBUG "Valid eCryptfs headers not found in "
1699 "file header region or xattr region\n");
1700 rc = -EINVAL;
1701 goto out;
1702 }
1703 rc = ecryptfs_read_headers_virt(page_virt, crypt_stat,
1704 ecryptfs_dentry,
1705 ECRYPTFS_DONT_VALIDATE_HEADER_SIZE);
1706 if (rc) {
1707 printk(KERN_DEBUG "Valid eCryptfs headers not found in "
1708 "file xattr region either\n");
1709 rc = -EINVAL;
1710 }
1711 if (crypt_stat->mount_crypt_stat->flags
1712 & ECRYPTFS_XATTR_METADATA_ENABLED) {
1713 crypt_stat->flags |= ECRYPTFS_METADATA_IN_XATTR;
1714 } else {
1715 printk(KERN_WARNING "Attempt to access file with "
1716 "crypto metadata only in the extended attribute "
1717 "region, but eCryptfs was mounted without "
1718 "xattr support enabled. eCryptfs will not treat "
1719 "this like an encrypted file.\n");
1720 rc = -EINVAL;
1721 }
Michael Halcrow237fead2006-10-04 02:16:22 -07001722 }
1723out:
1724 if (page_virt) {
1725 memset(page_virt, 0, PAGE_CACHE_SIZE);
1726 kmem_cache_free(ecryptfs_header_cache_1, page_virt);
1727 }
1728 return rc;
1729}
1730
1731/**
1732 * ecryptfs_encode_filename - converts a plaintext file name to cipher text
1733 * @crypt_stat: The crypt_stat struct associated with the file anem to encode
1734 * @name: The plaintext name
1735 * @length: The length of the plaintext
1736 * @encoded_name: The encypted name
1737 *
1738 * Encrypts and encodes a filename into something that constitutes a
1739 * valid filename for a filesystem, with printable characters.
1740 *
1741 * We assume that we have a properly initialized crypto context,
1742 * pointed to by crypt_stat->tfm.
1743 *
1744 * TODO: Implement filename decoding and decryption here, in place of
1745 * memcpy. We are keeping the framework around for now to (1)
1746 * facilitate testing of the components needed to implement filename
1747 * encryption and (2) to provide a code base from which other
1748 * developers in the community can easily implement this feature.
1749 *
1750 * Returns the length of encoded filename; negative if error
1751 */
1752int
1753ecryptfs_encode_filename(struct ecryptfs_crypt_stat *crypt_stat,
1754 const char *name, int length, char **encoded_name)
1755{
1756 int error = 0;
1757
1758 (*encoded_name) = kmalloc(length + 2, GFP_KERNEL);
1759 if (!(*encoded_name)) {
1760 error = -ENOMEM;
1761 goto out;
1762 }
1763 /* TODO: Filename encryption is a scheduled feature for a
1764 * future version of eCryptfs. This function is here only for
1765 * the purpose of providing a framework for other developers
1766 * to easily implement filename encryption. Hint: Replace this
1767 * memcpy() with a call to encrypt and encode the
1768 * filename, the set the length accordingly. */
1769 memcpy((void *)(*encoded_name), (void *)name, length);
1770 (*encoded_name)[length] = '\0';
1771 error = length + 1;
1772out:
1773 return error;
1774}
1775
1776/**
1777 * ecryptfs_decode_filename - converts the cipher text name to plaintext
1778 * @crypt_stat: The crypt_stat struct associated with the file
1779 * @name: The filename in cipher text
1780 * @length: The length of the cipher text name
1781 * @decrypted_name: The plaintext name
1782 *
1783 * Decodes and decrypts the filename.
1784 *
1785 * We assume that we have a properly initialized crypto context,
1786 * pointed to by crypt_stat->tfm.
1787 *
1788 * TODO: Implement filename decoding and decryption here, in place of
1789 * memcpy. We are keeping the framework around for now to (1)
1790 * facilitate testing of the components needed to implement filename
1791 * encryption and (2) to provide a code base from which other
1792 * developers in the community can easily implement this feature.
1793 *
1794 * Returns the length of decoded filename; negative if error
1795 */
1796int
1797ecryptfs_decode_filename(struct ecryptfs_crypt_stat *crypt_stat,
1798 const char *name, int length, char **decrypted_name)
1799{
1800 int error = 0;
1801
1802 (*decrypted_name) = kmalloc(length + 2, GFP_KERNEL);
1803 if (!(*decrypted_name)) {
1804 error = -ENOMEM;
1805 goto out;
1806 }
1807 /* TODO: Filename encryption is a scheduled feature for a
1808 * future version of eCryptfs. This function is here only for
1809 * the purpose of providing a framework for other developers
1810 * to easily implement filename encryption. Hint: Replace this
1811 * memcpy() with a call to decode and decrypt the
1812 * filename, the set the length accordingly. */
1813 memcpy((void *)(*decrypted_name), (void *)name, length);
1814 (*decrypted_name)[length + 1] = '\0'; /* Only for convenience
1815 * in printing out the
1816 * string in debug
1817 * messages */
1818 error = length;
1819out:
1820 return error;
1821}
1822
1823/**
Michael Halcrowf4aad162007-10-16 01:27:53 -07001824 * ecryptfs_process_key_cipher - Perform key cipher initialization.
Michael Halcrow237fead2006-10-04 02:16:22 -07001825 * @key_tfm: Crypto context for key material, set by this function
Michael Halcrowe5d9cbd2006-10-30 22:07:16 -08001826 * @cipher_name: Name of the cipher
1827 * @key_size: Size of the key in bytes
Michael Halcrow237fead2006-10-04 02:16:22 -07001828 *
1829 * Returns zero on success. Any crypto_tfm structs allocated here
1830 * should be released by other functions, such as on a superblock put
1831 * event, regardless of whether this function succeeds for fails.
1832 */
1833int
Michael Halcrowf4aad162007-10-16 01:27:53 -07001834ecryptfs_process_key_cipher(struct crypto_blkcipher **key_tfm,
1835 char *cipher_name, size_t *key_size)
Michael Halcrow237fead2006-10-04 02:16:22 -07001836{
1837 char dummy_key[ECRYPTFS_MAX_KEY_BYTES];
Michael Halcrow8bba0662006-10-30 22:07:18 -08001838 char *full_alg_name;
Michael Halcrow237fead2006-10-04 02:16:22 -07001839 int rc;
1840
Michael Halcrowe5d9cbd2006-10-30 22:07:16 -08001841 *key_tfm = NULL;
1842 if (*key_size > ECRYPTFS_MAX_KEY_BYTES) {
Michael Halcrow237fead2006-10-04 02:16:22 -07001843 rc = -EINVAL;
1844 printk(KERN_ERR "Requested key size is [%Zd] bytes; maximum "
Michael Halcrowe5d9cbd2006-10-30 22:07:16 -08001845 "allowable is [%d]\n", *key_size, ECRYPTFS_MAX_KEY_BYTES);
Michael Halcrow237fead2006-10-04 02:16:22 -07001846 goto out;
1847 }
Michael Halcrow8bba0662006-10-30 22:07:18 -08001848 rc = ecryptfs_crypto_api_algify_cipher_name(&full_alg_name, cipher_name,
1849 "ecb");
1850 if (rc)
1851 goto out;
1852 *key_tfm = crypto_alloc_blkcipher(full_alg_name, 0, CRYPTO_ALG_ASYNC);
1853 kfree(full_alg_name);
1854 if (IS_ERR(*key_tfm)) {
1855 rc = PTR_ERR(*key_tfm);
Michael Halcrow237fead2006-10-04 02:16:22 -07001856 printk(KERN_ERR "Unable to allocate crypto cipher with name "
Michael Halcrow8bba0662006-10-30 22:07:18 -08001857 "[%s]; rc = [%d]\n", cipher_name, rc);
Michael Halcrow237fead2006-10-04 02:16:22 -07001858 goto out;
1859 }
Michael Halcrow8bba0662006-10-30 22:07:18 -08001860 crypto_blkcipher_set_flags(*key_tfm, CRYPTO_TFM_REQ_WEAK_KEY);
1861 if (*key_size == 0) {
1862 struct blkcipher_alg *alg = crypto_blkcipher_alg(*key_tfm);
1863
1864 *key_size = alg->max_keysize;
1865 }
Michael Halcrowe5d9cbd2006-10-30 22:07:16 -08001866 get_random_bytes(dummy_key, *key_size);
Michael Halcrow8bba0662006-10-30 22:07:18 -08001867 rc = crypto_blkcipher_setkey(*key_tfm, dummy_key, *key_size);
Michael Halcrow237fead2006-10-04 02:16:22 -07001868 if (rc) {
1869 printk(KERN_ERR "Error attempting to set key of size [%Zd] for "
Michael Halcrowe5d9cbd2006-10-30 22:07:16 -08001870 "cipher [%s]; rc = [%d]\n", *key_size, cipher_name, rc);
Michael Halcrow237fead2006-10-04 02:16:22 -07001871 rc = -EINVAL;
1872 goto out;
1873 }
1874out:
1875 return rc;
1876}
Michael Halcrowf4aad162007-10-16 01:27:53 -07001877
1878struct kmem_cache *ecryptfs_key_tfm_cache;
1879struct list_head key_tfm_list;
1880struct mutex key_tfm_list_mutex;
1881
1882int ecryptfs_init_crypto(void)
1883{
1884 mutex_init(&key_tfm_list_mutex);
1885 INIT_LIST_HEAD(&key_tfm_list);
1886 return 0;
1887}
1888
1889int ecryptfs_destruct_crypto(void)
1890{
1891 struct ecryptfs_key_tfm *key_tfm, *key_tfm_tmp;
1892
1893 mutex_lock(&key_tfm_list_mutex);
1894 list_for_each_entry_safe(key_tfm, key_tfm_tmp, &key_tfm_list,
1895 key_tfm_list) {
1896 list_del(&key_tfm->key_tfm_list);
1897 if (key_tfm->key_tfm)
1898 crypto_free_blkcipher(key_tfm->key_tfm);
1899 kmem_cache_free(ecryptfs_key_tfm_cache, key_tfm);
1900 }
1901 mutex_unlock(&key_tfm_list_mutex);
1902 return 0;
1903}
1904
1905int
1906ecryptfs_add_new_key_tfm(struct ecryptfs_key_tfm **key_tfm, char *cipher_name,
1907 size_t key_size)
1908{
1909 struct ecryptfs_key_tfm *tmp_tfm;
1910 int rc = 0;
1911
1912 tmp_tfm = kmem_cache_alloc(ecryptfs_key_tfm_cache, GFP_KERNEL);
1913 if (key_tfm != NULL)
1914 (*key_tfm) = tmp_tfm;
1915 if (!tmp_tfm) {
1916 rc = -ENOMEM;
1917 printk(KERN_ERR "Error attempting to allocate from "
1918 "ecryptfs_key_tfm_cache\n");
1919 goto out;
1920 }
1921 mutex_init(&tmp_tfm->key_tfm_mutex);
1922 strncpy(tmp_tfm->cipher_name, cipher_name,
1923 ECRYPTFS_MAX_CIPHER_NAME_SIZE);
1924 tmp_tfm->key_size = key_size;
1925 if ((rc = ecryptfs_process_key_cipher(&tmp_tfm->key_tfm,
1926 tmp_tfm->cipher_name,
1927 &tmp_tfm->key_size))) {
1928 printk(KERN_ERR "Error attempting to initialize key TFM "
1929 "cipher with name = [%s]; rc = [%d]\n",
1930 tmp_tfm->cipher_name, rc);
1931 kmem_cache_free(ecryptfs_key_tfm_cache, tmp_tfm);
1932 if (key_tfm != NULL)
1933 (*key_tfm) = NULL;
1934 goto out;
1935 }
1936 mutex_lock(&key_tfm_list_mutex);
1937 list_add(&tmp_tfm->key_tfm_list, &key_tfm_list);
1938 mutex_unlock(&key_tfm_list_mutex);
1939out:
1940 return rc;
1941}
1942
1943int ecryptfs_get_tfm_and_mutex_for_cipher_name(struct crypto_blkcipher **tfm,
1944 struct mutex **tfm_mutex,
1945 char *cipher_name)
1946{
1947 struct ecryptfs_key_tfm *key_tfm;
1948 int rc = 0;
1949
1950 (*tfm) = NULL;
1951 (*tfm_mutex) = NULL;
1952 mutex_lock(&key_tfm_list_mutex);
1953 list_for_each_entry(key_tfm, &key_tfm_list, key_tfm_list) {
1954 if (strcmp(key_tfm->cipher_name, cipher_name) == 0) {
1955 (*tfm) = key_tfm->key_tfm;
1956 (*tfm_mutex) = &key_tfm->key_tfm_mutex;
1957 mutex_unlock(&key_tfm_list_mutex);
1958 goto out;
1959 }
1960 }
1961 mutex_unlock(&key_tfm_list_mutex);
1962 if ((rc = ecryptfs_add_new_key_tfm(&key_tfm, cipher_name, 0))) {
1963 printk(KERN_ERR "Error adding new key_tfm to list; rc = [%d]\n",
1964 rc);
1965 goto out;
1966 }
1967 (*tfm) = key_tfm->key_tfm;
1968 (*tfm_mutex) = &key_tfm->key_tfm_mutex;
1969out:
1970 return rc;
1971}