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  */
derive_key_aes(const u8 * master_key,const u8 nonce[FSCRYPT_FILE_NONCE_SIZE],u8 * derived_key,unsigned int derived_keysize)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 	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 =
56 		crypto_alloc_skcipher("ecb(aes)", 0, FSCRYPT_CRYPTOAPI_MASK);
57 
58 	if (IS_ERR(tfm)) {
59 		res = PTR_ERR(tfm);
60 		tfm = NULL;
61 		goto out;
62 	}
63 	crypto_skcipher_set_flags(tfm, CRYPTO_TFM_REQ_FORBID_WEAK_KEYS);
64 	req = skcipher_request_alloc(tfm, GFP_KERNEL);
65 	if (!req) {
66 		res = -ENOMEM;
67 		goto out;
68 	}
69 	skcipher_request_set_callback(req,
70 			CRYPTO_TFM_REQ_MAY_BACKLOG | CRYPTO_TFM_REQ_MAY_SLEEP,
71 			crypto_req_done, &wait);
72 	res = crypto_skcipher_setkey(tfm, nonce, FSCRYPT_FILE_NONCE_SIZE);
73 	if (res < 0)
74 		goto out;
75 
76 	sg_init_one(&src_sg, master_key, derived_keysize);
77 	sg_init_one(&dst_sg, derived_key, derived_keysize);
78 	skcipher_request_set_crypt(req, &src_sg, &dst_sg, derived_keysize,
79 				   NULL);
80 	res = crypto_wait_req(crypto_skcipher_encrypt(req), &wait);
81 out:
82 	skcipher_request_free(req);
83 	crypto_free_skcipher(tfm);
84 	return res;
85 }
86 
87 /*
88  * Search the current task's subscribed keyrings for a "logon" key with
89  * description prefix:descriptor, and if found acquire a read lock on it and
90  * return a pointer to its validated payload in *payload_ret.
91  */
92 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)93 find_and_lock_process_key(const char *prefix,
94 			  const u8 descriptor[FSCRYPT_KEY_DESCRIPTOR_SIZE],
95 			  unsigned int min_keysize,
96 			  const struct fscrypt_key **payload_ret)
97 {
98 	char *description;
99 	struct key *key;
100 	const struct user_key_payload *ukp;
101 	const struct fscrypt_key *payload;
102 
103 	description = kasprintf(GFP_KERNEL, "%s%*phN", prefix,
104 				FSCRYPT_KEY_DESCRIPTOR_SIZE, descriptor);
105 	if (!description)
106 		return ERR_PTR(-ENOMEM);
107 
108 	key = request_key(&key_type_logon, description, NULL);
109 	kfree(description);
110 	if (IS_ERR(key))
111 		return key;
112 
113 	down_read(&key->sem);
114 	ukp = user_key_payload_locked(key);
115 
116 	if (!ukp) /* was the key revoked before we acquired its semaphore? */
117 		goto invalid;
118 
119 	payload = (const struct fscrypt_key *)ukp->data;
120 
121 	if (ukp->datalen != sizeof(struct fscrypt_key) ||
122 	    payload->size < 1 ||
123 	    payload->size > FSCRYPT_MAX_STANDARD_KEY_SIZE) {
124 		fscrypt_warn(NULL,
125 			     "key with description '%s' has invalid payload",
126 			     key->description);
127 		goto invalid;
128 	}
129 
130 	if (payload->size < min_keysize) {
131 		fscrypt_warn(NULL,
132 			     "key with description '%s' is too short (got %u bytes, need %u+ bytes)",
133 			     key->description, payload->size, min_keysize);
134 		goto invalid;
135 	}
136 
137 	*payload_ret = payload;
138 	return key;
139 
140 invalid:
141 	up_read(&key->sem);
142 	key_put(key);
143 	return ERR_PTR(-ENOKEY);
144 }
145 
146 /* Master key referenced by DIRECT_KEY policy */
147 struct fscrypt_direct_key {
148 	struct super_block		*dk_sb;
149 	struct hlist_node		dk_node;
150 	refcount_t			dk_refcount;
151 	const struct fscrypt_mode	*dk_mode;
152 	struct fscrypt_prepared_key	dk_key;
153 	u8				dk_descriptor[FSCRYPT_KEY_DESCRIPTOR_SIZE];
154 	u8				dk_raw[FSCRYPT_MAX_STANDARD_KEY_SIZE];
155 };
156 
free_direct_key(struct fscrypt_direct_key * dk)157 static void free_direct_key(struct fscrypt_direct_key *dk)
158 {
159 	if (dk) {
160 		fscrypt_destroy_prepared_key(dk->dk_sb, &dk->dk_key);
161 		kfree_sensitive(dk);
162 	}
163 }
164 
fscrypt_put_direct_key(struct fscrypt_direct_key * dk)165 void fscrypt_put_direct_key(struct fscrypt_direct_key *dk)
166 {
167 	if (!refcount_dec_and_lock(&dk->dk_refcount, &fscrypt_direct_keys_lock))
168 		return;
169 	hash_del(&dk->dk_node);
170 	spin_unlock(&fscrypt_direct_keys_lock);
171 
172 	free_direct_key(dk);
173 }
174 
175 /*
176  * Find/insert the given key into the fscrypt_direct_keys table.  If found, it
177  * is returned with elevated refcount, and 'to_insert' is freed if non-NULL.  If
178  * not found, 'to_insert' is inserted and returned if it's non-NULL; otherwise
179  * NULL is returned.
180  */
181 static struct fscrypt_direct_key *
find_or_insert_direct_key(struct fscrypt_direct_key * to_insert,const u8 * raw_key,const struct fscrypt_inode_info * ci)182 find_or_insert_direct_key(struct fscrypt_direct_key *to_insert,
183 			  const u8 *raw_key,
184 			  const struct fscrypt_inode_info *ci)
185 {
186 	unsigned long hash_key;
187 	struct fscrypt_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) > FSCRYPT_KEY_DESCRIPTOR_SIZE);
196 	memcpy(&hash_key, ci->ci_policy.v1.master_key_descriptor,
197 	       sizeof(hash_key));
198 
199 	spin_lock(&fscrypt_direct_keys_lock);
200 	hash_for_each_possible(fscrypt_direct_keys, dk, dk_node, hash_key) {
201 		if (memcmp(ci->ci_policy.v1.master_key_descriptor,
202 			   dk->dk_descriptor, FSCRYPT_KEY_DESCRIPTOR_SIZE) != 0)
203 			continue;
204 		if (ci->ci_mode != dk->dk_mode)
205 			continue;
206 		if (!fscrypt_is_key_prepared(&dk->dk_key, ci))
207 			continue;
208 		if (crypto_memneq(raw_key, dk->dk_raw, ci->ci_mode->keysize))
209 			continue;
210 		/* using existing tfm with same (descriptor, mode, raw_key) */
211 		refcount_inc(&dk->dk_refcount);
212 		spin_unlock(&fscrypt_direct_keys_lock);
213 		free_direct_key(to_insert);
214 		return dk;
215 	}
216 	if (to_insert)
217 		hash_add(fscrypt_direct_keys, &to_insert->dk_node, hash_key);
218 	spin_unlock(&fscrypt_direct_keys_lock);
219 	return to_insert;
220 }
221 
222 /* Prepare to encrypt directly using the master key in the given mode */
223 static struct fscrypt_direct_key *
fscrypt_get_direct_key(const struct fscrypt_inode_info * ci,const u8 * raw_key)224 fscrypt_get_direct_key(const struct fscrypt_inode_info *ci, const u8 *raw_key)
225 {
226 	struct fscrypt_direct_key *dk;
227 	int err;
228 
229 	/* Is there already a tfm for this key? */
230 	dk = find_or_insert_direct_key(NULL, raw_key, ci);
231 	if (dk)
232 		return dk;
233 
234 	/* Nope, allocate one. */
235 	dk = kzalloc(sizeof(*dk), GFP_KERNEL);
236 	if (!dk)
237 		return ERR_PTR(-ENOMEM);
238 	dk->dk_sb = ci->ci_inode->i_sb;
239 	refcount_set(&dk->dk_refcount, 1);
240 	dk->dk_mode = ci->ci_mode;
241 	err = fscrypt_prepare_key(&dk->dk_key, raw_key, ci);
242 	if (err)
243 		goto err_free_dk;
244 	memcpy(dk->dk_descriptor, ci->ci_policy.v1.master_key_descriptor,
245 	       FSCRYPT_KEY_DESCRIPTOR_SIZE);
246 	memcpy(dk->dk_raw, raw_key, ci->ci_mode->keysize);
247 
248 	return find_or_insert_direct_key(dk, raw_key, ci);
249 
250 err_free_dk:
251 	free_direct_key(dk);
252 	return ERR_PTR(err);
253 }
254 
255 /* v1 policy, DIRECT_KEY: use the master key directly */
setup_v1_file_key_direct(struct fscrypt_inode_info * ci,const u8 * raw_master_key)256 static int setup_v1_file_key_direct(struct fscrypt_inode_info *ci,
257 				    const u8 *raw_master_key)
258 {
259 	struct fscrypt_direct_key *dk;
260 
261 	dk = fscrypt_get_direct_key(ci, raw_master_key);
262 	if (IS_ERR(dk))
263 		return PTR_ERR(dk);
264 	ci->ci_direct_key = dk;
265 	ci->ci_enc_key = dk->dk_key;
266 	return 0;
267 }
268 
269 /* v1 policy, !DIRECT_KEY: derive the file's encryption key */
setup_v1_file_key_derived(struct fscrypt_inode_info * ci,const u8 * raw_master_key)270 static int setup_v1_file_key_derived(struct fscrypt_inode_info *ci,
271 				     const u8 *raw_master_key)
272 {
273 	u8 *derived_key;
274 	int err;
275 
276 	/*
277 	 * This cannot be a stack buffer because it will be passed to the
278 	 * scatterlist crypto API during derive_key_aes().
279 	 */
280 	derived_key = kmalloc(ci->ci_mode->keysize, GFP_KERNEL);
281 	if (!derived_key)
282 		return -ENOMEM;
283 
284 	err = derive_key_aes(raw_master_key, ci->ci_nonce,
285 			     derived_key, ci->ci_mode->keysize);
286 	if (err)
287 		goto out;
288 
289 	err = fscrypt_set_per_file_enc_key(ci, derived_key);
290 out:
291 	kfree_sensitive(derived_key);
292 	return err;
293 }
294 
fscrypt_setup_v1_file_key(struct fscrypt_inode_info * ci,const u8 * raw_master_key)295 int fscrypt_setup_v1_file_key(struct fscrypt_inode_info *ci,
296 			      const u8 *raw_master_key)
297 {
298 	if (ci->ci_policy.v1.flags & FSCRYPT_POLICY_FLAG_DIRECT_KEY)
299 		return setup_v1_file_key_direct(ci, raw_master_key);
300 	else
301 		return setup_v1_file_key_derived(ci, raw_master_key);
302 }
303 
304 int
fscrypt_setup_v1_file_key_via_subscribed_keyrings(struct fscrypt_inode_info * ci)305 fscrypt_setup_v1_file_key_via_subscribed_keyrings(struct fscrypt_inode_info *ci)
306 {
307 	const struct super_block *sb = ci->ci_inode->i_sb;
308 	struct key *key;
309 	const struct fscrypt_key *payload;
310 	int err;
311 
312 	key = find_and_lock_process_key(FSCRYPT_KEY_DESC_PREFIX,
313 					ci->ci_policy.v1.master_key_descriptor,
314 					ci->ci_mode->keysize, &payload);
315 	if (key == ERR_PTR(-ENOKEY) && sb->s_cop->legacy_key_prefix) {
316 		key = find_and_lock_process_key(sb->s_cop->legacy_key_prefix,
317 						ci->ci_policy.v1.master_key_descriptor,
318 						ci->ci_mode->keysize, &payload);
319 	}
320 	if (IS_ERR(key))
321 		return PTR_ERR(key);
322 
323 	err = fscrypt_setup_v1_file_key(ci, payload->raw);
324 	up_read(&key->sem);
325 	key_put(key);
326 	return err;
327 }
328