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 | 97426f9 | 2011-12-05 13:17:42 +0200 | [diff] [blame] | 31 | static DEFINE_MUTEX(mutex); |
| 32 | |
James Morris | 8fcc995 | 2012-01-09 12:16:48 +1100 | [diff] [blame] | 33 | static struct shash_desc *init_desc(char type) |
Mimi Zohar | 66dbc325 | 2011-03-15 16:12:09 -0400 | [diff] [blame] | 34 | { |
Dmitry Kasatkin | 143b01d | 2011-12-05 13:17:42 +0200 | [diff] [blame] | 35 | long rc; |
Dmitry Kasatkin | 15647eb | 2011-09-01 14:41:40 +0300 | [diff] [blame] | 36 | char *algo; |
| 37 | struct crypto_shash **tfm; |
Dmitry Kasatkin | d46eb36 | 2011-03-09 15:07:36 -0500 | [diff] [blame] | 38 | struct shash_desc *desc; |
Mimi Zohar | 66dbc325 | 2011-03-15 16:12:09 -0400 | [diff] [blame] | 39 | |
Dmitry Kasatkin | 15647eb | 2011-09-01 14:41:40 +0300 | [diff] [blame] | 40 | if (type == EVM_XATTR_HMAC) { |
| 41 | tfm = &hmac_tfm; |
| 42 | algo = evm_hmac; |
| 43 | } else { |
| 44 | tfm = &hash_tfm; |
| 45 | algo = evm_hash; |
| 46 | } |
| 47 | |
| 48 | if (*tfm == NULL) { |
Dmitry Kasatkin | 97426f9 | 2011-12-05 13:17:42 +0200 | [diff] [blame] | 49 | mutex_lock(&mutex); |
Dmitry Kasatkin | 143b01d | 2011-12-05 13:17:42 +0200 | [diff] [blame] | 50 | if (*tfm) |
Dmitry Kasatkin | 97426f9 | 2011-12-05 13:17:42 +0200 | [diff] [blame] | 51 | goto out; |
Dmitry Kasatkin | 15647eb | 2011-09-01 14:41:40 +0300 | [diff] [blame] | 52 | *tfm = crypto_alloc_shash(algo, 0, CRYPTO_ALG_ASYNC); |
| 53 | if (IS_ERR(*tfm)) { |
Dmitry Kasatkin | 15647eb | 2011-09-01 14:41:40 +0300 | [diff] [blame] | 54 | rc = PTR_ERR(*tfm); |
Dmitry Kasatkin | 143b01d | 2011-12-05 13:17:42 +0200 | [diff] [blame] | 55 | pr_err("Can not allocate %s (reason: %ld)\n", algo, rc); |
Dmitry Kasatkin | 15647eb | 2011-09-01 14:41:40 +0300 | [diff] [blame] | 56 | *tfm = NULL; |
Dmitry Kasatkin | 97426f9 | 2011-12-05 13:17:42 +0200 | [diff] [blame] | 57 | mutex_unlock(&mutex); |
Dmitry Kasatkin | d46eb36 | 2011-03-09 15:07:36 -0500 | [diff] [blame] | 58 | return ERR_PTR(rc); |
| 59 | } |
Dmitry Kasatkin | 88d7ed3 | 2011-12-05 13:17:41 +0200 | [diff] [blame] | 60 | if (type == EVM_XATTR_HMAC) { |
| 61 | rc = crypto_shash_setkey(*tfm, evmkey, evmkey_len); |
| 62 | if (rc) { |
| 63 | crypto_free_shash(*tfm); |
| 64 | *tfm = NULL; |
Dmitry Kasatkin | 143b01d | 2011-12-05 13:17:42 +0200 | [diff] [blame] | 65 | mutex_unlock(&mutex); |
Dmitry Kasatkin | 88d7ed3 | 2011-12-05 13:17:41 +0200 | [diff] [blame] | 66 | return ERR_PTR(rc); |
| 67 | } |
Dmitry Kasatkin | d21b594 | 2011-12-05 13:17:41 +0200 | [diff] [blame] | 68 | } |
Dmitry Kasatkin | 97426f9 | 2011-12-05 13:17:42 +0200 | [diff] [blame] | 69 | out: |
| 70 | mutex_unlock(&mutex); |
Mimi Zohar | 66dbc325 | 2011-03-15 16:12:09 -0400 | [diff] [blame] | 71 | } |
Dmitry Kasatkin | d46eb36 | 2011-03-09 15:07:36 -0500 | [diff] [blame] | 72 | |
Dmitry Kasatkin | 15647eb | 2011-09-01 14:41:40 +0300 | [diff] [blame] | 73 | desc = kmalloc(sizeof(*desc) + crypto_shash_descsize(*tfm), |
Dmitry Kasatkin | d46eb36 | 2011-03-09 15:07:36 -0500 | [diff] [blame] | 74 | GFP_KERNEL); |
| 75 | if (!desc) |
| 76 | return ERR_PTR(-ENOMEM); |
| 77 | |
Dmitry Kasatkin | 15647eb | 2011-09-01 14:41:40 +0300 | [diff] [blame] | 78 | desc->tfm = *tfm; |
Dmitry Kasatkin | d46eb36 | 2011-03-09 15:07:36 -0500 | [diff] [blame] | 79 | desc->flags = CRYPTO_TFM_REQ_MAY_SLEEP; |
| 80 | |
Dmitry Kasatkin | d46eb36 | 2011-03-09 15:07:36 -0500 | [diff] [blame] | 81 | rc = crypto_shash_init(desc); |
Dmitry Kasatkin | d46eb36 | 2011-03-09 15:07:36 -0500 | [diff] [blame] | 82 | if (rc) { |
| 83 | kfree(desc); |
| 84 | return ERR_PTR(rc); |
| 85 | } |
| 86 | return desc; |
Mimi Zohar | 66dbc325 | 2011-03-15 16:12:09 -0400 | [diff] [blame] | 87 | } |
| 88 | |
| 89 | /* Protect against 'cutting & pasting' security.evm xattr, include inode |
| 90 | * specific info. |
| 91 | * |
| 92 | * (Additional directory/file metadata needs to be added for more complete |
| 93 | * protection.) |
| 94 | */ |
Dmitry Kasatkin | d46eb36 | 2011-03-09 15:07:36 -0500 | [diff] [blame] | 95 | static void hmac_add_misc(struct shash_desc *desc, struct inode *inode, |
Mimi Zohar | 66dbc325 | 2011-03-15 16:12:09 -0400 | [diff] [blame] | 96 | char *digest) |
| 97 | { |
| 98 | struct h_misc { |
| 99 | unsigned long ino; |
| 100 | __u32 generation; |
| 101 | uid_t uid; |
| 102 | gid_t gid; |
| 103 | umode_t mode; |
| 104 | } hmac_misc; |
Mimi Zohar | 66dbc325 | 2011-03-15 16:12:09 -0400 | [diff] [blame] | 105 | |
| 106 | memset(&hmac_misc, 0, sizeof hmac_misc); |
| 107 | hmac_misc.ino = inode->i_ino; |
| 108 | hmac_misc.generation = inode->i_generation; |
Eric W. Biederman | cf9c935 | 2012-05-25 18:22:35 -0600 | [diff] [blame] | 109 | hmac_misc.uid = from_kuid(&init_user_ns, inode->i_uid); |
| 110 | hmac_misc.gid = from_kgid(&init_user_ns, inode->i_gid); |
Mimi Zohar | 66dbc325 | 2011-03-15 16:12:09 -0400 | [diff] [blame] | 111 | hmac_misc.mode = inode->i_mode; |
Dmitry Kasatkin | d46eb36 | 2011-03-09 15:07:36 -0500 | [diff] [blame] | 112 | crypto_shash_update(desc, (const u8 *)&hmac_misc, sizeof hmac_misc); |
Dmitry Kasatkin | 74de668 | 2012-09-10 10:37:20 +0300 | [diff] [blame] | 113 | if (evm_hmac_version > 1) |
| 114 | crypto_shash_update(desc, inode->i_sb->s_uuid, |
| 115 | sizeof(inode->i_sb->s_uuid)); |
Dmitry Kasatkin | d46eb36 | 2011-03-09 15:07:36 -0500 | [diff] [blame] | 116 | crypto_shash_final(desc, digest); |
Mimi Zohar | 66dbc325 | 2011-03-15 16:12:09 -0400 | [diff] [blame] | 117 | } |
| 118 | |
| 119 | /* |
| 120 | * Calculate the HMAC value across the set of protected security xattrs. |
| 121 | * |
| 122 | * Instead of retrieving the requested xattr, for performance, calculate |
| 123 | * the hmac using the requested xattr value. Don't alloc/free memory for |
| 124 | * each xattr, but attempt to re-use the previously allocated memory. |
| 125 | */ |
Dmitry Kasatkin | 15647eb | 2011-09-01 14:41:40 +0300 | [diff] [blame] | 126 | static int evm_calc_hmac_or_hash(struct dentry *dentry, |
| 127 | const char *req_xattr_name, |
| 128 | const char *req_xattr_value, |
| 129 | size_t req_xattr_value_len, |
| 130 | char type, char *digest) |
Mimi Zohar | 66dbc325 | 2011-03-15 16:12:09 -0400 | [diff] [blame] | 131 | { |
| 132 | struct inode *inode = dentry->d_inode; |
Dmitry Kasatkin | d46eb36 | 2011-03-09 15:07:36 -0500 | [diff] [blame] | 133 | struct shash_desc *desc; |
Mimi Zohar | 66dbc325 | 2011-03-15 16:12:09 -0400 | [diff] [blame] | 134 | char **xattrname; |
| 135 | size_t xattr_size = 0; |
| 136 | char *xattr_value = NULL; |
| 137 | int error; |
| 138 | int size; |
| 139 | |
| 140 | if (!inode->i_op || !inode->i_op->getxattr) |
| 141 | return -EOPNOTSUPP; |
Dmitry Kasatkin | 15647eb | 2011-09-01 14:41:40 +0300 | [diff] [blame] | 142 | desc = init_desc(type); |
Dmitry Kasatkin | d46eb36 | 2011-03-09 15:07:36 -0500 | [diff] [blame] | 143 | if (IS_ERR(desc)) |
| 144 | return PTR_ERR(desc); |
Mimi Zohar | 66dbc325 | 2011-03-15 16:12:09 -0400 | [diff] [blame] | 145 | |
| 146 | error = -ENODATA; |
| 147 | for (xattrname = evm_config_xattrnames; *xattrname != NULL; xattrname++) { |
| 148 | if ((req_xattr_name && req_xattr_value) |
| 149 | && !strcmp(*xattrname, req_xattr_name)) { |
| 150 | error = 0; |
Dmitry Kasatkin | d46eb36 | 2011-03-09 15:07:36 -0500 | [diff] [blame] | 151 | crypto_shash_update(desc, (const u8 *)req_xattr_value, |
| 152 | req_xattr_value_len); |
Mimi Zohar | 66dbc325 | 2011-03-15 16:12:09 -0400 | [diff] [blame] | 153 | continue; |
| 154 | } |
| 155 | size = vfs_getxattr_alloc(dentry, *xattrname, |
| 156 | &xattr_value, xattr_size, GFP_NOFS); |
| 157 | if (size == -ENOMEM) { |
| 158 | error = -ENOMEM; |
| 159 | goto out; |
| 160 | } |
| 161 | if (size < 0) |
| 162 | continue; |
| 163 | |
| 164 | error = 0; |
| 165 | xattr_size = size; |
Dmitry Kasatkin | d46eb36 | 2011-03-09 15:07:36 -0500 | [diff] [blame] | 166 | crypto_shash_update(desc, (const u8 *)xattr_value, xattr_size); |
Mimi Zohar | 66dbc325 | 2011-03-15 16:12:09 -0400 | [diff] [blame] | 167 | } |
Dmitry Kasatkin | d46eb36 | 2011-03-09 15:07:36 -0500 | [diff] [blame] | 168 | hmac_add_misc(desc, inode, digest); |
| 169 | |
Mimi Zohar | 66dbc325 | 2011-03-15 16:12:09 -0400 | [diff] [blame] | 170 | out: |
Dmitry Kasatkin | d46eb36 | 2011-03-09 15:07:36 -0500 | [diff] [blame] | 171 | kfree(xattr_value); |
| 172 | kfree(desc); |
Mimi Zohar | 66dbc325 | 2011-03-15 16:12:09 -0400 | [diff] [blame] | 173 | return error; |
| 174 | } |
| 175 | |
Dmitry Kasatkin | 15647eb | 2011-09-01 14:41:40 +0300 | [diff] [blame] | 176 | int evm_calc_hmac(struct dentry *dentry, const char *req_xattr_name, |
| 177 | const char *req_xattr_value, size_t req_xattr_value_len, |
| 178 | char *digest) |
| 179 | { |
| 180 | return evm_calc_hmac_or_hash(dentry, req_xattr_name, req_xattr_value, |
| 181 | req_xattr_value_len, EVM_XATTR_HMAC, digest); |
| 182 | } |
| 183 | |
| 184 | int evm_calc_hash(struct dentry *dentry, const char *req_xattr_name, |
| 185 | const char *req_xattr_value, size_t req_xattr_value_len, |
| 186 | char *digest) |
| 187 | { |
| 188 | return evm_calc_hmac_or_hash(dentry, req_xattr_name, req_xattr_value, |
| 189 | req_xattr_value_len, IMA_XATTR_DIGEST, digest); |
| 190 | } |
| 191 | |
Mimi Zohar | 66dbc325 | 2011-03-15 16:12:09 -0400 | [diff] [blame] | 192 | /* |
| 193 | * Calculate the hmac and update security.evm xattr |
| 194 | * |
| 195 | * Expects to be called with i_mutex locked. |
| 196 | */ |
| 197 | int evm_update_evmxattr(struct dentry *dentry, const char *xattr_name, |
| 198 | const char *xattr_value, size_t xattr_value_len) |
| 199 | { |
| 200 | struct inode *inode = dentry->d_inode; |
Dmitry Kasatkin | 6be5cc5 | 2011-03-09 14:28:20 -0500 | [diff] [blame] | 201 | struct evm_ima_xattr_data xattr_data; |
Mimi Zohar | 66dbc325 | 2011-03-15 16:12:09 -0400 | [diff] [blame] | 202 | int rc = 0; |
| 203 | |
| 204 | rc = evm_calc_hmac(dentry, xattr_name, xattr_value, |
Dmitry Kasatkin | 6be5cc5 | 2011-03-09 14:28:20 -0500 | [diff] [blame] | 205 | xattr_value_len, xattr_data.digest); |
| 206 | if (rc == 0) { |
| 207 | xattr_data.type = EVM_XATTR_HMAC; |
Mimi Zohar | 66dbc325 | 2011-03-15 16:12:09 -0400 | [diff] [blame] | 208 | rc = __vfs_setxattr_noperm(dentry, XATTR_NAME_EVM, |
Dmitry Kasatkin | 6be5cc5 | 2011-03-09 14:28:20 -0500 | [diff] [blame] | 209 | &xattr_data, |
| 210 | sizeof(xattr_data), 0); |
Dmitry Kasatkin | a67adb9 | 2013-01-18 23:56:39 +0200 | [diff] [blame] | 211 | } else if (rc == -ENODATA && inode->i_op->removexattr) { |
Mimi Zohar | 66dbc325 | 2011-03-15 16:12:09 -0400 | [diff] [blame] | 212 | rc = inode->i_op->removexattr(dentry, XATTR_NAME_EVM); |
Dmitry Kasatkin | a67adb9 | 2013-01-18 23:56:39 +0200 | [diff] [blame] | 213 | } |
Mimi Zohar | 66dbc325 | 2011-03-15 16:12:09 -0400 | [diff] [blame] | 214 | return rc; |
| 215 | } |
| 216 | |
Mimi Zohar | cb723180 | 2011-03-09 14:40:44 -0500 | [diff] [blame] | 217 | int evm_init_hmac(struct inode *inode, const struct xattr *lsm_xattr, |
| 218 | char *hmac_val) |
| 219 | { |
Dmitry Kasatkin | d46eb36 | 2011-03-09 15:07:36 -0500 | [diff] [blame] | 220 | struct shash_desc *desc; |
Mimi Zohar | cb723180 | 2011-03-09 14:40:44 -0500 | [diff] [blame] | 221 | |
Dmitry Kasatkin | 15647eb | 2011-09-01 14:41:40 +0300 | [diff] [blame] | 222 | desc = init_desc(EVM_XATTR_HMAC); |
Dmitry Kasatkin | d46eb36 | 2011-03-09 15:07:36 -0500 | [diff] [blame] | 223 | if (IS_ERR(desc)) { |
Mimi Zohar | cb723180 | 2011-03-09 14:40:44 -0500 | [diff] [blame] | 224 | printk(KERN_INFO "init_desc failed\n"); |
Dmitry Kasatkin | d46eb36 | 2011-03-09 15:07:36 -0500 | [diff] [blame] | 225 | return PTR_ERR(desc); |
Mimi Zohar | cb723180 | 2011-03-09 14:40:44 -0500 | [diff] [blame] | 226 | } |
| 227 | |
Dmitry Kasatkin | d46eb36 | 2011-03-09 15:07:36 -0500 | [diff] [blame] | 228 | crypto_shash_update(desc, lsm_xattr->value, lsm_xattr->value_len); |
| 229 | hmac_add_misc(desc, inode, hmac_val); |
| 230 | kfree(desc); |
Mimi Zohar | cb723180 | 2011-03-09 14:40:44 -0500 | [diff] [blame] | 231 | return 0; |
| 232 | } |
| 233 | |
Mimi Zohar | 66dbc325 | 2011-03-15 16:12:09 -0400 | [diff] [blame] | 234 | /* |
| 235 | * Get the key from the TPM for the SHA1-HMAC |
| 236 | */ |
| 237 | int evm_init_key(void) |
| 238 | { |
| 239 | struct key *evm_key; |
| 240 | struct encrypted_key_payload *ekp; |
| 241 | int rc = 0; |
| 242 | |
| 243 | evm_key = request_key(&key_type_encrypted, EVMKEY, NULL); |
| 244 | if (IS_ERR(evm_key)) |
| 245 | return -ENOENT; |
| 246 | |
| 247 | down_read(&evm_key->sem); |
| 248 | ekp = evm_key->payload.data; |
| 249 | if (ekp->decrypted_datalen > MAX_KEY_SIZE) { |
| 250 | rc = -EINVAL; |
| 251 | goto out; |
| 252 | } |
| 253 | memcpy(evmkey, ekp->decrypted_data, ekp->decrypted_datalen); |
| 254 | out: |
| 255 | /* burn the original key contents */ |
| 256 | memset(ekp->decrypted_data, 0, ekp->decrypted_datalen); |
| 257 | up_read(&evm_key->sem); |
| 258 | key_put(evm_key); |
| 259 | return rc; |
| 260 | } |