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