blob: d0a1b8edfd9dbe003507aa7ca3347eac61f7e87d [file] [log] [blame]
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001/* SPDX-License-Identifier: GPL-2.0 */
2/*
3 * fscrypt.h: declarations for per-file encryption
4 *
David Brazdil0f672f62019-12-10 10:32:29 +00005 * Filesystems that implement per-file encryption must include this header
6 * file.
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00007 *
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>
David Brazdil0f672f62019-12-10 10:32:29 +000017#include <linux/mm.h>
18#include <linux/slab.h>
19#include <uapi/linux/fscrypt.h>
Andrew Scullb4b6d4a2019-01-02 15:54:55 +000020
21#define FS_CRYPTO_BLOCK_SIZE 16
22
Olivier Deprez157378f2022-04-04 15:47:50 +020023union fscrypt_policy;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +000024struct fscrypt_info;
Olivier Deprez157378f2022-04-04 15:47:50 +020025struct seq_file;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +000026
27struct fscrypt_str {
28 unsigned char *name;
29 u32 len;
30};
31
32struct fscrypt_name {
33 const struct qstr *usr_fname;
34 struct fscrypt_str disk_name;
35 u32 hash;
36 u32 minor_hash;
37 struct fscrypt_str crypto_buf;
Olivier Deprez157378f2022-04-04 15:47:50 +020038 bool is_nokey_name;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +000039};
40
41#define FSTR_INIT(n, l) { .name = n, .len = l }
42#define FSTR_TO_QSTR(f) QSTR_INIT((f)->name, (f)->len)
43#define fname_name(p) ((p)->disk_name.name)
44#define fname_len(p) ((p)->disk_name.len)
45
46/* Maximum value for the third parameter of fscrypt_operations.set_context(). */
David Brazdil0f672f62019-12-10 10:32:29 +000047#define FSCRYPT_SET_CONTEXT_MAX_SIZE 40
Andrew Scullb4b6d4a2019-01-02 15:54:55 +000048
David Brazdil0f672f62019-12-10 10:32:29 +000049#ifdef CONFIG_FS_ENCRYPTION
50/*
51 * fscrypt superblock flags
52 */
53#define FS_CFLG_OWN_PAGES (1U << 1)
54
55/*
56 * crypto operations for filesystems
57 */
58struct fscrypt_operations {
59 unsigned int flags;
60 const char *key_prefix;
Olivier Deprez157378f2022-04-04 15:47:50 +020061 int (*get_context)(struct inode *inode, void *ctx, size_t len);
62 int (*set_context)(struct inode *inode, const void *ctx, size_t len,
63 void *fs_data);
64 const union fscrypt_policy *(*get_dummy_policy)(struct super_block *sb);
65 bool (*empty_dir)(struct inode *inode);
David Brazdil0f672f62019-12-10 10:32:29 +000066 unsigned int max_namelen;
Olivier Deprez157378f2022-04-04 15:47:50 +020067 bool (*has_stable_inodes)(struct super_block *sb);
68 void (*get_ino_and_lblk_bits)(struct super_block *sb,
69 int *ino_bits_ret, int *lblk_bits_ret);
70 int (*get_num_devices)(struct super_block *sb);
71 void (*get_devices)(struct super_block *sb,
72 struct request_queue **devs);
David Brazdil0f672f62019-12-10 10:32:29 +000073};
74
Olivier Deprez157378f2022-04-04 15:47:50 +020075static inline struct fscrypt_info *fscrypt_get_info(const struct inode *inode)
David Brazdil0f672f62019-12-10 10:32:29 +000076{
Olivier Deprez157378f2022-04-04 15:47:50 +020077 /*
78 * Pairs with the cmpxchg_release() in fscrypt_get_encryption_info().
79 * I.e., another task may publish ->i_crypt_info concurrently, executing
80 * a RELEASE barrier. We need to use smp_load_acquire() here to safely
81 * ACQUIRE the memory the other task published.
82 */
83 return smp_load_acquire(&inode->i_crypt_info);
David Brazdil0f672f62019-12-10 10:32:29 +000084}
85
Olivier Deprez157378f2022-04-04 15:47:50 +020086/**
87 * fscrypt_needs_contents_encryption() - check whether an inode needs
88 * contents encryption
89 * @inode: the inode to check
90 *
91 * Return: %true iff the inode is an encrypted regular file and the kernel was
92 * built with fscrypt support.
93 *
94 * If you need to know whether the encrypt bit is set even when the kernel was
95 * built without fscrypt support, you must use IS_ENCRYPTED() directly instead.
96 */
97static inline bool fscrypt_needs_contents_encryption(const struct inode *inode)
David Brazdil0f672f62019-12-10 10:32:29 +000098{
Olivier Deprez157378f2022-04-04 15:47:50 +020099 return IS_ENCRYPTED(inode) && S_ISREG(inode->i_mode);
David Brazdil0f672f62019-12-10 10:32:29 +0000100}
101
102/*
Olivier Deprez157378f2022-04-04 15:47:50 +0200103 * When d_splice_alias() moves a directory's no-key alias to its plaintext alias
104 * as a result of the encryption key being added, DCACHE_NOKEY_NAME must be
105 * cleared. Note that we don't have to support arbitrary moves of this flag
106 * because fscrypt doesn't allow no-key names to be the source or target of a
107 * rename().
David Brazdil0f672f62019-12-10 10:32:29 +0000108 */
109static inline void fscrypt_handle_d_move(struct dentry *dentry)
110{
Olivier Deprez157378f2022-04-04 15:47:50 +0200111 dentry->d_flags &= ~DCACHE_NOKEY_NAME;
David Brazdil0f672f62019-12-10 10:32:29 +0000112}
113
Olivier Deprez0e641232021-09-23 10:07:05 +0200114/**
115 * fscrypt_is_nokey_name() - test whether a dentry is a no-key name
116 * @dentry: the dentry to check
117 *
118 * This returns true if the dentry is a no-key dentry. A no-key dentry is a
119 * dentry that was created in an encrypted directory that hasn't had its
120 * encryption key added yet. Such dentries may be either positive or negative.
121 *
122 * When a filesystem is asked to create a new filename in an encrypted directory
123 * and the new filename's dentry is a no-key dentry, it must fail the operation
124 * with ENOKEY. This includes ->create(), ->mkdir(), ->mknod(), ->symlink(),
125 * ->rename(), and ->link(). (However, ->rename() and ->link() are already
126 * handled by fscrypt_prepare_rename() and fscrypt_prepare_link().)
127 *
128 * This is necessary because creating a filename requires the directory's
129 * encryption key, but just checking for the key on the directory inode during
130 * the final filesystem operation doesn't guarantee that the key was available
131 * during the preceding dentry lookup. And the key must have already been
132 * available during the dentry lookup in order for it to have been checked
133 * whether the filename already exists in the directory and for the new file's
134 * dentry not to be invalidated due to it incorrectly having the no-key flag.
135 *
136 * Return: %true if the dentry is a no-key name
137 */
138static inline bool fscrypt_is_nokey_name(const struct dentry *dentry)
139{
Olivier Deprez157378f2022-04-04 15:47:50 +0200140 return dentry->d_flags & DCACHE_NOKEY_NAME;
Olivier Deprez0e641232021-09-23 10:07:05 +0200141}
142
David Brazdil0f672f62019-12-10 10:32:29 +0000143/* crypto.c */
Olivier Deprez157378f2022-04-04 15:47:50 +0200144void fscrypt_enqueue_decrypt_work(struct work_struct *);
David Brazdil0f672f62019-12-10 10:32:29 +0000145
Olivier Deprez157378f2022-04-04 15:47:50 +0200146struct page *fscrypt_encrypt_pagecache_blocks(struct page *page,
147 unsigned int len,
148 unsigned int offs,
149 gfp_t gfp_flags);
150int fscrypt_encrypt_block_inplace(const struct inode *inode, struct page *page,
151 unsigned int len, unsigned int offs,
152 u64 lblk_num, gfp_t gfp_flags);
David Brazdil0f672f62019-12-10 10:32:29 +0000153
Olivier Deprez157378f2022-04-04 15:47:50 +0200154int fscrypt_decrypt_pagecache_blocks(struct page *page, unsigned int len,
155 unsigned int offs);
156int fscrypt_decrypt_block_inplace(const struct inode *inode, struct page *page,
157 unsigned int len, unsigned int offs,
158 u64 lblk_num);
David Brazdil0f672f62019-12-10 10:32:29 +0000159
160static inline bool fscrypt_is_bounce_page(struct page *page)
161{
162 return page->mapping == NULL;
163}
164
165static inline struct page *fscrypt_pagecache_page(struct page *bounce_page)
166{
167 return (struct page *)page_private(bounce_page);
168}
169
Olivier Deprez157378f2022-04-04 15:47:50 +0200170void fscrypt_free_bounce_page(struct page *bounce_page);
David Brazdil0f672f62019-12-10 10:32:29 +0000171
172/* policy.c */
Olivier Deprez157378f2022-04-04 15:47:50 +0200173int fscrypt_ioctl_set_policy(struct file *filp, const void __user *arg);
174int fscrypt_ioctl_get_policy(struct file *filp, void __user *arg);
175int fscrypt_ioctl_get_policy_ex(struct file *filp, void __user *arg);
176int fscrypt_ioctl_get_nonce(struct file *filp, void __user *arg);
177int fscrypt_has_permitted_context(struct inode *parent, struct inode *child);
178int fscrypt_set_context(struct inode *inode, void *fs_data);
179
180struct fscrypt_dummy_policy {
181 const union fscrypt_policy *policy;
182};
183
184int fscrypt_set_test_dummy_encryption(struct super_block *sb, const char *arg,
185 struct fscrypt_dummy_policy *dummy_policy);
186void fscrypt_show_test_dummy_encryption(struct seq_file *seq, char sep,
187 struct super_block *sb);
188static inline void
189fscrypt_free_dummy_policy(struct fscrypt_dummy_policy *dummy_policy)
190{
191 kfree(dummy_policy->policy);
192 dummy_policy->policy = NULL;
193}
194
David Brazdil0f672f62019-12-10 10:32:29 +0000195/* keyring.c */
Olivier Deprez157378f2022-04-04 15:47:50 +0200196void fscrypt_sb_free(struct super_block *sb);
197int fscrypt_ioctl_add_key(struct file *filp, void __user *arg);
198int fscrypt_ioctl_remove_key(struct file *filp, void __user *arg);
199int fscrypt_ioctl_remove_key_all_users(struct file *filp, void __user *arg);
200int fscrypt_ioctl_get_key_status(struct file *filp, void __user *arg);
David Brazdil0f672f62019-12-10 10:32:29 +0000201
202/* keysetup.c */
Olivier Deprez157378f2022-04-04 15:47:50 +0200203int fscrypt_get_encryption_info(struct inode *inode);
204int fscrypt_prepare_new_inode(struct inode *dir, struct inode *inode,
205 bool *encrypt_ret);
206void fscrypt_put_encryption_info(struct inode *inode);
207void fscrypt_free_inode(struct inode *inode);
208int fscrypt_drop_inode(struct inode *inode);
David Brazdil0f672f62019-12-10 10:32:29 +0000209
210/* fname.c */
Olivier Deprez157378f2022-04-04 15:47:50 +0200211int fscrypt_setup_filename(struct inode *inode, const struct qstr *iname,
212 int lookup, struct fscrypt_name *fname);
David Brazdil0f672f62019-12-10 10:32:29 +0000213
214static inline void fscrypt_free_filename(struct fscrypt_name *fname)
215{
216 kfree(fname->crypto_buf.name);
217}
218
Olivier Deprez157378f2022-04-04 15:47:50 +0200219int fscrypt_fname_alloc_buffer(u32 max_encrypted_len,
220 struct fscrypt_str *crypto_str);
221void fscrypt_fname_free_buffer(struct fscrypt_str *crypto_str);
222int fscrypt_fname_disk_to_usr(const struct inode *inode,
223 u32 hash, u32 minor_hash,
224 const struct fscrypt_str *iname,
225 struct fscrypt_str *oname);
226bool fscrypt_match_name(const struct fscrypt_name *fname,
227 const u8 *de_name, u32 de_name_len);
228u64 fscrypt_fname_siphash(const struct inode *dir, const struct qstr *name);
229int fscrypt_d_revalidate(struct dentry *dentry, unsigned int flags);
David Brazdil0f672f62019-12-10 10:32:29 +0000230
231/* bio.c */
Olivier Deprez157378f2022-04-04 15:47:50 +0200232void fscrypt_decrypt_bio(struct bio *bio);
233int fscrypt_zeroout_range(const struct inode *inode, pgoff_t lblk,
234 sector_t pblk, unsigned int len);
David Brazdil0f672f62019-12-10 10:32:29 +0000235
236/* hooks.c */
Olivier Deprez157378f2022-04-04 15:47:50 +0200237int fscrypt_file_open(struct inode *inode, struct file *filp);
238int __fscrypt_prepare_link(struct inode *inode, struct inode *dir,
239 struct dentry *dentry);
240int __fscrypt_prepare_rename(struct inode *old_dir, struct dentry *old_dentry,
241 struct inode *new_dir, struct dentry *new_dentry,
242 unsigned int flags);
243int __fscrypt_prepare_lookup(struct inode *dir, struct dentry *dentry,
244 struct fscrypt_name *fname);
245int fscrypt_prepare_setflags(struct inode *inode,
246 unsigned int oldflags, unsigned int flags);
247int fscrypt_prepare_symlink(struct inode *dir, const char *target,
248 unsigned int len, unsigned int max_len,
249 struct fscrypt_str *disk_link);
250int __fscrypt_encrypt_symlink(struct inode *inode, const char *target,
251 unsigned int len, struct fscrypt_str *disk_link);
252const char *fscrypt_get_symlink(struct inode *inode, const void *caddr,
253 unsigned int max_size,
254 struct delayed_call *done);
Olivier Deprez0e641232021-09-23 10:07:05 +0200255int fscrypt_symlink_getattr(const struct path *path, struct kstat *stat);
David Brazdil0f672f62019-12-10 10:32:29 +0000256static inline void fscrypt_set_ops(struct super_block *sb,
257 const struct fscrypt_operations *s_cop)
258{
259 sb->s_cop = s_cop;
260}
261#else /* !CONFIG_FS_ENCRYPTION */
262
Olivier Deprez157378f2022-04-04 15:47:50 +0200263static inline struct fscrypt_info *fscrypt_get_info(const struct inode *inode)
David Brazdil0f672f62019-12-10 10:32:29 +0000264{
Olivier Deprez157378f2022-04-04 15:47:50 +0200265 return NULL;
David Brazdil0f672f62019-12-10 10:32:29 +0000266}
267
Olivier Deprez157378f2022-04-04 15:47:50 +0200268static inline bool fscrypt_needs_contents_encryption(const struct inode *inode)
David Brazdil0f672f62019-12-10 10:32:29 +0000269{
270 return false;
271}
272
273static inline void fscrypt_handle_d_move(struct dentry *dentry)
274{
275}
276
Olivier Deprez0e641232021-09-23 10:07:05 +0200277static inline bool fscrypt_is_nokey_name(const struct dentry *dentry)
278{
279 return false;
280}
281
David Brazdil0f672f62019-12-10 10:32:29 +0000282/* crypto.c */
283static inline void fscrypt_enqueue_decrypt_work(struct work_struct *work)
284{
285}
286
David Brazdil0f672f62019-12-10 10:32:29 +0000287static inline struct page *fscrypt_encrypt_pagecache_blocks(struct page *page,
288 unsigned int len,
289 unsigned int offs,
290 gfp_t gfp_flags)
291{
292 return ERR_PTR(-EOPNOTSUPP);
293}
294
295static inline int fscrypt_encrypt_block_inplace(const struct inode *inode,
296 struct page *page,
297 unsigned int len,
298 unsigned int offs, u64 lblk_num,
299 gfp_t gfp_flags)
300{
301 return -EOPNOTSUPP;
302}
303
304static inline int fscrypt_decrypt_pagecache_blocks(struct page *page,
305 unsigned int len,
306 unsigned int offs)
307{
308 return -EOPNOTSUPP;
309}
310
311static inline int fscrypt_decrypt_block_inplace(const struct inode *inode,
312 struct page *page,
313 unsigned int len,
314 unsigned int offs, u64 lblk_num)
315{
316 return -EOPNOTSUPP;
317}
318
319static inline bool fscrypt_is_bounce_page(struct page *page)
320{
321 return false;
322}
323
324static inline struct page *fscrypt_pagecache_page(struct page *bounce_page)
325{
326 WARN_ON_ONCE(1);
327 return ERR_PTR(-EINVAL);
328}
329
330static inline void fscrypt_free_bounce_page(struct page *bounce_page)
331{
332}
333
334/* policy.c */
335static inline int fscrypt_ioctl_set_policy(struct file *filp,
336 const void __user *arg)
337{
338 return -EOPNOTSUPP;
339}
340
341static inline int fscrypt_ioctl_get_policy(struct file *filp, void __user *arg)
342{
343 return -EOPNOTSUPP;
344}
345
346static inline int fscrypt_ioctl_get_policy_ex(struct file *filp,
347 void __user *arg)
348{
349 return -EOPNOTSUPP;
350}
351
Olivier Deprez157378f2022-04-04 15:47:50 +0200352static inline int fscrypt_ioctl_get_nonce(struct file *filp, void __user *arg)
353{
354 return -EOPNOTSUPP;
355}
356
David Brazdil0f672f62019-12-10 10:32:29 +0000357static inline int fscrypt_has_permitted_context(struct inode *parent,
358 struct inode *child)
359{
360 return 0;
361}
362
Olivier Deprez157378f2022-04-04 15:47:50 +0200363static inline int fscrypt_set_context(struct inode *inode, void *fs_data)
David Brazdil0f672f62019-12-10 10:32:29 +0000364{
365 return -EOPNOTSUPP;
366}
367
Olivier Deprez157378f2022-04-04 15:47:50 +0200368struct fscrypt_dummy_policy {
369};
370
371static inline void fscrypt_show_test_dummy_encryption(struct seq_file *seq,
372 char sep,
373 struct super_block *sb)
374{
375}
376
377static inline void
378fscrypt_free_dummy_policy(struct fscrypt_dummy_policy *dummy_policy)
379{
380}
381
David Brazdil0f672f62019-12-10 10:32:29 +0000382/* keyring.c */
383static inline void fscrypt_sb_free(struct super_block *sb)
384{
385}
386
387static inline int fscrypt_ioctl_add_key(struct file *filp, void __user *arg)
388{
389 return -EOPNOTSUPP;
390}
391
392static inline int fscrypt_ioctl_remove_key(struct file *filp, void __user *arg)
393{
394 return -EOPNOTSUPP;
395}
396
397static inline int fscrypt_ioctl_remove_key_all_users(struct file *filp,
398 void __user *arg)
399{
400 return -EOPNOTSUPP;
401}
402
403static inline int fscrypt_ioctl_get_key_status(struct file *filp,
404 void __user *arg)
405{
406 return -EOPNOTSUPP;
407}
408
409/* keysetup.c */
410static inline int fscrypt_get_encryption_info(struct inode *inode)
411{
412 return -EOPNOTSUPP;
413}
414
Olivier Deprez157378f2022-04-04 15:47:50 +0200415static inline int fscrypt_prepare_new_inode(struct inode *dir,
416 struct inode *inode,
417 bool *encrypt_ret)
418{
419 if (IS_ENCRYPTED(dir))
420 return -EOPNOTSUPP;
421 return 0;
422}
423
David Brazdil0f672f62019-12-10 10:32:29 +0000424static inline void fscrypt_put_encryption_info(struct inode *inode)
425{
426 return;
427}
428
429static inline void fscrypt_free_inode(struct inode *inode)
430{
431}
432
433static inline int fscrypt_drop_inode(struct inode *inode)
434{
435 return 0;
436}
437
438 /* fname.c */
439static inline int fscrypt_setup_filename(struct inode *dir,
440 const struct qstr *iname,
441 int lookup, struct fscrypt_name *fname)
442{
443 if (IS_ENCRYPTED(dir))
444 return -EOPNOTSUPP;
445
446 memset(fname, 0, sizeof(*fname));
447 fname->usr_fname = iname;
448 fname->disk_name.name = (unsigned char *)iname->name;
449 fname->disk_name.len = iname->len;
450 return 0;
451}
452
453static inline void fscrypt_free_filename(struct fscrypt_name *fname)
454{
455 return;
456}
457
Olivier Deprez157378f2022-04-04 15:47:50 +0200458static inline int fscrypt_fname_alloc_buffer(u32 max_encrypted_len,
David Brazdil0f672f62019-12-10 10:32:29 +0000459 struct fscrypt_str *crypto_str)
460{
461 return -EOPNOTSUPP;
462}
463
464static inline void fscrypt_fname_free_buffer(struct fscrypt_str *crypto_str)
465{
466 return;
467}
468
Olivier Deprez157378f2022-04-04 15:47:50 +0200469static inline int fscrypt_fname_disk_to_usr(const struct inode *inode,
David Brazdil0f672f62019-12-10 10:32:29 +0000470 u32 hash, u32 minor_hash,
471 const struct fscrypt_str *iname,
472 struct fscrypt_str *oname)
473{
474 return -EOPNOTSUPP;
475}
476
477static inline bool fscrypt_match_name(const struct fscrypt_name *fname,
478 const u8 *de_name, u32 de_name_len)
479{
480 /* Encryption support disabled; use standard comparison */
481 if (de_name_len != fname->disk_name.len)
482 return false;
483 return !memcmp(de_name, fname->disk_name.name, fname->disk_name.len);
484}
485
Olivier Deprez157378f2022-04-04 15:47:50 +0200486static inline u64 fscrypt_fname_siphash(const struct inode *dir,
487 const struct qstr *name)
David Brazdil0f672f62019-12-10 10:32:29 +0000488{
Olivier Deprez157378f2022-04-04 15:47:50 +0200489 WARN_ON_ONCE(1);
490 return 0;
David Brazdil0f672f62019-12-10 10:32:29 +0000491}
492
Olivier Deprez157378f2022-04-04 15:47:50 +0200493static inline int fscrypt_d_revalidate(struct dentry *dentry,
494 unsigned int flags)
495{
496 return 1;
497}
498
499/* bio.c */
500static inline void fscrypt_decrypt_bio(struct bio *bio)
David Brazdil0f672f62019-12-10 10:32:29 +0000501{
502}
503
504static inline int fscrypt_zeroout_range(const struct inode *inode, pgoff_t lblk,
505 sector_t pblk, unsigned int len)
506{
507 return -EOPNOTSUPP;
508}
509
510/* hooks.c */
511
512static inline int fscrypt_file_open(struct inode *inode, struct file *filp)
513{
514 if (IS_ENCRYPTED(inode))
515 return -EOPNOTSUPP;
516 return 0;
517}
518
519static inline int __fscrypt_prepare_link(struct inode *inode, struct inode *dir,
520 struct dentry *dentry)
521{
522 return -EOPNOTSUPP;
523}
524
525static inline int __fscrypt_prepare_rename(struct inode *old_dir,
526 struct dentry *old_dentry,
527 struct inode *new_dir,
528 struct dentry *new_dentry,
529 unsigned int flags)
530{
531 return -EOPNOTSUPP;
532}
533
534static inline int __fscrypt_prepare_lookup(struct inode *dir,
535 struct dentry *dentry,
536 struct fscrypt_name *fname)
537{
538 return -EOPNOTSUPP;
539}
540
Olivier Deprez157378f2022-04-04 15:47:50 +0200541static inline int fscrypt_prepare_setflags(struct inode *inode,
542 unsigned int oldflags,
543 unsigned int flags)
David Brazdil0f672f62019-12-10 10:32:29 +0000544{
Olivier Deprez157378f2022-04-04 15:47:50 +0200545 return 0;
David Brazdil0f672f62019-12-10 10:32:29 +0000546}
547
Olivier Deprez157378f2022-04-04 15:47:50 +0200548static inline int fscrypt_prepare_symlink(struct inode *dir,
549 const char *target,
550 unsigned int len,
551 unsigned int max_len,
552 struct fscrypt_str *disk_link)
553{
554 if (IS_ENCRYPTED(dir))
555 return -EOPNOTSUPP;
556 disk_link->name = (unsigned char *)target;
557 disk_link->len = len + 1;
558 if (disk_link->len > max_len)
559 return -ENAMETOOLONG;
560 return 0;
561}
David Brazdil0f672f62019-12-10 10:32:29 +0000562
563static inline int __fscrypt_encrypt_symlink(struct inode *inode,
564 const char *target,
565 unsigned int len,
566 struct fscrypt_str *disk_link)
567{
568 return -EOPNOTSUPP;
569}
570
571static inline const char *fscrypt_get_symlink(struct inode *inode,
572 const void *caddr,
573 unsigned int max_size,
574 struct delayed_call *done)
575{
576 return ERR_PTR(-EOPNOTSUPP);
577}
578
Olivier Deprez0e641232021-09-23 10:07:05 +0200579static inline int fscrypt_symlink_getattr(const struct path *path,
580 struct kstat *stat)
581{
582 return -EOPNOTSUPP;
583}
584
David Brazdil0f672f62019-12-10 10:32:29 +0000585static inline void fscrypt_set_ops(struct super_block *sb,
586 const struct fscrypt_operations *s_cop)
587{
588}
589
590#endif /* !CONFIG_FS_ENCRYPTION */
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000591
Olivier Deprez157378f2022-04-04 15:47:50 +0200592/* inline_crypt.c */
593#ifdef CONFIG_FS_ENCRYPTION_INLINE_CRYPT
594
595bool __fscrypt_inode_uses_inline_crypto(const struct inode *inode);
596
597void fscrypt_set_bio_crypt_ctx(struct bio *bio,
598 const struct inode *inode, u64 first_lblk,
599 gfp_t gfp_mask);
600
601void fscrypt_set_bio_crypt_ctx_bh(struct bio *bio,
602 const struct buffer_head *first_bh,
603 gfp_t gfp_mask);
604
605bool fscrypt_mergeable_bio(struct bio *bio, const struct inode *inode,
606 u64 next_lblk);
607
608bool fscrypt_mergeable_bio_bh(struct bio *bio,
609 const struct buffer_head *next_bh);
610
611#else /* CONFIG_FS_ENCRYPTION_INLINE_CRYPT */
612
613static inline bool __fscrypt_inode_uses_inline_crypto(const struct inode *inode)
614{
615 return false;
616}
617
618static inline void fscrypt_set_bio_crypt_ctx(struct bio *bio,
619 const struct inode *inode,
620 u64 first_lblk, gfp_t gfp_mask) { }
621
622static inline void fscrypt_set_bio_crypt_ctx_bh(
623 struct bio *bio,
624 const struct buffer_head *first_bh,
625 gfp_t gfp_mask) { }
626
627static inline bool fscrypt_mergeable_bio(struct bio *bio,
628 const struct inode *inode,
629 u64 next_lblk)
630{
631 return true;
632}
633
634static inline bool fscrypt_mergeable_bio_bh(struct bio *bio,
635 const struct buffer_head *next_bh)
636{
637 return true;
638}
639#endif /* !CONFIG_FS_ENCRYPTION_INLINE_CRYPT */
640
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000641/**
Olivier Deprez157378f2022-04-04 15:47:50 +0200642 * fscrypt_inode_uses_inline_crypto() - test whether an inode uses inline
643 * encryption
644 * @inode: an inode. If encrypted, its key must be set up.
645 *
646 * Return: true if the inode requires file contents encryption and if the
647 * encryption should be done in the block layer via blk-crypto rather
648 * than in the filesystem layer.
649 */
650static inline bool fscrypt_inode_uses_inline_crypto(const struct inode *inode)
651{
652 return fscrypt_needs_contents_encryption(inode) &&
653 __fscrypt_inode_uses_inline_crypto(inode);
654}
655
656/**
657 * fscrypt_inode_uses_fs_layer_crypto() - test whether an inode uses fs-layer
658 * encryption
659 * @inode: an inode. If encrypted, its key must be set up.
660 *
661 * Return: true if the inode requires file contents encryption and if the
662 * encryption should be done in the filesystem layer rather than in the
663 * block layer via blk-crypto.
664 */
665static inline bool fscrypt_inode_uses_fs_layer_crypto(const struct inode *inode)
666{
667 return fscrypt_needs_contents_encryption(inode) &&
668 !__fscrypt_inode_uses_inline_crypto(inode);
669}
670
671/**
672 * fscrypt_has_encryption_key() - check whether an inode has had its key set up
673 * @inode: the inode to check
674 *
675 * Return: %true if the inode has had its encryption key set up, else %false.
676 *
677 * Usually this should be preceded by fscrypt_get_encryption_info() to try to
678 * set up the key first.
679 */
680static inline bool fscrypt_has_encryption_key(const struct inode *inode)
681{
682 return fscrypt_get_info(inode) != NULL;
683}
684
685/**
686 * fscrypt_require_key() - require an inode's encryption key
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000687 * @inode: the inode we need the key for
688 *
689 * If the inode is encrypted, set up its encryption key if not already done.
690 * Then require that the key be present and return -ENOKEY otherwise.
691 *
692 * No locks are needed, and the key will live as long as the struct inode --- so
693 * it won't go away from under you.
694 *
695 * Return: 0 on success, -ENOKEY if the key is missing, or another -errno code
696 * if a problem occurred while setting up the encryption key.
697 */
698static inline int fscrypt_require_key(struct inode *inode)
699{
700 if (IS_ENCRYPTED(inode)) {
701 int err = fscrypt_get_encryption_info(inode);
702
703 if (err)
704 return err;
705 if (!fscrypt_has_encryption_key(inode))
706 return -ENOKEY;
707 }
708 return 0;
709}
710
711/**
Olivier Deprez157378f2022-04-04 15:47:50 +0200712 * fscrypt_prepare_link() - prepare to link an inode into a possibly-encrypted
713 * directory
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000714 * @old_dentry: an existing dentry for the inode being linked
715 * @dir: the target directory
716 * @dentry: negative dentry for the target filename
717 *
718 * A new link can only be added to an encrypted directory if the directory's
719 * encryption key is available --- since otherwise we'd have no way to encrypt
720 * the filename. Therefore, we first set up the directory's encryption key (if
721 * not already done) and return an error if it's unavailable.
722 *
723 * We also verify that the link will not violate the constraint that all files
724 * in an encrypted directory tree use the same encryption policy.
725 *
726 * Return: 0 on success, -ENOKEY if the directory's encryption key is missing,
David Brazdil0f672f62019-12-10 10:32:29 +0000727 * -EXDEV if the link would result in an inconsistent encryption policy, or
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000728 * another -errno code.
729 */
730static inline int fscrypt_prepare_link(struct dentry *old_dentry,
731 struct inode *dir,
732 struct dentry *dentry)
733{
734 if (IS_ENCRYPTED(dir))
David Brazdil0f672f62019-12-10 10:32:29 +0000735 return __fscrypt_prepare_link(d_inode(old_dentry), dir, dentry);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000736 return 0;
737}
738
739/**
Olivier Deprez157378f2022-04-04 15:47:50 +0200740 * fscrypt_prepare_rename() - prepare for a rename between possibly-encrypted
741 * directories
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000742 * @old_dir: source directory
743 * @old_dentry: dentry for source file
744 * @new_dir: target directory
745 * @new_dentry: dentry for target location (may be negative unless exchanging)
746 * @flags: rename flags (we care at least about %RENAME_EXCHANGE)
747 *
748 * Prepare for ->rename() where the source and/or target directories may be
749 * encrypted. A new link can only be added to an encrypted directory if the
750 * directory's encryption key is available --- since otherwise we'd have no way
751 * to encrypt the filename. A rename to an existing name, on the other hand,
752 * *is* cryptographically possible without the key. However, we take the more
753 * conservative approach and just forbid all no-key renames.
754 *
755 * We also verify that the rename will not violate the constraint that all files
756 * in an encrypted directory tree use the same encryption policy.
757 *
David Brazdil0f672f62019-12-10 10:32:29 +0000758 * Return: 0 on success, -ENOKEY if an encryption key is missing, -EXDEV if the
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000759 * rename would cause inconsistent encryption policies, or another -errno code.
760 */
761static inline int fscrypt_prepare_rename(struct inode *old_dir,
762 struct dentry *old_dentry,
763 struct inode *new_dir,
764 struct dentry *new_dentry,
765 unsigned int flags)
766{
767 if (IS_ENCRYPTED(old_dir) || IS_ENCRYPTED(new_dir))
768 return __fscrypt_prepare_rename(old_dir, old_dentry,
769 new_dir, new_dentry, flags);
770 return 0;
771}
772
773/**
Olivier Deprez157378f2022-04-04 15:47:50 +0200774 * fscrypt_prepare_lookup() - prepare to lookup a name in a possibly-encrypted
775 * directory
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000776 * @dir: directory being searched
777 * @dentry: filename being looked up
David Brazdil0f672f62019-12-10 10:32:29 +0000778 * @fname: (output) the name to use to search the on-disk directory
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000779 *
David Brazdil0f672f62019-12-10 10:32:29 +0000780 * Prepare for ->lookup() in a directory which may be encrypted by determining
Olivier Deprez157378f2022-04-04 15:47:50 +0200781 * the name that will actually be used to search the directory on-disk. If the
782 * directory's encryption key is available, then the lookup is assumed to be by
783 * plaintext name; otherwise, it is assumed to be by no-key name.
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000784 *
David Brazdil0f672f62019-12-10 10:32:29 +0000785 * This also installs a custom ->d_revalidate() method which will invalidate the
786 * dentry if it was created without the key and the key is later added.
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000787 *
Olivier Deprez157378f2022-04-04 15:47:50 +0200788 * Return: 0 on success; -ENOENT if the directory's key is unavailable but the
789 * filename isn't a valid no-key name, so a negative dentry should be created;
790 * or another -errno code.
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000791 */
792static inline int fscrypt_prepare_lookup(struct inode *dir,
793 struct dentry *dentry,
David Brazdil0f672f62019-12-10 10:32:29 +0000794 struct fscrypt_name *fname)
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000795{
796 if (IS_ENCRYPTED(dir))
David Brazdil0f672f62019-12-10 10:32:29 +0000797 return __fscrypt_prepare_lookup(dir, dentry, fname);
798
799 memset(fname, 0, sizeof(*fname));
800 fname->usr_fname = &dentry->d_name;
801 fname->disk_name.name = (unsigned char *)dentry->d_name.name;
802 fname->disk_name.len = dentry->d_name.len;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000803 return 0;
804}
805
806/**
Olivier Deprez157378f2022-04-04 15:47:50 +0200807 * fscrypt_prepare_setattr() - prepare to change a possibly-encrypted inode's
808 * attributes
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000809 * @dentry: dentry through which the inode is being changed
810 * @attr: attributes to change
811 *
812 * Prepare for ->setattr() on a possibly-encrypted inode. On an encrypted file,
813 * most attribute changes are allowed even without the encryption key. However,
814 * without the encryption key we do have to forbid truncates. This is needed
815 * because the size being truncated to may not be a multiple of the filesystem
816 * block size, and in that case we'd have to decrypt the final block, zero the
817 * portion past i_size, and re-encrypt it. (We *could* allow truncating to a
818 * filesystem block boundary, but it's simpler to just forbid all truncates ---
819 * and we already forbid all other contents modifications without the key.)
820 *
821 * Return: 0 on success, -ENOKEY if the key is missing, or another -errno code
822 * if a problem occurred while setting up the encryption key.
823 */
824static inline int fscrypt_prepare_setattr(struct dentry *dentry,
825 struct iattr *attr)
826{
827 if (attr->ia_valid & ATTR_SIZE)
828 return fscrypt_require_key(d_inode(dentry));
829 return 0;
830}
831
832/**
Olivier Deprez157378f2022-04-04 15:47:50 +0200833 * fscrypt_encrypt_symlink() - encrypt the symlink target if needed
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000834 * @inode: symlink inode
835 * @target: plaintext symlink target
836 * @len: length of @target excluding null terminator
837 * @disk_link: (in/out) the on-disk symlink target being prepared
838 *
839 * If the symlink target needs to be encrypted, then this function encrypts it
840 * into @disk_link->name. fscrypt_prepare_symlink() must have been called
841 * previously to compute @disk_link->len. If the filesystem did not allocate a
842 * buffer for @disk_link->name after calling fscrypt_prepare_link(), then one
843 * will be kmalloc()'ed and the filesystem will be responsible for freeing it.
844 *
845 * Return: 0 on success, -errno on failure
846 */
847static inline int fscrypt_encrypt_symlink(struct inode *inode,
848 const char *target,
849 unsigned int len,
850 struct fscrypt_str *disk_link)
851{
852 if (IS_ENCRYPTED(inode))
853 return __fscrypt_encrypt_symlink(inode, target, len, disk_link);
854 return 0;
855}
856
David Brazdil0f672f62019-12-10 10:32:29 +0000857/* If *pagep is a bounce page, free it and set *pagep to the pagecache page */
858static inline void fscrypt_finalize_bounce_page(struct page **pagep)
859{
860 struct page *page = *pagep;
861
862 if (fscrypt_is_bounce_page(page)) {
863 *pagep = fscrypt_pagecache_page(page);
864 fscrypt_free_bounce_page(page);
865 }
866}
867
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000868#endif /* _LINUX_FSCRYPT_H */