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