blob: a455e29e807e9bf78368d45cfc47a8aea6e0fa81 [file] [log] [blame]
b.liue9582032025-04-17 19:18:16 +08001/* 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
33struct 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
42struct 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 */
62union 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 */
72static 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 */
86static 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 */
93static 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
105union 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 */
115static 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 */
127static inline u8
128fscrypt_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 */
140static inline u8
141fscrypt_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 */
153static inline u8
154fscrypt_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 */
169struct 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 */
181struct 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 */
195struct 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 struct key *ci_master_key;
226
227 /*
228 * Link in list of inodes that were unlocked with the master key.
229 * Only used when ->ci_master_key is set.
230 */
231 struct list_head ci_master_key_link;
232
233 /*
234 * If non-NULL, then encryption is done using the master key directly
235 * and ci_enc_key will equal ci_direct_key->dk_key.
236 */
237 struct fscrypt_direct_key *ci_direct_key;
238
239 /*
240 * This inode's hash key for filenames. This is a 128-bit SipHash-2-4
241 * key. This is only set for directories that use a keyed dirhash over
242 * the plaintext filenames -- currently just casefolded directories.
243 */
244 siphash_key_t ci_dirhash_key;
245 bool ci_dirhash_key_initialized;
246
247 /* The encryption policy used by this inode */
248 union fscrypt_policy ci_policy;
249
250 /* This inode's nonce, copied from the fscrypt_context */
251 u8 ci_nonce[FSCRYPT_FILE_NONCE_SIZE];
252
253 /* Hashed inode number. Only set for IV_INO_LBLK_32 */
254 u32 ci_hashed_ino;
255};
256
257typedef enum {
258 FS_DECRYPT = 0,
259 FS_ENCRYPT,
260} fscrypt_direction_t;
261
262/* crypto.c */
263extern struct kmem_cache *fscrypt_info_cachep;
264int fscrypt_initialize(unsigned int cop_flags);
265int fscrypt_crypt_block(const struct inode *inode, fscrypt_direction_t rw,
266 u64 lblk_num, struct page *src_page,
267 struct page *dest_page, unsigned int len,
268 unsigned int offs, gfp_t gfp_flags);
269struct page *fscrypt_alloc_bounce_page(gfp_t gfp_flags);
270
271void __printf(3, 4) __cold
272fscrypt_msg(const struct inode *inode, const char *level, const char *fmt, ...);
273
274#define fscrypt_warn(inode, fmt, ...) \
275 fscrypt_msg((inode), KERN_WARNING, fmt, ##__VA_ARGS__)
276#define fscrypt_err(inode, fmt, ...) \
277 fscrypt_msg((inode), KERN_ERR, fmt, ##__VA_ARGS__)
278
279#define FSCRYPT_MAX_IV_SIZE 32
280
281union fscrypt_iv {
282 struct {
283 /* logical block number within the file */
284 __le64 lblk_num;
285
286 /* per-file nonce; only set in DIRECT_KEY mode */
287 u8 nonce[FSCRYPT_FILE_NONCE_SIZE];
288 };
289 u8 raw[FSCRYPT_MAX_IV_SIZE];
290 __le64 dun[FSCRYPT_MAX_IV_SIZE / sizeof(__le64)];
291};
292
293void fscrypt_generate_iv(union fscrypt_iv *iv, u64 lblk_num,
294 const struct fscrypt_info *ci);
295
296/* fname.c */
297int fscrypt_fname_encrypt(const struct inode *inode, const struct qstr *iname,
298 u8 *out, unsigned int olen);
299bool fscrypt_fname_encrypted_size(const union fscrypt_policy *policy,
300 u32 orig_len, u32 max_len,
301 u32 *encrypted_len_ret);
302
303/* hkdf.c */
304
305struct fscrypt_hkdf {
306 struct crypto_shash *hmac_tfm;
307};
308
309int fscrypt_init_hkdf(struct fscrypt_hkdf *hkdf, const u8 *master_key,
310 unsigned int master_key_size);
311
312/*
313 * The list of contexts in which fscrypt uses HKDF. These values are used as
314 * the first byte of the HKDF application-specific info string to guarantee that
315 * info strings are never repeated between contexts. This ensures that all HKDF
316 * outputs are unique and cryptographically isolated, i.e. knowledge of one
317 * output doesn't reveal another.
318 */
319#define HKDF_CONTEXT_KEY_IDENTIFIER 1 /* info=<empty> */
320#define HKDF_CONTEXT_PER_FILE_ENC_KEY 2 /* info=file_nonce */
321#define HKDF_CONTEXT_DIRECT_KEY 3 /* info=mode_num */
322#define HKDF_CONTEXT_IV_INO_LBLK_64_KEY 4 /* info=mode_num||fs_uuid */
323#define HKDF_CONTEXT_DIRHASH_KEY 5 /* info=file_nonce */
324#define HKDF_CONTEXT_IV_INO_LBLK_32_KEY 6 /* info=mode_num||fs_uuid */
325#define HKDF_CONTEXT_INODE_HASH_KEY 7 /* info=<empty> */
326
327int fscrypt_hkdf_expand(const struct fscrypt_hkdf *hkdf, u8 context,
328 const u8 *info, unsigned int infolen,
329 u8 *okm, unsigned int okmlen);
330
331void fscrypt_destroy_hkdf(struct fscrypt_hkdf *hkdf);
332
333/* inline_crypt.c */
334#ifdef CONFIG_FS_ENCRYPTION_INLINE_CRYPT
335int fscrypt_select_encryption_impl(struct fscrypt_info *ci,
336 bool is_hw_wrapped_key);
337
338static inline bool
339fscrypt_using_inline_encryption(const struct fscrypt_info *ci)
340{
341 return ci->ci_inlinecrypt;
342}
343
344int fscrypt_prepare_inline_crypt_key(struct fscrypt_prepared_key *prep_key,
345 const u8 *raw_key,
346 unsigned int raw_key_size,
347 bool is_hw_wrapped,
348 const struct fscrypt_info *ci);
349
350void fscrypt_destroy_inline_crypt_key(struct fscrypt_prepared_key *prep_key);
351
352extern int fscrypt_derive_raw_secret(struct super_block *sb,
353 const u8 *wrapped_key,
354 unsigned int wrapped_key_size,
355 u8 *raw_secret,
356 unsigned int raw_secret_size);
357
358/*
359 * Check whether the crypto transform or blk-crypto key has been allocated in
360 * @prep_key, depending on which encryption implementation the file will use.
361 */
362static inline bool
363fscrypt_is_key_prepared(struct fscrypt_prepared_key *prep_key,
364 const struct fscrypt_info *ci)
365{
366 /*
367 * The two smp_load_acquire()'s here pair with the smp_store_release()'s
368 * in fscrypt_prepare_inline_crypt_key() and fscrypt_prepare_key().
369 * I.e., in some cases (namely, if this prep_key is a per-mode
370 * encryption key) another task can publish blk_key or tfm concurrently,
371 * executing a RELEASE barrier. We need to use smp_load_acquire() here
372 * to safely ACQUIRE the memory the other task published.
373 */
374 if (fscrypt_using_inline_encryption(ci))
375 return smp_load_acquire(&prep_key->blk_key) != NULL;
376 return smp_load_acquire(&prep_key->tfm) != NULL;
377}
378
379#else /* CONFIG_FS_ENCRYPTION_INLINE_CRYPT */
380
381static inline int fscrypt_select_encryption_impl(struct fscrypt_info *ci,
382 bool is_hw_wrapped_key)
383{
384 return 0;
385}
386
387static inline bool
388fscrypt_using_inline_encryption(const struct fscrypt_info *ci)
389{
390 return false;
391}
392
393static inline int
394fscrypt_prepare_inline_crypt_key(struct fscrypt_prepared_key *prep_key,
395 const u8 *raw_key, unsigned int raw_key_size,
396 bool is_hw_wrapped,
397 const struct fscrypt_info *ci)
398{
399 WARN_ON(1);
400 return -EOPNOTSUPP;
401}
402
403static inline void
404fscrypt_destroy_inline_crypt_key(struct fscrypt_prepared_key *prep_key)
405{
406}
407
408static inline int fscrypt_derive_raw_secret(struct super_block *sb,
409 const u8 *wrapped_key,
410 unsigned int wrapped_key_size,
411 u8 *raw_secret,
412 unsigned int raw_secret_size)
413{
414 fscrypt_warn(NULL,
415 "kernel built without support for hardware-wrapped keys");
416 return -EOPNOTSUPP;
417}
418
419static inline bool
420fscrypt_is_key_prepared(struct fscrypt_prepared_key *prep_key,
421 const struct fscrypt_info *ci)
422{
423 return smp_load_acquire(&prep_key->tfm) != NULL;
424}
425#endif /* !CONFIG_FS_ENCRYPTION_INLINE_CRYPT */
426
427/* keyring.c */
428
429/*
430 * fscrypt_master_key_secret - secret key material of an in-use master key
431 */
432struct fscrypt_master_key_secret {
433
434 /*
435 * For v2 policy keys: HKDF context keyed by this master key.
436 * For v1 policy keys: not set (hkdf.hmac_tfm == NULL).
437 */
438 struct fscrypt_hkdf hkdf;
439
440 /* Size of the raw key in bytes. Set even if ->raw isn't set. */
441 u32 size;
442
443 /* True if the key in ->raw is a hardware-wrapped key. */
444 bool is_hw_wrapped;
445
446 /*
447 * For v1 policy keys: the raw key. Wiped for v2 policy keys, unless
448 * ->is_hw_wrapped is true, in which case this contains the wrapped key
449 * rather than the key with which 'hkdf' was keyed.
450 */
451 u8 raw[FSCRYPT_MAX_HW_WRAPPED_KEY_SIZE];
452
453} __randomize_layout;
454
455/*
456 * fscrypt_master_key - an in-use master key
457 *
458 * This represents a master encryption key which has been added to the
459 * filesystem and can be used to "unlock" the encrypted files which were
460 * encrypted with it.
461 */
462struct fscrypt_master_key {
463
464 /*
465 * The secret key material. After FS_IOC_REMOVE_ENCRYPTION_KEY is
466 * executed, this is wiped and no new inodes can be unlocked with this
467 * key; however, there may still be inodes in ->mk_decrypted_inodes
468 * which could not be evicted. As long as some inodes still remain,
469 * FS_IOC_REMOVE_ENCRYPTION_KEY can be retried, or
470 * FS_IOC_ADD_ENCRYPTION_KEY can add the secret again.
471 *
472 * Locking: protected by this master key's key->sem.
473 */
474 struct fscrypt_master_key_secret mk_secret;
475
476 /*
477 * For v1 policy keys: an arbitrary key descriptor which was assigned by
478 * userspace (->descriptor).
479 *
480 * For v2 policy keys: a cryptographic hash of this key (->identifier).
481 */
482 struct fscrypt_key_specifier mk_spec;
483
484 /*
485 * Keyring which contains a key of type 'key_type_fscrypt_user' for each
486 * user who has added this key. Normally each key will be added by just
487 * one user, but it's possible that multiple users share a key, and in
488 * that case we need to keep track of those users so that one user can't
489 * remove the key before the others want it removed too.
490 *
491 * This is NULL for v1 policy keys; those can only be added by root.
492 *
493 * Locking: in addition to this keyring's own semaphore, this is
494 * protected by this master key's key->sem, so we can do atomic
495 * search+insert. It can also be searched without taking any locks, but
496 * in that case the returned key may have already been removed.
497 */
498 struct key *mk_users;
499
500 /*
501 * Length of ->mk_decrypted_inodes, plus one if mk_secret is present.
502 * Once this goes to 0, the master key is removed from ->s_master_keys.
503 * The 'struct fscrypt_master_key' will continue to live as long as the
504 * 'struct key' whose payload it is, but we won't let this reference
505 * count rise again.
506 */
507 refcount_t mk_refcount;
508
509 /*
510 * List of inodes that were unlocked using this key. This allows the
511 * inodes to be evicted efficiently if the key is removed.
512 */
513 struct list_head mk_decrypted_inodes;
514 spinlock_t mk_decrypted_inodes_lock;
515
516 /*
517 * Per-mode encryption keys for the various types of encryption policies
518 * that use them. Allocated and derived on-demand.
519 */
520 struct fscrypt_prepared_key mk_direct_keys[FSCRYPT_MODE_MAX + 1];
521 struct fscrypt_prepared_key mk_iv_ino_lblk_64_keys[FSCRYPT_MODE_MAX + 1];
522 struct fscrypt_prepared_key mk_iv_ino_lblk_32_keys[FSCRYPT_MODE_MAX + 1];
523
524 /* Hash key for inode numbers. Initialized only when needed. */
525 siphash_key_t mk_ino_hash_key;
526 bool mk_ino_hash_key_initialized;
527
528} __randomize_layout;
529
530static inline bool
531is_master_key_secret_present(const struct fscrypt_master_key_secret *secret)
532{
533 /*
534 * The READ_ONCE() is only necessary for fscrypt_drop_inode() and
535 * fscrypt_key_describe(). These run in atomic context, so they can't
536 * take the key semaphore and thus 'secret' can change concurrently
537 * which would be a data race. But they only need to know whether the
538 * secret *was* present at the time of check, so READ_ONCE() suffices.
539 */
540 return READ_ONCE(secret->size) != 0;
541}
542
543static inline const char *master_key_spec_type(
544 const struct fscrypt_key_specifier *spec)
545{
546 switch (spec->type) {
547 case FSCRYPT_KEY_SPEC_TYPE_DESCRIPTOR:
548 return "descriptor";
549 case FSCRYPT_KEY_SPEC_TYPE_IDENTIFIER:
550 return "identifier";
551 }
552 return "[unknown]";
553}
554
555static inline int master_key_spec_len(const struct fscrypt_key_specifier *spec)
556{
557 switch (spec->type) {
558 case FSCRYPT_KEY_SPEC_TYPE_DESCRIPTOR:
559 return FSCRYPT_KEY_DESCRIPTOR_SIZE;
560 case FSCRYPT_KEY_SPEC_TYPE_IDENTIFIER:
561 return FSCRYPT_KEY_IDENTIFIER_SIZE;
562 }
563 return 0;
564}
565
566struct key *
567fscrypt_find_master_key(struct super_block *sb,
568 const struct fscrypt_key_specifier *mk_spec);
569
570int fscrypt_add_test_dummy_key(struct super_block *sb,
571 struct fscrypt_key_specifier *key_spec);
572
573int fscrypt_verify_key_added(struct super_block *sb,
574 const u8 identifier[FSCRYPT_KEY_IDENTIFIER_SIZE]);
575
576int __init fscrypt_init_keyring(void);
577
578/* keysetup.c */
579
580struct fscrypt_mode {
581 const char *friendly_name;
582 const char *cipher_str;
583 int keysize;
584 int ivsize;
585 int logged_impl_name;
586 enum blk_crypto_mode_num blk_crypto_mode;
587};
588
589extern struct fscrypt_mode fscrypt_modes[];
590
591int fscrypt_prepare_key(struct fscrypt_prepared_key *prep_key,
592 const u8 *raw_key, unsigned int raw_key_size,
593 bool is_hw_wrapped, const struct fscrypt_info *ci);
594
595void fscrypt_destroy_prepared_key(struct fscrypt_prepared_key *prep_key);
596
597int fscrypt_set_per_file_enc_key(struct fscrypt_info *ci, const u8 *raw_key);
598
599int fscrypt_derive_dirhash_key(struct fscrypt_info *ci,
600 const struct fscrypt_master_key *mk);
601
602void fscrypt_hash_inode_number(struct fscrypt_info *ci,
603 const struct fscrypt_master_key *mk);
604
605int fscrypt_get_encryption_info(struct inode *inode, bool allow_unsupported);
606
607/**
608 * fscrypt_require_key() - require an inode's encryption key
609 * @inode: the inode we need the key for
610 *
611 * If the inode is encrypted, set up its encryption key if not already done.
612 * Then require that the key be present and return -ENOKEY otherwise.
613 *
614 * No locks are needed, and the key will live as long as the struct inode --- so
615 * it won't go away from under you.
616 *
617 * Return: 0 on success, -ENOKEY if the key is missing, or another -errno code
618 * if a problem occurred while setting up the encryption key.
619 */
620static inline int fscrypt_require_key(struct inode *inode)
621{
622 if (IS_ENCRYPTED(inode)) {
623 int err = fscrypt_get_encryption_info(inode, false);
624
625 if (err)
626 return err;
627 if (!fscrypt_has_encryption_key(inode))
628 return -ENOKEY;
629 }
630 return 0;
631}
632
633/* keysetup_v1.c */
634
635void fscrypt_put_direct_key(struct fscrypt_direct_key *dk);
636
637int fscrypt_setup_v1_file_key(struct fscrypt_info *ci,
638 const u8 *raw_master_key);
639
640int fscrypt_setup_v1_file_key_via_subscribed_keyrings(struct fscrypt_info *ci);
641
642/* policy.c */
643
644bool fscrypt_policies_equal(const union fscrypt_policy *policy1,
645 const union fscrypt_policy *policy2);
646bool fscrypt_supported_policy(const union fscrypt_policy *policy_u,
647 const struct inode *inode);
648int fscrypt_policy_from_context(union fscrypt_policy *policy_u,
649 const union fscrypt_context *ctx_u,
650 int ctx_size);
651const union fscrypt_policy *fscrypt_policy_to_inherit(struct inode *dir);
652
653#endif /* _FSCRYPT_PRIVATE_H */