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