ima_main.c 9.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353
  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 <linux/xattr.h>
  26. #include <linux/ima.h>
  27. #include <crypto/hash_info.h>
  28. #include "ima.h"
  29. int ima_initialized;
  30. #ifdef CONFIG_IMA_APPRAISE
  31. int ima_appraise = IMA_APPRAISE_ENFORCE;
  32. #else
  33. int ima_appraise;
  34. #endif
  35. int ima_hash_algo = HASH_ALGO_SHA1;
  36. static int hash_setup_done;
  37. static int __init hash_setup(char *str)
  38. {
  39. struct ima_template_desc *template_desc = ima_template_desc_current();
  40. int i;
  41. if (hash_setup_done)
  42. return 1;
  43. if (strcmp(template_desc->name, IMA_TEMPLATE_IMA_NAME) == 0) {
  44. if (strncmp(str, "sha1", 4) == 0)
  45. ima_hash_algo = HASH_ALGO_SHA1;
  46. else if (strncmp(str, "md5", 3) == 0)
  47. ima_hash_algo = HASH_ALGO_MD5;
  48. goto out;
  49. }
  50. for (i = 0; i < HASH_ALGO__LAST; i++) {
  51. if (strcmp(str, hash_algo_name[i]) == 0) {
  52. ima_hash_algo = i;
  53. break;
  54. }
  55. }
  56. out:
  57. hash_setup_done = 1;
  58. return 1;
  59. }
  60. __setup("ima_hash=", hash_setup);
  61. /*
  62. * ima_rdwr_violation_check
  63. *
  64. * Only invalidate the PCR for measured files:
  65. * - Opening a file for write when already open for read,
  66. * results in a time of measure, time of use (ToMToU) error.
  67. * - Opening a file for read when already open for write,
  68. * could result in a file measurement error.
  69. *
  70. */
  71. static void ima_rdwr_violation_check(struct file *file)
  72. {
  73. struct inode *inode = file_inode(file);
  74. fmode_t mode = file->f_mode;
  75. bool send_tomtou = false, send_writers = false;
  76. char *pathbuf = NULL;
  77. const char *pathname;
  78. if (!S_ISREG(inode->i_mode) || !ima_initialized)
  79. return;
  80. if (mode & FMODE_WRITE) {
  81. if (atomic_read(&inode->i_readcount) && IS_IMA(inode)) {
  82. struct integrity_iint_cache *iint;
  83. iint = integrity_iint_find(inode);
  84. /* IMA_MEASURE is set from reader side */
  85. if (iint && (iint->flags & IMA_MEASURE))
  86. send_tomtou = true;
  87. }
  88. } else {
  89. if ((atomic_read(&inode->i_writecount) > 0) &&
  90. ima_must_measure(inode, MAY_READ, FILE_CHECK))
  91. send_writers = true;
  92. }
  93. if (!send_tomtou && !send_writers)
  94. return;
  95. pathname = ima_d_path(&file->f_path, &pathbuf);
  96. if (send_tomtou)
  97. ima_add_violation(file, pathname, "invalid_pcr", "ToMToU");
  98. if (send_writers)
  99. ima_add_violation(file, pathname,
  100. "invalid_pcr", "open_writers");
  101. kfree(pathbuf);
  102. }
  103. static void ima_check_last_writer(struct integrity_iint_cache *iint,
  104. struct inode *inode, struct file *file)
  105. {
  106. fmode_t mode = file->f_mode;
  107. if (!(mode & FMODE_WRITE))
  108. return;
  109. mutex_lock(&inode->i_mutex);
  110. if (atomic_read(&inode->i_writecount) == 1 &&
  111. iint->version != inode->i_version) {
  112. iint->flags &= ~IMA_DONE_MASK;
  113. if (iint->flags & IMA_APPRAISE)
  114. ima_update_xattr(iint, file);
  115. }
  116. mutex_unlock(&inode->i_mutex);
  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. */
  124. void ima_file_free(struct file *file)
  125. {
  126. struct inode *inode = file_inode(file);
  127. struct integrity_iint_cache *iint;
  128. if (!iint_initialized || !S_ISREG(inode->i_mode))
  129. return;
  130. iint = integrity_iint_find(inode);
  131. if (!iint)
  132. return;
  133. ima_check_last_writer(iint, inode, file);
  134. }
  135. static int process_measurement(struct file *file, const char *filename,
  136. int mask, int function)
  137. {
  138. struct inode *inode = file_inode(file);
  139. struct integrity_iint_cache *iint;
  140. struct ima_template_desc *template_desc;
  141. char *pathbuf = NULL;
  142. const char *pathname = NULL;
  143. int rc = -ENOMEM, action, must_appraise, _func;
  144. struct evm_ima_xattr_data *xattr_value = NULL, **xattr_ptr = NULL;
  145. int xattr_len = 0;
  146. if (!ima_initialized || !S_ISREG(inode->i_mode))
  147. return 0;
  148. /* Return an IMA_MEASURE, IMA_APPRAISE, IMA_AUDIT action
  149. * bitmask based on the appraise/audit/measurement policy.
  150. * Included is the appraise submask.
  151. */
  152. action = ima_get_action(inode, mask, function);
  153. if (!action)
  154. return 0;
  155. must_appraise = action & IMA_APPRAISE;
  156. /* Is the appraise rule hook specific? */
  157. _func = (action & IMA_FILE_APPRAISE) ? FILE_CHECK : function;
  158. mutex_lock(&inode->i_mutex);
  159. iint = integrity_inode_get(inode);
  160. if (!iint)
  161. goto out;
  162. /* Determine if already appraised/measured based on bitmask
  163. * (IMA_MEASURE, IMA_MEASURED, IMA_XXXX_APPRAISE, IMA_XXXX_APPRAISED,
  164. * IMA_AUDIT, IMA_AUDITED)
  165. */
  166. iint->flags |= action;
  167. action &= IMA_DO_MASK;
  168. action &= ~((iint->flags & IMA_DONE_MASK) >> 1);
  169. /* Nothing to do, just return existing appraised status */
  170. if (!action) {
  171. if (must_appraise)
  172. rc = ima_get_cache_status(iint, _func);
  173. goto out_digsig;
  174. }
  175. template_desc = ima_template_desc_current();
  176. if (strcmp(template_desc->name, IMA_TEMPLATE_IMA_NAME) == 0) {
  177. if (action & IMA_APPRAISE_SUBMASK)
  178. xattr_ptr = &xattr_value;
  179. } else
  180. xattr_ptr = &xattr_value;
  181. rc = ima_collect_measurement(iint, file, xattr_ptr, &xattr_len);
  182. if (rc != 0) {
  183. if (file->f_flags & O_DIRECT)
  184. rc = (iint->flags & IMA_PERMIT_DIRECTIO) ? 0 : -EACCES;
  185. goto out_digsig;
  186. }
  187. pathname = filename ?: ima_d_path(&file->f_path, &pathbuf);
  188. if (action & IMA_MEASURE)
  189. ima_store_measurement(iint, file, pathname,
  190. xattr_value, xattr_len);
  191. if (action & IMA_APPRAISE_SUBMASK)
  192. rc = ima_appraise_measurement(_func, iint, file, pathname,
  193. xattr_value, xattr_len);
  194. if (action & IMA_AUDIT)
  195. ima_audit_measurement(iint, pathname);
  196. kfree(pathbuf);
  197. out_digsig:
  198. if ((mask & MAY_WRITE) && (iint->flags & IMA_DIGSIG))
  199. rc = -EACCES;
  200. out:
  201. mutex_unlock(&inode->i_mutex);
  202. kfree(xattr_value);
  203. if ((rc && must_appraise) && (ima_appraise & IMA_APPRAISE_ENFORCE))
  204. return -EACCES;
  205. return 0;
  206. }
  207. /**
  208. * ima_file_mmap - based on policy, collect/store measurement.
  209. * @file: pointer to the file to be measured (May be NULL)
  210. * @prot: contains the protection that will be applied by the kernel.
  211. *
  212. * Measure files being mmapped executable based on the ima_must_measure()
  213. * policy decision.
  214. *
  215. * On success return 0. On integrity appraisal error, assuming the file
  216. * is in policy and IMA-appraisal is in enforcing mode, return -EACCES.
  217. */
  218. int ima_file_mmap(struct file *file, unsigned long prot)
  219. {
  220. if (file && (prot & PROT_EXEC))
  221. return process_measurement(file, NULL, MAY_EXEC, MMAP_CHECK);
  222. return 0;
  223. }
  224. /**
  225. * ima_bprm_check - based on policy, collect/store measurement.
  226. * @bprm: contains the linux_binprm structure
  227. *
  228. * The OS protects against an executable file, already open for write,
  229. * from being executed in deny_write_access() and an executable file,
  230. * already open for execute, from being modified in get_write_access().
  231. * So we can be certain that what we verify and measure here is actually
  232. * what is being executed.
  233. *
  234. * On success return 0. On integrity appraisal error, assuming the file
  235. * is in policy and IMA-appraisal is in enforcing mode, return -EACCES.
  236. */
  237. int ima_bprm_check(struct linux_binprm *bprm)
  238. {
  239. return process_measurement(bprm->file,
  240. (strcmp(bprm->filename, bprm->interp) == 0) ?
  241. bprm->filename : bprm->interp,
  242. MAY_EXEC, BPRM_CHECK);
  243. }
  244. /**
  245. * ima_path_check - based on policy, collect/store measurement.
  246. * @file: pointer to the file to be measured
  247. * @mask: contains MAY_READ, MAY_WRITE or MAY_EXECUTE
  248. *
  249. * Measure files based on the ima_must_measure() policy decision.
  250. *
  251. * On success return 0. On integrity appraisal error, assuming the file
  252. * is in policy and IMA-appraisal is in enforcing mode, return -EACCES.
  253. */
  254. int ima_file_check(struct file *file, int mask)
  255. {
  256. ima_rdwr_violation_check(file);
  257. return process_measurement(file, NULL,
  258. mask & (MAY_READ | MAY_WRITE | MAY_EXEC),
  259. FILE_CHECK);
  260. }
  261. EXPORT_SYMBOL_GPL(ima_file_check);
  262. /**
  263. * ima_module_check - based on policy, collect/store/appraise measurement.
  264. * @file: pointer to the file to be measured/appraised
  265. *
  266. * Measure/appraise kernel modules based on policy.
  267. *
  268. * On success return 0. On integrity appraisal error, assuming the file
  269. * is in policy and IMA-appraisal is in enforcing mode, return -EACCES.
  270. */
  271. int ima_module_check(struct file *file)
  272. {
  273. if (!file) {
  274. #ifndef CONFIG_MODULE_SIG_FORCE
  275. if ((ima_appraise & IMA_APPRAISE_MODULES) &&
  276. (ima_appraise & IMA_APPRAISE_ENFORCE))
  277. return -EACCES; /* INTEGRITY_UNKNOWN */
  278. #endif
  279. return 0; /* We rely on module signature checking */
  280. }
  281. return process_measurement(file, NULL, MAY_EXEC, MODULE_CHECK);
  282. }
  283. int ima_fw_from_file(struct file *file, char *buf, size_t size)
  284. {
  285. if (!file) {
  286. if ((ima_appraise & IMA_APPRAISE_FIRMWARE) &&
  287. (ima_appraise & IMA_APPRAISE_ENFORCE))
  288. return -EACCES; /* INTEGRITY_UNKNOWN */
  289. return 0;
  290. }
  291. return process_measurement(file, NULL, MAY_EXEC, FIRMWARE_CHECK);
  292. }
  293. static int __init init_ima(void)
  294. {
  295. int error;
  296. hash_setup(CONFIG_IMA_DEFAULT_HASH);
  297. error = ima_init();
  298. if (error)
  299. goto out;
  300. error = ima_init_keyring(INTEGRITY_KEYRING_IMA);
  301. if (error)
  302. goto out;
  303. ima_initialized = 1;
  304. out:
  305. return error;
  306. }
  307. late_initcall(init_ima); /* Start IMA after the TPM is available */
  308. MODULE_DESCRIPTION("Integrity Measurement Architecture");
  309. MODULE_LICENSE("GPL");