9308fe4b66e6375959d30ea441d743c4c0cd22c2
[releases.git] / crypto_key.c
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
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 derivation.
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  */
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                 wait_for_completion(&ecr.completion);
75                 res = ecr.res;
76         }
77
78 out:
79         if (req)
80                 ablkcipher_request_free(req);
81         if (tfm)
82                 crypto_free_ablkcipher(tfm);
83         return res;
84 }
85
86 void ext4_free_crypt_info(struct ext4_crypt_info *ci)
87 {
88         if (!ci)
89                 return;
90
91         crypto_free_ablkcipher(ci->ci_ctfm);
92         kmem_cache_free(ext4_crypt_info_cachep, ci);
93 }
94
95 void ext4_free_encryption_info(struct inode *inode,
96                                struct ext4_crypt_info *ci)
97 {
98         struct ext4_inode_info *ei = EXT4_I(inode);
99         struct ext4_crypt_info *prev;
100
101         if (ci == NULL)
102                 ci = ACCESS_ONCE(ei->i_crypt_info);
103         if (ci == NULL)
104                 return;
105         prev = cmpxchg(&ei->i_crypt_info, ci, NULL);
106         if (prev != ci)
107                 return;
108
109         ext4_free_crypt_info(ci);
110 }
111
112 int ext4_get_encryption_info(struct inode *inode)
113 {
114         struct ext4_inode_info *ei = EXT4_I(inode);
115         struct ext4_crypt_info *crypt_info;
116         char full_key_descriptor[EXT4_KEY_DESC_PREFIX_SIZE +
117                                  (EXT4_KEY_DESCRIPTOR_SIZE * 2) + 1];
118         struct key *keyring_key = NULL;
119         struct ext4_encryption_key *master_key;
120         struct ext4_encryption_context ctx;
121         const struct user_key_payload *ukp;
122         struct ext4_sb_info *sbi = EXT4_SB(inode->i_sb);
123         struct crypto_ablkcipher *ctfm;
124         const char *cipher_str;
125         char raw_key[EXT4_MAX_KEY_SIZE];
126         char mode;
127         int res;
128
129         if (ei->i_crypt_info)
130                 return 0;
131
132         res = ext4_init_crypto();
133         if (res)
134                 return res;
135
136         res = ext4_xattr_get(inode, EXT4_XATTR_INDEX_ENCRYPTION,
137                                  EXT4_XATTR_NAME_ENCRYPTION_CONTEXT,
138                                  &ctx, sizeof(ctx));
139         if (res < 0) {
140                 if (!DUMMY_ENCRYPTION_ENABLED(sbi))
141                         return res;
142                 ctx.contents_encryption_mode = EXT4_ENCRYPTION_MODE_AES_256_XTS;
143                 ctx.filenames_encryption_mode =
144                         EXT4_ENCRYPTION_MODE_AES_256_CTS;
145                 ctx.flags = 0;
146         } else if (res != sizeof(ctx))
147                 return -EINVAL;
148         res = 0;
149
150         crypt_info = kmem_cache_alloc(ext4_crypt_info_cachep, GFP_KERNEL);
151         if (!crypt_info)
152                 return -ENOMEM;
153
154         crypt_info->ci_flags = ctx.flags;
155         crypt_info->ci_data_mode = ctx.contents_encryption_mode;
156         crypt_info->ci_filename_mode = ctx.filenames_encryption_mode;
157         crypt_info->ci_ctfm = NULL;
158         memcpy(crypt_info->ci_master_key, ctx.master_key_descriptor,
159                sizeof(crypt_info->ci_master_key));
160         if (S_ISREG(inode->i_mode))
161                 mode = crypt_info->ci_data_mode;
162         else if (S_ISDIR(inode->i_mode) || S_ISLNK(inode->i_mode))
163                 mode = crypt_info->ci_filename_mode;
164         else
165                 BUG();
166         switch (mode) {
167         case EXT4_ENCRYPTION_MODE_AES_256_XTS:
168                 cipher_str = "xts(aes)";
169                 break;
170         case EXT4_ENCRYPTION_MODE_AES_256_CTS:
171                 cipher_str = "cts(cbc(aes))";
172                 break;
173         default:
174                 printk_once(KERN_WARNING
175                             "ext4: unsupported key mode %d (ino %u)\n",
176                             mode, (unsigned) inode->i_ino);
177                 res = -ENOKEY;
178                 goto out;
179         }
180         if (DUMMY_ENCRYPTION_ENABLED(sbi)) {
181                 memset(raw_key, 0x42, EXT4_AES_256_XTS_KEY_SIZE);
182                 goto got_key;
183         }
184         memcpy(full_key_descriptor, EXT4_KEY_DESC_PREFIX,
185                EXT4_KEY_DESC_PREFIX_SIZE);
186         sprintf(full_key_descriptor + EXT4_KEY_DESC_PREFIX_SIZE,
187                 "%*phN", EXT4_KEY_DESCRIPTOR_SIZE,
188                 ctx.master_key_descriptor);
189         full_key_descriptor[EXT4_KEY_DESC_PREFIX_SIZE +
190                             (2 * EXT4_KEY_DESCRIPTOR_SIZE)] = '\0';
191         keyring_key = request_key(&key_type_logon, full_key_descriptor, NULL);
192         if (IS_ERR(keyring_key)) {
193                 res = PTR_ERR(keyring_key);
194                 keyring_key = NULL;
195                 goto out;
196         }
197         if (keyring_key->type != &key_type_logon) {
198                 printk_once(KERN_WARNING
199                             "ext4: key type must be logon\n");
200                 res = -ENOKEY;
201                 goto out;
202         }
203         down_read(&keyring_key->sem);
204         ukp = user_key_payload(keyring_key);
205         if (!ukp) {
206                 /* key was revoked before we acquired its semaphore */
207                 res = -EKEYREVOKED;
208                 up_read(&keyring_key->sem);
209                 goto out;
210         }
211         if (ukp->datalen != sizeof(struct ext4_encryption_key)) {
212                 res = -EINVAL;
213                 up_read(&keyring_key->sem);
214                 goto out;
215         }
216         master_key = (struct ext4_encryption_key *)ukp->data;
217         BUILD_BUG_ON(EXT4_AES_128_ECB_KEY_SIZE !=
218                      EXT4_KEY_DERIVATION_NONCE_SIZE);
219         if (master_key->size != EXT4_AES_256_XTS_KEY_SIZE) {
220                 printk_once(KERN_WARNING
221                             "ext4: key size incorrect: %d\n",
222                             master_key->size);
223                 res = -ENOKEY;
224                 up_read(&keyring_key->sem);
225                 goto out;
226         }
227         res = ext4_derive_key_aes(ctx.nonce, master_key->raw,
228                                   raw_key);
229         up_read(&keyring_key->sem);
230         if (res)
231                 goto out;
232 got_key:
233         ctfm = crypto_alloc_ablkcipher(cipher_str, 0, 0);
234         if (!ctfm || IS_ERR(ctfm)) {
235                 res = ctfm ? PTR_ERR(ctfm) : -ENOMEM;
236                 printk(KERN_DEBUG
237                        "%s: error %d (inode %u) allocating crypto tfm\n",
238                        __func__, res, (unsigned) inode->i_ino);
239                 goto out;
240         }
241         crypt_info->ci_ctfm = ctfm;
242         crypto_ablkcipher_clear_flags(ctfm, ~0);
243         crypto_tfm_set_flags(crypto_ablkcipher_tfm(ctfm),
244                              CRYPTO_TFM_REQ_WEAK_KEY);
245         res = crypto_ablkcipher_setkey(ctfm, raw_key,
246                                        ext4_encryption_key_size(mode));
247         if (res)
248                 goto out;
249
250         if (cmpxchg(&ei->i_crypt_info, NULL, crypt_info) == NULL)
251                 crypt_info = NULL;
252 out:
253         if (res == -ENOKEY)
254                 res = 0;
255         key_put(keyring_key);
256         ext4_free_crypt_info(crypt_info);
257         memzero_explicit(raw_key, sizeof(raw_key));
258         return res;
259 }
260
261 int ext4_has_encryption_key(struct inode *inode)
262 {
263         struct ext4_inode_info *ei = EXT4_I(inode);
264
265         return (ei->i_crypt_info != NULL);
266 }