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 | |
Joe Perches | 20ee451 | 2014-02-24 13:59:56 -0800 | [diff] [blame] | 16 | #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt |
| 17 | |
Mimi Zohar | 66dbc325 | 2011-03-15 16:12:09 -0400 | [diff] [blame] | 18 | #include <linux/module.h> |
| 19 | #include <linux/crypto.h> |
| 20 | #include <linux/xattr.h> |
Dmitry Kasatkin | 7626676 | 2015-10-22 21:26:32 +0300 | [diff] [blame] | 21 | #include <linux/evm.h> |
Mimi Zohar | 66dbc325 | 2011-03-15 16:12:09 -0400 | [diff] [blame] | 22 | #include <keys/encrypted-type.h> |
Dmitry Kasatkin | d46eb36 | 2011-03-09 15:07:36 -0500 | [diff] [blame] | 23 | #include <crypto/hash.h> |
Mimi Zohar | 66dbc325 | 2011-03-15 16:12:09 -0400 | [diff] [blame] | 24 | #include "evm.h" |
| 25 | |
| 26 | #define EVMKEY "evm-key" |
| 27 | #define MAX_KEY_SIZE 128 |
| 28 | static unsigned char evmkey[MAX_KEY_SIZE]; |
| 29 | static int evmkey_len = MAX_KEY_SIZE; |
| 30 | |
Dmitry Kasatkin | d46eb36 | 2011-03-09 15:07:36 -0500 | [diff] [blame] | 31 | struct crypto_shash *hmac_tfm; |
Dmitry Kasatkin | 15647eb | 2011-09-01 14:41:40 +0300 | [diff] [blame] | 32 | struct crypto_shash *hash_tfm; |
Dmitry Kasatkin | d46eb36 | 2011-03-09 15:07:36 -0500 | [diff] [blame] | 33 | |
Dmitry Kasatkin | 97426f9 | 2011-12-05 13:17:42 +0200 | [diff] [blame] | 34 | static DEFINE_MUTEX(mutex); |
| 35 | |
Dmitry Kasatkin | 7626676 | 2015-10-22 21:26:32 +0300 | [diff] [blame] | 36 | #define EVM_SET_KEY_BUSY 0 |
| 37 | |
| 38 | static unsigned long evm_set_key_flags; |
| 39 | |
| 40 | /** |
| 41 | * evm_set_key() - set EVM HMAC key from the kernel |
| 42 | * @key: pointer to a buffer with the key data |
| 43 | * @size: length of the key data |
| 44 | * |
| 45 | * This function allows setting the EVM HMAC key from the kernel |
| 46 | * without using the "encrypted" key subsystem keys. It can be used |
| 47 | * by the crypto HW kernel module which has its own way of managing |
| 48 | * keys. |
| 49 | * |
| 50 | * key length should be between 32 and 128 bytes long |
| 51 | */ |
| 52 | int evm_set_key(void *key, size_t keylen) |
| 53 | { |
| 54 | int rc; |
| 55 | |
| 56 | rc = -EBUSY; |
| 57 | if (test_and_set_bit(EVM_SET_KEY_BUSY, &evm_set_key_flags)) |
| 58 | goto busy; |
| 59 | rc = -EINVAL; |
| 60 | if (keylen > MAX_KEY_SIZE) |
| 61 | goto inval; |
| 62 | memcpy(evmkey, key, keylen); |
| 63 | evm_initialized |= EVM_INIT_HMAC; |
| 64 | pr_info("key initialized\n"); |
| 65 | return 0; |
| 66 | inval: |
| 67 | clear_bit(EVM_SET_KEY_BUSY, &evm_set_key_flags); |
| 68 | busy: |
| 69 | pr_err("key initialization failed\n"); |
| 70 | return rc; |
| 71 | } |
| 72 | EXPORT_SYMBOL_GPL(evm_set_key); |
| 73 | |
James Morris | 8fcc995 | 2012-01-09 12:16:48 +1100 | [diff] [blame] | 74 | static struct shash_desc *init_desc(char type) |
Mimi Zohar | 66dbc325 | 2011-03-15 16:12:09 -0400 | [diff] [blame] | 75 | { |
Dmitry Kasatkin | 143b01d | 2011-12-05 13:17:42 +0200 | [diff] [blame] | 76 | long rc; |
Dmitry Kasatkin | 15647eb | 2011-09-01 14:41:40 +0300 | [diff] [blame] | 77 | char *algo; |
| 78 | struct crypto_shash **tfm; |
Dmitry Kasatkin | d46eb36 | 2011-03-09 15:07:36 -0500 | [diff] [blame] | 79 | struct shash_desc *desc; |
Mimi Zohar | 66dbc325 | 2011-03-15 16:12:09 -0400 | [diff] [blame] | 80 | |
Dmitry Kasatkin | 15647eb | 2011-09-01 14:41:40 +0300 | [diff] [blame] | 81 | if (type == EVM_XATTR_HMAC) { |
Dmitry Kasatkin | 26ddabf | 2015-10-22 21:26:26 +0300 | [diff] [blame] | 82 | if (!(evm_initialized & EVM_INIT_HMAC)) { |
| 83 | pr_err("HMAC key is not set\n"); |
| 84 | return ERR_PTR(-ENOKEY); |
| 85 | } |
Dmitry Kasatkin | 15647eb | 2011-09-01 14:41:40 +0300 | [diff] [blame] | 86 | tfm = &hmac_tfm; |
| 87 | algo = evm_hmac; |
| 88 | } else { |
| 89 | tfm = &hash_tfm; |
| 90 | algo = evm_hash; |
| 91 | } |
| 92 | |
| 93 | if (*tfm == NULL) { |
Dmitry Kasatkin | 97426f9 | 2011-12-05 13:17:42 +0200 | [diff] [blame] | 94 | mutex_lock(&mutex); |
Dmitry Kasatkin | 143b01d | 2011-12-05 13:17:42 +0200 | [diff] [blame] | 95 | if (*tfm) |
Dmitry Kasatkin | 97426f9 | 2011-12-05 13:17:42 +0200 | [diff] [blame] | 96 | goto out; |
Dmitry Kasatkin | 15647eb | 2011-09-01 14:41:40 +0300 | [diff] [blame] | 97 | *tfm = crypto_alloc_shash(algo, 0, CRYPTO_ALG_ASYNC); |
| 98 | if (IS_ERR(*tfm)) { |
Dmitry Kasatkin | 15647eb | 2011-09-01 14:41:40 +0300 | [diff] [blame] | 99 | rc = PTR_ERR(*tfm); |
Dmitry Kasatkin | 143b01d | 2011-12-05 13:17:42 +0200 | [diff] [blame] | 100 | pr_err("Can not allocate %s (reason: %ld)\n", algo, rc); |
Dmitry Kasatkin | 15647eb | 2011-09-01 14:41:40 +0300 | [diff] [blame] | 101 | *tfm = NULL; |
Dmitry Kasatkin | 97426f9 | 2011-12-05 13:17:42 +0200 | [diff] [blame] | 102 | mutex_unlock(&mutex); |
Dmitry Kasatkin | d46eb36 | 2011-03-09 15:07:36 -0500 | [diff] [blame] | 103 | return ERR_PTR(rc); |
| 104 | } |
Dmitry Kasatkin | 88d7ed3 | 2011-12-05 13:17:41 +0200 | [diff] [blame] | 105 | if (type == EVM_XATTR_HMAC) { |
| 106 | rc = crypto_shash_setkey(*tfm, evmkey, evmkey_len); |
| 107 | if (rc) { |
| 108 | crypto_free_shash(*tfm); |
| 109 | *tfm = NULL; |
Dmitry Kasatkin | 143b01d | 2011-12-05 13:17:42 +0200 | [diff] [blame] | 110 | mutex_unlock(&mutex); |
Dmitry Kasatkin | 88d7ed3 | 2011-12-05 13:17:41 +0200 | [diff] [blame] | 111 | return ERR_PTR(rc); |
| 112 | } |
Dmitry Kasatkin | d21b594 | 2011-12-05 13:17:41 +0200 | [diff] [blame] | 113 | } |
Dmitry Kasatkin | 97426f9 | 2011-12-05 13:17:42 +0200 | [diff] [blame] | 114 | out: |
| 115 | mutex_unlock(&mutex); |
Mimi Zohar | 66dbc325 | 2011-03-15 16:12:09 -0400 | [diff] [blame] | 116 | } |
Dmitry Kasatkin | d46eb36 | 2011-03-09 15:07:36 -0500 | [diff] [blame] | 117 | |
Dmitry Kasatkin | 15647eb | 2011-09-01 14:41:40 +0300 | [diff] [blame] | 118 | desc = kmalloc(sizeof(*desc) + crypto_shash_descsize(*tfm), |
Dmitry Kasatkin | d46eb36 | 2011-03-09 15:07:36 -0500 | [diff] [blame] | 119 | GFP_KERNEL); |
| 120 | if (!desc) |
| 121 | return ERR_PTR(-ENOMEM); |
| 122 | |
Dmitry Kasatkin | 15647eb | 2011-09-01 14:41:40 +0300 | [diff] [blame] | 123 | desc->tfm = *tfm; |
Dmitry Kasatkin | d46eb36 | 2011-03-09 15:07:36 -0500 | [diff] [blame] | 124 | desc->flags = CRYPTO_TFM_REQ_MAY_SLEEP; |
| 125 | |
Dmitry Kasatkin | d46eb36 | 2011-03-09 15:07:36 -0500 | [diff] [blame] | 126 | rc = crypto_shash_init(desc); |
Dmitry Kasatkin | d46eb36 | 2011-03-09 15:07:36 -0500 | [diff] [blame] | 127 | if (rc) { |
| 128 | kfree(desc); |
| 129 | return ERR_PTR(rc); |
| 130 | } |
| 131 | return desc; |
Mimi Zohar | 66dbc325 | 2011-03-15 16:12:09 -0400 | [diff] [blame] | 132 | } |
| 133 | |
| 134 | /* Protect against 'cutting & pasting' security.evm xattr, include inode |
| 135 | * specific info. |
| 136 | * |
| 137 | * (Additional directory/file metadata needs to be added for more complete |
| 138 | * protection.) |
| 139 | */ |
Dmitry Kasatkin | d46eb36 | 2011-03-09 15:07:36 -0500 | [diff] [blame] | 140 | static void hmac_add_misc(struct shash_desc *desc, struct inode *inode, |
Mimi Zohar | 66dbc325 | 2011-03-15 16:12:09 -0400 | [diff] [blame] | 141 | char *digest) |
| 142 | { |
| 143 | struct h_misc { |
| 144 | unsigned long ino; |
| 145 | __u32 generation; |
| 146 | uid_t uid; |
| 147 | gid_t gid; |
| 148 | umode_t mode; |
| 149 | } hmac_misc; |
Mimi Zohar | 66dbc325 | 2011-03-15 16:12:09 -0400 | [diff] [blame] | 150 | |
Dmitry Kasatkin | 2bb930a | 2014-03-04 18:04:20 +0200 | [diff] [blame] | 151 | memset(&hmac_misc, 0, sizeof(hmac_misc)); |
Mimi Zohar | 66dbc325 | 2011-03-15 16:12:09 -0400 | [diff] [blame] | 152 | hmac_misc.ino = inode->i_ino; |
| 153 | hmac_misc.generation = inode->i_generation; |
Seth Forshee | 0b3c976 | 2015-02-05 10:44:50 -0600 | [diff] [blame] | 154 | hmac_misc.uid = from_kuid(inode->i_sb->s_user_ns, inode->i_uid); |
| 155 | hmac_misc.gid = from_kgid(inode->i_sb->s_user_ns, inode->i_gid); |
Mimi Zohar | 66dbc325 | 2011-03-15 16:12:09 -0400 | [diff] [blame] | 156 | hmac_misc.mode = inode->i_mode; |
Dmitry Kasatkin | 2bb930a | 2014-03-04 18:04:20 +0200 | [diff] [blame] | 157 | crypto_shash_update(desc, (const u8 *)&hmac_misc, sizeof(hmac_misc)); |
Dmitry Kasatkin | d3b3367 | 2014-03-28 14:31:04 +0200 | [diff] [blame] | 158 | if (evm_hmac_attrs & EVM_ATTR_FSUUID) |
Dmitry Kasatkin | 74de668 | 2012-09-10 10:37:20 +0300 | [diff] [blame] | 159 | crypto_shash_update(desc, inode->i_sb->s_uuid, |
| 160 | sizeof(inode->i_sb->s_uuid)); |
Dmitry Kasatkin | d46eb36 | 2011-03-09 15:07:36 -0500 | [diff] [blame] | 161 | crypto_shash_final(desc, digest); |
Mimi Zohar | 66dbc325 | 2011-03-15 16:12:09 -0400 | [diff] [blame] | 162 | } |
| 163 | |
| 164 | /* |
| 165 | * Calculate the HMAC value across the set of protected security xattrs. |
| 166 | * |
| 167 | * Instead of retrieving the requested xattr, for performance, calculate |
| 168 | * the hmac using the requested xattr value. Don't alloc/free memory for |
| 169 | * each xattr, but attempt to re-use the previously allocated memory. |
| 170 | */ |
Dmitry Kasatkin | 15647eb | 2011-09-01 14:41:40 +0300 | [diff] [blame] | 171 | static int evm_calc_hmac_or_hash(struct dentry *dentry, |
| 172 | const char *req_xattr_name, |
| 173 | const char *req_xattr_value, |
| 174 | size_t req_xattr_value_len, |
| 175 | char type, char *digest) |
Mimi Zohar | 66dbc325 | 2011-03-15 16:12:09 -0400 | [diff] [blame] | 176 | { |
David Howells | c6f493d | 2015-03-17 22:26:22 +0000 | [diff] [blame] | 177 | struct inode *inode = d_backing_inode(dentry); |
Dmitry Kasatkin | d46eb36 | 2011-03-09 15:07:36 -0500 | [diff] [blame] | 178 | struct shash_desc *desc; |
Mimi Zohar | 66dbc325 | 2011-03-15 16:12:09 -0400 | [diff] [blame] | 179 | char **xattrname; |
| 180 | size_t xattr_size = 0; |
| 181 | char *xattr_value = NULL; |
| 182 | int error; |
| 183 | int size; |
| 184 | |
Andreas Gruenbacher | 5d6c319 | 2016-09-29 17:48:42 +0200 | [diff] [blame] | 185 | if (!(inode->i_opflags & IOP_XATTR)) |
Mimi Zohar | 66dbc325 | 2011-03-15 16:12:09 -0400 | [diff] [blame] | 186 | return -EOPNOTSUPP; |
Andreas Gruenbacher | 5d6c319 | 2016-09-29 17:48:42 +0200 | [diff] [blame] | 187 | |
Dmitry Kasatkin | 15647eb | 2011-09-01 14:41:40 +0300 | [diff] [blame] | 188 | desc = init_desc(type); |
Dmitry Kasatkin | d46eb36 | 2011-03-09 15:07:36 -0500 | [diff] [blame] | 189 | if (IS_ERR(desc)) |
| 190 | return PTR_ERR(desc); |
Mimi Zohar | 66dbc325 | 2011-03-15 16:12:09 -0400 | [diff] [blame] | 191 | |
| 192 | error = -ENODATA; |
| 193 | for (xattrname = evm_config_xattrnames; *xattrname != NULL; xattrname++) { |
| 194 | if ((req_xattr_name && req_xattr_value) |
| 195 | && !strcmp(*xattrname, req_xattr_name)) { |
| 196 | error = 0; |
Dmitry Kasatkin | d46eb36 | 2011-03-09 15:07:36 -0500 | [diff] [blame] | 197 | crypto_shash_update(desc, (const u8 *)req_xattr_value, |
| 198 | req_xattr_value_len); |
Mimi Zohar | 66dbc325 | 2011-03-15 16:12:09 -0400 | [diff] [blame] | 199 | continue; |
| 200 | } |
| 201 | size = vfs_getxattr_alloc(dentry, *xattrname, |
| 202 | &xattr_value, xattr_size, GFP_NOFS); |
| 203 | if (size == -ENOMEM) { |
| 204 | error = -ENOMEM; |
| 205 | goto out; |
| 206 | } |
| 207 | if (size < 0) |
| 208 | continue; |
| 209 | |
| 210 | error = 0; |
| 211 | xattr_size = size; |
Dmitry Kasatkin | d46eb36 | 2011-03-09 15:07:36 -0500 | [diff] [blame] | 212 | crypto_shash_update(desc, (const u8 *)xattr_value, xattr_size); |
Mimi Zohar | 66dbc325 | 2011-03-15 16:12:09 -0400 | [diff] [blame] | 213 | } |
Dmitry Kasatkin | d46eb36 | 2011-03-09 15:07:36 -0500 | [diff] [blame] | 214 | hmac_add_misc(desc, inode, digest); |
| 215 | |
Mimi Zohar | 66dbc325 | 2011-03-15 16:12:09 -0400 | [diff] [blame] | 216 | out: |
Dmitry Kasatkin | d46eb36 | 2011-03-09 15:07:36 -0500 | [diff] [blame] | 217 | kfree(xattr_value); |
| 218 | kfree(desc); |
Mimi Zohar | 66dbc325 | 2011-03-15 16:12:09 -0400 | [diff] [blame] | 219 | return error; |
| 220 | } |
| 221 | |
Dmitry Kasatkin | 15647eb | 2011-09-01 14:41:40 +0300 | [diff] [blame] | 222 | int evm_calc_hmac(struct dentry *dentry, const char *req_xattr_name, |
| 223 | const char *req_xattr_value, size_t req_xattr_value_len, |
| 224 | char *digest) |
| 225 | { |
| 226 | return evm_calc_hmac_or_hash(dentry, req_xattr_name, req_xattr_value, |
| 227 | req_xattr_value_len, EVM_XATTR_HMAC, digest); |
| 228 | } |
| 229 | |
| 230 | int evm_calc_hash(struct dentry *dentry, const char *req_xattr_name, |
| 231 | const char *req_xattr_value, size_t req_xattr_value_len, |
| 232 | char *digest) |
| 233 | { |
| 234 | return evm_calc_hmac_or_hash(dentry, req_xattr_name, req_xattr_value, |
| 235 | req_xattr_value_len, IMA_XATTR_DIGEST, digest); |
| 236 | } |
| 237 | |
Mimi Zohar | 66dbc325 | 2011-03-15 16:12:09 -0400 | [diff] [blame] | 238 | /* |
| 239 | * Calculate the hmac and update security.evm xattr |
| 240 | * |
| 241 | * Expects to be called with i_mutex locked. |
| 242 | */ |
| 243 | int evm_update_evmxattr(struct dentry *dentry, const char *xattr_name, |
| 244 | const char *xattr_value, size_t xattr_value_len) |
| 245 | { |
David Howells | c6f493d | 2015-03-17 22:26:22 +0000 | [diff] [blame] | 246 | struct inode *inode = d_backing_inode(dentry); |
Dmitry Kasatkin | 6be5cc5 | 2011-03-09 14:28:20 -0500 | [diff] [blame] | 247 | struct evm_ima_xattr_data xattr_data; |
Mimi Zohar | 66dbc325 | 2011-03-15 16:12:09 -0400 | [diff] [blame] | 248 | int rc = 0; |
| 249 | |
| 250 | rc = evm_calc_hmac(dentry, xattr_name, xattr_value, |
Dmitry Kasatkin | 6be5cc5 | 2011-03-09 14:28:20 -0500 | [diff] [blame] | 251 | xattr_value_len, xattr_data.digest); |
| 252 | if (rc == 0) { |
| 253 | xattr_data.type = EVM_XATTR_HMAC; |
Mimi Zohar | 66dbc325 | 2011-03-15 16:12:09 -0400 | [diff] [blame] | 254 | rc = __vfs_setxattr_noperm(dentry, XATTR_NAME_EVM, |
Dmitry Kasatkin | 6be5cc5 | 2011-03-09 14:28:20 -0500 | [diff] [blame] | 255 | &xattr_data, |
| 256 | sizeof(xattr_data), 0); |
Andreas Gruenbacher | 5d6c319 | 2016-09-29 17:48:42 +0200 | [diff] [blame] | 257 | } else if (rc == -ENODATA && (inode->i_opflags & IOP_XATTR)) { |
| 258 | rc = __vfs_removexattr(dentry, XATTR_NAME_EVM); |
Dmitry Kasatkin | a67adb9 | 2013-01-18 23:56:39 +0200 | [diff] [blame] | 259 | } |
Mimi Zohar | 66dbc325 | 2011-03-15 16:12:09 -0400 | [diff] [blame] | 260 | return rc; |
| 261 | } |
| 262 | |
Mimi Zohar | cb72318 | 2011-03-09 14:40:44 -0500 | [diff] [blame] | 263 | int evm_init_hmac(struct inode *inode, const struct xattr *lsm_xattr, |
| 264 | char *hmac_val) |
| 265 | { |
Dmitry Kasatkin | d46eb36 | 2011-03-09 15:07:36 -0500 | [diff] [blame] | 266 | struct shash_desc *desc; |
Mimi Zohar | cb72318 | 2011-03-09 14:40:44 -0500 | [diff] [blame] | 267 | |
Dmitry Kasatkin | 15647eb | 2011-09-01 14:41:40 +0300 | [diff] [blame] | 268 | desc = init_desc(EVM_XATTR_HMAC); |
Dmitry Kasatkin | d46eb36 | 2011-03-09 15:07:36 -0500 | [diff] [blame] | 269 | if (IS_ERR(desc)) { |
Joe Perches | 20ee451 | 2014-02-24 13:59:56 -0800 | [diff] [blame] | 270 | pr_info("init_desc failed\n"); |
Dmitry Kasatkin | d46eb36 | 2011-03-09 15:07:36 -0500 | [diff] [blame] | 271 | return PTR_ERR(desc); |
Mimi Zohar | cb72318 | 2011-03-09 14:40:44 -0500 | [diff] [blame] | 272 | } |
| 273 | |
Dmitry Kasatkin | d46eb36 | 2011-03-09 15:07:36 -0500 | [diff] [blame] | 274 | crypto_shash_update(desc, lsm_xattr->value, lsm_xattr->value_len); |
| 275 | hmac_add_misc(desc, inode, hmac_val); |
| 276 | kfree(desc); |
Mimi Zohar | cb72318 | 2011-03-09 14:40:44 -0500 | [diff] [blame] | 277 | return 0; |
| 278 | } |
| 279 | |
Mimi Zohar | 66dbc325 | 2011-03-15 16:12:09 -0400 | [diff] [blame] | 280 | /* |
| 281 | * Get the key from the TPM for the SHA1-HMAC |
| 282 | */ |
| 283 | int evm_init_key(void) |
| 284 | { |
| 285 | struct key *evm_key; |
| 286 | struct encrypted_key_payload *ekp; |
Dmitry Kasatkin | 7626676 | 2015-10-22 21:26:32 +0300 | [diff] [blame] | 287 | int rc; |
Mimi Zohar | 66dbc325 | 2011-03-15 16:12:09 -0400 | [diff] [blame] | 288 | |
| 289 | evm_key = request_key(&key_type_encrypted, EVMKEY, NULL); |
| 290 | if (IS_ERR(evm_key)) |
| 291 | return -ENOENT; |
| 292 | |
| 293 | down_read(&evm_key->sem); |
David Howells | 146aa8b | 2015-10-21 14:04:48 +0100 | [diff] [blame] | 294 | ekp = evm_key->payload.data[0]; |
Dmitry Kasatkin | 7626676 | 2015-10-22 21:26:32 +0300 | [diff] [blame] | 295 | |
| 296 | rc = evm_set_key(ekp->decrypted_data, ekp->decrypted_datalen); |
| 297 | |
Mimi Zohar | 66dbc325 | 2011-03-15 16:12:09 -0400 | [diff] [blame] | 298 | /* burn the original key contents */ |
| 299 | memset(ekp->decrypted_data, 0, ekp->decrypted_datalen); |
| 300 | up_read(&evm_key->sem); |
| 301 | key_put(evm_key); |
| 302 | return rc; |
| 303 | } |