blob: 15f34bd40abed1530216be53c59658b6245d280b [file] [log] [blame]
Mimi Zohar3323eec2009-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 */
17#include <linux/module.h>
18#include <linux/scatterlist.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090019#include <linux/slab.h>
Mimi Zohar3323eec2009-02-04 09:06:58 -050020#include <linux/err.h>
Dmitry Kasatkin09ef5432013-06-07 12:16:25 +020021#include <crypto/hash_info.h>
Mimi Zohar3323eec2009-02-04 09:06:58 -050022#include "ima.h"
23
24/* name for boot aggregate entry */
Mimi Zohar523979a2009-02-11 11:12:28 -050025static const char *boot_aggregate_name = "boot_aggregate";
Mimi Zohar3323eec2009-02-04 09:06:58 -050026int ima_used_chip;
27
28/* Add the boot aggregate to the IMA measurement list and extend
29 * the PCR register.
30 *
31 * Calculate the boot aggregate, a SHA1 over tpm registers 0-7,
32 * assuming a TPM chip exists, and zeroes if the TPM chip does not
33 * exist. Add the boot aggregate measurement to the measurement
34 * list and extend the PCR register.
35 *
36 * If a tpm chip does not exist, indicate the core root of trust is
37 * not hardware based by invalidating the aggregate PCR value.
38 * (The aggregate PCR value is invalidated by adding one value to
39 * the measurement list and extending the aggregate PCR value with
40 * a different value.) Violations add a zero entry to the measurement
41 * list and extend the aggregate PCR value with ff...ff's.
42 */
Eric Paris932995f2009-05-21 15:43:32 -040043static void __init ima_add_boot_aggregate(void)
Mimi Zohar3323eec2009-02-04 09:06:58 -050044{
45 struct ima_template_entry *entry;
Roberto Sassu7bc5f442013-06-07 12:16:28 +020046 struct integrity_iint_cache tmp_iint, *iint = &tmp_iint;
Mimi Zohar3323eec2009-02-04 09:06:58 -050047 const char *op = "add_boot_aggregate";
48 const char *audit_cause = "ENOMEM";
49 int result = -ENOMEM;
Roberto Sassu7bc5f442013-06-07 12:16:28 +020050 int violation = 0;
Dmitry Kasatkin09ef5432013-06-07 12:16:25 +020051 struct {
52 struct ima_digest_data hdr;
53 char digest[TPM_DIGEST_SIZE];
54 } hash;
Mimi Zohar3323eec2009-02-04 09:06:58 -050055
Roberto Sassu7bc5f442013-06-07 12:16:28 +020056 memset(iint, 0, sizeof(*iint));
57 memset(&hash, 0, sizeof(hash));
58 iint->ima_hash = &hash.hdr;
59 iint->ima_hash->algo = HASH_ALGO_SHA1;
60 iint->ima_hash->length = SHA1_DIGEST_SIZE;
Mimi Zohar3323eec2009-02-04 09:06:58 -050061
Mimi Zohar3323eec2009-02-04 09:06:58 -050062 if (ima_used_chip) {
Dmitry Kasatkin09ef5432013-06-07 12:16:25 +020063 result = ima_calc_boot_aggregate(&hash.hdr);
Mimi Zohar3323eec2009-02-04 09:06:58 -050064 if (result < 0) {
65 audit_cause = "hashing_error";
66 kfree(entry);
67 goto err_out;
68 }
69 }
Roberto Sassu7bc5f442013-06-07 12:16:28 +020070
71 result = ima_alloc_init_template(iint, NULL, boot_aggregate_name,
Mimi Zoharbcbc9b0c2013-07-23 11:15:00 -040072 NULL, 0, &entry);
Roberto Sassu7bc5f442013-06-07 12:16:28 +020073 if (result < 0)
74 return;
75
Roberto Sassu9803d412013-06-07 12:16:27 +020076 result = ima_store_template(entry, violation, NULL,
77 boot_aggregate_name);
Mimi Zohar3323eec2009-02-04 09:06:58 -050078 if (result < 0)
79 kfree(entry);
80 return;
81err_out:
82 integrity_audit_msg(AUDIT_INTEGRITY_PCR, NULL, boot_aggregate_name, op,
83 audit_cause, result, 0);
84}
85
Eric Paris932995f2009-05-21 15:43:32 -040086int __init ima_init(void)
Mimi Zohar3323eec2009-02-04 09:06:58 -050087{
Mimi Zohar140d8022013-03-11 20:29:47 -040088 u8 pcr_i[TPM_DIGEST_SIZE];
Mimi Zohar3323eec2009-02-04 09:06:58 -050089 int rc;
90
91 ima_used_chip = 0;
92 rc = tpm_pcr_read(TPM_ANY_NUM, 0, pcr_i);
93 if (rc == 0)
94 ima_used_chip = 1;
95
96 if (!ima_used_chip)
Eric Pariseb8dae92010-04-22 10:49:36 -040097 pr_info("IMA: No TPM chip found, activating TPM-bypass!\n");
Mimi Zohar3323eec2009-02-04 09:06:58 -050098
Dmitry Kasatkin76bb28f2012-06-08 10:42:30 +030099 rc = ima_init_crypto();
100 if (rc)
101 return rc;
Roberto Sassuadf53a72013-06-07 12:16:29 +0200102 rc = ima_init_template();
103 if (rc != 0)
104 return rc;
105
Mimi Zohar3323eec2009-02-04 09:06:58 -0500106 ima_add_boot_aggregate(); /* boot aggregate must be first entry */
107 ima_init_policy();
Mimi Zoharbab73932009-02-04 09:06:59 -0500108
109 return ima_fs_init();
110}