1 // SPDX-License-Identifier: GPL-2.0-or-later
3 * eCryptfs: Linux filesystem encryption layer
5 * Copyright (C) 1997-2004 Erez Zadok
6 * Copyright (C) 2001-2004 Stony Brook University
7 * Copyright (C) 2004-2007 International Business Machines Corp.
8 * Author(s): Michael A. Halcrow <mahalcro@us.ibm.com>
9 * Michael C. Thompson <mcthomps@us.ibm.com>
12 #include <crypto/hash.h>
13 #include <crypto/skcipher.h>
15 #include <linux/mount.h>
16 #include <linux/pagemap.h>
17 #include <linux/random.h>
18 #include <linux/compiler.h>
19 #include <linux/key.h>
20 #include <linux/namei.h>
21 #include <linux/file.h>
22 #include <linux/scatterlist.h>
23 #include <linux/slab.h>
24 #include <asm/unaligned.h>
25 #include <linux/kernel.h>
26 #include "ecryptfs_kernel.h"
33 * @dst: Buffer to take the bytes from src hex; must be at least of
35 * @src: Buffer to be converted from a hex string representation to raw value
36 * @dst_size: size of dst buffer, or number of hex characters pairs to convert
38 void ecryptfs_from_hex(char *dst, char *src, int dst_size)
43 for (x = 0; x < dst_size; x++) {
45 tmp[1] = src[x * 2 + 1];
46 dst[x] = (unsigned char)simple_strtol(tmp, NULL, 16);
50 static int ecryptfs_hash_digest(struct crypto_shash *tfm,
51 char *src, int len, char *dst)
53 SHASH_DESC_ON_STACK(desc, tfm);
57 err = crypto_shash_digest(desc, src, len, dst);
58 shash_desc_zero(desc);
63 * ecryptfs_calculate_md5 - calculates the md5 of @src
64 * @dst: Pointer to 16 bytes of allocated memory
65 * @crypt_stat: Pointer to crypt_stat struct for the current inode
66 * @src: Data to be md5'd
67 * @len: Length of @src
69 * Uses the allocated crypto context that crypt_stat references to
70 * generate the MD5 sum of the contents of src.
72 static int ecryptfs_calculate_md5(char *dst,
73 struct ecryptfs_crypt_stat *crypt_stat,
76 struct crypto_shash *tfm;
79 tfm = crypt_stat->hash_tfm;
80 rc = ecryptfs_hash_digest(tfm, src, len, dst);
83 "%s: Error computing crypto hash; rc = [%d]\n",
91 static int ecryptfs_crypto_api_algify_cipher_name(char **algified_name,
93 char *chaining_modifier)
95 int cipher_name_len = strlen(cipher_name);
96 int chaining_modifier_len = strlen(chaining_modifier);
97 int algified_name_len;
100 algified_name_len = (chaining_modifier_len + cipher_name_len + 3);
101 (*algified_name) = kmalloc(algified_name_len, GFP_KERNEL);
102 if (!(*algified_name)) {
106 snprintf((*algified_name), algified_name_len, "%s(%s)",
107 chaining_modifier, cipher_name);
115 * @iv: destination for the derived iv vale
116 * @crypt_stat: Pointer to crypt_stat struct for the current inode
117 * @offset: Offset of the extent whose IV we are to derive
119 * Generate the initialization vector from the given root IV and page
122 * Returns zero on success; non-zero on error.
124 int ecryptfs_derive_iv(char *iv, struct ecryptfs_crypt_stat *crypt_stat,
128 char dst[MD5_DIGEST_SIZE];
129 char src[ECRYPTFS_MAX_IV_BYTES + 16];
131 if (unlikely(ecryptfs_verbosity > 0)) {
132 ecryptfs_printk(KERN_DEBUG, "root iv:\n");
133 ecryptfs_dump_hex(crypt_stat->root_iv, crypt_stat->iv_bytes);
135 /* TODO: It is probably secure to just cast the least
136 * significant bits of the root IV into an unsigned long and
137 * add the offset to that rather than go through all this
138 * hashing business. -Halcrow */
139 memcpy(src, crypt_stat->root_iv, crypt_stat->iv_bytes);
140 memset((src + crypt_stat->iv_bytes), 0, 16);
141 snprintf((src + crypt_stat->iv_bytes), 16, "%lld", offset);
142 if (unlikely(ecryptfs_verbosity > 0)) {
143 ecryptfs_printk(KERN_DEBUG, "source:\n");
144 ecryptfs_dump_hex(src, (crypt_stat->iv_bytes + 16));
146 rc = ecryptfs_calculate_md5(dst, crypt_stat, src,
147 (crypt_stat->iv_bytes + 16));
149 ecryptfs_printk(KERN_WARNING, "Error attempting to compute "
150 "MD5 while generating IV for a page\n");
153 memcpy(iv, dst, crypt_stat->iv_bytes);
154 if (unlikely(ecryptfs_verbosity > 0)) {
155 ecryptfs_printk(KERN_DEBUG, "derived iv:\n");
156 ecryptfs_dump_hex(iv, crypt_stat->iv_bytes);
163 * ecryptfs_init_crypt_stat
164 * @crypt_stat: Pointer to the crypt_stat struct to initialize.
166 * Initialize the crypt_stat structure.
168 int ecryptfs_init_crypt_stat(struct ecryptfs_crypt_stat *crypt_stat)
170 struct crypto_shash *tfm;
173 tfm = crypto_alloc_shash(ECRYPTFS_DEFAULT_HASH, 0, 0);
176 ecryptfs_printk(KERN_ERR, "Error attempting to "
177 "allocate crypto context; rc = [%d]\n",
182 memset((void *)crypt_stat, 0, sizeof(struct ecryptfs_crypt_stat));
183 INIT_LIST_HEAD(&crypt_stat->keysig_list);
184 mutex_init(&crypt_stat->keysig_list_mutex);
185 mutex_init(&crypt_stat->cs_mutex);
186 mutex_init(&crypt_stat->cs_tfm_mutex);
187 crypt_stat->hash_tfm = tfm;
188 crypt_stat->flags |= ECRYPTFS_STRUCT_INITIALIZED;
194 * ecryptfs_destroy_crypt_stat
195 * @crypt_stat: Pointer to the crypt_stat struct to initialize.
197 * Releases all memory associated with a crypt_stat struct.
199 void ecryptfs_destroy_crypt_stat(struct ecryptfs_crypt_stat *crypt_stat)
201 struct ecryptfs_key_sig *key_sig, *key_sig_tmp;
203 crypto_free_skcipher(crypt_stat->tfm);
204 crypto_free_shash(crypt_stat->hash_tfm);
205 list_for_each_entry_safe(key_sig, key_sig_tmp,
206 &crypt_stat->keysig_list, crypt_stat_list) {
207 list_del(&key_sig->crypt_stat_list);
208 kmem_cache_free(ecryptfs_key_sig_cache, key_sig);
210 memset(crypt_stat, 0, sizeof(struct ecryptfs_crypt_stat));
213 void ecryptfs_destroy_mount_crypt_stat(
214 struct ecryptfs_mount_crypt_stat *mount_crypt_stat)
216 struct ecryptfs_global_auth_tok *auth_tok, *auth_tok_tmp;
218 if (!(mount_crypt_stat->flags & ECRYPTFS_MOUNT_CRYPT_STAT_INITIALIZED))
220 mutex_lock(&mount_crypt_stat->global_auth_tok_list_mutex);
221 list_for_each_entry_safe(auth_tok, auth_tok_tmp,
222 &mount_crypt_stat->global_auth_tok_list,
223 mount_crypt_stat_list) {
224 list_del(&auth_tok->mount_crypt_stat_list);
225 if (!(auth_tok->flags & ECRYPTFS_AUTH_TOK_INVALID))
226 key_put(auth_tok->global_auth_tok_key);
227 kmem_cache_free(ecryptfs_global_auth_tok_cache, auth_tok);
229 mutex_unlock(&mount_crypt_stat->global_auth_tok_list_mutex);
230 memset(mount_crypt_stat, 0, sizeof(struct ecryptfs_mount_crypt_stat));
234 * virt_to_scatterlist
235 * @addr: Virtual address
236 * @size: Size of data; should be an even multiple of the block size
237 * @sg: Pointer to scatterlist array; set to NULL to obtain only
238 * the number of scatterlist structs required in array
239 * @sg_size: Max array size
241 * Fills in a scatterlist array with page references for a passed
244 * Returns the number of scatterlist structs in array used
246 int virt_to_scatterlist(const void *addr, int size, struct scatterlist *sg,
252 int remainder_of_page;
254 sg_init_table(sg, sg_size);
256 while (size > 0 && i < sg_size) {
257 pg = virt_to_page(addr);
258 offset = offset_in_page(addr);
259 sg_set_page(&sg[i], pg, 0, offset);
260 remainder_of_page = PAGE_SIZE - offset;
261 if (size >= remainder_of_page) {
262 sg[i].length = remainder_of_page;
263 addr += remainder_of_page;
264 size -= remainder_of_page;
277 struct extent_crypt_result {
278 struct completion completion;
282 static void extent_crypt_complete(struct crypto_async_request *req, int rc)
284 struct extent_crypt_result *ecr = req->data;
286 if (rc == -EINPROGRESS)
290 complete(&ecr->completion);
295 * @crypt_stat: Pointer to the crypt_stat struct to initialize.
296 * @dst_sg: Destination of the data after performing the crypto operation
297 * @src_sg: Data to be encrypted or decrypted
298 * @size: Length of data
300 * @op: ENCRYPT or DECRYPT to indicate the desired operation
302 * Returns the number of bytes encrypted or decrypted; negative value on error
304 static int crypt_scatterlist(struct ecryptfs_crypt_stat *crypt_stat,
305 struct scatterlist *dst_sg,
306 struct scatterlist *src_sg, int size,
307 unsigned char *iv, int op)
309 struct skcipher_request *req = NULL;
310 struct extent_crypt_result ecr;
313 BUG_ON(!crypt_stat || !crypt_stat->tfm
314 || !(crypt_stat->flags & ECRYPTFS_STRUCT_INITIALIZED));
315 if (unlikely(ecryptfs_verbosity > 0)) {
316 ecryptfs_printk(KERN_DEBUG, "Key size [%zd]; key:\n",
317 crypt_stat->key_size);
318 ecryptfs_dump_hex(crypt_stat->key,
319 crypt_stat->key_size);
322 init_completion(&ecr.completion);
324 mutex_lock(&crypt_stat->cs_tfm_mutex);
325 req = skcipher_request_alloc(crypt_stat->tfm, GFP_NOFS);
327 mutex_unlock(&crypt_stat->cs_tfm_mutex);
332 skcipher_request_set_callback(req,
333 CRYPTO_TFM_REQ_MAY_BACKLOG | CRYPTO_TFM_REQ_MAY_SLEEP,
334 extent_crypt_complete, &ecr);
335 /* Consider doing this once, when the file is opened */
336 if (!(crypt_stat->flags & ECRYPTFS_KEY_SET)) {
337 rc = crypto_skcipher_setkey(crypt_stat->tfm, crypt_stat->key,
338 crypt_stat->key_size);
340 ecryptfs_printk(KERN_ERR,
341 "Error setting key; rc = [%d]\n",
343 mutex_unlock(&crypt_stat->cs_tfm_mutex);
347 crypt_stat->flags |= ECRYPTFS_KEY_SET;
349 mutex_unlock(&crypt_stat->cs_tfm_mutex);
350 skcipher_request_set_crypt(req, src_sg, dst_sg, size, iv);
351 rc = op == ENCRYPT ? crypto_skcipher_encrypt(req) :
352 crypto_skcipher_decrypt(req);
353 if (rc == -EINPROGRESS || rc == -EBUSY) {
354 struct extent_crypt_result *ecr = req->base.data;
356 wait_for_completion(&ecr->completion);
358 reinit_completion(&ecr->completion);
361 skcipher_request_free(req);
366 * lower_offset_for_page
368 * Convert an eCryptfs page index into a lower byte offset
370 static loff_t lower_offset_for_page(struct ecryptfs_crypt_stat *crypt_stat,
373 return ecryptfs_lower_header_size(crypt_stat) +
374 ((loff_t)page->index << PAGE_SHIFT);
379 * @crypt_stat: crypt_stat containing cryptographic context for the
380 * encryption operation
381 * @dst_page: The page to write the result into
382 * @src_page: The page to read from
383 * @extent_offset: Page extent offset for use in generating IV
384 * @op: ENCRYPT or DECRYPT to indicate the desired operation
386 * Encrypts or decrypts one extent of data.
388 * Return zero on success; non-zero otherwise
390 static int crypt_extent(struct ecryptfs_crypt_stat *crypt_stat,
391 struct page *dst_page,
392 struct page *src_page,
393 unsigned long extent_offset, int op)
395 pgoff_t page_index = op == ENCRYPT ? src_page->index : dst_page->index;
397 char extent_iv[ECRYPTFS_MAX_IV_BYTES];
398 struct scatterlist src_sg, dst_sg;
399 size_t extent_size = crypt_stat->extent_size;
402 extent_base = (((loff_t)page_index) * (PAGE_SIZE / extent_size));
403 rc = ecryptfs_derive_iv(extent_iv, crypt_stat,
404 (extent_base + extent_offset));
406 ecryptfs_printk(KERN_ERR, "Error attempting to derive IV for "
407 "extent [0x%.16llx]; rc = [%d]\n",
408 (unsigned long long)(extent_base + extent_offset), rc);
412 sg_init_table(&src_sg, 1);
413 sg_init_table(&dst_sg, 1);
415 sg_set_page(&src_sg, src_page, extent_size,
416 extent_offset * extent_size);
417 sg_set_page(&dst_sg, dst_page, extent_size,
418 extent_offset * extent_size);
420 rc = crypt_scatterlist(crypt_stat, &dst_sg, &src_sg, extent_size,
423 printk(KERN_ERR "%s: Error attempting to crypt page with "
424 "page_index = [%ld], extent_offset = [%ld]; "
425 "rc = [%d]\n", __func__, page_index, extent_offset, rc);
434 * ecryptfs_encrypt_page
435 * @page: Page mapped from the eCryptfs inode for the file; contains
436 * decrypted content that needs to be encrypted (to a temporary
437 * page; not in place) and written out to the lower file
439 * Encrypt an eCryptfs page. This is done on a per-extent basis. Note
440 * that eCryptfs pages may straddle the lower pages -- for instance,
441 * if the file was created on a machine with an 8K page size
442 * (resulting in an 8K header), and then the file is copied onto a
443 * host with a 32K page size, then when reading page 0 of the eCryptfs
444 * file, 24K of page 0 of the lower file will be read and decrypted,
445 * and then 8K of page 1 of the lower file will be read and decrypted.
447 * Returns zero on success; negative on error
449 int ecryptfs_encrypt_page(struct page *page)
451 struct inode *ecryptfs_inode;
452 struct ecryptfs_crypt_stat *crypt_stat;
453 char *enc_extent_virt;
454 struct page *enc_extent_page = NULL;
455 loff_t extent_offset;
459 ecryptfs_inode = page->mapping->host;
461 &(ecryptfs_inode_to_private(ecryptfs_inode)->crypt_stat);
462 BUG_ON(!(crypt_stat->flags & ECRYPTFS_ENCRYPTED));
463 enc_extent_page = alloc_page(GFP_USER);
464 if (!enc_extent_page) {
466 ecryptfs_printk(KERN_ERR, "Error allocating memory for "
467 "encrypted extent\n");
471 for (extent_offset = 0;
472 extent_offset < (PAGE_SIZE / crypt_stat->extent_size);
474 rc = crypt_extent(crypt_stat, enc_extent_page, page,
475 extent_offset, ENCRYPT);
477 printk(KERN_ERR "%s: Error encrypting extent; "
478 "rc = [%d]\n", __func__, rc);
483 lower_offset = lower_offset_for_page(crypt_stat, page);
484 enc_extent_virt = kmap(enc_extent_page);
485 rc = ecryptfs_write_lower(ecryptfs_inode, enc_extent_virt, lower_offset,
487 kunmap(enc_extent_page);
489 ecryptfs_printk(KERN_ERR,
490 "Error attempting to write lower page; rc = [%d]\n",
496 if (enc_extent_page) {
497 __free_page(enc_extent_page);
503 * ecryptfs_decrypt_page
504 * @page: Page mapped from the eCryptfs inode for the file; data read
505 * and decrypted from the lower file will be written into this
508 * Decrypt an eCryptfs page. This is done on a per-extent basis. Note
509 * that eCryptfs pages may straddle the lower pages -- for instance,
510 * if the file was created on a machine with an 8K page size
511 * (resulting in an 8K header), and then the file is copied onto a
512 * host with a 32K page size, then when reading page 0 of the eCryptfs
513 * file, 24K of page 0 of the lower file will be read and decrypted,
514 * and then 8K of page 1 of the lower file will be read and decrypted.
516 * Returns zero on success; negative on error
518 int ecryptfs_decrypt_page(struct page *page)
520 struct inode *ecryptfs_inode;
521 struct ecryptfs_crypt_stat *crypt_stat;
523 unsigned long extent_offset;
527 ecryptfs_inode = page->mapping->host;
529 &(ecryptfs_inode_to_private(ecryptfs_inode)->crypt_stat);
530 BUG_ON(!(crypt_stat->flags & ECRYPTFS_ENCRYPTED));
532 lower_offset = lower_offset_for_page(crypt_stat, page);
533 page_virt = kmap(page);
534 rc = ecryptfs_read_lower(page_virt, lower_offset, PAGE_SIZE,
538 ecryptfs_printk(KERN_ERR,
539 "Error attempting to read lower page; rc = [%d]\n",
544 for (extent_offset = 0;
545 extent_offset < (PAGE_SIZE / crypt_stat->extent_size);
547 rc = crypt_extent(crypt_stat, page, page,
548 extent_offset, DECRYPT);
550 printk(KERN_ERR "%s: Error encrypting extent; "
551 "rc = [%d]\n", __func__, rc);
559 #define ECRYPTFS_MAX_SCATTERLIST_LEN 4
562 * ecryptfs_init_crypt_ctx
563 * @crypt_stat: Uninitialized crypt stats structure
565 * Initialize the crypto context.
567 * TODO: Performance: Keep a cache of initialized cipher contexts;
568 * only init if needed
570 int ecryptfs_init_crypt_ctx(struct ecryptfs_crypt_stat *crypt_stat)
575 ecryptfs_printk(KERN_DEBUG,
576 "Initializing cipher [%s]; strlen = [%d]; "
577 "key_size_bits = [%zd]\n",
578 crypt_stat->cipher, (int)strlen(crypt_stat->cipher),
579 crypt_stat->key_size << 3);
580 mutex_lock(&crypt_stat->cs_tfm_mutex);
581 if (crypt_stat->tfm) {
585 rc = ecryptfs_crypto_api_algify_cipher_name(&full_alg_name,
586 crypt_stat->cipher, "cbc");
589 crypt_stat->tfm = crypto_alloc_skcipher(full_alg_name, 0, 0);
590 if (IS_ERR(crypt_stat->tfm)) {
591 rc = PTR_ERR(crypt_stat->tfm);
592 crypt_stat->tfm = NULL;
593 ecryptfs_printk(KERN_ERR, "cryptfs: init_crypt_ctx(): "
594 "Error initializing cipher [%s]\n",
598 crypto_skcipher_set_flags(crypt_stat->tfm,
599 CRYPTO_TFM_REQ_FORBID_WEAK_KEYS);
602 kfree(full_alg_name);
604 mutex_unlock(&crypt_stat->cs_tfm_mutex);
608 static void set_extent_mask_and_shift(struct ecryptfs_crypt_stat *crypt_stat)
612 crypt_stat->extent_mask = 0xFFFFFFFF;
613 crypt_stat->extent_shift = 0;
614 if (crypt_stat->extent_size == 0)
616 extent_size_tmp = crypt_stat->extent_size;
617 while ((extent_size_tmp & 0x01) == 0) {
618 extent_size_tmp >>= 1;
619 crypt_stat->extent_mask <<= 1;
620 crypt_stat->extent_shift++;
624 void ecryptfs_set_default_sizes(struct ecryptfs_crypt_stat *crypt_stat)
626 /* Default values; may be overwritten as we are parsing the
628 crypt_stat->extent_size = ECRYPTFS_DEFAULT_EXTENT_SIZE;
629 set_extent_mask_and_shift(crypt_stat);
630 crypt_stat->iv_bytes = ECRYPTFS_DEFAULT_IV_BYTES;
631 if (crypt_stat->flags & ECRYPTFS_METADATA_IN_XATTR)
632 crypt_stat->metadata_size = ECRYPTFS_MINIMUM_HEADER_EXTENT_SIZE;
634 if (PAGE_SIZE <= ECRYPTFS_MINIMUM_HEADER_EXTENT_SIZE)
635 crypt_stat->metadata_size =
636 ECRYPTFS_MINIMUM_HEADER_EXTENT_SIZE;
638 crypt_stat->metadata_size = PAGE_SIZE;
643 * ecryptfs_compute_root_iv
646 * On error, sets the root IV to all 0's.
648 int ecryptfs_compute_root_iv(struct ecryptfs_crypt_stat *crypt_stat)
651 char dst[MD5_DIGEST_SIZE];
653 BUG_ON(crypt_stat->iv_bytes > MD5_DIGEST_SIZE);
654 BUG_ON(crypt_stat->iv_bytes <= 0);
655 if (!(crypt_stat->flags & ECRYPTFS_KEY_VALID)) {
657 ecryptfs_printk(KERN_WARNING, "Session key not valid; "
658 "cannot generate root IV\n");
661 rc = ecryptfs_calculate_md5(dst, crypt_stat, crypt_stat->key,
662 crypt_stat->key_size);
664 ecryptfs_printk(KERN_WARNING, "Error attempting to compute "
665 "MD5 while generating root IV\n");
668 memcpy(crypt_stat->root_iv, dst, crypt_stat->iv_bytes);
671 memset(crypt_stat->root_iv, 0, crypt_stat->iv_bytes);
672 crypt_stat->flags |= ECRYPTFS_SECURITY_WARNING;
677 static void ecryptfs_generate_new_key(struct ecryptfs_crypt_stat *crypt_stat)
679 get_random_bytes(crypt_stat->key, crypt_stat->key_size);
680 crypt_stat->flags |= ECRYPTFS_KEY_VALID;
681 ecryptfs_compute_root_iv(crypt_stat);
682 if (unlikely(ecryptfs_verbosity > 0)) {
683 ecryptfs_printk(KERN_DEBUG, "Generated new session key:\n");
684 ecryptfs_dump_hex(crypt_stat->key,
685 crypt_stat->key_size);
690 * ecryptfs_copy_mount_wide_flags_to_inode_flags
691 * @crypt_stat: The inode's cryptographic context
692 * @mount_crypt_stat: The mount point's cryptographic context
694 * This function propagates the mount-wide flags to individual inode
697 static void ecryptfs_copy_mount_wide_flags_to_inode_flags(
698 struct ecryptfs_crypt_stat *crypt_stat,
699 struct ecryptfs_mount_crypt_stat *mount_crypt_stat)
701 if (mount_crypt_stat->flags & ECRYPTFS_XATTR_METADATA_ENABLED)
702 crypt_stat->flags |= ECRYPTFS_METADATA_IN_XATTR;
703 if (mount_crypt_stat->flags & ECRYPTFS_ENCRYPTED_VIEW_ENABLED)
704 crypt_stat->flags |= ECRYPTFS_VIEW_AS_ENCRYPTED;
705 if (mount_crypt_stat->flags & ECRYPTFS_GLOBAL_ENCRYPT_FILENAMES) {
706 crypt_stat->flags |= ECRYPTFS_ENCRYPT_FILENAMES;
707 if (mount_crypt_stat->flags
708 & ECRYPTFS_GLOBAL_ENCFN_USE_MOUNT_FNEK)
709 crypt_stat->flags |= ECRYPTFS_ENCFN_USE_MOUNT_FNEK;
710 else if (mount_crypt_stat->flags
711 & ECRYPTFS_GLOBAL_ENCFN_USE_FEK)
712 crypt_stat->flags |= ECRYPTFS_ENCFN_USE_FEK;
716 static int ecryptfs_copy_mount_wide_sigs_to_inode_sigs(
717 struct ecryptfs_crypt_stat *crypt_stat,
718 struct ecryptfs_mount_crypt_stat *mount_crypt_stat)
720 struct ecryptfs_global_auth_tok *global_auth_tok;
723 mutex_lock(&crypt_stat->keysig_list_mutex);
724 mutex_lock(&mount_crypt_stat->global_auth_tok_list_mutex);
726 list_for_each_entry(global_auth_tok,
727 &mount_crypt_stat->global_auth_tok_list,
728 mount_crypt_stat_list) {
729 if (global_auth_tok->flags & ECRYPTFS_AUTH_TOK_FNEK)
731 rc = ecryptfs_add_keysig(crypt_stat, global_auth_tok->sig);
733 printk(KERN_ERR "Error adding keysig; rc = [%d]\n", rc);
739 mutex_unlock(&mount_crypt_stat->global_auth_tok_list_mutex);
740 mutex_unlock(&crypt_stat->keysig_list_mutex);
745 * ecryptfs_set_default_crypt_stat_vals
746 * @crypt_stat: The inode's cryptographic context
747 * @mount_crypt_stat: The mount point's cryptographic context
749 * Default values in the event that policy does not override them.
751 static void ecryptfs_set_default_crypt_stat_vals(
752 struct ecryptfs_crypt_stat *crypt_stat,
753 struct ecryptfs_mount_crypt_stat *mount_crypt_stat)
755 ecryptfs_copy_mount_wide_flags_to_inode_flags(crypt_stat,
757 ecryptfs_set_default_sizes(crypt_stat);
758 strcpy(crypt_stat->cipher, ECRYPTFS_DEFAULT_CIPHER);
759 crypt_stat->key_size = ECRYPTFS_DEFAULT_KEY_BYTES;
760 crypt_stat->flags &= ~(ECRYPTFS_KEY_VALID);
761 crypt_stat->file_version = ECRYPTFS_FILE_VERSION;
762 crypt_stat->mount_crypt_stat = mount_crypt_stat;
766 * ecryptfs_new_file_context
767 * @ecryptfs_inode: The eCryptfs inode
769 * If the crypto context for the file has not yet been established,
770 * this is where we do that. Establishing a new crypto context
771 * involves the following decisions:
772 * - What cipher to use?
773 * - What set of authentication tokens to use?
774 * Here we just worry about getting enough information into the
775 * authentication tokens so that we know that they are available.
776 * We associate the available authentication tokens with the new file
777 * via the set of signatures in the crypt_stat struct. Later, when
778 * the headers are actually written out, we may again defer to
779 * userspace to perform the encryption of the session key; for the
780 * foreseeable future, this will be the case with public key packets.
782 * Returns zero on success; non-zero otherwise
784 int ecryptfs_new_file_context(struct inode *ecryptfs_inode)
786 struct ecryptfs_crypt_stat *crypt_stat =
787 &ecryptfs_inode_to_private(ecryptfs_inode)->crypt_stat;
788 struct ecryptfs_mount_crypt_stat *mount_crypt_stat =
789 &ecryptfs_superblock_to_private(
790 ecryptfs_inode->i_sb)->mount_crypt_stat;
794 ecryptfs_set_default_crypt_stat_vals(crypt_stat, mount_crypt_stat);
795 crypt_stat->flags |= (ECRYPTFS_ENCRYPTED | ECRYPTFS_KEY_VALID);
796 ecryptfs_copy_mount_wide_flags_to_inode_flags(crypt_stat,
798 rc = ecryptfs_copy_mount_wide_sigs_to_inode_sigs(crypt_stat,
801 printk(KERN_ERR "Error attempting to copy mount-wide key sigs "
802 "to the inode key sigs; rc = [%d]\n", rc);
806 strlen(mount_crypt_stat->global_default_cipher_name);
807 memcpy(crypt_stat->cipher,
808 mount_crypt_stat->global_default_cipher_name,
810 crypt_stat->cipher[cipher_name_len] = '\0';
811 crypt_stat->key_size =
812 mount_crypt_stat->global_default_cipher_key_size;
813 ecryptfs_generate_new_key(crypt_stat);
814 rc = ecryptfs_init_crypt_ctx(crypt_stat);
816 ecryptfs_printk(KERN_ERR, "Error initializing cryptographic "
817 "context for cipher [%s]: rc = [%d]\n",
818 crypt_stat->cipher, rc);
824 * ecryptfs_validate_marker - check for the ecryptfs marker
825 * @data: The data block in which to check
827 * Returns zero if marker found; -EINVAL if not found
829 static int ecryptfs_validate_marker(char *data)
833 m_1 = get_unaligned_be32(data);
834 m_2 = get_unaligned_be32(data + 4);
835 if ((m_1 ^ MAGIC_ECRYPTFS_MARKER) == m_2)
837 ecryptfs_printk(KERN_DEBUG, "m_1 = [0x%.8x]; m_2 = [0x%.8x]; "
838 "MAGIC_ECRYPTFS_MARKER = [0x%.8x]\n", m_1, m_2,
839 MAGIC_ECRYPTFS_MARKER);
840 ecryptfs_printk(KERN_DEBUG, "(m_1 ^ MAGIC_ECRYPTFS_MARKER) = "
841 "[0x%.8x]\n", (m_1 ^ MAGIC_ECRYPTFS_MARKER));
845 struct ecryptfs_flag_map_elem {
850 /* Add support for additional flags by adding elements here. */
851 static struct ecryptfs_flag_map_elem ecryptfs_flag_map[] = {
852 {0x00000001, ECRYPTFS_ENABLE_HMAC},
853 {0x00000002, ECRYPTFS_ENCRYPTED},
854 {0x00000004, ECRYPTFS_METADATA_IN_XATTR},
855 {0x00000008, ECRYPTFS_ENCRYPT_FILENAMES}
859 * ecryptfs_process_flags
860 * @crypt_stat: The cryptographic context
861 * @page_virt: Source data to be parsed
862 * @bytes_read: Updated with the number of bytes read
864 * Returns zero on success; non-zero if the flag set is invalid
866 static int ecryptfs_process_flags(struct ecryptfs_crypt_stat *crypt_stat,
867 char *page_virt, int *bytes_read)
873 flags = get_unaligned_be32(page_virt);
874 for (i = 0; i < ARRAY_SIZE(ecryptfs_flag_map); i++)
875 if (flags & ecryptfs_flag_map[i].file_flag) {
876 crypt_stat->flags |= ecryptfs_flag_map[i].local_flag;
878 crypt_stat->flags &= ~(ecryptfs_flag_map[i].local_flag);
879 /* Version is in top 8 bits of the 32-bit flag vector */
880 crypt_stat->file_version = ((flags >> 24) & 0xFF);
886 * write_ecryptfs_marker
887 * @page_virt: The pointer to in a page to begin writing the marker
888 * @written: Number of bytes written
890 * Marker = 0x3c81b7f5
892 static void write_ecryptfs_marker(char *page_virt, size_t *written)
896 get_random_bytes(&m_1, (MAGIC_ECRYPTFS_MARKER_SIZE_BYTES / 2));
897 m_2 = (m_1 ^ MAGIC_ECRYPTFS_MARKER);
898 put_unaligned_be32(m_1, page_virt);
899 page_virt += (MAGIC_ECRYPTFS_MARKER_SIZE_BYTES / 2);
900 put_unaligned_be32(m_2, page_virt);
901 (*written) = MAGIC_ECRYPTFS_MARKER_SIZE_BYTES;
904 void ecryptfs_write_crypt_stat_flags(char *page_virt,
905 struct ecryptfs_crypt_stat *crypt_stat,
911 for (i = 0; i < ARRAY_SIZE(ecryptfs_flag_map); i++)
912 if (crypt_stat->flags & ecryptfs_flag_map[i].local_flag)
913 flags |= ecryptfs_flag_map[i].file_flag;
914 /* Version is in top 8 bits of the 32-bit flag vector */
915 flags |= ((((u8)crypt_stat->file_version) << 24) & 0xFF000000);
916 put_unaligned_be32(flags, page_virt);
920 struct ecryptfs_cipher_code_str_map_elem {
925 /* Add support for additional ciphers by adding elements here. The
926 * cipher_code is whatever OpenPGP applications use to identify the
927 * ciphers. List in order of probability. */
928 static struct ecryptfs_cipher_code_str_map_elem
929 ecryptfs_cipher_code_str_map[] = {
930 {"aes",RFC2440_CIPHER_AES_128 },
931 {"blowfish", RFC2440_CIPHER_BLOWFISH},
932 {"des3_ede", RFC2440_CIPHER_DES3_EDE},
933 {"cast5", RFC2440_CIPHER_CAST_5},
934 {"twofish", RFC2440_CIPHER_TWOFISH},
935 {"cast6", RFC2440_CIPHER_CAST_6},
936 {"aes", RFC2440_CIPHER_AES_192},
937 {"aes", RFC2440_CIPHER_AES_256}
941 * ecryptfs_code_for_cipher_string
942 * @cipher_name: The string alias for the cipher
943 * @key_bytes: Length of key in bytes; used for AES code selection
945 * Returns zero on no match, or the cipher code on match
947 u8 ecryptfs_code_for_cipher_string(char *cipher_name, size_t key_bytes)
951 struct ecryptfs_cipher_code_str_map_elem *map =
952 ecryptfs_cipher_code_str_map;
954 if (strcmp(cipher_name, "aes") == 0) {
957 code = RFC2440_CIPHER_AES_128;
960 code = RFC2440_CIPHER_AES_192;
963 code = RFC2440_CIPHER_AES_256;
966 for (i = 0; i < ARRAY_SIZE(ecryptfs_cipher_code_str_map); i++)
967 if (strcmp(cipher_name, map[i].cipher_str) == 0) {
968 code = map[i].cipher_code;
976 * ecryptfs_cipher_code_to_string
977 * @str: Destination to write out the cipher name
978 * @cipher_code: The code to convert to cipher name string
980 * Returns zero on success
982 int ecryptfs_cipher_code_to_string(char *str, u8 cipher_code)
988 for (i = 0; i < ARRAY_SIZE(ecryptfs_cipher_code_str_map); i++)
989 if (cipher_code == ecryptfs_cipher_code_str_map[i].cipher_code)
990 strcpy(str, ecryptfs_cipher_code_str_map[i].cipher_str);
991 if (str[0] == '\0') {
992 ecryptfs_printk(KERN_WARNING, "Cipher code not recognized: "
993 "[%d]\n", cipher_code);
999 int ecryptfs_read_and_validate_header_region(struct inode *inode)
1001 u8 file_size[ECRYPTFS_SIZE_AND_MARKER_BYTES];
1002 u8 *marker = file_size + ECRYPTFS_FILE_SIZE_BYTES;
1005 rc = ecryptfs_read_lower(file_size, 0, ECRYPTFS_SIZE_AND_MARKER_BYTES,
1007 if (rc < ECRYPTFS_SIZE_AND_MARKER_BYTES)
1008 return rc >= 0 ? -EINVAL : rc;
1009 rc = ecryptfs_validate_marker(marker);
1011 ecryptfs_i_size_init(file_size, inode);
1016 ecryptfs_write_header_metadata(char *virt,
1017 struct ecryptfs_crypt_stat *crypt_stat,
1020 u32 header_extent_size;
1021 u16 num_header_extents_at_front;
1023 header_extent_size = (u32)crypt_stat->extent_size;
1024 num_header_extents_at_front =
1025 (u16)(crypt_stat->metadata_size / crypt_stat->extent_size);
1026 put_unaligned_be32(header_extent_size, virt);
1028 put_unaligned_be16(num_header_extents_at_front, virt);
1032 struct kmem_cache *ecryptfs_header_cache;
1035 * ecryptfs_write_headers_virt
1036 * @page_virt: The virtual address to write the headers to
1037 * @max: The size of memory allocated at page_virt
1038 * @size: Set to the number of bytes written by this function
1039 * @crypt_stat: The cryptographic context
1040 * @ecryptfs_dentry: The eCryptfs dentry
1045 * Octets 0-7: Unencrypted file size (big-endian)
1046 * Octets 8-15: eCryptfs special marker
1047 * Octets 16-19: Flags
1048 * Octet 16: File format version number (between 0 and 255)
1049 * Octets 17-18: Reserved
1050 * Octet 19: Bit 1 (lsb): Reserved
1052 * Bits 3-8: Reserved
1053 * Octets 20-23: Header extent size (big-endian)
1054 * Octets 24-25: Number of header extents at front of file
1056 * Octet 26: Begin RFC 2440 authentication token packet set
1058 * Lower data (CBC encrypted)
1060 * Lower data (CBC encrypted)
1063 * Returns zero on success
1065 static int ecryptfs_write_headers_virt(char *page_virt, size_t max,
1067 struct ecryptfs_crypt_stat *crypt_stat,
1068 struct dentry *ecryptfs_dentry)
1074 offset = ECRYPTFS_FILE_SIZE_BYTES;
1075 write_ecryptfs_marker((page_virt + offset), &written);
1077 ecryptfs_write_crypt_stat_flags((page_virt + offset), crypt_stat,
1080 ecryptfs_write_header_metadata((page_virt + offset), crypt_stat,
1083 rc = ecryptfs_generate_key_packet_set((page_virt + offset), crypt_stat,
1084 ecryptfs_dentry, &written,
1087 ecryptfs_printk(KERN_WARNING, "Error generating key packet "
1088 "set; rc = [%d]\n", rc);
1097 ecryptfs_write_metadata_to_contents(struct inode *ecryptfs_inode,
1098 char *virt, size_t virt_len)
1102 rc = ecryptfs_write_lower(ecryptfs_inode, virt,
1105 printk(KERN_ERR "%s: Error attempting to write header "
1106 "information to lower file; rc = [%d]\n", __func__, rc);
1113 ecryptfs_write_metadata_to_xattr(struct dentry *ecryptfs_dentry,
1114 struct inode *ecryptfs_inode,
1115 char *page_virt, size_t size)
1119 rc = ecryptfs_setxattr(ecryptfs_dentry, ecryptfs_inode,
1120 ECRYPTFS_XATTR_NAME, page_virt, size, 0);
1124 static unsigned long ecryptfs_get_zeroed_pages(gfp_t gfp_mask,
1129 page = alloc_pages(gfp_mask | __GFP_ZERO, order);
1131 return (unsigned long) page_address(page);
1136 * ecryptfs_write_metadata
1137 * @ecryptfs_dentry: The eCryptfs dentry, which should be negative
1138 * @ecryptfs_inode: The newly created eCryptfs inode
1140 * Write the file headers out. This will likely involve a userspace
1141 * callout, in which the session key is encrypted with one or more
1142 * public keys and/or the passphrase necessary to do the encryption is
1143 * retrieved via a prompt. Exactly what happens at this point should
1144 * be policy-dependent.
1146 * Returns zero on success; non-zero on error
1148 int ecryptfs_write_metadata(struct dentry *ecryptfs_dentry,
1149 struct inode *ecryptfs_inode)
1151 struct ecryptfs_crypt_stat *crypt_stat =
1152 &ecryptfs_inode_to_private(ecryptfs_inode)->crypt_stat;
1159 if (likely(crypt_stat->flags & ECRYPTFS_ENCRYPTED)) {
1160 if (!(crypt_stat->flags & ECRYPTFS_KEY_VALID)) {
1161 printk(KERN_ERR "Key is invalid; bailing out\n");
1166 printk(KERN_WARNING "%s: Encrypted flag not set\n",
1171 virt_len = crypt_stat->metadata_size;
1172 order = get_order(virt_len);
1173 /* Released in this function */
1174 virt = (char *)ecryptfs_get_zeroed_pages(GFP_KERNEL, order);
1176 printk(KERN_ERR "%s: Out of memory\n", __func__);
1180 /* Zeroed page ensures the in-header unencrypted i_size is set to 0 */
1181 rc = ecryptfs_write_headers_virt(virt, virt_len, &size, crypt_stat,
1184 printk(KERN_ERR "%s: Error whilst writing headers; rc = [%d]\n",
1188 if (crypt_stat->flags & ECRYPTFS_METADATA_IN_XATTR)
1189 rc = ecryptfs_write_metadata_to_xattr(ecryptfs_dentry, ecryptfs_inode,
1192 rc = ecryptfs_write_metadata_to_contents(ecryptfs_inode, virt,
1195 printk(KERN_ERR "%s: Error writing metadata out to lower file; "
1196 "rc = [%d]\n", __func__, rc);
1200 free_pages((unsigned long)virt, order);
1205 #define ECRYPTFS_DONT_VALIDATE_HEADER_SIZE 0
1206 #define ECRYPTFS_VALIDATE_HEADER_SIZE 1
1207 static int parse_header_metadata(struct ecryptfs_crypt_stat *crypt_stat,
1208 char *virt, int *bytes_read,
1209 int validate_header_size)
1212 u32 header_extent_size;
1213 u16 num_header_extents_at_front;
1215 header_extent_size = get_unaligned_be32(virt);
1216 virt += sizeof(__be32);
1217 num_header_extents_at_front = get_unaligned_be16(virt);
1218 crypt_stat->metadata_size = (((size_t)num_header_extents_at_front
1219 * (size_t)header_extent_size));
1220 (*bytes_read) = (sizeof(__be32) + sizeof(__be16));
1221 if ((validate_header_size == ECRYPTFS_VALIDATE_HEADER_SIZE)
1222 && (crypt_stat->metadata_size
1223 < ECRYPTFS_MINIMUM_HEADER_EXTENT_SIZE)) {
1225 printk(KERN_WARNING "Invalid header size: [%zd]\n",
1226 crypt_stat->metadata_size);
1232 * set_default_header_data
1233 * @crypt_stat: The cryptographic context
1235 * For version 0 file format; this function is only for backwards
1236 * compatibility for files created with the prior versions of
1239 static void set_default_header_data(struct ecryptfs_crypt_stat *crypt_stat)
1241 crypt_stat->metadata_size = ECRYPTFS_MINIMUM_HEADER_EXTENT_SIZE;
1244 void ecryptfs_i_size_init(const char *page_virt, struct inode *inode)
1246 struct ecryptfs_mount_crypt_stat *mount_crypt_stat;
1247 struct ecryptfs_crypt_stat *crypt_stat;
1250 crypt_stat = &ecryptfs_inode_to_private(inode)->crypt_stat;
1252 &ecryptfs_superblock_to_private(inode->i_sb)->mount_crypt_stat;
1253 if (mount_crypt_stat->flags & ECRYPTFS_ENCRYPTED_VIEW_ENABLED) {
1254 file_size = i_size_read(ecryptfs_inode_to_lower(inode));
1255 if (crypt_stat->flags & ECRYPTFS_METADATA_IN_XATTR)
1256 file_size += crypt_stat->metadata_size;
1258 file_size = get_unaligned_be64(page_virt);
1259 i_size_write(inode, (loff_t)file_size);
1260 crypt_stat->flags |= ECRYPTFS_I_SIZE_INITIALIZED;
1264 * ecryptfs_read_headers_virt
1265 * @page_virt: The virtual address into which to read the headers
1266 * @crypt_stat: The cryptographic context
1267 * @ecryptfs_dentry: The eCryptfs dentry
1268 * @validate_header_size: Whether to validate the header size while reading
1270 * Read/parse the header data. The header format is detailed in the
1271 * comment block for the ecryptfs_write_headers_virt() function.
1273 * Returns zero on success
1275 static int ecryptfs_read_headers_virt(char *page_virt,
1276 struct ecryptfs_crypt_stat *crypt_stat,
1277 struct dentry *ecryptfs_dentry,
1278 int validate_header_size)
1284 ecryptfs_set_default_sizes(crypt_stat);
1285 crypt_stat->mount_crypt_stat = &ecryptfs_superblock_to_private(
1286 ecryptfs_dentry->d_sb)->mount_crypt_stat;
1287 offset = ECRYPTFS_FILE_SIZE_BYTES;
1288 rc = ecryptfs_validate_marker(page_virt + offset);
1291 if (!(crypt_stat->flags & ECRYPTFS_I_SIZE_INITIALIZED))
1292 ecryptfs_i_size_init(page_virt, d_inode(ecryptfs_dentry));
1293 offset += MAGIC_ECRYPTFS_MARKER_SIZE_BYTES;
1294 rc = ecryptfs_process_flags(crypt_stat, (page_virt + offset),
1297 ecryptfs_printk(KERN_WARNING, "Error processing flags\n");
1300 if (crypt_stat->file_version > ECRYPTFS_SUPPORTED_FILE_VERSION) {
1301 ecryptfs_printk(KERN_WARNING, "File version is [%d]; only "
1302 "file version [%d] is supported by this "
1303 "version of eCryptfs\n",
1304 crypt_stat->file_version,
1305 ECRYPTFS_SUPPORTED_FILE_VERSION);
1309 offset += bytes_read;
1310 if (crypt_stat->file_version >= 1) {
1311 rc = parse_header_metadata(crypt_stat, (page_virt + offset),
1312 &bytes_read, validate_header_size);
1314 ecryptfs_printk(KERN_WARNING, "Error reading header "
1315 "metadata; rc = [%d]\n", rc);
1317 offset += bytes_read;
1319 set_default_header_data(crypt_stat);
1320 rc = ecryptfs_parse_packet_set(crypt_stat, (page_virt + offset),
1327 * ecryptfs_read_xattr_region
1328 * @page_virt: The vitual address into which to read the xattr data
1329 * @ecryptfs_inode: The eCryptfs inode
1331 * Attempts to read the crypto metadata from the extended attribute
1332 * region of the lower file.
1334 * Returns zero on success; non-zero on error
1336 int ecryptfs_read_xattr_region(char *page_virt, struct inode *ecryptfs_inode)
1338 struct dentry *lower_dentry =
1339 ecryptfs_inode_to_private(ecryptfs_inode)->lower_file->f_path.dentry;
1343 size = ecryptfs_getxattr_lower(lower_dentry,
1344 ecryptfs_inode_to_lower(ecryptfs_inode),
1345 ECRYPTFS_XATTR_NAME,
1346 page_virt, ECRYPTFS_DEFAULT_EXTENT_SIZE);
1348 if (unlikely(ecryptfs_verbosity > 0))
1349 printk(KERN_INFO "Error attempting to read the [%s] "
1350 "xattr from the lower file; return value = "
1351 "[%zd]\n", ECRYPTFS_XATTR_NAME, size);
1359 int ecryptfs_read_and_validate_xattr_region(struct dentry *dentry,
1360 struct inode *inode)
1362 u8 file_size[ECRYPTFS_SIZE_AND_MARKER_BYTES];
1363 u8 *marker = file_size + ECRYPTFS_FILE_SIZE_BYTES;
1366 rc = ecryptfs_getxattr_lower(ecryptfs_dentry_to_lower(dentry),
1367 ecryptfs_inode_to_lower(inode),
1368 ECRYPTFS_XATTR_NAME, file_size,
1369 ECRYPTFS_SIZE_AND_MARKER_BYTES);
1370 if (rc < ECRYPTFS_SIZE_AND_MARKER_BYTES)
1371 return rc >= 0 ? -EINVAL : rc;
1372 rc = ecryptfs_validate_marker(marker);
1374 ecryptfs_i_size_init(file_size, inode);
1379 * ecryptfs_read_metadata
1381 * Common entry point for reading file metadata. From here, we could
1382 * retrieve the header information from the header region of the file,
1383 * the xattr region of the file, or some other repository that is
1384 * stored separately from the file itself. The current implementation
1385 * supports retrieving the metadata information from the file contents
1386 * and from the xattr region.
1388 * Returns zero if valid headers found and parsed; non-zero otherwise
1390 int ecryptfs_read_metadata(struct dentry *ecryptfs_dentry)
1394 struct inode *ecryptfs_inode = d_inode(ecryptfs_dentry);
1395 struct ecryptfs_crypt_stat *crypt_stat =
1396 &ecryptfs_inode_to_private(ecryptfs_inode)->crypt_stat;
1397 struct ecryptfs_mount_crypt_stat *mount_crypt_stat =
1398 &ecryptfs_superblock_to_private(
1399 ecryptfs_dentry->d_sb)->mount_crypt_stat;
1401 ecryptfs_copy_mount_wide_flags_to_inode_flags(crypt_stat,
1403 /* Read the first page from the underlying file */
1404 page_virt = kmem_cache_alloc(ecryptfs_header_cache, GFP_USER);
1409 rc = ecryptfs_read_lower(page_virt, 0, crypt_stat->extent_size,
1412 rc = ecryptfs_read_headers_virt(page_virt, crypt_stat,
1414 ECRYPTFS_VALIDATE_HEADER_SIZE);
1416 /* metadata is not in the file header, so try xattrs */
1417 memset(page_virt, 0, PAGE_SIZE);
1418 rc = ecryptfs_read_xattr_region(page_virt, ecryptfs_inode);
1420 printk(KERN_DEBUG "Valid eCryptfs headers not found in "
1421 "file header region or xattr region, inode %lu\n",
1422 ecryptfs_inode->i_ino);
1426 rc = ecryptfs_read_headers_virt(page_virt, crypt_stat,
1428 ECRYPTFS_DONT_VALIDATE_HEADER_SIZE);
1430 printk(KERN_DEBUG "Valid eCryptfs headers not found in "
1431 "file xattr region either, inode %lu\n",
1432 ecryptfs_inode->i_ino);
1435 if (crypt_stat->mount_crypt_stat->flags
1436 & ECRYPTFS_XATTR_METADATA_ENABLED) {
1437 crypt_stat->flags |= ECRYPTFS_METADATA_IN_XATTR;
1439 printk(KERN_WARNING "Attempt to access file with "
1440 "crypto metadata only in the extended attribute "
1441 "region, but eCryptfs was mounted without "
1442 "xattr support enabled. eCryptfs will not treat "
1443 "this like an encrypted file, inode %lu\n",
1444 ecryptfs_inode->i_ino);
1450 memset(page_virt, 0, PAGE_SIZE);
1451 kmem_cache_free(ecryptfs_header_cache, page_virt);
1457 * ecryptfs_encrypt_filename - encrypt filename
1459 * CBC-encrypts the filename. We do not want to encrypt the same
1460 * filename with the same key and IV, which may happen with hard
1461 * links, so we prepend random bits to each filename.
1463 * Returns zero on success; non-zero otherwise
1466 ecryptfs_encrypt_filename(struct ecryptfs_filename *filename,
1467 struct ecryptfs_mount_crypt_stat *mount_crypt_stat)
1471 filename->encrypted_filename = NULL;
1472 filename->encrypted_filename_size = 0;
1473 if (mount_crypt_stat && (mount_crypt_stat->flags
1474 & ECRYPTFS_GLOBAL_ENCFN_USE_MOUNT_FNEK)) {
1476 size_t remaining_bytes;
1478 rc = ecryptfs_write_tag_70_packet(
1480 &filename->encrypted_filename_size,
1481 mount_crypt_stat, NULL,
1482 filename->filename_size);
1484 printk(KERN_ERR "%s: Error attempting to get packet "
1485 "size for tag 72; rc = [%d]\n", __func__,
1487 filename->encrypted_filename_size = 0;
1490 filename->encrypted_filename =
1491 kmalloc(filename->encrypted_filename_size, GFP_KERNEL);
1492 if (!filename->encrypted_filename) {
1496 remaining_bytes = filename->encrypted_filename_size;
1497 rc = ecryptfs_write_tag_70_packet(filename->encrypted_filename,
1502 filename->filename_size);
1504 printk(KERN_ERR "%s: Error attempting to generate "
1505 "tag 70 packet; rc = [%d]\n", __func__,
1507 kfree(filename->encrypted_filename);
1508 filename->encrypted_filename = NULL;
1509 filename->encrypted_filename_size = 0;
1512 filename->encrypted_filename_size = packet_size;
1514 printk(KERN_ERR "%s: No support for requested filename "
1515 "encryption method in this release\n", __func__);
1523 static int ecryptfs_copy_filename(char **copied_name, size_t *copied_name_size,
1524 const char *name, size_t name_size)
1528 (*copied_name) = kmalloc((name_size + 1), GFP_KERNEL);
1529 if (!(*copied_name)) {
1533 memcpy((void *)(*copied_name), (void *)name, name_size);
1534 (*copied_name)[(name_size)] = '\0'; /* Only for convenience
1535 * in printing out the
1538 (*copied_name_size) = name_size;
1544 * ecryptfs_process_key_cipher - Perform key cipher initialization.
1545 * @key_tfm: Crypto context for key material, set by this function
1546 * @cipher_name: Name of the cipher
1547 * @key_size: Size of the key in bytes
1549 * Returns zero on success. Any crypto_tfm structs allocated here
1550 * should be released by other functions, such as on a superblock put
1551 * event, regardless of whether this function succeeds for fails.
1554 ecryptfs_process_key_cipher(struct crypto_skcipher **key_tfm,
1555 char *cipher_name, size_t *key_size)
1557 char dummy_key[ECRYPTFS_MAX_KEY_BYTES];
1558 char *full_alg_name = NULL;
1562 if (*key_size > ECRYPTFS_MAX_KEY_BYTES) {
1564 printk(KERN_ERR "Requested key size is [%zd] bytes; maximum "
1565 "allowable is [%d]\n", *key_size, ECRYPTFS_MAX_KEY_BYTES);
1568 rc = ecryptfs_crypto_api_algify_cipher_name(&full_alg_name, cipher_name,
1572 *key_tfm = crypto_alloc_skcipher(full_alg_name, 0, CRYPTO_ALG_ASYNC);
1573 if (IS_ERR(*key_tfm)) {
1574 rc = PTR_ERR(*key_tfm);
1575 printk(KERN_ERR "Unable to allocate crypto cipher with name "
1576 "[%s]; rc = [%d]\n", full_alg_name, rc);
1579 crypto_skcipher_set_flags(*key_tfm, CRYPTO_TFM_REQ_FORBID_WEAK_KEYS);
1581 *key_size = crypto_skcipher_default_keysize(*key_tfm);
1582 get_random_bytes(dummy_key, *key_size);
1583 rc = crypto_skcipher_setkey(*key_tfm, dummy_key, *key_size);
1585 printk(KERN_ERR "Error attempting to set key of size [%zd] for "
1586 "cipher [%s]; rc = [%d]\n", *key_size, full_alg_name,
1592 kfree(full_alg_name);
1596 struct kmem_cache *ecryptfs_key_tfm_cache;
1597 static struct list_head key_tfm_list;
1598 struct mutex key_tfm_list_mutex;
1600 int __init ecryptfs_init_crypto(void)
1602 mutex_init(&key_tfm_list_mutex);
1603 INIT_LIST_HEAD(&key_tfm_list);
1608 * ecryptfs_destroy_crypto - free all cached key_tfms on key_tfm_list
1610 * Called only at module unload time
1612 int ecryptfs_destroy_crypto(void)
1614 struct ecryptfs_key_tfm *key_tfm, *key_tfm_tmp;
1616 mutex_lock(&key_tfm_list_mutex);
1617 list_for_each_entry_safe(key_tfm, key_tfm_tmp, &key_tfm_list,
1619 list_del(&key_tfm->key_tfm_list);
1620 crypto_free_skcipher(key_tfm->key_tfm);
1621 kmem_cache_free(ecryptfs_key_tfm_cache, key_tfm);
1623 mutex_unlock(&key_tfm_list_mutex);
1628 ecryptfs_add_new_key_tfm(struct ecryptfs_key_tfm **key_tfm, char *cipher_name,
1631 struct ecryptfs_key_tfm *tmp_tfm;
1634 BUG_ON(!mutex_is_locked(&key_tfm_list_mutex));
1636 tmp_tfm = kmem_cache_alloc(ecryptfs_key_tfm_cache, GFP_KERNEL);
1638 (*key_tfm) = tmp_tfm;
1643 mutex_init(&tmp_tfm->key_tfm_mutex);
1644 strncpy(tmp_tfm->cipher_name, cipher_name,
1645 ECRYPTFS_MAX_CIPHER_NAME_SIZE);
1646 tmp_tfm->cipher_name[ECRYPTFS_MAX_CIPHER_NAME_SIZE] = '\0';
1647 tmp_tfm->key_size = key_size;
1648 rc = ecryptfs_process_key_cipher(&tmp_tfm->key_tfm,
1649 tmp_tfm->cipher_name,
1650 &tmp_tfm->key_size);
1652 printk(KERN_ERR "Error attempting to initialize key TFM "
1653 "cipher with name = [%s]; rc = [%d]\n",
1654 tmp_tfm->cipher_name, rc);
1655 kmem_cache_free(ecryptfs_key_tfm_cache, tmp_tfm);
1660 list_add(&tmp_tfm->key_tfm_list, &key_tfm_list);
1666 * ecryptfs_tfm_exists - Search for existing tfm for cipher_name.
1667 * @cipher_name: the name of the cipher to search for
1668 * @key_tfm: set to corresponding tfm if found
1670 * Searches for cached key_tfm matching @cipher_name
1671 * Must be called with &key_tfm_list_mutex held
1672 * Returns 1 if found, with @key_tfm set
1673 * Returns 0 if not found, with @key_tfm set to NULL
1675 int ecryptfs_tfm_exists(char *cipher_name, struct ecryptfs_key_tfm **key_tfm)
1677 struct ecryptfs_key_tfm *tmp_key_tfm;
1679 BUG_ON(!mutex_is_locked(&key_tfm_list_mutex));
1681 list_for_each_entry(tmp_key_tfm, &key_tfm_list, key_tfm_list) {
1682 if (strcmp(tmp_key_tfm->cipher_name, cipher_name) == 0) {
1684 (*key_tfm) = tmp_key_tfm;
1694 * ecryptfs_get_tfm_and_mutex_for_cipher_name
1696 * @tfm: set to cached tfm found, or new tfm created
1697 * @tfm_mutex: set to mutex for cached tfm found, or new tfm created
1698 * @cipher_name: the name of the cipher to search for and/or add
1700 * Sets pointers to @tfm & @tfm_mutex matching @cipher_name.
1701 * Searches for cached item first, and creates new if not found.
1702 * Returns 0 on success, non-zero if adding new cipher failed
1704 int ecryptfs_get_tfm_and_mutex_for_cipher_name(struct crypto_skcipher **tfm,
1705 struct mutex **tfm_mutex,
1708 struct ecryptfs_key_tfm *key_tfm;
1712 (*tfm_mutex) = NULL;
1714 mutex_lock(&key_tfm_list_mutex);
1715 if (!ecryptfs_tfm_exists(cipher_name, &key_tfm)) {
1716 rc = ecryptfs_add_new_key_tfm(&key_tfm, cipher_name, 0);
1718 printk(KERN_ERR "Error adding new key_tfm to list; "
1723 (*tfm) = key_tfm->key_tfm;
1724 (*tfm_mutex) = &key_tfm->key_tfm_mutex;
1726 mutex_unlock(&key_tfm_list_mutex);
1730 /* 64 characters forming a 6-bit target field */
1731 static unsigned char *portable_filename_chars = ("-.0123456789ABCD"
1734 "klmnopqrstuvwxyz");
1736 /* We could either offset on every reverse map or just pad some 0x00's
1737 * at the front here */
1738 static const unsigned char filename_rev_map[256] = {
1739 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, /* 7 */
1740 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, /* 15 */
1741 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, /* 23 */
1742 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, /* 31 */
1743 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, /* 39 */
1744 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x01, 0x00, /* 47 */
1745 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, /* 55 */
1746 0x0A, 0x0B, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, /* 63 */
1747 0x00, 0x0C, 0x0D, 0x0E, 0x0F, 0x10, 0x11, 0x12, /* 71 */
1748 0x13, 0x14, 0x15, 0x16, 0x17, 0x18, 0x19, 0x1A, /* 79 */
1749 0x1B, 0x1C, 0x1D, 0x1E, 0x1F, 0x20, 0x21, 0x22, /* 87 */
1750 0x23, 0x24, 0x25, 0x00, 0x00, 0x00, 0x00, 0x00, /* 95 */
1751 0x00, 0x26, 0x27, 0x28, 0x29, 0x2A, 0x2B, 0x2C, /* 103 */
1752 0x2D, 0x2E, 0x2F, 0x30, 0x31, 0x32, 0x33, 0x34, /* 111 */
1753 0x35, 0x36, 0x37, 0x38, 0x39, 0x3A, 0x3B, 0x3C, /* 119 */
1754 0x3D, 0x3E, 0x3F /* 123 - 255 initialized to 0x00 */
1758 * ecryptfs_encode_for_filename
1759 * @dst: Destination location for encoded filename
1760 * @dst_size: Size of the encoded filename in bytes
1761 * @src: Source location for the filename to encode
1762 * @src_size: Size of the source in bytes
1764 static void ecryptfs_encode_for_filename(unsigned char *dst, size_t *dst_size,
1765 unsigned char *src, size_t src_size)
1768 size_t block_num = 0;
1769 size_t dst_offset = 0;
1770 unsigned char last_block[3];
1772 if (src_size == 0) {
1776 num_blocks = (src_size / 3);
1777 if ((src_size % 3) == 0) {
1778 memcpy(last_block, (&src[src_size - 3]), 3);
1781 last_block[2] = 0x00;
1782 switch (src_size % 3) {
1784 last_block[0] = src[src_size - 1];
1785 last_block[1] = 0x00;
1788 last_block[0] = src[src_size - 2];
1789 last_block[1] = src[src_size - 1];
1792 (*dst_size) = (num_blocks * 4);
1795 while (block_num < num_blocks) {
1796 unsigned char *src_block;
1797 unsigned char dst_block[4];
1799 if (block_num == (num_blocks - 1))
1800 src_block = last_block;
1802 src_block = &src[block_num * 3];
1803 dst_block[0] = ((src_block[0] >> 2) & 0x3F);
1804 dst_block[1] = (((src_block[0] << 4) & 0x30)
1805 | ((src_block[1] >> 4) & 0x0F));
1806 dst_block[2] = (((src_block[1] << 2) & 0x3C)
1807 | ((src_block[2] >> 6) & 0x03));
1808 dst_block[3] = (src_block[2] & 0x3F);
1809 dst[dst_offset++] = portable_filename_chars[dst_block[0]];
1810 dst[dst_offset++] = portable_filename_chars[dst_block[1]];
1811 dst[dst_offset++] = portable_filename_chars[dst_block[2]];
1812 dst[dst_offset++] = portable_filename_chars[dst_block[3]];
1819 static size_t ecryptfs_max_decoded_size(size_t encoded_size)
1821 /* Not exact; conservatively long. Every block of 4
1822 * encoded characters decodes into a block of 3
1823 * decoded characters. This segment of code provides
1824 * the caller with the maximum amount of allocated
1825 * space that @dst will need to point to in a
1826 * subsequent call. */
1827 return ((encoded_size + 1) * 3) / 4;
1831 * ecryptfs_decode_from_filename
1832 * @dst: If NULL, this function only sets @dst_size and returns. If
1833 * non-NULL, this function decodes the encoded octets in @src
1834 * into the memory that @dst points to.
1835 * @dst_size: Set to the size of the decoded string.
1836 * @src: The encoded set of octets to decode.
1837 * @src_size: The size of the encoded set of octets to decode.
1840 ecryptfs_decode_from_filename(unsigned char *dst, size_t *dst_size,
1841 const unsigned char *src, size_t src_size)
1843 u8 current_bit_offset = 0;
1844 size_t src_byte_offset = 0;
1845 size_t dst_byte_offset = 0;
1848 (*dst_size) = ecryptfs_max_decoded_size(src_size);
1851 while (src_byte_offset < src_size) {
1852 unsigned char src_byte =
1853 filename_rev_map[(int)src[src_byte_offset]];
1855 switch (current_bit_offset) {
1857 dst[dst_byte_offset] = (src_byte << 2);
1858 current_bit_offset = 6;
1861 dst[dst_byte_offset++] |= (src_byte >> 4);
1862 dst[dst_byte_offset] = ((src_byte & 0xF)
1864 current_bit_offset = 4;
1867 dst[dst_byte_offset++] |= (src_byte >> 2);
1868 dst[dst_byte_offset] = (src_byte << 6);
1869 current_bit_offset = 2;
1872 dst[dst_byte_offset++] |= (src_byte);
1873 current_bit_offset = 0;
1878 (*dst_size) = dst_byte_offset;
1884 * ecryptfs_encrypt_and_encode_filename - converts a plaintext file name to cipher text
1885 * @crypt_stat: The crypt_stat struct associated with the file anem to encode
1886 * @name: The plaintext name
1887 * @length: The length of the plaintext
1888 * @encoded_name: The encypted name
1890 * Encrypts and encodes a filename into something that constitutes a
1891 * valid filename for a filesystem, with printable characters.
1893 * We assume that we have a properly initialized crypto context,
1894 * pointed to by crypt_stat->tfm.
1896 * Returns zero on success; non-zero on otherwise
1898 int ecryptfs_encrypt_and_encode_filename(
1899 char **encoded_name,
1900 size_t *encoded_name_size,
1901 struct ecryptfs_mount_crypt_stat *mount_crypt_stat,
1902 const char *name, size_t name_size)
1904 size_t encoded_name_no_prefix_size;
1907 (*encoded_name) = NULL;
1908 (*encoded_name_size) = 0;
1909 if (mount_crypt_stat && (mount_crypt_stat->flags
1910 & ECRYPTFS_GLOBAL_ENCRYPT_FILENAMES)) {
1911 struct ecryptfs_filename *filename;
1913 filename = kzalloc(sizeof(*filename), GFP_KERNEL);
1918 filename->filename = (char *)name;
1919 filename->filename_size = name_size;
1920 rc = ecryptfs_encrypt_filename(filename, mount_crypt_stat);
1922 printk(KERN_ERR "%s: Error attempting to encrypt "
1923 "filename; rc = [%d]\n", __func__, rc);
1927 ecryptfs_encode_for_filename(
1928 NULL, &encoded_name_no_prefix_size,
1929 filename->encrypted_filename,
1930 filename->encrypted_filename_size);
1931 if (mount_crypt_stat
1932 && (mount_crypt_stat->flags
1933 & ECRYPTFS_GLOBAL_ENCFN_USE_MOUNT_FNEK))
1934 (*encoded_name_size) =
1935 (ECRYPTFS_FNEK_ENCRYPTED_FILENAME_PREFIX_SIZE
1936 + encoded_name_no_prefix_size);
1938 (*encoded_name_size) =
1939 (ECRYPTFS_FEK_ENCRYPTED_FILENAME_PREFIX_SIZE
1940 + encoded_name_no_prefix_size);
1941 (*encoded_name) = kmalloc((*encoded_name_size) + 1, GFP_KERNEL);
1942 if (!(*encoded_name)) {
1944 kfree(filename->encrypted_filename);
1948 if (mount_crypt_stat
1949 && (mount_crypt_stat->flags
1950 & ECRYPTFS_GLOBAL_ENCFN_USE_MOUNT_FNEK)) {
1951 memcpy((*encoded_name),
1952 ECRYPTFS_FNEK_ENCRYPTED_FILENAME_PREFIX,
1953 ECRYPTFS_FNEK_ENCRYPTED_FILENAME_PREFIX_SIZE);
1954 ecryptfs_encode_for_filename(
1956 + ECRYPTFS_FNEK_ENCRYPTED_FILENAME_PREFIX_SIZE),
1957 &encoded_name_no_prefix_size,
1958 filename->encrypted_filename,
1959 filename->encrypted_filename_size);
1960 (*encoded_name_size) =
1961 (ECRYPTFS_FNEK_ENCRYPTED_FILENAME_PREFIX_SIZE
1962 + encoded_name_no_prefix_size);
1963 (*encoded_name)[(*encoded_name_size)] = '\0';
1968 printk(KERN_ERR "%s: Error attempting to encode "
1969 "encrypted filename; rc = [%d]\n", __func__,
1971 kfree((*encoded_name));
1972 (*encoded_name) = NULL;
1973 (*encoded_name_size) = 0;
1975 kfree(filename->encrypted_filename);
1978 rc = ecryptfs_copy_filename(encoded_name,
1986 static bool is_dot_dotdot(const char *name, size_t name_size)
1988 if (name_size == 1 && name[0] == '.')
1990 else if (name_size == 2 && name[0] == '.' && name[1] == '.')
1997 * ecryptfs_decode_and_decrypt_filename - converts the encoded cipher text name to decoded plaintext
1998 * @plaintext_name: The plaintext name
1999 * @plaintext_name_size: The plaintext name size
2000 * @ecryptfs_dir_dentry: eCryptfs directory dentry
2001 * @name: The filename in cipher text
2002 * @name_size: The cipher text name size
2004 * Decrypts and decodes the filename.
2006 * Returns zero on error; non-zero otherwise
2008 int ecryptfs_decode_and_decrypt_filename(char **plaintext_name,
2009 size_t *plaintext_name_size,
2010 struct super_block *sb,
2011 const char *name, size_t name_size)
2013 struct ecryptfs_mount_crypt_stat *mount_crypt_stat =
2014 &ecryptfs_superblock_to_private(sb)->mount_crypt_stat;
2016 size_t decoded_name_size;
2020 if ((mount_crypt_stat->flags & ECRYPTFS_GLOBAL_ENCRYPT_FILENAMES) &&
2021 !(mount_crypt_stat->flags & ECRYPTFS_ENCRYPTED_VIEW_ENABLED)) {
2022 if (is_dot_dotdot(name, name_size)) {
2023 rc = ecryptfs_copy_filename(plaintext_name,
2024 plaintext_name_size,
2029 if (name_size <= ECRYPTFS_FNEK_ENCRYPTED_FILENAME_PREFIX_SIZE ||
2030 strncmp(name, ECRYPTFS_FNEK_ENCRYPTED_FILENAME_PREFIX,
2031 ECRYPTFS_FNEK_ENCRYPTED_FILENAME_PREFIX_SIZE)) {
2036 name += ECRYPTFS_FNEK_ENCRYPTED_FILENAME_PREFIX_SIZE;
2037 name_size -= ECRYPTFS_FNEK_ENCRYPTED_FILENAME_PREFIX_SIZE;
2038 ecryptfs_decode_from_filename(NULL, &decoded_name_size,
2040 decoded_name = kmalloc(decoded_name_size, GFP_KERNEL);
2041 if (!decoded_name) {
2045 ecryptfs_decode_from_filename(decoded_name, &decoded_name_size,
2047 rc = ecryptfs_parse_tag_70_packet(plaintext_name,
2048 plaintext_name_size,
2054 ecryptfs_printk(KERN_DEBUG,
2055 "%s: Could not parse tag 70 packet from filename\n",
2060 rc = ecryptfs_copy_filename(plaintext_name,
2061 plaintext_name_size,
2066 kfree(decoded_name);
2071 #define ENC_NAME_MAX_BLOCKLEN_8_OR_16 143
2073 int ecryptfs_set_f_namelen(long *namelen, long lower_namelen,
2074 struct ecryptfs_mount_crypt_stat *mount_crypt_stat)
2076 struct crypto_skcipher *tfm;
2077 struct mutex *tfm_mutex;
2078 size_t cipher_blocksize;
2081 if (!(mount_crypt_stat->flags & ECRYPTFS_GLOBAL_ENCRYPT_FILENAMES)) {
2082 (*namelen) = lower_namelen;
2086 rc = ecryptfs_get_tfm_and_mutex_for_cipher_name(&tfm, &tfm_mutex,
2087 mount_crypt_stat->global_default_fn_cipher_name);
2093 mutex_lock(tfm_mutex);
2094 cipher_blocksize = crypto_skcipher_blocksize(tfm);
2095 mutex_unlock(tfm_mutex);
2097 /* Return an exact amount for the common cases */
2098 if (lower_namelen == NAME_MAX
2099 && (cipher_blocksize == 8 || cipher_blocksize == 16)) {
2100 (*namelen) = ENC_NAME_MAX_BLOCKLEN_8_OR_16;
2104 /* Return a safe estimate for the uncommon cases */
2105 (*namelen) = lower_namelen;
2106 (*namelen) -= ECRYPTFS_FNEK_ENCRYPTED_FILENAME_PREFIX_SIZE;
2107 /* Since this is the max decoded size, subtract 1 "decoded block" len */
2108 (*namelen) = ecryptfs_max_decoded_size(*namelen) - 3;
2109 (*namelen) -= ECRYPTFS_TAG_70_MAX_METADATA_SIZE;
2110 (*namelen) -= ECRYPTFS_FILENAME_MIN_RANDOM_PREPEND_BYTES;
2111 /* Worst case is that the filename is padded nearly a full block size */
2112 (*namelen) -= cipher_blocksize - 1;