ima_main.c 9.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390
  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_path_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. /*
  79. * Update the counts given an fmode_t
  80. */
  81. static void ima_inc_counts(struct ima_iint_cache *iint, fmode_t mode)
  82. {
  83. BUG_ON(!mutex_is_locked(&iint->mutex));
  84. iint->opencount++;
  85. if ((mode & (FMODE_READ | FMODE_WRITE)) == FMODE_READ)
  86. iint->readcount++;
  87. if (mode & FMODE_WRITE)
  88. iint->writecount++;
  89. }
  90. /*
  91. * Decrement ima counts
  92. */
  93. static void ima_dec_counts(struct ima_iint_cache *iint, struct inode *inode,
  94. struct file *file)
  95. {
  96. mode_t mode = file->f_mode;
  97. BUG_ON(!mutex_is_locked(&iint->mutex));
  98. iint->opencount--;
  99. if ((mode & (FMODE_READ | FMODE_WRITE)) == FMODE_READ)
  100. iint->readcount--;
  101. if (mode & FMODE_WRITE) {
  102. iint->writecount--;
  103. if (iint->writecount == 0) {
  104. if (iint->version != inode->i_version)
  105. iint->flags &= ~IMA_MEASURED;
  106. }
  107. }
  108. if (((iint->opencount < 0) ||
  109. (iint->readcount < 0) ||
  110. (iint->writecount < 0)) &&
  111. !ima_limit_imbalance(file)) {
  112. printk(KERN_INFO "%s: open/free imbalance (r:%ld w:%ld o:%ld)\n",
  113. __FUNCTION__, iint->readcount, iint->writecount,
  114. iint->opencount);
  115. dump_stack();
  116. }
  117. }
  118. /**
  119. * ima_file_free - called on __fput()
  120. * @file: pointer to file structure being freed
  121. *
  122. * Flag files that changed, based on i_version;
  123. * and decrement the iint readcount/writecount.
  124. */
  125. void ima_file_free(struct file *file)
  126. {
  127. struct inode *inode = file->f_dentry->d_inode;
  128. struct ima_iint_cache *iint;
  129. if (!ima_initialized || !S_ISREG(inode->i_mode))
  130. return;
  131. iint = ima_iint_find_get(inode);
  132. if (!iint)
  133. return;
  134. mutex_lock(&iint->mutex);
  135. ima_dec_counts(iint, inode, file);
  136. mutex_unlock(&iint->mutex);
  137. kref_put(&iint->refcount, iint_free);
  138. }
  139. /* ima_read_write_check - reflect possible reading/writing errors in the PCR.
  140. *
  141. * When opening a file for read, if the file is already open for write,
  142. * the file could change, resulting in a file measurement error.
  143. *
  144. * Opening a file for write, if the file is already open for read, results
  145. * in a time of measure, time of use (ToMToU) error.
  146. *
  147. * In either case invalidate the PCR.
  148. */
  149. enum iint_pcr_error { TOMTOU, OPEN_WRITERS };
  150. static void ima_read_write_check(enum iint_pcr_error error,
  151. struct ima_iint_cache *iint,
  152. struct inode *inode,
  153. const unsigned char *filename)
  154. {
  155. switch (error) {
  156. case TOMTOU:
  157. if (iint->readcount > 0)
  158. ima_add_violation(inode, filename, "invalid_pcr",
  159. "ToMToU");
  160. break;
  161. case OPEN_WRITERS:
  162. if (iint->writecount > 0)
  163. ima_add_violation(inode, filename, "invalid_pcr",
  164. "open_writers");
  165. break;
  166. }
  167. }
  168. static int get_path_measurement(struct ima_iint_cache *iint, struct file *file,
  169. const unsigned char *filename)
  170. {
  171. int rc = 0;
  172. ima_inc_counts(iint, file->f_mode);
  173. rc = ima_collect_measurement(iint, file);
  174. if (!rc)
  175. ima_store_measurement(iint, file, filename);
  176. return rc;
  177. }
  178. /**
  179. * ima_path_check - based on policy, collect/store measurement.
  180. * @path: contains a pointer to the path to be measured
  181. * @mask: contains MAY_READ, MAY_WRITE or MAY_EXECUTE
  182. *
  183. * Measure the file being open for readonly, based on the
  184. * ima_must_measure() policy decision.
  185. *
  186. * Keep read/write counters for all files, but only
  187. * invalidate the PCR for measured files:
  188. * - Opening a file for write when already open for read,
  189. * results in a time of measure, time of use (ToMToU) error.
  190. * - Opening a file for read when already open for write,
  191. * could result in a file measurement error.
  192. *
  193. * Always return 0 and audit dentry_open failures.
  194. * (Return code will be based upon measurement appraisal.)
  195. */
  196. int ima_path_check(struct path *path, int mask)
  197. {
  198. struct inode *inode = path->dentry->d_inode;
  199. struct ima_iint_cache *iint;
  200. struct file *file = NULL;
  201. int rc;
  202. if (!ima_initialized || !S_ISREG(inode->i_mode))
  203. return 0;
  204. iint = ima_iint_find_get(inode);
  205. if (!iint)
  206. return 0;
  207. mutex_lock(&iint->mutex);
  208. rc = ima_must_measure(iint, inode, MAY_READ, PATH_CHECK);
  209. if (rc < 0)
  210. goto out;
  211. if ((mask & MAY_WRITE) || (mask == 0))
  212. ima_read_write_check(TOMTOU, iint, inode,
  213. path->dentry->d_name.name);
  214. if ((mask & (MAY_WRITE | MAY_READ | MAY_EXEC)) != MAY_READ)
  215. goto out;
  216. ima_read_write_check(OPEN_WRITERS, iint, inode,
  217. path->dentry->d_name.name);
  218. if (!(iint->flags & IMA_MEASURED)) {
  219. struct dentry *dentry = dget(path->dentry);
  220. struct vfsmount *mnt = mntget(path->mnt);
  221. file = dentry_open(dentry, mnt, O_RDONLY | O_LARGEFILE,
  222. current_cred());
  223. if (IS_ERR(file)) {
  224. int audit_info = 0;
  225. integrity_audit_msg(AUDIT_INTEGRITY_PCR, inode,
  226. dentry->d_name.name,
  227. "add_measurement",
  228. "dentry_open failed",
  229. 1, audit_info);
  230. file = NULL;
  231. goto out;
  232. }
  233. rc = get_path_measurement(iint, file, dentry->d_name.name);
  234. }
  235. out:
  236. mutex_unlock(&iint->mutex);
  237. if (file)
  238. fput(file);
  239. kref_put(&iint->refcount, iint_free);
  240. return 0;
  241. }
  242. EXPORT_SYMBOL_GPL(ima_path_check);
  243. static int process_measurement(struct file *file, const unsigned char *filename,
  244. int mask, int function)
  245. {
  246. struct inode *inode = file->f_dentry->d_inode;
  247. struct ima_iint_cache *iint;
  248. int rc;
  249. if (!ima_initialized || !S_ISREG(inode->i_mode))
  250. return 0;
  251. iint = ima_iint_find_get(inode);
  252. if (!iint)
  253. return -ENOMEM;
  254. mutex_lock(&iint->mutex);
  255. rc = ima_must_measure(iint, inode, mask, function);
  256. if (rc != 0)
  257. goto out;
  258. rc = ima_collect_measurement(iint, file);
  259. if (!rc)
  260. ima_store_measurement(iint, file, filename);
  261. out:
  262. mutex_unlock(&iint->mutex);
  263. kref_put(&iint->refcount, iint_free);
  264. return rc;
  265. }
  266. /*
  267. * ima_counts_get - increment file counts
  268. *
  269. * - for IPC shm and shmat file.
  270. * - for nfsd exported files.
  271. *
  272. * Increment the counts for these files to prevent unnecessary
  273. * imbalance messages.
  274. */
  275. void ima_counts_get(struct file *file)
  276. {
  277. struct inode *inode = file->f_dentry->d_inode;
  278. struct ima_iint_cache *iint;
  279. if (!ima_initialized || !S_ISREG(inode->i_mode))
  280. return;
  281. iint = ima_iint_find_get(inode);
  282. if (!iint)
  283. return;
  284. mutex_lock(&iint->mutex);
  285. ima_inc_counts(iint, file->f_mode);
  286. mutex_unlock(&iint->mutex);
  287. kref_put(&iint->refcount, iint_free);
  288. }
  289. EXPORT_SYMBOL_GPL(ima_counts_get);
  290. /**
  291. * ima_file_mmap - based on policy, collect/store measurement.
  292. * @file: pointer to the file to be measured (May be NULL)
  293. * @prot: contains the protection that will be applied by the kernel.
  294. *
  295. * Measure files being mmapped executable based on the ima_must_measure()
  296. * policy decision.
  297. *
  298. * Return 0 on success, an error code on failure.
  299. * (Based on the results of appraise_measurement().)
  300. */
  301. int ima_file_mmap(struct file *file, unsigned long prot)
  302. {
  303. int rc;
  304. if (!file)
  305. return 0;
  306. if (prot & PROT_EXEC)
  307. rc = process_measurement(file, file->f_dentry->d_name.name,
  308. MAY_EXEC, FILE_MMAP);
  309. return 0;
  310. }
  311. /**
  312. * ima_bprm_check - based on policy, collect/store measurement.
  313. * @bprm: contains the linux_binprm structure
  314. *
  315. * The OS protects against an executable file, already open for write,
  316. * from being executed in deny_write_access() and an executable file,
  317. * already open for execute, from being modified in get_write_access().
  318. * So we can be certain that what we verify and measure here is actually
  319. * what is being executed.
  320. *
  321. * Return 0 on success, an error code on failure.
  322. * (Based on the results of appraise_measurement().)
  323. */
  324. int ima_bprm_check(struct linux_binprm *bprm)
  325. {
  326. int rc;
  327. rc = process_measurement(bprm->file, bprm->filename,
  328. MAY_EXEC, BPRM_CHECK);
  329. return 0;
  330. }
  331. static int __init init_ima(void)
  332. {
  333. int error;
  334. ima_iintcache_init();
  335. error = ima_init();
  336. ima_initialized = 1;
  337. return error;
  338. }
  339. static void __exit cleanup_ima(void)
  340. {
  341. ima_cleanup();
  342. }
  343. late_initcall(init_ima); /* Start IMA after the TPM is available */
  344. MODULE_DESCRIPTION("Integrity Measurement Architecture");
  345. MODULE_LICENSE("GPL");