->lookup() in an encrypted directory begins as follows: 1. fscrypt_prepare_lookup(): a. Try to load the directory's encryption key. b. If the key is unavailable, mark the dentry as a ciphertext name via d_flags. 2. fscrypt_setup_filename(): a. Try to load the directory's encryption key. b. If the key is available, encrypt the name (treated as a plaintext name) to get the on-disk name. Otherwise decode the name (treated as a ciphertext name) to get the on-disk name. But if the key is concurrently added, it may be found at (2a) but not at (1a). In this case, the dentry will be wrongly marked as a ciphertext name even though it was actually treated as plaintext. This will cause the dentry to be wrongly invalidated on the next lookup, potentially causing problems. For example, if the racy ->lookup() was part of sys_mount(), then the new mount will be detached when anything tries to access it. This is despite the mountpoint having a plaintext path, which should remain valid now that the key was added. Of course, this is only possible if there's a userspace race. Still, the additional kernel-side race is confusing and unexpected. Close the kernel-side race by changing fscrypt_prepare_lookup() to also set the on-disk filename (step 2b), consistent with the d_flags update.
Showing the changes to include/linux (1 of 7 files). Show all
Unified · Split
| @@ -33,6 +33,7 @@ struct fscrypt_name { · 32 unmodified lines | |||
| 33 | u32 hash; | 33 | u32 hash; |
| 34 | u32 minor_hash; | 34 | u32 minor_hash; |
| 35 | struct fscrypt_str crypto_buf; | 35 | struct fscrypt_str crypto_buf; |
| 36 | bool is_ciphertext_name; | ||
| 36 | }; | 37 | }; |
| 37 | 38 | ||
| 38 | #define FSTR_INIT(n, l) { .name = n, .len = l } | 39 | #define FSTR_INIT(n, l) { .name = n, .len = l } |
| @@ -234,7 +235,8 @@ extern int __fscrypt_prepare_rename(struct inode *old_dir, · 195 unmodified lines | |||
| 234 | struct inode *new_dir, | 235 | struct inode *new_dir, |
| 235 | struct dentry *new_dentry, | 236 | struct dentry *new_dentry, |
| 236 | unsigned int flags); | 237 | unsigned int flags); |
| 237 | extern int __fscrypt_prepare_lookup(struct inode *dir, struct dentry *dentry); | 238 | extern int __fscrypt_prepare_lookup(struct inode *dir, struct dentry *dentry, |
| 239 | struct fscrypt_name *fname); | ||
| 238 | extern int __fscrypt_prepare_symlink(struct inode *dir, unsigned int len, | 240 | extern int __fscrypt_prepare_symlink(struct inode *dir, unsigned int len, |
| 239 | unsigned int max_len, | 241 | unsigned int max_len, |
| 240 | struct fscrypt_str *disk_link); | 242 | struct fscrypt_str *disk_link); |
| @@ -347,7 +349,7 @@ static inline int fscrypt_setup_filename(struct inode *dir, · 106 unmodified lines | |||
| 347 | if (IS_ENCRYPTED(dir)) | 349 | if (IS_ENCRYPTED(dir)) |
| 348 | return -EOPNOTSUPP; | 350 | return -EOPNOTSUPP; |
| 349 | 351 | ||
| 350 | memset(fname, 0, sizeof(struct fscrypt_name)); | 352 | memset(fname, 0, sizeof(*fname)); |
| 351 | fname->usr_fname = iname; | 353 | fname->usr_fname = iname; |
| 352 | fname->disk_name.name = (unsigned char *)iname->name; | 354 | fname->disk_name.name = (unsigned char *)iname->name; |
| 353 | fname->disk_name.len = iname->len; | 355 | fname->disk_name.len = iname->len; |
| @@ -434,7 +436,8 @@ static inline int __fscrypt_prepare_rename(struct inode *old_dir, · 80 unmodified lines | |||
| 434 | } | 436 | } |
| 435 | 437 | ||
| 436 | static inline int __fscrypt_prepare_lookup(struct inode *dir, | 438 | static inline int __fscrypt_prepare_lookup(struct inode *dir, |
| 437 | struct dentry *dentry) | 439 | struct dentry *dentry, |
| 440 | struct fscrypt_name *fname) | ||
| 438 | { | 441 | { |
| 439 | return -EOPNOTSUPP; | 442 | return -EOPNOTSUPP; |
| 440 | } | 443 | } |
| @@ -555,25 +558,32 @@ static inline int fscrypt_prepare_rename(struct inode *old_dir, · 114 unmodified lines | |||
| 555 | * fscrypt_prepare_lookup - prepare to lookup a name in a possibly-encrypted directory | 558 | * fscrypt_prepare_lookup - prepare to lookup a name in a possibly-encrypted directory |
| 556 | * @dir: directory being searched | 559 | * @dir: directory being searched |
| 557 | * @dentry: filename being looked up | 560 | * @dentry: filename being looked up |
| 558 | * @flags: lookup flags | 561 | * @fname: (output) the name to use to search the on-disk directory |
| 559 | * | 562 | * |
| 560 | * Prepare for ->lookup() in a directory which may be encrypted. Lookups can be | 563 | * Prepare for ->lookup() in a directory which may be encrypted by determining |
| 561 | * done with or without the directory's encryption key; without the key, | 564 | * the name that will actually be used to search the directory on-disk. Lookups |
| 565 | * can be done with or without the directory's encryption key; without the key, | ||
| 562 | * filenames are presented in encrypted form. Therefore, we'll try to set up | 566 | * filenames are presented in encrypted form. Therefore, we'll try to set up |
| 563 | * the directory's encryption key, but even without it the lookup can continue. | 567 | * the directory's encryption key, but even without it the lookup can continue. |
| 564 | * | 568 | * |
| 565 | * This also installs a custom ->d_revalidate() method which will invalidate the | 569 | * This also installs a custom ->d_revalidate() method which will invalidate the |
| 566 | * dentry if it was created without the key and the key is later added. | 570 | * dentry if it was created without the key and the key is later added. |
| 567 | * | 571 | * |
| 568 | * Return: 0 on success, -errno if a problem occurred while setting up the | 572 | * Return: 0 on success; -ENOENT if key is unavailable but the filename isn't a |
| 569 | * encryption key | 573 | * correctly formed encoded ciphertext name, so a negative dentry should be |
| 574 | * created; or another -errno code. | ||
| 570 | */ | 575 | */ |
| 571 | static inline int fscrypt_prepare_lookup(struct inode *dir, | 576 | static inline int fscrypt_prepare_lookup(struct inode *dir, |
| 572 | struct dentry *dentry, | 577 | struct dentry *dentry, |
| 573 | unsigned int flags) | 578 | struct fscrypt_name *fname) |
| 574 | { | 579 | { |
| 575 | if (IS_ENCRYPTED(dir)) | 580 | if (IS_ENCRYPTED(dir)) |
| 576 | return __fscrypt_prepare_lookup(dir, dentry); | 581 | return __fscrypt_prepare_lookup(dir, dentry, fname); |
| 582 | |||
| 583 | memset(fname, 0, sizeof(*fname)); | ||
| 584 | fname->usr_fname = &dentry->d_name; | ||
| 585 | fname->disk_name.name = (unsigned char *)dentry->d_name.name; | ||
| 586 | fname->disk_name.len = dentry->d_name.len; | ||
| 577 | return 0; | 587 | return 0; |
| 578 | } | 588 | } |
| 579 | 589 | ||