ima_crypto.c 20 KB

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