ima_main.c 7.6 KB

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