ima_main.c 8.4 KB

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