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 | * |
| 5 | * Filesystems that implement per-file encryption include this header |
| 6 | * file with the __FS_HAS_ENCRYPTION set according to whether that filesystem |
| 7 | * is being built with encryption support or not. |
Eric Biggers | 46f47e4 | 2017-01-24 10:58:06 -0800 | [diff] [blame] | 8 | * |
| 9 | * Copyright (C) 2015, Google, Inc. |
| 10 | * |
| 11 | * Written by Michael Halcrow, 2015. |
| 12 | * Modified by Jaegeuk Kim, 2015. |
| 13 | */ |
Dave Chinner | 734f0d2 | 2017-10-09 12:15:34 -0700 | [diff] [blame] | 14 | #ifndef _LINUX_FSCRYPT_H |
| 15 | #define _LINUX_FSCRYPT_H |
Eric Biggers | 46f47e4 | 2017-01-24 10:58:06 -0800 | [diff] [blame] | 16 | |
| 17 | #include <linux/key.h> |
| 18 | #include <linux/fs.h> |
| 19 | #include <linux/mm.h> |
| 20 | #include <linux/bio.h> |
| 21 | #include <linux/dcache.h> |
| 22 | #include <crypto/skcipher.h> |
| 23 | #include <uapi/linux/fs.h> |
| 24 | |
| 25 | #define FS_CRYPTO_BLOCK_SIZE 16 |
| 26 | |
| 27 | struct fscrypt_info; |
| 28 | |
| 29 | struct fscrypt_ctx { |
| 30 | union { |
| 31 | struct { |
| 32 | struct page *bounce_page; /* Ciphertext page */ |
| 33 | struct page *control_page; /* Original page */ |
| 34 | } w; |
| 35 | struct { |
| 36 | struct bio *bio; |
| 37 | struct work_struct work; |
| 38 | } r; |
| 39 | struct list_head free_list; /* Free list */ |
| 40 | }; |
| 41 | u8 flags; /* Flags */ |
| 42 | }; |
| 43 | |
| 44 | /** |
| 45 | * For encrypted symlinks, the ciphertext length is stored at the beginning |
| 46 | * of the string in little-endian format. |
| 47 | */ |
| 48 | struct fscrypt_symlink_data { |
| 49 | __le16 len; |
| 50 | char encrypted_path[1]; |
| 51 | } __packed; |
| 52 | |
Eric Biggers | 46f47e4 | 2017-01-24 10:58:06 -0800 | [diff] [blame] | 53 | struct fscrypt_str { |
| 54 | unsigned char *name; |
| 55 | u32 len; |
| 56 | }; |
| 57 | |
| 58 | struct fscrypt_name { |
| 59 | const struct qstr *usr_fname; |
| 60 | struct fscrypt_str disk_name; |
| 61 | u32 hash; |
| 62 | u32 minor_hash; |
| 63 | struct fscrypt_str crypto_buf; |
| 64 | }; |
| 65 | |
| 66 | #define FSTR_INIT(n, l) { .name = n, .len = l } |
| 67 | #define FSTR_TO_QSTR(f) QSTR_INIT((f)->name, (f)->len) |
| 68 | #define fname_name(p) ((p)->disk_name.name) |
| 69 | #define fname_len(p) ((p)->disk_name.len) |
| 70 | |
| 71 | /* |
| 72 | * fscrypt superblock flags |
| 73 | */ |
| 74 | #define FS_CFLG_OWN_PAGES (1U << 1) |
| 75 | |
| 76 | /* |
| 77 | * crypto opertions for filesystems |
| 78 | */ |
| 79 | struct fscrypt_operations { |
| 80 | unsigned int flags; |
| 81 | const char *key_prefix; |
| 82 | int (*get_context)(struct inode *, void *, size_t); |
Eric Biggers | 46f47e4 | 2017-01-24 10:58:06 -0800 | [diff] [blame] | 83 | int (*set_context)(struct inode *, const void *, size_t, void *); |
Eric Biggers | c250b7d | 2017-06-22 12:14:40 -0700 | [diff] [blame] | 84 | bool (*dummy_context)(struct inode *); |
Eric Biggers | 46f47e4 | 2017-01-24 10:58:06 -0800 | [diff] [blame] | 85 | bool (*empty_dir)(struct inode *); |
| 86 | unsigned (*max_namelen)(struct inode *); |
| 87 | }; |
| 88 | |
Tahsin Erdogan | af65207 | 2017-07-06 00:01:59 -0400 | [diff] [blame] | 89 | /* Maximum value for the third parameter of fscrypt_operations.set_context(). */ |
| 90 | #define FSCRYPT_SET_CONTEXT_MAX_SIZE 28 |
| 91 | |
Eric Biggers | 46f47e4 | 2017-01-24 10:58:06 -0800 | [diff] [blame] | 92 | static inline bool fscrypt_dummy_context_enabled(struct inode *inode) |
| 93 | { |
| 94 | if (inode->i_sb->s_cop->dummy_context && |
| 95 | inode->i_sb->s_cop->dummy_context(inode)) |
| 96 | return true; |
| 97 | return false; |
| 98 | } |
| 99 | |
Daniel Walter | b7e7cf7 | 2017-06-19 09:27:58 +0200 | [diff] [blame] | 100 | static inline bool fscrypt_valid_enc_modes(u32 contents_mode, |
| 101 | u32 filenames_mode) |
Eric Biggers | 46f47e4 | 2017-01-24 10:58:06 -0800 | [diff] [blame] | 102 | { |
Daniel Walter | b7e7cf7 | 2017-06-19 09:27:58 +0200 | [diff] [blame] | 103 | if (contents_mode == FS_ENCRYPTION_MODE_AES_128_CBC && |
| 104 | filenames_mode == FS_ENCRYPTION_MODE_AES_128_CTS) |
| 105 | return true; |
Eric Biggers | 46f47e4 | 2017-01-24 10:58:06 -0800 | [diff] [blame] | 106 | |
Daniel Walter | b7e7cf7 | 2017-06-19 09:27:58 +0200 | [diff] [blame] | 107 | if (contents_mode == FS_ENCRYPTION_MODE_AES_256_XTS && |
| 108 | filenames_mode == FS_ENCRYPTION_MODE_AES_256_CTS) |
| 109 | return true; |
| 110 | |
| 111 | return false; |
Eric Biggers | 46f47e4 | 2017-01-24 10:58:06 -0800 | [diff] [blame] | 112 | } |
| 113 | |
| 114 | static inline bool fscrypt_is_dot_dotdot(const struct qstr *str) |
| 115 | { |
| 116 | if (str->len == 1 && str->name[0] == '.') |
| 117 | return true; |
| 118 | |
| 119 | if (str->len == 2 && str->name[0] == '.' && str->name[1] == '.') |
| 120 | return true; |
| 121 | |
| 122 | return false; |
| 123 | } |
| 124 | |
Dave Chinner | 734f0d2 | 2017-10-09 12:15:34 -0700 | [diff] [blame] | 125 | #if __FS_HAS_ENCRYPTION |
Dave Chinner | 734f0d2 | 2017-10-09 12:15:34 -0700 | [diff] [blame] | 126 | #include <linux/fscrypt_supp.h> |
Eric Biggers | 4fd4b15 | 2018-01-05 10:44:53 -0800 | [diff] [blame^] | 127 | #else |
Dave Chinner | 734f0d2 | 2017-10-09 12:15:34 -0700 | [diff] [blame] | 128 | #include <linux/fscrypt_notsupp.h> |
Eric Biggers | 4fd4b15 | 2018-01-05 10:44:53 -0800 | [diff] [blame^] | 129 | #endif |
Dave Chinner | 734f0d2 | 2017-10-09 12:15:34 -0700 | [diff] [blame] | 130 | |
Eric Biggers | d293c3e | 2017-10-09 12:15:39 -0700 | [diff] [blame] | 131 | /** |
| 132 | * fscrypt_require_key - require an inode's encryption key |
| 133 | * @inode: the inode we need the key for |
| 134 | * |
| 135 | * If the inode is encrypted, set up its encryption key if not already done. |
| 136 | * Then require that the key be present and return -ENOKEY otherwise. |
| 137 | * |
| 138 | * No locks are needed, and the key will live as long as the struct inode --- so |
| 139 | * it won't go away from under you. |
| 140 | * |
| 141 | * Return: 0 on success, -ENOKEY if the key is missing, or another -errno code |
| 142 | * if a problem occurred while setting up the encryption key. |
| 143 | */ |
| 144 | static inline int fscrypt_require_key(struct inode *inode) |
| 145 | { |
| 146 | if (IS_ENCRYPTED(inode)) { |
| 147 | int err = fscrypt_get_encryption_info(inode); |
| 148 | |
| 149 | if (err) |
| 150 | return err; |
| 151 | if (!fscrypt_has_encryption_key(inode)) |
| 152 | return -ENOKEY; |
| 153 | } |
| 154 | return 0; |
| 155 | } |
Dave Chinner | 734f0d2 | 2017-10-09 12:15:34 -0700 | [diff] [blame] | 156 | |
Eric Biggers | 0ea87a9 | 2017-10-09 12:15:41 -0700 | [diff] [blame] | 157 | /** |
| 158 | * fscrypt_prepare_link - prepare to link an inode into a possibly-encrypted directory |
| 159 | * @old_dentry: an existing dentry for the inode being linked |
| 160 | * @dir: the target directory |
| 161 | * @dentry: negative dentry for the target filename |
| 162 | * |
| 163 | * A new link can only be added to an encrypted directory if the directory's |
| 164 | * encryption key is available --- since otherwise we'd have no way to encrypt |
| 165 | * the filename. Therefore, we first set up the directory's encryption key (if |
| 166 | * not already done) and return an error if it's unavailable. |
| 167 | * |
| 168 | * We also verify that the link will not violate the constraint that all files |
| 169 | * in an encrypted directory tree use the same encryption policy. |
| 170 | * |
| 171 | * Return: 0 on success, -ENOKEY if the directory's encryption key is missing, |
| 172 | * -EPERM if the link would result in an inconsistent encryption policy, or |
| 173 | * another -errno code. |
| 174 | */ |
| 175 | static inline int fscrypt_prepare_link(struct dentry *old_dentry, |
| 176 | struct inode *dir, |
| 177 | struct dentry *dentry) |
| 178 | { |
| 179 | if (IS_ENCRYPTED(dir)) |
| 180 | return __fscrypt_prepare_link(d_inode(old_dentry), dir); |
| 181 | return 0; |
| 182 | } |
| 183 | |
Eric Biggers | 94b26f3 | 2017-10-09 12:15:42 -0700 | [diff] [blame] | 184 | /** |
| 185 | * fscrypt_prepare_rename - prepare for a rename between possibly-encrypted directories |
| 186 | * @old_dir: source directory |
| 187 | * @old_dentry: dentry for source file |
| 188 | * @new_dir: target directory |
| 189 | * @new_dentry: dentry for target location (may be negative unless exchanging) |
| 190 | * @flags: rename flags (we care at least about %RENAME_EXCHANGE) |
| 191 | * |
| 192 | * Prepare for ->rename() where the source and/or target directories may be |
| 193 | * encrypted. A new link can only be added to an encrypted directory if the |
| 194 | * directory's encryption key is available --- since otherwise we'd have no way |
| 195 | * to encrypt the filename. A rename to an existing name, on the other hand, |
| 196 | * *is* cryptographically possible without the key. However, we take the more |
| 197 | * conservative approach and just forbid all no-key renames. |
| 198 | * |
| 199 | * We also verify that the rename will not violate the constraint that all files |
| 200 | * in an encrypted directory tree use the same encryption policy. |
| 201 | * |
| 202 | * Return: 0 on success, -ENOKEY if an encryption key is missing, -EPERM if the |
| 203 | * rename would cause inconsistent encryption policies, or another -errno code. |
| 204 | */ |
| 205 | static inline int fscrypt_prepare_rename(struct inode *old_dir, |
| 206 | struct dentry *old_dentry, |
| 207 | struct inode *new_dir, |
| 208 | struct dentry *new_dentry, |
| 209 | unsigned int flags) |
| 210 | { |
| 211 | if (IS_ENCRYPTED(old_dir) || IS_ENCRYPTED(new_dir)) |
| 212 | return __fscrypt_prepare_rename(old_dir, old_dentry, |
| 213 | new_dir, new_dentry, flags); |
| 214 | return 0; |
| 215 | } |
| 216 | |
Eric Biggers | 32c3cf0 | 2017-10-09 12:15:43 -0700 | [diff] [blame] | 217 | /** |
| 218 | * fscrypt_prepare_lookup - prepare to lookup a name in a possibly-encrypted directory |
| 219 | * @dir: directory being searched |
| 220 | * @dentry: filename being looked up |
| 221 | * @flags: lookup flags |
| 222 | * |
| 223 | * Prepare for ->lookup() in a directory which may be encrypted. Lookups can be |
| 224 | * done with or without the directory's encryption key; without the key, |
| 225 | * filenames are presented in encrypted form. Therefore, we'll try to set up |
| 226 | * the directory's encryption key, but even without it the lookup can continue. |
| 227 | * |
| 228 | * To allow invalidating stale dentries if the directory's encryption key is |
| 229 | * added later, we also install a custom ->d_revalidate() method and use the |
| 230 | * DCACHE_ENCRYPTED_WITH_KEY flag to indicate whether a given dentry is a |
| 231 | * plaintext name (flag set) or a ciphertext name (flag cleared). |
| 232 | * |
| 233 | * Return: 0 on success, -errno if a problem occurred while setting up the |
| 234 | * encryption key |
| 235 | */ |
| 236 | static inline int fscrypt_prepare_lookup(struct inode *dir, |
| 237 | struct dentry *dentry, |
| 238 | unsigned int flags) |
| 239 | { |
| 240 | if (IS_ENCRYPTED(dir)) |
| 241 | return __fscrypt_prepare_lookup(dir, dentry); |
| 242 | return 0; |
| 243 | } |
| 244 | |
Eric Biggers | 815dac3 | 2017-10-09 12:15:44 -0700 | [diff] [blame] | 245 | /** |
| 246 | * fscrypt_prepare_setattr - prepare to change a possibly-encrypted inode's attributes |
| 247 | * @dentry: dentry through which the inode is being changed |
| 248 | * @attr: attributes to change |
| 249 | * |
| 250 | * Prepare for ->setattr() on a possibly-encrypted inode. On an encrypted file, |
| 251 | * most attribute changes are allowed even without the encryption key. However, |
| 252 | * without the encryption key we do have to forbid truncates. This is needed |
| 253 | * because the size being truncated to may not be a multiple of the filesystem |
| 254 | * block size, and in that case we'd have to decrypt the final block, zero the |
| 255 | * portion past i_size, and re-encrypt it. (We *could* allow truncating to a |
| 256 | * filesystem block boundary, but it's simpler to just forbid all truncates --- |
| 257 | * and we already forbid all other contents modifications without the key.) |
| 258 | * |
| 259 | * Return: 0 on success, -ENOKEY if the key is missing, or another -errno code |
| 260 | * if a problem occurred while setting up the encryption key. |
| 261 | */ |
| 262 | static inline int fscrypt_prepare_setattr(struct dentry *dentry, |
| 263 | struct iattr *attr) |
| 264 | { |
| 265 | if (attr->ia_valid & ATTR_SIZE) |
| 266 | return fscrypt_require_key(d_inode(dentry)); |
| 267 | return 0; |
| 268 | } |
| 269 | |
Dave Chinner | 734f0d2 | 2017-10-09 12:15:34 -0700 | [diff] [blame] | 270 | #endif /* _LINUX_FSCRYPT_H */ |