blob: 52d4dbe1e8e79c0a3af433f1d4f8778c541d6aa5 [file] [log] [blame]
Greg Kroah-Hartmanb2441312017-11-01 15:07:57 +01001// SPDX-License-Identifier: GPL-2.0
Jaegeuk Kim0b81d072015-05-15 16:26:10 -07002/*
3 * This contains functions for filename crypto management
4 *
5 * Copyright (C) 2015, Google, Inc.
6 * Copyright (C) 2015, Motorola Mobility
7 *
8 * Written by Uday Savagaonkar, 2014.
9 * Modified by Jaegeuk Kim, 2015.
10 *
11 * This has not yet undergone a rigorous security audit.
12 */
13
Jaegeuk Kim0b81d072015-05-15 16:26:10 -070014#include <linux/scatterlist.h>
15#include <linux/ratelimit.h>
Eric Biggersa5757842018-01-05 10:45:00 -080016#include <crypto/skcipher.h>
Theodore Ts'o3325bea2016-11-26 20:32:46 -050017#include "fscrypt_private.h"
Jaegeuk Kim0b81d072015-05-15 16:26:10 -070018
Eric Biggersdcf0db92018-01-05 10:44:59 -080019static inline bool fscrypt_is_dot_dotdot(const struct qstr *str)
20{
21 if (str->len == 1 && str->name[0] == '.')
22 return true;
23
24 if (str->len == 2 && str->name[0] == '.' && str->name[1] == '.')
25 return true;
26
27 return false;
28}
29
Jaegeuk Kim0b81d072015-05-15 16:26:10 -070030/**
Eric Biggersef1eb3a2016-09-15 17:25:55 -040031 * fname_encrypt() - encrypt a filename
Jaegeuk Kim0b81d072015-05-15 16:26:10 -070032 *
Eric Biggersef1eb3a2016-09-15 17:25:55 -040033 * The caller must have allocated sufficient memory for the @oname string.
34 *
35 * Return: 0 on success, -errno on failure
Jaegeuk Kim0b81d072015-05-15 16:26:10 -070036 */
37static int fname_encrypt(struct inode *inode,
38 const struct qstr *iname, struct fscrypt_str *oname)
39{
Linus Torvaldsd4075742016-03-21 11:03:02 -070040 struct skcipher_request *req = NULL;
Gilad Ben-Yossefd0082e12017-10-18 08:00:44 +010041 DECLARE_CRYPTO_WAIT(wait);
Jaegeuk Kim0b81d072015-05-15 16:26:10 -070042 struct fscrypt_info *ci = inode->i_crypt_info;
Linus Torvaldsd4075742016-03-21 11:03:02 -070043 struct crypto_skcipher *tfm = ci->ci_ctfm;
Jaegeuk Kim0b81d072015-05-15 16:26:10 -070044 int res = 0;
45 char iv[FS_CRYPTO_BLOCK_SIZE];
Eric Biggers08ae8772016-11-13 20:35:52 -050046 struct scatterlist sg;
Jaegeuk Kim0b81d072015-05-15 16:26:10 -070047 int padding = 4 << (ci->ci_flags & FS_POLICY_FLAGS_PAD_MASK);
Eric Biggers08ae8772016-11-13 20:35:52 -050048 unsigned int lim;
49 unsigned int cryptlen;
Jaegeuk Kim0b81d072015-05-15 16:26:10 -070050
51 lim = inode->i_sb->s_cop->max_namelen(inode);
52 if (iname->len <= 0 || iname->len > lim)
53 return -EIO;
54
Eric Biggers08ae8772016-11-13 20:35:52 -050055 /*
56 * Copy the filename to the output buffer for encrypting in-place and
57 * pad it with the needed number of NUL bytes.
58 */
59 cryptlen = max_t(unsigned int, iname->len, FS_CRYPTO_BLOCK_SIZE);
60 cryptlen = round_up(cryptlen, padding);
61 cryptlen = min(cryptlen, lim);
62 memcpy(oname->name, iname->name, iname->len);
63 memset(oname->name + iname->len, 0, cryptlen - iname->len);
Jaegeuk Kim0b81d072015-05-15 16:26:10 -070064
Eric Biggers08ae8772016-11-13 20:35:52 -050065 /* Initialize the IV */
66 memset(iv, 0, FS_CRYPTO_BLOCK_SIZE);
Jaegeuk Kim0b81d072015-05-15 16:26:10 -070067
Eric Biggers08ae8772016-11-13 20:35:52 -050068 /* Set up the encryption request */
Linus Torvaldsd4075742016-03-21 11:03:02 -070069 req = skcipher_request_alloc(tfm, GFP_NOFS);
Jaegeuk Kim0b81d072015-05-15 16:26:10 -070070 if (!req) {
71 printk_ratelimited(KERN_ERR
Eric Biggers08ae8772016-11-13 20:35:52 -050072 "%s: skcipher_request_alloc() failed\n", __func__);
Jaegeuk Kim0b81d072015-05-15 16:26:10 -070073 return -ENOMEM;
74 }
Linus Torvaldsd4075742016-03-21 11:03:02 -070075 skcipher_request_set_callback(req,
Jaegeuk Kim0b81d072015-05-15 16:26:10 -070076 CRYPTO_TFM_REQ_MAY_BACKLOG | CRYPTO_TFM_REQ_MAY_SLEEP,
Gilad Ben-Yossefd0082e12017-10-18 08:00:44 +010077 crypto_req_done, &wait);
Eric Biggers08ae8772016-11-13 20:35:52 -050078 sg_init_one(&sg, oname->name, cryptlen);
79 skcipher_request_set_crypt(req, &sg, &sg, cryptlen, iv);
Jaegeuk Kim0b81d072015-05-15 16:26:10 -070080
Eric Biggers08ae8772016-11-13 20:35:52 -050081 /* Do the encryption */
Gilad Ben-Yossefd0082e12017-10-18 08:00:44 +010082 res = crypto_wait_req(crypto_skcipher_encrypt(req), &wait);
Linus Torvaldsd4075742016-03-21 11:03:02 -070083 skcipher_request_free(req);
Eric Biggersef1eb3a2016-09-15 17:25:55 -040084 if (res < 0) {
Jaegeuk Kim0b81d072015-05-15 16:26:10 -070085 printk_ratelimited(KERN_ERR
86 "%s: Error (error code %d)\n", __func__, res);
Eric Biggersef1eb3a2016-09-15 17:25:55 -040087 return res;
88 }
Jaegeuk Kim0b81d072015-05-15 16:26:10 -070089
Eric Biggers08ae8772016-11-13 20:35:52 -050090 oname->len = cryptlen;
Eric Biggersef1eb3a2016-09-15 17:25:55 -040091 return 0;
Jaegeuk Kim0b81d072015-05-15 16:26:10 -070092}
93
Eric Biggersef1eb3a2016-09-15 17:25:55 -040094/**
95 * fname_decrypt() - decrypt a filename
96 *
97 * The caller must have allocated sufficient memory for the @oname string.
98 *
99 * Return: 0 on success, -errno on failure
Jaegeuk Kim0b81d072015-05-15 16:26:10 -0700100 */
101static int fname_decrypt(struct inode *inode,
102 const struct fscrypt_str *iname,
103 struct fscrypt_str *oname)
104{
Linus Torvaldsd4075742016-03-21 11:03:02 -0700105 struct skcipher_request *req = NULL;
Gilad Ben-Yossefd0082e12017-10-18 08:00:44 +0100106 DECLARE_CRYPTO_WAIT(wait);
Jaegeuk Kim0b81d072015-05-15 16:26:10 -0700107 struct scatterlist src_sg, dst_sg;
108 struct fscrypt_info *ci = inode->i_crypt_info;
Linus Torvaldsd4075742016-03-21 11:03:02 -0700109 struct crypto_skcipher *tfm = ci->ci_ctfm;
Jaegeuk Kim0b81d072015-05-15 16:26:10 -0700110 int res = 0;
111 char iv[FS_CRYPTO_BLOCK_SIZE];
112 unsigned lim;
113
114 lim = inode->i_sb->s_cop->max_namelen(inode);
115 if (iname->len <= 0 || iname->len > lim)
116 return -EIO;
117
118 /* Allocate request */
Linus Torvaldsd4075742016-03-21 11:03:02 -0700119 req = skcipher_request_alloc(tfm, GFP_NOFS);
Jaegeuk Kim0b81d072015-05-15 16:26:10 -0700120 if (!req) {
121 printk_ratelimited(KERN_ERR
122 "%s: crypto_request_alloc() failed\n", __func__);
123 return -ENOMEM;
124 }
Linus Torvaldsd4075742016-03-21 11:03:02 -0700125 skcipher_request_set_callback(req,
Jaegeuk Kim0b81d072015-05-15 16:26:10 -0700126 CRYPTO_TFM_REQ_MAY_BACKLOG | CRYPTO_TFM_REQ_MAY_SLEEP,
Gilad Ben-Yossefd0082e12017-10-18 08:00:44 +0100127 crypto_req_done, &wait);
Jaegeuk Kim0b81d072015-05-15 16:26:10 -0700128
129 /* Initialize IV */
130 memset(iv, 0, FS_CRYPTO_BLOCK_SIZE);
131
132 /* Create decryption request */
133 sg_init_one(&src_sg, iname->name, iname->len);
134 sg_init_one(&dst_sg, oname->name, oname->len);
Linus Torvaldsd4075742016-03-21 11:03:02 -0700135 skcipher_request_set_crypt(req, &src_sg, &dst_sg, iname->len, iv);
Gilad Ben-Yossefd0082e12017-10-18 08:00:44 +0100136 res = crypto_wait_req(crypto_skcipher_decrypt(req), &wait);
Linus Torvaldsd4075742016-03-21 11:03:02 -0700137 skcipher_request_free(req);
Jaegeuk Kim0b81d072015-05-15 16:26:10 -0700138 if (res < 0) {
139 printk_ratelimited(KERN_ERR
140 "%s: Error (error code %d)\n", __func__, res);
141 return res;
142 }
143
144 oname->len = strnlen(oname->name, iname->len);
Eric Biggersef1eb3a2016-09-15 17:25:55 -0400145 return 0;
Jaegeuk Kim0b81d072015-05-15 16:26:10 -0700146}
147
148static const char *lookup_table =
149 "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+,";
150
Eric Biggers17159422017-04-24 10:00:10 -0700151#define BASE64_CHARS(nbytes) DIV_ROUND_UP((nbytes) * 4, 3)
152
Jaegeuk Kim0b81d072015-05-15 16:26:10 -0700153/**
154 * digest_encode() -
155 *
156 * Encodes the input digest using characters from the set [a-zA-Z0-9_+].
157 * The encoded string is roughly 4/3 times the size of the input string.
158 */
159static int digest_encode(const char *src, int len, char *dst)
160{
161 int i = 0, bits = 0, ac = 0;
162 char *cp = dst;
163
164 while (i < len) {
165 ac += (((unsigned char) src[i]) << bits);
166 bits += 8;
167 do {
168 *cp++ = lookup_table[ac & 0x3f];
169 ac >>= 6;
170 bits -= 6;
171 } while (bits >= 6);
172 i++;
173 }
174 if (bits)
175 *cp++ = lookup_table[ac & 0x3f];
176 return cp - dst;
177}
178
179static int digest_decode(const char *src, int len, char *dst)
180{
181 int i = 0, bits = 0, ac = 0;
182 const char *p;
183 char *cp = dst;
184
185 while (i < len) {
186 p = strchr(lookup_table, src[i]);
187 if (p == NULL || src[i] == 0)
188 return -2;
189 ac += (p - lookup_table) << bits;
190 bits += 6;
191 if (bits >= 8) {
192 *cp++ = ac & 0xff;
193 ac >>= 8;
194 bits -= 8;
195 }
196 i++;
197 }
198 if (ac)
199 return -1;
200 return cp - dst;
201}
202
David Gstir0b93e1b2016-11-13 22:20:47 +0100203u32 fscrypt_fname_encrypted_size(const struct inode *inode, u32 ilen)
Jaegeuk Kim0b81d072015-05-15 16:26:10 -0700204{
205 int padding = 32;
206 struct fscrypt_info *ci = inode->i_crypt_info;
207
208 if (ci)
209 padding = 4 << (ci->ci_flags & FS_POLICY_FLAGS_PAD_MASK);
Eric Biggers55be3142016-09-30 01:46:18 -0400210 ilen = max(ilen, (u32)FS_CRYPTO_BLOCK_SIZE);
211 return round_up(ilen, padding);
Jaegeuk Kim0b81d072015-05-15 16:26:10 -0700212}
213EXPORT_SYMBOL(fscrypt_fname_encrypted_size);
214
215/**
216 * fscrypt_fname_crypto_alloc_obuff() -
217 *
218 * Allocates an output buffer that is sufficient for the crypto operation
219 * specified by the context and the direction.
220 */
David Gstir0b93e1b2016-11-13 22:20:47 +0100221int fscrypt_fname_alloc_buffer(const struct inode *inode,
Jaegeuk Kim0b81d072015-05-15 16:26:10 -0700222 u32 ilen, struct fscrypt_str *crypto_str)
223{
Eric Biggers17159422017-04-24 10:00:10 -0700224 u32 olen = fscrypt_fname_encrypted_size(inode, ilen);
225 const u32 max_encoded_len =
226 max_t(u32, BASE64_CHARS(FSCRYPT_FNAME_MAX_UNDIGESTED_SIZE),
227 1 + BASE64_CHARS(sizeof(struct fscrypt_digested_name)));
Jaegeuk Kim0b81d072015-05-15 16:26:10 -0700228
229 crypto_str->len = olen;
Eric Biggers17159422017-04-24 10:00:10 -0700230 olen = max(olen, max_encoded_len);
231
Jaegeuk Kim0b81d072015-05-15 16:26:10 -0700232 /*
233 * Allocated buffer can hold one more character to null-terminate the
234 * string
235 */
236 crypto_str->name = kmalloc(olen + 1, GFP_NOFS);
237 if (!(crypto_str->name))
238 return -ENOMEM;
239 return 0;
240}
241EXPORT_SYMBOL(fscrypt_fname_alloc_buffer);
242
243/**
244 * fscrypt_fname_crypto_free_buffer() -
245 *
246 * Frees the buffer allocated for crypto operation.
247 */
248void fscrypt_fname_free_buffer(struct fscrypt_str *crypto_str)
249{
250 if (!crypto_str)
251 return;
252 kfree(crypto_str->name);
253 crypto_str->name = NULL;
254}
255EXPORT_SYMBOL(fscrypt_fname_free_buffer);
256
257/**
258 * fscrypt_fname_disk_to_usr() - converts a filename from disk space to user
259 * space
Eric Biggersef1eb3a2016-09-15 17:25:55 -0400260 *
261 * The caller must have allocated sufficient memory for the @oname string.
262 *
Eric Biggers17159422017-04-24 10:00:10 -0700263 * If the key is available, we'll decrypt the disk name; otherwise, we'll encode
264 * it for presentation. Short names are directly base64-encoded, while long
265 * names are encoded in fscrypt_digested_name format.
266 *
Eric Biggersef1eb3a2016-09-15 17:25:55 -0400267 * Return: 0 on success, -errno on failure
Jaegeuk Kim0b81d072015-05-15 16:26:10 -0700268 */
269int fscrypt_fname_disk_to_usr(struct inode *inode,
270 u32 hash, u32 minor_hash,
271 const struct fscrypt_str *iname,
272 struct fscrypt_str *oname)
273{
274 const struct qstr qname = FSTR_TO_QSTR(iname);
Eric Biggers17159422017-04-24 10:00:10 -0700275 struct fscrypt_digested_name digested_name;
Jaegeuk Kim0b81d072015-05-15 16:26:10 -0700276
277 if (fscrypt_is_dot_dotdot(&qname)) {
278 oname->name[0] = '.';
279 oname->name[iname->len - 1] = '.';
280 oname->len = iname->len;
Eric Biggersef1eb3a2016-09-15 17:25:55 -0400281 return 0;
Jaegeuk Kim0b81d072015-05-15 16:26:10 -0700282 }
283
284 if (iname->len < FS_CRYPTO_BLOCK_SIZE)
285 return -EUCLEAN;
286
287 if (inode->i_crypt_info)
288 return fname_decrypt(inode, iname, oname);
289
Eric Biggers17159422017-04-24 10:00:10 -0700290 if (iname->len <= FSCRYPT_FNAME_MAX_UNDIGESTED_SIZE) {
Eric Biggersef1eb3a2016-09-15 17:25:55 -0400291 oname->len = digest_encode(iname->name, iname->len,
292 oname->name);
293 return 0;
Jaegeuk Kim0b81d072015-05-15 16:26:10 -0700294 }
295 if (hash) {
Eric Biggers17159422017-04-24 10:00:10 -0700296 digested_name.hash = hash;
297 digested_name.minor_hash = minor_hash;
Jaegeuk Kim0b81d072015-05-15 16:26:10 -0700298 } else {
Eric Biggers17159422017-04-24 10:00:10 -0700299 digested_name.hash = 0;
300 digested_name.minor_hash = 0;
Jaegeuk Kim0b81d072015-05-15 16:26:10 -0700301 }
Eric Biggers17159422017-04-24 10:00:10 -0700302 memcpy(digested_name.digest,
303 FSCRYPT_FNAME_DIGEST(iname->name, iname->len),
304 FSCRYPT_FNAME_DIGEST_SIZE);
Jaegeuk Kim0b81d072015-05-15 16:26:10 -0700305 oname->name[0] = '_';
Eric Biggers17159422017-04-24 10:00:10 -0700306 oname->len = 1 + digest_encode((const char *)&digested_name,
307 sizeof(digested_name), oname->name + 1);
Eric Biggersef1eb3a2016-09-15 17:25:55 -0400308 return 0;
Jaegeuk Kim0b81d072015-05-15 16:26:10 -0700309}
310EXPORT_SYMBOL(fscrypt_fname_disk_to_usr);
311
312/**
313 * fscrypt_fname_usr_to_disk() - converts a filename from user space to disk
314 * space
Eric Biggersef1eb3a2016-09-15 17:25:55 -0400315 *
316 * The caller must have allocated sufficient memory for the @oname string.
317 *
318 * Return: 0 on success, -errno on failure
Jaegeuk Kim0b81d072015-05-15 16:26:10 -0700319 */
320int fscrypt_fname_usr_to_disk(struct inode *inode,
321 const struct qstr *iname,
322 struct fscrypt_str *oname)
323{
324 if (fscrypt_is_dot_dotdot(iname)) {
325 oname->name[0] = '.';
326 oname->name[iname->len - 1] = '.';
327 oname->len = iname->len;
Eric Biggersef1eb3a2016-09-15 17:25:55 -0400328 return 0;
Jaegeuk Kim0b81d072015-05-15 16:26:10 -0700329 }
330 if (inode->i_crypt_info)
331 return fname_encrypt(inode, iname, oname);
332 /*
333 * Without a proper key, a user is not allowed to modify the filenames
334 * in a directory. Consequently, a user space name cannot be mapped to
335 * a disk-space name
336 */
Eric Biggers54475f52016-12-05 11:12:44 -0800337 return -ENOKEY;
Jaegeuk Kim0b81d072015-05-15 16:26:10 -0700338}
339EXPORT_SYMBOL(fscrypt_fname_usr_to_disk);
340
Eric Biggers17159422017-04-24 10:00:10 -0700341/**
342 * fscrypt_setup_filename() - prepare to search a possibly encrypted directory
343 * @dir: the directory that will be searched
344 * @iname: the user-provided filename being searched for
345 * @lookup: 1 if we're allowed to proceed without the key because it's
346 * ->lookup() or we're finding the dir_entry for deletion; 0 if we cannot
347 * proceed without the key because we're going to create the dir_entry.
348 * @fname: the filename information to be filled in
349 *
350 * Given a user-provided filename @iname, this function sets @fname->disk_name
351 * to the name that would be stored in the on-disk directory entry, if possible.
352 * If the directory is unencrypted this is simply @iname. Else, if we have the
353 * directory's encryption key, then @iname is the plaintext, so we encrypt it to
354 * get the disk_name.
355 *
356 * Else, for keyless @lookup operations, @iname is the presented ciphertext, so
357 * we decode it to get either the ciphertext disk_name (for short names) or the
358 * fscrypt_digested_name (for long names). Non-@lookup operations will be
359 * impossible in this case, so we fail them with ENOKEY.
360 *
361 * If successful, fscrypt_free_filename() must be called later to clean up.
362 *
363 * Return: 0 on success, -errno on failure
364 */
Jaegeuk Kim0b81d072015-05-15 16:26:10 -0700365int fscrypt_setup_filename(struct inode *dir, const struct qstr *iname,
366 int lookup, struct fscrypt_name *fname)
367{
Eric Biggers17159422017-04-24 10:00:10 -0700368 int ret;
369 int digested;
Jaegeuk Kim0b81d072015-05-15 16:26:10 -0700370
371 memset(fname, 0, sizeof(struct fscrypt_name));
372 fname->usr_fname = iname;
373
Eric Biggerse0428a22017-10-09 12:15:36 -0700374 if (!IS_ENCRYPTED(dir) || fscrypt_is_dot_dotdot(iname)) {
Jaegeuk Kim0b81d072015-05-15 16:26:10 -0700375 fname->disk_name.name = (unsigned char *)iname->name;
376 fname->disk_name.len = iname->len;
377 return 0;
378 }
Eric Biggers1b53cf92017-02-21 15:07:11 -0800379 ret = fscrypt_get_encryption_info(dir);
Jaegeuk Kim0b81d072015-05-15 16:26:10 -0700380 if (ret && ret != -EOPNOTSUPP)
381 return ret;
382
383 if (dir->i_crypt_info) {
384 ret = fscrypt_fname_alloc_buffer(dir, iname->len,
385 &fname->crypto_buf);
Eric Biggersef1eb3a2016-09-15 17:25:55 -0400386 if (ret)
Jaegeuk Kim0b81d072015-05-15 16:26:10 -0700387 return ret;
388 ret = fname_encrypt(dir, iname, &fname->crypto_buf);
Eric Biggersef1eb3a2016-09-15 17:25:55 -0400389 if (ret)
Jaegeuk Kim0b81d072015-05-15 16:26:10 -0700390 goto errout;
391 fname->disk_name.name = fname->crypto_buf.name;
392 fname->disk_name.len = fname->crypto_buf.len;
393 return 0;
394 }
395 if (!lookup)
Eric Biggers54475f52016-12-05 11:12:44 -0800396 return -ENOKEY;
Jaegeuk Kim0b81d072015-05-15 16:26:10 -0700397
398 /*
399 * We don't have the key and we are doing a lookup; decode the
400 * user-supplied name
401 */
Eric Biggers17159422017-04-24 10:00:10 -0700402 if (iname->name[0] == '_') {
403 if (iname->len !=
404 1 + BASE64_CHARS(sizeof(struct fscrypt_digested_name)))
405 return -ENOENT;
406 digested = 1;
407 } else {
408 if (iname->len >
409 BASE64_CHARS(FSCRYPT_FNAME_MAX_UNDIGESTED_SIZE))
410 return -ENOENT;
411 digested = 0;
412 }
Jaegeuk Kim0b81d072015-05-15 16:26:10 -0700413
Eric Biggers17159422017-04-24 10:00:10 -0700414 fname->crypto_buf.name =
415 kmalloc(max_t(size_t, FSCRYPT_FNAME_MAX_UNDIGESTED_SIZE,
416 sizeof(struct fscrypt_digested_name)),
417 GFP_KERNEL);
Jaegeuk Kim0b81d072015-05-15 16:26:10 -0700418 if (fname->crypto_buf.name == NULL)
419 return -ENOMEM;
420
Eric Biggers17159422017-04-24 10:00:10 -0700421 ret = digest_decode(iname->name + digested, iname->len - digested,
Jaegeuk Kim0b81d072015-05-15 16:26:10 -0700422 fname->crypto_buf.name);
423 if (ret < 0) {
424 ret = -ENOENT;
425 goto errout;
426 }
427 fname->crypto_buf.len = ret;
Eric Biggers17159422017-04-24 10:00:10 -0700428 if (digested) {
429 const struct fscrypt_digested_name *n =
430 (const void *)fname->crypto_buf.name;
431 fname->hash = n->hash;
432 fname->minor_hash = n->minor_hash;
Jaegeuk Kim0b81d072015-05-15 16:26:10 -0700433 } else {
434 fname->disk_name.name = fname->crypto_buf.name;
435 fname->disk_name.len = fname->crypto_buf.len;
436 }
437 return 0;
438
439errout:
440 fscrypt_fname_free_buffer(&fname->crypto_buf);
441 return ret;
442}
443EXPORT_SYMBOL(fscrypt_setup_filename);