blob: a6396bf721acfd86a98c8810b2e2bf85d4d71f8d [file] [log] [blame]
Eric Biggersefcc7ae2017-10-09 12:15:40 -07001/*
2 * fs/crypto/hooks.c
3 *
4 * Encryption hooks for higher-level filesystem operations.
5 */
6
Daniel Rosenberg7495f912020-01-20 14:31:57 -08007#include <linux/key.h>
8
Eric Biggersefcc7ae2017-10-09 12:15:40 -07009#include "fscrypt_private.h"
10
11/**
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
15 *
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.
20 *
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.
28 *
29 * Return: 0 on success, -ENOKEY if the key is missing, or another -errno code
30 */
31int fscrypt_file_open(struct inode *inode, struct file *filp)
32{
33 int err;
34 struct dentry *dir;
35
36 err = fscrypt_require_key(inode);
37 if (err)
38 return err;
39
40 dir = dget_parent(file_dentry(filp));
41 if (IS_ENCRYPTED(d_inode(dir)) &&
42 !fscrypt_has_permitted_context(d_inode(dir), inode)) {
Eric Biggers9aa799b2019-07-24 11:07:58 -070043 fscrypt_warn(inode,
44 "Inconsistent encryption context (parent directory: %lu)",
45 d_inode(dir)->i_ino);
Eric Biggersefcc7ae2017-10-09 12:15:40 -070046 err = -EPERM;
47 }
48 dput(dir);
49 return err;
50}
51EXPORT_SYMBOL_GPL(fscrypt_file_open);
Eric Biggers0ea87a92017-10-09 12:15:41 -070052
Eric Biggers081985b2019-03-20 11:39:10 -070053int __fscrypt_prepare_link(struct inode *inode, struct inode *dir,
54 struct dentry *dentry)
Eric Biggers0ea87a92017-10-09 12:15:41 -070055{
56 int err;
57
58 err = fscrypt_require_key(dir);
59 if (err)
60 return err;
61
Eric Biggers081985b2019-03-20 11:39:10 -070062 /* ... in case we looked up ciphertext name before key was added */
63 if (dentry->d_flags & DCACHE_ENCRYPTED_NAME)
64 return -ENOKEY;
65
Eric Biggers0ea87a92017-10-09 12:15:41 -070066 if (!fscrypt_has_permitted_context(dir, inode))
Eric Biggers94e10682019-01-22 16:20:21 -080067 return -EXDEV;
Eric Biggers0ea87a92017-10-09 12:15:41 -070068
69 return 0;
70}
71EXPORT_SYMBOL_GPL(__fscrypt_prepare_link);
Eric Biggers94b26f32017-10-09 12:15:42 -070072
73int __fscrypt_prepare_rename(struct inode *old_dir, struct dentry *old_dentry,
74 struct inode *new_dir, struct dentry *new_dentry,
75 unsigned int flags)
76{
77 int err;
78
79 err = fscrypt_require_key(old_dir);
80 if (err)
81 return err;
82
83 err = fscrypt_require_key(new_dir);
84 if (err)
85 return err;
86
Eric Biggers081985b2019-03-20 11:39:10 -070087 /* ... 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)
90 return -ENOKEY;
91
Eric Biggers94b26f32017-10-09 12:15:42 -070092 if (old_dir != new_dir) {
93 if (IS_ENCRYPTED(new_dir) &&
94 !fscrypt_has_permitted_context(new_dir,
95 d_inode(old_dentry)))
Eric Biggers94e10682019-01-22 16:20:21 -080096 return -EXDEV;
Eric Biggers94b26f32017-10-09 12:15:42 -070097
98 if ((flags & RENAME_EXCHANGE) &&
99 IS_ENCRYPTED(old_dir) &&
100 !fscrypt_has_permitted_context(old_dir,
101 d_inode(new_dentry)))
Eric Biggers94e10682019-01-22 16:20:21 -0800102 return -EXDEV;
Eric Biggers94b26f32017-10-09 12:15:42 -0700103 }
104 return 0;
105}
106EXPORT_SYMBOL_GPL(__fscrypt_prepare_rename);
Eric Biggers32c3cf02017-10-09 12:15:43 -0700107
Eric Biggers716c3702019-03-20 11:39:13 -0700108int __fscrypt_prepare_lookup(struct inode *dir, struct dentry *dentry,
109 struct fscrypt_name *fname)
Eric Biggers32c3cf02017-10-09 12:15:43 -0700110{
Eric Biggers716c3702019-03-20 11:39:13 -0700111 int err = fscrypt_setup_filename(dir, &dentry->d_name, 1, fname);
Eric Biggers32c3cf02017-10-09 12:15:43 -0700112
Eric Biggers716c3702019-03-20 11:39:13 -0700113 if (err && err != -ENOENT)
Eric Biggers32c3cf02017-10-09 12:15:43 -0700114 return err;
115
Eric Biggers716c3702019-03-20 11:39:13 -0700116 if (fname->is_ciphertext_name) {
Eric Biggers32c3cf02017-10-09 12:15:43 -0700117 spin_lock(&dentry->d_lock);
Eric Biggers37b434e2019-03-20 11:39:09 -0700118 dentry->d_flags |= DCACHE_ENCRYPTED_NAME;
Eric Biggers32c3cf02017-10-09 12:15:43 -0700119 spin_unlock(&dentry->d_lock);
120 }
Eric Biggers716c3702019-03-20 11:39:13 -0700121 return err;
Eric Biggers32c3cf02017-10-09 12:15:43 -0700122}
123EXPORT_SYMBOL_GPL(__fscrypt_prepare_lookup);
Eric Biggers76e81d62018-01-05 10:45:01 -0800124
Daniel Rosenbergf4951342020-01-20 14:31:56 -0800125/**
126 * fscrypt_prepare_setflags() - prepare to change flags with FS_IOC_SETFLAGS
127 * @inode: the inode on which flags are being changed
128 * @oldflags: the old flags
129 * @flags: the new flags
130 *
131 * The caller should be holding i_rwsem for write.
132 *
133 * Return: 0 on success; -errno if the flags change isn't allowed or if
134 * another error occurs.
135 */
136int fscrypt_prepare_setflags(struct inode *inode,
137 unsigned int oldflags, unsigned int flags)
138{
139 struct fscrypt_info *ci;
Daniel Rosenberg7495f912020-01-20 14:31:57 -0800140 struct fscrypt_master_key *mk;
Daniel Rosenbergf4951342020-01-20 14:31:56 -0800141 int err;
142
Daniel Rosenberg7495f912020-01-20 14:31:57 -0800143 /*
144 * When the CASEFOLD flag is set on an encrypted directory, we must
145 * derive the secret key needed for the dirhash. This is only possible
146 * if the directory uses a v2 encryption policy.
147 */
Daniel Rosenbergf4951342020-01-20 14:31:56 -0800148 if (IS_ENCRYPTED(inode) && (flags & ~oldflags & FS_CASEFOLD_FL)) {
149 err = fscrypt_require_key(inode);
150 if (err)
151 return err;
152 ci = inode->i_crypt_info;
153 if (ci->ci_policy.version != FSCRYPT_POLICY_V2)
154 return -EINVAL;
Daniel Rosenberg7495f912020-01-20 14:31:57 -0800155 mk = ci->ci_master_key->payload.data[0];
156 down_read(&mk->mk_secret_sem);
157 if (is_master_key_secret_present(&mk->mk_secret))
158 err = fscrypt_derive_dirhash_key(ci, mk);
159 else
160 err = -ENOKEY;
161 up_read(&mk->mk_secret_sem);
162 return err;
Daniel Rosenbergf4951342020-01-20 14:31:56 -0800163 }
164 return 0;
165}
166
Eric Biggers76e81d62018-01-05 10:45:01 -0800167int __fscrypt_prepare_symlink(struct inode *dir, unsigned int len,
168 unsigned int max_len,
169 struct fscrypt_str *disk_link)
170{
171 int err;
172
173 /*
174 * To calculate the size of the encrypted symlink target we need to know
175 * the amount of NUL padding, which is determined by the flags set in
176 * the encryption policy which will be inherited from the directory.
177 * The easiest way to get access to this is to just load the directory's
178 * fscrypt_info, since we'll need it to create the dir_entry anyway.
179 *
180 * Note: in test_dummy_encryption mode, @dir may be unencrypted.
181 */
182 err = fscrypt_get_encryption_info(dir);
183 if (err)
184 return err;
185 if (!fscrypt_has_encryption_key(dir))
186 return -ENOKEY;
187
188 /*
189 * Calculate the size of the encrypted symlink and verify it won't
190 * exceed max_len. Note that for historical reasons, encrypted symlink
191 * targets are prefixed with the ciphertext length, despite this
192 * actually being redundant with i_size. This decreases by 2 bytes the
193 * longest symlink target we can accept.
194 *
195 * We could recover 1 byte by not counting a null terminator, but
196 * counting it (even though it is meaningless for ciphertext) is simpler
197 * for now since filesystems will assume it is there and subtract it.
198 */
Eric Biggersb9db0b42018-01-11 23:30:08 -0500199 if (!fscrypt_fname_encrypted_size(dir, len,
200 max_len - sizeof(struct fscrypt_symlink_data),
201 &disk_link->len))
Eric Biggers76e81d62018-01-05 10:45:01 -0800202 return -ENAMETOOLONG;
Eric Biggersb9db0b42018-01-11 23:30:08 -0500203 disk_link->len += sizeof(struct fscrypt_symlink_data);
204
Eric Biggers76e81d62018-01-05 10:45:01 -0800205 disk_link->name = NULL;
206 return 0;
207}
208EXPORT_SYMBOL_GPL(__fscrypt_prepare_symlink);
209
210int __fscrypt_encrypt_symlink(struct inode *inode, const char *target,
211 unsigned int len, struct fscrypt_str *disk_link)
212{
213 int err;
Eric Biggers0b1dfa42018-01-19 13:45:24 -0800214 struct qstr iname = QSTR_INIT(target, len);
Eric Biggers76e81d62018-01-05 10:45:01 -0800215 struct fscrypt_symlink_data *sd;
216 unsigned int ciphertext_len;
Eric Biggers76e81d62018-01-05 10:45:01 -0800217
218 err = fscrypt_require_key(inode);
219 if (err)
220 return err;
221
222 if (disk_link->name) {
223 /* filesystem-provided buffer */
224 sd = (struct fscrypt_symlink_data *)disk_link->name;
225 } else {
226 sd = kmalloc(disk_link->len, GFP_NOFS);
227 if (!sd)
228 return -ENOMEM;
229 }
230 ciphertext_len = disk_link->len - sizeof(*sd);
231 sd->len = cpu_to_le16(ciphertext_len);
232
Eric Biggers2ad325d2020-01-19 23:17:36 -0800233 err = fscrypt_fname_encrypt(inode, &iname, sd->encrypted_path,
234 ciphertext_len);
Eric Biggers1f1be4a2019-04-10 13:21:15 -0700235 if (err)
236 goto err_free_sd;
237
Eric Biggers76e81d62018-01-05 10:45:01 -0800238 /*
239 * Null-terminating the ciphertext doesn't make sense, but we still
240 * count the null terminator in the length, so we might as well
241 * initialize it just in case the filesystem writes it out.
242 */
243 sd->encrypted_path[ciphertext_len] = '\0';
244
Eric Biggers1f1be4a2019-04-10 13:21:15 -0700245 /* Cache the plaintext symlink target for later use by get_link() */
246 err = -ENOMEM;
247 inode->i_link = kmemdup(target, len + 1, GFP_NOFS);
248 if (!inode->i_link)
249 goto err_free_sd;
250
Eric Biggers76e81d62018-01-05 10:45:01 -0800251 if (!disk_link->name)
252 disk_link->name = (unsigned char *)sd;
253 return 0;
Eric Biggers1f1be4a2019-04-10 13:21:15 -0700254
255err_free_sd:
256 if (!disk_link->name)
257 kfree(sd);
258 return err;
Eric Biggers76e81d62018-01-05 10:45:01 -0800259}
260EXPORT_SYMBOL_GPL(__fscrypt_encrypt_symlink);
Eric Biggers3b0d8832018-01-05 10:45:02 -0800261
262/**
263 * fscrypt_get_symlink - get the target of an encrypted symlink
264 * @inode: the symlink inode
265 * @caddr: the on-disk contents of the symlink
266 * @max_size: size of @caddr buffer
Eric Biggers1f1be4a2019-04-10 13:21:15 -0700267 * @done: if successful, will be set up to free the returned target if needed
Eric Biggers3b0d8832018-01-05 10:45:02 -0800268 *
269 * If the symlink's encryption key is available, we decrypt its target.
270 * Otherwise, we encode its target for presentation.
271 *
272 * This may sleep, so the filesystem must have dropped out of RCU mode already.
273 *
274 * Return: the presentable symlink target or an ERR_PTR()
275 */
276const char *fscrypt_get_symlink(struct inode *inode, const void *caddr,
277 unsigned int max_size,
278 struct delayed_call *done)
279{
280 const struct fscrypt_symlink_data *sd;
281 struct fscrypt_str cstr, pstr;
Eric Biggers1f1be4a2019-04-10 13:21:15 -0700282 bool has_key;
Eric Biggers3b0d8832018-01-05 10:45:02 -0800283 int err;
284
285 /* This is for encrypted symlinks only */
286 if (WARN_ON(!IS_ENCRYPTED(inode)))
287 return ERR_PTR(-EINVAL);
288
Eric Biggers1f1be4a2019-04-10 13:21:15 -0700289 /* If the decrypted target is already cached, just return it. */
290 pstr.name = READ_ONCE(inode->i_link);
291 if (pstr.name)
292 return pstr.name;
293
Eric Biggers3b0d8832018-01-05 10:45:02 -0800294 /*
295 * Try to set up the symlink's encryption key, but we can continue
296 * regardless of whether the key is available or not.
297 */
298 err = fscrypt_get_encryption_info(inode);
299 if (err)
300 return ERR_PTR(err);
Eric Biggers1f1be4a2019-04-10 13:21:15 -0700301 has_key = fscrypt_has_encryption_key(inode);
Eric Biggers3b0d8832018-01-05 10:45:02 -0800302
303 /*
304 * For historical reasons, encrypted symlink targets are prefixed with
305 * the ciphertext length, even though this is redundant with i_size.
306 */
307
308 if (max_size < sizeof(*sd))
309 return ERR_PTR(-EUCLEAN);
310 sd = caddr;
311 cstr.name = (unsigned char *)sd->encrypted_path;
312 cstr.len = le16_to_cpu(sd->len);
313
314 if (cstr.len == 0)
315 return ERR_PTR(-EUCLEAN);
316
317 if (cstr.len + sizeof(*sd) - 1 > max_size)
318 return ERR_PTR(-EUCLEAN);
319
320 err = fscrypt_fname_alloc_buffer(inode, cstr.len, &pstr);
321 if (err)
322 return ERR_PTR(err);
323
324 err = fscrypt_fname_disk_to_usr(inode, 0, 0, &cstr, &pstr);
325 if (err)
326 goto err_kfree;
327
328 err = -EUCLEAN;
329 if (pstr.name[0] == '\0')
330 goto err_kfree;
331
332 pstr.name[pstr.len] = '\0';
Eric Biggers1f1be4a2019-04-10 13:21:15 -0700333
334 /*
335 * Cache decrypted symlink targets in i_link for later use. Don't cache
336 * symlink targets encoded without the key, since those become outdated
337 * once the key is added. This pairs with the READ_ONCE() above and in
338 * the VFS path lookup code.
339 */
340 if (!has_key ||
341 cmpxchg_release(&inode->i_link, NULL, pstr.name) != NULL)
342 set_delayed_call(done, kfree_link, pstr.name);
343
Eric Biggers3b0d8832018-01-05 10:45:02 -0800344 return pstr.name;
345
346err_kfree:
347 kfree(pstr.name);
348 return ERR_PTR(err);
349}
350EXPORT_SYMBOL_GPL(fscrypt_get_symlink);