blob: fb54a0182f2ea2c70b968a26de1ef88c329d56f3 [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
Tyler Hicks00a69942013-04-05 23:26:22 -070040#define DECRYPT 0
41#define ENCRYPT 1
42
Michael Halcrow237fead2006-10-04 02:16:22 -070043static int
44ecryptfs_decrypt_page_offset(struct ecryptfs_crypt_stat *crypt_stat,
Tyler Hicks28916d12013-04-15 16:37:27 -070045 struct page *dst_page, struct page *src_page,
46 int offset, int size, unsigned char *iv);
Michael Halcrow237fead2006-10-04 02:16:22 -070047static int
48ecryptfs_encrypt_page_offset(struct ecryptfs_crypt_stat *crypt_stat,
Tyler Hicks28916d12013-04-15 16:37:27 -070049 struct page *dst_page, struct page *src_page,
50 int offset, int size, unsigned char *iv);
Michael Halcrow237fead2006-10-04 02:16:22 -070051
52/**
53 * ecryptfs_to_hex
54 * @dst: Buffer to take hex character representation of contents of
55 * src; must be at least of size (src_size * 2)
56 * @src: Buffer to be converted to a hex string respresentation
57 * @src_size: number of bytes to convert
58 */
59void ecryptfs_to_hex(char *dst, char *src, size_t src_size)
60{
61 int x;
62
63 for (x = 0; x < src_size; x++)
64 sprintf(&dst[x * 2], "%.2x", (unsigned char)src[x]);
65}
66
67/**
68 * ecryptfs_from_hex
69 * @dst: Buffer to take the bytes from src hex; must be at least of
70 * size (src_size / 2)
71 * @src: Buffer to be converted from a hex string respresentation to raw value
72 * @dst_size: size of dst buffer, or number of hex characters pairs to convert
73 */
74void ecryptfs_from_hex(char *dst, char *src, int dst_size)
75{
76 int x;
77 char tmp[3] = { 0, };
78
79 for (x = 0; x < dst_size; x++) {
80 tmp[0] = src[x * 2];
81 tmp[1] = src[x * 2 + 1];
82 dst[x] = (unsigned char)simple_strtol(tmp, NULL, 16);
83 }
84}
85
86/**
87 * ecryptfs_calculate_md5 - calculates the md5 of @src
88 * @dst: Pointer to 16 bytes of allocated memory
89 * @crypt_stat: Pointer to crypt_stat struct for the current inode
90 * @src: Data to be md5'd
91 * @len: Length of @src
92 *
93 * Uses the allocated crypto context that crypt_stat references to
94 * generate the MD5 sum of the contents of src.
95 */
96static int ecryptfs_calculate_md5(char *dst,
97 struct ecryptfs_crypt_stat *crypt_stat,
98 char *src, int len)
99{
Michael Halcrow237fead2006-10-04 02:16:22 -0700100 struct scatterlist sg;
Michael Halcrow565d97242006-10-30 22:07:17 -0800101 struct hash_desc desc = {
102 .tfm = crypt_stat->hash_tfm,
103 .flags = CRYPTO_TFM_REQ_MAY_SLEEP
104 };
105 int rc = 0;
Michael Halcrow237fead2006-10-04 02:16:22 -0700106
Michael Halcrow565d97242006-10-30 22:07:17 -0800107 mutex_lock(&crypt_stat->cs_hash_tfm_mutex);
Michael Halcrow237fead2006-10-04 02:16:22 -0700108 sg_init_one(&sg, (u8 *)src, len);
Michael Halcrow565d97242006-10-30 22:07:17 -0800109 if (!desc.tfm) {
110 desc.tfm = crypto_alloc_hash(ECRYPTFS_DEFAULT_HASH, 0,
111 CRYPTO_ALG_ASYNC);
112 if (IS_ERR(desc.tfm)) {
113 rc = PTR_ERR(desc.tfm);
Michael Halcrow237fead2006-10-04 02:16:22 -0700114 ecryptfs_printk(KERN_ERR, "Error attempting to "
Michael Halcrow565d97242006-10-30 22:07:17 -0800115 "allocate crypto context; rc = [%d]\n",
116 rc);
Michael Halcrow237fead2006-10-04 02:16:22 -0700117 goto out;
118 }
Michael Halcrow565d97242006-10-30 22:07:17 -0800119 crypt_stat->hash_tfm = desc.tfm;
Michael Halcrow237fead2006-10-04 02:16:22 -0700120 }
Michael Halcrow8a29f2b2007-11-05 14:51:04 -0800121 rc = crypto_hash_init(&desc);
122 if (rc) {
123 printk(KERN_ERR
124 "%s: Error initializing crypto hash; rc = [%d]\n",
Harvey Harrison18d1dbf2008-04-29 00:59:48 -0700125 __func__, rc);
Michael Halcrow8a29f2b2007-11-05 14:51:04 -0800126 goto out;
127 }
128 rc = crypto_hash_update(&desc, &sg, len);
129 if (rc) {
130 printk(KERN_ERR
131 "%s: Error updating crypto hash; rc = [%d]\n",
Harvey Harrison18d1dbf2008-04-29 00:59:48 -0700132 __func__, rc);
Michael Halcrow8a29f2b2007-11-05 14:51:04 -0800133 goto out;
134 }
135 rc = crypto_hash_final(&desc, dst);
136 if (rc) {
137 printk(KERN_ERR
138 "%s: Error finalizing crypto hash; rc = [%d]\n",
Harvey Harrison18d1dbf2008-04-29 00:59:48 -0700139 __func__, rc);
Michael Halcrow8a29f2b2007-11-05 14:51:04 -0800140 goto out;
141 }
Michael Halcrow237fead2006-10-04 02:16:22 -0700142out:
Michael Halcrow8a29f2b2007-11-05 14:51:04 -0800143 mutex_unlock(&crypt_stat->cs_hash_tfm_mutex);
Michael Halcrow237fead2006-10-04 02:16:22 -0700144 return rc;
145}
146
Michael Halcrowcd9d67d2007-10-16 01:28:04 -0700147static int ecryptfs_crypto_api_algify_cipher_name(char **algified_name,
148 char *cipher_name,
149 char *chaining_modifier)
Michael Halcrow8bba0662006-10-30 22:07:18 -0800150{
151 int cipher_name_len = strlen(cipher_name);
152 int chaining_modifier_len = strlen(chaining_modifier);
153 int algified_name_len;
154 int rc;
155
156 algified_name_len = (chaining_modifier_len + cipher_name_len + 3);
157 (*algified_name) = kmalloc(algified_name_len, GFP_KERNEL);
Michael Halcrow7bd473f2006-11-02 22:06:56 -0800158 if (!(*algified_name)) {
Michael Halcrow8bba0662006-10-30 22:07:18 -0800159 rc = -ENOMEM;
160 goto out;
161 }
162 snprintf((*algified_name), algified_name_len, "%s(%s)",
163 chaining_modifier, cipher_name);
164 rc = 0;
165out:
166 return rc;
167}
168
Michael Halcrow237fead2006-10-04 02:16:22 -0700169/**
170 * ecryptfs_derive_iv
171 * @iv: destination for the derived iv vale
172 * @crypt_stat: Pointer to crypt_stat struct for the current inode
Michael Halcrowd6a13c12007-10-16 01:28:12 -0700173 * @offset: Offset of the extent whose IV we are to derive
Michael Halcrow237fead2006-10-04 02:16:22 -0700174 *
175 * Generate the initialization vector from the given root IV and page
176 * offset.
177 *
178 * Returns zero on success; non-zero on error.
179 */
Michael Halcrowa34f60f2009-01-06 14:41:58 -0800180int ecryptfs_derive_iv(char *iv, struct ecryptfs_crypt_stat *crypt_stat,
181 loff_t offset)
Michael Halcrow237fead2006-10-04 02:16:22 -0700182{
183 int rc = 0;
184 char dst[MD5_DIGEST_SIZE];
185 char src[ECRYPTFS_MAX_IV_BYTES + 16];
186
187 if (unlikely(ecryptfs_verbosity > 0)) {
188 ecryptfs_printk(KERN_DEBUG, "root iv:\n");
189 ecryptfs_dump_hex(crypt_stat->root_iv, crypt_stat->iv_bytes);
190 }
191 /* TODO: It is probably secure to just cast the least
192 * significant bits of the root IV into an unsigned long and
193 * add the offset to that rather than go through all this
194 * hashing business. -Halcrow */
195 memcpy(src, crypt_stat->root_iv, crypt_stat->iv_bytes);
196 memset((src + crypt_stat->iv_bytes), 0, 16);
Michael Halcrowd6a13c12007-10-16 01:28:12 -0700197 snprintf((src + crypt_stat->iv_bytes), 16, "%lld", offset);
Michael Halcrow237fead2006-10-04 02:16:22 -0700198 if (unlikely(ecryptfs_verbosity > 0)) {
199 ecryptfs_printk(KERN_DEBUG, "source:\n");
200 ecryptfs_dump_hex(src, (crypt_stat->iv_bytes + 16));
201 }
202 rc = ecryptfs_calculate_md5(dst, crypt_stat, src,
203 (crypt_stat->iv_bytes + 16));
204 if (rc) {
205 ecryptfs_printk(KERN_WARNING, "Error attempting to compute "
206 "MD5 while generating IV for a page\n");
207 goto out;
208 }
209 memcpy(iv, dst, crypt_stat->iv_bytes);
210 if (unlikely(ecryptfs_verbosity > 0)) {
211 ecryptfs_printk(KERN_DEBUG, "derived iv:\n");
212 ecryptfs_dump_hex(iv, crypt_stat->iv_bytes);
213 }
214out:
215 return rc;
216}
217
218/**
219 * ecryptfs_init_crypt_stat
220 * @crypt_stat: Pointer to the crypt_stat struct to initialize.
221 *
222 * Initialize the crypt_stat structure.
223 */
224void
225ecryptfs_init_crypt_stat(struct ecryptfs_crypt_stat *crypt_stat)
226{
227 memset((void *)crypt_stat, 0, sizeof(struct ecryptfs_crypt_stat));
Michael Halcrowf4aad162007-10-16 01:27:53 -0700228 INIT_LIST_HEAD(&crypt_stat->keysig_list);
229 mutex_init(&crypt_stat->keysig_list_mutex);
Michael Halcrow237fead2006-10-04 02:16:22 -0700230 mutex_init(&crypt_stat->cs_mutex);
231 mutex_init(&crypt_stat->cs_tfm_mutex);
Michael Halcrow565d97242006-10-30 22:07:17 -0800232 mutex_init(&crypt_stat->cs_hash_tfm_mutex);
Michael Halcrowe2bd99e2007-02-12 00:53:49 -0800233 crypt_stat->flags |= ECRYPTFS_STRUCT_INITIALIZED;
Michael Halcrow237fead2006-10-04 02:16:22 -0700234}
235
236/**
Michael Halcrowfcd12832007-10-16 01:28:01 -0700237 * ecryptfs_destroy_crypt_stat
Michael Halcrow237fead2006-10-04 02:16:22 -0700238 * @crypt_stat: Pointer to the crypt_stat struct to initialize.
239 *
240 * Releases all memory associated with a crypt_stat struct.
241 */
Michael Halcrowfcd12832007-10-16 01:28:01 -0700242void ecryptfs_destroy_crypt_stat(struct ecryptfs_crypt_stat *crypt_stat)
Michael Halcrow237fead2006-10-04 02:16:22 -0700243{
Michael Halcrowf4aad162007-10-16 01:27:53 -0700244 struct ecryptfs_key_sig *key_sig, *key_sig_tmp;
245
Michael Halcrow237fead2006-10-04 02:16:22 -0700246 if (crypt_stat->tfm)
Tyler Hicks4dfea4f2013-05-09 16:55:07 -0700247 crypto_free_ablkcipher(crypt_stat->tfm);
Michael Halcrow565d97242006-10-30 22:07:17 -0800248 if (crypt_stat->hash_tfm)
249 crypto_free_hash(crypt_stat->hash_tfm);
Michael Halcrowf4aad162007-10-16 01:27:53 -0700250 list_for_each_entry_safe(key_sig, key_sig_tmp,
251 &crypt_stat->keysig_list, crypt_stat_list) {
252 list_del(&key_sig->crypt_stat_list);
253 kmem_cache_free(ecryptfs_key_sig_cache, key_sig);
254 }
Michael Halcrow237fead2006-10-04 02:16:22 -0700255 memset(crypt_stat, 0, sizeof(struct ecryptfs_crypt_stat));
256}
257
Michael Halcrowfcd12832007-10-16 01:28:01 -0700258void ecryptfs_destroy_mount_crypt_stat(
Michael Halcrow237fead2006-10-04 02:16:22 -0700259 struct ecryptfs_mount_crypt_stat *mount_crypt_stat)
260{
Michael Halcrowf4aad162007-10-16 01:27:53 -0700261 struct ecryptfs_global_auth_tok *auth_tok, *auth_tok_tmp;
262
263 if (!(mount_crypt_stat->flags & ECRYPTFS_MOUNT_CRYPT_STAT_INITIALIZED))
264 return;
265 mutex_lock(&mount_crypt_stat->global_auth_tok_list_mutex);
266 list_for_each_entry_safe(auth_tok, auth_tok_tmp,
267 &mount_crypt_stat->global_auth_tok_list,
268 mount_crypt_stat_list) {
269 list_del(&auth_tok->mount_crypt_stat_list);
Michael Halcrowf4aad162007-10-16 01:27:53 -0700270 if (auth_tok->global_auth_tok_key
271 && !(auth_tok->flags & ECRYPTFS_AUTH_TOK_INVALID))
272 key_put(auth_tok->global_auth_tok_key);
273 kmem_cache_free(ecryptfs_global_auth_tok_cache, auth_tok);
274 }
275 mutex_unlock(&mount_crypt_stat->global_auth_tok_list_mutex);
Michael Halcrow237fead2006-10-04 02:16:22 -0700276 memset(mount_crypt_stat, 0, sizeof(struct ecryptfs_mount_crypt_stat));
277}
278
279/**
280 * virt_to_scatterlist
281 * @addr: Virtual address
282 * @size: Size of data; should be an even multiple of the block size
283 * @sg: Pointer to scatterlist array; set to NULL to obtain only
284 * the number of scatterlist structs required in array
285 * @sg_size: Max array size
286 *
287 * Fills in a scatterlist array with page references for a passed
288 * virtual address.
289 *
290 * Returns the number of scatterlist structs in array used
291 */
292int virt_to_scatterlist(const void *addr, int size, struct scatterlist *sg,
293 int sg_size)
294{
295 int i = 0;
296 struct page *pg;
297 int offset;
298 int remainder_of_page;
299
Herbert Xu68e3f5d2007-10-27 00:52:07 -0700300 sg_init_table(sg, sg_size);
301
Michael Halcrow237fead2006-10-04 02:16:22 -0700302 while (size > 0 && i < sg_size) {
303 pg = virt_to_page(addr);
304 offset = offset_in_page(addr);
Dan Carpentera07c48a2013-01-26 10:48:42 +0300305 sg_set_page(&sg[i], pg, 0, offset);
Michael Halcrow237fead2006-10-04 02:16:22 -0700306 remainder_of_page = PAGE_CACHE_SIZE - offset;
307 if (size >= remainder_of_page) {
Dan Carpentera07c48a2013-01-26 10:48:42 +0300308 sg[i].length = remainder_of_page;
Michael Halcrow237fead2006-10-04 02:16:22 -0700309 addr += remainder_of_page;
310 size -= remainder_of_page;
311 } else {
Dan Carpentera07c48a2013-01-26 10:48:42 +0300312 sg[i].length = size;
Michael Halcrow237fead2006-10-04 02:16:22 -0700313 addr += size;
314 size = 0;
315 }
316 i++;
317 }
318 if (size > 0)
319 return -ENOMEM;
320 return i;
321}
322
Tyler Hicks4dfea4f2013-05-09 16:55:07 -0700323struct extent_crypt_result {
324 struct completion completion;
325 int rc;
326};
327
328static void extent_crypt_complete(struct crypto_async_request *req, int rc)
329{
330 struct extent_crypt_result *ecr = req->data;
331
332 if (rc == -EINPROGRESS)
333 return;
334
335 ecr->rc = rc;
336 complete(&ecr->completion);
337}
338
Michael Halcrow237fead2006-10-04 02:16:22 -0700339/**
Tyler Hicks00a69942013-04-05 23:26:22 -0700340 * crypt_scatterlist
Michael Halcrow237fead2006-10-04 02:16:22 -0700341 * @crypt_stat: Pointer to the crypt_stat struct to initialize.
Tyler Hicks00a69942013-04-05 23:26:22 -0700342 * @dest_sg: Destination of the data after performing the crypto operation
343 * @src_sg: Data to be encrypted or decrypted
344 * @size: Length of data
345 * @iv: IV to use
346 * @op: ENCRYPT or DECRYPT to indicate the desired operation
Michael Halcrow237fead2006-10-04 02:16:22 -0700347 *
Tyler Hicks00a69942013-04-05 23:26:22 -0700348 * Returns the number of bytes encrypted or decrypted; negative value on error
Michael Halcrow237fead2006-10-04 02:16:22 -0700349 */
Tyler Hicks00a69942013-04-05 23:26:22 -0700350static int crypt_scatterlist(struct ecryptfs_crypt_stat *crypt_stat,
351 struct scatterlist *dest_sg,
352 struct scatterlist *src_sg, int size,
353 unsigned char *iv, int op)
Michael Halcrow237fead2006-10-04 02:16:22 -0700354{
Tyler Hicks4dfea4f2013-05-09 16:55:07 -0700355 struct ablkcipher_request *req = NULL;
356 struct extent_crypt_result ecr;
Michael Halcrow237fead2006-10-04 02:16:22 -0700357 int rc = 0;
358
359 BUG_ON(!crypt_stat || !crypt_stat->tfm
Michael Halcrowe2bd99e2007-02-12 00:53:49 -0800360 || !(crypt_stat->flags & ECRYPTFS_STRUCT_INITIALIZED));
Michael Halcrow237fead2006-10-04 02:16:22 -0700361 if (unlikely(ecryptfs_verbosity > 0)) {
Tyler Hicksf24b3882010-11-15 17:36:38 -0600362 ecryptfs_printk(KERN_DEBUG, "Key size [%zd]; key:\n",
Michael Halcrow237fead2006-10-04 02:16:22 -0700363 crypt_stat->key_size);
364 ecryptfs_dump_hex(crypt_stat->key,
365 crypt_stat->key_size);
366 }
Tyler Hicks4dfea4f2013-05-09 16:55:07 -0700367
368 init_completion(&ecr.completion);
369
Michael Halcrow237fead2006-10-04 02:16:22 -0700370 mutex_lock(&crypt_stat->cs_tfm_mutex);
Tyler Hicks4dfea4f2013-05-09 16:55:07 -0700371 req = ablkcipher_request_alloc(crypt_stat->tfm, GFP_NOFS);
372 if (!req) {
Michael Halcrow237fead2006-10-04 02:16:22 -0700373 mutex_unlock(&crypt_stat->cs_tfm_mutex);
Tyler Hicks4dfea4f2013-05-09 16:55:07 -0700374 rc = -ENOMEM;
Michael Halcrow237fead2006-10-04 02:16:22 -0700375 goto out;
376 }
Tyler Hicks4dfea4f2013-05-09 16:55:07 -0700377
378 ablkcipher_request_set_callback(req,
379 CRYPTO_TFM_REQ_MAY_BACKLOG | CRYPTO_TFM_REQ_MAY_SLEEP,
380 extent_crypt_complete, &ecr);
381 /* Consider doing this once, when the file is opened */
382 if (!(crypt_stat->flags & ECRYPTFS_KEY_SET)) {
383 rc = crypto_ablkcipher_setkey(crypt_stat->tfm, crypt_stat->key,
384 crypt_stat->key_size);
385 if (rc) {
386 ecryptfs_printk(KERN_ERR,
387 "Error setting key; rc = [%d]\n",
388 rc);
389 mutex_unlock(&crypt_stat->cs_tfm_mutex);
390 rc = -EINVAL;
391 goto out;
392 }
393 crypt_stat->flags |= ECRYPTFS_KEY_SET;
394 }
Michael Halcrow237fead2006-10-04 02:16:22 -0700395 mutex_unlock(&crypt_stat->cs_tfm_mutex);
Tyler Hicks4dfea4f2013-05-09 16:55:07 -0700396 ablkcipher_request_set_crypt(req, src_sg, dest_sg, size, iv);
Tyler Hicks00a69942013-04-05 23:26:22 -0700397 rc = op == ENCRYPT ? crypto_ablkcipher_encrypt(req) :
398 crypto_ablkcipher_decrypt(req);
Tyler Hicks4dfea4f2013-05-09 16:55:07 -0700399 if (rc == -EINPROGRESS || rc == -EBUSY) {
400 struct extent_crypt_result *ecr = req->base.data;
401
402 wait_for_completion(&ecr->completion);
403 rc = ecr->rc;
404 INIT_COMPLETION(ecr->completion);
405 }
Michael Halcrow237fead2006-10-04 02:16:22 -0700406out:
Tyler Hicks4dfea4f2013-05-09 16:55:07 -0700407 ablkcipher_request_free(req);
Michael Halcrow237fead2006-10-04 02:16:22 -0700408 return rc;
409}
410
Michael Halcrow237fead2006-10-04 02:16:22 -0700411/**
Tyler Hicks24d15262013-04-15 17:28:09 -0700412 * lower_offset_for_page
Michael Halcrow0216f7f2007-10-16 01:28:08 -0700413 *
414 * Convert an eCryptfs page index into a lower byte offset
415 */
Tyler Hicks24d15262013-04-15 17:28:09 -0700416static loff_t lower_offset_for_page(struct ecryptfs_crypt_stat *crypt_stat,
417 struct page *page)
Michael Halcrow0216f7f2007-10-16 01:28:08 -0700418{
Tyler Hicks24d15262013-04-15 17:28:09 -0700419 return ecryptfs_lower_header_size(crypt_stat) +
420 (page->index << PAGE_CACHE_SHIFT);
Michael Halcrow0216f7f2007-10-16 01:28:08 -0700421}
422
423/**
424 * ecryptfs_encrypt_extent
425 * @enc_extent_page: Allocated page into which to encrypt the data in
426 * @page
427 * @crypt_stat: crypt_stat containing cryptographic context for the
428 * encryption operation
429 * @page: Page containing plaintext data extent to encrypt
430 * @extent_offset: Page extent offset for use in generating IV
431 *
432 * Encrypts one extent of data.
433 *
434 * Return zero on success; non-zero otherwise
435 */
436static int ecryptfs_encrypt_extent(struct page *enc_extent_page,
437 struct ecryptfs_crypt_stat *crypt_stat,
438 struct page *page,
439 unsigned long extent_offset)
440{
Michael Halcrowd6a13c12007-10-16 01:28:12 -0700441 loff_t extent_base;
Michael Halcrow0216f7f2007-10-16 01:28:08 -0700442 char extent_iv[ECRYPTFS_MAX_IV_BYTES];
443 int rc;
444
Michael Halcrowd6a13c12007-10-16 01:28:12 -0700445 extent_base = (((loff_t)page->index)
Michael Halcrow0216f7f2007-10-16 01:28:08 -0700446 * (PAGE_CACHE_SIZE / crypt_stat->extent_size));
447 rc = ecryptfs_derive_iv(extent_iv, crypt_stat,
448 (extent_base + extent_offset));
449 if (rc) {
Joe Perches888d57b2010-11-10 15:46:16 -0800450 ecryptfs_printk(KERN_ERR, "Error attempting to derive IV for "
451 "extent [0x%.16llx]; rc = [%d]\n",
452 (unsigned long long)(extent_base + extent_offset), rc);
Michael Halcrow0216f7f2007-10-16 01:28:08 -0700453 goto out;
454 }
Tyler Hicks28916d12013-04-15 16:37:27 -0700455 rc = ecryptfs_encrypt_page_offset(crypt_stat, enc_extent_page, page,
Tyler Hicks12003e52013-04-16 23:21:24 -0700456 extent_offset * crypt_stat->extent_size,
457 crypt_stat->extent_size, extent_iv);
Michael Halcrow0216f7f2007-10-16 01:28:08 -0700458 if (rc < 0) {
459 printk(KERN_ERR "%s: Error attempting to encrypt page with "
460 "page->index = [%ld], extent_offset = [%ld]; "
Harvey Harrison18d1dbf2008-04-29 00:59:48 -0700461 "rc = [%d]\n", __func__, page->index, extent_offset,
Michael Halcrow0216f7f2007-10-16 01:28:08 -0700462 rc);
463 goto out;
464 }
465 rc = 0;
Michael Halcrow0216f7f2007-10-16 01:28:08 -0700466out:
467 return rc;
468}
469
470/**
Michael Halcrow237fead2006-10-04 02:16:22 -0700471 * ecryptfs_encrypt_page
Michael Halcrow0216f7f2007-10-16 01:28:08 -0700472 * @page: Page mapped from the eCryptfs inode for the file; contains
473 * decrypted content that needs to be encrypted (to a temporary
474 * page; not in place) and written out to the lower file
Michael Halcrow237fead2006-10-04 02:16:22 -0700475 *
476 * Encrypt an eCryptfs page. This is done on a per-extent basis. Note
477 * that eCryptfs pages may straddle the lower pages -- for instance,
478 * if the file was created on a machine with an 8K page size
479 * (resulting in an 8K header), and then the file is copied onto a
480 * host with a 32K page size, then when reading page 0 of the eCryptfs
481 * file, 24K of page 0 of the lower file will be read and decrypted,
482 * and then 8K of page 1 of the lower file will be read and decrypted.
483 *
Michael Halcrow237fead2006-10-04 02:16:22 -0700484 * Returns zero on success; negative on error
485 */
Michael Halcrow0216f7f2007-10-16 01:28:08 -0700486int ecryptfs_encrypt_page(struct page *page)
Michael Halcrow237fead2006-10-04 02:16:22 -0700487{
Michael Halcrow0216f7f2007-10-16 01:28:08 -0700488 struct inode *ecryptfs_inode;
Michael Halcrow237fead2006-10-04 02:16:22 -0700489 struct ecryptfs_crypt_stat *crypt_stat;
Eric Sandeen7fcba052008-07-28 15:46:39 -0700490 char *enc_extent_virt;
491 struct page *enc_extent_page = NULL;
Michael Halcrow0216f7f2007-10-16 01:28:08 -0700492 loff_t extent_offset;
Tyler Hicks0f896172013-04-15 16:16:24 -0700493 loff_t lower_offset;
Michael Halcrow237fead2006-10-04 02:16:22 -0700494 int rc = 0;
Michael Halcrow237fead2006-10-04 02:16:22 -0700495
Michael Halcrow0216f7f2007-10-16 01:28:08 -0700496 ecryptfs_inode = page->mapping->host;
497 crypt_stat =
498 &(ecryptfs_inode_to_private(ecryptfs_inode)->crypt_stat);
Tyler Hicks13a791b2009-04-13 15:29:27 -0500499 BUG_ON(!(crypt_stat->flags & ECRYPTFS_ENCRYPTED));
Eric Sandeen7fcba052008-07-28 15:46:39 -0700500 enc_extent_page = alloc_page(GFP_USER);
501 if (!enc_extent_page) {
Michael Halcrow0216f7f2007-10-16 01:28:08 -0700502 rc = -ENOMEM;
503 ecryptfs_printk(KERN_ERR, "Error allocating memory for "
504 "encrypted extent\n");
505 goto out;
506 }
Tyler Hicks0f896172013-04-15 16:16:24 -0700507
Michael Halcrow0216f7f2007-10-16 01:28:08 -0700508 for (extent_offset = 0;
509 extent_offset < (PAGE_CACHE_SIZE / crypt_stat->extent_size);
510 extent_offset++) {
Michael Halcrow0216f7f2007-10-16 01:28:08 -0700511 rc = ecryptfs_encrypt_extent(enc_extent_page, crypt_stat, page,
512 extent_offset);
Michael Halcrow237fead2006-10-04 02:16:22 -0700513 if (rc) {
Michael Halcrow0216f7f2007-10-16 01:28:08 -0700514 printk(KERN_ERR "%s: Error encrypting extent; "
Harvey Harrison18d1dbf2008-04-29 00:59:48 -0700515 "rc = [%d]\n", __func__, rc);
Michael Halcrow237fead2006-10-04 02:16:22 -0700516 goto out;
517 }
Tyler Hicks0f896172013-04-15 16:16:24 -0700518 }
519
Tyler Hicks24d15262013-04-15 17:28:09 -0700520 lower_offset = lower_offset_for_page(crypt_stat, page);
Tyler Hicks0f896172013-04-15 16:16:24 -0700521 enc_extent_virt = kmap(enc_extent_page);
522 rc = ecryptfs_write_lower(ecryptfs_inode, enc_extent_virt, lower_offset,
523 PAGE_CACHE_SIZE);
524 kunmap(enc_extent_page);
525 if (rc < 0) {
526 ecryptfs_printk(KERN_ERR,
527 "Error attempting to write lower page; rc = [%d]\n",
528 rc);
529 goto out;
Michael Halcrow237fead2006-10-04 02:16:22 -0700530 }
Tyler Hicks96a7b9c2009-09-16 19:04:20 -0500531 rc = 0;
Michael Halcrow0216f7f2007-10-16 01:28:08 -0700532out:
Eric Sandeen7fcba052008-07-28 15:46:39 -0700533 if (enc_extent_page) {
Eric Sandeen7fcba052008-07-28 15:46:39 -0700534 __free_page(enc_extent_page);
535 }
Michael Halcrow0216f7f2007-10-16 01:28:08 -0700536 return rc;
537}
538
539static int ecryptfs_decrypt_extent(struct page *page,
540 struct ecryptfs_crypt_stat *crypt_stat,
541 struct page *enc_extent_page,
542 unsigned long extent_offset)
543{
Michael Halcrowd6a13c12007-10-16 01:28:12 -0700544 loff_t extent_base;
Michael Halcrow0216f7f2007-10-16 01:28:08 -0700545 char extent_iv[ECRYPTFS_MAX_IV_BYTES];
546 int rc;
547
Michael Halcrowd6a13c12007-10-16 01:28:12 -0700548 extent_base = (((loff_t)page->index)
Michael Halcrow0216f7f2007-10-16 01:28:08 -0700549 * (PAGE_CACHE_SIZE / crypt_stat->extent_size));
550 rc = ecryptfs_derive_iv(extent_iv, crypt_stat,
551 (extent_base + extent_offset));
Michael Halcrow237fead2006-10-04 02:16:22 -0700552 if (rc) {
Joe Perches888d57b2010-11-10 15:46:16 -0800553 ecryptfs_printk(KERN_ERR, "Error attempting to derive IV for "
554 "extent [0x%.16llx]; rc = [%d]\n",
555 (unsigned long long)(extent_base + extent_offset), rc);
Michael Halcrow0216f7f2007-10-16 01:28:08 -0700556 goto out;
557 }
Tyler Hicks28916d12013-04-15 16:37:27 -0700558 rc = ecryptfs_decrypt_page_offset(crypt_stat, page, enc_extent_page,
Tyler Hicks12003e52013-04-16 23:21:24 -0700559 extent_offset * crypt_stat->extent_size,
560 crypt_stat->extent_size, extent_iv);
Michael Halcrow0216f7f2007-10-16 01:28:08 -0700561 if (rc < 0) {
562 printk(KERN_ERR "%s: Error attempting to decrypt to page with "
563 "page->index = [%ld], extent_offset = [%ld]; "
Harvey Harrison18d1dbf2008-04-29 00:59:48 -0700564 "rc = [%d]\n", __func__, page->index, extent_offset,
Michael Halcrow0216f7f2007-10-16 01:28:08 -0700565 rc);
566 goto out;
567 }
568 rc = 0;
Michael Halcrow237fead2006-10-04 02:16:22 -0700569out:
570 return rc;
571}
572
573/**
574 * ecryptfs_decrypt_page
Michael Halcrow0216f7f2007-10-16 01:28:08 -0700575 * @page: Page mapped from the eCryptfs inode for the file; data read
576 * and decrypted from the lower file will be written into this
577 * page
Michael Halcrow237fead2006-10-04 02:16:22 -0700578 *
579 * Decrypt an eCryptfs page. This is done on a per-extent basis. Note
580 * that eCryptfs pages may straddle the lower pages -- for instance,
581 * if the file was created on a machine with an 8K page size
582 * (resulting in an 8K header), and then the file is copied onto a
583 * host with a 32K page size, then when reading page 0 of the eCryptfs
584 * file, 24K of page 0 of the lower file will be read and decrypted,
585 * and then 8K of page 1 of the lower file will be read and decrypted.
586 *
587 * Returns zero on success; negative on error
588 */
Michael Halcrow0216f7f2007-10-16 01:28:08 -0700589int ecryptfs_decrypt_page(struct page *page)
Michael Halcrow237fead2006-10-04 02:16:22 -0700590{
Michael Halcrow0216f7f2007-10-16 01:28:08 -0700591 struct inode *ecryptfs_inode;
Michael Halcrow237fead2006-10-04 02:16:22 -0700592 struct ecryptfs_crypt_stat *crypt_stat;
Tyler Hicks9c6043f2013-04-06 00:41:48 -0700593 char *page_virt;
Michael Halcrow0216f7f2007-10-16 01:28:08 -0700594 unsigned long extent_offset;
Tyler Hicks0f896172013-04-15 16:16:24 -0700595 loff_t lower_offset;
Michael Halcrow237fead2006-10-04 02:16:22 -0700596 int rc = 0;
Michael Halcrow237fead2006-10-04 02:16:22 -0700597
Michael Halcrow0216f7f2007-10-16 01:28:08 -0700598 ecryptfs_inode = page->mapping->host;
599 crypt_stat =
600 &(ecryptfs_inode_to_private(ecryptfs_inode)->crypt_stat);
Tyler Hicks13a791b2009-04-13 15:29:27 -0500601 BUG_ON(!(crypt_stat->flags & ECRYPTFS_ENCRYPTED));
Tyler Hicks0f896172013-04-15 16:16:24 -0700602
Tyler Hicks24d15262013-04-15 17:28:09 -0700603 lower_offset = lower_offset_for_page(crypt_stat, page);
Tyler Hicks9c6043f2013-04-06 00:41:48 -0700604 page_virt = kmap(page);
605 rc = ecryptfs_read_lower(page_virt, lower_offset, PAGE_CACHE_SIZE,
Tyler Hicks0f896172013-04-15 16:16:24 -0700606 ecryptfs_inode);
Tyler Hicks9c6043f2013-04-06 00:41:48 -0700607 kunmap(page);
Tyler Hicks0f896172013-04-15 16:16:24 -0700608 if (rc < 0) {
609 ecryptfs_printk(KERN_ERR,
610 "Error attempting to read lower page; rc = [%d]\n",
611 rc);
612 goto out;
613 }
614
Michael Halcrow0216f7f2007-10-16 01:28:08 -0700615 for (extent_offset = 0;
616 extent_offset < (PAGE_CACHE_SIZE / crypt_stat->extent_size);
617 extent_offset++) {
Tyler Hicks9c6043f2013-04-06 00:41:48 -0700618 rc = ecryptfs_decrypt_extent(page, crypt_stat, page,
Michael Halcrow0216f7f2007-10-16 01:28:08 -0700619 extent_offset);
620 if (rc) {
621 printk(KERN_ERR "%s: Error encrypting extent; "
Harvey Harrison18d1dbf2008-04-29 00:59:48 -0700622 "rc = [%d]\n", __func__, rc);
Michael Halcrow16a72c42007-10-16 01:28:14 -0700623 goto out;
Michael Halcrow237fead2006-10-04 02:16:22 -0700624 }
Michael Halcrow237fead2006-10-04 02:16:22 -0700625 }
626out:
Michael Halcrow237fead2006-10-04 02:16:22 -0700627 return rc;
628}
629
630/**
Michael Halcrow237fead2006-10-04 02:16:22 -0700631 * ecryptfs_encrypt_page_offset
Michael Halcrow22e78fa2007-10-16 01:28:02 -0700632 * @crypt_stat: The cryptographic context
633 * @dst_page: The page to encrypt into
Michael Halcrow22e78fa2007-10-16 01:28:02 -0700634 * @src_page: The page to encrypt from
Tyler Hicks28916d12013-04-15 16:37:27 -0700635 * @offset: The byte offset into the dst_page and src_page
Michael Halcrow22e78fa2007-10-16 01:28:02 -0700636 * @size: The number of bytes to encrypt
637 * @iv: The initialization vector to use for the encryption
Michael Halcrow237fead2006-10-04 02:16:22 -0700638 *
639 * Returns the number of bytes encrypted
640 */
641static int
642ecryptfs_encrypt_page_offset(struct ecryptfs_crypt_stat *crypt_stat,
Tyler Hicks28916d12013-04-15 16:37:27 -0700643 struct page *dst_page, struct page *src_page,
644 int offset, int size, unsigned char *iv)
Michael Halcrow237fead2006-10-04 02:16:22 -0700645{
646 struct scatterlist src_sg, dst_sg;
647
Jens Axboe60c74f82007-10-22 19:43:30 +0200648 sg_init_table(&src_sg, 1);
649 sg_init_table(&dst_sg, 1);
650
Tyler Hicks28916d12013-04-15 16:37:27 -0700651 sg_set_page(&src_sg, src_page, size, offset);
652 sg_set_page(&dst_sg, dst_page, size, offset);
Tyler Hicks00a69942013-04-05 23:26:22 -0700653 return crypt_scatterlist(crypt_stat, &dst_sg, &src_sg,
654 size, iv, ENCRYPT);
Michael Halcrow237fead2006-10-04 02:16:22 -0700655}
656
657/**
658 * ecryptfs_decrypt_page_offset
Michael Halcrow22e78fa2007-10-16 01:28:02 -0700659 * @crypt_stat: The cryptographic context
660 * @dst_page: The page to decrypt into
Michael Halcrow22e78fa2007-10-16 01:28:02 -0700661 * @src_page: The page to decrypt from
Tyler Hicks28916d12013-04-15 16:37:27 -0700662 * @offset: The byte offset into the dst_page and src_page
Michael Halcrow22e78fa2007-10-16 01:28:02 -0700663 * @size: The number of bytes to decrypt
664 * @iv: The initialization vector to use for the decryption
Michael Halcrow237fead2006-10-04 02:16:22 -0700665 *
666 * Returns the number of bytes decrypted
667 */
668static int
669ecryptfs_decrypt_page_offset(struct ecryptfs_crypt_stat *crypt_stat,
Tyler Hicks28916d12013-04-15 16:37:27 -0700670 struct page *dst_page, struct page *src_page,
671 int offset, int size, unsigned char *iv)
Michael Halcrow237fead2006-10-04 02:16:22 -0700672{
673 struct scatterlist src_sg, dst_sg;
674
Jens Axboe60c74f82007-10-22 19:43:30 +0200675 sg_init_table(&src_sg, 1);
Tyler Hicks28916d12013-04-15 16:37:27 -0700676 sg_set_page(&src_sg, src_page, size, offset);
Jens Axboe60c74f82007-10-22 19:43:30 +0200677
Jens Axboe642f149032007-10-24 11:20:47 +0200678 sg_init_table(&dst_sg, 1);
Tyler Hicks28916d12013-04-15 16:37:27 -0700679 sg_set_page(&dst_sg, dst_page, size, offset);
Jens Axboe642f149032007-10-24 11:20:47 +0200680
Tyler Hicks00a69942013-04-05 23:26:22 -0700681 return crypt_scatterlist(crypt_stat, &dst_sg, &src_sg,
682 size, iv, DECRYPT);
Michael Halcrow237fead2006-10-04 02:16:22 -0700683}
684
685#define ECRYPTFS_MAX_SCATTERLIST_LEN 4
686
687/**
688 * ecryptfs_init_crypt_ctx
Uwe Kleine-König421f91d2010-06-11 12:17:00 +0200689 * @crypt_stat: Uninitialized crypt stats structure
Michael Halcrow237fead2006-10-04 02:16:22 -0700690 *
691 * Initialize the crypto context.
692 *
693 * TODO: Performance: Keep a cache of initialized cipher contexts;
694 * only init if needed
695 */
696int ecryptfs_init_crypt_ctx(struct ecryptfs_crypt_stat *crypt_stat)
697{
Michael Halcrow8bba0662006-10-30 22:07:18 -0800698 char *full_alg_name;
Michael Halcrow237fead2006-10-04 02:16:22 -0700699 int rc = -EINVAL;
700
701 if (!crypt_stat->cipher) {
702 ecryptfs_printk(KERN_ERR, "No cipher specified\n");
703 goto out;
704 }
705 ecryptfs_printk(KERN_DEBUG,
706 "Initializing cipher [%s]; strlen = [%d]; "
Tyler Hicksf24b3882010-11-15 17:36:38 -0600707 "key_size_bits = [%zd]\n",
Michael Halcrow237fead2006-10-04 02:16:22 -0700708 crypt_stat->cipher, (int)strlen(crypt_stat->cipher),
709 crypt_stat->key_size << 3);
710 if (crypt_stat->tfm) {
711 rc = 0;
712 goto out;
713 }
714 mutex_lock(&crypt_stat->cs_tfm_mutex);
Michael Halcrow8bba0662006-10-30 22:07:18 -0800715 rc = ecryptfs_crypto_api_algify_cipher_name(&full_alg_name,
716 crypt_stat->cipher, "cbc");
717 if (rc)
Eric Sandeenc8161f62007-12-22 14:03:26 -0800718 goto out_unlock;
Tyler Hicks4dfea4f2013-05-09 16:55:07 -0700719 crypt_stat->tfm = crypto_alloc_ablkcipher(full_alg_name, 0, 0);
Michael Halcrow8bba0662006-10-30 22:07:18 -0800720 kfree(full_alg_name);
Akinobu Mitade887772006-11-28 12:29:49 -0800721 if (IS_ERR(crypt_stat->tfm)) {
722 rc = PTR_ERR(crypt_stat->tfm);
Tyler Hicksb0105ea2009-08-11 00:36:32 -0500723 crypt_stat->tfm = NULL;
Michael Halcrow237fead2006-10-04 02:16:22 -0700724 ecryptfs_printk(KERN_ERR, "cryptfs: init_crypt_ctx(): "
725 "Error initializing cipher [%s]\n",
726 crypt_stat->cipher);
Eric Sandeenc8161f62007-12-22 14:03:26 -0800727 goto out_unlock;
Michael Halcrow237fead2006-10-04 02:16:22 -0700728 }
Tyler Hicks4dfea4f2013-05-09 16:55:07 -0700729 crypto_ablkcipher_set_flags(crypt_stat->tfm, CRYPTO_TFM_REQ_WEAK_KEY);
Michael Halcrow237fead2006-10-04 02:16:22 -0700730 rc = 0;
Eric Sandeenc8161f62007-12-22 14:03:26 -0800731out_unlock:
732 mutex_unlock(&crypt_stat->cs_tfm_mutex);
Michael Halcrow237fead2006-10-04 02:16:22 -0700733out:
734 return rc;
735}
736
737static void set_extent_mask_and_shift(struct ecryptfs_crypt_stat *crypt_stat)
738{
739 int extent_size_tmp;
740
741 crypt_stat->extent_mask = 0xFFFFFFFF;
742 crypt_stat->extent_shift = 0;
743 if (crypt_stat->extent_size == 0)
744 return;
745 extent_size_tmp = crypt_stat->extent_size;
746 while ((extent_size_tmp & 0x01) == 0) {
747 extent_size_tmp >>= 1;
748 crypt_stat->extent_mask <<= 1;
749 crypt_stat->extent_shift++;
750 }
751}
752
753void ecryptfs_set_default_sizes(struct ecryptfs_crypt_stat *crypt_stat)
754{
755 /* Default values; may be overwritten as we are parsing the
756 * packets. */
757 crypt_stat->extent_size = ECRYPTFS_DEFAULT_EXTENT_SIZE;
758 set_extent_mask_and_shift(crypt_stat);
759 crypt_stat->iv_bytes = ECRYPTFS_DEFAULT_IV_BYTES;
Michael Halcrowdd2a3b72007-02-12 00:53:46 -0800760 if (crypt_stat->flags & ECRYPTFS_METADATA_IN_XATTR)
Tyler Hicksfa3ef1c2010-02-11 05:09:14 -0600761 crypt_stat->metadata_size = ECRYPTFS_MINIMUM_HEADER_EXTENT_SIZE;
Michael Halcrow45eaab72007-10-16 01:28:05 -0700762 else {
763 if (PAGE_CACHE_SIZE <= ECRYPTFS_MINIMUM_HEADER_EXTENT_SIZE)
Tyler Hicksfa3ef1c2010-02-11 05:09:14 -0600764 crypt_stat->metadata_size =
Michael Halcrowcc11bef2008-02-06 01:38:32 -0800765 ECRYPTFS_MINIMUM_HEADER_EXTENT_SIZE;
Michael Halcrow45eaab72007-10-16 01:28:05 -0700766 else
Tyler Hicksfa3ef1c2010-02-11 05:09:14 -0600767 crypt_stat->metadata_size = PAGE_CACHE_SIZE;
Michael Halcrow45eaab72007-10-16 01:28:05 -0700768 }
Michael Halcrow237fead2006-10-04 02:16:22 -0700769}
770
771/**
772 * ecryptfs_compute_root_iv
773 * @crypt_stats
774 *
775 * On error, sets the root IV to all 0's.
776 */
777int ecryptfs_compute_root_iv(struct ecryptfs_crypt_stat *crypt_stat)
778{
779 int rc = 0;
780 char dst[MD5_DIGEST_SIZE];
781
782 BUG_ON(crypt_stat->iv_bytes > MD5_DIGEST_SIZE);
783 BUG_ON(crypt_stat->iv_bytes <= 0);
Michael Halcrowe2bd99e2007-02-12 00:53:49 -0800784 if (!(crypt_stat->flags & ECRYPTFS_KEY_VALID)) {
Michael Halcrow237fead2006-10-04 02:16:22 -0700785 rc = -EINVAL;
786 ecryptfs_printk(KERN_WARNING, "Session key not valid; "
787 "cannot generate root IV\n");
788 goto out;
789 }
790 rc = ecryptfs_calculate_md5(dst, crypt_stat, crypt_stat->key,
791 crypt_stat->key_size);
792 if (rc) {
793 ecryptfs_printk(KERN_WARNING, "Error attempting to compute "
794 "MD5 while generating root IV\n");
795 goto out;
796 }
797 memcpy(crypt_stat->root_iv, dst, crypt_stat->iv_bytes);
798out:
799 if (rc) {
800 memset(crypt_stat->root_iv, 0, crypt_stat->iv_bytes);
Michael Halcrowe2bd99e2007-02-12 00:53:49 -0800801 crypt_stat->flags |= ECRYPTFS_SECURITY_WARNING;
Michael Halcrow237fead2006-10-04 02:16:22 -0700802 }
803 return rc;
804}
805
806static void ecryptfs_generate_new_key(struct ecryptfs_crypt_stat *crypt_stat)
807{
808 get_random_bytes(crypt_stat->key, crypt_stat->key_size);
Michael Halcrowe2bd99e2007-02-12 00:53:49 -0800809 crypt_stat->flags |= ECRYPTFS_KEY_VALID;
Michael Halcrow237fead2006-10-04 02:16:22 -0700810 ecryptfs_compute_root_iv(crypt_stat);
811 if (unlikely(ecryptfs_verbosity > 0)) {
812 ecryptfs_printk(KERN_DEBUG, "Generated new session key:\n");
813 ecryptfs_dump_hex(crypt_stat->key,
814 crypt_stat->key_size);
815 }
816}
817
818/**
Michael Halcrow17398952007-02-12 00:53:45 -0800819 * ecryptfs_copy_mount_wide_flags_to_inode_flags
Michael Halcrow22e78fa2007-10-16 01:28:02 -0700820 * @crypt_stat: The inode's cryptographic context
821 * @mount_crypt_stat: The mount point's cryptographic context
Michael Halcrow17398952007-02-12 00:53:45 -0800822 *
823 * This function propagates the mount-wide flags to individual inode
824 * flags.
825 */
826static void ecryptfs_copy_mount_wide_flags_to_inode_flags(
827 struct ecryptfs_crypt_stat *crypt_stat,
828 struct ecryptfs_mount_crypt_stat *mount_crypt_stat)
829{
830 if (mount_crypt_stat->flags & ECRYPTFS_XATTR_METADATA_ENABLED)
831 crypt_stat->flags |= ECRYPTFS_METADATA_IN_XATTR;
832 if (mount_crypt_stat->flags & ECRYPTFS_ENCRYPTED_VIEW_ENABLED)
833 crypt_stat->flags |= ECRYPTFS_VIEW_AS_ENCRYPTED;
Michael Halcrowaddd65ad2009-01-06 14:42:00 -0800834 if (mount_crypt_stat->flags & ECRYPTFS_GLOBAL_ENCRYPT_FILENAMES) {
835 crypt_stat->flags |= ECRYPTFS_ENCRYPT_FILENAMES;
836 if (mount_crypt_stat->flags
837 & ECRYPTFS_GLOBAL_ENCFN_USE_MOUNT_FNEK)
838 crypt_stat->flags |= ECRYPTFS_ENCFN_USE_MOUNT_FNEK;
839 else if (mount_crypt_stat->flags
840 & ECRYPTFS_GLOBAL_ENCFN_USE_FEK)
841 crypt_stat->flags |= ECRYPTFS_ENCFN_USE_FEK;
842 }
Michael Halcrow17398952007-02-12 00:53:45 -0800843}
844
Michael Halcrowf4aad162007-10-16 01:27:53 -0700845static int ecryptfs_copy_mount_wide_sigs_to_inode_sigs(
846 struct ecryptfs_crypt_stat *crypt_stat,
847 struct ecryptfs_mount_crypt_stat *mount_crypt_stat)
848{
849 struct ecryptfs_global_auth_tok *global_auth_tok;
850 int rc = 0;
851
Roland Dreieraa061172009-07-01 15:48:18 -0700852 mutex_lock(&crypt_stat->keysig_list_mutex);
Michael Halcrowf4aad162007-10-16 01:27:53 -0700853 mutex_lock(&mount_crypt_stat->global_auth_tok_list_mutex);
Roland Dreieraa061172009-07-01 15:48:18 -0700854
Michael Halcrowf4aad162007-10-16 01:27:53 -0700855 list_for_each_entry(global_auth_tok,
856 &mount_crypt_stat->global_auth_tok_list,
857 mount_crypt_stat_list) {
Tyler Hicks84814d62009-03-13 13:51:59 -0700858 if (global_auth_tok->flags & ECRYPTFS_AUTH_TOK_FNEK)
859 continue;
Michael Halcrowf4aad162007-10-16 01:27:53 -0700860 rc = ecryptfs_add_keysig(crypt_stat, global_auth_tok->sig);
861 if (rc) {
862 printk(KERN_ERR "Error adding keysig; rc = [%d]\n", rc);
Michael Halcrowf4aad162007-10-16 01:27:53 -0700863 goto out;
864 }
865 }
Roland Dreieraa061172009-07-01 15:48:18 -0700866
Michael Halcrowf4aad162007-10-16 01:27:53 -0700867out:
Roland Dreieraa061172009-07-01 15:48:18 -0700868 mutex_unlock(&mount_crypt_stat->global_auth_tok_list_mutex);
869 mutex_unlock(&crypt_stat->keysig_list_mutex);
Michael Halcrowf4aad162007-10-16 01:27:53 -0700870 return rc;
871}
872
Michael Halcrow17398952007-02-12 00:53:45 -0800873/**
Michael Halcrow237fead2006-10-04 02:16:22 -0700874 * ecryptfs_set_default_crypt_stat_vals
Michael Halcrow22e78fa2007-10-16 01:28:02 -0700875 * @crypt_stat: The inode's cryptographic context
876 * @mount_crypt_stat: The mount point's cryptographic context
Michael Halcrow237fead2006-10-04 02:16:22 -0700877 *
878 * Default values in the event that policy does not override them.
879 */
880static void ecryptfs_set_default_crypt_stat_vals(
881 struct ecryptfs_crypt_stat *crypt_stat,
882 struct ecryptfs_mount_crypt_stat *mount_crypt_stat)
883{
Michael Halcrow17398952007-02-12 00:53:45 -0800884 ecryptfs_copy_mount_wide_flags_to_inode_flags(crypt_stat,
885 mount_crypt_stat);
Michael Halcrow237fead2006-10-04 02:16:22 -0700886 ecryptfs_set_default_sizes(crypt_stat);
887 strcpy(crypt_stat->cipher, ECRYPTFS_DEFAULT_CIPHER);
888 crypt_stat->key_size = ECRYPTFS_DEFAULT_KEY_BYTES;
Michael Halcrowe2bd99e2007-02-12 00:53:49 -0800889 crypt_stat->flags &= ~(ECRYPTFS_KEY_VALID);
Michael Halcrow237fead2006-10-04 02:16:22 -0700890 crypt_stat->file_version = ECRYPTFS_FILE_VERSION;
891 crypt_stat->mount_crypt_stat = mount_crypt_stat;
892}
893
894/**
895 * ecryptfs_new_file_context
Tyler Hicksb59db432011-11-21 17:31:02 -0600896 * @ecryptfs_inode: The eCryptfs inode
Michael Halcrow237fead2006-10-04 02:16:22 -0700897 *
898 * If the crypto context for the file has not yet been established,
899 * this is where we do that. Establishing a new crypto context
900 * involves the following decisions:
901 * - What cipher to use?
902 * - What set of authentication tokens to use?
903 * Here we just worry about getting enough information into the
904 * authentication tokens so that we know that they are available.
905 * We associate the available authentication tokens with the new file
906 * via the set of signatures in the crypt_stat struct. Later, when
907 * the headers are actually written out, we may again defer to
908 * userspace to perform the encryption of the session key; for the
909 * foreseeable future, this will be the case with public key packets.
910 *
911 * Returns zero on success; non-zero otherwise
912 */
Tyler Hicksb59db432011-11-21 17:31:02 -0600913int ecryptfs_new_file_context(struct inode *ecryptfs_inode)
Michael Halcrow237fead2006-10-04 02:16:22 -0700914{
Michael Halcrow237fead2006-10-04 02:16:22 -0700915 struct ecryptfs_crypt_stat *crypt_stat =
Tyler Hicksb59db432011-11-21 17:31:02 -0600916 &ecryptfs_inode_to_private(ecryptfs_inode)->crypt_stat;
Michael Halcrow237fead2006-10-04 02:16:22 -0700917 struct ecryptfs_mount_crypt_stat *mount_crypt_stat =
918 &ecryptfs_superblock_to_private(
Tyler Hicksb59db432011-11-21 17:31:02 -0600919 ecryptfs_inode->i_sb)->mount_crypt_stat;
Michael Halcrow237fead2006-10-04 02:16:22 -0700920 int cipher_name_len;
Michael Halcrowf4aad162007-10-16 01:27:53 -0700921 int rc = 0;
Michael Halcrow237fead2006-10-04 02:16:22 -0700922
923 ecryptfs_set_default_crypt_stat_vals(crypt_stat, mount_crypt_stat);
Michael Halcrowaf655dc2007-10-16 01:28:00 -0700924 crypt_stat->flags |= (ECRYPTFS_ENCRYPTED | ECRYPTFS_KEY_VALID);
Michael Halcrowf4aad162007-10-16 01:27:53 -0700925 ecryptfs_copy_mount_wide_flags_to_inode_flags(crypt_stat,
926 mount_crypt_stat);
927 rc = ecryptfs_copy_mount_wide_sigs_to_inode_sigs(crypt_stat,
928 mount_crypt_stat);
929 if (rc) {
930 printk(KERN_ERR "Error attempting to copy mount-wide key sigs "
931 "to the inode key sigs; rc = [%d]\n", rc);
932 goto out;
933 }
934 cipher_name_len =
935 strlen(mount_crypt_stat->global_default_cipher_name);
936 memcpy(crypt_stat->cipher,
937 mount_crypt_stat->global_default_cipher_name,
938 cipher_name_len);
939 crypt_stat->cipher[cipher_name_len] = '\0';
940 crypt_stat->key_size =
941 mount_crypt_stat->global_default_cipher_key_size;
942 ecryptfs_generate_new_key(crypt_stat);
Michael Halcrow237fead2006-10-04 02:16:22 -0700943 rc = ecryptfs_init_crypt_ctx(crypt_stat);
944 if (rc)
945 ecryptfs_printk(KERN_ERR, "Error initializing cryptographic "
946 "context for cipher [%s]: rc = [%d]\n",
947 crypt_stat->cipher, rc);
Michael Halcrowf4aad162007-10-16 01:27:53 -0700948out:
Michael Halcrow237fead2006-10-04 02:16:22 -0700949 return rc;
950}
951
952/**
Tyler Hicks7a866172011-05-02 00:39:54 -0500953 * ecryptfs_validate_marker - check for the ecryptfs marker
Michael Halcrow237fead2006-10-04 02:16:22 -0700954 * @data: The data block in which to check
955 *
Tyler Hicks7a866172011-05-02 00:39:54 -0500956 * Returns zero if marker found; -EINVAL if not found
Michael Halcrow237fead2006-10-04 02:16:22 -0700957 */
Tyler Hicks7a866172011-05-02 00:39:54 -0500958static int ecryptfs_validate_marker(char *data)
Michael Halcrow237fead2006-10-04 02:16:22 -0700959{
960 u32 m_1, m_2;
961
Harvey Harrison29335c62008-07-23 21:30:06 -0700962 m_1 = get_unaligned_be32(data);
963 m_2 = get_unaligned_be32(data + 4);
Michael Halcrow237fead2006-10-04 02:16:22 -0700964 if ((m_1 ^ MAGIC_ECRYPTFS_MARKER) == m_2)
Tyler Hicks7a866172011-05-02 00:39:54 -0500965 return 0;
Michael Halcrow237fead2006-10-04 02:16:22 -0700966 ecryptfs_printk(KERN_DEBUG, "m_1 = [0x%.8x]; m_2 = [0x%.8x]; "
967 "MAGIC_ECRYPTFS_MARKER = [0x%.8x]\n", m_1, m_2,
968 MAGIC_ECRYPTFS_MARKER);
969 ecryptfs_printk(KERN_DEBUG, "(m_1 ^ MAGIC_ECRYPTFS_MARKER) = "
970 "[0x%.8x]\n", (m_1 ^ MAGIC_ECRYPTFS_MARKER));
Tyler Hicks7a866172011-05-02 00:39:54 -0500971 return -EINVAL;
Michael Halcrow237fead2006-10-04 02:16:22 -0700972}
973
974struct ecryptfs_flag_map_elem {
975 u32 file_flag;
976 u32 local_flag;
977};
978
979/* Add support for additional flags by adding elements here. */
980static struct ecryptfs_flag_map_elem ecryptfs_flag_map[] = {
981 {0x00000001, ECRYPTFS_ENABLE_HMAC},
Michael Halcrowdd2a3b72007-02-12 00:53:46 -0800982 {0x00000002, ECRYPTFS_ENCRYPTED},
Michael Halcrowaddd65ad2009-01-06 14:42:00 -0800983 {0x00000004, ECRYPTFS_METADATA_IN_XATTR},
984 {0x00000008, ECRYPTFS_ENCRYPT_FILENAMES}
Michael Halcrow237fead2006-10-04 02:16:22 -0700985};
986
987/**
988 * ecryptfs_process_flags
Michael Halcrow22e78fa2007-10-16 01:28:02 -0700989 * @crypt_stat: The cryptographic context
Michael Halcrow237fead2006-10-04 02:16:22 -0700990 * @page_virt: Source data to be parsed
991 * @bytes_read: Updated with the number of bytes read
992 *
993 * Returns zero on success; non-zero if the flag set is invalid
994 */
995static int ecryptfs_process_flags(struct ecryptfs_crypt_stat *crypt_stat,
996 char *page_virt, int *bytes_read)
997{
998 int rc = 0;
999 int i;
1000 u32 flags;
1001
Harvey Harrison29335c62008-07-23 21:30:06 -07001002 flags = get_unaligned_be32(page_virt);
Michael Halcrow237fead2006-10-04 02:16:22 -07001003 for (i = 0; i < ((sizeof(ecryptfs_flag_map)
1004 / sizeof(struct ecryptfs_flag_map_elem))); i++)
1005 if (flags & ecryptfs_flag_map[i].file_flag) {
Michael Halcrowe2bd99e2007-02-12 00:53:49 -08001006 crypt_stat->flags |= ecryptfs_flag_map[i].local_flag;
Michael Halcrow237fead2006-10-04 02:16:22 -07001007 } else
Michael Halcrowe2bd99e2007-02-12 00:53:49 -08001008 crypt_stat->flags &= ~(ecryptfs_flag_map[i].local_flag);
Michael Halcrow237fead2006-10-04 02:16:22 -07001009 /* Version is in top 8 bits of the 32-bit flag vector */
1010 crypt_stat->file_version = ((flags >> 24) & 0xFF);
1011 (*bytes_read) = 4;
1012 return rc;
1013}
1014
1015/**
1016 * write_ecryptfs_marker
1017 * @page_virt: The pointer to in a page to begin writing the marker
1018 * @written: Number of bytes written
1019 *
1020 * Marker = 0x3c81b7f5
1021 */
1022static void write_ecryptfs_marker(char *page_virt, size_t *written)
1023{
1024 u32 m_1, m_2;
1025
1026 get_random_bytes(&m_1, (MAGIC_ECRYPTFS_MARKER_SIZE_BYTES / 2));
1027 m_2 = (m_1 ^ MAGIC_ECRYPTFS_MARKER);
Harvey Harrison29335c62008-07-23 21:30:06 -07001028 put_unaligned_be32(m_1, page_virt);
1029 page_virt += (MAGIC_ECRYPTFS_MARKER_SIZE_BYTES / 2);
1030 put_unaligned_be32(m_2, page_virt);
Michael Halcrow237fead2006-10-04 02:16:22 -07001031 (*written) = MAGIC_ECRYPTFS_MARKER_SIZE_BYTES;
1032}
1033
Tyler Hicksf4e60e62010-02-11 00:02:32 -06001034void ecryptfs_write_crypt_stat_flags(char *page_virt,
1035 struct ecryptfs_crypt_stat *crypt_stat,
1036 size_t *written)
Michael Halcrow237fead2006-10-04 02:16:22 -07001037{
1038 u32 flags = 0;
1039 int i;
1040
1041 for (i = 0; i < ((sizeof(ecryptfs_flag_map)
1042 / sizeof(struct ecryptfs_flag_map_elem))); i++)
Michael Halcrowe2bd99e2007-02-12 00:53:49 -08001043 if (crypt_stat->flags & ecryptfs_flag_map[i].local_flag)
Michael Halcrow237fead2006-10-04 02:16:22 -07001044 flags |= ecryptfs_flag_map[i].file_flag;
1045 /* Version is in top 8 bits of the 32-bit flag vector */
1046 flags |= ((((u8)crypt_stat->file_version) << 24) & 0xFF000000);
Harvey Harrison29335c62008-07-23 21:30:06 -07001047 put_unaligned_be32(flags, page_virt);
Michael Halcrow237fead2006-10-04 02:16:22 -07001048 (*written) = 4;
1049}
1050
1051struct ecryptfs_cipher_code_str_map_elem {
1052 char cipher_str[16];
Trevor Highland19e66a62008-02-06 01:38:36 -08001053 u8 cipher_code;
Michael Halcrow237fead2006-10-04 02:16:22 -07001054};
1055
1056/* Add support for additional ciphers by adding elements here. The
1057 * cipher_code is whatever OpenPGP applicatoins use to identify the
1058 * ciphers. List in order of probability. */
1059static struct ecryptfs_cipher_code_str_map_elem
1060ecryptfs_cipher_code_str_map[] = {
1061 {"aes",RFC2440_CIPHER_AES_128 },
1062 {"blowfish", RFC2440_CIPHER_BLOWFISH},
1063 {"des3_ede", RFC2440_CIPHER_DES3_EDE},
1064 {"cast5", RFC2440_CIPHER_CAST_5},
1065 {"twofish", RFC2440_CIPHER_TWOFISH},
1066 {"cast6", RFC2440_CIPHER_CAST_6},
1067 {"aes", RFC2440_CIPHER_AES_192},
1068 {"aes", RFC2440_CIPHER_AES_256}
1069};
1070
1071/**
1072 * ecryptfs_code_for_cipher_string
Michael Halcrow9c79f342009-01-06 14:41:57 -08001073 * @cipher_name: The string alias for the cipher
1074 * @key_bytes: Length of key in bytes; used for AES code selection
Michael Halcrow237fead2006-10-04 02:16:22 -07001075 *
1076 * Returns zero on no match, or the cipher code on match
1077 */
Michael Halcrow9c79f342009-01-06 14:41:57 -08001078u8 ecryptfs_code_for_cipher_string(char *cipher_name, size_t key_bytes)
Michael Halcrow237fead2006-10-04 02:16:22 -07001079{
1080 int i;
Trevor Highland19e66a62008-02-06 01:38:36 -08001081 u8 code = 0;
Michael Halcrow237fead2006-10-04 02:16:22 -07001082 struct ecryptfs_cipher_code_str_map_elem *map =
1083 ecryptfs_cipher_code_str_map;
1084
Michael Halcrow9c79f342009-01-06 14:41:57 -08001085 if (strcmp(cipher_name, "aes") == 0) {
1086 switch (key_bytes) {
Michael Halcrow237fead2006-10-04 02:16:22 -07001087 case 16:
1088 code = RFC2440_CIPHER_AES_128;
1089 break;
1090 case 24:
1091 code = RFC2440_CIPHER_AES_192;
1092 break;
1093 case 32:
1094 code = RFC2440_CIPHER_AES_256;
1095 }
1096 } else {
1097 for (i = 0; i < ARRAY_SIZE(ecryptfs_cipher_code_str_map); i++)
Michael Halcrow9c79f342009-01-06 14:41:57 -08001098 if (strcmp(cipher_name, map[i].cipher_str) == 0) {
Michael Halcrow237fead2006-10-04 02:16:22 -07001099 code = map[i].cipher_code;
1100 break;
1101 }
1102 }
1103 return code;
1104}
1105
1106/**
1107 * ecryptfs_cipher_code_to_string
1108 * @str: Destination to write out the cipher name
1109 * @cipher_code: The code to convert to cipher name string
1110 *
1111 * Returns zero on success
1112 */
Trevor Highland19e66a62008-02-06 01:38:36 -08001113int ecryptfs_cipher_code_to_string(char *str, u8 cipher_code)
Michael Halcrow237fead2006-10-04 02:16:22 -07001114{
1115 int rc = 0;
1116 int i;
1117
1118 str[0] = '\0';
1119 for (i = 0; i < ARRAY_SIZE(ecryptfs_cipher_code_str_map); i++)
1120 if (cipher_code == ecryptfs_cipher_code_str_map[i].cipher_code)
1121 strcpy(str, ecryptfs_cipher_code_str_map[i].cipher_str);
1122 if (str[0] == '\0') {
1123 ecryptfs_printk(KERN_WARNING, "Cipher code not recognized: "
1124 "[%d]\n", cipher_code);
1125 rc = -EINVAL;
1126 }
1127 return rc;
1128}
1129
Tyler Hicks778aeb42011-05-24 04:56:23 -05001130int ecryptfs_read_and_validate_header_region(struct inode *inode)
Michael Halcrowdd2a3b72007-02-12 00:53:46 -08001131{
Tyler Hicks778aeb42011-05-24 04:56:23 -05001132 u8 file_size[ECRYPTFS_SIZE_AND_MARKER_BYTES];
1133 u8 *marker = file_size + ECRYPTFS_FILE_SIZE_BYTES;
Michael Halcrowdd2a3b72007-02-12 00:53:46 -08001134 int rc;
1135
Tyler Hicks778aeb42011-05-24 04:56:23 -05001136 rc = ecryptfs_read_lower(file_size, 0, ECRYPTFS_SIZE_AND_MARKER_BYTES,
1137 inode);
1138 if (rc < ECRYPTFS_SIZE_AND_MARKER_BYTES)
1139 return rc >= 0 ? -EINVAL : rc;
1140 rc = ecryptfs_validate_marker(marker);
1141 if (!rc)
1142 ecryptfs_i_size_init(file_size, inode);
Michael Halcrowdd2a3b72007-02-12 00:53:46 -08001143 return rc;
1144}
1145
Michael Halcrowe77a56d2007-02-12 00:53:47 -08001146void
1147ecryptfs_write_header_metadata(char *virt,
1148 struct ecryptfs_crypt_stat *crypt_stat,
1149 size_t *written)
Michael Halcrow237fead2006-10-04 02:16:22 -07001150{
1151 u32 header_extent_size;
1152 u16 num_header_extents_at_front;
1153
Michael Halcrow45eaab72007-10-16 01:28:05 -07001154 header_extent_size = (u32)crypt_stat->extent_size;
Michael Halcrow237fead2006-10-04 02:16:22 -07001155 num_header_extents_at_front =
Tyler Hicksfa3ef1c2010-02-11 05:09:14 -06001156 (u16)(crypt_stat->metadata_size / crypt_stat->extent_size);
Harvey Harrison29335c62008-07-23 21:30:06 -07001157 put_unaligned_be32(header_extent_size, virt);
Michael Halcrow237fead2006-10-04 02:16:22 -07001158 virt += 4;
Harvey Harrison29335c62008-07-23 21:30:06 -07001159 put_unaligned_be16(num_header_extents_at_front, virt);
Michael Halcrow237fead2006-10-04 02:16:22 -07001160 (*written) = 6;
1161}
1162
Tyler Hicks30632872011-05-24 05:11:12 -05001163struct kmem_cache *ecryptfs_header_cache;
Michael Halcrow237fead2006-10-04 02:16:22 -07001164
1165/**
1166 * ecryptfs_write_headers_virt
Michael Halcrow22e78fa2007-10-16 01:28:02 -07001167 * @page_virt: The virtual address to write the headers to
Eric Sandeen87b811c32008-10-29 14:01:08 -07001168 * @max: The size of memory allocated at page_virt
Michael Halcrow22e78fa2007-10-16 01:28:02 -07001169 * @size: Set to the number of bytes written by this function
1170 * @crypt_stat: The cryptographic context
1171 * @ecryptfs_dentry: The eCryptfs dentry
Michael Halcrow237fead2006-10-04 02:16:22 -07001172 *
1173 * Format version: 1
1174 *
1175 * Header Extent:
1176 * Octets 0-7: Unencrypted file size (big-endian)
1177 * Octets 8-15: eCryptfs special marker
1178 * Octets 16-19: Flags
1179 * Octet 16: File format version number (between 0 and 255)
1180 * Octets 17-18: Reserved
1181 * Octet 19: Bit 1 (lsb): Reserved
1182 * Bit 2: Encrypted?
1183 * Bits 3-8: Reserved
1184 * Octets 20-23: Header extent size (big-endian)
1185 * Octets 24-25: Number of header extents at front of file
1186 * (big-endian)
1187 * Octet 26: Begin RFC 2440 authentication token packet set
1188 * Data Extent 0:
1189 * Lower data (CBC encrypted)
1190 * Data Extent 1:
1191 * Lower data (CBC encrypted)
1192 * ...
1193 *
1194 * Returns zero on success
1195 */
Eric Sandeen87b811c32008-10-29 14:01:08 -07001196static int ecryptfs_write_headers_virt(char *page_virt, size_t max,
1197 size_t *size,
Michael Halcrowdd2a3b72007-02-12 00:53:46 -08001198 struct ecryptfs_crypt_stat *crypt_stat,
1199 struct dentry *ecryptfs_dentry)
Michael Halcrow237fead2006-10-04 02:16:22 -07001200{
1201 int rc;
1202 size_t written;
1203 size_t offset;
1204
1205 offset = ECRYPTFS_FILE_SIZE_BYTES;
1206 write_ecryptfs_marker((page_virt + offset), &written);
1207 offset += written;
Tyler Hicksf4e60e62010-02-11 00:02:32 -06001208 ecryptfs_write_crypt_stat_flags((page_virt + offset), crypt_stat,
1209 &written);
Michael Halcrow237fead2006-10-04 02:16:22 -07001210 offset += written;
Michael Halcrowe77a56d2007-02-12 00:53:47 -08001211 ecryptfs_write_header_metadata((page_virt + offset), crypt_stat,
1212 &written);
Michael Halcrow237fead2006-10-04 02:16:22 -07001213 offset += written;
1214 rc = ecryptfs_generate_key_packet_set((page_virt + offset), crypt_stat,
1215 ecryptfs_dentry, &written,
Eric Sandeen87b811c32008-10-29 14:01:08 -07001216 max - offset);
Michael Halcrow237fead2006-10-04 02:16:22 -07001217 if (rc)
1218 ecryptfs_printk(KERN_WARNING, "Error generating key packet "
1219 "set; rc = [%d]\n", rc);
Michael Halcrowdd2a3b72007-02-12 00:53:46 -08001220 if (size) {
1221 offset += written;
1222 *size = offset;
1223 }
1224 return rc;
1225}
1226
Michael Halcrow22e78fa2007-10-16 01:28:02 -07001227static int
Tyler Hicksb59db432011-11-21 17:31:02 -06001228ecryptfs_write_metadata_to_contents(struct inode *ecryptfs_inode,
Tyler Hicks8faece52009-03-20 01:25:09 -05001229 char *virt, size_t virt_len)
Michael Halcrowdd2a3b72007-02-12 00:53:46 -08001230{
Michael Halcrowd7cdc5f2007-10-16 01:28:10 -07001231 int rc;
Michael Halcrowdd2a3b72007-02-12 00:53:46 -08001232
Tyler Hicksb59db432011-11-21 17:31:02 -06001233 rc = ecryptfs_write_lower(ecryptfs_inode, virt,
Tyler Hicks8faece52009-03-20 01:25:09 -05001234 0, virt_len);
Tyler Hicks96a7b9c2009-09-16 19:04:20 -05001235 if (rc < 0)
Michael Halcrowd7cdc5f2007-10-16 01:28:10 -07001236 printk(KERN_ERR "%s: Error attempting to write header "
Tyler Hicks96a7b9c2009-09-16 19:04:20 -05001237 "information to lower file; rc = [%d]\n", __func__, rc);
1238 else
1239 rc = 0;
Michael Halcrow70456602007-02-12 00:53:48 -08001240 return rc;
Michael Halcrowdd2a3b72007-02-12 00:53:46 -08001241}
1242
Michael Halcrow22e78fa2007-10-16 01:28:02 -07001243static int
1244ecryptfs_write_metadata_to_xattr(struct dentry *ecryptfs_dentry,
Michael Halcrow22e78fa2007-10-16 01:28:02 -07001245 char *page_virt, size_t size)
Michael Halcrowdd2a3b72007-02-12 00:53:46 -08001246{
1247 int rc;
1248
1249 rc = ecryptfs_setxattr(ecryptfs_dentry, ECRYPTFS_XATTR_NAME, page_virt,
1250 size, 0);
Michael Halcrow237fead2006-10-04 02:16:22 -07001251 return rc;
1252}
1253
Tyler Hicks8faece52009-03-20 01:25:09 -05001254static unsigned long ecryptfs_get_zeroed_pages(gfp_t gfp_mask,
1255 unsigned int order)
1256{
1257 struct page *page;
1258
1259 page = alloc_pages(gfp_mask | __GFP_ZERO, order);
1260 if (page)
1261 return (unsigned long) page_address(page);
1262 return 0;
1263}
1264
Michael Halcrow237fead2006-10-04 02:16:22 -07001265/**
Michael Halcrowdd2a3b72007-02-12 00:53:46 -08001266 * ecryptfs_write_metadata
Tyler Hicksb59db432011-11-21 17:31:02 -06001267 * @ecryptfs_dentry: The eCryptfs dentry, which should be negative
1268 * @ecryptfs_inode: The newly created eCryptfs inode
Michael Halcrow237fead2006-10-04 02:16:22 -07001269 *
1270 * Write the file headers out. This will likely involve a userspace
1271 * callout, in which the session key is encrypted with one or more
1272 * public keys and/or the passphrase necessary to do the encryption is
1273 * retrieved via a prompt. Exactly what happens at this point should
1274 * be policy-dependent.
1275 *
1276 * Returns zero on success; non-zero on error
1277 */
Tyler Hicksb59db432011-11-21 17:31:02 -06001278int ecryptfs_write_metadata(struct dentry *ecryptfs_dentry,
1279 struct inode *ecryptfs_inode)
Michael Halcrow237fead2006-10-04 02:16:22 -07001280{
Michael Halcrowd7cdc5f2007-10-16 01:28:10 -07001281 struct ecryptfs_crypt_stat *crypt_stat =
Tyler Hicksb59db432011-11-21 17:31:02 -06001282 &ecryptfs_inode_to_private(ecryptfs_inode)->crypt_stat;
Tyler Hicks8faece52009-03-20 01:25:09 -05001283 unsigned int order;
Michael Halcrowcc11bef2008-02-06 01:38:32 -08001284 char *virt;
Tyler Hicks8faece52009-03-20 01:25:09 -05001285 size_t virt_len;
Michael Halcrowd7cdc5f2007-10-16 01:28:10 -07001286 size_t size = 0;
Michael Halcrow237fead2006-10-04 02:16:22 -07001287 int rc = 0;
1288
Michael Halcrowe2bd99e2007-02-12 00:53:49 -08001289 if (likely(crypt_stat->flags & ECRYPTFS_ENCRYPTED)) {
1290 if (!(crypt_stat->flags & ECRYPTFS_KEY_VALID)) {
Michael Halcrowd7cdc5f2007-10-16 01:28:10 -07001291 printk(KERN_ERR "Key is invalid; bailing out\n");
Michael Halcrow237fead2006-10-04 02:16:22 -07001292 rc = -EINVAL;
1293 goto out;
1294 }
1295 } else {
Michael Halcrowcc11bef2008-02-06 01:38:32 -08001296 printk(KERN_WARNING "%s: Encrypted flag not set\n",
Harvey Harrison18d1dbf2008-04-29 00:59:48 -07001297 __func__);
Michael Halcrow237fead2006-10-04 02:16:22 -07001298 rc = -EINVAL;
Michael Halcrow237fead2006-10-04 02:16:22 -07001299 goto out;
1300 }
Tyler Hicksfa3ef1c2010-02-11 05:09:14 -06001301 virt_len = crypt_stat->metadata_size;
Tyler Hicks8faece52009-03-20 01:25:09 -05001302 order = get_order(virt_len);
Michael Halcrow237fead2006-10-04 02:16:22 -07001303 /* Released in this function */
Tyler Hicks8faece52009-03-20 01:25:09 -05001304 virt = (char *)ecryptfs_get_zeroed_pages(GFP_KERNEL, order);
Michael Halcrowcc11bef2008-02-06 01:38:32 -08001305 if (!virt) {
Harvey Harrison18d1dbf2008-04-29 00:59:48 -07001306 printk(KERN_ERR "%s: Out of memory\n", __func__);
Michael Halcrow237fead2006-10-04 02:16:22 -07001307 rc = -ENOMEM;
1308 goto out;
1309 }
Tyler Hicksbd4f0fe2011-02-23 00:14:19 -06001310 /* Zeroed page ensures the in-header unencrypted i_size is set to 0 */
Tyler Hicks8faece52009-03-20 01:25:09 -05001311 rc = ecryptfs_write_headers_virt(virt, virt_len, &size, crypt_stat,
1312 ecryptfs_dentry);
Michael Halcrow237fead2006-10-04 02:16:22 -07001313 if (unlikely(rc)) {
Michael Halcrowcc11bef2008-02-06 01:38:32 -08001314 printk(KERN_ERR "%s: Error whilst writing headers; rc = [%d]\n",
Harvey Harrison18d1dbf2008-04-29 00:59:48 -07001315 __func__, rc);
Michael Halcrow237fead2006-10-04 02:16:22 -07001316 goto out_free;
1317 }
Michael Halcrowdd2a3b72007-02-12 00:53:46 -08001318 if (crypt_stat->flags & ECRYPTFS_METADATA_IN_XATTR)
Tyler Hicks8faece52009-03-20 01:25:09 -05001319 rc = ecryptfs_write_metadata_to_xattr(ecryptfs_dentry, virt,
1320 size);
Michael Halcrowdd2a3b72007-02-12 00:53:46 -08001321 else
Tyler Hicksb59db432011-11-21 17:31:02 -06001322 rc = ecryptfs_write_metadata_to_contents(ecryptfs_inode, virt,
Tyler Hicks8faece52009-03-20 01:25:09 -05001323 virt_len);
Michael Halcrowdd2a3b72007-02-12 00:53:46 -08001324 if (rc) {
Michael Halcrowcc11bef2008-02-06 01:38:32 -08001325 printk(KERN_ERR "%s: Error writing metadata out to lower file; "
Harvey Harrison18d1dbf2008-04-29 00:59:48 -07001326 "rc = [%d]\n", __func__, rc);
Michael Halcrowdd2a3b72007-02-12 00:53:46 -08001327 goto out_free;
Michael Halcrow237fead2006-10-04 02:16:22 -07001328 }
Michael Halcrow237fead2006-10-04 02:16:22 -07001329out_free:
Tyler Hicks8faece52009-03-20 01:25:09 -05001330 free_pages((unsigned long)virt, order);
Michael Halcrow237fead2006-10-04 02:16:22 -07001331out:
1332 return rc;
1333}
1334
Michael Halcrowdd2a3b72007-02-12 00:53:46 -08001335#define ECRYPTFS_DONT_VALIDATE_HEADER_SIZE 0
1336#define ECRYPTFS_VALIDATE_HEADER_SIZE 1
Michael Halcrow237fead2006-10-04 02:16:22 -07001337static int parse_header_metadata(struct ecryptfs_crypt_stat *crypt_stat,
Michael Halcrowdd2a3b72007-02-12 00:53:46 -08001338 char *virt, int *bytes_read,
1339 int validate_header_size)
Michael Halcrow237fead2006-10-04 02:16:22 -07001340{
1341 int rc = 0;
1342 u32 header_extent_size;
1343 u16 num_header_extents_at_front;
1344
Harvey Harrison29335c62008-07-23 21:30:06 -07001345 header_extent_size = get_unaligned_be32(virt);
1346 virt += sizeof(__be32);
1347 num_header_extents_at_front = get_unaligned_be16(virt);
Tyler Hicksfa3ef1c2010-02-11 05:09:14 -06001348 crypt_stat->metadata_size = (((size_t)num_header_extents_at_front
1349 * (size_t)header_extent_size));
Harvey Harrison29335c62008-07-23 21:30:06 -07001350 (*bytes_read) = (sizeof(__be32) + sizeof(__be16));
Michael Halcrowdd2a3b72007-02-12 00:53:46 -08001351 if ((validate_header_size == ECRYPTFS_VALIDATE_HEADER_SIZE)
Tyler Hicksfa3ef1c2010-02-11 05:09:14 -06001352 && (crypt_stat->metadata_size
Michael Halcrowdd2a3b72007-02-12 00:53:46 -08001353 < ECRYPTFS_MINIMUM_HEADER_EXTENT_SIZE)) {
Michael Halcrow237fead2006-10-04 02:16:22 -07001354 rc = -EINVAL;
Michael Halcrowcc11bef2008-02-06 01:38:32 -08001355 printk(KERN_WARNING "Invalid header size: [%zd]\n",
Tyler Hicksfa3ef1c2010-02-11 05:09:14 -06001356 crypt_stat->metadata_size);
Michael Halcrow237fead2006-10-04 02:16:22 -07001357 }
1358 return rc;
1359}
1360
1361/**
1362 * set_default_header_data
Michael Halcrow22e78fa2007-10-16 01:28:02 -07001363 * @crypt_stat: The cryptographic context
Michael Halcrow237fead2006-10-04 02:16:22 -07001364 *
1365 * For version 0 file format; this function is only for backwards
1366 * compatibility for files created with the prior versions of
1367 * eCryptfs.
1368 */
1369static void set_default_header_data(struct ecryptfs_crypt_stat *crypt_stat)
1370{
Tyler Hicksfa3ef1c2010-02-11 05:09:14 -06001371 crypt_stat->metadata_size = ECRYPTFS_MINIMUM_HEADER_EXTENT_SIZE;
Michael Halcrow237fead2006-10-04 02:16:22 -07001372}
1373
Tyler Hicks3aeb86e2011-03-15 14:54:00 -05001374void ecryptfs_i_size_init(const char *page_virt, struct inode *inode)
1375{
1376 struct ecryptfs_mount_crypt_stat *mount_crypt_stat;
1377 struct ecryptfs_crypt_stat *crypt_stat;
1378 u64 file_size;
1379
1380 crypt_stat = &ecryptfs_inode_to_private(inode)->crypt_stat;
1381 mount_crypt_stat =
1382 &ecryptfs_superblock_to_private(inode->i_sb)->mount_crypt_stat;
1383 if (mount_crypt_stat->flags & ECRYPTFS_ENCRYPTED_VIEW_ENABLED) {
1384 file_size = i_size_read(ecryptfs_inode_to_lower(inode));
1385 if (crypt_stat->flags & ECRYPTFS_METADATA_IN_XATTR)
1386 file_size += crypt_stat->metadata_size;
1387 } else
1388 file_size = get_unaligned_be64(page_virt);
1389 i_size_write(inode, (loff_t)file_size);
1390 crypt_stat->flags |= ECRYPTFS_I_SIZE_INITIALIZED;
1391}
1392
Michael Halcrow237fead2006-10-04 02:16:22 -07001393/**
1394 * ecryptfs_read_headers_virt
Michael Halcrow22e78fa2007-10-16 01:28:02 -07001395 * @page_virt: The virtual address into which to read the headers
1396 * @crypt_stat: The cryptographic context
1397 * @ecryptfs_dentry: The eCryptfs dentry
1398 * @validate_header_size: Whether to validate the header size while reading
Michael Halcrow237fead2006-10-04 02:16:22 -07001399 *
1400 * Read/parse the header data. The header format is detailed in the
1401 * comment block for the ecryptfs_write_headers_virt() function.
1402 *
1403 * Returns zero on success
1404 */
1405static int ecryptfs_read_headers_virt(char *page_virt,
1406 struct ecryptfs_crypt_stat *crypt_stat,
Michael Halcrowdd2a3b72007-02-12 00:53:46 -08001407 struct dentry *ecryptfs_dentry,
1408 int validate_header_size)
Michael Halcrow237fead2006-10-04 02:16:22 -07001409{
1410 int rc = 0;
1411 int offset;
1412 int bytes_read;
1413
1414 ecryptfs_set_default_sizes(crypt_stat);
1415 crypt_stat->mount_crypt_stat = &ecryptfs_superblock_to_private(
1416 ecryptfs_dentry->d_sb)->mount_crypt_stat;
1417 offset = ECRYPTFS_FILE_SIZE_BYTES;
Tyler Hicks7a866172011-05-02 00:39:54 -05001418 rc = ecryptfs_validate_marker(page_virt + offset);
1419 if (rc)
Michael Halcrow237fead2006-10-04 02:16:22 -07001420 goto out;
Tyler Hicks3aeb86e2011-03-15 14:54:00 -05001421 if (!(crypt_stat->flags & ECRYPTFS_I_SIZE_INITIALIZED))
1422 ecryptfs_i_size_init(page_virt, ecryptfs_dentry->d_inode);
Michael Halcrow237fead2006-10-04 02:16:22 -07001423 offset += MAGIC_ECRYPTFS_MARKER_SIZE_BYTES;
1424 rc = ecryptfs_process_flags(crypt_stat, (page_virt + offset),
1425 &bytes_read);
1426 if (rc) {
1427 ecryptfs_printk(KERN_WARNING, "Error processing flags\n");
1428 goto out;
1429 }
1430 if (crypt_stat->file_version > ECRYPTFS_SUPPORTED_FILE_VERSION) {
1431 ecryptfs_printk(KERN_WARNING, "File version is [%d]; only "
1432 "file version [%d] is supported by this "
1433 "version of eCryptfs\n",
1434 crypt_stat->file_version,
1435 ECRYPTFS_SUPPORTED_FILE_VERSION);
1436 rc = -EINVAL;
1437 goto out;
1438 }
1439 offset += bytes_read;
1440 if (crypt_stat->file_version >= 1) {
1441 rc = parse_header_metadata(crypt_stat, (page_virt + offset),
Michael Halcrowdd2a3b72007-02-12 00:53:46 -08001442 &bytes_read, validate_header_size);
Michael Halcrow237fead2006-10-04 02:16:22 -07001443 if (rc) {
1444 ecryptfs_printk(KERN_WARNING, "Error reading header "
1445 "metadata; rc = [%d]\n", rc);
1446 }
1447 offset += bytes_read;
1448 } else
1449 set_default_header_data(crypt_stat);
1450 rc = ecryptfs_parse_packet_set(crypt_stat, (page_virt + offset),
1451 ecryptfs_dentry);
1452out:
1453 return rc;
1454}
1455
1456/**
Michael Halcrowdd2a3b72007-02-12 00:53:46 -08001457 * ecryptfs_read_xattr_region
Michael Halcrow22e78fa2007-10-16 01:28:02 -07001458 * @page_virt: The vitual address into which to read the xattr data
Michael Halcrow2ed92552007-10-16 01:28:10 -07001459 * @ecryptfs_inode: The eCryptfs inode
Michael Halcrowdd2a3b72007-02-12 00:53:46 -08001460 *
1461 * Attempts to read the crypto metadata from the extended attribute
1462 * region of the lower file.
Michael Halcrow22e78fa2007-10-16 01:28:02 -07001463 *
1464 * Returns zero on success; non-zero on error
Michael Halcrowdd2a3b72007-02-12 00:53:46 -08001465 */
Michael Halcrowd7cdc5f2007-10-16 01:28:10 -07001466int ecryptfs_read_xattr_region(char *page_virt, struct inode *ecryptfs_inode)
Michael Halcrowdd2a3b72007-02-12 00:53:46 -08001467{
Michael Halcrowd7cdc5f2007-10-16 01:28:10 -07001468 struct dentry *lower_dentry =
1469 ecryptfs_inode_to_private(ecryptfs_inode)->lower_file->f_dentry;
Michael Halcrowdd2a3b72007-02-12 00:53:46 -08001470 ssize_t size;
1471 int rc = 0;
1472
Michael Halcrowd7cdc5f2007-10-16 01:28:10 -07001473 size = ecryptfs_getxattr_lower(lower_dentry, ECRYPTFS_XATTR_NAME,
1474 page_virt, ECRYPTFS_DEFAULT_EXTENT_SIZE);
Michael Halcrowdd2a3b72007-02-12 00:53:46 -08001475 if (size < 0) {
Michael Halcrow25bd8172008-02-06 01:38:35 -08001476 if (unlikely(ecryptfs_verbosity > 0))
1477 printk(KERN_INFO "Error attempting to read the [%s] "
1478 "xattr from the lower file; return value = "
1479 "[%zd]\n", ECRYPTFS_XATTR_NAME, size);
Michael Halcrowdd2a3b72007-02-12 00:53:46 -08001480 rc = -EINVAL;
1481 goto out;
1482 }
1483out:
1484 return rc;
1485}
1486
Tyler Hicks778aeb42011-05-24 04:56:23 -05001487int ecryptfs_read_and_validate_xattr_region(struct dentry *dentry,
Tyler Hicks3b06b3e2011-05-24 03:49:02 -05001488 struct inode *inode)
Michael Halcrowdd2a3b72007-02-12 00:53:46 -08001489{
Tyler Hicks778aeb42011-05-24 04:56:23 -05001490 u8 file_size[ECRYPTFS_SIZE_AND_MARKER_BYTES];
1491 u8 *marker = file_size + ECRYPTFS_FILE_SIZE_BYTES;
Michael Halcrowdd2a3b72007-02-12 00:53:46 -08001492 int rc;
1493
Tyler Hicks778aeb42011-05-24 04:56:23 -05001494 rc = ecryptfs_getxattr_lower(ecryptfs_dentry_to_lower(dentry),
1495 ECRYPTFS_XATTR_NAME, file_size,
1496 ECRYPTFS_SIZE_AND_MARKER_BYTES);
1497 if (rc < ECRYPTFS_SIZE_AND_MARKER_BYTES)
1498 return rc >= 0 ? -EINVAL : rc;
1499 rc = ecryptfs_validate_marker(marker);
1500 if (!rc)
1501 ecryptfs_i_size_init(file_size, inode);
Michael Halcrowdd2a3b72007-02-12 00:53:46 -08001502 return rc;
1503}
1504
1505/**
1506 * ecryptfs_read_metadata
1507 *
1508 * Common entry point for reading file metadata. From here, we could
1509 * retrieve the header information from the header region of the file,
1510 * the xattr region of the file, or some other repostory that is
1511 * stored separately from the file itself. The current implementation
1512 * supports retrieving the metadata information from the file contents
1513 * and from the xattr region.
Michael Halcrow237fead2006-10-04 02:16:22 -07001514 *
1515 * Returns zero if valid headers found and parsed; non-zero otherwise
1516 */
Michael Halcrowd7cdc5f2007-10-16 01:28:10 -07001517int ecryptfs_read_metadata(struct dentry *ecryptfs_dentry)
Michael Halcrow237fead2006-10-04 02:16:22 -07001518{
Tim Gardnerbb450362012-01-12 16:31:55 +01001519 int rc;
1520 char *page_virt;
Michael Halcrowd7cdc5f2007-10-16 01:28:10 -07001521 struct inode *ecryptfs_inode = ecryptfs_dentry->d_inode;
Michael Halcrow237fead2006-10-04 02:16:22 -07001522 struct ecryptfs_crypt_stat *crypt_stat =
Michael Halcrowd7cdc5f2007-10-16 01:28:10 -07001523 &ecryptfs_inode_to_private(ecryptfs_inode)->crypt_stat;
Michael Halcrowe77a56d2007-02-12 00:53:47 -08001524 struct ecryptfs_mount_crypt_stat *mount_crypt_stat =
1525 &ecryptfs_superblock_to_private(
1526 ecryptfs_dentry->d_sb)->mount_crypt_stat;
Michael Halcrow237fead2006-10-04 02:16:22 -07001527
Michael Halcrowe77a56d2007-02-12 00:53:47 -08001528 ecryptfs_copy_mount_wide_flags_to_inode_flags(crypt_stat,
1529 mount_crypt_stat);
Michael Halcrow237fead2006-10-04 02:16:22 -07001530 /* Read the first page from the underlying file */
Tyler Hicks30632872011-05-24 05:11:12 -05001531 page_virt = kmem_cache_alloc(ecryptfs_header_cache, GFP_USER);
Michael Halcrow237fead2006-10-04 02:16:22 -07001532 if (!page_virt) {
1533 rc = -ENOMEM;
Michael Halcrowd7cdc5f2007-10-16 01:28:10 -07001534 printk(KERN_ERR "%s: Unable to allocate page_virt\n",
Harvey Harrison18d1dbf2008-04-29 00:59:48 -07001535 __func__);
Michael Halcrow237fead2006-10-04 02:16:22 -07001536 goto out;
1537 }
Michael Halcrowd7cdc5f2007-10-16 01:28:10 -07001538 rc = ecryptfs_read_lower(page_virt, 0, crypt_stat->extent_size,
1539 ecryptfs_inode);
Tyler Hicks96a7b9c2009-09-16 19:04:20 -05001540 if (rc >= 0)
Michael Halcrowd7cdc5f2007-10-16 01:28:10 -07001541 rc = ecryptfs_read_headers_virt(page_virt, crypt_stat,
1542 ecryptfs_dentry,
1543 ECRYPTFS_VALIDATE_HEADER_SIZE);
Michael Halcrow237fead2006-10-04 02:16:22 -07001544 if (rc) {
Tim Gardnerbb450362012-01-12 16:31:55 +01001545 /* metadata is not in the file header, so try xattrs */
Tyler Hicks1984c232010-02-10 23:17:44 -06001546 memset(page_virt, 0, PAGE_CACHE_SIZE);
Michael Halcrowd7cdc5f2007-10-16 01:28:10 -07001547 rc = ecryptfs_read_xattr_region(page_virt, ecryptfs_inode);
Michael Halcrowdd2a3b72007-02-12 00:53:46 -08001548 if (rc) {
1549 printk(KERN_DEBUG "Valid eCryptfs headers not found in "
Tim Gardner30373dc2012-01-12 16:31:55 +01001550 "file header region or xattr region, inode %lu\n",
1551 ecryptfs_inode->i_ino);
Michael Halcrowdd2a3b72007-02-12 00:53:46 -08001552 rc = -EINVAL;
1553 goto out;
1554 }
1555 rc = ecryptfs_read_headers_virt(page_virt, crypt_stat,
1556 ecryptfs_dentry,
1557 ECRYPTFS_DONT_VALIDATE_HEADER_SIZE);
1558 if (rc) {
1559 printk(KERN_DEBUG "Valid eCryptfs headers not found in "
Tim Gardner30373dc2012-01-12 16:31:55 +01001560 "file xattr region either, inode %lu\n",
1561 ecryptfs_inode->i_ino);
Michael Halcrowdd2a3b72007-02-12 00:53:46 -08001562 rc = -EINVAL;
1563 }
1564 if (crypt_stat->mount_crypt_stat->flags
1565 & ECRYPTFS_XATTR_METADATA_ENABLED) {
1566 crypt_stat->flags |= ECRYPTFS_METADATA_IN_XATTR;
1567 } else {
1568 printk(KERN_WARNING "Attempt to access file with "
1569 "crypto metadata only in the extended attribute "
1570 "region, but eCryptfs was mounted without "
1571 "xattr support enabled. eCryptfs will not treat "
Tim Gardner30373dc2012-01-12 16:31:55 +01001572 "this like an encrypted file, inode %lu\n",
1573 ecryptfs_inode->i_ino);
Michael Halcrowdd2a3b72007-02-12 00:53:46 -08001574 rc = -EINVAL;
1575 }
Michael Halcrow237fead2006-10-04 02:16:22 -07001576 }
1577out:
1578 if (page_virt) {
1579 memset(page_virt, 0, PAGE_CACHE_SIZE);
Tyler Hicks30632872011-05-24 05:11:12 -05001580 kmem_cache_free(ecryptfs_header_cache, page_virt);
Michael Halcrow237fead2006-10-04 02:16:22 -07001581 }
1582 return rc;
1583}
1584
1585/**
Michael Halcrow51ca58d2009-01-06 14:41:59 -08001586 * ecryptfs_encrypt_filename - encrypt filename
1587 *
1588 * CBC-encrypts the filename. We do not want to encrypt the same
1589 * filename with the same key and IV, which may happen with hard
1590 * links, so we prepend random bits to each filename.
1591 *
1592 * Returns zero on success; non-zero otherwise
1593 */
1594static int
1595ecryptfs_encrypt_filename(struct ecryptfs_filename *filename,
1596 struct ecryptfs_crypt_stat *crypt_stat,
1597 struct ecryptfs_mount_crypt_stat *mount_crypt_stat)
1598{
1599 int rc = 0;
1600
1601 filename->encrypted_filename = NULL;
1602 filename->encrypted_filename_size = 0;
1603 if ((crypt_stat && (crypt_stat->flags & ECRYPTFS_ENCFN_USE_MOUNT_FNEK))
1604 || (mount_crypt_stat && (mount_crypt_stat->flags
1605 & ECRYPTFS_GLOBAL_ENCFN_USE_MOUNT_FNEK))) {
1606 size_t packet_size;
1607 size_t remaining_bytes;
1608
1609 rc = ecryptfs_write_tag_70_packet(
1610 NULL, NULL,
1611 &filename->encrypted_filename_size,
1612 mount_crypt_stat, NULL,
1613 filename->filename_size);
1614 if (rc) {
1615 printk(KERN_ERR "%s: Error attempting to get packet "
1616 "size for tag 72; rc = [%d]\n", __func__,
1617 rc);
1618 filename->encrypted_filename_size = 0;
1619 goto out;
1620 }
1621 filename->encrypted_filename =
1622 kmalloc(filename->encrypted_filename_size, GFP_KERNEL);
1623 if (!filename->encrypted_filename) {
1624 printk(KERN_ERR "%s: Out of memory whilst attempting "
Michael Halcrowdf261c52009-01-06 14:42:02 -08001625 "to kmalloc [%zd] bytes\n", __func__,
Michael Halcrow51ca58d2009-01-06 14:41:59 -08001626 filename->encrypted_filename_size);
1627 rc = -ENOMEM;
1628 goto out;
1629 }
1630 remaining_bytes = filename->encrypted_filename_size;
1631 rc = ecryptfs_write_tag_70_packet(filename->encrypted_filename,
1632 &remaining_bytes,
1633 &packet_size,
1634 mount_crypt_stat,
1635 filename->filename,
1636 filename->filename_size);
1637 if (rc) {
1638 printk(KERN_ERR "%s: Error attempting to generate "
1639 "tag 70 packet; rc = [%d]\n", __func__,
1640 rc);
1641 kfree(filename->encrypted_filename);
1642 filename->encrypted_filename = NULL;
1643 filename->encrypted_filename_size = 0;
1644 goto out;
1645 }
1646 filename->encrypted_filename_size = packet_size;
1647 } else {
1648 printk(KERN_ERR "%s: No support for requested filename "
1649 "encryption method in this release\n", __func__);
Tyler Hicksdf6ad332009-08-21 04:27:46 -05001650 rc = -EOPNOTSUPP;
Michael Halcrow51ca58d2009-01-06 14:41:59 -08001651 goto out;
1652 }
1653out:
1654 return rc;
1655}
1656
1657static int ecryptfs_copy_filename(char **copied_name, size_t *copied_name_size,
1658 const char *name, size_t name_size)
1659{
1660 int rc = 0;
1661
Tyler Hicksfd9fc842009-02-06 18:06:51 -06001662 (*copied_name) = kmalloc((name_size + 1), GFP_KERNEL);
Michael Halcrow51ca58d2009-01-06 14:41:59 -08001663 if (!(*copied_name)) {
1664 rc = -ENOMEM;
1665 goto out;
1666 }
1667 memcpy((void *)(*copied_name), (void *)name, name_size);
1668 (*copied_name)[(name_size)] = '\0'; /* Only for convenience
1669 * in printing out the
1670 * string in debug
1671 * messages */
Tyler Hicksfd9fc842009-02-06 18:06:51 -06001672 (*copied_name_size) = name_size;
Michael Halcrow51ca58d2009-01-06 14:41:59 -08001673out:
1674 return rc;
1675}
1676
1677/**
Michael Halcrowf4aad162007-10-16 01:27:53 -07001678 * ecryptfs_process_key_cipher - Perform key cipher initialization.
Michael Halcrow237fead2006-10-04 02:16:22 -07001679 * @key_tfm: Crypto context for key material, set by this function
Michael Halcrowe5d9cbd2006-10-30 22:07:16 -08001680 * @cipher_name: Name of the cipher
1681 * @key_size: Size of the key in bytes
Michael Halcrow237fead2006-10-04 02:16:22 -07001682 *
1683 * Returns zero on success. Any crypto_tfm structs allocated here
1684 * should be released by other functions, such as on a superblock put
1685 * event, regardless of whether this function succeeds for fails.
1686 */
Michael Halcrowcd9d67d2007-10-16 01:28:04 -07001687static int
Michael Halcrowf4aad162007-10-16 01:27:53 -07001688ecryptfs_process_key_cipher(struct crypto_blkcipher **key_tfm,
1689 char *cipher_name, size_t *key_size)
Michael Halcrow237fead2006-10-04 02:16:22 -07001690{
1691 char dummy_key[ECRYPTFS_MAX_KEY_BYTES];
Dan Carpenterece550f2010-01-19 12:34:32 +03001692 char *full_alg_name = NULL;
Michael Halcrow237fead2006-10-04 02:16:22 -07001693 int rc;
1694
Michael Halcrowe5d9cbd2006-10-30 22:07:16 -08001695 *key_tfm = NULL;
1696 if (*key_size > ECRYPTFS_MAX_KEY_BYTES) {
Michael Halcrow237fead2006-10-04 02:16:22 -07001697 rc = -EINVAL;
Michael Halcrowdf261c52009-01-06 14:42:02 -08001698 printk(KERN_ERR "Requested key size is [%zd] bytes; maximum "
Michael Halcrowe5d9cbd2006-10-30 22:07:16 -08001699 "allowable is [%d]\n", *key_size, ECRYPTFS_MAX_KEY_BYTES);
Michael Halcrow237fead2006-10-04 02:16:22 -07001700 goto out;
1701 }
Michael Halcrow8bba0662006-10-30 22:07:18 -08001702 rc = ecryptfs_crypto_api_algify_cipher_name(&full_alg_name, cipher_name,
1703 "ecb");
1704 if (rc)
1705 goto out;
1706 *key_tfm = crypto_alloc_blkcipher(full_alg_name, 0, CRYPTO_ALG_ASYNC);
Michael Halcrow8bba0662006-10-30 22:07:18 -08001707 if (IS_ERR(*key_tfm)) {
1708 rc = PTR_ERR(*key_tfm);
Michael Halcrow237fead2006-10-04 02:16:22 -07001709 printk(KERN_ERR "Unable to allocate crypto cipher with name "
Dave Hansen38268492009-08-27 09:47:07 -07001710 "[%s]; rc = [%d]\n", full_alg_name, rc);
Michael Halcrow237fead2006-10-04 02:16:22 -07001711 goto out;
1712 }
Michael Halcrow8bba0662006-10-30 22:07:18 -08001713 crypto_blkcipher_set_flags(*key_tfm, CRYPTO_TFM_REQ_WEAK_KEY);
1714 if (*key_size == 0) {
1715 struct blkcipher_alg *alg = crypto_blkcipher_alg(*key_tfm);
1716
1717 *key_size = alg->max_keysize;
1718 }
Michael Halcrowe5d9cbd2006-10-30 22:07:16 -08001719 get_random_bytes(dummy_key, *key_size);
Michael Halcrow8bba0662006-10-30 22:07:18 -08001720 rc = crypto_blkcipher_setkey(*key_tfm, dummy_key, *key_size);
Michael Halcrow237fead2006-10-04 02:16:22 -07001721 if (rc) {
Michael Halcrowdf261c52009-01-06 14:42:02 -08001722 printk(KERN_ERR "Error attempting to set key of size [%zd] for "
Dave Hansen38268492009-08-27 09:47:07 -07001723 "cipher [%s]; rc = [%d]\n", *key_size, full_alg_name,
1724 rc);
Michael Halcrow237fead2006-10-04 02:16:22 -07001725 rc = -EINVAL;
1726 goto out;
1727 }
1728out:
Dan Carpenterece550f2010-01-19 12:34:32 +03001729 kfree(full_alg_name);
Michael Halcrow237fead2006-10-04 02:16:22 -07001730 return rc;
1731}
Michael Halcrowf4aad162007-10-16 01:27:53 -07001732
1733struct kmem_cache *ecryptfs_key_tfm_cache;
Adrian Bunk7896b632008-02-06 01:38:32 -08001734static struct list_head key_tfm_list;
Eric Sandeenaf440f52008-02-06 01:38:37 -08001735struct mutex key_tfm_list_mutex;
Michael Halcrowf4aad162007-10-16 01:27:53 -07001736
Jerome Marchand7371a382010-08-17 17:24:05 +02001737int __init ecryptfs_init_crypto(void)
Michael Halcrowf4aad162007-10-16 01:27:53 -07001738{
1739 mutex_init(&key_tfm_list_mutex);
1740 INIT_LIST_HEAD(&key_tfm_list);
1741 return 0;
1742}
1743
Eric Sandeenaf440f52008-02-06 01:38:37 -08001744/**
1745 * ecryptfs_destroy_crypto - free all cached key_tfms on key_tfm_list
1746 *
1747 * Called only at module unload time
1748 */
Michael Halcrowfcd12832007-10-16 01:28:01 -07001749int ecryptfs_destroy_crypto(void)
Michael Halcrowf4aad162007-10-16 01:27:53 -07001750{
1751 struct ecryptfs_key_tfm *key_tfm, *key_tfm_tmp;
1752
1753 mutex_lock(&key_tfm_list_mutex);
1754 list_for_each_entry_safe(key_tfm, key_tfm_tmp, &key_tfm_list,
1755 key_tfm_list) {
1756 list_del(&key_tfm->key_tfm_list);
1757 if (key_tfm->key_tfm)
1758 crypto_free_blkcipher(key_tfm->key_tfm);
1759 kmem_cache_free(ecryptfs_key_tfm_cache, key_tfm);
1760 }
1761 mutex_unlock(&key_tfm_list_mutex);
1762 return 0;
1763}
1764
1765int
1766ecryptfs_add_new_key_tfm(struct ecryptfs_key_tfm **key_tfm, char *cipher_name,
1767 size_t key_size)
1768{
1769 struct ecryptfs_key_tfm *tmp_tfm;
1770 int rc = 0;
1771
Eric Sandeenaf440f52008-02-06 01:38:37 -08001772 BUG_ON(!mutex_is_locked(&key_tfm_list_mutex));
1773
Michael Halcrowf4aad162007-10-16 01:27:53 -07001774 tmp_tfm = kmem_cache_alloc(ecryptfs_key_tfm_cache, GFP_KERNEL);
1775 if (key_tfm != NULL)
1776 (*key_tfm) = tmp_tfm;
1777 if (!tmp_tfm) {
1778 rc = -ENOMEM;
1779 printk(KERN_ERR "Error attempting to allocate from "
1780 "ecryptfs_key_tfm_cache\n");
1781 goto out;
1782 }
1783 mutex_init(&tmp_tfm->key_tfm_mutex);
1784 strncpy(tmp_tfm->cipher_name, cipher_name,
1785 ECRYPTFS_MAX_CIPHER_NAME_SIZE);
Eric Sandeenb8862902007-12-22 14:03:24 -08001786 tmp_tfm->cipher_name[ECRYPTFS_MAX_CIPHER_NAME_SIZE] = '\0';
Michael Halcrowf4aad162007-10-16 01:27:53 -07001787 tmp_tfm->key_size = key_size;
Michael Halcrow5dda6992007-10-16 01:28:06 -07001788 rc = ecryptfs_process_key_cipher(&tmp_tfm->key_tfm,
1789 tmp_tfm->cipher_name,
1790 &tmp_tfm->key_size);
1791 if (rc) {
Michael Halcrowf4aad162007-10-16 01:27:53 -07001792 printk(KERN_ERR "Error attempting to initialize key TFM "
1793 "cipher with name = [%s]; rc = [%d]\n",
1794 tmp_tfm->cipher_name, rc);
1795 kmem_cache_free(ecryptfs_key_tfm_cache, tmp_tfm);
1796 if (key_tfm != NULL)
1797 (*key_tfm) = NULL;
1798 goto out;
1799 }
Michael Halcrowf4aad162007-10-16 01:27:53 -07001800 list_add(&tmp_tfm->key_tfm_list, &key_tfm_list);
Michael Halcrowf4aad162007-10-16 01:27:53 -07001801out:
1802 return rc;
1803}
1804
Eric Sandeenaf440f52008-02-06 01:38:37 -08001805/**
1806 * ecryptfs_tfm_exists - Search for existing tfm for cipher_name.
1807 * @cipher_name: the name of the cipher to search for
1808 * @key_tfm: set to corresponding tfm if found
1809 *
1810 * Searches for cached key_tfm matching @cipher_name
1811 * Must be called with &key_tfm_list_mutex held
1812 * Returns 1 if found, with @key_tfm set
1813 * Returns 0 if not found, with @key_tfm set to NULL
1814 */
1815int ecryptfs_tfm_exists(char *cipher_name, struct ecryptfs_key_tfm **key_tfm)
1816{
1817 struct ecryptfs_key_tfm *tmp_key_tfm;
1818
1819 BUG_ON(!mutex_is_locked(&key_tfm_list_mutex));
1820
1821 list_for_each_entry(tmp_key_tfm, &key_tfm_list, key_tfm_list) {
1822 if (strcmp(tmp_key_tfm->cipher_name, cipher_name) == 0) {
1823 if (key_tfm)
1824 (*key_tfm) = tmp_key_tfm;
1825 return 1;
1826 }
1827 }
1828 if (key_tfm)
1829 (*key_tfm) = NULL;
1830 return 0;
1831}
1832
1833/**
1834 * ecryptfs_get_tfm_and_mutex_for_cipher_name
1835 *
1836 * @tfm: set to cached tfm found, or new tfm created
1837 * @tfm_mutex: set to mutex for cached tfm found, or new tfm created
1838 * @cipher_name: the name of the cipher to search for and/or add
1839 *
1840 * Sets pointers to @tfm & @tfm_mutex matching @cipher_name.
1841 * Searches for cached item first, and creates new if not found.
1842 * Returns 0 on success, non-zero if adding new cipher failed
1843 */
Michael Halcrowf4aad162007-10-16 01:27:53 -07001844int ecryptfs_get_tfm_and_mutex_for_cipher_name(struct crypto_blkcipher **tfm,
1845 struct mutex **tfm_mutex,
1846 char *cipher_name)
1847{
1848 struct ecryptfs_key_tfm *key_tfm;
1849 int rc = 0;
1850
1851 (*tfm) = NULL;
1852 (*tfm_mutex) = NULL;
Eric Sandeenaf440f52008-02-06 01:38:37 -08001853
Michael Halcrowf4aad162007-10-16 01:27:53 -07001854 mutex_lock(&key_tfm_list_mutex);
Eric Sandeenaf440f52008-02-06 01:38:37 -08001855 if (!ecryptfs_tfm_exists(cipher_name, &key_tfm)) {
1856 rc = ecryptfs_add_new_key_tfm(&key_tfm, cipher_name, 0);
1857 if (rc) {
1858 printk(KERN_ERR "Error adding new key_tfm to list; "
1859 "rc = [%d]\n", rc);
Michael Halcrowf4aad162007-10-16 01:27:53 -07001860 goto out;
1861 }
1862 }
Michael Halcrowf4aad162007-10-16 01:27:53 -07001863 (*tfm) = key_tfm->key_tfm;
1864 (*tfm_mutex) = &key_tfm->key_tfm_mutex;
1865out:
Cyrill Gorcunov71fd5172008-05-23 13:04:20 -07001866 mutex_unlock(&key_tfm_list_mutex);
Michael Halcrowf4aad162007-10-16 01:27:53 -07001867 return rc;
1868}
Michael Halcrow51ca58d2009-01-06 14:41:59 -08001869
1870/* 64 characters forming a 6-bit target field */
1871static unsigned char *portable_filename_chars = ("-.0123456789ABCD"
1872 "EFGHIJKLMNOPQRST"
1873 "UVWXYZabcdefghij"
1874 "klmnopqrstuvwxyz");
1875
1876/* We could either offset on every reverse map or just pad some 0x00's
1877 * at the front here */
Tyler Hicks0f751e62011-11-23 11:31:24 -06001878static const unsigned char filename_rev_map[256] = {
Michael Halcrow51ca58d2009-01-06 14:41:59 -08001879 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, /* 7 */
1880 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, /* 15 */
1881 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, /* 23 */
1882 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, /* 31 */
1883 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, /* 39 */
1884 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x01, 0x00, /* 47 */
1885 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, /* 55 */
1886 0x0A, 0x0B, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, /* 63 */
1887 0x00, 0x0C, 0x0D, 0x0E, 0x0F, 0x10, 0x11, 0x12, /* 71 */
1888 0x13, 0x14, 0x15, 0x16, 0x17, 0x18, 0x19, 0x1A, /* 79 */
1889 0x1B, 0x1C, 0x1D, 0x1E, 0x1F, 0x20, 0x21, 0x22, /* 87 */
1890 0x23, 0x24, 0x25, 0x00, 0x00, 0x00, 0x00, 0x00, /* 95 */
1891 0x00, 0x26, 0x27, 0x28, 0x29, 0x2A, 0x2B, 0x2C, /* 103 */
1892 0x2D, 0x2E, 0x2F, 0x30, 0x31, 0x32, 0x33, 0x34, /* 111 */
1893 0x35, 0x36, 0x37, 0x38, 0x39, 0x3A, 0x3B, 0x3C, /* 119 */
Tyler Hicks0f751e62011-11-23 11:31:24 -06001894 0x3D, 0x3E, 0x3F /* 123 - 255 initialized to 0x00 */
Michael Halcrow51ca58d2009-01-06 14:41:59 -08001895};
1896
1897/**
1898 * ecryptfs_encode_for_filename
1899 * @dst: Destination location for encoded filename
1900 * @dst_size: Size of the encoded filename in bytes
1901 * @src: Source location for the filename to encode
1902 * @src_size: Size of the source in bytes
1903 */
Cong Ding37028752012-12-07 22:21:56 +00001904static void ecryptfs_encode_for_filename(unsigned char *dst, size_t *dst_size,
Michael Halcrow51ca58d2009-01-06 14:41:59 -08001905 unsigned char *src, size_t src_size)
1906{
1907 size_t num_blocks;
1908 size_t block_num = 0;
1909 size_t dst_offset = 0;
1910 unsigned char last_block[3];
1911
1912 if (src_size == 0) {
1913 (*dst_size) = 0;
1914 goto out;
1915 }
1916 num_blocks = (src_size / 3);
1917 if ((src_size % 3) == 0) {
1918 memcpy(last_block, (&src[src_size - 3]), 3);
1919 } else {
1920 num_blocks++;
1921 last_block[2] = 0x00;
1922 switch (src_size % 3) {
1923 case 1:
1924 last_block[0] = src[src_size - 1];
1925 last_block[1] = 0x00;
1926 break;
1927 case 2:
1928 last_block[0] = src[src_size - 2];
1929 last_block[1] = src[src_size - 1];
1930 }
1931 }
1932 (*dst_size) = (num_blocks * 4);
1933 if (!dst)
1934 goto out;
1935 while (block_num < num_blocks) {
1936 unsigned char *src_block;
1937 unsigned char dst_block[4];
1938
1939 if (block_num == (num_blocks - 1))
1940 src_block = last_block;
1941 else
1942 src_block = &src[block_num * 3];
1943 dst_block[0] = ((src_block[0] >> 2) & 0x3F);
1944 dst_block[1] = (((src_block[0] << 4) & 0x30)
1945 | ((src_block[1] >> 4) & 0x0F));
1946 dst_block[2] = (((src_block[1] << 2) & 0x3C)
1947 | ((src_block[2] >> 6) & 0x03));
1948 dst_block[3] = (src_block[2] & 0x3F);
1949 dst[dst_offset++] = portable_filename_chars[dst_block[0]];
1950 dst[dst_offset++] = portable_filename_chars[dst_block[1]];
1951 dst[dst_offset++] = portable_filename_chars[dst_block[2]];
1952 dst[dst_offset++] = portable_filename_chars[dst_block[3]];
1953 block_num++;
1954 }
1955out:
1956 return;
1957}
1958
Tyler Hicks4a266202011-11-05 13:45:08 -04001959static size_t ecryptfs_max_decoded_size(size_t encoded_size)
1960{
1961 /* Not exact; conservatively long. Every block of 4
1962 * encoded characters decodes into a block of 3
1963 * decoded characters. This segment of code provides
1964 * the caller with the maximum amount of allocated
1965 * space that @dst will need to point to in a
1966 * subsequent call. */
1967 return ((encoded_size + 1) * 3) / 4;
1968}
1969
Michael Halcrow71c11c32009-01-06 14:42:05 -08001970/**
1971 * ecryptfs_decode_from_filename
1972 * @dst: If NULL, this function only sets @dst_size and returns. If
1973 * non-NULL, this function decodes the encoded octets in @src
1974 * into the memory that @dst points to.
1975 * @dst_size: Set to the size of the decoded string.
1976 * @src: The encoded set of octets to decode.
1977 * @src_size: The size of the encoded set of octets to decode.
1978 */
1979static void
1980ecryptfs_decode_from_filename(unsigned char *dst, size_t *dst_size,
1981 const unsigned char *src, size_t src_size)
Michael Halcrow51ca58d2009-01-06 14:41:59 -08001982{
1983 u8 current_bit_offset = 0;
1984 size_t src_byte_offset = 0;
1985 size_t dst_byte_offset = 0;
Michael Halcrow51ca58d2009-01-06 14:41:59 -08001986
1987 if (dst == NULL) {
Tyler Hicks4a266202011-11-05 13:45:08 -04001988 (*dst_size) = ecryptfs_max_decoded_size(src_size);
Michael Halcrow51ca58d2009-01-06 14:41:59 -08001989 goto out;
1990 }
1991 while (src_byte_offset < src_size) {
1992 unsigned char src_byte =
1993 filename_rev_map[(int)src[src_byte_offset]];
1994
1995 switch (current_bit_offset) {
1996 case 0:
1997 dst[dst_byte_offset] = (src_byte << 2);
1998 current_bit_offset = 6;
1999 break;
2000 case 6:
2001 dst[dst_byte_offset++] |= (src_byte >> 4);
2002 dst[dst_byte_offset] = ((src_byte & 0xF)
2003 << 4);
2004 current_bit_offset = 4;
2005 break;
2006 case 4:
2007 dst[dst_byte_offset++] |= (src_byte >> 2);
2008 dst[dst_byte_offset] = (src_byte << 6);
2009 current_bit_offset = 2;
2010 break;
2011 case 2:
2012 dst[dst_byte_offset++] |= (src_byte);
2013 dst[dst_byte_offset] = 0;
2014 current_bit_offset = 0;
2015 break;
2016 }
2017 src_byte_offset++;
2018 }
2019 (*dst_size) = dst_byte_offset;
2020out:
Michael Halcrow71c11c32009-01-06 14:42:05 -08002021 return;
Michael Halcrow51ca58d2009-01-06 14:41:59 -08002022}
2023
2024/**
2025 * ecryptfs_encrypt_and_encode_filename - converts a plaintext file name to cipher text
2026 * @crypt_stat: The crypt_stat struct associated with the file anem to encode
2027 * @name: The plaintext name
2028 * @length: The length of the plaintext
2029 * @encoded_name: The encypted name
2030 *
2031 * Encrypts and encodes a filename into something that constitutes a
2032 * valid filename for a filesystem, with printable characters.
2033 *
2034 * We assume that we have a properly initialized crypto context,
2035 * pointed to by crypt_stat->tfm.
2036 *
2037 * Returns zero on success; non-zero on otherwise
2038 */
2039int ecryptfs_encrypt_and_encode_filename(
2040 char **encoded_name,
2041 size_t *encoded_name_size,
2042 struct ecryptfs_crypt_stat *crypt_stat,
2043 struct ecryptfs_mount_crypt_stat *mount_crypt_stat,
2044 const char *name, size_t name_size)
2045{
2046 size_t encoded_name_no_prefix_size;
2047 int rc = 0;
2048
2049 (*encoded_name) = NULL;
2050 (*encoded_name_size) = 0;
2051 if ((crypt_stat && (crypt_stat->flags & ECRYPTFS_ENCRYPT_FILENAMES))
2052 || (mount_crypt_stat && (mount_crypt_stat->flags
2053 & ECRYPTFS_GLOBAL_ENCRYPT_FILENAMES))) {
2054 struct ecryptfs_filename *filename;
2055
2056 filename = kzalloc(sizeof(*filename), GFP_KERNEL);
2057 if (!filename) {
2058 printk(KERN_ERR "%s: Out of memory whilst attempting "
Michael Halcrowa8f12862009-01-06 14:42:03 -08002059 "to kzalloc [%zd] bytes\n", __func__,
Michael Halcrow51ca58d2009-01-06 14:41:59 -08002060 sizeof(*filename));
2061 rc = -ENOMEM;
2062 goto out;
2063 }
2064 filename->filename = (char *)name;
2065 filename->filename_size = name_size;
2066 rc = ecryptfs_encrypt_filename(filename, crypt_stat,
2067 mount_crypt_stat);
2068 if (rc) {
2069 printk(KERN_ERR "%s: Error attempting to encrypt "
2070 "filename; rc = [%d]\n", __func__, rc);
2071 kfree(filename);
2072 goto out;
2073 }
2074 ecryptfs_encode_for_filename(
2075 NULL, &encoded_name_no_prefix_size,
2076 filename->encrypted_filename,
2077 filename->encrypted_filename_size);
2078 if ((crypt_stat && (crypt_stat->flags
2079 & ECRYPTFS_ENCFN_USE_MOUNT_FNEK))
2080 || (mount_crypt_stat
2081 && (mount_crypt_stat->flags
2082 & ECRYPTFS_GLOBAL_ENCFN_USE_MOUNT_FNEK)))
2083 (*encoded_name_size) =
2084 (ECRYPTFS_FNEK_ENCRYPTED_FILENAME_PREFIX_SIZE
2085 + encoded_name_no_prefix_size);
2086 else
2087 (*encoded_name_size) =
2088 (ECRYPTFS_FEK_ENCRYPTED_FILENAME_PREFIX_SIZE
2089 + encoded_name_no_prefix_size);
2090 (*encoded_name) = kmalloc((*encoded_name_size) + 1, GFP_KERNEL);
2091 if (!(*encoded_name)) {
2092 printk(KERN_ERR "%s: Out of memory whilst attempting "
Michael Halcrowa8f12862009-01-06 14:42:03 -08002093 "to kzalloc [%zd] bytes\n", __func__,
Michael Halcrow51ca58d2009-01-06 14:41:59 -08002094 (*encoded_name_size));
2095 rc = -ENOMEM;
2096 kfree(filename->encrypted_filename);
2097 kfree(filename);
2098 goto out;
2099 }
2100 if ((crypt_stat && (crypt_stat->flags
2101 & ECRYPTFS_ENCFN_USE_MOUNT_FNEK))
2102 || (mount_crypt_stat
2103 && (mount_crypt_stat->flags
2104 & ECRYPTFS_GLOBAL_ENCFN_USE_MOUNT_FNEK))) {
2105 memcpy((*encoded_name),
2106 ECRYPTFS_FNEK_ENCRYPTED_FILENAME_PREFIX,
2107 ECRYPTFS_FNEK_ENCRYPTED_FILENAME_PREFIX_SIZE);
2108 ecryptfs_encode_for_filename(
2109 ((*encoded_name)
2110 + ECRYPTFS_FNEK_ENCRYPTED_FILENAME_PREFIX_SIZE),
2111 &encoded_name_no_prefix_size,
2112 filename->encrypted_filename,
2113 filename->encrypted_filename_size);
2114 (*encoded_name_size) =
2115 (ECRYPTFS_FNEK_ENCRYPTED_FILENAME_PREFIX_SIZE
2116 + encoded_name_no_prefix_size);
2117 (*encoded_name)[(*encoded_name_size)] = '\0';
Michael Halcrow51ca58d2009-01-06 14:41:59 -08002118 } else {
Tyler Hicksdf6ad332009-08-21 04:27:46 -05002119 rc = -EOPNOTSUPP;
Michael Halcrow51ca58d2009-01-06 14:41:59 -08002120 }
2121 if (rc) {
2122 printk(KERN_ERR "%s: Error attempting to encode "
2123 "encrypted filename; rc = [%d]\n", __func__,
2124 rc);
2125 kfree((*encoded_name));
2126 (*encoded_name) = NULL;
2127 (*encoded_name_size) = 0;
2128 }
2129 kfree(filename->encrypted_filename);
2130 kfree(filename);
2131 } else {
2132 rc = ecryptfs_copy_filename(encoded_name,
2133 encoded_name_size,
2134 name, name_size);
2135 }
2136out:
2137 return rc;
2138}
2139
2140/**
2141 * ecryptfs_decode_and_decrypt_filename - converts the encoded cipher text name to decoded plaintext
2142 * @plaintext_name: The plaintext name
2143 * @plaintext_name_size: The plaintext name size
2144 * @ecryptfs_dir_dentry: eCryptfs directory dentry
2145 * @name: The filename in cipher text
2146 * @name_size: The cipher text name size
2147 *
2148 * Decrypts and decodes the filename.
2149 *
2150 * Returns zero on error; non-zero otherwise
2151 */
2152int ecryptfs_decode_and_decrypt_filename(char **plaintext_name,
2153 size_t *plaintext_name_size,
2154 struct dentry *ecryptfs_dir_dentry,
2155 const char *name, size_t name_size)
2156{
Tyler Hicks2aac0cf2009-03-20 02:23:57 -05002157 struct ecryptfs_mount_crypt_stat *mount_crypt_stat =
2158 &ecryptfs_superblock_to_private(
2159 ecryptfs_dir_dentry->d_sb)->mount_crypt_stat;
Michael Halcrow51ca58d2009-01-06 14:41:59 -08002160 char *decoded_name;
2161 size_t decoded_name_size;
2162 size_t packet_size;
2163 int rc = 0;
2164
Tyler Hicks2aac0cf2009-03-20 02:23:57 -05002165 if ((mount_crypt_stat->flags & ECRYPTFS_GLOBAL_ENCRYPT_FILENAMES)
2166 && !(mount_crypt_stat->flags & ECRYPTFS_ENCRYPTED_VIEW_ENABLED)
2167 && (name_size > ECRYPTFS_FNEK_ENCRYPTED_FILENAME_PREFIX_SIZE)
Michael Halcrow51ca58d2009-01-06 14:41:59 -08002168 && (strncmp(name, ECRYPTFS_FNEK_ENCRYPTED_FILENAME_PREFIX,
2169 ECRYPTFS_FNEK_ENCRYPTED_FILENAME_PREFIX_SIZE) == 0)) {
Michael Halcrow51ca58d2009-01-06 14:41:59 -08002170 const char *orig_name = name;
2171 size_t orig_name_size = name_size;
2172
2173 name += ECRYPTFS_FNEK_ENCRYPTED_FILENAME_PREFIX_SIZE;
2174 name_size -= ECRYPTFS_FNEK_ENCRYPTED_FILENAME_PREFIX_SIZE;
Michael Halcrow71c11c32009-01-06 14:42:05 -08002175 ecryptfs_decode_from_filename(NULL, &decoded_name_size,
2176 name, name_size);
Michael Halcrow51ca58d2009-01-06 14:41:59 -08002177 decoded_name = kmalloc(decoded_name_size, GFP_KERNEL);
2178 if (!decoded_name) {
2179 printk(KERN_ERR "%s: Out of memory whilst attempting "
Michael Halcrowdf261c52009-01-06 14:42:02 -08002180 "to kmalloc [%zd] bytes\n", __func__,
Michael Halcrow51ca58d2009-01-06 14:41:59 -08002181 decoded_name_size);
2182 rc = -ENOMEM;
2183 goto out;
2184 }
Michael Halcrow71c11c32009-01-06 14:42:05 -08002185 ecryptfs_decode_from_filename(decoded_name, &decoded_name_size,
2186 name, name_size);
Michael Halcrow51ca58d2009-01-06 14:41:59 -08002187 rc = ecryptfs_parse_tag_70_packet(plaintext_name,
2188 plaintext_name_size,
2189 &packet_size,
2190 mount_crypt_stat,
2191 decoded_name,
2192 decoded_name_size);
2193 if (rc) {
2194 printk(KERN_INFO "%s: Could not parse tag 70 packet "
2195 "from filename; copying through filename "
2196 "as-is\n", __func__);
2197 rc = ecryptfs_copy_filename(plaintext_name,
2198 plaintext_name_size,
2199 orig_name, orig_name_size);
2200 goto out_free;
2201 }
2202 } else {
2203 rc = ecryptfs_copy_filename(plaintext_name,
2204 plaintext_name_size,
2205 name, name_size);
2206 goto out;
2207 }
2208out_free:
2209 kfree(decoded_name);
2210out:
2211 return rc;
2212}
Tyler Hicks4a266202011-11-05 13:45:08 -04002213
2214#define ENC_NAME_MAX_BLOCKLEN_8_OR_16 143
2215
2216int ecryptfs_set_f_namelen(long *namelen, long lower_namelen,
2217 struct ecryptfs_mount_crypt_stat *mount_crypt_stat)
2218{
2219 struct blkcipher_desc desc;
2220 struct mutex *tfm_mutex;
2221 size_t cipher_blocksize;
2222 int rc;
2223
2224 if (!(mount_crypt_stat->flags & ECRYPTFS_GLOBAL_ENCRYPT_FILENAMES)) {
2225 (*namelen) = lower_namelen;
2226 return 0;
2227 }
2228
2229 rc = ecryptfs_get_tfm_and_mutex_for_cipher_name(&desc.tfm, &tfm_mutex,
2230 mount_crypt_stat->global_default_fn_cipher_name);
2231 if (unlikely(rc)) {
2232 (*namelen) = 0;
2233 return rc;
2234 }
2235
2236 mutex_lock(tfm_mutex);
2237 cipher_blocksize = crypto_blkcipher_blocksize(desc.tfm);
2238 mutex_unlock(tfm_mutex);
2239
2240 /* Return an exact amount for the common cases */
2241 if (lower_namelen == NAME_MAX
2242 && (cipher_blocksize == 8 || cipher_blocksize == 16)) {
2243 (*namelen) = ENC_NAME_MAX_BLOCKLEN_8_OR_16;
2244 return 0;
2245 }
2246
2247 /* Return a safe estimate for the uncommon cases */
2248 (*namelen) = lower_namelen;
2249 (*namelen) -= ECRYPTFS_FNEK_ENCRYPTED_FILENAME_PREFIX_SIZE;
2250 /* Since this is the max decoded size, subtract 1 "decoded block" len */
2251 (*namelen) = ecryptfs_max_decoded_size(*namelen) - 3;
2252 (*namelen) -= ECRYPTFS_TAG_70_MAX_METADATA_SIZE;
2253 (*namelen) -= ECRYPTFS_FILENAME_MIN_RANDOM_PREPEND_BYTES;
2254 /* Worst case is that the filename is padded nearly a full block size */
2255 (*namelen) -= cipher_blocksize - 1;
2256
2257 if ((*namelen) < 0)
2258 (*namelen) = 0;
2259
2260 return 0;
2261}