Linus Torvalds | 32190f0 | 2017-11-14 11:35:15 -0800 | [diff] [blame] | 1 | /* SPDX-License-Identifier: GPL-2.0 */ |
Eric Biggers | 46f47e4 | 2017-01-24 10:58:06 -0800 | [diff] [blame] | 2 | /* |
Dave Chinner | 734f0d2 | 2017-10-09 12:15:34 -0700 | [diff] [blame] | 3 | * fscrypt.h: declarations for per-file encryption |
| 4 | * |
Chandan Rajendra | 643fa96 | 2018-12-12 15:20:12 +0530 | [diff] [blame] | 5 | * Filesystems that implement per-file encryption must include this header |
| 6 | * file. |
Eric Biggers | 46f47e4 | 2017-01-24 10:58:06 -0800 | [diff] [blame] | 7 | * |
| 8 | * Copyright (C) 2015, Google, Inc. |
| 9 | * |
| 10 | * Written by Michael Halcrow, 2015. |
| 11 | * Modified by Jaegeuk Kim, 2015. |
| 12 | */ |
Dave Chinner | 734f0d2 | 2017-10-09 12:15:34 -0700 | [diff] [blame] | 13 | #ifndef _LINUX_FSCRYPT_H |
| 14 | #define _LINUX_FSCRYPT_H |
Eric Biggers | 46f47e4 | 2017-01-24 10:58:06 -0800 | [diff] [blame] | 15 | |
Eric Biggers | 46f47e4 | 2017-01-24 10:58:06 -0800 | [diff] [blame] | 16 | #include <linux/fs.h> |
Chandan Rajendra | 643fa96 | 2018-12-12 15:20:12 +0530 | [diff] [blame] | 17 | #include <linux/mm.h> |
| 18 | #include <linux/slab.h> |
Eric Biggers | 7af0ab0 | 2019-08-04 19:35:43 -0700 | [diff] [blame] | 19 | #include <uapi/linux/fscrypt.h> |
Eric Biggers | 46f47e4 | 2017-01-24 10:58:06 -0800 | [diff] [blame] | 20 | |
| 21 | #define FS_CRYPTO_BLOCK_SIZE 16 |
| 22 | |
Eric Biggers | ac4acb1 | 2020-09-16 21:11:35 -0700 | [diff] [blame] | 23 | union fscrypt_policy; |
Eric Biggers | 46f47e4 | 2017-01-24 10:58:06 -0800 | [diff] [blame] | 24 | struct fscrypt_info; |
Eric Biggers | ed318a6 | 2020-05-12 16:32:50 -0700 | [diff] [blame] | 25 | struct seq_file; |
Eric Biggers | 46f47e4 | 2017-01-24 10:58:06 -0800 | [diff] [blame] | 26 | |
Eric Biggers | 46f47e4 | 2017-01-24 10:58:06 -0800 | [diff] [blame] | 27 | struct fscrypt_str { |
| 28 | unsigned char *name; |
| 29 | u32 len; |
| 30 | }; |
| 31 | |
| 32 | struct 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 Biggers | 70fb261 | 2020-09-23 21:26:23 -0700 | [diff] [blame] | 38 | bool is_nokey_name; |
Eric Biggers | 46f47e4 | 2017-01-24 10:58:06 -0800 | [diff] [blame] | 39 | }; |
| 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 Erdogan | af65207 | 2017-07-06 00:01:59 -0400 | [diff] [blame] | 46 | /* Maximum value for the third parameter of fscrypt_operations.set_context(). */ |
Eric Biggers | 5dae460 | 2019-08-04 19:35:47 -0700 | [diff] [blame] | 47 | #define FSCRYPT_SET_CONTEXT_MAX_SIZE 40 |
Tahsin Erdogan | af65207 | 2017-07-06 00:01:59 -0400 | [diff] [blame] | 48 | |
Chandan Rajendra | 643fa96 | 2018-12-12 15:20:12 +0530 | [diff] [blame] | 49 | #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 | */ |
| 58 | struct fscrypt_operations { |
| 59 | unsigned int flags; |
| 60 | const char *key_prefix; |
Eric Biggers | fe015a7 | 2020-05-11 12:13:57 -0700 | [diff] [blame] | 61 | 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 Biggers | ac4acb1 | 2020-09-16 21:11:35 -0700 | [diff] [blame] | 64 | const union fscrypt_policy *(*get_dummy_policy)(struct super_block *sb); |
Eric Biggers | fe015a7 | 2020-05-11 12:13:57 -0700 | [diff] [blame] | 65 | bool (*empty_dir)(struct inode *inode); |
Chandan Rajendra | 643fa96 | 2018-12-12 15:20:12 +0530 | [diff] [blame] | 66 | unsigned int max_namelen; |
Eric Biggers | afca03c | 2019-10-24 14:43:36 -0700 | [diff] [blame] | 67 | 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 Tangirala | cfd7e6c | 2019-12-17 14:26:29 -0800 | [diff] [blame] | 70 | int (*get_num_devices)(struct super_block *sb); |
| 71 | void (*get_devices)(struct super_block *sb, |
| 72 | struct request_queue **devs); |
Eric Biggers | 194fd92 | 2021-07-02 10:25:47 -0700 | [diff] [blame] | 73 | |
| 74 | ANDROID_KABI_RESERVE(1); |
| 75 | ANDROID_KABI_RESERVE(2); |
| 76 | ANDROID_KABI_RESERVE(3); |
| 77 | ANDROID_KABI_RESERVE(4); |
Eric Biggers | 2ed5fbf | 2021-07-02 10:25:48 -0700 | [diff] [blame] | 78 | |
| 79 | ANDROID_OEM_DATA_ARRAY(1, 4); |
Chandan Rajendra | 643fa96 | 2018-12-12 15:20:12 +0530 | [diff] [blame] | 80 | }; |
| 81 | |
Eric Biggers | ab673b9 | 2020-07-21 15:59:19 -0700 | [diff] [blame] | 82 | static inline struct fscrypt_info *fscrypt_get_info(const struct inode *inode) |
Chandan Rajendra | 643fa96 | 2018-12-12 15:20:12 +0530 | [diff] [blame] | 83 | { |
Eric Biggers | ab673b9 | 2020-07-21 15:59:19 -0700 | [diff] [blame] | 84 | /* |
Eric Biggers | 94bb3de | 2020-12-02 18:20:40 -0800 | [diff] [blame] | 85 | * Pairs with the cmpxchg_release() in fscrypt_setup_encryption_info(). |
Eric Biggers | ab673b9 | 2020-07-21 15:59:19 -0700 | [diff] [blame] | 86 | * 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 Rajendra | 643fa96 | 2018-12-12 15:20:12 +0530 | [diff] [blame] | 91 | } |
| 92 | |
Eric Biggers | 56dce71 | 2019-12-09 12:50:21 -0800 | [diff] [blame] | 93 | /** |
| 94 | * fscrypt_needs_contents_encryption() - check whether an inode needs |
| 95 | * contents encryption |
Eric Biggers | d2fe975 | 2020-05-11 12:13:56 -0700 | [diff] [blame] | 96 | * @inode: the inode to check |
Eric Biggers | 56dce71 | 2019-12-09 12:50:21 -0800 | [diff] [blame] | 97 | * |
| 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 | */ |
| 104 | static inline bool fscrypt_needs_contents_encryption(const struct inode *inode) |
| 105 | { |
| 106 | return IS_ENCRYPTED(inode) && S_ISREG(inode->i_mode); |
| 107 | } |
| 108 | |
Eric Biggers | 0bf3d5c1 | 2019-03-20 11:39:11 -0700 | [diff] [blame] | 109 | /* |
Eric Biggers | 501e43f | 2020-09-23 21:26:24 -0700 | [diff] [blame] | 110 | * 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 Biggers | 0bf3d5c1 | 2019-03-20 11:39:11 -0700 | [diff] [blame] | 115 | */ |
| 116 | static inline void fscrypt_handle_d_move(struct dentry *dentry) |
| 117 | { |
Eric Biggers | 501e43f | 2020-09-23 21:26:24 -0700 | [diff] [blame] | 118 | dentry->d_flags &= ~DCACHE_NOKEY_NAME; |
Eric Biggers | 0bf3d5c1 | 2019-03-20 11:39:11 -0700 | [diff] [blame] | 119 | } |
| 120 | |
Eric Biggers | 2da473e | 2020-11-17 23:56:05 -0800 | [diff] [blame] | 121 | /** |
| 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 | */ |
| 145 | static inline bool fscrypt_is_nokey_name(const struct dentry *dentry) |
| 146 | { |
| 147 | return dentry->d_flags & DCACHE_NOKEY_NAME; |
| 148 | } |
| 149 | |
Chandan Rajendra | 643fa96 | 2018-12-12 15:20:12 +0530 | [diff] [blame] | 150 | /* crypto.c */ |
Eric Biggers | 6070090 | 2020-05-11 12:13:58 -0700 | [diff] [blame] | 151 | void fscrypt_enqueue_decrypt_work(struct work_struct *); |
Eric Biggers | 53bc1d85 | 2019-05-20 09:29:44 -0700 | [diff] [blame] | 152 | |
Eric Biggers | 6070090 | 2020-05-11 12:13:58 -0700 | [diff] [blame] | 153 | struct page *fscrypt_encrypt_pagecache_blocks(struct page *page, |
| 154 | unsigned int len, |
| 155 | unsigned int offs, |
| 156 | gfp_t gfp_flags); |
| 157 | int 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 Biggers | aa8bc1a | 2019-05-20 09:29:47 -0700 | [diff] [blame] | 160 | |
Eric Biggers | 6070090 | 2020-05-11 12:13:58 -0700 | [diff] [blame] | 161 | int fscrypt_decrypt_pagecache_blocks(struct page *page, unsigned int len, |
| 162 | unsigned int offs); |
| 163 | int fscrypt_decrypt_block_inplace(const struct inode *inode, struct page *page, |
| 164 | unsigned int len, unsigned int offs, |
| 165 | u64 lblk_num); |
Chandan Rajendra | 643fa96 | 2018-12-12 15:20:12 +0530 | [diff] [blame] | 166 | |
Eric Biggers | d2d0727 | 2019-05-20 09:29:39 -0700 | [diff] [blame] | 167 | static inline bool fscrypt_is_bounce_page(struct page *page) |
Chandan Rajendra | 643fa96 | 2018-12-12 15:20:12 +0530 | [diff] [blame] | 168 | { |
Eric Biggers | d2d0727 | 2019-05-20 09:29:39 -0700 | [diff] [blame] | 169 | return page->mapping == NULL; |
Chandan Rajendra | 643fa96 | 2018-12-12 15:20:12 +0530 | [diff] [blame] | 170 | } |
| 171 | |
Eric Biggers | d2d0727 | 2019-05-20 09:29:39 -0700 | [diff] [blame] | 172 | static inline struct page *fscrypt_pagecache_page(struct page *bounce_page) |
| 173 | { |
| 174 | return (struct page *)page_private(bounce_page); |
| 175 | } |
| 176 | |
Eric Biggers | 6070090 | 2020-05-11 12:13:58 -0700 | [diff] [blame] | 177 | void fscrypt_free_bounce_page(struct page *bounce_page); |
Chandan Rajendra | 643fa96 | 2018-12-12 15:20:12 +0530 | [diff] [blame] | 178 | |
| 179 | /* policy.c */ |
Eric Biggers | 6070090 | 2020-05-11 12:13:58 -0700 | [diff] [blame] | 180 | int fscrypt_ioctl_set_policy(struct file *filp, const void __user *arg); |
| 181 | int fscrypt_ioctl_get_policy(struct file *filp, void __user *arg); |
| 182 | int fscrypt_ioctl_get_policy_ex(struct file *filp, void __user *arg); |
| 183 | int fscrypt_ioctl_get_nonce(struct file *filp, void __user *arg); |
| 184 | int fscrypt_has_permitted_context(struct inode *parent, struct inode *child); |
Eric Biggers | a992b20 | 2020-09-16 21:11:24 -0700 | [diff] [blame] | 185 | int fscrypt_set_context(struct inode *inode, void *fs_data); |
Eric Biggers | fe015a7 | 2020-05-11 12:13:57 -0700 | [diff] [blame] | 186 | |
Eric Biggers | ac4acb1 | 2020-09-16 21:11:35 -0700 | [diff] [blame] | 187 | struct fscrypt_dummy_policy { |
| 188 | const union fscrypt_policy *policy; |
Eric Biggers | ed318a6 | 2020-05-12 16:32:50 -0700 | [diff] [blame] | 189 | }; |
| 190 | |
Eric Biggers | c8c868a | 2020-09-16 21:11:36 -0700 | [diff] [blame] | 191 | int fscrypt_set_test_dummy_encryption(struct super_block *sb, const char *arg, |
Eric Biggers | ac4acb1 | 2020-09-16 21:11:35 -0700 | [diff] [blame] | 192 | struct fscrypt_dummy_policy *dummy_policy); |
Eric Biggers | ed318a6 | 2020-05-12 16:32:50 -0700 | [diff] [blame] | 193 | void fscrypt_show_test_dummy_encryption(struct seq_file *seq, char sep, |
| 194 | struct super_block *sb); |
| 195 | static inline void |
Eric Biggers | ac4acb1 | 2020-09-16 21:11:35 -0700 | [diff] [blame] | 196 | fscrypt_free_dummy_policy(struct fscrypt_dummy_policy *dummy_policy) |
Eric Biggers | ed318a6 | 2020-05-12 16:32:50 -0700 | [diff] [blame] | 197 | { |
Eric Biggers | ac4acb1 | 2020-09-16 21:11:35 -0700 | [diff] [blame] | 198 | kfree(dummy_policy->policy); |
| 199 | dummy_policy->policy = NULL; |
Eric Biggers | ed318a6 | 2020-05-12 16:32:50 -0700 | [diff] [blame] | 200 | } |
| 201 | |
Eric Biggers | 22d94f4 | 2019-08-04 19:35:46 -0700 | [diff] [blame] | 202 | /* keyring.c */ |
Eric Biggers | 6070090 | 2020-05-11 12:13:58 -0700 | [diff] [blame] | 203 | void fscrypt_sb_free(struct super_block *sb); |
| 204 | int fscrypt_ioctl_add_key(struct file *filp, void __user *arg); |
| 205 | int fscrypt_ioctl_remove_key(struct file *filp, void __user *arg); |
| 206 | int fscrypt_ioctl_remove_key_all_users(struct file *filp, void __user *arg); |
| 207 | int fscrypt_ioctl_get_key_status(struct file *filp, void __user *arg); |
Eric Biggers | 22d94f4 | 2019-08-04 19:35:46 -0700 | [diff] [blame] | 208 | |
Eric Biggers | feed825 | 2019-08-04 19:35:45 -0700 | [diff] [blame] | 209 | /* keysetup.c */ |
Eric Biggers | a992b20 | 2020-09-16 21:11:24 -0700 | [diff] [blame] | 210 | int fscrypt_prepare_new_inode(struct inode *dir, struct inode *inode, |
| 211 | bool *encrypt_ret); |
Eric Biggers | 6070090 | 2020-05-11 12:13:58 -0700 | [diff] [blame] | 212 | void fscrypt_put_encryption_info(struct inode *inode); |
| 213 | void fscrypt_free_inode(struct inode *inode); |
| 214 | int fscrypt_drop_inode(struct inode *inode); |
Chandan Rajendra | 643fa96 | 2018-12-12 15:20:12 +0530 | [diff] [blame] | 215 | |
| 216 | /* fname.c */ |
Eric Biggers | 6070090 | 2020-05-11 12:13:58 -0700 | [diff] [blame] | 217 | int fscrypt_setup_filename(struct inode *inode, const struct qstr *iname, |
| 218 | int lookup, struct fscrypt_name *fname); |
Chandan Rajendra | 643fa96 | 2018-12-12 15:20:12 +0530 | [diff] [blame] | 219 | |
| 220 | static inline void fscrypt_free_filename(struct fscrypt_name *fname) |
| 221 | { |
| 222 | kfree(fname->crypto_buf.name); |
| 223 | } |
| 224 | |
Jeff Layton | 8b10fe6 | 2020-08-10 10:21:39 -0400 | [diff] [blame] | 225 | int fscrypt_fname_alloc_buffer(u32 max_encrypted_len, |
Eric Biggers | 6070090 | 2020-05-11 12:13:58 -0700 | [diff] [blame] | 226 | struct fscrypt_str *crypto_str); |
| 227 | void fscrypt_fname_free_buffer(struct fscrypt_str *crypto_str); |
| 228 | int 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); |
| 232 | bool fscrypt_match_name(const struct fscrypt_name *fname, |
| 233 | const u8 *de_name, u32 de_name_len); |
| 234 | u64 fscrypt_fname_siphash(const struct inode *dir, const struct qstr *name); |
Eric Biggers | 5b2a828 | 2020-09-23 22:47:21 -0700 | [diff] [blame] | 235 | int fscrypt_d_revalidate(struct dentry *dentry, unsigned int flags); |
Chandan Rajendra | 643fa96 | 2018-12-12 15:20:12 +0530 | [diff] [blame] | 236 | |
| 237 | /* bio.c */ |
Eric Biggers | 6070090 | 2020-05-11 12:13:58 -0700 | [diff] [blame] | 238 | void fscrypt_decrypt_bio(struct bio *bio); |
| 239 | int fscrypt_zeroout_range(const struct inode *inode, pgoff_t lblk, |
| 240 | sector_t pblk, unsigned int len); |
Chandan Rajendra | 643fa96 | 2018-12-12 15:20:12 +0530 | [diff] [blame] | 241 | |
| 242 | /* hooks.c */ |
Eric Biggers | 6070090 | 2020-05-11 12:13:58 -0700 | [diff] [blame] | 243 | int fscrypt_file_open(struct inode *inode, struct file *filp); |
| 244 | int __fscrypt_prepare_link(struct inode *inode, struct inode *dir, |
| 245 | struct dentry *dentry); |
| 246 | int __fscrypt_prepare_rename(struct inode *old_dir, struct dentry *old_dentry, |
| 247 | struct inode *new_dir, struct dentry *new_dentry, |
| 248 | unsigned int flags); |
| 249 | int __fscrypt_prepare_lookup(struct inode *dir, struct dentry *dentry, |
| 250 | struct fscrypt_name *fname); |
Eric Biggers | 4af7c34 | 2020-12-02 18:20:37 -0800 | [diff] [blame] | 251 | int __fscrypt_prepare_readdir(struct inode *dir); |
Eric Biggers | 9aa0e0a | 2020-12-02 18:20:38 -0800 | [diff] [blame] | 252 | int __fscrypt_prepare_setattr(struct dentry *dentry, struct iattr *attr); |
Eric Biggers | 6070090 | 2020-05-11 12:13:58 -0700 | [diff] [blame] | 253 | int fscrypt_prepare_setflags(struct inode *inode, |
| 254 | unsigned int oldflags, unsigned int flags); |
Eric Biggers | 3111472 | 2020-09-16 21:11:34 -0700 | [diff] [blame] | 255 | int fscrypt_prepare_symlink(struct inode *dir, const char *target, |
| 256 | unsigned int len, unsigned int max_len, |
| 257 | struct fscrypt_str *disk_link); |
Eric Biggers | 6070090 | 2020-05-11 12:13:58 -0700 | [diff] [blame] | 258 | int __fscrypt_encrypt_symlink(struct inode *inode, const char *target, |
| 259 | unsigned int len, struct fscrypt_str *disk_link); |
| 260 | const char *fscrypt_get_symlink(struct inode *inode, const void *caddr, |
| 261 | unsigned int max_size, |
| 262 | struct delayed_call *done); |
Eric Biggers | b8c298c | 2021-09-01 09:27:18 -0700 | [diff] [blame] | 263 | int fscrypt_symlink_getattr(const struct path *path, struct kstat *stat); |
Sascha Hauer | eea2c05 | 2019-03-26 08:52:31 +0100 | [diff] [blame] | 264 | static 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 Rajendra | 643fa96 | 2018-12-12 15:20:12 +0530 | [diff] [blame] | 269 | #else /* !CONFIG_FS_ENCRYPTION */ |
| 270 | |
Eric Biggers | ab673b9 | 2020-07-21 15:59:19 -0700 | [diff] [blame] | 271 | static inline struct fscrypt_info *fscrypt_get_info(const struct inode *inode) |
Chandan Rajendra | 643fa96 | 2018-12-12 15:20:12 +0530 | [diff] [blame] | 272 | { |
Eric Biggers | ab673b9 | 2020-07-21 15:59:19 -0700 | [diff] [blame] | 273 | return NULL; |
Chandan Rajendra | 643fa96 | 2018-12-12 15:20:12 +0530 | [diff] [blame] | 274 | } |
| 275 | |
Eric Biggers | 56dce71 | 2019-12-09 12:50:21 -0800 | [diff] [blame] | 276 | static inline bool fscrypt_needs_contents_encryption(const struct inode *inode) |
| 277 | { |
| 278 | return false; |
| 279 | } |
| 280 | |
Eric Biggers | 0bf3d5c1 | 2019-03-20 11:39:11 -0700 | [diff] [blame] | 281 | static inline void fscrypt_handle_d_move(struct dentry *dentry) |
| 282 | { |
| 283 | } |
| 284 | |
Eric Biggers | 2da473e | 2020-11-17 23:56:05 -0800 | [diff] [blame] | 285 | static inline bool fscrypt_is_nokey_name(const struct dentry *dentry) |
| 286 | { |
| 287 | return false; |
| 288 | } |
| 289 | |
Chandan Rajendra | 643fa96 | 2018-12-12 15:20:12 +0530 | [diff] [blame] | 290 | /* crypto.c */ |
| 291 | static inline void fscrypt_enqueue_decrypt_work(struct work_struct *work) |
| 292 | { |
| 293 | } |
| 294 | |
Eric Biggers | 53bc1d85 | 2019-05-20 09:29:44 -0700 | [diff] [blame] | 295 | static inline struct page *fscrypt_encrypt_pagecache_blocks(struct page *page, |
| 296 | unsigned int len, |
| 297 | unsigned int offs, |
| 298 | gfp_t gfp_flags) |
Chandan Rajendra | 643fa96 | 2018-12-12 15:20:12 +0530 | [diff] [blame] | 299 | { |
| 300 | return ERR_PTR(-EOPNOTSUPP); |
| 301 | } |
| 302 | |
Eric Biggers | 03569f2 | 2019-05-20 09:29:43 -0700 | [diff] [blame] | 303 | static 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 Biggers | aa8bc1a | 2019-05-20 09:29:47 -0700 | [diff] [blame] | 312 | static inline int fscrypt_decrypt_pagecache_blocks(struct page *page, |
| 313 | unsigned int len, |
| 314 | unsigned int offs) |
Chandan Rajendra | 643fa96 | 2018-12-12 15:20:12 +0530 | [diff] [blame] | 315 | { |
| 316 | return -EOPNOTSUPP; |
| 317 | } |
| 318 | |
Eric Biggers | 41adbcb | 2019-05-20 09:29:46 -0700 | [diff] [blame] | 319 | static 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 Biggers | d2d0727 | 2019-05-20 09:29:39 -0700 | [diff] [blame] | 327 | static inline bool fscrypt_is_bounce_page(struct page *page) |
| 328 | { |
| 329 | return false; |
| 330 | } |
| 331 | |
| 332 | static inline struct page *fscrypt_pagecache_page(struct page *bounce_page) |
Chandan Rajendra | 643fa96 | 2018-12-12 15:20:12 +0530 | [diff] [blame] | 333 | { |
| 334 | WARN_ON_ONCE(1); |
| 335 | return ERR_PTR(-EINVAL); |
| 336 | } |
| 337 | |
Eric Biggers | d2d0727 | 2019-05-20 09:29:39 -0700 | [diff] [blame] | 338 | static inline void fscrypt_free_bounce_page(struct page *bounce_page) |
Chandan Rajendra | 643fa96 | 2018-12-12 15:20:12 +0530 | [diff] [blame] | 339 | { |
Chandan Rajendra | 643fa96 | 2018-12-12 15:20:12 +0530 | [diff] [blame] | 340 | } |
| 341 | |
| 342 | /* policy.c */ |
| 343 | static inline int fscrypt_ioctl_set_policy(struct file *filp, |
| 344 | const void __user *arg) |
| 345 | { |
| 346 | return -EOPNOTSUPP; |
| 347 | } |
| 348 | |
| 349 | static inline int fscrypt_ioctl_get_policy(struct file *filp, void __user *arg) |
| 350 | { |
| 351 | return -EOPNOTSUPP; |
| 352 | } |
| 353 | |
Eric Biggers | 5dae460 | 2019-08-04 19:35:47 -0700 | [diff] [blame] | 354 | static inline int fscrypt_ioctl_get_policy_ex(struct file *filp, |
| 355 | void __user *arg) |
| 356 | { |
| 357 | return -EOPNOTSUPP; |
| 358 | } |
| 359 | |
Eric Biggers | 54eb371 | 2020-03-14 13:50:49 -0700 | [diff] [blame] | 360 | static inline int fscrypt_ioctl_get_nonce(struct file *filp, void __user *arg) |
| 361 | { |
| 362 | return -EOPNOTSUPP; |
| 363 | } |
| 364 | |
Chandan Rajendra | 643fa96 | 2018-12-12 15:20:12 +0530 | [diff] [blame] | 365 | static inline int fscrypt_has_permitted_context(struct inode *parent, |
| 366 | struct inode *child) |
| 367 | { |
| 368 | return 0; |
| 369 | } |
| 370 | |
Eric Biggers | a992b20 | 2020-09-16 21:11:24 -0700 | [diff] [blame] | 371 | static inline int fscrypt_set_context(struct inode *inode, void *fs_data) |
Chandan Rajendra | 643fa96 | 2018-12-12 15:20:12 +0530 | [diff] [blame] | 372 | { |
| 373 | return -EOPNOTSUPP; |
| 374 | } |
| 375 | |
Eric Biggers | ac4acb1 | 2020-09-16 21:11:35 -0700 | [diff] [blame] | 376 | struct fscrypt_dummy_policy { |
Eric Biggers | ed318a6 | 2020-05-12 16:32:50 -0700 | [diff] [blame] | 377 | }; |
| 378 | |
| 379 | static inline void fscrypt_show_test_dummy_encryption(struct seq_file *seq, |
| 380 | char sep, |
| 381 | struct super_block *sb) |
| 382 | { |
| 383 | } |
| 384 | |
| 385 | static inline void |
Eric Biggers | ac4acb1 | 2020-09-16 21:11:35 -0700 | [diff] [blame] | 386 | fscrypt_free_dummy_policy(struct fscrypt_dummy_policy *dummy_policy) |
Eric Biggers | ed318a6 | 2020-05-12 16:32:50 -0700 | [diff] [blame] | 387 | { |
| 388 | } |
| 389 | |
Eric Biggers | 22d94f4 | 2019-08-04 19:35:46 -0700 | [diff] [blame] | 390 | /* keyring.c */ |
| 391 | static inline void fscrypt_sb_free(struct super_block *sb) |
| 392 | { |
| 393 | } |
| 394 | |
| 395 | static inline int fscrypt_ioctl_add_key(struct file *filp, void __user *arg) |
| 396 | { |
| 397 | return -EOPNOTSUPP; |
| 398 | } |
| 399 | |
Eric Biggers | b1c0ec3 | 2019-08-04 19:35:46 -0700 | [diff] [blame] | 400 | static inline int fscrypt_ioctl_remove_key(struct file *filp, void __user *arg) |
| 401 | { |
| 402 | return -EOPNOTSUPP; |
| 403 | } |
| 404 | |
Eric Biggers | 78a1b96b | 2019-08-04 19:35:47 -0700 | [diff] [blame] | 405 | static inline int fscrypt_ioctl_remove_key_all_users(struct file *filp, |
| 406 | void __user *arg) |
| 407 | { |
| 408 | return -EOPNOTSUPP; |
| 409 | } |
| 410 | |
Eric Biggers | 5a7e299 | 2019-08-04 19:35:46 -0700 | [diff] [blame] | 411 | static inline int fscrypt_ioctl_get_key_status(struct file *filp, |
| 412 | void __user *arg) |
| 413 | { |
| 414 | return -EOPNOTSUPP; |
| 415 | } |
| 416 | |
Eric Biggers | feed825 | 2019-08-04 19:35:45 -0700 | [diff] [blame] | 417 | /* keysetup.c */ |
Chandan Rajendra | 643fa96 | 2018-12-12 15:20:12 +0530 | [diff] [blame] | 418 | |
Eric Biggers | a992b20 | 2020-09-16 21:11:24 -0700 | [diff] [blame] | 419 | static 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 Rajendra | 643fa96 | 2018-12-12 15:20:12 +0530 | [diff] [blame] | 428 | static inline void fscrypt_put_encryption_info(struct inode *inode) |
| 429 | { |
| 430 | return; |
| 431 | } |
| 432 | |
Eric Biggers | 2c58d54 | 2019-04-10 13:21:15 -0700 | [diff] [blame] | 433 | static inline void fscrypt_free_inode(struct inode *inode) |
| 434 | { |
| 435 | } |
| 436 | |
Eric Biggers | b1c0ec3 | 2019-08-04 19:35:46 -0700 | [diff] [blame] | 437 | static inline int fscrypt_drop_inode(struct inode *inode) |
| 438 | { |
| 439 | return 0; |
| 440 | } |
| 441 | |
Chandan Rajendra | 643fa96 | 2018-12-12 15:20:12 +0530 | [diff] [blame] | 442 | /* fname.c */ |
| 443 | static 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 Biggers | b01531d | 2019-03-20 11:39:13 -0700 | [diff] [blame] | 450 | memset(fname, 0, sizeof(*fname)); |
Chandan Rajendra | 643fa96 | 2018-12-12 15:20:12 +0530 | [diff] [blame] | 451 | 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 | |
| 457 | static inline void fscrypt_free_filename(struct fscrypt_name *fname) |
| 458 | { |
| 459 | return; |
| 460 | } |
| 461 | |
Jeff Layton | 8b10fe6 | 2020-08-10 10:21:39 -0400 | [diff] [blame] | 462 | static inline int fscrypt_fname_alloc_buffer(u32 max_encrypted_len, |
Chandan Rajendra | 643fa96 | 2018-12-12 15:20:12 +0530 | [diff] [blame] | 463 | struct fscrypt_str *crypto_str) |
| 464 | { |
| 465 | return -EOPNOTSUPP; |
| 466 | } |
| 467 | |
| 468 | static inline void fscrypt_fname_free_buffer(struct fscrypt_str *crypto_str) |
| 469 | { |
| 470 | return; |
| 471 | } |
| 472 | |
Eric Biggers | 8a4ab0b | 2019-12-15 13:39:47 -0800 | [diff] [blame] | 473 | static inline int fscrypt_fname_disk_to_usr(const struct inode *inode, |
Chandan Rajendra | 643fa96 | 2018-12-12 15:20:12 +0530 | [diff] [blame] | 474 | u32 hash, u32 minor_hash, |
| 475 | const struct fscrypt_str *iname, |
| 476 | struct fscrypt_str *oname) |
| 477 | { |
| 478 | return -EOPNOTSUPP; |
| 479 | } |
| 480 | |
| 481 | static 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 Rosenberg | aa408f8 | 2020-01-20 14:31:57 -0800 | [diff] [blame] | 490 | static 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 Biggers | 5b2a828 | 2020-09-23 22:47:21 -0700 | [diff] [blame] | 497 | static inline int fscrypt_d_revalidate(struct dentry *dentry, |
| 498 | unsigned int flags) |
| 499 | { |
| 500 | return 1; |
| 501 | } |
| 502 | |
Chandan Rajendra | 643fa96 | 2018-12-12 15:20:12 +0530 | [diff] [blame] | 503 | /* bio.c */ |
| 504 | static inline void fscrypt_decrypt_bio(struct bio *bio) |
| 505 | { |
| 506 | } |
| 507 | |
Chandan Rajendra | 643fa96 | 2018-12-12 15:20:12 +0530 | [diff] [blame] | 508 | static 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 | |
| 516 | static 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 Biggers | 968dd6d | 2019-03-20 11:39:10 -0700 | [diff] [blame] | 523 | static inline int __fscrypt_prepare_link(struct inode *inode, struct inode *dir, |
| 524 | struct dentry *dentry) |
Chandan Rajendra | 643fa96 | 2018-12-12 15:20:12 +0530 | [diff] [blame] | 525 | { |
| 526 | return -EOPNOTSUPP; |
| 527 | } |
| 528 | |
| 529 | static 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 | |
| 538 | static inline int __fscrypt_prepare_lookup(struct inode *dir, |
Eric Biggers | b01531d | 2019-03-20 11:39:13 -0700 | [diff] [blame] | 539 | struct dentry *dentry, |
| 540 | struct fscrypt_name *fname) |
Chandan Rajendra | 643fa96 | 2018-12-12 15:20:12 +0530 | [diff] [blame] | 541 | { |
| 542 | return -EOPNOTSUPP; |
| 543 | } |
| 544 | |
Eric Biggers | 4af7c34 | 2020-12-02 18:20:37 -0800 | [diff] [blame] | 545 | static inline int __fscrypt_prepare_readdir(struct inode *dir) |
| 546 | { |
| 547 | return -EOPNOTSUPP; |
| 548 | } |
| 549 | |
Eric Biggers | 9aa0e0a | 2020-12-02 18:20:38 -0800 | [diff] [blame] | 550 | static inline int __fscrypt_prepare_setattr(struct dentry *dentry, |
| 551 | struct iattr *attr) |
| 552 | { |
| 553 | return -EOPNOTSUPP; |
| 554 | } |
| 555 | |
Daniel Rosenberg | 6e1918c | 2020-01-20 14:31:56 -0800 | [diff] [blame] | 556 | static inline int fscrypt_prepare_setflags(struct inode *inode, |
| 557 | unsigned int oldflags, |
| 558 | unsigned int flags) |
| 559 | { |
| 560 | return 0; |
| 561 | } |
| 562 | |
Eric Biggers | 3111472 | 2020-09-16 21:11:34 -0700 | [diff] [blame] | 563 | static 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 Rajendra | 643fa96 | 2018-12-12 15:20:12 +0530 | [diff] [blame] | 568 | { |
Eric Biggers | 3111472 | 2020-09-16 21:11:34 -0700 | [diff] [blame] | 569 | 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 Rajendra | 643fa96 | 2018-12-12 15:20:12 +0530 | [diff] [blame] | 576 | } |
| 577 | |
Chandan Rajendra | 643fa96 | 2018-12-12 15:20:12 +0530 | [diff] [blame] | 578 | static 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 | |
| 586 | static 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 Hauer | eea2c05 | 2019-03-26 08:52:31 +0100 | [diff] [blame] | 593 | |
Eric Biggers | b8c298c | 2021-09-01 09:27:18 -0700 | [diff] [blame] | 594 | static inline int fscrypt_symlink_getattr(const struct path *path, |
| 595 | struct kstat *stat) |
| 596 | { |
| 597 | return -EOPNOTSUPP; |
| 598 | } |
| 599 | |
Sascha Hauer | eea2c05 | 2019-03-26 08:52:31 +0100 | [diff] [blame] | 600 | static inline void fscrypt_set_ops(struct super_block *sb, |
| 601 | const struct fscrypt_operations *s_cop) |
| 602 | { |
| 603 | } |
| 604 | |
Chandan Rajendra | 643fa96 | 2018-12-12 15:20:12 +0530 | [diff] [blame] | 605 | #endif /* !CONFIG_FS_ENCRYPTION */ |
Dave Chinner | 734f0d2 | 2017-10-09 12:15:34 -0700 | [diff] [blame] | 606 | |
Satya Tangirala | f5f6acf | 2019-10-24 14:44:29 -0700 | [diff] [blame] | 607 | /* inline_crypt.c */ |
| 608 | #ifdef CONFIG_FS_ENCRYPTION_INLINE_CRYPT |
Satya Tangirala | f5f6acf | 2019-10-24 14:44:29 -0700 | [diff] [blame] | 609 | |
Satya Tangirala | c2b86b7 | 2020-06-16 14:33:37 -0700 | [diff] [blame] | 610 | bool __fscrypt_inode_uses_inline_crypto(const struct inode *inode); |
Satya Tangirala | f5f6acf | 2019-10-24 14:44:29 -0700 | [diff] [blame] | 611 | |
Satya Tangirala | c2b86b7 | 2020-06-16 14:33:37 -0700 | [diff] [blame] | 612 | void fscrypt_set_bio_crypt_ctx(struct bio *bio, |
| 613 | const struct inode *inode, u64 first_lblk, |
| 614 | gfp_t gfp_mask); |
Satya Tangirala | f5f6acf | 2019-10-24 14:44:29 -0700 | [diff] [blame] | 615 | |
Satya Tangirala | c2b86b7 | 2020-06-16 14:33:37 -0700 | [diff] [blame] | 616 | void fscrypt_set_bio_crypt_ctx_bh(struct bio *bio, |
| 617 | const struct buffer_head *first_bh, |
| 618 | gfp_t gfp_mask); |
Satya Tangirala | f5f6acf | 2019-10-24 14:44:29 -0700 | [diff] [blame] | 619 | |
Satya Tangirala | c2b86b7 | 2020-06-16 14:33:37 -0700 | [diff] [blame] | 620 | bool fscrypt_mergeable_bio(struct bio *bio, const struct inode *inode, |
| 621 | u64 next_lblk); |
Satya Tangirala | f5f6acf | 2019-10-24 14:44:29 -0700 | [diff] [blame] | 622 | |
Satya Tangirala | c2b86b7 | 2020-06-16 14:33:37 -0700 | [diff] [blame] | 623 | bool fscrypt_mergeable_bio_bh(struct bio *bio, |
| 624 | const struct buffer_head *next_bh); |
Satya Tangirala | f5f6acf | 2019-10-24 14:44:29 -0700 | [diff] [blame] | 625 | |
Eric Biggers | c7615ef | 2020-07-24 18:44:55 +0000 | [diff] [blame] | 626 | bool fscrypt_dio_supported(struct kiocb *iocb, struct iov_iter *iter); |
| 627 | |
| 628 | u64 fscrypt_limit_io_blocks(const struct inode *inode, u64 lblk, u64 nr_blocks); |
| 629 | |
Satya Tangirala | f5f6acf | 2019-10-24 14:44:29 -0700 | [diff] [blame] | 630 | #else /* CONFIG_FS_ENCRYPTION_INLINE_CRYPT */ |
Satya Tangirala | c2b86b7 | 2020-06-16 14:33:37 -0700 | [diff] [blame] | 631 | |
| 632 | static inline bool __fscrypt_inode_uses_inline_crypto(const struct inode *inode) |
Satya Tangirala | f5f6acf | 2019-10-24 14:44:29 -0700 | [diff] [blame] | 633 | { |
| 634 | return false; |
| 635 | } |
| 636 | |
Satya Tangirala | cfd7e6c | 2019-12-17 14:26:29 -0800 | [diff] [blame] | 637 | static inline void fscrypt_set_bio_crypt_ctx(struct bio *bio, |
| 638 | const struct inode *inode, |
| 639 | u64 first_lblk, gfp_t gfp_mask) { } |
Satya Tangirala | f5f6acf | 2019-10-24 14:44:29 -0700 | [diff] [blame] | 640 | |
Satya Tangirala | cfd7e6c | 2019-12-17 14:26:29 -0800 | [diff] [blame] | 641 | static 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 Tangirala | f5f6acf | 2019-10-24 14:44:29 -0700 | [diff] [blame] | 645 | |
| 646 | static inline bool fscrypt_mergeable_bio(struct bio *bio, |
| 647 | const struct inode *inode, |
| 648 | u64 next_lblk) |
| 649 | { |
| 650 | return true; |
| 651 | } |
| 652 | |
| 653 | static inline bool fscrypt_mergeable_bio_bh(struct bio *bio, |
| 654 | const struct buffer_head *next_bh) |
| 655 | { |
| 656 | return true; |
| 657 | } |
Eric Biggers | c7615ef | 2020-07-24 18:44:55 +0000 | [diff] [blame] | 658 | |
| 659 | static 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 | |
| 667 | static inline u64 fscrypt_limit_io_blocks(const struct inode *inode, u64 lblk, |
| 668 | u64 nr_blocks) |
| 669 | { |
| 670 | return nr_blocks; |
| 671 | } |
Satya Tangirala | f5f6acf | 2019-10-24 14:44:29 -0700 | [diff] [blame] | 672 | #endif /* !CONFIG_FS_ENCRYPTION_INLINE_CRYPT */ |
| 673 | |
Eric Biggers | cb39ec0 | 2020-01-21 09:27:47 -0800 | [diff] [blame] | 674 | #if IS_ENABLED(CONFIG_FS_ENCRYPTION) && IS_ENABLED(CONFIG_DM_DEFAULT_KEY) |
| 675 | static inline bool |
| 676 | fscrypt_inode_should_skip_dm_default_key(const struct inode *inode) |
| 677 | { |
| 678 | return IS_ENCRYPTED(inode) && S_ISREG(inode->i_mode); |
| 679 | } |
| 680 | #else |
| 681 | static inline bool |
| 682 | fscrypt_inode_should_skip_dm_default_key(const struct inode *inode) |
| 683 | { |
| 684 | return false; |
| 685 | } |
| 686 | #endif |
| 687 | |
Eric Biggers | d293c3e | 2017-10-09 12:15:39 -0700 | [diff] [blame] | 688 | /** |
Satya Tangirala | c2b86b7 | 2020-06-16 14:33:37 -0700 | [diff] [blame] | 689 | * 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 | */ |
| 697 | static 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 | */ |
| 712 | static 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 Biggers | ab673b9 | 2020-07-21 15:59:19 -0700 | [diff] [blame] | 719 | * 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 | */ |
| 727 | static inline bool fscrypt_has_encryption_key(const struct inode *inode) |
| 728 | { |
| 729 | return fscrypt_get_info(inode) != NULL; |
| 730 | } |
| 731 | |
| 732 | /** |
Eric Biggers | d2fe975 | 2020-05-11 12:13:56 -0700 | [diff] [blame] | 733 | * fscrypt_prepare_link() - prepare to link an inode into a possibly-encrypted |
| 734 | * directory |
Eric Biggers | 0ea87a9 | 2017-10-09 12:15:41 -0700 | [diff] [blame] | 735 | * @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 Biggers | 26461e30a | 2020-11-17 23:56:09 -0800 | [diff] [blame] | 741 | * the filename. |
Eric Biggers | 0ea87a9 | 2017-10-09 12:15:41 -0700 | [diff] [blame] | 742 | * |
| 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 Biggers | f5e55e7 | 2019-01-22 16:20:21 -0800 | [diff] [blame] | 747 | * -EXDEV if the link would result in an inconsistent encryption policy, or |
Eric Biggers | 0ea87a9 | 2017-10-09 12:15:41 -0700 | [diff] [blame] | 748 | * another -errno code. |
| 749 | */ |
| 750 | static inline int fscrypt_prepare_link(struct dentry *old_dentry, |
| 751 | struct inode *dir, |
| 752 | struct dentry *dentry) |
| 753 | { |
| 754 | if (IS_ENCRYPTED(dir)) |
Eric Biggers | 968dd6d | 2019-03-20 11:39:10 -0700 | [diff] [blame] | 755 | return __fscrypt_prepare_link(d_inode(old_dentry), dir, dentry); |
Eric Biggers | 0ea87a9 | 2017-10-09 12:15:41 -0700 | [diff] [blame] | 756 | return 0; |
| 757 | } |
| 758 | |
Eric Biggers | 94b26f3 | 2017-10-09 12:15:42 -0700 | [diff] [blame] | 759 | /** |
Eric Biggers | d2fe975 | 2020-05-11 12:13:56 -0700 | [diff] [blame] | 760 | * fscrypt_prepare_rename() - prepare for a rename between possibly-encrypted |
| 761 | * directories |
Eric Biggers | 94b26f3 | 2017-10-09 12:15:42 -0700 | [diff] [blame] | 762 | * @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 Biggers | f5e55e7 | 2019-01-22 16:20:21 -0800 | [diff] [blame] | 778 | * Return: 0 on success, -ENOKEY if an encryption key is missing, -EXDEV if the |
Eric Biggers | 94b26f3 | 2017-10-09 12:15:42 -0700 | [diff] [blame] | 779 | * rename would cause inconsistent encryption policies, or another -errno code. |
| 780 | */ |
| 781 | static 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 Biggers | 32c3cf0 | 2017-10-09 12:15:43 -0700 | [diff] [blame] | 793 | /** |
Eric Biggers | d2fe975 | 2020-05-11 12:13:56 -0700 | [diff] [blame] | 794 | * fscrypt_prepare_lookup() - prepare to lookup a name in a possibly-encrypted |
| 795 | * directory |
Eric Biggers | 32c3cf0 | 2017-10-09 12:15:43 -0700 | [diff] [blame] | 796 | * @dir: directory being searched |
| 797 | * @dentry: filename being looked up |
Eric Biggers | b01531d | 2019-03-20 11:39:13 -0700 | [diff] [blame] | 798 | * @fname: (output) the name to use to search the on-disk directory |
Eric Biggers | 32c3cf0 | 2017-10-09 12:15:43 -0700 | [diff] [blame] | 799 | * |
Eric Biggers | b01531d | 2019-03-20 11:39:13 -0700 | [diff] [blame] | 800 | * Prepare for ->lookup() in a directory which may be encrypted by determining |
Eric Biggers | 70fb261 | 2020-09-23 21:26:23 -0700 | [diff] [blame] | 801 | * the name that will actually be used to search the directory on-disk. If the |
Eric Biggers | a735996 | 2020-12-02 18:20:41 -0800 | [diff] [blame] | 802 | * 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 Biggers | 32c3cf0 | 2017-10-09 12:15:43 -0700 | [diff] [blame] | 805 | * |
Eric Biggers | 3569371 | 2020-11-20 10:34:32 -0800 | [diff] [blame] | 806 | * 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 Biggers | 32c3cf0 | 2017-10-09 12:15:43 -0700 | [diff] [blame] | 811 | * |
Eric Biggers | 70fb261 | 2020-09-23 21:26:23 -0700 | [diff] [blame] | 812 | * 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 Biggers | 32c3cf0 | 2017-10-09 12:15:43 -0700 | [diff] [blame] | 815 | */ |
| 816 | static inline int fscrypt_prepare_lookup(struct inode *dir, |
| 817 | struct dentry *dentry, |
Eric Biggers | b01531d | 2019-03-20 11:39:13 -0700 | [diff] [blame] | 818 | struct fscrypt_name *fname) |
Eric Biggers | 32c3cf0 | 2017-10-09 12:15:43 -0700 | [diff] [blame] | 819 | { |
| 820 | if (IS_ENCRYPTED(dir)) |
Eric Biggers | b01531d | 2019-03-20 11:39:13 -0700 | [diff] [blame] | 821 | 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 Biggers | 32c3cf0 | 2017-10-09 12:15:43 -0700 | [diff] [blame] | 827 | return 0; |
| 828 | } |
| 829 | |
Eric Biggers | 815dac3 | 2017-10-09 12:15:44 -0700 | [diff] [blame] | 830 | /** |
Eric Biggers | 4af7c34 | 2020-12-02 18:20:37 -0800 | [diff] [blame] | 831 | * 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 Biggers | a735996 | 2020-12-02 18:20:41 -0800 | [diff] [blame] | 839 | * 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 Biggers | 4af7c34 | 2020-12-02 18:20:37 -0800 | [diff] [blame] | 842 | */ |
| 843 | static 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 Biggers | d2fe975 | 2020-05-11 12:13:56 -0700 | [diff] [blame] | 851 | * fscrypt_prepare_setattr() - prepare to change a possibly-encrypted inode's |
| 852 | * attributes |
Eric Biggers | 815dac3 | 2017-10-09 12:15:44 -0700 | [diff] [blame] | 853 | * @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 | */ |
| 868 | static inline int fscrypt_prepare_setattr(struct dentry *dentry, |
| 869 | struct iattr *attr) |
| 870 | { |
Eric Biggers | 9aa0e0a | 2020-12-02 18:20:38 -0800 | [diff] [blame] | 871 | if (IS_ENCRYPTED(d_inode(dentry))) |
| 872 | return __fscrypt_prepare_setattr(dentry, attr); |
Eric Biggers | 815dac3 | 2017-10-09 12:15:44 -0700 | [diff] [blame] | 873 | return 0; |
| 874 | } |
| 875 | |
Eric Biggers | 76e81d6 | 2018-01-05 10:45:01 -0800 | [diff] [blame] | 876 | /** |
Eric Biggers | d2fe975 | 2020-05-11 12:13:56 -0700 | [diff] [blame] | 877 | * fscrypt_encrypt_symlink() - encrypt the symlink target if needed |
Eric Biggers | 76e81d6 | 2018-01-05 10:45:01 -0800 | [diff] [blame] | 878 | * @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 | */ |
| 891 | static 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 Biggers | d2d0727 | 2019-05-20 09:29:39 -0700 | [diff] [blame] | 901 | /* If *pagep is a bounce page, free it and set *pagep to the pagecache page */ |
| 902 | static 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 Chinner | 734f0d2 | 2017-10-09 12:15:34 -0700 | [diff] [blame] | 912 | #endif /* _LINUX_FSCRYPT_H */ |