ima_main.c 8.4 KB

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