1 /*
2 * Copyright (C) 2005-2010 IBM Corporation
3 *
4 * Author:
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_main.c
13 * implements evm_inode_setxattr, evm_inode_post_setxattr,
14 * evm_inode_removexattr, and evm_verifyxattr
15 */
16
17 #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
18
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"
29
30 int evm_initialized;
31
32 static char *integrity_status_msg[] = {
33 "pass", "fail", "no_label", "no_xattrs", "unknown"
34 };
35 char *evm_hmac = "hmac(sha1)";
36 char *evm_hash = "sha1";
37 int evm_hmac_attrs;
38
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
56 };
57
58 static int evm_fixmode;
evm_set_fixmode(char * str)59 static int __init evm_set_fixmode(char *str)
60 {
61 if (strncmp(str, "fix", 3) == 0)
62 evm_fixmode = 1;
63 return 0;
64 }
65 __setup("evm=", evm_set_fixmode);
66
evm_init_config(void)67 static void __init evm_init_config(void)
68 {
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);
73 }
74
evm_find_protected_xattrs(struct dentry * dentry)75 static int evm_find_protected_xattrs(struct dentry *dentry)
76 {
77 struct inode *inode = dentry->d_inode;
78 char **xattr;
79 int error;
80 int count = 0;
81
82 if (!inode->i_op->getxattr)
83 return -EOPNOTSUPP;
84
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;
91 }
92 count++;
93 }
94
95 return count;
96 }
97
98 /*
99 * evm_verify_hmac - calculate and compare the HMAC with the EVM xattr
100 *
101 * Compute the HMAC on the dentry's protected set of extended attributes
102 * and compare it against the stored security.evm xattr.
103 *
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.
108 *
109 * Returns integrity status
110 */
evm_verify_hmac(struct dentry * dentry,const char * xattr_name,char * xattr_value,size_t xattr_value_len,struct integrity_iint_cache * iint)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)
116 {
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;
121
122 if (iint && iint->evm_status == INTEGRITY_PASS)
123 return iint->evm_status;
124
125 /* if status is not PASS, try to check again - against -ENOMEM */
126
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 evm_status = INTEGRITY_FAIL;
132 if (rc == -ENODATA) {
133 rc = evm_find_protected_xattrs(dentry);
134 if (rc > 0)
135 evm_status = INTEGRITY_NOLABEL;
136 else if (rc == 0)
137 evm_status = INTEGRITY_NOXATTRS; /* new file */
138 } else if (rc == -EOPNOTSUPP) {
139 evm_status = INTEGRITY_UNKNOWN;
140 }
141 goto out;
142 }
143
144 xattr_len = rc;
145
146 /* check value type */
147 switch (xattr_data->type) {
148 case EVM_XATTR_HMAC:
149 rc = evm_calc_hmac(dentry, xattr_name, xattr_value,
150 xattr_value_len, calc.digest);
151 if (rc)
152 break;
153 rc = crypto_memneq(xattr_data->digest, calc.digest,
154 sizeof(calc.digest));
155 if (rc)
156 rc = -EINVAL;
157 break;
158 case EVM_IMA_XATTR_DIGSIG:
159 rc = evm_calc_hash(dentry, xattr_name, xattr_value,
160 xattr_value_len, calc.digest);
161 if (rc)
162 break;
163 rc = integrity_digsig_verify(INTEGRITY_KEYRING_EVM,
164 (const char *)xattr_data, xattr_len,
165 calc.digest, sizeof(calc.digest));
166 if (!rc) {
167 /* we probably want to replace rsa with hmac here */
168 evm_update_evmxattr(dentry, xattr_name, xattr_value,
169 xattr_value_len);
170 }
171 break;
172 default:
173 rc = -EINVAL;
174 break;
175 }
176
177 if (rc)
178 evm_status = (rc == -ENODATA) ?
179 INTEGRITY_NOXATTRS : INTEGRITY_FAIL;
180 out:
181 if (iint)
182 iint->evm_status = evm_status;
183 kfree(xattr_data);
184 return evm_status;
185 }
186
evm_protected_xattr(const char * req_xattr_name)187 static int evm_protected_xattr(const char *req_xattr_name)
188 {
189 char **xattrname;
190 int namelen;
191 int found = 0;
192
193 namelen = strlen(req_xattr_name);
194 for (xattrname = evm_config_xattrnames; *xattrname != NULL; xattrname++) {
195 if ((strlen(*xattrname) == namelen)
196 && (strncmp(req_xattr_name, *xattrname, namelen) == 0)) {
197 found = 1;
198 break;
199 }
200 if (strncmp(req_xattr_name,
201 *xattrname + XATTR_SECURITY_PREFIX_LEN,
202 strlen(req_xattr_name)) == 0) {
203 found = 1;
204 break;
205 }
206 }
207 return found;
208 }
209
210 /**
211 * evm_verifyxattr - verify the integrity of the requested xattr
212 * @dentry: object of the verify xattr
213 * @xattr_name: requested xattr
214 * @xattr_value: requested xattr value
215 * @xattr_value_len: requested xattr value length
216 *
217 * Calculate the HMAC for the given dentry and verify it against the stored
218 * security.evm xattr. For performance, use the xattr value and length
219 * previously retrieved to calculate the HMAC.
220 *
221 * Returns the xattr integrity status.
222 *
223 * This function requires the caller to lock the inode's i_mutex before it
224 * is executed.
225 */
evm_verifyxattr(struct dentry * dentry,const char * xattr_name,void * xattr_value,size_t xattr_value_len,struct integrity_iint_cache * iint)226 enum integrity_status evm_verifyxattr(struct dentry *dentry,
227 const char *xattr_name,
228 void *xattr_value, size_t xattr_value_len,
229 struct integrity_iint_cache *iint)
230 {
231 if (!evm_initialized || !evm_protected_xattr(xattr_name))
232 return INTEGRITY_UNKNOWN;
233
234 if (!iint) {
235 iint = integrity_iint_find(dentry->d_inode);
236 if (!iint)
237 return INTEGRITY_UNKNOWN;
238 }
239 return evm_verify_hmac(dentry, xattr_name, xattr_value,
240 xattr_value_len, iint);
241 }
242 EXPORT_SYMBOL_GPL(evm_verifyxattr);
243
244 /*
245 * evm_verify_current_integrity - verify the dentry's metadata integrity
246 * @dentry: pointer to the affected dentry
247 *
248 * Verify and return the dentry's metadata integrity. The exceptions are
249 * before EVM is initialized or in 'fix' mode.
250 */
evm_verify_current_integrity(struct dentry * dentry)251 static enum integrity_status evm_verify_current_integrity(struct dentry *dentry)
252 {
253 struct inode *inode = dentry->d_inode;
254
255 if (!evm_initialized || !S_ISREG(inode->i_mode) || evm_fixmode)
256 return 0;
257 return evm_verify_hmac(dentry, NULL, NULL, 0, NULL);
258 }
259
260 /*
261 * evm_protect_xattr - protect the EVM extended attribute
262 *
263 * Prevent security.evm from being modified or removed without the
264 * necessary permissions or when the existing value is invalid.
265 *
266 * The posix xattr acls are 'system' prefixed, which normally would not
267 * affect security.evm. An interesting side affect of writing posix xattr
268 * acls is their modifying of the i_mode, which is included in security.evm.
269 * For posix xattr acls only, permit security.evm, even if it currently
270 * doesn't exist, to be updated.
271 */
evm_protect_xattr(struct dentry * dentry,const char * xattr_name,const void * xattr_value,size_t xattr_value_len)272 static int evm_protect_xattr(struct dentry *dentry, const char *xattr_name,
273 const void *xattr_value, size_t xattr_value_len)
274 {
275 enum integrity_status evm_status;
276
277 if (strcmp(xattr_name, XATTR_NAME_EVM) == 0) {
278 if (!capable(CAP_SYS_ADMIN))
279 return -EPERM;
280 } else if (!evm_protected_xattr(xattr_name)) {
281 if (!posix_xattr_acl(xattr_name))
282 return 0;
283 evm_status = evm_verify_current_integrity(dentry);
284 if ((evm_status == INTEGRITY_PASS) ||
285 (evm_status == INTEGRITY_NOXATTRS))
286 return 0;
287 goto out;
288 }
289 evm_status = evm_verify_current_integrity(dentry);
290 if (evm_status == INTEGRITY_NOXATTRS) {
291 struct integrity_iint_cache *iint;
292
293 iint = integrity_iint_find(dentry->d_inode);
294 if (iint && (iint->flags & IMA_NEW_FILE))
295 return 0;
296
297 /* exception for pseudo filesystems */
298 if (dentry->d_inode->i_sb->s_magic == TMPFS_MAGIC
299 || dentry->d_inode->i_sb->s_magic == SYSFS_MAGIC)
300 return 0;
301
302 integrity_audit_msg(AUDIT_INTEGRITY_METADATA,
303 dentry->d_inode, dentry->d_name.name,
304 "update_metadata",
305 integrity_status_msg[evm_status],
306 -EPERM, 0);
307 }
308 out:
309 if (evm_status != INTEGRITY_PASS)
310 integrity_audit_msg(AUDIT_INTEGRITY_METADATA, dentry->d_inode,
311 dentry->d_name.name, "appraise_metadata",
312 integrity_status_msg[evm_status],
313 -EPERM, 0);
314 return evm_status == INTEGRITY_PASS ? 0 : -EPERM;
315 }
316
317 /**
318 * evm_inode_setxattr - protect the EVM extended attribute
319 * @dentry: pointer to the affected dentry
320 * @xattr_name: pointer to the affected extended attribute name
321 * @xattr_value: pointer to the new extended attribute value
322 * @xattr_value_len: pointer to the new extended attribute value length
323 *
324 * Before allowing the 'security.evm' protected xattr to be updated,
325 * verify the existing value is valid. As only the kernel should have
326 * access to the EVM encrypted key needed to calculate the HMAC, prevent
327 * userspace from writing HMAC value. Writing 'security.evm' requires
328 * requires CAP_SYS_ADMIN privileges.
329 */
evm_inode_setxattr(struct dentry * dentry,const char * xattr_name,const void * xattr_value,size_t xattr_value_len)330 int evm_inode_setxattr(struct dentry *dentry, const char *xattr_name,
331 const void *xattr_value, size_t xattr_value_len)
332 {
333 const struct evm_ima_xattr_data *xattr_data = xattr_value;
334
335 if (strcmp(xattr_name, XATTR_NAME_EVM) == 0) {
336 if (!xattr_value_len)
337 return -EINVAL;
338 if (xattr_data->type != EVM_IMA_XATTR_DIGSIG)
339 return -EPERM;
340 }
341 return evm_protect_xattr(dentry, xattr_name, xattr_value,
342 xattr_value_len);
343 }
344
345 /**
346 * evm_inode_removexattr - protect the EVM extended attribute
347 * @dentry: pointer to the affected dentry
348 * @xattr_name: pointer to the affected extended attribute name
349 *
350 * Removing 'security.evm' requires CAP_SYS_ADMIN privileges and that
351 * the current value is valid.
352 */
evm_inode_removexattr(struct dentry * dentry,const char * xattr_name)353 int evm_inode_removexattr(struct dentry *dentry, const char *xattr_name)
354 {
355 return evm_protect_xattr(dentry, xattr_name, NULL, 0);
356 }
357
358 /**
359 * evm_inode_post_setxattr - update 'security.evm' to reflect the changes
360 * @dentry: pointer to the affected dentry
361 * @xattr_name: pointer to the affected extended attribute name
362 * @xattr_value: pointer to the new extended attribute value
363 * @xattr_value_len: pointer to the new extended attribute value length
364 *
365 * Update the HMAC stored in 'security.evm' to reflect the change.
366 *
367 * No need to take the i_mutex lock here, as this function is called from
368 * __vfs_setxattr_noperm(). The caller of which has taken the inode's
369 * i_mutex lock.
370 */
evm_inode_post_setxattr(struct dentry * dentry,const char * xattr_name,const void * xattr_value,size_t xattr_value_len)371 void evm_inode_post_setxattr(struct dentry *dentry, const char *xattr_name,
372 const void *xattr_value, size_t xattr_value_len)
373 {
374 if (!evm_initialized || (!evm_protected_xattr(xattr_name)
375 && !posix_xattr_acl(xattr_name)))
376 return;
377
378 evm_update_evmxattr(dentry, xattr_name, xattr_value, xattr_value_len);
379 }
380
381 /**
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
385 *
386 * Update the HMAC stored in 'security.evm' to reflect removal of the xattr.
387 */
evm_inode_post_removexattr(struct dentry * dentry,const char * xattr_name)388 void evm_inode_post_removexattr(struct dentry *dentry, const char *xattr_name)
389 {
390 struct inode *inode = dentry->d_inode;
391
392 if (!evm_initialized || !evm_protected_xattr(xattr_name))
393 return;
394
395 mutex_lock(&inode->i_mutex);
396 evm_update_evmxattr(dentry, xattr_name, NULL, 0);
397 mutex_unlock(&inode->i_mutex);
398 }
399
400 /**
401 * evm_inode_setattr - prevent updating an invalid EVM extended attribute
402 * @dentry: pointer to the affected dentry
403 */
evm_inode_setattr(struct dentry * dentry,struct iattr * attr)404 int evm_inode_setattr(struct dentry *dentry, struct iattr *attr)
405 {
406 unsigned int ia_valid = attr->ia_valid;
407 enum integrity_status evm_status;
408
409 if (!(ia_valid & (ATTR_MODE | ATTR_UID | ATTR_GID)))
410 return 0;
411 evm_status = evm_verify_current_integrity(dentry);
412 if ((evm_status == INTEGRITY_PASS) ||
413 (evm_status == INTEGRITY_NOXATTRS))
414 return 0;
415 integrity_audit_msg(AUDIT_INTEGRITY_METADATA, dentry->d_inode,
416 dentry->d_name.name, "appraise_metadata",
417 integrity_status_msg[evm_status], -EPERM, 0);
418 return -EPERM;
419 }
420
421 /**
422 * evm_inode_post_setattr - update 'security.evm' after modifying metadata
423 * @dentry: pointer to the affected dentry
424 * @ia_valid: for the UID and GID status
425 *
426 * For now, update the HMAC stored in 'security.evm' to reflect UID/GID
427 * changes.
428 *
429 * This function is called from notify_change(), which expects the caller
430 * to lock the inode's i_mutex.
431 */
evm_inode_post_setattr(struct dentry * dentry,int ia_valid)432 void evm_inode_post_setattr(struct dentry *dentry, int ia_valid)
433 {
434 if (!evm_initialized)
435 return;
436
437 if (ia_valid & (ATTR_MODE | ATTR_UID | ATTR_GID))
438 evm_update_evmxattr(dentry, NULL, NULL, 0);
439 }
440
441 /*
442 * evm_inode_init_security - initializes security.evm
443 */
evm_inode_init_security(struct inode * inode,const struct xattr * lsm_xattr,struct xattr * evm_xattr)444 int evm_inode_init_security(struct inode *inode,
445 const struct xattr *lsm_xattr,
446 struct xattr *evm_xattr)
447 {
448 struct evm_ima_xattr_data *xattr_data;
449 int rc;
450
451 if (!evm_initialized || !evm_protected_xattr(lsm_xattr->name))
452 return 0;
453
454 xattr_data = kzalloc(sizeof(*xattr_data), GFP_NOFS);
455 if (!xattr_data)
456 return -ENOMEM;
457
458 xattr_data->type = EVM_XATTR_HMAC;
459 rc = evm_init_hmac(inode, lsm_xattr, xattr_data->digest);
460 if (rc < 0)
461 goto out;
462
463 evm_xattr->value = xattr_data;
464 evm_xattr->value_len = sizeof(*xattr_data);
465 evm_xattr->name = XATTR_EVM_SUFFIX;
466 return 0;
467 out:
468 kfree(xattr_data);
469 return rc;
470 }
471 EXPORT_SYMBOL_GPL(evm_inode_init_security);
472
init_evm(void)473 static int __init init_evm(void)
474 {
475 int error;
476
477 evm_init_config();
478
479 error = evm_init_secfs();
480 if (error < 0) {
481 pr_info("Error registering secfs\n");
482 goto err;
483 }
484
485 return 0;
486 err:
487 return error;
488 }
489
490 /*
491 * evm_display_config - list the EVM protected security extended attributes
492 */
evm_display_config(void)493 static int __init evm_display_config(void)
494 {
495 char **xattrname;
496
497 for (xattrname = evm_config_xattrnames; *xattrname != NULL; xattrname++)
498 pr_info("%s\n", *xattrname);
499 return 0;
500 }
501
502 pure_initcall(evm_display_config);
503 late_initcall(init_evm);
504
505 MODULE_DESCRIPTION("Extended Verification Module");
506 MODULE_LICENSE("GPL");
507