ima_crypto.c 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882
  1. // SPDX-License-Identifier: GPL-2.0-only
  2. /*
  3. * Copyright (C) 2005,2006,2007,2008 IBM Corporation
  4. *
  5. * Authors:
  6. * Mimi Zohar <[email protected]>
  7. * Kylene Hall <[email protected]>
  8. *
  9. * File: ima_crypto.c
  10. * Calculates md5/sha1 file hash, template hash, boot-aggreate hash
  11. */
  12. #include <linux/kernel.h>
  13. #include <linux/moduleparam.h>
  14. #include <linux/ratelimit.h>
  15. #include <linux/file.h>
  16. #include <linux/crypto.h>
  17. #include <linux/scatterlist.h>
  18. #include <linux/err.h>
  19. #include <linux/slab.h>
  20. #include <crypto/hash.h>
  21. #include "ima.h"
  22. /* minimum file size for ahash use */
  23. static unsigned long ima_ahash_minsize;
  24. module_param_named(ahash_minsize, ima_ahash_minsize, ulong, 0644);
  25. MODULE_PARM_DESC(ahash_minsize, "Minimum file size for ahash use");
  26. /* default is 0 - 1 page. */
  27. static int ima_maxorder;
  28. static unsigned int ima_bufsize = PAGE_SIZE;
  29. static int param_set_bufsize(const char *val, const struct kernel_param *kp)
  30. {
  31. unsigned long long size;
  32. int order;
  33. size = memparse(val, NULL);
  34. order = get_order(size);
  35. if (order >= MAX_ORDER)
  36. return -EINVAL;
  37. ima_maxorder = order;
  38. ima_bufsize = PAGE_SIZE << order;
  39. return 0;
  40. }
  41. static const struct kernel_param_ops param_ops_bufsize = {
  42. .set = param_set_bufsize,
  43. .get = param_get_uint,
  44. };
  45. #define param_check_bufsize(name, p) __param_check(name, p, unsigned int)
  46. module_param_named(ahash_bufsize, ima_bufsize, bufsize, 0644);
  47. MODULE_PARM_DESC(ahash_bufsize, "Maximum ahash buffer size");
  48. static struct crypto_shash *ima_shash_tfm;
  49. static struct crypto_ahash *ima_ahash_tfm;
  50. struct ima_algo_desc {
  51. struct crypto_shash *tfm;
  52. enum hash_algo algo;
  53. };
  54. int ima_sha1_idx __ro_after_init;
  55. int ima_hash_algo_idx __ro_after_init;
  56. /*
  57. * Additional number of slots reserved, as needed, for SHA1
  58. * and IMA default algo.
  59. */
  60. int ima_extra_slots __ro_after_init;
  61. static struct ima_algo_desc *ima_algo_array;
  62. static int __init ima_init_ima_crypto(void)
  63. {
  64. long rc;
  65. ima_shash_tfm = crypto_alloc_shash(hash_algo_name[ima_hash_algo], 0, 0);
  66. if (IS_ERR(ima_shash_tfm)) {
  67. rc = PTR_ERR(ima_shash_tfm);
  68. pr_err("Can not allocate %s (reason: %ld)\n",
  69. hash_algo_name[ima_hash_algo], rc);
  70. return rc;
  71. }
  72. pr_info("Allocated hash algorithm: %s\n",
  73. hash_algo_name[ima_hash_algo]);
  74. return 0;
  75. }
  76. static struct crypto_shash *ima_alloc_tfm(enum hash_algo algo)
  77. {
  78. struct crypto_shash *tfm = ima_shash_tfm;
  79. int rc, i;
  80. if (algo < 0 || algo >= HASH_ALGO__LAST)
  81. algo = ima_hash_algo;
  82. if (algo == ima_hash_algo)
  83. return tfm;
  84. for (i = 0; i < NR_BANKS(ima_tpm_chip) + ima_extra_slots; i++)
  85. if (ima_algo_array[i].tfm && ima_algo_array[i].algo == algo)
  86. return ima_algo_array[i].tfm;
  87. tfm = crypto_alloc_shash(hash_algo_name[algo], 0, 0);
  88. if (IS_ERR(tfm)) {
  89. rc = PTR_ERR(tfm);
  90. pr_err("Can not allocate %s (reason: %d)\n",
  91. hash_algo_name[algo], rc);
  92. }
  93. return tfm;
  94. }
  95. int __init ima_init_crypto(void)
  96. {
  97. enum hash_algo algo;
  98. long rc;
  99. int i;
  100. rc = ima_init_ima_crypto();
  101. if (rc)
  102. return rc;
  103. ima_sha1_idx = -1;
  104. ima_hash_algo_idx = -1;
  105. for (i = 0; i < NR_BANKS(ima_tpm_chip); i++) {
  106. algo = ima_tpm_chip->allocated_banks[i].crypto_id;
  107. if (algo == HASH_ALGO_SHA1)
  108. ima_sha1_idx = i;
  109. if (algo == ima_hash_algo)
  110. ima_hash_algo_idx = i;
  111. }
  112. if (ima_sha1_idx < 0) {
  113. ima_sha1_idx = NR_BANKS(ima_tpm_chip) + ima_extra_slots++;
  114. if (ima_hash_algo == HASH_ALGO_SHA1)
  115. ima_hash_algo_idx = ima_sha1_idx;
  116. }
  117. if (ima_hash_algo_idx < 0)
  118. ima_hash_algo_idx = NR_BANKS(ima_tpm_chip) + ima_extra_slots++;
  119. ima_algo_array = kcalloc(NR_BANKS(ima_tpm_chip) + ima_extra_slots,
  120. sizeof(*ima_algo_array), GFP_KERNEL);
  121. if (!ima_algo_array) {
  122. rc = -ENOMEM;
  123. goto out;
  124. }
  125. for (i = 0; i < NR_BANKS(ima_tpm_chip); i++) {
  126. algo = ima_tpm_chip->allocated_banks[i].crypto_id;
  127. ima_algo_array[i].algo = algo;
  128. /* unknown TPM algorithm */
  129. if (algo == HASH_ALGO__LAST)
  130. continue;
  131. if (algo == ima_hash_algo) {
  132. ima_algo_array[i].tfm = ima_shash_tfm;
  133. continue;
  134. }
  135. ima_algo_array[i].tfm = ima_alloc_tfm(algo);
  136. if (IS_ERR(ima_algo_array[i].tfm)) {
  137. if (algo == HASH_ALGO_SHA1) {
  138. rc = PTR_ERR(ima_algo_array[i].tfm);
  139. ima_algo_array[i].tfm = NULL;
  140. goto out_array;
  141. }
  142. ima_algo_array[i].tfm = NULL;
  143. }
  144. }
  145. if (ima_sha1_idx >= NR_BANKS(ima_tpm_chip)) {
  146. if (ima_hash_algo == HASH_ALGO_SHA1) {
  147. ima_algo_array[ima_sha1_idx].tfm = ima_shash_tfm;
  148. } else {
  149. ima_algo_array[ima_sha1_idx].tfm =
  150. ima_alloc_tfm(HASH_ALGO_SHA1);
  151. if (IS_ERR(ima_algo_array[ima_sha1_idx].tfm)) {
  152. rc = PTR_ERR(ima_algo_array[ima_sha1_idx].tfm);
  153. goto out_array;
  154. }
  155. }
  156. ima_algo_array[ima_sha1_idx].algo = HASH_ALGO_SHA1;
  157. }
  158. if (ima_hash_algo_idx >= NR_BANKS(ima_tpm_chip) &&
  159. ima_hash_algo_idx != ima_sha1_idx) {
  160. ima_algo_array[ima_hash_algo_idx].tfm = ima_shash_tfm;
  161. ima_algo_array[ima_hash_algo_idx].algo = ima_hash_algo;
  162. }
  163. return 0;
  164. out_array:
  165. for (i = 0; i < NR_BANKS(ima_tpm_chip) + ima_extra_slots; i++) {
  166. if (!ima_algo_array[i].tfm ||
  167. ima_algo_array[i].tfm == ima_shash_tfm)
  168. continue;
  169. crypto_free_shash(ima_algo_array[i].tfm);
  170. }
  171. kfree(ima_algo_array);
  172. out:
  173. crypto_free_shash(ima_shash_tfm);
  174. return rc;
  175. }
  176. static void ima_free_tfm(struct crypto_shash *tfm)
  177. {
  178. int i;
  179. if (tfm == ima_shash_tfm)
  180. return;
  181. for (i = 0; i < NR_BANKS(ima_tpm_chip) + ima_extra_slots; i++)
  182. if (ima_algo_array[i].tfm == tfm)
  183. return;
  184. crypto_free_shash(tfm);
  185. }
  186. /**
  187. * ima_alloc_pages() - Allocate contiguous pages.
  188. * @max_size: Maximum amount of memory to allocate.
  189. * @allocated_size: Returned size of actual allocation.
  190. * @last_warn: Should the min_size allocation warn or not.
  191. *
  192. * Tries to do opportunistic allocation for memory first trying to allocate
  193. * max_size amount of memory and then splitting that until zero order is
  194. * reached. Allocation is tried without generating allocation warnings unless
  195. * last_warn is set. Last_warn set affects only last allocation of zero order.
  196. *
  197. * By default, ima_maxorder is 0 and it is equivalent to kmalloc(GFP_KERNEL)
  198. *
  199. * Return pointer to allocated memory, or NULL on failure.
  200. */
  201. static void *ima_alloc_pages(loff_t max_size, size_t *allocated_size,
  202. int last_warn)
  203. {
  204. void *ptr;
  205. int order = ima_maxorder;
  206. gfp_t gfp_mask = __GFP_RECLAIM | __GFP_NOWARN | __GFP_NORETRY;
  207. if (order)
  208. order = min(get_order(max_size), order);
  209. for (; order; order--) {
  210. ptr = (void *)__get_free_pages(gfp_mask, order);
  211. if (ptr) {
  212. *allocated_size = PAGE_SIZE << order;
  213. return ptr;
  214. }
  215. }
  216. /* order is zero - one page */
  217. gfp_mask = GFP_KERNEL;
  218. if (!last_warn)
  219. gfp_mask |= __GFP_NOWARN;
  220. ptr = (void *)__get_free_pages(gfp_mask, 0);
  221. if (ptr) {
  222. *allocated_size = PAGE_SIZE;
  223. return ptr;
  224. }
  225. *allocated_size = 0;
  226. return NULL;
  227. }
  228. /**
  229. * ima_free_pages() - Free pages allocated by ima_alloc_pages().
  230. * @ptr: Pointer to allocated pages.
  231. * @size: Size of allocated buffer.
  232. */
  233. static void ima_free_pages(void *ptr, size_t size)
  234. {
  235. if (!ptr)
  236. return;
  237. free_pages((unsigned long)ptr, get_order(size));
  238. }
  239. static struct crypto_ahash *ima_alloc_atfm(enum hash_algo algo)
  240. {
  241. struct crypto_ahash *tfm = ima_ahash_tfm;
  242. int rc;
  243. if (algo < 0 || algo >= HASH_ALGO__LAST)
  244. algo = ima_hash_algo;
  245. if (algo != ima_hash_algo || !tfm) {
  246. tfm = crypto_alloc_ahash(hash_algo_name[algo], 0, 0);
  247. if (!IS_ERR(tfm)) {
  248. if (algo == ima_hash_algo)
  249. ima_ahash_tfm = tfm;
  250. } else {
  251. rc = PTR_ERR(tfm);
  252. pr_err("Can not allocate %s (reason: %d)\n",
  253. hash_algo_name[algo], rc);
  254. }
  255. }
  256. return tfm;
  257. }
  258. static void ima_free_atfm(struct crypto_ahash *tfm)
  259. {
  260. if (tfm != ima_ahash_tfm)
  261. crypto_free_ahash(tfm);
  262. }
  263. static inline int ahash_wait(int err, struct crypto_wait *wait)
  264. {
  265. err = crypto_wait_req(err, wait);
  266. if (err)
  267. pr_crit_ratelimited("ahash calculation failed: err: %d\n", err);
  268. return err;
  269. }
  270. static int ima_calc_file_hash_atfm(struct file *file,
  271. struct ima_digest_data *hash,
  272. struct crypto_ahash *tfm)
  273. {
  274. loff_t i_size, offset;
  275. char *rbuf[2] = { NULL, };
  276. int rc, rbuf_len, active = 0, ahash_rc = 0;
  277. struct ahash_request *req;
  278. struct scatterlist sg[1];
  279. struct crypto_wait wait;
  280. size_t rbuf_size[2];
  281. hash->length = crypto_ahash_digestsize(tfm);
  282. req = ahash_request_alloc(tfm, GFP_KERNEL);
  283. if (!req)
  284. return -ENOMEM;
  285. crypto_init_wait(&wait);
  286. ahash_request_set_callback(req, CRYPTO_TFM_REQ_MAY_BACKLOG |
  287. CRYPTO_TFM_REQ_MAY_SLEEP,
  288. crypto_req_done, &wait);
  289. rc = ahash_wait(crypto_ahash_init(req), &wait);
  290. if (rc)
  291. goto out1;
  292. i_size = i_size_read(file_inode(file));
  293. if (i_size == 0)
  294. goto out2;
  295. /*
  296. * Try to allocate maximum size of memory.
  297. * Fail if even a single page cannot be allocated.
  298. */
  299. rbuf[0] = ima_alloc_pages(i_size, &rbuf_size[0], 1);
  300. if (!rbuf[0]) {
  301. rc = -ENOMEM;
  302. goto out1;
  303. }
  304. /* Only allocate one buffer if that is enough. */
  305. if (i_size > rbuf_size[0]) {
  306. /*
  307. * Try to allocate secondary buffer. If that fails fallback to
  308. * using single buffering. Use previous memory allocation size
  309. * as baseline for possible allocation size.
  310. */
  311. rbuf[1] = ima_alloc_pages(i_size - rbuf_size[0],
  312. &rbuf_size[1], 0);
  313. }
  314. for (offset = 0; offset < i_size; offset += rbuf_len) {
  315. if (!rbuf[1] && offset) {
  316. /* Not using two buffers, and it is not the first
  317. * read/request, wait for the completion of the
  318. * previous ahash_update() request.
  319. */
  320. rc = ahash_wait(ahash_rc, &wait);
  321. if (rc)
  322. goto out3;
  323. }
  324. /* read buffer */
  325. rbuf_len = min_t(loff_t, i_size - offset, rbuf_size[active]);
  326. rc = integrity_kernel_read(file, offset, rbuf[active],
  327. rbuf_len);
  328. if (rc != rbuf_len) {
  329. if (rc >= 0)
  330. rc = -EINVAL;
  331. /*
  332. * Forward current rc, do not overwrite with return value
  333. * from ahash_wait()
  334. */
  335. ahash_wait(ahash_rc, &wait);
  336. goto out3;
  337. }
  338. if (rbuf[1] && offset) {
  339. /* Using two buffers, and it is not the first
  340. * read/request, wait for the completion of the
  341. * previous ahash_update() request.
  342. */
  343. rc = ahash_wait(ahash_rc, &wait);
  344. if (rc)
  345. goto out3;
  346. }
  347. sg_init_one(&sg[0], rbuf[active], rbuf_len);
  348. ahash_request_set_crypt(req, sg, NULL, rbuf_len);
  349. ahash_rc = crypto_ahash_update(req);
  350. if (rbuf[1])
  351. active = !active; /* swap buffers, if we use two */
  352. }
  353. /* wait for the last update request to complete */
  354. rc = ahash_wait(ahash_rc, &wait);
  355. out3:
  356. ima_free_pages(rbuf[0], rbuf_size[0]);
  357. ima_free_pages(rbuf[1], rbuf_size[1]);
  358. out2:
  359. if (!rc) {
  360. ahash_request_set_crypt(req, NULL, hash->digest, 0);
  361. rc = ahash_wait(crypto_ahash_final(req), &wait);
  362. }
  363. out1:
  364. ahash_request_free(req);
  365. return rc;
  366. }
  367. static int ima_calc_file_ahash(struct file *file, struct ima_digest_data *hash)
  368. {
  369. struct crypto_ahash *tfm;
  370. int rc;
  371. tfm = ima_alloc_atfm(hash->algo);
  372. if (IS_ERR(tfm))
  373. return PTR_ERR(tfm);
  374. rc = ima_calc_file_hash_atfm(file, hash, tfm);
  375. ima_free_atfm(tfm);
  376. return rc;
  377. }
  378. static int ima_calc_file_hash_tfm(struct file *file,
  379. struct ima_digest_data *hash,
  380. struct crypto_shash *tfm)
  381. {
  382. loff_t i_size, offset = 0;
  383. char *rbuf;
  384. int rc;
  385. SHASH_DESC_ON_STACK(shash, tfm);
  386. shash->tfm = tfm;
  387. hash->length = crypto_shash_digestsize(tfm);
  388. rc = crypto_shash_init(shash);
  389. if (rc != 0)
  390. return rc;
  391. i_size = i_size_read(file_inode(file));
  392. if (i_size == 0)
  393. goto out;
  394. rbuf = kzalloc(PAGE_SIZE, GFP_KERNEL);
  395. if (!rbuf)
  396. return -ENOMEM;
  397. while (offset < i_size) {
  398. int rbuf_len;
  399. rbuf_len = integrity_kernel_read(file, offset, rbuf, PAGE_SIZE);
  400. if (rbuf_len < 0) {
  401. rc = rbuf_len;
  402. break;
  403. }
  404. if (rbuf_len == 0) { /* unexpected EOF */
  405. rc = -EINVAL;
  406. break;
  407. }
  408. offset += rbuf_len;
  409. rc = crypto_shash_update(shash, rbuf, rbuf_len);
  410. if (rc)
  411. break;
  412. }
  413. kfree(rbuf);
  414. out:
  415. if (!rc)
  416. rc = crypto_shash_final(shash, hash->digest);
  417. return rc;
  418. }
  419. static int ima_calc_file_shash(struct file *file, struct ima_digest_data *hash)
  420. {
  421. struct crypto_shash *tfm;
  422. int rc;
  423. tfm = ima_alloc_tfm(hash->algo);
  424. if (IS_ERR(tfm))
  425. return PTR_ERR(tfm);
  426. rc = ima_calc_file_hash_tfm(file, hash, tfm);
  427. ima_free_tfm(tfm);
  428. return rc;
  429. }
  430. /*
  431. * ima_calc_file_hash - calculate file hash
  432. *
  433. * Asynchronous hash (ahash) allows using HW acceleration for calculating
  434. * a hash. ahash performance varies for different data sizes on different
  435. * crypto accelerators. shash performance might be better for smaller files.
  436. * The 'ima.ahash_minsize' module parameter allows specifying the best
  437. * minimum file size for using ahash on the system.
  438. *
  439. * If the ima.ahash_minsize parameter is not specified, this function uses
  440. * shash for the hash calculation. If ahash fails, it falls back to using
  441. * shash.
  442. */
  443. int ima_calc_file_hash(struct file *file, struct ima_digest_data *hash)
  444. {
  445. loff_t i_size;
  446. int rc;
  447. struct file *f = file;
  448. bool new_file_instance = false;
  449. /*
  450. * For consistency, fail file's opened with the O_DIRECT flag on
  451. * filesystems mounted with/without DAX option.
  452. */
  453. if (file->f_flags & O_DIRECT) {
  454. hash->length = hash_digest_size[ima_hash_algo];
  455. hash->algo = ima_hash_algo;
  456. return -EINVAL;
  457. }
  458. /* Open a new file instance in O_RDONLY if we cannot read */
  459. if (!(file->f_mode & FMODE_READ)) {
  460. int flags = file->f_flags & ~(O_WRONLY | O_APPEND |
  461. O_TRUNC | O_CREAT | O_NOCTTY | O_EXCL);
  462. flags |= O_RDONLY;
  463. f = dentry_open(&file->f_path, flags, file->f_cred);
  464. if (IS_ERR(f))
  465. return PTR_ERR(f);
  466. new_file_instance = true;
  467. }
  468. i_size = i_size_read(file_inode(f));
  469. if (ima_ahash_minsize && i_size >= ima_ahash_minsize) {
  470. rc = ima_calc_file_ahash(f, hash);
  471. if (!rc)
  472. goto out;
  473. }
  474. rc = ima_calc_file_shash(f, hash);
  475. out:
  476. if (new_file_instance)
  477. fput(f);
  478. return rc;
  479. }
  480. /*
  481. * Calculate the hash of template data
  482. */
  483. static int ima_calc_field_array_hash_tfm(struct ima_field_data *field_data,
  484. struct ima_template_entry *entry,
  485. int tfm_idx)
  486. {
  487. SHASH_DESC_ON_STACK(shash, ima_algo_array[tfm_idx].tfm);
  488. struct ima_template_desc *td = entry->template_desc;
  489. int num_fields = entry->template_desc->num_fields;
  490. int rc, i;
  491. shash->tfm = ima_algo_array[tfm_idx].tfm;
  492. rc = crypto_shash_init(shash);
  493. if (rc != 0)
  494. return rc;
  495. for (i = 0; i < num_fields; i++) {
  496. u8 buffer[IMA_EVENT_NAME_LEN_MAX + 1] = { 0 };
  497. u8 *data_to_hash = field_data[i].data;
  498. u32 datalen = field_data[i].len;
  499. u32 datalen_to_hash = !ima_canonical_fmt ?
  500. datalen : (__force u32)cpu_to_le32(datalen);
  501. if (strcmp(td->name, IMA_TEMPLATE_IMA_NAME) != 0) {
  502. rc = crypto_shash_update(shash,
  503. (const u8 *) &datalen_to_hash,
  504. sizeof(datalen_to_hash));
  505. if (rc)
  506. break;
  507. } else if (strcmp(td->fields[i]->field_id, "n") == 0) {
  508. memcpy(buffer, data_to_hash, datalen);
  509. data_to_hash = buffer;
  510. datalen = IMA_EVENT_NAME_LEN_MAX + 1;
  511. }
  512. rc = crypto_shash_update(shash, data_to_hash, datalen);
  513. if (rc)
  514. break;
  515. }
  516. if (!rc)
  517. rc = crypto_shash_final(shash, entry->digests[tfm_idx].digest);
  518. return rc;
  519. }
  520. int ima_calc_field_array_hash(struct ima_field_data *field_data,
  521. struct ima_template_entry *entry)
  522. {
  523. u16 alg_id;
  524. int rc, i;
  525. rc = ima_calc_field_array_hash_tfm(field_data, entry, ima_sha1_idx);
  526. if (rc)
  527. return rc;
  528. entry->digests[ima_sha1_idx].alg_id = TPM_ALG_SHA1;
  529. for (i = 0; i < NR_BANKS(ima_tpm_chip) + ima_extra_slots; i++) {
  530. if (i == ima_sha1_idx)
  531. continue;
  532. if (i < NR_BANKS(ima_tpm_chip)) {
  533. alg_id = ima_tpm_chip->allocated_banks[i].alg_id;
  534. entry->digests[i].alg_id = alg_id;
  535. }
  536. /* for unmapped TPM algorithms digest is still a padded SHA1 */
  537. if (!ima_algo_array[i].tfm) {
  538. memcpy(entry->digests[i].digest,
  539. entry->digests[ima_sha1_idx].digest,
  540. TPM_DIGEST_SIZE);
  541. continue;
  542. }
  543. rc = ima_calc_field_array_hash_tfm(field_data, entry, i);
  544. if (rc)
  545. return rc;
  546. }
  547. return rc;
  548. }
  549. static int calc_buffer_ahash_atfm(const void *buf, loff_t len,
  550. struct ima_digest_data *hash,
  551. struct crypto_ahash *tfm)
  552. {
  553. struct ahash_request *req;
  554. struct scatterlist sg;
  555. struct crypto_wait wait;
  556. int rc, ahash_rc = 0;
  557. hash->length = crypto_ahash_digestsize(tfm);
  558. req = ahash_request_alloc(tfm, GFP_KERNEL);
  559. if (!req)
  560. return -ENOMEM;
  561. crypto_init_wait(&wait);
  562. ahash_request_set_callback(req, CRYPTO_TFM_REQ_MAY_BACKLOG |
  563. CRYPTO_TFM_REQ_MAY_SLEEP,
  564. crypto_req_done, &wait);
  565. rc = ahash_wait(crypto_ahash_init(req), &wait);
  566. if (rc)
  567. goto out;
  568. sg_init_one(&sg, buf, len);
  569. ahash_request_set_crypt(req, &sg, NULL, len);
  570. ahash_rc = crypto_ahash_update(req);
  571. /* wait for the update request to complete */
  572. rc = ahash_wait(ahash_rc, &wait);
  573. if (!rc) {
  574. ahash_request_set_crypt(req, NULL, hash->digest, 0);
  575. rc = ahash_wait(crypto_ahash_final(req), &wait);
  576. }
  577. out:
  578. ahash_request_free(req);
  579. return rc;
  580. }
  581. static int calc_buffer_ahash(const void *buf, loff_t len,
  582. struct ima_digest_data *hash)
  583. {
  584. struct crypto_ahash *tfm;
  585. int rc;
  586. tfm = ima_alloc_atfm(hash->algo);
  587. if (IS_ERR(tfm))
  588. return PTR_ERR(tfm);
  589. rc = calc_buffer_ahash_atfm(buf, len, hash, tfm);
  590. ima_free_atfm(tfm);
  591. return rc;
  592. }
  593. static int calc_buffer_shash_tfm(const void *buf, loff_t size,
  594. struct ima_digest_data *hash,
  595. struct crypto_shash *tfm)
  596. {
  597. SHASH_DESC_ON_STACK(shash, tfm);
  598. unsigned int len;
  599. int rc;
  600. shash->tfm = tfm;
  601. hash->length = crypto_shash_digestsize(tfm);
  602. rc = crypto_shash_init(shash);
  603. if (rc != 0)
  604. return rc;
  605. while (size) {
  606. len = size < PAGE_SIZE ? size : PAGE_SIZE;
  607. rc = crypto_shash_update(shash, buf, len);
  608. if (rc)
  609. break;
  610. buf += len;
  611. size -= len;
  612. }
  613. if (!rc)
  614. rc = crypto_shash_final(shash, hash->digest);
  615. return rc;
  616. }
  617. static int calc_buffer_shash(const void *buf, loff_t len,
  618. struct ima_digest_data *hash)
  619. {
  620. struct crypto_shash *tfm;
  621. int rc;
  622. tfm = ima_alloc_tfm(hash->algo);
  623. if (IS_ERR(tfm))
  624. return PTR_ERR(tfm);
  625. rc = calc_buffer_shash_tfm(buf, len, hash, tfm);
  626. ima_free_tfm(tfm);
  627. return rc;
  628. }
  629. int ima_calc_buffer_hash(const void *buf, loff_t len,
  630. struct ima_digest_data *hash)
  631. {
  632. int rc;
  633. if (ima_ahash_minsize && len >= ima_ahash_minsize) {
  634. rc = calc_buffer_ahash(buf, len, hash);
  635. if (!rc)
  636. return 0;
  637. }
  638. return calc_buffer_shash(buf, len, hash);
  639. }
  640. static void ima_pcrread(u32 idx, struct tpm_digest *d)
  641. {
  642. if (!ima_tpm_chip)
  643. return;
  644. if (tpm_pcr_read(ima_tpm_chip, idx, d) != 0)
  645. pr_err("Error Communicating to TPM chip\n");
  646. }
  647. /*
  648. * The boot_aggregate is a cumulative hash over TPM registers 0 - 7. With
  649. * TPM 1.2 the boot_aggregate was based on reading the SHA1 PCRs, but with
  650. * TPM 2.0 hash agility, TPM chips could support multiple TPM PCR banks,
  651. * allowing firmware to configure and enable different banks.
  652. *
  653. * Knowing which TPM bank is read to calculate the boot_aggregate digest
  654. * needs to be conveyed to a verifier. For this reason, use the same
  655. * hash algorithm for reading the TPM PCRs as for calculating the boot
  656. * aggregate digest as stored in the measurement list.
  657. */
  658. static int ima_calc_boot_aggregate_tfm(char *digest, u16 alg_id,
  659. struct crypto_shash *tfm)
  660. {
  661. struct tpm_digest d = { .alg_id = alg_id, .digest = {0} };
  662. int rc;
  663. u32 i;
  664. SHASH_DESC_ON_STACK(shash, tfm);
  665. shash->tfm = tfm;
  666. pr_devel("calculating the boot-aggregate based on TPM bank: %04x\n",
  667. d.alg_id);
  668. rc = crypto_shash_init(shash);
  669. if (rc != 0)
  670. return rc;
  671. /* cumulative digest over TPM registers 0-7 */
  672. for (i = TPM_PCR0; i < TPM_PCR8; i++) {
  673. ima_pcrread(i, &d);
  674. /* now accumulate with current aggregate */
  675. rc = crypto_shash_update(shash, d.digest,
  676. crypto_shash_digestsize(tfm));
  677. if (rc != 0)
  678. return rc;
  679. }
  680. /*
  681. * Extend cumulative digest over TPM registers 8-9, which contain
  682. * measurement for the kernel command line (reg. 8) and image (reg. 9)
  683. * in a typical PCR allocation. Registers 8-9 are only included in
  684. * non-SHA1 boot_aggregate digests to avoid ambiguity.
  685. */
  686. if (alg_id != TPM_ALG_SHA1) {
  687. for (i = TPM_PCR8; i < TPM_PCR10; i++) {
  688. ima_pcrread(i, &d);
  689. rc = crypto_shash_update(shash, d.digest,
  690. crypto_shash_digestsize(tfm));
  691. }
  692. }
  693. if (!rc)
  694. crypto_shash_final(shash, digest);
  695. return rc;
  696. }
  697. int ima_calc_boot_aggregate(struct ima_digest_data *hash)
  698. {
  699. struct crypto_shash *tfm;
  700. u16 crypto_id, alg_id;
  701. int rc, i, bank_idx = -1;
  702. for (i = 0; i < ima_tpm_chip->nr_allocated_banks; i++) {
  703. crypto_id = ima_tpm_chip->allocated_banks[i].crypto_id;
  704. if (crypto_id == hash->algo) {
  705. bank_idx = i;
  706. break;
  707. }
  708. if (crypto_id == HASH_ALGO_SHA256)
  709. bank_idx = i;
  710. if (bank_idx == -1 && crypto_id == HASH_ALGO_SHA1)
  711. bank_idx = i;
  712. }
  713. if (bank_idx == -1) {
  714. pr_err("No suitable TPM algorithm for boot aggregate\n");
  715. return 0;
  716. }
  717. hash->algo = ima_tpm_chip->allocated_banks[bank_idx].crypto_id;
  718. tfm = ima_alloc_tfm(hash->algo);
  719. if (IS_ERR(tfm))
  720. return PTR_ERR(tfm);
  721. hash->length = crypto_shash_digestsize(tfm);
  722. alg_id = ima_tpm_chip->allocated_banks[bank_idx].alg_id;
  723. rc = ima_calc_boot_aggregate_tfm(hash->digest, alg_id, tfm);
  724. ima_free_tfm(tfm);
  725. return rc;
  726. }