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 | 542060c | 2018-01-05 10:44:55 -0800 | [diff] [blame] | 23 | struct fscrypt_ctx; |
Eric Biggers | 46f47e4 | 2017-01-24 10:58:06 -0800 | [diff] [blame] | 24 | struct fscrypt_info; |
| 25 | |
Eric Biggers | 46f47e4 | 2017-01-24 10:58:06 -0800 | [diff] [blame] | 26 | struct fscrypt_str { |
| 27 | unsigned char *name; |
| 28 | u32 len; |
| 29 | }; |
| 30 | |
| 31 | struct fscrypt_name { |
| 32 | const struct qstr *usr_fname; |
| 33 | struct fscrypt_str disk_name; |
| 34 | u32 hash; |
| 35 | u32 minor_hash; |
| 36 | struct fscrypt_str crypto_buf; |
Eric Biggers | b01531d | 2019-03-20 11:39:13 -0700 | [diff] [blame] | 37 | bool is_ciphertext_name; |
Eric Biggers | 46f47e4 | 2017-01-24 10:58:06 -0800 | [diff] [blame] | 38 | }; |
| 39 | |
| 40 | #define FSTR_INIT(n, l) { .name = n, .len = l } |
| 41 | #define FSTR_TO_QSTR(f) QSTR_INIT((f)->name, (f)->len) |
| 42 | #define fname_name(p) ((p)->disk_name.name) |
| 43 | #define fname_len(p) ((p)->disk_name.len) |
| 44 | |
Tahsin Erdogan | af65207 | 2017-07-06 00:01:59 -0400 | [diff] [blame] | 45 | /* Maximum value for the third parameter of fscrypt_operations.set_context(). */ |
| 46 | #define FSCRYPT_SET_CONTEXT_MAX_SIZE 28 |
| 47 | |
Chandan Rajendra | 643fa96 | 2018-12-12 15:20:12 +0530 | [diff] [blame] | 48 | #ifdef CONFIG_FS_ENCRYPTION |
| 49 | /* |
| 50 | * fscrypt superblock flags |
| 51 | */ |
| 52 | #define FS_CFLG_OWN_PAGES (1U << 1) |
| 53 | |
| 54 | /* |
| 55 | * crypto operations for filesystems |
| 56 | */ |
| 57 | struct fscrypt_operations { |
| 58 | unsigned int flags; |
| 59 | const char *key_prefix; |
| 60 | int (*get_context)(struct inode *, void *, size_t); |
| 61 | int (*set_context)(struct inode *, const void *, size_t, void *); |
| 62 | bool (*dummy_context)(struct inode *); |
| 63 | bool (*empty_dir)(struct inode *); |
| 64 | unsigned int max_namelen; |
| 65 | }; |
| 66 | |
Eric Biggers | 2a415a0 | 2019-05-20 09:29:40 -0700 | [diff] [blame] | 67 | /* Decryption work */ |
Chandan Rajendra | 643fa96 | 2018-12-12 15:20:12 +0530 | [diff] [blame] | 68 | struct fscrypt_ctx { |
| 69 | union { |
| 70 | struct { |
Chandan Rajendra | 643fa96 | 2018-12-12 15:20:12 +0530 | [diff] [blame] | 71 | struct bio *bio; |
| 72 | struct work_struct work; |
Eric Biggers | 2a415a0 | 2019-05-20 09:29:40 -0700 | [diff] [blame] | 73 | }; |
Chandan Rajendra | 643fa96 | 2018-12-12 15:20:12 +0530 | [diff] [blame] | 74 | struct list_head free_list; /* Free list */ |
| 75 | }; |
| 76 | u8 flags; /* Flags */ |
| 77 | }; |
| 78 | |
| 79 | static inline bool fscrypt_has_encryption_key(const struct inode *inode) |
| 80 | { |
Eric Biggers | e37a784 | 2019-04-11 14:32:15 -0700 | [diff] [blame] | 81 | /* pairs with cmpxchg_release() in fscrypt_get_encryption_info() */ |
| 82 | return READ_ONCE(inode->i_crypt_info) != NULL; |
Chandan Rajendra | 643fa96 | 2018-12-12 15:20:12 +0530 | [diff] [blame] | 83 | } |
| 84 | |
| 85 | static inline bool fscrypt_dummy_context_enabled(struct inode *inode) |
| 86 | { |
| 87 | return inode->i_sb->s_cop->dummy_context && |
| 88 | inode->i_sb->s_cop->dummy_context(inode); |
| 89 | } |
| 90 | |
Eric Biggers | 0bf3d5c1 | 2019-03-20 11:39:11 -0700 | [diff] [blame] | 91 | /* |
| 92 | * When d_splice_alias() moves a directory's encrypted alias to its decrypted |
| 93 | * alias as a result of the encryption key being added, DCACHE_ENCRYPTED_NAME |
| 94 | * must be cleared. Note that we don't have to support arbitrary moves of this |
| 95 | * flag because fscrypt doesn't allow encrypted aliases to be the source or |
| 96 | * target of a rename(). |
| 97 | */ |
| 98 | static inline void fscrypt_handle_d_move(struct dentry *dentry) |
| 99 | { |
| 100 | dentry->d_flags &= ~DCACHE_ENCRYPTED_NAME; |
| 101 | } |
| 102 | |
Chandan Rajendra | 643fa96 | 2018-12-12 15:20:12 +0530 | [diff] [blame] | 103 | /* crypto.c */ |
| 104 | extern void fscrypt_enqueue_decrypt_work(struct work_struct *); |
Eric Biggers | cd0265f | 2019-03-18 10:23:33 -0700 | [diff] [blame] | 105 | extern struct fscrypt_ctx *fscrypt_get_ctx(gfp_t); |
Chandan Rajendra | 643fa96 | 2018-12-12 15:20:12 +0530 | [diff] [blame] | 106 | extern void fscrypt_release_ctx(struct fscrypt_ctx *); |
Eric Biggers | 53bc1d85 | 2019-05-20 09:29:44 -0700 | [diff] [blame] | 107 | |
| 108 | extern struct page *fscrypt_encrypt_pagecache_blocks(struct page *page, |
| 109 | unsigned int len, |
| 110 | unsigned int offs, |
| 111 | gfp_t gfp_flags); |
Eric Biggers | 03569f2 | 2019-05-20 09:29:43 -0700 | [diff] [blame] | 112 | extern int fscrypt_encrypt_block_inplace(const struct inode *inode, |
| 113 | struct page *page, unsigned int len, |
| 114 | unsigned int offs, u64 lblk_num, |
| 115 | gfp_t gfp_flags); |
Eric Biggers | aa8bc1a | 2019-05-20 09:29:47 -0700 | [diff] [blame] | 116 | |
| 117 | extern int fscrypt_decrypt_pagecache_blocks(struct page *page, unsigned int len, |
| 118 | unsigned int offs); |
Eric Biggers | 41adbcb | 2019-05-20 09:29:46 -0700 | [diff] [blame] | 119 | extern int fscrypt_decrypt_block_inplace(const struct inode *inode, |
| 120 | struct page *page, unsigned int len, |
| 121 | unsigned int offs, u64 lblk_num); |
Chandan Rajendra | 643fa96 | 2018-12-12 15:20:12 +0530 | [diff] [blame] | 122 | |
Eric Biggers | d2d0727 | 2019-05-20 09:29:39 -0700 | [diff] [blame] | 123 | static inline bool fscrypt_is_bounce_page(struct page *page) |
Chandan Rajendra | 643fa96 | 2018-12-12 15:20:12 +0530 | [diff] [blame] | 124 | { |
Eric Biggers | d2d0727 | 2019-05-20 09:29:39 -0700 | [diff] [blame] | 125 | return page->mapping == NULL; |
Chandan Rajendra | 643fa96 | 2018-12-12 15:20:12 +0530 | [diff] [blame] | 126 | } |
| 127 | |
Eric Biggers | d2d0727 | 2019-05-20 09:29:39 -0700 | [diff] [blame] | 128 | static inline struct page *fscrypt_pagecache_page(struct page *bounce_page) |
| 129 | { |
| 130 | return (struct page *)page_private(bounce_page); |
| 131 | } |
| 132 | |
| 133 | extern void fscrypt_free_bounce_page(struct page *bounce_page); |
Chandan Rajendra | 643fa96 | 2018-12-12 15:20:12 +0530 | [diff] [blame] | 134 | |
| 135 | /* policy.c */ |
| 136 | extern int fscrypt_ioctl_set_policy(struct file *, const void __user *); |
| 137 | extern int fscrypt_ioctl_get_policy(struct file *, void __user *); |
| 138 | extern int fscrypt_has_permitted_context(struct inode *, struct inode *); |
| 139 | extern int fscrypt_inherit_context(struct inode *, struct inode *, |
| 140 | void *, bool); |
Eric Biggers | 22d94f4 | 2019-08-04 19:35:46 -0700 | [diff] [blame^] | 141 | /* keyring.c */ |
| 142 | extern void fscrypt_sb_free(struct super_block *sb); |
| 143 | extern int fscrypt_ioctl_add_key(struct file *filp, void __user *arg); |
| 144 | |
Eric Biggers | feed825 | 2019-08-04 19:35:45 -0700 | [diff] [blame] | 145 | /* keysetup.c */ |
Chandan Rajendra | 643fa96 | 2018-12-12 15:20:12 +0530 | [diff] [blame] | 146 | extern int fscrypt_get_encryption_info(struct inode *); |
| 147 | extern void fscrypt_put_encryption_info(struct inode *); |
Eric Biggers | 2c58d54 | 2019-04-10 13:21:15 -0700 | [diff] [blame] | 148 | extern void fscrypt_free_inode(struct inode *); |
Chandan Rajendra | 643fa96 | 2018-12-12 15:20:12 +0530 | [diff] [blame] | 149 | |
| 150 | /* fname.c */ |
| 151 | extern int fscrypt_setup_filename(struct inode *, const struct qstr *, |
| 152 | int lookup, struct fscrypt_name *); |
| 153 | |
| 154 | static inline void fscrypt_free_filename(struct fscrypt_name *fname) |
| 155 | { |
| 156 | kfree(fname->crypto_buf.name); |
| 157 | } |
| 158 | |
| 159 | extern int fscrypt_fname_alloc_buffer(const struct inode *, u32, |
| 160 | struct fscrypt_str *); |
| 161 | extern void fscrypt_fname_free_buffer(struct fscrypt_str *); |
| 162 | extern int fscrypt_fname_disk_to_usr(struct inode *, u32, u32, |
| 163 | const struct fscrypt_str *, struct fscrypt_str *); |
| 164 | |
| 165 | #define FSCRYPT_FNAME_MAX_UNDIGESTED_SIZE 32 |
| 166 | |
| 167 | /* Extracts the second-to-last ciphertext block; see explanation below */ |
| 168 | #define FSCRYPT_FNAME_DIGEST(name, len) \ |
| 169 | ((name) + round_down((len) - FS_CRYPTO_BLOCK_SIZE - 1, \ |
| 170 | FS_CRYPTO_BLOCK_SIZE)) |
| 171 | |
| 172 | #define FSCRYPT_FNAME_DIGEST_SIZE FS_CRYPTO_BLOCK_SIZE |
| 173 | |
| 174 | /** |
| 175 | * fscrypt_digested_name - alternate identifier for an on-disk filename |
| 176 | * |
| 177 | * When userspace lists an encrypted directory without access to the key, |
| 178 | * filenames whose ciphertext is longer than FSCRYPT_FNAME_MAX_UNDIGESTED_SIZE |
| 179 | * bytes are shown in this abbreviated form (base64-encoded) rather than as the |
| 180 | * full ciphertext (base64-encoded). This is necessary to allow supporting |
| 181 | * filenames up to NAME_MAX bytes, since base64 encoding expands the length. |
| 182 | * |
| 183 | * To make it possible for filesystems to still find the correct directory entry |
| 184 | * despite not knowing the full on-disk name, we encode any filesystem-specific |
| 185 | * 'hash' and/or 'minor_hash' which the filesystem may need for its lookups, |
| 186 | * followed by the second-to-last ciphertext block of the filename. Due to the |
| 187 | * use of the CBC-CTS encryption mode, the second-to-last ciphertext block |
| 188 | * depends on the full plaintext. (Note that ciphertext stealing causes the |
| 189 | * last two blocks to appear "flipped".) This makes accidental collisions very |
| 190 | * unlikely: just a 1 in 2^128 chance for two filenames to collide even if they |
| 191 | * share the same filesystem-specific hashes. |
| 192 | * |
| 193 | * However, this scheme isn't immune to intentional collisions, which can be |
| 194 | * created by anyone able to create arbitrary plaintext filenames and view them |
| 195 | * without the key. Making the "digest" be a real cryptographic hash like |
| 196 | * SHA-256 over the full ciphertext would prevent this, although it would be |
| 197 | * less efficient and harder to implement, especially since the filesystem would |
| 198 | * need to calculate it for each directory entry examined during a search. |
| 199 | */ |
| 200 | struct fscrypt_digested_name { |
| 201 | u32 hash; |
| 202 | u32 minor_hash; |
| 203 | u8 digest[FSCRYPT_FNAME_DIGEST_SIZE]; |
| 204 | }; |
| 205 | |
| 206 | /** |
| 207 | * fscrypt_match_name() - test whether the given name matches a directory entry |
| 208 | * @fname: the name being searched for |
| 209 | * @de_name: the name from the directory entry |
| 210 | * @de_name_len: the length of @de_name in bytes |
| 211 | * |
| 212 | * Normally @fname->disk_name will be set, and in that case we simply compare |
| 213 | * that to the name stored in the directory entry. The only exception is that |
| 214 | * if we don't have the key for an encrypted directory and a filename in it is |
| 215 | * very long, then we won't have the full disk_name and we'll instead need to |
| 216 | * match against the fscrypt_digested_name. |
| 217 | * |
| 218 | * Return: %true if the name matches, otherwise %false. |
| 219 | */ |
| 220 | static inline bool fscrypt_match_name(const struct fscrypt_name *fname, |
| 221 | const u8 *de_name, u32 de_name_len) |
| 222 | { |
| 223 | if (unlikely(!fname->disk_name.name)) { |
| 224 | const struct fscrypt_digested_name *n = |
| 225 | (const void *)fname->crypto_buf.name; |
| 226 | if (WARN_ON_ONCE(fname->usr_fname->name[0] != '_')) |
| 227 | return false; |
| 228 | if (de_name_len <= FSCRYPT_FNAME_MAX_UNDIGESTED_SIZE) |
| 229 | return false; |
| 230 | return !memcmp(FSCRYPT_FNAME_DIGEST(de_name, de_name_len), |
| 231 | n->digest, FSCRYPT_FNAME_DIGEST_SIZE); |
| 232 | } |
| 233 | |
| 234 | if (de_name_len != fname->disk_name.len) |
| 235 | return false; |
| 236 | return !memcmp(de_name, fname->disk_name.name, fname->disk_name.len); |
| 237 | } |
| 238 | |
| 239 | /* bio.c */ |
| 240 | extern void fscrypt_decrypt_bio(struct bio *); |
| 241 | extern void fscrypt_enqueue_decrypt_bio(struct fscrypt_ctx *ctx, |
| 242 | struct bio *bio); |
Chandan Rajendra | 643fa96 | 2018-12-12 15:20:12 +0530 | [diff] [blame] | 243 | extern int fscrypt_zeroout_range(const struct inode *, pgoff_t, sector_t, |
| 244 | unsigned int); |
| 245 | |
| 246 | /* hooks.c */ |
| 247 | extern int fscrypt_file_open(struct inode *inode, struct file *filp); |
Eric Biggers | 968dd6d | 2019-03-20 11:39:10 -0700 | [diff] [blame] | 248 | extern int __fscrypt_prepare_link(struct inode *inode, struct inode *dir, |
| 249 | struct dentry *dentry); |
Chandan Rajendra | 643fa96 | 2018-12-12 15:20:12 +0530 | [diff] [blame] | 250 | extern int __fscrypt_prepare_rename(struct inode *old_dir, |
| 251 | struct dentry *old_dentry, |
| 252 | struct inode *new_dir, |
| 253 | struct dentry *new_dentry, |
| 254 | unsigned int flags); |
Eric Biggers | b01531d | 2019-03-20 11:39:13 -0700 | [diff] [blame] | 255 | extern int __fscrypt_prepare_lookup(struct inode *dir, struct dentry *dentry, |
| 256 | struct fscrypt_name *fname); |
Chandan Rajendra | 643fa96 | 2018-12-12 15:20:12 +0530 | [diff] [blame] | 257 | extern int __fscrypt_prepare_symlink(struct inode *dir, unsigned int len, |
| 258 | unsigned int max_len, |
| 259 | struct fscrypt_str *disk_link); |
| 260 | extern int __fscrypt_encrypt_symlink(struct inode *inode, const char *target, |
| 261 | unsigned int len, |
| 262 | struct fscrypt_str *disk_link); |
| 263 | extern const char *fscrypt_get_symlink(struct inode *inode, const void *caddr, |
| 264 | unsigned int max_size, |
| 265 | struct delayed_call *done); |
Sascha Hauer | eea2c05 | 2019-03-26 08:52:31 +0100 | [diff] [blame] | 266 | static inline void fscrypt_set_ops(struct super_block *sb, |
| 267 | const struct fscrypt_operations *s_cop) |
| 268 | { |
| 269 | sb->s_cop = s_cop; |
| 270 | } |
Chandan Rajendra | 643fa96 | 2018-12-12 15:20:12 +0530 | [diff] [blame] | 271 | #else /* !CONFIG_FS_ENCRYPTION */ |
| 272 | |
| 273 | static inline bool fscrypt_has_encryption_key(const struct inode *inode) |
| 274 | { |
| 275 | return false; |
| 276 | } |
| 277 | |
| 278 | static inline bool fscrypt_dummy_context_enabled(struct inode *inode) |
| 279 | { |
| 280 | return false; |
| 281 | } |
| 282 | |
Eric Biggers | 0bf3d5c1 | 2019-03-20 11:39:11 -0700 | [diff] [blame] | 283 | static inline void fscrypt_handle_d_move(struct dentry *dentry) |
| 284 | { |
| 285 | } |
| 286 | |
Chandan Rajendra | 643fa96 | 2018-12-12 15:20:12 +0530 | [diff] [blame] | 287 | /* crypto.c */ |
| 288 | static inline void fscrypt_enqueue_decrypt_work(struct work_struct *work) |
| 289 | { |
| 290 | } |
| 291 | |
Eric Biggers | cd0265f | 2019-03-18 10:23:33 -0700 | [diff] [blame] | 292 | static inline struct fscrypt_ctx *fscrypt_get_ctx(gfp_t gfp_flags) |
Chandan Rajendra | 643fa96 | 2018-12-12 15:20:12 +0530 | [diff] [blame] | 293 | { |
| 294 | return ERR_PTR(-EOPNOTSUPP); |
| 295 | } |
| 296 | |
| 297 | static inline void fscrypt_release_ctx(struct fscrypt_ctx *ctx) |
| 298 | { |
| 299 | return; |
| 300 | } |
| 301 | |
Eric Biggers | 53bc1d85 | 2019-05-20 09:29:44 -0700 | [diff] [blame] | 302 | static inline struct page *fscrypt_encrypt_pagecache_blocks(struct page *page, |
| 303 | unsigned int len, |
| 304 | unsigned int offs, |
| 305 | gfp_t gfp_flags) |
Chandan Rajendra | 643fa96 | 2018-12-12 15:20:12 +0530 | [diff] [blame] | 306 | { |
| 307 | return ERR_PTR(-EOPNOTSUPP); |
| 308 | } |
| 309 | |
Eric Biggers | 03569f2 | 2019-05-20 09:29:43 -0700 | [diff] [blame] | 310 | static inline int fscrypt_encrypt_block_inplace(const struct inode *inode, |
| 311 | struct page *page, |
| 312 | unsigned int len, |
| 313 | unsigned int offs, u64 lblk_num, |
| 314 | gfp_t gfp_flags) |
| 315 | { |
| 316 | return -EOPNOTSUPP; |
| 317 | } |
| 318 | |
Eric Biggers | aa8bc1a | 2019-05-20 09:29:47 -0700 | [diff] [blame] | 319 | static inline int fscrypt_decrypt_pagecache_blocks(struct page *page, |
| 320 | unsigned int len, |
| 321 | unsigned int offs) |
Chandan Rajendra | 643fa96 | 2018-12-12 15:20:12 +0530 | [diff] [blame] | 322 | { |
| 323 | return -EOPNOTSUPP; |
| 324 | } |
| 325 | |
Eric Biggers | 41adbcb | 2019-05-20 09:29:46 -0700 | [diff] [blame] | 326 | static inline int fscrypt_decrypt_block_inplace(const struct inode *inode, |
| 327 | struct page *page, |
| 328 | unsigned int len, |
| 329 | unsigned int offs, u64 lblk_num) |
| 330 | { |
| 331 | return -EOPNOTSUPP; |
| 332 | } |
| 333 | |
Eric Biggers | d2d0727 | 2019-05-20 09:29:39 -0700 | [diff] [blame] | 334 | static inline bool fscrypt_is_bounce_page(struct page *page) |
| 335 | { |
| 336 | return false; |
| 337 | } |
| 338 | |
| 339 | static inline struct page *fscrypt_pagecache_page(struct page *bounce_page) |
Chandan Rajendra | 643fa96 | 2018-12-12 15:20:12 +0530 | [diff] [blame] | 340 | { |
| 341 | WARN_ON_ONCE(1); |
| 342 | return ERR_PTR(-EINVAL); |
| 343 | } |
| 344 | |
Eric Biggers | d2d0727 | 2019-05-20 09:29:39 -0700 | [diff] [blame] | 345 | static inline void fscrypt_free_bounce_page(struct page *bounce_page) |
Chandan Rajendra | 643fa96 | 2018-12-12 15:20:12 +0530 | [diff] [blame] | 346 | { |
Chandan Rajendra | 643fa96 | 2018-12-12 15:20:12 +0530 | [diff] [blame] | 347 | } |
| 348 | |
| 349 | /* policy.c */ |
| 350 | static inline int fscrypt_ioctl_set_policy(struct file *filp, |
| 351 | const void __user *arg) |
| 352 | { |
| 353 | return -EOPNOTSUPP; |
| 354 | } |
| 355 | |
| 356 | static inline int fscrypt_ioctl_get_policy(struct file *filp, void __user *arg) |
| 357 | { |
| 358 | return -EOPNOTSUPP; |
| 359 | } |
| 360 | |
| 361 | static inline int fscrypt_has_permitted_context(struct inode *parent, |
| 362 | struct inode *child) |
| 363 | { |
| 364 | return 0; |
| 365 | } |
| 366 | |
| 367 | static inline int fscrypt_inherit_context(struct inode *parent, |
| 368 | struct inode *child, |
| 369 | void *fs_data, bool preload) |
| 370 | { |
| 371 | return -EOPNOTSUPP; |
| 372 | } |
| 373 | |
Eric Biggers | 22d94f4 | 2019-08-04 19:35:46 -0700 | [diff] [blame^] | 374 | /* keyring.c */ |
| 375 | static inline void fscrypt_sb_free(struct super_block *sb) |
| 376 | { |
| 377 | } |
| 378 | |
| 379 | static inline int fscrypt_ioctl_add_key(struct file *filp, void __user *arg) |
| 380 | { |
| 381 | return -EOPNOTSUPP; |
| 382 | } |
| 383 | |
Eric Biggers | feed825 | 2019-08-04 19:35:45 -0700 | [diff] [blame] | 384 | /* keysetup.c */ |
Chandan Rajendra | 643fa96 | 2018-12-12 15:20:12 +0530 | [diff] [blame] | 385 | static inline int fscrypt_get_encryption_info(struct inode *inode) |
| 386 | { |
| 387 | return -EOPNOTSUPP; |
| 388 | } |
| 389 | |
| 390 | static inline void fscrypt_put_encryption_info(struct inode *inode) |
| 391 | { |
| 392 | return; |
| 393 | } |
| 394 | |
Eric Biggers | 2c58d54 | 2019-04-10 13:21:15 -0700 | [diff] [blame] | 395 | static inline void fscrypt_free_inode(struct inode *inode) |
| 396 | { |
| 397 | } |
| 398 | |
Chandan Rajendra | 643fa96 | 2018-12-12 15:20:12 +0530 | [diff] [blame] | 399 | /* fname.c */ |
| 400 | static inline int fscrypt_setup_filename(struct inode *dir, |
| 401 | const struct qstr *iname, |
| 402 | int lookup, struct fscrypt_name *fname) |
| 403 | { |
| 404 | if (IS_ENCRYPTED(dir)) |
| 405 | return -EOPNOTSUPP; |
| 406 | |
Eric Biggers | b01531d | 2019-03-20 11:39:13 -0700 | [diff] [blame] | 407 | memset(fname, 0, sizeof(*fname)); |
Chandan Rajendra | 643fa96 | 2018-12-12 15:20:12 +0530 | [diff] [blame] | 408 | fname->usr_fname = iname; |
| 409 | fname->disk_name.name = (unsigned char *)iname->name; |
| 410 | fname->disk_name.len = iname->len; |
| 411 | return 0; |
| 412 | } |
| 413 | |
| 414 | static inline void fscrypt_free_filename(struct fscrypt_name *fname) |
| 415 | { |
| 416 | return; |
| 417 | } |
| 418 | |
| 419 | static inline int fscrypt_fname_alloc_buffer(const struct inode *inode, |
| 420 | u32 max_encrypted_len, |
| 421 | struct fscrypt_str *crypto_str) |
| 422 | { |
| 423 | return -EOPNOTSUPP; |
| 424 | } |
| 425 | |
| 426 | static inline void fscrypt_fname_free_buffer(struct fscrypt_str *crypto_str) |
| 427 | { |
| 428 | return; |
| 429 | } |
| 430 | |
| 431 | static inline int fscrypt_fname_disk_to_usr(struct inode *inode, |
| 432 | u32 hash, u32 minor_hash, |
| 433 | const struct fscrypt_str *iname, |
| 434 | struct fscrypt_str *oname) |
| 435 | { |
| 436 | return -EOPNOTSUPP; |
| 437 | } |
| 438 | |
| 439 | static inline bool fscrypt_match_name(const struct fscrypt_name *fname, |
| 440 | const u8 *de_name, u32 de_name_len) |
| 441 | { |
| 442 | /* Encryption support disabled; use standard comparison */ |
| 443 | if (de_name_len != fname->disk_name.len) |
| 444 | return false; |
| 445 | return !memcmp(de_name, fname->disk_name.name, fname->disk_name.len); |
| 446 | } |
| 447 | |
| 448 | /* bio.c */ |
| 449 | static inline void fscrypt_decrypt_bio(struct bio *bio) |
| 450 | { |
| 451 | } |
| 452 | |
| 453 | static inline void fscrypt_enqueue_decrypt_bio(struct fscrypt_ctx *ctx, |
| 454 | struct bio *bio) |
| 455 | { |
| 456 | } |
| 457 | |
Chandan Rajendra | 643fa96 | 2018-12-12 15:20:12 +0530 | [diff] [blame] | 458 | static inline int fscrypt_zeroout_range(const struct inode *inode, pgoff_t lblk, |
| 459 | sector_t pblk, unsigned int len) |
| 460 | { |
| 461 | return -EOPNOTSUPP; |
| 462 | } |
| 463 | |
| 464 | /* hooks.c */ |
| 465 | |
| 466 | static inline int fscrypt_file_open(struct inode *inode, struct file *filp) |
| 467 | { |
| 468 | if (IS_ENCRYPTED(inode)) |
| 469 | return -EOPNOTSUPP; |
| 470 | return 0; |
| 471 | } |
| 472 | |
Eric Biggers | 968dd6d | 2019-03-20 11:39:10 -0700 | [diff] [blame] | 473 | static inline int __fscrypt_prepare_link(struct inode *inode, struct inode *dir, |
| 474 | struct dentry *dentry) |
Chandan Rajendra | 643fa96 | 2018-12-12 15:20:12 +0530 | [diff] [blame] | 475 | { |
| 476 | return -EOPNOTSUPP; |
| 477 | } |
| 478 | |
| 479 | static inline int __fscrypt_prepare_rename(struct inode *old_dir, |
| 480 | struct dentry *old_dentry, |
| 481 | struct inode *new_dir, |
| 482 | struct dentry *new_dentry, |
| 483 | unsigned int flags) |
| 484 | { |
| 485 | return -EOPNOTSUPP; |
| 486 | } |
| 487 | |
| 488 | static inline int __fscrypt_prepare_lookup(struct inode *dir, |
Eric Biggers | b01531d | 2019-03-20 11:39:13 -0700 | [diff] [blame] | 489 | struct dentry *dentry, |
| 490 | struct fscrypt_name *fname) |
Chandan Rajendra | 643fa96 | 2018-12-12 15:20:12 +0530 | [diff] [blame] | 491 | { |
| 492 | return -EOPNOTSUPP; |
| 493 | } |
| 494 | |
| 495 | static inline int __fscrypt_prepare_symlink(struct inode *dir, |
| 496 | unsigned int len, |
| 497 | unsigned int max_len, |
| 498 | struct fscrypt_str *disk_link) |
| 499 | { |
| 500 | return -EOPNOTSUPP; |
| 501 | } |
| 502 | |
| 503 | |
| 504 | static inline int __fscrypt_encrypt_symlink(struct inode *inode, |
| 505 | const char *target, |
| 506 | unsigned int len, |
| 507 | struct fscrypt_str *disk_link) |
| 508 | { |
| 509 | return -EOPNOTSUPP; |
| 510 | } |
| 511 | |
| 512 | static inline const char *fscrypt_get_symlink(struct inode *inode, |
| 513 | const void *caddr, |
| 514 | unsigned int max_size, |
| 515 | struct delayed_call *done) |
| 516 | { |
| 517 | return ERR_PTR(-EOPNOTSUPP); |
| 518 | } |
Sascha Hauer | eea2c05 | 2019-03-26 08:52:31 +0100 | [diff] [blame] | 519 | |
| 520 | static inline void fscrypt_set_ops(struct super_block *sb, |
| 521 | const struct fscrypt_operations *s_cop) |
| 522 | { |
| 523 | } |
| 524 | |
Chandan Rajendra | 643fa96 | 2018-12-12 15:20:12 +0530 | [diff] [blame] | 525 | #endif /* !CONFIG_FS_ENCRYPTION */ |
Dave Chinner | 734f0d2 | 2017-10-09 12:15:34 -0700 | [diff] [blame] | 526 | |
Eric Biggers | d293c3e | 2017-10-09 12:15:39 -0700 | [diff] [blame] | 527 | /** |
| 528 | * fscrypt_require_key - require an inode's encryption key |
| 529 | * @inode: the inode we need the key for |
| 530 | * |
| 531 | * If the inode is encrypted, set up its encryption key if not already done. |
| 532 | * Then require that the key be present and return -ENOKEY otherwise. |
| 533 | * |
| 534 | * No locks are needed, and the key will live as long as the struct inode --- so |
| 535 | * it won't go away from under you. |
| 536 | * |
| 537 | * Return: 0 on success, -ENOKEY if the key is missing, or another -errno code |
| 538 | * if a problem occurred while setting up the encryption key. |
| 539 | */ |
| 540 | static inline int fscrypt_require_key(struct inode *inode) |
| 541 | { |
| 542 | if (IS_ENCRYPTED(inode)) { |
| 543 | int err = fscrypt_get_encryption_info(inode); |
| 544 | |
| 545 | if (err) |
| 546 | return err; |
| 547 | if (!fscrypt_has_encryption_key(inode)) |
| 548 | return -ENOKEY; |
| 549 | } |
| 550 | return 0; |
| 551 | } |
Dave Chinner | 734f0d2 | 2017-10-09 12:15:34 -0700 | [diff] [blame] | 552 | |
Eric Biggers | 0ea87a9 | 2017-10-09 12:15:41 -0700 | [diff] [blame] | 553 | /** |
| 554 | * fscrypt_prepare_link - prepare to link an inode into a possibly-encrypted directory |
| 555 | * @old_dentry: an existing dentry for the inode being linked |
| 556 | * @dir: the target directory |
| 557 | * @dentry: negative dentry for the target filename |
| 558 | * |
| 559 | * A new link can only be added to an encrypted directory if the directory's |
| 560 | * encryption key is available --- since otherwise we'd have no way to encrypt |
| 561 | * the filename. Therefore, we first set up the directory's encryption key (if |
| 562 | * not already done) and return an error if it's unavailable. |
| 563 | * |
| 564 | * We also verify that the link will not violate the constraint that all files |
| 565 | * in an encrypted directory tree use the same encryption policy. |
| 566 | * |
| 567 | * 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] | 568 | * -EXDEV if the link would result in an inconsistent encryption policy, or |
Eric Biggers | 0ea87a9 | 2017-10-09 12:15:41 -0700 | [diff] [blame] | 569 | * another -errno code. |
| 570 | */ |
| 571 | static inline int fscrypt_prepare_link(struct dentry *old_dentry, |
| 572 | struct inode *dir, |
| 573 | struct dentry *dentry) |
| 574 | { |
| 575 | if (IS_ENCRYPTED(dir)) |
Eric Biggers | 968dd6d | 2019-03-20 11:39:10 -0700 | [diff] [blame] | 576 | return __fscrypt_prepare_link(d_inode(old_dentry), dir, dentry); |
Eric Biggers | 0ea87a9 | 2017-10-09 12:15:41 -0700 | [diff] [blame] | 577 | return 0; |
| 578 | } |
| 579 | |
Eric Biggers | 94b26f3 | 2017-10-09 12:15:42 -0700 | [diff] [blame] | 580 | /** |
| 581 | * fscrypt_prepare_rename - prepare for a rename between possibly-encrypted directories |
| 582 | * @old_dir: source directory |
| 583 | * @old_dentry: dentry for source file |
| 584 | * @new_dir: target directory |
| 585 | * @new_dentry: dentry for target location (may be negative unless exchanging) |
| 586 | * @flags: rename flags (we care at least about %RENAME_EXCHANGE) |
| 587 | * |
| 588 | * Prepare for ->rename() where the source and/or target directories may be |
| 589 | * encrypted. A new link can only be added to an encrypted directory if the |
| 590 | * directory's encryption key is available --- since otherwise we'd have no way |
| 591 | * to encrypt the filename. A rename to an existing name, on the other hand, |
| 592 | * *is* cryptographically possible without the key. However, we take the more |
| 593 | * conservative approach and just forbid all no-key renames. |
| 594 | * |
| 595 | * We also verify that the rename will not violate the constraint that all files |
| 596 | * in an encrypted directory tree use the same encryption policy. |
| 597 | * |
Eric Biggers | f5e55e7 | 2019-01-22 16:20:21 -0800 | [diff] [blame] | 598 | * 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] | 599 | * rename would cause inconsistent encryption policies, or another -errno code. |
| 600 | */ |
| 601 | static inline int fscrypt_prepare_rename(struct inode *old_dir, |
| 602 | struct dentry *old_dentry, |
| 603 | struct inode *new_dir, |
| 604 | struct dentry *new_dentry, |
| 605 | unsigned int flags) |
| 606 | { |
| 607 | if (IS_ENCRYPTED(old_dir) || IS_ENCRYPTED(new_dir)) |
| 608 | return __fscrypt_prepare_rename(old_dir, old_dentry, |
| 609 | new_dir, new_dentry, flags); |
| 610 | return 0; |
| 611 | } |
| 612 | |
Eric Biggers | 32c3cf0 | 2017-10-09 12:15:43 -0700 | [diff] [blame] | 613 | /** |
| 614 | * fscrypt_prepare_lookup - prepare to lookup a name in a possibly-encrypted directory |
| 615 | * @dir: directory being searched |
| 616 | * @dentry: filename being looked up |
Eric Biggers | b01531d | 2019-03-20 11:39:13 -0700 | [diff] [blame] | 617 | * @fname: (output) the name to use to search the on-disk directory |
Eric Biggers | 32c3cf0 | 2017-10-09 12:15:43 -0700 | [diff] [blame] | 618 | * |
Eric Biggers | b01531d | 2019-03-20 11:39:13 -0700 | [diff] [blame] | 619 | * Prepare for ->lookup() in a directory which may be encrypted by determining |
| 620 | * the name that will actually be used to search the directory on-disk. Lookups |
| 621 | * can be done with or without the directory's encryption key; without the key, |
Eric Biggers | 32c3cf0 | 2017-10-09 12:15:43 -0700 | [diff] [blame] | 622 | * filenames are presented in encrypted form. Therefore, we'll try to set up |
| 623 | * the directory's encryption key, but even without it the lookup can continue. |
| 624 | * |
Eric Biggers | 6cc2486 | 2019-03-20 11:39:09 -0700 | [diff] [blame] | 625 | * This also installs a custom ->d_revalidate() method which will invalidate the |
| 626 | * 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] | 627 | * |
Eric Biggers | b01531d | 2019-03-20 11:39:13 -0700 | [diff] [blame] | 628 | * Return: 0 on success; -ENOENT if key is unavailable but the filename isn't a |
| 629 | * correctly formed encoded ciphertext name, so a negative dentry should be |
| 630 | * created; or another -errno code. |
Eric Biggers | 32c3cf0 | 2017-10-09 12:15:43 -0700 | [diff] [blame] | 631 | */ |
| 632 | static inline int fscrypt_prepare_lookup(struct inode *dir, |
| 633 | struct dentry *dentry, |
Eric Biggers | b01531d | 2019-03-20 11:39:13 -0700 | [diff] [blame] | 634 | struct fscrypt_name *fname) |
Eric Biggers | 32c3cf0 | 2017-10-09 12:15:43 -0700 | [diff] [blame] | 635 | { |
| 636 | if (IS_ENCRYPTED(dir)) |
Eric Biggers | b01531d | 2019-03-20 11:39:13 -0700 | [diff] [blame] | 637 | return __fscrypt_prepare_lookup(dir, dentry, fname); |
| 638 | |
| 639 | memset(fname, 0, sizeof(*fname)); |
| 640 | fname->usr_fname = &dentry->d_name; |
| 641 | fname->disk_name.name = (unsigned char *)dentry->d_name.name; |
| 642 | fname->disk_name.len = dentry->d_name.len; |
Eric Biggers | 32c3cf0 | 2017-10-09 12:15:43 -0700 | [diff] [blame] | 643 | return 0; |
| 644 | } |
| 645 | |
Eric Biggers | 815dac3 | 2017-10-09 12:15:44 -0700 | [diff] [blame] | 646 | /** |
| 647 | * fscrypt_prepare_setattr - prepare to change a possibly-encrypted inode's attributes |
| 648 | * @dentry: dentry through which the inode is being changed |
| 649 | * @attr: attributes to change |
| 650 | * |
| 651 | * Prepare for ->setattr() on a possibly-encrypted inode. On an encrypted file, |
| 652 | * most attribute changes are allowed even without the encryption key. However, |
| 653 | * without the encryption key we do have to forbid truncates. This is needed |
| 654 | * because the size being truncated to may not be a multiple of the filesystem |
| 655 | * block size, and in that case we'd have to decrypt the final block, zero the |
| 656 | * portion past i_size, and re-encrypt it. (We *could* allow truncating to a |
| 657 | * filesystem block boundary, but it's simpler to just forbid all truncates --- |
| 658 | * and we already forbid all other contents modifications without the key.) |
| 659 | * |
| 660 | * Return: 0 on success, -ENOKEY if the key is missing, or another -errno code |
| 661 | * if a problem occurred while setting up the encryption key. |
| 662 | */ |
| 663 | static inline int fscrypt_prepare_setattr(struct dentry *dentry, |
| 664 | struct iattr *attr) |
| 665 | { |
| 666 | if (attr->ia_valid & ATTR_SIZE) |
| 667 | return fscrypt_require_key(d_inode(dentry)); |
| 668 | return 0; |
| 669 | } |
| 670 | |
Eric Biggers | 76e81d6 | 2018-01-05 10:45:01 -0800 | [diff] [blame] | 671 | /** |
| 672 | * fscrypt_prepare_symlink - prepare to create a possibly-encrypted symlink |
| 673 | * @dir: directory in which the symlink is being created |
| 674 | * @target: plaintext symlink target |
| 675 | * @len: length of @target excluding null terminator |
| 676 | * @max_len: space the filesystem has available to store the symlink target |
| 677 | * @disk_link: (out) the on-disk symlink target being prepared |
| 678 | * |
| 679 | * This function computes the size the symlink target will require on-disk, |
| 680 | * stores it in @disk_link->len, and validates it against @max_len. An |
| 681 | * encrypted symlink may be longer than the original. |
| 682 | * |
| 683 | * Additionally, @disk_link->name is set to @target if the symlink will be |
| 684 | * unencrypted, but left NULL if the symlink will be encrypted. For encrypted |
| 685 | * symlinks, the filesystem must call fscrypt_encrypt_symlink() to create the |
| 686 | * on-disk target later. (The reason for the two-step process is that some |
| 687 | * filesystems need to know the size of the symlink target before creating the |
| 688 | * inode, e.g. to determine whether it will be a "fast" or "slow" symlink.) |
| 689 | * |
| 690 | * Return: 0 on success, -ENAMETOOLONG if the symlink target is too long, |
| 691 | * -ENOKEY if the encryption key is missing, or another -errno code if a problem |
| 692 | * occurred while setting up the encryption key. |
| 693 | */ |
| 694 | static inline int fscrypt_prepare_symlink(struct inode *dir, |
| 695 | const char *target, |
| 696 | unsigned int len, |
| 697 | unsigned int max_len, |
| 698 | struct fscrypt_str *disk_link) |
| 699 | { |
| 700 | if (IS_ENCRYPTED(dir) || fscrypt_dummy_context_enabled(dir)) |
| 701 | return __fscrypt_prepare_symlink(dir, len, max_len, disk_link); |
| 702 | |
| 703 | disk_link->name = (unsigned char *)target; |
| 704 | disk_link->len = len + 1; |
| 705 | if (disk_link->len > max_len) |
| 706 | return -ENAMETOOLONG; |
| 707 | return 0; |
| 708 | } |
| 709 | |
| 710 | /** |
| 711 | * fscrypt_encrypt_symlink - encrypt the symlink target if needed |
| 712 | * @inode: symlink inode |
| 713 | * @target: plaintext symlink target |
| 714 | * @len: length of @target excluding null terminator |
| 715 | * @disk_link: (in/out) the on-disk symlink target being prepared |
| 716 | * |
| 717 | * If the symlink target needs to be encrypted, then this function encrypts it |
| 718 | * into @disk_link->name. fscrypt_prepare_symlink() must have been called |
| 719 | * previously to compute @disk_link->len. If the filesystem did not allocate a |
| 720 | * buffer for @disk_link->name after calling fscrypt_prepare_link(), then one |
| 721 | * will be kmalloc()'ed and the filesystem will be responsible for freeing it. |
| 722 | * |
| 723 | * Return: 0 on success, -errno on failure |
| 724 | */ |
| 725 | static inline int fscrypt_encrypt_symlink(struct inode *inode, |
| 726 | const char *target, |
| 727 | unsigned int len, |
| 728 | struct fscrypt_str *disk_link) |
| 729 | { |
| 730 | if (IS_ENCRYPTED(inode)) |
| 731 | return __fscrypt_encrypt_symlink(inode, target, len, disk_link); |
| 732 | return 0; |
| 733 | } |
| 734 | |
Eric Biggers | d2d0727 | 2019-05-20 09:29:39 -0700 | [diff] [blame] | 735 | /* If *pagep is a bounce page, free it and set *pagep to the pagecache page */ |
| 736 | static inline void fscrypt_finalize_bounce_page(struct page **pagep) |
| 737 | { |
| 738 | struct page *page = *pagep; |
| 739 | |
| 740 | if (fscrypt_is_bounce_page(page)) { |
| 741 | *pagep = fscrypt_pagecache_page(page); |
| 742 | fscrypt_free_bounce_page(page); |
| 743 | } |
| 744 | } |
| 745 | |
Dave Chinner | 734f0d2 | 2017-10-09 12:15:34 -0700 | [diff] [blame] | 746 | #endif /* _LINUX_FSCRYPT_H */ |