1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23#include <crypto/algapi.h>
24#include <crypto/skcipher.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
32static DEFINE_HASHTABLE(fscrypt_direct_keys, 6);
33static DEFINE_SPINLOCK(fscrypt_direct_keys_lock);
34
35
36
37
38
39
40
41
42
43
44
45
46
47static 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 int res = 0;
52 struct skcipher_request *req = NULL;
53 DECLARE_CRYPTO_WAIT(wait);
54 struct scatterlist src_sg, dst_sg;
55 struct crypto_skcipher *tfm = crypto_alloc_skcipher("ecb(aes)", 0, 0);
56
57 if (IS_ERR(tfm)) {
58 res = PTR_ERR(tfm);
59 tfm = NULL;
60 goto out;
61 }
62 crypto_skcipher_set_flags(tfm, CRYPTO_TFM_REQ_FORBID_WEAK_KEYS);
63 req = skcipher_request_alloc(tfm, GFP_KERNEL);
64 if (!req) {
65 res = -ENOMEM;
66 goto out;
67 }
68 skcipher_request_set_callback(req,
69 CRYPTO_TFM_REQ_MAY_BACKLOG | CRYPTO_TFM_REQ_MAY_SLEEP,
70 crypto_req_done, &wait);
71 res = crypto_skcipher_setkey(tfm, nonce, FSCRYPT_FILE_NONCE_SIZE);
72 if (res < 0)
73 goto out;
74
75 sg_init_one(&src_sg, master_key, derived_keysize);
76 sg_init_one(&dst_sg, derived_key, derived_keysize);
77 skcipher_request_set_crypt(req, &src_sg, &dst_sg, derived_keysize,
78 NULL);
79 res = crypto_wait_req(crypto_skcipher_encrypt(req), &wait);
80out:
81 skcipher_request_free(req);
82 crypto_free_skcipher(tfm);
83 return res;
84}
85
86
87
88
89
90
91static struct key *
92find_and_lock_process_key(const char *prefix,
93 const u8 descriptor[FSCRYPT_KEY_DESCRIPTOR_SIZE],
94 unsigned int min_keysize,
95 const struct fscrypt_key **payload_ret)
96{
97 char *description;
98 struct key *key;
99 const struct user_key_payload *ukp;
100 const struct fscrypt_key *payload;
101
102 description = kasprintf(GFP_KERNEL, "%s%*phN", prefix,
103 FSCRYPT_KEY_DESCRIPTOR_SIZE, descriptor);
104 if (!description)
105 return ERR_PTR(-ENOMEM);
106
107 key = request_key(&key_type_logon, description, NULL);
108 kfree(description);
109 if (IS_ERR(key))
110 return key;
111
112 down_read(&key->sem);
113 ukp = user_key_payload_locked(key);
114
115 if (!ukp)
116 goto invalid;
117
118 payload = (const struct fscrypt_key *)ukp->data;
119
120 if (ukp->datalen != sizeof(struct fscrypt_key) ||
121 payload->size < 1 || payload->size > FSCRYPT_MAX_KEY_SIZE) {
122 fscrypt_warn(NULL,
123 "key with description '%s' has invalid payload",
124 key->description);
125 goto invalid;
126 }
127
128 if (payload->size < min_keysize) {
129 fscrypt_warn(NULL,
130 "key with description '%s' is too short (got %u bytes, need %u+ bytes)",
131 key->description, payload->size, min_keysize);
132 goto invalid;
133 }
134
135 *payload_ret = payload;
136 return key;
137
138invalid:
139 up_read(&key->sem);
140 key_put(key);
141 return ERR_PTR(-ENOKEY);
142}
143
144
145struct fscrypt_direct_key {
146 struct hlist_node dk_node;
147 refcount_t dk_refcount;
148 const struct fscrypt_mode *dk_mode;
149 struct fscrypt_prepared_key dk_key;
150 u8 dk_descriptor[FSCRYPT_KEY_DESCRIPTOR_SIZE];
151 u8 dk_raw[FSCRYPT_MAX_KEY_SIZE];
152};
153
154static void free_direct_key(struct fscrypt_direct_key *dk)
155{
156 if (dk) {
157 fscrypt_destroy_prepared_key(&dk->dk_key);
158 kfree_sensitive(dk);
159 }
160}
161
162void fscrypt_put_direct_key(struct fscrypt_direct_key *dk)
163{
164 if (!refcount_dec_and_lock(&dk->dk_refcount, &fscrypt_direct_keys_lock))
165 return;
166 hash_del(&dk->dk_node);
167 spin_unlock(&fscrypt_direct_keys_lock);
168
169 free_direct_key(dk);
170}
171
172
173
174
175
176
177
178static struct fscrypt_direct_key *
179find_or_insert_direct_key(struct fscrypt_direct_key *to_insert,
180 const u8 *raw_key, const struct fscrypt_info *ci)
181{
182 unsigned long hash_key;
183 struct fscrypt_direct_key *dk;
184
185
186
187
188
189
190
191 BUILD_BUG_ON(sizeof(hash_key) > FSCRYPT_KEY_DESCRIPTOR_SIZE);
192 memcpy(&hash_key, ci->ci_policy.v1.master_key_descriptor,
193 sizeof(hash_key));
194
195 spin_lock(&fscrypt_direct_keys_lock);
196 hash_for_each_possible(fscrypt_direct_keys, dk, dk_node, hash_key) {
197 if (memcmp(ci->ci_policy.v1.master_key_descriptor,
198 dk->dk_descriptor, FSCRYPT_KEY_DESCRIPTOR_SIZE) != 0)
199 continue;
200 if (ci->ci_mode != dk->dk_mode)
201 continue;
202 if (!fscrypt_is_key_prepared(&dk->dk_key, ci))
203 continue;
204 if (crypto_memneq(raw_key, dk->dk_raw, ci->ci_mode->keysize))
205 continue;
206
207 refcount_inc(&dk->dk_refcount);
208 spin_unlock(&fscrypt_direct_keys_lock);
209 free_direct_key(to_insert);
210 return dk;
211 }
212 if (to_insert)
213 hash_add(fscrypt_direct_keys, &to_insert->dk_node, hash_key);
214 spin_unlock(&fscrypt_direct_keys_lock);
215 return to_insert;
216}
217
218
219static struct fscrypt_direct_key *
220fscrypt_get_direct_key(const struct fscrypt_info *ci, const u8 *raw_key)
221{
222 struct fscrypt_direct_key *dk;
223 int err;
224
225
226 dk = find_or_insert_direct_key(NULL, raw_key, ci);
227 if (dk)
228 return dk;
229
230
231 dk = kzalloc(sizeof(*dk), GFP_KERNEL);
232 if (!dk)
233 return ERR_PTR(-ENOMEM);
234 refcount_set(&dk->dk_refcount, 1);
235 dk->dk_mode = ci->ci_mode;
236 err = fscrypt_prepare_key(&dk->dk_key, raw_key, ci);
237 if (err)
238 goto err_free_dk;
239 memcpy(dk->dk_descriptor, ci->ci_policy.v1.master_key_descriptor,
240 FSCRYPT_KEY_DESCRIPTOR_SIZE);
241 memcpy(dk->dk_raw, raw_key, ci->ci_mode->keysize);
242
243 return find_or_insert_direct_key(dk, raw_key, ci);
244
245err_free_dk:
246 free_direct_key(dk);
247 return ERR_PTR(err);
248}
249
250
251static int setup_v1_file_key_direct(struct fscrypt_info *ci,
252 const u8 *raw_master_key)
253{
254 struct fscrypt_direct_key *dk;
255
256 dk = fscrypt_get_direct_key(ci, raw_master_key);
257 if (IS_ERR(dk))
258 return PTR_ERR(dk);
259 ci->ci_direct_key = dk;
260 ci->ci_enc_key = dk->dk_key;
261 return 0;
262}
263
264
265static int setup_v1_file_key_derived(struct fscrypt_info *ci,
266 const u8 *raw_master_key)
267{
268 u8 *derived_key;
269 int err;
270
271
272
273
274
275 derived_key = kmalloc(ci->ci_mode->keysize, GFP_KERNEL);
276 if (!derived_key)
277 return -ENOMEM;
278
279 err = derive_key_aes(raw_master_key, ci->ci_nonce,
280 derived_key, ci->ci_mode->keysize);
281 if (err)
282 goto out;
283
284 err = fscrypt_set_per_file_enc_key(ci, derived_key);
285out:
286 kfree_sensitive(derived_key);
287 return err;
288}
289
290int fscrypt_setup_v1_file_key(struct fscrypt_info *ci, const u8 *raw_master_key)
291{
292 if (ci->ci_policy.v1.flags & FSCRYPT_POLICY_FLAG_DIRECT_KEY)
293 return setup_v1_file_key_direct(ci, raw_master_key);
294 else
295 return setup_v1_file_key_derived(ci, raw_master_key);
296}
297
298int fscrypt_setup_v1_file_key_via_subscribed_keyrings(struct fscrypt_info *ci)
299{
300 struct key *key;
301 const struct fscrypt_key *payload;
302 int err;
303
304 key = find_and_lock_process_key(FSCRYPT_KEY_DESC_PREFIX,
305 ci->ci_policy.v1.master_key_descriptor,
306 ci->ci_mode->keysize, &payload);
307 if (key == ERR_PTR(-ENOKEY) && ci->ci_inode->i_sb->s_cop->key_prefix) {
308 key = find_and_lock_process_key(ci->ci_inode->i_sb->s_cop->key_prefix,
309 ci->ci_policy.v1.master_key_descriptor,
310 ci->ci_mode->keysize, &payload);
311 }
312 if (IS_ERR(key))
313 return PTR_ERR(key);
314
315 err = fscrypt_setup_v1_file_key(ci, payload->raw);
316 up_read(&key->sem);
317 key_put(key);
318 return err;
319}
320