2 * eCryptfs: Linux filesystem encryption layer
4 * Copyright (C) 1997-2004 Erez Zadok
5 * Copyright (C) 2001-2004 Stony Brook University
6 * Copyright (C) 2004-2007 International Business Machines Corp.
7 * Author(s): Michael A. Halcrow <mahalcro@us.ibm.com>
8 * Michael C. Thompson <mcthomps@us.ibm.com>
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.
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.
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
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"
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
,
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
,
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
56 void ecryptfs_to_hex(char *dst
, char *src
, size_t src_size
)
60 for (x
= 0; x
< src_size
; x
++)
61 sprintf(&dst
[x
* 2], "%.2x", (unsigned char)src
[x
]);
66 * @dst: Buffer to take the bytes from src hex; must be at least of
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
71 void ecryptfs_from_hex(char *dst
, char *src
, int dst_size
)
76 for (x
= 0; x
< dst_size
; x
++) {
78 tmp
[1] = src
[x
* 2 + 1];
79 dst
[x
] = (unsigned char)simple_strtol(tmp
, NULL
, 16);
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
90 * Uses the allocated crypto context that crypt_stat references to
91 * generate the MD5 sum of the contents of src.
93 static int ecryptfs_calculate_md5(char *dst
,
94 struct ecryptfs_crypt_stat
*crypt_stat
,
97 struct scatterlist sg
;
98 struct hash_desc desc
= {
99 .tfm
= crypt_stat
->hash_tfm
,
100 .flags
= CRYPTO_TFM_REQ_MAY_SLEEP
104 mutex_lock(&crypt_stat
->cs_hash_tfm_mutex
);
105 sg_init_one(&sg
, (u8
*)src
, len
);
107 desc
.tfm
= crypto_alloc_hash(ECRYPTFS_DEFAULT_HASH
, 0,
109 if (IS_ERR(desc
.tfm
)) {
110 rc
= PTR_ERR(desc
.tfm
);
111 ecryptfs_printk(KERN_ERR
, "Error attempting to "
112 "allocate crypto context; rc = [%d]\n",
116 crypt_stat
->hash_tfm
= desc
.tfm
;
118 rc
= crypto_hash_init(&desc
);
121 "%s: Error initializing crypto hash; rc = [%d]\n",
125 rc
= crypto_hash_update(&desc
, &sg
, len
);
128 "%s: Error updating crypto hash; rc = [%d]\n",
132 rc
= crypto_hash_final(&desc
, dst
);
135 "%s: Error finalizing crypto hash; rc = [%d]\n",
140 mutex_unlock(&crypt_stat
->cs_hash_tfm_mutex
);
144 static int ecryptfs_crypto_api_algify_cipher_name(char **algified_name
,
146 char *chaining_modifier
)
148 int cipher_name_len
= strlen(cipher_name
);
149 int chaining_modifier_len
= strlen(chaining_modifier
);
150 int algified_name_len
;
153 algified_name_len
= (chaining_modifier_len
+ cipher_name_len
+ 3);
154 (*algified_name
) = kmalloc(algified_name_len
, GFP_KERNEL
);
155 if (!(*algified_name
)) {
159 snprintf((*algified_name
), algified_name_len
, "%s(%s)",
160 chaining_modifier
, cipher_name
);
168 * @iv: destination for the derived iv vale
169 * @crypt_stat: Pointer to crypt_stat struct for the current inode
170 * @offset: Offset of the extent whose IV we are to derive
172 * Generate the initialization vector from the given root IV and page
175 * Returns zero on success; non-zero on error.
177 static int ecryptfs_derive_iv(char *iv
, struct ecryptfs_crypt_stat
*crypt_stat
,
181 char dst
[MD5_DIGEST_SIZE
];
182 char src
[ECRYPTFS_MAX_IV_BYTES
+ 16];
184 if (unlikely(ecryptfs_verbosity
> 0)) {
185 ecryptfs_printk(KERN_DEBUG
, "root iv:\n");
186 ecryptfs_dump_hex(crypt_stat
->root_iv
, crypt_stat
->iv_bytes
);
188 /* TODO: It is probably secure to just cast the least
189 * significant bits of the root IV into an unsigned long and
190 * add the offset to that rather than go through all this
191 * hashing business. -Halcrow */
192 memcpy(src
, crypt_stat
->root_iv
, crypt_stat
->iv_bytes
);
193 memset((src
+ crypt_stat
->iv_bytes
), 0, 16);
194 snprintf((src
+ crypt_stat
->iv_bytes
), 16, "%lld", offset
);
195 if (unlikely(ecryptfs_verbosity
> 0)) {
196 ecryptfs_printk(KERN_DEBUG
, "source:\n");
197 ecryptfs_dump_hex(src
, (crypt_stat
->iv_bytes
+ 16));
199 rc
= ecryptfs_calculate_md5(dst
, crypt_stat
, src
,
200 (crypt_stat
->iv_bytes
+ 16));
202 ecryptfs_printk(KERN_WARNING
, "Error attempting to compute "
203 "MD5 while generating IV for a page\n");
206 memcpy(iv
, dst
, crypt_stat
->iv_bytes
);
207 if (unlikely(ecryptfs_verbosity
> 0)) {
208 ecryptfs_printk(KERN_DEBUG
, "derived iv:\n");
209 ecryptfs_dump_hex(iv
, crypt_stat
->iv_bytes
);
216 * ecryptfs_init_crypt_stat
217 * @crypt_stat: Pointer to the crypt_stat struct to initialize.
219 * Initialize the crypt_stat structure.
222 ecryptfs_init_crypt_stat(struct ecryptfs_crypt_stat
*crypt_stat
)
224 memset((void *)crypt_stat
, 0, sizeof(struct ecryptfs_crypt_stat
));
225 INIT_LIST_HEAD(&crypt_stat
->keysig_list
);
226 mutex_init(&crypt_stat
->keysig_list_mutex
);
227 mutex_init(&crypt_stat
->cs_mutex
);
228 mutex_init(&crypt_stat
->cs_tfm_mutex
);
229 mutex_init(&crypt_stat
->cs_hash_tfm_mutex
);
230 crypt_stat
->flags
|= ECRYPTFS_STRUCT_INITIALIZED
;
234 * ecryptfs_destroy_crypt_stat
235 * @crypt_stat: Pointer to the crypt_stat struct to initialize.
237 * Releases all memory associated with a crypt_stat struct.
239 void ecryptfs_destroy_crypt_stat(struct ecryptfs_crypt_stat
*crypt_stat
)
241 struct ecryptfs_key_sig
*key_sig
, *key_sig_tmp
;
244 crypto_free_blkcipher(crypt_stat
->tfm
);
245 if (crypt_stat
->hash_tfm
)
246 crypto_free_hash(crypt_stat
->hash_tfm
);
247 mutex_lock(&crypt_stat
->keysig_list_mutex
);
248 list_for_each_entry_safe(key_sig
, key_sig_tmp
,
249 &crypt_stat
->keysig_list
, crypt_stat_list
) {
250 list_del(&key_sig
->crypt_stat_list
);
251 kmem_cache_free(ecryptfs_key_sig_cache
, key_sig
);
253 mutex_unlock(&crypt_stat
->keysig_list_mutex
);
254 memset(crypt_stat
, 0, sizeof(struct ecryptfs_crypt_stat
));
257 void ecryptfs_destroy_mount_crypt_stat(
258 struct ecryptfs_mount_crypt_stat
*mount_crypt_stat
)
260 struct ecryptfs_global_auth_tok
*auth_tok
, *auth_tok_tmp
;
262 if (!(mount_crypt_stat
->flags
& ECRYPTFS_MOUNT_CRYPT_STAT_INITIALIZED
))
264 mutex_lock(&mount_crypt_stat
->global_auth_tok_list_mutex
);
265 list_for_each_entry_safe(auth_tok
, auth_tok_tmp
,
266 &mount_crypt_stat
->global_auth_tok_list
,
267 mount_crypt_stat_list
) {
268 list_del(&auth_tok
->mount_crypt_stat_list
);
269 mount_crypt_stat
->num_global_auth_toks
--;
270 if (auth_tok
->global_auth_tok_key
271 && !(auth_tok
->flags
& ECRYPTFS_AUTH_TOK_INVALID
))
272 key_put(auth_tok
->global_auth_tok_key
);
273 kmem_cache_free(ecryptfs_global_auth_tok_cache
, auth_tok
);
275 mutex_unlock(&mount_crypt_stat
->global_auth_tok_list_mutex
);
276 memset(mount_crypt_stat
, 0, sizeof(struct ecryptfs_mount_crypt_stat
));
280 * virt_to_scatterlist
281 * @addr: Virtual address
282 * @size: Size of data; should be an even multiple of the block size
283 * @sg: Pointer to scatterlist array; set to NULL to obtain only
284 * the number of scatterlist structs required in array
285 * @sg_size: Max array size
287 * Fills in a scatterlist array with page references for a passed
290 * Returns the number of scatterlist structs in array used
292 int virt_to_scatterlist(const void *addr
, int size
, struct scatterlist
*sg
,
298 int remainder_of_page
;
300 sg_init_table(sg
, sg_size
);
302 while (size
> 0 && i
< sg_size
) {
303 pg
= virt_to_page(addr
);
304 offset
= offset_in_page(addr
);
306 sg_set_page(&sg
[i
], pg
, 0, offset
);
307 remainder_of_page
= PAGE_CACHE_SIZE
- offset
;
308 if (size
>= remainder_of_page
) {
310 sg
[i
].length
= remainder_of_page
;
311 addr
+= remainder_of_page
;
312 size
-= remainder_of_page
;
327 * encrypt_scatterlist
328 * @crypt_stat: Pointer to the crypt_stat struct to initialize.
329 * @dest_sg: Destination of encrypted data
330 * @src_sg: Data to be encrypted
331 * @size: Length of data to be encrypted
332 * @iv: iv to use during encryption
334 * Returns the number of bytes encrypted; negative value on error
336 static int encrypt_scatterlist(struct ecryptfs_crypt_stat
*crypt_stat
,
337 struct scatterlist
*dest_sg
,
338 struct scatterlist
*src_sg
, int size
,
341 struct blkcipher_desc desc
= {
342 .tfm
= crypt_stat
->tfm
,
344 .flags
= CRYPTO_TFM_REQ_MAY_SLEEP
348 BUG_ON(!crypt_stat
|| !crypt_stat
->tfm
349 || !(crypt_stat
->flags
& ECRYPTFS_STRUCT_INITIALIZED
));
350 if (unlikely(ecryptfs_verbosity
> 0)) {
351 ecryptfs_printk(KERN_DEBUG
, "Key size [%d]; key:\n",
352 crypt_stat
->key_size
);
353 ecryptfs_dump_hex(crypt_stat
->key
,
354 crypt_stat
->key_size
);
356 /* Consider doing this once, when the file is opened */
357 mutex_lock(&crypt_stat
->cs_tfm_mutex
);
358 rc
= crypto_blkcipher_setkey(crypt_stat
->tfm
, crypt_stat
->key
,
359 crypt_stat
->key_size
);
361 ecryptfs_printk(KERN_ERR
, "Error setting key; rc = [%d]\n",
363 mutex_unlock(&crypt_stat
->cs_tfm_mutex
);
367 ecryptfs_printk(KERN_DEBUG
, "Encrypting [%d] bytes.\n", size
);
368 crypto_blkcipher_encrypt_iv(&desc
, dest_sg
, src_sg
, size
);
369 mutex_unlock(&crypt_stat
->cs_tfm_mutex
);
375 * ecryptfs_lower_offset_for_extent
377 * Convert an eCryptfs page index into a lower byte offset
379 void ecryptfs_lower_offset_for_extent(loff_t
*offset
, loff_t extent_num
,
380 struct ecryptfs_crypt_stat
*crypt_stat
)
382 (*offset
) = ((crypt_stat
->extent_size
383 * crypt_stat
->num_header_extents_at_front
)
384 + (crypt_stat
->extent_size
* extent_num
));
388 * ecryptfs_encrypt_extent
389 * @enc_extent_page: Allocated page into which to encrypt the data in
391 * @crypt_stat: crypt_stat containing cryptographic context for the
392 * encryption operation
393 * @page: Page containing plaintext data extent to encrypt
394 * @extent_offset: Page extent offset for use in generating IV
396 * Encrypts one extent of data.
398 * Return zero on success; non-zero otherwise
400 static int ecryptfs_encrypt_extent(struct page
*enc_extent_page
,
401 struct ecryptfs_crypt_stat
*crypt_stat
,
403 unsigned long extent_offset
)
406 char extent_iv
[ECRYPTFS_MAX_IV_BYTES
];
409 extent_base
= (((loff_t
)page
->index
)
410 * (PAGE_CACHE_SIZE
/ crypt_stat
->extent_size
));
411 rc
= ecryptfs_derive_iv(extent_iv
, crypt_stat
,
412 (extent_base
+ extent_offset
));
414 ecryptfs_printk(KERN_ERR
, "Error attempting to "
415 "derive IV for extent [0x%.16x]; "
416 "rc = [%d]\n", (extent_base
+ extent_offset
),
420 if (unlikely(ecryptfs_verbosity
> 0)) {
421 ecryptfs_printk(KERN_DEBUG
, "Encrypting extent "
423 ecryptfs_dump_hex(extent_iv
, crypt_stat
->iv_bytes
);
424 ecryptfs_printk(KERN_DEBUG
, "First 8 bytes before "
426 ecryptfs_dump_hex((char *)
428 + (extent_offset
* crypt_stat
->extent_size
)),
431 rc
= ecryptfs_encrypt_page_offset(crypt_stat
, enc_extent_page
, 0,
433 * crypt_stat
->extent_size
),
434 crypt_stat
->extent_size
, extent_iv
);
436 printk(KERN_ERR
"%s: Error attempting to encrypt page with "
437 "page->index = [%ld], extent_offset = [%ld]; "
438 "rc = [%d]\n", __FUNCTION__
, page
->index
, extent_offset
,
443 if (unlikely(ecryptfs_verbosity
> 0)) {
444 ecryptfs_printk(KERN_DEBUG
, "Encrypt extent [0x%.16x]; "
445 "rc = [%d]\n", (extent_base
+ extent_offset
),
447 ecryptfs_printk(KERN_DEBUG
, "First 8 bytes after "
449 ecryptfs_dump_hex((char *)(page_address(enc_extent_page
)), 8);
456 * ecryptfs_encrypt_page
457 * @page: Page mapped from the eCryptfs inode for the file; contains
458 * decrypted content that needs to be encrypted (to a temporary
459 * page; not in place) and written out to the lower file
461 * Encrypt an eCryptfs page. This is done on a per-extent basis. Note
462 * that eCryptfs pages may straddle the lower pages -- for instance,
463 * if the file was created on a machine with an 8K page size
464 * (resulting in an 8K header), and then the file is copied onto a
465 * host with a 32K page size, then when reading page 0 of the eCryptfs
466 * file, 24K of page 0 of the lower file will be read and decrypted,
467 * and then 8K of page 1 of the lower file will be read and decrypted.
469 * Returns zero on success; negative on error
471 int ecryptfs_encrypt_page(struct page
*page
)
473 struct inode
*ecryptfs_inode
;
474 struct ecryptfs_crypt_stat
*crypt_stat
;
475 char *enc_extent_virt
= NULL
;
476 struct page
*enc_extent_page
;
477 loff_t extent_offset
;
480 ecryptfs_inode
= page
->mapping
->host
;
482 &(ecryptfs_inode_to_private(ecryptfs_inode
)->crypt_stat
);
483 if (!(crypt_stat
->flags
& ECRYPTFS_ENCRYPTED
)) {
484 rc
= ecryptfs_write_lower_page_segment(ecryptfs_inode
, page
,
487 printk(KERN_ERR
"%s: Error attempting to copy "
488 "page at index [%ld]\n", __FUNCTION__
,
492 enc_extent_virt
= kmalloc(PAGE_CACHE_SIZE
, GFP_USER
);
493 if (!enc_extent_virt
) {
495 ecryptfs_printk(KERN_ERR
, "Error allocating memory for "
496 "encrypted extent\n");
499 enc_extent_page
= virt_to_page(enc_extent_virt
);
500 for (extent_offset
= 0;
501 extent_offset
< (PAGE_CACHE_SIZE
/ crypt_stat
->extent_size
);
505 rc
= ecryptfs_encrypt_extent(enc_extent_page
, crypt_stat
, page
,
508 printk(KERN_ERR
"%s: Error encrypting extent; "
509 "rc = [%d]\n", __FUNCTION__
, rc
);
512 ecryptfs_lower_offset_for_extent(
513 &offset
, ((((loff_t
)page
->index
)
515 / crypt_stat
->extent_size
))
516 + extent_offset
), crypt_stat
);
517 rc
= ecryptfs_write_lower(ecryptfs_inode
, enc_extent_virt
,
518 offset
, crypt_stat
->extent_size
);
520 ecryptfs_printk(KERN_ERR
, "Error attempting "
521 "to write lower page; rc = [%d]"
527 kfree(enc_extent_virt
);
531 static int ecryptfs_decrypt_extent(struct page
*page
,
532 struct ecryptfs_crypt_stat
*crypt_stat
,
533 struct page
*enc_extent_page
,
534 unsigned long extent_offset
)
537 char extent_iv
[ECRYPTFS_MAX_IV_BYTES
];
540 extent_base
= (((loff_t
)page
->index
)
541 * (PAGE_CACHE_SIZE
/ crypt_stat
->extent_size
));
542 rc
= ecryptfs_derive_iv(extent_iv
, crypt_stat
,
543 (extent_base
+ extent_offset
));
545 ecryptfs_printk(KERN_ERR
, "Error attempting to "
546 "derive IV for extent [0x%.16x]; "
547 "rc = [%d]\n", (extent_base
+ extent_offset
),
551 if (unlikely(ecryptfs_verbosity
> 0)) {
552 ecryptfs_printk(KERN_DEBUG
, "Decrypting extent "
554 ecryptfs_dump_hex(extent_iv
, crypt_stat
->iv_bytes
);
555 ecryptfs_printk(KERN_DEBUG
, "First 8 bytes before "
557 ecryptfs_dump_hex((char *)
558 (page_address(enc_extent_page
)
559 + (extent_offset
* crypt_stat
->extent_size
)),
562 rc
= ecryptfs_decrypt_page_offset(crypt_stat
, page
,
564 * crypt_stat
->extent_size
),
566 crypt_stat
->extent_size
, extent_iv
);
568 printk(KERN_ERR
"%s: Error attempting to decrypt to page with "
569 "page->index = [%ld], extent_offset = [%ld]; "
570 "rc = [%d]\n", __FUNCTION__
, page
->index
, extent_offset
,
575 if (unlikely(ecryptfs_verbosity
> 0)) {
576 ecryptfs_printk(KERN_DEBUG
, "Decrypt extent [0x%.16x]; "
577 "rc = [%d]\n", (extent_base
+ extent_offset
),
579 ecryptfs_printk(KERN_DEBUG
, "First 8 bytes after "
581 ecryptfs_dump_hex((char *)(page_address(page
)
583 * crypt_stat
->extent_size
)), 8);
590 * ecryptfs_decrypt_page
591 * @page: Page mapped from the eCryptfs inode for the file; data read
592 * and decrypted from the lower file will be written into this
595 * Decrypt an eCryptfs page. This is done on a per-extent basis. Note
596 * that eCryptfs pages may straddle the lower pages -- for instance,
597 * if the file was created on a machine with an 8K page size
598 * (resulting in an 8K header), and then the file is copied onto a
599 * host with a 32K page size, then when reading page 0 of the eCryptfs
600 * file, 24K of page 0 of the lower file will be read and decrypted,
601 * and then 8K of page 1 of the lower file will be read and decrypted.
603 * Returns zero on success; negative on error
605 int ecryptfs_decrypt_page(struct page
*page
)
607 struct inode
*ecryptfs_inode
;
608 struct ecryptfs_crypt_stat
*crypt_stat
;
609 char *enc_extent_virt
= NULL
;
610 struct page
*enc_extent_page
;
611 unsigned long extent_offset
;
614 ecryptfs_inode
= page
->mapping
->host
;
616 &(ecryptfs_inode_to_private(ecryptfs_inode
)->crypt_stat
);
617 if (!(crypt_stat
->flags
& ECRYPTFS_ENCRYPTED
)) {
618 rc
= ecryptfs_read_lower_page_segment(page
, page
->index
, 0,
622 printk(KERN_ERR
"%s: Error attempting to copy "
623 "page at index [%ld]\n", __FUNCTION__
,
627 enc_extent_virt
= kmalloc(PAGE_CACHE_SIZE
, GFP_USER
);
628 if (!enc_extent_virt
) {
630 ecryptfs_printk(KERN_ERR
, "Error allocating memory for "
631 "encrypted extent\n");
634 enc_extent_page
= virt_to_page(enc_extent_virt
);
635 for (extent_offset
= 0;
636 extent_offset
< (PAGE_CACHE_SIZE
/ crypt_stat
->extent_size
);
640 ecryptfs_lower_offset_for_extent(
641 &offset
, ((page
->index
* (PAGE_CACHE_SIZE
642 / crypt_stat
->extent_size
))
643 + extent_offset
), crypt_stat
);
644 rc
= ecryptfs_read_lower(enc_extent_virt
, offset
,
645 crypt_stat
->extent_size
,
648 ecryptfs_printk(KERN_ERR
, "Error attempting "
649 "to read lower page; rc = [%d]"
653 rc
= ecryptfs_decrypt_extent(page
, crypt_stat
, enc_extent_page
,
656 printk(KERN_ERR
"%s: Error encrypting extent; "
657 "rc = [%d]\n", __FUNCTION__
, rc
);
662 kfree(enc_extent_virt
);
667 * decrypt_scatterlist
668 * @crypt_stat: Cryptographic context
669 * @dest_sg: The destination scatterlist to decrypt into
670 * @src_sg: The source scatterlist to decrypt from
671 * @size: The number of bytes to decrypt
672 * @iv: The initialization vector to use for the decryption
674 * Returns the number of bytes decrypted; negative value on error
676 static int decrypt_scatterlist(struct ecryptfs_crypt_stat
*crypt_stat
,
677 struct scatterlist
*dest_sg
,
678 struct scatterlist
*src_sg
, int size
,
681 struct blkcipher_desc desc
= {
682 .tfm
= crypt_stat
->tfm
,
684 .flags
= CRYPTO_TFM_REQ_MAY_SLEEP
688 /* Consider doing this once, when the file is opened */
689 mutex_lock(&crypt_stat
->cs_tfm_mutex
);
690 rc
= crypto_blkcipher_setkey(crypt_stat
->tfm
, crypt_stat
->key
,
691 crypt_stat
->key_size
);
693 ecryptfs_printk(KERN_ERR
, "Error setting key; rc = [%d]\n",
695 mutex_unlock(&crypt_stat
->cs_tfm_mutex
);
699 ecryptfs_printk(KERN_DEBUG
, "Decrypting [%d] bytes.\n", size
);
700 rc
= crypto_blkcipher_decrypt_iv(&desc
, dest_sg
, src_sg
, size
);
701 mutex_unlock(&crypt_stat
->cs_tfm_mutex
);
703 ecryptfs_printk(KERN_ERR
, "Error decrypting; rc = [%d]\n",
713 * ecryptfs_encrypt_page_offset
714 * @crypt_stat: The cryptographic context
715 * @dst_page: The page to encrypt into
716 * @dst_offset: The offset in the page to encrypt into
717 * @src_page: The page to encrypt from
718 * @src_offset: The offset in the page to encrypt from
719 * @size: The number of bytes to encrypt
720 * @iv: The initialization vector to use for the encryption
722 * Returns the number of bytes encrypted
725 ecryptfs_encrypt_page_offset(struct ecryptfs_crypt_stat
*crypt_stat
,
726 struct page
*dst_page
, int dst_offset
,
727 struct page
*src_page
, int src_offset
, int size
,
730 struct scatterlist src_sg
, dst_sg
;
732 sg_init_table(&src_sg
, 1);
733 sg_init_table(&dst_sg
, 1);
735 sg_set_page(&src_sg
, src_page
, size
, src_offset
);
736 sg_set_page(&dst_sg
, dst_page
, size
, dst_offset
);
737 return encrypt_scatterlist(crypt_stat
, &dst_sg
, &src_sg
, size
, iv
);
741 * ecryptfs_decrypt_page_offset
742 * @crypt_stat: The cryptographic context
743 * @dst_page: The page to decrypt into
744 * @dst_offset: The offset in the page to decrypt into
745 * @src_page: The page to decrypt from
746 * @src_offset: The offset in the page to decrypt from
747 * @size: The number of bytes to decrypt
748 * @iv: The initialization vector to use for the decryption
750 * Returns the number of bytes decrypted
753 ecryptfs_decrypt_page_offset(struct ecryptfs_crypt_stat
*crypt_stat
,
754 struct page
*dst_page
, int dst_offset
,
755 struct page
*src_page
, int src_offset
, int size
,
758 struct scatterlist src_sg
, dst_sg
;
760 sg_init_table(&src_sg
, 1);
761 sg_set_page(&src_sg
, src_page
, size
, src_offset
);
763 sg_init_table(&dst_sg
, 1);
764 sg_set_page(&dst_sg
, dst_page
, size
, dst_offset
);
766 return decrypt_scatterlist(crypt_stat
, &dst_sg
, &src_sg
, size
, iv
);
769 #define ECRYPTFS_MAX_SCATTERLIST_LEN 4
772 * ecryptfs_init_crypt_ctx
773 * @crypt_stat: Uninitilized crypt stats structure
775 * Initialize the crypto context.
777 * TODO: Performance: Keep a cache of initialized cipher contexts;
778 * only init if needed
780 int ecryptfs_init_crypt_ctx(struct ecryptfs_crypt_stat
*crypt_stat
)
785 if (!crypt_stat
->cipher
) {
786 ecryptfs_printk(KERN_ERR
, "No cipher specified\n");
789 ecryptfs_printk(KERN_DEBUG
,
790 "Initializing cipher [%s]; strlen = [%d]; "
791 "key_size_bits = [%d]\n",
792 crypt_stat
->cipher
, (int)strlen(crypt_stat
->cipher
),
793 crypt_stat
->key_size
<< 3);
794 if (crypt_stat
->tfm
) {
798 mutex_lock(&crypt_stat
->cs_tfm_mutex
);
799 rc
= ecryptfs_crypto_api_algify_cipher_name(&full_alg_name
,
800 crypt_stat
->cipher
, "cbc");
803 crypt_stat
->tfm
= crypto_alloc_blkcipher(full_alg_name
, 0,
805 kfree(full_alg_name
);
806 if (IS_ERR(crypt_stat
->tfm
)) {
807 rc
= PTR_ERR(crypt_stat
->tfm
);
808 ecryptfs_printk(KERN_ERR
, "cryptfs: init_crypt_ctx(): "
809 "Error initializing cipher [%s]\n",
813 crypto_blkcipher_set_flags(crypt_stat
->tfm
, CRYPTO_TFM_REQ_WEAK_KEY
);
816 mutex_unlock(&crypt_stat
->cs_tfm_mutex
);
821 static void set_extent_mask_and_shift(struct ecryptfs_crypt_stat
*crypt_stat
)
825 crypt_stat
->extent_mask
= 0xFFFFFFFF;
826 crypt_stat
->extent_shift
= 0;
827 if (crypt_stat
->extent_size
== 0)
829 extent_size_tmp
= crypt_stat
->extent_size
;
830 while ((extent_size_tmp
& 0x01) == 0) {
831 extent_size_tmp
>>= 1;
832 crypt_stat
->extent_mask
<<= 1;
833 crypt_stat
->extent_shift
++;
837 void ecryptfs_set_default_sizes(struct ecryptfs_crypt_stat
*crypt_stat
)
839 /* Default values; may be overwritten as we are parsing the
841 crypt_stat
->extent_size
= ECRYPTFS_DEFAULT_EXTENT_SIZE
;
842 set_extent_mask_and_shift(crypt_stat
);
843 crypt_stat
->iv_bytes
= ECRYPTFS_DEFAULT_IV_BYTES
;
844 if (crypt_stat
->flags
& ECRYPTFS_METADATA_IN_XATTR
)
845 crypt_stat
->num_header_extents_at_front
= 0;
847 if (PAGE_CACHE_SIZE
<= ECRYPTFS_MINIMUM_HEADER_EXTENT_SIZE
)
848 crypt_stat
->num_header_extents_at_front
=
849 (ECRYPTFS_MINIMUM_HEADER_EXTENT_SIZE
850 / crypt_stat
->extent_size
);
852 crypt_stat
->num_header_extents_at_front
=
853 (PAGE_CACHE_SIZE
/ crypt_stat
->extent_size
);
858 * ecryptfs_compute_root_iv
861 * On error, sets the root IV to all 0's.
863 int ecryptfs_compute_root_iv(struct ecryptfs_crypt_stat
*crypt_stat
)
866 char dst
[MD5_DIGEST_SIZE
];
868 BUG_ON(crypt_stat
->iv_bytes
> MD5_DIGEST_SIZE
);
869 BUG_ON(crypt_stat
->iv_bytes
<= 0);
870 if (!(crypt_stat
->flags
& ECRYPTFS_KEY_VALID
)) {
872 ecryptfs_printk(KERN_WARNING
, "Session key not valid; "
873 "cannot generate root IV\n");
876 rc
= ecryptfs_calculate_md5(dst
, crypt_stat
, crypt_stat
->key
,
877 crypt_stat
->key_size
);
879 ecryptfs_printk(KERN_WARNING
, "Error attempting to compute "
880 "MD5 while generating root IV\n");
883 memcpy(crypt_stat
->root_iv
, dst
, crypt_stat
->iv_bytes
);
886 memset(crypt_stat
->root_iv
, 0, crypt_stat
->iv_bytes
);
887 crypt_stat
->flags
|= ECRYPTFS_SECURITY_WARNING
;
892 static void ecryptfs_generate_new_key(struct ecryptfs_crypt_stat
*crypt_stat
)
894 get_random_bytes(crypt_stat
->key
, crypt_stat
->key_size
);
895 crypt_stat
->flags
|= ECRYPTFS_KEY_VALID
;
896 ecryptfs_compute_root_iv(crypt_stat
);
897 if (unlikely(ecryptfs_verbosity
> 0)) {
898 ecryptfs_printk(KERN_DEBUG
, "Generated new session key:\n");
899 ecryptfs_dump_hex(crypt_stat
->key
,
900 crypt_stat
->key_size
);
905 * ecryptfs_copy_mount_wide_flags_to_inode_flags
906 * @crypt_stat: The inode's cryptographic context
907 * @mount_crypt_stat: The mount point's cryptographic context
909 * This function propagates the mount-wide flags to individual inode
912 static void ecryptfs_copy_mount_wide_flags_to_inode_flags(
913 struct ecryptfs_crypt_stat
*crypt_stat
,
914 struct ecryptfs_mount_crypt_stat
*mount_crypt_stat
)
916 if (mount_crypt_stat
->flags
& ECRYPTFS_XATTR_METADATA_ENABLED
)
917 crypt_stat
->flags
|= ECRYPTFS_METADATA_IN_XATTR
;
918 if (mount_crypt_stat
->flags
& ECRYPTFS_ENCRYPTED_VIEW_ENABLED
)
919 crypt_stat
->flags
|= ECRYPTFS_VIEW_AS_ENCRYPTED
;
922 static int ecryptfs_copy_mount_wide_sigs_to_inode_sigs(
923 struct ecryptfs_crypt_stat
*crypt_stat
,
924 struct ecryptfs_mount_crypt_stat
*mount_crypt_stat
)
926 struct ecryptfs_global_auth_tok
*global_auth_tok
;
929 mutex_lock(&mount_crypt_stat
->global_auth_tok_list_mutex
);
930 list_for_each_entry(global_auth_tok
,
931 &mount_crypt_stat
->global_auth_tok_list
,
932 mount_crypt_stat_list
) {
933 rc
= ecryptfs_add_keysig(crypt_stat
, global_auth_tok
->sig
);
935 printk(KERN_ERR
"Error adding keysig; rc = [%d]\n", rc
);
937 &mount_crypt_stat
->global_auth_tok_list_mutex
);
941 mutex_unlock(&mount_crypt_stat
->global_auth_tok_list_mutex
);
947 * ecryptfs_set_default_crypt_stat_vals
948 * @crypt_stat: The inode's cryptographic context
949 * @mount_crypt_stat: The mount point's cryptographic context
951 * Default values in the event that policy does not override them.
953 static void ecryptfs_set_default_crypt_stat_vals(
954 struct ecryptfs_crypt_stat
*crypt_stat
,
955 struct ecryptfs_mount_crypt_stat
*mount_crypt_stat
)
957 ecryptfs_copy_mount_wide_flags_to_inode_flags(crypt_stat
,
959 ecryptfs_set_default_sizes(crypt_stat
);
960 strcpy(crypt_stat
->cipher
, ECRYPTFS_DEFAULT_CIPHER
);
961 crypt_stat
->key_size
= ECRYPTFS_DEFAULT_KEY_BYTES
;
962 crypt_stat
->flags
&= ~(ECRYPTFS_KEY_VALID
);
963 crypt_stat
->file_version
= ECRYPTFS_FILE_VERSION
;
964 crypt_stat
->mount_crypt_stat
= mount_crypt_stat
;
968 * ecryptfs_new_file_context
969 * @ecryptfs_dentry: The eCryptfs dentry
971 * If the crypto context for the file has not yet been established,
972 * this is where we do that. Establishing a new crypto context
973 * involves the following decisions:
974 * - What cipher to use?
975 * - What set of authentication tokens to use?
976 * Here we just worry about getting enough information into the
977 * authentication tokens so that we know that they are available.
978 * We associate the available authentication tokens with the new file
979 * via the set of signatures in the crypt_stat struct. Later, when
980 * the headers are actually written out, we may again defer to
981 * userspace to perform the encryption of the session key; for the
982 * foreseeable future, this will be the case with public key packets.
984 * Returns zero on success; non-zero otherwise
986 int ecryptfs_new_file_context(struct dentry
*ecryptfs_dentry
)
988 struct ecryptfs_crypt_stat
*crypt_stat
=
989 &ecryptfs_inode_to_private(ecryptfs_dentry
->d_inode
)->crypt_stat
;
990 struct ecryptfs_mount_crypt_stat
*mount_crypt_stat
=
991 &ecryptfs_superblock_to_private(
992 ecryptfs_dentry
->d_sb
)->mount_crypt_stat
;
996 ecryptfs_set_default_crypt_stat_vals(crypt_stat
, mount_crypt_stat
);
997 crypt_stat
->flags
|= (ECRYPTFS_ENCRYPTED
| ECRYPTFS_KEY_VALID
);
998 ecryptfs_copy_mount_wide_flags_to_inode_flags(crypt_stat
,
1000 rc
= ecryptfs_copy_mount_wide_sigs_to_inode_sigs(crypt_stat
,
1003 printk(KERN_ERR
"Error attempting to copy mount-wide key sigs "
1004 "to the inode key sigs; rc = [%d]\n", rc
);
1008 strlen(mount_crypt_stat
->global_default_cipher_name
);
1009 memcpy(crypt_stat
->cipher
,
1010 mount_crypt_stat
->global_default_cipher_name
,
1012 crypt_stat
->cipher
[cipher_name_len
] = '\0';
1013 crypt_stat
->key_size
=
1014 mount_crypt_stat
->global_default_cipher_key_size
;
1015 ecryptfs_generate_new_key(crypt_stat
);
1016 rc
= ecryptfs_init_crypt_ctx(crypt_stat
);
1018 ecryptfs_printk(KERN_ERR
, "Error initializing cryptographic "
1019 "context for cipher [%s]: rc = [%d]\n",
1020 crypt_stat
->cipher
, rc
);
1026 * contains_ecryptfs_marker - check for the ecryptfs marker
1027 * @data: The data block in which to check
1029 * Returns one if marker found; zero if not found
1031 static int contains_ecryptfs_marker(char *data
)
1035 memcpy(&m_1
, data
, 4);
1036 m_1
= be32_to_cpu(m_1
);
1037 memcpy(&m_2
, (data
+ 4), 4);
1038 m_2
= be32_to_cpu(m_2
);
1039 if ((m_1
^ MAGIC_ECRYPTFS_MARKER
) == m_2
)
1041 ecryptfs_printk(KERN_DEBUG
, "m_1 = [0x%.8x]; m_2 = [0x%.8x]; "
1042 "MAGIC_ECRYPTFS_MARKER = [0x%.8x]\n", m_1
, m_2
,
1043 MAGIC_ECRYPTFS_MARKER
);
1044 ecryptfs_printk(KERN_DEBUG
, "(m_1 ^ MAGIC_ECRYPTFS_MARKER) = "
1045 "[0x%.8x]\n", (m_1
^ MAGIC_ECRYPTFS_MARKER
));
1049 struct ecryptfs_flag_map_elem
{
1054 /* Add support for additional flags by adding elements here. */
1055 static struct ecryptfs_flag_map_elem ecryptfs_flag_map
[] = {
1056 {0x00000001, ECRYPTFS_ENABLE_HMAC
},
1057 {0x00000002, ECRYPTFS_ENCRYPTED
},
1058 {0x00000004, ECRYPTFS_METADATA_IN_XATTR
}
1062 * ecryptfs_process_flags
1063 * @crypt_stat: The cryptographic context
1064 * @page_virt: Source data to be parsed
1065 * @bytes_read: Updated with the number of bytes read
1067 * Returns zero on success; non-zero if the flag set is invalid
1069 static int ecryptfs_process_flags(struct ecryptfs_crypt_stat
*crypt_stat
,
1070 char *page_virt
, int *bytes_read
)
1076 memcpy(&flags
, page_virt
, 4);
1077 flags
= be32_to_cpu(flags
);
1078 for (i
= 0; i
< ((sizeof(ecryptfs_flag_map
)
1079 / sizeof(struct ecryptfs_flag_map_elem
))); i
++)
1080 if (flags
& ecryptfs_flag_map
[i
].file_flag
) {
1081 crypt_stat
->flags
|= ecryptfs_flag_map
[i
].local_flag
;
1083 crypt_stat
->flags
&= ~(ecryptfs_flag_map
[i
].local_flag
);
1084 /* Version is in top 8 bits of the 32-bit flag vector */
1085 crypt_stat
->file_version
= ((flags
>> 24) & 0xFF);
1091 * write_ecryptfs_marker
1092 * @page_virt: The pointer to in a page to begin writing the marker
1093 * @written: Number of bytes written
1095 * Marker = 0x3c81b7f5
1097 static void write_ecryptfs_marker(char *page_virt
, size_t *written
)
1101 get_random_bytes(&m_1
, (MAGIC_ECRYPTFS_MARKER_SIZE_BYTES
/ 2));
1102 m_2
= (m_1
^ MAGIC_ECRYPTFS_MARKER
);
1103 m_1
= cpu_to_be32(m_1
);
1104 memcpy(page_virt
, &m_1
, (MAGIC_ECRYPTFS_MARKER_SIZE_BYTES
/ 2));
1105 m_2
= cpu_to_be32(m_2
);
1106 memcpy(page_virt
+ (MAGIC_ECRYPTFS_MARKER_SIZE_BYTES
/ 2), &m_2
,
1107 (MAGIC_ECRYPTFS_MARKER_SIZE_BYTES
/ 2));
1108 (*written
) = MAGIC_ECRYPTFS_MARKER_SIZE_BYTES
;
1112 write_ecryptfs_flags(char *page_virt
, struct ecryptfs_crypt_stat
*crypt_stat
,
1118 for (i
= 0; i
< ((sizeof(ecryptfs_flag_map
)
1119 / sizeof(struct ecryptfs_flag_map_elem
))); i
++)
1120 if (crypt_stat
->flags
& ecryptfs_flag_map
[i
].local_flag
)
1121 flags
|= ecryptfs_flag_map
[i
].file_flag
;
1122 /* Version is in top 8 bits of the 32-bit flag vector */
1123 flags
|= ((((u8
)crypt_stat
->file_version
) << 24) & 0xFF000000);
1124 flags
= cpu_to_be32(flags
);
1125 memcpy(page_virt
, &flags
, 4);
1129 struct ecryptfs_cipher_code_str_map_elem
{
1130 char cipher_str
[16];
1134 /* Add support for additional ciphers by adding elements here. The
1135 * cipher_code is whatever OpenPGP applicatoins use to identify the
1136 * ciphers. List in order of probability. */
1137 static struct ecryptfs_cipher_code_str_map_elem
1138 ecryptfs_cipher_code_str_map
[] = {
1139 {"aes",RFC2440_CIPHER_AES_128
},
1140 {"blowfish", RFC2440_CIPHER_BLOWFISH
},
1141 {"des3_ede", RFC2440_CIPHER_DES3_EDE
},
1142 {"cast5", RFC2440_CIPHER_CAST_5
},
1143 {"twofish", RFC2440_CIPHER_TWOFISH
},
1144 {"cast6", RFC2440_CIPHER_CAST_6
},
1145 {"aes", RFC2440_CIPHER_AES_192
},
1146 {"aes", RFC2440_CIPHER_AES_256
}
1150 * ecryptfs_code_for_cipher_string
1151 * @crypt_stat: The cryptographic context
1153 * Returns zero on no match, or the cipher code on match
1155 u16
ecryptfs_code_for_cipher_string(struct ecryptfs_crypt_stat
*crypt_stat
)
1159 struct ecryptfs_cipher_code_str_map_elem
*map
=
1160 ecryptfs_cipher_code_str_map
;
1162 if (strcmp(crypt_stat
->cipher
, "aes") == 0) {
1163 switch (crypt_stat
->key_size
) {
1165 code
= RFC2440_CIPHER_AES_128
;
1168 code
= RFC2440_CIPHER_AES_192
;
1171 code
= RFC2440_CIPHER_AES_256
;
1174 for (i
= 0; i
< ARRAY_SIZE(ecryptfs_cipher_code_str_map
); i
++)
1175 if (strcmp(crypt_stat
->cipher
, map
[i
].cipher_str
) == 0){
1176 code
= map
[i
].cipher_code
;
1184 * ecryptfs_cipher_code_to_string
1185 * @str: Destination to write out the cipher name
1186 * @cipher_code: The code to convert to cipher name string
1188 * Returns zero on success
1190 int ecryptfs_cipher_code_to_string(char *str
, u16 cipher_code
)
1196 for (i
= 0; i
< ARRAY_SIZE(ecryptfs_cipher_code_str_map
); i
++)
1197 if (cipher_code
== ecryptfs_cipher_code_str_map
[i
].cipher_code
)
1198 strcpy(str
, ecryptfs_cipher_code_str_map
[i
].cipher_str
);
1199 if (str
[0] == '\0') {
1200 ecryptfs_printk(KERN_WARNING
, "Cipher code not recognized: "
1201 "[%d]\n", cipher_code
);
1207 int ecryptfs_read_and_validate_header_region(char *data
,
1208 struct inode
*ecryptfs_inode
)
1210 struct ecryptfs_crypt_stat
*crypt_stat
=
1211 &(ecryptfs_inode_to_private(ecryptfs_inode
)->crypt_stat
);
1214 rc
= ecryptfs_read_lower(data
, 0, crypt_stat
->extent_size
,
1217 printk(KERN_ERR
"%s: Error reading header region; rc = [%d]\n",
1221 if (!contains_ecryptfs_marker(data
+ ECRYPTFS_FILE_SIZE_BYTES
)) {
1223 ecryptfs_printk(KERN_DEBUG
, "Valid marker not found\n");
1230 ecryptfs_write_header_metadata(char *virt
,
1231 struct ecryptfs_crypt_stat
*crypt_stat
,
1234 u32 header_extent_size
;
1235 u16 num_header_extents_at_front
;
1237 header_extent_size
= (u32
)crypt_stat
->extent_size
;
1238 num_header_extents_at_front
=
1239 (u16
)crypt_stat
->num_header_extents_at_front
;
1240 header_extent_size
= cpu_to_be32(header_extent_size
);
1241 memcpy(virt
, &header_extent_size
, 4);
1243 num_header_extents_at_front
= cpu_to_be16(num_header_extents_at_front
);
1244 memcpy(virt
, &num_header_extents_at_front
, 2);
1248 struct kmem_cache
*ecryptfs_header_cache_0
;
1249 struct kmem_cache
*ecryptfs_header_cache_1
;
1250 struct kmem_cache
*ecryptfs_header_cache_2
;
1253 * ecryptfs_write_headers_virt
1254 * @page_virt: The virtual address to write the headers to
1255 * @size: Set to the number of bytes written by this function
1256 * @crypt_stat: The cryptographic context
1257 * @ecryptfs_dentry: The eCryptfs dentry
1262 * Octets 0-7: Unencrypted file size (big-endian)
1263 * Octets 8-15: eCryptfs special marker
1264 * Octets 16-19: Flags
1265 * Octet 16: File format version number (between 0 and 255)
1266 * Octets 17-18: Reserved
1267 * Octet 19: Bit 1 (lsb): Reserved
1269 * Bits 3-8: Reserved
1270 * Octets 20-23: Header extent size (big-endian)
1271 * Octets 24-25: Number of header extents at front of file
1273 * Octet 26: Begin RFC 2440 authentication token packet set
1275 * Lower data (CBC encrypted)
1277 * Lower data (CBC encrypted)
1280 * Returns zero on success
1282 static int ecryptfs_write_headers_virt(char *page_virt
, size_t *size
,
1283 struct ecryptfs_crypt_stat
*crypt_stat
,
1284 struct dentry
*ecryptfs_dentry
)
1290 offset
= ECRYPTFS_FILE_SIZE_BYTES
;
1291 write_ecryptfs_marker((page_virt
+ offset
), &written
);
1293 write_ecryptfs_flags((page_virt
+ offset
), crypt_stat
, &written
);
1295 ecryptfs_write_header_metadata((page_virt
+ offset
), crypt_stat
,
1298 rc
= ecryptfs_generate_key_packet_set((page_virt
+ offset
), crypt_stat
,
1299 ecryptfs_dentry
, &written
,
1300 PAGE_CACHE_SIZE
- offset
);
1302 ecryptfs_printk(KERN_WARNING
, "Error generating key packet "
1303 "set; rc = [%d]\n", rc
);
1312 ecryptfs_write_metadata_to_contents(struct ecryptfs_crypt_stat
*crypt_stat
,
1313 struct dentry
*ecryptfs_dentry
,
1316 int current_header_page
;
1320 rc
= ecryptfs_write_lower(ecryptfs_dentry
->d_inode
, page_virt
,
1321 0, PAGE_CACHE_SIZE
);
1323 printk(KERN_ERR
"%s: Error attempting to write header "
1324 "information to lower file; rc = [%d]\n", __FUNCTION__
,
1328 header_pages
= ((crypt_stat
->extent_size
1329 * crypt_stat
->num_header_extents_at_front
)
1331 memset(page_virt
, 0, PAGE_CACHE_SIZE
);
1332 current_header_page
= 1;
1333 while (current_header_page
< header_pages
) {
1336 offset
= (((loff_t
)current_header_page
) << PAGE_CACHE_SHIFT
);
1337 if ((rc
= ecryptfs_write_lower(ecryptfs_dentry
->d_inode
,
1339 PAGE_CACHE_SIZE
))) {
1340 printk(KERN_ERR
"%s: Error attempting to write header "
1341 "information to lower file; rc = [%d]\n",
1345 current_header_page
++;
1352 ecryptfs_write_metadata_to_xattr(struct dentry
*ecryptfs_dentry
,
1353 struct ecryptfs_crypt_stat
*crypt_stat
,
1354 char *page_virt
, size_t size
)
1358 rc
= ecryptfs_setxattr(ecryptfs_dentry
, ECRYPTFS_XATTR_NAME
, page_virt
,
1364 * ecryptfs_write_metadata
1365 * @ecryptfs_dentry: The eCryptfs dentry
1367 * Write the file headers out. This will likely involve a userspace
1368 * callout, in which the session key is encrypted with one or more
1369 * public keys and/or the passphrase necessary to do the encryption is
1370 * retrieved via a prompt. Exactly what happens at this point should
1371 * be policy-dependent.
1373 * TODO: Support header information spanning multiple pages
1375 * Returns zero on success; non-zero on error
1377 int ecryptfs_write_metadata(struct dentry
*ecryptfs_dentry
)
1379 struct ecryptfs_crypt_stat
*crypt_stat
=
1380 &ecryptfs_inode_to_private(ecryptfs_dentry
->d_inode
)->crypt_stat
;
1385 if (likely(crypt_stat
->flags
& ECRYPTFS_ENCRYPTED
)) {
1386 if (!(crypt_stat
->flags
& ECRYPTFS_KEY_VALID
)) {
1387 printk(KERN_ERR
"Key is invalid; bailing out\n");
1393 ecryptfs_printk(KERN_WARNING
,
1394 "Called with crypt_stat->encrypted == 0\n");
1397 /* Released in this function */
1398 page_virt
= kmem_cache_zalloc(ecryptfs_header_cache_0
, GFP_USER
);
1400 ecryptfs_printk(KERN_ERR
, "Out of memory\n");
1404 rc
= ecryptfs_write_headers_virt(page_virt
, &size
, crypt_stat
,
1407 ecryptfs_printk(KERN_ERR
, "Error whilst writing headers\n");
1408 memset(page_virt
, 0, PAGE_CACHE_SIZE
);
1411 if (crypt_stat
->flags
& ECRYPTFS_METADATA_IN_XATTR
)
1412 rc
= ecryptfs_write_metadata_to_xattr(ecryptfs_dentry
,
1413 crypt_stat
, page_virt
,
1416 rc
= ecryptfs_write_metadata_to_contents(crypt_stat
,
1420 printk(KERN_ERR
"Error writing metadata out to lower file; "
1425 kmem_cache_free(ecryptfs_header_cache_0
, page_virt
);
1430 #define ECRYPTFS_DONT_VALIDATE_HEADER_SIZE 0
1431 #define ECRYPTFS_VALIDATE_HEADER_SIZE 1
1432 static int parse_header_metadata(struct ecryptfs_crypt_stat
*crypt_stat
,
1433 char *virt
, int *bytes_read
,
1434 int validate_header_size
)
1437 u32 header_extent_size
;
1438 u16 num_header_extents_at_front
;
1440 memcpy(&header_extent_size
, virt
, sizeof(u32
));
1441 header_extent_size
= be32_to_cpu(header_extent_size
);
1442 virt
+= sizeof(u32
);
1443 memcpy(&num_header_extents_at_front
, virt
, sizeof(u16
));
1444 num_header_extents_at_front
= be16_to_cpu(num_header_extents_at_front
);
1445 crypt_stat
->num_header_extents_at_front
=
1446 (int)num_header_extents_at_front
;
1447 (*bytes_read
) = (sizeof(u32
) + sizeof(u16
));
1448 if ((validate_header_size
== ECRYPTFS_VALIDATE_HEADER_SIZE
)
1449 && ((crypt_stat
->extent_size
1450 * crypt_stat
->num_header_extents_at_front
)
1451 < ECRYPTFS_MINIMUM_HEADER_EXTENT_SIZE
)) {
1453 printk(KERN_WARNING
"Invalid number of header extents: [%zd]\n",
1454 crypt_stat
->num_header_extents_at_front
);
1460 * set_default_header_data
1461 * @crypt_stat: The cryptographic context
1463 * For version 0 file format; this function is only for backwards
1464 * compatibility for files created with the prior versions of
1467 static void set_default_header_data(struct ecryptfs_crypt_stat
*crypt_stat
)
1469 crypt_stat
->num_header_extents_at_front
= 2;
1473 * ecryptfs_read_headers_virt
1474 * @page_virt: The virtual address into which to read the headers
1475 * @crypt_stat: The cryptographic context
1476 * @ecryptfs_dentry: The eCryptfs dentry
1477 * @validate_header_size: Whether to validate the header size while reading
1479 * Read/parse the header data. The header format is detailed in the
1480 * comment block for the ecryptfs_write_headers_virt() function.
1482 * Returns zero on success
1484 static int ecryptfs_read_headers_virt(char *page_virt
,
1485 struct ecryptfs_crypt_stat
*crypt_stat
,
1486 struct dentry
*ecryptfs_dentry
,
1487 int validate_header_size
)
1493 ecryptfs_set_default_sizes(crypt_stat
);
1494 crypt_stat
->mount_crypt_stat
= &ecryptfs_superblock_to_private(
1495 ecryptfs_dentry
->d_sb
)->mount_crypt_stat
;
1496 offset
= ECRYPTFS_FILE_SIZE_BYTES
;
1497 rc
= contains_ecryptfs_marker(page_virt
+ offset
);
1502 offset
+= MAGIC_ECRYPTFS_MARKER_SIZE_BYTES
;
1503 rc
= ecryptfs_process_flags(crypt_stat
, (page_virt
+ offset
),
1506 ecryptfs_printk(KERN_WARNING
, "Error processing flags\n");
1509 if (crypt_stat
->file_version
> ECRYPTFS_SUPPORTED_FILE_VERSION
) {
1510 ecryptfs_printk(KERN_WARNING
, "File version is [%d]; only "
1511 "file version [%d] is supported by this "
1512 "version of eCryptfs\n",
1513 crypt_stat
->file_version
,
1514 ECRYPTFS_SUPPORTED_FILE_VERSION
);
1518 offset
+= bytes_read
;
1519 if (crypt_stat
->file_version
>= 1) {
1520 rc
= parse_header_metadata(crypt_stat
, (page_virt
+ offset
),
1521 &bytes_read
, validate_header_size
);
1523 ecryptfs_printk(KERN_WARNING
, "Error reading header "
1524 "metadata; rc = [%d]\n", rc
);
1526 offset
+= bytes_read
;
1528 set_default_header_data(crypt_stat
);
1529 rc
= ecryptfs_parse_packet_set(crypt_stat
, (page_virt
+ offset
),
1536 * ecryptfs_read_xattr_region
1537 * @page_virt: The vitual address into which to read the xattr data
1538 * @ecryptfs_inode: The eCryptfs inode
1540 * Attempts to read the crypto metadata from the extended attribute
1541 * region of the lower file.
1543 * Returns zero on success; non-zero on error
1545 int ecryptfs_read_xattr_region(char *page_virt
, struct inode
*ecryptfs_inode
)
1547 struct dentry
*lower_dentry
=
1548 ecryptfs_inode_to_private(ecryptfs_inode
)->lower_file
->f_dentry
;
1552 size
= ecryptfs_getxattr_lower(lower_dentry
, ECRYPTFS_XATTR_NAME
,
1553 page_virt
, ECRYPTFS_DEFAULT_EXTENT_SIZE
);
1555 printk(KERN_ERR
"Error attempting to read the [%s] "
1556 "xattr from the lower file; return value = [%zd]\n",
1557 ECRYPTFS_XATTR_NAME
, size
);
1565 int ecryptfs_read_and_validate_xattr_region(char *page_virt
,
1566 struct dentry
*ecryptfs_dentry
)
1570 rc
= ecryptfs_read_xattr_region(page_virt
, ecryptfs_dentry
->d_inode
);
1573 if (!contains_ecryptfs_marker(page_virt
+ ECRYPTFS_FILE_SIZE_BYTES
)) {
1574 printk(KERN_WARNING
"Valid data found in [%s] xattr, but "
1575 "the marker is invalid\n", ECRYPTFS_XATTR_NAME
);
1583 * ecryptfs_read_metadata
1585 * Common entry point for reading file metadata. From here, we could
1586 * retrieve the header information from the header region of the file,
1587 * the xattr region of the file, or some other repostory that is
1588 * stored separately from the file itself. The current implementation
1589 * supports retrieving the metadata information from the file contents
1590 * and from the xattr region.
1592 * Returns zero if valid headers found and parsed; non-zero otherwise
1594 int ecryptfs_read_metadata(struct dentry
*ecryptfs_dentry
)
1597 char *page_virt
= NULL
;
1598 struct inode
*ecryptfs_inode
= ecryptfs_dentry
->d_inode
;
1599 struct ecryptfs_crypt_stat
*crypt_stat
=
1600 &ecryptfs_inode_to_private(ecryptfs_inode
)->crypt_stat
;
1601 struct ecryptfs_mount_crypt_stat
*mount_crypt_stat
=
1602 &ecryptfs_superblock_to_private(
1603 ecryptfs_dentry
->d_sb
)->mount_crypt_stat
;
1605 ecryptfs_copy_mount_wide_flags_to_inode_flags(crypt_stat
,
1607 /* Read the first page from the underlying file */
1608 page_virt
= kmem_cache_alloc(ecryptfs_header_cache_1
, GFP_USER
);
1611 printk(KERN_ERR
"%s: Unable to allocate page_virt\n",
1615 rc
= ecryptfs_read_lower(page_virt
, 0, crypt_stat
->extent_size
,
1618 rc
= ecryptfs_read_headers_virt(page_virt
, crypt_stat
,
1620 ECRYPTFS_VALIDATE_HEADER_SIZE
);
1622 rc
= ecryptfs_read_xattr_region(page_virt
, ecryptfs_inode
);
1624 printk(KERN_DEBUG
"Valid eCryptfs headers not found in "
1625 "file header region or xattr region\n");
1629 rc
= ecryptfs_read_headers_virt(page_virt
, crypt_stat
,
1631 ECRYPTFS_DONT_VALIDATE_HEADER_SIZE
);
1633 printk(KERN_DEBUG
"Valid eCryptfs headers not found in "
1634 "file xattr region either\n");
1637 if (crypt_stat
->mount_crypt_stat
->flags
1638 & ECRYPTFS_XATTR_METADATA_ENABLED
) {
1639 crypt_stat
->flags
|= ECRYPTFS_METADATA_IN_XATTR
;
1641 printk(KERN_WARNING
"Attempt to access file with "
1642 "crypto metadata only in the extended attribute "
1643 "region, but eCryptfs was mounted without "
1644 "xattr support enabled. eCryptfs will not treat "
1645 "this like an encrypted file.\n");
1651 memset(page_virt
, 0, PAGE_CACHE_SIZE
);
1652 kmem_cache_free(ecryptfs_header_cache_1
, page_virt
);
1658 * ecryptfs_encode_filename - converts a plaintext file name to cipher text
1659 * @crypt_stat: The crypt_stat struct associated with the file anem to encode
1660 * @name: The plaintext name
1661 * @length: The length of the plaintext
1662 * @encoded_name: The encypted name
1664 * Encrypts and encodes a filename into something that constitutes a
1665 * valid filename for a filesystem, with printable characters.
1667 * We assume that we have a properly initialized crypto context,
1668 * pointed to by crypt_stat->tfm.
1670 * TODO: Implement filename decoding and decryption here, in place of
1671 * memcpy. We are keeping the framework around for now to (1)
1672 * facilitate testing of the components needed to implement filename
1673 * encryption and (2) to provide a code base from which other
1674 * developers in the community can easily implement this feature.
1676 * Returns the length of encoded filename; negative if error
1679 ecryptfs_encode_filename(struct ecryptfs_crypt_stat
*crypt_stat
,
1680 const char *name
, int length
, char **encoded_name
)
1684 (*encoded_name
) = kmalloc(length
+ 2, GFP_KERNEL
);
1685 if (!(*encoded_name
)) {
1689 /* TODO: Filename encryption is a scheduled feature for a
1690 * future version of eCryptfs. This function is here only for
1691 * the purpose of providing a framework for other developers
1692 * to easily implement filename encryption. Hint: Replace this
1693 * memcpy() with a call to encrypt and encode the
1694 * filename, the set the length accordingly. */
1695 memcpy((void *)(*encoded_name
), (void *)name
, length
);
1696 (*encoded_name
)[length
] = '\0';
1703 * ecryptfs_decode_filename - converts the cipher text name to plaintext
1704 * @crypt_stat: The crypt_stat struct associated with the file
1705 * @name: The filename in cipher text
1706 * @length: The length of the cipher text name
1707 * @decrypted_name: The plaintext name
1709 * Decodes and decrypts the filename.
1711 * We assume that we have a properly initialized crypto context,
1712 * pointed to by crypt_stat->tfm.
1714 * TODO: Implement filename decoding and decryption here, in place of
1715 * memcpy. We are keeping the framework around for now to (1)
1716 * facilitate testing of the components needed to implement filename
1717 * encryption and (2) to provide a code base from which other
1718 * developers in the community can easily implement this feature.
1720 * Returns the length of decoded filename; negative if error
1723 ecryptfs_decode_filename(struct ecryptfs_crypt_stat
*crypt_stat
,
1724 const char *name
, int length
, char **decrypted_name
)
1728 (*decrypted_name
) = kmalloc(length
+ 2, GFP_KERNEL
);
1729 if (!(*decrypted_name
)) {
1733 /* TODO: Filename encryption is a scheduled feature for a
1734 * future version of eCryptfs. This function is here only for
1735 * the purpose of providing a framework for other developers
1736 * to easily implement filename encryption. Hint: Replace this
1737 * memcpy() with a call to decode and decrypt the
1738 * filename, the set the length accordingly. */
1739 memcpy((void *)(*decrypted_name
), (void *)name
, length
);
1740 (*decrypted_name
)[length
+ 1] = '\0'; /* Only for convenience
1741 * in printing out the
1750 * ecryptfs_process_key_cipher - Perform key cipher initialization.
1751 * @key_tfm: Crypto context for key material, set by this function
1752 * @cipher_name: Name of the cipher
1753 * @key_size: Size of the key in bytes
1755 * Returns zero on success. Any crypto_tfm structs allocated here
1756 * should be released by other functions, such as on a superblock put
1757 * event, regardless of whether this function succeeds for fails.
1760 ecryptfs_process_key_cipher(struct crypto_blkcipher
**key_tfm
,
1761 char *cipher_name
, size_t *key_size
)
1763 char dummy_key
[ECRYPTFS_MAX_KEY_BYTES
];
1764 char *full_alg_name
;
1768 if (*key_size
> ECRYPTFS_MAX_KEY_BYTES
) {
1770 printk(KERN_ERR
"Requested key size is [%Zd] bytes; maximum "
1771 "allowable is [%d]\n", *key_size
, ECRYPTFS_MAX_KEY_BYTES
);
1774 rc
= ecryptfs_crypto_api_algify_cipher_name(&full_alg_name
, cipher_name
,
1778 *key_tfm
= crypto_alloc_blkcipher(full_alg_name
, 0, CRYPTO_ALG_ASYNC
);
1779 kfree(full_alg_name
);
1780 if (IS_ERR(*key_tfm
)) {
1781 rc
= PTR_ERR(*key_tfm
);
1782 printk(KERN_ERR
"Unable to allocate crypto cipher with name "
1783 "[%s]; rc = [%d]\n", cipher_name
, rc
);
1786 crypto_blkcipher_set_flags(*key_tfm
, CRYPTO_TFM_REQ_WEAK_KEY
);
1787 if (*key_size
== 0) {
1788 struct blkcipher_alg
*alg
= crypto_blkcipher_alg(*key_tfm
);
1790 *key_size
= alg
->max_keysize
;
1792 get_random_bytes(dummy_key
, *key_size
);
1793 rc
= crypto_blkcipher_setkey(*key_tfm
, dummy_key
, *key_size
);
1795 printk(KERN_ERR
"Error attempting to set key of size [%Zd] for "
1796 "cipher [%s]; rc = [%d]\n", *key_size
, cipher_name
, rc
);
1804 struct kmem_cache
*ecryptfs_key_tfm_cache
;
1805 struct list_head key_tfm_list
;
1806 struct mutex key_tfm_list_mutex
;
1808 int ecryptfs_init_crypto(void)
1810 mutex_init(&key_tfm_list_mutex
);
1811 INIT_LIST_HEAD(&key_tfm_list
);
1815 int ecryptfs_destroy_crypto(void)
1817 struct ecryptfs_key_tfm
*key_tfm
, *key_tfm_tmp
;
1819 mutex_lock(&key_tfm_list_mutex
);
1820 list_for_each_entry_safe(key_tfm
, key_tfm_tmp
, &key_tfm_list
,
1822 list_del(&key_tfm
->key_tfm_list
);
1823 if (key_tfm
->key_tfm
)
1824 crypto_free_blkcipher(key_tfm
->key_tfm
);
1825 kmem_cache_free(ecryptfs_key_tfm_cache
, key_tfm
);
1827 mutex_unlock(&key_tfm_list_mutex
);
1832 ecryptfs_add_new_key_tfm(struct ecryptfs_key_tfm
**key_tfm
, char *cipher_name
,
1835 struct ecryptfs_key_tfm
*tmp_tfm
;
1838 tmp_tfm
= kmem_cache_alloc(ecryptfs_key_tfm_cache
, GFP_KERNEL
);
1839 if (key_tfm
!= NULL
)
1840 (*key_tfm
) = tmp_tfm
;
1843 printk(KERN_ERR
"Error attempting to allocate from "
1844 "ecryptfs_key_tfm_cache\n");
1847 mutex_init(&tmp_tfm
->key_tfm_mutex
);
1848 strncpy(tmp_tfm
->cipher_name
, cipher_name
,
1849 ECRYPTFS_MAX_CIPHER_NAME_SIZE
);
1850 tmp_tfm
->cipher_name
[ECRYPTFS_MAX_CIPHER_NAME_SIZE
] = '\0';
1851 tmp_tfm
->key_size
= key_size
;
1852 rc
= ecryptfs_process_key_cipher(&tmp_tfm
->key_tfm
,
1853 tmp_tfm
->cipher_name
,
1854 &tmp_tfm
->key_size
);
1856 printk(KERN_ERR
"Error attempting to initialize key TFM "
1857 "cipher with name = [%s]; rc = [%d]\n",
1858 tmp_tfm
->cipher_name
, rc
);
1859 kmem_cache_free(ecryptfs_key_tfm_cache
, tmp_tfm
);
1860 if (key_tfm
!= NULL
)
1864 mutex_lock(&key_tfm_list_mutex
);
1865 list_add(&tmp_tfm
->key_tfm_list
, &key_tfm_list
);
1866 mutex_unlock(&key_tfm_list_mutex
);
1871 int ecryptfs_get_tfm_and_mutex_for_cipher_name(struct crypto_blkcipher
**tfm
,
1872 struct mutex
**tfm_mutex
,
1875 struct ecryptfs_key_tfm
*key_tfm
;
1879 (*tfm_mutex
) = NULL
;
1880 mutex_lock(&key_tfm_list_mutex
);
1881 list_for_each_entry(key_tfm
, &key_tfm_list
, key_tfm_list
) {
1882 if (strcmp(key_tfm
->cipher_name
, cipher_name
) == 0) {
1883 (*tfm
) = key_tfm
->key_tfm
;
1884 (*tfm_mutex
) = &key_tfm
->key_tfm_mutex
;
1885 mutex_unlock(&key_tfm_list_mutex
);
1889 mutex_unlock(&key_tfm_list_mutex
);
1890 rc
= ecryptfs_add_new_key_tfm(&key_tfm
, cipher_name
, 0);
1892 printk(KERN_ERR
"Error adding new key_tfm to list; rc = [%d]\n",
1896 (*tfm
) = key_tfm
->key_tfm
;
1897 (*tfm_mutex
) = &key_tfm
->key_tfm_mutex
;