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