1 /* SPDX-License-Identifier: GPL-2.0 */ 2 /* 3 * fscrypt_private.h 4 * 5 * Copyright (C) 2015, Google, Inc. 6 * 7 * Originally written by Michael Halcrow, Ildar Muslukhov, and Uday Savagaonkar. 8 * Heavily modified since then. 9 */ 10 11 #ifndef _FSCRYPT_PRIVATE_H 12 #define _FSCRYPT_PRIVATE_H 13 14 #include <linux/fscrypt.h> 15 #include <linux/minmax.h> 16 #include <linux/siphash.h> 17 #include <crypto/hash.h> 18 #include <linux/blk-crypto.h> 19 20 #define CONST_STRLEN(str) (sizeof(str) - 1) 21 22 #define FSCRYPT_FILE_NONCE_SIZE 16 23 24 /* 25 * Minimum size of an fscrypt master key. Note: a longer key will be required 26 * if ciphers with a 256-bit security strength are used. This is just the 27 * absolute minimum, which applies when only 128-bit encryption is used. 28 */ 29 #define FSCRYPT_MIN_KEY_SIZE 16 30 31 /* Maximum size of a raw fscrypt master key */ 32 #define FSCRYPT_MAX_RAW_KEY_SIZE 64 33 34 /* Maximum size of a hardware-wrapped fscrypt master key */ 35 #define FSCRYPT_MAX_HW_WRAPPED_KEY_SIZE BLK_CRYPTO_MAX_HW_WRAPPED_KEY_SIZE 36 37 /* Maximum size of an fscrypt master key across both key types */ 38 #define FSCRYPT_MAX_ANY_KEY_SIZE \ 39 MAX(FSCRYPT_MAX_RAW_KEY_SIZE, FSCRYPT_MAX_HW_WRAPPED_KEY_SIZE) 40 41 /* 42 * FSCRYPT_MAX_KEY_SIZE is defined in the UAPI header, but the addition of 43 * hardware-wrapped keys has made it misleading as it's only for raw keys. 44 * Don't use it in kernel code; use one of the above constants instead. 45 */ 46 #undef FSCRYPT_MAX_KEY_SIZE 47 48 /* 49 * This mask is passed as the third argument to the crypto_alloc_*() functions 50 * to prevent fscrypt from using the Crypto API drivers for non-inline crypto 51 * engines. Those drivers have been problematic for fscrypt. fscrypt users 52 * have reported hangs and even incorrect en/decryption with these drivers. 53 * Since going to the driver, off CPU, and back again is really slow, such 54 * drivers can be over 50 times slower than the CPU-based code for fscrypt's 55 * workload. Even on platforms that lack AES instructions on the CPU, using the 56 * offloads has been shown to be slower, even staying with AES. (Of course, 57 * Adiantum is faster still, and is the recommended option on such platforms...) 58 * 59 * Note that fscrypt also supports inline crypto engines. Those don't use the 60 * Crypto API and work much better than the old-style (non-inline) engines. 61 */ 62 #define FSCRYPT_CRYPTOAPI_MASK \ 63 (CRYPTO_ALG_ASYNC | CRYPTO_ALG_ALLOCATES_MEMORY | \ 64 CRYPTO_ALG_KERN_DRIVER_ONLY) 65 66 #define FSCRYPT_CONTEXT_V1 1 67 #define FSCRYPT_CONTEXT_V2 2 68 69 /* Keep this in sync with include/uapi/linux/fscrypt.h */ 70 #define FSCRYPT_MODE_MAX FSCRYPT_MODE_AES_256_HCTR2 71 72 struct fscrypt_context_v1 { 73 u8 version; /* FSCRYPT_CONTEXT_V1 */ 74 u8 contents_encryption_mode; 75 u8 filenames_encryption_mode; 76 u8 flags; 77 u8 master_key_descriptor[FSCRYPT_KEY_DESCRIPTOR_SIZE]; 78 u8 nonce[FSCRYPT_FILE_NONCE_SIZE]; 79 }; 80 81 struct fscrypt_context_v2 { 82 u8 version; /* FSCRYPT_CONTEXT_V2 */ 83 u8 contents_encryption_mode; 84 u8 filenames_encryption_mode; 85 u8 flags; 86 u8 log2_data_unit_size; 87 u8 __reserved[3]; 88 u8 master_key_identifier[FSCRYPT_KEY_IDENTIFIER_SIZE]; 89 u8 nonce[FSCRYPT_FILE_NONCE_SIZE]; 90 }; 91 92 /* 93 * fscrypt_context - the encryption context of an inode 94 * 95 * This is the on-disk equivalent of an fscrypt_policy, stored alongside each 96 * encrypted file usually in a hidden extended attribute. It contains the 97 * fields from the fscrypt_policy, in order to identify the encryption algorithm 98 * and key with which the file is encrypted. It also contains a nonce that was 99 * randomly generated by fscrypt itself; this is used as KDF input or as a tweak 100 * to cause different files to be encrypted differently. 101 */ 102 union fscrypt_context { 103 u8 version; 104 struct fscrypt_context_v1 v1; 105 struct fscrypt_context_v2 v2; 106 }; 107 108 /* 109 * Return the size expected for the given fscrypt_context based on its version 110 * number, or 0 if the context version is unrecognized. 111 */ 112 static inline int fscrypt_context_size(const union fscrypt_context *ctx) 113 { 114 switch (ctx->version) { 115 case FSCRYPT_CONTEXT_V1: 116 BUILD_BUG_ON(sizeof(ctx->v1) != 28); 117 return sizeof(ctx->v1); 118 case FSCRYPT_CONTEXT_V2: 119 BUILD_BUG_ON(sizeof(ctx->v2) != 40); 120 return sizeof(ctx->v2); 121 } 122 return 0; 123 } 124 125 /* Check whether an fscrypt_context has a recognized version number and size */ 126 static inline bool fscrypt_context_is_valid(const union fscrypt_context *ctx, 127 int ctx_size) 128 { 129 return ctx_size >= 1 && ctx_size == fscrypt_context_size(ctx); 130 } 131 132 /* Retrieve the context's nonce, assuming the context was already validated */ 133 static inline const u8 *fscrypt_context_nonce(const union fscrypt_context *ctx) 134 { 135 switch (ctx->version) { 136 case FSCRYPT_CONTEXT_V1: 137 return ctx->v1.nonce; 138 case FSCRYPT_CONTEXT_V2: 139 return ctx->v2.nonce; 140 } 141 WARN_ON_ONCE(1); 142 return NULL; 143 } 144 145 union fscrypt_policy { 146 u8 version; 147 struct fscrypt_policy_v1 v1; 148 struct fscrypt_policy_v2 v2; 149 }; 150 151 /* 152 * Return the size expected for the given fscrypt_policy based on its version 153 * number, or 0 if the policy version is unrecognized. 154 */ 155 static inline int fscrypt_policy_size(const union fscrypt_policy *policy) 156 { 157 switch (policy->version) { 158 case FSCRYPT_POLICY_V1: 159 return sizeof(policy->v1); 160 case FSCRYPT_POLICY_V2: 161 return sizeof(policy->v2); 162 } 163 return 0; 164 } 165 166 /* Return the contents encryption mode of a valid encryption policy */ 167 static inline u8 168 fscrypt_policy_contents_mode(const union fscrypt_policy *policy) 169 { 170 switch (policy->version) { 171 case FSCRYPT_POLICY_V1: 172 return policy->v1.contents_encryption_mode; 173 case FSCRYPT_POLICY_V2: 174 return policy->v2.contents_encryption_mode; 175 } 176 BUG(); 177 } 178 179 /* Return the filenames encryption mode of a valid encryption policy */ 180 static inline u8 181 fscrypt_policy_fnames_mode(const union fscrypt_policy *policy) 182 { 183 switch (policy->version) { 184 case FSCRYPT_POLICY_V1: 185 return policy->v1.filenames_encryption_mode; 186 case FSCRYPT_POLICY_V2: 187 return policy->v2.filenames_encryption_mode; 188 } 189 BUG(); 190 } 191 192 /* Return the flags (FSCRYPT_POLICY_FLAG*) of a valid encryption policy */ 193 static inline u8 194 fscrypt_policy_flags(const union fscrypt_policy *policy) 195 { 196 switch (policy->version) { 197 case FSCRYPT_POLICY_V1: 198 return policy->v1.flags; 199 case FSCRYPT_POLICY_V2: 200 return policy->v2.flags; 201 } 202 BUG(); 203 } 204 205 static inline int 206 fscrypt_policy_v2_du_bits(const struct fscrypt_policy_v2 *policy, 207 const struct inode *inode) 208 { 209 return policy->log2_data_unit_size ?: inode->i_blkbits; 210 } 211 212 static inline int 213 fscrypt_policy_du_bits(const union fscrypt_policy *policy, 214 const struct inode *inode) 215 { 216 switch (policy->version) { 217 case FSCRYPT_POLICY_V1: 218 return inode->i_blkbits; 219 case FSCRYPT_POLICY_V2: 220 return fscrypt_policy_v2_du_bits(&policy->v2, inode); 221 } 222 BUG(); 223 } 224 225 /* 226 * For encrypted symlinks, the ciphertext length is stored at the beginning 227 * of the string in little-endian format. 228 */ 229 struct fscrypt_symlink_data { 230 __le16 len; 231 char encrypted_path[]; 232 } __packed; 233 234 /** 235 * struct fscrypt_prepared_key - a key prepared for actual encryption/decryption 236 * @tfm: crypto API transform object 237 * @blk_key: key for blk-crypto 238 * 239 * Normally only one of the fields will be non-NULL. 240 */ 241 struct fscrypt_prepared_key { 242 struct crypto_sync_skcipher *tfm; 243 #ifdef CONFIG_FS_ENCRYPTION_INLINE_CRYPT 244 struct blk_crypto_key *blk_key; 245 #endif 246 }; 247 248 /* 249 * fscrypt_inode_info - the "encryption key" for an inode 250 * 251 * When an encrypted file's key is made available, an instance of this struct is 252 * allocated and stored in ->i_crypt_info. Once created, it remains until the 253 * inode is evicted. 254 */ 255 struct fscrypt_inode_info { 256 257 /* The key in a form prepared for actual encryption/decryption */ 258 struct fscrypt_prepared_key ci_enc_key; 259 260 /* True if ci_enc_key should be freed when this struct is freed */ 261 u8 ci_owns_key : 1; 262 263 #ifdef CONFIG_FS_ENCRYPTION_INLINE_CRYPT 264 /* 265 * True if this inode will use inline encryption (blk-crypto) instead of 266 * the traditional filesystem-layer encryption. 267 */ 268 u8 ci_inlinecrypt : 1; 269 #endif 270 271 /* True if ci_dirhash_key is initialized */ 272 u8 ci_dirhash_key_initialized : 1; 273 274 /* 275 * log2 of the data unit size (granularity of contents encryption) of 276 * this file. This is computable from ci_policy and ci_inode but is 277 * cached here for efficiency. Only used for regular files. 278 */ 279 u8 ci_data_unit_bits; 280 281 /* Cached value: log2 of number of data units per FS block */ 282 u8 ci_data_units_per_block_bits; 283 284 /* Hashed inode number. Only set for IV_INO_LBLK_32 */ 285 u32 ci_hashed_ino; 286 287 /* 288 * Encryption mode used for this inode. It corresponds to either the 289 * contents or filenames encryption mode, depending on the inode type. 290 */ 291 struct fscrypt_mode *ci_mode; 292 293 /* Back-pointer to the inode */ 294 struct inode *ci_inode; 295 296 /* 297 * The master key with which this inode was unlocked (decrypted). This 298 * will be NULL if the master key was found in a process-subscribed 299 * keyring rather than in the filesystem-level keyring. 300 */ 301 struct fscrypt_master_key *ci_master_key; 302 303 /* 304 * Link in list of inodes that were unlocked with the master key. 305 * Only used when ->ci_master_key is set. 306 */ 307 struct list_head ci_master_key_link; 308 309 /* 310 * If non-NULL, then encryption is done using the master key directly 311 * and ci_enc_key will equal ci_direct_key->dk_key. 312 */ 313 struct fscrypt_direct_key *ci_direct_key; 314 315 /* 316 * This inode's hash key for filenames. This is a 128-bit SipHash-2-4 317 * key. This is only set for directories that use a keyed dirhash over 318 * the plaintext filenames -- currently just casefolded directories. 319 */ 320 siphash_key_t ci_dirhash_key; 321 322 /* The encryption policy used by this inode */ 323 union fscrypt_policy ci_policy; 324 325 /* This inode's nonce, copied from the fscrypt_context */ 326 u8 ci_nonce[FSCRYPT_FILE_NONCE_SIZE]; 327 }; 328 329 typedef enum { 330 FS_DECRYPT = 0, 331 FS_ENCRYPT, 332 } fscrypt_direction_t; 333 334 /* crypto.c */ 335 extern struct kmem_cache *fscrypt_inode_info_cachep; 336 int fscrypt_initialize(struct super_block *sb); 337 int fscrypt_crypt_data_unit(const struct fscrypt_inode_info *ci, 338 fscrypt_direction_t rw, u64 index, 339 struct page *src_page, struct page *dest_page, 340 unsigned int len, unsigned int offs); 341 struct page *fscrypt_alloc_bounce_page(gfp_t gfp_flags); 342 343 void __printf(3, 4) __cold 344 fscrypt_msg(const struct inode *inode, const char *level, const char *fmt, ...); 345 346 #define fscrypt_warn(inode, fmt, ...) \ 347 fscrypt_msg((inode), KERN_WARNING, fmt, ##__VA_ARGS__) 348 #define fscrypt_err(inode, fmt, ...) \ 349 fscrypt_msg((inode), KERN_ERR, fmt, ##__VA_ARGS__) 350 351 #define FSCRYPT_MAX_IV_SIZE 32 352 353 union fscrypt_iv { 354 struct { 355 /* zero-based index of data unit within the file */ 356 __le64 index; 357 358 /* per-file nonce; only set in DIRECT_KEY mode */ 359 u8 nonce[FSCRYPT_FILE_NONCE_SIZE]; 360 }; 361 u8 raw[FSCRYPT_MAX_IV_SIZE]; 362 __le64 dun[FSCRYPT_MAX_IV_SIZE / sizeof(__le64)]; 363 }; 364 365 void fscrypt_generate_iv(union fscrypt_iv *iv, u64 index, 366 const struct fscrypt_inode_info *ci); 367 368 /* 369 * Return the number of bits used by the maximum file data unit index that is 370 * possible on the given filesystem, using the given log2 data unit size. 371 */ 372 static inline int 373 fscrypt_max_file_dun_bits(const struct super_block *sb, int du_bits) 374 { 375 return fls64(sb->s_maxbytes - 1) - du_bits; 376 } 377 378 /* fname.c */ 379 bool __fscrypt_fname_encrypted_size(const union fscrypt_policy *policy, 380 u32 orig_len, u32 max_len, 381 u32 *encrypted_len_ret); 382 383 /* hkdf.c */ 384 struct fscrypt_hkdf { 385 struct crypto_shash *hmac_tfm; 386 }; 387 388 int fscrypt_init_hkdf(struct fscrypt_hkdf *hkdf, const u8 *master_key, 389 unsigned int master_key_size); 390 391 /* 392 * The list of contexts in which fscrypt uses HKDF. These values are used as 393 * the first byte of the HKDF application-specific info string to guarantee that 394 * info strings are never repeated between contexts. This ensures that all HKDF 395 * outputs are unique and cryptographically isolated, i.e. knowledge of one 396 * output doesn't reveal another. 397 */ 398 #define HKDF_CONTEXT_KEY_IDENTIFIER_FOR_RAW_KEY 1 /* info=<empty> */ 399 #define HKDF_CONTEXT_PER_FILE_ENC_KEY 2 /* info=file_nonce */ 400 #define HKDF_CONTEXT_DIRECT_KEY 3 /* info=mode_num */ 401 #define HKDF_CONTEXT_IV_INO_LBLK_64_KEY 4 /* info=mode_num||fs_uuid */ 402 #define HKDF_CONTEXT_DIRHASH_KEY 5 /* info=file_nonce */ 403 #define HKDF_CONTEXT_IV_INO_LBLK_32_KEY 6 /* info=mode_num||fs_uuid */ 404 #define HKDF_CONTEXT_INODE_HASH_KEY 7 /* info=<empty> */ 405 #define HKDF_CONTEXT_KEY_IDENTIFIER_FOR_HW_WRAPPED_KEY \ 406 8 /* info=<empty> */ 407 408 int fscrypt_hkdf_expand(const struct fscrypt_hkdf *hkdf, u8 context, 409 const u8 *info, unsigned int infolen, 410 u8 *okm, unsigned int okmlen); 411 412 void fscrypt_destroy_hkdf(struct fscrypt_hkdf *hkdf); 413 414 /* inline_crypt.c */ 415 #ifdef CONFIG_FS_ENCRYPTION_INLINE_CRYPT 416 int fscrypt_select_encryption_impl(struct fscrypt_inode_info *ci, 417 bool is_hw_wrapped_key); 418 419 static inline bool 420 fscrypt_using_inline_encryption(const struct fscrypt_inode_info *ci) 421 { 422 return ci->ci_inlinecrypt; 423 } 424 425 int fscrypt_prepare_inline_crypt_key(struct fscrypt_prepared_key *prep_key, 426 const u8 *key_bytes, size_t key_size, 427 bool is_hw_wrapped, 428 const struct fscrypt_inode_info *ci); 429 430 void fscrypt_destroy_inline_crypt_key(struct super_block *sb, 431 struct fscrypt_prepared_key *prep_key); 432 433 int fscrypt_derive_sw_secret(struct super_block *sb, 434 const u8 *wrapped_key, size_t wrapped_key_size, 435 u8 sw_secret[BLK_CRYPTO_SW_SECRET_SIZE]); 436 437 /* 438 * Check whether the crypto transform or blk-crypto key has been allocated in 439 * @prep_key, depending on which encryption implementation the file will use. 440 */ 441 static inline bool 442 fscrypt_is_key_prepared(struct fscrypt_prepared_key *prep_key, 443 const struct fscrypt_inode_info *ci) 444 { 445 /* 446 * The two smp_load_acquire()'s here pair with the smp_store_release()'s 447 * in fscrypt_prepare_inline_crypt_key() and fscrypt_prepare_key(). 448 * I.e., in some cases (namely, if this prep_key is a per-mode 449 * encryption key) another task can publish blk_key or tfm concurrently, 450 * executing a RELEASE barrier. We need to use smp_load_acquire() here 451 * to safely ACQUIRE the memory the other task published. 452 */ 453 if (fscrypt_using_inline_encryption(ci)) 454 return smp_load_acquire(&prep_key->blk_key) != NULL; 455 return smp_load_acquire(&prep_key->tfm) != NULL; 456 } 457 458 #else /* CONFIG_FS_ENCRYPTION_INLINE_CRYPT */ 459 460 static inline int fscrypt_select_encryption_impl(struct fscrypt_inode_info *ci, 461 bool is_hw_wrapped_key) 462 { 463 return 0; 464 } 465 466 static inline bool 467 fscrypt_using_inline_encryption(const struct fscrypt_inode_info *ci) 468 { 469 return false; 470 } 471 472 static inline int 473 fscrypt_prepare_inline_crypt_key(struct fscrypt_prepared_key *prep_key, 474 const u8 *key_bytes, size_t key_size, 475 bool is_hw_wrapped, 476 const struct fscrypt_inode_info *ci) 477 { 478 WARN_ON_ONCE(1); 479 return -EOPNOTSUPP; 480 } 481 482 static inline void 483 fscrypt_destroy_inline_crypt_key(struct super_block *sb, 484 struct fscrypt_prepared_key *prep_key) 485 { 486 } 487 488 static inline int 489 fscrypt_derive_sw_secret(struct super_block *sb, 490 const u8 *wrapped_key, size_t wrapped_key_size, 491 u8 sw_secret[BLK_CRYPTO_SW_SECRET_SIZE]) 492 { 493 fscrypt_warn(NULL, "kernel doesn't support hardware-wrapped keys"); 494 return -EOPNOTSUPP; 495 } 496 497 static inline bool 498 fscrypt_is_key_prepared(struct fscrypt_prepared_key *prep_key, 499 const struct fscrypt_inode_info *ci) 500 { 501 return smp_load_acquire(&prep_key->tfm) != NULL; 502 } 503 #endif /* !CONFIG_FS_ENCRYPTION_INLINE_CRYPT */ 504 505 /* keyring.c */ 506 507 /* 508 * fscrypt_master_key_secret - secret key material of an in-use master key 509 */ 510 struct fscrypt_master_key_secret { 511 512 /* 513 * The KDF with which subkeys of this key can be derived. 514 * 515 * For v1 policy keys, this isn't applicable and won't be set. 516 * Otherwise, this KDF will be keyed by this master key if 517 * ->is_hw_wrapped=false, or by the "software secret" that hardware 518 * derived from this master key if ->is_hw_wrapped=true. 519 */ 520 struct fscrypt_hkdf hkdf; 521 522 /* 523 * True if this key is a hardware-wrapped key; false if this key is a 524 * raw key (i.e. a "software key"). For v1 policy keys this will always 525 * be false, as v1 policy support is a legacy feature which doesn't 526 * support newer functionality such as hardware-wrapped keys. 527 */ 528 bool is_hw_wrapped; 529 530 /* 531 * Size of the key in bytes. This remains set even if ->bytes was 532 * zeroized due to no longer being needed. I.e. we still remember the 533 * size of the key even if we don't need to remember the key itself. 534 */ 535 u32 size; 536 537 /* 538 * The bytes of the key, when still needed. This can be either a raw 539 * key or a hardware-wrapped key, as indicated by ->is_hw_wrapped. In 540 * the case of a raw, v2 policy key, there is no need to remember the 541 * actual key separately from ->hkdf so this field will be zeroized as 542 * soon as ->hkdf is initialized. 543 */ 544 u8 bytes[FSCRYPT_MAX_ANY_KEY_SIZE]; 545 546 } __randomize_layout; 547 548 /* 549 * fscrypt_master_key - an in-use master key 550 * 551 * This represents a master encryption key which has been added to the 552 * filesystem. There are three high-level states that a key can be in: 553 * 554 * FSCRYPT_KEY_STATUS_PRESENT 555 * Key is fully usable; it can be used to unlock inodes that are encrypted 556 * with it (this includes being able to create new inodes). ->mk_present 557 * indicates whether the key is in this state. ->mk_secret exists, the key 558 * is in the keyring, and ->mk_active_refs > 0 due to ->mk_present. 559 * 560 * FSCRYPT_KEY_STATUS_INCOMPLETELY_REMOVED 561 * Removal of this key has been initiated, but some inodes that were 562 * unlocked with it are still in-use. Like ABSENT, ->mk_secret is wiped, 563 * and the key can no longer be used to unlock inodes. Unlike ABSENT, the 564 * key is still in the keyring; ->mk_decrypted_inodes is nonempty; and 565 * ->mk_active_refs > 0, being equal to the size of ->mk_decrypted_inodes. 566 * 567 * This state transitions to ABSENT if ->mk_decrypted_inodes becomes empty, 568 * or to PRESENT if FS_IOC_ADD_ENCRYPTION_KEY is called again for this key. 569 * 570 * FSCRYPT_KEY_STATUS_ABSENT 571 * Key is fully removed. The key is no longer in the keyring, 572 * ->mk_decrypted_inodes is empty, ->mk_active_refs == 0, ->mk_secret is 573 * wiped, and the key can no longer be used to unlock inodes. 574 */ 575 struct fscrypt_master_key { 576 577 /* 578 * Link in ->s_master_keys->key_hashtable. 579 * Only valid if ->mk_active_refs > 0. 580 */ 581 struct hlist_node mk_node; 582 583 /* Semaphore that protects ->mk_secret, ->mk_users, and ->mk_present */ 584 struct rw_semaphore mk_sem; 585 586 /* 587 * Active and structural reference counts. An active ref guarantees 588 * that the struct continues to exist, continues to be in the keyring 589 * ->s_master_keys, and that any embedded subkeys (e.g. 590 * ->mk_direct_keys) that have been prepared continue to exist. 591 * A structural ref only guarantees that the struct continues to exist. 592 * 593 * There is one active ref associated with ->mk_present being true, and 594 * one active ref for each inode in ->mk_decrypted_inodes. 595 * 596 * There is one structural ref associated with the active refcount being 597 * nonzero. Finding a key in the keyring also takes a structural ref, 598 * which is then held temporarily while the key is operated on. 599 */ 600 refcount_t mk_active_refs; 601 refcount_t mk_struct_refs; 602 603 struct rcu_head mk_rcu_head; 604 605 /* 606 * The secret key material. Wiped as soon as it is no longer needed; 607 * for details, see the fscrypt_master_key struct comment. 608 * 609 * Locking: protected by ->mk_sem. 610 */ 611 struct fscrypt_master_key_secret mk_secret; 612 613 /* 614 * For v1 policy keys: an arbitrary key descriptor which was assigned by 615 * userspace (->descriptor). 616 * 617 * For v2 policy keys: a cryptographic hash of this key (->identifier). 618 */ 619 struct fscrypt_key_specifier mk_spec; 620 621 /* 622 * Keyring which contains a key of type 'key_type_fscrypt_user' for each 623 * user who has added this key. Normally each key will be added by just 624 * one user, but it's possible that multiple users share a key, and in 625 * that case we need to keep track of those users so that one user can't 626 * remove the key before the others want it removed too. 627 * 628 * This is NULL for v1 policy keys; those can only be added by root. 629 * 630 * Locking: protected by ->mk_sem. (We don't just rely on the keyrings 631 * subsystem semaphore ->mk_users->sem, as we need support for atomic 632 * search+insert along with proper synchronization with other fields.) 633 */ 634 struct key *mk_users; 635 636 /* 637 * List of inodes that were unlocked using this key. This allows the 638 * inodes to be evicted efficiently if the key is removed. 639 */ 640 struct list_head mk_decrypted_inodes; 641 spinlock_t mk_decrypted_inodes_lock; 642 643 /* 644 * Per-mode encryption keys for the various types of encryption policies 645 * that use them. Allocated and derived on-demand. 646 */ 647 struct fscrypt_prepared_key mk_direct_keys[FSCRYPT_MODE_MAX + 1]; 648 struct fscrypt_prepared_key mk_iv_ino_lblk_64_keys[FSCRYPT_MODE_MAX + 1]; 649 struct fscrypt_prepared_key mk_iv_ino_lblk_32_keys[FSCRYPT_MODE_MAX + 1]; 650 651 /* Hash key for inode numbers. Initialized only when needed. */ 652 siphash_key_t mk_ino_hash_key; 653 bool mk_ino_hash_key_initialized; 654 655 /* 656 * Whether this key is in the "present" state, i.e. fully usable. For 657 * details, see the fscrypt_master_key struct comment. 658 * 659 * Locking: protected by ->mk_sem, but can be read locklessly using 660 * READ_ONCE(). Writers must use WRITE_ONCE() when concurrent readers 661 * are possible. 662 */ 663 bool mk_present; 664 665 } __randomize_layout; 666 667 static inline const char *master_key_spec_type( 668 const struct fscrypt_key_specifier *spec) 669 { 670 switch (spec->type) { 671 case FSCRYPT_KEY_SPEC_TYPE_DESCRIPTOR: 672 return "descriptor"; 673 case FSCRYPT_KEY_SPEC_TYPE_IDENTIFIER: 674 return "identifier"; 675 } 676 return "[unknown]"; 677 } 678 679 static inline int master_key_spec_len(const struct fscrypt_key_specifier *spec) 680 { 681 switch (spec->type) { 682 case FSCRYPT_KEY_SPEC_TYPE_DESCRIPTOR: 683 return FSCRYPT_KEY_DESCRIPTOR_SIZE; 684 case FSCRYPT_KEY_SPEC_TYPE_IDENTIFIER: 685 return FSCRYPT_KEY_IDENTIFIER_SIZE; 686 } 687 return 0; 688 } 689 690 void fscrypt_put_master_key(struct fscrypt_master_key *mk); 691 692 void fscrypt_put_master_key_activeref(struct super_block *sb, 693 struct fscrypt_master_key *mk); 694 695 struct fscrypt_master_key * 696 fscrypt_find_master_key(struct super_block *sb, 697 const struct fscrypt_key_specifier *mk_spec); 698 699 int fscrypt_get_test_dummy_key_identifier( 700 u8 key_identifier[FSCRYPT_KEY_IDENTIFIER_SIZE]); 701 702 int fscrypt_add_test_dummy_key(struct super_block *sb, 703 struct fscrypt_key_specifier *key_spec); 704 705 int fscrypt_verify_key_added(struct super_block *sb, 706 const u8 identifier[FSCRYPT_KEY_IDENTIFIER_SIZE]); 707 708 int __init fscrypt_init_keyring(void); 709 710 /* keysetup.c */ 711 712 struct fscrypt_mode { 713 const char *friendly_name; 714 const char *cipher_str; 715 int keysize; /* key size in bytes */ 716 int security_strength; /* security strength in bytes */ 717 int ivsize; /* IV size in bytes */ 718 int logged_cryptoapi_impl; 719 int logged_blk_crypto_native; 720 int logged_blk_crypto_fallback; 721 enum blk_crypto_mode_num blk_crypto_mode; 722 }; 723 724 extern struct fscrypt_mode fscrypt_modes[]; 725 726 int fscrypt_prepare_key(struct fscrypt_prepared_key *prep_key, 727 const u8 *raw_key, const struct fscrypt_inode_info *ci); 728 729 void fscrypt_destroy_prepared_key(struct super_block *sb, 730 struct fscrypt_prepared_key *prep_key); 731 732 int fscrypt_set_per_file_enc_key(struct fscrypt_inode_info *ci, 733 const u8 *raw_key); 734 735 int fscrypt_derive_dirhash_key(struct fscrypt_inode_info *ci, 736 const struct fscrypt_master_key *mk); 737 738 void fscrypt_hash_inode_number(struct fscrypt_inode_info *ci, 739 const struct fscrypt_master_key *mk); 740 741 int fscrypt_get_encryption_info(struct inode *inode, bool allow_unsupported); 742 743 /** 744 * fscrypt_require_key() - require an inode's encryption key 745 * @inode: the inode we need the key for 746 * 747 * If the inode is encrypted, set up its encryption key if not already done. 748 * Then require that the key be present and return -ENOKEY otherwise. 749 * 750 * No locks are needed, and the key will live as long as the struct inode --- so 751 * it won't go away from under you. 752 * 753 * Return: 0 on success, -ENOKEY if the key is missing, or another -errno code 754 * if a problem occurred while setting up the encryption key. 755 */ 756 static inline int fscrypt_require_key(struct inode *inode) 757 { 758 if (IS_ENCRYPTED(inode)) { 759 int err = fscrypt_get_encryption_info(inode, false); 760 761 if (err) 762 return err; 763 if (!fscrypt_has_encryption_key(inode)) 764 return -ENOKEY; 765 } 766 return 0; 767 } 768 769 /* keysetup_v1.c */ 770 771 void fscrypt_put_direct_key(struct fscrypt_direct_key *dk); 772 773 int fscrypt_setup_v1_file_key(struct fscrypt_inode_info *ci, 774 const u8 *raw_master_key); 775 776 int fscrypt_setup_v1_file_key_via_subscribed_keyrings( 777 struct fscrypt_inode_info *ci); 778 779 /* policy.c */ 780 781 bool fscrypt_policies_equal(const union fscrypt_policy *policy1, 782 const union fscrypt_policy *policy2); 783 int fscrypt_policy_to_key_spec(const union fscrypt_policy *policy, 784 struct fscrypt_key_specifier *key_spec); 785 const union fscrypt_policy *fscrypt_get_dummy_policy(struct super_block *sb); 786 bool fscrypt_supported_policy(const union fscrypt_policy *policy_u, 787 const struct inode *inode); 788 int fscrypt_policy_from_context(union fscrypt_policy *policy_u, 789 const union fscrypt_context *ctx_u, 790 int ctx_size); 791 const union fscrypt_policy *fscrypt_policy_to_inherit(struct inode *dir); 792 793 #endif /* _FSCRYPT_PRIVATE_H */ 794