Merge branch 'stable-4.6' of git://git.infradead.org/users/pcmoore/audit
[deliverable/linux.git] / security / integrity / evm / evm_crypto.c
CommitLineData
66dbc325
MZ
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
20ee451f
JP
16#define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
17
66dbc325
MZ
18#include <linux/module.h>
19#include <linux/crypto.h>
20#include <linux/xattr.h>
76266763 21#include <linux/evm.h>
66dbc325 22#include <keys/encrypted-type.h>
d46eb369 23#include <crypto/hash.h>
66dbc325
MZ
24#include "evm.h"
25
26#define EVMKEY "evm-key"
27#define MAX_KEY_SIZE 128
28static unsigned char evmkey[MAX_KEY_SIZE];
29static int evmkey_len = MAX_KEY_SIZE;
30
d46eb369 31struct crypto_shash *hmac_tfm;
15647eb3 32struct crypto_shash *hash_tfm;
d46eb369 33
97426f98
DK
34static DEFINE_MUTEX(mutex);
35
76266763
DK
36#define EVM_SET_KEY_BUSY 0
37
38static 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 */
52int 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;
66inval:
67 clear_bit(EVM_SET_KEY_BUSY, &evm_set_key_flags);
68busy:
69 pr_err("key initialization failed\n");
70 return rc;
71}
72EXPORT_SYMBOL_GPL(evm_set_key);
73
8fcc9954 74static struct shash_desc *init_desc(char type)
66dbc325 75{
143b01d3 76 long rc;
15647eb3
DK
77 char *algo;
78 struct crypto_shash **tfm;
d46eb369
DK
79 struct shash_desc *desc;
80
15647eb3 81 if (type == EVM_XATTR_HMAC) {
26ddabfe
DK
82 if (!(evm_initialized & EVM_INIT_HMAC)) {
83 pr_err("HMAC key is not set\n");
84 return ERR_PTR(-ENOKEY);
85 }
15647eb3
DK
86 tfm = &hmac_tfm;
87 algo = evm_hmac;
88 } else {
89 tfm = &hash_tfm;
90 algo = evm_hash;
91 }
92
93 if (*tfm == NULL) {
97426f98 94 mutex_lock(&mutex);
143b01d3 95 if (*tfm)
97426f98 96 goto out;
15647eb3
DK
97 *tfm = crypto_alloc_shash(algo, 0, CRYPTO_ALG_ASYNC);
98 if (IS_ERR(*tfm)) {
15647eb3 99 rc = PTR_ERR(*tfm);
143b01d3 100 pr_err("Can not allocate %s (reason: %ld)\n", algo, rc);
15647eb3 101 *tfm = NULL;
97426f98 102 mutex_unlock(&mutex);
d46eb369
DK
103 return ERR_PTR(rc);
104 }
88d7ed35
DK
105 if (type == EVM_XATTR_HMAC) {
106 rc = crypto_shash_setkey(*tfm, evmkey, evmkey_len);
107 if (rc) {
108 crypto_free_shash(*tfm);
109 *tfm = NULL;
143b01d3 110 mutex_unlock(&mutex);
88d7ed35
DK
111 return ERR_PTR(rc);
112 }
d21b5945 113 }
97426f98
DK
114out:
115 mutex_unlock(&mutex);
66dbc325 116 }
d46eb369 117
15647eb3 118 desc = kmalloc(sizeof(*desc) + crypto_shash_descsize(*tfm),
d46eb369
DK
119 GFP_KERNEL);
120 if (!desc)
121 return ERR_PTR(-ENOMEM);
122
15647eb3 123 desc->tfm = *tfm;
d46eb369
DK
124 desc->flags = CRYPTO_TFM_REQ_MAY_SLEEP;
125
d46eb369 126 rc = crypto_shash_init(desc);
d46eb369
DK
127 if (rc) {
128 kfree(desc);
129 return ERR_PTR(rc);
130 }
131 return desc;
66dbc325
MZ
132}
133
134/* Protect against 'cutting & pasting' security.evm xattr, include inode
135 * specific info.
136 *
137 * (Additional directory/file metadata needs to be added for more complete
138 * protection.)
139 */
d46eb369 140static void hmac_add_misc(struct shash_desc *desc, struct inode *inode,
66dbc325
MZ
141 char *digest)
142{
143 struct h_misc {
144 unsigned long ino;
145 __u32 generation;
146 uid_t uid;
147 gid_t gid;
148 umode_t mode;
149 } hmac_misc;
66dbc325 150
2bb930ab 151 memset(&hmac_misc, 0, sizeof(hmac_misc));
66dbc325
MZ
152 hmac_misc.ino = inode->i_ino;
153 hmac_misc.generation = inode->i_generation;
cf9c9352
EB
154 hmac_misc.uid = from_kuid(&init_user_ns, inode->i_uid);
155 hmac_misc.gid = from_kgid(&init_user_ns, inode->i_gid);
66dbc325 156 hmac_misc.mode = inode->i_mode;
2bb930ab 157 crypto_shash_update(desc, (const u8 *)&hmac_misc, sizeof(hmac_misc));
d3b33679 158 if (evm_hmac_attrs & EVM_ATTR_FSUUID)
74de6684
DK
159 crypto_shash_update(desc, inode->i_sb->s_uuid,
160 sizeof(inode->i_sb->s_uuid));
d46eb369 161 crypto_shash_final(desc, digest);
66dbc325
MZ
162}
163
164/*
165 * Calculate the HMAC value across the set of protected security xattrs.
166 *
167 * Instead of retrieving the requested xattr, for performance, calculate
168 * the hmac using the requested xattr value. Don't alloc/free memory for
169 * each xattr, but attempt to re-use the previously allocated memory.
170 */
15647eb3
DK
171static int evm_calc_hmac_or_hash(struct dentry *dentry,
172 const char *req_xattr_name,
173 const char *req_xattr_value,
174 size_t req_xattr_value_len,
175 char type, char *digest)
66dbc325 176{
c6f493d6 177 struct inode *inode = d_backing_inode(dentry);
d46eb369 178 struct shash_desc *desc;
66dbc325
MZ
179 char **xattrname;
180 size_t xattr_size = 0;
181 char *xattr_value = NULL;
182 int error;
183 int size;
184
627bf81a 185 if (!inode->i_op->getxattr)
66dbc325 186 return -EOPNOTSUPP;
15647eb3 187 desc = init_desc(type);
d46eb369
DK
188 if (IS_ERR(desc))
189 return PTR_ERR(desc);
66dbc325
MZ
190
191 error = -ENODATA;
192 for (xattrname = evm_config_xattrnames; *xattrname != NULL; xattrname++) {
193 if ((req_xattr_name && req_xattr_value)
194 && !strcmp(*xattrname, req_xattr_name)) {
195 error = 0;
d46eb369
DK
196 crypto_shash_update(desc, (const u8 *)req_xattr_value,
197 req_xattr_value_len);
66dbc325
MZ
198 continue;
199 }
200 size = vfs_getxattr_alloc(dentry, *xattrname,
201 &xattr_value, xattr_size, GFP_NOFS);
202 if (size == -ENOMEM) {
203 error = -ENOMEM;
204 goto out;
205 }
206 if (size < 0)
207 continue;
208
209 error = 0;
210 xattr_size = size;
d46eb369 211 crypto_shash_update(desc, (const u8 *)xattr_value, xattr_size);
66dbc325 212 }
d46eb369
DK
213 hmac_add_misc(desc, inode, digest);
214
66dbc325 215out:
d46eb369
DK
216 kfree(xattr_value);
217 kfree(desc);
66dbc325
MZ
218 return error;
219}
220
15647eb3
DK
221int evm_calc_hmac(struct dentry *dentry, const char *req_xattr_name,
222 const char *req_xattr_value, size_t req_xattr_value_len,
223 char *digest)
224{
225 return evm_calc_hmac_or_hash(dentry, req_xattr_name, req_xattr_value,
226 req_xattr_value_len, EVM_XATTR_HMAC, digest);
227}
228
229int evm_calc_hash(struct dentry *dentry, const char *req_xattr_name,
230 const char *req_xattr_value, size_t req_xattr_value_len,
231 char *digest)
232{
233 return evm_calc_hmac_or_hash(dentry, req_xattr_name, req_xattr_value,
234 req_xattr_value_len, IMA_XATTR_DIGEST, digest);
235}
236
66dbc325
MZ
237/*
238 * Calculate the hmac and update security.evm xattr
239 *
240 * Expects to be called with i_mutex locked.
241 */
242int evm_update_evmxattr(struct dentry *dentry, const char *xattr_name,
243 const char *xattr_value, size_t xattr_value_len)
244{
c6f493d6 245 struct inode *inode = d_backing_inode(dentry);
6be5cc52 246 struct evm_ima_xattr_data xattr_data;
66dbc325
MZ
247 int rc = 0;
248
249 rc = evm_calc_hmac(dentry, xattr_name, xattr_value,
6be5cc52
DK
250 xattr_value_len, xattr_data.digest);
251 if (rc == 0) {
252 xattr_data.type = EVM_XATTR_HMAC;
66dbc325 253 rc = __vfs_setxattr_noperm(dentry, XATTR_NAME_EVM,
6be5cc52
DK
254 &xattr_data,
255 sizeof(xattr_data), 0);
a67adb99 256 } else if (rc == -ENODATA && inode->i_op->removexattr) {
66dbc325 257 rc = inode->i_op->removexattr(dentry, XATTR_NAME_EVM);
a67adb99 258 }
66dbc325
MZ
259 return rc;
260}
261
cb723180
MZ
262int evm_init_hmac(struct inode *inode, const struct xattr *lsm_xattr,
263 char *hmac_val)
264{
d46eb369 265 struct shash_desc *desc;
cb723180 266
15647eb3 267 desc = init_desc(EVM_XATTR_HMAC);
d46eb369 268 if (IS_ERR(desc)) {
20ee451f 269 pr_info("init_desc failed\n");
d46eb369 270 return PTR_ERR(desc);
cb723180
MZ
271 }
272
d46eb369
DK
273 crypto_shash_update(desc, lsm_xattr->value, lsm_xattr->value_len);
274 hmac_add_misc(desc, inode, hmac_val);
275 kfree(desc);
cb723180
MZ
276 return 0;
277}
278
66dbc325
MZ
279/*
280 * Get the key from the TPM for the SHA1-HMAC
281 */
282int evm_init_key(void)
283{
284 struct key *evm_key;
285 struct encrypted_key_payload *ekp;
76266763 286 int rc;
66dbc325
MZ
287
288 evm_key = request_key(&key_type_encrypted, EVMKEY, NULL);
289 if (IS_ERR(evm_key))
290 return -ENOENT;
291
292 down_read(&evm_key->sem);
146aa8b1 293 ekp = evm_key->payload.data[0];
76266763
DK
294
295 rc = evm_set_key(ekp->decrypted_data, ekp->decrypted_datalen);
296
66dbc325
MZ
297 /* burn the original key contents */
298 memset(ekp->decrypted_data, 0, ekp->decrypted_datalen);
299 up_read(&evm_key->sem);
300 key_put(evm_key);
301 return rc;
302}
This page took 0.292133 seconds and 5 git commands to generate.