Mimi Zohar | 3323eec9 | 2009-02-04 09:06:58 -0500 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2005,2006,2007,2008 IBM Corporation |
| 3 | * |
| 4 | * Authors: |
| 5 | * Reiner Sailer <sailer@watson.ibm.com> |
| 6 | * Leendert van Doorn <leendert@watson.ibm.com> |
| 7 | * Mimi Zohar <zohar@us.ibm.com> |
| 8 | * |
| 9 | * This program is free software; you can redistribute it and/or |
| 10 | * modify it under the terms of the GNU General Public License as |
| 11 | * published by the Free Software Foundation, version 2 of the |
| 12 | * License. |
| 13 | * |
| 14 | * File: ima_init.c |
| 15 | * initialization and cleanup functions |
| 16 | */ |
Joe Perches | 20ee451 | 2014-02-24 13:59:56 -0800 | [diff] [blame] | 17 | |
| 18 | #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt |
| 19 | |
Mimi Zohar | 3323eec9 | 2009-02-04 09:06:58 -0500 | [diff] [blame] | 20 | #include <linux/module.h> |
| 21 | #include <linux/scatterlist.h> |
Tejun Heo | 5a0e3ad | 2010-03-24 17:04:11 +0900 | [diff] [blame] | 22 | #include <linux/slab.h> |
Mimi Zohar | 3323eec9 | 2009-02-04 09:06:58 -0500 | [diff] [blame] | 23 | #include <linux/err.h> |
Dmitry Kasatkin | 09ef543 | 2013-06-07 12:16:25 +0200 | [diff] [blame] | 24 | #include <crypto/hash_info.h> |
Mimi Zohar | 3323eec9 | 2009-02-04 09:06:58 -0500 | [diff] [blame] | 25 | #include "ima.h" |
| 26 | |
| 27 | /* name for boot aggregate entry */ |
Mimi Zohar | 523979a | 2009-02-11 11:12:28 -0500 | [diff] [blame] | 28 | static const char *boot_aggregate_name = "boot_aggregate"; |
Mimi Zohar | 3323eec9 | 2009-02-04 09:06:58 -0500 | [diff] [blame] | 29 | int ima_used_chip; |
| 30 | |
| 31 | /* Add the boot aggregate to the IMA measurement list and extend |
| 32 | * the PCR register. |
| 33 | * |
| 34 | * Calculate the boot aggregate, a SHA1 over tpm registers 0-7, |
| 35 | * assuming a TPM chip exists, and zeroes if the TPM chip does not |
| 36 | * exist. Add the boot aggregate measurement to the measurement |
| 37 | * list and extend the PCR register. |
| 38 | * |
| 39 | * If a tpm chip does not exist, indicate the core root of trust is |
| 40 | * not hardware based by invalidating the aggregate PCR value. |
| 41 | * (The aggregate PCR value is invalidated by adding one value to |
| 42 | * the measurement list and extending the aggregate PCR value with |
| 43 | * a different value.) Violations add a zero entry to the measurement |
| 44 | * list and extend the aggregate PCR value with ff...ff's. |
| 45 | */ |
Eric Paris | 932995f | 2009-05-21 15:43:32 -0400 | [diff] [blame] | 46 | static void __init ima_add_boot_aggregate(void) |
Mimi Zohar | 3323eec9 | 2009-02-04 09:06:58 -0500 | [diff] [blame] | 47 | { |
Mimi Zohar | 52a1328 | 2013-12-11 14:44:04 -0500 | [diff] [blame] | 48 | static const char op[] = "add_boot_aggregate"; |
| 49 | const char *audit_cause = "ENOMEM"; |
Mimi Zohar | 3323eec9 | 2009-02-04 09:06:58 -0500 | [diff] [blame] | 50 | struct ima_template_entry *entry; |
Roberto Sassu | 7bc5f44 | 2013-06-07 12:16:28 +0200 | [diff] [blame] | 51 | struct integrity_iint_cache tmp_iint, *iint = &tmp_iint; |
Mimi Zohar | 3323eec9 | 2009-02-04 09:06:58 -0500 | [diff] [blame] | 52 | int result = -ENOMEM; |
Roberto Sassu | 7bc5f44 | 2013-06-07 12:16:28 +0200 | [diff] [blame] | 53 | int violation = 0; |
Dmitry Kasatkin | 09ef543 | 2013-06-07 12:16:25 +0200 | [diff] [blame] | 54 | struct { |
| 55 | struct ima_digest_data hdr; |
| 56 | char digest[TPM_DIGEST_SIZE]; |
| 57 | } hash; |
Mimi Zohar | 3323eec9 | 2009-02-04 09:06:58 -0500 | [diff] [blame] | 58 | |
Roberto Sassu | 7bc5f44 | 2013-06-07 12:16:28 +0200 | [diff] [blame] | 59 | memset(iint, 0, sizeof(*iint)); |
| 60 | memset(&hash, 0, sizeof(hash)); |
| 61 | iint->ima_hash = &hash.hdr; |
| 62 | iint->ima_hash->algo = HASH_ALGO_SHA1; |
| 63 | iint->ima_hash->length = SHA1_DIGEST_SIZE; |
Mimi Zohar | 3323eec9 | 2009-02-04 09:06:58 -0500 | [diff] [blame] | 64 | |
Mimi Zohar | 3323eec9 | 2009-02-04 09:06:58 -0500 | [diff] [blame] | 65 | if (ima_used_chip) { |
Dmitry Kasatkin | 09ef543 | 2013-06-07 12:16:25 +0200 | [diff] [blame] | 66 | result = ima_calc_boot_aggregate(&hash.hdr); |
Mimi Zohar | 3323eec9 | 2009-02-04 09:06:58 -0500 | [diff] [blame] | 67 | if (result < 0) { |
| 68 | audit_cause = "hashing_error"; |
Mimi Zohar | 3323eec9 | 2009-02-04 09:06:58 -0500 | [diff] [blame] | 69 | goto err_out; |
| 70 | } |
| 71 | } |
Roberto Sassu | 7bc5f44 | 2013-06-07 12:16:28 +0200 | [diff] [blame] | 72 | |
| 73 | result = ima_alloc_init_template(iint, NULL, boot_aggregate_name, |
Mimi Zohar | bcbc9b0 | 2013-07-23 11:15:00 -0400 | [diff] [blame] | 74 | NULL, 0, &entry); |
Roberto Sassu | 7bc5f44 | 2013-06-07 12:16:28 +0200 | [diff] [blame] | 75 | if (result < 0) |
| 76 | return; |
| 77 | |
Roberto Sassu | 9803d41 | 2013-06-07 12:16:27 +0200 | [diff] [blame] | 78 | result = ima_store_template(entry, violation, NULL, |
| 79 | boot_aggregate_name); |
Mimi Zohar | 3323eec9 | 2009-02-04 09:06:58 -0500 | [diff] [blame] | 80 | if (result < 0) |
Roberto Sassu | a7ed7c6 | 2013-12-02 19:40:34 +0100 | [diff] [blame] | 81 | ima_free_template_entry(entry); |
Mimi Zohar | 3323eec9 | 2009-02-04 09:06:58 -0500 | [diff] [blame] | 82 | return; |
| 83 | err_out: |
| 84 | integrity_audit_msg(AUDIT_INTEGRITY_PCR, NULL, boot_aggregate_name, op, |
| 85 | audit_cause, result, 0); |
| 86 | } |
| 87 | |
Eric Paris | 932995f | 2009-05-21 15:43:32 -0400 | [diff] [blame] | 88 | int __init ima_init(void) |
Mimi Zohar | 3323eec9 | 2009-02-04 09:06:58 -0500 | [diff] [blame] | 89 | { |
Mimi Zohar | 140d802 | 2013-03-11 20:29:47 -0400 | [diff] [blame] | 90 | u8 pcr_i[TPM_DIGEST_SIZE]; |
Mimi Zohar | 3323eec9 | 2009-02-04 09:06:58 -0500 | [diff] [blame] | 91 | int rc; |
| 92 | |
| 93 | ima_used_chip = 0; |
| 94 | rc = tpm_pcr_read(TPM_ANY_NUM, 0, pcr_i); |
| 95 | if (rc == 0) |
| 96 | ima_used_chip = 1; |
| 97 | |
| 98 | if (!ima_used_chip) |
Joe Perches | 20ee451 | 2014-02-24 13:59:56 -0800 | [diff] [blame] | 99 | pr_info("No TPM chip found, activating TPM-bypass!\n"); |
Mimi Zohar | 3323eec9 | 2009-02-04 09:06:58 -0500 | [diff] [blame] | 100 | |
Dmitry Kasatkin | 76bb28f | 2012-06-08 10:42:30 +0300 | [diff] [blame] | 101 | rc = ima_init_crypto(); |
| 102 | if (rc) |
| 103 | return rc; |
Roberto Sassu | adf53a7 | 2013-06-07 12:16:29 +0200 | [diff] [blame] | 104 | rc = ima_init_template(); |
| 105 | if (rc != 0) |
| 106 | return rc; |
| 107 | |
Mimi Zohar | 3323eec9 | 2009-02-04 09:06:58 -0500 | [diff] [blame] | 108 | ima_add_boot_aggregate(); /* boot aggregate must be first entry */ |
| 109 | ima_init_policy(); |
Mimi Zohar | bab7393 | 2009-02-04 09:06:59 -0500 | [diff] [blame] | 110 | |
| 111 | return ima_fs_init(); |
| 112 | } |