1 // SPDX-License-Identifier: GPL-2.0 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 14 #include <linux/namei.h> 15 #include <linux/scatterlist.h> 16 #include <crypto/hash.h> 17 #include <crypto/sha.h> 18 #include <crypto/skcipher.h> 19 #include "fscrypt_private.h" 20 21 /** 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 29 * way. We use base64. But that can cause names to exceed NAME_MAX (255 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 * 41 * To meet all these requirements, we base64-encode the following 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]; 55 }; /* 189 bytes => 252 bytes base64-encoded, which is <= NAME_MAX (255) */ 56 57 /* 58 * Decoded size of max-size nokey name, i.e. a name that was abbreviated using 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 64 static struct crypto_shash *sha256_hash_tfm; 65 66 static int fscrypt_do_sha256(const u8 *data, unsigned int data_len, u8 *result) 67 { 68 struct crypto_shash *tfm = READ_ONCE(sha256_hash_tfm); 69 70 if (unlikely(!tfm)) { 71 struct crypto_shash *prev_tfm; 72 73 tfm = crypto_alloc_shash("sha256", 0, 0); 74 if (IS_ERR(tfm)) { 75 fscrypt_err(NULL, 76 "Error allocating SHA-256 transform: %ld", 77 PTR_ERR(tfm)); 78 return PTR_ERR(tfm); 79 } 80 prev_tfm = cmpxchg(&sha256_hash_tfm, NULL, tfm); 81 if (prev_tfm) { 82 crypto_free_shash(tfm); 83 tfm = prev_tfm; 84 } 85 } 86 { 87 SHASH_DESC_ON_STACK(desc, tfm); 88 89 desc->tfm = tfm; 90 91 return crypto_shash_digest(desc, data, data_len, result); 92 } 93 } 94 95 static inline bool fscrypt_is_dot_dotdot(const struct qstr *str) 96 { 97 if (str->len == 1 && str->name[0] == '.') 98 return true; 99 100 if (str->len == 2 && str->name[0] == '.' && str->name[1] == '.') 101 return true; 102 103 return false; 104 } 105 106 /** 107 * fscrypt_fname_encrypt() - encrypt a filename 108 * 109 * The output buffer must be at least as large as the input buffer. 110 * Any extra space is filled with NUL padding before encryption. 111 * 112 * Return: 0 on success, -errno on failure 113 */ 114 int fscrypt_fname_encrypt(const struct inode *inode, const struct qstr *iname, 115 u8 *out, unsigned int olen) 116 { 117 struct skcipher_request *req = NULL; 118 DECLARE_CRYPTO_WAIT(wait); 119 const struct fscrypt_info *ci = inode->i_crypt_info; 120 struct crypto_skcipher *tfm = ci->ci_ctfm; 121 union fscrypt_iv iv; 122 struct scatterlist sg; 123 int res; 124 125 /* 126 * Copy the filename to the output buffer for encrypting in-place and 127 * pad it with the needed number of NUL bytes. 128 */ 129 if (WARN_ON(olen < iname->len)) 130 return -ENOBUFS; 131 memcpy(out, iname->name, iname->len); 132 memset(out + iname->len, 0, olen - iname->len); 133 134 /* Initialize the IV */ 135 fscrypt_generate_iv(&iv, 0, ci); 136 137 /* Set up the encryption request */ 138 req = skcipher_request_alloc(tfm, GFP_NOFS); 139 if (!req) 140 return -ENOMEM; 141 skcipher_request_set_callback(req, 142 CRYPTO_TFM_REQ_MAY_BACKLOG | CRYPTO_TFM_REQ_MAY_SLEEP, 143 crypto_req_done, &wait); 144 sg_init_one(&sg, out, olen); 145 skcipher_request_set_crypt(req, &sg, &sg, olen, &iv); 146 147 /* Do the encryption */ 148 res = crypto_wait_req(crypto_skcipher_encrypt(req), &wait); 149 skcipher_request_free(req); 150 if (res < 0) { 151 fscrypt_err(inode, "Filename encryption failed: %d", res); 152 return res; 153 } 154 155 return 0; 156 } 157 158 /** 159 * fname_decrypt() - decrypt a filename 160 * 161 * The caller must have allocated sufficient memory for the @oname string. 162 * 163 * Return: 0 on success, -errno on failure 164 */ 165 static int fname_decrypt(const struct inode *inode, 166 const struct fscrypt_str *iname, 167 struct fscrypt_str *oname) 168 { 169 struct skcipher_request *req = NULL; 170 DECLARE_CRYPTO_WAIT(wait); 171 struct scatterlist src_sg, dst_sg; 172 const struct fscrypt_info *ci = inode->i_crypt_info; 173 struct crypto_skcipher *tfm = ci->ci_ctfm; 174 union fscrypt_iv iv; 175 int res; 176 177 /* Allocate request */ 178 req = skcipher_request_alloc(tfm, GFP_NOFS); 179 if (!req) 180 return -ENOMEM; 181 skcipher_request_set_callback(req, 182 CRYPTO_TFM_REQ_MAY_BACKLOG | CRYPTO_TFM_REQ_MAY_SLEEP, 183 crypto_req_done, &wait); 184 185 /* Initialize IV */ 186 fscrypt_generate_iv(&iv, 0, ci); 187 188 /* Create decryption request */ 189 sg_init_one(&src_sg, iname->name, iname->len); 190 sg_init_one(&dst_sg, oname->name, oname->len); 191 skcipher_request_set_crypt(req, &src_sg, &dst_sg, iname->len, &iv); 192 res = crypto_wait_req(crypto_skcipher_decrypt(req), &wait); 193 skcipher_request_free(req); 194 if (res < 0) { 195 fscrypt_err(inode, "Filename decryption failed: %d", res); 196 return res; 197 } 198 199 oname->len = strnlen(oname->name, iname->len); 200 return 0; 201 } 202 203 static const char lookup_table[65] = 204 "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+,"; 205 206 #define BASE64_CHARS(nbytes) DIV_ROUND_UP((nbytes) * 4, 3) 207 208 /** 209 * base64_encode() - 210 * 211 * Encodes the input string using characters from the set [A-Za-z0-9+,]. 212 * The encoded string is roughly 4/3 times the size of the input string. 213 * 214 * Return: length of the encoded string 215 */ 216 static int base64_encode(const u8 *src, int len, char *dst) 217 { 218 int i, bits = 0, ac = 0; 219 char *cp = dst; 220 221 for (i = 0; i < len; i++) { 222 ac += src[i] << bits; 223 bits += 8; 224 do { 225 *cp++ = lookup_table[ac & 0x3f]; 226 ac >>= 6; 227 bits -= 6; 228 } while (bits >= 6); 229 } 230 if (bits) 231 *cp++ = lookup_table[ac & 0x3f]; 232 return cp - dst; 233 } 234 235 static int base64_decode(const char *src, int len, u8 *dst) 236 { 237 int i, bits = 0, ac = 0; 238 const char *p; 239 u8 *cp = dst; 240 241 for (i = 0; i < len; i++) { 242 p = strchr(lookup_table, src[i]); 243 if (p == NULL || src[i] == 0) 244 return -2; 245 ac += (p - lookup_table) << bits; 246 bits += 6; 247 if (bits >= 8) { 248 *cp++ = ac & 0xff; 249 ac >>= 8; 250 bits -= 8; 251 } 252 } 253 if (ac) 254 return -1; 255 return cp - dst; 256 } 257 258 bool fscrypt_fname_encrypted_size(const struct inode *inode, u32 orig_len, 259 u32 max_len, u32 *encrypted_len_ret) 260 { 261 const struct fscrypt_info *ci = inode->i_crypt_info; 262 int padding = 4 << (fscrypt_policy_flags(&ci->ci_policy) & 263 FSCRYPT_POLICY_FLAGS_PAD_MASK); 264 u32 encrypted_len; 265 266 if (orig_len > max_len) 267 return false; 268 encrypted_len = max(orig_len, (u32)FS_CRYPTO_BLOCK_SIZE); 269 encrypted_len = round_up(encrypted_len, padding); 270 *encrypted_len_ret = min(encrypted_len, max_len); 271 return true; 272 } 273 274 /** 275 * fscrypt_fname_alloc_buffer - allocate a buffer for presented filenames 276 * 277 * Allocate a buffer that is large enough to hold any decrypted or encoded 278 * filename (null-terminated), for the given maximum encrypted filename length. 279 * 280 * Return: 0 on success, -errno on failure 281 */ 282 int fscrypt_fname_alloc_buffer(const struct inode *inode, 283 u32 max_encrypted_len, 284 struct fscrypt_str *crypto_str) 285 { 286 const u32 max_encoded_len = BASE64_CHARS(FSCRYPT_NOKEY_NAME_MAX); 287 u32 max_presented_len; 288 289 max_presented_len = max(max_encoded_len, max_encrypted_len); 290 291 crypto_str->name = kmalloc(max_presented_len + 1, GFP_NOFS); 292 if (!crypto_str->name) 293 return -ENOMEM; 294 crypto_str->len = max_presented_len; 295 return 0; 296 } 297 EXPORT_SYMBOL(fscrypt_fname_alloc_buffer); 298 299 /** 300 * fscrypt_fname_free_buffer - free the buffer for presented filenames 301 * 302 * Free the buffer allocated by fscrypt_fname_alloc_buffer(). 303 */ 304 void fscrypt_fname_free_buffer(struct fscrypt_str *crypto_str) 305 { 306 if (!crypto_str) 307 return; 308 kfree(crypto_str->name); 309 crypto_str->name = NULL; 310 } 311 EXPORT_SYMBOL(fscrypt_fname_free_buffer); 312 313 /** 314 * fscrypt_fname_disk_to_usr() - converts a filename from disk space to user 315 * space 316 * 317 * The caller must have allocated sufficient memory for the @oname string. 318 * 319 * If the key is available, we'll decrypt the disk name. Otherwise, we'll 320 * encode it for presentation in fscrypt_nokey_name format. 321 * See struct fscrypt_nokey_name for details. 322 * 323 * Return: 0 on success, -errno on failure 324 */ 325 int fscrypt_fname_disk_to_usr(const struct inode *inode, 326 u32 hash, u32 minor_hash, 327 const struct fscrypt_str *iname, 328 struct fscrypt_str *oname) 329 { 330 const struct qstr qname = FSTR_TO_QSTR(iname); 331 struct fscrypt_nokey_name nokey_name; 332 u32 size; /* size of the unencoded no-key name */ 333 int err; 334 335 if (fscrypt_is_dot_dotdot(&qname)) { 336 oname->name[0] = '.'; 337 oname->name[iname->len - 1] = '.'; 338 oname->len = iname->len; 339 return 0; 340 } 341 342 if (iname->len < FS_CRYPTO_BLOCK_SIZE) 343 return -EUCLEAN; 344 345 if (fscrypt_has_encryption_key(inode)) 346 return fname_decrypt(inode, iname, oname); 347 348 /* 349 * Sanity check that struct fscrypt_nokey_name doesn't have padding 350 * between fields and that its encoded size never exceeds NAME_MAX. 351 */ 352 BUILD_BUG_ON(offsetofend(struct fscrypt_nokey_name, dirhash) != 353 offsetof(struct fscrypt_nokey_name, bytes)); 354 BUILD_BUG_ON(offsetofend(struct fscrypt_nokey_name, bytes) != 355 offsetof(struct fscrypt_nokey_name, sha256)); 356 BUILD_BUG_ON(BASE64_CHARS(FSCRYPT_NOKEY_NAME_MAX) > NAME_MAX); 357 358 if (hash) { 359 nokey_name.dirhash[0] = hash; 360 nokey_name.dirhash[1] = minor_hash; 361 } else { 362 nokey_name.dirhash[0] = 0; 363 nokey_name.dirhash[1] = 0; 364 } 365 if (iname->len <= sizeof(nokey_name.bytes)) { 366 memcpy(nokey_name.bytes, iname->name, iname->len); 367 size = offsetof(struct fscrypt_nokey_name, bytes[iname->len]); 368 } else { 369 memcpy(nokey_name.bytes, iname->name, sizeof(nokey_name.bytes)); 370 /* Compute strong hash of remaining part of name. */ 371 err = fscrypt_do_sha256(&iname->name[sizeof(nokey_name.bytes)], 372 iname->len - sizeof(nokey_name.bytes), 373 nokey_name.sha256); 374 if (err) 375 return err; 376 size = FSCRYPT_NOKEY_NAME_MAX; 377 } 378 oname->len = base64_encode((const u8 *)&nokey_name, size, oname->name); 379 return 0; 380 } 381 EXPORT_SYMBOL(fscrypt_fname_disk_to_usr); 382 383 /** 384 * fscrypt_setup_filename() - prepare to search a possibly encrypted directory 385 * @dir: the directory that will be searched 386 * @iname: the user-provided filename being searched for 387 * @lookup: 1 if we're allowed to proceed without the key because it's 388 * ->lookup() or we're finding the dir_entry for deletion; 0 if we cannot 389 * proceed without the key because we're going to create the dir_entry. 390 * @fname: the filename information to be filled in 391 * 392 * Given a user-provided filename @iname, this function sets @fname->disk_name 393 * to the name that would be stored in the on-disk directory entry, if possible. 394 * If the directory is unencrypted this is simply @iname. Else, if we have the 395 * directory's encryption key, then @iname is the plaintext, so we encrypt it to 396 * get the disk_name. 397 * 398 * Else, for keyless @lookup operations, @iname is the presented ciphertext, so 399 * we decode it to get the fscrypt_nokey_name. Non-@lookup operations will be 400 * impossible in this case, so we fail them with ENOKEY. 401 * 402 * If successful, fscrypt_free_filename() must be called later to clean up. 403 * 404 * Return: 0 on success, -errno on failure 405 */ 406 int fscrypt_setup_filename(struct inode *dir, const struct qstr *iname, 407 int lookup, struct fscrypt_name *fname) 408 { 409 struct fscrypt_nokey_name *nokey_name; 410 int ret; 411 412 memset(fname, 0, sizeof(struct fscrypt_name)); 413 fname->usr_fname = iname; 414 415 if (!IS_ENCRYPTED(dir) || fscrypt_is_dot_dotdot(iname)) { 416 fname->disk_name.name = (unsigned char *)iname->name; 417 fname->disk_name.len = iname->len; 418 return 0; 419 } 420 ret = fscrypt_get_encryption_info(dir); 421 if (ret) 422 return ret; 423 424 if (fscrypt_has_encryption_key(dir)) { 425 if (!fscrypt_fname_encrypted_size(dir, iname->len, 426 dir->i_sb->s_cop->max_namelen, 427 &fname->crypto_buf.len)) 428 return -ENAMETOOLONG; 429 fname->crypto_buf.name = kmalloc(fname->crypto_buf.len, 430 GFP_NOFS); 431 if (!fname->crypto_buf.name) 432 return -ENOMEM; 433 434 ret = fscrypt_fname_encrypt(dir, iname, fname->crypto_buf.name, 435 fname->crypto_buf.len); 436 if (ret) 437 goto errout; 438 fname->disk_name.name = fname->crypto_buf.name; 439 fname->disk_name.len = fname->crypto_buf.len; 440 return 0; 441 } 442 if (!lookup) 443 return -ENOKEY; 444 fname->is_ciphertext_name = true; 445 446 /* 447 * We don't have the key and we are doing a lookup; decode the 448 * user-supplied name 449 */ 450 451 if (iname->len > BASE64_CHARS(FSCRYPT_NOKEY_NAME_MAX)) 452 return -ENOENT; 453 454 fname->crypto_buf.name = kmalloc(FSCRYPT_NOKEY_NAME_MAX, GFP_KERNEL); 455 if (fname->crypto_buf.name == NULL) 456 return -ENOMEM; 457 458 ret = base64_decode(iname->name, iname->len, fname->crypto_buf.name); 459 if (ret < (int)offsetof(struct fscrypt_nokey_name, bytes[1]) || 460 (ret > offsetof(struct fscrypt_nokey_name, sha256) && 461 ret != FSCRYPT_NOKEY_NAME_MAX)) { 462 ret = -ENOENT; 463 goto errout; 464 } 465 fname->crypto_buf.len = ret; 466 467 nokey_name = (void *)fname->crypto_buf.name; 468 fname->hash = nokey_name->dirhash[0]; 469 fname->minor_hash = nokey_name->dirhash[1]; 470 if (ret != FSCRYPT_NOKEY_NAME_MAX) { 471 /* The full ciphertext filename is available. */ 472 fname->disk_name.name = nokey_name->bytes; 473 fname->disk_name.len = 474 ret - offsetof(struct fscrypt_nokey_name, bytes); 475 } 476 return 0; 477 478 errout: 479 kfree(fname->crypto_buf.name); 480 return ret; 481 } 482 EXPORT_SYMBOL(fscrypt_setup_filename); 483 484 /** 485 * fscrypt_match_name() - test whether the given name matches a directory entry 486 * @fname: the name being searched for 487 * @de_name: the name from the directory entry 488 * @de_name_len: the length of @de_name in bytes 489 * 490 * Normally @fname->disk_name will be set, and in that case we simply compare 491 * that to the name stored in the directory entry. The only exception is that 492 * if we don't have the key for an encrypted directory and the name we're 493 * looking for is very long, then we won't have the full disk_name and instead 494 * we'll need to match against a fscrypt_nokey_name that includes a strong hash. 495 * 496 * Return: %true if the name matches, otherwise %false. 497 */ 498 bool fscrypt_match_name(const struct fscrypt_name *fname, 499 const u8 *de_name, u32 de_name_len) 500 { 501 const struct fscrypt_nokey_name *nokey_name = 502 (const void *)fname->crypto_buf.name; 503 u8 sha256[SHA256_DIGEST_SIZE]; 504 505 if (likely(fname->disk_name.name)) { 506 if (de_name_len != fname->disk_name.len) 507 return false; 508 return !memcmp(de_name, fname->disk_name.name, de_name_len); 509 } 510 if (de_name_len <= sizeof(nokey_name->bytes)) 511 return false; 512 if (memcmp(de_name, nokey_name->bytes, sizeof(nokey_name->bytes))) 513 return false; 514 if (fscrypt_do_sha256(&de_name[sizeof(nokey_name->bytes)], 515 de_name_len - sizeof(nokey_name->bytes), sha256)) 516 return false; 517 return !memcmp(sha256, nokey_name->sha256, sizeof(sha256)); 518 } 519 EXPORT_SYMBOL_GPL(fscrypt_match_name); 520 521 /** 522 * fscrypt_fname_siphash() - calculate the SipHash of a filename 523 * @dir: the parent directory 524 * @name: the filename to calculate the SipHash of 525 * 526 * Given a plaintext filename @name and a directory @dir which uses SipHash as 527 * its dirhash method and has had its fscrypt key set up, this function 528 * calculates the SipHash of that name using the directory's secret dirhash key. 529 * 530 * Return: the SipHash of @name using the hash key of @dir 531 */ 532 u64 fscrypt_fname_siphash(const struct inode *dir, const struct qstr *name) 533 { 534 const struct fscrypt_info *ci = dir->i_crypt_info; 535 536 WARN_ON(!ci->ci_dirhash_key_initialized); 537 538 return siphash(name->name, name->len, &ci->ci_dirhash_key); 539 } 540 EXPORT_SYMBOL_GPL(fscrypt_fname_siphash); 541 542 /* 543 * Validate dentries in encrypted directories to make sure we aren't potentially 544 * caching stale dentries after a key has been added. 545 */ 546 static int fscrypt_d_revalidate(struct dentry *dentry, unsigned int flags) 547 { 548 struct dentry *dir; 549 int err; 550 int valid; 551 552 /* 553 * Plaintext names are always valid, since fscrypt doesn't support 554 * reverting to ciphertext names without evicting the directory's inode 555 * -- which implies eviction of the dentries in the directory. 556 */ 557 if (!(dentry->d_flags & DCACHE_ENCRYPTED_NAME)) 558 return 1; 559 560 /* 561 * Ciphertext name; valid if the directory's key is still unavailable. 562 * 563 * Although fscrypt forbids rename() on ciphertext names, we still must 564 * use dget_parent() here rather than use ->d_parent directly. That's 565 * because a corrupted fs image may contain directory hard links, which 566 * the VFS handles by moving the directory's dentry tree in the dcache 567 * each time ->lookup() finds the directory and it already has a dentry 568 * elsewhere. Thus ->d_parent can be changing, and we must safely grab 569 * a reference to some ->d_parent to prevent it from being freed. 570 */ 571 572 if (flags & LOOKUP_RCU) 573 return -ECHILD; 574 575 dir = dget_parent(dentry); 576 err = fscrypt_get_encryption_info(d_inode(dir)); 577 valid = !fscrypt_has_encryption_key(d_inode(dir)); 578 dput(dir); 579 580 if (err < 0) 581 return err; 582 583 return valid; 584 } 585 586 const struct dentry_operations fscrypt_d_ops = { 587 .d_revalidate = fscrypt_d_revalidate, 588 }; 589