~ [ source navigation ] ~ [ diff markup ] ~ [ identifier search ] ~

TOMOYO Linux Cross Reference
Linux/security/integrity/ima/ima_crypto.c

Version: ~ [ linux-5.10-rc1 ] ~ [ linux-5.9.1 ] ~ [ linux-5.8.16 ] ~ [ linux-5.7.19 ] ~ [ linux-5.6.19 ] ~ [ linux-5.5.19 ] ~ [ linux-5.4.72 ] ~ [ linux-5.3.18 ] ~ [ linux-5.2.21 ] ~ [ linux-5.1.21 ] ~ [ linux-5.0.21 ] ~ [ linux-4.20.17 ] ~ [ linux-4.19.152 ] ~ [ linux-4.18.20 ] ~ [ linux-4.17.19 ] ~ [ linux-4.16.18 ] ~ [ linux-4.15.18 ] ~ [ linux-4.14.202 ] ~ [ linux-4.13.16 ] ~ [ linux-4.12.14 ] ~ [ linux-4.11.12 ] ~ [ linux-4.10.17 ] ~ [ linux-4.9.240 ] ~ [ linux-4.8.17 ] ~ [ linux-4.7.10 ] ~ [ linux-4.6.7 ] ~ [ linux-4.5.7 ] ~ [ linux-4.4.240 ] ~ [ linux-4.3.6 ] ~ [ linux-4.2.8 ] ~ [ linux-4.1.52 ] ~ [ linux-4.0.9 ] ~ [ linux-3.19.8 ] ~ [ linux-3.18.140 ] ~ [ linux-3.17.8 ] ~ [ linux-3.16.85 ] ~ [ linux-3.15.10 ] ~ [ linux-3.14.79 ] ~ [ linux-3.13.11 ] ~ [ linux-3.12.74 ] ~ [ linux-3.11.10 ] ~ [ linux-3.10.108 ] ~ [ linux-2.6.32.71 ] ~ [ linux-2.6.0 ] ~ [ linux-2.4.37.11 ] ~ [ unix-v6-master ] ~ [ ccs-tools-1.8.5 ] ~ [ policy-sample ] ~
Architecture: ~ [ i386 ] ~ [ alpha ] ~ [ m68k ] ~ [ mips ] ~ [ ppc ] ~ [ sparc ] ~ [ sparc64 ] ~

  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 
 16 #include <linux/kernel.h>
 17 #include <linux/file.h>
 18 #include <linux/crypto.h>
 19 #include <linux/scatterlist.h>
 20 #include <linux/err.h>
 21 #include <linux/slab.h>
 22 #include <crypto/hash.h>
 23 #include "ima.h"
 24 
 25 static struct crypto_shash *ima_shash_tfm;
 26 
 27 /**
 28  * ima_kernel_read - read file content
 29  *
 30  * This is a function for reading file content instead of kernel_read().
 31  * It does not perform locking checks to ensure it cannot be blocked.
 32  * It does not perform security checks because it is irrelevant for IMA.
 33  *
 34  */
 35 static int ima_kernel_read(struct file *file, loff_t offset,
 36                            char *addr, unsigned long count)
 37 {
 38         mm_segment_t old_fs;
 39         char __user *buf = addr;
 40         ssize_t ret;
 41 
 42         if (!(file->f_mode & FMODE_READ))
 43                 return -EBADF;
 44         if (!file->f_op->read && !file->f_op->aio_read)
 45                 return -EINVAL;
 46 
 47         old_fs = get_fs();
 48         set_fs(get_ds());
 49         if (file->f_op->read)
 50                 ret = file->f_op->read(file, buf, count, &offset);
 51         else
 52                 ret = do_sync_read(file, buf, count, &offset);
 53         set_fs(old_fs);
 54         return ret;
 55 }
 56 
 57 int ima_init_crypto(void)
 58 {
 59         long rc;
 60 
 61         ima_shash_tfm = crypto_alloc_shash(ima_hash, 0, 0);
 62         if (IS_ERR(ima_shash_tfm)) {
 63                 rc = PTR_ERR(ima_shash_tfm);
 64                 pr_err("Can not allocate %s (reason: %ld)\n", ima_hash, rc);
 65                 return rc;
 66         }
 67         return 0;
 68 }
 69 
 70 /*
 71  * Calculate the MD5/SHA1 file digest
 72  */
 73 int ima_calc_file_hash(struct file *file, char *digest)
 74 {
 75         loff_t i_size, offset = 0;
 76         char *rbuf;
 77         int rc, read = 0;
 78         struct {
 79                 struct shash_desc shash;
 80                 char ctx[crypto_shash_descsize(ima_shash_tfm)];
 81         } desc;
 82 
 83         desc.shash.tfm = ima_shash_tfm;
 84         desc.shash.flags = 0;
 85 
 86         rc = crypto_shash_init(&desc.shash);
 87         if (rc != 0)
 88                 return rc;
 89 
 90         rbuf = kzalloc(PAGE_SIZE, GFP_KERNEL);
 91         if (!rbuf) {
 92                 rc = -ENOMEM;
 93                 goto out;
 94         }
 95         if (!(file->f_mode & FMODE_READ)) {
 96                 file->f_mode |= FMODE_READ;
 97                 read = 1;
 98         }
 99         i_size = i_size_read(file_inode(file));
100         while (offset < i_size) {
101                 int rbuf_len;
102 
103                 rbuf_len = ima_kernel_read(file, offset, rbuf, PAGE_SIZE);
104                 if (rbuf_len < 0) {
105                         rc = rbuf_len;
106                         break;
107                 }
108                 if (rbuf_len == 0)
109                         break;
110                 offset += rbuf_len;
111 
112                 rc = crypto_shash_update(&desc.shash, rbuf, rbuf_len);
113                 if (rc)
114                         break;
115         }
116         kfree(rbuf);
117         if (!rc)
118                 rc = crypto_shash_final(&desc.shash, digest);
119         if (read)
120                 file->f_mode &= ~FMODE_READ;
121 out:
122         return rc;
123 }
124 
125 /*
126  * Calculate the hash of a given buffer
127  */
128 int ima_calc_buffer_hash(const void *data, int len, char *digest)
129 {
130         struct {
131                 struct shash_desc shash;
132                 char ctx[crypto_shash_descsize(ima_shash_tfm)];
133         } desc;
134 
135         desc.shash.tfm = ima_shash_tfm;
136         desc.shash.flags = 0;
137 
138         return crypto_shash_digest(&desc.shash, data, len, digest);
139 }
140 
141 static void __init ima_pcrread(int idx, u8 *pcr)
142 {
143         if (!ima_used_chip)
144                 return;
145 
146         if (tpm_pcr_read(TPM_ANY_NUM, idx, pcr) != 0)
147                 pr_err("IMA: Error Communicating to TPM chip\n");
148 }
149 
150 /*
151  * Calculate the boot aggregate hash
152  */
153 int __init ima_calc_boot_aggregate(char *digest)
154 {
155         u8 pcr_i[IMA_DIGEST_SIZE];
156         int rc, i;
157         struct {
158                 struct shash_desc shash;
159                 char ctx[crypto_shash_descsize(ima_shash_tfm)];
160         } desc;
161 
162         desc.shash.tfm = ima_shash_tfm;
163         desc.shash.flags = 0;
164 
165         rc = crypto_shash_init(&desc.shash);
166         if (rc != 0)
167                 return rc;
168 
169         /* cumulative sha1 over tpm registers 0-7 */
170         for (i = TPM_PCR0; i < TPM_PCR8; i++) {
171                 ima_pcrread(i, pcr_i);
172                 /* now accumulate with current aggregate */
173                 rc = crypto_shash_update(&desc.shash, pcr_i, IMA_DIGEST_SIZE);
174         }
175         if (!rc)
176                 crypto_shash_final(&desc.shash, digest);
177         return rc;
178 }
179 

~ [ source navigation ] ~ [ diff markup ] ~ [ identifier search ] ~

kernel.org | git.kernel.org | LWN.net | Project Home | Wiki (Japanese) | Wiki (English) | SVN repository | Mail admin

Linux® is a registered trademark of Linus Torvalds in the United States and other countries.
TOMOYO® is a registered trademark of NTT DATA CORPORATION.

osdn.jp