ima_main.c 8.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336
  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 "ima.h"
  26. int ima_initialized;
  27. char *ima_hash = "sha1";
  28. static int __init hash_setup(char *str)
  29. {
  30. if (strncmp(str, "md5", 3) == 0)
  31. ima_hash = "md5";
  32. return 1;
  33. }
  34. __setup("ima_hash=", hash_setup);
  35. struct ima_imbalance {
  36. struct hlist_node node;
  37. unsigned long fsmagic;
  38. };
  39. /*
  40. * ima_limit_imbalance - emit one imbalance message per filesystem type
  41. *
  42. * Maintain list of filesystem types that do not measure files properly.
  43. * Return false if unknown, true if known.
  44. */
  45. static bool ima_limit_imbalance(struct file *file)
  46. {
  47. static DEFINE_SPINLOCK(ima_imbalance_lock);
  48. static HLIST_HEAD(ima_imbalance_list);
  49. struct super_block *sb = file->f_dentry->d_sb;
  50. struct ima_imbalance *entry;
  51. struct hlist_node *node;
  52. bool found = false;
  53. rcu_read_lock();
  54. hlist_for_each_entry_rcu(entry, node, &ima_imbalance_list, node) {
  55. if (entry->fsmagic == sb->s_magic) {
  56. found = true;
  57. break;
  58. }
  59. }
  60. rcu_read_unlock();
  61. if (found)
  62. goto out;
  63. entry = kmalloc(sizeof(*entry), GFP_NOFS);
  64. if (!entry)
  65. goto out;
  66. entry->fsmagic = sb->s_magic;
  67. spin_lock(&ima_imbalance_lock);
  68. /*
  69. * we could have raced and something else might have added this fs
  70. * to the list, but we don't really care
  71. */
  72. hlist_add_head_rcu(&entry->node, &ima_imbalance_list);
  73. spin_unlock(&ima_imbalance_lock);
  74. printk(KERN_INFO "IMA: unmeasured files on fsmagic: %lX\n",
  75. entry->fsmagic);
  76. out:
  77. return found;
  78. }
  79. /* ima_read_write_check - reflect possible reading/writing errors in the PCR.
  80. *
  81. * When opening a file for read, if the file is already open for write,
  82. * the file could change, resulting in a file measurement error.
  83. *
  84. * Opening a file for write, if the file is already open for read, results
  85. * in a time of measure, time of use (ToMToU) error.
  86. *
  87. * In either case invalidate the PCR.
  88. */
  89. enum iint_pcr_error { TOMTOU, OPEN_WRITERS };
  90. static void ima_read_write_check(enum iint_pcr_error error,
  91. struct ima_iint_cache *iint,
  92. struct inode *inode,
  93. const unsigned char *filename)
  94. {
  95. switch (error) {
  96. case TOMTOU:
  97. if (iint->readcount > 0)
  98. ima_add_violation(inode, filename, "invalid_pcr",
  99. "ToMToU");
  100. break;
  101. case OPEN_WRITERS:
  102. if (iint->writecount > 0)
  103. ima_add_violation(inode, filename, "invalid_pcr",
  104. "open_writers");
  105. break;
  106. }
  107. }
  108. /*
  109. * Update the counts given an fmode_t
  110. */
  111. static void ima_inc_counts(struct ima_iint_cache *iint, fmode_t mode)
  112. {
  113. BUG_ON(!mutex_is_locked(&iint->mutex));
  114. if ((mode & (FMODE_READ | FMODE_WRITE)) == FMODE_READ)
  115. iint->readcount++;
  116. if (mode & FMODE_WRITE)
  117. iint->writecount++;
  118. }
  119. /*
  120. * ima_counts_get - increment file counts
  121. *
  122. * Maintain read/write counters for all files, but only
  123. * invalidate the PCR for measured files:
  124. * - Opening a file for write when already open for read,
  125. * results in a time of measure, time of use (ToMToU) error.
  126. * - Opening a file for read when already open for write,
  127. * could result in a file measurement error.
  128. *
  129. */
  130. void ima_counts_get(struct file *file)
  131. {
  132. struct dentry *dentry = file->f_path.dentry;
  133. struct inode *inode = dentry->d_inode;
  134. fmode_t mode = file->f_mode;
  135. struct ima_iint_cache *iint;
  136. int rc;
  137. if (!iint_initialized || !S_ISREG(inode->i_mode))
  138. return;
  139. iint = ima_iint_find_get(inode);
  140. if (!iint)
  141. return;
  142. mutex_lock(&iint->mutex);
  143. if (!ima_initialized)
  144. goto out;
  145. rc = ima_must_measure(iint, inode, MAY_READ, FILE_CHECK);
  146. if (rc < 0)
  147. goto out;
  148. if (mode & FMODE_WRITE) {
  149. ima_read_write_check(TOMTOU, iint, inode, dentry->d_name.name);
  150. goto out;
  151. }
  152. ima_read_write_check(OPEN_WRITERS, iint, inode, dentry->d_name.name);
  153. out:
  154. ima_inc_counts(iint, file->f_mode);
  155. mutex_unlock(&iint->mutex);
  156. kref_put(&iint->refcount, iint_free);
  157. }
  158. /*
  159. * Decrement ima counts
  160. */
  161. static void ima_dec_counts(struct ima_iint_cache *iint, struct inode *inode,
  162. struct file *file)
  163. {
  164. mode_t mode = file->f_mode;
  165. BUG_ON(!mutex_is_locked(&iint->mutex));
  166. if ((mode & (FMODE_READ | FMODE_WRITE)) == FMODE_READ)
  167. iint->readcount--;
  168. if (mode & FMODE_WRITE) {
  169. iint->writecount--;
  170. if (iint->writecount == 0) {
  171. if (iint->version != inode->i_version)
  172. iint->flags &= ~IMA_MEASURED;
  173. }
  174. }
  175. if (((iint->readcount < 0) ||
  176. (iint->writecount < 0)) &&
  177. !ima_limit_imbalance(file)) {
  178. printk(KERN_INFO "%s: open/free imbalance (r:%ld w:%ld)\n",
  179. __func__, iint->readcount, iint->writecount);
  180. dump_stack();
  181. }
  182. }
  183. /**
  184. * ima_file_free - called on __fput()
  185. * @file: pointer to file structure being freed
  186. *
  187. * Flag files that changed, based on i_version;
  188. * and decrement the iint readcount/writecount.
  189. */
  190. void ima_file_free(struct file *file)
  191. {
  192. struct inode *inode = file->f_dentry->d_inode;
  193. struct ima_iint_cache *iint;
  194. if (!iint_initialized || !S_ISREG(inode->i_mode))
  195. return;
  196. iint = ima_iint_find_get(inode);
  197. if (!iint)
  198. return;
  199. mutex_lock(&iint->mutex);
  200. ima_dec_counts(iint, inode, file);
  201. mutex_unlock(&iint->mutex);
  202. kref_put(&iint->refcount, iint_free);
  203. }
  204. static int process_measurement(struct file *file, const unsigned char *filename,
  205. int mask, int function)
  206. {
  207. struct inode *inode = file->f_dentry->d_inode;
  208. struct ima_iint_cache *iint;
  209. int rc = 0;
  210. if (!ima_initialized || !S_ISREG(inode->i_mode))
  211. return 0;
  212. iint = ima_iint_find_get(inode);
  213. if (!iint)
  214. return -ENOMEM;
  215. mutex_lock(&iint->mutex);
  216. rc = ima_must_measure(iint, inode, mask, function);
  217. if (rc != 0)
  218. goto out;
  219. rc = ima_collect_measurement(iint, file);
  220. if (!rc)
  221. ima_store_measurement(iint, file, filename);
  222. out:
  223. mutex_unlock(&iint->mutex);
  224. kref_put(&iint->refcount, iint_free);
  225. return rc;
  226. }
  227. /**
  228. * ima_file_mmap - based on policy, collect/store measurement.
  229. * @file: pointer to the file to be measured (May be NULL)
  230. * @prot: contains the protection that will be applied by the kernel.
  231. *
  232. * Measure files being mmapped executable based on the ima_must_measure()
  233. * policy decision.
  234. *
  235. * Return 0 on success, an error code on failure.
  236. * (Based on the results of appraise_measurement().)
  237. */
  238. int ima_file_mmap(struct file *file, unsigned long prot)
  239. {
  240. int rc;
  241. if (!file)
  242. return 0;
  243. if (prot & PROT_EXEC)
  244. rc = process_measurement(file, file->f_dentry->d_name.name,
  245. MAY_EXEC, FILE_MMAP);
  246. return 0;
  247. }
  248. /**
  249. * ima_bprm_check - based on policy, collect/store measurement.
  250. * @bprm: contains the linux_binprm structure
  251. *
  252. * The OS protects against an executable file, already open for write,
  253. * from being executed in deny_write_access() and an executable file,
  254. * already open for execute, from being modified in get_write_access().
  255. * So we can be certain that what we verify and measure here is actually
  256. * what is being executed.
  257. *
  258. * Return 0 on success, an error code on failure.
  259. * (Based on the results of appraise_measurement().)
  260. */
  261. int ima_bprm_check(struct linux_binprm *bprm)
  262. {
  263. int rc;
  264. rc = process_measurement(bprm->file, bprm->filename,
  265. MAY_EXEC, BPRM_CHECK);
  266. return 0;
  267. }
  268. /**
  269. * ima_path_check - based on policy, collect/store measurement.
  270. * @file: pointer to the file to be measured
  271. * @mask: contains MAY_READ, MAY_WRITE or MAY_EXECUTE
  272. *
  273. * Measure files based on the ima_must_measure() policy decision.
  274. *
  275. * Always return 0 and audit dentry_open failures.
  276. * (Return code will be based upon measurement appraisal.)
  277. */
  278. int ima_file_check(struct file *file, int mask)
  279. {
  280. int rc;
  281. rc = process_measurement(file, file->f_dentry->d_name.name,
  282. mask & (MAY_READ | MAY_WRITE | MAY_EXEC),
  283. FILE_CHECK);
  284. return 0;
  285. }
  286. EXPORT_SYMBOL_GPL(ima_file_check);
  287. static int __init init_ima(void)
  288. {
  289. int error;
  290. error = ima_init();
  291. ima_initialized = 1;
  292. return error;
  293. }
  294. static void __exit cleanup_ima(void)
  295. {
  296. ima_cleanup();
  297. }
  298. late_initcall(init_ima); /* Start IMA after the TPM is available */
  299. MODULE_DESCRIPTION("Integrity Measurement Architecture");
  300. MODULE_LICENSE("GPL");