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 | b103fb7 | 2019-10-24 14:54: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 | 5fee360 | 2020-07-02 01:56:05 +0000 | [diff] [blame] | 70 | int (*get_num_devices)(struct super_block *sb); |
| 71 | void (*get_devices)(struct super_block *sb, |
| 72 | struct request_queue **devs); |
Chandan Rajendra | 643fa96 | 2018-12-12 15:20:12 +0530 | [diff] [blame] | 73 | }; |
| 74 | |
Eric Biggers | ab673b9 | 2020-07-21 15:59:19 -0700 | [diff] [blame] | 75 | static inline struct fscrypt_info *fscrypt_get_info(const struct inode *inode) |
Chandan Rajendra | 643fa96 | 2018-12-12 15:20:12 +0530 | [diff] [blame] | 76 | { |
Eric Biggers | ab673b9 | 2020-07-21 15:59:19 -0700 | [diff] [blame] | 77 | /* |
| 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 Rajendra | 643fa96 | 2018-12-12 15:20:12 +0530 | [diff] [blame] | 84 | } |
| 85 | |
Eric Biggers | 56dce71 | 2019-12-09 12:50:21 -0800 | [diff] [blame] | 86 | /** |
| 87 | * fscrypt_needs_contents_encryption() - check whether an inode needs |
| 88 | * contents encryption |
Eric Biggers | d2fe975 | 2020-05-11 12:13:56 -0700 | [diff] [blame] | 89 | * @inode: the inode to check |
Eric Biggers | 56dce71 | 2019-12-09 12:50:21 -0800 | [diff] [blame] | 90 | * |
| 91 | * Return: %true iff the inode is an encrypted regular file and the kernel was |
| 92 | * built with fscrypt support. |
| 93 | * |
| 94 | * If you need to know whether the encrypt bit is set even when the kernel was |
| 95 | * built without fscrypt support, you must use IS_ENCRYPTED() directly instead. |
| 96 | */ |
| 97 | static inline bool fscrypt_needs_contents_encryption(const struct inode *inode) |
| 98 | { |
| 99 | return IS_ENCRYPTED(inode) && S_ISREG(inode->i_mode); |
| 100 | } |
| 101 | |
Eric Biggers | 0bf3d5c1 | 2019-03-20 11:39:11 -0700 | [diff] [blame] | 102 | /* |
Eric Biggers | 501e43f | 2020-09-23 21:26:24 -0700 | [diff] [blame] | 103 | * 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 Biggers | 0bf3d5c1 | 2019-03-20 11:39:11 -0700 | [diff] [blame] | 108 | */ |
| 109 | static inline void fscrypt_handle_d_move(struct dentry *dentry) |
| 110 | { |
Eric Biggers | 501e43f | 2020-09-23 21:26:24 -0700 | [diff] [blame] | 111 | dentry->d_flags &= ~DCACHE_NOKEY_NAME; |
Eric Biggers | 0bf3d5c1 | 2019-03-20 11:39:11 -0700 | [diff] [blame] | 112 | } |
| 113 | |
Eric Biggers | 2da473e | 2020-11-17 23:56:05 -0800 | [diff] [blame^] | 114 | /** |
| 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 | */ |
| 138 | static inline bool fscrypt_is_nokey_name(const struct dentry *dentry) |
| 139 | { |
| 140 | return dentry->d_flags & DCACHE_NOKEY_NAME; |
| 141 | } |
| 142 | |
Chandan Rajendra | 643fa96 | 2018-12-12 15:20:12 +0530 | [diff] [blame] | 143 | /* crypto.c */ |
Eric Biggers | 6070090 | 2020-05-11 12:13:58 -0700 | [diff] [blame] | 144 | void fscrypt_enqueue_decrypt_work(struct work_struct *); |
Eric Biggers | 53bc1d85 | 2019-05-20 09:29:44 -0700 | [diff] [blame] | 145 | |
Eric Biggers | 6070090 | 2020-05-11 12:13:58 -0700 | [diff] [blame] | 146 | struct page *fscrypt_encrypt_pagecache_blocks(struct page *page, |
| 147 | unsigned int len, |
| 148 | unsigned int offs, |
| 149 | gfp_t gfp_flags); |
| 150 | int 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 Biggers | aa8bc1a | 2019-05-20 09:29:47 -0700 | [diff] [blame] | 153 | |
Eric Biggers | 6070090 | 2020-05-11 12:13:58 -0700 | [diff] [blame] | 154 | int fscrypt_decrypt_pagecache_blocks(struct page *page, unsigned int len, |
| 155 | unsigned int offs); |
| 156 | int fscrypt_decrypt_block_inplace(const struct inode *inode, struct page *page, |
| 157 | unsigned int len, unsigned int offs, |
| 158 | u64 lblk_num); |
Chandan Rajendra | 643fa96 | 2018-12-12 15:20:12 +0530 | [diff] [blame] | 159 | |
Eric Biggers | d2d0727 | 2019-05-20 09:29:39 -0700 | [diff] [blame] | 160 | static inline bool fscrypt_is_bounce_page(struct page *page) |
Chandan Rajendra | 643fa96 | 2018-12-12 15:20:12 +0530 | [diff] [blame] | 161 | { |
Eric Biggers | d2d0727 | 2019-05-20 09:29:39 -0700 | [diff] [blame] | 162 | return page->mapping == NULL; |
Chandan Rajendra | 643fa96 | 2018-12-12 15:20:12 +0530 | [diff] [blame] | 163 | } |
| 164 | |
Eric Biggers | d2d0727 | 2019-05-20 09:29:39 -0700 | [diff] [blame] | 165 | static inline struct page *fscrypt_pagecache_page(struct page *bounce_page) |
| 166 | { |
| 167 | return (struct page *)page_private(bounce_page); |
| 168 | } |
| 169 | |
Eric Biggers | 6070090 | 2020-05-11 12:13:58 -0700 | [diff] [blame] | 170 | void fscrypt_free_bounce_page(struct page *bounce_page); |
Chandan Rajendra | 643fa96 | 2018-12-12 15:20:12 +0530 | [diff] [blame] | 171 | |
| 172 | /* policy.c */ |
Eric Biggers | 6070090 | 2020-05-11 12:13:58 -0700 | [diff] [blame] | 173 | int fscrypt_ioctl_set_policy(struct file *filp, const void __user *arg); |
| 174 | int fscrypt_ioctl_get_policy(struct file *filp, void __user *arg); |
| 175 | int fscrypt_ioctl_get_policy_ex(struct file *filp, void __user *arg); |
| 176 | int fscrypt_ioctl_get_nonce(struct file *filp, void __user *arg); |
| 177 | int fscrypt_has_permitted_context(struct inode *parent, struct inode *child); |
Eric Biggers | a992b20 | 2020-09-16 21:11:24 -0700 | [diff] [blame] | 178 | int fscrypt_set_context(struct inode *inode, void *fs_data); |
Eric Biggers | fe015a7 | 2020-05-11 12:13:57 -0700 | [diff] [blame] | 179 | |
Eric Biggers | ac4acb1 | 2020-09-16 21:11:35 -0700 | [diff] [blame] | 180 | struct fscrypt_dummy_policy { |
| 181 | const union fscrypt_policy *policy; |
Eric Biggers | ed318a6 | 2020-05-12 16:32:50 -0700 | [diff] [blame] | 182 | }; |
| 183 | |
Eric Biggers | c8c868a | 2020-09-16 21:11:36 -0700 | [diff] [blame] | 184 | 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] | 185 | struct fscrypt_dummy_policy *dummy_policy); |
Eric Biggers | ed318a6 | 2020-05-12 16:32:50 -0700 | [diff] [blame] | 186 | void fscrypt_show_test_dummy_encryption(struct seq_file *seq, char sep, |
| 187 | struct super_block *sb); |
| 188 | static inline void |
Eric Biggers | ac4acb1 | 2020-09-16 21:11:35 -0700 | [diff] [blame] | 189 | fscrypt_free_dummy_policy(struct fscrypt_dummy_policy *dummy_policy) |
Eric Biggers | ed318a6 | 2020-05-12 16:32:50 -0700 | [diff] [blame] | 190 | { |
Eric Biggers | ac4acb1 | 2020-09-16 21:11:35 -0700 | [diff] [blame] | 191 | kfree(dummy_policy->policy); |
| 192 | dummy_policy->policy = NULL; |
Eric Biggers | ed318a6 | 2020-05-12 16:32:50 -0700 | [diff] [blame] | 193 | } |
| 194 | |
Eric Biggers | 22d94f4 | 2019-08-04 19:35:46 -0700 | [diff] [blame] | 195 | /* keyring.c */ |
Eric Biggers | 6070090 | 2020-05-11 12:13:58 -0700 | [diff] [blame] | 196 | void fscrypt_sb_free(struct super_block *sb); |
| 197 | int fscrypt_ioctl_add_key(struct file *filp, void __user *arg); |
| 198 | int fscrypt_ioctl_remove_key(struct file *filp, void __user *arg); |
| 199 | int fscrypt_ioctl_remove_key_all_users(struct file *filp, void __user *arg); |
| 200 | int fscrypt_ioctl_get_key_status(struct file *filp, void __user *arg); |
Eric Biggers | 22d94f4 | 2019-08-04 19:35:46 -0700 | [diff] [blame] | 201 | |
Eric Biggers | feed825 | 2019-08-04 19:35:45 -0700 | [diff] [blame] | 202 | /* keysetup.c */ |
Eric Biggers | 6070090 | 2020-05-11 12:13:58 -0700 | [diff] [blame] | 203 | int fscrypt_get_encryption_info(struct inode *inode); |
Eric Biggers | a992b20 | 2020-09-16 21:11:24 -0700 | [diff] [blame] | 204 | int fscrypt_prepare_new_inode(struct inode *dir, struct inode *inode, |
| 205 | bool *encrypt_ret); |
Eric Biggers | 6070090 | 2020-05-11 12:13:58 -0700 | [diff] [blame] | 206 | void fscrypt_put_encryption_info(struct inode *inode); |
| 207 | void fscrypt_free_inode(struct inode *inode); |
| 208 | int fscrypt_drop_inode(struct inode *inode); |
Chandan Rajendra | 643fa96 | 2018-12-12 15:20:12 +0530 | [diff] [blame] | 209 | |
| 210 | /* fname.c */ |
Eric Biggers | 6070090 | 2020-05-11 12:13:58 -0700 | [diff] [blame] | 211 | int fscrypt_setup_filename(struct inode *inode, const struct qstr *iname, |
| 212 | int lookup, struct fscrypt_name *fname); |
Chandan Rajendra | 643fa96 | 2018-12-12 15:20:12 +0530 | [diff] [blame] | 213 | |
| 214 | static inline void fscrypt_free_filename(struct fscrypt_name *fname) |
| 215 | { |
| 216 | kfree(fname->crypto_buf.name); |
| 217 | } |
| 218 | |
Jeff Layton | 8b10fe6 | 2020-08-10 10:21:39 -0400 | [diff] [blame] | 219 | int fscrypt_fname_alloc_buffer(u32 max_encrypted_len, |
Eric Biggers | 6070090 | 2020-05-11 12:13:58 -0700 | [diff] [blame] | 220 | struct fscrypt_str *crypto_str); |
| 221 | void fscrypt_fname_free_buffer(struct fscrypt_str *crypto_str); |
| 222 | int 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); |
| 226 | bool fscrypt_match_name(const struct fscrypt_name *fname, |
| 227 | const u8 *de_name, u32 de_name_len); |
| 228 | u64 fscrypt_fname_siphash(const struct inode *dir, const struct qstr *name); |
Eric Biggers | 5b2a828 | 2020-09-23 22:47:21 -0700 | [diff] [blame] | 229 | int fscrypt_d_revalidate(struct dentry *dentry, unsigned int flags); |
Daniel Rosenberg | aa408f8 | 2020-01-20 14:31:57 -0800 | [diff] [blame] | 230 | |
Chandan Rajendra | 643fa96 | 2018-12-12 15:20:12 +0530 | [diff] [blame] | 231 | /* bio.c */ |
Eric Biggers | 6070090 | 2020-05-11 12:13:58 -0700 | [diff] [blame] | 232 | void fscrypt_decrypt_bio(struct bio *bio); |
| 233 | int fscrypt_zeroout_range(const struct inode *inode, pgoff_t lblk, |
| 234 | sector_t pblk, unsigned int len); |
Chandan Rajendra | 643fa96 | 2018-12-12 15:20:12 +0530 | [diff] [blame] | 235 | |
| 236 | /* hooks.c */ |
Eric Biggers | 6070090 | 2020-05-11 12:13:58 -0700 | [diff] [blame] | 237 | int fscrypt_file_open(struct inode *inode, struct file *filp); |
| 238 | int __fscrypt_prepare_link(struct inode *inode, struct inode *dir, |
| 239 | struct dentry *dentry); |
| 240 | int __fscrypt_prepare_rename(struct inode *old_dir, struct dentry *old_dentry, |
| 241 | struct inode *new_dir, struct dentry *new_dentry, |
| 242 | unsigned int flags); |
| 243 | int __fscrypt_prepare_lookup(struct inode *dir, struct dentry *dentry, |
| 244 | struct fscrypt_name *fname); |
| 245 | int fscrypt_prepare_setflags(struct inode *inode, |
| 246 | unsigned int oldflags, unsigned int flags); |
Eric Biggers | 3111472 | 2020-09-16 21:11:34 -0700 | [diff] [blame] | 247 | int fscrypt_prepare_symlink(struct inode *dir, const char *target, |
| 248 | unsigned int len, unsigned int max_len, |
| 249 | struct fscrypt_str *disk_link); |
Eric Biggers | 6070090 | 2020-05-11 12:13:58 -0700 | [diff] [blame] | 250 | int __fscrypt_encrypt_symlink(struct inode *inode, const char *target, |
| 251 | unsigned int len, struct fscrypt_str *disk_link); |
| 252 | const char *fscrypt_get_symlink(struct inode *inode, const void *caddr, |
| 253 | unsigned int max_size, |
| 254 | struct delayed_call *done); |
Sascha Hauer | eea2c05 | 2019-03-26 08:52:31 +0100 | [diff] [blame] | 255 | static 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 Rajendra | 643fa96 | 2018-12-12 15:20:12 +0530 | [diff] [blame] | 260 | #else /* !CONFIG_FS_ENCRYPTION */ |
| 261 | |
Eric Biggers | ab673b9 | 2020-07-21 15:59:19 -0700 | [diff] [blame] | 262 | static inline struct fscrypt_info *fscrypt_get_info(const struct inode *inode) |
Chandan Rajendra | 643fa96 | 2018-12-12 15:20:12 +0530 | [diff] [blame] | 263 | { |
Eric Biggers | ab673b9 | 2020-07-21 15:59:19 -0700 | [diff] [blame] | 264 | return NULL; |
Chandan Rajendra | 643fa96 | 2018-12-12 15:20:12 +0530 | [diff] [blame] | 265 | } |
| 266 | |
Eric Biggers | 56dce71 | 2019-12-09 12:50:21 -0800 | [diff] [blame] | 267 | static inline bool fscrypt_needs_contents_encryption(const struct inode *inode) |
| 268 | { |
| 269 | return false; |
| 270 | } |
| 271 | |
Eric Biggers | 0bf3d5c1 | 2019-03-20 11:39:11 -0700 | [diff] [blame] | 272 | static inline void fscrypt_handle_d_move(struct dentry *dentry) |
| 273 | { |
| 274 | } |
| 275 | |
Eric Biggers | 2da473e | 2020-11-17 23:56:05 -0800 | [diff] [blame^] | 276 | static inline bool fscrypt_is_nokey_name(const struct dentry *dentry) |
| 277 | { |
| 278 | return false; |
| 279 | } |
| 280 | |
Chandan Rajendra | 643fa96 | 2018-12-12 15:20:12 +0530 | [diff] [blame] | 281 | /* crypto.c */ |
| 282 | static inline void fscrypt_enqueue_decrypt_work(struct work_struct *work) |
| 283 | { |
| 284 | } |
| 285 | |
Eric Biggers | 53bc1d85 | 2019-05-20 09:29:44 -0700 | [diff] [blame] | 286 | static inline struct page *fscrypt_encrypt_pagecache_blocks(struct page *page, |
| 287 | unsigned int len, |
| 288 | unsigned int offs, |
| 289 | gfp_t gfp_flags) |
Chandan Rajendra | 643fa96 | 2018-12-12 15:20:12 +0530 | [diff] [blame] | 290 | { |
| 291 | return ERR_PTR(-EOPNOTSUPP); |
| 292 | } |
| 293 | |
Eric Biggers | 03569f2 | 2019-05-20 09:29:43 -0700 | [diff] [blame] | 294 | static 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 Biggers | aa8bc1a | 2019-05-20 09:29:47 -0700 | [diff] [blame] | 303 | static inline int fscrypt_decrypt_pagecache_blocks(struct page *page, |
| 304 | unsigned int len, |
| 305 | unsigned int offs) |
Chandan Rajendra | 643fa96 | 2018-12-12 15:20:12 +0530 | [diff] [blame] | 306 | { |
| 307 | return -EOPNOTSUPP; |
| 308 | } |
| 309 | |
Eric Biggers | 41adbcb | 2019-05-20 09:29:46 -0700 | [diff] [blame] | 310 | static 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 Biggers | d2d0727 | 2019-05-20 09:29:39 -0700 | [diff] [blame] | 318 | static inline bool fscrypt_is_bounce_page(struct page *page) |
| 319 | { |
| 320 | return false; |
| 321 | } |
| 322 | |
| 323 | static inline struct page *fscrypt_pagecache_page(struct page *bounce_page) |
Chandan Rajendra | 643fa96 | 2018-12-12 15:20:12 +0530 | [diff] [blame] | 324 | { |
| 325 | WARN_ON_ONCE(1); |
| 326 | return ERR_PTR(-EINVAL); |
| 327 | } |
| 328 | |
Eric Biggers | d2d0727 | 2019-05-20 09:29:39 -0700 | [diff] [blame] | 329 | static inline void fscrypt_free_bounce_page(struct page *bounce_page) |
Chandan Rajendra | 643fa96 | 2018-12-12 15:20:12 +0530 | [diff] [blame] | 330 | { |
Chandan Rajendra | 643fa96 | 2018-12-12 15:20:12 +0530 | [diff] [blame] | 331 | } |
| 332 | |
| 333 | /* policy.c */ |
| 334 | static inline int fscrypt_ioctl_set_policy(struct file *filp, |
| 335 | const void __user *arg) |
| 336 | { |
| 337 | return -EOPNOTSUPP; |
| 338 | } |
| 339 | |
| 340 | static inline int fscrypt_ioctl_get_policy(struct file *filp, void __user *arg) |
| 341 | { |
| 342 | return -EOPNOTSUPP; |
| 343 | } |
| 344 | |
Eric Biggers | 5dae460 | 2019-08-04 19:35:47 -0700 | [diff] [blame] | 345 | static inline int fscrypt_ioctl_get_policy_ex(struct file *filp, |
| 346 | void __user *arg) |
| 347 | { |
| 348 | return -EOPNOTSUPP; |
| 349 | } |
| 350 | |
Eric Biggers | e98ad46 | 2020-03-14 13:50:49 -0700 | [diff] [blame] | 351 | static inline int fscrypt_ioctl_get_nonce(struct file *filp, void __user *arg) |
| 352 | { |
| 353 | return -EOPNOTSUPP; |
| 354 | } |
| 355 | |
Chandan Rajendra | 643fa96 | 2018-12-12 15:20:12 +0530 | [diff] [blame] | 356 | static inline int fscrypt_has_permitted_context(struct inode *parent, |
| 357 | struct inode *child) |
| 358 | { |
| 359 | return 0; |
| 360 | } |
| 361 | |
Eric Biggers | a992b20 | 2020-09-16 21:11:24 -0700 | [diff] [blame] | 362 | static inline int fscrypt_set_context(struct inode *inode, void *fs_data) |
| 363 | { |
| 364 | return -EOPNOTSUPP; |
| 365 | } |
| 366 | |
Eric Biggers | ac4acb1 | 2020-09-16 21:11:35 -0700 | [diff] [blame] | 367 | struct fscrypt_dummy_policy { |
Eric Biggers | ed318a6 | 2020-05-12 16:32:50 -0700 | [diff] [blame] | 368 | }; |
| 369 | |
| 370 | static inline void fscrypt_show_test_dummy_encryption(struct seq_file *seq, |
| 371 | char sep, |
| 372 | struct super_block *sb) |
| 373 | { |
| 374 | } |
| 375 | |
| 376 | static inline void |
Eric Biggers | ac4acb1 | 2020-09-16 21:11:35 -0700 | [diff] [blame] | 377 | fscrypt_free_dummy_policy(struct fscrypt_dummy_policy *dummy_policy) |
Eric Biggers | ed318a6 | 2020-05-12 16:32:50 -0700 | [diff] [blame] | 378 | { |
| 379 | } |
| 380 | |
Eric Biggers | 22d94f4 | 2019-08-04 19:35:46 -0700 | [diff] [blame] | 381 | /* keyring.c */ |
| 382 | static inline void fscrypt_sb_free(struct super_block *sb) |
| 383 | { |
| 384 | } |
| 385 | |
| 386 | static inline int fscrypt_ioctl_add_key(struct file *filp, void __user *arg) |
| 387 | { |
| 388 | return -EOPNOTSUPP; |
| 389 | } |
| 390 | |
Eric Biggers | b1c0ec3 | 2019-08-04 19:35:46 -0700 | [diff] [blame] | 391 | static inline int fscrypt_ioctl_remove_key(struct file *filp, void __user *arg) |
| 392 | { |
| 393 | return -EOPNOTSUPP; |
| 394 | } |
| 395 | |
Eric Biggers | 78a1b96b | 2019-08-04 19:35:47 -0700 | [diff] [blame] | 396 | static inline int fscrypt_ioctl_remove_key_all_users(struct file *filp, |
| 397 | void __user *arg) |
| 398 | { |
| 399 | return -EOPNOTSUPP; |
| 400 | } |
| 401 | |
Eric Biggers | 5a7e299 | 2019-08-04 19:35:46 -0700 | [diff] [blame] | 402 | static inline int fscrypt_ioctl_get_key_status(struct file *filp, |
| 403 | void __user *arg) |
| 404 | { |
| 405 | return -EOPNOTSUPP; |
| 406 | } |
| 407 | |
Eric Biggers | feed825 | 2019-08-04 19:35:45 -0700 | [diff] [blame] | 408 | /* keysetup.c */ |
Chandan Rajendra | 643fa96 | 2018-12-12 15:20:12 +0530 | [diff] [blame] | 409 | static inline int fscrypt_get_encryption_info(struct inode *inode) |
| 410 | { |
| 411 | return -EOPNOTSUPP; |
| 412 | } |
| 413 | |
Eric Biggers | a992b20 | 2020-09-16 21:11:24 -0700 | [diff] [blame] | 414 | static 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 Rajendra | 643fa96 | 2018-12-12 15:20:12 +0530 | [diff] [blame] | 423 | static inline void fscrypt_put_encryption_info(struct inode *inode) |
| 424 | { |
| 425 | return; |
| 426 | } |
| 427 | |
Eric Biggers | 2c58d54 | 2019-04-10 13:21:15 -0700 | [diff] [blame] | 428 | static inline void fscrypt_free_inode(struct inode *inode) |
| 429 | { |
| 430 | } |
| 431 | |
Eric Biggers | b1c0ec3 | 2019-08-04 19:35:46 -0700 | [diff] [blame] | 432 | static inline int fscrypt_drop_inode(struct inode *inode) |
| 433 | { |
| 434 | return 0; |
| 435 | } |
| 436 | |
Chandan Rajendra | 643fa96 | 2018-12-12 15:20:12 +0530 | [diff] [blame] | 437 | /* fname.c */ |
| 438 | static 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 Biggers | b01531d | 2019-03-20 11:39:13 -0700 | [diff] [blame] | 445 | memset(fname, 0, sizeof(*fname)); |
Chandan Rajendra | 643fa96 | 2018-12-12 15:20:12 +0530 | [diff] [blame] | 446 | 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 | |
| 452 | static inline void fscrypt_free_filename(struct fscrypt_name *fname) |
| 453 | { |
| 454 | return; |
| 455 | } |
| 456 | |
Jeff Layton | 8b10fe6 | 2020-08-10 10:21:39 -0400 | [diff] [blame] | 457 | static inline int fscrypt_fname_alloc_buffer(u32 max_encrypted_len, |
Chandan Rajendra | 643fa96 | 2018-12-12 15:20:12 +0530 | [diff] [blame] | 458 | struct fscrypt_str *crypto_str) |
| 459 | { |
| 460 | return -EOPNOTSUPP; |
| 461 | } |
| 462 | |
| 463 | static inline void fscrypt_fname_free_buffer(struct fscrypt_str *crypto_str) |
| 464 | { |
| 465 | return; |
| 466 | } |
| 467 | |
Eric Biggers | 8a4ab0b | 2019-12-15 13:39:47 -0800 | [diff] [blame] | 468 | static inline int fscrypt_fname_disk_to_usr(const struct inode *inode, |
Chandan Rajendra | 643fa96 | 2018-12-12 15:20:12 +0530 | [diff] [blame] | 469 | u32 hash, u32 minor_hash, |
| 470 | const struct fscrypt_str *iname, |
| 471 | struct fscrypt_str *oname) |
| 472 | { |
| 473 | return -EOPNOTSUPP; |
| 474 | } |
| 475 | |
| 476 | static 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 Rosenberg | aa408f8 | 2020-01-20 14:31:57 -0800 | [diff] [blame] | 485 | static 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 Biggers | 5b2a828 | 2020-09-23 22:47:21 -0700 | [diff] [blame] | 492 | static inline int fscrypt_d_revalidate(struct dentry *dentry, |
| 493 | unsigned int flags) |
| 494 | { |
| 495 | return 1; |
| 496 | } |
| 497 | |
Chandan Rajendra | 643fa96 | 2018-12-12 15:20:12 +0530 | [diff] [blame] | 498 | /* bio.c */ |
| 499 | static inline void fscrypt_decrypt_bio(struct bio *bio) |
| 500 | { |
| 501 | } |
| 502 | |
Chandan Rajendra | 643fa96 | 2018-12-12 15:20:12 +0530 | [diff] [blame] | 503 | static 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 | |
| 511 | static 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 Biggers | 968dd6d | 2019-03-20 11:39:10 -0700 | [diff] [blame] | 518 | static inline int __fscrypt_prepare_link(struct inode *inode, struct inode *dir, |
| 519 | struct dentry *dentry) |
Chandan Rajendra | 643fa96 | 2018-12-12 15:20:12 +0530 | [diff] [blame] | 520 | { |
| 521 | return -EOPNOTSUPP; |
| 522 | } |
| 523 | |
| 524 | static 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 | |
| 533 | static inline int __fscrypt_prepare_lookup(struct inode *dir, |
Eric Biggers | b01531d | 2019-03-20 11:39:13 -0700 | [diff] [blame] | 534 | struct dentry *dentry, |
| 535 | struct fscrypt_name *fname) |
Chandan Rajendra | 643fa96 | 2018-12-12 15:20:12 +0530 | [diff] [blame] | 536 | { |
| 537 | return -EOPNOTSUPP; |
| 538 | } |
| 539 | |
Daniel Rosenberg | 6e1918c | 2020-01-20 14:31:56 -0800 | [diff] [blame] | 540 | static inline int fscrypt_prepare_setflags(struct inode *inode, |
| 541 | unsigned int oldflags, |
| 542 | unsigned int flags) |
| 543 | { |
| 544 | return 0; |
| 545 | } |
| 546 | |
Eric Biggers | 3111472 | 2020-09-16 21:11:34 -0700 | [diff] [blame] | 547 | static 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 Rajendra | 643fa96 | 2018-12-12 15:20:12 +0530 | [diff] [blame] | 552 | { |
Eric Biggers | 3111472 | 2020-09-16 21:11:34 -0700 | [diff] [blame] | 553 | 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 Rajendra | 643fa96 | 2018-12-12 15:20:12 +0530 | [diff] [blame] | 560 | } |
| 561 | |
Chandan Rajendra | 643fa96 | 2018-12-12 15:20:12 +0530 | [diff] [blame] | 562 | static 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 | |
| 570 | static 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 Hauer | eea2c05 | 2019-03-26 08:52:31 +0100 | [diff] [blame] | 577 | |
| 578 | static inline void fscrypt_set_ops(struct super_block *sb, |
| 579 | const struct fscrypt_operations *s_cop) |
| 580 | { |
| 581 | } |
| 582 | |
Chandan Rajendra | 643fa96 | 2018-12-12 15:20:12 +0530 | [diff] [blame] | 583 | #endif /* !CONFIG_FS_ENCRYPTION */ |
Dave Chinner | 734f0d2 | 2017-10-09 12:15:34 -0700 | [diff] [blame] | 584 | |
Satya Tangirala | 5fee360 | 2020-07-02 01:56:05 +0000 | [diff] [blame] | 585 | /* inline_crypt.c */ |
| 586 | #ifdef CONFIG_FS_ENCRYPTION_INLINE_CRYPT |
| 587 | |
| 588 | bool __fscrypt_inode_uses_inline_crypto(const struct inode *inode); |
| 589 | |
| 590 | void fscrypt_set_bio_crypt_ctx(struct bio *bio, |
| 591 | const struct inode *inode, u64 first_lblk, |
| 592 | gfp_t gfp_mask); |
| 593 | |
| 594 | void fscrypt_set_bio_crypt_ctx_bh(struct bio *bio, |
| 595 | const struct buffer_head *first_bh, |
| 596 | gfp_t gfp_mask); |
| 597 | |
| 598 | bool fscrypt_mergeable_bio(struct bio *bio, const struct inode *inode, |
| 599 | u64 next_lblk); |
| 600 | |
| 601 | bool fscrypt_mergeable_bio_bh(struct bio *bio, |
| 602 | const struct buffer_head *next_bh); |
| 603 | |
| 604 | #else /* CONFIG_FS_ENCRYPTION_INLINE_CRYPT */ |
| 605 | |
| 606 | static inline bool __fscrypt_inode_uses_inline_crypto(const struct inode *inode) |
| 607 | { |
| 608 | return false; |
| 609 | } |
| 610 | |
| 611 | static inline void fscrypt_set_bio_crypt_ctx(struct bio *bio, |
| 612 | const struct inode *inode, |
| 613 | u64 first_lblk, gfp_t gfp_mask) { } |
| 614 | |
| 615 | static 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 | |
| 620 | static inline bool fscrypt_mergeable_bio(struct bio *bio, |
| 621 | const struct inode *inode, |
| 622 | u64 next_lblk) |
| 623 | { |
| 624 | return true; |
| 625 | } |
| 626 | |
| 627 | static 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 | */ |
| 643 | static 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 | */ |
| 658 | static 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 Biggers | d293c3e | 2017-10-09 12:15:39 -0700 | [diff] [blame] | 664 | /** |
Eric Biggers | ab673b9 | 2020-07-21 15:59:19 -0700 | [diff] [blame] | 665 | * 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 | */ |
| 673 | static inline bool fscrypt_has_encryption_key(const struct inode *inode) |
| 674 | { |
| 675 | return fscrypt_get_info(inode) != NULL; |
| 676 | } |
| 677 | |
| 678 | /** |
Eric Biggers | d2fe975 | 2020-05-11 12:13:56 -0700 | [diff] [blame] | 679 | * fscrypt_require_key() - require an inode's encryption key |
Eric Biggers | d293c3e | 2017-10-09 12:15:39 -0700 | [diff] [blame] | 680 | * @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 | */ |
| 691 | static 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 Chinner | 734f0d2 | 2017-10-09 12:15:34 -0700 | [diff] [blame] | 703 | |
Eric Biggers | 0ea87a9 | 2017-10-09 12:15:41 -0700 | [diff] [blame] | 704 | /** |
Eric Biggers | d2fe975 | 2020-05-11 12:13:56 -0700 | [diff] [blame] | 705 | * fscrypt_prepare_link() - prepare to link an inode into a possibly-encrypted |
| 706 | * directory |
Eric Biggers | 0ea87a9 | 2017-10-09 12:15:41 -0700 | [diff] [blame] | 707 | * @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 Biggers | f5e55e7 | 2019-01-22 16:20:21 -0800 | [diff] [blame] | 720 | * -EXDEV if the link would result in an inconsistent encryption policy, or |
Eric Biggers | 0ea87a9 | 2017-10-09 12:15:41 -0700 | [diff] [blame] | 721 | * another -errno code. |
| 722 | */ |
| 723 | static inline int fscrypt_prepare_link(struct dentry *old_dentry, |
| 724 | struct inode *dir, |
| 725 | struct dentry *dentry) |
| 726 | { |
| 727 | if (IS_ENCRYPTED(dir)) |
Eric Biggers | 968dd6d | 2019-03-20 11:39:10 -0700 | [diff] [blame] | 728 | return __fscrypt_prepare_link(d_inode(old_dentry), dir, dentry); |
Eric Biggers | 0ea87a9 | 2017-10-09 12:15:41 -0700 | [diff] [blame] | 729 | return 0; |
| 730 | } |
| 731 | |
Eric Biggers | 94b26f3 | 2017-10-09 12:15:42 -0700 | [diff] [blame] | 732 | /** |
Eric Biggers | d2fe975 | 2020-05-11 12:13:56 -0700 | [diff] [blame] | 733 | * fscrypt_prepare_rename() - prepare for a rename between possibly-encrypted |
| 734 | * directories |
Eric Biggers | 94b26f3 | 2017-10-09 12:15:42 -0700 | [diff] [blame] | 735 | * @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 Biggers | f5e55e7 | 2019-01-22 16:20:21 -0800 | [diff] [blame] | 751 | * 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] | 752 | * rename would cause inconsistent encryption policies, or another -errno code. |
| 753 | */ |
| 754 | static 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 Biggers | 32c3cf0 | 2017-10-09 12:15:43 -0700 | [diff] [blame] | 766 | /** |
Eric Biggers | d2fe975 | 2020-05-11 12:13:56 -0700 | [diff] [blame] | 767 | * fscrypt_prepare_lookup() - prepare to lookup a name in a possibly-encrypted |
| 768 | * directory |
Eric Biggers | 32c3cf0 | 2017-10-09 12:15:43 -0700 | [diff] [blame] | 769 | * @dir: directory being searched |
| 770 | * @dentry: filename being looked up |
Eric Biggers | b01531d | 2019-03-20 11:39:13 -0700 | [diff] [blame] | 771 | * @fname: (output) the name to use to search the on-disk directory |
Eric Biggers | 32c3cf0 | 2017-10-09 12:15:43 -0700 | [diff] [blame] | 772 | * |
Eric Biggers | b01531d | 2019-03-20 11:39:13 -0700 | [diff] [blame] | 773 | * Prepare for ->lookup() in a directory which may be encrypted by determining |
Eric Biggers | 70fb261 | 2020-09-23 21:26:23 -0700 | [diff] [blame] | 774 | * 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 Biggers | 32c3cf0 | 2017-10-09 12:15:43 -0700 | [diff] [blame] | 777 | * |
Eric Biggers | 6cc2486 | 2019-03-20 11:39:09 -0700 | [diff] [blame] | 778 | * 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 Biggers | 32c3cf0 | 2017-10-09 12:15:43 -0700 | [diff] [blame] | 780 | * |
Eric Biggers | 70fb261 | 2020-09-23 21:26:23 -0700 | [diff] [blame] | 781 | * 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 Biggers | 32c3cf0 | 2017-10-09 12:15:43 -0700 | [diff] [blame] | 784 | */ |
| 785 | static inline int fscrypt_prepare_lookup(struct inode *dir, |
| 786 | struct dentry *dentry, |
Eric Biggers | b01531d | 2019-03-20 11:39:13 -0700 | [diff] [blame] | 787 | struct fscrypt_name *fname) |
Eric Biggers | 32c3cf0 | 2017-10-09 12:15:43 -0700 | [diff] [blame] | 788 | { |
| 789 | if (IS_ENCRYPTED(dir)) |
Eric Biggers | b01531d | 2019-03-20 11:39:13 -0700 | [diff] [blame] | 790 | 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 Biggers | 32c3cf0 | 2017-10-09 12:15:43 -0700 | [diff] [blame] | 796 | return 0; |
| 797 | } |
| 798 | |
Eric Biggers | 815dac3 | 2017-10-09 12:15:44 -0700 | [diff] [blame] | 799 | /** |
Eric Biggers | d2fe975 | 2020-05-11 12:13:56 -0700 | [diff] [blame] | 800 | * fscrypt_prepare_setattr() - prepare to change a possibly-encrypted inode's |
| 801 | * attributes |
Eric Biggers | 815dac3 | 2017-10-09 12:15:44 -0700 | [diff] [blame] | 802 | * @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 | */ |
| 817 | static 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 Biggers | 76e81d6 | 2018-01-05 10:45:01 -0800 | [diff] [blame] | 825 | /** |
Eric Biggers | d2fe975 | 2020-05-11 12:13:56 -0700 | [diff] [blame] | 826 | * fscrypt_encrypt_symlink() - encrypt the symlink target if needed |
Eric Biggers | 76e81d6 | 2018-01-05 10:45:01 -0800 | [diff] [blame] | 827 | * @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 | */ |
| 840 | static 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 Biggers | d2d0727 | 2019-05-20 09:29:39 -0700 | [diff] [blame] | 850 | /* If *pagep is a bounce page, free it and set *pagep to the pagecache page */ |
| 851 | static 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 Chinner | 734f0d2 | 2017-10-09 12:15:34 -0700 | [diff] [blame] | 861 | #endif /* _LINUX_FSCRYPT_H */ |