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 #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>
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";
39 char *evm_config_xattrnames
[] = {
40 #ifdef CONFIG_SECURITY_SELINUX
43 #ifdef CONFIG_SECURITY_SMACK
45 #ifdef CONFIG_EVM_EXTRA_SMACK_XATTRS
47 XATTR_NAME_SMACKTRANSMUTE
,
51 #ifdef CONFIG_IMA_APPRAISE
58 static int evm_fixmode
;
59 static int __init
evm_set_fixmode(char *str
)
61 if (strncmp(str
, "fix", 3) == 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
;
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
;
82 if (!inode
->i_op
->getxattr
)
85 for (xattr
= evm_config_xattrnames
; *xattr
!= NULL
; xattr
++) {
86 error
= inode
->i_op
->getxattr(dentry
, *xattr
, NULL
, 0);
88 if (error
== -ENODATA
)
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.
105 * - use the previoulsy retrieved xattr value and length to calculate the
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
,
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
;
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,
132 evm_status
= INTEGRITY_FAIL
; /* empty */
133 else if (rc
== -ENODATA
) {
134 rc
= evm_find_protected_xattrs(dentry
);
136 evm_status
= INTEGRITY_NOLABEL
;
138 evm_status
= INTEGRITY_NOXATTRS
; /* new file */
145 /* check value type */
146 switch (xattr_data
->type
) {
148 rc
= evm_calc_hmac(dentry
, xattr_name
, xattr_value
,
149 xattr_value_len
, calc
.digest
);
152 rc
= crypto_memneq(xattr_data
->digest
, calc
.digest
,
153 sizeof(calc
.digest
));
157 case EVM_IMA_XATTR_DIGSIG
:
158 rc
= evm_calc_hash(dentry
, xattr_name
, xattr_value
,
159 xattr_value_len
, calc
.digest
);
162 rc
= integrity_digsig_verify(INTEGRITY_KEYRING_EVM
,
163 (const char *)xattr_data
, xattr_len
,
164 calc
.digest
, sizeof(calc
.digest
));
166 /* we probably want to replace rsa with hmac here */
167 evm_update_evmxattr(dentry
, xattr_name
, xattr_value
,
177 evm_status
= (rc
== -ENODATA
) ?
178 INTEGRITY_NOXATTRS
: INTEGRITY_FAIL
;
181 iint
->evm_status
= evm_status
;
186 static int evm_protected_xattr(const char *req_xattr_name
)
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)) {
199 if (strncmp(req_xattr_name
,
200 *xattrname
+ XATTR_SECURITY_PREFIX_LEN
,
201 strlen(req_xattr_name
)) == 0) {
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
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
;
234 iint
= integrity_iint_find(dentry
->d_inode
);
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
)
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
))
279 } else if (!evm_protected_xattr(xattr_name
)) {
280 if (!posix_xattr_acl(xattr_name
))
282 evm_status
= evm_verify_current_integrity(dentry
);
283 if ((evm_status
== INTEGRITY_PASS
) ||
284 (evm_status
== INTEGRITY_NOXATTRS
))
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
))
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
)
301 integrity_audit_msg(AUDIT_INTEGRITY_METADATA
,
302 dentry
->d_inode
, dentry
->d_name
.name
,
304 integrity_status_msg
[evm_status
],
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
],
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
)
337 if (xattr_data
->type
!= EVM_IMA_XATTR_DIGSIG
)
340 return evm_protect_xattr(dentry
, xattr_name
, xattr_value
,
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
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
)))
377 evm_update_evmxattr(dentry
, xattr_name
, xattr_value
, xattr_value_len
);
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
))
395 mutex_lock(&inode
->i_mutex
);
396 evm_update_evmxattr(dentry
, xattr_name
, NULL
, 0);
397 mutex_unlock(&inode
->i_mutex
);
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
)))
412 evm_status
= evm_verify_current_integrity(dentry
);
413 if ((evm_status
== INTEGRITY_PASS
) ||
414 (evm_status
== INTEGRITY_NOXATTRS
))
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);
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
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
)
438 if (ia_valid
& (ATTR_MODE
| ATTR_UID
| ATTR_GID
))
439 evm_update_evmxattr(dentry
, NULL
, NULL
, 0);
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
;
453 if (!evm_initialized
|| !evm_protected_xattr(lsm_xattr
->name
))
456 xattr_data
= kzalloc(sizeof(*xattr_data
), GFP_NOFS
);
460 xattr_data
->type
= EVM_XATTR_HMAC
;
461 rc
= evm_init_hmac(inode
, lsm_xattr
, xattr_data
->digest
);
465 evm_xattr
->value
= xattr_data
;
466 evm_xattr
->value_len
= sizeof(*xattr_data
);
467 evm_xattr
->name
= XATTR_EVM_SUFFIX
;
473 EXPORT_SYMBOL_GPL(evm_inode_init_security
);
475 static int __init
init_evm(void)
481 error
= evm_init_secfs();
483 pr_info("Error registering secfs\n");
493 * evm_display_config - list the EVM protected security extended attributes
495 static int __init
evm_display_config(void)
499 for (xattrname
= evm_config_xattrnames
; *xattrname
!= NULL
; xattrname
++)
500 pr_info("%s\n", *xattrname
);
504 pure_initcall(evm_display_config
);
505 late_initcall(init_evm
);
507 MODULE_DESCRIPTION("Extended Verification Module");
508 MODULE_LICENSE("GPL");