blob: 65424b89a1d16949044c5f4d617f6b0fbf357532 [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 Biggers50c961d2018-01-11 23:30:08 -050033 * The output buffer must be at least as large as the input buffer.
34 * Any extra space is filled with NUL padding before encryption.
Eric Biggersef1eb3a2016-09-15 17:25:55 -040035 *
36 * Return: 0 on success, -errno on failure
Jaegeuk Kim0b81d072015-05-15 16:26:10 -070037 */
Eric Biggers50c961d2018-01-11 23:30:08 -050038int fname_encrypt(struct inode *inode, const struct qstr *iname,
39 u8 *out, unsigned int olen)
Jaegeuk Kim0b81d072015-05-15 16:26:10 -070040{
Linus Torvaldsd4075742016-03-21 11:03:02 -070041 struct skcipher_request *req = NULL;
Gilad Ben-Yossefd0082e12017-10-18 08:00:44 +010042 DECLARE_CRYPTO_WAIT(wait);
Eric Biggers50c961d2018-01-11 23:30:08 -050043 struct crypto_skcipher *tfm = inode->i_crypt_info->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
Eric Biggers08ae8772016-11-13 20:35:52 -050048 /*
49 * Copy the filename to the output buffer for encrypting in-place and
50 * pad it with the needed number of NUL bytes.
51 */
Eric Biggers50c961d2018-01-11 23:30:08 -050052 if (WARN_ON(olen < iname->len))
Eric Biggers76e81d62018-01-05 10:45:01 -080053 return -ENOBUFS;
Eric Biggers50c961d2018-01-11 23:30:08 -050054 memcpy(out, iname->name, iname->len);
55 memset(out + iname->len, 0, olen - iname->len);
Jaegeuk Kim0b81d072015-05-15 16:26:10 -070056
Eric Biggers08ae8772016-11-13 20:35:52 -050057 /* Initialize the IV */
58 memset(iv, 0, FS_CRYPTO_BLOCK_SIZE);
Jaegeuk Kim0b81d072015-05-15 16:26:10 -070059
Eric Biggers08ae8772016-11-13 20:35:52 -050060 /* Set up the encryption request */
Linus Torvaldsd4075742016-03-21 11:03:02 -070061 req = skcipher_request_alloc(tfm, GFP_NOFS);
Jaegeuk Kim0b81d072015-05-15 16:26:10 -070062 if (!req) {
63 printk_ratelimited(KERN_ERR
Eric Biggers08ae8772016-11-13 20:35:52 -050064 "%s: skcipher_request_alloc() failed\n", __func__);
Jaegeuk Kim0b81d072015-05-15 16:26:10 -070065 return -ENOMEM;
66 }
Linus Torvaldsd4075742016-03-21 11:03:02 -070067 skcipher_request_set_callback(req,
Jaegeuk Kim0b81d072015-05-15 16:26:10 -070068 CRYPTO_TFM_REQ_MAY_BACKLOG | CRYPTO_TFM_REQ_MAY_SLEEP,
Gilad Ben-Yossefd0082e12017-10-18 08:00:44 +010069 crypto_req_done, &wait);
Eric Biggers50c961d2018-01-11 23:30:08 -050070 sg_init_one(&sg, out, olen);
71 skcipher_request_set_crypt(req, &sg, &sg, olen, iv);
Jaegeuk Kim0b81d072015-05-15 16:26:10 -070072
Eric Biggers08ae8772016-11-13 20:35:52 -050073 /* Do the encryption */
Gilad Ben-Yossefd0082e12017-10-18 08:00:44 +010074 res = crypto_wait_req(crypto_skcipher_encrypt(req), &wait);
Linus Torvaldsd4075742016-03-21 11:03:02 -070075 skcipher_request_free(req);
Eric Biggersef1eb3a2016-09-15 17:25:55 -040076 if (res < 0) {
Jaegeuk Kim0b81d072015-05-15 16:26:10 -070077 printk_ratelimited(KERN_ERR
78 "%s: Error (error code %d)\n", __func__, res);
Eric Biggersef1eb3a2016-09-15 17:25:55 -040079 return res;
80 }
Jaegeuk Kim0b81d072015-05-15 16:26:10 -070081
Eric Biggersef1eb3a2016-09-15 17:25:55 -040082 return 0;
Jaegeuk Kim0b81d072015-05-15 16:26:10 -070083}
84
Eric Biggersef1eb3a2016-09-15 17:25:55 -040085/**
86 * fname_decrypt() - decrypt a filename
87 *
88 * The caller must have allocated sufficient memory for the @oname string.
89 *
90 * Return: 0 on success, -errno on failure
Jaegeuk Kim0b81d072015-05-15 16:26:10 -070091 */
92static int fname_decrypt(struct inode *inode,
93 const struct fscrypt_str *iname,
94 struct fscrypt_str *oname)
95{
Linus Torvaldsd4075742016-03-21 11:03:02 -070096 struct skcipher_request *req = NULL;
Gilad Ben-Yossefd0082e12017-10-18 08:00:44 +010097 DECLARE_CRYPTO_WAIT(wait);
Jaegeuk Kim0b81d072015-05-15 16:26:10 -070098 struct scatterlist src_sg, dst_sg;
99 struct fscrypt_info *ci = inode->i_crypt_info;
Linus Torvaldsd4075742016-03-21 11:03:02 -0700100 struct crypto_skcipher *tfm = ci->ci_ctfm;
Jaegeuk Kim0b81d072015-05-15 16:26:10 -0700101 int res = 0;
102 char iv[FS_CRYPTO_BLOCK_SIZE];
103 unsigned lim;
104
105 lim = inode->i_sb->s_cop->max_namelen(inode);
106 if (iname->len <= 0 || iname->len > lim)
107 return -EIO;
108
109 /* Allocate request */
Linus Torvaldsd4075742016-03-21 11:03:02 -0700110 req = skcipher_request_alloc(tfm, GFP_NOFS);
Jaegeuk Kim0b81d072015-05-15 16:26:10 -0700111 if (!req) {
112 printk_ratelimited(KERN_ERR
113 "%s: crypto_request_alloc() failed\n", __func__);
114 return -ENOMEM;
115 }
Linus Torvaldsd4075742016-03-21 11:03:02 -0700116 skcipher_request_set_callback(req,
Jaegeuk Kim0b81d072015-05-15 16:26:10 -0700117 CRYPTO_TFM_REQ_MAY_BACKLOG | CRYPTO_TFM_REQ_MAY_SLEEP,
Gilad Ben-Yossefd0082e12017-10-18 08:00:44 +0100118 crypto_req_done, &wait);
Jaegeuk Kim0b81d072015-05-15 16:26:10 -0700119
120 /* Initialize IV */
121 memset(iv, 0, FS_CRYPTO_BLOCK_SIZE);
122
123 /* Create decryption request */
124 sg_init_one(&src_sg, iname->name, iname->len);
125 sg_init_one(&dst_sg, oname->name, oname->len);
Linus Torvaldsd4075742016-03-21 11:03:02 -0700126 skcipher_request_set_crypt(req, &src_sg, &dst_sg, iname->len, iv);
Gilad Ben-Yossefd0082e12017-10-18 08:00:44 +0100127 res = crypto_wait_req(crypto_skcipher_decrypt(req), &wait);
Linus Torvaldsd4075742016-03-21 11:03:02 -0700128 skcipher_request_free(req);
Jaegeuk Kim0b81d072015-05-15 16:26:10 -0700129 if (res < 0) {
130 printk_ratelimited(KERN_ERR
131 "%s: Error (error code %d)\n", __func__, res);
132 return res;
133 }
134
135 oname->len = strnlen(oname->name, iname->len);
Eric Biggersef1eb3a2016-09-15 17:25:55 -0400136 return 0;
Jaegeuk Kim0b81d072015-05-15 16:26:10 -0700137}
138
139static const char *lookup_table =
140 "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+,";
141
Eric Biggers17159422017-04-24 10:00:10 -0700142#define BASE64_CHARS(nbytes) DIV_ROUND_UP((nbytes) * 4, 3)
143
Jaegeuk Kim0b81d072015-05-15 16:26:10 -0700144/**
145 * digest_encode() -
146 *
147 * Encodes the input digest using characters from the set [a-zA-Z0-9_+].
148 * The encoded string is roughly 4/3 times the size of the input string.
149 */
150static int digest_encode(const char *src, int len, char *dst)
151{
152 int i = 0, bits = 0, ac = 0;
153 char *cp = dst;
154
155 while (i < len) {
156 ac += (((unsigned char) src[i]) << bits);
157 bits += 8;
158 do {
159 *cp++ = lookup_table[ac & 0x3f];
160 ac >>= 6;
161 bits -= 6;
162 } while (bits >= 6);
163 i++;
164 }
165 if (bits)
166 *cp++ = lookup_table[ac & 0x3f];
167 return cp - dst;
168}
169
170static int digest_decode(const char *src, int len, char *dst)
171{
172 int i = 0, bits = 0, ac = 0;
173 const char *p;
174 char *cp = dst;
175
176 while (i < len) {
177 p = strchr(lookup_table, src[i]);
178 if (p == NULL || src[i] == 0)
179 return -2;
180 ac += (p - lookup_table) << bits;
181 bits += 6;
182 if (bits >= 8) {
183 *cp++ = ac & 0xff;
184 ac >>= 8;
185 bits -= 8;
186 }
187 i++;
188 }
189 if (ac)
190 return -1;
191 return cp - dst;
192}
193
David Gstir0b93e1b2016-11-13 22:20:47 +0100194u32 fscrypt_fname_encrypted_size(const struct inode *inode, u32 ilen)
Jaegeuk Kim0b81d072015-05-15 16:26:10 -0700195{
196 int padding = 32;
197 struct fscrypt_info *ci = inode->i_crypt_info;
198
199 if (ci)
200 padding = 4 << (ci->ci_flags & FS_POLICY_FLAGS_PAD_MASK);
Eric Biggers55be3142016-09-30 01:46:18 -0400201 ilen = max(ilen, (u32)FS_CRYPTO_BLOCK_SIZE);
202 return round_up(ilen, padding);
Jaegeuk Kim0b81d072015-05-15 16:26:10 -0700203}
204EXPORT_SYMBOL(fscrypt_fname_encrypted_size);
205
206/**
207 * fscrypt_fname_crypto_alloc_obuff() -
208 *
209 * Allocates an output buffer that is sufficient for the crypto operation
210 * specified by the context and the direction.
211 */
David Gstir0b93e1b2016-11-13 22:20:47 +0100212int fscrypt_fname_alloc_buffer(const struct inode *inode,
Jaegeuk Kim0b81d072015-05-15 16:26:10 -0700213 u32 ilen, struct fscrypt_str *crypto_str)
214{
Eric Biggers17159422017-04-24 10:00:10 -0700215 u32 olen = fscrypt_fname_encrypted_size(inode, ilen);
216 const u32 max_encoded_len =
217 max_t(u32, BASE64_CHARS(FSCRYPT_FNAME_MAX_UNDIGESTED_SIZE),
218 1 + BASE64_CHARS(sizeof(struct fscrypt_digested_name)));
Jaegeuk Kim0b81d072015-05-15 16:26:10 -0700219
220 crypto_str->len = olen;
Eric Biggers17159422017-04-24 10:00:10 -0700221 olen = max(olen, max_encoded_len);
222
Jaegeuk Kim0b81d072015-05-15 16:26:10 -0700223 /*
224 * Allocated buffer can hold one more character to null-terminate the
225 * string
226 */
227 crypto_str->name = kmalloc(olen + 1, GFP_NOFS);
228 if (!(crypto_str->name))
229 return -ENOMEM;
230 return 0;
231}
232EXPORT_SYMBOL(fscrypt_fname_alloc_buffer);
233
234/**
235 * fscrypt_fname_crypto_free_buffer() -
236 *
237 * Frees the buffer allocated for crypto operation.
238 */
239void fscrypt_fname_free_buffer(struct fscrypt_str *crypto_str)
240{
241 if (!crypto_str)
242 return;
243 kfree(crypto_str->name);
244 crypto_str->name = NULL;
245}
246EXPORT_SYMBOL(fscrypt_fname_free_buffer);
247
248/**
249 * fscrypt_fname_disk_to_usr() - converts a filename from disk space to user
250 * space
Eric Biggersef1eb3a2016-09-15 17:25:55 -0400251 *
252 * The caller must have allocated sufficient memory for the @oname string.
253 *
Eric Biggers17159422017-04-24 10:00:10 -0700254 * If the key is available, we'll decrypt the disk name; otherwise, we'll encode
255 * it for presentation. Short names are directly base64-encoded, while long
256 * names are encoded in fscrypt_digested_name format.
257 *
Eric Biggersef1eb3a2016-09-15 17:25:55 -0400258 * Return: 0 on success, -errno on failure
Jaegeuk Kim0b81d072015-05-15 16:26:10 -0700259 */
260int fscrypt_fname_disk_to_usr(struct inode *inode,
261 u32 hash, u32 minor_hash,
262 const struct fscrypt_str *iname,
263 struct fscrypt_str *oname)
264{
265 const struct qstr qname = FSTR_TO_QSTR(iname);
Eric Biggers17159422017-04-24 10:00:10 -0700266 struct fscrypt_digested_name digested_name;
Jaegeuk Kim0b81d072015-05-15 16:26:10 -0700267
268 if (fscrypt_is_dot_dotdot(&qname)) {
269 oname->name[0] = '.';
270 oname->name[iname->len - 1] = '.';
271 oname->len = iname->len;
Eric Biggersef1eb3a2016-09-15 17:25:55 -0400272 return 0;
Jaegeuk Kim0b81d072015-05-15 16:26:10 -0700273 }
274
275 if (iname->len < FS_CRYPTO_BLOCK_SIZE)
276 return -EUCLEAN;
277
278 if (inode->i_crypt_info)
279 return fname_decrypt(inode, iname, oname);
280
Eric Biggers17159422017-04-24 10:00:10 -0700281 if (iname->len <= FSCRYPT_FNAME_MAX_UNDIGESTED_SIZE) {
Eric Biggersef1eb3a2016-09-15 17:25:55 -0400282 oname->len = digest_encode(iname->name, iname->len,
283 oname->name);
284 return 0;
Jaegeuk Kim0b81d072015-05-15 16:26:10 -0700285 }
286 if (hash) {
Eric Biggers17159422017-04-24 10:00:10 -0700287 digested_name.hash = hash;
288 digested_name.minor_hash = minor_hash;
Jaegeuk Kim0b81d072015-05-15 16:26:10 -0700289 } else {
Eric Biggers17159422017-04-24 10:00:10 -0700290 digested_name.hash = 0;
291 digested_name.minor_hash = 0;
Jaegeuk Kim0b81d072015-05-15 16:26:10 -0700292 }
Eric Biggers17159422017-04-24 10:00:10 -0700293 memcpy(digested_name.digest,
294 FSCRYPT_FNAME_DIGEST(iname->name, iname->len),
295 FSCRYPT_FNAME_DIGEST_SIZE);
Jaegeuk Kim0b81d072015-05-15 16:26:10 -0700296 oname->name[0] = '_';
Eric Biggers17159422017-04-24 10:00:10 -0700297 oname->len = 1 + digest_encode((const char *)&digested_name,
298 sizeof(digested_name), oname->name + 1);
Eric Biggersef1eb3a2016-09-15 17:25:55 -0400299 return 0;
Jaegeuk Kim0b81d072015-05-15 16:26:10 -0700300}
301EXPORT_SYMBOL(fscrypt_fname_disk_to_usr);
302
303/**
Eric Biggers17159422017-04-24 10:00:10 -0700304 * fscrypt_setup_filename() - prepare to search a possibly encrypted directory
305 * @dir: the directory that will be searched
306 * @iname: the user-provided filename being searched for
307 * @lookup: 1 if we're allowed to proceed without the key because it's
308 * ->lookup() or we're finding the dir_entry for deletion; 0 if we cannot
309 * proceed without the key because we're going to create the dir_entry.
310 * @fname: the filename information to be filled in
311 *
312 * Given a user-provided filename @iname, this function sets @fname->disk_name
313 * to the name that would be stored in the on-disk directory entry, if possible.
314 * If the directory is unencrypted this is simply @iname. Else, if we have the
315 * directory's encryption key, then @iname is the plaintext, so we encrypt it to
316 * get the disk_name.
317 *
318 * Else, for keyless @lookup operations, @iname is the presented ciphertext, so
319 * we decode it to get either the ciphertext disk_name (for short names) or the
320 * fscrypt_digested_name (for long names). Non-@lookup operations will be
321 * impossible in this case, so we fail them with ENOKEY.
322 *
323 * If successful, fscrypt_free_filename() must be called later to clean up.
324 *
325 * Return: 0 on success, -errno on failure
326 */
Jaegeuk Kim0b81d072015-05-15 16:26:10 -0700327int fscrypt_setup_filename(struct inode *dir, const struct qstr *iname,
328 int lookup, struct fscrypt_name *fname)
329{
Eric Biggers17159422017-04-24 10:00:10 -0700330 int ret;
331 int digested;
Jaegeuk Kim0b81d072015-05-15 16:26:10 -0700332
333 memset(fname, 0, sizeof(struct fscrypt_name));
334 fname->usr_fname = iname;
335
Eric Biggerse0428a22017-10-09 12:15:36 -0700336 if (!IS_ENCRYPTED(dir) || fscrypt_is_dot_dotdot(iname)) {
Jaegeuk Kim0b81d072015-05-15 16:26:10 -0700337 fname->disk_name.name = (unsigned char *)iname->name;
338 fname->disk_name.len = iname->len;
339 return 0;
340 }
Eric Biggers1b53cf92017-02-21 15:07:11 -0800341 ret = fscrypt_get_encryption_info(dir);
Jaegeuk Kim0b81d072015-05-15 16:26:10 -0700342 if (ret && ret != -EOPNOTSUPP)
343 return ret;
344
345 if (dir->i_crypt_info) {
Eric Biggers50c961d2018-01-11 23:30:08 -0500346 unsigned int max_len = dir->i_sb->s_cop->max_namelen(dir);
347
348 if (iname->len > max_len)
349 return -ENAMETOOLONG;
350
351 fname->crypto_buf.len =
352 min(fscrypt_fname_encrypted_size(dir, iname->len),
353 max_len);
354 fname->crypto_buf.name = kmalloc(fname->crypto_buf.len,
355 GFP_NOFS);
356 if (!fname->crypto_buf.name)
357 return -ENOMEM;
358
359 ret = fname_encrypt(dir, iname, fname->crypto_buf.name,
360 fname->crypto_buf.len);
Eric Biggersef1eb3a2016-09-15 17:25:55 -0400361 if (ret)
Jaegeuk Kim0b81d072015-05-15 16:26:10 -0700362 goto errout;
363 fname->disk_name.name = fname->crypto_buf.name;
364 fname->disk_name.len = fname->crypto_buf.len;
365 return 0;
366 }
367 if (!lookup)
Eric Biggers54475f52016-12-05 11:12:44 -0800368 return -ENOKEY;
Jaegeuk Kim0b81d072015-05-15 16:26:10 -0700369
370 /*
371 * We don't have the key and we are doing a lookup; decode the
372 * user-supplied name
373 */
Eric Biggers17159422017-04-24 10:00:10 -0700374 if (iname->name[0] == '_') {
375 if (iname->len !=
376 1 + BASE64_CHARS(sizeof(struct fscrypt_digested_name)))
377 return -ENOENT;
378 digested = 1;
379 } else {
380 if (iname->len >
381 BASE64_CHARS(FSCRYPT_FNAME_MAX_UNDIGESTED_SIZE))
382 return -ENOENT;
383 digested = 0;
384 }
Jaegeuk Kim0b81d072015-05-15 16:26:10 -0700385
Eric Biggers17159422017-04-24 10:00:10 -0700386 fname->crypto_buf.name =
387 kmalloc(max_t(size_t, FSCRYPT_FNAME_MAX_UNDIGESTED_SIZE,
388 sizeof(struct fscrypt_digested_name)),
389 GFP_KERNEL);
Jaegeuk Kim0b81d072015-05-15 16:26:10 -0700390 if (fname->crypto_buf.name == NULL)
391 return -ENOMEM;
392
Eric Biggers17159422017-04-24 10:00:10 -0700393 ret = digest_decode(iname->name + digested, iname->len - digested,
Jaegeuk Kim0b81d072015-05-15 16:26:10 -0700394 fname->crypto_buf.name);
395 if (ret < 0) {
396 ret = -ENOENT;
397 goto errout;
398 }
399 fname->crypto_buf.len = ret;
Eric Biggers17159422017-04-24 10:00:10 -0700400 if (digested) {
401 const struct fscrypt_digested_name *n =
402 (const void *)fname->crypto_buf.name;
403 fname->hash = n->hash;
404 fname->minor_hash = n->minor_hash;
Jaegeuk Kim0b81d072015-05-15 16:26:10 -0700405 } else {
406 fname->disk_name.name = fname->crypto_buf.name;
407 fname->disk_name.len = fname->crypto_buf.len;
408 }
409 return 0;
410
411errout:
Eric Biggers50c961d2018-01-11 23:30:08 -0500412 kfree(fname->crypto_buf.name);
Jaegeuk Kim0b81d072015-05-15 16:26:10 -0700413 return ret;
414}
415EXPORT_SYMBOL(fscrypt_setup_filename);