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