ima_main.c 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317
  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_path_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 "ima.h"
  25. int ima_initialized;
  26. char *ima_hash = "sha1";
  27. static int __init hash_setup(char *str)
  28. {
  29. if (strncmp(str, "md5", 3) == 0)
  30. ima_hash = "md5";
  31. return 1;
  32. }
  33. __setup("ima_hash=", hash_setup);
  34. /**
  35. * ima_file_free - called on __fput()
  36. * @file: pointer to file structure being freed
  37. *
  38. * Flag files that changed, based on i_version;
  39. * and decrement the iint readcount/writecount.
  40. */
  41. void ima_file_free(struct file *file)
  42. {
  43. struct inode *inode = file->f_dentry->d_inode;
  44. struct ima_iint_cache *iint;
  45. if (!ima_initialized || !S_ISREG(inode->i_mode))
  46. return;
  47. iint = ima_iint_find_get(inode);
  48. if (!iint)
  49. return;
  50. mutex_lock(&iint->mutex);
  51. if (iint->opencount <= 0) {
  52. printk(KERN_INFO
  53. "%s: %s open/free imbalance (r:%ld w:%ld o:%ld f:%ld)\n",
  54. __FUNCTION__, file->f_dentry->d_name.name,
  55. iint->readcount, iint->writecount,
  56. iint->opencount, atomic_long_read(&file->f_count));
  57. if (!(iint->flags & IMA_IINT_DUMP_STACK)) {
  58. dump_stack();
  59. iint->flags |= IMA_IINT_DUMP_STACK;
  60. }
  61. }
  62. iint->opencount--;
  63. if ((file->f_mode & (FMODE_READ | FMODE_WRITE)) == FMODE_READ)
  64. iint->readcount--;
  65. if (file->f_mode & FMODE_WRITE) {
  66. iint->writecount--;
  67. if (iint->writecount == 0) {
  68. if (iint->version != inode->i_version)
  69. iint->flags &= ~IMA_MEASURED;
  70. }
  71. }
  72. mutex_unlock(&iint->mutex);
  73. kref_put(&iint->refcount, iint_free);
  74. }
  75. /* ima_read_write_check - reflect possible reading/writing errors in the PCR.
  76. *
  77. * When opening a file for read, if the file is already open for write,
  78. * the file could change, resulting in a file measurement error.
  79. *
  80. * Opening a file for write, if the file is already open for read, results
  81. * in a time of measure, time of use (ToMToU) error.
  82. *
  83. * In either case invalidate the PCR.
  84. */
  85. enum iint_pcr_error { TOMTOU, OPEN_WRITERS };
  86. static void ima_read_write_check(enum iint_pcr_error error,
  87. struct ima_iint_cache *iint,
  88. struct inode *inode,
  89. const unsigned char *filename)
  90. {
  91. switch (error) {
  92. case TOMTOU:
  93. if (iint->readcount > 0)
  94. ima_add_violation(inode, filename, "invalid_pcr",
  95. "ToMToU");
  96. break;
  97. case OPEN_WRITERS:
  98. if (iint->writecount > 0)
  99. ima_add_violation(inode, filename, "invalid_pcr",
  100. "open_writers");
  101. break;
  102. }
  103. }
  104. static int get_path_measurement(struct ima_iint_cache *iint, struct file *file,
  105. const unsigned char *filename)
  106. {
  107. int rc = 0;
  108. iint->opencount++;
  109. iint->readcount++;
  110. rc = ima_collect_measurement(iint, file);
  111. if (!rc)
  112. ima_store_measurement(iint, file, filename);
  113. return rc;
  114. }
  115. /**
  116. * ima_path_check - based on policy, collect/store measurement.
  117. * @path: contains a pointer to the path to be measured
  118. * @mask: contains MAY_READ, MAY_WRITE or MAY_EXECUTE
  119. *
  120. * Measure the file being open for readonly, based on the
  121. * ima_must_measure() policy decision.
  122. *
  123. * Keep read/write counters for all files, but only
  124. * invalidate the PCR for measured files:
  125. * - Opening a file for write when already open for read,
  126. * results in a time of measure, time of use (ToMToU) error.
  127. * - Opening a file for read when already open for write,
  128. * could result in a file measurement error.
  129. *
  130. * Return 0 on success, an error code on failure.
  131. * (Based on the results of appraise_measurement().)
  132. */
  133. int ima_path_check(struct path *path, int mask)
  134. {
  135. struct inode *inode = path->dentry->d_inode;
  136. struct ima_iint_cache *iint;
  137. struct file *file = NULL;
  138. int rc;
  139. if (!ima_initialized || !S_ISREG(inode->i_mode))
  140. return 0;
  141. iint = ima_iint_find_insert_get(inode);
  142. if (!iint)
  143. return 0;
  144. mutex_lock(&iint->mutex);
  145. iint->opencount++;
  146. if ((mask & MAY_WRITE) || (mask == 0))
  147. iint->writecount++;
  148. else if (mask & (MAY_READ | MAY_EXEC))
  149. iint->readcount++;
  150. rc = ima_must_measure(iint, inode, MAY_READ, PATH_CHECK);
  151. if (rc < 0)
  152. goto out;
  153. if ((mask & MAY_WRITE) || (mask == 0))
  154. ima_read_write_check(TOMTOU, iint, inode,
  155. path->dentry->d_name.name);
  156. if ((mask & (MAY_WRITE | MAY_READ | MAY_EXEC)) != MAY_READ)
  157. goto out;
  158. ima_read_write_check(OPEN_WRITERS, iint, inode,
  159. path->dentry->d_name.name);
  160. if (!(iint->flags & IMA_MEASURED)) {
  161. struct dentry *dentry = dget(path->dentry);
  162. struct vfsmount *mnt = mntget(path->mnt);
  163. file = dentry_open(dentry, mnt, O_RDONLY, current_cred());
  164. if (IS_ERR(file)) {
  165. pr_info("%s dentry_open failed\n", dentry->d_name.name);
  166. rc = PTR_ERR(file);
  167. file = NULL;
  168. goto out;
  169. }
  170. rc = get_path_measurement(iint, file, dentry->d_name.name);
  171. }
  172. out:
  173. mutex_unlock(&iint->mutex);
  174. if (file)
  175. fput(file);
  176. kref_put(&iint->refcount, iint_free);
  177. return 0;
  178. }
  179. static int process_measurement(struct file *file, const unsigned char *filename,
  180. int mask, int function)
  181. {
  182. struct inode *inode = file->f_dentry->d_inode;
  183. struct ima_iint_cache *iint;
  184. int rc;
  185. if (!ima_initialized || !S_ISREG(inode->i_mode))
  186. return 0;
  187. iint = ima_iint_find_insert_get(inode);
  188. if (!iint)
  189. return -ENOMEM;
  190. mutex_lock(&iint->mutex);
  191. rc = ima_must_measure(iint, inode, mask, function);
  192. if (rc != 0)
  193. goto out;
  194. rc = ima_collect_measurement(iint, file);
  195. if (!rc)
  196. ima_store_measurement(iint, file, filename);
  197. out:
  198. mutex_unlock(&iint->mutex);
  199. kref_put(&iint->refcount, iint_free);
  200. return rc;
  201. }
  202. static void opencount_get(struct file *file)
  203. {
  204. struct inode *inode = file->f_dentry->d_inode;
  205. struct ima_iint_cache *iint;
  206. if (!ima_initialized || !S_ISREG(inode->i_mode))
  207. return;
  208. iint = ima_iint_find_insert_get(inode);
  209. if (!iint)
  210. return;
  211. mutex_lock(&iint->mutex);
  212. iint->opencount++;
  213. mutex_unlock(&iint->mutex);
  214. }
  215. /**
  216. * ima_file_mmap - based on policy, collect/store measurement.
  217. * @file: pointer to the file to be measured (May be NULL)
  218. * @prot: contains the protection that will be applied by the kernel.
  219. *
  220. * Measure files being mmapped executable based on the ima_must_measure()
  221. * policy decision.
  222. *
  223. * Return 0 on success, an error code on failure.
  224. * (Based on the results of appraise_measurement().)
  225. */
  226. int ima_file_mmap(struct file *file, unsigned long prot)
  227. {
  228. int rc;
  229. if (!file)
  230. return 0;
  231. if (prot & PROT_EXEC)
  232. rc = process_measurement(file, file->f_dentry->d_name.name,
  233. MAY_EXEC, FILE_MMAP);
  234. return 0;
  235. }
  236. /*
  237. * ima_shm_check - IPC shm and shmat create/fput a file
  238. *
  239. * Maintain the opencount for these files to prevent unnecessary
  240. * imbalance messages.
  241. */
  242. void ima_shm_check(struct file *file)
  243. {
  244. opencount_get(file);
  245. return;
  246. }
  247. /**
  248. * ima_bprm_check - based on policy, collect/store measurement.
  249. * @bprm: contains the linux_binprm structure
  250. *
  251. * The OS protects against an executable file, already open for write,
  252. * from being executed in deny_write_access() and an executable file,
  253. * already open for execute, from being modified in get_write_access().
  254. * So we can be certain that what we verify and measure here is actually
  255. * what is being executed.
  256. *
  257. * Return 0 on success, an error code on failure.
  258. * (Based on the results of appraise_measurement().)
  259. */
  260. int ima_bprm_check(struct linux_binprm *bprm)
  261. {
  262. int rc;
  263. rc = process_measurement(bprm->file, bprm->filename,
  264. MAY_EXEC, BPRM_CHECK);
  265. return 0;
  266. }
  267. static int __init init_ima(void)
  268. {
  269. int error;
  270. ima_iintcache_init();
  271. error = ima_init();
  272. ima_initialized = 1;
  273. return error;
  274. }
  275. static void __exit cleanup_ima(void)
  276. {
  277. ima_cleanup();
  278. }
  279. late_initcall(init_ima); /* Start IMA after the TPM is available */
  280. MODULE_DESCRIPTION("Integrity Measurement Architecture");
  281. MODULE_LICENSE("GPL");