ima_api.c 9.3 KB

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