Mimi Zohar | 66dbc325 | 2011-03-15 16:12:09 -0400 | [diff] [blame] | 1 | /* |
| 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 Zohar | 66dbc325 | 2011-03-15 16:12:09 -0400 | [diff] [blame] | 19 | #include <keys/encrypted-type.h> |
Dmitry Kasatkin | d46eb36 | 2011-03-09 15:07:36 -0500 | [diff] [blame] | 20 | #include <crypto/hash.h> |
Mimi Zohar | 66dbc325 | 2011-03-15 16:12:09 -0400 | [diff] [blame] | 21 | #include "evm.h" |
| 22 | |
| 23 | #define EVMKEY "evm-key" |
| 24 | #define MAX_KEY_SIZE 128 |
| 25 | static unsigned char evmkey[MAX_KEY_SIZE]; |
| 26 | static int evmkey_len = MAX_KEY_SIZE; |
| 27 | |
Dmitry Kasatkin | d46eb36 | 2011-03-09 15:07:36 -0500 | [diff] [blame] | 28 | struct crypto_shash *hmac_tfm; |
Dmitry Kasatkin | 15647eb | 2011-09-01 14:41:40 +0300 | [diff] [blame^] | 29 | struct crypto_shash *hash_tfm; |
Dmitry Kasatkin | d46eb36 | 2011-03-09 15:07:36 -0500 | [diff] [blame] | 30 | |
Dmitry Kasatkin | 15647eb | 2011-09-01 14:41:40 +0300 | [diff] [blame^] | 31 | static struct shash_desc *init_desc(const char type) |
Mimi Zohar | 66dbc325 | 2011-03-15 16:12:09 -0400 | [diff] [blame] | 32 | { |
| 33 | int rc; |
Dmitry Kasatkin | 15647eb | 2011-09-01 14:41:40 +0300 | [diff] [blame^] | 34 | char *algo; |
| 35 | struct crypto_shash **tfm; |
Dmitry Kasatkin | d46eb36 | 2011-03-09 15:07:36 -0500 | [diff] [blame] | 36 | struct shash_desc *desc; |
Mimi Zohar | 66dbc325 | 2011-03-15 16:12:09 -0400 | [diff] [blame] | 37 | |
Dmitry Kasatkin | 15647eb | 2011-09-01 14:41:40 +0300 | [diff] [blame^] | 38 | 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 Kasatkin | d46eb36 | 2011-03-09 15:07:36 -0500 | [diff] [blame] | 49 | pr_err("Can not allocate %s (reason: %ld)\n", |
Dmitry Kasatkin | 15647eb | 2011-09-01 14:41:40 +0300 | [diff] [blame^] | 50 | algo, PTR_ERR(*tfm)); |
| 51 | rc = PTR_ERR(*tfm); |
| 52 | *tfm = NULL; |
Dmitry Kasatkin | d46eb36 | 2011-03-09 15:07:36 -0500 | [diff] [blame] | 53 | return ERR_PTR(rc); |
| 54 | } |
Mimi Zohar | 66dbc325 | 2011-03-15 16:12:09 -0400 | [diff] [blame] | 55 | } |
Dmitry Kasatkin | d46eb36 | 2011-03-09 15:07:36 -0500 | [diff] [blame] | 56 | |
Dmitry Kasatkin | 15647eb | 2011-09-01 14:41:40 +0300 | [diff] [blame^] | 57 | desc = kmalloc(sizeof(*desc) + crypto_shash_descsize(*tfm), |
Dmitry Kasatkin | d46eb36 | 2011-03-09 15:07:36 -0500 | [diff] [blame] | 58 | GFP_KERNEL); |
| 59 | if (!desc) |
| 60 | return ERR_PTR(-ENOMEM); |
| 61 | |
Dmitry Kasatkin | 15647eb | 2011-09-01 14:41:40 +0300 | [diff] [blame^] | 62 | desc->tfm = *tfm; |
Dmitry Kasatkin | d46eb36 | 2011-03-09 15:07:36 -0500 | [diff] [blame] | 63 | desc->flags = CRYPTO_TFM_REQ_MAY_SLEEP; |
| 64 | |
Dmitry Kasatkin | 15647eb | 2011-09-01 14:41:40 +0300 | [diff] [blame^] | 65 | if (type == EVM_XATTR_HMAC) { |
| 66 | rc = crypto_shash_setkey(*tfm, evmkey, evmkey_len); |
| 67 | if (rc) |
| 68 | goto out; |
| 69 | } |
| 70 | |
Dmitry Kasatkin | d46eb36 | 2011-03-09 15:07:36 -0500 | [diff] [blame] | 71 | rc = crypto_shash_init(desc); |
Mimi Zohar | 66dbc325 | 2011-03-15 16:12:09 -0400 | [diff] [blame] | 72 | out: |
Dmitry Kasatkin | d46eb36 | 2011-03-09 15:07:36 -0500 | [diff] [blame] | 73 | if (rc) { |
| 74 | kfree(desc); |
| 75 | return ERR_PTR(rc); |
| 76 | } |
| 77 | return desc; |
Mimi Zohar | 66dbc325 | 2011-03-15 16:12:09 -0400 | [diff] [blame] | 78 | } |
| 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 Kasatkin | d46eb36 | 2011-03-09 15:07:36 -0500 | [diff] [blame] | 86 | static void hmac_add_misc(struct shash_desc *desc, struct inode *inode, |
Mimi Zohar | 66dbc325 | 2011-03-15 16:12:09 -0400 | [diff] [blame] | 87 | 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 Zohar | 66dbc325 | 2011-03-15 16:12:09 -0400 | [diff] [blame] | 96 | |
| 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 Kasatkin | d46eb36 | 2011-03-09 15:07:36 -0500 | [diff] [blame] | 103 | crypto_shash_update(desc, (const u8 *)&hmac_misc, sizeof hmac_misc); |
| 104 | crypto_shash_final(desc, digest); |
Mimi Zohar | 66dbc325 | 2011-03-15 16:12:09 -0400 | [diff] [blame] | 105 | } |
| 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 Kasatkin | 15647eb | 2011-09-01 14:41:40 +0300 | [diff] [blame^] | 114 | static 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 Zohar | 66dbc325 | 2011-03-15 16:12:09 -0400 | [diff] [blame] | 119 | { |
| 120 | struct inode *inode = dentry->d_inode; |
Dmitry Kasatkin | d46eb36 | 2011-03-09 15:07:36 -0500 | [diff] [blame] | 121 | struct shash_desc *desc; |
Mimi Zohar | 66dbc325 | 2011-03-15 16:12:09 -0400 | [diff] [blame] | 122 | 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 Kasatkin | 15647eb | 2011-09-01 14:41:40 +0300 | [diff] [blame^] | 130 | desc = init_desc(type); |
Dmitry Kasatkin | d46eb36 | 2011-03-09 15:07:36 -0500 | [diff] [blame] | 131 | if (IS_ERR(desc)) |
| 132 | return PTR_ERR(desc); |
Mimi Zohar | 66dbc325 | 2011-03-15 16:12:09 -0400 | [diff] [blame] | 133 | |
| 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 Kasatkin | d46eb36 | 2011-03-09 15:07:36 -0500 | [diff] [blame] | 139 | crypto_shash_update(desc, (const u8 *)req_xattr_value, |
| 140 | req_xattr_value_len); |
Mimi Zohar | 66dbc325 | 2011-03-15 16:12:09 -0400 | [diff] [blame] | 141 | 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 Kasatkin | d46eb36 | 2011-03-09 15:07:36 -0500 | [diff] [blame] | 154 | crypto_shash_update(desc, (const u8 *)xattr_value, xattr_size); |
Mimi Zohar | 66dbc325 | 2011-03-15 16:12:09 -0400 | [diff] [blame] | 155 | } |
Dmitry Kasatkin | d46eb36 | 2011-03-09 15:07:36 -0500 | [diff] [blame] | 156 | hmac_add_misc(desc, inode, digest); |
| 157 | |
Mimi Zohar | 66dbc325 | 2011-03-15 16:12:09 -0400 | [diff] [blame] | 158 | out: |
Dmitry Kasatkin | d46eb36 | 2011-03-09 15:07:36 -0500 | [diff] [blame] | 159 | kfree(xattr_value); |
| 160 | kfree(desc); |
Mimi Zohar | 66dbc325 | 2011-03-15 16:12:09 -0400 | [diff] [blame] | 161 | return error; |
| 162 | } |
| 163 | |
Dmitry Kasatkin | 15647eb | 2011-09-01 14:41:40 +0300 | [diff] [blame^] | 164 | int 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 | |
| 172 | int 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 Zohar | 66dbc325 | 2011-03-15 16:12:09 -0400 | [diff] [blame] | 180 | /* |
| 181 | * Calculate the hmac and update security.evm xattr |
| 182 | * |
| 183 | * Expects to be called with i_mutex locked. |
| 184 | */ |
| 185 | int 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 Kasatkin | 6be5cc5 | 2011-03-09 14:28:20 -0500 | [diff] [blame] | 189 | struct evm_ima_xattr_data xattr_data; |
Mimi Zohar | 66dbc325 | 2011-03-15 16:12:09 -0400 | [diff] [blame] | 190 | int rc = 0; |
| 191 | |
| 192 | rc = evm_calc_hmac(dentry, xattr_name, xattr_value, |
Dmitry Kasatkin | 6be5cc5 | 2011-03-09 14:28:20 -0500 | [diff] [blame] | 193 | xattr_value_len, xattr_data.digest); |
| 194 | if (rc == 0) { |
| 195 | xattr_data.type = EVM_XATTR_HMAC; |
Mimi Zohar | 66dbc325 | 2011-03-15 16:12:09 -0400 | [diff] [blame] | 196 | rc = __vfs_setxattr_noperm(dentry, XATTR_NAME_EVM, |
Dmitry Kasatkin | 6be5cc5 | 2011-03-09 14:28:20 -0500 | [diff] [blame] | 197 | &xattr_data, |
| 198 | sizeof(xattr_data), 0); |
| 199 | } |
Mimi Zohar | 66dbc325 | 2011-03-15 16:12:09 -0400 | [diff] [blame] | 200 | else if (rc == -ENODATA) |
| 201 | rc = inode->i_op->removexattr(dentry, XATTR_NAME_EVM); |
| 202 | return rc; |
| 203 | } |
| 204 | |
Mimi Zohar | cb723180 | 2011-03-09 14:40:44 -0500 | [diff] [blame] | 205 | int evm_init_hmac(struct inode *inode, const struct xattr *lsm_xattr, |
| 206 | char *hmac_val) |
| 207 | { |
Dmitry Kasatkin | d46eb36 | 2011-03-09 15:07:36 -0500 | [diff] [blame] | 208 | struct shash_desc *desc; |
Mimi Zohar | cb723180 | 2011-03-09 14:40:44 -0500 | [diff] [blame] | 209 | |
Dmitry Kasatkin | 15647eb | 2011-09-01 14:41:40 +0300 | [diff] [blame^] | 210 | desc = init_desc(EVM_XATTR_HMAC); |
Dmitry Kasatkin | d46eb36 | 2011-03-09 15:07:36 -0500 | [diff] [blame] | 211 | if (IS_ERR(desc)) { |
Mimi Zohar | cb723180 | 2011-03-09 14:40:44 -0500 | [diff] [blame] | 212 | printk(KERN_INFO "init_desc failed\n"); |
Dmitry Kasatkin | d46eb36 | 2011-03-09 15:07:36 -0500 | [diff] [blame] | 213 | return PTR_ERR(desc); |
Mimi Zohar | cb723180 | 2011-03-09 14:40:44 -0500 | [diff] [blame] | 214 | } |
| 215 | |
Dmitry Kasatkin | d46eb36 | 2011-03-09 15:07:36 -0500 | [diff] [blame] | 216 | crypto_shash_update(desc, lsm_xattr->value, lsm_xattr->value_len); |
| 217 | hmac_add_misc(desc, inode, hmac_val); |
| 218 | kfree(desc); |
Mimi Zohar | cb723180 | 2011-03-09 14:40:44 -0500 | [diff] [blame] | 219 | return 0; |
| 220 | } |
| 221 | |
Mimi Zohar | 66dbc325 | 2011-03-15 16:12:09 -0400 | [diff] [blame] | 222 | /* |
| 223 | * Get the key from the TPM for the SHA1-HMAC |
| 224 | */ |
| 225 | int 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); |
| 242 | out: |
| 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 | } |