ima_main.c 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344
  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. /*
  80. * ima_counts_get - increment file counts
  81. *
  82. * Maintain read/write counters for all files, but only
  83. * invalidate the PCR for measured files:
  84. * - Opening a file for write when already open for read,
  85. * results in a time of measure, time of use (ToMToU) error.
  86. * - Opening a file for read when already open for write,
  87. * could result in a file measurement error.
  88. *
  89. */
  90. void ima_counts_get(struct file *file)
  91. {
  92. struct dentry *dentry = file->f_path.dentry;
  93. struct inode *inode = dentry->d_inode;
  94. fmode_t mode = file->f_mode;
  95. int rc;
  96. bool send_tomtou = false, send_writers = false;
  97. if (!S_ISREG(inode->i_mode))
  98. return;
  99. spin_lock(&inode->i_lock);
  100. if (!ima_initialized)
  101. goto out;
  102. if (mode & FMODE_WRITE) {
  103. if (inode->i_readcount && IS_IMA(inode))
  104. send_tomtou = true;
  105. goto out;
  106. }
  107. rc = ima_must_measure(NULL, inode, MAY_READ, FILE_CHECK);
  108. if (rc < 0)
  109. goto out;
  110. if (atomic_read(&inode->i_writecount) > 0)
  111. send_writers = true;
  112. out:
  113. /* remember the vfs deals with i_writecount */
  114. if ((mode & (FMODE_READ | FMODE_WRITE)) == FMODE_READ)
  115. inode->i_readcount++;
  116. spin_unlock(&inode->i_lock);
  117. if (send_tomtou)
  118. ima_add_violation(inode, dentry->d_name.name, "invalid_pcr",
  119. "ToMToU");
  120. if (send_writers)
  121. ima_add_violation(inode, dentry->d_name.name, "invalid_pcr",
  122. "open_writers");
  123. }
  124. /*
  125. * Decrement ima counts
  126. */
  127. static void ima_dec_counts(struct inode *inode, struct file *file)
  128. {
  129. mode_t mode = file->f_mode;
  130. assert_spin_locked(&inode->i_lock);
  131. if ((mode & (FMODE_READ | FMODE_WRITE)) == FMODE_READ) {
  132. if (unlikely(inode->i_readcount == 0)) {
  133. if (!ima_limit_imbalance(file)) {
  134. printk(KERN_INFO "%s: open/free imbalance (r:%u)\n",
  135. __func__, inode->i_readcount);
  136. dump_stack();
  137. }
  138. return;
  139. }
  140. inode->i_readcount--;
  141. }
  142. }
  143. static void ima_check_last_writer(struct ima_iint_cache *iint,
  144. struct inode *inode,
  145. struct file *file)
  146. {
  147. mode_t mode = file->f_mode;
  148. BUG_ON(!mutex_is_locked(&iint->mutex));
  149. assert_spin_locked(&inode->i_lock);
  150. if (mode & FMODE_WRITE &&
  151. atomic_read(&inode->i_writecount) == 1 &&
  152. iint->version != inode->i_version)
  153. iint->flags &= ~IMA_MEASURED;
  154. }
  155. static void ima_file_free_iint(struct ima_iint_cache *iint, struct inode *inode,
  156. struct file *file)
  157. {
  158. mutex_lock(&iint->mutex);
  159. spin_lock(&inode->i_lock);
  160. ima_dec_counts(inode, file);
  161. ima_check_last_writer(iint, inode, file);
  162. spin_unlock(&inode->i_lock);
  163. mutex_unlock(&iint->mutex);
  164. }
  165. static void ima_file_free_noiint(struct inode *inode, struct file *file)
  166. {
  167. spin_lock(&inode->i_lock);
  168. ima_dec_counts(inode, file);
  169. spin_unlock(&inode->i_lock);
  170. }
  171. /**
  172. * ima_file_free - called on __fput()
  173. * @file: pointer to file structure being freed
  174. *
  175. * Flag files that changed, based on i_version;
  176. * and decrement the i_readcount.
  177. */
  178. void ima_file_free(struct file *file)
  179. {
  180. struct inode *inode = file->f_dentry->d_inode;
  181. struct ima_iint_cache *iint;
  182. if (!iint_initialized || !S_ISREG(inode->i_mode))
  183. return;
  184. iint = ima_iint_find(inode);
  185. if (iint)
  186. ima_file_free_iint(iint, inode, file);
  187. else
  188. ima_file_free_noiint(inode, file);
  189. }
  190. static int process_measurement(struct file *file, const unsigned char *filename,
  191. int mask, int function)
  192. {
  193. struct inode *inode = file->f_dentry->d_inode;
  194. struct ima_iint_cache *iint;
  195. int rc = 0;
  196. if (!ima_initialized || !S_ISREG(inode->i_mode))
  197. return 0;
  198. rc = ima_must_measure(NULL, inode, mask, function);
  199. if (rc != 0)
  200. return rc;
  201. retry:
  202. iint = ima_iint_find(inode);
  203. if (!iint) {
  204. rc = ima_inode_alloc(inode);
  205. if (!rc || rc == -EEXIST)
  206. goto retry;
  207. return rc;
  208. }
  209. mutex_lock(&iint->mutex);
  210. rc = ima_must_measure(iint, inode, mask, function);
  211. if (rc != 0)
  212. goto out;
  213. rc = ima_collect_measurement(iint, file);
  214. if (!rc)
  215. ima_store_measurement(iint, file, filename);
  216. out:
  217. mutex_unlock(&iint->mutex);
  218. return rc;
  219. }
  220. /**
  221. * ima_file_mmap - based on policy, collect/store measurement.
  222. * @file: pointer to the file to be measured (May be NULL)
  223. * @prot: contains the protection that will be applied by the kernel.
  224. *
  225. * Measure files being mmapped executable based on the ima_must_measure()
  226. * policy decision.
  227. *
  228. * Return 0 on success, an error code on failure.
  229. * (Based on the results of appraise_measurement().)
  230. */
  231. int ima_file_mmap(struct file *file, unsigned long prot)
  232. {
  233. int rc;
  234. if (!file)
  235. return 0;
  236. if (prot & PROT_EXEC)
  237. rc = process_measurement(file, file->f_dentry->d_name.name,
  238. MAY_EXEC, FILE_MMAP);
  239. return 0;
  240. }
  241. /**
  242. * ima_bprm_check - based on policy, collect/store measurement.
  243. * @bprm: contains the linux_binprm structure
  244. *
  245. * The OS protects against an executable file, already open for write,
  246. * from being executed in deny_write_access() and an executable file,
  247. * already open for execute, from being modified in get_write_access().
  248. * So we can be certain that what we verify and measure here is actually
  249. * what is being executed.
  250. *
  251. * Return 0 on success, an error code on failure.
  252. * (Based on the results of appraise_measurement().)
  253. */
  254. int ima_bprm_check(struct linux_binprm *bprm)
  255. {
  256. int rc;
  257. rc = process_measurement(bprm->file, bprm->filename,
  258. MAY_EXEC, BPRM_CHECK);
  259. return 0;
  260. }
  261. /**
  262. * ima_path_check - based on policy, collect/store measurement.
  263. * @file: pointer to the file to be measured
  264. * @mask: contains MAY_READ, MAY_WRITE or MAY_EXECUTE
  265. *
  266. * Measure files based on the ima_must_measure() policy decision.
  267. *
  268. * Always return 0 and audit dentry_open failures.
  269. * (Return code will be based upon measurement appraisal.)
  270. */
  271. int ima_file_check(struct file *file, int mask)
  272. {
  273. int rc;
  274. rc = process_measurement(file, file->f_dentry->d_name.name,
  275. mask & (MAY_READ | MAY_WRITE | MAY_EXEC),
  276. FILE_CHECK);
  277. return 0;
  278. }
  279. EXPORT_SYMBOL_GPL(ima_file_check);
  280. static int __init init_ima(void)
  281. {
  282. int error;
  283. error = ima_init();
  284. ima_initialized = 1;
  285. return error;
  286. }
  287. static void __exit cleanup_ima(void)
  288. {
  289. ima_cleanup();
  290. }
  291. late_initcall(init_ima); /* Start IMA after the TPM is available */
  292. MODULE_DESCRIPTION("Integrity Measurement Architecture");
  293. MODULE_LICENSE("GPL");