ima_main.c 9.3 KB

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