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