blob: 3547708fb4e1f8877dd707d0b5f6091769842f08 [file] [log] [blame]
Michael Halcrow237fead2006-10-04 02:16:22 -07001/**
2 * eCryptfs: Linux filesystem encryption layer
3 *
4 * Copyright (C) 1997-2004 Erez Zadok
5 * Copyright (C) 2001-2004 Stony Brook University
Michael Halcrowdd2a3b72007-02-12 00:53:46 -08006 * Copyright (C) 2004-2007 International Business Machines Corp.
Michael Halcrow237fead2006-10-04 02:16:22 -07007 * Author(s): Michael A. Halcrow <mahalcro@us.ibm.com>
8 * Michael C. Thompson <mcthomps@us.ibm.com>
9 *
10 * This program is free software; you can redistribute it and/or
11 * modify it under the terms of the GNU General Public License as
12 * published by the Free Software Foundation; either version 2 of the
13 * License, or (at your option) any later version.
14 *
15 * This program is distributed in the hope that it will be useful, but
16 * WITHOUT ANY WARRANTY; without even the implied warranty of
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
18 * General Public License for more details.
19 *
20 * You should have received a copy of the GNU General Public License
21 * along with this program; if not, write to the Free Software
22 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA
23 * 02111-1307, USA.
24 */
25
26#include <linux/fs.h>
27#include <linux/mount.h>
28#include <linux/pagemap.h>
29#include <linux/random.h>
30#include <linux/compiler.h>
31#include <linux/key.h>
32#include <linux/namei.h>
33#include <linux/crypto.h>
34#include <linux/file.h>
35#include <linux/scatterlist.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090036#include <linux/slab.h>
Harvey Harrison29335c62008-07-23 21:30:06 -070037#include <asm/unaligned.h>
Michael Halcrow237fead2006-10-04 02:16:22 -070038#include "ecryptfs_kernel.h"
39
40static int
41ecryptfs_decrypt_page_offset(struct ecryptfs_crypt_stat *crypt_stat,
42 struct page *dst_page, int dst_offset,
43 struct page *src_page, int src_offset, int size,
44 unsigned char *iv);
45static int
46ecryptfs_encrypt_page_offset(struct ecryptfs_crypt_stat *crypt_stat,
47 struct page *dst_page, int dst_offset,
48 struct page *src_page, int src_offset, int size,
49 unsigned char *iv);
50
51/**
52 * ecryptfs_to_hex
53 * @dst: Buffer to take hex character representation of contents of
54 * src; must be at least of size (src_size * 2)
55 * @src: Buffer to be converted to a hex string respresentation
56 * @src_size: number of bytes to convert
57 */
58void ecryptfs_to_hex(char *dst, char *src, size_t src_size)
59{
60 int x;
61
62 for (x = 0; x < src_size; x++)
63 sprintf(&dst[x * 2], "%.2x", (unsigned char)src[x]);
64}
65
66/**
67 * ecryptfs_from_hex
68 * @dst: Buffer to take the bytes from src hex; must be at least of
69 * size (src_size / 2)
70 * @src: Buffer to be converted from a hex string respresentation to raw value
71 * @dst_size: size of dst buffer, or number of hex characters pairs to convert
72 */
73void ecryptfs_from_hex(char *dst, char *src, int dst_size)
74{
75 int x;
76 char tmp[3] = { 0, };
77
78 for (x = 0; x < dst_size; x++) {
79 tmp[0] = src[x * 2];
80 tmp[1] = src[x * 2 + 1];
81 dst[x] = (unsigned char)simple_strtol(tmp, NULL, 16);
82 }
83}
84
85/**
86 * ecryptfs_calculate_md5 - calculates the md5 of @src
87 * @dst: Pointer to 16 bytes of allocated memory
88 * @crypt_stat: Pointer to crypt_stat struct for the current inode
89 * @src: Data to be md5'd
90 * @len: Length of @src
91 *
92 * Uses the allocated crypto context that crypt_stat references to
93 * generate the MD5 sum of the contents of src.
94 */
95static int ecryptfs_calculate_md5(char *dst,
96 struct ecryptfs_crypt_stat *crypt_stat,
97 char *src, int len)
98{
Michael Halcrow237fead2006-10-04 02:16:22 -070099 struct scatterlist sg;
Michael Halcrow565d97242006-10-30 22:07:17 -0800100 struct hash_desc desc = {
101 .tfm = crypt_stat->hash_tfm,
102 .flags = CRYPTO_TFM_REQ_MAY_SLEEP
103 };
104 int rc = 0;
Michael Halcrow237fead2006-10-04 02:16:22 -0700105
Michael Halcrow565d97242006-10-30 22:07:17 -0800106 mutex_lock(&crypt_stat->cs_hash_tfm_mutex);
Michael Halcrow237fead2006-10-04 02:16:22 -0700107 sg_init_one(&sg, (u8 *)src, len);
Michael Halcrow565d97242006-10-30 22:07:17 -0800108 if (!desc.tfm) {
109 desc.tfm = crypto_alloc_hash(ECRYPTFS_DEFAULT_HASH, 0,
110 CRYPTO_ALG_ASYNC);
111 if (IS_ERR(desc.tfm)) {
112 rc = PTR_ERR(desc.tfm);
Michael Halcrow237fead2006-10-04 02:16:22 -0700113 ecryptfs_printk(KERN_ERR, "Error attempting to "
Michael Halcrow565d97242006-10-30 22:07:17 -0800114 "allocate crypto context; rc = [%d]\n",
115 rc);
Michael Halcrow237fead2006-10-04 02:16:22 -0700116 goto out;
117 }
Michael Halcrow565d97242006-10-30 22:07:17 -0800118 crypt_stat->hash_tfm = desc.tfm;
Michael Halcrow237fead2006-10-04 02:16:22 -0700119 }
Michael Halcrow8a29f2b2007-11-05 14:51:04 -0800120 rc = crypto_hash_init(&desc);
121 if (rc) {
122 printk(KERN_ERR
123 "%s: Error initializing crypto hash; rc = [%d]\n",
Harvey Harrison18d1dbf2008-04-29 00:59:48 -0700124 __func__, rc);
Michael Halcrow8a29f2b2007-11-05 14:51:04 -0800125 goto out;
126 }
127 rc = crypto_hash_update(&desc, &sg, len);
128 if (rc) {
129 printk(KERN_ERR
130 "%s: Error updating crypto hash; rc = [%d]\n",
Harvey Harrison18d1dbf2008-04-29 00:59:48 -0700131 __func__, rc);
Michael Halcrow8a29f2b2007-11-05 14:51:04 -0800132 goto out;
133 }
134 rc = crypto_hash_final(&desc, dst);
135 if (rc) {
136 printk(KERN_ERR
137 "%s: Error finalizing crypto hash; rc = [%d]\n",
Harvey Harrison18d1dbf2008-04-29 00:59:48 -0700138 __func__, rc);
Michael Halcrow8a29f2b2007-11-05 14:51:04 -0800139 goto out;
140 }
Michael Halcrow237fead2006-10-04 02:16:22 -0700141out:
Michael Halcrow8a29f2b2007-11-05 14:51:04 -0800142 mutex_unlock(&crypt_stat->cs_hash_tfm_mutex);
Michael Halcrow237fead2006-10-04 02:16:22 -0700143 return rc;
144}
145
Michael Halcrowcd9d67d2007-10-16 01:28:04 -0700146static int ecryptfs_crypto_api_algify_cipher_name(char **algified_name,
147 char *cipher_name,
148 char *chaining_modifier)
Michael Halcrow8bba0662006-10-30 22:07:18 -0800149{
150 int cipher_name_len = strlen(cipher_name);
151 int chaining_modifier_len = strlen(chaining_modifier);
152 int algified_name_len;
153 int rc;
154
155 algified_name_len = (chaining_modifier_len + cipher_name_len + 3);
156 (*algified_name) = kmalloc(algified_name_len, GFP_KERNEL);
Michael Halcrow7bd473f2006-11-02 22:06:56 -0800157 if (!(*algified_name)) {
Michael Halcrow8bba0662006-10-30 22:07:18 -0800158 rc = -ENOMEM;
159 goto out;
160 }
161 snprintf((*algified_name), algified_name_len, "%s(%s)",
162 chaining_modifier, cipher_name);
163 rc = 0;
164out:
165 return rc;
166}
167
Michael Halcrow237fead2006-10-04 02:16:22 -0700168/**
169 * ecryptfs_derive_iv
170 * @iv: destination for the derived iv vale
171 * @crypt_stat: Pointer to crypt_stat struct for the current inode
Michael Halcrowd6a13c12007-10-16 01:28:12 -0700172 * @offset: Offset of the extent whose IV we are to derive
Michael Halcrow237fead2006-10-04 02:16:22 -0700173 *
174 * Generate the initialization vector from the given root IV and page
175 * offset.
176 *
177 * Returns zero on success; non-zero on error.
178 */
Michael Halcrowa34f60f2009-01-06 14:41:58 -0800179int ecryptfs_derive_iv(char *iv, struct ecryptfs_crypt_stat *crypt_stat,
180 loff_t offset)
Michael Halcrow237fead2006-10-04 02:16:22 -0700181{
182 int rc = 0;
183 char dst[MD5_DIGEST_SIZE];
184 char src[ECRYPTFS_MAX_IV_BYTES + 16];
185
186 if (unlikely(ecryptfs_verbosity > 0)) {
187 ecryptfs_printk(KERN_DEBUG, "root iv:\n");
188 ecryptfs_dump_hex(crypt_stat->root_iv, crypt_stat->iv_bytes);
189 }
190 /* TODO: It is probably secure to just cast the least
191 * significant bits of the root IV into an unsigned long and
192 * add the offset to that rather than go through all this
193 * hashing business. -Halcrow */
194 memcpy(src, crypt_stat->root_iv, crypt_stat->iv_bytes);
195 memset((src + crypt_stat->iv_bytes), 0, 16);
Michael Halcrowd6a13c12007-10-16 01:28:12 -0700196 snprintf((src + crypt_stat->iv_bytes), 16, "%lld", offset);
Michael Halcrow237fead2006-10-04 02:16:22 -0700197 if (unlikely(ecryptfs_verbosity > 0)) {
198 ecryptfs_printk(KERN_DEBUG, "source:\n");
199 ecryptfs_dump_hex(src, (crypt_stat->iv_bytes + 16));
200 }
201 rc = ecryptfs_calculate_md5(dst, crypt_stat, src,
202 (crypt_stat->iv_bytes + 16));
203 if (rc) {
204 ecryptfs_printk(KERN_WARNING, "Error attempting to compute "
205 "MD5 while generating IV for a page\n");
206 goto out;
207 }
208 memcpy(iv, dst, crypt_stat->iv_bytes);
209 if (unlikely(ecryptfs_verbosity > 0)) {
210 ecryptfs_printk(KERN_DEBUG, "derived iv:\n");
211 ecryptfs_dump_hex(iv, crypt_stat->iv_bytes);
212 }
213out:
214 return rc;
215}
216
217/**
218 * ecryptfs_init_crypt_stat
219 * @crypt_stat: Pointer to the crypt_stat struct to initialize.
220 *
221 * Initialize the crypt_stat structure.
222 */
223void
224ecryptfs_init_crypt_stat(struct ecryptfs_crypt_stat *crypt_stat)
225{
226 memset((void *)crypt_stat, 0, sizeof(struct ecryptfs_crypt_stat));
Michael Halcrowf4aad162007-10-16 01:27:53 -0700227 INIT_LIST_HEAD(&crypt_stat->keysig_list);
228 mutex_init(&crypt_stat->keysig_list_mutex);
Michael Halcrow237fead2006-10-04 02:16:22 -0700229 mutex_init(&crypt_stat->cs_mutex);
230 mutex_init(&crypt_stat->cs_tfm_mutex);
Michael Halcrow565d97242006-10-30 22:07:17 -0800231 mutex_init(&crypt_stat->cs_hash_tfm_mutex);
Michael Halcrowe2bd99e2007-02-12 00:53:49 -0800232 crypt_stat->flags |= ECRYPTFS_STRUCT_INITIALIZED;
Michael Halcrow237fead2006-10-04 02:16:22 -0700233}
234
235/**
Michael Halcrowfcd12832007-10-16 01:28:01 -0700236 * ecryptfs_destroy_crypt_stat
Michael Halcrow237fead2006-10-04 02:16:22 -0700237 * @crypt_stat: Pointer to the crypt_stat struct to initialize.
238 *
239 * Releases all memory associated with a crypt_stat struct.
240 */
Michael Halcrowfcd12832007-10-16 01:28:01 -0700241void ecryptfs_destroy_crypt_stat(struct ecryptfs_crypt_stat *crypt_stat)
Michael Halcrow237fead2006-10-04 02:16:22 -0700242{
Michael Halcrowf4aad162007-10-16 01:27:53 -0700243 struct ecryptfs_key_sig *key_sig, *key_sig_tmp;
244
Michael Halcrow237fead2006-10-04 02:16:22 -0700245 if (crypt_stat->tfm)
Tyler Hicks4dfea4f2013-05-09 16:55:07 -0700246 crypto_free_ablkcipher(crypt_stat->tfm);
Michael Halcrow565d97242006-10-30 22:07:17 -0800247 if (crypt_stat->hash_tfm)
248 crypto_free_hash(crypt_stat->hash_tfm);
Michael Halcrowf4aad162007-10-16 01:27:53 -0700249 list_for_each_entry_safe(key_sig, key_sig_tmp,
250 &crypt_stat->keysig_list, crypt_stat_list) {
251 list_del(&key_sig->crypt_stat_list);
252 kmem_cache_free(ecryptfs_key_sig_cache, key_sig);
253 }
Michael Halcrow237fead2006-10-04 02:16:22 -0700254 memset(crypt_stat, 0, sizeof(struct ecryptfs_crypt_stat));
255}
256
Michael Halcrowfcd12832007-10-16 01:28:01 -0700257void ecryptfs_destroy_mount_crypt_stat(
Michael Halcrow237fead2006-10-04 02:16:22 -0700258 struct ecryptfs_mount_crypt_stat *mount_crypt_stat)
259{
Michael Halcrowf4aad162007-10-16 01:27:53 -0700260 struct ecryptfs_global_auth_tok *auth_tok, *auth_tok_tmp;
261
262 if (!(mount_crypt_stat->flags & ECRYPTFS_MOUNT_CRYPT_STAT_INITIALIZED))
263 return;
264 mutex_lock(&mount_crypt_stat->global_auth_tok_list_mutex);
265 list_for_each_entry_safe(auth_tok, auth_tok_tmp,
266 &mount_crypt_stat->global_auth_tok_list,
267 mount_crypt_stat_list) {
268 list_del(&auth_tok->mount_crypt_stat_list);
Michael Halcrowf4aad162007-10-16 01:27:53 -0700269 if (auth_tok->global_auth_tok_key
270 && !(auth_tok->flags & ECRYPTFS_AUTH_TOK_INVALID))
271 key_put(auth_tok->global_auth_tok_key);
272 kmem_cache_free(ecryptfs_global_auth_tok_cache, auth_tok);
273 }
274 mutex_unlock(&mount_crypt_stat->global_auth_tok_list_mutex);
Michael Halcrow237fead2006-10-04 02:16:22 -0700275 memset(mount_crypt_stat, 0, sizeof(struct ecryptfs_mount_crypt_stat));
276}
277
278/**
279 * virt_to_scatterlist
280 * @addr: Virtual address
281 * @size: Size of data; should be an even multiple of the block size
282 * @sg: Pointer to scatterlist array; set to NULL to obtain only
283 * the number of scatterlist structs required in array
284 * @sg_size: Max array size
285 *
286 * Fills in a scatterlist array with page references for a passed
287 * virtual address.
288 *
289 * Returns the number of scatterlist structs in array used
290 */
291int virt_to_scatterlist(const void *addr, int size, struct scatterlist *sg,
292 int sg_size)
293{
294 int i = 0;
295 struct page *pg;
296 int offset;
297 int remainder_of_page;
298
Herbert Xu68e3f5d2007-10-27 00:52:07 -0700299 sg_init_table(sg, sg_size);
300
Michael Halcrow237fead2006-10-04 02:16:22 -0700301 while (size > 0 && i < sg_size) {
302 pg = virt_to_page(addr);
303 offset = offset_in_page(addr);
Dan Carpentera07c48a2013-01-26 10:48:42 +0300304 sg_set_page(&sg[i], pg, 0, offset);
Michael Halcrow237fead2006-10-04 02:16:22 -0700305 remainder_of_page = PAGE_CACHE_SIZE - offset;
306 if (size >= remainder_of_page) {
Dan Carpentera07c48a2013-01-26 10:48:42 +0300307 sg[i].length = remainder_of_page;
Michael Halcrow237fead2006-10-04 02:16:22 -0700308 addr += remainder_of_page;
309 size -= remainder_of_page;
310 } else {
Dan Carpentera07c48a2013-01-26 10:48:42 +0300311 sg[i].length = size;
Michael Halcrow237fead2006-10-04 02:16:22 -0700312 addr += size;
313 size = 0;
314 }
315 i++;
316 }
317 if (size > 0)
318 return -ENOMEM;
319 return i;
320}
321
Tyler Hicks4dfea4f2013-05-09 16:55:07 -0700322struct extent_crypt_result {
323 struct completion completion;
324 int rc;
325};
326
327static void extent_crypt_complete(struct crypto_async_request *req, int rc)
328{
329 struct extent_crypt_result *ecr = req->data;
330
331 if (rc == -EINPROGRESS)
332 return;
333
334 ecr->rc = rc;
335 complete(&ecr->completion);
336}
337
Michael Halcrow237fead2006-10-04 02:16:22 -0700338/**
339 * encrypt_scatterlist
340 * @crypt_stat: Pointer to the crypt_stat struct to initialize.
341 * @dest_sg: Destination of encrypted data
342 * @src_sg: Data to be encrypted
343 * @size: Length of data to be encrypted
344 * @iv: iv to use during encryption
345 *
346 * Returns the number of bytes encrypted; negative value on error
347 */
348static int encrypt_scatterlist(struct ecryptfs_crypt_stat *crypt_stat,
349 struct scatterlist *dest_sg,
350 struct scatterlist *src_sg, int size,
351 unsigned char *iv)
352{
Tyler Hicks4dfea4f2013-05-09 16:55:07 -0700353 struct ablkcipher_request *req = NULL;
354 struct extent_crypt_result ecr;
Michael Halcrow237fead2006-10-04 02:16:22 -0700355 int rc = 0;
356
357 BUG_ON(!crypt_stat || !crypt_stat->tfm
Michael Halcrowe2bd99e2007-02-12 00:53:49 -0800358 || !(crypt_stat->flags & ECRYPTFS_STRUCT_INITIALIZED));
Michael Halcrow237fead2006-10-04 02:16:22 -0700359 if (unlikely(ecryptfs_verbosity > 0)) {
Tyler Hicksf24b3882010-11-15 17:36:38 -0600360 ecryptfs_printk(KERN_DEBUG, "Key size [%zd]; key:\n",
Michael Halcrow237fead2006-10-04 02:16:22 -0700361 crypt_stat->key_size);
362 ecryptfs_dump_hex(crypt_stat->key,
363 crypt_stat->key_size);
364 }
Tyler Hicks4dfea4f2013-05-09 16:55:07 -0700365
366 init_completion(&ecr.completion);
367
Michael Halcrow237fead2006-10-04 02:16:22 -0700368 mutex_lock(&crypt_stat->cs_tfm_mutex);
Tyler Hicks4dfea4f2013-05-09 16:55:07 -0700369 req = ablkcipher_request_alloc(crypt_stat->tfm, GFP_NOFS);
370 if (!req) {
Michael Halcrow237fead2006-10-04 02:16:22 -0700371 mutex_unlock(&crypt_stat->cs_tfm_mutex);
Tyler Hicks4dfea4f2013-05-09 16:55:07 -0700372 rc = -ENOMEM;
Michael Halcrow237fead2006-10-04 02:16:22 -0700373 goto out;
374 }
Tyler Hicks4dfea4f2013-05-09 16:55:07 -0700375
376 ablkcipher_request_set_callback(req,
377 CRYPTO_TFM_REQ_MAY_BACKLOG | CRYPTO_TFM_REQ_MAY_SLEEP,
378 extent_crypt_complete, &ecr);
379 /* Consider doing this once, when the file is opened */
380 if (!(crypt_stat->flags & ECRYPTFS_KEY_SET)) {
381 rc = crypto_ablkcipher_setkey(crypt_stat->tfm, crypt_stat->key,
382 crypt_stat->key_size);
383 if (rc) {
384 ecryptfs_printk(KERN_ERR,
385 "Error setting key; rc = [%d]\n",
386 rc);
387 mutex_unlock(&crypt_stat->cs_tfm_mutex);
388 rc = -EINVAL;
389 goto out;
390 }
391 crypt_stat->flags |= ECRYPTFS_KEY_SET;
392 }
Michael Halcrow237fead2006-10-04 02:16:22 -0700393 mutex_unlock(&crypt_stat->cs_tfm_mutex);
Tyler Hicks4dfea4f2013-05-09 16:55:07 -0700394 ecryptfs_printk(KERN_DEBUG, "Encrypting [%d] bytes.\n", size);
395 ablkcipher_request_set_crypt(req, src_sg, dest_sg, size, iv);
396 rc = crypto_ablkcipher_encrypt(req);
397 if (rc == -EINPROGRESS || rc == -EBUSY) {
398 struct extent_crypt_result *ecr = req->base.data;
399
400 wait_for_completion(&ecr->completion);
401 rc = ecr->rc;
402 INIT_COMPLETION(ecr->completion);
403 }
Michael Halcrow237fead2006-10-04 02:16:22 -0700404out:
Tyler Hicks4dfea4f2013-05-09 16:55:07 -0700405 ablkcipher_request_free(req);
Michael Halcrow237fead2006-10-04 02:16:22 -0700406 return rc;
407}
408
Michael Halcrow237fead2006-10-04 02:16:22 -0700409/**
Tyler Hicks24d15262013-04-15 17:28:09 -0700410 * lower_offset_for_page
Michael Halcrow0216f7f2007-10-16 01:28:08 -0700411 *
412 * Convert an eCryptfs page index into a lower byte offset
413 */
Tyler Hicks24d15262013-04-15 17:28:09 -0700414static loff_t lower_offset_for_page(struct ecryptfs_crypt_stat *crypt_stat,
415 struct page *page)
Michael Halcrow0216f7f2007-10-16 01:28:08 -0700416{
Tyler Hicks24d15262013-04-15 17:28:09 -0700417 return ecryptfs_lower_header_size(crypt_stat) +
418 (page->index << PAGE_CACHE_SHIFT);
Michael Halcrow0216f7f2007-10-16 01:28:08 -0700419}
420
421/**
422 * ecryptfs_encrypt_extent
423 * @enc_extent_page: Allocated page into which to encrypt the data in
424 * @page
425 * @crypt_stat: crypt_stat containing cryptographic context for the
426 * encryption operation
427 * @page: Page containing plaintext data extent to encrypt
428 * @extent_offset: Page extent offset for use in generating IV
429 *
430 * Encrypts one extent of data.
431 *
432 * Return zero on success; non-zero otherwise
433 */
434static int ecryptfs_encrypt_extent(struct page *enc_extent_page,
435 struct ecryptfs_crypt_stat *crypt_stat,
436 struct page *page,
437 unsigned long extent_offset)
438{
Michael Halcrowd6a13c12007-10-16 01:28:12 -0700439 loff_t extent_base;
Michael Halcrow0216f7f2007-10-16 01:28:08 -0700440 char extent_iv[ECRYPTFS_MAX_IV_BYTES];
441 int rc;
442
Michael Halcrowd6a13c12007-10-16 01:28:12 -0700443 extent_base = (((loff_t)page->index)
Michael Halcrow0216f7f2007-10-16 01:28:08 -0700444 * (PAGE_CACHE_SIZE / crypt_stat->extent_size));
445 rc = ecryptfs_derive_iv(extent_iv, crypt_stat,
446 (extent_base + extent_offset));
447 if (rc) {
Joe Perches888d57b2010-11-10 15:46:16 -0800448 ecryptfs_printk(KERN_ERR, "Error attempting to derive IV for "
449 "extent [0x%.16llx]; rc = [%d]\n",
450 (unsigned long long)(extent_base + extent_offset), rc);
Michael Halcrow0216f7f2007-10-16 01:28:08 -0700451 goto out;
452 }
Tyler Hicks12003e52013-04-16 23:21:24 -0700453 rc = ecryptfs_encrypt_page_offset(crypt_stat, enc_extent_page,
454 extent_offset * crypt_stat->extent_size,
455 page,
456 extent_offset * crypt_stat->extent_size,
457 crypt_stat->extent_size, extent_iv);
Michael Halcrow0216f7f2007-10-16 01:28:08 -0700458 if (rc < 0) {
459 printk(KERN_ERR "%s: Error attempting to encrypt page with "
460 "page->index = [%ld], extent_offset = [%ld]; "
Harvey Harrison18d1dbf2008-04-29 00:59:48 -0700461 "rc = [%d]\n", __func__, page->index, extent_offset,
Michael Halcrow0216f7f2007-10-16 01:28:08 -0700462 rc);
463 goto out;
464 }
465 rc = 0;
Michael Halcrow0216f7f2007-10-16 01:28:08 -0700466out:
467 return rc;
468}
469
470/**
Michael Halcrow237fead2006-10-04 02:16:22 -0700471 * ecryptfs_encrypt_page
Michael Halcrow0216f7f2007-10-16 01:28:08 -0700472 * @page: Page mapped from the eCryptfs inode for the file; contains
473 * decrypted content that needs to be encrypted (to a temporary
474 * page; not in place) and written out to the lower file
Michael Halcrow237fead2006-10-04 02:16:22 -0700475 *
476 * Encrypt an eCryptfs page. This is done on a per-extent basis. Note
477 * that eCryptfs pages may straddle the lower pages -- for instance,
478 * if the file was created on a machine with an 8K page size
479 * (resulting in an 8K header), and then the file is copied onto a
480 * host with a 32K page size, then when reading page 0 of the eCryptfs
481 * file, 24K of page 0 of the lower file will be read and decrypted,
482 * and then 8K of page 1 of the lower file will be read and decrypted.
483 *
Michael Halcrow237fead2006-10-04 02:16:22 -0700484 * Returns zero on success; negative on error
485 */
Michael Halcrow0216f7f2007-10-16 01:28:08 -0700486int ecryptfs_encrypt_page(struct page *page)
Michael Halcrow237fead2006-10-04 02:16:22 -0700487{
Michael Halcrow0216f7f2007-10-16 01:28:08 -0700488 struct inode *ecryptfs_inode;
Michael Halcrow237fead2006-10-04 02:16:22 -0700489 struct ecryptfs_crypt_stat *crypt_stat;
Eric Sandeen7fcba052008-07-28 15:46:39 -0700490 char *enc_extent_virt;
491 struct page *enc_extent_page = NULL;
Michael Halcrow0216f7f2007-10-16 01:28:08 -0700492 loff_t extent_offset;
Tyler Hicks0f896172013-04-15 16:16:24 -0700493 loff_t lower_offset;
Michael Halcrow237fead2006-10-04 02:16:22 -0700494 int rc = 0;
Michael Halcrow237fead2006-10-04 02:16:22 -0700495
Michael Halcrow0216f7f2007-10-16 01:28:08 -0700496 ecryptfs_inode = page->mapping->host;
497 crypt_stat =
498 &(ecryptfs_inode_to_private(ecryptfs_inode)->crypt_stat);
Tyler Hicks13a791b2009-04-13 15:29:27 -0500499 BUG_ON(!(crypt_stat->flags & ECRYPTFS_ENCRYPTED));
Eric Sandeen7fcba052008-07-28 15:46:39 -0700500 enc_extent_page = alloc_page(GFP_USER);
501 if (!enc_extent_page) {
Michael Halcrow0216f7f2007-10-16 01:28:08 -0700502 rc = -ENOMEM;
503 ecryptfs_printk(KERN_ERR, "Error allocating memory for "
504 "encrypted extent\n");
505 goto out;
506 }
Tyler Hicks0f896172013-04-15 16:16:24 -0700507
Michael Halcrow0216f7f2007-10-16 01:28:08 -0700508 for (extent_offset = 0;
509 extent_offset < (PAGE_CACHE_SIZE / crypt_stat->extent_size);
510 extent_offset++) {
Michael Halcrow0216f7f2007-10-16 01:28:08 -0700511 rc = ecryptfs_encrypt_extent(enc_extent_page, crypt_stat, page,
512 extent_offset);
Michael Halcrow237fead2006-10-04 02:16:22 -0700513 if (rc) {
Michael Halcrow0216f7f2007-10-16 01:28:08 -0700514 printk(KERN_ERR "%s: Error encrypting extent; "
Harvey Harrison18d1dbf2008-04-29 00:59:48 -0700515 "rc = [%d]\n", __func__, rc);
Michael Halcrow237fead2006-10-04 02:16:22 -0700516 goto out;
517 }
Tyler Hicks0f896172013-04-15 16:16:24 -0700518 }
519
Tyler Hicks24d15262013-04-15 17:28:09 -0700520 lower_offset = lower_offset_for_page(crypt_stat, page);
Tyler Hicks0f896172013-04-15 16:16:24 -0700521 enc_extent_virt = kmap(enc_extent_page);
522 rc = ecryptfs_write_lower(ecryptfs_inode, enc_extent_virt, lower_offset,
523 PAGE_CACHE_SIZE);
524 kunmap(enc_extent_page);
525 if (rc < 0) {
526 ecryptfs_printk(KERN_ERR,
527 "Error attempting to write lower page; rc = [%d]\n",
528 rc);
529 goto out;
Michael Halcrow237fead2006-10-04 02:16:22 -0700530 }
Tyler Hicks96a7b9c2009-09-16 19:04:20 -0500531 rc = 0;
Michael Halcrow0216f7f2007-10-16 01:28:08 -0700532out:
Eric Sandeen7fcba052008-07-28 15:46:39 -0700533 if (enc_extent_page) {
Eric Sandeen7fcba052008-07-28 15:46:39 -0700534 __free_page(enc_extent_page);
535 }
Michael Halcrow0216f7f2007-10-16 01:28:08 -0700536 return rc;
537}
538
539static int ecryptfs_decrypt_extent(struct page *page,
540 struct ecryptfs_crypt_stat *crypt_stat,
541 struct page *enc_extent_page,
542 unsigned long extent_offset)
543{
Michael Halcrowd6a13c12007-10-16 01:28:12 -0700544 loff_t extent_base;
Michael Halcrow0216f7f2007-10-16 01:28:08 -0700545 char extent_iv[ECRYPTFS_MAX_IV_BYTES];
546 int rc;
547
Michael Halcrowd6a13c12007-10-16 01:28:12 -0700548 extent_base = (((loff_t)page->index)
Michael Halcrow0216f7f2007-10-16 01:28:08 -0700549 * (PAGE_CACHE_SIZE / crypt_stat->extent_size));
550 rc = ecryptfs_derive_iv(extent_iv, crypt_stat,
551 (extent_base + extent_offset));
Michael Halcrow237fead2006-10-04 02:16:22 -0700552 if (rc) {
Joe Perches888d57b2010-11-10 15:46:16 -0800553 ecryptfs_printk(KERN_ERR, "Error attempting to derive IV for "
554 "extent [0x%.16llx]; rc = [%d]\n",
555 (unsigned long long)(extent_base + extent_offset), rc);
Michael Halcrow0216f7f2007-10-16 01:28:08 -0700556 goto out;
557 }
Michael Halcrow0216f7f2007-10-16 01:28:08 -0700558 rc = ecryptfs_decrypt_page_offset(crypt_stat, page,
Tyler Hicks12003e52013-04-16 23:21:24 -0700559 extent_offset * crypt_stat->extent_size,
560 enc_extent_page,
561 extent_offset * crypt_stat->extent_size,
562 crypt_stat->extent_size, extent_iv);
Michael Halcrow0216f7f2007-10-16 01:28:08 -0700563 if (rc < 0) {
564 printk(KERN_ERR "%s: Error attempting to decrypt to page with "
565 "page->index = [%ld], extent_offset = [%ld]; "
Harvey Harrison18d1dbf2008-04-29 00:59:48 -0700566 "rc = [%d]\n", __func__, page->index, extent_offset,
Michael Halcrow0216f7f2007-10-16 01:28:08 -0700567 rc);
568 goto out;
569 }
570 rc = 0;
Michael Halcrow237fead2006-10-04 02:16:22 -0700571out:
572 return rc;
573}
574
575/**
576 * ecryptfs_decrypt_page
Michael Halcrow0216f7f2007-10-16 01:28:08 -0700577 * @page: Page mapped from the eCryptfs inode for the file; data read
578 * and decrypted from the lower file will be written into this
579 * page
Michael Halcrow237fead2006-10-04 02:16:22 -0700580 *
581 * Decrypt an eCryptfs page. This is done on a per-extent basis. Note
582 * that eCryptfs pages may straddle the lower pages -- for instance,
583 * if the file was created on a machine with an 8K page size
584 * (resulting in an 8K header), and then the file is copied onto a
585 * host with a 32K page size, then when reading page 0 of the eCryptfs
586 * file, 24K of page 0 of the lower file will be read and decrypted,
587 * and then 8K of page 1 of the lower file will be read and decrypted.
588 *
589 * Returns zero on success; negative on error
590 */
Michael Halcrow0216f7f2007-10-16 01:28:08 -0700591int ecryptfs_decrypt_page(struct page *page)
Michael Halcrow237fead2006-10-04 02:16:22 -0700592{
Michael Halcrow0216f7f2007-10-16 01:28:08 -0700593 struct inode *ecryptfs_inode;
Michael Halcrow237fead2006-10-04 02:16:22 -0700594 struct ecryptfs_crypt_stat *crypt_stat;
Eric Sandeen7fcba052008-07-28 15:46:39 -0700595 char *enc_extent_virt;
596 struct page *enc_extent_page = NULL;
Michael Halcrow0216f7f2007-10-16 01:28:08 -0700597 unsigned long extent_offset;
Tyler Hicks0f896172013-04-15 16:16:24 -0700598 loff_t lower_offset;
Michael Halcrow237fead2006-10-04 02:16:22 -0700599 int rc = 0;
Michael Halcrow237fead2006-10-04 02:16:22 -0700600
Michael Halcrow0216f7f2007-10-16 01:28:08 -0700601 ecryptfs_inode = page->mapping->host;
602 crypt_stat =
603 &(ecryptfs_inode_to_private(ecryptfs_inode)->crypt_stat);
Tyler Hicks13a791b2009-04-13 15:29:27 -0500604 BUG_ON(!(crypt_stat->flags & ECRYPTFS_ENCRYPTED));
Eric Sandeen7fcba052008-07-28 15:46:39 -0700605 enc_extent_page = alloc_page(GFP_USER);
606 if (!enc_extent_page) {
Michael Halcrow237fead2006-10-04 02:16:22 -0700607 rc = -ENOMEM;
Michael Halcrow0216f7f2007-10-16 01:28:08 -0700608 ecryptfs_printk(KERN_ERR, "Error allocating memory for "
609 "encrypted extent\n");
Michael Halcrow16a72c42007-10-16 01:28:14 -0700610 goto out;
Michael Halcrow237fead2006-10-04 02:16:22 -0700611 }
Tyler Hicks0f896172013-04-15 16:16:24 -0700612
Tyler Hicks24d15262013-04-15 17:28:09 -0700613 lower_offset = lower_offset_for_page(crypt_stat, page);
Eric Sandeen7fcba052008-07-28 15:46:39 -0700614 enc_extent_virt = kmap(enc_extent_page);
Tyler Hicks0f896172013-04-15 16:16:24 -0700615 rc = ecryptfs_read_lower(enc_extent_virt, lower_offset, PAGE_CACHE_SIZE,
616 ecryptfs_inode);
617 kunmap(enc_extent_page);
618 if (rc < 0) {
619 ecryptfs_printk(KERN_ERR,
620 "Error attempting to read lower page; rc = [%d]\n",
621 rc);
622 goto out;
623 }
624
Michael Halcrow0216f7f2007-10-16 01:28:08 -0700625 for (extent_offset = 0;
626 extent_offset < (PAGE_CACHE_SIZE / crypt_stat->extent_size);
627 extent_offset++) {
Michael Halcrow0216f7f2007-10-16 01:28:08 -0700628 rc = ecryptfs_decrypt_extent(page, crypt_stat, enc_extent_page,
629 extent_offset);
630 if (rc) {
631 printk(KERN_ERR "%s: Error encrypting extent; "
Harvey Harrison18d1dbf2008-04-29 00:59:48 -0700632 "rc = [%d]\n", __func__, rc);
Michael Halcrow16a72c42007-10-16 01:28:14 -0700633 goto out;
Michael Halcrow237fead2006-10-04 02:16:22 -0700634 }
Michael Halcrow237fead2006-10-04 02:16:22 -0700635 }
636out:
Eric Sandeen7fcba052008-07-28 15:46:39 -0700637 if (enc_extent_page) {
Eric Sandeen7fcba052008-07-28 15:46:39 -0700638 __free_page(enc_extent_page);
639 }
Michael Halcrow237fead2006-10-04 02:16:22 -0700640 return rc;
641}
642
643/**
644 * decrypt_scatterlist
Michael Halcrow22e78fa2007-10-16 01:28:02 -0700645 * @crypt_stat: Cryptographic context
646 * @dest_sg: The destination scatterlist to decrypt into
647 * @src_sg: The source scatterlist to decrypt from
648 * @size: The number of bytes to decrypt
649 * @iv: The initialization vector to use for the decryption
Michael Halcrow237fead2006-10-04 02:16:22 -0700650 *
651 * Returns the number of bytes decrypted; negative value on error
652 */
653static int decrypt_scatterlist(struct ecryptfs_crypt_stat *crypt_stat,
654 struct scatterlist *dest_sg,
655 struct scatterlist *src_sg, int size,
656 unsigned char *iv)
657{
Tyler Hicks4dfea4f2013-05-09 16:55:07 -0700658 struct ablkcipher_request *req = NULL;
659 struct extent_crypt_result ecr;
Michael Halcrow237fead2006-10-04 02:16:22 -0700660 int rc = 0;
661
Tyler Hicks4dfea4f2013-05-09 16:55:07 -0700662 BUG_ON(!crypt_stat || !crypt_stat->tfm
663 || !(crypt_stat->flags & ECRYPTFS_STRUCT_INITIALIZED));
664 if (unlikely(ecryptfs_verbosity > 0)) {
665 ecryptfs_printk(KERN_DEBUG, "Key size [%zd]; key:\n",
666 crypt_stat->key_size);
667 ecryptfs_dump_hex(crypt_stat->key,
668 crypt_stat->key_size);
669 }
670
671 init_completion(&ecr.completion);
672
Michael Halcrow237fead2006-10-04 02:16:22 -0700673 mutex_lock(&crypt_stat->cs_tfm_mutex);
Tyler Hicks4dfea4f2013-05-09 16:55:07 -0700674 req = ablkcipher_request_alloc(crypt_stat->tfm, GFP_NOFS);
675 if (!req) {
Michael Halcrow237fead2006-10-04 02:16:22 -0700676 mutex_unlock(&crypt_stat->cs_tfm_mutex);
Tyler Hicks4dfea4f2013-05-09 16:55:07 -0700677 rc = -ENOMEM;
Michael Halcrow237fead2006-10-04 02:16:22 -0700678 goto out;
679 }
Tyler Hicks4dfea4f2013-05-09 16:55:07 -0700680
681 ablkcipher_request_set_callback(req,
682 CRYPTO_TFM_REQ_MAY_BACKLOG | CRYPTO_TFM_REQ_MAY_SLEEP,
683 extent_crypt_complete, &ecr);
684 /* Consider doing this once, when the file is opened */
685 if (!(crypt_stat->flags & ECRYPTFS_KEY_SET)) {
686 rc = crypto_ablkcipher_setkey(crypt_stat->tfm, crypt_stat->key,
687 crypt_stat->key_size);
688 if (rc) {
689 ecryptfs_printk(KERN_ERR,
690 "Error setting key; rc = [%d]\n",
691 rc);
692 mutex_unlock(&crypt_stat->cs_tfm_mutex);
693 rc = -EINVAL;
694 goto out;
695 }
696 crypt_stat->flags |= ECRYPTFS_KEY_SET;
697 }
Michael Halcrow237fead2006-10-04 02:16:22 -0700698 mutex_unlock(&crypt_stat->cs_tfm_mutex);
Tyler Hicks4dfea4f2013-05-09 16:55:07 -0700699 ecryptfs_printk(KERN_DEBUG, "Decrypting [%d] bytes.\n", size);
700 ablkcipher_request_set_crypt(req, src_sg, dest_sg, size, iv);
701 rc = crypto_ablkcipher_decrypt(req);
702 if (rc == -EINPROGRESS || rc == -EBUSY) {
703 struct extent_crypt_result *ecr = req->base.data;
704
705 wait_for_completion(&ecr->completion);
706 rc = ecr->rc;
707 INIT_COMPLETION(ecr->completion);
Michael Halcrow237fead2006-10-04 02:16:22 -0700708 }
Michael Halcrow237fead2006-10-04 02:16:22 -0700709out:
Tyler Hicks4dfea4f2013-05-09 16:55:07 -0700710 ablkcipher_request_free(req);
Michael Halcrow237fead2006-10-04 02:16:22 -0700711 return rc;
Tyler Hicks4dfea4f2013-05-09 16:55:07 -0700712
Michael Halcrow237fead2006-10-04 02:16:22 -0700713}
714
715/**
716 * ecryptfs_encrypt_page_offset
Michael Halcrow22e78fa2007-10-16 01:28:02 -0700717 * @crypt_stat: The cryptographic context
718 * @dst_page: The page to encrypt into
719 * @dst_offset: The offset in the page to encrypt into
720 * @src_page: The page to encrypt from
721 * @src_offset: The offset in the page to encrypt from
722 * @size: The number of bytes to encrypt
723 * @iv: The initialization vector to use for the encryption
Michael Halcrow237fead2006-10-04 02:16:22 -0700724 *
725 * Returns the number of bytes encrypted
726 */
727static int
728ecryptfs_encrypt_page_offset(struct ecryptfs_crypt_stat *crypt_stat,
729 struct page *dst_page, int dst_offset,
730 struct page *src_page, int src_offset, int size,
731 unsigned char *iv)
732{
733 struct scatterlist src_sg, dst_sg;
734
Jens Axboe60c74f82007-10-22 19:43:30 +0200735 sg_init_table(&src_sg, 1);
736 sg_init_table(&dst_sg, 1);
737
Jens Axboe642f1492007-10-24 11:20:47 +0200738 sg_set_page(&src_sg, src_page, size, src_offset);
739 sg_set_page(&dst_sg, dst_page, size, dst_offset);
Michael Halcrow237fead2006-10-04 02:16:22 -0700740 return encrypt_scatterlist(crypt_stat, &dst_sg, &src_sg, size, iv);
741}
742
743/**
744 * ecryptfs_decrypt_page_offset
Michael Halcrow22e78fa2007-10-16 01:28:02 -0700745 * @crypt_stat: The cryptographic context
746 * @dst_page: The page to decrypt into
747 * @dst_offset: The offset in the page to decrypt into
748 * @src_page: The page to decrypt from
749 * @src_offset: The offset in the page to decrypt from
750 * @size: The number of bytes to decrypt
751 * @iv: The initialization vector to use for the decryption
Michael Halcrow237fead2006-10-04 02:16:22 -0700752 *
753 * Returns the number of bytes decrypted
754 */
755static int
756ecryptfs_decrypt_page_offset(struct ecryptfs_crypt_stat *crypt_stat,
757 struct page *dst_page, int dst_offset,
758 struct page *src_page, int src_offset, int size,
759 unsigned char *iv)
760{
761 struct scatterlist src_sg, dst_sg;
762
Jens Axboe60c74f82007-10-22 19:43:30 +0200763 sg_init_table(&src_sg, 1);
Jens Axboe642f1492007-10-24 11:20:47 +0200764 sg_set_page(&src_sg, src_page, size, src_offset);
Jens Axboe60c74f82007-10-22 19:43:30 +0200765
Jens Axboe642f1492007-10-24 11:20:47 +0200766 sg_init_table(&dst_sg, 1);
767 sg_set_page(&dst_sg, dst_page, size, dst_offset);
768
Michael Halcrow237fead2006-10-04 02:16:22 -0700769 return decrypt_scatterlist(crypt_stat, &dst_sg, &src_sg, size, iv);
770}
771
772#define ECRYPTFS_MAX_SCATTERLIST_LEN 4
773
774/**
775 * ecryptfs_init_crypt_ctx
Uwe Kleine-König421f91d2010-06-11 12:17:00 +0200776 * @crypt_stat: Uninitialized crypt stats structure
Michael Halcrow237fead2006-10-04 02:16:22 -0700777 *
778 * Initialize the crypto context.
779 *
780 * TODO: Performance: Keep a cache of initialized cipher contexts;
781 * only init if needed
782 */
783int ecryptfs_init_crypt_ctx(struct ecryptfs_crypt_stat *crypt_stat)
784{
Michael Halcrow8bba0662006-10-30 22:07:18 -0800785 char *full_alg_name;
Michael Halcrow237fead2006-10-04 02:16:22 -0700786 int rc = -EINVAL;
787
788 if (!crypt_stat->cipher) {
789 ecryptfs_printk(KERN_ERR, "No cipher specified\n");
790 goto out;
791 }
792 ecryptfs_printk(KERN_DEBUG,
793 "Initializing cipher [%s]; strlen = [%d]; "
Tyler Hicksf24b3882010-11-15 17:36:38 -0600794 "key_size_bits = [%zd]\n",
Michael Halcrow237fead2006-10-04 02:16:22 -0700795 crypt_stat->cipher, (int)strlen(crypt_stat->cipher),
796 crypt_stat->key_size << 3);
797 if (crypt_stat->tfm) {
798 rc = 0;
799 goto out;
800 }
801 mutex_lock(&crypt_stat->cs_tfm_mutex);
Michael Halcrow8bba0662006-10-30 22:07:18 -0800802 rc = ecryptfs_crypto_api_algify_cipher_name(&full_alg_name,
803 crypt_stat->cipher, "cbc");
804 if (rc)
Eric Sandeenc8161f62007-12-22 14:03:26 -0800805 goto out_unlock;
Tyler Hicks4dfea4f2013-05-09 16:55:07 -0700806 crypt_stat->tfm = crypto_alloc_ablkcipher(full_alg_name, 0, 0);
Michael Halcrow8bba0662006-10-30 22:07:18 -0800807 kfree(full_alg_name);
Akinobu Mitade887772006-11-28 12:29:49 -0800808 if (IS_ERR(crypt_stat->tfm)) {
809 rc = PTR_ERR(crypt_stat->tfm);
Tyler Hicksb0105ea2009-08-11 00:36:32 -0500810 crypt_stat->tfm = NULL;
Michael Halcrow237fead2006-10-04 02:16:22 -0700811 ecryptfs_printk(KERN_ERR, "cryptfs: init_crypt_ctx(): "
812 "Error initializing cipher [%s]\n",
813 crypt_stat->cipher);
Eric Sandeenc8161f62007-12-22 14:03:26 -0800814 goto out_unlock;
Michael Halcrow237fead2006-10-04 02:16:22 -0700815 }
Tyler Hicks4dfea4f2013-05-09 16:55:07 -0700816 crypto_ablkcipher_set_flags(crypt_stat->tfm, CRYPTO_TFM_REQ_WEAK_KEY);
Michael Halcrow237fead2006-10-04 02:16:22 -0700817 rc = 0;
Eric Sandeenc8161f62007-12-22 14:03:26 -0800818out_unlock:
819 mutex_unlock(&crypt_stat->cs_tfm_mutex);
Michael Halcrow237fead2006-10-04 02:16:22 -0700820out:
821 return rc;
822}
823
824static void set_extent_mask_and_shift(struct ecryptfs_crypt_stat *crypt_stat)
825{
826 int extent_size_tmp;
827
828 crypt_stat->extent_mask = 0xFFFFFFFF;
829 crypt_stat->extent_shift = 0;
830 if (crypt_stat->extent_size == 0)
831 return;
832 extent_size_tmp = crypt_stat->extent_size;
833 while ((extent_size_tmp & 0x01) == 0) {
834 extent_size_tmp >>= 1;
835 crypt_stat->extent_mask <<= 1;
836 crypt_stat->extent_shift++;
837 }
838}
839
840void ecryptfs_set_default_sizes(struct ecryptfs_crypt_stat *crypt_stat)
841{
842 /* Default values; may be overwritten as we are parsing the
843 * packets. */
844 crypt_stat->extent_size = ECRYPTFS_DEFAULT_EXTENT_SIZE;
845 set_extent_mask_and_shift(crypt_stat);
846 crypt_stat->iv_bytes = ECRYPTFS_DEFAULT_IV_BYTES;
Michael Halcrowdd2a3b72007-02-12 00:53:46 -0800847 if (crypt_stat->flags & ECRYPTFS_METADATA_IN_XATTR)
Tyler Hicksfa3ef1c2010-02-11 05:09:14 -0600848 crypt_stat->metadata_size = ECRYPTFS_MINIMUM_HEADER_EXTENT_SIZE;
Michael Halcrow45eaab72007-10-16 01:28:05 -0700849 else {
850 if (PAGE_CACHE_SIZE <= ECRYPTFS_MINIMUM_HEADER_EXTENT_SIZE)
Tyler Hicksfa3ef1c2010-02-11 05:09:14 -0600851 crypt_stat->metadata_size =
Michael Halcrowcc11bef2008-02-06 01:38:32 -0800852 ECRYPTFS_MINIMUM_HEADER_EXTENT_SIZE;
Michael Halcrow45eaab72007-10-16 01:28:05 -0700853 else
Tyler Hicksfa3ef1c2010-02-11 05:09:14 -0600854 crypt_stat->metadata_size = PAGE_CACHE_SIZE;
Michael Halcrow45eaab72007-10-16 01:28:05 -0700855 }
Michael Halcrow237fead2006-10-04 02:16:22 -0700856}
857
858/**
859 * ecryptfs_compute_root_iv
860 * @crypt_stats
861 *
862 * On error, sets the root IV to all 0's.
863 */
864int ecryptfs_compute_root_iv(struct ecryptfs_crypt_stat *crypt_stat)
865{
866 int rc = 0;
867 char dst[MD5_DIGEST_SIZE];
868
869 BUG_ON(crypt_stat->iv_bytes > MD5_DIGEST_SIZE);
870 BUG_ON(crypt_stat->iv_bytes <= 0);
Michael Halcrowe2bd99e2007-02-12 00:53:49 -0800871 if (!(crypt_stat->flags & ECRYPTFS_KEY_VALID)) {
Michael Halcrow237fead2006-10-04 02:16:22 -0700872 rc = -EINVAL;
873 ecryptfs_printk(KERN_WARNING, "Session key not valid; "
874 "cannot generate root IV\n");
875 goto out;
876 }
877 rc = ecryptfs_calculate_md5(dst, crypt_stat, crypt_stat->key,
878 crypt_stat->key_size);
879 if (rc) {
880 ecryptfs_printk(KERN_WARNING, "Error attempting to compute "
881 "MD5 while generating root IV\n");
882 goto out;
883 }
884 memcpy(crypt_stat->root_iv, dst, crypt_stat->iv_bytes);
885out:
886 if (rc) {
887 memset(crypt_stat->root_iv, 0, crypt_stat->iv_bytes);
Michael Halcrowe2bd99e2007-02-12 00:53:49 -0800888 crypt_stat->flags |= ECRYPTFS_SECURITY_WARNING;
Michael Halcrow237fead2006-10-04 02:16:22 -0700889 }
890 return rc;
891}
892
893static void ecryptfs_generate_new_key(struct ecryptfs_crypt_stat *crypt_stat)
894{
895 get_random_bytes(crypt_stat->key, crypt_stat->key_size);
Michael Halcrowe2bd99e2007-02-12 00:53:49 -0800896 crypt_stat->flags |= ECRYPTFS_KEY_VALID;
Michael Halcrow237fead2006-10-04 02:16:22 -0700897 ecryptfs_compute_root_iv(crypt_stat);
898 if (unlikely(ecryptfs_verbosity > 0)) {
899 ecryptfs_printk(KERN_DEBUG, "Generated new session key:\n");
900 ecryptfs_dump_hex(crypt_stat->key,
901 crypt_stat->key_size);
902 }
903}
904
905/**
Michael Halcrow17398952007-02-12 00:53:45 -0800906 * ecryptfs_copy_mount_wide_flags_to_inode_flags
Michael Halcrow22e78fa2007-10-16 01:28:02 -0700907 * @crypt_stat: The inode's cryptographic context
908 * @mount_crypt_stat: The mount point's cryptographic context
Michael Halcrow17398952007-02-12 00:53:45 -0800909 *
910 * This function propagates the mount-wide flags to individual inode
911 * flags.
912 */
913static void ecryptfs_copy_mount_wide_flags_to_inode_flags(
914 struct ecryptfs_crypt_stat *crypt_stat,
915 struct ecryptfs_mount_crypt_stat *mount_crypt_stat)
916{
917 if (mount_crypt_stat->flags & ECRYPTFS_XATTR_METADATA_ENABLED)
918 crypt_stat->flags |= ECRYPTFS_METADATA_IN_XATTR;
919 if (mount_crypt_stat->flags & ECRYPTFS_ENCRYPTED_VIEW_ENABLED)
920 crypt_stat->flags |= ECRYPTFS_VIEW_AS_ENCRYPTED;
Michael Halcrowaddd65ad2009-01-06 14:42:00 -0800921 if (mount_crypt_stat->flags & ECRYPTFS_GLOBAL_ENCRYPT_FILENAMES) {
922 crypt_stat->flags |= ECRYPTFS_ENCRYPT_FILENAMES;
923 if (mount_crypt_stat->flags
924 & ECRYPTFS_GLOBAL_ENCFN_USE_MOUNT_FNEK)
925 crypt_stat->flags |= ECRYPTFS_ENCFN_USE_MOUNT_FNEK;
926 else if (mount_crypt_stat->flags
927 & ECRYPTFS_GLOBAL_ENCFN_USE_FEK)
928 crypt_stat->flags |= ECRYPTFS_ENCFN_USE_FEK;
929 }
Michael Halcrow17398952007-02-12 00:53:45 -0800930}
931
Michael Halcrowf4aad162007-10-16 01:27:53 -0700932static int ecryptfs_copy_mount_wide_sigs_to_inode_sigs(
933 struct ecryptfs_crypt_stat *crypt_stat,
934 struct ecryptfs_mount_crypt_stat *mount_crypt_stat)
935{
936 struct ecryptfs_global_auth_tok *global_auth_tok;
937 int rc = 0;
938
Roland Dreieraa061172009-07-01 15:48:18 -0700939 mutex_lock(&crypt_stat->keysig_list_mutex);
Michael Halcrowf4aad162007-10-16 01:27:53 -0700940 mutex_lock(&mount_crypt_stat->global_auth_tok_list_mutex);
Roland Dreieraa061172009-07-01 15:48:18 -0700941
Michael Halcrowf4aad162007-10-16 01:27:53 -0700942 list_for_each_entry(global_auth_tok,
943 &mount_crypt_stat->global_auth_tok_list,
944 mount_crypt_stat_list) {
Tyler Hicks84814d62009-03-13 13:51:59 -0700945 if (global_auth_tok->flags & ECRYPTFS_AUTH_TOK_FNEK)
946 continue;
Michael Halcrowf4aad162007-10-16 01:27:53 -0700947 rc = ecryptfs_add_keysig(crypt_stat, global_auth_tok->sig);
948 if (rc) {
949 printk(KERN_ERR "Error adding keysig; rc = [%d]\n", rc);
Michael Halcrowf4aad162007-10-16 01:27:53 -0700950 goto out;
951 }
952 }
Roland Dreieraa061172009-07-01 15:48:18 -0700953
Michael Halcrowf4aad162007-10-16 01:27:53 -0700954out:
Roland Dreieraa061172009-07-01 15:48:18 -0700955 mutex_unlock(&mount_crypt_stat->global_auth_tok_list_mutex);
956 mutex_unlock(&crypt_stat->keysig_list_mutex);
Michael Halcrowf4aad162007-10-16 01:27:53 -0700957 return rc;
958}
959
Michael Halcrow17398952007-02-12 00:53:45 -0800960/**
Michael Halcrow237fead2006-10-04 02:16:22 -0700961 * ecryptfs_set_default_crypt_stat_vals
Michael Halcrow22e78fa2007-10-16 01:28:02 -0700962 * @crypt_stat: The inode's cryptographic context
963 * @mount_crypt_stat: The mount point's cryptographic context
Michael Halcrow237fead2006-10-04 02:16:22 -0700964 *
965 * Default values in the event that policy does not override them.
966 */
967static void ecryptfs_set_default_crypt_stat_vals(
968 struct ecryptfs_crypt_stat *crypt_stat,
969 struct ecryptfs_mount_crypt_stat *mount_crypt_stat)
970{
Michael Halcrow17398952007-02-12 00:53:45 -0800971 ecryptfs_copy_mount_wide_flags_to_inode_flags(crypt_stat,
972 mount_crypt_stat);
Michael Halcrow237fead2006-10-04 02:16:22 -0700973 ecryptfs_set_default_sizes(crypt_stat);
974 strcpy(crypt_stat->cipher, ECRYPTFS_DEFAULT_CIPHER);
975 crypt_stat->key_size = ECRYPTFS_DEFAULT_KEY_BYTES;
Michael Halcrowe2bd99e2007-02-12 00:53:49 -0800976 crypt_stat->flags &= ~(ECRYPTFS_KEY_VALID);
Michael Halcrow237fead2006-10-04 02:16:22 -0700977 crypt_stat->file_version = ECRYPTFS_FILE_VERSION;
978 crypt_stat->mount_crypt_stat = mount_crypt_stat;
979}
980
981/**
982 * ecryptfs_new_file_context
Tyler Hicksb59db432011-11-21 17:31:02 -0600983 * @ecryptfs_inode: The eCryptfs inode
Michael Halcrow237fead2006-10-04 02:16:22 -0700984 *
985 * If the crypto context for the file has not yet been established,
986 * this is where we do that. Establishing a new crypto context
987 * involves the following decisions:
988 * - What cipher to use?
989 * - What set of authentication tokens to use?
990 * Here we just worry about getting enough information into the
991 * authentication tokens so that we know that they are available.
992 * We associate the available authentication tokens with the new file
993 * via the set of signatures in the crypt_stat struct. Later, when
994 * the headers are actually written out, we may again defer to
995 * userspace to perform the encryption of the session key; for the
996 * foreseeable future, this will be the case with public key packets.
997 *
998 * Returns zero on success; non-zero otherwise
999 */
Tyler Hicksb59db432011-11-21 17:31:02 -06001000int ecryptfs_new_file_context(struct inode *ecryptfs_inode)
Michael Halcrow237fead2006-10-04 02:16:22 -07001001{
Michael Halcrow237fead2006-10-04 02:16:22 -07001002 struct ecryptfs_crypt_stat *crypt_stat =
Tyler Hicksb59db432011-11-21 17:31:02 -06001003 &ecryptfs_inode_to_private(ecryptfs_inode)->crypt_stat;
Michael Halcrow237fead2006-10-04 02:16:22 -07001004 struct ecryptfs_mount_crypt_stat *mount_crypt_stat =
1005 &ecryptfs_superblock_to_private(
Tyler Hicksb59db432011-11-21 17:31:02 -06001006 ecryptfs_inode->i_sb)->mount_crypt_stat;
Michael Halcrow237fead2006-10-04 02:16:22 -07001007 int cipher_name_len;
Michael Halcrowf4aad162007-10-16 01:27:53 -07001008 int rc = 0;
Michael Halcrow237fead2006-10-04 02:16:22 -07001009
1010 ecryptfs_set_default_crypt_stat_vals(crypt_stat, mount_crypt_stat);
Michael Halcrowaf655dc2007-10-16 01:28:00 -07001011 crypt_stat->flags |= (ECRYPTFS_ENCRYPTED | ECRYPTFS_KEY_VALID);
Michael Halcrowf4aad162007-10-16 01:27:53 -07001012 ecryptfs_copy_mount_wide_flags_to_inode_flags(crypt_stat,
1013 mount_crypt_stat);
1014 rc = ecryptfs_copy_mount_wide_sigs_to_inode_sigs(crypt_stat,
1015 mount_crypt_stat);
1016 if (rc) {
1017 printk(KERN_ERR "Error attempting to copy mount-wide key sigs "
1018 "to the inode key sigs; rc = [%d]\n", rc);
1019 goto out;
1020 }
1021 cipher_name_len =
1022 strlen(mount_crypt_stat->global_default_cipher_name);
1023 memcpy(crypt_stat->cipher,
1024 mount_crypt_stat->global_default_cipher_name,
1025 cipher_name_len);
1026 crypt_stat->cipher[cipher_name_len] = '\0';
1027 crypt_stat->key_size =
1028 mount_crypt_stat->global_default_cipher_key_size;
1029 ecryptfs_generate_new_key(crypt_stat);
Michael Halcrow237fead2006-10-04 02:16:22 -07001030 rc = ecryptfs_init_crypt_ctx(crypt_stat);
1031 if (rc)
1032 ecryptfs_printk(KERN_ERR, "Error initializing cryptographic "
1033 "context for cipher [%s]: rc = [%d]\n",
1034 crypt_stat->cipher, rc);
Michael Halcrowf4aad162007-10-16 01:27:53 -07001035out:
Michael Halcrow237fead2006-10-04 02:16:22 -07001036 return rc;
1037}
1038
1039/**
Tyler Hicks7a866172011-05-02 00:39:54 -05001040 * ecryptfs_validate_marker - check for the ecryptfs marker
Michael Halcrow237fead2006-10-04 02:16:22 -07001041 * @data: The data block in which to check
1042 *
Tyler Hicks7a866172011-05-02 00:39:54 -05001043 * Returns zero if marker found; -EINVAL if not found
Michael Halcrow237fead2006-10-04 02:16:22 -07001044 */
Tyler Hicks7a866172011-05-02 00:39:54 -05001045static int ecryptfs_validate_marker(char *data)
Michael Halcrow237fead2006-10-04 02:16:22 -07001046{
1047 u32 m_1, m_2;
1048
Harvey Harrison29335c62008-07-23 21:30:06 -07001049 m_1 = get_unaligned_be32(data);
1050 m_2 = get_unaligned_be32(data + 4);
Michael Halcrow237fead2006-10-04 02:16:22 -07001051 if ((m_1 ^ MAGIC_ECRYPTFS_MARKER) == m_2)
Tyler Hicks7a866172011-05-02 00:39:54 -05001052 return 0;
Michael Halcrow237fead2006-10-04 02:16:22 -07001053 ecryptfs_printk(KERN_DEBUG, "m_1 = [0x%.8x]; m_2 = [0x%.8x]; "
1054 "MAGIC_ECRYPTFS_MARKER = [0x%.8x]\n", m_1, m_2,
1055 MAGIC_ECRYPTFS_MARKER);
1056 ecryptfs_printk(KERN_DEBUG, "(m_1 ^ MAGIC_ECRYPTFS_MARKER) = "
1057 "[0x%.8x]\n", (m_1 ^ MAGIC_ECRYPTFS_MARKER));
Tyler Hicks7a866172011-05-02 00:39:54 -05001058 return -EINVAL;
Michael Halcrow237fead2006-10-04 02:16:22 -07001059}
1060
1061struct ecryptfs_flag_map_elem {
1062 u32 file_flag;
1063 u32 local_flag;
1064};
1065
1066/* Add support for additional flags by adding elements here. */
1067static struct ecryptfs_flag_map_elem ecryptfs_flag_map[] = {
1068 {0x00000001, ECRYPTFS_ENABLE_HMAC},
Michael Halcrowdd2a3b72007-02-12 00:53:46 -08001069 {0x00000002, ECRYPTFS_ENCRYPTED},
Michael Halcrowaddd65ad2009-01-06 14:42:00 -08001070 {0x00000004, ECRYPTFS_METADATA_IN_XATTR},
1071 {0x00000008, ECRYPTFS_ENCRYPT_FILENAMES}
Michael Halcrow237fead2006-10-04 02:16:22 -07001072};
1073
1074/**
1075 * ecryptfs_process_flags
Michael Halcrow22e78fa2007-10-16 01:28:02 -07001076 * @crypt_stat: The cryptographic context
Michael Halcrow237fead2006-10-04 02:16:22 -07001077 * @page_virt: Source data to be parsed
1078 * @bytes_read: Updated with the number of bytes read
1079 *
1080 * Returns zero on success; non-zero if the flag set is invalid
1081 */
1082static int ecryptfs_process_flags(struct ecryptfs_crypt_stat *crypt_stat,
1083 char *page_virt, int *bytes_read)
1084{
1085 int rc = 0;
1086 int i;
1087 u32 flags;
1088
Harvey Harrison29335c62008-07-23 21:30:06 -07001089 flags = get_unaligned_be32(page_virt);
Michael Halcrow237fead2006-10-04 02:16:22 -07001090 for (i = 0; i < ((sizeof(ecryptfs_flag_map)
1091 / sizeof(struct ecryptfs_flag_map_elem))); i++)
1092 if (flags & ecryptfs_flag_map[i].file_flag) {
Michael Halcrowe2bd99e2007-02-12 00:53:49 -08001093 crypt_stat->flags |= ecryptfs_flag_map[i].local_flag;
Michael Halcrow237fead2006-10-04 02:16:22 -07001094 } else
Michael Halcrowe2bd99e2007-02-12 00:53:49 -08001095 crypt_stat->flags &= ~(ecryptfs_flag_map[i].local_flag);
Michael Halcrow237fead2006-10-04 02:16:22 -07001096 /* Version is in top 8 bits of the 32-bit flag vector */
1097 crypt_stat->file_version = ((flags >> 24) & 0xFF);
1098 (*bytes_read) = 4;
1099 return rc;
1100}
1101
1102/**
1103 * write_ecryptfs_marker
1104 * @page_virt: The pointer to in a page to begin writing the marker
1105 * @written: Number of bytes written
1106 *
1107 * Marker = 0x3c81b7f5
1108 */
1109static void write_ecryptfs_marker(char *page_virt, size_t *written)
1110{
1111 u32 m_1, m_2;
1112
1113 get_random_bytes(&m_1, (MAGIC_ECRYPTFS_MARKER_SIZE_BYTES / 2));
1114 m_2 = (m_1 ^ MAGIC_ECRYPTFS_MARKER);
Harvey Harrison29335c62008-07-23 21:30:06 -07001115 put_unaligned_be32(m_1, page_virt);
1116 page_virt += (MAGIC_ECRYPTFS_MARKER_SIZE_BYTES / 2);
1117 put_unaligned_be32(m_2, page_virt);
Michael Halcrow237fead2006-10-04 02:16:22 -07001118 (*written) = MAGIC_ECRYPTFS_MARKER_SIZE_BYTES;
1119}
1120
Tyler Hicksf4e60e62010-02-11 00:02:32 -06001121void ecryptfs_write_crypt_stat_flags(char *page_virt,
1122 struct ecryptfs_crypt_stat *crypt_stat,
1123 size_t *written)
Michael Halcrow237fead2006-10-04 02:16:22 -07001124{
1125 u32 flags = 0;
1126 int i;
1127
1128 for (i = 0; i < ((sizeof(ecryptfs_flag_map)
1129 / sizeof(struct ecryptfs_flag_map_elem))); i++)
Michael Halcrowe2bd99e2007-02-12 00:53:49 -08001130 if (crypt_stat->flags & ecryptfs_flag_map[i].local_flag)
Michael Halcrow237fead2006-10-04 02:16:22 -07001131 flags |= ecryptfs_flag_map[i].file_flag;
1132 /* Version is in top 8 bits of the 32-bit flag vector */
1133 flags |= ((((u8)crypt_stat->file_version) << 24) & 0xFF000000);
Harvey Harrison29335c62008-07-23 21:30:06 -07001134 put_unaligned_be32(flags, page_virt);
Michael Halcrow237fead2006-10-04 02:16:22 -07001135 (*written) = 4;
1136}
1137
1138struct ecryptfs_cipher_code_str_map_elem {
1139 char cipher_str[16];
Trevor Highland19e66a62008-02-06 01:38:36 -08001140 u8 cipher_code;
Michael Halcrow237fead2006-10-04 02:16:22 -07001141};
1142
1143/* Add support for additional ciphers by adding elements here. The
1144 * cipher_code is whatever OpenPGP applicatoins use to identify the
1145 * ciphers. List in order of probability. */
1146static struct ecryptfs_cipher_code_str_map_elem
1147ecryptfs_cipher_code_str_map[] = {
1148 {"aes",RFC2440_CIPHER_AES_128 },
1149 {"blowfish", RFC2440_CIPHER_BLOWFISH},
1150 {"des3_ede", RFC2440_CIPHER_DES3_EDE},
1151 {"cast5", RFC2440_CIPHER_CAST_5},
1152 {"twofish", RFC2440_CIPHER_TWOFISH},
1153 {"cast6", RFC2440_CIPHER_CAST_6},
1154 {"aes", RFC2440_CIPHER_AES_192},
1155 {"aes", RFC2440_CIPHER_AES_256}
1156};
1157
1158/**
1159 * ecryptfs_code_for_cipher_string
Michael Halcrow9c79f342009-01-06 14:41:57 -08001160 * @cipher_name: The string alias for the cipher
1161 * @key_bytes: Length of key in bytes; used for AES code selection
Michael Halcrow237fead2006-10-04 02:16:22 -07001162 *
1163 * Returns zero on no match, or the cipher code on match
1164 */
Michael Halcrow9c79f342009-01-06 14:41:57 -08001165u8 ecryptfs_code_for_cipher_string(char *cipher_name, size_t key_bytes)
Michael Halcrow237fead2006-10-04 02:16:22 -07001166{
1167 int i;
Trevor Highland19e66a62008-02-06 01:38:36 -08001168 u8 code = 0;
Michael Halcrow237fead2006-10-04 02:16:22 -07001169 struct ecryptfs_cipher_code_str_map_elem *map =
1170 ecryptfs_cipher_code_str_map;
1171
Michael Halcrow9c79f342009-01-06 14:41:57 -08001172 if (strcmp(cipher_name, "aes") == 0) {
1173 switch (key_bytes) {
Michael Halcrow237fead2006-10-04 02:16:22 -07001174 case 16:
1175 code = RFC2440_CIPHER_AES_128;
1176 break;
1177 case 24:
1178 code = RFC2440_CIPHER_AES_192;
1179 break;
1180 case 32:
1181 code = RFC2440_CIPHER_AES_256;
1182 }
1183 } else {
1184 for (i = 0; i < ARRAY_SIZE(ecryptfs_cipher_code_str_map); i++)
Michael Halcrow9c79f342009-01-06 14:41:57 -08001185 if (strcmp(cipher_name, map[i].cipher_str) == 0) {
Michael Halcrow237fead2006-10-04 02:16:22 -07001186 code = map[i].cipher_code;
1187 break;
1188 }
1189 }
1190 return code;
1191}
1192
1193/**
1194 * ecryptfs_cipher_code_to_string
1195 * @str: Destination to write out the cipher name
1196 * @cipher_code: The code to convert to cipher name string
1197 *
1198 * Returns zero on success
1199 */
Trevor Highland19e66a62008-02-06 01:38:36 -08001200int ecryptfs_cipher_code_to_string(char *str, u8 cipher_code)
Michael Halcrow237fead2006-10-04 02:16:22 -07001201{
1202 int rc = 0;
1203 int i;
1204
1205 str[0] = '\0';
1206 for (i = 0; i < ARRAY_SIZE(ecryptfs_cipher_code_str_map); i++)
1207 if (cipher_code == ecryptfs_cipher_code_str_map[i].cipher_code)
1208 strcpy(str, ecryptfs_cipher_code_str_map[i].cipher_str);
1209 if (str[0] == '\0') {
1210 ecryptfs_printk(KERN_WARNING, "Cipher code not recognized: "
1211 "[%d]\n", cipher_code);
1212 rc = -EINVAL;
1213 }
1214 return rc;
1215}
1216
Tyler Hicks778aeb42011-05-24 04:56:23 -05001217int ecryptfs_read_and_validate_header_region(struct inode *inode)
Michael Halcrowdd2a3b72007-02-12 00:53:46 -08001218{
Tyler Hicks778aeb42011-05-24 04:56:23 -05001219 u8 file_size[ECRYPTFS_SIZE_AND_MARKER_BYTES];
1220 u8 *marker = file_size + ECRYPTFS_FILE_SIZE_BYTES;
Michael Halcrowdd2a3b72007-02-12 00:53:46 -08001221 int rc;
1222
Tyler Hicks778aeb42011-05-24 04:56:23 -05001223 rc = ecryptfs_read_lower(file_size, 0, ECRYPTFS_SIZE_AND_MARKER_BYTES,
1224 inode);
1225 if (rc < ECRYPTFS_SIZE_AND_MARKER_BYTES)
1226 return rc >= 0 ? -EINVAL : rc;
1227 rc = ecryptfs_validate_marker(marker);
1228 if (!rc)
1229 ecryptfs_i_size_init(file_size, inode);
Michael Halcrowdd2a3b72007-02-12 00:53:46 -08001230 return rc;
1231}
1232
Michael Halcrowe77a56d2007-02-12 00:53:47 -08001233void
1234ecryptfs_write_header_metadata(char *virt,
1235 struct ecryptfs_crypt_stat *crypt_stat,
1236 size_t *written)
Michael Halcrow237fead2006-10-04 02:16:22 -07001237{
1238 u32 header_extent_size;
1239 u16 num_header_extents_at_front;
1240
Michael Halcrow45eaab72007-10-16 01:28:05 -07001241 header_extent_size = (u32)crypt_stat->extent_size;
Michael Halcrow237fead2006-10-04 02:16:22 -07001242 num_header_extents_at_front =
Tyler Hicksfa3ef1c2010-02-11 05:09:14 -06001243 (u16)(crypt_stat->metadata_size / crypt_stat->extent_size);
Harvey Harrison29335c62008-07-23 21:30:06 -07001244 put_unaligned_be32(header_extent_size, virt);
Michael Halcrow237fead2006-10-04 02:16:22 -07001245 virt += 4;
Harvey Harrison29335c62008-07-23 21:30:06 -07001246 put_unaligned_be16(num_header_extents_at_front, virt);
Michael Halcrow237fead2006-10-04 02:16:22 -07001247 (*written) = 6;
1248}
1249
Tyler Hicks30632872011-05-24 05:11:12 -05001250struct kmem_cache *ecryptfs_header_cache;
Michael Halcrow237fead2006-10-04 02:16:22 -07001251
1252/**
1253 * ecryptfs_write_headers_virt
Michael Halcrow22e78fa2007-10-16 01:28:02 -07001254 * @page_virt: The virtual address to write the headers to
Eric Sandeen87b811c2008-10-29 14:01:08 -07001255 * @max: The size of memory allocated at page_virt
Michael Halcrow22e78fa2007-10-16 01:28:02 -07001256 * @size: Set to the number of bytes written by this function
1257 * @crypt_stat: The cryptographic context
1258 * @ecryptfs_dentry: The eCryptfs dentry
Michael Halcrow237fead2006-10-04 02:16:22 -07001259 *
1260 * Format version: 1
1261 *
1262 * Header Extent:
1263 * Octets 0-7: Unencrypted file size (big-endian)
1264 * Octets 8-15: eCryptfs special marker
1265 * Octets 16-19: Flags
1266 * Octet 16: File format version number (between 0 and 255)
1267 * Octets 17-18: Reserved
1268 * Octet 19: Bit 1 (lsb): Reserved
1269 * Bit 2: Encrypted?
1270 * Bits 3-8: Reserved
1271 * Octets 20-23: Header extent size (big-endian)
1272 * Octets 24-25: Number of header extents at front of file
1273 * (big-endian)
1274 * Octet 26: Begin RFC 2440 authentication token packet set
1275 * Data Extent 0:
1276 * Lower data (CBC encrypted)
1277 * Data Extent 1:
1278 * Lower data (CBC encrypted)
1279 * ...
1280 *
1281 * Returns zero on success
1282 */
Eric Sandeen87b811c2008-10-29 14:01:08 -07001283static int ecryptfs_write_headers_virt(char *page_virt, size_t max,
1284 size_t *size,
Michael Halcrowdd2a3b72007-02-12 00:53:46 -08001285 struct ecryptfs_crypt_stat *crypt_stat,
1286 struct dentry *ecryptfs_dentry)
Michael Halcrow237fead2006-10-04 02:16:22 -07001287{
1288 int rc;
1289 size_t written;
1290 size_t offset;
1291
1292 offset = ECRYPTFS_FILE_SIZE_BYTES;
1293 write_ecryptfs_marker((page_virt + offset), &written);
1294 offset += written;
Tyler Hicksf4e60e62010-02-11 00:02:32 -06001295 ecryptfs_write_crypt_stat_flags((page_virt + offset), crypt_stat,
1296 &written);
Michael Halcrow237fead2006-10-04 02:16:22 -07001297 offset += written;
Michael Halcrowe77a56d2007-02-12 00:53:47 -08001298 ecryptfs_write_header_metadata((page_virt + offset), crypt_stat,
1299 &written);
Michael Halcrow237fead2006-10-04 02:16:22 -07001300 offset += written;
1301 rc = ecryptfs_generate_key_packet_set((page_virt + offset), crypt_stat,
1302 ecryptfs_dentry, &written,
Eric Sandeen87b811c2008-10-29 14:01:08 -07001303 max - offset);
Michael Halcrow237fead2006-10-04 02:16:22 -07001304 if (rc)
1305 ecryptfs_printk(KERN_WARNING, "Error generating key packet "
1306 "set; rc = [%d]\n", rc);
Michael Halcrowdd2a3b72007-02-12 00:53:46 -08001307 if (size) {
1308 offset += written;
1309 *size = offset;
1310 }
1311 return rc;
1312}
1313
Michael Halcrow22e78fa2007-10-16 01:28:02 -07001314static int
Tyler Hicksb59db432011-11-21 17:31:02 -06001315ecryptfs_write_metadata_to_contents(struct inode *ecryptfs_inode,
Tyler Hicks8faece52009-03-20 01:25:09 -05001316 char *virt, size_t virt_len)
Michael Halcrowdd2a3b72007-02-12 00:53:46 -08001317{
Michael Halcrowd7cdc5f2007-10-16 01:28:10 -07001318 int rc;
Michael Halcrowdd2a3b72007-02-12 00:53:46 -08001319
Tyler Hicksb59db432011-11-21 17:31:02 -06001320 rc = ecryptfs_write_lower(ecryptfs_inode, virt,
Tyler Hicks8faece52009-03-20 01:25:09 -05001321 0, virt_len);
Tyler Hicks96a7b9c2009-09-16 19:04:20 -05001322 if (rc < 0)
Michael Halcrowd7cdc5f2007-10-16 01:28:10 -07001323 printk(KERN_ERR "%s: Error attempting to write header "
Tyler Hicks96a7b9c2009-09-16 19:04:20 -05001324 "information to lower file; rc = [%d]\n", __func__, rc);
1325 else
1326 rc = 0;
Michael Halcrow70456602007-02-12 00:53:48 -08001327 return rc;
Michael Halcrowdd2a3b72007-02-12 00:53:46 -08001328}
1329
Michael Halcrow22e78fa2007-10-16 01:28:02 -07001330static int
1331ecryptfs_write_metadata_to_xattr(struct dentry *ecryptfs_dentry,
Michael Halcrow22e78fa2007-10-16 01:28:02 -07001332 char *page_virt, size_t size)
Michael Halcrowdd2a3b72007-02-12 00:53:46 -08001333{
1334 int rc;
1335
1336 rc = ecryptfs_setxattr(ecryptfs_dentry, ECRYPTFS_XATTR_NAME, page_virt,
1337 size, 0);
Michael Halcrow237fead2006-10-04 02:16:22 -07001338 return rc;
1339}
1340
Tyler Hicks8faece52009-03-20 01:25:09 -05001341static unsigned long ecryptfs_get_zeroed_pages(gfp_t gfp_mask,
1342 unsigned int order)
1343{
1344 struct page *page;
1345
1346 page = alloc_pages(gfp_mask | __GFP_ZERO, order);
1347 if (page)
1348 return (unsigned long) page_address(page);
1349 return 0;
1350}
1351
Michael Halcrow237fead2006-10-04 02:16:22 -07001352/**
Michael Halcrowdd2a3b72007-02-12 00:53:46 -08001353 * ecryptfs_write_metadata
Tyler Hicksb59db432011-11-21 17:31:02 -06001354 * @ecryptfs_dentry: The eCryptfs dentry, which should be negative
1355 * @ecryptfs_inode: The newly created eCryptfs inode
Michael Halcrow237fead2006-10-04 02:16:22 -07001356 *
1357 * Write the file headers out. This will likely involve a userspace
1358 * callout, in which the session key is encrypted with one or more
1359 * public keys and/or the passphrase necessary to do the encryption is
1360 * retrieved via a prompt. Exactly what happens at this point should
1361 * be policy-dependent.
1362 *
1363 * Returns zero on success; non-zero on error
1364 */
Tyler Hicksb59db432011-11-21 17:31:02 -06001365int ecryptfs_write_metadata(struct dentry *ecryptfs_dentry,
1366 struct inode *ecryptfs_inode)
Michael Halcrow237fead2006-10-04 02:16:22 -07001367{
Michael Halcrowd7cdc5f2007-10-16 01:28:10 -07001368 struct ecryptfs_crypt_stat *crypt_stat =
Tyler Hicksb59db432011-11-21 17:31:02 -06001369 &ecryptfs_inode_to_private(ecryptfs_inode)->crypt_stat;
Tyler Hicks8faece52009-03-20 01:25:09 -05001370 unsigned int order;
Michael Halcrowcc11bef2008-02-06 01:38:32 -08001371 char *virt;
Tyler Hicks8faece52009-03-20 01:25:09 -05001372 size_t virt_len;
Michael Halcrowd7cdc5f2007-10-16 01:28:10 -07001373 size_t size = 0;
Michael Halcrow237fead2006-10-04 02:16:22 -07001374 int rc = 0;
1375
Michael Halcrowe2bd99e2007-02-12 00:53:49 -08001376 if (likely(crypt_stat->flags & ECRYPTFS_ENCRYPTED)) {
1377 if (!(crypt_stat->flags & ECRYPTFS_KEY_VALID)) {
Michael Halcrowd7cdc5f2007-10-16 01:28:10 -07001378 printk(KERN_ERR "Key is invalid; bailing out\n");
Michael Halcrow237fead2006-10-04 02:16:22 -07001379 rc = -EINVAL;
1380 goto out;
1381 }
1382 } else {
Michael Halcrowcc11bef2008-02-06 01:38:32 -08001383 printk(KERN_WARNING "%s: Encrypted flag not set\n",
Harvey Harrison18d1dbf2008-04-29 00:59:48 -07001384 __func__);
Michael Halcrow237fead2006-10-04 02:16:22 -07001385 rc = -EINVAL;
Michael Halcrow237fead2006-10-04 02:16:22 -07001386 goto out;
1387 }
Tyler Hicksfa3ef1c2010-02-11 05:09:14 -06001388 virt_len = crypt_stat->metadata_size;
Tyler Hicks8faece52009-03-20 01:25:09 -05001389 order = get_order(virt_len);
Michael Halcrow237fead2006-10-04 02:16:22 -07001390 /* Released in this function */
Tyler Hicks8faece52009-03-20 01:25:09 -05001391 virt = (char *)ecryptfs_get_zeroed_pages(GFP_KERNEL, order);
Michael Halcrowcc11bef2008-02-06 01:38:32 -08001392 if (!virt) {
Harvey Harrison18d1dbf2008-04-29 00:59:48 -07001393 printk(KERN_ERR "%s: Out of memory\n", __func__);
Michael Halcrow237fead2006-10-04 02:16:22 -07001394 rc = -ENOMEM;
1395 goto out;
1396 }
Tyler Hicksbd4f0fe2011-02-23 00:14:19 -06001397 /* Zeroed page ensures the in-header unencrypted i_size is set to 0 */
Tyler Hicks8faece52009-03-20 01:25:09 -05001398 rc = ecryptfs_write_headers_virt(virt, virt_len, &size, crypt_stat,
1399 ecryptfs_dentry);
Michael Halcrow237fead2006-10-04 02:16:22 -07001400 if (unlikely(rc)) {
Michael Halcrowcc11bef2008-02-06 01:38:32 -08001401 printk(KERN_ERR "%s: Error whilst writing headers; rc = [%d]\n",
Harvey Harrison18d1dbf2008-04-29 00:59:48 -07001402 __func__, rc);
Michael Halcrow237fead2006-10-04 02:16:22 -07001403 goto out_free;
1404 }
Michael Halcrowdd2a3b72007-02-12 00:53:46 -08001405 if (crypt_stat->flags & ECRYPTFS_METADATA_IN_XATTR)
Tyler Hicks8faece52009-03-20 01:25:09 -05001406 rc = ecryptfs_write_metadata_to_xattr(ecryptfs_dentry, virt,
1407 size);
Michael Halcrowdd2a3b72007-02-12 00:53:46 -08001408 else
Tyler Hicksb59db432011-11-21 17:31:02 -06001409 rc = ecryptfs_write_metadata_to_contents(ecryptfs_inode, virt,
Tyler Hicks8faece52009-03-20 01:25:09 -05001410 virt_len);
Michael Halcrowdd2a3b72007-02-12 00:53:46 -08001411 if (rc) {
Michael Halcrowcc11bef2008-02-06 01:38:32 -08001412 printk(KERN_ERR "%s: Error writing metadata out to lower file; "
Harvey Harrison18d1dbf2008-04-29 00:59:48 -07001413 "rc = [%d]\n", __func__, rc);
Michael Halcrowdd2a3b72007-02-12 00:53:46 -08001414 goto out_free;
Michael Halcrow237fead2006-10-04 02:16:22 -07001415 }
Michael Halcrow237fead2006-10-04 02:16:22 -07001416out_free:
Tyler Hicks8faece52009-03-20 01:25:09 -05001417 free_pages((unsigned long)virt, order);
Michael Halcrow237fead2006-10-04 02:16:22 -07001418out:
1419 return rc;
1420}
1421
Michael Halcrowdd2a3b72007-02-12 00:53:46 -08001422#define ECRYPTFS_DONT_VALIDATE_HEADER_SIZE 0
1423#define ECRYPTFS_VALIDATE_HEADER_SIZE 1
Michael Halcrow237fead2006-10-04 02:16:22 -07001424static int parse_header_metadata(struct ecryptfs_crypt_stat *crypt_stat,
Michael Halcrowdd2a3b72007-02-12 00:53:46 -08001425 char *virt, int *bytes_read,
1426 int validate_header_size)
Michael Halcrow237fead2006-10-04 02:16:22 -07001427{
1428 int rc = 0;
1429 u32 header_extent_size;
1430 u16 num_header_extents_at_front;
1431
Harvey Harrison29335c62008-07-23 21:30:06 -07001432 header_extent_size = get_unaligned_be32(virt);
1433 virt += sizeof(__be32);
1434 num_header_extents_at_front = get_unaligned_be16(virt);
Tyler Hicksfa3ef1c2010-02-11 05:09:14 -06001435 crypt_stat->metadata_size = (((size_t)num_header_extents_at_front
1436 * (size_t)header_extent_size));
Harvey Harrison29335c62008-07-23 21:30:06 -07001437 (*bytes_read) = (sizeof(__be32) + sizeof(__be16));
Michael Halcrowdd2a3b72007-02-12 00:53:46 -08001438 if ((validate_header_size == ECRYPTFS_VALIDATE_HEADER_SIZE)
Tyler Hicksfa3ef1c2010-02-11 05:09:14 -06001439 && (crypt_stat->metadata_size
Michael Halcrowdd2a3b72007-02-12 00:53:46 -08001440 < ECRYPTFS_MINIMUM_HEADER_EXTENT_SIZE)) {
Michael Halcrow237fead2006-10-04 02:16:22 -07001441 rc = -EINVAL;
Michael Halcrowcc11bef2008-02-06 01:38:32 -08001442 printk(KERN_WARNING "Invalid header size: [%zd]\n",
Tyler Hicksfa3ef1c2010-02-11 05:09:14 -06001443 crypt_stat->metadata_size);
Michael Halcrow237fead2006-10-04 02:16:22 -07001444 }
1445 return rc;
1446}
1447
1448/**
1449 * set_default_header_data
Michael Halcrow22e78fa2007-10-16 01:28:02 -07001450 * @crypt_stat: The cryptographic context
Michael Halcrow237fead2006-10-04 02:16:22 -07001451 *
1452 * For version 0 file format; this function is only for backwards
1453 * compatibility for files created with the prior versions of
1454 * eCryptfs.
1455 */
1456static void set_default_header_data(struct ecryptfs_crypt_stat *crypt_stat)
1457{
Tyler Hicksfa3ef1c2010-02-11 05:09:14 -06001458 crypt_stat->metadata_size = ECRYPTFS_MINIMUM_HEADER_EXTENT_SIZE;
Michael Halcrow237fead2006-10-04 02:16:22 -07001459}
1460
Tyler Hicks3aeb86e2011-03-15 14:54:00 -05001461void ecryptfs_i_size_init(const char *page_virt, struct inode *inode)
1462{
1463 struct ecryptfs_mount_crypt_stat *mount_crypt_stat;
1464 struct ecryptfs_crypt_stat *crypt_stat;
1465 u64 file_size;
1466
1467 crypt_stat = &ecryptfs_inode_to_private(inode)->crypt_stat;
1468 mount_crypt_stat =
1469 &ecryptfs_superblock_to_private(inode->i_sb)->mount_crypt_stat;
1470 if (mount_crypt_stat->flags & ECRYPTFS_ENCRYPTED_VIEW_ENABLED) {
1471 file_size = i_size_read(ecryptfs_inode_to_lower(inode));
1472 if (crypt_stat->flags & ECRYPTFS_METADATA_IN_XATTR)
1473 file_size += crypt_stat->metadata_size;
1474 } else
1475 file_size = get_unaligned_be64(page_virt);
1476 i_size_write(inode, (loff_t)file_size);
1477 crypt_stat->flags |= ECRYPTFS_I_SIZE_INITIALIZED;
1478}
1479
Michael Halcrow237fead2006-10-04 02:16:22 -07001480/**
1481 * ecryptfs_read_headers_virt
Michael Halcrow22e78fa2007-10-16 01:28:02 -07001482 * @page_virt: The virtual address into which to read the headers
1483 * @crypt_stat: The cryptographic context
1484 * @ecryptfs_dentry: The eCryptfs dentry
1485 * @validate_header_size: Whether to validate the header size while reading
Michael Halcrow237fead2006-10-04 02:16:22 -07001486 *
1487 * Read/parse the header data. The header format is detailed in the
1488 * comment block for the ecryptfs_write_headers_virt() function.
1489 *
1490 * Returns zero on success
1491 */
1492static int ecryptfs_read_headers_virt(char *page_virt,
1493 struct ecryptfs_crypt_stat *crypt_stat,
Michael Halcrowdd2a3b72007-02-12 00:53:46 -08001494 struct dentry *ecryptfs_dentry,
1495 int validate_header_size)
Michael Halcrow237fead2006-10-04 02:16:22 -07001496{
1497 int rc = 0;
1498 int offset;
1499 int bytes_read;
1500
1501 ecryptfs_set_default_sizes(crypt_stat);
1502 crypt_stat->mount_crypt_stat = &ecryptfs_superblock_to_private(
1503 ecryptfs_dentry->d_sb)->mount_crypt_stat;
1504 offset = ECRYPTFS_FILE_SIZE_BYTES;
Tyler Hicks7a866172011-05-02 00:39:54 -05001505 rc = ecryptfs_validate_marker(page_virt + offset);
1506 if (rc)
Michael Halcrow237fead2006-10-04 02:16:22 -07001507 goto out;
Tyler Hicks3aeb86e2011-03-15 14:54:00 -05001508 if (!(crypt_stat->flags & ECRYPTFS_I_SIZE_INITIALIZED))
1509 ecryptfs_i_size_init(page_virt, ecryptfs_dentry->d_inode);
Michael Halcrow237fead2006-10-04 02:16:22 -07001510 offset += MAGIC_ECRYPTFS_MARKER_SIZE_BYTES;
1511 rc = ecryptfs_process_flags(crypt_stat, (page_virt + offset),
1512 &bytes_read);
1513 if (rc) {
1514 ecryptfs_printk(KERN_WARNING, "Error processing flags\n");
1515 goto out;
1516 }
1517 if (crypt_stat->file_version > ECRYPTFS_SUPPORTED_FILE_VERSION) {
1518 ecryptfs_printk(KERN_WARNING, "File version is [%d]; only "
1519 "file version [%d] is supported by this "
1520 "version of eCryptfs\n",
1521 crypt_stat->file_version,
1522 ECRYPTFS_SUPPORTED_FILE_VERSION);
1523 rc = -EINVAL;
1524 goto out;
1525 }
1526 offset += bytes_read;
1527 if (crypt_stat->file_version >= 1) {
1528 rc = parse_header_metadata(crypt_stat, (page_virt + offset),
Michael Halcrowdd2a3b72007-02-12 00:53:46 -08001529 &bytes_read, validate_header_size);
Michael Halcrow237fead2006-10-04 02:16:22 -07001530 if (rc) {
1531 ecryptfs_printk(KERN_WARNING, "Error reading header "
1532 "metadata; rc = [%d]\n", rc);
1533 }
1534 offset += bytes_read;
1535 } else
1536 set_default_header_data(crypt_stat);
1537 rc = ecryptfs_parse_packet_set(crypt_stat, (page_virt + offset),
1538 ecryptfs_dentry);
1539out:
1540 return rc;
1541}
1542
1543/**
Michael Halcrowdd2a3b72007-02-12 00:53:46 -08001544 * ecryptfs_read_xattr_region
Michael Halcrow22e78fa2007-10-16 01:28:02 -07001545 * @page_virt: The vitual address into which to read the xattr data
Michael Halcrow2ed92552007-10-16 01:28:10 -07001546 * @ecryptfs_inode: The eCryptfs inode
Michael Halcrowdd2a3b72007-02-12 00:53:46 -08001547 *
1548 * Attempts to read the crypto metadata from the extended attribute
1549 * region of the lower file.
Michael Halcrow22e78fa2007-10-16 01:28:02 -07001550 *
1551 * Returns zero on success; non-zero on error
Michael Halcrowdd2a3b72007-02-12 00:53:46 -08001552 */
Michael Halcrowd7cdc5f2007-10-16 01:28:10 -07001553int ecryptfs_read_xattr_region(char *page_virt, struct inode *ecryptfs_inode)
Michael Halcrowdd2a3b72007-02-12 00:53:46 -08001554{
Michael Halcrowd7cdc5f2007-10-16 01:28:10 -07001555 struct dentry *lower_dentry =
1556 ecryptfs_inode_to_private(ecryptfs_inode)->lower_file->f_dentry;
Michael Halcrowdd2a3b72007-02-12 00:53:46 -08001557 ssize_t size;
1558 int rc = 0;
1559
Michael Halcrowd7cdc5f2007-10-16 01:28:10 -07001560 size = ecryptfs_getxattr_lower(lower_dentry, ECRYPTFS_XATTR_NAME,
1561 page_virt, ECRYPTFS_DEFAULT_EXTENT_SIZE);
Michael Halcrowdd2a3b72007-02-12 00:53:46 -08001562 if (size < 0) {
Michael Halcrow25bd8172008-02-06 01:38:35 -08001563 if (unlikely(ecryptfs_verbosity > 0))
1564 printk(KERN_INFO "Error attempting to read the [%s] "
1565 "xattr from the lower file; return value = "
1566 "[%zd]\n", ECRYPTFS_XATTR_NAME, size);
Michael Halcrowdd2a3b72007-02-12 00:53:46 -08001567 rc = -EINVAL;
1568 goto out;
1569 }
1570out:
1571 return rc;
1572}
1573
Tyler Hicks778aeb42011-05-24 04:56:23 -05001574int ecryptfs_read_and_validate_xattr_region(struct dentry *dentry,
Tyler Hicks3b06b3e2011-05-24 03:49:02 -05001575 struct inode *inode)
Michael Halcrowdd2a3b72007-02-12 00:53:46 -08001576{
Tyler Hicks778aeb42011-05-24 04:56:23 -05001577 u8 file_size[ECRYPTFS_SIZE_AND_MARKER_BYTES];
1578 u8 *marker = file_size + ECRYPTFS_FILE_SIZE_BYTES;
Michael Halcrowdd2a3b72007-02-12 00:53:46 -08001579 int rc;
1580
Tyler Hicks778aeb42011-05-24 04:56:23 -05001581 rc = ecryptfs_getxattr_lower(ecryptfs_dentry_to_lower(dentry),
1582 ECRYPTFS_XATTR_NAME, file_size,
1583 ECRYPTFS_SIZE_AND_MARKER_BYTES);
1584 if (rc < ECRYPTFS_SIZE_AND_MARKER_BYTES)
1585 return rc >= 0 ? -EINVAL : rc;
1586 rc = ecryptfs_validate_marker(marker);
1587 if (!rc)
1588 ecryptfs_i_size_init(file_size, inode);
Michael Halcrowdd2a3b72007-02-12 00:53:46 -08001589 return rc;
1590}
1591
1592/**
1593 * ecryptfs_read_metadata
1594 *
1595 * Common entry point for reading file metadata. From here, we could
1596 * retrieve the header information from the header region of the file,
1597 * the xattr region of the file, or some other repostory that is
1598 * stored separately from the file itself. The current implementation
1599 * supports retrieving the metadata information from the file contents
1600 * and from the xattr region.
Michael Halcrow237fead2006-10-04 02:16:22 -07001601 *
1602 * Returns zero if valid headers found and parsed; non-zero otherwise
1603 */
Michael Halcrowd7cdc5f2007-10-16 01:28:10 -07001604int ecryptfs_read_metadata(struct dentry *ecryptfs_dentry)
Michael Halcrow237fead2006-10-04 02:16:22 -07001605{
Tim Gardnerbb450362012-01-12 16:31:55 +01001606 int rc;
1607 char *page_virt;
Michael Halcrowd7cdc5f2007-10-16 01:28:10 -07001608 struct inode *ecryptfs_inode = ecryptfs_dentry->d_inode;
Michael Halcrow237fead2006-10-04 02:16:22 -07001609 struct ecryptfs_crypt_stat *crypt_stat =
Michael Halcrowd7cdc5f2007-10-16 01:28:10 -07001610 &ecryptfs_inode_to_private(ecryptfs_inode)->crypt_stat;
Michael Halcrowe77a56d2007-02-12 00:53:47 -08001611 struct ecryptfs_mount_crypt_stat *mount_crypt_stat =
1612 &ecryptfs_superblock_to_private(
1613 ecryptfs_dentry->d_sb)->mount_crypt_stat;
Michael Halcrow237fead2006-10-04 02:16:22 -07001614
Michael Halcrowe77a56d2007-02-12 00:53:47 -08001615 ecryptfs_copy_mount_wide_flags_to_inode_flags(crypt_stat,
1616 mount_crypt_stat);
Michael Halcrow237fead2006-10-04 02:16:22 -07001617 /* Read the first page from the underlying file */
Tyler Hicks30632872011-05-24 05:11:12 -05001618 page_virt = kmem_cache_alloc(ecryptfs_header_cache, GFP_USER);
Michael Halcrow237fead2006-10-04 02:16:22 -07001619 if (!page_virt) {
1620 rc = -ENOMEM;
Michael Halcrowd7cdc5f2007-10-16 01:28:10 -07001621 printk(KERN_ERR "%s: Unable to allocate page_virt\n",
Harvey Harrison18d1dbf2008-04-29 00:59:48 -07001622 __func__);
Michael Halcrow237fead2006-10-04 02:16:22 -07001623 goto out;
1624 }
Michael Halcrowd7cdc5f2007-10-16 01:28:10 -07001625 rc = ecryptfs_read_lower(page_virt, 0, crypt_stat->extent_size,
1626 ecryptfs_inode);
Tyler Hicks96a7b9c2009-09-16 19:04:20 -05001627 if (rc >= 0)
Michael Halcrowd7cdc5f2007-10-16 01:28:10 -07001628 rc = ecryptfs_read_headers_virt(page_virt, crypt_stat,
1629 ecryptfs_dentry,
1630 ECRYPTFS_VALIDATE_HEADER_SIZE);
Michael Halcrow237fead2006-10-04 02:16:22 -07001631 if (rc) {
Tim Gardnerbb450362012-01-12 16:31:55 +01001632 /* metadata is not in the file header, so try xattrs */
Tyler Hicks1984c232010-02-10 23:17:44 -06001633 memset(page_virt, 0, PAGE_CACHE_SIZE);
Michael Halcrowd7cdc5f2007-10-16 01:28:10 -07001634 rc = ecryptfs_read_xattr_region(page_virt, ecryptfs_inode);
Michael Halcrowdd2a3b72007-02-12 00:53:46 -08001635 if (rc) {
1636 printk(KERN_DEBUG "Valid eCryptfs headers not found in "
Tim Gardner30373dc2012-01-12 16:31:55 +01001637 "file header region or xattr region, inode %lu\n",
1638 ecryptfs_inode->i_ino);
Michael Halcrowdd2a3b72007-02-12 00:53:46 -08001639 rc = -EINVAL;
1640 goto out;
1641 }
1642 rc = ecryptfs_read_headers_virt(page_virt, crypt_stat,
1643 ecryptfs_dentry,
1644 ECRYPTFS_DONT_VALIDATE_HEADER_SIZE);
1645 if (rc) {
1646 printk(KERN_DEBUG "Valid eCryptfs headers not found in "
Tim Gardner30373dc2012-01-12 16:31:55 +01001647 "file xattr region either, inode %lu\n",
1648 ecryptfs_inode->i_ino);
Michael Halcrowdd2a3b72007-02-12 00:53:46 -08001649 rc = -EINVAL;
1650 }
1651 if (crypt_stat->mount_crypt_stat->flags
1652 & ECRYPTFS_XATTR_METADATA_ENABLED) {
1653 crypt_stat->flags |= ECRYPTFS_METADATA_IN_XATTR;
1654 } else {
1655 printk(KERN_WARNING "Attempt to access file with "
1656 "crypto metadata only in the extended attribute "
1657 "region, but eCryptfs was mounted without "
1658 "xattr support enabled. eCryptfs will not treat "
Tim Gardner30373dc2012-01-12 16:31:55 +01001659 "this like an encrypted file, inode %lu\n",
1660 ecryptfs_inode->i_ino);
Michael Halcrowdd2a3b72007-02-12 00:53:46 -08001661 rc = -EINVAL;
1662 }
Michael Halcrow237fead2006-10-04 02:16:22 -07001663 }
1664out:
1665 if (page_virt) {
1666 memset(page_virt, 0, PAGE_CACHE_SIZE);
Tyler Hicks30632872011-05-24 05:11:12 -05001667 kmem_cache_free(ecryptfs_header_cache, page_virt);
Michael Halcrow237fead2006-10-04 02:16:22 -07001668 }
1669 return rc;
1670}
1671
1672/**
Michael Halcrow51ca58d2009-01-06 14:41:59 -08001673 * ecryptfs_encrypt_filename - encrypt filename
1674 *
1675 * CBC-encrypts the filename. We do not want to encrypt the same
1676 * filename with the same key and IV, which may happen with hard
1677 * links, so we prepend random bits to each filename.
1678 *
1679 * Returns zero on success; non-zero otherwise
1680 */
1681static int
1682ecryptfs_encrypt_filename(struct ecryptfs_filename *filename,
1683 struct ecryptfs_crypt_stat *crypt_stat,
1684 struct ecryptfs_mount_crypt_stat *mount_crypt_stat)
1685{
1686 int rc = 0;
1687
1688 filename->encrypted_filename = NULL;
1689 filename->encrypted_filename_size = 0;
1690 if ((crypt_stat && (crypt_stat->flags & ECRYPTFS_ENCFN_USE_MOUNT_FNEK))
1691 || (mount_crypt_stat && (mount_crypt_stat->flags
1692 & ECRYPTFS_GLOBAL_ENCFN_USE_MOUNT_FNEK))) {
1693 size_t packet_size;
1694 size_t remaining_bytes;
1695
1696 rc = ecryptfs_write_tag_70_packet(
1697 NULL, NULL,
1698 &filename->encrypted_filename_size,
1699 mount_crypt_stat, NULL,
1700 filename->filename_size);
1701 if (rc) {
1702 printk(KERN_ERR "%s: Error attempting to get packet "
1703 "size for tag 72; rc = [%d]\n", __func__,
1704 rc);
1705 filename->encrypted_filename_size = 0;
1706 goto out;
1707 }
1708 filename->encrypted_filename =
1709 kmalloc(filename->encrypted_filename_size, GFP_KERNEL);
1710 if (!filename->encrypted_filename) {
1711 printk(KERN_ERR "%s: Out of memory whilst attempting "
Michael Halcrowdf261c52009-01-06 14:42:02 -08001712 "to kmalloc [%zd] bytes\n", __func__,
Michael Halcrow51ca58d2009-01-06 14:41:59 -08001713 filename->encrypted_filename_size);
1714 rc = -ENOMEM;
1715 goto out;
1716 }
1717 remaining_bytes = filename->encrypted_filename_size;
1718 rc = ecryptfs_write_tag_70_packet(filename->encrypted_filename,
1719 &remaining_bytes,
1720 &packet_size,
1721 mount_crypt_stat,
1722 filename->filename,
1723 filename->filename_size);
1724 if (rc) {
1725 printk(KERN_ERR "%s: Error attempting to generate "
1726 "tag 70 packet; rc = [%d]\n", __func__,
1727 rc);
1728 kfree(filename->encrypted_filename);
1729 filename->encrypted_filename = NULL;
1730 filename->encrypted_filename_size = 0;
1731 goto out;
1732 }
1733 filename->encrypted_filename_size = packet_size;
1734 } else {
1735 printk(KERN_ERR "%s: No support for requested filename "
1736 "encryption method in this release\n", __func__);
Tyler Hicksdf6ad332009-08-21 04:27:46 -05001737 rc = -EOPNOTSUPP;
Michael Halcrow51ca58d2009-01-06 14:41:59 -08001738 goto out;
1739 }
1740out:
1741 return rc;
1742}
1743
1744static int ecryptfs_copy_filename(char **copied_name, size_t *copied_name_size,
1745 const char *name, size_t name_size)
1746{
1747 int rc = 0;
1748
Tyler Hicksfd9fc842009-02-06 18:06:51 -06001749 (*copied_name) = kmalloc((name_size + 1), GFP_KERNEL);
Michael Halcrow51ca58d2009-01-06 14:41:59 -08001750 if (!(*copied_name)) {
1751 rc = -ENOMEM;
1752 goto out;
1753 }
1754 memcpy((void *)(*copied_name), (void *)name, name_size);
1755 (*copied_name)[(name_size)] = '\0'; /* Only for convenience
1756 * in printing out the
1757 * string in debug
1758 * messages */
Tyler Hicksfd9fc842009-02-06 18:06:51 -06001759 (*copied_name_size) = name_size;
Michael Halcrow51ca58d2009-01-06 14:41:59 -08001760out:
1761 return rc;
1762}
1763
1764/**
Michael Halcrowf4aad162007-10-16 01:27:53 -07001765 * ecryptfs_process_key_cipher - Perform key cipher initialization.
Michael Halcrow237fead2006-10-04 02:16:22 -07001766 * @key_tfm: Crypto context for key material, set by this function
Michael Halcrowe5d9cbd2006-10-30 22:07:16 -08001767 * @cipher_name: Name of the cipher
1768 * @key_size: Size of the key in bytes
Michael Halcrow237fead2006-10-04 02:16:22 -07001769 *
1770 * Returns zero on success. Any crypto_tfm structs allocated here
1771 * should be released by other functions, such as on a superblock put
1772 * event, regardless of whether this function succeeds for fails.
1773 */
Michael Halcrowcd9d67d2007-10-16 01:28:04 -07001774static int
Michael Halcrowf4aad162007-10-16 01:27:53 -07001775ecryptfs_process_key_cipher(struct crypto_blkcipher **key_tfm,
1776 char *cipher_name, size_t *key_size)
Michael Halcrow237fead2006-10-04 02:16:22 -07001777{
1778 char dummy_key[ECRYPTFS_MAX_KEY_BYTES];
Dan Carpenterece550f2010-01-19 12:34:32 +03001779 char *full_alg_name = NULL;
Michael Halcrow237fead2006-10-04 02:16:22 -07001780 int rc;
1781
Michael Halcrowe5d9cbd2006-10-30 22:07:16 -08001782 *key_tfm = NULL;
1783 if (*key_size > ECRYPTFS_MAX_KEY_BYTES) {
Michael Halcrow237fead2006-10-04 02:16:22 -07001784 rc = -EINVAL;
Michael Halcrowdf261c52009-01-06 14:42:02 -08001785 printk(KERN_ERR "Requested key size is [%zd] bytes; maximum "
Michael Halcrowe5d9cbd2006-10-30 22:07:16 -08001786 "allowable is [%d]\n", *key_size, ECRYPTFS_MAX_KEY_BYTES);
Michael Halcrow237fead2006-10-04 02:16:22 -07001787 goto out;
1788 }
Michael Halcrow8bba0662006-10-30 22:07:18 -08001789 rc = ecryptfs_crypto_api_algify_cipher_name(&full_alg_name, cipher_name,
1790 "ecb");
1791 if (rc)
1792 goto out;
1793 *key_tfm = crypto_alloc_blkcipher(full_alg_name, 0, CRYPTO_ALG_ASYNC);
Michael Halcrow8bba0662006-10-30 22:07:18 -08001794 if (IS_ERR(*key_tfm)) {
1795 rc = PTR_ERR(*key_tfm);
Michael Halcrow237fead2006-10-04 02:16:22 -07001796 printk(KERN_ERR "Unable to allocate crypto cipher with name "
Dave Hansen38268492009-08-27 09:47:07 -07001797 "[%s]; rc = [%d]\n", full_alg_name, rc);
Michael Halcrow237fead2006-10-04 02:16:22 -07001798 goto out;
1799 }
Michael Halcrow8bba0662006-10-30 22:07:18 -08001800 crypto_blkcipher_set_flags(*key_tfm, CRYPTO_TFM_REQ_WEAK_KEY);
1801 if (*key_size == 0) {
1802 struct blkcipher_alg *alg = crypto_blkcipher_alg(*key_tfm);
1803
1804 *key_size = alg->max_keysize;
1805 }
Michael Halcrowe5d9cbd2006-10-30 22:07:16 -08001806 get_random_bytes(dummy_key, *key_size);
Michael Halcrow8bba0662006-10-30 22:07:18 -08001807 rc = crypto_blkcipher_setkey(*key_tfm, dummy_key, *key_size);
Michael Halcrow237fead2006-10-04 02:16:22 -07001808 if (rc) {
Michael Halcrowdf261c52009-01-06 14:42:02 -08001809 printk(KERN_ERR "Error attempting to set key of size [%zd] for "
Dave Hansen38268492009-08-27 09:47:07 -07001810 "cipher [%s]; rc = [%d]\n", *key_size, full_alg_name,
1811 rc);
Michael Halcrow237fead2006-10-04 02:16:22 -07001812 rc = -EINVAL;
1813 goto out;
1814 }
1815out:
Dan Carpenterece550f2010-01-19 12:34:32 +03001816 kfree(full_alg_name);
Michael Halcrow237fead2006-10-04 02:16:22 -07001817 return rc;
1818}
Michael Halcrowf4aad162007-10-16 01:27:53 -07001819
1820struct kmem_cache *ecryptfs_key_tfm_cache;
Adrian Bunk7896b632008-02-06 01:38:32 -08001821static struct list_head key_tfm_list;
Eric Sandeenaf440f52008-02-06 01:38:37 -08001822struct mutex key_tfm_list_mutex;
Michael Halcrowf4aad162007-10-16 01:27:53 -07001823
Jerome Marchand7371a382010-08-17 17:24:05 +02001824int __init ecryptfs_init_crypto(void)
Michael Halcrowf4aad162007-10-16 01:27:53 -07001825{
1826 mutex_init(&key_tfm_list_mutex);
1827 INIT_LIST_HEAD(&key_tfm_list);
1828 return 0;
1829}
1830
Eric Sandeenaf440f52008-02-06 01:38:37 -08001831/**
1832 * ecryptfs_destroy_crypto - free all cached key_tfms on key_tfm_list
1833 *
1834 * Called only at module unload time
1835 */
Michael Halcrowfcd12832007-10-16 01:28:01 -07001836int ecryptfs_destroy_crypto(void)
Michael Halcrowf4aad162007-10-16 01:27:53 -07001837{
1838 struct ecryptfs_key_tfm *key_tfm, *key_tfm_tmp;
1839
1840 mutex_lock(&key_tfm_list_mutex);
1841 list_for_each_entry_safe(key_tfm, key_tfm_tmp, &key_tfm_list,
1842 key_tfm_list) {
1843 list_del(&key_tfm->key_tfm_list);
1844 if (key_tfm->key_tfm)
1845 crypto_free_blkcipher(key_tfm->key_tfm);
1846 kmem_cache_free(ecryptfs_key_tfm_cache, key_tfm);
1847 }
1848 mutex_unlock(&key_tfm_list_mutex);
1849 return 0;
1850}
1851
1852int
1853ecryptfs_add_new_key_tfm(struct ecryptfs_key_tfm **key_tfm, char *cipher_name,
1854 size_t key_size)
1855{
1856 struct ecryptfs_key_tfm *tmp_tfm;
1857 int rc = 0;
1858
Eric Sandeenaf440f52008-02-06 01:38:37 -08001859 BUG_ON(!mutex_is_locked(&key_tfm_list_mutex));
1860
Michael Halcrowf4aad162007-10-16 01:27:53 -07001861 tmp_tfm = kmem_cache_alloc(ecryptfs_key_tfm_cache, GFP_KERNEL);
1862 if (key_tfm != NULL)
1863 (*key_tfm) = tmp_tfm;
1864 if (!tmp_tfm) {
1865 rc = -ENOMEM;
1866 printk(KERN_ERR "Error attempting to allocate from "
1867 "ecryptfs_key_tfm_cache\n");
1868 goto out;
1869 }
1870 mutex_init(&tmp_tfm->key_tfm_mutex);
1871 strncpy(tmp_tfm->cipher_name, cipher_name,
1872 ECRYPTFS_MAX_CIPHER_NAME_SIZE);
Eric Sandeenb8862902007-12-22 14:03:24 -08001873 tmp_tfm->cipher_name[ECRYPTFS_MAX_CIPHER_NAME_SIZE] = '\0';
Michael Halcrowf4aad162007-10-16 01:27:53 -07001874 tmp_tfm->key_size = key_size;
Michael Halcrow5dda6992007-10-16 01:28:06 -07001875 rc = ecryptfs_process_key_cipher(&tmp_tfm->key_tfm,
1876 tmp_tfm->cipher_name,
1877 &tmp_tfm->key_size);
1878 if (rc) {
Michael Halcrowf4aad162007-10-16 01:27:53 -07001879 printk(KERN_ERR "Error attempting to initialize key TFM "
1880 "cipher with name = [%s]; rc = [%d]\n",
1881 tmp_tfm->cipher_name, rc);
1882 kmem_cache_free(ecryptfs_key_tfm_cache, tmp_tfm);
1883 if (key_tfm != NULL)
1884 (*key_tfm) = NULL;
1885 goto out;
1886 }
Michael Halcrowf4aad162007-10-16 01:27:53 -07001887 list_add(&tmp_tfm->key_tfm_list, &key_tfm_list);
Michael Halcrowf4aad162007-10-16 01:27:53 -07001888out:
1889 return rc;
1890}
1891
Eric Sandeenaf440f52008-02-06 01:38:37 -08001892/**
1893 * ecryptfs_tfm_exists - Search for existing tfm for cipher_name.
1894 * @cipher_name: the name of the cipher to search for
1895 * @key_tfm: set to corresponding tfm if found
1896 *
1897 * Searches for cached key_tfm matching @cipher_name
1898 * Must be called with &key_tfm_list_mutex held
1899 * Returns 1 if found, with @key_tfm set
1900 * Returns 0 if not found, with @key_tfm set to NULL
1901 */
1902int ecryptfs_tfm_exists(char *cipher_name, struct ecryptfs_key_tfm **key_tfm)
1903{
1904 struct ecryptfs_key_tfm *tmp_key_tfm;
1905
1906 BUG_ON(!mutex_is_locked(&key_tfm_list_mutex));
1907
1908 list_for_each_entry(tmp_key_tfm, &key_tfm_list, key_tfm_list) {
1909 if (strcmp(tmp_key_tfm->cipher_name, cipher_name) == 0) {
1910 if (key_tfm)
1911 (*key_tfm) = tmp_key_tfm;
1912 return 1;
1913 }
1914 }
1915 if (key_tfm)
1916 (*key_tfm) = NULL;
1917 return 0;
1918}
1919
1920/**
1921 * ecryptfs_get_tfm_and_mutex_for_cipher_name
1922 *
1923 * @tfm: set to cached tfm found, or new tfm created
1924 * @tfm_mutex: set to mutex for cached tfm found, or new tfm created
1925 * @cipher_name: the name of the cipher to search for and/or add
1926 *
1927 * Sets pointers to @tfm & @tfm_mutex matching @cipher_name.
1928 * Searches for cached item first, and creates new if not found.
1929 * Returns 0 on success, non-zero if adding new cipher failed
1930 */
Michael Halcrowf4aad162007-10-16 01:27:53 -07001931int ecryptfs_get_tfm_and_mutex_for_cipher_name(struct crypto_blkcipher **tfm,
1932 struct mutex **tfm_mutex,
1933 char *cipher_name)
1934{
1935 struct ecryptfs_key_tfm *key_tfm;
1936 int rc = 0;
1937
1938 (*tfm) = NULL;
1939 (*tfm_mutex) = NULL;
Eric Sandeenaf440f52008-02-06 01:38:37 -08001940
Michael Halcrowf4aad162007-10-16 01:27:53 -07001941 mutex_lock(&key_tfm_list_mutex);
Eric Sandeenaf440f52008-02-06 01:38:37 -08001942 if (!ecryptfs_tfm_exists(cipher_name, &key_tfm)) {
1943 rc = ecryptfs_add_new_key_tfm(&key_tfm, cipher_name, 0);
1944 if (rc) {
1945 printk(KERN_ERR "Error adding new key_tfm to list; "
1946 "rc = [%d]\n", rc);
Michael Halcrowf4aad162007-10-16 01:27:53 -07001947 goto out;
1948 }
1949 }
Michael Halcrowf4aad162007-10-16 01:27:53 -07001950 (*tfm) = key_tfm->key_tfm;
1951 (*tfm_mutex) = &key_tfm->key_tfm_mutex;
1952out:
Cyrill Gorcunov71fd5172008-05-23 13:04:20 -07001953 mutex_unlock(&key_tfm_list_mutex);
Michael Halcrowf4aad162007-10-16 01:27:53 -07001954 return rc;
1955}
Michael Halcrow51ca58d2009-01-06 14:41:59 -08001956
1957/* 64 characters forming a 6-bit target field */
1958static unsigned char *portable_filename_chars = ("-.0123456789ABCD"
1959 "EFGHIJKLMNOPQRST"
1960 "UVWXYZabcdefghij"
1961 "klmnopqrstuvwxyz");
1962
1963/* We could either offset on every reverse map or just pad some 0x00's
1964 * at the front here */
Tyler Hicks0f751e62011-11-23 11:31:24 -06001965static const unsigned char filename_rev_map[256] = {
Michael Halcrow51ca58d2009-01-06 14:41:59 -08001966 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, /* 7 */
1967 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, /* 15 */
1968 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, /* 23 */
1969 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, /* 31 */
1970 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, /* 39 */
1971 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x01, 0x00, /* 47 */
1972 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, /* 55 */
1973 0x0A, 0x0B, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, /* 63 */
1974 0x00, 0x0C, 0x0D, 0x0E, 0x0F, 0x10, 0x11, 0x12, /* 71 */
1975 0x13, 0x14, 0x15, 0x16, 0x17, 0x18, 0x19, 0x1A, /* 79 */
1976 0x1B, 0x1C, 0x1D, 0x1E, 0x1F, 0x20, 0x21, 0x22, /* 87 */
1977 0x23, 0x24, 0x25, 0x00, 0x00, 0x00, 0x00, 0x00, /* 95 */
1978 0x00, 0x26, 0x27, 0x28, 0x29, 0x2A, 0x2B, 0x2C, /* 103 */
1979 0x2D, 0x2E, 0x2F, 0x30, 0x31, 0x32, 0x33, 0x34, /* 111 */
1980 0x35, 0x36, 0x37, 0x38, 0x39, 0x3A, 0x3B, 0x3C, /* 119 */
Tyler Hicks0f751e62011-11-23 11:31:24 -06001981 0x3D, 0x3E, 0x3F /* 123 - 255 initialized to 0x00 */
Michael Halcrow51ca58d2009-01-06 14:41:59 -08001982};
1983
1984/**
1985 * ecryptfs_encode_for_filename
1986 * @dst: Destination location for encoded filename
1987 * @dst_size: Size of the encoded filename in bytes
1988 * @src: Source location for the filename to encode
1989 * @src_size: Size of the source in bytes
1990 */
Cong Ding37028752012-12-07 22:21:56 +00001991static void ecryptfs_encode_for_filename(unsigned char *dst, size_t *dst_size,
Michael Halcrow51ca58d2009-01-06 14:41:59 -08001992 unsigned char *src, size_t src_size)
1993{
1994 size_t num_blocks;
1995 size_t block_num = 0;
1996 size_t dst_offset = 0;
1997 unsigned char last_block[3];
1998
1999 if (src_size == 0) {
2000 (*dst_size) = 0;
2001 goto out;
2002 }
2003 num_blocks = (src_size / 3);
2004 if ((src_size % 3) == 0) {
2005 memcpy(last_block, (&src[src_size - 3]), 3);
2006 } else {
2007 num_blocks++;
2008 last_block[2] = 0x00;
2009 switch (src_size % 3) {
2010 case 1:
2011 last_block[0] = src[src_size - 1];
2012 last_block[1] = 0x00;
2013 break;
2014 case 2:
2015 last_block[0] = src[src_size - 2];
2016 last_block[1] = src[src_size - 1];
2017 }
2018 }
2019 (*dst_size) = (num_blocks * 4);
2020 if (!dst)
2021 goto out;
2022 while (block_num < num_blocks) {
2023 unsigned char *src_block;
2024 unsigned char dst_block[4];
2025
2026 if (block_num == (num_blocks - 1))
2027 src_block = last_block;
2028 else
2029 src_block = &src[block_num * 3];
2030 dst_block[0] = ((src_block[0] >> 2) & 0x3F);
2031 dst_block[1] = (((src_block[0] << 4) & 0x30)
2032 | ((src_block[1] >> 4) & 0x0F));
2033 dst_block[2] = (((src_block[1] << 2) & 0x3C)
2034 | ((src_block[2] >> 6) & 0x03));
2035 dst_block[3] = (src_block[2] & 0x3F);
2036 dst[dst_offset++] = portable_filename_chars[dst_block[0]];
2037 dst[dst_offset++] = portable_filename_chars[dst_block[1]];
2038 dst[dst_offset++] = portable_filename_chars[dst_block[2]];
2039 dst[dst_offset++] = portable_filename_chars[dst_block[3]];
2040 block_num++;
2041 }
2042out:
2043 return;
2044}
2045
Tyler Hicks4a266202011-11-05 13:45:08 -04002046static size_t ecryptfs_max_decoded_size(size_t encoded_size)
2047{
2048 /* Not exact; conservatively long. Every block of 4
2049 * encoded characters decodes into a block of 3
2050 * decoded characters. This segment of code provides
2051 * the caller with the maximum amount of allocated
2052 * space that @dst will need to point to in a
2053 * subsequent call. */
2054 return ((encoded_size + 1) * 3) / 4;
2055}
2056
Michael Halcrow71c11c32009-01-06 14:42:05 -08002057/**
2058 * ecryptfs_decode_from_filename
2059 * @dst: If NULL, this function only sets @dst_size and returns. If
2060 * non-NULL, this function decodes the encoded octets in @src
2061 * into the memory that @dst points to.
2062 * @dst_size: Set to the size of the decoded string.
2063 * @src: The encoded set of octets to decode.
2064 * @src_size: The size of the encoded set of octets to decode.
2065 */
2066static void
2067ecryptfs_decode_from_filename(unsigned char *dst, size_t *dst_size,
2068 const unsigned char *src, size_t src_size)
Michael Halcrow51ca58d2009-01-06 14:41:59 -08002069{
2070 u8 current_bit_offset = 0;
2071 size_t src_byte_offset = 0;
2072 size_t dst_byte_offset = 0;
Michael Halcrow51ca58d2009-01-06 14:41:59 -08002073
2074 if (dst == NULL) {
Tyler Hicks4a266202011-11-05 13:45:08 -04002075 (*dst_size) = ecryptfs_max_decoded_size(src_size);
Michael Halcrow51ca58d2009-01-06 14:41:59 -08002076 goto out;
2077 }
2078 while (src_byte_offset < src_size) {
2079 unsigned char src_byte =
2080 filename_rev_map[(int)src[src_byte_offset]];
2081
2082 switch (current_bit_offset) {
2083 case 0:
2084 dst[dst_byte_offset] = (src_byte << 2);
2085 current_bit_offset = 6;
2086 break;
2087 case 6:
2088 dst[dst_byte_offset++] |= (src_byte >> 4);
2089 dst[dst_byte_offset] = ((src_byte & 0xF)
2090 << 4);
2091 current_bit_offset = 4;
2092 break;
2093 case 4:
2094 dst[dst_byte_offset++] |= (src_byte >> 2);
2095 dst[dst_byte_offset] = (src_byte << 6);
2096 current_bit_offset = 2;
2097 break;
2098 case 2:
2099 dst[dst_byte_offset++] |= (src_byte);
2100 dst[dst_byte_offset] = 0;
2101 current_bit_offset = 0;
2102 break;
2103 }
2104 src_byte_offset++;
2105 }
2106 (*dst_size) = dst_byte_offset;
2107out:
Michael Halcrow71c11c32009-01-06 14:42:05 -08002108 return;
Michael Halcrow51ca58d2009-01-06 14:41:59 -08002109}
2110
2111/**
2112 * ecryptfs_encrypt_and_encode_filename - converts a plaintext file name to cipher text
2113 * @crypt_stat: The crypt_stat struct associated with the file anem to encode
2114 * @name: The plaintext name
2115 * @length: The length of the plaintext
2116 * @encoded_name: The encypted name
2117 *
2118 * Encrypts and encodes a filename into something that constitutes a
2119 * valid filename for a filesystem, with printable characters.
2120 *
2121 * We assume that we have a properly initialized crypto context,
2122 * pointed to by crypt_stat->tfm.
2123 *
2124 * Returns zero on success; non-zero on otherwise
2125 */
2126int ecryptfs_encrypt_and_encode_filename(
2127 char **encoded_name,
2128 size_t *encoded_name_size,
2129 struct ecryptfs_crypt_stat *crypt_stat,
2130 struct ecryptfs_mount_crypt_stat *mount_crypt_stat,
2131 const char *name, size_t name_size)
2132{
2133 size_t encoded_name_no_prefix_size;
2134 int rc = 0;
2135
2136 (*encoded_name) = NULL;
2137 (*encoded_name_size) = 0;
2138 if ((crypt_stat && (crypt_stat->flags & ECRYPTFS_ENCRYPT_FILENAMES))
2139 || (mount_crypt_stat && (mount_crypt_stat->flags
2140 & ECRYPTFS_GLOBAL_ENCRYPT_FILENAMES))) {
2141 struct ecryptfs_filename *filename;
2142
2143 filename = kzalloc(sizeof(*filename), GFP_KERNEL);
2144 if (!filename) {
2145 printk(KERN_ERR "%s: Out of memory whilst attempting "
Michael Halcrowa8f12862009-01-06 14:42:03 -08002146 "to kzalloc [%zd] bytes\n", __func__,
Michael Halcrow51ca58d2009-01-06 14:41:59 -08002147 sizeof(*filename));
2148 rc = -ENOMEM;
2149 goto out;
2150 }
2151 filename->filename = (char *)name;
2152 filename->filename_size = name_size;
2153 rc = ecryptfs_encrypt_filename(filename, crypt_stat,
2154 mount_crypt_stat);
2155 if (rc) {
2156 printk(KERN_ERR "%s: Error attempting to encrypt "
2157 "filename; rc = [%d]\n", __func__, rc);
2158 kfree(filename);
2159 goto out;
2160 }
2161 ecryptfs_encode_for_filename(
2162 NULL, &encoded_name_no_prefix_size,
2163 filename->encrypted_filename,
2164 filename->encrypted_filename_size);
2165 if ((crypt_stat && (crypt_stat->flags
2166 & ECRYPTFS_ENCFN_USE_MOUNT_FNEK))
2167 || (mount_crypt_stat
2168 && (mount_crypt_stat->flags
2169 & ECRYPTFS_GLOBAL_ENCFN_USE_MOUNT_FNEK)))
2170 (*encoded_name_size) =
2171 (ECRYPTFS_FNEK_ENCRYPTED_FILENAME_PREFIX_SIZE
2172 + encoded_name_no_prefix_size);
2173 else
2174 (*encoded_name_size) =
2175 (ECRYPTFS_FEK_ENCRYPTED_FILENAME_PREFIX_SIZE
2176 + encoded_name_no_prefix_size);
2177 (*encoded_name) = kmalloc((*encoded_name_size) + 1, GFP_KERNEL);
2178 if (!(*encoded_name)) {
2179 printk(KERN_ERR "%s: Out of memory whilst attempting "
Michael Halcrowa8f12862009-01-06 14:42:03 -08002180 "to kzalloc [%zd] bytes\n", __func__,
Michael Halcrow51ca58d2009-01-06 14:41:59 -08002181 (*encoded_name_size));
2182 rc = -ENOMEM;
2183 kfree(filename->encrypted_filename);
2184 kfree(filename);
2185 goto out;
2186 }
2187 if ((crypt_stat && (crypt_stat->flags
2188 & ECRYPTFS_ENCFN_USE_MOUNT_FNEK))
2189 || (mount_crypt_stat
2190 && (mount_crypt_stat->flags
2191 & ECRYPTFS_GLOBAL_ENCFN_USE_MOUNT_FNEK))) {
2192 memcpy((*encoded_name),
2193 ECRYPTFS_FNEK_ENCRYPTED_FILENAME_PREFIX,
2194 ECRYPTFS_FNEK_ENCRYPTED_FILENAME_PREFIX_SIZE);
2195 ecryptfs_encode_for_filename(
2196 ((*encoded_name)
2197 + ECRYPTFS_FNEK_ENCRYPTED_FILENAME_PREFIX_SIZE),
2198 &encoded_name_no_prefix_size,
2199 filename->encrypted_filename,
2200 filename->encrypted_filename_size);
2201 (*encoded_name_size) =
2202 (ECRYPTFS_FNEK_ENCRYPTED_FILENAME_PREFIX_SIZE
2203 + encoded_name_no_prefix_size);
2204 (*encoded_name)[(*encoded_name_size)] = '\0';
Michael Halcrow51ca58d2009-01-06 14:41:59 -08002205 } else {
Tyler Hicksdf6ad332009-08-21 04:27:46 -05002206 rc = -EOPNOTSUPP;
Michael Halcrow51ca58d2009-01-06 14:41:59 -08002207 }
2208 if (rc) {
2209 printk(KERN_ERR "%s: Error attempting to encode "
2210 "encrypted filename; rc = [%d]\n", __func__,
2211 rc);
2212 kfree((*encoded_name));
2213 (*encoded_name) = NULL;
2214 (*encoded_name_size) = 0;
2215 }
2216 kfree(filename->encrypted_filename);
2217 kfree(filename);
2218 } else {
2219 rc = ecryptfs_copy_filename(encoded_name,
2220 encoded_name_size,
2221 name, name_size);
2222 }
2223out:
2224 return rc;
2225}
2226
2227/**
2228 * ecryptfs_decode_and_decrypt_filename - converts the encoded cipher text name to decoded plaintext
2229 * @plaintext_name: The plaintext name
2230 * @plaintext_name_size: The plaintext name size
2231 * @ecryptfs_dir_dentry: eCryptfs directory dentry
2232 * @name: The filename in cipher text
2233 * @name_size: The cipher text name size
2234 *
2235 * Decrypts and decodes the filename.
2236 *
2237 * Returns zero on error; non-zero otherwise
2238 */
2239int ecryptfs_decode_and_decrypt_filename(char **plaintext_name,
2240 size_t *plaintext_name_size,
2241 struct dentry *ecryptfs_dir_dentry,
2242 const char *name, size_t name_size)
2243{
Tyler Hicks2aac0cf2009-03-20 02:23:57 -05002244 struct ecryptfs_mount_crypt_stat *mount_crypt_stat =
2245 &ecryptfs_superblock_to_private(
2246 ecryptfs_dir_dentry->d_sb)->mount_crypt_stat;
Michael Halcrow51ca58d2009-01-06 14:41:59 -08002247 char *decoded_name;
2248 size_t decoded_name_size;
2249 size_t packet_size;
2250 int rc = 0;
2251
Tyler Hicks2aac0cf2009-03-20 02:23:57 -05002252 if ((mount_crypt_stat->flags & ECRYPTFS_GLOBAL_ENCRYPT_FILENAMES)
2253 && !(mount_crypt_stat->flags & ECRYPTFS_ENCRYPTED_VIEW_ENABLED)
2254 && (name_size > ECRYPTFS_FNEK_ENCRYPTED_FILENAME_PREFIX_SIZE)
Michael Halcrow51ca58d2009-01-06 14:41:59 -08002255 && (strncmp(name, ECRYPTFS_FNEK_ENCRYPTED_FILENAME_PREFIX,
2256 ECRYPTFS_FNEK_ENCRYPTED_FILENAME_PREFIX_SIZE) == 0)) {
Michael Halcrow51ca58d2009-01-06 14:41:59 -08002257 const char *orig_name = name;
2258 size_t orig_name_size = name_size;
2259
2260 name += ECRYPTFS_FNEK_ENCRYPTED_FILENAME_PREFIX_SIZE;
2261 name_size -= ECRYPTFS_FNEK_ENCRYPTED_FILENAME_PREFIX_SIZE;
Michael Halcrow71c11c32009-01-06 14:42:05 -08002262 ecryptfs_decode_from_filename(NULL, &decoded_name_size,
2263 name, name_size);
Michael Halcrow51ca58d2009-01-06 14:41:59 -08002264 decoded_name = kmalloc(decoded_name_size, GFP_KERNEL);
2265 if (!decoded_name) {
2266 printk(KERN_ERR "%s: Out of memory whilst attempting "
Michael Halcrowdf261c52009-01-06 14:42:02 -08002267 "to kmalloc [%zd] bytes\n", __func__,
Michael Halcrow51ca58d2009-01-06 14:41:59 -08002268 decoded_name_size);
2269 rc = -ENOMEM;
2270 goto out;
2271 }
Michael Halcrow71c11c32009-01-06 14:42:05 -08002272 ecryptfs_decode_from_filename(decoded_name, &decoded_name_size,
2273 name, name_size);
Michael Halcrow51ca58d2009-01-06 14:41:59 -08002274 rc = ecryptfs_parse_tag_70_packet(plaintext_name,
2275 plaintext_name_size,
2276 &packet_size,
2277 mount_crypt_stat,
2278 decoded_name,
2279 decoded_name_size);
2280 if (rc) {
2281 printk(KERN_INFO "%s: Could not parse tag 70 packet "
2282 "from filename; copying through filename "
2283 "as-is\n", __func__);
2284 rc = ecryptfs_copy_filename(plaintext_name,
2285 plaintext_name_size,
2286 orig_name, orig_name_size);
2287 goto out_free;
2288 }
2289 } else {
2290 rc = ecryptfs_copy_filename(plaintext_name,
2291 plaintext_name_size,
2292 name, name_size);
2293 goto out;
2294 }
2295out_free:
2296 kfree(decoded_name);
2297out:
2298 return rc;
2299}
Tyler Hicks4a266202011-11-05 13:45:08 -04002300
2301#define ENC_NAME_MAX_BLOCKLEN_8_OR_16 143
2302
2303int ecryptfs_set_f_namelen(long *namelen, long lower_namelen,
2304 struct ecryptfs_mount_crypt_stat *mount_crypt_stat)
2305{
2306 struct blkcipher_desc desc;
2307 struct mutex *tfm_mutex;
2308 size_t cipher_blocksize;
2309 int rc;
2310
2311 if (!(mount_crypt_stat->flags & ECRYPTFS_GLOBAL_ENCRYPT_FILENAMES)) {
2312 (*namelen) = lower_namelen;
2313 return 0;
2314 }
2315
2316 rc = ecryptfs_get_tfm_and_mutex_for_cipher_name(&desc.tfm, &tfm_mutex,
2317 mount_crypt_stat->global_default_fn_cipher_name);
2318 if (unlikely(rc)) {
2319 (*namelen) = 0;
2320 return rc;
2321 }
2322
2323 mutex_lock(tfm_mutex);
2324 cipher_blocksize = crypto_blkcipher_blocksize(desc.tfm);
2325 mutex_unlock(tfm_mutex);
2326
2327 /* Return an exact amount for the common cases */
2328 if (lower_namelen == NAME_MAX
2329 && (cipher_blocksize == 8 || cipher_blocksize == 16)) {
2330 (*namelen) = ENC_NAME_MAX_BLOCKLEN_8_OR_16;
2331 return 0;
2332 }
2333
2334 /* Return a safe estimate for the uncommon cases */
2335 (*namelen) = lower_namelen;
2336 (*namelen) -= ECRYPTFS_FNEK_ENCRYPTED_FILENAME_PREFIX_SIZE;
2337 /* Since this is the max decoded size, subtract 1 "decoded block" len */
2338 (*namelen) = ecryptfs_max_decoded_size(*namelen) - 3;
2339 (*namelen) -= ECRYPTFS_TAG_70_MAX_METADATA_SIZE;
2340 (*namelen) -= ECRYPTFS_FILENAME_MIN_RANDOM_PREPEND_BYTES;
2341 /* Worst case is that the filename is padded nearly a full block size */
2342 (*namelen) -= cipher_blocksize - 1;
2343
2344 if ((*namelen) < 0)
2345 (*namelen) = 0;
2346
2347 return 0;
2348}