Thomas Gleixner | b886d83c | 2019-06-01 10:08:55 +0200 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0-only |
Mimi Zohar | 3323eec | 2009-02-04 09:06:58 -0500 | [diff] [blame] | 2 | /* |
| 3 | * Copyright (C) 2005,2006,2007,2008 IBM Corporation |
| 4 | * |
| 5 | * Authors: |
| 6 | * Mimi Zohar <zohar@us.ibm.com> |
| 7 | * Kylene Hall <kjhall@us.ibm.com> |
| 8 | * |
Mimi Zohar | 3323eec | 2009-02-04 09:06:58 -0500 | [diff] [blame] | 9 | * File: ima_crypto.c |
Dmitry Kasatkin | 2bb930a | 2014-03-04 18:04:20 +0200 | [diff] [blame] | 10 | * Calculates md5/sha1 file hash, template hash, boot-aggreate hash |
Mimi Zohar | 3323eec | 2009-02-04 09:06:58 -0500 | [diff] [blame] | 11 | */ |
| 12 | |
| 13 | #include <linux/kernel.h> |
Dmitry Kasatkin | 3bcced3 | 2014-02-26 17:05:20 +0200 | [diff] [blame] | 14 | #include <linux/moduleparam.h> |
| 15 | #include <linux/ratelimit.h> |
Mimi Zohar | 3323eec | 2009-02-04 09:06:58 -0500 | [diff] [blame] | 16 | #include <linux/file.h> |
| 17 | #include <linux/crypto.h> |
| 18 | #include <linux/scatterlist.h> |
| 19 | #include <linux/err.h> |
Tejun Heo | 5a0e3ad | 2010-03-24 17:04:11 +0900 | [diff] [blame] | 20 | #include <linux/slab.h> |
Dmitry Kasatkin | 76bb28f | 2012-06-08 10:42:30 +0300 | [diff] [blame] | 21 | #include <crypto/hash.h> |
Dmitry Kasatkin | 1525b06 | 2014-10-30 12:39:39 +0200 | [diff] [blame] | 22 | |
Mimi Zohar | 3323eec | 2009-02-04 09:06:58 -0500 | [diff] [blame] | 23 | #include "ima.h" |
| 24 | |
Dmitry Kasatkin | 3bcced3 | 2014-02-26 17:05:20 +0200 | [diff] [blame] | 25 | /* minimum file size for ahash use */ |
| 26 | static unsigned long ima_ahash_minsize; |
| 27 | module_param_named(ahash_minsize, ima_ahash_minsize, ulong, 0644); |
| 28 | MODULE_PARM_DESC(ahash_minsize, "Minimum file size for ahash use"); |
| 29 | |
Dmitry Kasatkin | 6edf7a8 | 2014-05-06 14:47:13 +0300 | [diff] [blame] | 30 | /* default is 0 - 1 page. */ |
| 31 | static int ima_maxorder; |
| 32 | static unsigned int ima_bufsize = PAGE_SIZE; |
| 33 | |
| 34 | static int param_set_bufsize(const char *val, const struct kernel_param *kp) |
| 35 | { |
| 36 | unsigned long long size; |
| 37 | int order; |
| 38 | |
| 39 | size = memparse(val, NULL); |
| 40 | order = get_order(size); |
| 41 | if (order >= MAX_ORDER) |
| 42 | return -EINVAL; |
| 43 | ima_maxorder = order; |
| 44 | ima_bufsize = PAGE_SIZE << order; |
| 45 | return 0; |
| 46 | } |
| 47 | |
Luis R. Rodriguez | 9c27847 | 2015-05-27 11:09:38 +0930 | [diff] [blame] | 48 | static const struct kernel_param_ops param_ops_bufsize = { |
Dmitry Kasatkin | 6edf7a8 | 2014-05-06 14:47:13 +0300 | [diff] [blame] | 49 | .set = param_set_bufsize, |
| 50 | .get = param_get_uint, |
| 51 | }; |
| 52 | #define param_check_bufsize(name, p) __param_check(name, p, unsigned int) |
| 53 | |
| 54 | module_param_named(ahash_bufsize, ima_bufsize, bufsize, 0644); |
| 55 | MODULE_PARM_DESC(ahash_bufsize, "Maximum ahash buffer size"); |
| 56 | |
Dmitry Kasatkin | 76bb28f | 2012-06-08 10:42:30 +0300 | [diff] [blame] | 57 | static struct crypto_shash *ima_shash_tfm; |
Dmitry Kasatkin | 3bcced3 | 2014-02-26 17:05:20 +0200 | [diff] [blame] | 58 | static struct crypto_ahash *ima_ahash_tfm; |
Mimi Zohar | 3323eec | 2009-02-04 09:06:58 -0500 | [diff] [blame] | 59 | |
Dmitry Kasatkin | e4a9c51 | 2014-09-03 10:19:58 +0300 | [diff] [blame] | 60 | int __init ima_init_crypto(void) |
Dmitry Kasatkin | 76bb28f | 2012-06-08 10:42:30 +0300 | [diff] [blame] | 61 | { |
| 62 | long rc; |
| 63 | |
Dmitry Kasatkin | c7c8bb2 | 2013-04-25 10:43:56 +0300 | [diff] [blame] | 64 | ima_shash_tfm = crypto_alloc_shash(hash_algo_name[ima_hash_algo], 0, 0); |
Dmitry Kasatkin | 76bb28f | 2012-06-08 10:42:30 +0300 | [diff] [blame] | 65 | if (IS_ERR(ima_shash_tfm)) { |
| 66 | rc = PTR_ERR(ima_shash_tfm); |
Dmitry Kasatkin | c7c8bb2 | 2013-04-25 10:43:56 +0300 | [diff] [blame] | 67 | pr_err("Can not allocate %s (reason: %ld)\n", |
| 68 | hash_algo_name[ima_hash_algo], rc); |
Mimi Zohar | 3323eec | 2009-02-04 09:06:58 -0500 | [diff] [blame] | 69 | return rc; |
| 70 | } |
Petr Vorel | ab60368 | 2018-03-23 14:41:08 +0100 | [diff] [blame] | 71 | pr_info("Allocated hash algorithm: %s\n", |
| 72 | hash_algo_name[ima_hash_algo]); |
Dmitry Kasatkin | 76bb28f | 2012-06-08 10:42:30 +0300 | [diff] [blame] | 73 | return 0; |
Mimi Zohar | 3323eec | 2009-02-04 09:06:58 -0500 | [diff] [blame] | 74 | } |
| 75 | |
Dmitry Kasatkin | 723326b | 2013-07-04 17:40:01 +0300 | [diff] [blame] | 76 | static struct crypto_shash *ima_alloc_tfm(enum hash_algo algo) |
| 77 | { |
| 78 | struct crypto_shash *tfm = ima_shash_tfm; |
| 79 | int rc; |
| 80 | |
Dmitry Kasatkin | 23c19e2 | 2014-08-15 13:28:52 +0300 | [diff] [blame] | 81 | if (algo < 0 || algo >= HASH_ALGO__LAST) |
| 82 | algo = ima_hash_algo; |
| 83 | |
| 84 | if (algo != ima_hash_algo) { |
Dmitry Kasatkin | 723326b | 2013-07-04 17:40:01 +0300 | [diff] [blame] | 85 | tfm = crypto_alloc_shash(hash_algo_name[algo], 0, 0); |
| 86 | if (IS_ERR(tfm)) { |
| 87 | rc = PTR_ERR(tfm); |
| 88 | pr_err("Can not allocate %s (reason: %d)\n", |
| 89 | hash_algo_name[algo], rc); |
| 90 | } |
| 91 | } |
| 92 | return tfm; |
| 93 | } |
| 94 | |
| 95 | static void ima_free_tfm(struct crypto_shash *tfm) |
| 96 | { |
| 97 | if (tfm != ima_shash_tfm) |
| 98 | crypto_free_shash(tfm); |
| 99 | } |
| 100 | |
Dmitry Kasatkin | 6edf7a8 | 2014-05-06 14:47:13 +0300 | [diff] [blame] | 101 | /** |
| 102 | * ima_alloc_pages() - Allocate contiguous pages. |
| 103 | * @max_size: Maximum amount of memory to allocate. |
| 104 | * @allocated_size: Returned size of actual allocation. |
| 105 | * @last_warn: Should the min_size allocation warn or not. |
| 106 | * |
| 107 | * Tries to do opportunistic allocation for memory first trying to allocate |
| 108 | * max_size amount of memory and then splitting that until zero order is |
| 109 | * reached. Allocation is tried without generating allocation warnings unless |
| 110 | * last_warn is set. Last_warn set affects only last allocation of zero order. |
| 111 | * |
| 112 | * By default, ima_maxorder is 0 and it is equivalent to kmalloc(GFP_KERNEL) |
| 113 | * |
| 114 | * Return pointer to allocated memory, or NULL on failure. |
| 115 | */ |
| 116 | static void *ima_alloc_pages(loff_t max_size, size_t *allocated_size, |
| 117 | int last_warn) |
| 118 | { |
| 119 | void *ptr; |
| 120 | int order = ima_maxorder; |
Mel Gorman | 71baba4 | 2015-11-06 16:28:28 -0800 | [diff] [blame] | 121 | gfp_t gfp_mask = __GFP_RECLAIM | __GFP_NOWARN | __GFP_NORETRY; |
Dmitry Kasatkin | 6edf7a8 | 2014-05-06 14:47:13 +0300 | [diff] [blame] | 122 | |
| 123 | if (order) |
| 124 | order = min(get_order(max_size), order); |
| 125 | |
| 126 | for (; order; order--) { |
| 127 | ptr = (void *)__get_free_pages(gfp_mask, order); |
| 128 | if (ptr) { |
| 129 | *allocated_size = PAGE_SIZE << order; |
| 130 | return ptr; |
| 131 | } |
| 132 | } |
| 133 | |
| 134 | /* order is zero - one page */ |
| 135 | |
| 136 | gfp_mask = GFP_KERNEL; |
| 137 | |
| 138 | if (!last_warn) |
| 139 | gfp_mask |= __GFP_NOWARN; |
| 140 | |
| 141 | ptr = (void *)__get_free_pages(gfp_mask, 0); |
| 142 | if (ptr) { |
| 143 | *allocated_size = PAGE_SIZE; |
| 144 | return ptr; |
| 145 | } |
| 146 | |
| 147 | *allocated_size = 0; |
| 148 | return NULL; |
| 149 | } |
| 150 | |
| 151 | /** |
| 152 | * ima_free_pages() - Free pages allocated by ima_alloc_pages(). |
| 153 | * @ptr: Pointer to allocated pages. |
| 154 | * @size: Size of allocated buffer. |
| 155 | */ |
| 156 | static void ima_free_pages(void *ptr, size_t size) |
| 157 | { |
| 158 | if (!ptr) |
| 159 | return; |
| 160 | free_pages((unsigned long)ptr, get_order(size)); |
| 161 | } |
| 162 | |
Dmitry Kasatkin | 3bcced3 | 2014-02-26 17:05:20 +0200 | [diff] [blame] | 163 | static struct crypto_ahash *ima_alloc_atfm(enum hash_algo algo) |
| 164 | { |
| 165 | struct crypto_ahash *tfm = ima_ahash_tfm; |
| 166 | int rc; |
| 167 | |
Mimi Zohar | 9a8d289 | 2014-07-28 07:59:49 -0400 | [diff] [blame] | 168 | if (algo < 0 || algo >= HASH_ALGO__LAST) |
| 169 | algo = ima_hash_algo; |
| 170 | |
| 171 | if (algo != ima_hash_algo || !tfm) { |
Dmitry Kasatkin | 3bcced3 | 2014-02-26 17:05:20 +0200 | [diff] [blame] | 172 | tfm = crypto_alloc_ahash(hash_algo_name[algo], 0, 0); |
| 173 | if (!IS_ERR(tfm)) { |
| 174 | if (algo == ima_hash_algo) |
| 175 | ima_ahash_tfm = tfm; |
| 176 | } else { |
| 177 | rc = PTR_ERR(tfm); |
| 178 | pr_err("Can not allocate %s (reason: %d)\n", |
| 179 | hash_algo_name[algo], rc); |
| 180 | } |
| 181 | } |
| 182 | return tfm; |
| 183 | } |
| 184 | |
| 185 | static void ima_free_atfm(struct crypto_ahash *tfm) |
| 186 | { |
| 187 | if (tfm != ima_ahash_tfm) |
| 188 | crypto_free_ahash(tfm); |
| 189 | } |
| 190 | |
Gilad Ben-Yossef | 46f1414 | 2017-10-18 08:00:47 +0100 | [diff] [blame] | 191 | static inline int ahash_wait(int err, struct crypto_wait *wait) |
Dmitry Kasatkin | 3bcced3 | 2014-02-26 17:05:20 +0200 | [diff] [blame] | 192 | { |
Dmitry Kasatkin | 3bcced3 | 2014-02-26 17:05:20 +0200 | [diff] [blame] | 193 | |
Gilad Ben-Yossef | 46f1414 | 2017-10-18 08:00:47 +0100 | [diff] [blame] | 194 | err = crypto_wait_req(err, wait); |
Dmitry Kasatkin | 3bcced3 | 2014-02-26 17:05:20 +0200 | [diff] [blame] | 195 | |
Gilad Ben-Yossef | 46f1414 | 2017-10-18 08:00:47 +0100 | [diff] [blame] | 196 | if (err) |
Dmitry Kasatkin | 3bcced3 | 2014-02-26 17:05:20 +0200 | [diff] [blame] | 197 | pr_crit_ratelimited("ahash calculation failed: err: %d\n", err); |
Dmitry Kasatkin | 3bcced3 | 2014-02-26 17:05:20 +0200 | [diff] [blame] | 198 | |
| 199 | return err; |
| 200 | } |
| 201 | |
| 202 | static int ima_calc_file_hash_atfm(struct file *file, |
| 203 | struct ima_digest_data *hash, |
| 204 | struct crypto_ahash *tfm) |
| 205 | { |
| 206 | loff_t i_size, offset; |
Dmitry Kasatkin | 32c2e67 | 2014-05-06 14:54:27 +0300 | [diff] [blame] | 207 | char *rbuf[2] = { NULL, }; |
Goldwyn Rodrigues | a408e4a | 2018-10-09 10:12:33 -0500 | [diff] [blame] | 208 | int rc, rbuf_len, active = 0, ahash_rc = 0; |
Dmitry Kasatkin | 3bcced3 | 2014-02-26 17:05:20 +0200 | [diff] [blame] | 209 | struct ahash_request *req; |
| 210 | struct scatterlist sg[1]; |
Gilad Ben-Yossef | 46f1414 | 2017-10-18 08:00:47 +0100 | [diff] [blame] | 211 | struct crypto_wait wait; |
Dmitry Kasatkin | 32c2e67 | 2014-05-06 14:54:27 +0300 | [diff] [blame] | 212 | size_t rbuf_size[2]; |
Dmitry Kasatkin | 3bcced3 | 2014-02-26 17:05:20 +0200 | [diff] [blame] | 213 | |
| 214 | hash->length = crypto_ahash_digestsize(tfm); |
| 215 | |
| 216 | req = ahash_request_alloc(tfm, GFP_KERNEL); |
| 217 | if (!req) |
| 218 | return -ENOMEM; |
| 219 | |
Gilad Ben-Yossef | 46f1414 | 2017-10-18 08:00:47 +0100 | [diff] [blame] | 220 | crypto_init_wait(&wait); |
Dmitry Kasatkin | 3bcced3 | 2014-02-26 17:05:20 +0200 | [diff] [blame] | 221 | ahash_request_set_callback(req, CRYPTO_TFM_REQ_MAY_BACKLOG | |
| 222 | CRYPTO_TFM_REQ_MAY_SLEEP, |
Gilad Ben-Yossef | 46f1414 | 2017-10-18 08:00:47 +0100 | [diff] [blame] | 223 | crypto_req_done, &wait); |
Dmitry Kasatkin | 3bcced3 | 2014-02-26 17:05:20 +0200 | [diff] [blame] | 224 | |
Gilad Ben-Yossef | 46f1414 | 2017-10-18 08:00:47 +0100 | [diff] [blame] | 225 | rc = ahash_wait(crypto_ahash_init(req), &wait); |
Dmitry Kasatkin | 3bcced3 | 2014-02-26 17:05:20 +0200 | [diff] [blame] | 226 | if (rc) |
| 227 | goto out1; |
| 228 | |
| 229 | i_size = i_size_read(file_inode(file)); |
| 230 | |
| 231 | if (i_size == 0) |
| 232 | goto out2; |
| 233 | |
Dmitry Kasatkin | 6edf7a8 | 2014-05-06 14:47:13 +0300 | [diff] [blame] | 234 | /* |
| 235 | * Try to allocate maximum size of memory. |
| 236 | * Fail if even a single page cannot be allocated. |
| 237 | */ |
Dmitry Kasatkin | 32c2e67 | 2014-05-06 14:54:27 +0300 | [diff] [blame] | 238 | rbuf[0] = ima_alloc_pages(i_size, &rbuf_size[0], 1); |
| 239 | if (!rbuf[0]) { |
Dmitry Kasatkin | 3bcced3 | 2014-02-26 17:05:20 +0200 | [diff] [blame] | 240 | rc = -ENOMEM; |
| 241 | goto out1; |
| 242 | } |
| 243 | |
Dmitry Kasatkin | 32c2e67 | 2014-05-06 14:54:27 +0300 | [diff] [blame] | 244 | /* Only allocate one buffer if that is enough. */ |
| 245 | if (i_size > rbuf_size[0]) { |
| 246 | /* |
| 247 | * Try to allocate secondary buffer. If that fails fallback to |
| 248 | * using single buffering. Use previous memory allocation size |
| 249 | * as baseline for possible allocation size. |
| 250 | */ |
| 251 | rbuf[1] = ima_alloc_pages(i_size - rbuf_size[0], |
| 252 | &rbuf_size[1], 0); |
| 253 | } |
| 254 | |
Dmitry Kasatkin | 3bcced3 | 2014-02-26 17:05:20 +0200 | [diff] [blame] | 255 | for (offset = 0; offset < i_size; offset += rbuf_len) { |
Dmitry Kasatkin | 32c2e67 | 2014-05-06 14:54:27 +0300 | [diff] [blame] | 256 | if (!rbuf[1] && offset) { |
| 257 | /* Not using two buffers, and it is not the first |
| 258 | * read/request, wait for the completion of the |
| 259 | * previous ahash_update() request. |
| 260 | */ |
Gilad Ben-Yossef | 46f1414 | 2017-10-18 08:00:47 +0100 | [diff] [blame] | 261 | rc = ahash_wait(ahash_rc, &wait); |
Dmitry Kasatkin | 32c2e67 | 2014-05-06 14:54:27 +0300 | [diff] [blame] | 262 | if (rc) |
| 263 | goto out3; |
Dmitry Kasatkin | 3bcced3 | 2014-02-26 17:05:20 +0200 | [diff] [blame] | 264 | } |
Dmitry Kasatkin | 32c2e67 | 2014-05-06 14:54:27 +0300 | [diff] [blame] | 265 | /* read buffer */ |
| 266 | rbuf_len = min_t(loff_t, i_size - offset, rbuf_size[active]); |
Dmitry Kasatkin | e3c4abb | 2014-11-05 17:01:12 +0200 | [diff] [blame] | 267 | rc = integrity_kernel_read(file, offset, rbuf[active], |
| 268 | rbuf_len); |
Sascha Hauer | f5e1040 | 2019-07-02 10:00:40 +0200 | [diff] [blame] | 269 | if (rc != rbuf_len) { |
| 270 | if (rc >= 0) |
| 271 | rc = -EINVAL; |
Sascha Hauer | 4ece312 | 2019-07-02 10:00:41 +0200 | [diff] [blame] | 272 | /* |
| 273 | * Forward current rc, do not overwrite with return value |
| 274 | * from ahash_wait() |
| 275 | */ |
| 276 | ahash_wait(ahash_rc, &wait); |
Dmitry Kasatkin | 32c2e67 | 2014-05-06 14:54:27 +0300 | [diff] [blame] | 277 | goto out3; |
Sascha Hauer | f5e1040 | 2019-07-02 10:00:40 +0200 | [diff] [blame] | 278 | } |
Dmitry Kasatkin | 3bcced3 | 2014-02-26 17:05:20 +0200 | [diff] [blame] | 279 | |
Dmitry Kasatkin | 32c2e67 | 2014-05-06 14:54:27 +0300 | [diff] [blame] | 280 | if (rbuf[1] && offset) { |
| 281 | /* Using two buffers, and it is not the first |
| 282 | * read/request, wait for the completion of the |
| 283 | * previous ahash_update() request. |
| 284 | */ |
Gilad Ben-Yossef | 46f1414 | 2017-10-18 08:00:47 +0100 | [diff] [blame] | 285 | rc = ahash_wait(ahash_rc, &wait); |
Dmitry Kasatkin | 32c2e67 | 2014-05-06 14:54:27 +0300 | [diff] [blame] | 286 | if (rc) |
| 287 | goto out3; |
| 288 | } |
| 289 | |
| 290 | sg_init_one(&sg[0], rbuf[active], rbuf_len); |
Dmitry Kasatkin | 3bcced3 | 2014-02-26 17:05:20 +0200 | [diff] [blame] | 291 | ahash_request_set_crypt(req, sg, NULL, rbuf_len); |
| 292 | |
Dmitry Kasatkin | 32c2e67 | 2014-05-06 14:54:27 +0300 | [diff] [blame] | 293 | ahash_rc = crypto_ahash_update(req); |
| 294 | |
| 295 | if (rbuf[1]) |
| 296 | active = !active; /* swap buffers, if we use two */ |
Dmitry Kasatkin | 3bcced3 | 2014-02-26 17:05:20 +0200 | [diff] [blame] | 297 | } |
Dmitry Kasatkin | 32c2e67 | 2014-05-06 14:54:27 +0300 | [diff] [blame] | 298 | /* wait for the last update request to complete */ |
Gilad Ben-Yossef | 46f1414 | 2017-10-18 08:00:47 +0100 | [diff] [blame] | 299 | rc = ahash_wait(ahash_rc, &wait); |
Dmitry Kasatkin | 32c2e67 | 2014-05-06 14:54:27 +0300 | [diff] [blame] | 300 | out3: |
Dmitry Kasatkin | 32c2e67 | 2014-05-06 14:54:27 +0300 | [diff] [blame] | 301 | ima_free_pages(rbuf[0], rbuf_size[0]); |
| 302 | ima_free_pages(rbuf[1], rbuf_size[1]); |
Dmitry Kasatkin | 3bcced3 | 2014-02-26 17:05:20 +0200 | [diff] [blame] | 303 | out2: |
| 304 | if (!rc) { |
| 305 | ahash_request_set_crypt(req, NULL, hash->digest, 0); |
Gilad Ben-Yossef | 46f1414 | 2017-10-18 08:00:47 +0100 | [diff] [blame] | 306 | rc = ahash_wait(crypto_ahash_final(req), &wait); |
Dmitry Kasatkin | 3bcced3 | 2014-02-26 17:05:20 +0200 | [diff] [blame] | 307 | } |
| 308 | out1: |
| 309 | ahash_request_free(req); |
| 310 | return rc; |
| 311 | } |
| 312 | |
| 313 | static int ima_calc_file_ahash(struct file *file, struct ima_digest_data *hash) |
| 314 | { |
| 315 | struct crypto_ahash *tfm; |
| 316 | int rc; |
| 317 | |
| 318 | tfm = ima_alloc_atfm(hash->algo); |
| 319 | if (IS_ERR(tfm)) |
| 320 | return PTR_ERR(tfm); |
| 321 | |
| 322 | rc = ima_calc_file_hash_atfm(file, hash, tfm); |
| 323 | |
| 324 | ima_free_atfm(tfm); |
| 325 | |
| 326 | return rc; |
| 327 | } |
| 328 | |
Dmitry Kasatkin | c7c8bb2 | 2013-04-25 10:43:56 +0300 | [diff] [blame] | 329 | static int ima_calc_file_hash_tfm(struct file *file, |
| 330 | struct ima_digest_data *hash, |
| 331 | struct crypto_shash *tfm) |
Mimi Zohar | 3323eec | 2009-02-04 09:06:58 -0500 | [diff] [blame] | 332 | { |
Mimi Zohar | 16bfa38 | 2009-08-21 14:32:49 -0400 | [diff] [blame] | 333 | loff_t i_size, offset = 0; |
Mimi Zohar | 3323eec | 2009-02-04 09:06:58 -0500 | [diff] [blame] | 334 | char *rbuf; |
Goldwyn Rodrigues | a408e4a | 2018-10-09 10:12:33 -0500 | [diff] [blame] | 335 | int rc; |
Behan Webster | 357aabe | 2014-04-04 18:18:00 -0300 | [diff] [blame] | 336 | SHASH_DESC_ON_STACK(shash, tfm); |
Mimi Zohar | 3323eec | 2009-02-04 09:06:58 -0500 | [diff] [blame] | 337 | |
Behan Webster | 357aabe | 2014-04-04 18:18:00 -0300 | [diff] [blame] | 338 | shash->tfm = tfm; |
Dmitry Kasatkin | 76bb28f | 2012-06-08 10:42:30 +0300 | [diff] [blame] | 339 | |
Dmitry Kasatkin | 723326b | 2013-07-04 17:40:01 +0300 | [diff] [blame] | 340 | hash->length = crypto_shash_digestsize(tfm); |
| 341 | |
Behan Webster | 357aabe | 2014-04-04 18:18:00 -0300 | [diff] [blame] | 342 | rc = crypto_shash_init(shash); |
Mimi Zohar | 3323eec | 2009-02-04 09:06:58 -0500 | [diff] [blame] | 343 | if (rc != 0) |
| 344 | return rc; |
| 345 | |
Dmitry Kasatkin | 1d91ac6 | 2014-02-27 20:16:47 +0200 | [diff] [blame] | 346 | i_size = i_size_read(file_inode(file)); |
| 347 | |
| 348 | if (i_size == 0) |
Mimi Zohar | 3323eec | 2009-02-04 09:06:58 -0500 | [diff] [blame] | 349 | goto out; |
Dmitry Kasatkin | 1d91ac6 | 2014-02-27 20:16:47 +0200 | [diff] [blame] | 350 | |
| 351 | rbuf = kzalloc(PAGE_SIZE, GFP_KERNEL); |
| 352 | if (!rbuf) |
| 353 | return -ENOMEM; |
| 354 | |
Mimi Zohar | 3323eec | 2009-02-04 09:06:58 -0500 | [diff] [blame] | 355 | while (offset < i_size) { |
| 356 | int rbuf_len; |
| 357 | |
Dmitry Kasatkin | e3c4abb | 2014-11-05 17:01:12 +0200 | [diff] [blame] | 358 | rbuf_len = integrity_kernel_read(file, offset, rbuf, PAGE_SIZE); |
Mimi Zohar | 3323eec | 2009-02-04 09:06:58 -0500 | [diff] [blame] | 359 | if (rbuf_len < 0) { |
| 360 | rc = rbuf_len; |
| 361 | break; |
| 362 | } |
Patrick Callaghan | 96c9e1d | 2019-11-11 14:23:48 -0500 | [diff] [blame] | 363 | if (rbuf_len == 0) { /* unexpected EOF */ |
| 364 | rc = -EINVAL; |
Mimi Zohar | 16bfa38 | 2009-08-21 14:32:49 -0400 | [diff] [blame] | 365 | break; |
Patrick Callaghan | 96c9e1d | 2019-11-11 14:23:48 -0500 | [diff] [blame] | 366 | } |
Mimi Zohar | 3323eec | 2009-02-04 09:06:58 -0500 | [diff] [blame] | 367 | offset += rbuf_len; |
Mimi Zohar | 3323eec | 2009-02-04 09:06:58 -0500 | [diff] [blame] | 368 | |
Behan Webster | 357aabe | 2014-04-04 18:18:00 -0300 | [diff] [blame] | 369 | rc = crypto_shash_update(shash, rbuf, rbuf_len); |
Mimi Zohar | 3323eec | 2009-02-04 09:06:58 -0500 | [diff] [blame] | 370 | if (rc) |
| 371 | break; |
| 372 | } |
Dmitry Kasatkin | 1d91ac6 | 2014-02-27 20:16:47 +0200 | [diff] [blame] | 373 | kfree(rbuf); |
Mimi Zohar | 3323eec | 2009-02-04 09:06:58 -0500 | [diff] [blame] | 374 | out: |
Dmitry Kasatkin | 1d91ac6 | 2014-02-27 20:16:47 +0200 | [diff] [blame] | 375 | if (!rc) |
Behan Webster | 357aabe | 2014-04-04 18:18:00 -0300 | [diff] [blame] | 376 | rc = crypto_shash_final(shash, hash->digest); |
Mimi Zohar | 3323eec | 2009-02-04 09:06:58 -0500 | [diff] [blame] | 377 | return rc; |
| 378 | } |
| 379 | |
Dmitry Kasatkin | 3bcced3 | 2014-02-26 17:05:20 +0200 | [diff] [blame] | 380 | static int ima_calc_file_shash(struct file *file, struct ima_digest_data *hash) |
Dmitry Kasatkin | c7c8bb2 | 2013-04-25 10:43:56 +0300 | [diff] [blame] | 381 | { |
Dmitry Kasatkin | 723326b | 2013-07-04 17:40:01 +0300 | [diff] [blame] | 382 | struct crypto_shash *tfm; |
Dmitry Kasatkin | c7c8bb2 | 2013-04-25 10:43:56 +0300 | [diff] [blame] | 383 | int rc; |
| 384 | |
Dmitry Kasatkin | 723326b | 2013-07-04 17:40:01 +0300 | [diff] [blame] | 385 | tfm = ima_alloc_tfm(hash->algo); |
| 386 | if (IS_ERR(tfm)) |
| 387 | return PTR_ERR(tfm); |
Dmitry Kasatkin | c7c8bb2 | 2013-04-25 10:43:56 +0300 | [diff] [blame] | 388 | |
| 389 | rc = ima_calc_file_hash_tfm(file, hash, tfm); |
| 390 | |
Dmitry Kasatkin | 723326b | 2013-07-04 17:40:01 +0300 | [diff] [blame] | 391 | ima_free_tfm(tfm); |
Dmitry Kasatkin | c7c8bb2 | 2013-04-25 10:43:56 +0300 | [diff] [blame] | 392 | |
| 393 | return rc; |
| 394 | } |
| 395 | |
Mimi Zohar | 3323eec | 2009-02-04 09:06:58 -0500 | [diff] [blame] | 396 | /* |
Dmitry Kasatkin | 3bcced3 | 2014-02-26 17:05:20 +0200 | [diff] [blame] | 397 | * ima_calc_file_hash - calculate file hash |
| 398 | * |
| 399 | * Asynchronous hash (ahash) allows using HW acceleration for calculating |
| 400 | * a hash. ahash performance varies for different data sizes on different |
| 401 | * crypto accelerators. shash performance might be better for smaller files. |
| 402 | * The 'ima.ahash_minsize' module parameter allows specifying the best |
| 403 | * minimum file size for using ahash on the system. |
| 404 | * |
| 405 | * If the ima.ahash_minsize parameter is not specified, this function uses |
| 406 | * shash for the hash calculation. If ahash fails, it falls back to using |
| 407 | * shash. |
| 408 | */ |
| 409 | int ima_calc_file_hash(struct file *file, struct ima_digest_data *hash) |
| 410 | { |
| 411 | loff_t i_size; |
| 412 | int rc; |
Goldwyn Rodrigues | a408e4a | 2018-10-09 10:12:33 -0500 | [diff] [blame] | 413 | struct file *f = file; |
| 414 | bool new_file_instance = false, modified_flags = false; |
Dmitry Kasatkin | 3bcced3 | 2014-02-26 17:05:20 +0200 | [diff] [blame] | 415 | |
Mimi Zohar | f3cc6b2 | 2017-06-17 23:56:23 -0400 | [diff] [blame] | 416 | /* |
| 417 | * For consistency, fail file's opened with the O_DIRECT flag on |
| 418 | * filesystems mounted with/without DAX option. |
| 419 | */ |
| 420 | if (file->f_flags & O_DIRECT) { |
| 421 | hash->length = hash_digest_size[ima_hash_algo]; |
| 422 | hash->algo = ima_hash_algo; |
| 423 | return -EINVAL; |
| 424 | } |
| 425 | |
Goldwyn Rodrigues | a408e4a | 2018-10-09 10:12:33 -0500 | [diff] [blame] | 426 | /* Open a new file instance in O_RDONLY if we cannot read */ |
| 427 | if (!(file->f_mode & FMODE_READ)) { |
| 428 | int flags = file->f_flags & ~(O_WRONLY | O_APPEND | |
| 429 | O_TRUNC | O_CREAT | O_NOCTTY | O_EXCL); |
| 430 | flags |= O_RDONLY; |
| 431 | f = dentry_open(&file->f_path, flags, file->f_cred); |
| 432 | if (IS_ERR(f)) { |
| 433 | /* |
| 434 | * Cannot open the file again, lets modify f_flags |
| 435 | * of original and continue |
| 436 | */ |
| 437 | pr_info_ratelimited("Unable to reopen file for reading.\n"); |
| 438 | f = file; |
| 439 | f->f_flags |= FMODE_READ; |
| 440 | modified_flags = true; |
| 441 | } else { |
| 442 | new_file_instance = true; |
| 443 | } |
Dmitry Kasatkin | 3bcced3 | 2014-02-26 17:05:20 +0200 | [diff] [blame] | 444 | } |
| 445 | |
Goldwyn Rodrigues | a408e4a | 2018-10-09 10:12:33 -0500 | [diff] [blame] | 446 | i_size = i_size_read(file_inode(f)); |
| 447 | |
| 448 | if (ima_ahash_minsize && i_size >= ima_ahash_minsize) { |
| 449 | rc = ima_calc_file_ahash(f, hash); |
| 450 | if (!rc) |
| 451 | goto out; |
| 452 | } |
| 453 | |
| 454 | rc = ima_calc_file_shash(f, hash); |
| 455 | out: |
| 456 | if (new_file_instance) |
| 457 | fput(f); |
| 458 | else if (modified_flags) |
| 459 | f->f_flags &= ~FMODE_READ; |
| 460 | return rc; |
Dmitry Kasatkin | 3bcced3 | 2014-02-26 17:05:20 +0200 | [diff] [blame] | 461 | } |
| 462 | |
| 463 | /* |
Roberto Sassu | a71dc65 | 2013-06-07 12:16:33 +0200 | [diff] [blame] | 464 | * Calculate the hash of template data |
Mimi Zohar | 3323eec | 2009-02-04 09:06:58 -0500 | [diff] [blame] | 465 | */ |
Roberto Sassu | a71dc65 | 2013-06-07 12:16:33 +0200 | [diff] [blame] | 466 | static int ima_calc_field_array_hash_tfm(struct ima_field_data *field_data, |
Roberto Sassu | b6f8f16 | 2013-11-08 19:21:39 +0100 | [diff] [blame] | 467 | struct ima_template_desc *td, |
Roberto Sassu | a71dc65 | 2013-06-07 12:16:33 +0200 | [diff] [blame] | 468 | int num_fields, |
| 469 | struct ima_digest_data *hash, |
| 470 | struct crypto_shash *tfm) |
Mimi Zohar | 3323eec | 2009-02-04 09:06:58 -0500 | [diff] [blame] | 471 | { |
Behan Webster | 357aabe | 2014-04-04 18:18:00 -0300 | [diff] [blame] | 472 | SHASH_DESC_ON_STACK(shash, tfm); |
Roberto Sassu | a71dc65 | 2013-06-07 12:16:33 +0200 | [diff] [blame] | 473 | int rc, i; |
Mimi Zohar | 3323eec | 2009-02-04 09:06:58 -0500 | [diff] [blame] | 474 | |
Behan Webster | 357aabe | 2014-04-04 18:18:00 -0300 | [diff] [blame] | 475 | shash->tfm = tfm; |
Mimi Zohar | 3323eec | 2009-02-04 09:06:58 -0500 | [diff] [blame] | 476 | |
Dmitry Kasatkin | ea59399 | 2013-06-07 12:16:24 +0200 | [diff] [blame] | 477 | hash->length = crypto_shash_digestsize(tfm); |
Dmitry Kasatkin | c7c8bb2 | 2013-04-25 10:43:56 +0300 | [diff] [blame] | 478 | |
Behan Webster | 357aabe | 2014-04-04 18:18:00 -0300 | [diff] [blame] | 479 | rc = crypto_shash_init(shash); |
Roberto Sassu | a71dc65 | 2013-06-07 12:16:33 +0200 | [diff] [blame] | 480 | if (rc != 0) |
| 481 | return rc; |
| 482 | |
| 483 | for (i = 0; i < num_fields; i++) { |
Roberto Sassu | e3b64c2 | 2014-02-03 13:56:05 +0100 | [diff] [blame] | 484 | u8 buffer[IMA_EVENT_NAME_LEN_MAX + 1] = { 0 }; |
| 485 | u8 *data_to_hash = field_data[i].data; |
| 486 | u32 datalen = field_data[i].len; |
Andreas Steffen | 98e1d55d | 2016-12-19 16:23:00 -0800 | [diff] [blame] | 487 | u32 datalen_to_hash = |
| 488 | !ima_canonical_fmt ? datalen : cpu_to_le32(datalen); |
Roberto Sassu | e3b64c2 | 2014-02-03 13:56:05 +0100 | [diff] [blame] | 489 | |
Roberto Sassu | b6f8f16 | 2013-11-08 19:21:39 +0100 | [diff] [blame] | 490 | if (strcmp(td->name, IMA_TEMPLATE_IMA_NAME) != 0) { |
Behan Webster | 357aabe | 2014-04-04 18:18:00 -0300 | [diff] [blame] | 491 | rc = crypto_shash_update(shash, |
Andreas Steffen | 98e1d55d | 2016-12-19 16:23:00 -0800 | [diff] [blame] | 492 | (const u8 *) &datalen_to_hash, |
| 493 | sizeof(datalen_to_hash)); |
Roberto Sassu | b6f8f16 | 2013-11-08 19:21:39 +0100 | [diff] [blame] | 494 | if (rc) |
| 495 | break; |
Roberto Sassu | e3b64c2 | 2014-02-03 13:56:05 +0100 | [diff] [blame] | 496 | } else if (strcmp(td->fields[i]->field_id, "n") == 0) { |
| 497 | memcpy(buffer, data_to_hash, datalen); |
| 498 | data_to_hash = buffer; |
| 499 | datalen = IMA_EVENT_NAME_LEN_MAX + 1; |
Roberto Sassu | b6f8f16 | 2013-11-08 19:21:39 +0100 | [diff] [blame] | 500 | } |
Behan Webster | 357aabe | 2014-04-04 18:18:00 -0300 | [diff] [blame] | 501 | rc = crypto_shash_update(shash, data_to_hash, datalen); |
Roberto Sassu | a71dc65 | 2013-06-07 12:16:33 +0200 | [diff] [blame] | 502 | if (rc) |
| 503 | break; |
| 504 | } |
| 505 | |
| 506 | if (!rc) |
Behan Webster | 357aabe | 2014-04-04 18:18:00 -0300 | [diff] [blame] | 507 | rc = crypto_shash_final(shash, hash->digest); |
Roberto Sassu | a71dc65 | 2013-06-07 12:16:33 +0200 | [diff] [blame] | 508 | |
| 509 | return rc; |
Mimi Zohar | 3323eec | 2009-02-04 09:06:58 -0500 | [diff] [blame] | 510 | } |
| 511 | |
Roberto Sassu | b6f8f16 | 2013-11-08 19:21:39 +0100 | [diff] [blame] | 512 | int ima_calc_field_array_hash(struct ima_field_data *field_data, |
| 513 | struct ima_template_desc *desc, int num_fields, |
Roberto Sassu | a71dc65 | 2013-06-07 12:16:33 +0200 | [diff] [blame] | 514 | struct ima_digest_data *hash) |
Dmitry Kasatkin | ea59399 | 2013-06-07 12:16:24 +0200 | [diff] [blame] | 515 | { |
| 516 | struct crypto_shash *tfm; |
| 517 | int rc; |
| 518 | |
| 519 | tfm = ima_alloc_tfm(hash->algo); |
| 520 | if (IS_ERR(tfm)) |
| 521 | return PTR_ERR(tfm); |
| 522 | |
Roberto Sassu | b6f8f16 | 2013-11-08 19:21:39 +0100 | [diff] [blame] | 523 | rc = ima_calc_field_array_hash_tfm(field_data, desc, num_fields, |
| 524 | hash, tfm); |
Dmitry Kasatkin | ea59399 | 2013-06-07 12:16:24 +0200 | [diff] [blame] | 525 | |
| 526 | ima_free_tfm(tfm); |
| 527 | |
| 528 | return rc; |
| 529 | } |
| 530 | |
Mimi Zohar | 98304bc | 2015-12-28 11:56:09 -0500 | [diff] [blame] | 531 | static int calc_buffer_ahash_atfm(const void *buf, loff_t len, |
| 532 | struct ima_digest_data *hash, |
| 533 | struct crypto_ahash *tfm) |
| 534 | { |
| 535 | struct ahash_request *req; |
| 536 | struct scatterlist sg; |
Gilad Ben-Yossef | 46f1414 | 2017-10-18 08:00:47 +0100 | [diff] [blame] | 537 | struct crypto_wait wait; |
Mimi Zohar | 98304bc | 2015-12-28 11:56:09 -0500 | [diff] [blame] | 538 | int rc, ahash_rc = 0; |
| 539 | |
| 540 | hash->length = crypto_ahash_digestsize(tfm); |
| 541 | |
| 542 | req = ahash_request_alloc(tfm, GFP_KERNEL); |
| 543 | if (!req) |
| 544 | return -ENOMEM; |
| 545 | |
Gilad Ben-Yossef | 46f1414 | 2017-10-18 08:00:47 +0100 | [diff] [blame] | 546 | crypto_init_wait(&wait); |
Mimi Zohar | 98304bc | 2015-12-28 11:56:09 -0500 | [diff] [blame] | 547 | ahash_request_set_callback(req, CRYPTO_TFM_REQ_MAY_BACKLOG | |
| 548 | CRYPTO_TFM_REQ_MAY_SLEEP, |
Gilad Ben-Yossef | 46f1414 | 2017-10-18 08:00:47 +0100 | [diff] [blame] | 549 | crypto_req_done, &wait); |
Mimi Zohar | 98304bc | 2015-12-28 11:56:09 -0500 | [diff] [blame] | 550 | |
Gilad Ben-Yossef | 46f1414 | 2017-10-18 08:00:47 +0100 | [diff] [blame] | 551 | rc = ahash_wait(crypto_ahash_init(req), &wait); |
Mimi Zohar | 98304bc | 2015-12-28 11:56:09 -0500 | [diff] [blame] | 552 | if (rc) |
| 553 | goto out; |
| 554 | |
| 555 | sg_init_one(&sg, buf, len); |
| 556 | ahash_request_set_crypt(req, &sg, NULL, len); |
| 557 | |
| 558 | ahash_rc = crypto_ahash_update(req); |
| 559 | |
| 560 | /* wait for the update request to complete */ |
Gilad Ben-Yossef | 46f1414 | 2017-10-18 08:00:47 +0100 | [diff] [blame] | 561 | rc = ahash_wait(ahash_rc, &wait); |
Mimi Zohar | 98304bc | 2015-12-28 11:56:09 -0500 | [diff] [blame] | 562 | if (!rc) { |
| 563 | ahash_request_set_crypt(req, NULL, hash->digest, 0); |
Gilad Ben-Yossef | 46f1414 | 2017-10-18 08:00:47 +0100 | [diff] [blame] | 564 | rc = ahash_wait(crypto_ahash_final(req), &wait); |
Mimi Zohar | 98304bc | 2015-12-28 11:56:09 -0500 | [diff] [blame] | 565 | } |
| 566 | out: |
| 567 | ahash_request_free(req); |
| 568 | return rc; |
| 569 | } |
| 570 | |
| 571 | static int calc_buffer_ahash(const void *buf, loff_t len, |
| 572 | struct ima_digest_data *hash) |
| 573 | { |
| 574 | struct crypto_ahash *tfm; |
| 575 | int rc; |
| 576 | |
| 577 | tfm = ima_alloc_atfm(hash->algo); |
| 578 | if (IS_ERR(tfm)) |
| 579 | return PTR_ERR(tfm); |
| 580 | |
| 581 | rc = calc_buffer_ahash_atfm(buf, len, hash, tfm); |
| 582 | |
| 583 | ima_free_atfm(tfm); |
| 584 | |
| 585 | return rc; |
| 586 | } |
| 587 | |
Dmitry Kasatkin | 11d7646 | 2014-04-17 12:01:40 +0300 | [diff] [blame] | 588 | static int calc_buffer_shash_tfm(const void *buf, loff_t size, |
| 589 | struct ima_digest_data *hash, |
| 590 | struct crypto_shash *tfm) |
| 591 | { |
| 592 | SHASH_DESC_ON_STACK(shash, tfm); |
| 593 | unsigned int len; |
| 594 | int rc; |
| 595 | |
| 596 | shash->tfm = tfm; |
Dmitry Kasatkin | 11d7646 | 2014-04-17 12:01:40 +0300 | [diff] [blame] | 597 | |
| 598 | hash->length = crypto_shash_digestsize(tfm); |
| 599 | |
| 600 | rc = crypto_shash_init(shash); |
| 601 | if (rc != 0) |
| 602 | return rc; |
| 603 | |
| 604 | while (size) { |
| 605 | len = size < PAGE_SIZE ? size : PAGE_SIZE; |
| 606 | rc = crypto_shash_update(shash, buf, len); |
| 607 | if (rc) |
| 608 | break; |
| 609 | buf += len; |
| 610 | size -= len; |
| 611 | } |
| 612 | |
| 613 | if (!rc) |
| 614 | rc = crypto_shash_final(shash, hash->digest); |
| 615 | return rc; |
| 616 | } |
| 617 | |
Mimi Zohar | 98304bc | 2015-12-28 11:56:09 -0500 | [diff] [blame] | 618 | static int calc_buffer_shash(const void *buf, loff_t len, |
| 619 | struct ima_digest_data *hash) |
Dmitry Kasatkin | 11d7646 | 2014-04-17 12:01:40 +0300 | [diff] [blame] | 620 | { |
| 621 | struct crypto_shash *tfm; |
| 622 | int rc; |
| 623 | |
| 624 | tfm = ima_alloc_tfm(hash->algo); |
| 625 | if (IS_ERR(tfm)) |
| 626 | return PTR_ERR(tfm); |
| 627 | |
| 628 | rc = calc_buffer_shash_tfm(buf, len, hash, tfm); |
| 629 | |
| 630 | ima_free_tfm(tfm); |
| 631 | return rc; |
| 632 | } |
| 633 | |
Mimi Zohar | 98304bc | 2015-12-28 11:56:09 -0500 | [diff] [blame] | 634 | int ima_calc_buffer_hash(const void *buf, loff_t len, |
| 635 | struct ima_digest_data *hash) |
| 636 | { |
| 637 | int rc; |
| 638 | |
| 639 | if (ima_ahash_minsize && len >= ima_ahash_minsize) { |
| 640 | rc = calc_buffer_ahash(buf, len, hash); |
| 641 | if (!rc) |
| 642 | return 0; |
| 643 | } |
| 644 | |
| 645 | return calc_buffer_shash(buf, len, hash); |
| 646 | } |
| 647 | |
Roberto Sassu | 879b589 | 2019-02-06 17:24:49 +0100 | [diff] [blame] | 648 | static void __init ima_pcrread(u32 idx, struct tpm_digest *d) |
Mimi Zohar | 3323eec | 2009-02-04 09:06:58 -0500 | [diff] [blame] | 649 | { |
Stefan Berger | ec403d8e | 2018-06-26 15:09:33 -0400 | [diff] [blame] | 650 | if (!ima_tpm_chip) |
Mimi Zohar | 3323eec | 2009-02-04 09:06:58 -0500 | [diff] [blame] | 651 | return; |
| 652 | |
Roberto Sassu | 879b589 | 2019-02-06 17:24:49 +0100 | [diff] [blame] | 653 | if (tpm_pcr_read(ima_tpm_chip, idx, d) != 0) |
Joe Perches | 20ee451 | 2014-02-24 13:59:56 -0800 | [diff] [blame] | 654 | pr_err("Error Communicating to TPM chip\n"); |
Mimi Zohar | 3323eec | 2009-02-04 09:06:58 -0500 | [diff] [blame] | 655 | } |
| 656 | |
| 657 | /* |
| 658 | * Calculate the boot aggregate hash |
| 659 | */ |
Dmitry Kasatkin | 09ef543 | 2013-06-07 12:16:25 +0200 | [diff] [blame] | 660 | static int __init ima_calc_boot_aggregate_tfm(char *digest, |
| 661 | struct crypto_shash *tfm) |
Mimi Zohar | 3323eec | 2009-02-04 09:06:58 -0500 | [diff] [blame] | 662 | { |
Roberto Sassu | 879b589 | 2019-02-06 17:24:49 +0100 | [diff] [blame] | 663 | struct tpm_digest d = { .alg_id = TPM_ALG_SHA1, .digest = {0} }; |
Tomas Winkler | 95adc6b | 2018-10-19 21:23:07 +0300 | [diff] [blame] | 664 | int rc; |
| 665 | u32 i; |
Behan Webster | 357aabe | 2014-04-04 18:18:00 -0300 | [diff] [blame] | 666 | SHASH_DESC_ON_STACK(shash, tfm); |
Mimi Zohar | 3323eec | 2009-02-04 09:06:58 -0500 | [diff] [blame] | 667 | |
Behan Webster | 357aabe | 2014-04-04 18:18:00 -0300 | [diff] [blame] | 668 | shash->tfm = tfm; |
Dmitry Kasatkin | 76bb28f | 2012-06-08 10:42:30 +0300 | [diff] [blame] | 669 | |
Behan Webster | 357aabe | 2014-04-04 18:18:00 -0300 | [diff] [blame] | 670 | rc = crypto_shash_init(shash); |
Mimi Zohar | 3323eec | 2009-02-04 09:06:58 -0500 | [diff] [blame] | 671 | if (rc != 0) |
| 672 | return rc; |
| 673 | |
| 674 | /* cumulative sha1 over tpm registers 0-7 */ |
| 675 | for (i = TPM_PCR0; i < TPM_PCR8; i++) { |
Roberto Sassu | 879b589 | 2019-02-06 17:24:49 +0100 | [diff] [blame] | 676 | ima_pcrread(i, &d); |
Mimi Zohar | 3323eec | 2009-02-04 09:06:58 -0500 | [diff] [blame] | 677 | /* now accumulate with current aggregate */ |
Roberto Sassu | 879b589 | 2019-02-06 17:24:49 +0100 | [diff] [blame] | 678 | rc = crypto_shash_update(shash, d.digest, TPM_DIGEST_SIZE); |
Mimi Zohar | 3323eec | 2009-02-04 09:06:58 -0500 | [diff] [blame] | 679 | } |
| 680 | if (!rc) |
Behan Webster | 357aabe | 2014-04-04 18:18:00 -0300 | [diff] [blame] | 681 | crypto_shash_final(shash, digest); |
Mimi Zohar | 3323eec | 2009-02-04 09:06:58 -0500 | [diff] [blame] | 682 | return rc; |
| 683 | } |
Dmitry Kasatkin | 09ef543 | 2013-06-07 12:16:25 +0200 | [diff] [blame] | 684 | |
| 685 | int __init ima_calc_boot_aggregate(struct ima_digest_data *hash) |
| 686 | { |
| 687 | struct crypto_shash *tfm; |
| 688 | int rc; |
| 689 | |
| 690 | tfm = ima_alloc_tfm(hash->algo); |
| 691 | if (IS_ERR(tfm)) |
| 692 | return PTR_ERR(tfm); |
| 693 | |
| 694 | hash->length = crypto_shash_digestsize(tfm); |
| 695 | rc = ima_calc_boot_aggregate_tfm(hash->digest, tfm); |
| 696 | |
| 697 | ima_free_tfm(tfm); |
| 698 | |
| 699 | return rc; |
| 700 | } |