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