ima_api.c 9.1 KB

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