blob: ec640ebcdea8ed42f7dd699eee84902885e71f30 [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,
Tyler Hicks28916d12013-04-15 16:37:27 -070042 struct page *dst_page, struct page *src_page,
43 int offset, int size, unsigned char *iv);
Michael Halcrow237fead2006-10-04 02:16:22 -070044static int
45ecryptfs_encrypt_page_offset(struct ecryptfs_crypt_stat *crypt_stat,
Tyler Hicks28916d12013-04-15 16:37:27 -070046 struct page *dst_page, struct page *src_page,
47 int offset, int size, unsigned char *iv);
Michael Halcrow237fead2006-10-04 02:16:22 -070048
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 Halcrow565d97242006-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 Halcrow565d97242006-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 Halcrow565d97242006-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 Halcrow565d97242006-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 Halcrow565d97242006-10-30 22:07:17 -0800116 crypt_stat->hash_tfm = desc.tfm;
Michael Halcrow237fead2006-10-04 02:16:22 -0700117 }
Michael Halcrow8a29f2b2007-11-05 14:51:04 -0800118 rc = crypto_hash_init(&desc);
119 if (rc) {
120 printk(KERN_ERR
121 "%s: Error initializing crypto hash; rc = [%d]\n",
Harvey Harrison18d1dbf2008-04-29 00:59:48 -0700122 __func__, rc);
Michael Halcrow8a29f2b2007-11-05 14:51:04 -0800123 goto out;
124 }
125 rc = crypto_hash_update(&desc, &sg, len);
126 if (rc) {
127 printk(KERN_ERR
128 "%s: Error updating crypto hash; rc = [%d]\n",
Harvey Harrison18d1dbf2008-04-29 00:59:48 -0700129 __func__, rc);
Michael Halcrow8a29f2b2007-11-05 14:51:04 -0800130 goto out;
131 }
132 rc = crypto_hash_final(&desc, dst);
133 if (rc) {
134 printk(KERN_ERR
135 "%s: Error finalizing crypto hash; rc = [%d]\n",
Harvey Harrison18d1dbf2008-04-29 00:59:48 -0700136 __func__, rc);
Michael Halcrow8a29f2b2007-11-05 14:51:04 -0800137 goto out;
138 }
Michael Halcrow237fead2006-10-04 02:16:22 -0700139out:
Michael Halcrow8a29f2b2007-11-05 14:51:04 -0800140 mutex_unlock(&crypt_stat->cs_hash_tfm_mutex);
Michael Halcrow237fead2006-10-04 02:16:22 -0700141 return rc;
142}
143
Michael Halcrowcd9d67d2007-10-16 01:28:04 -0700144static int ecryptfs_crypto_api_algify_cipher_name(char **algified_name,
145 char *cipher_name,
146 char *chaining_modifier)
Michael Halcrow8bba0662006-10-30 22:07:18 -0800147{
148 int cipher_name_len = strlen(cipher_name);
149 int chaining_modifier_len = strlen(chaining_modifier);
150 int algified_name_len;
151 int rc;
152
153 algified_name_len = (chaining_modifier_len + cipher_name_len + 3);
154 (*algified_name) = kmalloc(algified_name_len, GFP_KERNEL);
Michael Halcrow7bd473f2006-11-02 22:06:56 -0800155 if (!(*algified_name)) {
Michael Halcrow8bba0662006-10-30 22:07:18 -0800156 rc = -ENOMEM;
157 goto out;
158 }
159 snprintf((*algified_name), algified_name_len, "%s(%s)",
160 chaining_modifier, cipher_name);
161 rc = 0;
162out:
163 return rc;
164}
165
Michael Halcrow237fead2006-10-04 02:16:22 -0700166/**
167 * ecryptfs_derive_iv
168 * @iv: destination for the derived iv vale
169 * @crypt_stat: Pointer to crypt_stat struct for the current inode
Michael Halcrowd6a13c12007-10-16 01:28:12 -0700170 * @offset: Offset of the extent whose IV we are to derive
Michael Halcrow237fead2006-10-04 02:16:22 -0700171 *
172 * Generate the initialization vector from the given root IV and page
173 * offset.
174 *
175 * Returns zero on success; non-zero on error.
176 */
Michael Halcrowa34f60f2009-01-06 14:41:58 -0800177int ecryptfs_derive_iv(char *iv, struct ecryptfs_crypt_stat *crypt_stat,
178 loff_t offset)
Michael Halcrow237fead2006-10-04 02:16:22 -0700179{
180 int rc = 0;
181 char dst[MD5_DIGEST_SIZE];
182 char src[ECRYPTFS_MAX_IV_BYTES + 16];
183
184 if (unlikely(ecryptfs_verbosity > 0)) {
185 ecryptfs_printk(KERN_DEBUG, "root iv:\n");
186 ecryptfs_dump_hex(crypt_stat->root_iv, crypt_stat->iv_bytes);
187 }
188 /* TODO: It is probably secure to just cast the least
189 * significant bits of the root IV into an unsigned long and
190 * add the offset to that rather than go through all this
191 * hashing business. -Halcrow */
192 memcpy(src, crypt_stat->root_iv, crypt_stat->iv_bytes);
193 memset((src + crypt_stat->iv_bytes), 0, 16);
Michael Halcrowd6a13c12007-10-16 01:28:12 -0700194 snprintf((src + crypt_stat->iv_bytes), 16, "%lld", offset);
Michael Halcrow237fead2006-10-04 02:16:22 -0700195 if (unlikely(ecryptfs_verbosity > 0)) {
196 ecryptfs_printk(KERN_DEBUG, "source:\n");
197 ecryptfs_dump_hex(src, (crypt_stat->iv_bytes + 16));
198 }
199 rc = ecryptfs_calculate_md5(dst, crypt_stat, src,
200 (crypt_stat->iv_bytes + 16));
201 if (rc) {
202 ecryptfs_printk(KERN_WARNING, "Error attempting to compute "
203 "MD5 while generating IV for a page\n");
204 goto out;
205 }
206 memcpy(iv, dst, crypt_stat->iv_bytes);
207 if (unlikely(ecryptfs_verbosity > 0)) {
208 ecryptfs_printk(KERN_DEBUG, "derived iv:\n");
209 ecryptfs_dump_hex(iv, crypt_stat->iv_bytes);
210 }
211out:
212 return rc;
213}
214
215/**
216 * ecryptfs_init_crypt_stat
217 * @crypt_stat: Pointer to the crypt_stat struct to initialize.
218 *
219 * Initialize the crypt_stat structure.
220 */
221void
222ecryptfs_init_crypt_stat(struct ecryptfs_crypt_stat *crypt_stat)
223{
224 memset((void *)crypt_stat, 0, sizeof(struct ecryptfs_crypt_stat));
Michael Halcrowf4aad162007-10-16 01:27:53 -0700225 INIT_LIST_HEAD(&crypt_stat->keysig_list);
226 mutex_init(&crypt_stat->keysig_list_mutex);
Michael Halcrow237fead2006-10-04 02:16:22 -0700227 mutex_init(&crypt_stat->cs_mutex);
228 mutex_init(&crypt_stat->cs_tfm_mutex);
Michael Halcrow565d97242006-10-30 22:07:17 -0800229 mutex_init(&crypt_stat->cs_hash_tfm_mutex);
Michael Halcrowe2bd99e2007-02-12 00:53:49 -0800230 crypt_stat->flags |= ECRYPTFS_STRUCT_INITIALIZED;
Michael Halcrow237fead2006-10-04 02:16:22 -0700231}
232
233/**
Michael Halcrowfcd12832007-10-16 01:28:01 -0700234 * ecryptfs_destroy_crypt_stat
Michael Halcrow237fead2006-10-04 02:16:22 -0700235 * @crypt_stat: Pointer to the crypt_stat struct to initialize.
236 *
237 * Releases all memory associated with a crypt_stat struct.
238 */
Michael Halcrowfcd12832007-10-16 01:28:01 -0700239void ecryptfs_destroy_crypt_stat(struct ecryptfs_crypt_stat *crypt_stat)
Michael Halcrow237fead2006-10-04 02:16:22 -0700240{
Michael Halcrowf4aad162007-10-16 01:27:53 -0700241 struct ecryptfs_key_sig *key_sig, *key_sig_tmp;
242
Michael Halcrow237fead2006-10-04 02:16:22 -0700243 if (crypt_stat->tfm)
Tyler Hicks4dfea4f2013-05-09 16:55:07 -0700244 crypto_free_ablkcipher(crypt_stat->tfm);
Michael Halcrow565d97242006-10-30 22:07:17 -0800245 if (crypt_stat->hash_tfm)
246 crypto_free_hash(crypt_stat->hash_tfm);
Michael Halcrowf4aad162007-10-16 01:27:53 -0700247 list_for_each_entry_safe(key_sig, key_sig_tmp,
248 &crypt_stat->keysig_list, crypt_stat_list) {
249 list_del(&key_sig->crypt_stat_list);
250 kmem_cache_free(ecryptfs_key_sig_cache, key_sig);
251 }
Michael Halcrow237fead2006-10-04 02:16:22 -0700252 memset(crypt_stat, 0, sizeof(struct ecryptfs_crypt_stat));
253}
254
Michael Halcrowfcd12832007-10-16 01:28:01 -0700255void ecryptfs_destroy_mount_crypt_stat(
Michael Halcrow237fead2006-10-04 02:16:22 -0700256 struct ecryptfs_mount_crypt_stat *mount_crypt_stat)
257{
Michael Halcrowf4aad162007-10-16 01:27:53 -0700258 struct ecryptfs_global_auth_tok *auth_tok, *auth_tok_tmp;
259
260 if (!(mount_crypt_stat->flags & ECRYPTFS_MOUNT_CRYPT_STAT_INITIALIZED))
261 return;
262 mutex_lock(&mount_crypt_stat->global_auth_tok_list_mutex);
263 list_for_each_entry_safe(auth_tok, auth_tok_tmp,
264 &mount_crypt_stat->global_auth_tok_list,
265 mount_crypt_stat_list) {
266 list_del(&auth_tok->mount_crypt_stat_list);
Michael Halcrowf4aad162007-10-16 01:27:53 -0700267 if (auth_tok->global_auth_tok_key
268 && !(auth_tok->flags & ECRYPTFS_AUTH_TOK_INVALID))
269 key_put(auth_tok->global_auth_tok_key);
270 kmem_cache_free(ecryptfs_global_auth_tok_cache, auth_tok);
271 }
272 mutex_unlock(&mount_crypt_stat->global_auth_tok_list_mutex);
Michael Halcrow237fead2006-10-04 02:16:22 -0700273 memset(mount_crypt_stat, 0, sizeof(struct ecryptfs_mount_crypt_stat));
274}
275
276/**
277 * virt_to_scatterlist
278 * @addr: Virtual address
279 * @size: Size of data; should be an even multiple of the block size
280 * @sg: Pointer to scatterlist array; set to NULL to obtain only
281 * the number of scatterlist structs required in array
282 * @sg_size: Max array size
283 *
284 * Fills in a scatterlist array with page references for a passed
285 * virtual address.
286 *
287 * Returns the number of scatterlist structs in array used
288 */
289int virt_to_scatterlist(const void *addr, int size, struct scatterlist *sg,
290 int sg_size)
291{
292 int i = 0;
293 struct page *pg;
294 int offset;
295 int remainder_of_page;
296
Herbert Xu68e3f5d2007-10-27 00:52:07 -0700297 sg_init_table(sg, sg_size);
298
Michael Halcrow237fead2006-10-04 02:16:22 -0700299 while (size > 0 && i < sg_size) {
300 pg = virt_to_page(addr);
301 offset = offset_in_page(addr);
Dan Carpentera07c48a2013-01-26 10:48:42 +0300302 sg_set_page(&sg[i], pg, 0, offset);
Michael Halcrow237fead2006-10-04 02:16:22 -0700303 remainder_of_page = PAGE_CACHE_SIZE - offset;
304 if (size >= remainder_of_page) {
Dan Carpentera07c48a2013-01-26 10:48:42 +0300305 sg[i].length = remainder_of_page;
Michael Halcrow237fead2006-10-04 02:16:22 -0700306 addr += remainder_of_page;
307 size -= remainder_of_page;
308 } else {
Dan Carpentera07c48a2013-01-26 10:48:42 +0300309 sg[i].length = size;
Michael Halcrow237fead2006-10-04 02:16:22 -0700310 addr += size;
311 size = 0;
312 }
313 i++;
314 }
315 if (size > 0)
316 return -ENOMEM;
317 return i;
318}
319
Tyler Hicks4dfea4f2013-05-09 16:55:07 -0700320struct extent_crypt_result {
321 struct completion completion;
322 int rc;
323};
324
325static void extent_crypt_complete(struct crypto_async_request *req, int rc)
326{
327 struct extent_crypt_result *ecr = req->data;
328
329 if (rc == -EINPROGRESS)
330 return;
331
332 ecr->rc = rc;
333 complete(&ecr->completion);
334}
335
Michael Halcrow237fead2006-10-04 02:16:22 -0700336/**
337 * encrypt_scatterlist
338 * @crypt_stat: Pointer to the crypt_stat struct to initialize.
339 * @dest_sg: Destination of encrypted data
340 * @src_sg: Data to be encrypted
341 * @size: Length of data to be encrypted
342 * @iv: iv to use during encryption
343 *
344 * Returns the number of bytes encrypted; negative value on error
345 */
346static int encrypt_scatterlist(struct ecryptfs_crypt_stat *crypt_stat,
347 struct scatterlist *dest_sg,
348 struct scatterlist *src_sg, int size,
349 unsigned char *iv)
350{
Tyler Hicks4dfea4f2013-05-09 16:55:07 -0700351 struct ablkcipher_request *req = NULL;
352 struct extent_crypt_result ecr;
Michael Halcrow237fead2006-10-04 02:16:22 -0700353 int rc = 0;
354
355 BUG_ON(!crypt_stat || !crypt_stat->tfm
Michael Halcrowe2bd99e2007-02-12 00:53:49 -0800356 || !(crypt_stat->flags & ECRYPTFS_STRUCT_INITIALIZED));
Michael Halcrow237fead2006-10-04 02:16:22 -0700357 if (unlikely(ecryptfs_verbosity > 0)) {
Tyler Hicksf24b3882010-11-15 17:36:38 -0600358 ecryptfs_printk(KERN_DEBUG, "Key size [%zd]; key:\n",
Michael Halcrow237fead2006-10-04 02:16:22 -0700359 crypt_stat->key_size);
360 ecryptfs_dump_hex(crypt_stat->key,
361 crypt_stat->key_size);
362 }
Tyler Hicks4dfea4f2013-05-09 16:55:07 -0700363
364 init_completion(&ecr.completion);
365
Michael Halcrow237fead2006-10-04 02:16:22 -0700366 mutex_lock(&crypt_stat->cs_tfm_mutex);
Tyler Hicks4dfea4f2013-05-09 16:55:07 -0700367 req = ablkcipher_request_alloc(crypt_stat->tfm, GFP_NOFS);
368 if (!req) {
Michael Halcrow237fead2006-10-04 02:16:22 -0700369 mutex_unlock(&crypt_stat->cs_tfm_mutex);
Tyler Hicks4dfea4f2013-05-09 16:55:07 -0700370 rc = -ENOMEM;
Michael Halcrow237fead2006-10-04 02:16:22 -0700371 goto out;
372 }
Tyler Hicks4dfea4f2013-05-09 16:55:07 -0700373
374 ablkcipher_request_set_callback(req,
375 CRYPTO_TFM_REQ_MAY_BACKLOG | CRYPTO_TFM_REQ_MAY_SLEEP,
376 extent_crypt_complete, &ecr);
377 /* Consider doing this once, when the file is opened */
378 if (!(crypt_stat->flags & ECRYPTFS_KEY_SET)) {
379 rc = crypto_ablkcipher_setkey(crypt_stat->tfm, crypt_stat->key,
380 crypt_stat->key_size);
381 if (rc) {
382 ecryptfs_printk(KERN_ERR,
383 "Error setting key; rc = [%d]\n",
384 rc);
385 mutex_unlock(&crypt_stat->cs_tfm_mutex);
386 rc = -EINVAL;
387 goto out;
388 }
389 crypt_stat->flags |= ECRYPTFS_KEY_SET;
390 }
Michael Halcrow237fead2006-10-04 02:16:22 -0700391 mutex_unlock(&crypt_stat->cs_tfm_mutex);
Tyler Hicks4dfea4f2013-05-09 16:55:07 -0700392 ecryptfs_printk(KERN_DEBUG, "Encrypting [%d] bytes.\n", size);
393 ablkcipher_request_set_crypt(req, src_sg, dest_sg, size, iv);
394 rc = crypto_ablkcipher_encrypt(req);
395 if (rc == -EINPROGRESS || rc == -EBUSY) {
396 struct extent_crypt_result *ecr = req->base.data;
397
398 wait_for_completion(&ecr->completion);
399 rc = ecr->rc;
400 INIT_COMPLETION(ecr->completion);
401 }
Michael Halcrow237fead2006-10-04 02:16:22 -0700402out:
Tyler Hicks4dfea4f2013-05-09 16:55:07 -0700403 ablkcipher_request_free(req);
Michael Halcrow237fead2006-10-04 02:16:22 -0700404 return rc;
405}
406
Michael Halcrow237fead2006-10-04 02:16:22 -0700407/**
Tyler Hicks24d15262013-04-15 17:28:09 -0700408 * lower_offset_for_page
Michael Halcrow0216f7f2007-10-16 01:28:08 -0700409 *
410 * Convert an eCryptfs page index into a lower byte offset
411 */
Tyler Hicks24d15262013-04-15 17:28:09 -0700412static loff_t lower_offset_for_page(struct ecryptfs_crypt_stat *crypt_stat,
413 struct page *page)
Michael Halcrow0216f7f2007-10-16 01:28:08 -0700414{
Tyler Hicks24d15262013-04-15 17:28:09 -0700415 return ecryptfs_lower_header_size(crypt_stat) +
416 (page->index << PAGE_CACHE_SHIFT);
Michael Halcrow0216f7f2007-10-16 01:28:08 -0700417}
418
419/**
420 * ecryptfs_encrypt_extent
421 * @enc_extent_page: Allocated page into which to encrypt the data in
422 * @page
423 * @crypt_stat: crypt_stat containing cryptographic context for the
424 * encryption operation
425 * @page: Page containing plaintext data extent to encrypt
426 * @extent_offset: Page extent offset for use in generating IV
427 *
428 * Encrypts one extent of data.
429 *
430 * Return zero on success; non-zero otherwise
431 */
432static int ecryptfs_encrypt_extent(struct page *enc_extent_page,
433 struct ecryptfs_crypt_stat *crypt_stat,
434 struct page *page,
435 unsigned long extent_offset)
436{
Michael Halcrowd6a13c12007-10-16 01:28:12 -0700437 loff_t extent_base;
Michael Halcrow0216f7f2007-10-16 01:28:08 -0700438 char extent_iv[ECRYPTFS_MAX_IV_BYTES];
439 int rc;
440
Michael Halcrowd6a13c12007-10-16 01:28:12 -0700441 extent_base = (((loff_t)page->index)
Michael Halcrow0216f7f2007-10-16 01:28:08 -0700442 * (PAGE_CACHE_SIZE / crypt_stat->extent_size));
443 rc = ecryptfs_derive_iv(extent_iv, crypt_stat,
444 (extent_base + extent_offset));
445 if (rc) {
Joe Perches888d57b2010-11-10 15:46:16 -0800446 ecryptfs_printk(KERN_ERR, "Error attempting to derive IV for "
447 "extent [0x%.16llx]; rc = [%d]\n",
448 (unsigned long long)(extent_base + extent_offset), rc);
Michael Halcrow0216f7f2007-10-16 01:28:08 -0700449 goto out;
450 }
Tyler Hicks28916d12013-04-15 16:37:27 -0700451 rc = ecryptfs_encrypt_page_offset(crypt_stat, enc_extent_page, page,
Tyler Hicks12003e52013-04-16 23:21:24 -0700452 extent_offset * crypt_stat->extent_size,
453 crypt_stat->extent_size, extent_iv);
Michael Halcrow0216f7f2007-10-16 01:28:08 -0700454 if (rc < 0) {
455 printk(KERN_ERR "%s: Error attempting to encrypt page with "
456 "page->index = [%ld], extent_offset = [%ld]; "
Harvey Harrison18d1dbf2008-04-29 00:59:48 -0700457 "rc = [%d]\n", __func__, page->index, extent_offset,
Michael Halcrow0216f7f2007-10-16 01:28:08 -0700458 rc);
459 goto out;
460 }
461 rc = 0;
Michael Halcrow0216f7f2007-10-16 01:28:08 -0700462out:
463 return rc;
464}
465
466/**
Michael Halcrow237fead2006-10-04 02:16:22 -0700467 * ecryptfs_encrypt_page
Michael Halcrow0216f7f2007-10-16 01:28:08 -0700468 * @page: Page mapped from the eCryptfs inode for the file; contains
469 * decrypted content that needs to be encrypted (to a temporary
470 * page; not in place) and written out to the lower file
Michael Halcrow237fead2006-10-04 02:16:22 -0700471 *
472 * Encrypt an eCryptfs page. This is done on a per-extent basis. Note
473 * that eCryptfs pages may straddle the lower pages -- for instance,
474 * if the file was created on a machine with an 8K page size
475 * (resulting in an 8K header), and then the file is copied onto a
476 * host with a 32K page size, then when reading page 0 of the eCryptfs
477 * file, 24K of page 0 of the lower file will be read and decrypted,
478 * and then 8K of page 1 of the lower file will be read and decrypted.
479 *
Michael Halcrow237fead2006-10-04 02:16:22 -0700480 * Returns zero on success; negative on error
481 */
Michael Halcrow0216f7f2007-10-16 01:28:08 -0700482int ecryptfs_encrypt_page(struct page *page)
Michael Halcrow237fead2006-10-04 02:16:22 -0700483{
Michael Halcrow0216f7f2007-10-16 01:28:08 -0700484 struct inode *ecryptfs_inode;
Michael Halcrow237fead2006-10-04 02:16:22 -0700485 struct ecryptfs_crypt_stat *crypt_stat;
Eric Sandeen7fcba052008-07-28 15:46:39 -0700486 char *enc_extent_virt;
487 struct page *enc_extent_page = NULL;
Michael Halcrow0216f7f2007-10-16 01:28:08 -0700488 loff_t extent_offset;
Tyler Hicks0f896172013-04-15 16:16:24 -0700489 loff_t lower_offset;
Michael Halcrow237fead2006-10-04 02:16:22 -0700490 int rc = 0;
Michael Halcrow237fead2006-10-04 02:16:22 -0700491
Michael Halcrow0216f7f2007-10-16 01:28:08 -0700492 ecryptfs_inode = page->mapping->host;
493 crypt_stat =
494 &(ecryptfs_inode_to_private(ecryptfs_inode)->crypt_stat);
Tyler Hicks13a791b2009-04-13 15:29:27 -0500495 BUG_ON(!(crypt_stat->flags & ECRYPTFS_ENCRYPTED));
Eric Sandeen7fcba052008-07-28 15:46:39 -0700496 enc_extent_page = alloc_page(GFP_USER);
497 if (!enc_extent_page) {
Michael Halcrow0216f7f2007-10-16 01:28:08 -0700498 rc = -ENOMEM;
499 ecryptfs_printk(KERN_ERR, "Error allocating memory for "
500 "encrypted extent\n");
501 goto out;
502 }
Tyler Hicks0f896172013-04-15 16:16:24 -0700503
Michael Halcrow0216f7f2007-10-16 01:28:08 -0700504 for (extent_offset = 0;
505 extent_offset < (PAGE_CACHE_SIZE / crypt_stat->extent_size);
506 extent_offset++) {
Michael Halcrow0216f7f2007-10-16 01:28:08 -0700507 rc = ecryptfs_encrypt_extent(enc_extent_page, crypt_stat, page,
508 extent_offset);
Michael Halcrow237fead2006-10-04 02:16:22 -0700509 if (rc) {
Michael Halcrow0216f7f2007-10-16 01:28:08 -0700510 printk(KERN_ERR "%s: Error encrypting extent; "
Harvey Harrison18d1dbf2008-04-29 00:59:48 -0700511 "rc = [%d]\n", __func__, rc);
Michael Halcrow237fead2006-10-04 02:16:22 -0700512 goto out;
513 }
Tyler Hicks0f896172013-04-15 16:16:24 -0700514 }
515
Tyler Hicks24d15262013-04-15 17:28:09 -0700516 lower_offset = lower_offset_for_page(crypt_stat, page);
Tyler Hicks0f896172013-04-15 16:16:24 -0700517 enc_extent_virt = kmap(enc_extent_page);
518 rc = ecryptfs_write_lower(ecryptfs_inode, enc_extent_virt, lower_offset,
519 PAGE_CACHE_SIZE);
520 kunmap(enc_extent_page);
521 if (rc < 0) {
522 ecryptfs_printk(KERN_ERR,
523 "Error attempting to write lower page; rc = [%d]\n",
524 rc);
525 goto out;
Michael Halcrow237fead2006-10-04 02:16:22 -0700526 }
Tyler Hicks96a7b9c2009-09-16 19:04:20 -0500527 rc = 0;
Michael Halcrow0216f7f2007-10-16 01:28:08 -0700528out:
Eric Sandeen7fcba052008-07-28 15:46:39 -0700529 if (enc_extent_page) {
Eric Sandeen7fcba052008-07-28 15:46:39 -0700530 __free_page(enc_extent_page);
531 }
Michael Halcrow0216f7f2007-10-16 01:28:08 -0700532 return rc;
533}
534
535static int ecryptfs_decrypt_extent(struct page *page,
536 struct ecryptfs_crypt_stat *crypt_stat,
537 struct page *enc_extent_page,
538 unsigned long extent_offset)
539{
Michael Halcrowd6a13c12007-10-16 01:28:12 -0700540 loff_t extent_base;
Michael Halcrow0216f7f2007-10-16 01:28:08 -0700541 char extent_iv[ECRYPTFS_MAX_IV_BYTES];
542 int rc;
543
Michael Halcrowd6a13c12007-10-16 01:28:12 -0700544 extent_base = (((loff_t)page->index)
Michael Halcrow0216f7f2007-10-16 01:28:08 -0700545 * (PAGE_CACHE_SIZE / crypt_stat->extent_size));
546 rc = ecryptfs_derive_iv(extent_iv, crypt_stat,
547 (extent_base + extent_offset));
Michael Halcrow237fead2006-10-04 02:16:22 -0700548 if (rc) {
Joe Perches888d57b2010-11-10 15:46:16 -0800549 ecryptfs_printk(KERN_ERR, "Error attempting to derive IV for "
550 "extent [0x%.16llx]; rc = [%d]\n",
551 (unsigned long long)(extent_base + extent_offset), rc);
Michael Halcrow0216f7f2007-10-16 01:28:08 -0700552 goto out;
553 }
Tyler Hicks28916d12013-04-15 16:37:27 -0700554 rc = ecryptfs_decrypt_page_offset(crypt_stat, page, enc_extent_page,
Tyler Hicks12003e52013-04-16 23:21:24 -0700555 extent_offset * crypt_stat->extent_size,
556 crypt_stat->extent_size, extent_iv);
Michael Halcrow0216f7f2007-10-16 01:28:08 -0700557 if (rc < 0) {
558 printk(KERN_ERR "%s: Error attempting to decrypt to page with "
559 "page->index = [%ld], extent_offset = [%ld]; "
Harvey Harrison18d1dbf2008-04-29 00:59:48 -0700560 "rc = [%d]\n", __func__, page->index, extent_offset,
Michael Halcrow0216f7f2007-10-16 01:28:08 -0700561 rc);
562 goto out;
563 }
564 rc = 0;
Michael Halcrow237fead2006-10-04 02:16:22 -0700565out:
566 return rc;
567}
568
569/**
570 * ecryptfs_decrypt_page
Michael Halcrow0216f7f2007-10-16 01:28:08 -0700571 * @page: Page mapped from the eCryptfs inode for the file; data read
572 * and decrypted from the lower file will be written into this
573 * page
Michael Halcrow237fead2006-10-04 02:16:22 -0700574 *
575 * Decrypt an eCryptfs page. This is done on a per-extent basis. Note
576 * that eCryptfs pages may straddle the lower pages -- for instance,
577 * if the file was created on a machine with an 8K page size
578 * (resulting in an 8K header), and then the file is copied onto a
579 * host with a 32K page size, then when reading page 0 of the eCryptfs
580 * file, 24K of page 0 of the lower file will be read and decrypted,
581 * and then 8K of page 1 of the lower file will be read and decrypted.
582 *
583 * Returns zero on success; negative on error
584 */
Michael Halcrow0216f7f2007-10-16 01:28:08 -0700585int ecryptfs_decrypt_page(struct page *page)
Michael Halcrow237fead2006-10-04 02:16:22 -0700586{
Michael Halcrow0216f7f2007-10-16 01:28:08 -0700587 struct inode *ecryptfs_inode;
Michael Halcrow237fead2006-10-04 02:16:22 -0700588 struct ecryptfs_crypt_stat *crypt_stat;
Eric Sandeen7fcba052008-07-28 15:46:39 -0700589 char *enc_extent_virt;
590 struct page *enc_extent_page = NULL;
Michael Halcrow0216f7f2007-10-16 01:28:08 -0700591 unsigned long extent_offset;
Tyler Hicks0f896172013-04-15 16:16:24 -0700592 loff_t lower_offset;
Michael Halcrow237fead2006-10-04 02:16:22 -0700593 int rc = 0;
Michael Halcrow237fead2006-10-04 02:16:22 -0700594
Michael Halcrow0216f7f2007-10-16 01:28:08 -0700595 ecryptfs_inode = page->mapping->host;
596 crypt_stat =
597 &(ecryptfs_inode_to_private(ecryptfs_inode)->crypt_stat);
Tyler Hicks13a791b2009-04-13 15:29:27 -0500598 BUG_ON(!(crypt_stat->flags & ECRYPTFS_ENCRYPTED));
Eric Sandeen7fcba052008-07-28 15:46:39 -0700599 enc_extent_page = alloc_page(GFP_USER);
600 if (!enc_extent_page) {
Michael Halcrow237fead2006-10-04 02:16:22 -0700601 rc = -ENOMEM;
Michael Halcrow0216f7f2007-10-16 01:28:08 -0700602 ecryptfs_printk(KERN_ERR, "Error allocating memory for "
603 "encrypted extent\n");
Michael Halcrow16a72c42007-10-16 01:28:14 -0700604 goto out;
Michael Halcrow237fead2006-10-04 02:16:22 -0700605 }
Tyler Hicks0f896172013-04-15 16:16:24 -0700606
Tyler Hicks24d15262013-04-15 17:28:09 -0700607 lower_offset = lower_offset_for_page(crypt_stat, page);
Eric Sandeen7fcba052008-07-28 15:46:39 -0700608 enc_extent_virt = kmap(enc_extent_page);
Tyler Hicks0f896172013-04-15 16:16:24 -0700609 rc = ecryptfs_read_lower(enc_extent_virt, lower_offset, PAGE_CACHE_SIZE,
610 ecryptfs_inode);
611 kunmap(enc_extent_page);
612 if (rc < 0) {
613 ecryptfs_printk(KERN_ERR,
614 "Error attempting to read lower page; rc = [%d]\n",
615 rc);
616 goto out;
617 }
618
Michael Halcrow0216f7f2007-10-16 01:28:08 -0700619 for (extent_offset = 0;
620 extent_offset < (PAGE_CACHE_SIZE / crypt_stat->extent_size);
621 extent_offset++) {
Michael Halcrow0216f7f2007-10-16 01:28:08 -0700622 rc = ecryptfs_decrypt_extent(page, crypt_stat, enc_extent_page,
623 extent_offset);
624 if (rc) {
625 printk(KERN_ERR "%s: Error encrypting extent; "
Harvey Harrison18d1dbf2008-04-29 00:59:48 -0700626 "rc = [%d]\n", __func__, rc);
Michael Halcrow16a72c42007-10-16 01:28:14 -0700627 goto out;
Michael Halcrow237fead2006-10-04 02:16:22 -0700628 }
Michael Halcrow237fead2006-10-04 02:16:22 -0700629 }
630out:
Eric Sandeen7fcba052008-07-28 15:46:39 -0700631 if (enc_extent_page) {
Eric Sandeen7fcba052008-07-28 15:46:39 -0700632 __free_page(enc_extent_page);
633 }
Michael Halcrow237fead2006-10-04 02:16:22 -0700634 return rc;
635}
636
637/**
638 * decrypt_scatterlist
Michael Halcrow22e78fa2007-10-16 01:28:02 -0700639 * @crypt_stat: Cryptographic context
640 * @dest_sg: The destination scatterlist to decrypt into
641 * @src_sg: The source scatterlist to decrypt from
642 * @size: The number of bytes to decrypt
643 * @iv: The initialization vector to use for the decryption
Michael Halcrow237fead2006-10-04 02:16:22 -0700644 *
645 * Returns the number of bytes decrypted; negative value on error
646 */
647static int decrypt_scatterlist(struct ecryptfs_crypt_stat *crypt_stat,
648 struct scatterlist *dest_sg,
649 struct scatterlist *src_sg, int size,
650 unsigned char *iv)
651{
Tyler Hicks4dfea4f2013-05-09 16:55:07 -0700652 struct ablkcipher_request *req = NULL;
653 struct extent_crypt_result ecr;
Michael Halcrow237fead2006-10-04 02:16:22 -0700654 int rc = 0;
655
Tyler Hicks4dfea4f2013-05-09 16:55:07 -0700656 BUG_ON(!crypt_stat || !crypt_stat->tfm
657 || !(crypt_stat->flags & ECRYPTFS_STRUCT_INITIALIZED));
658 if (unlikely(ecryptfs_verbosity > 0)) {
659 ecryptfs_printk(KERN_DEBUG, "Key size [%zd]; key:\n",
660 crypt_stat->key_size);
661 ecryptfs_dump_hex(crypt_stat->key,
662 crypt_stat->key_size);
663 }
664
665 init_completion(&ecr.completion);
666
Michael Halcrow237fead2006-10-04 02:16:22 -0700667 mutex_lock(&crypt_stat->cs_tfm_mutex);
Tyler Hicks4dfea4f2013-05-09 16:55:07 -0700668 req = ablkcipher_request_alloc(crypt_stat->tfm, GFP_NOFS);
669 if (!req) {
Michael Halcrow237fead2006-10-04 02:16:22 -0700670 mutex_unlock(&crypt_stat->cs_tfm_mutex);
Tyler Hicks4dfea4f2013-05-09 16:55:07 -0700671 rc = -ENOMEM;
Michael Halcrow237fead2006-10-04 02:16:22 -0700672 goto out;
673 }
Tyler Hicks4dfea4f2013-05-09 16:55:07 -0700674
675 ablkcipher_request_set_callback(req,
676 CRYPTO_TFM_REQ_MAY_BACKLOG | CRYPTO_TFM_REQ_MAY_SLEEP,
677 extent_crypt_complete, &ecr);
678 /* Consider doing this once, when the file is opened */
679 if (!(crypt_stat->flags & ECRYPTFS_KEY_SET)) {
680 rc = crypto_ablkcipher_setkey(crypt_stat->tfm, crypt_stat->key,
681 crypt_stat->key_size);
682 if (rc) {
683 ecryptfs_printk(KERN_ERR,
684 "Error setting key; rc = [%d]\n",
685 rc);
686 mutex_unlock(&crypt_stat->cs_tfm_mutex);
687 rc = -EINVAL;
688 goto out;
689 }
690 crypt_stat->flags |= ECRYPTFS_KEY_SET;
691 }
Michael Halcrow237fead2006-10-04 02:16:22 -0700692 mutex_unlock(&crypt_stat->cs_tfm_mutex);
Tyler Hicks4dfea4f2013-05-09 16:55:07 -0700693 ecryptfs_printk(KERN_DEBUG, "Decrypting [%d] bytes.\n", size);
694 ablkcipher_request_set_crypt(req, src_sg, dest_sg, size, iv);
695 rc = crypto_ablkcipher_decrypt(req);
696 if (rc == -EINPROGRESS || rc == -EBUSY) {
697 struct extent_crypt_result *ecr = req->base.data;
698
699 wait_for_completion(&ecr->completion);
700 rc = ecr->rc;
701 INIT_COMPLETION(ecr->completion);
Michael Halcrow237fead2006-10-04 02:16:22 -0700702 }
Michael Halcrow237fead2006-10-04 02:16:22 -0700703out:
Tyler Hicks4dfea4f2013-05-09 16:55:07 -0700704 ablkcipher_request_free(req);
Michael Halcrow237fead2006-10-04 02:16:22 -0700705 return rc;
Tyler Hicks4dfea4f2013-05-09 16:55:07 -0700706
Michael Halcrow237fead2006-10-04 02:16:22 -0700707}
708
709/**
710 * ecryptfs_encrypt_page_offset
Michael Halcrow22e78fa2007-10-16 01:28:02 -0700711 * @crypt_stat: The cryptographic context
712 * @dst_page: The page to encrypt into
Michael Halcrow22e78fa2007-10-16 01:28:02 -0700713 * @src_page: The page to encrypt from
Tyler Hicks28916d12013-04-15 16:37:27 -0700714 * @offset: The byte offset into the dst_page and src_page
Michael Halcrow22e78fa2007-10-16 01:28:02 -0700715 * @size: The number of bytes to encrypt
716 * @iv: The initialization vector to use for the encryption
Michael Halcrow237fead2006-10-04 02:16:22 -0700717 *
718 * Returns the number of bytes encrypted
719 */
720static int
721ecryptfs_encrypt_page_offset(struct ecryptfs_crypt_stat *crypt_stat,
Tyler Hicks28916d12013-04-15 16:37:27 -0700722 struct page *dst_page, struct page *src_page,
723 int offset, int size, unsigned char *iv)
Michael Halcrow237fead2006-10-04 02:16:22 -0700724{
725 struct scatterlist src_sg, dst_sg;
726
Jens Axboe60c74f82007-10-22 19:43:30 +0200727 sg_init_table(&src_sg, 1);
728 sg_init_table(&dst_sg, 1);
729
Tyler Hicks28916d12013-04-15 16:37:27 -0700730 sg_set_page(&src_sg, src_page, size, offset);
731 sg_set_page(&dst_sg, dst_page, size, offset);
Michael Halcrow237fead2006-10-04 02:16:22 -0700732 return encrypt_scatterlist(crypt_stat, &dst_sg, &src_sg, size, iv);
733}
734
735/**
736 * ecryptfs_decrypt_page_offset
Michael Halcrow22e78fa2007-10-16 01:28:02 -0700737 * @crypt_stat: The cryptographic context
738 * @dst_page: The page to decrypt into
Michael Halcrow22e78fa2007-10-16 01:28:02 -0700739 * @src_page: The page to decrypt from
Tyler Hicks28916d12013-04-15 16:37:27 -0700740 * @offset: The byte offset into the dst_page and src_page
Michael Halcrow22e78fa2007-10-16 01:28:02 -0700741 * @size: The number of bytes to decrypt
742 * @iv: The initialization vector to use for the decryption
Michael Halcrow237fead2006-10-04 02:16:22 -0700743 *
744 * Returns the number of bytes decrypted
745 */
746static int
747ecryptfs_decrypt_page_offset(struct ecryptfs_crypt_stat *crypt_stat,
Tyler Hicks28916d12013-04-15 16:37:27 -0700748 struct page *dst_page, struct page *src_page,
749 int offset, int size, unsigned char *iv)
Michael Halcrow237fead2006-10-04 02:16:22 -0700750{
751 struct scatterlist src_sg, dst_sg;
752
Jens Axboe60c74f82007-10-22 19:43:30 +0200753 sg_init_table(&src_sg, 1);
Tyler Hicks28916d12013-04-15 16:37:27 -0700754 sg_set_page(&src_sg, src_page, size, offset);
Jens Axboe60c74f82007-10-22 19:43:30 +0200755
Jens Axboe642f1492007-10-24 11:20:47 +0200756 sg_init_table(&dst_sg, 1);
Tyler Hicks28916d12013-04-15 16:37:27 -0700757 sg_set_page(&dst_sg, dst_page, size, offset);
Jens Axboe642f1492007-10-24 11:20:47 +0200758
Michael Halcrow237fead2006-10-04 02:16:22 -0700759 return decrypt_scatterlist(crypt_stat, &dst_sg, &src_sg, size, iv);
760}
761
762#define ECRYPTFS_MAX_SCATTERLIST_LEN 4
763
764/**
765 * ecryptfs_init_crypt_ctx
Uwe Kleine-König421f91d2010-06-11 12:17:00 +0200766 * @crypt_stat: Uninitialized crypt stats structure
Michael Halcrow237fead2006-10-04 02:16:22 -0700767 *
768 * Initialize the crypto context.
769 *
770 * TODO: Performance: Keep a cache of initialized cipher contexts;
771 * only init if needed
772 */
773int ecryptfs_init_crypt_ctx(struct ecryptfs_crypt_stat *crypt_stat)
774{
Michael Halcrow8bba0662006-10-30 22:07:18 -0800775 char *full_alg_name;
Michael Halcrow237fead2006-10-04 02:16:22 -0700776 int rc = -EINVAL;
777
778 if (!crypt_stat->cipher) {
779 ecryptfs_printk(KERN_ERR, "No cipher specified\n");
780 goto out;
781 }
782 ecryptfs_printk(KERN_DEBUG,
783 "Initializing cipher [%s]; strlen = [%d]; "
Tyler Hicksf24b3882010-11-15 17:36:38 -0600784 "key_size_bits = [%zd]\n",
Michael Halcrow237fead2006-10-04 02:16:22 -0700785 crypt_stat->cipher, (int)strlen(crypt_stat->cipher),
786 crypt_stat->key_size << 3);
787 if (crypt_stat->tfm) {
788 rc = 0;
789 goto out;
790 }
791 mutex_lock(&crypt_stat->cs_tfm_mutex);
Michael Halcrow8bba0662006-10-30 22:07:18 -0800792 rc = ecryptfs_crypto_api_algify_cipher_name(&full_alg_name,
793 crypt_stat->cipher, "cbc");
794 if (rc)
Eric Sandeenc8161f62007-12-22 14:03:26 -0800795 goto out_unlock;
Tyler Hicks4dfea4f2013-05-09 16:55:07 -0700796 crypt_stat->tfm = crypto_alloc_ablkcipher(full_alg_name, 0, 0);
Michael Halcrow8bba0662006-10-30 22:07:18 -0800797 kfree(full_alg_name);
Akinobu Mitade887772006-11-28 12:29:49 -0800798 if (IS_ERR(crypt_stat->tfm)) {
799 rc = PTR_ERR(crypt_stat->tfm);
Tyler Hicksb0105ea2009-08-11 00:36:32 -0500800 crypt_stat->tfm = NULL;
Michael Halcrow237fead2006-10-04 02:16:22 -0700801 ecryptfs_printk(KERN_ERR, "cryptfs: init_crypt_ctx(): "
802 "Error initializing cipher [%s]\n",
803 crypt_stat->cipher);
Eric Sandeenc8161f62007-12-22 14:03:26 -0800804 goto out_unlock;
Michael Halcrow237fead2006-10-04 02:16:22 -0700805 }
Tyler Hicks4dfea4f2013-05-09 16:55:07 -0700806 crypto_ablkcipher_set_flags(crypt_stat->tfm, CRYPTO_TFM_REQ_WEAK_KEY);
Michael Halcrow237fead2006-10-04 02:16:22 -0700807 rc = 0;
Eric Sandeenc8161f62007-12-22 14:03:26 -0800808out_unlock:
809 mutex_unlock(&crypt_stat->cs_tfm_mutex);
Michael Halcrow237fead2006-10-04 02:16:22 -0700810out:
811 return rc;
812}
813
814static void set_extent_mask_and_shift(struct ecryptfs_crypt_stat *crypt_stat)
815{
816 int extent_size_tmp;
817
818 crypt_stat->extent_mask = 0xFFFFFFFF;
819 crypt_stat->extent_shift = 0;
820 if (crypt_stat->extent_size == 0)
821 return;
822 extent_size_tmp = crypt_stat->extent_size;
823 while ((extent_size_tmp & 0x01) == 0) {
824 extent_size_tmp >>= 1;
825 crypt_stat->extent_mask <<= 1;
826 crypt_stat->extent_shift++;
827 }
828}
829
830void ecryptfs_set_default_sizes(struct ecryptfs_crypt_stat *crypt_stat)
831{
832 /* Default values; may be overwritten as we are parsing the
833 * packets. */
834 crypt_stat->extent_size = ECRYPTFS_DEFAULT_EXTENT_SIZE;
835 set_extent_mask_and_shift(crypt_stat);
836 crypt_stat->iv_bytes = ECRYPTFS_DEFAULT_IV_BYTES;
Michael Halcrowdd2a3b72007-02-12 00:53:46 -0800837 if (crypt_stat->flags & ECRYPTFS_METADATA_IN_XATTR)
Tyler Hicksfa3ef1c2010-02-11 05:09:14 -0600838 crypt_stat->metadata_size = ECRYPTFS_MINIMUM_HEADER_EXTENT_SIZE;
Michael Halcrow45eaab72007-10-16 01:28:05 -0700839 else {
840 if (PAGE_CACHE_SIZE <= ECRYPTFS_MINIMUM_HEADER_EXTENT_SIZE)
Tyler Hicksfa3ef1c2010-02-11 05:09:14 -0600841 crypt_stat->metadata_size =
Michael Halcrowcc11bef2008-02-06 01:38:32 -0800842 ECRYPTFS_MINIMUM_HEADER_EXTENT_SIZE;
Michael Halcrow45eaab72007-10-16 01:28:05 -0700843 else
Tyler Hicksfa3ef1c2010-02-11 05:09:14 -0600844 crypt_stat->metadata_size = PAGE_CACHE_SIZE;
Michael Halcrow45eaab72007-10-16 01:28:05 -0700845 }
Michael Halcrow237fead2006-10-04 02:16:22 -0700846}
847
848/**
849 * ecryptfs_compute_root_iv
850 * @crypt_stats
851 *
852 * On error, sets the root IV to all 0's.
853 */
854int ecryptfs_compute_root_iv(struct ecryptfs_crypt_stat *crypt_stat)
855{
856 int rc = 0;
857 char dst[MD5_DIGEST_SIZE];
858
859 BUG_ON(crypt_stat->iv_bytes > MD5_DIGEST_SIZE);
860 BUG_ON(crypt_stat->iv_bytes <= 0);
Michael Halcrowe2bd99e2007-02-12 00:53:49 -0800861 if (!(crypt_stat->flags & ECRYPTFS_KEY_VALID)) {
Michael Halcrow237fead2006-10-04 02:16:22 -0700862 rc = -EINVAL;
863 ecryptfs_printk(KERN_WARNING, "Session key not valid; "
864 "cannot generate root IV\n");
865 goto out;
866 }
867 rc = ecryptfs_calculate_md5(dst, crypt_stat, crypt_stat->key,
868 crypt_stat->key_size);
869 if (rc) {
870 ecryptfs_printk(KERN_WARNING, "Error attempting to compute "
871 "MD5 while generating root IV\n");
872 goto out;
873 }
874 memcpy(crypt_stat->root_iv, dst, crypt_stat->iv_bytes);
875out:
876 if (rc) {
877 memset(crypt_stat->root_iv, 0, crypt_stat->iv_bytes);
Michael Halcrowe2bd99e2007-02-12 00:53:49 -0800878 crypt_stat->flags |= ECRYPTFS_SECURITY_WARNING;
Michael Halcrow237fead2006-10-04 02:16:22 -0700879 }
880 return rc;
881}
882
883static void ecryptfs_generate_new_key(struct ecryptfs_crypt_stat *crypt_stat)
884{
885 get_random_bytes(crypt_stat->key, crypt_stat->key_size);
Michael Halcrowe2bd99e2007-02-12 00:53:49 -0800886 crypt_stat->flags |= ECRYPTFS_KEY_VALID;
Michael Halcrow237fead2006-10-04 02:16:22 -0700887 ecryptfs_compute_root_iv(crypt_stat);
888 if (unlikely(ecryptfs_verbosity > 0)) {
889 ecryptfs_printk(KERN_DEBUG, "Generated new session key:\n");
890 ecryptfs_dump_hex(crypt_stat->key,
891 crypt_stat->key_size);
892 }
893}
894
895/**
Michael Halcrow17398952007-02-12 00:53:45 -0800896 * ecryptfs_copy_mount_wide_flags_to_inode_flags
Michael Halcrow22e78fa2007-10-16 01:28:02 -0700897 * @crypt_stat: The inode's cryptographic context
898 * @mount_crypt_stat: The mount point's cryptographic context
Michael Halcrow17398952007-02-12 00:53:45 -0800899 *
900 * This function propagates the mount-wide flags to individual inode
901 * flags.
902 */
903static void ecryptfs_copy_mount_wide_flags_to_inode_flags(
904 struct ecryptfs_crypt_stat *crypt_stat,
905 struct ecryptfs_mount_crypt_stat *mount_crypt_stat)
906{
907 if (mount_crypt_stat->flags & ECRYPTFS_XATTR_METADATA_ENABLED)
908 crypt_stat->flags |= ECRYPTFS_METADATA_IN_XATTR;
909 if (mount_crypt_stat->flags & ECRYPTFS_ENCRYPTED_VIEW_ENABLED)
910 crypt_stat->flags |= ECRYPTFS_VIEW_AS_ENCRYPTED;
Michael Halcrowaddd65ad2009-01-06 14:42:00 -0800911 if (mount_crypt_stat->flags & ECRYPTFS_GLOBAL_ENCRYPT_FILENAMES) {
912 crypt_stat->flags |= ECRYPTFS_ENCRYPT_FILENAMES;
913 if (mount_crypt_stat->flags
914 & ECRYPTFS_GLOBAL_ENCFN_USE_MOUNT_FNEK)
915 crypt_stat->flags |= ECRYPTFS_ENCFN_USE_MOUNT_FNEK;
916 else if (mount_crypt_stat->flags
917 & ECRYPTFS_GLOBAL_ENCFN_USE_FEK)
918 crypt_stat->flags |= ECRYPTFS_ENCFN_USE_FEK;
919 }
Michael Halcrow17398952007-02-12 00:53:45 -0800920}
921
Michael Halcrowf4aad162007-10-16 01:27:53 -0700922static int ecryptfs_copy_mount_wide_sigs_to_inode_sigs(
923 struct ecryptfs_crypt_stat *crypt_stat,
924 struct ecryptfs_mount_crypt_stat *mount_crypt_stat)
925{
926 struct ecryptfs_global_auth_tok *global_auth_tok;
927 int rc = 0;
928
Roland Dreieraa061172009-07-01 15:48:18 -0700929 mutex_lock(&crypt_stat->keysig_list_mutex);
Michael Halcrowf4aad162007-10-16 01:27:53 -0700930 mutex_lock(&mount_crypt_stat->global_auth_tok_list_mutex);
Roland Dreieraa061172009-07-01 15:48:18 -0700931
Michael Halcrowf4aad162007-10-16 01:27:53 -0700932 list_for_each_entry(global_auth_tok,
933 &mount_crypt_stat->global_auth_tok_list,
934 mount_crypt_stat_list) {
Tyler Hicks84814d62009-03-13 13:51:59 -0700935 if (global_auth_tok->flags & ECRYPTFS_AUTH_TOK_FNEK)
936 continue;
Michael Halcrowf4aad162007-10-16 01:27:53 -0700937 rc = ecryptfs_add_keysig(crypt_stat, global_auth_tok->sig);
938 if (rc) {
939 printk(KERN_ERR "Error adding keysig; rc = [%d]\n", rc);
Michael Halcrowf4aad162007-10-16 01:27:53 -0700940 goto out;
941 }
942 }
Roland Dreieraa061172009-07-01 15:48:18 -0700943
Michael Halcrowf4aad162007-10-16 01:27:53 -0700944out:
Roland Dreieraa061172009-07-01 15:48:18 -0700945 mutex_unlock(&mount_crypt_stat->global_auth_tok_list_mutex);
946 mutex_unlock(&crypt_stat->keysig_list_mutex);
Michael Halcrowf4aad162007-10-16 01:27:53 -0700947 return rc;
948}
949
Michael Halcrow17398952007-02-12 00:53:45 -0800950/**
Michael Halcrow237fead2006-10-04 02:16:22 -0700951 * ecryptfs_set_default_crypt_stat_vals
Michael Halcrow22e78fa2007-10-16 01:28:02 -0700952 * @crypt_stat: The inode's cryptographic context
953 * @mount_crypt_stat: The mount point's cryptographic context
Michael Halcrow237fead2006-10-04 02:16:22 -0700954 *
955 * Default values in the event that policy does not override them.
956 */
957static void ecryptfs_set_default_crypt_stat_vals(
958 struct ecryptfs_crypt_stat *crypt_stat,
959 struct ecryptfs_mount_crypt_stat *mount_crypt_stat)
960{
Michael Halcrow17398952007-02-12 00:53:45 -0800961 ecryptfs_copy_mount_wide_flags_to_inode_flags(crypt_stat,
962 mount_crypt_stat);
Michael Halcrow237fead2006-10-04 02:16:22 -0700963 ecryptfs_set_default_sizes(crypt_stat);
964 strcpy(crypt_stat->cipher, ECRYPTFS_DEFAULT_CIPHER);
965 crypt_stat->key_size = ECRYPTFS_DEFAULT_KEY_BYTES;
Michael Halcrowe2bd99e2007-02-12 00:53:49 -0800966 crypt_stat->flags &= ~(ECRYPTFS_KEY_VALID);
Michael Halcrow237fead2006-10-04 02:16:22 -0700967 crypt_stat->file_version = ECRYPTFS_FILE_VERSION;
968 crypt_stat->mount_crypt_stat = mount_crypt_stat;
969}
970
971/**
972 * ecryptfs_new_file_context
Tyler Hicksb59db432011-11-21 17:31:02 -0600973 * @ecryptfs_inode: The eCryptfs inode
Michael Halcrow237fead2006-10-04 02:16:22 -0700974 *
975 * If the crypto context for the file has not yet been established,
976 * this is where we do that. Establishing a new crypto context
977 * involves the following decisions:
978 * - What cipher to use?
979 * - What set of authentication tokens to use?
980 * Here we just worry about getting enough information into the
981 * authentication tokens so that we know that they are available.
982 * We associate the available authentication tokens with the new file
983 * via the set of signatures in the crypt_stat struct. Later, when
984 * the headers are actually written out, we may again defer to
985 * userspace to perform the encryption of the session key; for the
986 * foreseeable future, this will be the case with public key packets.
987 *
988 * Returns zero on success; non-zero otherwise
989 */
Tyler Hicksb59db432011-11-21 17:31:02 -0600990int ecryptfs_new_file_context(struct inode *ecryptfs_inode)
Michael Halcrow237fead2006-10-04 02:16:22 -0700991{
Michael Halcrow237fead2006-10-04 02:16:22 -0700992 struct ecryptfs_crypt_stat *crypt_stat =
Tyler Hicksb59db432011-11-21 17:31:02 -0600993 &ecryptfs_inode_to_private(ecryptfs_inode)->crypt_stat;
Michael Halcrow237fead2006-10-04 02:16:22 -0700994 struct ecryptfs_mount_crypt_stat *mount_crypt_stat =
995 &ecryptfs_superblock_to_private(
Tyler Hicksb59db432011-11-21 17:31:02 -0600996 ecryptfs_inode->i_sb)->mount_crypt_stat;
Michael Halcrow237fead2006-10-04 02:16:22 -0700997 int cipher_name_len;
Michael Halcrowf4aad162007-10-16 01:27:53 -0700998 int rc = 0;
Michael Halcrow237fead2006-10-04 02:16:22 -0700999
1000 ecryptfs_set_default_crypt_stat_vals(crypt_stat, mount_crypt_stat);
Michael Halcrowaf655dc2007-10-16 01:28:00 -07001001 crypt_stat->flags |= (ECRYPTFS_ENCRYPTED | ECRYPTFS_KEY_VALID);
Michael Halcrowf4aad162007-10-16 01:27:53 -07001002 ecryptfs_copy_mount_wide_flags_to_inode_flags(crypt_stat,
1003 mount_crypt_stat);
1004 rc = ecryptfs_copy_mount_wide_sigs_to_inode_sigs(crypt_stat,
1005 mount_crypt_stat);
1006 if (rc) {
1007 printk(KERN_ERR "Error attempting to copy mount-wide key sigs "
1008 "to the inode key sigs; rc = [%d]\n", rc);
1009 goto out;
1010 }
1011 cipher_name_len =
1012 strlen(mount_crypt_stat->global_default_cipher_name);
1013 memcpy(crypt_stat->cipher,
1014 mount_crypt_stat->global_default_cipher_name,
1015 cipher_name_len);
1016 crypt_stat->cipher[cipher_name_len] = '\0';
1017 crypt_stat->key_size =
1018 mount_crypt_stat->global_default_cipher_key_size;
1019 ecryptfs_generate_new_key(crypt_stat);
Michael Halcrow237fead2006-10-04 02:16:22 -07001020 rc = ecryptfs_init_crypt_ctx(crypt_stat);
1021 if (rc)
1022 ecryptfs_printk(KERN_ERR, "Error initializing cryptographic "
1023 "context for cipher [%s]: rc = [%d]\n",
1024 crypt_stat->cipher, rc);
Michael Halcrowf4aad162007-10-16 01:27:53 -07001025out:
Michael Halcrow237fead2006-10-04 02:16:22 -07001026 return rc;
1027}
1028
1029/**
Tyler Hicks7a866172011-05-02 00:39:54 -05001030 * ecryptfs_validate_marker - check for the ecryptfs marker
Michael Halcrow237fead2006-10-04 02:16:22 -07001031 * @data: The data block in which to check
1032 *
Tyler Hicks7a866172011-05-02 00:39:54 -05001033 * Returns zero if marker found; -EINVAL if not found
Michael Halcrow237fead2006-10-04 02:16:22 -07001034 */
Tyler Hicks7a866172011-05-02 00:39:54 -05001035static int ecryptfs_validate_marker(char *data)
Michael Halcrow237fead2006-10-04 02:16:22 -07001036{
1037 u32 m_1, m_2;
1038
Harvey Harrison29335c62008-07-23 21:30:06 -07001039 m_1 = get_unaligned_be32(data);
1040 m_2 = get_unaligned_be32(data + 4);
Michael Halcrow237fead2006-10-04 02:16:22 -07001041 if ((m_1 ^ MAGIC_ECRYPTFS_MARKER) == m_2)
Tyler Hicks7a866172011-05-02 00:39:54 -05001042 return 0;
Michael Halcrow237fead2006-10-04 02:16:22 -07001043 ecryptfs_printk(KERN_DEBUG, "m_1 = [0x%.8x]; m_2 = [0x%.8x]; "
1044 "MAGIC_ECRYPTFS_MARKER = [0x%.8x]\n", m_1, m_2,
1045 MAGIC_ECRYPTFS_MARKER);
1046 ecryptfs_printk(KERN_DEBUG, "(m_1 ^ MAGIC_ECRYPTFS_MARKER) = "
1047 "[0x%.8x]\n", (m_1 ^ MAGIC_ECRYPTFS_MARKER));
Tyler Hicks7a866172011-05-02 00:39:54 -05001048 return -EINVAL;
Michael Halcrow237fead2006-10-04 02:16:22 -07001049}
1050
1051struct ecryptfs_flag_map_elem {
1052 u32 file_flag;
1053 u32 local_flag;
1054};
1055
1056/* Add support for additional flags by adding elements here. */
1057static struct ecryptfs_flag_map_elem ecryptfs_flag_map[] = {
1058 {0x00000001, ECRYPTFS_ENABLE_HMAC},
Michael Halcrowdd2a3b72007-02-12 00:53:46 -08001059 {0x00000002, ECRYPTFS_ENCRYPTED},
Michael Halcrowaddd65ad2009-01-06 14:42:00 -08001060 {0x00000004, ECRYPTFS_METADATA_IN_XATTR},
1061 {0x00000008, ECRYPTFS_ENCRYPT_FILENAMES}
Michael Halcrow237fead2006-10-04 02:16:22 -07001062};
1063
1064/**
1065 * ecryptfs_process_flags
Michael Halcrow22e78fa2007-10-16 01:28:02 -07001066 * @crypt_stat: The cryptographic context
Michael Halcrow237fead2006-10-04 02:16:22 -07001067 * @page_virt: Source data to be parsed
1068 * @bytes_read: Updated with the number of bytes read
1069 *
1070 * Returns zero on success; non-zero if the flag set is invalid
1071 */
1072static int ecryptfs_process_flags(struct ecryptfs_crypt_stat *crypt_stat,
1073 char *page_virt, int *bytes_read)
1074{
1075 int rc = 0;
1076 int i;
1077 u32 flags;
1078
Harvey Harrison29335c62008-07-23 21:30:06 -07001079 flags = get_unaligned_be32(page_virt);
Michael Halcrow237fead2006-10-04 02:16:22 -07001080 for (i = 0; i < ((sizeof(ecryptfs_flag_map)
1081 / sizeof(struct ecryptfs_flag_map_elem))); i++)
1082 if (flags & ecryptfs_flag_map[i].file_flag) {
Michael Halcrowe2bd99e2007-02-12 00:53:49 -08001083 crypt_stat->flags |= ecryptfs_flag_map[i].local_flag;
Michael Halcrow237fead2006-10-04 02:16:22 -07001084 } else
Michael Halcrowe2bd99e2007-02-12 00:53:49 -08001085 crypt_stat->flags &= ~(ecryptfs_flag_map[i].local_flag);
Michael Halcrow237fead2006-10-04 02:16:22 -07001086 /* Version is in top 8 bits of the 32-bit flag vector */
1087 crypt_stat->file_version = ((flags >> 24) & 0xFF);
1088 (*bytes_read) = 4;
1089 return rc;
1090}
1091
1092/**
1093 * write_ecryptfs_marker
1094 * @page_virt: The pointer to in a page to begin writing the marker
1095 * @written: Number of bytes written
1096 *
1097 * Marker = 0x3c81b7f5
1098 */
1099static void write_ecryptfs_marker(char *page_virt, size_t *written)
1100{
1101 u32 m_1, m_2;
1102
1103 get_random_bytes(&m_1, (MAGIC_ECRYPTFS_MARKER_SIZE_BYTES / 2));
1104 m_2 = (m_1 ^ MAGIC_ECRYPTFS_MARKER);
Harvey Harrison29335c62008-07-23 21:30:06 -07001105 put_unaligned_be32(m_1, page_virt);
1106 page_virt += (MAGIC_ECRYPTFS_MARKER_SIZE_BYTES / 2);
1107 put_unaligned_be32(m_2, page_virt);
Michael Halcrow237fead2006-10-04 02:16:22 -07001108 (*written) = MAGIC_ECRYPTFS_MARKER_SIZE_BYTES;
1109}
1110
Tyler Hicksf4e60e62010-02-11 00:02:32 -06001111void ecryptfs_write_crypt_stat_flags(char *page_virt,
1112 struct ecryptfs_crypt_stat *crypt_stat,
1113 size_t *written)
Michael Halcrow237fead2006-10-04 02:16:22 -07001114{
1115 u32 flags = 0;
1116 int i;
1117
1118 for (i = 0; i < ((sizeof(ecryptfs_flag_map)
1119 / sizeof(struct ecryptfs_flag_map_elem))); i++)
Michael Halcrowe2bd99e2007-02-12 00:53:49 -08001120 if (crypt_stat->flags & ecryptfs_flag_map[i].local_flag)
Michael Halcrow237fead2006-10-04 02:16:22 -07001121 flags |= ecryptfs_flag_map[i].file_flag;
1122 /* Version is in top 8 bits of the 32-bit flag vector */
1123 flags |= ((((u8)crypt_stat->file_version) << 24) & 0xFF000000);
Harvey Harrison29335c62008-07-23 21:30:06 -07001124 put_unaligned_be32(flags, page_virt);
Michael Halcrow237fead2006-10-04 02:16:22 -07001125 (*written) = 4;
1126}
1127
1128struct ecryptfs_cipher_code_str_map_elem {
1129 char cipher_str[16];
Trevor Highland19e66a62008-02-06 01:38:36 -08001130 u8 cipher_code;
Michael Halcrow237fead2006-10-04 02:16:22 -07001131};
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
Michael Halcrow9c79f342009-01-06 14:41:57 -08001150 * @cipher_name: The string alias for the cipher
1151 * @key_bytes: Length of key in bytes; used for AES code selection
Michael Halcrow237fead2006-10-04 02:16:22 -07001152 *
1153 * Returns zero on no match, or the cipher code on match
1154 */
Michael Halcrow9c79f342009-01-06 14:41:57 -08001155u8 ecryptfs_code_for_cipher_string(char *cipher_name, size_t key_bytes)
Michael Halcrow237fead2006-10-04 02:16:22 -07001156{
1157 int i;
Trevor Highland19e66a62008-02-06 01:38:36 -08001158 u8 code = 0;
Michael Halcrow237fead2006-10-04 02:16:22 -07001159 struct ecryptfs_cipher_code_str_map_elem *map =
1160 ecryptfs_cipher_code_str_map;
1161
Michael Halcrow9c79f342009-01-06 14:41:57 -08001162 if (strcmp(cipher_name, "aes") == 0) {
1163 switch (key_bytes) {
Michael Halcrow237fead2006-10-04 02:16:22 -07001164 case 16:
1165 code = RFC2440_CIPHER_AES_128;
1166 break;
1167 case 24:
1168 code = RFC2440_CIPHER_AES_192;
1169 break;
1170 case 32:
1171 code = RFC2440_CIPHER_AES_256;
1172 }
1173 } else {
1174 for (i = 0; i < ARRAY_SIZE(ecryptfs_cipher_code_str_map); i++)
Michael Halcrow9c79f342009-01-06 14:41:57 -08001175 if (strcmp(cipher_name, map[i].cipher_str) == 0) {
Michael Halcrow237fead2006-10-04 02:16:22 -07001176 code = map[i].cipher_code;
1177 break;
1178 }
1179 }
1180 return code;
1181}
1182
1183/**
1184 * ecryptfs_cipher_code_to_string
1185 * @str: Destination to write out the cipher name
1186 * @cipher_code: The code to convert to cipher name string
1187 *
1188 * Returns zero on success
1189 */
Trevor Highland19e66a62008-02-06 01:38:36 -08001190int ecryptfs_cipher_code_to_string(char *str, u8 cipher_code)
Michael Halcrow237fead2006-10-04 02:16:22 -07001191{
1192 int rc = 0;
1193 int i;
1194
1195 str[0] = '\0';
1196 for (i = 0; i < ARRAY_SIZE(ecryptfs_cipher_code_str_map); i++)
1197 if (cipher_code == ecryptfs_cipher_code_str_map[i].cipher_code)
1198 strcpy(str, ecryptfs_cipher_code_str_map[i].cipher_str);
1199 if (str[0] == '\0') {
1200 ecryptfs_printk(KERN_WARNING, "Cipher code not recognized: "
1201 "[%d]\n", cipher_code);
1202 rc = -EINVAL;
1203 }
1204 return rc;
1205}
1206
Tyler Hicks778aeb42011-05-24 04:56:23 -05001207int ecryptfs_read_and_validate_header_region(struct inode *inode)
Michael Halcrowdd2a3b72007-02-12 00:53:46 -08001208{
Tyler Hicks778aeb42011-05-24 04:56:23 -05001209 u8 file_size[ECRYPTFS_SIZE_AND_MARKER_BYTES];
1210 u8 *marker = file_size + ECRYPTFS_FILE_SIZE_BYTES;
Michael Halcrowdd2a3b72007-02-12 00:53:46 -08001211 int rc;
1212
Tyler Hicks778aeb42011-05-24 04:56:23 -05001213 rc = ecryptfs_read_lower(file_size, 0, ECRYPTFS_SIZE_AND_MARKER_BYTES,
1214 inode);
1215 if (rc < ECRYPTFS_SIZE_AND_MARKER_BYTES)
1216 return rc >= 0 ? -EINVAL : rc;
1217 rc = ecryptfs_validate_marker(marker);
1218 if (!rc)
1219 ecryptfs_i_size_init(file_size, inode);
Michael Halcrowdd2a3b72007-02-12 00:53:46 -08001220 return rc;
1221}
1222
Michael Halcrowe77a56d2007-02-12 00:53:47 -08001223void
1224ecryptfs_write_header_metadata(char *virt,
1225 struct ecryptfs_crypt_stat *crypt_stat,
1226 size_t *written)
Michael Halcrow237fead2006-10-04 02:16:22 -07001227{
1228 u32 header_extent_size;
1229 u16 num_header_extents_at_front;
1230
Michael Halcrow45eaab72007-10-16 01:28:05 -07001231 header_extent_size = (u32)crypt_stat->extent_size;
Michael Halcrow237fead2006-10-04 02:16:22 -07001232 num_header_extents_at_front =
Tyler Hicksfa3ef1c2010-02-11 05:09:14 -06001233 (u16)(crypt_stat->metadata_size / crypt_stat->extent_size);
Harvey Harrison29335c62008-07-23 21:30:06 -07001234 put_unaligned_be32(header_extent_size, virt);
Michael Halcrow237fead2006-10-04 02:16:22 -07001235 virt += 4;
Harvey Harrison29335c62008-07-23 21:30:06 -07001236 put_unaligned_be16(num_header_extents_at_front, virt);
Michael Halcrow237fead2006-10-04 02:16:22 -07001237 (*written) = 6;
1238}
1239
Tyler Hicks30632872011-05-24 05:11:12 -05001240struct kmem_cache *ecryptfs_header_cache;
Michael Halcrow237fead2006-10-04 02:16:22 -07001241
1242/**
1243 * ecryptfs_write_headers_virt
Michael Halcrow22e78fa2007-10-16 01:28:02 -07001244 * @page_virt: The virtual address to write the headers to
Eric Sandeen87b811c2008-10-29 14:01:08 -07001245 * @max: The size of memory allocated at page_virt
Michael Halcrow22e78fa2007-10-16 01:28:02 -07001246 * @size: Set to the number of bytes written by this function
1247 * @crypt_stat: The cryptographic context
1248 * @ecryptfs_dentry: The eCryptfs dentry
Michael Halcrow237fead2006-10-04 02:16:22 -07001249 *
1250 * Format version: 1
1251 *
1252 * Header Extent:
1253 * Octets 0-7: Unencrypted file size (big-endian)
1254 * Octets 8-15: eCryptfs special marker
1255 * Octets 16-19: Flags
1256 * Octet 16: File format version number (between 0 and 255)
1257 * Octets 17-18: Reserved
1258 * Octet 19: Bit 1 (lsb): Reserved
1259 * Bit 2: Encrypted?
1260 * Bits 3-8: Reserved
1261 * Octets 20-23: Header extent size (big-endian)
1262 * Octets 24-25: Number of header extents at front of file
1263 * (big-endian)
1264 * Octet 26: Begin RFC 2440 authentication token packet set
1265 * Data Extent 0:
1266 * Lower data (CBC encrypted)
1267 * Data Extent 1:
1268 * Lower data (CBC encrypted)
1269 * ...
1270 *
1271 * Returns zero on success
1272 */
Eric Sandeen87b811c2008-10-29 14:01:08 -07001273static int ecryptfs_write_headers_virt(char *page_virt, size_t max,
1274 size_t *size,
Michael Halcrowdd2a3b72007-02-12 00:53:46 -08001275 struct ecryptfs_crypt_stat *crypt_stat,
1276 struct dentry *ecryptfs_dentry)
Michael Halcrow237fead2006-10-04 02:16:22 -07001277{
1278 int rc;
1279 size_t written;
1280 size_t offset;
1281
1282 offset = ECRYPTFS_FILE_SIZE_BYTES;
1283 write_ecryptfs_marker((page_virt + offset), &written);
1284 offset += written;
Tyler Hicksf4e60e62010-02-11 00:02:32 -06001285 ecryptfs_write_crypt_stat_flags((page_virt + offset), crypt_stat,
1286 &written);
Michael Halcrow237fead2006-10-04 02:16:22 -07001287 offset += written;
Michael Halcrowe77a56d2007-02-12 00:53:47 -08001288 ecryptfs_write_header_metadata((page_virt + offset), crypt_stat,
1289 &written);
Michael Halcrow237fead2006-10-04 02:16:22 -07001290 offset += written;
1291 rc = ecryptfs_generate_key_packet_set((page_virt + offset), crypt_stat,
1292 ecryptfs_dentry, &written,
Eric Sandeen87b811c2008-10-29 14:01:08 -07001293 max - offset);
Michael Halcrow237fead2006-10-04 02:16:22 -07001294 if (rc)
1295 ecryptfs_printk(KERN_WARNING, "Error generating key packet "
1296 "set; rc = [%d]\n", rc);
Michael Halcrowdd2a3b72007-02-12 00:53:46 -08001297 if (size) {
1298 offset += written;
1299 *size = offset;
1300 }
1301 return rc;
1302}
1303
Michael Halcrow22e78fa2007-10-16 01:28:02 -07001304static int
Tyler Hicksb59db432011-11-21 17:31:02 -06001305ecryptfs_write_metadata_to_contents(struct inode *ecryptfs_inode,
Tyler Hicks8faece52009-03-20 01:25:09 -05001306 char *virt, size_t virt_len)
Michael Halcrowdd2a3b72007-02-12 00:53:46 -08001307{
Michael Halcrowd7cdc5f2007-10-16 01:28:10 -07001308 int rc;
Michael Halcrowdd2a3b72007-02-12 00:53:46 -08001309
Tyler Hicksb59db432011-11-21 17:31:02 -06001310 rc = ecryptfs_write_lower(ecryptfs_inode, virt,
Tyler Hicks8faece52009-03-20 01:25:09 -05001311 0, virt_len);
Tyler Hicks96a7b9c2009-09-16 19:04:20 -05001312 if (rc < 0)
Michael Halcrowd7cdc5f2007-10-16 01:28:10 -07001313 printk(KERN_ERR "%s: Error attempting to write header "
Tyler Hicks96a7b9c2009-09-16 19:04:20 -05001314 "information to lower file; rc = [%d]\n", __func__, rc);
1315 else
1316 rc = 0;
Michael Halcrow70456602007-02-12 00:53:48 -08001317 return rc;
Michael Halcrowdd2a3b72007-02-12 00:53:46 -08001318}
1319
Michael Halcrow22e78fa2007-10-16 01:28:02 -07001320static int
1321ecryptfs_write_metadata_to_xattr(struct dentry *ecryptfs_dentry,
Michael Halcrow22e78fa2007-10-16 01:28:02 -07001322 char *page_virt, size_t size)
Michael Halcrowdd2a3b72007-02-12 00:53:46 -08001323{
1324 int rc;
1325
1326 rc = ecryptfs_setxattr(ecryptfs_dentry, ECRYPTFS_XATTR_NAME, page_virt,
1327 size, 0);
Michael Halcrow237fead2006-10-04 02:16:22 -07001328 return rc;
1329}
1330
Tyler Hicks8faece52009-03-20 01:25:09 -05001331static unsigned long ecryptfs_get_zeroed_pages(gfp_t gfp_mask,
1332 unsigned int order)
1333{
1334 struct page *page;
1335
1336 page = alloc_pages(gfp_mask | __GFP_ZERO, order);
1337 if (page)
1338 return (unsigned long) page_address(page);
1339 return 0;
1340}
1341
Michael Halcrow237fead2006-10-04 02:16:22 -07001342/**
Michael Halcrowdd2a3b72007-02-12 00:53:46 -08001343 * ecryptfs_write_metadata
Tyler Hicksb59db432011-11-21 17:31:02 -06001344 * @ecryptfs_dentry: The eCryptfs dentry, which should be negative
1345 * @ecryptfs_inode: The newly created eCryptfs inode
Michael Halcrow237fead2006-10-04 02:16:22 -07001346 *
1347 * Write the file headers out. This will likely involve a userspace
1348 * callout, in which the session key is encrypted with one or more
1349 * public keys and/or the passphrase necessary to do the encryption is
1350 * retrieved via a prompt. Exactly what happens at this point should
1351 * be policy-dependent.
1352 *
1353 * Returns zero on success; non-zero on error
1354 */
Tyler Hicksb59db432011-11-21 17:31:02 -06001355int ecryptfs_write_metadata(struct dentry *ecryptfs_dentry,
1356 struct inode *ecryptfs_inode)
Michael Halcrow237fead2006-10-04 02:16:22 -07001357{
Michael Halcrowd7cdc5f2007-10-16 01:28:10 -07001358 struct ecryptfs_crypt_stat *crypt_stat =
Tyler Hicksb59db432011-11-21 17:31:02 -06001359 &ecryptfs_inode_to_private(ecryptfs_inode)->crypt_stat;
Tyler Hicks8faece52009-03-20 01:25:09 -05001360 unsigned int order;
Michael Halcrowcc11bef2008-02-06 01:38:32 -08001361 char *virt;
Tyler Hicks8faece52009-03-20 01:25:09 -05001362 size_t virt_len;
Michael Halcrowd7cdc5f2007-10-16 01:28:10 -07001363 size_t size = 0;
Michael Halcrow237fead2006-10-04 02:16:22 -07001364 int rc = 0;
1365
Michael Halcrowe2bd99e2007-02-12 00:53:49 -08001366 if (likely(crypt_stat->flags & ECRYPTFS_ENCRYPTED)) {
1367 if (!(crypt_stat->flags & ECRYPTFS_KEY_VALID)) {
Michael Halcrowd7cdc5f2007-10-16 01:28:10 -07001368 printk(KERN_ERR "Key is invalid; bailing out\n");
Michael Halcrow237fead2006-10-04 02:16:22 -07001369 rc = -EINVAL;
1370 goto out;
1371 }
1372 } else {
Michael Halcrowcc11bef2008-02-06 01:38:32 -08001373 printk(KERN_WARNING "%s: Encrypted flag not set\n",
Harvey Harrison18d1dbf2008-04-29 00:59:48 -07001374 __func__);
Michael Halcrow237fead2006-10-04 02:16:22 -07001375 rc = -EINVAL;
Michael Halcrow237fead2006-10-04 02:16:22 -07001376 goto out;
1377 }
Tyler Hicksfa3ef1c2010-02-11 05:09:14 -06001378 virt_len = crypt_stat->metadata_size;
Tyler Hicks8faece52009-03-20 01:25:09 -05001379 order = get_order(virt_len);
Michael Halcrow237fead2006-10-04 02:16:22 -07001380 /* Released in this function */
Tyler Hicks8faece52009-03-20 01:25:09 -05001381 virt = (char *)ecryptfs_get_zeroed_pages(GFP_KERNEL, order);
Michael Halcrowcc11bef2008-02-06 01:38:32 -08001382 if (!virt) {
Harvey Harrison18d1dbf2008-04-29 00:59:48 -07001383 printk(KERN_ERR "%s: Out of memory\n", __func__);
Michael Halcrow237fead2006-10-04 02:16:22 -07001384 rc = -ENOMEM;
1385 goto out;
1386 }
Tyler Hicksbd4f0fe2011-02-23 00:14:19 -06001387 /* Zeroed page ensures the in-header unencrypted i_size is set to 0 */
Tyler Hicks8faece52009-03-20 01:25:09 -05001388 rc = ecryptfs_write_headers_virt(virt, virt_len, &size, crypt_stat,
1389 ecryptfs_dentry);
Michael Halcrow237fead2006-10-04 02:16:22 -07001390 if (unlikely(rc)) {
Michael Halcrowcc11bef2008-02-06 01:38:32 -08001391 printk(KERN_ERR "%s: Error whilst writing headers; rc = [%d]\n",
Harvey Harrison18d1dbf2008-04-29 00:59:48 -07001392 __func__, rc);
Michael Halcrow237fead2006-10-04 02:16:22 -07001393 goto out_free;
1394 }
Michael Halcrowdd2a3b72007-02-12 00:53:46 -08001395 if (crypt_stat->flags & ECRYPTFS_METADATA_IN_XATTR)
Tyler Hicks8faece52009-03-20 01:25:09 -05001396 rc = ecryptfs_write_metadata_to_xattr(ecryptfs_dentry, virt,
1397 size);
Michael Halcrowdd2a3b72007-02-12 00:53:46 -08001398 else
Tyler Hicksb59db432011-11-21 17:31:02 -06001399 rc = ecryptfs_write_metadata_to_contents(ecryptfs_inode, virt,
Tyler Hicks8faece52009-03-20 01:25:09 -05001400 virt_len);
Michael Halcrowdd2a3b72007-02-12 00:53:46 -08001401 if (rc) {
Michael Halcrowcc11bef2008-02-06 01:38:32 -08001402 printk(KERN_ERR "%s: Error writing metadata out to lower file; "
Harvey Harrison18d1dbf2008-04-29 00:59:48 -07001403 "rc = [%d]\n", __func__, rc);
Michael Halcrowdd2a3b72007-02-12 00:53:46 -08001404 goto out_free;
Michael Halcrow237fead2006-10-04 02:16:22 -07001405 }
Michael Halcrow237fead2006-10-04 02:16:22 -07001406out_free:
Tyler Hicks8faece52009-03-20 01:25:09 -05001407 free_pages((unsigned long)virt, order);
Michael Halcrow237fead2006-10-04 02:16:22 -07001408out:
1409 return rc;
1410}
1411
Michael Halcrowdd2a3b72007-02-12 00:53:46 -08001412#define ECRYPTFS_DONT_VALIDATE_HEADER_SIZE 0
1413#define ECRYPTFS_VALIDATE_HEADER_SIZE 1
Michael Halcrow237fead2006-10-04 02:16:22 -07001414static int parse_header_metadata(struct ecryptfs_crypt_stat *crypt_stat,
Michael Halcrowdd2a3b72007-02-12 00:53:46 -08001415 char *virt, int *bytes_read,
1416 int validate_header_size)
Michael Halcrow237fead2006-10-04 02:16:22 -07001417{
1418 int rc = 0;
1419 u32 header_extent_size;
1420 u16 num_header_extents_at_front;
1421
Harvey Harrison29335c62008-07-23 21:30:06 -07001422 header_extent_size = get_unaligned_be32(virt);
1423 virt += sizeof(__be32);
1424 num_header_extents_at_front = get_unaligned_be16(virt);
Tyler Hicksfa3ef1c2010-02-11 05:09:14 -06001425 crypt_stat->metadata_size = (((size_t)num_header_extents_at_front
1426 * (size_t)header_extent_size));
Harvey Harrison29335c62008-07-23 21:30:06 -07001427 (*bytes_read) = (sizeof(__be32) + sizeof(__be16));
Michael Halcrowdd2a3b72007-02-12 00:53:46 -08001428 if ((validate_header_size == ECRYPTFS_VALIDATE_HEADER_SIZE)
Tyler Hicksfa3ef1c2010-02-11 05:09:14 -06001429 && (crypt_stat->metadata_size
Michael Halcrowdd2a3b72007-02-12 00:53:46 -08001430 < ECRYPTFS_MINIMUM_HEADER_EXTENT_SIZE)) {
Michael Halcrow237fead2006-10-04 02:16:22 -07001431 rc = -EINVAL;
Michael Halcrowcc11bef2008-02-06 01:38:32 -08001432 printk(KERN_WARNING "Invalid header size: [%zd]\n",
Tyler Hicksfa3ef1c2010-02-11 05:09:14 -06001433 crypt_stat->metadata_size);
Michael Halcrow237fead2006-10-04 02:16:22 -07001434 }
1435 return rc;
1436}
1437
1438/**
1439 * set_default_header_data
Michael Halcrow22e78fa2007-10-16 01:28:02 -07001440 * @crypt_stat: The cryptographic context
Michael Halcrow237fead2006-10-04 02:16:22 -07001441 *
1442 * For version 0 file format; this function is only for backwards
1443 * compatibility for files created with the prior versions of
1444 * eCryptfs.
1445 */
1446static void set_default_header_data(struct ecryptfs_crypt_stat *crypt_stat)
1447{
Tyler Hicksfa3ef1c2010-02-11 05:09:14 -06001448 crypt_stat->metadata_size = ECRYPTFS_MINIMUM_HEADER_EXTENT_SIZE;
Michael Halcrow237fead2006-10-04 02:16:22 -07001449}
1450
Tyler Hicks3aeb86e2011-03-15 14:54:00 -05001451void ecryptfs_i_size_init(const char *page_virt, struct inode *inode)
1452{
1453 struct ecryptfs_mount_crypt_stat *mount_crypt_stat;
1454 struct ecryptfs_crypt_stat *crypt_stat;
1455 u64 file_size;
1456
1457 crypt_stat = &ecryptfs_inode_to_private(inode)->crypt_stat;
1458 mount_crypt_stat =
1459 &ecryptfs_superblock_to_private(inode->i_sb)->mount_crypt_stat;
1460 if (mount_crypt_stat->flags & ECRYPTFS_ENCRYPTED_VIEW_ENABLED) {
1461 file_size = i_size_read(ecryptfs_inode_to_lower(inode));
1462 if (crypt_stat->flags & ECRYPTFS_METADATA_IN_XATTR)
1463 file_size += crypt_stat->metadata_size;
1464 } else
1465 file_size = get_unaligned_be64(page_virt);
1466 i_size_write(inode, (loff_t)file_size);
1467 crypt_stat->flags |= ECRYPTFS_I_SIZE_INITIALIZED;
1468}
1469
Michael Halcrow237fead2006-10-04 02:16:22 -07001470/**
1471 * ecryptfs_read_headers_virt
Michael Halcrow22e78fa2007-10-16 01:28:02 -07001472 * @page_virt: The virtual address into which to read the headers
1473 * @crypt_stat: The cryptographic context
1474 * @ecryptfs_dentry: The eCryptfs dentry
1475 * @validate_header_size: Whether to validate the header size while reading
Michael Halcrow237fead2006-10-04 02:16:22 -07001476 *
1477 * Read/parse the header data. The header format is detailed in the
1478 * comment block for the ecryptfs_write_headers_virt() function.
1479 *
1480 * Returns zero on success
1481 */
1482static int ecryptfs_read_headers_virt(char *page_virt,
1483 struct ecryptfs_crypt_stat *crypt_stat,
Michael Halcrowdd2a3b72007-02-12 00:53:46 -08001484 struct dentry *ecryptfs_dentry,
1485 int validate_header_size)
Michael Halcrow237fead2006-10-04 02:16:22 -07001486{
1487 int rc = 0;
1488 int offset;
1489 int bytes_read;
1490
1491 ecryptfs_set_default_sizes(crypt_stat);
1492 crypt_stat->mount_crypt_stat = &ecryptfs_superblock_to_private(
1493 ecryptfs_dentry->d_sb)->mount_crypt_stat;
1494 offset = ECRYPTFS_FILE_SIZE_BYTES;
Tyler Hicks7a866172011-05-02 00:39:54 -05001495 rc = ecryptfs_validate_marker(page_virt + offset);
1496 if (rc)
Michael Halcrow237fead2006-10-04 02:16:22 -07001497 goto out;
Tyler Hicks3aeb86e2011-03-15 14:54:00 -05001498 if (!(crypt_stat->flags & ECRYPTFS_I_SIZE_INITIALIZED))
1499 ecryptfs_i_size_init(page_virt, ecryptfs_dentry->d_inode);
Michael Halcrow237fead2006-10-04 02:16:22 -07001500 offset += MAGIC_ECRYPTFS_MARKER_SIZE_BYTES;
1501 rc = ecryptfs_process_flags(crypt_stat, (page_virt + offset),
1502 &bytes_read);
1503 if (rc) {
1504 ecryptfs_printk(KERN_WARNING, "Error processing flags\n");
1505 goto out;
1506 }
1507 if (crypt_stat->file_version > ECRYPTFS_SUPPORTED_FILE_VERSION) {
1508 ecryptfs_printk(KERN_WARNING, "File version is [%d]; only "
1509 "file version [%d] is supported by this "
1510 "version of eCryptfs\n",
1511 crypt_stat->file_version,
1512 ECRYPTFS_SUPPORTED_FILE_VERSION);
1513 rc = -EINVAL;
1514 goto out;
1515 }
1516 offset += bytes_read;
1517 if (crypt_stat->file_version >= 1) {
1518 rc = parse_header_metadata(crypt_stat, (page_virt + offset),
Michael Halcrowdd2a3b72007-02-12 00:53:46 -08001519 &bytes_read, validate_header_size);
Michael Halcrow237fead2006-10-04 02:16:22 -07001520 if (rc) {
1521 ecryptfs_printk(KERN_WARNING, "Error reading header "
1522 "metadata; rc = [%d]\n", rc);
1523 }
1524 offset += bytes_read;
1525 } else
1526 set_default_header_data(crypt_stat);
1527 rc = ecryptfs_parse_packet_set(crypt_stat, (page_virt + offset),
1528 ecryptfs_dentry);
1529out:
1530 return rc;
1531}
1532
1533/**
Michael Halcrowdd2a3b72007-02-12 00:53:46 -08001534 * ecryptfs_read_xattr_region
Michael Halcrow22e78fa2007-10-16 01:28:02 -07001535 * @page_virt: The vitual address into which to read the xattr data
Michael Halcrow2ed92552007-10-16 01:28:10 -07001536 * @ecryptfs_inode: The eCryptfs inode
Michael Halcrowdd2a3b72007-02-12 00:53:46 -08001537 *
1538 * Attempts to read the crypto metadata from the extended attribute
1539 * region of the lower file.
Michael Halcrow22e78fa2007-10-16 01:28:02 -07001540 *
1541 * Returns zero on success; non-zero on error
Michael Halcrowdd2a3b72007-02-12 00:53:46 -08001542 */
Michael Halcrowd7cdc5f2007-10-16 01:28:10 -07001543int ecryptfs_read_xattr_region(char *page_virt, struct inode *ecryptfs_inode)
Michael Halcrowdd2a3b72007-02-12 00:53:46 -08001544{
Michael Halcrowd7cdc5f2007-10-16 01:28:10 -07001545 struct dentry *lower_dentry =
1546 ecryptfs_inode_to_private(ecryptfs_inode)->lower_file->f_dentry;
Michael Halcrowdd2a3b72007-02-12 00:53:46 -08001547 ssize_t size;
1548 int rc = 0;
1549
Michael Halcrowd7cdc5f2007-10-16 01:28:10 -07001550 size = ecryptfs_getxattr_lower(lower_dentry, ECRYPTFS_XATTR_NAME,
1551 page_virt, ECRYPTFS_DEFAULT_EXTENT_SIZE);
Michael Halcrowdd2a3b72007-02-12 00:53:46 -08001552 if (size < 0) {
Michael Halcrow25bd8172008-02-06 01:38:35 -08001553 if (unlikely(ecryptfs_verbosity > 0))
1554 printk(KERN_INFO "Error attempting to read the [%s] "
1555 "xattr from the lower file; return value = "
1556 "[%zd]\n", ECRYPTFS_XATTR_NAME, size);
Michael Halcrowdd2a3b72007-02-12 00:53:46 -08001557 rc = -EINVAL;
1558 goto out;
1559 }
1560out:
1561 return rc;
1562}
1563
Tyler Hicks778aeb42011-05-24 04:56:23 -05001564int ecryptfs_read_and_validate_xattr_region(struct dentry *dentry,
Tyler Hicks3b06b3e2011-05-24 03:49:02 -05001565 struct inode *inode)
Michael Halcrowdd2a3b72007-02-12 00:53:46 -08001566{
Tyler Hicks778aeb42011-05-24 04:56:23 -05001567 u8 file_size[ECRYPTFS_SIZE_AND_MARKER_BYTES];
1568 u8 *marker = file_size + ECRYPTFS_FILE_SIZE_BYTES;
Michael Halcrowdd2a3b72007-02-12 00:53:46 -08001569 int rc;
1570
Tyler Hicks778aeb42011-05-24 04:56:23 -05001571 rc = ecryptfs_getxattr_lower(ecryptfs_dentry_to_lower(dentry),
1572 ECRYPTFS_XATTR_NAME, file_size,
1573 ECRYPTFS_SIZE_AND_MARKER_BYTES);
1574 if (rc < ECRYPTFS_SIZE_AND_MARKER_BYTES)
1575 return rc >= 0 ? -EINVAL : rc;
1576 rc = ecryptfs_validate_marker(marker);
1577 if (!rc)
1578 ecryptfs_i_size_init(file_size, inode);
Michael Halcrowdd2a3b72007-02-12 00:53:46 -08001579 return rc;
1580}
1581
1582/**
1583 * ecryptfs_read_metadata
1584 *
1585 * Common entry point for reading file metadata. From here, we could
1586 * retrieve the header information from the header region of the file,
1587 * the xattr region of the file, or some other repostory that is
1588 * stored separately from the file itself. The current implementation
1589 * supports retrieving the metadata information from the file contents
1590 * and from the xattr region.
Michael Halcrow237fead2006-10-04 02:16:22 -07001591 *
1592 * Returns zero if valid headers found and parsed; non-zero otherwise
1593 */
Michael Halcrowd7cdc5f2007-10-16 01:28:10 -07001594int ecryptfs_read_metadata(struct dentry *ecryptfs_dentry)
Michael Halcrow237fead2006-10-04 02:16:22 -07001595{
Tim Gardnerbb450362012-01-12 16:31:55 +01001596 int rc;
1597 char *page_virt;
Michael Halcrowd7cdc5f2007-10-16 01:28:10 -07001598 struct inode *ecryptfs_inode = ecryptfs_dentry->d_inode;
Michael Halcrow237fead2006-10-04 02:16:22 -07001599 struct ecryptfs_crypt_stat *crypt_stat =
Michael Halcrowd7cdc5f2007-10-16 01:28:10 -07001600 &ecryptfs_inode_to_private(ecryptfs_inode)->crypt_stat;
Michael Halcrowe77a56d2007-02-12 00:53:47 -08001601 struct ecryptfs_mount_crypt_stat *mount_crypt_stat =
1602 &ecryptfs_superblock_to_private(
1603 ecryptfs_dentry->d_sb)->mount_crypt_stat;
Michael Halcrow237fead2006-10-04 02:16:22 -07001604
Michael Halcrowe77a56d2007-02-12 00:53:47 -08001605 ecryptfs_copy_mount_wide_flags_to_inode_flags(crypt_stat,
1606 mount_crypt_stat);
Michael Halcrow237fead2006-10-04 02:16:22 -07001607 /* Read the first page from the underlying file */
Tyler Hicks30632872011-05-24 05:11:12 -05001608 page_virt = kmem_cache_alloc(ecryptfs_header_cache, GFP_USER);
Michael Halcrow237fead2006-10-04 02:16:22 -07001609 if (!page_virt) {
1610 rc = -ENOMEM;
Michael Halcrowd7cdc5f2007-10-16 01:28:10 -07001611 printk(KERN_ERR "%s: Unable to allocate page_virt\n",
Harvey Harrison18d1dbf2008-04-29 00:59:48 -07001612 __func__);
Michael Halcrow237fead2006-10-04 02:16:22 -07001613 goto out;
1614 }
Michael Halcrowd7cdc5f2007-10-16 01:28:10 -07001615 rc = ecryptfs_read_lower(page_virt, 0, crypt_stat->extent_size,
1616 ecryptfs_inode);
Tyler Hicks96a7b9c2009-09-16 19:04:20 -05001617 if (rc >= 0)
Michael Halcrowd7cdc5f2007-10-16 01:28:10 -07001618 rc = ecryptfs_read_headers_virt(page_virt, crypt_stat,
1619 ecryptfs_dentry,
1620 ECRYPTFS_VALIDATE_HEADER_SIZE);
Michael Halcrow237fead2006-10-04 02:16:22 -07001621 if (rc) {
Tim Gardnerbb450362012-01-12 16:31:55 +01001622 /* metadata is not in the file header, so try xattrs */
Tyler Hicks1984c232010-02-10 23:17:44 -06001623 memset(page_virt, 0, PAGE_CACHE_SIZE);
Michael Halcrowd7cdc5f2007-10-16 01:28:10 -07001624 rc = ecryptfs_read_xattr_region(page_virt, ecryptfs_inode);
Michael Halcrowdd2a3b72007-02-12 00:53:46 -08001625 if (rc) {
1626 printk(KERN_DEBUG "Valid eCryptfs headers not found in "
Tim Gardner30373dc2012-01-12 16:31:55 +01001627 "file header region or xattr region, inode %lu\n",
1628 ecryptfs_inode->i_ino);
Michael Halcrowdd2a3b72007-02-12 00:53:46 -08001629 rc = -EINVAL;
1630 goto out;
1631 }
1632 rc = ecryptfs_read_headers_virt(page_virt, crypt_stat,
1633 ecryptfs_dentry,
1634 ECRYPTFS_DONT_VALIDATE_HEADER_SIZE);
1635 if (rc) {
1636 printk(KERN_DEBUG "Valid eCryptfs headers not found in "
Tim Gardner30373dc2012-01-12 16:31:55 +01001637 "file xattr region either, inode %lu\n",
1638 ecryptfs_inode->i_ino);
Michael Halcrowdd2a3b72007-02-12 00:53:46 -08001639 rc = -EINVAL;
1640 }
1641 if (crypt_stat->mount_crypt_stat->flags
1642 & ECRYPTFS_XATTR_METADATA_ENABLED) {
1643 crypt_stat->flags |= ECRYPTFS_METADATA_IN_XATTR;
1644 } else {
1645 printk(KERN_WARNING "Attempt to access file with "
1646 "crypto metadata only in the extended attribute "
1647 "region, but eCryptfs was mounted without "
1648 "xattr support enabled. eCryptfs will not treat "
Tim Gardner30373dc2012-01-12 16:31:55 +01001649 "this like an encrypted file, inode %lu\n",
1650 ecryptfs_inode->i_ino);
Michael Halcrowdd2a3b72007-02-12 00:53:46 -08001651 rc = -EINVAL;
1652 }
Michael Halcrow237fead2006-10-04 02:16:22 -07001653 }
1654out:
1655 if (page_virt) {
1656 memset(page_virt, 0, PAGE_CACHE_SIZE);
Tyler Hicks30632872011-05-24 05:11:12 -05001657 kmem_cache_free(ecryptfs_header_cache, page_virt);
Michael Halcrow237fead2006-10-04 02:16:22 -07001658 }
1659 return rc;
1660}
1661
1662/**
Michael Halcrow51ca58d2009-01-06 14:41:59 -08001663 * ecryptfs_encrypt_filename - encrypt filename
1664 *
1665 * CBC-encrypts the filename. We do not want to encrypt the same
1666 * filename with the same key and IV, which may happen with hard
1667 * links, so we prepend random bits to each filename.
1668 *
1669 * Returns zero on success; non-zero otherwise
1670 */
1671static int
1672ecryptfs_encrypt_filename(struct ecryptfs_filename *filename,
1673 struct ecryptfs_crypt_stat *crypt_stat,
1674 struct ecryptfs_mount_crypt_stat *mount_crypt_stat)
1675{
1676 int rc = 0;
1677
1678 filename->encrypted_filename = NULL;
1679 filename->encrypted_filename_size = 0;
1680 if ((crypt_stat && (crypt_stat->flags & ECRYPTFS_ENCFN_USE_MOUNT_FNEK))
1681 || (mount_crypt_stat && (mount_crypt_stat->flags
1682 & ECRYPTFS_GLOBAL_ENCFN_USE_MOUNT_FNEK))) {
1683 size_t packet_size;
1684 size_t remaining_bytes;
1685
1686 rc = ecryptfs_write_tag_70_packet(
1687 NULL, NULL,
1688 &filename->encrypted_filename_size,
1689 mount_crypt_stat, NULL,
1690 filename->filename_size);
1691 if (rc) {
1692 printk(KERN_ERR "%s: Error attempting to get packet "
1693 "size for tag 72; rc = [%d]\n", __func__,
1694 rc);
1695 filename->encrypted_filename_size = 0;
1696 goto out;
1697 }
1698 filename->encrypted_filename =
1699 kmalloc(filename->encrypted_filename_size, GFP_KERNEL);
1700 if (!filename->encrypted_filename) {
1701 printk(KERN_ERR "%s: Out of memory whilst attempting "
Michael Halcrowdf261c52009-01-06 14:42:02 -08001702 "to kmalloc [%zd] bytes\n", __func__,
Michael Halcrow51ca58d2009-01-06 14:41:59 -08001703 filename->encrypted_filename_size);
1704 rc = -ENOMEM;
1705 goto out;
1706 }
1707 remaining_bytes = filename->encrypted_filename_size;
1708 rc = ecryptfs_write_tag_70_packet(filename->encrypted_filename,
1709 &remaining_bytes,
1710 &packet_size,
1711 mount_crypt_stat,
1712 filename->filename,
1713 filename->filename_size);
1714 if (rc) {
1715 printk(KERN_ERR "%s: Error attempting to generate "
1716 "tag 70 packet; rc = [%d]\n", __func__,
1717 rc);
1718 kfree(filename->encrypted_filename);
1719 filename->encrypted_filename = NULL;
1720 filename->encrypted_filename_size = 0;
1721 goto out;
1722 }
1723 filename->encrypted_filename_size = packet_size;
1724 } else {
1725 printk(KERN_ERR "%s: No support for requested filename "
1726 "encryption method in this release\n", __func__);
Tyler Hicksdf6ad332009-08-21 04:27:46 -05001727 rc = -EOPNOTSUPP;
Michael Halcrow51ca58d2009-01-06 14:41:59 -08001728 goto out;
1729 }
1730out:
1731 return rc;
1732}
1733
1734static int ecryptfs_copy_filename(char **copied_name, size_t *copied_name_size,
1735 const char *name, size_t name_size)
1736{
1737 int rc = 0;
1738
Tyler Hicksfd9fc842009-02-06 18:06:51 -06001739 (*copied_name) = kmalloc((name_size + 1), GFP_KERNEL);
Michael Halcrow51ca58d2009-01-06 14:41:59 -08001740 if (!(*copied_name)) {
1741 rc = -ENOMEM;
1742 goto out;
1743 }
1744 memcpy((void *)(*copied_name), (void *)name, name_size);
1745 (*copied_name)[(name_size)] = '\0'; /* Only for convenience
1746 * in printing out the
1747 * string in debug
1748 * messages */
Tyler Hicksfd9fc842009-02-06 18:06:51 -06001749 (*copied_name_size) = name_size;
Michael Halcrow51ca58d2009-01-06 14:41:59 -08001750out:
1751 return rc;
1752}
1753
1754/**
Michael Halcrowf4aad162007-10-16 01:27:53 -07001755 * ecryptfs_process_key_cipher - Perform key cipher initialization.
Michael Halcrow237fead2006-10-04 02:16:22 -07001756 * @key_tfm: Crypto context for key material, set by this function
Michael Halcrowe5d9cbd2006-10-30 22:07:16 -08001757 * @cipher_name: Name of the cipher
1758 * @key_size: Size of the key in bytes
Michael Halcrow237fead2006-10-04 02:16:22 -07001759 *
1760 * Returns zero on success. Any crypto_tfm structs allocated here
1761 * should be released by other functions, such as on a superblock put
1762 * event, regardless of whether this function succeeds for fails.
1763 */
Michael Halcrowcd9d67d2007-10-16 01:28:04 -07001764static int
Michael Halcrowf4aad162007-10-16 01:27:53 -07001765ecryptfs_process_key_cipher(struct crypto_blkcipher **key_tfm,
1766 char *cipher_name, size_t *key_size)
Michael Halcrow237fead2006-10-04 02:16:22 -07001767{
1768 char dummy_key[ECRYPTFS_MAX_KEY_BYTES];
Dan Carpenterece550f2010-01-19 12:34:32 +03001769 char *full_alg_name = NULL;
Michael Halcrow237fead2006-10-04 02:16:22 -07001770 int rc;
1771
Michael Halcrowe5d9cbd2006-10-30 22:07:16 -08001772 *key_tfm = NULL;
1773 if (*key_size > ECRYPTFS_MAX_KEY_BYTES) {
Michael Halcrow237fead2006-10-04 02:16:22 -07001774 rc = -EINVAL;
Michael Halcrowdf261c52009-01-06 14:42:02 -08001775 printk(KERN_ERR "Requested key size is [%zd] bytes; maximum "
Michael Halcrowe5d9cbd2006-10-30 22:07:16 -08001776 "allowable is [%d]\n", *key_size, ECRYPTFS_MAX_KEY_BYTES);
Michael Halcrow237fead2006-10-04 02:16:22 -07001777 goto out;
1778 }
Michael Halcrow8bba0662006-10-30 22:07:18 -08001779 rc = ecryptfs_crypto_api_algify_cipher_name(&full_alg_name, cipher_name,
1780 "ecb");
1781 if (rc)
1782 goto out;
1783 *key_tfm = crypto_alloc_blkcipher(full_alg_name, 0, CRYPTO_ALG_ASYNC);
Michael Halcrow8bba0662006-10-30 22:07:18 -08001784 if (IS_ERR(*key_tfm)) {
1785 rc = PTR_ERR(*key_tfm);
Michael Halcrow237fead2006-10-04 02:16:22 -07001786 printk(KERN_ERR "Unable to allocate crypto cipher with name "
Dave Hansen38268492009-08-27 09:47:07 -07001787 "[%s]; rc = [%d]\n", full_alg_name, rc);
Michael Halcrow237fead2006-10-04 02:16:22 -07001788 goto out;
1789 }
Michael Halcrow8bba0662006-10-30 22:07:18 -08001790 crypto_blkcipher_set_flags(*key_tfm, CRYPTO_TFM_REQ_WEAK_KEY);
1791 if (*key_size == 0) {
1792 struct blkcipher_alg *alg = crypto_blkcipher_alg(*key_tfm);
1793
1794 *key_size = alg->max_keysize;
1795 }
Michael Halcrowe5d9cbd2006-10-30 22:07:16 -08001796 get_random_bytes(dummy_key, *key_size);
Michael Halcrow8bba0662006-10-30 22:07:18 -08001797 rc = crypto_blkcipher_setkey(*key_tfm, dummy_key, *key_size);
Michael Halcrow237fead2006-10-04 02:16:22 -07001798 if (rc) {
Michael Halcrowdf261c52009-01-06 14:42:02 -08001799 printk(KERN_ERR "Error attempting to set key of size [%zd] for "
Dave Hansen38268492009-08-27 09:47:07 -07001800 "cipher [%s]; rc = [%d]\n", *key_size, full_alg_name,
1801 rc);
Michael Halcrow237fead2006-10-04 02:16:22 -07001802 rc = -EINVAL;
1803 goto out;
1804 }
1805out:
Dan Carpenterece550f2010-01-19 12:34:32 +03001806 kfree(full_alg_name);
Michael Halcrow237fead2006-10-04 02:16:22 -07001807 return rc;
1808}
Michael Halcrowf4aad162007-10-16 01:27:53 -07001809
1810struct kmem_cache *ecryptfs_key_tfm_cache;
Adrian Bunk7896b632008-02-06 01:38:32 -08001811static struct list_head key_tfm_list;
Eric Sandeenaf440f52008-02-06 01:38:37 -08001812struct mutex key_tfm_list_mutex;
Michael Halcrowf4aad162007-10-16 01:27:53 -07001813
Jerome Marchand7371a382010-08-17 17:24:05 +02001814int __init ecryptfs_init_crypto(void)
Michael Halcrowf4aad162007-10-16 01:27:53 -07001815{
1816 mutex_init(&key_tfm_list_mutex);
1817 INIT_LIST_HEAD(&key_tfm_list);
1818 return 0;
1819}
1820
Eric Sandeenaf440f52008-02-06 01:38:37 -08001821/**
1822 * ecryptfs_destroy_crypto - free all cached key_tfms on key_tfm_list
1823 *
1824 * Called only at module unload time
1825 */
Michael Halcrowfcd12832007-10-16 01:28:01 -07001826int ecryptfs_destroy_crypto(void)
Michael Halcrowf4aad162007-10-16 01:27:53 -07001827{
1828 struct ecryptfs_key_tfm *key_tfm, *key_tfm_tmp;
1829
1830 mutex_lock(&key_tfm_list_mutex);
1831 list_for_each_entry_safe(key_tfm, key_tfm_tmp, &key_tfm_list,
1832 key_tfm_list) {
1833 list_del(&key_tfm->key_tfm_list);
1834 if (key_tfm->key_tfm)
1835 crypto_free_blkcipher(key_tfm->key_tfm);
1836 kmem_cache_free(ecryptfs_key_tfm_cache, key_tfm);
1837 }
1838 mutex_unlock(&key_tfm_list_mutex);
1839 return 0;
1840}
1841
1842int
1843ecryptfs_add_new_key_tfm(struct ecryptfs_key_tfm **key_tfm, char *cipher_name,
1844 size_t key_size)
1845{
1846 struct ecryptfs_key_tfm *tmp_tfm;
1847 int rc = 0;
1848
Eric Sandeenaf440f52008-02-06 01:38:37 -08001849 BUG_ON(!mutex_is_locked(&key_tfm_list_mutex));
1850
Michael Halcrowf4aad162007-10-16 01:27:53 -07001851 tmp_tfm = kmem_cache_alloc(ecryptfs_key_tfm_cache, GFP_KERNEL);
1852 if (key_tfm != NULL)
1853 (*key_tfm) = tmp_tfm;
1854 if (!tmp_tfm) {
1855 rc = -ENOMEM;
1856 printk(KERN_ERR "Error attempting to allocate from "
1857 "ecryptfs_key_tfm_cache\n");
1858 goto out;
1859 }
1860 mutex_init(&tmp_tfm->key_tfm_mutex);
1861 strncpy(tmp_tfm->cipher_name, cipher_name,
1862 ECRYPTFS_MAX_CIPHER_NAME_SIZE);
Eric Sandeenb8862902007-12-22 14:03:24 -08001863 tmp_tfm->cipher_name[ECRYPTFS_MAX_CIPHER_NAME_SIZE] = '\0';
Michael Halcrowf4aad162007-10-16 01:27:53 -07001864 tmp_tfm->key_size = key_size;
Michael Halcrow5dda6992007-10-16 01:28:06 -07001865 rc = ecryptfs_process_key_cipher(&tmp_tfm->key_tfm,
1866 tmp_tfm->cipher_name,
1867 &tmp_tfm->key_size);
1868 if (rc) {
Michael Halcrowf4aad162007-10-16 01:27:53 -07001869 printk(KERN_ERR "Error attempting to initialize key TFM "
1870 "cipher with name = [%s]; rc = [%d]\n",
1871 tmp_tfm->cipher_name, rc);
1872 kmem_cache_free(ecryptfs_key_tfm_cache, tmp_tfm);
1873 if (key_tfm != NULL)
1874 (*key_tfm) = NULL;
1875 goto out;
1876 }
Michael Halcrowf4aad162007-10-16 01:27:53 -07001877 list_add(&tmp_tfm->key_tfm_list, &key_tfm_list);
Michael Halcrowf4aad162007-10-16 01:27:53 -07001878out:
1879 return rc;
1880}
1881
Eric Sandeenaf440f52008-02-06 01:38:37 -08001882/**
1883 * ecryptfs_tfm_exists - Search for existing tfm for cipher_name.
1884 * @cipher_name: the name of the cipher to search for
1885 * @key_tfm: set to corresponding tfm if found
1886 *
1887 * Searches for cached key_tfm matching @cipher_name
1888 * Must be called with &key_tfm_list_mutex held
1889 * Returns 1 if found, with @key_tfm set
1890 * Returns 0 if not found, with @key_tfm set to NULL
1891 */
1892int ecryptfs_tfm_exists(char *cipher_name, struct ecryptfs_key_tfm **key_tfm)
1893{
1894 struct ecryptfs_key_tfm *tmp_key_tfm;
1895
1896 BUG_ON(!mutex_is_locked(&key_tfm_list_mutex));
1897
1898 list_for_each_entry(tmp_key_tfm, &key_tfm_list, key_tfm_list) {
1899 if (strcmp(tmp_key_tfm->cipher_name, cipher_name) == 0) {
1900 if (key_tfm)
1901 (*key_tfm) = tmp_key_tfm;
1902 return 1;
1903 }
1904 }
1905 if (key_tfm)
1906 (*key_tfm) = NULL;
1907 return 0;
1908}
1909
1910/**
1911 * ecryptfs_get_tfm_and_mutex_for_cipher_name
1912 *
1913 * @tfm: set to cached tfm found, or new tfm created
1914 * @tfm_mutex: set to mutex for cached tfm found, or new tfm created
1915 * @cipher_name: the name of the cipher to search for and/or add
1916 *
1917 * Sets pointers to @tfm & @tfm_mutex matching @cipher_name.
1918 * Searches for cached item first, and creates new if not found.
1919 * Returns 0 on success, non-zero if adding new cipher failed
1920 */
Michael Halcrowf4aad162007-10-16 01:27:53 -07001921int ecryptfs_get_tfm_and_mutex_for_cipher_name(struct crypto_blkcipher **tfm,
1922 struct mutex **tfm_mutex,
1923 char *cipher_name)
1924{
1925 struct ecryptfs_key_tfm *key_tfm;
1926 int rc = 0;
1927
1928 (*tfm) = NULL;
1929 (*tfm_mutex) = NULL;
Eric Sandeenaf440f52008-02-06 01:38:37 -08001930
Michael Halcrowf4aad162007-10-16 01:27:53 -07001931 mutex_lock(&key_tfm_list_mutex);
Eric Sandeenaf440f52008-02-06 01:38:37 -08001932 if (!ecryptfs_tfm_exists(cipher_name, &key_tfm)) {
1933 rc = ecryptfs_add_new_key_tfm(&key_tfm, cipher_name, 0);
1934 if (rc) {
1935 printk(KERN_ERR "Error adding new key_tfm to list; "
1936 "rc = [%d]\n", rc);
Michael Halcrowf4aad162007-10-16 01:27:53 -07001937 goto out;
1938 }
1939 }
Michael Halcrowf4aad162007-10-16 01:27:53 -07001940 (*tfm) = key_tfm->key_tfm;
1941 (*tfm_mutex) = &key_tfm->key_tfm_mutex;
1942out:
Cyrill Gorcunov71fd5172008-05-23 13:04:20 -07001943 mutex_unlock(&key_tfm_list_mutex);
Michael Halcrowf4aad162007-10-16 01:27:53 -07001944 return rc;
1945}
Michael Halcrow51ca58d2009-01-06 14:41:59 -08001946
1947/* 64 characters forming a 6-bit target field */
1948static unsigned char *portable_filename_chars = ("-.0123456789ABCD"
1949 "EFGHIJKLMNOPQRST"
1950 "UVWXYZabcdefghij"
1951 "klmnopqrstuvwxyz");
1952
1953/* We could either offset on every reverse map or just pad some 0x00's
1954 * at the front here */
Tyler Hicks0f751e62011-11-23 11:31:24 -06001955static const unsigned char filename_rev_map[256] = {
Michael Halcrow51ca58d2009-01-06 14:41:59 -08001956 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, /* 7 */
1957 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, /* 15 */
1958 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, /* 23 */
1959 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, /* 31 */
1960 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, /* 39 */
1961 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x01, 0x00, /* 47 */
1962 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, /* 55 */
1963 0x0A, 0x0B, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, /* 63 */
1964 0x00, 0x0C, 0x0D, 0x0E, 0x0F, 0x10, 0x11, 0x12, /* 71 */
1965 0x13, 0x14, 0x15, 0x16, 0x17, 0x18, 0x19, 0x1A, /* 79 */
1966 0x1B, 0x1C, 0x1D, 0x1E, 0x1F, 0x20, 0x21, 0x22, /* 87 */
1967 0x23, 0x24, 0x25, 0x00, 0x00, 0x00, 0x00, 0x00, /* 95 */
1968 0x00, 0x26, 0x27, 0x28, 0x29, 0x2A, 0x2B, 0x2C, /* 103 */
1969 0x2D, 0x2E, 0x2F, 0x30, 0x31, 0x32, 0x33, 0x34, /* 111 */
1970 0x35, 0x36, 0x37, 0x38, 0x39, 0x3A, 0x3B, 0x3C, /* 119 */
Tyler Hicks0f751e62011-11-23 11:31:24 -06001971 0x3D, 0x3E, 0x3F /* 123 - 255 initialized to 0x00 */
Michael Halcrow51ca58d2009-01-06 14:41:59 -08001972};
1973
1974/**
1975 * ecryptfs_encode_for_filename
1976 * @dst: Destination location for encoded filename
1977 * @dst_size: Size of the encoded filename in bytes
1978 * @src: Source location for the filename to encode
1979 * @src_size: Size of the source in bytes
1980 */
Cong Ding37028752012-12-07 22:21:56 +00001981static void ecryptfs_encode_for_filename(unsigned char *dst, size_t *dst_size,
Michael Halcrow51ca58d2009-01-06 14:41:59 -08001982 unsigned char *src, size_t src_size)
1983{
1984 size_t num_blocks;
1985 size_t block_num = 0;
1986 size_t dst_offset = 0;
1987 unsigned char last_block[3];
1988
1989 if (src_size == 0) {
1990 (*dst_size) = 0;
1991 goto out;
1992 }
1993 num_blocks = (src_size / 3);
1994 if ((src_size % 3) == 0) {
1995 memcpy(last_block, (&src[src_size - 3]), 3);
1996 } else {
1997 num_blocks++;
1998 last_block[2] = 0x00;
1999 switch (src_size % 3) {
2000 case 1:
2001 last_block[0] = src[src_size - 1];
2002 last_block[1] = 0x00;
2003 break;
2004 case 2:
2005 last_block[0] = src[src_size - 2];
2006 last_block[1] = src[src_size - 1];
2007 }
2008 }
2009 (*dst_size) = (num_blocks * 4);
2010 if (!dst)
2011 goto out;
2012 while (block_num < num_blocks) {
2013 unsigned char *src_block;
2014 unsigned char dst_block[4];
2015
2016 if (block_num == (num_blocks - 1))
2017 src_block = last_block;
2018 else
2019 src_block = &src[block_num * 3];
2020 dst_block[0] = ((src_block[0] >> 2) & 0x3F);
2021 dst_block[1] = (((src_block[0] << 4) & 0x30)
2022 | ((src_block[1] >> 4) & 0x0F));
2023 dst_block[2] = (((src_block[1] << 2) & 0x3C)
2024 | ((src_block[2] >> 6) & 0x03));
2025 dst_block[3] = (src_block[2] & 0x3F);
2026 dst[dst_offset++] = portable_filename_chars[dst_block[0]];
2027 dst[dst_offset++] = portable_filename_chars[dst_block[1]];
2028 dst[dst_offset++] = portable_filename_chars[dst_block[2]];
2029 dst[dst_offset++] = portable_filename_chars[dst_block[3]];
2030 block_num++;
2031 }
2032out:
2033 return;
2034}
2035
Tyler Hicks4a266202011-11-05 13:45:08 -04002036static size_t ecryptfs_max_decoded_size(size_t encoded_size)
2037{
2038 /* Not exact; conservatively long. Every block of 4
2039 * encoded characters decodes into a block of 3
2040 * decoded characters. This segment of code provides
2041 * the caller with the maximum amount of allocated
2042 * space that @dst will need to point to in a
2043 * subsequent call. */
2044 return ((encoded_size + 1) * 3) / 4;
2045}
2046
Michael Halcrow71c11c32009-01-06 14:42:05 -08002047/**
2048 * ecryptfs_decode_from_filename
2049 * @dst: If NULL, this function only sets @dst_size and returns. If
2050 * non-NULL, this function decodes the encoded octets in @src
2051 * into the memory that @dst points to.
2052 * @dst_size: Set to the size of the decoded string.
2053 * @src: The encoded set of octets to decode.
2054 * @src_size: The size of the encoded set of octets to decode.
2055 */
2056static void
2057ecryptfs_decode_from_filename(unsigned char *dst, size_t *dst_size,
2058 const unsigned char *src, size_t src_size)
Michael Halcrow51ca58d2009-01-06 14:41:59 -08002059{
2060 u8 current_bit_offset = 0;
2061 size_t src_byte_offset = 0;
2062 size_t dst_byte_offset = 0;
Michael Halcrow51ca58d2009-01-06 14:41:59 -08002063
2064 if (dst == NULL) {
Tyler Hicks4a266202011-11-05 13:45:08 -04002065 (*dst_size) = ecryptfs_max_decoded_size(src_size);
Michael Halcrow51ca58d2009-01-06 14:41:59 -08002066 goto out;
2067 }
2068 while (src_byte_offset < src_size) {
2069 unsigned char src_byte =
2070 filename_rev_map[(int)src[src_byte_offset]];
2071
2072 switch (current_bit_offset) {
2073 case 0:
2074 dst[dst_byte_offset] = (src_byte << 2);
2075 current_bit_offset = 6;
2076 break;
2077 case 6:
2078 dst[dst_byte_offset++] |= (src_byte >> 4);
2079 dst[dst_byte_offset] = ((src_byte & 0xF)
2080 << 4);
2081 current_bit_offset = 4;
2082 break;
2083 case 4:
2084 dst[dst_byte_offset++] |= (src_byte >> 2);
2085 dst[dst_byte_offset] = (src_byte << 6);
2086 current_bit_offset = 2;
2087 break;
2088 case 2:
2089 dst[dst_byte_offset++] |= (src_byte);
2090 dst[dst_byte_offset] = 0;
2091 current_bit_offset = 0;
2092 break;
2093 }
2094 src_byte_offset++;
2095 }
2096 (*dst_size) = dst_byte_offset;
2097out:
Michael Halcrow71c11c32009-01-06 14:42:05 -08002098 return;
Michael Halcrow51ca58d2009-01-06 14:41:59 -08002099}
2100
2101/**
2102 * ecryptfs_encrypt_and_encode_filename - converts a plaintext file name to cipher text
2103 * @crypt_stat: The crypt_stat struct associated with the file anem to encode
2104 * @name: The plaintext name
2105 * @length: The length of the plaintext
2106 * @encoded_name: The encypted name
2107 *
2108 * Encrypts and encodes a filename into something that constitutes a
2109 * valid filename for a filesystem, with printable characters.
2110 *
2111 * We assume that we have a properly initialized crypto context,
2112 * pointed to by crypt_stat->tfm.
2113 *
2114 * Returns zero on success; non-zero on otherwise
2115 */
2116int ecryptfs_encrypt_and_encode_filename(
2117 char **encoded_name,
2118 size_t *encoded_name_size,
2119 struct ecryptfs_crypt_stat *crypt_stat,
2120 struct ecryptfs_mount_crypt_stat *mount_crypt_stat,
2121 const char *name, size_t name_size)
2122{
2123 size_t encoded_name_no_prefix_size;
2124 int rc = 0;
2125
2126 (*encoded_name) = NULL;
2127 (*encoded_name_size) = 0;
2128 if ((crypt_stat && (crypt_stat->flags & ECRYPTFS_ENCRYPT_FILENAMES))
2129 || (mount_crypt_stat && (mount_crypt_stat->flags
2130 & ECRYPTFS_GLOBAL_ENCRYPT_FILENAMES))) {
2131 struct ecryptfs_filename *filename;
2132
2133 filename = kzalloc(sizeof(*filename), GFP_KERNEL);
2134 if (!filename) {
2135 printk(KERN_ERR "%s: Out of memory whilst attempting "
Michael Halcrowa8f12862009-01-06 14:42:03 -08002136 "to kzalloc [%zd] bytes\n", __func__,
Michael Halcrow51ca58d2009-01-06 14:41:59 -08002137 sizeof(*filename));
2138 rc = -ENOMEM;
2139 goto out;
2140 }
2141 filename->filename = (char *)name;
2142 filename->filename_size = name_size;
2143 rc = ecryptfs_encrypt_filename(filename, crypt_stat,
2144 mount_crypt_stat);
2145 if (rc) {
2146 printk(KERN_ERR "%s: Error attempting to encrypt "
2147 "filename; rc = [%d]\n", __func__, rc);
2148 kfree(filename);
2149 goto out;
2150 }
2151 ecryptfs_encode_for_filename(
2152 NULL, &encoded_name_no_prefix_size,
2153 filename->encrypted_filename,
2154 filename->encrypted_filename_size);
2155 if ((crypt_stat && (crypt_stat->flags
2156 & ECRYPTFS_ENCFN_USE_MOUNT_FNEK))
2157 || (mount_crypt_stat
2158 && (mount_crypt_stat->flags
2159 & ECRYPTFS_GLOBAL_ENCFN_USE_MOUNT_FNEK)))
2160 (*encoded_name_size) =
2161 (ECRYPTFS_FNEK_ENCRYPTED_FILENAME_PREFIX_SIZE
2162 + encoded_name_no_prefix_size);
2163 else
2164 (*encoded_name_size) =
2165 (ECRYPTFS_FEK_ENCRYPTED_FILENAME_PREFIX_SIZE
2166 + encoded_name_no_prefix_size);
2167 (*encoded_name) = kmalloc((*encoded_name_size) + 1, GFP_KERNEL);
2168 if (!(*encoded_name)) {
2169 printk(KERN_ERR "%s: Out of memory whilst attempting "
Michael Halcrowa8f12862009-01-06 14:42:03 -08002170 "to kzalloc [%zd] bytes\n", __func__,
Michael Halcrow51ca58d2009-01-06 14:41:59 -08002171 (*encoded_name_size));
2172 rc = -ENOMEM;
2173 kfree(filename->encrypted_filename);
2174 kfree(filename);
2175 goto out;
2176 }
2177 if ((crypt_stat && (crypt_stat->flags
2178 & ECRYPTFS_ENCFN_USE_MOUNT_FNEK))
2179 || (mount_crypt_stat
2180 && (mount_crypt_stat->flags
2181 & ECRYPTFS_GLOBAL_ENCFN_USE_MOUNT_FNEK))) {
2182 memcpy((*encoded_name),
2183 ECRYPTFS_FNEK_ENCRYPTED_FILENAME_PREFIX,
2184 ECRYPTFS_FNEK_ENCRYPTED_FILENAME_PREFIX_SIZE);
2185 ecryptfs_encode_for_filename(
2186 ((*encoded_name)
2187 + ECRYPTFS_FNEK_ENCRYPTED_FILENAME_PREFIX_SIZE),
2188 &encoded_name_no_prefix_size,
2189 filename->encrypted_filename,
2190 filename->encrypted_filename_size);
2191 (*encoded_name_size) =
2192 (ECRYPTFS_FNEK_ENCRYPTED_FILENAME_PREFIX_SIZE
2193 + encoded_name_no_prefix_size);
2194 (*encoded_name)[(*encoded_name_size)] = '\0';
Michael Halcrow51ca58d2009-01-06 14:41:59 -08002195 } else {
Tyler Hicksdf6ad332009-08-21 04:27:46 -05002196 rc = -EOPNOTSUPP;
Michael Halcrow51ca58d2009-01-06 14:41:59 -08002197 }
2198 if (rc) {
2199 printk(KERN_ERR "%s: Error attempting to encode "
2200 "encrypted filename; rc = [%d]\n", __func__,
2201 rc);
2202 kfree((*encoded_name));
2203 (*encoded_name) = NULL;
2204 (*encoded_name_size) = 0;
2205 }
2206 kfree(filename->encrypted_filename);
2207 kfree(filename);
2208 } else {
2209 rc = ecryptfs_copy_filename(encoded_name,
2210 encoded_name_size,
2211 name, name_size);
2212 }
2213out:
2214 return rc;
2215}
2216
2217/**
2218 * ecryptfs_decode_and_decrypt_filename - converts the encoded cipher text name to decoded plaintext
2219 * @plaintext_name: The plaintext name
2220 * @plaintext_name_size: The plaintext name size
2221 * @ecryptfs_dir_dentry: eCryptfs directory dentry
2222 * @name: The filename in cipher text
2223 * @name_size: The cipher text name size
2224 *
2225 * Decrypts and decodes the filename.
2226 *
2227 * Returns zero on error; non-zero otherwise
2228 */
2229int ecryptfs_decode_and_decrypt_filename(char **plaintext_name,
2230 size_t *plaintext_name_size,
2231 struct dentry *ecryptfs_dir_dentry,
2232 const char *name, size_t name_size)
2233{
Tyler Hicks2aac0cf2009-03-20 02:23:57 -05002234 struct ecryptfs_mount_crypt_stat *mount_crypt_stat =
2235 &ecryptfs_superblock_to_private(
2236 ecryptfs_dir_dentry->d_sb)->mount_crypt_stat;
Michael Halcrow51ca58d2009-01-06 14:41:59 -08002237 char *decoded_name;
2238 size_t decoded_name_size;
2239 size_t packet_size;
2240 int rc = 0;
2241
Tyler Hicks2aac0cf2009-03-20 02:23:57 -05002242 if ((mount_crypt_stat->flags & ECRYPTFS_GLOBAL_ENCRYPT_FILENAMES)
2243 && !(mount_crypt_stat->flags & ECRYPTFS_ENCRYPTED_VIEW_ENABLED)
2244 && (name_size > ECRYPTFS_FNEK_ENCRYPTED_FILENAME_PREFIX_SIZE)
Michael Halcrow51ca58d2009-01-06 14:41:59 -08002245 && (strncmp(name, ECRYPTFS_FNEK_ENCRYPTED_FILENAME_PREFIX,
2246 ECRYPTFS_FNEK_ENCRYPTED_FILENAME_PREFIX_SIZE) == 0)) {
Michael Halcrow51ca58d2009-01-06 14:41:59 -08002247 const char *orig_name = name;
2248 size_t orig_name_size = name_size;
2249
2250 name += ECRYPTFS_FNEK_ENCRYPTED_FILENAME_PREFIX_SIZE;
2251 name_size -= ECRYPTFS_FNEK_ENCRYPTED_FILENAME_PREFIX_SIZE;
Michael Halcrow71c11c32009-01-06 14:42:05 -08002252 ecryptfs_decode_from_filename(NULL, &decoded_name_size,
2253 name, name_size);
Michael Halcrow51ca58d2009-01-06 14:41:59 -08002254 decoded_name = kmalloc(decoded_name_size, GFP_KERNEL);
2255 if (!decoded_name) {
2256 printk(KERN_ERR "%s: Out of memory whilst attempting "
Michael Halcrowdf261c52009-01-06 14:42:02 -08002257 "to kmalloc [%zd] bytes\n", __func__,
Michael Halcrow51ca58d2009-01-06 14:41:59 -08002258 decoded_name_size);
2259 rc = -ENOMEM;
2260 goto out;
2261 }
Michael Halcrow71c11c32009-01-06 14:42:05 -08002262 ecryptfs_decode_from_filename(decoded_name, &decoded_name_size,
2263 name, name_size);
Michael Halcrow51ca58d2009-01-06 14:41:59 -08002264 rc = ecryptfs_parse_tag_70_packet(plaintext_name,
2265 plaintext_name_size,
2266 &packet_size,
2267 mount_crypt_stat,
2268 decoded_name,
2269 decoded_name_size);
2270 if (rc) {
2271 printk(KERN_INFO "%s: Could not parse tag 70 packet "
2272 "from filename; copying through filename "
2273 "as-is\n", __func__);
2274 rc = ecryptfs_copy_filename(plaintext_name,
2275 plaintext_name_size,
2276 orig_name, orig_name_size);
2277 goto out_free;
2278 }
2279 } else {
2280 rc = ecryptfs_copy_filename(plaintext_name,
2281 plaintext_name_size,
2282 name, name_size);
2283 goto out;
2284 }
2285out_free:
2286 kfree(decoded_name);
2287out:
2288 return rc;
2289}
Tyler Hicks4a266202011-11-05 13:45:08 -04002290
2291#define ENC_NAME_MAX_BLOCKLEN_8_OR_16 143
2292
2293int ecryptfs_set_f_namelen(long *namelen, long lower_namelen,
2294 struct ecryptfs_mount_crypt_stat *mount_crypt_stat)
2295{
2296 struct blkcipher_desc desc;
2297 struct mutex *tfm_mutex;
2298 size_t cipher_blocksize;
2299 int rc;
2300
2301 if (!(mount_crypt_stat->flags & ECRYPTFS_GLOBAL_ENCRYPT_FILENAMES)) {
2302 (*namelen) = lower_namelen;
2303 return 0;
2304 }
2305
2306 rc = ecryptfs_get_tfm_and_mutex_for_cipher_name(&desc.tfm, &tfm_mutex,
2307 mount_crypt_stat->global_default_fn_cipher_name);
2308 if (unlikely(rc)) {
2309 (*namelen) = 0;
2310 return rc;
2311 }
2312
2313 mutex_lock(tfm_mutex);
2314 cipher_blocksize = crypto_blkcipher_blocksize(desc.tfm);
2315 mutex_unlock(tfm_mutex);
2316
2317 /* Return an exact amount for the common cases */
2318 if (lower_namelen == NAME_MAX
2319 && (cipher_blocksize == 8 || cipher_blocksize == 16)) {
2320 (*namelen) = ENC_NAME_MAX_BLOCKLEN_8_OR_16;
2321 return 0;
2322 }
2323
2324 /* Return a safe estimate for the uncommon cases */
2325 (*namelen) = lower_namelen;
2326 (*namelen) -= ECRYPTFS_FNEK_ENCRYPTED_FILENAME_PREFIX_SIZE;
2327 /* Since this is the max decoded size, subtract 1 "decoded block" len */
2328 (*namelen) = ecryptfs_max_decoded_size(*namelen) - 3;
2329 (*namelen) -= ECRYPTFS_TAG_70_MAX_METADATA_SIZE;
2330 (*namelen) -= ECRYPTFS_FILENAME_MIN_RANDOM_PREPEND_BYTES;
2331 /* Worst case is that the filename is padded nearly a full block size */
2332 (*namelen) -= cipher_blocksize - 1;
2333
2334 if ((*namelen) < 0)
2335 (*namelen) = 0;
2336
2337 return 0;
2338}