ima_api.c 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272
  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. struct ima_digest_data hash;
  46. memset(entry->digest, 0, sizeof(entry->digest));
  47. entry->template_name = IMA_TEMPLATE_NAME;
  48. entry->template_len = sizeof(entry->template);
  49. if (!violation) {
  50. result = ima_calc_buffer_hash(&entry->template,
  51. entry->template_len, &hash);
  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. memcpy(entry->digest, hash.digest, hash.length);
  59. }
  60. result = ima_add_template_entry(entry, violation, op, inode);
  61. return result;
  62. }
  63. /*
  64. * ima_add_violation - add violation to measurement list.
  65. *
  66. * Violations are flagged in the measurement list with zero hash values.
  67. * By extending the PCR with 0xFF's instead of with zeroes, the PCR
  68. * value is invalidated.
  69. */
  70. void ima_add_violation(struct inode *inode, const unsigned char *filename,
  71. const char *op, const char *cause)
  72. {
  73. struct ima_template_entry *entry;
  74. int violation = 1;
  75. int result;
  76. /* can overflow, only indicator */
  77. atomic_long_inc(&ima_htable.violations);
  78. entry = kmalloc(sizeof(*entry), GFP_KERNEL);
  79. if (!entry) {
  80. result = -ENOMEM;
  81. goto err_out;
  82. }
  83. memset(&entry->template, 0, sizeof(entry->template));
  84. strncpy(entry->template.file_name, filename, IMA_EVENT_NAME_LEN_MAX);
  85. result = ima_store_template(entry, violation, inode);
  86. if (result < 0)
  87. kfree(entry);
  88. err_out:
  89. integrity_audit_msg(AUDIT_INTEGRITY_PCR, inode, filename,
  90. op, cause, result, 0);
  91. }
  92. /**
  93. * ima_get_action - appraise & measure decision based on policy.
  94. * @inode: pointer to inode to measure
  95. * @mask: contains the permission mask (MAY_READ, MAY_WRITE, MAY_EXECUTE)
  96. * @function: calling function (FILE_CHECK, BPRM_CHECK, MMAP_CHECK, MODULE_CHECK)
  97. *
  98. * The policy is defined in terms of keypairs:
  99. * subj=, obj=, type=, func=, mask=, fsmagic=
  100. * subj,obj, and type: are LSM specific.
  101. * func: FILE_CHECK | BPRM_CHECK | MMAP_CHECK | MODULE_CHECK
  102. * mask: contains the permission mask
  103. * fsmagic: hex value
  104. *
  105. * Returns IMA_MEASURE, IMA_APPRAISE mask.
  106. *
  107. */
  108. int ima_get_action(struct inode *inode, int mask, int function)
  109. {
  110. int flags = IMA_MEASURE | IMA_AUDIT | IMA_APPRAISE;
  111. if (!ima_appraise)
  112. flags &= ~IMA_APPRAISE;
  113. return ima_match_policy(inode, function, mask, flags);
  114. }
  115. int ima_must_measure(struct inode *inode, int mask, int function)
  116. {
  117. return ima_match_policy(inode, function, mask, IMA_MEASURE);
  118. }
  119. /*
  120. * ima_collect_measurement - collect file measurement
  121. *
  122. * Calculate the file hash, if it doesn't already exist,
  123. * storing the measurement and i_version in the iint.
  124. *
  125. * Must be called with iint->mutex held.
  126. *
  127. * Return 0 on success, error code otherwise
  128. */
  129. int ima_collect_measurement(struct integrity_iint_cache *iint,
  130. struct file *file)
  131. {
  132. struct inode *inode = file_inode(file);
  133. const char *filename = file->f_dentry->d_name.name;
  134. int result = 0;
  135. if (!(iint->flags & IMA_COLLECTED)) {
  136. u64 i_version = file_inode(file)->i_version;
  137. /* use default hash algorithm */
  138. iint->ima_hash.algo = ima_hash_algo;
  139. result = ima_calc_file_hash(file, &iint->ima_hash);
  140. if (!result) {
  141. iint->version = i_version;
  142. iint->flags |= IMA_COLLECTED;
  143. }
  144. }
  145. if (result)
  146. integrity_audit_msg(AUDIT_INTEGRITY_DATA, inode,
  147. filename, "collect_data", "failed",
  148. result, 0);
  149. return result;
  150. }
  151. /*
  152. * ima_store_measurement - store file measurement
  153. *
  154. * Create an "ima" template and then store the template by calling
  155. * ima_store_template.
  156. *
  157. * We only get here if the inode has not already been measured,
  158. * but the measurement could already exist:
  159. * - multiple copies of the same file on either the same or
  160. * different filesystems.
  161. * - the inode was previously flushed as well as the iint info,
  162. * containing the hashing info.
  163. *
  164. * Must be called with iint->mutex held.
  165. */
  166. void ima_store_measurement(struct integrity_iint_cache *iint,
  167. struct file *file, const unsigned char *filename)
  168. {
  169. const char *op = "add_template_measure";
  170. const char *audit_cause = "ENOMEM";
  171. int result = -ENOMEM;
  172. struct inode *inode = file_inode(file);
  173. struct ima_template_entry *entry;
  174. int violation = 0;
  175. if (iint->flags & IMA_MEASURED)
  176. return;
  177. entry = kmalloc(sizeof(*entry), GFP_KERNEL);
  178. if (!entry) {
  179. integrity_audit_msg(AUDIT_INTEGRITY_PCR, inode, filename,
  180. op, audit_cause, result, 0);
  181. return;
  182. }
  183. memset(&entry->template, 0, sizeof(entry->template));
  184. if (iint->ima_hash.algo != ima_hash_algo) {
  185. struct ima_digest_data hash;
  186. hash.algo = ima_hash_algo;
  187. result = ima_calc_file_hash(file, &hash);
  188. if (result)
  189. integrity_audit_msg(AUDIT_INTEGRITY_DATA, inode,
  190. filename, "collect_data", "failed",
  191. result, 0);
  192. else
  193. memcpy(entry->template.digest, hash.digest,
  194. hash.length);
  195. } else
  196. memcpy(entry->template.digest, iint->ima_hash.digest,
  197. iint->ima_hash.length);
  198. strcpy(entry->template.file_name,
  199. (strlen(filename) > IMA_EVENT_NAME_LEN_MAX) ?
  200. file->f_dentry->d_name.name : filename);
  201. result = ima_store_template(entry, violation, inode);
  202. if (!result || result == -EEXIST)
  203. iint->flags |= IMA_MEASURED;
  204. if (result < 0)
  205. kfree(entry);
  206. }
  207. void ima_audit_measurement(struct integrity_iint_cache *iint,
  208. const unsigned char *filename)
  209. {
  210. struct audit_buffer *ab;
  211. char hash[(iint->ima_hash.length * 2) + 1];
  212. int i;
  213. if (iint->flags & IMA_AUDITED)
  214. return;
  215. for (i = 0; i < iint->ima_hash.length; i++)
  216. hex_byte_pack(hash + (i * 2), iint->ima_hash.digest[i]);
  217. hash[i * 2] = '\0';
  218. ab = audit_log_start(current->audit_context, GFP_KERNEL,
  219. AUDIT_INTEGRITY_RULE);
  220. if (!ab)
  221. return;
  222. audit_log_format(ab, "file=");
  223. audit_log_untrustedstring(ab, filename);
  224. audit_log_format(ab, " hash=");
  225. audit_log_untrustedstring(ab, hash);
  226. audit_log_task_info(ab, current);
  227. audit_log_end(ab);
  228. iint->flags |= IMA_AUDITED;
  229. }
  230. const char *ima_d_path(struct path *path, char **pathbuf)
  231. {
  232. char *pathname = NULL;
  233. /* We will allow 11 spaces for ' (deleted)' to be appended */
  234. *pathbuf = kmalloc(PATH_MAX + 11, GFP_KERNEL);
  235. if (*pathbuf) {
  236. pathname = d_path(path, *pathbuf, PATH_MAX + 11);
  237. if (IS_ERR(pathname)) {
  238. kfree(*pathbuf);
  239. *pathbuf = NULL;
  240. pathname = NULL;
  241. }
  242. }
  243. return pathname;
  244. }