ima_queue.c 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290
  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/reboot.h>
  19. #include <linux/slab.h>
  20. #include "ima.h"
  21. #define AUDIT_CAUSE_LEN_MAX 32
  22. /* pre-allocated array of tpm_digest structures to extend a PCR */
  23. static struct tpm_digest *digests;
  24. LIST_HEAD(ima_measurements); /* list of all measurements */
  25. #ifdef CONFIG_IMA_KEXEC
  26. static unsigned long binary_runtime_size;
  27. #else
  28. static unsigned long binary_runtime_size = ULONG_MAX;
  29. #endif
  30. /* key: inode (before secure-hashing a file) */
  31. struct ima_h_table ima_htable = {
  32. .len = ATOMIC_LONG_INIT(0),
  33. .violations = ATOMIC_LONG_INIT(0),
  34. .queue[0 ... IMA_MEASURE_HTABLE_SIZE - 1] = HLIST_HEAD_INIT
  35. };
  36. /* mutex protects atomicity of extending measurement list
  37. * and extending the TPM PCR aggregate. Since tpm_extend can take
  38. * long (and the tpm driver uses a mutex), we can't use the spinlock.
  39. */
  40. static DEFINE_MUTEX(ima_extend_list_mutex);
  41. /*
  42. * Used internally by the kernel to suspend measurements.
  43. * Protected by ima_extend_list_mutex.
  44. */
  45. static bool ima_measurements_suspended;
  46. /* lookup up the digest value in the hash table, and return the entry */
  47. static struct ima_queue_entry *ima_lookup_digest_entry(u8 *digest_value,
  48. int pcr)
  49. {
  50. struct ima_queue_entry *qe, *ret = NULL;
  51. unsigned int key;
  52. int rc;
  53. key = ima_hash_key(digest_value);
  54. rcu_read_lock();
  55. hlist_for_each_entry_rcu(qe, &ima_htable.queue[key], hnext) {
  56. rc = memcmp(qe->entry->digests[ima_hash_algo_idx].digest,
  57. digest_value, hash_digest_size[ima_hash_algo]);
  58. if ((rc == 0) && (qe->entry->pcr == pcr)) {
  59. ret = qe;
  60. break;
  61. }
  62. }
  63. rcu_read_unlock();
  64. return ret;
  65. }
  66. /*
  67. * Calculate the memory required for serializing a single
  68. * binary_runtime_measurement list entry, which contains a
  69. * couple of variable length fields (e.g template name and data).
  70. */
  71. static int get_binary_runtime_size(struct ima_template_entry *entry)
  72. {
  73. int size = 0;
  74. size += sizeof(u32); /* pcr */
  75. size += TPM_DIGEST_SIZE;
  76. size += sizeof(int); /* template name size field */
  77. size += strlen(entry->template_desc->name);
  78. size += sizeof(entry->template_data_len);
  79. size += entry->template_data_len;
  80. return size;
  81. }
  82. /* ima_add_template_entry helper function:
  83. * - Add template entry to the measurement list and hash table, for
  84. * all entries except those carried across kexec.
  85. *
  86. * (Called with ima_extend_list_mutex held.)
  87. */
  88. static int ima_add_digest_entry(struct ima_template_entry *entry,
  89. bool update_htable)
  90. {
  91. struct ima_queue_entry *qe;
  92. unsigned int key;
  93. qe = kmalloc_obj(*qe);
  94. if (qe == NULL) {
  95. pr_err("OUT OF MEMORY ERROR creating queue entry\n");
  96. return -ENOMEM;
  97. }
  98. qe->entry = entry;
  99. INIT_LIST_HEAD(&qe->later);
  100. list_add_tail_rcu(&qe->later, &ima_measurements);
  101. atomic_long_inc(&ima_htable.len);
  102. if (update_htable) {
  103. key = ima_hash_key(entry->digests[ima_hash_algo_idx].digest);
  104. hlist_add_head_rcu(&qe->hnext, &ima_htable.queue[key]);
  105. }
  106. if (binary_runtime_size != ULONG_MAX) {
  107. int size;
  108. size = get_binary_runtime_size(entry);
  109. binary_runtime_size = (binary_runtime_size < ULONG_MAX - size) ?
  110. binary_runtime_size + size : ULONG_MAX;
  111. }
  112. return 0;
  113. }
  114. /*
  115. * Return the amount of memory required for serializing the
  116. * entire binary_runtime_measurement list, including the ima_kexec_hdr
  117. * structure.
  118. */
  119. unsigned long ima_get_binary_runtime_size(void)
  120. {
  121. if (binary_runtime_size >= (ULONG_MAX - sizeof(struct ima_kexec_hdr)))
  122. return ULONG_MAX;
  123. else
  124. return binary_runtime_size + sizeof(struct ima_kexec_hdr);
  125. }
  126. static int ima_pcr_extend(struct tpm_digest *digests_arg, int pcr)
  127. {
  128. int result = 0;
  129. if (!ima_tpm_chip)
  130. return result;
  131. result = tpm_pcr_extend(ima_tpm_chip, pcr, digests_arg);
  132. if (result != 0)
  133. pr_err("Error Communicating to TPM chip, result: %d\n", result);
  134. return result;
  135. }
  136. /*
  137. * Add template entry to the measurement list and hash table, and
  138. * extend the pcr.
  139. *
  140. * On systems which support carrying the IMA measurement list across
  141. * kexec, maintain the total memory size required for serializing the
  142. * binary_runtime_measurements.
  143. */
  144. int ima_add_template_entry(struct ima_template_entry *entry, int violation,
  145. const char *op, struct inode *inode,
  146. const unsigned char *filename)
  147. {
  148. u8 *digest = entry->digests[ima_hash_algo_idx].digest;
  149. struct tpm_digest *digests_arg = entry->digests;
  150. const char *audit_cause = "hash_added";
  151. char tpm_audit_cause[AUDIT_CAUSE_LEN_MAX];
  152. int audit_info = 1;
  153. int result = 0, tpmresult = 0;
  154. mutex_lock(&ima_extend_list_mutex);
  155. /*
  156. * Avoid appending to the measurement log when the TPM subsystem has
  157. * been shut down while preparing for system reboot.
  158. */
  159. if (ima_measurements_suspended) {
  160. audit_cause = "measurements_suspended";
  161. audit_info = 0;
  162. result = -ENODEV;
  163. goto out;
  164. }
  165. if (!violation && !IS_ENABLED(CONFIG_IMA_DISABLE_HTABLE)) {
  166. if (ima_lookup_digest_entry(digest, entry->pcr)) {
  167. audit_cause = "hash_exists";
  168. result = -EEXIST;
  169. goto out;
  170. }
  171. }
  172. result = ima_add_digest_entry(entry,
  173. !IS_ENABLED(CONFIG_IMA_DISABLE_HTABLE));
  174. if (result < 0) {
  175. audit_cause = "ENOMEM";
  176. audit_info = 0;
  177. goto out;
  178. }
  179. if (violation) /* invalidate pcr */
  180. digests_arg = digests;
  181. tpmresult = ima_pcr_extend(digests_arg, entry->pcr);
  182. if (tpmresult != 0) {
  183. snprintf(tpm_audit_cause, AUDIT_CAUSE_LEN_MAX, "TPM_error(%d)",
  184. tpmresult);
  185. audit_cause = tpm_audit_cause;
  186. audit_info = 0;
  187. }
  188. out:
  189. mutex_unlock(&ima_extend_list_mutex);
  190. integrity_audit_msg(AUDIT_INTEGRITY_PCR, inode, filename,
  191. op, audit_cause, result, audit_info);
  192. return result;
  193. }
  194. int ima_restore_measurement_entry(struct ima_template_entry *entry)
  195. {
  196. int result = 0;
  197. mutex_lock(&ima_extend_list_mutex);
  198. result = ima_add_digest_entry(entry, 0);
  199. mutex_unlock(&ima_extend_list_mutex);
  200. return result;
  201. }
  202. static void ima_measurements_suspend(void)
  203. {
  204. mutex_lock(&ima_extend_list_mutex);
  205. ima_measurements_suspended = true;
  206. mutex_unlock(&ima_extend_list_mutex);
  207. }
  208. static int ima_reboot_notifier(struct notifier_block *nb,
  209. unsigned long action,
  210. void *data)
  211. {
  212. #ifdef CONFIG_IMA_KEXEC
  213. if (action == SYS_RESTART && data && !strcmp(data, "kexec reboot"))
  214. ima_measure_kexec_event("kexec_execute");
  215. #endif
  216. ima_measurements_suspend();
  217. return NOTIFY_DONE;
  218. }
  219. static struct notifier_block ima_reboot_nb = {
  220. .notifier_call = ima_reboot_notifier,
  221. };
  222. void __init ima_init_reboot_notifier(void)
  223. {
  224. register_reboot_notifier(&ima_reboot_nb);
  225. }
  226. int __init ima_init_digests(void)
  227. {
  228. u16 digest_size;
  229. u16 crypto_id;
  230. int i;
  231. if (!ima_tpm_chip)
  232. return 0;
  233. digests = kzalloc_objs(*digests, ima_tpm_chip->nr_allocated_banks,
  234. GFP_NOFS);
  235. if (!digests)
  236. return -ENOMEM;
  237. for (i = 0; i < ima_tpm_chip->nr_allocated_banks; i++) {
  238. digests[i].alg_id = ima_tpm_chip->allocated_banks[i].alg_id;
  239. digest_size = ima_tpm_chip->allocated_banks[i].digest_size;
  240. crypto_id = ima_tpm_chip->allocated_banks[i].crypto_id;
  241. /* for unmapped TPM algorithms digest is still a padded SHA1 */
  242. if (crypto_id == HASH_ALGO__LAST)
  243. digest_size = SHA1_DIGEST_SIZE;
  244. memset(digests[i].digest, 0xff, digest_size);
  245. }
  246. return 0;
  247. }