forked from ibm-openbmc/phosphor-state-manager
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsecure_boot_check.cpp
144 lines (124 loc) · 4.24 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
#include "config.h"
#include "utils.hpp"
#include <phosphor-logging/lg2.hpp>
#include <filesystem>
#include <fstream>
#include <string>
PHOSPHOR_LOG2_USING;
constexpr auto PROPERTY_INTERFACE = "org.freedesktop.DBus.Properties";
// 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 = "xyz.openbmc_project.Logging.Settings";
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::exception& 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::xyz::openbmc_project::Logging::server::Entry::Level::
Warning,
additionalData);
}
}
return 0;
}