blob: 219f28bf97ca2da1de31319835e8f73c04352e80 [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 Biggersafca03c2019-10-24 14:43: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 Tangiralacfd7e6c2019-12-17 14:26:29 -080070 int (*get_num_devices)(struct super_block *sb);
71 void (*get_devices)(struct super_block *sb,
72 struct request_queue **devs);
Eric Biggers194fd922021-07-02 10:25:47 -070073
74 ANDROID_KABI_RESERVE(1);
75 ANDROID_KABI_RESERVE(2);
76 ANDROID_KABI_RESERVE(3);
77 ANDROID_KABI_RESERVE(4);
Eric Biggers2ed5fbf2021-07-02 10:25:48 -070078
79 ANDROID_OEM_DATA_ARRAY(1, 4);
Chandan Rajendra643fa962018-12-12 15:20:12 +053080};
81
Eric Biggersab673b92020-07-21 15:59:19 -070082static inline struct fscrypt_info *fscrypt_get_info(const struct inode *inode)
Chandan Rajendra643fa962018-12-12 15:20:12 +053083{
Eric Biggersab673b92020-07-21 15:59:19 -070084 /*
Eric Biggers94bb3de2020-12-02 18:20:40 -080085 * Pairs with the cmpxchg_release() in fscrypt_setup_encryption_info().
Eric Biggersab673b92020-07-21 15:59:19 -070086 * I.e., another task may publish ->i_crypt_info concurrently, executing
87 * a RELEASE barrier. We need to use smp_load_acquire() here to safely
88 * ACQUIRE the memory the other task published.
89 */
90 return smp_load_acquire(&inode->i_crypt_info);
Chandan Rajendra643fa962018-12-12 15:20:12 +053091}
92
Eric Biggers56dce712019-12-09 12:50:21 -080093/**
94 * fscrypt_needs_contents_encryption() - check whether an inode needs
95 * contents encryption
Eric Biggersd2fe9752020-05-11 12:13:56 -070096 * @inode: the inode to check
Eric Biggers56dce712019-12-09 12:50:21 -080097 *
98 * Return: %true iff the inode is an encrypted regular file and the kernel was
99 * built with fscrypt support.
100 *
101 * If you need to know whether the encrypt bit is set even when the kernel was
102 * built without fscrypt support, you must use IS_ENCRYPTED() directly instead.
103 */
104static inline bool fscrypt_needs_contents_encryption(const struct inode *inode)
105{
106 return IS_ENCRYPTED(inode) && S_ISREG(inode->i_mode);
107}
108
Eric Biggers0bf3d5c12019-03-20 11:39:11 -0700109/*
Eric Biggers501e43f2020-09-23 21:26:24 -0700110 * When d_splice_alias() moves a directory's no-key alias to its plaintext alias
111 * as a result of the encryption key being added, DCACHE_NOKEY_NAME must be
112 * cleared. Note that we don't have to support arbitrary moves of this flag
113 * because fscrypt doesn't allow no-key names to be the source or target of a
114 * rename().
Eric Biggers0bf3d5c12019-03-20 11:39:11 -0700115 */
116static inline void fscrypt_handle_d_move(struct dentry *dentry)
117{
Eric Biggers501e43f2020-09-23 21:26:24 -0700118 dentry->d_flags &= ~DCACHE_NOKEY_NAME;
Eric Biggers0bf3d5c12019-03-20 11:39:11 -0700119}
120
Eric Biggers2da473e2020-11-17 23:56:05 -0800121/**
122 * fscrypt_is_nokey_name() - test whether a dentry is a no-key name
123 * @dentry: the dentry to check
124 *
125 * This returns true if the dentry is a no-key dentry. A no-key dentry is a
126 * dentry that was created in an encrypted directory that hasn't had its
127 * encryption key added yet. Such dentries may be either positive or negative.
128 *
129 * When a filesystem is asked to create a new filename in an encrypted directory
130 * and the new filename's dentry is a no-key dentry, it must fail the operation
131 * with ENOKEY. This includes ->create(), ->mkdir(), ->mknod(), ->symlink(),
132 * ->rename(), and ->link(). (However, ->rename() and ->link() are already
133 * handled by fscrypt_prepare_rename() and fscrypt_prepare_link().)
134 *
135 * This is necessary because creating a filename requires the directory's
136 * encryption key, but just checking for the key on the directory inode during
137 * the final filesystem operation doesn't guarantee that the key was available
138 * during the preceding dentry lookup. And the key must have already been
139 * available during the dentry lookup in order for it to have been checked
140 * whether the filename already exists in the directory and for the new file's
141 * dentry not to be invalidated due to it incorrectly having the no-key flag.
142 *
143 * Return: %true if the dentry is a no-key name
144 */
145static inline bool fscrypt_is_nokey_name(const struct dentry *dentry)
146{
147 return dentry->d_flags & DCACHE_NOKEY_NAME;
148}
149
Chandan Rajendra643fa962018-12-12 15:20:12 +0530150/* crypto.c */
Eric Biggers60700902020-05-11 12:13:58 -0700151void fscrypt_enqueue_decrypt_work(struct work_struct *);
Eric Biggers53bc1d852019-05-20 09:29:44 -0700152
Eric Biggers60700902020-05-11 12:13:58 -0700153struct page *fscrypt_encrypt_pagecache_blocks(struct page *page,
154 unsigned int len,
155 unsigned int offs,
156 gfp_t gfp_flags);
157int fscrypt_encrypt_block_inplace(const struct inode *inode, struct page *page,
158 unsigned int len, unsigned int offs,
159 u64 lblk_num, gfp_t gfp_flags);
Eric Biggersaa8bc1a2019-05-20 09:29:47 -0700160
Eric Biggers60700902020-05-11 12:13:58 -0700161int fscrypt_decrypt_pagecache_blocks(struct page *page, unsigned int len,
162 unsigned int offs);
163int fscrypt_decrypt_block_inplace(const struct inode *inode, struct page *page,
164 unsigned int len, unsigned int offs,
165 u64 lblk_num);
Chandan Rajendra643fa962018-12-12 15:20:12 +0530166
Eric Biggersd2d07272019-05-20 09:29:39 -0700167static inline bool fscrypt_is_bounce_page(struct page *page)
Chandan Rajendra643fa962018-12-12 15:20:12 +0530168{
Eric Biggersd2d07272019-05-20 09:29:39 -0700169 return page->mapping == NULL;
Chandan Rajendra643fa962018-12-12 15:20:12 +0530170}
171
Eric Biggersd2d07272019-05-20 09:29:39 -0700172static inline struct page *fscrypt_pagecache_page(struct page *bounce_page)
173{
174 return (struct page *)page_private(bounce_page);
175}
176
Eric Biggers60700902020-05-11 12:13:58 -0700177void fscrypt_free_bounce_page(struct page *bounce_page);
Chandan Rajendra643fa962018-12-12 15:20:12 +0530178
179/* policy.c */
Eric Biggers60700902020-05-11 12:13:58 -0700180int fscrypt_ioctl_set_policy(struct file *filp, const void __user *arg);
181int fscrypt_ioctl_get_policy(struct file *filp, void __user *arg);
182int fscrypt_ioctl_get_policy_ex(struct file *filp, void __user *arg);
183int fscrypt_ioctl_get_nonce(struct file *filp, void __user *arg);
184int fscrypt_has_permitted_context(struct inode *parent, struct inode *child);
Eric Biggersa992b202020-09-16 21:11:24 -0700185int fscrypt_set_context(struct inode *inode, void *fs_data);
Eric Biggersfe015a72020-05-11 12:13:57 -0700186
Eric Biggersac4acb12020-09-16 21:11:35 -0700187struct fscrypt_dummy_policy {
188 const union fscrypt_policy *policy;
Eric Biggersed318a62020-05-12 16:32:50 -0700189};
190
Eric Biggersc8c868a2020-09-16 21:11:36 -0700191int fscrypt_set_test_dummy_encryption(struct super_block *sb, const char *arg,
Eric Biggersac4acb12020-09-16 21:11:35 -0700192 struct fscrypt_dummy_policy *dummy_policy);
Eric Biggersed318a62020-05-12 16:32:50 -0700193void fscrypt_show_test_dummy_encryption(struct seq_file *seq, char sep,
194 struct super_block *sb);
195static inline void
Eric Biggersac4acb12020-09-16 21:11:35 -0700196fscrypt_free_dummy_policy(struct fscrypt_dummy_policy *dummy_policy)
Eric Biggersed318a62020-05-12 16:32:50 -0700197{
Eric Biggersac4acb12020-09-16 21:11:35 -0700198 kfree(dummy_policy->policy);
199 dummy_policy->policy = NULL;
Eric Biggersed318a62020-05-12 16:32:50 -0700200}
201
Eric Biggers22d94f42019-08-04 19:35:46 -0700202/* keyring.c */
Eric Biggers60700902020-05-11 12:13:58 -0700203void fscrypt_sb_free(struct super_block *sb);
204int fscrypt_ioctl_add_key(struct file *filp, void __user *arg);
205int fscrypt_ioctl_remove_key(struct file *filp, void __user *arg);
206int fscrypt_ioctl_remove_key_all_users(struct file *filp, void __user *arg);
207int fscrypt_ioctl_get_key_status(struct file *filp, void __user *arg);
Eric Biggers22d94f42019-08-04 19:35:46 -0700208
Eric Biggersfeed8252019-08-04 19:35:45 -0700209/* keysetup.c */
Eric Biggersa992b202020-09-16 21:11:24 -0700210int fscrypt_prepare_new_inode(struct inode *dir, struct inode *inode,
211 bool *encrypt_ret);
Eric Biggers60700902020-05-11 12:13:58 -0700212void fscrypt_put_encryption_info(struct inode *inode);
213void fscrypt_free_inode(struct inode *inode);
214int fscrypt_drop_inode(struct inode *inode);
Chandan Rajendra643fa962018-12-12 15:20:12 +0530215
216/* fname.c */
Eric Biggers60700902020-05-11 12:13:58 -0700217int fscrypt_setup_filename(struct inode *inode, const struct qstr *iname,
218 int lookup, struct fscrypt_name *fname);
Chandan Rajendra643fa962018-12-12 15:20:12 +0530219
220static inline void fscrypt_free_filename(struct fscrypt_name *fname)
221{
222 kfree(fname->crypto_buf.name);
223}
224
Jeff Layton8b10fe62020-08-10 10:21:39 -0400225int fscrypt_fname_alloc_buffer(u32 max_encrypted_len,
Eric Biggers60700902020-05-11 12:13:58 -0700226 struct fscrypt_str *crypto_str);
227void fscrypt_fname_free_buffer(struct fscrypt_str *crypto_str);
228int fscrypt_fname_disk_to_usr(const struct inode *inode,
229 u32 hash, u32 minor_hash,
230 const struct fscrypt_str *iname,
231 struct fscrypt_str *oname);
232bool fscrypt_match_name(const struct fscrypt_name *fname,
233 const u8 *de_name, u32 de_name_len);
234u64 fscrypt_fname_siphash(const struct inode *dir, const struct qstr *name);
Eric Biggers5b2a8282020-09-23 22:47:21 -0700235int fscrypt_d_revalidate(struct dentry *dentry, unsigned int flags);
Chandan Rajendra643fa962018-12-12 15:20:12 +0530236
237/* bio.c */
Eric Biggers60700902020-05-11 12:13:58 -0700238void fscrypt_decrypt_bio(struct bio *bio);
239int fscrypt_zeroout_range(const struct inode *inode, pgoff_t lblk,
240 sector_t pblk, unsigned int len);
Chandan Rajendra643fa962018-12-12 15:20:12 +0530241
242/* hooks.c */
Eric Biggers60700902020-05-11 12:13:58 -0700243int fscrypt_file_open(struct inode *inode, struct file *filp);
244int __fscrypt_prepare_link(struct inode *inode, struct inode *dir,
245 struct dentry *dentry);
246int __fscrypt_prepare_rename(struct inode *old_dir, struct dentry *old_dentry,
247 struct inode *new_dir, struct dentry *new_dentry,
248 unsigned int flags);
249int __fscrypt_prepare_lookup(struct inode *dir, struct dentry *dentry,
250 struct fscrypt_name *fname);
Eric Biggers4af7c342020-12-02 18:20:37 -0800251int __fscrypt_prepare_readdir(struct inode *dir);
Eric Biggers9aa0e0a2020-12-02 18:20:38 -0800252int __fscrypt_prepare_setattr(struct dentry *dentry, struct iattr *attr);
Eric Biggers60700902020-05-11 12:13:58 -0700253int fscrypt_prepare_setflags(struct inode *inode,
254 unsigned int oldflags, unsigned int flags);
Eric Biggers31114722020-09-16 21:11:34 -0700255int fscrypt_prepare_symlink(struct inode *dir, const char *target,
256 unsigned int len, unsigned int max_len,
257 struct fscrypt_str *disk_link);
Eric Biggers60700902020-05-11 12:13:58 -0700258int __fscrypt_encrypt_symlink(struct inode *inode, const char *target,
259 unsigned int len, struct fscrypt_str *disk_link);
260const char *fscrypt_get_symlink(struct inode *inode, const void *caddr,
261 unsigned int max_size,
262 struct delayed_call *done);
Eric Biggersb8c298c2021-09-01 09:27:18 -0700263int fscrypt_symlink_getattr(const struct path *path, struct kstat *stat);
Sascha Hauereea2c052019-03-26 08:52:31 +0100264static inline void fscrypt_set_ops(struct super_block *sb,
265 const struct fscrypt_operations *s_cop)
266{
267 sb->s_cop = s_cop;
268}
Chandan Rajendra643fa962018-12-12 15:20:12 +0530269#else /* !CONFIG_FS_ENCRYPTION */
270
Eric Biggersab673b92020-07-21 15:59:19 -0700271static inline struct fscrypt_info *fscrypt_get_info(const struct inode *inode)
Chandan Rajendra643fa962018-12-12 15:20:12 +0530272{
Eric Biggersab673b92020-07-21 15:59:19 -0700273 return NULL;
Chandan Rajendra643fa962018-12-12 15:20:12 +0530274}
275
Eric Biggers56dce712019-12-09 12:50:21 -0800276static inline bool fscrypt_needs_contents_encryption(const struct inode *inode)
277{
278 return false;
279}
280
Eric Biggers0bf3d5c12019-03-20 11:39:11 -0700281static inline void fscrypt_handle_d_move(struct dentry *dentry)
282{
283}
284
Eric Biggers2da473e2020-11-17 23:56:05 -0800285static inline bool fscrypt_is_nokey_name(const struct dentry *dentry)
286{
287 return false;
288}
289
Chandan Rajendra643fa962018-12-12 15:20:12 +0530290/* crypto.c */
291static inline void fscrypt_enqueue_decrypt_work(struct work_struct *work)
292{
293}
294
Eric Biggers53bc1d852019-05-20 09:29:44 -0700295static inline struct page *fscrypt_encrypt_pagecache_blocks(struct page *page,
296 unsigned int len,
297 unsigned int offs,
298 gfp_t gfp_flags)
Chandan Rajendra643fa962018-12-12 15:20:12 +0530299{
300 return ERR_PTR(-EOPNOTSUPP);
301}
302
Eric Biggers03569f22019-05-20 09:29:43 -0700303static inline int fscrypt_encrypt_block_inplace(const struct inode *inode,
304 struct page *page,
305 unsigned int len,
306 unsigned int offs, u64 lblk_num,
307 gfp_t gfp_flags)
308{
309 return -EOPNOTSUPP;
310}
311
Eric Biggersaa8bc1a2019-05-20 09:29:47 -0700312static inline int fscrypt_decrypt_pagecache_blocks(struct page *page,
313 unsigned int len,
314 unsigned int offs)
Chandan Rajendra643fa962018-12-12 15:20:12 +0530315{
316 return -EOPNOTSUPP;
317}
318
Eric Biggers41adbcb2019-05-20 09:29:46 -0700319static inline int fscrypt_decrypt_block_inplace(const struct inode *inode,
320 struct page *page,
321 unsigned int len,
322 unsigned int offs, u64 lblk_num)
323{
324 return -EOPNOTSUPP;
325}
326
Eric Biggersd2d07272019-05-20 09:29:39 -0700327static inline bool fscrypt_is_bounce_page(struct page *page)
328{
329 return false;
330}
331
332static inline struct page *fscrypt_pagecache_page(struct page *bounce_page)
Chandan Rajendra643fa962018-12-12 15:20:12 +0530333{
334 WARN_ON_ONCE(1);
335 return ERR_PTR(-EINVAL);
336}
337
Eric Biggersd2d07272019-05-20 09:29:39 -0700338static inline void fscrypt_free_bounce_page(struct page *bounce_page)
Chandan Rajendra643fa962018-12-12 15:20:12 +0530339{
Chandan Rajendra643fa962018-12-12 15:20:12 +0530340}
341
342/* policy.c */
343static inline int fscrypt_ioctl_set_policy(struct file *filp,
344 const void __user *arg)
345{
346 return -EOPNOTSUPP;
347}
348
349static inline int fscrypt_ioctl_get_policy(struct file *filp, void __user *arg)
350{
351 return -EOPNOTSUPP;
352}
353
Eric Biggers5dae4602019-08-04 19:35:47 -0700354static inline int fscrypt_ioctl_get_policy_ex(struct file *filp,
355 void __user *arg)
356{
357 return -EOPNOTSUPP;
358}
359
Eric Biggers54eb3712020-03-14 13:50:49 -0700360static inline int fscrypt_ioctl_get_nonce(struct file *filp, void __user *arg)
361{
362 return -EOPNOTSUPP;
363}
364
Chandan Rajendra643fa962018-12-12 15:20:12 +0530365static inline int fscrypt_has_permitted_context(struct inode *parent,
366 struct inode *child)
367{
368 return 0;
369}
370
Eric Biggersa992b202020-09-16 21:11:24 -0700371static inline int fscrypt_set_context(struct inode *inode, void *fs_data)
Chandan Rajendra643fa962018-12-12 15:20:12 +0530372{
373 return -EOPNOTSUPP;
374}
375
Eric Biggersac4acb12020-09-16 21:11:35 -0700376struct fscrypt_dummy_policy {
Eric Biggersed318a62020-05-12 16:32:50 -0700377};
378
379static inline void fscrypt_show_test_dummy_encryption(struct seq_file *seq,
380 char sep,
381 struct super_block *sb)
382{
383}
384
385static inline void
Eric Biggersac4acb12020-09-16 21:11:35 -0700386fscrypt_free_dummy_policy(struct fscrypt_dummy_policy *dummy_policy)
Eric Biggersed318a62020-05-12 16:32:50 -0700387{
388}
389
Eric Biggers22d94f42019-08-04 19:35:46 -0700390/* keyring.c */
391static inline void fscrypt_sb_free(struct super_block *sb)
392{
393}
394
395static inline int fscrypt_ioctl_add_key(struct file *filp, void __user *arg)
396{
397 return -EOPNOTSUPP;
398}
399
Eric Biggersb1c0ec32019-08-04 19:35:46 -0700400static inline int fscrypt_ioctl_remove_key(struct file *filp, void __user *arg)
401{
402 return -EOPNOTSUPP;
403}
404
Eric Biggers78a1b96b2019-08-04 19:35:47 -0700405static inline int fscrypt_ioctl_remove_key_all_users(struct file *filp,
406 void __user *arg)
407{
408 return -EOPNOTSUPP;
409}
410
Eric Biggers5a7e2992019-08-04 19:35:46 -0700411static inline int fscrypt_ioctl_get_key_status(struct file *filp,
412 void __user *arg)
413{
414 return -EOPNOTSUPP;
415}
416
Eric Biggersfeed8252019-08-04 19:35:45 -0700417/* keysetup.c */
Chandan Rajendra643fa962018-12-12 15:20:12 +0530418
Eric Biggersa992b202020-09-16 21:11:24 -0700419static inline int fscrypt_prepare_new_inode(struct inode *dir,
420 struct inode *inode,
421 bool *encrypt_ret)
422{
423 if (IS_ENCRYPTED(dir))
424 return -EOPNOTSUPP;
425 return 0;
426}
427
Chandan Rajendra643fa962018-12-12 15:20:12 +0530428static inline void fscrypt_put_encryption_info(struct inode *inode)
429{
430 return;
431}
432
Eric Biggers2c58d542019-04-10 13:21:15 -0700433static inline void fscrypt_free_inode(struct inode *inode)
434{
435}
436
Eric Biggersb1c0ec32019-08-04 19:35:46 -0700437static inline int fscrypt_drop_inode(struct inode *inode)
438{
439 return 0;
440}
441
Chandan Rajendra643fa962018-12-12 15:20:12 +0530442 /* fname.c */
443static inline int fscrypt_setup_filename(struct inode *dir,
444 const struct qstr *iname,
445 int lookup, struct fscrypt_name *fname)
446{
447 if (IS_ENCRYPTED(dir))
448 return -EOPNOTSUPP;
449
Eric Biggersb01531d2019-03-20 11:39:13 -0700450 memset(fname, 0, sizeof(*fname));
Chandan Rajendra643fa962018-12-12 15:20:12 +0530451 fname->usr_fname = iname;
452 fname->disk_name.name = (unsigned char *)iname->name;
453 fname->disk_name.len = iname->len;
454 return 0;
455}
456
457static inline void fscrypt_free_filename(struct fscrypt_name *fname)
458{
459 return;
460}
461
Jeff Layton8b10fe62020-08-10 10:21:39 -0400462static inline int fscrypt_fname_alloc_buffer(u32 max_encrypted_len,
Chandan Rajendra643fa962018-12-12 15:20:12 +0530463 struct fscrypt_str *crypto_str)
464{
465 return -EOPNOTSUPP;
466}
467
468static inline void fscrypt_fname_free_buffer(struct fscrypt_str *crypto_str)
469{
470 return;
471}
472
Eric Biggers8a4ab0b2019-12-15 13:39:47 -0800473static inline int fscrypt_fname_disk_to_usr(const struct inode *inode,
Chandan Rajendra643fa962018-12-12 15:20:12 +0530474 u32 hash, u32 minor_hash,
475 const struct fscrypt_str *iname,
476 struct fscrypt_str *oname)
477{
478 return -EOPNOTSUPP;
479}
480
481static inline bool fscrypt_match_name(const struct fscrypt_name *fname,
482 const u8 *de_name, u32 de_name_len)
483{
484 /* Encryption support disabled; use standard comparison */
485 if (de_name_len != fname->disk_name.len)
486 return false;
487 return !memcmp(de_name, fname->disk_name.name, fname->disk_name.len);
488}
489
Daniel Rosenbergaa408f82020-01-20 14:31:57 -0800490static inline u64 fscrypt_fname_siphash(const struct inode *dir,
491 const struct qstr *name)
492{
493 WARN_ON_ONCE(1);
494 return 0;
495}
496
Eric Biggers5b2a8282020-09-23 22:47:21 -0700497static inline int fscrypt_d_revalidate(struct dentry *dentry,
498 unsigned int flags)
499{
500 return 1;
501}
502
Chandan Rajendra643fa962018-12-12 15:20:12 +0530503/* bio.c */
504static inline void fscrypt_decrypt_bio(struct bio *bio)
505{
506}
507
Chandan Rajendra643fa962018-12-12 15:20:12 +0530508static inline int fscrypt_zeroout_range(const struct inode *inode, pgoff_t lblk,
509 sector_t pblk, unsigned int len)
510{
511 return -EOPNOTSUPP;
512}
513
514/* hooks.c */
515
516static inline int fscrypt_file_open(struct inode *inode, struct file *filp)
517{
518 if (IS_ENCRYPTED(inode))
519 return -EOPNOTSUPP;
520 return 0;
521}
522
Eric Biggers968dd6d2019-03-20 11:39:10 -0700523static inline int __fscrypt_prepare_link(struct inode *inode, struct inode *dir,
524 struct dentry *dentry)
Chandan Rajendra643fa962018-12-12 15:20:12 +0530525{
526 return -EOPNOTSUPP;
527}
528
529static inline int __fscrypt_prepare_rename(struct inode *old_dir,
530 struct dentry *old_dentry,
531 struct inode *new_dir,
532 struct dentry *new_dentry,
533 unsigned int flags)
534{
535 return -EOPNOTSUPP;
536}
537
538static inline int __fscrypt_prepare_lookup(struct inode *dir,
Eric Biggersb01531d2019-03-20 11:39:13 -0700539 struct dentry *dentry,
540 struct fscrypt_name *fname)
Chandan Rajendra643fa962018-12-12 15:20:12 +0530541{
542 return -EOPNOTSUPP;
543}
544
Eric Biggers4af7c342020-12-02 18:20:37 -0800545static inline int __fscrypt_prepare_readdir(struct inode *dir)
546{
547 return -EOPNOTSUPP;
548}
549
Eric Biggers9aa0e0a2020-12-02 18:20:38 -0800550static inline int __fscrypt_prepare_setattr(struct dentry *dentry,
551 struct iattr *attr)
552{
553 return -EOPNOTSUPP;
554}
555
Daniel Rosenberg6e1918c2020-01-20 14:31:56 -0800556static inline int fscrypt_prepare_setflags(struct inode *inode,
557 unsigned int oldflags,
558 unsigned int flags)
559{
560 return 0;
561}
562
Eric Biggers31114722020-09-16 21:11:34 -0700563static inline int fscrypt_prepare_symlink(struct inode *dir,
564 const char *target,
565 unsigned int len,
566 unsigned int max_len,
567 struct fscrypt_str *disk_link)
Chandan Rajendra643fa962018-12-12 15:20:12 +0530568{
Eric Biggers31114722020-09-16 21:11:34 -0700569 if (IS_ENCRYPTED(dir))
570 return -EOPNOTSUPP;
571 disk_link->name = (unsigned char *)target;
572 disk_link->len = len + 1;
573 if (disk_link->len > max_len)
574 return -ENAMETOOLONG;
575 return 0;
Chandan Rajendra643fa962018-12-12 15:20:12 +0530576}
577
Chandan Rajendra643fa962018-12-12 15:20:12 +0530578static inline int __fscrypt_encrypt_symlink(struct inode *inode,
579 const char *target,
580 unsigned int len,
581 struct fscrypt_str *disk_link)
582{
583 return -EOPNOTSUPP;
584}
585
586static inline const char *fscrypt_get_symlink(struct inode *inode,
587 const void *caddr,
588 unsigned int max_size,
589 struct delayed_call *done)
590{
591 return ERR_PTR(-EOPNOTSUPP);
592}
Sascha Hauereea2c052019-03-26 08:52:31 +0100593
Eric Biggersb8c298c2021-09-01 09:27:18 -0700594static inline int fscrypt_symlink_getattr(const struct path *path,
595 struct kstat *stat)
596{
597 return -EOPNOTSUPP;
598}
599
Sascha Hauereea2c052019-03-26 08:52:31 +0100600static inline void fscrypt_set_ops(struct super_block *sb,
601 const struct fscrypt_operations *s_cop)
602{
603}
604
Chandan Rajendra643fa962018-12-12 15:20:12 +0530605#endif /* !CONFIG_FS_ENCRYPTION */
Dave Chinner734f0d22017-10-09 12:15:34 -0700606
Satya Tangiralaf5f6acf2019-10-24 14:44:29 -0700607/* inline_crypt.c */
608#ifdef CONFIG_FS_ENCRYPTION_INLINE_CRYPT
Satya Tangiralaf5f6acf2019-10-24 14:44:29 -0700609
Satya Tangiralac2b86b72020-06-16 14:33:37 -0700610bool __fscrypt_inode_uses_inline_crypto(const struct inode *inode);
Satya Tangiralaf5f6acf2019-10-24 14:44:29 -0700611
Satya Tangiralac2b86b72020-06-16 14:33:37 -0700612void fscrypt_set_bio_crypt_ctx(struct bio *bio,
613 const struct inode *inode, u64 first_lblk,
614 gfp_t gfp_mask);
Satya Tangiralaf5f6acf2019-10-24 14:44:29 -0700615
Satya Tangiralac2b86b72020-06-16 14:33:37 -0700616void fscrypt_set_bio_crypt_ctx_bh(struct bio *bio,
617 const struct buffer_head *first_bh,
618 gfp_t gfp_mask);
Satya Tangiralaf5f6acf2019-10-24 14:44:29 -0700619
Satya Tangiralac2b86b72020-06-16 14:33:37 -0700620bool fscrypt_mergeable_bio(struct bio *bio, const struct inode *inode,
621 u64 next_lblk);
Satya Tangiralaf5f6acf2019-10-24 14:44:29 -0700622
Satya Tangiralac2b86b72020-06-16 14:33:37 -0700623bool fscrypt_mergeable_bio_bh(struct bio *bio,
624 const struct buffer_head *next_bh);
Satya Tangiralaf5f6acf2019-10-24 14:44:29 -0700625
Eric Biggersc7615ef2020-07-24 18:44:55 +0000626bool fscrypt_dio_supported(struct kiocb *iocb, struct iov_iter *iter);
627
628u64 fscrypt_limit_io_blocks(const struct inode *inode, u64 lblk, u64 nr_blocks);
629
Satya Tangiralaf5f6acf2019-10-24 14:44:29 -0700630#else /* CONFIG_FS_ENCRYPTION_INLINE_CRYPT */
Satya Tangiralac2b86b72020-06-16 14:33:37 -0700631
632static inline bool __fscrypt_inode_uses_inline_crypto(const struct inode *inode)
Satya Tangiralaf5f6acf2019-10-24 14:44:29 -0700633{
634 return false;
635}
636
Satya Tangiralacfd7e6c2019-12-17 14:26:29 -0800637static inline void fscrypt_set_bio_crypt_ctx(struct bio *bio,
638 const struct inode *inode,
639 u64 first_lblk, gfp_t gfp_mask) { }
Satya Tangiralaf5f6acf2019-10-24 14:44:29 -0700640
Satya Tangiralacfd7e6c2019-12-17 14:26:29 -0800641static inline void fscrypt_set_bio_crypt_ctx_bh(
642 struct bio *bio,
643 const struct buffer_head *first_bh,
644 gfp_t gfp_mask) { }
Satya Tangiralaf5f6acf2019-10-24 14:44:29 -0700645
646static inline bool fscrypt_mergeable_bio(struct bio *bio,
647 const struct inode *inode,
648 u64 next_lblk)
649{
650 return true;
651}
652
653static inline bool fscrypt_mergeable_bio_bh(struct bio *bio,
654 const struct buffer_head *next_bh)
655{
656 return true;
657}
Eric Biggersc7615ef2020-07-24 18:44:55 +0000658
659static inline bool fscrypt_dio_supported(struct kiocb *iocb,
660 struct iov_iter *iter)
661{
662 const struct inode *inode = file_inode(iocb->ki_filp);
663
664 return !fscrypt_needs_contents_encryption(inode);
665}
666
667static inline u64 fscrypt_limit_io_blocks(const struct inode *inode, u64 lblk,
668 u64 nr_blocks)
669{
670 return nr_blocks;
671}
Satya Tangiralaf5f6acf2019-10-24 14:44:29 -0700672#endif /* !CONFIG_FS_ENCRYPTION_INLINE_CRYPT */
673
Eric Biggerscb39ec02020-01-21 09:27:47 -0800674#if IS_ENABLED(CONFIG_FS_ENCRYPTION) && IS_ENABLED(CONFIG_DM_DEFAULT_KEY)
675static inline bool
676fscrypt_inode_should_skip_dm_default_key(const struct inode *inode)
677{
678 return IS_ENCRYPTED(inode) && S_ISREG(inode->i_mode);
679}
680#else
681static inline bool
682fscrypt_inode_should_skip_dm_default_key(const struct inode *inode)
683{
684 return false;
685}
686#endif
687
Eric Biggersd293c3e2017-10-09 12:15:39 -0700688/**
Satya Tangiralac2b86b72020-06-16 14:33:37 -0700689 * fscrypt_inode_uses_inline_crypto() - test whether an inode uses inline
690 * encryption
691 * @inode: an inode. If encrypted, its key must be set up.
692 *
693 * Return: true if the inode requires file contents encryption and if the
694 * encryption should be done in the block layer via blk-crypto rather
695 * than in the filesystem layer.
696 */
697static inline bool fscrypt_inode_uses_inline_crypto(const struct inode *inode)
698{
699 return fscrypt_needs_contents_encryption(inode) &&
700 __fscrypt_inode_uses_inline_crypto(inode);
701}
702
703/**
704 * fscrypt_inode_uses_fs_layer_crypto() - test whether an inode uses fs-layer
705 * encryption
706 * @inode: an inode. If encrypted, its key must be set up.
707 *
708 * Return: true if the inode requires file contents encryption and if the
709 * encryption should be done in the filesystem layer rather than in the
710 * block layer via blk-crypto.
711 */
712static inline bool fscrypt_inode_uses_fs_layer_crypto(const struct inode *inode)
713{
714 return fscrypt_needs_contents_encryption(inode) &&
715 !__fscrypt_inode_uses_inline_crypto(inode);
716}
717
718/**
Eric Biggersab673b92020-07-21 15:59:19 -0700719 * fscrypt_has_encryption_key() - check whether an inode has had its key set up
720 * @inode: the inode to check
721 *
722 * Return: %true if the inode has had its encryption key set up, else %false.
723 *
724 * Usually this should be preceded by fscrypt_get_encryption_info() to try to
725 * set up the key first.
726 */
727static inline bool fscrypt_has_encryption_key(const struct inode *inode)
728{
729 return fscrypt_get_info(inode) != NULL;
730}
731
732/**
Eric Biggersd2fe9752020-05-11 12:13:56 -0700733 * fscrypt_prepare_link() - prepare to link an inode into a possibly-encrypted
734 * directory
Eric Biggers0ea87a92017-10-09 12:15:41 -0700735 * @old_dentry: an existing dentry for the inode being linked
736 * @dir: the target directory
737 * @dentry: negative dentry for the target filename
738 *
739 * A new link can only be added to an encrypted directory if the directory's
740 * encryption key is available --- since otherwise we'd have no way to encrypt
Eric Biggers26461e30a2020-11-17 23:56:09 -0800741 * the filename.
Eric Biggers0ea87a92017-10-09 12:15:41 -0700742 *
743 * We also verify that the link will not violate the constraint that all files
744 * in an encrypted directory tree use the same encryption policy.
745 *
746 * Return: 0 on success, -ENOKEY if the directory's encryption key is missing,
Eric Biggersf5e55e72019-01-22 16:20:21 -0800747 * -EXDEV if the link would result in an inconsistent encryption policy, or
Eric Biggers0ea87a92017-10-09 12:15:41 -0700748 * another -errno code.
749 */
750static inline int fscrypt_prepare_link(struct dentry *old_dentry,
751 struct inode *dir,
752 struct dentry *dentry)
753{
754 if (IS_ENCRYPTED(dir))
Eric Biggers968dd6d2019-03-20 11:39:10 -0700755 return __fscrypt_prepare_link(d_inode(old_dentry), dir, dentry);
Eric Biggers0ea87a92017-10-09 12:15:41 -0700756 return 0;
757}
758
Eric Biggers94b26f32017-10-09 12:15:42 -0700759/**
Eric Biggersd2fe9752020-05-11 12:13:56 -0700760 * fscrypt_prepare_rename() - prepare for a rename between possibly-encrypted
761 * directories
Eric Biggers94b26f32017-10-09 12:15:42 -0700762 * @old_dir: source directory
763 * @old_dentry: dentry for source file
764 * @new_dir: target directory
765 * @new_dentry: dentry for target location (may be negative unless exchanging)
766 * @flags: rename flags (we care at least about %RENAME_EXCHANGE)
767 *
768 * Prepare for ->rename() where the source and/or target directories may be
769 * encrypted. A new link can only be added to an encrypted directory if the
770 * directory's encryption key is available --- since otherwise we'd have no way
771 * to encrypt the filename. A rename to an existing name, on the other hand,
772 * *is* cryptographically possible without the key. However, we take the more
773 * conservative approach and just forbid all no-key renames.
774 *
775 * We also verify that the rename will not violate the constraint that all files
776 * in an encrypted directory tree use the same encryption policy.
777 *
Eric Biggersf5e55e72019-01-22 16:20:21 -0800778 * Return: 0 on success, -ENOKEY if an encryption key is missing, -EXDEV if the
Eric Biggers94b26f32017-10-09 12:15:42 -0700779 * rename would cause inconsistent encryption policies, or another -errno code.
780 */
781static inline int fscrypt_prepare_rename(struct inode *old_dir,
782 struct dentry *old_dentry,
783 struct inode *new_dir,
784 struct dentry *new_dentry,
785 unsigned int flags)
786{
787 if (IS_ENCRYPTED(old_dir) || IS_ENCRYPTED(new_dir))
788 return __fscrypt_prepare_rename(old_dir, old_dentry,
789 new_dir, new_dentry, flags);
790 return 0;
791}
792
Eric Biggers32c3cf02017-10-09 12:15:43 -0700793/**
Eric Biggersd2fe9752020-05-11 12:13:56 -0700794 * fscrypt_prepare_lookup() - prepare to lookup a name in a possibly-encrypted
795 * directory
Eric Biggers32c3cf02017-10-09 12:15:43 -0700796 * @dir: directory being searched
797 * @dentry: filename being looked up
Eric Biggersb01531d2019-03-20 11:39:13 -0700798 * @fname: (output) the name to use to search the on-disk directory
Eric Biggers32c3cf02017-10-09 12:15:43 -0700799 *
Eric Biggersb01531d2019-03-20 11:39:13 -0700800 * Prepare for ->lookup() in a directory which may be encrypted by determining
Eric Biggers70fb2612020-09-23 21:26:23 -0700801 * the name that will actually be used to search the directory on-disk. If the
Eric Biggersa7359962020-12-02 18:20:41 -0800802 * directory's encryption policy is supported by this kernel and its encryption
803 * key is available, then the lookup is assumed to be by plaintext name;
804 * otherwise, it is assumed to be by no-key name.
Eric Biggers32c3cf02017-10-09 12:15:43 -0700805 *
Eric Biggers35693712020-11-20 10:34:32 -0800806 * This will set DCACHE_NOKEY_NAME on the dentry if the lookup is by no-key
807 * name. In this case the filesystem must assign the dentry a dentry_operations
808 * which contains fscrypt_d_revalidate (or contains a d_revalidate method that
809 * calls fscrypt_d_revalidate), so that the dentry will be invalidated if the
810 * directory's encryption key is later added.
Eric Biggers32c3cf02017-10-09 12:15:43 -0700811 *
Eric Biggers70fb2612020-09-23 21:26:23 -0700812 * Return: 0 on success; -ENOENT if the directory's key is unavailable but the
813 * filename isn't a valid no-key name, so a negative dentry should be created;
814 * or another -errno code.
Eric Biggers32c3cf02017-10-09 12:15:43 -0700815 */
816static inline int fscrypt_prepare_lookup(struct inode *dir,
817 struct dentry *dentry,
Eric Biggersb01531d2019-03-20 11:39:13 -0700818 struct fscrypt_name *fname)
Eric Biggers32c3cf02017-10-09 12:15:43 -0700819{
820 if (IS_ENCRYPTED(dir))
Eric Biggersb01531d2019-03-20 11:39:13 -0700821 return __fscrypt_prepare_lookup(dir, dentry, fname);
822
823 memset(fname, 0, sizeof(*fname));
824 fname->usr_fname = &dentry->d_name;
825 fname->disk_name.name = (unsigned char *)dentry->d_name.name;
826 fname->disk_name.len = dentry->d_name.len;
Eric Biggers32c3cf02017-10-09 12:15:43 -0700827 return 0;
828}
829
Eric Biggers815dac32017-10-09 12:15:44 -0700830/**
Eric Biggers4af7c342020-12-02 18:20:37 -0800831 * fscrypt_prepare_readdir() - prepare to read a possibly-encrypted directory
832 * @dir: the directory inode
833 *
834 * If the directory is encrypted and it doesn't already have its encryption key
835 * set up, try to set it up so that the filenames will be listed in plaintext
836 * form rather than in no-key form.
837 *
838 * Return: 0 on success; -errno on error. Note that the encryption key being
Eric Biggersa7359962020-12-02 18:20:41 -0800839 * unavailable is not considered an error. It is also not an error if
840 * the encryption policy is unsupported by this kernel; that is treated
841 * like the key being unavailable, so that files can still be deleted.
Eric Biggers4af7c342020-12-02 18:20:37 -0800842 */
843static inline int fscrypt_prepare_readdir(struct inode *dir)
844{
845 if (IS_ENCRYPTED(dir))
846 return __fscrypt_prepare_readdir(dir);
847 return 0;
848}
849
850/**
Eric Biggersd2fe9752020-05-11 12:13:56 -0700851 * fscrypt_prepare_setattr() - prepare to change a possibly-encrypted inode's
852 * attributes
Eric Biggers815dac32017-10-09 12:15:44 -0700853 * @dentry: dentry through which the inode is being changed
854 * @attr: attributes to change
855 *
856 * Prepare for ->setattr() on a possibly-encrypted inode. On an encrypted file,
857 * most attribute changes are allowed even without the encryption key. However,
858 * without the encryption key we do have to forbid truncates. This is needed
859 * because the size being truncated to may not be a multiple of the filesystem
860 * block size, and in that case we'd have to decrypt the final block, zero the
861 * portion past i_size, and re-encrypt it. (We *could* allow truncating to a
862 * filesystem block boundary, but it's simpler to just forbid all truncates ---
863 * and we already forbid all other contents modifications without the key.)
864 *
865 * Return: 0 on success, -ENOKEY if the key is missing, or another -errno code
866 * if a problem occurred while setting up the encryption key.
867 */
868static inline int fscrypt_prepare_setattr(struct dentry *dentry,
869 struct iattr *attr)
870{
Eric Biggers9aa0e0a2020-12-02 18:20:38 -0800871 if (IS_ENCRYPTED(d_inode(dentry)))
872 return __fscrypt_prepare_setattr(dentry, attr);
Eric Biggers815dac32017-10-09 12:15:44 -0700873 return 0;
874}
875
Eric Biggers76e81d62018-01-05 10:45:01 -0800876/**
Eric Biggersd2fe9752020-05-11 12:13:56 -0700877 * fscrypt_encrypt_symlink() - encrypt the symlink target if needed
Eric Biggers76e81d62018-01-05 10:45:01 -0800878 * @inode: symlink inode
879 * @target: plaintext symlink target
880 * @len: length of @target excluding null terminator
881 * @disk_link: (in/out) the on-disk symlink target being prepared
882 *
883 * If the symlink target needs to be encrypted, then this function encrypts it
884 * into @disk_link->name. fscrypt_prepare_symlink() must have been called
885 * previously to compute @disk_link->len. If the filesystem did not allocate a
886 * buffer for @disk_link->name after calling fscrypt_prepare_link(), then one
887 * will be kmalloc()'ed and the filesystem will be responsible for freeing it.
888 *
889 * Return: 0 on success, -errno on failure
890 */
891static inline int fscrypt_encrypt_symlink(struct inode *inode,
892 const char *target,
893 unsigned int len,
894 struct fscrypt_str *disk_link)
895{
896 if (IS_ENCRYPTED(inode))
897 return __fscrypt_encrypt_symlink(inode, target, len, disk_link);
898 return 0;
899}
900
Eric Biggersd2d07272019-05-20 09:29:39 -0700901/* If *pagep is a bounce page, free it and set *pagep to the pagecache page */
902static inline void fscrypt_finalize_bounce_page(struct page **pagep)
903{
904 struct page *page = *pagep;
905
906 if (fscrypt_is_bounce_page(page)) {
907 *pagep = fscrypt_pagecache_page(page);
908 fscrypt_free_bounce_page(page);
909 }
910}
911
Dave Chinner734f0d22017-10-09 12:15:34 -0700912#endif /* _LINUX_FSCRYPT_H */