ima_api.c 9.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352
  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, int pcr)
  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. entry->pcr = pcr;
  106. result = ima_add_template_entry(entry, violation, op, inode, filename);
  107. return result;
  108. }
  109. /*
  110. * ima_add_violation - add violation to measurement list.
  111. *
  112. * Violations are flagged in the measurement list with zero hash values.
  113. * By extending the PCR with 0xFF's instead of with zeroes, the PCR
  114. * value is invalidated.
  115. */
  116. void ima_add_violation(struct file *file, const unsigned char *filename,
  117. struct integrity_iint_cache *iint,
  118. const char *op, const char *cause)
  119. {
  120. struct ima_template_entry *entry;
  121. struct inode *inode = file_inode(file);
  122. struct ima_event_data event_data = {iint, file, filename, NULL, 0,
  123. cause};
  124. int violation = 1;
  125. int result;
  126. /* can overflow, only indicator */
  127. atomic_long_inc(&ima_htable.violations);
  128. result = ima_alloc_init_template(&event_data, &entry);
  129. if (result < 0) {
  130. result = -ENOMEM;
  131. goto err_out;
  132. }
  133. result = ima_store_template(entry, violation, inode,
  134. filename, CONFIG_IMA_MEASURE_PCR_IDX);
  135. if (result < 0)
  136. ima_free_template_entry(entry);
  137. err_out:
  138. integrity_audit_msg(AUDIT_INTEGRITY_PCR, inode, filename,
  139. op, cause, result, 0);
  140. }
  141. /**
  142. * ima_get_action - appraise & measure decision based on policy.
  143. * @inode: pointer to inode to measure
  144. * @mask: contains the permission mask (MAY_READ, MAY_WRITE, MAY_EXECUTE)
  145. * @func: caller identifier
  146. * @pcr: pointer filled in if matched measure policy sets pcr=
  147. *
  148. * The policy is defined in terms of keypairs:
  149. * subj=, obj=, type=, func=, mask=, fsmagic=
  150. * subj,obj, and type: are LSM specific.
  151. * func: FILE_CHECK | BPRM_CHECK | MMAP_CHECK | MODULE_CHECK
  152. * mask: contains the permission mask
  153. * fsmagic: hex value
  154. *
  155. * Returns IMA_MEASURE, IMA_APPRAISE mask.
  156. *
  157. */
  158. int ima_get_action(struct inode *inode, int mask, enum ima_hooks func, int *pcr)
  159. {
  160. int flags = IMA_MEASURE | IMA_AUDIT | IMA_APPRAISE;
  161. flags &= ima_policy_flag;
  162. return ima_match_policy(inode, func, mask, flags, pcr);
  163. }
  164. /*
  165. * ima_collect_measurement - collect file measurement
  166. *
  167. * Calculate the file hash, if it doesn't already exist,
  168. * storing the measurement and i_version in the iint.
  169. *
  170. * Must be called with iint->mutex held.
  171. *
  172. * Return 0 on success, error code otherwise
  173. */
  174. int ima_collect_measurement(struct integrity_iint_cache *iint,
  175. struct file *file, void *buf, loff_t size,
  176. enum hash_algo algo)
  177. {
  178. const char *audit_cause = "failed";
  179. struct inode *inode = file_inode(file);
  180. const char *filename = file->f_path.dentry->d_name.name;
  181. int result = 0;
  182. struct {
  183. struct ima_digest_data hdr;
  184. char digest[IMA_MAX_DIGEST_SIZE];
  185. } hash;
  186. if (!(iint->flags & IMA_COLLECTED)) {
  187. u64 i_version = file_inode(file)->i_version;
  188. if (file->f_flags & O_DIRECT) {
  189. audit_cause = "failed(directio)";
  190. result = -EACCES;
  191. goto out;
  192. }
  193. hash.hdr.algo = algo;
  194. result = (!buf) ? ima_calc_file_hash(file, &hash.hdr) :
  195. ima_calc_buffer_hash(buf, size, &hash.hdr);
  196. if (!result) {
  197. int length = sizeof(hash.hdr) + hash.hdr.length;
  198. void *tmpbuf = krealloc(iint->ima_hash, length,
  199. GFP_NOFS);
  200. if (tmpbuf) {
  201. iint->ima_hash = tmpbuf;
  202. memcpy(iint->ima_hash, &hash, length);
  203. iint->version = i_version;
  204. iint->flags |= IMA_COLLECTED;
  205. } else
  206. result = -ENOMEM;
  207. }
  208. }
  209. out:
  210. if (result)
  211. integrity_audit_msg(AUDIT_INTEGRITY_DATA, inode,
  212. filename, "collect_data", audit_cause,
  213. result, 0);
  214. return result;
  215. }
  216. /*
  217. * ima_store_measurement - store file measurement
  218. *
  219. * Create an "ima" template and then store the template by calling
  220. * ima_store_template.
  221. *
  222. * We only get here if the inode has not already been measured,
  223. * but the measurement could already exist:
  224. * - multiple copies of the same file on either the same or
  225. * different filesystems.
  226. * - the inode was previously flushed as well as the iint info,
  227. * containing the hashing info.
  228. *
  229. * Must be called with iint->mutex held.
  230. */
  231. void ima_store_measurement(struct integrity_iint_cache *iint,
  232. struct file *file, const unsigned char *filename,
  233. struct evm_ima_xattr_data *xattr_value,
  234. int xattr_len, int pcr)
  235. {
  236. static const char op[] = "add_template_measure";
  237. static const char audit_cause[] = "ENOMEM";
  238. int result = -ENOMEM;
  239. struct inode *inode = file_inode(file);
  240. struct ima_template_entry *entry;
  241. struct ima_event_data event_data = {iint, file, filename, xattr_value,
  242. xattr_len, NULL};
  243. int violation = 0;
  244. if (iint->measured_pcrs & (0x1 << pcr))
  245. return;
  246. result = ima_alloc_init_template(&event_data, &entry);
  247. if (result < 0) {
  248. integrity_audit_msg(AUDIT_INTEGRITY_PCR, inode, filename,
  249. op, audit_cause, result, 0);
  250. return;
  251. }
  252. result = ima_store_template(entry, violation, inode, filename, pcr);
  253. if (!result || result == -EEXIST) {
  254. iint->flags |= IMA_MEASURED;
  255. iint->measured_pcrs |= (0x1 << pcr);
  256. }
  257. if (result < 0)
  258. ima_free_template_entry(entry);
  259. }
  260. void ima_audit_measurement(struct integrity_iint_cache *iint,
  261. const unsigned char *filename)
  262. {
  263. struct audit_buffer *ab;
  264. char hash[(iint->ima_hash->length * 2) + 1];
  265. const char *algo_name = hash_algo_name[iint->ima_hash->algo];
  266. char algo_hash[sizeof(hash) + strlen(algo_name) + 2];
  267. int i;
  268. if (iint->flags & IMA_AUDITED)
  269. return;
  270. for (i = 0; i < iint->ima_hash->length; i++)
  271. hex_byte_pack(hash + (i * 2), iint->ima_hash->digest[i]);
  272. hash[i * 2] = '\0';
  273. ab = audit_log_start(current->audit_context, GFP_KERNEL,
  274. AUDIT_INTEGRITY_RULE);
  275. if (!ab)
  276. return;
  277. audit_log_format(ab, "file=");
  278. audit_log_untrustedstring(ab, filename);
  279. audit_log_format(ab, " hash=");
  280. snprintf(algo_hash, sizeof(algo_hash), "%s:%s", algo_name, hash);
  281. audit_log_untrustedstring(ab, algo_hash);
  282. audit_log_task_info(ab, current);
  283. audit_log_end(ab);
  284. iint->flags |= IMA_AUDITED;
  285. }
  286. /*
  287. * ima_d_path - return a pointer to the full pathname
  288. *
  289. * Attempt to return a pointer to the full pathname for use in the
  290. * IMA measurement list, IMA audit records, and auditing logs.
  291. *
  292. * On failure, return a pointer to a copy of the filename, not dname.
  293. * Returning a pointer to dname, could result in using the pointer
  294. * after the memory has been freed.
  295. */
  296. const char *ima_d_path(const struct path *path, char **pathbuf, char *namebuf)
  297. {
  298. char *pathname = NULL;
  299. *pathbuf = __getname();
  300. if (*pathbuf) {
  301. pathname = d_absolute_path(path, *pathbuf, PATH_MAX);
  302. if (IS_ERR(pathname)) {
  303. __putname(*pathbuf);
  304. *pathbuf = NULL;
  305. pathname = NULL;
  306. }
  307. }
  308. if (!pathname) {
  309. strlcpy(namebuf, path->dentry->d_name.name, NAME_MAX);
  310. pathname = namebuf;
  311. }
  312. return pathname;
  313. }