• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * linux/fs/ext4/crypto_key.c
3  *
4  * Copyright (C) 2015, Google, Inc.
5  *
6  * This contains encryption key functions for ext4
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 
17 #include "ext4.h"
18 #include "xattr.h"
19 
derive_crypt_complete(struct crypto_async_request * req,int rc)20 static void derive_crypt_complete(struct crypto_async_request *req, int rc)
21 {
22 	struct ext4_completion_result *ecr = req->data;
23 
24 	if (rc == -EINPROGRESS)
25 		return;
26 
27 	ecr->res = rc;
28 	complete(&ecr->completion);
29 }
30 
31 /**
32  * ext4_derive_key_aes() - Derive a key using AES-128-ECB
33  * @deriving_key: Encryption key used for derivatio.
34  * @source_key:   Source key to which to apply derivation.
35  * @derived_key:  Derived key.
36  *
37  * Return: Zero on success; non-zero otherwise.
38  */
ext4_derive_key_aes(char deriving_key[EXT4_AES_128_ECB_KEY_SIZE],char source_key[EXT4_AES_256_XTS_KEY_SIZE],char derived_key[EXT4_AES_256_XTS_KEY_SIZE])39 static int ext4_derive_key_aes(char deriving_key[EXT4_AES_128_ECB_KEY_SIZE],
40 			       char source_key[EXT4_AES_256_XTS_KEY_SIZE],
41 			       char derived_key[EXT4_AES_256_XTS_KEY_SIZE])
42 {
43 	int res = 0;
44 	struct ablkcipher_request *req = NULL;
45 	DECLARE_EXT4_COMPLETION_RESULT(ecr);
46 	struct scatterlist src_sg, dst_sg;
47 	struct crypto_ablkcipher *tfm = crypto_alloc_ablkcipher("ecb(aes)", 0,
48 								0);
49 
50 	if (IS_ERR(tfm)) {
51 		res = PTR_ERR(tfm);
52 		tfm = NULL;
53 		goto out;
54 	}
55 	crypto_ablkcipher_set_flags(tfm, CRYPTO_TFM_REQ_WEAK_KEY);
56 	req = ablkcipher_request_alloc(tfm, GFP_NOFS);
57 	if (!req) {
58 		res = -ENOMEM;
59 		goto out;
60 	}
61 	ablkcipher_request_set_callback(req,
62 			CRYPTO_TFM_REQ_MAY_BACKLOG | CRYPTO_TFM_REQ_MAY_SLEEP,
63 			derive_crypt_complete, &ecr);
64 	res = crypto_ablkcipher_setkey(tfm, deriving_key,
65 				       EXT4_AES_128_ECB_KEY_SIZE);
66 	if (res < 0)
67 		goto out;
68 	sg_init_one(&src_sg, source_key, EXT4_AES_256_XTS_KEY_SIZE);
69 	sg_init_one(&dst_sg, derived_key, EXT4_AES_256_XTS_KEY_SIZE);
70 	ablkcipher_request_set_crypt(req, &src_sg, &dst_sg,
71 				     EXT4_AES_256_XTS_KEY_SIZE, NULL);
72 	res = crypto_ablkcipher_encrypt(req);
73 	if (res == -EINPROGRESS || res == -EBUSY) {
74 		BUG_ON(req->base.data != &ecr);
75 		wait_for_completion(&ecr.completion);
76 		res = ecr.res;
77 	}
78 
79 out:
80 	if (req)
81 		ablkcipher_request_free(req);
82 	if (tfm)
83 		crypto_free_ablkcipher(tfm);
84 	return res;
85 }
86 
ext4_free_crypt_info(struct ext4_crypt_info * ci)87 void ext4_free_crypt_info(struct ext4_crypt_info *ci)
88 {
89 	if (!ci)
90 		return;
91 
92 	if (ci->ci_keyring_key)
93 		key_put(ci->ci_keyring_key);
94 	crypto_free_ablkcipher(ci->ci_ctfm);
95 	kmem_cache_free(ext4_crypt_info_cachep, ci);
96 }
97 
ext4_free_encryption_info(struct inode * inode,struct ext4_crypt_info * ci)98 void ext4_free_encryption_info(struct inode *inode,
99 			       struct ext4_crypt_info *ci)
100 {
101 	struct ext4_inode_info *ei = EXT4_I(inode);
102 	struct ext4_crypt_info *prev;
103 
104 	if (ci == NULL)
105 		ci = ACCESS_ONCE(ei->i_crypt_info);
106 	if (ci == NULL)
107 		return;
108 	prev = cmpxchg(&ei->i_crypt_info, ci, NULL);
109 	if (prev != ci)
110 		return;
111 
112 	ext4_free_crypt_info(ci);
113 }
114 
_ext4_get_encryption_info(struct inode * inode)115 int _ext4_get_encryption_info(struct inode *inode)
116 {
117 	struct ext4_inode_info *ei = EXT4_I(inode);
118 	struct ext4_crypt_info *crypt_info;
119 	char full_key_descriptor[EXT4_KEY_DESC_PREFIX_SIZE +
120 				 (EXT4_KEY_DESCRIPTOR_SIZE * 2) + 1];
121 	struct key *keyring_key = NULL;
122 	struct ext4_encryption_key *master_key;
123 	struct ext4_encryption_context ctx;
124 	struct user_key_payload *ukp;
125 	struct ext4_sb_info *sbi = EXT4_SB(inode->i_sb);
126 	struct crypto_ablkcipher *ctfm;
127 	const char *cipher_str;
128 	char raw_key[EXT4_MAX_KEY_SIZE];
129 	char mode;
130 	int res;
131 
132 	if (!ext4_read_workqueue) {
133 		res = ext4_init_crypto();
134 		if (res)
135 			return res;
136 	}
137 
138 retry:
139 	crypt_info = ACCESS_ONCE(ei->i_crypt_info);
140 	if (crypt_info) {
141 		if (!crypt_info->ci_keyring_key ||
142 		    key_validate(crypt_info->ci_keyring_key) == 0)
143 			return 0;
144 		ext4_free_encryption_info(inode, crypt_info);
145 		goto retry;
146 	}
147 
148 	res = ext4_xattr_get(inode, EXT4_XATTR_INDEX_ENCRYPTION,
149 				 EXT4_XATTR_NAME_ENCRYPTION_CONTEXT,
150 				 &ctx, sizeof(ctx));
151 	if (res < 0) {
152 		if (!DUMMY_ENCRYPTION_ENABLED(sbi))
153 			return res;
154 		ctx.contents_encryption_mode = EXT4_ENCRYPTION_MODE_AES_256_XTS;
155 		ctx.filenames_encryption_mode =
156 			EXT4_ENCRYPTION_MODE_AES_256_CTS;
157 		ctx.flags = 0;
158 	} else if (res != sizeof(ctx))
159 		return -EINVAL;
160 	res = 0;
161 
162 	crypt_info = kmem_cache_alloc(ext4_crypt_info_cachep, GFP_KERNEL);
163 	if (!crypt_info)
164 		return -ENOMEM;
165 
166 	crypt_info->ci_flags = ctx.flags;
167 	crypt_info->ci_data_mode = ctx.contents_encryption_mode;
168 	crypt_info->ci_filename_mode = ctx.filenames_encryption_mode;
169 	crypt_info->ci_ctfm = NULL;
170 	crypt_info->ci_keyring_key = NULL;
171 	memcpy(crypt_info->ci_master_key, ctx.master_key_descriptor,
172 	       sizeof(crypt_info->ci_master_key));
173 	if (S_ISREG(inode->i_mode))
174 		mode = crypt_info->ci_data_mode;
175 	else if (S_ISDIR(inode->i_mode) || S_ISLNK(inode->i_mode))
176 		mode = crypt_info->ci_filename_mode;
177 	else
178 		BUG();
179 	switch (mode) {
180 	case EXT4_ENCRYPTION_MODE_AES_256_XTS:
181 		cipher_str = "xts(aes)";
182 		break;
183 	case EXT4_ENCRYPTION_MODE_AES_256_CTS:
184 		cipher_str = "cts(cbc(aes))";
185 		break;
186 	default:
187 		printk_once(KERN_WARNING
188 			    "ext4: unsupported key mode %d (ino %u)\n",
189 			    mode, (unsigned) inode->i_ino);
190 		res = -ENOKEY;
191 		goto out;
192 	}
193 	if (DUMMY_ENCRYPTION_ENABLED(sbi)) {
194 		memset(raw_key, 0x42, EXT4_AES_256_XTS_KEY_SIZE);
195 		goto got_key;
196 	}
197 	memcpy(full_key_descriptor, EXT4_KEY_DESC_PREFIX,
198 	       EXT4_KEY_DESC_PREFIX_SIZE);
199 	sprintf(full_key_descriptor + EXT4_KEY_DESC_PREFIX_SIZE,
200 		"%*phN", EXT4_KEY_DESCRIPTOR_SIZE,
201 		ctx.master_key_descriptor);
202 	full_key_descriptor[EXT4_KEY_DESC_PREFIX_SIZE +
203 			    (2 * EXT4_KEY_DESCRIPTOR_SIZE)] = '\0';
204 	keyring_key = request_key(&key_type_logon, full_key_descriptor, NULL);
205 	if (IS_ERR(keyring_key)) {
206 		res = PTR_ERR(keyring_key);
207 		keyring_key = NULL;
208 		goto out;
209 	}
210 	crypt_info->ci_keyring_key = keyring_key;
211 	BUG_ON(keyring_key->type != &key_type_logon);
212 	ukp = ((struct user_key_payload *)keyring_key->payload.data);
213 	if (ukp->datalen != sizeof(struct ext4_encryption_key)) {
214 		res = -EINVAL;
215 		goto out;
216 	}
217 	master_key = (struct ext4_encryption_key *)ukp->data;
218 	BUILD_BUG_ON(EXT4_AES_128_ECB_KEY_SIZE !=
219 		     EXT4_KEY_DERIVATION_NONCE_SIZE);
220 	BUG_ON(master_key->size != EXT4_AES_256_XTS_KEY_SIZE);
221 	res = ext4_derive_key_aes(ctx.nonce, master_key->raw,
222 				  raw_key);
223 got_key:
224 	ctfm = crypto_alloc_ablkcipher(cipher_str, 0, 0);
225 	if (!ctfm || IS_ERR(ctfm)) {
226 		res = ctfm ? PTR_ERR(ctfm) : -ENOMEM;
227 		printk(KERN_DEBUG
228 		       "%s: error %d (inode %u) allocating crypto tfm\n",
229 		       __func__, res, (unsigned) inode->i_ino);
230 		goto out;
231 	}
232 	crypt_info->ci_ctfm = ctfm;
233 	crypto_ablkcipher_clear_flags(ctfm, ~0);
234 	crypto_tfm_set_flags(crypto_ablkcipher_tfm(ctfm),
235 			     CRYPTO_TFM_REQ_WEAK_KEY);
236 	res = crypto_ablkcipher_setkey(ctfm, raw_key,
237 				       ext4_encryption_key_size(mode));
238 	if (res)
239 		goto out;
240 	memset(raw_key, 0, sizeof(raw_key));
241 	if (cmpxchg(&ei->i_crypt_info, NULL, crypt_info) != NULL) {
242 		ext4_free_crypt_info(crypt_info);
243 		goto retry;
244 	}
245 	return 0;
246 
247 out:
248 	if (res == -ENOKEY)
249 		res = 0;
250 	ext4_free_crypt_info(crypt_info);
251 	memset(raw_key, 0, sizeof(raw_key));
252 	return res;
253 }
254 
ext4_has_encryption_key(struct inode * inode)255 int ext4_has_encryption_key(struct inode *inode)
256 {
257 	struct ext4_inode_info *ei = EXT4_I(inode);
258 
259 	return (ei->i_crypt_info != NULL);
260 }
261