1 /* SPDX-License-Identifier: GPL-2.0 */
2 /*
3 * fscrypt.h: declarations for per-file encryption
4 *
5 * Filesystems that implement per-file encryption must include this header
6 * file.
7 *
8 * Copyright (C) 2015, Google, Inc.
9 *
10 * Written by Michael Halcrow, 2015.
11 * Modified by Jaegeuk Kim, 2015.
12 */
13 #ifndef _LINUX_FSCRYPT_H
14 #define _LINUX_FSCRYPT_H
15
16 #include <linux/fs.h>
17 #include <linux/mm.h>
18 #include <linux/slab.h>
19 #include <uapi/linux/fscrypt.h>
20
21 /*
22 * The lengths of all file contents blocks must be divisible by this value.
23 * This is needed to ensure that all contents encryption modes will work, as
24 * some of the supported modes don't support arbitrarily byte-aligned messages.
25 *
26 * Since the needed alignment is 16 bytes, most filesystems will meet this
27 * requirement naturally, as typical block sizes are powers of 2. However, if a
28 * filesystem can generate arbitrarily byte-aligned block lengths (e.g., via
29 * compression), then it will need to pad to this alignment before encryption.
30 */
31 #define FSCRYPT_CONTENTS_ALIGNMENT 16
32
33 union fscrypt_policy;
34 struct fscrypt_inode_info;
35 struct fs_parameter;
36 struct seq_file;
37
38 struct fscrypt_str {
39 unsigned char *name;
40 u32 len;
41 };
42
43 struct fscrypt_name {
44 const struct qstr *usr_fname;
45 struct fscrypt_str disk_name;
46 u32 hash;
47 u32 minor_hash;
48 struct fscrypt_str crypto_buf;
49 bool is_nokey_name;
50 };
51
52 #define FSTR_INIT(n, l) { .name = n, .len = l }
53 #define FSTR_TO_QSTR(f) QSTR_INIT((f)->name, (f)->len)
54 #define fname_name(p) ((p)->disk_name.name)
55 #define fname_len(p) ((p)->disk_name.len)
56
57 /* Maximum value for the third parameter of fscrypt_operations.set_context(). */
58 #define FSCRYPT_SET_CONTEXT_MAX_SIZE 40
59
60 #ifdef CONFIG_FS_ENCRYPTION
61
62 /* Crypto operations for filesystems */
63 struct fscrypt_operations {
64
65 /*
66 * If set, then fs/crypto/ will allocate a global bounce page pool the
67 * first time an encryption key is set up for a file. The bounce page
68 * pool is required by the following functions:
69 *
70 * - fscrypt_encrypt_pagecache_blocks()
71 * - fscrypt_zeroout_range() for files not using inline crypto
72 *
73 * If the filesystem doesn't use those, it doesn't need to set this.
74 */
75 unsigned int needs_bounce_pages : 1;
76
77 /*
78 * If set, then fs/crypto/ will allow the use of encryption settings
79 * that assume inode numbers fit in 32 bits (i.e.
80 * FSCRYPT_POLICY_FLAG_IV_INO_LBLK_{32,64}), provided that the other
81 * prerequisites for these settings are also met. This is only useful
82 * if the filesystem wants to support inline encryption hardware that is
83 * limited to 32-bit or 64-bit data unit numbers and where programming
84 * keyslots is very slow.
85 */
86 unsigned int has_32bit_inodes : 1;
87
88 /*
89 * If set, then fs/crypto/ will allow users to select a crypto data unit
90 * size that is less than the filesystem block size. This is done via
91 * the log2_data_unit_size field of the fscrypt policy. This flag is
92 * not compatible with filesystems that encrypt variable-length blocks
93 * (i.e. blocks that aren't all equal to filesystem's block size), for
94 * example as a result of compression. It's also not compatible with
95 * the fscrypt_encrypt_block_inplace() and
96 * fscrypt_decrypt_block_inplace() functions.
97 */
98 unsigned int supports_subblock_data_units : 1;
99
100 /*
101 * This field exists only for backwards compatibility reasons and should
102 * only be set by the filesystems that are setting it already. It
103 * contains the filesystem-specific key description prefix that is
104 * accepted for "logon" keys for v1 fscrypt policies. This
105 * functionality is deprecated in favor of the generic prefix
106 * "fscrypt:", which itself is deprecated in favor of the filesystem
107 * keyring ioctls such as FS_IOC_ADD_ENCRYPTION_KEY. Filesystems that
108 * are newly adding fscrypt support should not set this field.
109 */
110 const char *legacy_key_prefix;
111
112 /*
113 * Get the fscrypt context of the given inode.
114 *
115 * @inode: the inode whose context to get
116 * @ctx: the buffer into which to get the context
117 * @len: length of the @ctx buffer in bytes
118 *
119 * Return: On success, returns the length of the context in bytes; this
120 * may be less than @len. On failure, returns -ENODATA if the
121 * inode doesn't have a context, -ERANGE if the context is
122 * longer than @len, or another -errno code.
123 */
124 int (*get_context)(struct inode *inode, void *ctx, size_t len);
125
126 /*
127 * Set an fscrypt context on the given inode.
128 *
129 * @inode: the inode whose context to set. The inode won't already have
130 * an fscrypt context.
131 * @ctx: the context to set
132 * @len: length of @ctx in bytes (at most FSCRYPT_SET_CONTEXT_MAX_SIZE)
133 * @fs_data: If called from fscrypt_set_context(), this will be the
134 * value the filesystem passed to fscrypt_set_context().
135 * Otherwise (i.e. when called from
136 * FS_IOC_SET_ENCRYPTION_POLICY) this will be NULL.
137 *
138 * i_rwsem will be held for write.
139 *
140 * Return: 0 on success, -errno on failure.
141 */
142 int (*set_context)(struct inode *inode, const void *ctx, size_t len,
143 void *fs_data);
144
145 /*
146 * Get the dummy fscrypt policy in use on the filesystem (if any).
147 *
148 * Filesystems only need to implement this function if they support the
149 * test_dummy_encryption mount option.
150 *
151 * Return: A pointer to the dummy fscrypt policy, if the filesystem is
152 * mounted with test_dummy_encryption; otherwise NULL.
153 */
154 const union fscrypt_policy *(*get_dummy_policy)(struct super_block *sb);
155
156 /*
157 * Check whether a directory is empty. i_rwsem will be held for write.
158 */
159 bool (*empty_dir)(struct inode *inode);
160
161 /*
162 * Check whether the filesystem's inode numbers and UUID are stable,
163 * meaning that they will never be changed even by offline operations
164 * such as filesystem shrinking and therefore can be used in the
165 * encryption without the possibility of files becoming unreadable.
166 *
167 * Filesystems only need to implement this function if they want to
168 * support the FSCRYPT_POLICY_FLAG_IV_INO_LBLK_{32,64} flags. These
169 * flags are designed to work around the limitations of UFS and eMMC
170 * inline crypto hardware, and they shouldn't be used in scenarios where
171 * such hardware isn't being used.
172 *
173 * Leaving this NULL is equivalent to always returning false.
174 */
175 bool (*has_stable_inodes)(struct super_block *sb);
176
177 /*
178 * Return an array of pointers to the block devices to which the
179 * filesystem may write encrypted file contents, NULL if the filesystem
180 * only has a single such block device, or an ERR_PTR() on error.
181 *
182 * On successful non-NULL return, *num_devs is set to the number of
183 * devices in the returned array. The caller must free the returned
184 * array using kfree().
185 *
186 * If the filesystem can use multiple block devices (other than block
187 * devices that aren't used for encrypted file contents, such as
188 * external journal devices), and wants to support inline encryption,
189 * then it must implement this function. Otherwise it's not needed.
190 */
191 struct block_device **(*get_devices)(struct super_block *sb,
192 unsigned int *num_devs);
193 };
194
195 int fscrypt_d_revalidate(struct inode *dir, const struct qstr *name,
196 struct dentry *dentry, unsigned int flags);
197
198 static inline struct fscrypt_inode_info *
fscrypt_get_inode_info(const struct inode * inode)199 fscrypt_get_inode_info(const struct inode *inode)
200 {
201 /*
202 * Pairs with the cmpxchg_release() in fscrypt_setup_encryption_info().
203 * I.e., another task may publish ->i_crypt_info concurrently, executing
204 * a RELEASE barrier. We need to use smp_load_acquire() here to safely
205 * ACQUIRE the memory the other task published.
206 */
207 return smp_load_acquire(&inode->i_crypt_info);
208 }
209
210 /**
211 * fscrypt_needs_contents_encryption() - check whether an inode needs
212 * contents encryption
213 * @inode: the inode to check
214 *
215 * Return: %true iff the inode is an encrypted regular file and the kernel was
216 * built with fscrypt support.
217 *
218 * If you need to know whether the encrypt bit is set even when the kernel was
219 * built without fscrypt support, you must use IS_ENCRYPTED() directly instead.
220 */
fscrypt_needs_contents_encryption(const struct inode * inode)221 static inline bool fscrypt_needs_contents_encryption(const struct inode *inode)
222 {
223 return IS_ENCRYPTED(inode) && S_ISREG(inode->i_mode);
224 }
225
226 /*
227 * When d_splice_alias() moves a directory's no-key alias to its
228 * plaintext alias as a result of the encryption key being added,
229 * DCACHE_NOKEY_NAME must be cleared and there might be an opportunity
230 * to disable d_revalidate. Note that we don't have to support the
231 * inverse operation because fscrypt doesn't allow no-key names to be
232 * the source or target of a rename().
233 */
fscrypt_handle_d_move(struct dentry * dentry)234 static inline void fscrypt_handle_d_move(struct dentry *dentry)
235 {
236 /*
237 * VFS calls fscrypt_handle_d_move even for non-fscrypt
238 * filesystems.
239 */
240 if (dentry->d_flags & DCACHE_NOKEY_NAME) {
241 dentry->d_flags &= ~DCACHE_NOKEY_NAME;
242
243 /*
244 * Other filesystem features might be handling dentry
245 * revalidation, in which case it cannot be disabled.
246 */
247 if (dentry->d_op->d_revalidate == fscrypt_d_revalidate)
248 dentry->d_flags &= ~DCACHE_OP_REVALIDATE;
249 }
250 }
251
252 /**
253 * fscrypt_is_nokey_name() - test whether a dentry is a no-key name
254 * @dentry: the dentry to check
255 *
256 * This returns true if the dentry is a no-key dentry. A no-key dentry is a
257 * dentry that was created in an encrypted directory that hasn't had its
258 * encryption key added yet. Such dentries may be either positive or negative.
259 *
260 * When a filesystem is asked to create a new filename in an encrypted directory
261 * and the new filename's dentry is a no-key dentry, it must fail the operation
262 * with ENOKEY. This includes ->create(), ->mkdir(), ->mknod(), ->symlink(),
263 * ->rename(), and ->link(). (However, ->rename() and ->link() are already
264 * handled by fscrypt_prepare_rename() and fscrypt_prepare_link().)
265 *
266 * This is necessary because creating a filename requires the directory's
267 * encryption key, but just checking for the key on the directory inode during
268 * the final filesystem operation doesn't guarantee that the key was available
269 * during the preceding dentry lookup. And the key must have already been
270 * available during the dentry lookup in order for it to have been checked
271 * whether the filename already exists in the directory and for the new file's
272 * dentry not to be invalidated due to it incorrectly having the no-key flag.
273 *
274 * Return: %true if the dentry is a no-key name
275 */
fscrypt_is_nokey_name(const struct dentry * dentry)276 static inline bool fscrypt_is_nokey_name(const struct dentry *dentry)
277 {
278 return dentry->d_flags & DCACHE_NOKEY_NAME;
279 }
280
fscrypt_prepare_dentry(struct dentry * dentry,bool is_nokey_name)281 static inline void fscrypt_prepare_dentry(struct dentry *dentry,
282 bool is_nokey_name)
283 {
284 /*
285 * This code tries to only take ->d_lock when necessary to write
286 * to ->d_flags. We shouldn't be peeking on d_flags for
287 * DCACHE_OP_REVALIDATE unlocked, but in the unlikely case
288 * there is a race, the worst it can happen is that we fail to
289 * unset DCACHE_OP_REVALIDATE and pay the cost of an extra
290 * d_revalidate.
291 */
292 if (is_nokey_name) {
293 spin_lock(&dentry->d_lock);
294 dentry->d_flags |= DCACHE_NOKEY_NAME;
295 spin_unlock(&dentry->d_lock);
296 } else if (dentry->d_flags & DCACHE_OP_REVALIDATE &&
297 dentry->d_op->d_revalidate == fscrypt_d_revalidate) {
298 /*
299 * Unencrypted dentries and encrypted dentries where the
300 * key is available are always valid from fscrypt
301 * perspective. Avoid the cost of calling
302 * fscrypt_d_revalidate unnecessarily.
303 */
304 spin_lock(&dentry->d_lock);
305 dentry->d_flags &= ~DCACHE_OP_REVALIDATE;
306 spin_unlock(&dentry->d_lock);
307 }
308 }
309
310 /* crypto.c */
311 void fscrypt_enqueue_decrypt_work(struct work_struct *);
312
313 struct page *fscrypt_encrypt_pagecache_blocks(struct folio *folio,
314 size_t len, size_t offs, gfp_t gfp_flags);
315 int fscrypt_encrypt_block_inplace(const struct inode *inode, struct page *page,
316 unsigned int len, unsigned int offs,
317 u64 lblk_num);
318
319 int fscrypt_decrypt_pagecache_blocks(struct folio *folio, size_t len,
320 size_t offs);
321 int fscrypt_decrypt_block_inplace(const struct inode *inode, struct page *page,
322 unsigned int len, unsigned int offs,
323 u64 lblk_num);
324
fscrypt_is_bounce_page(struct page * page)325 static inline bool fscrypt_is_bounce_page(struct page *page)
326 {
327 return page->mapping == NULL;
328 }
329
fscrypt_pagecache_page(struct page * bounce_page)330 static inline struct page *fscrypt_pagecache_page(struct page *bounce_page)
331 {
332 return (struct page *)page_private(bounce_page);
333 }
334
fscrypt_is_bounce_folio(struct folio * folio)335 static inline bool fscrypt_is_bounce_folio(struct folio *folio)
336 {
337 return folio->mapping == NULL;
338 }
339
fscrypt_pagecache_folio(struct folio * bounce_folio)340 static inline struct folio *fscrypt_pagecache_folio(struct folio *bounce_folio)
341 {
342 return bounce_folio->private;
343 }
344
345 void fscrypt_free_bounce_page(struct page *bounce_page);
346
347 /* policy.c */
348 int fscrypt_ioctl_set_policy(struct file *filp, const void __user *arg);
349 int fscrypt_ioctl_get_policy(struct file *filp, void __user *arg);
350 int fscrypt_ioctl_get_policy_ex(struct file *filp, void __user *arg);
351 int fscrypt_ioctl_get_nonce(struct file *filp, void __user *arg);
352 int fscrypt_has_permitted_context(struct inode *parent, struct inode *child);
353 int fscrypt_context_for_new_inode(void *ctx, struct inode *inode);
354 int fscrypt_set_context(struct inode *inode, void *fs_data);
355
356 struct fscrypt_dummy_policy {
357 const union fscrypt_policy *policy;
358 };
359
360 int fscrypt_parse_test_dummy_encryption(const struct fs_parameter *param,
361 struct fscrypt_dummy_policy *dummy_policy);
362 bool fscrypt_dummy_policies_equal(const struct fscrypt_dummy_policy *p1,
363 const struct fscrypt_dummy_policy *p2);
364 void fscrypt_show_test_dummy_encryption(struct seq_file *seq, char sep,
365 struct super_block *sb);
366 static inline bool
fscrypt_is_dummy_policy_set(const struct fscrypt_dummy_policy * dummy_policy)367 fscrypt_is_dummy_policy_set(const struct fscrypt_dummy_policy *dummy_policy)
368 {
369 return dummy_policy->policy != NULL;
370 }
371 static inline void
fscrypt_free_dummy_policy(struct fscrypt_dummy_policy * dummy_policy)372 fscrypt_free_dummy_policy(struct fscrypt_dummy_policy *dummy_policy)
373 {
374 kfree(dummy_policy->policy);
375 dummy_policy->policy = NULL;
376 }
377
378 /* keyring.c */
379 void fscrypt_destroy_keyring(struct super_block *sb);
380 int fscrypt_ioctl_add_key(struct file *filp, void __user *arg);
381 int fscrypt_ioctl_remove_key(struct file *filp, void __user *arg);
382 int fscrypt_ioctl_remove_key_all_users(struct file *filp, void __user *arg);
383 int fscrypt_ioctl_get_key_status(struct file *filp, void __user *arg);
384
385 /* keysetup.c */
386 int fscrypt_prepare_new_inode(struct inode *dir, struct inode *inode,
387 bool *encrypt_ret);
388 void fscrypt_put_encryption_info(struct inode *inode);
389 void fscrypt_free_inode(struct inode *inode);
390 int fscrypt_drop_inode(struct inode *inode);
391
392 /* fname.c */
393 int fscrypt_fname_encrypt(const struct inode *inode, const struct qstr *iname,
394 u8 *out, unsigned int olen);
395 bool fscrypt_fname_encrypted_size(const struct inode *inode, u32 orig_len,
396 u32 max_len, u32 *encrypted_len_ret);
397 int fscrypt_setup_filename(struct inode *inode, const struct qstr *iname,
398 int lookup, struct fscrypt_name *fname);
399
fscrypt_free_filename(struct fscrypt_name * fname)400 static inline void fscrypt_free_filename(struct fscrypt_name *fname)
401 {
402 kfree(fname->crypto_buf.name);
403 }
404
405 int fscrypt_fname_alloc_buffer(u32 max_encrypted_len,
406 struct fscrypt_str *crypto_str);
407 void fscrypt_fname_free_buffer(struct fscrypt_str *crypto_str);
408 int fscrypt_fname_disk_to_usr(const struct inode *inode,
409 u32 hash, u32 minor_hash,
410 const struct fscrypt_str *iname,
411 struct fscrypt_str *oname);
412 bool fscrypt_match_name(const struct fscrypt_name *fname,
413 const u8 *de_name, u32 de_name_len);
414 u64 fscrypt_fname_siphash(const struct inode *dir, const struct qstr *name);
415
416 /* bio.c */
417 bool fscrypt_decrypt_bio(struct bio *bio);
418 int fscrypt_zeroout_range(const struct inode *inode, pgoff_t lblk,
419 sector_t pblk, unsigned int len);
420
421 /* hooks.c */
422 int fscrypt_file_open(struct inode *inode, struct file *filp);
423 int __fscrypt_prepare_link(struct inode *inode, struct inode *dir,
424 struct dentry *dentry);
425 int __fscrypt_prepare_rename(struct inode *old_dir, struct dentry *old_dentry,
426 struct inode *new_dir, struct dentry *new_dentry,
427 unsigned int flags);
428 int __fscrypt_prepare_lookup(struct inode *dir, struct dentry *dentry,
429 struct fscrypt_name *fname);
430 int fscrypt_prepare_lookup_partial(struct inode *dir, struct dentry *dentry);
431 int __fscrypt_prepare_readdir(struct inode *dir);
432 int __fscrypt_prepare_setattr(struct dentry *dentry, struct iattr *attr);
433 int fscrypt_prepare_setflags(struct inode *inode,
434 unsigned int oldflags, unsigned int flags);
435 int fscrypt_prepare_symlink(struct inode *dir, const char *target,
436 unsigned int len, unsigned int max_len,
437 struct fscrypt_str *disk_link);
438 int __fscrypt_encrypt_symlink(struct inode *inode, const char *target,
439 unsigned int len, struct fscrypt_str *disk_link);
440 const char *fscrypt_get_symlink(struct inode *inode, const void *caddr,
441 unsigned int max_size,
442 struct delayed_call *done);
443 int fscrypt_symlink_getattr(const struct path *path, struct kstat *stat);
fscrypt_set_ops(struct super_block * sb,const struct fscrypt_operations * s_cop)444 static inline void fscrypt_set_ops(struct super_block *sb,
445 const struct fscrypt_operations *s_cop)
446 {
447 sb->s_cop = s_cop;
448 }
449 #else /* !CONFIG_FS_ENCRYPTION */
450
451 static inline struct fscrypt_inode_info *
fscrypt_get_inode_info(const struct inode * inode)452 fscrypt_get_inode_info(const struct inode *inode)
453 {
454 return NULL;
455 }
456
fscrypt_needs_contents_encryption(const struct inode * inode)457 static inline bool fscrypt_needs_contents_encryption(const struct inode *inode)
458 {
459 return false;
460 }
461
fscrypt_handle_d_move(struct dentry * dentry)462 static inline void fscrypt_handle_d_move(struct dentry *dentry)
463 {
464 }
465
fscrypt_is_nokey_name(const struct dentry * dentry)466 static inline bool fscrypt_is_nokey_name(const struct dentry *dentry)
467 {
468 return false;
469 }
470
fscrypt_prepare_dentry(struct dentry * dentry,bool is_nokey_name)471 static inline void fscrypt_prepare_dentry(struct dentry *dentry,
472 bool is_nokey_name)
473 {
474 }
475
476 /* crypto.c */
fscrypt_enqueue_decrypt_work(struct work_struct * work)477 static inline void fscrypt_enqueue_decrypt_work(struct work_struct *work)
478 {
479 }
480
fscrypt_encrypt_pagecache_blocks(struct folio * folio,size_t len,size_t offs,gfp_t gfp_flags)481 static inline struct page *fscrypt_encrypt_pagecache_blocks(struct folio *folio,
482 size_t len, size_t offs, gfp_t gfp_flags)
483 {
484 return ERR_PTR(-EOPNOTSUPP);
485 }
486
fscrypt_encrypt_block_inplace(const struct inode * inode,struct page * page,unsigned int len,unsigned int offs,u64 lblk_num)487 static inline int fscrypt_encrypt_block_inplace(const struct inode *inode,
488 struct page *page,
489 unsigned int len,
490 unsigned int offs, u64 lblk_num)
491 {
492 return -EOPNOTSUPP;
493 }
494
fscrypt_decrypt_pagecache_blocks(struct folio * folio,size_t len,size_t offs)495 static inline int fscrypt_decrypt_pagecache_blocks(struct folio *folio,
496 size_t len, size_t offs)
497 {
498 return -EOPNOTSUPP;
499 }
500
fscrypt_decrypt_block_inplace(const struct inode * inode,struct page * page,unsigned int len,unsigned int offs,u64 lblk_num)501 static inline int fscrypt_decrypt_block_inplace(const struct inode *inode,
502 struct page *page,
503 unsigned int len,
504 unsigned int offs, u64 lblk_num)
505 {
506 return -EOPNOTSUPP;
507 }
508
fscrypt_is_bounce_page(struct page * page)509 static inline bool fscrypt_is_bounce_page(struct page *page)
510 {
511 return false;
512 }
513
fscrypt_pagecache_page(struct page * bounce_page)514 static inline struct page *fscrypt_pagecache_page(struct page *bounce_page)
515 {
516 WARN_ON_ONCE(1);
517 return ERR_PTR(-EINVAL);
518 }
519
fscrypt_is_bounce_folio(struct folio * folio)520 static inline bool fscrypt_is_bounce_folio(struct folio *folio)
521 {
522 return false;
523 }
524
fscrypt_pagecache_folio(struct folio * bounce_folio)525 static inline struct folio *fscrypt_pagecache_folio(struct folio *bounce_folio)
526 {
527 WARN_ON_ONCE(1);
528 return ERR_PTR(-EINVAL);
529 }
530
fscrypt_free_bounce_page(struct page * bounce_page)531 static inline void fscrypt_free_bounce_page(struct page *bounce_page)
532 {
533 }
534
535 /* policy.c */
fscrypt_ioctl_set_policy(struct file * filp,const void __user * arg)536 static inline int fscrypt_ioctl_set_policy(struct file *filp,
537 const void __user *arg)
538 {
539 return -EOPNOTSUPP;
540 }
541
fscrypt_ioctl_get_policy(struct file * filp,void __user * arg)542 static inline int fscrypt_ioctl_get_policy(struct file *filp, void __user *arg)
543 {
544 return -EOPNOTSUPP;
545 }
546
fscrypt_ioctl_get_policy_ex(struct file * filp,void __user * arg)547 static inline int fscrypt_ioctl_get_policy_ex(struct file *filp,
548 void __user *arg)
549 {
550 return -EOPNOTSUPP;
551 }
552
fscrypt_ioctl_get_nonce(struct file * filp,void __user * arg)553 static inline int fscrypt_ioctl_get_nonce(struct file *filp, void __user *arg)
554 {
555 return -EOPNOTSUPP;
556 }
557
fscrypt_has_permitted_context(struct inode * parent,struct inode * child)558 static inline int fscrypt_has_permitted_context(struct inode *parent,
559 struct inode *child)
560 {
561 return 0;
562 }
563
fscrypt_set_context(struct inode * inode,void * fs_data)564 static inline int fscrypt_set_context(struct inode *inode, void *fs_data)
565 {
566 return -EOPNOTSUPP;
567 }
568
569 struct fscrypt_dummy_policy {
570 };
571
572 static inline int
fscrypt_parse_test_dummy_encryption(const struct fs_parameter * param,struct fscrypt_dummy_policy * dummy_policy)573 fscrypt_parse_test_dummy_encryption(const struct fs_parameter *param,
574 struct fscrypt_dummy_policy *dummy_policy)
575 {
576 return -EINVAL;
577 }
578
579 static inline bool
fscrypt_dummy_policies_equal(const struct fscrypt_dummy_policy * p1,const struct fscrypt_dummy_policy * p2)580 fscrypt_dummy_policies_equal(const struct fscrypt_dummy_policy *p1,
581 const struct fscrypt_dummy_policy *p2)
582 {
583 return true;
584 }
585
fscrypt_show_test_dummy_encryption(struct seq_file * seq,char sep,struct super_block * sb)586 static inline void fscrypt_show_test_dummy_encryption(struct seq_file *seq,
587 char sep,
588 struct super_block *sb)
589 {
590 }
591
592 static inline bool
fscrypt_is_dummy_policy_set(const struct fscrypt_dummy_policy * dummy_policy)593 fscrypt_is_dummy_policy_set(const struct fscrypt_dummy_policy *dummy_policy)
594 {
595 return false;
596 }
597
598 static inline void
fscrypt_free_dummy_policy(struct fscrypt_dummy_policy * dummy_policy)599 fscrypt_free_dummy_policy(struct fscrypt_dummy_policy *dummy_policy)
600 {
601 }
602
603 /* keyring.c */
fscrypt_destroy_keyring(struct super_block * sb)604 static inline void fscrypt_destroy_keyring(struct super_block *sb)
605 {
606 }
607
fscrypt_ioctl_add_key(struct file * filp,void __user * arg)608 static inline int fscrypt_ioctl_add_key(struct file *filp, void __user *arg)
609 {
610 return -EOPNOTSUPP;
611 }
612
fscrypt_ioctl_remove_key(struct file * filp,void __user * arg)613 static inline int fscrypt_ioctl_remove_key(struct file *filp, void __user *arg)
614 {
615 return -EOPNOTSUPP;
616 }
617
fscrypt_ioctl_remove_key_all_users(struct file * filp,void __user * arg)618 static inline int fscrypt_ioctl_remove_key_all_users(struct file *filp,
619 void __user *arg)
620 {
621 return -EOPNOTSUPP;
622 }
623
fscrypt_ioctl_get_key_status(struct file * filp,void __user * arg)624 static inline int fscrypt_ioctl_get_key_status(struct file *filp,
625 void __user *arg)
626 {
627 return -EOPNOTSUPP;
628 }
629
630 /* keysetup.c */
631
fscrypt_prepare_new_inode(struct inode * dir,struct inode * inode,bool * encrypt_ret)632 static inline int fscrypt_prepare_new_inode(struct inode *dir,
633 struct inode *inode,
634 bool *encrypt_ret)
635 {
636 if (IS_ENCRYPTED(dir))
637 return -EOPNOTSUPP;
638 return 0;
639 }
640
fscrypt_put_encryption_info(struct inode * inode)641 static inline void fscrypt_put_encryption_info(struct inode *inode)
642 {
643 return;
644 }
645
fscrypt_free_inode(struct inode * inode)646 static inline void fscrypt_free_inode(struct inode *inode)
647 {
648 }
649
fscrypt_drop_inode(struct inode * inode)650 static inline int fscrypt_drop_inode(struct inode *inode)
651 {
652 return 0;
653 }
654
655 /* fname.c */
fscrypt_setup_filename(struct inode * dir,const struct qstr * iname,int lookup,struct fscrypt_name * fname)656 static inline int fscrypt_setup_filename(struct inode *dir,
657 const struct qstr *iname,
658 int lookup, struct fscrypt_name *fname)
659 {
660 if (IS_ENCRYPTED(dir))
661 return -EOPNOTSUPP;
662
663 memset(fname, 0, sizeof(*fname));
664 fname->usr_fname = iname;
665 fname->disk_name.name = (unsigned char *)iname->name;
666 fname->disk_name.len = iname->len;
667 return 0;
668 }
669
fscrypt_free_filename(struct fscrypt_name * fname)670 static inline void fscrypt_free_filename(struct fscrypt_name *fname)
671 {
672 return;
673 }
674
fscrypt_fname_alloc_buffer(u32 max_encrypted_len,struct fscrypt_str * crypto_str)675 static inline int fscrypt_fname_alloc_buffer(u32 max_encrypted_len,
676 struct fscrypt_str *crypto_str)
677 {
678 return -EOPNOTSUPP;
679 }
680
fscrypt_fname_free_buffer(struct fscrypt_str * crypto_str)681 static inline void fscrypt_fname_free_buffer(struct fscrypt_str *crypto_str)
682 {
683 return;
684 }
685
fscrypt_fname_disk_to_usr(const struct inode * inode,u32 hash,u32 minor_hash,const struct fscrypt_str * iname,struct fscrypt_str * oname)686 static inline int fscrypt_fname_disk_to_usr(const struct inode *inode,
687 u32 hash, u32 minor_hash,
688 const struct fscrypt_str *iname,
689 struct fscrypt_str *oname)
690 {
691 return -EOPNOTSUPP;
692 }
693
fscrypt_match_name(const struct fscrypt_name * fname,const u8 * de_name,u32 de_name_len)694 static inline bool fscrypt_match_name(const struct fscrypt_name *fname,
695 const u8 *de_name, u32 de_name_len)
696 {
697 /* Encryption support disabled; use standard comparison */
698 if (de_name_len != fname->disk_name.len)
699 return false;
700 return !memcmp(de_name, fname->disk_name.name, fname->disk_name.len);
701 }
702
fscrypt_fname_siphash(const struct inode * dir,const struct qstr * name)703 static inline u64 fscrypt_fname_siphash(const struct inode *dir,
704 const struct qstr *name)
705 {
706 WARN_ON_ONCE(1);
707 return 0;
708 }
709
fscrypt_d_revalidate(struct inode * dir,const struct qstr * name,struct dentry * dentry,unsigned int flags)710 static inline int fscrypt_d_revalidate(struct inode *dir, const struct qstr *name,
711 struct dentry *dentry, unsigned int flags)
712 {
713 return 1;
714 }
715
716 /* bio.c */
fscrypt_decrypt_bio(struct bio * bio)717 static inline bool fscrypt_decrypt_bio(struct bio *bio)
718 {
719 return true;
720 }
721
fscrypt_zeroout_range(const struct inode * inode,pgoff_t lblk,sector_t pblk,unsigned int len)722 static inline int fscrypt_zeroout_range(const struct inode *inode, pgoff_t lblk,
723 sector_t pblk, unsigned int len)
724 {
725 return -EOPNOTSUPP;
726 }
727
728 /* hooks.c */
729
fscrypt_file_open(struct inode * inode,struct file * filp)730 static inline int fscrypt_file_open(struct inode *inode, struct file *filp)
731 {
732 if (IS_ENCRYPTED(inode))
733 return -EOPNOTSUPP;
734 return 0;
735 }
736
__fscrypt_prepare_link(struct inode * inode,struct inode * dir,struct dentry * dentry)737 static inline int __fscrypt_prepare_link(struct inode *inode, struct inode *dir,
738 struct dentry *dentry)
739 {
740 return -EOPNOTSUPP;
741 }
742
__fscrypt_prepare_rename(struct inode * old_dir,struct dentry * old_dentry,struct inode * new_dir,struct dentry * new_dentry,unsigned int flags)743 static inline int __fscrypt_prepare_rename(struct inode *old_dir,
744 struct dentry *old_dentry,
745 struct inode *new_dir,
746 struct dentry *new_dentry,
747 unsigned int flags)
748 {
749 return -EOPNOTSUPP;
750 }
751
__fscrypt_prepare_lookup(struct inode * dir,struct dentry * dentry,struct fscrypt_name * fname)752 static inline int __fscrypt_prepare_lookup(struct inode *dir,
753 struct dentry *dentry,
754 struct fscrypt_name *fname)
755 {
756 return -EOPNOTSUPP;
757 }
758
fscrypt_prepare_lookup_partial(struct inode * dir,struct dentry * dentry)759 static inline int fscrypt_prepare_lookup_partial(struct inode *dir,
760 struct dentry *dentry)
761 {
762 return -EOPNOTSUPP;
763 }
764
__fscrypt_prepare_readdir(struct inode * dir)765 static inline int __fscrypt_prepare_readdir(struct inode *dir)
766 {
767 return -EOPNOTSUPP;
768 }
769
__fscrypt_prepare_setattr(struct dentry * dentry,struct iattr * attr)770 static inline int __fscrypt_prepare_setattr(struct dentry *dentry,
771 struct iattr *attr)
772 {
773 return -EOPNOTSUPP;
774 }
775
fscrypt_prepare_setflags(struct inode * inode,unsigned int oldflags,unsigned int flags)776 static inline int fscrypt_prepare_setflags(struct inode *inode,
777 unsigned int oldflags,
778 unsigned int flags)
779 {
780 return 0;
781 }
782
fscrypt_prepare_symlink(struct inode * dir,const char * target,unsigned int len,unsigned int max_len,struct fscrypt_str * disk_link)783 static inline int fscrypt_prepare_symlink(struct inode *dir,
784 const char *target,
785 unsigned int len,
786 unsigned int max_len,
787 struct fscrypt_str *disk_link)
788 {
789 if (IS_ENCRYPTED(dir))
790 return -EOPNOTSUPP;
791 disk_link->name = (unsigned char *)target;
792 disk_link->len = len + 1;
793 if (disk_link->len > max_len)
794 return -ENAMETOOLONG;
795 return 0;
796 }
797
__fscrypt_encrypt_symlink(struct inode * inode,const char * target,unsigned int len,struct fscrypt_str * disk_link)798 static inline int __fscrypt_encrypt_symlink(struct inode *inode,
799 const char *target,
800 unsigned int len,
801 struct fscrypt_str *disk_link)
802 {
803 return -EOPNOTSUPP;
804 }
805
fscrypt_get_symlink(struct inode * inode,const void * caddr,unsigned int max_size,struct delayed_call * done)806 static inline const char *fscrypt_get_symlink(struct inode *inode,
807 const void *caddr,
808 unsigned int max_size,
809 struct delayed_call *done)
810 {
811 return ERR_PTR(-EOPNOTSUPP);
812 }
813
fscrypt_symlink_getattr(const struct path * path,struct kstat * stat)814 static inline int fscrypt_symlink_getattr(const struct path *path,
815 struct kstat *stat)
816 {
817 return -EOPNOTSUPP;
818 }
819
fscrypt_set_ops(struct super_block * sb,const struct fscrypt_operations * s_cop)820 static inline void fscrypt_set_ops(struct super_block *sb,
821 const struct fscrypt_operations *s_cop)
822 {
823 }
824
825 #endif /* !CONFIG_FS_ENCRYPTION */
826
827 /* inline_crypt.c */
828 #ifdef CONFIG_FS_ENCRYPTION_INLINE_CRYPT
829
830 bool __fscrypt_inode_uses_inline_crypto(const struct inode *inode);
831
832 void fscrypt_set_bio_crypt_ctx(struct bio *bio,
833 const struct inode *inode, u64 first_lblk,
834 gfp_t gfp_mask);
835
836 void fscrypt_set_bio_crypt_ctx_bh(struct bio *bio,
837 const struct buffer_head *first_bh,
838 gfp_t gfp_mask);
839
840 bool fscrypt_mergeable_bio(struct bio *bio, const struct inode *inode,
841 u64 next_lblk);
842
843 bool fscrypt_mergeable_bio_bh(struct bio *bio,
844 const struct buffer_head *next_bh);
845
846 bool fscrypt_dio_supported(struct inode *inode);
847
848 u64 fscrypt_limit_io_blocks(const struct inode *inode, u64 lblk, u64 nr_blocks);
849
850 #else /* CONFIG_FS_ENCRYPTION_INLINE_CRYPT */
851
__fscrypt_inode_uses_inline_crypto(const struct inode * inode)852 static inline bool __fscrypt_inode_uses_inline_crypto(const struct inode *inode)
853 {
854 return false;
855 }
856
fscrypt_set_bio_crypt_ctx(struct bio * bio,const struct inode * inode,u64 first_lblk,gfp_t gfp_mask)857 static inline void fscrypt_set_bio_crypt_ctx(struct bio *bio,
858 const struct inode *inode,
859 u64 first_lblk, gfp_t gfp_mask) { }
860
fscrypt_set_bio_crypt_ctx_bh(struct bio * bio,const struct buffer_head * first_bh,gfp_t gfp_mask)861 static inline void fscrypt_set_bio_crypt_ctx_bh(
862 struct bio *bio,
863 const struct buffer_head *first_bh,
864 gfp_t gfp_mask) { }
865
fscrypt_mergeable_bio(struct bio * bio,const struct inode * inode,u64 next_lblk)866 static inline bool fscrypt_mergeable_bio(struct bio *bio,
867 const struct inode *inode,
868 u64 next_lblk)
869 {
870 return true;
871 }
872
fscrypt_mergeable_bio_bh(struct bio * bio,const struct buffer_head * next_bh)873 static inline bool fscrypt_mergeable_bio_bh(struct bio *bio,
874 const struct buffer_head *next_bh)
875 {
876 return true;
877 }
878
fscrypt_dio_supported(struct inode * inode)879 static inline bool fscrypt_dio_supported(struct inode *inode)
880 {
881 return !fscrypt_needs_contents_encryption(inode);
882 }
883
fscrypt_limit_io_blocks(const struct inode * inode,u64 lblk,u64 nr_blocks)884 static inline u64 fscrypt_limit_io_blocks(const struct inode *inode, u64 lblk,
885 u64 nr_blocks)
886 {
887 return nr_blocks;
888 }
889 #endif /* !CONFIG_FS_ENCRYPTION_INLINE_CRYPT */
890
891 /**
892 * fscrypt_inode_uses_inline_crypto() - test whether an inode uses inline
893 * encryption
894 * @inode: an inode. If encrypted, its key must be set up.
895 *
896 * Return: true if the inode requires file contents encryption and if the
897 * encryption should be done in the block layer via blk-crypto rather
898 * than in the filesystem layer.
899 */
fscrypt_inode_uses_inline_crypto(const struct inode * inode)900 static inline bool fscrypt_inode_uses_inline_crypto(const struct inode *inode)
901 {
902 return fscrypt_needs_contents_encryption(inode) &&
903 __fscrypt_inode_uses_inline_crypto(inode);
904 }
905
906 /**
907 * fscrypt_inode_uses_fs_layer_crypto() - test whether an inode uses fs-layer
908 * encryption
909 * @inode: an inode. If encrypted, its key must be set up.
910 *
911 * Return: true if the inode requires file contents encryption and if the
912 * encryption should be done in the filesystem layer rather than in the
913 * block layer via blk-crypto.
914 */
fscrypt_inode_uses_fs_layer_crypto(const struct inode * inode)915 static inline bool fscrypt_inode_uses_fs_layer_crypto(const struct inode *inode)
916 {
917 return fscrypt_needs_contents_encryption(inode) &&
918 !__fscrypt_inode_uses_inline_crypto(inode);
919 }
920
921 /**
922 * fscrypt_has_encryption_key() - check whether an inode has had its key set up
923 * @inode: the inode to check
924 *
925 * Return: %true if the inode has had its encryption key set up, else %false.
926 *
927 * Usually this should be preceded by fscrypt_get_encryption_info() to try to
928 * set up the key first.
929 */
fscrypt_has_encryption_key(const struct inode * inode)930 static inline bool fscrypt_has_encryption_key(const struct inode *inode)
931 {
932 return fscrypt_get_inode_info(inode) != NULL;
933 }
934
935 /**
936 * fscrypt_prepare_link() - prepare to link an inode into a possibly-encrypted
937 * directory
938 * @old_dentry: an existing dentry for the inode being linked
939 * @dir: the target directory
940 * @dentry: negative dentry for the target filename
941 *
942 * A new link can only be added to an encrypted directory if the directory's
943 * encryption key is available --- since otherwise we'd have no way to encrypt
944 * the filename.
945 *
946 * We also verify that the link will not violate the constraint that all files
947 * in an encrypted directory tree use the same encryption policy.
948 *
949 * Return: 0 on success, -ENOKEY if the directory's encryption key is missing,
950 * -EXDEV if the link would result in an inconsistent encryption policy, or
951 * another -errno code.
952 */
fscrypt_prepare_link(struct dentry * old_dentry,struct inode * dir,struct dentry * dentry)953 static inline int fscrypt_prepare_link(struct dentry *old_dentry,
954 struct inode *dir,
955 struct dentry *dentry)
956 {
957 if (IS_ENCRYPTED(dir))
958 return __fscrypt_prepare_link(d_inode(old_dentry), dir, dentry);
959 return 0;
960 }
961
962 /**
963 * fscrypt_prepare_rename() - prepare for a rename between possibly-encrypted
964 * directories
965 * @old_dir: source directory
966 * @old_dentry: dentry for source file
967 * @new_dir: target directory
968 * @new_dentry: dentry for target location (may be negative unless exchanging)
969 * @flags: rename flags (we care at least about %RENAME_EXCHANGE)
970 *
971 * Prepare for ->rename() where the source and/or target directories may be
972 * encrypted. A new link can only be added to an encrypted directory if the
973 * directory's encryption key is available --- since otherwise we'd have no way
974 * to encrypt the filename. A rename to an existing name, on the other hand,
975 * *is* cryptographically possible without the key. However, we take the more
976 * conservative approach and just forbid all no-key renames.
977 *
978 * We also verify that the rename will not violate the constraint that all files
979 * in an encrypted directory tree use the same encryption policy.
980 *
981 * Return: 0 on success, -ENOKEY if an encryption key is missing, -EXDEV if the
982 * rename would cause inconsistent encryption policies, or another -errno code.
983 */
fscrypt_prepare_rename(struct inode * old_dir,struct dentry * old_dentry,struct inode * new_dir,struct dentry * new_dentry,unsigned int flags)984 static inline int fscrypt_prepare_rename(struct inode *old_dir,
985 struct dentry *old_dentry,
986 struct inode *new_dir,
987 struct dentry *new_dentry,
988 unsigned int flags)
989 {
990 if (IS_ENCRYPTED(old_dir) || IS_ENCRYPTED(new_dir))
991 return __fscrypt_prepare_rename(old_dir, old_dentry,
992 new_dir, new_dentry, flags);
993 return 0;
994 }
995
996 /**
997 * fscrypt_prepare_lookup() - prepare to lookup a name in a possibly-encrypted
998 * directory
999 * @dir: directory being searched
1000 * @dentry: filename being looked up
1001 * @fname: (output) the name to use to search the on-disk directory
1002 *
1003 * Prepare for ->lookup() in a directory which may be encrypted by determining
1004 * the name that will actually be used to search the directory on-disk. If the
1005 * directory's encryption policy is supported by this kernel and its encryption
1006 * key is available, then the lookup is assumed to be by plaintext name;
1007 * otherwise, it is assumed to be by no-key name.
1008 *
1009 * This will set DCACHE_NOKEY_NAME on the dentry if the lookup is by no-key
1010 * name. In this case the filesystem must assign the dentry a dentry_operations
1011 * which contains fscrypt_d_revalidate (or contains a d_revalidate method that
1012 * calls fscrypt_d_revalidate), so that the dentry will be invalidated if the
1013 * directory's encryption key is later added.
1014 *
1015 * Return: 0 on success; -ENOENT if the directory's key is unavailable but the
1016 * filename isn't a valid no-key name, so a negative dentry should be created;
1017 * or another -errno code.
1018 */
fscrypt_prepare_lookup(struct inode * dir,struct dentry * dentry,struct fscrypt_name * fname)1019 static inline int fscrypt_prepare_lookup(struct inode *dir,
1020 struct dentry *dentry,
1021 struct fscrypt_name *fname)
1022 {
1023 if (IS_ENCRYPTED(dir))
1024 return __fscrypt_prepare_lookup(dir, dentry, fname);
1025
1026 memset(fname, 0, sizeof(*fname));
1027 fname->usr_fname = &dentry->d_name;
1028 fname->disk_name.name = (unsigned char *)dentry->d_name.name;
1029 fname->disk_name.len = dentry->d_name.len;
1030
1031 fscrypt_prepare_dentry(dentry, false);
1032
1033 return 0;
1034 }
1035
1036 /**
1037 * fscrypt_prepare_readdir() - prepare to read a possibly-encrypted directory
1038 * @dir: the directory inode
1039 *
1040 * If the directory is encrypted and it doesn't already have its encryption key
1041 * set up, try to set it up so that the filenames will be listed in plaintext
1042 * form rather than in no-key form.
1043 *
1044 * Return: 0 on success; -errno on error. Note that the encryption key being
1045 * unavailable is not considered an error. It is also not an error if
1046 * the encryption policy is unsupported by this kernel; that is treated
1047 * like the key being unavailable, so that files can still be deleted.
1048 */
fscrypt_prepare_readdir(struct inode * dir)1049 static inline int fscrypt_prepare_readdir(struct inode *dir)
1050 {
1051 if (IS_ENCRYPTED(dir))
1052 return __fscrypt_prepare_readdir(dir);
1053 return 0;
1054 }
1055
1056 /**
1057 * fscrypt_prepare_setattr() - prepare to change a possibly-encrypted inode's
1058 * attributes
1059 * @dentry: dentry through which the inode is being changed
1060 * @attr: attributes to change
1061 *
1062 * Prepare for ->setattr() on a possibly-encrypted inode. On an encrypted file,
1063 * most attribute changes are allowed even without the encryption key. However,
1064 * without the encryption key we do have to forbid truncates. This is needed
1065 * because the size being truncated to may not be a multiple of the filesystem
1066 * block size, and in that case we'd have to decrypt the final block, zero the
1067 * portion past i_size, and re-encrypt it. (We *could* allow truncating to a
1068 * filesystem block boundary, but it's simpler to just forbid all truncates ---
1069 * and we already forbid all other contents modifications without the key.)
1070 *
1071 * Return: 0 on success, -ENOKEY if the key is missing, or another -errno code
1072 * if a problem occurred while setting up the encryption key.
1073 */
fscrypt_prepare_setattr(struct dentry * dentry,struct iattr * attr)1074 static inline int fscrypt_prepare_setattr(struct dentry *dentry,
1075 struct iattr *attr)
1076 {
1077 if (IS_ENCRYPTED(d_inode(dentry)))
1078 return __fscrypt_prepare_setattr(dentry, attr);
1079 return 0;
1080 }
1081
1082 /**
1083 * fscrypt_encrypt_symlink() - encrypt the symlink target if needed
1084 * @inode: symlink inode
1085 * @target: plaintext symlink target
1086 * @len: length of @target excluding null terminator
1087 * @disk_link: (in/out) the on-disk symlink target being prepared
1088 *
1089 * If the symlink target needs to be encrypted, then this function encrypts it
1090 * into @disk_link->name. fscrypt_prepare_symlink() must have been called
1091 * previously to compute @disk_link->len. If the filesystem did not allocate a
1092 * buffer for @disk_link->name after calling fscrypt_prepare_link(), then one
1093 * will be kmalloc()'ed and the filesystem will be responsible for freeing it.
1094 *
1095 * Return: 0 on success, -errno on failure
1096 */
fscrypt_encrypt_symlink(struct inode * inode,const char * target,unsigned int len,struct fscrypt_str * disk_link)1097 static inline int fscrypt_encrypt_symlink(struct inode *inode,
1098 const char *target,
1099 unsigned int len,
1100 struct fscrypt_str *disk_link)
1101 {
1102 if (IS_ENCRYPTED(inode))
1103 return __fscrypt_encrypt_symlink(inode, target, len, disk_link);
1104 return 0;
1105 }
1106
1107 /* If *pagep is a bounce page, free it and set *pagep to the pagecache page */
fscrypt_finalize_bounce_page(struct page ** pagep)1108 static inline void fscrypt_finalize_bounce_page(struct page **pagep)
1109 {
1110 struct page *page = *pagep;
1111
1112 if (fscrypt_is_bounce_page(page)) {
1113 *pagep = fscrypt_pagecache_page(page);
1114 fscrypt_free_bounce_page(page);
1115 }
1116 }
1117
1118 #endif /* _LINUX_FSCRYPT_H */
1119