blob: 3b9f5a080e4f69b937b2c782f67142541811fba9 [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 }
Dmitry Kasatkin88d7ed32011-12-05 13:17:41 +020055 if (type == EVM_XATTR_HMAC) {
56 rc = crypto_shash_setkey(*tfm, evmkey, evmkey_len);
57 if (rc) {
58 crypto_free_shash(*tfm);
59 *tfm = NULL;
60 return ERR_PTR(rc);
61 }
62 }
Mimi Zohar66dbc3252011-03-15 16:12:09 -040063 }
Dmitry Kasatkind46eb362011-03-09 15:07:36 -050064
Dmitry Kasatkin15647eb2011-09-01 14:41:40 +030065 desc = kmalloc(sizeof(*desc) + crypto_shash_descsize(*tfm),
Dmitry Kasatkind46eb362011-03-09 15:07:36 -050066 GFP_KERNEL);
67 if (!desc)
68 return ERR_PTR(-ENOMEM);
69
Dmitry Kasatkin15647eb2011-09-01 14:41:40 +030070 desc->tfm = *tfm;
Dmitry Kasatkind46eb362011-03-09 15:07:36 -050071 desc->flags = CRYPTO_TFM_REQ_MAY_SLEEP;
72
Dmitry Kasatkind46eb362011-03-09 15:07:36 -050073 rc = crypto_shash_init(desc);
Dmitry Kasatkind46eb362011-03-09 15:07:36 -050074 if (rc) {
75 kfree(desc);
76 return ERR_PTR(rc);
77 }
78 return desc;
Mimi Zohar66dbc3252011-03-15 16:12:09 -040079}
80
81/* Protect against 'cutting & pasting' security.evm xattr, include inode
82 * specific info.
83 *
84 * (Additional directory/file metadata needs to be added for more complete
85 * protection.)
86 */
Dmitry Kasatkind46eb362011-03-09 15:07:36 -050087static void hmac_add_misc(struct shash_desc *desc, struct inode *inode,
Mimi Zohar66dbc3252011-03-15 16:12:09 -040088 char *digest)
89{
90 struct h_misc {
91 unsigned long ino;
92 __u32 generation;
93 uid_t uid;
94 gid_t gid;
95 umode_t mode;
96 } hmac_misc;
Mimi Zohar66dbc3252011-03-15 16:12:09 -040097
98 memset(&hmac_misc, 0, sizeof hmac_misc);
99 hmac_misc.ino = inode->i_ino;
100 hmac_misc.generation = inode->i_generation;
101 hmac_misc.uid = inode->i_uid;
102 hmac_misc.gid = inode->i_gid;
103 hmac_misc.mode = inode->i_mode;
Dmitry Kasatkind46eb362011-03-09 15:07:36 -0500104 crypto_shash_update(desc, (const u8 *)&hmac_misc, sizeof hmac_misc);
105 crypto_shash_final(desc, digest);
Mimi Zohar66dbc3252011-03-15 16:12:09 -0400106}
107
108/*
109 * Calculate the HMAC value across the set of protected security xattrs.
110 *
111 * Instead of retrieving the requested xattr, for performance, calculate
112 * the hmac using the requested xattr value. Don't alloc/free memory for
113 * each xattr, but attempt to re-use the previously allocated memory.
114 */
Dmitry Kasatkin15647eb2011-09-01 14:41:40 +0300115static int evm_calc_hmac_or_hash(struct dentry *dentry,
116 const char *req_xattr_name,
117 const char *req_xattr_value,
118 size_t req_xattr_value_len,
119 char type, char *digest)
Mimi Zohar66dbc3252011-03-15 16:12:09 -0400120{
121 struct inode *inode = dentry->d_inode;
Dmitry Kasatkind46eb362011-03-09 15:07:36 -0500122 struct shash_desc *desc;
Mimi Zohar66dbc3252011-03-15 16:12:09 -0400123 char **xattrname;
124 size_t xattr_size = 0;
125 char *xattr_value = NULL;
126 int error;
127 int size;
128
129 if (!inode->i_op || !inode->i_op->getxattr)
130 return -EOPNOTSUPP;
Dmitry Kasatkin15647eb2011-09-01 14:41:40 +0300131 desc = init_desc(type);
Dmitry Kasatkind46eb362011-03-09 15:07:36 -0500132 if (IS_ERR(desc))
133 return PTR_ERR(desc);
Mimi Zohar66dbc3252011-03-15 16:12:09 -0400134
135 error = -ENODATA;
136 for (xattrname = evm_config_xattrnames; *xattrname != NULL; xattrname++) {
137 if ((req_xattr_name && req_xattr_value)
138 && !strcmp(*xattrname, req_xattr_name)) {
139 error = 0;
Dmitry Kasatkind46eb362011-03-09 15:07:36 -0500140 crypto_shash_update(desc, (const u8 *)req_xattr_value,
141 req_xattr_value_len);
Mimi Zohar66dbc3252011-03-15 16:12:09 -0400142 continue;
143 }
144 size = vfs_getxattr_alloc(dentry, *xattrname,
145 &xattr_value, xattr_size, GFP_NOFS);
146 if (size == -ENOMEM) {
147 error = -ENOMEM;
148 goto out;
149 }
150 if (size < 0)
151 continue;
152
153 error = 0;
154 xattr_size = size;
Dmitry Kasatkind46eb362011-03-09 15:07:36 -0500155 crypto_shash_update(desc, (const u8 *)xattr_value, xattr_size);
Mimi Zohar66dbc3252011-03-15 16:12:09 -0400156 }
Dmitry Kasatkind46eb362011-03-09 15:07:36 -0500157 hmac_add_misc(desc, inode, digest);
158
Mimi Zohar66dbc3252011-03-15 16:12:09 -0400159out:
Dmitry Kasatkind46eb362011-03-09 15:07:36 -0500160 kfree(xattr_value);
161 kfree(desc);
Mimi Zohar66dbc3252011-03-15 16:12:09 -0400162 return error;
163}
164
Dmitry Kasatkin15647eb2011-09-01 14:41:40 +0300165int evm_calc_hmac(struct dentry *dentry, const char *req_xattr_name,
166 const char *req_xattr_value, size_t req_xattr_value_len,
167 char *digest)
168{
169 return evm_calc_hmac_or_hash(dentry, req_xattr_name, req_xattr_value,
170 req_xattr_value_len, EVM_XATTR_HMAC, digest);
171}
172
173int evm_calc_hash(struct dentry *dentry, const char *req_xattr_name,
174 const char *req_xattr_value, size_t req_xattr_value_len,
175 char *digest)
176{
177 return evm_calc_hmac_or_hash(dentry, req_xattr_name, req_xattr_value,
178 req_xattr_value_len, IMA_XATTR_DIGEST, digest);
179}
180
Mimi Zohar66dbc3252011-03-15 16:12:09 -0400181/*
182 * Calculate the hmac and update security.evm xattr
183 *
184 * Expects to be called with i_mutex locked.
185 */
186int evm_update_evmxattr(struct dentry *dentry, const char *xattr_name,
187 const char *xattr_value, size_t xattr_value_len)
188{
189 struct inode *inode = dentry->d_inode;
Dmitry Kasatkin6be5cc52011-03-09 14:28:20 -0500190 struct evm_ima_xattr_data xattr_data;
Mimi Zohar66dbc3252011-03-15 16:12:09 -0400191 int rc = 0;
192
193 rc = evm_calc_hmac(dentry, xattr_name, xattr_value,
Dmitry Kasatkin6be5cc52011-03-09 14:28:20 -0500194 xattr_value_len, xattr_data.digest);
195 if (rc == 0) {
196 xattr_data.type = EVM_XATTR_HMAC;
Mimi Zohar66dbc3252011-03-15 16:12:09 -0400197 rc = __vfs_setxattr_noperm(dentry, XATTR_NAME_EVM,
Dmitry Kasatkin6be5cc52011-03-09 14:28:20 -0500198 &xattr_data,
199 sizeof(xattr_data), 0);
200 }
Mimi Zohar66dbc3252011-03-15 16:12:09 -0400201 else if (rc == -ENODATA)
202 rc = inode->i_op->removexattr(dentry, XATTR_NAME_EVM);
203 return rc;
204}
205
Mimi Zoharcb723182011-03-09 14:40:44 -0500206int evm_init_hmac(struct inode *inode, const struct xattr *lsm_xattr,
207 char *hmac_val)
208{
Dmitry Kasatkind46eb362011-03-09 15:07:36 -0500209 struct shash_desc *desc;
Mimi Zoharcb723182011-03-09 14:40:44 -0500210
Dmitry Kasatkin15647eb2011-09-01 14:41:40 +0300211 desc = init_desc(EVM_XATTR_HMAC);
Dmitry Kasatkind46eb362011-03-09 15:07:36 -0500212 if (IS_ERR(desc)) {
Mimi Zoharcb723182011-03-09 14:40:44 -0500213 printk(KERN_INFO "init_desc failed\n");
Dmitry Kasatkind46eb362011-03-09 15:07:36 -0500214 return PTR_ERR(desc);
Mimi Zoharcb723182011-03-09 14:40:44 -0500215 }
216
Dmitry Kasatkind46eb362011-03-09 15:07:36 -0500217 crypto_shash_update(desc, lsm_xattr->value, lsm_xattr->value_len);
218 hmac_add_misc(desc, inode, hmac_val);
219 kfree(desc);
Mimi Zoharcb723182011-03-09 14:40:44 -0500220 return 0;
221}
222
Mimi Zohar66dbc3252011-03-15 16:12:09 -0400223/*
224 * Get the key from the TPM for the SHA1-HMAC
225 */
226int evm_init_key(void)
227{
228 struct key *evm_key;
229 struct encrypted_key_payload *ekp;
230 int rc = 0;
231
232 evm_key = request_key(&key_type_encrypted, EVMKEY, NULL);
233 if (IS_ERR(evm_key))
234 return -ENOENT;
235
236 down_read(&evm_key->sem);
237 ekp = evm_key->payload.data;
238 if (ekp->decrypted_datalen > MAX_KEY_SIZE) {
239 rc = -EINVAL;
240 goto out;
241 }
242 memcpy(evmkey, ekp->decrypted_data, ekp->decrypted_datalen);
243out:
244 /* burn the original key contents */
245 memset(ekp->decrypted_data, 0, ekp->decrypted_datalen);
246 up_read(&evm_key->sem);
247 key_put(evm_key);
248 return rc;
249}