-
Notifications
You must be signed in to change notification settings - Fork 77
/
whitelist-filter.cpp
253 lines (213 loc) · 7.58 KB
/
whitelist-filter.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
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
#include <ipmiallowlist.hpp>
#include <ipmid/api.hpp>
#include <ipmid/utils.hpp>
#include <phosphor-logging/elog-errors.hpp>
#include <phosphor-logging/lg2.hpp>
#include <settings.hpp>
#include <xyz/openbmc_project/Common/error.hpp>
#include <xyz/openbmc_project/Control/Security/RestrictionMode/server.hpp>
#include <algorithm>
#include <array>
using namespace phosphor::logging;
using namespace sdbusplus::error::xyz::openbmc_project::common;
namespace ipmi
{
// put the filter provider in an unnamed namespace
namespace
{
/** @class AllowlistFilter
*
* Class that implements an IPMI message filter based
* on incoming interface and a restriction mode setting
*/
class AllowlistFilter
{
public:
AllowlistFilter();
~AllowlistFilter() = default;
AllowlistFilter(const AllowlistFilter&) = delete;
AllowlistFilter(AllowlistFilter&&) = delete;
AllowlistFilter& operator=(const AllowlistFilter&) = delete;
AllowlistFilter& operator=(AllowlistFilter&&) = delete;
private:
void postInit();
void cacheRestrictedMode(const std::vector<std::string>& devices);
void handleRestrictedModeChange(
sdbusplus::message_t& m,
const std::map<std::string, size_t>& deviceList);
ipmi::Cc filterMessage(ipmi::message::Request::ptr request);
std::vector<bool> restrictedMode;
std::shared_ptr<sdbusplus::asio::connection> bus;
std::unique_ptr<settings::Objects> objects;
std::unique_ptr<sdbusplus::bus::match_t> modeChangeMatch;
static constexpr const char restrictionModeIntf[] =
"xyz.openbmc_project.Control.Security.RestrictionMode";
};
AllowlistFilter::AllowlistFilter()
{
bus = getSdBus();
lg2::info("Loading allowlist filter");
ipmi::registerFilter(ipmi::prioOpenBmcBase,
[this](ipmi::message::Request::ptr request) {
return filterMessage(request);
});
// wait until io->run is going to fetch RestrictionMode
post_work([this]() { postInit(); });
}
/** @brief Get RestrictionMode of the devices which has RestrictionMode support
* enabled
* @param[in] devices - vector of devices object path
* @returns void.
*/
void AllowlistFilter::cacheRestrictedMode(
const std::vector<std::string>& devices)
{
using namespace sdbusplus::server::xyz::openbmc_project::control::security;
std::string restrictionModeSetting;
std::string restrictionModeService;
for (auto& dev : devices)
{
try
{
restrictionModeSetting = dev;
restrictionModeService =
objects->service(restrictionModeSetting, restrictionModeIntf);
}
catch (const std::out_of_range& e)
{
lg2::error(
"Could not look up restriction mode interface from cache");
return;
}
std::string mode;
try
{
auto propValue = ipmi::getDbusProperty(
*bus, restrictionModeService, restrictionModeSetting,
restrictionModeIntf, "RestrictionMode");
mode = std::get<std::string>(propValue);
}
catch (const std::exception& e)
{
lg2::error("Error in RestrictionMode Get");
// Fail-safe to true.
size_t index = std::distance(&*std::begin(devices), &dev);
restrictedMode[index] = true;
}
auto restrictionMode = RestrictionMode::convertModesFromString(mode);
bool restrictMode =
(restrictionMode == RestrictionMode::Modes::Allowlist);
restrictedMode.emplace_back(restrictMode);
lg2::info("Set restrictedMode = {RESTRICTED_MODE}", "RESTRICTED_MODE",
restrictMode);
}
}
/** @brief Update RestrictionMode if any changes in RestrictionMode
* @param[in] m - sdbusplus message. Using this to get Updated Mode dbus path
* @param[in] deviceList - map to store devices path and their index
* @returns void.
*/
void AllowlistFilter::handleRestrictedModeChange(
sdbusplus::message_t& m, const std::map<std::string, size_t>& deviceList)
{
using namespace sdbusplus::server::xyz::openbmc_project::control::security;
std::string intf;
std::vector<std::pair<std::string, ipmi::Value>> propertyList;
m.read(intf, propertyList);
std::string path = m.get_path();
size_t hostId = 0;
auto it = deviceList.find(path);
if (it == deviceList.end())
{
lg2::error("Key not found in deviceList ");
}
else
{
hostId = it->second;
}
for (const auto& property : propertyList)
{
if (property.first == "RestrictionMode")
{
RestrictionMode::Modes restrictionMode =
RestrictionMode::convertModesFromString(
std::get<std::string>(property.second));
bool restrictMode =
(restrictionMode == RestrictionMode::Modes::Allowlist);
restrictedMode[hostId] = restrictMode;
lg2::info("Updated restrictedMode = {RESTRICTED_MODE}",
"RESTRICTED_MODE", restrictMode);
}
}
}
/** @brief Get and Update RestrictionModes of supported devices
* @param[in] void
* @returns void.
*/
void AllowlistFilter::postInit()
{
objects = std::make_unique<settings::Objects>(
*bus, std::vector<settings::Interface>({restrictionModeIntf}));
if (!objects)
{
lg2::error(
"Failed to create settings object; defaulting to restricted mode");
return;
}
std::vector<std::string> devices;
try
{
devices = objects->map.at(restrictionModeIntf);
}
catch (const std::out_of_range& e)
{
lg2::error("Could not look up restriction mode interface from cache");
return;
}
// Initialize restricted mode
cacheRestrictedMode(devices);
// Wait for changes on Restricted mode
std::map<std::string, size_t> deviceList;
for (size_t index = 0; index < devices.size(); index++)
{
deviceList.emplace(devices[index], index);
}
std::string filterStr;
std::string devicesDbusPath{"/xyz/openbmc_project/control"};
filterStr = sdbusplus::bus::match::rules::propertiesChangedNamespace(
devicesDbusPath, restrictionModeIntf);
modeChangeMatch = std::make_unique<sdbusplus::bus::match_t>(
*bus, filterStr, [this, deviceList](sdbusplus::message_t& m) {
handleRestrictedModeChange(m, deviceList);
});
}
/** @brief Filter IPMI messages with RestrictedMode
* @param[in] request - IPMI messahe request
* @returns IPMI completion code success or error.
*/
ipmi::Cc AllowlistFilter::filterMessage(ipmi::message::Request::ptr request)
{
/* Getting hostIdx for all IPMI devices like hosts, debugcard and other
devices from ipmi::message::Request and call postInit() to get the
restriction mode for all the IPMI commands */
size_t hostIdx = request->ctx->hostIdx;
if (request->ctx->channel == ipmi::channelSystemIface &&
restrictedMode[hostIdx])
{
if (!std::binary_search(
allowlist.cbegin(), allowlist.cend(),
std::make_pair(request->ctx->netFn, request->ctx->cmd)))
{
lg2::error("Net function not allowlisted, "
"NetFn: {NETFN}, Cmd: {CMD}",
"NETFN", lg2::hex, request->ctx->netFn, "CMD", lg2::hex,
request->ctx->cmd);
return ipmi::ccInsufficientPrivilege;
}
}
return ipmi::ccSuccess;
}
// instantiate the AllowlistFilter when this shared object is loaded
AllowlistFilter allowlistFilter;
} // namespace
} // namespace ipmi