ima_crypto.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606
  1. /*
  2. * Copyright (C) 2005,2006,2007,2008 IBM Corporation
  3. *
  4. * Authors:
  5. * Mimi Zohar <zohar@us.ibm.com>
  6. * Kylene Hall <kjhall@us.ibm.com>
  7. *
  8. * This program is free software; you can redistribute it and/or modify
  9. * it under the terms of the GNU General Public License as published by
  10. * the Free Software Foundation, version 2 of the License.
  11. *
  12. * File: ima_crypto.c
  13. * Calculates md5/sha1 file hash, template hash, boot-aggreate hash
  14. */
  15. #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
  16. #include <linux/kernel.h>
  17. #include <linux/moduleparam.h>
  18. #include <linux/ratelimit.h>
  19. #include <linux/file.h>
  20. #include <linux/crypto.h>
  21. #include <linux/scatterlist.h>
  22. #include <linux/err.h>
  23. #include <linux/slab.h>
  24. #include <crypto/hash.h>
  25. #include <crypto/hash_info.h>
  26. #include "ima.h"
  27. struct ahash_completion {
  28. struct completion completion;
  29. int err;
  30. };
  31. /* minimum file size for ahash use */
  32. static unsigned long ima_ahash_minsize;
  33. module_param_named(ahash_minsize, ima_ahash_minsize, ulong, 0644);
  34. MODULE_PARM_DESC(ahash_minsize, "Minimum file size for ahash use");
  35. /* default is 0 - 1 page. */
  36. static int ima_maxorder;
  37. static unsigned int ima_bufsize = PAGE_SIZE;
  38. static int param_set_bufsize(const char *val, const struct kernel_param *kp)
  39. {
  40. unsigned long long size;
  41. int order;
  42. size = memparse(val, NULL);
  43. order = get_order(size);
  44. if (order >= MAX_ORDER)
  45. return -EINVAL;
  46. ima_maxorder = order;
  47. ima_bufsize = PAGE_SIZE << order;
  48. return 0;
  49. }
  50. static struct kernel_param_ops param_ops_bufsize = {
  51. .set = param_set_bufsize,
  52. .get = param_get_uint,
  53. };
  54. #define param_check_bufsize(name, p) __param_check(name, p, unsigned int)
  55. module_param_named(ahash_bufsize, ima_bufsize, bufsize, 0644);
  56. MODULE_PARM_DESC(ahash_bufsize, "Maximum ahash buffer size");
  57. static struct crypto_shash *ima_shash_tfm;
  58. static struct crypto_ahash *ima_ahash_tfm;
  59. /**
  60. * ima_kernel_read - read file content
  61. *
  62. * This is a function for reading file content instead of kernel_read().
  63. * It does not perform locking checks to ensure it cannot be blocked.
  64. * It does not perform security checks because it is irrelevant for IMA.
  65. *
  66. */
  67. static int ima_kernel_read(struct file *file, loff_t offset,
  68. char *addr, unsigned long count)
  69. {
  70. mm_segment_t old_fs;
  71. char __user *buf = addr;
  72. ssize_t ret;
  73. if (!(file->f_mode & FMODE_READ))
  74. return -EBADF;
  75. if (!file->f_op->read && !file->f_op->aio_read)
  76. return -EINVAL;
  77. old_fs = get_fs();
  78. set_fs(get_ds());
  79. if (file->f_op->read)
  80. ret = file->f_op->read(file, buf, count, &offset);
  81. else
  82. ret = do_sync_read(file, buf, count, &offset);
  83. set_fs(old_fs);
  84. return ret;
  85. }
  86. int ima_init_crypto(void)
  87. {
  88. long rc;
  89. ima_shash_tfm = crypto_alloc_shash(hash_algo_name[ima_hash_algo], 0, 0);
  90. if (IS_ERR(ima_shash_tfm)) {
  91. rc = PTR_ERR(ima_shash_tfm);
  92. pr_err("Can not allocate %s (reason: %ld)\n",
  93. hash_algo_name[ima_hash_algo], rc);
  94. return rc;
  95. }
  96. return 0;
  97. }
  98. static struct crypto_shash *ima_alloc_tfm(enum hash_algo algo)
  99. {
  100. struct crypto_shash *tfm = ima_shash_tfm;
  101. int rc;
  102. if (algo != ima_hash_algo && algo < HASH_ALGO__LAST) {
  103. tfm = crypto_alloc_shash(hash_algo_name[algo], 0, 0);
  104. if (IS_ERR(tfm)) {
  105. rc = PTR_ERR(tfm);
  106. pr_err("Can not allocate %s (reason: %d)\n",
  107. hash_algo_name[algo], rc);
  108. }
  109. }
  110. return tfm;
  111. }
  112. static void ima_free_tfm(struct crypto_shash *tfm)
  113. {
  114. if (tfm != ima_shash_tfm)
  115. crypto_free_shash(tfm);
  116. }
  117. /**
  118. * ima_alloc_pages() - Allocate contiguous pages.
  119. * @max_size: Maximum amount of memory to allocate.
  120. * @allocated_size: Returned size of actual allocation.
  121. * @last_warn: Should the min_size allocation warn or not.
  122. *
  123. * Tries to do opportunistic allocation for memory first trying to allocate
  124. * max_size amount of memory and then splitting that until zero order is
  125. * reached. Allocation is tried without generating allocation warnings unless
  126. * last_warn is set. Last_warn set affects only last allocation of zero order.
  127. *
  128. * By default, ima_maxorder is 0 and it is equivalent to kmalloc(GFP_KERNEL)
  129. *
  130. * Return pointer to allocated memory, or NULL on failure.
  131. */
  132. static void *ima_alloc_pages(loff_t max_size, size_t *allocated_size,
  133. int last_warn)
  134. {
  135. void *ptr;
  136. int order = ima_maxorder;
  137. gfp_t gfp_mask = __GFP_WAIT | __GFP_NOWARN | __GFP_NORETRY;
  138. if (order)
  139. order = min(get_order(max_size), order);
  140. for (; order; order--) {
  141. ptr = (void *)__get_free_pages(gfp_mask, order);
  142. if (ptr) {
  143. *allocated_size = PAGE_SIZE << order;
  144. return ptr;
  145. }
  146. }
  147. /* order is zero - one page */
  148. gfp_mask = GFP_KERNEL;
  149. if (!last_warn)
  150. gfp_mask |= __GFP_NOWARN;
  151. ptr = (void *)__get_free_pages(gfp_mask, 0);
  152. if (ptr) {
  153. *allocated_size = PAGE_SIZE;
  154. return ptr;
  155. }
  156. *allocated_size = 0;
  157. return NULL;
  158. }
  159. /**
  160. * ima_free_pages() - Free pages allocated by ima_alloc_pages().
  161. * @ptr: Pointer to allocated pages.
  162. * @size: Size of allocated buffer.
  163. */
  164. static void ima_free_pages(void *ptr, size_t size)
  165. {
  166. if (!ptr)
  167. return;
  168. free_pages((unsigned long)ptr, get_order(size));
  169. }
  170. static struct crypto_ahash *ima_alloc_atfm(enum hash_algo algo)
  171. {
  172. struct crypto_ahash *tfm = ima_ahash_tfm;
  173. int rc;
  174. if ((algo != ima_hash_algo && algo < HASH_ALGO__LAST) || !tfm) {
  175. tfm = crypto_alloc_ahash(hash_algo_name[algo], 0, 0);
  176. if (!IS_ERR(tfm)) {
  177. if (algo == ima_hash_algo)
  178. ima_ahash_tfm = tfm;
  179. } else {
  180. rc = PTR_ERR(tfm);
  181. pr_err("Can not allocate %s (reason: %d)\n",
  182. hash_algo_name[algo], rc);
  183. }
  184. }
  185. return tfm;
  186. }
  187. static void ima_free_atfm(struct crypto_ahash *tfm)
  188. {
  189. if (tfm != ima_ahash_tfm)
  190. crypto_free_ahash(tfm);
  191. }
  192. static void ahash_complete(struct crypto_async_request *req, int err)
  193. {
  194. struct ahash_completion *res = req->data;
  195. if (err == -EINPROGRESS)
  196. return;
  197. res->err = err;
  198. complete(&res->completion);
  199. }
  200. static int ahash_wait(int err, struct ahash_completion *res)
  201. {
  202. switch (err) {
  203. case 0:
  204. break;
  205. case -EINPROGRESS:
  206. case -EBUSY:
  207. wait_for_completion(&res->completion);
  208. reinit_completion(&res->completion);
  209. err = res->err;
  210. /* fall through */
  211. default:
  212. pr_crit_ratelimited("ahash calculation failed: err: %d\n", err);
  213. }
  214. return err;
  215. }
  216. static int ima_calc_file_hash_atfm(struct file *file,
  217. struct ima_digest_data *hash,
  218. struct crypto_ahash *tfm)
  219. {
  220. loff_t i_size, offset;
  221. char *rbuf[2] = { NULL, };
  222. int rc, read = 0, rbuf_len, active = 0, ahash_rc = 0;
  223. struct ahash_request *req;
  224. struct scatterlist sg[1];
  225. struct ahash_completion res;
  226. size_t rbuf_size[2];
  227. hash->length = crypto_ahash_digestsize(tfm);
  228. req = ahash_request_alloc(tfm, GFP_KERNEL);
  229. if (!req)
  230. return -ENOMEM;
  231. init_completion(&res.completion);
  232. ahash_request_set_callback(req, CRYPTO_TFM_REQ_MAY_BACKLOG |
  233. CRYPTO_TFM_REQ_MAY_SLEEP,
  234. ahash_complete, &res);
  235. rc = ahash_wait(crypto_ahash_init(req), &res);
  236. if (rc)
  237. goto out1;
  238. i_size = i_size_read(file_inode(file));
  239. if (i_size == 0)
  240. goto out2;
  241. /*
  242. * Try to allocate maximum size of memory.
  243. * Fail if even a single page cannot be allocated.
  244. */
  245. rbuf[0] = ima_alloc_pages(i_size, &rbuf_size[0], 1);
  246. if (!rbuf[0]) {
  247. rc = -ENOMEM;
  248. goto out1;
  249. }
  250. /* Only allocate one buffer if that is enough. */
  251. if (i_size > rbuf_size[0]) {
  252. /*
  253. * Try to allocate secondary buffer. If that fails fallback to
  254. * using single buffering. Use previous memory allocation size
  255. * as baseline for possible allocation size.
  256. */
  257. rbuf[1] = ima_alloc_pages(i_size - rbuf_size[0],
  258. &rbuf_size[1], 0);
  259. }
  260. if (!(file->f_mode & FMODE_READ)) {
  261. file->f_mode |= FMODE_READ;
  262. read = 1;
  263. }
  264. for (offset = 0; offset < i_size; offset += rbuf_len) {
  265. if (!rbuf[1] && offset) {
  266. /* Not using two buffers, and it is not the first
  267. * read/request, wait for the completion of the
  268. * previous ahash_update() request.
  269. */
  270. rc = ahash_wait(ahash_rc, &res);
  271. if (rc)
  272. goto out3;
  273. }
  274. /* read buffer */
  275. rbuf_len = min_t(loff_t, i_size - offset, rbuf_size[active]);
  276. rc = ima_kernel_read(file, offset, rbuf[active], rbuf_len);
  277. if (rc != rbuf_len)
  278. goto out3;
  279. if (rbuf[1] && offset) {
  280. /* Using two buffers, and it is not the first
  281. * read/request, wait for the completion of the
  282. * previous ahash_update() request.
  283. */
  284. rc = ahash_wait(ahash_rc, &res);
  285. if (rc)
  286. goto out3;
  287. }
  288. sg_init_one(&sg[0], rbuf[active], rbuf_len);
  289. ahash_request_set_crypt(req, sg, NULL, rbuf_len);
  290. ahash_rc = crypto_ahash_update(req);
  291. if (rbuf[1])
  292. active = !active; /* swap buffers, if we use two */
  293. }
  294. /* wait for the last update request to complete */
  295. rc = ahash_wait(ahash_rc, &res);
  296. out3:
  297. if (read)
  298. file->f_mode &= ~FMODE_READ;
  299. ima_free_pages(rbuf[0], rbuf_size[0]);
  300. ima_free_pages(rbuf[1], rbuf_size[1]);
  301. out2:
  302. if (!rc) {
  303. ahash_request_set_crypt(req, NULL, hash->digest, 0);
  304. rc = ahash_wait(crypto_ahash_final(req), &res);
  305. }
  306. out1:
  307. ahash_request_free(req);
  308. return rc;
  309. }
  310. static int ima_calc_file_ahash(struct file *file, struct ima_digest_data *hash)
  311. {
  312. struct crypto_ahash *tfm;
  313. int rc;
  314. tfm = ima_alloc_atfm(hash->algo);
  315. if (IS_ERR(tfm))
  316. return PTR_ERR(tfm);
  317. rc = ima_calc_file_hash_atfm(file, hash, tfm);
  318. ima_free_atfm(tfm);
  319. return rc;
  320. }
  321. static int ima_calc_file_hash_tfm(struct file *file,
  322. struct ima_digest_data *hash,
  323. struct crypto_shash *tfm)
  324. {
  325. loff_t i_size, offset = 0;
  326. char *rbuf;
  327. int rc, read = 0;
  328. struct {
  329. struct shash_desc shash;
  330. char ctx[crypto_shash_descsize(tfm)];
  331. } desc;
  332. desc.shash.tfm = tfm;
  333. desc.shash.flags = 0;
  334. hash->length = crypto_shash_digestsize(tfm);
  335. rc = crypto_shash_init(&desc.shash);
  336. if (rc != 0)
  337. return rc;
  338. i_size = i_size_read(file_inode(file));
  339. if (i_size == 0)
  340. goto out;
  341. rbuf = kzalloc(PAGE_SIZE, GFP_KERNEL);
  342. if (!rbuf)
  343. return -ENOMEM;
  344. if (!(file->f_mode & FMODE_READ)) {
  345. file->f_mode |= FMODE_READ;
  346. read = 1;
  347. }
  348. while (offset < i_size) {
  349. int rbuf_len;
  350. rbuf_len = ima_kernel_read(file, offset, rbuf, PAGE_SIZE);
  351. if (rbuf_len < 0) {
  352. rc = rbuf_len;
  353. break;
  354. }
  355. if (rbuf_len == 0)
  356. break;
  357. offset += rbuf_len;
  358. rc = crypto_shash_update(&desc.shash, rbuf, rbuf_len);
  359. if (rc)
  360. break;
  361. }
  362. if (read)
  363. file->f_mode &= ~FMODE_READ;
  364. kfree(rbuf);
  365. out:
  366. if (!rc)
  367. rc = crypto_shash_final(&desc.shash, hash->digest);
  368. return rc;
  369. }
  370. static int ima_calc_file_shash(struct file *file, struct ima_digest_data *hash)
  371. {
  372. struct crypto_shash *tfm;
  373. int rc;
  374. tfm = ima_alloc_tfm(hash->algo);
  375. if (IS_ERR(tfm))
  376. return PTR_ERR(tfm);
  377. rc = ima_calc_file_hash_tfm(file, hash, tfm);
  378. ima_free_tfm(tfm);
  379. return rc;
  380. }
  381. /*
  382. * ima_calc_file_hash - calculate file hash
  383. *
  384. * Asynchronous hash (ahash) allows using HW acceleration for calculating
  385. * a hash. ahash performance varies for different data sizes on different
  386. * crypto accelerators. shash performance might be better for smaller files.
  387. * The 'ima.ahash_minsize' module parameter allows specifying the best
  388. * minimum file size for using ahash on the system.
  389. *
  390. * If the ima.ahash_minsize parameter is not specified, this function uses
  391. * shash for the hash calculation. If ahash fails, it falls back to using
  392. * shash.
  393. */
  394. int ima_calc_file_hash(struct file *file, struct ima_digest_data *hash)
  395. {
  396. loff_t i_size;
  397. int rc;
  398. i_size = i_size_read(file_inode(file));
  399. if (ima_ahash_minsize && i_size >= ima_ahash_minsize) {
  400. rc = ima_calc_file_ahash(file, hash);
  401. if (!rc)
  402. return 0;
  403. }
  404. return ima_calc_file_shash(file, hash);
  405. }
  406. /*
  407. * Calculate the hash of template data
  408. */
  409. static int ima_calc_field_array_hash_tfm(struct ima_field_data *field_data,
  410. struct ima_template_desc *td,
  411. int num_fields,
  412. struct ima_digest_data *hash,
  413. struct crypto_shash *tfm)
  414. {
  415. struct {
  416. struct shash_desc shash;
  417. char ctx[crypto_shash_descsize(tfm)];
  418. } desc;
  419. int rc, i;
  420. desc.shash.tfm = tfm;
  421. desc.shash.flags = 0;
  422. hash->length = crypto_shash_digestsize(tfm);
  423. rc = crypto_shash_init(&desc.shash);
  424. if (rc != 0)
  425. return rc;
  426. for (i = 0; i < num_fields; i++) {
  427. u8 buffer[IMA_EVENT_NAME_LEN_MAX + 1] = { 0 };
  428. u8 *data_to_hash = field_data[i].data;
  429. u32 datalen = field_data[i].len;
  430. if (strcmp(td->name, IMA_TEMPLATE_IMA_NAME) != 0) {
  431. rc = crypto_shash_update(&desc.shash,
  432. (const u8 *) &field_data[i].len,
  433. sizeof(field_data[i].len));
  434. if (rc)
  435. break;
  436. } else if (strcmp(td->fields[i]->field_id, "n") == 0) {
  437. memcpy(buffer, data_to_hash, datalen);
  438. data_to_hash = buffer;
  439. datalen = IMA_EVENT_NAME_LEN_MAX + 1;
  440. }
  441. rc = crypto_shash_update(&desc.shash, data_to_hash, datalen);
  442. if (rc)
  443. break;
  444. }
  445. if (!rc)
  446. rc = crypto_shash_final(&desc.shash, hash->digest);
  447. return rc;
  448. }
  449. int ima_calc_field_array_hash(struct ima_field_data *field_data,
  450. struct ima_template_desc *desc, int num_fields,
  451. struct ima_digest_data *hash)
  452. {
  453. struct crypto_shash *tfm;
  454. int rc;
  455. tfm = ima_alloc_tfm(hash->algo);
  456. if (IS_ERR(tfm))
  457. return PTR_ERR(tfm);
  458. rc = ima_calc_field_array_hash_tfm(field_data, desc, num_fields,
  459. hash, tfm);
  460. ima_free_tfm(tfm);
  461. return rc;
  462. }
  463. static void __init ima_pcrread(int idx, u8 *pcr)
  464. {
  465. if (!ima_used_chip)
  466. return;
  467. if (tpm_pcr_read(TPM_ANY_NUM, idx, pcr) != 0)
  468. pr_err("Error Communicating to TPM chip\n");
  469. }
  470. /*
  471. * Calculate the boot aggregate hash
  472. */
  473. static int __init ima_calc_boot_aggregate_tfm(char *digest,
  474. struct crypto_shash *tfm)
  475. {
  476. u8 pcr_i[TPM_DIGEST_SIZE];
  477. int rc, i;
  478. struct {
  479. struct shash_desc shash;
  480. char ctx[crypto_shash_descsize(tfm)];
  481. } desc;
  482. desc.shash.tfm = tfm;
  483. desc.shash.flags = 0;
  484. rc = crypto_shash_init(&desc.shash);
  485. if (rc != 0)
  486. return rc;
  487. /* cumulative sha1 over tpm registers 0-7 */
  488. for (i = TPM_PCR0; i < TPM_PCR8; i++) {
  489. ima_pcrread(i, pcr_i);
  490. /* now accumulate with current aggregate */
  491. rc = crypto_shash_update(&desc.shash, pcr_i, TPM_DIGEST_SIZE);
  492. }
  493. if (!rc)
  494. crypto_shash_final(&desc.shash, digest);
  495. return rc;
  496. }
  497. int __init ima_calc_boot_aggregate(struct ima_digest_data *hash)
  498. {
  499. struct crypto_shash *tfm;
  500. int rc;
  501. tfm = ima_alloc_tfm(hash->algo);
  502. if (IS_ERR(tfm))
  503. return PTR_ERR(tfm);
  504. hash->length = crypto_shash_digestsize(tfm);
  505. rc = ima_calc_boot_aggregate_tfm(hash->digest, tfm);
  506. ima_free_tfm(tfm);
  507. return rc;
  508. }