ima_api.c 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256
  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_appraise_or_measure, collect_measurement,
  13. * appraise_measurement, store_measurement and store_template.
  14. */
  15. #include <linux/module.h>
  16. #include <linux/slab.h>
  17. #include <linux/file.h>
  18. #include <linux/fs.h>
  19. #include <linux/xattr.h>
  20. #include <linux/evm.h>
  21. #include "ima.h"
  22. static const char *IMA_TEMPLATE_NAME = "ima";
  23. /*
  24. * ima_store_template - store ima template measurements
  25. *
  26. * Calculate the hash of a template entry, add the template entry
  27. * to an ordered list of measurement entries maintained inside the kernel,
  28. * and also update the aggregate integrity value (maintained inside the
  29. * configured TPM PCR) over the hashes of the current list of measurement
  30. * entries.
  31. *
  32. * Applications retrieve the current kernel-held measurement list through
  33. * the securityfs entries in /sys/kernel/security/ima. The signed aggregate
  34. * TPM PCR (called quote) can be retrieved using a TPM user space library
  35. * and is used to validate the measurement list.
  36. *
  37. * Returns 0 on success, error code otherwise
  38. */
  39. int ima_store_template(struct ima_template_entry *entry,
  40. int violation, struct inode *inode)
  41. {
  42. const char *op = "add_template_measure";
  43. const char *audit_cause = "hashing_error";
  44. int result;
  45. memset(entry->digest, 0, sizeof(entry->digest));
  46. entry->template_name = IMA_TEMPLATE_NAME;
  47. entry->template_len = sizeof(entry->template);
  48. if (!violation) {
  49. result = ima_calc_buffer_hash(&entry->template,
  50. entry->template_len,
  51. entry->digest);
  52. if (result < 0) {
  53. integrity_audit_msg(AUDIT_INTEGRITY_PCR, inode,
  54. entry->template_name, op,
  55. audit_cause, result, 0);
  56. return result;
  57. }
  58. }
  59. result = ima_add_template_entry(entry, violation, op, inode);
  60. return result;
  61. }
  62. /*
  63. * ima_add_violation - add violation to measurement list.
  64. *
  65. * Violations are flagged in the measurement list with zero hash values.
  66. * By extending the PCR with 0xFF's instead of with zeroes, the PCR
  67. * value is invalidated.
  68. */
  69. void ima_add_violation(struct inode *inode, const unsigned char *filename,
  70. const char *op, const char *cause)
  71. {
  72. struct ima_template_entry *entry;
  73. int violation = 1;
  74. int result;
  75. /* can overflow, only indicator */
  76. atomic_long_inc(&ima_htable.violations);
  77. entry = kmalloc(sizeof(*entry), GFP_KERNEL);
  78. if (!entry) {
  79. result = -ENOMEM;
  80. goto err_out;
  81. }
  82. memset(&entry->template, 0, sizeof(entry->template));
  83. strncpy(entry->template.file_name, filename, IMA_EVENT_NAME_LEN_MAX);
  84. result = ima_store_template(entry, violation, inode);
  85. if (result < 0)
  86. kfree(entry);
  87. err_out:
  88. integrity_audit_msg(AUDIT_INTEGRITY_PCR, inode, filename,
  89. op, cause, result, 0);
  90. }
  91. /**
  92. * ima_get_action - appraise & measure decision based on policy.
  93. * @inode: pointer to inode to measure
  94. * @mask: contains the permission mask (MAY_READ, MAY_WRITE, MAY_EXECUTE)
  95. * @function: calling function (FILE_CHECK, BPRM_CHECK, MMAP_CHECK, MODULE_CHECK)
  96. *
  97. * The policy is defined in terms of keypairs:
  98. * subj=, obj=, type=, func=, mask=, fsmagic=
  99. * subj,obj, and type: are LSM specific.
  100. * func: FILE_CHECK | BPRM_CHECK | MMAP_CHECK | MODULE_CHECK
  101. * mask: contains the permission mask
  102. * fsmagic: hex value
  103. *
  104. * Returns IMA_MEASURE, IMA_APPRAISE mask.
  105. *
  106. */
  107. int ima_get_action(struct inode *inode, int mask, int function)
  108. {
  109. int flags = IMA_MEASURE | IMA_AUDIT | IMA_APPRAISE;
  110. if (!ima_appraise)
  111. flags &= ~IMA_APPRAISE;
  112. return ima_match_policy(inode, function, mask, flags);
  113. }
  114. int ima_must_measure(struct inode *inode, int mask, int function)
  115. {
  116. return ima_match_policy(inode, function, mask, IMA_MEASURE);
  117. }
  118. /*
  119. * ima_collect_measurement - collect file measurement
  120. *
  121. * Calculate the file hash, if it doesn't already exist,
  122. * storing the measurement and i_version in the iint.
  123. *
  124. * Must be called with iint->mutex held.
  125. *
  126. * Return 0 on success, error code otherwise
  127. */
  128. int ima_collect_measurement(struct integrity_iint_cache *iint,
  129. struct file *file)
  130. {
  131. struct inode *inode = file_inode(file);
  132. const char *filename = file->f_dentry->d_name.name;
  133. int result = 0;
  134. if (!(iint->flags & IMA_COLLECTED)) {
  135. u64 i_version = file_inode(file)->i_version;
  136. iint->ima_xattr.type = IMA_XATTR_DIGEST;
  137. result = ima_calc_file_hash(file, iint->ima_xattr.digest);
  138. if (!result) {
  139. iint->version = i_version;
  140. iint->flags |= IMA_COLLECTED;
  141. }
  142. }
  143. if (result)
  144. integrity_audit_msg(AUDIT_INTEGRITY_DATA, inode,
  145. filename, "collect_data", "failed",
  146. result, 0);
  147. return result;
  148. }
  149. /*
  150. * ima_store_measurement - store file measurement
  151. *
  152. * Create an "ima" template and then store the template by calling
  153. * ima_store_template.
  154. *
  155. * We only get here if the inode has not already been measured,
  156. * but the measurement could already exist:
  157. * - multiple copies of the same file on either the same or
  158. * different filesystems.
  159. * - the inode was previously flushed as well as the iint info,
  160. * containing the hashing info.
  161. *
  162. * Must be called with iint->mutex held.
  163. */
  164. void ima_store_measurement(struct integrity_iint_cache *iint,
  165. struct file *file, const unsigned char *filename)
  166. {
  167. const char *op = "add_template_measure";
  168. const char *audit_cause = "ENOMEM";
  169. int result = -ENOMEM;
  170. struct inode *inode = file_inode(file);
  171. struct ima_template_entry *entry;
  172. int violation = 0;
  173. if (iint->flags & IMA_MEASURED)
  174. return;
  175. entry = kmalloc(sizeof(*entry), GFP_KERNEL);
  176. if (!entry) {
  177. integrity_audit_msg(AUDIT_INTEGRITY_PCR, inode, filename,
  178. op, audit_cause, result, 0);
  179. return;
  180. }
  181. memset(&entry->template, 0, sizeof(entry->template));
  182. memcpy(entry->template.digest, iint->ima_xattr.digest, IMA_DIGEST_SIZE);
  183. strcpy(entry->template.file_name,
  184. (strlen(filename) > IMA_EVENT_NAME_LEN_MAX) ?
  185. file->f_dentry->d_name.name : filename);
  186. result = ima_store_template(entry, violation, inode);
  187. if (!result || result == -EEXIST)
  188. iint->flags |= IMA_MEASURED;
  189. if (result < 0)
  190. kfree(entry);
  191. }
  192. void ima_audit_measurement(struct integrity_iint_cache *iint,
  193. const unsigned char *filename)
  194. {
  195. struct audit_buffer *ab;
  196. char hash[(IMA_DIGEST_SIZE * 2) + 1];
  197. int i;
  198. if (iint->flags & IMA_AUDITED)
  199. return;
  200. for (i = 0; i < IMA_DIGEST_SIZE; i++)
  201. hex_byte_pack(hash + (i * 2), iint->ima_xattr.digest[i]);
  202. hash[i * 2] = '\0';
  203. ab = audit_log_start(current->audit_context, GFP_KERNEL,
  204. AUDIT_INTEGRITY_RULE);
  205. if (!ab)
  206. return;
  207. audit_log_format(ab, "file=");
  208. audit_log_untrustedstring(ab, filename);
  209. audit_log_format(ab, " hash=");
  210. audit_log_untrustedstring(ab, hash);
  211. audit_log_task_info(ab, current);
  212. audit_log_end(ab);
  213. iint->flags |= IMA_AUDITED;
  214. }
  215. const char *ima_d_path(struct path *path, char **pathbuf)
  216. {
  217. char *pathname = NULL;
  218. /* We will allow 11 spaces for ' (deleted)' to be appended */
  219. *pathbuf = kmalloc(PATH_MAX + 11, GFP_KERNEL);
  220. if (*pathbuf) {
  221. pathname = d_path(path, *pathbuf, PATH_MAX + 11);
  222. if (IS_ERR(pathname)) {
  223. kfree(*pathbuf);
  224. *pathbuf = NULL;
  225. pathname = NULL;
  226. }
  227. }
  228. return pathname;
  229. }