ima_main.c 13 KB

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