blob: 23d7f1d56b007ea334bfcd044a577a4b9179876d [file] [log] [blame]
Michael Halcrowd5d0e8c2015-04-12 00:56:17 -04001/*
2 * linux/fs/ext4/crypto_fname.c
3 *
4 * Copyright (C) 2015, Google, Inc.
5 *
6 * This contains functions for filename crypto management in ext4
7 *
8 * Written by Uday Savagaonkar, 2014.
9 *
10 * This has not yet undergone a rigorous security audit.
11 *
12 */
13
14#include <crypto/hash.h>
15#include <crypto/sha.h>
16#include <keys/encrypted-type.h>
17#include <keys/user-type.h>
18#include <linux/crypto.h>
19#include <linux/gfp.h>
20#include <linux/kernel.h>
21#include <linux/key.h>
22#include <linux/key.h>
23#include <linux/list.h>
24#include <linux/mempool.h>
25#include <linux/random.h>
26#include <linux/scatterlist.h>
27#include <linux/spinlock_types.h>
28
29#include "ext4.h"
30#include "ext4_crypto.h"
31#include "xattr.h"
32
33/**
34 * ext4_dir_crypt_complete() -
35 */
36static void ext4_dir_crypt_complete(struct crypto_async_request *req, int res)
37{
38 struct ext4_completion_result *ecr = req->data;
39
40 if (res == -EINPROGRESS)
41 return;
42 ecr->res = res;
43 complete(&ecr->completion);
44}
45
46bool ext4_valid_filenames_enc_mode(uint32_t mode)
47{
48 return (mode == EXT4_ENCRYPTION_MODE_AES_256_CTS);
49}
50
51/**
52 * ext4_fname_encrypt() -
53 *
54 * This function encrypts the input filename, and returns the length of the
55 * ciphertext. Errors are returned as negative numbers. We trust the caller to
56 * allocate sufficient memory to oname string.
57 */
58static int ext4_fname_encrypt(struct ext4_fname_crypto_ctx *ctx,
59 const struct qstr *iname,
60 struct ext4_str *oname)
61{
62 u32 ciphertext_len;
63 struct ablkcipher_request *req = NULL;
64 DECLARE_EXT4_COMPLETION_RESULT(ecr);
65 struct crypto_ablkcipher *tfm = ctx->ctfm;
66 int res = 0;
67 char iv[EXT4_CRYPTO_BLOCK_SIZE];
Theodore Ts'od2299592015-05-18 13:15:47 -040068 struct scatterlist src_sg, dst_sg;
Theodore Ts'oa44cd7a2015-05-01 16:56:50 -040069 int padding = 4 << (ctx->flags & EXT4_POLICY_FLAGS_PAD_MASK);
Theodore Ts'od2299592015-05-18 13:15:47 -040070 char *workbuf, buf[32], *alloc_buf = NULL;
Michael Halcrowd5d0e8c2015-04-12 00:56:17 -040071
72 if (iname->len <= 0 || iname->len > ctx->lim)
73 return -EIO;
74
75 ciphertext_len = (iname->len < EXT4_CRYPTO_BLOCK_SIZE) ?
76 EXT4_CRYPTO_BLOCK_SIZE : iname->len;
Theodore Ts'oa44cd7a2015-05-01 16:56:50 -040077 ciphertext_len = ext4_fname_crypto_round_up(ciphertext_len, padding);
Michael Halcrowd5d0e8c2015-04-12 00:56:17 -040078 ciphertext_len = (ciphertext_len > ctx->lim)
79 ? ctx->lim : ciphertext_len;
80
Theodore Ts'od2299592015-05-18 13:15:47 -040081 if (ciphertext_len <= sizeof(buf)) {
82 workbuf = buf;
83 } else {
84 alloc_buf = kmalloc(ciphertext_len, GFP_NOFS);
85 if (!alloc_buf)
86 return -ENOMEM;
87 workbuf = alloc_buf;
88 }
89
Michael Halcrowd5d0e8c2015-04-12 00:56:17 -040090 /* Allocate request */
91 req = ablkcipher_request_alloc(tfm, GFP_NOFS);
92 if (!req) {
93 printk_ratelimited(
94 KERN_ERR "%s: crypto_request_alloc() failed\n", __func__);
Theodore Ts'od2299592015-05-18 13:15:47 -040095 kfree(alloc_buf);
Michael Halcrowd5d0e8c2015-04-12 00:56:17 -040096 return -ENOMEM;
97 }
98 ablkcipher_request_set_callback(req,
99 CRYPTO_TFM_REQ_MAY_BACKLOG | CRYPTO_TFM_REQ_MAY_SLEEP,
100 ext4_dir_crypt_complete, &ecr);
101
Michael Halcrowd5d0e8c2015-04-12 00:56:17 -0400102 /* Copy the input */
103 memcpy(workbuf, iname->name, iname->len);
104 if (iname->len < ciphertext_len)
105 memset(workbuf + iname->len, 0, ciphertext_len - iname->len);
106
107 /* Initialize IV */
108 memset(iv, 0, EXT4_CRYPTO_BLOCK_SIZE);
109
110 /* Create encryption request */
Theodore Ts'od2299592015-05-18 13:15:47 -0400111 sg_init_one(&src_sg, workbuf, ciphertext_len);
112 sg_init_one(&dst_sg, oname->name, ciphertext_len);
113 ablkcipher_request_set_crypt(req, &src_sg, &dst_sg, ciphertext_len, iv);
Michael Halcrowd5d0e8c2015-04-12 00:56:17 -0400114 res = crypto_ablkcipher_encrypt(req);
115 if (res == -EINPROGRESS || res == -EBUSY) {
116 BUG_ON(req->base.data != &ecr);
117 wait_for_completion(&ecr.completion);
118 res = ecr.res;
119 }
Theodore Ts'od2299592015-05-18 13:15:47 -0400120 kfree(alloc_buf);
Michael Halcrowd5d0e8c2015-04-12 00:56:17 -0400121 ablkcipher_request_free(req);
122 if (res < 0) {
123 printk_ratelimited(
124 KERN_ERR "%s: Error (error code %d)\n", __func__, res);
125 }
126 oname->len = ciphertext_len;
127 return res;
128}
129
130/*
131 * ext4_fname_decrypt()
132 * This function decrypts the input filename, and returns
133 * the length of the plaintext.
134 * Errors are returned as negative numbers.
135 * We trust the caller to allocate sufficient memory to oname string.
136 */
137static int ext4_fname_decrypt(struct ext4_fname_crypto_ctx *ctx,
138 const struct ext4_str *iname,
139 struct ext4_str *oname)
140{
141 struct ext4_str tmp_in[2], tmp_out[1];
142 struct ablkcipher_request *req = NULL;
143 DECLARE_EXT4_COMPLETION_RESULT(ecr);
Theodore Ts'od2299592015-05-18 13:15:47 -0400144 struct scatterlist src_sg, dst_sg;
Michael Halcrowd5d0e8c2015-04-12 00:56:17 -0400145 struct crypto_ablkcipher *tfm = ctx->ctfm;
146 int res = 0;
147 char iv[EXT4_CRYPTO_BLOCK_SIZE];
Michael Halcrowd5d0e8c2015-04-12 00:56:17 -0400148
149 if (iname->len <= 0 || iname->len > ctx->lim)
150 return -EIO;
151
152 tmp_in[0].name = iname->name;
153 tmp_in[0].len = iname->len;
154 tmp_out[0].name = oname->name;
155
156 /* Allocate request */
157 req = ablkcipher_request_alloc(tfm, GFP_NOFS);
158 if (!req) {
159 printk_ratelimited(
160 KERN_ERR "%s: crypto_request_alloc() failed\n", __func__);
161 return -ENOMEM;
162 }
163 ablkcipher_request_set_callback(req,
164 CRYPTO_TFM_REQ_MAY_BACKLOG | CRYPTO_TFM_REQ_MAY_SLEEP,
165 ext4_dir_crypt_complete, &ecr);
166
Michael Halcrowd5d0e8c2015-04-12 00:56:17 -0400167 /* Initialize IV */
168 memset(iv, 0, EXT4_CRYPTO_BLOCK_SIZE);
169
170 /* Create encryption request */
Theodore Ts'od2299592015-05-18 13:15:47 -0400171 sg_init_one(&src_sg, iname->name, iname->len);
172 sg_init_one(&dst_sg, oname->name, oname->len);
173 ablkcipher_request_set_crypt(req, &src_sg, &dst_sg, iname->len, iv);
Michael Halcrowd5d0e8c2015-04-12 00:56:17 -0400174 res = crypto_ablkcipher_decrypt(req);
175 if (res == -EINPROGRESS || res == -EBUSY) {
176 BUG_ON(req->base.data != &ecr);
177 wait_for_completion(&ecr.completion);
178 res = ecr.res;
179 }
Michael Halcrowd5d0e8c2015-04-12 00:56:17 -0400180 ablkcipher_request_free(req);
181 if (res < 0) {
182 printk_ratelimited(
183 KERN_ERR "%s: Error in ext4_fname_encrypt (error code %d)\n",
184 __func__, res);
185 return res;
186 }
187
188 oname->len = strnlen(oname->name, iname->len);
189 return oname->len;
190}
191
Theodore Ts'o5de0b4d2015-05-01 16:56:45 -0400192static const char *lookup_table =
193 "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+,";
194
Michael Halcrowd5d0e8c2015-04-12 00:56:17 -0400195/**
196 * ext4_fname_encode_digest() -
197 *
198 * Encodes the input digest using characters from the set [a-zA-Z0-9_+].
199 * The encoded string is roughly 4/3 times the size of the input string.
200 */
Theodore Ts'o5de0b4d2015-05-01 16:56:45 -0400201static int digest_encode(const char *src, int len, char *dst)
Michael Halcrowd5d0e8c2015-04-12 00:56:17 -0400202{
Theodore Ts'o5de0b4d2015-05-01 16:56:45 -0400203 int i = 0, bits = 0, ac = 0;
204 char *cp = dst;
Michael Halcrowd5d0e8c2015-04-12 00:56:17 -0400205
Theodore Ts'o5de0b4d2015-05-01 16:56:45 -0400206 while (i < len) {
207 ac += (((unsigned char) src[i]) << bits);
208 bits += 8;
209 do {
210 *cp++ = lookup_table[ac & 0x3f];
211 ac >>= 6;
212 bits -= 6;
213 } while (bits >= 6);
Michael Halcrowd5d0e8c2015-04-12 00:56:17 -0400214 i++;
215 }
Theodore Ts'o5de0b4d2015-05-01 16:56:45 -0400216 if (bits)
217 *cp++ = lookup_table[ac & 0x3f];
218 return cp - dst;
Michael Halcrowd5d0e8c2015-04-12 00:56:17 -0400219}
220
Theodore Ts'o5de0b4d2015-05-01 16:56:45 -0400221static int digest_decode(const char *src, int len, char *dst)
Michael Halcrowd5d0e8c2015-04-12 00:56:17 -0400222{
Theodore Ts'o5de0b4d2015-05-01 16:56:45 -0400223 int i = 0, bits = 0, ac = 0;
224 const char *p;
225 char *cp = dst;
Michael Halcrowd5d0e8c2015-04-12 00:56:17 -0400226
Theodore Ts'o5de0b4d2015-05-01 16:56:45 -0400227 while (i < len) {
228 p = strchr(lookup_table, src[i]);
229 if (p == NULL || src[i] == 0)
230 return -2;
231 ac += (p - lookup_table) << bits;
232 bits += 6;
233 if (bits >= 8) {
234 *cp++ = ac & 0xff;
235 ac >>= 8;
236 bits -= 8;
237 }
238 i++;
Michael Halcrowd5d0e8c2015-04-12 00:56:17 -0400239 }
Theodore Ts'o5de0b4d2015-05-01 16:56:45 -0400240 if (ac)
241 return -1;
242 return cp - dst;
Michael Halcrowd5d0e8c2015-04-12 00:56:17 -0400243}
244
245/**
246 * ext4_free_fname_crypto_ctx() -
247 *
248 * Frees up a crypto context.
249 */
250void ext4_free_fname_crypto_ctx(struct ext4_fname_crypto_ctx *ctx)
251{
252 if (ctx == NULL || IS_ERR(ctx))
253 return;
254
255 if (ctx->ctfm && !IS_ERR(ctx->ctfm))
256 crypto_free_ablkcipher(ctx->ctfm);
257 if (ctx->htfm && !IS_ERR(ctx->htfm))
258 crypto_free_hash(ctx->htfm);
Michael Halcrowd5d0e8c2015-04-12 00:56:17 -0400259 kfree(ctx);
260}
261
262/**
263 * ext4_put_fname_crypto_ctx() -
264 *
265 * Return: The crypto context onto free list. If the free list is above a
266 * threshold, completely frees up the context, and returns the memory.
267 *
268 * TODO: Currently we directly free the crypto context. Eventually we should
269 * add code it to return to free list. Such an approach will increase
270 * efficiency of directory lookup.
271 */
272void ext4_put_fname_crypto_ctx(struct ext4_fname_crypto_ctx **ctx)
273{
274 if (*ctx == NULL || IS_ERR(*ctx))
275 return;
276 ext4_free_fname_crypto_ctx(*ctx);
277 *ctx = NULL;
278}
279
280/**
281 * ext4_search_fname_crypto_ctx() -
282 */
283static struct ext4_fname_crypto_ctx *ext4_search_fname_crypto_ctx(
284 const struct ext4_encryption_key *key)
285{
286 return NULL;
287}
288
289/**
290 * ext4_alloc_fname_crypto_ctx() -
291 */
292struct ext4_fname_crypto_ctx *ext4_alloc_fname_crypto_ctx(
293 const struct ext4_encryption_key *key)
294{
295 struct ext4_fname_crypto_ctx *ctx;
296
297 ctx = kmalloc(sizeof(struct ext4_fname_crypto_ctx), GFP_NOFS);
298 if (ctx == NULL)
299 return ERR_PTR(-ENOMEM);
300 if (key->mode == EXT4_ENCRYPTION_MODE_INVALID) {
301 /* This will automatically set key mode to invalid
302 * As enum for ENCRYPTION_MODE_INVALID is zero */
303 memset(&ctx->key, 0, sizeof(ctx->key));
304 } else {
305 memcpy(&ctx->key, key, sizeof(struct ext4_encryption_key));
306 }
307 ctx->has_valid_key = (EXT4_ENCRYPTION_MODE_INVALID == key->mode)
308 ? 0 : 1;
309 ctx->ctfm_key_is_ready = 0;
310 ctx->ctfm = NULL;
311 ctx->htfm = NULL;
Michael Halcrowd5d0e8c2015-04-12 00:56:17 -0400312 return ctx;
313}
314
315/**
316 * ext4_get_fname_crypto_ctx() -
317 *
318 * Allocates a free crypto context and initializes it to hold
319 * the crypto material for the inode.
320 *
321 * Return: NULL if not encrypted. Error value on error. Valid pointer otherwise.
322 */
323struct ext4_fname_crypto_ctx *ext4_get_fname_crypto_ctx(
324 struct inode *inode, u32 max_ciphertext_len)
325{
326 struct ext4_fname_crypto_ctx *ctx;
327 struct ext4_inode_info *ei = EXT4_I(inode);
328 int res;
329
330 /* Check if the crypto policy is set on the inode */
331 res = ext4_encrypted_inode(inode);
332 if (res == 0)
333 return NULL;
334
335 if (!ext4_has_encryption_key(inode))
336 ext4_generate_encryption_key(inode);
337
338 /* Get a crypto context based on the key.
339 * A new context is allocated if no context matches the requested key.
340 */
341 ctx = ext4_search_fname_crypto_ctx(&(ei->i_encryption_key));
342 if (ctx == NULL)
343 ctx = ext4_alloc_fname_crypto_ctx(&(ei->i_encryption_key));
344 if (IS_ERR(ctx))
345 return ctx;
346
Theodore Ts'oa44cd7a2015-05-01 16:56:50 -0400347 ctx->flags = ei->i_crypt_policy_flags;
Michael Halcrowd5d0e8c2015-04-12 00:56:17 -0400348 if (ctx->has_valid_key) {
349 if (ctx->key.mode != EXT4_ENCRYPTION_MODE_AES_256_CTS) {
350 printk_once(KERN_WARNING
351 "ext4: unsupported key mode %d\n",
352 ctx->key.mode);
353 return ERR_PTR(-ENOKEY);
354 }
355
356 /* As a first cut, we will allocate new tfm in every call.
357 * later, we will keep the tfm around, in case the key gets
358 * re-used */
359 if (ctx->ctfm == NULL) {
360 ctx->ctfm = crypto_alloc_ablkcipher("cts(cbc(aes))",
361 0, 0);
362 }
363 if (IS_ERR(ctx->ctfm)) {
364 res = PTR_ERR(ctx->ctfm);
365 printk(
366 KERN_DEBUG "%s: error (%d) allocating crypto tfm\n",
367 __func__, res);
368 ctx->ctfm = NULL;
369 ext4_put_fname_crypto_ctx(&ctx);
370 return ERR_PTR(res);
371 }
372 if (ctx->ctfm == NULL) {
373 printk(
374 KERN_DEBUG "%s: could not allocate crypto tfm\n",
375 __func__);
376 ext4_put_fname_crypto_ctx(&ctx);
377 return ERR_PTR(-ENOMEM);
378 }
Michael Halcrowd5d0e8c2015-04-12 00:56:17 -0400379 ctx->lim = max_ciphertext_len;
380 crypto_ablkcipher_clear_flags(ctx->ctfm, ~0);
381 crypto_tfm_set_flags(crypto_ablkcipher_tfm(ctx->ctfm),
382 CRYPTO_TFM_REQ_WEAK_KEY);
383
384 /* If we are lucky, we will get a context that is already
385 * set up with the right key. Else, we will have to
386 * set the key */
387 if (!ctx->ctfm_key_is_ready) {
388 /* Since our crypto objectives for filename encryption
389 * are pretty weak,
390 * we directly use the inode master key */
391 res = crypto_ablkcipher_setkey(ctx->ctfm,
392 ctx->key.raw, ctx->key.size);
393 if (res) {
394 ext4_put_fname_crypto_ctx(&ctx);
395 return ERR_PTR(-EIO);
396 }
397 ctx->ctfm_key_is_ready = 1;
398 } else {
399 /* In the current implementation, key should never be
400 * marked "ready" for a context that has just been
401 * allocated. So we should never reach here */
402 BUG();
403 }
404 }
405 if (ctx->htfm == NULL)
406 ctx->htfm = crypto_alloc_hash("sha256", 0, CRYPTO_ALG_ASYNC);
407 if (IS_ERR(ctx->htfm)) {
408 res = PTR_ERR(ctx->htfm);
409 printk(KERN_DEBUG "%s: error (%d) allocating hash tfm\n",
410 __func__, res);
411 ctx->htfm = NULL;
412 ext4_put_fname_crypto_ctx(&ctx);
413 return ERR_PTR(res);
414 }
415 if (ctx->htfm == NULL) {
416 printk(KERN_DEBUG "%s: could not allocate hash tfm\n",
417 __func__);
418 ext4_put_fname_crypto_ctx(&ctx);
419 return ERR_PTR(-ENOMEM);
420 }
421
422 return ctx;
423}
424
425/**
426 * ext4_fname_crypto_round_up() -
427 *
428 * Return: The next multiple of block size
429 */
430u32 ext4_fname_crypto_round_up(u32 size, u32 blksize)
431{
432 return ((size+blksize-1)/blksize)*blksize;
433}
434
435/**
436 * ext4_fname_crypto_namelen_on_disk() -
437 */
438int ext4_fname_crypto_namelen_on_disk(struct ext4_fname_crypto_ctx *ctx,
439 u32 namelen)
440{
441 u32 ciphertext_len;
Theodore Ts'oa44cd7a2015-05-01 16:56:50 -0400442 int padding = 4 << (ctx->flags & EXT4_POLICY_FLAGS_PAD_MASK);
Michael Halcrowd5d0e8c2015-04-12 00:56:17 -0400443
444 if (ctx == NULL)
445 return -EIO;
446 if (!(ctx->has_valid_key))
447 return -EACCES;
448 ciphertext_len = (namelen < EXT4_CRYPTO_BLOCK_SIZE) ?
449 EXT4_CRYPTO_BLOCK_SIZE : namelen;
Theodore Ts'oa44cd7a2015-05-01 16:56:50 -0400450 ciphertext_len = ext4_fname_crypto_round_up(ciphertext_len, padding);
Michael Halcrowd5d0e8c2015-04-12 00:56:17 -0400451 ciphertext_len = (ciphertext_len > ctx->lim)
452 ? ctx->lim : ciphertext_len;
453 return (int) ciphertext_len;
454}
455
456/**
457 * ext4_fname_crypto_alloc_obuff() -
458 *
459 * Allocates an output buffer that is sufficient for the crypto operation
460 * specified by the context and the direction.
461 */
462int ext4_fname_crypto_alloc_buffer(struct ext4_fname_crypto_ctx *ctx,
463 u32 ilen, struct ext4_str *crypto_str)
464{
465 unsigned int olen;
Theodore Ts'oa44cd7a2015-05-01 16:56:50 -0400466 int padding = 4 << (ctx->flags & EXT4_POLICY_FLAGS_PAD_MASK);
Michael Halcrowd5d0e8c2015-04-12 00:56:17 -0400467
468 if (!ctx)
469 return -EIO;
Theodore Ts'oa44cd7a2015-05-01 16:56:50 -0400470 if (padding < EXT4_CRYPTO_BLOCK_SIZE)
471 padding = EXT4_CRYPTO_BLOCK_SIZE;
472 olen = ext4_fname_crypto_round_up(ilen, padding);
Michael Halcrowd5d0e8c2015-04-12 00:56:17 -0400473 crypto_str->len = olen;
474 if (olen < EXT4_FNAME_CRYPTO_DIGEST_SIZE*2)
475 olen = EXT4_FNAME_CRYPTO_DIGEST_SIZE*2;
476 /* Allocated buffer can hold one more character to null-terminate the
477 * string */
478 crypto_str->name = kmalloc(olen+1, GFP_NOFS);
479 if (!(crypto_str->name))
480 return -ENOMEM;
481 return 0;
482}
483
484/**
485 * ext4_fname_crypto_free_buffer() -
486 *
487 * Frees the buffer allocated for crypto operation.
488 */
489void ext4_fname_crypto_free_buffer(struct ext4_str *crypto_str)
490{
491 if (!crypto_str)
492 return;
493 kfree(crypto_str->name);
494 crypto_str->name = NULL;
495}
496
497/**
498 * ext4_fname_disk_to_usr() - converts a filename from disk space to user space
499 */
500int _ext4_fname_disk_to_usr(struct ext4_fname_crypto_ctx *ctx,
Theodore Ts'o5de0b4d2015-05-01 16:56:45 -0400501 struct dx_hash_info *hinfo,
502 const struct ext4_str *iname,
503 struct ext4_str *oname)
Michael Halcrowd5d0e8c2015-04-12 00:56:17 -0400504{
Theodore Ts'o5de0b4d2015-05-01 16:56:45 -0400505 char buf[24];
506 int ret;
507
Michael Halcrowd5d0e8c2015-04-12 00:56:17 -0400508 if (ctx == NULL)
509 return -EIO;
510 if (iname->len < 3) {
511 /*Check for . and .. */
512 if (iname->name[0] == '.' && iname->name[iname->len-1] == '.') {
513 oname->name[0] = '.';
514 oname->name[iname->len-1] = '.';
515 oname->len = iname->len;
516 return oname->len;
517 }
518 }
519 if (ctx->has_valid_key)
520 return ext4_fname_decrypt(ctx, iname, oname);
Theodore Ts'o5de0b4d2015-05-01 16:56:45 -0400521
522 if (iname->len <= EXT4_FNAME_CRYPTO_DIGEST_SIZE) {
523 ret = digest_encode(iname->name, iname->len, oname->name);
524 oname->len = ret;
525 return ret;
526 }
527 if (hinfo) {
528 memcpy(buf, &hinfo->hash, 4);
529 memcpy(buf+4, &hinfo->minor_hash, 4);
530 } else
531 memset(buf, 0, 8);
532 memcpy(buf + 8, iname->name + iname->len - 16, 16);
533 oname->name[0] = '_';
534 ret = digest_encode(buf, 24, oname->name+1);
535 oname->len = ret + 1;
536 return ret + 1;
Michael Halcrowd5d0e8c2015-04-12 00:56:17 -0400537}
538
539int ext4_fname_disk_to_usr(struct ext4_fname_crypto_ctx *ctx,
Theodore Ts'o5de0b4d2015-05-01 16:56:45 -0400540 struct dx_hash_info *hinfo,
Michael Halcrowd5d0e8c2015-04-12 00:56:17 -0400541 const struct ext4_dir_entry_2 *de,
542 struct ext4_str *oname)
543{
544 struct ext4_str iname = {.name = (unsigned char *) de->name,
545 .len = de->name_len };
546
Theodore Ts'o5de0b4d2015-05-01 16:56:45 -0400547 return _ext4_fname_disk_to_usr(ctx, hinfo, &iname, oname);
Michael Halcrowd5d0e8c2015-04-12 00:56:17 -0400548}
549
550
551/**
552 * ext4_fname_usr_to_disk() - converts a filename from user space to disk space
553 */
554int ext4_fname_usr_to_disk(struct ext4_fname_crypto_ctx *ctx,
555 const struct qstr *iname,
556 struct ext4_str *oname)
557{
558 int res;
559
560 if (ctx == NULL)
561 return -EIO;
562 if (iname->len < 3) {
563 /*Check for . and .. */
564 if (iname->name[0] == '.' &&
565 iname->name[iname->len-1] == '.') {
566 oname->name[0] = '.';
567 oname->name[iname->len-1] = '.';
568 oname->len = iname->len;
569 return oname->len;
570 }
571 }
572 if (ctx->has_valid_key) {
573 res = ext4_fname_encrypt(ctx, iname, oname);
574 return res;
575 }
576 /* Without a proper key, a user is not allowed to modify the filenames
577 * in a directory. Consequently, a user space name cannot be mapped to
578 * a disk-space name */
579 return -EACCES;
580}
581
Theodore Ts'o5b643f92015-05-18 13:14:47 -0400582int ext4_fname_setup_filename(struct inode *dir, const struct qstr *iname,
583 int lookup, struct ext4_filename *fname)
Michael Halcrowd5d0e8c2015-04-12 00:56:17 -0400584{
Theodore Ts'o5b643f92015-05-18 13:14:47 -0400585 struct ext4_fname_crypto_ctx *ctx;
586 int ret = 0, bigname = 0;
Michael Halcrowd5d0e8c2015-04-12 00:56:17 -0400587
Theodore Ts'o5b643f92015-05-18 13:14:47 -0400588 memset(fname, 0, sizeof(struct ext4_filename));
589 fname->usr_fname = iname;
590
591 ctx = ext4_get_fname_crypto_ctx(dir, EXT4_NAME_LEN);
592 if (IS_ERR(ctx))
593 return PTR_ERR(ctx);
594 if ((ctx == NULL) ||
Michael Halcrowd5d0e8c2015-04-12 00:56:17 -0400595 ((iname->name[0] == '.') &&
596 ((iname->len == 1) ||
597 ((iname->name[1] == '.') && (iname->len == 2))))) {
Theodore Ts'o5b643f92015-05-18 13:14:47 -0400598 fname->disk_name.name = (unsigned char *) iname->name;
599 fname->disk_name.len = iname->len;
600 goto out;
Michael Halcrowd5d0e8c2015-04-12 00:56:17 -0400601 }
Theodore Ts'o5b643f92015-05-18 13:14:47 -0400602 if (ctx->has_valid_key) {
603 ret = ext4_fname_crypto_alloc_buffer(ctx, iname->len,
604 &fname->crypto_buf);
605 if (ret < 0)
606 goto out;
607 ret = ext4_fname_encrypt(ctx, iname, &fname->crypto_buf);
608 if (ret < 0)
609 goto out;
610 fname->disk_name.name = fname->crypto_buf.name;
611 fname->disk_name.len = fname->crypto_buf.len;
Theodore Ts'o5de0b4d2015-05-01 16:56:45 -0400612 ret = 0;
Theodore Ts'o5b643f92015-05-18 13:14:47 -0400613 goto out;
614 }
615 if (!lookup) {
616 ret = -EACCES;
617 goto out;
Michael Halcrowd5d0e8c2015-04-12 00:56:17 -0400618 }
619
Theodore Ts'o5b643f92015-05-18 13:14:47 -0400620 /* We don't have the key and we are doing a lookup; decode the
621 * user-supplied name
622 */
623 if (iname->name[0] == '_')
624 bigname = 1;
625 if ((bigname && (iname->len != 33)) ||
626 (!bigname && (iname->len > 43))) {
627 ret = -ENOENT;
628 }
629 fname->crypto_buf.name = kmalloc(32, GFP_KERNEL);
630 if (fname->crypto_buf.name == NULL) {
631 ret = -ENOMEM;
632 goto out;
633 }
634 ret = digest_decode(iname->name + bigname, iname->len - bigname,
635 fname->crypto_buf.name);
636 if (ret < 0) {
637 ret = -ENOENT;
638 goto out;
639 }
640 fname->crypto_buf.len = ret;
641 if (bigname) {
642 memcpy(&fname->hinfo.hash, fname->crypto_buf.name, 4);
643 memcpy(&fname->hinfo.minor_hash, fname->crypto_buf.name + 4, 4);
644 } else {
645 fname->disk_name.name = fname->crypto_buf.name;
646 fname->disk_name.len = fname->crypto_buf.len;
647 }
648 ret = 0;
649out:
650 ext4_put_fname_crypto_ctx(&ctx);
Michael Halcrowd5d0e8c2015-04-12 00:56:17 -0400651 return ret;
652}
653
Theodore Ts'o5b643f92015-05-18 13:14:47 -0400654void ext4_fname_free_filename(struct ext4_filename *fname)
Michael Halcrowd5d0e8c2015-04-12 00:56:17 -0400655{
Theodore Ts'o5b643f92015-05-18 13:14:47 -0400656 kfree(fname->crypto_buf.name);
657 fname->crypto_buf.name = NULL;
658 fname->usr_fname = NULL;
659 fname->disk_name.name = NULL;
Michael Halcrowd5d0e8c2015-04-12 00:56:17 -0400660}