1 // SPDX-License-Identifier: GPL-2.0-only 2 /* 3 * Copyright (C) 2005,2006,2007,2008 IBM Corporation 4 * 5 * Authors: 6 * Reiner Sailer <sailer@watson.ibm.com> 7 * Leendert van Doorn <leendert@watson.ibm.com> 8 * Mimi Zohar <zohar@us.ibm.com> 9 * 10 * File: ima_init.c 11 * initialization and cleanup functions 12 */ 13 14 #include <linux/init.h> 15 #include <linux/scatterlist.h> 16 #include <linux/slab.h> 17 #include <linux/err.h> 18 19 #include "ima.h" 20 21 /* name for boot aggregate entry */ 22 static const char boot_aggregate_name[] = "boot_aggregate"; 23 struct tpm_chip *ima_tpm_chip; 24 25 /* Add the boot aggregate to the IMA measurement list and extend 26 * the PCR register. 27 * 28 * Calculate the boot aggregate, a SHA1 over tpm registers 0-7, 29 * assuming a TPM chip exists, and zeroes if the TPM chip does not 30 * exist. Add the boot aggregate measurement to the measurement 31 * list and extend the PCR register. 32 * 33 * If a tpm chip does not exist, indicate the core root of trust is 34 * not hardware based by invalidating the aggregate PCR value. 35 * (The aggregate PCR value is invalidated by adding one value to 36 * the measurement list and extending the aggregate PCR value with 37 * a different value.) Violations add a zero entry to the measurement 38 * list and extend the aggregate PCR value with ff...ff's. 39 */ 40 static int __init ima_add_boot_aggregate(void) 41 { 42 static const char op[] = "add_boot_aggregate"; 43 const char *audit_cause = "ENOMEM"; 44 struct ima_template_entry *entry; 45 struct integrity_iint_cache tmp_iint, *iint = &tmp_iint; 46 struct ima_event_data event_data = { .iint = iint, 47 .filename = boot_aggregate_name }; 48 int result = -ENOMEM; 49 int violation = 0; 50 struct { 51 struct ima_digest_data hdr; 52 char digest[TPM_DIGEST_SIZE]; 53 } hash; 54 55 memset(iint, 0, sizeof(*iint)); 56 memset(&hash, 0, sizeof(hash)); 57 iint->ima_hash = &hash.hdr; 58 iint->ima_hash->algo = HASH_ALGO_SHA1; 59 iint->ima_hash->length = SHA1_DIGEST_SIZE; 60 61 if (ima_tpm_chip) { 62 result = ima_calc_boot_aggregate(&hash.hdr); 63 if (result < 0) { 64 audit_cause = "hashing_error"; 65 goto err_out; 66 } 67 } 68 69 result = ima_alloc_init_template(&event_data, &entry, NULL); 70 if (result < 0) { 71 audit_cause = "alloc_entry"; 72 goto err_out; 73 } 74 75 result = ima_store_template(entry, violation, NULL, 76 boot_aggregate_name, 77 CONFIG_IMA_MEASURE_PCR_IDX); 78 if (result < 0) { 79 ima_free_template_entry(entry); 80 audit_cause = "store_entry"; 81 goto err_out; 82 } 83 return 0; 84 err_out: 85 integrity_audit_msg(AUDIT_INTEGRITY_PCR, NULL, boot_aggregate_name, op, 86 audit_cause, result, 0); 87 return result; 88 } 89 90 #ifdef CONFIG_IMA_LOAD_X509 91 void __init ima_load_x509(void) 92 { 93 int unset_flags = ima_policy_flag & IMA_APPRAISE; 94 95 ima_policy_flag &= ~unset_flags; 96 integrity_load_x509(INTEGRITY_KEYRING_IMA, CONFIG_IMA_X509_PATH); 97 ima_policy_flag |= unset_flags; 98 } 99 #endif 100 101 int __init ima_init(void) 102 { 103 int rc; 104 105 ima_tpm_chip = tpm_default_chip(); 106 if (!ima_tpm_chip) 107 pr_info("No TPM chip found, activating TPM-bypass!\n"); 108 109 rc = integrity_init_keyring(INTEGRITY_KEYRING_IMA); 110 if (rc) 111 return rc; 112 113 rc = ima_init_crypto(); 114 if (rc) 115 return rc; 116 rc = ima_init_template(); 117 if (rc != 0) 118 return rc; 119 120 /* It can be called before ima_init_digests(), it does not use TPM. */ 121 ima_load_kexec_buffer(); 122 123 rc = ima_init_digests(); 124 if (rc != 0) 125 return rc; 126 rc = ima_add_boot_aggregate(); /* boot aggregate must be first entry */ 127 if (rc != 0) 128 return rc; 129 130 ima_init_policy(); 131 132 rc = ima_fs_init(); 133 if (rc != 0) 134 return rc; 135 136 ima_init_key_queue(); 137 138 return rc; 139 } 140