blob: 2c3591eca98937d205ae191439d803cc6c126006 [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
Joe Perches20ee4512014-02-24 13:59:56 -080016#define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
17
Mimi Zohar66dbc3252011-03-15 16:12:09 -040018#include <linux/module.h>
19#include <linux/crypto.h>
20#include <linux/xattr.h>
Mimi Zohar66dbc3252011-03-15 16:12:09 -040021#include <keys/encrypted-type.h>
Dmitry Kasatkind46eb362011-03-09 15:07:36 -050022#include <crypto/hash.h>
Mimi Zohar66dbc3252011-03-15 16:12:09 -040023#include "evm.h"
24
25#define EVMKEY "evm-key"
26#define MAX_KEY_SIZE 128
27static unsigned char evmkey[MAX_KEY_SIZE];
28static int evmkey_len = MAX_KEY_SIZE;
29
Dmitry Kasatkind46eb362011-03-09 15:07:36 -050030struct crypto_shash *hmac_tfm;
Dmitry Kasatkin15647eb2011-09-01 14:41:40 +030031struct crypto_shash *hash_tfm;
Dmitry Kasatkind46eb362011-03-09 15:07:36 -050032
Dmitry Kasatkin97426f92011-12-05 13:17:42 +020033static DEFINE_MUTEX(mutex);
34
James Morris8fcc9952012-01-09 12:16:48 +110035static struct shash_desc *init_desc(char type)
Mimi Zohar66dbc3252011-03-15 16:12:09 -040036{
Dmitry Kasatkin143b01d2011-12-05 13:17:42 +020037 long rc;
Dmitry Kasatkin15647eb2011-09-01 14:41:40 +030038 char *algo;
39 struct crypto_shash **tfm;
Dmitry Kasatkind46eb362011-03-09 15:07:36 -050040 struct shash_desc *desc;
Mimi Zohar66dbc3252011-03-15 16:12:09 -040041
Dmitry Kasatkin15647eb2011-09-01 14:41:40 +030042 if (type == EVM_XATTR_HMAC) {
Dmitry Kasatkin26ddabf2015-10-22 21:26:26 +030043 if (!(evm_initialized & EVM_INIT_HMAC)) {
44 pr_err("HMAC key is not set\n");
45 return ERR_PTR(-ENOKEY);
46 }
Dmitry Kasatkin15647eb2011-09-01 14:41:40 +030047 tfm = &hmac_tfm;
48 algo = evm_hmac;
49 } else {
50 tfm = &hash_tfm;
51 algo = evm_hash;
52 }
53
54 if (*tfm == NULL) {
Dmitry Kasatkin97426f92011-12-05 13:17:42 +020055 mutex_lock(&mutex);
Dmitry Kasatkin143b01d2011-12-05 13:17:42 +020056 if (*tfm)
Dmitry Kasatkin97426f92011-12-05 13:17:42 +020057 goto out;
Dmitry Kasatkin15647eb2011-09-01 14:41:40 +030058 *tfm = crypto_alloc_shash(algo, 0, CRYPTO_ALG_ASYNC);
59 if (IS_ERR(*tfm)) {
Dmitry Kasatkin15647eb2011-09-01 14:41:40 +030060 rc = PTR_ERR(*tfm);
Dmitry Kasatkin143b01d2011-12-05 13:17:42 +020061 pr_err("Can not allocate %s (reason: %ld)\n", algo, rc);
Dmitry Kasatkin15647eb2011-09-01 14:41:40 +030062 *tfm = NULL;
Dmitry Kasatkin97426f92011-12-05 13:17:42 +020063 mutex_unlock(&mutex);
Dmitry Kasatkind46eb362011-03-09 15:07:36 -050064 return ERR_PTR(rc);
65 }
Dmitry Kasatkin88d7ed32011-12-05 13:17:41 +020066 if (type == EVM_XATTR_HMAC) {
67 rc = crypto_shash_setkey(*tfm, evmkey, evmkey_len);
68 if (rc) {
69 crypto_free_shash(*tfm);
70 *tfm = NULL;
Dmitry Kasatkin143b01d2011-12-05 13:17:42 +020071 mutex_unlock(&mutex);
Dmitry Kasatkin88d7ed32011-12-05 13:17:41 +020072 return ERR_PTR(rc);
73 }
Dmitry Kasatkind21b5942011-12-05 13:17:41 +020074 }
Dmitry Kasatkin97426f92011-12-05 13:17:42 +020075out:
76 mutex_unlock(&mutex);
Mimi Zohar66dbc3252011-03-15 16:12:09 -040077 }
Dmitry Kasatkind46eb362011-03-09 15:07:36 -050078
Dmitry Kasatkin15647eb2011-09-01 14:41:40 +030079 desc = kmalloc(sizeof(*desc) + crypto_shash_descsize(*tfm),
Dmitry Kasatkind46eb362011-03-09 15:07:36 -050080 GFP_KERNEL);
81 if (!desc)
82 return ERR_PTR(-ENOMEM);
83
Dmitry Kasatkin15647eb2011-09-01 14:41:40 +030084 desc->tfm = *tfm;
Dmitry Kasatkind46eb362011-03-09 15:07:36 -050085 desc->flags = CRYPTO_TFM_REQ_MAY_SLEEP;
86
Dmitry Kasatkind46eb362011-03-09 15:07:36 -050087 rc = crypto_shash_init(desc);
Dmitry Kasatkind46eb362011-03-09 15:07:36 -050088 if (rc) {
89 kfree(desc);
90 return ERR_PTR(rc);
91 }
92 return desc;
Mimi Zohar66dbc3252011-03-15 16:12:09 -040093}
94
95/* Protect against 'cutting & pasting' security.evm xattr, include inode
96 * specific info.
97 *
98 * (Additional directory/file metadata needs to be added for more complete
99 * protection.)
100 */
Dmitry Kasatkind46eb362011-03-09 15:07:36 -0500101static void hmac_add_misc(struct shash_desc *desc, struct inode *inode,
Mimi Zohar66dbc3252011-03-15 16:12:09 -0400102 char *digest)
103{
104 struct h_misc {
105 unsigned long ino;
106 __u32 generation;
107 uid_t uid;
108 gid_t gid;
109 umode_t mode;
110 } hmac_misc;
Mimi Zohar66dbc3252011-03-15 16:12:09 -0400111
Dmitry Kasatkin2bb930a2014-03-04 18:04:20 +0200112 memset(&hmac_misc, 0, sizeof(hmac_misc));
Mimi Zohar66dbc3252011-03-15 16:12:09 -0400113 hmac_misc.ino = inode->i_ino;
114 hmac_misc.generation = inode->i_generation;
Eric W. Biedermancf9c9352012-05-25 18:22:35 -0600115 hmac_misc.uid = from_kuid(&init_user_ns, inode->i_uid);
116 hmac_misc.gid = from_kgid(&init_user_ns, inode->i_gid);
Mimi Zohar66dbc3252011-03-15 16:12:09 -0400117 hmac_misc.mode = inode->i_mode;
Dmitry Kasatkin2bb930a2014-03-04 18:04:20 +0200118 crypto_shash_update(desc, (const u8 *)&hmac_misc, sizeof(hmac_misc));
Dmitry Kasatkind3b33672014-03-28 14:31:04 +0200119 if (evm_hmac_attrs & EVM_ATTR_FSUUID)
Dmitry Kasatkin74de6682012-09-10 10:37:20 +0300120 crypto_shash_update(desc, inode->i_sb->s_uuid,
121 sizeof(inode->i_sb->s_uuid));
Dmitry Kasatkind46eb362011-03-09 15:07:36 -0500122 crypto_shash_final(desc, digest);
Mimi Zohar66dbc3252011-03-15 16:12:09 -0400123}
124
125/*
126 * Calculate the HMAC value across the set of protected security xattrs.
127 *
128 * Instead of retrieving the requested xattr, for performance, calculate
129 * the hmac using the requested xattr value. Don't alloc/free memory for
130 * each xattr, but attempt to re-use the previously allocated memory.
131 */
Dmitry Kasatkin15647eb2011-09-01 14:41:40 +0300132static int evm_calc_hmac_or_hash(struct dentry *dentry,
133 const char *req_xattr_name,
134 const char *req_xattr_value,
135 size_t req_xattr_value_len,
136 char type, char *digest)
Mimi Zohar66dbc3252011-03-15 16:12:09 -0400137{
David Howellsc6f493d2015-03-17 22:26:22 +0000138 struct inode *inode = d_backing_inode(dentry);
Dmitry Kasatkind46eb362011-03-09 15:07:36 -0500139 struct shash_desc *desc;
Mimi Zohar66dbc3252011-03-15 16:12:09 -0400140 char **xattrname;
141 size_t xattr_size = 0;
142 char *xattr_value = NULL;
143 int error;
144 int size;
145
Al Viro627bf812014-02-01 04:43:32 -0500146 if (!inode->i_op->getxattr)
Mimi Zohar66dbc3252011-03-15 16:12:09 -0400147 return -EOPNOTSUPP;
Dmitry Kasatkin15647eb2011-09-01 14:41:40 +0300148 desc = init_desc(type);
Dmitry Kasatkind46eb362011-03-09 15:07:36 -0500149 if (IS_ERR(desc))
150 return PTR_ERR(desc);
Mimi Zohar66dbc3252011-03-15 16:12:09 -0400151
152 error = -ENODATA;
153 for (xattrname = evm_config_xattrnames; *xattrname != NULL; xattrname++) {
154 if ((req_xattr_name && req_xattr_value)
155 && !strcmp(*xattrname, req_xattr_name)) {
156 error = 0;
Dmitry Kasatkind46eb362011-03-09 15:07:36 -0500157 crypto_shash_update(desc, (const u8 *)req_xattr_value,
158 req_xattr_value_len);
Mimi Zohar66dbc3252011-03-15 16:12:09 -0400159 continue;
160 }
161 size = vfs_getxattr_alloc(dentry, *xattrname,
162 &xattr_value, xattr_size, GFP_NOFS);
163 if (size == -ENOMEM) {
164 error = -ENOMEM;
165 goto out;
166 }
167 if (size < 0)
168 continue;
169
170 error = 0;
171 xattr_size = size;
Dmitry Kasatkind46eb362011-03-09 15:07:36 -0500172 crypto_shash_update(desc, (const u8 *)xattr_value, xattr_size);
Mimi Zohar66dbc3252011-03-15 16:12:09 -0400173 }
Dmitry Kasatkind46eb362011-03-09 15:07:36 -0500174 hmac_add_misc(desc, inode, digest);
175
Mimi Zohar66dbc3252011-03-15 16:12:09 -0400176out:
Dmitry Kasatkind46eb362011-03-09 15:07:36 -0500177 kfree(xattr_value);
178 kfree(desc);
Mimi Zohar66dbc3252011-03-15 16:12:09 -0400179 return error;
180}
181
Dmitry Kasatkin15647eb2011-09-01 14:41:40 +0300182int evm_calc_hmac(struct dentry *dentry, const char *req_xattr_name,
183 const char *req_xattr_value, size_t req_xattr_value_len,
184 char *digest)
185{
186 return evm_calc_hmac_or_hash(dentry, req_xattr_name, req_xattr_value,
187 req_xattr_value_len, EVM_XATTR_HMAC, digest);
188}
189
190int evm_calc_hash(struct dentry *dentry, const char *req_xattr_name,
191 const char *req_xattr_value, size_t req_xattr_value_len,
192 char *digest)
193{
194 return evm_calc_hmac_or_hash(dentry, req_xattr_name, req_xattr_value,
195 req_xattr_value_len, IMA_XATTR_DIGEST, digest);
196}
197
Mimi Zohar66dbc3252011-03-15 16:12:09 -0400198/*
199 * Calculate the hmac and update security.evm xattr
200 *
201 * Expects to be called with i_mutex locked.
202 */
203int evm_update_evmxattr(struct dentry *dentry, const char *xattr_name,
204 const char *xattr_value, size_t xattr_value_len)
205{
David Howellsc6f493d2015-03-17 22:26:22 +0000206 struct inode *inode = d_backing_inode(dentry);
Dmitry Kasatkin6be5cc52011-03-09 14:28:20 -0500207 struct evm_ima_xattr_data xattr_data;
Mimi Zohar66dbc3252011-03-15 16:12:09 -0400208 int rc = 0;
209
210 rc = evm_calc_hmac(dentry, xattr_name, xattr_value,
Dmitry Kasatkin6be5cc52011-03-09 14:28:20 -0500211 xattr_value_len, xattr_data.digest);
212 if (rc == 0) {
213 xattr_data.type = EVM_XATTR_HMAC;
Mimi Zohar66dbc3252011-03-15 16:12:09 -0400214 rc = __vfs_setxattr_noperm(dentry, XATTR_NAME_EVM,
Dmitry Kasatkin6be5cc52011-03-09 14:28:20 -0500215 &xattr_data,
216 sizeof(xattr_data), 0);
Dmitry Kasatkina67adb92013-01-18 23:56:39 +0200217 } else if (rc == -ENODATA && inode->i_op->removexattr) {
Mimi Zohar66dbc3252011-03-15 16:12:09 -0400218 rc = inode->i_op->removexattr(dentry, XATTR_NAME_EVM);
Dmitry Kasatkina67adb92013-01-18 23:56:39 +0200219 }
Mimi Zohar66dbc3252011-03-15 16:12:09 -0400220 return rc;
221}
222
Mimi Zoharcb7231802011-03-09 14:40:44 -0500223int evm_init_hmac(struct inode *inode, const struct xattr *lsm_xattr,
224 char *hmac_val)
225{
Dmitry Kasatkind46eb362011-03-09 15:07:36 -0500226 struct shash_desc *desc;
Mimi Zoharcb7231802011-03-09 14:40:44 -0500227
Dmitry Kasatkin15647eb2011-09-01 14:41:40 +0300228 desc = init_desc(EVM_XATTR_HMAC);
Dmitry Kasatkind46eb362011-03-09 15:07:36 -0500229 if (IS_ERR(desc)) {
Joe Perches20ee4512014-02-24 13:59:56 -0800230 pr_info("init_desc failed\n");
Dmitry Kasatkind46eb362011-03-09 15:07:36 -0500231 return PTR_ERR(desc);
Mimi Zoharcb7231802011-03-09 14:40:44 -0500232 }
233
Dmitry Kasatkind46eb362011-03-09 15:07:36 -0500234 crypto_shash_update(desc, lsm_xattr->value, lsm_xattr->value_len);
235 hmac_add_misc(desc, inode, hmac_val);
236 kfree(desc);
Mimi Zoharcb7231802011-03-09 14:40:44 -0500237 return 0;
238}
239
Mimi Zohar66dbc3252011-03-15 16:12:09 -0400240/*
241 * Get the key from the TPM for the SHA1-HMAC
242 */
243int evm_init_key(void)
244{
245 struct key *evm_key;
246 struct encrypted_key_payload *ekp;
247 int rc = 0;
248
249 evm_key = request_key(&key_type_encrypted, EVMKEY, NULL);
250 if (IS_ERR(evm_key))
251 return -ENOENT;
252
253 down_read(&evm_key->sem);
David Howells146aa8b2015-10-21 14:04:48 +0100254 ekp = evm_key->payload.data[0];
Mimi Zohar66dbc3252011-03-15 16:12:09 -0400255 if (ekp->decrypted_datalen > MAX_KEY_SIZE) {
256 rc = -EINVAL;
257 goto out;
258 }
259 memcpy(evmkey, ekp->decrypted_data, ekp->decrypted_datalen);
260out:
261 /* burn the original key contents */
262 memset(ekp->decrypted_data, 0, ekp->decrypted_datalen);
263 up_read(&evm_key->sem);
264 key_put(evm_key);
265 return rc;
266}