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