xref: /openbmc/linux/fs/ecryptfs/crypto.c (revision 8dde5715)
1 // SPDX-License-Identifier: GPL-2.0-or-later
2 /**
3  * eCryptfs: Linux filesystem encryption layer
4  *
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>
10  */
11 
12 #include <crypto/hash.h>
13 #include <crypto/skcipher.h>
14 #include <linux/fs.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"
27 
28 #define DECRYPT		0
29 #define ENCRYPT		1
30 
31 /**
32  * ecryptfs_from_hex
33  * @dst: Buffer to take the bytes from src hex; must be at least of
34  *       size (src_size / 2)
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
37  */
38 void ecryptfs_from_hex(char *dst, char *src, int dst_size)
39 {
40 	int x;
41 	char tmp[3] = { 0, };
42 
43 	for (x = 0; x < dst_size; x++) {
44 		tmp[0] = src[x * 2];
45 		tmp[1] = src[x * 2 + 1];
46 		dst[x] = (unsigned char)simple_strtol(tmp, NULL, 16);
47 	}
48 }
49 
50 static int ecryptfs_hash_digest(struct crypto_shash *tfm,
51 				char *src, int len, char *dst)
52 {
53 	SHASH_DESC_ON_STACK(desc, tfm);
54 	int err;
55 
56 	desc->tfm = tfm;
57 	err = crypto_shash_digest(desc, src, len, dst);
58 	shash_desc_zero(desc);
59 	return err;
60 }
61 
62 /**
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
68  *
69  * Uses the allocated crypto context that crypt_stat references to
70  * generate the MD5 sum of the contents of src.
71  */
72 static int ecryptfs_calculate_md5(char *dst,
73 				  struct ecryptfs_crypt_stat *crypt_stat,
74 				  char *src, int len)
75 {
76 	struct crypto_shash *tfm;
77 	int rc = 0;
78 
79 	tfm = crypt_stat->hash_tfm;
80 	rc = ecryptfs_hash_digest(tfm, src, len, dst);
81 	if (rc) {
82 		printk(KERN_ERR
83 		       "%s: Error computing crypto hash; rc = [%d]\n",
84 		       __func__, rc);
85 		goto out;
86 	}
87 out:
88 	return rc;
89 }
90 
91 static int ecryptfs_crypto_api_algify_cipher_name(char **algified_name,
92 						  char *cipher_name,
93 						  char *chaining_modifier)
94 {
95 	int cipher_name_len = strlen(cipher_name);
96 	int chaining_modifier_len = strlen(chaining_modifier);
97 	int algified_name_len;
98 	int rc;
99 
100 	algified_name_len = (chaining_modifier_len + cipher_name_len + 3);
101 	(*algified_name) = kmalloc(algified_name_len, GFP_KERNEL);
102 	if (!(*algified_name)) {
103 		rc = -ENOMEM;
104 		goto out;
105 	}
106 	snprintf((*algified_name), algified_name_len, "%s(%s)",
107 		 chaining_modifier, cipher_name);
108 	rc = 0;
109 out:
110 	return rc;
111 }
112 
113 /**
114  * ecryptfs_derive_iv
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
118  *
119  * Generate the initialization vector from the given root IV and page
120  * offset.
121  *
122  * Returns zero on success; non-zero on error.
123  */
124 int ecryptfs_derive_iv(char *iv, struct ecryptfs_crypt_stat *crypt_stat,
125 		       loff_t offset)
126 {
127 	int rc = 0;
128 	char dst[MD5_DIGEST_SIZE];
129 	char src[ECRYPTFS_MAX_IV_BYTES + 16];
130 
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);
134 	}
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));
145 	}
146 	rc = ecryptfs_calculate_md5(dst, crypt_stat, src,
147 				    (crypt_stat->iv_bytes + 16));
148 	if (rc) {
149 		ecryptfs_printk(KERN_WARNING, "Error attempting to compute "
150 				"MD5 while generating IV for a page\n");
151 		goto out;
152 	}
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);
157 	}
158 out:
159 	return rc;
160 }
161 
162 /**
163  * ecryptfs_init_crypt_stat
164  * @crypt_stat: Pointer to the crypt_stat struct to initialize.
165  *
166  * Initialize the crypt_stat structure.
167  */
168 int ecryptfs_init_crypt_stat(struct ecryptfs_crypt_stat *crypt_stat)
169 {
170 	struct crypto_shash *tfm;
171 	int rc;
172 
173 	tfm = crypto_alloc_shash(ECRYPTFS_DEFAULT_HASH, 0, 0);
174 	if (IS_ERR(tfm)) {
175 		rc = PTR_ERR(tfm);
176 		ecryptfs_printk(KERN_ERR, "Error attempting to "
177 				"allocate crypto context; rc = [%d]\n",
178 				rc);
179 		return rc;
180 	}
181 
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;
189 
190 	return 0;
191 }
192 
193 /**
194  * ecryptfs_destroy_crypt_stat
195  * @crypt_stat: Pointer to the crypt_stat struct to initialize.
196  *
197  * Releases all memory associated with a crypt_stat struct.
198  */
199 void ecryptfs_destroy_crypt_stat(struct ecryptfs_crypt_stat *crypt_stat)
200 {
201 	struct ecryptfs_key_sig *key_sig, *key_sig_tmp;
202 
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);
209 	}
210 	memset(crypt_stat, 0, sizeof(struct ecryptfs_crypt_stat));
211 }
212 
213 void ecryptfs_destroy_mount_crypt_stat(
214 	struct ecryptfs_mount_crypt_stat *mount_crypt_stat)
215 {
216 	struct ecryptfs_global_auth_tok *auth_tok, *auth_tok_tmp;
217 
218 	if (!(mount_crypt_stat->flags & ECRYPTFS_MOUNT_CRYPT_STAT_INITIALIZED))
219 		return;
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);
228 	}
229 	mutex_unlock(&mount_crypt_stat->global_auth_tok_list_mutex);
230 	memset(mount_crypt_stat, 0, sizeof(struct ecryptfs_mount_crypt_stat));
231 }
232 
233 /**
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
240  *
241  * Fills in a scatterlist array with page references for a passed
242  * virtual address.
243  *
244  * Returns the number of scatterlist structs in array used
245  */
246 int virt_to_scatterlist(const void *addr, int size, struct scatterlist *sg,
247 			int sg_size)
248 {
249 	int i = 0;
250 	struct page *pg;
251 	int offset;
252 	int remainder_of_page;
253 
254 	sg_init_table(sg, sg_size);
255 
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;
265 		} else {
266 			sg[i].length = size;
267 			addr += size;
268 			size = 0;
269 		}
270 		i++;
271 	}
272 	if (size > 0)
273 		return -ENOMEM;
274 	return i;
275 }
276 
277 struct extent_crypt_result {
278 	struct completion completion;
279 	int rc;
280 };
281 
282 static void extent_crypt_complete(struct crypto_async_request *req, int rc)
283 {
284 	struct extent_crypt_result *ecr = req->data;
285 
286 	if (rc == -EINPROGRESS)
287 		return;
288 
289 	ecr->rc = rc;
290 	complete(&ecr->completion);
291 }
292 
293 /**
294  * crypt_scatterlist
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
299  * @iv: IV to use
300  * @op: ENCRYPT or DECRYPT to indicate the desired operation
301  *
302  * Returns the number of bytes encrypted or decrypted; negative value on error
303  */
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)
308 {
309 	struct skcipher_request *req = NULL;
310 	struct extent_crypt_result ecr;
311 	int rc = 0;
312 
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);
320 	}
321 
322 	init_completion(&ecr.completion);
323 
324 	mutex_lock(&crypt_stat->cs_tfm_mutex);
325 	req = skcipher_request_alloc(crypt_stat->tfm, GFP_NOFS);
326 	if (!req) {
327 		mutex_unlock(&crypt_stat->cs_tfm_mutex);
328 		rc = -ENOMEM;
329 		goto out;
330 	}
331 
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);
339 		if (rc) {
340 			ecryptfs_printk(KERN_ERR,
341 					"Error setting key; rc = [%d]\n",
342 					rc);
343 			mutex_unlock(&crypt_stat->cs_tfm_mutex);
344 			rc = -EINVAL;
345 			goto out;
346 		}
347 		crypt_stat->flags |= ECRYPTFS_KEY_SET;
348 	}
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;
355 
356 		wait_for_completion(&ecr->completion);
357 		rc = ecr->rc;
358 		reinit_completion(&ecr->completion);
359 	}
360 out:
361 	skcipher_request_free(req);
362 	return rc;
363 }
364 
365 /**
366  * lower_offset_for_page
367  *
368  * Convert an eCryptfs page index into a lower byte offset
369  */
370 static loff_t lower_offset_for_page(struct ecryptfs_crypt_stat *crypt_stat,
371 				    struct page *page)
372 {
373 	return ecryptfs_lower_header_size(crypt_stat) +
374 	       ((loff_t)page->index << PAGE_SHIFT);
375 }
376 
377 /**
378  * crypt_extent
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
385  *
386  * Encrypts or decrypts one extent of data.
387  *
388  * Return zero on success; non-zero otherwise
389  */
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)
394 {
395 	pgoff_t page_index = op == ENCRYPT ? src_page->index : dst_page->index;
396 	loff_t extent_base;
397 	char extent_iv[ECRYPTFS_MAX_IV_BYTES];
398 	struct scatterlist src_sg, dst_sg;
399 	size_t extent_size = crypt_stat->extent_size;
400 	int rc;
401 
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));
405 	if (rc) {
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);
409 		goto out;
410 	}
411 
412 	sg_init_table(&src_sg, 1);
413 	sg_init_table(&dst_sg, 1);
414 
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);
419 
420 	rc = crypt_scatterlist(crypt_stat, &dst_sg, &src_sg, extent_size,
421 			       extent_iv, op);
422 	if (rc < 0) {
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);
426 		goto out;
427 	}
428 	rc = 0;
429 out:
430 	return rc;
431 }
432 
433 /**
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
438  *
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.
446  *
447  * Returns zero on success; negative on error
448  */
449 int ecryptfs_encrypt_page(struct page *page)
450 {
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;
456 	loff_t lower_offset;
457 	int rc = 0;
458 
459 	ecryptfs_inode = page->mapping->host;
460 	crypt_stat =
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) {
465 		rc = -ENOMEM;
466 		ecryptfs_printk(KERN_ERR, "Error allocating memory for "
467 				"encrypted extent\n");
468 		goto out;
469 	}
470 
471 	for (extent_offset = 0;
472 	     extent_offset < (PAGE_SIZE / crypt_stat->extent_size);
473 	     extent_offset++) {
474 		rc = crypt_extent(crypt_stat, enc_extent_page, page,
475 				  extent_offset, ENCRYPT);
476 		if (rc) {
477 			printk(KERN_ERR "%s: Error encrypting extent; "
478 			       "rc = [%d]\n", __func__, rc);
479 			goto out;
480 		}
481 	}
482 
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,
486 				  PAGE_SIZE);
487 	kunmap(enc_extent_page);
488 	if (rc < 0) {
489 		ecryptfs_printk(KERN_ERR,
490 			"Error attempting to write lower page; rc = [%d]\n",
491 			rc);
492 		goto out;
493 	}
494 	rc = 0;
495 out:
496 	if (enc_extent_page) {
497 		__free_page(enc_extent_page);
498 	}
499 	return rc;
500 }
501 
502 /**
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
506  *        page
507  *
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.
515  *
516  * Returns zero on success; negative on error
517  */
518 int ecryptfs_decrypt_page(struct page *page)
519 {
520 	struct inode *ecryptfs_inode;
521 	struct ecryptfs_crypt_stat *crypt_stat;
522 	char *page_virt;
523 	unsigned long extent_offset;
524 	loff_t lower_offset;
525 	int rc = 0;
526 
527 	ecryptfs_inode = page->mapping->host;
528 	crypt_stat =
529 		&(ecryptfs_inode_to_private(ecryptfs_inode)->crypt_stat);
530 	BUG_ON(!(crypt_stat->flags & ECRYPTFS_ENCRYPTED));
531 
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,
535 				 ecryptfs_inode);
536 	kunmap(page);
537 	if (rc < 0) {
538 		ecryptfs_printk(KERN_ERR,
539 			"Error attempting to read lower page; rc = [%d]\n",
540 			rc);
541 		goto out;
542 	}
543 
544 	for (extent_offset = 0;
545 	     extent_offset < (PAGE_SIZE / crypt_stat->extent_size);
546 	     extent_offset++) {
547 		rc = crypt_extent(crypt_stat, page, page,
548 				  extent_offset, DECRYPT);
549 		if (rc) {
550 			printk(KERN_ERR "%s: Error encrypting extent; "
551 			       "rc = [%d]\n", __func__, rc);
552 			goto out;
553 		}
554 	}
555 out:
556 	return rc;
557 }
558 
559 #define ECRYPTFS_MAX_SCATTERLIST_LEN 4
560 
561 /**
562  * ecryptfs_init_crypt_ctx
563  * @crypt_stat: Uninitialized crypt stats structure
564  *
565  * Initialize the crypto context.
566  *
567  * TODO: Performance: Keep a cache of initialized cipher contexts;
568  * only init if needed
569  */
570 int ecryptfs_init_crypt_ctx(struct ecryptfs_crypt_stat *crypt_stat)
571 {
572 	char *full_alg_name;
573 	int rc = -EINVAL;
574 
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) {
582 		rc = 0;
583 		goto out_unlock;
584 	}
585 	rc = ecryptfs_crypto_api_algify_cipher_name(&full_alg_name,
586 						    crypt_stat->cipher, "cbc");
587 	if (rc)
588 		goto out_unlock;
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",
595 				full_alg_name);
596 		goto out_free;
597 	}
598 	crypto_skcipher_set_flags(crypt_stat->tfm,
599 				  CRYPTO_TFM_REQ_FORBID_WEAK_KEYS);
600 	rc = 0;
601 out_free:
602 	kfree(full_alg_name);
603 out_unlock:
604 	mutex_unlock(&crypt_stat->cs_tfm_mutex);
605 	return rc;
606 }
607 
608 static void set_extent_mask_and_shift(struct ecryptfs_crypt_stat *crypt_stat)
609 {
610 	int extent_size_tmp;
611 
612 	crypt_stat->extent_mask = 0xFFFFFFFF;
613 	crypt_stat->extent_shift = 0;
614 	if (crypt_stat->extent_size == 0)
615 		return;
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++;
621 	}
622 }
623 
624 void ecryptfs_set_default_sizes(struct ecryptfs_crypt_stat *crypt_stat)
625 {
626 	/* Default values; may be overwritten as we are parsing the
627 	 * packets. */
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;
633 	else {
634 		if (PAGE_SIZE <= ECRYPTFS_MINIMUM_HEADER_EXTENT_SIZE)
635 			crypt_stat->metadata_size =
636 				ECRYPTFS_MINIMUM_HEADER_EXTENT_SIZE;
637 		else
638 			crypt_stat->metadata_size = PAGE_SIZE;
639 	}
640 }
641 
642 /**
643  * ecryptfs_compute_root_iv
644  * @crypt_stats
645  *
646  * On error, sets the root IV to all 0's.
647  */
648 int ecryptfs_compute_root_iv(struct ecryptfs_crypt_stat *crypt_stat)
649 {
650 	int rc = 0;
651 	char dst[MD5_DIGEST_SIZE];
652 
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)) {
656 		rc = -EINVAL;
657 		ecryptfs_printk(KERN_WARNING, "Session key not valid; "
658 				"cannot generate root IV\n");
659 		goto out;
660 	}
661 	rc = ecryptfs_calculate_md5(dst, crypt_stat, crypt_stat->key,
662 				    crypt_stat->key_size);
663 	if (rc) {
664 		ecryptfs_printk(KERN_WARNING, "Error attempting to compute "
665 				"MD5 while generating root IV\n");
666 		goto out;
667 	}
668 	memcpy(crypt_stat->root_iv, dst, crypt_stat->iv_bytes);
669 out:
670 	if (rc) {
671 		memset(crypt_stat->root_iv, 0, crypt_stat->iv_bytes);
672 		crypt_stat->flags |= ECRYPTFS_SECURITY_WARNING;
673 	}
674 	return rc;
675 }
676 
677 static void ecryptfs_generate_new_key(struct ecryptfs_crypt_stat *crypt_stat)
678 {
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);
686 	}
687 }
688 
689 /**
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
693  *
694  * This function propagates the mount-wide flags to individual inode
695  * flags.
696  */
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)
700 {
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;
713 	}
714 }
715 
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)
719 {
720 	struct ecryptfs_global_auth_tok *global_auth_tok;
721 	int rc = 0;
722 
723 	mutex_lock(&crypt_stat->keysig_list_mutex);
724 	mutex_lock(&mount_crypt_stat->global_auth_tok_list_mutex);
725 
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)
730 			continue;
731 		rc = ecryptfs_add_keysig(crypt_stat, global_auth_tok->sig);
732 		if (rc) {
733 			printk(KERN_ERR "Error adding keysig; rc = [%d]\n", rc);
734 			goto out;
735 		}
736 	}
737 
738 out:
739 	mutex_unlock(&mount_crypt_stat->global_auth_tok_list_mutex);
740 	mutex_unlock(&crypt_stat->keysig_list_mutex);
741 	return rc;
742 }
743 
744 /**
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
748  *
749  * Default values in the event that policy does not override them.
750  */
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)
754 {
755 	ecryptfs_copy_mount_wide_flags_to_inode_flags(crypt_stat,
756 						      mount_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;
763 }
764 
765 /**
766  * ecryptfs_new_file_context
767  * @ecryptfs_inode: The eCryptfs inode
768  *
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.
781  *
782  * Returns zero on success; non-zero otherwise
783  */
784 int ecryptfs_new_file_context(struct inode *ecryptfs_inode)
785 {
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;
791 	int cipher_name_len;
792 	int rc = 0;
793 
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,
797 						      mount_crypt_stat);
798 	rc = ecryptfs_copy_mount_wide_sigs_to_inode_sigs(crypt_stat,
799 							 mount_crypt_stat);
800 	if (rc) {
801 		printk(KERN_ERR "Error attempting to copy mount-wide key sigs "
802 		       "to the inode key sigs; rc = [%d]\n", rc);
803 		goto out;
804 	}
805 	cipher_name_len =
806 		strlen(mount_crypt_stat->global_default_cipher_name);
807 	memcpy(crypt_stat->cipher,
808 	       mount_crypt_stat->global_default_cipher_name,
809 	       cipher_name_len);
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);
815 	if (rc)
816 		ecryptfs_printk(KERN_ERR, "Error initializing cryptographic "
817 				"context for cipher [%s]: rc = [%d]\n",
818 				crypt_stat->cipher, rc);
819 out:
820 	return rc;
821 }
822 
823 /**
824  * ecryptfs_validate_marker - check for the ecryptfs marker
825  * @data: The data block in which to check
826  *
827  * Returns zero if marker found; -EINVAL if not found
828  */
829 static int ecryptfs_validate_marker(char *data)
830 {
831 	u32 m_1, m_2;
832 
833 	m_1 = get_unaligned_be32(data);
834 	m_2 = get_unaligned_be32(data + 4);
835 	if ((m_1 ^ MAGIC_ECRYPTFS_MARKER) == m_2)
836 		return 0;
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));
842 	return -EINVAL;
843 }
844 
845 struct ecryptfs_flag_map_elem {
846 	u32 file_flag;
847 	u32 local_flag;
848 };
849 
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}
856 };
857 
858 /**
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
863  *
864  * Returns zero on success; non-zero if the flag set is invalid
865  */
866 static int ecryptfs_process_flags(struct ecryptfs_crypt_stat *crypt_stat,
867 				  char *page_virt, int *bytes_read)
868 {
869 	int rc = 0;
870 	int i;
871 	u32 flags;
872 
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;
877 		} else
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);
881 	(*bytes_read) = 4;
882 	return rc;
883 }
884 
885 /**
886  * write_ecryptfs_marker
887  * @page_virt: The pointer to in a page to begin writing the marker
888  * @written: Number of bytes written
889  *
890  * Marker = 0x3c81b7f5
891  */
892 static void write_ecryptfs_marker(char *page_virt, size_t *written)
893 {
894 	u32 m_1, m_2;
895 
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;
902 }
903 
904 void ecryptfs_write_crypt_stat_flags(char *page_virt,
905 				     struct ecryptfs_crypt_stat *crypt_stat,
906 				     size_t *written)
907 {
908 	u32 flags = 0;
909 	int i;
910 
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);
917 	(*written) = 4;
918 }
919 
920 struct ecryptfs_cipher_code_str_map_elem {
921 	char cipher_str[16];
922 	u8 cipher_code;
923 };
924 
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}
938 };
939 
940 /**
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
944  *
945  * Returns zero on no match, or the cipher code on match
946  */
947 u8 ecryptfs_code_for_cipher_string(char *cipher_name, size_t key_bytes)
948 {
949 	int i;
950 	u8 code = 0;
951 	struct ecryptfs_cipher_code_str_map_elem *map =
952 		ecryptfs_cipher_code_str_map;
953 
954 	if (strcmp(cipher_name, "aes") == 0) {
955 		switch (key_bytes) {
956 		case 16:
957 			code = RFC2440_CIPHER_AES_128;
958 			break;
959 		case 24:
960 			code = RFC2440_CIPHER_AES_192;
961 			break;
962 		case 32:
963 			code = RFC2440_CIPHER_AES_256;
964 		}
965 	} else {
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;
969 				break;
970 			}
971 	}
972 	return code;
973 }
974 
975 /**
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
979  *
980  * Returns zero on success
981  */
982 int ecryptfs_cipher_code_to_string(char *str, u8 cipher_code)
983 {
984 	int rc = 0;
985 	int i;
986 
987 	str[0] = '\0';
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);
994 		rc = -EINVAL;
995 	}
996 	return rc;
997 }
998 
999 int ecryptfs_read_and_validate_header_region(struct inode *inode)
1000 {
1001 	u8 file_size[ECRYPTFS_SIZE_AND_MARKER_BYTES];
1002 	u8 *marker = file_size + ECRYPTFS_FILE_SIZE_BYTES;
1003 	int rc;
1004 
1005 	rc = ecryptfs_read_lower(file_size, 0, ECRYPTFS_SIZE_AND_MARKER_BYTES,
1006 				 inode);
1007 	if (rc < ECRYPTFS_SIZE_AND_MARKER_BYTES)
1008 		return rc >= 0 ? -EINVAL : rc;
1009 	rc = ecryptfs_validate_marker(marker);
1010 	if (!rc)
1011 		ecryptfs_i_size_init(file_size, inode);
1012 	return rc;
1013 }
1014 
1015 void
1016 ecryptfs_write_header_metadata(char *virt,
1017 			       struct ecryptfs_crypt_stat *crypt_stat,
1018 			       size_t *written)
1019 {
1020 	u32 header_extent_size;
1021 	u16 num_header_extents_at_front;
1022 
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);
1027 	virt += 4;
1028 	put_unaligned_be16(num_header_extents_at_front, virt);
1029 	(*written) = 6;
1030 }
1031 
1032 struct kmem_cache *ecryptfs_header_cache;
1033 
1034 /**
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
1041  *
1042  * Format version: 1
1043  *
1044  *   Header Extent:
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
1051  *                        Bit 2: Encrypted?
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
1055  *                        (big-endian)
1056  *     Octet  26:         Begin RFC 2440 authentication token packet set
1057  *   Data Extent 0:
1058  *     Lower data (CBC encrypted)
1059  *   Data Extent 1:
1060  *     Lower data (CBC encrypted)
1061  *   ...
1062  *
1063  * Returns zero on success
1064  */
1065 static int ecryptfs_write_headers_virt(char *page_virt, size_t max,
1066 				       size_t *size,
1067 				       struct ecryptfs_crypt_stat *crypt_stat,
1068 				       struct dentry *ecryptfs_dentry)
1069 {
1070 	int rc;
1071 	size_t written;
1072 	size_t offset;
1073 
1074 	offset = ECRYPTFS_FILE_SIZE_BYTES;
1075 	write_ecryptfs_marker((page_virt + offset), &written);
1076 	offset += written;
1077 	ecryptfs_write_crypt_stat_flags((page_virt + offset), crypt_stat,
1078 					&written);
1079 	offset += written;
1080 	ecryptfs_write_header_metadata((page_virt + offset), crypt_stat,
1081 				       &written);
1082 	offset += written;
1083 	rc = ecryptfs_generate_key_packet_set((page_virt + offset), crypt_stat,
1084 					      ecryptfs_dentry, &written,
1085 					      max - offset);
1086 	if (rc)
1087 		ecryptfs_printk(KERN_WARNING, "Error generating key packet "
1088 				"set; rc = [%d]\n", rc);
1089 	if (size) {
1090 		offset += written;
1091 		*size = offset;
1092 	}
1093 	return rc;
1094 }
1095 
1096 static int
1097 ecryptfs_write_metadata_to_contents(struct inode *ecryptfs_inode,
1098 				    char *virt, size_t virt_len)
1099 {
1100 	int rc;
1101 
1102 	rc = ecryptfs_write_lower(ecryptfs_inode, virt,
1103 				  0, virt_len);
1104 	if (rc < 0)
1105 		printk(KERN_ERR "%s: Error attempting to write header "
1106 		       "information to lower file; rc = [%d]\n", __func__, rc);
1107 	else
1108 		rc = 0;
1109 	return rc;
1110 }
1111 
1112 static int
1113 ecryptfs_write_metadata_to_xattr(struct dentry *ecryptfs_dentry,
1114 				 struct inode *ecryptfs_inode,
1115 				 char *page_virt, size_t size)
1116 {
1117 	int rc;
1118 
1119 	rc = ecryptfs_setxattr(ecryptfs_dentry, ecryptfs_inode,
1120 			       ECRYPTFS_XATTR_NAME, page_virt, size, 0);
1121 	return rc;
1122 }
1123 
1124 static unsigned long ecryptfs_get_zeroed_pages(gfp_t gfp_mask,
1125 					       unsigned int order)
1126 {
1127 	struct page *page;
1128 
1129 	page = alloc_pages(gfp_mask | __GFP_ZERO, order);
1130 	if (page)
1131 		return (unsigned long) page_address(page);
1132 	return 0;
1133 }
1134 
1135 /**
1136  * ecryptfs_write_metadata
1137  * @ecryptfs_dentry: The eCryptfs dentry, which should be negative
1138  * @ecryptfs_inode: The newly created eCryptfs inode
1139  *
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.
1145  *
1146  * Returns zero on success; non-zero on error
1147  */
1148 int ecryptfs_write_metadata(struct dentry *ecryptfs_dentry,
1149 			    struct inode *ecryptfs_inode)
1150 {
1151 	struct ecryptfs_crypt_stat *crypt_stat =
1152 		&ecryptfs_inode_to_private(ecryptfs_inode)->crypt_stat;
1153 	unsigned int order;
1154 	char *virt;
1155 	size_t virt_len;
1156 	size_t size = 0;
1157 	int rc = 0;
1158 
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");
1162 			rc = -EINVAL;
1163 			goto out;
1164 		}
1165 	} else {
1166 		printk(KERN_WARNING "%s: Encrypted flag not set\n",
1167 		       __func__);
1168 		rc = -EINVAL;
1169 		goto out;
1170 	}
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);
1175 	if (!virt) {
1176 		printk(KERN_ERR "%s: Out of memory\n", __func__);
1177 		rc = -ENOMEM;
1178 		goto out;
1179 	}
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,
1182 					 ecryptfs_dentry);
1183 	if (unlikely(rc)) {
1184 		printk(KERN_ERR "%s: Error whilst writing headers; rc = [%d]\n",
1185 		       __func__, rc);
1186 		goto out_free;
1187 	}
1188 	if (crypt_stat->flags & ECRYPTFS_METADATA_IN_XATTR)
1189 		rc = ecryptfs_write_metadata_to_xattr(ecryptfs_dentry, ecryptfs_inode,
1190 						      virt, size);
1191 	else
1192 		rc = ecryptfs_write_metadata_to_contents(ecryptfs_inode, virt,
1193 							 virt_len);
1194 	if (rc) {
1195 		printk(KERN_ERR "%s: Error writing metadata out to lower file; "
1196 		       "rc = [%d]\n", __func__, rc);
1197 		goto out_free;
1198 	}
1199 out_free:
1200 	free_pages((unsigned long)virt, order);
1201 out:
1202 	return rc;
1203 }
1204 
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)
1210 {
1211 	int rc = 0;
1212 	u32 header_extent_size;
1213 	u16 num_header_extents_at_front;
1214 
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)) {
1224 		rc = -EINVAL;
1225 		printk(KERN_WARNING "Invalid header size: [%zd]\n",
1226 		       crypt_stat->metadata_size);
1227 	}
1228 	return rc;
1229 }
1230 
1231 /**
1232  * set_default_header_data
1233  * @crypt_stat: The cryptographic context
1234  *
1235  * For version 0 file format; this function is only for backwards
1236  * compatibility for files created with the prior versions of
1237  * eCryptfs.
1238  */
1239 static void set_default_header_data(struct ecryptfs_crypt_stat *crypt_stat)
1240 {
1241 	crypt_stat->metadata_size = ECRYPTFS_MINIMUM_HEADER_EXTENT_SIZE;
1242 }
1243 
1244 void ecryptfs_i_size_init(const char *page_virt, struct inode *inode)
1245 {
1246 	struct ecryptfs_mount_crypt_stat *mount_crypt_stat;
1247 	struct ecryptfs_crypt_stat *crypt_stat;
1248 	u64 file_size;
1249 
1250 	crypt_stat = &ecryptfs_inode_to_private(inode)->crypt_stat;
1251 	mount_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;
1257 	} else
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;
1261 }
1262 
1263 /**
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
1269  *
1270  * Read/parse the header data. The header format is detailed in the
1271  * comment block for the ecryptfs_write_headers_virt() function.
1272  *
1273  * Returns zero on success
1274  */
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)
1279 {
1280 	int rc = 0;
1281 	int offset;
1282 	int bytes_read;
1283 
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);
1289 	if (rc)
1290 		goto out;
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),
1295 				    &bytes_read);
1296 	if (rc) {
1297 		ecryptfs_printk(KERN_WARNING, "Error processing flags\n");
1298 		goto out;
1299 	}
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);
1306 		rc = -EINVAL;
1307 		goto out;
1308 	}
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);
1313 		if (rc) {
1314 			ecryptfs_printk(KERN_WARNING, "Error reading header "
1315 					"metadata; rc = [%d]\n", rc);
1316 		}
1317 		offset += bytes_read;
1318 	} else
1319 		set_default_header_data(crypt_stat);
1320 	rc = ecryptfs_parse_packet_set(crypt_stat, (page_virt + offset),
1321 				       ecryptfs_dentry);
1322 out:
1323 	return rc;
1324 }
1325 
1326 /**
1327  * ecryptfs_read_xattr_region
1328  * @page_virt: The vitual address into which to read the xattr data
1329  * @ecryptfs_inode: The eCryptfs inode
1330  *
1331  * Attempts to read the crypto metadata from the extended attribute
1332  * region of the lower file.
1333  *
1334  * Returns zero on success; non-zero on error
1335  */
1336 int ecryptfs_read_xattr_region(char *page_virt, struct inode *ecryptfs_inode)
1337 {
1338 	struct dentry *lower_dentry =
1339 		ecryptfs_inode_to_private(ecryptfs_inode)->lower_file->f_path.dentry;
1340 	ssize_t size;
1341 	int rc = 0;
1342 
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);
1347 	if (size < 0) {
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);
1352 		rc = -EINVAL;
1353 		goto out;
1354 	}
1355 out:
1356 	return rc;
1357 }
1358 
1359 int ecryptfs_read_and_validate_xattr_region(struct dentry *dentry,
1360 					    struct inode *inode)
1361 {
1362 	u8 file_size[ECRYPTFS_SIZE_AND_MARKER_BYTES];
1363 	u8 *marker = file_size + ECRYPTFS_FILE_SIZE_BYTES;
1364 	int rc;
1365 
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);
1373 	if (!rc)
1374 		ecryptfs_i_size_init(file_size, inode);
1375 	return rc;
1376 }
1377 
1378 /**
1379  * ecryptfs_read_metadata
1380  *
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.
1387  *
1388  * Returns zero if valid headers found and parsed; non-zero otherwise
1389  */
1390 int ecryptfs_read_metadata(struct dentry *ecryptfs_dentry)
1391 {
1392 	int rc;
1393 	char *page_virt;
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;
1400 
1401 	ecryptfs_copy_mount_wide_flags_to_inode_flags(crypt_stat,
1402 						      mount_crypt_stat);
1403 	/* Read the first page from the underlying file */
1404 	page_virt = kmem_cache_alloc(ecryptfs_header_cache, GFP_USER);
1405 	if (!page_virt) {
1406 		rc = -ENOMEM;
1407 		goto out;
1408 	}
1409 	rc = ecryptfs_read_lower(page_virt, 0, crypt_stat->extent_size,
1410 				 ecryptfs_inode);
1411 	if (rc >= 0)
1412 		rc = ecryptfs_read_headers_virt(page_virt, crypt_stat,
1413 						ecryptfs_dentry,
1414 						ECRYPTFS_VALIDATE_HEADER_SIZE);
1415 	if (rc) {
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);
1419 		if (rc) {
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);
1423 			rc = -EINVAL;
1424 			goto out;
1425 		}
1426 		rc = ecryptfs_read_headers_virt(page_virt, crypt_stat,
1427 						ecryptfs_dentry,
1428 						ECRYPTFS_DONT_VALIDATE_HEADER_SIZE);
1429 		if (rc) {
1430 			printk(KERN_DEBUG "Valid eCryptfs headers not found in "
1431 			       "file xattr region either, inode %lu\n",
1432 				ecryptfs_inode->i_ino);
1433 			rc = -EINVAL;
1434 		}
1435 		if (crypt_stat->mount_crypt_stat->flags
1436 		    & ECRYPTFS_XATTR_METADATA_ENABLED) {
1437 			crypt_stat->flags |= ECRYPTFS_METADATA_IN_XATTR;
1438 		} else {
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);
1445 			rc = -EINVAL;
1446 		}
1447 	}
1448 out:
1449 	if (page_virt) {
1450 		memset(page_virt, 0, PAGE_SIZE);
1451 		kmem_cache_free(ecryptfs_header_cache, page_virt);
1452 	}
1453 	return rc;
1454 }
1455 
1456 /**
1457  * ecryptfs_encrypt_filename - encrypt filename
1458  *
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.
1462  *
1463  * Returns zero on success; non-zero otherwise
1464  */
1465 static int
1466 ecryptfs_encrypt_filename(struct ecryptfs_filename *filename,
1467 			  struct ecryptfs_mount_crypt_stat *mount_crypt_stat)
1468 {
1469 	int rc = 0;
1470 
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)) {
1475 		size_t packet_size;
1476 		size_t remaining_bytes;
1477 
1478 		rc = ecryptfs_write_tag_70_packet(
1479 			NULL, NULL,
1480 			&filename->encrypted_filename_size,
1481 			mount_crypt_stat, NULL,
1482 			filename->filename_size);
1483 		if (rc) {
1484 			printk(KERN_ERR "%s: Error attempting to get packet "
1485 			       "size for tag 72; rc = [%d]\n", __func__,
1486 			       rc);
1487 			filename->encrypted_filename_size = 0;
1488 			goto out;
1489 		}
1490 		filename->encrypted_filename =
1491 			kmalloc(filename->encrypted_filename_size, GFP_KERNEL);
1492 		if (!filename->encrypted_filename) {
1493 			rc = -ENOMEM;
1494 			goto out;
1495 		}
1496 		remaining_bytes = filename->encrypted_filename_size;
1497 		rc = ecryptfs_write_tag_70_packet(filename->encrypted_filename,
1498 						  &remaining_bytes,
1499 						  &packet_size,
1500 						  mount_crypt_stat,
1501 						  filename->filename,
1502 						  filename->filename_size);
1503 		if (rc) {
1504 			printk(KERN_ERR "%s: Error attempting to generate "
1505 			       "tag 70 packet; rc = [%d]\n", __func__,
1506 			       rc);
1507 			kfree(filename->encrypted_filename);
1508 			filename->encrypted_filename = NULL;
1509 			filename->encrypted_filename_size = 0;
1510 			goto out;
1511 		}
1512 		filename->encrypted_filename_size = packet_size;
1513 	} else {
1514 		printk(KERN_ERR "%s: No support for requested filename "
1515 		       "encryption method in this release\n", __func__);
1516 		rc = -EOPNOTSUPP;
1517 		goto out;
1518 	}
1519 out:
1520 	return rc;
1521 }
1522 
1523 static int ecryptfs_copy_filename(char **copied_name, size_t *copied_name_size,
1524 				  const char *name, size_t name_size)
1525 {
1526 	int rc = 0;
1527 
1528 	(*copied_name) = kmalloc((name_size + 1), GFP_KERNEL);
1529 	if (!(*copied_name)) {
1530 		rc = -ENOMEM;
1531 		goto out;
1532 	}
1533 	memcpy((void *)(*copied_name), (void *)name, name_size);
1534 	(*copied_name)[(name_size)] = '\0';	/* Only for convenience
1535 						 * in printing out the
1536 						 * string in debug
1537 						 * messages */
1538 	(*copied_name_size) = name_size;
1539 out:
1540 	return rc;
1541 }
1542 
1543 /**
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
1548  *
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.
1552  */
1553 static int
1554 ecryptfs_process_key_cipher(struct crypto_skcipher **key_tfm,
1555 			    char *cipher_name, size_t *key_size)
1556 {
1557 	char dummy_key[ECRYPTFS_MAX_KEY_BYTES];
1558 	char *full_alg_name = NULL;
1559 	int rc;
1560 
1561 	*key_tfm = NULL;
1562 	if (*key_size > ECRYPTFS_MAX_KEY_BYTES) {
1563 		rc = -EINVAL;
1564 		printk(KERN_ERR "Requested key size is [%zd] bytes; maximum "
1565 		      "allowable is [%d]\n", *key_size, ECRYPTFS_MAX_KEY_BYTES);
1566 		goto out;
1567 	}
1568 	rc = ecryptfs_crypto_api_algify_cipher_name(&full_alg_name, cipher_name,
1569 						    "ecb");
1570 	if (rc)
1571 		goto out;
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);
1577 		goto out;
1578 	}
1579 	crypto_skcipher_set_flags(*key_tfm, CRYPTO_TFM_REQ_FORBID_WEAK_KEYS);
1580 	if (*key_size == 0)
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);
1584 	if (rc) {
1585 		printk(KERN_ERR "Error attempting to set key of size [%zd] for "
1586 		       "cipher [%s]; rc = [%d]\n", *key_size, full_alg_name,
1587 		       rc);
1588 		rc = -EINVAL;
1589 		goto out;
1590 	}
1591 out:
1592 	kfree(full_alg_name);
1593 	return rc;
1594 }
1595 
1596 struct kmem_cache *ecryptfs_key_tfm_cache;
1597 static struct list_head key_tfm_list;
1598 struct mutex key_tfm_list_mutex;
1599 
1600 int __init ecryptfs_init_crypto(void)
1601 {
1602 	mutex_init(&key_tfm_list_mutex);
1603 	INIT_LIST_HEAD(&key_tfm_list);
1604 	return 0;
1605 }
1606 
1607 /**
1608  * ecryptfs_destroy_crypto - free all cached key_tfms on key_tfm_list
1609  *
1610  * Called only at module unload time
1611  */
1612 int ecryptfs_destroy_crypto(void)
1613 {
1614 	struct ecryptfs_key_tfm *key_tfm, *key_tfm_tmp;
1615 
1616 	mutex_lock(&key_tfm_list_mutex);
1617 	list_for_each_entry_safe(key_tfm, key_tfm_tmp, &key_tfm_list,
1618 				 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);
1622 	}
1623 	mutex_unlock(&key_tfm_list_mutex);
1624 	return 0;
1625 }
1626 
1627 int
1628 ecryptfs_add_new_key_tfm(struct ecryptfs_key_tfm **key_tfm, char *cipher_name,
1629 			 size_t key_size)
1630 {
1631 	struct ecryptfs_key_tfm *tmp_tfm;
1632 	int rc = 0;
1633 
1634 	BUG_ON(!mutex_is_locked(&key_tfm_list_mutex));
1635 
1636 	tmp_tfm = kmem_cache_alloc(ecryptfs_key_tfm_cache, GFP_KERNEL);
1637 	if (key_tfm)
1638 		(*key_tfm) = tmp_tfm;
1639 	if (!tmp_tfm) {
1640 		rc = -ENOMEM;
1641 		goto out;
1642 	}
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);
1651 	if (rc) {
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);
1656 		if (key_tfm)
1657 			(*key_tfm) = NULL;
1658 		goto out;
1659 	}
1660 	list_add(&tmp_tfm->key_tfm_list, &key_tfm_list);
1661 out:
1662 	return rc;
1663 }
1664 
1665 /**
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
1669  *
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
1674  */
1675 int ecryptfs_tfm_exists(char *cipher_name, struct ecryptfs_key_tfm **key_tfm)
1676 {
1677 	struct ecryptfs_key_tfm *tmp_key_tfm;
1678 
1679 	BUG_ON(!mutex_is_locked(&key_tfm_list_mutex));
1680 
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) {
1683 			if (key_tfm)
1684 				(*key_tfm) = tmp_key_tfm;
1685 			return 1;
1686 		}
1687 	}
1688 	if (key_tfm)
1689 		(*key_tfm) = NULL;
1690 	return 0;
1691 }
1692 
1693 /**
1694  * ecryptfs_get_tfm_and_mutex_for_cipher_name
1695  *
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
1699  *
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
1703  */
1704 int ecryptfs_get_tfm_and_mutex_for_cipher_name(struct crypto_skcipher **tfm,
1705 					       struct mutex **tfm_mutex,
1706 					       char *cipher_name)
1707 {
1708 	struct ecryptfs_key_tfm *key_tfm;
1709 	int rc = 0;
1710 
1711 	(*tfm) = NULL;
1712 	(*tfm_mutex) = NULL;
1713 
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);
1717 		if (rc) {
1718 			printk(KERN_ERR "Error adding new key_tfm to list; "
1719 					"rc = [%d]\n", rc);
1720 			goto out;
1721 		}
1722 	}
1723 	(*tfm) = key_tfm->key_tfm;
1724 	(*tfm_mutex) = &key_tfm->key_tfm_mutex;
1725 out:
1726 	mutex_unlock(&key_tfm_list_mutex);
1727 	return rc;
1728 }
1729 
1730 /* 64 characters forming a 6-bit target field */
1731 static unsigned char *portable_filename_chars = ("-.0123456789ABCD"
1732 						 "EFGHIJKLMNOPQRST"
1733 						 "UVWXYZabcdefghij"
1734 						 "klmnopqrstuvwxyz");
1735 
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 */
1755 };
1756 
1757 /**
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
1763  */
1764 static void ecryptfs_encode_for_filename(unsigned char *dst, size_t *dst_size,
1765 				  unsigned char *src, size_t src_size)
1766 {
1767 	size_t num_blocks;
1768 	size_t block_num = 0;
1769 	size_t dst_offset = 0;
1770 	unsigned char last_block[3];
1771 
1772 	if (src_size == 0) {
1773 		(*dst_size) = 0;
1774 		goto out;
1775 	}
1776 	num_blocks = (src_size / 3);
1777 	if ((src_size % 3) == 0) {
1778 		memcpy(last_block, (&src[src_size - 3]), 3);
1779 	} else {
1780 		num_blocks++;
1781 		last_block[2] = 0x00;
1782 		switch (src_size % 3) {
1783 		case 1:
1784 			last_block[0] = src[src_size - 1];
1785 			last_block[1] = 0x00;
1786 			break;
1787 		case 2:
1788 			last_block[0] = src[src_size - 2];
1789 			last_block[1] = src[src_size - 1];
1790 		}
1791 	}
1792 	(*dst_size) = (num_blocks * 4);
1793 	if (!dst)
1794 		goto out;
1795 	while (block_num < num_blocks) {
1796 		unsigned char *src_block;
1797 		unsigned char dst_block[4];
1798 
1799 		if (block_num == (num_blocks - 1))
1800 			src_block = last_block;
1801 		else
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]];
1813 		block_num++;
1814 	}
1815 out:
1816 	return;
1817 }
1818 
1819 static size_t ecryptfs_max_decoded_size(size_t encoded_size)
1820 {
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;
1828 }
1829 
1830 /**
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.
1838  */
1839 static void
1840 ecryptfs_decode_from_filename(unsigned char *dst, size_t *dst_size,
1841 			      const unsigned char *src, size_t src_size)
1842 {
1843 	u8 current_bit_offset = 0;
1844 	size_t src_byte_offset = 0;
1845 	size_t dst_byte_offset = 0;
1846 
1847 	if (!dst) {
1848 		(*dst_size) = ecryptfs_max_decoded_size(src_size);
1849 		goto out;
1850 	}
1851 	while (src_byte_offset < src_size) {
1852 		unsigned char src_byte =
1853 				filename_rev_map[(int)src[src_byte_offset]];
1854 
1855 		switch (current_bit_offset) {
1856 		case 0:
1857 			dst[dst_byte_offset] = (src_byte << 2);
1858 			current_bit_offset = 6;
1859 			break;
1860 		case 6:
1861 			dst[dst_byte_offset++] |= (src_byte >> 4);
1862 			dst[dst_byte_offset] = ((src_byte & 0xF)
1863 						 << 4);
1864 			current_bit_offset = 4;
1865 			break;
1866 		case 4:
1867 			dst[dst_byte_offset++] |= (src_byte >> 2);
1868 			dst[dst_byte_offset] = (src_byte << 6);
1869 			current_bit_offset = 2;
1870 			break;
1871 		case 2:
1872 			dst[dst_byte_offset++] |= (src_byte);
1873 			current_bit_offset = 0;
1874 			break;
1875 		}
1876 		src_byte_offset++;
1877 	}
1878 	(*dst_size) = dst_byte_offset;
1879 out:
1880 	return;
1881 }
1882 
1883 /**
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
1889  *
1890  * Encrypts and encodes a filename into something that constitutes a
1891  * valid filename for a filesystem, with printable characters.
1892  *
1893  * We assume that we have a properly initialized crypto context,
1894  * pointed to by crypt_stat->tfm.
1895  *
1896  * Returns zero on success; non-zero on otherwise
1897  */
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)
1903 {
1904 	size_t encoded_name_no_prefix_size;
1905 	int rc = 0;
1906 
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;
1912 
1913 		filename = kzalloc(sizeof(*filename), GFP_KERNEL);
1914 		if (!filename) {
1915 			rc = -ENOMEM;
1916 			goto out;
1917 		}
1918 		filename->filename = (char *)name;
1919 		filename->filename_size = name_size;
1920 		rc = ecryptfs_encrypt_filename(filename, mount_crypt_stat);
1921 		if (rc) {
1922 			printk(KERN_ERR "%s: Error attempting to encrypt "
1923 			       "filename; rc = [%d]\n", __func__, rc);
1924 			kfree(filename);
1925 			goto out;
1926 		}
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);
1937 		else
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)) {
1943 			rc = -ENOMEM;
1944 			kfree(filename->encrypted_filename);
1945 			kfree(filename);
1946 			goto out;
1947 		}
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(
1955 			    ((*encoded_name)
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';
1964 		} else {
1965 			rc = -EOPNOTSUPP;
1966 		}
1967 		if (rc) {
1968 			printk(KERN_ERR "%s: Error attempting to encode "
1969 			       "encrypted filename; rc = [%d]\n", __func__,
1970 			       rc);
1971 			kfree((*encoded_name));
1972 			(*encoded_name) = NULL;
1973 			(*encoded_name_size) = 0;
1974 		}
1975 		kfree(filename->encrypted_filename);
1976 		kfree(filename);
1977 	} else {
1978 		rc = ecryptfs_copy_filename(encoded_name,
1979 					    encoded_name_size,
1980 					    name, name_size);
1981 	}
1982 out:
1983 	return rc;
1984 }
1985 
1986 static bool is_dot_dotdot(const char *name, size_t name_size)
1987 {
1988 	if (name_size == 1 && name[0] == '.')
1989 		return true;
1990 	else if (name_size == 2 && name[0] == '.' && name[1] == '.')
1991 		return true;
1992 
1993 	return false;
1994 }
1995 
1996 /**
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
2003  *
2004  * Decrypts and decodes the filename.
2005  *
2006  * Returns zero on error; non-zero otherwise
2007  */
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)
2012 {
2013 	struct ecryptfs_mount_crypt_stat *mount_crypt_stat =
2014 		&ecryptfs_superblock_to_private(sb)->mount_crypt_stat;
2015 	char *decoded_name;
2016 	size_t decoded_name_size;
2017 	size_t packet_size;
2018 	int rc = 0;
2019 
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,
2025 						    name, name_size);
2026 			goto out;
2027 		}
2028 
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)) {
2032 			rc = -EINVAL;
2033 			goto out;
2034 		}
2035 
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,
2039 					      name, name_size);
2040 		decoded_name = kmalloc(decoded_name_size, GFP_KERNEL);
2041 		if (!decoded_name) {
2042 			rc = -ENOMEM;
2043 			goto out;
2044 		}
2045 		ecryptfs_decode_from_filename(decoded_name, &decoded_name_size,
2046 					      name, name_size);
2047 		rc = ecryptfs_parse_tag_70_packet(plaintext_name,
2048 						  plaintext_name_size,
2049 						  &packet_size,
2050 						  mount_crypt_stat,
2051 						  decoded_name,
2052 						  decoded_name_size);
2053 		if (rc) {
2054 			ecryptfs_printk(KERN_DEBUG,
2055 					"%s: Could not parse tag 70 packet from filename\n",
2056 					__func__);
2057 			goto out_free;
2058 		}
2059 	} else {
2060 		rc = ecryptfs_copy_filename(plaintext_name,
2061 					    plaintext_name_size,
2062 					    name, name_size);
2063 		goto out;
2064 	}
2065 out_free:
2066 	kfree(decoded_name);
2067 out:
2068 	return rc;
2069 }
2070 
2071 #define ENC_NAME_MAX_BLOCKLEN_8_OR_16	143
2072 
2073 int ecryptfs_set_f_namelen(long *namelen, long lower_namelen,
2074 			   struct ecryptfs_mount_crypt_stat *mount_crypt_stat)
2075 {
2076 	struct crypto_skcipher *tfm;
2077 	struct mutex *tfm_mutex;
2078 	size_t cipher_blocksize;
2079 	int rc;
2080 
2081 	if (!(mount_crypt_stat->flags & ECRYPTFS_GLOBAL_ENCRYPT_FILENAMES)) {
2082 		(*namelen) = lower_namelen;
2083 		return 0;
2084 	}
2085 
2086 	rc = ecryptfs_get_tfm_and_mutex_for_cipher_name(&tfm, &tfm_mutex,
2087 			mount_crypt_stat->global_default_fn_cipher_name);
2088 	if (unlikely(rc)) {
2089 		(*namelen) = 0;
2090 		return rc;
2091 	}
2092 
2093 	mutex_lock(tfm_mutex);
2094 	cipher_blocksize = crypto_skcipher_blocksize(tfm);
2095 	mutex_unlock(tfm_mutex);
2096 
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;
2101 		return 0;
2102 	}
2103 
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;
2113 
2114 	if ((*namelen) < 0)
2115 		(*namelen) = 0;
2116 
2117 	return 0;
2118 }
2119