xref: /openbmc/linux/fs/crypto/keysetup.c (revision bd4af432)
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 struct fscrypt_mode fscrypt_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 &fscrypt_modes[fscrypt_policy_contents_mode(policy)];
55 
56 	if (S_ISDIR(inode->i_mode) || S_ISLNK(inode->i_mode))
57 		return &fscrypt_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, crypto_skcipher_driver_name(tfm));
93 	}
94 	if (WARN_ON(crypto_skcipher_ivsize(tfm) != mode->ivsize)) {
95 		err = -EINVAL;
96 		goto err_free_tfm;
97 	}
98 	crypto_skcipher_set_flags(tfm, CRYPTO_TFM_REQ_FORBID_WEAK_KEYS);
99 	err = crypto_skcipher_setkey(tfm, raw_key, mode->keysize);
100 	if (err)
101 		goto err_free_tfm;
102 
103 	return tfm;
104 
105 err_free_tfm:
106 	crypto_free_skcipher(tfm);
107 	return ERR_PTR(err);
108 }
109 
110 /* Given a per-file encryption key, set up the file's crypto transform object */
111 int fscrypt_set_per_file_enc_key(struct fscrypt_info *ci, const u8 *raw_key)
112 {
113 	struct crypto_skcipher *tfm;
114 
115 	tfm = fscrypt_allocate_skcipher(ci->ci_mode, raw_key, ci->ci_inode);
116 	if (IS_ERR(tfm))
117 		return PTR_ERR(tfm);
118 
119 	ci->ci_ctfm = tfm;
120 	ci->ci_owns_key = true;
121 	return 0;
122 }
123 
124 static int setup_per_mode_enc_key(struct fscrypt_info *ci,
125 				  struct fscrypt_master_key *mk,
126 				  struct crypto_skcipher **tfms,
127 				  u8 hkdf_context, bool include_fs_uuid)
128 {
129 	const struct inode *inode = ci->ci_inode;
130 	const struct super_block *sb = inode->i_sb;
131 	struct fscrypt_mode *mode = ci->ci_mode;
132 	const u8 mode_num = mode - fscrypt_modes;
133 	struct crypto_skcipher *tfm, *prev_tfm;
134 	u8 mode_key[FSCRYPT_MAX_KEY_SIZE];
135 	u8 hkdf_info[sizeof(mode_num) + sizeof(sb->s_uuid)];
136 	unsigned int hkdf_infolen = 0;
137 	int err;
138 
139 	if (WARN_ON(mode_num > __FSCRYPT_MODE_MAX))
140 		return -EINVAL;
141 
142 	/* pairs with cmpxchg() below */
143 	tfm = READ_ONCE(tfms[mode_num]);
144 	if (likely(tfm != NULL))
145 		goto done;
146 
147 	BUILD_BUG_ON(sizeof(mode_num) != 1);
148 	BUILD_BUG_ON(sizeof(sb->s_uuid) != 16);
149 	BUILD_BUG_ON(sizeof(hkdf_info) != 17);
150 	hkdf_info[hkdf_infolen++] = mode_num;
151 	if (include_fs_uuid) {
152 		memcpy(&hkdf_info[hkdf_infolen], &sb->s_uuid,
153 		       sizeof(sb->s_uuid));
154 		hkdf_infolen += sizeof(sb->s_uuid);
155 	}
156 	err = fscrypt_hkdf_expand(&mk->mk_secret.hkdf,
157 				  hkdf_context, hkdf_info, hkdf_infolen,
158 				  mode_key, mode->keysize);
159 	if (err)
160 		return err;
161 	tfm = fscrypt_allocate_skcipher(mode, mode_key, inode);
162 	memzero_explicit(mode_key, mode->keysize);
163 	if (IS_ERR(tfm))
164 		return PTR_ERR(tfm);
165 
166 	/* pairs with READ_ONCE() above */
167 	prev_tfm = cmpxchg(&tfms[mode_num], NULL, tfm);
168 	if (prev_tfm != NULL) {
169 		crypto_free_skcipher(tfm);
170 		tfm = prev_tfm;
171 	}
172 done:
173 	ci->ci_ctfm = tfm;
174 	return 0;
175 }
176 
177 int fscrypt_derive_dirhash_key(struct fscrypt_info *ci,
178 			       const struct fscrypt_master_key *mk)
179 {
180 	int err;
181 
182 	err = fscrypt_hkdf_expand(&mk->mk_secret.hkdf, HKDF_CONTEXT_DIRHASH_KEY,
183 				  ci->ci_nonce, FS_KEY_DERIVATION_NONCE_SIZE,
184 				  (u8 *)&ci->ci_dirhash_key,
185 				  sizeof(ci->ci_dirhash_key));
186 	if (err)
187 		return err;
188 	ci->ci_dirhash_key_initialized = true;
189 	return 0;
190 }
191 
192 static int fscrypt_setup_v2_file_key(struct fscrypt_info *ci,
193 				     struct fscrypt_master_key *mk)
194 {
195 	int err;
196 
197 	if (ci->ci_policy.v2.flags & FSCRYPT_POLICY_FLAG_DIRECT_KEY) {
198 		/*
199 		 * DIRECT_KEY: instead of deriving per-file encryption keys, the
200 		 * per-file nonce will be included in all the IVs.  But unlike
201 		 * v1 policies, for v2 policies in this case we don't encrypt
202 		 * with the master key directly but rather derive a per-mode
203 		 * encryption key.  This ensures that the master key is
204 		 * consistently used only for HKDF, avoiding key reuse issues.
205 		 */
206 		err = setup_per_mode_enc_key(ci, mk, mk->mk_direct_tfms,
207 					     HKDF_CONTEXT_DIRECT_KEY, false);
208 	} else if (ci->ci_policy.v2.flags &
209 		   FSCRYPT_POLICY_FLAG_IV_INO_LBLK_64) {
210 		/*
211 		 * IV_INO_LBLK_64: encryption keys are derived from (master_key,
212 		 * mode_num, filesystem_uuid), and inode number is included in
213 		 * the IVs.  This format is optimized for use with inline
214 		 * encryption hardware compliant with the UFS or eMMC standards.
215 		 */
216 		err = setup_per_mode_enc_key(ci, mk, mk->mk_iv_ino_lblk_64_tfms,
217 					     HKDF_CONTEXT_IV_INO_LBLK_64_KEY,
218 					     true);
219 	} else {
220 		u8 derived_key[FSCRYPT_MAX_KEY_SIZE];
221 
222 		err = fscrypt_hkdf_expand(&mk->mk_secret.hkdf,
223 					  HKDF_CONTEXT_PER_FILE_ENC_KEY,
224 					  ci->ci_nonce,
225 					  FS_KEY_DERIVATION_NONCE_SIZE,
226 					  derived_key, ci->ci_mode->keysize);
227 		if (err)
228 			return err;
229 
230 		err = fscrypt_set_per_file_enc_key(ci, derived_key);
231 		memzero_explicit(derived_key, ci->ci_mode->keysize);
232 	}
233 	if (err)
234 		return err;
235 
236 	/* Derive a secret dirhash key for directories that need it. */
237 	if (S_ISDIR(ci->ci_inode->i_mode) && IS_CASEFOLDED(ci->ci_inode)) {
238 		err = fscrypt_derive_dirhash_key(ci, mk);
239 		if (err)
240 			return err;
241 	}
242 
243 	return 0;
244 }
245 
246 /*
247  * Find the master key, then set up the inode's actual encryption key.
248  *
249  * If the master key is found in the filesystem-level keyring, then the
250  * corresponding 'struct key' is returned in *master_key_ret with
251  * ->mk_secret_sem read-locked.  This is needed to ensure that only one task
252  * links the fscrypt_info into ->mk_decrypted_inodes (as multiple tasks may race
253  * to create an fscrypt_info for the same inode), and to synchronize the master
254  * key being removed with a new inode starting to use it.
255  */
256 static int setup_file_encryption_key(struct fscrypt_info *ci,
257 				     struct key **master_key_ret)
258 {
259 	struct key *key;
260 	struct fscrypt_master_key *mk = NULL;
261 	struct fscrypt_key_specifier mk_spec;
262 	int err;
263 
264 	switch (ci->ci_policy.version) {
265 	case FSCRYPT_POLICY_V1:
266 		mk_spec.type = FSCRYPT_KEY_SPEC_TYPE_DESCRIPTOR;
267 		memcpy(mk_spec.u.descriptor,
268 		       ci->ci_policy.v1.master_key_descriptor,
269 		       FSCRYPT_KEY_DESCRIPTOR_SIZE);
270 		break;
271 	case FSCRYPT_POLICY_V2:
272 		mk_spec.type = FSCRYPT_KEY_SPEC_TYPE_IDENTIFIER;
273 		memcpy(mk_spec.u.identifier,
274 		       ci->ci_policy.v2.master_key_identifier,
275 		       FSCRYPT_KEY_IDENTIFIER_SIZE);
276 		break;
277 	default:
278 		WARN_ON(1);
279 		return -EINVAL;
280 	}
281 
282 	key = fscrypt_find_master_key(ci->ci_inode->i_sb, &mk_spec);
283 	if (IS_ERR(key)) {
284 		if (key != ERR_PTR(-ENOKEY) ||
285 		    ci->ci_policy.version != FSCRYPT_POLICY_V1)
286 			return PTR_ERR(key);
287 
288 		/*
289 		 * As a legacy fallback for v1 policies, search for the key in
290 		 * the current task's subscribed keyrings too.  Don't move this
291 		 * to before the search of ->s_master_keys, since users
292 		 * shouldn't be able to override filesystem-level keys.
293 		 */
294 		return fscrypt_setup_v1_file_key_via_subscribed_keyrings(ci);
295 	}
296 
297 	mk = key->payload.data[0];
298 	down_read(&mk->mk_secret_sem);
299 
300 	/* Has the secret been removed (via FS_IOC_REMOVE_ENCRYPTION_KEY)? */
301 	if (!is_master_key_secret_present(&mk->mk_secret)) {
302 		err = -ENOKEY;
303 		goto out_release_key;
304 	}
305 
306 	/*
307 	 * Require that the master key be at least as long as the derived key.
308 	 * Otherwise, the derived key cannot possibly contain as much entropy as
309 	 * that required by the encryption mode it will be used for.  For v1
310 	 * policies it's also required for the KDF to work at all.
311 	 */
312 	if (mk->mk_secret.size < ci->ci_mode->keysize) {
313 		fscrypt_warn(NULL,
314 			     "key with %s %*phN is too short (got %u bytes, need %u+ bytes)",
315 			     master_key_spec_type(&mk_spec),
316 			     master_key_spec_len(&mk_spec), (u8 *)&mk_spec.u,
317 			     mk->mk_secret.size, ci->ci_mode->keysize);
318 		err = -ENOKEY;
319 		goto out_release_key;
320 	}
321 
322 	switch (ci->ci_policy.version) {
323 	case FSCRYPT_POLICY_V1:
324 		err = fscrypt_setup_v1_file_key(ci, mk->mk_secret.raw);
325 		break;
326 	case FSCRYPT_POLICY_V2:
327 		err = fscrypt_setup_v2_file_key(ci, mk);
328 		break;
329 	default:
330 		WARN_ON(1);
331 		err = -EINVAL;
332 		break;
333 	}
334 	if (err)
335 		goto out_release_key;
336 
337 	*master_key_ret = key;
338 	return 0;
339 
340 out_release_key:
341 	up_read(&mk->mk_secret_sem);
342 	key_put(key);
343 	return err;
344 }
345 
346 static void put_crypt_info(struct fscrypt_info *ci)
347 {
348 	struct key *key;
349 
350 	if (!ci)
351 		return;
352 
353 	if (ci->ci_direct_key)
354 		fscrypt_put_direct_key(ci->ci_direct_key);
355 	else if (ci->ci_owns_key)
356 		crypto_free_skcipher(ci->ci_ctfm);
357 
358 	key = ci->ci_master_key;
359 	if (key) {
360 		struct fscrypt_master_key *mk = key->payload.data[0];
361 
362 		/*
363 		 * Remove this inode from the list of inodes that were unlocked
364 		 * with the master key.
365 		 *
366 		 * In addition, if we're removing the last inode from a key that
367 		 * already had its secret removed, invalidate the key so that it
368 		 * gets removed from ->s_master_keys.
369 		 */
370 		spin_lock(&mk->mk_decrypted_inodes_lock);
371 		list_del(&ci->ci_master_key_link);
372 		spin_unlock(&mk->mk_decrypted_inodes_lock);
373 		if (refcount_dec_and_test(&mk->mk_refcount))
374 			key_invalidate(key);
375 		key_put(key);
376 	}
377 	memzero_explicit(ci, sizeof(*ci));
378 	kmem_cache_free(fscrypt_info_cachep, ci);
379 }
380 
381 int fscrypt_get_encryption_info(struct inode *inode)
382 {
383 	struct fscrypt_info *crypt_info;
384 	union fscrypt_context ctx;
385 	struct fscrypt_mode *mode;
386 	struct key *master_key = NULL;
387 	int res;
388 
389 	if (fscrypt_has_encryption_key(inode))
390 		return 0;
391 
392 	res = fscrypt_initialize(inode->i_sb->s_cop->flags);
393 	if (res)
394 		return res;
395 
396 	res = inode->i_sb->s_cop->get_context(inode, &ctx, sizeof(ctx));
397 	if (res < 0) {
398 		if (!fscrypt_dummy_context_enabled(inode) ||
399 		    IS_ENCRYPTED(inode)) {
400 			fscrypt_warn(inode,
401 				     "Error %d getting encryption context",
402 				     res);
403 			return res;
404 		}
405 		/* Fake up a context for an unencrypted directory */
406 		memset(&ctx, 0, sizeof(ctx));
407 		ctx.version = FSCRYPT_CONTEXT_V1;
408 		ctx.v1.contents_encryption_mode = FSCRYPT_MODE_AES_256_XTS;
409 		ctx.v1.filenames_encryption_mode = FSCRYPT_MODE_AES_256_CTS;
410 		memset(ctx.v1.master_key_descriptor, 0x42,
411 		       FSCRYPT_KEY_DESCRIPTOR_SIZE);
412 		res = sizeof(ctx.v1);
413 	}
414 
415 	crypt_info = kmem_cache_zalloc(fscrypt_info_cachep, GFP_NOFS);
416 	if (!crypt_info)
417 		return -ENOMEM;
418 
419 	crypt_info->ci_inode = inode;
420 
421 	res = fscrypt_policy_from_context(&crypt_info->ci_policy, &ctx, res);
422 	if (res) {
423 		fscrypt_warn(inode,
424 			     "Unrecognized or corrupt encryption context");
425 		goto out;
426 	}
427 
428 	memcpy(crypt_info->ci_nonce, fscrypt_context_nonce(&ctx),
429 	       FS_KEY_DERIVATION_NONCE_SIZE);
430 
431 	if (!fscrypt_supported_policy(&crypt_info->ci_policy, inode)) {
432 		res = -EINVAL;
433 		goto out;
434 	}
435 
436 	mode = select_encryption_mode(&crypt_info->ci_policy, inode);
437 	if (IS_ERR(mode)) {
438 		res = PTR_ERR(mode);
439 		goto out;
440 	}
441 	WARN_ON(mode->ivsize > FSCRYPT_MAX_IV_SIZE);
442 	crypt_info->ci_mode = mode;
443 
444 	res = setup_file_encryption_key(crypt_info, &master_key);
445 	if (res)
446 		goto out;
447 
448 	if (cmpxchg_release(&inode->i_crypt_info, NULL, crypt_info) == NULL) {
449 		if (master_key) {
450 			struct fscrypt_master_key *mk =
451 				master_key->payload.data[0];
452 
453 			refcount_inc(&mk->mk_refcount);
454 			crypt_info->ci_master_key = key_get(master_key);
455 			spin_lock(&mk->mk_decrypted_inodes_lock);
456 			list_add(&crypt_info->ci_master_key_link,
457 				 &mk->mk_decrypted_inodes);
458 			spin_unlock(&mk->mk_decrypted_inodes_lock);
459 		}
460 		crypt_info = NULL;
461 	}
462 	res = 0;
463 out:
464 	if (master_key) {
465 		struct fscrypt_master_key *mk = master_key->payload.data[0];
466 
467 		up_read(&mk->mk_secret_sem);
468 		key_put(master_key);
469 	}
470 	if (res == -ENOKEY)
471 		res = 0;
472 	put_crypt_info(crypt_info);
473 	return res;
474 }
475 EXPORT_SYMBOL(fscrypt_get_encryption_info);
476 
477 /**
478  * fscrypt_put_encryption_info - free most of an inode's fscrypt data
479  *
480  * Free the inode's fscrypt_info.  Filesystems must call this when the inode is
481  * being evicted.  An RCU grace period need not have elapsed yet.
482  */
483 void fscrypt_put_encryption_info(struct inode *inode)
484 {
485 	put_crypt_info(inode->i_crypt_info);
486 	inode->i_crypt_info = NULL;
487 }
488 EXPORT_SYMBOL(fscrypt_put_encryption_info);
489 
490 /**
491  * fscrypt_free_inode - free an inode's fscrypt data requiring RCU delay
492  *
493  * Free the inode's cached decrypted symlink target, if any.  Filesystems must
494  * call this after an RCU grace period, just before they free the inode.
495  */
496 void fscrypt_free_inode(struct inode *inode)
497 {
498 	if (IS_ENCRYPTED(inode) && S_ISLNK(inode->i_mode)) {
499 		kfree(inode->i_link);
500 		inode->i_link = NULL;
501 	}
502 }
503 EXPORT_SYMBOL(fscrypt_free_inode);
504 
505 /**
506  * fscrypt_drop_inode - check whether the inode's master key has been removed
507  *
508  * Filesystems supporting fscrypt must call this from their ->drop_inode()
509  * method so that encrypted inodes are evicted as soon as they're no longer in
510  * use and their master key has been removed.
511  *
512  * Return: 1 if fscrypt wants the inode to be evicted now, otherwise 0
513  */
514 int fscrypt_drop_inode(struct inode *inode)
515 {
516 	const struct fscrypt_info *ci = READ_ONCE(inode->i_crypt_info);
517 	const struct fscrypt_master_key *mk;
518 
519 	/*
520 	 * If ci is NULL, then the inode doesn't have an encryption key set up
521 	 * so it's irrelevant.  If ci_master_key is NULL, then the master key
522 	 * was provided via the legacy mechanism of the process-subscribed
523 	 * keyrings, so we don't know whether it's been removed or not.
524 	 */
525 	if (!ci || !ci->ci_master_key)
526 		return 0;
527 	mk = ci->ci_master_key->payload.data[0];
528 
529 	/*
530 	 * With proper, non-racy use of FS_IOC_REMOVE_ENCRYPTION_KEY, all inodes
531 	 * protected by the key were cleaned by sync_filesystem().  But if
532 	 * userspace is still using the files, inodes can be dirtied between
533 	 * then and now.  We mustn't lose any writes, so skip dirty inodes here.
534 	 */
535 	if (inode->i_state & I_DIRTY_ALL)
536 		return 0;
537 
538 	/*
539 	 * Note: since we aren't holding ->mk_secret_sem, the result here can
540 	 * immediately become outdated.  But there's no correctness problem with
541 	 * unnecessarily evicting.  Nor is there a correctness problem with not
542 	 * evicting while iput() is racing with the key being removed, since
543 	 * then the thread removing the key will either evict the inode itself
544 	 * or will correctly detect that it wasn't evicted due to the race.
545 	 */
546 	return !is_master_key_secret_present(&mk->mk_secret);
547 }
548 EXPORT_SYMBOL_GPL(fscrypt_drop_inode);
549