blob: 3b5164b159cba2f9b48e7596f57fd258756de7d2 [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);
Eric Biggersc90fd7752018-04-30 15:51:38 -070062 if (!req)
Jaegeuk Kim0b81d072015-05-15 16:26:10 -070063 return -ENOMEM;
Linus Torvaldsd4075742016-03-21 11:03:02 -070064 skcipher_request_set_callback(req,
Jaegeuk Kim0b81d072015-05-15 16:26:10 -070065 CRYPTO_TFM_REQ_MAY_BACKLOG | CRYPTO_TFM_REQ_MAY_SLEEP,
Gilad Ben-Yossefd0082e12017-10-18 08:00:44 +010066 crypto_req_done, &wait);
Eric Biggers50c961d2018-01-11 23:30:08 -050067 sg_init_one(&sg, out, olen);
68 skcipher_request_set_crypt(req, &sg, &sg, olen, iv);
Jaegeuk Kim0b81d072015-05-15 16:26:10 -070069
Eric Biggers08ae8772016-11-13 20:35:52 -050070 /* Do the encryption */
Gilad Ben-Yossefd0082e12017-10-18 08:00:44 +010071 res = crypto_wait_req(crypto_skcipher_encrypt(req), &wait);
Linus Torvaldsd4075742016-03-21 11:03:02 -070072 skcipher_request_free(req);
Eric Biggersef1eb3a2016-09-15 17:25:55 -040073 if (res < 0) {
Jaegeuk Kim0b81d072015-05-15 16:26:10 -070074 printk_ratelimited(KERN_ERR
75 "%s: Error (error code %d)\n", __func__, res);
Eric Biggersef1eb3a2016-09-15 17:25:55 -040076 return res;
77 }
Jaegeuk Kim0b81d072015-05-15 16:26:10 -070078
Eric Biggersef1eb3a2016-09-15 17:25:55 -040079 return 0;
Jaegeuk Kim0b81d072015-05-15 16:26:10 -070080}
81
Eric Biggersef1eb3a2016-09-15 17:25:55 -040082/**
83 * fname_decrypt() - decrypt a filename
84 *
85 * The caller must have allocated sufficient memory for the @oname string.
86 *
87 * Return: 0 on success, -errno on failure
Jaegeuk Kim0b81d072015-05-15 16:26:10 -070088 */
89static int fname_decrypt(struct inode *inode,
90 const struct fscrypt_str *iname,
91 struct fscrypt_str *oname)
92{
Linus Torvaldsd4075742016-03-21 11:03:02 -070093 struct skcipher_request *req = NULL;
Gilad Ben-Yossefd0082e12017-10-18 08:00:44 +010094 DECLARE_CRYPTO_WAIT(wait);
Jaegeuk Kim0b81d072015-05-15 16:26:10 -070095 struct scatterlist src_sg, dst_sg;
96 struct fscrypt_info *ci = inode->i_crypt_info;
Linus Torvaldsd4075742016-03-21 11:03:02 -070097 struct crypto_skcipher *tfm = ci->ci_ctfm;
Jaegeuk Kim0b81d072015-05-15 16:26:10 -070098 int res = 0;
99 char iv[FS_CRYPTO_BLOCK_SIZE];
100 unsigned lim;
101
102 lim = inode->i_sb->s_cop->max_namelen(inode);
103 if (iname->len <= 0 || iname->len > lim)
104 return -EIO;
105
106 /* Allocate request */
Linus Torvaldsd4075742016-03-21 11:03:02 -0700107 req = skcipher_request_alloc(tfm, GFP_NOFS);
Eric Biggersc90fd7752018-04-30 15:51:38 -0700108 if (!req)
Jaegeuk Kim0b81d072015-05-15 16:26:10 -0700109 return -ENOMEM;
Linus Torvaldsd4075742016-03-21 11:03:02 -0700110 skcipher_request_set_callback(req,
Jaegeuk Kim0b81d072015-05-15 16:26:10 -0700111 CRYPTO_TFM_REQ_MAY_BACKLOG | CRYPTO_TFM_REQ_MAY_SLEEP,
Gilad Ben-Yossefd0082e12017-10-18 08:00:44 +0100112 crypto_req_done, &wait);
Jaegeuk Kim0b81d072015-05-15 16:26:10 -0700113
114 /* Initialize IV */
115 memset(iv, 0, FS_CRYPTO_BLOCK_SIZE);
116
117 /* Create decryption request */
118 sg_init_one(&src_sg, iname->name, iname->len);
119 sg_init_one(&dst_sg, oname->name, oname->len);
Linus Torvaldsd4075742016-03-21 11:03:02 -0700120 skcipher_request_set_crypt(req, &src_sg, &dst_sg, iname->len, iv);
Gilad Ben-Yossefd0082e12017-10-18 08:00:44 +0100121 res = crypto_wait_req(crypto_skcipher_decrypt(req), &wait);
Linus Torvaldsd4075742016-03-21 11:03:02 -0700122 skcipher_request_free(req);
Jaegeuk Kim0b81d072015-05-15 16:26:10 -0700123 if (res < 0) {
124 printk_ratelimited(KERN_ERR
125 "%s: Error (error code %d)\n", __func__, res);
126 return res;
127 }
128
129 oname->len = strnlen(oname->name, iname->len);
Eric Biggersef1eb3a2016-09-15 17:25:55 -0400130 return 0;
Jaegeuk Kim0b81d072015-05-15 16:26:10 -0700131}
132
133static const char *lookup_table =
134 "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+,";
135
Eric Biggers17159422017-04-24 10:00:10 -0700136#define BASE64_CHARS(nbytes) DIV_ROUND_UP((nbytes) * 4, 3)
137
Jaegeuk Kim0b81d072015-05-15 16:26:10 -0700138/**
139 * digest_encode() -
140 *
141 * Encodes the input digest using characters from the set [a-zA-Z0-9_+].
142 * The encoded string is roughly 4/3 times the size of the input string.
143 */
144static int digest_encode(const char *src, int len, char *dst)
145{
146 int i = 0, bits = 0, ac = 0;
147 char *cp = dst;
148
149 while (i < len) {
150 ac += (((unsigned char) src[i]) << bits);
151 bits += 8;
152 do {
153 *cp++ = lookup_table[ac & 0x3f];
154 ac >>= 6;
155 bits -= 6;
156 } while (bits >= 6);
157 i++;
158 }
159 if (bits)
160 *cp++ = lookup_table[ac & 0x3f];
161 return cp - dst;
162}
163
164static int digest_decode(const char *src, int len, char *dst)
165{
166 int i = 0, bits = 0, ac = 0;
167 const char *p;
168 char *cp = dst;
169
170 while (i < len) {
171 p = strchr(lookup_table, src[i]);
172 if (p == NULL || src[i] == 0)
173 return -2;
174 ac += (p - lookup_table) << bits;
175 bits += 6;
176 if (bits >= 8) {
177 *cp++ = ac & 0xff;
178 ac >>= 8;
179 bits -= 8;
180 }
181 i++;
182 }
183 if (ac)
184 return -1;
185 return cp - dst;
186}
187
Eric Biggersb9db0b42018-01-11 23:30:08 -0500188bool fscrypt_fname_encrypted_size(const struct inode *inode, u32 orig_len,
189 u32 max_len, u32 *encrypted_len_ret)
Jaegeuk Kim0b81d072015-05-15 16:26:10 -0700190{
Eric Biggersb9db0b42018-01-11 23:30:08 -0500191 int padding = 4 << (inode->i_crypt_info->ci_flags &
192 FS_POLICY_FLAGS_PAD_MASK);
193 u32 encrypted_len;
Jaegeuk Kim0b81d072015-05-15 16:26:10 -0700194
Eric Biggersb9db0b42018-01-11 23:30:08 -0500195 if (orig_len > max_len)
196 return false;
197 encrypted_len = max(orig_len, (u32)FS_CRYPTO_BLOCK_SIZE);
198 encrypted_len = round_up(encrypted_len, padding);
199 *encrypted_len_ret = min(encrypted_len, max_len);
200 return true;
Jaegeuk Kim0b81d072015-05-15 16:26:10 -0700201}
Jaegeuk Kim0b81d072015-05-15 16:26:10 -0700202
203/**
Eric Biggers2cbadad2018-01-11 23:30:08 -0500204 * fscrypt_fname_alloc_buffer - allocate a buffer for presented filenames
Jaegeuk Kim0b81d072015-05-15 16:26:10 -0700205 *
Eric Biggers2cbadad2018-01-11 23:30:08 -0500206 * Allocate a buffer that is large enough to hold any decrypted or encoded
207 * filename (null-terminated), for the given maximum encrypted filename length.
208 *
209 * Return: 0 on success, -errno on failure
Jaegeuk Kim0b81d072015-05-15 16:26:10 -0700210 */
David Gstir0b93e1b2016-11-13 22:20:47 +0100211int fscrypt_fname_alloc_buffer(const struct inode *inode,
Eric Biggers2cbadad2018-01-11 23:30:08 -0500212 u32 max_encrypted_len,
213 struct fscrypt_str *crypto_str)
Jaegeuk Kim0b81d072015-05-15 16:26:10 -0700214{
Eric Biggers17159422017-04-24 10:00:10 -0700215 const u32 max_encoded_len =
216 max_t(u32, BASE64_CHARS(FSCRYPT_FNAME_MAX_UNDIGESTED_SIZE),
217 1 + BASE64_CHARS(sizeof(struct fscrypt_digested_name)));
Eric Biggers2cbadad2018-01-11 23:30:08 -0500218 u32 max_presented_len;
Jaegeuk Kim0b81d072015-05-15 16:26:10 -0700219
Eric Biggers2cbadad2018-01-11 23:30:08 -0500220 max_presented_len = max(max_encoded_len, max_encrypted_len);
Eric Biggers17159422017-04-24 10:00:10 -0700221
Eric Biggers2cbadad2018-01-11 23:30:08 -0500222 crypto_str->name = kmalloc(max_presented_len + 1, GFP_NOFS);
223 if (!crypto_str->name)
Jaegeuk Kim0b81d072015-05-15 16:26:10 -0700224 return -ENOMEM;
Eric Biggers2cbadad2018-01-11 23:30:08 -0500225 crypto_str->len = max_presented_len;
Jaegeuk Kim0b81d072015-05-15 16:26:10 -0700226 return 0;
227}
228EXPORT_SYMBOL(fscrypt_fname_alloc_buffer);
229
230/**
Eric Biggers2cbadad2018-01-11 23:30:08 -0500231 * fscrypt_fname_free_buffer - free the buffer for presented filenames
Jaegeuk Kim0b81d072015-05-15 16:26:10 -0700232 *
Eric Biggers2cbadad2018-01-11 23:30:08 -0500233 * Free the buffer allocated by fscrypt_fname_alloc_buffer().
Jaegeuk Kim0b81d072015-05-15 16:26:10 -0700234 */
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 *
Eric Biggers17159422017-04-24 10:00:10 -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);
Eric Biggers17159422017-04-24 10:00:10 -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
Eric Biggers17159422017-04-24 10:00:10 -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) {
Eric Biggers17159422017-04-24 10:00:10 -0700283 digested_name.hash = hash;
284 digested_name.minor_hash = minor_hash;
Jaegeuk Kim0b81d072015-05-15 16:26:10 -0700285 } else {
Eric Biggers17159422017-04-24 10:00:10 -0700286 digested_name.hash = 0;
287 digested_name.minor_hash = 0;
Jaegeuk Kim0b81d072015-05-15 16:26:10 -0700288 }
Eric Biggers17159422017-04-24 10:00:10 -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] = '_';
Eric Biggers17159422017-04-24 10:00:10 -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/**
Eric Biggers17159422017-04-24 10:00:10 -0700300 * fscrypt_setup_filename() - prepare to search a possibly encrypted directory
301 * @dir: the directory that will be searched
302 * @iname: the user-provided filename being searched for
303 * @lookup: 1 if we're allowed to proceed without the key because it's
304 * ->lookup() or we're finding the dir_entry for deletion; 0 if we cannot
305 * proceed without the key because we're going to create the dir_entry.
306 * @fname: the filename information to be filled in
307 *
308 * Given a user-provided filename @iname, this function sets @fname->disk_name
309 * to the name that would be stored in the on-disk directory entry, if possible.
310 * If the directory is unencrypted this is simply @iname. Else, if we have the
311 * directory's encryption key, then @iname is the plaintext, so we encrypt it to
312 * get the disk_name.
313 *
314 * Else, for keyless @lookup operations, @iname is the presented ciphertext, so
315 * we decode it to get either the ciphertext disk_name (for short names) or the
316 * fscrypt_digested_name (for long names). Non-@lookup operations will be
317 * impossible in this case, so we fail them with ENOKEY.
318 *
319 * If successful, fscrypt_free_filename() must be called later to clean up.
320 *
321 * Return: 0 on success, -errno on failure
322 */
Jaegeuk Kim0b81d072015-05-15 16:26:10 -0700323int fscrypt_setup_filename(struct inode *dir, const struct qstr *iname,
324 int lookup, struct fscrypt_name *fname)
325{
Eric Biggers17159422017-04-24 10:00:10 -0700326 int ret;
327 int digested;
Jaegeuk Kim0b81d072015-05-15 16:26:10 -0700328
329 memset(fname, 0, sizeof(struct fscrypt_name));
330 fname->usr_fname = iname;
331
Eric Biggerse0428a22017-10-09 12:15:36 -0700332 if (!IS_ENCRYPTED(dir) || fscrypt_is_dot_dotdot(iname)) {
Jaegeuk Kim0b81d072015-05-15 16:26:10 -0700333 fname->disk_name.name = (unsigned char *)iname->name;
334 fname->disk_name.len = iname->len;
335 return 0;
336 }
Eric Biggers1b53cf92017-02-21 15:07:11 -0800337 ret = fscrypt_get_encryption_info(dir);
Jaegeuk Kim0b81d072015-05-15 16:26:10 -0700338 if (ret && ret != -EOPNOTSUPP)
339 return ret;
340
341 if (dir->i_crypt_info) {
Eric Biggersb9db0b42018-01-11 23:30:08 -0500342 if (!fscrypt_fname_encrypted_size(dir, iname->len,
343 dir->i_sb->s_cop->max_namelen(dir),
344 &fname->crypto_buf.len))
Eric Biggers50c961d2018-01-11 23:30:08 -0500345 return -ENAMETOOLONG;
Eric Biggers50c961d2018-01-11 23:30:08 -0500346 fname->crypto_buf.name = kmalloc(fname->crypto_buf.len,
347 GFP_NOFS);
348 if (!fname->crypto_buf.name)
349 return -ENOMEM;
350
351 ret = fname_encrypt(dir, iname, fname->crypto_buf.name,
352 fname->crypto_buf.len);
Eric Biggersef1eb3a2016-09-15 17:25:55 -0400353 if (ret)
Jaegeuk Kim0b81d072015-05-15 16:26:10 -0700354 goto errout;
355 fname->disk_name.name = fname->crypto_buf.name;
356 fname->disk_name.len = fname->crypto_buf.len;
357 return 0;
358 }
359 if (!lookup)
Eric Biggers54475f52016-12-05 11:12:44 -0800360 return -ENOKEY;
Jaegeuk Kim0b81d072015-05-15 16:26:10 -0700361
362 /*
363 * We don't have the key and we are doing a lookup; decode the
364 * user-supplied name
365 */
Eric Biggers17159422017-04-24 10:00:10 -0700366 if (iname->name[0] == '_') {
367 if (iname->len !=
368 1 + BASE64_CHARS(sizeof(struct fscrypt_digested_name)))
369 return -ENOENT;
370 digested = 1;
371 } else {
372 if (iname->len >
373 BASE64_CHARS(FSCRYPT_FNAME_MAX_UNDIGESTED_SIZE))
374 return -ENOENT;
375 digested = 0;
376 }
Jaegeuk Kim0b81d072015-05-15 16:26:10 -0700377
Eric Biggers17159422017-04-24 10:00:10 -0700378 fname->crypto_buf.name =
379 kmalloc(max_t(size_t, FSCRYPT_FNAME_MAX_UNDIGESTED_SIZE,
380 sizeof(struct fscrypt_digested_name)),
381 GFP_KERNEL);
Jaegeuk Kim0b81d072015-05-15 16:26:10 -0700382 if (fname->crypto_buf.name == NULL)
383 return -ENOMEM;
384
Eric Biggers17159422017-04-24 10:00:10 -0700385 ret = digest_decode(iname->name + digested, iname->len - digested,
Jaegeuk Kim0b81d072015-05-15 16:26:10 -0700386 fname->crypto_buf.name);
387 if (ret < 0) {
388 ret = -ENOENT;
389 goto errout;
390 }
391 fname->crypto_buf.len = ret;
Eric Biggers17159422017-04-24 10:00:10 -0700392 if (digested) {
393 const struct fscrypt_digested_name *n =
394 (const void *)fname->crypto_buf.name;
395 fname->hash = n->hash;
396 fname->minor_hash = n->minor_hash;
Jaegeuk Kim0b81d072015-05-15 16:26:10 -0700397 } else {
398 fname->disk_name.name = fname->crypto_buf.name;
399 fname->disk_name.len = fname->crypto_buf.len;
400 }
401 return 0;
402
403errout:
Eric Biggers50c961d2018-01-11 23:30:08 -0500404 kfree(fname->crypto_buf.name);
Jaegeuk Kim0b81d072015-05-15 16:26:10 -0700405 return ret;
406}
407EXPORT_SYMBOL(fscrypt_setup_filename);