Back to home page

OSCL-LXR

 
 

    


0001 /* SPDX-License-Identifier: GPL-2.0 */
0002 
0003 #ifndef __LINUX_TPM_EVENTLOG_H__
0004 #define __LINUX_TPM_EVENTLOG_H__
0005 
0006 #include <linux/tpm.h>
0007 
0008 #define TCG_EVENT_NAME_LEN_MAX  255
0009 #define MAX_TEXT_EVENT      1000    /* Max event string length */
0010 #define ACPI_TCPA_SIG       "TCPA"  /* 0x41504354 /'TCPA' */
0011 
0012 #define EFI_TCG2_EVENT_LOG_FORMAT_TCG_1_2 0x1
0013 #define EFI_TCG2_EVENT_LOG_FORMAT_TCG_2   0x2
0014 
0015 #ifdef CONFIG_PPC64
0016 #define do_endian_conversion(x) be32_to_cpu(x)
0017 #else
0018 #define do_endian_conversion(x) x
0019 #endif
0020 
0021 enum bios_platform_class {
0022     BIOS_CLIENT = 0x00,
0023     BIOS_SERVER = 0x01,
0024 };
0025 
0026 struct tcpa_event {
0027     u32 pcr_index;
0028     u32 event_type;
0029     u8 pcr_value[20];   /* SHA1 */
0030     u32 event_size;
0031     u8 event_data[];
0032 };
0033 
0034 enum tcpa_event_types {
0035     PREBOOT = 0,
0036     POST_CODE,
0037     UNUSED,
0038     NO_ACTION,
0039     SEPARATOR,
0040     ACTION,
0041     EVENT_TAG,
0042     SCRTM_CONTENTS,
0043     SCRTM_VERSION,
0044     CPU_MICROCODE,
0045     PLATFORM_CONFIG_FLAGS,
0046     TABLE_OF_DEVICES,
0047     COMPACT_HASH,
0048     IPL,
0049     IPL_PARTITION_DATA,
0050     NONHOST_CODE,
0051     NONHOST_CONFIG,
0052     NONHOST_INFO,
0053 };
0054 
0055 struct tcpa_pc_event {
0056     u32 event_id;
0057     u32 event_size;
0058     u8 event_data[];
0059 };
0060 
0061 enum tcpa_pc_event_ids {
0062     SMBIOS = 1,
0063     BIS_CERT,
0064     POST_BIOS_ROM,
0065     ESCD,
0066     CMOS,
0067     NVRAM,
0068     OPTION_ROM_EXEC,
0069     OPTION_ROM_CONFIG,
0070     OPTION_ROM_MICROCODE = 10,
0071     S_CRTM_VERSION,
0072     S_CRTM_CONTENTS,
0073     POST_CONTENTS,
0074     HOST_TABLE_OF_DEVICES,
0075 };
0076 
0077 /* http://www.trustedcomputinggroup.org/tcg-efi-protocol-specification/ */
0078 
0079 struct tcg_efi_specid_event_algs {
0080     u16 alg_id;
0081     u16 digest_size;
0082 } __packed;
0083 
0084 #define TCG_SPECID_SIG "Spec ID Event03"
0085 
0086 struct tcg_efi_specid_event_head {
0087     u8 signature[16];
0088     u32 platform_class;
0089     u8 spec_version_minor;
0090     u8 spec_version_major;
0091     u8 spec_errata;
0092     u8 uintnsize;
0093     u32 num_algs;
0094     struct tcg_efi_specid_event_algs digest_sizes[];
0095 } __packed;
0096 
0097 struct tcg_pcr_event {
0098     u32 pcr_idx;
0099     u32 event_type;
0100     u8 digest[20];
0101     u32 event_size;
0102     u8 event[];
0103 } __packed;
0104 
0105 struct tcg_event_field {
0106     u32 event_size;
0107     u8 event[];
0108 } __packed;
0109 
0110 struct tcg_pcr_event2_head {
0111     u32 pcr_idx;
0112     u32 event_type;
0113     u32 count;
0114     struct tpm_digest digests[];
0115 } __packed;
0116 
0117 struct tcg_algorithm_size {
0118     u16 algorithm_id;
0119     u16 algorithm_size;
0120 };
0121 
0122 struct tcg_algorithm_info {
0123     u8 signature[16];
0124     u32 platform_class;
0125     u8 spec_version_minor;
0126     u8 spec_version_major;
0127     u8 spec_errata;
0128     u8 uintn_size;
0129     u32 number_of_algorithms;
0130     struct tcg_algorithm_size digest_sizes[];
0131 };
0132 
0133 #ifndef TPM_MEMREMAP
0134 #define TPM_MEMREMAP(start, size) NULL
0135 #endif
0136 
0137 #ifndef TPM_MEMUNMAP
0138 #define TPM_MEMUNMAP(start, size) do{} while(0)
0139 #endif
0140 
0141 /**
0142  * __calc_tpm2_event_size - calculate the size of a TPM2 event log entry
0143  * @event:        Pointer to the event whose size should be calculated
0144  * @event_header: Pointer to the initial event containing the digest lengths
0145  * @do_mapping:   Whether or not the event needs to be mapped
0146  *
0147  * The TPM2 event log format can contain multiple digests corresponding to
0148  * separate PCR banks, and also contains a variable length of the data that
0149  * was measured. This requires knowledge of how long each digest type is,
0150  * and this information is contained within the first event in the log.
0151  *
0152  * We calculate the length by examining the number of events, and then looking
0153  * at each event in turn to determine how much space is used for events in
0154  * total. Once we've done this we know the offset of the data length field,
0155  * and can calculate the total size of the event.
0156  *
0157  * Return: size of the event on success, 0 on failure
0158  */
0159 
0160 static __always_inline int __calc_tpm2_event_size(struct tcg_pcr_event2_head *event,
0161                      struct tcg_pcr_event *event_header,
0162                      bool do_mapping)
0163 {
0164     struct tcg_efi_specid_event_head *efispecid;
0165     struct tcg_event_field *event_field;
0166     void *mapping = NULL;
0167     int mapping_size;
0168     void *marker;
0169     void *marker_start;
0170     u32 halg_size;
0171     size_t size;
0172     u16 halg;
0173     int i;
0174     int j;
0175     u32 count, event_type;
0176     const u8 zero_digest[sizeof(event_header->digest)] = {0};
0177 
0178     marker = event;
0179     marker_start = marker;
0180     marker = marker + sizeof(event->pcr_idx) + sizeof(event->event_type)
0181         + sizeof(event->count);
0182 
0183     /* Map the event header */
0184     if (do_mapping) {
0185         mapping_size = marker - marker_start;
0186         mapping = TPM_MEMREMAP((unsigned long)marker_start,
0187                        mapping_size);
0188         if (!mapping) {
0189             size = 0;
0190             goto out;
0191         }
0192     } else {
0193         mapping = marker_start;
0194     }
0195 
0196     event = (struct tcg_pcr_event2_head *)mapping;
0197     /*
0198      * The loop below will unmap these fields if the log is larger than
0199      * one page, so save them here for reference:
0200      */
0201     count = READ_ONCE(event->count);
0202     event_type = READ_ONCE(event->event_type);
0203 
0204     /* Verify that it's the log header */
0205     if (event_header->pcr_idx != 0 ||
0206         event_header->event_type != NO_ACTION ||
0207         memcmp(event_header->digest, zero_digest, sizeof(zero_digest))) {
0208         size = 0;
0209         goto out;
0210     }
0211 
0212     efispecid = (struct tcg_efi_specid_event_head *)event_header->event;
0213 
0214     /*
0215      * Perform validation of the event in order to identify malformed
0216      * events. This function may be asked to parse arbitrary byte sequences
0217      * immediately following a valid event log. The caller expects this
0218      * function to recognize that the byte sequence is not a valid event
0219      * and to return an event size of 0.
0220      */
0221     if (memcmp(efispecid->signature, TCG_SPECID_SIG,
0222            sizeof(TCG_SPECID_SIG)) ||
0223         !efispecid->num_algs || count != efispecid->num_algs) {
0224         size = 0;
0225         goto out;
0226     }
0227 
0228     for (i = 0; i < count; i++) {
0229         halg_size = sizeof(event->digests[i].alg_id);
0230 
0231         /* Map the digest's algorithm identifier */
0232         if (do_mapping) {
0233             TPM_MEMUNMAP(mapping, mapping_size);
0234             mapping_size = halg_size;
0235             mapping = TPM_MEMREMAP((unsigned long)marker,
0236                          mapping_size);
0237             if (!mapping) {
0238                 size = 0;
0239                 goto out;
0240             }
0241         } else {
0242             mapping = marker;
0243         }
0244 
0245         memcpy(&halg, mapping, halg_size);
0246         marker = marker + halg_size;
0247 
0248         for (j = 0; j < efispecid->num_algs; j++) {
0249             if (halg == efispecid->digest_sizes[j].alg_id) {
0250                 marker +=
0251                     efispecid->digest_sizes[j].digest_size;
0252                 break;
0253             }
0254         }
0255         /* Algorithm without known length. Such event is unparseable. */
0256         if (j == efispecid->num_algs) {
0257             size = 0;
0258             goto out;
0259         }
0260     }
0261 
0262     /*
0263      * Map the event size - we don't read from the event itself, so
0264      * we don't need to map it
0265      */
0266     if (do_mapping) {
0267         TPM_MEMUNMAP(mapping, mapping_size);
0268         mapping_size += sizeof(event_field->event_size);
0269         mapping = TPM_MEMREMAP((unsigned long)marker,
0270                        mapping_size);
0271         if (!mapping) {
0272             size = 0;
0273             goto out;
0274         }
0275     } else {
0276         mapping = marker;
0277     }
0278 
0279     event_field = (struct tcg_event_field *)mapping;
0280 
0281     marker = marker + sizeof(event_field->event_size)
0282         + event_field->event_size;
0283     size = marker - marker_start;
0284 
0285     if (event_type == 0 && event_field->event_size == 0)
0286         size = 0;
0287 
0288 out:
0289     if (do_mapping)
0290         TPM_MEMUNMAP(mapping, mapping_size);
0291     return size;
0292 }
0293 
0294 #endif