blob: a9be4bc74a94a02342ce0e1309fb0567605379c1 [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
Eric Biggers2ebdef62019-12-09 12:43:59 -080014#include <linux/namei.h>
Jaegeuk Kim0b81d072015-05-15 16:26:10 -070015#include <linux/scatterlist.h>
Daniel Rosenbergedc440e2020-01-20 14:32:01 -080016#include <crypto/hash.h>
Eric Biggersa24d22b2020-11-12 21:20:21 -080017#include <crypto/sha2.h>
Eric Biggersa5757842018-01-05 10:45:00 -080018#include <crypto/skcipher.h>
Theodore Ts'o3325bea2016-11-26 20:32:46 -050019#include "fscrypt_private.h"
Jaegeuk Kim0b81d072015-05-15 16:26:10 -070020
Eric Biggersd2fe9752020-05-11 12:13:56 -070021/*
Daniel Rosenbergedc440e2020-01-20 14:32:01 -080022 * struct fscrypt_nokey_name - identifier for directory entry when key is absent
23 *
24 * When userspace lists an encrypted directory without access to the key, the
25 * filesystem must present a unique "no-key name" for each filename that allows
26 * it to find the directory entry again if requested. Naively, that would just
27 * mean using the ciphertext filenames. However, since the ciphertext filenames
28 * can contain illegal characters ('\0' and '/'), they must be encoded in some
Eric Biggersba47b512021-07-17 19:01:25 -050029 * way. We use base64url. But that can cause names to exceed NAME_MAX (255
Daniel Rosenbergedc440e2020-01-20 14:32:01 -080030 * bytes), so we also need to use a strong hash to abbreviate long names.
31 *
32 * The filesystem may also need another kind of hash, the "dirhash", to quickly
33 * find the directory entry. Since filesystems normally compute the dirhash
34 * over the on-disk filename (i.e. the ciphertext), it's not computable from
35 * no-key names that abbreviate the ciphertext using the strong hash to fit in
36 * NAME_MAX. It's also not computable if it's a keyed hash taken over the
37 * plaintext (but it may still be available in the on-disk directory entry);
38 * casefolded directories use this type of dirhash. At least in these cases,
39 * each no-key name must include the name's dirhash too.
40 *
Eric Biggersba47b512021-07-17 19:01:25 -050041 * To meet all these requirements, we base64url-encode the following
Daniel Rosenbergedc440e2020-01-20 14:32:01 -080042 * variable-length structure. It contains the dirhash, or 0's if the filesystem
43 * didn't provide one; up to 149 bytes of the ciphertext name; and for
44 * ciphertexts longer than 149 bytes, also the SHA-256 of the remaining bytes.
45 *
46 * This ensures that each no-key name contains everything needed to find the
47 * directory entry again, contains only legal characters, doesn't exceed
48 * NAME_MAX, is unambiguous unless there's a SHA-256 collision, and that we only
49 * take the performance hit of SHA-256 on very long filenames (which are rare).
50 */
51struct fscrypt_nokey_name {
52 u32 dirhash[2];
53 u8 bytes[149];
54 u8 sha256[SHA256_DIGEST_SIZE];
Eric Biggersba47b512021-07-17 19:01:25 -050055}; /* 189 bytes => 252 bytes base64url-encoded, which is <= NAME_MAX (255) */
Daniel Rosenbergedc440e2020-01-20 14:32:01 -080056
57/*
Eric Biggersba47b512021-07-17 19:01:25 -050058 * Decoded size of max-size no-key name, i.e. a name that was abbreviated using
Daniel Rosenbergedc440e2020-01-20 14:32:01 -080059 * the strong hash and thus includes the 'sha256' field. This isn't simply
60 * sizeof(struct fscrypt_nokey_name), as the padding at the end isn't included.
61 */
62#define FSCRYPT_NOKEY_NAME_MAX offsetofend(struct fscrypt_nokey_name, sha256)
63
Eric Biggersba47b512021-07-17 19:01:25 -050064/* Encoded size of max-size no-key name */
65#define FSCRYPT_NOKEY_NAME_MAX_ENCODED \
66 FSCRYPT_BASE64URL_CHARS(FSCRYPT_NOKEY_NAME_MAX)
67
Eric Biggersdcf0db92018-01-05 10:44:59 -080068static inline bool fscrypt_is_dot_dotdot(const struct qstr *str)
69{
70 if (str->len == 1 && str->name[0] == '.')
71 return true;
72
73 if (str->len == 2 && str->name[0] == '.' && str->name[1] == '.')
74 return true;
75
76 return false;
77}
78
Jaegeuk Kim0b81d072015-05-15 16:26:10 -070079/**
Eric Biggers1b3b8272020-01-19 23:17:36 -080080 * fscrypt_fname_encrypt() - encrypt a filename
Eric Biggersd2fe9752020-05-11 12:13:56 -070081 * @inode: inode of the parent directory (for regular filenames)
82 * or of the symlink (for symlink targets)
83 * @iname: the filename to encrypt
84 * @out: (output) the encrypted filename
85 * @olen: size of the encrypted filename. It must be at least @iname->len.
86 * Any extra space is filled with NUL padding before encryption.
Eric Biggersef1eb3a2016-09-15 17:25:55 -040087 *
88 * Return: 0 on success, -errno on failure
Jaegeuk Kim0b81d072015-05-15 16:26:10 -070089 */
Eric Biggers1b3b8272020-01-19 23:17:36 -080090int fscrypt_fname_encrypt(const struct inode *inode, const struct qstr *iname,
91 u8 *out, unsigned int olen)
Jaegeuk Kim0b81d072015-05-15 16:26:10 -070092{
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);
Eric Biggers8a4ab0b2019-12-15 13:39:47 -080095 const struct fscrypt_info *ci = inode->i_crypt_info;
Satya Tangirala5fee3602020-07-02 01:56:05 +000096 struct crypto_skcipher *tfm = ci->ci_enc_key.tfm;
Eric Biggers8094c3c2019-01-06 08:36:21 -050097 union fscrypt_iv iv;
Eric Biggers08ae8772016-11-13 20:35:52 -050098 struct scatterlist sg;
Eric Biggers8094c3c2019-01-06 08:36:21 -050099 int res;
Jaegeuk Kim0b81d072015-05-15 16:26:10 -0700100
Eric Biggers08ae8772016-11-13 20:35:52 -0500101 /*
102 * Copy the filename to the output buffer for encrypting in-place and
103 * pad it with the needed number of NUL bytes.
104 */
Eric Biggers50c961d2018-01-11 23:30:08 -0500105 if (WARN_ON(olen < iname->len))
Eric Biggers76e81d62018-01-05 10:45:01 -0800106 return -ENOBUFS;
Eric Biggers50c961d2018-01-11 23:30:08 -0500107 memcpy(out, iname->name, iname->len);
108 memset(out + iname->len, 0, olen - iname->len);
Jaegeuk Kim0b81d072015-05-15 16:26:10 -0700109
Eric Biggers08ae8772016-11-13 20:35:52 -0500110 /* Initialize the IV */
Eric Biggers8094c3c2019-01-06 08:36:21 -0500111 fscrypt_generate_iv(&iv, 0, ci);
Jaegeuk Kim0b81d072015-05-15 16:26:10 -0700112
Eric Biggers08ae8772016-11-13 20:35:52 -0500113 /* Set up the encryption request */
Linus Torvaldsd4075742016-03-21 11:03:02 -0700114 req = skcipher_request_alloc(tfm, GFP_NOFS);
Eric Biggersc90fd7752018-04-30 15:51:38 -0700115 if (!req)
Jaegeuk Kim0b81d072015-05-15 16:26:10 -0700116 return -ENOMEM;
Linus Torvaldsd4075742016-03-21 11:03:02 -0700117 skcipher_request_set_callback(req,
Jaegeuk Kim0b81d072015-05-15 16:26:10 -0700118 CRYPTO_TFM_REQ_MAY_BACKLOG | CRYPTO_TFM_REQ_MAY_SLEEP,
Gilad Ben-Yossefd0082e12017-10-18 08:00:44 +0100119 crypto_req_done, &wait);
Eric Biggers50c961d2018-01-11 23:30:08 -0500120 sg_init_one(&sg, out, olen);
Eric Biggers8094c3c2019-01-06 08:36:21 -0500121 skcipher_request_set_crypt(req, &sg, &sg, olen, &iv);
Jaegeuk Kim0b81d072015-05-15 16:26:10 -0700122
Eric Biggers08ae8772016-11-13 20:35:52 -0500123 /* Do the encryption */
Gilad Ben-Yossefd0082e12017-10-18 08:00:44 +0100124 res = crypto_wait_req(crypto_skcipher_encrypt(req), &wait);
Linus Torvaldsd4075742016-03-21 11:03:02 -0700125 skcipher_request_free(req);
Eric Biggersef1eb3a2016-09-15 17:25:55 -0400126 if (res < 0) {
Eric Biggers886da8b2019-07-24 11:07:58 -0700127 fscrypt_err(inode, "Filename encryption failed: %d", res);
Eric Biggersef1eb3a2016-09-15 17:25:55 -0400128 return res;
129 }
Jaegeuk Kim0b81d072015-05-15 16:26:10 -0700130
Eric Biggersef1eb3a2016-09-15 17:25:55 -0400131 return 0;
Jaegeuk Kim0b81d072015-05-15 16:26:10 -0700132}
133
Eric Biggersef1eb3a2016-09-15 17:25:55 -0400134/**
135 * fname_decrypt() - decrypt a filename
Eric Biggersd2fe9752020-05-11 12:13:56 -0700136 * @inode: inode of the parent directory (for regular filenames)
137 * or of the symlink (for symlink targets)
138 * @iname: the encrypted filename to decrypt
139 * @oname: (output) the decrypted filename. The caller must have allocated
140 * enough space for this, e.g. using fscrypt_fname_alloc_buffer().
Eric Biggersef1eb3a2016-09-15 17:25:55 -0400141 *
142 * Return: 0 on success, -errno on failure
Jaegeuk Kim0b81d072015-05-15 16:26:10 -0700143 */
Eric Biggers8a4ab0b2019-12-15 13:39:47 -0800144static int fname_decrypt(const struct inode *inode,
145 const struct fscrypt_str *iname,
146 struct fscrypt_str *oname)
Jaegeuk Kim0b81d072015-05-15 16:26:10 -0700147{
Linus Torvaldsd4075742016-03-21 11:03:02 -0700148 struct skcipher_request *req = NULL;
Gilad Ben-Yossefd0082e12017-10-18 08:00:44 +0100149 DECLARE_CRYPTO_WAIT(wait);
Jaegeuk Kim0b81d072015-05-15 16:26:10 -0700150 struct scatterlist src_sg, dst_sg;
Eric Biggers8a4ab0b2019-12-15 13:39:47 -0800151 const struct fscrypt_info *ci = inode->i_crypt_info;
Satya Tangirala5fee3602020-07-02 01:56:05 +0000152 struct crypto_skcipher *tfm = ci->ci_enc_key.tfm;
Eric Biggers8094c3c2019-01-06 08:36:21 -0500153 union fscrypt_iv iv;
154 int res;
Jaegeuk Kim0b81d072015-05-15 16:26:10 -0700155
Jaegeuk Kim0b81d072015-05-15 16:26:10 -0700156 /* Allocate request */
Linus Torvaldsd4075742016-03-21 11:03:02 -0700157 req = skcipher_request_alloc(tfm, GFP_NOFS);
Eric Biggersc90fd7752018-04-30 15:51:38 -0700158 if (!req)
Jaegeuk Kim0b81d072015-05-15 16:26:10 -0700159 return -ENOMEM;
Linus Torvaldsd4075742016-03-21 11:03:02 -0700160 skcipher_request_set_callback(req,
Jaegeuk Kim0b81d072015-05-15 16:26:10 -0700161 CRYPTO_TFM_REQ_MAY_BACKLOG | CRYPTO_TFM_REQ_MAY_SLEEP,
Gilad Ben-Yossefd0082e12017-10-18 08:00:44 +0100162 crypto_req_done, &wait);
Jaegeuk Kim0b81d072015-05-15 16:26:10 -0700163
164 /* Initialize IV */
Eric Biggers8094c3c2019-01-06 08:36:21 -0500165 fscrypt_generate_iv(&iv, 0, ci);
Jaegeuk Kim0b81d072015-05-15 16:26:10 -0700166
167 /* Create decryption request */
168 sg_init_one(&src_sg, iname->name, iname->len);
169 sg_init_one(&dst_sg, oname->name, oname->len);
Eric Biggers8094c3c2019-01-06 08:36:21 -0500170 skcipher_request_set_crypt(req, &src_sg, &dst_sg, iname->len, &iv);
Gilad Ben-Yossefd0082e12017-10-18 08:00:44 +0100171 res = crypto_wait_req(crypto_skcipher_decrypt(req), &wait);
Linus Torvaldsd4075742016-03-21 11:03:02 -0700172 skcipher_request_free(req);
Jaegeuk Kim0b81d072015-05-15 16:26:10 -0700173 if (res < 0) {
Eric Biggers886da8b2019-07-24 11:07:58 -0700174 fscrypt_err(inode, "Filename decryption failed: %d", res);
Jaegeuk Kim0b81d072015-05-15 16:26:10 -0700175 return res;
176 }
177
178 oname->len = strnlen(oname->name, iname->len);
Eric Biggersef1eb3a2016-09-15 17:25:55 -0400179 return 0;
Jaegeuk Kim0b81d072015-05-15 16:26:10 -0700180}
181
Eric Biggersba47b512021-07-17 19:01:25 -0500182static const char base64url_table[65] =
183 "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789-_";
Jaegeuk Kim0b81d072015-05-15 16:26:10 -0700184
Eric Biggersba47b512021-07-17 19:01:25 -0500185#define FSCRYPT_BASE64URL_CHARS(nbytes) DIV_ROUND_UP((nbytes) * 4, 3)
Eric Biggers17159422017-04-24 10:00:10 -0700186
Jaegeuk Kim0b81d072015-05-15 16:26:10 -0700187/**
Eric Biggersba47b512021-07-17 19:01:25 -0500188 * fscrypt_base64url_encode() - base64url-encode some binary data
189 * @src: the binary data to encode
190 * @srclen: the length of @src in bytes
191 * @dst: (output) the base64url-encoded string. Not NUL-terminated.
Jaegeuk Kim0b81d072015-05-15 16:26:10 -0700192 *
Eric Biggersba47b512021-07-17 19:01:25 -0500193 * Encodes data using base64url encoding, i.e. the "Base 64 Encoding with URL
194 * and Filename Safe Alphabet" specified by RFC 4648. '='-padding isn't used,
195 * as it's unneeded and not required by the RFC. base64url is used instead of
196 * base64 to avoid the '/' character, which isn't allowed in filenames.
Eric Biggers1c5100a2019-07-24 11:07:58 -0700197 *
Eric Biggersba47b512021-07-17 19:01:25 -0500198 * Return: the length of the resulting base64url-encoded string in bytes.
199 * This will be equal to FSCRYPT_BASE64URL_CHARS(srclen).
Jaegeuk Kim0b81d072015-05-15 16:26:10 -0700200 */
Eric Biggersba47b512021-07-17 19:01:25 -0500201static int fscrypt_base64url_encode(const u8 *src, int srclen, char *dst)
Jaegeuk Kim0b81d072015-05-15 16:26:10 -0700202{
Eric Biggersba47b512021-07-17 19:01:25 -0500203 u32 ac = 0;
204 int bits = 0;
205 int i;
Jaegeuk Kim0b81d072015-05-15 16:26:10 -0700206 char *cp = dst;
207
Eric Biggersba47b512021-07-17 19:01:25 -0500208 for (i = 0; i < srclen; i++) {
209 ac = (ac << 8) | src[i];
Jaegeuk Kim0b81d072015-05-15 16:26:10 -0700210 bits += 8;
211 do {
Jaegeuk Kim0b81d072015-05-15 16:26:10 -0700212 bits -= 6;
Eric Biggersba47b512021-07-17 19:01:25 -0500213 *cp++ = base64url_table[(ac >> bits) & 0x3f];
Jaegeuk Kim0b81d072015-05-15 16:26:10 -0700214 } while (bits >= 6);
Jaegeuk Kim0b81d072015-05-15 16:26:10 -0700215 }
216 if (bits)
Eric Biggersba47b512021-07-17 19:01:25 -0500217 *cp++ = base64url_table[(ac << (6 - bits)) & 0x3f];
Jaegeuk Kim0b81d072015-05-15 16:26:10 -0700218 return cp - dst;
219}
220
Eric Biggersba47b512021-07-17 19:01:25 -0500221/**
222 * fscrypt_base64url_decode() - base64url-decode a string
223 * @src: the string to decode. Doesn't need to be NUL-terminated.
224 * @srclen: the length of @src in bytes
225 * @dst: (output) the decoded binary data
226 *
227 * Decodes a string using base64url encoding, i.e. the "Base 64 Encoding with
228 * URL and Filename Safe Alphabet" specified by RFC 4648. '='-padding isn't
229 * accepted, nor are non-encoding characters such as whitespace.
230 *
231 * This implementation hasn't been optimized for performance.
232 *
233 * Return: the length of the resulting decoded binary data in bytes,
234 * or -1 if the string isn't a valid base64url string.
235 */
236static int fscrypt_base64url_decode(const char *src, int srclen, u8 *dst)
Jaegeuk Kim0b81d072015-05-15 16:26:10 -0700237{
Eric Biggersba47b512021-07-17 19:01:25 -0500238 u32 ac = 0;
239 int bits = 0;
240 int i;
241 u8 *bp = dst;
Jaegeuk Kim0b81d072015-05-15 16:26:10 -0700242
Eric Biggersba47b512021-07-17 19:01:25 -0500243 for (i = 0; i < srclen; i++) {
244 const char *p = strchr(base64url_table, src[i]);
245
Jaegeuk Kim0b81d072015-05-15 16:26:10 -0700246 if (p == NULL || src[i] == 0)
Eric Biggersba47b512021-07-17 19:01:25 -0500247 return -1;
248 ac = (ac << 6) | (p - base64url_table);
Jaegeuk Kim0b81d072015-05-15 16:26:10 -0700249 bits += 6;
250 if (bits >= 8) {
Jaegeuk Kim0b81d072015-05-15 16:26:10 -0700251 bits -= 8;
Eric Biggersba47b512021-07-17 19:01:25 -0500252 *bp++ = (u8)(ac >> bits);
Jaegeuk Kim0b81d072015-05-15 16:26:10 -0700253 }
Jaegeuk Kim0b81d072015-05-15 16:26:10 -0700254 }
Eric Biggersba47b512021-07-17 19:01:25 -0500255 if (ac & ((1 << bits) - 1))
Jaegeuk Kim0b81d072015-05-15 16:26:10 -0700256 return -1;
Eric Biggersba47b512021-07-17 19:01:25 -0500257 return bp - dst;
Jaegeuk Kim0b81d072015-05-15 16:26:10 -0700258}
259
Eric Biggersac4acb12020-09-16 21:11:35 -0700260bool fscrypt_fname_encrypted_size(const union fscrypt_policy *policy,
261 u32 orig_len, u32 max_len,
262 u32 *encrypted_len_ret)
Jaegeuk Kim0b81d072015-05-15 16:26:10 -0700263{
Eric Biggersac4acb12020-09-16 21:11:35 -0700264 int padding = 4 << (fscrypt_policy_flags(policy) &
Eric Biggers3b6df592019-08-04 19:35:44 -0700265 FSCRYPT_POLICY_FLAGS_PAD_MASK);
Eric Biggersb9db0b42018-01-11 23:30:08 -0500266 u32 encrypted_len;
Jaegeuk Kim0b81d072015-05-15 16:26:10 -0700267
Eric Biggersb9db0b42018-01-11 23:30:08 -0500268 if (orig_len > max_len)
269 return false;
270 encrypted_len = max(orig_len, (u32)FS_CRYPTO_BLOCK_SIZE);
271 encrypted_len = round_up(encrypted_len, padding);
272 *encrypted_len_ret = min(encrypted_len, max_len);
273 return true;
Jaegeuk Kim0b81d072015-05-15 16:26:10 -0700274}
Jaegeuk Kim0b81d072015-05-15 16:26:10 -0700275
276/**
Eric Biggersd2fe9752020-05-11 12:13:56 -0700277 * fscrypt_fname_alloc_buffer() - allocate a buffer for presented filenames
Eric Biggersd2fe9752020-05-11 12:13:56 -0700278 * @max_encrypted_len: maximum length of encrypted filenames the buffer will be
279 * used to present
280 * @crypto_str: (output) buffer to allocate
Jaegeuk Kim0b81d072015-05-15 16:26:10 -0700281 *
Eric Biggers2cbadad2018-01-11 23:30:08 -0500282 * Allocate a buffer that is large enough to hold any decrypted or encoded
283 * filename (null-terminated), for the given maximum encrypted filename length.
284 *
285 * Return: 0 on success, -errno on failure
Jaegeuk Kim0b81d072015-05-15 16:26:10 -0700286 */
Jeff Layton8b10fe62020-08-10 10:21:39 -0400287int fscrypt_fname_alloc_buffer(u32 max_encrypted_len,
Eric Biggers2cbadad2018-01-11 23:30:08 -0500288 struct fscrypt_str *crypto_str)
Jaegeuk Kim0b81d072015-05-15 16:26:10 -0700289{
Eric Biggersba47b512021-07-17 19:01:25 -0500290 u32 max_presented_len = max_t(u32, FSCRYPT_NOKEY_NAME_MAX_ENCODED,
291 max_encrypted_len);
Eric Biggers17159422017-04-24 10:00:10 -0700292
Eric Biggers2cbadad2018-01-11 23:30:08 -0500293 crypto_str->name = kmalloc(max_presented_len + 1, GFP_NOFS);
294 if (!crypto_str->name)
Jaegeuk Kim0b81d072015-05-15 16:26:10 -0700295 return -ENOMEM;
Eric Biggers2cbadad2018-01-11 23:30:08 -0500296 crypto_str->len = max_presented_len;
Jaegeuk Kim0b81d072015-05-15 16:26:10 -0700297 return 0;
298}
299EXPORT_SYMBOL(fscrypt_fname_alloc_buffer);
300
301/**
Eric Biggersd2fe9752020-05-11 12:13:56 -0700302 * fscrypt_fname_free_buffer() - free a buffer for presented filenames
303 * @crypto_str: the buffer to free
Jaegeuk Kim0b81d072015-05-15 16:26:10 -0700304 *
Eric Biggersd2fe9752020-05-11 12:13:56 -0700305 * Free a buffer that was allocated by fscrypt_fname_alloc_buffer().
Jaegeuk Kim0b81d072015-05-15 16:26:10 -0700306 */
307void fscrypt_fname_free_buffer(struct fscrypt_str *crypto_str)
308{
309 if (!crypto_str)
310 return;
311 kfree(crypto_str->name);
312 crypto_str->name = NULL;
313}
314EXPORT_SYMBOL(fscrypt_fname_free_buffer);
315
316/**
Eric Biggersd2fe9752020-05-11 12:13:56 -0700317 * fscrypt_fname_disk_to_usr() - convert an encrypted filename to
318 * user-presentable form
319 * @inode: inode of the parent directory (for regular filenames)
320 * or of the symlink (for symlink targets)
321 * @hash: first part of the name's dirhash, if applicable. This only needs to
322 * be provided if the filename is located in an indexed directory whose
323 * encryption key may be unavailable. Not needed for symlink targets.
324 * @minor_hash: second part of the name's dirhash, if applicable
325 * @iname: encrypted filename to convert. May also be "." or "..", which
326 * aren't actually encrypted.
327 * @oname: output buffer for the user-presentable filename. The caller must
328 * have allocated enough space for this, e.g. using
329 * fscrypt_fname_alloc_buffer().
Eric Biggersef1eb3a2016-09-15 17:25:55 -0400330 *
Daniel Rosenbergedc440e2020-01-20 14:32:01 -0800331 * If the key is available, we'll decrypt the disk name. Otherwise, we'll
332 * encode it for presentation in fscrypt_nokey_name format.
333 * See struct fscrypt_nokey_name for details.
Eric Biggers17159422017-04-24 10:00:10 -0700334 *
Eric Biggersef1eb3a2016-09-15 17:25:55 -0400335 * Return: 0 on success, -errno on failure
Jaegeuk Kim0b81d072015-05-15 16:26:10 -0700336 */
Eric Biggers8a4ab0b2019-12-15 13:39:47 -0800337int fscrypt_fname_disk_to_usr(const struct inode *inode,
338 u32 hash, u32 minor_hash,
339 const struct fscrypt_str *iname,
340 struct fscrypt_str *oname)
Jaegeuk Kim0b81d072015-05-15 16:26:10 -0700341{
342 const struct qstr qname = FSTR_TO_QSTR(iname);
Daniel Rosenbergedc440e2020-01-20 14:32:01 -0800343 struct fscrypt_nokey_name nokey_name;
344 u32 size; /* size of the unencoded no-key name */
Jaegeuk Kim0b81d072015-05-15 16:26:10 -0700345
346 if (fscrypt_is_dot_dotdot(&qname)) {
347 oname->name[0] = '.';
348 oname->name[iname->len - 1] = '.';
349 oname->len = iname->len;
Eric Biggersef1eb3a2016-09-15 17:25:55 -0400350 return 0;
Jaegeuk Kim0b81d072015-05-15 16:26:10 -0700351 }
352
353 if (iname->len < FS_CRYPTO_BLOCK_SIZE)
354 return -EUCLEAN;
355
Eric Biggerse37a7842019-04-11 14:32:15 -0700356 if (fscrypt_has_encryption_key(inode))
Jaegeuk Kim0b81d072015-05-15 16:26:10 -0700357 return fname_decrypt(inode, iname, oname);
358
Daniel Rosenbergedc440e2020-01-20 14:32:01 -0800359 /*
360 * Sanity check that struct fscrypt_nokey_name doesn't have padding
361 * between fields and that its encoded size never exceeds NAME_MAX.
362 */
363 BUILD_BUG_ON(offsetofend(struct fscrypt_nokey_name, dirhash) !=
364 offsetof(struct fscrypt_nokey_name, bytes));
365 BUILD_BUG_ON(offsetofend(struct fscrypt_nokey_name, bytes) !=
366 offsetof(struct fscrypt_nokey_name, sha256));
Eric Biggersba47b512021-07-17 19:01:25 -0500367 BUILD_BUG_ON(FSCRYPT_NOKEY_NAME_MAX_ENCODED > NAME_MAX);
Daniel Rosenbergedc440e2020-01-20 14:32:01 -0800368
Eric Biggers77f30bf2021-05-27 16:52:36 -0700369 nokey_name.dirhash[0] = hash;
370 nokey_name.dirhash[1] = minor_hash;
371
Daniel Rosenbergedc440e2020-01-20 14:32:01 -0800372 if (iname->len <= sizeof(nokey_name.bytes)) {
373 memcpy(nokey_name.bytes, iname->name, iname->len);
374 size = offsetof(struct fscrypt_nokey_name, bytes[iname->len]);
375 } else {
376 memcpy(nokey_name.bytes, iname->name, sizeof(nokey_name.bytes));
377 /* Compute strong hash of remaining part of name. */
Eric Biggers0c6a1132020-09-16 21:53:41 -0700378 sha256(&iname->name[sizeof(nokey_name.bytes)],
379 iname->len - sizeof(nokey_name.bytes),
380 nokey_name.sha256);
Daniel Rosenbergedc440e2020-01-20 14:32:01 -0800381 size = FSCRYPT_NOKEY_NAME_MAX;
382 }
Eric Biggersba47b512021-07-17 19:01:25 -0500383 oname->len = fscrypt_base64url_encode((const u8 *)&nokey_name, size,
384 oname->name);
Eric Biggersef1eb3a2016-09-15 17:25:55 -0400385 return 0;
Jaegeuk Kim0b81d072015-05-15 16:26:10 -0700386}
387EXPORT_SYMBOL(fscrypt_fname_disk_to_usr);
388
389/**
Eric Biggers17159422017-04-24 10:00:10 -0700390 * fscrypt_setup_filename() - prepare to search a possibly encrypted directory
391 * @dir: the directory that will be searched
392 * @iname: the user-provided filename being searched for
393 * @lookup: 1 if we're allowed to proceed without the key because it's
394 * ->lookup() or we're finding the dir_entry for deletion; 0 if we cannot
395 * proceed without the key because we're going to create the dir_entry.
396 * @fname: the filename information to be filled in
397 *
398 * Given a user-provided filename @iname, this function sets @fname->disk_name
399 * to the name that would be stored in the on-disk directory entry, if possible.
400 * If the directory is unencrypted this is simply @iname. Else, if we have the
401 * directory's encryption key, then @iname is the plaintext, so we encrypt it to
402 * get the disk_name.
403 *
Eric Biggers70fb2612020-09-23 21:26:23 -0700404 * Else, for keyless @lookup operations, @iname should be a no-key name, so we
405 * decode it to get the struct fscrypt_nokey_name. Non-@lookup operations will
406 * be impossible in this case, so we fail them with ENOKEY.
Eric Biggers17159422017-04-24 10:00:10 -0700407 *
408 * If successful, fscrypt_free_filename() must be called later to clean up.
409 *
410 * Return: 0 on success, -errno on failure
411 */
Jaegeuk Kim0b81d072015-05-15 16:26:10 -0700412int fscrypt_setup_filename(struct inode *dir, const struct qstr *iname,
413 int lookup, struct fscrypt_name *fname)
414{
Daniel Rosenbergedc440e2020-01-20 14:32:01 -0800415 struct fscrypt_nokey_name *nokey_name;
Eric Biggers17159422017-04-24 10:00:10 -0700416 int ret;
Jaegeuk Kim0b81d072015-05-15 16:26:10 -0700417
418 memset(fname, 0, sizeof(struct fscrypt_name));
419 fname->usr_fname = iname;
420
Eric Biggerse0428a22017-10-09 12:15:36 -0700421 if (!IS_ENCRYPTED(dir) || fscrypt_is_dot_dotdot(iname)) {
Jaegeuk Kim0b81d072015-05-15 16:26:10 -0700422 fname->disk_name.name = (unsigned char *)iname->name;
423 fname->disk_name.len = iname->len;
424 return 0;
425 }
Eric Biggersa14d0b62020-12-02 18:20:41 -0800426 ret = fscrypt_get_encryption_info(dir, lookup);
Eric Biggers17bfde62018-04-30 15:51:41 -0700427 if (ret)
Jaegeuk Kim0b81d072015-05-15 16:26:10 -0700428 return ret;
429
Eric Biggerse37a7842019-04-11 14:32:15 -0700430 if (fscrypt_has_encryption_key(dir)) {
Eric Biggersac4acb12020-09-16 21:11:35 -0700431 if (!fscrypt_fname_encrypted_size(&dir->i_crypt_info->ci_policy,
Eric Biggers4373b3d2021-09-09 11:45:13 -0700432 iname->len, NAME_MAX,
Eric Biggersb9db0b42018-01-11 23:30:08 -0500433 &fname->crypto_buf.len))
Eric Biggers50c961d2018-01-11 23:30:08 -0500434 return -ENAMETOOLONG;
Eric Biggers50c961d2018-01-11 23:30:08 -0500435 fname->crypto_buf.name = kmalloc(fname->crypto_buf.len,
436 GFP_NOFS);
437 if (!fname->crypto_buf.name)
438 return -ENOMEM;
439
Eric Biggers1b3b8272020-01-19 23:17:36 -0800440 ret = fscrypt_fname_encrypt(dir, iname, fname->crypto_buf.name,
441 fname->crypto_buf.len);
Eric Biggersef1eb3a2016-09-15 17:25:55 -0400442 if (ret)
Jaegeuk Kim0b81d072015-05-15 16:26:10 -0700443 goto errout;
444 fname->disk_name.name = fname->crypto_buf.name;
445 fname->disk_name.len = fname->crypto_buf.len;
446 return 0;
447 }
448 if (!lookup)
Eric Biggers54475f52016-12-05 11:12:44 -0800449 return -ENOKEY;
Eric Biggers70fb2612020-09-23 21:26:23 -0700450 fname->is_nokey_name = true;
Jaegeuk Kim0b81d072015-05-15 16:26:10 -0700451
452 /*
453 * We don't have the key and we are doing a lookup; decode the
454 * user-supplied name
455 */
Jaegeuk Kim0b81d072015-05-15 16:26:10 -0700456
Eric Biggersba47b512021-07-17 19:01:25 -0500457 if (iname->len > FSCRYPT_NOKEY_NAME_MAX_ENCODED)
Daniel Rosenbergedc440e2020-01-20 14:32:01 -0800458 return -ENOENT;
459
460 fname->crypto_buf.name = kmalloc(FSCRYPT_NOKEY_NAME_MAX, GFP_KERNEL);
Jaegeuk Kim0b81d072015-05-15 16:26:10 -0700461 if (fname->crypto_buf.name == NULL)
462 return -ENOMEM;
463
Eric Biggersba47b512021-07-17 19:01:25 -0500464 ret = fscrypt_base64url_decode(iname->name, iname->len,
465 fname->crypto_buf.name);
Daniel Rosenbergedc440e2020-01-20 14:32:01 -0800466 if (ret < (int)offsetof(struct fscrypt_nokey_name, bytes[1]) ||
467 (ret > offsetof(struct fscrypt_nokey_name, sha256) &&
468 ret != FSCRYPT_NOKEY_NAME_MAX)) {
Jaegeuk Kim0b81d072015-05-15 16:26:10 -0700469 ret = -ENOENT;
470 goto errout;
471 }
472 fname->crypto_buf.len = ret;
Daniel Rosenbergedc440e2020-01-20 14:32:01 -0800473
474 nokey_name = (void *)fname->crypto_buf.name;
475 fname->hash = nokey_name->dirhash[0];
476 fname->minor_hash = nokey_name->dirhash[1];
477 if (ret != FSCRYPT_NOKEY_NAME_MAX) {
478 /* The full ciphertext filename is available. */
479 fname->disk_name.name = nokey_name->bytes;
480 fname->disk_name.len =
481 ret - offsetof(struct fscrypt_nokey_name, bytes);
Jaegeuk Kim0b81d072015-05-15 16:26:10 -0700482 }
483 return 0;
484
485errout:
Eric Biggers50c961d2018-01-11 23:30:08 -0500486 kfree(fname->crypto_buf.name);
Jaegeuk Kim0b81d072015-05-15 16:26:10 -0700487 return ret;
488}
489EXPORT_SYMBOL(fscrypt_setup_filename);
Eric Biggers2ebdef62019-12-09 12:43:59 -0800490
Daniel Rosenbergaa408f82020-01-20 14:31:57 -0800491/**
Daniel Rosenbergedc440e2020-01-20 14:32:01 -0800492 * fscrypt_match_name() - test whether the given name matches a directory entry
493 * @fname: the name being searched for
494 * @de_name: the name from the directory entry
495 * @de_name_len: the length of @de_name in bytes
496 *
497 * Normally @fname->disk_name will be set, and in that case we simply compare
498 * that to the name stored in the directory entry. The only exception is that
499 * if we don't have the key for an encrypted directory and the name we're
500 * looking for is very long, then we won't have the full disk_name and instead
501 * we'll need to match against a fscrypt_nokey_name that includes a strong hash.
502 *
503 * Return: %true if the name matches, otherwise %false.
504 */
505bool fscrypt_match_name(const struct fscrypt_name *fname,
506 const u8 *de_name, u32 de_name_len)
507{
508 const struct fscrypt_nokey_name *nokey_name =
509 (const void *)fname->crypto_buf.name;
Eric Biggers0c6a1132020-09-16 21:53:41 -0700510 u8 digest[SHA256_DIGEST_SIZE];
Daniel Rosenbergedc440e2020-01-20 14:32:01 -0800511
512 if (likely(fname->disk_name.name)) {
513 if (de_name_len != fname->disk_name.len)
514 return false;
515 return !memcmp(de_name, fname->disk_name.name, de_name_len);
516 }
517 if (de_name_len <= sizeof(nokey_name->bytes))
518 return false;
519 if (memcmp(de_name, nokey_name->bytes, sizeof(nokey_name->bytes)))
520 return false;
Eric Biggers0c6a1132020-09-16 21:53:41 -0700521 sha256(&de_name[sizeof(nokey_name->bytes)],
522 de_name_len - sizeof(nokey_name->bytes), digest);
523 return !memcmp(digest, nokey_name->sha256, sizeof(digest));
Daniel Rosenbergedc440e2020-01-20 14:32:01 -0800524}
525EXPORT_SYMBOL_GPL(fscrypt_match_name);
526
527/**
Daniel Rosenbergaa408f82020-01-20 14:31:57 -0800528 * fscrypt_fname_siphash() - calculate the SipHash of a filename
529 * @dir: the parent directory
530 * @name: the filename to calculate the SipHash of
531 *
532 * Given a plaintext filename @name and a directory @dir which uses SipHash as
533 * its dirhash method and has had its fscrypt key set up, this function
534 * calculates the SipHash of that name using the directory's secret dirhash key.
535 *
536 * Return: the SipHash of @name using the hash key of @dir
537 */
538u64 fscrypt_fname_siphash(const struct inode *dir, const struct qstr *name)
539{
540 const struct fscrypt_info *ci = dir->i_crypt_info;
541
542 WARN_ON(!ci->ci_dirhash_key_initialized);
543
544 return siphash(name->name, name->len, &ci->ci_dirhash_key);
545}
546EXPORT_SYMBOL_GPL(fscrypt_fname_siphash);
547
Eric Biggers2ebdef62019-12-09 12:43:59 -0800548/*
549 * Validate dentries in encrypted directories to make sure we aren't potentially
550 * caching stale dentries after a key has been added.
551 */
Eric Biggers5b2a8282020-09-23 22:47:21 -0700552int fscrypt_d_revalidate(struct dentry *dentry, unsigned int flags)
Eric Biggers2ebdef62019-12-09 12:43:59 -0800553{
554 struct dentry *dir;
555 int err;
556 int valid;
557
558 /*
559 * Plaintext names are always valid, since fscrypt doesn't support
Eric Biggers70fb2612020-09-23 21:26:23 -0700560 * reverting to no-key names without evicting the directory's inode
Eric Biggers2ebdef62019-12-09 12:43:59 -0800561 * -- which implies eviction of the dentries in the directory.
562 */
Eric Biggers501e43f2020-09-23 21:26:24 -0700563 if (!(dentry->d_flags & DCACHE_NOKEY_NAME))
Eric Biggers2ebdef62019-12-09 12:43:59 -0800564 return 1;
565
566 /*
Eric Biggers70fb2612020-09-23 21:26:23 -0700567 * No-key name; valid if the directory's key is still unavailable.
Eric Biggers2ebdef62019-12-09 12:43:59 -0800568 *
Eric Biggers70fb2612020-09-23 21:26:23 -0700569 * Although fscrypt forbids rename() on no-key names, we still must use
570 * dget_parent() here rather than use ->d_parent directly. That's
Eric Biggers2ebdef62019-12-09 12:43:59 -0800571 * because a corrupted fs image may contain directory hard links, which
572 * the VFS handles by moving the directory's dentry tree in the dcache
573 * each time ->lookup() finds the directory and it already has a dentry
574 * elsewhere. Thus ->d_parent can be changing, and we must safely grab
575 * a reference to some ->d_parent to prevent it from being freed.
576 */
577
578 if (flags & LOOKUP_RCU)
579 return -ECHILD;
580
581 dir = dget_parent(dentry);
Eric Biggersa14d0b62020-12-02 18:20:41 -0800582 /*
583 * Pass allow_unsupported=true, so that files with an unsupported
584 * encryption policy can be deleted.
585 */
586 err = fscrypt_get_encryption_info(d_inode(dir), true);
Eric Biggers2ebdef62019-12-09 12:43:59 -0800587 valid = !fscrypt_has_encryption_key(d_inode(dir));
588 dput(dir);
589
590 if (err < 0)
591 return err;
592
593 return valid;
594}
Eric Biggers5b2a8282020-09-23 22:47:21 -0700595EXPORT_SYMBOL_GPL(fscrypt_d_revalidate);