History

fscrypt: fix race where ->lookup() marks plaintext dentry as ciphertext

->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.

Eric Biggers committed 7 years ago

Commit b01531d1 parent d456a337 files · +139 −66Browse files

Showing the changes to include/linux (1 of 7 files). Show all

Unified · Split

include/linux/fscrypt.h+20−10
@@ -33,6 +33,7 @@ struct fscrypt_name { · 32 unmodified lines
3333 u32 hash;
3434 u32 minor_hash;
3535 struct fscrypt_str crypto_buf;
36+ bool is_ciphertext_name;
3637 };
3738
3839 #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
234235 struct inode *new_dir,
235236 struct dentry *new_dentry,
236237 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);
238240 extern int __fscrypt_prepare_symlink(struct inode *dir, unsigned int len,
239241 unsigned int max_len,
240242 struct fscrypt_str *disk_link);
@@ -347,7 +349,7 @@ static inline int fscrypt_setup_filename(struct inode *dir, · 106 unmodified lines
347349 if (IS_ENCRYPTED(dir))
348350 return -EOPNOTSUPP;
349351
350− memset(fname, 0, sizeof(struct fscrypt_name));
352+ memset(fname, 0, sizeof(*fname));
351353 fname->usr_fname = iname;
352354 fname->disk_name.name = (unsigned char *)iname->name;
353355 fname->disk_name.len = iname->len;
@@ -434,7 +436,8 @@ static inline int __fscrypt_prepare_rename(struct inode *old_dir, · 80 unmodified lines
434436 }
435437
436438 static inline int __fscrypt_prepare_lookup(struct inode *dir,
437− struct dentry *dentry)
439+ struct dentry *dentry,
440+ struct fscrypt_name *fname)
438441 {
439442 return -EOPNOTSUPP;
440443 }
@@ -555,25 +558,32 @@ static inline int fscrypt_prepare_rename(struct inode *old_dir, · 114 unmodified lines
555558 * fscrypt_prepare_lookup - prepare to lookup a name in a possibly-encrypted directory
556559 * @dir: directory being searched
557560 * @dentry: filename being looked up
558− * @flags: lookup flags
561+ * @fname: (output) the name to use to search the on-disk directory
559562 *
560− * Prepare for ->lookup() in a directory which may be encrypted. Lookups can be
561− * done with or without the directory's encryption key; without the key,
563+ * Prepare for ->lookup() in a directory which may be encrypted by determining
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,
562566 * filenames are presented in encrypted form. Therefore, we'll try to set up
563567 * the directory's encryption key, but even without it the lookup can continue.
564568 *
565569 * This also installs a custom ->d_revalidate() method which will invalidate the
566570 * dentry if it was created without the key and the key is later added.
567571 *
568− * Return: 0 on success, -errno if a problem occurred while setting up the
569− * encryption key
572+ * Return: 0 on success; -ENOENT if key is unavailable but the filename isn't a
573+ * correctly formed encoded ciphertext name, so a negative dentry should be
574+ * created; or another -errno code.
570575 */
571576 static inline int fscrypt_prepare_lookup(struct inode *dir,
572577 struct dentry *dentry,
573− unsigned int flags)
578+ struct fscrypt_name *fname)
574579 {
575580 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;
577587 return 0;
578588 }
579589

Keyboard shortcuts

General

Show keyboard shortcuts
?
Focus the current search
/

Issues and pull requests

Open filters
f

Issues, pull requests and Inbox

Select next item
j↓
Select previous item
k↑
Open selected item
Enter
Dismiss filters or clear selection
Esc

Repository code

Go to file
t