Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
torvalds
GitHub Repository: torvalds/linux
Path: blob/master/fs/crypto/keysetup_v1.c
26282 views
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
/*
9
* This file implements compatibility functions for the original encryption
10
* policy version ("v1"), including:
11
*
12
* - Deriving per-file encryption keys using the AES-128-ECB based KDF
13
* (rather than the new method of using HKDF-SHA512)
14
*
15
* - Retrieving fscrypt master keys from process-subscribed keyrings
16
* (rather than the new method of using a filesystem-level keyring)
17
*
18
* - Handling policies with the DIRECT_KEY flag set using a master key table
19
* (rather than the new method of implementing DIRECT_KEY with per-mode keys
20
* managed alongside the master keys in the filesystem-level keyring)
21
*/
22
23
#include <crypto/skcipher.h>
24
#include <crypto/utils.h>
25
#include <keys/user-type.h>
26
#include <linux/hashtable.h>
27
#include <linux/scatterlist.h>
28
29
#include "fscrypt_private.h"
30
31
/* Table of keys referenced by DIRECT_KEY policies */
32
static DEFINE_HASHTABLE(fscrypt_direct_keys, 6); /* 6 bits = 64 buckets */
33
static DEFINE_SPINLOCK(fscrypt_direct_keys_lock);
34
35
/*
36
* v1 key derivation function. This generates the derived key by encrypting the
37
* master key with AES-128-ECB using the nonce as the AES key. This provides a
38
* unique derived key with sufficient entropy for each inode. However, it's
39
* nonstandard, non-extensible, doesn't evenly distribute the entropy from the
40
* master key, and is trivially reversible: an attacker who compromises a
41
* derived key can "decrypt" it to get back to the master key, then derive any
42
* other key. For all new code, use HKDF instead.
43
*
44
* The master key must be at least as long as the derived key. If the master
45
* key is longer, then only the first 'derived_keysize' bytes are used.
46
*/
47
static int derive_key_aes(const u8 *master_key,
48
const u8 nonce[FSCRYPT_FILE_NONCE_SIZE],
49
u8 *derived_key, unsigned int derived_keysize)
50
{
51
struct crypto_sync_skcipher *tfm;
52
int err;
53
54
tfm = crypto_alloc_sync_skcipher("ecb(aes)", 0, FSCRYPT_CRYPTOAPI_MASK);
55
if (IS_ERR(tfm))
56
return PTR_ERR(tfm);
57
58
err = crypto_sync_skcipher_setkey(tfm, nonce, FSCRYPT_FILE_NONCE_SIZE);
59
if (err == 0) {
60
SYNC_SKCIPHER_REQUEST_ON_STACK(req, tfm);
61
struct scatterlist src_sg, dst_sg;
62
63
skcipher_request_set_callback(req,
64
CRYPTO_TFM_REQ_MAY_BACKLOG |
65
CRYPTO_TFM_REQ_MAY_SLEEP,
66
NULL, NULL);
67
sg_init_one(&src_sg, master_key, derived_keysize);
68
sg_init_one(&dst_sg, derived_key, derived_keysize);
69
skcipher_request_set_crypt(req, &src_sg, &dst_sg,
70
derived_keysize, NULL);
71
err = crypto_skcipher_encrypt(req);
72
}
73
crypto_free_sync_skcipher(tfm);
74
return err;
75
}
76
77
/*
78
* Search the current task's subscribed keyrings for a "logon" key with
79
* description prefix:descriptor, and if found acquire a read lock on it and
80
* return a pointer to its validated payload in *payload_ret.
81
*/
82
static struct key *
83
find_and_lock_process_key(const char *prefix,
84
const u8 descriptor[FSCRYPT_KEY_DESCRIPTOR_SIZE],
85
unsigned int min_keysize,
86
const struct fscrypt_key **payload_ret)
87
{
88
char *description;
89
struct key *key;
90
const struct user_key_payload *ukp;
91
const struct fscrypt_key *payload;
92
93
description = kasprintf(GFP_KERNEL, "%s%*phN", prefix,
94
FSCRYPT_KEY_DESCRIPTOR_SIZE, descriptor);
95
if (!description)
96
return ERR_PTR(-ENOMEM);
97
98
key = request_key(&key_type_logon, description, NULL);
99
kfree(description);
100
if (IS_ERR(key))
101
return key;
102
103
down_read(&key->sem);
104
ukp = user_key_payload_locked(key);
105
106
if (!ukp) /* was the key revoked before we acquired its semaphore? */
107
goto invalid;
108
109
payload = (const struct fscrypt_key *)ukp->data;
110
111
if (ukp->datalen != sizeof(struct fscrypt_key) ||
112
payload->size < 1 || payload->size > sizeof(payload->raw)) {
113
fscrypt_warn(NULL,
114
"key with description '%s' has invalid payload",
115
key->description);
116
goto invalid;
117
}
118
119
if (payload->size < min_keysize) {
120
fscrypt_warn(NULL,
121
"key with description '%s' is too short (got %u bytes, need %u+ bytes)",
122
key->description, payload->size, min_keysize);
123
goto invalid;
124
}
125
126
*payload_ret = payload;
127
return key;
128
129
invalid:
130
up_read(&key->sem);
131
key_put(key);
132
return ERR_PTR(-ENOKEY);
133
}
134
135
/* Master key referenced by DIRECT_KEY policy */
136
struct fscrypt_direct_key {
137
struct super_block *dk_sb;
138
struct hlist_node dk_node;
139
refcount_t dk_refcount;
140
const struct fscrypt_mode *dk_mode;
141
struct fscrypt_prepared_key dk_key;
142
u8 dk_descriptor[FSCRYPT_KEY_DESCRIPTOR_SIZE];
143
u8 dk_raw[FSCRYPT_MAX_RAW_KEY_SIZE];
144
};
145
146
static void free_direct_key(struct fscrypt_direct_key *dk)
147
{
148
if (dk) {
149
fscrypt_destroy_prepared_key(dk->dk_sb, &dk->dk_key);
150
kfree_sensitive(dk);
151
}
152
}
153
154
void fscrypt_put_direct_key(struct fscrypt_direct_key *dk)
155
{
156
if (!refcount_dec_and_lock(&dk->dk_refcount, &fscrypt_direct_keys_lock))
157
return;
158
hash_del(&dk->dk_node);
159
spin_unlock(&fscrypt_direct_keys_lock);
160
161
free_direct_key(dk);
162
}
163
164
/*
165
* Find/insert the given key into the fscrypt_direct_keys table. If found, it
166
* is returned with elevated refcount, and 'to_insert' is freed if non-NULL. If
167
* not found, 'to_insert' is inserted and returned if it's non-NULL; otherwise
168
* NULL is returned.
169
*/
170
static struct fscrypt_direct_key *
171
find_or_insert_direct_key(struct fscrypt_direct_key *to_insert,
172
const u8 *raw_key,
173
const struct fscrypt_inode_info *ci)
174
{
175
unsigned long hash_key;
176
struct fscrypt_direct_key *dk;
177
178
/*
179
* Careful: to avoid potentially leaking secret key bytes via timing
180
* information, we must key the hash table by descriptor rather than by
181
* raw key, and use crypto_memneq() when comparing raw keys.
182
*/
183
184
BUILD_BUG_ON(sizeof(hash_key) > FSCRYPT_KEY_DESCRIPTOR_SIZE);
185
memcpy(&hash_key, ci->ci_policy.v1.master_key_descriptor,
186
sizeof(hash_key));
187
188
spin_lock(&fscrypt_direct_keys_lock);
189
hash_for_each_possible(fscrypt_direct_keys, dk, dk_node, hash_key) {
190
if (memcmp(ci->ci_policy.v1.master_key_descriptor,
191
dk->dk_descriptor, FSCRYPT_KEY_DESCRIPTOR_SIZE) != 0)
192
continue;
193
if (ci->ci_mode != dk->dk_mode)
194
continue;
195
if (!fscrypt_is_key_prepared(&dk->dk_key, ci))
196
continue;
197
if (crypto_memneq(raw_key, dk->dk_raw, ci->ci_mode->keysize))
198
continue;
199
/* using existing tfm with same (descriptor, mode, raw_key) */
200
refcount_inc(&dk->dk_refcount);
201
spin_unlock(&fscrypt_direct_keys_lock);
202
free_direct_key(to_insert);
203
return dk;
204
}
205
if (to_insert)
206
hash_add(fscrypt_direct_keys, &to_insert->dk_node, hash_key);
207
spin_unlock(&fscrypt_direct_keys_lock);
208
return to_insert;
209
}
210
211
/* Prepare to encrypt directly using the master key in the given mode */
212
static struct fscrypt_direct_key *
213
fscrypt_get_direct_key(const struct fscrypt_inode_info *ci, const u8 *raw_key)
214
{
215
struct fscrypt_direct_key *dk;
216
int err;
217
218
/* Is there already a tfm for this key? */
219
dk = find_or_insert_direct_key(NULL, raw_key, ci);
220
if (dk)
221
return dk;
222
223
/* Nope, allocate one. */
224
dk = kzalloc(sizeof(*dk), GFP_KERNEL);
225
if (!dk)
226
return ERR_PTR(-ENOMEM);
227
dk->dk_sb = ci->ci_inode->i_sb;
228
refcount_set(&dk->dk_refcount, 1);
229
dk->dk_mode = ci->ci_mode;
230
err = fscrypt_prepare_key(&dk->dk_key, raw_key, ci);
231
if (err)
232
goto err_free_dk;
233
memcpy(dk->dk_descriptor, ci->ci_policy.v1.master_key_descriptor,
234
FSCRYPT_KEY_DESCRIPTOR_SIZE);
235
memcpy(dk->dk_raw, raw_key, ci->ci_mode->keysize);
236
237
return find_or_insert_direct_key(dk, raw_key, ci);
238
239
err_free_dk:
240
free_direct_key(dk);
241
return ERR_PTR(err);
242
}
243
244
/* v1 policy, DIRECT_KEY: use the master key directly */
245
static int setup_v1_file_key_direct(struct fscrypt_inode_info *ci,
246
const u8 *raw_master_key)
247
{
248
struct fscrypt_direct_key *dk;
249
250
dk = fscrypt_get_direct_key(ci, raw_master_key);
251
if (IS_ERR(dk))
252
return PTR_ERR(dk);
253
ci->ci_direct_key = dk;
254
ci->ci_enc_key = dk->dk_key;
255
return 0;
256
}
257
258
/* v1 policy, !DIRECT_KEY: derive the file's encryption key */
259
static int setup_v1_file_key_derived(struct fscrypt_inode_info *ci,
260
const u8 *raw_master_key)
261
{
262
u8 *derived_key;
263
int err;
264
265
/*
266
* This cannot be a stack buffer because it will be passed to the
267
* scatterlist crypto API during derive_key_aes().
268
*/
269
derived_key = kmalloc(ci->ci_mode->keysize, GFP_KERNEL);
270
if (!derived_key)
271
return -ENOMEM;
272
273
err = derive_key_aes(raw_master_key, ci->ci_nonce,
274
derived_key, ci->ci_mode->keysize);
275
if (err)
276
goto out;
277
278
err = fscrypt_set_per_file_enc_key(ci, derived_key);
279
out:
280
kfree_sensitive(derived_key);
281
return err;
282
}
283
284
int fscrypt_setup_v1_file_key(struct fscrypt_inode_info *ci,
285
const u8 *raw_master_key)
286
{
287
if (ci->ci_policy.v1.flags & FSCRYPT_POLICY_FLAG_DIRECT_KEY)
288
return setup_v1_file_key_direct(ci, raw_master_key);
289
else
290
return setup_v1_file_key_derived(ci, raw_master_key);
291
}
292
293
int
294
fscrypt_setup_v1_file_key_via_subscribed_keyrings(struct fscrypt_inode_info *ci)
295
{
296
const struct super_block *sb = ci->ci_inode->i_sb;
297
struct key *key;
298
const struct fscrypt_key *payload;
299
int err;
300
301
key = find_and_lock_process_key(FSCRYPT_KEY_DESC_PREFIX,
302
ci->ci_policy.v1.master_key_descriptor,
303
ci->ci_mode->keysize, &payload);
304
if (key == ERR_PTR(-ENOKEY) && sb->s_cop->legacy_key_prefix) {
305
key = find_and_lock_process_key(sb->s_cop->legacy_key_prefix,
306
ci->ci_policy.v1.master_key_descriptor,
307
ci->ci_mode->keysize, &payload);
308
}
309
if (IS_ERR(key))
310
return PTR_ERR(key);
311
312
err = fscrypt_setup_v1_file_key(ci, payload->raw);
313
up_read(&key->sem);
314
key_put(key);
315
return err;
316
}
317
318