ima_main.c 8.2 KB

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