ima_main.c 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566
  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 <linux/iversion.h>
  29. #include <linux/fs.h>
  30. #include "ima.h"
  31. #ifdef CONFIG_IMA_APPRAISE
  32. int ima_appraise = IMA_APPRAISE_ENFORCE;
  33. #else
  34. int ima_appraise;
  35. #endif
  36. int ima_hash_algo = HASH_ALGO_SHA1;
  37. static int hash_setup_done;
  38. static int __init hash_setup(char *str)
  39. {
  40. struct ima_template_desc *template_desc = ima_template_desc_current();
  41. int i;
  42. if (hash_setup_done)
  43. return 1;
  44. if (strcmp(template_desc->name, IMA_TEMPLATE_IMA_NAME) == 0) {
  45. if (strncmp(str, "sha1", 4) == 0)
  46. ima_hash_algo = HASH_ALGO_SHA1;
  47. else if (strncmp(str, "md5", 3) == 0)
  48. ima_hash_algo = HASH_ALGO_MD5;
  49. else
  50. return 1;
  51. goto out;
  52. }
  53. i = match_string(hash_algo_name, HASH_ALGO__LAST, str);
  54. if (i < 0)
  55. return 1;
  56. ima_hash_algo = i;
  57. out:
  58. hash_setup_done = 1;
  59. return 1;
  60. }
  61. __setup("ima_hash=", hash_setup);
  62. /*
  63. * ima_rdwr_violation_check
  64. *
  65. * Only invalidate the PCR for measured files:
  66. * - Opening a file for write when already open for read,
  67. * results in a time of measure, time of use (ToMToU) error.
  68. * - Opening a file for read when already open for write,
  69. * could result in a file measurement error.
  70. *
  71. */
  72. static void ima_rdwr_violation_check(struct file *file,
  73. struct integrity_iint_cache *iint,
  74. int must_measure,
  75. char **pathbuf,
  76. const char **pathname,
  77. char *filename)
  78. {
  79. struct inode *inode = file_inode(file);
  80. fmode_t mode = file->f_mode;
  81. bool send_tomtou = false, send_writers = false;
  82. if (mode & FMODE_WRITE) {
  83. if (atomic_read(&inode->i_readcount) && IS_IMA(inode)) {
  84. if (!iint)
  85. iint = integrity_iint_find(inode);
  86. /* IMA_MEASURE is set from reader side */
  87. if (iint && test_bit(IMA_MUST_MEASURE,
  88. &iint->atomic_flags))
  89. send_tomtou = true;
  90. }
  91. } else {
  92. if (must_measure)
  93. set_bit(IMA_MUST_MEASURE, &iint->atomic_flags);
  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. bool update;
  112. if (!(mode & FMODE_WRITE))
  113. return;
  114. mutex_lock(&iint->mutex);
  115. if (atomic_read(&inode->i_writecount) == 1) {
  116. update = test_and_clear_bit(IMA_UPDATE_XATTR,
  117. &iint->atomic_flags);
  118. if (!IS_I_VERSION(inode) ||
  119. !inode_eq_iversion(inode, iint->version) ||
  120. (iint->flags & IMA_NEW_FILE)) {
  121. iint->flags &= ~(IMA_DONE_MASK | IMA_NEW_FILE);
  122. iint->measured_pcrs = 0;
  123. if (update)
  124. ima_update_xattr(iint, file);
  125. }
  126. }
  127. mutex_unlock(&iint->mutex);
  128. }
  129. /**
  130. * ima_file_free - called on __fput()
  131. * @file: pointer to file structure being freed
  132. *
  133. * Flag files that changed, based on i_version
  134. */
  135. void ima_file_free(struct file *file)
  136. {
  137. struct inode *inode = file_inode(file);
  138. struct integrity_iint_cache *iint;
  139. if (!ima_policy_flag || !S_ISREG(inode->i_mode))
  140. return;
  141. iint = integrity_iint_find(inode);
  142. if (!iint)
  143. return;
  144. ima_check_last_writer(iint, inode, file);
  145. }
  146. static int process_measurement(struct file *file, const struct cred *cred,
  147. u32 secid, char *buf, loff_t size, int mask,
  148. enum ima_hooks func)
  149. {
  150. struct inode *inode = file_inode(file);
  151. struct integrity_iint_cache *iint = NULL;
  152. struct ima_template_desc *template_desc;
  153. char *pathbuf = NULL;
  154. char filename[NAME_MAX];
  155. const char *pathname = NULL;
  156. int rc = 0, action, must_appraise = 0;
  157. int pcr = CONFIG_IMA_MEASURE_PCR_IDX;
  158. struct evm_ima_xattr_data *xattr_value = NULL;
  159. int xattr_len = 0;
  160. bool violation_check;
  161. enum hash_algo hash_algo;
  162. if (!ima_policy_flag || !S_ISREG(inode->i_mode))
  163. return 0;
  164. /* Return an IMA_MEASURE, IMA_APPRAISE, IMA_AUDIT action
  165. * bitmask based on the appraise/audit/measurement policy.
  166. * Included is the appraise submask.
  167. */
  168. action = ima_get_action(inode, cred, secid, mask, func, &pcr);
  169. violation_check = ((func == FILE_CHECK || func == MMAP_CHECK) &&
  170. (ima_policy_flag & IMA_MEASURE));
  171. if (!action && !violation_check)
  172. return 0;
  173. must_appraise = action & IMA_APPRAISE;
  174. /* Is the appraise rule hook specific? */
  175. if (action & IMA_FILE_APPRAISE)
  176. func = FILE_CHECK;
  177. inode_lock(inode);
  178. if (action) {
  179. iint = integrity_inode_get(inode);
  180. if (!iint)
  181. rc = -ENOMEM;
  182. }
  183. if (!rc && violation_check)
  184. ima_rdwr_violation_check(file, iint, action & IMA_MEASURE,
  185. &pathbuf, &pathname, filename);
  186. inode_unlock(inode);
  187. if (rc)
  188. goto out;
  189. if (!action)
  190. goto out;
  191. mutex_lock(&iint->mutex);
  192. if (test_and_clear_bit(IMA_CHANGE_ATTR, &iint->atomic_flags))
  193. /* reset appraisal flags if ima_inode_post_setattr was called */
  194. iint->flags &= ~(IMA_APPRAISE | IMA_APPRAISED |
  195. IMA_APPRAISE_SUBMASK | IMA_APPRAISED_SUBMASK |
  196. IMA_ACTION_FLAGS);
  197. /*
  198. * Re-evaulate the file if either the xattr has changed or the
  199. * kernel has no way of detecting file change on the filesystem.
  200. * (Limited to privileged mounted filesystems.)
  201. */
  202. if (test_and_clear_bit(IMA_CHANGE_XATTR, &iint->atomic_flags) ||
  203. ((inode->i_sb->s_iflags & SB_I_IMA_UNVERIFIABLE_SIGNATURE) &&
  204. !(inode->i_sb->s_iflags & SB_I_UNTRUSTED_MOUNTER) &&
  205. !(action & IMA_FAIL_UNVERIFIABLE_SIGS))) {
  206. iint->flags &= ~IMA_DONE_MASK;
  207. iint->measured_pcrs = 0;
  208. }
  209. /* Determine if already appraised/measured based on bitmask
  210. * (IMA_MEASURE, IMA_MEASURED, IMA_XXXX_APPRAISE, IMA_XXXX_APPRAISED,
  211. * IMA_AUDIT, IMA_AUDITED)
  212. */
  213. iint->flags |= action;
  214. action &= IMA_DO_MASK;
  215. action &= ~((iint->flags & (IMA_DONE_MASK ^ IMA_MEASURED)) >> 1);
  216. /* If target pcr is already measured, unset IMA_MEASURE action */
  217. if ((action & IMA_MEASURE) && (iint->measured_pcrs & (0x1 << pcr)))
  218. action ^= IMA_MEASURE;
  219. /* HASH sets the digital signature and update flags, nothing else */
  220. if ((action & IMA_HASH) &&
  221. !(test_bit(IMA_DIGSIG, &iint->atomic_flags))) {
  222. xattr_len = ima_read_xattr(file_dentry(file), &xattr_value);
  223. if ((xattr_value && xattr_len > 2) &&
  224. (xattr_value->type == EVM_IMA_XATTR_DIGSIG))
  225. set_bit(IMA_DIGSIG, &iint->atomic_flags);
  226. iint->flags |= IMA_HASHED;
  227. action ^= IMA_HASH;
  228. set_bit(IMA_UPDATE_XATTR, &iint->atomic_flags);
  229. }
  230. /* Nothing to do, just return existing appraised status */
  231. if (!action) {
  232. if (must_appraise)
  233. rc = ima_get_cache_status(iint, func);
  234. goto out_locked;
  235. }
  236. template_desc = ima_template_desc_current();
  237. if ((action & IMA_APPRAISE_SUBMASK) ||
  238. strcmp(template_desc->name, IMA_TEMPLATE_IMA_NAME) != 0)
  239. /* read 'security.ima' */
  240. xattr_len = ima_read_xattr(file_dentry(file), &xattr_value);
  241. hash_algo = ima_get_hash_algo(xattr_value, xattr_len);
  242. rc = ima_collect_measurement(iint, file, buf, size, hash_algo);
  243. if (rc != 0 && rc != -EBADF && rc != -EINVAL)
  244. goto out_locked;
  245. if (!pathbuf) /* ima_rdwr_violation possibly pre-fetched */
  246. pathname = ima_d_path(&file->f_path, &pathbuf, filename);
  247. if (action & IMA_MEASURE)
  248. ima_store_measurement(iint, file, pathname,
  249. xattr_value, xattr_len, pcr);
  250. if (rc == 0 && (action & IMA_APPRAISE_SUBMASK)) {
  251. inode_lock(inode);
  252. rc = ima_appraise_measurement(func, iint, file, pathname,
  253. xattr_value, xattr_len);
  254. inode_unlock(inode);
  255. }
  256. if (action & IMA_AUDIT)
  257. ima_audit_measurement(iint, pathname);
  258. if ((file->f_flags & O_DIRECT) && (iint->flags & IMA_PERMIT_DIRECTIO))
  259. rc = 0;
  260. out_locked:
  261. if ((mask & MAY_WRITE) && test_bit(IMA_DIGSIG, &iint->atomic_flags) &&
  262. !(iint->flags & IMA_NEW_FILE))
  263. rc = -EACCES;
  264. mutex_unlock(&iint->mutex);
  265. kfree(xattr_value);
  266. out:
  267. if (pathbuf)
  268. __putname(pathbuf);
  269. if (must_appraise) {
  270. if (rc && (ima_appraise & IMA_APPRAISE_ENFORCE))
  271. return -EACCES;
  272. if (file->f_mode & FMODE_WRITE)
  273. set_bit(IMA_UPDATE_XATTR, &iint->atomic_flags);
  274. }
  275. return 0;
  276. }
  277. /**
  278. * ima_file_mmap - based on policy, collect/store measurement.
  279. * @file: pointer to the file to be measured (May be NULL)
  280. * @prot: contains the protection that will be applied by the kernel.
  281. *
  282. * Measure files being mmapped executable based on the ima_must_measure()
  283. * policy decision.
  284. *
  285. * On success return 0. On integrity appraisal error, assuming the file
  286. * is in policy and IMA-appraisal is in enforcing mode, return -EACCES.
  287. */
  288. int ima_file_mmap(struct file *file, unsigned long prot)
  289. {
  290. u32 secid;
  291. if (file && (prot & PROT_EXEC)) {
  292. security_task_getsecid(current, &secid);
  293. return process_measurement(file, current_cred(), secid, NULL,
  294. 0, MAY_EXEC, MMAP_CHECK);
  295. }
  296. return 0;
  297. }
  298. /**
  299. * ima_bprm_check - based on policy, collect/store measurement.
  300. * @bprm: contains the linux_binprm structure
  301. *
  302. * The OS protects against an executable file, already open for write,
  303. * from being executed in deny_write_access() and an executable file,
  304. * already open for execute, from being modified in get_write_access().
  305. * So we can be certain that what we verify and measure here is actually
  306. * what is being executed.
  307. *
  308. * On success return 0. On integrity appraisal error, assuming the file
  309. * is in policy and IMA-appraisal is in enforcing mode, return -EACCES.
  310. */
  311. int ima_bprm_check(struct linux_binprm *bprm)
  312. {
  313. int ret;
  314. u32 secid;
  315. security_task_getsecid(current, &secid);
  316. ret = process_measurement(bprm->file, current_cred(), secid, NULL, 0,
  317. MAY_EXEC, BPRM_CHECK);
  318. if (ret)
  319. return ret;
  320. security_cred_getsecid(bprm->cred, &secid);
  321. return process_measurement(bprm->file, bprm->cred, secid, NULL, 0,
  322. MAY_EXEC, CREDS_CHECK);
  323. }
  324. /**
  325. * ima_path_check - based on policy, collect/store measurement.
  326. * @file: pointer to the file to be measured
  327. * @mask: contains MAY_READ, MAY_WRITE, MAY_EXEC or MAY_APPEND
  328. *
  329. * Measure files based on the ima_must_measure() policy decision.
  330. *
  331. * On success return 0. On integrity appraisal error, assuming the file
  332. * is in policy and IMA-appraisal is in enforcing mode, return -EACCES.
  333. */
  334. int ima_file_check(struct file *file, int mask)
  335. {
  336. u32 secid;
  337. security_task_getsecid(current, &secid);
  338. return process_measurement(file, current_cred(), secid, NULL, 0,
  339. mask & (MAY_READ | MAY_WRITE | MAY_EXEC |
  340. MAY_APPEND), FILE_CHECK);
  341. }
  342. EXPORT_SYMBOL_GPL(ima_file_check);
  343. /**
  344. * ima_post_path_mknod - mark as a new inode
  345. * @dentry: newly created dentry
  346. *
  347. * Mark files created via the mknodat syscall as new, so that the
  348. * file data can be written later.
  349. */
  350. void ima_post_path_mknod(struct dentry *dentry)
  351. {
  352. struct integrity_iint_cache *iint;
  353. struct inode *inode = dentry->d_inode;
  354. int must_appraise;
  355. must_appraise = ima_must_appraise(inode, MAY_ACCESS, FILE_CHECK);
  356. if (!must_appraise)
  357. return;
  358. iint = integrity_inode_get(inode);
  359. if (iint)
  360. iint->flags |= IMA_NEW_FILE;
  361. }
  362. /**
  363. * ima_read_file - pre-measure/appraise hook decision based on policy
  364. * @file: pointer to the file to be measured/appraised/audit
  365. * @read_id: caller identifier
  366. *
  367. * Permit reading a file based on policy. The policy rules are written
  368. * in terms of the policy identifier. Appraising the integrity of
  369. * a file requires a file descriptor.
  370. *
  371. * For permission return 0, otherwise return -EACCES.
  372. */
  373. int ima_read_file(struct file *file, enum kernel_read_file_id read_id)
  374. {
  375. /*
  376. * READING_FIRMWARE_PREALLOC_BUFFER
  377. *
  378. * Do devices using pre-allocated memory run the risk of the
  379. * firmware being accessible to the device prior to the completion
  380. * of IMA's signature verification any more than when using two
  381. * buffers?
  382. */
  383. return 0;
  384. }
  385. static int read_idmap[READING_MAX_ID] = {
  386. [READING_FIRMWARE] = FIRMWARE_CHECK,
  387. [READING_FIRMWARE_PREALLOC_BUFFER] = FIRMWARE_CHECK,
  388. [READING_MODULE] = MODULE_CHECK,
  389. [READING_KEXEC_IMAGE] = KEXEC_KERNEL_CHECK,
  390. [READING_KEXEC_INITRAMFS] = KEXEC_INITRAMFS_CHECK,
  391. [READING_POLICY] = POLICY_CHECK
  392. };
  393. /**
  394. * ima_post_read_file - in memory collect/appraise/audit measurement
  395. * @file: pointer to the file to be measured/appraised/audit
  396. * @buf: pointer to in memory file contents
  397. * @size: size of in memory file contents
  398. * @read_id: caller identifier
  399. *
  400. * Measure/appraise/audit in memory file based on policy. Policy rules
  401. * are written in terms of a policy identifier.
  402. *
  403. * On success return 0. On integrity appraisal error, assuming the file
  404. * is in policy and IMA-appraisal is in enforcing mode, return -EACCES.
  405. */
  406. int ima_post_read_file(struct file *file, void *buf, loff_t size,
  407. enum kernel_read_file_id read_id)
  408. {
  409. enum ima_hooks func;
  410. u32 secid;
  411. if (!file && read_id == READING_FIRMWARE) {
  412. if ((ima_appraise & IMA_APPRAISE_FIRMWARE) &&
  413. (ima_appraise & IMA_APPRAISE_ENFORCE)) {
  414. pr_err("Prevent firmware loading_store.\n");
  415. return -EACCES; /* INTEGRITY_UNKNOWN */
  416. }
  417. return 0;
  418. }
  419. /* permit signed certs */
  420. if (!file && read_id == READING_X509_CERTIFICATE)
  421. return 0;
  422. if (!file || !buf || size == 0) { /* should never happen */
  423. if (ima_appraise & IMA_APPRAISE_ENFORCE)
  424. return -EACCES;
  425. return 0;
  426. }
  427. func = read_idmap[read_id] ?: FILE_CHECK;
  428. security_task_getsecid(current, &secid);
  429. return process_measurement(file, current_cred(), secid, buf, size,
  430. MAY_READ, func);
  431. }
  432. /**
  433. * ima_load_data - appraise decision based on policy
  434. * @id: kernel load data caller identifier
  435. *
  436. * Callers of this LSM hook can not measure, appraise, or audit the
  437. * data provided by userspace. Enforce policy rules requring a file
  438. * signature (eg. kexec'ed kernel image).
  439. *
  440. * For permission return 0, otherwise return -EACCES.
  441. */
  442. int ima_load_data(enum kernel_load_data_id id)
  443. {
  444. bool sig_enforce;
  445. if ((ima_appraise & IMA_APPRAISE_ENFORCE) != IMA_APPRAISE_ENFORCE)
  446. return 0;
  447. switch (id) {
  448. case LOADING_KEXEC_IMAGE:
  449. if (ima_appraise & IMA_APPRAISE_KEXEC) {
  450. pr_err("impossible to appraise a kernel image without a file descriptor; try using kexec_file_load syscall.\n");
  451. return -EACCES; /* INTEGRITY_UNKNOWN */
  452. }
  453. break;
  454. case LOADING_FIRMWARE:
  455. if (ima_appraise & IMA_APPRAISE_FIRMWARE) {
  456. pr_err("Prevent firmware sysfs fallback loading.\n");
  457. return -EACCES; /* INTEGRITY_UNKNOWN */
  458. }
  459. break;
  460. case LOADING_MODULE:
  461. sig_enforce = is_module_sig_enforced();
  462. if (!sig_enforce && (ima_appraise & IMA_APPRAISE_MODULES)) {
  463. pr_err("impossible to appraise a module without a file descriptor. sig_enforce kernel parameter might help\n");
  464. return -EACCES; /* INTEGRITY_UNKNOWN */
  465. }
  466. default:
  467. break;
  468. }
  469. return 0;
  470. }
  471. static int __init init_ima(void)
  472. {
  473. int error;
  474. ima_init_template_list();
  475. hash_setup(CONFIG_IMA_DEFAULT_HASH);
  476. error = ima_init();
  477. if (error && strcmp(hash_algo_name[ima_hash_algo],
  478. CONFIG_IMA_DEFAULT_HASH) != 0) {
  479. pr_info("Allocating %s failed, going to use default hash algorithm %s\n",
  480. hash_algo_name[ima_hash_algo], CONFIG_IMA_DEFAULT_HASH);
  481. hash_setup_done = 0;
  482. hash_setup(CONFIG_IMA_DEFAULT_HASH);
  483. error = ima_init();
  484. }
  485. if (!error)
  486. ima_update_policy_flag();
  487. return error;
  488. }
  489. late_initcall(init_ima); /* Start IMA after the TPM is available */
  490. MODULE_DESCRIPTION("Integrity Measurement Architecture");
  491. MODULE_LICENSE("GPL");