1 // SPDX-License-Identifier: GPL-2.0
2 /*
3  * key management facility for FS encryption support.
4  *
5  * Copyright (C) 2015, Google, Inc.
6  *
7  * This contains encryption key functions.
8  *
9  * Written by Michael Halcrow, Ildar Muslukhov, and Uday Savagaonkar, 2015.
10  */
11 
12 #include <keys/user-type.h>
13 #include <linux/scatterlist.h>
14 #include <linux/ratelimit.h>
15 #include <crypto/aes.h>
16 #include <crypto/sha.h>
17 #include <crypto/skcipher.h>
18 #include "fscrypt_private.h"
19 
20 static struct crypto_shash *essiv_hash_tfm;
21 
22 /*
23  * Key derivation function.  This generates the derived key by encrypting the
24  * master key with AES-128-ECB using the inode's nonce as the AES key.
25  *
26  * The master key must be at least as long as the derived key.  If the master
27  * key is longer, then only the first 'derived_keysize' bytes are used.
28  */
derive_key_aes(const u8 * master_key,const struct fscrypt_context * ctx,u8 * derived_key,unsigned int derived_keysize)29 static int derive_key_aes(const u8 *master_key,
30 			  const struct fscrypt_context *ctx,
31 			  u8 *derived_key, unsigned int derived_keysize)
32 {
33 	int res = 0;
34 	struct skcipher_request *req = NULL;
35 	DECLARE_CRYPTO_WAIT(wait);
36 	struct scatterlist src_sg, dst_sg;
37 	struct crypto_skcipher *tfm = crypto_alloc_skcipher("ecb(aes)", 0, 0);
38 
39 	if (IS_ERR(tfm)) {
40 		res = PTR_ERR(tfm);
41 		tfm = NULL;
42 		goto out;
43 	}
44 	crypto_skcipher_set_flags(tfm, CRYPTO_TFM_REQ_WEAK_KEY);
45 	req = skcipher_request_alloc(tfm, GFP_NOFS);
46 	if (!req) {
47 		res = -ENOMEM;
48 		goto out;
49 	}
50 	skcipher_request_set_callback(req,
51 			CRYPTO_TFM_REQ_MAY_BACKLOG | CRYPTO_TFM_REQ_MAY_SLEEP,
52 			crypto_req_done, &wait);
53 	res = crypto_skcipher_setkey(tfm, ctx->nonce, sizeof(ctx->nonce));
54 	if (res < 0)
55 		goto out;
56 
57 	sg_init_one(&src_sg, master_key, derived_keysize);
58 	sg_init_one(&dst_sg, derived_key, derived_keysize);
59 	skcipher_request_set_crypt(req, &src_sg, &dst_sg, derived_keysize,
60 				   NULL);
61 	res = crypto_wait_req(crypto_skcipher_encrypt(req), &wait);
62 out:
63 	skcipher_request_free(req);
64 	crypto_free_skcipher(tfm);
65 	return res;
66 }
67 
68 /*
69  * Search the current task's subscribed keyrings for a "logon" key with
70  * description prefix:descriptor, and if found acquire a read lock on it and
71  * return a pointer to its validated payload in *payload_ret.
72  */
73 static struct key *
find_and_lock_process_key(const char * prefix,const u8 descriptor[FS_KEY_DESCRIPTOR_SIZE],unsigned int min_keysize,const struct fscrypt_key ** payload_ret)74 find_and_lock_process_key(const char *prefix,
75 			  const u8 descriptor[FS_KEY_DESCRIPTOR_SIZE],
76 			  unsigned int min_keysize,
77 			  const struct fscrypt_key **payload_ret)
78 {
79 	char *description;
80 	struct key *key;
81 	const struct user_key_payload *ukp;
82 	const struct fscrypt_key *payload;
83 
84 	description = kasprintf(GFP_NOFS, "%s%*phN", prefix,
85 				FS_KEY_DESCRIPTOR_SIZE, descriptor);
86 	if (!description)
87 		return ERR_PTR(-ENOMEM);
88 
89 	key = request_key(&key_type_logon, description, NULL);
90 	kfree(description);
91 	if (IS_ERR(key))
92 		return key;
93 
94 	down_read(&key->sem);
95 	ukp = user_key_payload_locked(key);
96 
97 	if (!ukp) /* was the key revoked before we acquired its semaphore? */
98 		goto invalid;
99 
100 	payload = (const struct fscrypt_key *)ukp->data;
101 
102 	if (ukp->datalen != sizeof(struct fscrypt_key) ||
103 	    payload->size < 1 || payload->size > FS_MAX_KEY_SIZE) {
104 		fscrypt_warn(NULL,
105 			     "key with description '%s' has invalid payload",
106 			     key->description);
107 		goto invalid;
108 	}
109 
110 	if (payload->size < min_keysize) {
111 		fscrypt_warn(NULL,
112 			     "key with description '%s' is too short (got %u bytes, need %u+ bytes)",
113 			     key->description, payload->size, min_keysize);
114 		goto invalid;
115 	}
116 
117 	*payload_ret = payload;
118 	return key;
119 
120 invalid:
121 	up_read(&key->sem);
122 	key_put(key);
123 	return ERR_PTR(-ENOKEY);
124 }
125 
126 /* Find the master key, then derive the inode's actual encryption key */
find_and_derive_key(const struct inode * inode,const struct fscrypt_context * ctx,u8 * derived_key,unsigned int derived_keysize)127 static int find_and_derive_key(const struct inode *inode,
128 			       const struct fscrypt_context *ctx,
129 			       u8 *derived_key, unsigned int derived_keysize)
130 {
131 	struct key *key;
132 	const struct fscrypt_key *payload;
133 	int err;
134 
135 	key = find_and_lock_process_key(FS_KEY_DESC_PREFIX,
136 					ctx->master_key_descriptor,
137 					derived_keysize, &payload);
138 	if (key == ERR_PTR(-ENOKEY) && inode->i_sb->s_cop->key_prefix) {
139 		key = find_and_lock_process_key(inode->i_sb->s_cop->key_prefix,
140 						ctx->master_key_descriptor,
141 						derived_keysize, &payload);
142 	}
143 	if (IS_ERR(key))
144 		return PTR_ERR(key);
145 	err = derive_key_aes(payload->raw, ctx, derived_key, derived_keysize);
146 	up_read(&key->sem);
147 	key_put(key);
148 	return err;
149 }
150 
151 static struct fscrypt_mode {
152 	const char *friendly_name;
153 	const char *cipher_str;
154 	int keysize;
155 	bool logged_impl_name;
156 } available_modes[] = {
157 	[FS_ENCRYPTION_MODE_AES_256_XTS] = {
158 		.friendly_name = "AES-256-XTS",
159 		.cipher_str = "xts(aes)",
160 		.keysize = 64,
161 	},
162 	[FS_ENCRYPTION_MODE_AES_256_CTS] = {
163 		.friendly_name = "AES-256-CTS-CBC",
164 		.cipher_str = "cts(cbc(aes))",
165 		.keysize = 32,
166 	},
167 	[FS_ENCRYPTION_MODE_AES_128_CBC] = {
168 		.friendly_name = "AES-128-CBC",
169 		.cipher_str = "cbc(aes)",
170 		.keysize = 16,
171 	},
172 	[FS_ENCRYPTION_MODE_AES_128_CTS] = {
173 		.friendly_name = "AES-128-CTS-CBC",
174 		.cipher_str = "cts(cbc(aes))",
175 		.keysize = 16,
176 	},
177 };
178 
179 static struct fscrypt_mode *
select_encryption_mode(const struct fscrypt_info * ci,const struct inode * inode)180 select_encryption_mode(const struct fscrypt_info *ci, const struct inode *inode)
181 {
182 	if (!fscrypt_valid_enc_modes(ci->ci_data_mode, ci->ci_filename_mode)) {
183 		fscrypt_warn(inode->i_sb,
184 			     "inode %lu uses unsupported encryption modes (contents mode %d, filenames mode %d)",
185 			     inode->i_ino, ci->ci_data_mode,
186 			     ci->ci_filename_mode);
187 		return ERR_PTR(-EINVAL);
188 	}
189 
190 	if (S_ISREG(inode->i_mode))
191 		return &available_modes[ci->ci_data_mode];
192 
193 	if (S_ISDIR(inode->i_mode) || S_ISLNK(inode->i_mode))
194 		return &available_modes[ci->ci_filename_mode];
195 
196 	WARN_ONCE(1, "fscrypt: filesystem tried to load encryption info for inode %lu, which is not encryptable (file type %d)\n",
197 		  inode->i_ino, (inode->i_mode & S_IFMT));
198 	return ERR_PTR(-EINVAL);
199 }
200 
put_crypt_info(struct fscrypt_info * ci)201 static void put_crypt_info(struct fscrypt_info *ci)
202 {
203 	if (!ci)
204 		return;
205 
206 	crypto_free_skcipher(ci->ci_ctfm);
207 	crypto_free_cipher(ci->ci_essiv_tfm);
208 	kmem_cache_free(fscrypt_info_cachep, ci);
209 }
210 
derive_essiv_salt(const u8 * key,int keysize,u8 * salt)211 static int derive_essiv_salt(const u8 *key, int keysize, u8 *salt)
212 {
213 	struct crypto_shash *tfm = READ_ONCE(essiv_hash_tfm);
214 
215 	/* init hash transform on demand */
216 	if (unlikely(!tfm)) {
217 		struct crypto_shash *prev_tfm;
218 
219 		tfm = crypto_alloc_shash("sha256", 0, 0);
220 		if (IS_ERR(tfm)) {
221 			fscrypt_warn(NULL,
222 				     "error allocating SHA-256 transform: %ld",
223 				     PTR_ERR(tfm));
224 			return PTR_ERR(tfm);
225 		}
226 		prev_tfm = cmpxchg(&essiv_hash_tfm, NULL, tfm);
227 		if (prev_tfm) {
228 			crypto_free_shash(tfm);
229 			tfm = prev_tfm;
230 		}
231 	}
232 
233 	{
234 		SHASH_DESC_ON_STACK(desc, tfm);
235 		desc->tfm = tfm;
236 		desc->flags = 0;
237 
238 		return crypto_shash_digest(desc, key, keysize, salt);
239 	}
240 }
241 
init_essiv_generator(struct fscrypt_info * ci,const u8 * raw_key,int keysize)242 static int init_essiv_generator(struct fscrypt_info *ci, const u8 *raw_key,
243 				int keysize)
244 {
245 	int err;
246 	struct crypto_cipher *essiv_tfm;
247 	u8 salt[SHA256_DIGEST_SIZE];
248 
249 	essiv_tfm = crypto_alloc_cipher("aes", 0, 0);
250 	if (IS_ERR(essiv_tfm))
251 		return PTR_ERR(essiv_tfm);
252 
253 	ci->ci_essiv_tfm = essiv_tfm;
254 
255 	err = derive_essiv_salt(raw_key, keysize, salt);
256 	if (err)
257 		goto out;
258 
259 	/*
260 	 * Using SHA256 to derive the salt/key will result in AES-256 being
261 	 * used for IV generation. File contents encryption will still use the
262 	 * configured keysize (AES-128) nevertheless.
263 	 */
264 	err = crypto_cipher_setkey(essiv_tfm, salt, sizeof(salt));
265 	if (err)
266 		goto out;
267 
268 out:
269 	memzero_explicit(salt, sizeof(salt));
270 	return err;
271 }
272 
fscrypt_essiv_cleanup(void)273 void __exit fscrypt_essiv_cleanup(void)
274 {
275 	crypto_free_shash(essiv_hash_tfm);
276 }
277 
fscrypt_get_encryption_info(struct inode * inode)278 int fscrypt_get_encryption_info(struct inode *inode)
279 {
280 	struct fscrypt_info *crypt_info;
281 	struct fscrypt_context ctx;
282 	struct crypto_skcipher *ctfm;
283 	struct fscrypt_mode *mode;
284 	u8 *raw_key = NULL;
285 	int res;
286 
287 	if (inode->i_crypt_info)
288 		return 0;
289 
290 	res = fscrypt_initialize(inode->i_sb->s_cop->flags);
291 	if (res)
292 		return res;
293 
294 	res = inode->i_sb->s_cop->get_context(inode, &ctx, sizeof(ctx));
295 	if (res < 0) {
296 		if (!fscrypt_dummy_context_enabled(inode) ||
297 		    IS_ENCRYPTED(inode))
298 			return res;
299 		/* Fake up a context for an unencrypted directory */
300 		memset(&ctx, 0, sizeof(ctx));
301 		ctx.format = FS_ENCRYPTION_CONTEXT_FORMAT_V1;
302 		ctx.contents_encryption_mode = FS_ENCRYPTION_MODE_AES_256_XTS;
303 		ctx.filenames_encryption_mode = FS_ENCRYPTION_MODE_AES_256_CTS;
304 		memset(ctx.master_key_descriptor, 0x42, FS_KEY_DESCRIPTOR_SIZE);
305 	} else if (res != sizeof(ctx)) {
306 		return -EINVAL;
307 	}
308 
309 	if (ctx.format != FS_ENCRYPTION_CONTEXT_FORMAT_V1)
310 		return -EINVAL;
311 
312 	if (ctx.flags & ~FS_POLICY_FLAGS_VALID)
313 		return -EINVAL;
314 
315 	crypt_info = kmem_cache_alloc(fscrypt_info_cachep, GFP_NOFS);
316 	if (!crypt_info)
317 		return -ENOMEM;
318 
319 	crypt_info->ci_flags = ctx.flags;
320 	crypt_info->ci_data_mode = ctx.contents_encryption_mode;
321 	crypt_info->ci_filename_mode = ctx.filenames_encryption_mode;
322 	crypt_info->ci_ctfm = NULL;
323 	crypt_info->ci_essiv_tfm = NULL;
324 	memcpy(crypt_info->ci_master_key, ctx.master_key_descriptor,
325 				sizeof(crypt_info->ci_master_key));
326 
327 	mode = select_encryption_mode(crypt_info, inode);
328 	if (IS_ERR(mode)) {
329 		res = PTR_ERR(mode);
330 		goto out;
331 	}
332 
333 	/*
334 	 * This cannot be a stack buffer because it is passed to the scatterlist
335 	 * crypto API as part of key derivation.
336 	 */
337 	res = -ENOMEM;
338 	raw_key = kmalloc(mode->keysize, GFP_NOFS);
339 	if (!raw_key)
340 		goto out;
341 
342 	res = find_and_derive_key(inode, &ctx, raw_key, mode->keysize);
343 	if (res)
344 		goto out;
345 
346 	ctfm = crypto_alloc_skcipher(mode->cipher_str, 0, 0);
347 	if (IS_ERR(ctfm)) {
348 		res = PTR_ERR(ctfm);
349 		fscrypt_warn(inode->i_sb,
350 			     "error allocating '%s' transform for inode %lu: %d",
351 			     mode->cipher_str, inode->i_ino, res);
352 		goto out;
353 	}
354 	if (unlikely(!mode->logged_impl_name)) {
355 		/*
356 		 * fscrypt performance can vary greatly depending on which
357 		 * crypto algorithm implementation is used.  Help people debug
358 		 * performance problems by logging the ->cra_driver_name the
359 		 * first time a mode is used.  Note that multiple threads can
360 		 * race here, but it doesn't really matter.
361 		 */
362 		mode->logged_impl_name = true;
363 		pr_info("fscrypt: %s using implementation \"%s\"\n",
364 			mode->friendly_name,
365 			crypto_skcipher_alg(ctfm)->base.cra_driver_name);
366 	}
367 	crypt_info->ci_ctfm = ctfm;
368 	crypto_skcipher_set_flags(ctfm, CRYPTO_TFM_REQ_WEAK_KEY);
369 	res = crypto_skcipher_setkey(ctfm, raw_key, mode->keysize);
370 	if (res)
371 		goto out;
372 
373 	if (S_ISREG(inode->i_mode) &&
374 	    crypt_info->ci_data_mode == FS_ENCRYPTION_MODE_AES_128_CBC) {
375 		res = init_essiv_generator(crypt_info, raw_key, mode->keysize);
376 		if (res) {
377 			fscrypt_warn(inode->i_sb,
378 				     "error initializing ESSIV generator for inode %lu: %d",
379 				     inode->i_ino, res);
380 			goto out;
381 		}
382 	}
383 	if (cmpxchg(&inode->i_crypt_info, NULL, crypt_info) == NULL)
384 		crypt_info = NULL;
385 out:
386 	if (res == -ENOKEY)
387 		res = 0;
388 	put_crypt_info(crypt_info);
389 	kzfree(raw_key);
390 	return res;
391 }
392 EXPORT_SYMBOL(fscrypt_get_encryption_info);
393 
fscrypt_put_encryption_info(struct inode * inode)394 void fscrypt_put_encryption_info(struct inode *inode)
395 {
396 	put_crypt_info(inode->i_crypt_info);
397 	inode->i_crypt_info = NULL;
398 }
399 EXPORT_SYMBOL(fscrypt_put_encryption_info);
400