blob: 989b76e982a7afedea8d69948b328cd37aaa2c26 [file] [log] [blame]
Linus Torvalds32190f02017-11-14 11:35:15 -08001/* SPDX-License-Identifier: GPL-2.0 */
Eric Biggers46f47e42017-01-24 10:58:06 -08002/*
Dave Chinner734f0d22017-10-09 12:15:34 -07003 * fscrypt.h: declarations for per-file encryption
4 *
Chandan Rajendraad8ceb02018-12-12 15:20:12 +05305 * Filesystems that implement per-file encryption must include this header
6 * file.
Eric Biggers46f47e42017-01-24 10:58:06 -08007 *
8 * Copyright (C) 2015, Google, Inc.
9 *
10 * Written by Michael Halcrow, 2015.
11 * Modified by Jaegeuk Kim, 2015.
12 */
Dave Chinner734f0d22017-10-09 12:15:34 -070013#ifndef _LINUX_FSCRYPT_H
14#define _LINUX_FSCRYPT_H
Eric Biggers46f47e42017-01-24 10:58:06 -080015
Eric Biggers46f47e42017-01-24 10:58:06 -080016#include <linux/fs.h>
Chandan Rajendraad8ceb02018-12-12 15:20:12 +053017#include <linux/mm.h>
18#include <linux/slab.h>
Blagovest Kolenichev94699592020-03-27 03:19:47 -070019#include <uapi/linux/fscrypt.h>
Eric Biggers46f47e42017-01-24 10:58:06 -080020
21#define FS_CRYPTO_BLOCK_SIZE 16
22
23struct fscrypt_info;
24
Eric Biggers46f47e42017-01-24 10:58:06 -080025struct fscrypt_str {
26 unsigned char *name;
27 u32 len;
28};
29
30struct fscrypt_name {
31 const struct qstr *usr_fname;
32 struct fscrypt_str disk_name;
33 u32 hash;
34 u32 minor_hash;
35 struct fscrypt_str crypto_buf;
Eric Biggers716c3702019-03-20 11:39:13 -070036 bool is_ciphertext_name;
Eric Biggers46f47e42017-01-24 10:58:06 -080037};
38
39#define FSTR_INIT(n, l) { .name = n, .len = l }
40#define FSTR_TO_QSTR(f) QSTR_INIT((f)->name, (f)->len)
41#define fname_name(p) ((p)->disk_name.name)
42#define fname_len(p) ((p)->disk_name.len)
43
Tahsin Erdoganaf652072017-07-06 00:01:59 -040044/* Maximum value for the third parameter of fscrypt_operations.set_context(). */
Blagovest Kolenichev94699592020-03-27 03:19:47 -070045#define FSCRYPT_SET_CONTEXT_MAX_SIZE 40
Tahsin Erdoganaf652072017-07-06 00:01:59 -040046
Chandan Rajendraad8ceb02018-12-12 15:20:12 +053047#ifdef CONFIG_FS_ENCRYPTION
48/*
49 * fscrypt superblock flags
50 */
51#define FS_CFLG_OWN_PAGES (1U << 1)
52
53/*
54 * crypto operations for filesystems
55 */
56struct fscrypt_operations {
57 unsigned int flags;
58 const char *key_prefix;
59 int (*get_context)(struct inode *, void *, size_t);
60 int (*set_context)(struct inode *, const void *, size_t, void *);
61 bool (*dummy_context)(struct inode *);
62 bool (*empty_dir)(struct inode *);
63 unsigned int max_namelen;
Blagovest Kolenichev94699592020-03-27 03:19:47 -070064 bool (*has_stable_inodes)(struct super_block *sb);
65 void (*get_ino_and_lblk_bits)(struct super_block *sb,
66 int *ino_bits_ret, int *lblk_bits_ret);
67 bool (*inline_crypt_enabled)(struct super_block *sb);
Blagovest Kolenichev49875672020-04-20 07:29:59 -070068 int (*get_num_devices)(struct super_block *sb);
69 void (*get_devices)(struct super_block *sb,
70 struct request_queue **devs);
Chandan Rajendraad8ceb02018-12-12 15:20:12 +053071};
72
73static inline bool fscrypt_has_encryption_key(const struct inode *inode)
74{
Eric Biggers7a8861d2019-04-11 14:32:15 -070075 /* pairs with cmpxchg_release() in fscrypt_get_encryption_info() */
76 return READ_ONCE(inode->i_crypt_info) != NULL;
Chandan Rajendraad8ceb02018-12-12 15:20:12 +053077}
78
Eric Biggers3597e502019-12-09 12:50:21 -080079/**
80 * fscrypt_needs_contents_encryption() - check whether an inode needs
81 * contents encryption
82 *
83 * Return: %true iff the inode is an encrypted regular file and the kernel was
84 * built with fscrypt support.
85 *
86 * If you need to know whether the encrypt bit is set even when the kernel was
87 * built without fscrypt support, you must use IS_ENCRYPTED() directly instead.
88 */
89static inline bool fscrypt_needs_contents_encryption(const struct inode *inode)
90{
91 return IS_ENCRYPTED(inode) && S_ISREG(inode->i_mode);
92}
93
Chandan Rajendraad8ceb02018-12-12 15:20:12 +053094static inline bool fscrypt_dummy_context_enabled(struct inode *inode)
95{
96 return inode->i_sb->s_cop->dummy_context &&
97 inode->i_sb->s_cop->dummy_context(inode);
98}
99
Eric Biggersdd038b32019-03-20 11:39:11 -0700100/*
101 * When d_splice_alias() moves a directory's encrypted alias to its decrypted
102 * alias as a result of the encryption key being added, DCACHE_ENCRYPTED_NAME
103 * must be cleared. Note that we don't have to support arbitrary moves of this
104 * flag because fscrypt doesn't allow encrypted aliases to be the source or
105 * target of a rename().
106 */
107static inline void fscrypt_handle_d_move(struct dentry *dentry)
108{
109 dentry->d_flags &= ~DCACHE_ENCRYPTED_NAME;
110}
111
Chandan Rajendraad8ceb02018-12-12 15:20:12 +0530112/* crypto.c */
113extern void fscrypt_enqueue_decrypt_work(struct work_struct *);
Chandan Rajendraad8ceb02018-12-12 15:20:12 +0530114
Eric Biggersfd0e18f2019-05-20 09:29:44 -0700115extern struct page *fscrypt_encrypt_pagecache_blocks(struct page *page,
116 unsigned int len,
117 unsigned int offs,
118 gfp_t gfp_flags);
Eric Biggersce7b7a22019-05-20 09:29:43 -0700119extern int fscrypt_encrypt_block_inplace(const struct inode *inode,
120 struct page *page, unsigned int len,
121 unsigned int offs, u64 lblk_num,
122 gfp_t gfp_flags);
Eric Biggers87806292019-05-20 09:29:47 -0700123
124extern int fscrypt_decrypt_pagecache_blocks(struct page *page, unsigned int len,
125 unsigned int offs);
Eric Biggers45822362019-05-20 09:29:46 -0700126extern int fscrypt_decrypt_block_inplace(const struct inode *inode,
127 struct page *page, unsigned int len,
128 unsigned int offs, u64 lblk_num);
Chandan Rajendraad8ceb02018-12-12 15:20:12 +0530129
Eric Biggers2edab9e2019-05-20 09:29:39 -0700130static inline bool fscrypt_is_bounce_page(struct page *page)
Chandan Rajendraad8ceb02018-12-12 15:20:12 +0530131{
Eric Biggers2edab9e2019-05-20 09:29:39 -0700132 return page->mapping == NULL;
Chandan Rajendraad8ceb02018-12-12 15:20:12 +0530133}
134
Eric Biggers2edab9e2019-05-20 09:29:39 -0700135static inline struct page *fscrypt_pagecache_page(struct page *bounce_page)
136{
137 return (struct page *)page_private(bounce_page);
138}
139
140extern void fscrypt_free_bounce_page(struct page *bounce_page);
Daniel Rosenbergaf2b6ea2020-01-27 20:04:40 -0800141extern int fscrypt_d_revalidate(struct dentry *dentry, unsigned int flags);
Chandan Rajendraad8ceb02018-12-12 15:20:12 +0530142
143/* policy.c */
144extern int fscrypt_ioctl_set_policy(struct file *, const void __user *);
145extern int fscrypt_ioctl_get_policy(struct file *, void __user *);
Blagovest Kolenichev94699592020-03-27 03:19:47 -0700146extern int fscrypt_ioctl_get_policy_ex(struct file *, void __user *);
Chandan Rajendraad8ceb02018-12-12 15:20:12 +0530147extern int fscrypt_has_permitted_context(struct inode *, struct inode *);
148extern int fscrypt_inherit_context(struct inode *, struct inode *,
149 void *, bool);
Blagovest Kolenichev94699592020-03-27 03:19:47 -0700150/* keyring.c */
151extern void fscrypt_sb_free(struct super_block *sb);
152extern int fscrypt_ioctl_add_key(struct file *filp, void __user *arg);
153extern int fscrypt_ioctl_remove_key(struct file *filp, void __user *arg);
154extern int fscrypt_ioctl_remove_key_all_users(struct file *filp,
155 void __user *arg);
156extern int fscrypt_ioctl_get_key_status(struct file *filp, void __user *arg);
157extern int fscrypt_register_key_removal_notifier(struct notifier_block *nb);
158extern int fscrypt_unregister_key_removal_notifier(struct notifier_block *nb);
159
160/* keysetup.c */
Chandan Rajendraad8ceb02018-12-12 15:20:12 +0530161extern int fscrypt_get_encryption_info(struct inode *);
162extern void fscrypt_put_encryption_info(struct inode *);
Eric Biggers1f1be4a2019-04-10 13:21:15 -0700163extern void fscrypt_free_inode(struct inode *);
Blagovest Kolenichev94699592020-03-27 03:19:47 -0700164extern int fscrypt_drop_inode(struct inode *inode);
Chandan Rajendraad8ceb02018-12-12 15:20:12 +0530165
166/* fname.c */
167extern int fscrypt_setup_filename(struct inode *, const struct qstr *,
168 int lookup, struct fscrypt_name *);
169
170static inline void fscrypt_free_filename(struct fscrypt_name *fname)
171{
172 kfree(fname->crypto_buf.name);
173}
174
175extern int fscrypt_fname_alloc_buffer(const struct inode *, u32,
176 struct fscrypt_str *);
177extern void fscrypt_fname_free_buffer(struct fscrypt_str *);
Eric Biggersbac335a2019-12-15 13:39:47 -0800178extern int fscrypt_fname_disk_to_usr(const struct inode *inode,
179 u32 hash, u32 minor_hash,
180 const struct fscrypt_str *iname,
181 struct fscrypt_str *oname);
Daniel Rosenberg86eb43f2020-01-20 14:32:01 -0800182extern bool fscrypt_match_name(const struct fscrypt_name *fname,
183 const u8 *de_name, u32 de_name_len);
Daniel Rosenberg7495f912020-01-20 14:31:57 -0800184extern u64 fscrypt_fname_siphash(const struct inode *dir,
185 const struct qstr *name);
Chandan Rajendraad8ceb02018-12-12 15:20:12 +0530186
187/* bio.c */
188extern void fscrypt_decrypt_bio(struct bio *);
Chandan Rajendraad8ceb02018-12-12 15:20:12 +0530189extern int fscrypt_zeroout_range(const struct inode *, pgoff_t, sector_t,
190 unsigned int);
191
192/* hooks.c */
193extern int fscrypt_file_open(struct inode *inode, struct file *filp);
Eric Biggers081985b2019-03-20 11:39:10 -0700194extern int __fscrypt_prepare_link(struct inode *inode, struct inode *dir,
195 struct dentry *dentry);
Chandan Rajendraad8ceb02018-12-12 15:20:12 +0530196extern int __fscrypt_prepare_rename(struct inode *old_dir,
197 struct dentry *old_dentry,
198 struct inode *new_dir,
199 struct dentry *new_dentry,
200 unsigned int flags);
Eric Biggers716c3702019-03-20 11:39:13 -0700201extern int __fscrypt_prepare_lookup(struct inode *dir, struct dentry *dentry,
202 struct fscrypt_name *fname);
Daniel Rosenbergf4951342020-01-20 14:31:56 -0800203extern int fscrypt_prepare_setflags(struct inode *inode,
204 unsigned int oldflags, unsigned int flags);
Chandan Rajendraad8ceb02018-12-12 15:20:12 +0530205extern int __fscrypt_prepare_symlink(struct inode *dir, unsigned int len,
206 unsigned int max_len,
207 struct fscrypt_str *disk_link);
208extern int __fscrypt_encrypt_symlink(struct inode *inode, const char *target,
209 unsigned int len,
210 struct fscrypt_str *disk_link);
211extern const char *fscrypt_get_symlink(struct inode *inode, const void *caddr,
212 unsigned int max_size,
213 struct delayed_call *done);
214#else /* !CONFIG_FS_ENCRYPTION */
215
216static inline bool fscrypt_has_encryption_key(const struct inode *inode)
217{
218 return false;
219}
220
Eric Biggers3597e502019-12-09 12:50:21 -0800221static inline bool fscrypt_needs_contents_encryption(const struct inode *inode)
222{
223 return false;
224}
225
Chandan Rajendraad8ceb02018-12-12 15:20:12 +0530226static inline bool fscrypt_dummy_context_enabled(struct inode *inode)
227{
228 return false;
229}
230
Eric Biggersdd038b32019-03-20 11:39:11 -0700231static inline void fscrypt_handle_d_move(struct dentry *dentry)
232{
233}
234
Chandan Rajendraad8ceb02018-12-12 15:20:12 +0530235/* crypto.c */
236static inline void fscrypt_enqueue_decrypt_work(struct work_struct *work)
237{
238}
239
Eric Biggersfd0e18f2019-05-20 09:29:44 -0700240static inline struct page *fscrypt_encrypt_pagecache_blocks(struct page *page,
241 unsigned int len,
242 unsigned int offs,
243 gfp_t gfp_flags)
Chandan Rajendraad8ceb02018-12-12 15:20:12 +0530244{
245 return ERR_PTR(-EOPNOTSUPP);
246}
247
Eric Biggersce7b7a22019-05-20 09:29:43 -0700248static inline int fscrypt_encrypt_block_inplace(const struct inode *inode,
249 struct page *page,
250 unsigned int len,
251 unsigned int offs, u64 lblk_num,
252 gfp_t gfp_flags)
Chandan Rajendraad8ceb02018-12-12 15:20:12 +0530253{
254 return -EOPNOTSUPP;
255}
256
Eric Biggers87806292019-05-20 09:29:47 -0700257static inline int fscrypt_decrypt_pagecache_blocks(struct page *page,
258 unsigned int len,
259 unsigned int offs)
Chandan Rajendraad8ceb02018-12-12 15:20:12 +0530260{
261 return -EOPNOTSUPP;
262}
263
Eric Biggers45822362019-05-20 09:29:46 -0700264static inline int fscrypt_decrypt_block_inplace(const struct inode *inode,
265 struct page *page,
266 unsigned int len,
267 unsigned int offs, u64 lblk_num)
268{
269 return -EOPNOTSUPP;
270}
271
Eric Biggers2edab9e2019-05-20 09:29:39 -0700272static inline bool fscrypt_is_bounce_page(struct page *page)
273{
274 return false;
275}
276
277static inline struct page *fscrypt_pagecache_page(struct page *bounce_page)
Chandan Rajendraad8ceb02018-12-12 15:20:12 +0530278{
279 WARN_ON_ONCE(1);
280 return ERR_PTR(-EINVAL);
281}
282
Eric Biggers2edab9e2019-05-20 09:29:39 -0700283static inline void fscrypt_free_bounce_page(struct page *bounce_page)
Chandan Rajendraad8ceb02018-12-12 15:20:12 +0530284{
Chandan Rajendraad8ceb02018-12-12 15:20:12 +0530285}
286
287/* policy.c */
288static inline int fscrypt_ioctl_set_policy(struct file *filp,
289 const void __user *arg)
290{
291 return -EOPNOTSUPP;
292}
293
294static inline int fscrypt_ioctl_get_policy(struct file *filp, void __user *arg)
295{
296 return -EOPNOTSUPP;
297}
298
Blagovest Kolenichev94699592020-03-27 03:19:47 -0700299static inline int fscrypt_ioctl_get_policy_ex(struct file *filp,
300 void __user *arg)
301{
302 return -EOPNOTSUPP;
303}
304
Chandan Rajendraad8ceb02018-12-12 15:20:12 +0530305static inline int fscrypt_has_permitted_context(struct inode *parent,
306 struct inode *child)
307{
308 return 0;
309}
310
311static inline int fscrypt_inherit_context(struct inode *parent,
312 struct inode *child,
313 void *fs_data, bool preload)
314{
315 return -EOPNOTSUPP;
316}
317
Blagovest Kolenichev94699592020-03-27 03:19:47 -0700318/* keyring.c */
319static inline void fscrypt_sb_free(struct super_block *sb)
320{
321}
322
323static inline int fscrypt_ioctl_add_key(struct file *filp, void __user *arg)
324{
325 return -EOPNOTSUPP;
326}
327
328static inline int fscrypt_ioctl_remove_key(struct file *filp, void __user *arg)
329{
330 return -EOPNOTSUPP;
331}
332
333static inline int fscrypt_ioctl_remove_key_all_users(struct file *filp,
334 void __user *arg)
335{
336 return -EOPNOTSUPP;
337}
338
339static inline int fscrypt_ioctl_get_key_status(struct file *filp,
340 void __user *arg)
341{
342 return -EOPNOTSUPP;
343}
344
345static inline int fscrypt_register_key_removal_notifier(
346 struct notifier_block *nb)
347{
348 return 0;
349}
350
351static inline int fscrypt_unregister_key_removal_notifier(
352 struct notifier_block *nb)
353{
354 return 0;
355}
356
357/* keysetup.c */
Chandan Rajendraad8ceb02018-12-12 15:20:12 +0530358static inline int fscrypt_get_encryption_info(struct inode *inode)
359{
360 return -EOPNOTSUPP;
361}
362
363static inline void fscrypt_put_encryption_info(struct inode *inode)
364{
365 return;
366}
367
Eric Biggers1f1be4a2019-04-10 13:21:15 -0700368static inline void fscrypt_free_inode(struct inode *inode)
369{
370}
371
Blagovest Kolenichev94699592020-03-27 03:19:47 -0700372static inline int fscrypt_drop_inode(struct inode *inode)
373{
374 return 0;
375}
376
Chandan Rajendraad8ceb02018-12-12 15:20:12 +0530377 /* fname.c */
378static inline int fscrypt_setup_filename(struct inode *dir,
379 const struct qstr *iname,
380 int lookup, struct fscrypt_name *fname)
381{
382 if (IS_ENCRYPTED(dir))
383 return -EOPNOTSUPP;
384
Eric Biggers716c3702019-03-20 11:39:13 -0700385 memset(fname, 0, sizeof(*fname));
Chandan Rajendraad8ceb02018-12-12 15:20:12 +0530386 fname->usr_fname = iname;
387 fname->disk_name.name = (unsigned char *)iname->name;
388 fname->disk_name.len = iname->len;
389 return 0;
390}
391
392static inline void fscrypt_free_filename(struct fscrypt_name *fname)
393{
394 return;
395}
396
397static inline int fscrypt_fname_alloc_buffer(const struct inode *inode,
398 u32 max_encrypted_len,
399 struct fscrypt_str *crypto_str)
400{
401 return -EOPNOTSUPP;
402}
403
404static inline void fscrypt_fname_free_buffer(struct fscrypt_str *crypto_str)
405{
406 return;
407}
408
Eric Biggersbac335a2019-12-15 13:39:47 -0800409static inline int fscrypt_fname_disk_to_usr(const struct inode *inode,
Chandan Rajendraad8ceb02018-12-12 15:20:12 +0530410 u32 hash, u32 minor_hash,
411 const struct fscrypt_str *iname,
412 struct fscrypt_str *oname)
413{
414 return -EOPNOTSUPP;
415}
416
417static inline bool fscrypt_match_name(const struct fscrypt_name *fname,
418 const u8 *de_name, u32 de_name_len)
419{
420 /* Encryption support disabled; use standard comparison */
421 if (de_name_len != fname->disk_name.len)
422 return false;
423 return !memcmp(de_name, fname->disk_name.name, fname->disk_name.len);
424}
425
Daniel Rosenberg7495f912020-01-20 14:31:57 -0800426static inline u64 fscrypt_fname_siphash(const struct inode *dir,
427 const struct qstr *name)
428{
429 WARN_ON_ONCE(1);
430 return 0;
431}
432
Chandan Rajendraad8ceb02018-12-12 15:20:12 +0530433/* bio.c */
434static inline void fscrypt_decrypt_bio(struct bio *bio)
435{
436}
437
Chandan Rajendraad8ceb02018-12-12 15:20:12 +0530438static inline int fscrypt_zeroout_range(const struct inode *inode, pgoff_t lblk,
439 sector_t pblk, unsigned int len)
440{
441 return -EOPNOTSUPP;
442}
443
444/* hooks.c */
445
446static inline int fscrypt_file_open(struct inode *inode, struct file *filp)
447{
448 if (IS_ENCRYPTED(inode))
449 return -EOPNOTSUPP;
450 return 0;
451}
452
Eric Biggers081985b2019-03-20 11:39:10 -0700453static inline int __fscrypt_prepare_link(struct inode *inode, struct inode *dir,
454 struct dentry *dentry)
Chandan Rajendraad8ceb02018-12-12 15:20:12 +0530455{
456 return -EOPNOTSUPP;
457}
458
459static inline int __fscrypt_prepare_rename(struct inode *old_dir,
460 struct dentry *old_dentry,
461 struct inode *new_dir,
462 struct dentry *new_dentry,
463 unsigned int flags)
464{
465 return -EOPNOTSUPP;
466}
467
468static inline int __fscrypt_prepare_lookup(struct inode *dir,
Eric Biggers716c3702019-03-20 11:39:13 -0700469 struct dentry *dentry,
470 struct fscrypt_name *fname)
Chandan Rajendraad8ceb02018-12-12 15:20:12 +0530471{
472 return -EOPNOTSUPP;
473}
474
Daniel Rosenbergf4951342020-01-20 14:31:56 -0800475static inline int fscrypt_prepare_setflags(struct inode *inode,
476 unsigned int oldflags,
477 unsigned int flags)
478{
479 return 0;
480}
481
Chandan Rajendraad8ceb02018-12-12 15:20:12 +0530482static inline int __fscrypt_prepare_symlink(struct inode *dir,
483 unsigned int len,
484 unsigned int max_len,
485 struct fscrypt_str *disk_link)
486{
487 return -EOPNOTSUPP;
488}
489
490
491static inline int __fscrypt_encrypt_symlink(struct inode *inode,
492 const char *target,
493 unsigned int len,
494 struct fscrypt_str *disk_link)
495{
496 return -EOPNOTSUPP;
497}
498
499static inline const char *fscrypt_get_symlink(struct inode *inode,
500 const void *caddr,
501 unsigned int max_size,
502 struct delayed_call *done)
503{
504 return ERR_PTR(-EOPNOTSUPP);
505}
506#endif /* !CONFIG_FS_ENCRYPTION */
Dave Chinner734f0d22017-10-09 12:15:34 -0700507
Blagovest Kolenichev94699592020-03-27 03:19:47 -0700508/* inline_crypt.c */
509#ifdef CONFIG_FS_ENCRYPTION_INLINE_CRYPT
510extern bool fscrypt_inode_uses_inline_crypto(const struct inode *inode);
511
512extern bool fscrypt_inode_uses_fs_layer_crypto(const struct inode *inode);
513
Blagovest Kolenichev49875672020-04-20 07:29:59 -0700514extern void fscrypt_set_bio_crypt_ctx(struct bio *bio,
515 const struct inode *inode,
516 u64 first_lblk, gfp_t gfp_mask);
Blagovest Kolenichev94699592020-03-27 03:19:47 -0700517
Blagovest Kolenichev49875672020-04-20 07:29:59 -0700518extern void fscrypt_set_bio_crypt_ctx_bh(struct bio *bio,
519 const struct buffer_head *first_bh,
520 gfp_t gfp_mask);
Blagovest Kolenichev94699592020-03-27 03:19:47 -0700521
522extern bool fscrypt_mergeable_bio(struct bio *bio, const struct inode *inode,
523 u64 next_lblk);
524
525extern bool fscrypt_mergeable_bio_bh(struct bio *bio,
526 const struct buffer_head *next_bh);
527
528#else /* CONFIG_FS_ENCRYPTION_INLINE_CRYPT */
529static inline bool fscrypt_inode_uses_inline_crypto(const struct inode *inode)
530{
531 return false;
532}
533
534static inline bool fscrypt_inode_uses_fs_layer_crypto(const struct inode *inode)
535{
536 return IS_ENCRYPTED(inode) && S_ISREG(inode->i_mode);
537}
538
Blagovest Kolenichev49875672020-04-20 07:29:59 -0700539static inline void fscrypt_set_bio_crypt_ctx(struct bio *bio,
540 const struct inode *inode,
541 u64 first_lblk, gfp_t gfp_mask) { }
Blagovest Kolenichev94699592020-03-27 03:19:47 -0700542
Blagovest Kolenichev49875672020-04-20 07:29:59 -0700543static inline void fscrypt_set_bio_crypt_ctx_bh(
544 struct bio *bio,
545 const struct buffer_head *first_bh,
546 gfp_t gfp_mask) { }
Blagovest Kolenichev94699592020-03-27 03:19:47 -0700547
548static inline bool fscrypt_mergeable_bio(struct bio *bio,
549 const struct inode *inode,
550 u64 next_lblk)
551{
552 return true;
553}
554
555static inline bool fscrypt_mergeable_bio_bh(struct bio *bio,
556 const struct buffer_head *next_bh)
557{
558 return true;
559}
560#endif /* !CONFIG_FS_ENCRYPTION_INLINE_CRYPT */
561
Blagovest Kolenichev49875672020-04-20 07:29:59 -0700562#if IS_ENABLED(CONFIG_FS_ENCRYPTION) && IS_ENABLED(CONFIG_DM_DEFAULT_KEY)
563static inline bool
564fscrypt_inode_should_skip_dm_default_key(const struct inode *inode)
565{
566 return IS_ENCRYPTED(inode) && S_ISREG(inode->i_mode);
567}
568#else
569static inline bool
570fscrypt_inode_should_skip_dm_default_key(const struct inode *inode)
571{
572 return false;
573}
574#endif
575
Eric Biggersd293c3e2017-10-09 12:15:39 -0700576/**
577 * fscrypt_require_key - require an inode's encryption key
578 * @inode: the inode we need the key for
579 *
580 * If the inode is encrypted, set up its encryption key if not already done.
581 * Then require that the key be present and return -ENOKEY otherwise.
582 *
583 * No locks are needed, and the key will live as long as the struct inode --- so
584 * it won't go away from under you.
585 *
586 * Return: 0 on success, -ENOKEY if the key is missing, or another -errno code
587 * if a problem occurred while setting up the encryption key.
588 */
589static inline int fscrypt_require_key(struct inode *inode)
590{
591 if (IS_ENCRYPTED(inode)) {
592 int err = fscrypt_get_encryption_info(inode);
593
594 if (err)
595 return err;
596 if (!fscrypt_has_encryption_key(inode))
597 return -ENOKEY;
598 }
599 return 0;
600}
Dave Chinner734f0d22017-10-09 12:15:34 -0700601
Eric Biggers0ea87a92017-10-09 12:15:41 -0700602/**
603 * fscrypt_prepare_link - prepare to link an inode into a possibly-encrypted directory
604 * @old_dentry: an existing dentry for the inode being linked
605 * @dir: the target directory
606 * @dentry: negative dentry for the target filename
607 *
608 * A new link can only be added to an encrypted directory if the directory's
609 * encryption key is available --- since otherwise we'd have no way to encrypt
610 * the filename. Therefore, we first set up the directory's encryption key (if
611 * not already done) and return an error if it's unavailable.
612 *
613 * We also verify that the link will not violate the constraint that all files
614 * in an encrypted directory tree use the same encryption policy.
615 *
616 * Return: 0 on success, -ENOKEY if the directory's encryption key is missing,
Eric Biggers94e10682019-01-22 16:20:21 -0800617 * -EXDEV if the link would result in an inconsistent encryption policy, or
Eric Biggers0ea87a92017-10-09 12:15:41 -0700618 * another -errno code.
619 */
620static inline int fscrypt_prepare_link(struct dentry *old_dentry,
621 struct inode *dir,
622 struct dentry *dentry)
623{
624 if (IS_ENCRYPTED(dir))
Eric Biggers081985b2019-03-20 11:39:10 -0700625 return __fscrypt_prepare_link(d_inode(old_dentry), dir, dentry);
Eric Biggers0ea87a92017-10-09 12:15:41 -0700626 return 0;
627}
628
Eric Biggers94b26f32017-10-09 12:15:42 -0700629/**
630 * fscrypt_prepare_rename - prepare for a rename between possibly-encrypted directories
631 * @old_dir: source directory
632 * @old_dentry: dentry for source file
633 * @new_dir: target directory
634 * @new_dentry: dentry for target location (may be negative unless exchanging)
635 * @flags: rename flags (we care at least about %RENAME_EXCHANGE)
636 *
637 * Prepare for ->rename() where the source and/or target directories may be
638 * encrypted. A new link can only be added to an encrypted directory if the
639 * directory's encryption key is available --- since otherwise we'd have no way
640 * to encrypt the filename. A rename to an existing name, on the other hand,
641 * *is* cryptographically possible without the key. However, we take the more
642 * conservative approach and just forbid all no-key renames.
643 *
644 * We also verify that the rename will not violate the constraint that all files
645 * in an encrypted directory tree use the same encryption policy.
646 *
Eric Biggers94e10682019-01-22 16:20:21 -0800647 * Return: 0 on success, -ENOKEY if an encryption key is missing, -EXDEV if the
Eric Biggers94b26f32017-10-09 12:15:42 -0700648 * rename would cause inconsistent encryption policies, or another -errno code.
649 */
650static inline int fscrypt_prepare_rename(struct inode *old_dir,
651 struct dentry *old_dentry,
652 struct inode *new_dir,
653 struct dentry *new_dentry,
654 unsigned int flags)
655{
656 if (IS_ENCRYPTED(old_dir) || IS_ENCRYPTED(new_dir))
657 return __fscrypt_prepare_rename(old_dir, old_dentry,
658 new_dir, new_dentry, flags);
659 return 0;
660}
661
Eric Biggers32c3cf02017-10-09 12:15:43 -0700662/**
663 * fscrypt_prepare_lookup - prepare to lookup a name in a possibly-encrypted directory
664 * @dir: directory being searched
665 * @dentry: filename being looked up
Eric Biggers716c3702019-03-20 11:39:13 -0700666 * @fname: (output) the name to use to search the on-disk directory
Eric Biggers32c3cf02017-10-09 12:15:43 -0700667 *
Eric Biggers716c3702019-03-20 11:39:13 -0700668 * Prepare for ->lookup() in a directory which may be encrypted by determining
669 * the name that will actually be used to search the directory on-disk. Lookups
670 * can be done with or without the directory's encryption key; without the key,
Eric Biggers32c3cf02017-10-09 12:15:43 -0700671 * filenames are presented in encrypted form. Therefore, we'll try to set up
672 * the directory's encryption key, but even without it the lookup can continue.
673 *
Daniel Rosenbergaf2b6ea2020-01-27 20:04:40 -0800674 * After calling this function, a filesystem should ensure that it's dentry
675 * operations contain fscrypt_d_revalidate if DCACHE_ENCRYPTED_NAME was set,
676 * so that the dentry can be invalidated if the key is later added.
Eric Biggers32c3cf02017-10-09 12:15:43 -0700677 *
Eric Biggers716c3702019-03-20 11:39:13 -0700678 * Return: 0 on success; -ENOENT if key is unavailable but the filename isn't a
679 * correctly formed encoded ciphertext name, so a negative dentry should be
680 * created; or another -errno code.
Eric Biggers32c3cf02017-10-09 12:15:43 -0700681 */
682static inline int fscrypt_prepare_lookup(struct inode *dir,
683 struct dentry *dentry,
Eric Biggers716c3702019-03-20 11:39:13 -0700684 struct fscrypt_name *fname)
Eric Biggers32c3cf02017-10-09 12:15:43 -0700685{
686 if (IS_ENCRYPTED(dir))
Eric Biggers716c3702019-03-20 11:39:13 -0700687 return __fscrypt_prepare_lookup(dir, dentry, fname);
688
689 memset(fname, 0, sizeof(*fname));
690 fname->usr_fname = &dentry->d_name;
691 fname->disk_name.name = (unsigned char *)dentry->d_name.name;
692 fname->disk_name.len = dentry->d_name.len;
Eric Biggers32c3cf02017-10-09 12:15:43 -0700693 return 0;
694}
695
Eric Biggers815dac32017-10-09 12:15:44 -0700696/**
697 * fscrypt_prepare_setattr - prepare to change a possibly-encrypted inode's attributes
698 * @dentry: dentry through which the inode is being changed
699 * @attr: attributes to change
700 *
701 * Prepare for ->setattr() on a possibly-encrypted inode. On an encrypted file,
702 * most attribute changes are allowed even without the encryption key. However,
703 * without the encryption key we do have to forbid truncates. This is needed
704 * because the size being truncated to may not be a multiple of the filesystem
705 * block size, and in that case we'd have to decrypt the final block, zero the
706 * portion past i_size, and re-encrypt it. (We *could* allow truncating to a
707 * filesystem block boundary, but it's simpler to just forbid all truncates ---
708 * and we already forbid all other contents modifications without the key.)
709 *
710 * Return: 0 on success, -ENOKEY if the key is missing, or another -errno code
711 * if a problem occurred while setting up the encryption key.
712 */
713static inline int fscrypt_prepare_setattr(struct dentry *dentry,
714 struct iattr *attr)
715{
716 if (attr->ia_valid & ATTR_SIZE)
717 return fscrypt_require_key(d_inode(dentry));
718 return 0;
719}
720
Eric Biggers76e81d62018-01-05 10:45:01 -0800721/**
722 * fscrypt_prepare_symlink - prepare to create a possibly-encrypted symlink
723 * @dir: directory in which the symlink is being created
724 * @target: plaintext symlink target
725 * @len: length of @target excluding null terminator
726 * @max_len: space the filesystem has available to store the symlink target
727 * @disk_link: (out) the on-disk symlink target being prepared
728 *
729 * This function computes the size the symlink target will require on-disk,
730 * stores it in @disk_link->len, and validates it against @max_len. An
731 * encrypted symlink may be longer than the original.
732 *
733 * Additionally, @disk_link->name is set to @target if the symlink will be
734 * unencrypted, but left NULL if the symlink will be encrypted. For encrypted
735 * symlinks, the filesystem must call fscrypt_encrypt_symlink() to create the
736 * on-disk target later. (The reason for the two-step process is that some
737 * filesystems need to know the size of the symlink target before creating the
738 * inode, e.g. to determine whether it will be a "fast" or "slow" symlink.)
739 *
740 * Return: 0 on success, -ENAMETOOLONG if the symlink target is too long,
741 * -ENOKEY if the encryption key is missing, or another -errno code if a problem
742 * occurred while setting up the encryption key.
743 */
744static inline int fscrypt_prepare_symlink(struct inode *dir,
745 const char *target,
746 unsigned int len,
747 unsigned int max_len,
748 struct fscrypt_str *disk_link)
749{
750 if (IS_ENCRYPTED(dir) || fscrypt_dummy_context_enabled(dir))
751 return __fscrypt_prepare_symlink(dir, len, max_len, disk_link);
752
753 disk_link->name = (unsigned char *)target;
754 disk_link->len = len + 1;
755 if (disk_link->len > max_len)
756 return -ENAMETOOLONG;
757 return 0;
758}
759
760/**
761 * fscrypt_encrypt_symlink - encrypt the symlink target if needed
762 * @inode: symlink inode
763 * @target: plaintext symlink target
764 * @len: length of @target excluding null terminator
765 * @disk_link: (in/out) the on-disk symlink target being prepared
766 *
767 * If the symlink target needs to be encrypted, then this function encrypts it
768 * into @disk_link->name. fscrypt_prepare_symlink() must have been called
769 * previously to compute @disk_link->len. If the filesystem did not allocate a
770 * buffer for @disk_link->name after calling fscrypt_prepare_link(), then one
771 * will be kmalloc()'ed and the filesystem will be responsible for freeing it.
772 *
773 * Return: 0 on success, -errno on failure
774 */
775static inline int fscrypt_encrypt_symlink(struct inode *inode,
776 const char *target,
777 unsigned int len,
778 struct fscrypt_str *disk_link)
779{
780 if (IS_ENCRYPTED(inode))
781 return __fscrypt_encrypt_symlink(inode, target, len, disk_link);
782 return 0;
783}
784
Eric Biggers2edab9e2019-05-20 09:29:39 -0700785/* If *pagep is a bounce page, free it and set *pagep to the pagecache page */
786static inline void fscrypt_finalize_bounce_page(struct page **pagep)
787{
788 struct page *page = *pagep;
789
790 if (fscrypt_is_bounce_page(page)) {
791 *pagep = fscrypt_pagecache_page(page);
792 fscrypt_free_bounce_page(page);
793 }
794}
Dave Chinner734f0d22017-10-09 12:15:34 -0700795#endif /* _LINUX_FSCRYPT_H */