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 */
17
18#define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
19
20#include <linux/module.h>
21#include <linux/scatterlist.h>
22#include <linux/slab.h>
23#include <linux/err.h>
24#include <crypto/hash_info.h>
25#include "ima.h"
26
27#ifdef CONFIG_IMA_X509_PATH
28#define IMA_X509_PATH	CONFIG_IMA_X509_PATH
29#else
30#define IMA_X509_PATH	"/etc/keys/x509_ima.der"
31#endif
32
33/* name for boot aggregate entry */
34static const char *boot_aggregate_name = "boot_aggregate";
35int ima_used_chip;
36
37/* Add the boot aggregate to the IMA measurement list and extend
38 * the PCR register.
39 *
40 * Calculate the boot aggregate, a SHA1 over tpm registers 0-7,
41 * assuming a TPM chip exists, and zeroes if the TPM chip does not
42 * exist.  Add the boot aggregate measurement to the measurement
43 * list and extend the PCR register.
44 *
45 * If a tpm chip does not exist, indicate the core root of trust is
46 * not hardware based by invalidating the aggregate PCR value.
47 * (The aggregate PCR value is invalidated by adding one value to
48 * the measurement list and extending the aggregate PCR value with
49 * a different value.) Violations add a zero entry to the measurement
50 * list and extend the aggregate PCR value with ff...ff's.
51 */
52static int __init ima_add_boot_aggregate(void)
53{
54	static const char op[] = "add_boot_aggregate";
55	const char *audit_cause = "ENOMEM";
56	struct ima_template_entry *entry;
57	struct integrity_iint_cache tmp_iint, *iint = &tmp_iint;
58	int result = -ENOMEM;
59	int violation = 0;
60	struct {
61		struct ima_digest_data hdr;
62		char digest[TPM_DIGEST_SIZE];
63	} hash;
64
65	memset(iint, 0, sizeof(*iint));
66	memset(&hash, 0, sizeof(hash));
67	iint->ima_hash = &hash.hdr;
68	iint->ima_hash->algo = HASH_ALGO_SHA1;
69	iint->ima_hash->length = SHA1_DIGEST_SIZE;
70
71	if (ima_used_chip) {
72		result = ima_calc_boot_aggregate(&hash.hdr);
73		if (result < 0) {
74			audit_cause = "hashing_error";
75			goto err_out;
76		}
77	}
78
79	result = ima_alloc_init_template(iint, NULL, boot_aggregate_name,
80					 NULL, 0, &entry);
81	if (result < 0) {
82		audit_cause = "alloc_entry";
83		goto err_out;
84	}
85
86	result = ima_store_template(entry, violation, NULL,
87				    boot_aggregate_name);
88	if (result < 0) {
89		ima_free_template_entry(entry);
90		audit_cause = "store_entry";
91		goto err_out;
92	}
93	return 0;
94err_out:
95	integrity_audit_msg(AUDIT_INTEGRITY_PCR, NULL, boot_aggregate_name, op,
96			    audit_cause, result, 0);
97	return result;
98}
99
100#ifdef CONFIG_IMA_LOAD_X509
101void __init ima_load_x509(void)
102{
103	int unset_flags = ima_policy_flag & IMA_APPRAISE;
104
105	ima_policy_flag &= ~unset_flags;
106	integrity_load_x509(INTEGRITY_KEYRING_IMA, IMA_X509_PATH);
107	ima_policy_flag |= unset_flags;
108}
109#endif
110
111int __init ima_init(void)
112{
113	u8 pcr_i[TPM_DIGEST_SIZE];
114	int rc;
115
116	ima_used_chip = 0;
117	rc = tpm_pcr_read(TPM_ANY_NUM, 0, pcr_i);
118	if (rc == 0)
119		ima_used_chip = 1;
120
121	if (!ima_used_chip)
122		pr_info("No TPM chip found, activating TPM-bypass!\n");
123
124	rc = ima_init_keyring(INTEGRITY_KEYRING_IMA);
125	if (rc)
126		return rc;
127
128	rc = ima_init_crypto();
129	if (rc)
130		return rc;
131	rc = ima_init_template();
132	if (rc != 0)
133		return rc;
134
135	rc = ima_add_boot_aggregate();	/* boot aggregate must be first entry */
136	if (rc != 0)
137		return rc;
138
139	ima_init_policy();
140
141	return ima_fs_init();
142}
143