ima_main.c 12 KB

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