ima_main.c 9.1 KB

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