1 // SPDX-License-Identifier: GPL-2.0-only
5 * Encryption hooks for higher-level filesystem operations.
8 #include <linux/ratelimit.h>
9 #include "fscrypt_private.h"
12 * fscrypt_file_open - prepare to open a possibly-encrypted regular file
13 * @inode: the inode being opened
14 * @filp: the struct file being set up
16 * Currently, an encrypted regular file can only be opened if its encryption key
17 * is available; access to the raw encrypted contents is not supported.
18 * Therefore, we first set up the inode's encryption key (if not already done)
19 * and return an error if it's unavailable.
21 * We also verify that if the parent directory (from the path via which the file
22 * is being opened) is encrypted, then the inode being opened uses the same
23 * encryption policy. This is needed as part of the enforcement that all files
24 * in an encrypted directory tree use the same encryption policy, as a
25 * protection against certain types of offline attacks. Note that this check is
26 * needed even when opening an *unencrypted* file, since it's forbidden to have
27 * an unencrypted file in an encrypted directory.
29 * Return: 0 on success, -ENOKEY if the key is missing, or another -errno code
31 int fscrypt_file_open(struct inode
*inode
, struct file
*filp
)
36 err
= fscrypt_require_key(inode
);
40 dir
= dget_parent(file_dentry(filp
));
41 if (IS_ENCRYPTED(d_inode(dir
)) &&
42 !fscrypt_has_permitted_context(d_inode(dir
), inode
)) {
43 fscrypt_warn(inode
->i_sb
,
44 "inconsistent encryption contexts: %lu/%lu",
45 d_inode(dir
)->i_ino
, inode
->i_ino
);
51 EXPORT_SYMBOL_GPL(fscrypt_file_open
);
53 int __fscrypt_prepare_link(struct inode
*inode
, struct inode
*dir
,
54 struct dentry
*dentry
)
58 err
= fscrypt_require_key(dir
);
62 /* ... in case we looked up ciphertext name before key was added */
63 if (dentry
->d_flags
& DCACHE_ENCRYPTED_NAME
)
66 if (!fscrypt_has_permitted_context(dir
, inode
))
71 EXPORT_SYMBOL_GPL(__fscrypt_prepare_link
);
73 int __fscrypt_prepare_rename(struct inode
*old_dir
, struct dentry
*old_dentry
,
74 struct inode
*new_dir
, struct dentry
*new_dentry
,
79 err
= fscrypt_require_key(old_dir
);
83 err
= fscrypt_require_key(new_dir
);
87 /* ... in case we looked up ciphertext name(s) before key was added */
88 if ((old_dentry
->d_flags
| new_dentry
->d_flags
) &
89 DCACHE_ENCRYPTED_NAME
)
92 if (old_dir
!= new_dir
) {
93 if (IS_ENCRYPTED(new_dir
) &&
94 !fscrypt_has_permitted_context(new_dir
,
98 if ((flags
& RENAME_EXCHANGE
) &&
99 IS_ENCRYPTED(old_dir
) &&
100 !fscrypt_has_permitted_context(old_dir
,
101 d_inode(new_dentry
)))
106 EXPORT_SYMBOL_GPL(__fscrypt_prepare_rename
);
108 int __fscrypt_prepare_lookup(struct inode
*dir
, struct dentry
*dentry
,
109 struct fscrypt_name
*fname
)
111 int err
= fscrypt_setup_filename(dir
, &dentry
->d_name
, 1, fname
);
113 if (err
&& err
!= -ENOENT
)
116 if (fname
->is_ciphertext_name
) {
117 spin_lock(&dentry
->d_lock
);
118 dentry
->d_flags
|= DCACHE_ENCRYPTED_NAME
;
119 spin_unlock(&dentry
->d_lock
);
120 d_set_d_op(dentry
, &fscrypt_d_ops
);
124 EXPORT_SYMBOL_GPL(__fscrypt_prepare_lookup
);
126 int __fscrypt_prepare_symlink(struct inode
*dir
, unsigned int len
,
127 unsigned int max_len
,
128 struct fscrypt_str
*disk_link
)
133 * To calculate the size of the encrypted symlink target we need to know
134 * the amount of NUL padding, which is determined by the flags set in
135 * the encryption policy which will be inherited from the directory.
136 * The easiest way to get access to this is to just load the directory's
137 * fscrypt_info, since we'll need it to create the dir_entry anyway.
139 * Note: in test_dummy_encryption mode, @dir may be unencrypted.
141 err
= fscrypt_get_encryption_info(dir
);
144 if (!fscrypt_has_encryption_key(dir
))
148 * Calculate the size of the encrypted symlink and verify it won't
149 * exceed max_len. Note that for historical reasons, encrypted symlink
150 * targets are prefixed with the ciphertext length, despite this
151 * actually being redundant with i_size. This decreases by 2 bytes the
152 * longest symlink target we can accept.
154 * We could recover 1 byte by not counting a null terminator, but
155 * counting it (even though it is meaningless for ciphertext) is simpler
156 * for now since filesystems will assume it is there and subtract it.
158 if (!fscrypt_fname_encrypted_size(dir
, len
,
159 max_len
- sizeof(struct fscrypt_symlink_data
),
161 return -ENAMETOOLONG
;
162 disk_link
->len
+= sizeof(struct fscrypt_symlink_data
);
164 disk_link
->name
= NULL
;
167 EXPORT_SYMBOL_GPL(__fscrypt_prepare_symlink
);
169 int __fscrypt_encrypt_symlink(struct inode
*inode
, const char *target
,
170 unsigned int len
, struct fscrypt_str
*disk_link
)
173 struct qstr iname
= QSTR_INIT(target
, len
);
174 struct fscrypt_symlink_data
*sd
;
175 unsigned int ciphertext_len
;
177 err
= fscrypt_require_key(inode
);
181 if (disk_link
->name
) {
182 /* filesystem-provided buffer */
183 sd
= (struct fscrypt_symlink_data
*)disk_link
->name
;
185 sd
= kmalloc(disk_link
->len
, GFP_NOFS
);
189 ciphertext_len
= disk_link
->len
- sizeof(*sd
);
190 sd
->len
= cpu_to_le16(ciphertext_len
);
192 err
= fname_encrypt(inode
, &iname
, sd
->encrypted_path
, ciphertext_len
);
197 * Null-terminating the ciphertext doesn't make sense, but we still
198 * count the null terminator in the length, so we might as well
199 * initialize it just in case the filesystem writes it out.
201 sd
->encrypted_path
[ciphertext_len
] = '\0';
203 /* Cache the plaintext symlink target for later use by get_link() */
205 inode
->i_link
= kmemdup(target
, len
+ 1, GFP_NOFS
);
209 if (!disk_link
->name
)
210 disk_link
->name
= (unsigned char *)sd
;
214 if (!disk_link
->name
)
218 EXPORT_SYMBOL_GPL(__fscrypt_encrypt_symlink
);
221 * fscrypt_get_symlink - get the target of an encrypted symlink
222 * @inode: the symlink inode
223 * @caddr: the on-disk contents of the symlink
224 * @max_size: size of @caddr buffer
225 * @done: if successful, will be set up to free the returned target if needed
227 * If the symlink's encryption key is available, we decrypt its target.
228 * Otherwise, we encode its target for presentation.
230 * This may sleep, so the filesystem must have dropped out of RCU mode already.
232 * Return: the presentable symlink target or an ERR_PTR()
234 const char *fscrypt_get_symlink(struct inode
*inode
, const void *caddr
,
235 unsigned int max_size
,
236 struct delayed_call
*done
)
238 const struct fscrypt_symlink_data
*sd
;
239 struct fscrypt_str cstr
, pstr
;
243 /* This is for encrypted symlinks only */
244 if (WARN_ON(!IS_ENCRYPTED(inode
)))
245 return ERR_PTR(-EINVAL
);
247 /* If the decrypted target is already cached, just return it. */
248 pstr
.name
= READ_ONCE(inode
->i_link
);
253 * Try to set up the symlink's encryption key, but we can continue
254 * regardless of whether the key is available or not.
256 err
= fscrypt_get_encryption_info(inode
);
259 has_key
= fscrypt_has_encryption_key(inode
);
262 * For historical reasons, encrypted symlink targets are prefixed with
263 * the ciphertext length, even though this is redundant with i_size.
266 if (max_size
< sizeof(*sd
))
267 return ERR_PTR(-EUCLEAN
);
269 cstr
.name
= (unsigned char *)sd
->encrypted_path
;
270 cstr
.len
= le16_to_cpu(sd
->len
);
273 return ERR_PTR(-EUCLEAN
);
275 if (cstr
.len
+ sizeof(*sd
) - 1 > max_size
)
276 return ERR_PTR(-EUCLEAN
);
278 err
= fscrypt_fname_alloc_buffer(inode
, cstr
.len
, &pstr
);
282 err
= fscrypt_fname_disk_to_usr(inode
, 0, 0, &cstr
, &pstr
);
287 if (pstr
.name
[0] == '\0')
290 pstr
.name
[pstr
.len
] = '\0';
293 * Cache decrypted symlink targets in i_link for later use. Don't cache
294 * symlink targets encoded without the key, since those become outdated
295 * once the key is added. This pairs with the READ_ONCE() above and in
296 * the VFS path lookup code.
299 cmpxchg_release(&inode
->i_link
, NULL
, pstr
.name
) != NULL
)
300 set_delayed_call(done
, kfree_link
, pstr
.name
);
308 EXPORT_SYMBOL_GPL(fscrypt_get_symlink
);