ima_api.c 8.5 KB

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