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