ima_main.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435
  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. char filename[NAME_MAX];
  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, filename);
  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. inode_lock(inode);
  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. iint->measured_pcrs = 0;
  114. if (iint->flags & IMA_APPRAISE)
  115. ima_update_xattr(iint, file);
  116. }
  117. }
  118. inode_unlock(inode);
  119. }
  120. /**
  121. * ima_file_free - called on __fput()
  122. * @file: pointer to file structure being freed
  123. *
  124. * Flag files that changed, based on i_version
  125. */
  126. void ima_file_free(struct file *file)
  127. {
  128. struct inode *inode = file_inode(file);
  129. struct integrity_iint_cache *iint;
  130. if (!ima_policy_flag || !S_ISREG(inode->i_mode))
  131. return;
  132. iint = integrity_iint_find(inode);
  133. if (!iint)
  134. return;
  135. ima_check_last_writer(iint, inode, file);
  136. }
  137. static int process_measurement(struct file *file, char *buf, loff_t size,
  138. int mask, enum ima_hooks func, int opened)
  139. {
  140. struct inode *inode = file_inode(file);
  141. struct integrity_iint_cache *iint = NULL;
  142. struct ima_template_desc *template_desc;
  143. char *pathbuf = NULL;
  144. char filename[NAME_MAX];
  145. const char *pathname = NULL;
  146. int rc = -ENOMEM, action, must_appraise;
  147. int pcr = CONFIG_IMA_MEASURE_PCR_IDX;
  148. struct evm_ima_xattr_data *xattr_value = NULL;
  149. int xattr_len = 0;
  150. bool violation_check;
  151. enum hash_algo hash_algo;
  152. if (!ima_policy_flag || !S_ISREG(inode->i_mode))
  153. return 0;
  154. /* Return an IMA_MEASURE, IMA_APPRAISE, IMA_AUDIT action
  155. * bitmask based on the appraise/audit/measurement policy.
  156. * Included is the appraise submask.
  157. */
  158. action = ima_get_action(inode, mask, func, &pcr);
  159. violation_check = ((func == FILE_CHECK || func == MMAP_CHECK) &&
  160. (ima_policy_flag & IMA_MEASURE));
  161. if (!action && !violation_check)
  162. return 0;
  163. must_appraise = action & IMA_APPRAISE;
  164. /* Is the appraise rule hook specific? */
  165. if (action & IMA_FILE_APPRAISE)
  166. func = FILE_CHECK;
  167. inode_lock(inode);
  168. if (action) {
  169. iint = integrity_inode_get(inode);
  170. if (!iint)
  171. goto out;
  172. }
  173. if (violation_check) {
  174. ima_rdwr_violation_check(file, iint, action & IMA_MEASURE,
  175. &pathbuf, &pathname);
  176. if (!action) {
  177. rc = 0;
  178. goto out_free;
  179. }
  180. }
  181. /* Determine if already appraised/measured based on bitmask
  182. * (IMA_MEASURE, IMA_MEASURED, IMA_XXXX_APPRAISE, IMA_XXXX_APPRAISED,
  183. * IMA_AUDIT, IMA_AUDITED)
  184. */
  185. iint->flags |= action;
  186. action &= IMA_DO_MASK;
  187. action &= ~((iint->flags & (IMA_DONE_MASK ^ IMA_MEASURED)) >> 1);
  188. /* If target pcr is already measured, unset IMA_MEASURE action */
  189. if ((action & IMA_MEASURE) && (iint->measured_pcrs & (0x1 << pcr)))
  190. action ^= IMA_MEASURE;
  191. /* Nothing to do, just return existing appraised status */
  192. if (!action) {
  193. if (must_appraise)
  194. rc = ima_get_cache_status(iint, func);
  195. goto out_digsig;
  196. }
  197. template_desc = ima_template_desc_current();
  198. if ((action & IMA_APPRAISE_SUBMASK) ||
  199. strcmp(template_desc->name, IMA_TEMPLATE_IMA_NAME) != 0)
  200. /* read 'security.ima' */
  201. xattr_len = ima_read_xattr(file_dentry(file), &xattr_value);
  202. hash_algo = ima_get_hash_algo(xattr_value, xattr_len);
  203. rc = ima_collect_measurement(iint, file, buf, size, hash_algo);
  204. if (rc != 0) {
  205. if (file->f_flags & O_DIRECT)
  206. rc = (iint->flags & IMA_PERMIT_DIRECTIO) ? 0 : -EACCES;
  207. goto out_digsig;
  208. }
  209. if (!pathbuf) /* ima_rdwr_violation possibly pre-fetched */
  210. pathname = ima_d_path(&file->f_path, &pathbuf, filename);
  211. if (action & IMA_MEASURE)
  212. ima_store_measurement(iint, file, pathname,
  213. xattr_value, xattr_len, pcr);
  214. if (action & IMA_APPRAISE_SUBMASK)
  215. rc = ima_appraise_measurement(func, iint, file, pathname,
  216. xattr_value, xattr_len, opened);
  217. if (action & IMA_AUDIT)
  218. ima_audit_measurement(iint, pathname);
  219. out_digsig:
  220. if ((mask & MAY_WRITE) && (iint->flags & IMA_DIGSIG) &&
  221. !(iint->flags & IMA_NEW_FILE))
  222. rc = -EACCES;
  223. kfree(xattr_value);
  224. out_free:
  225. if (pathbuf)
  226. __putname(pathbuf);
  227. out:
  228. inode_unlock(inode);
  229. if ((rc && must_appraise) && (ima_appraise & IMA_APPRAISE_ENFORCE))
  230. return -EACCES;
  231. return 0;
  232. }
  233. /**
  234. * ima_file_mmap - based on policy, collect/store measurement.
  235. * @file: pointer to the file to be measured (May be NULL)
  236. * @prot: contains the protection that will be applied by the kernel.
  237. *
  238. * Measure files being mmapped executable based on the ima_must_measure()
  239. * policy decision.
  240. *
  241. * On success return 0. On integrity appraisal error, assuming the file
  242. * is in policy and IMA-appraisal is in enforcing mode, return -EACCES.
  243. */
  244. int ima_file_mmap(struct file *file, unsigned long prot)
  245. {
  246. if (file && (prot & PROT_EXEC))
  247. return process_measurement(file, NULL, 0, MAY_EXEC,
  248. MMAP_CHECK, 0);
  249. return 0;
  250. }
  251. /**
  252. * ima_bprm_check - based on policy, collect/store measurement.
  253. * @bprm: contains the linux_binprm structure
  254. *
  255. * The OS protects against an executable file, already open for write,
  256. * from being executed in deny_write_access() and an executable file,
  257. * already open for execute, from being modified in get_write_access().
  258. * So we can be certain that what we verify and measure here is actually
  259. * what is being executed.
  260. *
  261. * On success return 0. On integrity appraisal error, assuming the file
  262. * is in policy and IMA-appraisal is in enforcing mode, return -EACCES.
  263. */
  264. int ima_bprm_check(struct linux_binprm *bprm)
  265. {
  266. return process_measurement(bprm->file, NULL, 0, MAY_EXEC,
  267. BPRM_CHECK, 0);
  268. }
  269. /**
  270. * ima_path_check - based on policy, collect/store measurement.
  271. * @file: pointer to the file to be measured
  272. * @mask: contains MAY_READ, MAY_WRITE or MAY_EXECUTE
  273. *
  274. * Measure files based on the ima_must_measure() policy decision.
  275. *
  276. * On success return 0. On integrity appraisal error, assuming the file
  277. * is in policy and IMA-appraisal is in enforcing mode, return -EACCES.
  278. */
  279. int ima_file_check(struct file *file, int mask, int opened)
  280. {
  281. return process_measurement(file, NULL, 0,
  282. mask & (MAY_READ | MAY_WRITE | MAY_EXEC),
  283. FILE_CHECK, opened);
  284. }
  285. EXPORT_SYMBOL_GPL(ima_file_check);
  286. /**
  287. * ima_post_path_mknod - mark as a new inode
  288. * @dentry: newly created dentry
  289. *
  290. * Mark files created via the mknodat syscall as new, so that the
  291. * file data can be written later.
  292. */
  293. void ima_post_path_mknod(struct dentry *dentry)
  294. {
  295. struct integrity_iint_cache *iint;
  296. struct inode *inode = dentry->d_inode;
  297. int must_appraise;
  298. must_appraise = ima_must_appraise(inode, MAY_ACCESS, FILE_CHECK);
  299. if (!must_appraise)
  300. return;
  301. iint = integrity_inode_get(inode);
  302. if (iint)
  303. iint->flags |= IMA_NEW_FILE;
  304. }
  305. /**
  306. * ima_read_file - pre-measure/appraise hook decision based on policy
  307. * @file: pointer to the file to be measured/appraised/audit
  308. * @read_id: caller identifier
  309. *
  310. * Permit reading a file based on policy. The policy rules are written
  311. * in terms of the policy identifier. Appraising the integrity of
  312. * a file requires a file descriptor.
  313. *
  314. * For permission return 0, otherwise return -EACCES.
  315. */
  316. int ima_read_file(struct file *file, enum kernel_read_file_id read_id)
  317. {
  318. if (!file && read_id == READING_MODULE) {
  319. #ifndef CONFIG_MODULE_SIG_FORCE
  320. if ((ima_appraise & IMA_APPRAISE_MODULES) &&
  321. (ima_appraise & IMA_APPRAISE_ENFORCE))
  322. return -EACCES; /* INTEGRITY_UNKNOWN */
  323. #endif
  324. return 0; /* We rely on module signature checking */
  325. }
  326. return 0;
  327. }
  328. static int read_idmap[READING_MAX_ID] = {
  329. [READING_FIRMWARE] = FIRMWARE_CHECK,
  330. [READING_MODULE] = MODULE_CHECK,
  331. [READING_KEXEC_IMAGE] = KEXEC_KERNEL_CHECK,
  332. [READING_KEXEC_INITRAMFS] = KEXEC_INITRAMFS_CHECK,
  333. [READING_POLICY] = POLICY_CHECK
  334. };
  335. /**
  336. * ima_post_read_file - in memory collect/appraise/audit measurement
  337. * @file: pointer to the file to be measured/appraised/audit
  338. * @buf: pointer to in memory file contents
  339. * @size: size of in memory file contents
  340. * @read_id: caller identifier
  341. *
  342. * Measure/appraise/audit in memory file based on policy. Policy rules
  343. * are written in terms of a policy identifier.
  344. *
  345. * On success return 0. On integrity appraisal error, assuming the file
  346. * is in policy and IMA-appraisal is in enforcing mode, return -EACCES.
  347. */
  348. int ima_post_read_file(struct file *file, void *buf, loff_t size,
  349. enum kernel_read_file_id read_id)
  350. {
  351. enum ima_hooks func;
  352. if (!file && read_id == READING_FIRMWARE) {
  353. if ((ima_appraise & IMA_APPRAISE_FIRMWARE) &&
  354. (ima_appraise & IMA_APPRAISE_ENFORCE))
  355. return -EACCES; /* INTEGRITY_UNKNOWN */
  356. return 0;
  357. }
  358. if (!file && read_id == READING_MODULE) /* MODULE_SIG_FORCE enabled */
  359. return 0;
  360. if (!file || !buf || size == 0) { /* should never happen */
  361. if (ima_appraise & IMA_APPRAISE_ENFORCE)
  362. return -EACCES;
  363. return 0;
  364. }
  365. func = read_idmap[read_id] ?: FILE_CHECK;
  366. return process_measurement(file, buf, size, MAY_READ, func, 0);
  367. }
  368. static int __init init_ima(void)
  369. {
  370. int error;
  371. hash_setup(CONFIG_IMA_DEFAULT_HASH);
  372. error = ima_init();
  373. if (!error) {
  374. ima_initialized = 1;
  375. ima_update_policy_flag();
  376. }
  377. return error;
  378. }
  379. late_initcall(init_ima); /* Start IMA after the TPM is available */
  380. MODULE_DESCRIPTION("Integrity Measurement Architecture");
  381. MODULE_LICENSE("GPL");