xref: /openbmc/linux/fs/ecryptfs/crypto.c (revision 39f60c1ccee72caa0104145b5dbf5d37cce1ea39)
11a59d1b8SThomas Gleixner // SPDX-License-Identifier: GPL-2.0-or-later
25da877eaSLee Jones /*
3237fead6SMichael Halcrow  * eCryptfs: Linux filesystem encryption layer
4237fead6SMichael Halcrow  *
5237fead6SMichael Halcrow  * Copyright (C) 1997-2004 Erez Zadok
6237fead6SMichael Halcrow  * Copyright (C) 2001-2004 Stony Brook University
7dd2a3b7aSMichael Halcrow  * Copyright (C) 2004-2007 International Business Machines Corp.
8237fead6SMichael Halcrow  *   Author(s): Michael A. Halcrow <mahalcro@us.ibm.com>
9237fead6SMichael Halcrow  *   		Michael C. Thompson <mcthomps@us.ibm.com>
10237fead6SMichael Halcrow  */
11237fead6SMichael Halcrow 
123095e8e3SHerbert Xu #include <crypto/hash.h>
133095e8e3SHerbert Xu #include <crypto/skcipher.h>
14237fead6SMichael Halcrow #include <linux/fs.h>
15237fead6SMichael Halcrow #include <linux/mount.h>
16237fead6SMichael Halcrow #include <linux/pagemap.h>
17237fead6SMichael Halcrow #include <linux/random.h>
18237fead6SMichael Halcrow #include <linux/compiler.h>
19237fead6SMichael Halcrow #include <linux/key.h>
20237fead6SMichael Halcrow #include <linux/namei.h>
21237fead6SMichael Halcrow #include <linux/file.h>
22237fead6SMichael Halcrow #include <linux/scatterlist.h>
235a0e3ad6STejun Heo #include <linux/slab.h>
2429335c6aSHarvey Harrison #include <asm/unaligned.h>
2502f9876eSJérémy Lefaure #include <linux/kernel.h>
26d43388deSRobbie Ko #include <linux/xattr.h>
27237fead6SMichael Halcrow #include "ecryptfs_kernel.h"
28237fead6SMichael Halcrow 
2900a69940STyler Hicks #define DECRYPT		0
3000a69940STyler Hicks #define ENCRYPT		1
31237fead6SMichael Halcrow 
32237fead6SMichael Halcrow /**
33237fead6SMichael Halcrow  * ecryptfs_from_hex
34237fead6SMichael Halcrow  * @dst: Buffer to take the bytes from src hex; must be at least of
35237fead6SMichael Halcrow  *       size (src_size / 2)
365f9f2c2aSWei Yuan  * @src: Buffer to be converted from a hex string representation to raw value
37237fead6SMichael Halcrow  * @dst_size: size of dst buffer, or number of hex characters pairs to convert
38237fead6SMichael Halcrow  */
39237fead6SMichael Halcrow void ecryptfs_from_hex(char *dst, char *src, int dst_size)
40237fead6SMichael Halcrow {
41237fead6SMichael Halcrow 	int x;
42237fead6SMichael Halcrow 	char tmp[3] = { 0, };
43237fead6SMichael Halcrow 
44237fead6SMichael Halcrow 	for (x = 0; x < dst_size; x++) {
45237fead6SMichael Halcrow 		tmp[0] = src[x * 2];
46237fead6SMichael Halcrow 		tmp[1] = src[x * 2 + 1];
47237fead6SMichael Halcrow 		dst[x] = (unsigned char)simple_strtol(tmp, NULL, 16);
48237fead6SMichael Halcrow 	}
49237fead6SMichael Halcrow }
50237fead6SMichael Halcrow 
51237fead6SMichael Halcrow /**
52237fead6SMichael Halcrow  * ecryptfs_calculate_md5 - calculates the md5 of @src
53237fead6SMichael Halcrow  * @dst: Pointer to 16 bytes of allocated memory
54237fead6SMichael Halcrow  * @crypt_stat: Pointer to crypt_stat struct for the current inode
55237fead6SMichael Halcrow  * @src: Data to be md5'd
56237fead6SMichael Halcrow  * @len: Length of @src
57237fead6SMichael Halcrow  *
58237fead6SMichael Halcrow  * Uses the allocated crypto context that crypt_stat references to
59237fead6SMichael Halcrow  * generate the MD5 sum of the contents of src.
60237fead6SMichael Halcrow  */
61237fead6SMichael Halcrow static int ecryptfs_calculate_md5(char *dst,
62237fead6SMichael Halcrow 				  struct ecryptfs_crypt_stat *crypt_stat,
63237fead6SMichael Halcrow 				  char *src, int len)
64237fead6SMichael Halcrow {
6519798113SEric Biggers 	int rc = crypto_shash_tfm_digest(crypt_stat->hash_tfm, src, len, dst);
66237fead6SMichael Halcrow 
678a29f2b0SMichael Halcrow 	if (rc) {
688a29f2b0SMichael Halcrow 		printk(KERN_ERR
693095e8e3SHerbert Xu 		       "%s: Error computing crypto hash; rc = [%d]\n",
7018d1dbf1SHarvey Harrison 		       __func__, rc);
718a29f2b0SMichael Halcrow 		goto out;
728a29f2b0SMichael Halcrow 	}
73237fead6SMichael Halcrow out:
74237fead6SMichael Halcrow 	return rc;
75237fead6SMichael Halcrow }
76237fead6SMichael Halcrow 
77cd9d67dfSMichael Halcrow static int ecryptfs_crypto_api_algify_cipher_name(char **algified_name,
788bba066fSMichael Halcrow 						  char *cipher_name,
798bba066fSMichael Halcrow 						  char *chaining_modifier)
808bba066fSMichael Halcrow {
818bba066fSMichael Halcrow 	int cipher_name_len = strlen(cipher_name);
828bba066fSMichael Halcrow 	int chaining_modifier_len = strlen(chaining_modifier);
838bba066fSMichael Halcrow 	int algified_name_len;
848bba066fSMichael Halcrow 	int rc;
858bba066fSMichael Halcrow 
868bba066fSMichael Halcrow 	algified_name_len = (chaining_modifier_len + cipher_name_len + 3);
878bba066fSMichael Halcrow 	(*algified_name) = kmalloc(algified_name_len, GFP_KERNEL);
887bd473fcSMichael Halcrow 	if (!(*algified_name)) {
898bba066fSMichael Halcrow 		rc = -ENOMEM;
908bba066fSMichael Halcrow 		goto out;
918bba066fSMichael Halcrow 	}
928bba066fSMichael Halcrow 	snprintf((*algified_name), algified_name_len, "%s(%s)",
938bba066fSMichael Halcrow 		 chaining_modifier, cipher_name);
948bba066fSMichael Halcrow 	rc = 0;
958bba066fSMichael Halcrow out:
968bba066fSMichael Halcrow 	return rc;
978bba066fSMichael Halcrow }
988bba066fSMichael Halcrow 
99237fead6SMichael Halcrow /**
100237fead6SMichael Halcrow  * ecryptfs_derive_iv
101237fead6SMichael Halcrow  * @iv: destination for the derived iv vale
102237fead6SMichael Halcrow  * @crypt_stat: Pointer to crypt_stat struct for the current inode
103d6a13c17SMichael Halcrow  * @offset: Offset of the extent whose IV we are to derive
104237fead6SMichael Halcrow  *
105237fead6SMichael Halcrow  * Generate the initialization vector from the given root IV and page
106237fead6SMichael Halcrow  * offset.
107237fead6SMichael Halcrow  *
108237fead6SMichael Halcrow  * Returns zero on success; non-zero on error.
109237fead6SMichael Halcrow  */
110a34f60f7SMichael Halcrow int ecryptfs_derive_iv(char *iv, struct ecryptfs_crypt_stat *crypt_stat,
111d6a13c17SMichael Halcrow 		       loff_t offset)
112237fead6SMichael Halcrow {
113237fead6SMichael Halcrow 	int rc = 0;
114237fead6SMichael Halcrow 	char dst[MD5_DIGEST_SIZE];
115237fead6SMichael Halcrow 	char src[ECRYPTFS_MAX_IV_BYTES + 16];
116237fead6SMichael Halcrow 
117237fead6SMichael Halcrow 	if (unlikely(ecryptfs_verbosity > 0)) {
118237fead6SMichael Halcrow 		ecryptfs_printk(KERN_DEBUG, "root iv:\n");
119237fead6SMichael Halcrow 		ecryptfs_dump_hex(crypt_stat->root_iv, crypt_stat->iv_bytes);
120237fead6SMichael Halcrow 	}
121237fead6SMichael Halcrow 	/* TODO: It is probably secure to just cast the least
122237fead6SMichael Halcrow 	 * significant bits of the root IV into an unsigned long and
123237fead6SMichael Halcrow 	 * add the offset to that rather than go through all this
124237fead6SMichael Halcrow 	 * hashing business. -Halcrow */
125237fead6SMichael Halcrow 	memcpy(src, crypt_stat->root_iv, crypt_stat->iv_bytes);
126237fead6SMichael Halcrow 	memset((src + crypt_stat->iv_bytes), 0, 16);
127d6a13c17SMichael Halcrow 	snprintf((src + crypt_stat->iv_bytes), 16, "%lld", offset);
128237fead6SMichael Halcrow 	if (unlikely(ecryptfs_verbosity > 0)) {
129237fead6SMichael Halcrow 		ecryptfs_printk(KERN_DEBUG, "source:\n");
130237fead6SMichael Halcrow 		ecryptfs_dump_hex(src, (crypt_stat->iv_bytes + 16));
131237fead6SMichael Halcrow 	}
132237fead6SMichael Halcrow 	rc = ecryptfs_calculate_md5(dst, crypt_stat, src,
133237fead6SMichael Halcrow 				    (crypt_stat->iv_bytes + 16));
134237fead6SMichael Halcrow 	if (rc) {
135237fead6SMichael Halcrow 		ecryptfs_printk(KERN_WARNING, "Error attempting to compute "
136237fead6SMichael Halcrow 				"MD5 while generating IV for a page\n");
137237fead6SMichael Halcrow 		goto out;
138237fead6SMichael Halcrow 	}
139237fead6SMichael Halcrow 	memcpy(iv, dst, crypt_stat->iv_bytes);
140237fead6SMichael Halcrow 	if (unlikely(ecryptfs_verbosity > 0)) {
141237fead6SMichael Halcrow 		ecryptfs_printk(KERN_DEBUG, "derived iv:\n");
142237fead6SMichael Halcrow 		ecryptfs_dump_hex(iv, crypt_stat->iv_bytes);
143237fead6SMichael Halcrow 	}
144237fead6SMichael Halcrow out:
145237fead6SMichael Halcrow 	return rc;
146237fead6SMichael Halcrow }
147237fead6SMichael Halcrow 
148237fead6SMichael Halcrow /**
149237fead6SMichael Halcrow  * ecryptfs_init_crypt_stat
150237fead6SMichael Halcrow  * @crypt_stat: Pointer to the crypt_stat struct to initialize.
151237fead6SMichael Halcrow  *
152237fead6SMichael Halcrow  * Initialize the crypt_stat structure.
153237fead6SMichael Halcrow  */
154e81f3340SHerbert Xu int ecryptfs_init_crypt_stat(struct ecryptfs_crypt_stat *crypt_stat)
155237fead6SMichael Halcrow {
156e81f3340SHerbert Xu 	struct crypto_shash *tfm;
157e81f3340SHerbert Xu 	int rc;
158e81f3340SHerbert Xu 
159e81f3340SHerbert Xu 	tfm = crypto_alloc_shash(ECRYPTFS_DEFAULT_HASH, 0, 0);
160e81f3340SHerbert Xu 	if (IS_ERR(tfm)) {
161e81f3340SHerbert Xu 		rc = PTR_ERR(tfm);
162e81f3340SHerbert Xu 		ecryptfs_printk(KERN_ERR, "Error attempting to "
163e81f3340SHerbert Xu 				"allocate crypto context; rc = [%d]\n",
164e81f3340SHerbert Xu 				rc);
165e81f3340SHerbert Xu 		return rc;
166e81f3340SHerbert Xu 	}
167e81f3340SHerbert Xu 
168237fead6SMichael Halcrow 	memset((void *)crypt_stat, 0, sizeof(struct ecryptfs_crypt_stat));
169f4aad16aSMichael Halcrow 	INIT_LIST_HEAD(&crypt_stat->keysig_list);
170f4aad16aSMichael Halcrow 	mutex_init(&crypt_stat->keysig_list_mutex);
171237fead6SMichael Halcrow 	mutex_init(&crypt_stat->cs_mutex);
172237fead6SMichael Halcrow 	mutex_init(&crypt_stat->cs_tfm_mutex);
173e81f3340SHerbert Xu 	crypt_stat->hash_tfm = tfm;
174e2bd99ecSMichael Halcrow 	crypt_stat->flags |= ECRYPTFS_STRUCT_INITIALIZED;
175e81f3340SHerbert Xu 
176e81f3340SHerbert Xu 	return 0;
177237fead6SMichael Halcrow }
178237fead6SMichael Halcrow 
179237fead6SMichael Halcrow /**
180fcd12835SMichael Halcrow  * ecryptfs_destroy_crypt_stat
181237fead6SMichael Halcrow  * @crypt_stat: Pointer to the crypt_stat struct to initialize.
182237fead6SMichael Halcrow  *
183237fead6SMichael Halcrow  * Releases all memory associated with a crypt_stat struct.
184237fead6SMichael Halcrow  */
185fcd12835SMichael Halcrow void ecryptfs_destroy_crypt_stat(struct ecryptfs_crypt_stat *crypt_stat)
186237fead6SMichael Halcrow {
187f4aad16aSMichael Halcrow 	struct ecryptfs_key_sig *key_sig, *key_sig_tmp;
188f4aad16aSMichael Halcrow 
1893095e8e3SHerbert Xu 	crypto_free_skcipher(crypt_stat->tfm);
1903095e8e3SHerbert Xu 	crypto_free_shash(crypt_stat->hash_tfm);
191f4aad16aSMichael Halcrow 	list_for_each_entry_safe(key_sig, key_sig_tmp,
192f4aad16aSMichael Halcrow 				 &crypt_stat->keysig_list, crypt_stat_list) {
193f4aad16aSMichael Halcrow 		list_del(&key_sig->crypt_stat_list);
194f4aad16aSMichael Halcrow 		kmem_cache_free(ecryptfs_key_sig_cache, key_sig);
195f4aad16aSMichael Halcrow 	}
196237fead6SMichael Halcrow 	memset(crypt_stat, 0, sizeof(struct ecryptfs_crypt_stat));
197237fead6SMichael Halcrow }
198237fead6SMichael Halcrow 
199fcd12835SMichael Halcrow void ecryptfs_destroy_mount_crypt_stat(
200237fead6SMichael Halcrow 	struct ecryptfs_mount_crypt_stat *mount_crypt_stat)
201237fead6SMichael Halcrow {
202f4aad16aSMichael Halcrow 	struct ecryptfs_global_auth_tok *auth_tok, *auth_tok_tmp;
203f4aad16aSMichael Halcrow 
204f4aad16aSMichael Halcrow 	if (!(mount_crypt_stat->flags & ECRYPTFS_MOUNT_CRYPT_STAT_INITIALIZED))
205f4aad16aSMichael Halcrow 		return;
206f4aad16aSMichael Halcrow 	mutex_lock(&mount_crypt_stat->global_auth_tok_list_mutex);
207f4aad16aSMichael Halcrow 	list_for_each_entry_safe(auth_tok, auth_tok_tmp,
208f4aad16aSMichael Halcrow 				 &mount_crypt_stat->global_auth_tok_list,
209f4aad16aSMichael Halcrow 				 mount_crypt_stat_list) {
210f4aad16aSMichael Halcrow 		list_del(&auth_tok->mount_crypt_stat_list);
2110dad87fcSMarkus Elfring 		if (!(auth_tok->flags & ECRYPTFS_AUTH_TOK_INVALID))
212f4aad16aSMichael Halcrow 			key_put(auth_tok->global_auth_tok_key);
213f4aad16aSMichael Halcrow 		kmem_cache_free(ecryptfs_global_auth_tok_cache, auth_tok);
214f4aad16aSMichael Halcrow 	}
215f4aad16aSMichael Halcrow 	mutex_unlock(&mount_crypt_stat->global_auth_tok_list_mutex);
216237fead6SMichael Halcrow 	memset(mount_crypt_stat, 0, sizeof(struct ecryptfs_mount_crypt_stat));
217237fead6SMichael Halcrow }
218237fead6SMichael Halcrow 
219237fead6SMichael Halcrow /**
220237fead6SMichael Halcrow  * virt_to_scatterlist
221237fead6SMichael Halcrow  * @addr: Virtual address
222237fead6SMichael Halcrow  * @size: Size of data; should be an even multiple of the block size
223237fead6SMichael Halcrow  * @sg: Pointer to scatterlist array; set to NULL to obtain only
224237fead6SMichael Halcrow  *      the number of scatterlist structs required in array
225237fead6SMichael Halcrow  * @sg_size: Max array size
226237fead6SMichael Halcrow  *
227237fead6SMichael Halcrow  * Fills in a scatterlist array with page references for a passed
228237fead6SMichael Halcrow  * virtual address.
229237fead6SMichael Halcrow  *
230237fead6SMichael Halcrow  * Returns the number of scatterlist structs in array used
231237fead6SMichael Halcrow  */
232237fead6SMichael Halcrow int virt_to_scatterlist(const void *addr, int size, struct scatterlist *sg,
233237fead6SMichael Halcrow 			int sg_size)
234237fead6SMichael Halcrow {
235237fead6SMichael Halcrow 	int i = 0;
236237fead6SMichael Halcrow 	struct page *pg;
237237fead6SMichael Halcrow 	int offset;
238237fead6SMichael Halcrow 	int remainder_of_page;
239237fead6SMichael Halcrow 
24068e3f5ddSHerbert Xu 	sg_init_table(sg, sg_size);
24168e3f5ddSHerbert Xu 
242237fead6SMichael Halcrow 	while (size > 0 && i < sg_size) {
243237fead6SMichael Halcrow 		pg = virt_to_page(addr);
244237fead6SMichael Halcrow 		offset = offset_in_page(addr);
245642f1490SJens Axboe 		sg_set_page(&sg[i], pg, 0, offset);
24609cbfeafSKirill A. Shutemov 		remainder_of_page = PAGE_SIZE - offset;
247237fead6SMichael Halcrow 		if (size >= remainder_of_page) {
248237fead6SMichael Halcrow 			sg[i].length = remainder_of_page;
249237fead6SMichael Halcrow 			addr += remainder_of_page;
250237fead6SMichael Halcrow 			size -= remainder_of_page;
251237fead6SMichael Halcrow 		} else {
252237fead6SMichael Halcrow 			sg[i].length = size;
253237fead6SMichael Halcrow 			addr += size;
254237fead6SMichael Halcrow 			size = 0;
255237fead6SMichael Halcrow 		}
256237fead6SMichael Halcrow 		i++;
257237fead6SMichael Halcrow 	}
258237fead6SMichael Halcrow 	if (size > 0)
259237fead6SMichael Halcrow 		return -ENOMEM;
260237fead6SMichael Halcrow 	return i;
261237fead6SMichael Halcrow }
262237fead6SMichael Halcrow 
2634dfea4f0STyler Hicks struct extent_crypt_result {
2644dfea4f0STyler Hicks 	struct completion completion;
2654dfea4f0STyler Hicks 	int rc;
2664dfea4f0STyler Hicks };
2674dfea4f0STyler Hicks 
2684dfea4f0STyler Hicks static void extent_crypt_complete(struct crypto_async_request *req, int rc)
2694dfea4f0STyler Hicks {
2704dfea4f0STyler Hicks 	struct extent_crypt_result *ecr = req->data;
2714dfea4f0STyler Hicks 
2724dfea4f0STyler Hicks 	if (rc == -EINPROGRESS)
2734dfea4f0STyler Hicks 		return;
2744dfea4f0STyler Hicks 
2754dfea4f0STyler Hicks 	ecr->rc = rc;
2764dfea4f0STyler Hicks 	complete(&ecr->completion);
2774dfea4f0STyler Hicks }
2784dfea4f0STyler Hicks 
279237fead6SMichael Halcrow /**
28000a69940STyler Hicks  * crypt_scatterlist
281237fead6SMichael Halcrow  * @crypt_stat: Pointer to the crypt_stat struct to initialize.
2820df5ed65STyler Hicks  * @dst_sg: Destination of the data after performing the crypto operation
28300a69940STyler Hicks  * @src_sg: Data to be encrypted or decrypted
28400a69940STyler Hicks  * @size: Length of data
28500a69940STyler Hicks  * @iv: IV to use
28600a69940STyler Hicks  * @op: ENCRYPT or DECRYPT to indicate the desired operation
287237fead6SMichael Halcrow  *
28800a69940STyler Hicks  * Returns the number of bytes encrypted or decrypted; negative value on error
289237fead6SMichael Halcrow  */
29000a69940STyler Hicks static int crypt_scatterlist(struct ecryptfs_crypt_stat *crypt_stat,
2910df5ed65STyler Hicks 			     struct scatterlist *dst_sg,
292237fead6SMichael Halcrow 			     struct scatterlist *src_sg, int size,
29300a69940STyler Hicks 			     unsigned char *iv, int op)
294237fead6SMichael Halcrow {
2953095e8e3SHerbert Xu 	struct skcipher_request *req = NULL;
2964dfea4f0STyler Hicks 	struct extent_crypt_result ecr;
297237fead6SMichael Halcrow 	int rc = 0;
298237fead6SMichael Halcrow 
299237fead6SMichael Halcrow 	if (unlikely(ecryptfs_verbosity > 0)) {
300f24b3887STyler Hicks 		ecryptfs_printk(KERN_DEBUG, "Key size [%zd]; key:\n",
301237fead6SMichael Halcrow 				crypt_stat->key_size);
302237fead6SMichael Halcrow 		ecryptfs_dump_hex(crypt_stat->key,
303237fead6SMichael Halcrow 				  crypt_stat->key_size);
304237fead6SMichael Halcrow 	}
3054dfea4f0STyler Hicks 
3064dfea4f0STyler Hicks 	init_completion(&ecr.completion);
3074dfea4f0STyler Hicks 
308237fead6SMichael Halcrow 	mutex_lock(&crypt_stat->cs_tfm_mutex);
3093095e8e3SHerbert Xu 	req = skcipher_request_alloc(crypt_stat->tfm, GFP_NOFS);
3104dfea4f0STyler Hicks 	if (!req) {
3114dfea4f0STyler Hicks 		mutex_unlock(&crypt_stat->cs_tfm_mutex);
3124dfea4f0STyler Hicks 		rc = -ENOMEM;
3134dfea4f0STyler Hicks 		goto out;
3148e3a6f16STrevor Highland 	}
3154dfea4f0STyler Hicks 
3163095e8e3SHerbert Xu 	skcipher_request_set_callback(req,
3174dfea4f0STyler Hicks 			CRYPTO_TFM_REQ_MAY_BACKLOG | CRYPTO_TFM_REQ_MAY_SLEEP,
3184dfea4f0STyler Hicks 			extent_crypt_complete, &ecr);
3194dfea4f0STyler Hicks 	/* Consider doing this once, when the file is opened */
3204dfea4f0STyler Hicks 	if (!(crypt_stat->flags & ECRYPTFS_KEY_SET)) {
3213095e8e3SHerbert Xu 		rc = crypto_skcipher_setkey(crypt_stat->tfm, crypt_stat->key,
3224dfea4f0STyler Hicks 					    crypt_stat->key_size);
323237fead6SMichael Halcrow 		if (rc) {
3244dfea4f0STyler Hicks 			ecryptfs_printk(KERN_ERR,
3254dfea4f0STyler Hicks 					"Error setting key; rc = [%d]\n",
326237fead6SMichael Halcrow 					rc);
327237fead6SMichael Halcrow 			mutex_unlock(&crypt_stat->cs_tfm_mutex);
328237fead6SMichael Halcrow 			rc = -EINVAL;
329237fead6SMichael Halcrow 			goto out;
330237fead6SMichael Halcrow 		}
3314dfea4f0STyler Hicks 		crypt_stat->flags |= ECRYPTFS_KEY_SET;
3324dfea4f0STyler Hicks 	}
333237fead6SMichael Halcrow 	mutex_unlock(&crypt_stat->cs_tfm_mutex);
3343095e8e3SHerbert Xu 	skcipher_request_set_crypt(req, src_sg, dst_sg, size, iv);
3353095e8e3SHerbert Xu 	rc = op == ENCRYPT ? crypto_skcipher_encrypt(req) :
3363095e8e3SHerbert Xu 			     crypto_skcipher_decrypt(req);
3374dfea4f0STyler Hicks 	if (rc == -EINPROGRESS || rc == -EBUSY) {
3384dfea4f0STyler Hicks 		struct extent_crypt_result *ecr = req->base.data;
3394dfea4f0STyler Hicks 
3404dfea4f0STyler Hicks 		wait_for_completion(&ecr->completion);
3414dfea4f0STyler Hicks 		rc = ecr->rc;
34216735d02SWolfram Sang 		reinit_completion(&ecr->completion);
3434dfea4f0STyler Hicks 	}
344237fead6SMichael Halcrow out:
3453095e8e3SHerbert Xu 	skcipher_request_free(req);
346237fead6SMichael Halcrow 	return rc;
347237fead6SMichael Halcrow }
348237fead6SMichael Halcrow 
3495da877eaSLee Jones /*
35024d15266STyler Hicks  * lower_offset_for_page
351237fead6SMichael Halcrow  *
3520216f7f7SMichael Halcrow  * Convert an eCryptfs page index into a lower byte offset
353237fead6SMichael Halcrow  */
35424d15266STyler Hicks static loff_t lower_offset_for_page(struct ecryptfs_crypt_stat *crypt_stat,
35524d15266STyler Hicks 				    struct page *page)
356237fead6SMichael Halcrow {
35724d15266STyler Hicks 	return ecryptfs_lower_header_size(crypt_stat) +
35809cbfeafSKirill A. Shutemov 	       ((loff_t)page->index << PAGE_SHIFT);
3590216f7f7SMichael Halcrow }
360237fead6SMichael Halcrow 
3610216f7f7SMichael Halcrow /**
362d78de618STyler Hicks  * crypt_extent
3630216f7f7SMichael Halcrow  * @crypt_stat: crypt_stat containing cryptographic context for the
3640216f7f7SMichael Halcrow  *              encryption operation
3650df5ed65STyler Hicks  * @dst_page: The page to write the result into
366d78de618STyler Hicks  * @src_page: The page to read from
3670216f7f7SMichael Halcrow  * @extent_offset: Page extent offset for use in generating IV
368d78de618STyler Hicks  * @op: ENCRYPT or DECRYPT to indicate the desired operation
3690216f7f7SMichael Halcrow  *
370d78de618STyler Hicks  * Encrypts or decrypts one extent of data.
3710216f7f7SMichael Halcrow  *
3720216f7f7SMichael Halcrow  * Return zero on success; non-zero otherwise
3730216f7f7SMichael Halcrow  */
3740df5ed65STyler Hicks static int crypt_extent(struct ecryptfs_crypt_stat *crypt_stat,
3750df5ed65STyler Hicks 			struct page *dst_page,
376d78de618STyler Hicks 			struct page *src_page,
377d78de618STyler Hicks 			unsigned long extent_offset, int op)
3780216f7f7SMichael Halcrow {
379d78de618STyler Hicks 	pgoff_t page_index = op == ENCRYPT ? src_page->index : dst_page->index;
380d6a13c17SMichael Halcrow 	loff_t extent_base;
3810216f7f7SMichael Halcrow 	char extent_iv[ECRYPTFS_MAX_IV_BYTES];
382406c93dfSTyler Hicks 	struct scatterlist src_sg, dst_sg;
383406c93dfSTyler Hicks 	size_t extent_size = crypt_stat->extent_size;
3840216f7f7SMichael Halcrow 	int rc;
3850216f7f7SMichael Halcrow 
38609cbfeafSKirill A. Shutemov 	extent_base = (((loff_t)page_index) * (PAGE_SIZE / extent_size));
387237fead6SMichael Halcrow 	rc = ecryptfs_derive_iv(extent_iv, crypt_stat,
3880216f7f7SMichael Halcrow 				(extent_base + extent_offset));
389237fead6SMichael Halcrow 	if (rc) {
390888d57bbSJoe Perches 		ecryptfs_printk(KERN_ERR, "Error attempting to derive IV for "
391888d57bbSJoe Perches 			"extent [0x%.16llx]; rc = [%d]\n",
392888d57bbSJoe Perches 			(unsigned long long)(extent_base + extent_offset), rc);
393237fead6SMichael Halcrow 		goto out;
394237fead6SMichael Halcrow 	}
395406c93dfSTyler Hicks 
396406c93dfSTyler Hicks 	sg_init_table(&src_sg, 1);
397406c93dfSTyler Hicks 	sg_init_table(&dst_sg, 1);
398406c93dfSTyler Hicks 
399406c93dfSTyler Hicks 	sg_set_page(&src_sg, src_page, extent_size,
400406c93dfSTyler Hicks 		    extent_offset * extent_size);
401406c93dfSTyler Hicks 	sg_set_page(&dst_sg, dst_page, extent_size,
402406c93dfSTyler Hicks 		    extent_offset * extent_size);
403406c93dfSTyler Hicks 
404406c93dfSTyler Hicks 	rc = crypt_scatterlist(crypt_stat, &dst_sg, &src_sg, extent_size,
405406c93dfSTyler Hicks 			       extent_iv, op);
4060216f7f7SMichael Halcrow 	if (rc < 0) {
407d78de618STyler Hicks 		printk(KERN_ERR "%s: Error attempting to crypt page with "
408d78de618STyler Hicks 		       "page_index = [%ld], extent_offset = [%ld]; "
409d78de618STyler Hicks 		       "rc = [%d]\n", __func__, page_index, extent_offset, rc);
4100216f7f7SMichael Halcrow 		goto out;
4110216f7f7SMichael Halcrow 	}
4120216f7f7SMichael Halcrow 	rc = 0;
4130216f7f7SMichael Halcrow out:
4140216f7f7SMichael Halcrow 	return rc;
4150216f7f7SMichael Halcrow }
4160216f7f7SMichael Halcrow 
4170216f7f7SMichael Halcrow /**
4180216f7f7SMichael Halcrow  * ecryptfs_encrypt_page
4190216f7f7SMichael Halcrow  * @page: Page mapped from the eCryptfs inode for the file; contains
4200216f7f7SMichael Halcrow  *        decrypted content that needs to be encrypted (to a temporary
4210216f7f7SMichael Halcrow  *        page; not in place) and written out to the lower file
4220216f7f7SMichael Halcrow  *
4230216f7f7SMichael Halcrow  * Encrypt an eCryptfs page. This is done on a per-extent basis. Note
4240216f7f7SMichael Halcrow  * that eCryptfs pages may straddle the lower pages -- for instance,
4250216f7f7SMichael Halcrow  * if the file was created on a machine with an 8K page size
4260216f7f7SMichael Halcrow  * (resulting in an 8K header), and then the file is copied onto a
4270216f7f7SMichael Halcrow  * host with a 32K page size, then when reading page 0 of the eCryptfs
4280216f7f7SMichael Halcrow  * file, 24K of page 0 of the lower file will be read and decrypted,
4290216f7f7SMichael Halcrow  * and then 8K of page 1 of the lower file will be read and decrypted.
4300216f7f7SMichael Halcrow  *
4310216f7f7SMichael Halcrow  * Returns zero on success; negative on error
4320216f7f7SMichael Halcrow  */
4330216f7f7SMichael Halcrow int ecryptfs_encrypt_page(struct page *page)
4340216f7f7SMichael Halcrow {
4350216f7f7SMichael Halcrow 	struct inode *ecryptfs_inode;
4360216f7f7SMichael Halcrow 	struct ecryptfs_crypt_stat *crypt_stat;
4377fcba054SEric Sandeen 	char *enc_extent_virt;
4387fcba054SEric Sandeen 	struct page *enc_extent_page = NULL;
4390216f7f7SMichael Halcrow 	loff_t extent_offset;
4400f896176STyler Hicks 	loff_t lower_offset;
4410216f7f7SMichael Halcrow 	int rc = 0;
4420216f7f7SMichael Halcrow 
4430216f7f7SMichael Halcrow 	ecryptfs_inode = page->mapping->host;
4440216f7f7SMichael Halcrow 	crypt_stat =
4450216f7f7SMichael Halcrow 		&(ecryptfs_inode_to_private(ecryptfs_inode)->crypt_stat);
44613a791b4STyler Hicks 	BUG_ON(!(crypt_stat->flags & ECRYPTFS_ENCRYPTED));
4477fcba054SEric Sandeen 	enc_extent_page = alloc_page(GFP_USER);
4487fcba054SEric Sandeen 	if (!enc_extent_page) {
4490216f7f7SMichael Halcrow 		rc = -ENOMEM;
4500216f7f7SMichael Halcrow 		ecryptfs_printk(KERN_ERR, "Error allocating memory for "
4510216f7f7SMichael Halcrow 				"encrypted extent\n");
4520216f7f7SMichael Halcrow 		goto out;
4530216f7f7SMichael Halcrow 	}
4540f896176STyler Hicks 
4550216f7f7SMichael Halcrow 	for (extent_offset = 0;
45609cbfeafSKirill A. Shutemov 	     extent_offset < (PAGE_SIZE / crypt_stat->extent_size);
4570216f7f7SMichael Halcrow 	     extent_offset++) {
4580df5ed65STyler Hicks 		rc = crypt_extent(crypt_stat, enc_extent_page, page,
459d78de618STyler Hicks 				  extent_offset, ENCRYPT);
4600216f7f7SMichael Halcrow 		if (rc) {
4610216f7f7SMichael Halcrow 			printk(KERN_ERR "%s: Error encrypting extent; "
46218d1dbf1SHarvey Harrison 			       "rc = [%d]\n", __func__, rc);
4630216f7f7SMichael Halcrow 			goto out;
4640216f7f7SMichael Halcrow 		}
4650216f7f7SMichael Halcrow 	}
4660f896176STyler Hicks 
46724d15266STyler Hicks 	lower_offset = lower_offset_for_page(crypt_stat, page);
4680f896176STyler Hicks 	enc_extent_virt = kmap(enc_extent_page);
4690f896176STyler Hicks 	rc = ecryptfs_write_lower(ecryptfs_inode, enc_extent_virt, lower_offset,
47009cbfeafSKirill A. Shutemov 				  PAGE_SIZE);
4717fcba054SEric Sandeen 	kunmap(enc_extent_page);
4720216f7f7SMichael Halcrow 	if (rc < 0) {
4730f896176STyler Hicks 		ecryptfs_printk(KERN_ERR,
4740f896176STyler Hicks 			"Error attempting to write lower page; rc = [%d]\n",
4750216f7f7SMichael Halcrow 			rc);
4760216f7f7SMichael Halcrow 		goto out;
4770216f7f7SMichael Halcrow 	}
4780216f7f7SMichael Halcrow 	rc = 0;
479237fead6SMichael Halcrow out:
480237fead6SMichael Halcrow 	if (enc_extent_page) {
481237fead6SMichael Halcrow 		__free_page(enc_extent_page);
482237fead6SMichael Halcrow 	}
483237fead6SMichael Halcrow 	return rc;
484237fead6SMichael Halcrow }
485237fead6SMichael Halcrow 
486237fead6SMichael Halcrow /**
487237fead6SMichael Halcrow  * ecryptfs_decrypt_page
4880216f7f7SMichael Halcrow  * @page: Page mapped from the eCryptfs inode for the file; data read
4890216f7f7SMichael Halcrow  *        and decrypted from the lower file will be written into this
4900216f7f7SMichael Halcrow  *        page
491237fead6SMichael Halcrow  *
492237fead6SMichael Halcrow  * Decrypt an eCryptfs page. This is done on a per-extent basis. Note
493237fead6SMichael Halcrow  * that eCryptfs pages may straddle the lower pages -- for instance,
494237fead6SMichael Halcrow  * if the file was created on a machine with an 8K page size
495237fead6SMichael Halcrow  * (resulting in an 8K header), and then the file is copied onto a
496237fead6SMichael Halcrow  * host with a 32K page size, then when reading page 0 of the eCryptfs
497237fead6SMichael Halcrow  * file, 24K of page 0 of the lower file will be read and decrypted,
498237fead6SMichael Halcrow  * and then 8K of page 1 of the lower file will be read and decrypted.
499237fead6SMichael Halcrow  *
500237fead6SMichael Halcrow  * Returns zero on success; negative on error
501237fead6SMichael Halcrow  */
5020216f7f7SMichael Halcrow int ecryptfs_decrypt_page(struct page *page)
503237fead6SMichael Halcrow {
5040216f7f7SMichael Halcrow 	struct inode *ecryptfs_inode;
505237fead6SMichael Halcrow 	struct ecryptfs_crypt_stat *crypt_stat;
5069c6043f4STyler Hicks 	char *page_virt;
5070216f7f7SMichael Halcrow 	unsigned long extent_offset;
5080f896176STyler Hicks 	loff_t lower_offset;
509237fead6SMichael Halcrow 	int rc = 0;
510237fead6SMichael Halcrow 
5110216f7f7SMichael Halcrow 	ecryptfs_inode = page->mapping->host;
5120216f7f7SMichael Halcrow 	crypt_stat =
5130216f7f7SMichael Halcrow 		&(ecryptfs_inode_to_private(ecryptfs_inode)->crypt_stat);
51413a791b4STyler Hicks 	BUG_ON(!(crypt_stat->flags & ECRYPTFS_ENCRYPTED));
5150f896176STyler Hicks 
51624d15266STyler Hicks 	lower_offset = lower_offset_for_page(crypt_stat, page);
5179c6043f4STyler Hicks 	page_virt = kmap(page);
51809cbfeafSKirill A. Shutemov 	rc = ecryptfs_read_lower(page_virt, lower_offset, PAGE_SIZE,
5190f896176STyler Hicks 				 ecryptfs_inode);
5209c6043f4STyler Hicks 	kunmap(page);
5210f896176STyler Hicks 	if (rc < 0) {
5220f896176STyler Hicks 		ecryptfs_printk(KERN_ERR,
5230f896176STyler Hicks 			"Error attempting to read lower page; rc = [%d]\n",
5240f896176STyler Hicks 			rc);
52516a72c45SMichael Halcrow 		goto out;
526237fead6SMichael Halcrow 	}
5270f896176STyler Hicks 
5280216f7f7SMichael Halcrow 	for (extent_offset = 0;
52909cbfeafSKirill A. Shutemov 	     extent_offset < (PAGE_SIZE / crypt_stat->extent_size);
5300216f7f7SMichael Halcrow 	     extent_offset++) {
5310df5ed65STyler Hicks 		rc = crypt_extent(crypt_stat, page, page,
532d78de618STyler Hicks 				  extent_offset, DECRYPT);
5330216f7f7SMichael Halcrow 		if (rc) {
5341abbe110SSascha Hauer 			printk(KERN_ERR "%s: Error decrypting extent; "
53518d1dbf1SHarvey Harrison 			       "rc = [%d]\n", __func__, rc);
53616a72c45SMichael Halcrow 			goto out;
537237fead6SMichael Halcrow 		}
538237fead6SMichael Halcrow 	}
539237fead6SMichael Halcrow out:
540237fead6SMichael Halcrow 	return rc;
541237fead6SMichael Halcrow }
542237fead6SMichael Halcrow 
543237fead6SMichael Halcrow #define ECRYPTFS_MAX_SCATTERLIST_LEN 4
544237fead6SMichael Halcrow 
545237fead6SMichael Halcrow /**
546237fead6SMichael Halcrow  * ecryptfs_init_crypt_ctx
547421f91d2SUwe Kleine-König  * @crypt_stat: Uninitialized crypt stats structure
548237fead6SMichael Halcrow  *
549237fead6SMichael Halcrow  * Initialize the crypto context.
550237fead6SMichael Halcrow  *
551237fead6SMichael Halcrow  * TODO: Performance: Keep a cache of initialized cipher contexts;
552237fead6SMichael Halcrow  * only init if needed
553237fead6SMichael Halcrow  */
554237fead6SMichael Halcrow int ecryptfs_init_crypt_ctx(struct ecryptfs_crypt_stat *crypt_stat)
555237fead6SMichael Halcrow {
5568bba066fSMichael Halcrow 	char *full_alg_name;
557237fead6SMichael Halcrow 	int rc = -EINVAL;
558237fead6SMichael Halcrow 
559237fead6SMichael Halcrow 	ecryptfs_printk(KERN_DEBUG,
560237fead6SMichael Halcrow 			"Initializing cipher [%s]; strlen = [%d]; "
561f24b3887STyler Hicks 			"key_size_bits = [%zd]\n",
562237fead6SMichael Halcrow 			crypt_stat->cipher, (int)strlen(crypt_stat->cipher),
563237fead6SMichael Halcrow 			crypt_stat->key_size << 3);
564cb69f36bSKees Cook 	mutex_lock(&crypt_stat->cs_tfm_mutex);
565237fead6SMichael Halcrow 	if (crypt_stat->tfm) {
566237fead6SMichael Halcrow 		rc = 0;
567cb69f36bSKees Cook 		goto out_unlock;
568237fead6SMichael Halcrow 	}
5698bba066fSMichael Halcrow 	rc = ecryptfs_crypto_api_algify_cipher_name(&full_alg_name,
5708bba066fSMichael Halcrow 						    crypt_stat->cipher, "cbc");
5718bba066fSMichael Halcrow 	if (rc)
572c8161f64SEric Sandeen 		goto out_unlock;
5733095e8e3SHerbert Xu 	crypt_stat->tfm = crypto_alloc_skcipher(full_alg_name, 0, 0);
574de88777eSAkinobu Mita 	if (IS_ERR(crypt_stat->tfm)) {
575de88777eSAkinobu Mita 		rc = PTR_ERR(crypt_stat->tfm);
576b0105eaeSTyler Hicks 		crypt_stat->tfm = NULL;
577237fead6SMichael Halcrow 		ecryptfs_printk(KERN_ERR, "cryptfs: init_crypt_ctx(): "
578237fead6SMichael Halcrow 				"Error initializing cipher [%s]\n",
579cb69f36bSKees Cook 				full_alg_name);
580cb69f36bSKees Cook 		goto out_free;
581237fead6SMichael Halcrow 	}
582231baecdSEric Biggers 	crypto_skcipher_set_flags(crypt_stat->tfm,
583231baecdSEric Biggers 				  CRYPTO_TFM_REQ_FORBID_WEAK_KEYS);
584237fead6SMichael Halcrow 	rc = 0;
585cb69f36bSKees Cook out_free:
586cb69f36bSKees Cook 	kfree(full_alg_name);
587c8161f64SEric Sandeen out_unlock:
588c8161f64SEric Sandeen 	mutex_unlock(&crypt_stat->cs_tfm_mutex);
589237fead6SMichael Halcrow 	return rc;
590237fead6SMichael Halcrow }
591237fead6SMichael Halcrow 
592237fead6SMichael Halcrow static void set_extent_mask_and_shift(struct ecryptfs_crypt_stat *crypt_stat)
593237fead6SMichael Halcrow {
594237fead6SMichael Halcrow 	int extent_size_tmp;
595237fead6SMichael Halcrow 
596237fead6SMichael Halcrow 	crypt_stat->extent_mask = 0xFFFFFFFF;
597237fead6SMichael Halcrow 	crypt_stat->extent_shift = 0;
598237fead6SMichael Halcrow 	if (crypt_stat->extent_size == 0)
599237fead6SMichael Halcrow 		return;
600237fead6SMichael Halcrow 	extent_size_tmp = crypt_stat->extent_size;
601237fead6SMichael Halcrow 	while ((extent_size_tmp & 0x01) == 0) {
602237fead6SMichael Halcrow 		extent_size_tmp >>= 1;
603237fead6SMichael Halcrow 		crypt_stat->extent_mask <<= 1;
604237fead6SMichael Halcrow 		crypt_stat->extent_shift++;
605237fead6SMichael Halcrow 	}
606237fead6SMichael Halcrow }
607237fead6SMichael Halcrow 
608237fead6SMichael Halcrow void ecryptfs_set_default_sizes(struct ecryptfs_crypt_stat *crypt_stat)
609237fead6SMichael Halcrow {
610237fead6SMichael Halcrow 	/* Default values; may be overwritten as we are parsing the
611237fead6SMichael Halcrow 	 * packets. */
612237fead6SMichael Halcrow 	crypt_stat->extent_size = ECRYPTFS_DEFAULT_EXTENT_SIZE;
613237fead6SMichael Halcrow 	set_extent_mask_and_shift(crypt_stat);
614237fead6SMichael Halcrow 	crypt_stat->iv_bytes = ECRYPTFS_DEFAULT_IV_BYTES;
615dd2a3b7aSMichael Halcrow 	if (crypt_stat->flags & ECRYPTFS_METADATA_IN_XATTR)
616fa3ef1cbSTyler Hicks 		crypt_stat->metadata_size = ECRYPTFS_MINIMUM_HEADER_EXTENT_SIZE;
61745eaab79SMichael Halcrow 	else {
61809cbfeafSKirill A. Shutemov 		if (PAGE_SIZE <= ECRYPTFS_MINIMUM_HEADER_EXTENT_SIZE)
619fa3ef1cbSTyler Hicks 			crypt_stat->metadata_size =
620cc11beffSMichael Halcrow 				ECRYPTFS_MINIMUM_HEADER_EXTENT_SIZE;
621dd2a3b7aSMichael Halcrow 		else
62209cbfeafSKirill A. Shutemov 			crypt_stat->metadata_size = PAGE_SIZE;
62345eaab79SMichael Halcrow 	}
624237fead6SMichael Halcrow }
625237fead6SMichael Halcrow 
6265da877eaSLee Jones /*
627237fead6SMichael Halcrow  * ecryptfs_compute_root_iv
628237fead6SMichael Halcrow  *
629237fead6SMichael Halcrow  * On error, sets the root IV to all 0's.
630237fead6SMichael Halcrow  */
631237fead6SMichael Halcrow int ecryptfs_compute_root_iv(struct ecryptfs_crypt_stat *crypt_stat)
632237fead6SMichael Halcrow {
633237fead6SMichael Halcrow 	int rc = 0;
634237fead6SMichael Halcrow 	char dst[MD5_DIGEST_SIZE];
635237fead6SMichael Halcrow 
636237fead6SMichael Halcrow 	BUG_ON(crypt_stat->iv_bytes > MD5_DIGEST_SIZE);
637237fead6SMichael Halcrow 	BUG_ON(crypt_stat->iv_bytes <= 0);
638e2bd99ecSMichael Halcrow 	if (!(crypt_stat->flags & ECRYPTFS_KEY_VALID)) {
639237fead6SMichael Halcrow 		rc = -EINVAL;
640237fead6SMichael Halcrow 		ecryptfs_printk(KERN_WARNING, "Session key not valid; "
641237fead6SMichael Halcrow 				"cannot generate root IV\n");
642237fead6SMichael Halcrow 		goto out;
643237fead6SMichael Halcrow 	}
644237fead6SMichael Halcrow 	rc = ecryptfs_calculate_md5(dst, crypt_stat, crypt_stat->key,
645237fead6SMichael Halcrow 				    crypt_stat->key_size);
646237fead6SMichael Halcrow 	if (rc) {
647237fead6SMichael Halcrow 		ecryptfs_printk(KERN_WARNING, "Error attempting to compute "
648237fead6SMichael Halcrow 				"MD5 while generating root IV\n");
649237fead6SMichael Halcrow 		goto out;
650237fead6SMichael Halcrow 	}
651237fead6SMichael Halcrow 	memcpy(crypt_stat->root_iv, dst, crypt_stat->iv_bytes);
652237fead6SMichael Halcrow out:
653237fead6SMichael Halcrow 	if (rc) {
654237fead6SMichael Halcrow 		memset(crypt_stat->root_iv, 0, crypt_stat->iv_bytes);
655e2bd99ecSMichael Halcrow 		crypt_stat->flags |= ECRYPTFS_SECURITY_WARNING;
656237fead6SMichael Halcrow 	}
657237fead6SMichael Halcrow 	return rc;
658237fead6SMichael Halcrow }
659237fead6SMichael Halcrow 
660237fead6SMichael Halcrow static void ecryptfs_generate_new_key(struct ecryptfs_crypt_stat *crypt_stat)
661237fead6SMichael Halcrow {
662237fead6SMichael Halcrow 	get_random_bytes(crypt_stat->key, crypt_stat->key_size);
663e2bd99ecSMichael Halcrow 	crypt_stat->flags |= ECRYPTFS_KEY_VALID;
664237fead6SMichael Halcrow 	ecryptfs_compute_root_iv(crypt_stat);
665237fead6SMichael Halcrow 	if (unlikely(ecryptfs_verbosity > 0)) {
666237fead6SMichael Halcrow 		ecryptfs_printk(KERN_DEBUG, "Generated new session key:\n");
667237fead6SMichael Halcrow 		ecryptfs_dump_hex(crypt_stat->key,
668237fead6SMichael Halcrow 				  crypt_stat->key_size);
669237fead6SMichael Halcrow 	}
670237fead6SMichael Halcrow }
671237fead6SMichael Halcrow 
672237fead6SMichael Halcrow /**
67317398957SMichael Halcrow  * ecryptfs_copy_mount_wide_flags_to_inode_flags
67422e78fafSMichael Halcrow  * @crypt_stat: The inode's cryptographic context
67522e78fafSMichael Halcrow  * @mount_crypt_stat: The mount point's cryptographic context
67617398957SMichael Halcrow  *
67717398957SMichael Halcrow  * This function propagates the mount-wide flags to individual inode
67817398957SMichael Halcrow  * flags.
67917398957SMichael Halcrow  */
68017398957SMichael Halcrow static void ecryptfs_copy_mount_wide_flags_to_inode_flags(
68117398957SMichael Halcrow 	struct ecryptfs_crypt_stat *crypt_stat,
68217398957SMichael Halcrow 	struct ecryptfs_mount_crypt_stat *mount_crypt_stat)
68317398957SMichael Halcrow {
68417398957SMichael Halcrow 	if (mount_crypt_stat->flags & ECRYPTFS_XATTR_METADATA_ENABLED)
68517398957SMichael Halcrow 		crypt_stat->flags |= ECRYPTFS_METADATA_IN_XATTR;
68617398957SMichael Halcrow 	if (mount_crypt_stat->flags & ECRYPTFS_ENCRYPTED_VIEW_ENABLED)
68717398957SMichael Halcrow 		crypt_stat->flags |= ECRYPTFS_VIEW_AS_ENCRYPTED;
688addd65adSMichael Halcrow 	if (mount_crypt_stat->flags & ECRYPTFS_GLOBAL_ENCRYPT_FILENAMES) {
689addd65adSMichael Halcrow 		crypt_stat->flags |= ECRYPTFS_ENCRYPT_FILENAMES;
690addd65adSMichael Halcrow 		if (mount_crypt_stat->flags
691addd65adSMichael Halcrow 		    & ECRYPTFS_GLOBAL_ENCFN_USE_MOUNT_FNEK)
692addd65adSMichael Halcrow 			crypt_stat->flags |= ECRYPTFS_ENCFN_USE_MOUNT_FNEK;
693addd65adSMichael Halcrow 		else if (mount_crypt_stat->flags
694addd65adSMichael Halcrow 			 & ECRYPTFS_GLOBAL_ENCFN_USE_FEK)
695addd65adSMichael Halcrow 			crypt_stat->flags |= ECRYPTFS_ENCFN_USE_FEK;
696addd65adSMichael Halcrow 	}
69717398957SMichael Halcrow }
69817398957SMichael Halcrow 
699f4aad16aSMichael Halcrow static int ecryptfs_copy_mount_wide_sigs_to_inode_sigs(
700f4aad16aSMichael Halcrow 	struct ecryptfs_crypt_stat *crypt_stat,
701f4aad16aSMichael Halcrow 	struct ecryptfs_mount_crypt_stat *mount_crypt_stat)
702f4aad16aSMichael Halcrow {
703f4aad16aSMichael Halcrow 	struct ecryptfs_global_auth_tok *global_auth_tok;
704f4aad16aSMichael Halcrow 	int rc = 0;
705f4aad16aSMichael Halcrow 
706aa06117fSRoland Dreier 	mutex_lock(&crypt_stat->keysig_list_mutex);
707f4aad16aSMichael Halcrow 	mutex_lock(&mount_crypt_stat->global_auth_tok_list_mutex);
708aa06117fSRoland Dreier 
709f4aad16aSMichael Halcrow 	list_for_each_entry(global_auth_tok,
710f4aad16aSMichael Halcrow 			    &mount_crypt_stat->global_auth_tok_list,
711f4aad16aSMichael Halcrow 			    mount_crypt_stat_list) {
71284814d64STyler Hicks 		if (global_auth_tok->flags & ECRYPTFS_AUTH_TOK_FNEK)
71384814d64STyler Hicks 			continue;
714f4aad16aSMichael Halcrow 		rc = ecryptfs_add_keysig(crypt_stat, global_auth_tok->sig);
715f4aad16aSMichael Halcrow 		if (rc) {
716f4aad16aSMichael Halcrow 			printk(KERN_ERR "Error adding keysig; rc = [%d]\n", rc);
717f4aad16aSMichael Halcrow 			goto out;
718f4aad16aSMichael Halcrow 		}
719f4aad16aSMichael Halcrow 	}
720aa06117fSRoland Dreier 
721f4aad16aSMichael Halcrow out:
722aa06117fSRoland Dreier 	mutex_unlock(&mount_crypt_stat->global_auth_tok_list_mutex);
723aa06117fSRoland Dreier 	mutex_unlock(&crypt_stat->keysig_list_mutex);
724f4aad16aSMichael Halcrow 	return rc;
725f4aad16aSMichael Halcrow }
726f4aad16aSMichael Halcrow 
72717398957SMichael Halcrow /**
728237fead6SMichael Halcrow  * ecryptfs_set_default_crypt_stat_vals
72922e78fafSMichael Halcrow  * @crypt_stat: The inode's cryptographic context
73022e78fafSMichael Halcrow  * @mount_crypt_stat: The mount point's cryptographic context
731237fead6SMichael Halcrow  *
732237fead6SMichael Halcrow  * Default values in the event that policy does not override them.
733237fead6SMichael Halcrow  */
734237fead6SMichael Halcrow static void ecryptfs_set_default_crypt_stat_vals(
735237fead6SMichael Halcrow 	struct ecryptfs_crypt_stat *crypt_stat,
736237fead6SMichael Halcrow 	struct ecryptfs_mount_crypt_stat *mount_crypt_stat)
737237fead6SMichael Halcrow {
73817398957SMichael Halcrow 	ecryptfs_copy_mount_wide_flags_to_inode_flags(crypt_stat,
73917398957SMichael Halcrow 						      mount_crypt_stat);
740237fead6SMichael Halcrow 	ecryptfs_set_default_sizes(crypt_stat);
741237fead6SMichael Halcrow 	strcpy(crypt_stat->cipher, ECRYPTFS_DEFAULT_CIPHER);
742237fead6SMichael Halcrow 	crypt_stat->key_size = ECRYPTFS_DEFAULT_KEY_BYTES;
743e2bd99ecSMichael Halcrow 	crypt_stat->flags &= ~(ECRYPTFS_KEY_VALID);
744237fead6SMichael Halcrow 	crypt_stat->file_version = ECRYPTFS_FILE_VERSION;
745237fead6SMichael Halcrow 	crypt_stat->mount_crypt_stat = mount_crypt_stat;
746237fead6SMichael Halcrow }
747237fead6SMichael Halcrow 
748237fead6SMichael Halcrow /**
749237fead6SMichael Halcrow  * ecryptfs_new_file_context
750b59db43aSTyler Hicks  * @ecryptfs_inode: The eCryptfs inode
751237fead6SMichael Halcrow  *
752237fead6SMichael Halcrow  * If the crypto context for the file has not yet been established,
753237fead6SMichael Halcrow  * this is where we do that.  Establishing a new crypto context
754237fead6SMichael Halcrow  * involves the following decisions:
755237fead6SMichael Halcrow  *  - What cipher to use?
756237fead6SMichael Halcrow  *  - What set of authentication tokens to use?
757237fead6SMichael Halcrow  * Here we just worry about getting enough information into the
758237fead6SMichael Halcrow  * authentication tokens so that we know that they are available.
759237fead6SMichael Halcrow  * We associate the available authentication tokens with the new file
760237fead6SMichael Halcrow  * via the set of signatures in the crypt_stat struct.  Later, when
761237fead6SMichael Halcrow  * the headers are actually written out, we may again defer to
762237fead6SMichael Halcrow  * userspace to perform the encryption of the session key; for the
763237fead6SMichael Halcrow  * foreseeable future, this will be the case with public key packets.
764237fead6SMichael Halcrow  *
765237fead6SMichael Halcrow  * Returns zero on success; non-zero otherwise
766237fead6SMichael Halcrow  */
767b59db43aSTyler Hicks int ecryptfs_new_file_context(struct inode *ecryptfs_inode)
768237fead6SMichael Halcrow {
769237fead6SMichael Halcrow 	struct ecryptfs_crypt_stat *crypt_stat =
770b59db43aSTyler Hicks 	    &ecryptfs_inode_to_private(ecryptfs_inode)->crypt_stat;
771237fead6SMichael Halcrow 	struct ecryptfs_mount_crypt_stat *mount_crypt_stat =
772237fead6SMichael Halcrow 	    &ecryptfs_superblock_to_private(
773b59db43aSTyler Hicks 		    ecryptfs_inode->i_sb)->mount_crypt_stat;
774237fead6SMichael Halcrow 	int cipher_name_len;
775f4aad16aSMichael Halcrow 	int rc = 0;
776237fead6SMichael Halcrow 
777237fead6SMichael Halcrow 	ecryptfs_set_default_crypt_stat_vals(crypt_stat, mount_crypt_stat);
778af655dc6SMichael Halcrow 	crypt_stat->flags |= (ECRYPTFS_ENCRYPTED | ECRYPTFS_KEY_VALID);
77917398957SMichael Halcrow 	ecryptfs_copy_mount_wide_flags_to_inode_flags(crypt_stat,
78017398957SMichael Halcrow 						      mount_crypt_stat);
781f4aad16aSMichael Halcrow 	rc = ecryptfs_copy_mount_wide_sigs_to_inode_sigs(crypt_stat,
782f4aad16aSMichael Halcrow 							 mount_crypt_stat);
783f4aad16aSMichael Halcrow 	if (rc) {
784f4aad16aSMichael Halcrow 		printk(KERN_ERR "Error attempting to copy mount-wide key sigs "
785f4aad16aSMichael Halcrow 		       "to the inode key sigs; rc = [%d]\n", rc);
786f4aad16aSMichael Halcrow 		goto out;
787f4aad16aSMichael Halcrow 	}
788237fead6SMichael Halcrow 	cipher_name_len =
789237fead6SMichael Halcrow 		strlen(mount_crypt_stat->global_default_cipher_name);
790237fead6SMichael Halcrow 	memcpy(crypt_stat->cipher,
791237fead6SMichael Halcrow 	       mount_crypt_stat->global_default_cipher_name,
792237fead6SMichael Halcrow 	       cipher_name_len);
793237fead6SMichael Halcrow 	crypt_stat->cipher[cipher_name_len] = '\0';
794237fead6SMichael Halcrow 	crypt_stat->key_size =
795237fead6SMichael Halcrow 		mount_crypt_stat->global_default_cipher_key_size;
796237fead6SMichael Halcrow 	ecryptfs_generate_new_key(crypt_stat);
797237fead6SMichael Halcrow 	rc = ecryptfs_init_crypt_ctx(crypt_stat);
798237fead6SMichael Halcrow 	if (rc)
799237fead6SMichael Halcrow 		ecryptfs_printk(KERN_ERR, "Error initializing cryptographic "
800237fead6SMichael Halcrow 				"context for cipher [%s]: rc = [%d]\n",
801237fead6SMichael Halcrow 				crypt_stat->cipher, rc);
802f4aad16aSMichael Halcrow out:
803237fead6SMichael Halcrow 	return rc;
804237fead6SMichael Halcrow }
805237fead6SMichael Halcrow 
806237fead6SMichael Halcrow /**
8077a86617eSTyler Hicks  * ecryptfs_validate_marker - check for the ecryptfs marker
808237fead6SMichael Halcrow  * @data: The data block in which to check
809237fead6SMichael Halcrow  *
8107a86617eSTyler Hicks  * Returns zero if marker found; -EINVAL if not found
811237fead6SMichael Halcrow  */
8127a86617eSTyler Hicks static int ecryptfs_validate_marker(char *data)
813237fead6SMichael Halcrow {
814237fead6SMichael Halcrow 	u32 m_1, m_2;
815237fead6SMichael Halcrow 
81629335c6aSHarvey Harrison 	m_1 = get_unaligned_be32(data);
81729335c6aSHarvey Harrison 	m_2 = get_unaligned_be32(data + 4);
818237fead6SMichael Halcrow 	if ((m_1 ^ MAGIC_ECRYPTFS_MARKER) == m_2)
8197a86617eSTyler Hicks 		return 0;
820237fead6SMichael Halcrow 	ecryptfs_printk(KERN_DEBUG, "m_1 = [0x%.8x]; m_2 = [0x%.8x]; "
821237fead6SMichael Halcrow 			"MAGIC_ECRYPTFS_MARKER = [0x%.8x]\n", m_1, m_2,
822237fead6SMichael Halcrow 			MAGIC_ECRYPTFS_MARKER);
823237fead6SMichael Halcrow 	ecryptfs_printk(KERN_DEBUG, "(m_1 ^ MAGIC_ECRYPTFS_MARKER) = "
824237fead6SMichael Halcrow 			"[0x%.8x]\n", (m_1 ^ MAGIC_ECRYPTFS_MARKER));
8257a86617eSTyler Hicks 	return -EINVAL;
826237fead6SMichael Halcrow }
827237fead6SMichael Halcrow 
828237fead6SMichael Halcrow struct ecryptfs_flag_map_elem {
829237fead6SMichael Halcrow 	u32 file_flag;
830237fead6SMichael Halcrow 	u32 local_flag;
831237fead6SMichael Halcrow };
832237fead6SMichael Halcrow 
833237fead6SMichael Halcrow /* Add support for additional flags by adding elements here. */
834237fead6SMichael Halcrow static struct ecryptfs_flag_map_elem ecryptfs_flag_map[] = {
835237fead6SMichael Halcrow 	{0x00000001, ECRYPTFS_ENABLE_HMAC},
836dd2a3b7aSMichael Halcrow 	{0x00000002, ECRYPTFS_ENCRYPTED},
837addd65adSMichael Halcrow 	{0x00000004, ECRYPTFS_METADATA_IN_XATTR},
838addd65adSMichael Halcrow 	{0x00000008, ECRYPTFS_ENCRYPT_FILENAMES}
839237fead6SMichael Halcrow };
840237fead6SMichael Halcrow 
841237fead6SMichael Halcrow /**
842237fead6SMichael Halcrow  * ecryptfs_process_flags
84322e78fafSMichael Halcrow  * @crypt_stat: The cryptographic context
844237fead6SMichael Halcrow  * @page_virt: Source data to be parsed
845237fead6SMichael Halcrow  * @bytes_read: Updated with the number of bytes read
846237fead6SMichael Halcrow  */
8477451c54aSHariprasad Kelam static void ecryptfs_process_flags(struct ecryptfs_crypt_stat *crypt_stat,
848237fead6SMichael Halcrow 				  char *page_virt, int *bytes_read)
849237fead6SMichael Halcrow {
850237fead6SMichael Halcrow 	int i;
851237fead6SMichael Halcrow 	u32 flags;
852237fead6SMichael Halcrow 
85329335c6aSHarvey Harrison 	flags = get_unaligned_be32(page_virt);
85402f9876eSJérémy Lefaure 	for (i = 0; i < ARRAY_SIZE(ecryptfs_flag_map); i++)
855237fead6SMichael Halcrow 		if (flags & ecryptfs_flag_map[i].file_flag) {
856e2bd99ecSMichael Halcrow 			crypt_stat->flags |= ecryptfs_flag_map[i].local_flag;
857237fead6SMichael Halcrow 		} else
858e2bd99ecSMichael Halcrow 			crypt_stat->flags &= ~(ecryptfs_flag_map[i].local_flag);
859237fead6SMichael Halcrow 	/* Version is in top 8 bits of the 32-bit flag vector */
860237fead6SMichael Halcrow 	crypt_stat->file_version = ((flags >> 24) & 0xFF);
861237fead6SMichael Halcrow 	(*bytes_read) = 4;
862237fead6SMichael Halcrow }
863237fead6SMichael Halcrow 
864237fead6SMichael Halcrow /**
865237fead6SMichael Halcrow  * write_ecryptfs_marker
866237fead6SMichael Halcrow  * @page_virt: The pointer to in a page to begin writing the marker
867237fead6SMichael Halcrow  * @written: Number of bytes written
868237fead6SMichael Halcrow  *
869237fead6SMichael Halcrow  * Marker = 0x3c81b7f5
870237fead6SMichael Halcrow  */
871237fead6SMichael Halcrow static void write_ecryptfs_marker(char *page_virt, size_t *written)
872237fead6SMichael Halcrow {
873237fead6SMichael Halcrow 	u32 m_1, m_2;
874237fead6SMichael Halcrow 
875237fead6SMichael Halcrow 	get_random_bytes(&m_1, (MAGIC_ECRYPTFS_MARKER_SIZE_BYTES / 2));
876237fead6SMichael Halcrow 	m_2 = (m_1 ^ MAGIC_ECRYPTFS_MARKER);
87729335c6aSHarvey Harrison 	put_unaligned_be32(m_1, page_virt);
87829335c6aSHarvey Harrison 	page_virt += (MAGIC_ECRYPTFS_MARKER_SIZE_BYTES / 2);
87929335c6aSHarvey Harrison 	put_unaligned_be32(m_2, page_virt);
880237fead6SMichael Halcrow 	(*written) = MAGIC_ECRYPTFS_MARKER_SIZE_BYTES;
881237fead6SMichael Halcrow }
882237fead6SMichael Halcrow 
883f4e60e6bSTyler Hicks void ecryptfs_write_crypt_stat_flags(char *page_virt,
884f4e60e6bSTyler Hicks 				     struct ecryptfs_crypt_stat *crypt_stat,
885237fead6SMichael Halcrow 				     size_t *written)
886237fead6SMichael Halcrow {
887237fead6SMichael Halcrow 	u32 flags = 0;
888237fead6SMichael Halcrow 	int i;
889237fead6SMichael Halcrow 
89002f9876eSJérémy Lefaure 	for (i = 0; i < ARRAY_SIZE(ecryptfs_flag_map); i++)
891e2bd99ecSMichael Halcrow 		if (crypt_stat->flags & ecryptfs_flag_map[i].local_flag)
892237fead6SMichael Halcrow 			flags |= ecryptfs_flag_map[i].file_flag;
893237fead6SMichael Halcrow 	/* Version is in top 8 bits of the 32-bit flag vector */
894237fead6SMichael Halcrow 	flags |= ((((u8)crypt_stat->file_version) << 24) & 0xFF000000);
89529335c6aSHarvey Harrison 	put_unaligned_be32(flags, page_virt);
896237fead6SMichael Halcrow 	(*written) = 4;
897237fead6SMichael Halcrow }
898237fead6SMichael Halcrow 
899237fead6SMichael Halcrow struct ecryptfs_cipher_code_str_map_elem {
900237fead6SMichael Halcrow 	char cipher_str[16];
90119e66a67STrevor Highland 	u8 cipher_code;
902237fead6SMichael Halcrow };
903237fead6SMichael Halcrow 
904237fead6SMichael Halcrow /* Add support for additional ciphers by adding elements here. The
90540f0fd37SChris J Arges  * cipher_code is whatever OpenPGP applications use to identify the
906237fead6SMichael Halcrow  * ciphers. List in order of probability. */
907237fead6SMichael Halcrow static struct ecryptfs_cipher_code_str_map_elem
908237fead6SMichael Halcrow ecryptfs_cipher_code_str_map[] = {
909237fead6SMichael Halcrow 	{"aes",RFC2440_CIPHER_AES_128 },
910237fead6SMichael Halcrow 	{"blowfish", RFC2440_CIPHER_BLOWFISH},
911237fead6SMichael Halcrow 	{"des3_ede", RFC2440_CIPHER_DES3_EDE},
912237fead6SMichael Halcrow 	{"cast5", RFC2440_CIPHER_CAST_5},
913237fead6SMichael Halcrow 	{"twofish", RFC2440_CIPHER_TWOFISH},
914237fead6SMichael Halcrow 	{"cast6", RFC2440_CIPHER_CAST_6},
915237fead6SMichael Halcrow 	{"aes", RFC2440_CIPHER_AES_192},
916237fead6SMichael Halcrow 	{"aes", RFC2440_CIPHER_AES_256}
917237fead6SMichael Halcrow };
918237fead6SMichael Halcrow 
919237fead6SMichael Halcrow /**
920237fead6SMichael Halcrow  * ecryptfs_code_for_cipher_string
9219c79f34fSMichael Halcrow  * @cipher_name: The string alias for the cipher
9229c79f34fSMichael Halcrow  * @key_bytes: Length of key in bytes; used for AES code selection
923237fead6SMichael Halcrow  *
924237fead6SMichael Halcrow  * Returns zero on no match, or the cipher code on match
925237fead6SMichael Halcrow  */
9269c79f34fSMichael Halcrow u8 ecryptfs_code_for_cipher_string(char *cipher_name, size_t key_bytes)
927237fead6SMichael Halcrow {
928237fead6SMichael Halcrow 	int i;
92919e66a67STrevor Highland 	u8 code = 0;
930237fead6SMichael Halcrow 	struct ecryptfs_cipher_code_str_map_elem *map =
931237fead6SMichael Halcrow 		ecryptfs_cipher_code_str_map;
932237fead6SMichael Halcrow 
9339c79f34fSMichael Halcrow 	if (strcmp(cipher_name, "aes") == 0) {
9349c79f34fSMichael Halcrow 		switch (key_bytes) {
935237fead6SMichael Halcrow 		case 16:
936237fead6SMichael Halcrow 			code = RFC2440_CIPHER_AES_128;
937237fead6SMichael Halcrow 			break;
938237fead6SMichael Halcrow 		case 24:
939237fead6SMichael Halcrow 			code = RFC2440_CIPHER_AES_192;
940237fead6SMichael Halcrow 			break;
941237fead6SMichael Halcrow 		case 32:
942237fead6SMichael Halcrow 			code = RFC2440_CIPHER_AES_256;
943237fead6SMichael Halcrow 		}
944237fead6SMichael Halcrow 	} else {
945237fead6SMichael Halcrow 		for (i = 0; i < ARRAY_SIZE(ecryptfs_cipher_code_str_map); i++)
9469c79f34fSMichael Halcrow 			if (strcmp(cipher_name, map[i].cipher_str) == 0) {
947237fead6SMichael Halcrow 				code = map[i].cipher_code;
948237fead6SMichael Halcrow 				break;
949237fead6SMichael Halcrow 			}
950237fead6SMichael Halcrow 	}
951237fead6SMichael Halcrow 	return code;
952237fead6SMichael Halcrow }
953237fead6SMichael Halcrow 
954237fead6SMichael Halcrow /**
955237fead6SMichael Halcrow  * ecryptfs_cipher_code_to_string
956237fead6SMichael Halcrow  * @str: Destination to write out the cipher name
957237fead6SMichael Halcrow  * @cipher_code: The code to convert to cipher name string
958237fead6SMichael Halcrow  *
959237fead6SMichael Halcrow  * Returns zero on success
960237fead6SMichael Halcrow  */
96119e66a67STrevor Highland int ecryptfs_cipher_code_to_string(char *str, u8 cipher_code)
962237fead6SMichael Halcrow {
963237fead6SMichael Halcrow 	int rc = 0;
964237fead6SMichael Halcrow 	int i;
965237fead6SMichael Halcrow 
966237fead6SMichael Halcrow 	str[0] = '\0';
967237fead6SMichael Halcrow 	for (i = 0; i < ARRAY_SIZE(ecryptfs_cipher_code_str_map); i++)
968237fead6SMichael Halcrow 		if (cipher_code == ecryptfs_cipher_code_str_map[i].cipher_code)
969237fead6SMichael Halcrow 			strcpy(str, ecryptfs_cipher_code_str_map[i].cipher_str);
970237fead6SMichael Halcrow 	if (str[0] == '\0') {
971237fead6SMichael Halcrow 		ecryptfs_printk(KERN_WARNING, "Cipher code not recognized: "
972237fead6SMichael Halcrow 				"[%d]\n", cipher_code);
973237fead6SMichael Halcrow 		rc = -EINVAL;
974237fead6SMichael Halcrow 	}
975237fead6SMichael Halcrow 	return rc;
976237fead6SMichael Halcrow }
977237fead6SMichael Halcrow 
978778aeb42STyler Hicks int ecryptfs_read_and_validate_header_region(struct inode *inode)
979dd2a3b7aSMichael Halcrow {
980778aeb42STyler Hicks 	u8 file_size[ECRYPTFS_SIZE_AND_MARKER_BYTES];
981778aeb42STyler Hicks 	u8 *marker = file_size + ECRYPTFS_FILE_SIZE_BYTES;
982dd2a3b7aSMichael Halcrow 	int rc;
983dd2a3b7aSMichael Halcrow 
984778aeb42STyler Hicks 	rc = ecryptfs_read_lower(file_size, 0, ECRYPTFS_SIZE_AND_MARKER_BYTES,
985778aeb42STyler Hicks 				 inode);
9860bdf8a82SDan Carpenter 	if (rc < 0)
9870bdf8a82SDan Carpenter 		return rc;
9880bdf8a82SDan Carpenter 	else if (rc < ECRYPTFS_SIZE_AND_MARKER_BYTES)
9890bdf8a82SDan Carpenter 		return -EINVAL;
990778aeb42STyler Hicks 	rc = ecryptfs_validate_marker(marker);
991778aeb42STyler Hicks 	if (!rc)
992778aeb42STyler Hicks 		ecryptfs_i_size_init(file_size, inode);
993dd2a3b7aSMichael Halcrow 	return rc;
994dd2a3b7aSMichael Halcrow }
995dd2a3b7aSMichael Halcrow 
996e77a56ddSMichael Halcrow void
997e77a56ddSMichael Halcrow ecryptfs_write_header_metadata(char *virt,
998e77a56ddSMichael Halcrow 			       struct ecryptfs_crypt_stat *crypt_stat,
999237fead6SMichael Halcrow 			       size_t *written)
1000237fead6SMichael Halcrow {
1001237fead6SMichael Halcrow 	u32 header_extent_size;
1002237fead6SMichael Halcrow 	u16 num_header_extents_at_front;
1003237fead6SMichael Halcrow 
100445eaab79SMichael Halcrow 	header_extent_size = (u32)crypt_stat->extent_size;
1005237fead6SMichael Halcrow 	num_header_extents_at_front =
1006fa3ef1cbSTyler Hicks 		(u16)(crypt_stat->metadata_size / crypt_stat->extent_size);
100729335c6aSHarvey Harrison 	put_unaligned_be32(header_extent_size, virt);
1008237fead6SMichael Halcrow 	virt += 4;
100929335c6aSHarvey Harrison 	put_unaligned_be16(num_header_extents_at_front, virt);
1010237fead6SMichael Halcrow 	(*written) = 6;
1011237fead6SMichael Halcrow }
1012237fead6SMichael Halcrow 
101330632870STyler Hicks struct kmem_cache *ecryptfs_header_cache;
1014237fead6SMichael Halcrow 
1015237fead6SMichael Halcrow /**
1016237fead6SMichael Halcrow  * ecryptfs_write_headers_virt
101722e78fafSMichael Halcrow  * @page_virt: The virtual address to write the headers to
101887b811c3SEric Sandeen  * @max: The size of memory allocated at page_virt
101922e78fafSMichael Halcrow  * @size: Set to the number of bytes written by this function
102022e78fafSMichael Halcrow  * @crypt_stat: The cryptographic context
102122e78fafSMichael Halcrow  * @ecryptfs_dentry: The eCryptfs dentry
1022237fead6SMichael Halcrow  *
1023237fead6SMichael Halcrow  * Format version: 1
1024237fead6SMichael Halcrow  *
1025237fead6SMichael Halcrow  *   Header Extent:
1026237fead6SMichael Halcrow  *     Octets 0-7:        Unencrypted file size (big-endian)
1027237fead6SMichael Halcrow  *     Octets 8-15:       eCryptfs special marker
1028237fead6SMichael Halcrow  *     Octets 16-19:      Flags
1029237fead6SMichael Halcrow  *      Octet 16:         File format version number (between 0 and 255)
1030237fead6SMichael Halcrow  *      Octets 17-18:     Reserved
1031237fead6SMichael Halcrow  *      Octet 19:         Bit 1 (lsb): Reserved
1032237fead6SMichael Halcrow  *                        Bit 2: Encrypted?
1033237fead6SMichael Halcrow  *                        Bits 3-8: Reserved
1034237fead6SMichael Halcrow  *     Octets 20-23:      Header extent size (big-endian)
1035237fead6SMichael Halcrow  *     Octets 24-25:      Number of header extents at front of file
1036237fead6SMichael Halcrow  *                        (big-endian)
1037237fead6SMichael Halcrow  *     Octet  26:         Begin RFC 2440 authentication token packet set
1038237fead6SMichael Halcrow  *   Data Extent 0:
1039237fead6SMichael Halcrow  *     Lower data (CBC encrypted)
1040237fead6SMichael Halcrow  *   Data Extent 1:
1041237fead6SMichael Halcrow  *     Lower data (CBC encrypted)
1042237fead6SMichael Halcrow  *   ...
1043237fead6SMichael Halcrow  *
1044237fead6SMichael Halcrow  * Returns zero on success
1045237fead6SMichael Halcrow  */
104687b811c3SEric Sandeen static int ecryptfs_write_headers_virt(char *page_virt, size_t max,
104787b811c3SEric Sandeen 				       size_t *size,
1048237fead6SMichael Halcrow 				       struct ecryptfs_crypt_stat *crypt_stat,
1049237fead6SMichael Halcrow 				       struct dentry *ecryptfs_dentry)
1050237fead6SMichael Halcrow {
1051237fead6SMichael Halcrow 	int rc;
1052237fead6SMichael Halcrow 	size_t written;
1053237fead6SMichael Halcrow 	size_t offset;
1054237fead6SMichael Halcrow 
1055237fead6SMichael Halcrow 	offset = ECRYPTFS_FILE_SIZE_BYTES;
1056237fead6SMichael Halcrow 	write_ecryptfs_marker((page_virt + offset), &written);
1057237fead6SMichael Halcrow 	offset += written;
1058f4e60e6bSTyler Hicks 	ecryptfs_write_crypt_stat_flags((page_virt + offset), crypt_stat,
1059f4e60e6bSTyler Hicks 					&written);
1060237fead6SMichael Halcrow 	offset += written;
1061e77a56ddSMichael Halcrow 	ecryptfs_write_header_metadata((page_virt + offset), crypt_stat,
1062e77a56ddSMichael Halcrow 				       &written);
1063237fead6SMichael Halcrow 	offset += written;
1064237fead6SMichael Halcrow 	rc = ecryptfs_generate_key_packet_set((page_virt + offset), crypt_stat,
1065237fead6SMichael Halcrow 					      ecryptfs_dentry, &written,
106687b811c3SEric Sandeen 					      max - offset);
1067237fead6SMichael Halcrow 	if (rc)
1068237fead6SMichael Halcrow 		ecryptfs_printk(KERN_WARNING, "Error generating key packet "
1069237fead6SMichael Halcrow 				"set; rc = [%d]\n", rc);
1070dd2a3b7aSMichael Halcrow 	if (size) {
1071dd2a3b7aSMichael Halcrow 		offset += written;
1072dd2a3b7aSMichael Halcrow 		*size = offset;
1073dd2a3b7aSMichael Halcrow 	}
1074dd2a3b7aSMichael Halcrow 	return rc;
1075dd2a3b7aSMichael Halcrow }
1076dd2a3b7aSMichael Halcrow 
107722e78fafSMichael Halcrow static int
1078b59db43aSTyler Hicks ecryptfs_write_metadata_to_contents(struct inode *ecryptfs_inode,
10798faece5fSTyler Hicks 				    char *virt, size_t virt_len)
1080dd2a3b7aSMichael Halcrow {
1081d7cdc5feSMichael Halcrow 	int rc;
1082dd2a3b7aSMichael Halcrow 
1083b59db43aSTyler Hicks 	rc = ecryptfs_write_lower(ecryptfs_inode, virt,
10848faece5fSTyler Hicks 				  0, virt_len);
108596a7b9c2STyler Hicks 	if (rc < 0)
1086d7cdc5feSMichael Halcrow 		printk(KERN_ERR "%s: Error attempting to write header "
108796a7b9c2STyler Hicks 		       "information to lower file; rc = [%d]\n", __func__, rc);
108896a7b9c2STyler Hicks 	else
108996a7b9c2STyler Hicks 		rc = 0;
109070456600SMichael Halcrow 	return rc;
1091dd2a3b7aSMichael Halcrow }
1092dd2a3b7aSMichael Halcrow 
109322e78fafSMichael Halcrow static int
109422e78fafSMichael Halcrow ecryptfs_write_metadata_to_xattr(struct dentry *ecryptfs_dentry,
10953767e255SAl Viro 				 struct inode *ecryptfs_inode,
1096dd2a3b7aSMichael Halcrow 				 char *page_virt, size_t size)
1097dd2a3b7aSMichael Halcrow {
1098dd2a3b7aSMichael Halcrow 	int rc;
1099d43388deSRobbie Ko 	struct dentry *lower_dentry = ecryptfs_dentry_to_lower(ecryptfs_dentry);
1100d43388deSRobbie Ko 	struct inode *lower_inode = d_inode(lower_dentry);
1101dd2a3b7aSMichael Halcrow 
1102d43388deSRobbie Ko 	if (!(lower_inode->i_opflags & IOP_XATTR)) {
1103d43388deSRobbie Ko 		rc = -EOPNOTSUPP;
1104d43388deSRobbie Ko 		goto out;
1105d43388deSRobbie Ko 	}
1106d43388deSRobbie Ko 
1107d43388deSRobbie Ko 	inode_lock(lower_inode);
1108*39f60c1cSChristian Brauner 	rc = __vfs_setxattr(&nop_mnt_idmap, lower_dentry, lower_inode,
1109c7c7a1a1STycho Andersen 			    ECRYPTFS_XATTR_NAME, page_virt, size, 0);
1110d43388deSRobbie Ko 	if (!rc && ecryptfs_inode)
1111d43388deSRobbie Ko 		fsstack_copy_attr_all(ecryptfs_inode, lower_inode);
1112d43388deSRobbie Ko 	inode_unlock(lower_inode);
1113d43388deSRobbie Ko out:
1114237fead6SMichael Halcrow 	return rc;
1115237fead6SMichael Halcrow }
1116237fead6SMichael Halcrow 
11178faece5fSTyler Hicks static unsigned long ecryptfs_get_zeroed_pages(gfp_t gfp_mask,
11188faece5fSTyler Hicks 					       unsigned int order)
11198faece5fSTyler Hicks {
11208faece5fSTyler Hicks 	struct page *page;
11218faece5fSTyler Hicks 
11228faece5fSTyler Hicks 	page = alloc_pages(gfp_mask | __GFP_ZERO, order);
11238faece5fSTyler Hicks 	if (page)
11248faece5fSTyler Hicks 		return (unsigned long) page_address(page);
11258faece5fSTyler Hicks 	return 0;
11268faece5fSTyler Hicks }
11278faece5fSTyler Hicks 
1128237fead6SMichael Halcrow /**
1129dd2a3b7aSMichael Halcrow  * ecryptfs_write_metadata
1130b59db43aSTyler Hicks  * @ecryptfs_dentry: The eCryptfs dentry, which should be negative
1131b59db43aSTyler Hicks  * @ecryptfs_inode: The newly created eCryptfs inode
1132237fead6SMichael Halcrow  *
1133237fead6SMichael Halcrow  * Write the file headers out.  This will likely involve a userspace
1134237fead6SMichael Halcrow  * callout, in which the session key is encrypted with one or more
1135237fead6SMichael Halcrow  * public keys and/or the passphrase necessary to do the encryption is
1136237fead6SMichael Halcrow  * retrieved via a prompt.  Exactly what happens at this point should
1137237fead6SMichael Halcrow  * be policy-dependent.
1138237fead6SMichael Halcrow  *
1139237fead6SMichael Halcrow  * Returns zero on success; non-zero on error
1140237fead6SMichael Halcrow  */
1141b59db43aSTyler Hicks int ecryptfs_write_metadata(struct dentry *ecryptfs_dentry,
1142b59db43aSTyler Hicks 			    struct inode *ecryptfs_inode)
1143237fead6SMichael Halcrow {
1144d7cdc5feSMichael Halcrow 	struct ecryptfs_crypt_stat *crypt_stat =
1145b59db43aSTyler Hicks 		&ecryptfs_inode_to_private(ecryptfs_inode)->crypt_stat;
11468faece5fSTyler Hicks 	unsigned int order;
1147cc11beffSMichael Halcrow 	char *virt;
11488faece5fSTyler Hicks 	size_t virt_len;
1149d7cdc5feSMichael Halcrow 	size_t size = 0;
1150237fead6SMichael Halcrow 	int rc = 0;
1151237fead6SMichael Halcrow 
1152e2bd99ecSMichael Halcrow 	if (likely(crypt_stat->flags & ECRYPTFS_ENCRYPTED)) {
1153e2bd99ecSMichael Halcrow 		if (!(crypt_stat->flags & ECRYPTFS_KEY_VALID)) {
1154d7cdc5feSMichael Halcrow 			printk(KERN_ERR "Key is invalid; bailing out\n");
1155237fead6SMichael Halcrow 			rc = -EINVAL;
1156237fead6SMichael Halcrow 			goto out;
1157237fead6SMichael Halcrow 		}
1158237fead6SMichael Halcrow 	} else {
1159cc11beffSMichael Halcrow 		printk(KERN_WARNING "%s: Encrypted flag not set\n",
116018d1dbf1SHarvey Harrison 		       __func__);
1161237fead6SMichael Halcrow 		rc = -EINVAL;
1162237fead6SMichael Halcrow 		goto out;
1163237fead6SMichael Halcrow 	}
1164fa3ef1cbSTyler Hicks 	virt_len = crypt_stat->metadata_size;
11658faece5fSTyler Hicks 	order = get_order(virt_len);
1166237fead6SMichael Halcrow 	/* Released in this function */
11678faece5fSTyler Hicks 	virt = (char *)ecryptfs_get_zeroed_pages(GFP_KERNEL, order);
1168cc11beffSMichael Halcrow 	if (!virt) {
116918d1dbf1SHarvey Harrison 		printk(KERN_ERR "%s: Out of memory\n", __func__);
1170237fead6SMichael Halcrow 		rc = -ENOMEM;
1171237fead6SMichael Halcrow 		goto out;
1172237fead6SMichael Halcrow 	}
1173bd4f0fe8STyler Hicks 	/* Zeroed page ensures the in-header unencrypted i_size is set to 0 */
11748faece5fSTyler Hicks 	rc = ecryptfs_write_headers_virt(virt, virt_len, &size, crypt_stat,
11758faece5fSTyler Hicks 					 ecryptfs_dentry);
1176237fead6SMichael Halcrow 	if (unlikely(rc)) {
1177cc11beffSMichael Halcrow 		printk(KERN_ERR "%s: Error whilst writing headers; rc = [%d]\n",
117818d1dbf1SHarvey Harrison 		       __func__, rc);
1179237fead6SMichael Halcrow 		goto out_free;
1180237fead6SMichael Halcrow 	}
1181dd2a3b7aSMichael Halcrow 	if (crypt_stat->flags & ECRYPTFS_METADATA_IN_XATTR)
11823767e255SAl Viro 		rc = ecryptfs_write_metadata_to_xattr(ecryptfs_dentry, ecryptfs_inode,
11833767e255SAl Viro 						      virt, size);
1184dd2a3b7aSMichael Halcrow 	else
1185b59db43aSTyler Hicks 		rc = ecryptfs_write_metadata_to_contents(ecryptfs_inode, virt,
11868faece5fSTyler Hicks 							 virt_len);
1187dd2a3b7aSMichael Halcrow 	if (rc) {
1188cc11beffSMichael Halcrow 		printk(KERN_ERR "%s: Error writing metadata out to lower file; "
118918d1dbf1SHarvey Harrison 		       "rc = [%d]\n", __func__, rc);
1190dd2a3b7aSMichael Halcrow 		goto out_free;
1191237fead6SMichael Halcrow 	}
1192237fead6SMichael Halcrow out_free:
11938faece5fSTyler Hicks 	free_pages((unsigned long)virt, order);
1194237fead6SMichael Halcrow out:
1195237fead6SMichael Halcrow 	return rc;
1196237fead6SMichael Halcrow }
1197237fead6SMichael Halcrow 
1198dd2a3b7aSMichael Halcrow #define ECRYPTFS_DONT_VALIDATE_HEADER_SIZE 0
1199dd2a3b7aSMichael Halcrow #define ECRYPTFS_VALIDATE_HEADER_SIZE 1
1200237fead6SMichael Halcrow static int parse_header_metadata(struct ecryptfs_crypt_stat *crypt_stat,
1201dd2a3b7aSMichael Halcrow 				 char *virt, int *bytes_read,
1202dd2a3b7aSMichael Halcrow 				 int validate_header_size)
1203237fead6SMichael Halcrow {
1204237fead6SMichael Halcrow 	int rc = 0;
1205237fead6SMichael Halcrow 	u32 header_extent_size;
1206237fead6SMichael Halcrow 	u16 num_header_extents_at_front;
1207237fead6SMichael Halcrow 
120829335c6aSHarvey Harrison 	header_extent_size = get_unaligned_be32(virt);
120929335c6aSHarvey Harrison 	virt += sizeof(__be32);
121029335c6aSHarvey Harrison 	num_header_extents_at_front = get_unaligned_be16(virt);
1211fa3ef1cbSTyler Hicks 	crypt_stat->metadata_size = (((size_t)num_header_extents_at_front
1212cc11beffSMichael Halcrow 				     * (size_t)header_extent_size));
121329335c6aSHarvey Harrison 	(*bytes_read) = (sizeof(__be32) + sizeof(__be16));
1214dd2a3b7aSMichael Halcrow 	if ((validate_header_size == ECRYPTFS_VALIDATE_HEADER_SIZE)
1215fa3ef1cbSTyler Hicks 	    && (crypt_stat->metadata_size
1216dd2a3b7aSMichael Halcrow 		< ECRYPTFS_MINIMUM_HEADER_EXTENT_SIZE)) {
1217237fead6SMichael Halcrow 		rc = -EINVAL;
1218cc11beffSMichael Halcrow 		printk(KERN_WARNING "Invalid header size: [%zd]\n",
1219fa3ef1cbSTyler Hicks 		       crypt_stat->metadata_size);
1220237fead6SMichael Halcrow 	}
1221237fead6SMichael Halcrow 	return rc;
1222237fead6SMichael Halcrow }
1223237fead6SMichael Halcrow 
1224237fead6SMichael Halcrow /**
1225237fead6SMichael Halcrow  * set_default_header_data
122622e78fafSMichael Halcrow  * @crypt_stat: The cryptographic context
1227237fead6SMichael Halcrow  *
1228237fead6SMichael Halcrow  * For version 0 file format; this function is only for backwards
1229237fead6SMichael Halcrow  * compatibility for files created with the prior versions of
1230237fead6SMichael Halcrow  * eCryptfs.
1231237fead6SMichael Halcrow  */
1232237fead6SMichael Halcrow static void set_default_header_data(struct ecryptfs_crypt_stat *crypt_stat)
1233237fead6SMichael Halcrow {
1234fa3ef1cbSTyler Hicks 	crypt_stat->metadata_size = ECRYPTFS_MINIMUM_HEADER_EXTENT_SIZE;
1235237fead6SMichael Halcrow }
1236237fead6SMichael Halcrow 
12373aeb86eaSTyler Hicks void ecryptfs_i_size_init(const char *page_virt, struct inode *inode)
12383aeb86eaSTyler Hicks {
12393aeb86eaSTyler Hicks 	struct ecryptfs_mount_crypt_stat *mount_crypt_stat;
12403aeb86eaSTyler Hicks 	struct ecryptfs_crypt_stat *crypt_stat;
12413aeb86eaSTyler Hicks 	u64 file_size;
12423aeb86eaSTyler Hicks 
12433aeb86eaSTyler Hicks 	crypt_stat = &ecryptfs_inode_to_private(inode)->crypt_stat;
12443aeb86eaSTyler Hicks 	mount_crypt_stat =
12453aeb86eaSTyler Hicks 		&ecryptfs_superblock_to_private(inode->i_sb)->mount_crypt_stat;
12463aeb86eaSTyler Hicks 	if (mount_crypt_stat->flags & ECRYPTFS_ENCRYPTED_VIEW_ENABLED) {
12473aeb86eaSTyler Hicks 		file_size = i_size_read(ecryptfs_inode_to_lower(inode));
12483aeb86eaSTyler Hicks 		if (crypt_stat->flags & ECRYPTFS_METADATA_IN_XATTR)
12493aeb86eaSTyler Hicks 			file_size += crypt_stat->metadata_size;
12503aeb86eaSTyler Hicks 	} else
12513aeb86eaSTyler Hicks 		file_size = get_unaligned_be64(page_virt);
12523aeb86eaSTyler Hicks 	i_size_write(inode, (loff_t)file_size);
12533aeb86eaSTyler Hicks 	crypt_stat->flags |= ECRYPTFS_I_SIZE_INITIALIZED;
12543aeb86eaSTyler Hicks }
12553aeb86eaSTyler Hicks 
1256237fead6SMichael Halcrow /**
1257237fead6SMichael Halcrow  * ecryptfs_read_headers_virt
125822e78fafSMichael Halcrow  * @page_virt: The virtual address into which to read the headers
125922e78fafSMichael Halcrow  * @crypt_stat: The cryptographic context
126022e78fafSMichael Halcrow  * @ecryptfs_dentry: The eCryptfs dentry
126122e78fafSMichael Halcrow  * @validate_header_size: Whether to validate the header size while reading
1262237fead6SMichael Halcrow  *
1263237fead6SMichael Halcrow  * Read/parse the header data. The header format is detailed in the
1264237fead6SMichael Halcrow  * comment block for the ecryptfs_write_headers_virt() function.
1265237fead6SMichael Halcrow  *
1266237fead6SMichael Halcrow  * Returns zero on success
1267237fead6SMichael Halcrow  */
1268237fead6SMichael Halcrow static int ecryptfs_read_headers_virt(char *page_virt,
1269237fead6SMichael Halcrow 				      struct ecryptfs_crypt_stat *crypt_stat,
1270dd2a3b7aSMichael Halcrow 				      struct dentry *ecryptfs_dentry,
1271dd2a3b7aSMichael Halcrow 				      int validate_header_size)
1272237fead6SMichael Halcrow {
1273237fead6SMichael Halcrow 	int rc = 0;
1274237fead6SMichael Halcrow 	int offset;
1275237fead6SMichael Halcrow 	int bytes_read;
1276237fead6SMichael Halcrow 
1277237fead6SMichael Halcrow 	ecryptfs_set_default_sizes(crypt_stat);
1278237fead6SMichael Halcrow 	crypt_stat->mount_crypt_stat = &ecryptfs_superblock_to_private(
1279237fead6SMichael Halcrow 		ecryptfs_dentry->d_sb)->mount_crypt_stat;
1280237fead6SMichael Halcrow 	offset = ECRYPTFS_FILE_SIZE_BYTES;
12817a86617eSTyler Hicks 	rc = ecryptfs_validate_marker(page_virt + offset);
12827a86617eSTyler Hicks 	if (rc)
1283237fead6SMichael Halcrow 		goto out;
12843aeb86eaSTyler Hicks 	if (!(crypt_stat->flags & ECRYPTFS_I_SIZE_INITIALIZED))
12852b0143b5SDavid Howells 		ecryptfs_i_size_init(page_virt, d_inode(ecryptfs_dentry));
1286237fead6SMichael Halcrow 	offset += MAGIC_ECRYPTFS_MARKER_SIZE_BYTES;
12877451c54aSHariprasad Kelam 	ecryptfs_process_flags(crypt_stat, (page_virt + offset), &bytes_read);
1288237fead6SMichael Halcrow 	if (crypt_stat->file_version > ECRYPTFS_SUPPORTED_FILE_VERSION) {
1289237fead6SMichael Halcrow 		ecryptfs_printk(KERN_WARNING, "File version is [%d]; only "
1290237fead6SMichael Halcrow 				"file version [%d] is supported by this "
1291237fead6SMichael Halcrow 				"version of eCryptfs\n",
1292237fead6SMichael Halcrow 				crypt_stat->file_version,
1293237fead6SMichael Halcrow 				ECRYPTFS_SUPPORTED_FILE_VERSION);
1294237fead6SMichael Halcrow 		rc = -EINVAL;
1295237fead6SMichael Halcrow 		goto out;
1296237fead6SMichael Halcrow 	}
1297237fead6SMichael Halcrow 	offset += bytes_read;
1298237fead6SMichael Halcrow 	if (crypt_stat->file_version >= 1) {
1299237fead6SMichael Halcrow 		rc = parse_header_metadata(crypt_stat, (page_virt + offset),
1300dd2a3b7aSMichael Halcrow 					   &bytes_read, validate_header_size);
1301237fead6SMichael Halcrow 		if (rc) {
1302237fead6SMichael Halcrow 			ecryptfs_printk(KERN_WARNING, "Error reading header "
1303237fead6SMichael Halcrow 					"metadata; rc = [%d]\n", rc);
1304237fead6SMichael Halcrow 		}
1305237fead6SMichael Halcrow 		offset += bytes_read;
1306237fead6SMichael Halcrow 	} else
1307237fead6SMichael Halcrow 		set_default_header_data(crypt_stat);
1308237fead6SMichael Halcrow 	rc = ecryptfs_parse_packet_set(crypt_stat, (page_virt + offset),
1309237fead6SMichael Halcrow 				       ecryptfs_dentry);
1310237fead6SMichael Halcrow out:
1311237fead6SMichael Halcrow 	return rc;
1312237fead6SMichael Halcrow }
1313237fead6SMichael Halcrow 
1314237fead6SMichael Halcrow /**
1315dd2a3b7aSMichael Halcrow  * ecryptfs_read_xattr_region
131622e78fafSMichael Halcrow  * @page_virt: The vitual address into which to read the xattr data
13172ed92554SMichael Halcrow  * @ecryptfs_inode: The eCryptfs inode
1318dd2a3b7aSMichael Halcrow  *
1319dd2a3b7aSMichael Halcrow  * Attempts to read the crypto metadata from the extended attribute
1320dd2a3b7aSMichael Halcrow  * region of the lower file.
132122e78fafSMichael Halcrow  *
132222e78fafSMichael Halcrow  * Returns zero on success; non-zero on error
1323dd2a3b7aSMichael Halcrow  */
1324d7cdc5feSMichael Halcrow int ecryptfs_read_xattr_region(char *page_virt, struct inode *ecryptfs_inode)
1325dd2a3b7aSMichael Halcrow {
1326d7cdc5feSMichael Halcrow 	struct dentry *lower_dentry =
1327b583043eSAl Viro 		ecryptfs_inode_to_private(ecryptfs_inode)->lower_file->f_path.dentry;
1328dd2a3b7aSMichael Halcrow 	ssize_t size;
1329dd2a3b7aSMichael Halcrow 	int rc = 0;
1330dd2a3b7aSMichael Halcrow 
1331ce23e640SAl Viro 	size = ecryptfs_getxattr_lower(lower_dentry,
1332ce23e640SAl Viro 				       ecryptfs_inode_to_lower(ecryptfs_inode),
1333ce23e640SAl Viro 				       ECRYPTFS_XATTR_NAME,
1334dd2a3b7aSMichael Halcrow 				       page_virt, ECRYPTFS_DEFAULT_EXTENT_SIZE);
1335dd2a3b7aSMichael Halcrow 	if (size < 0) {
133625bd8174SMichael Halcrow 		if (unlikely(ecryptfs_verbosity > 0))
133725bd8174SMichael Halcrow 			printk(KERN_INFO "Error attempting to read the [%s] "
133825bd8174SMichael Halcrow 			       "xattr from the lower file; return value = "
133925bd8174SMichael Halcrow 			       "[%zd]\n", ECRYPTFS_XATTR_NAME, size);
1340dd2a3b7aSMichael Halcrow 		rc = -EINVAL;
1341dd2a3b7aSMichael Halcrow 		goto out;
1342dd2a3b7aSMichael Halcrow 	}
1343dd2a3b7aSMichael Halcrow out:
1344dd2a3b7aSMichael Halcrow 	return rc;
1345dd2a3b7aSMichael Halcrow }
1346dd2a3b7aSMichael Halcrow 
1347778aeb42STyler Hicks int ecryptfs_read_and_validate_xattr_region(struct dentry *dentry,
13483b06b3ebSTyler Hicks 					    struct inode *inode)
1349dd2a3b7aSMichael Halcrow {
1350778aeb42STyler Hicks 	u8 file_size[ECRYPTFS_SIZE_AND_MARKER_BYTES];
1351778aeb42STyler Hicks 	u8 *marker = file_size + ECRYPTFS_FILE_SIZE_BYTES;
1352dd2a3b7aSMichael Halcrow 	int rc;
1353dd2a3b7aSMichael Halcrow 
1354778aeb42STyler Hicks 	rc = ecryptfs_getxattr_lower(ecryptfs_dentry_to_lower(dentry),
1355ce23e640SAl Viro 				     ecryptfs_inode_to_lower(inode),
1356778aeb42STyler Hicks 				     ECRYPTFS_XATTR_NAME, file_size,
1357778aeb42STyler Hicks 				     ECRYPTFS_SIZE_AND_MARKER_BYTES);
13580bdf8a82SDan Carpenter 	if (rc < 0)
13590bdf8a82SDan Carpenter 		return rc;
13600bdf8a82SDan Carpenter 	else if (rc < ECRYPTFS_SIZE_AND_MARKER_BYTES)
13610bdf8a82SDan Carpenter 		return -EINVAL;
1362778aeb42STyler Hicks 	rc = ecryptfs_validate_marker(marker);
1363778aeb42STyler Hicks 	if (!rc)
1364778aeb42STyler Hicks 		ecryptfs_i_size_init(file_size, inode);
1365dd2a3b7aSMichael Halcrow 	return rc;
1366dd2a3b7aSMichael Halcrow }
1367dd2a3b7aSMichael Halcrow 
13685da877eaSLee Jones /*
1369dd2a3b7aSMichael Halcrow  * ecryptfs_read_metadata
1370dd2a3b7aSMichael Halcrow  *
1371dd2a3b7aSMichael Halcrow  * Common entry point for reading file metadata. From here, we could
1372dd2a3b7aSMichael Halcrow  * retrieve the header information from the header region of the file,
137340f0fd37SChris J Arges  * the xattr region of the file, or some other repository that is
1374dd2a3b7aSMichael Halcrow  * stored separately from the file itself. The current implementation
1375dd2a3b7aSMichael Halcrow  * supports retrieving the metadata information from the file contents
1376dd2a3b7aSMichael Halcrow  * and from the xattr region.
1377237fead6SMichael Halcrow  *
1378237fead6SMichael Halcrow  * Returns zero if valid headers found and parsed; non-zero otherwise
1379237fead6SMichael Halcrow  */
1380d7cdc5feSMichael Halcrow int ecryptfs_read_metadata(struct dentry *ecryptfs_dentry)
1381237fead6SMichael Halcrow {
1382bb450361STim Gardner 	int rc;
1383bb450361STim Gardner 	char *page_virt;
13842b0143b5SDavid Howells 	struct inode *ecryptfs_inode = d_inode(ecryptfs_dentry);
1385237fead6SMichael Halcrow 	struct ecryptfs_crypt_stat *crypt_stat =
1386d7cdc5feSMichael Halcrow 	    &ecryptfs_inode_to_private(ecryptfs_inode)->crypt_stat;
1387e77a56ddSMichael Halcrow 	struct ecryptfs_mount_crypt_stat *mount_crypt_stat =
1388e77a56ddSMichael Halcrow 		&ecryptfs_superblock_to_private(
1389e77a56ddSMichael Halcrow 			ecryptfs_dentry->d_sb)->mount_crypt_stat;
1390237fead6SMichael Halcrow 
1391e77a56ddSMichael Halcrow 	ecryptfs_copy_mount_wide_flags_to_inode_flags(crypt_stat,
1392e77a56ddSMichael Halcrow 						      mount_crypt_stat);
1393237fead6SMichael Halcrow 	/* Read the first page from the underlying file */
139430632870STyler Hicks 	page_virt = kmem_cache_alloc(ecryptfs_header_cache, GFP_USER);
1395237fead6SMichael Halcrow 	if (!page_virt) {
1396237fead6SMichael Halcrow 		rc = -ENOMEM;
1397237fead6SMichael Halcrow 		goto out;
1398237fead6SMichael Halcrow 	}
1399d7cdc5feSMichael Halcrow 	rc = ecryptfs_read_lower(page_virt, 0, crypt_stat->extent_size,
1400d7cdc5feSMichael Halcrow 				 ecryptfs_inode);
140196a7b9c2STyler Hicks 	if (rc >= 0)
1402237fead6SMichael Halcrow 		rc = ecryptfs_read_headers_virt(page_virt, crypt_stat,
1403dd2a3b7aSMichael Halcrow 						ecryptfs_dentry,
1404dd2a3b7aSMichael Halcrow 						ECRYPTFS_VALIDATE_HEADER_SIZE);
1405dd2a3b7aSMichael Halcrow 	if (rc) {
1406bb450361STim Gardner 		/* metadata is not in the file header, so try xattrs */
140709cbfeafSKirill A. Shutemov 		memset(page_virt, 0, PAGE_SIZE);
1408d7cdc5feSMichael Halcrow 		rc = ecryptfs_read_xattr_region(page_virt, ecryptfs_inode);
1409237fead6SMichael Halcrow 		if (rc) {
1410dd2a3b7aSMichael Halcrow 			printk(KERN_DEBUG "Valid eCryptfs headers not found in "
141130373dc0STim Gardner 			       "file header region or xattr region, inode %lu\n",
141230373dc0STim Gardner 				ecryptfs_inode->i_ino);
1413237fead6SMichael Halcrow 			rc = -EINVAL;
1414dd2a3b7aSMichael Halcrow 			goto out;
1415dd2a3b7aSMichael Halcrow 		}
1416dd2a3b7aSMichael Halcrow 		rc = ecryptfs_read_headers_virt(page_virt, crypt_stat,
1417dd2a3b7aSMichael Halcrow 						ecryptfs_dentry,
1418dd2a3b7aSMichael Halcrow 						ECRYPTFS_DONT_VALIDATE_HEADER_SIZE);
1419dd2a3b7aSMichael Halcrow 		if (rc) {
1420dd2a3b7aSMichael Halcrow 			printk(KERN_DEBUG "Valid eCryptfs headers not found in "
142130373dc0STim Gardner 			       "file xattr region either, inode %lu\n",
142230373dc0STim Gardner 				ecryptfs_inode->i_ino);
1423dd2a3b7aSMichael Halcrow 			rc = -EINVAL;
1424dd2a3b7aSMichael Halcrow 		}
1425dd2a3b7aSMichael Halcrow 		if (crypt_stat->mount_crypt_stat->flags
1426dd2a3b7aSMichael Halcrow 		    & ECRYPTFS_XATTR_METADATA_ENABLED) {
1427dd2a3b7aSMichael Halcrow 			crypt_stat->flags |= ECRYPTFS_METADATA_IN_XATTR;
1428dd2a3b7aSMichael Halcrow 		} else {
1429dd2a3b7aSMichael Halcrow 			printk(KERN_WARNING "Attempt to access file with "
1430dd2a3b7aSMichael Halcrow 			       "crypto metadata only in the extended attribute "
1431dd2a3b7aSMichael Halcrow 			       "region, but eCryptfs was mounted without "
1432dd2a3b7aSMichael Halcrow 			       "xattr support enabled. eCryptfs will not treat "
143330373dc0STim Gardner 			       "this like an encrypted file, inode %lu\n",
143430373dc0STim Gardner 				ecryptfs_inode->i_ino);
1435dd2a3b7aSMichael Halcrow 			rc = -EINVAL;
1436dd2a3b7aSMichael Halcrow 		}
1437237fead6SMichael Halcrow 	}
1438237fead6SMichael Halcrow out:
1439237fead6SMichael Halcrow 	if (page_virt) {
144009cbfeafSKirill A. Shutemov 		memset(page_virt, 0, PAGE_SIZE);
144130632870STyler Hicks 		kmem_cache_free(ecryptfs_header_cache, page_virt);
1442237fead6SMichael Halcrow 	}
1443237fead6SMichael Halcrow 	return rc;
1444237fead6SMichael Halcrow }
1445237fead6SMichael Halcrow 
14465da877eaSLee Jones /*
144751ca58dcSMichael Halcrow  * ecryptfs_encrypt_filename - encrypt filename
144851ca58dcSMichael Halcrow  *
144951ca58dcSMichael Halcrow  * CBC-encrypts the filename. We do not want to encrypt the same
145051ca58dcSMichael Halcrow  * filename with the same key and IV, which may happen with hard
145151ca58dcSMichael Halcrow  * links, so we prepend random bits to each filename.
145251ca58dcSMichael Halcrow  *
145351ca58dcSMichael Halcrow  * Returns zero on success; non-zero otherwise
145451ca58dcSMichael Halcrow  */
145551ca58dcSMichael Halcrow static int
145651ca58dcSMichael Halcrow ecryptfs_encrypt_filename(struct ecryptfs_filename *filename,
145751ca58dcSMichael Halcrow 			  struct ecryptfs_mount_crypt_stat *mount_crypt_stat)
145851ca58dcSMichael Halcrow {
145951ca58dcSMichael Halcrow 	int rc = 0;
146051ca58dcSMichael Halcrow 
146151ca58dcSMichael Halcrow 	filename->encrypted_filename = NULL;
146251ca58dcSMichael Halcrow 	filename->encrypted_filename_size = 0;
146397c31606SAl Viro 	if (mount_crypt_stat && (mount_crypt_stat->flags
146497c31606SAl Viro 				     & ECRYPTFS_GLOBAL_ENCFN_USE_MOUNT_FNEK)) {
146551ca58dcSMichael Halcrow 		size_t packet_size;
146651ca58dcSMichael Halcrow 		size_t remaining_bytes;
146751ca58dcSMichael Halcrow 
146851ca58dcSMichael Halcrow 		rc = ecryptfs_write_tag_70_packet(
146951ca58dcSMichael Halcrow 			NULL, NULL,
147051ca58dcSMichael Halcrow 			&filename->encrypted_filename_size,
147151ca58dcSMichael Halcrow 			mount_crypt_stat, NULL,
147251ca58dcSMichael Halcrow 			filename->filename_size);
147351ca58dcSMichael Halcrow 		if (rc) {
147451ca58dcSMichael Halcrow 			printk(KERN_ERR "%s: Error attempting to get packet "
147551ca58dcSMichael Halcrow 			       "size for tag 72; rc = [%d]\n", __func__,
147651ca58dcSMichael Halcrow 			       rc);
147751ca58dcSMichael Halcrow 			filename->encrypted_filename_size = 0;
147851ca58dcSMichael Halcrow 			goto out;
147951ca58dcSMichael Halcrow 		}
148051ca58dcSMichael Halcrow 		filename->encrypted_filename =
148151ca58dcSMichael Halcrow 			kmalloc(filename->encrypted_filename_size, GFP_KERNEL);
148251ca58dcSMichael Halcrow 		if (!filename->encrypted_filename) {
148351ca58dcSMichael Halcrow 			rc = -ENOMEM;
148451ca58dcSMichael Halcrow 			goto out;
148551ca58dcSMichael Halcrow 		}
148651ca58dcSMichael Halcrow 		remaining_bytes = filename->encrypted_filename_size;
148751ca58dcSMichael Halcrow 		rc = ecryptfs_write_tag_70_packet(filename->encrypted_filename,
148851ca58dcSMichael Halcrow 						  &remaining_bytes,
148951ca58dcSMichael Halcrow 						  &packet_size,
149051ca58dcSMichael Halcrow 						  mount_crypt_stat,
149151ca58dcSMichael Halcrow 						  filename->filename,
149251ca58dcSMichael Halcrow 						  filename->filename_size);
149351ca58dcSMichael Halcrow 		if (rc) {
149451ca58dcSMichael Halcrow 			printk(KERN_ERR "%s: Error attempting to generate "
149551ca58dcSMichael Halcrow 			       "tag 70 packet; rc = [%d]\n", __func__,
149651ca58dcSMichael Halcrow 			       rc);
149751ca58dcSMichael Halcrow 			kfree(filename->encrypted_filename);
149851ca58dcSMichael Halcrow 			filename->encrypted_filename = NULL;
149951ca58dcSMichael Halcrow 			filename->encrypted_filename_size = 0;
150051ca58dcSMichael Halcrow 			goto out;
150151ca58dcSMichael Halcrow 		}
150251ca58dcSMichael Halcrow 		filename->encrypted_filename_size = packet_size;
150351ca58dcSMichael Halcrow 	} else {
150451ca58dcSMichael Halcrow 		printk(KERN_ERR "%s: No support for requested filename "
150551ca58dcSMichael Halcrow 		       "encryption method in this release\n", __func__);
1506df6ad33bSTyler Hicks 		rc = -EOPNOTSUPP;
150751ca58dcSMichael Halcrow 		goto out;
150851ca58dcSMichael Halcrow 	}
150951ca58dcSMichael Halcrow out:
151051ca58dcSMichael Halcrow 	return rc;
151151ca58dcSMichael Halcrow }
151251ca58dcSMichael Halcrow 
151351ca58dcSMichael Halcrow static int ecryptfs_copy_filename(char **copied_name, size_t *copied_name_size,
151451ca58dcSMichael Halcrow 				  const char *name, size_t name_size)
151551ca58dcSMichael Halcrow {
151651ca58dcSMichael Halcrow 	int rc = 0;
151751ca58dcSMichael Halcrow 
1518fd9fc842STyler Hicks 	(*copied_name) = kmalloc((name_size + 1), GFP_KERNEL);
151951ca58dcSMichael Halcrow 	if (!(*copied_name)) {
152051ca58dcSMichael Halcrow 		rc = -ENOMEM;
152151ca58dcSMichael Halcrow 		goto out;
152251ca58dcSMichael Halcrow 	}
152351ca58dcSMichael Halcrow 	memcpy((void *)(*copied_name), (void *)name, name_size);
152451ca58dcSMichael Halcrow 	(*copied_name)[(name_size)] = '\0';	/* Only for convenience
152551ca58dcSMichael Halcrow 						 * in printing out the
152651ca58dcSMichael Halcrow 						 * string in debug
152751ca58dcSMichael Halcrow 						 * messages */
1528fd9fc842STyler Hicks 	(*copied_name_size) = name_size;
152951ca58dcSMichael Halcrow out:
153051ca58dcSMichael Halcrow 	return rc;
153151ca58dcSMichael Halcrow }
153251ca58dcSMichael Halcrow 
153351ca58dcSMichael Halcrow /**
1534f4aad16aSMichael Halcrow  * ecryptfs_process_key_cipher - Perform key cipher initialization.
1535237fead6SMichael Halcrow  * @key_tfm: Crypto context for key material, set by this function
1536e5d9cbdeSMichael Halcrow  * @cipher_name: Name of the cipher
1537e5d9cbdeSMichael Halcrow  * @key_size: Size of the key in bytes
1538237fead6SMichael Halcrow  *
1539237fead6SMichael Halcrow  * Returns zero on success. Any crypto_tfm structs allocated here
1540237fead6SMichael Halcrow  * should be released by other functions, such as on a superblock put
1541237fead6SMichael Halcrow  * event, regardless of whether this function succeeds for fails.
1542237fead6SMichael Halcrow  */
1543cd9d67dfSMichael Halcrow static int
15443095e8e3SHerbert Xu ecryptfs_process_key_cipher(struct crypto_skcipher **key_tfm,
1545f4aad16aSMichael Halcrow 			    char *cipher_name, size_t *key_size)
1546237fead6SMichael Halcrow {
1547237fead6SMichael Halcrow 	char dummy_key[ECRYPTFS_MAX_KEY_BYTES];
1548ece550f5SDan Carpenter 	char *full_alg_name = NULL;
1549237fead6SMichael Halcrow 	int rc;
1550237fead6SMichael Halcrow 
1551e5d9cbdeSMichael Halcrow 	*key_tfm = NULL;
1552e5d9cbdeSMichael Halcrow 	if (*key_size > ECRYPTFS_MAX_KEY_BYTES) {
1553237fead6SMichael Halcrow 		rc = -EINVAL;
1554df261c52SMichael Halcrow 		printk(KERN_ERR "Requested key size is [%zd] bytes; maximum "
1555e5d9cbdeSMichael Halcrow 		      "allowable is [%d]\n", *key_size, ECRYPTFS_MAX_KEY_BYTES);
1556237fead6SMichael Halcrow 		goto out;
1557237fead6SMichael Halcrow 	}
15588bba066fSMichael Halcrow 	rc = ecryptfs_crypto_api_algify_cipher_name(&full_alg_name, cipher_name,
15598bba066fSMichael Halcrow 						    "ecb");
15608bba066fSMichael Halcrow 	if (rc)
15618bba066fSMichael Halcrow 		goto out;
15623095e8e3SHerbert Xu 	*key_tfm = crypto_alloc_skcipher(full_alg_name, 0, CRYPTO_ALG_ASYNC);
15638bba066fSMichael Halcrow 	if (IS_ERR(*key_tfm)) {
15648bba066fSMichael Halcrow 		rc = PTR_ERR(*key_tfm);
1565237fead6SMichael Halcrow 		printk(KERN_ERR "Unable to allocate crypto cipher with name "
156638268498SDave Hansen 		       "[%s]; rc = [%d]\n", full_alg_name, rc);
1567237fead6SMichael Halcrow 		goto out;
1568237fead6SMichael Halcrow 	}
1569231baecdSEric Biggers 	crypto_skcipher_set_flags(*key_tfm, CRYPTO_TFM_REQ_FORBID_WEAK_KEYS);
15703095e8e3SHerbert Xu 	if (*key_size == 0)
15719ac0d136SEric Biggers 		*key_size = crypto_skcipher_max_keysize(*key_tfm);
1572e5d9cbdeSMichael Halcrow 	get_random_bytes(dummy_key, *key_size);
15733095e8e3SHerbert Xu 	rc = crypto_skcipher_setkey(*key_tfm, dummy_key, *key_size);
1574237fead6SMichael Halcrow 	if (rc) {
1575df261c52SMichael Halcrow 		printk(KERN_ERR "Error attempting to set key of size [%zd] for "
157638268498SDave Hansen 		       "cipher [%s]; rc = [%d]\n", *key_size, full_alg_name,
157738268498SDave Hansen 		       rc);
1578237fead6SMichael Halcrow 		rc = -EINVAL;
1579237fead6SMichael Halcrow 		goto out;
1580237fead6SMichael Halcrow 	}
1581237fead6SMichael Halcrow out:
1582ece550f5SDan Carpenter 	kfree(full_alg_name);
1583237fead6SMichael Halcrow 	return rc;
1584237fead6SMichael Halcrow }
1585f4aad16aSMichael Halcrow 
1586f4aad16aSMichael Halcrow struct kmem_cache *ecryptfs_key_tfm_cache;
15877896b631SAdrian Bunk static struct list_head key_tfm_list;
1588902af369SZheng Yongjun DEFINE_MUTEX(key_tfm_list_mutex);
1589f4aad16aSMichael Halcrow 
15907371a382SJerome Marchand int __init ecryptfs_init_crypto(void)
1591f4aad16aSMichael Halcrow {
1592f4aad16aSMichael Halcrow 	INIT_LIST_HEAD(&key_tfm_list);
1593f4aad16aSMichael Halcrow 	return 0;
1594f4aad16aSMichael Halcrow }
1595f4aad16aSMichael Halcrow 
1596af440f52SEric Sandeen /**
1597af440f52SEric Sandeen  * ecryptfs_destroy_crypto - free all cached key_tfms on key_tfm_list
1598af440f52SEric Sandeen  *
1599af440f52SEric Sandeen  * Called only at module unload time
1600af440f52SEric Sandeen  */
1601fcd12835SMichael Halcrow int ecryptfs_destroy_crypto(void)
1602f4aad16aSMichael Halcrow {
1603f4aad16aSMichael Halcrow 	struct ecryptfs_key_tfm *key_tfm, *key_tfm_tmp;
1604f4aad16aSMichael Halcrow 
1605f4aad16aSMichael Halcrow 	mutex_lock(&key_tfm_list_mutex);
1606f4aad16aSMichael Halcrow 	list_for_each_entry_safe(key_tfm, key_tfm_tmp, &key_tfm_list,
1607f4aad16aSMichael Halcrow 				 key_tfm_list) {
1608f4aad16aSMichael Halcrow 		list_del(&key_tfm->key_tfm_list);
16093095e8e3SHerbert Xu 		crypto_free_skcipher(key_tfm->key_tfm);
1610f4aad16aSMichael Halcrow 		kmem_cache_free(ecryptfs_key_tfm_cache, key_tfm);
1611f4aad16aSMichael Halcrow 	}
1612f4aad16aSMichael Halcrow 	mutex_unlock(&key_tfm_list_mutex);
1613f4aad16aSMichael Halcrow 	return 0;
1614f4aad16aSMichael Halcrow }
1615f4aad16aSMichael Halcrow 
1616f4aad16aSMichael Halcrow int
1617f4aad16aSMichael Halcrow ecryptfs_add_new_key_tfm(struct ecryptfs_key_tfm **key_tfm, char *cipher_name,
1618f4aad16aSMichael Halcrow 			 size_t key_size)
1619f4aad16aSMichael Halcrow {
1620f4aad16aSMichael Halcrow 	struct ecryptfs_key_tfm *tmp_tfm;
1621f4aad16aSMichael Halcrow 	int rc = 0;
1622f4aad16aSMichael Halcrow 
1623af440f52SEric Sandeen 	BUG_ON(!mutex_is_locked(&key_tfm_list_mutex));
1624af440f52SEric Sandeen 
1625f4aad16aSMichael Halcrow 	tmp_tfm = kmem_cache_alloc(ecryptfs_key_tfm_cache, GFP_KERNEL);
16265032f360SMarkus Elfring 	if (key_tfm)
1627f4aad16aSMichael Halcrow 		(*key_tfm) = tmp_tfm;
1628f4aad16aSMichael Halcrow 	if (!tmp_tfm) {
1629f4aad16aSMichael Halcrow 		rc = -ENOMEM;
1630f4aad16aSMichael Halcrow 		goto out;
1631f4aad16aSMichael Halcrow 	}
1632f4aad16aSMichael Halcrow 	mutex_init(&tmp_tfm->key_tfm_mutex);
1633f4aad16aSMichael Halcrow 	strncpy(tmp_tfm->cipher_name, cipher_name,
1634f4aad16aSMichael Halcrow 		ECRYPTFS_MAX_CIPHER_NAME_SIZE);
1635b8862906SEric Sandeen 	tmp_tfm->cipher_name[ECRYPTFS_MAX_CIPHER_NAME_SIZE] = '\0';
1636f4aad16aSMichael Halcrow 	tmp_tfm->key_size = key_size;
16375dda6992SMichael Halcrow 	rc = ecryptfs_process_key_cipher(&tmp_tfm->key_tfm,
1638f4aad16aSMichael Halcrow 					 tmp_tfm->cipher_name,
16395dda6992SMichael Halcrow 					 &tmp_tfm->key_size);
16405dda6992SMichael Halcrow 	if (rc) {
1641f4aad16aSMichael Halcrow 		printk(KERN_ERR "Error attempting to initialize key TFM "
1642f4aad16aSMichael Halcrow 		       "cipher with name = [%s]; rc = [%d]\n",
1643f4aad16aSMichael Halcrow 		       tmp_tfm->cipher_name, rc);
1644f4aad16aSMichael Halcrow 		kmem_cache_free(ecryptfs_key_tfm_cache, tmp_tfm);
16455032f360SMarkus Elfring 		if (key_tfm)
1646f4aad16aSMichael Halcrow 			(*key_tfm) = NULL;
1647f4aad16aSMichael Halcrow 		goto out;
1648f4aad16aSMichael Halcrow 	}
1649f4aad16aSMichael Halcrow 	list_add(&tmp_tfm->key_tfm_list, &key_tfm_list);
1650f4aad16aSMichael Halcrow out:
1651f4aad16aSMichael Halcrow 	return rc;
1652f4aad16aSMichael Halcrow }
1653f4aad16aSMichael Halcrow 
1654af440f52SEric Sandeen /**
1655af440f52SEric Sandeen  * ecryptfs_tfm_exists - Search for existing tfm for cipher_name.
1656af440f52SEric Sandeen  * @cipher_name: the name of the cipher to search for
1657af440f52SEric Sandeen  * @key_tfm: set to corresponding tfm if found
1658af440f52SEric Sandeen  *
1659af440f52SEric Sandeen  * Searches for cached key_tfm matching @cipher_name
1660af440f52SEric Sandeen  * Must be called with &key_tfm_list_mutex held
1661af440f52SEric Sandeen  * Returns 1 if found, with @key_tfm set
1662af440f52SEric Sandeen  * Returns 0 if not found, with @key_tfm set to NULL
1663af440f52SEric Sandeen  */
1664af440f52SEric Sandeen int ecryptfs_tfm_exists(char *cipher_name, struct ecryptfs_key_tfm **key_tfm)
1665af440f52SEric Sandeen {
1666af440f52SEric Sandeen 	struct ecryptfs_key_tfm *tmp_key_tfm;
1667af440f52SEric Sandeen 
1668af440f52SEric Sandeen 	BUG_ON(!mutex_is_locked(&key_tfm_list_mutex));
1669af440f52SEric Sandeen 
1670af440f52SEric Sandeen 	list_for_each_entry(tmp_key_tfm, &key_tfm_list, key_tfm_list) {
1671af440f52SEric Sandeen 		if (strcmp(tmp_key_tfm->cipher_name, cipher_name) == 0) {
1672af440f52SEric Sandeen 			if (key_tfm)
1673af440f52SEric Sandeen 				(*key_tfm) = tmp_key_tfm;
1674af440f52SEric Sandeen 			return 1;
1675af440f52SEric Sandeen 		}
1676af440f52SEric Sandeen 	}
1677af440f52SEric Sandeen 	if (key_tfm)
1678af440f52SEric Sandeen 		(*key_tfm) = NULL;
1679af440f52SEric Sandeen 	return 0;
1680af440f52SEric Sandeen }
1681af440f52SEric Sandeen 
1682af440f52SEric Sandeen /**
1683af440f52SEric Sandeen  * ecryptfs_get_tfm_and_mutex_for_cipher_name
1684af440f52SEric Sandeen  *
1685af440f52SEric Sandeen  * @tfm: set to cached tfm found, or new tfm created
1686af440f52SEric Sandeen  * @tfm_mutex: set to mutex for cached tfm found, or new tfm created
1687af440f52SEric Sandeen  * @cipher_name: the name of the cipher to search for and/or add
1688af440f52SEric Sandeen  *
1689af440f52SEric Sandeen  * Sets pointers to @tfm & @tfm_mutex matching @cipher_name.
1690af440f52SEric Sandeen  * Searches for cached item first, and creates new if not found.
1691af440f52SEric Sandeen  * Returns 0 on success, non-zero if adding new cipher failed
1692af440f52SEric Sandeen  */
16933095e8e3SHerbert Xu int ecryptfs_get_tfm_and_mutex_for_cipher_name(struct crypto_skcipher **tfm,
1694f4aad16aSMichael Halcrow 					       struct mutex **tfm_mutex,
1695f4aad16aSMichael Halcrow 					       char *cipher_name)
1696f4aad16aSMichael Halcrow {
1697f4aad16aSMichael Halcrow 	struct ecryptfs_key_tfm *key_tfm;
1698f4aad16aSMichael Halcrow 	int rc = 0;
1699f4aad16aSMichael Halcrow 
1700f4aad16aSMichael Halcrow 	(*tfm) = NULL;
1701f4aad16aSMichael Halcrow 	(*tfm_mutex) = NULL;
1702af440f52SEric Sandeen 
1703f4aad16aSMichael Halcrow 	mutex_lock(&key_tfm_list_mutex);
1704af440f52SEric Sandeen 	if (!ecryptfs_tfm_exists(cipher_name, &key_tfm)) {
17055dda6992SMichael Halcrow 		rc = ecryptfs_add_new_key_tfm(&key_tfm, cipher_name, 0);
17065dda6992SMichael Halcrow 		if (rc) {
1707af440f52SEric Sandeen 			printk(KERN_ERR "Error adding new key_tfm to list; "
1708af440f52SEric Sandeen 					"rc = [%d]\n", rc);
1709f4aad16aSMichael Halcrow 			goto out;
1710f4aad16aSMichael Halcrow 		}
1711af440f52SEric Sandeen 	}
1712f4aad16aSMichael Halcrow 	(*tfm) = key_tfm->key_tfm;
1713f4aad16aSMichael Halcrow 	(*tfm_mutex) = &key_tfm->key_tfm_mutex;
1714f4aad16aSMichael Halcrow out:
171571fd5179SCyrill Gorcunov 	mutex_unlock(&key_tfm_list_mutex);
1716f4aad16aSMichael Halcrow 	return rc;
1717f4aad16aSMichael Halcrow }
171851ca58dcSMichael Halcrow 
171951ca58dcSMichael Halcrow /* 64 characters forming a 6-bit target field */
172051ca58dcSMichael Halcrow static unsigned char *portable_filename_chars = ("-.0123456789ABCD"
172151ca58dcSMichael Halcrow 						 "EFGHIJKLMNOPQRST"
172251ca58dcSMichael Halcrow 						 "UVWXYZabcdefghij"
172351ca58dcSMichael Halcrow 						 "klmnopqrstuvwxyz");
172451ca58dcSMichael Halcrow 
172551ca58dcSMichael Halcrow /* We could either offset on every reverse map or just pad some 0x00's
172651ca58dcSMichael Halcrow  * at the front here */
17270f751e64STyler Hicks static const unsigned char filename_rev_map[256] = {
172851ca58dcSMichael Halcrow 	0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, /* 7 */
172951ca58dcSMichael Halcrow 	0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, /* 15 */
173051ca58dcSMichael Halcrow 	0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, /* 23 */
173151ca58dcSMichael Halcrow 	0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, /* 31 */
173251ca58dcSMichael Halcrow 	0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, /* 39 */
173351ca58dcSMichael Halcrow 	0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x01, 0x00, /* 47 */
173451ca58dcSMichael Halcrow 	0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, /* 55 */
173551ca58dcSMichael Halcrow 	0x0A, 0x0B, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, /* 63 */
173651ca58dcSMichael Halcrow 	0x00, 0x0C, 0x0D, 0x0E, 0x0F, 0x10, 0x11, 0x12, /* 71 */
173751ca58dcSMichael Halcrow 	0x13, 0x14, 0x15, 0x16, 0x17, 0x18, 0x19, 0x1A, /* 79 */
173851ca58dcSMichael Halcrow 	0x1B, 0x1C, 0x1D, 0x1E, 0x1F, 0x20, 0x21, 0x22, /* 87 */
173951ca58dcSMichael Halcrow 	0x23, 0x24, 0x25, 0x00, 0x00, 0x00, 0x00, 0x00, /* 95 */
174051ca58dcSMichael Halcrow 	0x00, 0x26, 0x27, 0x28, 0x29, 0x2A, 0x2B, 0x2C, /* 103 */
174151ca58dcSMichael Halcrow 	0x2D, 0x2E, 0x2F, 0x30, 0x31, 0x32, 0x33, 0x34, /* 111 */
174251ca58dcSMichael Halcrow 	0x35, 0x36, 0x37, 0x38, 0x39, 0x3A, 0x3B, 0x3C, /* 119 */
17430f751e64STyler Hicks 	0x3D, 0x3E, 0x3F /* 123 - 255 initialized to 0x00 */
174451ca58dcSMichael Halcrow };
174551ca58dcSMichael Halcrow 
174651ca58dcSMichael Halcrow /**
174751ca58dcSMichael Halcrow  * ecryptfs_encode_for_filename
174851ca58dcSMichael Halcrow  * @dst: Destination location for encoded filename
174951ca58dcSMichael Halcrow  * @dst_size: Size of the encoded filename in bytes
175051ca58dcSMichael Halcrow  * @src: Source location for the filename to encode
175151ca58dcSMichael Halcrow  * @src_size: Size of the source in bytes
175251ca58dcSMichael Halcrow  */
175337028758SCong Ding static void ecryptfs_encode_for_filename(unsigned char *dst, size_t *dst_size,
175451ca58dcSMichael Halcrow 				  unsigned char *src, size_t src_size)
175551ca58dcSMichael Halcrow {
175651ca58dcSMichael Halcrow 	size_t num_blocks;
175751ca58dcSMichael Halcrow 	size_t block_num = 0;
175851ca58dcSMichael Halcrow 	size_t dst_offset = 0;
175951ca58dcSMichael Halcrow 	unsigned char last_block[3];
176051ca58dcSMichael Halcrow 
176151ca58dcSMichael Halcrow 	if (src_size == 0) {
176251ca58dcSMichael Halcrow 		(*dst_size) = 0;
176351ca58dcSMichael Halcrow 		goto out;
176451ca58dcSMichael Halcrow 	}
176551ca58dcSMichael Halcrow 	num_blocks = (src_size / 3);
176651ca58dcSMichael Halcrow 	if ((src_size % 3) == 0) {
176751ca58dcSMichael Halcrow 		memcpy(last_block, (&src[src_size - 3]), 3);
176851ca58dcSMichael Halcrow 	} else {
176951ca58dcSMichael Halcrow 		num_blocks++;
177051ca58dcSMichael Halcrow 		last_block[2] = 0x00;
177151ca58dcSMichael Halcrow 		switch (src_size % 3) {
177251ca58dcSMichael Halcrow 		case 1:
177351ca58dcSMichael Halcrow 			last_block[0] = src[src_size - 1];
177451ca58dcSMichael Halcrow 			last_block[1] = 0x00;
177551ca58dcSMichael Halcrow 			break;
177651ca58dcSMichael Halcrow 		case 2:
177751ca58dcSMichael Halcrow 			last_block[0] = src[src_size - 2];
177851ca58dcSMichael Halcrow 			last_block[1] = src[src_size - 1];
177951ca58dcSMichael Halcrow 		}
178051ca58dcSMichael Halcrow 	}
178151ca58dcSMichael Halcrow 	(*dst_size) = (num_blocks * 4);
178251ca58dcSMichael Halcrow 	if (!dst)
178351ca58dcSMichael Halcrow 		goto out;
178451ca58dcSMichael Halcrow 	while (block_num < num_blocks) {
178551ca58dcSMichael Halcrow 		unsigned char *src_block;
178651ca58dcSMichael Halcrow 		unsigned char dst_block[4];
178751ca58dcSMichael Halcrow 
178851ca58dcSMichael Halcrow 		if (block_num == (num_blocks - 1))
178951ca58dcSMichael Halcrow 			src_block = last_block;
179051ca58dcSMichael Halcrow 		else
179151ca58dcSMichael Halcrow 			src_block = &src[block_num * 3];
179251ca58dcSMichael Halcrow 		dst_block[0] = ((src_block[0] >> 2) & 0x3F);
179351ca58dcSMichael Halcrow 		dst_block[1] = (((src_block[0] << 4) & 0x30)
179451ca58dcSMichael Halcrow 				| ((src_block[1] >> 4) & 0x0F));
179551ca58dcSMichael Halcrow 		dst_block[2] = (((src_block[1] << 2) & 0x3C)
179651ca58dcSMichael Halcrow 				| ((src_block[2] >> 6) & 0x03));
179751ca58dcSMichael Halcrow 		dst_block[3] = (src_block[2] & 0x3F);
179851ca58dcSMichael Halcrow 		dst[dst_offset++] = portable_filename_chars[dst_block[0]];
179951ca58dcSMichael Halcrow 		dst[dst_offset++] = portable_filename_chars[dst_block[1]];
180051ca58dcSMichael Halcrow 		dst[dst_offset++] = portable_filename_chars[dst_block[2]];
180151ca58dcSMichael Halcrow 		dst[dst_offset++] = portable_filename_chars[dst_block[3]];
180251ca58dcSMichael Halcrow 		block_num++;
180351ca58dcSMichael Halcrow 	}
180451ca58dcSMichael Halcrow out:
180551ca58dcSMichael Halcrow 	return;
180651ca58dcSMichael Halcrow }
180751ca58dcSMichael Halcrow 
18084a26620dSTyler Hicks static size_t ecryptfs_max_decoded_size(size_t encoded_size)
18094a26620dSTyler Hicks {
18104a26620dSTyler Hicks 	/* Not exact; conservatively long. Every block of 4
18114a26620dSTyler Hicks 	 * encoded characters decodes into a block of 3
18124a26620dSTyler Hicks 	 * decoded characters. This segment of code provides
18134a26620dSTyler Hicks 	 * the caller with the maximum amount of allocated
18144a26620dSTyler Hicks 	 * space that @dst will need to point to in a
18154a26620dSTyler Hicks 	 * subsequent call. */
18164a26620dSTyler Hicks 	return ((encoded_size + 1) * 3) / 4;
18174a26620dSTyler Hicks }
18184a26620dSTyler Hicks 
181971c11c37SMichael Halcrow /**
182071c11c37SMichael Halcrow  * ecryptfs_decode_from_filename
182171c11c37SMichael Halcrow  * @dst: If NULL, this function only sets @dst_size and returns. If
182271c11c37SMichael Halcrow  *       non-NULL, this function decodes the encoded octets in @src
182371c11c37SMichael Halcrow  *       into the memory that @dst points to.
182471c11c37SMichael Halcrow  * @dst_size: Set to the size of the decoded string.
182571c11c37SMichael Halcrow  * @src: The encoded set of octets to decode.
182671c11c37SMichael Halcrow  * @src_size: The size of the encoded set of octets to decode.
182771c11c37SMichael Halcrow  */
182871c11c37SMichael Halcrow static void
182971c11c37SMichael Halcrow ecryptfs_decode_from_filename(unsigned char *dst, size_t *dst_size,
183051ca58dcSMichael Halcrow 			      const unsigned char *src, size_t src_size)
183151ca58dcSMichael Halcrow {
183251ca58dcSMichael Halcrow 	u8 current_bit_offset = 0;
183351ca58dcSMichael Halcrow 	size_t src_byte_offset = 0;
183451ca58dcSMichael Halcrow 	size_t dst_byte_offset = 0;
183551ca58dcSMichael Halcrow 
18365032f360SMarkus Elfring 	if (!dst) {
18374a26620dSTyler Hicks 		(*dst_size) = ecryptfs_max_decoded_size(src_size);
183851ca58dcSMichael Halcrow 		goto out;
183951ca58dcSMichael Halcrow 	}
184051ca58dcSMichael Halcrow 	while (src_byte_offset < src_size) {
184151ca58dcSMichael Halcrow 		unsigned char src_byte =
184251ca58dcSMichael Halcrow 				filename_rev_map[(int)src[src_byte_offset]];
184351ca58dcSMichael Halcrow 
184451ca58dcSMichael Halcrow 		switch (current_bit_offset) {
184551ca58dcSMichael Halcrow 		case 0:
184651ca58dcSMichael Halcrow 			dst[dst_byte_offset] = (src_byte << 2);
184751ca58dcSMichael Halcrow 			current_bit_offset = 6;
184851ca58dcSMichael Halcrow 			break;
184951ca58dcSMichael Halcrow 		case 6:
185051ca58dcSMichael Halcrow 			dst[dst_byte_offset++] |= (src_byte >> 4);
185151ca58dcSMichael Halcrow 			dst[dst_byte_offset] = ((src_byte & 0xF)
185251ca58dcSMichael Halcrow 						 << 4);
185351ca58dcSMichael Halcrow 			current_bit_offset = 4;
185451ca58dcSMichael Halcrow 			break;
185551ca58dcSMichael Halcrow 		case 4:
185651ca58dcSMichael Halcrow 			dst[dst_byte_offset++] |= (src_byte >> 2);
185751ca58dcSMichael Halcrow 			dst[dst_byte_offset] = (src_byte << 6);
185851ca58dcSMichael Halcrow 			current_bit_offset = 2;
185951ca58dcSMichael Halcrow 			break;
186051ca58dcSMichael Halcrow 		case 2:
186151ca58dcSMichael Halcrow 			dst[dst_byte_offset++] |= (src_byte);
186251ca58dcSMichael Halcrow 			current_bit_offset = 0;
186351ca58dcSMichael Halcrow 			break;
186451ca58dcSMichael Halcrow 		}
186551ca58dcSMichael Halcrow 		src_byte_offset++;
186651ca58dcSMichael Halcrow 	}
186751ca58dcSMichael Halcrow 	(*dst_size) = dst_byte_offset;
186851ca58dcSMichael Halcrow out:
186971c11c37SMichael Halcrow 	return;
187051ca58dcSMichael Halcrow }
187151ca58dcSMichael Halcrow 
187251ca58dcSMichael Halcrow /**
187351ca58dcSMichael Halcrow  * ecryptfs_encrypt_and_encode_filename - converts a plaintext file name to cipher text
18745da877eaSLee Jones  * @encoded_name: The encrypted name
18755da877eaSLee Jones  * @encoded_name_size: Length of the encrypted name
18765da877eaSLee Jones  * @mount_crypt_stat: The crypt_stat struct associated with the file name to encode
187751ca58dcSMichael Halcrow  * @name: The plaintext name
18785da877eaSLee Jones  * @name_size: The length of the plaintext name
187951ca58dcSMichael Halcrow  *
188051ca58dcSMichael Halcrow  * Encrypts and encodes a filename into something that constitutes a
188151ca58dcSMichael Halcrow  * valid filename for a filesystem, with printable characters.
188251ca58dcSMichael Halcrow  *
188351ca58dcSMichael Halcrow  * We assume that we have a properly initialized crypto context,
188451ca58dcSMichael Halcrow  * pointed to by crypt_stat->tfm.
188551ca58dcSMichael Halcrow  *
188651ca58dcSMichael Halcrow  * Returns zero on success; non-zero on otherwise
188751ca58dcSMichael Halcrow  */
188851ca58dcSMichael Halcrow int ecryptfs_encrypt_and_encode_filename(
188951ca58dcSMichael Halcrow 	char **encoded_name,
189051ca58dcSMichael Halcrow 	size_t *encoded_name_size,
189151ca58dcSMichael Halcrow 	struct ecryptfs_mount_crypt_stat *mount_crypt_stat,
189251ca58dcSMichael Halcrow 	const char *name, size_t name_size)
189351ca58dcSMichael Halcrow {
189451ca58dcSMichael Halcrow 	size_t encoded_name_no_prefix_size;
189551ca58dcSMichael Halcrow 	int rc = 0;
189651ca58dcSMichael Halcrow 
189751ca58dcSMichael Halcrow 	(*encoded_name) = NULL;
189851ca58dcSMichael Halcrow 	(*encoded_name_size) = 0;
189997c31606SAl Viro 	if (mount_crypt_stat && (mount_crypt_stat->flags
190097c31606SAl Viro 				     & ECRYPTFS_GLOBAL_ENCRYPT_FILENAMES)) {
190151ca58dcSMichael Halcrow 		struct ecryptfs_filename *filename;
190251ca58dcSMichael Halcrow 
190351ca58dcSMichael Halcrow 		filename = kzalloc(sizeof(*filename), GFP_KERNEL);
190451ca58dcSMichael Halcrow 		if (!filename) {
190551ca58dcSMichael Halcrow 			rc = -ENOMEM;
190651ca58dcSMichael Halcrow 			goto out;
190751ca58dcSMichael Halcrow 		}
190851ca58dcSMichael Halcrow 		filename->filename = (char *)name;
190951ca58dcSMichael Halcrow 		filename->filename_size = name_size;
191097c31606SAl Viro 		rc = ecryptfs_encrypt_filename(filename, mount_crypt_stat);
191151ca58dcSMichael Halcrow 		if (rc) {
191251ca58dcSMichael Halcrow 			printk(KERN_ERR "%s: Error attempting to encrypt "
191351ca58dcSMichael Halcrow 			       "filename; rc = [%d]\n", __func__, rc);
191451ca58dcSMichael Halcrow 			kfree(filename);
191551ca58dcSMichael Halcrow 			goto out;
191651ca58dcSMichael Halcrow 		}
191751ca58dcSMichael Halcrow 		ecryptfs_encode_for_filename(
191851ca58dcSMichael Halcrow 			NULL, &encoded_name_no_prefix_size,
191951ca58dcSMichael Halcrow 			filename->encrypted_filename,
192051ca58dcSMichael Halcrow 			filename->encrypted_filename_size);
192197c31606SAl Viro 		if (mount_crypt_stat
192251ca58dcSMichael Halcrow 			&& (mount_crypt_stat->flags
192397c31606SAl Viro 			    & ECRYPTFS_GLOBAL_ENCFN_USE_MOUNT_FNEK))
192451ca58dcSMichael Halcrow 			(*encoded_name_size) =
192551ca58dcSMichael Halcrow 				(ECRYPTFS_FNEK_ENCRYPTED_FILENAME_PREFIX_SIZE
192651ca58dcSMichael Halcrow 				 + encoded_name_no_prefix_size);
192751ca58dcSMichael Halcrow 		else
192851ca58dcSMichael Halcrow 			(*encoded_name_size) =
192951ca58dcSMichael Halcrow 				(ECRYPTFS_FEK_ENCRYPTED_FILENAME_PREFIX_SIZE
193051ca58dcSMichael Halcrow 				 + encoded_name_no_prefix_size);
193151ca58dcSMichael Halcrow 		(*encoded_name) = kmalloc((*encoded_name_size) + 1, GFP_KERNEL);
193251ca58dcSMichael Halcrow 		if (!(*encoded_name)) {
193351ca58dcSMichael Halcrow 			rc = -ENOMEM;
193451ca58dcSMichael Halcrow 			kfree(filename->encrypted_filename);
193551ca58dcSMichael Halcrow 			kfree(filename);
193651ca58dcSMichael Halcrow 			goto out;
193751ca58dcSMichael Halcrow 		}
193897c31606SAl Viro 		if (mount_crypt_stat
193951ca58dcSMichael Halcrow 			&& (mount_crypt_stat->flags
194097c31606SAl Viro 			    & ECRYPTFS_GLOBAL_ENCFN_USE_MOUNT_FNEK)) {
194151ca58dcSMichael Halcrow 			memcpy((*encoded_name),
194251ca58dcSMichael Halcrow 			       ECRYPTFS_FNEK_ENCRYPTED_FILENAME_PREFIX,
194351ca58dcSMichael Halcrow 			       ECRYPTFS_FNEK_ENCRYPTED_FILENAME_PREFIX_SIZE);
194451ca58dcSMichael Halcrow 			ecryptfs_encode_for_filename(
194551ca58dcSMichael Halcrow 			    ((*encoded_name)
194651ca58dcSMichael Halcrow 			     + ECRYPTFS_FNEK_ENCRYPTED_FILENAME_PREFIX_SIZE),
194751ca58dcSMichael Halcrow 			    &encoded_name_no_prefix_size,
194851ca58dcSMichael Halcrow 			    filename->encrypted_filename,
194951ca58dcSMichael Halcrow 			    filename->encrypted_filename_size);
195051ca58dcSMichael Halcrow 			(*encoded_name_size) =
195151ca58dcSMichael Halcrow 				(ECRYPTFS_FNEK_ENCRYPTED_FILENAME_PREFIX_SIZE
195251ca58dcSMichael Halcrow 				 + encoded_name_no_prefix_size);
195351ca58dcSMichael Halcrow 			(*encoded_name)[(*encoded_name_size)] = '\0';
195451ca58dcSMichael Halcrow 		} else {
1955df6ad33bSTyler Hicks 			rc = -EOPNOTSUPP;
195651ca58dcSMichael Halcrow 		}
195751ca58dcSMichael Halcrow 		if (rc) {
195851ca58dcSMichael Halcrow 			printk(KERN_ERR "%s: Error attempting to encode "
195951ca58dcSMichael Halcrow 			       "encrypted filename; rc = [%d]\n", __func__,
196051ca58dcSMichael Halcrow 			       rc);
196151ca58dcSMichael Halcrow 			kfree((*encoded_name));
196251ca58dcSMichael Halcrow 			(*encoded_name) = NULL;
196351ca58dcSMichael Halcrow 			(*encoded_name_size) = 0;
196451ca58dcSMichael Halcrow 		}
196551ca58dcSMichael Halcrow 		kfree(filename->encrypted_filename);
196651ca58dcSMichael Halcrow 		kfree(filename);
196751ca58dcSMichael Halcrow 	} else {
196851ca58dcSMichael Halcrow 		rc = ecryptfs_copy_filename(encoded_name,
196951ca58dcSMichael Halcrow 					    encoded_name_size,
197051ca58dcSMichael Halcrow 					    name, name_size);
197151ca58dcSMichael Halcrow 	}
197251ca58dcSMichael Halcrow out:
197351ca58dcSMichael Halcrow 	return rc;
197451ca58dcSMichael Halcrow }
197551ca58dcSMichael Halcrow 
1976e86281e7STyler Hicks static bool is_dot_dotdot(const char *name, size_t name_size)
1977e86281e7STyler Hicks {
1978e86281e7STyler Hicks 	if (name_size == 1 && name[0] == '.')
1979e86281e7STyler Hicks 		return true;
1980e86281e7STyler Hicks 	else if (name_size == 2 && name[0] == '.' && name[1] == '.')
1981e86281e7STyler Hicks 		return true;
1982e86281e7STyler Hicks 
1983e86281e7STyler Hicks 	return false;
1984e86281e7STyler Hicks }
1985e86281e7STyler Hicks 
198651ca58dcSMichael Halcrow /**
198751ca58dcSMichael Halcrow  * ecryptfs_decode_and_decrypt_filename - converts the encoded cipher text name to decoded plaintext
198851ca58dcSMichael Halcrow  * @plaintext_name: The plaintext name
198951ca58dcSMichael Halcrow  * @plaintext_name_size: The plaintext name size
19905da877eaSLee Jones  * @sb: Ecryptfs's super_block
199151ca58dcSMichael Halcrow  * @name: The filename in cipher text
199251ca58dcSMichael Halcrow  * @name_size: The cipher text name size
199351ca58dcSMichael Halcrow  *
199451ca58dcSMichael Halcrow  * Decrypts and decodes the filename.
199551ca58dcSMichael Halcrow  *
199651ca58dcSMichael Halcrow  * Returns zero on error; non-zero otherwise
199751ca58dcSMichael Halcrow  */
199851ca58dcSMichael Halcrow int ecryptfs_decode_and_decrypt_filename(char **plaintext_name,
199951ca58dcSMichael Halcrow 					 size_t *plaintext_name_size,
20000747fdb2SAl Viro 					 struct super_block *sb,
200151ca58dcSMichael Halcrow 					 const char *name, size_t name_size)
200251ca58dcSMichael Halcrow {
20032aac0cf8STyler Hicks 	struct ecryptfs_mount_crypt_stat *mount_crypt_stat =
20040747fdb2SAl Viro 		&ecryptfs_superblock_to_private(sb)->mount_crypt_stat;
200551ca58dcSMichael Halcrow 	char *decoded_name;
200651ca58dcSMichael Halcrow 	size_t decoded_name_size;
200751ca58dcSMichael Halcrow 	size_t packet_size;
200851ca58dcSMichael Halcrow 	int rc = 0;
200951ca58dcSMichael Halcrow 
2010e86281e7STyler Hicks 	if ((mount_crypt_stat->flags & ECRYPTFS_GLOBAL_ENCRYPT_FILENAMES) &&
2011e86281e7STyler Hicks 	    !(mount_crypt_stat->flags & ECRYPTFS_ENCRYPTED_VIEW_ENABLED)) {
2012e86281e7STyler Hicks 		if (is_dot_dotdot(name, name_size)) {
2013e86281e7STyler Hicks 			rc = ecryptfs_copy_filename(plaintext_name,
2014e86281e7STyler Hicks 						    plaintext_name_size,
2015e86281e7STyler Hicks 						    name, name_size);
2016e86281e7STyler Hicks 			goto out;
2017e86281e7STyler Hicks 		}
2018e86281e7STyler Hicks 
2019e86281e7STyler Hicks 		if (name_size <= ECRYPTFS_FNEK_ENCRYPTED_FILENAME_PREFIX_SIZE ||
2020e86281e7STyler Hicks 		    strncmp(name, ECRYPTFS_FNEK_ENCRYPTED_FILENAME_PREFIX,
2021e86281e7STyler Hicks 			    ECRYPTFS_FNEK_ENCRYPTED_FILENAME_PREFIX_SIZE)) {
2022e86281e7STyler Hicks 			rc = -EINVAL;
2023e86281e7STyler Hicks 			goto out;
2024e86281e7STyler Hicks 		}
202551ca58dcSMichael Halcrow 
202651ca58dcSMichael Halcrow 		name += ECRYPTFS_FNEK_ENCRYPTED_FILENAME_PREFIX_SIZE;
202751ca58dcSMichael Halcrow 		name_size -= ECRYPTFS_FNEK_ENCRYPTED_FILENAME_PREFIX_SIZE;
202871c11c37SMichael Halcrow 		ecryptfs_decode_from_filename(NULL, &decoded_name_size,
202951ca58dcSMichael Halcrow 					      name, name_size);
203051ca58dcSMichael Halcrow 		decoded_name = kmalloc(decoded_name_size, GFP_KERNEL);
203151ca58dcSMichael Halcrow 		if (!decoded_name) {
203251ca58dcSMichael Halcrow 			rc = -ENOMEM;
203351ca58dcSMichael Halcrow 			goto out;
203451ca58dcSMichael Halcrow 		}
203571c11c37SMichael Halcrow 		ecryptfs_decode_from_filename(decoded_name, &decoded_name_size,
203651ca58dcSMichael Halcrow 					      name, name_size);
203751ca58dcSMichael Halcrow 		rc = ecryptfs_parse_tag_70_packet(plaintext_name,
203851ca58dcSMichael Halcrow 						  plaintext_name_size,
203951ca58dcSMichael Halcrow 						  &packet_size,
204051ca58dcSMichael Halcrow 						  mount_crypt_stat,
204151ca58dcSMichael Halcrow 						  decoded_name,
204251ca58dcSMichael Halcrow 						  decoded_name_size);
204351ca58dcSMichael Halcrow 		if (rc) {
2044e86281e7STyler Hicks 			ecryptfs_printk(KERN_DEBUG,
2045e86281e7STyler Hicks 					"%s: Could not parse tag 70 packet from filename\n",
2046e86281e7STyler Hicks 					__func__);
204751ca58dcSMichael Halcrow 			goto out_free;
204851ca58dcSMichael Halcrow 		}
204951ca58dcSMichael Halcrow 	} else {
205051ca58dcSMichael Halcrow 		rc = ecryptfs_copy_filename(plaintext_name,
205151ca58dcSMichael Halcrow 					    plaintext_name_size,
205251ca58dcSMichael Halcrow 					    name, name_size);
205351ca58dcSMichael Halcrow 		goto out;
205451ca58dcSMichael Halcrow 	}
205551ca58dcSMichael Halcrow out_free:
205651ca58dcSMichael Halcrow 	kfree(decoded_name);
205751ca58dcSMichael Halcrow out:
205851ca58dcSMichael Halcrow 	return rc;
205951ca58dcSMichael Halcrow }
20604a26620dSTyler Hicks 
20614a26620dSTyler Hicks #define ENC_NAME_MAX_BLOCKLEN_8_OR_16	143
20624a26620dSTyler Hicks 
20634a26620dSTyler Hicks int ecryptfs_set_f_namelen(long *namelen, long lower_namelen,
20644a26620dSTyler Hicks 			   struct ecryptfs_mount_crypt_stat *mount_crypt_stat)
20654a26620dSTyler Hicks {
20663095e8e3SHerbert Xu 	struct crypto_skcipher *tfm;
20674a26620dSTyler Hicks 	struct mutex *tfm_mutex;
20684a26620dSTyler Hicks 	size_t cipher_blocksize;
20694a26620dSTyler Hicks 	int rc;
20704a26620dSTyler Hicks 
20714a26620dSTyler Hicks 	if (!(mount_crypt_stat->flags & ECRYPTFS_GLOBAL_ENCRYPT_FILENAMES)) {
20724a26620dSTyler Hicks 		(*namelen) = lower_namelen;
20734a26620dSTyler Hicks 		return 0;
20744a26620dSTyler Hicks 	}
20754a26620dSTyler Hicks 
20763095e8e3SHerbert Xu 	rc = ecryptfs_get_tfm_and_mutex_for_cipher_name(&tfm, &tfm_mutex,
20774a26620dSTyler Hicks 			mount_crypt_stat->global_default_fn_cipher_name);
20784a26620dSTyler Hicks 	if (unlikely(rc)) {
20794a26620dSTyler Hicks 		(*namelen) = 0;
20804a26620dSTyler Hicks 		return rc;
20814a26620dSTyler Hicks 	}
20824a26620dSTyler Hicks 
20834a26620dSTyler Hicks 	mutex_lock(tfm_mutex);
20843095e8e3SHerbert Xu 	cipher_blocksize = crypto_skcipher_blocksize(tfm);
20854a26620dSTyler Hicks 	mutex_unlock(tfm_mutex);
20864a26620dSTyler Hicks 
20874a26620dSTyler Hicks 	/* Return an exact amount for the common cases */
20884a26620dSTyler Hicks 	if (lower_namelen == NAME_MAX
20894a26620dSTyler Hicks 	    && (cipher_blocksize == 8 || cipher_blocksize == 16)) {
20904a26620dSTyler Hicks 		(*namelen) = ENC_NAME_MAX_BLOCKLEN_8_OR_16;
20914a26620dSTyler Hicks 		return 0;
20924a26620dSTyler Hicks 	}
20934a26620dSTyler Hicks 
20944a26620dSTyler Hicks 	/* Return a safe estimate for the uncommon cases */
20954a26620dSTyler Hicks 	(*namelen) = lower_namelen;
20964a26620dSTyler Hicks 	(*namelen) -= ECRYPTFS_FNEK_ENCRYPTED_FILENAME_PREFIX_SIZE;
20974a26620dSTyler Hicks 	/* Since this is the max decoded size, subtract 1 "decoded block" len */
20984a26620dSTyler Hicks 	(*namelen) = ecryptfs_max_decoded_size(*namelen) - 3;
20994a26620dSTyler Hicks 	(*namelen) -= ECRYPTFS_TAG_70_MAX_METADATA_SIZE;
21004a26620dSTyler Hicks 	(*namelen) -= ECRYPTFS_FILENAME_MIN_RANDOM_PREPEND_BYTES;
21014a26620dSTyler Hicks 	/* Worst case is that the filename is padded nearly a full block size */
21024a26620dSTyler Hicks 	(*namelen) -= cipher_blocksize - 1;
21034a26620dSTyler Hicks 
21044a26620dSTyler Hicks 	if ((*namelen) < 0)
21054a26620dSTyler Hicks 		(*namelen) = 0;
21064a26620dSTyler Hicks 
21074a26620dSTyler Hicks 	return 0;
21084a26620dSTyler Hicks }
2109