ima_main.c 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269
  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_file_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 <linux/slab.h>
  25. #include <linux/ima.h>
  26. #include "ima.h"
  27. int ima_initialized;
  28. char *ima_hash = "sha1";
  29. static int __init hash_setup(char *str)
  30. {
  31. if (strncmp(str, "md5", 3) == 0)
  32. ima_hash = "md5";
  33. return 1;
  34. }
  35. __setup("ima_hash=", hash_setup);
  36. /*
  37. * ima_rdwr_violation_check
  38. *
  39. * Only invalidate the PCR for measured files:
  40. * - Opening a file for write when already open for read,
  41. * results in a time of measure, time of use (ToMToU) error.
  42. * - Opening a file for read when already open for write,
  43. * could result in a file measurement error.
  44. *
  45. */
  46. static void ima_rdwr_violation_check(struct file *file)
  47. {
  48. struct dentry *dentry = file->f_path.dentry;
  49. struct inode *inode = dentry->d_inode;
  50. fmode_t mode = file->f_mode;
  51. int rc;
  52. bool send_tomtou = false, send_writers = false;
  53. unsigned char *pathname = NULL, *pathbuf = NULL;
  54. if (!S_ISREG(inode->i_mode) || !ima_initialized)
  55. return;
  56. mutex_lock(&inode->i_mutex); /* file metadata: permissions, xattr */
  57. if (mode & FMODE_WRITE) {
  58. if (atomic_read(&inode->i_readcount) && IS_IMA(inode))
  59. send_tomtou = true;
  60. goto out;
  61. }
  62. rc = ima_must_measure(inode, MAY_READ, FILE_CHECK);
  63. if (rc < 0)
  64. goto out;
  65. if (atomic_read(&inode->i_writecount) > 0)
  66. send_writers = true;
  67. out:
  68. mutex_unlock(&inode->i_mutex);
  69. if (!send_tomtou && !send_writers)
  70. return;
  71. /* We will allow 11 spaces for ' (deleted)' to be appended */
  72. pathbuf = kmalloc(PATH_MAX + 11, GFP_KERNEL);
  73. if (pathbuf) {
  74. pathname = d_path(&file->f_path, pathbuf, PATH_MAX + 11);
  75. if (IS_ERR(pathname))
  76. pathname = NULL;
  77. else if (strlen(pathname) > IMA_EVENT_NAME_LEN_MAX)
  78. pathname = NULL;
  79. }
  80. if (send_tomtou)
  81. ima_add_violation(inode,
  82. !pathname ? dentry->d_name.name : pathname,
  83. "invalid_pcr", "ToMToU");
  84. if (send_writers)
  85. ima_add_violation(inode,
  86. !pathname ? dentry->d_name.name : pathname,
  87. "invalid_pcr", "open_writers");
  88. kfree(pathbuf);
  89. }
  90. static void ima_check_last_writer(struct integrity_iint_cache *iint,
  91. struct inode *inode,
  92. struct file *file)
  93. {
  94. fmode_t mode = file->f_mode;
  95. mutex_lock(&iint->mutex);
  96. if (mode & FMODE_WRITE &&
  97. atomic_read(&inode->i_writecount) == 1 &&
  98. iint->version != inode->i_version)
  99. iint->flags &= ~IMA_MEASURED;
  100. mutex_unlock(&iint->mutex);
  101. }
  102. /**
  103. * ima_file_free - called on __fput()
  104. * @file: pointer to file structure being freed
  105. *
  106. * Flag files that changed, based on i_version
  107. */
  108. void ima_file_free(struct file *file)
  109. {
  110. struct inode *inode = file->f_dentry->d_inode;
  111. struct integrity_iint_cache *iint;
  112. if (!iint_initialized || !S_ISREG(inode->i_mode))
  113. return;
  114. iint = integrity_iint_find(inode);
  115. if (!iint)
  116. return;
  117. ima_check_last_writer(iint, inode, file);
  118. }
  119. static int process_measurement(struct file *file, const unsigned char *filename,
  120. int mask, int function)
  121. {
  122. struct inode *inode = file->f_dentry->d_inode;
  123. struct integrity_iint_cache *iint;
  124. unsigned char *pathname = NULL, *pathbuf = NULL;
  125. int rc = 0;
  126. if (!ima_initialized || !S_ISREG(inode->i_mode))
  127. return 0;
  128. rc = ima_must_measure(inode, mask, function);
  129. if (rc != 0)
  130. return rc;
  131. retry:
  132. iint = integrity_iint_find(inode);
  133. if (!iint) {
  134. rc = integrity_inode_alloc(inode);
  135. if (!rc || rc == -EEXIST)
  136. goto retry;
  137. return rc;
  138. }
  139. mutex_lock(&iint->mutex);
  140. rc = iint->flags & IMA_MEASURED ? 1 : 0;
  141. if (rc != 0)
  142. goto out;
  143. rc = ima_collect_measurement(iint, file);
  144. if (rc != 0)
  145. goto out;
  146. if (function != BPRM_CHECK) {
  147. /* We will allow 11 spaces for ' (deleted)' to be appended */
  148. pathbuf = kmalloc(PATH_MAX + 11, GFP_KERNEL);
  149. if (pathbuf) {
  150. pathname =
  151. d_path(&file->f_path, pathbuf, PATH_MAX + 11);
  152. if (IS_ERR(pathname))
  153. pathname = NULL;
  154. }
  155. }
  156. ima_store_measurement(iint, file, !pathname ? filename : pathname);
  157. kfree(pathbuf);
  158. out:
  159. mutex_unlock(&iint->mutex);
  160. return rc;
  161. }
  162. /**
  163. * ima_file_mmap - based on policy, collect/store measurement.
  164. * @file: pointer to the file to be measured (May be NULL)
  165. * @prot: contains the protection that will be applied by the kernel.
  166. *
  167. * Measure files being mmapped executable based on the ima_must_measure()
  168. * policy decision.
  169. *
  170. * Return 0 on success, an error code on failure.
  171. * (Based on the results of appraise_measurement().)
  172. */
  173. int ima_file_mmap(struct file *file, unsigned long prot)
  174. {
  175. int rc;
  176. if (!file)
  177. return 0;
  178. if (prot & PROT_EXEC)
  179. rc = process_measurement(file, file->f_dentry->d_name.name,
  180. MAY_EXEC, FILE_MMAP);
  181. return 0;
  182. }
  183. /**
  184. * ima_bprm_check - based on policy, collect/store measurement.
  185. * @bprm: contains the linux_binprm structure
  186. *
  187. * The OS protects against an executable file, already open for write,
  188. * from being executed in deny_write_access() and an executable file,
  189. * already open for execute, from being modified in get_write_access().
  190. * So we can be certain that what we verify and measure here is actually
  191. * what is being executed.
  192. *
  193. * Return 0 on success, an error code on failure.
  194. * (Based on the results of appraise_measurement().)
  195. */
  196. int ima_bprm_check(struct linux_binprm *bprm)
  197. {
  198. int rc;
  199. rc = process_measurement(bprm->file,
  200. (strcmp(bprm->filename, bprm->interp) == 0) ?
  201. bprm->filename : bprm->interp,
  202. MAY_EXEC, BPRM_CHECK);
  203. return 0;
  204. }
  205. /**
  206. * ima_path_check - based on policy, collect/store measurement.
  207. * @file: pointer to the file to be measured
  208. * @mask: contains MAY_READ, MAY_WRITE or MAY_EXECUTE
  209. *
  210. * Measure files based on the ima_must_measure() policy decision.
  211. *
  212. * Always return 0 and audit dentry_open failures.
  213. * (Return code will be based upon measurement appraisal.)
  214. */
  215. int ima_file_check(struct file *file, int mask)
  216. {
  217. int rc;
  218. ima_rdwr_violation_check(file);
  219. rc = process_measurement(file, file->f_dentry->d_name.name,
  220. mask & (MAY_READ | MAY_WRITE | MAY_EXEC),
  221. FILE_CHECK);
  222. return 0;
  223. }
  224. EXPORT_SYMBOL_GPL(ima_file_check);
  225. static int __init init_ima(void)
  226. {
  227. int error;
  228. error = ima_init();
  229. if (!error)
  230. ima_initialized = 1;
  231. return error;
  232. }
  233. late_initcall(init_ima); /* Start IMA after the TPM is available */
  234. MODULE_DESCRIPTION("Integrity Measurement Architecture");
  235. MODULE_LICENSE("GPL");