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>
22 #include <crypto/hash.h>
27 char *evm_hmac = "hmac(sha1)";
29 char *evm_config_xattrnames[] = {
30 #ifdef CONFIG_SECURITY_SELINUX
33 #ifdef CONFIG_SECURITY_SMACK
41 * evm_verify_hmac - calculate and compare the HMAC with the EVM xattr
43 * Compute the HMAC on the dentry's protected set of extended attributes
44 * and compare it against the stored security.evm xattr. (For performance,
45 * use the previoulsy retrieved xattr value and length to calculate the
48 * Returns integrity status
50 static enum integrity_status evm_verify_hmac(struct dentry *dentry,
51 const char *xattr_name,
53 size_t xattr_value_len,
54 struct integrity_iint_cache *iint)
56 struct evm_ima_xattr_data xattr_data;
59 if (iint->hmac_status != INTEGRITY_UNKNOWN)
60 return iint->hmac_status;
62 rc = evm_calc_hmac(dentry, xattr_name, xattr_value,
63 xattr_value_len, xattr_data.digest);
65 return INTEGRITY_UNKNOWN;
67 xattr_data.type = EVM_XATTR_HMAC;
68 rc = vfs_xattr_cmp(dentry, XATTR_NAME_EVM, (u8 *)&xattr_data,
69 sizeof xattr_data, GFP_NOFS);
72 iint->hmac_status = INTEGRITY_PASS;
73 return iint->hmac_status;
77 case -ENODATA: /* file not labelled */
78 iint->hmac_status = INTEGRITY_NOLABEL;
81 iint->hmac_status = INTEGRITY_FAIL;
84 iint->hmac_status = INTEGRITY_UNKNOWN;
86 return iint->hmac_status;
89 static int evm_protected_xattr(const char *req_xattr_name)
95 namelen = strlen(req_xattr_name);
96 for (xattrname = evm_config_xattrnames; *xattrname != NULL; xattrname++) {
97 if ((strlen(*xattrname) == namelen)
98 && (strncmp(req_xattr_name, *xattrname, namelen) == 0)) {
102 if (strncmp(req_xattr_name,
103 *xattrname + XATTR_SECURITY_PREFIX_LEN,
104 strlen(req_xattr_name)) == 0) {
113 * evm_verifyxattr - verify the integrity of the requested xattr
114 * @dentry: object of the verify xattr
115 * @xattr_name: requested xattr
116 * @xattr_value: requested xattr value
117 * @xattr_value_len: requested xattr value length
119 * Calculate the HMAC for the given dentry and verify it against the stored
120 * security.evm xattr. For performance, use the xattr value and length
121 * previously retrieved to calculate the HMAC.
123 * Returns the xattr integrity status.
125 * This function requires the caller to lock the inode's i_mutex before it
128 enum integrity_status evm_verifyxattr(struct dentry *dentry,
129 const char *xattr_name,
130 void *xattr_value, size_t xattr_value_len,
131 struct integrity_iint_cache *iint)
133 if (!evm_initialized || !evm_protected_xattr(xattr_name))
134 return INTEGRITY_UNKNOWN;
137 iint = integrity_iint_find(dentry->d_inode);
139 return INTEGRITY_UNKNOWN;
141 return evm_verify_hmac(dentry, xattr_name, xattr_value,
142 xattr_value_len, iint);
144 EXPORT_SYMBOL_GPL(evm_verifyxattr);
147 * evm_protect_xattr - protect the EVM extended attribute
149 * Prevent security.evm from being modified or removed.
151 static int evm_protect_xattr(struct dentry *dentry, const char *xattr_name,
152 const void *xattr_value, size_t xattr_value_len)
154 if (strcmp(xattr_name, XATTR_NAME_EVM) == 0) {
155 if (!capable(CAP_SYS_ADMIN))
162 * evm_inode_setxattr - protect the EVM extended attribute
163 * @dentry: pointer to the affected dentry
164 * @xattr_name: pointer to the affected extended attribute name
165 * @xattr_value: pointer to the new extended attribute value
166 * @xattr_value_len: pointer to the new extended attribute value length
168 * Prevent 'security.evm' from being modified
170 int evm_inode_setxattr(struct dentry *dentry, const char *xattr_name,
171 const void *xattr_value, size_t xattr_value_len)
173 return evm_protect_xattr(dentry, xattr_name, xattr_value,
178 * evm_inode_removexattr - protect the EVM extended attribute
179 * @dentry: pointer to the affected dentry
180 * @xattr_name: pointer to the affected extended attribute name
182 * Prevent 'security.evm' from being removed.
184 int evm_inode_removexattr(struct dentry *dentry, const char *xattr_name)
186 return evm_protect_xattr(dentry, xattr_name, NULL, 0);
190 * evm_inode_post_setxattr - update 'security.evm' to reflect the changes
191 * @dentry: pointer to the affected dentry
192 * @xattr_name: pointer to the affected extended attribute name
193 * @xattr_value: pointer to the new extended attribute value
194 * @xattr_value_len: pointer to the new extended attribute value length
196 * Update the HMAC stored in 'security.evm' to reflect the change.
198 * No need to take the i_mutex lock here, as this function is called from
199 * __vfs_setxattr_noperm(). The caller of which has taken the inode's
202 void evm_inode_post_setxattr(struct dentry *dentry, const char *xattr_name,
203 const void *xattr_value, size_t xattr_value_len)
205 if (!evm_initialized || !evm_protected_xattr(xattr_name))
208 evm_update_evmxattr(dentry, xattr_name, xattr_value, xattr_value_len);
213 * evm_inode_post_removexattr - update 'security.evm' after removing the xattr
214 * @dentry: pointer to the affected dentry
215 * @xattr_name: pointer to the affected extended attribute name
217 * Update the HMAC stored in 'security.evm' to reflect removal of the xattr.
219 void evm_inode_post_removexattr(struct dentry *dentry, const char *xattr_name)
221 struct inode *inode = dentry->d_inode;
223 if (!evm_initialized || !evm_protected_xattr(xattr_name))
226 mutex_lock(&inode->i_mutex);
227 evm_update_evmxattr(dentry, xattr_name, NULL, 0);
228 mutex_unlock(&inode->i_mutex);
233 * evm_inode_post_setattr - update 'security.evm' after modifying metadata
234 * @dentry: pointer to the affected dentry
235 * @ia_valid: for the UID and GID status
237 * For now, update the HMAC stored in 'security.evm' to reflect UID/GID
240 * This function is called from notify_change(), which expects the caller
241 * to lock the inode's i_mutex.
243 void evm_inode_post_setattr(struct dentry *dentry, int ia_valid)
245 if (!evm_initialized)
248 if (ia_valid & (ATTR_MODE | ATTR_UID | ATTR_GID))
249 evm_update_evmxattr(dentry, NULL, NULL, 0);
254 * evm_inode_init_security - initializes security.evm
256 int evm_inode_init_security(struct inode *inode,
257 const struct xattr *lsm_xattr,
258 struct xattr *evm_xattr)
260 struct evm_ima_xattr_data *xattr_data;
263 if (!evm_initialized || !evm_protected_xattr(lsm_xattr->name))
266 xattr_data = kzalloc(sizeof(*xattr_data), GFP_NOFS);
270 xattr_data->type = EVM_XATTR_HMAC;
271 rc = evm_init_hmac(inode, lsm_xattr, xattr_data->digest);
275 evm_xattr->value = xattr_data;
276 evm_xattr->value_len = sizeof(*xattr_data);
277 evm_xattr->name = kstrdup(XATTR_EVM_SUFFIX, GFP_NOFS);
283 EXPORT_SYMBOL_GPL(evm_inode_init_security);
285 static int __init init_evm(void)
289 error = evm_init_secfs();
291 printk(KERN_INFO "EVM: Error registering secfs\n");
298 static void __exit cleanup_evm(void)
302 crypto_free_shash(hmac_tfm);
306 * evm_display_config - list the EVM protected security extended attributes
308 static int __init evm_display_config(void)
312 for (xattrname = evm_config_xattrnames; *xattrname != NULL; xattrname++)
313 printk(KERN_INFO "EVM: %s\n", *xattrname);
317 pure_initcall(evm_display_config);
318 late_initcall(init_evm);
320 MODULE_DESCRIPTION("Extended Verification Module");
321 MODULE_LICENSE("GPL");