ima_crypto.c 15 KB

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