@@ -49,14 +49,19 @@ int fscrypt_file_open(struct inode *inode, struct file *filp)
4949}
5050EXPORT_SYMBOL_GPL (fscrypt_file_open );
5151
52- int __fscrypt_prepare_link (struct inode * inode , struct inode * dir )
52+ int __fscrypt_prepare_link (struct inode * inode , struct inode * dir ,
53+ struct dentry * dentry )
5354{
5455 int err ;
5556
5657 err = fscrypt_require_key (dir );
5758 if (err )
5859 return err ;
5960
61+ /* ... in case we looked up ciphertext name before key was added */
62+ if (dentry -> d_flags & DCACHE_ENCRYPTED_NAME )
63+ return - ENOKEY ;
64+
6065 if (!fscrypt_has_permitted_context (dir , inode ))
6166 return - EXDEV ;
6267
@@ -78,6 +83,11 @@ int __fscrypt_prepare_rename(struct inode *old_dir, struct dentry *old_dentry,
7883 if (err )
7984 return err ;
8085
86+ /* ... in case we looked up ciphertext name(s) before key was added */
87+ if ((old_dentry -> d_flags | new_dentry -> d_flags ) &
88+ DCACHE_ENCRYPTED_NAME )
89+ return - ENOKEY ;
90+
8191 if (old_dir != new_dir ) {
8292 if (IS_ENCRYPTED (new_dir ) &&
8393 !fscrypt_has_permitted_context (new_dir ,
@@ -94,21 +104,21 @@ int __fscrypt_prepare_rename(struct inode *old_dir, struct dentry *old_dentry,
94104}
95105EXPORT_SYMBOL_GPL (__fscrypt_prepare_rename );
96106
97- int __fscrypt_prepare_lookup (struct inode * dir , struct dentry * dentry )
107+ int __fscrypt_prepare_lookup (struct inode * dir , struct dentry * dentry ,
108+ struct fscrypt_name * fname )
98109{
99- int err = fscrypt_get_encryption_info (dir );
110+ int err = fscrypt_setup_filename (dir , & dentry -> d_name , 1 , fname );
100111
101- if (err )
112+ if (err && err != - ENOENT )
102113 return err ;
103114
104- if (fscrypt_has_encryption_key ( dir ) ) {
115+ if (fname -> is_ciphertext_name ) {
105116 spin_lock (& dentry -> d_lock );
106- dentry -> d_flags |= DCACHE_ENCRYPTED_WITH_KEY ;
117+ dentry -> d_flags |= DCACHE_ENCRYPTED_NAME ;
107118 spin_unlock (& dentry -> d_lock );
119+ d_set_d_op (dentry , & fscrypt_d_ops );
108120 }
109-
110- d_set_d_op (dentry , & fscrypt_d_ops );
111- return 0 ;
121+ return err ;
112122}
113123EXPORT_SYMBOL_GPL (__fscrypt_prepare_lookup );
114124
@@ -179,21 +189,30 @@ int __fscrypt_encrypt_symlink(struct inode *inode, const char *target,
179189 sd -> len = cpu_to_le16 (ciphertext_len );
180190
181191 err = fname_encrypt (inode , & iname , sd -> encrypted_path , ciphertext_len );
182- if (err ) {
183- if (!disk_link -> name )
184- kfree (sd );
185- return err ;
186- }
192+ if (err )
193+ goto err_free_sd ;
194+
187195 /*
188196 * Null-terminating the ciphertext doesn't make sense, but we still
189197 * count the null terminator in the length, so we might as well
190198 * initialize it just in case the filesystem writes it out.
191199 */
192200 sd -> encrypted_path [ciphertext_len ] = '\0' ;
193201
202+ /* Cache the plaintext symlink target for later use by get_link() */
203+ err = - ENOMEM ;
204+ inode -> i_link = kmemdup (target , len + 1 , GFP_NOFS );
205+ if (!inode -> i_link )
206+ goto err_free_sd ;
207+
194208 if (!disk_link -> name )
195209 disk_link -> name = (unsigned char * )sd ;
196210 return 0 ;
211+
212+ err_free_sd :
213+ if (!disk_link -> name )
214+ kfree (sd );
215+ return err ;
197216}
198217EXPORT_SYMBOL_GPL (__fscrypt_encrypt_symlink );
199218
@@ -202,7 +221,7 @@ EXPORT_SYMBOL_GPL(__fscrypt_encrypt_symlink);
202221 * @inode: the symlink inode
203222 * @caddr: the on-disk contents of the symlink
204223 * @max_size: size of @caddr buffer
205- * @done: if successful, will be set up to free the returned target
224+ * @done: if successful, will be set up to free the returned target if needed
206225 *
207226 * If the symlink's encryption key is available, we decrypt its target.
208227 * Otherwise, we encode its target for presentation.
@@ -217,19 +236,26 @@ const char *fscrypt_get_symlink(struct inode *inode, const void *caddr,
217236{
218237 const struct fscrypt_symlink_data * sd ;
219238 struct fscrypt_str cstr , pstr ;
239+ bool has_key ;
220240 int err ;
221241
222242 /* This is for encrypted symlinks only */
223243 if (WARN_ON (!IS_ENCRYPTED (inode )))
224244 return ERR_PTR (- EINVAL );
225245
246+ /* If the decrypted target is already cached, just return it. */
247+ pstr .name = READ_ONCE (inode -> i_link );
248+ if (pstr .name )
249+ return pstr .name ;
250+
226251 /*
227252 * Try to set up the symlink's encryption key, but we can continue
228253 * regardless of whether the key is available or not.
229254 */
230255 err = fscrypt_get_encryption_info (inode );
231256 if (err )
232257 return ERR_PTR (err );
258+ has_key = fscrypt_has_encryption_key (inode );
233259
234260 /*
235261 * For historical reasons, encrypted symlink targets are prefixed with
@@ -261,7 +287,17 @@ const char *fscrypt_get_symlink(struct inode *inode, const void *caddr,
261287 goto err_kfree ;
262288
263289 pstr .name [pstr .len ] = '\0' ;
264- set_delayed_call (done , kfree_link , pstr .name );
290+
291+ /*
292+ * Cache decrypted symlink targets in i_link for later use. Don't cache
293+ * symlink targets encoded without the key, since those become outdated
294+ * once the key is added. This pairs with the READ_ONCE() above and in
295+ * the VFS path lookup code.
296+ */
297+ if (!has_key ||
298+ cmpxchg_release (& inode -> i_link , NULL , pstr .name ) != NULL )
299+ set_delayed_call (done , kfree_link , pstr .name );
300+
265301 return pstr .name ;
266302
267303err_kfree :
0 commit comments