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> |
| 19 | #include <linux/scatterlist.h> |
| 20 | #include <keys/encrypted-type.h> |
| 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 | |
| 28 | static int init_desc(struct hash_desc *desc) |
| 29 | { |
| 30 | int rc; |
| 31 | |
| 32 | desc->tfm = crypto_alloc_hash(evm_hmac, 0, CRYPTO_ALG_ASYNC); |
| 33 | if (IS_ERR(desc->tfm)) { |
| 34 | pr_info("Can not allocate %s (reason: %ld)\n", |
| 35 | evm_hmac, PTR_ERR(desc->tfm)); |
| 36 | rc = PTR_ERR(desc->tfm); |
| 37 | return rc; |
| 38 | } |
| 39 | desc->flags = 0; |
| 40 | rc = crypto_hash_setkey(desc->tfm, evmkey, evmkey_len); |
| 41 | if (rc) |
| 42 | goto out; |
| 43 | rc = crypto_hash_init(desc); |
| 44 | out: |
| 45 | if (rc) |
| 46 | crypto_free_hash(desc->tfm); |
| 47 | return rc; |
| 48 | } |
| 49 | |
| 50 | /* Protect against 'cutting & pasting' security.evm xattr, include inode |
| 51 | * specific info. |
| 52 | * |
| 53 | * (Additional directory/file metadata needs to be added for more complete |
| 54 | * protection.) |
| 55 | */ |
| 56 | static void hmac_add_misc(struct hash_desc *desc, struct inode *inode, |
| 57 | char *digest) |
| 58 | { |
| 59 | struct h_misc { |
| 60 | unsigned long ino; |
| 61 | __u32 generation; |
| 62 | uid_t uid; |
| 63 | gid_t gid; |
| 64 | umode_t mode; |
| 65 | } hmac_misc; |
| 66 | struct scatterlist sg[1]; |
| 67 | |
| 68 | memset(&hmac_misc, 0, sizeof hmac_misc); |
| 69 | hmac_misc.ino = inode->i_ino; |
| 70 | hmac_misc.generation = inode->i_generation; |
| 71 | hmac_misc.uid = inode->i_uid; |
| 72 | hmac_misc.gid = inode->i_gid; |
| 73 | hmac_misc.mode = inode->i_mode; |
| 74 | sg_init_one(sg, &hmac_misc, sizeof hmac_misc); |
| 75 | crypto_hash_update(desc, sg, sizeof hmac_misc); |
| 76 | crypto_hash_final(desc, digest); |
| 77 | } |
| 78 | |
| 79 | /* |
| 80 | * Calculate the HMAC value across the set of protected security xattrs. |
| 81 | * |
| 82 | * Instead of retrieving the requested xattr, for performance, calculate |
| 83 | * the hmac using the requested xattr value. Don't alloc/free memory for |
| 84 | * each xattr, but attempt to re-use the previously allocated memory. |
| 85 | */ |
| 86 | int evm_calc_hmac(struct dentry *dentry, const char *req_xattr_name, |
| 87 | const char *req_xattr_value, size_t req_xattr_value_len, |
| 88 | char *digest) |
| 89 | { |
| 90 | struct inode *inode = dentry->d_inode; |
| 91 | struct hash_desc desc; |
| 92 | struct scatterlist sg[1]; |
| 93 | char **xattrname; |
| 94 | size_t xattr_size = 0; |
| 95 | char *xattr_value = NULL; |
| 96 | int error; |
| 97 | int size; |
| 98 | |
| 99 | if (!inode->i_op || !inode->i_op->getxattr) |
| 100 | return -EOPNOTSUPP; |
| 101 | error = init_desc(&desc); |
| 102 | if (error) |
| 103 | return error; |
| 104 | |
| 105 | error = -ENODATA; |
| 106 | for (xattrname = evm_config_xattrnames; *xattrname != NULL; xattrname++) { |
| 107 | if ((req_xattr_name && req_xattr_value) |
| 108 | && !strcmp(*xattrname, req_xattr_name)) { |
| 109 | error = 0; |
| 110 | sg_init_one(sg, req_xattr_value, req_xattr_value_len); |
| 111 | crypto_hash_update(&desc, sg, req_xattr_value_len); |
| 112 | continue; |
| 113 | } |
| 114 | size = vfs_getxattr_alloc(dentry, *xattrname, |
| 115 | &xattr_value, xattr_size, GFP_NOFS); |
| 116 | if (size == -ENOMEM) { |
| 117 | error = -ENOMEM; |
| 118 | goto out; |
| 119 | } |
| 120 | if (size < 0) |
| 121 | continue; |
| 122 | |
| 123 | error = 0; |
| 124 | xattr_size = size; |
| 125 | sg_init_one(sg, xattr_value, xattr_size); |
| 126 | crypto_hash_update(&desc, sg, xattr_size); |
| 127 | } |
| 128 | hmac_add_misc(&desc, inode, digest); |
| 129 | kfree(xattr_value); |
| 130 | out: |
| 131 | crypto_free_hash(desc.tfm); |
| 132 | return error; |
| 133 | } |
| 134 | |
| 135 | /* |
| 136 | * Calculate the hmac and update security.evm xattr |
| 137 | * |
| 138 | * Expects to be called with i_mutex locked. |
| 139 | */ |
| 140 | int evm_update_evmxattr(struct dentry *dentry, const char *xattr_name, |
| 141 | const char *xattr_value, size_t xattr_value_len) |
| 142 | { |
| 143 | struct inode *inode = dentry->d_inode; |
Dmitry Kasatkin | 6be5cc5 | 2011-03-09 14:28:20 -0500 | [diff] [blame^] | 144 | struct evm_ima_xattr_data xattr_data; |
Mimi Zohar | 66dbc325 | 2011-03-15 16:12:09 -0400 | [diff] [blame] | 145 | int rc = 0; |
| 146 | |
| 147 | rc = evm_calc_hmac(dentry, xattr_name, xattr_value, |
Dmitry Kasatkin | 6be5cc5 | 2011-03-09 14:28:20 -0500 | [diff] [blame^] | 148 | xattr_value_len, xattr_data.digest); |
| 149 | if (rc == 0) { |
| 150 | xattr_data.type = EVM_XATTR_HMAC; |
Mimi Zohar | 66dbc325 | 2011-03-15 16:12:09 -0400 | [diff] [blame] | 151 | rc = __vfs_setxattr_noperm(dentry, XATTR_NAME_EVM, |
Dmitry Kasatkin | 6be5cc5 | 2011-03-09 14:28:20 -0500 | [diff] [blame^] | 152 | &xattr_data, |
| 153 | sizeof(xattr_data), 0); |
| 154 | } |
Mimi Zohar | 66dbc325 | 2011-03-15 16:12:09 -0400 | [diff] [blame] | 155 | else if (rc == -ENODATA) |
| 156 | rc = inode->i_op->removexattr(dentry, XATTR_NAME_EVM); |
| 157 | return rc; |
| 158 | } |
| 159 | |
| 160 | /* |
| 161 | * Get the key from the TPM for the SHA1-HMAC |
| 162 | */ |
| 163 | int evm_init_key(void) |
| 164 | { |
| 165 | struct key *evm_key; |
| 166 | struct encrypted_key_payload *ekp; |
| 167 | int rc = 0; |
| 168 | |
| 169 | evm_key = request_key(&key_type_encrypted, EVMKEY, NULL); |
| 170 | if (IS_ERR(evm_key)) |
| 171 | return -ENOENT; |
| 172 | |
| 173 | down_read(&evm_key->sem); |
| 174 | ekp = evm_key->payload.data; |
| 175 | if (ekp->decrypted_datalen > MAX_KEY_SIZE) { |
| 176 | rc = -EINVAL; |
| 177 | goto out; |
| 178 | } |
| 179 | memcpy(evmkey, ekp->decrypted_data, ekp->decrypted_datalen); |
| 180 | out: |
| 181 | /* burn the original key contents */ |
| 182 | memset(ekp->decrypted_data, 0, ekp->decrypted_datalen); |
| 183 | up_read(&evm_key->sem); |
| 184 | key_put(evm_key); |
| 185 | return rc; |
| 186 | } |