// SPDX-License-Identifier: GPL-2.0-only
/*
* Copyright (C) 2005-2010 IBM Corporation
*
* Author:
* Mimi Zohar <zohar@us.ibm.com>
* Kylene Hall <kjhall@us.ibm.com>
*
* File: evm_main.c
* implements evm_inode_setxattr, evm_inode_post_setxattr,
* evm_inode_removexattr, evm_verifyxattr, and evm_inode_set_acl.
*/
#define pr_fmt(fmt) "EVM: "fmt
#include <linux/init.h>
#include <linux/audit.h>
#include <linux/xattr.h>
#include <linux/integrity.h>
#include <linux/evm.h>
#include <linux/magic.h>
#include <linux/posix_acl_xattr.h>
#include <linux/lsm_hooks.h>
#include <crypto/hash.h>
#include <crypto/hash_info.h>
#include <crypto/utils.h>
#include "evm.h"
int evm_initialized;
static const char * const integrity_status_msg[] = {
"pass", "pass_immutable", "fail", "fail_immutable", "no_label",
"no_xattrs", "unknown"
};
int evm_hmac_attrs;
static struct xattr_list evm_config_default_xattrnames[] = {
{
.name = XATTR_NAME_SELINUX,
.enabled = IS_ENABLED(CONFIG_SECURITY_SELINUX)
},
{
.name = XATTR_NAME_SMACK,
.enabled = IS_ENABLED(CONFIG_SECURITY_SMACK)
},
{
.name = XATTR_NAME_SMACKEXEC,
.enabled = IS_ENABLED(CONFIG_EVM_EXTRA_SMACK_XATTRS)
},
{
.name = XATTR_NAME_SMACKTRANSMUTE,
.enabled = IS_ENABLED(CONFIG_EVM_EXTRA_SMACK_XATTRS)
},
{
.name = XATTR_NAME_SMACKMMAP,
.enabled = IS_ENABLED(CONFIG_EVM_EXTRA_SMACK_XATTRS)
},
{
.name = XATTR_NAME_APPARMOR,
.enabled = IS_ENABLED(CONFIG_SECURITY_APPARMOR)
},
{
.name = XATTR_NAME_IMA,
.enabled = IS_ENABLED(CONFIG_IMA_APPRAISE)
},
{
.name = XATTR_NAME_CAPS,
.enabled = true
},
};
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%s\n", evm_config_default_xattrnames[i].name,
!evm_config_default_xattrnames[i].enabled ?
" (disabled)" : "");
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);
}
/*
* This function determines whether or not it is safe to ignore verification
* errors, based on the ability of EVM to calculate HMACs. If the HMAC key
* is not loaded, and it cannot be loaded in the future due to the
* EVM_SETUP_COMPLETE initialization flag, allowing an operation despite the
* attrs/xattrs being found invalid will not make them valid.
*/
static bool evm_hmac_disabled(void)
{
if (evm_initialized & EVM_INIT_HMAC)
return false;
if (!(evm_initialized & EVM_SETUP_COMPLETE))
return false;
return true;
}
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);
if (error < 0) {
if (error == -ENODATA)
continue;
return error;
}
count++;
}
return count;
}
static int is_unsupported_hmac_fs(struct dentry *dentry)
{
struct inode *inode = d_backing_inode(dentry);
if (inode->i_sb->s_iflags & SB_I_EVM_HMAC_UNSUPPORTED) {
pr_info_once(