Greg Kroah-Hartman | b244131 | 2017-11-01 15:07:57 +0100 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0 |
Jaegeuk Kim | 0b81d07 | 2015-05-15 16:26:10 -0700 | [diff] [blame] | 2 | /* |
| 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 Biggers | 2ebdef6 | 2019-12-09 12:43:59 -0800 | [diff] [blame] | 14 | #include <linux/namei.h> |
Jaegeuk Kim | 0b81d07 | 2015-05-15 16:26:10 -0700 | [diff] [blame] | 15 | #include <linux/scatterlist.h> |
Daniel Rosenberg | edc440e | 2020-01-20 14:32:01 -0800 | [diff] [blame] | 16 | #include <crypto/hash.h> |
Eric Biggers | a24d22b | 2020-11-12 21:20:21 -0800 | [diff] [blame] | 17 | #include <crypto/sha2.h> |
Eric Biggers | a575784 | 2018-01-05 10:45:00 -0800 | [diff] [blame] | 18 | #include <crypto/skcipher.h> |
Theodore Ts'o | 3325bea | 2016-11-26 20:32:46 -0500 | [diff] [blame] | 19 | #include "fscrypt_private.h" |
Jaegeuk Kim | 0b81d07 | 2015-05-15 16:26:10 -0700 | [diff] [blame] | 20 | |
Eric Biggers | d2fe975 | 2020-05-11 12:13:56 -0700 | [diff] [blame] | 21 | /* |
Daniel Rosenberg | edc440e | 2020-01-20 14:32:01 -0800 | [diff] [blame] | 22 | * 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 Biggers | ba47b51 | 2021-07-17 19:01:25 -0500 | [diff] [blame] | 29 | * way. We use base64url. But that can cause names to exceed NAME_MAX (255 |
Daniel Rosenberg | edc440e | 2020-01-20 14:32:01 -0800 | [diff] [blame] | 30 | * 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 Biggers | ba47b51 | 2021-07-17 19:01:25 -0500 | [diff] [blame] | 41 | * To meet all these requirements, we base64url-encode the following |
Daniel Rosenberg | edc440e | 2020-01-20 14:32:01 -0800 | [diff] [blame] | 42 | * 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 | */ |
| 51 | struct fscrypt_nokey_name { |
| 52 | u32 dirhash[2]; |
| 53 | u8 bytes[149]; |
| 54 | u8 sha256[SHA256_DIGEST_SIZE]; |
Eric Biggers | ba47b51 | 2021-07-17 19:01:25 -0500 | [diff] [blame] | 55 | }; /* 189 bytes => 252 bytes base64url-encoded, which is <= NAME_MAX (255) */ |
Daniel Rosenberg | edc440e | 2020-01-20 14:32:01 -0800 | [diff] [blame] | 56 | |
| 57 | /* |
Eric Biggers | ba47b51 | 2021-07-17 19:01:25 -0500 | [diff] [blame] | 58 | * Decoded size of max-size no-key name, i.e. a name that was abbreviated using |
Daniel Rosenberg | edc440e | 2020-01-20 14:32:01 -0800 | [diff] [blame] | 59 | * 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 Biggers | ba47b51 | 2021-07-17 19:01:25 -0500 | [diff] [blame] | 64 | /* 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 Biggers | dcf0db9 | 2018-01-05 10:44:59 -0800 | [diff] [blame] | 68 | static 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 Kim | 0b81d07 | 2015-05-15 16:26:10 -0700 | [diff] [blame] | 79 | /** |
Eric Biggers | 1b3b827 | 2020-01-19 23:17:36 -0800 | [diff] [blame] | 80 | * fscrypt_fname_encrypt() - encrypt a filename |
Eric Biggers | d2fe975 | 2020-05-11 12:13:56 -0700 | [diff] [blame] | 81 | * @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 Biggers | ef1eb3a | 2016-09-15 17:25:55 -0400 | [diff] [blame] | 87 | * |
| 88 | * Return: 0 on success, -errno on failure |
Jaegeuk Kim | 0b81d07 | 2015-05-15 16:26:10 -0700 | [diff] [blame] | 89 | */ |
Eric Biggers | 1b3b827 | 2020-01-19 23:17:36 -0800 | [diff] [blame] | 90 | int fscrypt_fname_encrypt(const struct inode *inode, const struct qstr *iname, |
| 91 | u8 *out, unsigned int olen) |
Jaegeuk Kim | 0b81d07 | 2015-05-15 16:26:10 -0700 | [diff] [blame] | 92 | { |
Linus Torvalds | d407574 | 2016-03-21 11:03:02 -0700 | [diff] [blame] | 93 | struct skcipher_request *req = NULL; |
Gilad Ben-Yossef | d0082e1 | 2017-10-18 08:00:44 +0100 | [diff] [blame] | 94 | DECLARE_CRYPTO_WAIT(wait); |
Eric Biggers | 8a4ab0b | 2019-12-15 13:39:47 -0800 | [diff] [blame] | 95 | const struct fscrypt_info *ci = inode->i_crypt_info; |
Satya Tangirala | 5fee360 | 2020-07-02 01:56:05 +0000 | [diff] [blame] | 96 | struct crypto_skcipher *tfm = ci->ci_enc_key.tfm; |
Eric Biggers | 8094c3c | 2019-01-06 08:36:21 -0500 | [diff] [blame] | 97 | union fscrypt_iv iv; |
Eric Biggers | 08ae877 | 2016-11-13 20:35:52 -0500 | [diff] [blame] | 98 | struct scatterlist sg; |
Eric Biggers | 8094c3c | 2019-01-06 08:36:21 -0500 | [diff] [blame] | 99 | int res; |
Jaegeuk Kim | 0b81d07 | 2015-05-15 16:26:10 -0700 | [diff] [blame] | 100 | |
Eric Biggers | 08ae877 | 2016-11-13 20:35:52 -0500 | [diff] [blame] | 101 | /* |
| 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 Biggers | 50c961d | 2018-01-11 23:30:08 -0500 | [diff] [blame] | 105 | if (WARN_ON(olen < iname->len)) |
Eric Biggers | 76e81d6 | 2018-01-05 10:45:01 -0800 | [diff] [blame] | 106 | return -ENOBUFS; |
Eric Biggers | 50c961d | 2018-01-11 23:30:08 -0500 | [diff] [blame] | 107 | memcpy(out, iname->name, iname->len); |
| 108 | memset(out + iname->len, 0, olen - iname->len); |
Jaegeuk Kim | 0b81d07 | 2015-05-15 16:26:10 -0700 | [diff] [blame] | 109 | |
Eric Biggers | 08ae877 | 2016-11-13 20:35:52 -0500 | [diff] [blame] | 110 | /* Initialize the IV */ |
Eric Biggers | 8094c3c | 2019-01-06 08:36:21 -0500 | [diff] [blame] | 111 | fscrypt_generate_iv(&iv, 0, ci); |
Jaegeuk Kim | 0b81d07 | 2015-05-15 16:26:10 -0700 | [diff] [blame] | 112 | |
Eric Biggers | 08ae877 | 2016-11-13 20:35:52 -0500 | [diff] [blame] | 113 | /* Set up the encryption request */ |
Linus Torvalds | d407574 | 2016-03-21 11:03:02 -0700 | [diff] [blame] | 114 | req = skcipher_request_alloc(tfm, GFP_NOFS); |
Eric Biggers | c90fd775 | 2018-04-30 15:51:38 -0700 | [diff] [blame] | 115 | if (!req) |
Jaegeuk Kim | 0b81d07 | 2015-05-15 16:26:10 -0700 | [diff] [blame] | 116 | return -ENOMEM; |
Linus Torvalds | d407574 | 2016-03-21 11:03:02 -0700 | [diff] [blame] | 117 | skcipher_request_set_callback(req, |
Jaegeuk Kim | 0b81d07 | 2015-05-15 16:26:10 -0700 | [diff] [blame] | 118 | CRYPTO_TFM_REQ_MAY_BACKLOG | CRYPTO_TFM_REQ_MAY_SLEEP, |
Gilad Ben-Yossef | d0082e1 | 2017-10-18 08:00:44 +0100 | [diff] [blame] | 119 | crypto_req_done, &wait); |
Eric Biggers | 50c961d | 2018-01-11 23:30:08 -0500 | [diff] [blame] | 120 | sg_init_one(&sg, out, olen); |
Eric Biggers | 8094c3c | 2019-01-06 08:36:21 -0500 | [diff] [blame] | 121 | skcipher_request_set_crypt(req, &sg, &sg, olen, &iv); |
Jaegeuk Kim | 0b81d07 | 2015-05-15 16:26:10 -0700 | [diff] [blame] | 122 | |
Eric Biggers | 08ae877 | 2016-11-13 20:35:52 -0500 | [diff] [blame] | 123 | /* Do the encryption */ |
Gilad Ben-Yossef | d0082e1 | 2017-10-18 08:00:44 +0100 | [diff] [blame] | 124 | res = crypto_wait_req(crypto_skcipher_encrypt(req), &wait); |
Linus Torvalds | d407574 | 2016-03-21 11:03:02 -0700 | [diff] [blame] | 125 | skcipher_request_free(req); |
Eric Biggers | ef1eb3a | 2016-09-15 17:25:55 -0400 | [diff] [blame] | 126 | if (res < 0) { |
Eric Biggers | 886da8b | 2019-07-24 11:07:58 -0700 | [diff] [blame] | 127 | fscrypt_err(inode, "Filename encryption failed: %d", res); |
Eric Biggers | ef1eb3a | 2016-09-15 17:25:55 -0400 | [diff] [blame] | 128 | return res; |
| 129 | } |
Jaegeuk Kim | 0b81d07 | 2015-05-15 16:26:10 -0700 | [diff] [blame] | 130 | |
Eric Biggers | ef1eb3a | 2016-09-15 17:25:55 -0400 | [diff] [blame] | 131 | return 0; |
Jaegeuk Kim | 0b81d07 | 2015-05-15 16:26:10 -0700 | [diff] [blame] | 132 | } |
| 133 | |
Eric Biggers | ef1eb3a | 2016-09-15 17:25:55 -0400 | [diff] [blame] | 134 | /** |
| 135 | * fname_decrypt() - decrypt a filename |
Eric Biggers | d2fe975 | 2020-05-11 12:13:56 -0700 | [diff] [blame] | 136 | * @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 Biggers | ef1eb3a | 2016-09-15 17:25:55 -0400 | [diff] [blame] | 141 | * |
| 142 | * Return: 0 on success, -errno on failure |
Jaegeuk Kim | 0b81d07 | 2015-05-15 16:26:10 -0700 | [diff] [blame] | 143 | */ |
Eric Biggers | 8a4ab0b | 2019-12-15 13:39:47 -0800 | [diff] [blame] | 144 | static int fname_decrypt(const struct inode *inode, |
| 145 | const struct fscrypt_str *iname, |
| 146 | struct fscrypt_str *oname) |
Jaegeuk Kim | 0b81d07 | 2015-05-15 16:26:10 -0700 | [diff] [blame] | 147 | { |
Linus Torvalds | d407574 | 2016-03-21 11:03:02 -0700 | [diff] [blame] | 148 | struct skcipher_request *req = NULL; |
Gilad Ben-Yossef | d0082e1 | 2017-10-18 08:00:44 +0100 | [diff] [blame] | 149 | DECLARE_CRYPTO_WAIT(wait); |
Jaegeuk Kim | 0b81d07 | 2015-05-15 16:26:10 -0700 | [diff] [blame] | 150 | struct scatterlist src_sg, dst_sg; |
Eric Biggers | 8a4ab0b | 2019-12-15 13:39:47 -0800 | [diff] [blame] | 151 | const struct fscrypt_info *ci = inode->i_crypt_info; |
Satya Tangirala | 5fee360 | 2020-07-02 01:56:05 +0000 | [diff] [blame] | 152 | struct crypto_skcipher *tfm = ci->ci_enc_key.tfm; |
Eric Biggers | 8094c3c | 2019-01-06 08:36:21 -0500 | [diff] [blame] | 153 | union fscrypt_iv iv; |
| 154 | int res; |
Jaegeuk Kim | 0b81d07 | 2015-05-15 16:26:10 -0700 | [diff] [blame] | 155 | |
Jaegeuk Kim | 0b81d07 | 2015-05-15 16:26:10 -0700 | [diff] [blame] | 156 | /* Allocate request */ |
Linus Torvalds | d407574 | 2016-03-21 11:03:02 -0700 | [diff] [blame] | 157 | req = skcipher_request_alloc(tfm, GFP_NOFS); |
Eric Biggers | c90fd775 | 2018-04-30 15:51:38 -0700 | [diff] [blame] | 158 | if (!req) |
Jaegeuk Kim | 0b81d07 | 2015-05-15 16:26:10 -0700 | [diff] [blame] | 159 | return -ENOMEM; |
Linus Torvalds | d407574 | 2016-03-21 11:03:02 -0700 | [diff] [blame] | 160 | skcipher_request_set_callback(req, |
Jaegeuk Kim | 0b81d07 | 2015-05-15 16:26:10 -0700 | [diff] [blame] | 161 | CRYPTO_TFM_REQ_MAY_BACKLOG | CRYPTO_TFM_REQ_MAY_SLEEP, |
Gilad Ben-Yossef | d0082e1 | 2017-10-18 08:00:44 +0100 | [diff] [blame] | 162 | crypto_req_done, &wait); |
Jaegeuk Kim | 0b81d07 | 2015-05-15 16:26:10 -0700 | [diff] [blame] | 163 | |
| 164 | /* Initialize IV */ |
Eric Biggers | 8094c3c | 2019-01-06 08:36:21 -0500 | [diff] [blame] | 165 | fscrypt_generate_iv(&iv, 0, ci); |
Jaegeuk Kim | 0b81d07 | 2015-05-15 16:26:10 -0700 | [diff] [blame] | 166 | |
| 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 Biggers | 8094c3c | 2019-01-06 08:36:21 -0500 | [diff] [blame] | 170 | skcipher_request_set_crypt(req, &src_sg, &dst_sg, iname->len, &iv); |
Gilad Ben-Yossef | d0082e1 | 2017-10-18 08:00:44 +0100 | [diff] [blame] | 171 | res = crypto_wait_req(crypto_skcipher_decrypt(req), &wait); |
Linus Torvalds | d407574 | 2016-03-21 11:03:02 -0700 | [diff] [blame] | 172 | skcipher_request_free(req); |
Jaegeuk Kim | 0b81d07 | 2015-05-15 16:26:10 -0700 | [diff] [blame] | 173 | if (res < 0) { |
Eric Biggers | 886da8b | 2019-07-24 11:07:58 -0700 | [diff] [blame] | 174 | fscrypt_err(inode, "Filename decryption failed: %d", res); |
Jaegeuk Kim | 0b81d07 | 2015-05-15 16:26:10 -0700 | [diff] [blame] | 175 | return res; |
| 176 | } |
| 177 | |
| 178 | oname->len = strnlen(oname->name, iname->len); |
Eric Biggers | ef1eb3a | 2016-09-15 17:25:55 -0400 | [diff] [blame] | 179 | return 0; |
Jaegeuk Kim | 0b81d07 | 2015-05-15 16:26:10 -0700 | [diff] [blame] | 180 | } |
| 181 | |
Eric Biggers | ba47b51 | 2021-07-17 19:01:25 -0500 | [diff] [blame] | 182 | static const char base64url_table[65] = |
| 183 | "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789-_"; |
Jaegeuk Kim | 0b81d07 | 2015-05-15 16:26:10 -0700 | [diff] [blame] | 184 | |
Eric Biggers | ba47b51 | 2021-07-17 19:01:25 -0500 | [diff] [blame] | 185 | #define FSCRYPT_BASE64URL_CHARS(nbytes) DIV_ROUND_UP((nbytes) * 4, 3) |
Eric Biggers | 1715942 | 2017-04-24 10:00:10 -0700 | [diff] [blame] | 186 | |
Jaegeuk Kim | 0b81d07 | 2015-05-15 16:26:10 -0700 | [diff] [blame] | 187 | /** |
Eric Biggers | ba47b51 | 2021-07-17 19:01:25 -0500 | [diff] [blame] | 188 | * 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 Kim | 0b81d07 | 2015-05-15 16:26:10 -0700 | [diff] [blame] | 192 | * |
Eric Biggers | ba47b51 | 2021-07-17 19:01:25 -0500 | [diff] [blame] | 193 | * 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 Biggers | 1c5100a | 2019-07-24 11:07:58 -0700 | [diff] [blame] | 197 | * |
Eric Biggers | ba47b51 | 2021-07-17 19:01:25 -0500 | [diff] [blame] | 198 | * Return: the length of the resulting base64url-encoded string in bytes. |
| 199 | * This will be equal to FSCRYPT_BASE64URL_CHARS(srclen). |
Jaegeuk Kim | 0b81d07 | 2015-05-15 16:26:10 -0700 | [diff] [blame] | 200 | */ |
Eric Biggers | ba47b51 | 2021-07-17 19:01:25 -0500 | [diff] [blame] | 201 | static int fscrypt_base64url_encode(const u8 *src, int srclen, char *dst) |
Jaegeuk Kim | 0b81d07 | 2015-05-15 16:26:10 -0700 | [diff] [blame] | 202 | { |
Eric Biggers | ba47b51 | 2021-07-17 19:01:25 -0500 | [diff] [blame] | 203 | u32 ac = 0; |
| 204 | int bits = 0; |
| 205 | int i; |
Jaegeuk Kim | 0b81d07 | 2015-05-15 16:26:10 -0700 | [diff] [blame] | 206 | char *cp = dst; |
| 207 | |
Eric Biggers | ba47b51 | 2021-07-17 19:01:25 -0500 | [diff] [blame] | 208 | for (i = 0; i < srclen; i++) { |
| 209 | ac = (ac << 8) | src[i]; |
Jaegeuk Kim | 0b81d07 | 2015-05-15 16:26:10 -0700 | [diff] [blame] | 210 | bits += 8; |
| 211 | do { |
Jaegeuk Kim | 0b81d07 | 2015-05-15 16:26:10 -0700 | [diff] [blame] | 212 | bits -= 6; |
Eric Biggers | ba47b51 | 2021-07-17 19:01:25 -0500 | [diff] [blame] | 213 | *cp++ = base64url_table[(ac >> bits) & 0x3f]; |
Jaegeuk Kim | 0b81d07 | 2015-05-15 16:26:10 -0700 | [diff] [blame] | 214 | } while (bits >= 6); |
Jaegeuk Kim | 0b81d07 | 2015-05-15 16:26:10 -0700 | [diff] [blame] | 215 | } |
| 216 | if (bits) |
Eric Biggers | ba47b51 | 2021-07-17 19:01:25 -0500 | [diff] [blame] | 217 | *cp++ = base64url_table[(ac << (6 - bits)) & 0x3f]; |
Jaegeuk Kim | 0b81d07 | 2015-05-15 16:26:10 -0700 | [diff] [blame] | 218 | return cp - dst; |
| 219 | } |
| 220 | |
Eric Biggers | ba47b51 | 2021-07-17 19:01:25 -0500 | [diff] [blame] | 221 | /** |
| 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 | */ |
| 236 | static int fscrypt_base64url_decode(const char *src, int srclen, u8 *dst) |
Jaegeuk Kim | 0b81d07 | 2015-05-15 16:26:10 -0700 | [diff] [blame] | 237 | { |
Eric Biggers | ba47b51 | 2021-07-17 19:01:25 -0500 | [diff] [blame] | 238 | u32 ac = 0; |
| 239 | int bits = 0; |
| 240 | int i; |
| 241 | u8 *bp = dst; |
Jaegeuk Kim | 0b81d07 | 2015-05-15 16:26:10 -0700 | [diff] [blame] | 242 | |
Eric Biggers | ba47b51 | 2021-07-17 19:01:25 -0500 | [diff] [blame] | 243 | for (i = 0; i < srclen; i++) { |
| 244 | const char *p = strchr(base64url_table, src[i]); |
| 245 | |
Jaegeuk Kim | 0b81d07 | 2015-05-15 16:26:10 -0700 | [diff] [blame] | 246 | if (p == NULL || src[i] == 0) |
Eric Biggers | ba47b51 | 2021-07-17 19:01:25 -0500 | [diff] [blame] | 247 | return -1; |
| 248 | ac = (ac << 6) | (p - base64url_table); |
Jaegeuk Kim | 0b81d07 | 2015-05-15 16:26:10 -0700 | [diff] [blame] | 249 | bits += 6; |
| 250 | if (bits >= 8) { |
Jaegeuk Kim | 0b81d07 | 2015-05-15 16:26:10 -0700 | [diff] [blame] | 251 | bits -= 8; |
Eric Biggers | ba47b51 | 2021-07-17 19:01:25 -0500 | [diff] [blame] | 252 | *bp++ = (u8)(ac >> bits); |
Jaegeuk Kim | 0b81d07 | 2015-05-15 16:26:10 -0700 | [diff] [blame] | 253 | } |
Jaegeuk Kim | 0b81d07 | 2015-05-15 16:26:10 -0700 | [diff] [blame] | 254 | } |
Eric Biggers | ba47b51 | 2021-07-17 19:01:25 -0500 | [diff] [blame] | 255 | if (ac & ((1 << bits) - 1)) |
Jaegeuk Kim | 0b81d07 | 2015-05-15 16:26:10 -0700 | [diff] [blame] | 256 | return -1; |
Eric Biggers | ba47b51 | 2021-07-17 19:01:25 -0500 | [diff] [blame] | 257 | return bp - dst; |
Jaegeuk Kim | 0b81d07 | 2015-05-15 16:26:10 -0700 | [diff] [blame] | 258 | } |
| 259 | |
Eric Biggers | ac4acb1 | 2020-09-16 21:11:35 -0700 | [diff] [blame] | 260 | bool fscrypt_fname_encrypted_size(const union fscrypt_policy *policy, |
| 261 | u32 orig_len, u32 max_len, |
| 262 | u32 *encrypted_len_ret) |
Jaegeuk Kim | 0b81d07 | 2015-05-15 16:26:10 -0700 | [diff] [blame] | 263 | { |
Eric Biggers | ac4acb1 | 2020-09-16 21:11:35 -0700 | [diff] [blame] | 264 | int padding = 4 << (fscrypt_policy_flags(policy) & |
Eric Biggers | 3b6df59 | 2019-08-04 19:35:44 -0700 | [diff] [blame] | 265 | FSCRYPT_POLICY_FLAGS_PAD_MASK); |
Eric Biggers | b9db0b4 | 2018-01-11 23:30:08 -0500 | [diff] [blame] | 266 | u32 encrypted_len; |
Jaegeuk Kim | 0b81d07 | 2015-05-15 16:26:10 -0700 | [diff] [blame] | 267 | |
Eric Biggers | b9db0b4 | 2018-01-11 23:30:08 -0500 | [diff] [blame] | 268 | 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 Kim | 0b81d07 | 2015-05-15 16:26:10 -0700 | [diff] [blame] | 274 | } |
Jaegeuk Kim | 0b81d07 | 2015-05-15 16:26:10 -0700 | [diff] [blame] | 275 | |
| 276 | /** |
Eric Biggers | d2fe975 | 2020-05-11 12:13:56 -0700 | [diff] [blame] | 277 | * fscrypt_fname_alloc_buffer() - allocate a buffer for presented filenames |
Eric Biggers | d2fe975 | 2020-05-11 12:13:56 -0700 | [diff] [blame] | 278 | * @max_encrypted_len: maximum length of encrypted filenames the buffer will be |
| 279 | * used to present |
| 280 | * @crypto_str: (output) buffer to allocate |
Jaegeuk Kim | 0b81d07 | 2015-05-15 16:26:10 -0700 | [diff] [blame] | 281 | * |
Eric Biggers | 2cbadad | 2018-01-11 23:30:08 -0500 | [diff] [blame] | 282 | * 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 Kim | 0b81d07 | 2015-05-15 16:26:10 -0700 | [diff] [blame] | 286 | */ |
Jeff Layton | 8b10fe6 | 2020-08-10 10:21:39 -0400 | [diff] [blame] | 287 | int fscrypt_fname_alloc_buffer(u32 max_encrypted_len, |
Eric Biggers | 2cbadad | 2018-01-11 23:30:08 -0500 | [diff] [blame] | 288 | struct fscrypt_str *crypto_str) |
Jaegeuk Kim | 0b81d07 | 2015-05-15 16:26:10 -0700 | [diff] [blame] | 289 | { |
Eric Biggers | ba47b51 | 2021-07-17 19:01:25 -0500 | [diff] [blame] | 290 | u32 max_presented_len = max_t(u32, FSCRYPT_NOKEY_NAME_MAX_ENCODED, |
| 291 | max_encrypted_len); |
Eric Biggers | 1715942 | 2017-04-24 10:00:10 -0700 | [diff] [blame] | 292 | |
Eric Biggers | 2cbadad | 2018-01-11 23:30:08 -0500 | [diff] [blame] | 293 | crypto_str->name = kmalloc(max_presented_len + 1, GFP_NOFS); |
| 294 | if (!crypto_str->name) |
Jaegeuk Kim | 0b81d07 | 2015-05-15 16:26:10 -0700 | [diff] [blame] | 295 | return -ENOMEM; |
Eric Biggers | 2cbadad | 2018-01-11 23:30:08 -0500 | [diff] [blame] | 296 | crypto_str->len = max_presented_len; |
Jaegeuk Kim | 0b81d07 | 2015-05-15 16:26:10 -0700 | [diff] [blame] | 297 | return 0; |
| 298 | } |
| 299 | EXPORT_SYMBOL(fscrypt_fname_alloc_buffer); |
| 300 | |
| 301 | /** |
Eric Biggers | d2fe975 | 2020-05-11 12:13:56 -0700 | [diff] [blame] | 302 | * fscrypt_fname_free_buffer() - free a buffer for presented filenames |
| 303 | * @crypto_str: the buffer to free |
Jaegeuk Kim | 0b81d07 | 2015-05-15 16:26:10 -0700 | [diff] [blame] | 304 | * |
Eric Biggers | d2fe975 | 2020-05-11 12:13:56 -0700 | [diff] [blame] | 305 | * Free a buffer that was allocated by fscrypt_fname_alloc_buffer(). |
Jaegeuk Kim | 0b81d07 | 2015-05-15 16:26:10 -0700 | [diff] [blame] | 306 | */ |
| 307 | void 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 | } |
| 314 | EXPORT_SYMBOL(fscrypt_fname_free_buffer); |
| 315 | |
| 316 | /** |
Eric Biggers | d2fe975 | 2020-05-11 12:13:56 -0700 | [diff] [blame] | 317 | * 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 Biggers | ef1eb3a | 2016-09-15 17:25:55 -0400 | [diff] [blame] | 330 | * |
Daniel Rosenberg | edc440e | 2020-01-20 14:32:01 -0800 | [diff] [blame] | 331 | * 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 Biggers | 1715942 | 2017-04-24 10:00:10 -0700 | [diff] [blame] | 334 | * |
Eric Biggers | ef1eb3a | 2016-09-15 17:25:55 -0400 | [diff] [blame] | 335 | * Return: 0 on success, -errno on failure |
Jaegeuk Kim | 0b81d07 | 2015-05-15 16:26:10 -0700 | [diff] [blame] | 336 | */ |
Eric Biggers | 8a4ab0b | 2019-12-15 13:39:47 -0800 | [diff] [blame] | 337 | int 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 Kim | 0b81d07 | 2015-05-15 16:26:10 -0700 | [diff] [blame] | 341 | { |
| 342 | const struct qstr qname = FSTR_TO_QSTR(iname); |
Daniel Rosenberg | edc440e | 2020-01-20 14:32:01 -0800 | [diff] [blame] | 343 | struct fscrypt_nokey_name nokey_name; |
| 344 | u32 size; /* size of the unencoded no-key name */ |
Jaegeuk Kim | 0b81d07 | 2015-05-15 16:26:10 -0700 | [diff] [blame] | 345 | |
| 346 | if (fscrypt_is_dot_dotdot(&qname)) { |
| 347 | oname->name[0] = '.'; |
| 348 | oname->name[iname->len - 1] = '.'; |
| 349 | oname->len = iname->len; |
Eric Biggers | ef1eb3a | 2016-09-15 17:25:55 -0400 | [diff] [blame] | 350 | return 0; |
Jaegeuk Kim | 0b81d07 | 2015-05-15 16:26:10 -0700 | [diff] [blame] | 351 | } |
| 352 | |
| 353 | if (iname->len < FS_CRYPTO_BLOCK_SIZE) |
| 354 | return -EUCLEAN; |
| 355 | |
Eric Biggers | e37a784 | 2019-04-11 14:32:15 -0700 | [diff] [blame] | 356 | if (fscrypt_has_encryption_key(inode)) |
Jaegeuk Kim | 0b81d07 | 2015-05-15 16:26:10 -0700 | [diff] [blame] | 357 | return fname_decrypt(inode, iname, oname); |
| 358 | |
Daniel Rosenberg | edc440e | 2020-01-20 14:32:01 -0800 | [diff] [blame] | 359 | /* |
| 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 Biggers | ba47b51 | 2021-07-17 19:01:25 -0500 | [diff] [blame] | 367 | BUILD_BUG_ON(FSCRYPT_NOKEY_NAME_MAX_ENCODED > NAME_MAX); |
Daniel Rosenberg | edc440e | 2020-01-20 14:32:01 -0800 | [diff] [blame] | 368 | |
Eric Biggers | 77f30bf | 2021-05-27 16:52:36 -0700 | [diff] [blame] | 369 | nokey_name.dirhash[0] = hash; |
| 370 | nokey_name.dirhash[1] = minor_hash; |
| 371 | |
Daniel Rosenberg | edc440e | 2020-01-20 14:32:01 -0800 | [diff] [blame] | 372 | 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 Biggers | 0c6a113 | 2020-09-16 21:53:41 -0700 | [diff] [blame] | 378 | sha256(&iname->name[sizeof(nokey_name.bytes)], |
| 379 | iname->len - sizeof(nokey_name.bytes), |
| 380 | nokey_name.sha256); |
Daniel Rosenberg | edc440e | 2020-01-20 14:32:01 -0800 | [diff] [blame] | 381 | size = FSCRYPT_NOKEY_NAME_MAX; |
| 382 | } |
Eric Biggers | ba47b51 | 2021-07-17 19:01:25 -0500 | [diff] [blame] | 383 | oname->len = fscrypt_base64url_encode((const u8 *)&nokey_name, size, |
| 384 | oname->name); |
Eric Biggers | ef1eb3a | 2016-09-15 17:25:55 -0400 | [diff] [blame] | 385 | return 0; |
Jaegeuk Kim | 0b81d07 | 2015-05-15 16:26:10 -0700 | [diff] [blame] | 386 | } |
| 387 | EXPORT_SYMBOL(fscrypt_fname_disk_to_usr); |
| 388 | |
| 389 | /** |
Eric Biggers | 1715942 | 2017-04-24 10:00:10 -0700 | [diff] [blame] | 390 | * 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 Biggers | 70fb261 | 2020-09-23 21:26:23 -0700 | [diff] [blame] | 404 | * 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 Biggers | 1715942 | 2017-04-24 10:00:10 -0700 | [diff] [blame] | 407 | * |
| 408 | * If successful, fscrypt_free_filename() must be called later to clean up. |
| 409 | * |
| 410 | * Return: 0 on success, -errno on failure |
| 411 | */ |
Jaegeuk Kim | 0b81d07 | 2015-05-15 16:26:10 -0700 | [diff] [blame] | 412 | int fscrypt_setup_filename(struct inode *dir, const struct qstr *iname, |
| 413 | int lookup, struct fscrypt_name *fname) |
| 414 | { |
Daniel Rosenberg | edc440e | 2020-01-20 14:32:01 -0800 | [diff] [blame] | 415 | struct fscrypt_nokey_name *nokey_name; |
Eric Biggers | 1715942 | 2017-04-24 10:00:10 -0700 | [diff] [blame] | 416 | int ret; |
Jaegeuk Kim | 0b81d07 | 2015-05-15 16:26:10 -0700 | [diff] [blame] | 417 | |
| 418 | memset(fname, 0, sizeof(struct fscrypt_name)); |
| 419 | fname->usr_fname = iname; |
| 420 | |
Eric Biggers | e0428a2 | 2017-10-09 12:15:36 -0700 | [diff] [blame] | 421 | if (!IS_ENCRYPTED(dir) || fscrypt_is_dot_dotdot(iname)) { |
Jaegeuk Kim | 0b81d07 | 2015-05-15 16:26:10 -0700 | [diff] [blame] | 422 | fname->disk_name.name = (unsigned char *)iname->name; |
| 423 | fname->disk_name.len = iname->len; |
| 424 | return 0; |
| 425 | } |
Eric Biggers | a14d0b6 | 2020-12-02 18:20:41 -0800 | [diff] [blame] | 426 | ret = fscrypt_get_encryption_info(dir, lookup); |
Eric Biggers | 17bfde6 | 2018-04-30 15:51:41 -0700 | [diff] [blame] | 427 | if (ret) |
Jaegeuk Kim | 0b81d07 | 2015-05-15 16:26:10 -0700 | [diff] [blame] | 428 | return ret; |
| 429 | |
Eric Biggers | e37a784 | 2019-04-11 14:32:15 -0700 | [diff] [blame] | 430 | if (fscrypt_has_encryption_key(dir)) { |
Eric Biggers | ac4acb1 | 2020-09-16 21:11:35 -0700 | [diff] [blame] | 431 | if (!fscrypt_fname_encrypted_size(&dir->i_crypt_info->ci_policy, |
Eric Biggers | 4373b3d | 2021-09-09 11:45:13 -0700 | [diff] [blame] | 432 | iname->len, NAME_MAX, |
Eric Biggers | b9db0b4 | 2018-01-11 23:30:08 -0500 | [diff] [blame] | 433 | &fname->crypto_buf.len)) |
Eric Biggers | 50c961d | 2018-01-11 23:30:08 -0500 | [diff] [blame] | 434 | return -ENAMETOOLONG; |
Eric Biggers | 50c961d | 2018-01-11 23:30:08 -0500 | [diff] [blame] | 435 | fname->crypto_buf.name = kmalloc(fname->crypto_buf.len, |
| 436 | GFP_NOFS); |
| 437 | if (!fname->crypto_buf.name) |
| 438 | return -ENOMEM; |
| 439 | |
Eric Biggers | 1b3b827 | 2020-01-19 23:17:36 -0800 | [diff] [blame] | 440 | ret = fscrypt_fname_encrypt(dir, iname, fname->crypto_buf.name, |
| 441 | fname->crypto_buf.len); |
Eric Biggers | ef1eb3a | 2016-09-15 17:25:55 -0400 | [diff] [blame] | 442 | if (ret) |
Jaegeuk Kim | 0b81d07 | 2015-05-15 16:26:10 -0700 | [diff] [blame] | 443 | 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 Biggers | 54475f5 | 2016-12-05 11:12:44 -0800 | [diff] [blame] | 449 | return -ENOKEY; |
Eric Biggers | 70fb261 | 2020-09-23 21:26:23 -0700 | [diff] [blame] | 450 | fname->is_nokey_name = true; |
Jaegeuk Kim | 0b81d07 | 2015-05-15 16:26:10 -0700 | [diff] [blame] | 451 | |
| 452 | /* |
| 453 | * We don't have the key and we are doing a lookup; decode the |
| 454 | * user-supplied name |
| 455 | */ |
Jaegeuk Kim | 0b81d07 | 2015-05-15 16:26:10 -0700 | [diff] [blame] | 456 | |
Eric Biggers | ba47b51 | 2021-07-17 19:01:25 -0500 | [diff] [blame] | 457 | if (iname->len > FSCRYPT_NOKEY_NAME_MAX_ENCODED) |
Daniel Rosenberg | edc440e | 2020-01-20 14:32:01 -0800 | [diff] [blame] | 458 | return -ENOENT; |
| 459 | |
| 460 | fname->crypto_buf.name = kmalloc(FSCRYPT_NOKEY_NAME_MAX, GFP_KERNEL); |
Jaegeuk Kim | 0b81d07 | 2015-05-15 16:26:10 -0700 | [diff] [blame] | 461 | if (fname->crypto_buf.name == NULL) |
| 462 | return -ENOMEM; |
| 463 | |
Eric Biggers | ba47b51 | 2021-07-17 19:01:25 -0500 | [diff] [blame] | 464 | ret = fscrypt_base64url_decode(iname->name, iname->len, |
| 465 | fname->crypto_buf.name); |
Daniel Rosenberg | edc440e | 2020-01-20 14:32:01 -0800 | [diff] [blame] | 466 | 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 Kim | 0b81d07 | 2015-05-15 16:26:10 -0700 | [diff] [blame] | 469 | ret = -ENOENT; |
| 470 | goto errout; |
| 471 | } |
| 472 | fname->crypto_buf.len = ret; |
Daniel Rosenberg | edc440e | 2020-01-20 14:32:01 -0800 | [diff] [blame] | 473 | |
| 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 Kim | 0b81d07 | 2015-05-15 16:26:10 -0700 | [diff] [blame] | 482 | } |
| 483 | return 0; |
| 484 | |
| 485 | errout: |
Eric Biggers | 50c961d | 2018-01-11 23:30:08 -0500 | [diff] [blame] | 486 | kfree(fname->crypto_buf.name); |
Jaegeuk Kim | 0b81d07 | 2015-05-15 16:26:10 -0700 | [diff] [blame] | 487 | return ret; |
| 488 | } |
| 489 | EXPORT_SYMBOL(fscrypt_setup_filename); |
Eric Biggers | 2ebdef6 | 2019-12-09 12:43:59 -0800 | [diff] [blame] | 490 | |
Daniel Rosenberg | aa408f8 | 2020-01-20 14:31:57 -0800 | [diff] [blame] | 491 | /** |
Daniel Rosenberg | edc440e | 2020-01-20 14:32:01 -0800 | [diff] [blame] | 492 | * 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 | */ |
| 505 | bool 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 Biggers | 0c6a113 | 2020-09-16 21:53:41 -0700 | [diff] [blame] | 510 | u8 digest[SHA256_DIGEST_SIZE]; |
Daniel Rosenberg | edc440e | 2020-01-20 14:32:01 -0800 | [diff] [blame] | 511 | |
| 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 Biggers | 0c6a113 | 2020-09-16 21:53:41 -0700 | [diff] [blame] | 521 | 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 Rosenberg | edc440e | 2020-01-20 14:32:01 -0800 | [diff] [blame] | 524 | } |
| 525 | EXPORT_SYMBOL_GPL(fscrypt_match_name); |
| 526 | |
| 527 | /** |
Daniel Rosenberg | aa408f8 | 2020-01-20 14:31:57 -0800 | [diff] [blame] | 528 | * 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 | */ |
| 538 | u64 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 | } |
| 546 | EXPORT_SYMBOL_GPL(fscrypt_fname_siphash); |
| 547 | |
Eric Biggers | 2ebdef6 | 2019-12-09 12:43:59 -0800 | [diff] [blame] | 548 | /* |
| 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 Biggers | 5b2a828 | 2020-09-23 22:47:21 -0700 | [diff] [blame] | 552 | int fscrypt_d_revalidate(struct dentry *dentry, unsigned int flags) |
Eric Biggers | 2ebdef6 | 2019-12-09 12:43:59 -0800 | [diff] [blame] | 553 | { |
| 554 | struct dentry *dir; |
| 555 | int err; |
| 556 | int valid; |
| 557 | |
| 558 | /* |
| 559 | * Plaintext names are always valid, since fscrypt doesn't support |
Eric Biggers | 70fb261 | 2020-09-23 21:26:23 -0700 | [diff] [blame] | 560 | * reverting to no-key names without evicting the directory's inode |
Eric Biggers | 2ebdef6 | 2019-12-09 12:43:59 -0800 | [diff] [blame] | 561 | * -- which implies eviction of the dentries in the directory. |
| 562 | */ |
Eric Biggers | 501e43f | 2020-09-23 21:26:24 -0700 | [diff] [blame] | 563 | if (!(dentry->d_flags & DCACHE_NOKEY_NAME)) |
Eric Biggers | 2ebdef6 | 2019-12-09 12:43:59 -0800 | [diff] [blame] | 564 | return 1; |
| 565 | |
| 566 | /* |
Eric Biggers | 70fb261 | 2020-09-23 21:26:23 -0700 | [diff] [blame] | 567 | * No-key name; valid if the directory's key is still unavailable. |
Eric Biggers | 2ebdef6 | 2019-12-09 12:43:59 -0800 | [diff] [blame] | 568 | * |
Eric Biggers | 70fb261 | 2020-09-23 21:26:23 -0700 | [diff] [blame] | 569 | * 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 Biggers | 2ebdef6 | 2019-12-09 12:43:59 -0800 | [diff] [blame] | 571 | * 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 Biggers | a14d0b6 | 2020-12-02 18:20:41 -0800 | [diff] [blame] | 582 | /* |
| 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 Biggers | 2ebdef6 | 2019-12-09 12:43:59 -0800 | [diff] [blame] | 587 | 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 Biggers | 5b2a828 | 2020-09-23 22:47:21 -0700 | [diff] [blame] | 595 | EXPORT_SYMBOL_GPL(fscrypt_d_revalidate); |