ima_crypto.c 13 KB

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