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