1b886d83cSThomas Gleixner // SPDX-License-Identifier: GPL-2.0-only 22fe5d6deSMimi Zohar /* 32fe5d6deSMimi Zohar * Copyright (C) 2011 IBM Corporation 42fe5d6deSMimi Zohar * 52fe5d6deSMimi Zohar * Author: 62fe5d6deSMimi Zohar * Mimi Zohar <zohar@us.ibm.com> 72fe5d6deSMimi Zohar */ 8876979c9SPaul Gortmaker #include <linux/init.h> 92fe5d6deSMimi Zohar #include <linux/file.h> 102fe5d6deSMimi Zohar #include <linux/fs.h> 112fe5d6deSMimi Zohar #include <linux/xattr.h> 122fe5d6deSMimi Zohar #include <linux/magic.h> 132fe5d6deSMimi Zohar #include <linux/ima.h> 142fe5d6deSMimi Zohar #include <linux/evm.h> 15273df864SNayna Jain #include <keys/system_keyring.h> 162fe5d6deSMimi Zohar 172fe5d6deSMimi Zohar #include "ima.h" 182fe5d6deSMimi Zohar 192fe5d6deSMimi Zohar static int __init default_appraise_setup(char *str) 202fe5d6deSMimi Zohar { 21e1f5e01fSMimi Zohar #ifdef CONFIG_IMA_APPRAISE_BOOTPARAM 22311aa6aaSBruno Meneguele if (arch_ima_get_secureboot()) { 23311aa6aaSBruno Meneguele pr_info("Secure boot enabled: ignoring ima_appraise=%s boot parameter option", 24311aa6aaSBruno Meneguele str); 25311aa6aaSBruno Meneguele return 1; 26311aa6aaSBruno Meneguele } 27311aa6aaSBruno Meneguele 282fe5d6deSMimi Zohar if (strncmp(str, "off", 3) == 0) 292fe5d6deSMimi Zohar ima_appraise = 0; 302faa6ef3SDmitry Kasatkin else if (strncmp(str, "log", 3) == 0) 312faa6ef3SDmitry Kasatkin ima_appraise = IMA_APPRAISE_LOG; 322fe5d6deSMimi Zohar else if (strncmp(str, "fix", 3) == 0) 332fe5d6deSMimi Zohar ima_appraise = IMA_APPRAISE_FIX; 344afb28abSBruno Meneguele else if (strncmp(str, "enforce", 7) == 0) 354afb28abSBruno Meneguele ima_appraise = IMA_APPRAISE_ENFORCE; 36*7fe2bb7eSBruno Meneguele else 37*7fe2bb7eSBruno Meneguele pr_err("invalid \"%s\" appraise option", str); 38e1f5e01fSMimi Zohar #endif 392fe5d6deSMimi Zohar return 1; 402fe5d6deSMimi Zohar } 412fe5d6deSMimi Zohar 422fe5d6deSMimi Zohar __setup("ima_appraise=", default_appraise_setup); 432fe5d6deSMimi Zohar 442fe5d6deSMimi Zohar /* 456f6723e2SMimi Zohar * is_ima_appraise_enabled - return appraise status 466f6723e2SMimi Zohar * 476f6723e2SMimi Zohar * Only return enabled, if not in ima_appraise="fix" or "log" modes. 486f6723e2SMimi Zohar */ 496f6723e2SMimi Zohar bool is_ima_appraise_enabled(void) 506f6723e2SMimi Zohar { 51e5729f86SThiago Jung Bauermann return ima_appraise & IMA_APPRAISE_ENFORCE; 526f6723e2SMimi Zohar } 536f6723e2SMimi Zohar 546f6723e2SMimi Zohar /* 552fe5d6deSMimi Zohar * ima_must_appraise - set appraise flag 562fe5d6deSMimi Zohar * 57da1b0029SMimi Zohar * Return 1 to appraise or hash 582fe5d6deSMimi Zohar */ 59d26e1936SDmitry Kasatkin int ima_must_appraise(struct inode *inode, int mask, enum ima_hooks func) 602fe5d6deSMimi Zohar { 61d906c10dSMatthew Garrett u32 secid; 62d906c10dSMatthew Garrett 6307f6a794SMimi Zohar if (!ima_appraise) 642fe5d6deSMimi Zohar return 0; 6507f6a794SMimi Zohar 66d906c10dSMatthew Garrett security_task_getsecid(current, &secid); 67d906c10dSMatthew Garrett return ima_match_policy(inode, current_cred(), secid, func, mask, 68e9085e0aSLakshmi Ramasubramanian IMA_APPRAISE | IMA_HASH, NULL, NULL, NULL); 692fe5d6deSMimi Zohar } 702fe5d6deSMimi Zohar 71def3e8b9SDmitry Kasatkin static int ima_fix_xattr(struct dentry *dentry, 722fe5d6deSMimi Zohar struct integrity_iint_cache *iint) 732fe5d6deSMimi Zohar { 743ea7a560SDmitry Kasatkin int rc, offset; 753ea7a560SDmitry Kasatkin u8 algo = iint->ima_hash->algo; 763ea7a560SDmitry Kasatkin 773ea7a560SDmitry Kasatkin if (algo <= HASH_ALGO_SHA1) { 783ea7a560SDmitry Kasatkin offset = 1; 793ea7a560SDmitry Kasatkin iint->ima_hash->xattr.sha1.type = IMA_XATTR_DIGEST; 803ea7a560SDmitry Kasatkin } else { 813ea7a560SDmitry Kasatkin offset = 0; 823ea7a560SDmitry Kasatkin iint->ima_hash->xattr.ng.type = IMA_XATTR_DIGEST_NG; 833ea7a560SDmitry Kasatkin iint->ima_hash->xattr.ng.algo = algo; 843ea7a560SDmitry Kasatkin } 853ea7a560SDmitry Kasatkin rc = __vfs_setxattr_noperm(dentry, XATTR_NAME_IMA, 863ea7a560SDmitry Kasatkin &iint->ima_hash->xattr.data[offset], 873ea7a560SDmitry Kasatkin (sizeof(iint->ima_hash->xattr) - offset) + 883ea7a560SDmitry Kasatkin iint->ima_hash->length, 0); 893ea7a560SDmitry Kasatkin return rc; 902fe5d6deSMimi Zohar } 912fe5d6deSMimi Zohar 92d79d72e0SMimi Zohar /* Return specific func appraised cached result */ 93d79d72e0SMimi Zohar enum integrity_status ima_get_cache_status(struct integrity_iint_cache *iint, 944ad87a3dSMimi Zohar enum ima_hooks func) 95d79d72e0SMimi Zohar { 96d79d72e0SMimi Zohar switch (func) { 97d79d72e0SMimi Zohar case MMAP_CHECK: 98d79d72e0SMimi Zohar return iint->ima_mmap_status; 99d79d72e0SMimi Zohar case BPRM_CHECK: 100d79d72e0SMimi Zohar return iint->ima_bprm_status; 101d906c10dSMatthew Garrett case CREDS_CHECK: 102d906c10dSMatthew Garrett return iint->ima_creds_status; 103d79d72e0SMimi Zohar case FILE_CHECK: 104c6af8efeSMimi Zohar case POST_SETATTR: 105d79d72e0SMimi Zohar return iint->ima_file_status; 106c6af8efeSMimi Zohar case MODULE_CHECK ... MAX_CHECK - 1: 107c6af8efeSMimi Zohar default: 108c6af8efeSMimi Zohar return iint->ima_read_status; 109d79d72e0SMimi Zohar } 110d79d72e0SMimi Zohar } 111d79d72e0SMimi Zohar 112d79d72e0SMimi Zohar static void ima_set_cache_status(struct integrity_iint_cache *iint, 1134ad87a3dSMimi Zohar enum ima_hooks func, 1144ad87a3dSMimi Zohar enum integrity_status status) 115d79d72e0SMimi Zohar { 116d79d72e0SMimi Zohar switch (func) { 117d79d72e0SMimi Zohar case MMAP_CHECK: 118d79d72e0SMimi Zohar iint->ima_mmap_status = status; 119d79d72e0SMimi Zohar break; 120d79d72e0SMimi Zohar case BPRM_CHECK: 121d79d72e0SMimi Zohar iint->ima_bprm_status = status; 122d79d72e0SMimi Zohar break; 123d906c10dSMatthew Garrett case CREDS_CHECK: 124d906c10dSMatthew Garrett iint->ima_creds_status = status; 12509186e50SGustavo A. R. Silva break; 126d79d72e0SMimi Zohar case FILE_CHECK: 127c6af8efeSMimi Zohar case POST_SETATTR: 128d79d72e0SMimi Zohar iint->ima_file_status = status; 129c6af8efeSMimi Zohar break; 130c6af8efeSMimi Zohar case MODULE_CHECK ... MAX_CHECK - 1: 131c6af8efeSMimi Zohar default: 132c6af8efeSMimi Zohar iint->ima_read_status = status; 133c6af8efeSMimi Zohar break; 134d79d72e0SMimi Zohar } 135d79d72e0SMimi Zohar } 136d79d72e0SMimi Zohar 1374ad87a3dSMimi Zohar static void ima_cache_flags(struct integrity_iint_cache *iint, 1384ad87a3dSMimi Zohar enum ima_hooks func) 139d79d72e0SMimi Zohar { 140d79d72e0SMimi Zohar switch (func) { 141d79d72e0SMimi Zohar case MMAP_CHECK: 142d79d72e0SMimi Zohar iint->flags |= (IMA_MMAP_APPRAISED | IMA_APPRAISED); 143d79d72e0SMimi Zohar break; 144d79d72e0SMimi Zohar case BPRM_CHECK: 145d79d72e0SMimi Zohar iint->flags |= (IMA_BPRM_APPRAISED | IMA_APPRAISED); 146d79d72e0SMimi Zohar break; 147d906c10dSMatthew Garrett case CREDS_CHECK: 148d906c10dSMatthew Garrett iint->flags |= (IMA_CREDS_APPRAISED | IMA_APPRAISED); 149d906c10dSMatthew Garrett break; 150d79d72e0SMimi Zohar case FILE_CHECK: 151c6af8efeSMimi Zohar case POST_SETATTR: 152d79d72e0SMimi Zohar iint->flags |= (IMA_FILE_APPRAISED | IMA_APPRAISED); 153c6af8efeSMimi Zohar break; 154c6af8efeSMimi Zohar case MODULE_CHECK ... MAX_CHECK - 1: 155c6af8efeSMimi Zohar default: 156c6af8efeSMimi Zohar iint->flags |= (IMA_READ_APPRAISED | IMA_APPRAISED); 157c6af8efeSMimi Zohar break; 158d79d72e0SMimi Zohar } 159d79d72e0SMimi Zohar } 160d79d72e0SMimi Zohar 1611525b06dSDmitry Kasatkin enum hash_algo ima_get_hash_algo(struct evm_ima_xattr_data *xattr_value, 1621525b06dSDmitry Kasatkin int xattr_len) 163d3634d0fSDmitry Kasatkin { 164d3634d0fSDmitry Kasatkin struct signature_v2_hdr *sig; 165b4bfec7fSSeth Forshee enum hash_algo ret; 166d3634d0fSDmitry Kasatkin 1673ea7a560SDmitry Kasatkin if (!xattr_value || xattr_len < 2) 1681525b06dSDmitry Kasatkin /* return default hash algo */ 1691525b06dSDmitry Kasatkin return ima_hash_algo; 170d3634d0fSDmitry Kasatkin 1713ea7a560SDmitry Kasatkin switch (xattr_value->type) { 1723ea7a560SDmitry Kasatkin case EVM_IMA_XATTR_DIGSIG: 1733ea7a560SDmitry Kasatkin sig = (typeof(sig))xattr_value; 1743ea7a560SDmitry Kasatkin if (sig->version != 2 || xattr_len <= sizeof(*sig)) 1751525b06dSDmitry Kasatkin return ima_hash_algo; 1761525b06dSDmitry Kasatkin return sig->hash_algo; 1773ea7a560SDmitry Kasatkin break; 1783ea7a560SDmitry Kasatkin case IMA_XATTR_DIGEST_NG: 179650b29dbSThiago Jung Bauermann /* first byte contains algorithm id */ 180650b29dbSThiago Jung Bauermann ret = xattr_value->data[0]; 181b4bfec7fSSeth Forshee if (ret < HASH_ALGO__LAST) 182b4bfec7fSSeth Forshee return ret; 1833ea7a560SDmitry Kasatkin break; 1843ea7a560SDmitry Kasatkin case IMA_XATTR_DIGEST: 1853ea7a560SDmitry Kasatkin /* this is for backward compatibility */ 1863ea7a560SDmitry Kasatkin if (xattr_len == 21) { 1873ea7a560SDmitry Kasatkin unsigned int zero = 0; 188650b29dbSThiago Jung Bauermann if (!memcmp(&xattr_value->data[16], &zero, 4)) 1891525b06dSDmitry Kasatkin return HASH_ALGO_MD5; 1903ea7a560SDmitry Kasatkin else 1911525b06dSDmitry Kasatkin return HASH_ALGO_SHA1; 1923ea7a560SDmitry Kasatkin } else if (xattr_len == 17) 1931525b06dSDmitry Kasatkin return HASH_ALGO_MD5; 1943ea7a560SDmitry Kasatkin break; 1953ea7a560SDmitry Kasatkin } 1961525b06dSDmitry Kasatkin 1971525b06dSDmitry Kasatkin /* return default hash algo */ 1981525b06dSDmitry Kasatkin return ima_hash_algo; 199d3634d0fSDmitry Kasatkin } 200d3634d0fSDmitry Kasatkin 201d3634d0fSDmitry Kasatkin int ima_read_xattr(struct dentry *dentry, 202d3634d0fSDmitry Kasatkin struct evm_ima_xattr_data **xattr_value) 203d3634d0fSDmitry Kasatkin { 2045d6c3191SAndreas Gruenbacher ssize_t ret; 205d3634d0fSDmitry Kasatkin 2065d6c3191SAndreas Gruenbacher ret = vfs_getxattr_alloc(dentry, XATTR_NAME_IMA, (char **)xattr_value, 207d3634d0fSDmitry Kasatkin 0, GFP_NOFS); 2085d6c3191SAndreas Gruenbacher if (ret == -EOPNOTSUPP) 2095d6c3191SAndreas Gruenbacher ret = 0; 2105d6c3191SAndreas Gruenbacher return ret; 211d3634d0fSDmitry Kasatkin } 212d3634d0fSDmitry Kasatkin 2132fe5d6deSMimi Zohar /* 214a5fbeb61SThiago Jung Bauermann * xattr_verify - verify xattr digest or signature 215a5fbeb61SThiago Jung Bauermann * 216a5fbeb61SThiago Jung Bauermann * Verify whether the hash or signature matches the file contents. 217a5fbeb61SThiago Jung Bauermann * 218a5fbeb61SThiago Jung Bauermann * Return 0 on success, error code otherwise. 219a5fbeb61SThiago Jung Bauermann */ 220a5fbeb61SThiago Jung Bauermann static int xattr_verify(enum ima_hooks func, struct integrity_iint_cache *iint, 221a5fbeb61SThiago Jung Bauermann struct evm_ima_xattr_data *xattr_value, int xattr_len, 222a5fbeb61SThiago Jung Bauermann enum integrity_status *status, const char **cause) 223a5fbeb61SThiago Jung Bauermann { 224a5fbeb61SThiago Jung Bauermann int rc = -EINVAL, hash_start = 0; 225a5fbeb61SThiago Jung Bauermann 226a5fbeb61SThiago Jung Bauermann switch (xattr_value->type) { 227a5fbeb61SThiago Jung Bauermann case IMA_XATTR_DIGEST_NG: 228a5fbeb61SThiago Jung Bauermann /* first byte contains algorithm id */ 229a5fbeb61SThiago Jung Bauermann hash_start = 1; 230a5fbeb61SThiago Jung Bauermann /* fall through */ 231a5fbeb61SThiago Jung Bauermann case IMA_XATTR_DIGEST: 232a5fbeb61SThiago Jung Bauermann if (iint->flags & IMA_DIGSIG_REQUIRED) { 233a5fbeb61SThiago Jung Bauermann *cause = "IMA-signature-required"; 234a5fbeb61SThiago Jung Bauermann *status = INTEGRITY_FAIL; 235a5fbeb61SThiago Jung Bauermann break; 236a5fbeb61SThiago Jung Bauermann } 237a5fbeb61SThiago Jung Bauermann clear_bit(IMA_DIGSIG, &iint->atomic_flags); 238a5fbeb61SThiago Jung Bauermann if (xattr_len - sizeof(xattr_value->type) - hash_start >= 239a5fbeb61SThiago Jung Bauermann iint->ima_hash->length) 240a5fbeb61SThiago Jung Bauermann /* 241a5fbeb61SThiago Jung Bauermann * xattr length may be longer. md5 hash in previous 242a5fbeb61SThiago Jung Bauermann * version occupied 20 bytes in xattr, instead of 16 243a5fbeb61SThiago Jung Bauermann */ 244a5fbeb61SThiago Jung Bauermann rc = memcmp(&xattr_value->data[hash_start], 245a5fbeb61SThiago Jung Bauermann iint->ima_hash->digest, 246a5fbeb61SThiago Jung Bauermann iint->ima_hash->length); 247a5fbeb61SThiago Jung Bauermann else 248a5fbeb61SThiago Jung Bauermann rc = -EINVAL; 249a5fbeb61SThiago Jung Bauermann if (rc) { 250a5fbeb61SThiago Jung Bauermann *cause = "invalid-hash"; 251a5fbeb61SThiago Jung Bauermann *status = INTEGRITY_FAIL; 252a5fbeb61SThiago Jung Bauermann break; 253a5fbeb61SThiago Jung Bauermann } 254a5fbeb61SThiago Jung Bauermann *status = INTEGRITY_PASS; 255a5fbeb61SThiago Jung Bauermann break; 256a5fbeb61SThiago Jung Bauermann case EVM_IMA_XATTR_DIGSIG: 257a5fbeb61SThiago Jung Bauermann set_bit(IMA_DIGSIG, &iint->atomic_flags); 258a5fbeb61SThiago Jung Bauermann rc = integrity_digsig_verify(INTEGRITY_KEYRING_IMA, 259a5fbeb61SThiago Jung Bauermann (const char *)xattr_value, 260a5fbeb61SThiago Jung Bauermann xattr_len, 261a5fbeb61SThiago Jung Bauermann iint->ima_hash->digest, 262a5fbeb61SThiago Jung Bauermann iint->ima_hash->length); 263a5fbeb61SThiago Jung Bauermann if (rc == -EOPNOTSUPP) { 264a5fbeb61SThiago Jung Bauermann *status = INTEGRITY_UNKNOWN; 265a5fbeb61SThiago Jung Bauermann break; 266a5fbeb61SThiago Jung Bauermann } 267a5fbeb61SThiago Jung Bauermann if (IS_ENABLED(CONFIG_INTEGRITY_PLATFORM_KEYRING) && rc && 268a5fbeb61SThiago Jung Bauermann func == KEXEC_KERNEL_CHECK) 269a5fbeb61SThiago Jung Bauermann rc = integrity_digsig_verify(INTEGRITY_KEYRING_PLATFORM, 270a5fbeb61SThiago Jung Bauermann (const char *)xattr_value, 271a5fbeb61SThiago Jung Bauermann xattr_len, 272a5fbeb61SThiago Jung Bauermann iint->ima_hash->digest, 273a5fbeb61SThiago Jung Bauermann iint->ima_hash->length); 274a5fbeb61SThiago Jung Bauermann if (rc) { 275a5fbeb61SThiago Jung Bauermann *cause = "invalid-signature"; 276a5fbeb61SThiago Jung Bauermann *status = INTEGRITY_FAIL; 277a5fbeb61SThiago Jung Bauermann } else { 278a5fbeb61SThiago Jung Bauermann *status = INTEGRITY_PASS; 279a5fbeb61SThiago Jung Bauermann } 280a5fbeb61SThiago Jung Bauermann break; 281a5fbeb61SThiago Jung Bauermann default: 282a5fbeb61SThiago Jung Bauermann *status = INTEGRITY_UNKNOWN; 283a5fbeb61SThiago Jung Bauermann *cause = "unknown-ima-data"; 284a5fbeb61SThiago Jung Bauermann break; 285a5fbeb61SThiago Jung Bauermann } 286a5fbeb61SThiago Jung Bauermann 287a5fbeb61SThiago Jung Bauermann return rc; 288a5fbeb61SThiago Jung Bauermann } 289a5fbeb61SThiago Jung Bauermann 290a5fbeb61SThiago Jung Bauermann /* 29139b07096SThiago Jung Bauermann * modsig_verify - verify modsig signature 29239b07096SThiago Jung Bauermann * 29339b07096SThiago Jung Bauermann * Verify whether the signature matches the file contents. 29439b07096SThiago Jung Bauermann * 29539b07096SThiago Jung Bauermann * Return 0 on success, error code otherwise. 29639b07096SThiago Jung Bauermann */ 29739b07096SThiago Jung Bauermann static int modsig_verify(enum ima_hooks func, const struct modsig *modsig, 29839b07096SThiago Jung Bauermann enum integrity_status *status, const char **cause) 29939b07096SThiago Jung Bauermann { 30039b07096SThiago Jung Bauermann int rc; 30139b07096SThiago Jung Bauermann 30239b07096SThiago Jung Bauermann rc = integrity_modsig_verify(INTEGRITY_KEYRING_IMA, modsig); 30339b07096SThiago Jung Bauermann if (IS_ENABLED(CONFIG_INTEGRITY_PLATFORM_KEYRING) && rc && 30439b07096SThiago Jung Bauermann func == KEXEC_KERNEL_CHECK) 30539b07096SThiago Jung Bauermann rc = integrity_modsig_verify(INTEGRITY_KEYRING_PLATFORM, 30639b07096SThiago Jung Bauermann modsig); 30739b07096SThiago Jung Bauermann if (rc) { 30839b07096SThiago Jung Bauermann *cause = "invalid-signature"; 30939b07096SThiago Jung Bauermann *status = INTEGRITY_FAIL; 31039b07096SThiago Jung Bauermann } else { 31139b07096SThiago Jung Bauermann *status = INTEGRITY_PASS; 31239b07096SThiago Jung Bauermann } 31339b07096SThiago Jung Bauermann 31439b07096SThiago Jung Bauermann return rc; 31539b07096SThiago Jung Bauermann } 31639b07096SThiago Jung Bauermann 31739b07096SThiago Jung Bauermann /* 318273df864SNayna Jain * ima_check_blacklist - determine if the binary is blacklisted. 319273df864SNayna Jain * 320273df864SNayna Jain * Add the hash of the blacklisted binary to the measurement list, based 321273df864SNayna Jain * on policy. 322273df864SNayna Jain * 323273df864SNayna Jain * Returns -EPERM if the hash is blacklisted. 324273df864SNayna Jain */ 325273df864SNayna Jain int ima_check_blacklist(struct integrity_iint_cache *iint, 326273df864SNayna Jain const struct modsig *modsig, int pcr) 327273df864SNayna Jain { 328273df864SNayna Jain enum hash_algo hash_algo; 329273df864SNayna Jain const u8 *digest = NULL; 330273df864SNayna Jain u32 digestsize = 0; 331273df864SNayna Jain int rc = 0; 332273df864SNayna Jain 333273df864SNayna Jain if (!(iint->flags & IMA_CHECK_BLACKLIST)) 334273df864SNayna Jain return 0; 335273df864SNayna Jain 336273df864SNayna Jain if (iint->flags & IMA_MODSIG_ALLOWED && modsig) { 337273df864SNayna Jain ima_get_modsig_digest(modsig, &hash_algo, &digest, &digestsize); 338273df864SNayna Jain 339273df864SNayna Jain rc = is_binary_blacklisted(digest, digestsize); 340273df864SNayna Jain if ((rc == -EPERM) && (iint->flags & IMA_MEASURE)) 3414834177eSTyler Hicks process_buffer_measurement(NULL, digest, digestsize, 342273df864SNayna Jain "blacklisted-hash", NONE, 343e9085e0aSLakshmi Ramasubramanian pcr, NULL); 344273df864SNayna Jain } 345273df864SNayna Jain 346273df864SNayna Jain return rc; 347273df864SNayna Jain } 348273df864SNayna Jain 349273df864SNayna Jain /* 3502fe5d6deSMimi Zohar * ima_appraise_measurement - appraise file measurement 3512fe5d6deSMimi Zohar * 3522fe5d6deSMimi Zohar * Call evm_verifyxattr() to verify the integrity of 'security.ima'. 3532fe5d6deSMimi Zohar * Assuming success, compare the xattr hash with the collected measurement. 3542fe5d6deSMimi Zohar * 3552fe5d6deSMimi Zohar * Return 0 on success, error code otherwise 3562fe5d6deSMimi Zohar */ 3574ad87a3dSMimi Zohar int ima_appraise_measurement(enum ima_hooks func, 3584ad87a3dSMimi Zohar struct integrity_iint_cache *iint, 359d3634d0fSDmitry Kasatkin struct file *file, const unsigned char *filename, 360d3634d0fSDmitry Kasatkin struct evm_ima_xattr_data *xattr_value, 36139b07096SThiago Jung Bauermann int xattr_len, const struct modsig *modsig) 3622fe5d6deSMimi Zohar { 36352a13284SMimi Zohar static const char op[] = "appraise_data"; 364f5e51fa3SThiago Jung Bauermann const char *cause = "unknown"; 365e71b9dffSMiklos Szeredi struct dentry *dentry = file_dentry(file); 366c6f493d6SDavid Howells struct inode *inode = d_backing_inode(dentry); 3672fe5d6deSMimi Zohar enum integrity_status status = INTEGRITY_UNKNOWN; 368a5fbeb61SThiago Jung Bauermann int rc = xattr_len; 36939b07096SThiago Jung Bauermann bool try_modsig = iint->flags & IMA_MODSIG_ALLOWED && modsig; 3702fe5d6deSMimi Zohar 37139b07096SThiago Jung Bauermann /* If not appraising a modsig, we need an xattr. */ 37239b07096SThiago Jung Bauermann if (!(inode->i_opflags & IOP_XATTR) && !try_modsig) 3732fe5d6deSMimi Zohar return INTEGRITY_UNKNOWN; 3742fe5d6deSMimi Zohar 37539b07096SThiago Jung Bauermann /* If reading the xattr failed and there's no modsig, error out. */ 37639b07096SThiago Jung Bauermann if (rc <= 0 && !try_modsig) { 3772fe5d6deSMimi Zohar if (rc && rc != -ENODATA) 3782fe5d6deSMimi Zohar goto out; 3792fe5d6deSMimi Zohar 380915d9d25SThiago Jung Bauermann cause = iint->flags & IMA_DIGSIG_REQUIRED ? 381915d9d25SThiago Jung Bauermann "IMA-signature-required" : "missing-hash"; 382b151d6b0SDmitry Kasatkin status = INTEGRITY_NOLABEL; 3836035a27bSAl Viro if (file->f_mode & FMODE_CREATED) 384b151d6b0SDmitry Kasatkin iint->flags |= IMA_NEW_FILE; 3851ac202e9SDaniel Glöckner if ((iint->flags & IMA_NEW_FILE) && 386b7e27bc1SMimi Zohar (!(iint->flags & IMA_DIGSIG_REQUIRED) || 387b7e27bc1SMimi Zohar (inode->i_size == 0))) 388b151d6b0SDmitry Kasatkin status = INTEGRITY_PASS; 3892fe5d6deSMimi Zohar goto out; 3902fe5d6deSMimi Zohar } 3912fe5d6deSMimi Zohar 3928606404fSDmitry Kasatkin status = evm_verifyxattr(dentry, XATTR_NAME_IMA, xattr_value, rc, iint); 393f5e51fa3SThiago Jung Bauermann switch (status) { 394f5e51fa3SThiago Jung Bauermann case INTEGRITY_PASS: 395f5e51fa3SThiago Jung Bauermann case INTEGRITY_PASS_IMMUTABLE: 396f5e51fa3SThiago Jung Bauermann case INTEGRITY_UNKNOWN: 397f5e51fa3SThiago Jung Bauermann break; 398f5e51fa3SThiago Jung Bauermann case INTEGRITY_NOXATTRS: /* No EVM protected xattrs. */ 39939b07096SThiago Jung Bauermann /* It's fine not to have xattrs when using a modsig. */ 40039b07096SThiago Jung Bauermann if (try_modsig) 40139b07096SThiago Jung Bauermann break; 40239b07096SThiago Jung Bauermann /* fall through */ 403f5e51fa3SThiago Jung Bauermann case INTEGRITY_NOLABEL: /* No security.evm xattr. */ 4042fe5d6deSMimi Zohar cause = "missing-HMAC"; 405f5e51fa3SThiago Jung Bauermann goto out; 406f5e51fa3SThiago Jung Bauermann case INTEGRITY_FAIL: /* Invalid HMAC/signature. */ 4072fe5d6deSMimi Zohar cause = "invalid-HMAC"; 4082fe5d6deSMimi Zohar goto out; 409f5e51fa3SThiago Jung Bauermann default: 410f5e51fa3SThiago Jung Bauermann WARN_ONCE(true, "Unexpected integrity status %d\n", status); 4112fe5d6deSMimi Zohar } 412f5e51fa3SThiago Jung Bauermann 413a5fbeb61SThiago Jung Bauermann if (xattr_value) 414a5fbeb61SThiago Jung Bauermann rc = xattr_verify(func, iint, xattr_value, xattr_len, &status, 415a5fbeb61SThiago Jung Bauermann &cause); 4168606404fSDmitry Kasatkin 41739b07096SThiago Jung Bauermann /* 41839b07096SThiago Jung Bauermann * If we have a modsig and either no imasig or the imasig's key isn't 41939b07096SThiago Jung Bauermann * known, then try verifying the modsig. 42039b07096SThiago Jung Bauermann */ 42139b07096SThiago Jung Bauermann if (try_modsig && 42239b07096SThiago Jung Bauermann (!xattr_value || xattr_value->type == IMA_XATTR_DIGEST_NG || 42339b07096SThiago Jung Bauermann rc == -ENOKEY)) 42439b07096SThiago Jung Bauermann rc = modsig_verify(func, modsig, &status, &cause); 42539b07096SThiago Jung Bauermann 4262fe5d6deSMimi Zohar out: 42757b56ac6SMimi Zohar /* 42857b56ac6SMimi Zohar * File signatures on some filesystems can not be properly verified. 4299e67028eSMimi Zohar * When such filesystems are mounted by an untrusted mounter or on a 4309e67028eSMimi Zohar * system not willing to accept such a risk, fail the file signature 4319e67028eSMimi Zohar * verification. 43257b56ac6SMimi Zohar */ 4339e67028eSMimi Zohar if ((inode->i_sb->s_iflags & SB_I_IMA_UNVERIFIABLE_SIGNATURE) && 4349e67028eSMimi Zohar ((inode->i_sb->s_iflags & SB_I_UNTRUSTED_MOUNTER) || 4359e67028eSMimi Zohar (iint->flags & IMA_FAIL_UNVERIFIABLE_SIGS))) { 43657b56ac6SMimi Zohar status = INTEGRITY_FAIL; 43757b56ac6SMimi Zohar cause = "unverifiable-signature"; 43857b56ac6SMimi Zohar integrity_audit_msg(AUDIT_INTEGRITY_DATA, inode, filename, 43957b56ac6SMimi Zohar op, cause, rc, 0); 44057b56ac6SMimi Zohar } else if (status != INTEGRITY_PASS) { 441f5e51fa3SThiago Jung Bauermann /* Fix mode, but don't replace file signatures. */ 44239b07096SThiago Jung Bauermann if ((ima_appraise & IMA_APPRAISE_FIX) && !try_modsig && 4438606404fSDmitry Kasatkin (!xattr_value || 4448606404fSDmitry Kasatkin xattr_value->type != EVM_IMA_XATTR_DIGSIG)) { 445def3e8b9SDmitry Kasatkin if (!ima_fix_xattr(dentry, iint)) 4462fe5d6deSMimi Zohar status = INTEGRITY_PASS; 447f5e51fa3SThiago Jung Bauermann } 448f5e51fa3SThiago Jung Bauermann 449f5e51fa3SThiago Jung Bauermann /* Permit new files with file signatures, but without data. */ 450f5e51fa3SThiago Jung Bauermann if (inode->i_size == 0 && iint->flags & IMA_NEW_FILE && 451f5e51fa3SThiago Jung Bauermann xattr_value && xattr_value->type == EVM_IMA_XATTR_DIGSIG) { 45205d1a717SMimi Zohar status = INTEGRITY_PASS; 4532fe5d6deSMimi Zohar } 454f5e51fa3SThiago Jung Bauermann 4552fe5d6deSMimi Zohar integrity_audit_msg(AUDIT_INTEGRITY_DATA, inode, filename, 4562fe5d6deSMimi Zohar op, cause, rc, 0); 4578606404fSDmitry Kasatkin } else { 458d79d72e0SMimi Zohar ima_cache_flags(iint, func); 4592fe5d6deSMimi Zohar } 46057b56ac6SMimi Zohar 461d79d72e0SMimi Zohar ima_set_cache_status(iint, func, status); 4622fe5d6deSMimi Zohar return status; 4632fe5d6deSMimi Zohar } 4642fe5d6deSMimi Zohar 4652fe5d6deSMimi Zohar /* 4662fe5d6deSMimi Zohar * ima_update_xattr - update 'security.ima' hash value 4672fe5d6deSMimi Zohar */ 4682fe5d6deSMimi Zohar void ima_update_xattr(struct integrity_iint_cache *iint, struct file *file) 4692fe5d6deSMimi Zohar { 470e71b9dffSMiklos Szeredi struct dentry *dentry = file_dentry(file); 4712fe5d6deSMimi Zohar int rc = 0; 4722fe5d6deSMimi Zohar 4738606404fSDmitry Kasatkin /* do not collect and update hash for digital signatures */ 4740d73a552SDmitry Kasatkin if (test_bit(IMA_DIGSIG, &iint->atomic_flags)) 4758606404fSDmitry Kasatkin return; 4768606404fSDmitry Kasatkin 477da1b0029SMimi Zohar if ((iint->ima_file_status != INTEGRITY_PASS) && 478da1b0029SMimi Zohar !(iint->flags & IMA_HASH)) 479020aae3eSRoberto Sassu return; 480020aae3eSRoberto Sassu 48115588227SThiago Jung Bauermann rc = ima_collect_measurement(iint, file, NULL, 0, ima_hash_algo, NULL); 4822fe5d6deSMimi Zohar if (rc < 0) 4832fe5d6deSMimi Zohar return; 4848606404fSDmitry Kasatkin 4850d73a552SDmitry Kasatkin inode_lock(file_inode(file)); 4862fe5d6deSMimi Zohar ima_fix_xattr(dentry, iint); 4870d73a552SDmitry Kasatkin inode_unlock(file_inode(file)); 4882fe5d6deSMimi Zohar } 4892fe5d6deSMimi Zohar 4902fe5d6deSMimi Zohar /** 4912fe5d6deSMimi Zohar * ima_inode_post_setattr - reflect file metadata changes 4922fe5d6deSMimi Zohar * @dentry: pointer to the affected dentry 4932fe5d6deSMimi Zohar * 4942fe5d6deSMimi Zohar * Changes to a dentry's metadata might result in needing to appraise. 4952fe5d6deSMimi Zohar * 4962fe5d6deSMimi Zohar * This function is called from notify_change(), which expects the caller 4972fe5d6deSMimi Zohar * to lock the inode's i_mutex. 4982fe5d6deSMimi Zohar */ 4992fe5d6deSMimi Zohar void ima_inode_post_setattr(struct dentry *dentry) 5002fe5d6deSMimi Zohar { 501c6f493d6SDavid Howells struct inode *inode = d_backing_inode(dentry); 5022fe5d6deSMimi Zohar struct integrity_iint_cache *iint; 503da1b0029SMimi Zohar int action; 5042fe5d6deSMimi Zohar 505a756024eSRoberto Sassu if (!(ima_policy_flag & IMA_APPRAISE) || !S_ISREG(inode->i_mode) 5065d6c3191SAndreas Gruenbacher || !(inode->i_opflags & IOP_XATTR)) 5072fe5d6deSMimi Zohar return; 5082fe5d6deSMimi Zohar 509da1b0029SMimi Zohar action = ima_must_appraise(inode, MAY_ACCESS, POST_SETATTR); 510da1b0029SMimi Zohar if (!action) 5115d6c3191SAndreas Gruenbacher __vfs_removexattr(dentry, XATTR_NAME_IMA); 5120d73a552SDmitry Kasatkin iint = integrity_iint_find(inode); 5130d73a552SDmitry Kasatkin if (iint) { 5140d73a552SDmitry Kasatkin set_bit(IMA_CHANGE_ATTR, &iint->atomic_flags); 515da1b0029SMimi Zohar if (!action) 5160d73a552SDmitry Kasatkin clear_bit(IMA_UPDATE_XATTR, &iint->atomic_flags); 5170d73a552SDmitry Kasatkin } 5182fe5d6deSMimi Zohar } 51942c63330SMimi Zohar 52042c63330SMimi Zohar /* 52142c63330SMimi Zohar * ima_protect_xattr - protect 'security.ima' 52242c63330SMimi Zohar * 52342c63330SMimi Zohar * Ensure that not just anyone can modify or remove 'security.ima'. 52442c63330SMimi Zohar */ 52542c63330SMimi Zohar static int ima_protect_xattr(struct dentry *dentry, const char *xattr_name, 52642c63330SMimi Zohar const void *xattr_value, size_t xattr_value_len) 52742c63330SMimi Zohar { 52842c63330SMimi Zohar if (strcmp(xattr_name, XATTR_NAME_IMA) == 0) { 52942c63330SMimi Zohar if (!capable(CAP_SYS_ADMIN)) 53042c63330SMimi Zohar return -EPERM; 53142c63330SMimi Zohar return 1; 53242c63330SMimi Zohar } 53342c63330SMimi Zohar return 0; 53442c63330SMimi Zohar } 53542c63330SMimi Zohar 536060bdebfSMimi Zohar static void ima_reset_appraise_flags(struct inode *inode, int digsig) 53742c63330SMimi Zohar { 53842c63330SMimi Zohar struct integrity_iint_cache *iint; 53942c63330SMimi Zohar 540a756024eSRoberto Sassu if (!(ima_policy_flag & IMA_APPRAISE) || !S_ISREG(inode->i_mode)) 54142c63330SMimi Zohar return; 54242c63330SMimi Zohar 54342c63330SMimi Zohar iint = integrity_iint_find(inode); 54442c63330SMimi Zohar if (!iint) 54542c63330SMimi Zohar return; 546a422638dSEric Richter iint->measured_pcrs = 0; 5470d73a552SDmitry Kasatkin set_bit(IMA_CHANGE_XATTR, &iint->atomic_flags); 548060bdebfSMimi Zohar if (digsig) 5490d73a552SDmitry Kasatkin set_bit(IMA_DIGSIG, &iint->atomic_flags); 5500d73a552SDmitry Kasatkin else 5510d73a552SDmitry Kasatkin clear_bit(IMA_DIGSIG, &iint->atomic_flags); 55242c63330SMimi Zohar } 55342c63330SMimi Zohar 55442c63330SMimi Zohar int ima_inode_setxattr(struct dentry *dentry, const char *xattr_name, 55542c63330SMimi Zohar const void *xattr_value, size_t xattr_value_len) 55642c63330SMimi Zohar { 557060bdebfSMimi Zohar const struct evm_ima_xattr_data *xvalue = xattr_value; 55842c63330SMimi Zohar int result; 55942c63330SMimi Zohar 56042c63330SMimi Zohar result = ima_protect_xattr(dentry, xattr_name, xattr_value, 56142c63330SMimi Zohar xattr_value_len); 56242c63330SMimi Zohar if (result == 1) { 563a48fda9dSDmitry Kasatkin if (!xattr_value_len || (xvalue->type >= IMA_XATTR_LAST)) 564a48fda9dSDmitry Kasatkin return -EINVAL; 565f5acb3dcSMimi Zohar ima_reset_appraise_flags(d_backing_inode(dentry), 566e5729f86SThiago Jung Bauermann xvalue->type == EVM_IMA_XATTR_DIGSIG); 56742c63330SMimi Zohar result = 0; 56842c63330SMimi Zohar } 56942c63330SMimi Zohar return result; 57042c63330SMimi Zohar } 57142c63330SMimi Zohar 57242c63330SMimi Zohar int ima_inode_removexattr(struct dentry *dentry, const char *xattr_name) 57342c63330SMimi Zohar { 57442c63330SMimi Zohar int result; 57542c63330SMimi Zohar 57642c63330SMimi Zohar result = ima_protect_xattr(dentry, xattr_name, NULL, 0); 57742c63330SMimi Zohar if (result == 1) { 578c6f493d6SDavid Howells ima_reset_appraise_flags(d_backing_inode(dentry), 0); 57942c63330SMimi Zohar result = 0; 58042c63330SMimi Zohar } 58142c63330SMimi Zohar return result; 58242c63330SMimi Zohar } 583