blob: 8e1d31c959bfaed8a21709b6bf6c8f6b09042df8 [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 Rajendra643fa962018-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 Rajendra643fa962018-12-12 15:20:12 +053017#include <linux/mm.h>
18#include <linux/slab.h>
Eric Biggers7af0ab02019-08-04 19:35:43 -070019#include <uapi/linux/fscrypt.h>
Eric Biggers46f47e42017-01-24 10:58:06 -080020
21#define FS_CRYPTO_BLOCK_SIZE 16
22
Eric Biggersac4acb12020-09-16 21:11:35 -070023union fscrypt_policy;
Eric Biggers46f47e42017-01-24 10:58:06 -080024struct fscrypt_info;
Eric Biggersed318a62020-05-12 16:32:50 -070025struct seq_file;
Eric Biggers46f47e42017-01-24 10:58:06 -080026
Eric Biggers46f47e42017-01-24 10:58:06 -080027struct 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;
Eric Biggers70fb2612020-09-23 21:26:23 -070038 bool is_nokey_name;
Eric Biggers46f47e42017-01-24 10:58:06 -080039};
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
Tahsin Erdoganaf652072017-07-06 00:01:59 -040046/* Maximum value for the third parameter of fscrypt_operations.set_context(). */
Eric Biggers5dae4602019-08-04 19:35:47 -070047#define FSCRYPT_SET_CONTEXT_MAX_SIZE 40
Tahsin Erdoganaf652072017-07-06 00:01:59 -040048
Chandan Rajendra643fa962018-12-12 15:20:12 +053049#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;
Eric Biggersfe015a72020-05-11 12:13:57 -070061 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);
Eric Biggersac4acb12020-09-16 21:11:35 -070064 const union fscrypt_policy *(*get_dummy_policy)(struct super_block *sb);
Eric Biggersfe015a72020-05-11 12:13:57 -070065 bool (*empty_dir)(struct inode *inode);
Chandan Rajendra643fa962018-12-12 15:20:12 +053066 unsigned int max_namelen;
Eric Biggersb103fb72019-10-24 14:54:36 -070067 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);
Satya Tangirala5fee3602020-07-02 01:56:05 +000070 int (*get_num_devices)(struct super_block *sb);
71 void (*get_devices)(struct super_block *sb,
72 struct request_queue **devs);
Chandan Rajendra643fa962018-12-12 15:20:12 +053073};
74
Eric Biggersab673b92020-07-21 15:59:19 -070075static inline struct fscrypt_info *fscrypt_get_info(const struct inode *inode)
Chandan Rajendra643fa962018-12-12 15:20:12 +053076{
Eric Biggersab673b92020-07-21 15:59:19 -070077 /*
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);
Chandan Rajendra643fa962018-12-12 15:20:12 +053084}
85
Eric Biggers56dce712019-12-09 12:50:21 -080086/**
87 * fscrypt_needs_contents_encryption() - check whether an inode needs
88 * contents encryption
Eric Biggersd2fe9752020-05-11 12:13:56 -070089 * @inode: the inode to check
Eric Biggers56dce712019-12-09 12:50:21 -080090 *
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)
98{
99 return IS_ENCRYPTED(inode) && S_ISREG(inode->i_mode);
100}
101
Eric Biggers0bf3d5c12019-03-20 11:39:11 -0700102/*
Eric Biggers501e43f2020-09-23 21:26:24 -0700103 * 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().
Eric Biggers0bf3d5c12019-03-20 11:39:11 -0700108 */
109static inline void fscrypt_handle_d_move(struct dentry *dentry)
110{
Eric Biggers501e43f2020-09-23 21:26:24 -0700111 dentry->d_flags &= ~DCACHE_NOKEY_NAME;
Eric Biggers0bf3d5c12019-03-20 11:39:11 -0700112}
113
Eric Biggers2da473e2020-11-17 23:56:05 -0800114/**
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{
140 return dentry->d_flags & DCACHE_NOKEY_NAME;
141}
142
Chandan Rajendra643fa962018-12-12 15:20:12 +0530143/* crypto.c */
Eric Biggers60700902020-05-11 12:13:58 -0700144void fscrypt_enqueue_decrypt_work(struct work_struct *);
Eric Biggers53bc1d852019-05-20 09:29:44 -0700145
Eric Biggers60700902020-05-11 12:13:58 -0700146struct 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);
Eric Biggersaa8bc1a2019-05-20 09:29:47 -0700153
Eric Biggers60700902020-05-11 12:13:58 -0700154int 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);
Chandan Rajendra643fa962018-12-12 15:20:12 +0530159
Eric Biggersd2d07272019-05-20 09:29:39 -0700160static inline bool fscrypt_is_bounce_page(struct page *page)
Chandan Rajendra643fa962018-12-12 15:20:12 +0530161{
Eric Biggersd2d07272019-05-20 09:29:39 -0700162 return page->mapping == NULL;
Chandan Rajendra643fa962018-12-12 15:20:12 +0530163}
164
Eric Biggersd2d07272019-05-20 09:29:39 -0700165static inline struct page *fscrypt_pagecache_page(struct page *bounce_page)
166{
167 return (struct page *)page_private(bounce_page);
168}
169
Eric Biggers60700902020-05-11 12:13:58 -0700170void fscrypt_free_bounce_page(struct page *bounce_page);
Chandan Rajendra643fa962018-12-12 15:20:12 +0530171
172/* policy.c */
Eric Biggers60700902020-05-11 12:13:58 -0700173int 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);
Eric Biggersa992b202020-09-16 21:11:24 -0700178int fscrypt_set_context(struct inode *inode, void *fs_data);
Eric Biggersfe015a72020-05-11 12:13:57 -0700179
Eric Biggersac4acb12020-09-16 21:11:35 -0700180struct fscrypt_dummy_policy {
181 const union fscrypt_policy *policy;
Eric Biggersed318a62020-05-12 16:32:50 -0700182};
183
Eric Biggersc8c868a2020-09-16 21:11:36 -0700184int fscrypt_set_test_dummy_encryption(struct super_block *sb, const char *arg,
Eric Biggersac4acb12020-09-16 21:11:35 -0700185 struct fscrypt_dummy_policy *dummy_policy);
Eric Biggersed318a62020-05-12 16:32:50 -0700186void fscrypt_show_test_dummy_encryption(struct seq_file *seq, char sep,
187 struct super_block *sb);
188static inline void
Eric Biggersac4acb12020-09-16 21:11:35 -0700189fscrypt_free_dummy_policy(struct fscrypt_dummy_policy *dummy_policy)
Eric Biggersed318a62020-05-12 16:32:50 -0700190{
Eric Biggersac4acb12020-09-16 21:11:35 -0700191 kfree(dummy_policy->policy);
192 dummy_policy->policy = NULL;
Eric Biggersed318a62020-05-12 16:32:50 -0700193}
194
Eric Biggers22d94f42019-08-04 19:35:46 -0700195/* keyring.c */
Eric Biggers60700902020-05-11 12:13:58 -0700196void 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);
Eric Biggers22d94f42019-08-04 19:35:46 -0700201
Eric Biggersfeed8252019-08-04 19:35:45 -0700202/* keysetup.c */
Eric Biggers60700902020-05-11 12:13:58 -0700203int fscrypt_get_encryption_info(struct inode *inode);
Eric Biggersa992b202020-09-16 21:11:24 -0700204int fscrypt_prepare_new_inode(struct inode *dir, struct inode *inode,
205 bool *encrypt_ret);
Eric Biggers60700902020-05-11 12:13:58 -0700206void fscrypt_put_encryption_info(struct inode *inode);
207void fscrypt_free_inode(struct inode *inode);
208int fscrypt_drop_inode(struct inode *inode);
Chandan Rajendra643fa962018-12-12 15:20:12 +0530209
210/* fname.c */
Eric Biggers60700902020-05-11 12:13:58 -0700211int fscrypt_setup_filename(struct inode *inode, const struct qstr *iname,
212 int lookup, struct fscrypt_name *fname);
Chandan Rajendra643fa962018-12-12 15:20:12 +0530213
214static inline void fscrypt_free_filename(struct fscrypt_name *fname)
215{
216 kfree(fname->crypto_buf.name);
217}
218
Jeff Layton8b10fe62020-08-10 10:21:39 -0400219int fscrypt_fname_alloc_buffer(u32 max_encrypted_len,
Eric Biggers60700902020-05-11 12:13:58 -0700220 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);
Eric Biggers5b2a8282020-09-23 22:47:21 -0700229int fscrypt_d_revalidate(struct dentry *dentry, unsigned int flags);
Daniel Rosenbergaa408f82020-01-20 14:31:57 -0800230
Chandan Rajendra643fa962018-12-12 15:20:12 +0530231/* bio.c */
Eric Biggers60700902020-05-11 12:13:58 -0700232void fscrypt_decrypt_bio(struct bio *bio);
233int fscrypt_zeroout_range(const struct inode *inode, pgoff_t lblk,
234 sector_t pblk, unsigned int len);
Chandan Rajendra643fa962018-12-12 15:20:12 +0530235
236/* hooks.c */
Eric Biggers60700902020-05-11 12:13:58 -0700237int 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);
Eric Biggers31114722020-09-16 21:11:34 -0700247int fscrypt_prepare_symlink(struct inode *dir, const char *target,
248 unsigned int len, unsigned int max_len,
249 struct fscrypt_str *disk_link);
Eric Biggers60700902020-05-11 12:13:58 -0700250int __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);
Sascha Hauereea2c052019-03-26 08:52:31 +0100255static inline void fscrypt_set_ops(struct super_block *sb,
256 const struct fscrypt_operations *s_cop)
257{
258 sb->s_cop = s_cop;
259}
Chandan Rajendra643fa962018-12-12 15:20:12 +0530260#else /* !CONFIG_FS_ENCRYPTION */
261
Eric Biggersab673b92020-07-21 15:59:19 -0700262static inline struct fscrypt_info *fscrypt_get_info(const struct inode *inode)
Chandan Rajendra643fa962018-12-12 15:20:12 +0530263{
Eric Biggersab673b92020-07-21 15:59:19 -0700264 return NULL;
Chandan Rajendra643fa962018-12-12 15:20:12 +0530265}
266
Eric Biggers56dce712019-12-09 12:50:21 -0800267static inline bool fscrypt_needs_contents_encryption(const struct inode *inode)
268{
269 return false;
270}
271
Eric Biggers0bf3d5c12019-03-20 11:39:11 -0700272static inline void fscrypt_handle_d_move(struct dentry *dentry)
273{
274}
275
Eric Biggers2da473e2020-11-17 23:56:05 -0800276static inline bool fscrypt_is_nokey_name(const struct dentry *dentry)
277{
278 return false;
279}
280
Chandan Rajendra643fa962018-12-12 15:20:12 +0530281/* crypto.c */
282static inline void fscrypt_enqueue_decrypt_work(struct work_struct *work)
283{
284}
285
Eric Biggers53bc1d852019-05-20 09:29:44 -0700286static inline struct page *fscrypt_encrypt_pagecache_blocks(struct page *page,
287 unsigned int len,
288 unsigned int offs,
289 gfp_t gfp_flags)
Chandan Rajendra643fa962018-12-12 15:20:12 +0530290{
291 return ERR_PTR(-EOPNOTSUPP);
292}
293
Eric Biggers03569f22019-05-20 09:29:43 -0700294static inline int fscrypt_encrypt_block_inplace(const struct inode *inode,
295 struct page *page,
296 unsigned int len,
297 unsigned int offs, u64 lblk_num,
298 gfp_t gfp_flags)
299{
300 return -EOPNOTSUPP;
301}
302
Eric Biggersaa8bc1a2019-05-20 09:29:47 -0700303static inline int fscrypt_decrypt_pagecache_blocks(struct page *page,
304 unsigned int len,
305 unsigned int offs)
Chandan Rajendra643fa962018-12-12 15:20:12 +0530306{
307 return -EOPNOTSUPP;
308}
309
Eric Biggers41adbcb2019-05-20 09:29:46 -0700310static inline int fscrypt_decrypt_block_inplace(const struct inode *inode,
311 struct page *page,
312 unsigned int len,
313 unsigned int offs, u64 lblk_num)
314{
315 return -EOPNOTSUPP;
316}
317
Eric Biggersd2d07272019-05-20 09:29:39 -0700318static inline bool fscrypt_is_bounce_page(struct page *page)
319{
320 return false;
321}
322
323static inline struct page *fscrypt_pagecache_page(struct page *bounce_page)
Chandan Rajendra643fa962018-12-12 15:20:12 +0530324{
325 WARN_ON_ONCE(1);
326 return ERR_PTR(-EINVAL);
327}
328
Eric Biggersd2d07272019-05-20 09:29:39 -0700329static inline void fscrypt_free_bounce_page(struct page *bounce_page)
Chandan Rajendra643fa962018-12-12 15:20:12 +0530330{
Chandan Rajendra643fa962018-12-12 15:20:12 +0530331}
332
333/* policy.c */
334static inline int fscrypt_ioctl_set_policy(struct file *filp,
335 const void __user *arg)
336{
337 return -EOPNOTSUPP;
338}
339
340static inline int fscrypt_ioctl_get_policy(struct file *filp, void __user *arg)
341{
342 return -EOPNOTSUPP;
343}
344
Eric Biggers5dae4602019-08-04 19:35:47 -0700345static inline int fscrypt_ioctl_get_policy_ex(struct file *filp,
346 void __user *arg)
347{
348 return -EOPNOTSUPP;
349}
350
Eric Biggerse98ad462020-03-14 13:50:49 -0700351static inline int fscrypt_ioctl_get_nonce(struct file *filp, void __user *arg)
352{
353 return -EOPNOTSUPP;
354}
355
Chandan Rajendra643fa962018-12-12 15:20:12 +0530356static inline int fscrypt_has_permitted_context(struct inode *parent,
357 struct inode *child)
358{
359 return 0;
360}
361
Eric Biggersa992b202020-09-16 21:11:24 -0700362static inline int fscrypt_set_context(struct inode *inode, void *fs_data)
363{
364 return -EOPNOTSUPP;
365}
366
Eric Biggersac4acb12020-09-16 21:11:35 -0700367struct fscrypt_dummy_policy {
Eric Biggersed318a62020-05-12 16:32:50 -0700368};
369
370static inline void fscrypt_show_test_dummy_encryption(struct seq_file *seq,
371 char sep,
372 struct super_block *sb)
373{
374}
375
376static inline void
Eric Biggersac4acb12020-09-16 21:11:35 -0700377fscrypt_free_dummy_policy(struct fscrypt_dummy_policy *dummy_policy)
Eric Biggersed318a62020-05-12 16:32:50 -0700378{
379}
380
Eric Biggers22d94f42019-08-04 19:35:46 -0700381/* keyring.c */
382static inline void fscrypt_sb_free(struct super_block *sb)
383{
384}
385
386static inline int fscrypt_ioctl_add_key(struct file *filp, void __user *arg)
387{
388 return -EOPNOTSUPP;
389}
390
Eric Biggersb1c0ec32019-08-04 19:35:46 -0700391static inline int fscrypt_ioctl_remove_key(struct file *filp, void __user *arg)
392{
393 return -EOPNOTSUPP;
394}
395
Eric Biggers78a1b96b2019-08-04 19:35:47 -0700396static inline int fscrypt_ioctl_remove_key_all_users(struct file *filp,
397 void __user *arg)
398{
399 return -EOPNOTSUPP;
400}
401
Eric Biggers5a7e2992019-08-04 19:35:46 -0700402static inline int fscrypt_ioctl_get_key_status(struct file *filp,
403 void __user *arg)
404{
405 return -EOPNOTSUPP;
406}
407
Eric Biggersfeed8252019-08-04 19:35:45 -0700408/* keysetup.c */
Chandan Rajendra643fa962018-12-12 15:20:12 +0530409static inline int fscrypt_get_encryption_info(struct inode *inode)
410{
411 return -EOPNOTSUPP;
412}
413
Eric Biggersa992b202020-09-16 21:11:24 -0700414static inline int fscrypt_prepare_new_inode(struct inode *dir,
415 struct inode *inode,
416 bool *encrypt_ret)
417{
418 if (IS_ENCRYPTED(dir))
419 return -EOPNOTSUPP;
420 return 0;
421}
422
Chandan Rajendra643fa962018-12-12 15:20:12 +0530423static inline void fscrypt_put_encryption_info(struct inode *inode)
424{
425 return;
426}
427
Eric Biggers2c58d542019-04-10 13:21:15 -0700428static inline void fscrypt_free_inode(struct inode *inode)
429{
430}
431
Eric Biggersb1c0ec32019-08-04 19:35:46 -0700432static inline int fscrypt_drop_inode(struct inode *inode)
433{
434 return 0;
435}
436
Chandan Rajendra643fa962018-12-12 15:20:12 +0530437 /* fname.c */
438static inline int fscrypt_setup_filename(struct inode *dir,
439 const struct qstr *iname,
440 int lookup, struct fscrypt_name *fname)
441{
442 if (IS_ENCRYPTED(dir))
443 return -EOPNOTSUPP;
444
Eric Biggersb01531d2019-03-20 11:39:13 -0700445 memset(fname, 0, sizeof(*fname));
Chandan Rajendra643fa962018-12-12 15:20:12 +0530446 fname->usr_fname = iname;
447 fname->disk_name.name = (unsigned char *)iname->name;
448 fname->disk_name.len = iname->len;
449 return 0;
450}
451
452static inline void fscrypt_free_filename(struct fscrypt_name *fname)
453{
454 return;
455}
456
Jeff Layton8b10fe62020-08-10 10:21:39 -0400457static inline int fscrypt_fname_alloc_buffer(u32 max_encrypted_len,
Chandan Rajendra643fa962018-12-12 15:20:12 +0530458 struct fscrypt_str *crypto_str)
459{
460 return -EOPNOTSUPP;
461}
462
463static inline void fscrypt_fname_free_buffer(struct fscrypt_str *crypto_str)
464{
465 return;
466}
467
Eric Biggers8a4ab0b2019-12-15 13:39:47 -0800468static inline int fscrypt_fname_disk_to_usr(const struct inode *inode,
Chandan Rajendra643fa962018-12-12 15:20:12 +0530469 u32 hash, u32 minor_hash,
470 const struct fscrypt_str *iname,
471 struct fscrypt_str *oname)
472{
473 return -EOPNOTSUPP;
474}
475
476static inline bool fscrypt_match_name(const struct fscrypt_name *fname,
477 const u8 *de_name, u32 de_name_len)
478{
479 /* Encryption support disabled; use standard comparison */
480 if (de_name_len != fname->disk_name.len)
481 return false;
482 return !memcmp(de_name, fname->disk_name.name, fname->disk_name.len);
483}
484
Daniel Rosenbergaa408f82020-01-20 14:31:57 -0800485static inline u64 fscrypt_fname_siphash(const struct inode *dir,
486 const struct qstr *name)
487{
488 WARN_ON_ONCE(1);
489 return 0;
490}
491
Eric Biggers5b2a8282020-09-23 22:47:21 -0700492static inline int fscrypt_d_revalidate(struct dentry *dentry,
493 unsigned int flags)
494{
495 return 1;
496}
497
Chandan Rajendra643fa962018-12-12 15:20:12 +0530498/* bio.c */
499static inline void fscrypt_decrypt_bio(struct bio *bio)
500{
501}
502
Chandan Rajendra643fa962018-12-12 15:20:12 +0530503static inline int fscrypt_zeroout_range(const struct inode *inode, pgoff_t lblk,
504 sector_t pblk, unsigned int len)
505{
506 return -EOPNOTSUPP;
507}
508
509/* hooks.c */
510
511static inline int fscrypt_file_open(struct inode *inode, struct file *filp)
512{
513 if (IS_ENCRYPTED(inode))
514 return -EOPNOTSUPP;
515 return 0;
516}
517
Eric Biggers968dd6d2019-03-20 11:39:10 -0700518static inline int __fscrypt_prepare_link(struct inode *inode, struct inode *dir,
519 struct dentry *dentry)
Chandan Rajendra643fa962018-12-12 15:20:12 +0530520{
521 return -EOPNOTSUPP;
522}
523
524static inline int __fscrypt_prepare_rename(struct inode *old_dir,
525 struct dentry *old_dentry,
526 struct inode *new_dir,
527 struct dentry *new_dentry,
528 unsigned int flags)
529{
530 return -EOPNOTSUPP;
531}
532
533static inline int __fscrypt_prepare_lookup(struct inode *dir,
Eric Biggersb01531d2019-03-20 11:39:13 -0700534 struct dentry *dentry,
535 struct fscrypt_name *fname)
Chandan Rajendra643fa962018-12-12 15:20:12 +0530536{
537 return -EOPNOTSUPP;
538}
539
Daniel Rosenberg6e1918c2020-01-20 14:31:56 -0800540static inline int fscrypt_prepare_setflags(struct inode *inode,
541 unsigned int oldflags,
542 unsigned int flags)
543{
544 return 0;
545}
546
Eric Biggers31114722020-09-16 21:11:34 -0700547static inline int fscrypt_prepare_symlink(struct inode *dir,
548 const char *target,
549 unsigned int len,
550 unsigned int max_len,
551 struct fscrypt_str *disk_link)
Chandan Rajendra643fa962018-12-12 15:20:12 +0530552{
Eric Biggers31114722020-09-16 21:11:34 -0700553 if (IS_ENCRYPTED(dir))
554 return -EOPNOTSUPP;
555 disk_link->name = (unsigned char *)target;
556 disk_link->len = len + 1;
557 if (disk_link->len > max_len)
558 return -ENAMETOOLONG;
559 return 0;
Chandan Rajendra643fa962018-12-12 15:20:12 +0530560}
561
Chandan Rajendra643fa962018-12-12 15:20:12 +0530562static inline int __fscrypt_encrypt_symlink(struct inode *inode,
563 const char *target,
564 unsigned int len,
565 struct fscrypt_str *disk_link)
566{
567 return -EOPNOTSUPP;
568}
569
570static inline const char *fscrypt_get_symlink(struct inode *inode,
571 const void *caddr,
572 unsigned int max_size,
573 struct delayed_call *done)
574{
575 return ERR_PTR(-EOPNOTSUPP);
576}
Sascha Hauereea2c052019-03-26 08:52:31 +0100577
578static inline void fscrypt_set_ops(struct super_block *sb,
579 const struct fscrypt_operations *s_cop)
580{
581}
582
Chandan Rajendra643fa962018-12-12 15:20:12 +0530583#endif /* !CONFIG_FS_ENCRYPTION */
Dave Chinner734f0d22017-10-09 12:15:34 -0700584
Satya Tangirala5fee3602020-07-02 01:56:05 +0000585/* inline_crypt.c */
586#ifdef CONFIG_FS_ENCRYPTION_INLINE_CRYPT
587
588bool __fscrypt_inode_uses_inline_crypto(const struct inode *inode);
589
590void fscrypt_set_bio_crypt_ctx(struct bio *bio,
591 const struct inode *inode, u64 first_lblk,
592 gfp_t gfp_mask);
593
594void fscrypt_set_bio_crypt_ctx_bh(struct bio *bio,
595 const struct buffer_head *first_bh,
596 gfp_t gfp_mask);
597
598bool fscrypt_mergeable_bio(struct bio *bio, const struct inode *inode,
599 u64 next_lblk);
600
601bool fscrypt_mergeable_bio_bh(struct bio *bio,
602 const struct buffer_head *next_bh);
603
604#else /* CONFIG_FS_ENCRYPTION_INLINE_CRYPT */
605
606static inline bool __fscrypt_inode_uses_inline_crypto(const struct inode *inode)
607{
608 return false;
609}
610
611static inline void fscrypt_set_bio_crypt_ctx(struct bio *bio,
612 const struct inode *inode,
613 u64 first_lblk, gfp_t gfp_mask) { }
614
615static inline void fscrypt_set_bio_crypt_ctx_bh(
616 struct bio *bio,
617 const struct buffer_head *first_bh,
618 gfp_t gfp_mask) { }
619
620static inline bool fscrypt_mergeable_bio(struct bio *bio,
621 const struct inode *inode,
622 u64 next_lblk)
623{
624 return true;
625}
626
627static inline bool fscrypt_mergeable_bio_bh(struct bio *bio,
628 const struct buffer_head *next_bh)
629{
630 return true;
631}
632#endif /* !CONFIG_FS_ENCRYPTION_INLINE_CRYPT */
633
634/**
635 * fscrypt_inode_uses_inline_crypto() - test whether an inode uses inline
636 * encryption
637 * @inode: an inode. If encrypted, its key must be set up.
638 *
639 * Return: true if the inode requires file contents encryption and if the
640 * encryption should be done in the block layer via blk-crypto rather
641 * than in the filesystem layer.
642 */
643static inline bool fscrypt_inode_uses_inline_crypto(const struct inode *inode)
644{
645 return fscrypt_needs_contents_encryption(inode) &&
646 __fscrypt_inode_uses_inline_crypto(inode);
647}
648
649/**
650 * fscrypt_inode_uses_fs_layer_crypto() - test whether an inode uses fs-layer
651 * encryption
652 * @inode: an inode. If encrypted, its key must be set up.
653 *
654 * Return: true if the inode requires file contents encryption and if the
655 * encryption should be done in the filesystem layer rather than in the
656 * block layer via blk-crypto.
657 */
658static inline bool fscrypt_inode_uses_fs_layer_crypto(const struct inode *inode)
659{
660 return fscrypt_needs_contents_encryption(inode) &&
661 !__fscrypt_inode_uses_inline_crypto(inode);
662}
663
Eric Biggersd293c3e2017-10-09 12:15:39 -0700664/**
Eric Biggersab673b92020-07-21 15:59:19 -0700665 * fscrypt_has_encryption_key() - check whether an inode has had its key set up
666 * @inode: the inode to check
667 *
668 * Return: %true if the inode has had its encryption key set up, else %false.
669 *
670 * Usually this should be preceded by fscrypt_get_encryption_info() to try to
671 * set up the key first.
672 */
673static inline bool fscrypt_has_encryption_key(const struct inode *inode)
674{
675 return fscrypt_get_info(inode) != NULL;
676}
677
678/**
Eric Biggersd2fe9752020-05-11 12:13:56 -0700679 * fscrypt_require_key() - require an inode's encryption key
Eric Biggersd293c3e2017-10-09 12:15:39 -0700680 * @inode: the inode we need the key for
681 *
682 * If the inode is encrypted, set up its encryption key if not already done.
683 * Then require that the key be present and return -ENOKEY otherwise.
684 *
685 * No locks are needed, and the key will live as long as the struct inode --- so
686 * it won't go away from under you.
687 *
688 * Return: 0 on success, -ENOKEY if the key is missing, or another -errno code
689 * if a problem occurred while setting up the encryption key.
690 */
691static inline int fscrypt_require_key(struct inode *inode)
692{
693 if (IS_ENCRYPTED(inode)) {
694 int err = fscrypt_get_encryption_info(inode);
695
696 if (err)
697 return err;
698 if (!fscrypt_has_encryption_key(inode))
699 return -ENOKEY;
700 }
701 return 0;
702}
Dave Chinner734f0d22017-10-09 12:15:34 -0700703
Eric Biggers0ea87a92017-10-09 12:15:41 -0700704/**
Eric Biggersd2fe9752020-05-11 12:13:56 -0700705 * fscrypt_prepare_link() - prepare to link an inode into a possibly-encrypted
706 * directory
Eric Biggers0ea87a92017-10-09 12:15:41 -0700707 * @old_dentry: an existing dentry for the inode being linked
708 * @dir: the target directory
709 * @dentry: negative dentry for the target filename
710 *
711 * A new link can only be added to an encrypted directory if the directory's
712 * encryption key is available --- since otherwise we'd have no way to encrypt
713 * the filename. Therefore, we first set up the directory's encryption key (if
714 * not already done) and return an error if it's unavailable.
715 *
716 * We also verify that the link will not violate the constraint that all files
717 * in an encrypted directory tree use the same encryption policy.
718 *
719 * Return: 0 on success, -ENOKEY if the directory's encryption key is missing,
Eric Biggersf5e55e72019-01-22 16:20:21 -0800720 * -EXDEV if the link would result in an inconsistent encryption policy, or
Eric Biggers0ea87a92017-10-09 12:15:41 -0700721 * another -errno code.
722 */
723static inline int fscrypt_prepare_link(struct dentry *old_dentry,
724 struct inode *dir,
725 struct dentry *dentry)
726{
727 if (IS_ENCRYPTED(dir))
Eric Biggers968dd6d2019-03-20 11:39:10 -0700728 return __fscrypt_prepare_link(d_inode(old_dentry), dir, dentry);
Eric Biggers0ea87a92017-10-09 12:15:41 -0700729 return 0;
730}
731
Eric Biggers94b26f32017-10-09 12:15:42 -0700732/**
Eric Biggersd2fe9752020-05-11 12:13:56 -0700733 * fscrypt_prepare_rename() - prepare for a rename between possibly-encrypted
734 * directories
Eric Biggers94b26f32017-10-09 12:15:42 -0700735 * @old_dir: source directory
736 * @old_dentry: dentry for source file
737 * @new_dir: target directory
738 * @new_dentry: dentry for target location (may be negative unless exchanging)
739 * @flags: rename flags (we care at least about %RENAME_EXCHANGE)
740 *
741 * Prepare for ->rename() where the source and/or target directories may be
742 * encrypted. A new link can only be added to an encrypted directory if the
743 * directory's encryption key is available --- since otherwise we'd have no way
744 * to encrypt the filename. A rename to an existing name, on the other hand,
745 * *is* cryptographically possible without the key. However, we take the more
746 * conservative approach and just forbid all no-key renames.
747 *
748 * We also verify that the rename will not violate the constraint that all files
749 * in an encrypted directory tree use the same encryption policy.
750 *
Eric Biggersf5e55e72019-01-22 16:20:21 -0800751 * Return: 0 on success, -ENOKEY if an encryption key is missing, -EXDEV if the
Eric Biggers94b26f32017-10-09 12:15:42 -0700752 * rename would cause inconsistent encryption policies, or another -errno code.
753 */
754static inline int fscrypt_prepare_rename(struct inode *old_dir,
755 struct dentry *old_dentry,
756 struct inode *new_dir,
757 struct dentry *new_dentry,
758 unsigned int flags)
759{
760 if (IS_ENCRYPTED(old_dir) || IS_ENCRYPTED(new_dir))
761 return __fscrypt_prepare_rename(old_dir, old_dentry,
762 new_dir, new_dentry, flags);
763 return 0;
764}
765
Eric Biggers32c3cf02017-10-09 12:15:43 -0700766/**
Eric Biggersd2fe9752020-05-11 12:13:56 -0700767 * fscrypt_prepare_lookup() - prepare to lookup a name in a possibly-encrypted
768 * directory
Eric Biggers32c3cf02017-10-09 12:15:43 -0700769 * @dir: directory being searched
770 * @dentry: filename being looked up
Eric Biggersb01531d2019-03-20 11:39:13 -0700771 * @fname: (output) the name to use to search the on-disk directory
Eric Biggers32c3cf02017-10-09 12:15:43 -0700772 *
Eric Biggersb01531d2019-03-20 11:39:13 -0700773 * Prepare for ->lookup() in a directory which may be encrypted by determining
Eric Biggers70fb2612020-09-23 21:26:23 -0700774 * the name that will actually be used to search the directory on-disk. If the
775 * directory's encryption key is available, then the lookup is assumed to be by
776 * plaintext name; otherwise, it is assumed to be by no-key name.
Eric Biggers32c3cf02017-10-09 12:15:43 -0700777 *
Eric Biggers6cc24862019-03-20 11:39:09 -0700778 * This also installs a custom ->d_revalidate() method which will invalidate the
779 * dentry if it was created without the key and the key is later added.
Eric Biggers32c3cf02017-10-09 12:15:43 -0700780 *
Eric Biggers70fb2612020-09-23 21:26:23 -0700781 * Return: 0 on success; -ENOENT if the directory's key is unavailable but the
782 * filename isn't a valid no-key name, so a negative dentry should be created;
783 * or another -errno code.
Eric Biggers32c3cf02017-10-09 12:15:43 -0700784 */
785static inline int fscrypt_prepare_lookup(struct inode *dir,
786 struct dentry *dentry,
Eric Biggersb01531d2019-03-20 11:39:13 -0700787 struct fscrypt_name *fname)
Eric Biggers32c3cf02017-10-09 12:15:43 -0700788{
789 if (IS_ENCRYPTED(dir))
Eric Biggersb01531d2019-03-20 11:39:13 -0700790 return __fscrypt_prepare_lookup(dir, dentry, fname);
791
792 memset(fname, 0, sizeof(*fname));
793 fname->usr_fname = &dentry->d_name;
794 fname->disk_name.name = (unsigned char *)dentry->d_name.name;
795 fname->disk_name.len = dentry->d_name.len;
Eric Biggers32c3cf02017-10-09 12:15:43 -0700796 return 0;
797}
798
Eric Biggers815dac32017-10-09 12:15:44 -0700799/**
Eric Biggersd2fe9752020-05-11 12:13:56 -0700800 * fscrypt_prepare_setattr() - prepare to change a possibly-encrypted inode's
801 * attributes
Eric Biggers815dac32017-10-09 12:15:44 -0700802 * @dentry: dentry through which the inode is being changed
803 * @attr: attributes to change
804 *
805 * Prepare for ->setattr() on a possibly-encrypted inode. On an encrypted file,
806 * most attribute changes are allowed even without the encryption key. However,
807 * without the encryption key we do have to forbid truncates. This is needed
808 * because the size being truncated to may not be a multiple of the filesystem
809 * block size, and in that case we'd have to decrypt the final block, zero the
810 * portion past i_size, and re-encrypt it. (We *could* allow truncating to a
811 * filesystem block boundary, but it's simpler to just forbid all truncates ---
812 * and we already forbid all other contents modifications without the key.)
813 *
814 * Return: 0 on success, -ENOKEY if the key is missing, or another -errno code
815 * if a problem occurred while setting up the encryption key.
816 */
817static inline int fscrypt_prepare_setattr(struct dentry *dentry,
818 struct iattr *attr)
819{
820 if (attr->ia_valid & ATTR_SIZE)
821 return fscrypt_require_key(d_inode(dentry));
822 return 0;
823}
824
Eric Biggers76e81d62018-01-05 10:45:01 -0800825/**
Eric Biggersd2fe9752020-05-11 12:13:56 -0700826 * fscrypt_encrypt_symlink() - encrypt the symlink target if needed
Eric Biggers76e81d62018-01-05 10:45:01 -0800827 * @inode: symlink inode
828 * @target: plaintext symlink target
829 * @len: length of @target excluding null terminator
830 * @disk_link: (in/out) the on-disk symlink target being prepared
831 *
832 * If the symlink target needs to be encrypted, then this function encrypts it
833 * into @disk_link->name. fscrypt_prepare_symlink() must have been called
834 * previously to compute @disk_link->len. If the filesystem did not allocate a
835 * buffer for @disk_link->name after calling fscrypt_prepare_link(), then one
836 * will be kmalloc()'ed and the filesystem will be responsible for freeing it.
837 *
838 * Return: 0 on success, -errno on failure
839 */
840static inline int fscrypt_encrypt_symlink(struct inode *inode,
841 const char *target,
842 unsigned int len,
843 struct fscrypt_str *disk_link)
844{
845 if (IS_ENCRYPTED(inode))
846 return __fscrypt_encrypt_symlink(inode, target, len, disk_link);
847 return 0;
848}
849
Eric Biggersd2d07272019-05-20 09:29:39 -0700850/* If *pagep is a bounce page, free it and set *pagep to the pagecache page */
851static inline void fscrypt_finalize_bounce_page(struct page **pagep)
852{
853 struct page *page = *pagep;
854
855 if (fscrypt_is_bounce_page(page)) {
856 *pagep = fscrypt_pagecache_page(page);
857 fscrypt_free_bounce_page(page);
858 }
859}
860
Dave Chinner734f0d22017-10-09 12:15:34 -0700861#endif /* _LINUX_FSCRYPT_H */