Back to home page

OSCL-LXR

 
 

    


0001 // SPDX-License-Identifier: GPL-2.0-only
0002 /*
0003  * Copyright (C) 2005,2006,2007,2008 IBM Corporation
0004  *
0005  * Authors:
0006  * Serge Hallyn <serue@us.ibm.com>
0007  * Reiner Sailer <sailer@watson.ibm.com>
0008  * Mimi Zohar <zohar@us.ibm.com>
0009  *
0010  * File: ima_queue.c
0011  *       Implements queues that store template measurements and
0012  *       maintains aggregate over the stored measurements
0013  *       in the pre-configured TPM PCR (if available).
0014  *       The measurement list is append-only. No entry is
0015  *       ever removed or changed during the boot-cycle.
0016  */
0017 
0018 #include <linux/rculist.h>
0019 #include <linux/slab.h>
0020 #include "ima.h"
0021 
0022 #define AUDIT_CAUSE_LEN_MAX 32
0023 
0024 /* pre-allocated array of tpm_digest structures to extend a PCR */
0025 static struct tpm_digest *digests;
0026 
0027 LIST_HEAD(ima_measurements);    /* list of all measurements */
0028 #ifdef CONFIG_IMA_KEXEC
0029 static unsigned long binary_runtime_size;
0030 #else
0031 static unsigned long binary_runtime_size = ULONG_MAX;
0032 #endif
0033 
0034 /* key: inode (before secure-hashing a file) */
0035 struct ima_h_table ima_htable = {
0036     .len = ATOMIC_LONG_INIT(0),
0037     .violations = ATOMIC_LONG_INIT(0),
0038     .queue[0 ... IMA_MEASURE_HTABLE_SIZE - 1] = HLIST_HEAD_INIT
0039 };
0040 
0041 /* mutex protects atomicity of extending measurement list
0042  * and extending the TPM PCR aggregate. Since tpm_extend can take
0043  * long (and the tpm driver uses a mutex), we can't use the spinlock.
0044  */
0045 static DEFINE_MUTEX(ima_extend_list_mutex);
0046 
0047 /* lookup up the digest value in the hash table, and return the entry */
0048 static struct ima_queue_entry *ima_lookup_digest_entry(u8 *digest_value,
0049                                int pcr)
0050 {
0051     struct ima_queue_entry *qe, *ret = NULL;
0052     unsigned int key;
0053     int rc;
0054 
0055     key = ima_hash_key(digest_value);
0056     rcu_read_lock();
0057     hlist_for_each_entry_rcu(qe, &ima_htable.queue[key], hnext) {
0058         rc = memcmp(qe->entry->digests[ima_hash_algo_idx].digest,
0059                 digest_value, hash_digest_size[ima_hash_algo]);
0060         if ((rc == 0) && (qe->entry->pcr == pcr)) {
0061             ret = qe;
0062             break;
0063         }
0064     }
0065     rcu_read_unlock();
0066     return ret;
0067 }
0068 
0069 /*
0070  * Calculate the memory required for serializing a single
0071  * binary_runtime_measurement list entry, which contains a
0072  * couple of variable length fields (e.g template name and data).
0073  */
0074 static int get_binary_runtime_size(struct ima_template_entry *entry)
0075 {
0076     int size = 0;
0077 
0078     size += sizeof(u32);    /* pcr */
0079     size += TPM_DIGEST_SIZE;
0080     size += sizeof(int);    /* template name size field */
0081     size += strlen(entry->template_desc->name);
0082     size += sizeof(entry->template_data_len);
0083     size += entry->template_data_len;
0084     return size;
0085 }
0086 
0087 /* ima_add_template_entry helper function:
0088  * - Add template entry to the measurement list and hash table, for
0089  *   all entries except those carried across kexec.
0090  *
0091  * (Called with ima_extend_list_mutex held.)
0092  */
0093 static int ima_add_digest_entry(struct ima_template_entry *entry,
0094                 bool update_htable)
0095 {
0096     struct ima_queue_entry *qe;
0097     unsigned int key;
0098 
0099     qe = kmalloc(sizeof(*qe), GFP_KERNEL);
0100     if (qe == NULL) {
0101         pr_err("OUT OF MEMORY ERROR creating queue entry\n");
0102         return -ENOMEM;
0103     }
0104     qe->entry = entry;
0105 
0106     INIT_LIST_HEAD(&qe->later);
0107     list_add_tail_rcu(&qe->later, &ima_measurements);
0108 
0109     atomic_long_inc(&ima_htable.len);
0110     if (update_htable) {
0111         key = ima_hash_key(entry->digests[ima_hash_algo_idx].digest);
0112         hlist_add_head_rcu(&qe->hnext, &ima_htable.queue[key]);
0113     }
0114 
0115     if (binary_runtime_size != ULONG_MAX) {
0116         int size;
0117 
0118         size = get_binary_runtime_size(entry);
0119         binary_runtime_size = (binary_runtime_size < ULONG_MAX - size) ?
0120              binary_runtime_size + size : ULONG_MAX;
0121     }
0122     return 0;
0123 }
0124 
0125 /*
0126  * Return the amount of memory required for serializing the
0127  * entire binary_runtime_measurement list, including the ima_kexec_hdr
0128  * structure.
0129  */
0130 unsigned long ima_get_binary_runtime_size(void)
0131 {
0132     if (binary_runtime_size >= (ULONG_MAX - sizeof(struct ima_kexec_hdr)))
0133         return ULONG_MAX;
0134     else
0135         return binary_runtime_size + sizeof(struct ima_kexec_hdr);
0136 }
0137 
0138 static int ima_pcr_extend(struct tpm_digest *digests_arg, int pcr)
0139 {
0140     int result = 0;
0141 
0142     if (!ima_tpm_chip)
0143         return result;
0144 
0145     result = tpm_pcr_extend(ima_tpm_chip, pcr, digests_arg);
0146     if (result != 0)
0147         pr_err("Error Communicating to TPM chip, result: %d\n", result);
0148     return result;
0149 }
0150 
0151 /*
0152  * Add template entry to the measurement list and hash table, and
0153  * extend the pcr.
0154  *
0155  * On systems which support carrying the IMA measurement list across
0156  * kexec, maintain the total memory size required for serializing the
0157  * binary_runtime_measurements.
0158  */
0159 int ima_add_template_entry(struct ima_template_entry *entry, int violation,
0160                const char *op, struct inode *inode,
0161                const unsigned char *filename)
0162 {
0163     u8 *digest = entry->digests[ima_hash_algo_idx].digest;
0164     struct tpm_digest *digests_arg = entry->digests;
0165     const char *audit_cause = "hash_added";
0166     char tpm_audit_cause[AUDIT_CAUSE_LEN_MAX];
0167     int audit_info = 1;
0168     int result = 0, tpmresult = 0;
0169 
0170     mutex_lock(&ima_extend_list_mutex);
0171     if (!violation && !IS_ENABLED(CONFIG_IMA_DISABLE_HTABLE)) {
0172         if (ima_lookup_digest_entry(digest, entry->pcr)) {
0173             audit_cause = "hash_exists";
0174             result = -EEXIST;
0175             goto out;
0176         }
0177     }
0178 
0179     result = ima_add_digest_entry(entry,
0180                       !IS_ENABLED(CONFIG_IMA_DISABLE_HTABLE));
0181     if (result < 0) {
0182         audit_cause = "ENOMEM";
0183         audit_info = 0;
0184         goto out;
0185     }
0186 
0187     if (violation)      /* invalidate pcr */
0188         digests_arg = digests;
0189 
0190     tpmresult = ima_pcr_extend(digests_arg, entry->pcr);
0191     if (tpmresult != 0) {
0192         snprintf(tpm_audit_cause, AUDIT_CAUSE_LEN_MAX, "TPM_error(%d)",
0193              tpmresult);
0194         audit_cause = tpm_audit_cause;
0195         audit_info = 0;
0196     }
0197 out:
0198     mutex_unlock(&ima_extend_list_mutex);
0199     integrity_audit_msg(AUDIT_INTEGRITY_PCR, inode, filename,
0200                 op, audit_cause, result, audit_info);
0201     return result;
0202 }
0203 
0204 int ima_restore_measurement_entry(struct ima_template_entry *entry)
0205 {
0206     int result = 0;
0207 
0208     mutex_lock(&ima_extend_list_mutex);
0209     result = ima_add_digest_entry(entry, 0);
0210     mutex_unlock(&ima_extend_list_mutex);
0211     return result;
0212 }
0213 
0214 int __init ima_init_digests(void)
0215 {
0216     u16 digest_size;
0217     u16 crypto_id;
0218     int i;
0219 
0220     if (!ima_tpm_chip)
0221         return 0;
0222 
0223     digests = kcalloc(ima_tpm_chip->nr_allocated_banks, sizeof(*digests),
0224               GFP_NOFS);
0225     if (!digests)
0226         return -ENOMEM;
0227 
0228     for (i = 0; i < ima_tpm_chip->nr_allocated_banks; i++) {
0229         digests[i].alg_id = ima_tpm_chip->allocated_banks[i].alg_id;
0230         digest_size = ima_tpm_chip->allocated_banks[i].digest_size;
0231         crypto_id = ima_tpm_chip->allocated_banks[i].crypto_id;
0232 
0233         /* for unmapped TPM algorithms digest is still a padded SHA1 */
0234         if (crypto_id == HASH_ALGO__LAST)
0235             digest_size = SHA1_DIGEST_SIZE;
0236 
0237         memset(digests[i].digest, 0xff, digest_size);
0238     }
0239 
0240     return 0;
0241 }