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