ima_crypto.c 14 KB

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