ima_main.c 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241
  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. if (!S_ISREG(inode->i_mode) || !ima_initialized)
  54. return;
  55. mutex_lock(&inode->i_mutex); /* file metadata: permissions, xattr */
  56. if (mode & FMODE_WRITE) {
  57. if (atomic_read(&inode->i_readcount) && IS_IMA(inode))
  58. send_tomtou = true;
  59. goto out;
  60. }
  61. rc = ima_must_measure(inode, MAY_READ, FILE_CHECK);
  62. if (rc < 0)
  63. goto out;
  64. if (atomic_read(&inode->i_writecount) > 0)
  65. send_writers = true;
  66. out:
  67. mutex_unlock(&inode->i_mutex);
  68. if (send_tomtou)
  69. ima_add_violation(inode, dentry->d_name.name, "invalid_pcr",
  70. "ToMToU");
  71. if (send_writers)
  72. ima_add_violation(inode, dentry->d_name.name, "invalid_pcr",
  73. "open_writers");
  74. }
  75. static void ima_check_last_writer(struct integrity_iint_cache *iint,
  76. struct inode *inode,
  77. struct file *file)
  78. {
  79. fmode_t mode = file->f_mode;
  80. mutex_lock(&iint->mutex);
  81. if (mode & FMODE_WRITE &&
  82. atomic_read(&inode->i_writecount) == 1 &&
  83. iint->version != inode->i_version)
  84. iint->flags &= ~IMA_MEASURED;
  85. mutex_unlock(&iint->mutex);
  86. }
  87. /**
  88. * ima_file_free - called on __fput()
  89. * @file: pointer to file structure being freed
  90. *
  91. * Flag files that changed, based on i_version
  92. */
  93. void ima_file_free(struct file *file)
  94. {
  95. struct inode *inode = file->f_dentry->d_inode;
  96. struct integrity_iint_cache *iint;
  97. if (!iint_initialized || !S_ISREG(inode->i_mode))
  98. return;
  99. iint = integrity_iint_find(inode);
  100. if (!iint)
  101. return;
  102. ima_check_last_writer(iint, inode, file);
  103. }
  104. static int process_measurement(struct file *file, const unsigned char *filename,
  105. int mask, int function)
  106. {
  107. struct inode *inode = file->f_dentry->d_inode;
  108. struct integrity_iint_cache *iint;
  109. int rc = 0;
  110. if (!ima_initialized || !S_ISREG(inode->i_mode))
  111. return 0;
  112. rc = ima_must_measure(inode, mask, function);
  113. if (rc != 0)
  114. return rc;
  115. retry:
  116. iint = integrity_iint_find(inode);
  117. if (!iint) {
  118. rc = integrity_inode_alloc(inode);
  119. if (!rc || rc == -EEXIST)
  120. goto retry;
  121. return rc;
  122. }
  123. mutex_lock(&iint->mutex);
  124. rc = iint->flags & IMA_MEASURED ? 1 : 0;
  125. if (rc != 0)
  126. goto out;
  127. rc = ima_collect_measurement(iint, file);
  128. if (!rc)
  129. ima_store_measurement(iint, file, filename);
  130. out:
  131. mutex_unlock(&iint->mutex);
  132. return rc;
  133. }
  134. /**
  135. * ima_file_mmap - based on policy, collect/store measurement.
  136. * @file: pointer to the file to be measured (May be NULL)
  137. * @prot: contains the protection that will be applied by the kernel.
  138. *
  139. * Measure files being mmapped executable based on the ima_must_measure()
  140. * policy decision.
  141. *
  142. * Return 0 on success, an error code on failure.
  143. * (Based on the results of appraise_measurement().)
  144. */
  145. int ima_file_mmap(struct file *file, unsigned long prot)
  146. {
  147. int rc;
  148. if (!file)
  149. return 0;
  150. if (prot & PROT_EXEC)
  151. rc = process_measurement(file, file->f_dentry->d_name.name,
  152. MAY_EXEC, FILE_MMAP);
  153. return 0;
  154. }
  155. /**
  156. * ima_bprm_check - based on policy, collect/store measurement.
  157. * @bprm: contains the linux_binprm structure
  158. *
  159. * The OS protects against an executable file, already open for write,
  160. * from being executed in deny_write_access() and an executable file,
  161. * already open for execute, from being modified in get_write_access().
  162. * So we can be certain that what we verify and measure here is actually
  163. * what is being executed.
  164. *
  165. * Return 0 on success, an error code on failure.
  166. * (Based on the results of appraise_measurement().)
  167. */
  168. int ima_bprm_check(struct linux_binprm *bprm)
  169. {
  170. int rc;
  171. rc = process_measurement(bprm->file, bprm->filename,
  172. MAY_EXEC, BPRM_CHECK);
  173. return 0;
  174. }
  175. /**
  176. * ima_path_check - based on policy, collect/store measurement.
  177. * @file: pointer to the file to be measured
  178. * @mask: contains MAY_READ, MAY_WRITE or MAY_EXECUTE
  179. *
  180. * Measure files based on the ima_must_measure() policy decision.
  181. *
  182. * Always return 0 and audit dentry_open failures.
  183. * (Return code will be based upon measurement appraisal.)
  184. */
  185. int ima_file_check(struct file *file, int mask)
  186. {
  187. int rc;
  188. ima_rdwr_violation_check(file);
  189. rc = process_measurement(file, file->f_dentry->d_name.name,
  190. mask & (MAY_READ | MAY_WRITE | MAY_EXEC),
  191. FILE_CHECK);
  192. return 0;
  193. }
  194. EXPORT_SYMBOL_GPL(ima_file_check);
  195. static int __init init_ima(void)
  196. {
  197. int error;
  198. error = ima_init();
  199. ima_initialized = 1;
  200. return error;
  201. }
  202. static void __exit cleanup_ima(void)
  203. {
  204. ima_cleanup();
  205. }
  206. late_initcall(init_ima); /* Start IMA after the TPM is available */
  207. MODULE_DESCRIPTION("Integrity Measurement Architecture");
  208. MODULE_LICENSE("GPL");