blob: 5e9687f02e1b14e56e56d3a303afbef23ea79dca [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) {
43 tfm = &hmac_tfm;
44 algo = evm_hmac;
45 } else {
46 tfm = &hash_tfm;
47 algo = evm_hash;
48 }
49
50 if (*tfm == NULL) {
Dmitry Kasatkin97426f92011-12-05 13:17:42 +020051 mutex_lock(&mutex);
Dmitry Kasatkin143b01d2011-12-05 13:17:42 +020052 if (*tfm)
Dmitry Kasatkin97426f92011-12-05 13:17:42 +020053 goto out;
Dmitry Kasatkin15647eb2011-09-01 14:41:40 +030054 *tfm = crypto_alloc_shash(algo, 0, CRYPTO_ALG_ASYNC);
55 if (IS_ERR(*tfm)) {
Dmitry Kasatkin15647eb2011-09-01 14:41:40 +030056 rc = PTR_ERR(*tfm);
Dmitry Kasatkin143b01d2011-12-05 13:17:42 +020057 pr_err("Can not allocate %s (reason: %ld)\n", algo, rc);
Dmitry Kasatkin15647eb2011-09-01 14:41:40 +030058 *tfm = NULL;
Dmitry Kasatkin97426f92011-12-05 13:17:42 +020059 mutex_unlock(&mutex);
Dmitry Kasatkind46eb362011-03-09 15:07:36 -050060 return ERR_PTR(rc);
61 }
Dmitry Kasatkin88d7ed32011-12-05 13:17:41 +020062 if (type == EVM_XATTR_HMAC) {
63 rc = crypto_shash_setkey(*tfm, evmkey, evmkey_len);
64 if (rc) {
65 crypto_free_shash(*tfm);
66 *tfm = NULL;
Dmitry Kasatkin143b01d2011-12-05 13:17:42 +020067 mutex_unlock(&mutex);
Dmitry Kasatkin88d7ed32011-12-05 13:17:41 +020068 return ERR_PTR(rc);
69 }
Dmitry Kasatkind21b5942011-12-05 13:17:41 +020070 }
Dmitry Kasatkin97426f92011-12-05 13:17:42 +020071out:
72 mutex_unlock(&mutex);
Mimi Zohar66dbc3252011-03-15 16:12:09 -040073 }
Dmitry Kasatkind46eb362011-03-09 15:07:36 -050074
Dmitry Kasatkin15647eb2011-09-01 14:41:40 +030075 desc = kmalloc(sizeof(*desc) + crypto_shash_descsize(*tfm),
Dmitry Kasatkind46eb362011-03-09 15:07:36 -050076 GFP_KERNEL);
77 if (!desc)
78 return ERR_PTR(-ENOMEM);
79
Dmitry Kasatkin15647eb2011-09-01 14:41:40 +030080 desc->tfm = *tfm;
Dmitry Kasatkind46eb362011-03-09 15:07:36 -050081 desc->flags = CRYPTO_TFM_REQ_MAY_SLEEP;
82
Dmitry Kasatkind46eb362011-03-09 15:07:36 -050083 rc = crypto_shash_init(desc);
Dmitry Kasatkind46eb362011-03-09 15:07:36 -050084 if (rc) {
85 kfree(desc);
86 return ERR_PTR(rc);
87 }
88 return desc;
Mimi Zohar66dbc3252011-03-15 16:12:09 -040089}
90
91/* Protect against 'cutting & pasting' security.evm xattr, include inode
92 * specific info.
93 *
94 * (Additional directory/file metadata needs to be added for more complete
95 * protection.)
96 */
Dmitry Kasatkind46eb362011-03-09 15:07:36 -050097static void hmac_add_misc(struct shash_desc *desc, struct inode *inode,
Mimi Zohar66dbc3252011-03-15 16:12:09 -040098 char *digest)
99{
100 struct h_misc {
101 unsigned long ino;
102 __u32 generation;
103 uid_t uid;
104 gid_t gid;
105 umode_t mode;
106 } hmac_misc;
Mimi Zohar66dbc3252011-03-15 16:12:09 -0400107
Dmitry Kasatkin2bb930a2014-03-04 18:04:20 +0200108 memset(&hmac_misc, 0, sizeof(hmac_misc));
Mimi Zohar66dbc3252011-03-15 16:12:09 -0400109 hmac_misc.ino = inode->i_ino;
110 hmac_misc.generation = inode->i_generation;
Eric W. Biedermancf9c9352012-05-25 18:22:35 -0600111 hmac_misc.uid = from_kuid(&init_user_ns, inode->i_uid);
112 hmac_misc.gid = from_kgid(&init_user_ns, inode->i_gid);
Mimi Zohar66dbc3252011-03-15 16:12:09 -0400113 hmac_misc.mode = inode->i_mode;
Dmitry Kasatkin2bb930a2014-03-04 18:04:20 +0200114 crypto_shash_update(desc, (const u8 *)&hmac_misc, sizeof(hmac_misc));
Dmitry Kasatkind3b33672014-03-28 14:31:04 +0200115 if (evm_hmac_attrs & EVM_ATTR_FSUUID)
Dmitry Kasatkin74de6682012-09-10 10:37:20 +0300116 crypto_shash_update(desc, inode->i_sb->s_uuid,
117 sizeof(inode->i_sb->s_uuid));
Dmitry Kasatkind46eb362011-03-09 15:07:36 -0500118 crypto_shash_final(desc, digest);
Mimi Zohar66dbc3252011-03-15 16:12:09 -0400119}
120
121/*
122 * Calculate the HMAC value across the set of protected security xattrs.
123 *
124 * Instead of retrieving the requested xattr, for performance, calculate
125 * the hmac using the requested xattr value. Don't alloc/free memory for
126 * each xattr, but attempt to re-use the previously allocated memory.
127 */
Dmitry Kasatkin15647eb2011-09-01 14:41:40 +0300128static int evm_calc_hmac_or_hash(struct dentry *dentry,
129 const char *req_xattr_name,
130 const char *req_xattr_value,
131 size_t req_xattr_value_len,
132 char type, char *digest)
Mimi Zohar66dbc3252011-03-15 16:12:09 -0400133{
134 struct inode *inode = dentry->d_inode;
Dmitry Kasatkind46eb362011-03-09 15:07:36 -0500135 struct shash_desc *desc;
Mimi Zohar66dbc3252011-03-15 16:12:09 -0400136 char **xattrname;
137 size_t xattr_size = 0;
138 char *xattr_value = NULL;
139 int error;
140 int size;
141
Al Viro627bf812014-02-01 04:43:32 -0500142 if (!inode->i_op->getxattr)
Mimi Zohar66dbc3252011-03-15 16:12:09 -0400143 return -EOPNOTSUPP;
Dmitry Kasatkin15647eb2011-09-01 14:41:40 +0300144 desc = init_desc(type);
Dmitry Kasatkind46eb362011-03-09 15:07:36 -0500145 if (IS_ERR(desc))
146 return PTR_ERR(desc);
Mimi Zohar66dbc3252011-03-15 16:12:09 -0400147
148 error = -ENODATA;
149 for (xattrname = evm_config_xattrnames; *xattrname != NULL; xattrname++) {
150 if ((req_xattr_name && req_xattr_value)
151 && !strcmp(*xattrname, req_xattr_name)) {
152 error = 0;
Dmitry Kasatkind46eb362011-03-09 15:07:36 -0500153 crypto_shash_update(desc, (const u8 *)req_xattr_value,
154 req_xattr_value_len);
Mimi Zohar66dbc3252011-03-15 16:12:09 -0400155 continue;
156 }
157 size = vfs_getxattr_alloc(dentry, *xattrname,
158 &xattr_value, xattr_size, GFP_NOFS);
159 if (size == -ENOMEM) {
160 error = -ENOMEM;
161 goto out;
162 }
163 if (size < 0)
164 continue;
165
166 error = 0;
167 xattr_size = size;
Dmitry Kasatkind46eb362011-03-09 15:07:36 -0500168 crypto_shash_update(desc, (const u8 *)xattr_value, xattr_size);
Mimi Zohar66dbc3252011-03-15 16:12:09 -0400169 }
Dmitry Kasatkind46eb362011-03-09 15:07:36 -0500170 hmac_add_misc(desc, inode, digest);
171
Mimi Zohar66dbc3252011-03-15 16:12:09 -0400172out:
Dmitry Kasatkind46eb362011-03-09 15:07:36 -0500173 kfree(xattr_value);
174 kfree(desc);
Mimi Zohar66dbc3252011-03-15 16:12:09 -0400175 return error;
176}
177
Dmitry Kasatkin15647eb2011-09-01 14:41:40 +0300178int evm_calc_hmac(struct dentry *dentry, const char *req_xattr_name,
179 const char *req_xattr_value, size_t req_xattr_value_len,
180 char *digest)
181{
182 return evm_calc_hmac_or_hash(dentry, req_xattr_name, req_xattr_value,
183 req_xattr_value_len, EVM_XATTR_HMAC, digest);
184}
185
186int evm_calc_hash(struct dentry *dentry, const char *req_xattr_name,
187 const char *req_xattr_value, size_t req_xattr_value_len,
188 char *digest)
189{
190 return evm_calc_hmac_or_hash(dentry, req_xattr_name, req_xattr_value,
191 req_xattr_value_len, IMA_XATTR_DIGEST, digest);
192}
193
Mimi Zohar66dbc3252011-03-15 16:12:09 -0400194/*
195 * Calculate the hmac and update security.evm xattr
196 *
197 * Expects to be called with i_mutex locked.
198 */
199int evm_update_evmxattr(struct dentry *dentry, const char *xattr_name,
200 const char *xattr_value, size_t xattr_value_len)
201{
202 struct inode *inode = dentry->d_inode;
Dmitry Kasatkin6be5cc52011-03-09 14:28:20 -0500203 struct evm_ima_xattr_data xattr_data;
Mimi Zohar66dbc3252011-03-15 16:12:09 -0400204 int rc = 0;
205
206 rc = evm_calc_hmac(dentry, xattr_name, xattr_value,
Dmitry Kasatkin6be5cc52011-03-09 14:28:20 -0500207 xattr_value_len, xattr_data.digest);
208 if (rc == 0) {
209 xattr_data.type = EVM_XATTR_HMAC;
Mimi Zohar66dbc3252011-03-15 16:12:09 -0400210 rc = __vfs_setxattr_noperm(dentry, XATTR_NAME_EVM,
Dmitry Kasatkin6be5cc52011-03-09 14:28:20 -0500211 &xattr_data,
212 sizeof(xattr_data), 0);
Dmitry Kasatkina67adb92013-01-18 23:56:39 +0200213 } else if (rc == -ENODATA && inode->i_op->removexattr) {
Mimi Zohar66dbc3252011-03-15 16:12:09 -0400214 rc = inode->i_op->removexattr(dentry, XATTR_NAME_EVM);
Dmitry Kasatkina67adb92013-01-18 23:56:39 +0200215 }
Mimi Zohar66dbc3252011-03-15 16:12:09 -0400216 return rc;
217}
218
Mimi Zoharcb723182011-03-09 14:40:44 -0500219int evm_init_hmac(struct inode *inode, const struct xattr *lsm_xattr,
220 char *hmac_val)
221{
Dmitry Kasatkind46eb362011-03-09 15:07:36 -0500222 struct shash_desc *desc;
Mimi Zoharcb723182011-03-09 14:40:44 -0500223
Dmitry Kasatkin15647eb2011-09-01 14:41:40 +0300224 desc = init_desc(EVM_XATTR_HMAC);
Dmitry Kasatkind46eb362011-03-09 15:07:36 -0500225 if (IS_ERR(desc)) {
Joe Perches20ee4512014-02-24 13:59:56 -0800226 pr_info("init_desc failed\n");
Dmitry Kasatkind46eb362011-03-09 15:07:36 -0500227 return PTR_ERR(desc);
Mimi Zoharcb723182011-03-09 14:40:44 -0500228 }
229
Dmitry Kasatkind46eb362011-03-09 15:07:36 -0500230 crypto_shash_update(desc, lsm_xattr->value, lsm_xattr->value_len);
231 hmac_add_misc(desc, inode, hmac_val);
232 kfree(desc);
Mimi Zoharcb723182011-03-09 14:40:44 -0500233 return 0;
234}
235
Mimi Zohar66dbc3252011-03-15 16:12:09 -0400236/*
237 * Get the key from the TPM for the SHA1-HMAC
238 */
239int evm_init_key(void)
240{
241 struct key *evm_key;
242 struct encrypted_key_payload *ekp;
243 int rc = 0;
244
245 evm_key = request_key(&key_type_encrypted, EVMKEY, NULL);
246 if (IS_ERR(evm_key))
247 return -ENOENT;
248
249 down_read(&evm_key->sem);
250 ekp = evm_key->payload.data;
251 if (ekp->decrypted_datalen > MAX_KEY_SIZE) {
252 rc = -EINVAL;
253 goto out;
254 }
255 memcpy(evmkey, ekp->decrypted_data, ekp->decrypted_datalen);
256out:
257 /* burn the original key contents */
258 memset(ekp->decrypted_data, 0, ekp->decrypted_datalen);
259 up_read(&evm_key->sem);
260 key_put(evm_key);
261 return rc;
262}