ima_main.c 8.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310
  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_inode(file);
  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_inode(file);
  125. struct integrity_iint_cache *iint;
  126. char *pathbuf = NULL;
  127. const char *pathname = NULL;
  128. int rc = -ENOMEM, action, must_appraise, _func;
  129. if (!ima_initialized || !S_ISREG(inode->i_mode))
  130. return 0;
  131. /* Return an IMA_MEASURE, IMA_APPRAISE, IMA_AUDIT action
  132. * bitmask based on the appraise/audit/measurement policy.
  133. * Included is the appraise submask.
  134. */
  135. action = ima_get_action(inode, mask, function);
  136. if (!action)
  137. return 0;
  138. must_appraise = action & IMA_APPRAISE;
  139. /* Is the appraise rule hook specific? */
  140. _func = (action & IMA_FILE_APPRAISE) ? FILE_CHECK : function;
  141. mutex_lock(&inode->i_mutex);
  142. iint = integrity_inode_get(inode);
  143. if (!iint)
  144. goto out;
  145. /* Determine if already appraised/measured based on bitmask
  146. * (IMA_MEASURE, IMA_MEASURED, IMA_XXXX_APPRAISE, IMA_XXXX_APPRAISED,
  147. * IMA_AUDIT, IMA_AUDITED)
  148. */
  149. iint->flags |= action;
  150. action &= IMA_DO_MASK;
  151. action &= ~((iint->flags & IMA_DONE_MASK) >> 1);
  152. /* Nothing to do, just return existing appraised status */
  153. if (!action) {
  154. if (must_appraise)
  155. rc = ima_get_cache_status(iint, _func);
  156. goto out_digsig;
  157. }
  158. rc = ima_collect_measurement(iint, file);
  159. if (rc != 0)
  160. goto out_digsig;
  161. if (function != BPRM_CHECK)
  162. pathname = ima_d_path(&file->f_path, &pathbuf);
  163. if (!pathname)
  164. pathname = filename;
  165. if (action & IMA_MEASURE)
  166. ima_store_measurement(iint, file, pathname);
  167. if (action & IMA_APPRAISE_SUBMASK)
  168. rc = ima_appraise_measurement(_func, iint, file, pathname);
  169. if (action & IMA_AUDIT)
  170. ima_audit_measurement(iint, pathname);
  171. kfree(pathbuf);
  172. out_digsig:
  173. if ((mask & MAY_WRITE) && (iint->flags & IMA_DIGSIG))
  174. rc = -EACCES;
  175. out:
  176. mutex_unlock(&inode->i_mutex);
  177. if ((rc && must_appraise) && (ima_appraise & IMA_APPRAISE_ENFORCE))
  178. return -EACCES;
  179. return 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. * On success return 0. On integrity appraisal error, assuming the file
  190. * is in policy and IMA-appraisal is in enforcing mode, return -EACCES.
  191. */
  192. int ima_file_mmap(struct file *file, unsigned long prot)
  193. {
  194. if (file && (prot & PROT_EXEC))
  195. return process_measurement(file, file->f_dentry->d_name.name,
  196. MAY_EXEC, MMAP_CHECK);
  197. return 0;
  198. }
  199. /**
  200. * ima_bprm_check - based on policy, collect/store measurement.
  201. * @bprm: contains the linux_binprm structure
  202. *
  203. * The OS protects against an executable file, already open for write,
  204. * from being executed in deny_write_access() and an executable file,
  205. * already open for execute, from being modified in get_write_access().
  206. * So we can be certain that what we verify and measure here is actually
  207. * what is being executed.
  208. *
  209. * On success return 0. On integrity appraisal error, assuming the file
  210. * is in policy and IMA-appraisal is in enforcing mode, return -EACCES.
  211. */
  212. int ima_bprm_check(struct linux_binprm *bprm)
  213. {
  214. return process_measurement(bprm->file,
  215. (strcmp(bprm->filename, bprm->interp) == 0) ?
  216. bprm->filename : bprm->interp,
  217. MAY_EXEC, BPRM_CHECK);
  218. }
  219. /**
  220. * ima_path_check - based on policy, collect/store measurement.
  221. * @file: pointer to the file to be measured
  222. * @mask: contains MAY_READ, MAY_WRITE or MAY_EXECUTE
  223. *
  224. * Measure files based on the ima_must_measure() policy decision.
  225. *
  226. * On success return 0. On integrity appraisal error, assuming the file
  227. * is in policy and IMA-appraisal is in enforcing mode, return -EACCES.
  228. */
  229. int ima_file_check(struct file *file, int mask)
  230. {
  231. ima_rdwr_violation_check(file);
  232. return process_measurement(file, file->f_dentry->d_name.name,
  233. mask & (MAY_READ | MAY_WRITE | MAY_EXEC),
  234. FILE_CHECK);
  235. }
  236. EXPORT_SYMBOL_GPL(ima_file_check);
  237. /**
  238. * ima_module_check - based on policy, collect/store/appraise measurement.
  239. * @file: pointer to the file to be measured/appraised
  240. *
  241. * Measure/appraise kernel modules based on policy.
  242. *
  243. * On success return 0. On integrity appraisal error, assuming the file
  244. * is in policy and IMA-appraisal is in enforcing mode, return -EACCES.
  245. */
  246. int ima_module_check(struct file *file)
  247. {
  248. if (!file) {
  249. #ifndef CONFIG_MODULE_SIG_FORCE
  250. if ((ima_appraise & IMA_APPRAISE_MODULES) &&
  251. (ima_appraise & IMA_APPRAISE_ENFORCE))
  252. return -EACCES; /* INTEGRITY_UNKNOWN */
  253. #endif
  254. return 0; /* We rely on module signature checking */
  255. }
  256. return process_measurement(file, file->f_dentry->d_name.name,
  257. MAY_EXEC, MODULE_CHECK);
  258. }
  259. static int __init init_ima(void)
  260. {
  261. int error;
  262. error = ima_init();
  263. if (!error)
  264. ima_initialized = 1;
  265. return error;
  266. }
  267. late_initcall(init_ima); /* Start IMA after the TPM is available */
  268. MODULE_DESCRIPTION("Integrity Measurement Architecture");
  269. MODULE_LICENSE("GPL");