ima_main.c 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285
  1. /*
  2. * Copyright (C) 2005,2006,2007,2008 IBM Corporation
  3. *
  4. * Authors:
  5. * Reiner Sailer <sailer@watson.ibm.com>
  6. * Serge Hallyn <serue@us.ibm.com>
  7. * Kylene Hall <kylene@us.ibm.com>
  8. * Mimi Zohar <zohar@us.ibm.com>
  9. *
  10. * This program is free software; you can redistribute it and/or
  11. * modify it under the terms of the GNU General Public License as
  12. * published by the Free Software Foundation, version 2 of the
  13. * License.
  14. *
  15. * File: ima_main.c
  16. * implements the IMA hooks: ima_bprm_check, ima_file_mmap,
  17. * and ima_path_check.
  18. */
  19. #include <linux/module.h>
  20. #include <linux/file.h>
  21. #include <linux/binfmts.h>
  22. #include <linux/mount.h>
  23. #include <linux/mman.h>
  24. #include "ima.h"
  25. int ima_initialized;
  26. char *ima_hash = "sha1";
  27. static int __init hash_setup(char *str)
  28. {
  29. const char *op = "hash_setup";
  30. const char *hash = "sha1";
  31. int result = 0;
  32. int audit_info = 0;
  33. if (strncmp(str, "md5", 3) == 0) {
  34. hash = "md5";
  35. ima_hash = str;
  36. } else if (strncmp(str, "sha1", 4) != 0) {
  37. hash = "invalid_hash_type";
  38. result = 1;
  39. }
  40. integrity_audit_msg(AUDIT_INTEGRITY_HASH, NULL, NULL, op, hash,
  41. result, audit_info);
  42. return 1;
  43. }
  44. __setup("ima_hash=", hash_setup);
  45. /**
  46. * ima_file_free - called on __fput()
  47. * @file: pointer to file structure being freed
  48. *
  49. * Flag files that changed, based on i_version;
  50. * and decrement the iint readcount/writecount.
  51. */
  52. void ima_file_free(struct file *file)
  53. {
  54. struct inode *inode = file->f_dentry->d_inode;
  55. struct ima_iint_cache *iint;
  56. if (!ima_initialized || !S_ISREG(inode->i_mode))
  57. return;
  58. iint = ima_iint_find_get(inode);
  59. if (!iint)
  60. return;
  61. mutex_lock(&iint->mutex);
  62. if ((file->f_mode & (FMODE_READ | FMODE_WRITE)) == FMODE_READ)
  63. iint->readcount--;
  64. if (file->f_mode & FMODE_WRITE) {
  65. iint->writecount--;
  66. if (iint->writecount == 0) {
  67. if (iint->version != inode->i_version)
  68. iint->flags &= ~IMA_MEASURED;
  69. }
  70. }
  71. mutex_unlock(&iint->mutex);
  72. kref_put(&iint->refcount, iint_free);
  73. }
  74. /* ima_read_write_check - reflect possible reading/writing errors in the PCR.
  75. *
  76. * When opening a file for read, if the file is already open for write,
  77. * the file could change, resulting in a file measurement error.
  78. *
  79. * Opening a file for write, if the file is already open for read, results
  80. * in a time of measure, time of use (ToMToU) error.
  81. *
  82. * In either case invalidate the PCR.
  83. */
  84. enum iint_pcr_error { TOMTOU, OPEN_WRITERS };
  85. static void ima_read_write_check(enum iint_pcr_error error,
  86. struct ima_iint_cache *iint,
  87. struct inode *inode,
  88. const unsigned char *filename)
  89. {
  90. switch (error) {
  91. case TOMTOU:
  92. if (iint->readcount > 0)
  93. ima_add_violation(inode, filename, "invalid_pcr",
  94. "ToMToU");
  95. break;
  96. case OPEN_WRITERS:
  97. if (iint->writecount > 0)
  98. ima_add_violation(inode, filename, "invalid_pcr",
  99. "open_writers");
  100. break;
  101. }
  102. }
  103. static int get_path_measurement(struct ima_iint_cache *iint, struct file *file,
  104. const unsigned char *filename)
  105. {
  106. int rc = 0;
  107. if (IS_ERR(file)) {
  108. pr_info("%s dentry_open failed\n", filename);
  109. return rc;
  110. }
  111. iint->readcount++;
  112. rc = ima_collect_measurement(iint, file);
  113. if (!rc)
  114. ima_store_measurement(iint, file, filename);
  115. return rc;
  116. }
  117. /**
  118. * ima_path_check - based on policy, collect/store measurement.
  119. * @path: contains a pointer to the path to be measured
  120. * @mask: contains MAY_READ, MAY_WRITE or MAY_EXECUTE
  121. *
  122. * Measure the file being open for readonly, based on the
  123. * ima_must_measure() policy decision.
  124. *
  125. * Keep read/write counters for all files, but only
  126. * invalidate the PCR for measured files:
  127. * - Opening a file for write when already open for read,
  128. * results in a time of measure, time of use (ToMToU) error.
  129. * - Opening a file for read when already open for write,
  130. * could result in a file measurement error.
  131. *
  132. * Return 0 on success, an error code on failure.
  133. * (Based on the results of appraise_measurement().)
  134. */
  135. int ima_path_check(struct path *path, int mask)
  136. {
  137. struct inode *inode = path->dentry->d_inode;
  138. struct ima_iint_cache *iint;
  139. struct file *file = NULL;
  140. int rc;
  141. if (!ima_initialized || !S_ISREG(inode->i_mode))
  142. return 0;
  143. iint = ima_iint_find_insert_get(inode);
  144. if (!iint)
  145. return 0;
  146. mutex_lock(&iint->mutex);
  147. if ((mask & MAY_WRITE) || (mask == 0))
  148. iint->writecount++;
  149. else if (mask & (MAY_READ | MAY_EXEC))
  150. iint->readcount++;
  151. rc = ima_must_measure(iint, inode, MAY_READ, PATH_CHECK);
  152. if (rc < 0)
  153. goto out;
  154. if ((mask & MAY_WRITE) || (mask == 0))
  155. ima_read_write_check(TOMTOU, iint, inode,
  156. path->dentry->d_name.name);
  157. if ((mask & (MAY_WRITE | MAY_READ | MAY_EXEC)) != MAY_READ)
  158. goto out;
  159. ima_read_write_check(OPEN_WRITERS, iint, inode,
  160. path->dentry->d_name.name);
  161. if (!(iint->flags & IMA_MEASURED)) {
  162. struct dentry *dentry = dget(path->dentry);
  163. struct vfsmount *mnt = mntget(path->mnt);
  164. file = dentry_open(dentry, mnt, O_RDONLY, current->cred);
  165. rc = get_path_measurement(iint, file, dentry->d_name.name);
  166. }
  167. out:
  168. mutex_unlock(&iint->mutex);
  169. if (file)
  170. fput(file);
  171. kref_put(&iint->refcount, iint_free);
  172. return 0;
  173. }
  174. static int process_measurement(struct file *file, const unsigned char *filename,
  175. int mask, int function)
  176. {
  177. struct inode *inode = file->f_dentry->d_inode;
  178. struct ima_iint_cache *iint;
  179. int rc;
  180. if (!ima_initialized || !S_ISREG(inode->i_mode))
  181. return 0;
  182. iint = ima_iint_find_insert_get(inode);
  183. if (!iint)
  184. return -ENOMEM;
  185. mutex_lock(&iint->mutex);
  186. rc = ima_must_measure(iint, inode, mask, function);
  187. if (rc != 0)
  188. goto out;
  189. rc = ima_collect_measurement(iint, file);
  190. if (!rc)
  191. ima_store_measurement(iint, file, filename);
  192. out:
  193. mutex_unlock(&iint->mutex);
  194. kref_put(&iint->refcount, iint_free);
  195. return rc;
  196. }
  197. /**
  198. * ima_file_mmap - based on policy, collect/store measurement.
  199. * @file: pointer to the file to be measured (May be NULL)
  200. * @prot: contains the protection that will be applied by the kernel.
  201. *
  202. * Measure files being mmapped executable based on the ima_must_measure()
  203. * policy decision.
  204. *
  205. * Return 0 on success, an error code on failure.
  206. * (Based on the results of appraise_measurement().)
  207. */
  208. int ima_file_mmap(struct file *file, unsigned long prot)
  209. {
  210. int rc;
  211. if (!file)
  212. return 0;
  213. if (prot & PROT_EXEC)
  214. rc = process_measurement(file, file->f_dentry->d_name.name,
  215. MAY_EXEC, FILE_MMAP);
  216. return 0;
  217. }
  218. /**
  219. * ima_bprm_check - based on policy, collect/store measurement.
  220. * @bprm: contains the linux_binprm structure
  221. *
  222. * The OS protects against an executable file, already open for write,
  223. * from being executed in deny_write_access() and an executable file,
  224. * already open for execute, from being modified in get_write_access().
  225. * So we can be certain that what we verify and measure here is actually
  226. * what is being executed.
  227. *
  228. * Return 0 on success, an error code on failure.
  229. * (Based on the results of appraise_measurement().)
  230. */
  231. int ima_bprm_check(struct linux_binprm *bprm)
  232. {
  233. int rc;
  234. rc = process_measurement(bprm->file, bprm->filename,
  235. MAY_EXEC, BPRM_CHECK);
  236. return 0;
  237. }
  238. static int __init init_ima(void)
  239. {
  240. int error;
  241. ima_iintcache_init();
  242. error = ima_init();
  243. ima_initialized = 1;
  244. return error;
  245. }
  246. static void __exit cleanup_ima(void)
  247. {
  248. ima_cleanup();
  249. }
  250. late_initcall(init_ima); /* Start IMA after the TPM is available */
  251. MODULE_DESCRIPTION("Integrity Measurement Architecture");
  252. MODULE_LICENSE("GPL");