ima_api.c 8.4 KB

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