ima_api.c 9.0 KB

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