ima_api.c 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190
  1. /*
  2. * Copyright (C) 2008 IBM Corporation
  3. *
  4. * Author: Mimi Zohar <zohar@us.ibm.com>
  5. *
  6. * This program is free software; you can redistribute it and/or
  7. * modify it under the terms of the GNU General Public License as
  8. * published by the Free Software Foundation, version 2 of the
  9. * License.
  10. *
  11. * File: ima_api.c
  12. * Implements must_measure, collect_measurement, store_measurement,
  13. * and store_template.
  14. */
  15. #include <linux/module.h>
  16. #include "ima.h"
  17. static const char *IMA_TEMPLATE_NAME = "ima";
  18. /*
  19. * ima_store_template - store ima template measurements
  20. *
  21. * Calculate the hash of a template entry, add the template entry
  22. * to an ordered list of measurement entries maintained inside the kernel,
  23. * and also update the aggregate integrity value (maintained inside the
  24. * configured TPM PCR) over the hashes of the current list of measurement
  25. * entries.
  26. *
  27. * Applications retrieve the current kernel-held measurement list through
  28. * the securityfs entries in /sys/kernel/security/ima. The signed aggregate
  29. * TPM PCR (called quote) can be retrieved using a TPM user space library
  30. * and is used to validate the measurement list.
  31. *
  32. * Returns 0 on success, error code otherwise
  33. */
  34. int ima_store_template(struct ima_template_entry *entry,
  35. int violation, struct inode *inode)
  36. {
  37. const char *op = "add_template_measure";
  38. const char *audit_cause = "hashing_error";
  39. int result;
  40. memset(entry->digest, 0, sizeof(entry->digest));
  41. entry->template_name = IMA_TEMPLATE_NAME;
  42. entry->template_len = sizeof(entry->template);
  43. if (!violation) {
  44. result = ima_calc_template_hash(entry->template_len,
  45. &entry->template,
  46. entry->digest);
  47. if (result < 0) {
  48. integrity_audit_msg(AUDIT_INTEGRITY_PCR, inode,
  49. entry->template_name, op,
  50. audit_cause, result, 0);
  51. return result;
  52. }
  53. }
  54. result = ima_add_template_entry(entry, violation, op, inode);
  55. return result;
  56. }
  57. /*
  58. * ima_add_violation - add violation to measurement list.
  59. *
  60. * Violations are flagged in the measurement list with zero hash values.
  61. * By extending the PCR with 0xFF's instead of with zeroes, the PCR
  62. * value is invalidated.
  63. */
  64. void ima_add_violation(struct inode *inode, const unsigned char *filename,
  65. const char *op, const char *cause)
  66. {
  67. struct ima_template_entry *entry;
  68. int violation = 1;
  69. int result;
  70. /* can overflow, only indicator */
  71. atomic_long_inc(&ima_htable.violations);
  72. entry = kmalloc(sizeof(*entry), GFP_KERNEL);
  73. if (!entry) {
  74. result = -ENOMEM;
  75. goto err_out;
  76. }
  77. memset(&entry->template, 0, sizeof(entry->template));
  78. strncpy(entry->template.file_name, filename, IMA_EVENT_NAME_LEN_MAX);
  79. result = ima_store_template(entry, violation, inode);
  80. if (result < 0)
  81. kfree(entry);
  82. err_out:
  83. integrity_audit_msg(AUDIT_INTEGRITY_PCR, inode, filename,
  84. op, cause, result, 0);
  85. }
  86. /**
  87. * ima_must_measure - measure decision based on policy.
  88. * @inode: pointer to inode to measure
  89. * @mask: contains the permission mask (MAY_READ, MAY_WRITE, MAY_EXECUTE)
  90. * @function: calling function (PATH_CHECK, BPRM_CHECK, FILE_MMAP)
  91. *
  92. * The policy is defined in terms of keypairs:
  93. * subj=, obj=, type=, func=, mask=, fsmagic=
  94. * subj,obj, and type: are LSM specific.
  95. * func: PATH_CHECK | BPRM_CHECK | FILE_MMAP
  96. * mask: contains the permission mask
  97. * fsmagic: hex value
  98. *
  99. * Must be called with iint->mutex held.
  100. *
  101. * Return 0 to measure. Return 1 if already measured.
  102. * For matching a DONT_MEASURE policy, no policy, or other
  103. * error, return an error code.
  104. */
  105. int ima_must_measure(struct ima_iint_cache *iint, struct inode *inode,
  106. int mask, int function)
  107. {
  108. int must_measure;
  109. if (iint->flags & IMA_MEASURED)
  110. return 1;
  111. must_measure = ima_match_policy(inode, function, mask);
  112. return must_measure ? 0 : -EACCES;
  113. }
  114. /*
  115. * ima_collect_measurement - collect file measurement
  116. *
  117. * Calculate the file hash, if it doesn't already exist,
  118. * storing the measurement and i_version in the iint.
  119. *
  120. * Must be called with iint->mutex held.
  121. *
  122. * Return 0 on success, error code otherwise
  123. */
  124. int ima_collect_measurement(struct ima_iint_cache *iint, struct file *file)
  125. {
  126. int result = -EEXIST;
  127. if (!(iint->flags & IMA_MEASURED)) {
  128. u64 i_version = file->f_dentry->d_inode->i_version;
  129. memset(iint->digest, 0, IMA_DIGEST_SIZE);
  130. result = ima_calc_hash(file, iint->digest);
  131. if (!result)
  132. iint->version = i_version;
  133. }
  134. return result;
  135. }
  136. /*
  137. * ima_store_measurement - store file measurement
  138. *
  139. * Create an "ima" template and then store the template by calling
  140. * ima_store_template.
  141. *
  142. * We only get here if the inode has not already been measured,
  143. * but the measurement could already exist:
  144. * - multiple copies of the same file on either the same or
  145. * different filesystems.
  146. * - the inode was previously flushed as well as the iint info,
  147. * containing the hashing info.
  148. *
  149. * Must be called with iint->mutex held.
  150. */
  151. void ima_store_measurement(struct ima_iint_cache *iint, struct file *file,
  152. const unsigned char *filename)
  153. {
  154. const char *op = "add_template_measure";
  155. const char *audit_cause = "ENOMEM";
  156. int result = -ENOMEM;
  157. struct inode *inode = file->f_dentry->d_inode;
  158. struct ima_template_entry *entry;
  159. int violation = 0;
  160. entry = kmalloc(sizeof(*entry), GFP_KERNEL);
  161. if (!entry) {
  162. integrity_audit_msg(AUDIT_INTEGRITY_PCR, inode, filename,
  163. op, audit_cause, result, 0);
  164. return;
  165. }
  166. memset(&entry->template, 0, sizeof(entry->template));
  167. memcpy(entry->template.digest, iint->digest, IMA_DIGEST_SIZE);
  168. strncpy(entry->template.file_name, filename, IMA_EVENT_NAME_LEN_MAX);
  169. result = ima_store_template(entry, violation, inode);
  170. if (!result)
  171. iint->flags |= IMA_MEASURED;
  172. else
  173. kfree(entry);
  174. }