ima_api.c 9.3 KB

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