ima_api.c 5.6 KB

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