GNU Linux-libre 4.9.309-gnu1
[releases.git] / security / integrity / evm / evm_crypto.c
1 /*
2  * Copyright (C) 2005-2010 IBM Corporation
3  *
4  * Authors:
5  * Mimi Zohar <zohar@us.ibm.com>
6  * Kylene Hall <kjhall@us.ibm.com>
7  *
8  * This program is free software; you can redistribute it and/or modify
9  * it under the terms of the GNU General Public License as published by
10  * the Free Software Foundation, version 2 of the License.
11  *
12  * File: evm_crypto.c
13  *       Using root's kernel master key (kmk), calculate the HMAC
14  */
15
16 #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
17
18 #include <linux/module.h>
19 #include <linux/crypto.h>
20 #include <linux/xattr.h>
21 #include <linux/evm.h>
22 #include <keys/encrypted-type.h>
23 #include <crypto/hash.h>
24 #include "evm.h"
25
26 #define EVMKEY "evm-key"
27 #define MAX_KEY_SIZE 128
28 static unsigned char evmkey[MAX_KEY_SIZE];
29 static int evmkey_len = MAX_KEY_SIZE;
30
31 struct crypto_shash *hmac_tfm;
32 struct crypto_shash *hash_tfm;
33
34 static DEFINE_MUTEX(mutex);
35
36 #define EVM_SET_KEY_BUSY 0
37
38 static unsigned long evm_set_key_flags;
39
40 /**
41  * evm_set_key() - set EVM HMAC key from the kernel
42  * @key: pointer to a buffer with the key data
43  * @size: length of the key data
44  *
45  * This function allows setting the EVM HMAC key from the kernel
46  * without using the "encrypted" key subsystem keys. It can be used
47  * by the crypto HW kernel module which has its own way of managing
48  * keys.
49  *
50  * key length should be between 32 and 128 bytes long
51  */
52 int evm_set_key(void *key, size_t keylen)
53 {
54         int rc;
55
56         rc = -EBUSY;
57         if (test_and_set_bit(EVM_SET_KEY_BUSY, &evm_set_key_flags))
58                 goto busy;
59         rc = -EINVAL;
60         if (keylen > MAX_KEY_SIZE)
61                 goto inval;
62         memcpy(evmkey, key, keylen);
63         evm_initialized |= EVM_INIT_HMAC;
64         pr_info("key initialized\n");
65         return 0;
66 inval:
67         clear_bit(EVM_SET_KEY_BUSY, &evm_set_key_flags);
68 busy:
69         pr_err("key initialization failed\n");
70         return rc;
71 }
72 EXPORT_SYMBOL_GPL(evm_set_key);
73
74 static struct shash_desc *init_desc(char type)
75 {
76         long rc;
77         char *algo;
78         struct crypto_shash **tfm;
79         struct shash_desc *desc;
80
81         if (type == EVM_XATTR_HMAC) {
82                 if (!(evm_initialized & EVM_INIT_HMAC)) {
83                         pr_err("HMAC key is not set\n");
84                         return ERR_PTR(-ENOKEY);
85                 }
86                 tfm = &hmac_tfm;
87                 algo = evm_hmac;
88         } else {
89                 tfm = &hash_tfm;
90                 algo = evm_hash;
91         }
92
93         if (IS_ERR_OR_NULL(*tfm)) {
94                 mutex_lock(&mutex);
95                 if (*tfm)
96                         goto out;
97                 *tfm = crypto_alloc_shash(algo, 0,
98                                           CRYPTO_ALG_ASYNC | CRYPTO_NOLOAD);
99                 if (IS_ERR(*tfm)) {
100                         rc = PTR_ERR(*tfm);
101                         pr_err("Can not allocate %s (reason: %ld)\n", algo, rc);
102                         *tfm = NULL;
103                         mutex_unlock(&mutex);
104                         return ERR_PTR(rc);
105                 }
106                 if (type == EVM_XATTR_HMAC) {
107                         rc = crypto_shash_setkey(*tfm, evmkey, evmkey_len);
108                         if (rc) {
109                                 crypto_free_shash(*tfm);
110                                 *tfm = NULL;
111                                 mutex_unlock(&mutex);
112                                 return ERR_PTR(rc);
113                         }
114                 }
115 out:
116                 mutex_unlock(&mutex);
117         }
118
119         desc = kmalloc(sizeof(*desc) + crypto_shash_descsize(*tfm),
120                         GFP_KERNEL);
121         if (!desc)
122                 return ERR_PTR(-ENOMEM);
123
124         desc->tfm = *tfm;
125         desc->flags = CRYPTO_TFM_REQ_MAY_SLEEP;
126
127         rc = crypto_shash_init(desc);
128         if (rc) {
129                 kfree(desc);
130                 return ERR_PTR(rc);
131         }
132         return desc;
133 }
134
135 /* Protect against 'cutting & pasting' security.evm xattr, include inode
136  * specific info.
137  *
138  * (Additional directory/file metadata needs to be added for more complete
139  * protection.)
140  */
141 static void hmac_add_misc(struct shash_desc *desc, struct inode *inode,
142                           char type, char *digest)
143 {
144         struct h_misc {
145                 unsigned long ino;
146                 __u32 generation;
147                 uid_t uid;
148                 gid_t gid;
149                 umode_t mode;
150         } hmac_misc;
151
152         memset(&hmac_misc, 0, sizeof(hmac_misc));
153         /* Don't include the inode or generation number in portable
154          * signatures
155          */
156         if (type != EVM_XATTR_PORTABLE_DIGSIG) {
157                 hmac_misc.ino = inode->i_ino;
158                 hmac_misc.generation = inode->i_generation;
159         }
160         /* The hmac uid and gid must be encoded in the initial user
161          * namespace (not the filesystems user namespace) as encoding
162          * them in the filesystems user namespace allows an attack
163          * where first they are written in an unprivileged fuse mount
164          * of a filesystem and then the system is tricked to mount the
165          * filesystem for real on next boot and trust it because
166          * everything is signed.
167          */
168         hmac_misc.uid = from_kuid(&init_user_ns, inode->i_uid);
169         hmac_misc.gid = from_kgid(&init_user_ns, inode->i_gid);
170         hmac_misc.mode = inode->i_mode;
171         crypto_shash_update(desc, (const u8 *)&hmac_misc, sizeof(hmac_misc));
172         if ((evm_hmac_attrs & EVM_ATTR_FSUUID) &&
173             type != EVM_XATTR_PORTABLE_DIGSIG)
174                 crypto_shash_update(desc, inode->i_sb->s_uuid,
175                                     sizeof(inode->i_sb->s_uuid));
176         crypto_shash_final(desc, digest);
177 }
178
179 /*
180  * Calculate the HMAC value across the set of protected security xattrs.
181  *
182  * Instead of retrieving the requested xattr, for performance, calculate
183  * the hmac using the requested xattr value. Don't alloc/free memory for
184  * each xattr, but attempt to re-use the previously allocated memory.
185  */
186 static int evm_calc_hmac_or_hash(struct dentry *dentry,
187                                 const char *req_xattr_name,
188                                 const char *req_xattr_value,
189                                 size_t req_xattr_value_len,
190                                 char type, char *digest)
191 {
192         struct inode *inode = d_backing_inode(dentry);
193         struct shash_desc *desc;
194         char **xattrname;
195         size_t xattr_size = 0;
196         char *xattr_value = NULL;
197         int error;
198         int size;
199         bool ima_present = false;
200
201         if (!(inode->i_opflags & IOP_XATTR))
202                 return -EOPNOTSUPP;
203
204         desc = init_desc(type);
205         if (IS_ERR(desc))
206                 return PTR_ERR(desc);
207
208         error = -ENODATA;
209         for (xattrname = evm_config_xattrnames; *xattrname != NULL; xattrname++) {
210                 bool is_ima = false;
211
212                 if (strcmp(*xattrname, XATTR_NAME_IMA) == 0)
213                         is_ima = true;
214
215                 if ((req_xattr_name && req_xattr_value)
216                     && !strcmp(*xattrname, req_xattr_name)) {
217                         error = 0;
218                         crypto_shash_update(desc, (const u8 *)req_xattr_value,
219                                              req_xattr_value_len);
220                         if (is_ima)
221                                 ima_present = true;
222                         continue;
223                 }
224                 size = vfs_getxattr_alloc(dentry, *xattrname,
225                                           &xattr_value, xattr_size, GFP_NOFS);
226                 if (size == -ENOMEM) {
227                         error = -ENOMEM;
228                         goto out;
229                 }
230                 if (size < 0)
231                         continue;
232
233                 error = 0;
234                 xattr_size = size;
235                 crypto_shash_update(desc, (const u8 *)xattr_value, xattr_size);
236                 if (is_ima)
237                         ima_present = true;
238         }
239         hmac_add_misc(desc, inode, type, digest);
240
241         /* Portable EVM signatures must include an IMA hash */
242         if (type == EVM_XATTR_PORTABLE_DIGSIG && !ima_present)
243                 error = -EPERM;
244 out:
245         kfree(xattr_value);
246         kfree(desc);
247         return error;
248 }
249
250 int evm_calc_hmac(struct dentry *dentry, const char *req_xattr_name,
251                   const char *req_xattr_value, size_t req_xattr_value_len,
252                   char *digest)
253 {
254         return evm_calc_hmac_or_hash(dentry, req_xattr_name, req_xattr_value,
255                                req_xattr_value_len, EVM_XATTR_HMAC, digest);
256 }
257
258 int evm_calc_hash(struct dentry *dentry, const char *req_xattr_name,
259                   const char *req_xattr_value, size_t req_xattr_value_len,
260                   char type, char *digest)
261 {
262         return evm_calc_hmac_or_hash(dentry, req_xattr_name, req_xattr_value,
263                                      req_xattr_value_len, type, digest);
264 }
265
266 static int evm_is_immutable(struct dentry *dentry, struct inode *inode)
267 {
268         const struct evm_ima_xattr_data *xattr_data = NULL;
269         struct integrity_iint_cache *iint;
270         int rc = 0;
271
272         iint = integrity_iint_find(inode);
273         if (iint && (iint->flags & EVM_IMMUTABLE_DIGSIG))
274                 return 1;
275
276         /* Do this the hard way */
277         rc = vfs_getxattr_alloc(dentry, XATTR_NAME_EVM, (char **)&xattr_data, 0,
278                                 GFP_NOFS);
279         if (rc <= 0) {
280                 if (rc == -ENODATA)
281                         return 0;
282                 return rc;
283         }
284         if (xattr_data->type == EVM_XATTR_PORTABLE_DIGSIG)
285                 rc = 1;
286         else
287                 rc = 0;
288
289         kfree(xattr_data);
290         return rc;
291 }
292
293
294 /*
295  * Calculate the hmac and update security.evm xattr
296  *
297  * Expects to be called with i_mutex locked.
298  */
299 int evm_update_evmxattr(struct dentry *dentry, const char *xattr_name,
300                         const char *xattr_value, size_t xattr_value_len)
301 {
302         struct inode *inode = d_backing_inode(dentry);
303         struct evm_ima_xattr_data xattr_data;
304         int rc = 0;
305
306         /*
307          * Don't permit any transformation of the EVM xattr if the signature
308          * is of an immutable type
309          */
310         rc = evm_is_immutable(dentry, inode);
311         if (rc < 0)
312                 return rc;
313         if (rc)
314                 return -EPERM;
315
316         rc = evm_calc_hmac(dentry, xattr_name, xattr_value,
317                            xattr_value_len, xattr_data.digest);
318         if (rc == 0) {
319                 xattr_data.type = EVM_XATTR_HMAC;
320                 rc = __vfs_setxattr_noperm(dentry, XATTR_NAME_EVM,
321                                            &xattr_data,
322                                            sizeof(xattr_data), 0);
323         } else if (rc == -ENODATA && (inode->i_opflags & IOP_XATTR)) {
324                 rc = __vfs_removexattr(dentry, XATTR_NAME_EVM);
325         }
326         return rc;
327 }
328
329 int evm_init_hmac(struct inode *inode, const struct xattr *lsm_xattr,
330                   char *hmac_val)
331 {
332         struct shash_desc *desc;
333
334         desc = init_desc(EVM_XATTR_HMAC);
335         if (IS_ERR(desc)) {
336                 pr_info("init_desc failed\n");
337                 return PTR_ERR(desc);
338         }
339
340         crypto_shash_update(desc, lsm_xattr->value, lsm_xattr->value_len);
341         hmac_add_misc(desc, inode, EVM_XATTR_HMAC, hmac_val);
342         kfree(desc);
343         return 0;
344 }
345
346 /*
347  * Get the key from the TPM for the SHA1-HMAC
348  */
349 int evm_init_key(void)
350 {
351         struct key *evm_key;
352         struct encrypted_key_payload *ekp;
353         int rc;
354
355         evm_key = request_key(&key_type_encrypted, EVMKEY, NULL);
356         if (IS_ERR(evm_key))
357                 return -ENOENT;
358
359         down_read(&evm_key->sem);
360         ekp = evm_key->payload.data[0];
361
362         rc = evm_set_key(ekp->decrypted_data, ekp->decrypted_datalen);
363
364         /* burn the original key contents */
365         memset(ekp->decrypted_data, 0, ekp->decrypted_datalen);
366         up_read(&evm_key->sem);
367         key_put(evm_key);
368         return rc;
369 }