blob: e33f3d3c5ade8ce6e647e6b5021f7e51f00a96c1 [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
Eric Biggersb9db0b42018-01-11 23:30:08 -0500194bool fscrypt_fname_encrypted_size(const struct inode *inode, u32 orig_len,
195 u32 max_len, u32 *encrypted_len_ret)
Jaegeuk Kim0b81d072015-05-15 16:26:10 -0700196{
Eric Biggersb9db0b42018-01-11 23:30:08 -0500197 int padding = 4 << (inode->i_crypt_info->ci_flags &
198 FS_POLICY_FLAGS_PAD_MASK);
199 u32 encrypted_len;
Jaegeuk Kim0b81d072015-05-15 16:26:10 -0700200
Eric Biggersb9db0b42018-01-11 23:30:08 -0500201 if (orig_len > max_len)
202 return false;
203 encrypted_len = max(orig_len, (u32)FS_CRYPTO_BLOCK_SIZE);
204 encrypted_len = round_up(encrypted_len, padding);
205 *encrypted_len_ret = min(encrypted_len, max_len);
206 return true;
Jaegeuk Kim0b81d072015-05-15 16:26:10 -0700207}
Jaegeuk Kim0b81d072015-05-15 16:26:10 -0700208
209/**
Eric Biggers2cbadad2018-01-11 23:30:08 -0500210 * fscrypt_fname_alloc_buffer - allocate a buffer for presented filenames
Jaegeuk Kim0b81d072015-05-15 16:26:10 -0700211 *
Eric Biggers2cbadad2018-01-11 23:30:08 -0500212 * Allocate a buffer that is large enough to hold any decrypted or encoded
213 * filename (null-terminated), for the given maximum encrypted filename length.
214 *
215 * Return: 0 on success, -errno on failure
Jaegeuk Kim0b81d072015-05-15 16:26:10 -0700216 */
David Gstir0b93e1b2016-11-13 22:20:47 +0100217int fscrypt_fname_alloc_buffer(const struct inode *inode,
Eric Biggers2cbadad2018-01-11 23:30:08 -0500218 u32 max_encrypted_len,
219 struct fscrypt_str *crypto_str)
Jaegeuk Kim0b81d072015-05-15 16:26:10 -0700220{
Eric Biggers17159422017-04-24 10:00:10 -0700221 const u32 max_encoded_len =
222 max_t(u32, BASE64_CHARS(FSCRYPT_FNAME_MAX_UNDIGESTED_SIZE),
223 1 + BASE64_CHARS(sizeof(struct fscrypt_digested_name)));
Eric Biggers2cbadad2018-01-11 23:30:08 -0500224 u32 max_presented_len;
Jaegeuk Kim0b81d072015-05-15 16:26:10 -0700225
Eric Biggers2cbadad2018-01-11 23:30:08 -0500226 max_presented_len = max(max_encoded_len, max_encrypted_len);
Eric Biggers17159422017-04-24 10:00:10 -0700227
Eric Biggers2cbadad2018-01-11 23:30:08 -0500228 crypto_str->name = kmalloc(max_presented_len + 1, GFP_NOFS);
229 if (!crypto_str->name)
Jaegeuk Kim0b81d072015-05-15 16:26:10 -0700230 return -ENOMEM;
Eric Biggers2cbadad2018-01-11 23:30:08 -0500231 crypto_str->len = max_presented_len;
Jaegeuk Kim0b81d072015-05-15 16:26:10 -0700232 return 0;
233}
234EXPORT_SYMBOL(fscrypt_fname_alloc_buffer);
235
236/**
Eric Biggers2cbadad2018-01-11 23:30:08 -0500237 * fscrypt_fname_free_buffer - free the buffer for presented filenames
Jaegeuk Kim0b81d072015-05-15 16:26:10 -0700238 *
Eric Biggers2cbadad2018-01-11 23:30:08 -0500239 * Free the buffer allocated by fscrypt_fname_alloc_buffer().
Jaegeuk Kim0b81d072015-05-15 16:26:10 -0700240 */
241void fscrypt_fname_free_buffer(struct fscrypt_str *crypto_str)
242{
243 if (!crypto_str)
244 return;
245 kfree(crypto_str->name);
246 crypto_str->name = NULL;
247}
248EXPORT_SYMBOL(fscrypt_fname_free_buffer);
249
250/**
251 * fscrypt_fname_disk_to_usr() - converts a filename from disk space to user
252 * space
Eric Biggersef1eb3a2016-09-15 17:25:55 -0400253 *
254 * The caller must have allocated sufficient memory for the @oname string.
255 *
Eric Biggers17159422017-04-24 10:00:10 -0700256 * If the key is available, we'll decrypt the disk name; otherwise, we'll encode
257 * it for presentation. Short names are directly base64-encoded, while long
258 * names are encoded in fscrypt_digested_name format.
259 *
Eric Biggersef1eb3a2016-09-15 17:25:55 -0400260 * Return: 0 on success, -errno on failure
Jaegeuk Kim0b81d072015-05-15 16:26:10 -0700261 */
262int fscrypt_fname_disk_to_usr(struct inode *inode,
263 u32 hash, u32 minor_hash,
264 const struct fscrypt_str *iname,
265 struct fscrypt_str *oname)
266{
267 const struct qstr qname = FSTR_TO_QSTR(iname);
Eric Biggers17159422017-04-24 10:00:10 -0700268 struct fscrypt_digested_name digested_name;
Jaegeuk Kim0b81d072015-05-15 16:26:10 -0700269
270 if (fscrypt_is_dot_dotdot(&qname)) {
271 oname->name[0] = '.';
272 oname->name[iname->len - 1] = '.';
273 oname->len = iname->len;
Eric Biggersef1eb3a2016-09-15 17:25:55 -0400274 return 0;
Jaegeuk Kim0b81d072015-05-15 16:26:10 -0700275 }
276
277 if (iname->len < FS_CRYPTO_BLOCK_SIZE)
278 return -EUCLEAN;
279
280 if (inode->i_crypt_info)
281 return fname_decrypt(inode, iname, oname);
282
Eric Biggers17159422017-04-24 10:00:10 -0700283 if (iname->len <= FSCRYPT_FNAME_MAX_UNDIGESTED_SIZE) {
Eric Biggersef1eb3a2016-09-15 17:25:55 -0400284 oname->len = digest_encode(iname->name, iname->len,
285 oname->name);
286 return 0;
Jaegeuk Kim0b81d072015-05-15 16:26:10 -0700287 }
288 if (hash) {
Eric Biggers17159422017-04-24 10:00:10 -0700289 digested_name.hash = hash;
290 digested_name.minor_hash = minor_hash;
Jaegeuk Kim0b81d072015-05-15 16:26:10 -0700291 } else {
Eric Biggers17159422017-04-24 10:00:10 -0700292 digested_name.hash = 0;
293 digested_name.minor_hash = 0;
Jaegeuk Kim0b81d072015-05-15 16:26:10 -0700294 }
Eric Biggers17159422017-04-24 10:00:10 -0700295 memcpy(digested_name.digest,
296 FSCRYPT_FNAME_DIGEST(iname->name, iname->len),
297 FSCRYPT_FNAME_DIGEST_SIZE);
Jaegeuk Kim0b81d072015-05-15 16:26:10 -0700298 oname->name[0] = '_';
Eric Biggers17159422017-04-24 10:00:10 -0700299 oname->len = 1 + digest_encode((const char *)&digested_name,
300 sizeof(digested_name), oname->name + 1);
Eric Biggersef1eb3a2016-09-15 17:25:55 -0400301 return 0;
Jaegeuk Kim0b81d072015-05-15 16:26:10 -0700302}
303EXPORT_SYMBOL(fscrypt_fname_disk_to_usr);
304
305/**
Eric Biggers17159422017-04-24 10:00:10 -0700306 * fscrypt_setup_filename() - prepare to search a possibly encrypted directory
307 * @dir: the directory that will be searched
308 * @iname: the user-provided filename being searched for
309 * @lookup: 1 if we're allowed to proceed without the key because it's
310 * ->lookup() or we're finding the dir_entry for deletion; 0 if we cannot
311 * proceed without the key because we're going to create the dir_entry.
312 * @fname: the filename information to be filled in
313 *
314 * Given a user-provided filename @iname, this function sets @fname->disk_name
315 * to the name that would be stored in the on-disk directory entry, if possible.
316 * If the directory is unencrypted this is simply @iname. Else, if we have the
317 * directory's encryption key, then @iname is the plaintext, so we encrypt it to
318 * get the disk_name.
319 *
320 * Else, for keyless @lookup operations, @iname is the presented ciphertext, so
321 * we decode it to get either the ciphertext disk_name (for short names) or the
322 * fscrypt_digested_name (for long names). Non-@lookup operations will be
323 * impossible in this case, so we fail them with ENOKEY.
324 *
325 * If successful, fscrypt_free_filename() must be called later to clean up.
326 *
327 * Return: 0 on success, -errno on failure
328 */
Jaegeuk Kim0b81d072015-05-15 16:26:10 -0700329int fscrypt_setup_filename(struct inode *dir, const struct qstr *iname,
330 int lookup, struct fscrypt_name *fname)
331{
Eric Biggers17159422017-04-24 10:00:10 -0700332 int ret;
333 int digested;
Jaegeuk Kim0b81d072015-05-15 16:26:10 -0700334
335 memset(fname, 0, sizeof(struct fscrypt_name));
336 fname->usr_fname = iname;
337
Eric Biggerse0428a22017-10-09 12:15:36 -0700338 if (!IS_ENCRYPTED(dir) || fscrypt_is_dot_dotdot(iname)) {
Jaegeuk Kim0b81d072015-05-15 16:26:10 -0700339 fname->disk_name.name = (unsigned char *)iname->name;
340 fname->disk_name.len = iname->len;
341 return 0;
342 }
Eric Biggers1b53cf92017-02-21 15:07:11 -0800343 ret = fscrypt_get_encryption_info(dir);
Jaegeuk Kim0b81d072015-05-15 16:26:10 -0700344 if (ret && ret != -EOPNOTSUPP)
345 return ret;
346
347 if (dir->i_crypt_info) {
Eric Biggersb9db0b42018-01-11 23:30:08 -0500348 if (!fscrypt_fname_encrypted_size(dir, iname->len,
349 dir->i_sb->s_cop->max_namelen(dir),
350 &fname->crypto_buf.len))
Eric Biggers50c961d2018-01-11 23:30:08 -0500351 return -ENAMETOOLONG;
Eric Biggers50c961d2018-01-11 23:30:08 -0500352 fname->crypto_buf.name = kmalloc(fname->crypto_buf.len,
353 GFP_NOFS);
354 if (!fname->crypto_buf.name)
355 return -ENOMEM;
356
357 ret = fname_encrypt(dir, iname, fname->crypto_buf.name,
358 fname->crypto_buf.len);
Eric Biggersef1eb3a2016-09-15 17:25:55 -0400359 if (ret)
Jaegeuk Kim0b81d072015-05-15 16:26:10 -0700360 goto errout;
361 fname->disk_name.name = fname->crypto_buf.name;
362 fname->disk_name.len = fname->crypto_buf.len;
363 return 0;
364 }
365 if (!lookup)
Eric Biggers54475f52016-12-05 11:12:44 -0800366 return -ENOKEY;
Jaegeuk Kim0b81d072015-05-15 16:26:10 -0700367
368 /*
369 * We don't have the key and we are doing a lookup; decode the
370 * user-supplied name
371 */
Eric Biggers17159422017-04-24 10:00:10 -0700372 if (iname->name[0] == '_') {
373 if (iname->len !=
374 1 + BASE64_CHARS(sizeof(struct fscrypt_digested_name)))
375 return -ENOENT;
376 digested = 1;
377 } else {
378 if (iname->len >
379 BASE64_CHARS(FSCRYPT_FNAME_MAX_UNDIGESTED_SIZE))
380 return -ENOENT;
381 digested = 0;
382 }
Jaegeuk Kim0b81d072015-05-15 16:26:10 -0700383
Eric Biggers17159422017-04-24 10:00:10 -0700384 fname->crypto_buf.name =
385 kmalloc(max_t(size_t, FSCRYPT_FNAME_MAX_UNDIGESTED_SIZE,
386 sizeof(struct fscrypt_digested_name)),
387 GFP_KERNEL);
Jaegeuk Kim0b81d072015-05-15 16:26:10 -0700388 if (fname->crypto_buf.name == NULL)
389 return -ENOMEM;
390
Eric Biggers17159422017-04-24 10:00:10 -0700391 ret = digest_decode(iname->name + digested, iname->len - digested,
Jaegeuk Kim0b81d072015-05-15 16:26:10 -0700392 fname->crypto_buf.name);
393 if (ret < 0) {
394 ret = -ENOENT;
395 goto errout;
396 }
397 fname->crypto_buf.len = ret;
Eric Biggers17159422017-04-24 10:00:10 -0700398 if (digested) {
399 const struct fscrypt_digested_name *n =
400 (const void *)fname->crypto_buf.name;
401 fname->hash = n->hash;
402 fname->minor_hash = n->minor_hash;
Jaegeuk Kim0b81d072015-05-15 16:26:10 -0700403 } else {
404 fname->disk_name.name = fname->crypto_buf.name;
405 fname->disk_name.len = fname->crypto_buf.len;
406 }
407 return 0;
408
409errout:
Eric Biggers50c961d2018-01-11 23:30:08 -0500410 kfree(fname->crypto_buf.name);
Jaegeuk Kim0b81d072015-05-15 16:26:10 -0700411 return ret;
412}
413EXPORT_SYMBOL(fscrypt_setup_filename);