ima_api.c 9.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344
  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. int ima_must_measure(struct inode *inode, int mask, int function)
  165. {
  166. return ima_match_policy(inode, function, mask, IMA_MEASURE);
  167. }
  168. /*
  169. * ima_collect_measurement - collect file measurement
  170. *
  171. * Calculate the file hash, if it doesn't already exist,
  172. * storing the measurement and i_version in the iint.
  173. *
  174. * Must be called with iint->mutex held.
  175. *
  176. * Return 0 on success, error code otherwise
  177. */
  178. int ima_collect_measurement(struct integrity_iint_cache *iint,
  179. struct file *file,
  180. struct evm_ima_xattr_data **xattr_value,
  181. int *xattr_len)
  182. {
  183. const char *audit_cause = "failed";
  184. struct inode *inode = file_inode(file);
  185. const char *filename = file->f_dentry->d_name.name;
  186. int result = 0;
  187. struct {
  188. struct ima_digest_data hdr;
  189. char digest[IMA_MAX_DIGEST_SIZE];
  190. } hash;
  191. if (xattr_value)
  192. *xattr_len = ima_read_xattr(file->f_dentry, xattr_value);
  193. if (!(iint->flags & IMA_COLLECTED)) {
  194. u64 i_version = file_inode(file)->i_version;
  195. if (file->f_flags & O_DIRECT) {
  196. audit_cause = "failed(directio)";
  197. result = -EACCES;
  198. goto out;
  199. }
  200. /* use default hash algorithm */
  201. hash.hdr.algo = ima_hash_algo;
  202. if (xattr_value)
  203. ima_get_hash_algo(*xattr_value, *xattr_len, &hash.hdr);
  204. result = ima_calc_file_hash(file, &hash.hdr);
  205. if (!result) {
  206. int length = sizeof(hash.hdr) + hash.hdr.length;
  207. void *tmpbuf = krealloc(iint->ima_hash, length,
  208. GFP_NOFS);
  209. if (tmpbuf) {
  210. iint->ima_hash = tmpbuf;
  211. memcpy(iint->ima_hash, &hash, length);
  212. iint->version = i_version;
  213. iint->flags |= IMA_COLLECTED;
  214. } else
  215. result = -ENOMEM;
  216. }
  217. }
  218. out:
  219. if (result)
  220. integrity_audit_msg(AUDIT_INTEGRITY_DATA, inode,
  221. filename, "collect_data", audit_cause,
  222. result, 0);
  223. return result;
  224. }
  225. /*
  226. * ima_store_measurement - store file measurement
  227. *
  228. * Create an "ima" template and then store the template by calling
  229. * ima_store_template.
  230. *
  231. * We only get here if the inode has not already been measured,
  232. * but the measurement could already exist:
  233. * - multiple copies of the same file on either the same or
  234. * different filesystems.
  235. * - the inode was previously flushed as well as the iint info,
  236. * containing the hashing info.
  237. *
  238. * Must be called with iint->mutex held.
  239. */
  240. void ima_store_measurement(struct integrity_iint_cache *iint,
  241. struct file *file, const unsigned char *filename,
  242. struct evm_ima_xattr_data *xattr_value,
  243. int xattr_len)
  244. {
  245. static const char op[] = "add_template_measure";
  246. static const char audit_cause[] = "ENOMEM";
  247. int result = -ENOMEM;
  248. struct inode *inode = file_inode(file);
  249. struct ima_template_entry *entry;
  250. int violation = 0;
  251. if (iint->flags & IMA_MEASURED)
  252. return;
  253. result = ima_alloc_init_template(iint, file, filename,
  254. xattr_value, xattr_len, &entry);
  255. if (result < 0) {
  256. integrity_audit_msg(AUDIT_INTEGRITY_PCR, inode, filename,
  257. op, audit_cause, result, 0);
  258. return;
  259. }
  260. result = ima_store_template(entry, violation, inode, filename);
  261. if (!result || result == -EEXIST)
  262. iint->flags |= IMA_MEASURED;
  263. if (result < 0)
  264. ima_free_template_entry(entry);
  265. }
  266. void ima_audit_measurement(struct integrity_iint_cache *iint,
  267. const unsigned char *filename)
  268. {
  269. struct audit_buffer *ab;
  270. char hash[(iint->ima_hash->length * 2) + 1];
  271. const char *algo_name = hash_algo_name[iint->ima_hash->algo];
  272. char algo_hash[sizeof(hash) + strlen(algo_name) + 2];
  273. int i;
  274. if (iint->flags & IMA_AUDITED)
  275. return;
  276. for (i = 0; i < iint->ima_hash->length; i++)
  277. hex_byte_pack(hash + (i * 2), iint->ima_hash->digest[i]);
  278. hash[i * 2] = '\0';
  279. ab = audit_log_start(current->audit_context, GFP_KERNEL,
  280. AUDIT_INTEGRITY_RULE);
  281. if (!ab)
  282. return;
  283. audit_log_format(ab, "file=");
  284. audit_log_untrustedstring(ab, filename);
  285. audit_log_format(ab, " hash=");
  286. snprintf(algo_hash, sizeof(algo_hash), "%s:%s", algo_name, hash);
  287. audit_log_untrustedstring(ab, algo_hash);
  288. audit_log_task_info(ab, current);
  289. audit_log_end(ab);
  290. iint->flags |= IMA_AUDITED;
  291. }
  292. const char *ima_d_path(struct path *path, char **pathbuf)
  293. {
  294. char *pathname = NULL;
  295. /* We will allow 11 spaces for ' (deleted)' to be appended */
  296. *pathbuf = kmalloc(PATH_MAX + 11, GFP_KERNEL);
  297. if (*pathbuf) {
  298. pathname = d_path(path, *pathbuf, PATH_MAX + 11);
  299. if (IS_ERR(pathname)) {
  300. kfree(*pathbuf);
  301. *pathbuf = NULL;
  302. pathname = NULL;
  303. }
  304. }
  305. return pathname ?: (const char *)path->dentry->d_name.name;
  306. }