• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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/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 /* 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 = 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);
80 out:
81 	skcipher_request_free(req);
82 	crypto_free_skcipher(tfm);
83 	return res;
84 }
85 
86 /*
87  * Search the current task's subscribed keyrings for a "logon" key with
88  * description prefix:descriptor, and if found acquire a read lock on it and
89  * return a pointer to its validated payload in *payload_ret.
90  */
91 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 find_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) /* was the key revoked before we acquired its semaphore? */
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 ||
122 	    payload->size > FSCRYPT_MAX_STANDARD_KEY_SIZE) {
123 		fscrypt_warn(NULL,
124 			     "key with description '%s' has invalid payload",
125 			     key->description);
126 		goto invalid;
127 	}
128 
129 	if (payload->size < min_keysize) {
130 		fscrypt_warn(NULL,
131 			     "key with description '%s' is too short (got %u bytes, need %u+ bytes)",
132 			     key->description, payload->size, min_keysize);
133 		goto invalid;
134 	}
135 
136 	*payload_ret = payload;
137 	return key;
138 
139 invalid:
140 	up_read(&key->sem);
141 	key_put(key);
142 	return ERR_PTR(-ENOKEY);
143 }
144 
145 /* Master key referenced by DIRECT_KEY policy */
146 struct fscrypt_direct_key {
147 	struct super_block		*dk_sb;
148 	struct hlist_node		dk_node;
149 	refcount_t			dk_refcount;
150 	const struct fscrypt_mode	*dk_mode;
151 	struct fscrypt_prepared_key	dk_key;
152 	u8				dk_descriptor[FSCRYPT_KEY_DESCRIPTOR_SIZE];
153 	u8				dk_raw[FSCRYPT_MAX_STANDARD_KEY_SIZE];
154 };
155 
free_direct_key(struct fscrypt_direct_key * dk)156 static void free_direct_key(struct fscrypt_direct_key *dk)
157 {
158 	if (dk) {
159 		fscrypt_destroy_prepared_key(dk->dk_sb, &dk->dk_key);
160 		kfree_sensitive(dk);
161 	}
162 }
163 
fscrypt_put_direct_key(struct fscrypt_direct_key * dk)164 void fscrypt_put_direct_key(struct fscrypt_direct_key *dk)
165 {
166 	if (!refcount_dec_and_lock(&dk->dk_refcount, &fscrypt_direct_keys_lock))
167 		return;
168 	hash_del(&dk->dk_node);
169 	spin_unlock(&fscrypt_direct_keys_lock);
170 
171 	free_direct_key(dk);
172 }
173 
174 /*
175  * Find/insert the given key into the fscrypt_direct_keys table.  If found, it
176  * is returned with elevated refcount, and 'to_insert' is freed if non-NULL.  If
177  * not found, 'to_insert' is inserted and returned if it's non-NULL; otherwise
178  * NULL is returned.
179  */
180 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)181 find_or_insert_direct_key(struct fscrypt_direct_key *to_insert,
182 			  const u8 *raw_key,
183 			  const struct fscrypt_inode_info *ci)
184 {
185 	unsigned long hash_key;
186 	struct fscrypt_direct_key *dk;
187 
188 	/*
189 	 * Careful: to avoid potentially leaking secret key bytes via timing
190 	 * information, we must key the hash table by descriptor rather than by
191 	 * raw key, and use crypto_memneq() when comparing raw keys.
192 	 */
193 
194 	BUILD_BUG_ON(sizeof(hash_key) > FSCRYPT_KEY_DESCRIPTOR_SIZE);
195 	memcpy(&hash_key, ci->ci_policy.v1.master_key_descriptor,
196 	       sizeof(hash_key));
197 
198 	spin_lock(&fscrypt_direct_keys_lock);
199 	hash_for_each_possible(fscrypt_direct_keys, dk, dk_node, hash_key) {
200 		if (memcmp(ci->ci_policy.v1.master_key_descriptor,
201 			   dk->dk_descriptor, FSCRYPT_KEY_DESCRIPTOR_SIZE) != 0)
202 			continue;
203 		if (ci->ci_mode != dk->dk_mode)
204 			continue;
205 		if (!fscrypt_is_key_prepared(&dk->dk_key, ci))
206 			continue;
207 		if (crypto_memneq(raw_key, dk->dk_raw, ci->ci_mode->keysize))
208 			continue;
209 		/* using existing tfm with same (descriptor, mode, raw_key) */
210 		refcount_inc(&dk->dk_refcount);
211 		spin_unlock(&fscrypt_direct_keys_lock);
212 		free_direct_key(to_insert);
213 		return dk;
214 	}
215 	if (to_insert)
216 		hash_add(fscrypt_direct_keys, &to_insert->dk_node, hash_key);
217 	spin_unlock(&fscrypt_direct_keys_lock);
218 	return to_insert;
219 }
220 
221 /* Prepare to encrypt directly using the master key in the given mode */
222 static struct fscrypt_direct_key *
fscrypt_get_direct_key(const struct fscrypt_inode_info * ci,const u8 * raw_key)223 fscrypt_get_direct_key(const struct fscrypt_inode_info *ci, const u8 *raw_key)
224 {
225 	struct fscrypt_direct_key *dk;
226 	int err;
227 
228 	/* Is there already a tfm for this key? */
229 	dk = find_or_insert_direct_key(NULL, raw_key, ci);
230 	if (dk)
231 		return dk;
232 
233 	/* Nope, allocate one. */
234 	dk = kzalloc(sizeof(*dk), GFP_KERNEL);
235 	if (!dk)
236 		return ERR_PTR(-ENOMEM);
237 	dk->dk_sb = ci->ci_inode->i_sb;
238 	refcount_set(&dk->dk_refcount, 1);
239 	dk->dk_mode = ci->ci_mode;
240 	err = fscrypt_prepare_key(&dk->dk_key, raw_key, ci);
241 	if (err)
242 		goto err_free_dk;
243 	memcpy(dk->dk_descriptor, ci->ci_policy.v1.master_key_descriptor,
244 	       FSCRYPT_KEY_DESCRIPTOR_SIZE);
245 	memcpy(dk->dk_raw, raw_key, ci->ci_mode->keysize);
246 
247 	return find_or_insert_direct_key(dk, raw_key, ci);
248 
249 err_free_dk:
250 	free_direct_key(dk);
251 	return ERR_PTR(err);
252 }
253 
254 /* v1 policy, DIRECT_KEY: use the master key directly */
setup_v1_file_key_direct(struct fscrypt_inode_info * ci,const u8 * raw_master_key)255 static int setup_v1_file_key_direct(struct fscrypt_inode_info *ci,
256 				    const u8 *raw_master_key)
257 {
258 	struct fscrypt_direct_key *dk;
259 
260 	dk = fscrypt_get_direct_key(ci, raw_master_key);
261 	if (IS_ERR(dk))
262 		return PTR_ERR(dk);
263 	ci->ci_direct_key = dk;
264 	ci->ci_enc_key = dk->dk_key;
265 	return 0;
266 }
267 
268 /* 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)269 static int setup_v1_file_key_derived(struct fscrypt_inode_info *ci,
270 				     const u8 *raw_master_key)
271 {
272 	u8 *derived_key;
273 	int err;
274 
275 	/*
276 	 * This cannot be a stack buffer because it will be passed to the
277 	 * scatterlist crypto API during derive_key_aes().
278 	 */
279 	derived_key = kmalloc(ci->ci_mode->keysize, GFP_KERNEL);
280 	if (!derived_key)
281 		return -ENOMEM;
282 
283 	err = derive_key_aes(raw_master_key, ci->ci_nonce,
284 			     derived_key, ci->ci_mode->keysize);
285 	if (err)
286 		goto out;
287 
288 	err = fscrypt_set_per_file_enc_key(ci, derived_key);
289 out:
290 	kfree_sensitive(derived_key);
291 	return err;
292 }
293 
fscrypt_setup_v1_file_key(struct fscrypt_inode_info * ci,const u8 * raw_master_key)294 int fscrypt_setup_v1_file_key(struct fscrypt_inode_info *ci,
295 			      const u8 *raw_master_key)
296 {
297 	if (ci->ci_policy.v1.flags & FSCRYPT_POLICY_FLAG_DIRECT_KEY)
298 		return setup_v1_file_key_direct(ci, raw_master_key);
299 	else
300 		return setup_v1_file_key_derived(ci, raw_master_key);
301 }
302 
303 int
fscrypt_setup_v1_file_key_via_subscribed_keyrings(struct fscrypt_inode_info * ci)304 fscrypt_setup_v1_file_key_via_subscribed_keyrings(struct fscrypt_inode_info *ci)
305 {
306 	const struct super_block *sb = ci->ci_inode->i_sb;
307 	struct key *key;
308 	const struct fscrypt_key *payload;
309 	int err;
310 
311 	key = find_and_lock_process_key(FSCRYPT_KEY_DESC_PREFIX,
312 					ci->ci_policy.v1.master_key_descriptor,
313 					ci->ci_mode->keysize, &payload);
314 	if (key == ERR_PTR(-ENOKEY) && sb->s_cop->legacy_key_prefix) {
315 		key = find_and_lock_process_key(sb->s_cop->legacy_key_prefix,
316 						ci->ci_policy.v1.master_key_descriptor,
317 						ci->ci_mode->keysize, &payload);
318 	}
319 	if (IS_ERR(key))
320 		return PTR_ERR(key);
321 
322 	err = fscrypt_setup_v1_file_key(ci, payload->raw);
323 	up_read(&key->sem);
324 	key_put(key);
325 	return err;
326 }
327