ima_crypto.c 13 KB

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