blob: 44c2ec2e9e6af524c248f5ee96290b84ac3e4d17 [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>
36#include "ecryptfs_kernel.h"
37
38static int
39ecryptfs_decrypt_page_offset(struct ecryptfs_crypt_stat *crypt_stat,
40 struct page *dst_page, int dst_offset,
41 struct page *src_page, int src_offset, int size,
42 unsigned char *iv);
43static int
44ecryptfs_encrypt_page_offset(struct ecryptfs_crypt_stat *crypt_stat,
45 struct page *dst_page, int dst_offset,
46 struct page *src_page, int src_offset, int size,
47 unsigned char *iv);
48
49/**
50 * ecryptfs_to_hex
51 * @dst: Buffer to take hex character representation of contents of
52 * src; must be at least of size (src_size * 2)
53 * @src: Buffer to be converted to a hex string respresentation
54 * @src_size: number of bytes to convert
55 */
56void ecryptfs_to_hex(char *dst, char *src, size_t src_size)
57{
58 int x;
59
60 for (x = 0; x < src_size; x++)
61 sprintf(&dst[x * 2], "%.2x", (unsigned char)src[x]);
62}
63
64/**
65 * ecryptfs_from_hex
66 * @dst: Buffer to take the bytes from src hex; must be at least of
67 * size (src_size / 2)
68 * @src: Buffer to be converted from a hex string respresentation to raw value
69 * @dst_size: size of dst buffer, or number of hex characters pairs to convert
70 */
71void ecryptfs_from_hex(char *dst, char *src, int dst_size)
72{
73 int x;
74 char tmp[3] = { 0, };
75
76 for (x = 0; x < dst_size; x++) {
77 tmp[0] = src[x * 2];
78 tmp[1] = src[x * 2 + 1];
79 dst[x] = (unsigned char)simple_strtol(tmp, NULL, 16);
80 }
81}
82
83/**
84 * ecryptfs_calculate_md5 - calculates the md5 of @src
85 * @dst: Pointer to 16 bytes of allocated memory
86 * @crypt_stat: Pointer to crypt_stat struct for the current inode
87 * @src: Data to be md5'd
88 * @len: Length of @src
89 *
90 * Uses the allocated crypto context that crypt_stat references to
91 * generate the MD5 sum of the contents of src.
92 */
93static int ecryptfs_calculate_md5(char *dst,
94 struct ecryptfs_crypt_stat *crypt_stat,
95 char *src, int len)
96{
Michael Halcrow237fead2006-10-04 02:16:22 -070097 struct scatterlist sg;
Michael Halcrow565d9722006-10-30 22:07:17 -080098 struct hash_desc desc = {
99 .tfm = crypt_stat->hash_tfm,
100 .flags = CRYPTO_TFM_REQ_MAY_SLEEP
101 };
102 int rc = 0;
Michael Halcrow237fead2006-10-04 02:16:22 -0700103
Michael Halcrow565d9722006-10-30 22:07:17 -0800104 mutex_lock(&crypt_stat->cs_hash_tfm_mutex);
Michael Halcrow237fead2006-10-04 02:16:22 -0700105 sg_init_one(&sg, (u8 *)src, len);
Michael Halcrow565d9722006-10-30 22:07:17 -0800106 if (!desc.tfm) {
107 desc.tfm = crypto_alloc_hash(ECRYPTFS_DEFAULT_HASH, 0,
108 CRYPTO_ALG_ASYNC);
109 if (IS_ERR(desc.tfm)) {
110 rc = PTR_ERR(desc.tfm);
Michael Halcrow237fead2006-10-04 02:16:22 -0700111 ecryptfs_printk(KERN_ERR, "Error attempting to "
Michael Halcrow565d9722006-10-30 22:07:17 -0800112 "allocate crypto context; rc = [%d]\n",
113 rc);
Michael Halcrow237fead2006-10-04 02:16:22 -0700114 goto out;
115 }
Michael Halcrow565d9722006-10-30 22:07:17 -0800116 crypt_stat->hash_tfm = desc.tfm;
Michael Halcrow237fead2006-10-04 02:16:22 -0700117 }
Michael Halcrow565d9722006-10-30 22:07:17 -0800118 crypto_hash_init(&desc);
119 crypto_hash_update(&desc, &sg, len);
120 crypto_hash_final(&desc, dst);
121 mutex_unlock(&crypt_stat->cs_hash_tfm_mutex);
Michael Halcrow237fead2006-10-04 02:16:22 -0700122out:
123 return rc;
124}
125
Michael Halcrow8bba0662006-10-30 22:07:18 -0800126int ecryptfs_crypto_api_algify_cipher_name(char **algified_name,
127 char *cipher_name,
128 char *chaining_modifier)
129{
130 int cipher_name_len = strlen(cipher_name);
131 int chaining_modifier_len = strlen(chaining_modifier);
132 int algified_name_len;
133 int rc;
134
135 algified_name_len = (chaining_modifier_len + cipher_name_len + 3);
136 (*algified_name) = kmalloc(algified_name_len, GFP_KERNEL);
Michael Halcrow7bd473f2006-11-02 22:06:56 -0800137 if (!(*algified_name)) {
Michael Halcrow8bba0662006-10-30 22:07:18 -0800138 rc = -ENOMEM;
139 goto out;
140 }
141 snprintf((*algified_name), algified_name_len, "%s(%s)",
142 chaining_modifier, cipher_name);
143 rc = 0;
144out:
145 return rc;
146}
147
Michael Halcrow237fead2006-10-04 02:16:22 -0700148/**
149 * ecryptfs_derive_iv
150 * @iv: destination for the derived iv vale
151 * @crypt_stat: Pointer to crypt_stat struct for the current inode
152 * @offset: Offset of the page whose's iv we are to derive
153 *
154 * Generate the initialization vector from the given root IV and page
155 * offset.
156 *
157 * Returns zero on success; non-zero on error.
158 */
159static int ecryptfs_derive_iv(char *iv, struct ecryptfs_crypt_stat *crypt_stat,
160 pgoff_t offset)
161{
162 int rc = 0;
163 char dst[MD5_DIGEST_SIZE];
164 char src[ECRYPTFS_MAX_IV_BYTES + 16];
165
166 if (unlikely(ecryptfs_verbosity > 0)) {
167 ecryptfs_printk(KERN_DEBUG, "root iv:\n");
168 ecryptfs_dump_hex(crypt_stat->root_iv, crypt_stat->iv_bytes);
169 }
170 /* TODO: It is probably secure to just cast the least
171 * significant bits of the root IV into an unsigned long and
172 * add the offset to that rather than go through all this
173 * hashing business. -Halcrow */
174 memcpy(src, crypt_stat->root_iv, crypt_stat->iv_bytes);
175 memset((src + crypt_stat->iv_bytes), 0, 16);
176 snprintf((src + crypt_stat->iv_bytes), 16, "%ld", offset);
177 if (unlikely(ecryptfs_verbosity > 0)) {
178 ecryptfs_printk(KERN_DEBUG, "source:\n");
179 ecryptfs_dump_hex(src, (crypt_stat->iv_bytes + 16));
180 }
181 rc = ecryptfs_calculate_md5(dst, crypt_stat, src,
182 (crypt_stat->iv_bytes + 16));
183 if (rc) {
184 ecryptfs_printk(KERN_WARNING, "Error attempting to compute "
185 "MD5 while generating IV for a page\n");
186 goto out;
187 }
188 memcpy(iv, dst, crypt_stat->iv_bytes);
189 if (unlikely(ecryptfs_verbosity > 0)) {
190 ecryptfs_printk(KERN_DEBUG, "derived iv:\n");
191 ecryptfs_dump_hex(iv, crypt_stat->iv_bytes);
192 }
193out:
194 return rc;
195}
196
197/**
198 * ecryptfs_init_crypt_stat
199 * @crypt_stat: Pointer to the crypt_stat struct to initialize.
200 *
201 * Initialize the crypt_stat structure.
202 */
203void
204ecryptfs_init_crypt_stat(struct ecryptfs_crypt_stat *crypt_stat)
205{
206 memset((void *)crypt_stat, 0, sizeof(struct ecryptfs_crypt_stat));
207 mutex_init(&crypt_stat->cs_mutex);
208 mutex_init(&crypt_stat->cs_tfm_mutex);
Michael Halcrow565d9722006-10-30 22:07:17 -0800209 mutex_init(&crypt_stat->cs_hash_tfm_mutex);
Michael Halcrow237fead2006-10-04 02:16:22 -0700210 ECRYPTFS_SET_FLAG(crypt_stat->flags, ECRYPTFS_STRUCT_INITIALIZED);
211}
212
213/**
214 * ecryptfs_destruct_crypt_stat
215 * @crypt_stat: Pointer to the crypt_stat struct to initialize.
216 *
217 * Releases all memory associated with a crypt_stat struct.
218 */
219void ecryptfs_destruct_crypt_stat(struct ecryptfs_crypt_stat *crypt_stat)
220{
221 if (crypt_stat->tfm)
Michael Halcrow8bba0662006-10-30 22:07:18 -0800222 crypto_free_blkcipher(crypt_stat->tfm);
Michael Halcrow565d9722006-10-30 22:07:17 -0800223 if (crypt_stat->hash_tfm)
224 crypto_free_hash(crypt_stat->hash_tfm);
Michael Halcrow237fead2006-10-04 02:16:22 -0700225 memset(crypt_stat, 0, sizeof(struct ecryptfs_crypt_stat));
226}
227
228void ecryptfs_destruct_mount_crypt_stat(
229 struct ecryptfs_mount_crypt_stat *mount_crypt_stat)
230{
231 if (mount_crypt_stat->global_auth_tok_key)
232 key_put(mount_crypt_stat->global_auth_tok_key);
233 if (mount_crypt_stat->global_key_tfm)
Michael Halcrow8bba0662006-10-30 22:07:18 -0800234 crypto_free_blkcipher(mount_crypt_stat->global_key_tfm);
Michael Halcrow237fead2006-10-04 02:16:22 -0700235 memset(mount_crypt_stat, 0, sizeof(struct ecryptfs_mount_crypt_stat));
236}
237
238/**
239 * virt_to_scatterlist
240 * @addr: Virtual address
241 * @size: Size of data; should be an even multiple of the block size
242 * @sg: Pointer to scatterlist array; set to NULL to obtain only
243 * the number of scatterlist structs required in array
244 * @sg_size: Max array size
245 *
246 * Fills in a scatterlist array with page references for a passed
247 * virtual address.
248 *
249 * Returns the number of scatterlist structs in array used
250 */
251int virt_to_scatterlist(const void *addr, int size, struct scatterlist *sg,
252 int sg_size)
253{
254 int i = 0;
255 struct page *pg;
256 int offset;
257 int remainder_of_page;
258
259 while (size > 0 && i < sg_size) {
260 pg = virt_to_page(addr);
261 offset = offset_in_page(addr);
262 if (sg) {
263 sg[i].page = pg;
264 sg[i].offset = offset;
265 }
266 remainder_of_page = PAGE_CACHE_SIZE - offset;
267 if (size >= remainder_of_page) {
268 if (sg)
269 sg[i].length = remainder_of_page;
270 addr += remainder_of_page;
271 size -= remainder_of_page;
272 } else {
273 if (sg)
274 sg[i].length = size;
275 addr += size;
276 size = 0;
277 }
278 i++;
279 }
280 if (size > 0)
281 return -ENOMEM;
282 return i;
283}
284
285/**
286 * encrypt_scatterlist
287 * @crypt_stat: Pointer to the crypt_stat struct to initialize.
288 * @dest_sg: Destination of encrypted data
289 * @src_sg: Data to be encrypted
290 * @size: Length of data to be encrypted
291 * @iv: iv to use during encryption
292 *
293 * Returns the number of bytes encrypted; negative value on error
294 */
295static int encrypt_scatterlist(struct ecryptfs_crypt_stat *crypt_stat,
296 struct scatterlist *dest_sg,
297 struct scatterlist *src_sg, int size,
298 unsigned char *iv)
299{
Michael Halcrow8bba0662006-10-30 22:07:18 -0800300 struct blkcipher_desc desc = {
301 .tfm = crypt_stat->tfm,
302 .info = iv,
303 .flags = CRYPTO_TFM_REQ_MAY_SLEEP
304 };
Michael Halcrow237fead2006-10-04 02:16:22 -0700305 int rc = 0;
306
307 BUG_ON(!crypt_stat || !crypt_stat->tfm
308 || !ECRYPTFS_CHECK_FLAG(crypt_stat->flags,
309 ECRYPTFS_STRUCT_INITIALIZED));
310 if (unlikely(ecryptfs_verbosity > 0)) {
311 ecryptfs_printk(KERN_DEBUG, "Key size [%d]; key:\n",
312 crypt_stat->key_size);
313 ecryptfs_dump_hex(crypt_stat->key,
314 crypt_stat->key_size);
315 }
316 /* Consider doing this once, when the file is opened */
317 mutex_lock(&crypt_stat->cs_tfm_mutex);
Michael Halcrow8bba0662006-10-30 22:07:18 -0800318 rc = crypto_blkcipher_setkey(crypt_stat->tfm, crypt_stat->key,
319 crypt_stat->key_size);
Michael Halcrow237fead2006-10-04 02:16:22 -0700320 if (rc) {
321 ecryptfs_printk(KERN_ERR, "Error setting key; rc = [%d]\n",
322 rc);
323 mutex_unlock(&crypt_stat->cs_tfm_mutex);
324 rc = -EINVAL;
325 goto out;
326 }
327 ecryptfs_printk(KERN_DEBUG, "Encrypting [%d] bytes.\n", size);
Michael Halcrow8bba0662006-10-30 22:07:18 -0800328 crypto_blkcipher_encrypt_iv(&desc, dest_sg, src_sg, size);
Michael Halcrow237fead2006-10-04 02:16:22 -0700329 mutex_unlock(&crypt_stat->cs_tfm_mutex);
330out:
331 return rc;
332}
333
334static void
335ecryptfs_extent_to_lwr_pg_idx_and_offset(unsigned long *lower_page_idx,
336 int *byte_offset,
337 struct ecryptfs_crypt_stat *crypt_stat,
338 unsigned long extent_num)
339{
340 unsigned long lower_extent_num;
341 int extents_occupied_by_headers_at_front;
342 int bytes_occupied_by_headers_at_front;
343 int extent_offset;
344 int extents_per_page;
345
346 bytes_occupied_by_headers_at_front =
347 ( crypt_stat->header_extent_size
348 * crypt_stat->num_header_extents_at_front );
349 extents_occupied_by_headers_at_front =
350 ( bytes_occupied_by_headers_at_front
351 / crypt_stat->extent_size );
352 lower_extent_num = extents_occupied_by_headers_at_front + extent_num;
353 extents_per_page = PAGE_CACHE_SIZE / crypt_stat->extent_size;
354 (*lower_page_idx) = lower_extent_num / extents_per_page;
355 extent_offset = lower_extent_num % extents_per_page;
356 (*byte_offset) = extent_offset * crypt_stat->extent_size;
357 ecryptfs_printk(KERN_DEBUG, " * crypt_stat->header_extent_size = "
358 "[%d]\n", crypt_stat->header_extent_size);
359 ecryptfs_printk(KERN_DEBUG, " * crypt_stat->"
360 "num_header_extents_at_front = [%d]\n",
361 crypt_stat->num_header_extents_at_front);
362 ecryptfs_printk(KERN_DEBUG, " * extents_occupied_by_headers_at_"
363 "front = [%d]\n", extents_occupied_by_headers_at_front);
364 ecryptfs_printk(KERN_DEBUG, " * lower_extent_num = [0x%.16x]\n",
365 lower_extent_num);
366 ecryptfs_printk(KERN_DEBUG, " * extents_per_page = [%d]\n",
367 extents_per_page);
368 ecryptfs_printk(KERN_DEBUG, " * (*lower_page_idx) = [0x%.16x]\n",
369 (*lower_page_idx));
370 ecryptfs_printk(KERN_DEBUG, " * extent_offset = [%d]\n",
371 extent_offset);
372 ecryptfs_printk(KERN_DEBUG, " * (*byte_offset) = [%d]\n",
373 (*byte_offset));
374}
375
376static int ecryptfs_write_out_page(struct ecryptfs_page_crypt_context *ctx,
377 struct page *lower_page,
378 struct inode *lower_inode,
379 int byte_offset_in_page, int bytes_to_write)
380{
381 int rc = 0;
382
383 if (ctx->mode == ECRYPTFS_PREPARE_COMMIT_MODE) {
384 rc = ecryptfs_commit_lower_page(lower_page, lower_inode,
385 ctx->param.lower_file,
386 byte_offset_in_page,
387 bytes_to_write);
388 if (rc) {
389 ecryptfs_printk(KERN_ERR, "Error calling lower "
390 "commit; rc = [%d]\n", rc);
391 goto out;
392 }
393 } else {
394 rc = ecryptfs_writepage_and_release_lower_page(lower_page,
395 lower_inode,
396 ctx->param.wbc);
397 if (rc) {
398 ecryptfs_printk(KERN_ERR, "Error calling lower "
399 "writepage(); rc = [%d]\n", rc);
400 goto out;
401 }
402 }
403out:
404 return rc;
405}
406
407static int ecryptfs_read_in_page(struct ecryptfs_page_crypt_context *ctx,
408 struct page **lower_page,
409 struct inode *lower_inode,
410 unsigned long lower_page_idx,
411 int byte_offset_in_page)
412{
413 int rc = 0;
414
415 if (ctx->mode == ECRYPTFS_PREPARE_COMMIT_MODE) {
416 /* TODO: Limit this to only the data extents that are
417 * needed */
418 rc = ecryptfs_get_lower_page(lower_page, lower_inode,
419 ctx->param.lower_file,
420 lower_page_idx,
421 byte_offset_in_page,
422 (PAGE_CACHE_SIZE
423 - byte_offset_in_page));
424 if (rc) {
425 ecryptfs_printk(
426 KERN_ERR, "Error attempting to grab, map, "
427 "and prepare_write lower page with index "
428 "[0x%.16x]; rc = [%d]\n", lower_page_idx, rc);
429 goto out;
430 }
431 } else {
432 rc = ecryptfs_grab_and_map_lower_page(lower_page, NULL,
433 lower_inode,
434 lower_page_idx);
435 if (rc) {
436 ecryptfs_printk(
437 KERN_ERR, "Error attempting to grab and map "
438 "lower page with index [0x%.16x]; rc = [%d]\n",
439 lower_page_idx, rc);
440 goto out;
441 }
442 }
443out:
444 return rc;
445}
446
447/**
448 * ecryptfs_encrypt_page
449 * @ctx: The context of the page
450 *
451 * Encrypt an eCryptfs page. This is done on a per-extent basis. Note
452 * that eCryptfs pages may straddle the lower pages -- for instance,
453 * if the file was created on a machine with an 8K page size
454 * (resulting in an 8K header), and then the file is copied onto a
455 * host with a 32K page size, then when reading page 0 of the eCryptfs
456 * file, 24K of page 0 of the lower file will be read and decrypted,
457 * and then 8K of page 1 of the lower file will be read and decrypted.
458 *
459 * The actual operations performed on each page depends on the
460 * contents of the ecryptfs_page_crypt_context struct.
461 *
462 * Returns zero on success; negative on error
463 */
464int ecryptfs_encrypt_page(struct ecryptfs_page_crypt_context *ctx)
465{
466 char extent_iv[ECRYPTFS_MAX_IV_BYTES];
467 unsigned long base_extent;
468 unsigned long extent_offset = 0;
469 unsigned long lower_page_idx = 0;
470 unsigned long prior_lower_page_idx = 0;
471 struct page *lower_page;
472 struct inode *lower_inode;
473 struct ecryptfs_inode_info *inode_info;
474 struct ecryptfs_crypt_stat *crypt_stat;
475 int rc = 0;
476 int lower_byte_offset = 0;
477 int orig_byte_offset = 0;
478 int num_extents_per_page;
479#define ECRYPTFS_PAGE_STATE_UNREAD 0
480#define ECRYPTFS_PAGE_STATE_READ 1
481#define ECRYPTFS_PAGE_STATE_MODIFIED 2
482#define ECRYPTFS_PAGE_STATE_WRITTEN 3
483 int page_state;
484
485 lower_inode = ecryptfs_inode_to_lower(ctx->page->mapping->host);
486 inode_info = ecryptfs_inode_to_private(ctx->page->mapping->host);
487 crypt_stat = &inode_info->crypt_stat;
488 if (!ECRYPTFS_CHECK_FLAG(crypt_stat->flags, ECRYPTFS_ENCRYPTED)) {
489 rc = ecryptfs_copy_page_to_lower(ctx->page, lower_inode,
490 ctx->param.lower_file);
491 if (rc)
492 ecryptfs_printk(KERN_ERR, "Error attempting to copy "
493 "page at index [0x%.16x]\n",
494 ctx->page->index);
495 goto out;
496 }
497 num_extents_per_page = PAGE_CACHE_SIZE / crypt_stat->extent_size;
498 base_extent = (ctx->page->index * num_extents_per_page);
499 page_state = ECRYPTFS_PAGE_STATE_UNREAD;
500 while (extent_offset < num_extents_per_page) {
501 ecryptfs_extent_to_lwr_pg_idx_and_offset(
502 &lower_page_idx, &lower_byte_offset, crypt_stat,
503 (base_extent + extent_offset));
504 if (prior_lower_page_idx != lower_page_idx
505 && page_state == ECRYPTFS_PAGE_STATE_MODIFIED) {
506 rc = ecryptfs_write_out_page(ctx, lower_page,
507 lower_inode,
508 orig_byte_offset,
509 (PAGE_CACHE_SIZE
510 - orig_byte_offset));
511 if (rc) {
512 ecryptfs_printk(KERN_ERR, "Error attempting "
513 "to write out page; rc = [%d]"
514 "\n", rc);
515 goto out;
516 }
517 page_state = ECRYPTFS_PAGE_STATE_WRITTEN;
518 }
519 if (page_state == ECRYPTFS_PAGE_STATE_UNREAD
520 || page_state == ECRYPTFS_PAGE_STATE_WRITTEN) {
521 rc = ecryptfs_read_in_page(ctx, &lower_page,
522 lower_inode, lower_page_idx,
523 lower_byte_offset);
524 if (rc) {
525 ecryptfs_printk(KERN_ERR, "Error attempting "
526 "to read in lower page with "
527 "index [0x%.16x]; rc = [%d]\n",
528 lower_page_idx, rc);
529 goto out;
530 }
531 orig_byte_offset = lower_byte_offset;
532 prior_lower_page_idx = lower_page_idx;
533 page_state = ECRYPTFS_PAGE_STATE_READ;
534 }
535 BUG_ON(!(page_state == ECRYPTFS_PAGE_STATE_MODIFIED
536 || page_state == ECRYPTFS_PAGE_STATE_READ));
537 rc = ecryptfs_derive_iv(extent_iv, crypt_stat,
538 (base_extent + extent_offset));
539 if (rc) {
540 ecryptfs_printk(KERN_ERR, "Error attempting to "
541 "derive IV for extent [0x%.16x]; "
542 "rc = [%d]\n",
543 (base_extent + extent_offset), rc);
544 goto out;
545 }
546 if (unlikely(ecryptfs_verbosity > 0)) {
547 ecryptfs_printk(KERN_DEBUG, "Encrypting extent "
548 "with iv:\n");
549 ecryptfs_dump_hex(extent_iv, crypt_stat->iv_bytes);
550 ecryptfs_printk(KERN_DEBUG, "First 8 bytes before "
551 "encryption:\n");
552 ecryptfs_dump_hex((char *)
553 (page_address(ctx->page)
554 + (extent_offset
555 * crypt_stat->extent_size)), 8);
556 }
557 rc = ecryptfs_encrypt_page_offset(
558 crypt_stat, lower_page, lower_byte_offset, ctx->page,
559 (extent_offset * crypt_stat->extent_size),
560 crypt_stat->extent_size, extent_iv);
561 ecryptfs_printk(KERN_DEBUG, "Encrypt extent [0x%.16x]; "
562 "rc = [%d]\n",
563 (base_extent + extent_offset), rc);
564 if (unlikely(ecryptfs_verbosity > 0)) {
565 ecryptfs_printk(KERN_DEBUG, "First 8 bytes after "
566 "encryption:\n");
567 ecryptfs_dump_hex((char *)(page_address(lower_page)
568 + lower_byte_offset), 8);
569 }
570 page_state = ECRYPTFS_PAGE_STATE_MODIFIED;
571 extent_offset++;
572 }
573 BUG_ON(orig_byte_offset != 0);
574 rc = ecryptfs_write_out_page(ctx, lower_page, lower_inode, 0,
575 (lower_byte_offset
576 + crypt_stat->extent_size));
577 if (rc) {
578 ecryptfs_printk(KERN_ERR, "Error attempting to write out "
579 "page; rc = [%d]\n", rc);
580 goto out;
581 }
582out:
583 return rc;
584}
585
586/**
587 * ecryptfs_decrypt_page
588 * @file: The ecryptfs file
589 * @page: The page in ecryptfs to decrypt
590 *
591 * Decrypt an eCryptfs page. This is done on a per-extent basis. Note
592 * that eCryptfs pages may straddle the lower pages -- for instance,
593 * if the file was created on a machine with an 8K page size
594 * (resulting in an 8K header), and then the file is copied onto a
595 * host with a 32K page size, then when reading page 0 of the eCryptfs
596 * file, 24K of page 0 of the lower file will be read and decrypted,
597 * and then 8K of page 1 of the lower file will be read and decrypted.
598 *
599 * Returns zero on success; negative on error
600 */
601int ecryptfs_decrypt_page(struct file *file, struct page *page)
602{
603 char extent_iv[ECRYPTFS_MAX_IV_BYTES];
604 unsigned long base_extent;
605 unsigned long extent_offset = 0;
606 unsigned long lower_page_idx = 0;
607 unsigned long prior_lower_page_idx = 0;
608 struct page *lower_page;
609 char *lower_page_virt = NULL;
610 struct inode *lower_inode;
611 struct ecryptfs_crypt_stat *crypt_stat;
612 int rc = 0;
613 int byte_offset;
614 int num_extents_per_page;
615 int page_state;
616
617 crypt_stat = &(ecryptfs_inode_to_private(
618 page->mapping->host)->crypt_stat);
619 lower_inode = ecryptfs_inode_to_lower(page->mapping->host);
620 if (!ECRYPTFS_CHECK_FLAG(crypt_stat->flags, ECRYPTFS_ENCRYPTED)) {
621 rc = ecryptfs_do_readpage(file, page, page->index);
622 if (rc)
623 ecryptfs_printk(KERN_ERR, "Error attempting to copy "
624 "page at index [0x%.16x]\n",
625 page->index);
626 goto out;
627 }
628 num_extents_per_page = PAGE_CACHE_SIZE / crypt_stat->extent_size;
629 base_extent = (page->index * num_extents_per_page);
630 lower_page_virt = kmem_cache_alloc(ecryptfs_lower_page_cache,
Christoph Lametere94b1762006-12-06 20:33:17 -0800631 GFP_KERNEL);
Michael Halcrow237fead2006-10-04 02:16:22 -0700632 if (!lower_page_virt) {
633 rc = -ENOMEM;
634 ecryptfs_printk(KERN_ERR, "Error getting page for encrypted "
635 "lower page(s)\n");
636 goto out;
637 }
638 lower_page = virt_to_page(lower_page_virt);
639 page_state = ECRYPTFS_PAGE_STATE_UNREAD;
640 while (extent_offset < num_extents_per_page) {
641 ecryptfs_extent_to_lwr_pg_idx_and_offset(
642 &lower_page_idx, &byte_offset, crypt_stat,
643 (base_extent + extent_offset));
644 if (prior_lower_page_idx != lower_page_idx
645 || page_state == ECRYPTFS_PAGE_STATE_UNREAD) {
646 rc = ecryptfs_do_readpage(file, lower_page,
647 lower_page_idx);
648 if (rc) {
649 ecryptfs_printk(KERN_ERR, "Error reading "
650 "lower encrypted page; rc = "
651 "[%d]\n", rc);
652 goto out;
653 }
654 prior_lower_page_idx = lower_page_idx;
655 page_state = ECRYPTFS_PAGE_STATE_READ;
656 }
657 rc = ecryptfs_derive_iv(extent_iv, crypt_stat,
658 (base_extent + extent_offset));
659 if (rc) {
660 ecryptfs_printk(KERN_ERR, "Error attempting to "
661 "derive IV for extent [0x%.16x]; rc = "
662 "[%d]\n",
663 (base_extent + extent_offset), rc);
664 goto out;
665 }
666 if (unlikely(ecryptfs_verbosity > 0)) {
667 ecryptfs_printk(KERN_DEBUG, "Decrypting extent "
668 "with iv:\n");
669 ecryptfs_dump_hex(extent_iv, crypt_stat->iv_bytes);
670 ecryptfs_printk(KERN_DEBUG, "First 8 bytes before "
671 "decryption:\n");
672 ecryptfs_dump_hex((lower_page_virt + byte_offset), 8);
673 }
674 rc = ecryptfs_decrypt_page_offset(crypt_stat, page,
675 (extent_offset
676 * crypt_stat->extent_size),
677 lower_page, byte_offset,
678 crypt_stat->extent_size,
679 extent_iv);
680 if (rc != crypt_stat->extent_size) {
681 ecryptfs_printk(KERN_ERR, "Error attempting to "
682 "decrypt extent [0x%.16x]\n",
683 (base_extent + extent_offset));
684 goto out;
685 }
686 rc = 0;
687 if (unlikely(ecryptfs_verbosity > 0)) {
688 ecryptfs_printk(KERN_DEBUG, "First 8 bytes after "
689 "decryption:\n");
690 ecryptfs_dump_hex((char *)(page_address(page)
691 + byte_offset), 8);
692 }
693 extent_offset++;
694 }
695out:
696 if (lower_page_virt)
697 kmem_cache_free(ecryptfs_lower_page_cache, lower_page_virt);
698 return rc;
699}
700
701/**
702 * decrypt_scatterlist
703 *
704 * Returns the number of bytes decrypted; negative value on error
705 */
706static int decrypt_scatterlist(struct ecryptfs_crypt_stat *crypt_stat,
707 struct scatterlist *dest_sg,
708 struct scatterlist *src_sg, int size,
709 unsigned char *iv)
710{
Michael Halcrow8bba0662006-10-30 22:07:18 -0800711 struct blkcipher_desc desc = {
712 .tfm = crypt_stat->tfm,
713 .info = iv,
714 .flags = CRYPTO_TFM_REQ_MAY_SLEEP
715 };
Michael Halcrow237fead2006-10-04 02:16:22 -0700716 int rc = 0;
717
718 /* Consider doing this once, when the file is opened */
719 mutex_lock(&crypt_stat->cs_tfm_mutex);
Michael Halcrow8bba0662006-10-30 22:07:18 -0800720 rc = crypto_blkcipher_setkey(crypt_stat->tfm, crypt_stat->key,
721 crypt_stat->key_size);
Michael Halcrow237fead2006-10-04 02:16:22 -0700722 if (rc) {
723 ecryptfs_printk(KERN_ERR, "Error setting key; rc = [%d]\n",
724 rc);
725 mutex_unlock(&crypt_stat->cs_tfm_mutex);
726 rc = -EINVAL;
727 goto out;
728 }
729 ecryptfs_printk(KERN_DEBUG, "Decrypting [%d] bytes.\n", size);
Michael Halcrow8bba0662006-10-30 22:07:18 -0800730 rc = crypto_blkcipher_decrypt_iv(&desc, dest_sg, src_sg, size);
Michael Halcrow237fead2006-10-04 02:16:22 -0700731 mutex_unlock(&crypt_stat->cs_tfm_mutex);
732 if (rc) {
733 ecryptfs_printk(KERN_ERR, "Error decrypting; rc = [%d]\n",
734 rc);
735 goto out;
736 }
737 rc = size;
738out:
739 return rc;
740}
741
742/**
743 * ecryptfs_encrypt_page_offset
744 *
745 * Returns the number of bytes encrypted
746 */
747static int
748ecryptfs_encrypt_page_offset(struct ecryptfs_crypt_stat *crypt_stat,
749 struct page *dst_page, int dst_offset,
750 struct page *src_page, int src_offset, int size,
751 unsigned char *iv)
752{
753 struct scatterlist src_sg, dst_sg;
754
755 src_sg.page = src_page;
756 src_sg.offset = src_offset;
757 src_sg.length = size;
758 dst_sg.page = dst_page;
759 dst_sg.offset = dst_offset;
760 dst_sg.length = size;
761 return encrypt_scatterlist(crypt_stat, &dst_sg, &src_sg, size, iv);
762}
763
764/**
765 * ecryptfs_decrypt_page_offset
766 *
767 * Returns the number of bytes decrypted
768 */
769static int
770ecryptfs_decrypt_page_offset(struct ecryptfs_crypt_stat *crypt_stat,
771 struct page *dst_page, int dst_offset,
772 struct page *src_page, int src_offset, int size,
773 unsigned char *iv)
774{
775 struct scatterlist src_sg, dst_sg;
776
777 src_sg.page = src_page;
778 src_sg.offset = src_offset;
779 src_sg.length = size;
780 dst_sg.page = dst_page;
781 dst_sg.offset = dst_offset;
782 dst_sg.length = size;
783 return decrypt_scatterlist(crypt_stat, &dst_sg, &src_sg, size, iv);
784}
785
786#define ECRYPTFS_MAX_SCATTERLIST_LEN 4
787
788/**
789 * ecryptfs_init_crypt_ctx
790 * @crypt_stat: Uninitilized crypt stats structure
791 *
792 * Initialize the crypto context.
793 *
794 * TODO: Performance: Keep a cache of initialized cipher contexts;
795 * only init if needed
796 */
797int ecryptfs_init_crypt_ctx(struct ecryptfs_crypt_stat *crypt_stat)
798{
Michael Halcrow8bba0662006-10-30 22:07:18 -0800799 char *full_alg_name;
Michael Halcrow237fead2006-10-04 02:16:22 -0700800 int rc = -EINVAL;
801
802 if (!crypt_stat->cipher) {
803 ecryptfs_printk(KERN_ERR, "No cipher specified\n");
804 goto out;
805 }
806 ecryptfs_printk(KERN_DEBUG,
807 "Initializing cipher [%s]; strlen = [%d]; "
808 "key_size_bits = [%d]\n",
809 crypt_stat->cipher, (int)strlen(crypt_stat->cipher),
810 crypt_stat->key_size << 3);
811 if (crypt_stat->tfm) {
812 rc = 0;
813 goto out;
814 }
815 mutex_lock(&crypt_stat->cs_tfm_mutex);
Michael Halcrow8bba0662006-10-30 22:07:18 -0800816 rc = ecryptfs_crypto_api_algify_cipher_name(&full_alg_name,
817 crypt_stat->cipher, "cbc");
818 if (rc)
819 goto out;
820 crypt_stat->tfm = crypto_alloc_blkcipher(full_alg_name, 0,
821 CRYPTO_ALG_ASYNC);
822 kfree(full_alg_name);
Akinobu Mitade887772006-11-28 12:29:49 -0800823 if (IS_ERR(crypt_stat->tfm)) {
824 rc = PTR_ERR(crypt_stat->tfm);
Michael Halcrow237fead2006-10-04 02:16:22 -0700825 ecryptfs_printk(KERN_ERR, "cryptfs: init_crypt_ctx(): "
826 "Error initializing cipher [%s]\n",
827 crypt_stat->cipher);
Michael Halcrow8bba0662006-10-30 22:07:18 -0800828 mutex_unlock(&crypt_stat->cs_tfm_mutex);
Michael Halcrow237fead2006-10-04 02:16:22 -0700829 goto out;
830 }
Herbert Xuf1ddcaf2007-01-27 10:05:15 +1100831 crypto_blkcipher_set_flags(crypt_stat->tfm, CRYPTO_TFM_REQ_WEAK_KEY);
Michael Halcrow8bba0662006-10-30 22:07:18 -0800832 mutex_unlock(&crypt_stat->cs_tfm_mutex);
Michael Halcrow237fead2006-10-04 02:16:22 -0700833 rc = 0;
834out:
835 return rc;
836}
837
838static void set_extent_mask_and_shift(struct ecryptfs_crypt_stat *crypt_stat)
839{
840 int extent_size_tmp;
841
842 crypt_stat->extent_mask = 0xFFFFFFFF;
843 crypt_stat->extent_shift = 0;
844 if (crypt_stat->extent_size == 0)
845 return;
846 extent_size_tmp = crypt_stat->extent_size;
847 while ((extent_size_tmp & 0x01) == 0) {
848 extent_size_tmp >>= 1;
849 crypt_stat->extent_mask <<= 1;
850 crypt_stat->extent_shift++;
851 }
852}
853
854void ecryptfs_set_default_sizes(struct ecryptfs_crypt_stat *crypt_stat)
855{
856 /* Default values; may be overwritten as we are parsing the
857 * packets. */
858 crypt_stat->extent_size = ECRYPTFS_DEFAULT_EXTENT_SIZE;
859 set_extent_mask_and_shift(crypt_stat);
860 crypt_stat->iv_bytes = ECRYPTFS_DEFAULT_IV_BYTES;
861 if (PAGE_CACHE_SIZE <= ECRYPTFS_MINIMUM_HEADER_EXTENT_SIZE) {
862 crypt_stat->header_extent_size =
863 ECRYPTFS_MINIMUM_HEADER_EXTENT_SIZE;
864 } else
865 crypt_stat->header_extent_size = PAGE_CACHE_SIZE;
Michael Halcrowdd2a3b72007-02-12 00:53:46 -0800866 if (crypt_stat->flags & ECRYPTFS_METADATA_IN_XATTR)
867 crypt_stat->num_header_extents_at_front = 0;
868 else
869 crypt_stat->num_header_extents_at_front = 1;
Michael Halcrow237fead2006-10-04 02:16:22 -0700870}
871
872/**
873 * ecryptfs_compute_root_iv
874 * @crypt_stats
875 *
876 * On error, sets the root IV to all 0's.
877 */
878int ecryptfs_compute_root_iv(struct ecryptfs_crypt_stat *crypt_stat)
879{
880 int rc = 0;
881 char dst[MD5_DIGEST_SIZE];
882
883 BUG_ON(crypt_stat->iv_bytes > MD5_DIGEST_SIZE);
884 BUG_ON(crypt_stat->iv_bytes <= 0);
885 if (!ECRYPTFS_CHECK_FLAG(crypt_stat->flags, ECRYPTFS_KEY_VALID)) {
886 rc = -EINVAL;
887 ecryptfs_printk(KERN_WARNING, "Session key not valid; "
888 "cannot generate root IV\n");
889 goto out;
890 }
891 rc = ecryptfs_calculate_md5(dst, crypt_stat, crypt_stat->key,
892 crypt_stat->key_size);
893 if (rc) {
894 ecryptfs_printk(KERN_WARNING, "Error attempting to compute "
895 "MD5 while generating root IV\n");
896 goto out;
897 }
898 memcpy(crypt_stat->root_iv, dst, crypt_stat->iv_bytes);
899out:
900 if (rc) {
901 memset(crypt_stat->root_iv, 0, crypt_stat->iv_bytes);
902 ECRYPTFS_SET_FLAG(crypt_stat->flags,
903 ECRYPTFS_SECURITY_WARNING);
904 }
905 return rc;
906}
907
908static void ecryptfs_generate_new_key(struct ecryptfs_crypt_stat *crypt_stat)
909{
910 get_random_bytes(crypt_stat->key, crypt_stat->key_size);
911 ECRYPTFS_SET_FLAG(crypt_stat->flags, ECRYPTFS_KEY_VALID);
912 ecryptfs_compute_root_iv(crypt_stat);
913 if (unlikely(ecryptfs_verbosity > 0)) {
914 ecryptfs_printk(KERN_DEBUG, "Generated new session key:\n");
915 ecryptfs_dump_hex(crypt_stat->key,
916 crypt_stat->key_size);
917 }
918}
919
920/**
Michael Halcrow17398952007-02-12 00:53:45 -0800921 * ecryptfs_copy_mount_wide_flags_to_inode_flags
922 *
923 * This function propagates the mount-wide flags to individual inode
924 * flags.
925 */
926static void ecryptfs_copy_mount_wide_flags_to_inode_flags(
927 struct ecryptfs_crypt_stat *crypt_stat,
928 struct ecryptfs_mount_crypt_stat *mount_crypt_stat)
929{
930 if (mount_crypt_stat->flags & ECRYPTFS_XATTR_METADATA_ENABLED)
931 crypt_stat->flags |= ECRYPTFS_METADATA_IN_XATTR;
932 if (mount_crypt_stat->flags & ECRYPTFS_ENCRYPTED_VIEW_ENABLED)
933 crypt_stat->flags |= ECRYPTFS_VIEW_AS_ENCRYPTED;
934}
935
936/**
Michael Halcrow237fead2006-10-04 02:16:22 -0700937 * ecryptfs_set_default_crypt_stat_vals
938 * @crypt_stat
939 *
940 * Default values in the event that policy does not override them.
941 */
942static void ecryptfs_set_default_crypt_stat_vals(
943 struct ecryptfs_crypt_stat *crypt_stat,
944 struct ecryptfs_mount_crypt_stat *mount_crypt_stat)
945{
Michael Halcrow17398952007-02-12 00:53:45 -0800946 ecryptfs_copy_mount_wide_flags_to_inode_flags(crypt_stat,
947 mount_crypt_stat);
Michael Halcrow237fead2006-10-04 02:16:22 -0700948 ecryptfs_set_default_sizes(crypt_stat);
949 strcpy(crypt_stat->cipher, ECRYPTFS_DEFAULT_CIPHER);
950 crypt_stat->key_size = ECRYPTFS_DEFAULT_KEY_BYTES;
951 ECRYPTFS_CLEAR_FLAG(crypt_stat->flags, ECRYPTFS_KEY_VALID);
952 crypt_stat->file_version = ECRYPTFS_FILE_VERSION;
953 crypt_stat->mount_crypt_stat = mount_crypt_stat;
954}
955
956/**
957 * ecryptfs_new_file_context
958 * @ecryptfs_dentry
959 *
960 * If the crypto context for the file has not yet been established,
961 * this is where we do that. Establishing a new crypto context
962 * involves the following decisions:
963 * - What cipher to use?
964 * - What set of authentication tokens to use?
965 * Here we just worry about getting enough information into the
966 * authentication tokens so that we know that they are available.
967 * We associate the available authentication tokens with the new file
968 * via the set of signatures in the crypt_stat struct. Later, when
969 * the headers are actually written out, we may again defer to
970 * userspace to perform the encryption of the session key; for the
971 * foreseeable future, this will be the case with public key packets.
972 *
973 * Returns zero on success; non-zero otherwise
974 */
975/* Associate an authentication token(s) with the file */
976int ecryptfs_new_file_context(struct dentry *ecryptfs_dentry)
977{
978 int rc = 0;
979 struct ecryptfs_crypt_stat *crypt_stat =
980 &ecryptfs_inode_to_private(ecryptfs_dentry->d_inode)->crypt_stat;
981 struct ecryptfs_mount_crypt_stat *mount_crypt_stat =
982 &ecryptfs_superblock_to_private(
983 ecryptfs_dentry->d_sb)->mount_crypt_stat;
984 int cipher_name_len;
985
986 ecryptfs_set_default_crypt_stat_vals(crypt_stat, mount_crypt_stat);
987 /* See if there are mount crypt options */
988 if (mount_crypt_stat->global_auth_tok) {
989 ecryptfs_printk(KERN_DEBUG, "Initializing context for new "
990 "file using mount_crypt_stat\n");
991 ECRYPTFS_SET_FLAG(crypt_stat->flags, ECRYPTFS_ENCRYPTED);
992 ECRYPTFS_SET_FLAG(crypt_stat->flags, ECRYPTFS_KEY_VALID);
Michael Halcrow17398952007-02-12 00:53:45 -0800993 ecryptfs_copy_mount_wide_flags_to_inode_flags(crypt_stat,
994 mount_crypt_stat);
Michael Halcrow237fead2006-10-04 02:16:22 -0700995 memcpy(crypt_stat->keysigs[crypt_stat->num_keysigs++],
996 mount_crypt_stat->global_auth_tok_sig,
997 ECRYPTFS_SIG_SIZE_HEX);
998 cipher_name_len =
999 strlen(mount_crypt_stat->global_default_cipher_name);
1000 memcpy(crypt_stat->cipher,
1001 mount_crypt_stat->global_default_cipher_name,
1002 cipher_name_len);
1003 crypt_stat->cipher[cipher_name_len] = '\0';
1004 crypt_stat->key_size =
1005 mount_crypt_stat->global_default_cipher_key_size;
1006 ecryptfs_generate_new_key(crypt_stat);
1007 } else
1008 /* We should not encounter this scenario since we
1009 * should detect lack of global_auth_tok at mount time
1010 * TODO: Applies to 0.1 release only; remove in future
1011 * release */
1012 BUG();
1013 rc = ecryptfs_init_crypt_ctx(crypt_stat);
1014 if (rc)
1015 ecryptfs_printk(KERN_ERR, "Error initializing cryptographic "
1016 "context for cipher [%s]: rc = [%d]\n",
1017 crypt_stat->cipher, rc);
1018 return rc;
1019}
1020
1021/**
1022 * contains_ecryptfs_marker - check for the ecryptfs marker
1023 * @data: The data block in which to check
1024 *
1025 * Returns one if marker found; zero if not found
1026 */
Michael Halcrowdd2a3b72007-02-12 00:53:46 -08001027static int contains_ecryptfs_marker(char *data)
Michael Halcrow237fead2006-10-04 02:16:22 -07001028{
1029 u32 m_1, m_2;
1030
1031 memcpy(&m_1, data, 4);
1032 m_1 = be32_to_cpu(m_1);
1033 memcpy(&m_2, (data + 4), 4);
1034 m_2 = be32_to_cpu(m_2);
1035 if ((m_1 ^ MAGIC_ECRYPTFS_MARKER) == m_2)
1036 return 1;
1037 ecryptfs_printk(KERN_DEBUG, "m_1 = [0x%.8x]; m_2 = [0x%.8x]; "
1038 "MAGIC_ECRYPTFS_MARKER = [0x%.8x]\n", m_1, m_2,
1039 MAGIC_ECRYPTFS_MARKER);
1040 ecryptfs_printk(KERN_DEBUG, "(m_1 ^ MAGIC_ECRYPTFS_MARKER) = "
1041 "[0x%.8x]\n", (m_1 ^ MAGIC_ECRYPTFS_MARKER));
1042 return 0;
1043}
1044
1045struct ecryptfs_flag_map_elem {
1046 u32 file_flag;
1047 u32 local_flag;
1048};
1049
1050/* Add support for additional flags by adding elements here. */
1051static struct ecryptfs_flag_map_elem ecryptfs_flag_map[] = {
1052 {0x00000001, ECRYPTFS_ENABLE_HMAC},
Michael Halcrowdd2a3b72007-02-12 00:53:46 -08001053 {0x00000002, ECRYPTFS_ENCRYPTED},
1054 {0x00000004, ECRYPTFS_METADATA_IN_XATTR}
Michael Halcrow237fead2006-10-04 02:16:22 -07001055};
1056
1057/**
1058 * ecryptfs_process_flags
1059 * @crypt_stat
1060 * @page_virt: Source data to be parsed
1061 * @bytes_read: Updated with the number of bytes read
1062 *
1063 * Returns zero on success; non-zero if the flag set is invalid
1064 */
1065static int ecryptfs_process_flags(struct ecryptfs_crypt_stat *crypt_stat,
1066 char *page_virt, int *bytes_read)
1067{
1068 int rc = 0;
1069 int i;
1070 u32 flags;
1071
1072 memcpy(&flags, page_virt, 4);
1073 flags = be32_to_cpu(flags);
1074 for (i = 0; i < ((sizeof(ecryptfs_flag_map)
1075 / sizeof(struct ecryptfs_flag_map_elem))); i++)
1076 if (flags & ecryptfs_flag_map[i].file_flag) {
1077 ECRYPTFS_SET_FLAG(crypt_stat->flags,
1078 ecryptfs_flag_map[i].local_flag);
1079 } else
1080 ECRYPTFS_CLEAR_FLAG(crypt_stat->flags,
1081 ecryptfs_flag_map[i].local_flag);
1082 /* Version is in top 8 bits of the 32-bit flag vector */
1083 crypt_stat->file_version = ((flags >> 24) & 0xFF);
1084 (*bytes_read) = 4;
1085 return rc;
1086}
1087
1088/**
1089 * write_ecryptfs_marker
1090 * @page_virt: The pointer to in a page to begin writing the marker
1091 * @written: Number of bytes written
1092 *
1093 * Marker = 0x3c81b7f5
1094 */
1095static void write_ecryptfs_marker(char *page_virt, size_t *written)
1096{
1097 u32 m_1, m_2;
1098
1099 get_random_bytes(&m_1, (MAGIC_ECRYPTFS_MARKER_SIZE_BYTES / 2));
1100 m_2 = (m_1 ^ MAGIC_ECRYPTFS_MARKER);
1101 m_1 = cpu_to_be32(m_1);
1102 memcpy(page_virt, &m_1, (MAGIC_ECRYPTFS_MARKER_SIZE_BYTES / 2));
1103 m_2 = cpu_to_be32(m_2);
1104 memcpy(page_virt + (MAGIC_ECRYPTFS_MARKER_SIZE_BYTES / 2), &m_2,
1105 (MAGIC_ECRYPTFS_MARKER_SIZE_BYTES / 2));
1106 (*written) = MAGIC_ECRYPTFS_MARKER_SIZE_BYTES;
1107}
1108
1109static void
1110write_ecryptfs_flags(char *page_virt, struct ecryptfs_crypt_stat *crypt_stat,
1111 size_t *written)
1112{
1113 u32 flags = 0;
1114 int i;
1115
1116 for (i = 0; i < ((sizeof(ecryptfs_flag_map)
1117 / sizeof(struct ecryptfs_flag_map_elem))); i++)
1118 if (ECRYPTFS_CHECK_FLAG(crypt_stat->flags,
1119 ecryptfs_flag_map[i].local_flag))
1120 flags |= ecryptfs_flag_map[i].file_flag;
1121 /* Version is in top 8 bits of the 32-bit flag vector */
1122 flags |= ((((u8)crypt_stat->file_version) << 24) & 0xFF000000);
1123 flags = cpu_to_be32(flags);
1124 memcpy(page_virt, &flags, 4);
1125 (*written) = 4;
1126}
1127
1128struct ecryptfs_cipher_code_str_map_elem {
1129 char cipher_str[16];
1130 u16 cipher_code;
1131};
1132
1133/* Add support for additional ciphers by adding elements here. The
1134 * cipher_code is whatever OpenPGP applicatoins use to identify the
1135 * ciphers. List in order of probability. */
1136static struct ecryptfs_cipher_code_str_map_elem
1137ecryptfs_cipher_code_str_map[] = {
1138 {"aes",RFC2440_CIPHER_AES_128 },
1139 {"blowfish", RFC2440_CIPHER_BLOWFISH},
1140 {"des3_ede", RFC2440_CIPHER_DES3_EDE},
1141 {"cast5", RFC2440_CIPHER_CAST_5},
1142 {"twofish", RFC2440_CIPHER_TWOFISH},
1143 {"cast6", RFC2440_CIPHER_CAST_6},
1144 {"aes", RFC2440_CIPHER_AES_192},
1145 {"aes", RFC2440_CIPHER_AES_256}
1146};
1147
1148/**
1149 * ecryptfs_code_for_cipher_string
1150 * @str: The string representing the cipher name
1151 *
1152 * Returns zero on no match, or the cipher code on match
1153 */
1154u16 ecryptfs_code_for_cipher_string(struct ecryptfs_crypt_stat *crypt_stat)
1155{
1156 int i;
1157 u16 code = 0;
1158 struct ecryptfs_cipher_code_str_map_elem *map =
1159 ecryptfs_cipher_code_str_map;
1160
1161 if (strcmp(crypt_stat->cipher, "aes") == 0) {
1162 switch (crypt_stat->key_size) {
1163 case 16:
1164 code = RFC2440_CIPHER_AES_128;
1165 break;
1166 case 24:
1167 code = RFC2440_CIPHER_AES_192;
1168 break;
1169 case 32:
1170 code = RFC2440_CIPHER_AES_256;
1171 }
1172 } else {
1173 for (i = 0; i < ARRAY_SIZE(ecryptfs_cipher_code_str_map); i++)
1174 if (strcmp(crypt_stat->cipher, map[i].cipher_str) == 0){
1175 code = map[i].cipher_code;
1176 break;
1177 }
1178 }
1179 return code;
1180}
1181
1182/**
1183 * ecryptfs_cipher_code_to_string
1184 * @str: Destination to write out the cipher name
1185 * @cipher_code: The code to convert to cipher name string
1186 *
1187 * Returns zero on success
1188 */
1189int ecryptfs_cipher_code_to_string(char *str, u16 cipher_code)
1190{
1191 int rc = 0;
1192 int i;
1193
1194 str[0] = '\0';
1195 for (i = 0; i < ARRAY_SIZE(ecryptfs_cipher_code_str_map); i++)
1196 if (cipher_code == ecryptfs_cipher_code_str_map[i].cipher_code)
1197 strcpy(str, ecryptfs_cipher_code_str_map[i].cipher_str);
1198 if (str[0] == '\0') {
1199 ecryptfs_printk(KERN_WARNING, "Cipher code not recognized: "
1200 "[%d]\n", cipher_code);
1201 rc = -EINVAL;
1202 }
1203 return rc;
1204}
1205
1206/**
1207 * ecryptfs_read_header_region
1208 * @data
1209 * @dentry
1210 * @nd
1211 *
1212 * Returns zero on success; non-zero otherwise
1213 */
Michael Halcrowdd2a3b72007-02-12 00:53:46 -08001214static int ecryptfs_read_header_region(char *data, struct dentry *dentry,
1215 struct vfsmount *mnt)
Michael Halcrow237fead2006-10-04 02:16:22 -07001216{
Michael Halcrow7ff1d742006-10-30 22:07:19 -08001217 struct file *lower_file;
Michael Halcrow237fead2006-10-04 02:16:22 -07001218 mm_segment_t oldfs;
1219 int rc;
1220
Michael Halcrow7ff1d742006-10-30 22:07:19 -08001221 if ((rc = ecryptfs_open_lower_file(&lower_file, dentry, mnt,
1222 O_RDONLY))) {
1223 printk(KERN_ERR
1224 "Error opening lower_file to read header region\n");
Michael Halcrow237fead2006-10-04 02:16:22 -07001225 goto out;
1226 }
Michael Halcrow7ff1d742006-10-30 22:07:19 -08001227 lower_file->f_pos = 0;
Michael Halcrow237fead2006-10-04 02:16:22 -07001228 oldfs = get_fs();
1229 set_fs(get_ds());
1230 /* For releases 0.1 and 0.2, all of the header information
1231 * fits in the first data extent-sized region. */
Michael Halcrow7ff1d742006-10-30 22:07:19 -08001232 rc = lower_file->f_op->read(lower_file, (char __user *)data,
1233 ECRYPTFS_DEFAULT_EXTENT_SIZE, &lower_file->f_pos);
Michael Halcrow237fead2006-10-04 02:16:22 -07001234 set_fs(oldfs);
Michael Halcrow7ff1d742006-10-30 22:07:19 -08001235 if ((rc = ecryptfs_close_lower_file(lower_file))) {
1236 printk(KERN_ERR "Error closing lower_file\n");
1237 goto out;
1238 }
Michael Halcrow237fead2006-10-04 02:16:22 -07001239 rc = 0;
1240out:
1241 return rc;
1242}
1243
Michael Halcrowdd2a3b72007-02-12 00:53:46 -08001244int ecryptfs_read_and_validate_header_region(char *data, struct dentry *dentry,
1245 struct vfsmount *mnt)
1246{
1247 int rc;
1248
1249 rc = ecryptfs_read_header_region(data, dentry, mnt);
1250 if (rc)
1251 goto out;
1252 if (!contains_ecryptfs_marker(data + ECRYPTFS_FILE_SIZE_BYTES))
1253 rc = -EINVAL;
1254out:
1255 return rc;
1256}
1257
1258
Michael Halcrowe77a56d2007-02-12 00:53:47 -08001259void
1260ecryptfs_write_header_metadata(char *virt,
1261 struct ecryptfs_crypt_stat *crypt_stat,
1262 size_t *written)
Michael Halcrow237fead2006-10-04 02:16:22 -07001263{
1264 u32 header_extent_size;
1265 u16 num_header_extents_at_front;
1266
1267 header_extent_size = (u32)crypt_stat->header_extent_size;
1268 num_header_extents_at_front =
1269 (u16)crypt_stat->num_header_extents_at_front;
1270 header_extent_size = cpu_to_be32(header_extent_size);
1271 memcpy(virt, &header_extent_size, 4);
1272 virt += 4;
1273 num_header_extents_at_front = cpu_to_be16(num_header_extents_at_front);
1274 memcpy(virt, &num_header_extents_at_front, 2);
1275 (*written) = 6;
1276}
1277
1278struct kmem_cache *ecryptfs_header_cache_0;
1279struct kmem_cache *ecryptfs_header_cache_1;
1280struct kmem_cache *ecryptfs_header_cache_2;
1281
1282/**
1283 * ecryptfs_write_headers_virt
1284 * @page_virt
1285 * @crypt_stat
1286 * @ecryptfs_dentry
1287 *
1288 * Format version: 1
1289 *
1290 * Header Extent:
1291 * Octets 0-7: Unencrypted file size (big-endian)
1292 * Octets 8-15: eCryptfs special marker
1293 * Octets 16-19: Flags
1294 * Octet 16: File format version number (between 0 and 255)
1295 * Octets 17-18: Reserved
1296 * Octet 19: Bit 1 (lsb): Reserved
1297 * Bit 2: Encrypted?
1298 * Bits 3-8: Reserved
1299 * Octets 20-23: Header extent size (big-endian)
1300 * Octets 24-25: Number of header extents at front of file
1301 * (big-endian)
1302 * Octet 26: Begin RFC 2440 authentication token packet set
1303 * Data Extent 0:
1304 * Lower data (CBC encrypted)
1305 * Data Extent 1:
1306 * Lower data (CBC encrypted)
1307 * ...
1308 *
1309 * Returns zero on success
1310 */
Michael Halcrowdd2a3b72007-02-12 00:53:46 -08001311static int ecryptfs_write_headers_virt(char *page_virt, size_t *size,
1312 struct ecryptfs_crypt_stat *crypt_stat,
1313 struct dentry *ecryptfs_dentry)
Michael Halcrow237fead2006-10-04 02:16:22 -07001314{
1315 int rc;
1316 size_t written;
1317 size_t offset;
1318
1319 offset = ECRYPTFS_FILE_SIZE_BYTES;
1320 write_ecryptfs_marker((page_virt + offset), &written);
1321 offset += written;
1322 write_ecryptfs_flags((page_virt + offset), crypt_stat, &written);
1323 offset += written;
Michael Halcrowe77a56d2007-02-12 00:53:47 -08001324 ecryptfs_write_header_metadata((page_virt + offset), crypt_stat,
1325 &written);
Michael Halcrow237fead2006-10-04 02:16:22 -07001326 offset += written;
1327 rc = ecryptfs_generate_key_packet_set((page_virt + offset), crypt_stat,
1328 ecryptfs_dentry, &written,
1329 PAGE_CACHE_SIZE - offset);
1330 if (rc)
1331 ecryptfs_printk(KERN_WARNING, "Error generating key packet "
1332 "set; rc = [%d]\n", rc);
Michael Halcrowdd2a3b72007-02-12 00:53:46 -08001333 if (size) {
1334 offset += written;
1335 *size = offset;
1336 }
1337 return rc;
1338}
1339
1340static int ecryptfs_write_metadata_to_contents(struct ecryptfs_crypt_stat *crypt_stat,
1341 struct file *lower_file,
1342 char *page_virt)
1343{
1344 mm_segment_t oldfs;
1345 int current_header_page;
1346 int header_pages;
1347
1348 lower_file->f_pos = 0;
1349 oldfs = get_fs();
1350 set_fs(get_ds());
1351 lower_file->f_op->write(lower_file, (char __user *)page_virt,
1352 PAGE_CACHE_SIZE, &lower_file->f_pos);
1353 header_pages = ((crypt_stat->header_extent_size
1354 * crypt_stat->num_header_extents_at_front)
1355 / PAGE_CACHE_SIZE);
1356 memset(page_virt, 0, PAGE_CACHE_SIZE);
1357 current_header_page = 1;
1358 while (current_header_page < header_pages) {
1359 lower_file->f_op->write(lower_file, (char __user *)page_virt,
1360 PAGE_CACHE_SIZE, &lower_file->f_pos);
1361 current_header_page++;
1362 }
1363 set_fs(oldfs);
1364 return 0;
1365}
1366
1367static int ecryptfs_write_metadata_to_xattr(struct dentry *ecryptfs_dentry,
1368 struct ecryptfs_crypt_stat *crypt_stat,
1369 char *page_virt, size_t size)
1370{
1371 int rc;
1372
1373 rc = ecryptfs_setxattr(ecryptfs_dentry, ECRYPTFS_XATTR_NAME, page_virt,
1374 size, 0);
Michael Halcrow237fead2006-10-04 02:16:22 -07001375 return rc;
1376}
1377
1378/**
Michael Halcrowdd2a3b72007-02-12 00:53:46 -08001379 * ecryptfs_write_metadata
Michael Halcrow237fead2006-10-04 02:16:22 -07001380 * @lower_file: The lower file struct, which was returned from dentry_open
1381 *
1382 * Write the file headers out. This will likely involve a userspace
1383 * callout, in which the session key is encrypted with one or more
1384 * public keys and/or the passphrase necessary to do the encryption is
1385 * retrieved via a prompt. Exactly what happens at this point should
1386 * be policy-dependent.
1387 *
1388 * Returns zero on success; non-zero on error
1389 */
Michael Halcrowdd2a3b72007-02-12 00:53:46 -08001390int ecryptfs_write_metadata(struct dentry *ecryptfs_dentry,
1391 struct file *lower_file)
Michael Halcrow237fead2006-10-04 02:16:22 -07001392{
Michael Halcrow237fead2006-10-04 02:16:22 -07001393 struct ecryptfs_crypt_stat *crypt_stat;
1394 char *page_virt;
Michael Halcrowdd2a3b72007-02-12 00:53:46 -08001395 size_t size;
Michael Halcrow237fead2006-10-04 02:16:22 -07001396 int rc = 0;
1397
1398 crypt_stat = &ecryptfs_inode_to_private(
1399 ecryptfs_dentry->d_inode)->crypt_stat;
1400 if (likely(ECRYPTFS_CHECK_FLAG(crypt_stat->flags,
1401 ECRYPTFS_ENCRYPTED))) {
1402 if (!ECRYPTFS_CHECK_FLAG(crypt_stat->flags,
1403 ECRYPTFS_KEY_VALID)) {
1404 ecryptfs_printk(KERN_DEBUG, "Key is "
1405 "invalid; bailing out\n");
1406 rc = -EINVAL;
1407 goto out;
1408 }
1409 } else {
1410 rc = -EINVAL;
1411 ecryptfs_printk(KERN_WARNING,
1412 "Called with crypt_stat->encrypted == 0\n");
1413 goto out;
1414 }
1415 /* Released in this function */
Robert P. J. Dayc3762222007-02-10 01:45:03 -08001416 page_virt = kmem_cache_zalloc(ecryptfs_header_cache_0, GFP_USER);
Michael Halcrow237fead2006-10-04 02:16:22 -07001417 if (!page_virt) {
1418 ecryptfs_printk(KERN_ERR, "Out of memory\n");
1419 rc = -ENOMEM;
1420 goto out;
1421 }
Michael Halcrowdd2a3b72007-02-12 00:53:46 -08001422 rc = ecryptfs_write_headers_virt(page_virt, &size, crypt_stat,
1423 ecryptfs_dentry);
Michael Halcrow237fead2006-10-04 02:16:22 -07001424 if (unlikely(rc)) {
1425 ecryptfs_printk(KERN_ERR, "Error whilst writing headers\n");
1426 memset(page_virt, 0, PAGE_CACHE_SIZE);
1427 goto out_free;
1428 }
Michael Halcrowdd2a3b72007-02-12 00:53:46 -08001429 if (crypt_stat->flags & ECRYPTFS_METADATA_IN_XATTR)
1430 rc = ecryptfs_write_metadata_to_xattr(ecryptfs_dentry,
1431 crypt_stat, page_virt,
1432 size);
1433 else
1434 rc = ecryptfs_write_metadata_to_contents(crypt_stat, lower_file,
1435 page_virt);
1436 if (rc) {
1437 printk(KERN_ERR "Error writing metadata out to lower file; "
1438 "rc = [%d]\n", rc);
1439 goto out_free;
Michael Halcrow237fead2006-10-04 02:16:22 -07001440 }
Michael Halcrow237fead2006-10-04 02:16:22 -07001441out_free:
1442 kmem_cache_free(ecryptfs_header_cache_0, page_virt);
1443out:
1444 return rc;
1445}
1446
Michael Halcrowdd2a3b72007-02-12 00:53:46 -08001447#define ECRYPTFS_DONT_VALIDATE_HEADER_SIZE 0
1448#define ECRYPTFS_VALIDATE_HEADER_SIZE 1
Michael Halcrow237fead2006-10-04 02:16:22 -07001449static int parse_header_metadata(struct ecryptfs_crypt_stat *crypt_stat,
Michael Halcrowdd2a3b72007-02-12 00:53:46 -08001450 char *virt, int *bytes_read,
1451 int validate_header_size)
Michael Halcrow237fead2006-10-04 02:16:22 -07001452{
1453 int rc = 0;
1454 u32 header_extent_size;
1455 u16 num_header_extents_at_front;
1456
1457 memcpy(&header_extent_size, virt, 4);
1458 header_extent_size = be32_to_cpu(header_extent_size);
1459 virt += 4;
1460 memcpy(&num_header_extents_at_front, virt, 2);
1461 num_header_extents_at_front = be16_to_cpu(num_header_extents_at_front);
1462 crypt_stat->header_extent_size = (int)header_extent_size;
1463 crypt_stat->num_header_extents_at_front =
1464 (int)num_header_extents_at_front;
1465 (*bytes_read) = 6;
Michael Halcrowdd2a3b72007-02-12 00:53:46 -08001466 if ((validate_header_size == ECRYPTFS_VALIDATE_HEADER_SIZE)
1467 && ((crypt_stat->header_extent_size
1468 * crypt_stat->num_header_extents_at_front)
1469 < ECRYPTFS_MINIMUM_HEADER_EXTENT_SIZE)) {
Michael Halcrow237fead2006-10-04 02:16:22 -07001470 rc = -EINVAL;
1471 ecryptfs_printk(KERN_WARNING, "Invalid header extent size: "
1472 "[%d]\n", crypt_stat->header_extent_size);
1473 }
1474 return rc;
1475}
1476
1477/**
1478 * set_default_header_data
1479 *
1480 * For version 0 file format; this function is only for backwards
1481 * compatibility for files created with the prior versions of
1482 * eCryptfs.
1483 */
1484static void set_default_header_data(struct ecryptfs_crypt_stat *crypt_stat)
1485{
1486 crypt_stat->header_extent_size = 4096;
1487 crypt_stat->num_header_extents_at_front = 1;
1488}
1489
1490/**
1491 * ecryptfs_read_headers_virt
1492 *
1493 * Read/parse the header data. The header format is detailed in the
1494 * comment block for the ecryptfs_write_headers_virt() function.
1495 *
1496 * Returns zero on success
1497 */
1498static int ecryptfs_read_headers_virt(char *page_virt,
1499 struct ecryptfs_crypt_stat *crypt_stat,
Michael Halcrowdd2a3b72007-02-12 00:53:46 -08001500 struct dentry *ecryptfs_dentry,
1501 int validate_header_size)
Michael Halcrow237fead2006-10-04 02:16:22 -07001502{
1503 int rc = 0;
1504 int offset;
1505 int bytes_read;
1506
1507 ecryptfs_set_default_sizes(crypt_stat);
1508 crypt_stat->mount_crypt_stat = &ecryptfs_superblock_to_private(
1509 ecryptfs_dentry->d_sb)->mount_crypt_stat;
1510 offset = ECRYPTFS_FILE_SIZE_BYTES;
1511 rc = contains_ecryptfs_marker(page_virt + offset);
1512 if (rc == 0) {
1513 rc = -EINVAL;
1514 goto out;
1515 }
1516 offset += MAGIC_ECRYPTFS_MARKER_SIZE_BYTES;
1517 rc = ecryptfs_process_flags(crypt_stat, (page_virt + offset),
1518 &bytes_read);
1519 if (rc) {
1520 ecryptfs_printk(KERN_WARNING, "Error processing flags\n");
1521 goto out;
1522 }
1523 if (crypt_stat->file_version > ECRYPTFS_SUPPORTED_FILE_VERSION) {
1524 ecryptfs_printk(KERN_WARNING, "File version is [%d]; only "
1525 "file version [%d] is supported by this "
1526 "version of eCryptfs\n",
1527 crypt_stat->file_version,
1528 ECRYPTFS_SUPPORTED_FILE_VERSION);
1529 rc = -EINVAL;
1530 goto out;
1531 }
1532 offset += bytes_read;
1533 if (crypt_stat->file_version >= 1) {
1534 rc = parse_header_metadata(crypt_stat, (page_virt + offset),
Michael Halcrowdd2a3b72007-02-12 00:53:46 -08001535 &bytes_read, validate_header_size);
Michael Halcrow237fead2006-10-04 02:16:22 -07001536 if (rc) {
1537 ecryptfs_printk(KERN_WARNING, "Error reading header "
1538 "metadata; rc = [%d]\n", rc);
1539 }
1540 offset += bytes_read;
1541 } else
1542 set_default_header_data(crypt_stat);
1543 rc = ecryptfs_parse_packet_set(crypt_stat, (page_virt + offset),
1544 ecryptfs_dentry);
1545out:
1546 return rc;
1547}
1548
1549/**
Michael Halcrowdd2a3b72007-02-12 00:53:46 -08001550 * ecryptfs_read_xattr_region
1551 *
1552 * Attempts to read the crypto metadata from the extended attribute
1553 * region of the lower file.
1554 */
1555int ecryptfs_read_xattr_region(char *page_virt, struct dentry *ecryptfs_dentry)
1556{
1557 ssize_t size;
1558 int rc = 0;
1559
1560 size = ecryptfs_getxattr(ecryptfs_dentry, ECRYPTFS_XATTR_NAME,
1561 page_virt, ECRYPTFS_DEFAULT_EXTENT_SIZE);
1562 if (size < 0) {
1563 printk(KERN_DEBUG "Error attempting to read the [%s] "
1564 "xattr from the lower file; return value = [%zd]\n",
1565 ECRYPTFS_XATTR_NAME, size);
1566 rc = -EINVAL;
1567 goto out;
1568 }
1569out:
1570 return rc;
1571}
1572
1573int ecryptfs_read_and_validate_xattr_region(char *page_virt,
1574 struct dentry *ecryptfs_dentry)
1575{
1576 int rc;
1577
1578 rc = ecryptfs_read_xattr_region(page_virt, ecryptfs_dentry);
1579 if (rc)
1580 goto out;
1581 if (!contains_ecryptfs_marker(page_virt + ECRYPTFS_FILE_SIZE_BYTES)) {
1582 printk(KERN_WARNING "Valid data found in [%s] xattr, but "
1583 "the marker is invalid\n", ECRYPTFS_XATTR_NAME);
1584 rc = -EINVAL;
1585 }
1586out:
1587 return rc;
1588}
1589
1590/**
1591 * ecryptfs_read_metadata
1592 *
1593 * Common entry point for reading file metadata. From here, we could
1594 * retrieve the header information from the header region of the file,
1595 * the xattr region of the file, or some other repostory that is
1596 * stored separately from the file itself. The current implementation
1597 * supports retrieving the metadata information from the file contents
1598 * and from the xattr region.
Michael Halcrow237fead2006-10-04 02:16:22 -07001599 *
1600 * Returns zero if valid headers found and parsed; non-zero otherwise
1601 */
Michael Halcrowdd2a3b72007-02-12 00:53:46 -08001602int ecryptfs_read_metadata(struct dentry *ecryptfs_dentry,
1603 struct file *lower_file)
Michael Halcrow237fead2006-10-04 02:16:22 -07001604{
1605 int rc = 0;
1606 char *page_virt = NULL;
1607 mm_segment_t oldfs;
1608 ssize_t bytes_read;
1609 struct ecryptfs_crypt_stat *crypt_stat =
1610 &ecryptfs_inode_to_private(ecryptfs_dentry->d_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 */
Christoph Lameterf7267c02006-12-06 20:33:15 -08001618 page_virt = kmem_cache_alloc(ecryptfs_header_cache_1, GFP_USER);
Michael Halcrow237fead2006-10-04 02:16:22 -07001619 if (!page_virt) {
1620 rc = -ENOMEM;
1621 ecryptfs_printk(KERN_ERR, "Unable to allocate page_virt\n");
1622 goto out;
1623 }
1624 lower_file->f_pos = 0;
1625 oldfs = get_fs();
1626 set_fs(get_ds());
1627 bytes_read = lower_file->f_op->read(lower_file,
1628 (char __user *)page_virt,
1629 ECRYPTFS_DEFAULT_EXTENT_SIZE,
1630 &lower_file->f_pos);
1631 set_fs(oldfs);
1632 if (bytes_read != ECRYPTFS_DEFAULT_EXTENT_SIZE) {
1633 rc = -EINVAL;
1634 goto out;
1635 }
1636 rc = ecryptfs_read_headers_virt(page_virt, crypt_stat,
Michael Halcrowdd2a3b72007-02-12 00:53:46 -08001637 ecryptfs_dentry,
1638 ECRYPTFS_VALIDATE_HEADER_SIZE);
Michael Halcrow237fead2006-10-04 02:16:22 -07001639 if (rc) {
Michael Halcrowdd2a3b72007-02-12 00:53:46 -08001640 rc = ecryptfs_read_xattr_region(page_virt,
1641 ecryptfs_dentry);
1642 if (rc) {
1643 printk(KERN_DEBUG "Valid eCryptfs headers not found in "
1644 "file header region or xattr region\n");
1645 rc = -EINVAL;
1646 goto out;
1647 }
1648 rc = ecryptfs_read_headers_virt(page_virt, crypt_stat,
1649 ecryptfs_dentry,
1650 ECRYPTFS_DONT_VALIDATE_HEADER_SIZE);
1651 if (rc) {
1652 printk(KERN_DEBUG "Valid eCryptfs headers not found in "
1653 "file xattr region either\n");
1654 rc = -EINVAL;
1655 }
1656 if (crypt_stat->mount_crypt_stat->flags
1657 & ECRYPTFS_XATTR_METADATA_ENABLED) {
1658 crypt_stat->flags |= ECRYPTFS_METADATA_IN_XATTR;
1659 } else {
1660 printk(KERN_WARNING "Attempt to access file with "
1661 "crypto metadata only in the extended attribute "
1662 "region, but eCryptfs was mounted without "
1663 "xattr support enabled. eCryptfs will not treat "
1664 "this like an encrypted file.\n");
1665 rc = -EINVAL;
1666 }
Michael Halcrow237fead2006-10-04 02:16:22 -07001667 }
1668out:
1669 if (page_virt) {
1670 memset(page_virt, 0, PAGE_CACHE_SIZE);
1671 kmem_cache_free(ecryptfs_header_cache_1, page_virt);
1672 }
1673 return rc;
1674}
1675
1676/**
1677 * ecryptfs_encode_filename - converts a plaintext file name to cipher text
1678 * @crypt_stat: The crypt_stat struct associated with the file anem to encode
1679 * @name: The plaintext name
1680 * @length: The length of the plaintext
1681 * @encoded_name: The encypted name
1682 *
1683 * Encrypts and encodes a filename into something that constitutes a
1684 * valid filename for a filesystem, with printable characters.
1685 *
1686 * We assume that we have a properly initialized crypto context,
1687 * pointed to by crypt_stat->tfm.
1688 *
1689 * TODO: Implement filename decoding and decryption here, in place of
1690 * memcpy. We are keeping the framework around for now to (1)
1691 * facilitate testing of the components needed to implement filename
1692 * encryption and (2) to provide a code base from which other
1693 * developers in the community can easily implement this feature.
1694 *
1695 * Returns the length of encoded filename; negative if error
1696 */
1697int
1698ecryptfs_encode_filename(struct ecryptfs_crypt_stat *crypt_stat,
1699 const char *name, int length, char **encoded_name)
1700{
1701 int error = 0;
1702
1703 (*encoded_name) = kmalloc(length + 2, GFP_KERNEL);
1704 if (!(*encoded_name)) {
1705 error = -ENOMEM;
1706 goto out;
1707 }
1708 /* TODO: Filename encryption is a scheduled feature for a
1709 * future version of eCryptfs. This function is here only for
1710 * the purpose of providing a framework for other developers
1711 * to easily implement filename encryption. Hint: Replace this
1712 * memcpy() with a call to encrypt and encode the
1713 * filename, the set the length accordingly. */
1714 memcpy((void *)(*encoded_name), (void *)name, length);
1715 (*encoded_name)[length] = '\0';
1716 error = length + 1;
1717out:
1718 return error;
1719}
1720
1721/**
1722 * ecryptfs_decode_filename - converts the cipher text name to plaintext
1723 * @crypt_stat: The crypt_stat struct associated with the file
1724 * @name: The filename in cipher text
1725 * @length: The length of the cipher text name
1726 * @decrypted_name: The plaintext name
1727 *
1728 * Decodes and decrypts the filename.
1729 *
1730 * We assume that we have a properly initialized crypto context,
1731 * pointed to by crypt_stat->tfm.
1732 *
1733 * TODO: Implement filename decoding and decryption here, in place of
1734 * memcpy. We are keeping the framework around for now to (1)
1735 * facilitate testing of the components needed to implement filename
1736 * encryption and (2) to provide a code base from which other
1737 * developers in the community can easily implement this feature.
1738 *
1739 * Returns the length of decoded filename; negative if error
1740 */
1741int
1742ecryptfs_decode_filename(struct ecryptfs_crypt_stat *crypt_stat,
1743 const char *name, int length, char **decrypted_name)
1744{
1745 int error = 0;
1746
1747 (*decrypted_name) = kmalloc(length + 2, GFP_KERNEL);
1748 if (!(*decrypted_name)) {
1749 error = -ENOMEM;
1750 goto out;
1751 }
1752 /* TODO: Filename encryption is a scheduled feature for a
1753 * future version of eCryptfs. This function is here only for
1754 * the purpose of providing a framework for other developers
1755 * to easily implement filename encryption. Hint: Replace this
1756 * memcpy() with a call to decode and decrypt the
1757 * filename, the set the length accordingly. */
1758 memcpy((void *)(*decrypted_name), (void *)name, length);
1759 (*decrypted_name)[length + 1] = '\0'; /* Only for convenience
1760 * in printing out the
1761 * string in debug
1762 * messages */
1763 error = length;
1764out:
1765 return error;
1766}
1767
1768/**
1769 * ecryptfs_process_cipher - Perform cipher initialization.
Michael Halcrow237fead2006-10-04 02:16:22 -07001770 * @key_tfm: Crypto context for key material, set by this function
Michael Halcrowe5d9cbd2006-10-30 22:07:16 -08001771 * @cipher_name: Name of the cipher
1772 * @key_size: Size of the key in bytes
Michael Halcrow237fead2006-10-04 02:16:22 -07001773 *
1774 * Returns zero on success. Any crypto_tfm structs allocated here
1775 * should be released by other functions, such as on a superblock put
1776 * event, regardless of whether this function succeeds for fails.
1777 */
1778int
Michael Halcrow8bba0662006-10-30 22:07:18 -08001779ecryptfs_process_cipher(struct crypto_blkcipher **key_tfm, char *cipher_name,
Michael Halcrowe5d9cbd2006-10-30 22:07:16 -08001780 size_t *key_size)
Michael Halcrow237fead2006-10-04 02:16:22 -07001781{
1782 char dummy_key[ECRYPTFS_MAX_KEY_BYTES];
Michael Halcrow8bba0662006-10-30 22:07:18 -08001783 char *full_alg_name;
Michael Halcrow237fead2006-10-04 02:16:22 -07001784 int rc;
1785
Michael Halcrowe5d9cbd2006-10-30 22:07:16 -08001786 *key_tfm = NULL;
1787 if (*key_size > ECRYPTFS_MAX_KEY_BYTES) {
Michael Halcrow237fead2006-10-04 02:16:22 -07001788 rc = -EINVAL;
1789 printk(KERN_ERR "Requested key size is [%Zd] bytes; maximum "
Michael Halcrowe5d9cbd2006-10-30 22:07:16 -08001790 "allowable is [%d]\n", *key_size, ECRYPTFS_MAX_KEY_BYTES);
Michael Halcrow237fead2006-10-04 02:16:22 -07001791 goto out;
1792 }
Michael Halcrow8bba0662006-10-30 22:07:18 -08001793 rc = ecryptfs_crypto_api_algify_cipher_name(&full_alg_name, cipher_name,
1794 "ecb");
1795 if (rc)
1796 goto out;
1797 *key_tfm = crypto_alloc_blkcipher(full_alg_name, 0, CRYPTO_ALG_ASYNC);
1798 kfree(full_alg_name);
1799 if (IS_ERR(*key_tfm)) {
1800 rc = PTR_ERR(*key_tfm);
Michael Halcrow237fead2006-10-04 02:16:22 -07001801 printk(KERN_ERR "Unable to allocate crypto cipher with name "
Michael Halcrow8bba0662006-10-30 22:07:18 -08001802 "[%s]; rc = [%d]\n", cipher_name, rc);
Michael Halcrow237fead2006-10-04 02:16:22 -07001803 goto out;
1804 }
Michael Halcrow8bba0662006-10-30 22:07:18 -08001805 crypto_blkcipher_set_flags(*key_tfm, CRYPTO_TFM_REQ_WEAK_KEY);
1806 if (*key_size == 0) {
1807 struct blkcipher_alg *alg = crypto_blkcipher_alg(*key_tfm);
1808
1809 *key_size = alg->max_keysize;
1810 }
Michael Halcrowe5d9cbd2006-10-30 22:07:16 -08001811 get_random_bytes(dummy_key, *key_size);
Michael Halcrow8bba0662006-10-30 22:07:18 -08001812 rc = crypto_blkcipher_setkey(*key_tfm, dummy_key, *key_size);
Michael Halcrow237fead2006-10-04 02:16:22 -07001813 if (rc) {
1814 printk(KERN_ERR "Error attempting to set key of size [%Zd] for "
Michael Halcrowe5d9cbd2006-10-30 22:07:16 -08001815 "cipher [%s]; rc = [%d]\n", *key_size, cipher_name, rc);
Michael Halcrow237fead2006-10-04 02:16:22 -07001816 rc = -EINVAL;
1817 goto out;
1818 }
1819out:
1820 return rc;
1821}