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