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