GNU Linux-libre 5.4.257-gnu1
[releases.git] / fs / crypto / fscrypt_private.h
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 <crypto/hash.h>
16
17 #define CONST_STRLEN(str)       (sizeof(str) - 1)
18
19 #define FS_KEY_DERIVATION_NONCE_SIZE    16
20
21 #define FSCRYPT_MIN_KEY_SIZE            16
22
23 #define FSCRYPT_CONTEXT_V1      1
24 #define FSCRYPT_CONTEXT_V2      2
25
26 /* Keep this in sync with include/uapi/linux/fscrypt.h */
27 #define FSCRYPT_MODE_MAX        FSCRYPT_MODE_ADIANTUM
28
29 struct fscrypt_context_v1 {
30         u8 version; /* FSCRYPT_CONTEXT_V1 */
31         u8 contents_encryption_mode;
32         u8 filenames_encryption_mode;
33         u8 flags;
34         u8 master_key_descriptor[FSCRYPT_KEY_DESCRIPTOR_SIZE];
35         u8 nonce[FS_KEY_DERIVATION_NONCE_SIZE];
36 };
37
38 struct fscrypt_context_v2 {
39         u8 version; /* FSCRYPT_CONTEXT_V2 */
40         u8 contents_encryption_mode;
41         u8 filenames_encryption_mode;
42         u8 flags;
43         u8 __reserved[4];
44         u8 master_key_identifier[FSCRYPT_KEY_IDENTIFIER_SIZE];
45         u8 nonce[FS_KEY_DERIVATION_NONCE_SIZE];
46 };
47
48 /**
49  * fscrypt_context - the encryption context of an inode
50  *
51  * This is the on-disk equivalent of an fscrypt_policy, stored alongside each
52  * encrypted file usually in a hidden extended attribute.  It contains the
53  * fields from the fscrypt_policy, in order to identify the encryption algorithm
54  * and key with which the file is encrypted.  It also contains a nonce that was
55  * randomly generated by fscrypt itself; this is used as KDF input or as a tweak
56  * to cause different files to be encrypted differently.
57  */
58 union fscrypt_context {
59         u8 version;
60         struct fscrypt_context_v1 v1;
61         struct fscrypt_context_v2 v2;
62 };
63
64 /*
65  * Return the size expected for the given fscrypt_context based on its version
66  * number, or 0 if the context version is unrecognized.
67  */
68 static inline int fscrypt_context_size(const union fscrypt_context *ctx)
69 {
70         switch (ctx->version) {
71         case FSCRYPT_CONTEXT_V1:
72                 BUILD_BUG_ON(sizeof(ctx->v1) != 28);
73                 return sizeof(ctx->v1);
74         case FSCRYPT_CONTEXT_V2:
75                 BUILD_BUG_ON(sizeof(ctx->v2) != 40);
76                 return sizeof(ctx->v2);
77         }
78         return 0;
79 }
80
81 #undef fscrypt_policy
82 union fscrypt_policy {
83         u8 version;
84         struct fscrypt_policy_v1 v1;
85         struct fscrypt_policy_v2 v2;
86 };
87
88 /*
89  * Return the size expected for the given fscrypt_policy based on its version
90  * number, or 0 if the policy version is unrecognized.
91  */
92 static inline int fscrypt_policy_size(const union fscrypt_policy *policy)
93 {
94         switch (policy->version) {
95         case FSCRYPT_POLICY_V1:
96                 return sizeof(policy->v1);
97         case FSCRYPT_POLICY_V2:
98                 return sizeof(policy->v2);
99         }
100         return 0;
101 }
102
103 /* Return the contents encryption mode of a valid encryption policy */
104 static inline u8
105 fscrypt_policy_contents_mode(const union fscrypt_policy *policy)
106 {
107         switch (policy->version) {
108         case FSCRYPT_POLICY_V1:
109                 return policy->v1.contents_encryption_mode;
110         case FSCRYPT_POLICY_V2:
111                 return policy->v2.contents_encryption_mode;
112         }
113         BUG();
114 }
115
116 /* Return the filenames encryption mode of a valid encryption policy */
117 static inline u8
118 fscrypt_policy_fnames_mode(const union fscrypt_policy *policy)
119 {
120         switch (policy->version) {
121         case FSCRYPT_POLICY_V1:
122                 return policy->v1.filenames_encryption_mode;
123         case FSCRYPT_POLICY_V2:
124                 return policy->v2.filenames_encryption_mode;
125         }
126         BUG();
127 }
128
129 /* Return the flags (FSCRYPT_POLICY_FLAG*) of a valid encryption policy */
130 static inline u8
131 fscrypt_policy_flags(const union fscrypt_policy *policy)
132 {
133         switch (policy->version) {
134         case FSCRYPT_POLICY_V1:
135                 return policy->v1.flags;
136         case FSCRYPT_POLICY_V2:
137                 return policy->v2.flags;
138         }
139         BUG();
140 }
141
142 static inline bool
143 fscrypt_is_direct_key_policy(const union fscrypt_policy *policy)
144 {
145         return fscrypt_policy_flags(policy) & FSCRYPT_POLICY_FLAG_DIRECT_KEY;
146 }
147
148 /**
149  * For encrypted symlinks, the ciphertext length is stored at the beginning
150  * of the string in little-endian format.
151  */
152 struct fscrypt_symlink_data {
153         __le16 len;
154         char encrypted_path[1];
155 } __packed;
156
157 /*
158  * fscrypt_info - the "encryption key" for an inode
159  *
160  * When an encrypted file's key is made available, an instance of this struct is
161  * allocated and stored in ->i_crypt_info.  Once created, it remains until the
162  * inode is evicted.
163  */
164 struct fscrypt_info {
165
166         /* The actual crypto transform used for encryption and decryption */
167         struct crypto_skcipher *ci_ctfm;
168
169         /*
170          * Cipher for ESSIV IV generation.  Only set for CBC contents
171          * encryption, otherwise is NULL.
172          */
173         struct crypto_cipher *ci_essiv_tfm;
174
175         /*
176          * Encryption mode used for this inode.  It corresponds to either the
177          * contents or filenames encryption mode, depending on the inode type.
178          */
179         struct fscrypt_mode *ci_mode;
180
181         /* Back-pointer to the inode */
182         struct inode *ci_inode;
183
184         /*
185          * The master key with which this inode was unlocked (decrypted).  This
186          * will be NULL if the master key was found in a process-subscribed
187          * keyring rather than in the filesystem-level keyring.
188          */
189         struct key *ci_master_key;
190
191         /*
192          * Link in list of inodes that were unlocked with the master key.
193          * Only used when ->ci_master_key is set.
194          */
195         struct list_head ci_master_key_link;
196
197         /*
198          * If non-NULL, then encryption is done using the master key directly
199          * and ci_ctfm will equal ci_direct_key->dk_ctfm.
200          */
201         struct fscrypt_direct_key *ci_direct_key;
202
203         /* The encryption policy used by this inode */
204         union fscrypt_policy ci_policy;
205
206         /* This inode's nonce, copied from the fscrypt_context */
207         u8 ci_nonce[FS_KEY_DERIVATION_NONCE_SIZE];
208 };
209
210 typedef enum {
211         FS_DECRYPT = 0,
212         FS_ENCRYPT,
213 } fscrypt_direction_t;
214
215 #define FS_CTX_REQUIRES_FREE_ENCRYPT_FL         0x00000001
216
217 static inline bool fscrypt_valid_enc_modes(u32 contents_mode,
218                                            u32 filenames_mode)
219 {
220         if (contents_mode == FSCRYPT_MODE_AES_128_CBC &&
221             filenames_mode == FSCRYPT_MODE_AES_128_CTS)
222                 return true;
223
224         if (contents_mode == FSCRYPT_MODE_AES_256_XTS &&
225             filenames_mode == FSCRYPT_MODE_AES_256_CTS)
226                 return true;
227
228         if (contents_mode == FSCRYPT_MODE_ADIANTUM &&
229             filenames_mode == FSCRYPT_MODE_ADIANTUM)
230                 return true;
231
232         return false;
233 }
234
235 /* crypto.c */
236 extern struct kmem_cache *fscrypt_info_cachep;
237 extern int fscrypt_initialize(unsigned int cop_flags);
238 extern int fscrypt_crypt_block(const struct inode *inode,
239                                fscrypt_direction_t rw, u64 lblk_num,
240                                struct page *src_page, struct page *dest_page,
241                                unsigned int len, unsigned int offs,
242                                gfp_t gfp_flags);
243 extern struct page *fscrypt_alloc_bounce_page(gfp_t gfp_flags);
244 extern const struct dentry_operations fscrypt_d_ops;
245
246 extern void __printf(3, 4) __cold
247 fscrypt_msg(const struct inode *inode, const char *level, const char *fmt, ...);
248
249 #define fscrypt_warn(inode, fmt, ...)           \
250         fscrypt_msg((inode), KERN_WARNING, fmt, ##__VA_ARGS__)
251 #define fscrypt_err(inode, fmt, ...)            \
252         fscrypt_msg((inode), KERN_ERR, fmt, ##__VA_ARGS__)
253
254 #define FSCRYPT_MAX_IV_SIZE     32
255
256 union fscrypt_iv {
257         struct {
258                 /* logical block number within the file */
259                 __le64 lblk_num;
260
261                 /* per-file nonce; only set in DIRECT_KEY mode */
262                 u8 nonce[FS_KEY_DERIVATION_NONCE_SIZE];
263         };
264         u8 raw[FSCRYPT_MAX_IV_SIZE];
265 };
266
267 void fscrypt_generate_iv(union fscrypt_iv *iv, u64 lblk_num,
268                          const struct fscrypt_info *ci);
269
270 /* fname.c */
271 extern int fname_encrypt(struct inode *inode, const struct qstr *iname,
272                          u8 *out, unsigned int olen);
273 extern bool fscrypt_fname_encrypted_size(const struct inode *inode,
274                                          u32 orig_len, u32 max_len,
275                                          u32 *encrypted_len_ret);
276
277 /* hkdf.c */
278
279 struct fscrypt_hkdf {
280         struct crypto_shash *hmac_tfm;
281 };
282
283 extern int fscrypt_init_hkdf(struct fscrypt_hkdf *hkdf, const u8 *master_key,
284                              unsigned int master_key_size);
285
286 /*
287  * The list of contexts in which fscrypt uses HKDF.  These values are used as
288  * the first byte of the HKDF application-specific info string to guarantee that
289  * info strings are never repeated between contexts.  This ensures that all HKDF
290  * outputs are unique and cryptographically isolated, i.e. knowledge of one
291  * output doesn't reveal another.
292  */
293 #define HKDF_CONTEXT_KEY_IDENTIFIER     1
294 #define HKDF_CONTEXT_PER_FILE_KEY       2
295 #define HKDF_CONTEXT_PER_MODE_KEY       3
296
297 extern int fscrypt_hkdf_expand(struct fscrypt_hkdf *hkdf, u8 context,
298                                const u8 *info, unsigned int infolen,
299                                u8 *okm, unsigned int okmlen);
300
301 extern void fscrypt_destroy_hkdf(struct fscrypt_hkdf *hkdf);
302
303 /* keyring.c */
304
305 /*
306  * fscrypt_master_key_secret - secret key material of an in-use master key
307  */
308 struct fscrypt_master_key_secret {
309
310         /*
311          * For v2 policy keys: HKDF context keyed by this master key.
312          * For v1 policy keys: not set (hkdf.hmac_tfm == NULL).
313          */
314         struct fscrypt_hkdf     hkdf;
315
316         /* Size of the raw key in bytes.  Set even if ->raw isn't set. */
317         u32                     size;
318
319         /* For v1 policy keys: the raw key.  Wiped for v2 policy keys. */
320         u8                      raw[FSCRYPT_MAX_KEY_SIZE];
321
322 } __randomize_layout;
323
324 /*
325  * fscrypt_master_key - an in-use master key
326  *
327  * This represents a master encryption key which has been added to the
328  * filesystem and can be used to "unlock" the encrypted files which were
329  * encrypted with it.
330  */
331 struct fscrypt_master_key {
332
333         /*
334          * The secret key material.  After FS_IOC_REMOVE_ENCRYPTION_KEY is
335          * executed, this is wiped and no new inodes can be unlocked with this
336          * key; however, there may still be inodes in ->mk_decrypted_inodes
337          * which could not be evicted.  As long as some inodes still remain,
338          * FS_IOC_REMOVE_ENCRYPTION_KEY can be retried, or
339          * FS_IOC_ADD_ENCRYPTION_KEY can add the secret again.
340          *
341          * Locking: protected by key->sem (outer) and mk_secret_sem (inner).
342          * The reason for two locks is that key->sem also protects modifying
343          * mk_users, which ranks it above the semaphore for the keyring key
344          * type, which is in turn above page faults (via keyring_read).  But
345          * sometimes filesystems call fscrypt_get_encryption_info() from within
346          * a transaction, which ranks it below page faults.  So we need a
347          * separate lock which protects mk_secret but not also mk_users.
348          */
349         struct fscrypt_master_key_secret        mk_secret;
350         struct rw_semaphore                     mk_secret_sem;
351
352         /*
353          * For v1 policy keys: an arbitrary key descriptor which was assigned by
354          * userspace (->descriptor).
355          *
356          * For v2 policy keys: a cryptographic hash of this key (->identifier).
357          */
358         struct fscrypt_key_specifier            mk_spec;
359
360         /*
361          * Keyring which contains a key of type 'key_type_fscrypt_user' for each
362          * user who has added this key.  Normally each key will be added by just
363          * one user, but it's possible that multiple users share a key, and in
364          * that case we need to keep track of those users so that one user can't
365          * remove the key before the others want it removed too.
366          *
367          * This is NULL for v1 policy keys; those can only be added by root.
368          *
369          * Locking: in addition to this keyrings own semaphore, this is
370          * protected by the master key's key->sem, so we can do atomic
371          * search+insert.  It can also be searched without taking any locks, but
372          * in that case the returned key may have already been removed.
373          */
374         struct key              *mk_users;
375
376         /*
377          * Length of ->mk_decrypted_inodes, plus one if mk_secret is present.
378          * Once this goes to 0, the master key is removed from ->s_master_keys.
379          * The 'struct fscrypt_master_key' will continue to live as long as the
380          * 'struct key' whose payload it is, but we won't let this reference
381          * count rise again.
382          */
383         refcount_t              mk_refcount;
384
385         /*
386          * List of inodes that were unlocked using this key.  This allows the
387          * inodes to be evicted efficiently if the key is removed.
388          */
389         struct list_head        mk_decrypted_inodes;
390         spinlock_t              mk_decrypted_inodes_lock;
391
392         /* Per-mode tfms for DIRECT_KEY policies, allocated on-demand */
393         struct crypto_skcipher  *mk_mode_keys[FSCRYPT_MODE_MAX + 1];
394
395 } __randomize_layout;
396
397 static inline bool
398 is_master_key_secret_present(const struct fscrypt_master_key_secret *secret)
399 {
400         /*
401          * The READ_ONCE() is only necessary for fscrypt_drop_inode() and
402          * fscrypt_key_describe().  These run in atomic context, so they can't
403          * take ->mk_secret_sem and thus 'secret' can change concurrently which
404          * would be a data race.  But they only need to know whether the secret
405          * *was* present at the time of check, so READ_ONCE() suffices.
406          */
407         return READ_ONCE(secret->size) != 0;
408 }
409
410 static inline const char *master_key_spec_type(
411                                 const struct fscrypt_key_specifier *spec)
412 {
413         switch (spec->type) {
414         case FSCRYPT_KEY_SPEC_TYPE_DESCRIPTOR:
415                 return "descriptor";
416         case FSCRYPT_KEY_SPEC_TYPE_IDENTIFIER:
417                 return "identifier";
418         }
419         return "[unknown]";
420 }
421
422 static inline int master_key_spec_len(const struct fscrypt_key_specifier *spec)
423 {
424         switch (spec->type) {
425         case FSCRYPT_KEY_SPEC_TYPE_DESCRIPTOR:
426                 return FSCRYPT_KEY_DESCRIPTOR_SIZE;
427         case FSCRYPT_KEY_SPEC_TYPE_IDENTIFIER:
428                 return FSCRYPT_KEY_IDENTIFIER_SIZE;
429         }
430         return 0;
431 }
432
433 extern struct key *
434 fscrypt_find_master_key(struct super_block *sb,
435                         const struct fscrypt_key_specifier *mk_spec);
436
437 extern int fscrypt_verify_key_added(struct super_block *sb,
438                                     const u8 identifier[FSCRYPT_KEY_IDENTIFIER_SIZE]);
439
440 extern int __init fscrypt_init_keyring(void);
441
442 /* keysetup.c */
443
444 struct fscrypt_mode {
445         const char *friendly_name;
446         const char *cipher_str;
447         int keysize;
448         int ivsize;
449         bool logged_impl_name;
450         bool needs_essiv;
451 };
452
453 static inline bool
454 fscrypt_mode_supports_direct_key(const struct fscrypt_mode *mode)
455 {
456         return mode->ivsize >= offsetofend(union fscrypt_iv, nonce);
457 }
458
459 extern struct crypto_skcipher *
460 fscrypt_allocate_skcipher(struct fscrypt_mode *mode, const u8 *raw_key,
461                           const struct inode *inode);
462
463 extern int fscrypt_set_derived_key(struct fscrypt_info *ci,
464                                    const u8 *derived_key);
465
466 /* keysetup_v1.c */
467
468 extern void fscrypt_put_direct_key(struct fscrypt_direct_key *dk);
469
470 extern int fscrypt_setup_v1_file_key(struct fscrypt_info *ci,
471                                      const u8 *raw_master_key);
472
473 extern int fscrypt_setup_v1_file_key_via_subscribed_keyrings(
474                                         struct fscrypt_info *ci);
475 /* policy.c */
476
477 extern bool fscrypt_policies_equal(const union fscrypt_policy *policy1,
478                                    const union fscrypt_policy *policy2);
479 extern bool fscrypt_supported_policy(const union fscrypt_policy *policy_u,
480                                      const struct inode *inode);
481 extern int fscrypt_policy_from_context(union fscrypt_policy *policy_u,
482                                        const union fscrypt_context *ctx_u,
483                                        int ctx_size);
484
485 #endif /* _FSCRYPT_PRIVATE_H */