ima_api.c 8.4 KB

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