blob: d80038bdaa5b484093f983937ec22acac748bb35 [file] [log] [blame]
Mimi Zohar66dbc3252011-03-15 16:12:09 -04001/*
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 Perches20ee4512014-02-24 13:59:56 -080016#define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
17
Mimi Zohar66dbc3252011-03-15 16:12:09 -040018#include <linux/module.h>
19#include <linux/crypto.h>
20#include <linux/xattr.h>
Dmitry Kasatkin76266762015-10-22 21:26:32 +030021#include <linux/evm.h>
Mimi Zohar66dbc3252011-03-15 16:12:09 -040022#include <keys/encrypted-type.h>
Dmitry Kasatkind46eb362011-03-09 15:07:36 -050023#include <crypto/hash.h>
Mimi Zohar66dbc3252011-03-15 16:12:09 -040024#include "evm.h"
25
26#define EVMKEY "evm-key"
27#define MAX_KEY_SIZE 128
28static unsigned char evmkey[MAX_KEY_SIZE];
29static int evmkey_len = MAX_KEY_SIZE;
30
Dmitry Kasatkind46eb362011-03-09 15:07:36 -050031struct crypto_shash *hmac_tfm;
Dmitry Kasatkin15647eb2011-09-01 14:41:40 +030032struct crypto_shash *hash_tfm;
Dmitry Kasatkind46eb362011-03-09 15:07:36 -050033
Dmitry Kasatkin97426f92011-12-05 13:17:42 +020034static DEFINE_MUTEX(mutex);
35
Dmitry Kasatkin76266762015-10-22 21:26:32 +030036#define EVM_SET_KEY_BUSY 0
37
38static 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 */
52int 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;
66inval:
67 clear_bit(EVM_SET_KEY_BUSY, &evm_set_key_flags);
68busy:
69 pr_err("key initialization failed\n");
70 return rc;
71}
72EXPORT_SYMBOL_GPL(evm_set_key);
73
James Morris8fcc9952012-01-09 12:16:48 +110074static struct shash_desc *init_desc(char type)
Mimi Zohar66dbc3252011-03-15 16:12:09 -040075{
Dmitry Kasatkin143b01d2011-12-05 13:17:42 +020076 long rc;
Dmitry Kasatkin15647eb2011-09-01 14:41:40 +030077 char *algo;
78 struct crypto_shash **tfm;
Dmitry Kasatkind46eb362011-03-09 15:07:36 -050079 struct shash_desc *desc;
Mimi Zohar66dbc3252011-03-15 16:12:09 -040080
Dmitry Kasatkin15647eb2011-09-01 14:41:40 +030081 if (type == EVM_XATTR_HMAC) {
Dmitry Kasatkin26ddabf2015-10-22 21:26:26 +030082 if (!(evm_initialized & EVM_INIT_HMAC)) {
83 pr_err("HMAC key is not set\n");
84 return ERR_PTR(-ENOKEY);
85 }
Dmitry Kasatkin15647eb2011-09-01 14:41:40 +030086 tfm = &hmac_tfm;
87 algo = evm_hmac;
88 } else {
89 tfm = &hash_tfm;
90 algo = evm_hash;
91 }
92
93 if (*tfm == NULL) {
Dmitry Kasatkin97426f92011-12-05 13:17:42 +020094 mutex_lock(&mutex);
Dmitry Kasatkin143b01d2011-12-05 13:17:42 +020095 if (*tfm)
Dmitry Kasatkin97426f92011-12-05 13:17:42 +020096 goto out;
Matthew Garretteddbab12018-06-08 14:57:42 -070097 *tfm = crypto_alloc_shash(algo, 0,
98 CRYPTO_ALG_ASYNC | CRYPTO_NOLOAD);
Dmitry Kasatkin15647eb2011-09-01 14:41:40 +030099 if (IS_ERR(*tfm)) {
Dmitry Kasatkin15647eb2011-09-01 14:41:40 +0300100 rc = PTR_ERR(*tfm);
Dmitry Kasatkin143b01d2011-12-05 13:17:42 +0200101 pr_err("Can not allocate %s (reason: %ld)\n", algo, rc);
Dmitry Kasatkin15647eb2011-09-01 14:41:40 +0300102 *tfm = NULL;
Dmitry Kasatkin97426f92011-12-05 13:17:42 +0200103 mutex_unlock(&mutex);
Dmitry Kasatkind46eb362011-03-09 15:07:36 -0500104 return ERR_PTR(rc);
105 }
Dmitry Kasatkin88d7ed32011-12-05 13:17:41 +0200106 if (type == EVM_XATTR_HMAC) {
107 rc = crypto_shash_setkey(*tfm, evmkey, evmkey_len);
108 if (rc) {
109 crypto_free_shash(*tfm);
110 *tfm = NULL;
Dmitry Kasatkin143b01d2011-12-05 13:17:42 +0200111 mutex_unlock(&mutex);
Dmitry Kasatkin88d7ed32011-12-05 13:17:41 +0200112 return ERR_PTR(rc);
113 }
Dmitry Kasatkind21b5942011-12-05 13:17:41 +0200114 }
Dmitry Kasatkin97426f92011-12-05 13:17:42 +0200115out:
116 mutex_unlock(&mutex);
Mimi Zohar66dbc3252011-03-15 16:12:09 -0400117 }
Dmitry Kasatkind46eb362011-03-09 15:07:36 -0500118
Dmitry Kasatkin15647eb2011-09-01 14:41:40 +0300119 desc = kmalloc(sizeof(*desc) + crypto_shash_descsize(*tfm),
Dmitry Kasatkind46eb362011-03-09 15:07:36 -0500120 GFP_KERNEL);
121 if (!desc)
122 return ERR_PTR(-ENOMEM);
123
Dmitry Kasatkin15647eb2011-09-01 14:41:40 +0300124 desc->tfm = *tfm;
Dmitry Kasatkind46eb362011-03-09 15:07:36 -0500125 desc->flags = CRYPTO_TFM_REQ_MAY_SLEEP;
126
Dmitry Kasatkind46eb362011-03-09 15:07:36 -0500127 rc = crypto_shash_init(desc);
Dmitry Kasatkind46eb362011-03-09 15:07:36 -0500128 if (rc) {
129 kfree(desc);
130 return ERR_PTR(rc);
131 }
132 return desc;
Mimi Zohar66dbc3252011-03-15 16:12:09 -0400133}
134
135/* Protect against 'cutting & pasting' security.evm xattr, include inode
136 * specific info.
137 *
138 * (Additional directory/file metadata needs to be added for more complete
139 * protection.)
140 */
Dmitry Kasatkind46eb362011-03-09 15:07:36 -0500141static void hmac_add_misc(struct shash_desc *desc, struct inode *inode,
Mimi Zohar66dbc3252011-03-15 16:12:09 -0400142 char *digest)
143{
144 struct h_misc {
145 unsigned long ino;
146 __u32 generation;
147 uid_t uid;
148 gid_t gid;
149 umode_t mode;
150 } hmac_misc;
Mimi Zohar66dbc3252011-03-15 16:12:09 -0400151
Dmitry Kasatkin2bb930a2014-03-04 18:04:20 +0200152 memset(&hmac_misc, 0, sizeof(hmac_misc));
Mimi Zohar66dbc3252011-03-15 16:12:09 -0400153 hmac_misc.ino = inode->i_ino;
154 hmac_misc.generation = inode->i_generation;
Eric W. Biederman5fed1ff2016-12-02 09:35:31 -0600155 /* The hmac uid and gid must be encoded in the initial user
156 * namespace (not the filesystems user namespace) as encoding
157 * them in the filesystems user namespace allows an attack
158 * where first they are written in an unprivileged fuse mount
159 * of a filesystem and then the system is tricked to mount the
160 * filesystem for real on next boot and trust it because
161 * everything is signed.
162 */
163 hmac_misc.uid = from_kuid(&init_user_ns, inode->i_uid);
164 hmac_misc.gid = from_kgid(&init_user_ns, inode->i_gid);
Mimi Zohar66dbc3252011-03-15 16:12:09 -0400165 hmac_misc.mode = inode->i_mode;
Dmitry Kasatkin2bb930a2014-03-04 18:04:20 +0200166 crypto_shash_update(desc, (const u8 *)&hmac_misc, sizeof(hmac_misc));
Dmitry Kasatkind3b33672014-03-28 14:31:04 +0200167 if (evm_hmac_attrs & EVM_ATTR_FSUUID)
Dmitry Kasatkin74de6682012-09-10 10:37:20 +0300168 crypto_shash_update(desc, inode->i_sb->s_uuid,
169 sizeof(inode->i_sb->s_uuid));
Dmitry Kasatkind46eb362011-03-09 15:07:36 -0500170 crypto_shash_final(desc, digest);
Mimi Zohar66dbc3252011-03-15 16:12:09 -0400171}
172
173/*
174 * Calculate the HMAC value across the set of protected security xattrs.
175 *
176 * Instead of retrieving the requested xattr, for performance, calculate
177 * the hmac using the requested xattr value. Don't alloc/free memory for
178 * each xattr, but attempt to re-use the previously allocated memory.
179 */
Dmitry Kasatkin15647eb2011-09-01 14:41:40 +0300180static int evm_calc_hmac_or_hash(struct dentry *dentry,
181 const char *req_xattr_name,
182 const char *req_xattr_value,
183 size_t req_xattr_value_len,
184 char type, char *digest)
Mimi Zohar66dbc3252011-03-15 16:12:09 -0400185{
David Howellsc6f493d2015-03-17 22:26:22 +0000186 struct inode *inode = d_backing_inode(dentry);
Dmitry Kasatkind46eb362011-03-09 15:07:36 -0500187 struct shash_desc *desc;
Mimi Zohar66dbc3252011-03-15 16:12:09 -0400188 char **xattrname;
189 size_t xattr_size = 0;
190 char *xattr_value = NULL;
191 int error;
192 int size;
193
Andreas Gruenbacher5d6c3192016-09-29 17:48:42 +0200194 if (!(inode->i_opflags & IOP_XATTR))
Mimi Zohar66dbc3252011-03-15 16:12:09 -0400195 return -EOPNOTSUPP;
Andreas Gruenbacher5d6c3192016-09-29 17:48:42 +0200196
Dmitry Kasatkin15647eb2011-09-01 14:41:40 +0300197 desc = init_desc(type);
Dmitry Kasatkind46eb362011-03-09 15:07:36 -0500198 if (IS_ERR(desc))
199 return PTR_ERR(desc);
Mimi Zohar66dbc3252011-03-15 16:12:09 -0400200
201 error = -ENODATA;
202 for (xattrname = evm_config_xattrnames; *xattrname != NULL; xattrname++) {
203 if ((req_xattr_name && req_xattr_value)
204 && !strcmp(*xattrname, req_xattr_name)) {
205 error = 0;
Dmitry Kasatkind46eb362011-03-09 15:07:36 -0500206 crypto_shash_update(desc, (const u8 *)req_xattr_value,
207 req_xattr_value_len);
Mimi Zohar66dbc3252011-03-15 16:12:09 -0400208 continue;
209 }
210 size = vfs_getxattr_alloc(dentry, *xattrname,
211 &xattr_value, xattr_size, GFP_NOFS);
212 if (size == -ENOMEM) {
213 error = -ENOMEM;
214 goto out;
215 }
216 if (size < 0)
217 continue;
218
219 error = 0;
220 xattr_size = size;
Dmitry Kasatkind46eb362011-03-09 15:07:36 -0500221 crypto_shash_update(desc, (const u8 *)xattr_value, xattr_size);
Mimi Zohar66dbc3252011-03-15 16:12:09 -0400222 }
Dmitry Kasatkind46eb362011-03-09 15:07:36 -0500223 hmac_add_misc(desc, inode, digest);
224
Mimi Zohar66dbc3252011-03-15 16:12:09 -0400225out:
Dmitry Kasatkind46eb362011-03-09 15:07:36 -0500226 kfree(xattr_value);
227 kfree(desc);
Mimi Zohar66dbc3252011-03-15 16:12:09 -0400228 return error;
229}
230
Dmitry Kasatkin15647eb2011-09-01 14:41:40 +0300231int evm_calc_hmac(struct dentry *dentry, const char *req_xattr_name,
232 const char *req_xattr_value, size_t req_xattr_value_len,
233 char *digest)
234{
235 return evm_calc_hmac_or_hash(dentry, req_xattr_name, req_xattr_value,
236 req_xattr_value_len, EVM_XATTR_HMAC, digest);
237}
238
239int evm_calc_hash(struct dentry *dentry, const char *req_xattr_name,
240 const char *req_xattr_value, size_t req_xattr_value_len,
241 char *digest)
242{
243 return evm_calc_hmac_or_hash(dentry, req_xattr_name, req_xattr_value,
244 req_xattr_value_len, IMA_XATTR_DIGEST, digest);
245}
246
Mimi Zohar66dbc3252011-03-15 16:12:09 -0400247/*
248 * Calculate the hmac and update security.evm xattr
249 *
250 * Expects to be called with i_mutex locked.
251 */
252int evm_update_evmxattr(struct dentry *dentry, const char *xattr_name,
253 const char *xattr_value, size_t xattr_value_len)
254{
David Howellsc6f493d2015-03-17 22:26:22 +0000255 struct inode *inode = d_backing_inode(dentry);
Dmitry Kasatkin6be5cc52011-03-09 14:28:20 -0500256 struct evm_ima_xattr_data xattr_data;
Mimi Zohar66dbc3252011-03-15 16:12:09 -0400257 int rc = 0;
258
259 rc = evm_calc_hmac(dentry, xattr_name, xattr_value,
Dmitry Kasatkin6be5cc52011-03-09 14:28:20 -0500260 xattr_value_len, xattr_data.digest);
261 if (rc == 0) {
262 xattr_data.type = EVM_XATTR_HMAC;
Mimi Zohar66dbc3252011-03-15 16:12:09 -0400263 rc = __vfs_setxattr_noperm(dentry, XATTR_NAME_EVM,
Dmitry Kasatkin6be5cc52011-03-09 14:28:20 -0500264 &xattr_data,
265 sizeof(xattr_data), 0);
Andreas Gruenbacher5d6c3192016-09-29 17:48:42 +0200266 } else if (rc == -ENODATA && (inode->i_opflags & IOP_XATTR)) {
267 rc = __vfs_removexattr(dentry, XATTR_NAME_EVM);
Dmitry Kasatkina67adb92013-01-18 23:56:39 +0200268 }
Mimi Zohar66dbc3252011-03-15 16:12:09 -0400269 return rc;
270}
271
Mimi Zoharcb723182011-03-09 14:40:44 -0500272int evm_init_hmac(struct inode *inode, const struct xattr *lsm_xattr,
273 char *hmac_val)
274{
Dmitry Kasatkind46eb362011-03-09 15:07:36 -0500275 struct shash_desc *desc;
Mimi Zoharcb723182011-03-09 14:40:44 -0500276
Dmitry Kasatkin15647eb2011-09-01 14:41:40 +0300277 desc = init_desc(EVM_XATTR_HMAC);
Dmitry Kasatkind46eb362011-03-09 15:07:36 -0500278 if (IS_ERR(desc)) {
Joe Perches20ee4512014-02-24 13:59:56 -0800279 pr_info("init_desc failed\n");
Dmitry Kasatkind46eb362011-03-09 15:07:36 -0500280 return PTR_ERR(desc);
Mimi Zoharcb723182011-03-09 14:40:44 -0500281 }
282
Dmitry Kasatkind46eb362011-03-09 15:07:36 -0500283 crypto_shash_update(desc, lsm_xattr->value, lsm_xattr->value_len);
284 hmac_add_misc(desc, inode, hmac_val);
285 kfree(desc);
Mimi Zoharcb723182011-03-09 14:40:44 -0500286 return 0;
287}
288
Mimi Zohar66dbc3252011-03-15 16:12:09 -0400289/*
290 * Get the key from the TPM for the SHA1-HMAC
291 */
292int evm_init_key(void)
293{
294 struct key *evm_key;
295 struct encrypted_key_payload *ekp;
Dmitry Kasatkin76266762015-10-22 21:26:32 +0300296 int rc;
Mimi Zohar66dbc3252011-03-15 16:12:09 -0400297
298 evm_key = request_key(&key_type_encrypted, EVMKEY, NULL);
299 if (IS_ERR(evm_key))
300 return -ENOENT;
301
302 down_read(&evm_key->sem);
David Howells146aa8b2015-10-21 14:04:48 +0100303 ekp = evm_key->payload.data[0];
Dmitry Kasatkin76266762015-10-22 21:26:32 +0300304
305 rc = evm_set_key(ekp->decrypted_data, ekp->decrypted_datalen);
306
Mimi Zohar66dbc3252011-03-15 16:12:09 -0400307 /* burn the original key contents */
308 memset(ekp->decrypted_data, 0, ekp->decrypted_datalen);
309 up_read(&evm_key->sem);
310 key_put(evm_key);
311 return rc;
312}