-
Notifications
You must be signed in to change notification settings - Fork 23
/
secure_boot_check.cpp
201 lines (178 loc) · 6.08 KB
/
secure_boot_check.cpp
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
198
199
200
201
#include "config.h"
#include "utils.hpp"
#include <phosphor-logging/lg2.hpp>
#include <xyz/openbmc_project/Logging/Settings/client.hpp>
#include <filesystem>
#include <fstream>
#include <string>
PHOSPHOR_LOG2_USING;
constexpr auto PROPERTY_INTERFACE = "org.freedesktop.DBus.Properties";
using LoggingSettings =
sdbusplus::client::xyz::openbmc_project::logging::Settings<>;
// Check if the TPM measurement file exists and has a valid value.
// If the TPM measurement is invalid, it logs an error message.
void checkTpmMeasurement()
{
bool tpmError = false;
std::string errorMsg;
if (!std::filesystem::exists(std::string(SYSFS_TPM_MEASUREMENT_PATH)))
{
tpmError = true;
errorMsg = "TPM measurement file does not exist: " +
std::string(SYSFS_TPM_MEASUREMENT_PATH);
}
else
{
std::string tpmValueStr;
std::ifstream tpmFile(std::string(SYSFS_TPM_MEASUREMENT_PATH));
tpmFile >> tpmValueStr;
if (tpmValueStr.empty())
{
tpmError = true;
errorMsg = "TPM measurement value is empty: " +
std::string(SYSFS_TPM_MEASUREMENT_PATH);
}
else if (tpmValueStr == "0")
{
tpmError = true;
errorMsg = "TPM measurement value is 0: " +
std::string(SYSFS_TPM_MEASUREMENT_PATH);
}
tpmFile.close();
}
if (tpmError)
{
// Doesn't have valid TPM measurement, log an error message
std::map<std::string, std::string> additionalData;
error("{ERROR}", "ERROR", errorMsg);
additionalData.emplace("ERROR", errorMsg);
auto bus = sdbusplus::bus::new_default();
phosphor::state::manager::utils::createError(
bus, "xyz.openbmc_project.State.Error.TpmMeasurementFail",
sdbusplus::server::xyz::openbmc_project::logging::Entry::Level::
Error,
additionalData);
}
return;
}
// Utilize the QuiesceOnHwError setting as an indication that the system
// is operating in an environment where the user should be notified of
// security settings (i.e. "Manufacturing")
bool isMfgModeEnabled()
{
auto bus = sdbusplus::bus::new_default();
std::string path = "/xyz/openbmc_project/logging/settings";
std::string interface = LoggingSettings::interface;
std::string propertyName = "QuiesceOnHwError";
std::variant<bool> mfgModeEnabled;
std::string service =
phosphor::state::manager::utils::getService(bus, path, interface);
auto method = bus.new_method_call(service.c_str(), path.c_str(),
PROPERTY_INTERFACE, "Get");
method.append(interface, propertyName);
try
{
auto reply = bus.call(method);
reply.read(mfgModeEnabled);
}
catch (const sdbusplus::exception_t& e)
{
error("Error in property Get, error {ERROR}, property {PROPERTY}",
"ERROR", e, "PROPERTY", propertyName);
throw;
}
return std::get<bool>(mfgModeEnabled);
}
int main()
{
// Read the secure boot gpio
auto secureBootGpio =
phosphor::state::manager::utils::getGpioValue("bmc-secure-boot");
if (secureBootGpio == -1)
{
debug("bmc-secure-boot gpio not present or can not be read");
}
else if (secureBootGpio == 0)
{
info("bmc-secure-boot gpio found and indicates it is NOT enabled");
}
else
{
info("bmc-secure-boot found and indicates it is enabled");
}
// Now read the /sys/kernel/debug/aspeed/ files
std::string dbgVal;
std::ifstream dbgFile;
int secureBootVal = -1;
int abrImage = -1;
dbgFile.exceptions(
std::ifstream::failbit | std::ifstream::badbit | std::ifstream::eofbit);
if (std::filesystem::exists(SYSFS_SECURE_BOOT_PATH))
{
try
{
dbgFile.open(SYSFS_SECURE_BOOT_PATH);
dbgFile >> dbgVal;
dbgFile.close();
info("Read {SECURE_BOOT_VAL} from secure_boot", "SECURE_BOOT_VAL",
dbgVal);
secureBootVal = std::stoi(dbgVal);
}
catch (std::exception& e)
{
error("Failed to read secure boot sysfs file: {ERROR}", "ERROR", e);
// just continue and error will be logged at end if in mfg mode
}
}
else
{
info("sysfs file secure_boot not present");
}
if (std::filesystem::exists(SYSFS_ABR_IMAGE_PATH))
{
try
{
dbgFile.open(SYSFS_ABR_IMAGE_PATH);
dbgFile >> dbgVal;
dbgFile.close();
info("Read {ABR_IMAGE_VAL} from abr_image", "ABR_IMAGE_VAL",
dbgVal);
abrImage = std::stoi(dbgVal);
}
catch (std::exception& e)
{
error("Failed to read abr image sysfs file: {ERROR}", "ERROR", e);
// just continue and error will be logged at end if in mfg mode
}
}
else
{
info("sysfs file abr_image not present");
}
if (isMfgModeEnabled())
{
if ((secureBootGpio != 1) || (secureBootVal != 1) || (abrImage != 0))
{
error("The system is not secure");
std::map<std::string, std::string> additionalData;
additionalData.emplace("SECURE_BOOT_GPIO",
std::to_string(secureBootGpio));
additionalData.emplace("SYSFS_SECURE_BOOT_VAL",
std::to_string(secureBootVal));
additionalData.emplace("SYSFS_ABR_IMAGE_VAL",
std::to_string(abrImage));
auto bus = sdbusplus::bus::new_default();
phosphor::state::manager::utils::createError(
bus, "xyz.openbmc_project.State.Error.SecurityCheckFail",
sdbusplus::server::xyz::openbmc_project::logging::Entry::Level::
Warning,
additionalData);
}
}
// Check the TPM measurement if TPM is enabled
if (std::filesystem::exists(std::string(SYSFS_TPM_DEVICE_PATH)))
{
checkTpmMeasurement();
}
return 0;
}