1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
|
/* Copyright 2013-2017 IBM Corp.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
* implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#ifndef pr_fmt
#define pr_fmt(fmt) "STB: " fmt
#endif
#include <skiboot.h>
#include <device.h>
#include <nvram.h>
#include <opal-api.h>
#include <inttypes.h>
#include "secureboot.h"
static const void* hw_key_hash = NULL;
static size_t hw_key_hash_size;
static bool secure_mode = false;
static struct {
enum secureboot_version version;
const char *compat;
} secureboot_map[] = {
{ IBM_SECUREBOOT_V1, "ibm,secureboot-v1" },
{ IBM_SECUREBOOT_SOFTROM, "ibm,secureboot-v1-softrom" },
};
static void secureboot_enforce(void)
{
/* Sanity check */
if (!secure_mode)
return;
/*
* TODO: Ideally, the BMC should decide what security policy to apply
* (power off, reboot, switch PNOR sides, etc). We may need to provide
* extra info to BMC other than just abort. Terminate Immediate
* Attention ? (TI)
*/
prlog(PR_EMERG, "secure mode enforced, aborting.\n");
abort();
}
bool secureboot_is_compatible(struct dt_node *node, int *version, const char **compat)
{
int i;
if (!node)
return false;
for (i = 0; i < ARRAY_SIZE(secureboot_map); i++) {
if (dt_node_is_compatible(node, secureboot_map[i].compat)) {
if (version)
*version = secureboot_map[i].version;
if (compat)
*compat = secureboot_map[i].compat;
return true;
}
}
return false;
}
void secureboot_init(void)
{
struct dt_node *node;
const char *hash_algo;
const char *compat = NULL;
int version;
size_t size;
node = dt_find_by_path(dt_root, "/ibm,secureboot");
if (!node) {
prlog(PR_NOTICE, "secure boot not supported\n");
return;
}
if (!secureboot_is_compatible(node, &version, &compat)) {
/**
* @fwts-label SecureBootNotCompatible
* @fwts-advice Compatible secureboot driver not found. Probably,
* hostboot/mambo/skiboot has updated the
* /ibm,secureboot/compatible without adding a driver that
* supports it.
*/
prlog(PR_ERR, "%s FAILED, /ibm,secureboot not compatible.\n",
__func__);
return;
}
prlog(PR_NOTICE, "Found %s\n", compat);
if (nvram_query_eq("force-secure-mode", "always")) {
secure_mode = true;
prlog(PR_NOTICE, "secure mode on (FORCED by nvram)\n");
} else {
secure_mode = dt_has_node_property(node, "secure-enabled", NULL);
prlog(PR_NOTICE, "secure mode %s\n",
secure_mode ? "on" : "off");
}
if (!secure_mode)
return;
if (version == IBM_SECUREBOOT_V1 ||
version == IBM_SECUREBOOT_SOFTROM) {
hash_algo = dt_prop_get(node, "hash-algo");
if (strcmp(hash_algo, "sha512")) {
/**
* @fwts-label HashAlgoInvalid
* @fwts-advice Hash algorithm invalid, secureboot
* containers version 1 requires sha512. If you're
* running the latest POWER firmware, so probably there
* is a bug in the device tree received from hostboot.
*/
prlog(PR_EMERG, "secureboot init FAILED, hash-algo=%s "
"not supported\n", hash_algo);
secureboot_enforce();
}
hw_key_hash_size = SHA512_DIGEST_LENGTH;
} else {
prlog(PR_ERR, "%s FAILED. /ibm,secureboot not supported",
__func__);
secureboot_enforce();
}
hw_key_hash = dt_prop_get_def_size(node, "hw-key-hash", NULL, &size);
if (!hw_key_hash) {
prlog(PR_EMERG, "hw-key-hash not found\n");
secureboot_enforce();
}
if (size != hw_key_hash_size) {
prlog(PR_EMERG, "hw_key-hash wrong size %zd (expected=%zd)\n",
size, hw_key_hash_size);
secureboot_enforce();
}
if (cvc_init())
secureboot_enforce();
}
int secureboot_verify(enum resource_id id, void *buf, size_t len)
{
const char *name;
uint64_t log;
int rc = -1;
if (!secure_mode)
return 0;
name = flash_map_resource_name(id);
if (!name) {
prlog(PR_EMERG, "container NOT VERIFIED, resource_id=%d "
"unknown\n", id);
secureboot_enforce();
}
rc = call_cvc_verify(buf, len, hw_key_hash, hw_key_hash_size, &log);
if (rc == OPAL_SUCCESS) {
prlog(PR_INFO, "%s verified\n", name);
} else if (rc == OPAL_PARTIAL) {
/*
* The value returned in log indicates what checking has
* failed. Return codes defined in
* /hostboot/src/include/securerom/status_codes.H
*/
prlog(PR_EMERG, "%s verification FAILED. log=0x%" PRIx64 "\n",
name, be64_to_cpu(log));
secureboot_enforce();
} else if (rc == OPAL_PARAMETER) {
prlog(PR_EMERG, "%s NOT VERIFIED, invalid param. buf=%p, "
"len=%zd key-hash=%p hash-size=%zd\n", name, buf, len,
hw_key_hash, hw_key_hash_size);
secureboot_enforce();
} else if (rc == OPAL_UNSUPPORTED) {
prlog(PR_EMERG, "%s NOT VERIFIED, CVC-verify service not "
"supported\n", name);
secureboot_enforce();
} else {
prlog(PR_EMERG, "%s NOT VERIFIED, unknown CVC-verify error. "
"rc=%d\n", name, rc);
secureboot_enforce();
}
return 0;
}
|