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