ima_crypto.c 15 KB

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