• 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[];
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_inode_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_inode_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 struct 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_inode_info_cachep;
319 int fscrypt_initialize(struct super_block *sb);
320 int fscrypt_crypt_data_unit(const struct fscrypt_inode_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_inode_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_inode_info *ci,
399 				   bool is_hw_wrapped_key);
400 
401 static inline bool
fscrypt_using_inline_encryption(const struct fscrypt_inode_info * ci)402 fscrypt_using_inline_encryption(const struct fscrypt_inode_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_inode_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_inode_info * ci)424 fscrypt_is_key_prepared(struct fscrypt_prepared_key *prep_key,
425 			const struct fscrypt_inode_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_inode_info * ci,bool is_hw_wrapped_key)442 static inline int fscrypt_select_encryption_impl(struct fscrypt_inode_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_inode_info * ci)449 fscrypt_using_inline_encryption(const struct fscrypt_inode_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_inode_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_inode_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_inode_info * ci)480 fscrypt_is_key_prepared(struct fscrypt_prepared_key *prep_key,
481 			const struct fscrypt_inode_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.  There are three high-level states that a key can be in:
535  *
536  * FSCRYPT_KEY_STATUS_PRESENT
537  *	Key is fully usable; it can be used to unlock inodes that are encrypted
538  *	with it (this includes being able to create new inodes).  ->mk_present
539  *	indicates whether the key is in this state.  ->mk_secret exists, the key
540  *	is in the keyring, and ->mk_active_refs > 0 due to ->mk_present.
541  *
542  * FSCRYPT_KEY_STATUS_INCOMPLETELY_REMOVED
543  *	Removal of this key has been initiated, but some inodes that were
544  *	unlocked with it are still in-use.  Like ABSENT, ->mk_secret is wiped,
545  *	and the key can no longer be used to unlock inodes.  Unlike ABSENT, the
546  *	key is still in the keyring; ->mk_decrypted_inodes is nonempty; and
547  *	->mk_active_refs > 0, being equal to the size of ->mk_decrypted_inodes.
548  *
549  *	This state transitions to ABSENT if ->mk_decrypted_inodes becomes empty,
550  *	or to PRESENT if FS_IOC_ADD_ENCRYPTION_KEY is called again for this key.
551  *
552  * FSCRYPT_KEY_STATUS_ABSENT
553  *	Key is fully removed.  The key is no longer in the keyring,
554  *	->mk_decrypted_inodes is empty, ->mk_active_refs == 0, ->mk_secret is
555  *	wiped, and the key can no longer be used to unlock inodes.
556  */
557 struct fscrypt_master_key {
558 
559 	/*
560 	 * Link in ->s_master_keys->key_hashtable.
561 	 * Only valid if ->mk_active_refs > 0.
562 	 */
563 	struct hlist_node			mk_node;
564 
565 	/* Semaphore that protects ->mk_secret, ->mk_users, and ->mk_present */
566 	struct rw_semaphore			mk_sem;
567 
568 	/*
569 	 * Active and structural reference counts.  An active ref guarantees
570 	 * that the struct continues to exist, continues to be in the keyring
571 	 * ->s_master_keys, and that any embedded subkeys (e.g.
572 	 * ->mk_direct_keys) that have been prepared continue to exist.
573 	 * A structural ref only guarantees that the struct continues to exist.
574 	 *
575 	 * There is one active ref associated with ->mk_present being true, and
576 	 * one active ref for each inode in ->mk_decrypted_inodes.
577 	 *
578 	 * There is one structural ref associated with the active refcount being
579 	 * nonzero.  Finding a key in the keyring also takes a structural ref,
580 	 * which is then held temporarily while the key is operated on.
581 	 */
582 	refcount_t				mk_active_refs;
583 	refcount_t				mk_struct_refs;
584 
585 	struct rcu_head				mk_rcu_head;
586 
587 	/*
588 	 * The secret key material.  Wiped as soon as it is no longer needed;
589 	 * for details, see the fscrypt_master_key struct comment.
590 	 *
591 	 * Locking: protected by ->mk_sem.
592 	 */
593 	struct fscrypt_master_key_secret	mk_secret;
594 
595 	/*
596 	 * For v1 policy keys: an arbitrary key descriptor which was assigned by
597 	 * userspace (->descriptor).
598 	 *
599 	 * For v2 policy keys: a cryptographic hash of this key (->identifier).
600 	 */
601 	struct fscrypt_key_specifier		mk_spec;
602 
603 	/*
604 	 * Keyring which contains a key of type 'key_type_fscrypt_user' for each
605 	 * user who has added this key.  Normally each key will be added by just
606 	 * one user, but it's possible that multiple users share a key, and in
607 	 * that case we need to keep track of those users so that one user can't
608 	 * remove the key before the others want it removed too.
609 	 *
610 	 * This is NULL for v1 policy keys; those can only be added by root.
611 	 *
612 	 * Locking: protected by ->mk_sem.  (We don't just rely on the keyrings
613 	 * subsystem semaphore ->mk_users->sem, as we need support for atomic
614 	 * search+insert along with proper synchronization with other fields.)
615 	 */
616 	struct key		*mk_users;
617 
618 	/*
619 	 * List of inodes that were unlocked using this key.  This allows the
620 	 * inodes to be evicted efficiently if the key is removed.
621 	 */
622 	struct list_head	mk_decrypted_inodes;
623 	spinlock_t		mk_decrypted_inodes_lock;
624 
625 	/*
626 	 * Per-mode encryption keys for the various types of encryption policies
627 	 * that use them.  Allocated and derived on-demand.
628 	 */
629 	struct fscrypt_prepared_key mk_direct_keys[FSCRYPT_MODE_MAX + 1];
630 	struct fscrypt_prepared_key mk_iv_ino_lblk_64_keys[FSCRYPT_MODE_MAX + 1];
631 	struct fscrypt_prepared_key mk_iv_ino_lblk_32_keys[FSCRYPT_MODE_MAX + 1];
632 
633 	/* Hash key for inode numbers.  Initialized only when needed. */
634 	siphash_key_t		mk_ino_hash_key;
635 	bool			mk_ino_hash_key_initialized;
636 
637 	/*
638 	 * Whether this key is in the "present" state, i.e. fully usable.  For
639 	 * details, see the fscrypt_master_key struct comment.
640 	 *
641 	 * Locking: protected by ->mk_sem, but can be read locklessly using
642 	 * READ_ONCE().  Writers must use WRITE_ONCE() when concurrent readers
643 	 * are possible.
644 	 */
645 	bool			mk_present;
646 
647 } __randomize_layout;
648 
master_key_spec_type(const struct fscrypt_key_specifier * spec)649 static inline const char *master_key_spec_type(
650 				const struct fscrypt_key_specifier *spec)
651 {
652 	switch (spec->type) {
653 	case FSCRYPT_KEY_SPEC_TYPE_DESCRIPTOR:
654 		return "descriptor";
655 	case FSCRYPT_KEY_SPEC_TYPE_IDENTIFIER:
656 		return "identifier";
657 	}
658 	return "[unknown]";
659 }
660 
master_key_spec_len(const struct fscrypt_key_specifier * spec)661 static inline int master_key_spec_len(const struct fscrypt_key_specifier *spec)
662 {
663 	switch (spec->type) {
664 	case FSCRYPT_KEY_SPEC_TYPE_DESCRIPTOR:
665 		return FSCRYPT_KEY_DESCRIPTOR_SIZE;
666 	case FSCRYPT_KEY_SPEC_TYPE_IDENTIFIER:
667 		return FSCRYPT_KEY_IDENTIFIER_SIZE;
668 	}
669 	return 0;
670 }
671 
672 void fscrypt_put_master_key(struct fscrypt_master_key *mk);
673 
674 void fscrypt_put_master_key_activeref(struct super_block *sb,
675 				      struct fscrypt_master_key *mk);
676 
677 struct fscrypt_master_key *
678 fscrypt_find_master_key(struct super_block *sb,
679 			const struct fscrypt_key_specifier *mk_spec);
680 
681 int fscrypt_get_test_dummy_key_identifier(
682 			  u8 key_identifier[FSCRYPT_KEY_IDENTIFIER_SIZE]);
683 
684 int fscrypt_add_test_dummy_key(struct super_block *sb,
685 			       struct fscrypt_key_specifier *key_spec);
686 
687 int fscrypt_verify_key_added(struct super_block *sb,
688 			     const u8 identifier[FSCRYPT_KEY_IDENTIFIER_SIZE]);
689 
690 int __init fscrypt_init_keyring(void);
691 
692 /* keysetup.c */
693 
694 struct fscrypt_mode {
695 	const char *friendly_name;
696 	const char *cipher_str;
697 	int keysize;		/* key size in bytes */
698 	int security_strength;	/* security strength in bytes */
699 	int ivsize;		/* IV size in bytes */
700 	int logged_cryptoapi_impl;
701 	int logged_blk_crypto_native;
702 	int logged_blk_crypto_fallback;
703 	enum blk_crypto_mode_num blk_crypto_mode;
704 };
705 
706 extern struct fscrypt_mode fscrypt_modes[];
707 
708 int fscrypt_prepare_key(struct fscrypt_prepared_key *prep_key,
709 			const u8 *raw_key, const struct fscrypt_inode_info *ci);
710 
711 void fscrypt_destroy_prepared_key(struct super_block *sb,
712 				  struct fscrypt_prepared_key *prep_key);
713 
714 int fscrypt_set_per_file_enc_key(struct fscrypt_inode_info *ci,
715 				 const u8 *raw_key);
716 
717 int fscrypt_derive_dirhash_key(struct fscrypt_inode_info *ci,
718 			       const struct fscrypt_master_key *mk);
719 
720 void fscrypt_hash_inode_number(struct fscrypt_inode_info *ci,
721 			       const struct fscrypt_master_key *mk);
722 
723 int fscrypt_get_encryption_info(struct inode *inode, bool allow_unsupported);
724 
725 /**
726  * fscrypt_require_key() - require an inode's encryption key
727  * @inode: the inode we need the key for
728  *
729  * If the inode is encrypted, set up its encryption key if not already done.
730  * Then require that the key be present and return -ENOKEY otherwise.
731  *
732  * No locks are needed, and the key will live as long as the struct inode --- so
733  * it won't go away from under you.
734  *
735  * Return: 0 on success, -ENOKEY if the key is missing, or another -errno code
736  * if a problem occurred while setting up the encryption key.
737  */
fscrypt_require_key(struct inode * inode)738 static inline int fscrypt_require_key(struct inode *inode)
739 {
740 	if (IS_ENCRYPTED(inode)) {
741 		int err = fscrypt_get_encryption_info(inode, false);
742 
743 		if (err)
744 			return err;
745 		if (!fscrypt_has_encryption_key(inode))
746 			return -ENOKEY;
747 	}
748 	return 0;
749 }
750 
751 /* keysetup_v1.c */
752 
753 void fscrypt_put_direct_key(struct fscrypt_direct_key *dk);
754 
755 int fscrypt_setup_v1_file_key(struct fscrypt_inode_info *ci,
756 			      const u8 *raw_master_key);
757 
758 int fscrypt_setup_v1_file_key_via_subscribed_keyrings(
759 				struct fscrypt_inode_info *ci);
760 
761 /* policy.c */
762 
763 bool fscrypt_policies_equal(const union fscrypt_policy *policy1,
764 			    const union fscrypt_policy *policy2);
765 int fscrypt_policy_to_key_spec(const union fscrypt_policy *policy,
766 			       struct fscrypt_key_specifier *key_spec);
767 const union fscrypt_policy *fscrypt_get_dummy_policy(struct super_block *sb);
768 bool fscrypt_supported_policy(const union fscrypt_policy *policy_u,
769 			      const struct inode *inode);
770 int fscrypt_policy_from_context(union fscrypt_policy *policy_u,
771 				const union fscrypt_context *ctx_u,
772 				int ctx_size);
773 const union fscrypt_policy *fscrypt_policy_to_inherit(struct inode *dir);
774 
775 #endif /* _FSCRYPT_PRIVATE_H */
776