ima_main.c 8.3 KB

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