2 * Copyright (C) 2005-2010 IBM Corporation
5 * Mimi Zohar <zohar@us.ibm.com>
6 * Kylene Hall <kjhall@us.ibm.com>
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.
13 * implements evm_inode_setxattr, evm_inode_post_setxattr,
14 * evm_inode_removexattr, and evm_verifyxattr
17 #include <linux/module.h>
18 #include <linux/crypto.h>
19 #include <linux/xattr.h>
20 #include <linux/integrity.h>
21 #include <linux/evm.h>
26 char *evm_hmac = "hmac(sha1)";
28 char *evm_config_xattrnames[] = {
29 #ifdef CONFIG_SECURITY_SELINUX
32 #ifdef CONFIG_SECURITY_SMACK
40 * evm_verify_hmac - calculate and compare the HMAC with the EVM xattr
42 * Compute the HMAC on the dentry's protected set of extended attributes
43 * and compare it against the stored security.evm xattr. (For performance,
44 * use the previoulsy retrieved xattr value and length to calculate the
47 * Returns integrity status
49 static enum integrity_status evm_verify_hmac(struct dentry *dentry,
50 const char *xattr_name,
52 size_t xattr_value_len,
53 struct integrity_iint_cache *iint)
55 struct evm_ima_xattr_data xattr_data;
58 if (iint->hmac_status != INTEGRITY_UNKNOWN)
59 return iint->hmac_status;
61 rc = evm_calc_hmac(dentry, xattr_name, xattr_value,
62 xattr_value_len, xattr_data.digest);
64 return INTEGRITY_UNKNOWN;
66 xattr_data.type = EVM_XATTR_HMAC;
67 rc = vfs_xattr_cmp(dentry, XATTR_NAME_EVM, (u8 *)&xattr_data,
68 sizeof xattr_data, GFP_NOFS);
71 iint->hmac_status = INTEGRITY_PASS;
72 return iint->hmac_status;
76 case -ENODATA: /* file not labelled */
77 iint->hmac_status = INTEGRITY_NOLABEL;
80 iint->hmac_status = INTEGRITY_FAIL;
83 iint->hmac_status = INTEGRITY_UNKNOWN;
85 return iint->hmac_status;
88 static int evm_protected_xattr(const char *req_xattr_name)
94 namelen = strlen(req_xattr_name);
95 for (xattrname = evm_config_xattrnames; *xattrname != NULL; xattrname++) {
96 if ((strlen(*xattrname) == namelen)
97 && (strncmp(req_xattr_name, *xattrname, namelen) == 0)) {
101 if (strncmp(req_xattr_name,
102 *xattrname + XATTR_SECURITY_PREFIX_LEN,
103 strlen(req_xattr_name)) == 0) {
112 * evm_verifyxattr - verify the integrity of the requested xattr
113 * @dentry: object of the verify xattr
114 * @xattr_name: requested xattr
115 * @xattr_value: requested xattr value
116 * @xattr_value_len: requested xattr value length
118 * Calculate the HMAC for the given dentry and verify it against the stored
119 * security.evm xattr. For performance, use the xattr value and length
120 * previously retrieved to calculate the HMAC.
122 * Returns the xattr integrity status.
124 * This function requires the caller to lock the inode's i_mutex before it
127 enum integrity_status evm_verifyxattr(struct dentry *dentry,
128 const char *xattr_name,
129 void *xattr_value, size_t xattr_value_len)
131 struct inode *inode = dentry->d_inode;
132 struct integrity_iint_cache *iint;
133 enum integrity_status status;
135 if (!evm_initialized || !evm_protected_xattr(xattr_name))
136 return INTEGRITY_UNKNOWN;
138 iint = integrity_iint_find(inode);
140 return INTEGRITY_UNKNOWN;
141 status = evm_verify_hmac(dentry, xattr_name, xattr_value,
142 xattr_value_len, iint);
145 EXPORT_SYMBOL_GPL(evm_verifyxattr);
148 * evm_protect_xattr - protect the EVM extended attribute
150 * Prevent security.evm from being modified or removed.
152 static int evm_protect_xattr(struct dentry *dentry, const char *xattr_name,
153 const void *xattr_value, size_t xattr_value_len)
155 if (strcmp(xattr_name, XATTR_NAME_EVM) == 0) {
156 if (!capable(CAP_SYS_ADMIN))
163 * evm_inode_setxattr - protect the EVM extended attribute
164 * @dentry: pointer to the affected dentry
165 * @xattr_name: pointer to the affected extended attribute name
166 * @xattr_value: pointer to the new extended attribute value
167 * @xattr_value_len: pointer to the new extended attribute value length
169 * Prevent 'security.evm' from being modified
171 int evm_inode_setxattr(struct dentry *dentry, const char *xattr_name,
172 const void *xattr_value, size_t xattr_value_len)
174 return evm_protect_xattr(dentry, xattr_name, xattr_value,
179 * evm_inode_removexattr - protect the EVM extended attribute
180 * @dentry: pointer to the affected dentry
181 * @xattr_name: pointer to the affected extended attribute name
183 * Prevent 'security.evm' from being removed.
185 int evm_inode_removexattr(struct dentry *dentry, const char *xattr_name)
187 return evm_protect_xattr(dentry, xattr_name, NULL, 0);
191 * evm_inode_post_setxattr - update 'security.evm' to reflect the changes
192 * @dentry: pointer to the affected dentry
193 * @xattr_name: pointer to the affected extended attribute name
194 * @xattr_value: pointer to the new extended attribute value
195 * @xattr_value_len: pointer to the new extended attribute value length
197 * Update the HMAC stored in 'security.evm' to reflect the change.
199 * No need to take the i_mutex lock here, as this function is called from
200 * __vfs_setxattr_noperm(). The caller of which has taken the inode's
203 void evm_inode_post_setxattr(struct dentry *dentry, const char *xattr_name,
204 const void *xattr_value, size_t xattr_value_len)
206 if (!evm_initialized || !evm_protected_xattr(xattr_name))
209 evm_update_evmxattr(dentry, xattr_name, xattr_value, xattr_value_len);
214 * evm_inode_post_removexattr - update 'security.evm' after removing the xattr
215 * @dentry: pointer to the affected dentry
216 * @xattr_name: pointer to the affected extended attribute name
218 * Update the HMAC stored in 'security.evm' to reflect removal of the xattr.
220 void evm_inode_post_removexattr(struct dentry *dentry, const char *xattr_name)
222 struct inode *inode = dentry->d_inode;
224 if (!evm_initialized || !evm_protected_xattr(xattr_name))
227 mutex_lock(&inode->i_mutex);
228 evm_update_evmxattr(dentry, xattr_name, NULL, 0);
229 mutex_unlock(&inode->i_mutex);
234 * evm_inode_post_setattr - update 'security.evm' after modifying metadata
235 * @dentry: pointer to the affected dentry
236 * @ia_valid: for the UID and GID status
238 * For now, update the HMAC stored in 'security.evm' to reflect UID/GID
241 * This function is called from notify_change(), which expects the caller
242 * to lock the inode's i_mutex.
244 void evm_inode_post_setattr(struct dentry *dentry, int ia_valid)
246 if (!evm_initialized)
249 if (ia_valid & (ATTR_MODE | ATTR_UID | ATTR_GID))
250 evm_update_evmxattr(dentry, NULL, NULL, 0);
255 * evm_inode_init_security - initializes security.evm
257 int evm_inode_init_security(struct inode *inode,
258 const struct xattr *lsm_xattr,
259 struct xattr *evm_xattr)
261 struct evm_ima_xattr_data *xattr_data;
264 if (!evm_initialized || !evm_protected_xattr(lsm_xattr->name))
267 xattr_data = kzalloc(sizeof(*xattr_data), GFP_NOFS);
271 xattr_data->type = EVM_XATTR_HMAC;
272 rc = evm_init_hmac(inode, lsm_xattr, xattr_data->digest);
276 evm_xattr->value = xattr_data;
277 evm_xattr->value_len = sizeof(*xattr_data);
278 evm_xattr->name = kstrdup(XATTR_EVM_SUFFIX, GFP_NOFS);
284 EXPORT_SYMBOL_GPL(evm_inode_init_security);
286 static struct crypto_hash *tfm_hmac; /* preload crypto alg */
287 static int __init init_evm(void)
291 tfm_hmac = crypto_alloc_hash(evm_hmac, 0, CRYPTO_ALG_ASYNC);
292 error = evm_init_secfs();
294 printk(KERN_INFO "EVM: Error registering secfs\n");
301 static void __exit cleanup_evm(void)
304 crypto_free_hash(tfm_hmac);
308 * evm_display_config - list the EVM protected security extended attributes
310 static int __init evm_display_config(void)
314 for (xattrname = evm_config_xattrnames; *xattrname != NULL; xattrname++)
315 printk(KERN_INFO "EVM: %s\n", *xattrname);
319 pure_initcall(evm_display_config);
320 late_initcall(init_evm);
322 MODULE_DESCRIPTION("Extended Verification Module");
323 MODULE_LICENSE("GPL");