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