blob: 32912bd54eadda1d73bf24d30f0d7a6a9f849c42 [file] [log] [blame]
Mimi Zohar3323eec92009-02-04 09:06:58 -05001/*
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 Perches20ee4512014-02-24 13:59:56 -080017
18#define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
19
Mimi Zohar3323eec92009-02-04 09:06:58 -050020#include <linux/module.h>
21#include <linux/scatterlist.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090022#include <linux/slab.h>
Mimi Zohar3323eec92009-02-04 09:06:58 -050023#include <linux/err.h>
Dmitry Kasatkin1525b062014-10-30 12:39:39 +020024
Mimi Zohar3323eec92009-02-04 09:06:58 -050025#include "ima.h"
26
27/* name for boot aggregate entry */
Mimi Zohar523979a2009-02-11 11:12:28 -050028static const char *boot_aggregate_name = "boot_aggregate";
Mimi Zohar3323eec92009-02-04 09:06:58 -050029int 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 */
Roberto Sassube39ffc2014-09-12 19:35:53 +020046static int __init ima_add_boot_aggregate(void)
Mimi Zohar3323eec92009-02-04 09:06:58 -050047{
Mimi Zohar52a13282013-12-11 14:44:04 -050048 static const char op[] = "add_boot_aggregate";
49 const char *audit_cause = "ENOMEM";
Mimi Zohar3323eec92009-02-04 09:06:58 -050050 struct ima_template_entry *entry;
Roberto Sassu7bc5f442013-06-07 12:16:28 +020051 struct integrity_iint_cache tmp_iint, *iint = &tmp_iint;
Roberto Sassu23b57412015-04-11 17:09:50 +020052 struct ima_event_data event_data = {iint, NULL, boot_aggregate_name,
Roberto Sassu8d94eb92015-04-11 17:12:39 +020053 NULL, 0, NULL};
Mimi Zohar3323eec92009-02-04 09:06:58 -050054 int result = -ENOMEM;
Roberto Sassu7bc5f442013-06-07 12:16:28 +020055 int violation = 0;
Dmitry Kasatkin09ef5432013-06-07 12:16:25 +020056 struct {
57 struct ima_digest_data hdr;
58 char digest[TPM_DIGEST_SIZE];
59 } hash;
Mimi Zohar3323eec92009-02-04 09:06:58 -050060
Roberto Sassu7bc5f442013-06-07 12:16:28 +020061 memset(iint, 0, sizeof(*iint));
62 memset(&hash, 0, sizeof(hash));
63 iint->ima_hash = &hash.hdr;
64 iint->ima_hash->algo = HASH_ALGO_SHA1;
65 iint->ima_hash->length = SHA1_DIGEST_SIZE;
Mimi Zohar3323eec92009-02-04 09:06:58 -050066
Mimi Zohar3323eec92009-02-04 09:06:58 -050067 if (ima_used_chip) {
Dmitry Kasatkin09ef5432013-06-07 12:16:25 +020068 result = ima_calc_boot_aggregate(&hash.hdr);
Mimi Zohar3323eec92009-02-04 09:06:58 -050069 if (result < 0) {
70 audit_cause = "hashing_error";
Mimi Zohar3323eec92009-02-04 09:06:58 -050071 goto err_out;
72 }
73 }
Roberto Sassu7bc5f442013-06-07 12:16:28 +020074
Roberto Sassu23b57412015-04-11 17:09:50 +020075 result = ima_alloc_init_template(&event_data, &entry);
Roberto Sassube39ffc2014-09-12 19:35:53 +020076 if (result < 0) {
77 audit_cause = "alloc_entry";
78 goto err_out;
79 }
Roberto Sassu7bc5f442013-06-07 12:16:28 +020080
Roberto Sassu9803d412013-06-07 12:16:27 +020081 result = ima_store_template(entry, violation, NULL,
Eric Richter14b1da82016-06-01 13:14:03 -050082 boot_aggregate_name,
83 CONFIG_IMA_MEASURE_PCR_IDX);
Roberto Sassube39ffc2014-09-12 19:35:53 +020084 if (result < 0) {
Roberto Sassua7ed7c62013-12-02 19:40:34 +010085 ima_free_template_entry(entry);
Roberto Sassube39ffc2014-09-12 19:35:53 +020086 audit_cause = "store_entry";
87 goto err_out;
88 }
89 return 0;
Mimi Zohar3323eec92009-02-04 09:06:58 -050090err_out:
91 integrity_audit_msg(AUDIT_INTEGRITY_PCR, NULL, boot_aggregate_name, op,
92 audit_cause, result, 0);
Roberto Sassube39ffc2014-09-12 19:35:53 +020093 return result;
Mimi Zohar3323eec92009-02-04 09:06:58 -050094}
95
Dmitry Kasatkinfd5f4e902014-11-05 17:01:14 +020096#ifdef CONFIG_IMA_LOAD_X509
97void __init ima_load_x509(void)
98{
99 int unset_flags = ima_policy_flag & IMA_APPRAISE;
100
101 ima_policy_flag &= ~unset_flags;
Dmitry Kasatkina18d0cb2014-11-26 16:59:54 +0200102 integrity_load_x509(INTEGRITY_KEYRING_IMA, CONFIG_IMA_X509_PATH);
Dmitry Kasatkinfd5f4e902014-11-05 17:01:14 +0200103 ima_policy_flag |= unset_flags;
104}
105#endif
106
Eric Paris932995f2009-05-21 15:43:32 -0400107int __init ima_init(void)
Mimi Zohar3323eec92009-02-04 09:06:58 -0500108{
Mimi Zohar140d8022013-03-11 20:29:47 -0400109 u8 pcr_i[TPM_DIGEST_SIZE];
Mimi Zohar3323eec92009-02-04 09:06:58 -0500110 int rc;
111
112 ima_used_chip = 0;
113 rc = tpm_pcr_read(TPM_ANY_NUM, 0, pcr_i);
114 if (rc == 0)
115 ima_used_chip = 1;
116
117 if (!ima_used_chip)
Joe Perches20ee4512014-02-24 13:59:56 -0800118 pr_info("No TPM chip found, activating TPM-bypass!\n");
Mimi Zohar3323eec92009-02-04 09:06:58 -0500119
Dmitry Kasatkinf4dc3772015-10-22 21:26:10 +0300120 rc = integrity_init_keyring(INTEGRITY_KEYRING_IMA);
Dmitry Kasatkin31b70f62014-06-27 13:01:32 +0300121 if (rc)
122 return rc;
123
Dmitry Kasatkin76bb28f2012-06-08 10:42:30 +0300124 rc = ima_init_crypto();
125 if (rc)
126 return rc;
Roberto Sassuadf53a72013-06-07 12:16:29 +0200127 rc = ima_init_template();
128 if (rc != 0)
129 return rc;
130
Roberto Sassube39ffc2014-09-12 19:35:53 +0200131 rc = ima_add_boot_aggregate(); /* boot aggregate must be first entry */
132 if (rc != 0)
133 return rc;
134
Mimi Zohar3323eec92009-02-04 09:06:58 -0500135 ima_init_policy();
Mimi Zoharbab73932009-02-04 09:06:59 -0500136
137 return ima_fs_init();
138}