Back to home page

OSCL-LXR

 
 

    


0001 // SPDX-License-Identifier: GPL-2.0-only
0002 /*
0003  * Copyright (C) 2005,2006,2007,2008 IBM Corporation
0004  *
0005  * Authors:
0006  * Mimi Zohar <zohar@us.ibm.com>
0007  * Kylene Hall <kjhall@us.ibm.com>
0008  *
0009  * File: ima_crypto.c
0010  *  Calculates md5/sha1 file hash, template hash, boot-aggreate hash
0011  */
0012 
0013 #include <linux/kernel.h>
0014 #include <linux/moduleparam.h>
0015 #include <linux/ratelimit.h>
0016 #include <linux/file.h>
0017 #include <linux/crypto.h>
0018 #include <linux/scatterlist.h>
0019 #include <linux/err.h>
0020 #include <linux/slab.h>
0021 #include <crypto/hash.h>
0022 
0023 #include "ima.h"
0024 
0025 /* minimum file size for ahash use */
0026 static unsigned long ima_ahash_minsize;
0027 module_param_named(ahash_minsize, ima_ahash_minsize, ulong, 0644);
0028 MODULE_PARM_DESC(ahash_minsize, "Minimum file size for ahash use");
0029 
0030 /* default is 0 - 1 page. */
0031 static int ima_maxorder;
0032 static unsigned int ima_bufsize = PAGE_SIZE;
0033 
0034 static int param_set_bufsize(const char *val, const struct kernel_param *kp)
0035 {
0036     unsigned long long size;
0037     int order;
0038 
0039     size = memparse(val, NULL);
0040     order = get_order(size);
0041     if (order >= MAX_ORDER)
0042         return -EINVAL;
0043     ima_maxorder = order;
0044     ima_bufsize = PAGE_SIZE << order;
0045     return 0;
0046 }
0047 
0048 static const struct kernel_param_ops param_ops_bufsize = {
0049     .set = param_set_bufsize,
0050     .get = param_get_uint,
0051 };
0052 #define param_check_bufsize(name, p) __param_check(name, p, unsigned int)
0053 
0054 module_param_named(ahash_bufsize, ima_bufsize, bufsize, 0644);
0055 MODULE_PARM_DESC(ahash_bufsize, "Maximum ahash buffer size");
0056 
0057 static struct crypto_shash *ima_shash_tfm;
0058 static struct crypto_ahash *ima_ahash_tfm;
0059 
0060 struct ima_algo_desc {
0061     struct crypto_shash *tfm;
0062     enum hash_algo algo;
0063 };
0064 
0065 int ima_sha1_idx __ro_after_init;
0066 int ima_hash_algo_idx __ro_after_init;
0067 /*
0068  * Additional number of slots reserved, as needed, for SHA1
0069  * and IMA default algo.
0070  */
0071 int ima_extra_slots __ro_after_init;
0072 
0073 static struct ima_algo_desc *ima_algo_array;
0074 
0075 static int __init ima_init_ima_crypto(void)
0076 {
0077     long rc;
0078 
0079     ima_shash_tfm = crypto_alloc_shash(hash_algo_name[ima_hash_algo], 0, 0);
0080     if (IS_ERR(ima_shash_tfm)) {
0081         rc = PTR_ERR(ima_shash_tfm);
0082         pr_err("Can not allocate %s (reason: %ld)\n",
0083                hash_algo_name[ima_hash_algo], rc);
0084         return rc;
0085     }
0086     pr_info("Allocated hash algorithm: %s\n",
0087         hash_algo_name[ima_hash_algo]);
0088     return 0;
0089 }
0090 
0091 static struct crypto_shash *ima_alloc_tfm(enum hash_algo algo)
0092 {
0093     struct crypto_shash *tfm = ima_shash_tfm;
0094     int rc, i;
0095 
0096     if (algo < 0 || algo >= HASH_ALGO__LAST)
0097         algo = ima_hash_algo;
0098 
0099     if (algo == ima_hash_algo)
0100         return tfm;
0101 
0102     for (i = 0; i < NR_BANKS(ima_tpm_chip) + ima_extra_slots; i++)
0103         if (ima_algo_array[i].tfm && ima_algo_array[i].algo == algo)
0104             return ima_algo_array[i].tfm;
0105 
0106     tfm = crypto_alloc_shash(hash_algo_name[algo], 0, 0);
0107     if (IS_ERR(tfm)) {
0108         rc = PTR_ERR(tfm);
0109         pr_err("Can not allocate %s (reason: %d)\n",
0110                hash_algo_name[algo], rc);
0111     }
0112     return tfm;
0113 }
0114 
0115 int __init ima_init_crypto(void)
0116 {
0117     enum hash_algo algo;
0118     long rc;
0119     int i;
0120 
0121     rc = ima_init_ima_crypto();
0122     if (rc)
0123         return rc;
0124 
0125     ima_sha1_idx = -1;
0126     ima_hash_algo_idx = -1;
0127 
0128     for (i = 0; i < NR_BANKS(ima_tpm_chip); i++) {
0129         algo = ima_tpm_chip->allocated_banks[i].crypto_id;
0130         if (algo == HASH_ALGO_SHA1)
0131             ima_sha1_idx = i;
0132 
0133         if (algo == ima_hash_algo)
0134             ima_hash_algo_idx = i;
0135     }
0136 
0137     if (ima_sha1_idx < 0) {
0138         ima_sha1_idx = NR_BANKS(ima_tpm_chip) + ima_extra_slots++;
0139         if (ima_hash_algo == HASH_ALGO_SHA1)
0140             ima_hash_algo_idx = ima_sha1_idx;
0141     }
0142 
0143     if (ima_hash_algo_idx < 0)
0144         ima_hash_algo_idx = NR_BANKS(ima_tpm_chip) + ima_extra_slots++;
0145 
0146     ima_algo_array = kcalloc(NR_BANKS(ima_tpm_chip) + ima_extra_slots,
0147                  sizeof(*ima_algo_array), GFP_KERNEL);
0148     if (!ima_algo_array) {
0149         rc = -ENOMEM;
0150         goto out;
0151     }
0152 
0153     for (i = 0; i < NR_BANKS(ima_tpm_chip); i++) {
0154         algo = ima_tpm_chip->allocated_banks[i].crypto_id;
0155         ima_algo_array[i].algo = algo;
0156 
0157         /* unknown TPM algorithm */
0158         if (algo == HASH_ALGO__LAST)
0159             continue;
0160 
0161         if (algo == ima_hash_algo) {
0162             ima_algo_array[i].tfm = ima_shash_tfm;
0163             continue;
0164         }
0165 
0166         ima_algo_array[i].tfm = ima_alloc_tfm(algo);
0167         if (IS_ERR(ima_algo_array[i].tfm)) {
0168             if (algo == HASH_ALGO_SHA1) {
0169                 rc = PTR_ERR(ima_algo_array[i].tfm);
0170                 ima_algo_array[i].tfm = NULL;
0171                 goto out_array;
0172             }
0173 
0174             ima_algo_array[i].tfm = NULL;
0175         }
0176     }
0177 
0178     if (ima_sha1_idx >= NR_BANKS(ima_tpm_chip)) {
0179         if (ima_hash_algo == HASH_ALGO_SHA1) {
0180             ima_algo_array[ima_sha1_idx].tfm = ima_shash_tfm;
0181         } else {
0182             ima_algo_array[ima_sha1_idx].tfm =
0183                         ima_alloc_tfm(HASH_ALGO_SHA1);
0184             if (IS_ERR(ima_algo_array[ima_sha1_idx].tfm)) {
0185                 rc = PTR_ERR(ima_algo_array[ima_sha1_idx].tfm);
0186                 goto out_array;
0187             }
0188         }
0189 
0190         ima_algo_array[ima_sha1_idx].algo = HASH_ALGO_SHA1;
0191     }
0192 
0193     if (ima_hash_algo_idx >= NR_BANKS(ima_tpm_chip) &&
0194         ima_hash_algo_idx != ima_sha1_idx) {
0195         ima_algo_array[ima_hash_algo_idx].tfm = ima_shash_tfm;
0196         ima_algo_array[ima_hash_algo_idx].algo = ima_hash_algo;
0197     }
0198 
0199     return 0;
0200 out_array:
0201     for (i = 0; i < NR_BANKS(ima_tpm_chip) + ima_extra_slots; i++) {
0202         if (!ima_algo_array[i].tfm ||
0203             ima_algo_array[i].tfm == ima_shash_tfm)
0204             continue;
0205 
0206         crypto_free_shash(ima_algo_array[i].tfm);
0207     }
0208     kfree(ima_algo_array);
0209 out:
0210     crypto_free_shash(ima_shash_tfm);
0211     return rc;
0212 }
0213 
0214 static void ima_free_tfm(struct crypto_shash *tfm)
0215 {
0216     int i;
0217 
0218     if (tfm == ima_shash_tfm)
0219         return;
0220 
0221     for (i = 0; i < NR_BANKS(ima_tpm_chip) + ima_extra_slots; i++)
0222         if (ima_algo_array[i].tfm == tfm)
0223             return;
0224 
0225     crypto_free_shash(tfm);
0226 }
0227 
0228 /**
0229  * ima_alloc_pages() - Allocate contiguous pages.
0230  * @max_size:       Maximum amount of memory to allocate.
0231  * @allocated_size: Returned size of actual allocation.
0232  * @last_warn:      Should the min_size allocation warn or not.
0233  *
0234  * Tries to do opportunistic allocation for memory first trying to allocate
0235  * max_size amount of memory and then splitting that until zero order is
0236  * reached. Allocation is tried without generating allocation warnings unless
0237  * last_warn is set. Last_warn set affects only last allocation of zero order.
0238  *
0239  * By default, ima_maxorder is 0 and it is equivalent to kmalloc(GFP_KERNEL)
0240  *
0241  * Return pointer to allocated memory, or NULL on failure.
0242  */
0243 static void *ima_alloc_pages(loff_t max_size, size_t *allocated_size,
0244                  int last_warn)
0245 {
0246     void *ptr;
0247     int order = ima_maxorder;
0248     gfp_t gfp_mask = __GFP_RECLAIM | __GFP_NOWARN | __GFP_NORETRY;
0249 
0250     if (order)
0251         order = min(get_order(max_size), order);
0252 
0253     for (; order; order--) {
0254         ptr = (void *)__get_free_pages(gfp_mask, order);
0255         if (ptr) {
0256             *allocated_size = PAGE_SIZE << order;
0257             return ptr;
0258         }
0259     }
0260 
0261     /* order is zero - one page */
0262 
0263     gfp_mask = GFP_KERNEL;
0264 
0265     if (!last_warn)
0266         gfp_mask |= __GFP_NOWARN;
0267 
0268     ptr = (void *)__get_free_pages(gfp_mask, 0);
0269     if (ptr) {
0270         *allocated_size = PAGE_SIZE;
0271         return ptr;
0272     }
0273 
0274     *allocated_size = 0;
0275     return NULL;
0276 }
0277 
0278 /**
0279  * ima_free_pages() - Free pages allocated by ima_alloc_pages().
0280  * @ptr:  Pointer to allocated pages.
0281  * @size: Size of allocated buffer.
0282  */
0283 static void ima_free_pages(void *ptr, size_t size)
0284 {
0285     if (!ptr)
0286         return;
0287     free_pages((unsigned long)ptr, get_order(size));
0288 }
0289 
0290 static struct crypto_ahash *ima_alloc_atfm(enum hash_algo algo)
0291 {
0292     struct crypto_ahash *tfm = ima_ahash_tfm;
0293     int rc;
0294 
0295     if (algo < 0 || algo >= HASH_ALGO__LAST)
0296         algo = ima_hash_algo;
0297 
0298     if (algo != ima_hash_algo || !tfm) {
0299         tfm = crypto_alloc_ahash(hash_algo_name[algo], 0, 0);
0300         if (!IS_ERR(tfm)) {
0301             if (algo == ima_hash_algo)
0302                 ima_ahash_tfm = tfm;
0303         } else {
0304             rc = PTR_ERR(tfm);
0305             pr_err("Can not allocate %s (reason: %d)\n",
0306                    hash_algo_name[algo], rc);
0307         }
0308     }
0309     return tfm;
0310 }
0311 
0312 static void ima_free_atfm(struct crypto_ahash *tfm)
0313 {
0314     if (tfm != ima_ahash_tfm)
0315         crypto_free_ahash(tfm);
0316 }
0317 
0318 static inline int ahash_wait(int err, struct crypto_wait *wait)
0319 {
0320 
0321     err = crypto_wait_req(err, wait);
0322 
0323     if (err)
0324         pr_crit_ratelimited("ahash calculation failed: err: %d\n", err);
0325 
0326     return err;
0327 }
0328 
0329 static int ima_calc_file_hash_atfm(struct file *file,
0330                    struct ima_digest_data *hash,
0331                    struct crypto_ahash *tfm)
0332 {
0333     loff_t i_size, offset;
0334     char *rbuf[2] = { NULL, };
0335     int rc, rbuf_len, active = 0, ahash_rc = 0;
0336     struct ahash_request *req;
0337     struct scatterlist sg[1];
0338     struct crypto_wait wait;
0339     size_t rbuf_size[2];
0340 
0341     hash->length = crypto_ahash_digestsize(tfm);
0342 
0343     req = ahash_request_alloc(tfm, GFP_KERNEL);
0344     if (!req)
0345         return -ENOMEM;
0346 
0347     crypto_init_wait(&wait);
0348     ahash_request_set_callback(req, CRYPTO_TFM_REQ_MAY_BACKLOG |
0349                    CRYPTO_TFM_REQ_MAY_SLEEP,
0350                    crypto_req_done, &wait);
0351 
0352     rc = ahash_wait(crypto_ahash_init(req), &wait);
0353     if (rc)
0354         goto out1;
0355 
0356     i_size = i_size_read(file_inode(file));
0357 
0358     if (i_size == 0)
0359         goto out2;
0360 
0361     /*
0362      * Try to allocate maximum size of memory.
0363      * Fail if even a single page cannot be allocated.
0364      */
0365     rbuf[0] = ima_alloc_pages(i_size, &rbuf_size[0], 1);
0366     if (!rbuf[0]) {
0367         rc = -ENOMEM;
0368         goto out1;
0369     }
0370 
0371     /* Only allocate one buffer if that is enough. */
0372     if (i_size > rbuf_size[0]) {
0373         /*
0374          * Try to allocate secondary buffer. If that fails fallback to
0375          * using single buffering. Use previous memory allocation size
0376          * as baseline for possible allocation size.
0377          */
0378         rbuf[1] = ima_alloc_pages(i_size - rbuf_size[0],
0379                       &rbuf_size[1], 0);
0380     }
0381 
0382     for (offset = 0; offset < i_size; offset += rbuf_len) {
0383         if (!rbuf[1] && offset) {
0384             /* Not using two buffers, and it is not the first
0385              * read/request, wait for the completion of the
0386              * previous ahash_update() request.
0387              */
0388             rc = ahash_wait(ahash_rc, &wait);
0389             if (rc)
0390                 goto out3;
0391         }
0392         /* read buffer */
0393         rbuf_len = min_t(loff_t, i_size - offset, rbuf_size[active]);
0394         rc = integrity_kernel_read(file, offset, rbuf[active],
0395                        rbuf_len);
0396         if (rc != rbuf_len) {
0397             if (rc >= 0)
0398                 rc = -EINVAL;
0399             /*
0400              * Forward current rc, do not overwrite with return value
0401              * from ahash_wait()
0402              */
0403             ahash_wait(ahash_rc, &wait);
0404             goto out3;
0405         }
0406 
0407         if (rbuf[1] && offset) {
0408             /* Using two buffers, and it is not the first
0409              * read/request, wait for the completion of the
0410              * previous ahash_update() request.
0411              */
0412             rc = ahash_wait(ahash_rc, &wait);
0413             if (rc)
0414                 goto out3;
0415         }
0416 
0417         sg_init_one(&sg[0], rbuf[active], rbuf_len);
0418         ahash_request_set_crypt(req, sg, NULL, rbuf_len);
0419 
0420         ahash_rc = crypto_ahash_update(req);
0421 
0422         if (rbuf[1])
0423             active = !active; /* swap buffers, if we use two */
0424     }
0425     /* wait for the last update request to complete */
0426     rc = ahash_wait(ahash_rc, &wait);
0427 out3:
0428     ima_free_pages(rbuf[0], rbuf_size[0]);
0429     ima_free_pages(rbuf[1], rbuf_size[1]);
0430 out2:
0431     if (!rc) {
0432         ahash_request_set_crypt(req, NULL, hash->digest, 0);
0433         rc = ahash_wait(crypto_ahash_final(req), &wait);
0434     }
0435 out1:
0436     ahash_request_free(req);
0437     return rc;
0438 }
0439 
0440 static int ima_calc_file_ahash(struct file *file, struct ima_digest_data *hash)
0441 {
0442     struct crypto_ahash *tfm;
0443     int rc;
0444 
0445     tfm = ima_alloc_atfm(hash->algo);
0446     if (IS_ERR(tfm))
0447         return PTR_ERR(tfm);
0448 
0449     rc = ima_calc_file_hash_atfm(file, hash, tfm);
0450 
0451     ima_free_atfm(tfm);
0452 
0453     return rc;
0454 }
0455 
0456 static int ima_calc_file_hash_tfm(struct file *file,
0457                   struct ima_digest_data *hash,
0458                   struct crypto_shash *tfm)
0459 {
0460     loff_t i_size, offset = 0;
0461     char *rbuf;
0462     int rc;
0463     SHASH_DESC_ON_STACK(shash, tfm);
0464 
0465     shash->tfm = tfm;
0466 
0467     hash->length = crypto_shash_digestsize(tfm);
0468 
0469     rc = crypto_shash_init(shash);
0470     if (rc != 0)
0471         return rc;
0472 
0473     i_size = i_size_read(file_inode(file));
0474 
0475     if (i_size == 0)
0476         goto out;
0477 
0478     rbuf = kzalloc(PAGE_SIZE, GFP_KERNEL);
0479     if (!rbuf)
0480         return -ENOMEM;
0481 
0482     while (offset < i_size) {
0483         int rbuf_len;
0484 
0485         rbuf_len = integrity_kernel_read(file, offset, rbuf, PAGE_SIZE);
0486         if (rbuf_len < 0) {
0487             rc = rbuf_len;
0488             break;
0489         }
0490         if (rbuf_len == 0) {    /* unexpected EOF */
0491             rc = -EINVAL;
0492             break;
0493         }
0494         offset += rbuf_len;
0495 
0496         rc = crypto_shash_update(shash, rbuf, rbuf_len);
0497         if (rc)
0498             break;
0499     }
0500     kfree(rbuf);
0501 out:
0502     if (!rc)
0503         rc = crypto_shash_final(shash, hash->digest);
0504     return rc;
0505 }
0506 
0507 static int ima_calc_file_shash(struct file *file, struct ima_digest_data *hash)
0508 {
0509     struct crypto_shash *tfm;
0510     int rc;
0511 
0512     tfm = ima_alloc_tfm(hash->algo);
0513     if (IS_ERR(tfm))
0514         return PTR_ERR(tfm);
0515 
0516     rc = ima_calc_file_hash_tfm(file, hash, tfm);
0517 
0518     ima_free_tfm(tfm);
0519 
0520     return rc;
0521 }
0522 
0523 /*
0524  * ima_calc_file_hash - calculate file hash
0525  *
0526  * Asynchronous hash (ahash) allows using HW acceleration for calculating
0527  * a hash. ahash performance varies for different data sizes on different
0528  * crypto accelerators. shash performance might be better for smaller files.
0529  * The 'ima.ahash_minsize' module parameter allows specifying the best
0530  * minimum file size for using ahash on the system.
0531  *
0532  * If the ima.ahash_minsize parameter is not specified, this function uses
0533  * shash for the hash calculation.  If ahash fails, it falls back to using
0534  * shash.
0535  */
0536 int ima_calc_file_hash(struct file *file, struct ima_digest_data *hash)
0537 {
0538     loff_t i_size;
0539     int rc;
0540     struct file *f = file;
0541     bool new_file_instance = false;
0542 
0543     /*
0544      * For consistency, fail file's opened with the O_DIRECT flag on
0545      * filesystems mounted with/without DAX option.
0546      */
0547     if (file->f_flags & O_DIRECT) {
0548         hash->length = hash_digest_size[ima_hash_algo];
0549         hash->algo = ima_hash_algo;
0550         return -EINVAL;
0551     }
0552 
0553     /* Open a new file instance in O_RDONLY if we cannot read */
0554     if (!(file->f_mode & FMODE_READ)) {
0555         int flags = file->f_flags & ~(O_WRONLY | O_APPEND |
0556                 O_TRUNC | O_CREAT | O_NOCTTY | O_EXCL);
0557         flags |= O_RDONLY;
0558         f = dentry_open(&file->f_path, flags, file->f_cred);
0559         if (IS_ERR(f))
0560             return PTR_ERR(f);
0561 
0562         new_file_instance = true;
0563     }
0564 
0565     i_size = i_size_read(file_inode(f));
0566 
0567     if (ima_ahash_minsize && i_size >= ima_ahash_minsize) {
0568         rc = ima_calc_file_ahash(f, hash);
0569         if (!rc)
0570             goto out;
0571     }
0572 
0573     rc = ima_calc_file_shash(f, hash);
0574 out:
0575     if (new_file_instance)
0576         fput(f);
0577     return rc;
0578 }
0579 
0580 /*
0581  * Calculate the hash of template data
0582  */
0583 static int ima_calc_field_array_hash_tfm(struct ima_field_data *field_data,
0584                      struct ima_template_entry *entry,
0585                      int tfm_idx)
0586 {
0587     SHASH_DESC_ON_STACK(shash, ima_algo_array[tfm_idx].tfm);
0588     struct ima_template_desc *td = entry->template_desc;
0589     int num_fields = entry->template_desc->num_fields;
0590     int rc, i;
0591 
0592     shash->tfm = ima_algo_array[tfm_idx].tfm;
0593 
0594     rc = crypto_shash_init(shash);
0595     if (rc != 0)
0596         return rc;
0597 
0598     for (i = 0; i < num_fields; i++) {
0599         u8 buffer[IMA_EVENT_NAME_LEN_MAX + 1] = { 0 };
0600         u8 *data_to_hash = field_data[i].data;
0601         u32 datalen = field_data[i].len;
0602         u32 datalen_to_hash = !ima_canonical_fmt ?
0603                 datalen : (__force u32)cpu_to_le32(datalen);
0604 
0605         if (strcmp(td->name, IMA_TEMPLATE_IMA_NAME) != 0) {
0606             rc = crypto_shash_update(shash,
0607                         (const u8 *) &datalen_to_hash,
0608                         sizeof(datalen_to_hash));
0609             if (rc)
0610                 break;
0611         } else if (strcmp(td->fields[i]->field_id, "n") == 0) {
0612             memcpy(buffer, data_to_hash, datalen);
0613             data_to_hash = buffer;
0614             datalen = IMA_EVENT_NAME_LEN_MAX + 1;
0615         }
0616         rc = crypto_shash_update(shash, data_to_hash, datalen);
0617         if (rc)
0618             break;
0619     }
0620 
0621     if (!rc)
0622         rc = crypto_shash_final(shash, entry->digests[tfm_idx].digest);
0623 
0624     return rc;
0625 }
0626 
0627 int ima_calc_field_array_hash(struct ima_field_data *field_data,
0628                   struct ima_template_entry *entry)
0629 {
0630     u16 alg_id;
0631     int rc, i;
0632 
0633     rc = ima_calc_field_array_hash_tfm(field_data, entry, ima_sha1_idx);
0634     if (rc)
0635         return rc;
0636 
0637     entry->digests[ima_sha1_idx].alg_id = TPM_ALG_SHA1;
0638 
0639     for (i = 0; i < NR_BANKS(ima_tpm_chip) + ima_extra_slots; i++) {
0640         if (i == ima_sha1_idx)
0641             continue;
0642 
0643         if (i < NR_BANKS(ima_tpm_chip)) {
0644             alg_id = ima_tpm_chip->allocated_banks[i].alg_id;
0645             entry->digests[i].alg_id = alg_id;
0646         }
0647 
0648         /* for unmapped TPM algorithms digest is still a padded SHA1 */
0649         if (!ima_algo_array[i].tfm) {
0650             memcpy(entry->digests[i].digest,
0651                    entry->digests[ima_sha1_idx].digest,
0652                    TPM_DIGEST_SIZE);
0653             continue;
0654         }
0655 
0656         rc = ima_calc_field_array_hash_tfm(field_data, entry, i);
0657         if (rc)
0658             return rc;
0659     }
0660     return rc;
0661 }
0662 
0663 static int calc_buffer_ahash_atfm(const void *buf, loff_t len,
0664                   struct ima_digest_data *hash,
0665                   struct crypto_ahash *tfm)
0666 {
0667     struct ahash_request *req;
0668     struct scatterlist sg;
0669     struct crypto_wait wait;
0670     int rc, ahash_rc = 0;
0671 
0672     hash->length = crypto_ahash_digestsize(tfm);
0673 
0674     req = ahash_request_alloc(tfm, GFP_KERNEL);
0675     if (!req)
0676         return -ENOMEM;
0677 
0678     crypto_init_wait(&wait);
0679     ahash_request_set_callback(req, CRYPTO_TFM_REQ_MAY_BACKLOG |
0680                    CRYPTO_TFM_REQ_MAY_SLEEP,
0681                    crypto_req_done, &wait);
0682 
0683     rc = ahash_wait(crypto_ahash_init(req), &wait);
0684     if (rc)
0685         goto out;
0686 
0687     sg_init_one(&sg, buf, len);
0688     ahash_request_set_crypt(req, &sg, NULL, len);
0689 
0690     ahash_rc = crypto_ahash_update(req);
0691 
0692     /* wait for the update request to complete */
0693     rc = ahash_wait(ahash_rc, &wait);
0694     if (!rc) {
0695         ahash_request_set_crypt(req, NULL, hash->digest, 0);
0696         rc = ahash_wait(crypto_ahash_final(req), &wait);
0697     }
0698 out:
0699     ahash_request_free(req);
0700     return rc;
0701 }
0702 
0703 static int calc_buffer_ahash(const void *buf, loff_t len,
0704                  struct ima_digest_data *hash)
0705 {
0706     struct crypto_ahash *tfm;
0707     int rc;
0708 
0709     tfm = ima_alloc_atfm(hash->algo);
0710     if (IS_ERR(tfm))
0711         return PTR_ERR(tfm);
0712 
0713     rc = calc_buffer_ahash_atfm(buf, len, hash, tfm);
0714 
0715     ima_free_atfm(tfm);
0716 
0717     return rc;
0718 }
0719 
0720 static int calc_buffer_shash_tfm(const void *buf, loff_t size,
0721                 struct ima_digest_data *hash,
0722                 struct crypto_shash *tfm)
0723 {
0724     SHASH_DESC_ON_STACK(shash, tfm);
0725     unsigned int len;
0726     int rc;
0727 
0728     shash->tfm = tfm;
0729 
0730     hash->length = crypto_shash_digestsize(tfm);
0731 
0732     rc = crypto_shash_init(shash);
0733     if (rc != 0)
0734         return rc;
0735 
0736     while (size) {
0737         len = size < PAGE_SIZE ? size : PAGE_SIZE;
0738         rc = crypto_shash_update(shash, buf, len);
0739         if (rc)
0740             break;
0741         buf += len;
0742         size -= len;
0743     }
0744 
0745     if (!rc)
0746         rc = crypto_shash_final(shash, hash->digest);
0747     return rc;
0748 }
0749 
0750 static int calc_buffer_shash(const void *buf, loff_t len,
0751                  struct ima_digest_data *hash)
0752 {
0753     struct crypto_shash *tfm;
0754     int rc;
0755 
0756     tfm = ima_alloc_tfm(hash->algo);
0757     if (IS_ERR(tfm))
0758         return PTR_ERR(tfm);
0759 
0760     rc = calc_buffer_shash_tfm(buf, len, hash, tfm);
0761 
0762     ima_free_tfm(tfm);
0763     return rc;
0764 }
0765 
0766 int ima_calc_buffer_hash(const void *buf, loff_t len,
0767              struct ima_digest_data *hash)
0768 {
0769     int rc;
0770 
0771     if (ima_ahash_minsize && len >= ima_ahash_minsize) {
0772         rc = calc_buffer_ahash(buf, len, hash);
0773         if (!rc)
0774             return 0;
0775     }
0776 
0777     return calc_buffer_shash(buf, len, hash);
0778 }
0779 
0780 static void ima_pcrread(u32 idx, struct tpm_digest *d)
0781 {
0782     if (!ima_tpm_chip)
0783         return;
0784 
0785     if (tpm_pcr_read(ima_tpm_chip, idx, d) != 0)
0786         pr_err("Error Communicating to TPM chip\n");
0787 }
0788 
0789 /*
0790  * The boot_aggregate is a cumulative hash over TPM registers 0 - 7.  With
0791  * TPM 1.2 the boot_aggregate was based on reading the SHA1 PCRs, but with
0792  * TPM 2.0 hash agility, TPM chips could support multiple TPM PCR banks,
0793  * allowing firmware to configure and enable different banks.
0794  *
0795  * Knowing which TPM bank is read to calculate the boot_aggregate digest
0796  * needs to be conveyed to a verifier.  For this reason, use the same
0797  * hash algorithm for reading the TPM PCRs as for calculating the boot
0798  * aggregate digest as stored in the measurement list.
0799  */
0800 static int ima_calc_boot_aggregate_tfm(char *digest, u16 alg_id,
0801                        struct crypto_shash *tfm)
0802 {
0803     struct tpm_digest d = { .alg_id = alg_id, .digest = {0} };
0804     int rc;
0805     u32 i;
0806     SHASH_DESC_ON_STACK(shash, tfm);
0807 
0808     shash->tfm = tfm;
0809 
0810     pr_devel("calculating the boot-aggregate based on TPM bank: %04x\n",
0811          d.alg_id);
0812 
0813     rc = crypto_shash_init(shash);
0814     if (rc != 0)
0815         return rc;
0816 
0817     /* cumulative digest over TPM registers 0-7 */
0818     for (i = TPM_PCR0; i < TPM_PCR8; i++) {
0819         ima_pcrread(i, &d);
0820         /* now accumulate with current aggregate */
0821         rc = crypto_shash_update(shash, d.digest,
0822                      crypto_shash_digestsize(tfm));
0823         if (rc != 0)
0824             return rc;
0825     }
0826     /*
0827      * Extend cumulative digest over TPM registers 8-9, which contain
0828      * measurement for the kernel command line (reg. 8) and image (reg. 9)
0829      * in a typical PCR allocation. Registers 8-9 are only included in
0830      * non-SHA1 boot_aggregate digests to avoid ambiguity.
0831      */
0832     if (alg_id != TPM_ALG_SHA1) {
0833         for (i = TPM_PCR8; i < TPM_PCR10; i++) {
0834             ima_pcrread(i, &d);
0835             rc = crypto_shash_update(shash, d.digest,
0836                         crypto_shash_digestsize(tfm));
0837         }
0838     }
0839     if (!rc)
0840         crypto_shash_final(shash, digest);
0841     return rc;
0842 }
0843 
0844 int ima_calc_boot_aggregate(struct ima_digest_data *hash)
0845 {
0846     struct crypto_shash *tfm;
0847     u16 crypto_id, alg_id;
0848     int rc, i, bank_idx = -1;
0849 
0850     for (i = 0; i < ima_tpm_chip->nr_allocated_banks; i++) {
0851         crypto_id = ima_tpm_chip->allocated_banks[i].crypto_id;
0852         if (crypto_id == hash->algo) {
0853             bank_idx = i;
0854             break;
0855         }
0856 
0857         if (crypto_id == HASH_ALGO_SHA256)
0858             bank_idx = i;
0859 
0860         if (bank_idx == -1 && crypto_id == HASH_ALGO_SHA1)
0861             bank_idx = i;
0862     }
0863 
0864     if (bank_idx == -1) {
0865         pr_err("No suitable TPM algorithm for boot aggregate\n");
0866         return 0;
0867     }
0868 
0869     hash->algo = ima_tpm_chip->allocated_banks[bank_idx].crypto_id;
0870 
0871     tfm = ima_alloc_tfm(hash->algo);
0872     if (IS_ERR(tfm))
0873         return PTR_ERR(tfm);
0874 
0875     hash->length = crypto_shash_digestsize(tfm);
0876     alg_id = ima_tpm_chip->allocated_banks[bank_idx].alg_id;
0877     rc = ima_calc_boot_aggregate_tfm(hash->digest, alg_id, tfm);
0878 
0879     ima_free_tfm(tfm);
0880 
0881     return rc;
0882 }