162306a36Sopenharmony_ci// SPDX-License-Identifier: GPL-2.0-only 262306a36Sopenharmony_ci/* 362306a36Sopenharmony_ci * Copyright (C) 2011 IBM Corporation 462306a36Sopenharmony_ci * 562306a36Sopenharmony_ci * Author: 662306a36Sopenharmony_ci * Mimi Zohar <zohar@us.ibm.com> 762306a36Sopenharmony_ci */ 862306a36Sopenharmony_ci#include <linux/module.h> 962306a36Sopenharmony_ci#include <linux/init.h> 1062306a36Sopenharmony_ci#include <linux/file.h> 1162306a36Sopenharmony_ci#include <linux/fs.h> 1262306a36Sopenharmony_ci#include <linux/xattr.h> 1362306a36Sopenharmony_ci#include <linux/magic.h> 1462306a36Sopenharmony_ci#include <linux/ima.h> 1562306a36Sopenharmony_ci#include <linux/evm.h> 1662306a36Sopenharmony_ci#include <linux/fsverity.h> 1762306a36Sopenharmony_ci#include <keys/system_keyring.h> 1862306a36Sopenharmony_ci#include <uapi/linux/fsverity.h> 1962306a36Sopenharmony_ci 2062306a36Sopenharmony_ci#include "ima.h" 2162306a36Sopenharmony_ci 2262306a36Sopenharmony_ci#ifdef CONFIG_IMA_APPRAISE_BOOTPARAM 2362306a36Sopenharmony_cistatic char *ima_appraise_cmdline_default __initdata; 2462306a36Sopenharmony_cicore_param(ima_appraise, ima_appraise_cmdline_default, charp, 0); 2562306a36Sopenharmony_ci 2662306a36Sopenharmony_civoid __init ima_appraise_parse_cmdline(void) 2762306a36Sopenharmony_ci{ 2862306a36Sopenharmony_ci const char *str = ima_appraise_cmdline_default; 2962306a36Sopenharmony_ci bool sb_state = arch_ima_get_secureboot(); 3062306a36Sopenharmony_ci int appraisal_state = ima_appraise; 3162306a36Sopenharmony_ci 3262306a36Sopenharmony_ci if (!str) 3362306a36Sopenharmony_ci return; 3462306a36Sopenharmony_ci 3562306a36Sopenharmony_ci if (strncmp(str, "off", 3) == 0) 3662306a36Sopenharmony_ci appraisal_state = 0; 3762306a36Sopenharmony_ci else if (strncmp(str, "log", 3) == 0) 3862306a36Sopenharmony_ci appraisal_state = IMA_APPRAISE_LOG; 3962306a36Sopenharmony_ci else if (strncmp(str, "fix", 3) == 0) 4062306a36Sopenharmony_ci appraisal_state = IMA_APPRAISE_FIX; 4162306a36Sopenharmony_ci else if (strncmp(str, "enforce", 7) == 0) 4262306a36Sopenharmony_ci appraisal_state = IMA_APPRAISE_ENFORCE; 4362306a36Sopenharmony_ci else 4462306a36Sopenharmony_ci pr_err("invalid \"%s\" appraise option", str); 4562306a36Sopenharmony_ci 4662306a36Sopenharmony_ci /* If appraisal state was changed, but secure boot is enabled, 4762306a36Sopenharmony_ci * keep its default */ 4862306a36Sopenharmony_ci if (sb_state) { 4962306a36Sopenharmony_ci if (!(appraisal_state & IMA_APPRAISE_ENFORCE)) 5062306a36Sopenharmony_ci pr_info("Secure boot enabled: ignoring ima_appraise=%s option", 5162306a36Sopenharmony_ci str); 5262306a36Sopenharmony_ci } else { 5362306a36Sopenharmony_ci ima_appraise = appraisal_state; 5462306a36Sopenharmony_ci } 5562306a36Sopenharmony_ci} 5662306a36Sopenharmony_ci#endif 5762306a36Sopenharmony_ci 5862306a36Sopenharmony_ci/* 5962306a36Sopenharmony_ci * is_ima_appraise_enabled - return appraise status 6062306a36Sopenharmony_ci * 6162306a36Sopenharmony_ci * Only return enabled, if not in ima_appraise="fix" or "log" modes. 6262306a36Sopenharmony_ci */ 6362306a36Sopenharmony_cibool is_ima_appraise_enabled(void) 6462306a36Sopenharmony_ci{ 6562306a36Sopenharmony_ci return ima_appraise & IMA_APPRAISE_ENFORCE; 6662306a36Sopenharmony_ci} 6762306a36Sopenharmony_ci 6862306a36Sopenharmony_ci/* 6962306a36Sopenharmony_ci * ima_must_appraise - set appraise flag 7062306a36Sopenharmony_ci * 7162306a36Sopenharmony_ci * Return 1 to appraise or hash 7262306a36Sopenharmony_ci */ 7362306a36Sopenharmony_ciint ima_must_appraise(struct mnt_idmap *idmap, struct inode *inode, 7462306a36Sopenharmony_ci int mask, enum ima_hooks func) 7562306a36Sopenharmony_ci{ 7662306a36Sopenharmony_ci u32 secid; 7762306a36Sopenharmony_ci 7862306a36Sopenharmony_ci if (!ima_appraise) 7962306a36Sopenharmony_ci return 0; 8062306a36Sopenharmony_ci 8162306a36Sopenharmony_ci security_current_getsecid_subj(&secid); 8262306a36Sopenharmony_ci return ima_match_policy(idmap, inode, current_cred(), secid, 8362306a36Sopenharmony_ci func, mask, IMA_APPRAISE | IMA_HASH, NULL, 8462306a36Sopenharmony_ci NULL, NULL, NULL); 8562306a36Sopenharmony_ci} 8662306a36Sopenharmony_ci 8762306a36Sopenharmony_cistatic int ima_fix_xattr(struct dentry *dentry, 8862306a36Sopenharmony_ci struct integrity_iint_cache *iint) 8962306a36Sopenharmony_ci{ 9062306a36Sopenharmony_ci int rc, offset; 9162306a36Sopenharmony_ci u8 algo = iint->ima_hash->algo; 9262306a36Sopenharmony_ci 9362306a36Sopenharmony_ci if (algo <= HASH_ALGO_SHA1) { 9462306a36Sopenharmony_ci offset = 1; 9562306a36Sopenharmony_ci iint->ima_hash->xattr.sha1.type = IMA_XATTR_DIGEST; 9662306a36Sopenharmony_ci } else { 9762306a36Sopenharmony_ci offset = 0; 9862306a36Sopenharmony_ci iint->ima_hash->xattr.ng.type = IMA_XATTR_DIGEST_NG; 9962306a36Sopenharmony_ci iint->ima_hash->xattr.ng.algo = algo; 10062306a36Sopenharmony_ci } 10162306a36Sopenharmony_ci rc = __vfs_setxattr_noperm(&nop_mnt_idmap, dentry, XATTR_NAME_IMA, 10262306a36Sopenharmony_ci &iint->ima_hash->xattr.data[offset], 10362306a36Sopenharmony_ci (sizeof(iint->ima_hash->xattr) - offset) + 10462306a36Sopenharmony_ci iint->ima_hash->length, 0); 10562306a36Sopenharmony_ci return rc; 10662306a36Sopenharmony_ci} 10762306a36Sopenharmony_ci 10862306a36Sopenharmony_ci/* Return specific func appraised cached result */ 10962306a36Sopenharmony_cienum integrity_status ima_get_cache_status(struct integrity_iint_cache *iint, 11062306a36Sopenharmony_ci enum ima_hooks func) 11162306a36Sopenharmony_ci{ 11262306a36Sopenharmony_ci switch (func) { 11362306a36Sopenharmony_ci case MMAP_CHECK: 11462306a36Sopenharmony_ci case MMAP_CHECK_REQPROT: 11562306a36Sopenharmony_ci return iint->ima_mmap_status; 11662306a36Sopenharmony_ci case BPRM_CHECK: 11762306a36Sopenharmony_ci return iint->ima_bprm_status; 11862306a36Sopenharmony_ci case CREDS_CHECK: 11962306a36Sopenharmony_ci return iint->ima_creds_status; 12062306a36Sopenharmony_ci case FILE_CHECK: 12162306a36Sopenharmony_ci case POST_SETATTR: 12262306a36Sopenharmony_ci return iint->ima_file_status; 12362306a36Sopenharmony_ci case MODULE_CHECK ... MAX_CHECK - 1: 12462306a36Sopenharmony_ci default: 12562306a36Sopenharmony_ci return iint->ima_read_status; 12662306a36Sopenharmony_ci } 12762306a36Sopenharmony_ci} 12862306a36Sopenharmony_ci 12962306a36Sopenharmony_cistatic void ima_set_cache_status(struct integrity_iint_cache *iint, 13062306a36Sopenharmony_ci enum ima_hooks func, 13162306a36Sopenharmony_ci enum integrity_status status) 13262306a36Sopenharmony_ci{ 13362306a36Sopenharmony_ci switch (func) { 13462306a36Sopenharmony_ci case MMAP_CHECK: 13562306a36Sopenharmony_ci case MMAP_CHECK_REQPROT: 13662306a36Sopenharmony_ci iint->ima_mmap_status = status; 13762306a36Sopenharmony_ci break; 13862306a36Sopenharmony_ci case BPRM_CHECK: 13962306a36Sopenharmony_ci iint->ima_bprm_status = status; 14062306a36Sopenharmony_ci break; 14162306a36Sopenharmony_ci case CREDS_CHECK: 14262306a36Sopenharmony_ci iint->ima_creds_status = status; 14362306a36Sopenharmony_ci break; 14462306a36Sopenharmony_ci case FILE_CHECK: 14562306a36Sopenharmony_ci case POST_SETATTR: 14662306a36Sopenharmony_ci iint->ima_file_status = status; 14762306a36Sopenharmony_ci break; 14862306a36Sopenharmony_ci case MODULE_CHECK ... MAX_CHECK - 1: 14962306a36Sopenharmony_ci default: 15062306a36Sopenharmony_ci iint->ima_read_status = status; 15162306a36Sopenharmony_ci break; 15262306a36Sopenharmony_ci } 15362306a36Sopenharmony_ci} 15462306a36Sopenharmony_ci 15562306a36Sopenharmony_cistatic void ima_cache_flags(struct integrity_iint_cache *iint, 15662306a36Sopenharmony_ci enum ima_hooks func) 15762306a36Sopenharmony_ci{ 15862306a36Sopenharmony_ci switch (func) { 15962306a36Sopenharmony_ci case MMAP_CHECK: 16062306a36Sopenharmony_ci case MMAP_CHECK_REQPROT: 16162306a36Sopenharmony_ci iint->flags |= (IMA_MMAP_APPRAISED | IMA_APPRAISED); 16262306a36Sopenharmony_ci break; 16362306a36Sopenharmony_ci case BPRM_CHECK: 16462306a36Sopenharmony_ci iint->flags |= (IMA_BPRM_APPRAISED | IMA_APPRAISED); 16562306a36Sopenharmony_ci break; 16662306a36Sopenharmony_ci case CREDS_CHECK: 16762306a36Sopenharmony_ci iint->flags |= (IMA_CREDS_APPRAISED | IMA_APPRAISED); 16862306a36Sopenharmony_ci break; 16962306a36Sopenharmony_ci case FILE_CHECK: 17062306a36Sopenharmony_ci case POST_SETATTR: 17162306a36Sopenharmony_ci iint->flags |= (IMA_FILE_APPRAISED | IMA_APPRAISED); 17262306a36Sopenharmony_ci break; 17362306a36Sopenharmony_ci case MODULE_CHECK ... MAX_CHECK - 1: 17462306a36Sopenharmony_ci default: 17562306a36Sopenharmony_ci iint->flags |= (IMA_READ_APPRAISED | IMA_APPRAISED); 17662306a36Sopenharmony_ci break; 17762306a36Sopenharmony_ci } 17862306a36Sopenharmony_ci} 17962306a36Sopenharmony_ci 18062306a36Sopenharmony_cienum hash_algo ima_get_hash_algo(const struct evm_ima_xattr_data *xattr_value, 18162306a36Sopenharmony_ci int xattr_len) 18262306a36Sopenharmony_ci{ 18362306a36Sopenharmony_ci struct signature_v2_hdr *sig; 18462306a36Sopenharmony_ci enum hash_algo ret; 18562306a36Sopenharmony_ci 18662306a36Sopenharmony_ci if (!xattr_value || xattr_len < 2) 18762306a36Sopenharmony_ci /* return default hash algo */ 18862306a36Sopenharmony_ci return ima_hash_algo; 18962306a36Sopenharmony_ci 19062306a36Sopenharmony_ci switch (xattr_value->type) { 19162306a36Sopenharmony_ci case IMA_VERITY_DIGSIG: 19262306a36Sopenharmony_ci sig = (typeof(sig))xattr_value; 19362306a36Sopenharmony_ci if (sig->version != 3 || xattr_len <= sizeof(*sig) || 19462306a36Sopenharmony_ci sig->hash_algo >= HASH_ALGO__LAST) 19562306a36Sopenharmony_ci return ima_hash_algo; 19662306a36Sopenharmony_ci return sig->hash_algo; 19762306a36Sopenharmony_ci case EVM_IMA_XATTR_DIGSIG: 19862306a36Sopenharmony_ci sig = (typeof(sig))xattr_value; 19962306a36Sopenharmony_ci if (sig->version != 2 || xattr_len <= sizeof(*sig) 20062306a36Sopenharmony_ci || sig->hash_algo >= HASH_ALGO__LAST) 20162306a36Sopenharmony_ci return ima_hash_algo; 20262306a36Sopenharmony_ci return sig->hash_algo; 20362306a36Sopenharmony_ci case IMA_XATTR_DIGEST_NG: 20462306a36Sopenharmony_ci /* first byte contains algorithm id */ 20562306a36Sopenharmony_ci ret = xattr_value->data[0]; 20662306a36Sopenharmony_ci if (ret < HASH_ALGO__LAST) 20762306a36Sopenharmony_ci return ret; 20862306a36Sopenharmony_ci break; 20962306a36Sopenharmony_ci case IMA_XATTR_DIGEST: 21062306a36Sopenharmony_ci /* this is for backward compatibility */ 21162306a36Sopenharmony_ci if (xattr_len == 21) { 21262306a36Sopenharmony_ci unsigned int zero = 0; 21362306a36Sopenharmony_ci if (!memcmp(&xattr_value->data[16], &zero, 4)) 21462306a36Sopenharmony_ci return HASH_ALGO_MD5; 21562306a36Sopenharmony_ci else 21662306a36Sopenharmony_ci return HASH_ALGO_SHA1; 21762306a36Sopenharmony_ci } else if (xattr_len == 17) 21862306a36Sopenharmony_ci return HASH_ALGO_MD5; 21962306a36Sopenharmony_ci break; 22062306a36Sopenharmony_ci } 22162306a36Sopenharmony_ci 22262306a36Sopenharmony_ci /* return default hash algo */ 22362306a36Sopenharmony_ci return ima_hash_algo; 22462306a36Sopenharmony_ci} 22562306a36Sopenharmony_ci 22662306a36Sopenharmony_ciint ima_read_xattr(struct dentry *dentry, 22762306a36Sopenharmony_ci struct evm_ima_xattr_data **xattr_value, int xattr_len) 22862306a36Sopenharmony_ci{ 22962306a36Sopenharmony_ci int ret; 23062306a36Sopenharmony_ci 23162306a36Sopenharmony_ci ret = vfs_getxattr_alloc(&nop_mnt_idmap, dentry, XATTR_NAME_IMA, 23262306a36Sopenharmony_ci (char **)xattr_value, xattr_len, GFP_NOFS); 23362306a36Sopenharmony_ci if (ret == -EOPNOTSUPP) 23462306a36Sopenharmony_ci ret = 0; 23562306a36Sopenharmony_ci return ret; 23662306a36Sopenharmony_ci} 23762306a36Sopenharmony_ci 23862306a36Sopenharmony_ci/* 23962306a36Sopenharmony_ci * calc_file_id_hash - calculate the hash of the ima_file_id struct data 24062306a36Sopenharmony_ci * @type: xattr type [enum evm_ima_xattr_type] 24162306a36Sopenharmony_ci * @algo: hash algorithm [enum hash_algo] 24262306a36Sopenharmony_ci * @digest: pointer to the digest to be hashed 24362306a36Sopenharmony_ci * @hash: (out) pointer to the hash 24462306a36Sopenharmony_ci * 24562306a36Sopenharmony_ci * IMA signature version 3 disambiguates the data that is signed by 24662306a36Sopenharmony_ci * indirectly signing the hash of the ima_file_id structure data. 24762306a36Sopenharmony_ci * 24862306a36Sopenharmony_ci * Signing the ima_file_id struct is currently only supported for 24962306a36Sopenharmony_ci * IMA_VERITY_DIGSIG type xattrs. 25062306a36Sopenharmony_ci * 25162306a36Sopenharmony_ci * Return 0 on success, error code otherwise. 25262306a36Sopenharmony_ci */ 25362306a36Sopenharmony_cistatic int calc_file_id_hash(enum evm_ima_xattr_type type, 25462306a36Sopenharmony_ci enum hash_algo algo, const u8 *digest, 25562306a36Sopenharmony_ci struct ima_digest_data *hash) 25662306a36Sopenharmony_ci{ 25762306a36Sopenharmony_ci struct ima_file_id file_id = { 25862306a36Sopenharmony_ci .hash_type = IMA_VERITY_DIGSIG, .hash_algorithm = algo}; 25962306a36Sopenharmony_ci unsigned int unused = HASH_MAX_DIGESTSIZE - hash_digest_size[algo]; 26062306a36Sopenharmony_ci 26162306a36Sopenharmony_ci if (type != IMA_VERITY_DIGSIG) 26262306a36Sopenharmony_ci return -EINVAL; 26362306a36Sopenharmony_ci 26462306a36Sopenharmony_ci memcpy(file_id.hash, digest, hash_digest_size[algo]); 26562306a36Sopenharmony_ci 26662306a36Sopenharmony_ci hash->algo = algo; 26762306a36Sopenharmony_ci hash->length = hash_digest_size[algo]; 26862306a36Sopenharmony_ci 26962306a36Sopenharmony_ci return ima_calc_buffer_hash(&file_id, sizeof(file_id) - unused, hash); 27062306a36Sopenharmony_ci} 27162306a36Sopenharmony_ci 27262306a36Sopenharmony_ci/* 27362306a36Sopenharmony_ci * xattr_verify - verify xattr digest or signature 27462306a36Sopenharmony_ci * 27562306a36Sopenharmony_ci * Verify whether the hash or signature matches the file contents. 27662306a36Sopenharmony_ci * 27762306a36Sopenharmony_ci * Return 0 on success, error code otherwise. 27862306a36Sopenharmony_ci */ 27962306a36Sopenharmony_cistatic int xattr_verify(enum ima_hooks func, struct integrity_iint_cache *iint, 28062306a36Sopenharmony_ci struct evm_ima_xattr_data *xattr_value, int xattr_len, 28162306a36Sopenharmony_ci enum integrity_status *status, const char **cause) 28262306a36Sopenharmony_ci{ 28362306a36Sopenharmony_ci struct ima_max_digest_data hash; 28462306a36Sopenharmony_ci struct signature_v2_hdr *sig; 28562306a36Sopenharmony_ci int rc = -EINVAL, hash_start = 0; 28662306a36Sopenharmony_ci int mask; 28762306a36Sopenharmony_ci 28862306a36Sopenharmony_ci switch (xattr_value->type) { 28962306a36Sopenharmony_ci case IMA_XATTR_DIGEST_NG: 29062306a36Sopenharmony_ci /* first byte contains algorithm id */ 29162306a36Sopenharmony_ci hash_start = 1; 29262306a36Sopenharmony_ci fallthrough; 29362306a36Sopenharmony_ci case IMA_XATTR_DIGEST: 29462306a36Sopenharmony_ci if (*status != INTEGRITY_PASS_IMMUTABLE) { 29562306a36Sopenharmony_ci if (iint->flags & IMA_DIGSIG_REQUIRED) { 29662306a36Sopenharmony_ci if (iint->flags & IMA_VERITY_REQUIRED) 29762306a36Sopenharmony_ci *cause = "verity-signature-required"; 29862306a36Sopenharmony_ci else 29962306a36Sopenharmony_ci *cause = "IMA-signature-required"; 30062306a36Sopenharmony_ci *status = INTEGRITY_FAIL; 30162306a36Sopenharmony_ci break; 30262306a36Sopenharmony_ci } 30362306a36Sopenharmony_ci clear_bit(IMA_DIGSIG, &iint->atomic_flags); 30462306a36Sopenharmony_ci } else { 30562306a36Sopenharmony_ci set_bit(IMA_DIGSIG, &iint->atomic_flags); 30662306a36Sopenharmony_ci } 30762306a36Sopenharmony_ci if (xattr_len - sizeof(xattr_value->type) - hash_start >= 30862306a36Sopenharmony_ci iint->ima_hash->length) 30962306a36Sopenharmony_ci /* 31062306a36Sopenharmony_ci * xattr length may be longer. md5 hash in previous 31162306a36Sopenharmony_ci * version occupied 20 bytes in xattr, instead of 16 31262306a36Sopenharmony_ci */ 31362306a36Sopenharmony_ci rc = memcmp(&xattr_value->data[hash_start], 31462306a36Sopenharmony_ci iint->ima_hash->digest, 31562306a36Sopenharmony_ci iint->ima_hash->length); 31662306a36Sopenharmony_ci else 31762306a36Sopenharmony_ci rc = -EINVAL; 31862306a36Sopenharmony_ci if (rc) { 31962306a36Sopenharmony_ci *cause = "invalid-hash"; 32062306a36Sopenharmony_ci *status = INTEGRITY_FAIL; 32162306a36Sopenharmony_ci break; 32262306a36Sopenharmony_ci } 32362306a36Sopenharmony_ci *status = INTEGRITY_PASS; 32462306a36Sopenharmony_ci break; 32562306a36Sopenharmony_ci case EVM_IMA_XATTR_DIGSIG: 32662306a36Sopenharmony_ci set_bit(IMA_DIGSIG, &iint->atomic_flags); 32762306a36Sopenharmony_ci 32862306a36Sopenharmony_ci mask = IMA_DIGSIG_REQUIRED | IMA_VERITY_REQUIRED; 32962306a36Sopenharmony_ci if ((iint->flags & mask) == mask) { 33062306a36Sopenharmony_ci *cause = "verity-signature-required"; 33162306a36Sopenharmony_ci *status = INTEGRITY_FAIL; 33262306a36Sopenharmony_ci break; 33362306a36Sopenharmony_ci } 33462306a36Sopenharmony_ci 33562306a36Sopenharmony_ci sig = (typeof(sig))xattr_value; 33662306a36Sopenharmony_ci if (sig->version >= 3) { 33762306a36Sopenharmony_ci *cause = "invalid-signature-version"; 33862306a36Sopenharmony_ci *status = INTEGRITY_FAIL; 33962306a36Sopenharmony_ci break; 34062306a36Sopenharmony_ci } 34162306a36Sopenharmony_ci rc = integrity_digsig_verify(INTEGRITY_KEYRING_IMA, 34262306a36Sopenharmony_ci (const char *)xattr_value, 34362306a36Sopenharmony_ci xattr_len, 34462306a36Sopenharmony_ci iint->ima_hash->digest, 34562306a36Sopenharmony_ci iint->ima_hash->length); 34662306a36Sopenharmony_ci if (rc == -EOPNOTSUPP) { 34762306a36Sopenharmony_ci *status = INTEGRITY_UNKNOWN; 34862306a36Sopenharmony_ci break; 34962306a36Sopenharmony_ci } 35062306a36Sopenharmony_ci if (IS_ENABLED(CONFIG_INTEGRITY_PLATFORM_KEYRING) && rc && 35162306a36Sopenharmony_ci func == KEXEC_KERNEL_CHECK) 35262306a36Sopenharmony_ci rc = integrity_digsig_verify(INTEGRITY_KEYRING_PLATFORM, 35362306a36Sopenharmony_ci (const char *)xattr_value, 35462306a36Sopenharmony_ci xattr_len, 35562306a36Sopenharmony_ci iint->ima_hash->digest, 35662306a36Sopenharmony_ci iint->ima_hash->length); 35762306a36Sopenharmony_ci if (rc) { 35862306a36Sopenharmony_ci *cause = "invalid-signature"; 35962306a36Sopenharmony_ci *status = INTEGRITY_FAIL; 36062306a36Sopenharmony_ci } else { 36162306a36Sopenharmony_ci *status = INTEGRITY_PASS; 36262306a36Sopenharmony_ci } 36362306a36Sopenharmony_ci break; 36462306a36Sopenharmony_ci case IMA_VERITY_DIGSIG: 36562306a36Sopenharmony_ci set_bit(IMA_DIGSIG, &iint->atomic_flags); 36662306a36Sopenharmony_ci 36762306a36Sopenharmony_ci if (iint->flags & IMA_DIGSIG_REQUIRED) { 36862306a36Sopenharmony_ci if (!(iint->flags & IMA_VERITY_REQUIRED)) { 36962306a36Sopenharmony_ci *cause = "IMA-signature-required"; 37062306a36Sopenharmony_ci *status = INTEGRITY_FAIL; 37162306a36Sopenharmony_ci break; 37262306a36Sopenharmony_ci } 37362306a36Sopenharmony_ci } 37462306a36Sopenharmony_ci 37562306a36Sopenharmony_ci sig = (typeof(sig))xattr_value; 37662306a36Sopenharmony_ci if (sig->version != 3) { 37762306a36Sopenharmony_ci *cause = "invalid-signature-version"; 37862306a36Sopenharmony_ci *status = INTEGRITY_FAIL; 37962306a36Sopenharmony_ci break; 38062306a36Sopenharmony_ci } 38162306a36Sopenharmony_ci 38262306a36Sopenharmony_ci rc = calc_file_id_hash(IMA_VERITY_DIGSIG, iint->ima_hash->algo, 38362306a36Sopenharmony_ci iint->ima_hash->digest, &hash.hdr); 38462306a36Sopenharmony_ci if (rc) { 38562306a36Sopenharmony_ci *cause = "sigv3-hashing-error"; 38662306a36Sopenharmony_ci *status = INTEGRITY_FAIL; 38762306a36Sopenharmony_ci break; 38862306a36Sopenharmony_ci } 38962306a36Sopenharmony_ci 39062306a36Sopenharmony_ci rc = integrity_digsig_verify(INTEGRITY_KEYRING_IMA, 39162306a36Sopenharmony_ci (const char *)xattr_value, 39262306a36Sopenharmony_ci xattr_len, hash.digest, 39362306a36Sopenharmony_ci hash.hdr.length); 39462306a36Sopenharmony_ci if (rc) { 39562306a36Sopenharmony_ci *cause = "invalid-verity-signature"; 39662306a36Sopenharmony_ci *status = INTEGRITY_FAIL; 39762306a36Sopenharmony_ci } else { 39862306a36Sopenharmony_ci *status = INTEGRITY_PASS; 39962306a36Sopenharmony_ci } 40062306a36Sopenharmony_ci 40162306a36Sopenharmony_ci break; 40262306a36Sopenharmony_ci default: 40362306a36Sopenharmony_ci *status = INTEGRITY_UNKNOWN; 40462306a36Sopenharmony_ci *cause = "unknown-ima-data"; 40562306a36Sopenharmony_ci break; 40662306a36Sopenharmony_ci } 40762306a36Sopenharmony_ci 40862306a36Sopenharmony_ci return rc; 40962306a36Sopenharmony_ci} 41062306a36Sopenharmony_ci 41162306a36Sopenharmony_ci/* 41262306a36Sopenharmony_ci * modsig_verify - verify modsig signature 41362306a36Sopenharmony_ci * 41462306a36Sopenharmony_ci * Verify whether the signature matches the file contents. 41562306a36Sopenharmony_ci * 41662306a36Sopenharmony_ci * Return 0 on success, error code otherwise. 41762306a36Sopenharmony_ci */ 41862306a36Sopenharmony_cistatic int modsig_verify(enum ima_hooks func, const struct modsig *modsig, 41962306a36Sopenharmony_ci enum integrity_status *status, const char **cause) 42062306a36Sopenharmony_ci{ 42162306a36Sopenharmony_ci int rc; 42262306a36Sopenharmony_ci 42362306a36Sopenharmony_ci rc = integrity_modsig_verify(INTEGRITY_KEYRING_IMA, modsig); 42462306a36Sopenharmony_ci if (IS_ENABLED(CONFIG_INTEGRITY_PLATFORM_KEYRING) && rc && 42562306a36Sopenharmony_ci func == KEXEC_KERNEL_CHECK) 42662306a36Sopenharmony_ci rc = integrity_modsig_verify(INTEGRITY_KEYRING_PLATFORM, 42762306a36Sopenharmony_ci modsig); 42862306a36Sopenharmony_ci if (rc) { 42962306a36Sopenharmony_ci *cause = "invalid-signature"; 43062306a36Sopenharmony_ci *status = INTEGRITY_FAIL; 43162306a36Sopenharmony_ci } else { 43262306a36Sopenharmony_ci *status = INTEGRITY_PASS; 43362306a36Sopenharmony_ci } 43462306a36Sopenharmony_ci 43562306a36Sopenharmony_ci return rc; 43662306a36Sopenharmony_ci} 43762306a36Sopenharmony_ci 43862306a36Sopenharmony_ci/* 43962306a36Sopenharmony_ci * ima_check_blacklist - determine if the binary is blacklisted. 44062306a36Sopenharmony_ci * 44162306a36Sopenharmony_ci * Add the hash of the blacklisted binary to the measurement list, based 44262306a36Sopenharmony_ci * on policy. 44362306a36Sopenharmony_ci * 44462306a36Sopenharmony_ci * Returns -EPERM if the hash is blacklisted. 44562306a36Sopenharmony_ci */ 44662306a36Sopenharmony_ciint ima_check_blacklist(struct integrity_iint_cache *iint, 44762306a36Sopenharmony_ci const struct modsig *modsig, int pcr) 44862306a36Sopenharmony_ci{ 44962306a36Sopenharmony_ci enum hash_algo hash_algo; 45062306a36Sopenharmony_ci const u8 *digest = NULL; 45162306a36Sopenharmony_ci u32 digestsize = 0; 45262306a36Sopenharmony_ci int rc = 0; 45362306a36Sopenharmony_ci 45462306a36Sopenharmony_ci if (!(iint->flags & IMA_CHECK_BLACKLIST)) 45562306a36Sopenharmony_ci return 0; 45662306a36Sopenharmony_ci 45762306a36Sopenharmony_ci if (iint->flags & IMA_MODSIG_ALLOWED && modsig) { 45862306a36Sopenharmony_ci ima_get_modsig_digest(modsig, &hash_algo, &digest, &digestsize); 45962306a36Sopenharmony_ci 46062306a36Sopenharmony_ci rc = is_binary_blacklisted(digest, digestsize); 46162306a36Sopenharmony_ci } else if (iint->flags & IMA_DIGSIG_REQUIRED && iint->ima_hash) 46262306a36Sopenharmony_ci rc = is_binary_blacklisted(iint->ima_hash->digest, iint->ima_hash->length); 46362306a36Sopenharmony_ci 46462306a36Sopenharmony_ci if ((rc == -EPERM) && (iint->flags & IMA_MEASURE)) 46562306a36Sopenharmony_ci process_buffer_measurement(&nop_mnt_idmap, NULL, digest, digestsize, 46662306a36Sopenharmony_ci "blacklisted-hash", NONE, 46762306a36Sopenharmony_ci pcr, NULL, false, NULL, 0); 46862306a36Sopenharmony_ci 46962306a36Sopenharmony_ci return rc; 47062306a36Sopenharmony_ci} 47162306a36Sopenharmony_ci 47262306a36Sopenharmony_ci/* 47362306a36Sopenharmony_ci * ima_appraise_measurement - appraise file measurement 47462306a36Sopenharmony_ci * 47562306a36Sopenharmony_ci * Call evm_verifyxattr() to verify the integrity of 'security.ima'. 47662306a36Sopenharmony_ci * Assuming success, compare the xattr hash with the collected measurement. 47762306a36Sopenharmony_ci * 47862306a36Sopenharmony_ci * Return 0 on success, error code otherwise 47962306a36Sopenharmony_ci */ 48062306a36Sopenharmony_ciint ima_appraise_measurement(enum ima_hooks func, 48162306a36Sopenharmony_ci struct integrity_iint_cache *iint, 48262306a36Sopenharmony_ci struct file *file, const unsigned char *filename, 48362306a36Sopenharmony_ci struct evm_ima_xattr_data *xattr_value, 48462306a36Sopenharmony_ci int xattr_len, const struct modsig *modsig) 48562306a36Sopenharmony_ci{ 48662306a36Sopenharmony_ci static const char op[] = "appraise_data"; 48762306a36Sopenharmony_ci const char *cause = "unknown"; 48862306a36Sopenharmony_ci struct dentry *dentry = file_dentry(file); 48962306a36Sopenharmony_ci struct inode *inode = d_backing_inode(dentry); 49062306a36Sopenharmony_ci enum integrity_status status = INTEGRITY_UNKNOWN; 49162306a36Sopenharmony_ci int rc = xattr_len; 49262306a36Sopenharmony_ci bool try_modsig = iint->flags & IMA_MODSIG_ALLOWED && modsig; 49362306a36Sopenharmony_ci 49462306a36Sopenharmony_ci /* If not appraising a modsig, we need an xattr. */ 49562306a36Sopenharmony_ci if (!(inode->i_opflags & IOP_XATTR) && !try_modsig) 49662306a36Sopenharmony_ci return INTEGRITY_UNKNOWN; 49762306a36Sopenharmony_ci 49862306a36Sopenharmony_ci /* If reading the xattr failed and there's no modsig, error out. */ 49962306a36Sopenharmony_ci if (rc <= 0 && !try_modsig) { 50062306a36Sopenharmony_ci if (rc && rc != -ENODATA) 50162306a36Sopenharmony_ci goto out; 50262306a36Sopenharmony_ci 50362306a36Sopenharmony_ci if (iint->flags & IMA_DIGSIG_REQUIRED) { 50462306a36Sopenharmony_ci if (iint->flags & IMA_VERITY_REQUIRED) 50562306a36Sopenharmony_ci cause = "verity-signature-required"; 50662306a36Sopenharmony_ci else 50762306a36Sopenharmony_ci cause = "IMA-signature-required"; 50862306a36Sopenharmony_ci } else { 50962306a36Sopenharmony_ci cause = "missing-hash"; 51062306a36Sopenharmony_ci } 51162306a36Sopenharmony_ci 51262306a36Sopenharmony_ci status = INTEGRITY_NOLABEL; 51362306a36Sopenharmony_ci if (file->f_mode & FMODE_CREATED) 51462306a36Sopenharmony_ci iint->flags |= IMA_NEW_FILE; 51562306a36Sopenharmony_ci if ((iint->flags & IMA_NEW_FILE) && 51662306a36Sopenharmony_ci (!(iint->flags & IMA_DIGSIG_REQUIRED) || 51762306a36Sopenharmony_ci (inode->i_size == 0))) 51862306a36Sopenharmony_ci status = INTEGRITY_PASS; 51962306a36Sopenharmony_ci goto out; 52062306a36Sopenharmony_ci } 52162306a36Sopenharmony_ci 52262306a36Sopenharmony_ci status = evm_verifyxattr(dentry, XATTR_NAME_IMA, xattr_value, 52362306a36Sopenharmony_ci rc < 0 ? 0 : rc, iint); 52462306a36Sopenharmony_ci switch (status) { 52562306a36Sopenharmony_ci case INTEGRITY_PASS: 52662306a36Sopenharmony_ci case INTEGRITY_PASS_IMMUTABLE: 52762306a36Sopenharmony_ci case INTEGRITY_UNKNOWN: 52862306a36Sopenharmony_ci break; 52962306a36Sopenharmony_ci case INTEGRITY_NOXATTRS: /* No EVM protected xattrs. */ 53062306a36Sopenharmony_ci /* It's fine not to have xattrs when using a modsig. */ 53162306a36Sopenharmony_ci if (try_modsig) 53262306a36Sopenharmony_ci break; 53362306a36Sopenharmony_ci fallthrough; 53462306a36Sopenharmony_ci case INTEGRITY_NOLABEL: /* No security.evm xattr. */ 53562306a36Sopenharmony_ci cause = "missing-HMAC"; 53662306a36Sopenharmony_ci goto out; 53762306a36Sopenharmony_ci case INTEGRITY_FAIL_IMMUTABLE: 53862306a36Sopenharmony_ci set_bit(IMA_DIGSIG, &iint->atomic_flags); 53962306a36Sopenharmony_ci cause = "invalid-fail-immutable"; 54062306a36Sopenharmony_ci goto out; 54162306a36Sopenharmony_ci case INTEGRITY_FAIL: /* Invalid HMAC/signature. */ 54262306a36Sopenharmony_ci cause = "invalid-HMAC"; 54362306a36Sopenharmony_ci goto out; 54462306a36Sopenharmony_ci default: 54562306a36Sopenharmony_ci WARN_ONCE(true, "Unexpected integrity status %d\n", status); 54662306a36Sopenharmony_ci } 54762306a36Sopenharmony_ci 54862306a36Sopenharmony_ci if (xattr_value) 54962306a36Sopenharmony_ci rc = xattr_verify(func, iint, xattr_value, xattr_len, &status, 55062306a36Sopenharmony_ci &cause); 55162306a36Sopenharmony_ci 55262306a36Sopenharmony_ci /* 55362306a36Sopenharmony_ci * If we have a modsig and either no imasig or the imasig's key isn't 55462306a36Sopenharmony_ci * known, then try verifying the modsig. 55562306a36Sopenharmony_ci */ 55662306a36Sopenharmony_ci if (try_modsig && 55762306a36Sopenharmony_ci (!xattr_value || xattr_value->type == IMA_XATTR_DIGEST_NG || 55862306a36Sopenharmony_ci rc == -ENOKEY)) 55962306a36Sopenharmony_ci rc = modsig_verify(func, modsig, &status, &cause); 56062306a36Sopenharmony_ci 56162306a36Sopenharmony_ciout: 56262306a36Sopenharmony_ci /* 56362306a36Sopenharmony_ci * File signatures on some filesystems can not be properly verified. 56462306a36Sopenharmony_ci * When such filesystems are mounted by an untrusted mounter or on a 56562306a36Sopenharmony_ci * system not willing to accept such a risk, fail the file signature 56662306a36Sopenharmony_ci * verification. 56762306a36Sopenharmony_ci */ 56862306a36Sopenharmony_ci if ((inode->i_sb->s_iflags & SB_I_IMA_UNVERIFIABLE_SIGNATURE) && 56962306a36Sopenharmony_ci ((inode->i_sb->s_iflags & SB_I_UNTRUSTED_MOUNTER) || 57062306a36Sopenharmony_ci (iint->flags & IMA_FAIL_UNVERIFIABLE_SIGS))) { 57162306a36Sopenharmony_ci status = INTEGRITY_FAIL; 57262306a36Sopenharmony_ci cause = "unverifiable-signature"; 57362306a36Sopenharmony_ci integrity_audit_msg(AUDIT_INTEGRITY_DATA, inode, filename, 57462306a36Sopenharmony_ci op, cause, rc, 0); 57562306a36Sopenharmony_ci } else if (status != INTEGRITY_PASS) { 57662306a36Sopenharmony_ci /* Fix mode, but don't replace file signatures. */ 57762306a36Sopenharmony_ci if ((ima_appraise & IMA_APPRAISE_FIX) && !try_modsig && 57862306a36Sopenharmony_ci (!xattr_value || 57962306a36Sopenharmony_ci xattr_value->type != EVM_IMA_XATTR_DIGSIG)) { 58062306a36Sopenharmony_ci if (!ima_fix_xattr(dentry, iint)) 58162306a36Sopenharmony_ci status = INTEGRITY_PASS; 58262306a36Sopenharmony_ci } 58362306a36Sopenharmony_ci 58462306a36Sopenharmony_ci /* 58562306a36Sopenharmony_ci * Permit new files with file/EVM portable signatures, but 58662306a36Sopenharmony_ci * without data. 58762306a36Sopenharmony_ci */ 58862306a36Sopenharmony_ci if (inode->i_size == 0 && iint->flags & IMA_NEW_FILE && 58962306a36Sopenharmony_ci test_bit(IMA_DIGSIG, &iint->atomic_flags)) { 59062306a36Sopenharmony_ci status = INTEGRITY_PASS; 59162306a36Sopenharmony_ci } 59262306a36Sopenharmony_ci 59362306a36Sopenharmony_ci integrity_audit_msg(AUDIT_INTEGRITY_DATA, inode, filename, 59462306a36Sopenharmony_ci op, cause, rc, 0); 59562306a36Sopenharmony_ci } else { 59662306a36Sopenharmony_ci ima_cache_flags(iint, func); 59762306a36Sopenharmony_ci } 59862306a36Sopenharmony_ci 59962306a36Sopenharmony_ci ima_set_cache_status(iint, func, status); 60062306a36Sopenharmony_ci return status; 60162306a36Sopenharmony_ci} 60262306a36Sopenharmony_ci 60362306a36Sopenharmony_ci/* 60462306a36Sopenharmony_ci * ima_update_xattr - update 'security.ima' hash value 60562306a36Sopenharmony_ci */ 60662306a36Sopenharmony_civoid ima_update_xattr(struct integrity_iint_cache *iint, struct file *file) 60762306a36Sopenharmony_ci{ 60862306a36Sopenharmony_ci struct dentry *dentry = file_dentry(file); 60962306a36Sopenharmony_ci int rc = 0; 61062306a36Sopenharmony_ci 61162306a36Sopenharmony_ci /* do not collect and update hash for digital signatures */ 61262306a36Sopenharmony_ci if (test_bit(IMA_DIGSIG, &iint->atomic_flags)) 61362306a36Sopenharmony_ci return; 61462306a36Sopenharmony_ci 61562306a36Sopenharmony_ci if ((iint->ima_file_status != INTEGRITY_PASS) && 61662306a36Sopenharmony_ci !(iint->flags & IMA_HASH)) 61762306a36Sopenharmony_ci return; 61862306a36Sopenharmony_ci 61962306a36Sopenharmony_ci rc = ima_collect_measurement(iint, file, NULL, 0, ima_hash_algo, NULL); 62062306a36Sopenharmony_ci if (rc < 0) 62162306a36Sopenharmony_ci return; 62262306a36Sopenharmony_ci 62362306a36Sopenharmony_ci inode_lock(file_inode(file)); 62462306a36Sopenharmony_ci ima_fix_xattr(dentry, iint); 62562306a36Sopenharmony_ci inode_unlock(file_inode(file)); 62662306a36Sopenharmony_ci} 62762306a36Sopenharmony_ci 62862306a36Sopenharmony_ci/** 62962306a36Sopenharmony_ci * ima_inode_post_setattr - reflect file metadata changes 63062306a36Sopenharmony_ci * @idmap: idmap of the mount the inode was found from 63162306a36Sopenharmony_ci * @dentry: pointer to the affected dentry 63262306a36Sopenharmony_ci * 63362306a36Sopenharmony_ci * Changes to a dentry's metadata might result in needing to appraise. 63462306a36Sopenharmony_ci * 63562306a36Sopenharmony_ci * This function is called from notify_change(), which expects the caller 63662306a36Sopenharmony_ci * to lock the inode's i_mutex. 63762306a36Sopenharmony_ci */ 63862306a36Sopenharmony_civoid ima_inode_post_setattr(struct mnt_idmap *idmap, 63962306a36Sopenharmony_ci struct dentry *dentry) 64062306a36Sopenharmony_ci{ 64162306a36Sopenharmony_ci struct inode *inode = d_backing_inode(dentry); 64262306a36Sopenharmony_ci struct integrity_iint_cache *iint; 64362306a36Sopenharmony_ci int action; 64462306a36Sopenharmony_ci 64562306a36Sopenharmony_ci if (!(ima_policy_flag & IMA_APPRAISE) || !S_ISREG(inode->i_mode) 64662306a36Sopenharmony_ci || !(inode->i_opflags & IOP_XATTR)) 64762306a36Sopenharmony_ci return; 64862306a36Sopenharmony_ci 64962306a36Sopenharmony_ci action = ima_must_appraise(idmap, inode, MAY_ACCESS, POST_SETATTR); 65062306a36Sopenharmony_ci iint = integrity_iint_find(inode); 65162306a36Sopenharmony_ci if (iint) { 65262306a36Sopenharmony_ci set_bit(IMA_CHANGE_ATTR, &iint->atomic_flags); 65362306a36Sopenharmony_ci if (!action) 65462306a36Sopenharmony_ci clear_bit(IMA_UPDATE_XATTR, &iint->atomic_flags); 65562306a36Sopenharmony_ci } 65662306a36Sopenharmony_ci} 65762306a36Sopenharmony_ci 65862306a36Sopenharmony_ci/* 65962306a36Sopenharmony_ci * ima_protect_xattr - protect 'security.ima' 66062306a36Sopenharmony_ci * 66162306a36Sopenharmony_ci * Ensure that not just anyone can modify or remove 'security.ima'. 66262306a36Sopenharmony_ci */ 66362306a36Sopenharmony_cistatic int ima_protect_xattr(struct dentry *dentry, const char *xattr_name, 66462306a36Sopenharmony_ci const void *xattr_value, size_t xattr_value_len) 66562306a36Sopenharmony_ci{ 66662306a36Sopenharmony_ci if (strcmp(xattr_name, XATTR_NAME_IMA) == 0) { 66762306a36Sopenharmony_ci if (!capable(CAP_SYS_ADMIN)) 66862306a36Sopenharmony_ci return -EPERM; 66962306a36Sopenharmony_ci return 1; 67062306a36Sopenharmony_ci } 67162306a36Sopenharmony_ci return 0; 67262306a36Sopenharmony_ci} 67362306a36Sopenharmony_ci 67462306a36Sopenharmony_cistatic void ima_reset_appraise_flags(struct inode *inode, int digsig) 67562306a36Sopenharmony_ci{ 67662306a36Sopenharmony_ci struct integrity_iint_cache *iint; 67762306a36Sopenharmony_ci 67862306a36Sopenharmony_ci if (!(ima_policy_flag & IMA_APPRAISE) || !S_ISREG(inode->i_mode)) 67962306a36Sopenharmony_ci return; 68062306a36Sopenharmony_ci 68162306a36Sopenharmony_ci iint = integrity_iint_find(inode); 68262306a36Sopenharmony_ci if (!iint) 68362306a36Sopenharmony_ci return; 68462306a36Sopenharmony_ci iint->measured_pcrs = 0; 68562306a36Sopenharmony_ci set_bit(IMA_CHANGE_XATTR, &iint->atomic_flags); 68662306a36Sopenharmony_ci if (digsig) 68762306a36Sopenharmony_ci set_bit(IMA_DIGSIG, &iint->atomic_flags); 68862306a36Sopenharmony_ci else 68962306a36Sopenharmony_ci clear_bit(IMA_DIGSIG, &iint->atomic_flags); 69062306a36Sopenharmony_ci} 69162306a36Sopenharmony_ci 69262306a36Sopenharmony_ci/** 69362306a36Sopenharmony_ci * validate_hash_algo() - Block setxattr with unsupported hash algorithms 69462306a36Sopenharmony_ci * @dentry: object of the setxattr() 69562306a36Sopenharmony_ci * @xattr_value: userland supplied xattr value 69662306a36Sopenharmony_ci * @xattr_value_len: length of xattr_value 69762306a36Sopenharmony_ci * 69862306a36Sopenharmony_ci * The xattr value is mapped to its hash algorithm, and this algorithm 69962306a36Sopenharmony_ci * must be built in the kernel for the setxattr to be allowed. 70062306a36Sopenharmony_ci * 70162306a36Sopenharmony_ci * Emit an audit message when the algorithm is invalid. 70262306a36Sopenharmony_ci * 70362306a36Sopenharmony_ci * Return: 0 on success, else an error. 70462306a36Sopenharmony_ci */ 70562306a36Sopenharmony_cistatic int validate_hash_algo(struct dentry *dentry, 70662306a36Sopenharmony_ci const struct evm_ima_xattr_data *xattr_value, 70762306a36Sopenharmony_ci size_t xattr_value_len) 70862306a36Sopenharmony_ci{ 70962306a36Sopenharmony_ci char *path = NULL, *pathbuf = NULL; 71062306a36Sopenharmony_ci enum hash_algo xattr_hash_algo; 71162306a36Sopenharmony_ci const char *errmsg = "unavailable-hash-algorithm"; 71262306a36Sopenharmony_ci unsigned int allowed_hashes; 71362306a36Sopenharmony_ci 71462306a36Sopenharmony_ci xattr_hash_algo = ima_get_hash_algo(xattr_value, xattr_value_len); 71562306a36Sopenharmony_ci 71662306a36Sopenharmony_ci allowed_hashes = atomic_read(&ima_setxattr_allowed_hash_algorithms); 71762306a36Sopenharmony_ci 71862306a36Sopenharmony_ci if (allowed_hashes) { 71962306a36Sopenharmony_ci /* success if the algorithm is allowed in the ima policy */ 72062306a36Sopenharmony_ci if (allowed_hashes & (1U << xattr_hash_algo)) 72162306a36Sopenharmony_ci return 0; 72262306a36Sopenharmony_ci 72362306a36Sopenharmony_ci /* 72462306a36Sopenharmony_ci * We use a different audit message when the hash algorithm 72562306a36Sopenharmony_ci * is denied by a policy rule, instead of not being built 72662306a36Sopenharmony_ci * in the kernel image 72762306a36Sopenharmony_ci */ 72862306a36Sopenharmony_ci errmsg = "denied-hash-algorithm"; 72962306a36Sopenharmony_ci } else { 73062306a36Sopenharmony_ci if (likely(xattr_hash_algo == ima_hash_algo)) 73162306a36Sopenharmony_ci return 0; 73262306a36Sopenharmony_ci 73362306a36Sopenharmony_ci /* allow any xattr using an algorithm built in the kernel */ 73462306a36Sopenharmony_ci if (crypto_has_alg(hash_algo_name[xattr_hash_algo], 0, 0)) 73562306a36Sopenharmony_ci return 0; 73662306a36Sopenharmony_ci } 73762306a36Sopenharmony_ci 73862306a36Sopenharmony_ci pathbuf = kmalloc(PATH_MAX, GFP_KERNEL); 73962306a36Sopenharmony_ci if (!pathbuf) 74062306a36Sopenharmony_ci return -EACCES; 74162306a36Sopenharmony_ci 74262306a36Sopenharmony_ci path = dentry_path(dentry, pathbuf, PATH_MAX); 74362306a36Sopenharmony_ci 74462306a36Sopenharmony_ci integrity_audit_msg(AUDIT_INTEGRITY_DATA, d_inode(dentry), path, 74562306a36Sopenharmony_ci "set_data", errmsg, -EACCES, 0); 74662306a36Sopenharmony_ci 74762306a36Sopenharmony_ci kfree(pathbuf); 74862306a36Sopenharmony_ci 74962306a36Sopenharmony_ci return -EACCES; 75062306a36Sopenharmony_ci} 75162306a36Sopenharmony_ci 75262306a36Sopenharmony_ciint ima_inode_setxattr(struct dentry *dentry, const char *xattr_name, 75362306a36Sopenharmony_ci const void *xattr_value, size_t xattr_value_len) 75462306a36Sopenharmony_ci{ 75562306a36Sopenharmony_ci const struct evm_ima_xattr_data *xvalue = xattr_value; 75662306a36Sopenharmony_ci int digsig = 0; 75762306a36Sopenharmony_ci int result; 75862306a36Sopenharmony_ci int err; 75962306a36Sopenharmony_ci 76062306a36Sopenharmony_ci result = ima_protect_xattr(dentry, xattr_name, xattr_value, 76162306a36Sopenharmony_ci xattr_value_len); 76262306a36Sopenharmony_ci if (result == 1) { 76362306a36Sopenharmony_ci if (!xattr_value_len || (xvalue->type >= IMA_XATTR_LAST)) 76462306a36Sopenharmony_ci return -EINVAL; 76562306a36Sopenharmony_ci 76662306a36Sopenharmony_ci err = validate_hash_algo(dentry, xvalue, xattr_value_len); 76762306a36Sopenharmony_ci if (err) 76862306a36Sopenharmony_ci return err; 76962306a36Sopenharmony_ci 77062306a36Sopenharmony_ci digsig = (xvalue->type == EVM_IMA_XATTR_DIGSIG); 77162306a36Sopenharmony_ci } else if (!strcmp(xattr_name, XATTR_NAME_EVM) && xattr_value_len > 0) { 77262306a36Sopenharmony_ci digsig = (xvalue->type == EVM_XATTR_PORTABLE_DIGSIG); 77362306a36Sopenharmony_ci } 77462306a36Sopenharmony_ci if (result == 1 || evm_revalidate_status(xattr_name)) { 77562306a36Sopenharmony_ci ima_reset_appraise_flags(d_backing_inode(dentry), digsig); 77662306a36Sopenharmony_ci if (result == 1) 77762306a36Sopenharmony_ci result = 0; 77862306a36Sopenharmony_ci } 77962306a36Sopenharmony_ci return result; 78062306a36Sopenharmony_ci} 78162306a36Sopenharmony_ci 78262306a36Sopenharmony_ciint ima_inode_set_acl(struct mnt_idmap *idmap, struct dentry *dentry, 78362306a36Sopenharmony_ci const char *acl_name, struct posix_acl *kacl) 78462306a36Sopenharmony_ci{ 78562306a36Sopenharmony_ci if (evm_revalidate_status(acl_name)) 78662306a36Sopenharmony_ci ima_reset_appraise_flags(d_backing_inode(dentry), 0); 78762306a36Sopenharmony_ci 78862306a36Sopenharmony_ci return 0; 78962306a36Sopenharmony_ci} 79062306a36Sopenharmony_ci 79162306a36Sopenharmony_ciint ima_inode_removexattr(struct dentry *dentry, const char *xattr_name) 79262306a36Sopenharmony_ci{ 79362306a36Sopenharmony_ci int result; 79462306a36Sopenharmony_ci 79562306a36Sopenharmony_ci result = ima_protect_xattr(dentry, xattr_name, NULL, 0); 79662306a36Sopenharmony_ci if (result == 1 || evm_revalidate_status(xattr_name)) { 79762306a36Sopenharmony_ci ima_reset_appraise_flags(d_backing_inode(dentry), 0); 79862306a36Sopenharmony_ci if (result == 1) 79962306a36Sopenharmony_ci result = 0; 80062306a36Sopenharmony_ci } 80162306a36Sopenharmony_ci return result; 80262306a36Sopenharmony_ci} 803