hc
2024-05-10 37f49e37ab4cb5d0bc4c60eb5c6d4dd57db767bb
kernel/fs/crypto/fscrypt_private.h
....@@ -14,17 +14,21 @@
1414 #include <linux/fscrypt.h>
1515 #include <linux/siphash.h>
1616 #include <crypto/hash.h>
17
-#include <linux/bio-crypt-ctx.h>
17
+#include <linux/blk-crypto.h>
1818
1919 #define CONST_STRLEN(str) (sizeof(str) - 1)
2020
21
-#define FS_KEY_DERIVATION_NONCE_SIZE 16
21
+#define FSCRYPT_FILE_NONCE_SIZE 16
2222
23
-#define FSCRYPT_MIN_KEY_SIZE 16
23
+#define FSCRYPT_MIN_KEY_SIZE 16
24
+
2425 #define FSCRYPT_MAX_HW_WRAPPED_KEY_SIZE 128
2526
2627 #define FSCRYPT_CONTEXT_V1 1
2728 #define FSCRYPT_CONTEXT_V2 2
29
+
30
+/* Keep this in sync with include/uapi/linux/fscrypt.h */
31
+#define FSCRYPT_MODE_MAX FSCRYPT_MODE_ADIANTUM
2832
2933 struct fscrypt_context_v1 {
3034 u8 version; /* FSCRYPT_CONTEXT_V1 */
....@@ -32,7 +36,7 @@
3236 u8 filenames_encryption_mode;
3337 u8 flags;
3438 u8 master_key_descriptor[FSCRYPT_KEY_DESCRIPTOR_SIZE];
35
- u8 nonce[FS_KEY_DERIVATION_NONCE_SIZE];
39
+ u8 nonce[FSCRYPT_FILE_NONCE_SIZE];
3640 };
3741
3842 struct fscrypt_context_v2 {
....@@ -42,7 +46,7 @@
4246 u8 flags;
4347 u8 __reserved[4];
4448 u8 master_key_identifier[FSCRYPT_KEY_IDENTIFIER_SIZE];
45
- u8 nonce[FS_KEY_DERIVATION_NONCE_SIZE];
49
+ u8 nonce[FSCRYPT_FILE_NONCE_SIZE];
4650 };
4751
4852 /*
....@@ -98,7 +102,6 @@
98102 return NULL;
99103 }
100104
101
-#undef fscrypt_policy
102105 union fscrypt_policy {
103106 u8 version;
104107 struct fscrypt_policy_v1 v1;
....@@ -192,9 +195,9 @@
192195 struct fscrypt_info {
193196
194197 /* The key in a form prepared for actual encryption/decryption */
195
- struct fscrypt_prepared_key ci_key;
198
+ struct fscrypt_prepared_key ci_enc_key;
196199
197
- /* True if the key should be freed when this fscrypt_info is freed */
200
+ /* True if ci_enc_key should be freed when this fscrypt_info is freed */
198201 bool ci_owns_key;
199202
200203 #ifdef CONFIG_FS_ENCRYPTION_INLINE_CRYPT
....@@ -219,7 +222,16 @@
219222 * will be NULL if the master key was found in a process-subscribed
220223 * keyring rather than in the filesystem-level keyring.
221224 */
225
+#ifdef __GENKSYMS__
226
+ /*
227
+ * Android ABI CRC preservation due to commit 391cceee6d43 ("fscrypt:
228
+ * stop using keyrings subsystem for fscrypt_master_key") changing this
229
+ * type. Size is the same, this is a private field.
230
+ */
222231 struct key *ci_master_key;
232
+#else
233
+ struct fscrypt_master_key *ci_master_key;
234
+#endif
223235
224236 /*
225237 * Link in list of inodes that were unlocked with the master key.
....@@ -229,7 +241,7 @@
229241
230242 /*
231243 * If non-NULL, then encryption is done using the master key directly
232
- * and ci_key will equal ci_direct_key->dk_key.
244
+ * and ci_enc_key will equal ci_direct_key->dk_key.
233245 */
234246 struct fscrypt_direct_key *ci_direct_key;
235247
....@@ -245,7 +257,7 @@
245257 union fscrypt_policy ci_policy;
246258
247259 /* This inode's nonce, copied from the fscrypt_context */
248
- u8 ci_nonce[FS_KEY_DERIVATION_NONCE_SIZE];
260
+ u8 ci_nonce[FSCRYPT_FILE_NONCE_SIZE];
249261
250262 /* Hashed inode number. Only set for IV_INO_LBLK_32 */
251263 u32 ci_hashed_ino;
....@@ -281,7 +293,7 @@
281293 __le64 lblk_num;
282294
283295 /* per-file nonce; only set in DIRECT_KEY mode */
284
- u8 nonce[FS_KEY_DERIVATION_NONCE_SIZE];
296
+ u8 nonce[FSCRYPT_FILE_NONCE_SIZE];
285297 };
286298 u8 raw[FSCRYPT_MAX_IV_SIZE];
287299 __le64 dun[FSCRYPT_MAX_IV_SIZE / sizeof(__le64)];
....@@ -293,8 +305,9 @@
293305 /* fname.c */
294306 int fscrypt_fname_encrypt(const struct inode *inode, const struct qstr *iname,
295307 u8 *out, unsigned int olen);
296
-bool fscrypt_fname_encrypted_size(const struct inode *inode, u32 orig_len,
297
- u32 max_len, u32 *encrypted_len_ret);
308
+bool fscrypt_fname_encrypted_size(const union fscrypt_policy *policy,
309
+ u32 orig_len, u32 max_len,
310
+ u32 *encrypted_len_ret);
298311
299312 /* hkdf.c */
300313
....@@ -312,13 +325,13 @@
312325 * outputs are unique and cryptographically isolated, i.e. knowledge of one
313326 * output doesn't reveal another.
314327 */
315
-#define HKDF_CONTEXT_KEY_IDENTIFIER 1
316
-#define HKDF_CONTEXT_PER_FILE_ENC_KEY 2
317
-#define HKDF_CONTEXT_DIRECT_KEY 3
318
-#define HKDF_CONTEXT_IV_INO_LBLK_64_KEY 4
319
-#define HKDF_CONTEXT_DIRHASH_KEY 5
320
-#define HKDF_CONTEXT_IV_INO_LBLK_32_KEY 6
321
-#define HKDF_CONTEXT_INODE_HASH_KEY 7
328
+#define HKDF_CONTEXT_KEY_IDENTIFIER 1 /* info=<empty> */
329
+#define HKDF_CONTEXT_PER_FILE_ENC_KEY 2 /* info=file_nonce */
330
+#define HKDF_CONTEXT_DIRECT_KEY 3 /* info=mode_num */
331
+#define HKDF_CONTEXT_IV_INO_LBLK_64_KEY 4 /* info=mode_num||fs_uuid */
332
+#define HKDF_CONTEXT_DIRHASH_KEY 5 /* info=file_nonce */
333
+#define HKDF_CONTEXT_IV_INO_LBLK_32_KEY 6 /* info=mode_num||fs_uuid */
334
+#define HKDF_CONTEXT_INODE_HASH_KEY 7 /* info=<empty> */
322335
323336 int fscrypt_hkdf_expand(const struct fscrypt_hkdf *hkdf, u8 context,
324337 const u8 *info, unsigned int infolen,
....@@ -328,8 +341,8 @@
328341
329342 /* inline_crypt.c */
330343 #ifdef CONFIG_FS_ENCRYPTION_INLINE_CRYPT
331
-extern int fscrypt_select_encryption_impl(struct fscrypt_info *ci,
332
- bool is_hw_wrapped_key);
344
+int fscrypt_select_encryption_impl(struct fscrypt_info *ci,
345
+ bool is_hw_wrapped_key);
333346
334347 static inline bool
335348 fscrypt_using_inline_encryption(const struct fscrypt_info *ci)
....@@ -337,15 +350,13 @@
337350 return ci->ci_inlinecrypt;
338351 }
339352
340
-extern int fscrypt_prepare_inline_crypt_key(
341
- struct fscrypt_prepared_key *prep_key,
342
- const u8 *raw_key,
343
- unsigned int raw_key_size,
344
- bool is_hw_wrapped,
345
- const struct fscrypt_info *ci);
353
+int fscrypt_prepare_inline_crypt_key(struct fscrypt_prepared_key *prep_key,
354
+ const u8 *raw_key,
355
+ unsigned int raw_key_size,
356
+ bool is_hw_wrapped,
357
+ const struct fscrypt_info *ci);
346358
347
-extern void fscrypt_destroy_inline_crypt_key(
348
- struct fscrypt_prepared_key *prep_key);
359
+void fscrypt_destroy_inline_crypt_key(struct fscrypt_prepared_key *prep_key);
349360
350361 extern int fscrypt_derive_raw_secret(struct super_block *sb,
351362 const u8 *wrapped_key,
....@@ -382,8 +393,8 @@
382393 return 0;
383394 }
384395
385
-static inline bool fscrypt_using_inline_encryption(
386
- const struct fscrypt_info *ci)
396
+static inline bool
397
+fscrypt_using_inline_encryption(const struct fscrypt_info *ci)
387398 {
388399 return false;
389400 }
....@@ -460,6 +471,40 @@
460471 struct fscrypt_master_key {
461472
462473 /*
474
+ * Back-pointer to the super_block of the filesystem to which this
475
+ * master key has been added. Only valid if ->mk_active_refs > 0.
476
+ */
477
+ struct super_block *mk_sb;
478
+
479
+ /*
480
+ * Link in ->mk_sb->s_master_keys->key_hashtable.
481
+ * Only valid if ->mk_active_refs > 0.
482
+ */
483
+ struct hlist_node mk_node;
484
+
485
+ /* Semaphore that protects ->mk_secret and ->mk_users */
486
+ struct rw_semaphore mk_sem;
487
+
488
+ /*
489
+ * Active and structural reference counts. An active ref guarantees
490
+ * that the struct continues to exist, continues to be in the keyring
491
+ * ->mk_sb->s_master_keys, and that any embedded subkeys (e.g.
492
+ * ->mk_direct_keys) that have been prepared continue to exist.
493
+ * A structural ref only guarantees that the struct continues to exist.
494
+ *
495
+ * There is one active ref associated with ->mk_secret being present,
496
+ * and one active ref for each inode in ->mk_decrypted_inodes.
497
+ *
498
+ * There is one structural ref associated with the active refcount being
499
+ * nonzero. Finding a key in the keyring also takes a structural ref,
500
+ * which is then held temporarily while the key is operated on.
501
+ */
502
+ refcount_t mk_active_refs;
503
+ refcount_t mk_struct_refs;
504
+
505
+ struct rcu_head mk_rcu_head;
506
+
507
+ /*
463508 * The secret key material. After FS_IOC_REMOVE_ENCRYPTION_KEY is
464509 * executed, this is wiped and no new inodes can be unlocked with this
465510 * key; however, there may still be inodes in ->mk_decrypted_inodes
....@@ -467,16 +512,12 @@
467512 * FS_IOC_REMOVE_ENCRYPTION_KEY can be retried, or
468513 * FS_IOC_ADD_ENCRYPTION_KEY can add the secret again.
469514 *
470
- * Locking: protected by key->sem (outer) and mk_secret_sem (inner).
471
- * The reason for two locks is that key->sem also protects modifying
472
- * mk_users, which ranks it above the semaphore for the keyring key
473
- * type, which is in turn above page faults (via keyring_read). But
474
- * sometimes filesystems call fscrypt_get_encryption_info() from within
475
- * a transaction, which ranks it below page faults. So we need a
476
- * separate lock which protects mk_secret but not also mk_users.
515
+ * While ->mk_secret is present, one ref in ->mk_active_refs is held.
516
+ *
517
+ * Locking: protected by ->mk_sem. The manipulation of ->mk_active_refs
518
+ * associated with this field is protected by ->mk_sem as well.
477519 */
478520 struct fscrypt_master_key_secret mk_secret;
479
- struct rw_semaphore mk_secret_sem;
480521
481522 /*
482523 * For v1 policy keys: an arbitrary key descriptor which was assigned by
....@@ -495,21 +536,11 @@
495536 *
496537 * This is NULL for v1 policy keys; those can only be added by root.
497538 *
498
- * Locking: in addition to this keyrings own semaphore, this is
499
- * protected by the master key's key->sem, so we can do atomic
500
- * search+insert. It can also be searched without taking any locks, but
501
- * in that case the returned key may have already been removed.
539
+ * Locking: protected by ->mk_sem. (We don't just rely on the keyrings
540
+ * subsystem semaphore ->mk_users->sem, as we need support for atomic
541
+ * search+insert along with proper synchronization with ->mk_secret.)
502542 */
503543 struct key *mk_users;
504
-
505
- /*
506
- * Length of ->mk_decrypted_inodes, plus one if mk_secret is present.
507
- * Once this goes to 0, the master key is removed from ->s_master_keys.
508
- * The 'struct fscrypt_master_key' will continue to live as long as the
509
- * 'struct key' whose payload it is, but we won't let this reference
510
- * count rise again.
511
- */
512
- refcount_t mk_refcount;
513544
514545 /*
515546 * List of inodes that were unlocked using this key. This allows the
....@@ -522,9 +553,9 @@
522553 * Per-mode encryption keys for the various types of encryption policies
523554 * that use them. Allocated and derived on-demand.
524555 */
525
- struct fscrypt_prepared_key mk_direct_keys[__FSCRYPT_MODE_MAX + 1];
526
- struct fscrypt_prepared_key mk_iv_ino_lblk_64_keys[__FSCRYPT_MODE_MAX + 1];
527
- struct fscrypt_prepared_key mk_iv_ino_lblk_32_keys[__FSCRYPT_MODE_MAX + 1];
556
+ struct fscrypt_prepared_key mk_direct_keys[FSCRYPT_MODE_MAX + 1];
557
+ struct fscrypt_prepared_key mk_iv_ino_lblk_64_keys[FSCRYPT_MODE_MAX + 1];
558
+ struct fscrypt_prepared_key mk_iv_ino_lblk_32_keys[FSCRYPT_MODE_MAX + 1];
528559
529560 /* Hash key for inode numbers. Initialized only when needed. */
530561 siphash_key_t mk_ino_hash_key;
....@@ -536,11 +567,11 @@
536567 is_master_key_secret_present(const struct fscrypt_master_key_secret *secret)
537568 {
538569 /*
539
- * The READ_ONCE() is only necessary for fscrypt_drop_inode() and
540
- * fscrypt_key_describe(). These run in atomic context, so they can't
541
- * take ->mk_secret_sem and thus 'secret' can change concurrently which
542
- * would be a data race. But they only need to know whether the secret
543
- * *was* present at the time of check, so READ_ONCE() suffices.
570
+ * The READ_ONCE() is only necessary for fscrypt_drop_inode().
571
+ * fscrypt_drop_inode() runs in atomic context, so it can't take the key
572
+ * semaphore and thus 'secret' can change concurrently which would be a
573
+ * data race. But fscrypt_drop_inode() only need to know whether the
574
+ * secret *was* present at the time of check, so READ_ONCE() suffices.
544575 */
545576 return READ_ONCE(secret->size) != 0;
546577 }
....@@ -568,7 +599,11 @@
568599 return 0;
569600 }
570601
571
-struct key *
602
+void fscrypt_put_master_key(struct fscrypt_master_key *mk);
603
+
604
+void fscrypt_put_master_key_activeref(struct fscrypt_master_key *mk);
605
+
606
+struct fscrypt_master_key *
572607 fscrypt_find_master_key(struct super_block *sb,
573608 const struct fscrypt_key_specifier *mk_spec);
574609
....@@ -585,8 +620,9 @@
585620 struct fscrypt_mode {
586621 const char *friendly_name;
587622 const char *cipher_str;
588
- int keysize;
589
- int ivsize;
623
+ int keysize; /* key size in bytes */
624
+ int security_strength; /* security strength in bytes */
625
+ int ivsize; /* IV size in bytes */
590626 int logged_impl_name;
591627 enum blk_crypto_mode_num blk_crypto_mode;
592628 };
....@@ -603,6 +639,37 @@
603639
604640 int fscrypt_derive_dirhash_key(struct fscrypt_info *ci,
605641 const struct fscrypt_master_key *mk);
642
+
643
+void fscrypt_hash_inode_number(struct fscrypt_info *ci,
644
+ const struct fscrypt_master_key *mk);
645
+
646
+int fscrypt_get_encryption_info(struct inode *inode, bool allow_unsupported);
647
+
648
+/**
649
+ * fscrypt_require_key() - require an inode's encryption key
650
+ * @inode: the inode we need the key for
651
+ *
652
+ * If the inode is encrypted, set up its encryption key if not already done.
653
+ * Then require that the key be present and return -ENOKEY otherwise.
654
+ *
655
+ * No locks are needed, and the key will live as long as the struct inode --- so
656
+ * it won't go away from under you.
657
+ *
658
+ * Return: 0 on success, -ENOKEY if the key is missing, or another -errno code
659
+ * if a problem occurred while setting up the encryption key.
660
+ */
661
+static inline int fscrypt_require_key(struct inode *inode)
662
+{
663
+ if (IS_ENCRYPTED(inode)) {
664
+ int err = fscrypt_get_encryption_info(inode, false);
665
+
666
+ if (err)
667
+ return err;
668
+ if (!fscrypt_has_encryption_key(inode))
669
+ return -ENOKEY;
670
+ }
671
+ return 0;
672
+}
606673
607674 /* keysetup_v1.c */
608675
....@@ -622,5 +689,6 @@
622689 int fscrypt_policy_from_context(union fscrypt_policy *policy_u,
623690 const union fscrypt_context *ctx_u,
624691 int ctx_size);
692
+const union fscrypt_policy *fscrypt_policy_to_inherit(struct inode *dir);
625693
626694 #endif /* _FSCRYPT_PRIVATE_H */