evm: digital signature verification support
[profile/ivi/kernel-x86-ivi.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 #include <linux/module.h>
17 #include <linux/crypto.h>
18 #include <linux/xattr.h>
19 #include <keys/encrypted-type.h>
20 #include <crypto/hash.h>
21 #include "evm.h"
22
23 #define EVMKEY "evm-key"
24 #define MAX_KEY_SIZE 128
25 static unsigned char evmkey[MAX_KEY_SIZE];
26 static int evmkey_len = MAX_KEY_SIZE;
27
28 struct crypto_shash *hmac_tfm;
29 struct crypto_shash *hash_tfm;
30
31 static struct shash_desc *init_desc(const char type)
32 {
33         int rc;
34         char *algo;
35         struct crypto_shash **tfm;
36         struct shash_desc *desc;
37
38         if (type == EVM_XATTR_HMAC) {
39                 tfm = &hmac_tfm;
40                 algo = evm_hmac;
41         } else {
42                 tfm = &hash_tfm;
43                 algo = evm_hash;
44         }
45
46         if (*tfm == NULL) {
47                 *tfm = crypto_alloc_shash(algo, 0, CRYPTO_ALG_ASYNC);
48                 if (IS_ERR(*tfm)) {
49                         pr_err("Can not allocate %s (reason: %ld)\n",
50                                algo, PTR_ERR(*tfm));
51                         rc = PTR_ERR(*tfm);
52                         *tfm = NULL;
53                         return ERR_PTR(rc);
54                 }
55         }
56
57         desc = kmalloc(sizeof(*desc) + crypto_shash_descsize(*tfm),
58                         GFP_KERNEL);
59         if (!desc)
60                 return ERR_PTR(-ENOMEM);
61
62         desc->tfm = *tfm;
63         desc->flags = CRYPTO_TFM_REQ_MAY_SLEEP;
64
65         if (type == EVM_XATTR_HMAC) {
66                 rc = crypto_shash_setkey(*tfm, evmkey, evmkey_len);
67                 if (rc)
68                         goto out;
69         }
70
71         rc = crypto_shash_init(desc);
72 out:
73         if (rc) {
74                 kfree(desc);
75                 return ERR_PTR(rc);
76         }
77         return desc;
78 }
79
80 /* Protect against 'cutting & pasting' security.evm xattr, include inode
81  * specific info.
82  *
83  * (Additional directory/file metadata needs to be added for more complete
84  * protection.)
85  */
86 static void hmac_add_misc(struct shash_desc *desc, struct inode *inode,
87                           char *digest)
88 {
89         struct h_misc {
90                 unsigned long ino;
91                 __u32 generation;
92                 uid_t uid;
93                 gid_t gid;
94                 umode_t mode;
95         } hmac_misc;
96
97         memset(&hmac_misc, 0, sizeof hmac_misc);
98         hmac_misc.ino = inode->i_ino;
99         hmac_misc.generation = inode->i_generation;
100         hmac_misc.uid = inode->i_uid;
101         hmac_misc.gid = inode->i_gid;
102         hmac_misc.mode = inode->i_mode;
103         crypto_shash_update(desc, (const u8 *)&hmac_misc, sizeof hmac_misc);
104         crypto_shash_final(desc, digest);
105 }
106
107 /*
108  * Calculate the HMAC value across the set of protected security xattrs.
109  *
110  * Instead of retrieving the requested xattr, for performance, calculate
111  * the hmac using the requested xattr value. Don't alloc/free memory for
112  * each xattr, but attempt to re-use the previously allocated memory.
113  */
114 static int evm_calc_hmac_or_hash(struct dentry *dentry,
115                                 const char *req_xattr_name,
116                                 const char *req_xattr_value,
117                                 size_t req_xattr_value_len,
118                                 char type, char *digest)
119 {
120         struct inode *inode = dentry->d_inode;
121         struct shash_desc *desc;
122         char **xattrname;
123         size_t xattr_size = 0;
124         char *xattr_value = NULL;
125         int error;
126         int size;
127
128         if (!inode->i_op || !inode->i_op->getxattr)
129                 return -EOPNOTSUPP;
130         desc = init_desc(type);
131         if (IS_ERR(desc))
132                 return PTR_ERR(desc);
133
134         error = -ENODATA;
135         for (xattrname = evm_config_xattrnames; *xattrname != NULL; xattrname++) {
136                 if ((req_xattr_name && req_xattr_value)
137                     && !strcmp(*xattrname, req_xattr_name)) {
138                         error = 0;
139                         crypto_shash_update(desc, (const u8 *)req_xattr_value,
140                                              req_xattr_value_len);
141                         continue;
142                 }
143                 size = vfs_getxattr_alloc(dentry, *xattrname,
144                                           &xattr_value, xattr_size, GFP_NOFS);
145                 if (size == -ENOMEM) {
146                         error = -ENOMEM;
147                         goto out;
148                 }
149                 if (size < 0)
150                         continue;
151
152                 error = 0;
153                 xattr_size = size;
154                 crypto_shash_update(desc, (const u8 *)xattr_value, xattr_size);
155         }
156         hmac_add_misc(desc, inode, digest);
157
158 out:
159         kfree(xattr_value);
160         kfree(desc);
161         return error;
162 }
163
164 int evm_calc_hmac(struct dentry *dentry, const char *req_xattr_name,
165                   const char *req_xattr_value, size_t req_xattr_value_len,
166                   char *digest)
167 {
168         return evm_calc_hmac_or_hash(dentry, req_xattr_name, req_xattr_value,
169                                 req_xattr_value_len, EVM_XATTR_HMAC, digest);
170 }
171
172 int evm_calc_hash(struct dentry *dentry, const char *req_xattr_name,
173                   const char *req_xattr_value, size_t req_xattr_value_len,
174                   char *digest)
175 {
176         return evm_calc_hmac_or_hash(dentry, req_xattr_name, req_xattr_value,
177                                 req_xattr_value_len, IMA_XATTR_DIGEST, digest);
178 }
179
180 /*
181  * Calculate the hmac and update security.evm xattr
182  *
183  * Expects to be called with i_mutex locked.
184  */
185 int evm_update_evmxattr(struct dentry *dentry, const char *xattr_name,
186                         const char *xattr_value, size_t xattr_value_len)
187 {
188         struct inode *inode = dentry->d_inode;
189         struct evm_ima_xattr_data xattr_data;
190         int rc = 0;
191
192         rc = evm_calc_hmac(dentry, xattr_name, xattr_value,
193                            xattr_value_len, xattr_data.digest);
194         if (rc == 0) {
195                 xattr_data.type = EVM_XATTR_HMAC;
196                 rc = __vfs_setxattr_noperm(dentry, XATTR_NAME_EVM,
197                                            &xattr_data,
198                                            sizeof(xattr_data), 0);
199         }
200         else if (rc == -ENODATA)
201                 rc = inode->i_op->removexattr(dentry, XATTR_NAME_EVM);
202         return rc;
203 }
204
205 int evm_init_hmac(struct inode *inode, const struct xattr *lsm_xattr,
206                   char *hmac_val)
207 {
208         struct shash_desc *desc;
209
210         desc = init_desc(EVM_XATTR_HMAC);
211         if (IS_ERR(desc)) {
212                 printk(KERN_INFO "init_desc failed\n");
213                 return PTR_ERR(desc);
214         }
215
216         crypto_shash_update(desc, lsm_xattr->value, lsm_xattr->value_len);
217         hmac_add_misc(desc, inode, hmac_val);
218         kfree(desc);
219         return 0;
220 }
221
222 /*
223  * Get the key from the TPM for the SHA1-HMAC
224  */
225 int evm_init_key(void)
226 {
227         struct key *evm_key;
228         struct encrypted_key_payload *ekp;
229         int rc = 0;
230
231         evm_key = request_key(&key_type_encrypted, EVMKEY, NULL);
232         if (IS_ERR(evm_key))
233                 return -ENOENT;
234
235         down_read(&evm_key->sem);
236         ekp = evm_key->payload.data;
237         if (ekp->decrypted_datalen > MAX_KEY_SIZE) {
238                 rc = -EINVAL;
239                 goto out;
240         }
241         memcpy(evmkey, ekp->decrypted_data, ekp->decrypted_datalen);
242 out:
243         /* burn the original key contents */
244         memset(ekp->decrypted_data, 0, ekp->decrypted_datalen);
245         up_read(&evm_key->sem);
246         key_put(evm_key);
247         return rc;
248 }