blob: 847a2d7dff17e62711bf9efd32f5e3cae834ea8a [file] [log] [blame]
Mimi Zohar66dbc3252011-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 Zohar66dbc3252011-03-15 16:12:09 -040019#include <keys/encrypted-type.h>
Dmitry Kasatkind46eb362011-03-09 15:07:36 -050020#include <crypto/hash.h>
Mimi Zohar66dbc3252011-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;
Dmitry Kasatkin15647eb2011-09-01 14:41:40 +030029struct crypto_shash *hash_tfm;
Dmitry Kasatkind46eb362011-03-09 15:07:36 -050030
Dmitry Kasatkin15647eb2011-09-01 14:41:40 +030031static struct shash_desc *init_desc(const char type)
Mimi Zohar66dbc3252011-03-15 16:12:09 -040032{
33 int rc;
Dmitry Kasatkin15647eb2011-09-01 14:41:40 +030034 char *algo;
35 struct crypto_shash **tfm;
Dmitry Kasatkind46eb362011-03-09 15:07:36 -050036 struct shash_desc *desc;
Mimi Zohar66dbc3252011-03-15 16:12:09 -040037
Dmitry Kasatkin15647eb2011-09-01 14:41:40 +030038 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)) {
Dmitry Kasatkind46eb362011-03-09 15:07:36 -050049 pr_err("Can not allocate %s (reason: %ld)\n",
Dmitry Kasatkin15647eb2011-09-01 14:41:40 +030050 algo, PTR_ERR(*tfm));
51 rc = PTR_ERR(*tfm);
52 *tfm = NULL;
Dmitry Kasatkind46eb362011-03-09 15:07:36 -050053 return ERR_PTR(rc);
54 }
Mimi Zohar66dbc3252011-03-15 16:12:09 -040055 }
Dmitry Kasatkind46eb362011-03-09 15:07:36 -050056
Dmitry Kasatkin15647eb2011-09-01 14:41:40 +030057 desc = kmalloc(sizeof(*desc) + crypto_shash_descsize(*tfm),
Dmitry Kasatkind46eb362011-03-09 15:07:36 -050058 GFP_KERNEL);
59 if (!desc)
60 return ERR_PTR(-ENOMEM);
61
Dmitry Kasatkin15647eb2011-09-01 14:41:40 +030062 desc->tfm = *tfm;
Dmitry Kasatkind46eb362011-03-09 15:07:36 -050063 desc->flags = CRYPTO_TFM_REQ_MAY_SLEEP;
64
Dmitry Kasatkin15647eb2011-09-01 14:41:40 +030065 if (type == EVM_XATTR_HMAC) {
66 rc = crypto_shash_setkey(*tfm, evmkey, evmkey_len);
67 if (rc)
68 goto out;
69 }
70
Dmitry Kasatkind46eb362011-03-09 15:07:36 -050071 rc = crypto_shash_init(desc);
Mimi Zohar66dbc3252011-03-15 16:12:09 -040072out:
Dmitry Kasatkind46eb362011-03-09 15:07:36 -050073 if (rc) {
74 kfree(desc);
75 return ERR_PTR(rc);
76 }
77 return desc;
Mimi Zohar66dbc3252011-03-15 16:12:09 -040078}
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 */
Dmitry Kasatkind46eb362011-03-09 15:07:36 -050086static void hmac_add_misc(struct shash_desc *desc, struct inode *inode,
Mimi Zohar66dbc3252011-03-15 16:12:09 -040087 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;
Mimi Zohar66dbc3252011-03-15 16:12:09 -040096
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;
Dmitry Kasatkind46eb362011-03-09 15:07:36 -0500103 crypto_shash_update(desc, (const u8 *)&hmac_misc, sizeof hmac_misc);
104 crypto_shash_final(desc, digest);
Mimi Zohar66dbc3252011-03-15 16:12:09 -0400105}
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 */
Dmitry Kasatkin15647eb2011-09-01 14:41:40 +0300114static 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)
Mimi Zohar66dbc3252011-03-15 16:12:09 -0400119{
120 struct inode *inode = dentry->d_inode;
Dmitry Kasatkind46eb362011-03-09 15:07:36 -0500121 struct shash_desc *desc;
Mimi Zohar66dbc3252011-03-15 16:12:09 -0400122 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;
Dmitry Kasatkin15647eb2011-09-01 14:41:40 +0300130 desc = init_desc(type);
Dmitry Kasatkind46eb362011-03-09 15:07:36 -0500131 if (IS_ERR(desc))
132 return PTR_ERR(desc);
Mimi Zohar66dbc3252011-03-15 16:12:09 -0400133
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;
Dmitry Kasatkind46eb362011-03-09 15:07:36 -0500139 crypto_shash_update(desc, (const u8 *)req_xattr_value,
140 req_xattr_value_len);
Mimi Zohar66dbc3252011-03-15 16:12:09 -0400141 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;
Dmitry Kasatkind46eb362011-03-09 15:07:36 -0500154 crypto_shash_update(desc, (const u8 *)xattr_value, xattr_size);
Mimi Zohar66dbc3252011-03-15 16:12:09 -0400155 }
Dmitry Kasatkind46eb362011-03-09 15:07:36 -0500156 hmac_add_misc(desc, inode, digest);
157
Mimi Zohar66dbc3252011-03-15 16:12:09 -0400158out:
Dmitry Kasatkind46eb362011-03-09 15:07:36 -0500159 kfree(xattr_value);
160 kfree(desc);
Mimi Zohar66dbc3252011-03-15 16:12:09 -0400161 return error;
162}
163
Dmitry Kasatkin15647eb2011-09-01 14:41:40 +0300164int 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
172int 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
Mimi Zohar66dbc3252011-03-15 16:12:09 -0400180/*
181 * Calculate the hmac and update security.evm xattr
182 *
183 * Expects to be called with i_mutex locked.
184 */
185int 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;
Dmitry Kasatkin6be5cc52011-03-09 14:28:20 -0500189 struct evm_ima_xattr_data xattr_data;
Mimi Zohar66dbc3252011-03-15 16:12:09 -0400190 int rc = 0;
191
192 rc = evm_calc_hmac(dentry, xattr_name, xattr_value,
Dmitry Kasatkin6be5cc52011-03-09 14:28:20 -0500193 xattr_value_len, xattr_data.digest);
194 if (rc == 0) {
195 xattr_data.type = EVM_XATTR_HMAC;
Mimi Zohar66dbc3252011-03-15 16:12:09 -0400196 rc = __vfs_setxattr_noperm(dentry, XATTR_NAME_EVM,
Dmitry Kasatkin6be5cc52011-03-09 14:28:20 -0500197 &xattr_data,
198 sizeof(xattr_data), 0);
199 }
Mimi Zohar66dbc3252011-03-15 16:12:09 -0400200 else if (rc == -ENODATA)
201 rc = inode->i_op->removexattr(dentry, XATTR_NAME_EVM);
202 return rc;
203}
204
Mimi Zoharcb7231802011-03-09 14:40:44 -0500205int evm_init_hmac(struct inode *inode, const struct xattr *lsm_xattr,
206 char *hmac_val)
207{
Dmitry Kasatkind46eb362011-03-09 15:07:36 -0500208 struct shash_desc *desc;
Mimi Zoharcb7231802011-03-09 14:40:44 -0500209
Dmitry Kasatkin15647eb2011-09-01 14:41:40 +0300210 desc = init_desc(EVM_XATTR_HMAC);
Dmitry Kasatkind46eb362011-03-09 15:07:36 -0500211 if (IS_ERR(desc)) {
Mimi Zoharcb7231802011-03-09 14:40:44 -0500212 printk(KERN_INFO "init_desc failed\n");
Dmitry Kasatkind46eb362011-03-09 15:07:36 -0500213 return PTR_ERR(desc);
Mimi Zoharcb7231802011-03-09 14:40:44 -0500214 }
215
Dmitry Kasatkind46eb362011-03-09 15:07:36 -0500216 crypto_shash_update(desc, lsm_xattr->value, lsm_xattr->value_len);
217 hmac_add_misc(desc, inode, hmac_val);
218 kfree(desc);
Mimi Zoharcb7231802011-03-09 14:40:44 -0500219 return 0;
220}
221
Mimi Zohar66dbc3252011-03-15 16:12:09 -0400222/*
223 * Get the key from the TPM for the SHA1-HMAC
224 */
225int 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);
242out:
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}