blob: 4ad657d8809730d8db975e699dc79696634f9d8a [file] [log] [blame]
Mimi Zohar66dbc322011-03-15 16:12:09 -04001/*
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>
Mimi Zohar66dbc322011-03-15 16:12:09 -040019#include <keys/encrypted-type.h>
Dmitry Kasatkind46eb362011-03-09 15:07:36 -050020#include <crypto/hash.h>
Mimi Zohar66dbc322011-03-15 16:12:09 -040021#include "evm.h"
22
23#define EVMKEY "evm-key"
24#define MAX_KEY_SIZE 128
25static unsigned char evmkey[MAX_KEY_SIZE];
26static int evmkey_len = MAX_KEY_SIZE;
27
Dmitry Kasatkind46eb362011-03-09 15:07:36 -050028struct crypto_shash *hmac_tfm;
29
30static struct shash_desc *init_desc(void)
Mimi Zohar66dbc322011-03-15 16:12:09 -040031{
32 int rc;
Dmitry Kasatkind46eb362011-03-09 15:07:36 -050033 struct shash_desc *desc;
Mimi Zohar66dbc322011-03-15 16:12:09 -040034
Dmitry Kasatkind46eb362011-03-09 15:07:36 -050035 if (hmac_tfm == NULL) {
36 hmac_tfm = crypto_alloc_shash(evm_hmac, 0, CRYPTO_ALG_ASYNC);
37 if (IS_ERR(hmac_tfm)) {
38 pr_err("Can not allocate %s (reason: %ld)\n",
39 evm_hmac, PTR_ERR(hmac_tfm));
40 rc = PTR_ERR(hmac_tfm);
41 hmac_tfm = NULL;
42 return ERR_PTR(rc);
43 }
Dmitry Kasatkind21b5942011-12-05 13:17:41 +020044 rc = crypto_shash_setkey(hmac_tfm, evmkey, evmkey_len);
45 if (rc) {
46 crypto_free_shash(hmac_tfm);
47 hmac_tfm = NULL;
48 return ERR_PTR(rc);
49 }
Mimi Zohar66dbc322011-03-15 16:12:09 -040050 }
Dmitry Kasatkind46eb362011-03-09 15:07:36 -050051
52 desc = kmalloc(sizeof(*desc) + crypto_shash_descsize(hmac_tfm),
53 GFP_KERNEL);
54 if (!desc)
55 return ERR_PTR(-ENOMEM);
56
57 desc->tfm = hmac_tfm;
58 desc->flags = CRYPTO_TFM_REQ_MAY_SLEEP;
59
Dmitry Kasatkind46eb362011-03-09 15:07:36 -050060 rc = crypto_shash_init(desc);
Dmitry Kasatkind46eb362011-03-09 15:07:36 -050061 if (rc) {
62 kfree(desc);
63 return ERR_PTR(rc);
64 }
65 return desc;
Mimi Zohar66dbc322011-03-15 16:12:09 -040066}
67
68/* Protect against 'cutting & pasting' security.evm xattr, include inode
69 * specific info.
70 *
71 * (Additional directory/file metadata needs to be added for more complete
72 * protection.)
73 */
Dmitry Kasatkind46eb362011-03-09 15:07:36 -050074static void hmac_add_misc(struct shash_desc *desc, struct inode *inode,
Mimi Zohar66dbc322011-03-15 16:12:09 -040075 char *digest)
76{
77 struct h_misc {
78 unsigned long ino;
79 __u32 generation;
80 uid_t uid;
81 gid_t gid;
82 umode_t mode;
83 } hmac_misc;
Mimi Zohar66dbc322011-03-15 16:12:09 -040084
85 memset(&hmac_misc, 0, sizeof hmac_misc);
86 hmac_misc.ino = inode->i_ino;
87 hmac_misc.generation = inode->i_generation;
88 hmac_misc.uid = inode->i_uid;
89 hmac_misc.gid = inode->i_gid;
90 hmac_misc.mode = inode->i_mode;
Dmitry Kasatkind46eb362011-03-09 15:07:36 -050091 crypto_shash_update(desc, (const u8 *)&hmac_misc, sizeof hmac_misc);
92 crypto_shash_final(desc, digest);
Mimi Zohar66dbc322011-03-15 16:12:09 -040093}
94
95/*
96 * Calculate the HMAC value across the set of protected security xattrs.
97 *
98 * Instead of retrieving the requested xattr, for performance, calculate
99 * the hmac using the requested xattr value. Don't alloc/free memory for
100 * each xattr, but attempt to re-use the previously allocated memory.
101 */
102int evm_calc_hmac(struct dentry *dentry, const char *req_xattr_name,
103 const char *req_xattr_value, size_t req_xattr_value_len,
104 char *digest)
105{
106 struct inode *inode = dentry->d_inode;
Dmitry Kasatkind46eb362011-03-09 15:07:36 -0500107 struct shash_desc *desc;
Mimi Zohar66dbc322011-03-15 16:12:09 -0400108 char **xattrname;
109 size_t xattr_size = 0;
110 char *xattr_value = NULL;
111 int error;
112 int size;
113
114 if (!inode->i_op || !inode->i_op->getxattr)
115 return -EOPNOTSUPP;
Dmitry Kasatkind46eb362011-03-09 15:07:36 -0500116 desc = init_desc();
117 if (IS_ERR(desc))
118 return PTR_ERR(desc);
Mimi Zohar66dbc322011-03-15 16:12:09 -0400119
120 error = -ENODATA;
121 for (xattrname = evm_config_xattrnames; *xattrname != NULL; xattrname++) {
122 if ((req_xattr_name && req_xattr_value)
123 && !strcmp(*xattrname, req_xattr_name)) {
124 error = 0;
Dmitry Kasatkind46eb362011-03-09 15:07:36 -0500125 crypto_shash_update(desc, (const u8 *)req_xattr_value,
126 req_xattr_value_len);
Mimi Zohar66dbc322011-03-15 16:12:09 -0400127 continue;
128 }
129 size = vfs_getxattr_alloc(dentry, *xattrname,
130 &xattr_value, xattr_size, GFP_NOFS);
131 if (size == -ENOMEM) {
132 error = -ENOMEM;
133 goto out;
134 }
135 if (size < 0)
136 continue;
137
138 error = 0;
139 xattr_size = size;
Dmitry Kasatkind46eb362011-03-09 15:07:36 -0500140 crypto_shash_update(desc, (const u8 *)xattr_value, xattr_size);
Mimi Zohar66dbc322011-03-15 16:12:09 -0400141 }
Dmitry Kasatkind46eb362011-03-09 15:07:36 -0500142 hmac_add_misc(desc, inode, digest);
143
Mimi Zohar66dbc322011-03-15 16:12:09 -0400144out:
Dmitry Kasatkind46eb362011-03-09 15:07:36 -0500145 kfree(xattr_value);
146 kfree(desc);
Mimi Zohar66dbc322011-03-15 16:12:09 -0400147 return error;
148}
149
150/*
151 * Calculate the hmac and update security.evm xattr
152 *
153 * Expects to be called with i_mutex locked.
154 */
155int evm_update_evmxattr(struct dentry *dentry, const char *xattr_name,
156 const char *xattr_value, size_t xattr_value_len)
157{
158 struct inode *inode = dentry->d_inode;
Dmitry Kasatkin6be5cc52011-03-09 14:28:20 -0500159 struct evm_ima_xattr_data xattr_data;
Mimi Zohar66dbc322011-03-15 16:12:09 -0400160 int rc = 0;
161
162 rc = evm_calc_hmac(dentry, xattr_name, xattr_value,
Dmitry Kasatkin6be5cc52011-03-09 14:28:20 -0500163 xattr_value_len, xattr_data.digest);
164 if (rc == 0) {
165 xattr_data.type = EVM_XATTR_HMAC;
Mimi Zohar66dbc322011-03-15 16:12:09 -0400166 rc = __vfs_setxattr_noperm(dentry, XATTR_NAME_EVM,
Dmitry Kasatkin6be5cc52011-03-09 14:28:20 -0500167 &xattr_data,
168 sizeof(xattr_data), 0);
169 }
Mimi Zohar66dbc322011-03-15 16:12:09 -0400170 else if (rc == -ENODATA)
171 rc = inode->i_op->removexattr(dentry, XATTR_NAME_EVM);
172 return rc;
173}
174
Mimi Zoharcb723182011-03-09 14:40:44 -0500175int evm_init_hmac(struct inode *inode, const struct xattr *lsm_xattr,
176 char *hmac_val)
177{
Dmitry Kasatkind46eb362011-03-09 15:07:36 -0500178 struct shash_desc *desc;
Mimi Zoharcb723182011-03-09 14:40:44 -0500179
Dmitry Kasatkind46eb362011-03-09 15:07:36 -0500180 desc = init_desc();
181 if (IS_ERR(desc)) {
Mimi Zoharcb723182011-03-09 14:40:44 -0500182 printk(KERN_INFO "init_desc failed\n");
Dmitry Kasatkind46eb362011-03-09 15:07:36 -0500183 return PTR_ERR(desc);
Mimi Zoharcb723182011-03-09 14:40:44 -0500184 }
185
Dmitry Kasatkind46eb362011-03-09 15:07:36 -0500186 crypto_shash_update(desc, lsm_xattr->value, lsm_xattr->value_len);
187 hmac_add_misc(desc, inode, hmac_val);
188 kfree(desc);
Mimi Zoharcb723182011-03-09 14:40:44 -0500189 return 0;
190}
191
Mimi Zohar66dbc322011-03-15 16:12:09 -0400192/*
193 * Get the key from the TPM for the SHA1-HMAC
194 */
195int evm_init_key(void)
196{
197 struct key *evm_key;
198 struct encrypted_key_payload *ekp;
199 int rc = 0;
200
201 evm_key = request_key(&key_type_encrypted, EVMKEY, NULL);
202 if (IS_ERR(evm_key))
203 return -ENOENT;
204
205 down_read(&evm_key->sem);
206 ekp = evm_key->payload.data;
207 if (ekp->decrypted_datalen > MAX_KEY_SIZE) {
208 rc = -EINVAL;
209 goto out;
210 }
211 memcpy(evmkey, ekp->decrypted_data, ekp->decrypted_datalen);
212out:
213 /* burn the original key contents */
214 memset(ekp->decrypted_data, 0, ekp->decrypted_datalen);
215 up_read(&evm_key->sem);
216 key_put(evm_key);
217 return rc;
218}