Skip to content

Commit 2da473e

Browse files
ebiggersgregkh
authored andcommitted
fscrypt: add fscrypt_is_nokey_name()
commit 159e1de upstream. It's possible to create a duplicate filename in an encrypted directory by creating a file concurrently with adding the encryption key. Specifically, sys_open(O_CREAT) (or sys_mkdir(), sys_mknod(), or sys_symlink()) can lookup the target filename while the directory's encryption key hasn't been added yet, resulting in a negative no-key dentry. The VFS then calls ->create() (or ->mkdir(), ->mknod(), or ->symlink()) because the dentry is negative. Normally, ->create() would return -ENOKEY due to the directory's key being unavailable. However, if the key was added between the dentry lookup and ->create(), then the filesystem will go ahead and try to create the file. If the target filename happens to already exist as a normal name (not a no-key name), a duplicate filename may be added to the directory. In order to fix this, we need to fix the filesystems to prevent ->create(), ->mkdir(), ->mknod(), and ->symlink() on no-key names. (->rename() and ->link() need it too, but those are already handled correctly by fscrypt_prepare_rename() and fscrypt_prepare_link().) In preparation for this, add a helper function fscrypt_is_nokey_name() that filesystems can use to do this check. Use this helper function for the existing checks that fs/crypto/ does for rename and link. Cc: stable@vger.kernel.org Link: https://lore.kernel.org/r/20201118075609.120337-2-ebiggers@kernel.org Signed-off-by: Eric Biggers <ebiggers@google.com> Signed-off-by: Greg Kroah-Hartman <gregkh@linuxfoundation.org>
1 parent 3b7c17a commit 2da473e

2 files changed

Lines changed: 37 additions & 2 deletions

File tree

‎fs/crypto/hooks.c‎

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,7 @@ int __fscrypt_prepare_link(struct inode *inode, struct inode *dir,
6161
return err;
6262

6363
/* ... in case we looked up no-key name before key was added */
64-
if (dentry->d_flags & DCACHE_NOKEY_NAME)
64+
if (fscrypt_is_nokey_name(dentry))
6565
return -ENOKEY;
6666

6767
if (!fscrypt_has_permitted_context(dir, inode))
@@ -86,7 +86,8 @@ int __fscrypt_prepare_rename(struct inode *old_dir, struct dentry *old_dentry,
8686
return err;
8787

8888
/* ... in case we looked up no-key name(s) before key was added */
89-
if ((old_dentry->d_flags | new_dentry->d_flags) & DCACHE_NOKEY_NAME)
89+
if (fscrypt_is_nokey_name(old_dentry) ||
90+
fscrypt_is_nokey_name(new_dentry))
9091
return -ENOKEY;
9192

9293
if (old_dir != new_dir) {

‎include/linux/fscrypt.h‎

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -111,6 +111,35 @@ static inline void fscrypt_handle_d_move(struct dentry *dentry)
111111
dentry->d_flags &= ~DCACHE_NOKEY_NAME;
112112
}
113113

114+
/**
115+
* fscrypt_is_nokey_name() - test whether a dentry is a no-key name
116+
* @dentry: the dentry to check
117+
*
118+
* This returns true if the dentry is a no-key dentry. A no-key dentry is a
119+
* dentry that was created in an encrypted directory that hasn't had its
120+
* encryption key added yet. Such dentries may be either positive or negative.
121+
*
122+
* When a filesystem is asked to create a new filename in an encrypted directory
123+
* and the new filename's dentry is a no-key dentry, it must fail the operation
124+
* with ENOKEY. This includes ->create(), ->mkdir(), ->mknod(), ->symlink(),
125+
* ->rename(), and ->link(). (However, ->rename() and ->link() are already
126+
* handled by fscrypt_prepare_rename() and fscrypt_prepare_link().)
127+
*
128+
* This is necessary because creating a filename requires the directory's
129+
* encryption key, but just checking for the key on the directory inode during
130+
* the final filesystem operation doesn't guarantee that the key was available
131+
* during the preceding dentry lookup. And the key must have already been
132+
* available during the dentry lookup in order for it to have been checked
133+
* whether the filename already exists in the directory and for the new file's
134+
* dentry not to be invalidated due to it incorrectly having the no-key flag.
135+
*
136+
* Return: %true if the dentry is a no-key name
137+
*/
138+
static inline bool fscrypt_is_nokey_name(const struct dentry *dentry)
139+
{
140+
return dentry->d_flags & DCACHE_NOKEY_NAME;
141+
}
142+
114143
/* crypto.c */
115144
void fscrypt_enqueue_decrypt_work(struct work_struct *);
116145

@@ -244,6 +273,11 @@ static inline void fscrypt_handle_d_move(struct dentry *dentry)
244273
{
245274
}
246275

276+
static inline bool fscrypt_is_nokey_name(const struct dentry *dentry)
277+
{
278+
return false;
279+
}
280+
247281
/* crypto.c */
248282
static inline void fscrypt_enqueue_decrypt_work(struct work_struct *work)
249283
{

0 commit comments

Comments
 (0)