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 */ 8*b000d5cbSArd Biesheuvel #include <linux/module.h> 9876979c9SPaul Gortmaker #include <linux/init.h> 102fe5d6deSMimi Zohar #include <linux/file.h> 112fe5d6deSMimi Zohar #include <linux/fs.h> 122fe5d6deSMimi Zohar #include <linux/xattr.h> 132fe5d6deSMimi Zohar #include <linux/magic.h> 142fe5d6deSMimi Zohar #include <linux/ima.h> 152fe5d6deSMimi Zohar #include <linux/evm.h> 16273df864SNayna Jain #include <keys/system_keyring.h> 172fe5d6deSMimi Zohar 182fe5d6deSMimi Zohar #include "ima.h" 192fe5d6deSMimi Zohar 20e1f5e01fSMimi Zohar #ifdef CONFIG_IMA_APPRAISE_BOOTPARAM 21*b000d5cbSArd Biesheuvel static char *ima_appraise_cmdline_default __initdata; 22*b000d5cbSArd Biesheuvel core_param(ima_appraise, ima_appraise_cmdline_default, charp, 0); 23*b000d5cbSArd Biesheuvel 24*b000d5cbSArd Biesheuvel void __init ima_appraise_parse_cmdline(void) 25*b000d5cbSArd Biesheuvel { 26*b000d5cbSArd Biesheuvel const char *str = ima_appraise_cmdline_default; 27e4d7e2dfSBruno Meneguele bool sb_state = arch_ima_get_secureboot(); 28e4d7e2dfSBruno Meneguele int appraisal_state = ima_appraise; 29311aa6aaSBruno Meneguele 30*b000d5cbSArd Biesheuvel if (!str) 31*b000d5cbSArd Biesheuvel return; 32*b000d5cbSArd Biesheuvel 332fe5d6deSMimi Zohar if (strncmp(str, "off", 3) == 0) 34e4d7e2dfSBruno Meneguele appraisal_state = 0; 352faa6ef3SDmitry Kasatkin else if (strncmp(str, "log", 3) == 0) 36e4d7e2dfSBruno Meneguele appraisal_state = IMA_APPRAISE_LOG; 372fe5d6deSMimi Zohar else if (strncmp(str, "fix", 3) == 0) 38e4d7e2dfSBruno Meneguele appraisal_state = IMA_APPRAISE_FIX; 394afb28abSBruno Meneguele else if (strncmp(str, "enforce", 7) == 0) 40e4d7e2dfSBruno Meneguele appraisal_state = IMA_APPRAISE_ENFORCE; 417fe2bb7eSBruno Meneguele else 427fe2bb7eSBruno Meneguele pr_err("invalid \"%s\" appraise option", str); 43e4d7e2dfSBruno Meneguele 44e4d7e2dfSBruno Meneguele /* If appraisal state was changed, but secure boot is enabled, 45e4d7e2dfSBruno Meneguele * keep its default */ 46e4d7e2dfSBruno Meneguele if (sb_state) { 47e4d7e2dfSBruno Meneguele if (!(appraisal_state & IMA_APPRAISE_ENFORCE)) 48e4d7e2dfSBruno Meneguele pr_info("Secure boot enabled: ignoring ima_appraise=%s option", 49e4d7e2dfSBruno Meneguele str); 50e4d7e2dfSBruno Meneguele } else { 51e4d7e2dfSBruno Meneguele ima_appraise = appraisal_state; 52e4d7e2dfSBruno Meneguele } 532fe5d6deSMimi Zohar } 54*b000d5cbSArd Biesheuvel #endif 552fe5d6deSMimi Zohar 562fe5d6deSMimi Zohar /* 576f6723e2SMimi Zohar * is_ima_appraise_enabled - return appraise status 586f6723e2SMimi Zohar * 596f6723e2SMimi Zohar * Only return enabled, if not in ima_appraise="fix" or "log" modes. 606f6723e2SMimi Zohar */ 616f6723e2SMimi Zohar bool is_ima_appraise_enabled(void) 626f6723e2SMimi Zohar { 63e5729f86SThiago Jung Bauermann return ima_appraise & IMA_APPRAISE_ENFORCE; 646f6723e2SMimi Zohar } 656f6723e2SMimi Zohar 666f6723e2SMimi Zohar /* 672fe5d6deSMimi Zohar * ima_must_appraise - set appraise flag 682fe5d6deSMimi Zohar * 69da1b0029SMimi Zohar * Return 1 to appraise or hash 702fe5d6deSMimi Zohar */ 71d26e1936SDmitry Kasatkin int ima_must_appraise(struct inode *inode, int mask, enum ima_hooks func) 722fe5d6deSMimi Zohar { 73d906c10dSMatthew Garrett u32 secid; 74d906c10dSMatthew Garrett 7507f6a794SMimi Zohar if (!ima_appraise) 762fe5d6deSMimi Zohar return 0; 7707f6a794SMimi Zohar 78d906c10dSMatthew Garrett security_task_getsecid(current, &secid); 79d906c10dSMatthew Garrett return ima_match_policy(inode, current_cred(), secid, func, mask, 80e9085e0aSLakshmi Ramasubramanian IMA_APPRAISE | IMA_HASH, NULL, NULL, NULL); 812fe5d6deSMimi Zohar } 822fe5d6deSMimi Zohar 83def3e8b9SDmitry Kasatkin static int ima_fix_xattr(struct dentry *dentry, 842fe5d6deSMimi Zohar struct integrity_iint_cache *iint) 852fe5d6deSMimi Zohar { 863ea7a560SDmitry Kasatkin int rc, offset; 873ea7a560SDmitry Kasatkin u8 algo = iint->ima_hash->algo; 883ea7a560SDmitry Kasatkin 893ea7a560SDmitry Kasatkin if (algo <= HASH_ALGO_SHA1) { 903ea7a560SDmitry Kasatkin offset = 1; 913ea7a560SDmitry Kasatkin iint->ima_hash->xattr.sha1.type = IMA_XATTR_DIGEST; 923ea7a560SDmitry Kasatkin } else { 933ea7a560SDmitry Kasatkin offset = 0; 943ea7a560SDmitry Kasatkin iint->ima_hash->xattr.ng.type = IMA_XATTR_DIGEST_NG; 953ea7a560SDmitry Kasatkin iint->ima_hash->xattr.ng.algo = algo; 963ea7a560SDmitry Kasatkin } 973ea7a560SDmitry Kasatkin rc = __vfs_setxattr_noperm(dentry, XATTR_NAME_IMA, 983ea7a560SDmitry Kasatkin &iint->ima_hash->xattr.data[offset], 993ea7a560SDmitry Kasatkin (sizeof(iint->ima_hash->xattr) - offset) + 1003ea7a560SDmitry Kasatkin iint->ima_hash->length, 0); 1013ea7a560SDmitry Kasatkin return rc; 1022fe5d6deSMimi Zohar } 1032fe5d6deSMimi Zohar 104d79d72e0SMimi Zohar /* Return specific func appraised cached result */ 105d79d72e0SMimi Zohar enum integrity_status ima_get_cache_status(struct integrity_iint_cache *iint, 1064ad87a3dSMimi Zohar enum ima_hooks func) 107d79d72e0SMimi Zohar { 108d79d72e0SMimi Zohar switch (func) { 109d79d72e0SMimi Zohar case MMAP_CHECK: 110d79d72e0SMimi Zohar return iint->ima_mmap_status; 111d79d72e0SMimi Zohar case BPRM_CHECK: 112d79d72e0SMimi Zohar return iint->ima_bprm_status; 113d906c10dSMatthew Garrett case CREDS_CHECK: 114d906c10dSMatthew Garrett return iint->ima_creds_status; 115d79d72e0SMimi Zohar case FILE_CHECK: 116c6af8efeSMimi Zohar case POST_SETATTR: 117d79d72e0SMimi Zohar return iint->ima_file_status; 118c6af8efeSMimi Zohar case MODULE_CHECK ... MAX_CHECK - 1: 119c6af8efeSMimi Zohar default: 120c6af8efeSMimi Zohar return iint->ima_read_status; 121d79d72e0SMimi Zohar } 122d79d72e0SMimi Zohar } 123d79d72e0SMimi Zohar 124d79d72e0SMimi Zohar static void ima_set_cache_status(struct integrity_iint_cache *iint, 1254ad87a3dSMimi Zohar enum ima_hooks func, 1264ad87a3dSMimi Zohar enum integrity_status status) 127d79d72e0SMimi Zohar { 128d79d72e0SMimi Zohar switch (func) { 129d79d72e0SMimi Zohar case MMAP_CHECK: 130d79d72e0SMimi Zohar iint->ima_mmap_status = status; 131d79d72e0SMimi Zohar break; 132d79d72e0SMimi Zohar case BPRM_CHECK: 133d79d72e0SMimi Zohar iint->ima_bprm_status = status; 134d79d72e0SMimi Zohar break; 135d906c10dSMatthew Garrett case CREDS_CHECK: 136d906c10dSMatthew Garrett iint->ima_creds_status = status; 13709186e50SGustavo A. R. Silva break; 138d79d72e0SMimi Zohar case FILE_CHECK: 139c6af8efeSMimi Zohar case POST_SETATTR: 140d79d72e0SMimi Zohar iint->ima_file_status = status; 141c6af8efeSMimi Zohar break; 142c6af8efeSMimi Zohar case MODULE_CHECK ... MAX_CHECK - 1: 143c6af8efeSMimi Zohar default: 144c6af8efeSMimi Zohar iint->ima_read_status = status; 145c6af8efeSMimi Zohar break; 146d79d72e0SMimi Zohar } 147d79d72e0SMimi Zohar } 148d79d72e0SMimi Zohar 1494ad87a3dSMimi Zohar static void ima_cache_flags(struct integrity_iint_cache *iint, 1504ad87a3dSMimi Zohar enum ima_hooks func) 151d79d72e0SMimi Zohar { 152d79d72e0SMimi Zohar switch (func) { 153d79d72e0SMimi Zohar case MMAP_CHECK: 154d79d72e0SMimi Zohar iint->flags |= (IMA_MMAP_APPRAISED | IMA_APPRAISED); 155d79d72e0SMimi Zohar break; 156d79d72e0SMimi Zohar case BPRM_CHECK: 157d79d72e0SMimi Zohar iint->flags |= (IMA_BPRM_APPRAISED | IMA_APPRAISED); 158d79d72e0SMimi Zohar break; 159d906c10dSMatthew Garrett case CREDS_CHECK: 160d906c10dSMatthew Garrett iint->flags |= (IMA_CREDS_APPRAISED | IMA_APPRAISED); 161d906c10dSMatthew Garrett break; 162d79d72e0SMimi Zohar case FILE_CHECK: 163c6af8efeSMimi Zohar case POST_SETATTR: 164d79d72e0SMimi Zohar iint->flags |= (IMA_FILE_APPRAISED | IMA_APPRAISED); 165c6af8efeSMimi Zohar break; 166c6af8efeSMimi Zohar case MODULE_CHECK ... MAX_CHECK - 1: 167c6af8efeSMimi Zohar default: 168c6af8efeSMimi Zohar iint->flags |= (IMA_READ_APPRAISED | IMA_APPRAISED); 169c6af8efeSMimi Zohar break; 170d79d72e0SMimi Zohar } 171d79d72e0SMimi Zohar } 172d79d72e0SMimi Zohar 1731525b06dSDmitry Kasatkin enum hash_algo ima_get_hash_algo(struct evm_ima_xattr_data *xattr_value, 1741525b06dSDmitry Kasatkin int xattr_len) 175d3634d0fSDmitry Kasatkin { 176d3634d0fSDmitry Kasatkin struct signature_v2_hdr *sig; 177b4bfec7fSSeth Forshee enum hash_algo ret; 178d3634d0fSDmitry Kasatkin 1793ea7a560SDmitry Kasatkin if (!xattr_value || xattr_len < 2) 1801525b06dSDmitry Kasatkin /* return default hash algo */ 1811525b06dSDmitry Kasatkin return ima_hash_algo; 182d3634d0fSDmitry Kasatkin 1833ea7a560SDmitry Kasatkin switch (xattr_value->type) { 1843ea7a560SDmitry Kasatkin case EVM_IMA_XATTR_DIGSIG: 1853ea7a560SDmitry Kasatkin sig = (typeof(sig))xattr_value; 1863ea7a560SDmitry Kasatkin if (sig->version != 2 || xattr_len <= sizeof(*sig)) 1871525b06dSDmitry Kasatkin return ima_hash_algo; 1881525b06dSDmitry Kasatkin return sig->hash_algo; 1893ea7a560SDmitry Kasatkin break; 1903ea7a560SDmitry Kasatkin case IMA_XATTR_DIGEST_NG: 191650b29dbSThiago Jung Bauermann /* first byte contains algorithm id */ 192650b29dbSThiago Jung Bauermann ret = xattr_value->data[0]; 193b4bfec7fSSeth Forshee if (ret < HASH_ALGO__LAST) 194b4bfec7fSSeth Forshee return ret; 1953ea7a560SDmitry Kasatkin break; 1963ea7a560SDmitry Kasatkin case IMA_XATTR_DIGEST: 1973ea7a560SDmitry Kasatkin /* this is for backward compatibility */ 1983ea7a560SDmitry Kasatkin if (xattr_len == 21) { 1993ea7a560SDmitry Kasatkin unsigned int zero = 0; 200650b29dbSThiago Jung Bauermann if (!memcmp(&xattr_value->data[16], &zero, 4)) 2011525b06dSDmitry Kasatkin return HASH_ALGO_MD5; 2023ea7a560SDmitry Kasatkin else 2031525b06dSDmitry Kasatkin return HASH_ALGO_SHA1; 2043ea7a560SDmitry Kasatkin } else if (xattr_len == 17) 2051525b06dSDmitry Kasatkin return HASH_ALGO_MD5; 2063ea7a560SDmitry Kasatkin break; 2073ea7a560SDmitry Kasatkin } 2081525b06dSDmitry Kasatkin 2091525b06dSDmitry Kasatkin /* return default hash algo */ 2101525b06dSDmitry Kasatkin return ima_hash_algo; 211d3634d0fSDmitry Kasatkin } 212d3634d0fSDmitry Kasatkin 213d3634d0fSDmitry Kasatkin int ima_read_xattr(struct dentry *dentry, 214d3634d0fSDmitry Kasatkin struct evm_ima_xattr_data **xattr_value) 215d3634d0fSDmitry Kasatkin { 2165d6c3191SAndreas Gruenbacher ssize_t ret; 217d3634d0fSDmitry Kasatkin 2185d6c3191SAndreas Gruenbacher ret = vfs_getxattr_alloc(dentry, XATTR_NAME_IMA, (char **)xattr_value, 219d3634d0fSDmitry Kasatkin 0, GFP_NOFS); 2205d6c3191SAndreas Gruenbacher if (ret == -EOPNOTSUPP) 2215d6c3191SAndreas Gruenbacher ret = 0; 2225d6c3191SAndreas Gruenbacher return ret; 223d3634d0fSDmitry Kasatkin } 224d3634d0fSDmitry Kasatkin 2252fe5d6deSMimi Zohar /* 226a5fbeb61SThiago Jung Bauermann * xattr_verify - verify xattr digest or signature 227a5fbeb61SThiago Jung Bauermann * 228a5fbeb61SThiago Jung Bauermann * Verify whether the hash or signature matches the file contents. 229a5fbeb61SThiago Jung Bauermann * 230a5fbeb61SThiago Jung Bauermann * Return 0 on success, error code otherwise. 231a5fbeb61SThiago Jung Bauermann */ 232a5fbeb61SThiago Jung Bauermann static int xattr_verify(enum ima_hooks func, struct integrity_iint_cache *iint, 233a5fbeb61SThiago Jung Bauermann struct evm_ima_xattr_data *xattr_value, int xattr_len, 234a5fbeb61SThiago Jung Bauermann enum integrity_status *status, const char **cause) 235a5fbeb61SThiago Jung Bauermann { 236a5fbeb61SThiago Jung Bauermann int rc = -EINVAL, hash_start = 0; 237a5fbeb61SThiago Jung Bauermann 238a5fbeb61SThiago Jung Bauermann switch (xattr_value->type) { 239a5fbeb61SThiago Jung Bauermann case IMA_XATTR_DIGEST_NG: 240a5fbeb61SThiago Jung Bauermann /* first byte contains algorithm id */ 241a5fbeb61SThiago Jung Bauermann hash_start = 1; 242df561f66SGustavo A. R. Silva fallthrough; 243a5fbeb61SThiago Jung Bauermann case IMA_XATTR_DIGEST: 244a5fbeb61SThiago Jung Bauermann if (iint->flags & IMA_DIGSIG_REQUIRED) { 245a5fbeb61SThiago Jung Bauermann *cause = "IMA-signature-required"; 246a5fbeb61SThiago Jung Bauermann *status = INTEGRITY_FAIL; 247a5fbeb61SThiago Jung Bauermann break; 248a5fbeb61SThiago Jung Bauermann } 249a5fbeb61SThiago Jung Bauermann clear_bit(IMA_DIGSIG, &iint->atomic_flags); 250a5fbeb61SThiago Jung Bauermann if (xattr_len - sizeof(xattr_value->type) - hash_start >= 251a5fbeb61SThiago Jung Bauermann iint->ima_hash->length) 252a5fbeb61SThiago Jung Bauermann /* 253a5fbeb61SThiago Jung Bauermann * xattr length may be longer. md5 hash in previous 254a5fbeb61SThiago Jung Bauermann * version occupied 20 bytes in xattr, instead of 16 255a5fbeb61SThiago Jung Bauermann */ 256a5fbeb61SThiago Jung Bauermann rc = memcmp(&xattr_value->data[hash_start], 257a5fbeb61SThiago Jung Bauermann iint->ima_hash->digest, 258a5fbeb61SThiago Jung Bauermann iint->ima_hash->length); 259a5fbeb61SThiago Jung Bauermann else 260a5fbeb61SThiago Jung Bauermann rc = -EINVAL; 261a5fbeb61SThiago Jung Bauermann if (rc) { 262a5fbeb61SThiago Jung Bauermann *cause = "invalid-hash"; 263a5fbeb61SThiago Jung Bauermann *status = INTEGRITY_FAIL; 264a5fbeb61SThiago Jung Bauermann break; 265a5fbeb61SThiago Jung Bauermann } 266a5fbeb61SThiago Jung Bauermann *status = INTEGRITY_PASS; 267a5fbeb61SThiago Jung Bauermann break; 268a5fbeb61SThiago Jung Bauermann case EVM_IMA_XATTR_DIGSIG: 269a5fbeb61SThiago Jung Bauermann set_bit(IMA_DIGSIG, &iint->atomic_flags); 270a5fbeb61SThiago Jung Bauermann rc = integrity_digsig_verify(INTEGRITY_KEYRING_IMA, 271a5fbeb61SThiago Jung Bauermann (const char *)xattr_value, 272a5fbeb61SThiago Jung Bauermann xattr_len, 273a5fbeb61SThiago Jung Bauermann iint->ima_hash->digest, 274a5fbeb61SThiago Jung Bauermann iint->ima_hash->length); 275a5fbeb61SThiago Jung Bauermann if (rc == -EOPNOTSUPP) { 276a5fbeb61SThiago Jung Bauermann *status = INTEGRITY_UNKNOWN; 277a5fbeb61SThiago Jung Bauermann break; 278a5fbeb61SThiago Jung Bauermann } 279a5fbeb61SThiago Jung Bauermann if (IS_ENABLED(CONFIG_INTEGRITY_PLATFORM_KEYRING) && rc && 280a5fbeb61SThiago Jung Bauermann func == KEXEC_KERNEL_CHECK) 281a5fbeb61SThiago Jung Bauermann rc = integrity_digsig_verify(INTEGRITY_KEYRING_PLATFORM, 282a5fbeb61SThiago Jung Bauermann (const char *)xattr_value, 283a5fbeb61SThiago Jung Bauermann xattr_len, 284a5fbeb61SThiago Jung Bauermann iint->ima_hash->digest, 285a5fbeb61SThiago Jung Bauermann iint->ima_hash->length); 286a5fbeb61SThiago Jung Bauermann if (rc) { 287a5fbeb61SThiago Jung Bauermann *cause = "invalid-signature"; 288a5fbeb61SThiago Jung Bauermann *status = INTEGRITY_FAIL; 289a5fbeb61SThiago Jung Bauermann } else { 290a5fbeb61SThiago Jung Bauermann *status = INTEGRITY_PASS; 291a5fbeb61SThiago Jung Bauermann } 292a5fbeb61SThiago Jung Bauermann break; 293a5fbeb61SThiago Jung Bauermann default: 294a5fbeb61SThiago Jung Bauermann *status = INTEGRITY_UNKNOWN; 295a5fbeb61SThiago Jung Bauermann *cause = "unknown-ima-data"; 296a5fbeb61SThiago Jung Bauermann break; 297a5fbeb61SThiago Jung Bauermann } 298a5fbeb61SThiago Jung Bauermann 299a5fbeb61SThiago Jung Bauermann return rc; 300a5fbeb61SThiago Jung Bauermann } 301a5fbeb61SThiago Jung Bauermann 302a5fbeb61SThiago Jung Bauermann /* 30339b07096SThiago Jung Bauermann * modsig_verify - verify modsig signature 30439b07096SThiago Jung Bauermann * 30539b07096SThiago Jung Bauermann * Verify whether the signature matches the file contents. 30639b07096SThiago Jung Bauermann * 30739b07096SThiago Jung Bauermann * Return 0 on success, error code otherwise. 30839b07096SThiago Jung Bauermann */ 30939b07096SThiago Jung Bauermann static int modsig_verify(enum ima_hooks func, const struct modsig *modsig, 31039b07096SThiago Jung Bauermann enum integrity_status *status, const char **cause) 31139b07096SThiago Jung Bauermann { 31239b07096SThiago Jung Bauermann int rc; 31339b07096SThiago Jung Bauermann 31439b07096SThiago Jung Bauermann rc = integrity_modsig_verify(INTEGRITY_KEYRING_IMA, modsig); 31539b07096SThiago Jung Bauermann if (IS_ENABLED(CONFIG_INTEGRITY_PLATFORM_KEYRING) && rc && 31639b07096SThiago Jung Bauermann func == KEXEC_KERNEL_CHECK) 31739b07096SThiago Jung Bauermann rc = integrity_modsig_verify(INTEGRITY_KEYRING_PLATFORM, 31839b07096SThiago Jung Bauermann modsig); 31939b07096SThiago Jung Bauermann if (rc) { 32039b07096SThiago Jung Bauermann *cause = "invalid-signature"; 32139b07096SThiago Jung Bauermann *status = INTEGRITY_FAIL; 32239b07096SThiago Jung Bauermann } else { 32339b07096SThiago Jung Bauermann *status = INTEGRITY_PASS; 32439b07096SThiago Jung Bauermann } 32539b07096SThiago Jung Bauermann 32639b07096SThiago Jung Bauermann return rc; 32739b07096SThiago Jung Bauermann } 32839b07096SThiago Jung Bauermann 32939b07096SThiago Jung Bauermann /* 330273df864SNayna Jain * ima_check_blacklist - determine if the binary is blacklisted. 331273df864SNayna Jain * 332273df864SNayna Jain * Add the hash of the blacklisted binary to the measurement list, based 333273df864SNayna Jain * on policy. 334273df864SNayna Jain * 335273df864SNayna Jain * Returns -EPERM if the hash is blacklisted. 336273df864SNayna Jain */ 337273df864SNayna Jain int ima_check_blacklist(struct integrity_iint_cache *iint, 338273df864SNayna Jain const struct modsig *modsig, int pcr) 339273df864SNayna Jain { 340273df864SNayna Jain enum hash_algo hash_algo; 341273df864SNayna Jain const u8 *digest = NULL; 342273df864SNayna Jain u32 digestsize = 0; 343273df864SNayna Jain int rc = 0; 344273df864SNayna Jain 345273df864SNayna Jain if (!(iint->flags & IMA_CHECK_BLACKLIST)) 346273df864SNayna Jain return 0; 347273df864SNayna Jain 348273df864SNayna Jain if (iint->flags & IMA_MODSIG_ALLOWED && modsig) { 349273df864SNayna Jain ima_get_modsig_digest(modsig, &hash_algo, &digest, &digestsize); 350273df864SNayna Jain 351273df864SNayna Jain rc = is_binary_blacklisted(digest, digestsize); 352273df864SNayna Jain if ((rc == -EPERM) && (iint->flags & IMA_MEASURE)) 3534834177eSTyler Hicks process_buffer_measurement(NULL, digest, digestsize, 354273df864SNayna Jain "blacklisted-hash", NONE, 355e9085e0aSLakshmi Ramasubramanian pcr, NULL); 356273df864SNayna Jain } 357273df864SNayna Jain 358273df864SNayna Jain return rc; 359273df864SNayna Jain } 360273df864SNayna Jain 361273df864SNayna Jain /* 3622fe5d6deSMimi Zohar * ima_appraise_measurement - appraise file measurement 3632fe5d6deSMimi Zohar * 3642fe5d6deSMimi Zohar * Call evm_verifyxattr() to verify the integrity of 'security.ima'. 3652fe5d6deSMimi Zohar * Assuming success, compare the xattr hash with the collected measurement. 3662fe5d6deSMimi Zohar * 3672fe5d6deSMimi Zohar * Return 0 on success, error code otherwise 3682fe5d6deSMimi Zohar */ 3694ad87a3dSMimi Zohar int ima_appraise_measurement(enum ima_hooks func, 3704ad87a3dSMimi Zohar struct integrity_iint_cache *iint, 371d3634d0fSDmitry Kasatkin struct file *file, const unsigned char *filename, 372d3634d0fSDmitry Kasatkin struct evm_ima_xattr_data *xattr_value, 37339b07096SThiago Jung Bauermann int xattr_len, const struct modsig *modsig) 3742fe5d6deSMimi Zohar { 37552a13284SMimi Zohar static const char op[] = "appraise_data"; 376f5e51fa3SThiago Jung Bauermann const char *cause = "unknown"; 377e71b9dffSMiklos Szeredi struct dentry *dentry = file_dentry(file); 378c6f493d6SDavid Howells struct inode *inode = d_backing_inode(dentry); 3792fe5d6deSMimi Zohar enum integrity_status status = INTEGRITY_UNKNOWN; 380a5fbeb61SThiago Jung Bauermann int rc = xattr_len; 38139b07096SThiago Jung Bauermann bool try_modsig = iint->flags & IMA_MODSIG_ALLOWED && modsig; 3822fe5d6deSMimi Zohar 38339b07096SThiago Jung Bauermann /* If not appraising a modsig, we need an xattr. */ 38439b07096SThiago Jung Bauermann if (!(inode->i_opflags & IOP_XATTR) && !try_modsig) 3852fe5d6deSMimi Zohar return INTEGRITY_UNKNOWN; 3862fe5d6deSMimi Zohar 38739b07096SThiago Jung Bauermann /* If reading the xattr failed and there's no modsig, error out. */ 38839b07096SThiago Jung Bauermann if (rc <= 0 && !try_modsig) { 3892fe5d6deSMimi Zohar if (rc && rc != -ENODATA) 3902fe5d6deSMimi Zohar goto out; 3912fe5d6deSMimi Zohar 392915d9d25SThiago Jung Bauermann cause = iint->flags & IMA_DIGSIG_REQUIRED ? 393915d9d25SThiago Jung Bauermann "IMA-signature-required" : "missing-hash"; 394b151d6b0SDmitry Kasatkin status = INTEGRITY_NOLABEL; 3956035a27bSAl Viro if (file->f_mode & FMODE_CREATED) 396b151d6b0SDmitry Kasatkin iint->flags |= IMA_NEW_FILE; 3971ac202e9SDaniel Glöckner if ((iint->flags & IMA_NEW_FILE) && 398b7e27bc1SMimi Zohar (!(iint->flags & IMA_DIGSIG_REQUIRED) || 399b7e27bc1SMimi Zohar (inode->i_size == 0))) 400b151d6b0SDmitry Kasatkin status = INTEGRITY_PASS; 4012fe5d6deSMimi Zohar goto out; 4022fe5d6deSMimi Zohar } 4032fe5d6deSMimi Zohar 4048606404fSDmitry Kasatkin status = evm_verifyxattr(dentry, XATTR_NAME_IMA, xattr_value, rc, iint); 405f5e51fa3SThiago Jung Bauermann switch (status) { 406f5e51fa3SThiago Jung Bauermann case INTEGRITY_PASS: 407f5e51fa3SThiago Jung Bauermann case INTEGRITY_PASS_IMMUTABLE: 408f5e51fa3SThiago Jung Bauermann case INTEGRITY_UNKNOWN: 409f5e51fa3SThiago Jung Bauermann break; 410f5e51fa3SThiago Jung Bauermann case INTEGRITY_NOXATTRS: /* No EVM protected xattrs. */ 41139b07096SThiago Jung Bauermann /* It's fine not to have xattrs when using a modsig. */ 41239b07096SThiago Jung Bauermann if (try_modsig) 41339b07096SThiago Jung Bauermann break; 414df561f66SGustavo A. R. Silva fallthrough; 415f5e51fa3SThiago Jung Bauermann case INTEGRITY_NOLABEL: /* No security.evm xattr. */ 4162fe5d6deSMimi Zohar cause = "missing-HMAC"; 417f5e51fa3SThiago Jung Bauermann goto out; 418f5e51fa3SThiago Jung Bauermann case INTEGRITY_FAIL: /* Invalid HMAC/signature. */ 4192fe5d6deSMimi Zohar cause = "invalid-HMAC"; 4202fe5d6deSMimi Zohar goto out; 421f5e51fa3SThiago Jung Bauermann default: 422f5e51fa3SThiago Jung Bauermann WARN_ONCE(true, "Unexpected integrity status %d\n", status); 4232fe5d6deSMimi Zohar } 424f5e51fa3SThiago Jung Bauermann 425a5fbeb61SThiago Jung Bauermann if (xattr_value) 426a5fbeb61SThiago Jung Bauermann rc = xattr_verify(func, iint, xattr_value, xattr_len, &status, 427a5fbeb61SThiago Jung Bauermann &cause); 4288606404fSDmitry Kasatkin 42939b07096SThiago Jung Bauermann /* 43039b07096SThiago Jung Bauermann * If we have a modsig and either no imasig or the imasig's key isn't 43139b07096SThiago Jung Bauermann * known, then try verifying the modsig. 43239b07096SThiago Jung Bauermann */ 43339b07096SThiago Jung Bauermann if (try_modsig && 43439b07096SThiago Jung Bauermann (!xattr_value || xattr_value->type == IMA_XATTR_DIGEST_NG || 43539b07096SThiago Jung Bauermann rc == -ENOKEY)) 43639b07096SThiago Jung Bauermann rc = modsig_verify(func, modsig, &status, &cause); 43739b07096SThiago Jung Bauermann 4382fe5d6deSMimi Zohar out: 43957b56ac6SMimi Zohar /* 44057b56ac6SMimi Zohar * File signatures on some filesystems can not be properly verified. 4419e67028eSMimi Zohar * When such filesystems are mounted by an untrusted mounter or on a 4429e67028eSMimi Zohar * system not willing to accept such a risk, fail the file signature 4439e67028eSMimi Zohar * verification. 44457b56ac6SMimi Zohar */ 4459e67028eSMimi Zohar if ((inode->i_sb->s_iflags & SB_I_IMA_UNVERIFIABLE_SIGNATURE) && 4469e67028eSMimi Zohar ((inode->i_sb->s_iflags & SB_I_UNTRUSTED_MOUNTER) || 4479e67028eSMimi Zohar (iint->flags & IMA_FAIL_UNVERIFIABLE_SIGS))) { 44857b56ac6SMimi Zohar status = INTEGRITY_FAIL; 44957b56ac6SMimi Zohar cause = "unverifiable-signature"; 45057b56ac6SMimi Zohar integrity_audit_msg(AUDIT_INTEGRITY_DATA, inode, filename, 45157b56ac6SMimi Zohar op, cause, rc, 0); 45257b56ac6SMimi Zohar } else if (status != INTEGRITY_PASS) { 453f5e51fa3SThiago Jung Bauermann /* Fix mode, but don't replace file signatures. */ 45439b07096SThiago Jung Bauermann if ((ima_appraise & IMA_APPRAISE_FIX) && !try_modsig && 4558606404fSDmitry Kasatkin (!xattr_value || 4568606404fSDmitry Kasatkin xattr_value->type != EVM_IMA_XATTR_DIGSIG)) { 457def3e8b9SDmitry Kasatkin if (!ima_fix_xattr(dentry, iint)) 4582fe5d6deSMimi Zohar status = INTEGRITY_PASS; 459f5e51fa3SThiago Jung Bauermann } 460f5e51fa3SThiago Jung Bauermann 461f5e51fa3SThiago Jung Bauermann /* Permit new files with file signatures, but without data. */ 462f5e51fa3SThiago Jung Bauermann if (inode->i_size == 0 && iint->flags & IMA_NEW_FILE && 463f5e51fa3SThiago Jung Bauermann xattr_value && xattr_value->type == EVM_IMA_XATTR_DIGSIG) { 46405d1a717SMimi Zohar status = INTEGRITY_PASS; 4652fe5d6deSMimi Zohar } 466f5e51fa3SThiago Jung Bauermann 4672fe5d6deSMimi Zohar integrity_audit_msg(AUDIT_INTEGRITY_DATA, inode, filename, 4682fe5d6deSMimi Zohar op, cause, rc, 0); 4698606404fSDmitry Kasatkin } else { 470d79d72e0SMimi Zohar ima_cache_flags(iint, func); 4712fe5d6deSMimi Zohar } 47257b56ac6SMimi Zohar 473d79d72e0SMimi Zohar ima_set_cache_status(iint, func, status); 4742fe5d6deSMimi Zohar return status; 4752fe5d6deSMimi Zohar } 4762fe5d6deSMimi Zohar 4772fe5d6deSMimi Zohar /* 4782fe5d6deSMimi Zohar * ima_update_xattr - update 'security.ima' hash value 4792fe5d6deSMimi Zohar */ 4802fe5d6deSMimi Zohar void ima_update_xattr(struct integrity_iint_cache *iint, struct file *file) 4812fe5d6deSMimi Zohar { 482e71b9dffSMiklos Szeredi struct dentry *dentry = file_dentry(file); 4832fe5d6deSMimi Zohar int rc = 0; 4842fe5d6deSMimi Zohar 4858606404fSDmitry Kasatkin /* do not collect and update hash for digital signatures */ 4860d73a552SDmitry Kasatkin if (test_bit(IMA_DIGSIG, &iint->atomic_flags)) 4878606404fSDmitry Kasatkin return; 4888606404fSDmitry Kasatkin 489da1b0029SMimi Zohar if ((iint->ima_file_status != INTEGRITY_PASS) && 490da1b0029SMimi Zohar !(iint->flags & IMA_HASH)) 491020aae3eSRoberto Sassu return; 492020aae3eSRoberto Sassu 49315588227SThiago Jung Bauermann rc = ima_collect_measurement(iint, file, NULL, 0, ima_hash_algo, NULL); 4942fe5d6deSMimi Zohar if (rc < 0) 4952fe5d6deSMimi Zohar return; 4968606404fSDmitry Kasatkin 4970d73a552SDmitry Kasatkin inode_lock(file_inode(file)); 4982fe5d6deSMimi Zohar ima_fix_xattr(dentry, iint); 4990d73a552SDmitry Kasatkin inode_unlock(file_inode(file)); 5002fe5d6deSMimi Zohar } 5012fe5d6deSMimi Zohar 5022fe5d6deSMimi Zohar /** 5032fe5d6deSMimi Zohar * ima_inode_post_setattr - reflect file metadata changes 5042fe5d6deSMimi Zohar * @dentry: pointer to the affected dentry 5052fe5d6deSMimi Zohar * 5062fe5d6deSMimi Zohar * Changes to a dentry's metadata might result in needing to appraise. 5072fe5d6deSMimi Zohar * 5082fe5d6deSMimi Zohar * This function is called from notify_change(), which expects the caller 5092fe5d6deSMimi Zohar * to lock the inode's i_mutex. 5102fe5d6deSMimi Zohar */ 5112fe5d6deSMimi Zohar void ima_inode_post_setattr(struct dentry *dentry) 5122fe5d6deSMimi Zohar { 513c6f493d6SDavid Howells struct inode *inode = d_backing_inode(dentry); 5142fe5d6deSMimi Zohar struct integrity_iint_cache *iint; 515da1b0029SMimi Zohar int action; 5162fe5d6deSMimi Zohar 517a756024eSRoberto Sassu if (!(ima_policy_flag & IMA_APPRAISE) || !S_ISREG(inode->i_mode) 5185d6c3191SAndreas Gruenbacher || !(inode->i_opflags & IOP_XATTR)) 5192fe5d6deSMimi Zohar return; 5202fe5d6deSMimi Zohar 521da1b0029SMimi Zohar action = ima_must_appraise(inode, MAY_ACCESS, POST_SETATTR); 522da1b0029SMimi Zohar if (!action) 5235d6c3191SAndreas Gruenbacher __vfs_removexattr(dentry, XATTR_NAME_IMA); 5240d73a552SDmitry Kasatkin iint = integrity_iint_find(inode); 5250d73a552SDmitry Kasatkin if (iint) { 5260d73a552SDmitry Kasatkin set_bit(IMA_CHANGE_ATTR, &iint->atomic_flags); 527da1b0029SMimi Zohar if (!action) 5280d73a552SDmitry Kasatkin clear_bit(IMA_UPDATE_XATTR, &iint->atomic_flags); 5290d73a552SDmitry Kasatkin } 5302fe5d6deSMimi Zohar } 53142c63330SMimi Zohar 53242c63330SMimi Zohar /* 53342c63330SMimi Zohar * ima_protect_xattr - protect 'security.ima' 53442c63330SMimi Zohar * 53542c63330SMimi Zohar * Ensure that not just anyone can modify or remove 'security.ima'. 53642c63330SMimi Zohar */ 53742c63330SMimi Zohar static int ima_protect_xattr(struct dentry *dentry, const char *xattr_name, 53842c63330SMimi Zohar const void *xattr_value, size_t xattr_value_len) 53942c63330SMimi Zohar { 54042c63330SMimi Zohar if (strcmp(xattr_name, XATTR_NAME_IMA) == 0) { 54142c63330SMimi Zohar if (!capable(CAP_SYS_ADMIN)) 54242c63330SMimi Zohar return -EPERM; 54342c63330SMimi Zohar return 1; 54442c63330SMimi Zohar } 54542c63330SMimi Zohar return 0; 54642c63330SMimi Zohar } 54742c63330SMimi Zohar 548060bdebfSMimi Zohar static void ima_reset_appraise_flags(struct inode *inode, int digsig) 54942c63330SMimi Zohar { 55042c63330SMimi Zohar struct integrity_iint_cache *iint; 55142c63330SMimi Zohar 552a756024eSRoberto Sassu if (!(ima_policy_flag & IMA_APPRAISE) || !S_ISREG(inode->i_mode)) 55342c63330SMimi Zohar return; 55442c63330SMimi Zohar 55542c63330SMimi Zohar iint = integrity_iint_find(inode); 55642c63330SMimi Zohar if (!iint) 55742c63330SMimi Zohar return; 558a422638dSEric Richter iint->measured_pcrs = 0; 5590d73a552SDmitry Kasatkin set_bit(IMA_CHANGE_XATTR, &iint->atomic_flags); 560060bdebfSMimi Zohar if (digsig) 5610d73a552SDmitry Kasatkin set_bit(IMA_DIGSIG, &iint->atomic_flags); 5620d73a552SDmitry Kasatkin else 5630d73a552SDmitry Kasatkin clear_bit(IMA_DIGSIG, &iint->atomic_flags); 56442c63330SMimi Zohar } 56542c63330SMimi Zohar 56642c63330SMimi Zohar int ima_inode_setxattr(struct dentry *dentry, const char *xattr_name, 56742c63330SMimi Zohar const void *xattr_value, size_t xattr_value_len) 56842c63330SMimi Zohar { 569060bdebfSMimi Zohar const struct evm_ima_xattr_data *xvalue = xattr_value; 57042c63330SMimi Zohar int result; 57142c63330SMimi Zohar 57242c63330SMimi Zohar result = ima_protect_xattr(dentry, xattr_name, xattr_value, 57342c63330SMimi Zohar xattr_value_len); 57442c63330SMimi Zohar if (result == 1) { 575a48fda9dSDmitry Kasatkin if (!xattr_value_len || (xvalue->type >= IMA_XATTR_LAST)) 576a48fda9dSDmitry Kasatkin return -EINVAL; 577f5acb3dcSMimi Zohar ima_reset_appraise_flags(d_backing_inode(dentry), 578e5729f86SThiago Jung Bauermann xvalue->type == EVM_IMA_XATTR_DIGSIG); 57942c63330SMimi Zohar result = 0; 58042c63330SMimi Zohar } 58142c63330SMimi Zohar return result; 58242c63330SMimi Zohar } 58342c63330SMimi Zohar 58442c63330SMimi Zohar int ima_inode_removexattr(struct dentry *dentry, const char *xattr_name) 58542c63330SMimi Zohar { 58642c63330SMimi Zohar int result; 58742c63330SMimi Zohar 58842c63330SMimi Zohar result = ima_protect_xattr(dentry, xattr_name, NULL, 0); 58942c63330SMimi Zohar if (result == 1) { 590c6f493d6SDavid Howells ima_reset_appraise_flags(d_backing_inode(dentry), 0); 59142c63330SMimi Zohar result = 0; 59242c63330SMimi Zohar } 59342c63330SMimi Zohar return result; 59442c63330SMimi Zohar } 595