blob: 6eb434363ff2b6b2b7c61d44e6f8dd45feaf8833 [file] [log] [blame]
Jaegeuk Kim0b81d072015-05-15 16:26:10 -07001/*
2 * This contains functions for filename crypto management
3 *
4 * Copyright (C) 2015, Google, Inc.
5 * Copyright (C) 2015, Motorola Mobility
6 *
7 * Written by Uday Savagaonkar, 2014.
8 * Modified by Jaegeuk Kim, 2015.
9 *
10 * This has not yet undergone a rigorous security audit.
11 */
12
Jaegeuk Kim0b81d072015-05-15 16:26:10 -070013#include <linux/scatterlist.h>
14#include <linux/ratelimit.h>
Hyojun Kim63da4202017-10-06 17:10:08 -070015#include "fscrypt_private.h"
Jaegeuk Kim0b81d072015-05-15 16:26:10 -070016
Jaegeuk Kim0b81d072015-05-15 16:26:10 -070017/**
Eric Biggersef1eb3a2016-09-15 17:25:55 -040018 * fname_encrypt() - encrypt a filename
Jaegeuk Kim0b81d072015-05-15 16:26:10 -070019 *
Eric Biggersef1eb3a2016-09-15 17:25:55 -040020 * The caller must have allocated sufficient memory for the @oname string.
21 *
22 * Return: 0 on success, -errno on failure
Jaegeuk Kim0b81d072015-05-15 16:26:10 -070023 */
24static int fname_encrypt(struct inode *inode,
25 const struct qstr *iname, struct fscrypt_str *oname)
26{
Linus Torvaldsd4075742016-03-21 11:03:02 -070027 struct skcipher_request *req = NULL;
Jaegeuk Kim8dec0742017-06-22 12:14:40 -070028 DECLARE_CRYPTO_WAIT(wait);
Jaegeuk Kim0b81d072015-05-15 16:26:10 -070029 struct fscrypt_info *ci = inode->i_crypt_info;
Linus Torvaldsd4075742016-03-21 11:03:02 -070030 struct crypto_skcipher *tfm = ci->ci_ctfm;
Jaegeuk Kim0b81d072015-05-15 16:26:10 -070031 int res = 0;
32 char iv[FS_CRYPTO_BLOCK_SIZE];
Eric Biggers3c7018e2016-11-13 20:35:52 -050033 struct scatterlist sg;
Jaegeuk Kim0b81d072015-05-15 16:26:10 -070034 int padding = 4 << (ci->ci_flags & FS_POLICY_FLAGS_PAD_MASK);
Eric Biggers3c7018e2016-11-13 20:35:52 -050035 unsigned int lim;
36 unsigned int cryptlen;
Jaegeuk Kim0b81d072015-05-15 16:26:10 -070037
38 lim = inode->i_sb->s_cop->max_namelen(inode);
39 if (iname->len <= 0 || iname->len > lim)
40 return -EIO;
41
Eric Biggers3c7018e2016-11-13 20:35:52 -050042 /*
43 * Copy the filename to the output buffer for encrypting in-place and
44 * pad it with the needed number of NUL bytes.
45 */
46 cryptlen = max_t(unsigned int, iname->len, FS_CRYPTO_BLOCK_SIZE);
47 cryptlen = round_up(cryptlen, padding);
48 cryptlen = min(cryptlen, lim);
49 memcpy(oname->name, iname->name, iname->len);
50 memset(oname->name + iname->len, 0, cryptlen - iname->len);
Jaegeuk Kim0b81d072015-05-15 16:26:10 -070051
Eric Biggers3c7018e2016-11-13 20:35:52 -050052 /* Initialize the IV */
53 memset(iv, 0, FS_CRYPTO_BLOCK_SIZE);
Jaegeuk Kim0b81d072015-05-15 16:26:10 -070054
Eric Biggers3c7018e2016-11-13 20:35:52 -050055 /* Set up the encryption request */
Linus Torvaldsd4075742016-03-21 11:03:02 -070056 req = skcipher_request_alloc(tfm, GFP_NOFS);
Jaegeuk Kim0b81d072015-05-15 16:26:10 -070057 if (!req) {
58 printk_ratelimited(KERN_ERR
Eric Biggers3c7018e2016-11-13 20:35:52 -050059 "%s: skcipher_request_alloc() failed\n", __func__);
Jaegeuk Kim0b81d072015-05-15 16:26:10 -070060 return -ENOMEM;
61 }
Linus Torvaldsd4075742016-03-21 11:03:02 -070062 skcipher_request_set_callback(req,
Jaegeuk Kim0b81d072015-05-15 16:26:10 -070063 CRYPTO_TFM_REQ_MAY_BACKLOG | CRYPTO_TFM_REQ_MAY_SLEEP,
Jaegeuk Kim8dec0742017-06-22 12:14:40 -070064 crypto_req_done, &wait);
Eric Biggers3c7018e2016-11-13 20:35:52 -050065 sg_init_one(&sg, oname->name, cryptlen);
66 skcipher_request_set_crypt(req, &sg, &sg, cryptlen, iv);
Jaegeuk Kim0b81d072015-05-15 16:26:10 -070067
Eric Biggers3c7018e2016-11-13 20:35:52 -050068 /* Do the encryption */
Jaegeuk Kim8dec0742017-06-22 12:14:40 -070069 res = crypto_wait_req(crypto_skcipher_encrypt(req), &wait);
Linus Torvaldsd4075742016-03-21 11:03:02 -070070 skcipher_request_free(req);
Eric Biggersef1eb3a2016-09-15 17:25:55 -040071 if (res < 0) {
Jaegeuk Kim0b81d072015-05-15 16:26:10 -070072 printk_ratelimited(KERN_ERR
73 "%s: Error (error code %d)\n", __func__, res);
Eric Biggersef1eb3a2016-09-15 17:25:55 -040074 return res;
75 }
Jaegeuk Kim0b81d072015-05-15 16:26:10 -070076
Eric Biggers3c7018e2016-11-13 20:35:52 -050077 oname->len = cryptlen;
Eric Biggersef1eb3a2016-09-15 17:25:55 -040078 return 0;
Jaegeuk Kim0b81d072015-05-15 16:26:10 -070079}
80
Eric Biggersef1eb3a2016-09-15 17:25:55 -040081/**
82 * fname_decrypt() - decrypt a filename
83 *
84 * The caller must have allocated sufficient memory for the @oname string.
85 *
86 * Return: 0 on success, -errno on failure
Jaegeuk Kim0b81d072015-05-15 16:26:10 -070087 */
88static int fname_decrypt(struct inode *inode,
89 const struct fscrypt_str *iname,
90 struct fscrypt_str *oname)
91{
Linus Torvaldsd4075742016-03-21 11:03:02 -070092 struct skcipher_request *req = NULL;
Jaegeuk Kim8dec0742017-06-22 12:14:40 -070093 DECLARE_CRYPTO_WAIT(wait);
Jaegeuk Kim0b81d072015-05-15 16:26:10 -070094 struct scatterlist src_sg, dst_sg;
95 struct fscrypt_info *ci = inode->i_crypt_info;
Linus Torvaldsd4075742016-03-21 11:03:02 -070096 struct crypto_skcipher *tfm = ci->ci_ctfm;
Jaegeuk Kim0b81d072015-05-15 16:26:10 -070097 int res = 0;
98 char iv[FS_CRYPTO_BLOCK_SIZE];
99 unsigned lim;
100
101 lim = inode->i_sb->s_cop->max_namelen(inode);
102 if (iname->len <= 0 || iname->len > lim)
103 return -EIO;
104
105 /* Allocate request */
Linus Torvaldsd4075742016-03-21 11:03:02 -0700106 req = skcipher_request_alloc(tfm, GFP_NOFS);
Jaegeuk Kim0b81d072015-05-15 16:26:10 -0700107 if (!req) {
108 printk_ratelimited(KERN_ERR
109 "%s: crypto_request_alloc() failed\n", __func__);
110 return -ENOMEM;
111 }
Linus Torvaldsd4075742016-03-21 11:03:02 -0700112 skcipher_request_set_callback(req,
Jaegeuk Kim0b81d072015-05-15 16:26:10 -0700113 CRYPTO_TFM_REQ_MAY_BACKLOG | CRYPTO_TFM_REQ_MAY_SLEEP,
Jaegeuk Kim8dec0742017-06-22 12:14:40 -0700114 crypto_req_done, &wait);
Jaegeuk Kim0b81d072015-05-15 16:26:10 -0700115
116 /* Initialize IV */
117 memset(iv, 0, FS_CRYPTO_BLOCK_SIZE);
118
119 /* Create decryption request */
120 sg_init_one(&src_sg, iname->name, iname->len);
121 sg_init_one(&dst_sg, oname->name, oname->len);
Linus Torvaldsd4075742016-03-21 11:03:02 -0700122 skcipher_request_set_crypt(req, &src_sg, &dst_sg, iname->len, iv);
Jaegeuk Kim8dec0742017-06-22 12:14:40 -0700123 res = crypto_wait_req(crypto_skcipher_decrypt(req), &wait);
Linus Torvaldsd4075742016-03-21 11:03:02 -0700124 skcipher_request_free(req);
Jaegeuk Kim0b81d072015-05-15 16:26:10 -0700125 if (res < 0) {
126 printk_ratelimited(KERN_ERR
127 "%s: Error (error code %d)\n", __func__, res);
128 return res;
129 }
130
131 oname->len = strnlen(oname->name, iname->len);
Eric Biggersef1eb3a2016-09-15 17:25:55 -0400132 return 0;
Jaegeuk Kim0b81d072015-05-15 16:26:10 -0700133}
134
135static const char *lookup_table =
136 "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+,";
137
Hyojun Kim63da4202017-10-06 17:10:08 -0700138#define BASE64_CHARS(nbytes) DIV_ROUND_UP((nbytes) * 4, 3)
139
Jaegeuk Kim0b81d072015-05-15 16:26:10 -0700140/**
141 * digest_encode() -
142 *
143 * Encodes the input digest using characters from the set [a-zA-Z0-9_+].
144 * The encoded string is roughly 4/3 times the size of the input string.
145 */
146static int digest_encode(const char *src, int len, char *dst)
147{
148 int i = 0, bits = 0, ac = 0;
149 char *cp = dst;
150
151 while (i < len) {
152 ac += (((unsigned char) src[i]) << bits);
153 bits += 8;
154 do {
155 *cp++ = lookup_table[ac & 0x3f];
156 ac >>= 6;
157 bits -= 6;
158 } while (bits >= 6);
159 i++;
160 }
161 if (bits)
162 *cp++ = lookup_table[ac & 0x3f];
163 return cp - dst;
164}
165
166static int digest_decode(const char *src, int len, char *dst)
167{
168 int i = 0, bits = 0, ac = 0;
169 const char *p;
170 char *cp = dst;
171
172 while (i < len) {
173 p = strchr(lookup_table, src[i]);
174 if (p == NULL || src[i] == 0)
175 return -2;
176 ac += (p - lookup_table) << bits;
177 bits += 6;
178 if (bits >= 8) {
179 *cp++ = ac & 0xff;
180 ac >>= 8;
181 bits -= 8;
182 }
183 i++;
184 }
185 if (ac)
186 return -1;
187 return cp - dst;
188}
189
Hyojun Kim63da4202017-10-06 17:10:08 -0700190u32 fscrypt_fname_encrypted_size(const struct inode *inode, u32 ilen)
Jaegeuk Kim0b81d072015-05-15 16:26:10 -0700191{
192 int padding = 32;
193 struct fscrypt_info *ci = inode->i_crypt_info;
194
195 if (ci)
196 padding = 4 << (ci->ci_flags & FS_POLICY_FLAGS_PAD_MASK);
Eric Biggers55be3142016-09-30 01:46:18 -0400197 ilen = max(ilen, (u32)FS_CRYPTO_BLOCK_SIZE);
198 return round_up(ilen, padding);
Jaegeuk Kim0b81d072015-05-15 16:26:10 -0700199}
200EXPORT_SYMBOL(fscrypt_fname_encrypted_size);
201
202/**
203 * fscrypt_fname_crypto_alloc_obuff() -
204 *
205 * Allocates an output buffer that is sufficient for the crypto operation
206 * specified by the context and the direction.
207 */
Hyojun Kim63da4202017-10-06 17:10:08 -0700208int fscrypt_fname_alloc_buffer(const struct inode *inode,
Jaegeuk Kim0b81d072015-05-15 16:26:10 -0700209 u32 ilen, struct fscrypt_str *crypto_str)
210{
Hyojun Kim63da4202017-10-06 17:10:08 -0700211 u32 olen = fscrypt_fname_encrypted_size(inode, ilen);
212 const u32 max_encoded_len =
213 max_t(u32, BASE64_CHARS(FSCRYPT_FNAME_MAX_UNDIGESTED_SIZE),
214 1 + BASE64_CHARS(sizeof(struct fscrypt_digested_name)));
Jaegeuk Kim0b81d072015-05-15 16:26:10 -0700215
216 crypto_str->len = olen;
Hyojun Kim63da4202017-10-06 17:10:08 -0700217 olen = max(olen, max_encoded_len);
218
Jaegeuk Kim0b81d072015-05-15 16:26:10 -0700219 /*
220 * Allocated buffer can hold one more character to null-terminate the
221 * string
222 */
223 crypto_str->name = kmalloc(olen + 1, GFP_NOFS);
224 if (!(crypto_str->name))
225 return -ENOMEM;
226 return 0;
227}
228EXPORT_SYMBOL(fscrypt_fname_alloc_buffer);
229
230/**
231 * fscrypt_fname_crypto_free_buffer() -
232 *
233 * Frees the buffer allocated for crypto operation.
234 */
235void fscrypt_fname_free_buffer(struct fscrypt_str *crypto_str)
236{
237 if (!crypto_str)
238 return;
239 kfree(crypto_str->name);
240 crypto_str->name = NULL;
241}
242EXPORT_SYMBOL(fscrypt_fname_free_buffer);
243
244/**
245 * fscrypt_fname_disk_to_usr() - converts a filename from disk space to user
246 * space
Eric Biggersef1eb3a2016-09-15 17:25:55 -0400247 *
248 * The caller must have allocated sufficient memory for the @oname string.
249 *
Hyojun Kim63da4202017-10-06 17:10:08 -0700250 * If the key is available, we'll decrypt the disk name; otherwise, we'll encode
251 * it for presentation. Short names are directly base64-encoded, while long
252 * names are encoded in fscrypt_digested_name format.
253 *
Eric Biggersef1eb3a2016-09-15 17:25:55 -0400254 * Return: 0 on success, -errno on failure
Jaegeuk Kim0b81d072015-05-15 16:26:10 -0700255 */
256int fscrypt_fname_disk_to_usr(struct inode *inode,
257 u32 hash, u32 minor_hash,
258 const struct fscrypt_str *iname,
259 struct fscrypt_str *oname)
260{
261 const struct qstr qname = FSTR_TO_QSTR(iname);
Hyojun Kim63da4202017-10-06 17:10:08 -0700262 struct fscrypt_digested_name digested_name;
Jaegeuk Kim0b81d072015-05-15 16:26:10 -0700263
264 if (fscrypt_is_dot_dotdot(&qname)) {
265 oname->name[0] = '.';
266 oname->name[iname->len - 1] = '.';
267 oname->len = iname->len;
Eric Biggersef1eb3a2016-09-15 17:25:55 -0400268 return 0;
Jaegeuk Kim0b81d072015-05-15 16:26:10 -0700269 }
270
271 if (iname->len < FS_CRYPTO_BLOCK_SIZE)
272 return -EUCLEAN;
273
274 if (inode->i_crypt_info)
275 return fname_decrypt(inode, iname, oname);
276
Hyojun Kim63da4202017-10-06 17:10:08 -0700277 if (iname->len <= FSCRYPT_FNAME_MAX_UNDIGESTED_SIZE) {
Eric Biggersef1eb3a2016-09-15 17:25:55 -0400278 oname->len = digest_encode(iname->name, iname->len,
279 oname->name);
280 return 0;
Jaegeuk Kim0b81d072015-05-15 16:26:10 -0700281 }
282 if (hash) {
Hyojun Kim63da4202017-10-06 17:10:08 -0700283 digested_name.hash = hash;
284 digested_name.minor_hash = minor_hash;
Jaegeuk Kim0b81d072015-05-15 16:26:10 -0700285 } else {
Hyojun Kim63da4202017-10-06 17:10:08 -0700286 digested_name.hash = 0;
287 digested_name.minor_hash = 0;
Jaegeuk Kim0b81d072015-05-15 16:26:10 -0700288 }
Hyojun Kim63da4202017-10-06 17:10:08 -0700289 memcpy(digested_name.digest,
290 FSCRYPT_FNAME_DIGEST(iname->name, iname->len),
291 FSCRYPT_FNAME_DIGEST_SIZE);
Jaegeuk Kim0b81d072015-05-15 16:26:10 -0700292 oname->name[0] = '_';
Hyojun Kim63da4202017-10-06 17:10:08 -0700293 oname->len = 1 + digest_encode((const char *)&digested_name,
294 sizeof(digested_name), oname->name + 1);
Eric Biggersef1eb3a2016-09-15 17:25:55 -0400295 return 0;
Jaegeuk Kim0b81d072015-05-15 16:26:10 -0700296}
297EXPORT_SYMBOL(fscrypt_fname_disk_to_usr);
298
299/**
300 * fscrypt_fname_usr_to_disk() - converts a filename from user space to disk
301 * space
Eric Biggersef1eb3a2016-09-15 17:25:55 -0400302 *
303 * The caller must have allocated sufficient memory for the @oname string.
304 *
305 * Return: 0 on success, -errno on failure
Jaegeuk Kim0b81d072015-05-15 16:26:10 -0700306 */
307int fscrypt_fname_usr_to_disk(struct inode *inode,
308 const struct qstr *iname,
309 struct fscrypt_str *oname)
310{
311 if (fscrypt_is_dot_dotdot(iname)) {
312 oname->name[0] = '.';
313 oname->name[iname->len - 1] = '.';
314 oname->len = iname->len;
Eric Biggersef1eb3a2016-09-15 17:25:55 -0400315 return 0;
Jaegeuk Kim0b81d072015-05-15 16:26:10 -0700316 }
317 if (inode->i_crypt_info)
318 return fname_encrypt(inode, iname, oname);
319 /*
320 * Without a proper key, a user is not allowed to modify the filenames
321 * in a directory. Consequently, a user space name cannot be mapped to
322 * a disk-space name
323 */
Hyojun Kim63da4202017-10-06 17:10:08 -0700324 return -ENOKEY;
Jaegeuk Kim0b81d072015-05-15 16:26:10 -0700325}
326EXPORT_SYMBOL(fscrypt_fname_usr_to_disk);
327
Hyojun Kim63da4202017-10-06 17:10:08 -0700328/**
329 * fscrypt_setup_filename() - prepare to search a possibly encrypted directory
330 * @dir: the directory that will be searched
331 * @iname: the user-provided filename being searched for
332 * @lookup: 1 if we're allowed to proceed without the key because it's
333 * ->lookup() or we're finding the dir_entry for deletion; 0 if we cannot
334 * proceed without the key because we're going to create the dir_entry.
335 * @fname: the filename information to be filled in
336 *
337 * Given a user-provided filename @iname, this function sets @fname->disk_name
338 * to the name that would be stored in the on-disk directory entry, if possible.
339 * If the directory is unencrypted this is simply @iname. Else, if we have the
340 * directory's encryption key, then @iname is the plaintext, so we encrypt it to
341 * get the disk_name.
342 *
343 * Else, for keyless @lookup operations, @iname is the presented ciphertext, so
344 * we decode it to get either the ciphertext disk_name (for short names) or the
345 * fscrypt_digested_name (for long names). Non-@lookup operations will be
346 * impossible in this case, so we fail them with ENOKEY.
347 *
348 * If successful, fscrypt_free_filename() must be called later to clean up.
349 *
350 * Return: 0 on success, -errno on failure
351 */
Jaegeuk Kim0b81d072015-05-15 16:26:10 -0700352int fscrypt_setup_filename(struct inode *dir, const struct qstr *iname,
353 int lookup, struct fscrypt_name *fname)
354{
Hyojun Kim63da4202017-10-06 17:10:08 -0700355 int ret;
356 int digested;
Jaegeuk Kim0b81d072015-05-15 16:26:10 -0700357
358 memset(fname, 0, sizeof(struct fscrypt_name));
359 fname->usr_fname = iname;
360
Jaegeuk Kim8dec0742017-06-22 12:14:40 -0700361 if (!IS_ENCRYPTED(dir) || fscrypt_is_dot_dotdot(iname)) {
Jaegeuk Kim0b81d072015-05-15 16:26:10 -0700362 fname->disk_name.name = (unsigned char *)iname->name;
363 fname->disk_name.len = iname->len;
364 return 0;
365 }
Eric Biggers2984e522017-02-21 15:07:11 -0800366 ret = fscrypt_get_encryption_info(dir);
Jaegeuk Kim0b81d072015-05-15 16:26:10 -0700367 if (ret && ret != -EOPNOTSUPP)
368 return ret;
369
370 if (dir->i_crypt_info) {
371 ret = fscrypt_fname_alloc_buffer(dir, iname->len,
372 &fname->crypto_buf);
Eric Biggersef1eb3a2016-09-15 17:25:55 -0400373 if (ret)
Jaegeuk Kim0b81d072015-05-15 16:26:10 -0700374 return ret;
375 ret = fname_encrypt(dir, iname, &fname->crypto_buf);
Eric Biggersef1eb3a2016-09-15 17:25:55 -0400376 if (ret)
Jaegeuk Kim0b81d072015-05-15 16:26:10 -0700377 goto errout;
378 fname->disk_name.name = fname->crypto_buf.name;
379 fname->disk_name.len = fname->crypto_buf.len;
380 return 0;
381 }
382 if (!lookup)
Hyojun Kim63da4202017-10-06 17:10:08 -0700383 return -ENOKEY;
Jaegeuk Kim0b81d072015-05-15 16:26:10 -0700384
385 /*
386 * We don't have the key and we are doing a lookup; decode the
387 * user-supplied name
388 */
Hyojun Kim63da4202017-10-06 17:10:08 -0700389 if (iname->name[0] == '_') {
390 if (iname->len !=
391 1 + BASE64_CHARS(sizeof(struct fscrypt_digested_name)))
392 return -ENOENT;
393 digested = 1;
394 } else {
395 if (iname->len >
396 BASE64_CHARS(FSCRYPT_FNAME_MAX_UNDIGESTED_SIZE))
397 return -ENOENT;
398 digested = 0;
399 }
Jaegeuk Kim0b81d072015-05-15 16:26:10 -0700400
Hyojun Kim63da4202017-10-06 17:10:08 -0700401 fname->crypto_buf.name =
402 kmalloc(max_t(size_t, FSCRYPT_FNAME_MAX_UNDIGESTED_SIZE,
403 sizeof(struct fscrypt_digested_name)),
404 GFP_KERNEL);
Jaegeuk Kim0b81d072015-05-15 16:26:10 -0700405 if (fname->crypto_buf.name == NULL)
406 return -ENOMEM;
407
Hyojun Kim63da4202017-10-06 17:10:08 -0700408 ret = digest_decode(iname->name + digested, iname->len - digested,
Jaegeuk Kim0b81d072015-05-15 16:26:10 -0700409 fname->crypto_buf.name);
410 if (ret < 0) {
411 ret = -ENOENT;
412 goto errout;
413 }
414 fname->crypto_buf.len = ret;
Hyojun Kim63da4202017-10-06 17:10:08 -0700415 if (digested) {
416 const struct fscrypt_digested_name *n =
417 (const void *)fname->crypto_buf.name;
418 fname->hash = n->hash;
419 fname->minor_hash = n->minor_hash;
Jaegeuk Kim0b81d072015-05-15 16:26:10 -0700420 } else {
421 fname->disk_name.name = fname->crypto_buf.name;
422 fname->disk_name.len = fname->crypto_buf.len;
423 }
424 return 0;
425
426errout:
427 fscrypt_fname_free_buffer(&fname->crypto_buf);
428 return ret;
429}
430EXPORT_SYMBOL(fscrypt_setup_filename);