ima_main.c 9.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360
  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. struct integrity_iint_cache *iint,
  73. int must_measure,
  74. char **pathbuf,
  75. const char **pathname)
  76. {
  77. struct inode *inode = file_inode(file);
  78. fmode_t mode = file->f_mode;
  79. bool send_tomtou = false, send_writers = false;
  80. if (mode & FMODE_WRITE) {
  81. if (atomic_read(&inode->i_readcount) && IS_IMA(inode)) {
  82. if (!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) && must_measure)
  90. send_writers = true;
  91. }
  92. if (!send_tomtou && !send_writers)
  93. return;
  94. *pathname = ima_d_path(&file->f_path, pathbuf);
  95. if (send_tomtou)
  96. ima_add_violation(file, *pathname, "invalid_pcr", "ToMToU");
  97. if (send_writers)
  98. ima_add_violation(file, *pathname,
  99. "invalid_pcr", "open_writers");
  100. }
  101. static void ima_check_last_writer(struct integrity_iint_cache *iint,
  102. struct inode *inode, struct file *file)
  103. {
  104. fmode_t mode = file->f_mode;
  105. if (!(mode & FMODE_WRITE))
  106. return;
  107. mutex_lock(&inode->i_mutex);
  108. if (atomic_read(&inode->i_writecount) == 1) {
  109. if ((iint->version != inode->i_version) ||
  110. (iint->flags & IMA_NEW_FILE)) {
  111. iint->flags &= ~(IMA_DONE_MASK | IMA_NEW_FILE);
  112. if (iint->flags & IMA_APPRAISE)
  113. ima_update_xattr(iint, file);
  114. }
  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, int mask, int function,
  136. int opened)
  137. {
  138. struct inode *inode = file_inode(file);
  139. struct integrity_iint_cache *iint = NULL;
  140. struct ima_template_desc *template_desc;
  141. char *pathbuf = NULL;
  142. const char *pathname = NULL;
  143. int rc = -ENOMEM, action, must_appraise;
  144. struct evm_ima_xattr_data *xattr_value = NULL, **xattr_ptr = NULL;
  145. int xattr_len = 0;
  146. bool violation_check;
  147. if (!ima_policy_flag || !S_ISREG(inode->i_mode))
  148. return 0;
  149. /* Return an IMA_MEASURE, IMA_APPRAISE, IMA_AUDIT action
  150. * bitmask based on the appraise/audit/measurement policy.
  151. * Included is the appraise submask.
  152. */
  153. action = ima_get_action(inode, mask, function);
  154. violation_check = ((function == FILE_CHECK || function == MMAP_CHECK) &&
  155. (ima_policy_flag & IMA_MEASURE));
  156. if (!action && !violation_check)
  157. return 0;
  158. must_appraise = action & IMA_APPRAISE;
  159. /* Is the appraise rule hook specific? */
  160. if (action & IMA_FILE_APPRAISE)
  161. function = FILE_CHECK;
  162. mutex_lock(&inode->i_mutex);
  163. if (action) {
  164. iint = integrity_inode_get(inode);
  165. if (!iint)
  166. goto out;
  167. }
  168. if (violation_check) {
  169. ima_rdwr_violation_check(file, iint, action & IMA_MEASURE,
  170. &pathbuf, &pathname);
  171. if (!action) {
  172. rc = 0;
  173. goto out_free;
  174. }
  175. }
  176. /* Determine if already appraised/measured based on bitmask
  177. * (IMA_MEASURE, IMA_MEASURED, IMA_XXXX_APPRAISE, IMA_XXXX_APPRAISED,
  178. * IMA_AUDIT, IMA_AUDITED)
  179. */
  180. iint->flags |= action;
  181. action &= IMA_DO_MASK;
  182. action &= ~((iint->flags & IMA_DONE_MASK) >> 1);
  183. /* Nothing to do, just return existing appraised status */
  184. if (!action) {
  185. if (must_appraise)
  186. rc = ima_get_cache_status(iint, function);
  187. goto out_digsig;
  188. }
  189. template_desc = ima_template_desc_current();
  190. if ((action & IMA_APPRAISE_SUBMASK) ||
  191. strcmp(template_desc->name, IMA_TEMPLATE_IMA_NAME) != 0)
  192. xattr_ptr = &xattr_value;
  193. rc = ima_collect_measurement(iint, file, xattr_ptr, &xattr_len);
  194. if (rc != 0) {
  195. if (file->f_flags & O_DIRECT)
  196. rc = (iint->flags & IMA_PERMIT_DIRECTIO) ? 0 : -EACCES;
  197. goto out_digsig;
  198. }
  199. if (!pathname) /* ima_rdwr_violation possibly pre-fetched */
  200. pathname = ima_d_path(&file->f_path, &pathbuf);
  201. if (action & IMA_MEASURE)
  202. ima_store_measurement(iint, file, pathname,
  203. xattr_value, xattr_len);
  204. if (action & IMA_APPRAISE_SUBMASK)
  205. rc = ima_appraise_measurement(function, iint, file, pathname,
  206. xattr_value, xattr_len, opened);
  207. if (action & IMA_AUDIT)
  208. ima_audit_measurement(iint, pathname);
  209. out_digsig:
  210. if ((mask & MAY_WRITE) && (iint->flags & IMA_DIGSIG))
  211. rc = -EACCES;
  212. kfree(xattr_value);
  213. out_free:
  214. kfree(pathbuf);
  215. out:
  216. mutex_unlock(&inode->i_mutex);
  217. if ((rc && must_appraise) && (ima_appraise & IMA_APPRAISE_ENFORCE))
  218. return -EACCES;
  219. return 0;
  220. }
  221. /**
  222. * ima_file_mmap - based on policy, collect/store measurement.
  223. * @file: pointer to the file to be measured (May be NULL)
  224. * @prot: contains the protection that will be applied by the kernel.
  225. *
  226. * Measure files being mmapped executable based on the ima_must_measure()
  227. * policy decision.
  228. *
  229. * On success return 0. On integrity appraisal error, assuming the file
  230. * is in policy and IMA-appraisal is in enforcing mode, return -EACCES.
  231. */
  232. int ima_file_mmap(struct file *file, unsigned long prot)
  233. {
  234. if (file && (prot & PROT_EXEC))
  235. return process_measurement(file, MAY_EXEC, MMAP_CHECK, 0);
  236. return 0;
  237. }
  238. /**
  239. * ima_bprm_check - based on policy, collect/store measurement.
  240. * @bprm: contains the linux_binprm structure
  241. *
  242. * The OS protects against an executable file, already open for write,
  243. * from being executed in deny_write_access() and an executable file,
  244. * already open for execute, from being modified in get_write_access().
  245. * So we can be certain that what we verify and measure here is actually
  246. * what is being executed.
  247. *
  248. * On success return 0. On integrity appraisal error, assuming the file
  249. * is in policy and IMA-appraisal is in enforcing mode, return -EACCES.
  250. */
  251. int ima_bprm_check(struct linux_binprm *bprm)
  252. {
  253. return process_measurement(bprm->file, MAY_EXEC, BPRM_CHECK, 0);
  254. }
  255. /**
  256. * ima_path_check - based on policy, collect/store measurement.
  257. * @file: pointer to the file to be measured
  258. * @mask: contains MAY_READ, MAY_WRITE or MAY_EXECUTE
  259. *
  260. * Measure files based on the ima_must_measure() policy decision.
  261. *
  262. * On success return 0. On integrity appraisal error, assuming the file
  263. * is in policy and IMA-appraisal is in enforcing mode, return -EACCES.
  264. */
  265. int ima_file_check(struct file *file, int mask, int opened)
  266. {
  267. return process_measurement(file,
  268. mask & (MAY_READ | MAY_WRITE | MAY_EXEC),
  269. FILE_CHECK, opened);
  270. }
  271. EXPORT_SYMBOL_GPL(ima_file_check);
  272. /**
  273. * ima_module_check - based on policy, collect/store/appraise measurement.
  274. * @file: pointer to the file to be measured/appraised
  275. *
  276. * Measure/appraise kernel modules based on policy.
  277. *
  278. * On success return 0. On integrity appraisal error, assuming the file
  279. * is in policy and IMA-appraisal is in enforcing mode, return -EACCES.
  280. */
  281. int ima_module_check(struct file *file)
  282. {
  283. if (!file) {
  284. #ifndef CONFIG_MODULE_SIG_FORCE
  285. if ((ima_appraise & IMA_APPRAISE_MODULES) &&
  286. (ima_appraise & IMA_APPRAISE_ENFORCE))
  287. return -EACCES; /* INTEGRITY_UNKNOWN */
  288. #endif
  289. return 0; /* We rely on module signature checking */
  290. }
  291. return process_measurement(file, MAY_EXEC, MODULE_CHECK, 0);
  292. }
  293. int ima_fw_from_file(struct file *file, char *buf, size_t size)
  294. {
  295. if (!file) {
  296. if ((ima_appraise & IMA_APPRAISE_FIRMWARE) &&
  297. (ima_appraise & IMA_APPRAISE_ENFORCE))
  298. return -EACCES; /* INTEGRITY_UNKNOWN */
  299. return 0;
  300. }
  301. return process_measurement(file, MAY_EXEC, FIRMWARE_CHECK, 0);
  302. }
  303. static int __init init_ima(void)
  304. {
  305. int error;
  306. hash_setup(CONFIG_IMA_DEFAULT_HASH);
  307. error = ima_init();
  308. if (!error) {
  309. ima_initialized = 1;
  310. ima_update_policy_flag();
  311. }
  312. return error;
  313. }
  314. late_initcall(init_ima); /* Start IMA after the TPM is available */
  315. MODULE_DESCRIPTION("Integrity Measurement Architecture");
  316. MODULE_LICENSE("GPL");