ima_main.c 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294
  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. char *pathbuf = NULL;
  60. const char *pathname;
  61. if (!S_ISREG(inode->i_mode) || !ima_initialized)
  62. return;
  63. mutex_lock(&inode->i_mutex); /* file metadata: permissions, xattr */
  64. if (mode & FMODE_WRITE) {
  65. if (atomic_read(&inode->i_readcount) && IS_IMA(inode))
  66. send_tomtou = true;
  67. goto out;
  68. }
  69. must_measure = ima_must_measure(inode, MAY_READ, FILE_CHECK);
  70. if (!must_measure)
  71. goto out;
  72. if (atomic_read(&inode->i_writecount) > 0)
  73. send_writers = true;
  74. out:
  75. mutex_unlock(&inode->i_mutex);
  76. if (!send_tomtou && !send_writers)
  77. return;
  78. pathname = ima_d_path(&file->f_path, &pathbuf);
  79. if (!pathname || strlen(pathname) > IMA_EVENT_NAME_LEN_MAX)
  80. pathname = dentry->d_name.name;
  81. if (send_tomtou)
  82. ima_add_violation(inode, pathname,
  83. "invalid_pcr", "ToMToU");
  84. if (send_writers)
  85. ima_add_violation(inode, pathname,
  86. "invalid_pcr", "open_writers");
  87. kfree(pathbuf);
  88. }
  89. static void ima_check_last_writer(struct integrity_iint_cache *iint,
  90. struct inode *inode, struct file *file)
  91. {
  92. fmode_t mode = file->f_mode;
  93. if (!(mode & FMODE_WRITE))
  94. return;
  95. mutex_lock(&inode->i_mutex);
  96. if (atomic_read(&inode->i_writecount) == 1 &&
  97. iint->version != inode->i_version) {
  98. iint->flags &= ~IMA_DONE_MASK;
  99. if (iint->flags & IMA_APPRAISE)
  100. ima_update_xattr(iint, file);
  101. }
  102. mutex_unlock(&inode->i_mutex);
  103. }
  104. /**
  105. * ima_file_free - called on __fput()
  106. * @file: pointer to file structure being freed
  107. *
  108. * Flag files that changed, based on i_version
  109. */
  110. void ima_file_free(struct file *file)
  111. {
  112. struct inode *inode = file->f_dentry->d_inode;
  113. struct integrity_iint_cache *iint;
  114. if (!iint_initialized || !S_ISREG(inode->i_mode))
  115. return;
  116. iint = integrity_iint_find(inode);
  117. if (!iint)
  118. return;
  119. ima_check_last_writer(iint, inode, file);
  120. }
  121. static int process_measurement(struct file *file, const char *filename,
  122. int mask, int function)
  123. {
  124. struct inode *inode = file->f_dentry->d_inode;
  125. struct integrity_iint_cache *iint;
  126. char *pathbuf = NULL;
  127. const char *pathname = NULL;
  128. int rc = -ENOMEM, action, must_appraise;
  129. if (!ima_initialized || !S_ISREG(inode->i_mode))
  130. return 0;
  131. /* Determine if in appraise/audit/measurement policy,
  132. * returns IMA_MEASURE, IMA_APPRAISE, IMA_AUDIT bitmask. */
  133. action = ima_get_action(inode, mask, function);
  134. if (!action)
  135. return 0;
  136. must_appraise = action & IMA_APPRAISE;
  137. mutex_lock(&inode->i_mutex);
  138. iint = integrity_inode_get(inode);
  139. if (!iint)
  140. goto out;
  141. /* Determine if already appraised/measured based on bitmask
  142. * (IMA_MEASURE, IMA_MEASURED, IMA_APPRAISE, IMA_APPRAISED,
  143. * IMA_AUDIT, IMA_AUDITED) */
  144. iint->flags |= action;
  145. action &= ~((iint->flags & IMA_DONE_MASK) >> 1);
  146. /* Nothing to do, just return existing appraised status */
  147. if (!action) {
  148. if (iint->flags & IMA_APPRAISED)
  149. rc = iint->ima_status;
  150. goto out;
  151. }
  152. rc = ima_collect_measurement(iint, file);
  153. if (rc != 0)
  154. goto out;
  155. if (function != BPRM_CHECK)
  156. pathname = ima_d_path(&file->f_path, &pathbuf);
  157. if (!pathname)
  158. pathname = filename;
  159. if (action & IMA_MEASURE)
  160. ima_store_measurement(iint, file, pathname);
  161. if (action & IMA_APPRAISE)
  162. rc = ima_appraise_measurement(iint, file, pathname);
  163. if (action & IMA_AUDIT)
  164. ima_audit_measurement(iint, pathname);
  165. kfree(pathbuf);
  166. out:
  167. mutex_unlock(&inode->i_mutex);
  168. if ((rc && must_appraise) && (ima_appraise & IMA_APPRAISE_ENFORCE))
  169. return -EACCES;
  170. return 0;
  171. }
  172. /**
  173. * ima_file_mmap - based on policy, collect/store measurement.
  174. * @file: pointer to the file to be measured (May be NULL)
  175. * @prot: contains the protection that will be applied by the kernel.
  176. *
  177. * Measure files being mmapped executable based on the ima_must_measure()
  178. * policy decision.
  179. *
  180. * On success return 0. On integrity appraisal error, assuming the file
  181. * is in policy and IMA-appraisal is in enforcing mode, return -EACCES.
  182. */
  183. int ima_file_mmap(struct file *file, unsigned long prot)
  184. {
  185. if (file && (prot & PROT_EXEC))
  186. return process_measurement(file, file->f_dentry->d_name.name,
  187. MAY_EXEC, MMAP_CHECK);
  188. return 0;
  189. }
  190. /**
  191. * ima_bprm_check - based on policy, collect/store measurement.
  192. * @bprm: contains the linux_binprm structure
  193. *
  194. * The OS protects against an executable file, already open for write,
  195. * from being executed in deny_write_access() and an executable file,
  196. * already open for execute, from being modified in get_write_access().
  197. * So we can be certain that what we verify and measure here is actually
  198. * what is being executed.
  199. *
  200. * On success return 0. On integrity appraisal error, assuming the file
  201. * is in policy and IMA-appraisal is in enforcing mode, return -EACCES.
  202. */
  203. int ima_bprm_check(struct linux_binprm *bprm)
  204. {
  205. return process_measurement(bprm->file,
  206. (strcmp(bprm->filename, bprm->interp) == 0) ?
  207. bprm->filename : bprm->interp,
  208. MAY_EXEC, BPRM_CHECK);
  209. }
  210. /**
  211. * ima_path_check - based on policy, collect/store measurement.
  212. * @file: pointer to the file to be measured
  213. * @mask: contains MAY_READ, MAY_WRITE or MAY_EXECUTE
  214. *
  215. * Measure files based on the ima_must_measure() policy decision.
  216. *
  217. * On success return 0. On integrity appraisal error, assuming the file
  218. * is in policy and IMA-appraisal is in enforcing mode, return -EACCES.
  219. */
  220. int ima_file_check(struct file *file, int mask)
  221. {
  222. ima_rdwr_violation_check(file);
  223. return process_measurement(file, file->f_dentry->d_name.name,
  224. mask & (MAY_READ | MAY_WRITE | MAY_EXEC),
  225. FILE_CHECK);
  226. }
  227. EXPORT_SYMBOL_GPL(ima_file_check);
  228. /**
  229. * ima_module_check - based on policy, collect/store/appraise measurement.
  230. * @file: pointer to the file to be measured/appraised
  231. *
  232. * Measure/appraise kernel modules based on policy.
  233. *
  234. * On success return 0. On integrity appraisal error, assuming the file
  235. * is in policy and IMA-appraisal is in enforcing mode, return -EACCES.
  236. */
  237. int ima_module_check(struct file *file)
  238. {
  239. if (!file)
  240. return -EACCES; /* INTEGRITY_UNKNOWN */
  241. return process_measurement(file, file->f_dentry->d_name.name,
  242. MAY_EXEC, MODULE_CHECK);
  243. }
  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");