162306a36Sopenharmony_ci// SPDX-License-Identifier: GPL-2.0-only 262306a36Sopenharmony_ci/* 362306a36Sopenharmony_ci * Copyright (C) 2005,2006,2007,2008 IBM Corporation 462306a36Sopenharmony_ci * 562306a36Sopenharmony_ci * Authors: 662306a36Sopenharmony_ci * Mimi Zohar <zohar@us.ibm.com> 762306a36Sopenharmony_ci * Kylene Hall <kjhall@us.ibm.com> 862306a36Sopenharmony_ci * 962306a36Sopenharmony_ci * File: ima_crypto.c 1062306a36Sopenharmony_ci * Calculates md5/sha1 file hash, template hash, boot-aggreate hash 1162306a36Sopenharmony_ci */ 1262306a36Sopenharmony_ci 1362306a36Sopenharmony_ci#include <linux/kernel.h> 1462306a36Sopenharmony_ci#include <linux/moduleparam.h> 1562306a36Sopenharmony_ci#include <linux/ratelimit.h> 1662306a36Sopenharmony_ci#include <linux/file.h> 1762306a36Sopenharmony_ci#include <linux/crypto.h> 1862306a36Sopenharmony_ci#include <linux/scatterlist.h> 1962306a36Sopenharmony_ci#include <linux/err.h> 2062306a36Sopenharmony_ci#include <linux/slab.h> 2162306a36Sopenharmony_ci#include <crypto/hash.h> 2262306a36Sopenharmony_ci 2362306a36Sopenharmony_ci#include "ima.h" 2462306a36Sopenharmony_ci 2562306a36Sopenharmony_ci/* minimum file size for ahash use */ 2662306a36Sopenharmony_cistatic unsigned long ima_ahash_minsize; 2762306a36Sopenharmony_cimodule_param_named(ahash_minsize, ima_ahash_minsize, ulong, 0644); 2862306a36Sopenharmony_ciMODULE_PARM_DESC(ahash_minsize, "Minimum file size for ahash use"); 2962306a36Sopenharmony_ci 3062306a36Sopenharmony_ci/* default is 0 - 1 page. */ 3162306a36Sopenharmony_cistatic int ima_maxorder; 3262306a36Sopenharmony_cistatic unsigned int ima_bufsize = PAGE_SIZE; 3362306a36Sopenharmony_ci 3462306a36Sopenharmony_cistatic int param_set_bufsize(const char *val, const struct kernel_param *kp) 3562306a36Sopenharmony_ci{ 3662306a36Sopenharmony_ci unsigned long long size; 3762306a36Sopenharmony_ci int order; 3862306a36Sopenharmony_ci 3962306a36Sopenharmony_ci size = memparse(val, NULL); 4062306a36Sopenharmony_ci order = get_order(size); 4162306a36Sopenharmony_ci if (order > MAX_ORDER) 4262306a36Sopenharmony_ci return -EINVAL; 4362306a36Sopenharmony_ci ima_maxorder = order; 4462306a36Sopenharmony_ci ima_bufsize = PAGE_SIZE << order; 4562306a36Sopenharmony_ci return 0; 4662306a36Sopenharmony_ci} 4762306a36Sopenharmony_ci 4862306a36Sopenharmony_cistatic const struct kernel_param_ops param_ops_bufsize = { 4962306a36Sopenharmony_ci .set = param_set_bufsize, 5062306a36Sopenharmony_ci .get = param_get_uint, 5162306a36Sopenharmony_ci}; 5262306a36Sopenharmony_ci#define param_check_bufsize(name, p) __param_check(name, p, unsigned int) 5362306a36Sopenharmony_ci 5462306a36Sopenharmony_cimodule_param_named(ahash_bufsize, ima_bufsize, bufsize, 0644); 5562306a36Sopenharmony_ciMODULE_PARM_DESC(ahash_bufsize, "Maximum ahash buffer size"); 5662306a36Sopenharmony_ci 5762306a36Sopenharmony_cistatic struct crypto_shash *ima_shash_tfm; 5862306a36Sopenharmony_cistatic struct crypto_ahash *ima_ahash_tfm; 5962306a36Sopenharmony_ci 6062306a36Sopenharmony_cistruct ima_algo_desc { 6162306a36Sopenharmony_ci struct crypto_shash *tfm; 6262306a36Sopenharmony_ci enum hash_algo algo; 6362306a36Sopenharmony_ci}; 6462306a36Sopenharmony_ci 6562306a36Sopenharmony_ciint ima_sha1_idx __ro_after_init; 6662306a36Sopenharmony_ciint ima_hash_algo_idx __ro_after_init; 6762306a36Sopenharmony_ci/* 6862306a36Sopenharmony_ci * Additional number of slots reserved, as needed, for SHA1 6962306a36Sopenharmony_ci * and IMA default algo. 7062306a36Sopenharmony_ci */ 7162306a36Sopenharmony_ciint ima_extra_slots __ro_after_init; 7262306a36Sopenharmony_ci 7362306a36Sopenharmony_cistatic struct ima_algo_desc *ima_algo_array; 7462306a36Sopenharmony_ci 7562306a36Sopenharmony_cistatic int __init ima_init_ima_crypto(void) 7662306a36Sopenharmony_ci{ 7762306a36Sopenharmony_ci long rc; 7862306a36Sopenharmony_ci 7962306a36Sopenharmony_ci ima_shash_tfm = crypto_alloc_shash(hash_algo_name[ima_hash_algo], 0, 0); 8062306a36Sopenharmony_ci if (IS_ERR(ima_shash_tfm)) { 8162306a36Sopenharmony_ci rc = PTR_ERR(ima_shash_tfm); 8262306a36Sopenharmony_ci pr_err("Can not allocate %s (reason: %ld)\n", 8362306a36Sopenharmony_ci hash_algo_name[ima_hash_algo], rc); 8462306a36Sopenharmony_ci return rc; 8562306a36Sopenharmony_ci } 8662306a36Sopenharmony_ci pr_info("Allocated hash algorithm: %s\n", 8762306a36Sopenharmony_ci hash_algo_name[ima_hash_algo]); 8862306a36Sopenharmony_ci return 0; 8962306a36Sopenharmony_ci} 9062306a36Sopenharmony_ci 9162306a36Sopenharmony_cistatic struct crypto_shash *ima_alloc_tfm(enum hash_algo algo) 9262306a36Sopenharmony_ci{ 9362306a36Sopenharmony_ci struct crypto_shash *tfm = ima_shash_tfm; 9462306a36Sopenharmony_ci int rc, i; 9562306a36Sopenharmony_ci 9662306a36Sopenharmony_ci if (algo < 0 || algo >= HASH_ALGO__LAST) 9762306a36Sopenharmony_ci algo = ima_hash_algo; 9862306a36Sopenharmony_ci 9962306a36Sopenharmony_ci if (algo == ima_hash_algo) 10062306a36Sopenharmony_ci return tfm; 10162306a36Sopenharmony_ci 10262306a36Sopenharmony_ci for (i = 0; i < NR_BANKS(ima_tpm_chip) + ima_extra_slots; i++) 10362306a36Sopenharmony_ci if (ima_algo_array[i].tfm && ima_algo_array[i].algo == algo) 10462306a36Sopenharmony_ci return ima_algo_array[i].tfm; 10562306a36Sopenharmony_ci 10662306a36Sopenharmony_ci tfm = crypto_alloc_shash(hash_algo_name[algo], 0, 0); 10762306a36Sopenharmony_ci if (IS_ERR(tfm)) { 10862306a36Sopenharmony_ci rc = PTR_ERR(tfm); 10962306a36Sopenharmony_ci pr_err("Can not allocate %s (reason: %d)\n", 11062306a36Sopenharmony_ci hash_algo_name[algo], rc); 11162306a36Sopenharmony_ci } 11262306a36Sopenharmony_ci return tfm; 11362306a36Sopenharmony_ci} 11462306a36Sopenharmony_ci 11562306a36Sopenharmony_ciint __init ima_init_crypto(void) 11662306a36Sopenharmony_ci{ 11762306a36Sopenharmony_ci enum hash_algo algo; 11862306a36Sopenharmony_ci long rc; 11962306a36Sopenharmony_ci int i; 12062306a36Sopenharmony_ci 12162306a36Sopenharmony_ci rc = ima_init_ima_crypto(); 12262306a36Sopenharmony_ci if (rc) 12362306a36Sopenharmony_ci return rc; 12462306a36Sopenharmony_ci 12562306a36Sopenharmony_ci ima_sha1_idx = -1; 12662306a36Sopenharmony_ci ima_hash_algo_idx = -1; 12762306a36Sopenharmony_ci 12862306a36Sopenharmony_ci for (i = 0; i < NR_BANKS(ima_tpm_chip); i++) { 12962306a36Sopenharmony_ci algo = ima_tpm_chip->allocated_banks[i].crypto_id; 13062306a36Sopenharmony_ci if (algo == HASH_ALGO_SHA1) 13162306a36Sopenharmony_ci ima_sha1_idx = i; 13262306a36Sopenharmony_ci 13362306a36Sopenharmony_ci if (algo == ima_hash_algo) 13462306a36Sopenharmony_ci ima_hash_algo_idx = i; 13562306a36Sopenharmony_ci } 13662306a36Sopenharmony_ci 13762306a36Sopenharmony_ci if (ima_sha1_idx < 0) { 13862306a36Sopenharmony_ci ima_sha1_idx = NR_BANKS(ima_tpm_chip) + ima_extra_slots++; 13962306a36Sopenharmony_ci if (ima_hash_algo == HASH_ALGO_SHA1) 14062306a36Sopenharmony_ci ima_hash_algo_idx = ima_sha1_idx; 14162306a36Sopenharmony_ci } 14262306a36Sopenharmony_ci 14362306a36Sopenharmony_ci if (ima_hash_algo_idx < 0) 14462306a36Sopenharmony_ci ima_hash_algo_idx = NR_BANKS(ima_tpm_chip) + ima_extra_slots++; 14562306a36Sopenharmony_ci 14662306a36Sopenharmony_ci ima_algo_array = kcalloc(NR_BANKS(ima_tpm_chip) + ima_extra_slots, 14762306a36Sopenharmony_ci sizeof(*ima_algo_array), GFP_KERNEL); 14862306a36Sopenharmony_ci if (!ima_algo_array) { 14962306a36Sopenharmony_ci rc = -ENOMEM; 15062306a36Sopenharmony_ci goto out; 15162306a36Sopenharmony_ci } 15262306a36Sopenharmony_ci 15362306a36Sopenharmony_ci for (i = 0; i < NR_BANKS(ima_tpm_chip); i++) { 15462306a36Sopenharmony_ci algo = ima_tpm_chip->allocated_banks[i].crypto_id; 15562306a36Sopenharmony_ci ima_algo_array[i].algo = algo; 15662306a36Sopenharmony_ci 15762306a36Sopenharmony_ci /* unknown TPM algorithm */ 15862306a36Sopenharmony_ci if (algo == HASH_ALGO__LAST) 15962306a36Sopenharmony_ci continue; 16062306a36Sopenharmony_ci 16162306a36Sopenharmony_ci if (algo == ima_hash_algo) { 16262306a36Sopenharmony_ci ima_algo_array[i].tfm = ima_shash_tfm; 16362306a36Sopenharmony_ci continue; 16462306a36Sopenharmony_ci } 16562306a36Sopenharmony_ci 16662306a36Sopenharmony_ci ima_algo_array[i].tfm = ima_alloc_tfm(algo); 16762306a36Sopenharmony_ci if (IS_ERR(ima_algo_array[i].tfm)) { 16862306a36Sopenharmony_ci if (algo == HASH_ALGO_SHA1) { 16962306a36Sopenharmony_ci rc = PTR_ERR(ima_algo_array[i].tfm); 17062306a36Sopenharmony_ci ima_algo_array[i].tfm = NULL; 17162306a36Sopenharmony_ci goto out_array; 17262306a36Sopenharmony_ci } 17362306a36Sopenharmony_ci 17462306a36Sopenharmony_ci ima_algo_array[i].tfm = NULL; 17562306a36Sopenharmony_ci } 17662306a36Sopenharmony_ci } 17762306a36Sopenharmony_ci 17862306a36Sopenharmony_ci if (ima_sha1_idx >= NR_BANKS(ima_tpm_chip)) { 17962306a36Sopenharmony_ci if (ima_hash_algo == HASH_ALGO_SHA1) { 18062306a36Sopenharmony_ci ima_algo_array[ima_sha1_idx].tfm = ima_shash_tfm; 18162306a36Sopenharmony_ci } else { 18262306a36Sopenharmony_ci ima_algo_array[ima_sha1_idx].tfm = 18362306a36Sopenharmony_ci ima_alloc_tfm(HASH_ALGO_SHA1); 18462306a36Sopenharmony_ci if (IS_ERR(ima_algo_array[ima_sha1_idx].tfm)) { 18562306a36Sopenharmony_ci rc = PTR_ERR(ima_algo_array[ima_sha1_idx].tfm); 18662306a36Sopenharmony_ci goto out_array; 18762306a36Sopenharmony_ci } 18862306a36Sopenharmony_ci } 18962306a36Sopenharmony_ci 19062306a36Sopenharmony_ci ima_algo_array[ima_sha1_idx].algo = HASH_ALGO_SHA1; 19162306a36Sopenharmony_ci } 19262306a36Sopenharmony_ci 19362306a36Sopenharmony_ci if (ima_hash_algo_idx >= NR_BANKS(ima_tpm_chip) && 19462306a36Sopenharmony_ci ima_hash_algo_idx != ima_sha1_idx) { 19562306a36Sopenharmony_ci ima_algo_array[ima_hash_algo_idx].tfm = ima_shash_tfm; 19662306a36Sopenharmony_ci ima_algo_array[ima_hash_algo_idx].algo = ima_hash_algo; 19762306a36Sopenharmony_ci } 19862306a36Sopenharmony_ci 19962306a36Sopenharmony_ci return 0; 20062306a36Sopenharmony_ciout_array: 20162306a36Sopenharmony_ci for (i = 0; i < NR_BANKS(ima_tpm_chip) + ima_extra_slots; i++) { 20262306a36Sopenharmony_ci if (!ima_algo_array[i].tfm || 20362306a36Sopenharmony_ci ima_algo_array[i].tfm == ima_shash_tfm) 20462306a36Sopenharmony_ci continue; 20562306a36Sopenharmony_ci 20662306a36Sopenharmony_ci crypto_free_shash(ima_algo_array[i].tfm); 20762306a36Sopenharmony_ci } 20862306a36Sopenharmony_ci kfree(ima_algo_array); 20962306a36Sopenharmony_ciout: 21062306a36Sopenharmony_ci crypto_free_shash(ima_shash_tfm); 21162306a36Sopenharmony_ci return rc; 21262306a36Sopenharmony_ci} 21362306a36Sopenharmony_ci 21462306a36Sopenharmony_cistatic void ima_free_tfm(struct crypto_shash *tfm) 21562306a36Sopenharmony_ci{ 21662306a36Sopenharmony_ci int i; 21762306a36Sopenharmony_ci 21862306a36Sopenharmony_ci if (tfm == ima_shash_tfm) 21962306a36Sopenharmony_ci return; 22062306a36Sopenharmony_ci 22162306a36Sopenharmony_ci for (i = 0; i < NR_BANKS(ima_tpm_chip) + ima_extra_slots; i++) 22262306a36Sopenharmony_ci if (ima_algo_array[i].tfm == tfm) 22362306a36Sopenharmony_ci return; 22462306a36Sopenharmony_ci 22562306a36Sopenharmony_ci crypto_free_shash(tfm); 22662306a36Sopenharmony_ci} 22762306a36Sopenharmony_ci 22862306a36Sopenharmony_ci/** 22962306a36Sopenharmony_ci * ima_alloc_pages() - Allocate contiguous pages. 23062306a36Sopenharmony_ci * @max_size: Maximum amount of memory to allocate. 23162306a36Sopenharmony_ci * @allocated_size: Returned size of actual allocation. 23262306a36Sopenharmony_ci * @last_warn: Should the min_size allocation warn or not. 23362306a36Sopenharmony_ci * 23462306a36Sopenharmony_ci * Tries to do opportunistic allocation for memory first trying to allocate 23562306a36Sopenharmony_ci * max_size amount of memory and then splitting that until zero order is 23662306a36Sopenharmony_ci * reached. Allocation is tried without generating allocation warnings unless 23762306a36Sopenharmony_ci * last_warn is set. Last_warn set affects only last allocation of zero order. 23862306a36Sopenharmony_ci * 23962306a36Sopenharmony_ci * By default, ima_maxorder is 0 and it is equivalent to kmalloc(GFP_KERNEL) 24062306a36Sopenharmony_ci * 24162306a36Sopenharmony_ci * Return pointer to allocated memory, or NULL on failure. 24262306a36Sopenharmony_ci */ 24362306a36Sopenharmony_cistatic void *ima_alloc_pages(loff_t max_size, size_t *allocated_size, 24462306a36Sopenharmony_ci int last_warn) 24562306a36Sopenharmony_ci{ 24662306a36Sopenharmony_ci void *ptr; 24762306a36Sopenharmony_ci int order = ima_maxorder; 24862306a36Sopenharmony_ci gfp_t gfp_mask = __GFP_RECLAIM | __GFP_NOWARN | __GFP_NORETRY; 24962306a36Sopenharmony_ci 25062306a36Sopenharmony_ci if (order) 25162306a36Sopenharmony_ci order = min(get_order(max_size), order); 25262306a36Sopenharmony_ci 25362306a36Sopenharmony_ci for (; order; order--) { 25462306a36Sopenharmony_ci ptr = (void *)__get_free_pages(gfp_mask, order); 25562306a36Sopenharmony_ci if (ptr) { 25662306a36Sopenharmony_ci *allocated_size = PAGE_SIZE << order; 25762306a36Sopenharmony_ci return ptr; 25862306a36Sopenharmony_ci } 25962306a36Sopenharmony_ci } 26062306a36Sopenharmony_ci 26162306a36Sopenharmony_ci /* order is zero - one page */ 26262306a36Sopenharmony_ci 26362306a36Sopenharmony_ci gfp_mask = GFP_KERNEL; 26462306a36Sopenharmony_ci 26562306a36Sopenharmony_ci if (!last_warn) 26662306a36Sopenharmony_ci gfp_mask |= __GFP_NOWARN; 26762306a36Sopenharmony_ci 26862306a36Sopenharmony_ci ptr = (void *)__get_free_pages(gfp_mask, 0); 26962306a36Sopenharmony_ci if (ptr) { 27062306a36Sopenharmony_ci *allocated_size = PAGE_SIZE; 27162306a36Sopenharmony_ci return ptr; 27262306a36Sopenharmony_ci } 27362306a36Sopenharmony_ci 27462306a36Sopenharmony_ci *allocated_size = 0; 27562306a36Sopenharmony_ci return NULL; 27662306a36Sopenharmony_ci} 27762306a36Sopenharmony_ci 27862306a36Sopenharmony_ci/** 27962306a36Sopenharmony_ci * ima_free_pages() - Free pages allocated by ima_alloc_pages(). 28062306a36Sopenharmony_ci * @ptr: Pointer to allocated pages. 28162306a36Sopenharmony_ci * @size: Size of allocated buffer. 28262306a36Sopenharmony_ci */ 28362306a36Sopenharmony_cistatic void ima_free_pages(void *ptr, size_t size) 28462306a36Sopenharmony_ci{ 28562306a36Sopenharmony_ci if (!ptr) 28662306a36Sopenharmony_ci return; 28762306a36Sopenharmony_ci free_pages((unsigned long)ptr, get_order(size)); 28862306a36Sopenharmony_ci} 28962306a36Sopenharmony_ci 29062306a36Sopenharmony_cistatic struct crypto_ahash *ima_alloc_atfm(enum hash_algo algo) 29162306a36Sopenharmony_ci{ 29262306a36Sopenharmony_ci struct crypto_ahash *tfm = ima_ahash_tfm; 29362306a36Sopenharmony_ci int rc; 29462306a36Sopenharmony_ci 29562306a36Sopenharmony_ci if (algo < 0 || algo >= HASH_ALGO__LAST) 29662306a36Sopenharmony_ci algo = ima_hash_algo; 29762306a36Sopenharmony_ci 29862306a36Sopenharmony_ci if (algo != ima_hash_algo || !tfm) { 29962306a36Sopenharmony_ci tfm = crypto_alloc_ahash(hash_algo_name[algo], 0, 0); 30062306a36Sopenharmony_ci if (!IS_ERR(tfm)) { 30162306a36Sopenharmony_ci if (algo == ima_hash_algo) 30262306a36Sopenharmony_ci ima_ahash_tfm = tfm; 30362306a36Sopenharmony_ci } else { 30462306a36Sopenharmony_ci rc = PTR_ERR(tfm); 30562306a36Sopenharmony_ci pr_err("Can not allocate %s (reason: %d)\n", 30662306a36Sopenharmony_ci hash_algo_name[algo], rc); 30762306a36Sopenharmony_ci } 30862306a36Sopenharmony_ci } 30962306a36Sopenharmony_ci return tfm; 31062306a36Sopenharmony_ci} 31162306a36Sopenharmony_ci 31262306a36Sopenharmony_cistatic void ima_free_atfm(struct crypto_ahash *tfm) 31362306a36Sopenharmony_ci{ 31462306a36Sopenharmony_ci if (tfm != ima_ahash_tfm) 31562306a36Sopenharmony_ci crypto_free_ahash(tfm); 31662306a36Sopenharmony_ci} 31762306a36Sopenharmony_ci 31862306a36Sopenharmony_cistatic inline int ahash_wait(int err, struct crypto_wait *wait) 31962306a36Sopenharmony_ci{ 32062306a36Sopenharmony_ci 32162306a36Sopenharmony_ci err = crypto_wait_req(err, wait); 32262306a36Sopenharmony_ci 32362306a36Sopenharmony_ci if (err) 32462306a36Sopenharmony_ci pr_crit_ratelimited("ahash calculation failed: err: %d\n", err); 32562306a36Sopenharmony_ci 32662306a36Sopenharmony_ci return err; 32762306a36Sopenharmony_ci} 32862306a36Sopenharmony_ci 32962306a36Sopenharmony_cistatic int ima_calc_file_hash_atfm(struct file *file, 33062306a36Sopenharmony_ci struct ima_digest_data *hash, 33162306a36Sopenharmony_ci struct crypto_ahash *tfm) 33262306a36Sopenharmony_ci{ 33362306a36Sopenharmony_ci loff_t i_size, offset; 33462306a36Sopenharmony_ci char *rbuf[2] = { NULL, }; 33562306a36Sopenharmony_ci int rc, rbuf_len, active = 0, ahash_rc = 0; 33662306a36Sopenharmony_ci struct ahash_request *req; 33762306a36Sopenharmony_ci struct scatterlist sg[1]; 33862306a36Sopenharmony_ci struct crypto_wait wait; 33962306a36Sopenharmony_ci size_t rbuf_size[2]; 34062306a36Sopenharmony_ci 34162306a36Sopenharmony_ci hash->length = crypto_ahash_digestsize(tfm); 34262306a36Sopenharmony_ci 34362306a36Sopenharmony_ci req = ahash_request_alloc(tfm, GFP_KERNEL); 34462306a36Sopenharmony_ci if (!req) 34562306a36Sopenharmony_ci return -ENOMEM; 34662306a36Sopenharmony_ci 34762306a36Sopenharmony_ci crypto_init_wait(&wait); 34862306a36Sopenharmony_ci ahash_request_set_callback(req, CRYPTO_TFM_REQ_MAY_BACKLOG | 34962306a36Sopenharmony_ci CRYPTO_TFM_REQ_MAY_SLEEP, 35062306a36Sopenharmony_ci crypto_req_done, &wait); 35162306a36Sopenharmony_ci 35262306a36Sopenharmony_ci rc = ahash_wait(crypto_ahash_init(req), &wait); 35362306a36Sopenharmony_ci if (rc) 35462306a36Sopenharmony_ci goto out1; 35562306a36Sopenharmony_ci 35662306a36Sopenharmony_ci i_size = i_size_read(file_inode(file)); 35762306a36Sopenharmony_ci 35862306a36Sopenharmony_ci if (i_size == 0) 35962306a36Sopenharmony_ci goto out2; 36062306a36Sopenharmony_ci 36162306a36Sopenharmony_ci /* 36262306a36Sopenharmony_ci * Try to allocate maximum size of memory. 36362306a36Sopenharmony_ci * Fail if even a single page cannot be allocated. 36462306a36Sopenharmony_ci */ 36562306a36Sopenharmony_ci rbuf[0] = ima_alloc_pages(i_size, &rbuf_size[0], 1); 36662306a36Sopenharmony_ci if (!rbuf[0]) { 36762306a36Sopenharmony_ci rc = -ENOMEM; 36862306a36Sopenharmony_ci goto out1; 36962306a36Sopenharmony_ci } 37062306a36Sopenharmony_ci 37162306a36Sopenharmony_ci /* Only allocate one buffer if that is enough. */ 37262306a36Sopenharmony_ci if (i_size > rbuf_size[0]) { 37362306a36Sopenharmony_ci /* 37462306a36Sopenharmony_ci * Try to allocate secondary buffer. If that fails fallback to 37562306a36Sopenharmony_ci * using single buffering. Use previous memory allocation size 37662306a36Sopenharmony_ci * as baseline for possible allocation size. 37762306a36Sopenharmony_ci */ 37862306a36Sopenharmony_ci rbuf[1] = ima_alloc_pages(i_size - rbuf_size[0], 37962306a36Sopenharmony_ci &rbuf_size[1], 0); 38062306a36Sopenharmony_ci } 38162306a36Sopenharmony_ci 38262306a36Sopenharmony_ci for (offset = 0; offset < i_size; offset += rbuf_len) { 38362306a36Sopenharmony_ci if (!rbuf[1] && offset) { 38462306a36Sopenharmony_ci /* Not using two buffers, and it is not the first 38562306a36Sopenharmony_ci * read/request, wait for the completion of the 38662306a36Sopenharmony_ci * previous ahash_update() request. 38762306a36Sopenharmony_ci */ 38862306a36Sopenharmony_ci rc = ahash_wait(ahash_rc, &wait); 38962306a36Sopenharmony_ci if (rc) 39062306a36Sopenharmony_ci goto out3; 39162306a36Sopenharmony_ci } 39262306a36Sopenharmony_ci /* read buffer */ 39362306a36Sopenharmony_ci rbuf_len = min_t(loff_t, i_size - offset, rbuf_size[active]); 39462306a36Sopenharmony_ci rc = integrity_kernel_read(file, offset, rbuf[active], 39562306a36Sopenharmony_ci rbuf_len); 39662306a36Sopenharmony_ci if (rc != rbuf_len) { 39762306a36Sopenharmony_ci if (rc >= 0) 39862306a36Sopenharmony_ci rc = -EINVAL; 39962306a36Sopenharmony_ci /* 40062306a36Sopenharmony_ci * Forward current rc, do not overwrite with return value 40162306a36Sopenharmony_ci * from ahash_wait() 40262306a36Sopenharmony_ci */ 40362306a36Sopenharmony_ci ahash_wait(ahash_rc, &wait); 40462306a36Sopenharmony_ci goto out3; 40562306a36Sopenharmony_ci } 40662306a36Sopenharmony_ci 40762306a36Sopenharmony_ci if (rbuf[1] && offset) { 40862306a36Sopenharmony_ci /* Using two buffers, and it is not the first 40962306a36Sopenharmony_ci * read/request, wait for the completion of the 41062306a36Sopenharmony_ci * previous ahash_update() request. 41162306a36Sopenharmony_ci */ 41262306a36Sopenharmony_ci rc = ahash_wait(ahash_rc, &wait); 41362306a36Sopenharmony_ci if (rc) 41462306a36Sopenharmony_ci goto out3; 41562306a36Sopenharmony_ci } 41662306a36Sopenharmony_ci 41762306a36Sopenharmony_ci sg_init_one(&sg[0], rbuf[active], rbuf_len); 41862306a36Sopenharmony_ci ahash_request_set_crypt(req, sg, NULL, rbuf_len); 41962306a36Sopenharmony_ci 42062306a36Sopenharmony_ci ahash_rc = crypto_ahash_update(req); 42162306a36Sopenharmony_ci 42262306a36Sopenharmony_ci if (rbuf[1]) 42362306a36Sopenharmony_ci active = !active; /* swap buffers, if we use two */ 42462306a36Sopenharmony_ci } 42562306a36Sopenharmony_ci /* wait for the last update request to complete */ 42662306a36Sopenharmony_ci rc = ahash_wait(ahash_rc, &wait); 42762306a36Sopenharmony_ciout3: 42862306a36Sopenharmony_ci ima_free_pages(rbuf[0], rbuf_size[0]); 42962306a36Sopenharmony_ci ima_free_pages(rbuf[1], rbuf_size[1]); 43062306a36Sopenharmony_ciout2: 43162306a36Sopenharmony_ci if (!rc) { 43262306a36Sopenharmony_ci ahash_request_set_crypt(req, NULL, hash->digest, 0); 43362306a36Sopenharmony_ci rc = ahash_wait(crypto_ahash_final(req), &wait); 43462306a36Sopenharmony_ci } 43562306a36Sopenharmony_ciout1: 43662306a36Sopenharmony_ci ahash_request_free(req); 43762306a36Sopenharmony_ci return rc; 43862306a36Sopenharmony_ci} 43962306a36Sopenharmony_ci 44062306a36Sopenharmony_cistatic int ima_calc_file_ahash(struct file *file, struct ima_digest_data *hash) 44162306a36Sopenharmony_ci{ 44262306a36Sopenharmony_ci struct crypto_ahash *tfm; 44362306a36Sopenharmony_ci int rc; 44462306a36Sopenharmony_ci 44562306a36Sopenharmony_ci tfm = ima_alloc_atfm(hash->algo); 44662306a36Sopenharmony_ci if (IS_ERR(tfm)) 44762306a36Sopenharmony_ci return PTR_ERR(tfm); 44862306a36Sopenharmony_ci 44962306a36Sopenharmony_ci rc = ima_calc_file_hash_atfm(file, hash, tfm); 45062306a36Sopenharmony_ci 45162306a36Sopenharmony_ci ima_free_atfm(tfm); 45262306a36Sopenharmony_ci 45362306a36Sopenharmony_ci return rc; 45462306a36Sopenharmony_ci} 45562306a36Sopenharmony_ci 45662306a36Sopenharmony_cistatic int ima_calc_file_hash_tfm(struct file *file, 45762306a36Sopenharmony_ci struct ima_digest_data *hash, 45862306a36Sopenharmony_ci struct crypto_shash *tfm) 45962306a36Sopenharmony_ci{ 46062306a36Sopenharmony_ci loff_t i_size, offset = 0; 46162306a36Sopenharmony_ci char *rbuf; 46262306a36Sopenharmony_ci int rc; 46362306a36Sopenharmony_ci SHASH_DESC_ON_STACK(shash, tfm); 46462306a36Sopenharmony_ci 46562306a36Sopenharmony_ci shash->tfm = tfm; 46662306a36Sopenharmony_ci 46762306a36Sopenharmony_ci hash->length = crypto_shash_digestsize(tfm); 46862306a36Sopenharmony_ci 46962306a36Sopenharmony_ci rc = crypto_shash_init(shash); 47062306a36Sopenharmony_ci if (rc != 0) 47162306a36Sopenharmony_ci return rc; 47262306a36Sopenharmony_ci 47362306a36Sopenharmony_ci i_size = i_size_read(file_inode(file)); 47462306a36Sopenharmony_ci 47562306a36Sopenharmony_ci if (i_size == 0) 47662306a36Sopenharmony_ci goto out; 47762306a36Sopenharmony_ci 47862306a36Sopenharmony_ci rbuf = kzalloc(PAGE_SIZE, GFP_KERNEL); 47962306a36Sopenharmony_ci if (!rbuf) 48062306a36Sopenharmony_ci return -ENOMEM; 48162306a36Sopenharmony_ci 48262306a36Sopenharmony_ci while (offset < i_size) { 48362306a36Sopenharmony_ci int rbuf_len; 48462306a36Sopenharmony_ci 48562306a36Sopenharmony_ci rbuf_len = integrity_kernel_read(file, offset, rbuf, PAGE_SIZE); 48662306a36Sopenharmony_ci if (rbuf_len < 0) { 48762306a36Sopenharmony_ci rc = rbuf_len; 48862306a36Sopenharmony_ci break; 48962306a36Sopenharmony_ci } 49062306a36Sopenharmony_ci if (rbuf_len == 0) { /* unexpected EOF */ 49162306a36Sopenharmony_ci rc = -EINVAL; 49262306a36Sopenharmony_ci break; 49362306a36Sopenharmony_ci } 49462306a36Sopenharmony_ci offset += rbuf_len; 49562306a36Sopenharmony_ci 49662306a36Sopenharmony_ci rc = crypto_shash_update(shash, rbuf, rbuf_len); 49762306a36Sopenharmony_ci if (rc) 49862306a36Sopenharmony_ci break; 49962306a36Sopenharmony_ci } 50062306a36Sopenharmony_ci kfree(rbuf); 50162306a36Sopenharmony_ciout: 50262306a36Sopenharmony_ci if (!rc) 50362306a36Sopenharmony_ci rc = crypto_shash_final(shash, hash->digest); 50462306a36Sopenharmony_ci return rc; 50562306a36Sopenharmony_ci} 50662306a36Sopenharmony_ci 50762306a36Sopenharmony_cistatic int ima_calc_file_shash(struct file *file, struct ima_digest_data *hash) 50862306a36Sopenharmony_ci{ 50962306a36Sopenharmony_ci struct crypto_shash *tfm; 51062306a36Sopenharmony_ci int rc; 51162306a36Sopenharmony_ci 51262306a36Sopenharmony_ci tfm = ima_alloc_tfm(hash->algo); 51362306a36Sopenharmony_ci if (IS_ERR(tfm)) 51462306a36Sopenharmony_ci return PTR_ERR(tfm); 51562306a36Sopenharmony_ci 51662306a36Sopenharmony_ci rc = ima_calc_file_hash_tfm(file, hash, tfm); 51762306a36Sopenharmony_ci 51862306a36Sopenharmony_ci ima_free_tfm(tfm); 51962306a36Sopenharmony_ci 52062306a36Sopenharmony_ci return rc; 52162306a36Sopenharmony_ci} 52262306a36Sopenharmony_ci 52362306a36Sopenharmony_ci/* 52462306a36Sopenharmony_ci * ima_calc_file_hash - calculate file hash 52562306a36Sopenharmony_ci * 52662306a36Sopenharmony_ci * Asynchronous hash (ahash) allows using HW acceleration for calculating 52762306a36Sopenharmony_ci * a hash. ahash performance varies for different data sizes on different 52862306a36Sopenharmony_ci * crypto accelerators. shash performance might be better for smaller files. 52962306a36Sopenharmony_ci * The 'ima.ahash_minsize' module parameter allows specifying the best 53062306a36Sopenharmony_ci * minimum file size for using ahash on the system. 53162306a36Sopenharmony_ci * 53262306a36Sopenharmony_ci * If the ima.ahash_minsize parameter is not specified, this function uses 53362306a36Sopenharmony_ci * shash for the hash calculation. If ahash fails, it falls back to using 53462306a36Sopenharmony_ci * shash. 53562306a36Sopenharmony_ci */ 53662306a36Sopenharmony_ciint ima_calc_file_hash(struct file *file, struct ima_digest_data *hash) 53762306a36Sopenharmony_ci{ 53862306a36Sopenharmony_ci loff_t i_size; 53962306a36Sopenharmony_ci int rc; 54062306a36Sopenharmony_ci struct file *f = file; 54162306a36Sopenharmony_ci bool new_file_instance = false; 54262306a36Sopenharmony_ci 54362306a36Sopenharmony_ci /* 54462306a36Sopenharmony_ci * For consistency, fail file's opened with the O_DIRECT flag on 54562306a36Sopenharmony_ci * filesystems mounted with/without DAX option. 54662306a36Sopenharmony_ci */ 54762306a36Sopenharmony_ci if (file->f_flags & O_DIRECT) { 54862306a36Sopenharmony_ci hash->length = hash_digest_size[ima_hash_algo]; 54962306a36Sopenharmony_ci hash->algo = ima_hash_algo; 55062306a36Sopenharmony_ci return -EINVAL; 55162306a36Sopenharmony_ci } 55262306a36Sopenharmony_ci 55362306a36Sopenharmony_ci /* Open a new file instance in O_RDONLY if we cannot read */ 55462306a36Sopenharmony_ci if (!(file->f_mode & FMODE_READ)) { 55562306a36Sopenharmony_ci int flags = file->f_flags & ~(O_WRONLY | O_APPEND | 55662306a36Sopenharmony_ci O_TRUNC | O_CREAT | O_NOCTTY | O_EXCL); 55762306a36Sopenharmony_ci flags |= O_RDONLY; 55862306a36Sopenharmony_ci f = dentry_open(&file->f_path, flags, file->f_cred); 55962306a36Sopenharmony_ci if (IS_ERR(f)) 56062306a36Sopenharmony_ci return PTR_ERR(f); 56162306a36Sopenharmony_ci 56262306a36Sopenharmony_ci new_file_instance = true; 56362306a36Sopenharmony_ci } 56462306a36Sopenharmony_ci 56562306a36Sopenharmony_ci i_size = i_size_read(file_inode(f)); 56662306a36Sopenharmony_ci 56762306a36Sopenharmony_ci if (ima_ahash_minsize && i_size >= ima_ahash_minsize) { 56862306a36Sopenharmony_ci rc = ima_calc_file_ahash(f, hash); 56962306a36Sopenharmony_ci if (!rc) 57062306a36Sopenharmony_ci goto out; 57162306a36Sopenharmony_ci } 57262306a36Sopenharmony_ci 57362306a36Sopenharmony_ci rc = ima_calc_file_shash(f, hash); 57462306a36Sopenharmony_ciout: 57562306a36Sopenharmony_ci if (new_file_instance) 57662306a36Sopenharmony_ci fput(f); 57762306a36Sopenharmony_ci return rc; 57862306a36Sopenharmony_ci} 57962306a36Sopenharmony_ci 58062306a36Sopenharmony_ci/* 58162306a36Sopenharmony_ci * Calculate the hash of template data 58262306a36Sopenharmony_ci */ 58362306a36Sopenharmony_cistatic int ima_calc_field_array_hash_tfm(struct ima_field_data *field_data, 58462306a36Sopenharmony_ci struct ima_template_entry *entry, 58562306a36Sopenharmony_ci int tfm_idx) 58662306a36Sopenharmony_ci{ 58762306a36Sopenharmony_ci SHASH_DESC_ON_STACK(shash, ima_algo_array[tfm_idx].tfm); 58862306a36Sopenharmony_ci struct ima_template_desc *td = entry->template_desc; 58962306a36Sopenharmony_ci int num_fields = entry->template_desc->num_fields; 59062306a36Sopenharmony_ci int rc, i; 59162306a36Sopenharmony_ci 59262306a36Sopenharmony_ci shash->tfm = ima_algo_array[tfm_idx].tfm; 59362306a36Sopenharmony_ci 59462306a36Sopenharmony_ci rc = crypto_shash_init(shash); 59562306a36Sopenharmony_ci if (rc != 0) 59662306a36Sopenharmony_ci return rc; 59762306a36Sopenharmony_ci 59862306a36Sopenharmony_ci for (i = 0; i < num_fields; i++) { 59962306a36Sopenharmony_ci u8 buffer[IMA_EVENT_NAME_LEN_MAX + 1] = { 0 }; 60062306a36Sopenharmony_ci u8 *data_to_hash = field_data[i].data; 60162306a36Sopenharmony_ci u32 datalen = field_data[i].len; 60262306a36Sopenharmony_ci u32 datalen_to_hash = !ima_canonical_fmt ? 60362306a36Sopenharmony_ci datalen : (__force u32)cpu_to_le32(datalen); 60462306a36Sopenharmony_ci 60562306a36Sopenharmony_ci if (strcmp(td->name, IMA_TEMPLATE_IMA_NAME) != 0) { 60662306a36Sopenharmony_ci rc = crypto_shash_update(shash, 60762306a36Sopenharmony_ci (const u8 *) &datalen_to_hash, 60862306a36Sopenharmony_ci sizeof(datalen_to_hash)); 60962306a36Sopenharmony_ci if (rc) 61062306a36Sopenharmony_ci break; 61162306a36Sopenharmony_ci } else if (strcmp(td->fields[i]->field_id, "n") == 0) { 61262306a36Sopenharmony_ci memcpy(buffer, data_to_hash, datalen); 61362306a36Sopenharmony_ci data_to_hash = buffer; 61462306a36Sopenharmony_ci datalen = IMA_EVENT_NAME_LEN_MAX + 1; 61562306a36Sopenharmony_ci } 61662306a36Sopenharmony_ci rc = crypto_shash_update(shash, data_to_hash, datalen); 61762306a36Sopenharmony_ci if (rc) 61862306a36Sopenharmony_ci break; 61962306a36Sopenharmony_ci } 62062306a36Sopenharmony_ci 62162306a36Sopenharmony_ci if (!rc) 62262306a36Sopenharmony_ci rc = crypto_shash_final(shash, entry->digests[tfm_idx].digest); 62362306a36Sopenharmony_ci 62462306a36Sopenharmony_ci return rc; 62562306a36Sopenharmony_ci} 62662306a36Sopenharmony_ci 62762306a36Sopenharmony_ciint ima_calc_field_array_hash(struct ima_field_data *field_data, 62862306a36Sopenharmony_ci struct ima_template_entry *entry) 62962306a36Sopenharmony_ci{ 63062306a36Sopenharmony_ci u16 alg_id; 63162306a36Sopenharmony_ci int rc, i; 63262306a36Sopenharmony_ci 63362306a36Sopenharmony_ci rc = ima_calc_field_array_hash_tfm(field_data, entry, ima_sha1_idx); 63462306a36Sopenharmony_ci if (rc) 63562306a36Sopenharmony_ci return rc; 63662306a36Sopenharmony_ci 63762306a36Sopenharmony_ci entry->digests[ima_sha1_idx].alg_id = TPM_ALG_SHA1; 63862306a36Sopenharmony_ci 63962306a36Sopenharmony_ci for (i = 0; i < NR_BANKS(ima_tpm_chip) + ima_extra_slots; i++) { 64062306a36Sopenharmony_ci if (i == ima_sha1_idx) 64162306a36Sopenharmony_ci continue; 64262306a36Sopenharmony_ci 64362306a36Sopenharmony_ci if (i < NR_BANKS(ima_tpm_chip)) { 64462306a36Sopenharmony_ci alg_id = ima_tpm_chip->allocated_banks[i].alg_id; 64562306a36Sopenharmony_ci entry->digests[i].alg_id = alg_id; 64662306a36Sopenharmony_ci } 64762306a36Sopenharmony_ci 64862306a36Sopenharmony_ci /* for unmapped TPM algorithms digest is still a padded SHA1 */ 64962306a36Sopenharmony_ci if (!ima_algo_array[i].tfm) { 65062306a36Sopenharmony_ci memcpy(entry->digests[i].digest, 65162306a36Sopenharmony_ci entry->digests[ima_sha1_idx].digest, 65262306a36Sopenharmony_ci TPM_DIGEST_SIZE); 65362306a36Sopenharmony_ci continue; 65462306a36Sopenharmony_ci } 65562306a36Sopenharmony_ci 65662306a36Sopenharmony_ci rc = ima_calc_field_array_hash_tfm(field_data, entry, i); 65762306a36Sopenharmony_ci if (rc) 65862306a36Sopenharmony_ci return rc; 65962306a36Sopenharmony_ci } 66062306a36Sopenharmony_ci return rc; 66162306a36Sopenharmony_ci} 66262306a36Sopenharmony_ci 66362306a36Sopenharmony_cistatic int calc_buffer_ahash_atfm(const void *buf, loff_t len, 66462306a36Sopenharmony_ci struct ima_digest_data *hash, 66562306a36Sopenharmony_ci struct crypto_ahash *tfm) 66662306a36Sopenharmony_ci{ 66762306a36Sopenharmony_ci struct ahash_request *req; 66862306a36Sopenharmony_ci struct scatterlist sg; 66962306a36Sopenharmony_ci struct crypto_wait wait; 67062306a36Sopenharmony_ci int rc, ahash_rc = 0; 67162306a36Sopenharmony_ci 67262306a36Sopenharmony_ci hash->length = crypto_ahash_digestsize(tfm); 67362306a36Sopenharmony_ci 67462306a36Sopenharmony_ci req = ahash_request_alloc(tfm, GFP_KERNEL); 67562306a36Sopenharmony_ci if (!req) 67662306a36Sopenharmony_ci return -ENOMEM; 67762306a36Sopenharmony_ci 67862306a36Sopenharmony_ci crypto_init_wait(&wait); 67962306a36Sopenharmony_ci ahash_request_set_callback(req, CRYPTO_TFM_REQ_MAY_BACKLOG | 68062306a36Sopenharmony_ci CRYPTO_TFM_REQ_MAY_SLEEP, 68162306a36Sopenharmony_ci crypto_req_done, &wait); 68262306a36Sopenharmony_ci 68362306a36Sopenharmony_ci rc = ahash_wait(crypto_ahash_init(req), &wait); 68462306a36Sopenharmony_ci if (rc) 68562306a36Sopenharmony_ci goto out; 68662306a36Sopenharmony_ci 68762306a36Sopenharmony_ci sg_init_one(&sg, buf, len); 68862306a36Sopenharmony_ci ahash_request_set_crypt(req, &sg, NULL, len); 68962306a36Sopenharmony_ci 69062306a36Sopenharmony_ci ahash_rc = crypto_ahash_update(req); 69162306a36Sopenharmony_ci 69262306a36Sopenharmony_ci /* wait for the update request to complete */ 69362306a36Sopenharmony_ci rc = ahash_wait(ahash_rc, &wait); 69462306a36Sopenharmony_ci if (!rc) { 69562306a36Sopenharmony_ci ahash_request_set_crypt(req, NULL, hash->digest, 0); 69662306a36Sopenharmony_ci rc = ahash_wait(crypto_ahash_final(req), &wait); 69762306a36Sopenharmony_ci } 69862306a36Sopenharmony_ciout: 69962306a36Sopenharmony_ci ahash_request_free(req); 70062306a36Sopenharmony_ci return rc; 70162306a36Sopenharmony_ci} 70262306a36Sopenharmony_ci 70362306a36Sopenharmony_cistatic int calc_buffer_ahash(const void *buf, loff_t len, 70462306a36Sopenharmony_ci struct ima_digest_data *hash) 70562306a36Sopenharmony_ci{ 70662306a36Sopenharmony_ci struct crypto_ahash *tfm; 70762306a36Sopenharmony_ci int rc; 70862306a36Sopenharmony_ci 70962306a36Sopenharmony_ci tfm = ima_alloc_atfm(hash->algo); 71062306a36Sopenharmony_ci if (IS_ERR(tfm)) 71162306a36Sopenharmony_ci return PTR_ERR(tfm); 71262306a36Sopenharmony_ci 71362306a36Sopenharmony_ci rc = calc_buffer_ahash_atfm(buf, len, hash, tfm); 71462306a36Sopenharmony_ci 71562306a36Sopenharmony_ci ima_free_atfm(tfm); 71662306a36Sopenharmony_ci 71762306a36Sopenharmony_ci return rc; 71862306a36Sopenharmony_ci} 71962306a36Sopenharmony_ci 72062306a36Sopenharmony_cistatic int calc_buffer_shash_tfm(const void *buf, loff_t size, 72162306a36Sopenharmony_ci struct ima_digest_data *hash, 72262306a36Sopenharmony_ci struct crypto_shash *tfm) 72362306a36Sopenharmony_ci{ 72462306a36Sopenharmony_ci SHASH_DESC_ON_STACK(shash, tfm); 72562306a36Sopenharmony_ci unsigned int len; 72662306a36Sopenharmony_ci int rc; 72762306a36Sopenharmony_ci 72862306a36Sopenharmony_ci shash->tfm = tfm; 72962306a36Sopenharmony_ci 73062306a36Sopenharmony_ci hash->length = crypto_shash_digestsize(tfm); 73162306a36Sopenharmony_ci 73262306a36Sopenharmony_ci rc = crypto_shash_init(shash); 73362306a36Sopenharmony_ci if (rc != 0) 73462306a36Sopenharmony_ci return rc; 73562306a36Sopenharmony_ci 73662306a36Sopenharmony_ci while (size) { 73762306a36Sopenharmony_ci len = size < PAGE_SIZE ? size : PAGE_SIZE; 73862306a36Sopenharmony_ci rc = crypto_shash_update(shash, buf, len); 73962306a36Sopenharmony_ci if (rc) 74062306a36Sopenharmony_ci break; 74162306a36Sopenharmony_ci buf += len; 74262306a36Sopenharmony_ci size -= len; 74362306a36Sopenharmony_ci } 74462306a36Sopenharmony_ci 74562306a36Sopenharmony_ci if (!rc) 74662306a36Sopenharmony_ci rc = crypto_shash_final(shash, hash->digest); 74762306a36Sopenharmony_ci return rc; 74862306a36Sopenharmony_ci} 74962306a36Sopenharmony_ci 75062306a36Sopenharmony_cistatic int calc_buffer_shash(const void *buf, loff_t len, 75162306a36Sopenharmony_ci struct ima_digest_data *hash) 75262306a36Sopenharmony_ci{ 75362306a36Sopenharmony_ci struct crypto_shash *tfm; 75462306a36Sopenharmony_ci int rc; 75562306a36Sopenharmony_ci 75662306a36Sopenharmony_ci tfm = ima_alloc_tfm(hash->algo); 75762306a36Sopenharmony_ci if (IS_ERR(tfm)) 75862306a36Sopenharmony_ci return PTR_ERR(tfm); 75962306a36Sopenharmony_ci 76062306a36Sopenharmony_ci rc = calc_buffer_shash_tfm(buf, len, hash, tfm); 76162306a36Sopenharmony_ci 76262306a36Sopenharmony_ci ima_free_tfm(tfm); 76362306a36Sopenharmony_ci return rc; 76462306a36Sopenharmony_ci} 76562306a36Sopenharmony_ci 76662306a36Sopenharmony_ciint ima_calc_buffer_hash(const void *buf, loff_t len, 76762306a36Sopenharmony_ci struct ima_digest_data *hash) 76862306a36Sopenharmony_ci{ 76962306a36Sopenharmony_ci int rc; 77062306a36Sopenharmony_ci 77162306a36Sopenharmony_ci if (ima_ahash_minsize && len >= ima_ahash_minsize) { 77262306a36Sopenharmony_ci rc = calc_buffer_ahash(buf, len, hash); 77362306a36Sopenharmony_ci if (!rc) 77462306a36Sopenharmony_ci return 0; 77562306a36Sopenharmony_ci } 77662306a36Sopenharmony_ci 77762306a36Sopenharmony_ci return calc_buffer_shash(buf, len, hash); 77862306a36Sopenharmony_ci} 77962306a36Sopenharmony_ci 78062306a36Sopenharmony_cistatic void ima_pcrread(u32 idx, struct tpm_digest *d) 78162306a36Sopenharmony_ci{ 78262306a36Sopenharmony_ci if (!ima_tpm_chip) 78362306a36Sopenharmony_ci return; 78462306a36Sopenharmony_ci 78562306a36Sopenharmony_ci if (tpm_pcr_read(ima_tpm_chip, idx, d) != 0) 78662306a36Sopenharmony_ci pr_err("Error Communicating to TPM chip\n"); 78762306a36Sopenharmony_ci} 78862306a36Sopenharmony_ci 78962306a36Sopenharmony_ci/* 79062306a36Sopenharmony_ci * The boot_aggregate is a cumulative hash over TPM registers 0 - 7. With 79162306a36Sopenharmony_ci * TPM 1.2 the boot_aggregate was based on reading the SHA1 PCRs, but with 79262306a36Sopenharmony_ci * TPM 2.0 hash agility, TPM chips could support multiple TPM PCR banks, 79362306a36Sopenharmony_ci * allowing firmware to configure and enable different banks. 79462306a36Sopenharmony_ci * 79562306a36Sopenharmony_ci * Knowing which TPM bank is read to calculate the boot_aggregate digest 79662306a36Sopenharmony_ci * needs to be conveyed to a verifier. For this reason, use the same 79762306a36Sopenharmony_ci * hash algorithm for reading the TPM PCRs as for calculating the boot 79862306a36Sopenharmony_ci * aggregate digest as stored in the measurement list. 79962306a36Sopenharmony_ci */ 80062306a36Sopenharmony_cistatic int ima_calc_boot_aggregate_tfm(char *digest, u16 alg_id, 80162306a36Sopenharmony_ci struct crypto_shash *tfm) 80262306a36Sopenharmony_ci{ 80362306a36Sopenharmony_ci struct tpm_digest d = { .alg_id = alg_id, .digest = {0} }; 80462306a36Sopenharmony_ci int rc; 80562306a36Sopenharmony_ci u32 i; 80662306a36Sopenharmony_ci SHASH_DESC_ON_STACK(shash, tfm); 80762306a36Sopenharmony_ci 80862306a36Sopenharmony_ci shash->tfm = tfm; 80962306a36Sopenharmony_ci 81062306a36Sopenharmony_ci pr_devel("calculating the boot-aggregate based on TPM bank: %04x\n", 81162306a36Sopenharmony_ci d.alg_id); 81262306a36Sopenharmony_ci 81362306a36Sopenharmony_ci rc = crypto_shash_init(shash); 81462306a36Sopenharmony_ci if (rc != 0) 81562306a36Sopenharmony_ci return rc; 81662306a36Sopenharmony_ci 81762306a36Sopenharmony_ci /* cumulative digest over TPM registers 0-7 */ 81862306a36Sopenharmony_ci for (i = TPM_PCR0; i < TPM_PCR8; i++) { 81962306a36Sopenharmony_ci ima_pcrread(i, &d); 82062306a36Sopenharmony_ci /* now accumulate with current aggregate */ 82162306a36Sopenharmony_ci rc = crypto_shash_update(shash, d.digest, 82262306a36Sopenharmony_ci crypto_shash_digestsize(tfm)); 82362306a36Sopenharmony_ci if (rc != 0) 82462306a36Sopenharmony_ci return rc; 82562306a36Sopenharmony_ci } 82662306a36Sopenharmony_ci /* 82762306a36Sopenharmony_ci * Extend cumulative digest over TPM registers 8-9, which contain 82862306a36Sopenharmony_ci * measurement for the kernel command line (reg. 8) and image (reg. 9) 82962306a36Sopenharmony_ci * in a typical PCR allocation. Registers 8-9 are only included in 83062306a36Sopenharmony_ci * non-SHA1 boot_aggregate digests to avoid ambiguity. 83162306a36Sopenharmony_ci */ 83262306a36Sopenharmony_ci if (alg_id != TPM_ALG_SHA1) { 83362306a36Sopenharmony_ci for (i = TPM_PCR8; i < TPM_PCR10; i++) { 83462306a36Sopenharmony_ci ima_pcrread(i, &d); 83562306a36Sopenharmony_ci rc = crypto_shash_update(shash, d.digest, 83662306a36Sopenharmony_ci crypto_shash_digestsize(tfm)); 83762306a36Sopenharmony_ci } 83862306a36Sopenharmony_ci } 83962306a36Sopenharmony_ci if (!rc) 84062306a36Sopenharmony_ci crypto_shash_final(shash, digest); 84162306a36Sopenharmony_ci return rc; 84262306a36Sopenharmony_ci} 84362306a36Sopenharmony_ci 84462306a36Sopenharmony_ciint ima_calc_boot_aggregate(struct ima_digest_data *hash) 84562306a36Sopenharmony_ci{ 84662306a36Sopenharmony_ci struct crypto_shash *tfm; 84762306a36Sopenharmony_ci u16 crypto_id, alg_id; 84862306a36Sopenharmony_ci int rc, i, bank_idx = -1; 84962306a36Sopenharmony_ci 85062306a36Sopenharmony_ci for (i = 0; i < ima_tpm_chip->nr_allocated_banks; i++) { 85162306a36Sopenharmony_ci crypto_id = ima_tpm_chip->allocated_banks[i].crypto_id; 85262306a36Sopenharmony_ci if (crypto_id == hash->algo) { 85362306a36Sopenharmony_ci bank_idx = i; 85462306a36Sopenharmony_ci break; 85562306a36Sopenharmony_ci } 85662306a36Sopenharmony_ci 85762306a36Sopenharmony_ci if (crypto_id == HASH_ALGO_SHA256) 85862306a36Sopenharmony_ci bank_idx = i; 85962306a36Sopenharmony_ci 86062306a36Sopenharmony_ci if (bank_idx == -1 && crypto_id == HASH_ALGO_SHA1) 86162306a36Sopenharmony_ci bank_idx = i; 86262306a36Sopenharmony_ci } 86362306a36Sopenharmony_ci 86462306a36Sopenharmony_ci if (bank_idx == -1) { 86562306a36Sopenharmony_ci pr_err("No suitable TPM algorithm for boot aggregate\n"); 86662306a36Sopenharmony_ci return 0; 86762306a36Sopenharmony_ci } 86862306a36Sopenharmony_ci 86962306a36Sopenharmony_ci hash->algo = ima_tpm_chip->allocated_banks[bank_idx].crypto_id; 87062306a36Sopenharmony_ci 87162306a36Sopenharmony_ci tfm = ima_alloc_tfm(hash->algo); 87262306a36Sopenharmony_ci if (IS_ERR(tfm)) 87362306a36Sopenharmony_ci return PTR_ERR(tfm); 87462306a36Sopenharmony_ci 87562306a36Sopenharmony_ci hash->length = crypto_shash_digestsize(tfm); 87662306a36Sopenharmony_ci alg_id = ima_tpm_chip->allocated_banks[bank_idx].alg_id; 87762306a36Sopenharmony_ci rc = ima_calc_boot_aggregate_tfm(hash->digest, alg_id, tfm); 87862306a36Sopenharmony_ci 87962306a36Sopenharmony_ci ima_free_tfm(tfm); 88062306a36Sopenharmony_ci 88162306a36Sopenharmony_ci return rc; 88262306a36Sopenharmony_ci} 883