xref: /linux/fs/crypto/hooks.c (revision c532de5a67a70f8533d495f8f2aaa9a0491c3ad0)
1 // SPDX-License-Identifier: GPL-2.0-only
2 /*
3  * fs/crypto/hooks.c
4  *
5  * Encryption hooks for higher-level filesystem operations.
6  */
7 
8 #include "fscrypt_private.h"
9 
10 /**
11  * fscrypt_file_open() - prepare to open a possibly-encrypted regular file
12  * @inode: the inode being opened
13  * @filp: the struct file being set up
14  *
15  * Currently, an encrypted regular file can only be opened if its encryption key
16  * is available; access to the raw encrypted contents is not supported.
17  * Therefore, we first set up the inode's encryption key (if not already done)
18  * and return an error if it's unavailable.
19  *
20  * We also verify that if the parent directory (from the path via which the file
21  * is being opened) is encrypted, then the inode being opened uses the same
22  * encryption policy.  This is needed as part of the enforcement that all files
23  * in an encrypted directory tree use the same encryption policy, as a
24  * protection against certain types of offline attacks.  Note that this check is
25  * needed even when opening an *unencrypted* file, since it's forbidden to have
26  * an unencrypted file in an encrypted directory.
27  *
28  * Return: 0 on success, -ENOKEY if the key is missing, or another -errno code
29  */
30 int fscrypt_file_open(struct inode *inode, struct file *filp)
31 {
32 	int err;
33 	struct dentry *dentry, *dentry_parent;
34 	struct inode *inode_parent;
35 
36 	err = fscrypt_require_key(inode);
37 	if (err)
38 		return err;
39 
40 	dentry = file_dentry(filp);
41 
42 	/*
43 	 * Getting a reference to the parent dentry is needed for the actual
44 	 * encryption policy comparison, but it's expensive on multi-core
45 	 * systems.  Since this function runs on unencrypted files too, start
46 	 * with a lightweight RCU-mode check for the parent directory being
47 	 * unencrypted (in which case it's fine for the child to be either
48 	 * unencrypted, or encrypted with any policy).  Only continue on to the
49 	 * full policy check if the parent directory is actually encrypted.
50 	 */
51 	rcu_read_lock();
52 	dentry_parent = READ_ONCE(dentry->d_parent);
53 	inode_parent = d_inode_rcu(dentry_parent);
54 	if (inode_parent != NULL && !IS_ENCRYPTED(inode_parent)) {
55 		rcu_read_unlock();
56 		return 0;
57 	}
58 	rcu_read_unlock();
59 
60 	dentry_parent = dget_parent(dentry);
61 	if (!fscrypt_has_permitted_context(d_inode(dentry_parent), inode)) {
62 		fscrypt_warn(inode,
63 			     "Inconsistent encryption context (parent directory: %lu)",
64 			     d_inode(dentry_parent)->i_ino);
65 		err = -EPERM;
66 	}
67 	dput(dentry_parent);
68 	return err;
69 }
70 EXPORT_SYMBOL_GPL(fscrypt_file_open);
71 
72 int __fscrypt_prepare_link(struct inode *inode, struct inode *dir,
73 			   struct dentry *dentry)
74 {
75 	if (fscrypt_is_nokey_name(dentry))
76 		return -ENOKEY;
77 	/*
78 	 * We don't need to separately check that the directory inode's key is
79 	 * available, as it's implied by the dentry not being a no-key name.
80 	 */
81 
82 	if (!fscrypt_has_permitted_context(dir, inode))
83 		return -EXDEV;
84 
85 	return 0;
86 }
87 EXPORT_SYMBOL_GPL(__fscrypt_prepare_link);
88 
89 int __fscrypt_prepare_rename(struct inode *old_dir, struct dentry *old_dentry,
90 			     struct inode *new_dir, struct dentry *new_dentry,
91 			     unsigned int flags)
92 {
93 	if (fscrypt_is_nokey_name(old_dentry) ||
94 	    fscrypt_is_nokey_name(new_dentry))
95 		return -ENOKEY;
96 	/*
97 	 * We don't need to separately check that the directory inodes' keys are
98 	 * available, as it's implied by the dentries not being no-key names.
99 	 */
100 
101 	if (old_dir != new_dir) {
102 		if (IS_ENCRYPTED(new_dir) &&
103 		    !fscrypt_has_permitted_context(new_dir,
104 						   d_inode(old_dentry)))
105 			return -EXDEV;
106 
107 		if ((flags & RENAME_EXCHANGE) &&
108 		    IS_ENCRYPTED(old_dir) &&
109 		    !fscrypt_has_permitted_context(old_dir,
110 						   d_inode(new_dentry)))
111 			return -EXDEV;
112 	}
113 	return 0;
114 }
115 EXPORT_SYMBOL_GPL(__fscrypt_prepare_rename);
116 
117 int __fscrypt_prepare_lookup(struct inode *dir, struct dentry *dentry,
118 			     struct fscrypt_name *fname)
119 {
120 	int err = fscrypt_setup_filename(dir, &dentry->d_name, 1, fname);
121 
122 	if (err && err != -ENOENT)
123 		return err;
124 
125 	fscrypt_prepare_dentry(dentry, fname->is_nokey_name);
126 
127 	return err;
128 }
129 EXPORT_SYMBOL_GPL(__fscrypt_prepare_lookup);
130 
131 /**
132  * fscrypt_prepare_lookup_partial() - prepare lookup without filename setup
133  * @dir: the encrypted directory being searched
134  * @dentry: the dentry being looked up in @dir
135  *
136  * This function should be used by the ->lookup and ->atomic_open methods of
137  * filesystems that handle filename encryption and no-key name encoding
138  * themselves and thus can't use fscrypt_prepare_lookup().  Like
139  * fscrypt_prepare_lookup(), this will try to set up the directory's encryption
140  * key and will set DCACHE_NOKEY_NAME on the dentry if the key is unavailable.
141  * However, this function doesn't set up a struct fscrypt_name for the filename.
142  *
143  * Return: 0 on success; -errno on error.  Note that the encryption key being
144  *	   unavailable is not considered an error.  It is also not an error if
145  *	   the encryption policy is unsupported by this kernel; that is treated
146  *	   like the key being unavailable, so that files can still be deleted.
147  */
148 int fscrypt_prepare_lookup_partial(struct inode *dir, struct dentry *dentry)
149 {
150 	int err = fscrypt_get_encryption_info(dir, true);
151 	bool is_nokey_name = (!err && !fscrypt_has_encryption_key(dir));
152 
153 	fscrypt_prepare_dentry(dentry, is_nokey_name);
154 
155 	return err;
156 }
157 EXPORT_SYMBOL_GPL(fscrypt_prepare_lookup_partial);
158 
159 int __fscrypt_prepare_readdir(struct inode *dir)
160 {
161 	return fscrypt_get_encryption_info(dir, true);
162 }
163 EXPORT_SYMBOL_GPL(__fscrypt_prepare_readdir);
164 
165 int __fscrypt_prepare_setattr(struct dentry *dentry, struct iattr *attr)
166 {
167 	if (attr->ia_valid & ATTR_SIZE)
168 		return fscrypt_require_key(d_inode(dentry));
169 	return 0;
170 }
171 EXPORT_SYMBOL_GPL(__fscrypt_prepare_setattr);
172 
173 /**
174  * fscrypt_prepare_setflags() - prepare to change flags with FS_IOC_SETFLAGS
175  * @inode: the inode on which flags are being changed
176  * @oldflags: the old flags
177  * @flags: the new flags
178  *
179  * The caller should be holding i_rwsem for write.
180  *
181  * Return: 0 on success; -errno if the flags change isn't allowed or if
182  *	   another error occurs.
183  */
184 int fscrypt_prepare_setflags(struct inode *inode,
185 			     unsigned int oldflags, unsigned int flags)
186 {
187 	struct fscrypt_inode_info *ci;
188 	struct fscrypt_master_key *mk;
189 	int err;
190 
191 	/*
192 	 * When the CASEFOLD flag is set on an encrypted directory, we must
193 	 * derive the secret key needed for the dirhash.  This is only possible
194 	 * if the directory uses a v2 encryption policy.
195 	 */
196 	if (IS_ENCRYPTED(inode) && (flags & ~oldflags & FS_CASEFOLD_FL)) {
197 		err = fscrypt_require_key(inode);
198 		if (err)
199 			return err;
200 		ci = inode->i_crypt_info;
201 		if (ci->ci_policy.version != FSCRYPT_POLICY_V2)
202 			return -EINVAL;
203 		mk = ci->ci_master_key;
204 		down_read(&mk->mk_sem);
205 		if (mk->mk_present)
206 			err = fscrypt_derive_dirhash_key(ci, mk);
207 		else
208 			err = -ENOKEY;
209 		up_read(&mk->mk_sem);
210 		return err;
211 	}
212 	return 0;
213 }
214 
215 /**
216  * fscrypt_prepare_symlink() - prepare to create a possibly-encrypted symlink
217  * @dir: directory in which the symlink is being created
218  * @target: plaintext symlink target
219  * @len: length of @target excluding null terminator
220  * @max_len: space the filesystem has available to store the symlink target
221  * @disk_link: (out) the on-disk symlink target being prepared
222  *
223  * This function computes the size the symlink target will require on-disk,
224  * stores it in @disk_link->len, and validates it against @max_len.  An
225  * encrypted symlink may be longer than the original.
226  *
227  * Additionally, @disk_link->name is set to @target if the symlink will be
228  * unencrypted, but left NULL if the symlink will be encrypted.  For encrypted
229  * symlinks, the filesystem must call fscrypt_encrypt_symlink() to create the
230  * on-disk target later.  (The reason for the two-step process is that some
231  * filesystems need to know the size of the symlink target before creating the
232  * inode, e.g. to determine whether it will be a "fast" or "slow" symlink.)
233  *
234  * Return: 0 on success, -ENAMETOOLONG if the symlink target is too long,
235  * -ENOKEY if the encryption key is missing, or another -errno code if a problem
236  * occurred while setting up the encryption key.
237  */
238 int fscrypt_prepare_symlink(struct inode *dir, const char *target,
239 			    unsigned int len, unsigned int max_len,
240 			    struct fscrypt_str *disk_link)
241 {
242 	const union fscrypt_policy *policy;
243 
244 	/*
245 	 * To calculate the size of the encrypted symlink target we need to know
246 	 * the amount of NUL padding, which is determined by the flags set in
247 	 * the encryption policy which will be inherited from the directory.
248 	 */
249 	policy = fscrypt_policy_to_inherit(dir);
250 	if (policy == NULL) {
251 		/* Not encrypted */
252 		disk_link->name = (unsigned char *)target;
253 		disk_link->len = len + 1;
254 		if (disk_link->len > max_len)
255 			return -ENAMETOOLONG;
256 		return 0;
257 	}
258 	if (IS_ERR(policy))
259 		return PTR_ERR(policy);
260 
261 	/*
262 	 * Calculate the size of the encrypted symlink and verify it won't
263 	 * exceed max_len.  Note that for historical reasons, encrypted symlink
264 	 * targets are prefixed with the ciphertext length, despite this
265 	 * actually being redundant with i_size.  This decreases by 2 bytes the
266 	 * longest symlink target we can accept.
267 	 *
268 	 * We could recover 1 byte by not counting a null terminator, but
269 	 * counting it (even though it is meaningless for ciphertext) is simpler
270 	 * for now since filesystems will assume it is there and subtract it.
271 	 */
272 	if (!__fscrypt_fname_encrypted_size(policy, len,
273 					    max_len - sizeof(struct fscrypt_symlink_data) - 1,
274 					    &disk_link->len))
275 		return -ENAMETOOLONG;
276 	disk_link->len += sizeof(struct fscrypt_symlink_data) + 1;
277 
278 	disk_link->name = NULL;
279 	return 0;
280 }
281 EXPORT_SYMBOL_GPL(fscrypt_prepare_symlink);
282 
283 int __fscrypt_encrypt_symlink(struct inode *inode, const char *target,
284 			      unsigned int len, struct fscrypt_str *disk_link)
285 {
286 	int err;
287 	struct qstr iname = QSTR_INIT(target, len);
288 	struct fscrypt_symlink_data *sd;
289 	unsigned int ciphertext_len;
290 
291 	/*
292 	 * fscrypt_prepare_new_inode() should have already set up the new
293 	 * symlink inode's encryption key.  We don't wait until now to do it,
294 	 * since we may be in a filesystem transaction now.
295 	 */
296 	if (WARN_ON_ONCE(!fscrypt_has_encryption_key(inode)))
297 		return -ENOKEY;
298 
299 	if (disk_link->name) {
300 		/* filesystem-provided buffer */
301 		sd = (struct fscrypt_symlink_data *)disk_link->name;
302 	} else {
303 		sd = kmalloc(disk_link->len, GFP_NOFS);
304 		if (!sd)
305 			return -ENOMEM;
306 	}
307 	ciphertext_len = disk_link->len - sizeof(*sd) - 1;
308 	sd->len = cpu_to_le16(ciphertext_len);
309 
310 	err = fscrypt_fname_encrypt(inode, &iname, sd->encrypted_path,
311 				    ciphertext_len);
312 	if (err)
313 		goto err_free_sd;
314 
315 	/*
316 	 * Null-terminating the ciphertext doesn't make sense, but we still
317 	 * count the null terminator in the length, so we might as well
318 	 * initialize it just in case the filesystem writes it out.
319 	 */
320 	sd->encrypted_path[ciphertext_len] = '\0';
321 
322 	/* Cache the plaintext symlink target for later use by get_link() */
323 	err = -ENOMEM;
324 	inode->i_link = kmemdup(target, len + 1, GFP_NOFS);
325 	if (!inode->i_link)
326 		goto err_free_sd;
327 
328 	if (!disk_link->name)
329 		disk_link->name = (unsigned char *)sd;
330 	return 0;
331 
332 err_free_sd:
333 	if (!disk_link->name)
334 		kfree(sd);
335 	return err;
336 }
337 EXPORT_SYMBOL_GPL(__fscrypt_encrypt_symlink);
338 
339 /**
340  * fscrypt_get_symlink() - get the target of an encrypted symlink
341  * @inode: the symlink inode
342  * @caddr: the on-disk contents of the symlink
343  * @max_size: size of @caddr buffer
344  * @done: if successful, will be set up to free the returned target if needed
345  *
346  * If the symlink's encryption key is available, we decrypt its target.
347  * Otherwise, we encode its target for presentation.
348  *
349  * This may sleep, so the filesystem must have dropped out of RCU mode already.
350  *
351  * Return: the presentable symlink target or an ERR_PTR()
352  */
353 const char *fscrypt_get_symlink(struct inode *inode, const void *caddr,
354 				unsigned int max_size,
355 				struct delayed_call *done)
356 {
357 	const struct fscrypt_symlink_data *sd;
358 	struct fscrypt_str cstr, pstr;
359 	bool has_key;
360 	int err;
361 
362 	/* This is for encrypted symlinks only */
363 	if (WARN_ON_ONCE(!IS_ENCRYPTED(inode)))
364 		return ERR_PTR(-EINVAL);
365 
366 	/* If the decrypted target is already cached, just return it. */
367 	pstr.name = READ_ONCE(inode->i_link);
368 	if (pstr.name)
369 		return pstr.name;
370 
371 	/*
372 	 * Try to set up the symlink's encryption key, but we can continue
373 	 * regardless of whether the key is available or not.
374 	 */
375 	err = fscrypt_get_encryption_info(inode, false);
376 	if (err)
377 		return ERR_PTR(err);
378 	has_key = fscrypt_has_encryption_key(inode);
379 
380 	/*
381 	 * For historical reasons, encrypted symlink targets are prefixed with
382 	 * the ciphertext length, even though this is redundant with i_size.
383 	 */
384 
385 	if (max_size < sizeof(*sd) + 1)
386 		return ERR_PTR(-EUCLEAN);
387 	sd = caddr;
388 	cstr.name = (unsigned char *)sd->encrypted_path;
389 	cstr.len = le16_to_cpu(sd->len);
390 
391 	if (cstr.len == 0)
392 		return ERR_PTR(-EUCLEAN);
393 
394 	if (cstr.len + sizeof(*sd) > max_size)
395 		return ERR_PTR(-EUCLEAN);
396 
397 	err = fscrypt_fname_alloc_buffer(cstr.len, &pstr);
398 	if (err)
399 		return ERR_PTR(err);
400 
401 	err = fscrypt_fname_disk_to_usr(inode, 0, 0, &cstr, &pstr);
402 	if (err)
403 		goto err_kfree;
404 
405 	err = -EUCLEAN;
406 	if (pstr.name[0] == '\0')
407 		goto err_kfree;
408 
409 	pstr.name[pstr.len] = '\0';
410 
411 	/*
412 	 * Cache decrypted symlink targets in i_link for later use.  Don't cache
413 	 * symlink targets encoded without the key, since those become outdated
414 	 * once the key is added.  This pairs with the READ_ONCE() above and in
415 	 * the VFS path lookup code.
416 	 */
417 	if (!has_key ||
418 	    cmpxchg_release(&inode->i_link, NULL, pstr.name) != NULL)
419 		set_delayed_call(done, kfree_link, pstr.name);
420 
421 	return pstr.name;
422 
423 err_kfree:
424 	kfree(pstr.name);
425 	return ERR_PTR(err);
426 }
427 EXPORT_SYMBOL_GPL(fscrypt_get_symlink);
428 
429 /**
430  * fscrypt_symlink_getattr() - set the correct st_size for encrypted symlinks
431  * @path: the path for the encrypted symlink being queried
432  * @stat: the struct being filled with the symlink's attributes
433  *
434  * Override st_size of encrypted symlinks to be the length of the decrypted
435  * symlink target (or the no-key encoded symlink target, if the key is
436  * unavailable) rather than the length of the encrypted symlink target.  This is
437  * necessary for st_size to match the symlink target that userspace actually
438  * sees.  POSIX requires this, and some userspace programs depend on it.
439  *
440  * This requires reading the symlink target from disk if needed, setting up the
441  * inode's encryption key if possible, and then decrypting or encoding the
442  * symlink target.  This makes lstat() more heavyweight than is normally the
443  * case.  However, decrypted symlink targets will be cached in ->i_link, so
444  * usually the symlink won't have to be read and decrypted again later if/when
445  * it is actually followed, readlink() is called, or lstat() is called again.
446  *
447  * Return: 0 on success, -errno on failure
448  */
449 int fscrypt_symlink_getattr(const struct path *path, struct kstat *stat)
450 {
451 	struct dentry *dentry = path->dentry;
452 	struct inode *inode = d_inode(dentry);
453 	const char *link;
454 	DEFINE_DELAYED_CALL(done);
455 
456 	/*
457 	 * To get the symlink target that userspace will see (whether it's the
458 	 * decrypted target or the no-key encoded target), we can just get it in
459 	 * the same way the VFS does during path resolution and readlink().
460 	 */
461 	link = READ_ONCE(inode->i_link);
462 	if (!link) {
463 		link = inode->i_op->get_link(dentry, inode, &done);
464 		if (IS_ERR(link))
465 			return PTR_ERR(link);
466 	}
467 	stat->size = strlen(link);
468 	do_delayed_call(&done);
469 	return 0;
470 }
471 EXPORT_SYMBOL_GPL(fscrypt_symlink_getattr);
472