ima_api.c 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378
  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 <linux/iversion.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. const struct ima_template_field *field =
  48. template_desc->fields[i];
  49. u32 len;
  50. result = field->field_init(event_data,
  51. &((*entry)->template_data[i]));
  52. if (result != 0)
  53. goto out;
  54. len = (*entry)->template_data[i].len;
  55. (*entry)->template_data_len += sizeof(len);
  56. (*entry)->template_data_len += len;
  57. }
  58. return 0;
  59. out:
  60. ima_free_template_entry(*entry);
  61. *entry = NULL;
  62. return result;
  63. }
  64. /*
  65. * ima_store_template - store ima template measurements
  66. *
  67. * Calculate the hash of a template entry, add the template entry
  68. * to an ordered list of measurement entries maintained inside the kernel,
  69. * and also update the aggregate integrity value (maintained inside the
  70. * configured TPM PCR) over the hashes of the current list of measurement
  71. * entries.
  72. *
  73. * Applications retrieve the current kernel-held measurement list through
  74. * the securityfs entries in /sys/kernel/security/ima. The signed aggregate
  75. * TPM PCR (called quote) can be retrieved using a TPM user space library
  76. * and is used to validate the measurement list.
  77. *
  78. * Returns 0 on success, error code otherwise
  79. */
  80. int ima_store_template(struct ima_template_entry *entry,
  81. int violation, struct inode *inode,
  82. const unsigned char *filename, int pcr)
  83. {
  84. static const char op[] = "add_template_measure";
  85. static const char audit_cause[] = "hashing_error";
  86. char *template_name = entry->template_desc->name;
  87. int result;
  88. struct {
  89. struct ima_digest_data hdr;
  90. char digest[TPM_DIGEST_SIZE];
  91. } hash;
  92. if (!violation) {
  93. int num_fields = entry->template_desc->num_fields;
  94. /* this function uses default algo */
  95. hash.hdr.algo = HASH_ALGO_SHA1;
  96. result = ima_calc_field_array_hash(&entry->template_data[0],
  97. entry->template_desc,
  98. num_fields, &hash.hdr);
  99. if (result < 0) {
  100. integrity_audit_msg(AUDIT_INTEGRITY_PCR, inode,
  101. template_name, op,
  102. audit_cause, result, 0);
  103. return result;
  104. }
  105. memcpy(entry->digest, hash.hdr.digest, hash.hdr.length);
  106. }
  107. entry->pcr = pcr;
  108. result = ima_add_template_entry(entry, violation, op, inode, filename);
  109. return result;
  110. }
  111. /*
  112. * ima_add_violation - add violation to measurement list.
  113. *
  114. * Violations are flagged in the measurement list with zero hash values.
  115. * By extending the PCR with 0xFF's instead of with zeroes, the PCR
  116. * value is invalidated.
  117. */
  118. void ima_add_violation(struct file *file, const unsigned char *filename,
  119. struct integrity_iint_cache *iint,
  120. const char *op, const char *cause)
  121. {
  122. struct ima_template_entry *entry;
  123. struct inode *inode = file_inode(file);
  124. struct ima_event_data event_data = {iint, file, filename, NULL, 0,
  125. cause};
  126. int violation = 1;
  127. int result;
  128. /* can overflow, only indicator */
  129. atomic_long_inc(&ima_htable.violations);
  130. result = ima_alloc_init_template(&event_data, &entry);
  131. if (result < 0) {
  132. result = -ENOMEM;
  133. goto err_out;
  134. }
  135. result = ima_store_template(entry, violation, inode,
  136. filename, CONFIG_IMA_MEASURE_PCR_IDX);
  137. if (result < 0)
  138. ima_free_template_entry(entry);
  139. err_out:
  140. integrity_audit_msg(AUDIT_INTEGRITY_PCR, inode, filename,
  141. op, cause, result, 0);
  142. }
  143. /**
  144. * ima_get_action - appraise & measure decision based on policy.
  145. * @inode: pointer to inode to measure
  146. * @cred: pointer to credentials structure to validate
  147. * @secid: secid of the task being validated
  148. * @mask: contains the permission mask (MAY_READ, MAY_WRITE, MAY_EXEC,
  149. * MAY_APPEND)
  150. * @func: caller identifier
  151. * @pcr: pointer filled in if matched measure policy sets pcr=
  152. *
  153. * The policy is defined in terms of keypairs:
  154. * subj=, obj=, type=, func=, mask=, fsmagic=
  155. * subj,obj, and type: are LSM specific.
  156. * func: FILE_CHECK | BPRM_CHECK | CREDS_CHECK | MMAP_CHECK | MODULE_CHECK
  157. * mask: contains the permission mask
  158. * fsmagic: hex value
  159. *
  160. * Returns IMA_MEASURE, IMA_APPRAISE mask.
  161. *
  162. */
  163. int ima_get_action(struct inode *inode, const struct cred *cred, u32 secid,
  164. int mask, enum ima_hooks func, int *pcr)
  165. {
  166. int flags = IMA_MEASURE | IMA_AUDIT | IMA_APPRAISE | IMA_HASH;
  167. flags &= ima_policy_flag;
  168. return ima_match_policy(inode, cred, secid, func, mask, flags, pcr);
  169. }
  170. /*
  171. * ima_collect_measurement - collect file measurement
  172. *
  173. * Calculate the file hash, if it doesn't already exist,
  174. * storing the measurement and i_version in the iint.
  175. *
  176. * Must be called with iint->mutex held.
  177. *
  178. * Return 0 on success, error code otherwise
  179. */
  180. int ima_collect_measurement(struct integrity_iint_cache *iint,
  181. struct file *file, void *buf, loff_t size,
  182. enum hash_algo algo)
  183. {
  184. const char *audit_cause = "failed";
  185. struct inode *inode = file_inode(file);
  186. const char *filename = file->f_path.dentry->d_name.name;
  187. int result = 0;
  188. int length;
  189. void *tmpbuf;
  190. u64 i_version;
  191. struct {
  192. struct ima_digest_data hdr;
  193. char digest[IMA_MAX_DIGEST_SIZE];
  194. } hash;
  195. if (iint->flags & IMA_COLLECTED)
  196. goto out;
  197. /*
  198. * Dectecting file change is based on i_version. On filesystems
  199. * which do not support i_version, support is limited to an initial
  200. * measurement/appraisal/audit.
  201. */
  202. i_version = inode_query_iversion(inode);
  203. hash.hdr.algo = algo;
  204. /* Initialize hash digest to 0's in case of failure */
  205. memset(&hash.digest, 0, sizeof(hash.digest));
  206. if (buf)
  207. result = ima_calc_buffer_hash(buf, size, &hash.hdr);
  208. else
  209. result = ima_calc_file_hash(file, &hash.hdr);
  210. if (result && result != -EBADF && result != -EINVAL)
  211. goto out;
  212. length = sizeof(hash.hdr) + hash.hdr.length;
  213. tmpbuf = krealloc(iint->ima_hash, length, GFP_NOFS);
  214. if (!tmpbuf) {
  215. result = -ENOMEM;
  216. goto out;
  217. }
  218. iint->ima_hash = tmpbuf;
  219. memcpy(iint->ima_hash, &hash, length);
  220. iint->version = i_version;
  221. /* Possibly temporary failure due to type of read (eg. O_DIRECT) */
  222. if (!result)
  223. iint->flags |= IMA_COLLECTED;
  224. out:
  225. if (result) {
  226. if (file->f_flags & O_DIRECT)
  227. audit_cause = "failed(directio)";
  228. integrity_audit_msg(AUDIT_INTEGRITY_DATA, inode,
  229. filename, "collect_data", audit_cause,
  230. result, 0);
  231. }
  232. return result;
  233. }
  234. /*
  235. * ima_store_measurement - store file measurement
  236. *
  237. * Create an "ima" template and then store the template by calling
  238. * ima_store_template.
  239. *
  240. * We only get here if the inode has not already been measured,
  241. * but the measurement could already exist:
  242. * - multiple copies of the same file on either the same or
  243. * different filesystems.
  244. * - the inode was previously flushed as well as the iint info,
  245. * containing the hashing info.
  246. *
  247. * Must be called with iint->mutex held.
  248. */
  249. void ima_store_measurement(struct integrity_iint_cache *iint,
  250. struct file *file, const unsigned char *filename,
  251. struct evm_ima_xattr_data *xattr_value,
  252. int xattr_len, int pcr)
  253. {
  254. static const char op[] = "add_template_measure";
  255. static const char audit_cause[] = "ENOMEM";
  256. int result = -ENOMEM;
  257. struct inode *inode = file_inode(file);
  258. struct ima_template_entry *entry;
  259. struct ima_event_data event_data = {iint, file, filename, xattr_value,
  260. xattr_len, NULL};
  261. int violation = 0;
  262. if (iint->measured_pcrs & (0x1 << pcr))
  263. return;
  264. result = ima_alloc_init_template(&event_data, &entry);
  265. if (result < 0) {
  266. integrity_audit_msg(AUDIT_INTEGRITY_PCR, inode, filename,
  267. op, audit_cause, result, 0);
  268. return;
  269. }
  270. result = ima_store_template(entry, violation, inode, filename, pcr);
  271. if ((!result || result == -EEXIST) && !(file->f_flags & O_DIRECT)) {
  272. iint->flags |= IMA_MEASURED;
  273. iint->measured_pcrs |= (0x1 << pcr);
  274. }
  275. if (result < 0)
  276. ima_free_template_entry(entry);
  277. }
  278. void ima_audit_measurement(struct integrity_iint_cache *iint,
  279. const unsigned char *filename)
  280. {
  281. struct audit_buffer *ab;
  282. char *hash;
  283. const char *algo_name = hash_algo_name[iint->ima_hash->algo];
  284. int i;
  285. if (iint->flags & IMA_AUDITED)
  286. return;
  287. hash = kzalloc((iint->ima_hash->length * 2) + 1, GFP_KERNEL);
  288. if (!hash)
  289. return;
  290. for (i = 0; i < iint->ima_hash->length; i++)
  291. hex_byte_pack(hash + (i * 2), iint->ima_hash->digest[i]);
  292. hash[i * 2] = '\0';
  293. ab = audit_log_start(audit_context(), GFP_KERNEL,
  294. AUDIT_INTEGRITY_RULE);
  295. if (!ab)
  296. goto out;
  297. audit_log_format(ab, "file=");
  298. audit_log_untrustedstring(ab, filename);
  299. audit_log_format(ab, " hash=\"%s:%s\"", algo_name, hash);
  300. audit_log_task_info(ab, current);
  301. audit_log_end(ab);
  302. iint->flags |= IMA_AUDITED;
  303. out:
  304. kfree(hash);
  305. return;
  306. }
  307. /*
  308. * ima_d_path - return a pointer to the full pathname
  309. *
  310. * Attempt to return a pointer to the full pathname for use in the
  311. * IMA measurement list, IMA audit records, and auditing logs.
  312. *
  313. * On failure, return a pointer to a copy of the filename, not dname.
  314. * Returning a pointer to dname, could result in using the pointer
  315. * after the memory has been freed.
  316. */
  317. const char *ima_d_path(const struct path *path, char **pathbuf, char *namebuf)
  318. {
  319. char *pathname = NULL;
  320. *pathbuf = __getname();
  321. if (*pathbuf) {
  322. pathname = d_absolute_path(path, *pathbuf, PATH_MAX);
  323. if (IS_ERR(pathname)) {
  324. __putname(*pathbuf);
  325. *pathbuf = NULL;
  326. pathname = NULL;
  327. }
  328. }
  329. if (!pathname) {
  330. strlcpy(namebuf, path->dentry->d_name.name, NAME_MAX);
  331. pathname = namebuf;
  332. }
  333. return pathname;
  334. }