xref: /openbmc/linux/fs/crypto/keysetup.c (revision 7b73a9c8)
1 // SPDX-License-Identifier: GPL-2.0
2 /*
3  * Key setup facility for FS encryption support.
4  *
5  * Copyright (C) 2015, Google, Inc.
6  *
7  * Originally written by Michael Halcrow, Ildar Muslukhov, and Uday Savagaonkar.
8  * Heavily modified since then.
9  */
10 
11 #include <crypto/skcipher.h>
12 #include <linux/key.h>
13 
14 #include "fscrypt_private.h"
15 
16 static struct fscrypt_mode available_modes[] = {
17 	[FSCRYPT_MODE_AES_256_XTS] = {
18 		.friendly_name = "AES-256-XTS",
19 		.cipher_str = "xts(aes)",
20 		.keysize = 64,
21 		.ivsize = 16,
22 	},
23 	[FSCRYPT_MODE_AES_256_CTS] = {
24 		.friendly_name = "AES-256-CTS-CBC",
25 		.cipher_str = "cts(cbc(aes))",
26 		.keysize = 32,
27 		.ivsize = 16,
28 	},
29 	[FSCRYPT_MODE_AES_128_CBC] = {
30 		.friendly_name = "AES-128-CBC-ESSIV",
31 		.cipher_str = "essiv(cbc(aes),sha256)",
32 		.keysize = 16,
33 		.ivsize = 16,
34 	},
35 	[FSCRYPT_MODE_AES_128_CTS] = {
36 		.friendly_name = "AES-128-CTS-CBC",
37 		.cipher_str = "cts(cbc(aes))",
38 		.keysize = 16,
39 		.ivsize = 16,
40 	},
41 	[FSCRYPT_MODE_ADIANTUM] = {
42 		.friendly_name = "Adiantum",
43 		.cipher_str = "adiantum(xchacha12,aes)",
44 		.keysize = 32,
45 		.ivsize = 32,
46 	},
47 };
48 
49 static struct fscrypt_mode *
50 select_encryption_mode(const union fscrypt_policy *policy,
51 		       const struct inode *inode)
52 {
53 	if (S_ISREG(inode->i_mode))
54 		return &available_modes[fscrypt_policy_contents_mode(policy)];
55 
56 	if (S_ISDIR(inode->i_mode) || S_ISLNK(inode->i_mode))
57 		return &available_modes[fscrypt_policy_fnames_mode(policy)];
58 
59 	WARN_ONCE(1, "fscrypt: filesystem tried to load encryption info for inode %lu, which is not encryptable (file type %d)\n",
60 		  inode->i_ino, (inode->i_mode & S_IFMT));
61 	return ERR_PTR(-EINVAL);
62 }
63 
64 /* Create a symmetric cipher object for the given encryption mode and key */
65 struct crypto_skcipher *fscrypt_allocate_skcipher(struct fscrypt_mode *mode,
66 						  const u8 *raw_key,
67 						  const struct inode *inode)
68 {
69 	struct crypto_skcipher *tfm;
70 	int err;
71 
72 	tfm = crypto_alloc_skcipher(mode->cipher_str, 0, 0);
73 	if (IS_ERR(tfm)) {
74 		if (PTR_ERR(tfm) == -ENOENT) {
75 			fscrypt_warn(inode,
76 				     "Missing crypto API support for %s (API name: \"%s\")",
77 				     mode->friendly_name, mode->cipher_str);
78 			return ERR_PTR(-ENOPKG);
79 		}
80 		fscrypt_err(inode, "Error allocating '%s' transform: %ld",
81 			    mode->cipher_str, PTR_ERR(tfm));
82 		return tfm;
83 	}
84 	if (!xchg(&mode->logged_impl_name, 1)) {
85 		/*
86 		 * fscrypt performance can vary greatly depending on which
87 		 * crypto algorithm implementation is used.  Help people debug
88 		 * performance problems by logging the ->cra_driver_name the
89 		 * first time a mode is used.
90 		 */
91 		pr_info("fscrypt: %s using implementation \"%s\"\n",
92 			mode->friendly_name,
93 			crypto_skcipher_alg(tfm)->base.cra_driver_name);
94 	}
95 	crypto_skcipher_set_flags(tfm, CRYPTO_TFM_REQ_FORBID_WEAK_KEYS);
96 	err = crypto_skcipher_setkey(tfm, raw_key, mode->keysize);
97 	if (err)
98 		goto err_free_tfm;
99 
100 	return tfm;
101 
102 err_free_tfm:
103 	crypto_free_skcipher(tfm);
104 	return ERR_PTR(err);
105 }
106 
107 /* Given the per-file key, set up the file's crypto transform object */
108 int fscrypt_set_derived_key(struct fscrypt_info *ci, const u8 *derived_key)
109 {
110 	struct crypto_skcipher *tfm;
111 
112 	tfm = fscrypt_allocate_skcipher(ci->ci_mode, derived_key, ci->ci_inode);
113 	if (IS_ERR(tfm))
114 		return PTR_ERR(tfm);
115 
116 	ci->ci_ctfm = tfm;
117 	ci->ci_owns_key = true;
118 	return 0;
119 }
120 
121 static int setup_per_mode_key(struct fscrypt_info *ci,
122 			      struct fscrypt_master_key *mk,
123 			      struct crypto_skcipher **tfms,
124 			      u8 hkdf_context, bool include_fs_uuid)
125 {
126 	const struct inode *inode = ci->ci_inode;
127 	const struct super_block *sb = inode->i_sb;
128 	struct fscrypt_mode *mode = ci->ci_mode;
129 	u8 mode_num = mode - available_modes;
130 	struct crypto_skcipher *tfm, *prev_tfm;
131 	u8 mode_key[FSCRYPT_MAX_KEY_SIZE];
132 	u8 hkdf_info[sizeof(mode_num) + sizeof(sb->s_uuid)];
133 	unsigned int hkdf_infolen = 0;
134 	int err;
135 
136 	if (WARN_ON(mode_num > __FSCRYPT_MODE_MAX))
137 		return -EINVAL;
138 
139 	/* pairs with cmpxchg() below */
140 	tfm = READ_ONCE(tfms[mode_num]);
141 	if (likely(tfm != NULL))
142 		goto done;
143 
144 	BUILD_BUG_ON(sizeof(mode_num) != 1);
145 	BUILD_BUG_ON(sizeof(sb->s_uuid) != 16);
146 	BUILD_BUG_ON(sizeof(hkdf_info) != 17);
147 	hkdf_info[hkdf_infolen++] = mode_num;
148 	if (include_fs_uuid) {
149 		memcpy(&hkdf_info[hkdf_infolen], &sb->s_uuid,
150 		       sizeof(sb->s_uuid));
151 		hkdf_infolen += sizeof(sb->s_uuid);
152 	}
153 	err = fscrypt_hkdf_expand(&mk->mk_secret.hkdf,
154 				  hkdf_context, hkdf_info, hkdf_infolen,
155 				  mode_key, mode->keysize);
156 	if (err)
157 		return err;
158 	tfm = fscrypt_allocate_skcipher(mode, mode_key, inode);
159 	memzero_explicit(mode_key, mode->keysize);
160 	if (IS_ERR(tfm))
161 		return PTR_ERR(tfm);
162 
163 	/* pairs with READ_ONCE() above */
164 	prev_tfm = cmpxchg(&tfms[mode_num], NULL, tfm);
165 	if (prev_tfm != NULL) {
166 		crypto_free_skcipher(tfm);
167 		tfm = prev_tfm;
168 	}
169 done:
170 	ci->ci_ctfm = tfm;
171 	return 0;
172 }
173 
174 static int fscrypt_setup_v2_file_key(struct fscrypt_info *ci,
175 				     struct fscrypt_master_key *mk)
176 {
177 	u8 derived_key[FSCRYPT_MAX_KEY_SIZE];
178 	int err;
179 
180 	if (ci->ci_policy.v2.flags & FSCRYPT_POLICY_FLAG_DIRECT_KEY) {
181 		/*
182 		 * DIRECT_KEY: instead of deriving per-file keys, the per-file
183 		 * nonce will be included in all the IVs.  But unlike v1
184 		 * policies, for v2 policies in this case we don't encrypt with
185 		 * the master key directly but rather derive a per-mode key.
186 		 * This ensures that the master key is consistently used only
187 		 * for HKDF, avoiding key reuse issues.
188 		 */
189 		if (!fscrypt_mode_supports_direct_key(ci->ci_mode)) {
190 			fscrypt_warn(ci->ci_inode,
191 				     "Direct key flag not allowed with %s",
192 				     ci->ci_mode->friendly_name);
193 			return -EINVAL;
194 		}
195 		return setup_per_mode_key(ci, mk, mk->mk_direct_tfms,
196 					  HKDF_CONTEXT_DIRECT_KEY, false);
197 	} else if (ci->ci_policy.v2.flags &
198 		   FSCRYPT_POLICY_FLAG_IV_INO_LBLK_64) {
199 		/*
200 		 * IV_INO_LBLK_64: encryption keys are derived from (master_key,
201 		 * mode_num, filesystem_uuid), and inode number is included in
202 		 * the IVs.  This format is optimized for use with inline
203 		 * encryption hardware compliant with the UFS or eMMC standards.
204 		 */
205 		return setup_per_mode_key(ci, mk, mk->mk_iv_ino_lblk_64_tfms,
206 					  HKDF_CONTEXT_IV_INO_LBLK_64_KEY,
207 					  true);
208 	}
209 
210 	err = fscrypt_hkdf_expand(&mk->mk_secret.hkdf,
211 				  HKDF_CONTEXT_PER_FILE_KEY,
212 				  ci->ci_nonce, FS_KEY_DERIVATION_NONCE_SIZE,
213 				  derived_key, ci->ci_mode->keysize);
214 	if (err)
215 		return err;
216 
217 	err = fscrypt_set_derived_key(ci, derived_key);
218 	memzero_explicit(derived_key, ci->ci_mode->keysize);
219 	return err;
220 }
221 
222 /*
223  * Find the master key, then set up the inode's actual encryption key.
224  *
225  * If the master key is found in the filesystem-level keyring, then the
226  * corresponding 'struct key' is returned in *master_key_ret with
227  * ->mk_secret_sem read-locked.  This is needed to ensure that only one task
228  * links the fscrypt_info into ->mk_decrypted_inodes (as multiple tasks may race
229  * to create an fscrypt_info for the same inode), and to synchronize the master
230  * key being removed with a new inode starting to use it.
231  */
232 static int setup_file_encryption_key(struct fscrypt_info *ci,
233 				     struct key **master_key_ret)
234 {
235 	struct key *key;
236 	struct fscrypt_master_key *mk = NULL;
237 	struct fscrypt_key_specifier mk_spec;
238 	int err;
239 
240 	switch (ci->ci_policy.version) {
241 	case FSCRYPT_POLICY_V1:
242 		mk_spec.type = FSCRYPT_KEY_SPEC_TYPE_DESCRIPTOR;
243 		memcpy(mk_spec.u.descriptor,
244 		       ci->ci_policy.v1.master_key_descriptor,
245 		       FSCRYPT_KEY_DESCRIPTOR_SIZE);
246 		break;
247 	case FSCRYPT_POLICY_V2:
248 		mk_spec.type = FSCRYPT_KEY_SPEC_TYPE_IDENTIFIER;
249 		memcpy(mk_spec.u.identifier,
250 		       ci->ci_policy.v2.master_key_identifier,
251 		       FSCRYPT_KEY_IDENTIFIER_SIZE);
252 		break;
253 	default:
254 		WARN_ON(1);
255 		return -EINVAL;
256 	}
257 
258 	key = fscrypt_find_master_key(ci->ci_inode->i_sb, &mk_spec);
259 	if (IS_ERR(key)) {
260 		if (key != ERR_PTR(-ENOKEY) ||
261 		    ci->ci_policy.version != FSCRYPT_POLICY_V1)
262 			return PTR_ERR(key);
263 
264 		/*
265 		 * As a legacy fallback for v1 policies, search for the key in
266 		 * the current task's subscribed keyrings too.  Don't move this
267 		 * to before the search of ->s_master_keys, since users
268 		 * shouldn't be able to override filesystem-level keys.
269 		 */
270 		return fscrypt_setup_v1_file_key_via_subscribed_keyrings(ci);
271 	}
272 
273 	mk = key->payload.data[0];
274 	down_read(&mk->mk_secret_sem);
275 
276 	/* Has the secret been removed (via FS_IOC_REMOVE_ENCRYPTION_KEY)? */
277 	if (!is_master_key_secret_present(&mk->mk_secret)) {
278 		err = -ENOKEY;
279 		goto out_release_key;
280 	}
281 
282 	/*
283 	 * Require that the master key be at least as long as the derived key.
284 	 * Otherwise, the derived key cannot possibly contain as much entropy as
285 	 * that required by the encryption mode it will be used for.  For v1
286 	 * policies it's also required for the KDF to work at all.
287 	 */
288 	if (mk->mk_secret.size < ci->ci_mode->keysize) {
289 		fscrypt_warn(NULL,
290 			     "key with %s %*phN is too short (got %u bytes, need %u+ bytes)",
291 			     master_key_spec_type(&mk_spec),
292 			     master_key_spec_len(&mk_spec), (u8 *)&mk_spec.u,
293 			     mk->mk_secret.size, ci->ci_mode->keysize);
294 		err = -ENOKEY;
295 		goto out_release_key;
296 	}
297 
298 	switch (ci->ci_policy.version) {
299 	case FSCRYPT_POLICY_V1:
300 		err = fscrypt_setup_v1_file_key(ci, mk->mk_secret.raw);
301 		break;
302 	case FSCRYPT_POLICY_V2:
303 		err = fscrypt_setup_v2_file_key(ci, mk);
304 		break;
305 	default:
306 		WARN_ON(1);
307 		err = -EINVAL;
308 		break;
309 	}
310 	if (err)
311 		goto out_release_key;
312 
313 	*master_key_ret = key;
314 	return 0;
315 
316 out_release_key:
317 	up_read(&mk->mk_secret_sem);
318 	key_put(key);
319 	return err;
320 }
321 
322 static void put_crypt_info(struct fscrypt_info *ci)
323 {
324 	struct key *key;
325 
326 	if (!ci)
327 		return;
328 
329 	if (ci->ci_direct_key)
330 		fscrypt_put_direct_key(ci->ci_direct_key);
331 	else if (ci->ci_owns_key)
332 		crypto_free_skcipher(ci->ci_ctfm);
333 
334 	key = ci->ci_master_key;
335 	if (key) {
336 		struct fscrypt_master_key *mk = key->payload.data[0];
337 
338 		/*
339 		 * Remove this inode from the list of inodes that were unlocked
340 		 * with the master key.
341 		 *
342 		 * In addition, if we're removing the last inode from a key that
343 		 * already had its secret removed, invalidate the key so that it
344 		 * gets removed from ->s_master_keys.
345 		 */
346 		spin_lock(&mk->mk_decrypted_inodes_lock);
347 		list_del(&ci->ci_master_key_link);
348 		spin_unlock(&mk->mk_decrypted_inodes_lock);
349 		if (refcount_dec_and_test(&mk->mk_refcount))
350 			key_invalidate(key);
351 		key_put(key);
352 	}
353 	memzero_explicit(ci, sizeof(*ci));
354 	kmem_cache_free(fscrypt_info_cachep, ci);
355 }
356 
357 int fscrypt_get_encryption_info(struct inode *inode)
358 {
359 	struct fscrypt_info *crypt_info;
360 	union fscrypt_context ctx;
361 	struct fscrypt_mode *mode;
362 	struct key *master_key = NULL;
363 	int res;
364 
365 	if (fscrypt_has_encryption_key(inode))
366 		return 0;
367 
368 	res = fscrypt_initialize(inode->i_sb->s_cop->flags);
369 	if (res)
370 		return res;
371 
372 	res = inode->i_sb->s_cop->get_context(inode, &ctx, sizeof(ctx));
373 	if (res < 0) {
374 		if (!fscrypt_dummy_context_enabled(inode) ||
375 		    IS_ENCRYPTED(inode)) {
376 			fscrypt_warn(inode,
377 				     "Error %d getting encryption context",
378 				     res);
379 			return res;
380 		}
381 		/* Fake up a context for an unencrypted directory */
382 		memset(&ctx, 0, sizeof(ctx));
383 		ctx.version = FSCRYPT_CONTEXT_V1;
384 		ctx.v1.contents_encryption_mode = FSCRYPT_MODE_AES_256_XTS;
385 		ctx.v1.filenames_encryption_mode = FSCRYPT_MODE_AES_256_CTS;
386 		memset(ctx.v1.master_key_descriptor, 0x42,
387 		       FSCRYPT_KEY_DESCRIPTOR_SIZE);
388 		res = sizeof(ctx.v1);
389 	}
390 
391 	crypt_info = kmem_cache_zalloc(fscrypt_info_cachep, GFP_NOFS);
392 	if (!crypt_info)
393 		return -ENOMEM;
394 
395 	crypt_info->ci_inode = inode;
396 
397 	res = fscrypt_policy_from_context(&crypt_info->ci_policy, &ctx, res);
398 	if (res) {
399 		fscrypt_warn(inode,
400 			     "Unrecognized or corrupt encryption context");
401 		goto out;
402 	}
403 
404 	switch (ctx.version) {
405 	case FSCRYPT_CONTEXT_V1:
406 		memcpy(crypt_info->ci_nonce, ctx.v1.nonce,
407 		       FS_KEY_DERIVATION_NONCE_SIZE);
408 		break;
409 	case FSCRYPT_CONTEXT_V2:
410 		memcpy(crypt_info->ci_nonce, ctx.v2.nonce,
411 		       FS_KEY_DERIVATION_NONCE_SIZE);
412 		break;
413 	default:
414 		WARN_ON(1);
415 		res = -EINVAL;
416 		goto out;
417 	}
418 
419 	if (!fscrypt_supported_policy(&crypt_info->ci_policy, inode)) {
420 		res = -EINVAL;
421 		goto out;
422 	}
423 
424 	mode = select_encryption_mode(&crypt_info->ci_policy, inode);
425 	if (IS_ERR(mode)) {
426 		res = PTR_ERR(mode);
427 		goto out;
428 	}
429 	WARN_ON(mode->ivsize > FSCRYPT_MAX_IV_SIZE);
430 	crypt_info->ci_mode = mode;
431 
432 	res = setup_file_encryption_key(crypt_info, &master_key);
433 	if (res)
434 		goto out;
435 
436 	if (cmpxchg_release(&inode->i_crypt_info, NULL, crypt_info) == NULL) {
437 		if (master_key) {
438 			struct fscrypt_master_key *mk =
439 				master_key->payload.data[0];
440 
441 			refcount_inc(&mk->mk_refcount);
442 			crypt_info->ci_master_key = key_get(master_key);
443 			spin_lock(&mk->mk_decrypted_inodes_lock);
444 			list_add(&crypt_info->ci_master_key_link,
445 				 &mk->mk_decrypted_inodes);
446 			spin_unlock(&mk->mk_decrypted_inodes_lock);
447 		}
448 		crypt_info = NULL;
449 	}
450 	res = 0;
451 out:
452 	if (master_key) {
453 		struct fscrypt_master_key *mk = master_key->payload.data[0];
454 
455 		up_read(&mk->mk_secret_sem);
456 		key_put(master_key);
457 	}
458 	if (res == -ENOKEY)
459 		res = 0;
460 	put_crypt_info(crypt_info);
461 	return res;
462 }
463 EXPORT_SYMBOL(fscrypt_get_encryption_info);
464 
465 /**
466  * fscrypt_put_encryption_info - free most of an inode's fscrypt data
467  *
468  * Free the inode's fscrypt_info.  Filesystems must call this when the inode is
469  * being evicted.  An RCU grace period need not have elapsed yet.
470  */
471 void fscrypt_put_encryption_info(struct inode *inode)
472 {
473 	put_crypt_info(inode->i_crypt_info);
474 	inode->i_crypt_info = NULL;
475 }
476 EXPORT_SYMBOL(fscrypt_put_encryption_info);
477 
478 /**
479  * fscrypt_free_inode - free an inode's fscrypt data requiring RCU delay
480  *
481  * Free the inode's cached decrypted symlink target, if any.  Filesystems must
482  * call this after an RCU grace period, just before they free the inode.
483  */
484 void fscrypt_free_inode(struct inode *inode)
485 {
486 	if (IS_ENCRYPTED(inode) && S_ISLNK(inode->i_mode)) {
487 		kfree(inode->i_link);
488 		inode->i_link = NULL;
489 	}
490 }
491 EXPORT_SYMBOL(fscrypt_free_inode);
492 
493 /**
494  * fscrypt_drop_inode - check whether the inode's master key has been removed
495  *
496  * Filesystems supporting fscrypt must call this from their ->drop_inode()
497  * method so that encrypted inodes are evicted as soon as they're no longer in
498  * use and their master key has been removed.
499  *
500  * Return: 1 if fscrypt wants the inode to be evicted now, otherwise 0
501  */
502 int fscrypt_drop_inode(struct inode *inode)
503 {
504 	const struct fscrypt_info *ci = READ_ONCE(inode->i_crypt_info);
505 	const struct fscrypt_master_key *mk;
506 
507 	/*
508 	 * If ci is NULL, then the inode doesn't have an encryption key set up
509 	 * so it's irrelevant.  If ci_master_key is NULL, then the master key
510 	 * was provided via the legacy mechanism of the process-subscribed
511 	 * keyrings, so we don't know whether it's been removed or not.
512 	 */
513 	if (!ci || !ci->ci_master_key)
514 		return 0;
515 	mk = ci->ci_master_key->payload.data[0];
516 
517 	/*
518 	 * Note: since we aren't holding ->mk_secret_sem, the result here can
519 	 * immediately become outdated.  But there's no correctness problem with
520 	 * unnecessarily evicting.  Nor is there a correctness problem with not
521 	 * evicting while iput() is racing with the key being removed, since
522 	 * then the thread removing the key will either evict the inode itself
523 	 * or will correctly detect that it wasn't evicted due to the race.
524 	 */
525 	return !is_master_key_secret_present(&mk->mk_secret);
526 }
527 EXPORT_SYMBOL_GPL(fscrypt_drop_inode);
528