PageRenderTime 33ms CodeModel.GetById 20ms RepoModel.GetById 0ms app.codeStats 1ms

/security/integrity/ima/ima_queue.c

https://github.com/kvaneesh/linux
C | 241 lines | 159 code | 34 blank | 48 comment | 25 complexity | 03d04a0baf68f723187dfaad8fc4223a MD5 | raw file
  1. // SPDX-License-Identifier: GPL-2.0-only
  2. /*
  3. * Copyright (C) 2005,2006,2007,2008 IBM Corporation
  4. *
  5. * Authors:
  6. * Serge Hallyn <serue@us.ibm.com>
  7. * Reiner Sailer <sailer@watson.ibm.com>
  8. * Mimi Zohar <zohar@us.ibm.com>
  9. *
  10. * File: ima_queue.c
  11. * Implements queues that store template measurements and
  12. * maintains aggregate over the stored measurements
  13. * in the pre-configured TPM PCR (if available).
  14. * The measurement list is append-only. No entry is
  15. * ever removed or changed during the boot-cycle.
  16. */
  17. #include <linux/rculist.h>
  18. #include <linux/slab.h>
  19. #include "ima.h"
  20. #define AUDIT_CAUSE_LEN_MAX 32
  21. /* pre-allocated array of tpm_digest structures to extend a PCR */
  22. static struct tpm_digest *digests;
  23. LIST_HEAD(ima_measurements); /* list of all measurements */
  24. #ifdef CONFIG_IMA_KEXEC
  25. static unsigned long binary_runtime_size;
  26. #else
  27. static unsigned long binary_runtime_size = ULONG_MAX;
  28. #endif
  29. /* key: inode (before secure-hashing a file) */
  30. struct ima_h_table ima_htable = {
  31. .len = ATOMIC_LONG_INIT(0),
  32. .violations = ATOMIC_LONG_INIT(0),
  33. .queue[0 ... IMA_MEASURE_HTABLE_SIZE - 1] = HLIST_HEAD_INIT
  34. };
  35. /* mutex protects atomicity of extending measurement list
  36. * and extending the TPM PCR aggregate. Since tpm_extend can take
  37. * long (and the tpm driver uses a mutex), we can't use the spinlock.
  38. */
  39. static DEFINE_MUTEX(ima_extend_list_mutex);
  40. /* lookup up the digest value in the hash table, and return the entry */
  41. static struct ima_queue_entry *ima_lookup_digest_entry(u8 *digest_value,
  42. int pcr)
  43. {
  44. struct ima_queue_entry *qe, *ret = NULL;
  45. unsigned int key;
  46. int rc;
  47. key = ima_hash_key(digest_value);
  48. rcu_read_lock();
  49. hlist_for_each_entry_rcu(qe, &ima_htable.queue[key], hnext) {
  50. rc = memcmp(qe->entry->digests[ima_hash_algo_idx].digest,
  51. digest_value, hash_digest_size[ima_hash_algo]);
  52. if ((rc == 0) && (qe->entry->pcr == pcr)) {
  53. ret = qe;
  54. break;
  55. }
  56. }
  57. rcu_read_unlock();
  58. return ret;
  59. }
  60. /*
  61. * Calculate the memory required for serializing a single
  62. * binary_runtime_measurement list entry, which contains a
  63. * couple of variable length fields (e.g template name and data).
  64. */
  65. static int get_binary_runtime_size(struct ima_template_entry *entry)
  66. {
  67. int size = 0;
  68. size += sizeof(u32); /* pcr */
  69. size += TPM_DIGEST_SIZE;
  70. size += sizeof(int); /* template name size field */
  71. size += strlen(entry->template_desc->name);
  72. size += sizeof(entry->template_data_len);
  73. size += entry->template_data_len;
  74. return size;
  75. }
  76. /* ima_add_template_entry helper function:
  77. * - Add template entry to the measurement list and hash table, for
  78. * all entries except those carried across kexec.
  79. *
  80. * (Called with ima_extend_list_mutex held.)
  81. */
  82. static int ima_add_digest_entry(struct ima_template_entry *entry,
  83. bool update_htable)
  84. {
  85. struct ima_queue_entry *qe;
  86. unsigned int key;
  87. qe = kmalloc(sizeof(*qe), GFP_KERNEL);
  88. if (qe == NULL) {
  89. pr_err("OUT OF MEMORY ERROR creating queue entry\n");
  90. return -ENOMEM;
  91. }
  92. qe->entry = entry;
  93. INIT_LIST_HEAD(&qe->later);
  94. list_add_tail_rcu(&qe->later, &ima_measurements);
  95. atomic_long_inc(&ima_htable.len);
  96. if (update_htable) {
  97. key = ima_hash_key(entry->digests[ima_hash_algo_idx].digest);
  98. hlist_add_head_rcu(&qe->hnext, &ima_htable.queue[key]);
  99. }
  100. if (binary_runtime_size != ULONG_MAX) {
  101. int size;
  102. size = get_binary_runtime_size(entry);
  103. binary_runtime_size = (binary_runtime_size < ULONG_MAX - size) ?
  104. binary_runtime_size + size : ULONG_MAX;
  105. }
  106. return 0;
  107. }
  108. /*
  109. * Return the amount of memory required for serializing the
  110. * entire binary_runtime_measurement list, including the ima_kexec_hdr
  111. * structure.
  112. */
  113. unsigned long ima_get_binary_runtime_size(void)
  114. {
  115. if (binary_runtime_size >= (ULONG_MAX - sizeof(struct ima_kexec_hdr)))
  116. return ULONG_MAX;
  117. else
  118. return binary_runtime_size + sizeof(struct ima_kexec_hdr);
  119. }
  120. static int ima_pcr_extend(struct tpm_digest *digests_arg, int pcr)
  121. {
  122. int result = 0;
  123. if (!ima_tpm_chip)
  124. return result;
  125. result = tpm_pcr_extend(ima_tpm_chip, pcr, digests_arg);
  126. if (result != 0)
  127. pr_err("Error Communicating to TPM chip, result: %d\n", result);
  128. return result;
  129. }
  130. /*
  131. * Add template entry to the measurement list and hash table, and
  132. * extend the pcr.
  133. *
  134. * On systems which support carrying the IMA measurement list across
  135. * kexec, maintain the total memory size required for serializing the
  136. * binary_runtime_measurements.
  137. */
  138. int ima_add_template_entry(struct ima_template_entry *entry, int violation,
  139. const char *op, struct inode *inode,
  140. const unsigned char *filename)
  141. {
  142. u8 *digest = entry->digests[ima_hash_algo_idx].digest;
  143. struct tpm_digest *digests_arg = entry->digests;
  144. const char *audit_cause = "hash_added";
  145. char tpm_audit_cause[AUDIT_CAUSE_LEN_MAX];
  146. int audit_info = 1;
  147. int result = 0, tpmresult = 0;
  148. mutex_lock(&ima_extend_list_mutex);
  149. if (!violation && !IS_ENABLED(CONFIG_IMA_DISABLE_HTABLE)) {
  150. if (ima_lookup_digest_entry(digest, entry->pcr)) {
  151. audit_cause = "hash_exists";
  152. result = -EEXIST;
  153. goto out;
  154. }
  155. }
  156. result = ima_add_digest_entry(entry,
  157. !IS_ENABLED(CONFIG_IMA_DISABLE_HTABLE));
  158. if (result < 0) {
  159. audit_cause = "ENOMEM";
  160. audit_info = 0;
  161. goto out;
  162. }
  163. if (violation) /* invalidate pcr */
  164. digests_arg = digests;
  165. tpmresult = ima_pcr_extend(digests_arg, entry->pcr);
  166. if (tpmresult != 0) {
  167. snprintf(tpm_audit_cause, AUDIT_CAUSE_LEN_MAX, "TPM_error(%d)",
  168. tpmresult);
  169. audit_cause = tpm_audit_cause;
  170. audit_info = 0;
  171. }
  172. out:
  173. mutex_unlock(&ima_extend_list_mutex);
  174. integrity_audit_msg(AUDIT_INTEGRITY_PCR, inode, filename,
  175. op, audit_cause, result, audit_info);
  176. return result;
  177. }
  178. int ima_restore_measurement_entry(struct ima_template_entry *entry)
  179. {
  180. int result = 0;
  181. mutex_lock(&ima_extend_list_mutex);
  182. result = ima_add_digest_entry(entry, 0);
  183. mutex_unlock(&ima_extend_list_mutex);
  184. return result;
  185. }
  186. int __init ima_init_digests(void)
  187. {
  188. u16 digest_size;
  189. u16 crypto_id;
  190. int i;
  191. if (!ima_tpm_chip)
  192. return 0;
  193. digests = kcalloc(ima_tpm_chip->nr_allocated_banks, sizeof(*digests),
  194. GFP_NOFS);
  195. if (!digests)
  196. return -ENOMEM;
  197. for (i = 0; i < ima_tpm_chip->nr_allocated_banks; i++) {
  198. digests[i].alg_id = ima_tpm_chip->allocated_banks[i].alg_id;
  199. digest_size = ima_tpm_chip->allocated_banks[i].digest_size;
  200. crypto_id = ima_tpm_chip->allocated_banks[i].crypto_id;
  201. /* for unmapped TPM algorithms digest is still a padded SHA1 */
  202. if (crypto_id == HASH_ALGO__LAST)
  203. digest_size = SHA1_DIGEST_SIZE;
  204. memset(digests[i].digest, 0xff, digest_size);
  205. }
  206. return 0;
  207. }