ima_crypto.c 15 KB

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