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 /* The sb is used at eviction time, so it must be the same. */
151 if (ci->ci_inode->i_sb != dk->dk_sb)
152 continue;
153 if (ci->ci_mode != dk->dk_mode)
154 continue;
155 if (!fscrypt_is_key_prepared(&dk->dk_key, ci))
156 continue;
157 if (crypto_memneq(raw_key, dk->dk_raw, ci->ci_mode->keysize))
158 continue;
159 /*
160 * Use an existing prepared key with the same (descriptor, sb,
161 * mode, inlinecrypt, raw_key) combination.
162 */
163 refcount_inc(&dk->dk_refcount);
164 spin_unlock(&fscrypt_direct_keys_lock);
165 free_direct_key(to_insert);
166 return dk;
167 }
168 if (to_insert)
169 hash_add(fscrypt_direct_keys, &to_insert->dk_node, hash_key);
170 spin_unlock(&fscrypt_direct_keys_lock);
171 return to_insert;
172 }
173
174 /* Prepare to encrypt directly using the master key in the given mode */
175 static struct fscrypt_direct_key *
fscrypt_get_direct_key(const struct fscrypt_inode_info * ci,const u8 * raw_key)176 fscrypt_get_direct_key(const struct fscrypt_inode_info *ci, const u8 *raw_key)
177 {
178 struct fscrypt_direct_key *dk;
179 int err;
180
181 /* Is there already a tfm for this key? */
182 dk = find_or_insert_direct_key(NULL, raw_key, ci);
183 if (dk)
184 return dk;
185
186 /* Nope, allocate one. */
187 dk = kzalloc_obj(*dk);
188 if (!dk)
189 return ERR_PTR(-ENOMEM);
190 dk->dk_sb = ci->ci_inode->i_sb;
191 refcount_set(&dk->dk_refcount, 1);
192 dk->dk_mode = ci->ci_mode;
193 err = fscrypt_prepare_key(&dk->dk_key, raw_key, ci);
194 if (err)
195 goto err_free_dk;
196 memcpy(dk->dk_descriptor, ci->ci_policy.v1.master_key_descriptor,
197 FSCRYPT_KEY_DESCRIPTOR_SIZE);
198 memcpy(dk->dk_raw, raw_key, ci->ci_mode->keysize);
199
200 return find_or_insert_direct_key(dk, raw_key, ci);
201
202 err_free_dk:
203 free_direct_key(dk);
204 return ERR_PTR(err);
205 }
206
207 /* v1 policy, DIRECT_KEY: use the master key directly */
setup_v1_file_key_direct(struct fscrypt_inode_info * ci,const u8 * raw_master_key)208 static int setup_v1_file_key_direct(struct fscrypt_inode_info *ci,
209 const u8 *raw_master_key)
210 {
211 struct fscrypt_direct_key *dk;
212
213 dk = fscrypt_get_direct_key(ci, raw_master_key);
214 if (IS_ERR(dk))
215 return PTR_ERR(dk);
216 ci->ci_direct_key = dk;
217 ci->ci_enc_key = dk->dk_key;
218 return 0;
219 }
220
221 /*
222 * v1 policy, !DIRECT_KEY: derive the file's encryption key.
223 *
224 * The v1 key derivation function generates the derived key by encrypting the
225 * master key with AES-128-ECB using the file's nonce as the AES key. This
226 * provides a unique derived key with sufficient entropy for each inode.
227 * However, it's nonstandard, non-extensible, doesn't evenly distribute the
228 * entropy from the master key, and is trivially reversible: an attacker who
229 * compromises a derived key can "decrypt" it to get back to the master key,
230 * then derive any other key. For all new code, use HKDF instead.
231 *
232 * The master key must be at least as long as the derived key. If the master
233 * key is longer, then only the first ci->ci_mode->keysize bytes are used.
234 */
setup_v1_file_key_derived(struct fscrypt_inode_info * ci,const u8 * raw_master_key)235 static int setup_v1_file_key_derived(struct fscrypt_inode_info *ci,
236 const u8 *raw_master_key)
237 {
238 const unsigned int derived_keysize = ci->ci_mode->keysize;
239 u8 derived_key[FSCRYPT_MAX_RAW_KEY_SIZE];
240 struct aes_enckey aes;
241 int err;
242
243 if (WARN_ON_ONCE(derived_keysize > FSCRYPT_MAX_RAW_KEY_SIZE ||
244 derived_keysize % AES_BLOCK_SIZE != 0))
245 return -EINVAL;
246
247 static_assert(FSCRYPT_FILE_NONCE_SIZE == AES_KEYSIZE_128);
248 aes_prepareenckey(&aes, ci->ci_nonce, FSCRYPT_FILE_NONCE_SIZE);
249 for (unsigned int i = 0; i < derived_keysize; i += AES_BLOCK_SIZE)
250 aes_encrypt(&aes, &derived_key[i], &raw_master_key[i]);
251
252 err = fscrypt_set_per_file_enc_key(ci, derived_key);
253
254 memzero_explicit(derived_key, sizeof(derived_key));
255 /* No need to zeroize 'aes', as its key is not secret. */
256 return err;
257 }
258
fscrypt_setup_v1_file_key(struct fscrypt_inode_info * ci,const u8 * raw_master_key)259 int fscrypt_setup_v1_file_key(struct fscrypt_inode_info *ci,
260 const u8 *raw_master_key)
261 {
262 if (ci->ci_policy.v1.flags & FSCRYPT_POLICY_FLAG_DIRECT_KEY)
263 return setup_v1_file_key_direct(ci, raw_master_key);
264 else
265 return setup_v1_file_key_derived(ci, raw_master_key);
266 }
267
268 int
fscrypt_setup_v1_file_key_via_subscribed_keyrings(struct fscrypt_inode_info * ci)269 fscrypt_setup_v1_file_key_via_subscribed_keyrings(struct fscrypt_inode_info *ci)
270 {
271 const struct super_block *sb = ci->ci_inode->i_sb;
272 struct key *key;
273 const struct fscrypt_key *payload;
274 int err;
275
276 key = find_and_lock_process_key(FSCRYPT_KEY_DESC_PREFIX,
277 ci->ci_policy.v1.master_key_descriptor,
278 ci->ci_mode->keysize, &payload);
279 if (key == ERR_PTR(-ENOKEY) && sb->s_cop->legacy_key_prefix) {
280 key = find_and_lock_process_key(sb->s_cop->legacy_key_prefix,
281 ci->ci_policy.v1.master_key_descriptor,
282 ci->ci_mode->keysize, &payload);
283 }
284 if (IS_ERR(key))
285 return PTR_ERR(key);
286
287 err = fscrypt_setup_v1_file_key(ci, payload->raw);
288 up_read(&key->sem);
289 key_put(key);
290 return err;
291 }
292