Linux Audio

Check our new training course

Loading...
v4.17
  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
 20static struct crypto_shash *essiv_hash_tfm;
 
 
 
 
 
 
 
 
 
 21
 22/**
 23 * derive_key_aes() - Derive a key using AES-128-ECB
 24 * @deriving_key: Encryption key used for derivation.
 25 * @source_key:   Source key to which to apply derivation.
 26 * @derived_raw_key:  Derived raw key.
 27 *
 28 * Return: Zero on success; non-zero otherwise.
 29 */
 30static int derive_key_aes(u8 deriving_key[FS_AES_128_ECB_KEY_SIZE],
 31				const struct fscrypt_key *source_key,
 32				u8 derived_raw_key[FS_MAX_KEY_SIZE])
 33{
 34	int res = 0;
 35	struct skcipher_request *req = NULL;
 36	DECLARE_CRYPTO_WAIT(wait);
 37	struct scatterlist src_sg, dst_sg;
 38	struct crypto_skcipher *tfm = crypto_alloc_skcipher("ecb(aes)", 0, 0);
 39
 40	if (IS_ERR(tfm)) {
 41		res = PTR_ERR(tfm);
 42		tfm = NULL;
 43		goto out;
 44	}
 45	crypto_skcipher_set_flags(tfm, CRYPTO_TFM_REQ_WEAK_KEY);
 46	req = skcipher_request_alloc(tfm, GFP_NOFS);
 47	if (!req) {
 48		res = -ENOMEM;
 49		goto out;
 50	}
 51	skcipher_request_set_callback(req,
 52			CRYPTO_TFM_REQ_MAY_BACKLOG | CRYPTO_TFM_REQ_MAY_SLEEP,
 53			crypto_req_done, &wait);
 54	res = crypto_skcipher_setkey(tfm, deriving_key,
 55					FS_AES_128_ECB_KEY_SIZE);
 56	if (res < 0)
 57		goto out;
 58
 59	sg_init_one(&src_sg, source_key->raw, source_key->size);
 60	sg_init_one(&dst_sg, derived_raw_key, source_key->size);
 61	skcipher_request_set_crypt(req, &src_sg, &dst_sg, source_key->size,
 62				   NULL);
 63	res = crypto_wait_req(crypto_skcipher_encrypt(req), &wait);
 
 
 
 
 64out:
 65	skcipher_request_free(req);
 66	crypto_free_skcipher(tfm);
 67	return res;
 68}
 69
 70static int validate_user_key(struct fscrypt_info *crypt_info,
 71			struct fscrypt_context *ctx, u8 *raw_key,
 72			const char *prefix, int min_keysize)
 73{
 74	char *description;
 75	struct key *keyring_key;
 76	struct fscrypt_key *master_key;
 77	const struct user_key_payload *ukp;
 78	int res;
 79
 80	description = kasprintf(GFP_NOFS, "%s%*phN", prefix,
 81				FS_KEY_DESCRIPTOR_SIZE,
 82				ctx->master_key_descriptor);
 83	if (!description)
 84		return -ENOMEM;
 85
 86	keyring_key = request_key(&key_type_logon, description, NULL);
 87	kfree(description);
 88	if (IS_ERR(keyring_key))
 89		return PTR_ERR(keyring_key);
 90	down_read(&keyring_key->sem);
 91
 92	if (keyring_key->type != &key_type_logon) {
 93		printk_once(KERN_WARNING
 94				"%s: key type must be logon\n", __func__);
 95		res = -ENOKEY;
 96		goto out;
 97	}
 98	ukp = user_key_payload_locked(keyring_key);
 99	if (!ukp) {
100		/* key was revoked before we acquired its semaphore */
101		res = -EKEYREVOKED;
102		goto out;
103	}
104	if (ukp->datalen != sizeof(struct fscrypt_key)) {
105		res = -EINVAL;
106		goto out;
107	}
108	master_key = (struct fscrypt_key *)ukp->data;
109	BUILD_BUG_ON(FS_AES_128_ECB_KEY_SIZE != FS_KEY_DERIVATION_NONCE_SIZE);
110
111	if (master_key->size < min_keysize || master_key->size > FS_MAX_KEY_SIZE
112	    || master_key->size % AES_BLOCK_SIZE != 0) {
113		printk_once(KERN_WARNING
114				"%s: key size incorrect: %d\n",
115				__func__, master_key->size);
116		res = -ENOKEY;
117		goto out;
118	}
119	res = derive_key_aes(ctx->nonce, master_key, raw_key);
120out:
121	up_read(&keyring_key->sem);
122	key_put(keyring_key);
123	return res;
124}
125
126static const struct {
127	const char *cipher_str;
128	int keysize;
129} available_modes[] = {
130	[FS_ENCRYPTION_MODE_AES_256_XTS] = { "xts(aes)",
131					     FS_AES_256_XTS_KEY_SIZE },
132	[FS_ENCRYPTION_MODE_AES_256_CTS] = { "cts(cbc(aes))",
133					     FS_AES_256_CTS_KEY_SIZE },
134	[FS_ENCRYPTION_MODE_AES_128_CBC] = { "cbc(aes)",
135					     FS_AES_128_CBC_KEY_SIZE },
136	[FS_ENCRYPTION_MODE_AES_128_CTS] = { "cts(cbc(aes))",
137					     FS_AES_128_CTS_KEY_SIZE },
138};
139
140static int determine_cipher_type(struct fscrypt_info *ci, struct inode *inode,
141				 const char **cipher_str_ret, int *keysize_ret)
142{
143	u32 mode;
144
145	if (!fscrypt_valid_enc_modes(ci->ci_data_mode, ci->ci_filename_mode)) {
146		pr_warn_ratelimited("fscrypt: inode %lu uses unsupported encryption modes (contents mode %d, filenames mode %d)\n",
147				    inode->i_ino,
148				    ci->ci_data_mode, ci->ci_filename_mode);
149		return -EINVAL;
150	}
151
152	if (S_ISREG(inode->i_mode)) {
153		mode = ci->ci_data_mode;
154	} else if (S_ISDIR(inode->i_mode) || S_ISLNK(inode->i_mode)) {
155		mode = ci->ci_filename_mode;
156	} else {
157		WARN_ONCE(1, "fscrypt: filesystem tried to load encryption info for inode %lu, which is not encryptable (file type %d)\n",
158			  inode->i_ino, (inode->i_mode & S_IFMT));
159		return -EINVAL;
160	}
161
162	*cipher_str_ret = available_modes[mode].cipher_str;
163	*keysize_ret = available_modes[mode].keysize;
164	return 0;
165}
166
167static void put_crypt_info(struct fscrypt_info *ci)
168{
169	if (!ci)
170		return;
171
 
172	crypto_free_skcipher(ci->ci_ctfm);
173	crypto_free_cipher(ci->ci_essiv_tfm);
174	kmem_cache_free(fscrypt_info_cachep, ci);
175}
176
177static int derive_essiv_salt(const u8 *key, int keysize, u8 *salt)
178{
179	struct crypto_shash *tfm = READ_ONCE(essiv_hash_tfm);
180
181	/* init hash transform on demand */
182	if (unlikely(!tfm)) {
183		struct crypto_shash *prev_tfm;
184
185		tfm = crypto_alloc_shash("sha256", 0, 0);
186		if (IS_ERR(tfm)) {
187			pr_warn_ratelimited("fscrypt: error allocating SHA-256 transform: %ld\n",
188					    PTR_ERR(tfm));
189			return PTR_ERR(tfm);
190		}
191		prev_tfm = cmpxchg(&essiv_hash_tfm, NULL, tfm);
192		if (prev_tfm) {
193			crypto_free_shash(tfm);
194			tfm = prev_tfm;
195		}
196	}
197
198	{
199		SHASH_DESC_ON_STACK(desc, tfm);
200		desc->tfm = tfm;
201		desc->flags = 0;
202
203		return crypto_shash_digest(desc, key, keysize, salt);
204	}
205}
206
207static int init_essiv_generator(struct fscrypt_info *ci, const u8 *raw_key,
208				int keysize)
209{
210	int err;
211	struct crypto_cipher *essiv_tfm;
212	u8 salt[SHA256_DIGEST_SIZE];
213
214	essiv_tfm = crypto_alloc_cipher("aes", 0, 0);
215	if (IS_ERR(essiv_tfm))
216		return PTR_ERR(essiv_tfm);
217
218	ci->ci_essiv_tfm = essiv_tfm;
219
220	err = derive_essiv_salt(raw_key, keysize, salt);
221	if (err)
222		goto out;
223
224	/*
225	 * Using SHA256 to derive the salt/key will result in AES-256 being
226	 * used for IV generation. File contents encryption will still use the
227	 * configured keysize (AES-128) nevertheless.
228	 */
229	err = crypto_cipher_setkey(essiv_tfm, salt, sizeof(salt));
230	if (err)
231		goto out;
232
233out:
234	memzero_explicit(salt, sizeof(salt));
235	return err;
236}
237
238void __exit fscrypt_essiv_cleanup(void)
239{
240	crypto_free_shash(essiv_hash_tfm);
241}
242
243int fscrypt_get_encryption_info(struct inode *inode)
244{
245	struct fscrypt_info *crypt_info;
 
 
 
 
246	struct fscrypt_context ctx;
 
247	struct crypto_skcipher *ctfm;
248	const char *cipher_str;
249	int keysize;
250	u8 *raw_key = NULL;
251	int res;
252
253	if (inode->i_crypt_info)
254		return 0;
255
256	res = fscrypt_initialize(inode->i_sb->s_cop->flags);
257	if (res)
258		return res;
259
 
 
 
 
 
 
 
 
 
 
 
 
260	res = inode->i_sb->s_cop->get_context(inode, &ctx, sizeof(ctx));
261	if (res < 0) {
262		if (!fscrypt_dummy_context_enabled(inode) ||
263		    IS_ENCRYPTED(inode))
264			return res;
265		/* Fake up a context for an unencrypted directory */
266		memset(&ctx, 0, sizeof(ctx));
267		ctx.format = FS_ENCRYPTION_CONTEXT_FORMAT_V1;
268		ctx.contents_encryption_mode = FS_ENCRYPTION_MODE_AES_256_XTS;
269		ctx.filenames_encryption_mode = FS_ENCRYPTION_MODE_AES_256_CTS;
270		memset(ctx.master_key_descriptor, 0x42, FS_KEY_DESCRIPTOR_SIZE);
271	} else if (res != sizeof(ctx)) {
272		return -EINVAL;
273	}
274
275	if (ctx.format != FS_ENCRYPTION_CONTEXT_FORMAT_V1)
276		return -EINVAL;
277
278	if (ctx.flags & ~FS_POLICY_FLAGS_VALID)
279		return -EINVAL;
280
281	crypt_info = kmem_cache_alloc(fscrypt_info_cachep, GFP_NOFS);
282	if (!crypt_info)
283		return -ENOMEM;
284
285	crypt_info->ci_flags = ctx.flags;
286	crypt_info->ci_data_mode = ctx.contents_encryption_mode;
287	crypt_info->ci_filename_mode = ctx.filenames_encryption_mode;
288	crypt_info->ci_ctfm = NULL;
289	crypt_info->ci_essiv_tfm = NULL;
290	memcpy(crypt_info->ci_master_key, ctx.master_key_descriptor,
291				sizeof(crypt_info->ci_master_key));
292
293	res = determine_cipher_type(crypt_info, inode, &cipher_str, &keysize);
294	if (res)
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
295		goto out;
 
 
 
 
 
 
 
 
 
 
296
297	/*
298	 * This cannot be a stack buffer because it is passed to the scatterlist
299	 * crypto API as part of key derivation.
300	 */
301	res = -ENOMEM;
302	raw_key = kmalloc(FS_MAX_KEY_SIZE, GFP_NOFS);
303	if (!raw_key)
304		goto out;
305
306	res = validate_user_key(crypt_info, &ctx, raw_key, FS_KEY_DESC_PREFIX,
307				keysize);
308	if (res && inode->i_sb->s_cop->key_prefix) {
309		int res2 = validate_user_key(crypt_info, &ctx, raw_key,
310					     inode->i_sb->s_cop->key_prefix,
311					     keysize);
312		if (res2) {
313			if (res2 == -ENOKEY)
314				res = -ENOKEY;
315			goto out;
316		}
317	} else if (res) {
318		goto out;
319	}
 
 
 
 
 
320	ctfm = crypto_alloc_skcipher(cipher_str, 0, 0);
321	if (!ctfm || IS_ERR(ctfm)) {
322		res = ctfm ? PTR_ERR(ctfm) : -ENOMEM;
323		pr_debug("%s: error %d (inode %lu) allocating crypto tfm\n",
324			 __func__, res, inode->i_ino);
 
325		goto out;
326	}
327	crypt_info->ci_ctfm = ctfm;
328	crypto_skcipher_clear_flags(ctfm, ~0);
329	crypto_skcipher_set_flags(ctfm, CRYPTO_TFM_REQ_WEAK_KEY);
330	/*
331	 * if the provided key is longer than keysize, we use the first
332	 * keysize bytes of the derived key only
333	 */
334	res = crypto_skcipher_setkey(ctfm, raw_key, keysize);
335	if (res)
336		goto out;
337
338	if (S_ISREG(inode->i_mode) &&
339	    crypt_info->ci_data_mode == FS_ENCRYPTION_MODE_AES_128_CBC) {
340		res = init_essiv_generator(crypt_info, raw_key, keysize);
341		if (res) {
342			pr_debug("%s: error %d (inode %lu) allocating essiv tfm\n",
343				 __func__, res, inode->i_ino);
344			goto out;
345		}
346	}
347	if (cmpxchg(&inode->i_crypt_info, NULL, crypt_info) == NULL)
348		crypt_info = NULL;
349out:
350	if (res == -ENOKEY)
351		res = 0;
352	put_crypt_info(crypt_info);
353	kzfree(raw_key);
354	return res;
355}
356EXPORT_SYMBOL(fscrypt_get_encryption_info);
357
358void fscrypt_put_encryption_info(struct inode *inode)
359{
360	put_crypt_info(inode->i_crypt_info);
361	inode->i_crypt_info = NULL;
 
 
 
 
 
 
 
 
 
 
362}
363EXPORT_SYMBOL(fscrypt_put_encryption_info);
v4.6
 
  1/*
  2 * key management facility for FS encryption support.
  3 *
  4 * Copyright (C) 2015, Google, Inc.
  5 *
  6 * This contains encryption key functions.
  7 *
  8 * Written by Michael Halcrow, Ildar Muslukhov, and Uday Savagaonkar, 2015.
  9 */
 10
 11#include <keys/encrypted-type.h>
 12#include <keys/user-type.h>
 13#include <linux/random.h>
 14#include <linux/scatterlist.h>
 15#include <uapi/linux/keyctl.h>
 16#include <linux/fscrypto.h>
 
 
 
 17
 18static void derive_crypt_complete(struct crypto_async_request *req, int rc)
 19{
 20	struct fscrypt_completion_result *ecr = req->data;
 21
 22	if (rc == -EINPROGRESS)
 23		return;
 24
 25	ecr->res = rc;
 26	complete(&ecr->completion);
 27}
 28
 29/**
 30 * derive_key_aes() - Derive a key using AES-128-ECB
 31 * @deriving_key: Encryption key used for derivation.
 32 * @source_key:   Source key to which to apply derivation.
 33 * @derived_key:  Derived key.
 34 *
 35 * Return: Zero on success; non-zero otherwise.
 36 */
 37static int derive_key_aes(u8 deriving_key[FS_AES_128_ECB_KEY_SIZE],
 38				u8 source_key[FS_AES_256_XTS_KEY_SIZE],
 39				u8 derived_key[FS_AES_256_XTS_KEY_SIZE])
 40{
 41	int res = 0;
 42	struct skcipher_request *req = NULL;
 43	DECLARE_FS_COMPLETION_RESULT(ecr);
 44	struct scatterlist src_sg, dst_sg;
 45	struct crypto_skcipher *tfm = crypto_alloc_skcipher("ecb(aes)", 0, 0);
 46
 47	if (IS_ERR(tfm)) {
 48		res = PTR_ERR(tfm);
 49		tfm = NULL;
 50		goto out;
 51	}
 52	crypto_skcipher_set_flags(tfm, CRYPTO_TFM_REQ_WEAK_KEY);
 53	req = skcipher_request_alloc(tfm, GFP_NOFS);
 54	if (!req) {
 55		res = -ENOMEM;
 56		goto out;
 57	}
 58	skcipher_request_set_callback(req,
 59			CRYPTO_TFM_REQ_MAY_BACKLOG | CRYPTO_TFM_REQ_MAY_SLEEP,
 60			derive_crypt_complete, &ecr);
 61	res = crypto_skcipher_setkey(tfm, deriving_key,
 62					FS_AES_128_ECB_KEY_SIZE);
 63	if (res < 0)
 64		goto out;
 65
 66	sg_init_one(&src_sg, source_key, FS_AES_256_XTS_KEY_SIZE);
 67	sg_init_one(&dst_sg, derived_key, FS_AES_256_XTS_KEY_SIZE);
 68	skcipher_request_set_crypt(req, &src_sg, &dst_sg,
 69					FS_AES_256_XTS_KEY_SIZE, NULL);
 70	res = crypto_skcipher_encrypt(req);
 71	if (res == -EINPROGRESS || res == -EBUSY) {
 72		wait_for_completion(&ecr.completion);
 73		res = ecr.res;
 74	}
 75out:
 76	skcipher_request_free(req);
 77	crypto_free_skcipher(tfm);
 78	return res;
 79}
 80
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 81static void put_crypt_info(struct fscrypt_info *ci)
 82{
 83	if (!ci)
 84		return;
 85
 86	key_put(ci->ci_keyring_key);
 87	crypto_free_skcipher(ci->ci_ctfm);
 
 88	kmem_cache_free(fscrypt_info_cachep, ci);
 89}
 90
 91int get_crypt_info(struct inode *inode)
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 92{
 93	struct fscrypt_info *crypt_info;
 94	u8 full_key_descriptor[FS_KEY_DESC_PREFIX_SIZE +
 95				(FS_KEY_DESCRIPTOR_SIZE * 2) + 1];
 96	struct key *keyring_key = NULL;
 97	struct fscrypt_key *master_key;
 98	struct fscrypt_context ctx;
 99	const struct user_key_payload *ukp;
100	struct crypto_skcipher *ctfm;
101	const char *cipher_str;
102	u8 raw_key[FS_MAX_KEY_SIZE];
103	u8 mode;
104	int res;
105
106	res = fscrypt_initialize();
 
 
 
107	if (res)
108		return res;
109
110	if (!inode->i_sb->s_cop->get_context)
111		return -EOPNOTSUPP;
112retry:
113	crypt_info = ACCESS_ONCE(inode->i_crypt_info);
114	if (crypt_info) {
115		if (!crypt_info->ci_keyring_key ||
116				key_validate(crypt_info->ci_keyring_key) == 0)
117			return 0;
118		fscrypt_put_encryption_info(inode, crypt_info);
119		goto retry;
120	}
121
122	res = inode->i_sb->s_cop->get_context(inode, &ctx, sizeof(ctx));
123	if (res < 0) {
124		if (!fscrypt_dummy_context_enabled(inode))
 
125			return res;
 
 
 
126		ctx.contents_encryption_mode = FS_ENCRYPTION_MODE_AES_256_XTS;
127		ctx.filenames_encryption_mode = FS_ENCRYPTION_MODE_AES_256_CTS;
128		ctx.flags = 0;
129	} else if (res != sizeof(ctx)) {
130		return -EINVAL;
131	}
132	res = 0;
 
 
 
 
 
133
134	crypt_info = kmem_cache_alloc(fscrypt_info_cachep, GFP_NOFS);
135	if (!crypt_info)
136		return -ENOMEM;
137
138	crypt_info->ci_flags = ctx.flags;
139	crypt_info->ci_data_mode = ctx.contents_encryption_mode;
140	crypt_info->ci_filename_mode = ctx.filenames_encryption_mode;
141	crypt_info->ci_ctfm = NULL;
142	crypt_info->ci_keyring_key = NULL;
143	memcpy(crypt_info->ci_master_key, ctx.master_key_descriptor,
144				sizeof(crypt_info->ci_master_key));
145	if (S_ISREG(inode->i_mode))
146		mode = crypt_info->ci_data_mode;
147	else if (S_ISDIR(inode->i_mode) || S_ISLNK(inode->i_mode))
148		mode = crypt_info->ci_filename_mode;
149	else
150		BUG();
151
152	switch (mode) {
153	case FS_ENCRYPTION_MODE_AES_256_XTS:
154		cipher_str = "xts(aes)";
155		break;
156	case FS_ENCRYPTION_MODE_AES_256_CTS:
157		cipher_str = "cts(cbc(aes))";
158		break;
159	default:
160		printk_once(KERN_WARNING
161			    "%s: unsupported key mode %d (ino %u)\n",
162			    __func__, mode, (unsigned) inode->i_ino);
163		res = -ENOKEY;
164		goto out;
165	}
166	if (fscrypt_dummy_context_enabled(inode)) {
167		memset(raw_key, 0x42, FS_AES_256_XTS_KEY_SIZE);
168		goto got_key;
169	}
170	memcpy(full_key_descriptor, FS_KEY_DESC_PREFIX,
171					FS_KEY_DESC_PREFIX_SIZE);
172	sprintf(full_key_descriptor + FS_KEY_DESC_PREFIX_SIZE,
173					"%*phN", FS_KEY_DESCRIPTOR_SIZE,
174					ctx.master_key_descriptor);
175	full_key_descriptor[FS_KEY_DESC_PREFIX_SIZE +
176					(2 * FS_KEY_DESCRIPTOR_SIZE)] = '\0';
177	keyring_key = request_key(&key_type_logon, full_key_descriptor, NULL);
178	if (IS_ERR(keyring_key)) {
179		res = PTR_ERR(keyring_key);
180		keyring_key = NULL;
181		goto out;
182	}
183	crypt_info->ci_keyring_key = keyring_key;
184	if (keyring_key->type != &key_type_logon) {
185		printk_once(KERN_WARNING
186				"%s: key type must be logon\n", __func__);
187		res = -ENOKEY;
188		goto out;
189	}
190	down_read(&keyring_key->sem);
191	ukp = user_key_payload(keyring_key);
192	if (ukp->datalen != sizeof(struct fscrypt_key)) {
193		res = -EINVAL;
194		up_read(&keyring_key->sem);
195		goto out;
196	}
197	master_key = (struct fscrypt_key *)ukp->data;
198	BUILD_BUG_ON(FS_AES_128_ECB_KEY_SIZE != FS_KEY_DERIVATION_NONCE_SIZE);
199
200	if (master_key->size != FS_AES_256_XTS_KEY_SIZE) {
201		printk_once(KERN_WARNING
202				"%s: key size incorrect: %d\n",
203				__func__, master_key->size);
204		res = -ENOKEY;
205		up_read(&keyring_key->sem);
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
206		goto out;
207	}
208	res = derive_key_aes(ctx.nonce, master_key->raw, raw_key);
209	up_read(&keyring_key->sem);
210	if (res)
211		goto out;
212got_key:
213	ctfm = crypto_alloc_skcipher(cipher_str, 0, 0);
214	if (!ctfm || IS_ERR(ctfm)) {
215		res = ctfm ? PTR_ERR(ctfm) : -ENOMEM;
216		printk(KERN_DEBUG
217		       "%s: error %d (inode %u) allocating crypto tfm\n",
218		       __func__, res, (unsigned) inode->i_ino);
219		goto out;
220	}
221	crypt_info->ci_ctfm = ctfm;
222	crypto_skcipher_clear_flags(ctfm, ~0);
223	crypto_skcipher_set_flags(ctfm, CRYPTO_TFM_REQ_WEAK_KEY);
224	res = crypto_skcipher_setkey(ctfm, raw_key, fscrypt_key_size(mode));
 
 
 
 
225	if (res)
226		goto out;
227
228	memzero_explicit(raw_key, sizeof(raw_key));
229	if (cmpxchg(&inode->i_crypt_info, NULL, crypt_info) != NULL) {
230		put_crypt_info(crypt_info);
231		goto retry;
 
 
 
 
232	}
233	return 0;
234
235out:
236	if (res == -ENOKEY)
237		res = 0;
238	put_crypt_info(crypt_info);
239	memzero_explicit(raw_key, sizeof(raw_key));
240	return res;
241}
 
242
243void fscrypt_put_encryption_info(struct inode *inode, struct fscrypt_info *ci)
244{
245	struct fscrypt_info *prev;
246
247	if (ci == NULL)
248		ci = ACCESS_ONCE(inode->i_crypt_info);
249	if (ci == NULL)
250		return;
251
252	prev = cmpxchg(&inode->i_crypt_info, ci, NULL);
253	if (prev != ci)
254		return;
255
256	put_crypt_info(ci);
257}
258EXPORT_SYMBOL(fscrypt_put_encryption_info);
259
260int fscrypt_get_encryption_info(struct inode *inode)
261{
262	struct fscrypt_info *ci = inode->i_crypt_info;
263
264	if (!ci ||
265		(ci->ci_keyring_key &&
266		 (ci->ci_keyring_key->flags & ((1 << KEY_FLAG_INVALIDATED) |
267					       (1 << KEY_FLAG_REVOKED) |
268					       (1 << KEY_FLAG_DEAD)))))
269		return get_crypt_info(inode);
270	return 0;
271}
272EXPORT_SYMBOL(fscrypt_get_encryption_info);