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