Commit | Line | Data |
---|---|---|
237fead6 MH |
1 | /** |
2 | * eCryptfs: Linux filesystem encryption layer | |
3 | * | |
4 | * Copyright (C) 1997-2004 Erez Zadok | |
5 | * Copyright (C) 2001-2004 Stony Brook University | |
dd2a3b7a | 6 | * Copyright (C) 2004-2007 International Business Machines Corp. |
237fead6 MH |
7 | * 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> | |
5a0e3ad6 | 36 | #include <linux/slab.h> |
29335c6a | 37 | #include <asm/unaligned.h> |
237fead6 MH |
38 | #include "ecryptfs_kernel.h" |
39 | ||
40 | static int | |
41 | ecryptfs_decrypt_page_offset(struct ecryptfs_crypt_stat *crypt_stat, | |
42 | struct page *dst_page, int dst_offset, | |
43 | struct page *src_page, int src_offset, int size, | |
44 | unsigned char *iv); | |
45 | static int | |
46 | ecryptfs_encrypt_page_offset(struct ecryptfs_crypt_stat *crypt_stat, | |
47 | struct page *dst_page, int dst_offset, | |
48 | struct page *src_page, int src_offset, int size, | |
49 | unsigned char *iv); | |
50 | ||
51 | /** | |
52 | * ecryptfs_to_hex | |
53 | * @dst: Buffer to take hex character representation of contents of | |
54 | * src; must be at least of size (src_size * 2) | |
55 | * @src: Buffer to be converted to a hex string respresentation | |
56 | * @src_size: number of bytes to convert | |
57 | */ | |
58 | void ecryptfs_to_hex(char *dst, char *src, size_t src_size) | |
59 | { | |
60 | int x; | |
61 | ||
62 | for (x = 0; x < src_size; x++) | |
63 | sprintf(&dst[x * 2], "%.2x", (unsigned char)src[x]); | |
64 | } | |
65 | ||
66 | /** | |
67 | * ecryptfs_from_hex | |
68 | * @dst: Buffer to take the bytes from src hex; must be at least of | |
69 | * size (src_size / 2) | |
70 | * @src: Buffer to be converted from a hex string respresentation to raw value | |
71 | * @dst_size: size of dst buffer, or number of hex characters pairs to convert | |
72 | */ | |
73 | void ecryptfs_from_hex(char *dst, char *src, int dst_size) | |
74 | { | |
75 | int x; | |
76 | char tmp[3] = { 0, }; | |
77 | ||
78 | for (x = 0; x < dst_size; x++) { | |
79 | tmp[0] = src[x * 2]; | |
80 | tmp[1] = src[x * 2 + 1]; | |
81 | dst[x] = (unsigned char)simple_strtol(tmp, NULL, 16); | |
82 | } | |
83 | } | |
84 | ||
85 | /** | |
86 | * ecryptfs_calculate_md5 - calculates the md5 of @src | |
87 | * @dst: Pointer to 16 bytes of allocated memory | |
88 | * @crypt_stat: Pointer to crypt_stat struct for the current inode | |
89 | * @src: Data to be md5'd | |
90 | * @len: Length of @src | |
91 | * | |
92 | * Uses the allocated crypto context that crypt_stat references to | |
93 | * generate the MD5 sum of the contents of src. | |
94 | */ | |
95 | static int ecryptfs_calculate_md5(char *dst, | |
96 | struct ecryptfs_crypt_stat *crypt_stat, | |
97 | char *src, int len) | |
98 | { | |
237fead6 | 99 | struct scatterlist sg; |
565d9724 MH |
100 | struct hash_desc desc = { |
101 | .tfm = crypt_stat->hash_tfm, | |
102 | .flags = CRYPTO_TFM_REQ_MAY_SLEEP | |
103 | }; | |
104 | int rc = 0; | |
237fead6 | 105 | |
565d9724 | 106 | mutex_lock(&crypt_stat->cs_hash_tfm_mutex); |
237fead6 | 107 | sg_init_one(&sg, (u8 *)src, len); |
565d9724 MH |
108 | if (!desc.tfm) { |
109 | desc.tfm = crypto_alloc_hash(ECRYPTFS_DEFAULT_HASH, 0, | |
110 | CRYPTO_ALG_ASYNC); | |
111 | if (IS_ERR(desc.tfm)) { | |
112 | rc = PTR_ERR(desc.tfm); | |
237fead6 | 113 | ecryptfs_printk(KERN_ERR, "Error attempting to " |
565d9724 MH |
114 | "allocate crypto context; rc = [%d]\n", |
115 | rc); | |
237fead6 MH |
116 | goto out; |
117 | } | |
565d9724 | 118 | crypt_stat->hash_tfm = desc.tfm; |
237fead6 | 119 | } |
8a29f2b0 MH |
120 | rc = crypto_hash_init(&desc); |
121 | if (rc) { | |
122 | printk(KERN_ERR | |
123 | "%s: Error initializing crypto hash; rc = [%d]\n", | |
18d1dbf1 | 124 | __func__, rc); |
8a29f2b0 MH |
125 | goto out; |
126 | } | |
127 | rc = crypto_hash_update(&desc, &sg, len); | |
128 | if (rc) { | |
129 | printk(KERN_ERR | |
130 | "%s: Error updating crypto hash; rc = [%d]\n", | |
18d1dbf1 | 131 | __func__, rc); |
8a29f2b0 MH |
132 | goto out; |
133 | } | |
134 | rc = crypto_hash_final(&desc, dst); | |
135 | if (rc) { | |
136 | printk(KERN_ERR | |
137 | "%s: Error finalizing crypto hash; rc = [%d]\n", | |
18d1dbf1 | 138 | __func__, rc); |
8a29f2b0 MH |
139 | goto out; |
140 | } | |
237fead6 | 141 | out: |
8a29f2b0 | 142 | mutex_unlock(&crypt_stat->cs_hash_tfm_mutex); |
237fead6 MH |
143 | return rc; |
144 | } | |
145 | ||
cd9d67df MH |
146 | static int ecryptfs_crypto_api_algify_cipher_name(char **algified_name, |
147 | char *cipher_name, | |
148 | char *chaining_modifier) | |
8bba066f MH |
149 | { |
150 | int cipher_name_len = strlen(cipher_name); | |
151 | int chaining_modifier_len = strlen(chaining_modifier); | |
152 | int algified_name_len; | |
153 | int rc; | |
154 | ||
155 | algified_name_len = (chaining_modifier_len + cipher_name_len + 3); | |
156 | (*algified_name) = kmalloc(algified_name_len, GFP_KERNEL); | |
7bd473fc | 157 | if (!(*algified_name)) { |
8bba066f MH |
158 | rc = -ENOMEM; |
159 | goto out; | |
160 | } | |
161 | snprintf((*algified_name), algified_name_len, "%s(%s)", | |
162 | chaining_modifier, cipher_name); | |
163 | rc = 0; | |
164 | out: | |
165 | return rc; | |
166 | } | |
167 | ||
237fead6 MH |
168 | /** |
169 | * ecryptfs_derive_iv | |
170 | * @iv: destination for the derived iv vale | |
171 | * @crypt_stat: Pointer to crypt_stat struct for the current inode | |
d6a13c17 | 172 | * @offset: Offset of the extent whose IV we are to derive |
237fead6 MH |
173 | * |
174 | * Generate the initialization vector from the given root IV and page | |
175 | * offset. | |
176 | * | |
177 | * Returns zero on success; non-zero on error. | |
178 | */ | |
a34f60f7 MH |
179 | int ecryptfs_derive_iv(char *iv, struct ecryptfs_crypt_stat *crypt_stat, |
180 | loff_t offset) | |
237fead6 MH |
181 | { |
182 | int rc = 0; | |
183 | char dst[MD5_DIGEST_SIZE]; | |
184 | char src[ECRYPTFS_MAX_IV_BYTES + 16]; | |
185 | ||
186 | if (unlikely(ecryptfs_verbosity > 0)) { | |
187 | ecryptfs_printk(KERN_DEBUG, "root iv:\n"); | |
188 | ecryptfs_dump_hex(crypt_stat->root_iv, crypt_stat->iv_bytes); | |
189 | } | |
190 | /* TODO: It is probably secure to just cast the least | |
191 | * significant bits of the root IV into an unsigned long and | |
192 | * add the offset to that rather than go through all this | |
193 | * hashing business. -Halcrow */ | |
194 | memcpy(src, crypt_stat->root_iv, crypt_stat->iv_bytes); | |
195 | memset((src + crypt_stat->iv_bytes), 0, 16); | |
d6a13c17 | 196 | snprintf((src + crypt_stat->iv_bytes), 16, "%lld", offset); |
237fead6 MH |
197 | if (unlikely(ecryptfs_verbosity > 0)) { |
198 | ecryptfs_printk(KERN_DEBUG, "source:\n"); | |
199 | ecryptfs_dump_hex(src, (crypt_stat->iv_bytes + 16)); | |
200 | } | |
201 | rc = ecryptfs_calculate_md5(dst, crypt_stat, src, | |
202 | (crypt_stat->iv_bytes + 16)); | |
203 | if (rc) { | |
204 | ecryptfs_printk(KERN_WARNING, "Error attempting to compute " | |
205 | "MD5 while generating IV for a page\n"); | |
206 | goto out; | |
207 | } | |
208 | memcpy(iv, dst, crypt_stat->iv_bytes); | |
209 | if (unlikely(ecryptfs_verbosity > 0)) { | |
210 | ecryptfs_printk(KERN_DEBUG, "derived iv:\n"); | |
211 | ecryptfs_dump_hex(iv, crypt_stat->iv_bytes); | |
212 | } | |
213 | out: | |
214 | return rc; | |
215 | } | |
216 | ||
217 | /** | |
218 | * ecryptfs_init_crypt_stat | |
219 | * @crypt_stat: Pointer to the crypt_stat struct to initialize. | |
220 | * | |
221 | * Initialize the crypt_stat structure. | |
222 | */ | |
223 | void | |
224 | ecryptfs_init_crypt_stat(struct ecryptfs_crypt_stat *crypt_stat) | |
225 | { | |
226 | memset((void *)crypt_stat, 0, sizeof(struct ecryptfs_crypt_stat)); | |
f4aad16a MH |
227 | INIT_LIST_HEAD(&crypt_stat->keysig_list); |
228 | mutex_init(&crypt_stat->keysig_list_mutex); | |
237fead6 MH |
229 | mutex_init(&crypt_stat->cs_mutex); |
230 | mutex_init(&crypt_stat->cs_tfm_mutex); | |
565d9724 | 231 | mutex_init(&crypt_stat->cs_hash_tfm_mutex); |
e2bd99ec | 232 | crypt_stat->flags |= ECRYPTFS_STRUCT_INITIALIZED; |
237fead6 MH |
233 | } |
234 | ||
235 | /** | |
fcd12835 | 236 | * ecryptfs_destroy_crypt_stat |
237fead6 MH |
237 | * @crypt_stat: Pointer to the crypt_stat struct to initialize. |
238 | * | |
239 | * Releases all memory associated with a crypt_stat struct. | |
240 | */ | |
fcd12835 | 241 | void ecryptfs_destroy_crypt_stat(struct ecryptfs_crypt_stat *crypt_stat) |
237fead6 | 242 | { |
f4aad16a MH |
243 | struct ecryptfs_key_sig *key_sig, *key_sig_tmp; |
244 | ||
237fead6 | 245 | if (crypt_stat->tfm) |
4dfea4f0 | 246 | crypto_free_ablkcipher(crypt_stat->tfm); |
565d9724 MH |
247 | if (crypt_stat->hash_tfm) |
248 | crypto_free_hash(crypt_stat->hash_tfm); | |
f4aad16a MH |
249 | list_for_each_entry_safe(key_sig, key_sig_tmp, |
250 | &crypt_stat->keysig_list, crypt_stat_list) { | |
251 | list_del(&key_sig->crypt_stat_list); | |
252 | kmem_cache_free(ecryptfs_key_sig_cache, key_sig); | |
253 | } | |
237fead6 MH |
254 | memset(crypt_stat, 0, sizeof(struct ecryptfs_crypt_stat)); |
255 | } | |
256 | ||
fcd12835 | 257 | void ecryptfs_destroy_mount_crypt_stat( |
237fead6 MH |
258 | struct ecryptfs_mount_crypt_stat *mount_crypt_stat) |
259 | { | |
f4aad16a MH |
260 | struct ecryptfs_global_auth_tok *auth_tok, *auth_tok_tmp; |
261 | ||
262 | if (!(mount_crypt_stat->flags & ECRYPTFS_MOUNT_CRYPT_STAT_INITIALIZED)) | |
263 | return; | |
264 | mutex_lock(&mount_crypt_stat->global_auth_tok_list_mutex); | |
265 | list_for_each_entry_safe(auth_tok, auth_tok_tmp, | |
266 | &mount_crypt_stat->global_auth_tok_list, | |
267 | mount_crypt_stat_list) { | |
268 | list_del(&auth_tok->mount_crypt_stat_list); | |
f4aad16a MH |
269 | if (auth_tok->global_auth_tok_key |
270 | && !(auth_tok->flags & ECRYPTFS_AUTH_TOK_INVALID)) | |
271 | key_put(auth_tok->global_auth_tok_key); | |
272 | kmem_cache_free(ecryptfs_global_auth_tok_cache, auth_tok); | |
273 | } | |
274 | mutex_unlock(&mount_crypt_stat->global_auth_tok_list_mutex); | |
237fead6 MH |
275 | memset(mount_crypt_stat, 0, sizeof(struct ecryptfs_mount_crypt_stat)); |
276 | } | |
277 | ||
278 | /** | |
279 | * virt_to_scatterlist | |
280 | * @addr: Virtual address | |
281 | * @size: Size of data; should be an even multiple of the block size | |
282 | * @sg: Pointer to scatterlist array; set to NULL to obtain only | |
283 | * the number of scatterlist structs required in array | |
284 | * @sg_size: Max array size | |
285 | * | |
286 | * Fills in a scatterlist array with page references for a passed | |
287 | * virtual address. | |
288 | * | |
289 | * Returns the number of scatterlist structs in array used | |
290 | */ | |
291 | int virt_to_scatterlist(const void *addr, int size, struct scatterlist *sg, | |
292 | int sg_size) | |
293 | { | |
294 | int i = 0; | |
295 | struct page *pg; | |
296 | int offset; | |
297 | int remainder_of_page; | |
298 | ||
68e3f5dd HX |
299 | sg_init_table(sg, sg_size); |
300 | ||
237fead6 MH |
301 | while (size > 0 && i < sg_size) { |
302 | pg = virt_to_page(addr); | |
303 | offset = offset_in_page(addr); | |
a07c48ad | 304 | sg_set_page(&sg[i], pg, 0, offset); |
237fead6 MH |
305 | remainder_of_page = PAGE_CACHE_SIZE - offset; |
306 | if (size >= remainder_of_page) { | |
a07c48ad | 307 | sg[i].length = remainder_of_page; |
237fead6 MH |
308 | addr += remainder_of_page; |
309 | size -= remainder_of_page; | |
310 | } else { | |
a07c48ad | 311 | sg[i].length = size; |
237fead6 MH |
312 | addr += size; |
313 | size = 0; | |
314 | } | |
315 | i++; | |
316 | } | |
317 | if (size > 0) | |
318 | return -ENOMEM; | |
319 | return i; | |
320 | } | |
321 | ||
4dfea4f0 TH |
322 | struct extent_crypt_result { |
323 | struct completion completion; | |
324 | int rc; | |
325 | }; | |
326 | ||
327 | static void extent_crypt_complete(struct crypto_async_request *req, int rc) | |
328 | { | |
329 | struct extent_crypt_result *ecr = req->data; | |
330 | ||
331 | if (rc == -EINPROGRESS) | |
332 | return; | |
333 | ||
334 | ecr->rc = rc; | |
335 | complete(&ecr->completion); | |
336 | } | |
337 | ||
237fead6 MH |
338 | /** |
339 | * encrypt_scatterlist | |
340 | * @crypt_stat: Pointer to the crypt_stat struct to initialize. | |
341 | * @dest_sg: Destination of encrypted data | |
342 | * @src_sg: Data to be encrypted | |
343 | * @size: Length of data to be encrypted | |
344 | * @iv: iv to use during encryption | |
345 | * | |
346 | * Returns the number of bytes encrypted; negative value on error | |
347 | */ | |
348 | static int encrypt_scatterlist(struct ecryptfs_crypt_stat *crypt_stat, | |
349 | struct scatterlist *dest_sg, | |
350 | struct scatterlist *src_sg, int size, | |
351 | unsigned char *iv) | |
352 | { | |
4dfea4f0 TH |
353 | struct ablkcipher_request *req = NULL; |
354 | struct extent_crypt_result ecr; | |
237fead6 MH |
355 | int rc = 0; |
356 | ||
357 | BUG_ON(!crypt_stat || !crypt_stat->tfm | |
e2bd99ec | 358 | || !(crypt_stat->flags & ECRYPTFS_STRUCT_INITIALIZED)); |
237fead6 | 359 | if (unlikely(ecryptfs_verbosity > 0)) { |
f24b3887 | 360 | ecryptfs_printk(KERN_DEBUG, "Key size [%zd]; key:\n", |
237fead6 MH |
361 | crypt_stat->key_size); |
362 | ecryptfs_dump_hex(crypt_stat->key, | |
363 | crypt_stat->key_size); | |
364 | } | |
4dfea4f0 TH |
365 | |
366 | init_completion(&ecr.completion); | |
367 | ||
237fead6 | 368 | mutex_lock(&crypt_stat->cs_tfm_mutex); |
4dfea4f0 TH |
369 | req = ablkcipher_request_alloc(crypt_stat->tfm, GFP_NOFS); |
370 | if (!req) { | |
237fead6 | 371 | mutex_unlock(&crypt_stat->cs_tfm_mutex); |
4dfea4f0 | 372 | rc = -ENOMEM; |
237fead6 MH |
373 | goto out; |
374 | } | |
4dfea4f0 TH |
375 | |
376 | ablkcipher_request_set_callback(req, | |
377 | CRYPTO_TFM_REQ_MAY_BACKLOG | CRYPTO_TFM_REQ_MAY_SLEEP, | |
378 | extent_crypt_complete, &ecr); | |
379 | /* Consider doing this once, when the file is opened */ | |
380 | if (!(crypt_stat->flags & ECRYPTFS_KEY_SET)) { | |
381 | rc = crypto_ablkcipher_setkey(crypt_stat->tfm, crypt_stat->key, | |
382 | crypt_stat->key_size); | |
383 | if (rc) { | |
384 | ecryptfs_printk(KERN_ERR, | |
385 | "Error setting key; rc = [%d]\n", | |
386 | rc); | |
387 | mutex_unlock(&crypt_stat->cs_tfm_mutex); | |
388 | rc = -EINVAL; | |
389 | goto out; | |
390 | } | |
391 | crypt_stat->flags |= ECRYPTFS_KEY_SET; | |
392 | } | |
237fead6 | 393 | mutex_unlock(&crypt_stat->cs_tfm_mutex); |
4dfea4f0 TH |
394 | ecryptfs_printk(KERN_DEBUG, "Encrypting [%d] bytes.\n", size); |
395 | ablkcipher_request_set_crypt(req, src_sg, dest_sg, size, iv); | |
396 | rc = crypto_ablkcipher_encrypt(req); | |
397 | if (rc == -EINPROGRESS || rc == -EBUSY) { | |
398 | struct extent_crypt_result *ecr = req->base.data; | |
399 | ||
400 | wait_for_completion(&ecr->completion); | |
401 | rc = ecr->rc; | |
402 | INIT_COMPLETION(ecr->completion); | |
403 | } | |
237fead6 | 404 | out: |
4dfea4f0 | 405 | ablkcipher_request_free(req); |
237fead6 MH |
406 | return rc; |
407 | } | |
408 | ||
0216f7f7 MH |
409 | /** |
410 | * ecryptfs_lower_offset_for_extent | |
411 | * | |
412 | * Convert an eCryptfs page index into a lower byte offset | |
413 | */ | |
7896b631 AB |
414 | static void ecryptfs_lower_offset_for_extent(loff_t *offset, loff_t extent_num, |
415 | struct ecryptfs_crypt_stat *crypt_stat) | |
0216f7f7 | 416 | { |
157f1071 TH |
417 | (*offset) = ecryptfs_lower_header_size(crypt_stat) |
418 | + (crypt_stat->extent_size * extent_num); | |
0216f7f7 MH |
419 | } |
420 | ||
421 | /** | |
422 | * ecryptfs_encrypt_extent | |
423 | * @enc_extent_page: Allocated page into which to encrypt the data in | |
424 | * @page | |
425 | * @crypt_stat: crypt_stat containing cryptographic context for the | |
426 | * encryption operation | |
427 | * @page: Page containing plaintext data extent to encrypt | |
428 | * @extent_offset: Page extent offset for use in generating IV | |
429 | * | |
430 | * Encrypts one extent of data. | |
431 | * | |
432 | * Return zero on success; non-zero otherwise | |
433 | */ | |
434 | static int ecryptfs_encrypt_extent(struct page *enc_extent_page, | |
435 | struct ecryptfs_crypt_stat *crypt_stat, | |
436 | struct page *page, | |
437 | unsigned long extent_offset) | |
438 | { | |
d6a13c17 | 439 | loff_t extent_base; |
0216f7f7 MH |
440 | char extent_iv[ECRYPTFS_MAX_IV_BYTES]; |
441 | int rc; | |
442 | ||
d6a13c17 | 443 | extent_base = (((loff_t)page->index) |
0216f7f7 MH |
444 | * (PAGE_CACHE_SIZE / crypt_stat->extent_size)); |
445 | rc = ecryptfs_derive_iv(extent_iv, crypt_stat, | |
446 | (extent_base + extent_offset)); | |
447 | if (rc) { | |
888d57bb JP |
448 | ecryptfs_printk(KERN_ERR, "Error attempting to derive IV for " |
449 | "extent [0x%.16llx]; rc = [%d]\n", | |
450 | (unsigned long long)(extent_base + extent_offset), rc); | |
0216f7f7 MH |
451 | goto out; |
452 | } | |
12003e5b TH |
453 | rc = ecryptfs_encrypt_page_offset(crypt_stat, enc_extent_page, |
454 | extent_offset * crypt_stat->extent_size, | |
455 | page, | |
456 | extent_offset * crypt_stat->extent_size, | |
457 | crypt_stat->extent_size, extent_iv); | |
0216f7f7 MH |
458 | if (rc < 0) { |
459 | printk(KERN_ERR "%s: Error attempting to encrypt page with " | |
460 | "page->index = [%ld], extent_offset = [%ld]; " | |
18d1dbf1 | 461 | "rc = [%d]\n", __func__, page->index, extent_offset, |
0216f7f7 MH |
462 | rc); |
463 | goto out; | |
464 | } | |
465 | rc = 0; | |
0216f7f7 MH |
466 | out: |
467 | return rc; | |
468 | } | |
469 | ||
237fead6 MH |
470 | /** |
471 | * ecryptfs_encrypt_page | |
0216f7f7 MH |
472 | * @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 | |
237fead6 MH |
475 | * |
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 | * | |
237fead6 MH |
484 | * Returns zero on success; negative on error |
485 | */ | |
0216f7f7 | 486 | int ecryptfs_encrypt_page(struct page *page) |
237fead6 | 487 | { |
0216f7f7 | 488 | struct inode *ecryptfs_inode; |
237fead6 | 489 | struct ecryptfs_crypt_stat *crypt_stat; |
7fcba054 ES |
490 | char *enc_extent_virt; |
491 | struct page *enc_extent_page = NULL; | |
0216f7f7 | 492 | loff_t extent_offset; |
237fead6 | 493 | int rc = 0; |
0216f7f7 MH |
494 | |
495 | ecryptfs_inode = page->mapping->host; | |
496 | crypt_stat = | |
497 | &(ecryptfs_inode_to_private(ecryptfs_inode)->crypt_stat); | |
13a791b4 | 498 | BUG_ON(!(crypt_stat->flags & ECRYPTFS_ENCRYPTED)); |
7fcba054 ES |
499 | enc_extent_page = alloc_page(GFP_USER); |
500 | if (!enc_extent_page) { | |
0216f7f7 MH |
501 | rc = -ENOMEM; |
502 | ecryptfs_printk(KERN_ERR, "Error allocating memory for " | |
503 | "encrypted extent\n"); | |
504 | goto out; | |
505 | } | |
7fcba054 | 506 | enc_extent_virt = kmap(enc_extent_page); |
0216f7f7 MH |
507 | for (extent_offset = 0; |
508 | extent_offset < (PAGE_CACHE_SIZE / crypt_stat->extent_size); | |
509 | extent_offset++) { | |
510 | loff_t offset; | |
511 | ||
512 | rc = ecryptfs_encrypt_extent(enc_extent_page, crypt_stat, page, | |
513 | extent_offset); | |
237fead6 | 514 | if (rc) { |
0216f7f7 | 515 | printk(KERN_ERR "%s: Error encrypting extent; " |
18d1dbf1 | 516 | "rc = [%d]\n", __func__, rc); |
237fead6 MH |
517 | goto out; |
518 | } | |
0216f7f7 | 519 | ecryptfs_lower_offset_for_extent( |
d6a13c17 MH |
520 | &offset, ((((loff_t)page->index) |
521 | * (PAGE_CACHE_SIZE | |
522 | / crypt_stat->extent_size)) | |
0216f7f7 | 523 | + extent_offset), crypt_stat); |
12003e5b TH |
524 | rc = ecryptfs_write_lower(ecryptfs_inode, (enc_extent_virt + |
525 | extent_offset * crypt_stat->extent_size), | |
526 | offset, crypt_stat->extent_size); | |
96a7b9c2 | 527 | if (rc < 0) { |
0216f7f7 MH |
528 | ecryptfs_printk(KERN_ERR, "Error attempting " |
529 | "to write lower page; rc = [%d]" | |
530 | "\n", rc); | |
531 | goto out; | |
237fead6 | 532 | } |
237fead6 | 533 | } |
96a7b9c2 | 534 | rc = 0; |
0216f7f7 | 535 | out: |
7fcba054 ES |
536 | if (enc_extent_page) { |
537 | kunmap(enc_extent_page); | |
538 | __free_page(enc_extent_page); | |
539 | } | |
0216f7f7 MH |
540 | return rc; |
541 | } | |
542 | ||
543 | static int ecryptfs_decrypt_extent(struct page *page, | |
544 | struct ecryptfs_crypt_stat *crypt_stat, | |
545 | struct page *enc_extent_page, | |
546 | unsigned long extent_offset) | |
547 | { | |
d6a13c17 | 548 | loff_t extent_base; |
0216f7f7 MH |
549 | char extent_iv[ECRYPTFS_MAX_IV_BYTES]; |
550 | int rc; | |
551 | ||
d6a13c17 | 552 | extent_base = (((loff_t)page->index) |
0216f7f7 MH |
553 | * (PAGE_CACHE_SIZE / crypt_stat->extent_size)); |
554 | rc = ecryptfs_derive_iv(extent_iv, crypt_stat, | |
555 | (extent_base + extent_offset)); | |
237fead6 | 556 | if (rc) { |
888d57bb JP |
557 | ecryptfs_printk(KERN_ERR, "Error attempting to derive IV for " |
558 | "extent [0x%.16llx]; rc = [%d]\n", | |
559 | (unsigned long long)(extent_base + extent_offset), rc); | |
0216f7f7 MH |
560 | goto out; |
561 | } | |
0216f7f7 | 562 | rc = ecryptfs_decrypt_page_offset(crypt_stat, page, |
12003e5b TH |
563 | extent_offset * crypt_stat->extent_size, |
564 | enc_extent_page, | |
565 | extent_offset * crypt_stat->extent_size, | |
566 | crypt_stat->extent_size, extent_iv); | |
0216f7f7 MH |
567 | if (rc < 0) { |
568 | printk(KERN_ERR "%s: Error attempting to decrypt to page with " | |
569 | "page->index = [%ld], extent_offset = [%ld]; " | |
18d1dbf1 | 570 | "rc = [%d]\n", __func__, page->index, extent_offset, |
0216f7f7 MH |
571 | rc); |
572 | goto out; | |
573 | } | |
574 | rc = 0; | |
237fead6 MH |
575 | out: |
576 | return rc; | |
577 | } | |
578 | ||
579 | /** | |
580 | * ecryptfs_decrypt_page | |
0216f7f7 MH |
581 | * @page: Page mapped from the eCryptfs inode for the file; data read |
582 | * and decrypted from the lower file will be written into this | |
583 | * page | |
237fead6 MH |
584 | * |
585 | * Decrypt an eCryptfs page. This is done on a per-extent basis. Note | |
586 | * that eCryptfs pages may straddle the lower pages -- for instance, | |
587 | * if the file was created on a machine with an 8K page size | |
588 | * (resulting in an 8K header), and then the file is copied onto a | |
589 | * host with a 32K page size, then when reading page 0 of the eCryptfs | |
590 | * file, 24K of page 0 of the lower file will be read and decrypted, | |
591 | * and then 8K of page 1 of the lower file will be read and decrypted. | |
592 | * | |
593 | * Returns zero on success; negative on error | |
594 | */ | |
0216f7f7 | 595 | int ecryptfs_decrypt_page(struct page *page) |
237fead6 | 596 | { |
0216f7f7 | 597 | struct inode *ecryptfs_inode; |
237fead6 | 598 | struct ecryptfs_crypt_stat *crypt_stat; |
7fcba054 ES |
599 | char *enc_extent_virt; |
600 | struct page *enc_extent_page = NULL; | |
0216f7f7 | 601 | unsigned long extent_offset; |
237fead6 | 602 | int rc = 0; |
237fead6 | 603 | |
0216f7f7 MH |
604 | ecryptfs_inode = page->mapping->host; |
605 | crypt_stat = | |
606 | &(ecryptfs_inode_to_private(ecryptfs_inode)->crypt_stat); | |
13a791b4 | 607 | BUG_ON(!(crypt_stat->flags & ECRYPTFS_ENCRYPTED)); |
7fcba054 ES |
608 | enc_extent_page = alloc_page(GFP_USER); |
609 | if (!enc_extent_page) { | |
237fead6 | 610 | rc = -ENOMEM; |
0216f7f7 MH |
611 | ecryptfs_printk(KERN_ERR, "Error allocating memory for " |
612 | "encrypted extent\n"); | |
16a72c45 | 613 | goto out; |
237fead6 | 614 | } |
7fcba054 | 615 | enc_extent_virt = kmap(enc_extent_page); |
0216f7f7 MH |
616 | for (extent_offset = 0; |
617 | extent_offset < (PAGE_CACHE_SIZE / crypt_stat->extent_size); | |
618 | extent_offset++) { | |
619 | loff_t offset; | |
620 | ||
621 | ecryptfs_lower_offset_for_extent( | |
622 | &offset, ((page->index * (PAGE_CACHE_SIZE | |
623 | / crypt_stat->extent_size)) | |
624 | + extent_offset), crypt_stat); | |
12003e5b TH |
625 | rc = ecryptfs_read_lower((enc_extent_virt + |
626 | extent_offset * crypt_stat->extent_size), | |
627 | offset, crypt_stat->extent_size, | |
628 | ecryptfs_inode); | |
96a7b9c2 | 629 | if (rc < 0) { |
0216f7f7 MH |
630 | ecryptfs_printk(KERN_ERR, "Error attempting " |
631 | "to read lower page; rc = [%d]" | |
632 | "\n", rc); | |
16a72c45 | 633 | goto out; |
237fead6 | 634 | } |
0216f7f7 MH |
635 | rc = ecryptfs_decrypt_extent(page, crypt_stat, enc_extent_page, |
636 | extent_offset); | |
637 | if (rc) { | |
638 | printk(KERN_ERR "%s: Error encrypting extent; " | |
18d1dbf1 | 639 | "rc = [%d]\n", __func__, rc); |
16a72c45 | 640 | goto out; |
237fead6 | 641 | } |
237fead6 MH |
642 | } |
643 | out: | |
7fcba054 ES |
644 | if (enc_extent_page) { |
645 | kunmap(enc_extent_page); | |
646 | __free_page(enc_extent_page); | |
647 | } | |
237fead6 MH |
648 | return rc; |
649 | } | |
650 | ||
651 | /** | |
652 | * decrypt_scatterlist | |
22e78faf MH |
653 | * @crypt_stat: Cryptographic context |
654 | * @dest_sg: The destination scatterlist to decrypt into | |
655 | * @src_sg: The source scatterlist to decrypt from | |
656 | * @size: The number of bytes to decrypt | |
657 | * @iv: The initialization vector to use for the decryption | |
237fead6 MH |
658 | * |
659 | * Returns the number of bytes decrypted; negative value on error | |
660 | */ | |
661 | static int decrypt_scatterlist(struct ecryptfs_crypt_stat *crypt_stat, | |
662 | struct scatterlist *dest_sg, | |
663 | struct scatterlist *src_sg, int size, | |
664 | unsigned char *iv) | |
665 | { | |
4dfea4f0 TH |
666 | struct ablkcipher_request *req = NULL; |
667 | struct extent_crypt_result ecr; | |
237fead6 MH |
668 | int rc = 0; |
669 | ||
4dfea4f0 TH |
670 | BUG_ON(!crypt_stat || !crypt_stat->tfm |
671 | || !(crypt_stat->flags & ECRYPTFS_STRUCT_INITIALIZED)); | |
672 | if (unlikely(ecryptfs_verbosity > 0)) { | |
673 | ecryptfs_printk(KERN_DEBUG, "Key size [%zd]; key:\n", | |
674 | crypt_stat->key_size); | |
675 | ecryptfs_dump_hex(crypt_stat->key, | |
676 | crypt_stat->key_size); | |
677 | } | |
678 | ||
679 | init_completion(&ecr.completion); | |
680 | ||
237fead6 | 681 | mutex_lock(&crypt_stat->cs_tfm_mutex); |
4dfea4f0 TH |
682 | req = ablkcipher_request_alloc(crypt_stat->tfm, GFP_NOFS); |
683 | if (!req) { | |
237fead6 | 684 | mutex_unlock(&crypt_stat->cs_tfm_mutex); |
4dfea4f0 | 685 | rc = -ENOMEM; |
237fead6 MH |
686 | goto out; |
687 | } | |
4dfea4f0 TH |
688 | |
689 | ablkcipher_request_set_callback(req, | |
690 | CRYPTO_TFM_REQ_MAY_BACKLOG | CRYPTO_TFM_REQ_MAY_SLEEP, | |
691 | extent_crypt_complete, &ecr); | |
692 | /* Consider doing this once, when the file is opened */ | |
693 | if (!(crypt_stat->flags & ECRYPTFS_KEY_SET)) { | |
694 | rc = crypto_ablkcipher_setkey(crypt_stat->tfm, crypt_stat->key, | |
695 | crypt_stat->key_size); | |
696 | if (rc) { | |
697 | ecryptfs_printk(KERN_ERR, | |
698 | "Error setting key; rc = [%d]\n", | |
699 | rc); | |
700 | mutex_unlock(&crypt_stat->cs_tfm_mutex); | |
701 | rc = -EINVAL; | |
702 | goto out; | |
703 | } | |
704 | crypt_stat->flags |= ECRYPTFS_KEY_SET; | |
705 | } | |
237fead6 | 706 | mutex_unlock(&crypt_stat->cs_tfm_mutex); |
4dfea4f0 TH |
707 | ecryptfs_printk(KERN_DEBUG, "Decrypting [%d] bytes.\n", size); |
708 | ablkcipher_request_set_crypt(req, src_sg, dest_sg, size, iv); | |
709 | rc = crypto_ablkcipher_decrypt(req); | |
710 | if (rc == -EINPROGRESS || rc == -EBUSY) { | |
711 | struct extent_crypt_result *ecr = req->base.data; | |
712 | ||
713 | wait_for_completion(&ecr->completion); | |
714 | rc = ecr->rc; | |
715 | INIT_COMPLETION(ecr->completion); | |
237fead6 | 716 | } |
237fead6 | 717 | out: |
4dfea4f0 | 718 | ablkcipher_request_free(req); |
237fead6 | 719 | return rc; |
4dfea4f0 | 720 | |
237fead6 MH |
721 | } |
722 | ||
723 | /** | |
724 | * ecryptfs_encrypt_page_offset | |
22e78faf MH |
725 | * @crypt_stat: The cryptographic context |
726 | * @dst_page: The page to encrypt into | |
727 | * @dst_offset: The offset in the page to encrypt into | |
728 | * @src_page: The page to encrypt from | |
729 | * @src_offset: The offset in the page to encrypt from | |
730 | * @size: The number of bytes to encrypt | |
731 | * @iv: The initialization vector to use for the encryption | |
237fead6 MH |
732 | * |
733 | * Returns the number of bytes encrypted | |
734 | */ | |
735 | static int | |
736 | ecryptfs_encrypt_page_offset(struct ecryptfs_crypt_stat *crypt_stat, | |
737 | struct page *dst_page, int dst_offset, | |
738 | struct page *src_page, int src_offset, int size, | |
739 | unsigned char *iv) | |
740 | { | |
741 | struct scatterlist src_sg, dst_sg; | |
742 | ||
60c74f81 JA |
743 | sg_init_table(&src_sg, 1); |
744 | sg_init_table(&dst_sg, 1); | |
745 | ||
642f1490 JA |
746 | sg_set_page(&src_sg, src_page, size, src_offset); |
747 | sg_set_page(&dst_sg, dst_page, size, dst_offset); | |
237fead6 MH |
748 | return encrypt_scatterlist(crypt_stat, &dst_sg, &src_sg, size, iv); |
749 | } | |
750 | ||
751 | /** | |
752 | * ecryptfs_decrypt_page_offset | |
22e78faf MH |
753 | * @crypt_stat: The cryptographic context |
754 | * @dst_page: The page to decrypt into | |
755 | * @dst_offset: The offset in the page to decrypt into | |
756 | * @src_page: The page to decrypt from | |
757 | * @src_offset: The offset in the page to decrypt from | |
758 | * @size: The number of bytes to decrypt | |
759 | * @iv: The initialization vector to use for the decryption | |
237fead6 MH |
760 | * |
761 | * Returns the number of bytes decrypted | |
762 | */ | |
763 | static int | |
764 | ecryptfs_decrypt_page_offset(struct ecryptfs_crypt_stat *crypt_stat, | |
765 | struct page *dst_page, int dst_offset, | |
766 | struct page *src_page, int src_offset, int size, | |
767 | unsigned char *iv) | |
768 | { | |
769 | struct scatterlist src_sg, dst_sg; | |
770 | ||
60c74f81 | 771 | sg_init_table(&src_sg, 1); |
642f1490 JA |
772 | sg_set_page(&src_sg, src_page, size, src_offset); |
773 | ||
60c74f81 | 774 | sg_init_table(&dst_sg, 1); |
642f1490 | 775 | sg_set_page(&dst_sg, dst_page, size, dst_offset); |
60c74f81 | 776 | |
237fead6 MH |
777 | return decrypt_scatterlist(crypt_stat, &dst_sg, &src_sg, size, iv); |
778 | } | |
779 | ||
780 | #define ECRYPTFS_MAX_SCATTERLIST_LEN 4 | |
781 | ||
782 | /** | |
783 | * ecryptfs_init_crypt_ctx | |
421f91d2 | 784 | * @crypt_stat: Uninitialized crypt stats structure |
237fead6 MH |
785 | * |
786 | * Initialize the crypto context. | |
787 | * | |
788 | * TODO: Performance: Keep a cache of initialized cipher contexts; | |
789 | * only init if needed | |
790 | */ | |
791 | int ecryptfs_init_crypt_ctx(struct ecryptfs_crypt_stat *crypt_stat) | |
792 | { | |
8bba066f | 793 | char *full_alg_name; |
237fead6 MH |
794 | int rc = -EINVAL; |
795 | ||
796 | if (!crypt_stat->cipher) { | |
797 | ecryptfs_printk(KERN_ERR, "No cipher specified\n"); | |
798 | goto out; | |
799 | } | |
800 | ecryptfs_printk(KERN_DEBUG, | |
801 | "Initializing cipher [%s]; strlen = [%d]; " | |
f24b3887 | 802 | "key_size_bits = [%zd]\n", |
237fead6 MH |
803 | crypt_stat->cipher, (int)strlen(crypt_stat->cipher), |
804 | crypt_stat->key_size << 3); | |
805 | if (crypt_stat->tfm) { | |
806 | rc = 0; | |
807 | goto out; | |
808 | } | |
809 | mutex_lock(&crypt_stat->cs_tfm_mutex); | |
8bba066f MH |
810 | rc = ecryptfs_crypto_api_algify_cipher_name(&full_alg_name, |
811 | crypt_stat->cipher, "cbc"); | |
812 | if (rc) | |
c8161f64 | 813 | goto out_unlock; |
4dfea4f0 | 814 | crypt_stat->tfm = crypto_alloc_ablkcipher(full_alg_name, 0, 0); |
8bba066f | 815 | kfree(full_alg_name); |
de88777e AM |
816 | if (IS_ERR(crypt_stat->tfm)) { |
817 | rc = PTR_ERR(crypt_stat->tfm); | |
b0105eae | 818 | crypt_stat->tfm = NULL; |
237fead6 MH |
819 | ecryptfs_printk(KERN_ERR, "cryptfs: init_crypt_ctx(): " |
820 | "Error initializing cipher [%s]\n", | |
821 | crypt_stat->cipher); | |
c8161f64 | 822 | goto out_unlock; |
237fead6 | 823 | } |
4dfea4f0 | 824 | crypto_ablkcipher_set_flags(crypt_stat->tfm, CRYPTO_TFM_REQ_WEAK_KEY); |
237fead6 | 825 | rc = 0; |
c8161f64 ES |
826 | out_unlock: |
827 | mutex_unlock(&crypt_stat->cs_tfm_mutex); | |
237fead6 MH |
828 | out: |
829 | return rc; | |
830 | } | |
831 | ||
832 | static void set_extent_mask_and_shift(struct ecryptfs_crypt_stat *crypt_stat) | |
833 | { | |
834 | int extent_size_tmp; | |
835 | ||
836 | crypt_stat->extent_mask = 0xFFFFFFFF; | |
837 | crypt_stat->extent_shift = 0; | |
838 | if (crypt_stat->extent_size == 0) | |
839 | return; | |
840 | extent_size_tmp = crypt_stat->extent_size; | |
841 | while ((extent_size_tmp & 0x01) == 0) { | |
842 | extent_size_tmp >>= 1; | |
843 | crypt_stat->extent_mask <<= 1; | |
844 | crypt_stat->extent_shift++; | |
845 | } | |
846 | } | |
847 | ||
848 | void ecryptfs_set_default_sizes(struct ecryptfs_crypt_stat *crypt_stat) | |
849 | { | |
850 | /* Default values; may be overwritten as we are parsing the | |
851 | * packets. */ | |
852 | crypt_stat->extent_size = ECRYPTFS_DEFAULT_EXTENT_SIZE; | |
853 | set_extent_mask_and_shift(crypt_stat); | |
854 | crypt_stat->iv_bytes = ECRYPTFS_DEFAULT_IV_BYTES; | |
dd2a3b7a | 855 | if (crypt_stat->flags & ECRYPTFS_METADATA_IN_XATTR) |
fa3ef1cb | 856 | crypt_stat->metadata_size = ECRYPTFS_MINIMUM_HEADER_EXTENT_SIZE; |
45eaab79 MH |
857 | else { |
858 | if (PAGE_CACHE_SIZE <= ECRYPTFS_MINIMUM_HEADER_EXTENT_SIZE) | |
fa3ef1cb | 859 | crypt_stat->metadata_size = |
cc11beff | 860 | ECRYPTFS_MINIMUM_HEADER_EXTENT_SIZE; |
45eaab79 | 861 | else |
fa3ef1cb | 862 | crypt_stat->metadata_size = PAGE_CACHE_SIZE; |
45eaab79 | 863 | } |
237fead6 MH |
864 | } |
865 | ||
866 | /** | |
867 | * ecryptfs_compute_root_iv | |
868 | * @crypt_stats | |
869 | * | |
870 | * On error, sets the root IV to all 0's. | |
871 | */ | |
872 | int ecryptfs_compute_root_iv(struct ecryptfs_crypt_stat *crypt_stat) | |
873 | { | |
874 | int rc = 0; | |
875 | char dst[MD5_DIGEST_SIZE]; | |
876 | ||
877 | BUG_ON(crypt_stat->iv_bytes > MD5_DIGEST_SIZE); | |
878 | BUG_ON(crypt_stat->iv_bytes <= 0); | |
e2bd99ec | 879 | if (!(crypt_stat->flags & ECRYPTFS_KEY_VALID)) { |
237fead6 MH |
880 | rc = -EINVAL; |
881 | ecryptfs_printk(KERN_WARNING, "Session key not valid; " | |
882 | "cannot generate root IV\n"); | |
883 | goto out; | |
884 | } | |
885 | rc = ecryptfs_calculate_md5(dst, crypt_stat, crypt_stat->key, | |
886 | crypt_stat->key_size); | |
887 | if (rc) { | |
888 | ecryptfs_printk(KERN_WARNING, "Error attempting to compute " | |
889 | "MD5 while generating root IV\n"); | |
890 | goto out; | |
891 | } | |
892 | memcpy(crypt_stat->root_iv, dst, crypt_stat->iv_bytes); | |
893 | out: | |
894 | if (rc) { | |
895 | memset(crypt_stat->root_iv, 0, crypt_stat->iv_bytes); | |
e2bd99ec | 896 | crypt_stat->flags |= ECRYPTFS_SECURITY_WARNING; |
237fead6 MH |
897 | } |
898 | return rc; | |
899 | } | |
900 | ||
901 | static void ecryptfs_generate_new_key(struct ecryptfs_crypt_stat *crypt_stat) | |
902 | { | |
903 | get_random_bytes(crypt_stat->key, crypt_stat->key_size); | |
e2bd99ec | 904 | crypt_stat->flags |= ECRYPTFS_KEY_VALID; |
237fead6 MH |
905 | ecryptfs_compute_root_iv(crypt_stat); |
906 | if (unlikely(ecryptfs_verbosity > 0)) { | |
907 | ecryptfs_printk(KERN_DEBUG, "Generated new session key:\n"); | |
908 | ecryptfs_dump_hex(crypt_stat->key, | |
909 | crypt_stat->key_size); | |
910 | } | |
911 | } | |
912 | ||
17398957 MH |
913 | /** |
914 | * ecryptfs_copy_mount_wide_flags_to_inode_flags | |
22e78faf MH |
915 | * @crypt_stat: The inode's cryptographic context |
916 | * @mount_crypt_stat: The mount point's cryptographic context | |
17398957 MH |
917 | * |
918 | * This function propagates the mount-wide flags to individual inode | |
919 | * flags. | |
920 | */ | |
921 | static void ecryptfs_copy_mount_wide_flags_to_inode_flags( | |
922 | struct ecryptfs_crypt_stat *crypt_stat, | |
923 | struct ecryptfs_mount_crypt_stat *mount_crypt_stat) | |
924 | { | |
925 | if (mount_crypt_stat->flags & ECRYPTFS_XATTR_METADATA_ENABLED) | |
926 | crypt_stat->flags |= ECRYPTFS_METADATA_IN_XATTR; | |
927 | if (mount_crypt_stat->flags & ECRYPTFS_ENCRYPTED_VIEW_ENABLED) | |
928 | crypt_stat->flags |= ECRYPTFS_VIEW_AS_ENCRYPTED; | |
addd65ad MH |
929 | if (mount_crypt_stat->flags & ECRYPTFS_GLOBAL_ENCRYPT_FILENAMES) { |
930 | crypt_stat->flags |= ECRYPTFS_ENCRYPT_FILENAMES; | |
931 | if (mount_crypt_stat->flags | |
932 | & ECRYPTFS_GLOBAL_ENCFN_USE_MOUNT_FNEK) | |
933 | crypt_stat->flags |= ECRYPTFS_ENCFN_USE_MOUNT_FNEK; | |
934 | else if (mount_crypt_stat->flags | |
935 | & ECRYPTFS_GLOBAL_ENCFN_USE_FEK) | |
936 | crypt_stat->flags |= ECRYPTFS_ENCFN_USE_FEK; | |
937 | } | |
17398957 MH |
938 | } |
939 | ||
f4aad16a MH |
940 | static int ecryptfs_copy_mount_wide_sigs_to_inode_sigs( |
941 | struct ecryptfs_crypt_stat *crypt_stat, | |
942 | struct ecryptfs_mount_crypt_stat *mount_crypt_stat) | |
943 | { | |
944 | struct ecryptfs_global_auth_tok *global_auth_tok; | |
945 | int rc = 0; | |
946 | ||
aa06117f | 947 | mutex_lock(&crypt_stat->keysig_list_mutex); |
f4aad16a | 948 | mutex_lock(&mount_crypt_stat->global_auth_tok_list_mutex); |
aa06117f | 949 | |
f4aad16a MH |
950 | list_for_each_entry(global_auth_tok, |
951 | &mount_crypt_stat->global_auth_tok_list, | |
952 | mount_crypt_stat_list) { | |
84814d64 TH |
953 | if (global_auth_tok->flags & ECRYPTFS_AUTH_TOK_FNEK) |
954 | continue; | |
f4aad16a MH |
955 | rc = ecryptfs_add_keysig(crypt_stat, global_auth_tok->sig); |
956 | if (rc) { | |
957 | printk(KERN_ERR "Error adding keysig; rc = [%d]\n", rc); | |
f4aad16a MH |
958 | goto out; |
959 | } | |
960 | } | |
aa06117f | 961 | |
f4aad16a | 962 | out: |
aa06117f RD |
963 | mutex_unlock(&mount_crypt_stat->global_auth_tok_list_mutex); |
964 | mutex_unlock(&crypt_stat->keysig_list_mutex); | |
f4aad16a MH |
965 | return rc; |
966 | } | |
967 | ||
237fead6 MH |
968 | /** |
969 | * ecryptfs_set_default_crypt_stat_vals | |
22e78faf MH |
970 | * @crypt_stat: The inode's cryptographic context |
971 | * @mount_crypt_stat: The mount point's cryptographic context | |
237fead6 MH |
972 | * |
973 | * Default values in the event that policy does not override them. | |
974 | */ | |
975 | static void ecryptfs_set_default_crypt_stat_vals( | |
976 | struct ecryptfs_crypt_stat *crypt_stat, | |
977 | struct ecryptfs_mount_crypt_stat *mount_crypt_stat) | |
978 | { | |
17398957 MH |
979 | ecryptfs_copy_mount_wide_flags_to_inode_flags(crypt_stat, |
980 | mount_crypt_stat); | |
237fead6 MH |
981 | ecryptfs_set_default_sizes(crypt_stat); |
982 | strcpy(crypt_stat->cipher, ECRYPTFS_DEFAULT_CIPHER); | |
983 | crypt_stat->key_size = ECRYPTFS_DEFAULT_KEY_BYTES; | |
e2bd99ec | 984 | crypt_stat->flags &= ~(ECRYPTFS_KEY_VALID); |
237fead6 MH |
985 | crypt_stat->file_version = ECRYPTFS_FILE_VERSION; |
986 | crypt_stat->mount_crypt_stat = mount_crypt_stat; | |
987 | } | |
988 | ||
989 | /** | |
990 | * ecryptfs_new_file_context | |
b59db43a | 991 | * @ecryptfs_inode: The eCryptfs inode |
237fead6 MH |
992 | * |
993 | * If the crypto context for the file has not yet been established, | |
994 | * this is where we do that. Establishing a new crypto context | |
995 | * involves the following decisions: | |
996 | * - What cipher to use? | |
997 | * - What set of authentication tokens to use? | |
998 | * Here we just worry about getting enough information into the | |
999 | * authentication tokens so that we know that they are available. | |
1000 | * We associate the available authentication tokens with the new file | |
1001 | * via the set of signatures in the crypt_stat struct. Later, when | |
1002 | * the headers are actually written out, we may again defer to | |
1003 | * userspace to perform the encryption of the session key; for the | |
1004 | * foreseeable future, this will be the case with public key packets. | |
1005 | * | |
1006 | * Returns zero on success; non-zero otherwise | |
1007 | */ | |
b59db43a | 1008 | int ecryptfs_new_file_context(struct inode *ecryptfs_inode) |
237fead6 | 1009 | { |
237fead6 | 1010 | struct ecryptfs_crypt_stat *crypt_stat = |
b59db43a | 1011 | &ecryptfs_inode_to_private(ecryptfs_inode)->crypt_stat; |
237fead6 MH |
1012 | struct ecryptfs_mount_crypt_stat *mount_crypt_stat = |
1013 | &ecryptfs_superblock_to_private( | |
b59db43a | 1014 | ecryptfs_inode->i_sb)->mount_crypt_stat; |
237fead6 | 1015 | int cipher_name_len; |
f4aad16a | 1016 | int rc = 0; |
237fead6 MH |
1017 | |
1018 | ecryptfs_set_default_crypt_stat_vals(crypt_stat, mount_crypt_stat); | |
af655dc6 | 1019 | crypt_stat->flags |= (ECRYPTFS_ENCRYPTED | ECRYPTFS_KEY_VALID); |
f4aad16a MH |
1020 | ecryptfs_copy_mount_wide_flags_to_inode_flags(crypt_stat, |
1021 | mount_crypt_stat); | |
1022 | rc = ecryptfs_copy_mount_wide_sigs_to_inode_sigs(crypt_stat, | |
1023 | mount_crypt_stat); | |
1024 | if (rc) { | |
1025 | printk(KERN_ERR "Error attempting to copy mount-wide key sigs " | |
1026 | "to the inode key sigs; rc = [%d]\n", rc); | |
1027 | goto out; | |
1028 | } | |
1029 | cipher_name_len = | |
1030 | strlen(mount_crypt_stat->global_default_cipher_name); | |
1031 | memcpy(crypt_stat->cipher, | |
1032 | mount_crypt_stat->global_default_cipher_name, | |
1033 | cipher_name_len); | |
1034 | crypt_stat->cipher[cipher_name_len] = '\0'; | |
1035 | crypt_stat->key_size = | |
1036 | mount_crypt_stat->global_default_cipher_key_size; | |
1037 | ecryptfs_generate_new_key(crypt_stat); | |
237fead6 MH |
1038 | rc = ecryptfs_init_crypt_ctx(crypt_stat); |
1039 | if (rc) | |
1040 | ecryptfs_printk(KERN_ERR, "Error initializing cryptographic " | |
1041 | "context for cipher [%s]: rc = [%d]\n", | |
1042 | crypt_stat->cipher, rc); | |
f4aad16a | 1043 | out: |
237fead6 MH |
1044 | return rc; |
1045 | } | |
1046 | ||
1047 | /** | |
7a86617e | 1048 | * ecryptfs_validate_marker - check for the ecryptfs marker |
237fead6 MH |
1049 | * @data: The data block in which to check |
1050 | * | |
7a86617e | 1051 | * Returns zero if marker found; -EINVAL if not found |
237fead6 | 1052 | */ |
7a86617e | 1053 | static int ecryptfs_validate_marker(char *data) |
237fead6 MH |
1054 | { |
1055 | u32 m_1, m_2; | |
1056 | ||
29335c6a HH |
1057 | m_1 = get_unaligned_be32(data); |
1058 | m_2 = get_unaligned_be32(data + 4); | |
237fead6 | 1059 | if ((m_1 ^ MAGIC_ECRYPTFS_MARKER) == m_2) |
7a86617e | 1060 | return 0; |
237fead6 MH |
1061 | ecryptfs_printk(KERN_DEBUG, "m_1 = [0x%.8x]; m_2 = [0x%.8x]; " |
1062 | "MAGIC_ECRYPTFS_MARKER = [0x%.8x]\n", m_1, m_2, | |
1063 | MAGIC_ECRYPTFS_MARKER); | |
1064 | ecryptfs_printk(KERN_DEBUG, "(m_1 ^ MAGIC_ECRYPTFS_MARKER) = " | |
1065 | "[0x%.8x]\n", (m_1 ^ MAGIC_ECRYPTFS_MARKER)); | |
7a86617e | 1066 | return -EINVAL; |
237fead6 MH |
1067 | } |
1068 | ||
1069 | struct ecryptfs_flag_map_elem { | |
1070 | u32 file_flag; | |
1071 | u32 local_flag; | |
1072 | }; | |
1073 | ||
1074 | /* Add support for additional flags by adding elements here. */ | |
1075 | static struct ecryptfs_flag_map_elem ecryptfs_flag_map[] = { | |
1076 | {0x00000001, ECRYPTFS_ENABLE_HMAC}, | |
dd2a3b7a | 1077 | {0x00000002, ECRYPTFS_ENCRYPTED}, |
addd65ad MH |
1078 | {0x00000004, ECRYPTFS_METADATA_IN_XATTR}, |
1079 | {0x00000008, ECRYPTFS_ENCRYPT_FILENAMES} | |
237fead6 MH |
1080 | }; |
1081 | ||
1082 | /** | |
1083 | * ecryptfs_process_flags | |
22e78faf | 1084 | * @crypt_stat: The cryptographic context |
237fead6 MH |
1085 | * @page_virt: Source data to be parsed |
1086 | * @bytes_read: Updated with the number of bytes read | |
1087 | * | |
1088 | * Returns zero on success; non-zero if the flag set is invalid | |
1089 | */ | |
1090 | static int ecryptfs_process_flags(struct ecryptfs_crypt_stat *crypt_stat, | |
1091 | char *page_virt, int *bytes_read) | |
1092 | { | |
1093 | int rc = 0; | |
1094 | int i; | |
1095 | u32 flags; | |
1096 | ||
29335c6a | 1097 | flags = get_unaligned_be32(page_virt); |
237fead6 MH |
1098 | for (i = 0; i < ((sizeof(ecryptfs_flag_map) |
1099 | / sizeof(struct ecryptfs_flag_map_elem))); i++) | |
1100 | if (flags & ecryptfs_flag_map[i].file_flag) { | |
e2bd99ec | 1101 | crypt_stat->flags |= ecryptfs_flag_map[i].local_flag; |
237fead6 | 1102 | } else |
e2bd99ec | 1103 | crypt_stat->flags &= ~(ecryptfs_flag_map[i].local_flag); |
237fead6 MH |
1104 | /* Version is in top 8 bits of the 32-bit flag vector */ |
1105 | crypt_stat->file_version = ((flags >> 24) & 0xFF); | |
1106 | (*bytes_read) = 4; | |
1107 | return rc; | |
1108 | } | |
1109 | ||
1110 | /** | |
1111 | * write_ecryptfs_marker | |
1112 | * @page_virt: The pointer to in a page to begin writing the marker | |
1113 | * @written: Number of bytes written | |
1114 | * | |
1115 | * Marker = 0x3c81b7f5 | |
1116 | */ | |
1117 | static void write_ecryptfs_marker(char *page_virt, size_t *written) | |
1118 | { | |
1119 | u32 m_1, m_2; | |
1120 | ||
1121 | get_random_bytes(&m_1, (MAGIC_ECRYPTFS_MARKER_SIZE_BYTES / 2)); | |
1122 | m_2 = (m_1 ^ MAGIC_ECRYPTFS_MARKER); | |
29335c6a HH |
1123 | put_unaligned_be32(m_1, page_virt); |
1124 | page_virt += (MAGIC_ECRYPTFS_MARKER_SIZE_BYTES / 2); | |
1125 | put_unaligned_be32(m_2, page_virt); | |
237fead6 MH |
1126 | (*written) = MAGIC_ECRYPTFS_MARKER_SIZE_BYTES; |
1127 | } | |
1128 | ||
f4e60e6b TH |
1129 | void ecryptfs_write_crypt_stat_flags(char *page_virt, |
1130 | struct ecryptfs_crypt_stat *crypt_stat, | |
1131 | size_t *written) | |
237fead6 MH |
1132 | { |
1133 | u32 flags = 0; | |
1134 | int i; | |
1135 | ||
1136 | for (i = 0; i < ((sizeof(ecryptfs_flag_map) | |
1137 | / sizeof(struct ecryptfs_flag_map_elem))); i++) | |
e2bd99ec | 1138 | if (crypt_stat->flags & ecryptfs_flag_map[i].local_flag) |
237fead6 MH |
1139 | flags |= ecryptfs_flag_map[i].file_flag; |
1140 | /* Version is in top 8 bits of the 32-bit flag vector */ | |
1141 | flags |= ((((u8)crypt_stat->file_version) << 24) & 0xFF000000); | |
29335c6a | 1142 | put_unaligned_be32(flags, page_virt); |
237fead6 MH |
1143 | (*written) = 4; |
1144 | } | |
1145 | ||
1146 | struct ecryptfs_cipher_code_str_map_elem { | |
1147 | char cipher_str[16]; | |
19e66a67 | 1148 | u8 cipher_code; |
237fead6 MH |
1149 | }; |
1150 | ||
1151 | /* Add support for additional ciphers by adding elements here. The | |
1152 | * cipher_code is whatever OpenPGP applicatoins use to identify the | |
1153 | * ciphers. List in order of probability. */ | |
1154 | static struct ecryptfs_cipher_code_str_map_elem | |
1155 | ecryptfs_cipher_code_str_map[] = { | |
1156 | {"aes",RFC2440_CIPHER_AES_128 }, | |
1157 | {"blowfish", RFC2440_CIPHER_BLOWFISH}, | |
1158 | {"des3_ede", RFC2440_CIPHER_DES3_EDE}, | |
1159 | {"cast5", RFC2440_CIPHER_CAST_5}, | |
1160 | {"twofish", RFC2440_CIPHER_TWOFISH}, | |
1161 | {"cast6", RFC2440_CIPHER_CAST_6}, | |
1162 | {"aes", RFC2440_CIPHER_AES_192}, | |
1163 | {"aes", RFC2440_CIPHER_AES_256} | |
1164 | }; | |
1165 | ||
1166 | /** | |
1167 | * ecryptfs_code_for_cipher_string | |
9c79f34f MH |
1168 | * @cipher_name: The string alias for the cipher |
1169 | * @key_bytes: Length of key in bytes; used for AES code selection | |
237fead6 MH |
1170 | * |
1171 | * Returns zero on no match, or the cipher code on match | |
1172 | */ | |
9c79f34f | 1173 | u8 ecryptfs_code_for_cipher_string(char *cipher_name, size_t key_bytes) |
237fead6 MH |
1174 | { |
1175 | int i; | |
19e66a67 | 1176 | u8 code = 0; |
237fead6 MH |
1177 | struct ecryptfs_cipher_code_str_map_elem *map = |
1178 | ecryptfs_cipher_code_str_map; | |
1179 | ||
9c79f34f MH |
1180 | if (strcmp(cipher_name, "aes") == 0) { |
1181 | switch (key_bytes) { | |
237fead6 MH |
1182 | case 16: |
1183 | code = RFC2440_CIPHER_AES_128; | |
1184 | break; | |
1185 | case 24: | |
1186 | code = RFC2440_CIPHER_AES_192; | |
1187 | break; | |
1188 | case 32: | |
1189 | code = RFC2440_CIPHER_AES_256; | |
1190 | } | |
1191 | } else { | |
1192 | for (i = 0; i < ARRAY_SIZE(ecryptfs_cipher_code_str_map); i++) | |
9c79f34f | 1193 | if (strcmp(cipher_name, map[i].cipher_str) == 0) { |
237fead6 MH |
1194 | code = map[i].cipher_code; |
1195 | break; | |
1196 | } | |
1197 | } | |
1198 | return code; | |
1199 | } | |
1200 | ||
1201 | /** | |
1202 | * ecryptfs_cipher_code_to_string | |
1203 | * @str: Destination to write out the cipher name | |
1204 | * @cipher_code: The code to convert to cipher name string | |
1205 | * | |
1206 | * Returns zero on success | |
1207 | */ | |
19e66a67 | 1208 | int ecryptfs_cipher_code_to_string(char *str, u8 cipher_code) |
237fead6 MH |
1209 | { |
1210 | int rc = 0; | |
1211 | int i; | |
1212 | ||
1213 | str[0] = '\0'; | |
1214 | for (i = 0; i < ARRAY_SIZE(ecryptfs_cipher_code_str_map); i++) | |
1215 | if (cipher_code == ecryptfs_cipher_code_str_map[i].cipher_code) | |
1216 | strcpy(str, ecryptfs_cipher_code_str_map[i].cipher_str); | |
1217 | if (str[0] == '\0') { | |
1218 | ecryptfs_printk(KERN_WARNING, "Cipher code not recognized: " | |
1219 | "[%d]\n", cipher_code); | |
1220 | rc = -EINVAL; | |
1221 | } | |
1222 | return rc; | |
1223 | } | |
1224 | ||
778aeb42 | 1225 | int ecryptfs_read_and_validate_header_region(struct inode *inode) |
dd2a3b7a | 1226 | { |
778aeb42 TH |
1227 | u8 file_size[ECRYPTFS_SIZE_AND_MARKER_BYTES]; |
1228 | u8 *marker = file_size + ECRYPTFS_FILE_SIZE_BYTES; | |
dd2a3b7a MH |
1229 | int rc; |
1230 | ||
778aeb42 TH |
1231 | rc = ecryptfs_read_lower(file_size, 0, ECRYPTFS_SIZE_AND_MARKER_BYTES, |
1232 | inode); | |
1233 | if (rc < ECRYPTFS_SIZE_AND_MARKER_BYTES) | |
1234 | return rc >= 0 ? -EINVAL : rc; | |
1235 | rc = ecryptfs_validate_marker(marker); | |
1236 | if (!rc) | |
1237 | ecryptfs_i_size_init(file_size, inode); | |
dd2a3b7a MH |
1238 | return rc; |
1239 | } | |
1240 | ||
e77a56dd MH |
1241 | void |
1242 | ecryptfs_write_header_metadata(char *virt, | |
1243 | struct ecryptfs_crypt_stat *crypt_stat, | |
1244 | size_t *written) | |
237fead6 MH |
1245 | { |
1246 | u32 header_extent_size; | |
1247 | u16 num_header_extents_at_front; | |
1248 | ||
45eaab79 | 1249 | header_extent_size = (u32)crypt_stat->extent_size; |
237fead6 | 1250 | num_header_extents_at_front = |
fa3ef1cb | 1251 | (u16)(crypt_stat->metadata_size / crypt_stat->extent_size); |
29335c6a | 1252 | put_unaligned_be32(header_extent_size, virt); |
237fead6 | 1253 | virt += 4; |
29335c6a | 1254 | put_unaligned_be16(num_header_extents_at_front, virt); |
237fead6 MH |
1255 | (*written) = 6; |
1256 | } | |
1257 | ||
30632870 | 1258 | struct kmem_cache *ecryptfs_header_cache; |
237fead6 MH |
1259 | |
1260 | /** | |
1261 | * ecryptfs_write_headers_virt | |
22e78faf | 1262 | * @page_virt: The virtual address to write the headers to |
87b811c3 | 1263 | * @max: The size of memory allocated at page_virt |
22e78faf MH |
1264 | * @size: Set to the number of bytes written by this function |
1265 | * @crypt_stat: The cryptographic context | |
1266 | * @ecryptfs_dentry: The eCryptfs dentry | |
237fead6 MH |
1267 | * |
1268 | * Format version: 1 | |
1269 | * | |
1270 | * Header Extent: | |
1271 | * Octets 0-7: Unencrypted file size (big-endian) | |
1272 | * Octets 8-15: eCryptfs special marker | |
1273 | * Octets 16-19: Flags | |
1274 | * Octet 16: File format version number (between 0 and 255) | |
1275 | * Octets 17-18: Reserved | |
1276 | * Octet 19: Bit 1 (lsb): Reserved | |
1277 | * Bit 2: Encrypted? | |
1278 | * Bits 3-8: Reserved | |
1279 | * Octets 20-23: Header extent size (big-endian) | |
1280 | * Octets 24-25: Number of header extents at front of file | |
1281 | * (big-endian) | |
1282 | * Octet 26: Begin RFC 2440 authentication token packet set | |
1283 | * Data Extent 0: | |
1284 | * Lower data (CBC encrypted) | |
1285 | * Data Extent 1: | |
1286 | * Lower data (CBC encrypted) | |
1287 | * ... | |
1288 | * | |
1289 | * Returns zero on success | |
1290 | */ | |
87b811c3 ES |
1291 | static int ecryptfs_write_headers_virt(char *page_virt, size_t max, |
1292 | size_t *size, | |
dd2a3b7a MH |
1293 | struct ecryptfs_crypt_stat *crypt_stat, |
1294 | struct dentry *ecryptfs_dentry) | |
237fead6 MH |
1295 | { |
1296 | int rc; | |
1297 | size_t written; | |
1298 | size_t offset; | |
1299 | ||
1300 | offset = ECRYPTFS_FILE_SIZE_BYTES; | |
1301 | write_ecryptfs_marker((page_virt + offset), &written); | |
1302 | offset += written; | |
f4e60e6b TH |
1303 | ecryptfs_write_crypt_stat_flags((page_virt + offset), crypt_stat, |
1304 | &written); | |
237fead6 | 1305 | offset += written; |
e77a56dd MH |
1306 | ecryptfs_write_header_metadata((page_virt + offset), crypt_stat, |
1307 | &written); | |
237fead6 MH |
1308 | offset += written; |
1309 | rc = ecryptfs_generate_key_packet_set((page_virt + offset), crypt_stat, | |
1310 | ecryptfs_dentry, &written, | |
87b811c3 | 1311 | max - offset); |
237fead6 MH |
1312 | if (rc) |
1313 | ecryptfs_printk(KERN_WARNING, "Error generating key packet " | |
1314 | "set; rc = [%d]\n", rc); | |
dd2a3b7a MH |
1315 | if (size) { |
1316 | offset += written; | |
1317 | *size = offset; | |
1318 | } | |
1319 | return rc; | |
1320 | } | |
1321 | ||
22e78faf | 1322 | static int |
b59db43a | 1323 | ecryptfs_write_metadata_to_contents(struct inode *ecryptfs_inode, |
8faece5f | 1324 | char *virt, size_t virt_len) |
dd2a3b7a | 1325 | { |
d7cdc5fe | 1326 | int rc; |
dd2a3b7a | 1327 | |
b59db43a | 1328 | rc = ecryptfs_write_lower(ecryptfs_inode, virt, |
8faece5f | 1329 | 0, virt_len); |
96a7b9c2 | 1330 | if (rc < 0) |
d7cdc5fe | 1331 | printk(KERN_ERR "%s: Error attempting to write header " |
96a7b9c2 TH |
1332 | "information to lower file; rc = [%d]\n", __func__, rc); |
1333 | else | |
1334 | rc = 0; | |
70456600 | 1335 | return rc; |
dd2a3b7a MH |
1336 | } |
1337 | ||
22e78faf MH |
1338 | static int |
1339 | ecryptfs_write_metadata_to_xattr(struct dentry *ecryptfs_dentry, | |
22e78faf | 1340 | char *page_virt, size_t size) |
dd2a3b7a MH |
1341 | { |
1342 | int rc; | |
1343 | ||
1344 | rc = ecryptfs_setxattr(ecryptfs_dentry, ECRYPTFS_XATTR_NAME, page_virt, | |
1345 | size, 0); | |
237fead6 MH |
1346 | return rc; |
1347 | } | |
1348 | ||
8faece5f TH |
1349 | static unsigned long ecryptfs_get_zeroed_pages(gfp_t gfp_mask, |
1350 | unsigned int order) | |
1351 | { | |
1352 | struct page *page; | |
1353 | ||
1354 | page = alloc_pages(gfp_mask | __GFP_ZERO, order); | |
1355 | if (page) | |
1356 | return (unsigned long) page_address(page); | |
1357 | return 0; | |
1358 | } | |
1359 | ||
237fead6 | 1360 | /** |
dd2a3b7a | 1361 | * ecryptfs_write_metadata |
b59db43a TH |
1362 | * @ecryptfs_dentry: The eCryptfs dentry, which should be negative |
1363 | * @ecryptfs_inode: The newly created eCryptfs inode | |
237fead6 MH |
1364 | * |
1365 | * Write the file headers out. This will likely involve a userspace | |
1366 | * callout, in which the session key is encrypted with one or more | |
1367 | * public keys and/or the passphrase necessary to do the encryption is | |
1368 | * retrieved via a prompt. Exactly what happens at this point should | |
1369 | * be policy-dependent. | |
1370 | * | |
1371 | * Returns zero on success; non-zero on error | |
1372 | */ | |
b59db43a TH |
1373 | int ecryptfs_write_metadata(struct dentry *ecryptfs_dentry, |
1374 | struct inode *ecryptfs_inode) | |
237fead6 | 1375 | { |
d7cdc5fe | 1376 | struct ecryptfs_crypt_stat *crypt_stat = |
b59db43a | 1377 | &ecryptfs_inode_to_private(ecryptfs_inode)->crypt_stat; |
8faece5f | 1378 | unsigned int order; |
cc11beff | 1379 | char *virt; |
8faece5f | 1380 | size_t virt_len; |
d7cdc5fe | 1381 | size_t size = 0; |
237fead6 MH |
1382 | int rc = 0; |
1383 | ||
e2bd99ec MH |
1384 | if (likely(crypt_stat->flags & ECRYPTFS_ENCRYPTED)) { |
1385 | if (!(crypt_stat->flags & ECRYPTFS_KEY_VALID)) { | |
d7cdc5fe | 1386 | printk(KERN_ERR "Key is invalid; bailing out\n"); |
237fead6 MH |
1387 | rc = -EINVAL; |
1388 | goto out; | |
1389 | } | |
1390 | } else { | |
cc11beff | 1391 | printk(KERN_WARNING "%s: Encrypted flag not set\n", |
18d1dbf1 | 1392 | __func__); |
237fead6 | 1393 | rc = -EINVAL; |
237fead6 MH |
1394 | goto out; |
1395 | } | |
fa3ef1cb | 1396 | virt_len = crypt_stat->metadata_size; |
8faece5f | 1397 | order = get_order(virt_len); |
237fead6 | 1398 | /* Released in this function */ |
8faece5f | 1399 | virt = (char *)ecryptfs_get_zeroed_pages(GFP_KERNEL, order); |
cc11beff | 1400 | if (!virt) { |
18d1dbf1 | 1401 | printk(KERN_ERR "%s: Out of memory\n", __func__); |
237fead6 MH |
1402 | rc = -ENOMEM; |
1403 | goto out; | |
1404 | } | |
bd4f0fe8 | 1405 | /* Zeroed page ensures the in-header unencrypted i_size is set to 0 */ |
8faece5f TH |
1406 | rc = ecryptfs_write_headers_virt(virt, virt_len, &size, crypt_stat, |
1407 | ecryptfs_dentry); | |
237fead6 | 1408 | if (unlikely(rc)) { |
cc11beff | 1409 | printk(KERN_ERR "%s: Error whilst writing headers; rc = [%d]\n", |
18d1dbf1 | 1410 | __func__, rc); |
237fead6 MH |
1411 | goto out_free; |
1412 | } | |
dd2a3b7a | 1413 | if (crypt_stat->flags & ECRYPTFS_METADATA_IN_XATTR) |
8faece5f TH |
1414 | rc = ecryptfs_write_metadata_to_xattr(ecryptfs_dentry, virt, |
1415 | size); | |
dd2a3b7a | 1416 | else |
b59db43a | 1417 | rc = ecryptfs_write_metadata_to_contents(ecryptfs_inode, virt, |
8faece5f | 1418 | virt_len); |
dd2a3b7a | 1419 | if (rc) { |
cc11beff | 1420 | printk(KERN_ERR "%s: Error writing metadata out to lower file; " |
18d1dbf1 | 1421 | "rc = [%d]\n", __func__, rc); |
dd2a3b7a | 1422 | goto out_free; |
237fead6 | 1423 | } |
237fead6 | 1424 | out_free: |
8faece5f | 1425 | free_pages((unsigned long)virt, order); |
237fead6 MH |
1426 | out: |
1427 | return rc; | |
1428 | } | |
1429 | ||
dd2a3b7a MH |
1430 | #define ECRYPTFS_DONT_VALIDATE_HEADER_SIZE 0 |
1431 | #define ECRYPTFS_VALIDATE_HEADER_SIZE 1 | |
237fead6 | 1432 | static int parse_header_metadata(struct ecryptfs_crypt_stat *crypt_stat, |
dd2a3b7a MH |
1433 | char *virt, int *bytes_read, |
1434 | int validate_header_size) | |
237fead6 MH |
1435 | { |
1436 | int rc = 0; | |
1437 | u32 header_extent_size; | |
1438 | u16 num_header_extents_at_front; | |
1439 | ||
29335c6a HH |
1440 | header_extent_size = get_unaligned_be32(virt); |
1441 | virt += sizeof(__be32); | |
1442 | num_header_extents_at_front = get_unaligned_be16(virt); | |
fa3ef1cb TH |
1443 | crypt_stat->metadata_size = (((size_t)num_header_extents_at_front |
1444 | * (size_t)header_extent_size)); | |
29335c6a | 1445 | (*bytes_read) = (sizeof(__be32) + sizeof(__be16)); |
dd2a3b7a | 1446 | if ((validate_header_size == ECRYPTFS_VALIDATE_HEADER_SIZE) |
fa3ef1cb | 1447 | && (crypt_stat->metadata_size |
dd2a3b7a | 1448 | < ECRYPTFS_MINIMUM_HEADER_EXTENT_SIZE)) { |
237fead6 | 1449 | rc = -EINVAL; |
cc11beff | 1450 | printk(KERN_WARNING "Invalid header size: [%zd]\n", |
fa3ef1cb | 1451 | crypt_stat->metadata_size); |
237fead6 MH |
1452 | } |
1453 | return rc; | |
1454 | } | |
1455 | ||
1456 | /** | |
1457 | * set_default_header_data | |
22e78faf | 1458 | * @crypt_stat: The cryptographic context |
237fead6 MH |
1459 | * |
1460 | * For version 0 file format; this function is only for backwards | |
1461 | * compatibility for files created with the prior versions of | |
1462 | * eCryptfs. | |
1463 | */ | |
1464 | static void set_default_header_data(struct ecryptfs_crypt_stat *crypt_stat) | |
1465 | { | |
fa3ef1cb | 1466 | crypt_stat->metadata_size = ECRYPTFS_MINIMUM_HEADER_EXTENT_SIZE; |
237fead6 MH |
1467 | } |
1468 | ||
3aeb86ea TH |
1469 | void ecryptfs_i_size_init(const char *page_virt, struct inode *inode) |
1470 | { | |
1471 | struct ecryptfs_mount_crypt_stat *mount_crypt_stat; | |
1472 | struct ecryptfs_crypt_stat *crypt_stat; | |
1473 | u64 file_size; | |
1474 | ||
1475 | crypt_stat = &ecryptfs_inode_to_private(inode)->crypt_stat; | |
1476 | mount_crypt_stat = | |
1477 | &ecryptfs_superblock_to_private(inode->i_sb)->mount_crypt_stat; | |
1478 | if (mount_crypt_stat->flags & ECRYPTFS_ENCRYPTED_VIEW_ENABLED) { | |
1479 | file_size = i_size_read(ecryptfs_inode_to_lower(inode)); | |
1480 | if (crypt_stat->flags & ECRYPTFS_METADATA_IN_XATTR) | |
1481 | file_size += crypt_stat->metadata_size; | |
1482 | } else | |
1483 | file_size = get_unaligned_be64(page_virt); | |
1484 | i_size_write(inode, (loff_t)file_size); | |
1485 | crypt_stat->flags |= ECRYPTFS_I_SIZE_INITIALIZED; | |
1486 | } | |
1487 | ||
237fead6 MH |
1488 | /** |
1489 | * ecryptfs_read_headers_virt | |
22e78faf MH |
1490 | * @page_virt: The virtual address into which to read the headers |
1491 | * @crypt_stat: The cryptographic context | |
1492 | * @ecryptfs_dentry: The eCryptfs dentry | |
1493 | * @validate_header_size: Whether to validate the header size while reading | |
237fead6 MH |
1494 | * |
1495 | * Read/parse the header data. The header format is detailed in the | |
1496 | * comment block for the ecryptfs_write_headers_virt() function. | |
1497 | * | |
1498 | * Returns zero on success | |
1499 | */ | |
1500 | static int ecryptfs_read_headers_virt(char *page_virt, | |
1501 | struct ecryptfs_crypt_stat *crypt_stat, | |
dd2a3b7a MH |
1502 | struct dentry *ecryptfs_dentry, |
1503 | int validate_header_size) | |
237fead6 MH |
1504 | { |
1505 | int rc = 0; | |
1506 | int offset; | |
1507 | int bytes_read; | |
1508 | ||
1509 | ecryptfs_set_default_sizes(crypt_stat); | |
1510 | crypt_stat->mount_crypt_stat = &ecryptfs_superblock_to_private( | |
1511 | ecryptfs_dentry->d_sb)->mount_crypt_stat; | |
1512 | offset = ECRYPTFS_FILE_SIZE_BYTES; | |
7a86617e TH |
1513 | rc = ecryptfs_validate_marker(page_virt + offset); |
1514 | if (rc) | |
237fead6 | 1515 | goto out; |
3aeb86ea TH |
1516 | if (!(crypt_stat->flags & ECRYPTFS_I_SIZE_INITIALIZED)) |
1517 | ecryptfs_i_size_init(page_virt, ecryptfs_dentry->d_inode); | |
237fead6 MH |
1518 | offset += MAGIC_ECRYPTFS_MARKER_SIZE_BYTES; |
1519 | rc = ecryptfs_process_flags(crypt_stat, (page_virt + offset), | |
1520 | &bytes_read); | |
1521 | if (rc) { | |
1522 | ecryptfs_printk(KERN_WARNING, "Error processing flags\n"); | |
1523 | goto out; | |
1524 | } | |
1525 | if (crypt_stat->file_version > ECRYPTFS_SUPPORTED_FILE_VERSION) { | |
1526 | ecryptfs_printk(KERN_WARNING, "File version is [%d]; only " | |
1527 | "file version [%d] is supported by this " | |
1528 | "version of eCryptfs\n", | |
1529 | crypt_stat->file_version, | |
1530 | ECRYPTFS_SUPPORTED_FILE_VERSION); | |
1531 | rc = -EINVAL; | |
1532 | goto out; | |
1533 | } | |
1534 | offset += bytes_read; | |
1535 | if (crypt_stat->file_version >= 1) { | |
1536 | rc = parse_header_metadata(crypt_stat, (page_virt + offset), | |
dd2a3b7a | 1537 | &bytes_read, validate_header_size); |
237fead6 MH |
1538 | if (rc) { |
1539 | ecryptfs_printk(KERN_WARNING, "Error reading header " | |
1540 | "metadata; rc = [%d]\n", rc); | |
1541 | } | |
1542 | offset += bytes_read; | |
1543 | } else | |
1544 | set_default_header_data(crypt_stat); | |
1545 | rc = ecryptfs_parse_packet_set(crypt_stat, (page_virt + offset), | |
1546 | ecryptfs_dentry); | |
1547 | out: | |
1548 | return rc; | |
1549 | } | |
1550 | ||
1551 | /** | |
dd2a3b7a | 1552 | * ecryptfs_read_xattr_region |
22e78faf | 1553 | * @page_virt: The vitual address into which to read the xattr data |
2ed92554 | 1554 | * @ecryptfs_inode: The eCryptfs inode |
dd2a3b7a MH |
1555 | * |
1556 | * Attempts to read the crypto metadata from the extended attribute | |
1557 | * region of the lower file. | |
22e78faf MH |
1558 | * |
1559 | * Returns zero on success; non-zero on error | |
dd2a3b7a | 1560 | */ |
d7cdc5fe | 1561 | int ecryptfs_read_xattr_region(char *page_virt, struct inode *ecryptfs_inode) |
dd2a3b7a | 1562 | { |
d7cdc5fe MH |
1563 | struct dentry *lower_dentry = |
1564 | ecryptfs_inode_to_private(ecryptfs_inode)->lower_file->f_dentry; | |
dd2a3b7a MH |
1565 | ssize_t size; |
1566 | int rc = 0; | |
1567 | ||
d7cdc5fe MH |
1568 | size = ecryptfs_getxattr_lower(lower_dentry, ECRYPTFS_XATTR_NAME, |
1569 | page_virt, ECRYPTFS_DEFAULT_EXTENT_SIZE); | |
dd2a3b7a | 1570 | if (size < 0) { |
25bd8174 MH |
1571 | if (unlikely(ecryptfs_verbosity > 0)) |
1572 | printk(KERN_INFO "Error attempting to read the [%s] " | |
1573 | "xattr from the lower file; return value = " | |
1574 | "[%zd]\n", ECRYPTFS_XATTR_NAME, size); | |
dd2a3b7a MH |
1575 | rc = -EINVAL; |
1576 | goto out; | |
1577 | } | |
1578 | out: | |
1579 | return rc; | |
1580 | } | |
1581 | ||
778aeb42 | 1582 | int ecryptfs_read_and_validate_xattr_region(struct dentry *dentry, |
3b06b3eb | 1583 | struct inode *inode) |
dd2a3b7a | 1584 | { |
778aeb42 TH |
1585 | u8 file_size[ECRYPTFS_SIZE_AND_MARKER_BYTES]; |
1586 | u8 *marker = file_size + ECRYPTFS_FILE_SIZE_BYTES; | |
dd2a3b7a MH |
1587 | int rc; |
1588 | ||
778aeb42 TH |
1589 | rc = ecryptfs_getxattr_lower(ecryptfs_dentry_to_lower(dentry), |
1590 | ECRYPTFS_XATTR_NAME, file_size, | |
1591 | ECRYPTFS_SIZE_AND_MARKER_BYTES); | |
1592 | if (rc < ECRYPTFS_SIZE_AND_MARKER_BYTES) | |
1593 | return rc >= 0 ? -EINVAL : rc; | |
1594 | rc = ecryptfs_validate_marker(marker); | |
1595 | if (!rc) | |
1596 | ecryptfs_i_size_init(file_size, inode); | |
dd2a3b7a MH |
1597 | return rc; |
1598 | } | |
1599 | ||
1600 | /** | |
1601 | * ecryptfs_read_metadata | |
1602 | * | |
1603 | * Common entry point for reading file metadata. From here, we could | |
1604 | * retrieve the header information from the header region of the file, | |
1605 | * the xattr region of the file, or some other repostory that is | |
1606 | * stored separately from the file itself. The current implementation | |
1607 | * supports retrieving the metadata information from the file contents | |
1608 | * and from the xattr region. | |
237fead6 MH |
1609 | * |
1610 | * Returns zero if valid headers found and parsed; non-zero otherwise | |
1611 | */ | |
d7cdc5fe | 1612 | int ecryptfs_read_metadata(struct dentry *ecryptfs_dentry) |
237fead6 | 1613 | { |
bb450361 TG |
1614 | int rc; |
1615 | char *page_virt; | |
d7cdc5fe | 1616 | struct inode *ecryptfs_inode = ecryptfs_dentry->d_inode; |
237fead6 | 1617 | struct ecryptfs_crypt_stat *crypt_stat = |
d7cdc5fe | 1618 | &ecryptfs_inode_to_private(ecryptfs_inode)->crypt_stat; |
e77a56dd MH |
1619 | struct ecryptfs_mount_crypt_stat *mount_crypt_stat = |
1620 | &ecryptfs_superblock_to_private( | |
1621 | ecryptfs_dentry->d_sb)->mount_crypt_stat; | |
237fead6 | 1622 | |
e77a56dd MH |
1623 | ecryptfs_copy_mount_wide_flags_to_inode_flags(crypt_stat, |
1624 | mount_crypt_stat); | |
237fead6 | 1625 | /* Read the first page from the underlying file */ |
30632870 | 1626 | page_virt = kmem_cache_alloc(ecryptfs_header_cache, GFP_USER); |
237fead6 MH |
1627 | if (!page_virt) { |
1628 | rc = -ENOMEM; | |
d7cdc5fe | 1629 | printk(KERN_ERR "%s: Unable to allocate page_virt\n", |
18d1dbf1 | 1630 | __func__); |
237fead6 MH |
1631 | goto out; |
1632 | } | |
d7cdc5fe MH |
1633 | rc = ecryptfs_read_lower(page_virt, 0, crypt_stat->extent_size, |
1634 | ecryptfs_inode); | |
96a7b9c2 | 1635 | if (rc >= 0) |
d7cdc5fe MH |
1636 | rc = ecryptfs_read_headers_virt(page_virt, crypt_stat, |
1637 | ecryptfs_dentry, | |
1638 | ECRYPTFS_VALIDATE_HEADER_SIZE); | |
237fead6 | 1639 | if (rc) { |
bb450361 | 1640 | /* metadata is not in the file header, so try xattrs */ |
1984c23f | 1641 | memset(page_virt, 0, PAGE_CACHE_SIZE); |
d7cdc5fe | 1642 | rc = ecryptfs_read_xattr_region(page_virt, ecryptfs_inode); |
dd2a3b7a MH |
1643 | if (rc) { |
1644 | printk(KERN_DEBUG "Valid eCryptfs headers not found in " | |
30373dc0 TG |
1645 | "file header region or xattr region, inode %lu\n", |
1646 | ecryptfs_inode->i_ino); | |
dd2a3b7a MH |
1647 | rc = -EINVAL; |
1648 | goto out; | |
1649 | } | |
1650 | rc = ecryptfs_read_headers_virt(page_virt, crypt_stat, | |
1651 | ecryptfs_dentry, | |
1652 | ECRYPTFS_DONT_VALIDATE_HEADER_SIZE); | |
1653 | if (rc) { | |
1654 | printk(KERN_DEBUG "Valid eCryptfs headers not found in " | |
30373dc0 TG |
1655 | "file xattr region either, inode %lu\n", |
1656 | ecryptfs_inode->i_ino); | |
dd2a3b7a MH |
1657 | rc = -EINVAL; |
1658 | } | |
1659 | if (crypt_stat->mount_crypt_stat->flags | |
1660 | & ECRYPTFS_XATTR_METADATA_ENABLED) { | |
1661 | crypt_stat->flags |= ECRYPTFS_METADATA_IN_XATTR; | |
1662 | } else { | |
1663 | printk(KERN_WARNING "Attempt to access file with " | |
1664 | "crypto metadata only in the extended attribute " | |
1665 | "region, but eCryptfs was mounted without " | |
1666 | "xattr support enabled. eCryptfs will not treat " | |
30373dc0 TG |
1667 | "this like an encrypted file, inode %lu\n", |
1668 | ecryptfs_inode->i_ino); | |
dd2a3b7a MH |
1669 | rc = -EINVAL; |
1670 | } | |
237fead6 MH |
1671 | } |
1672 | out: | |
1673 | if (page_virt) { | |
1674 | memset(page_virt, 0, PAGE_CACHE_SIZE); | |
30632870 | 1675 | kmem_cache_free(ecryptfs_header_cache, page_virt); |
237fead6 MH |
1676 | } |
1677 | return rc; | |
1678 | } | |
1679 | ||
51ca58dc MH |
1680 | /** |
1681 | * ecryptfs_encrypt_filename - encrypt filename | |
1682 | * | |
1683 | * CBC-encrypts the filename. We do not want to encrypt the same | |
1684 | * filename with the same key and IV, which may happen with hard | |
1685 | * links, so we prepend random bits to each filename. | |
1686 | * | |
1687 | * Returns zero on success; non-zero otherwise | |
1688 | */ | |
1689 | static int | |
1690 | ecryptfs_encrypt_filename(struct ecryptfs_filename *filename, | |
1691 | struct ecryptfs_crypt_stat *crypt_stat, | |
1692 | struct ecryptfs_mount_crypt_stat *mount_crypt_stat) | |
1693 | { | |
1694 | int rc = 0; | |
1695 | ||
1696 | filename->encrypted_filename = NULL; | |
1697 | filename->encrypted_filename_size = 0; | |
1698 | if ((crypt_stat && (crypt_stat->flags & ECRYPTFS_ENCFN_USE_MOUNT_FNEK)) | |
1699 | || (mount_crypt_stat && (mount_crypt_stat->flags | |
1700 | & ECRYPTFS_GLOBAL_ENCFN_USE_MOUNT_FNEK))) { | |
1701 | size_t packet_size; | |
1702 | size_t remaining_bytes; | |
1703 | ||
1704 | rc = ecryptfs_write_tag_70_packet( | |
1705 | NULL, NULL, | |
1706 | &filename->encrypted_filename_size, | |
1707 | mount_crypt_stat, NULL, | |
1708 | filename->filename_size); | |
1709 | if (rc) { | |
1710 | printk(KERN_ERR "%s: Error attempting to get packet " | |
1711 | "size for tag 72; rc = [%d]\n", __func__, | |
1712 | rc); | |
1713 | filename->encrypted_filename_size = 0; | |
1714 | goto out; | |
1715 | } | |
1716 | filename->encrypted_filename = | |
1717 | kmalloc(filename->encrypted_filename_size, GFP_KERNEL); | |
1718 | if (!filename->encrypted_filename) { | |
1719 | printk(KERN_ERR "%s: Out of memory whilst attempting " | |
df261c52 | 1720 | "to kmalloc [%zd] bytes\n", __func__, |
51ca58dc MH |
1721 | filename->encrypted_filename_size); |
1722 | rc = -ENOMEM; | |
1723 | goto out; | |
1724 | } | |
1725 | remaining_bytes = filename->encrypted_filename_size; | |
1726 | rc = ecryptfs_write_tag_70_packet(filename->encrypted_filename, | |
1727 | &remaining_bytes, | |
1728 | &packet_size, | |
1729 | mount_crypt_stat, | |
1730 | filename->filename, | |
1731 | filename->filename_size); | |
1732 | if (rc) { | |
1733 | printk(KERN_ERR "%s: Error attempting to generate " | |
1734 | "tag 70 packet; rc = [%d]\n", __func__, | |
1735 | rc); | |
1736 | kfree(filename->encrypted_filename); | |
1737 | filename->encrypted_filename = NULL; | |
1738 | filename->encrypted_filename_size = 0; | |
1739 | goto out; | |
1740 | } | |
1741 | filename->encrypted_filename_size = packet_size; | |
1742 | } else { | |
1743 | printk(KERN_ERR "%s: No support for requested filename " | |
1744 | "encryption method in this release\n", __func__); | |
df6ad33b | 1745 | rc = -EOPNOTSUPP; |
51ca58dc MH |
1746 | goto out; |
1747 | } | |
1748 | out: | |
1749 | return rc; | |
1750 | } | |
1751 | ||
1752 | static int ecryptfs_copy_filename(char **copied_name, size_t *copied_name_size, | |
1753 | const char *name, size_t name_size) | |
1754 | { | |
1755 | int rc = 0; | |
1756 | ||
fd9fc842 | 1757 | (*copied_name) = kmalloc((name_size + 1), GFP_KERNEL); |
51ca58dc MH |
1758 | if (!(*copied_name)) { |
1759 | rc = -ENOMEM; | |
1760 | goto out; | |
1761 | } | |
1762 | memcpy((void *)(*copied_name), (void *)name, name_size); | |
1763 | (*copied_name)[(name_size)] = '\0'; /* Only for convenience | |
1764 | * in printing out the | |
1765 | * string in debug | |
1766 | * messages */ | |
fd9fc842 | 1767 | (*copied_name_size) = name_size; |
51ca58dc MH |
1768 | out: |
1769 | return rc; | |
1770 | } | |
1771 | ||
237fead6 | 1772 | /** |
f4aad16a | 1773 | * ecryptfs_process_key_cipher - Perform key cipher initialization. |
237fead6 | 1774 | * @key_tfm: Crypto context for key material, set by this function |
e5d9cbde MH |
1775 | * @cipher_name: Name of the cipher |
1776 | * @key_size: Size of the key in bytes | |
237fead6 MH |
1777 | * |
1778 | * Returns zero on success. Any crypto_tfm structs allocated here | |
1779 | * should be released by other functions, such as on a superblock put | |
1780 | * event, regardless of whether this function succeeds for fails. | |
1781 | */ | |
cd9d67df | 1782 | static int |
f4aad16a MH |
1783 | ecryptfs_process_key_cipher(struct crypto_blkcipher **key_tfm, |
1784 | char *cipher_name, size_t *key_size) | |
237fead6 MH |
1785 | { |
1786 | char dummy_key[ECRYPTFS_MAX_KEY_BYTES]; | |
ece550f5 | 1787 | char *full_alg_name = NULL; |
237fead6 MH |
1788 | int rc; |
1789 | ||
e5d9cbde MH |
1790 | *key_tfm = NULL; |
1791 | if (*key_size > ECRYPTFS_MAX_KEY_BYTES) { | |
237fead6 | 1792 | rc = -EINVAL; |
df261c52 | 1793 | printk(KERN_ERR "Requested key size is [%zd] bytes; maximum " |
e5d9cbde | 1794 | "allowable is [%d]\n", *key_size, ECRYPTFS_MAX_KEY_BYTES); |
237fead6 MH |
1795 | goto out; |
1796 | } | |
8bba066f MH |
1797 | rc = ecryptfs_crypto_api_algify_cipher_name(&full_alg_name, cipher_name, |
1798 | "ecb"); | |
1799 | if (rc) | |
1800 | goto out; | |
1801 | *key_tfm = crypto_alloc_blkcipher(full_alg_name, 0, CRYPTO_ALG_ASYNC); | |
8bba066f MH |
1802 | if (IS_ERR(*key_tfm)) { |
1803 | rc = PTR_ERR(*key_tfm); | |
237fead6 | 1804 | printk(KERN_ERR "Unable to allocate crypto cipher with name " |
38268498 | 1805 | "[%s]; rc = [%d]\n", full_alg_name, rc); |
237fead6 MH |
1806 | goto out; |
1807 | } | |
8bba066f MH |
1808 | crypto_blkcipher_set_flags(*key_tfm, CRYPTO_TFM_REQ_WEAK_KEY); |
1809 | if (*key_size == 0) { | |
1810 | struct blkcipher_alg *alg = crypto_blkcipher_alg(*key_tfm); | |
1811 | ||
1812 | *key_size = alg->max_keysize; | |
1813 | } | |
e5d9cbde | 1814 | get_random_bytes(dummy_key, *key_size); |
8bba066f | 1815 | rc = crypto_blkcipher_setkey(*key_tfm, dummy_key, *key_size); |
237fead6 | 1816 | if (rc) { |
df261c52 | 1817 | printk(KERN_ERR "Error attempting to set key of size [%zd] for " |
38268498 DH |
1818 | "cipher [%s]; rc = [%d]\n", *key_size, full_alg_name, |
1819 | rc); | |
237fead6 MH |
1820 | rc = -EINVAL; |
1821 | goto out; | |
1822 | } | |
1823 | out: | |
ece550f5 | 1824 | kfree(full_alg_name); |
237fead6 MH |
1825 | return rc; |
1826 | } | |
f4aad16a MH |
1827 | |
1828 | struct kmem_cache *ecryptfs_key_tfm_cache; | |
7896b631 | 1829 | static struct list_head key_tfm_list; |
af440f52 | 1830 | struct mutex key_tfm_list_mutex; |
f4aad16a | 1831 | |
7371a382 | 1832 | int __init ecryptfs_init_crypto(void) |
f4aad16a MH |
1833 | { |
1834 | mutex_init(&key_tfm_list_mutex); | |
1835 | INIT_LIST_HEAD(&key_tfm_list); | |
1836 | return 0; | |
1837 | } | |
1838 | ||
af440f52 ES |
1839 | /** |
1840 | * ecryptfs_destroy_crypto - free all cached key_tfms on key_tfm_list | |
1841 | * | |
1842 | * Called only at module unload time | |
1843 | */ | |
fcd12835 | 1844 | int ecryptfs_destroy_crypto(void) |
f4aad16a MH |
1845 | { |
1846 | struct ecryptfs_key_tfm *key_tfm, *key_tfm_tmp; | |
1847 | ||
1848 | mutex_lock(&key_tfm_list_mutex); | |
1849 | list_for_each_entry_safe(key_tfm, key_tfm_tmp, &key_tfm_list, | |
1850 | key_tfm_list) { | |
1851 | list_del(&key_tfm->key_tfm_list); | |
1852 | if (key_tfm->key_tfm) | |
1853 | crypto_free_blkcipher(key_tfm->key_tfm); | |
1854 | kmem_cache_free(ecryptfs_key_tfm_cache, key_tfm); | |
1855 | } | |
1856 | mutex_unlock(&key_tfm_list_mutex); | |
1857 | return 0; | |
1858 | } | |
1859 | ||
1860 | int | |
1861 | ecryptfs_add_new_key_tfm(struct ecryptfs_key_tfm **key_tfm, char *cipher_name, | |
1862 | size_t key_size) | |
1863 | { | |
1864 | struct ecryptfs_key_tfm *tmp_tfm; | |
1865 | int rc = 0; | |
1866 | ||
af440f52 ES |
1867 | BUG_ON(!mutex_is_locked(&key_tfm_list_mutex)); |
1868 | ||
f4aad16a MH |
1869 | tmp_tfm = kmem_cache_alloc(ecryptfs_key_tfm_cache, GFP_KERNEL); |
1870 | if (key_tfm != NULL) | |
1871 | (*key_tfm) = tmp_tfm; | |
1872 | if (!tmp_tfm) { | |
1873 | rc = -ENOMEM; | |
1874 | printk(KERN_ERR "Error attempting to allocate from " | |
1875 | "ecryptfs_key_tfm_cache\n"); | |
1876 | goto out; | |
1877 | } | |
1878 | mutex_init(&tmp_tfm->key_tfm_mutex); | |
1879 | strncpy(tmp_tfm->cipher_name, cipher_name, | |
1880 | ECRYPTFS_MAX_CIPHER_NAME_SIZE); | |
b8862906 | 1881 | tmp_tfm->cipher_name[ECRYPTFS_MAX_CIPHER_NAME_SIZE] = '\0'; |
f4aad16a | 1882 | tmp_tfm->key_size = key_size; |
5dda6992 MH |
1883 | rc = ecryptfs_process_key_cipher(&tmp_tfm->key_tfm, |
1884 | tmp_tfm->cipher_name, | |
1885 | &tmp_tfm->key_size); | |
1886 | if (rc) { | |
f4aad16a MH |
1887 | printk(KERN_ERR "Error attempting to initialize key TFM " |
1888 | "cipher with name = [%s]; rc = [%d]\n", | |
1889 | tmp_tfm->cipher_name, rc); | |
1890 | kmem_cache_free(ecryptfs_key_tfm_cache, tmp_tfm); | |
1891 | if (key_tfm != NULL) | |
1892 | (*key_tfm) = NULL; | |
1893 | goto out; | |
1894 | } | |
f4aad16a | 1895 | list_add(&tmp_tfm->key_tfm_list, &key_tfm_list); |
f4aad16a MH |
1896 | out: |
1897 | return rc; | |
1898 | } | |
1899 | ||
af440f52 ES |
1900 | /** |
1901 | * ecryptfs_tfm_exists - Search for existing tfm for cipher_name. | |
1902 | * @cipher_name: the name of the cipher to search for | |
1903 | * @key_tfm: set to corresponding tfm if found | |
1904 | * | |
1905 | * Searches for cached key_tfm matching @cipher_name | |
1906 | * Must be called with &key_tfm_list_mutex held | |
1907 | * Returns 1 if found, with @key_tfm set | |
1908 | * Returns 0 if not found, with @key_tfm set to NULL | |
1909 | */ | |
1910 | int ecryptfs_tfm_exists(char *cipher_name, struct ecryptfs_key_tfm **key_tfm) | |
1911 | { | |
1912 | struct ecryptfs_key_tfm *tmp_key_tfm; | |
1913 | ||
1914 | BUG_ON(!mutex_is_locked(&key_tfm_list_mutex)); | |
1915 | ||
1916 | list_for_each_entry(tmp_key_tfm, &key_tfm_list, key_tfm_list) { | |
1917 | if (strcmp(tmp_key_tfm->cipher_name, cipher_name) == 0) { | |
1918 | if (key_tfm) | |
1919 | (*key_tfm) = tmp_key_tfm; | |
1920 | return 1; | |
1921 | } | |
1922 | } | |
1923 | if (key_tfm) | |
1924 | (*key_tfm) = NULL; | |
1925 | return 0; | |
1926 | } | |
1927 | ||
1928 | /** | |
1929 | * ecryptfs_get_tfm_and_mutex_for_cipher_name | |
1930 | * | |
1931 | * @tfm: set to cached tfm found, or new tfm created | |
1932 | * @tfm_mutex: set to mutex for cached tfm found, or new tfm created | |
1933 | * @cipher_name: the name of the cipher to search for and/or add | |
1934 | * | |
1935 | * Sets pointers to @tfm & @tfm_mutex matching @cipher_name. | |
1936 | * Searches for cached item first, and creates new if not found. | |
1937 | * Returns 0 on success, non-zero if adding new cipher failed | |
1938 | */ | |
f4aad16a MH |
1939 | int ecryptfs_get_tfm_and_mutex_for_cipher_name(struct crypto_blkcipher **tfm, |
1940 | struct mutex **tfm_mutex, | |
1941 | char *cipher_name) | |
1942 | { | |
1943 | struct ecryptfs_key_tfm *key_tfm; | |
1944 | int rc = 0; | |
1945 | ||
1946 | (*tfm) = NULL; | |
1947 | (*tfm_mutex) = NULL; | |
af440f52 | 1948 | |
f4aad16a | 1949 | mutex_lock(&key_tfm_list_mutex); |
af440f52 ES |
1950 | if (!ecryptfs_tfm_exists(cipher_name, &key_tfm)) { |
1951 | rc = ecryptfs_add_new_key_tfm(&key_tfm, cipher_name, 0); | |
1952 | if (rc) { | |
1953 | printk(KERN_ERR "Error adding new key_tfm to list; " | |
1954 | "rc = [%d]\n", rc); | |
f4aad16a MH |
1955 | goto out; |
1956 | } | |
1957 | } | |
f4aad16a MH |
1958 | (*tfm) = key_tfm->key_tfm; |
1959 | (*tfm_mutex) = &key_tfm->key_tfm_mutex; | |
1960 | out: | |
71fd5179 | 1961 | mutex_unlock(&key_tfm_list_mutex); |
f4aad16a MH |
1962 | return rc; |
1963 | } | |
51ca58dc MH |
1964 | |
1965 | /* 64 characters forming a 6-bit target field */ | |
1966 | static unsigned char *portable_filename_chars = ("-.0123456789ABCD" | |
1967 | "EFGHIJKLMNOPQRST" | |
1968 | "UVWXYZabcdefghij" | |
1969 | "klmnopqrstuvwxyz"); | |
1970 | ||
1971 | /* We could either offset on every reverse map or just pad some 0x00's | |
1972 | * at the front here */ | |
0f751e64 | 1973 | static const unsigned char filename_rev_map[256] = { |
51ca58dc MH |
1974 | 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, /* 7 */ |
1975 | 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, /* 15 */ | |
1976 | 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, /* 23 */ | |
1977 | 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, /* 31 */ | |
1978 | 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, /* 39 */ | |
1979 | 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x01, 0x00, /* 47 */ | |
1980 | 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, /* 55 */ | |
1981 | 0x0A, 0x0B, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, /* 63 */ | |
1982 | 0x00, 0x0C, 0x0D, 0x0E, 0x0F, 0x10, 0x11, 0x12, /* 71 */ | |
1983 | 0x13, 0x14, 0x15, 0x16, 0x17, 0x18, 0x19, 0x1A, /* 79 */ | |
1984 | 0x1B, 0x1C, 0x1D, 0x1E, 0x1F, 0x20, 0x21, 0x22, /* 87 */ | |
1985 | 0x23, 0x24, 0x25, 0x00, 0x00, 0x00, 0x00, 0x00, /* 95 */ | |
1986 | 0x00, 0x26, 0x27, 0x28, 0x29, 0x2A, 0x2B, 0x2C, /* 103 */ | |
1987 | 0x2D, 0x2E, 0x2F, 0x30, 0x31, 0x32, 0x33, 0x34, /* 111 */ | |
1988 | 0x35, 0x36, 0x37, 0x38, 0x39, 0x3A, 0x3B, 0x3C, /* 119 */ | |
0f751e64 | 1989 | 0x3D, 0x3E, 0x3F /* 123 - 255 initialized to 0x00 */ |
51ca58dc MH |
1990 | }; |
1991 | ||
1992 | /** | |
1993 | * ecryptfs_encode_for_filename | |
1994 | * @dst: Destination location for encoded filename | |
1995 | * @dst_size: Size of the encoded filename in bytes | |
1996 | * @src: Source location for the filename to encode | |
1997 | * @src_size: Size of the source in bytes | |
1998 | */ | |
37028758 | 1999 | static void ecryptfs_encode_for_filename(unsigned char *dst, size_t *dst_size, |
51ca58dc MH |
2000 | unsigned char *src, size_t src_size) |
2001 | { | |
2002 | size_t num_blocks; | |
2003 | size_t block_num = 0; | |
2004 | size_t dst_offset = 0; | |
2005 | unsigned char last_block[3]; | |
2006 | ||
2007 | if (src_size == 0) { | |
2008 | (*dst_size) = 0; | |
2009 | goto out; | |
2010 | } | |
2011 | num_blocks = (src_size / 3); | |
2012 | if ((src_size % 3) == 0) { | |
2013 | memcpy(last_block, (&src[src_size - 3]), 3); | |
2014 | } else { | |
2015 | num_blocks++; | |
2016 | last_block[2] = 0x00; | |
2017 | switch (src_size % 3) { | |
2018 | case 1: | |
2019 | last_block[0] = src[src_size - 1]; | |
2020 | last_block[1] = 0x00; | |
2021 | break; | |
2022 | case 2: | |
2023 | last_block[0] = src[src_size - 2]; | |
2024 | last_block[1] = src[src_size - 1]; | |
2025 | } | |
2026 | } | |
2027 | (*dst_size) = (num_blocks * 4); | |
2028 | if (!dst) | |
2029 | goto out; | |
2030 | while (block_num < num_blocks) { | |
2031 | unsigned char *src_block; | |
2032 | unsigned char dst_block[4]; | |
2033 | ||
2034 | if (block_num == (num_blocks - 1)) | |
2035 | src_block = last_block; | |
2036 | else | |
2037 | src_block = &src[block_num * 3]; | |
2038 | dst_block[0] = ((src_block[0] >> 2) & 0x3F); | |
2039 | dst_block[1] = (((src_block[0] << 4) & 0x30) | |
2040 | | ((src_block[1] >> 4) & 0x0F)); | |
2041 | dst_block[2] = (((src_block[1] << 2) & 0x3C) | |
2042 | | ((src_block[2] >> 6) & 0x03)); | |
2043 | dst_block[3] = (src_block[2] & 0x3F); | |
2044 | dst[dst_offset++] = portable_filename_chars[dst_block[0]]; | |
2045 | dst[dst_offset++] = portable_filename_chars[dst_block[1]]; | |
2046 | dst[dst_offset++] = portable_filename_chars[dst_block[2]]; | |
2047 | dst[dst_offset++] = portable_filename_chars[dst_block[3]]; | |
2048 | block_num++; | |
2049 | } | |
2050 | out: | |
2051 | return; | |
2052 | } | |
2053 | ||
4a26620d TH |
2054 | static size_t ecryptfs_max_decoded_size(size_t encoded_size) |
2055 | { | |
2056 | /* Not exact; conservatively long. Every block of 4 | |
2057 | * encoded characters decodes into a block of 3 | |
2058 | * decoded characters. This segment of code provides | |
2059 | * the caller with the maximum amount of allocated | |
2060 | * space that @dst will need to point to in a | |
2061 | * subsequent call. */ | |
2062 | return ((encoded_size + 1) * 3) / 4; | |
2063 | } | |
2064 | ||
71c11c37 MH |
2065 | /** |
2066 | * ecryptfs_decode_from_filename | |
2067 | * @dst: If NULL, this function only sets @dst_size and returns. If | |
2068 | * non-NULL, this function decodes the encoded octets in @src | |
2069 | * into the memory that @dst points to. | |
2070 | * @dst_size: Set to the size of the decoded string. | |
2071 | * @src: The encoded set of octets to decode. | |
2072 | * @src_size: The size of the encoded set of octets to decode. | |
2073 | */ | |
2074 | static void | |
2075 | ecryptfs_decode_from_filename(unsigned char *dst, size_t *dst_size, | |
2076 | const unsigned char *src, size_t src_size) | |
51ca58dc MH |
2077 | { |
2078 | u8 current_bit_offset = 0; | |
2079 | size_t src_byte_offset = 0; | |
2080 | size_t dst_byte_offset = 0; | |
51ca58dc MH |
2081 | |
2082 | if (dst == NULL) { | |
4a26620d | 2083 | (*dst_size) = ecryptfs_max_decoded_size(src_size); |
51ca58dc MH |
2084 | goto out; |
2085 | } | |
2086 | while (src_byte_offset < src_size) { | |
2087 | unsigned char src_byte = | |
2088 | filename_rev_map[(int)src[src_byte_offset]]; | |
2089 | ||
2090 | switch (current_bit_offset) { | |
2091 | case 0: | |
2092 | dst[dst_byte_offset] = (src_byte << 2); | |
2093 | current_bit_offset = 6; | |
2094 | break; | |
2095 | case 6: | |
2096 | dst[dst_byte_offset++] |= (src_byte >> 4); | |
2097 | dst[dst_byte_offset] = ((src_byte & 0xF) | |
2098 | << 4); | |
2099 | current_bit_offset = 4; | |
2100 | break; | |
2101 | case 4: | |
2102 | dst[dst_byte_offset++] |= (src_byte >> 2); | |
2103 | dst[dst_byte_offset] = (src_byte << 6); | |
2104 | current_bit_offset = 2; | |
2105 | break; | |
2106 | case 2: | |
2107 | dst[dst_byte_offset++] |= (src_byte); | |
2108 | dst[dst_byte_offset] = 0; | |
2109 | current_bit_offset = 0; | |
2110 | break; | |
2111 | } | |
2112 | src_byte_offset++; | |
2113 | } | |
2114 | (*dst_size) = dst_byte_offset; | |
2115 | out: | |
71c11c37 | 2116 | return; |
51ca58dc MH |
2117 | } |
2118 | ||
2119 | /** | |
2120 | * ecryptfs_encrypt_and_encode_filename - converts a plaintext file name to cipher text | |
2121 | * @crypt_stat: The crypt_stat struct associated with the file anem to encode | |
2122 | * @name: The plaintext name | |
2123 | * @length: The length of the plaintext | |
2124 | * @encoded_name: The encypted name | |
2125 | * | |
2126 | * Encrypts and encodes a filename into something that constitutes a | |
2127 | * valid filename for a filesystem, with printable characters. | |
2128 | * | |
2129 | * We assume that we have a properly initialized crypto context, | |
2130 | * pointed to by crypt_stat->tfm. | |
2131 | * | |
2132 | * Returns zero on success; non-zero on otherwise | |
2133 | */ | |
2134 | int ecryptfs_encrypt_and_encode_filename( | |
2135 | char **encoded_name, | |
2136 | size_t *encoded_name_size, | |
2137 | struct ecryptfs_crypt_stat *crypt_stat, | |
2138 | struct ecryptfs_mount_crypt_stat *mount_crypt_stat, | |
2139 | const char *name, size_t name_size) | |
2140 | { | |
2141 | size_t encoded_name_no_prefix_size; | |
2142 | int rc = 0; | |
2143 | ||
2144 | (*encoded_name) = NULL; | |
2145 | (*encoded_name_size) = 0; | |
2146 | if ((crypt_stat && (crypt_stat->flags & ECRYPTFS_ENCRYPT_FILENAMES)) | |
2147 | || (mount_crypt_stat && (mount_crypt_stat->flags | |
2148 | & ECRYPTFS_GLOBAL_ENCRYPT_FILENAMES))) { | |
2149 | struct ecryptfs_filename *filename; | |
2150 | ||
2151 | filename = kzalloc(sizeof(*filename), GFP_KERNEL); | |
2152 | if (!filename) { | |
2153 | printk(KERN_ERR "%s: Out of memory whilst attempting " | |
a8f12864 | 2154 | "to kzalloc [%zd] bytes\n", __func__, |
51ca58dc MH |
2155 | sizeof(*filename)); |
2156 | rc = -ENOMEM; | |
2157 | goto out; | |
2158 | } | |
2159 | filename->filename = (char *)name; | |
2160 | filename->filename_size = name_size; | |
2161 | rc = ecryptfs_encrypt_filename(filename, crypt_stat, | |
2162 | mount_crypt_stat); | |
2163 | if (rc) { | |
2164 | printk(KERN_ERR "%s: Error attempting to encrypt " | |
2165 | "filename; rc = [%d]\n", __func__, rc); | |
2166 | kfree(filename); | |
2167 | goto out; | |
2168 | } | |
2169 | ecryptfs_encode_for_filename( | |
2170 | NULL, &encoded_name_no_prefix_size, | |
2171 | filename->encrypted_filename, | |
2172 | filename->encrypted_filename_size); | |
2173 | if ((crypt_stat && (crypt_stat->flags | |
2174 | & ECRYPTFS_ENCFN_USE_MOUNT_FNEK)) | |
2175 | || (mount_crypt_stat | |
2176 | && (mount_crypt_stat->flags | |
2177 | & ECRYPTFS_GLOBAL_ENCFN_USE_MOUNT_FNEK))) | |
2178 | (*encoded_name_size) = | |
2179 | (ECRYPTFS_FNEK_ENCRYPTED_FILENAME_PREFIX_SIZE | |
2180 | + encoded_name_no_prefix_size); | |
2181 | else | |
2182 | (*encoded_name_size) = | |
2183 | (ECRYPTFS_FEK_ENCRYPTED_FILENAME_PREFIX_SIZE | |
2184 | + encoded_name_no_prefix_size); | |
2185 | (*encoded_name) = kmalloc((*encoded_name_size) + 1, GFP_KERNEL); | |
2186 | if (!(*encoded_name)) { | |
2187 | printk(KERN_ERR "%s: Out of memory whilst attempting " | |
a8f12864 | 2188 | "to kzalloc [%zd] bytes\n", __func__, |
51ca58dc MH |
2189 | (*encoded_name_size)); |
2190 | rc = -ENOMEM; | |
2191 | kfree(filename->encrypted_filename); | |
2192 | kfree(filename); | |
2193 | goto out; | |
2194 | } | |
2195 | if ((crypt_stat && (crypt_stat->flags | |
2196 | & ECRYPTFS_ENCFN_USE_MOUNT_FNEK)) | |
2197 | || (mount_crypt_stat | |
2198 | && (mount_crypt_stat->flags | |
2199 | & ECRYPTFS_GLOBAL_ENCFN_USE_MOUNT_FNEK))) { | |
2200 | memcpy((*encoded_name), | |
2201 | ECRYPTFS_FNEK_ENCRYPTED_FILENAME_PREFIX, | |
2202 | ECRYPTFS_FNEK_ENCRYPTED_FILENAME_PREFIX_SIZE); | |
2203 | ecryptfs_encode_for_filename( | |
2204 | ((*encoded_name) | |
2205 | + ECRYPTFS_FNEK_ENCRYPTED_FILENAME_PREFIX_SIZE), | |
2206 | &encoded_name_no_prefix_size, | |
2207 | filename->encrypted_filename, | |
2208 | filename->encrypted_filename_size); | |
2209 | (*encoded_name_size) = | |
2210 | (ECRYPTFS_FNEK_ENCRYPTED_FILENAME_PREFIX_SIZE | |
2211 | + encoded_name_no_prefix_size); | |
2212 | (*encoded_name)[(*encoded_name_size)] = '\0'; | |
51ca58dc | 2213 | } else { |
df6ad33b | 2214 | rc = -EOPNOTSUPP; |
51ca58dc MH |
2215 | } |
2216 | if (rc) { | |
2217 | printk(KERN_ERR "%s: Error attempting to encode " | |
2218 | "encrypted filename; rc = [%d]\n", __func__, | |
2219 | rc); | |
2220 | kfree((*encoded_name)); | |
2221 | (*encoded_name) = NULL; | |
2222 | (*encoded_name_size) = 0; | |
2223 | } | |
2224 | kfree(filename->encrypted_filename); | |
2225 | kfree(filename); | |
2226 | } else { | |
2227 | rc = ecryptfs_copy_filename(encoded_name, | |
2228 | encoded_name_size, | |
2229 | name, name_size); | |
2230 | } | |
2231 | out: | |
2232 | return rc; | |
2233 | } | |
2234 | ||
2235 | /** | |
2236 | * ecryptfs_decode_and_decrypt_filename - converts the encoded cipher text name to decoded plaintext | |
2237 | * @plaintext_name: The plaintext name | |
2238 | * @plaintext_name_size: The plaintext name size | |
2239 | * @ecryptfs_dir_dentry: eCryptfs directory dentry | |
2240 | * @name: The filename in cipher text | |
2241 | * @name_size: The cipher text name size | |
2242 | * | |
2243 | * Decrypts and decodes the filename. | |
2244 | * | |
2245 | * Returns zero on error; non-zero otherwise | |
2246 | */ | |
2247 | int ecryptfs_decode_and_decrypt_filename(char **plaintext_name, | |
2248 | size_t *plaintext_name_size, | |
2249 | struct dentry *ecryptfs_dir_dentry, | |
2250 | const char *name, size_t name_size) | |
2251 | { | |
2aac0cf8 TH |
2252 | struct ecryptfs_mount_crypt_stat *mount_crypt_stat = |
2253 | &ecryptfs_superblock_to_private( | |
2254 | ecryptfs_dir_dentry->d_sb)->mount_crypt_stat; | |
51ca58dc MH |
2255 | char *decoded_name; |
2256 | size_t decoded_name_size; | |
2257 | size_t packet_size; | |
2258 | int rc = 0; | |
2259 | ||
2aac0cf8 TH |
2260 | if ((mount_crypt_stat->flags & ECRYPTFS_GLOBAL_ENCRYPT_FILENAMES) |
2261 | && !(mount_crypt_stat->flags & ECRYPTFS_ENCRYPTED_VIEW_ENABLED) | |
2262 | && (name_size > ECRYPTFS_FNEK_ENCRYPTED_FILENAME_PREFIX_SIZE) | |
51ca58dc MH |
2263 | && (strncmp(name, ECRYPTFS_FNEK_ENCRYPTED_FILENAME_PREFIX, |
2264 | ECRYPTFS_FNEK_ENCRYPTED_FILENAME_PREFIX_SIZE) == 0)) { | |
51ca58dc MH |
2265 | const char *orig_name = name; |
2266 | size_t orig_name_size = name_size; | |
2267 | ||
2268 | name += ECRYPTFS_FNEK_ENCRYPTED_FILENAME_PREFIX_SIZE; | |
2269 | name_size -= ECRYPTFS_FNEK_ENCRYPTED_FILENAME_PREFIX_SIZE; | |
71c11c37 MH |
2270 | ecryptfs_decode_from_filename(NULL, &decoded_name_size, |
2271 | name, name_size); | |
51ca58dc MH |
2272 | decoded_name = kmalloc(decoded_name_size, GFP_KERNEL); |
2273 | if (!decoded_name) { | |
2274 | printk(KERN_ERR "%s: Out of memory whilst attempting " | |
df261c52 | 2275 | "to kmalloc [%zd] bytes\n", __func__, |
51ca58dc MH |
2276 | decoded_name_size); |
2277 | rc = -ENOMEM; | |
2278 | goto out; | |
2279 | } | |
71c11c37 MH |
2280 | ecryptfs_decode_from_filename(decoded_name, &decoded_name_size, |
2281 | name, name_size); | |
51ca58dc MH |
2282 | rc = ecryptfs_parse_tag_70_packet(plaintext_name, |
2283 | plaintext_name_size, | |
2284 | &packet_size, | |
2285 | mount_crypt_stat, | |
2286 | decoded_name, | |
2287 | decoded_name_size); | |
2288 | if (rc) { | |
2289 | printk(KERN_INFO "%s: Could not parse tag 70 packet " | |
2290 | "from filename; copying through filename " | |
2291 | "as-is\n", __func__); | |
2292 | rc = ecryptfs_copy_filename(plaintext_name, | |
2293 | plaintext_name_size, | |
2294 | orig_name, orig_name_size); | |
2295 | goto out_free; | |
2296 | } | |
2297 | } else { | |
2298 | rc = ecryptfs_copy_filename(plaintext_name, | |
2299 | plaintext_name_size, | |
2300 | name, name_size); | |
2301 | goto out; | |
2302 | } | |
2303 | out_free: | |
2304 | kfree(decoded_name); | |
2305 | out: | |
2306 | return rc; | |
2307 | } | |
4a26620d TH |
2308 | |
2309 | #define ENC_NAME_MAX_BLOCKLEN_8_OR_16 143 | |
2310 | ||
2311 | int ecryptfs_set_f_namelen(long *namelen, long lower_namelen, | |
2312 | struct ecryptfs_mount_crypt_stat *mount_crypt_stat) | |
2313 | { | |
2314 | struct blkcipher_desc desc; | |
2315 | struct mutex *tfm_mutex; | |
2316 | size_t cipher_blocksize; | |
2317 | int rc; | |
2318 | ||
2319 | if (!(mount_crypt_stat->flags & ECRYPTFS_GLOBAL_ENCRYPT_FILENAMES)) { | |
2320 | (*namelen) = lower_namelen; | |
2321 | return 0; | |
2322 | } | |
2323 | ||
2324 | rc = ecryptfs_get_tfm_and_mutex_for_cipher_name(&desc.tfm, &tfm_mutex, | |
2325 | mount_crypt_stat->global_default_fn_cipher_name); | |
2326 | if (unlikely(rc)) { | |
2327 | (*namelen) = 0; | |
2328 | return rc; | |
2329 | } | |
2330 | ||
2331 | mutex_lock(tfm_mutex); | |
2332 | cipher_blocksize = crypto_blkcipher_blocksize(desc.tfm); | |
2333 | mutex_unlock(tfm_mutex); | |
2334 | ||
2335 | /* Return an exact amount for the common cases */ | |
2336 | if (lower_namelen == NAME_MAX | |
2337 | && (cipher_blocksize == 8 || cipher_blocksize == 16)) { | |
2338 | (*namelen) = ENC_NAME_MAX_BLOCKLEN_8_OR_16; | |
2339 | return 0; | |
2340 | } | |
2341 | ||
2342 | /* Return a safe estimate for the uncommon cases */ | |
2343 | (*namelen) = lower_namelen; | |
2344 | (*namelen) -= ECRYPTFS_FNEK_ENCRYPTED_FILENAME_PREFIX_SIZE; | |
2345 | /* Since this is the max decoded size, subtract 1 "decoded block" len */ | |
2346 | (*namelen) = ecryptfs_max_decoded_size(*namelen) - 3; | |
2347 | (*namelen) -= ECRYPTFS_TAG_70_MAX_METADATA_SIZE; | |
2348 | (*namelen) -= ECRYPTFS_FILENAME_MIN_RANDOM_PREPEND_BYTES; | |
2349 | /* Worst case is that the filename is padded nearly a full block size */ | |
2350 | (*namelen) -= cipher_blocksize - 1; | |
2351 | ||
2352 | if ((*namelen) < 0) | |
2353 | (*namelen) = 0; | |
2354 | ||
2355 | return 0; | |
2356 | } |