| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| #include <linux/init.h> |
| #include <linux/crypto.h> |
| #include <linux/audit.h> |
| #include <linux/xattr.h> |
| #include <linux/integrity.h> |
| #include <linux/evm.h> |
| #include <linux/magic.h> |
| |
| #include <crypto/hash.h> |
| #include <crypto/hash_info.h> |
| #include <crypto/algapi.h> |
| #include "evm.h" |
| |
| int evm_initialized; |
| |
| static const char * const integrity_status_msg[] = { |
| <------>"pass", "pass_immutable", "fail", "no_label", "no_xattrs", "unknown" |
| }; |
| int evm_hmac_attrs; |
| |
| static struct xattr_list evm_config_default_xattrnames[] = { |
| #ifdef CONFIG_SECURITY_SELINUX |
| <------>{.name = XATTR_NAME_SELINUX}, |
| #endif |
| #ifdef CONFIG_SECURITY_SMACK |
| <------>{.name = XATTR_NAME_SMACK}, |
| #ifdef CONFIG_EVM_EXTRA_SMACK_XATTRS |
| <------>{.name = XATTR_NAME_SMACKEXEC}, |
| <------>{.name = XATTR_NAME_SMACKTRANSMUTE}, |
| <------>{.name = XATTR_NAME_SMACKMMAP}, |
| #endif |
| #endif |
| #ifdef CONFIG_SECURITY_APPARMOR |
| <------>{.name = XATTR_NAME_APPARMOR}, |
| #endif |
| #ifdef CONFIG_IMA_APPRAISE |
| <------>{.name = XATTR_NAME_IMA}, |
| #endif |
| <------>{.name = XATTR_NAME_CAPS}, |
| }; |
| |
| LIST_HEAD(evm_config_xattrnames); |
| |
| static int evm_fixmode __ro_after_init; |
| static int __init evm_set_fixmode(char *str) |
| { |
| <------>if (strncmp(str, "fix", 3) == 0) |
| <------><------>evm_fixmode = 1; |
| <------>else |
| <------><------>pr_err("invalid \"%s\" mode", str); |
| |
| <------>return 1; |
| } |
| __setup("evm=", evm_set_fixmode); |
| |
| static void __init evm_init_config(void) |
| { |
| <------>int i, xattrs; |
| |
| <------>xattrs = ARRAY_SIZE(evm_config_default_xattrnames); |
| |
| <------>pr_info("Initialising EVM extended attributes:\n"); |
| <------>for (i = 0; i < xattrs; i++) { |
| <------><------>pr_info("%s\n", evm_config_default_xattrnames[i].name); |
| <------><------>list_add_tail(&evm_config_default_xattrnames[i].list, |
| <------><------><------> &evm_config_xattrnames); |
| <------>} |
| |
| #ifdef CONFIG_EVM_ATTR_FSUUID |
| <------>evm_hmac_attrs |= EVM_ATTR_FSUUID; |
| #endif |
| <------>pr_info("HMAC attrs: 0x%x\n", evm_hmac_attrs); |
| } |
| |
| static bool evm_key_loaded(void) |
| { |
| <------>return (bool)(evm_initialized & EVM_KEY_MASK); |
| } |
| |
| static int evm_find_protected_xattrs(struct dentry *dentry) |
| { |
| <------>struct inode *inode = d_backing_inode(dentry); |
| <------>struct xattr_list *xattr; |
| <------>int error; |
| <------>int count = 0; |
| |
| <------>if (!(inode->i_opflags & IOP_XATTR)) |
| <------><------>return -EOPNOTSUPP; |
| |
| <------>list_for_each_entry_lockless(xattr, &evm_config_xattrnames, list) { |
| <------><------>error = __vfs_getxattr(dentry, inode, xattr->name, NULL, 0, |
| <------><------><------><------> XATTR_NOSECURITY); |
| <------><------>if (error < 0) { |
| <------><------><------>if (error == -ENODATA) |
| <------><------><------><------>continue; |
| <------><------><------>return error; |
| <------><------>} |
| <------><------>count++; |
| <------>} |
| |
| <------>return count; |
| } |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| static enum integrity_status evm_verify_hmac(struct dentry *dentry, |
| <------><------><------><------><------> const char *xattr_name, |
| <------><------><------><------><------> char *xattr_value, |
| <------><------><------><------><------> size_t xattr_value_len, |
| <------><------><------><------><------> struct integrity_iint_cache *iint) |
| { |
| <------>struct evm_ima_xattr_data *xattr_data = NULL; |
| <------>struct signature_v2_hdr *hdr; |
| <------>enum integrity_status evm_status = INTEGRITY_PASS; |
| <------>struct evm_digest digest; |
| <------>struct inode *inode; |
| <------>int rc, xattr_len; |
| |
| <------>if (iint && (iint->evm_status == INTEGRITY_PASS || |
| <------><------> iint->evm_status == INTEGRITY_PASS_IMMUTABLE)) |
| <------><------>return iint->evm_status; |
| |
| <------> |
| |
| <------> |
| <------>rc = vfs_getxattr_alloc(dentry, XATTR_NAME_EVM, (char **)&xattr_data, 0, |
| <------><------><------><------>GFP_NOFS); |
| <------>if (rc <= 0) { |
| <------><------>evm_status = INTEGRITY_FAIL; |
| <------><------>if (rc == -ENODATA) { |
| <------><------><------>rc = evm_find_protected_xattrs(dentry); |
| <------><------><------>if (rc > 0) |
| <------><------><------><------>evm_status = INTEGRITY_NOLABEL; |
| <------><------><------>else if (rc == 0) |
| <------><------><------><------>evm_status = INTEGRITY_NOXATTRS; |
| <------><------>} else if (rc == -EOPNOTSUPP) { |
| <------><------><------>evm_status = INTEGRITY_UNKNOWN; |
| <------><------>} |
| <------><------>goto out; |
| <------>} |
| |
| <------>xattr_len = rc; |
| |
| <------> |
| <------>switch (xattr_data->type) { |
| <------>case EVM_XATTR_HMAC: |
| <------><------>if (xattr_len != sizeof(struct evm_xattr)) { |
| <------><------><------>evm_status = INTEGRITY_FAIL; |
| <------><------><------>goto out; |
| <------><------>} |
| |
| <------><------>digest.hdr.algo = HASH_ALGO_SHA1; |
| <------><------>rc = evm_calc_hmac(dentry, xattr_name, xattr_value, |
| <------><------><------><------> xattr_value_len, &digest); |
| <------><------>if (rc) |
| <------><------><------>break; |
| <------><------>rc = crypto_memneq(xattr_data->data, digest.digest, |
| <------><------><------><------> SHA1_DIGEST_SIZE); |
| <------><------>if (rc) |
| <------><------><------>rc = -EINVAL; |
| <------><------>break; |
| <------>case EVM_IMA_XATTR_DIGSIG: |
| <------>case EVM_XATTR_PORTABLE_DIGSIG: |
| <------><------> |
| <------><------>if (xattr_len <= sizeof(struct signature_v2_hdr)) { |
| <------><------><------>evm_status = INTEGRITY_FAIL; |
| <------><------><------>goto out; |
| <------><------>} |
| |
| <------><------>hdr = (struct signature_v2_hdr *)xattr_data; |
| <------><------>digest.hdr.algo = hdr->hash_algo; |
| <------><------>rc = evm_calc_hash(dentry, xattr_name, xattr_value, |
| <------><------><------><------> xattr_value_len, xattr_data->type, &digest); |
| <------><------>if (rc) |
| <------><------><------>break; |
| <------><------>rc = integrity_digsig_verify(INTEGRITY_KEYRING_EVM, |
| <------><------><------><------><------>(const char *)xattr_data, xattr_len, |
| <------><------><------><------><------>digest.digest, digest.hdr.length); |
| <------><------>if (!rc) { |
| <------><------><------>inode = d_backing_inode(dentry); |
| |
| <------><------><------>if (xattr_data->type == EVM_XATTR_PORTABLE_DIGSIG) { |
| <------><------><------><------>if (iint) |
| <------><------><------><------><------>iint->flags |= EVM_IMMUTABLE_DIGSIG; |
| <------><------><------><------>evm_status = INTEGRITY_PASS_IMMUTABLE; |
| <------><------><------>} else if (!IS_RDONLY(inode) && |
| <------><------><------><------> !(inode->i_sb->s_readonly_remount) && |
| <------><------><------><------> !IS_IMMUTABLE(inode)) { |
| <------><------><------><------>evm_update_evmxattr(dentry, xattr_name, |
| <------><------><------><------><------><------> xattr_value, |
| <------><------><------><------><------><------> xattr_value_len); |
| <------><------><------>} |
| <------><------>} |
| <------><------>break; |
| <------>default: |
| <------><------>rc = -EINVAL; |
| <------><------>break; |
| <------>} |
| |
| <------>if (rc) |
| <------><------>evm_status = (rc == -ENODATA) ? |
| <------><------><------><------>INTEGRITY_NOXATTRS : INTEGRITY_FAIL; |
| out: |
| <------>if (iint) |
| <------><------>iint->evm_status = evm_status; |
| <------>kfree(xattr_data); |
| <------>return evm_status; |
| } |
| |
| static int evm_protected_xattr(const char *req_xattr_name) |
| { |
| <------>int namelen; |
| <------>int found = 0; |
| <------>struct xattr_list *xattr; |
| |
| <------>namelen = strlen(req_xattr_name); |
| <------>list_for_each_entry_lockless(xattr, &evm_config_xattrnames, list) { |
| <------><------>if ((strlen(xattr->name) == namelen) |
| <------><------> && (strncmp(req_xattr_name, xattr->name, namelen) == 0)) { |
| <------><------><------>found = 1; |
| <------><------><------>break; |
| <------><------>} |
| <------><------>if (strncmp(req_xattr_name, |
| <------><------><------> xattr->name + XATTR_SECURITY_PREFIX_LEN, |
| <------><------><------> strlen(req_xattr_name)) == 0) { |
| <------><------><------>found = 1; |
| <------><------><------>break; |
| <------><------>} |
| <------>} |
| |
| <------>return found; |
| } |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| enum integrity_status evm_verifyxattr(struct dentry *dentry, |
| <------><------><------><------> const char *xattr_name, |
| <------><------><------><------> void *xattr_value, size_t xattr_value_len, |
| <------><------><------><------> struct integrity_iint_cache *iint) |
| { |
| <------>if (!evm_key_loaded() || !evm_protected_xattr(xattr_name)) |
| <------><------>return INTEGRITY_UNKNOWN; |
| |
| <------>if (!iint) { |
| <------><------>iint = integrity_iint_find(d_backing_inode(dentry)); |
| <------><------>if (!iint) |
| <------><------><------>return INTEGRITY_UNKNOWN; |
| <------>} |
| <------>return evm_verify_hmac(dentry, xattr_name, xattr_value, |
| <------><------><------><------> xattr_value_len, iint); |
| } |
| EXPORT_SYMBOL_GPL(evm_verifyxattr); |
| |
| |
| |
| |
| |
| |
| |
| |
| static enum integrity_status evm_verify_current_integrity(struct dentry *dentry) |
| { |
| <------>struct inode *inode = d_backing_inode(dentry); |
| |
| <------>if (!evm_key_loaded() || !S_ISREG(inode->i_mode) || evm_fixmode) |
| <------><------>return 0; |
| <------>return evm_verify_hmac(dentry, NULL, NULL, 0, NULL); |
| } |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| static int evm_protect_xattr(struct dentry *dentry, const char *xattr_name, |
| <------><------><------> const void *xattr_value, size_t xattr_value_len) |
| { |
| <------>enum integrity_status evm_status; |
| |
| <------>if (strcmp(xattr_name, XATTR_NAME_EVM) == 0) { |
| <------><------>if (!capable(CAP_SYS_ADMIN)) |
| <------><------><------>return -EPERM; |
| <------>} else if (!evm_protected_xattr(xattr_name)) { |
| <------><------>if (!posix_xattr_acl(xattr_name)) |
| <------><------><------>return 0; |
| <------><------>evm_status = evm_verify_current_integrity(dentry); |
| <------><------>if ((evm_status == INTEGRITY_PASS) || |
| <------><------> (evm_status == INTEGRITY_NOXATTRS)) |
| <------><------><------>return 0; |
| <------><------>goto out; |
| <------>} |
| |
| <------>evm_status = evm_verify_current_integrity(dentry); |
| <------>if (evm_status == INTEGRITY_NOXATTRS) { |
| <------><------>struct integrity_iint_cache *iint; |
| |
| <------><------>iint = integrity_iint_find(d_backing_inode(dentry)); |
| <------><------>if (iint && (iint->flags & IMA_NEW_FILE)) |
| <------><------><------>return 0; |
| |
| <------><------> |
| <------><------>if (dentry->d_sb->s_magic == TMPFS_MAGIC |
| <------><------> || dentry->d_sb->s_magic == SYSFS_MAGIC) |
| <------><------><------>return 0; |
| |
| <------><------>integrity_audit_msg(AUDIT_INTEGRITY_METADATA, |
| <------><------><------><------> dentry->d_inode, dentry->d_name.name, |
| <------><------><------><------> "update_metadata", |
| <------><------><------><------> integrity_status_msg[evm_status], |
| <------><------><------><------> -EPERM, 0); |
| <------>} |
| out: |
| <------>if (evm_status != INTEGRITY_PASS) |
| <------><------>integrity_audit_msg(AUDIT_INTEGRITY_METADATA, d_backing_inode(dentry), |
| <------><------><------><------> dentry->d_name.name, "appraise_metadata", |
| <------><------><------><------> integrity_status_msg[evm_status], |
| <------><------><------><------> -EPERM, 0); |
| <------>return evm_status == INTEGRITY_PASS ? 0 : -EPERM; |
| } |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| int evm_inode_setxattr(struct dentry *dentry, const char *xattr_name, |
| <------><------> const void *xattr_value, size_t xattr_value_len) |
| { |
| <------>const struct evm_ima_xattr_data *xattr_data = xattr_value; |
| |
| <------> |
| <------> * there's no HMAC key loaded |
| <------> */ |
| <------>if (evm_initialized & EVM_ALLOW_METADATA_WRITES) |
| <------><------>return 0; |
| |
| <------>if (strcmp(xattr_name, XATTR_NAME_EVM) == 0) { |
| <------><------>if (!xattr_value_len) |
| <------><------><------>return -EINVAL; |
| <------><------>if (xattr_data->type != EVM_IMA_XATTR_DIGSIG && |
| <------><------> xattr_data->type != EVM_XATTR_PORTABLE_DIGSIG) |
| <------><------><------>return -EPERM; |
| <------>} |
| <------>return evm_protect_xattr(dentry, xattr_name, xattr_value, |
| <------><------><------><------> xattr_value_len); |
| } |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| int evm_inode_removexattr(struct dentry *dentry, const char *xattr_name) |
| { |
| <------> |
| <------> * there's no HMAC key loaded |
| <------> */ |
| <------>if (evm_initialized & EVM_ALLOW_METADATA_WRITES) |
| <------><------>return 0; |
| |
| <------>return evm_protect_xattr(dentry, xattr_name, NULL, 0); |
| } |
| |
| static void evm_reset_status(struct inode *inode) |
| { |
| <------>struct integrity_iint_cache *iint; |
| |
| <------>iint = integrity_iint_find(inode); |
| <------>if (iint) |
| <------><------>iint->evm_status = INTEGRITY_UNKNOWN; |
| } |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| void evm_inode_post_setxattr(struct dentry *dentry, const char *xattr_name, |
| <------><------><------> const void *xattr_value, size_t xattr_value_len) |
| { |
| <------>if (!evm_key_loaded() || (!evm_protected_xattr(xattr_name) |
| <------><------><------><------> && !posix_xattr_acl(xattr_name))) |
| <------><------>return; |
| |
| <------>evm_reset_status(dentry->d_inode); |
| |
| <------>evm_update_evmxattr(dentry, xattr_name, xattr_value, xattr_value_len); |
| } |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| void evm_inode_post_removexattr(struct dentry *dentry, const char *xattr_name) |
| { |
| <------>if (!evm_key_loaded() || !evm_protected_xattr(xattr_name)) |
| <------><------>return; |
| |
| <------>evm_reset_status(dentry->d_inode); |
| |
| <------>evm_update_evmxattr(dentry, xattr_name, NULL, 0); |
| } |
| |
| |
| |
| |
| |
| |
| |
| |
| int evm_inode_setattr(struct dentry *dentry, struct iattr *attr) |
| { |
| <------>unsigned int ia_valid = attr->ia_valid; |
| <------>enum integrity_status evm_status; |
| |
| <------> |
| <------> * there's no HMAC key loaded |
| <------> */ |
| <------>if (evm_initialized & EVM_ALLOW_METADATA_WRITES) |
| <------><------>return 0; |
| |
| <------>if (!(ia_valid & (ATTR_MODE | ATTR_UID | ATTR_GID))) |
| <------><------>return 0; |
| <------>evm_status = evm_verify_current_integrity(dentry); |
| <------>if ((evm_status == INTEGRITY_PASS) || |
| <------> (evm_status == INTEGRITY_NOXATTRS)) |
| <------><------>return 0; |
| <------>integrity_audit_msg(AUDIT_INTEGRITY_METADATA, d_backing_inode(dentry), |
| <------><------><------> dentry->d_name.name, "appraise_metadata", |
| <------><------><------> integrity_status_msg[evm_status], -EPERM, 0); |
| <------>return -EPERM; |
| } |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| void evm_inode_post_setattr(struct dentry *dentry, int ia_valid) |
| { |
| <------>if (!evm_key_loaded()) |
| <------><------>return; |
| |
| <------>if (ia_valid & (ATTR_MODE | ATTR_UID | ATTR_GID)) |
| <------><------>evm_update_evmxattr(dentry, NULL, NULL, 0); |
| } |
| |
| |
| |
| |
| int evm_inode_init_security(struct inode *inode, |
| <------><------><------><------> const struct xattr *lsm_xattr, |
| <------><------><------><------> struct xattr *evm_xattr) |
| { |
| <------>struct evm_xattr *xattr_data; |
| <------>int rc; |
| |
| <------>if (!(evm_initialized & EVM_INIT_HMAC) || |
| <------> !evm_protected_xattr(lsm_xattr->name)) |
| <------><------>return 0; |
| |
| <------>xattr_data = kzalloc(sizeof(*xattr_data), GFP_NOFS); |
| <------>if (!xattr_data) |
| <------><------>return -ENOMEM; |
| |
| <------>xattr_data->data.type = EVM_XATTR_HMAC; |
| <------>rc = evm_init_hmac(inode, lsm_xattr, xattr_data->digest); |
| <------>if (rc < 0) |
| <------><------>goto out; |
| |
| <------>evm_xattr->value = xattr_data; |
| <------>evm_xattr->value_len = sizeof(*xattr_data); |
| <------>evm_xattr->name = XATTR_EVM_SUFFIX; |
| <------>return 0; |
| out: |
| <------>kfree(xattr_data); |
| <------>return rc; |
| } |
| EXPORT_SYMBOL_GPL(evm_inode_init_security); |
| |
| #ifdef CONFIG_EVM_LOAD_X509 |
| void __init evm_load_x509(void) |
| { |
| <------>int rc; |
| |
| <------>rc = integrity_load_x509(INTEGRITY_KEYRING_EVM, CONFIG_EVM_X509_PATH); |
| <------>if (!rc) |
| <------><------>evm_initialized |= EVM_INIT_X509; |
| } |
| #endif |
| |
| static int __init init_evm(void) |
| { |
| <------>int error; |
| <------>struct list_head *pos, *q; |
| |
| <------>evm_init_config(); |
| |
| <------>error = integrity_init_keyring(INTEGRITY_KEYRING_EVM); |
| <------>if (error) |
| <------><------>goto error; |
| |
| <------>error = evm_init_secfs(); |
| <------>if (error < 0) { |
| <------><------>pr_info("Error registering secfs\n"); |
| <------><------>goto error; |
| <------>} |
| |
| error: |
| <------>if (error != 0) { |
| <------><------>if (!list_empty(&evm_config_xattrnames)) { |
| <------><------><------>list_for_each_safe(pos, q, &evm_config_xattrnames) |
| <------><------><------><------>list_del(pos); |
| <------><------>} |
| <------>} |
| |
| <------>return error; |
| } |
| |
| late_initcall(init_evm); |
| |