ima_main.c 8.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321
  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_DONE_MASK;
  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/audit/measurement policy,
  138. * returns IMA_MEASURE, IMA_APPRAISE, IMA_AUDIT bitmask. */
  139. action = ima_get_action(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. * IMA_AUDIT, IMA_AUDITED) */
  150. iint->flags |= action;
  151. action &= ~((iint->flags & IMA_DONE_MASK) >> 1);
  152. /* Nothing to do, just return existing appraised status */
  153. if (!action) {
  154. if (iint->flags & IMA_APPRAISED)
  155. rc = iint->ima_status;
  156. goto out;
  157. }
  158. rc = ima_collect_measurement(iint, file);
  159. if (rc != 0)
  160. goto out;
  161. if (function != BPRM_CHECK) {
  162. /* We will allow 11 spaces for ' (deleted)' to be appended */
  163. pathbuf = kmalloc(PATH_MAX + 11, GFP_KERNEL);
  164. if (pathbuf) {
  165. pathname =
  166. d_path(&file->f_path, pathbuf, PATH_MAX + 11);
  167. if (IS_ERR(pathname))
  168. pathname = NULL;
  169. }
  170. }
  171. if (action & IMA_MEASURE)
  172. ima_store_measurement(iint, file,
  173. !pathname ? filename : pathname);
  174. if (action & IMA_APPRAISE)
  175. rc = ima_appraise_measurement(iint, file,
  176. !pathname ? filename : pathname);
  177. if (action & IMA_AUDIT)
  178. ima_audit_measurement(iint, !pathname ? filename : pathname);
  179. kfree(pathbuf);
  180. out:
  181. mutex_unlock(&inode->i_mutex);
  182. return (rc && must_appraise) ? -EACCES : 0;
  183. }
  184. /**
  185. * ima_file_mmap - based on policy, collect/store measurement.
  186. * @file: pointer to the file to be measured (May be NULL)
  187. * @prot: contains the protection that will be applied by the kernel.
  188. *
  189. * Measure files being mmapped executable based on the ima_must_measure()
  190. * policy decision.
  191. *
  192. * Return 0 on success, an error code on failure.
  193. * (Based on the results of appraise_measurement().)
  194. */
  195. int ima_file_mmap(struct file *file, unsigned long prot)
  196. {
  197. int rc = 0;
  198. if (!file)
  199. return 0;
  200. if (prot & PROT_EXEC)
  201. rc = process_measurement(file, file->f_dentry->d_name.name,
  202. MAY_EXEC, FILE_MMAP);
  203. return (ima_appraise & IMA_APPRAISE_ENFORCE) ? rc : 0;
  204. }
  205. /**
  206. * ima_bprm_check - based on policy, collect/store measurement.
  207. * @bprm: contains the linux_binprm structure
  208. *
  209. * The OS protects against an executable file, already open for write,
  210. * from being executed in deny_write_access() and an executable file,
  211. * already open for execute, from being modified in get_write_access().
  212. * So we can be certain that what we verify and measure here is actually
  213. * what is being executed.
  214. *
  215. * Return 0 on success, an error code on failure.
  216. * (Based on the results of appraise_measurement().)
  217. */
  218. int ima_bprm_check(struct linux_binprm *bprm)
  219. {
  220. int rc;
  221. rc = process_measurement(bprm->file,
  222. (strcmp(bprm->filename, bprm->interp) == 0) ?
  223. bprm->filename : bprm->interp,
  224. MAY_EXEC, BPRM_CHECK);
  225. return (ima_appraise & IMA_APPRAISE_ENFORCE) ? rc : 0;
  226. }
  227. /**
  228. * ima_path_check - based on policy, collect/store measurement.
  229. * @file: pointer to the file to be measured
  230. * @mask: contains MAY_READ, MAY_WRITE or MAY_EXECUTE
  231. *
  232. * Measure files based on the ima_must_measure() policy decision.
  233. *
  234. * Always return 0 and audit dentry_open failures.
  235. * (Return code will be based upon measurement appraisal.)
  236. */
  237. int ima_file_check(struct file *file, int mask)
  238. {
  239. int rc;
  240. ima_rdwr_violation_check(file);
  241. rc = process_measurement(file, file->f_dentry->d_name.name,
  242. mask & (MAY_READ | MAY_WRITE | MAY_EXEC),
  243. FILE_CHECK);
  244. return (ima_appraise & IMA_APPRAISE_ENFORCE) ? rc : 0;
  245. }
  246. EXPORT_SYMBOL_GPL(ima_file_check);
  247. /**
  248. * ima_module_check - based on policy, collect/store/appraise measurement.
  249. * @file: pointer to the file to be measured/appraised
  250. *
  251. * Measure/appraise kernel modules based on policy.
  252. *
  253. * Always return 0 and audit dentry_open failures.
  254. * Return code is based upon measurement appraisal.
  255. */
  256. int ima_module_check(struct file *file)
  257. {
  258. int rc = 0;
  259. if (!file) {
  260. if (ima_appraise & IMA_APPRAISE_MODULES) {
  261. #ifndef CONFIG_MODULE_SIG_FORCE
  262. rc = -EACCES; /* INTEGRITY_UNKNOWN */
  263. #endif
  264. }
  265. } else
  266. rc = process_measurement(file, file->f_dentry->d_name.name,
  267. MAY_EXEC, MODULE_CHECK);
  268. return (ima_appraise & IMA_APPRAISE_ENFORCE) ? rc : 0;
  269. }
  270. static int __init init_ima(void)
  271. {
  272. int error;
  273. error = ima_init();
  274. if (!error)
  275. ima_initialized = 1;
  276. return error;
  277. }
  278. late_initcall(init_ima); /* Start IMA after the TPM is available */
  279. MODULE_DESCRIPTION("Integrity Measurement Architecture");
  280. MODULE_LICENSE("GPL");