Jaegeuk Kim | 0b81d07 | 2015-05-15 16:26:10 -0700 | [diff] [blame] | 1 | /* |
| 2 | * This contains functions for filename crypto management |
| 3 | * |
| 4 | * Copyright (C) 2015, Google, Inc. |
| 5 | * Copyright (C) 2015, Motorola Mobility |
| 6 | * |
| 7 | * Written by Uday Savagaonkar, 2014. |
| 8 | * Modified by Jaegeuk Kim, 2015. |
| 9 | * |
| 10 | * This has not yet undergone a rigorous security audit. |
| 11 | */ |
| 12 | |
Jaegeuk Kim | 0b81d07 | 2015-05-15 16:26:10 -0700 | [diff] [blame] | 13 | #include <linux/scatterlist.h> |
| 14 | #include <linux/ratelimit.h> |
Eric Biggers | eb9c5fd | 2018-01-05 10:45:00 -0800 | [diff] [blame] | 15 | #include <crypto/skcipher.h> |
Jaegeuk Kim | e6b120d | 2017-07-10 12:55:09 -0700 | [diff] [blame] | 16 | #include "fscrypt_private.h" |
Jaegeuk Kim | 0b81d07 | 2015-05-15 16:26:10 -0700 | [diff] [blame] | 17 | |
Eric Biggers | 0a02472 | 2018-01-05 10:44:59 -0800 | [diff] [blame] | 18 | static inline bool fscrypt_is_dot_dotdot(const struct qstr *str) |
| 19 | { |
| 20 | if (str->len == 1 && str->name[0] == '.') |
| 21 | return true; |
| 22 | |
| 23 | if (str->len == 2 && str->name[0] == '.' && str->name[1] == '.') |
| 24 | return true; |
| 25 | |
| 26 | return false; |
| 27 | } |
| 28 | |
Jaegeuk Kim | 0b81d07 | 2015-05-15 16:26:10 -0700 | [diff] [blame] | 29 | /** |
Eric Biggers | ef1eb3a | 2016-09-15 17:25:55 -0400 | [diff] [blame] | 30 | * fname_encrypt() - encrypt a filename |
Jaegeuk Kim | 0b81d07 | 2015-05-15 16:26:10 -0700 | [diff] [blame] | 31 | * |
Eric Biggers | b0edc2f | 2018-01-11 23:30:08 -0500 | [diff] [blame] | 32 | * The output buffer must be at least as large as the input buffer. |
| 33 | * Any extra space is filled with NUL padding before encryption. |
Eric Biggers | ef1eb3a | 2016-09-15 17:25:55 -0400 | [diff] [blame] | 34 | * |
| 35 | * Return: 0 on success, -errno on failure |
Jaegeuk Kim | 0b81d07 | 2015-05-15 16:26:10 -0700 | [diff] [blame] | 36 | */ |
Eric Biggers | b0edc2f | 2018-01-11 23:30:08 -0500 | [diff] [blame] | 37 | int fname_encrypt(struct inode *inode, const struct qstr *iname, |
| 38 | u8 *out, unsigned int olen) |
Jaegeuk Kim | 0b81d07 | 2015-05-15 16:26:10 -0700 | [diff] [blame] | 39 | { |
Linus Torvalds | d407574 | 2016-03-21 11:03:02 -0700 | [diff] [blame] | 40 | struct skcipher_request *req = NULL; |
Gilad Ben-Yossef | 743205f | 2017-10-18 08:00:44 +0100 | [diff] [blame] | 41 | DECLARE_CRYPTO_WAIT(wait); |
Eric Biggers | b0edc2f | 2018-01-11 23:30:08 -0500 | [diff] [blame] | 42 | struct crypto_skcipher *tfm = inode->i_crypt_info->ci_ctfm; |
Jaegeuk Kim | 0b81d07 | 2015-05-15 16:26:10 -0700 | [diff] [blame] | 43 | int res = 0; |
| 44 | char iv[FS_CRYPTO_BLOCK_SIZE]; |
Eric Biggers | 3c7018e | 2016-11-13 20:35:52 -0500 | [diff] [blame] | 45 | struct scatterlist sg; |
Jaegeuk Kim | 0b81d07 | 2015-05-15 16:26:10 -0700 | [diff] [blame] | 46 | |
Eric Biggers | 3c7018e | 2016-11-13 20:35:52 -0500 | [diff] [blame] | 47 | /* |
| 48 | * Copy the filename to the output buffer for encrypting in-place and |
| 49 | * pad it with the needed number of NUL bytes. |
| 50 | */ |
Eric Biggers | b0edc2f | 2018-01-11 23:30:08 -0500 | [diff] [blame] | 51 | if (WARN_ON(olen < iname->len)) |
Eric Biggers | a7e05c7 | 2018-01-05 10:45:01 -0800 | [diff] [blame] | 52 | return -ENOBUFS; |
Eric Biggers | b0edc2f | 2018-01-11 23:30:08 -0500 | [diff] [blame] | 53 | memcpy(out, iname->name, iname->len); |
| 54 | memset(out + iname->len, 0, olen - iname->len); |
Jaegeuk Kim | 0b81d07 | 2015-05-15 16:26:10 -0700 | [diff] [blame] | 55 | |
Eric Biggers | 3c7018e | 2016-11-13 20:35:52 -0500 | [diff] [blame] | 56 | /* Initialize the IV */ |
| 57 | memset(iv, 0, FS_CRYPTO_BLOCK_SIZE); |
Jaegeuk Kim | 0b81d07 | 2015-05-15 16:26:10 -0700 | [diff] [blame] | 58 | |
Eric Biggers | 3c7018e | 2016-11-13 20:35:52 -0500 | [diff] [blame] | 59 | /* Set up the encryption request */ |
Linus Torvalds | d407574 | 2016-03-21 11:03:02 -0700 | [diff] [blame] | 60 | req = skcipher_request_alloc(tfm, GFP_NOFS); |
Eric Biggers | 1e04ac8 | 2018-04-30 15:51:38 -0700 | [diff] [blame] | 61 | if (!req) |
Jaegeuk Kim | 0b81d07 | 2015-05-15 16:26:10 -0700 | [diff] [blame] | 62 | return -ENOMEM; |
Linus Torvalds | d407574 | 2016-03-21 11:03:02 -0700 | [diff] [blame] | 63 | skcipher_request_set_callback(req, |
Jaegeuk Kim | 0b81d07 | 2015-05-15 16:26:10 -0700 | [diff] [blame] | 64 | CRYPTO_TFM_REQ_MAY_BACKLOG | CRYPTO_TFM_REQ_MAY_SLEEP, |
Gilad Ben-Yossef | 743205f | 2017-10-18 08:00:44 +0100 | [diff] [blame] | 65 | crypto_req_done, &wait); |
Eric Biggers | b0edc2f | 2018-01-11 23:30:08 -0500 | [diff] [blame] | 66 | sg_init_one(&sg, out, olen); |
| 67 | skcipher_request_set_crypt(req, &sg, &sg, olen, iv); |
Jaegeuk Kim | 0b81d07 | 2015-05-15 16:26:10 -0700 | [diff] [blame] | 68 | |
Eric Biggers | 3c7018e | 2016-11-13 20:35:52 -0500 | [diff] [blame] | 69 | /* Do the encryption */ |
Gilad Ben-Yossef | 743205f | 2017-10-18 08:00:44 +0100 | [diff] [blame] | 70 | res = crypto_wait_req(crypto_skcipher_encrypt(req), &wait); |
Linus Torvalds | d407574 | 2016-03-21 11:03:02 -0700 | [diff] [blame] | 71 | skcipher_request_free(req); |
Eric Biggers | ef1eb3a | 2016-09-15 17:25:55 -0400 | [diff] [blame] | 72 | if (res < 0) { |
Eric Biggers | 78275d8 | 2018-04-30 15:51:47 -0700 | [diff] [blame] | 73 | fscrypt_err(inode->i_sb, |
| 74 | "Filename encryption failed for inode %lu: %d", |
| 75 | inode->i_ino, res); |
Eric Biggers | ef1eb3a | 2016-09-15 17:25:55 -0400 | [diff] [blame] | 76 | return res; |
| 77 | } |
Jaegeuk Kim | 0b81d07 | 2015-05-15 16:26:10 -0700 | [diff] [blame] | 78 | |
Eric Biggers | ef1eb3a | 2016-09-15 17:25:55 -0400 | [diff] [blame] | 79 | return 0; |
Jaegeuk Kim | 0b81d07 | 2015-05-15 16:26:10 -0700 | [diff] [blame] | 80 | } |
| 81 | |
Eric Biggers | ef1eb3a | 2016-09-15 17:25:55 -0400 | [diff] [blame] | 82 | /** |
| 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 Kim | 0b81d07 | 2015-05-15 16:26:10 -0700 | [diff] [blame] | 88 | */ |
| 89 | static int fname_decrypt(struct inode *inode, |
| 90 | const struct fscrypt_str *iname, |
| 91 | struct fscrypt_str *oname) |
| 92 | { |
Linus Torvalds | d407574 | 2016-03-21 11:03:02 -0700 | [diff] [blame] | 93 | struct skcipher_request *req = NULL; |
Gilad Ben-Yossef | 743205f | 2017-10-18 08:00:44 +0100 | [diff] [blame] | 94 | DECLARE_CRYPTO_WAIT(wait); |
Jaegeuk Kim | 0b81d07 | 2015-05-15 16:26:10 -0700 | [diff] [blame] | 95 | struct scatterlist src_sg, dst_sg; |
Eric Biggers | e2a5784 | 2018-04-30 15:51:42 -0700 | [diff] [blame] | 96 | struct crypto_skcipher *tfm = inode->i_crypt_info->ci_ctfm; |
Jaegeuk Kim | 0b81d07 | 2015-05-15 16:26:10 -0700 | [diff] [blame] | 97 | int res = 0; |
| 98 | char iv[FS_CRYPTO_BLOCK_SIZE]; |
Jaegeuk Kim | 0b81d07 | 2015-05-15 16:26:10 -0700 | [diff] [blame] | 99 | |
Jaegeuk Kim | 0b81d07 | 2015-05-15 16:26:10 -0700 | [diff] [blame] | 100 | /* Allocate request */ |
Linus Torvalds | d407574 | 2016-03-21 11:03:02 -0700 | [diff] [blame] | 101 | req = skcipher_request_alloc(tfm, GFP_NOFS); |
Eric Biggers | 1e04ac8 | 2018-04-30 15:51:38 -0700 | [diff] [blame] | 102 | if (!req) |
Jaegeuk Kim | 0b81d07 | 2015-05-15 16:26:10 -0700 | [diff] [blame] | 103 | return -ENOMEM; |
Linus Torvalds | d407574 | 2016-03-21 11:03:02 -0700 | [diff] [blame] | 104 | skcipher_request_set_callback(req, |
Jaegeuk Kim | 0b81d07 | 2015-05-15 16:26:10 -0700 | [diff] [blame] | 105 | CRYPTO_TFM_REQ_MAY_BACKLOG | CRYPTO_TFM_REQ_MAY_SLEEP, |
Gilad Ben-Yossef | 743205f | 2017-10-18 08:00:44 +0100 | [diff] [blame] | 106 | crypto_req_done, &wait); |
Jaegeuk Kim | 0b81d07 | 2015-05-15 16:26:10 -0700 | [diff] [blame] | 107 | |
| 108 | /* Initialize IV */ |
| 109 | memset(iv, 0, FS_CRYPTO_BLOCK_SIZE); |
| 110 | |
| 111 | /* Create decryption request */ |
| 112 | sg_init_one(&src_sg, iname->name, iname->len); |
| 113 | sg_init_one(&dst_sg, oname->name, oname->len); |
Linus Torvalds | d407574 | 2016-03-21 11:03:02 -0700 | [diff] [blame] | 114 | skcipher_request_set_crypt(req, &src_sg, &dst_sg, iname->len, iv); |
Gilad Ben-Yossef | 743205f | 2017-10-18 08:00:44 +0100 | [diff] [blame] | 115 | res = crypto_wait_req(crypto_skcipher_decrypt(req), &wait); |
Linus Torvalds | d407574 | 2016-03-21 11:03:02 -0700 | [diff] [blame] | 116 | skcipher_request_free(req); |
Jaegeuk Kim | 0b81d07 | 2015-05-15 16:26:10 -0700 | [diff] [blame] | 117 | if (res < 0) { |
Eric Biggers | 78275d8 | 2018-04-30 15:51:47 -0700 | [diff] [blame] | 118 | fscrypt_err(inode->i_sb, |
| 119 | "Filename decryption failed for inode %lu: %d", |
| 120 | inode->i_ino, res); |
Jaegeuk Kim | 0b81d07 | 2015-05-15 16:26:10 -0700 | [diff] [blame] | 121 | return res; |
| 122 | } |
| 123 | |
| 124 | oname->len = strnlen(oname->name, iname->len); |
Eric Biggers | ef1eb3a | 2016-09-15 17:25:55 -0400 | [diff] [blame] | 125 | return 0; |
Jaegeuk Kim | 0b81d07 | 2015-05-15 16:26:10 -0700 | [diff] [blame] | 126 | } |
| 127 | |
| 128 | static const char *lookup_table = |
| 129 | "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+,"; |
| 130 | |
Jaegeuk Kim | e6b120d | 2017-07-10 12:55:09 -0700 | [diff] [blame] | 131 | #define BASE64_CHARS(nbytes) DIV_ROUND_UP((nbytes) * 4, 3) |
| 132 | |
Jaegeuk Kim | 0b81d07 | 2015-05-15 16:26:10 -0700 | [diff] [blame] | 133 | /** |
| 134 | * digest_encode() - |
| 135 | * |
| 136 | * Encodes the input digest using characters from the set [a-zA-Z0-9_+]. |
| 137 | * The encoded string is roughly 4/3 times the size of the input string. |
| 138 | */ |
| 139 | static int digest_encode(const char *src, int len, char *dst) |
| 140 | { |
| 141 | int i = 0, bits = 0, ac = 0; |
| 142 | char *cp = dst; |
| 143 | |
| 144 | while (i < len) { |
| 145 | ac += (((unsigned char) src[i]) << bits); |
| 146 | bits += 8; |
| 147 | do { |
| 148 | *cp++ = lookup_table[ac & 0x3f]; |
| 149 | ac >>= 6; |
| 150 | bits -= 6; |
| 151 | } while (bits >= 6); |
| 152 | i++; |
| 153 | } |
| 154 | if (bits) |
| 155 | *cp++ = lookup_table[ac & 0x3f]; |
| 156 | return cp - dst; |
| 157 | } |
| 158 | |
| 159 | static int digest_decode(const char *src, int len, char *dst) |
| 160 | { |
| 161 | int i = 0, bits = 0, ac = 0; |
| 162 | const char *p; |
| 163 | char *cp = dst; |
| 164 | |
| 165 | while (i < len) { |
| 166 | p = strchr(lookup_table, src[i]); |
| 167 | if (p == NULL || src[i] == 0) |
| 168 | return -2; |
| 169 | ac += (p - lookup_table) << bits; |
| 170 | bits += 6; |
| 171 | if (bits >= 8) { |
| 172 | *cp++ = ac & 0xff; |
| 173 | ac >>= 8; |
| 174 | bits -= 8; |
| 175 | } |
| 176 | i++; |
| 177 | } |
| 178 | if (ac) |
| 179 | return -1; |
| 180 | return cp - dst; |
| 181 | } |
| 182 | |
Eric Biggers | 549b206 | 2018-01-11 23:30:08 -0500 | [diff] [blame] | 183 | bool fscrypt_fname_encrypted_size(const struct inode *inode, u32 orig_len, |
| 184 | u32 max_len, u32 *encrypted_len_ret) |
Jaegeuk Kim | 0b81d07 | 2015-05-15 16:26:10 -0700 | [diff] [blame] | 185 | { |
Eric Biggers | 549b206 | 2018-01-11 23:30:08 -0500 | [diff] [blame] | 186 | int padding = 4 << (inode->i_crypt_info->ci_flags & |
| 187 | FS_POLICY_FLAGS_PAD_MASK); |
| 188 | u32 encrypted_len; |
Jaegeuk Kim | 0b81d07 | 2015-05-15 16:26:10 -0700 | [diff] [blame] | 189 | |
Eric Biggers | 549b206 | 2018-01-11 23:30:08 -0500 | [diff] [blame] | 190 | if (orig_len > max_len) |
| 191 | return false; |
| 192 | encrypted_len = max(orig_len, (u32)FS_CRYPTO_BLOCK_SIZE); |
| 193 | encrypted_len = round_up(encrypted_len, padding); |
| 194 | *encrypted_len_ret = min(encrypted_len, max_len); |
| 195 | return true; |
Jaegeuk Kim | 0b81d07 | 2015-05-15 16:26:10 -0700 | [diff] [blame] | 196 | } |
Jaegeuk Kim | 0b81d07 | 2015-05-15 16:26:10 -0700 | [diff] [blame] | 197 | |
| 198 | /** |
Eric Biggers | c440b509 | 2018-01-11 23:30:08 -0500 | [diff] [blame] | 199 | * fscrypt_fname_alloc_buffer - allocate a buffer for presented filenames |
Jaegeuk Kim | 0b81d07 | 2015-05-15 16:26:10 -0700 | [diff] [blame] | 200 | * |
Eric Biggers | c440b509 | 2018-01-11 23:30:08 -0500 | [diff] [blame] | 201 | * Allocate a buffer that is large enough to hold any decrypted or encoded |
| 202 | * filename (null-terminated), for the given maximum encrypted filename length. |
| 203 | * |
| 204 | * Return: 0 on success, -errno on failure |
Jaegeuk Kim | 0b81d07 | 2015-05-15 16:26:10 -0700 | [diff] [blame] | 205 | */ |
Jaegeuk Kim | e6b120d | 2017-07-10 12:55:09 -0700 | [diff] [blame] | 206 | int fscrypt_fname_alloc_buffer(const struct inode *inode, |
Eric Biggers | c440b509 | 2018-01-11 23:30:08 -0500 | [diff] [blame] | 207 | u32 max_encrypted_len, |
| 208 | struct fscrypt_str *crypto_str) |
Jaegeuk Kim | 0b81d07 | 2015-05-15 16:26:10 -0700 | [diff] [blame] | 209 | { |
Jaegeuk Kim | e6b120d | 2017-07-10 12:55:09 -0700 | [diff] [blame] | 210 | const u32 max_encoded_len = |
| 211 | max_t(u32, BASE64_CHARS(FSCRYPT_FNAME_MAX_UNDIGESTED_SIZE), |
| 212 | 1 + BASE64_CHARS(sizeof(struct fscrypt_digested_name))); |
Eric Biggers | c440b509 | 2018-01-11 23:30:08 -0500 | [diff] [blame] | 213 | u32 max_presented_len; |
Jaegeuk Kim | 0b81d07 | 2015-05-15 16:26:10 -0700 | [diff] [blame] | 214 | |
Eric Biggers | c440b509 | 2018-01-11 23:30:08 -0500 | [diff] [blame] | 215 | max_presented_len = max(max_encoded_len, max_encrypted_len); |
Jaegeuk Kim | e6b120d | 2017-07-10 12:55:09 -0700 | [diff] [blame] | 216 | |
Eric Biggers | c440b509 | 2018-01-11 23:30:08 -0500 | [diff] [blame] | 217 | crypto_str->name = kmalloc(max_presented_len + 1, GFP_NOFS); |
| 218 | if (!crypto_str->name) |
Jaegeuk Kim | 0b81d07 | 2015-05-15 16:26:10 -0700 | [diff] [blame] | 219 | return -ENOMEM; |
Eric Biggers | c440b509 | 2018-01-11 23:30:08 -0500 | [diff] [blame] | 220 | crypto_str->len = max_presented_len; |
Jaegeuk Kim | 0b81d07 | 2015-05-15 16:26:10 -0700 | [diff] [blame] | 221 | return 0; |
| 222 | } |
| 223 | EXPORT_SYMBOL(fscrypt_fname_alloc_buffer); |
| 224 | |
| 225 | /** |
Eric Biggers | c440b509 | 2018-01-11 23:30:08 -0500 | [diff] [blame] | 226 | * fscrypt_fname_free_buffer - free the buffer for presented filenames |
Jaegeuk Kim | 0b81d07 | 2015-05-15 16:26:10 -0700 | [diff] [blame] | 227 | * |
Eric Biggers | c440b509 | 2018-01-11 23:30:08 -0500 | [diff] [blame] | 228 | * Free the buffer allocated by fscrypt_fname_alloc_buffer(). |
Jaegeuk Kim | 0b81d07 | 2015-05-15 16:26:10 -0700 | [diff] [blame] | 229 | */ |
| 230 | void fscrypt_fname_free_buffer(struct fscrypt_str *crypto_str) |
| 231 | { |
| 232 | if (!crypto_str) |
| 233 | return; |
| 234 | kfree(crypto_str->name); |
| 235 | crypto_str->name = NULL; |
| 236 | } |
| 237 | EXPORT_SYMBOL(fscrypt_fname_free_buffer); |
| 238 | |
| 239 | /** |
| 240 | * fscrypt_fname_disk_to_usr() - converts a filename from disk space to user |
| 241 | * space |
Eric Biggers | ef1eb3a | 2016-09-15 17:25:55 -0400 | [diff] [blame] | 242 | * |
| 243 | * The caller must have allocated sufficient memory for the @oname string. |
| 244 | * |
Jaegeuk Kim | e6b120d | 2017-07-10 12:55:09 -0700 | [diff] [blame] | 245 | * If the key is available, we'll decrypt the disk name; otherwise, we'll encode |
| 246 | * it for presentation. Short names are directly base64-encoded, while long |
| 247 | * names are encoded in fscrypt_digested_name format. |
| 248 | * |
Eric Biggers | ef1eb3a | 2016-09-15 17:25:55 -0400 | [diff] [blame] | 249 | * Return: 0 on success, -errno on failure |
Jaegeuk Kim | 0b81d07 | 2015-05-15 16:26:10 -0700 | [diff] [blame] | 250 | */ |
| 251 | int fscrypt_fname_disk_to_usr(struct inode *inode, |
| 252 | u32 hash, u32 minor_hash, |
| 253 | const struct fscrypt_str *iname, |
| 254 | struct fscrypt_str *oname) |
| 255 | { |
| 256 | const struct qstr qname = FSTR_TO_QSTR(iname); |
Jaegeuk Kim | e6b120d | 2017-07-10 12:55:09 -0700 | [diff] [blame] | 257 | struct fscrypt_digested_name digested_name; |
Jaegeuk Kim | 0b81d07 | 2015-05-15 16:26:10 -0700 | [diff] [blame] | 258 | |
| 259 | if (fscrypt_is_dot_dotdot(&qname)) { |
| 260 | oname->name[0] = '.'; |
| 261 | oname->name[iname->len - 1] = '.'; |
| 262 | oname->len = iname->len; |
Eric Biggers | ef1eb3a | 2016-09-15 17:25:55 -0400 | [diff] [blame] | 263 | return 0; |
Jaegeuk Kim | 0b81d07 | 2015-05-15 16:26:10 -0700 | [diff] [blame] | 264 | } |
| 265 | |
| 266 | if (iname->len < FS_CRYPTO_BLOCK_SIZE) |
| 267 | return -EUCLEAN; |
| 268 | |
| 269 | if (inode->i_crypt_info) |
| 270 | return fname_decrypt(inode, iname, oname); |
| 271 | |
Jaegeuk Kim | e6b120d | 2017-07-10 12:55:09 -0700 | [diff] [blame] | 272 | if (iname->len <= FSCRYPT_FNAME_MAX_UNDIGESTED_SIZE) { |
Eric Biggers | ef1eb3a | 2016-09-15 17:25:55 -0400 | [diff] [blame] | 273 | oname->len = digest_encode(iname->name, iname->len, |
| 274 | oname->name); |
| 275 | return 0; |
Jaegeuk Kim | 0b81d07 | 2015-05-15 16:26:10 -0700 | [diff] [blame] | 276 | } |
| 277 | if (hash) { |
Jaegeuk Kim | e6b120d | 2017-07-10 12:55:09 -0700 | [diff] [blame] | 278 | digested_name.hash = hash; |
| 279 | digested_name.minor_hash = minor_hash; |
Jaegeuk Kim | 0b81d07 | 2015-05-15 16:26:10 -0700 | [diff] [blame] | 280 | } else { |
Jaegeuk Kim | e6b120d | 2017-07-10 12:55:09 -0700 | [diff] [blame] | 281 | digested_name.hash = 0; |
| 282 | digested_name.minor_hash = 0; |
Jaegeuk Kim | 0b81d07 | 2015-05-15 16:26:10 -0700 | [diff] [blame] | 283 | } |
Jaegeuk Kim | e6b120d | 2017-07-10 12:55:09 -0700 | [diff] [blame] | 284 | memcpy(digested_name.digest, |
| 285 | FSCRYPT_FNAME_DIGEST(iname->name, iname->len), |
| 286 | FSCRYPT_FNAME_DIGEST_SIZE); |
Jaegeuk Kim | 0b81d07 | 2015-05-15 16:26:10 -0700 | [diff] [blame] | 287 | oname->name[0] = '_'; |
Jaegeuk Kim | e6b120d | 2017-07-10 12:55:09 -0700 | [diff] [blame] | 288 | oname->len = 1 + digest_encode((const char *)&digested_name, |
| 289 | sizeof(digested_name), oname->name + 1); |
Eric Biggers | ef1eb3a | 2016-09-15 17:25:55 -0400 | [diff] [blame] | 290 | return 0; |
Jaegeuk Kim | 0b81d07 | 2015-05-15 16:26:10 -0700 | [diff] [blame] | 291 | } |
| 292 | EXPORT_SYMBOL(fscrypt_fname_disk_to_usr); |
| 293 | |
| 294 | /** |
Jaegeuk Kim | e6b120d | 2017-07-10 12:55:09 -0700 | [diff] [blame] | 295 | * fscrypt_setup_filename() - prepare to search a possibly encrypted directory |
| 296 | * @dir: the directory that will be searched |
| 297 | * @iname: the user-provided filename being searched for |
| 298 | * @lookup: 1 if we're allowed to proceed without the key because it's |
| 299 | * ->lookup() or we're finding the dir_entry for deletion; 0 if we cannot |
| 300 | * proceed without the key because we're going to create the dir_entry. |
| 301 | * @fname: the filename information to be filled in |
| 302 | * |
| 303 | * Given a user-provided filename @iname, this function sets @fname->disk_name |
| 304 | * to the name that would be stored in the on-disk directory entry, if possible. |
| 305 | * If the directory is unencrypted this is simply @iname. Else, if we have the |
| 306 | * directory's encryption key, then @iname is the plaintext, so we encrypt it to |
| 307 | * get the disk_name. |
| 308 | * |
| 309 | * Else, for keyless @lookup operations, @iname is the presented ciphertext, so |
| 310 | * we decode it to get either the ciphertext disk_name (for short names) or the |
| 311 | * fscrypt_digested_name (for long names). Non-@lookup operations will be |
| 312 | * impossible in this case, so we fail them with ENOKEY. |
| 313 | * |
| 314 | * If successful, fscrypt_free_filename() must be called later to clean up. |
| 315 | * |
| 316 | * Return: 0 on success, -errno on failure |
| 317 | */ |
Jaegeuk Kim | 0b81d07 | 2015-05-15 16:26:10 -0700 | [diff] [blame] | 318 | int fscrypt_setup_filename(struct inode *dir, const struct qstr *iname, |
| 319 | int lookup, struct fscrypt_name *fname) |
| 320 | { |
Jaegeuk Kim | e6b120d | 2017-07-10 12:55:09 -0700 | [diff] [blame] | 321 | int ret; |
| 322 | int digested; |
Jaegeuk Kim | 0b81d07 | 2015-05-15 16:26:10 -0700 | [diff] [blame] | 323 | |
| 324 | memset(fname, 0, sizeof(struct fscrypt_name)); |
| 325 | fname->usr_fname = iname; |
| 326 | |
Eric Biggers | d750ec7 | 2017-10-09 12:15:36 -0700 | [diff] [blame] | 327 | if (!IS_ENCRYPTED(dir) || fscrypt_is_dot_dotdot(iname)) { |
Jaegeuk Kim | 0b81d07 | 2015-05-15 16:26:10 -0700 | [diff] [blame] | 328 | fname->disk_name.name = (unsigned char *)iname->name; |
| 329 | fname->disk_name.len = iname->len; |
| 330 | return 0; |
| 331 | } |
Eric Biggers | 2984e52 | 2017-02-21 15:07:11 -0800 | [diff] [blame] | 332 | ret = fscrypt_get_encryption_info(dir); |
Eric Biggers | d367939 | 2018-04-30 15:51:41 -0700 | [diff] [blame] | 333 | if (ret) |
Jaegeuk Kim | 0b81d07 | 2015-05-15 16:26:10 -0700 | [diff] [blame] | 334 | return ret; |
| 335 | |
| 336 | if (dir->i_crypt_info) { |
Eric Biggers | 549b206 | 2018-01-11 23:30:08 -0500 | [diff] [blame] | 337 | if (!fscrypt_fname_encrypted_size(dir, iname->len, |
Eric Biggers | 4ddc3a8 | 2018-04-30 15:51:44 -0700 | [diff] [blame] | 338 | dir->i_sb->s_cop->max_namelen, |
Eric Biggers | 549b206 | 2018-01-11 23:30:08 -0500 | [diff] [blame] | 339 | &fname->crypto_buf.len)) |
Eric Biggers | b0edc2f | 2018-01-11 23:30:08 -0500 | [diff] [blame] | 340 | return -ENAMETOOLONG; |
Eric Biggers | b0edc2f | 2018-01-11 23:30:08 -0500 | [diff] [blame] | 341 | fname->crypto_buf.name = kmalloc(fname->crypto_buf.len, |
| 342 | GFP_NOFS); |
| 343 | if (!fname->crypto_buf.name) |
| 344 | return -ENOMEM; |
| 345 | |
| 346 | ret = fname_encrypt(dir, iname, fname->crypto_buf.name, |
| 347 | fname->crypto_buf.len); |
Eric Biggers | ef1eb3a | 2016-09-15 17:25:55 -0400 | [diff] [blame] | 348 | if (ret) |
Jaegeuk Kim | 0b81d07 | 2015-05-15 16:26:10 -0700 | [diff] [blame] | 349 | goto errout; |
| 350 | fname->disk_name.name = fname->crypto_buf.name; |
| 351 | fname->disk_name.len = fname->crypto_buf.len; |
| 352 | return 0; |
| 353 | } |
| 354 | if (!lookup) |
Jaegeuk Kim | e6b120d | 2017-07-10 12:55:09 -0700 | [diff] [blame] | 355 | return -ENOKEY; |
Jaegeuk Kim | 0b81d07 | 2015-05-15 16:26:10 -0700 | [diff] [blame] | 356 | |
| 357 | /* |
| 358 | * We don't have the key and we are doing a lookup; decode the |
| 359 | * user-supplied name |
| 360 | */ |
Jaegeuk Kim | e6b120d | 2017-07-10 12:55:09 -0700 | [diff] [blame] | 361 | if (iname->name[0] == '_') { |
| 362 | if (iname->len != |
| 363 | 1 + BASE64_CHARS(sizeof(struct fscrypt_digested_name))) |
| 364 | return -ENOENT; |
| 365 | digested = 1; |
| 366 | } else { |
| 367 | if (iname->len > |
| 368 | BASE64_CHARS(FSCRYPT_FNAME_MAX_UNDIGESTED_SIZE)) |
| 369 | return -ENOENT; |
| 370 | digested = 0; |
| 371 | } |
Jaegeuk Kim | 0b81d07 | 2015-05-15 16:26:10 -0700 | [diff] [blame] | 372 | |
Jaegeuk Kim | e6b120d | 2017-07-10 12:55:09 -0700 | [diff] [blame] | 373 | fname->crypto_buf.name = |
| 374 | kmalloc(max_t(size_t, FSCRYPT_FNAME_MAX_UNDIGESTED_SIZE, |
| 375 | sizeof(struct fscrypt_digested_name)), |
| 376 | GFP_KERNEL); |
Jaegeuk Kim | 0b81d07 | 2015-05-15 16:26:10 -0700 | [diff] [blame] | 377 | if (fname->crypto_buf.name == NULL) |
| 378 | return -ENOMEM; |
| 379 | |
Jaegeuk Kim | e6b120d | 2017-07-10 12:55:09 -0700 | [diff] [blame] | 380 | ret = digest_decode(iname->name + digested, iname->len - digested, |
Jaegeuk Kim | 0b81d07 | 2015-05-15 16:26:10 -0700 | [diff] [blame] | 381 | fname->crypto_buf.name); |
| 382 | if (ret < 0) { |
| 383 | ret = -ENOENT; |
| 384 | goto errout; |
| 385 | } |
| 386 | fname->crypto_buf.len = ret; |
Jaegeuk Kim | e6b120d | 2017-07-10 12:55:09 -0700 | [diff] [blame] | 387 | if (digested) { |
| 388 | const struct fscrypt_digested_name *n = |
| 389 | (const void *)fname->crypto_buf.name; |
| 390 | fname->hash = n->hash; |
| 391 | fname->minor_hash = n->minor_hash; |
Jaegeuk Kim | 0b81d07 | 2015-05-15 16:26:10 -0700 | [diff] [blame] | 392 | } else { |
| 393 | fname->disk_name.name = fname->crypto_buf.name; |
| 394 | fname->disk_name.len = fname->crypto_buf.len; |
| 395 | } |
| 396 | return 0; |
| 397 | |
| 398 | errout: |
Eric Biggers | b0edc2f | 2018-01-11 23:30:08 -0500 | [diff] [blame] | 399 | kfree(fname->crypto_buf.name); |
Jaegeuk Kim | 0b81d07 | 2015-05-15 16:26:10 -0700 | [diff] [blame] | 400 | return ret; |
| 401 | } |
| 402 | EXPORT_SYMBOL(fscrypt_setup_filename); |