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