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 */ 8b000d5cbSArd 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> 16398c42e2SMimi Zohar #include <linux/fsverity.h> 17273df864SNayna Jain #include <keys/system_keyring.h> 18398c42e2SMimi Zohar #include <uapi/linux/fsverity.h> 192fe5d6deSMimi Zohar 202fe5d6deSMimi Zohar #include "ima.h" 212fe5d6deSMimi Zohar 22e1f5e01fSMimi Zohar #ifdef CONFIG_IMA_APPRAISE_BOOTPARAM 23b000d5cbSArd Biesheuvel static char *ima_appraise_cmdline_default __initdata; 24b000d5cbSArd Biesheuvel core_param(ima_appraise, ima_appraise_cmdline_default, charp, 0); 25b000d5cbSArd Biesheuvel 26b000d5cbSArd Biesheuvel void __init ima_appraise_parse_cmdline(void) 27b000d5cbSArd Biesheuvel { 28b000d5cbSArd Biesheuvel const char *str = ima_appraise_cmdline_default; 29e4d7e2dfSBruno Meneguele bool sb_state = arch_ima_get_secureboot(); 30e4d7e2dfSBruno Meneguele int appraisal_state = ima_appraise; 31311aa6aaSBruno Meneguele 32b000d5cbSArd Biesheuvel if (!str) 33b000d5cbSArd Biesheuvel return; 34b000d5cbSArd Biesheuvel 352fe5d6deSMimi Zohar if (strncmp(str, "off", 3) == 0) 36e4d7e2dfSBruno Meneguele appraisal_state = 0; 372faa6ef3SDmitry Kasatkin else if (strncmp(str, "log", 3) == 0) 38e4d7e2dfSBruno Meneguele appraisal_state = IMA_APPRAISE_LOG; 392fe5d6deSMimi Zohar else if (strncmp(str, "fix", 3) == 0) 40e4d7e2dfSBruno Meneguele appraisal_state = IMA_APPRAISE_FIX; 414afb28abSBruno Meneguele else if (strncmp(str, "enforce", 7) == 0) 42e4d7e2dfSBruno Meneguele appraisal_state = IMA_APPRAISE_ENFORCE; 437fe2bb7eSBruno Meneguele else 447fe2bb7eSBruno Meneguele pr_err("invalid \"%s\" appraise option", str); 45e4d7e2dfSBruno Meneguele 46e4d7e2dfSBruno Meneguele /* If appraisal state was changed, but secure boot is enabled, 47e4d7e2dfSBruno Meneguele * keep its default */ 48e4d7e2dfSBruno Meneguele if (sb_state) { 49e4d7e2dfSBruno Meneguele if (!(appraisal_state & IMA_APPRAISE_ENFORCE)) 50e4d7e2dfSBruno Meneguele pr_info("Secure boot enabled: ignoring ima_appraise=%s option", 51e4d7e2dfSBruno Meneguele str); 52e4d7e2dfSBruno Meneguele } else { 53e4d7e2dfSBruno Meneguele ima_appraise = appraisal_state; 54e4d7e2dfSBruno Meneguele } 552fe5d6deSMimi Zohar } 56b000d5cbSArd Biesheuvel #endif 572fe5d6deSMimi Zohar 582fe5d6deSMimi Zohar /* 596f6723e2SMimi Zohar * is_ima_appraise_enabled - return appraise status 606f6723e2SMimi Zohar * 616f6723e2SMimi Zohar * Only return enabled, if not in ima_appraise="fix" or "log" modes. 626f6723e2SMimi Zohar */ 636f6723e2SMimi Zohar bool is_ima_appraise_enabled(void) 646f6723e2SMimi Zohar { 65e5729f86SThiago Jung Bauermann return ima_appraise & IMA_APPRAISE_ENFORCE; 666f6723e2SMimi Zohar } 676f6723e2SMimi Zohar 686f6723e2SMimi Zohar /* 692fe5d6deSMimi Zohar * ima_must_appraise - set appraise flag 702fe5d6deSMimi Zohar * 71da1b0029SMimi Zohar * Return 1 to appraise or hash 722fe5d6deSMimi Zohar */ 73a2d2329eSChristian Brauner int ima_must_appraise(struct user_namespace *mnt_userns, struct inode *inode, 74a2d2329eSChristian Brauner int mask, enum ima_hooks func) 752fe5d6deSMimi Zohar { 76d906c10dSMatthew Garrett u32 secid; 77d906c10dSMatthew Garrett 7807f6a794SMimi Zohar if (!ima_appraise) 792fe5d6deSMimi Zohar return 0; 8007f6a794SMimi Zohar 816326948fSPaul Moore security_current_getsecid_subj(&secid); 821624dc00STHOBY Simon return ima_match_policy(mnt_userns, inode, current_cred(), secid, 831624dc00STHOBY Simon func, mask, IMA_APPRAISE | IMA_HASH, NULL, 841624dc00STHOBY Simon NULL, NULL, NULL); 852fe5d6deSMimi Zohar } 862fe5d6deSMimi Zohar 87def3e8b9SDmitry Kasatkin static int ima_fix_xattr(struct dentry *dentry, 882fe5d6deSMimi Zohar struct integrity_iint_cache *iint) 892fe5d6deSMimi Zohar { 903ea7a560SDmitry Kasatkin int rc, offset; 913ea7a560SDmitry Kasatkin u8 algo = iint->ima_hash->algo; 923ea7a560SDmitry Kasatkin 933ea7a560SDmitry Kasatkin if (algo <= HASH_ALGO_SHA1) { 943ea7a560SDmitry Kasatkin offset = 1; 953ea7a560SDmitry Kasatkin iint->ima_hash->xattr.sha1.type = IMA_XATTR_DIGEST; 963ea7a560SDmitry Kasatkin } else { 973ea7a560SDmitry Kasatkin offset = 0; 983ea7a560SDmitry Kasatkin iint->ima_hash->xattr.ng.type = IMA_XATTR_DIGEST_NG; 993ea7a560SDmitry Kasatkin iint->ima_hash->xattr.ng.algo = algo; 1003ea7a560SDmitry Kasatkin } 101c7c7a1a1STycho Andersen rc = __vfs_setxattr_noperm(&init_user_ns, dentry, XATTR_NAME_IMA, 1023ea7a560SDmitry Kasatkin &iint->ima_hash->xattr.data[offset], 1033ea7a560SDmitry Kasatkin (sizeof(iint->ima_hash->xattr) - offset) + 1043ea7a560SDmitry Kasatkin iint->ima_hash->length, 0); 1053ea7a560SDmitry Kasatkin return rc; 1062fe5d6deSMimi Zohar } 1072fe5d6deSMimi Zohar 108d79d72e0SMimi Zohar /* Return specific func appraised cached result */ 109d79d72e0SMimi Zohar enum integrity_status ima_get_cache_status(struct integrity_iint_cache *iint, 1104ad87a3dSMimi Zohar enum ima_hooks func) 111d79d72e0SMimi Zohar { 112d79d72e0SMimi Zohar switch (func) { 113d79d72e0SMimi Zohar case MMAP_CHECK: 114d79d72e0SMimi Zohar return iint->ima_mmap_status; 115d79d72e0SMimi Zohar case BPRM_CHECK: 116d79d72e0SMimi Zohar return iint->ima_bprm_status; 117d906c10dSMatthew Garrett case CREDS_CHECK: 118d906c10dSMatthew Garrett return iint->ima_creds_status; 119d79d72e0SMimi Zohar case FILE_CHECK: 120c6af8efeSMimi Zohar case POST_SETATTR: 121d79d72e0SMimi Zohar return iint->ima_file_status; 122c6af8efeSMimi Zohar case MODULE_CHECK ... MAX_CHECK - 1: 123c6af8efeSMimi Zohar default: 124c6af8efeSMimi Zohar return iint->ima_read_status; 125d79d72e0SMimi Zohar } 126d79d72e0SMimi Zohar } 127d79d72e0SMimi Zohar 128d79d72e0SMimi Zohar static void ima_set_cache_status(struct integrity_iint_cache *iint, 1294ad87a3dSMimi Zohar enum ima_hooks func, 1304ad87a3dSMimi Zohar enum integrity_status status) 131d79d72e0SMimi Zohar { 132d79d72e0SMimi Zohar switch (func) { 133d79d72e0SMimi Zohar case MMAP_CHECK: 134d79d72e0SMimi Zohar iint->ima_mmap_status = status; 135d79d72e0SMimi Zohar break; 136d79d72e0SMimi Zohar case BPRM_CHECK: 137d79d72e0SMimi Zohar iint->ima_bprm_status = status; 138d79d72e0SMimi Zohar break; 139d906c10dSMatthew Garrett case CREDS_CHECK: 140d906c10dSMatthew Garrett iint->ima_creds_status = status; 14109186e50SGustavo A. R. Silva break; 142d79d72e0SMimi Zohar case FILE_CHECK: 143c6af8efeSMimi Zohar case POST_SETATTR: 144d79d72e0SMimi Zohar iint->ima_file_status = status; 145c6af8efeSMimi Zohar break; 146c6af8efeSMimi Zohar case MODULE_CHECK ... MAX_CHECK - 1: 147c6af8efeSMimi Zohar default: 148c6af8efeSMimi Zohar iint->ima_read_status = status; 149c6af8efeSMimi Zohar break; 150d79d72e0SMimi Zohar } 151d79d72e0SMimi Zohar } 152d79d72e0SMimi Zohar 1534ad87a3dSMimi Zohar static void ima_cache_flags(struct integrity_iint_cache *iint, 1544ad87a3dSMimi Zohar enum ima_hooks func) 155d79d72e0SMimi Zohar { 156d79d72e0SMimi Zohar switch (func) { 157d79d72e0SMimi Zohar case MMAP_CHECK: 158d79d72e0SMimi Zohar iint->flags |= (IMA_MMAP_APPRAISED | IMA_APPRAISED); 159d79d72e0SMimi Zohar break; 160d79d72e0SMimi Zohar case BPRM_CHECK: 161d79d72e0SMimi Zohar iint->flags |= (IMA_BPRM_APPRAISED | IMA_APPRAISED); 162d79d72e0SMimi Zohar break; 163d906c10dSMatthew Garrett case CREDS_CHECK: 164d906c10dSMatthew Garrett iint->flags |= (IMA_CREDS_APPRAISED | IMA_APPRAISED); 165d906c10dSMatthew Garrett break; 166d79d72e0SMimi Zohar case FILE_CHECK: 167c6af8efeSMimi Zohar case POST_SETATTR: 168d79d72e0SMimi Zohar iint->flags |= (IMA_FILE_APPRAISED | IMA_APPRAISED); 169c6af8efeSMimi Zohar break; 170c6af8efeSMimi Zohar case MODULE_CHECK ... MAX_CHECK - 1: 171c6af8efeSMimi Zohar default: 172c6af8efeSMimi Zohar iint->flags |= (IMA_READ_APPRAISED | IMA_APPRAISED); 173c6af8efeSMimi Zohar break; 174d79d72e0SMimi Zohar } 175d79d72e0SMimi Zohar } 176d79d72e0SMimi Zohar 17750f742ddSTHOBY Simon enum hash_algo ima_get_hash_algo(const struct evm_ima_xattr_data *xattr_value, 1781525b06dSDmitry Kasatkin int xattr_len) 179d3634d0fSDmitry Kasatkin { 180d3634d0fSDmitry Kasatkin struct signature_v2_hdr *sig; 181b4bfec7fSSeth Forshee enum hash_algo ret; 182d3634d0fSDmitry Kasatkin 1833ea7a560SDmitry Kasatkin if (!xattr_value || xattr_len < 2) 1841525b06dSDmitry Kasatkin /* return default hash algo */ 1851525b06dSDmitry Kasatkin return ima_hash_algo; 186d3634d0fSDmitry Kasatkin 1873ea7a560SDmitry Kasatkin switch (xattr_value->type) { 188398c42e2SMimi Zohar case IMA_VERITY_DIGSIG: 189398c42e2SMimi Zohar sig = (typeof(sig))xattr_value; 190398c42e2SMimi Zohar if (sig->version != 3 || xattr_len <= sizeof(*sig) || 191398c42e2SMimi Zohar sig->hash_algo >= HASH_ALGO__LAST) 192398c42e2SMimi Zohar return ima_hash_algo; 193398c42e2SMimi Zohar return sig->hash_algo; 1943ea7a560SDmitry Kasatkin case EVM_IMA_XATTR_DIGSIG: 1953ea7a560SDmitry Kasatkin sig = (typeof(sig))xattr_value; 196cb181da1STHOBY Simon if (sig->version != 2 || xattr_len <= sizeof(*sig) 197cb181da1STHOBY Simon || sig->hash_algo >= HASH_ALGO__LAST) 1981525b06dSDmitry Kasatkin return ima_hash_algo; 1991525b06dSDmitry Kasatkin return sig->hash_algo; 2003ea7a560SDmitry Kasatkin case IMA_XATTR_DIGEST_NG: 201650b29dbSThiago Jung Bauermann /* first byte contains algorithm id */ 202650b29dbSThiago Jung Bauermann ret = xattr_value->data[0]; 203b4bfec7fSSeth Forshee if (ret < HASH_ALGO__LAST) 204b4bfec7fSSeth Forshee return ret; 2053ea7a560SDmitry Kasatkin break; 2063ea7a560SDmitry Kasatkin case IMA_XATTR_DIGEST: 2073ea7a560SDmitry Kasatkin /* this is for backward compatibility */ 2083ea7a560SDmitry Kasatkin if (xattr_len == 21) { 2093ea7a560SDmitry Kasatkin unsigned int zero = 0; 210650b29dbSThiago Jung Bauermann if (!memcmp(&xattr_value->data[16], &zero, 4)) 2111525b06dSDmitry Kasatkin return HASH_ALGO_MD5; 2123ea7a560SDmitry Kasatkin else 2131525b06dSDmitry Kasatkin return HASH_ALGO_SHA1; 2143ea7a560SDmitry Kasatkin } else if (xattr_len == 17) 2151525b06dSDmitry Kasatkin return HASH_ALGO_MD5; 2163ea7a560SDmitry Kasatkin break; 2173ea7a560SDmitry Kasatkin } 2181525b06dSDmitry Kasatkin 2191525b06dSDmitry Kasatkin /* return default hash algo */ 2201525b06dSDmitry Kasatkin return ima_hash_algo; 221d3634d0fSDmitry Kasatkin } 222d3634d0fSDmitry Kasatkin 223d3634d0fSDmitry Kasatkin int ima_read_xattr(struct dentry *dentry, 224*f6fbd8cbSPaul Moore struct evm_ima_xattr_data **xattr_value, int xattr_len) 225d3634d0fSDmitry Kasatkin { 226*f6fbd8cbSPaul Moore int ret; 227d3634d0fSDmitry Kasatkin 228c7c7a1a1STycho Andersen ret = vfs_getxattr_alloc(&init_user_ns, dentry, XATTR_NAME_IMA, 229*f6fbd8cbSPaul Moore (char **)xattr_value, xattr_len, GFP_NOFS); 2305d6c3191SAndreas Gruenbacher if (ret == -EOPNOTSUPP) 2315d6c3191SAndreas Gruenbacher ret = 0; 2325d6c3191SAndreas Gruenbacher return ret; 233d3634d0fSDmitry Kasatkin } 234d3634d0fSDmitry Kasatkin 2352fe5d6deSMimi Zohar /* 236398c42e2SMimi Zohar * calc_file_id_hash - calculate the hash of the ima_file_id struct data 237398c42e2SMimi Zohar * @type: xattr type [enum evm_ima_xattr_type] 238398c42e2SMimi Zohar * @algo: hash algorithm [enum hash_algo] 239398c42e2SMimi Zohar * @digest: pointer to the digest to be hashed 240398c42e2SMimi Zohar * @hash: (out) pointer to the hash 241398c42e2SMimi Zohar * 242398c42e2SMimi Zohar * IMA signature version 3 disambiguates the data that is signed by 243398c42e2SMimi Zohar * indirectly signing the hash of the ima_file_id structure data. 244398c42e2SMimi Zohar * 245398c42e2SMimi Zohar * Signing the ima_file_id struct is currently only supported for 246398c42e2SMimi Zohar * IMA_VERITY_DIGSIG type xattrs. 247398c42e2SMimi Zohar * 248398c42e2SMimi Zohar * Return 0 on success, error code otherwise. 249398c42e2SMimi Zohar */ 250398c42e2SMimi Zohar static int calc_file_id_hash(enum evm_ima_xattr_type type, 251398c42e2SMimi Zohar enum hash_algo algo, const u8 *digest, 252398c42e2SMimi Zohar struct ima_digest_data *hash) 253398c42e2SMimi Zohar { 254398c42e2SMimi Zohar struct ima_file_id file_id = { 255398c42e2SMimi Zohar .hash_type = IMA_VERITY_DIGSIG, .hash_algorithm = algo}; 256398c42e2SMimi Zohar unsigned int unused = HASH_MAX_DIGESTSIZE - hash_digest_size[algo]; 257398c42e2SMimi Zohar 258398c42e2SMimi Zohar if (type != IMA_VERITY_DIGSIG) 259398c42e2SMimi Zohar return -EINVAL; 260398c42e2SMimi Zohar 261398c42e2SMimi Zohar memcpy(file_id.hash, digest, hash_digest_size[algo]); 262398c42e2SMimi Zohar 263398c42e2SMimi Zohar hash->algo = algo; 264398c42e2SMimi Zohar hash->length = hash_digest_size[algo]; 265398c42e2SMimi Zohar 266398c42e2SMimi Zohar return ima_calc_buffer_hash(&file_id, sizeof(file_id) - unused, hash); 267398c42e2SMimi Zohar } 268398c42e2SMimi Zohar 269398c42e2SMimi Zohar /* 270a5fbeb61SThiago Jung Bauermann * xattr_verify - verify xattr digest or signature 271a5fbeb61SThiago Jung Bauermann * 272a5fbeb61SThiago Jung Bauermann * Verify whether the hash or signature matches the file contents. 273a5fbeb61SThiago Jung Bauermann * 274a5fbeb61SThiago Jung Bauermann * Return 0 on success, error code otherwise. 275a5fbeb61SThiago Jung Bauermann */ 276a5fbeb61SThiago Jung Bauermann static int xattr_verify(enum ima_hooks func, struct integrity_iint_cache *iint, 277a5fbeb61SThiago Jung Bauermann struct evm_ima_xattr_data *xattr_value, int xattr_len, 278a5fbeb61SThiago Jung Bauermann enum integrity_status *status, const char **cause) 279a5fbeb61SThiago Jung Bauermann { 280398c42e2SMimi Zohar struct ima_max_digest_data hash; 281398c42e2SMimi Zohar struct signature_v2_hdr *sig; 282a5fbeb61SThiago Jung Bauermann int rc = -EINVAL, hash_start = 0; 283398c42e2SMimi Zohar int mask; 284a5fbeb61SThiago Jung Bauermann 285a5fbeb61SThiago Jung Bauermann switch (xattr_value->type) { 286a5fbeb61SThiago Jung Bauermann case IMA_XATTR_DIGEST_NG: 287a5fbeb61SThiago Jung Bauermann /* first byte contains algorithm id */ 288a5fbeb61SThiago Jung Bauermann hash_start = 1; 289df561f66SGustavo A. R. Silva fallthrough; 290a5fbeb61SThiago Jung Bauermann case IMA_XATTR_DIGEST: 2917aa5783dSRoberto Sassu if (*status != INTEGRITY_PASS_IMMUTABLE) { 292a5fbeb61SThiago Jung Bauermann if (iint->flags & IMA_DIGSIG_REQUIRED) { 293398c42e2SMimi Zohar if (iint->flags & IMA_VERITY_REQUIRED) 294398c42e2SMimi Zohar *cause = "verity-signature-required"; 295398c42e2SMimi Zohar else 296a5fbeb61SThiago Jung Bauermann *cause = "IMA-signature-required"; 297a5fbeb61SThiago Jung Bauermann *status = INTEGRITY_FAIL; 298a5fbeb61SThiago Jung Bauermann break; 299a5fbeb61SThiago Jung Bauermann } 300a5fbeb61SThiago Jung Bauermann clear_bit(IMA_DIGSIG, &iint->atomic_flags); 3017aa5783dSRoberto Sassu } else { 3027aa5783dSRoberto Sassu set_bit(IMA_DIGSIG, &iint->atomic_flags); 3037aa5783dSRoberto Sassu } 304a5fbeb61SThiago Jung Bauermann if (xattr_len - sizeof(xattr_value->type) - hash_start >= 305a5fbeb61SThiago Jung Bauermann iint->ima_hash->length) 306a5fbeb61SThiago Jung Bauermann /* 307a5fbeb61SThiago Jung Bauermann * xattr length may be longer. md5 hash in previous 308a5fbeb61SThiago Jung Bauermann * version occupied 20 bytes in xattr, instead of 16 309a5fbeb61SThiago Jung Bauermann */ 310a5fbeb61SThiago Jung Bauermann rc = memcmp(&xattr_value->data[hash_start], 311a5fbeb61SThiago Jung Bauermann iint->ima_hash->digest, 312a5fbeb61SThiago Jung Bauermann iint->ima_hash->length); 313a5fbeb61SThiago Jung Bauermann else 314a5fbeb61SThiago Jung Bauermann rc = -EINVAL; 315a5fbeb61SThiago Jung Bauermann if (rc) { 316a5fbeb61SThiago Jung Bauermann *cause = "invalid-hash"; 317a5fbeb61SThiago Jung Bauermann *status = INTEGRITY_FAIL; 318a5fbeb61SThiago Jung Bauermann break; 319a5fbeb61SThiago Jung Bauermann } 320a5fbeb61SThiago Jung Bauermann *status = INTEGRITY_PASS; 321a5fbeb61SThiago Jung Bauermann break; 322a5fbeb61SThiago Jung Bauermann case EVM_IMA_XATTR_DIGSIG: 323a5fbeb61SThiago Jung Bauermann set_bit(IMA_DIGSIG, &iint->atomic_flags); 324398c42e2SMimi Zohar 325398c42e2SMimi Zohar mask = IMA_DIGSIG_REQUIRED | IMA_VERITY_REQUIRED; 326398c42e2SMimi Zohar if ((iint->flags & mask) == mask) { 327398c42e2SMimi Zohar *cause = "verity-signature-required"; 328398c42e2SMimi Zohar *status = INTEGRITY_FAIL; 329398c42e2SMimi Zohar break; 330398c42e2SMimi Zohar } 331398c42e2SMimi Zohar 332398c42e2SMimi Zohar sig = (typeof(sig))xattr_value; 333398c42e2SMimi Zohar if (sig->version >= 3) { 334398c42e2SMimi Zohar *cause = "invalid-signature-version"; 335398c42e2SMimi Zohar *status = INTEGRITY_FAIL; 336398c42e2SMimi Zohar break; 337398c42e2SMimi Zohar } 338a5fbeb61SThiago Jung Bauermann rc = integrity_digsig_verify(INTEGRITY_KEYRING_IMA, 339a5fbeb61SThiago Jung Bauermann (const char *)xattr_value, 340a5fbeb61SThiago Jung Bauermann xattr_len, 341a5fbeb61SThiago Jung Bauermann iint->ima_hash->digest, 342a5fbeb61SThiago Jung Bauermann iint->ima_hash->length); 343a5fbeb61SThiago Jung Bauermann if (rc == -EOPNOTSUPP) { 344a5fbeb61SThiago Jung Bauermann *status = INTEGRITY_UNKNOWN; 345a5fbeb61SThiago Jung Bauermann break; 346a5fbeb61SThiago Jung Bauermann } 347a5fbeb61SThiago Jung Bauermann if (IS_ENABLED(CONFIG_INTEGRITY_PLATFORM_KEYRING) && rc && 348a5fbeb61SThiago Jung Bauermann func == KEXEC_KERNEL_CHECK) 349a5fbeb61SThiago Jung Bauermann rc = integrity_digsig_verify(INTEGRITY_KEYRING_PLATFORM, 350a5fbeb61SThiago Jung Bauermann (const char *)xattr_value, 351a5fbeb61SThiago Jung Bauermann xattr_len, 352a5fbeb61SThiago Jung Bauermann iint->ima_hash->digest, 353a5fbeb61SThiago Jung Bauermann iint->ima_hash->length); 354a5fbeb61SThiago Jung Bauermann if (rc) { 355a5fbeb61SThiago Jung Bauermann *cause = "invalid-signature"; 356a5fbeb61SThiago Jung Bauermann *status = INTEGRITY_FAIL; 357a5fbeb61SThiago Jung Bauermann } else { 358a5fbeb61SThiago Jung Bauermann *status = INTEGRITY_PASS; 359a5fbeb61SThiago Jung Bauermann } 360a5fbeb61SThiago Jung Bauermann break; 361398c42e2SMimi Zohar case IMA_VERITY_DIGSIG: 362398c42e2SMimi Zohar set_bit(IMA_DIGSIG, &iint->atomic_flags); 363398c42e2SMimi Zohar 364398c42e2SMimi Zohar if (iint->flags & IMA_DIGSIG_REQUIRED) { 365398c42e2SMimi Zohar if (!(iint->flags & IMA_VERITY_REQUIRED)) { 366398c42e2SMimi Zohar *cause = "IMA-signature-required"; 367398c42e2SMimi Zohar *status = INTEGRITY_FAIL; 368398c42e2SMimi Zohar break; 369398c42e2SMimi Zohar } 370398c42e2SMimi Zohar } 371398c42e2SMimi Zohar 372398c42e2SMimi Zohar sig = (typeof(sig))xattr_value; 373398c42e2SMimi Zohar if (sig->version != 3) { 374398c42e2SMimi Zohar *cause = "invalid-signature-version"; 375398c42e2SMimi Zohar *status = INTEGRITY_FAIL; 376398c42e2SMimi Zohar break; 377398c42e2SMimi Zohar } 378398c42e2SMimi Zohar 379398c42e2SMimi Zohar rc = calc_file_id_hash(IMA_VERITY_DIGSIG, iint->ima_hash->algo, 380398c42e2SMimi Zohar iint->ima_hash->digest, &hash.hdr); 381398c42e2SMimi Zohar if (rc) { 382398c42e2SMimi Zohar *cause = "sigv3-hashing-error"; 383398c42e2SMimi Zohar *status = INTEGRITY_FAIL; 384398c42e2SMimi Zohar break; 385398c42e2SMimi Zohar } 386398c42e2SMimi Zohar 387398c42e2SMimi Zohar rc = integrity_digsig_verify(INTEGRITY_KEYRING_IMA, 388398c42e2SMimi Zohar (const char *)xattr_value, 389398c42e2SMimi Zohar xattr_len, hash.digest, 390398c42e2SMimi Zohar hash.hdr.length); 391398c42e2SMimi Zohar if (rc) { 392398c42e2SMimi Zohar *cause = "invalid-verity-signature"; 393398c42e2SMimi Zohar *status = INTEGRITY_FAIL; 394398c42e2SMimi Zohar } else { 395398c42e2SMimi Zohar *status = INTEGRITY_PASS; 396398c42e2SMimi Zohar } 397398c42e2SMimi Zohar 398398c42e2SMimi Zohar break; 399a5fbeb61SThiago Jung Bauermann default: 400a5fbeb61SThiago Jung Bauermann *status = INTEGRITY_UNKNOWN; 401a5fbeb61SThiago Jung Bauermann *cause = "unknown-ima-data"; 402a5fbeb61SThiago Jung Bauermann break; 403a5fbeb61SThiago Jung Bauermann } 404a5fbeb61SThiago Jung Bauermann 405a5fbeb61SThiago Jung Bauermann return rc; 406a5fbeb61SThiago Jung Bauermann } 407a5fbeb61SThiago Jung Bauermann 408a5fbeb61SThiago Jung Bauermann /* 40939b07096SThiago Jung Bauermann * modsig_verify - verify modsig signature 41039b07096SThiago Jung Bauermann * 41139b07096SThiago Jung Bauermann * Verify whether the signature matches the file contents. 41239b07096SThiago Jung Bauermann * 41339b07096SThiago Jung Bauermann * Return 0 on success, error code otherwise. 41439b07096SThiago Jung Bauermann */ 41539b07096SThiago Jung Bauermann static int modsig_verify(enum ima_hooks func, const struct modsig *modsig, 41639b07096SThiago Jung Bauermann enum integrity_status *status, const char **cause) 41739b07096SThiago Jung Bauermann { 41839b07096SThiago Jung Bauermann int rc; 41939b07096SThiago Jung Bauermann 42039b07096SThiago Jung Bauermann rc = integrity_modsig_verify(INTEGRITY_KEYRING_IMA, modsig); 42139b07096SThiago Jung Bauermann if (IS_ENABLED(CONFIG_INTEGRITY_PLATFORM_KEYRING) && rc && 42239b07096SThiago Jung Bauermann func == KEXEC_KERNEL_CHECK) 42339b07096SThiago Jung Bauermann rc = integrity_modsig_verify(INTEGRITY_KEYRING_PLATFORM, 42439b07096SThiago Jung Bauermann modsig); 42539b07096SThiago Jung Bauermann if (rc) { 42639b07096SThiago Jung Bauermann *cause = "invalid-signature"; 42739b07096SThiago Jung Bauermann *status = INTEGRITY_FAIL; 42839b07096SThiago Jung Bauermann } else { 42939b07096SThiago Jung Bauermann *status = INTEGRITY_PASS; 43039b07096SThiago Jung Bauermann } 43139b07096SThiago Jung Bauermann 43239b07096SThiago Jung Bauermann return rc; 43339b07096SThiago Jung Bauermann } 43439b07096SThiago Jung Bauermann 43539b07096SThiago Jung Bauermann /* 436273df864SNayna Jain * ima_check_blacklist - determine if the binary is blacklisted. 437273df864SNayna Jain * 438273df864SNayna Jain * Add the hash of the blacklisted binary to the measurement list, based 439273df864SNayna Jain * on policy. 440273df864SNayna Jain * 441273df864SNayna Jain * Returns -EPERM if the hash is blacklisted. 442273df864SNayna Jain */ 443273df864SNayna Jain int ima_check_blacklist(struct integrity_iint_cache *iint, 444273df864SNayna Jain const struct modsig *modsig, int pcr) 445273df864SNayna Jain { 446273df864SNayna Jain enum hash_algo hash_algo; 447273df864SNayna Jain const u8 *digest = NULL; 448273df864SNayna Jain u32 digestsize = 0; 449273df864SNayna Jain int rc = 0; 450273df864SNayna Jain 451273df864SNayna Jain if (!(iint->flags & IMA_CHECK_BLACKLIST)) 452273df864SNayna Jain return 0; 453273df864SNayna Jain 454273df864SNayna Jain if (iint->flags & IMA_MODSIG_ALLOWED && modsig) { 455273df864SNayna Jain ima_get_modsig_digest(modsig, &hash_algo, &digest, &digestsize); 456273df864SNayna Jain 457273df864SNayna Jain rc = is_binary_blacklisted(digest, digestsize); 458273df864SNayna Jain if ((rc == -EPERM) && (iint->flags & IMA_MEASURE)) 459a2d2329eSChristian Brauner process_buffer_measurement(&init_user_ns, NULL, digest, digestsize, 460273df864SNayna Jain "blacklisted-hash", NONE, 461ca3c9bdbSRoberto Sassu pcr, NULL, false, NULL, 0); 462273df864SNayna Jain } 463273df864SNayna Jain 464273df864SNayna Jain return rc; 465273df864SNayna Jain } 466273df864SNayna Jain 467273df864SNayna Jain /* 4682fe5d6deSMimi Zohar * ima_appraise_measurement - appraise file measurement 4692fe5d6deSMimi Zohar * 4702fe5d6deSMimi Zohar * Call evm_verifyxattr() to verify the integrity of 'security.ima'. 4712fe5d6deSMimi Zohar * Assuming success, compare the xattr hash with the collected measurement. 4722fe5d6deSMimi Zohar * 4732fe5d6deSMimi Zohar * Return 0 on success, error code otherwise 4742fe5d6deSMimi Zohar */ 4754ad87a3dSMimi Zohar int ima_appraise_measurement(enum ima_hooks func, 4764ad87a3dSMimi Zohar struct integrity_iint_cache *iint, 477d3634d0fSDmitry Kasatkin struct file *file, const unsigned char *filename, 478d3634d0fSDmitry Kasatkin struct evm_ima_xattr_data *xattr_value, 47939b07096SThiago Jung Bauermann int xattr_len, const struct modsig *modsig) 4802fe5d6deSMimi Zohar { 48152a13284SMimi Zohar static const char op[] = "appraise_data"; 482f5e51fa3SThiago Jung Bauermann const char *cause = "unknown"; 483e71b9dffSMiklos Szeredi struct dentry *dentry = file_dentry(file); 484c6f493d6SDavid Howells struct inode *inode = d_backing_inode(dentry); 4852fe5d6deSMimi Zohar enum integrity_status status = INTEGRITY_UNKNOWN; 486a5fbeb61SThiago Jung Bauermann int rc = xattr_len; 48739b07096SThiago Jung Bauermann bool try_modsig = iint->flags & IMA_MODSIG_ALLOWED && modsig; 4882fe5d6deSMimi Zohar 48939b07096SThiago Jung Bauermann /* If not appraising a modsig, we need an xattr. */ 49039b07096SThiago Jung Bauermann if (!(inode->i_opflags & IOP_XATTR) && !try_modsig) 4912fe5d6deSMimi Zohar return INTEGRITY_UNKNOWN; 4922fe5d6deSMimi Zohar 49339b07096SThiago Jung Bauermann /* If reading the xattr failed and there's no modsig, error out. */ 49439b07096SThiago Jung Bauermann if (rc <= 0 && !try_modsig) { 4952fe5d6deSMimi Zohar if (rc && rc != -ENODATA) 4962fe5d6deSMimi Zohar goto out; 4972fe5d6deSMimi Zohar 498398c42e2SMimi Zohar if (iint->flags & IMA_DIGSIG_REQUIRED) { 499398c42e2SMimi Zohar if (iint->flags & IMA_VERITY_REQUIRED) 500398c42e2SMimi Zohar cause = "verity-signature-required"; 501398c42e2SMimi Zohar else 502398c42e2SMimi Zohar cause = "IMA-signature-required"; 503398c42e2SMimi Zohar } else { 504398c42e2SMimi Zohar cause = "missing-hash"; 505398c42e2SMimi Zohar } 506398c42e2SMimi Zohar 507b151d6b0SDmitry Kasatkin status = INTEGRITY_NOLABEL; 5086035a27bSAl Viro if (file->f_mode & FMODE_CREATED) 509b151d6b0SDmitry Kasatkin iint->flags |= IMA_NEW_FILE; 5101ac202e9SDaniel Glöckner if ((iint->flags & IMA_NEW_FILE) && 511b7e27bc1SMimi Zohar (!(iint->flags & IMA_DIGSIG_REQUIRED) || 512b7e27bc1SMimi Zohar (inode->i_size == 0))) 513b151d6b0SDmitry Kasatkin status = INTEGRITY_PASS; 5142fe5d6deSMimi Zohar goto out; 5152fe5d6deSMimi Zohar } 5162fe5d6deSMimi Zohar 517d2ee2cfcSHuaxin Lu status = evm_verifyxattr(dentry, XATTR_NAME_IMA, xattr_value, 518d2ee2cfcSHuaxin Lu rc < 0 ? 0 : rc, iint); 519f5e51fa3SThiago Jung Bauermann switch (status) { 520f5e51fa3SThiago Jung Bauermann case INTEGRITY_PASS: 521f5e51fa3SThiago Jung Bauermann case INTEGRITY_PASS_IMMUTABLE: 522f5e51fa3SThiago Jung Bauermann case INTEGRITY_UNKNOWN: 523f5e51fa3SThiago Jung Bauermann break; 524f5e51fa3SThiago Jung Bauermann case INTEGRITY_NOXATTRS: /* No EVM protected xattrs. */ 52539b07096SThiago Jung Bauermann /* It's fine not to have xattrs when using a modsig. */ 52639b07096SThiago Jung Bauermann if (try_modsig) 52739b07096SThiago Jung Bauermann break; 528df561f66SGustavo A. R. Silva fallthrough; 529f5e51fa3SThiago Jung Bauermann case INTEGRITY_NOLABEL: /* No security.evm xattr. */ 5302fe5d6deSMimi Zohar cause = "missing-HMAC"; 531f5e51fa3SThiago Jung Bauermann goto out; 532cdef685bSRoberto Sassu case INTEGRITY_FAIL_IMMUTABLE: 5337aa5783dSRoberto Sassu set_bit(IMA_DIGSIG, &iint->atomic_flags); 53455748ac6SMimi Zohar cause = "invalid-fail-immutable"; 53555748ac6SMimi Zohar goto out; 536f5e51fa3SThiago Jung Bauermann case INTEGRITY_FAIL: /* Invalid HMAC/signature. */ 5372fe5d6deSMimi Zohar cause = "invalid-HMAC"; 5382fe5d6deSMimi Zohar goto out; 539f5e51fa3SThiago Jung Bauermann default: 540f5e51fa3SThiago Jung Bauermann WARN_ONCE(true, "Unexpected integrity status %d\n", status); 5412fe5d6deSMimi Zohar } 542f5e51fa3SThiago Jung Bauermann 543a5fbeb61SThiago Jung Bauermann if (xattr_value) 544a5fbeb61SThiago Jung Bauermann rc = xattr_verify(func, iint, xattr_value, xattr_len, &status, 545a5fbeb61SThiago Jung Bauermann &cause); 5468606404fSDmitry Kasatkin 54739b07096SThiago Jung Bauermann /* 54839b07096SThiago Jung Bauermann * If we have a modsig and either no imasig or the imasig's key isn't 54939b07096SThiago Jung Bauermann * known, then try verifying the modsig. 55039b07096SThiago Jung Bauermann */ 55139b07096SThiago Jung Bauermann if (try_modsig && 55239b07096SThiago Jung Bauermann (!xattr_value || xattr_value->type == IMA_XATTR_DIGEST_NG || 55339b07096SThiago Jung Bauermann rc == -ENOKEY)) 55439b07096SThiago Jung Bauermann rc = modsig_verify(func, modsig, &status, &cause); 55539b07096SThiago Jung Bauermann 5562fe5d6deSMimi Zohar out: 55757b56ac6SMimi Zohar /* 55857b56ac6SMimi Zohar * File signatures on some filesystems can not be properly verified. 5599e67028eSMimi Zohar * When such filesystems are mounted by an untrusted mounter or on a 5609e67028eSMimi Zohar * system not willing to accept such a risk, fail the file signature 5619e67028eSMimi Zohar * verification. 56257b56ac6SMimi Zohar */ 5639e67028eSMimi Zohar if ((inode->i_sb->s_iflags & SB_I_IMA_UNVERIFIABLE_SIGNATURE) && 5649e67028eSMimi Zohar ((inode->i_sb->s_iflags & SB_I_UNTRUSTED_MOUNTER) || 5659e67028eSMimi Zohar (iint->flags & IMA_FAIL_UNVERIFIABLE_SIGS))) { 56657b56ac6SMimi Zohar status = INTEGRITY_FAIL; 56757b56ac6SMimi Zohar cause = "unverifiable-signature"; 56857b56ac6SMimi Zohar integrity_audit_msg(AUDIT_INTEGRITY_DATA, inode, filename, 56957b56ac6SMimi Zohar op, cause, rc, 0); 57057b56ac6SMimi Zohar } else if (status != INTEGRITY_PASS) { 571f5e51fa3SThiago Jung Bauermann /* Fix mode, but don't replace file signatures. */ 57239b07096SThiago Jung Bauermann if ((ima_appraise & IMA_APPRAISE_FIX) && !try_modsig && 5738606404fSDmitry Kasatkin (!xattr_value || 5748606404fSDmitry Kasatkin xattr_value->type != EVM_IMA_XATTR_DIGSIG)) { 575def3e8b9SDmitry Kasatkin if (!ima_fix_xattr(dentry, iint)) 5762fe5d6deSMimi Zohar status = INTEGRITY_PASS; 577f5e51fa3SThiago Jung Bauermann } 578f5e51fa3SThiago Jung Bauermann 5797aa5783dSRoberto Sassu /* 5807aa5783dSRoberto Sassu * Permit new files with file/EVM portable signatures, but 5817aa5783dSRoberto Sassu * without data. 5827aa5783dSRoberto Sassu */ 583f5e51fa3SThiago Jung Bauermann if (inode->i_size == 0 && iint->flags & IMA_NEW_FILE && 5847aa5783dSRoberto Sassu test_bit(IMA_DIGSIG, &iint->atomic_flags)) { 58505d1a717SMimi Zohar status = INTEGRITY_PASS; 5862fe5d6deSMimi Zohar } 587f5e51fa3SThiago Jung Bauermann 5882fe5d6deSMimi Zohar integrity_audit_msg(AUDIT_INTEGRITY_DATA, inode, filename, 5892fe5d6deSMimi Zohar op, cause, rc, 0); 5908606404fSDmitry Kasatkin } else { 591d79d72e0SMimi Zohar ima_cache_flags(iint, func); 5922fe5d6deSMimi Zohar } 59357b56ac6SMimi Zohar 594d79d72e0SMimi Zohar ima_set_cache_status(iint, func, status); 5952fe5d6deSMimi Zohar return status; 5962fe5d6deSMimi Zohar } 5972fe5d6deSMimi Zohar 5982fe5d6deSMimi Zohar /* 5992fe5d6deSMimi Zohar * ima_update_xattr - update 'security.ima' hash value 6002fe5d6deSMimi Zohar */ 6012fe5d6deSMimi Zohar void ima_update_xattr(struct integrity_iint_cache *iint, struct file *file) 6022fe5d6deSMimi Zohar { 603e71b9dffSMiklos Szeredi struct dentry *dentry = file_dentry(file); 6042fe5d6deSMimi Zohar int rc = 0; 6052fe5d6deSMimi Zohar 6068606404fSDmitry Kasatkin /* do not collect and update hash for digital signatures */ 6070d73a552SDmitry Kasatkin if (test_bit(IMA_DIGSIG, &iint->atomic_flags)) 6088606404fSDmitry Kasatkin return; 6098606404fSDmitry Kasatkin 610da1b0029SMimi Zohar if ((iint->ima_file_status != INTEGRITY_PASS) && 611da1b0029SMimi Zohar !(iint->flags & IMA_HASH)) 612020aae3eSRoberto Sassu return; 613020aae3eSRoberto Sassu 61415588227SThiago Jung Bauermann rc = ima_collect_measurement(iint, file, NULL, 0, ima_hash_algo, NULL); 6152fe5d6deSMimi Zohar if (rc < 0) 6162fe5d6deSMimi Zohar return; 6178606404fSDmitry Kasatkin 6180d73a552SDmitry Kasatkin inode_lock(file_inode(file)); 6192fe5d6deSMimi Zohar ima_fix_xattr(dentry, iint); 6200d73a552SDmitry Kasatkin inode_unlock(file_inode(file)); 6212fe5d6deSMimi Zohar } 6222fe5d6deSMimi Zohar 6232fe5d6deSMimi Zohar /** 6242fe5d6deSMimi Zohar * ima_inode_post_setattr - reflect file metadata changes 625a2d2329eSChristian Brauner * @mnt_userns: user namespace of the mount the inode was found from 6262fe5d6deSMimi Zohar * @dentry: pointer to the affected dentry 6272fe5d6deSMimi Zohar * 6282fe5d6deSMimi Zohar * Changes to a dentry's metadata might result in needing to appraise. 6292fe5d6deSMimi Zohar * 6302fe5d6deSMimi Zohar * This function is called from notify_change(), which expects the caller 6312fe5d6deSMimi Zohar * to lock the inode's i_mutex. 6322fe5d6deSMimi Zohar */ 633a2d2329eSChristian Brauner void ima_inode_post_setattr(struct user_namespace *mnt_userns, 634a2d2329eSChristian Brauner struct dentry *dentry) 6352fe5d6deSMimi Zohar { 636c6f493d6SDavid Howells struct inode *inode = d_backing_inode(dentry); 6372fe5d6deSMimi Zohar struct integrity_iint_cache *iint; 638da1b0029SMimi Zohar int action; 6392fe5d6deSMimi Zohar 640a756024eSRoberto Sassu if (!(ima_policy_flag & IMA_APPRAISE) || !S_ISREG(inode->i_mode) 6415d6c3191SAndreas Gruenbacher || !(inode->i_opflags & IOP_XATTR)) 6422fe5d6deSMimi Zohar return; 6432fe5d6deSMimi Zohar 644a2d2329eSChristian Brauner action = ima_must_appraise(mnt_userns, inode, MAY_ACCESS, POST_SETATTR); 6450d73a552SDmitry Kasatkin iint = integrity_iint_find(inode); 6460d73a552SDmitry Kasatkin if (iint) { 6470d73a552SDmitry Kasatkin set_bit(IMA_CHANGE_ATTR, &iint->atomic_flags); 648da1b0029SMimi Zohar if (!action) 6490d73a552SDmitry Kasatkin clear_bit(IMA_UPDATE_XATTR, &iint->atomic_flags); 6500d73a552SDmitry Kasatkin } 6512fe5d6deSMimi Zohar } 65242c63330SMimi Zohar 65342c63330SMimi Zohar /* 65442c63330SMimi Zohar * ima_protect_xattr - protect 'security.ima' 65542c63330SMimi Zohar * 65642c63330SMimi Zohar * Ensure that not just anyone can modify or remove 'security.ima'. 65742c63330SMimi Zohar */ 65842c63330SMimi Zohar static int ima_protect_xattr(struct dentry *dentry, const char *xattr_name, 65942c63330SMimi Zohar const void *xattr_value, size_t xattr_value_len) 66042c63330SMimi Zohar { 66142c63330SMimi Zohar if (strcmp(xattr_name, XATTR_NAME_IMA) == 0) { 66242c63330SMimi Zohar if (!capable(CAP_SYS_ADMIN)) 66342c63330SMimi Zohar return -EPERM; 66442c63330SMimi Zohar return 1; 66542c63330SMimi Zohar } 66642c63330SMimi Zohar return 0; 66742c63330SMimi Zohar } 66842c63330SMimi Zohar 669060bdebfSMimi Zohar static void ima_reset_appraise_flags(struct inode *inode, int digsig) 67042c63330SMimi Zohar { 67142c63330SMimi Zohar struct integrity_iint_cache *iint; 67242c63330SMimi Zohar 673a756024eSRoberto Sassu if (!(ima_policy_flag & IMA_APPRAISE) || !S_ISREG(inode->i_mode)) 67442c63330SMimi Zohar return; 67542c63330SMimi Zohar 67642c63330SMimi Zohar iint = integrity_iint_find(inode); 67742c63330SMimi Zohar if (!iint) 67842c63330SMimi Zohar return; 679a422638dSEric Richter iint->measured_pcrs = 0; 6800d73a552SDmitry Kasatkin set_bit(IMA_CHANGE_XATTR, &iint->atomic_flags); 681060bdebfSMimi Zohar if (digsig) 6820d73a552SDmitry Kasatkin set_bit(IMA_DIGSIG, &iint->atomic_flags); 6830d73a552SDmitry Kasatkin else 6840d73a552SDmitry Kasatkin clear_bit(IMA_DIGSIG, &iint->atomic_flags); 68542c63330SMimi Zohar } 68642c63330SMimi Zohar 68750f742ddSTHOBY Simon /** 68850f742ddSTHOBY Simon * validate_hash_algo() - Block setxattr with unsupported hash algorithms 68950f742ddSTHOBY Simon * @dentry: object of the setxattr() 69050f742ddSTHOBY Simon * @xattr_value: userland supplied xattr value 69150f742ddSTHOBY Simon * @xattr_value_len: length of xattr_value 69250f742ddSTHOBY Simon * 69350f742ddSTHOBY Simon * The xattr value is mapped to its hash algorithm, and this algorithm 69450f742ddSTHOBY Simon * must be built in the kernel for the setxattr to be allowed. 69550f742ddSTHOBY Simon * 69650f742ddSTHOBY Simon * Emit an audit message when the algorithm is invalid. 69750f742ddSTHOBY Simon * 69850f742ddSTHOBY Simon * Return: 0 on success, else an error. 69950f742ddSTHOBY Simon */ 70050f742ddSTHOBY Simon static int validate_hash_algo(struct dentry *dentry, 70150f742ddSTHOBY Simon const struct evm_ima_xattr_data *xattr_value, 70250f742ddSTHOBY Simon size_t xattr_value_len) 70350f742ddSTHOBY Simon { 70450f742ddSTHOBY Simon char *path = NULL, *pathbuf = NULL; 70550f742ddSTHOBY Simon enum hash_algo xattr_hash_algo; 7064f2946aaSTHOBY Simon const char *errmsg = "unavailable-hash-algorithm"; 7074f2946aaSTHOBY Simon unsigned int allowed_hashes; 70850f742ddSTHOBY Simon 70950f742ddSTHOBY Simon xattr_hash_algo = ima_get_hash_algo(xattr_value, xattr_value_len); 71050f742ddSTHOBY Simon 7114f2946aaSTHOBY Simon allowed_hashes = atomic_read(&ima_setxattr_allowed_hash_algorithms); 7124f2946aaSTHOBY Simon 7134f2946aaSTHOBY Simon if (allowed_hashes) { 7144f2946aaSTHOBY Simon /* success if the algorithm is allowed in the ima policy */ 7154f2946aaSTHOBY Simon if (allowed_hashes & (1U << xattr_hash_algo)) 71650f742ddSTHOBY Simon return 0; 71750f742ddSTHOBY Simon 7184f2946aaSTHOBY Simon /* 7194f2946aaSTHOBY Simon * We use a different audit message when the hash algorithm 7204f2946aaSTHOBY Simon * is denied by a policy rule, instead of not being built 7214f2946aaSTHOBY Simon * in the kernel image 7224f2946aaSTHOBY Simon */ 7234f2946aaSTHOBY Simon errmsg = "denied-hash-algorithm"; 7244f2946aaSTHOBY Simon } else { 7254f2946aaSTHOBY Simon if (likely(xattr_hash_algo == ima_hash_algo)) 7264f2946aaSTHOBY Simon return 0; 7274f2946aaSTHOBY Simon 7284f2946aaSTHOBY Simon /* allow any xattr using an algorithm built in the kernel */ 7294f2946aaSTHOBY Simon if (crypto_has_alg(hash_algo_name[xattr_hash_algo], 0, 0)) 7304f2946aaSTHOBY Simon return 0; 7314f2946aaSTHOBY Simon } 7324f2946aaSTHOBY Simon 73350f742ddSTHOBY Simon pathbuf = kmalloc(PATH_MAX, GFP_KERNEL); 73450f742ddSTHOBY Simon if (!pathbuf) 73550f742ddSTHOBY Simon return -EACCES; 73650f742ddSTHOBY Simon 73750f742ddSTHOBY Simon path = dentry_path(dentry, pathbuf, PATH_MAX); 73850f742ddSTHOBY Simon 73950f742ddSTHOBY Simon integrity_audit_msg(AUDIT_INTEGRITY_DATA, d_inode(dentry), path, 7404f2946aaSTHOBY Simon "set_data", errmsg, -EACCES, 0); 74150f742ddSTHOBY Simon 74250f742ddSTHOBY Simon kfree(pathbuf); 74350f742ddSTHOBY Simon 74450f742ddSTHOBY Simon return -EACCES; 74550f742ddSTHOBY Simon } 74650f742ddSTHOBY Simon 74742c63330SMimi Zohar int ima_inode_setxattr(struct dentry *dentry, const char *xattr_name, 74842c63330SMimi Zohar const void *xattr_value, size_t xattr_value_len) 74942c63330SMimi Zohar { 750060bdebfSMimi Zohar const struct evm_ima_xattr_data *xvalue = xattr_value; 751e3ccfe1aSRoberto Sassu int digsig = 0; 75242c63330SMimi Zohar int result; 7535926586fSMimi Zohar int err; 75442c63330SMimi Zohar 75542c63330SMimi Zohar result = ima_protect_xattr(dentry, xattr_name, xattr_value, 75642c63330SMimi Zohar xattr_value_len); 75742c63330SMimi Zohar if (result == 1) { 758a48fda9dSDmitry Kasatkin if (!xattr_value_len || (xvalue->type >= IMA_XATTR_LAST)) 759a48fda9dSDmitry Kasatkin return -EINVAL; 7605926586fSMimi Zohar 7615926586fSMimi Zohar err = validate_hash_algo(dentry, xvalue, xattr_value_len); 7625926586fSMimi Zohar if (err) 7635926586fSMimi Zohar return err; 7645926586fSMimi Zohar 765e3ccfe1aSRoberto Sassu digsig = (xvalue->type == EVM_IMA_XATTR_DIGSIG); 7667aa5783dSRoberto Sassu } else if (!strcmp(xattr_name, XATTR_NAME_EVM) && xattr_value_len > 0) { 7677aa5783dSRoberto Sassu digsig = (xvalue->type == EVM_XATTR_PORTABLE_DIGSIG); 768e3ccfe1aSRoberto Sassu } 769e3ccfe1aSRoberto Sassu if (result == 1 || evm_revalidate_status(xattr_name)) { 770e3ccfe1aSRoberto Sassu ima_reset_appraise_flags(d_backing_inode(dentry), digsig); 7715926586fSMimi Zohar if (result == 1) 7725926586fSMimi Zohar result = 0; 77342c63330SMimi Zohar } 77442c63330SMimi Zohar return result; 77542c63330SMimi Zohar } 77642c63330SMimi Zohar 77742c63330SMimi Zohar int ima_inode_removexattr(struct dentry *dentry, const char *xattr_name) 77842c63330SMimi Zohar { 77942c63330SMimi Zohar int result; 78042c63330SMimi Zohar 78142c63330SMimi Zohar result = ima_protect_xattr(dentry, xattr_name, NULL, 0); 782e3ccfe1aSRoberto Sassu if (result == 1 || evm_revalidate_status(xattr_name)) { 783c6f493d6SDavid Howells ima_reset_appraise_flags(d_backing_inode(dentry), 0); 784e3ccfe1aSRoberto Sassu if (result == 1) 78542c63330SMimi Zohar result = 0; 78642c63330SMimi Zohar } 78742c63330SMimi Zohar return result; 78842c63330SMimi Zohar } 789