Jaegeuk Kim | e6b120d | 2017-07-10 12:55:09 -0700 | [diff] [blame] | 1 | /* |
| 2 | * fscrypt_supp.h |
| 3 | * |
Dave Chinner | 1617929 | 2017-10-09 12:15:34 -0700 | [diff] [blame] | 4 | * Do not include this file directly. Use fscrypt.h instead! |
Jaegeuk Kim | e6b120d | 2017-07-10 12:55:09 -0700 | [diff] [blame] | 5 | */ |
Dave Chinner | 1617929 | 2017-10-09 12:15:34 -0700 | [diff] [blame] | 6 | #ifndef _LINUX_FSCRYPT_H |
| 7 | #error "Incorrect include of linux/fscrypt_supp.h!" |
| 8 | #endif |
Jaegeuk Kim | e6b120d | 2017-07-10 12:55:09 -0700 | [diff] [blame] | 9 | |
| 10 | #ifndef _LINUX_FSCRYPT_SUPP_H |
| 11 | #define _LINUX_FSCRYPT_SUPP_H |
| 12 | |
Eric Biggers | eb9c5fd | 2018-01-05 10:45:00 -0800 | [diff] [blame] | 13 | #include <linux/mm.h> |
| 14 | #include <linux/slab.h> |
| 15 | |
Eric Biggers | efbfa8c | 2018-01-05 10:44:57 -0800 | [diff] [blame] | 16 | /* |
| 17 | * fscrypt superblock flags |
| 18 | */ |
| 19 | #define FS_CFLG_OWN_PAGES (1U << 1) |
| 20 | |
| 21 | /* |
| 22 | * crypto operations for filesystems |
| 23 | */ |
| 24 | struct fscrypt_operations { |
| 25 | unsigned int flags; |
| 26 | const char *key_prefix; |
| 27 | int (*get_context)(struct inode *, void *, size_t); |
| 28 | int (*set_context)(struct inode *, const void *, size_t, void *); |
| 29 | bool (*dummy_context)(struct inode *); |
| 30 | bool (*empty_dir)(struct inode *); |
Eric Biggers | 4ddc3a8 | 2018-04-30 15:51:44 -0700 | [diff] [blame] | 31 | unsigned int max_namelen; |
Eric Biggers | efbfa8c | 2018-01-05 10:44:57 -0800 | [diff] [blame] | 32 | }; |
| 33 | |
Eric Biggers | f0c472b | 2018-01-05 10:44:55 -0800 | [diff] [blame] | 34 | struct fscrypt_ctx { |
| 35 | union { |
| 36 | struct { |
| 37 | struct page *bounce_page; /* Ciphertext page */ |
| 38 | struct page *control_page; /* Original page */ |
| 39 | } w; |
| 40 | struct { |
| 41 | struct bio *bio; |
| 42 | struct work_struct work; |
| 43 | } r; |
| 44 | struct list_head free_list; /* Free list */ |
| 45 | }; |
| 46 | u8 flags; /* Flags */ |
| 47 | }; |
| 48 | |
Eric Biggers | d8dfb89 | 2018-01-05 10:44:52 -0800 | [diff] [blame] | 49 | static inline bool fscrypt_has_encryption_key(const struct inode *inode) |
| 50 | { |
| 51 | return (inode->i_crypt_info != NULL); |
| 52 | } |
| 53 | |
Eric Biggers | 616dbd2 | 2018-01-05 10:44:56 -0800 | [diff] [blame] | 54 | static inline bool fscrypt_dummy_context_enabled(struct inode *inode) |
| 55 | { |
| 56 | return inode->i_sb->s_cop->dummy_context && |
| 57 | inode->i_sb->s_cop->dummy_context(inode); |
| 58 | } |
| 59 | |
Jaegeuk Kim | e6b120d | 2017-07-10 12:55:09 -0700 | [diff] [blame] | 60 | /* crypto.c */ |
Eric Biggers | 0509923 | 2018-04-18 11:09:47 -0700 | [diff] [blame] | 61 | extern void fscrypt_enqueue_decrypt_work(struct work_struct *); |
Jaegeuk Kim | e6b120d | 2017-07-10 12:55:09 -0700 | [diff] [blame] | 62 | extern struct fscrypt_ctx *fscrypt_get_ctx(const struct inode *, gfp_t); |
| 63 | extern void fscrypt_release_ctx(struct fscrypt_ctx *); |
| 64 | extern struct page *fscrypt_encrypt_page(const struct inode *, struct page *, |
| 65 | unsigned int, unsigned int, |
| 66 | u64, gfp_t); |
| 67 | extern int fscrypt_decrypt_page(const struct inode *, struct page *, unsigned int, |
| 68 | unsigned int, u64); |
Eric Biggers | b67b07e | 2018-01-05 10:44:53 -0800 | [diff] [blame] | 69 | |
| 70 | static inline struct page *fscrypt_control_page(struct page *page) |
| 71 | { |
| 72 | return ((struct fscrypt_ctx *)page_private(page))->w.control_page; |
| 73 | } |
| 74 | |
Jaegeuk Kim | e6b120d | 2017-07-10 12:55:09 -0700 | [diff] [blame] | 75 | extern void fscrypt_restore_control_page(struct page *); |
| 76 | |
Jaegeuk Kim | e6b120d | 2017-07-10 12:55:09 -0700 | [diff] [blame] | 77 | /* policy.c */ |
| 78 | extern int fscrypt_ioctl_set_policy(struct file *, const void __user *); |
| 79 | extern int fscrypt_ioctl_get_policy(struct file *, void __user *); |
| 80 | extern int fscrypt_has_permitted_context(struct inode *, struct inode *); |
| 81 | extern int fscrypt_inherit_context(struct inode *, struct inode *, |
| 82 | void *, bool); |
| 83 | /* keyinfo.c */ |
| 84 | extern int fscrypt_get_encryption_info(struct inode *); |
Eric Biggers | 401052f | 2018-01-11 23:30:13 -0500 | [diff] [blame] | 85 | extern void fscrypt_put_encryption_info(struct inode *); |
Jaegeuk Kim | e6b120d | 2017-07-10 12:55:09 -0700 | [diff] [blame] | 86 | |
| 87 | /* fname.c */ |
| 88 | extern int fscrypt_setup_filename(struct inode *, const struct qstr *, |
| 89 | int lookup, struct fscrypt_name *); |
Eric Biggers | 05b79cd | 2017-05-22 18:14:06 -0700 | [diff] [blame] | 90 | |
| 91 | static inline void fscrypt_free_filename(struct fscrypt_name *fname) |
| 92 | { |
| 93 | kfree(fname->crypto_buf.name); |
| 94 | } |
| 95 | |
Jaegeuk Kim | e6b120d | 2017-07-10 12:55:09 -0700 | [diff] [blame] | 96 | extern int fscrypt_fname_alloc_buffer(const struct inode *, u32, |
| 97 | struct fscrypt_str *); |
| 98 | extern void fscrypt_fname_free_buffer(struct fscrypt_str *); |
| 99 | extern int fscrypt_fname_disk_to_usr(struct inode *, u32, u32, |
| 100 | const struct fscrypt_str *, struct fscrypt_str *); |
Jaegeuk Kim | e6b120d | 2017-07-10 12:55:09 -0700 | [diff] [blame] | 101 | |
| 102 | #define FSCRYPT_FNAME_MAX_UNDIGESTED_SIZE 32 |
| 103 | |
| 104 | /* Extracts the second-to-last ciphertext block; see explanation below */ |
| 105 | #define FSCRYPT_FNAME_DIGEST(name, len) \ |
| 106 | ((name) + round_down((len) - FS_CRYPTO_BLOCK_SIZE - 1, \ |
| 107 | FS_CRYPTO_BLOCK_SIZE)) |
| 108 | |
| 109 | #define FSCRYPT_FNAME_DIGEST_SIZE FS_CRYPTO_BLOCK_SIZE |
| 110 | |
| 111 | /** |
| 112 | * fscrypt_digested_name - alternate identifier for an on-disk filename |
| 113 | * |
| 114 | * When userspace lists an encrypted directory without access to the key, |
| 115 | * filenames whose ciphertext is longer than FSCRYPT_FNAME_MAX_UNDIGESTED_SIZE |
| 116 | * bytes are shown in this abbreviated form (base64-encoded) rather than as the |
| 117 | * full ciphertext (base64-encoded). This is necessary to allow supporting |
| 118 | * filenames up to NAME_MAX bytes, since base64 encoding expands the length. |
| 119 | * |
| 120 | * To make it possible for filesystems to still find the correct directory entry |
| 121 | * despite not knowing the full on-disk name, we encode any filesystem-specific |
| 122 | * 'hash' and/or 'minor_hash' which the filesystem may need for its lookups, |
| 123 | * followed by the second-to-last ciphertext block of the filename. Due to the |
| 124 | * use of the CBC-CTS encryption mode, the second-to-last ciphertext block |
| 125 | * depends on the full plaintext. (Note that ciphertext stealing causes the |
| 126 | * last two blocks to appear "flipped".) This makes accidental collisions very |
| 127 | * unlikely: just a 1 in 2^128 chance for two filenames to collide even if they |
| 128 | * share the same filesystem-specific hashes. |
| 129 | * |
| 130 | * However, this scheme isn't immune to intentional collisions, which can be |
| 131 | * created by anyone able to create arbitrary plaintext filenames and view them |
| 132 | * without the key. Making the "digest" be a real cryptographic hash like |
| 133 | * SHA-256 over the full ciphertext would prevent this, although it would be |
| 134 | * less efficient and harder to implement, especially since the filesystem would |
| 135 | * need to calculate it for each directory entry examined during a search. |
| 136 | */ |
| 137 | struct fscrypt_digested_name { |
| 138 | u32 hash; |
| 139 | u32 minor_hash; |
| 140 | u8 digest[FSCRYPT_FNAME_DIGEST_SIZE]; |
| 141 | }; |
| 142 | |
| 143 | /** |
| 144 | * fscrypt_match_name() - test whether the given name matches a directory entry |
| 145 | * @fname: the name being searched for |
| 146 | * @de_name: the name from the directory entry |
| 147 | * @de_name_len: the length of @de_name in bytes |
| 148 | * |
| 149 | * Normally @fname->disk_name will be set, and in that case we simply compare |
| 150 | * that to the name stored in the directory entry. The only exception is that |
| 151 | * if we don't have the key for an encrypted directory and a filename in it is |
| 152 | * very long, then we won't have the full disk_name and we'll instead need to |
| 153 | * match against the fscrypt_digested_name. |
| 154 | * |
| 155 | * Return: %true if the name matches, otherwise %false. |
| 156 | */ |
| 157 | static inline bool fscrypt_match_name(const struct fscrypt_name *fname, |
| 158 | const u8 *de_name, u32 de_name_len) |
| 159 | { |
| 160 | if (unlikely(!fname->disk_name.name)) { |
| 161 | const struct fscrypt_digested_name *n = |
| 162 | (const void *)fname->crypto_buf.name; |
| 163 | if (WARN_ON_ONCE(fname->usr_fname->name[0] != '_')) |
| 164 | return false; |
| 165 | if (de_name_len <= FSCRYPT_FNAME_MAX_UNDIGESTED_SIZE) |
| 166 | return false; |
| 167 | return !memcmp(FSCRYPT_FNAME_DIGEST(de_name, de_name_len), |
| 168 | n->digest, FSCRYPT_FNAME_DIGEST_SIZE); |
| 169 | } |
| 170 | |
| 171 | if (de_name_len != fname->disk_name.len) |
| 172 | return false; |
| 173 | return !memcmp(de_name, fname->disk_name.name, fname->disk_name.len); |
| 174 | } |
| 175 | |
| 176 | /* bio.c */ |
Eric Biggers | 0509923 | 2018-04-18 11:09:47 -0700 | [diff] [blame] | 177 | extern void fscrypt_decrypt_bio(struct bio *); |
| 178 | extern void fscrypt_enqueue_decrypt_bio(struct fscrypt_ctx *ctx, |
| 179 | struct bio *bio); |
Jaegeuk Kim | e6b120d | 2017-07-10 12:55:09 -0700 | [diff] [blame] | 180 | extern void fscrypt_pullback_bio_page(struct page **, bool); |
| 181 | extern int fscrypt_zeroout_range(const struct inode *, pgoff_t, sector_t, |
| 182 | unsigned int); |
| 183 | |
Eric Biggers | d61dffb | 2017-10-09 12:15:40 -0700 | [diff] [blame] | 184 | /* hooks.c */ |
| 185 | extern int fscrypt_file_open(struct inode *inode, struct file *filp); |
Eric Biggers | f521870 | 2017-10-09 12:15:41 -0700 | [diff] [blame] | 186 | extern int __fscrypt_prepare_link(struct inode *inode, struct inode *dir); |
Eric Biggers | 98fe83a | 2017-10-09 12:15:42 -0700 | [diff] [blame] | 187 | extern int __fscrypt_prepare_rename(struct inode *old_dir, |
| 188 | struct dentry *old_dentry, |
| 189 | struct inode *new_dir, |
| 190 | struct dentry *new_dentry, |
| 191 | unsigned int flags); |
Eric Biggers | ec822ff | 2017-10-09 12:15:43 -0700 | [diff] [blame] | 192 | extern int __fscrypt_prepare_lookup(struct inode *dir, struct dentry *dentry); |
Eric Biggers | a7e05c7 | 2018-01-05 10:45:01 -0800 | [diff] [blame] | 193 | extern int __fscrypt_prepare_symlink(struct inode *dir, unsigned int len, |
| 194 | unsigned int max_len, |
| 195 | struct fscrypt_str *disk_link); |
| 196 | extern int __fscrypt_encrypt_symlink(struct inode *inode, const char *target, |
| 197 | unsigned int len, |
| 198 | struct fscrypt_str *disk_link); |
Eric Biggers | da32a16 | 2018-01-05 10:45:02 -0800 | [diff] [blame] | 199 | extern const char *fscrypt_get_symlink(struct inode *inode, const void *caddr, |
| 200 | unsigned int max_size, |
| 201 | struct delayed_call *done); |
Eric Biggers | d61dffb | 2017-10-09 12:15:40 -0700 | [diff] [blame] | 202 | |
Jaegeuk Kim | e6b120d | 2017-07-10 12:55:09 -0700 | [diff] [blame] | 203 | #endif /* _LINUX_FSCRYPT_SUPP_H */ |