Linux 3.16.75
[linux/fpc-iii.git] / security / integrity / evm / evm_main.c
blobc9db5ab63ad2ac2437f518eb0d3132c80855665d
1 /*
2 * Copyright (C) 2005-2010 IBM Corporation
4 * Author:
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.
12 * File: evm_main.c
13 * implements evm_inode_setxattr, evm_inode_post_setxattr,
14 * evm_inode_removexattr, and evm_verifyxattr
17 #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
19 #include <linux/module.h>
20 #include <linux/crypto.h>
21 #include <linux/audit.h>
22 #include <linux/xattr.h>
23 #include <linux/integrity.h>
24 #include <linux/evm.h>
25 #include <linux/magic.h>
26 #include <crypto/hash.h>
27 #include <crypto/algapi.h>
28 #include "evm.h"
30 int evm_initialized;
32 static char *integrity_status_msg[] = {
33 "pass", "fail", "no_label", "no_xattrs", "unknown"
35 char *evm_hmac = "hmac(sha1)";
36 char *evm_hash = "sha1";
37 int evm_hmac_attrs;
39 char *evm_config_xattrnames[] = {
40 #ifdef CONFIG_SECURITY_SELINUX
41 XATTR_NAME_SELINUX,
42 #endif
43 #ifdef CONFIG_SECURITY_SMACK
44 XATTR_NAME_SMACK,
45 #ifdef CONFIG_EVM_EXTRA_SMACK_XATTRS
46 XATTR_NAME_SMACKEXEC,
47 XATTR_NAME_SMACKTRANSMUTE,
48 XATTR_NAME_SMACKMMAP,
49 #endif
50 #endif
51 #ifdef CONFIG_IMA_APPRAISE
52 XATTR_NAME_IMA,
53 #endif
54 XATTR_NAME_CAPS,
55 NULL
58 static int evm_fixmode;
59 static int __init evm_set_fixmode(char *str)
61 if (strncmp(str, "fix", 3) == 0)
62 evm_fixmode = 1;
63 return 0;
65 __setup("evm=", evm_set_fixmode);
67 static void __init evm_init_config(void)
69 #ifdef CONFIG_EVM_ATTR_FSUUID
70 evm_hmac_attrs |= EVM_ATTR_FSUUID;
71 #endif
72 pr_info("HMAC attrs: 0x%x\n", evm_hmac_attrs);
75 static int evm_find_protected_xattrs(struct dentry *dentry)
77 struct inode *inode = dentry->d_inode;
78 char **xattr;
79 int error;
80 int count = 0;
82 if (!inode->i_op->getxattr)
83 return -EOPNOTSUPP;
85 for (xattr = evm_config_xattrnames; *xattr != NULL; xattr++) {
86 error = inode->i_op->getxattr(dentry, *xattr, NULL, 0);
87 if (error < 0) {
88 if (error == -ENODATA)
89 continue;
90 return error;
92 count++;
95 return count;
99 * evm_verify_hmac - calculate and compare the HMAC with the EVM xattr
101 * Compute the HMAC on the dentry's protected set of extended attributes
102 * and compare it against the stored security.evm xattr.
104 * For performance:
105 * - use the previoulsy retrieved xattr value and length to calculate the
106 * HMAC.)
107 * - cache the verification result in the iint, when available.
109 * Returns integrity status
111 static enum integrity_status evm_verify_hmac(struct dentry *dentry,
112 const char *xattr_name,
113 char *xattr_value,
114 size_t xattr_value_len,
115 struct integrity_iint_cache *iint)
117 struct evm_ima_xattr_data *xattr_data = NULL;
118 struct evm_ima_xattr_data calc;
119 enum integrity_status evm_status = INTEGRITY_PASS;
120 int rc, xattr_len;
122 if (iint && iint->evm_status == INTEGRITY_PASS)
123 return iint->evm_status;
125 /* if status is not PASS, try to check again - against -ENOMEM */
127 /* first need to know the sig type */
128 rc = vfs_getxattr_alloc(dentry, XATTR_NAME_EVM, (char **)&xattr_data, 0,
129 GFP_NOFS);
130 if (rc <= 0) {
131 if (rc == 0)
132 evm_status = INTEGRITY_FAIL; /* empty */
133 else if (rc == -ENODATA) {
134 rc = evm_find_protected_xattrs(dentry);
135 if (rc > 0)
136 evm_status = INTEGRITY_NOLABEL;
137 else if (rc == 0)
138 evm_status = INTEGRITY_NOXATTRS; /* new file */
140 goto out;
143 xattr_len = rc;
145 /* check value type */
146 switch (xattr_data->type) {
147 case EVM_XATTR_HMAC:
148 rc = evm_calc_hmac(dentry, xattr_name, xattr_value,
149 xattr_value_len, calc.digest);
150 if (rc)
151 break;
152 rc = crypto_memneq(xattr_data->digest, calc.digest,
153 sizeof(calc.digest));
154 if (rc)
155 rc = -EINVAL;
156 break;
157 case EVM_IMA_XATTR_DIGSIG:
158 rc = evm_calc_hash(dentry, xattr_name, xattr_value,
159 xattr_value_len, calc.digest);
160 if (rc)
161 break;
162 rc = integrity_digsig_verify(INTEGRITY_KEYRING_EVM,
163 (const char *)xattr_data, xattr_len,
164 calc.digest, sizeof(calc.digest));
165 if (!rc) {
166 /* we probably want to replace rsa with hmac here */
167 evm_update_evmxattr(dentry, xattr_name, xattr_value,
168 xattr_value_len);
170 break;
171 default:
172 rc = -EINVAL;
173 break;
176 if (rc)
177 evm_status = (rc == -ENODATA) ?
178 INTEGRITY_NOXATTRS : INTEGRITY_FAIL;
179 out:
180 if (iint)
181 iint->evm_status = evm_status;
182 kfree(xattr_data);
183 return evm_status;
186 static int evm_protected_xattr(const char *req_xattr_name)
188 char **xattrname;
189 int namelen;
190 int found = 0;
192 namelen = strlen(req_xattr_name);
193 for (xattrname = evm_config_xattrnames; *xattrname != NULL; xattrname++) {
194 if ((strlen(*xattrname) == namelen)
195 && (strncmp(req_xattr_name, *xattrname, namelen) == 0)) {
196 found = 1;
197 break;
199 if (strncmp(req_xattr_name,
200 *xattrname + XATTR_SECURITY_PREFIX_LEN,
201 strlen(req_xattr_name)) == 0) {
202 found = 1;
203 break;
206 return found;
210 * evm_verifyxattr - verify the integrity of the requested xattr
211 * @dentry: object of the verify xattr
212 * @xattr_name: requested xattr
213 * @xattr_value: requested xattr value
214 * @xattr_value_len: requested xattr value length
216 * Calculate the HMAC for the given dentry and verify it against the stored
217 * security.evm xattr. For performance, use the xattr value and length
218 * previously retrieved to calculate the HMAC.
220 * Returns the xattr integrity status.
222 * This function requires the caller to lock the inode's i_mutex before it
223 * is executed.
225 enum integrity_status evm_verifyxattr(struct dentry *dentry,
226 const char *xattr_name,
227 void *xattr_value, size_t xattr_value_len,
228 struct integrity_iint_cache *iint)
230 if (!evm_initialized || !evm_protected_xattr(xattr_name))
231 return INTEGRITY_UNKNOWN;
233 if (!iint) {
234 iint = integrity_iint_find(dentry->d_inode);
235 if (!iint)
236 return INTEGRITY_UNKNOWN;
238 return evm_verify_hmac(dentry, xattr_name, xattr_value,
239 xattr_value_len, iint);
241 EXPORT_SYMBOL_GPL(evm_verifyxattr);
244 * evm_verify_current_integrity - verify the dentry's metadata integrity
245 * @dentry: pointer to the affected dentry
247 * Verify and return the dentry's metadata integrity. The exceptions are
248 * before EVM is initialized or in 'fix' mode.
250 static enum integrity_status evm_verify_current_integrity(struct dentry *dentry)
252 struct inode *inode = dentry->d_inode;
254 if (!evm_initialized || !S_ISREG(inode->i_mode) || evm_fixmode)
255 return 0;
256 return evm_verify_hmac(dentry, NULL, NULL, 0, NULL);
260 * evm_protect_xattr - protect the EVM extended attribute
262 * Prevent security.evm from being modified or removed without the
263 * necessary permissions or when the existing value is invalid.
265 * The posix xattr acls are 'system' prefixed, which normally would not
266 * affect security.evm. An interesting side affect of writing posix xattr
267 * acls is their modifying of the i_mode, which is included in security.evm.
268 * For posix xattr acls only, permit security.evm, even if it currently
269 * doesn't exist, to be updated.
271 static int evm_protect_xattr(struct dentry *dentry, const char *xattr_name,
272 const void *xattr_value, size_t xattr_value_len)
274 enum integrity_status evm_status;
276 if (strcmp(xattr_name, XATTR_NAME_EVM) == 0) {
277 if (!capable(CAP_SYS_ADMIN))
278 return -EPERM;
279 } else if (!evm_protected_xattr(xattr_name)) {
280 if (!posix_xattr_acl(xattr_name))
281 return 0;
282 evm_status = evm_verify_current_integrity(dentry);
283 if ((evm_status == INTEGRITY_PASS) ||
284 (evm_status == INTEGRITY_NOXATTRS))
285 return 0;
286 goto out;
288 evm_status = evm_verify_current_integrity(dentry);
289 if (evm_status == INTEGRITY_NOXATTRS) {
290 struct integrity_iint_cache *iint;
292 iint = integrity_iint_find(dentry->d_inode);
293 if (iint && (iint->flags & IMA_NEW_FILE))
294 return 0;
296 /* exception for pseudo filesystems */
297 if (dentry->d_inode->i_sb->s_magic == TMPFS_MAGIC
298 || dentry->d_inode->i_sb->s_magic == SYSFS_MAGIC)
299 return 0;
301 integrity_audit_msg(AUDIT_INTEGRITY_METADATA,
302 dentry->d_inode, dentry->d_name.name,
303 "update_metadata",
304 integrity_status_msg[evm_status],
305 -EPERM, 0);
307 out:
308 if (evm_status != INTEGRITY_PASS)
309 integrity_audit_msg(AUDIT_INTEGRITY_METADATA, dentry->d_inode,
310 dentry->d_name.name, "appraise_metadata",
311 integrity_status_msg[evm_status],
312 -EPERM, 0);
313 return evm_status == INTEGRITY_PASS ? 0 : -EPERM;
317 * evm_inode_setxattr - protect the EVM extended attribute
318 * @dentry: pointer to the affected dentry
319 * @xattr_name: pointer to the affected extended attribute name
320 * @xattr_value: pointer to the new extended attribute value
321 * @xattr_value_len: pointer to the new extended attribute value length
323 * Before allowing the 'security.evm' protected xattr to be updated,
324 * verify the existing value is valid. As only the kernel should have
325 * access to the EVM encrypted key needed to calculate the HMAC, prevent
326 * userspace from writing HMAC value. Writing 'security.evm' requires
327 * requires CAP_SYS_ADMIN privileges.
329 int evm_inode_setxattr(struct dentry *dentry, const char *xattr_name,
330 const void *xattr_value, size_t xattr_value_len)
332 const struct evm_ima_xattr_data *xattr_data = xattr_value;
334 if (strcmp(xattr_name, XATTR_NAME_EVM) == 0) {
335 if (!xattr_value_len)
336 return -EINVAL;
337 if (xattr_data->type != EVM_IMA_XATTR_DIGSIG)
338 return -EPERM;
340 return evm_protect_xattr(dentry, xattr_name, xattr_value,
341 xattr_value_len);
345 * evm_inode_removexattr - protect the EVM extended attribute
346 * @dentry: pointer to the affected dentry
347 * @xattr_name: pointer to the affected extended attribute name
349 * Removing 'security.evm' requires CAP_SYS_ADMIN privileges and that
350 * the current value is valid.
352 int evm_inode_removexattr(struct dentry *dentry, const char *xattr_name)
354 return evm_protect_xattr(dentry, xattr_name, NULL, 0);
358 * evm_inode_post_setxattr - update 'security.evm' to reflect the changes
359 * @dentry: pointer to the affected dentry
360 * @xattr_name: pointer to the affected extended attribute name
361 * @xattr_value: pointer to the new extended attribute value
362 * @xattr_value_len: pointer to the new extended attribute value length
364 * Update the HMAC stored in 'security.evm' to reflect the change.
366 * No need to take the i_mutex lock here, as this function is called from
367 * __vfs_setxattr_noperm(). The caller of which has taken the inode's
368 * i_mutex lock.
370 void evm_inode_post_setxattr(struct dentry *dentry, const char *xattr_name,
371 const void *xattr_value, size_t xattr_value_len)
373 if (!evm_initialized || (!evm_protected_xattr(xattr_name)
374 && !posix_xattr_acl(xattr_name)))
375 return;
377 evm_update_evmxattr(dentry, xattr_name, xattr_value, xattr_value_len);
378 return;
382 * evm_inode_post_removexattr - update 'security.evm' after removing the xattr
383 * @dentry: pointer to the affected dentry
384 * @xattr_name: pointer to the affected extended attribute name
386 * Update the HMAC stored in 'security.evm' to reflect removal of the xattr.
388 void evm_inode_post_removexattr(struct dentry *dentry, const char *xattr_name)
390 struct inode *inode = dentry->d_inode;
392 if (!evm_initialized || !evm_protected_xattr(xattr_name))
393 return;
395 mutex_lock(&inode->i_mutex);
396 evm_update_evmxattr(dentry, xattr_name, NULL, 0);
397 mutex_unlock(&inode->i_mutex);
398 return;
402 * evm_inode_setattr - prevent updating an invalid EVM extended attribute
403 * @dentry: pointer to the affected dentry
405 int evm_inode_setattr(struct dentry *dentry, struct iattr *attr)
407 unsigned int ia_valid = attr->ia_valid;
408 enum integrity_status evm_status;
410 if (!(ia_valid & (ATTR_MODE | ATTR_UID | ATTR_GID)))
411 return 0;
412 evm_status = evm_verify_current_integrity(dentry);
413 if ((evm_status == INTEGRITY_PASS) ||
414 (evm_status == INTEGRITY_NOXATTRS))
415 return 0;
416 integrity_audit_msg(AUDIT_INTEGRITY_METADATA, dentry->d_inode,
417 dentry->d_name.name, "appraise_metadata",
418 integrity_status_msg[evm_status], -EPERM, 0);
419 return -EPERM;
423 * evm_inode_post_setattr - update 'security.evm' after modifying metadata
424 * @dentry: pointer to the affected dentry
425 * @ia_valid: for the UID and GID status
427 * For now, update the HMAC stored in 'security.evm' to reflect UID/GID
428 * changes.
430 * This function is called from notify_change(), which expects the caller
431 * to lock the inode's i_mutex.
433 void evm_inode_post_setattr(struct dentry *dentry, int ia_valid)
435 if (!evm_initialized)
436 return;
438 if (ia_valid & (ATTR_MODE | ATTR_UID | ATTR_GID))
439 evm_update_evmxattr(dentry, NULL, NULL, 0);
440 return;
444 * evm_inode_init_security - initializes security.evm
446 int evm_inode_init_security(struct inode *inode,
447 const struct xattr *lsm_xattr,
448 struct xattr *evm_xattr)
450 struct evm_ima_xattr_data *xattr_data;
451 int rc;
453 if (!evm_initialized || !evm_protected_xattr(lsm_xattr->name))
454 return 0;
456 xattr_data = kzalloc(sizeof(*xattr_data), GFP_NOFS);
457 if (!xattr_data)
458 return -ENOMEM;
460 xattr_data->type = EVM_XATTR_HMAC;
461 rc = evm_init_hmac(inode, lsm_xattr, xattr_data->digest);
462 if (rc < 0)
463 goto out;
465 evm_xattr->value = xattr_data;
466 evm_xattr->value_len = sizeof(*xattr_data);
467 evm_xattr->name = XATTR_EVM_SUFFIX;
468 return 0;
469 out:
470 kfree(xattr_data);
471 return rc;
473 EXPORT_SYMBOL_GPL(evm_inode_init_security);
475 static int __init init_evm(void)
477 int error;
479 evm_init_config();
481 error = evm_init_secfs();
482 if (error < 0) {
483 pr_info("Error registering secfs\n");
484 goto err;
487 return 0;
488 err:
489 return error;
493 * evm_display_config - list the EVM protected security extended attributes
495 static int __init evm_display_config(void)
497 char **xattrname;
499 for (xattrname = evm_config_xattrnames; *xattrname != NULL; xattrname++)
500 pr_info("%s\n", *xattrname);
501 return 0;
504 pure_initcall(evm_display_config);
505 late_initcall(init_evm);
507 MODULE_DESCRIPTION("Extended Verification Module");
508 MODULE_LICENSE("GPL");