ima_crypto.c 13 KB

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