Whamcloud - gitweb
LU-14195 libcfs: switch to kfree_sensitive
[fs/lustre-release.git] / libcfs / libcfs / crypto / keysetup_v1.c
1 // SPDX-License-Identifier: GPL-2.0
2 /*
3  * Key setup for v1 encryption policies
4  *
5  * Copyright 2015, 2019 Google LLC
6  */
7 /*
8  * Linux commit 219d54332a09
9  * tags/v5.4
10  */
11
12 /*
13  * This file implements compatibility functions for the original encryption
14  * policy version ("v1"), including:
15  *
16  * - Deriving per-file keys using the AES-128-ECB based KDF
17  *   (rather than the new method of using HKDF-SHA512)
18  *
19  * - Retrieving llcrypt master keys from process-subscribed keyrings
20  *   (rather than the new method of using a filesystem-level keyring)
21  *
22  * - Handling policies with the DIRECT_KEY flag set using a master key table
23  *   (rather than the new method of implementing DIRECT_KEY with per-mode keys
24  *    managed alongside the master keys in the filesystem-level keyring)
25  */
26
27 #include <crypto/algapi.h>
28 #include <crypto/skcipher.h>
29 #include <keys/user-type.h>
30 #include <linux/hashtable.h>
31 #include <linux/scatterlist.h>
32
33 #include "llcrypt_private.h"
34
35 /* Table of keys referenced by DIRECT_KEY policies */
36 static DEFINE_HASHTABLE(llcrypt_direct_keys, 6); /* 6 bits = 64 buckets */
37 static DEFINE_SPINLOCK(llcrypt_direct_keys_lock);
38
39 /*
40  * v1 key derivation function.  This generates the derived key by encrypting the
41  * master key with AES-128-ECB using the nonce as the AES key.  This provides a
42  * unique derived key with sufficient entropy for each inode.  However, it's
43  * nonstandard, non-extensible, doesn't evenly distribute the entropy from the
44  * master key, and is trivially reversible: an attacker who compromises a
45  * derived key can "decrypt" it to get back to the master key, then derive any
46  * other key.  For all new code, use HKDF instead.
47  *
48  * The master key must be at least as long as the derived key.  If the master
49  * key is longer, then only the first 'derived_keysize' bytes are used.
50  */
51 static int derive_key_aes(const u8 *master_key,
52                           const u8 nonce[FS_KEY_DERIVATION_NONCE_SIZE],
53                           u8 *derived_key, unsigned int derived_keysize)
54 {
55         int res = 0;
56         struct skcipher_request *req = NULL;
57         DECLARE_CRYPTO_WAIT(wait);
58         struct scatterlist src_sg, dst_sg;
59         struct crypto_skcipher *tfm = crypto_alloc_skcipher("ecb(aes)", 0, 0);
60
61         if (IS_ERR(tfm)) {
62                 res = PTR_ERR(tfm);
63                 tfm = NULL;
64                 goto out;
65         }
66         crypto_skcipher_set_flags(tfm, CRYPTO_TFM_REQ_FORBID_WEAK_KEYS);
67         req = skcipher_request_alloc(tfm, GFP_NOFS);
68         if (!req) {
69                 res = -ENOMEM;
70                 goto out;
71         }
72         skcipher_request_set_callback(req,
73                         CRYPTO_TFM_REQ_MAY_BACKLOG | CRYPTO_TFM_REQ_MAY_SLEEP,
74                         crypto_req_done, &wait);
75         res = crypto_skcipher_setkey(tfm, nonce, FS_KEY_DERIVATION_NONCE_SIZE);
76         if (res < 0)
77                 goto out;
78
79         sg_init_one(&src_sg, master_key, derived_keysize);
80         sg_init_one(&dst_sg, derived_key, derived_keysize);
81         skcipher_request_set_crypt(req, &src_sg, &dst_sg, derived_keysize,
82                                    NULL);
83         res = crypto_wait_req(crypto_skcipher_encrypt(req), &wait);
84 out:
85         skcipher_request_free(req);
86         crypto_free_skcipher(tfm);
87         return res;
88 }
89
90 /*
91  * Search the current task's subscribed keyrings for a "logon" key with
92  * description prefix:descriptor, and if found acquire a read lock on it and
93  * return a pointer to its validated payload in *payload_ret.
94  */
95 static struct key *
96 find_and_lock_process_key(const char *prefix,
97                           const u8 descriptor[LLCRYPT_KEY_DESCRIPTOR_SIZE],
98                           unsigned int min_keysize,
99                           const struct llcrypt_key **payload_ret)
100 {
101         char *description;
102         struct key *key;
103         const struct user_key_payload *ukp;
104         const struct llcrypt_key *payload;
105
106         description = kasprintf(GFP_NOFS, "%s%*phN", prefix,
107                                 LLCRYPT_KEY_DESCRIPTOR_SIZE, descriptor);
108         if (!description)
109                 return ERR_PTR(-ENOMEM);
110
111         key = request_key(&key_type_logon, description, NULL);
112         kfree(description);
113         if (IS_ERR(key))
114                 return key;
115
116         down_read(&key->sem);
117         ukp = user_key_payload_locked(key);
118
119         if (!ukp) /* was the key revoked before we acquired its semaphore? */
120                 goto invalid;
121
122         payload = (const struct llcrypt_key *)ukp->data;
123
124         if (ukp->datalen != sizeof(struct llcrypt_key) ||
125             payload->size < 1 || payload->size > LLCRYPT_MAX_KEY_SIZE) {
126                 llcrypt_warn(NULL,
127                              "key with description '%s' has invalid payload",
128                              key->description);
129                 goto invalid;
130         }
131
132         if (payload->size < min_keysize) {
133                 llcrypt_warn(NULL,
134                              "key with description '%s' is too short (got %u bytes, need %u+ bytes)",
135                              key->description, payload->size, min_keysize);
136                 goto invalid;
137         }
138
139         *payload_ret = payload;
140         return key;
141
142 invalid:
143         up_read(&key->sem);
144         key_put(key);
145         return ERR_PTR(-ENOKEY);
146 }
147
148 /* Master key referenced by DIRECT_KEY policy */
149 struct llcrypt_direct_key {
150         struct hlist_node               dk_node;
151         refcount_t                      dk_refcount;
152         const struct llcrypt_mode       *dk_mode;
153         struct crypto_skcipher          *dk_ctfm;
154         u8                              dk_descriptor[LLCRYPT_KEY_DESCRIPTOR_SIZE];
155         u8                              dk_raw[LLCRYPT_MAX_KEY_SIZE];
156 };
157
158 static void free_direct_key(struct llcrypt_direct_key *dk)
159 {
160         if (dk) {
161                 crypto_free_skcipher(dk->dk_ctfm);
162                 kfree_sensitive(dk);
163         }
164 }
165
166 void llcrypt_put_direct_key(struct llcrypt_direct_key *dk)
167 {
168         if (!refcount_dec_and_lock(&dk->dk_refcount, &llcrypt_direct_keys_lock))
169                 return;
170         hash_del(&dk->dk_node);
171         spin_unlock(&llcrypt_direct_keys_lock);
172
173         free_direct_key(dk);
174 }
175
176 /*
177  * Find/insert the given key into the llcrypt_direct_keys table.  If found, it
178  * is returned with elevated refcount, and 'to_insert' is freed if non-NULL.  If
179  * not found, 'to_insert' is inserted and returned if it's non-NULL; otherwise
180  * NULL is returned.
181  */
182 static struct llcrypt_direct_key *
183 find_or_insert_direct_key(struct llcrypt_direct_key *to_insert,
184                           const u8 *raw_key, const struct llcrypt_info *ci)
185 {
186         unsigned long hash_key;
187         struct llcrypt_direct_key *dk;
188
189         /*
190          * Careful: to avoid potentially leaking secret key bytes via timing
191          * information, we must key the hash table by descriptor rather than by
192          * raw key, and use crypto_memneq() when comparing raw keys.
193          */
194
195         BUILD_BUG_ON(sizeof(hash_key) > LLCRYPT_KEY_DESCRIPTOR_SIZE);
196         memcpy(&hash_key, ci->ci_policy.v1.master_key_descriptor,
197                sizeof(hash_key));
198
199         spin_lock(&llcrypt_direct_keys_lock);
200         hash_for_each_possible(llcrypt_direct_keys, dk, dk_node, hash_key) {
201                 if (memcmp(ci->ci_policy.v1.master_key_descriptor,
202                            dk->dk_descriptor, LLCRYPT_KEY_DESCRIPTOR_SIZE) != 0)
203                         continue;
204                 if (ci->ci_mode != dk->dk_mode)
205                         continue;
206                 if (crypto_memneq(raw_key, dk->dk_raw, ci->ci_mode->keysize))
207                         continue;
208                 /* using existing tfm with same (descriptor, mode, raw_key) */
209                 refcount_inc(&dk->dk_refcount);
210                 spin_unlock(&llcrypt_direct_keys_lock);
211                 free_direct_key(to_insert);
212                 return dk;
213         }
214         if (to_insert)
215                 hash_add(llcrypt_direct_keys, &to_insert->dk_node, hash_key);
216         spin_unlock(&llcrypt_direct_keys_lock);
217         return to_insert;
218 }
219
220 /* Prepare to encrypt directly using the master key in the given mode */
221 static struct llcrypt_direct_key *
222 llcrypt_get_direct_key(const struct llcrypt_info *ci, const u8 *raw_key)
223 {
224         struct llcrypt_direct_key *dk;
225         int err;
226
227         /* Is there already a tfm for this key? */
228         dk = find_or_insert_direct_key(NULL, raw_key, ci);
229         if (dk)
230                 return dk;
231
232         /* Nope, allocate one. */
233         dk = kzalloc(sizeof(*dk), GFP_NOFS);
234         if (!dk)
235                 return ERR_PTR(-ENOMEM);
236         refcount_set(&dk->dk_refcount, 1);
237         dk->dk_mode = ci->ci_mode;
238         dk->dk_ctfm = llcrypt_allocate_skcipher(ci->ci_mode, raw_key,
239                                                 ci->ci_inode);
240         if (IS_ERR(dk->dk_ctfm)) {
241                 err = PTR_ERR(dk->dk_ctfm);
242                 dk->dk_ctfm = NULL;
243                 goto err_free_dk;
244         }
245         memcpy(dk->dk_descriptor, ci->ci_policy.v1.master_key_descriptor,
246                LLCRYPT_KEY_DESCRIPTOR_SIZE);
247         memcpy(dk->dk_raw, raw_key, ci->ci_mode->keysize);
248
249         return find_or_insert_direct_key(dk, raw_key, ci);
250
251 err_free_dk:
252         free_direct_key(dk);
253         return ERR_PTR(err);
254 }
255
256 /* v1 policy, DIRECT_KEY: use the master key directly */
257 static int setup_v1_file_key_direct(struct llcrypt_info *ci,
258                                     const u8 *raw_master_key)
259 {
260         const struct llcrypt_mode *mode = ci->ci_mode;
261         struct llcrypt_direct_key *dk;
262
263         if (!llcrypt_mode_supports_direct_key(mode)) {
264                 llcrypt_warn(ci->ci_inode,
265                              "Direct key mode not allowed with %s",
266                              mode->friendly_name);
267                 return -EINVAL;
268         }
269
270         if (ci->ci_policy.v1.contents_encryption_mode !=
271             ci->ci_policy.v1.filenames_encryption_mode) {
272                 llcrypt_warn(ci->ci_inode,
273                              "Direct key mode not allowed with different contents and filenames modes");
274                 return -EINVAL;
275         }
276
277         /* ESSIV implies 16-byte IVs which implies !DIRECT_KEY */
278         if (WARN_ON(mode->needs_essiv))
279                 return -EINVAL;
280
281         dk = llcrypt_get_direct_key(ci, raw_master_key);
282         if (IS_ERR(dk))
283                 return PTR_ERR(dk);
284         ci->ci_direct_key = dk;
285         ci->ci_ctfm = dk->dk_ctfm;
286         return 0;
287 }
288
289 /* v1 policy, !DIRECT_KEY: derive the file's encryption key */
290 static int setup_v1_file_key_derived(struct llcrypt_info *ci,
291                                      const u8 *raw_master_key)
292 {
293         u8 *derived_key;
294         int err;
295
296         /*
297          * This cannot be a stack buffer because it will be passed to the
298          * scatterlist crypto API during derive_key_aes().
299          */
300         derived_key = kmalloc(ci->ci_mode->keysize, GFP_NOFS);
301         if (!derived_key)
302                 return -ENOMEM;
303
304         err = derive_key_aes(raw_master_key, ci->ci_nonce,
305                              derived_key, ci->ci_mode->keysize);
306         if (err)
307                 goto out;
308
309         err = llcrypt_set_derived_key(ci, derived_key);
310 out:
311         kfree_sensitive(derived_key);
312         return err;
313 }
314
315 int llcrypt_setup_v1_file_key(struct llcrypt_info *ci, const u8 *raw_master_key)
316 {
317         if (ci->ci_policy.v1.flags & LLCRYPT_POLICY_FLAG_DIRECT_KEY)
318                 return setup_v1_file_key_direct(ci, raw_master_key);
319         else
320                 return setup_v1_file_key_derived(ci, raw_master_key);
321 }
322
323 int llcrypt_setup_v1_file_key_via_subscribed_keyrings(struct llcrypt_info *ci)
324 {
325         struct key *key;
326         const struct llcrypt_key *payload;
327         int err;
328
329         key = find_and_lock_process_key(LLCRYPT_KEY_DESC_PREFIX,
330                                         ci->ci_policy.v1.master_key_descriptor,
331                                         ci->ci_mode->keysize, &payload);
332         if (key == ERR_PTR(-ENOKEY)) {
333                 struct lustre_sb_info *lsi = s2lsi(ci->ci_inode->i_sb);
334
335                 if (lsi && lsi->lsi_cop->key_prefix) {
336                         key =
337                             find_and_lock_process_key(lsi->lsi_cop->key_prefix,
338                                                       ci->ci_policy.v1.master_key_descriptor,
339                                                       ci->ci_mode->keysize,
340                                                       &payload);
341                 }
342         }
343         if (IS_ERR(key))
344                 return PTR_ERR(key);
345
346         err = llcrypt_setup_v1_file_key(ci, payload->raw);
347         up_read(&key->sem);
348         key_put(key);
349         return err;
350 }