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 | |
HernĂ¡n Gonzalez | 1a82cee | 2018-02-27 19:16:59 -0300 | [diff] [blame] | 40 | static char * const evm_hmac = "hmac(sha1)"; |
| 41 | static char * const evm_hash = "sha1"; |
| 42 | |
Dmitry Kasatkin | 7626676 | 2015-10-22 21:26:32 +0300 | [diff] [blame] | 43 | /** |
| 44 | * evm_set_key() - set EVM HMAC key from the kernel |
| 45 | * @key: pointer to a buffer with the key data |
| 46 | * @size: length of the key data |
| 47 | * |
| 48 | * This function allows setting the EVM HMAC key from the kernel |
| 49 | * without using the "encrypted" key subsystem keys. It can be used |
| 50 | * by the crypto HW kernel module which has its own way of managing |
| 51 | * keys. |
| 52 | * |
| 53 | * key length should be between 32 and 128 bytes long |
| 54 | */ |
| 55 | int evm_set_key(void *key, size_t keylen) |
| 56 | { |
| 57 | int rc; |
| 58 | |
| 59 | rc = -EBUSY; |
| 60 | if (test_and_set_bit(EVM_SET_KEY_BUSY, &evm_set_key_flags)) |
| 61 | goto busy; |
| 62 | rc = -EINVAL; |
| 63 | if (keylen > MAX_KEY_SIZE) |
| 64 | goto inval; |
| 65 | memcpy(evmkey, key, keylen); |
| 66 | evm_initialized |= EVM_INIT_HMAC; |
| 67 | pr_info("key initialized\n"); |
| 68 | return 0; |
| 69 | inval: |
| 70 | clear_bit(EVM_SET_KEY_BUSY, &evm_set_key_flags); |
| 71 | busy: |
| 72 | pr_err("key initialization failed\n"); |
| 73 | return rc; |
| 74 | } |
| 75 | EXPORT_SYMBOL_GPL(evm_set_key); |
| 76 | |
James Morris | 8fcc995 | 2012-01-09 12:16:48 +1100 | [diff] [blame] | 77 | static struct shash_desc *init_desc(char type) |
Mimi Zohar | 66dbc325 | 2011-03-15 16:12:09 -0400 | [diff] [blame] | 78 | { |
Dmitry Kasatkin | 143b01d | 2011-12-05 13:17:42 +0200 | [diff] [blame] | 79 | long rc; |
Dmitry Kasatkin | 15647eb | 2011-09-01 14:41:40 +0300 | [diff] [blame] | 80 | char *algo; |
| 81 | struct crypto_shash **tfm; |
Dmitry Kasatkin | d46eb36 | 2011-03-09 15:07:36 -0500 | [diff] [blame] | 82 | struct shash_desc *desc; |
Mimi Zohar | 66dbc325 | 2011-03-15 16:12:09 -0400 | [diff] [blame] | 83 | |
Dmitry Kasatkin | 15647eb | 2011-09-01 14:41:40 +0300 | [diff] [blame] | 84 | if (type == EVM_XATTR_HMAC) { |
Dmitry Kasatkin | 26ddabf | 2015-10-22 21:26:26 +0300 | [diff] [blame] | 85 | if (!(evm_initialized & EVM_INIT_HMAC)) { |
Matthew Garrett | 0485d06 | 2017-10-11 12:11:12 -0700 | [diff] [blame] | 86 | pr_err_once("HMAC key is not set\n"); |
Dmitry Kasatkin | 26ddabf | 2015-10-22 21:26:26 +0300 | [diff] [blame] | 87 | return ERR_PTR(-ENOKEY); |
| 88 | } |
Dmitry Kasatkin | 15647eb | 2011-09-01 14:41:40 +0300 | [diff] [blame] | 89 | tfm = &hmac_tfm; |
| 90 | algo = evm_hmac; |
| 91 | } else { |
| 92 | tfm = &hash_tfm; |
| 93 | algo = evm_hash; |
| 94 | } |
| 95 | |
| 96 | if (*tfm == NULL) { |
Dmitry Kasatkin | 97426f9 | 2011-12-05 13:17:42 +0200 | [diff] [blame] | 97 | mutex_lock(&mutex); |
Dmitry Kasatkin | 143b01d | 2011-12-05 13:17:42 +0200 | [diff] [blame] | 98 | if (*tfm) |
Dmitry Kasatkin | 97426f9 | 2011-12-05 13:17:42 +0200 | [diff] [blame] | 99 | goto out; |
Dmitry Kasatkin | 15647eb | 2011-09-01 14:41:40 +0300 | [diff] [blame] | 100 | *tfm = crypto_alloc_shash(algo, 0, CRYPTO_ALG_ASYNC); |
| 101 | if (IS_ERR(*tfm)) { |
Dmitry Kasatkin | 15647eb | 2011-09-01 14:41:40 +0300 | [diff] [blame] | 102 | rc = PTR_ERR(*tfm); |
Dmitry Kasatkin | 143b01d | 2011-12-05 13:17:42 +0200 | [diff] [blame] | 103 | pr_err("Can not allocate %s (reason: %ld)\n", algo, rc); |
Dmitry Kasatkin | 15647eb | 2011-09-01 14:41:40 +0300 | [diff] [blame] | 104 | *tfm = NULL; |
Dmitry Kasatkin | 97426f9 | 2011-12-05 13:17:42 +0200 | [diff] [blame] | 105 | mutex_unlock(&mutex); |
Dmitry Kasatkin | d46eb36 | 2011-03-09 15:07:36 -0500 | [diff] [blame] | 106 | return ERR_PTR(rc); |
| 107 | } |
Dmitry Kasatkin | 88d7ed3 | 2011-12-05 13:17:41 +0200 | [diff] [blame] | 108 | if (type == EVM_XATTR_HMAC) { |
| 109 | rc = crypto_shash_setkey(*tfm, evmkey, evmkey_len); |
| 110 | if (rc) { |
| 111 | crypto_free_shash(*tfm); |
| 112 | *tfm = NULL; |
Dmitry Kasatkin | 143b01d | 2011-12-05 13:17:42 +0200 | [diff] [blame] | 113 | mutex_unlock(&mutex); |
Dmitry Kasatkin | 88d7ed3 | 2011-12-05 13:17:41 +0200 | [diff] [blame] | 114 | return ERR_PTR(rc); |
| 115 | } |
Dmitry Kasatkin | d21b594 | 2011-12-05 13:17:41 +0200 | [diff] [blame] | 116 | } |
Dmitry Kasatkin | 97426f9 | 2011-12-05 13:17:42 +0200 | [diff] [blame] | 117 | out: |
| 118 | mutex_unlock(&mutex); |
Mimi Zohar | 66dbc325 | 2011-03-15 16:12:09 -0400 | [diff] [blame] | 119 | } |
Dmitry Kasatkin | d46eb36 | 2011-03-09 15:07:36 -0500 | [diff] [blame] | 120 | |
Dmitry Kasatkin | 15647eb | 2011-09-01 14:41:40 +0300 | [diff] [blame] | 121 | desc = kmalloc(sizeof(*desc) + crypto_shash_descsize(*tfm), |
Dmitry Kasatkin | d46eb36 | 2011-03-09 15:07:36 -0500 | [diff] [blame] | 122 | GFP_KERNEL); |
| 123 | if (!desc) |
| 124 | return ERR_PTR(-ENOMEM); |
| 125 | |
Dmitry Kasatkin | 15647eb | 2011-09-01 14:41:40 +0300 | [diff] [blame] | 126 | desc->tfm = *tfm; |
Dmitry Kasatkin | d46eb36 | 2011-03-09 15:07:36 -0500 | [diff] [blame] | 127 | desc->flags = CRYPTO_TFM_REQ_MAY_SLEEP; |
| 128 | |
Dmitry Kasatkin | d46eb36 | 2011-03-09 15:07:36 -0500 | [diff] [blame] | 129 | rc = crypto_shash_init(desc); |
Dmitry Kasatkin | d46eb36 | 2011-03-09 15:07:36 -0500 | [diff] [blame] | 130 | if (rc) { |
| 131 | kfree(desc); |
| 132 | return ERR_PTR(rc); |
| 133 | } |
| 134 | return desc; |
Mimi Zohar | 66dbc325 | 2011-03-15 16:12:09 -0400 | [diff] [blame] | 135 | } |
| 136 | |
| 137 | /* Protect against 'cutting & pasting' security.evm xattr, include inode |
| 138 | * specific info. |
| 139 | * |
| 140 | * (Additional directory/file metadata needs to be added for more complete |
| 141 | * protection.) |
| 142 | */ |
Dmitry Kasatkin | d46eb36 | 2011-03-09 15:07:36 -0500 | [diff] [blame] | 143 | static void hmac_add_misc(struct shash_desc *desc, struct inode *inode, |
Matthew Garrett | 50b9774 | 2017-11-07 07:17:42 -0800 | [diff] [blame] | 144 | char type, char *digest) |
Mimi Zohar | 66dbc325 | 2011-03-15 16:12:09 -0400 | [diff] [blame] | 145 | { |
| 146 | struct h_misc { |
| 147 | unsigned long ino; |
| 148 | __u32 generation; |
| 149 | uid_t uid; |
| 150 | gid_t gid; |
| 151 | umode_t mode; |
| 152 | } hmac_misc; |
Mimi Zohar | 66dbc325 | 2011-03-15 16:12:09 -0400 | [diff] [blame] | 153 | |
Dmitry Kasatkin | 2bb930a | 2014-03-04 18:04:20 +0200 | [diff] [blame] | 154 | memset(&hmac_misc, 0, sizeof(hmac_misc)); |
Matthew Garrett | 50b9774 | 2017-11-07 07:17:42 -0800 | [diff] [blame] | 155 | /* Don't include the inode or generation number in portable |
| 156 | * signatures |
| 157 | */ |
| 158 | if (type != EVM_XATTR_PORTABLE_DIGSIG) { |
| 159 | hmac_misc.ino = inode->i_ino; |
| 160 | hmac_misc.generation = inode->i_generation; |
| 161 | } |
Eric W. Biederman | 19339c2 | 2016-12-02 09:35:31 -0600 | [diff] [blame] | 162 | /* The hmac uid and gid must be encoded in the initial user |
| 163 | * namespace (not the filesystems user namespace) as encoding |
| 164 | * them in the filesystems user namespace allows an attack |
| 165 | * where first they are written in an unprivileged fuse mount |
| 166 | * of a filesystem and then the system is tricked to mount the |
| 167 | * filesystem for real on next boot and trust it because |
| 168 | * everything is signed. |
| 169 | */ |
| 170 | hmac_misc.uid = from_kuid(&init_user_ns, inode->i_uid); |
| 171 | hmac_misc.gid = from_kgid(&init_user_ns, inode->i_gid); |
Mimi Zohar | 66dbc325 | 2011-03-15 16:12:09 -0400 | [diff] [blame] | 172 | hmac_misc.mode = inode->i_mode; |
Dmitry Kasatkin | 2bb930a | 2014-03-04 18:04:20 +0200 | [diff] [blame] | 173 | crypto_shash_update(desc, (const u8 *)&hmac_misc, sizeof(hmac_misc)); |
Matthew Garrett | 50b9774 | 2017-11-07 07:17:42 -0800 | [diff] [blame] | 174 | if ((evm_hmac_attrs & EVM_ATTR_FSUUID) && |
| 175 | type != EVM_XATTR_PORTABLE_DIGSIG) |
Christoph Hellwig | 8578709 | 2017-05-10 15:06:33 +0200 | [diff] [blame] | 176 | crypto_shash_update(desc, &inode->i_sb->s_uuid.b[0], |
Dmitry Kasatkin | 74de668 | 2012-09-10 10:37:20 +0300 | [diff] [blame] | 177 | sizeof(inode->i_sb->s_uuid)); |
Dmitry Kasatkin | d46eb36 | 2011-03-09 15:07:36 -0500 | [diff] [blame] | 178 | crypto_shash_final(desc, digest); |
Mimi Zohar | 66dbc325 | 2011-03-15 16:12:09 -0400 | [diff] [blame] | 179 | } |
| 180 | |
| 181 | /* |
| 182 | * Calculate the HMAC value across the set of protected security xattrs. |
| 183 | * |
| 184 | * Instead of retrieving the requested xattr, for performance, calculate |
| 185 | * the hmac using the requested xattr value. Don't alloc/free memory for |
| 186 | * each xattr, but attempt to re-use the previously allocated memory. |
| 187 | */ |
Dmitry Kasatkin | 15647eb | 2011-09-01 14:41:40 +0300 | [diff] [blame] | 188 | static int evm_calc_hmac_or_hash(struct dentry *dentry, |
| 189 | const char *req_xattr_name, |
| 190 | const char *req_xattr_value, |
| 191 | size_t req_xattr_value_len, |
| 192 | char type, char *digest) |
Mimi Zohar | 66dbc325 | 2011-03-15 16:12:09 -0400 | [diff] [blame] | 193 | { |
David Howells | c6f493d | 2015-03-17 22:26:22 +0000 | [diff] [blame] | 194 | struct inode *inode = d_backing_inode(dentry); |
Dmitry Kasatkin | d46eb36 | 2011-03-09 15:07:36 -0500 | [diff] [blame] | 195 | struct shash_desc *desc; |
Mimi Zohar | 66dbc325 | 2011-03-15 16:12:09 -0400 | [diff] [blame] | 196 | char **xattrname; |
| 197 | size_t xattr_size = 0; |
| 198 | char *xattr_value = NULL; |
| 199 | int error; |
| 200 | int size; |
Matthew Garrett | 50b9774 | 2017-11-07 07:17:42 -0800 | [diff] [blame] | 201 | bool ima_present = false; |
Mimi Zohar | 66dbc325 | 2011-03-15 16:12:09 -0400 | [diff] [blame] | 202 | |
Andreas Gruenbacher | 5d6c319 | 2016-09-29 17:48:42 +0200 | [diff] [blame] | 203 | if (!(inode->i_opflags & IOP_XATTR)) |
Mimi Zohar | 66dbc325 | 2011-03-15 16:12:09 -0400 | [diff] [blame] | 204 | return -EOPNOTSUPP; |
Andreas Gruenbacher | 5d6c319 | 2016-09-29 17:48:42 +0200 | [diff] [blame] | 205 | |
Dmitry Kasatkin | 15647eb | 2011-09-01 14:41:40 +0300 | [diff] [blame] | 206 | desc = init_desc(type); |
Dmitry Kasatkin | d46eb36 | 2011-03-09 15:07:36 -0500 | [diff] [blame] | 207 | if (IS_ERR(desc)) |
| 208 | return PTR_ERR(desc); |
Mimi Zohar | 66dbc325 | 2011-03-15 16:12:09 -0400 | [diff] [blame] | 209 | |
| 210 | error = -ENODATA; |
| 211 | for (xattrname = evm_config_xattrnames; *xattrname != NULL; xattrname++) { |
Matthew Garrett | 50b9774 | 2017-11-07 07:17:42 -0800 | [diff] [blame] | 212 | bool is_ima = false; |
| 213 | |
| 214 | if (strcmp(*xattrname, XATTR_NAME_IMA) == 0) |
| 215 | is_ima = true; |
| 216 | |
Mimi Zohar | 66dbc325 | 2011-03-15 16:12:09 -0400 | [diff] [blame] | 217 | if ((req_xattr_name && req_xattr_value) |
| 218 | && !strcmp(*xattrname, req_xattr_name)) { |
| 219 | error = 0; |
Dmitry Kasatkin | d46eb36 | 2011-03-09 15:07:36 -0500 | [diff] [blame] | 220 | crypto_shash_update(desc, (const u8 *)req_xattr_value, |
| 221 | req_xattr_value_len); |
Matthew Garrett | 50b9774 | 2017-11-07 07:17:42 -0800 | [diff] [blame] | 222 | if (is_ima) |
| 223 | ima_present = true; |
Mimi Zohar | 66dbc325 | 2011-03-15 16:12:09 -0400 | [diff] [blame] | 224 | continue; |
| 225 | } |
| 226 | size = vfs_getxattr_alloc(dentry, *xattrname, |
| 227 | &xattr_value, xattr_size, GFP_NOFS); |
| 228 | if (size == -ENOMEM) { |
| 229 | error = -ENOMEM; |
| 230 | goto out; |
| 231 | } |
| 232 | if (size < 0) |
| 233 | continue; |
| 234 | |
| 235 | error = 0; |
| 236 | xattr_size = size; |
Dmitry Kasatkin | d46eb36 | 2011-03-09 15:07:36 -0500 | [diff] [blame] | 237 | crypto_shash_update(desc, (const u8 *)xattr_value, xattr_size); |
Matthew Garrett | 50b9774 | 2017-11-07 07:17:42 -0800 | [diff] [blame] | 238 | if (is_ima) |
| 239 | ima_present = true; |
Mimi Zohar | 66dbc325 | 2011-03-15 16:12:09 -0400 | [diff] [blame] | 240 | } |
Matthew Garrett | 50b9774 | 2017-11-07 07:17:42 -0800 | [diff] [blame] | 241 | hmac_add_misc(desc, inode, type, digest); |
Dmitry Kasatkin | d46eb36 | 2011-03-09 15:07:36 -0500 | [diff] [blame] | 242 | |
Matthew Garrett | 50b9774 | 2017-11-07 07:17:42 -0800 | [diff] [blame] | 243 | /* Portable EVM signatures must include an IMA hash */ |
| 244 | if (type == EVM_XATTR_PORTABLE_DIGSIG && !ima_present) |
| 245 | return -EPERM; |
Mimi Zohar | 66dbc325 | 2011-03-15 16:12:09 -0400 | [diff] [blame] | 246 | out: |
Dmitry Kasatkin | d46eb36 | 2011-03-09 15:07:36 -0500 | [diff] [blame] | 247 | kfree(xattr_value); |
| 248 | kfree(desc); |
Mimi Zohar | 66dbc325 | 2011-03-15 16:12:09 -0400 | [diff] [blame] | 249 | return error; |
| 250 | } |
| 251 | |
Dmitry Kasatkin | 15647eb | 2011-09-01 14:41:40 +0300 | [diff] [blame] | 252 | int evm_calc_hmac(struct dentry *dentry, const char *req_xattr_name, |
| 253 | const char *req_xattr_value, size_t req_xattr_value_len, |
| 254 | char *digest) |
| 255 | { |
| 256 | return evm_calc_hmac_or_hash(dentry, req_xattr_name, req_xattr_value, |
Matthew Garrett | 50b9774 | 2017-11-07 07:17:42 -0800 | [diff] [blame] | 257 | req_xattr_value_len, EVM_XATTR_HMAC, digest); |
Dmitry Kasatkin | 15647eb | 2011-09-01 14:41:40 +0300 | [diff] [blame] | 258 | } |
| 259 | |
| 260 | int evm_calc_hash(struct dentry *dentry, const char *req_xattr_name, |
| 261 | const char *req_xattr_value, size_t req_xattr_value_len, |
Matthew Garrett | 50b9774 | 2017-11-07 07:17:42 -0800 | [diff] [blame] | 262 | char type, char *digest) |
Dmitry Kasatkin | 15647eb | 2011-09-01 14:41:40 +0300 | [diff] [blame] | 263 | { |
| 264 | return evm_calc_hmac_or_hash(dentry, req_xattr_name, req_xattr_value, |
Matthew Garrett | 50b9774 | 2017-11-07 07:17:42 -0800 | [diff] [blame] | 265 | req_xattr_value_len, type, digest); |
Dmitry Kasatkin | 15647eb | 2011-09-01 14:41:40 +0300 | [diff] [blame] | 266 | } |
| 267 | |
Matthew Garrett | 50b9774 | 2017-11-07 07:17:42 -0800 | [diff] [blame] | 268 | static int evm_is_immutable(struct dentry *dentry, struct inode *inode) |
| 269 | { |
| 270 | const struct evm_ima_xattr_data *xattr_data = NULL; |
| 271 | struct integrity_iint_cache *iint; |
| 272 | int rc = 0; |
| 273 | |
| 274 | iint = integrity_iint_find(inode); |
| 275 | if (iint && (iint->flags & EVM_IMMUTABLE_DIGSIG)) |
| 276 | return 1; |
| 277 | |
| 278 | /* Do this the hard way */ |
| 279 | rc = vfs_getxattr_alloc(dentry, XATTR_NAME_EVM, (char **)&xattr_data, 0, |
| 280 | GFP_NOFS); |
| 281 | if (rc <= 0) { |
| 282 | if (rc == -ENODATA) |
| 283 | return 0; |
| 284 | return rc; |
| 285 | } |
| 286 | if (xattr_data->type == EVM_XATTR_PORTABLE_DIGSIG) |
| 287 | rc = 1; |
| 288 | else |
| 289 | rc = 0; |
| 290 | |
| 291 | kfree(xattr_data); |
| 292 | return rc; |
| 293 | } |
| 294 | |
| 295 | |
Mimi Zohar | 66dbc325 | 2011-03-15 16:12:09 -0400 | [diff] [blame] | 296 | /* |
| 297 | * Calculate the hmac and update security.evm xattr |
| 298 | * |
| 299 | * Expects to be called with i_mutex locked. |
| 300 | */ |
| 301 | int evm_update_evmxattr(struct dentry *dentry, const char *xattr_name, |
| 302 | const char *xattr_value, size_t xattr_value_len) |
| 303 | { |
David Howells | c6f493d | 2015-03-17 22:26:22 +0000 | [diff] [blame] | 304 | struct inode *inode = d_backing_inode(dentry); |
Dmitry Kasatkin | 6be5cc5 | 2011-03-09 14:28:20 -0500 | [diff] [blame] | 305 | struct evm_ima_xattr_data xattr_data; |
Mimi Zohar | 66dbc325 | 2011-03-15 16:12:09 -0400 | [diff] [blame] | 306 | int rc = 0; |
| 307 | |
Matthew Garrett | 50b9774 | 2017-11-07 07:17:42 -0800 | [diff] [blame] | 308 | /* |
| 309 | * Don't permit any transformation of the EVM xattr if the signature |
| 310 | * is of an immutable type |
| 311 | */ |
| 312 | rc = evm_is_immutable(dentry, inode); |
| 313 | if (rc < 0) |
| 314 | return rc; |
| 315 | if (rc) |
| 316 | return -EPERM; |
| 317 | |
Mimi Zohar | 66dbc325 | 2011-03-15 16:12:09 -0400 | [diff] [blame] | 318 | rc = evm_calc_hmac(dentry, xattr_name, xattr_value, |
Dmitry Kasatkin | 6be5cc5 | 2011-03-09 14:28:20 -0500 | [diff] [blame] | 319 | xattr_value_len, xattr_data.digest); |
| 320 | if (rc == 0) { |
| 321 | xattr_data.type = EVM_XATTR_HMAC; |
Mimi Zohar | 66dbc325 | 2011-03-15 16:12:09 -0400 | [diff] [blame] | 322 | rc = __vfs_setxattr_noperm(dentry, XATTR_NAME_EVM, |
Dmitry Kasatkin | 6be5cc5 | 2011-03-09 14:28:20 -0500 | [diff] [blame] | 323 | &xattr_data, |
| 324 | sizeof(xattr_data), 0); |
Andreas Gruenbacher | 5d6c319 | 2016-09-29 17:48:42 +0200 | [diff] [blame] | 325 | } else if (rc == -ENODATA && (inode->i_opflags & IOP_XATTR)) { |
| 326 | rc = __vfs_removexattr(dentry, XATTR_NAME_EVM); |
Dmitry Kasatkin | a67adb9 | 2013-01-18 23:56:39 +0200 | [diff] [blame] | 327 | } |
Mimi Zohar | 66dbc325 | 2011-03-15 16:12:09 -0400 | [diff] [blame] | 328 | return rc; |
| 329 | } |
| 330 | |
Mimi Zohar | cb723180 | 2011-03-09 14:40:44 -0500 | [diff] [blame] | 331 | int evm_init_hmac(struct inode *inode, const struct xattr *lsm_xattr, |
| 332 | char *hmac_val) |
| 333 | { |
Dmitry Kasatkin | d46eb36 | 2011-03-09 15:07:36 -0500 | [diff] [blame] | 334 | struct shash_desc *desc; |
Mimi Zohar | cb723180 | 2011-03-09 14:40:44 -0500 | [diff] [blame] | 335 | |
Dmitry Kasatkin | 15647eb | 2011-09-01 14:41:40 +0300 | [diff] [blame] | 336 | desc = init_desc(EVM_XATTR_HMAC); |
Dmitry Kasatkin | d46eb36 | 2011-03-09 15:07:36 -0500 | [diff] [blame] | 337 | if (IS_ERR(desc)) { |
Joe Perches | 20ee451 | 2014-02-24 13:59:56 -0800 | [diff] [blame] | 338 | pr_info("init_desc failed\n"); |
Dmitry Kasatkin | d46eb36 | 2011-03-09 15:07:36 -0500 | [diff] [blame] | 339 | return PTR_ERR(desc); |
Mimi Zohar | cb723180 | 2011-03-09 14:40:44 -0500 | [diff] [blame] | 340 | } |
| 341 | |
Dmitry Kasatkin | d46eb36 | 2011-03-09 15:07:36 -0500 | [diff] [blame] | 342 | crypto_shash_update(desc, lsm_xattr->value, lsm_xattr->value_len); |
Matthew Garrett | 50b9774 | 2017-11-07 07:17:42 -0800 | [diff] [blame] | 343 | hmac_add_misc(desc, inode, EVM_XATTR_HMAC, hmac_val); |
Dmitry Kasatkin | d46eb36 | 2011-03-09 15:07:36 -0500 | [diff] [blame] | 344 | kfree(desc); |
Mimi Zohar | cb723180 | 2011-03-09 14:40:44 -0500 | [diff] [blame] | 345 | return 0; |
| 346 | } |
| 347 | |
Mimi Zohar | 66dbc325 | 2011-03-15 16:12:09 -0400 | [diff] [blame] | 348 | /* |
| 349 | * Get the key from the TPM for the SHA1-HMAC |
| 350 | */ |
| 351 | int evm_init_key(void) |
| 352 | { |
| 353 | struct key *evm_key; |
| 354 | struct encrypted_key_payload *ekp; |
Dmitry Kasatkin | 7626676 | 2015-10-22 21:26:32 +0300 | [diff] [blame] | 355 | int rc; |
Mimi Zohar | 66dbc325 | 2011-03-15 16:12:09 -0400 | [diff] [blame] | 356 | |
| 357 | evm_key = request_key(&key_type_encrypted, EVMKEY, NULL); |
| 358 | if (IS_ERR(evm_key)) |
| 359 | return -ENOENT; |
| 360 | |
| 361 | down_read(&evm_key->sem); |
David Howells | 146aa8b | 2015-10-21 14:04:48 +0100 | [diff] [blame] | 362 | ekp = evm_key->payload.data[0]; |
Dmitry Kasatkin | 7626676 | 2015-10-22 21:26:32 +0300 | [diff] [blame] | 363 | |
| 364 | rc = evm_set_key(ekp->decrypted_data, ekp->decrypted_datalen); |
| 365 | |
Mimi Zohar | 66dbc325 | 2011-03-15 16:12:09 -0400 | [diff] [blame] | 366 | /* burn the original key contents */ |
| 367 | memset(ekp->decrypted_data, 0, ekp->decrypted_datalen); |
| 368 | up_read(&evm_key->sem); |
| 369 | key_put(evm_key); |
| 370 | return rc; |
| 371 | } |