ima_api.c 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186
  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. * Return 0 to measure. For matching a DONT_MEASURE policy, no policy,
  101. * or other error, return an error code.
  102. */
  103. int ima_must_measure(struct inode *inode, int mask, int function)
  104. {
  105. int must_measure;
  106. must_measure = ima_match_policy(inode, function, mask);
  107. return must_measure ? 0 : -EACCES;
  108. }
  109. /*
  110. * ima_collect_measurement - collect file measurement
  111. *
  112. * Calculate the file hash, if it doesn't already exist,
  113. * storing the measurement and i_version in the iint.
  114. *
  115. * Must be called with iint->mutex held.
  116. *
  117. * Return 0 on success, error code otherwise
  118. */
  119. int ima_collect_measurement(struct integrity_iint_cache *iint,
  120. struct file *file)
  121. {
  122. int result = -EEXIST;
  123. if (!(iint->flags & IMA_MEASURED)) {
  124. u64 i_version = file->f_dentry->d_inode->i_version;
  125. memset(iint->digest, 0, IMA_DIGEST_SIZE);
  126. result = ima_calc_hash(file, iint->digest);
  127. if (!result)
  128. iint->version = i_version;
  129. }
  130. return result;
  131. }
  132. /*
  133. * ima_store_measurement - store file measurement
  134. *
  135. * Create an "ima" template and then store the template by calling
  136. * ima_store_template.
  137. *
  138. * We only get here if the inode has not already been measured,
  139. * but the measurement could already exist:
  140. * - multiple copies of the same file on either the same or
  141. * different filesystems.
  142. * - the inode was previously flushed as well as the iint info,
  143. * containing the hashing info.
  144. *
  145. * Must be called with iint->mutex held.
  146. */
  147. void ima_store_measurement(struct integrity_iint_cache *iint,
  148. struct file *file, const unsigned char *filename)
  149. {
  150. const char *op = "add_template_measure";
  151. const char *audit_cause = "ENOMEM";
  152. int result = -ENOMEM;
  153. struct inode *inode = file->f_dentry->d_inode;
  154. struct ima_template_entry *entry;
  155. int violation = 0;
  156. entry = kmalloc(sizeof(*entry), GFP_KERNEL);
  157. if (!entry) {
  158. integrity_audit_msg(AUDIT_INTEGRITY_PCR, inode, filename,
  159. op, audit_cause, result, 0);
  160. return;
  161. }
  162. memset(&entry->template, 0, sizeof(entry->template));
  163. memcpy(entry->template.digest, iint->digest, IMA_DIGEST_SIZE);
  164. strncpy(entry->template.file_name, filename, IMA_EVENT_NAME_LEN_MAX);
  165. result = ima_store_template(entry, violation, inode);
  166. if (!result || result == -EEXIST)
  167. iint->flags |= IMA_MEASURED;
  168. if (result < 0)
  169. kfree(entry);
  170. }