aboutsummaryrefslogtreecommitdiffstats
path: root/security/integrity/evm/evm_crypto.c
diff options
context:
space:
mode:
authorMimi Zohar <zohar@linux.vnet.ibm.com>2011-03-15 16:12:09 -0400
committerMimi Zohar <zohar@linux.vnet.ibm.com>2011-07-18 12:29:40 -0400
commit66dbc325afcef909043c30e90930a36823fc734c (patch)
tree5c8a7fe063a058f4266c6db5e48229e8c04dd00e /security/integrity/evm/evm_crypto.c
parent1601fbad2b14e0b8d4dbb55e749bfe31e972818a (diff)
evm: re-release
EVM protects a file's security extended attributes(xattrs) against integrity attacks. This patchset provides the framework and an initial method. The initial method maintains an HMAC-sha1 value across the security extended attributes, storing the HMAC value as the extended attribute 'security.evm'. Other methods of validating the integrity of a file's metadata will be posted separately (eg. EVM-digital-signatures). While this patchset does authenticate the security xattrs, and cryptographically binds them to the inode, coming extensions will bind other directory and inode metadata for more complete protection. To help simplify the review and upstreaming process, each extension will be posted separately (eg. IMA-appraisal, IMA-appraisal-directory). For a general overview of the proposed Linux integrity subsystem, refer to Dave Safford's whitepaper: http://downloads.sf.net/project/linux-ima/linux-ima/Integrity_overview.pdf. EVM depends on the Kernel Key Retention System to provide it with a trusted/encrypted key for the HMAC-sha1 operation. The key is loaded onto the root's keyring using keyctl. Until EVM receives notification that the key has been successfully loaded onto the keyring (echo 1 > <securityfs>/evm), EVM can not create or validate the 'security.evm' xattr, but returns INTEGRITY_UNKNOWN. Loading the key and signaling EVM should be done as early as possible. Normally this is done in the initramfs, which has already been measured as part of the trusted boot. For more information on creating and loading existing trusted/encrypted keys, refer to Documentation/keys-trusted-encrypted.txt. A sample dracut patch, which loads the trusted/encrypted key and enables EVM, is available from http://linux-ima.sourceforge.net/#EVM. Based on the LSMs enabled, the set of EVM protected security xattrs is defined at compile. EVM adds the following three calls to the existing security hooks: evm_inode_setxattr(), evm_inode_post_setxattr(), and evm_inode_removexattr. To initialize and update the 'security.evm' extended attribute, EVM defines three calls: evm_inode_post_init(), evm_inode_post_setattr() and evm_inode_post_removexattr() hooks. To verify the integrity of a security xattr, EVM exports evm_verifyxattr(). Changelog v7: - Fixed URL in EVM ABI documentation Changelog v6: (based on Serge Hallyn's review) - fix URL in patch description - remove evm_hmac_size definition - use SHA1_DIGEST_SIZE (removed both MAX_DIGEST_SIZE and evm_hmac_size) - moved linux include before other includes - test for crypto_hash_setkey failure - fail earlier for invalid key - clear entire encrypted key, even on failure - check xattr name length before comparing xattr names Changelog: - locking based on i_mutex, remove evm_mutex - using trusted/encrypted keys for storing the EVM key used in the HMAC-sha1 operation. - replaced crypto hash with shash (Dmitry Kasatkin) - support for additional methods of verifying the security xattrs (Dmitry Kasatkin) - iint not allocated for all regular files, but only for those appraised - Use cap_sys_admin in lieu of cap_mac_admin - Use __vfs_setxattr_noperm(), without permission checks, from EVM Signed-off-by: Mimi Zohar <zohar@us.ibm.com> Acked-by: Serge Hallyn <serge.hallyn@canonical.com>
Diffstat (limited to 'security/integrity/evm/evm_crypto.c')
-rw-r--r--security/integrity/evm/evm_crypto.c183
1 files changed, 183 insertions, 0 deletions
diff --git a/security/integrity/evm/evm_crypto.c b/security/integrity/evm/evm_crypto.c
new file mode 100644
index 000000000000..d49bb002f3da
--- /dev/null
+++ b/security/integrity/evm/evm_crypto.c
@@ -0,0 +1,183 @@
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
25static unsigned char evmkey[MAX_KEY_SIZE];
26static int evmkey_len = MAX_KEY_SIZE;
27
28static 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);
44out:
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 */
56static 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 */
86int 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);
130out:
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 */
140int 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;
144 u8 hmac[SHA1_DIGEST_SIZE];
145 int rc = 0;
146
147 rc = evm_calc_hmac(dentry, xattr_name, xattr_value,
148 xattr_value_len, hmac);
149 if (rc == 0)
150 rc = __vfs_setxattr_noperm(dentry, XATTR_NAME_EVM,
151 hmac, SHA1_DIGEST_SIZE, 0);
152 else if (rc == -ENODATA)
153 rc = inode->i_op->removexattr(dentry, XATTR_NAME_EVM);
154 return rc;
155}
156
157/*
158 * Get the key from the TPM for the SHA1-HMAC
159 */
160int evm_init_key(void)
161{
162 struct key *evm_key;
163 struct encrypted_key_payload *ekp;
164 int rc = 0;
165
166 evm_key = request_key(&key_type_encrypted, EVMKEY, NULL);
167 if (IS_ERR(evm_key))
168 return -ENOENT;
169
170 down_read(&evm_key->sem);
171 ekp = evm_key->payload.data;
172 if (ekp->decrypted_datalen > MAX_KEY_SIZE) {
173 rc = -EINVAL;
174 goto out;
175 }
176 memcpy(evmkey, ekp->decrypted_data, ekp->decrypted_datalen);
177out:
178 /* burn the original key contents */
179 memset(ekp->decrypted_data, 0, ekp->decrypted_datalen);
180 up_read(&evm_key->sem);
181 key_put(evm_key);
182 return rc;
183}