ima_main.c 8.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338
  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. iint->opencount++;
  115. if ((mode & (FMODE_READ | FMODE_WRITE)) == FMODE_READ)
  116. iint->readcount++;
  117. if (mode & FMODE_WRITE)
  118. iint->writecount++;
  119. }
  120. /*
  121. * ima_counts_get - increment file counts
  122. *
  123. * Maintain 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. */
  131. void ima_counts_get(struct file *file)
  132. {
  133. struct dentry *dentry = file->f_path.dentry;
  134. struct inode *inode = dentry->d_inode;
  135. fmode_t mode = file->f_mode;
  136. struct ima_iint_cache *iint;
  137. int rc;
  138. if (!ima_initialized || !S_ISREG(inode->i_mode))
  139. return;
  140. iint = ima_iint_find_get(inode);
  141. if (!iint)
  142. return;
  143. mutex_lock(&iint->mutex);
  144. rc = ima_must_measure(iint, inode, MAY_READ, FILE_CHECK);
  145. if (rc < 0)
  146. goto out;
  147. if (mode & FMODE_WRITE) {
  148. ima_read_write_check(TOMTOU, iint, inode, dentry->d_name.name);
  149. goto out;
  150. }
  151. ima_read_write_check(OPEN_WRITERS, iint, inode, dentry->d_name.name);
  152. out:
  153. ima_inc_counts(iint, file->f_mode);
  154. mutex_unlock(&iint->mutex);
  155. kref_put(&iint->refcount, iint_free);
  156. }
  157. /*
  158. * Decrement ima counts
  159. */
  160. static void ima_dec_counts(struct ima_iint_cache *iint, struct inode *inode,
  161. struct file *file)
  162. {
  163. mode_t mode = file->f_mode;
  164. BUG_ON(!mutex_is_locked(&iint->mutex));
  165. iint->opencount--;
  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->opencount < 0) ||
  176. (iint->readcount < 0) ||
  177. (iint->writecount < 0)) &&
  178. !ima_limit_imbalance(file)) {
  179. printk(KERN_INFO "%s: open/free imbalance (r:%ld w:%ld o:%ld)\n",
  180. __func__, iint->readcount, iint->writecount,
  181. iint->opencount);
  182. dump_stack();
  183. }
  184. }
  185. /**
  186. * ima_file_free - called on __fput()
  187. * @file: pointer to file structure being freed
  188. *
  189. * Flag files that changed, based on i_version;
  190. * and decrement the iint readcount/writecount.
  191. */
  192. void ima_file_free(struct file *file)
  193. {
  194. struct inode *inode = file->f_dentry->d_inode;
  195. struct ima_iint_cache *iint;
  196. if (!ima_initialized || !S_ISREG(inode->i_mode))
  197. return;
  198. iint = ima_iint_find_get(inode);
  199. if (!iint)
  200. return;
  201. mutex_lock(&iint->mutex);
  202. ima_dec_counts(iint, inode, file);
  203. mutex_unlock(&iint->mutex);
  204. kref_put(&iint->refcount, iint_free);
  205. }
  206. static int process_measurement(struct file *file, const unsigned char *filename,
  207. int mask, int function)
  208. {
  209. struct inode *inode = file->f_dentry->d_inode;
  210. struct ima_iint_cache *iint;
  211. int rc;
  212. if (!ima_initialized || !S_ISREG(inode->i_mode))
  213. return 0;
  214. iint = ima_iint_find_get(inode);
  215. if (!iint)
  216. return -ENOMEM;
  217. mutex_lock(&iint->mutex);
  218. rc = ima_must_measure(iint, inode, mask, function);
  219. if (rc != 0)
  220. goto out;
  221. rc = ima_collect_measurement(iint, file);
  222. if (!rc)
  223. ima_store_measurement(iint, file, filename);
  224. out:
  225. mutex_unlock(&iint->mutex);
  226. kref_put(&iint->refcount, iint_free);
  227. return rc;
  228. }
  229. /**
  230. * ima_file_mmap - based on policy, collect/store measurement.
  231. * @file: pointer to the file to be measured (May be NULL)
  232. * @prot: contains the protection that will be applied by the kernel.
  233. *
  234. * Measure files being mmapped executable based on the ima_must_measure()
  235. * policy decision.
  236. *
  237. * Return 0 on success, an error code on failure.
  238. * (Based on the results of appraise_measurement().)
  239. */
  240. int ima_file_mmap(struct file *file, unsigned long prot)
  241. {
  242. int rc;
  243. if (!file)
  244. return 0;
  245. if (prot & PROT_EXEC)
  246. rc = process_measurement(file, file->f_dentry->d_name.name,
  247. MAY_EXEC, FILE_MMAP);
  248. return 0;
  249. }
  250. /**
  251. * ima_bprm_check - based on policy, collect/store measurement.
  252. * @bprm: contains the linux_binprm structure
  253. *
  254. * The OS protects against an executable file, already open for write,
  255. * from being executed in deny_write_access() and an executable file,
  256. * already open for execute, from being modified in get_write_access().
  257. * So we can be certain that what we verify and measure here is actually
  258. * what is being executed.
  259. *
  260. * Return 0 on success, an error code on failure.
  261. * (Based on the results of appraise_measurement().)
  262. */
  263. int ima_bprm_check(struct linux_binprm *bprm)
  264. {
  265. int rc;
  266. rc = process_measurement(bprm->file, bprm->filename,
  267. MAY_EXEC, BPRM_CHECK);
  268. return 0;
  269. }
  270. /**
  271. * ima_path_check - based on policy, collect/store measurement.
  272. * @file: pointer to the file to be measured
  273. * @mask: contains MAY_READ, MAY_WRITE or MAY_EXECUTE
  274. *
  275. * Measure files based on the ima_must_measure() policy decision.
  276. *
  277. * Always return 0 and audit dentry_open failures.
  278. * (Return code will be based upon measurement appraisal.)
  279. */
  280. int ima_file_check(struct file *file, int mask)
  281. {
  282. int rc;
  283. rc = process_measurement(file, file->f_dentry->d_name.name,
  284. mask & (MAY_READ | MAY_WRITE | MAY_EXEC),
  285. FILE_CHECK);
  286. return 0;
  287. }
  288. EXPORT_SYMBOL_GPL(ima_file_check);
  289. static int __init init_ima(void)
  290. {
  291. int error;
  292. error = ima_init();
  293. ima_initialized = 1;
  294. return error;
  295. }
  296. static void __exit cleanup_ima(void)
  297. {
  298. ima_cleanup();
  299. }
  300. late_initcall(init_ima); /* Start IMA after the TPM is available */
  301. MODULE_DESCRIPTION("Integrity Measurement Architecture");
  302. MODULE_LICENSE("GPL");