ima_api.c 9.0 KB

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