blob: 91d65f337d87a854ca6b2ab367fc5a725f3f9ca4 [file] [log] [blame]
Thomas Gleixner1a59d1b82019-05-27 08:55:05 +02001// SPDX-License-Identifier: GPL-2.0-or-later
Michael Halcrow237fead2006-10-04 02:16:22 -07002/**
3 * eCryptfs: Linux filesystem encryption layer
4 *
5 * Copyright (C) 1997-2004 Erez Zadok
6 * Copyright (C) 2001-2004 Stony Brook University
Michael Halcrowdd2a3b72007-02-12 00:53:46 -08007 * Copyright (C) 2004-2007 International Business Machines Corp.
Michael Halcrow237fead2006-10-04 02:16:22 -07008 * Author(s): Michael A. Halcrow <mahalcro@us.ibm.com>
9 * Michael C. Thompson <mcthomps@us.ibm.com>
Michael Halcrow237fead2006-10-04 02:16:22 -070010 */
11
Herbert Xu3095e8e2016-01-25 10:29:33 +080012#include <crypto/hash.h>
13#include <crypto/skcipher.h>
Michael Halcrow237fead2006-10-04 02:16:22 -070014#include <linux/fs.h>
15#include <linux/mount.h>
16#include <linux/pagemap.h>
17#include <linux/random.h>
18#include <linux/compiler.h>
19#include <linux/key.h>
20#include <linux/namei.h>
Michael Halcrow237fead2006-10-04 02:16:22 -070021#include <linux/file.h>
22#include <linux/scatterlist.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090023#include <linux/slab.h>
Harvey Harrison29335c62008-07-23 21:30:06 -070024#include <asm/unaligned.h>
Jérémy Lefaure02f98762017-10-01 15:30:46 -040025#include <linux/kernel.h>
Michael Halcrow237fead2006-10-04 02:16:22 -070026#include "ecryptfs_kernel.h"
27
Tyler Hicks00a69942013-04-05 23:26:22 -070028#define DECRYPT 0
29#define ENCRYPT 1
Michael Halcrow237fead2006-10-04 02:16:22 -070030
31/**
Michael Halcrow237fead2006-10-04 02:16:22 -070032 * ecryptfs_from_hex
33 * @dst: Buffer to take the bytes from src hex; must be at least of
34 * size (src_size / 2)
Wei Yuan5f9f2c22016-02-17 14:50:10 +080035 * @src: Buffer to be converted from a hex string representation to raw value
Michael Halcrow237fead2006-10-04 02:16:22 -070036 * @dst_size: size of dst buffer, or number of hex characters pairs to convert
37 */
38void ecryptfs_from_hex(char *dst, char *src, int dst_size)
39{
40 int x;
41 char tmp[3] = { 0, };
42
43 for (x = 0; x < dst_size; x++) {
44 tmp[0] = src[x * 2];
45 tmp[1] = src[x * 2 + 1];
46 dst[x] = (unsigned char)simple_strtol(tmp, NULL, 16);
47 }
48}
49
Herbert Xu3095e8e2016-01-25 10:29:33 +080050static int ecryptfs_hash_digest(struct crypto_shash *tfm,
51 char *src, int len, char *dst)
52{
53 SHASH_DESC_ON_STACK(desc, tfm);
54 int err;
55
56 desc->tfm = tfm;
Herbert Xu3095e8e2016-01-25 10:29:33 +080057 err = crypto_shash_digest(desc, src, len, dst);
58 shash_desc_zero(desc);
59 return err;
60}
61
Michael Halcrow237fead2006-10-04 02:16:22 -070062/**
63 * ecryptfs_calculate_md5 - calculates the md5 of @src
64 * @dst: Pointer to 16 bytes of allocated memory
65 * @crypt_stat: Pointer to crypt_stat struct for the current inode
66 * @src: Data to be md5'd
67 * @len: Length of @src
68 *
69 * Uses the allocated crypto context that crypt_stat references to
70 * generate the MD5 sum of the contents of src.
71 */
72static int ecryptfs_calculate_md5(char *dst,
73 struct ecryptfs_crypt_stat *crypt_stat,
74 char *src, int len)
75{
Herbert Xu3095e8e2016-01-25 10:29:33 +080076 struct crypto_shash *tfm;
Michael Halcrow565d97242006-10-30 22:07:17 -080077 int rc = 0;
Michael Halcrow237fead2006-10-04 02:16:22 -070078
Herbert Xu3095e8e2016-01-25 10:29:33 +080079 tfm = crypt_stat->hash_tfm;
Herbert Xu3095e8e2016-01-25 10:29:33 +080080 rc = ecryptfs_hash_digest(tfm, src, len, dst);
Michael Halcrow8a29f2b2007-11-05 14:51:04 -080081 if (rc) {
82 printk(KERN_ERR
Herbert Xu3095e8e2016-01-25 10:29:33 +080083 "%s: Error computing crypto hash; rc = [%d]\n",
Harvey Harrison18d1dbf2008-04-29 00:59:48 -070084 __func__, rc);
Michael Halcrow8a29f2b2007-11-05 14:51:04 -080085 goto out;
86 }
Michael Halcrow237fead2006-10-04 02:16:22 -070087out:
88 return rc;
89}
90
Michael Halcrowcd9d67d2007-10-16 01:28:04 -070091static int ecryptfs_crypto_api_algify_cipher_name(char **algified_name,
92 char *cipher_name,
93 char *chaining_modifier)
Michael Halcrow8bba0662006-10-30 22:07:18 -080094{
95 int cipher_name_len = strlen(cipher_name);
96 int chaining_modifier_len = strlen(chaining_modifier);
97 int algified_name_len;
98 int rc;
99
100 algified_name_len = (chaining_modifier_len + cipher_name_len + 3);
101 (*algified_name) = kmalloc(algified_name_len, GFP_KERNEL);
Michael Halcrow7bd473f2006-11-02 22:06:56 -0800102 if (!(*algified_name)) {
Michael Halcrow8bba0662006-10-30 22:07:18 -0800103 rc = -ENOMEM;
104 goto out;
105 }
106 snprintf((*algified_name), algified_name_len, "%s(%s)",
107 chaining_modifier, cipher_name);
108 rc = 0;
109out:
110 return rc;
111}
112
Michael Halcrow237fead2006-10-04 02:16:22 -0700113/**
114 * ecryptfs_derive_iv
115 * @iv: destination for the derived iv vale
116 * @crypt_stat: Pointer to crypt_stat struct for the current inode
Michael Halcrowd6a13c12007-10-16 01:28:12 -0700117 * @offset: Offset of the extent whose IV we are to derive
Michael Halcrow237fead2006-10-04 02:16:22 -0700118 *
119 * Generate the initialization vector from the given root IV and page
120 * offset.
121 *
122 * Returns zero on success; non-zero on error.
123 */
Michael Halcrowa34f60f2009-01-06 14:41:58 -0800124int ecryptfs_derive_iv(char *iv, struct ecryptfs_crypt_stat *crypt_stat,
125 loff_t offset)
Michael Halcrow237fead2006-10-04 02:16:22 -0700126{
127 int rc = 0;
128 char dst[MD5_DIGEST_SIZE];
129 char src[ECRYPTFS_MAX_IV_BYTES + 16];
130
131 if (unlikely(ecryptfs_verbosity > 0)) {
132 ecryptfs_printk(KERN_DEBUG, "root iv:\n");
133 ecryptfs_dump_hex(crypt_stat->root_iv, crypt_stat->iv_bytes);
134 }
135 /* TODO: It is probably secure to just cast the least
136 * significant bits of the root IV into an unsigned long and
137 * add the offset to that rather than go through all this
138 * hashing business. -Halcrow */
139 memcpy(src, crypt_stat->root_iv, crypt_stat->iv_bytes);
140 memset((src + crypt_stat->iv_bytes), 0, 16);
Michael Halcrowd6a13c12007-10-16 01:28:12 -0700141 snprintf((src + crypt_stat->iv_bytes), 16, "%lld", offset);
Michael Halcrow237fead2006-10-04 02:16:22 -0700142 if (unlikely(ecryptfs_verbosity > 0)) {
143 ecryptfs_printk(KERN_DEBUG, "source:\n");
144 ecryptfs_dump_hex(src, (crypt_stat->iv_bytes + 16));
145 }
146 rc = ecryptfs_calculate_md5(dst, crypt_stat, src,
147 (crypt_stat->iv_bytes + 16));
148 if (rc) {
149 ecryptfs_printk(KERN_WARNING, "Error attempting to compute "
150 "MD5 while generating IV for a page\n");
151 goto out;
152 }
153 memcpy(iv, dst, crypt_stat->iv_bytes);
154 if (unlikely(ecryptfs_verbosity > 0)) {
155 ecryptfs_printk(KERN_DEBUG, "derived iv:\n");
156 ecryptfs_dump_hex(iv, crypt_stat->iv_bytes);
157 }
158out:
159 return rc;
160}
161
162/**
163 * ecryptfs_init_crypt_stat
164 * @crypt_stat: Pointer to the crypt_stat struct to initialize.
165 *
166 * Initialize the crypt_stat structure.
167 */
Herbert Xue81f3342016-04-16 15:01:09 +0800168int ecryptfs_init_crypt_stat(struct ecryptfs_crypt_stat *crypt_stat)
Michael Halcrow237fead2006-10-04 02:16:22 -0700169{
Herbert Xue81f3342016-04-16 15:01:09 +0800170 struct crypto_shash *tfm;
171 int rc;
172
173 tfm = crypto_alloc_shash(ECRYPTFS_DEFAULT_HASH, 0, 0);
174 if (IS_ERR(tfm)) {
175 rc = PTR_ERR(tfm);
176 ecryptfs_printk(KERN_ERR, "Error attempting to "
177 "allocate crypto context; rc = [%d]\n",
178 rc);
179 return rc;
180 }
181
Michael Halcrow237fead2006-10-04 02:16:22 -0700182 memset((void *)crypt_stat, 0, sizeof(struct ecryptfs_crypt_stat));
Michael Halcrowf4aad162007-10-16 01:27:53 -0700183 INIT_LIST_HEAD(&crypt_stat->keysig_list);
184 mutex_init(&crypt_stat->keysig_list_mutex);
Michael Halcrow237fead2006-10-04 02:16:22 -0700185 mutex_init(&crypt_stat->cs_mutex);
186 mutex_init(&crypt_stat->cs_tfm_mutex);
Herbert Xue81f3342016-04-16 15:01:09 +0800187 crypt_stat->hash_tfm = tfm;
Michael Halcrowe2bd99e2007-02-12 00:53:49 -0800188 crypt_stat->flags |= ECRYPTFS_STRUCT_INITIALIZED;
Herbert Xue81f3342016-04-16 15:01:09 +0800189
190 return 0;
Michael Halcrow237fead2006-10-04 02:16:22 -0700191}
192
193/**
Michael Halcrowfcd12832007-10-16 01:28:01 -0700194 * ecryptfs_destroy_crypt_stat
Michael Halcrow237fead2006-10-04 02:16:22 -0700195 * @crypt_stat: Pointer to the crypt_stat struct to initialize.
196 *
197 * Releases all memory associated with a crypt_stat struct.
198 */
Michael Halcrowfcd12832007-10-16 01:28:01 -0700199void ecryptfs_destroy_crypt_stat(struct ecryptfs_crypt_stat *crypt_stat)
Michael Halcrow237fead2006-10-04 02:16:22 -0700200{
Michael Halcrowf4aad162007-10-16 01:27:53 -0700201 struct ecryptfs_key_sig *key_sig, *key_sig_tmp;
202
Herbert Xu3095e8e2016-01-25 10:29:33 +0800203 crypto_free_skcipher(crypt_stat->tfm);
204 crypto_free_shash(crypt_stat->hash_tfm);
Michael Halcrowf4aad162007-10-16 01:27:53 -0700205 list_for_each_entry_safe(key_sig, key_sig_tmp,
206 &crypt_stat->keysig_list, crypt_stat_list) {
207 list_del(&key_sig->crypt_stat_list);
208 kmem_cache_free(ecryptfs_key_sig_cache, key_sig);
209 }
Michael Halcrow237fead2006-10-04 02:16:22 -0700210 memset(crypt_stat, 0, sizeof(struct ecryptfs_crypt_stat));
211}
212
Michael Halcrowfcd12832007-10-16 01:28:01 -0700213void ecryptfs_destroy_mount_crypt_stat(
Michael Halcrow237fead2006-10-04 02:16:22 -0700214 struct ecryptfs_mount_crypt_stat *mount_crypt_stat)
215{
Michael Halcrowf4aad162007-10-16 01:27:53 -0700216 struct ecryptfs_global_auth_tok *auth_tok, *auth_tok_tmp;
217
218 if (!(mount_crypt_stat->flags & ECRYPTFS_MOUNT_CRYPT_STAT_INITIALIZED))
219 return;
220 mutex_lock(&mount_crypt_stat->global_auth_tok_list_mutex);
221 list_for_each_entry_safe(auth_tok, auth_tok_tmp,
222 &mount_crypt_stat->global_auth_tok_list,
223 mount_crypt_stat_list) {
224 list_del(&auth_tok->mount_crypt_stat_list);
Markus Elfring0dad87f2015-06-26 18:18:54 +0200225 if (!(auth_tok->flags & ECRYPTFS_AUTH_TOK_INVALID))
Michael Halcrowf4aad162007-10-16 01:27:53 -0700226 key_put(auth_tok->global_auth_tok_key);
227 kmem_cache_free(ecryptfs_global_auth_tok_cache, auth_tok);
228 }
229 mutex_unlock(&mount_crypt_stat->global_auth_tok_list_mutex);
Michael Halcrow237fead2006-10-04 02:16:22 -0700230 memset(mount_crypt_stat, 0, sizeof(struct ecryptfs_mount_crypt_stat));
231}
232
233/**
234 * virt_to_scatterlist
235 * @addr: Virtual address
236 * @size: Size of data; should be an even multiple of the block size
237 * @sg: Pointer to scatterlist array; set to NULL to obtain only
238 * the number of scatterlist structs required in array
239 * @sg_size: Max array size
240 *
241 * Fills in a scatterlist array with page references for a passed
242 * virtual address.
243 *
244 * Returns the number of scatterlist structs in array used
245 */
246int virt_to_scatterlist(const void *addr, int size, struct scatterlist *sg,
247 int sg_size)
248{
249 int i = 0;
250 struct page *pg;
251 int offset;
252 int remainder_of_page;
253
Herbert Xu68e3f5d2007-10-27 00:52:07 -0700254 sg_init_table(sg, sg_size);
255
Michael Halcrow237fead2006-10-04 02:16:22 -0700256 while (size > 0 && i < sg_size) {
257 pg = virt_to_page(addr);
258 offset = offset_in_page(addr);
Dan Carpentera07c48a2013-01-26 10:48:42 +0300259 sg_set_page(&sg[i], pg, 0, offset);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +0300260 remainder_of_page = PAGE_SIZE - offset;
Michael Halcrow237fead2006-10-04 02:16:22 -0700261 if (size >= remainder_of_page) {
Dan Carpentera07c48a2013-01-26 10:48:42 +0300262 sg[i].length = remainder_of_page;
Michael Halcrow237fead2006-10-04 02:16:22 -0700263 addr += remainder_of_page;
264 size -= remainder_of_page;
265 } else {
Dan Carpentera07c48a2013-01-26 10:48:42 +0300266 sg[i].length = size;
Michael Halcrow237fead2006-10-04 02:16:22 -0700267 addr += size;
268 size = 0;
269 }
270 i++;
271 }
272 if (size > 0)
273 return -ENOMEM;
274 return i;
275}
276
Tyler Hicks4dfea4f2013-05-09 16:55:07 -0700277struct extent_crypt_result {
278 struct completion completion;
279 int rc;
280};
281
282static void extent_crypt_complete(struct crypto_async_request *req, int rc)
283{
284 struct extent_crypt_result *ecr = req->data;
285
286 if (rc == -EINPROGRESS)
287 return;
288
289 ecr->rc = rc;
290 complete(&ecr->completion);
291}
292
Michael Halcrow237fead2006-10-04 02:16:22 -0700293/**
Tyler Hicks00a69942013-04-05 23:26:22 -0700294 * crypt_scatterlist
Michael Halcrow237fead2006-10-04 02:16:22 -0700295 * @crypt_stat: Pointer to the crypt_stat struct to initialize.
Tyler Hicks0df5ed62013-05-23 12:08:40 -0700296 * @dst_sg: Destination of the data after performing the crypto operation
Tyler Hicks00a69942013-04-05 23:26:22 -0700297 * @src_sg: Data to be encrypted or decrypted
298 * @size: Length of data
299 * @iv: IV to use
300 * @op: ENCRYPT or DECRYPT to indicate the desired operation
Michael Halcrow237fead2006-10-04 02:16:22 -0700301 *
Tyler Hicks00a69942013-04-05 23:26:22 -0700302 * Returns the number of bytes encrypted or decrypted; negative value on error
Michael Halcrow237fead2006-10-04 02:16:22 -0700303 */
Tyler Hicks00a69942013-04-05 23:26:22 -0700304static int crypt_scatterlist(struct ecryptfs_crypt_stat *crypt_stat,
Tyler Hicks0df5ed62013-05-23 12:08:40 -0700305 struct scatterlist *dst_sg,
Tyler Hicks00a69942013-04-05 23:26:22 -0700306 struct scatterlist *src_sg, int size,
307 unsigned char *iv, int op)
Michael Halcrow237fead2006-10-04 02:16:22 -0700308{
Herbert Xu3095e8e2016-01-25 10:29:33 +0800309 struct skcipher_request *req = NULL;
Tyler Hicks4dfea4f2013-05-09 16:55:07 -0700310 struct extent_crypt_result ecr;
Michael Halcrow237fead2006-10-04 02:16:22 -0700311 int rc = 0;
312
313 BUG_ON(!crypt_stat || !crypt_stat->tfm
Michael Halcrowe2bd99e2007-02-12 00:53:49 -0800314 || !(crypt_stat->flags & ECRYPTFS_STRUCT_INITIALIZED));
Michael Halcrow237fead2006-10-04 02:16:22 -0700315 if (unlikely(ecryptfs_verbosity > 0)) {
Tyler Hicksf24b3882010-11-15 17:36:38 -0600316 ecryptfs_printk(KERN_DEBUG, "Key size [%zd]; key:\n",
Michael Halcrow237fead2006-10-04 02:16:22 -0700317 crypt_stat->key_size);
318 ecryptfs_dump_hex(crypt_stat->key,
319 crypt_stat->key_size);
320 }
Tyler Hicks4dfea4f2013-05-09 16:55:07 -0700321
322 init_completion(&ecr.completion);
323
Michael Halcrow237fead2006-10-04 02:16:22 -0700324 mutex_lock(&crypt_stat->cs_tfm_mutex);
Herbert Xu3095e8e2016-01-25 10:29:33 +0800325 req = skcipher_request_alloc(crypt_stat->tfm, GFP_NOFS);
Tyler Hicks4dfea4f2013-05-09 16:55:07 -0700326 if (!req) {
Michael Halcrow237fead2006-10-04 02:16:22 -0700327 mutex_unlock(&crypt_stat->cs_tfm_mutex);
Tyler Hicks4dfea4f2013-05-09 16:55:07 -0700328 rc = -ENOMEM;
Michael Halcrow237fead2006-10-04 02:16:22 -0700329 goto out;
330 }
Tyler Hicks4dfea4f2013-05-09 16:55:07 -0700331
Herbert Xu3095e8e2016-01-25 10:29:33 +0800332 skcipher_request_set_callback(req,
Tyler Hicks4dfea4f2013-05-09 16:55:07 -0700333 CRYPTO_TFM_REQ_MAY_BACKLOG | CRYPTO_TFM_REQ_MAY_SLEEP,
334 extent_crypt_complete, &ecr);
335 /* Consider doing this once, when the file is opened */
336 if (!(crypt_stat->flags & ECRYPTFS_KEY_SET)) {
Herbert Xu3095e8e2016-01-25 10:29:33 +0800337 rc = crypto_skcipher_setkey(crypt_stat->tfm, crypt_stat->key,
338 crypt_stat->key_size);
Tyler Hicks4dfea4f2013-05-09 16:55:07 -0700339 if (rc) {
340 ecryptfs_printk(KERN_ERR,
341 "Error setting key; rc = [%d]\n",
342 rc);
343 mutex_unlock(&crypt_stat->cs_tfm_mutex);
344 rc = -EINVAL;
345 goto out;
346 }
347 crypt_stat->flags |= ECRYPTFS_KEY_SET;
348 }
Michael Halcrow237fead2006-10-04 02:16:22 -0700349 mutex_unlock(&crypt_stat->cs_tfm_mutex);
Herbert Xu3095e8e2016-01-25 10:29:33 +0800350 skcipher_request_set_crypt(req, src_sg, dst_sg, size, iv);
351 rc = op == ENCRYPT ? crypto_skcipher_encrypt(req) :
352 crypto_skcipher_decrypt(req);
Tyler Hicks4dfea4f2013-05-09 16:55:07 -0700353 if (rc == -EINPROGRESS || rc == -EBUSY) {
354 struct extent_crypt_result *ecr = req->base.data;
355
356 wait_for_completion(&ecr->completion);
357 rc = ecr->rc;
Wolfram Sang16735d02013-11-14 14:32:02 -0800358 reinit_completion(&ecr->completion);
Tyler Hicks4dfea4f2013-05-09 16:55:07 -0700359 }
Michael Halcrow237fead2006-10-04 02:16:22 -0700360out:
Herbert Xu3095e8e2016-01-25 10:29:33 +0800361 skcipher_request_free(req);
Michael Halcrow237fead2006-10-04 02:16:22 -0700362 return rc;
363}
364
Michael Halcrow237fead2006-10-04 02:16:22 -0700365/**
Tyler Hicks24d15262013-04-15 17:28:09 -0700366 * lower_offset_for_page
Michael Halcrow0216f7f2007-10-16 01:28:08 -0700367 *
368 * Convert an eCryptfs page index into a lower byte offset
369 */
Tyler Hicks24d15262013-04-15 17:28:09 -0700370static loff_t lower_offset_for_page(struct ecryptfs_crypt_stat *crypt_stat,
371 struct page *page)
Michael Halcrow0216f7f2007-10-16 01:28:08 -0700372{
Tyler Hicks24d15262013-04-15 17:28:09 -0700373 return ecryptfs_lower_header_size(crypt_stat) +
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +0300374 ((loff_t)page->index << PAGE_SHIFT);
Michael Halcrow0216f7f2007-10-16 01:28:08 -0700375}
376
377/**
Tyler Hicksd78de612013-04-06 00:08:48 -0700378 * crypt_extent
Michael Halcrow0216f7f2007-10-16 01:28:08 -0700379 * @crypt_stat: crypt_stat containing cryptographic context for the
380 * encryption operation
Tyler Hicks0df5ed62013-05-23 12:08:40 -0700381 * @dst_page: The page to write the result into
Tyler Hicksd78de612013-04-06 00:08:48 -0700382 * @src_page: The page to read from
Michael Halcrow0216f7f2007-10-16 01:28:08 -0700383 * @extent_offset: Page extent offset for use in generating IV
Tyler Hicksd78de612013-04-06 00:08:48 -0700384 * @op: ENCRYPT or DECRYPT to indicate the desired operation
Michael Halcrow0216f7f2007-10-16 01:28:08 -0700385 *
Tyler Hicksd78de612013-04-06 00:08:48 -0700386 * Encrypts or decrypts one extent of data.
Michael Halcrow0216f7f2007-10-16 01:28:08 -0700387 *
388 * Return zero on success; non-zero otherwise
389 */
Tyler Hicks0df5ed62013-05-23 12:08:40 -0700390static int crypt_extent(struct ecryptfs_crypt_stat *crypt_stat,
391 struct page *dst_page,
Tyler Hicksd78de612013-04-06 00:08:48 -0700392 struct page *src_page,
393 unsigned long extent_offset, int op)
Michael Halcrow0216f7f2007-10-16 01:28:08 -0700394{
Tyler Hicksd78de612013-04-06 00:08:48 -0700395 pgoff_t page_index = op == ENCRYPT ? src_page->index : dst_page->index;
Michael Halcrowd6a13c12007-10-16 01:28:12 -0700396 loff_t extent_base;
Michael Halcrow0216f7f2007-10-16 01:28:08 -0700397 char extent_iv[ECRYPTFS_MAX_IV_BYTES];
Tyler Hicks406c93d2013-04-15 17:49:31 -0700398 struct scatterlist src_sg, dst_sg;
399 size_t extent_size = crypt_stat->extent_size;
Michael Halcrow0216f7f2007-10-16 01:28:08 -0700400 int rc;
401
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +0300402 extent_base = (((loff_t)page_index) * (PAGE_SIZE / extent_size));
Michael Halcrow0216f7f2007-10-16 01:28:08 -0700403 rc = ecryptfs_derive_iv(extent_iv, crypt_stat,
404 (extent_base + extent_offset));
405 if (rc) {
Joe Perches888d57b2010-11-10 15:46:16 -0800406 ecryptfs_printk(KERN_ERR, "Error attempting to derive IV for "
407 "extent [0x%.16llx]; rc = [%d]\n",
408 (unsigned long long)(extent_base + extent_offset), rc);
Michael Halcrow0216f7f2007-10-16 01:28:08 -0700409 goto out;
410 }
Tyler Hicks406c93d2013-04-15 17:49:31 -0700411
412 sg_init_table(&src_sg, 1);
413 sg_init_table(&dst_sg, 1);
414
415 sg_set_page(&src_sg, src_page, extent_size,
416 extent_offset * extent_size);
417 sg_set_page(&dst_sg, dst_page, extent_size,
418 extent_offset * extent_size);
419
420 rc = crypt_scatterlist(crypt_stat, &dst_sg, &src_sg, extent_size,
421 extent_iv, op);
Michael Halcrow0216f7f2007-10-16 01:28:08 -0700422 if (rc < 0) {
Tyler Hicksd78de612013-04-06 00:08:48 -0700423 printk(KERN_ERR "%s: Error attempting to crypt page with "
424 "page_index = [%ld], extent_offset = [%ld]; "
425 "rc = [%d]\n", __func__, page_index, extent_offset, rc);
Michael Halcrow0216f7f2007-10-16 01:28:08 -0700426 goto out;
427 }
428 rc = 0;
Michael Halcrow0216f7f2007-10-16 01:28:08 -0700429out:
430 return rc;
431}
432
433/**
Michael Halcrow237fead2006-10-04 02:16:22 -0700434 * ecryptfs_encrypt_page
Michael Halcrow0216f7f2007-10-16 01:28:08 -0700435 * @page: Page mapped from the eCryptfs inode for the file; contains
436 * decrypted content that needs to be encrypted (to a temporary
437 * page; not in place) and written out to the lower file
Michael Halcrow237fead2006-10-04 02:16:22 -0700438 *
439 * Encrypt an eCryptfs page. This is done on a per-extent basis. Note
440 * that eCryptfs pages may straddle the lower pages -- for instance,
441 * if the file was created on a machine with an 8K page size
442 * (resulting in an 8K header), and then the file is copied onto a
443 * host with a 32K page size, then when reading page 0 of the eCryptfs
444 * file, 24K of page 0 of the lower file will be read and decrypted,
445 * and then 8K of page 1 of the lower file will be read and decrypted.
446 *
Michael Halcrow237fead2006-10-04 02:16:22 -0700447 * Returns zero on success; negative on error
448 */
Michael Halcrow0216f7f2007-10-16 01:28:08 -0700449int ecryptfs_encrypt_page(struct page *page)
Michael Halcrow237fead2006-10-04 02:16:22 -0700450{
Michael Halcrow0216f7f2007-10-16 01:28:08 -0700451 struct inode *ecryptfs_inode;
Michael Halcrow237fead2006-10-04 02:16:22 -0700452 struct ecryptfs_crypt_stat *crypt_stat;
Eric Sandeen7fcba052008-07-28 15:46:39 -0700453 char *enc_extent_virt;
454 struct page *enc_extent_page = NULL;
Michael Halcrow0216f7f2007-10-16 01:28:08 -0700455 loff_t extent_offset;
Tyler Hicks0f896172013-04-15 16:16:24 -0700456 loff_t lower_offset;
Michael Halcrow237fead2006-10-04 02:16:22 -0700457 int rc = 0;
Michael Halcrow237fead2006-10-04 02:16:22 -0700458
Michael Halcrow0216f7f2007-10-16 01:28:08 -0700459 ecryptfs_inode = page->mapping->host;
460 crypt_stat =
461 &(ecryptfs_inode_to_private(ecryptfs_inode)->crypt_stat);
Tyler Hicks13a791b2009-04-13 15:29:27 -0500462 BUG_ON(!(crypt_stat->flags & ECRYPTFS_ENCRYPTED));
Eric Sandeen7fcba052008-07-28 15:46:39 -0700463 enc_extent_page = alloc_page(GFP_USER);
464 if (!enc_extent_page) {
Michael Halcrow0216f7f2007-10-16 01:28:08 -0700465 rc = -ENOMEM;
466 ecryptfs_printk(KERN_ERR, "Error allocating memory for "
467 "encrypted extent\n");
468 goto out;
469 }
Tyler Hicks0f896172013-04-15 16:16:24 -0700470
Michael Halcrow0216f7f2007-10-16 01:28:08 -0700471 for (extent_offset = 0;
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +0300472 extent_offset < (PAGE_SIZE / crypt_stat->extent_size);
Michael Halcrow0216f7f2007-10-16 01:28:08 -0700473 extent_offset++) {
Tyler Hicks0df5ed62013-05-23 12:08:40 -0700474 rc = crypt_extent(crypt_stat, enc_extent_page, page,
Tyler Hicksd78de612013-04-06 00:08:48 -0700475 extent_offset, ENCRYPT);
Michael Halcrow237fead2006-10-04 02:16:22 -0700476 if (rc) {
Michael Halcrow0216f7f2007-10-16 01:28:08 -0700477 printk(KERN_ERR "%s: Error encrypting extent; "
Harvey Harrison18d1dbf2008-04-29 00:59:48 -0700478 "rc = [%d]\n", __func__, rc);
Michael Halcrow237fead2006-10-04 02:16:22 -0700479 goto out;
480 }
Tyler Hicks0f896172013-04-15 16:16:24 -0700481 }
482
Tyler Hicks24d15262013-04-15 17:28:09 -0700483 lower_offset = lower_offset_for_page(crypt_stat, page);
Tyler Hicks0f896172013-04-15 16:16:24 -0700484 enc_extent_virt = kmap(enc_extent_page);
485 rc = ecryptfs_write_lower(ecryptfs_inode, enc_extent_virt, lower_offset,
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +0300486 PAGE_SIZE);
Tyler Hicks0f896172013-04-15 16:16:24 -0700487 kunmap(enc_extent_page);
488 if (rc < 0) {
489 ecryptfs_printk(KERN_ERR,
490 "Error attempting to write lower page; rc = [%d]\n",
491 rc);
492 goto out;
Michael Halcrow237fead2006-10-04 02:16:22 -0700493 }
Tyler Hicks96a7b9c2009-09-16 19:04:20 -0500494 rc = 0;
Michael Halcrow0216f7f2007-10-16 01:28:08 -0700495out:
Eric Sandeen7fcba052008-07-28 15:46:39 -0700496 if (enc_extent_page) {
Eric Sandeen7fcba052008-07-28 15:46:39 -0700497 __free_page(enc_extent_page);
498 }
Michael Halcrow0216f7f2007-10-16 01:28:08 -0700499 return rc;
500}
501
Michael Halcrow237fead2006-10-04 02:16:22 -0700502/**
503 * ecryptfs_decrypt_page
Michael Halcrow0216f7f2007-10-16 01:28:08 -0700504 * @page: Page mapped from the eCryptfs inode for the file; data read
505 * and decrypted from the lower file will be written into this
506 * page
Michael Halcrow237fead2006-10-04 02:16:22 -0700507 *
508 * Decrypt an eCryptfs page. This is done on a per-extent basis. Note
509 * that eCryptfs pages may straddle the lower pages -- for instance,
510 * if the file was created on a machine with an 8K page size
511 * (resulting in an 8K header), and then the file is copied onto a
512 * host with a 32K page size, then when reading page 0 of the eCryptfs
513 * file, 24K of page 0 of the lower file will be read and decrypted,
514 * and then 8K of page 1 of the lower file will be read and decrypted.
515 *
516 * Returns zero on success; negative on error
517 */
Michael Halcrow0216f7f2007-10-16 01:28:08 -0700518int ecryptfs_decrypt_page(struct page *page)
Michael Halcrow237fead2006-10-04 02:16:22 -0700519{
Michael Halcrow0216f7f2007-10-16 01:28:08 -0700520 struct inode *ecryptfs_inode;
Michael Halcrow237fead2006-10-04 02:16:22 -0700521 struct ecryptfs_crypt_stat *crypt_stat;
Tyler Hicks9c6043f2013-04-06 00:41:48 -0700522 char *page_virt;
Michael Halcrow0216f7f2007-10-16 01:28:08 -0700523 unsigned long extent_offset;
Tyler Hicks0f896172013-04-15 16:16:24 -0700524 loff_t lower_offset;
Michael Halcrow237fead2006-10-04 02:16:22 -0700525 int rc = 0;
Michael Halcrow237fead2006-10-04 02:16:22 -0700526
Michael Halcrow0216f7f2007-10-16 01:28:08 -0700527 ecryptfs_inode = page->mapping->host;
528 crypt_stat =
529 &(ecryptfs_inode_to_private(ecryptfs_inode)->crypt_stat);
Tyler Hicks13a791b2009-04-13 15:29:27 -0500530 BUG_ON(!(crypt_stat->flags & ECRYPTFS_ENCRYPTED));
Tyler Hicks0f896172013-04-15 16:16:24 -0700531
Tyler Hicks24d15262013-04-15 17:28:09 -0700532 lower_offset = lower_offset_for_page(crypt_stat, page);
Tyler Hicks9c6043f2013-04-06 00:41:48 -0700533 page_virt = kmap(page);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +0300534 rc = ecryptfs_read_lower(page_virt, lower_offset, PAGE_SIZE,
Tyler Hicks0f896172013-04-15 16:16:24 -0700535 ecryptfs_inode);
Tyler Hicks9c6043f2013-04-06 00:41:48 -0700536 kunmap(page);
Tyler Hicks0f896172013-04-15 16:16:24 -0700537 if (rc < 0) {
538 ecryptfs_printk(KERN_ERR,
539 "Error attempting to read lower page; rc = [%d]\n",
540 rc);
Michael Halcrow16a72c42007-10-16 01:28:14 -0700541 goto out;
Michael Halcrow237fead2006-10-04 02:16:22 -0700542 }
Tyler Hicks0f896172013-04-15 16:16:24 -0700543
Michael Halcrow0216f7f2007-10-16 01:28:08 -0700544 for (extent_offset = 0;
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +0300545 extent_offset < (PAGE_SIZE / crypt_stat->extent_size);
Michael Halcrow0216f7f2007-10-16 01:28:08 -0700546 extent_offset++) {
Tyler Hicks0df5ed62013-05-23 12:08:40 -0700547 rc = crypt_extent(crypt_stat, page, page,
Tyler Hicksd78de612013-04-06 00:08:48 -0700548 extent_offset, DECRYPT);
Michael Halcrow0216f7f2007-10-16 01:28:08 -0700549 if (rc) {
550 printk(KERN_ERR "%s: Error encrypting extent; "
Harvey Harrison18d1dbf2008-04-29 00:59:48 -0700551 "rc = [%d]\n", __func__, rc);
Michael Halcrow16a72c42007-10-16 01:28:14 -0700552 goto out;
Michael Halcrow237fead2006-10-04 02:16:22 -0700553 }
Michael Halcrow237fead2006-10-04 02:16:22 -0700554 }
555out:
Michael Halcrow237fead2006-10-04 02:16:22 -0700556 return rc;
557}
558
Michael Halcrow237fead2006-10-04 02:16:22 -0700559#define ECRYPTFS_MAX_SCATTERLIST_LEN 4
560
561/**
562 * ecryptfs_init_crypt_ctx
Uwe Kleine-König421f91d2010-06-11 12:17:00 +0200563 * @crypt_stat: Uninitialized crypt stats structure
Michael Halcrow237fead2006-10-04 02:16:22 -0700564 *
565 * Initialize the crypto context.
566 *
567 * TODO: Performance: Keep a cache of initialized cipher contexts;
568 * only init if needed
569 */
570int ecryptfs_init_crypt_ctx(struct ecryptfs_crypt_stat *crypt_stat)
571{
Michael Halcrow8bba0662006-10-30 22:07:18 -0800572 char *full_alg_name;
Michael Halcrow237fead2006-10-04 02:16:22 -0700573 int rc = -EINVAL;
574
Michael Halcrow237fead2006-10-04 02:16:22 -0700575 ecryptfs_printk(KERN_DEBUG,
576 "Initializing cipher [%s]; strlen = [%d]; "
Tyler Hicksf24b3882010-11-15 17:36:38 -0600577 "key_size_bits = [%zd]\n",
Michael Halcrow237fead2006-10-04 02:16:22 -0700578 crypt_stat->cipher, (int)strlen(crypt_stat->cipher),
579 crypt_stat->key_size << 3);
Kees Cookcb69f362013-08-13 15:02:27 -0700580 mutex_lock(&crypt_stat->cs_tfm_mutex);
Michael Halcrow237fead2006-10-04 02:16:22 -0700581 if (crypt_stat->tfm) {
582 rc = 0;
Kees Cookcb69f362013-08-13 15:02:27 -0700583 goto out_unlock;
Michael Halcrow237fead2006-10-04 02:16:22 -0700584 }
Michael Halcrow8bba0662006-10-30 22:07:18 -0800585 rc = ecryptfs_crypto_api_algify_cipher_name(&full_alg_name,
586 crypt_stat->cipher, "cbc");
587 if (rc)
Eric Sandeenc8161f62007-12-22 14:03:26 -0800588 goto out_unlock;
Herbert Xu3095e8e2016-01-25 10:29:33 +0800589 crypt_stat->tfm = crypto_alloc_skcipher(full_alg_name, 0, 0);
Akinobu Mitade887772006-11-28 12:29:49 -0800590 if (IS_ERR(crypt_stat->tfm)) {
591 rc = PTR_ERR(crypt_stat->tfm);
Tyler Hicksb0105ea2009-08-11 00:36:32 -0500592 crypt_stat->tfm = NULL;
Michael Halcrow237fead2006-10-04 02:16:22 -0700593 ecryptfs_printk(KERN_ERR, "cryptfs: init_crypt_ctx(): "
594 "Error initializing cipher [%s]\n",
Kees Cookcb69f362013-08-13 15:02:27 -0700595 full_alg_name);
596 goto out_free;
Michael Halcrow237fead2006-10-04 02:16:22 -0700597 }
Eric Biggers231baec2019-01-18 22:48:00 -0800598 crypto_skcipher_set_flags(crypt_stat->tfm,
599 CRYPTO_TFM_REQ_FORBID_WEAK_KEYS);
Michael Halcrow237fead2006-10-04 02:16:22 -0700600 rc = 0;
Kees Cookcb69f362013-08-13 15:02:27 -0700601out_free:
602 kfree(full_alg_name);
Eric Sandeenc8161f62007-12-22 14:03:26 -0800603out_unlock:
604 mutex_unlock(&crypt_stat->cs_tfm_mutex);
Michael Halcrow237fead2006-10-04 02:16:22 -0700605 return rc;
606}
607
608static void set_extent_mask_and_shift(struct ecryptfs_crypt_stat *crypt_stat)
609{
610 int extent_size_tmp;
611
612 crypt_stat->extent_mask = 0xFFFFFFFF;
613 crypt_stat->extent_shift = 0;
614 if (crypt_stat->extent_size == 0)
615 return;
616 extent_size_tmp = crypt_stat->extent_size;
617 while ((extent_size_tmp & 0x01) == 0) {
618 extent_size_tmp >>= 1;
619 crypt_stat->extent_mask <<= 1;
620 crypt_stat->extent_shift++;
621 }
622}
623
624void ecryptfs_set_default_sizes(struct ecryptfs_crypt_stat *crypt_stat)
625{
626 /* Default values; may be overwritten as we are parsing the
627 * packets. */
628 crypt_stat->extent_size = ECRYPTFS_DEFAULT_EXTENT_SIZE;
629 set_extent_mask_and_shift(crypt_stat);
630 crypt_stat->iv_bytes = ECRYPTFS_DEFAULT_IV_BYTES;
Michael Halcrowdd2a3b72007-02-12 00:53:46 -0800631 if (crypt_stat->flags & ECRYPTFS_METADATA_IN_XATTR)
Tyler Hicksfa3ef1c2010-02-11 05:09:14 -0600632 crypt_stat->metadata_size = ECRYPTFS_MINIMUM_HEADER_EXTENT_SIZE;
Michael Halcrow45eaab72007-10-16 01:28:05 -0700633 else {
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +0300634 if (PAGE_SIZE <= ECRYPTFS_MINIMUM_HEADER_EXTENT_SIZE)
Tyler Hicksfa3ef1c2010-02-11 05:09:14 -0600635 crypt_stat->metadata_size =
Michael Halcrowcc11bef2008-02-06 01:38:32 -0800636 ECRYPTFS_MINIMUM_HEADER_EXTENT_SIZE;
Michael Halcrow45eaab72007-10-16 01:28:05 -0700637 else
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +0300638 crypt_stat->metadata_size = PAGE_SIZE;
Michael Halcrow45eaab72007-10-16 01:28:05 -0700639 }
Michael Halcrow237fead2006-10-04 02:16:22 -0700640}
641
642/**
643 * ecryptfs_compute_root_iv
644 * @crypt_stats
645 *
646 * On error, sets the root IV to all 0's.
647 */
648int ecryptfs_compute_root_iv(struct ecryptfs_crypt_stat *crypt_stat)
649{
650 int rc = 0;
651 char dst[MD5_DIGEST_SIZE];
652
653 BUG_ON(crypt_stat->iv_bytes > MD5_DIGEST_SIZE);
654 BUG_ON(crypt_stat->iv_bytes <= 0);
Michael Halcrowe2bd99e2007-02-12 00:53:49 -0800655 if (!(crypt_stat->flags & ECRYPTFS_KEY_VALID)) {
Michael Halcrow237fead2006-10-04 02:16:22 -0700656 rc = -EINVAL;
657 ecryptfs_printk(KERN_WARNING, "Session key not valid; "
658 "cannot generate root IV\n");
659 goto out;
660 }
661 rc = ecryptfs_calculate_md5(dst, crypt_stat, crypt_stat->key,
662 crypt_stat->key_size);
663 if (rc) {
664 ecryptfs_printk(KERN_WARNING, "Error attempting to compute "
665 "MD5 while generating root IV\n");
666 goto out;
667 }
668 memcpy(crypt_stat->root_iv, dst, crypt_stat->iv_bytes);
669out:
670 if (rc) {
671 memset(crypt_stat->root_iv, 0, crypt_stat->iv_bytes);
Michael Halcrowe2bd99e2007-02-12 00:53:49 -0800672 crypt_stat->flags |= ECRYPTFS_SECURITY_WARNING;
Michael Halcrow237fead2006-10-04 02:16:22 -0700673 }
674 return rc;
675}
676
677static void ecryptfs_generate_new_key(struct ecryptfs_crypt_stat *crypt_stat)
678{
679 get_random_bytes(crypt_stat->key, crypt_stat->key_size);
Michael Halcrowe2bd99e2007-02-12 00:53:49 -0800680 crypt_stat->flags |= ECRYPTFS_KEY_VALID;
Michael Halcrow237fead2006-10-04 02:16:22 -0700681 ecryptfs_compute_root_iv(crypt_stat);
682 if (unlikely(ecryptfs_verbosity > 0)) {
683 ecryptfs_printk(KERN_DEBUG, "Generated new session key:\n");
684 ecryptfs_dump_hex(crypt_stat->key,
685 crypt_stat->key_size);
686 }
687}
688
689/**
Michael Halcrow17398952007-02-12 00:53:45 -0800690 * ecryptfs_copy_mount_wide_flags_to_inode_flags
Michael Halcrow22e78fa2007-10-16 01:28:02 -0700691 * @crypt_stat: The inode's cryptographic context
692 * @mount_crypt_stat: The mount point's cryptographic context
Michael Halcrow17398952007-02-12 00:53:45 -0800693 *
694 * This function propagates the mount-wide flags to individual inode
695 * flags.
696 */
697static void ecryptfs_copy_mount_wide_flags_to_inode_flags(
698 struct ecryptfs_crypt_stat *crypt_stat,
699 struct ecryptfs_mount_crypt_stat *mount_crypt_stat)
700{
701 if (mount_crypt_stat->flags & ECRYPTFS_XATTR_METADATA_ENABLED)
702 crypt_stat->flags |= ECRYPTFS_METADATA_IN_XATTR;
703 if (mount_crypt_stat->flags & ECRYPTFS_ENCRYPTED_VIEW_ENABLED)
704 crypt_stat->flags |= ECRYPTFS_VIEW_AS_ENCRYPTED;
Michael Halcrowaddd65ad2009-01-06 14:42:00 -0800705 if (mount_crypt_stat->flags & ECRYPTFS_GLOBAL_ENCRYPT_FILENAMES) {
706 crypt_stat->flags |= ECRYPTFS_ENCRYPT_FILENAMES;
707 if (mount_crypt_stat->flags
708 & ECRYPTFS_GLOBAL_ENCFN_USE_MOUNT_FNEK)
709 crypt_stat->flags |= ECRYPTFS_ENCFN_USE_MOUNT_FNEK;
710 else if (mount_crypt_stat->flags
711 & ECRYPTFS_GLOBAL_ENCFN_USE_FEK)
712 crypt_stat->flags |= ECRYPTFS_ENCFN_USE_FEK;
713 }
Michael Halcrow17398952007-02-12 00:53:45 -0800714}
715
Michael Halcrowf4aad162007-10-16 01:27:53 -0700716static int ecryptfs_copy_mount_wide_sigs_to_inode_sigs(
717 struct ecryptfs_crypt_stat *crypt_stat,
718 struct ecryptfs_mount_crypt_stat *mount_crypt_stat)
719{
720 struct ecryptfs_global_auth_tok *global_auth_tok;
721 int rc = 0;
722
Roland Dreieraa061172009-07-01 15:48:18 -0700723 mutex_lock(&crypt_stat->keysig_list_mutex);
Michael Halcrowf4aad162007-10-16 01:27:53 -0700724 mutex_lock(&mount_crypt_stat->global_auth_tok_list_mutex);
Roland Dreieraa061172009-07-01 15:48:18 -0700725
Michael Halcrowf4aad162007-10-16 01:27:53 -0700726 list_for_each_entry(global_auth_tok,
727 &mount_crypt_stat->global_auth_tok_list,
728 mount_crypt_stat_list) {
Tyler Hicks84814d62009-03-13 13:51:59 -0700729 if (global_auth_tok->flags & ECRYPTFS_AUTH_TOK_FNEK)
730 continue;
Michael Halcrowf4aad162007-10-16 01:27:53 -0700731 rc = ecryptfs_add_keysig(crypt_stat, global_auth_tok->sig);
732 if (rc) {
733 printk(KERN_ERR "Error adding keysig; rc = [%d]\n", rc);
Michael Halcrowf4aad162007-10-16 01:27:53 -0700734 goto out;
735 }
736 }
Roland Dreieraa061172009-07-01 15:48:18 -0700737
Michael Halcrowf4aad162007-10-16 01:27:53 -0700738out:
Roland Dreieraa061172009-07-01 15:48:18 -0700739 mutex_unlock(&mount_crypt_stat->global_auth_tok_list_mutex);
740 mutex_unlock(&crypt_stat->keysig_list_mutex);
Michael Halcrowf4aad162007-10-16 01:27:53 -0700741 return rc;
742}
743
Michael Halcrow17398952007-02-12 00:53:45 -0800744/**
Michael Halcrow237fead2006-10-04 02:16:22 -0700745 * ecryptfs_set_default_crypt_stat_vals
Michael Halcrow22e78fa2007-10-16 01:28:02 -0700746 * @crypt_stat: The inode's cryptographic context
747 * @mount_crypt_stat: The mount point's cryptographic context
Michael Halcrow237fead2006-10-04 02:16:22 -0700748 *
749 * Default values in the event that policy does not override them.
750 */
751static void ecryptfs_set_default_crypt_stat_vals(
752 struct ecryptfs_crypt_stat *crypt_stat,
753 struct ecryptfs_mount_crypt_stat *mount_crypt_stat)
754{
Michael Halcrow17398952007-02-12 00:53:45 -0800755 ecryptfs_copy_mount_wide_flags_to_inode_flags(crypt_stat,
756 mount_crypt_stat);
Michael Halcrow237fead2006-10-04 02:16:22 -0700757 ecryptfs_set_default_sizes(crypt_stat);
758 strcpy(crypt_stat->cipher, ECRYPTFS_DEFAULT_CIPHER);
759 crypt_stat->key_size = ECRYPTFS_DEFAULT_KEY_BYTES;
Michael Halcrowe2bd99e2007-02-12 00:53:49 -0800760 crypt_stat->flags &= ~(ECRYPTFS_KEY_VALID);
Michael Halcrow237fead2006-10-04 02:16:22 -0700761 crypt_stat->file_version = ECRYPTFS_FILE_VERSION;
762 crypt_stat->mount_crypt_stat = mount_crypt_stat;
763}
764
765/**
766 * ecryptfs_new_file_context
Tyler Hicksb59db432011-11-21 17:31:02 -0600767 * @ecryptfs_inode: The eCryptfs inode
Michael Halcrow237fead2006-10-04 02:16:22 -0700768 *
769 * If the crypto context for the file has not yet been established,
770 * this is where we do that. Establishing a new crypto context
771 * involves the following decisions:
772 * - What cipher to use?
773 * - What set of authentication tokens to use?
774 * Here we just worry about getting enough information into the
775 * authentication tokens so that we know that they are available.
776 * We associate the available authentication tokens with the new file
777 * via the set of signatures in the crypt_stat struct. Later, when
778 * the headers are actually written out, we may again defer to
779 * userspace to perform the encryption of the session key; for the
780 * foreseeable future, this will be the case with public key packets.
781 *
782 * Returns zero on success; non-zero otherwise
783 */
Tyler Hicksb59db432011-11-21 17:31:02 -0600784int ecryptfs_new_file_context(struct inode *ecryptfs_inode)
Michael Halcrow237fead2006-10-04 02:16:22 -0700785{
Michael Halcrow237fead2006-10-04 02:16:22 -0700786 struct ecryptfs_crypt_stat *crypt_stat =
Tyler Hicksb59db432011-11-21 17:31:02 -0600787 &ecryptfs_inode_to_private(ecryptfs_inode)->crypt_stat;
Michael Halcrow237fead2006-10-04 02:16:22 -0700788 struct ecryptfs_mount_crypt_stat *mount_crypt_stat =
789 &ecryptfs_superblock_to_private(
Tyler Hicksb59db432011-11-21 17:31:02 -0600790 ecryptfs_inode->i_sb)->mount_crypt_stat;
Michael Halcrow237fead2006-10-04 02:16:22 -0700791 int cipher_name_len;
Michael Halcrowf4aad162007-10-16 01:27:53 -0700792 int rc = 0;
Michael Halcrow237fead2006-10-04 02:16:22 -0700793
794 ecryptfs_set_default_crypt_stat_vals(crypt_stat, mount_crypt_stat);
Michael Halcrowaf655dc2007-10-16 01:28:00 -0700795 crypt_stat->flags |= (ECRYPTFS_ENCRYPTED | ECRYPTFS_KEY_VALID);
Michael Halcrowf4aad162007-10-16 01:27:53 -0700796 ecryptfs_copy_mount_wide_flags_to_inode_flags(crypt_stat,
797 mount_crypt_stat);
798 rc = ecryptfs_copy_mount_wide_sigs_to_inode_sigs(crypt_stat,
799 mount_crypt_stat);
800 if (rc) {
801 printk(KERN_ERR "Error attempting to copy mount-wide key sigs "
802 "to the inode key sigs; rc = [%d]\n", rc);
803 goto out;
804 }
805 cipher_name_len =
806 strlen(mount_crypt_stat->global_default_cipher_name);
807 memcpy(crypt_stat->cipher,
808 mount_crypt_stat->global_default_cipher_name,
809 cipher_name_len);
810 crypt_stat->cipher[cipher_name_len] = '\0';
811 crypt_stat->key_size =
812 mount_crypt_stat->global_default_cipher_key_size;
813 ecryptfs_generate_new_key(crypt_stat);
Michael Halcrow237fead2006-10-04 02:16:22 -0700814 rc = ecryptfs_init_crypt_ctx(crypt_stat);
815 if (rc)
816 ecryptfs_printk(KERN_ERR, "Error initializing cryptographic "
817 "context for cipher [%s]: rc = [%d]\n",
818 crypt_stat->cipher, rc);
Michael Halcrowf4aad162007-10-16 01:27:53 -0700819out:
Michael Halcrow237fead2006-10-04 02:16:22 -0700820 return rc;
821}
822
823/**
Tyler Hicks7a866172011-05-02 00:39:54 -0500824 * ecryptfs_validate_marker - check for the ecryptfs marker
Michael Halcrow237fead2006-10-04 02:16:22 -0700825 * @data: The data block in which to check
826 *
Tyler Hicks7a866172011-05-02 00:39:54 -0500827 * Returns zero if marker found; -EINVAL if not found
Michael Halcrow237fead2006-10-04 02:16:22 -0700828 */
Tyler Hicks7a866172011-05-02 00:39:54 -0500829static int ecryptfs_validate_marker(char *data)
Michael Halcrow237fead2006-10-04 02:16:22 -0700830{
831 u32 m_1, m_2;
832
Harvey Harrison29335c62008-07-23 21:30:06 -0700833 m_1 = get_unaligned_be32(data);
834 m_2 = get_unaligned_be32(data + 4);
Michael Halcrow237fead2006-10-04 02:16:22 -0700835 if ((m_1 ^ MAGIC_ECRYPTFS_MARKER) == m_2)
Tyler Hicks7a866172011-05-02 00:39:54 -0500836 return 0;
Michael Halcrow237fead2006-10-04 02:16:22 -0700837 ecryptfs_printk(KERN_DEBUG, "m_1 = [0x%.8x]; m_2 = [0x%.8x]; "
838 "MAGIC_ECRYPTFS_MARKER = [0x%.8x]\n", m_1, m_2,
839 MAGIC_ECRYPTFS_MARKER);
840 ecryptfs_printk(KERN_DEBUG, "(m_1 ^ MAGIC_ECRYPTFS_MARKER) = "
841 "[0x%.8x]\n", (m_1 ^ MAGIC_ECRYPTFS_MARKER));
Tyler Hicks7a866172011-05-02 00:39:54 -0500842 return -EINVAL;
Michael Halcrow237fead2006-10-04 02:16:22 -0700843}
844
845struct ecryptfs_flag_map_elem {
846 u32 file_flag;
847 u32 local_flag;
848};
849
850/* Add support for additional flags by adding elements here. */
851static struct ecryptfs_flag_map_elem ecryptfs_flag_map[] = {
852 {0x00000001, ECRYPTFS_ENABLE_HMAC},
Michael Halcrowdd2a3b72007-02-12 00:53:46 -0800853 {0x00000002, ECRYPTFS_ENCRYPTED},
Michael Halcrowaddd65ad2009-01-06 14:42:00 -0800854 {0x00000004, ECRYPTFS_METADATA_IN_XATTR},
855 {0x00000008, ECRYPTFS_ENCRYPT_FILENAMES}
Michael Halcrow237fead2006-10-04 02:16:22 -0700856};
857
858/**
859 * ecryptfs_process_flags
Michael Halcrow22e78fa2007-10-16 01:28:02 -0700860 * @crypt_stat: The cryptographic context
Michael Halcrow237fead2006-10-04 02:16:22 -0700861 * @page_virt: Source data to be parsed
862 * @bytes_read: Updated with the number of bytes read
863 *
864 * Returns zero on success; non-zero if the flag set is invalid
865 */
866static int ecryptfs_process_flags(struct ecryptfs_crypt_stat *crypt_stat,
867 char *page_virt, int *bytes_read)
868{
869 int rc = 0;
870 int i;
871 u32 flags;
872
Harvey Harrison29335c62008-07-23 21:30:06 -0700873 flags = get_unaligned_be32(page_virt);
Jérémy Lefaure02f98762017-10-01 15:30:46 -0400874 for (i = 0; i < ARRAY_SIZE(ecryptfs_flag_map); i++)
Michael Halcrow237fead2006-10-04 02:16:22 -0700875 if (flags & ecryptfs_flag_map[i].file_flag) {
Michael Halcrowe2bd99e2007-02-12 00:53:49 -0800876 crypt_stat->flags |= ecryptfs_flag_map[i].local_flag;
Michael Halcrow237fead2006-10-04 02:16:22 -0700877 } else
Michael Halcrowe2bd99e2007-02-12 00:53:49 -0800878 crypt_stat->flags &= ~(ecryptfs_flag_map[i].local_flag);
Michael Halcrow237fead2006-10-04 02:16:22 -0700879 /* Version is in top 8 bits of the 32-bit flag vector */
880 crypt_stat->file_version = ((flags >> 24) & 0xFF);
881 (*bytes_read) = 4;
882 return rc;
883}
884
885/**
886 * write_ecryptfs_marker
887 * @page_virt: The pointer to in a page to begin writing the marker
888 * @written: Number of bytes written
889 *
890 * Marker = 0x3c81b7f5
891 */
892static void write_ecryptfs_marker(char *page_virt, size_t *written)
893{
894 u32 m_1, m_2;
895
896 get_random_bytes(&m_1, (MAGIC_ECRYPTFS_MARKER_SIZE_BYTES / 2));
897 m_2 = (m_1 ^ MAGIC_ECRYPTFS_MARKER);
Harvey Harrison29335c62008-07-23 21:30:06 -0700898 put_unaligned_be32(m_1, page_virt);
899 page_virt += (MAGIC_ECRYPTFS_MARKER_SIZE_BYTES / 2);
900 put_unaligned_be32(m_2, page_virt);
Michael Halcrow237fead2006-10-04 02:16:22 -0700901 (*written) = MAGIC_ECRYPTFS_MARKER_SIZE_BYTES;
902}
903
Tyler Hicksf4e60e62010-02-11 00:02:32 -0600904void ecryptfs_write_crypt_stat_flags(char *page_virt,
905 struct ecryptfs_crypt_stat *crypt_stat,
906 size_t *written)
Michael Halcrow237fead2006-10-04 02:16:22 -0700907{
908 u32 flags = 0;
909 int i;
910
Jérémy Lefaure02f98762017-10-01 15:30:46 -0400911 for (i = 0; i < ARRAY_SIZE(ecryptfs_flag_map); i++)
Michael Halcrowe2bd99e2007-02-12 00:53:49 -0800912 if (crypt_stat->flags & ecryptfs_flag_map[i].local_flag)
Michael Halcrow237fead2006-10-04 02:16:22 -0700913 flags |= ecryptfs_flag_map[i].file_flag;
914 /* Version is in top 8 bits of the 32-bit flag vector */
915 flags |= ((((u8)crypt_stat->file_version) << 24) & 0xFF000000);
Harvey Harrison29335c62008-07-23 21:30:06 -0700916 put_unaligned_be32(flags, page_virt);
Michael Halcrow237fead2006-10-04 02:16:22 -0700917 (*written) = 4;
918}
919
920struct ecryptfs_cipher_code_str_map_elem {
921 char cipher_str[16];
Trevor Highland19e66a62008-02-06 01:38:36 -0800922 u8 cipher_code;
Michael Halcrow237fead2006-10-04 02:16:22 -0700923};
924
925/* Add support for additional ciphers by adding elements here. The
Chris J Arges40f0fd32016-06-09 15:31:29 -0500926 * cipher_code is whatever OpenPGP applications use to identify the
Michael Halcrow237fead2006-10-04 02:16:22 -0700927 * ciphers. List in order of probability. */
928static struct ecryptfs_cipher_code_str_map_elem
929ecryptfs_cipher_code_str_map[] = {
930 {"aes",RFC2440_CIPHER_AES_128 },
931 {"blowfish", RFC2440_CIPHER_BLOWFISH},
932 {"des3_ede", RFC2440_CIPHER_DES3_EDE},
933 {"cast5", RFC2440_CIPHER_CAST_5},
934 {"twofish", RFC2440_CIPHER_TWOFISH},
935 {"cast6", RFC2440_CIPHER_CAST_6},
936 {"aes", RFC2440_CIPHER_AES_192},
937 {"aes", RFC2440_CIPHER_AES_256}
938};
939
940/**
941 * ecryptfs_code_for_cipher_string
Michael Halcrow9c79f342009-01-06 14:41:57 -0800942 * @cipher_name: The string alias for the cipher
943 * @key_bytes: Length of key in bytes; used for AES code selection
Michael Halcrow237fead2006-10-04 02:16:22 -0700944 *
945 * Returns zero on no match, or the cipher code on match
946 */
Michael Halcrow9c79f342009-01-06 14:41:57 -0800947u8 ecryptfs_code_for_cipher_string(char *cipher_name, size_t key_bytes)
Michael Halcrow237fead2006-10-04 02:16:22 -0700948{
949 int i;
Trevor Highland19e66a62008-02-06 01:38:36 -0800950 u8 code = 0;
Michael Halcrow237fead2006-10-04 02:16:22 -0700951 struct ecryptfs_cipher_code_str_map_elem *map =
952 ecryptfs_cipher_code_str_map;
953
Michael Halcrow9c79f342009-01-06 14:41:57 -0800954 if (strcmp(cipher_name, "aes") == 0) {
955 switch (key_bytes) {
Michael Halcrow237fead2006-10-04 02:16:22 -0700956 case 16:
957 code = RFC2440_CIPHER_AES_128;
958 break;
959 case 24:
960 code = RFC2440_CIPHER_AES_192;
961 break;
962 case 32:
963 code = RFC2440_CIPHER_AES_256;
964 }
965 } else {
966 for (i = 0; i < ARRAY_SIZE(ecryptfs_cipher_code_str_map); i++)
Michael Halcrow9c79f342009-01-06 14:41:57 -0800967 if (strcmp(cipher_name, map[i].cipher_str) == 0) {
Michael Halcrow237fead2006-10-04 02:16:22 -0700968 code = map[i].cipher_code;
969 break;
970 }
971 }
972 return code;
973}
974
975/**
976 * ecryptfs_cipher_code_to_string
977 * @str: Destination to write out the cipher name
978 * @cipher_code: The code to convert to cipher name string
979 *
980 * Returns zero on success
981 */
Trevor Highland19e66a62008-02-06 01:38:36 -0800982int ecryptfs_cipher_code_to_string(char *str, u8 cipher_code)
Michael Halcrow237fead2006-10-04 02:16:22 -0700983{
984 int rc = 0;
985 int i;
986
987 str[0] = '\0';
988 for (i = 0; i < ARRAY_SIZE(ecryptfs_cipher_code_str_map); i++)
989 if (cipher_code == ecryptfs_cipher_code_str_map[i].cipher_code)
990 strcpy(str, ecryptfs_cipher_code_str_map[i].cipher_str);
991 if (str[0] == '\0') {
992 ecryptfs_printk(KERN_WARNING, "Cipher code not recognized: "
993 "[%d]\n", cipher_code);
994 rc = -EINVAL;
995 }
996 return rc;
997}
998
Tyler Hicks778aeb42011-05-24 04:56:23 -0500999int ecryptfs_read_and_validate_header_region(struct inode *inode)
Michael Halcrowdd2a3b72007-02-12 00:53:46 -08001000{
Tyler Hicks778aeb42011-05-24 04:56:23 -05001001 u8 file_size[ECRYPTFS_SIZE_AND_MARKER_BYTES];
1002 u8 *marker = file_size + ECRYPTFS_FILE_SIZE_BYTES;
Michael Halcrowdd2a3b72007-02-12 00:53:46 -08001003 int rc;
1004
Tyler Hicks778aeb42011-05-24 04:56:23 -05001005 rc = ecryptfs_read_lower(file_size, 0, ECRYPTFS_SIZE_AND_MARKER_BYTES,
1006 inode);
1007 if (rc < ECRYPTFS_SIZE_AND_MARKER_BYTES)
1008 return rc >= 0 ? -EINVAL : rc;
1009 rc = ecryptfs_validate_marker(marker);
1010 if (!rc)
1011 ecryptfs_i_size_init(file_size, inode);
Michael Halcrowdd2a3b72007-02-12 00:53:46 -08001012 return rc;
1013}
1014
Michael Halcrowe77a56d2007-02-12 00:53:47 -08001015void
1016ecryptfs_write_header_metadata(char *virt,
1017 struct ecryptfs_crypt_stat *crypt_stat,
1018 size_t *written)
Michael Halcrow237fead2006-10-04 02:16:22 -07001019{
1020 u32 header_extent_size;
1021 u16 num_header_extents_at_front;
1022
Michael Halcrow45eaab72007-10-16 01:28:05 -07001023 header_extent_size = (u32)crypt_stat->extent_size;
Michael Halcrow237fead2006-10-04 02:16:22 -07001024 num_header_extents_at_front =
Tyler Hicksfa3ef1c2010-02-11 05:09:14 -06001025 (u16)(crypt_stat->metadata_size / crypt_stat->extent_size);
Harvey Harrison29335c62008-07-23 21:30:06 -07001026 put_unaligned_be32(header_extent_size, virt);
Michael Halcrow237fead2006-10-04 02:16:22 -07001027 virt += 4;
Harvey Harrison29335c62008-07-23 21:30:06 -07001028 put_unaligned_be16(num_header_extents_at_front, virt);
Michael Halcrow237fead2006-10-04 02:16:22 -07001029 (*written) = 6;
1030}
1031
Tyler Hicks30632872011-05-24 05:11:12 -05001032struct kmem_cache *ecryptfs_header_cache;
Michael Halcrow237fead2006-10-04 02:16:22 -07001033
1034/**
1035 * ecryptfs_write_headers_virt
Michael Halcrow22e78fa2007-10-16 01:28:02 -07001036 * @page_virt: The virtual address to write the headers to
Eric Sandeen87b811c32008-10-29 14:01:08 -07001037 * @max: The size of memory allocated at page_virt
Michael Halcrow22e78fa2007-10-16 01:28:02 -07001038 * @size: Set to the number of bytes written by this function
1039 * @crypt_stat: The cryptographic context
1040 * @ecryptfs_dentry: The eCryptfs dentry
Michael Halcrow237fead2006-10-04 02:16:22 -07001041 *
1042 * Format version: 1
1043 *
1044 * Header Extent:
1045 * Octets 0-7: Unencrypted file size (big-endian)
1046 * Octets 8-15: eCryptfs special marker
1047 * Octets 16-19: Flags
1048 * Octet 16: File format version number (between 0 and 255)
1049 * Octets 17-18: Reserved
1050 * Octet 19: Bit 1 (lsb): Reserved
1051 * Bit 2: Encrypted?
1052 * Bits 3-8: Reserved
1053 * Octets 20-23: Header extent size (big-endian)
1054 * Octets 24-25: Number of header extents at front of file
1055 * (big-endian)
1056 * Octet 26: Begin RFC 2440 authentication token packet set
1057 * Data Extent 0:
1058 * Lower data (CBC encrypted)
1059 * Data Extent 1:
1060 * Lower data (CBC encrypted)
1061 * ...
1062 *
1063 * Returns zero on success
1064 */
Eric Sandeen87b811c32008-10-29 14:01:08 -07001065static int ecryptfs_write_headers_virt(char *page_virt, size_t max,
1066 size_t *size,
Michael Halcrowdd2a3b72007-02-12 00:53:46 -08001067 struct ecryptfs_crypt_stat *crypt_stat,
1068 struct dentry *ecryptfs_dentry)
Michael Halcrow237fead2006-10-04 02:16:22 -07001069{
1070 int rc;
1071 size_t written;
1072 size_t offset;
1073
1074 offset = ECRYPTFS_FILE_SIZE_BYTES;
1075 write_ecryptfs_marker((page_virt + offset), &written);
1076 offset += written;
Tyler Hicksf4e60e62010-02-11 00:02:32 -06001077 ecryptfs_write_crypt_stat_flags((page_virt + offset), crypt_stat,
1078 &written);
Michael Halcrow237fead2006-10-04 02:16:22 -07001079 offset += written;
Michael Halcrowe77a56d2007-02-12 00:53:47 -08001080 ecryptfs_write_header_metadata((page_virt + offset), crypt_stat,
1081 &written);
Michael Halcrow237fead2006-10-04 02:16:22 -07001082 offset += written;
1083 rc = ecryptfs_generate_key_packet_set((page_virt + offset), crypt_stat,
1084 ecryptfs_dentry, &written,
Eric Sandeen87b811c32008-10-29 14:01:08 -07001085 max - offset);
Michael Halcrow237fead2006-10-04 02:16:22 -07001086 if (rc)
1087 ecryptfs_printk(KERN_WARNING, "Error generating key packet "
1088 "set; rc = [%d]\n", rc);
Michael Halcrowdd2a3b72007-02-12 00:53:46 -08001089 if (size) {
1090 offset += written;
1091 *size = offset;
1092 }
1093 return rc;
1094}
1095
Michael Halcrow22e78fa2007-10-16 01:28:02 -07001096static int
Tyler Hicksb59db432011-11-21 17:31:02 -06001097ecryptfs_write_metadata_to_contents(struct inode *ecryptfs_inode,
Tyler Hicks8faece52009-03-20 01:25:09 -05001098 char *virt, size_t virt_len)
Michael Halcrowdd2a3b72007-02-12 00:53:46 -08001099{
Michael Halcrowd7cdc5f2007-10-16 01:28:10 -07001100 int rc;
Michael Halcrowdd2a3b72007-02-12 00:53:46 -08001101
Tyler Hicksb59db432011-11-21 17:31:02 -06001102 rc = ecryptfs_write_lower(ecryptfs_inode, virt,
Tyler Hicks8faece52009-03-20 01:25:09 -05001103 0, virt_len);
Tyler Hicks96a7b9c2009-09-16 19:04:20 -05001104 if (rc < 0)
Michael Halcrowd7cdc5f2007-10-16 01:28:10 -07001105 printk(KERN_ERR "%s: Error attempting to write header "
Tyler Hicks96a7b9c2009-09-16 19:04:20 -05001106 "information to lower file; rc = [%d]\n", __func__, rc);
1107 else
1108 rc = 0;
Michael Halcrow70456602007-02-12 00:53:48 -08001109 return rc;
Michael Halcrowdd2a3b72007-02-12 00:53:46 -08001110}
1111
Michael Halcrow22e78fa2007-10-16 01:28:02 -07001112static int
1113ecryptfs_write_metadata_to_xattr(struct dentry *ecryptfs_dentry,
Al Viro3767e252016-05-27 11:06:05 -04001114 struct inode *ecryptfs_inode,
Michael Halcrow22e78fa2007-10-16 01:28:02 -07001115 char *page_virt, size_t size)
Michael Halcrowdd2a3b72007-02-12 00:53:46 -08001116{
1117 int rc;
1118
Al Viro3767e252016-05-27 11:06:05 -04001119 rc = ecryptfs_setxattr(ecryptfs_dentry, ecryptfs_inode,
1120 ECRYPTFS_XATTR_NAME, page_virt, size, 0);
Michael Halcrow237fead2006-10-04 02:16:22 -07001121 return rc;
1122}
1123
Tyler Hicks8faece52009-03-20 01:25:09 -05001124static unsigned long ecryptfs_get_zeroed_pages(gfp_t gfp_mask,
1125 unsigned int order)
1126{
1127 struct page *page;
1128
1129 page = alloc_pages(gfp_mask | __GFP_ZERO, order);
1130 if (page)
1131 return (unsigned long) page_address(page);
1132 return 0;
1133}
1134
Michael Halcrow237fead2006-10-04 02:16:22 -07001135/**
Michael Halcrowdd2a3b72007-02-12 00:53:46 -08001136 * ecryptfs_write_metadata
Tyler Hicksb59db432011-11-21 17:31:02 -06001137 * @ecryptfs_dentry: The eCryptfs dentry, which should be negative
1138 * @ecryptfs_inode: The newly created eCryptfs inode
Michael Halcrow237fead2006-10-04 02:16:22 -07001139 *
1140 * Write the file headers out. This will likely involve a userspace
1141 * callout, in which the session key is encrypted with one or more
1142 * public keys and/or the passphrase necessary to do the encryption is
1143 * retrieved via a prompt. Exactly what happens at this point should
1144 * be policy-dependent.
1145 *
1146 * Returns zero on success; non-zero on error
1147 */
Tyler Hicksb59db432011-11-21 17:31:02 -06001148int ecryptfs_write_metadata(struct dentry *ecryptfs_dentry,
1149 struct inode *ecryptfs_inode)
Michael Halcrow237fead2006-10-04 02:16:22 -07001150{
Michael Halcrowd7cdc5f2007-10-16 01:28:10 -07001151 struct ecryptfs_crypt_stat *crypt_stat =
Tyler Hicksb59db432011-11-21 17:31:02 -06001152 &ecryptfs_inode_to_private(ecryptfs_inode)->crypt_stat;
Tyler Hicks8faece52009-03-20 01:25:09 -05001153 unsigned int order;
Michael Halcrowcc11bef2008-02-06 01:38:32 -08001154 char *virt;
Tyler Hicks8faece52009-03-20 01:25:09 -05001155 size_t virt_len;
Michael Halcrowd7cdc5f2007-10-16 01:28:10 -07001156 size_t size = 0;
Michael Halcrow237fead2006-10-04 02:16:22 -07001157 int rc = 0;
1158
Michael Halcrowe2bd99e2007-02-12 00:53:49 -08001159 if (likely(crypt_stat->flags & ECRYPTFS_ENCRYPTED)) {
1160 if (!(crypt_stat->flags & ECRYPTFS_KEY_VALID)) {
Michael Halcrowd7cdc5f2007-10-16 01:28:10 -07001161 printk(KERN_ERR "Key is invalid; bailing out\n");
Michael Halcrow237fead2006-10-04 02:16:22 -07001162 rc = -EINVAL;
1163 goto out;
1164 }
1165 } else {
Michael Halcrowcc11bef2008-02-06 01:38:32 -08001166 printk(KERN_WARNING "%s: Encrypted flag not set\n",
Harvey Harrison18d1dbf2008-04-29 00:59:48 -07001167 __func__);
Michael Halcrow237fead2006-10-04 02:16:22 -07001168 rc = -EINVAL;
Michael Halcrow237fead2006-10-04 02:16:22 -07001169 goto out;
1170 }
Tyler Hicksfa3ef1c2010-02-11 05:09:14 -06001171 virt_len = crypt_stat->metadata_size;
Tyler Hicks8faece52009-03-20 01:25:09 -05001172 order = get_order(virt_len);
Michael Halcrow237fead2006-10-04 02:16:22 -07001173 /* Released in this function */
Tyler Hicks8faece52009-03-20 01:25:09 -05001174 virt = (char *)ecryptfs_get_zeroed_pages(GFP_KERNEL, order);
Michael Halcrowcc11bef2008-02-06 01:38:32 -08001175 if (!virt) {
Harvey Harrison18d1dbf2008-04-29 00:59:48 -07001176 printk(KERN_ERR "%s: Out of memory\n", __func__);
Michael Halcrow237fead2006-10-04 02:16:22 -07001177 rc = -ENOMEM;
1178 goto out;
1179 }
Tyler Hicksbd4f0fe2011-02-23 00:14:19 -06001180 /* Zeroed page ensures the in-header unencrypted i_size is set to 0 */
Tyler Hicks8faece52009-03-20 01:25:09 -05001181 rc = ecryptfs_write_headers_virt(virt, virt_len, &size, crypt_stat,
1182 ecryptfs_dentry);
Michael Halcrow237fead2006-10-04 02:16:22 -07001183 if (unlikely(rc)) {
Michael Halcrowcc11bef2008-02-06 01:38:32 -08001184 printk(KERN_ERR "%s: Error whilst writing headers; rc = [%d]\n",
Harvey Harrison18d1dbf2008-04-29 00:59:48 -07001185 __func__, rc);
Michael Halcrow237fead2006-10-04 02:16:22 -07001186 goto out_free;
1187 }
Michael Halcrowdd2a3b72007-02-12 00:53:46 -08001188 if (crypt_stat->flags & ECRYPTFS_METADATA_IN_XATTR)
Al Viro3767e252016-05-27 11:06:05 -04001189 rc = ecryptfs_write_metadata_to_xattr(ecryptfs_dentry, ecryptfs_inode,
1190 virt, size);
Michael Halcrowdd2a3b72007-02-12 00:53:46 -08001191 else
Tyler Hicksb59db432011-11-21 17:31:02 -06001192 rc = ecryptfs_write_metadata_to_contents(ecryptfs_inode, virt,
Tyler Hicks8faece52009-03-20 01:25:09 -05001193 virt_len);
Michael Halcrowdd2a3b72007-02-12 00:53:46 -08001194 if (rc) {
Michael Halcrowcc11bef2008-02-06 01:38:32 -08001195 printk(KERN_ERR "%s: Error writing metadata out to lower file; "
Harvey Harrison18d1dbf2008-04-29 00:59:48 -07001196 "rc = [%d]\n", __func__, rc);
Michael Halcrowdd2a3b72007-02-12 00:53:46 -08001197 goto out_free;
Michael Halcrow237fead2006-10-04 02:16:22 -07001198 }
Michael Halcrow237fead2006-10-04 02:16:22 -07001199out_free:
Tyler Hicks8faece52009-03-20 01:25:09 -05001200 free_pages((unsigned long)virt, order);
Michael Halcrow237fead2006-10-04 02:16:22 -07001201out:
1202 return rc;
1203}
1204
Michael Halcrowdd2a3b72007-02-12 00:53:46 -08001205#define ECRYPTFS_DONT_VALIDATE_HEADER_SIZE 0
1206#define ECRYPTFS_VALIDATE_HEADER_SIZE 1
Michael Halcrow237fead2006-10-04 02:16:22 -07001207static int parse_header_metadata(struct ecryptfs_crypt_stat *crypt_stat,
Michael Halcrowdd2a3b72007-02-12 00:53:46 -08001208 char *virt, int *bytes_read,
1209 int validate_header_size)
Michael Halcrow237fead2006-10-04 02:16:22 -07001210{
1211 int rc = 0;
1212 u32 header_extent_size;
1213 u16 num_header_extents_at_front;
1214
Harvey Harrison29335c62008-07-23 21:30:06 -07001215 header_extent_size = get_unaligned_be32(virt);
1216 virt += sizeof(__be32);
1217 num_header_extents_at_front = get_unaligned_be16(virt);
Tyler Hicksfa3ef1c2010-02-11 05:09:14 -06001218 crypt_stat->metadata_size = (((size_t)num_header_extents_at_front
1219 * (size_t)header_extent_size));
Harvey Harrison29335c62008-07-23 21:30:06 -07001220 (*bytes_read) = (sizeof(__be32) + sizeof(__be16));
Michael Halcrowdd2a3b72007-02-12 00:53:46 -08001221 if ((validate_header_size == ECRYPTFS_VALIDATE_HEADER_SIZE)
Tyler Hicksfa3ef1c2010-02-11 05:09:14 -06001222 && (crypt_stat->metadata_size
Michael Halcrowdd2a3b72007-02-12 00:53:46 -08001223 < ECRYPTFS_MINIMUM_HEADER_EXTENT_SIZE)) {
Michael Halcrow237fead2006-10-04 02:16:22 -07001224 rc = -EINVAL;
Michael Halcrowcc11bef2008-02-06 01:38:32 -08001225 printk(KERN_WARNING "Invalid header size: [%zd]\n",
Tyler Hicksfa3ef1c2010-02-11 05:09:14 -06001226 crypt_stat->metadata_size);
Michael Halcrow237fead2006-10-04 02:16:22 -07001227 }
1228 return rc;
1229}
1230
1231/**
1232 * set_default_header_data
Michael Halcrow22e78fa2007-10-16 01:28:02 -07001233 * @crypt_stat: The cryptographic context
Michael Halcrow237fead2006-10-04 02:16:22 -07001234 *
1235 * For version 0 file format; this function is only for backwards
1236 * compatibility for files created with the prior versions of
1237 * eCryptfs.
1238 */
1239static void set_default_header_data(struct ecryptfs_crypt_stat *crypt_stat)
1240{
Tyler Hicksfa3ef1c2010-02-11 05:09:14 -06001241 crypt_stat->metadata_size = ECRYPTFS_MINIMUM_HEADER_EXTENT_SIZE;
Michael Halcrow237fead2006-10-04 02:16:22 -07001242}
1243
Tyler Hicks3aeb86e2011-03-15 14:54:00 -05001244void ecryptfs_i_size_init(const char *page_virt, struct inode *inode)
1245{
1246 struct ecryptfs_mount_crypt_stat *mount_crypt_stat;
1247 struct ecryptfs_crypt_stat *crypt_stat;
1248 u64 file_size;
1249
1250 crypt_stat = &ecryptfs_inode_to_private(inode)->crypt_stat;
1251 mount_crypt_stat =
1252 &ecryptfs_superblock_to_private(inode->i_sb)->mount_crypt_stat;
1253 if (mount_crypt_stat->flags & ECRYPTFS_ENCRYPTED_VIEW_ENABLED) {
1254 file_size = i_size_read(ecryptfs_inode_to_lower(inode));
1255 if (crypt_stat->flags & ECRYPTFS_METADATA_IN_XATTR)
1256 file_size += crypt_stat->metadata_size;
1257 } else
1258 file_size = get_unaligned_be64(page_virt);
1259 i_size_write(inode, (loff_t)file_size);
1260 crypt_stat->flags |= ECRYPTFS_I_SIZE_INITIALIZED;
1261}
1262
Michael Halcrow237fead2006-10-04 02:16:22 -07001263/**
1264 * ecryptfs_read_headers_virt
Michael Halcrow22e78fa2007-10-16 01:28:02 -07001265 * @page_virt: The virtual address into which to read the headers
1266 * @crypt_stat: The cryptographic context
1267 * @ecryptfs_dentry: The eCryptfs dentry
1268 * @validate_header_size: Whether to validate the header size while reading
Michael Halcrow237fead2006-10-04 02:16:22 -07001269 *
1270 * Read/parse the header data. The header format is detailed in the
1271 * comment block for the ecryptfs_write_headers_virt() function.
1272 *
1273 * Returns zero on success
1274 */
1275static int ecryptfs_read_headers_virt(char *page_virt,
1276 struct ecryptfs_crypt_stat *crypt_stat,
Michael Halcrowdd2a3b72007-02-12 00:53:46 -08001277 struct dentry *ecryptfs_dentry,
1278 int validate_header_size)
Michael Halcrow237fead2006-10-04 02:16:22 -07001279{
1280 int rc = 0;
1281 int offset;
1282 int bytes_read;
1283
1284 ecryptfs_set_default_sizes(crypt_stat);
1285 crypt_stat->mount_crypt_stat = &ecryptfs_superblock_to_private(
1286 ecryptfs_dentry->d_sb)->mount_crypt_stat;
1287 offset = ECRYPTFS_FILE_SIZE_BYTES;
Tyler Hicks7a866172011-05-02 00:39:54 -05001288 rc = ecryptfs_validate_marker(page_virt + offset);
1289 if (rc)
Michael Halcrow237fead2006-10-04 02:16:22 -07001290 goto out;
Tyler Hicks3aeb86e2011-03-15 14:54:00 -05001291 if (!(crypt_stat->flags & ECRYPTFS_I_SIZE_INITIALIZED))
David Howells2b0143b2015-03-17 22:25:59 +00001292 ecryptfs_i_size_init(page_virt, d_inode(ecryptfs_dentry));
Michael Halcrow237fead2006-10-04 02:16:22 -07001293 offset += MAGIC_ECRYPTFS_MARKER_SIZE_BYTES;
1294 rc = ecryptfs_process_flags(crypt_stat, (page_virt + offset),
1295 &bytes_read);
1296 if (rc) {
1297 ecryptfs_printk(KERN_WARNING, "Error processing flags\n");
1298 goto out;
1299 }
1300 if (crypt_stat->file_version > ECRYPTFS_SUPPORTED_FILE_VERSION) {
1301 ecryptfs_printk(KERN_WARNING, "File version is [%d]; only "
1302 "file version [%d] is supported by this "
1303 "version of eCryptfs\n",
1304 crypt_stat->file_version,
1305 ECRYPTFS_SUPPORTED_FILE_VERSION);
1306 rc = -EINVAL;
1307 goto out;
1308 }
1309 offset += bytes_read;
1310 if (crypt_stat->file_version >= 1) {
1311 rc = parse_header_metadata(crypt_stat, (page_virt + offset),
Michael Halcrowdd2a3b72007-02-12 00:53:46 -08001312 &bytes_read, validate_header_size);
Michael Halcrow237fead2006-10-04 02:16:22 -07001313 if (rc) {
1314 ecryptfs_printk(KERN_WARNING, "Error reading header "
1315 "metadata; rc = [%d]\n", rc);
1316 }
1317 offset += bytes_read;
1318 } else
1319 set_default_header_data(crypt_stat);
1320 rc = ecryptfs_parse_packet_set(crypt_stat, (page_virt + offset),
1321 ecryptfs_dentry);
1322out:
1323 return rc;
1324}
1325
1326/**
Michael Halcrowdd2a3b72007-02-12 00:53:46 -08001327 * ecryptfs_read_xattr_region
Michael Halcrow22e78fa2007-10-16 01:28:02 -07001328 * @page_virt: The vitual address into which to read the xattr data
Michael Halcrow2ed92552007-10-16 01:28:10 -07001329 * @ecryptfs_inode: The eCryptfs inode
Michael Halcrowdd2a3b72007-02-12 00:53:46 -08001330 *
1331 * Attempts to read the crypto metadata from the extended attribute
1332 * region of the lower file.
Michael Halcrow22e78fa2007-10-16 01:28:02 -07001333 *
1334 * Returns zero on success; non-zero on error
Michael Halcrowdd2a3b72007-02-12 00:53:46 -08001335 */
Michael Halcrowd7cdc5f2007-10-16 01:28:10 -07001336int ecryptfs_read_xattr_region(char *page_virt, struct inode *ecryptfs_inode)
Michael Halcrowdd2a3b72007-02-12 00:53:46 -08001337{
Michael Halcrowd7cdc5f2007-10-16 01:28:10 -07001338 struct dentry *lower_dentry =
Al Virob5830432014-10-31 01:22:04 -04001339 ecryptfs_inode_to_private(ecryptfs_inode)->lower_file->f_path.dentry;
Michael Halcrowdd2a3b72007-02-12 00:53:46 -08001340 ssize_t size;
1341 int rc = 0;
1342
Al Viroce23e642016-04-11 00:48:00 -04001343 size = ecryptfs_getxattr_lower(lower_dentry,
1344 ecryptfs_inode_to_lower(ecryptfs_inode),
1345 ECRYPTFS_XATTR_NAME,
Michael Halcrowd7cdc5f2007-10-16 01:28:10 -07001346 page_virt, ECRYPTFS_DEFAULT_EXTENT_SIZE);
Michael Halcrowdd2a3b72007-02-12 00:53:46 -08001347 if (size < 0) {
Michael Halcrow25bd8172008-02-06 01:38:35 -08001348 if (unlikely(ecryptfs_verbosity > 0))
1349 printk(KERN_INFO "Error attempting to read the [%s] "
1350 "xattr from the lower file; return value = "
1351 "[%zd]\n", ECRYPTFS_XATTR_NAME, size);
Michael Halcrowdd2a3b72007-02-12 00:53:46 -08001352 rc = -EINVAL;
1353 goto out;
1354 }
1355out:
1356 return rc;
1357}
1358
Tyler Hicks778aeb42011-05-24 04:56:23 -05001359int ecryptfs_read_and_validate_xattr_region(struct dentry *dentry,
Tyler Hicks3b06b3e2011-05-24 03:49:02 -05001360 struct inode *inode)
Michael Halcrowdd2a3b72007-02-12 00:53:46 -08001361{
Tyler Hicks778aeb42011-05-24 04:56:23 -05001362 u8 file_size[ECRYPTFS_SIZE_AND_MARKER_BYTES];
1363 u8 *marker = file_size + ECRYPTFS_FILE_SIZE_BYTES;
Michael Halcrowdd2a3b72007-02-12 00:53:46 -08001364 int rc;
1365
Tyler Hicks778aeb42011-05-24 04:56:23 -05001366 rc = ecryptfs_getxattr_lower(ecryptfs_dentry_to_lower(dentry),
Al Viroce23e642016-04-11 00:48:00 -04001367 ecryptfs_inode_to_lower(inode),
Tyler Hicks778aeb42011-05-24 04:56:23 -05001368 ECRYPTFS_XATTR_NAME, file_size,
1369 ECRYPTFS_SIZE_AND_MARKER_BYTES);
1370 if (rc < ECRYPTFS_SIZE_AND_MARKER_BYTES)
1371 return rc >= 0 ? -EINVAL : rc;
1372 rc = ecryptfs_validate_marker(marker);
1373 if (!rc)
1374 ecryptfs_i_size_init(file_size, inode);
Michael Halcrowdd2a3b72007-02-12 00:53:46 -08001375 return rc;
1376}
1377
1378/**
1379 * ecryptfs_read_metadata
1380 *
1381 * Common entry point for reading file metadata. From here, we could
1382 * retrieve the header information from the header region of the file,
Chris J Arges40f0fd32016-06-09 15:31:29 -05001383 * the xattr region of the file, or some other repository that is
Michael Halcrowdd2a3b72007-02-12 00:53:46 -08001384 * stored separately from the file itself. The current implementation
1385 * supports retrieving the metadata information from the file contents
1386 * and from the xattr region.
Michael Halcrow237fead2006-10-04 02:16:22 -07001387 *
1388 * Returns zero if valid headers found and parsed; non-zero otherwise
1389 */
Michael Halcrowd7cdc5f2007-10-16 01:28:10 -07001390int ecryptfs_read_metadata(struct dentry *ecryptfs_dentry)
Michael Halcrow237fead2006-10-04 02:16:22 -07001391{
Tim Gardnerbb450362012-01-12 16:31:55 +01001392 int rc;
1393 char *page_virt;
David Howells2b0143b2015-03-17 22:25:59 +00001394 struct inode *ecryptfs_inode = d_inode(ecryptfs_dentry);
Michael Halcrow237fead2006-10-04 02:16:22 -07001395 struct ecryptfs_crypt_stat *crypt_stat =
Michael Halcrowd7cdc5f2007-10-16 01:28:10 -07001396 &ecryptfs_inode_to_private(ecryptfs_inode)->crypt_stat;
Michael Halcrowe77a56d2007-02-12 00:53:47 -08001397 struct ecryptfs_mount_crypt_stat *mount_crypt_stat =
1398 &ecryptfs_superblock_to_private(
1399 ecryptfs_dentry->d_sb)->mount_crypt_stat;
Michael Halcrow237fead2006-10-04 02:16:22 -07001400
Michael Halcrowe77a56d2007-02-12 00:53:47 -08001401 ecryptfs_copy_mount_wide_flags_to_inode_flags(crypt_stat,
1402 mount_crypt_stat);
Michael Halcrow237fead2006-10-04 02:16:22 -07001403 /* Read the first page from the underlying file */
Tyler Hicks30632872011-05-24 05:11:12 -05001404 page_virt = kmem_cache_alloc(ecryptfs_header_cache, GFP_USER);
Michael Halcrow237fead2006-10-04 02:16:22 -07001405 if (!page_virt) {
1406 rc = -ENOMEM;
Michael Halcrow237fead2006-10-04 02:16:22 -07001407 goto out;
1408 }
Michael Halcrowd7cdc5f2007-10-16 01:28:10 -07001409 rc = ecryptfs_read_lower(page_virt, 0, crypt_stat->extent_size,
1410 ecryptfs_inode);
Tyler Hicks96a7b9c2009-09-16 19:04:20 -05001411 if (rc >= 0)
Michael Halcrowd7cdc5f2007-10-16 01:28:10 -07001412 rc = ecryptfs_read_headers_virt(page_virt, crypt_stat,
1413 ecryptfs_dentry,
1414 ECRYPTFS_VALIDATE_HEADER_SIZE);
Michael Halcrow237fead2006-10-04 02:16:22 -07001415 if (rc) {
Tim Gardnerbb450362012-01-12 16:31:55 +01001416 /* metadata is not in the file header, so try xattrs */
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03001417 memset(page_virt, 0, PAGE_SIZE);
Michael Halcrowd7cdc5f2007-10-16 01:28:10 -07001418 rc = ecryptfs_read_xattr_region(page_virt, ecryptfs_inode);
Michael Halcrowdd2a3b72007-02-12 00:53:46 -08001419 if (rc) {
1420 printk(KERN_DEBUG "Valid eCryptfs headers not found in "
Tim Gardner30373dc2012-01-12 16:31:55 +01001421 "file header region or xattr region, inode %lu\n",
1422 ecryptfs_inode->i_ino);
Michael Halcrowdd2a3b72007-02-12 00:53:46 -08001423 rc = -EINVAL;
1424 goto out;
1425 }
1426 rc = ecryptfs_read_headers_virt(page_virt, crypt_stat,
1427 ecryptfs_dentry,
1428 ECRYPTFS_DONT_VALIDATE_HEADER_SIZE);
1429 if (rc) {
1430 printk(KERN_DEBUG "Valid eCryptfs headers not found in "
Tim Gardner30373dc2012-01-12 16:31:55 +01001431 "file xattr region either, inode %lu\n",
1432 ecryptfs_inode->i_ino);
Michael Halcrowdd2a3b72007-02-12 00:53:46 -08001433 rc = -EINVAL;
1434 }
1435 if (crypt_stat->mount_crypt_stat->flags
1436 & ECRYPTFS_XATTR_METADATA_ENABLED) {
1437 crypt_stat->flags |= ECRYPTFS_METADATA_IN_XATTR;
1438 } else {
1439 printk(KERN_WARNING "Attempt to access file with "
1440 "crypto metadata only in the extended attribute "
1441 "region, but eCryptfs was mounted without "
1442 "xattr support enabled. eCryptfs will not treat "
Tim Gardner30373dc2012-01-12 16:31:55 +01001443 "this like an encrypted file, inode %lu\n",
1444 ecryptfs_inode->i_ino);
Michael Halcrowdd2a3b72007-02-12 00:53:46 -08001445 rc = -EINVAL;
1446 }
Michael Halcrow237fead2006-10-04 02:16:22 -07001447 }
1448out:
1449 if (page_virt) {
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03001450 memset(page_virt, 0, PAGE_SIZE);
Tyler Hicks30632872011-05-24 05:11:12 -05001451 kmem_cache_free(ecryptfs_header_cache, page_virt);
Michael Halcrow237fead2006-10-04 02:16:22 -07001452 }
1453 return rc;
1454}
1455
1456/**
Michael Halcrow51ca58d2009-01-06 14:41:59 -08001457 * ecryptfs_encrypt_filename - encrypt filename
1458 *
1459 * CBC-encrypts the filename. We do not want to encrypt the same
1460 * filename with the same key and IV, which may happen with hard
1461 * links, so we prepend random bits to each filename.
1462 *
1463 * Returns zero on success; non-zero otherwise
1464 */
1465static int
1466ecryptfs_encrypt_filename(struct ecryptfs_filename *filename,
Michael Halcrow51ca58d2009-01-06 14:41:59 -08001467 struct ecryptfs_mount_crypt_stat *mount_crypt_stat)
1468{
1469 int rc = 0;
1470
1471 filename->encrypted_filename = NULL;
1472 filename->encrypted_filename_size = 0;
Al Viro97c31602016-02-22 18:14:25 -05001473 if (mount_crypt_stat && (mount_crypt_stat->flags
1474 & ECRYPTFS_GLOBAL_ENCFN_USE_MOUNT_FNEK)) {
Michael Halcrow51ca58d2009-01-06 14:41:59 -08001475 size_t packet_size;
1476 size_t remaining_bytes;
1477
1478 rc = ecryptfs_write_tag_70_packet(
1479 NULL, NULL,
1480 &filename->encrypted_filename_size,
1481 mount_crypt_stat, NULL,
1482 filename->filename_size);
1483 if (rc) {
1484 printk(KERN_ERR "%s: Error attempting to get packet "
1485 "size for tag 72; rc = [%d]\n", __func__,
1486 rc);
1487 filename->encrypted_filename_size = 0;
1488 goto out;
1489 }
1490 filename->encrypted_filename =
1491 kmalloc(filename->encrypted_filename_size, GFP_KERNEL);
1492 if (!filename->encrypted_filename) {
Michael Halcrow51ca58d2009-01-06 14:41:59 -08001493 rc = -ENOMEM;
1494 goto out;
1495 }
1496 remaining_bytes = filename->encrypted_filename_size;
1497 rc = ecryptfs_write_tag_70_packet(filename->encrypted_filename,
1498 &remaining_bytes,
1499 &packet_size,
1500 mount_crypt_stat,
1501 filename->filename,
1502 filename->filename_size);
1503 if (rc) {
1504 printk(KERN_ERR "%s: Error attempting to generate "
1505 "tag 70 packet; rc = [%d]\n", __func__,
1506 rc);
1507 kfree(filename->encrypted_filename);
1508 filename->encrypted_filename = NULL;
1509 filename->encrypted_filename_size = 0;
1510 goto out;
1511 }
1512 filename->encrypted_filename_size = packet_size;
1513 } else {
1514 printk(KERN_ERR "%s: No support for requested filename "
1515 "encryption method in this release\n", __func__);
Tyler Hicksdf6ad332009-08-21 04:27:46 -05001516 rc = -EOPNOTSUPP;
Michael Halcrow51ca58d2009-01-06 14:41:59 -08001517 goto out;
1518 }
1519out:
1520 return rc;
1521}
1522
1523static int ecryptfs_copy_filename(char **copied_name, size_t *copied_name_size,
1524 const char *name, size_t name_size)
1525{
1526 int rc = 0;
1527
Tyler Hicksfd9fc842009-02-06 18:06:51 -06001528 (*copied_name) = kmalloc((name_size + 1), GFP_KERNEL);
Michael Halcrow51ca58d2009-01-06 14:41:59 -08001529 if (!(*copied_name)) {
1530 rc = -ENOMEM;
1531 goto out;
1532 }
1533 memcpy((void *)(*copied_name), (void *)name, name_size);
1534 (*copied_name)[(name_size)] = '\0'; /* Only for convenience
1535 * in printing out the
1536 * string in debug
1537 * messages */
Tyler Hicksfd9fc842009-02-06 18:06:51 -06001538 (*copied_name_size) = name_size;
Michael Halcrow51ca58d2009-01-06 14:41:59 -08001539out:
1540 return rc;
1541}
1542
1543/**
Michael Halcrowf4aad162007-10-16 01:27:53 -07001544 * ecryptfs_process_key_cipher - Perform key cipher initialization.
Michael Halcrow237fead2006-10-04 02:16:22 -07001545 * @key_tfm: Crypto context for key material, set by this function
Michael Halcrowe5d9cbd2006-10-30 22:07:16 -08001546 * @cipher_name: Name of the cipher
1547 * @key_size: Size of the key in bytes
Michael Halcrow237fead2006-10-04 02:16:22 -07001548 *
1549 * Returns zero on success. Any crypto_tfm structs allocated here
1550 * should be released by other functions, such as on a superblock put
1551 * event, regardless of whether this function succeeds for fails.
1552 */
Michael Halcrowcd9d67d2007-10-16 01:28:04 -07001553static int
Herbert Xu3095e8e2016-01-25 10:29:33 +08001554ecryptfs_process_key_cipher(struct crypto_skcipher **key_tfm,
Michael Halcrowf4aad162007-10-16 01:27:53 -07001555 char *cipher_name, size_t *key_size)
Michael Halcrow237fead2006-10-04 02:16:22 -07001556{
1557 char dummy_key[ECRYPTFS_MAX_KEY_BYTES];
Dan Carpenterece550f2010-01-19 12:34:32 +03001558 char *full_alg_name = NULL;
Michael Halcrow237fead2006-10-04 02:16:22 -07001559 int rc;
1560
Michael Halcrowe5d9cbd2006-10-30 22:07:16 -08001561 *key_tfm = NULL;
1562 if (*key_size > ECRYPTFS_MAX_KEY_BYTES) {
Michael Halcrow237fead2006-10-04 02:16:22 -07001563 rc = -EINVAL;
Michael Halcrowdf261c52009-01-06 14:42:02 -08001564 printk(KERN_ERR "Requested key size is [%zd] bytes; maximum "
Michael Halcrowe5d9cbd2006-10-30 22:07:16 -08001565 "allowable is [%d]\n", *key_size, ECRYPTFS_MAX_KEY_BYTES);
Michael Halcrow237fead2006-10-04 02:16:22 -07001566 goto out;
1567 }
Michael Halcrow8bba0662006-10-30 22:07:18 -08001568 rc = ecryptfs_crypto_api_algify_cipher_name(&full_alg_name, cipher_name,
1569 "ecb");
1570 if (rc)
1571 goto out;
Herbert Xu3095e8e2016-01-25 10:29:33 +08001572 *key_tfm = crypto_alloc_skcipher(full_alg_name, 0, CRYPTO_ALG_ASYNC);
Michael Halcrow8bba0662006-10-30 22:07:18 -08001573 if (IS_ERR(*key_tfm)) {
1574 rc = PTR_ERR(*key_tfm);
Michael Halcrow237fead2006-10-04 02:16:22 -07001575 printk(KERN_ERR "Unable to allocate crypto cipher with name "
Dave Hansen38268492009-08-27 09:47:07 -07001576 "[%s]; rc = [%d]\n", full_alg_name, rc);
Michael Halcrow237fead2006-10-04 02:16:22 -07001577 goto out;
1578 }
Eric Biggers231baec2019-01-18 22:48:00 -08001579 crypto_skcipher_set_flags(*key_tfm, CRYPTO_TFM_REQ_FORBID_WEAK_KEYS);
Herbert Xu3095e8e2016-01-25 10:29:33 +08001580 if (*key_size == 0)
1581 *key_size = crypto_skcipher_default_keysize(*key_tfm);
Michael Halcrowe5d9cbd2006-10-30 22:07:16 -08001582 get_random_bytes(dummy_key, *key_size);
Herbert Xu3095e8e2016-01-25 10:29:33 +08001583 rc = crypto_skcipher_setkey(*key_tfm, dummy_key, *key_size);
Michael Halcrow237fead2006-10-04 02:16:22 -07001584 if (rc) {
Michael Halcrowdf261c52009-01-06 14:42:02 -08001585 printk(KERN_ERR "Error attempting to set key of size [%zd] for "
Dave Hansen38268492009-08-27 09:47:07 -07001586 "cipher [%s]; rc = [%d]\n", *key_size, full_alg_name,
1587 rc);
Michael Halcrow237fead2006-10-04 02:16:22 -07001588 rc = -EINVAL;
1589 goto out;
1590 }
1591out:
Dan Carpenterece550f2010-01-19 12:34:32 +03001592 kfree(full_alg_name);
Michael Halcrow237fead2006-10-04 02:16:22 -07001593 return rc;
1594}
Michael Halcrowf4aad162007-10-16 01:27:53 -07001595
1596struct kmem_cache *ecryptfs_key_tfm_cache;
Adrian Bunk7896b632008-02-06 01:38:32 -08001597static struct list_head key_tfm_list;
Eric Sandeenaf440f52008-02-06 01:38:37 -08001598struct mutex key_tfm_list_mutex;
Michael Halcrowf4aad162007-10-16 01:27:53 -07001599
Jerome Marchand7371a382010-08-17 17:24:05 +02001600int __init ecryptfs_init_crypto(void)
Michael Halcrowf4aad162007-10-16 01:27:53 -07001601{
1602 mutex_init(&key_tfm_list_mutex);
1603 INIT_LIST_HEAD(&key_tfm_list);
1604 return 0;
1605}
1606
Eric Sandeenaf440f52008-02-06 01:38:37 -08001607/**
1608 * ecryptfs_destroy_crypto - free all cached key_tfms on key_tfm_list
1609 *
1610 * Called only at module unload time
1611 */
Michael Halcrowfcd12832007-10-16 01:28:01 -07001612int ecryptfs_destroy_crypto(void)
Michael Halcrowf4aad162007-10-16 01:27:53 -07001613{
1614 struct ecryptfs_key_tfm *key_tfm, *key_tfm_tmp;
1615
1616 mutex_lock(&key_tfm_list_mutex);
1617 list_for_each_entry_safe(key_tfm, key_tfm_tmp, &key_tfm_list,
1618 key_tfm_list) {
1619 list_del(&key_tfm->key_tfm_list);
Herbert Xu3095e8e2016-01-25 10:29:33 +08001620 crypto_free_skcipher(key_tfm->key_tfm);
Michael Halcrowf4aad162007-10-16 01:27:53 -07001621 kmem_cache_free(ecryptfs_key_tfm_cache, key_tfm);
1622 }
1623 mutex_unlock(&key_tfm_list_mutex);
1624 return 0;
1625}
1626
1627int
1628ecryptfs_add_new_key_tfm(struct ecryptfs_key_tfm **key_tfm, char *cipher_name,
1629 size_t key_size)
1630{
1631 struct ecryptfs_key_tfm *tmp_tfm;
1632 int rc = 0;
1633
Eric Sandeenaf440f52008-02-06 01:38:37 -08001634 BUG_ON(!mutex_is_locked(&key_tfm_list_mutex));
1635
Michael Halcrowf4aad162007-10-16 01:27:53 -07001636 tmp_tfm = kmem_cache_alloc(ecryptfs_key_tfm_cache, GFP_KERNEL);
Markus Elfring5032f362017-08-19 18:00:22 +02001637 if (key_tfm)
Michael Halcrowf4aad162007-10-16 01:27:53 -07001638 (*key_tfm) = tmp_tfm;
1639 if (!tmp_tfm) {
1640 rc = -ENOMEM;
Michael Halcrowf4aad162007-10-16 01:27:53 -07001641 goto out;
1642 }
1643 mutex_init(&tmp_tfm->key_tfm_mutex);
1644 strncpy(tmp_tfm->cipher_name, cipher_name,
1645 ECRYPTFS_MAX_CIPHER_NAME_SIZE);
Eric Sandeenb8862902007-12-22 14:03:24 -08001646 tmp_tfm->cipher_name[ECRYPTFS_MAX_CIPHER_NAME_SIZE] = '\0';
Michael Halcrowf4aad162007-10-16 01:27:53 -07001647 tmp_tfm->key_size = key_size;
Michael Halcrow5dda6992007-10-16 01:28:06 -07001648 rc = ecryptfs_process_key_cipher(&tmp_tfm->key_tfm,
1649 tmp_tfm->cipher_name,
1650 &tmp_tfm->key_size);
1651 if (rc) {
Michael Halcrowf4aad162007-10-16 01:27:53 -07001652 printk(KERN_ERR "Error attempting to initialize key TFM "
1653 "cipher with name = [%s]; rc = [%d]\n",
1654 tmp_tfm->cipher_name, rc);
1655 kmem_cache_free(ecryptfs_key_tfm_cache, tmp_tfm);
Markus Elfring5032f362017-08-19 18:00:22 +02001656 if (key_tfm)
Michael Halcrowf4aad162007-10-16 01:27:53 -07001657 (*key_tfm) = NULL;
1658 goto out;
1659 }
Michael Halcrowf4aad162007-10-16 01:27:53 -07001660 list_add(&tmp_tfm->key_tfm_list, &key_tfm_list);
Michael Halcrowf4aad162007-10-16 01:27:53 -07001661out:
1662 return rc;
1663}
1664
Eric Sandeenaf440f52008-02-06 01:38:37 -08001665/**
1666 * ecryptfs_tfm_exists - Search for existing tfm for cipher_name.
1667 * @cipher_name: the name of the cipher to search for
1668 * @key_tfm: set to corresponding tfm if found
1669 *
1670 * Searches for cached key_tfm matching @cipher_name
1671 * Must be called with &key_tfm_list_mutex held
1672 * Returns 1 if found, with @key_tfm set
1673 * Returns 0 if not found, with @key_tfm set to NULL
1674 */
1675int ecryptfs_tfm_exists(char *cipher_name, struct ecryptfs_key_tfm **key_tfm)
1676{
1677 struct ecryptfs_key_tfm *tmp_key_tfm;
1678
1679 BUG_ON(!mutex_is_locked(&key_tfm_list_mutex));
1680
1681 list_for_each_entry(tmp_key_tfm, &key_tfm_list, key_tfm_list) {
1682 if (strcmp(tmp_key_tfm->cipher_name, cipher_name) == 0) {
1683 if (key_tfm)
1684 (*key_tfm) = tmp_key_tfm;
1685 return 1;
1686 }
1687 }
1688 if (key_tfm)
1689 (*key_tfm) = NULL;
1690 return 0;
1691}
1692
1693/**
1694 * ecryptfs_get_tfm_and_mutex_for_cipher_name
1695 *
1696 * @tfm: set to cached tfm found, or new tfm created
1697 * @tfm_mutex: set to mutex for cached tfm found, or new tfm created
1698 * @cipher_name: the name of the cipher to search for and/or add
1699 *
1700 * Sets pointers to @tfm & @tfm_mutex matching @cipher_name.
1701 * Searches for cached item first, and creates new if not found.
1702 * Returns 0 on success, non-zero if adding new cipher failed
1703 */
Herbert Xu3095e8e2016-01-25 10:29:33 +08001704int ecryptfs_get_tfm_and_mutex_for_cipher_name(struct crypto_skcipher **tfm,
Michael Halcrowf4aad162007-10-16 01:27:53 -07001705 struct mutex **tfm_mutex,
1706 char *cipher_name)
1707{
1708 struct ecryptfs_key_tfm *key_tfm;
1709 int rc = 0;
1710
1711 (*tfm) = NULL;
1712 (*tfm_mutex) = NULL;
Eric Sandeenaf440f52008-02-06 01:38:37 -08001713
Michael Halcrowf4aad162007-10-16 01:27:53 -07001714 mutex_lock(&key_tfm_list_mutex);
Eric Sandeenaf440f52008-02-06 01:38:37 -08001715 if (!ecryptfs_tfm_exists(cipher_name, &key_tfm)) {
1716 rc = ecryptfs_add_new_key_tfm(&key_tfm, cipher_name, 0);
1717 if (rc) {
1718 printk(KERN_ERR "Error adding new key_tfm to list; "
1719 "rc = [%d]\n", rc);
Michael Halcrowf4aad162007-10-16 01:27:53 -07001720 goto out;
1721 }
1722 }
Michael Halcrowf4aad162007-10-16 01:27:53 -07001723 (*tfm) = key_tfm->key_tfm;
1724 (*tfm_mutex) = &key_tfm->key_tfm_mutex;
1725out:
Cyrill Gorcunov71fd5172008-05-23 13:04:20 -07001726 mutex_unlock(&key_tfm_list_mutex);
Michael Halcrowf4aad162007-10-16 01:27:53 -07001727 return rc;
1728}
Michael Halcrow51ca58d2009-01-06 14:41:59 -08001729
1730/* 64 characters forming a 6-bit target field */
1731static unsigned char *portable_filename_chars = ("-.0123456789ABCD"
1732 "EFGHIJKLMNOPQRST"
1733 "UVWXYZabcdefghij"
1734 "klmnopqrstuvwxyz");
1735
1736/* We could either offset on every reverse map or just pad some 0x00's
1737 * at the front here */
Tyler Hicks0f751e62011-11-23 11:31:24 -06001738static const unsigned char filename_rev_map[256] = {
Michael Halcrow51ca58d2009-01-06 14:41:59 -08001739 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, /* 7 */
1740 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, /* 15 */
1741 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, /* 23 */
1742 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, /* 31 */
1743 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, /* 39 */
1744 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x01, 0x00, /* 47 */
1745 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, /* 55 */
1746 0x0A, 0x0B, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, /* 63 */
1747 0x00, 0x0C, 0x0D, 0x0E, 0x0F, 0x10, 0x11, 0x12, /* 71 */
1748 0x13, 0x14, 0x15, 0x16, 0x17, 0x18, 0x19, 0x1A, /* 79 */
1749 0x1B, 0x1C, 0x1D, 0x1E, 0x1F, 0x20, 0x21, 0x22, /* 87 */
1750 0x23, 0x24, 0x25, 0x00, 0x00, 0x00, 0x00, 0x00, /* 95 */
1751 0x00, 0x26, 0x27, 0x28, 0x29, 0x2A, 0x2B, 0x2C, /* 103 */
1752 0x2D, 0x2E, 0x2F, 0x30, 0x31, 0x32, 0x33, 0x34, /* 111 */
1753 0x35, 0x36, 0x37, 0x38, 0x39, 0x3A, 0x3B, 0x3C, /* 119 */
Tyler Hicks0f751e62011-11-23 11:31:24 -06001754 0x3D, 0x3E, 0x3F /* 123 - 255 initialized to 0x00 */
Michael Halcrow51ca58d2009-01-06 14:41:59 -08001755};
1756
1757/**
1758 * ecryptfs_encode_for_filename
1759 * @dst: Destination location for encoded filename
1760 * @dst_size: Size of the encoded filename in bytes
1761 * @src: Source location for the filename to encode
1762 * @src_size: Size of the source in bytes
1763 */
Cong Ding37028752012-12-07 22:21:56 +00001764static void ecryptfs_encode_for_filename(unsigned char *dst, size_t *dst_size,
Michael Halcrow51ca58d2009-01-06 14:41:59 -08001765 unsigned char *src, size_t src_size)
1766{
1767 size_t num_blocks;
1768 size_t block_num = 0;
1769 size_t dst_offset = 0;
1770 unsigned char last_block[3];
1771
1772 if (src_size == 0) {
1773 (*dst_size) = 0;
1774 goto out;
1775 }
1776 num_blocks = (src_size / 3);
1777 if ((src_size % 3) == 0) {
1778 memcpy(last_block, (&src[src_size - 3]), 3);
1779 } else {
1780 num_blocks++;
1781 last_block[2] = 0x00;
1782 switch (src_size % 3) {
1783 case 1:
1784 last_block[0] = src[src_size - 1];
1785 last_block[1] = 0x00;
1786 break;
1787 case 2:
1788 last_block[0] = src[src_size - 2];
1789 last_block[1] = src[src_size - 1];
1790 }
1791 }
1792 (*dst_size) = (num_blocks * 4);
1793 if (!dst)
1794 goto out;
1795 while (block_num < num_blocks) {
1796 unsigned char *src_block;
1797 unsigned char dst_block[4];
1798
1799 if (block_num == (num_blocks - 1))
1800 src_block = last_block;
1801 else
1802 src_block = &src[block_num * 3];
1803 dst_block[0] = ((src_block[0] >> 2) & 0x3F);
1804 dst_block[1] = (((src_block[0] << 4) & 0x30)
1805 | ((src_block[1] >> 4) & 0x0F));
1806 dst_block[2] = (((src_block[1] << 2) & 0x3C)
1807 | ((src_block[2] >> 6) & 0x03));
1808 dst_block[3] = (src_block[2] & 0x3F);
1809 dst[dst_offset++] = portable_filename_chars[dst_block[0]];
1810 dst[dst_offset++] = portable_filename_chars[dst_block[1]];
1811 dst[dst_offset++] = portable_filename_chars[dst_block[2]];
1812 dst[dst_offset++] = portable_filename_chars[dst_block[3]];
1813 block_num++;
1814 }
1815out:
1816 return;
1817}
1818
Tyler Hicks4a266202011-11-05 13:45:08 -04001819static size_t ecryptfs_max_decoded_size(size_t encoded_size)
1820{
1821 /* Not exact; conservatively long. Every block of 4
1822 * encoded characters decodes into a block of 3
1823 * decoded characters. This segment of code provides
1824 * the caller with the maximum amount of allocated
1825 * space that @dst will need to point to in a
1826 * subsequent call. */
1827 return ((encoded_size + 1) * 3) / 4;
1828}
1829
Michael Halcrow71c11c32009-01-06 14:42:05 -08001830/**
1831 * ecryptfs_decode_from_filename
1832 * @dst: If NULL, this function only sets @dst_size and returns. If
1833 * non-NULL, this function decodes the encoded octets in @src
1834 * into the memory that @dst points to.
1835 * @dst_size: Set to the size of the decoded string.
1836 * @src: The encoded set of octets to decode.
1837 * @src_size: The size of the encoded set of octets to decode.
1838 */
1839static void
1840ecryptfs_decode_from_filename(unsigned char *dst, size_t *dst_size,
1841 const unsigned char *src, size_t src_size)
Michael Halcrow51ca58d2009-01-06 14:41:59 -08001842{
1843 u8 current_bit_offset = 0;
1844 size_t src_byte_offset = 0;
1845 size_t dst_byte_offset = 0;
Michael Halcrow51ca58d2009-01-06 14:41:59 -08001846
Markus Elfring5032f362017-08-19 18:00:22 +02001847 if (!dst) {
Tyler Hicks4a266202011-11-05 13:45:08 -04001848 (*dst_size) = ecryptfs_max_decoded_size(src_size);
Michael Halcrow51ca58d2009-01-06 14:41:59 -08001849 goto out;
1850 }
1851 while (src_byte_offset < src_size) {
1852 unsigned char src_byte =
1853 filename_rev_map[(int)src[src_byte_offset]];
1854
1855 switch (current_bit_offset) {
1856 case 0:
1857 dst[dst_byte_offset] = (src_byte << 2);
1858 current_bit_offset = 6;
1859 break;
1860 case 6:
1861 dst[dst_byte_offset++] |= (src_byte >> 4);
1862 dst[dst_byte_offset] = ((src_byte & 0xF)
1863 << 4);
1864 current_bit_offset = 4;
1865 break;
1866 case 4:
1867 dst[dst_byte_offset++] |= (src_byte >> 2);
1868 dst[dst_byte_offset] = (src_byte << 6);
1869 current_bit_offset = 2;
1870 break;
1871 case 2:
1872 dst[dst_byte_offset++] |= (src_byte);
Michael Halcrow51ca58d2009-01-06 14:41:59 -08001873 current_bit_offset = 0;
1874 break;
1875 }
1876 src_byte_offset++;
1877 }
1878 (*dst_size) = dst_byte_offset;
1879out:
Michael Halcrow71c11c32009-01-06 14:42:05 -08001880 return;
Michael Halcrow51ca58d2009-01-06 14:41:59 -08001881}
1882
1883/**
1884 * ecryptfs_encrypt_and_encode_filename - converts a plaintext file name to cipher text
1885 * @crypt_stat: The crypt_stat struct associated with the file anem to encode
1886 * @name: The plaintext name
1887 * @length: The length of the plaintext
1888 * @encoded_name: The encypted name
1889 *
1890 * Encrypts and encodes a filename into something that constitutes a
1891 * valid filename for a filesystem, with printable characters.
1892 *
1893 * We assume that we have a properly initialized crypto context,
1894 * pointed to by crypt_stat->tfm.
1895 *
1896 * Returns zero on success; non-zero on otherwise
1897 */
1898int ecryptfs_encrypt_and_encode_filename(
1899 char **encoded_name,
1900 size_t *encoded_name_size,
Michael Halcrow51ca58d2009-01-06 14:41:59 -08001901 struct ecryptfs_mount_crypt_stat *mount_crypt_stat,
1902 const char *name, size_t name_size)
1903{
1904 size_t encoded_name_no_prefix_size;
1905 int rc = 0;
1906
1907 (*encoded_name) = NULL;
1908 (*encoded_name_size) = 0;
Al Viro97c31602016-02-22 18:14:25 -05001909 if (mount_crypt_stat && (mount_crypt_stat->flags
1910 & ECRYPTFS_GLOBAL_ENCRYPT_FILENAMES)) {
Michael Halcrow51ca58d2009-01-06 14:41:59 -08001911 struct ecryptfs_filename *filename;
1912
1913 filename = kzalloc(sizeof(*filename), GFP_KERNEL);
1914 if (!filename) {
Michael Halcrow51ca58d2009-01-06 14:41:59 -08001915 rc = -ENOMEM;
1916 goto out;
1917 }
1918 filename->filename = (char *)name;
1919 filename->filename_size = name_size;
Al Viro97c31602016-02-22 18:14:25 -05001920 rc = ecryptfs_encrypt_filename(filename, mount_crypt_stat);
Michael Halcrow51ca58d2009-01-06 14:41:59 -08001921 if (rc) {
1922 printk(KERN_ERR "%s: Error attempting to encrypt "
1923 "filename; rc = [%d]\n", __func__, rc);
1924 kfree(filename);
1925 goto out;
1926 }
1927 ecryptfs_encode_for_filename(
1928 NULL, &encoded_name_no_prefix_size,
1929 filename->encrypted_filename,
1930 filename->encrypted_filename_size);
Al Viro97c31602016-02-22 18:14:25 -05001931 if (mount_crypt_stat
Michael Halcrow51ca58d2009-01-06 14:41:59 -08001932 && (mount_crypt_stat->flags
Al Viro97c31602016-02-22 18:14:25 -05001933 & ECRYPTFS_GLOBAL_ENCFN_USE_MOUNT_FNEK))
Michael Halcrow51ca58d2009-01-06 14:41:59 -08001934 (*encoded_name_size) =
1935 (ECRYPTFS_FNEK_ENCRYPTED_FILENAME_PREFIX_SIZE
1936 + encoded_name_no_prefix_size);
1937 else
1938 (*encoded_name_size) =
1939 (ECRYPTFS_FEK_ENCRYPTED_FILENAME_PREFIX_SIZE
1940 + encoded_name_no_prefix_size);
1941 (*encoded_name) = kmalloc((*encoded_name_size) + 1, GFP_KERNEL);
1942 if (!(*encoded_name)) {
Michael Halcrow51ca58d2009-01-06 14:41:59 -08001943 rc = -ENOMEM;
1944 kfree(filename->encrypted_filename);
1945 kfree(filename);
1946 goto out;
1947 }
Al Viro97c31602016-02-22 18:14:25 -05001948 if (mount_crypt_stat
Michael Halcrow51ca58d2009-01-06 14:41:59 -08001949 && (mount_crypt_stat->flags
Al Viro97c31602016-02-22 18:14:25 -05001950 & ECRYPTFS_GLOBAL_ENCFN_USE_MOUNT_FNEK)) {
Michael Halcrow51ca58d2009-01-06 14:41:59 -08001951 memcpy((*encoded_name),
1952 ECRYPTFS_FNEK_ENCRYPTED_FILENAME_PREFIX,
1953 ECRYPTFS_FNEK_ENCRYPTED_FILENAME_PREFIX_SIZE);
1954 ecryptfs_encode_for_filename(
1955 ((*encoded_name)
1956 + ECRYPTFS_FNEK_ENCRYPTED_FILENAME_PREFIX_SIZE),
1957 &encoded_name_no_prefix_size,
1958 filename->encrypted_filename,
1959 filename->encrypted_filename_size);
1960 (*encoded_name_size) =
1961 (ECRYPTFS_FNEK_ENCRYPTED_FILENAME_PREFIX_SIZE
1962 + encoded_name_no_prefix_size);
1963 (*encoded_name)[(*encoded_name_size)] = '\0';
Michael Halcrow51ca58d2009-01-06 14:41:59 -08001964 } else {
Tyler Hicksdf6ad332009-08-21 04:27:46 -05001965 rc = -EOPNOTSUPP;
Michael Halcrow51ca58d2009-01-06 14:41:59 -08001966 }
1967 if (rc) {
1968 printk(KERN_ERR "%s: Error attempting to encode "
1969 "encrypted filename; rc = [%d]\n", __func__,
1970 rc);
1971 kfree((*encoded_name));
1972 (*encoded_name) = NULL;
1973 (*encoded_name_size) = 0;
1974 }
1975 kfree(filename->encrypted_filename);
1976 kfree(filename);
1977 } else {
1978 rc = ecryptfs_copy_filename(encoded_name,
1979 encoded_name_size,
1980 name, name_size);
1981 }
1982out:
1983 return rc;
1984}
1985
Tyler Hickse86281e2018-03-28 23:41:52 +00001986static bool is_dot_dotdot(const char *name, size_t name_size)
1987{
1988 if (name_size == 1 && name[0] == '.')
1989 return true;
1990 else if (name_size == 2 && name[0] == '.' && name[1] == '.')
1991 return true;
1992
1993 return false;
1994}
1995
Michael Halcrow51ca58d2009-01-06 14:41:59 -08001996/**
1997 * ecryptfs_decode_and_decrypt_filename - converts the encoded cipher text name to decoded plaintext
1998 * @plaintext_name: The plaintext name
1999 * @plaintext_name_size: The plaintext name size
2000 * @ecryptfs_dir_dentry: eCryptfs directory dentry
2001 * @name: The filename in cipher text
2002 * @name_size: The cipher text name size
2003 *
2004 * Decrypts and decodes the filename.
2005 *
2006 * Returns zero on error; non-zero otherwise
2007 */
2008int ecryptfs_decode_and_decrypt_filename(char **plaintext_name,
2009 size_t *plaintext_name_size,
Al Viro0747fdb2013-06-16 20:05:38 +04002010 struct super_block *sb,
Michael Halcrow51ca58d2009-01-06 14:41:59 -08002011 const char *name, size_t name_size)
2012{
Tyler Hicks2aac0cf2009-03-20 02:23:57 -05002013 struct ecryptfs_mount_crypt_stat *mount_crypt_stat =
Al Viro0747fdb2013-06-16 20:05:38 +04002014 &ecryptfs_superblock_to_private(sb)->mount_crypt_stat;
Michael Halcrow51ca58d2009-01-06 14:41:59 -08002015 char *decoded_name;
2016 size_t decoded_name_size;
2017 size_t packet_size;
2018 int rc = 0;
2019
Tyler Hickse86281e2018-03-28 23:41:52 +00002020 if ((mount_crypt_stat->flags & ECRYPTFS_GLOBAL_ENCRYPT_FILENAMES) &&
2021 !(mount_crypt_stat->flags & ECRYPTFS_ENCRYPTED_VIEW_ENABLED)) {
2022 if (is_dot_dotdot(name, name_size)) {
2023 rc = ecryptfs_copy_filename(plaintext_name,
2024 plaintext_name_size,
2025 name, name_size);
2026 goto out;
2027 }
2028
2029 if (name_size <= ECRYPTFS_FNEK_ENCRYPTED_FILENAME_PREFIX_SIZE ||
2030 strncmp(name, ECRYPTFS_FNEK_ENCRYPTED_FILENAME_PREFIX,
2031 ECRYPTFS_FNEK_ENCRYPTED_FILENAME_PREFIX_SIZE)) {
2032 rc = -EINVAL;
2033 goto out;
2034 }
Michael Halcrow51ca58d2009-01-06 14:41:59 -08002035
2036 name += ECRYPTFS_FNEK_ENCRYPTED_FILENAME_PREFIX_SIZE;
2037 name_size -= ECRYPTFS_FNEK_ENCRYPTED_FILENAME_PREFIX_SIZE;
Michael Halcrow71c11c32009-01-06 14:42:05 -08002038 ecryptfs_decode_from_filename(NULL, &decoded_name_size,
2039 name, name_size);
Michael Halcrow51ca58d2009-01-06 14:41:59 -08002040 decoded_name = kmalloc(decoded_name_size, GFP_KERNEL);
2041 if (!decoded_name) {
Michael Halcrow51ca58d2009-01-06 14:41:59 -08002042 rc = -ENOMEM;
2043 goto out;
2044 }
Michael Halcrow71c11c32009-01-06 14:42:05 -08002045 ecryptfs_decode_from_filename(decoded_name, &decoded_name_size,
2046 name, name_size);
Michael Halcrow51ca58d2009-01-06 14:41:59 -08002047 rc = ecryptfs_parse_tag_70_packet(plaintext_name,
2048 plaintext_name_size,
2049 &packet_size,
2050 mount_crypt_stat,
2051 decoded_name,
2052 decoded_name_size);
2053 if (rc) {
Tyler Hickse86281e2018-03-28 23:41:52 +00002054 ecryptfs_printk(KERN_DEBUG,
2055 "%s: Could not parse tag 70 packet from filename\n",
2056 __func__);
Michael Halcrow51ca58d2009-01-06 14:41:59 -08002057 goto out_free;
2058 }
2059 } else {
2060 rc = ecryptfs_copy_filename(plaintext_name,
2061 plaintext_name_size,
2062 name, name_size);
2063 goto out;
2064 }
2065out_free:
2066 kfree(decoded_name);
2067out:
2068 return rc;
2069}
Tyler Hicks4a266202011-11-05 13:45:08 -04002070
2071#define ENC_NAME_MAX_BLOCKLEN_8_OR_16 143
2072
2073int ecryptfs_set_f_namelen(long *namelen, long lower_namelen,
2074 struct ecryptfs_mount_crypt_stat *mount_crypt_stat)
2075{
Herbert Xu3095e8e2016-01-25 10:29:33 +08002076 struct crypto_skcipher *tfm;
Tyler Hicks4a266202011-11-05 13:45:08 -04002077 struct mutex *tfm_mutex;
2078 size_t cipher_blocksize;
2079 int rc;
2080
2081 if (!(mount_crypt_stat->flags & ECRYPTFS_GLOBAL_ENCRYPT_FILENAMES)) {
2082 (*namelen) = lower_namelen;
2083 return 0;
2084 }
2085
Herbert Xu3095e8e2016-01-25 10:29:33 +08002086 rc = ecryptfs_get_tfm_and_mutex_for_cipher_name(&tfm, &tfm_mutex,
Tyler Hicks4a266202011-11-05 13:45:08 -04002087 mount_crypt_stat->global_default_fn_cipher_name);
2088 if (unlikely(rc)) {
2089 (*namelen) = 0;
2090 return rc;
2091 }
2092
2093 mutex_lock(tfm_mutex);
Herbert Xu3095e8e2016-01-25 10:29:33 +08002094 cipher_blocksize = crypto_skcipher_blocksize(tfm);
Tyler Hicks4a266202011-11-05 13:45:08 -04002095 mutex_unlock(tfm_mutex);
2096
2097 /* Return an exact amount for the common cases */
2098 if (lower_namelen == NAME_MAX
2099 && (cipher_blocksize == 8 || cipher_blocksize == 16)) {
2100 (*namelen) = ENC_NAME_MAX_BLOCKLEN_8_OR_16;
2101 return 0;
2102 }
2103
2104 /* Return a safe estimate for the uncommon cases */
2105 (*namelen) = lower_namelen;
2106 (*namelen) -= ECRYPTFS_FNEK_ENCRYPTED_FILENAME_PREFIX_SIZE;
2107 /* Since this is the max decoded size, subtract 1 "decoded block" len */
2108 (*namelen) = ecryptfs_max_decoded_size(*namelen) - 3;
2109 (*namelen) -= ECRYPTFS_TAG_70_MAX_METADATA_SIZE;
2110 (*namelen) -= ECRYPTFS_FILENAME_MIN_RANDOM_PREPEND_BYTES;
2111 /* Worst case is that the filename is padded nearly a full block size */
2112 (*namelen) -= cipher_blocksize - 1;
2113
2114 if ((*namelen) < 0)
2115 (*namelen) = 0;
2116
2117 return 0;
2118}