ima_main.c 9.6 KB

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