Jaegeuk Kim | 0b81d07 | 2015-05-15 16:26:10 -0700 | [diff] [blame] | 1 | /* |
| 2 | * General per-file encryption definition |
| 3 | * |
| 4 | * Copyright (C) 2015, Google, Inc. |
| 5 | * |
| 6 | * Written by Michael Halcrow, 2015. |
| 7 | * Modified by Jaegeuk Kim, 2015. |
| 8 | */ |
| 9 | |
| 10 | #ifndef _LINUX_FSCRYPTO_H |
| 11 | #define _LINUX_FSCRYPTO_H |
| 12 | |
| 13 | #include <linux/key.h> |
| 14 | #include <linux/fs.h> |
| 15 | #include <linux/mm.h> |
| 16 | #include <linux/bio.h> |
| 17 | #include <linux/dcache.h> |
Linus Torvalds | d407574 | 2016-03-21 11:03:02 -0700 | [diff] [blame] | 18 | #include <crypto/skcipher.h> |
Jaegeuk Kim | 0b81d07 | 2015-05-15 16:26:10 -0700 | [diff] [blame] | 19 | #include <uapi/linux/fs.h> |
| 20 | |
| 21 | #define FS_KEY_DERIVATION_NONCE_SIZE 16 |
| 22 | #define FS_ENCRYPTION_CONTEXT_FORMAT_V1 1 |
| 23 | |
| 24 | #define FS_POLICY_FLAGS_PAD_4 0x00 |
| 25 | #define FS_POLICY_FLAGS_PAD_8 0x01 |
| 26 | #define FS_POLICY_FLAGS_PAD_16 0x02 |
| 27 | #define FS_POLICY_FLAGS_PAD_32 0x03 |
| 28 | #define FS_POLICY_FLAGS_PAD_MASK 0x03 |
| 29 | #define FS_POLICY_FLAGS_VALID 0x03 |
| 30 | |
| 31 | /* Encryption algorithms */ |
| 32 | #define FS_ENCRYPTION_MODE_INVALID 0 |
| 33 | #define FS_ENCRYPTION_MODE_AES_256_XTS 1 |
| 34 | #define FS_ENCRYPTION_MODE_AES_256_GCM 2 |
| 35 | #define FS_ENCRYPTION_MODE_AES_256_CBC 3 |
| 36 | #define FS_ENCRYPTION_MODE_AES_256_CTS 4 |
| 37 | |
| 38 | /** |
| 39 | * Encryption context for inode |
| 40 | * |
| 41 | * Protector format: |
| 42 | * 1 byte: Protector format (1 = this version) |
| 43 | * 1 byte: File contents encryption mode |
| 44 | * 1 byte: File names encryption mode |
| 45 | * 1 byte: Flags |
| 46 | * 8 bytes: Master Key descriptor |
| 47 | * 16 bytes: Encryption Key derivation nonce |
| 48 | */ |
| 49 | struct fscrypt_context { |
| 50 | u8 format; |
| 51 | u8 contents_encryption_mode; |
| 52 | u8 filenames_encryption_mode; |
| 53 | u8 flags; |
| 54 | u8 master_key_descriptor[FS_KEY_DESCRIPTOR_SIZE]; |
| 55 | u8 nonce[FS_KEY_DERIVATION_NONCE_SIZE]; |
| 56 | } __packed; |
| 57 | |
| 58 | /* Encryption parameters */ |
| 59 | #define FS_XTS_TWEAK_SIZE 16 |
| 60 | #define FS_AES_128_ECB_KEY_SIZE 16 |
| 61 | #define FS_AES_256_GCM_KEY_SIZE 32 |
| 62 | #define FS_AES_256_CBC_KEY_SIZE 32 |
| 63 | #define FS_AES_256_CTS_KEY_SIZE 32 |
| 64 | #define FS_AES_256_XTS_KEY_SIZE 64 |
| 65 | #define FS_MAX_KEY_SIZE 64 |
| 66 | |
| 67 | #define FS_KEY_DESC_PREFIX "fscrypt:" |
| 68 | #define FS_KEY_DESC_PREFIX_SIZE 8 |
| 69 | |
| 70 | /* This is passed in from userspace into the kernel keyring */ |
| 71 | struct fscrypt_key { |
| 72 | u32 mode; |
| 73 | u8 raw[FS_MAX_KEY_SIZE]; |
| 74 | u32 size; |
| 75 | } __packed; |
| 76 | |
| 77 | struct fscrypt_info { |
| 78 | u8 ci_data_mode; |
| 79 | u8 ci_filename_mode; |
| 80 | u8 ci_flags; |
Linus Torvalds | d407574 | 2016-03-21 11:03:02 -0700 | [diff] [blame] | 81 | struct crypto_skcipher *ci_ctfm; |
Jaegeuk Kim | 0b81d07 | 2015-05-15 16:26:10 -0700 | [diff] [blame] | 82 | struct key *ci_keyring_key; |
| 83 | u8 ci_master_key[FS_KEY_DESCRIPTOR_SIZE]; |
| 84 | }; |
| 85 | |
| 86 | #define FS_CTX_REQUIRES_FREE_ENCRYPT_FL 0x00000001 |
| 87 | #define FS_WRITE_PATH_FL 0x00000002 |
| 88 | |
| 89 | struct fscrypt_ctx { |
| 90 | union { |
| 91 | struct { |
| 92 | struct page *bounce_page; /* Ciphertext page */ |
| 93 | struct page *control_page; /* Original page */ |
| 94 | } w; |
| 95 | struct { |
| 96 | struct bio *bio; |
| 97 | struct work_struct work; |
| 98 | } r; |
| 99 | struct list_head free_list; /* Free list */ |
| 100 | }; |
| 101 | u8 flags; /* Flags */ |
| 102 | u8 mode; /* Encryption mode for tfm */ |
| 103 | }; |
| 104 | |
| 105 | struct fscrypt_completion_result { |
| 106 | struct completion completion; |
| 107 | int res; |
| 108 | }; |
| 109 | |
| 110 | #define DECLARE_FS_COMPLETION_RESULT(ecr) \ |
| 111 | struct fscrypt_completion_result ecr = { \ |
| 112 | COMPLETION_INITIALIZER((ecr).completion), 0 } |
| 113 | |
Jaegeuk Kim | 0b81d07 | 2015-05-15 16:26:10 -0700 | [diff] [blame] | 114 | #define FS_FNAME_NUM_SCATTER_ENTRIES 4 |
| 115 | #define FS_CRYPTO_BLOCK_SIZE 16 |
| 116 | #define FS_FNAME_CRYPTO_DIGEST_SIZE 32 |
| 117 | |
| 118 | /** |
| 119 | * For encrypted symlinks, the ciphertext length is stored at the beginning |
| 120 | * of the string in little-endian format. |
| 121 | */ |
| 122 | struct fscrypt_symlink_data { |
| 123 | __le16 len; |
| 124 | char encrypted_path[1]; |
| 125 | } __packed; |
| 126 | |
| 127 | /** |
| 128 | * This function is used to calculate the disk space required to |
| 129 | * store a filename of length l in encrypted symlink format. |
| 130 | */ |
| 131 | static inline u32 fscrypt_symlink_data_len(u32 l) |
| 132 | { |
| 133 | if (l < FS_CRYPTO_BLOCK_SIZE) |
| 134 | l = FS_CRYPTO_BLOCK_SIZE; |
| 135 | return (l + sizeof(struct fscrypt_symlink_data) - 1); |
| 136 | } |
| 137 | |
| 138 | struct fscrypt_str { |
| 139 | unsigned char *name; |
| 140 | u32 len; |
| 141 | }; |
| 142 | |
| 143 | struct fscrypt_name { |
| 144 | const struct qstr *usr_fname; |
| 145 | struct fscrypt_str disk_name; |
| 146 | u32 hash; |
| 147 | u32 minor_hash; |
| 148 | struct fscrypt_str crypto_buf; |
| 149 | }; |
| 150 | |
| 151 | #define FSTR_INIT(n, l) { .name = n, .len = l } |
| 152 | #define FSTR_TO_QSTR(f) QSTR_INIT((f)->name, (f)->len) |
| 153 | #define fname_name(p) ((p)->disk_name.name) |
| 154 | #define fname_len(p) ((p)->disk_name.len) |
| 155 | |
| 156 | /* |
David Gstir | 1c7dcf6 | 2016-11-13 22:20:44 +0100 | [diff] [blame] | 157 | * fscrypt superblock flags |
| 158 | */ |
| 159 | #define FS_CFLG_INPLACE_ENCRYPTION (1U << 1) |
| 160 | |
| 161 | /* |
Jaegeuk Kim | 0b81d07 | 2015-05-15 16:26:10 -0700 | [diff] [blame] | 162 | * crypto opertions for filesystems |
| 163 | */ |
| 164 | struct fscrypt_operations { |
David Gstir | 1c7dcf6 | 2016-11-13 22:20:44 +0100 | [diff] [blame] | 165 | unsigned int flags; |
Jaegeuk Kim | 0b81d07 | 2015-05-15 16:26:10 -0700 | [diff] [blame] | 166 | int (*get_context)(struct inode *, void *, size_t); |
Jaegeuk Kim | b5a7aef | 2016-05-04 22:05:01 -0700 | [diff] [blame] | 167 | int (*key_prefix)(struct inode *, u8 **); |
Jaegeuk Kim | 0b81d07 | 2015-05-15 16:26:10 -0700 | [diff] [blame] | 168 | int (*prepare_context)(struct inode *); |
| 169 | int (*set_context)(struct inode *, const void *, size_t, void *); |
| 170 | int (*dummy_context)(struct inode *); |
| 171 | bool (*is_encrypted)(struct inode *); |
| 172 | bool (*empty_dir)(struct inode *); |
| 173 | unsigned (*max_namelen)(struct inode *); |
| 174 | }; |
| 175 | |
| 176 | static inline bool fscrypt_dummy_context_enabled(struct inode *inode) |
| 177 | { |
| 178 | if (inode->i_sb->s_cop->dummy_context && |
| 179 | inode->i_sb->s_cop->dummy_context(inode)) |
| 180 | return true; |
| 181 | return false; |
| 182 | } |
| 183 | |
| 184 | static inline bool fscrypt_valid_contents_enc_mode(u32 mode) |
| 185 | { |
| 186 | return (mode == FS_ENCRYPTION_MODE_AES_256_XTS); |
| 187 | } |
| 188 | |
| 189 | static inline bool fscrypt_valid_filenames_enc_mode(u32 mode) |
| 190 | { |
| 191 | return (mode == FS_ENCRYPTION_MODE_AES_256_CTS); |
| 192 | } |
| 193 | |
Jaegeuk Kim | 0b81d07 | 2015-05-15 16:26:10 -0700 | [diff] [blame] | 194 | static inline bool fscrypt_is_dot_dotdot(const struct qstr *str) |
| 195 | { |
| 196 | if (str->len == 1 && str->name[0] == '.') |
| 197 | return true; |
| 198 | |
| 199 | if (str->len == 2 && str->name[0] == '.' && str->name[1] == '.') |
| 200 | return true; |
| 201 | |
| 202 | return false; |
| 203 | } |
| 204 | |
| 205 | static inline struct page *fscrypt_control_page(struct page *page) |
| 206 | { |
| 207 | #if IS_ENABLED(CONFIG_FS_ENCRYPTION) |
| 208 | return ((struct fscrypt_ctx *)page_private(page))->w.control_page; |
| 209 | #else |
| 210 | WARN_ON_ONCE(1); |
| 211 | return ERR_PTR(-EINVAL); |
| 212 | #endif |
| 213 | } |
| 214 | |
| 215 | static inline int fscrypt_has_encryption_key(struct inode *inode) |
| 216 | { |
| 217 | #if IS_ENABLED(CONFIG_FS_ENCRYPTION) |
| 218 | return (inode->i_crypt_info != NULL); |
| 219 | #else |
| 220 | return 0; |
| 221 | #endif |
| 222 | } |
| 223 | |
| 224 | static inline void fscrypt_set_encrypted_dentry(struct dentry *dentry) |
| 225 | { |
| 226 | #if IS_ENABLED(CONFIG_FS_ENCRYPTION) |
| 227 | spin_lock(&dentry->d_lock); |
| 228 | dentry->d_flags |= DCACHE_ENCRYPTED_WITH_KEY; |
| 229 | spin_unlock(&dentry->d_lock); |
| 230 | #endif |
| 231 | } |
| 232 | |
| 233 | #if IS_ENABLED(CONFIG_FS_ENCRYPTION) |
| 234 | extern const struct dentry_operations fscrypt_d_ops; |
| 235 | #endif |
| 236 | |
| 237 | static inline void fscrypt_set_d_op(struct dentry *dentry) |
| 238 | { |
| 239 | #if IS_ENABLED(CONFIG_FS_ENCRYPTION) |
| 240 | d_set_d_op(dentry, &fscrypt_d_ops); |
| 241 | #endif |
| 242 | } |
| 243 | |
| 244 | #if IS_ENABLED(CONFIG_FS_ENCRYPTION) |
| 245 | /* crypto.c */ |
| 246 | extern struct kmem_cache *fscrypt_info_cachep; |
| 247 | int fscrypt_initialize(void); |
| 248 | |
Jaegeuk Kim | b32e4482 | 2016-04-11 15:51:57 -0700 | [diff] [blame] | 249 | extern struct fscrypt_ctx *fscrypt_get_ctx(struct inode *, gfp_t); |
Jaegeuk Kim | 0b81d07 | 2015-05-15 16:26:10 -0700 | [diff] [blame] | 250 | extern void fscrypt_release_ctx(struct fscrypt_ctx *); |
David Gstir | 7821d4d | 2016-11-13 22:20:46 +0100 | [diff] [blame^] | 251 | extern struct page *fscrypt_encrypt_page(struct inode *, struct page *, |
| 252 | unsigned int, unsigned int, |
| 253 | gfp_t); |
| 254 | extern int fscrypt_decrypt_page(struct inode *, struct page *, unsigned int, |
| 255 | unsigned int); |
Jaegeuk Kim | 0b81d07 | 2015-05-15 16:26:10 -0700 | [diff] [blame] | 256 | extern void fscrypt_decrypt_bio_pages(struct fscrypt_ctx *, struct bio *); |
| 257 | extern void fscrypt_pullback_bio_page(struct page **, bool); |
| 258 | extern void fscrypt_restore_control_page(struct page *); |
| 259 | extern int fscrypt_zeroout_range(struct inode *, pgoff_t, sector_t, |
| 260 | unsigned int); |
| 261 | /* policy.c */ |
Eric Biggers | ba63f23 | 2016-09-08 14:20:38 -0700 | [diff] [blame] | 262 | extern int fscrypt_process_policy(struct file *, const struct fscrypt_policy *); |
Jaegeuk Kim | 0b81d07 | 2015-05-15 16:26:10 -0700 | [diff] [blame] | 263 | extern int fscrypt_get_policy(struct inode *, struct fscrypt_policy *); |
| 264 | extern int fscrypt_has_permitted_context(struct inode *, struct inode *); |
| 265 | extern int fscrypt_inherit_context(struct inode *, struct inode *, |
| 266 | void *, bool); |
| 267 | /* keyinfo.c */ |
| 268 | extern int get_crypt_info(struct inode *); |
| 269 | extern int fscrypt_get_encryption_info(struct inode *); |
| 270 | extern void fscrypt_put_encryption_info(struct inode *, struct fscrypt_info *); |
| 271 | |
| 272 | /* fname.c */ |
| 273 | extern int fscrypt_setup_filename(struct inode *, const struct qstr *, |
| 274 | int lookup, struct fscrypt_name *); |
| 275 | extern void fscrypt_free_filename(struct fscrypt_name *); |
| 276 | extern u32 fscrypt_fname_encrypted_size(struct inode *, u32); |
| 277 | extern int fscrypt_fname_alloc_buffer(struct inode *, u32, |
| 278 | struct fscrypt_str *); |
| 279 | extern void fscrypt_fname_free_buffer(struct fscrypt_str *); |
| 280 | extern int fscrypt_fname_disk_to_usr(struct inode *, u32, u32, |
| 281 | const struct fscrypt_str *, struct fscrypt_str *); |
| 282 | extern int fscrypt_fname_usr_to_disk(struct inode *, const struct qstr *, |
| 283 | struct fscrypt_str *); |
| 284 | #endif |
| 285 | |
| 286 | /* crypto.c */ |
Jaegeuk Kim | b32e4482 | 2016-04-11 15:51:57 -0700 | [diff] [blame] | 287 | static inline struct fscrypt_ctx *fscrypt_notsupp_get_ctx(struct inode *i, |
| 288 | gfp_t f) |
Jaegeuk Kim | 0b81d07 | 2015-05-15 16:26:10 -0700 | [diff] [blame] | 289 | { |
| 290 | return ERR_PTR(-EOPNOTSUPP); |
| 291 | } |
| 292 | |
| 293 | static inline void fscrypt_notsupp_release_ctx(struct fscrypt_ctx *c) |
| 294 | { |
| 295 | return; |
| 296 | } |
| 297 | |
| 298 | static inline struct page *fscrypt_notsupp_encrypt_page(struct inode *i, |
David Gstir | 7821d4d | 2016-11-13 22:20:46 +0100 | [diff] [blame^] | 299 | struct page *p, |
| 300 | unsigned int len, |
| 301 | unsigned int offs, |
| 302 | gfp_t f) |
Jaegeuk Kim | 0b81d07 | 2015-05-15 16:26:10 -0700 | [diff] [blame] | 303 | { |
| 304 | return ERR_PTR(-EOPNOTSUPP); |
| 305 | } |
| 306 | |
David Gstir | 7821d4d | 2016-11-13 22:20:46 +0100 | [diff] [blame^] | 307 | static inline int fscrypt_notsupp_decrypt_page(struct inode *i, struct page *p, |
| 308 | unsigned int len, unsigned int offs) |
Jaegeuk Kim | 0b81d07 | 2015-05-15 16:26:10 -0700 | [diff] [blame] | 309 | { |
| 310 | return -EOPNOTSUPP; |
| 311 | } |
| 312 | |
| 313 | static inline void fscrypt_notsupp_decrypt_bio_pages(struct fscrypt_ctx *c, |
| 314 | struct bio *b) |
| 315 | { |
| 316 | return; |
| 317 | } |
| 318 | |
| 319 | static inline void fscrypt_notsupp_pullback_bio_page(struct page **p, bool b) |
| 320 | { |
| 321 | return; |
| 322 | } |
| 323 | |
| 324 | static inline void fscrypt_notsupp_restore_control_page(struct page *p) |
| 325 | { |
| 326 | return; |
| 327 | } |
| 328 | |
| 329 | static inline int fscrypt_notsupp_zeroout_range(struct inode *i, pgoff_t p, |
| 330 | sector_t s, unsigned int f) |
| 331 | { |
| 332 | return -EOPNOTSUPP; |
| 333 | } |
| 334 | |
| 335 | /* policy.c */ |
Eric Biggers | ba63f23 | 2016-09-08 14:20:38 -0700 | [diff] [blame] | 336 | static inline int fscrypt_notsupp_process_policy(struct file *f, |
Jaegeuk Kim | 0b81d07 | 2015-05-15 16:26:10 -0700 | [diff] [blame] | 337 | const struct fscrypt_policy *p) |
| 338 | { |
| 339 | return -EOPNOTSUPP; |
| 340 | } |
| 341 | |
| 342 | static inline int fscrypt_notsupp_get_policy(struct inode *i, |
| 343 | struct fscrypt_policy *p) |
| 344 | { |
| 345 | return -EOPNOTSUPP; |
| 346 | } |
| 347 | |
| 348 | static inline int fscrypt_notsupp_has_permitted_context(struct inode *p, |
| 349 | struct inode *i) |
| 350 | { |
| 351 | return 0; |
| 352 | } |
| 353 | |
| 354 | static inline int fscrypt_notsupp_inherit_context(struct inode *p, |
| 355 | struct inode *i, void *v, bool b) |
| 356 | { |
| 357 | return -EOPNOTSUPP; |
| 358 | } |
| 359 | |
| 360 | /* keyinfo.c */ |
| 361 | static inline int fscrypt_notsupp_get_encryption_info(struct inode *i) |
| 362 | { |
| 363 | return -EOPNOTSUPP; |
| 364 | } |
| 365 | |
| 366 | static inline void fscrypt_notsupp_put_encryption_info(struct inode *i, |
| 367 | struct fscrypt_info *f) |
| 368 | { |
| 369 | return; |
| 370 | } |
| 371 | |
| 372 | /* fname.c */ |
| 373 | static inline int fscrypt_notsupp_setup_filename(struct inode *dir, |
| 374 | const struct qstr *iname, |
| 375 | int lookup, struct fscrypt_name *fname) |
| 376 | { |
| 377 | if (dir->i_sb->s_cop->is_encrypted(dir)) |
| 378 | return -EOPNOTSUPP; |
| 379 | |
| 380 | memset(fname, 0, sizeof(struct fscrypt_name)); |
| 381 | fname->usr_fname = iname; |
| 382 | fname->disk_name.name = (unsigned char *)iname->name; |
| 383 | fname->disk_name.len = iname->len; |
| 384 | return 0; |
| 385 | } |
| 386 | |
| 387 | static inline void fscrypt_notsupp_free_filename(struct fscrypt_name *fname) |
| 388 | { |
| 389 | return; |
| 390 | } |
| 391 | |
| 392 | static inline u32 fscrypt_notsupp_fname_encrypted_size(struct inode *i, u32 s) |
| 393 | { |
| 394 | /* never happens */ |
| 395 | WARN_ON(1); |
| 396 | return 0; |
| 397 | } |
| 398 | |
| 399 | static inline int fscrypt_notsupp_fname_alloc_buffer(struct inode *inode, |
| 400 | u32 ilen, struct fscrypt_str *crypto_str) |
| 401 | { |
| 402 | return -EOPNOTSUPP; |
| 403 | } |
| 404 | |
| 405 | static inline void fscrypt_notsupp_fname_free_buffer(struct fscrypt_str *c) |
| 406 | { |
| 407 | return; |
| 408 | } |
| 409 | |
| 410 | static inline int fscrypt_notsupp_fname_disk_to_usr(struct inode *inode, |
| 411 | u32 hash, u32 minor_hash, |
| 412 | const struct fscrypt_str *iname, |
| 413 | struct fscrypt_str *oname) |
| 414 | { |
| 415 | return -EOPNOTSUPP; |
| 416 | } |
| 417 | |
| 418 | static inline int fscrypt_notsupp_fname_usr_to_disk(struct inode *inode, |
| 419 | const struct qstr *iname, |
| 420 | struct fscrypt_str *oname) |
| 421 | { |
| 422 | return -EOPNOTSUPP; |
| 423 | } |
| 424 | #endif /* _LINUX_FSCRYPTO_H */ |