ima_main.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434
  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 "ima.h"
  28. int ima_initialized;
  29. #ifdef CONFIG_IMA_APPRAISE
  30. int ima_appraise = IMA_APPRAISE_ENFORCE;
  31. #else
  32. int ima_appraise;
  33. #endif
  34. int ima_hash_algo = HASH_ALGO_SHA1;
  35. static int hash_setup_done;
  36. static int __init hash_setup(char *str)
  37. {
  38. struct ima_template_desc *template_desc = ima_template_desc_current();
  39. int i;
  40. if (hash_setup_done)
  41. return 1;
  42. if (strcmp(template_desc->name, IMA_TEMPLATE_IMA_NAME) == 0) {
  43. if (strncmp(str, "sha1", 4) == 0)
  44. ima_hash_algo = HASH_ALGO_SHA1;
  45. else if (strncmp(str, "md5", 3) == 0)
  46. ima_hash_algo = HASH_ALGO_MD5;
  47. goto out;
  48. }
  49. for (i = 0; i < HASH_ALGO__LAST; i++) {
  50. if (strcmp(str, hash_algo_name[i]) == 0) {
  51. ima_hash_algo = i;
  52. break;
  53. }
  54. }
  55. out:
  56. hash_setup_done = 1;
  57. return 1;
  58. }
  59. __setup("ima_hash=", hash_setup);
  60. /*
  61. * ima_rdwr_violation_check
  62. *
  63. * Only invalidate the PCR for measured files:
  64. * - Opening a file for write when already open for read,
  65. * results in a time of measure, time of use (ToMToU) error.
  66. * - Opening a file for read when already open for write,
  67. * could result in a file measurement error.
  68. *
  69. */
  70. static void ima_rdwr_violation_check(struct file *file,
  71. struct integrity_iint_cache *iint,
  72. int must_measure,
  73. char **pathbuf,
  74. const char **pathname)
  75. {
  76. struct inode *inode = file_inode(file);
  77. fmode_t mode = file->f_mode;
  78. bool send_tomtou = false, send_writers = false;
  79. if (mode & FMODE_WRITE) {
  80. if (atomic_read(&inode->i_readcount) && IS_IMA(inode)) {
  81. if (!iint)
  82. iint = integrity_iint_find(inode);
  83. /* IMA_MEASURE is set from reader side */
  84. if (iint && (iint->flags & IMA_MEASURE))
  85. send_tomtou = true;
  86. }
  87. } else {
  88. if ((atomic_read(&inode->i_writecount) > 0) && must_measure)
  89. send_writers = true;
  90. }
  91. if (!send_tomtou && !send_writers)
  92. return;
  93. *pathname = ima_d_path(&file->f_path, pathbuf);
  94. if (send_tomtou)
  95. ima_add_violation(file, *pathname, iint,
  96. "invalid_pcr", "ToMToU");
  97. if (send_writers)
  98. ima_add_violation(file, *pathname, iint,
  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. inode_lock(inode);
  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. iint->measured_pcrs = 0;
  113. if (iint->flags & IMA_APPRAISE)
  114. ima_update_xattr(iint, file);
  115. }
  116. }
  117. inode_unlock(inode);
  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, char *buf, loff_t size,
  137. int mask, enum ima_hooks func, 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. int pcr = CONFIG_IMA_MEASURE_PCR_IDX;
  146. struct evm_ima_xattr_data *xattr_value = NULL;
  147. int xattr_len = 0;
  148. bool violation_check;
  149. enum hash_algo hash_algo;
  150. if (!ima_policy_flag || !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, func, &pcr);
  157. violation_check = ((func == FILE_CHECK || func == MMAP_CHECK) &&
  158. (ima_policy_flag & IMA_MEASURE));
  159. if (!action && !violation_check)
  160. return 0;
  161. must_appraise = action & IMA_APPRAISE;
  162. /* Is the appraise rule hook specific? */
  163. if (action & IMA_FILE_APPRAISE)
  164. func = FILE_CHECK;
  165. inode_lock(inode);
  166. if (action) {
  167. iint = integrity_inode_get(inode);
  168. if (!iint)
  169. goto out;
  170. }
  171. if (violation_check) {
  172. ima_rdwr_violation_check(file, iint, action & IMA_MEASURE,
  173. &pathbuf, &pathname);
  174. if (!action) {
  175. rc = 0;
  176. goto out_free;
  177. }
  178. }
  179. /* Determine if already appraised/measured based on bitmask
  180. * (IMA_MEASURE, IMA_MEASURED, IMA_XXXX_APPRAISE, IMA_XXXX_APPRAISED,
  181. * IMA_AUDIT, IMA_AUDITED)
  182. */
  183. iint->flags |= action;
  184. action &= IMA_DO_MASK;
  185. action &= ~((iint->flags & (IMA_DONE_MASK ^ IMA_MEASURED)) >> 1);
  186. /* If target pcr is already measured, unset IMA_MEASURE action */
  187. if ((action & IMA_MEASURE) && (iint->measured_pcrs & (0x1 << pcr)))
  188. action ^= IMA_MEASURE;
  189. /* Nothing to do, just return existing appraised status */
  190. if (!action) {
  191. if (must_appraise)
  192. rc = ima_get_cache_status(iint, func);
  193. goto out_digsig;
  194. }
  195. template_desc = ima_template_desc_current();
  196. if ((action & IMA_APPRAISE_SUBMASK) ||
  197. strcmp(template_desc->name, IMA_TEMPLATE_IMA_NAME) != 0)
  198. /* read 'security.ima' */
  199. xattr_len = ima_read_xattr(file_dentry(file), &xattr_value);
  200. hash_algo = ima_get_hash_algo(xattr_value, xattr_len);
  201. rc = ima_collect_measurement(iint, file, buf, size, hash_algo);
  202. if (rc != 0) {
  203. if (file->f_flags & O_DIRECT)
  204. rc = (iint->flags & IMA_PERMIT_DIRECTIO) ? 0 : -EACCES;
  205. goto out_digsig;
  206. }
  207. if (!pathname) /* ima_rdwr_violation possibly pre-fetched */
  208. pathname = ima_d_path(&file->f_path, &pathbuf);
  209. if (action & IMA_MEASURE)
  210. ima_store_measurement(iint, file, pathname,
  211. xattr_value, xattr_len, pcr);
  212. if (action & IMA_APPRAISE_SUBMASK)
  213. rc = ima_appraise_measurement(func, iint, file, pathname,
  214. xattr_value, xattr_len, opened);
  215. if (action & IMA_AUDIT)
  216. ima_audit_measurement(iint, pathname);
  217. out_digsig:
  218. if ((mask & MAY_WRITE) && (iint->flags & IMA_DIGSIG) &&
  219. !(iint->flags & IMA_NEW_FILE))
  220. rc = -EACCES;
  221. kfree(xattr_value);
  222. out_free:
  223. if (pathbuf)
  224. __putname(pathbuf);
  225. out:
  226. inode_unlock(inode);
  227. if ((rc && must_appraise) && (ima_appraise & IMA_APPRAISE_ENFORCE))
  228. return -EACCES;
  229. return 0;
  230. }
  231. /**
  232. * ima_file_mmap - based on policy, collect/store measurement.
  233. * @file: pointer to the file to be measured (May be NULL)
  234. * @prot: contains the protection that will be applied by the kernel.
  235. *
  236. * Measure files being mmapped executable based on the ima_must_measure()
  237. * policy decision.
  238. *
  239. * On success return 0. On integrity appraisal error, assuming the file
  240. * is in policy and IMA-appraisal is in enforcing mode, return -EACCES.
  241. */
  242. int ima_file_mmap(struct file *file, unsigned long prot)
  243. {
  244. if (file && (prot & PROT_EXEC))
  245. return process_measurement(file, NULL, 0, MAY_EXEC,
  246. MMAP_CHECK, 0);
  247. return 0;
  248. }
  249. /**
  250. * ima_bprm_check - based on policy, collect/store measurement.
  251. * @bprm: contains the linux_binprm structure
  252. *
  253. * The OS protects against an executable file, already open for write,
  254. * from being executed in deny_write_access() and an executable file,
  255. * already open for execute, from being modified in get_write_access().
  256. * So we can be certain that what we verify and measure here is actually
  257. * what is being executed.
  258. *
  259. * On success return 0. On integrity appraisal error, assuming the file
  260. * is in policy and IMA-appraisal is in enforcing mode, return -EACCES.
  261. */
  262. int ima_bprm_check(struct linux_binprm *bprm)
  263. {
  264. return process_measurement(bprm->file, NULL, 0, MAY_EXEC,
  265. BPRM_CHECK, 0);
  266. }
  267. /**
  268. * ima_path_check - based on policy, collect/store measurement.
  269. * @file: pointer to the file to be measured
  270. * @mask: contains MAY_READ, MAY_WRITE or MAY_EXECUTE
  271. *
  272. * Measure files based on the ima_must_measure() policy decision.
  273. *
  274. * On success return 0. On integrity appraisal error, assuming the file
  275. * is in policy and IMA-appraisal is in enforcing mode, return -EACCES.
  276. */
  277. int ima_file_check(struct file *file, int mask, int opened)
  278. {
  279. return process_measurement(file, NULL, 0,
  280. mask & (MAY_READ | MAY_WRITE | MAY_EXEC),
  281. FILE_CHECK, opened);
  282. }
  283. EXPORT_SYMBOL_GPL(ima_file_check);
  284. /**
  285. * ima_post_path_mknod - mark as a new inode
  286. * @dentry: newly created dentry
  287. *
  288. * Mark files created via the mknodat syscall as new, so that the
  289. * file data can be written later.
  290. */
  291. void ima_post_path_mknod(struct dentry *dentry)
  292. {
  293. struct integrity_iint_cache *iint;
  294. struct inode *inode = dentry->d_inode;
  295. int must_appraise;
  296. must_appraise = ima_must_appraise(inode, MAY_ACCESS, FILE_CHECK);
  297. if (!must_appraise)
  298. return;
  299. iint = integrity_inode_get(inode);
  300. if (iint)
  301. iint->flags |= IMA_NEW_FILE;
  302. }
  303. /**
  304. * ima_read_file - pre-measure/appraise hook decision based on policy
  305. * @file: pointer to the file to be measured/appraised/audit
  306. * @read_id: caller identifier
  307. *
  308. * Permit reading a file based on policy. The policy rules are written
  309. * in terms of the policy identifier. Appraising the integrity of
  310. * a file requires a file descriptor.
  311. *
  312. * For permission return 0, otherwise return -EACCES.
  313. */
  314. int ima_read_file(struct file *file, enum kernel_read_file_id read_id)
  315. {
  316. if (!file && read_id == READING_MODULE) {
  317. #ifndef CONFIG_MODULE_SIG_FORCE
  318. if ((ima_appraise & IMA_APPRAISE_MODULES) &&
  319. (ima_appraise & IMA_APPRAISE_ENFORCE))
  320. return -EACCES; /* INTEGRITY_UNKNOWN */
  321. #endif
  322. return 0; /* We rely on module signature checking */
  323. }
  324. return 0;
  325. }
  326. static int read_idmap[READING_MAX_ID] = {
  327. [READING_FIRMWARE] = FIRMWARE_CHECK,
  328. [READING_MODULE] = MODULE_CHECK,
  329. [READING_KEXEC_IMAGE] = KEXEC_KERNEL_CHECK,
  330. [READING_KEXEC_INITRAMFS] = KEXEC_INITRAMFS_CHECK,
  331. [READING_POLICY] = POLICY_CHECK
  332. };
  333. /**
  334. * ima_post_read_file - in memory collect/appraise/audit measurement
  335. * @file: pointer to the file to be measured/appraised/audit
  336. * @buf: pointer to in memory file contents
  337. * @size: size of in memory file contents
  338. * @read_id: caller identifier
  339. *
  340. * Measure/appraise/audit in memory file based on policy. Policy rules
  341. * are written in terms of a policy identifier.
  342. *
  343. * On success return 0. On integrity appraisal error, assuming the file
  344. * is in policy and IMA-appraisal is in enforcing mode, return -EACCES.
  345. */
  346. int ima_post_read_file(struct file *file, void *buf, loff_t size,
  347. enum kernel_read_file_id read_id)
  348. {
  349. enum ima_hooks func;
  350. if (!file && read_id == READING_FIRMWARE) {
  351. if ((ima_appraise & IMA_APPRAISE_FIRMWARE) &&
  352. (ima_appraise & IMA_APPRAISE_ENFORCE))
  353. return -EACCES; /* INTEGRITY_UNKNOWN */
  354. return 0;
  355. }
  356. if (!file && read_id == READING_MODULE) /* MODULE_SIG_FORCE enabled */
  357. return 0;
  358. if (!file || !buf || size == 0) { /* should never happen */
  359. if (ima_appraise & IMA_APPRAISE_ENFORCE)
  360. return -EACCES;
  361. return 0;
  362. }
  363. func = read_idmap[read_id] ?: FILE_CHECK;
  364. return process_measurement(file, buf, size, MAY_READ, func, 0);
  365. }
  366. static int __init init_ima(void)
  367. {
  368. int error;
  369. ima_init_template_list();
  370. hash_setup(CONFIG_IMA_DEFAULT_HASH);
  371. error = ima_init();
  372. if (!error) {
  373. ima_initialized = 1;
  374. ima_update_policy_flag();
  375. }
  376. return error;
  377. }
  378. late_initcall(init_ima); /* Start IMA after the TPM is available */
  379. MODULE_DESCRIPTION("Integrity Measurement Architecture");
  380. MODULE_LICENSE("GPL");