Vernon Mauery | 0d0cd16 | 2020-01-31 10:04:10 -0800 | [diff] [blame] | 1 | #include <ipmi-whitelist.hpp> |
| 2 | #include <ipmid/api.hpp> |
| 3 | #include <ipmid/utils.hpp> |
| 4 | #include <phosphor-logging/elog-errors.hpp> |
| 5 | #include <phosphor-logging/log.hpp> |
| 6 | #include <xyz/openbmc_project/Control/Security/RestrictionMode/server.hpp> |
| 7 | |
James Feist | fcd2d3a | 2020-05-28 10:38:15 -0700 | [diff] [blame] | 8 | #include <algorithm> |
| 9 | #include <array> |
| 10 | |
Vernon Mauery | 0d0cd16 | 2020-01-31 10:04:10 -0800 | [diff] [blame] | 11 | using namespace phosphor::logging; |
| 12 | using namespace sdbusplus::xyz::openbmc_project::Common::Error; |
| 13 | using namespace sdbusplus::xyz::openbmc_project::Control::Security::server; |
| 14 | |
| 15 | namespace ipmi |
| 16 | { |
| 17 | |
| 18 | // put the filter provider in an unnamed namespace |
| 19 | namespace |
| 20 | { |
| 21 | |
| 22 | /** @class WhitelistFilter |
| 23 | * |
| 24 | * Class that implements an IPMI message filter based |
| 25 | * on incoming interface and a restriction mode setting |
| 26 | */ |
| 27 | class WhitelistFilter |
| 28 | { |
| 29 | |
| 30 | public: |
| 31 | WhitelistFilter(); |
| 32 | ~WhitelistFilter() = default; |
| 33 | WhitelistFilter(WhitelistFilter const&) = delete; |
| 34 | WhitelistFilter(WhitelistFilter&&) = delete; |
| 35 | WhitelistFilter& operator=(WhitelistFilter const&) = delete; |
| 36 | WhitelistFilter& operator=(WhitelistFilter&&) = delete; |
| 37 | |
| 38 | private: |
| 39 | void postInit(); |
| 40 | void cacheRestrictedAndPostCompleteMode(); |
Patrick Williams | f944d2e | 2022-07-22 19:26:52 -0500 | [diff] [blame] | 41 | void handleRestrictedModeChange(sdbusplus::message_t& m); |
| 42 | void handlePostCompleteChange(sdbusplus::message_t& m); |
Vernon Mauery | 0d0cd16 | 2020-01-31 10:04:10 -0800 | [diff] [blame] | 43 | void updatePostComplete(const std::string& value); |
| 44 | void updateRestrictionMode(const std::string& value); |
| 45 | ipmi::Cc filterMessage(ipmi::message::Request::ptr request); |
Patrick Williams | f944d2e | 2022-07-22 19:26:52 -0500 | [diff] [blame] | 46 | void handleCoreBiosDoneChange(sdbusplus::message_t& m); |
Arun P. Mohanan | a4cea1a | 2020-03-18 16:01:57 +0530 | [diff] [blame] | 47 | void cacheCoreBiosDone(); |
Vernon Mauery | 0d0cd16 | 2020-01-31 10:04:10 -0800 | [diff] [blame] | 48 | |
| 49 | // the BMC KCS Policy Control Modes document uses different names |
| 50 | // than the RestrictionModes D-Bus interface; use aliases |
| 51 | static constexpr RestrictionMode::Modes restrictionModeAllowAll = |
| 52 | RestrictionMode::Modes::Provisioning; |
| 53 | static constexpr RestrictionMode::Modes restrictionModeRestricted = |
| 54 | RestrictionMode::Modes::ProvisionedHostWhitelist; |
| 55 | static constexpr RestrictionMode::Modes restrictionModeDenyAll = |
| 56 | RestrictionMode::Modes::ProvisionedHostDisabled; |
| 57 | |
| 58 | RestrictionMode::Modes restrictionMode = restrictionModeRestricted; |
Arun P. Mohanan | a4cea1a | 2020-03-18 16:01:57 +0530 | [diff] [blame] | 59 | bool postCompleted = true; |
| 60 | bool coreBIOSDone = true; |
arun-pm | 849c319 | 2020-02-12 18:10:32 +0530 | [diff] [blame] | 61 | int channelSMM = -1; |
Vernon Mauery | 0d0cd16 | 2020-01-31 10:04:10 -0800 | [diff] [blame] | 62 | std::shared_ptr<sdbusplus::asio::connection> bus; |
Patrick Williams | f944d2e | 2022-07-22 19:26:52 -0500 | [diff] [blame] | 63 | std::unique_ptr<sdbusplus::bus::match_t> modeChangeMatch; |
| 64 | std::unique_ptr<sdbusplus::bus::match_t> modeIntfAddedMatch; |
| 65 | std::unique_ptr<sdbusplus::bus::match_t> postCompleteMatch; |
| 66 | std::unique_ptr<sdbusplus::bus::match_t> postCompleteIntfAddedMatch; |
| 67 | std::unique_ptr<sdbusplus::bus::match_t> platStateChangeMatch; |
| 68 | std::unique_ptr<sdbusplus::bus::match_t> platStateIntfAddedMatch; |
Vernon Mauery | 0d0cd16 | 2020-01-31 10:04:10 -0800 | [diff] [blame] | 69 | |
| 70 | static constexpr const char restrictionModeIntf[] = |
| 71 | "xyz.openbmc_project.Control.Security.RestrictionMode"; |
| 72 | static constexpr const char* systemOsStatusIntf = |
| 73 | "xyz.openbmc_project.State.OperatingSystem.Status"; |
Arun P. Mohanan | a4cea1a | 2020-03-18 16:01:57 +0530 | [diff] [blame] | 74 | static constexpr const char* hostMiscIntf = |
| 75 | "xyz.openbmc_project.State.Host.Misc"; |
Arun P. Mohanan | 31c45f6 | 2021-10-18 21:52:39 +0530 | [diff] [blame] | 76 | static constexpr const char* restrictionModePath = |
| 77 | "/xyz/openbmc_project/control/security/restriction_mode"; |
| 78 | static constexpr const char* systemOsStatusPath = |
| 79 | "/xyz/openbmc_project/state/os"; |
Vernon Mauery | 0d0cd16 | 2020-01-31 10:04:10 -0800 | [diff] [blame] | 80 | }; |
| 81 | |
arun-pm | 849c319 | 2020-02-12 18:10:32 +0530 | [diff] [blame] | 82 | static inline uint8_t getSMMChannel() |
| 83 | { |
| 84 | ipmi::ChannelInfo chInfo; |
| 85 | |
| 86 | for (int channel = 0; channel < ipmi::maxIpmiChannels; channel++) |
| 87 | { |
| 88 | if (ipmi::getChannelInfo(channel, chInfo) != ipmi::ccSuccess) |
| 89 | { |
| 90 | continue; |
| 91 | } |
| 92 | |
| 93 | if (static_cast<ipmi::EChannelMediumType>(chInfo.mediumType) == |
| 94 | ipmi::EChannelMediumType::systemInterface && |
| 95 | channel != ipmi::channelSystemIface) |
| 96 | { |
| 97 | log<level::INFO>("SMM channel number", |
| 98 | entry("CHANNEL=%d", channel)); |
| 99 | return channel; |
| 100 | } |
| 101 | } |
| 102 | log<level::ERR>("Unable to find SMM Channel Info"); |
| 103 | return -1; |
| 104 | } |
| 105 | |
Vernon Mauery | 0d0cd16 | 2020-01-31 10:04:10 -0800 | [diff] [blame] | 106 | WhitelistFilter::WhitelistFilter() |
| 107 | { |
| 108 | bus = getSdBus(); |
| 109 | |
| 110 | log<level::INFO>("Loading whitelist filter"); |
| 111 | |
| 112 | ipmi::registerFilter(ipmi::prioOpenBmcBase, |
| 113 | [this](ipmi::message::Request::ptr request) { |
| 114 | return filterMessage(request); |
| 115 | }); |
| 116 | |
arun-pm | 849c319 | 2020-02-12 18:10:32 +0530 | [diff] [blame] | 117 | channelSMM = getSMMChannel(); |
Vernon Mauery | 0d0cd16 | 2020-01-31 10:04:10 -0800 | [diff] [blame] | 118 | // wait until io->run is going to fetch RestrictionMode |
| 119 | post_work([this]() { postInit(); }); |
| 120 | } |
| 121 | |
| 122 | void WhitelistFilter::cacheRestrictedAndPostCompleteMode() |
| 123 | { |
Vernon Mauery | 0d0cd16 | 2020-01-31 10:04:10 -0800 | [diff] [blame] | 124 | try |
| 125 | { |
Arun P. Mohanan | 31c45f6 | 2021-10-18 21:52:39 +0530 | [diff] [blame] | 126 | auto service = |
| 127 | ipmi::getService(*bus, restrictionModeIntf, restrictionModePath); |
| 128 | ipmi::Value v = |
| 129 | ipmi::getDbusProperty(*bus, service, restrictionModePath, |
| 130 | restrictionModeIntf, "RestrictionMode"); |
| 131 | auto& mode = std::get<std::string>(v); |
| 132 | restrictionMode = RestrictionMode::convertModesFromString(mode); |
| 133 | log<level::INFO>("Read restriction mode", |
| 134 | entry("VALUE=%d", static_cast<int>(restrictionMode))); |
Vernon Mauery | 0d0cd16 | 2020-01-31 10:04:10 -0800 | [diff] [blame] | 135 | } |
| 136 | catch (const std::exception&) |
| 137 | { |
Arun P. Mohanan | 31c45f6 | 2021-10-18 21:52:39 +0530 | [diff] [blame] | 138 | log<level::ERR>("Could not initialize provisioning mode, " |
| 139 | "defaulting to restricted", |
| 140 | entry("VALUE=%d", static_cast<int>(restrictionMode))); |
Vernon Mauery | 0d0cd16 | 2020-01-31 10:04:10 -0800 | [diff] [blame] | 141 | } |
| 142 | |
Arun P. Mohanan | 31c45f6 | 2021-10-18 21:52:39 +0530 | [diff] [blame] | 143 | try |
| 144 | { |
| 145 | auto service = |
| 146 | ipmi::getService(*bus, systemOsStatusIntf, systemOsStatusPath); |
| 147 | ipmi::Value v = |
| 148 | ipmi::getDbusProperty(*bus, service, systemOsStatusPath, |
| 149 | systemOsStatusIntf, "OperatingSystemState"); |
| 150 | auto& value = std::get<std::string>(v); |
Andrei Kartashev | bc9a82d | 2021-12-27 17:21:50 +0300 | [diff] [blame] | 151 | updatePostComplete(value); |
Arun P. Mohanan | 31c45f6 | 2021-10-18 21:52:39 +0530 | [diff] [blame] | 152 | log<level::INFO>("Read POST complete value", |
| 153 | entry("VALUE=%d", postCompleted)); |
| 154 | } |
| 155 | catch (const std::exception&) |
| 156 | { |
| 157 | log<level::ERR>("Error in OperatingSystemState Get"); |
| 158 | postCompleted = true; |
| 159 | } |
Vernon Mauery | 0d0cd16 | 2020-01-31 10:04:10 -0800 | [diff] [blame] | 160 | } |
| 161 | |
| 162 | void WhitelistFilter::updateRestrictionMode(const std::string& value) |
| 163 | { |
| 164 | restrictionMode = RestrictionMode::convertModesFromString(value); |
| 165 | log<level::INFO>("Updated restriction mode", |
| 166 | entry("VALUE=%d", static_cast<int>(restrictionMode))); |
| 167 | } |
| 168 | |
Patrick Williams | f944d2e | 2022-07-22 19:26:52 -0500 | [diff] [blame] | 169 | void WhitelistFilter::handleRestrictedModeChange(sdbusplus::message_t& m) |
Vernon Mauery | 0d0cd16 | 2020-01-31 10:04:10 -0800 | [diff] [blame] | 170 | { |
| 171 | std::string signal = m.get_member(); |
| 172 | if (signal == "PropertiesChanged") |
| 173 | { |
| 174 | std::string intf; |
| 175 | std::vector<std::pair<std::string, ipmi::Value>> propertyList; |
| 176 | m.read(intf, propertyList); |
| 177 | for (const auto& property : propertyList) |
| 178 | { |
| 179 | if (property.first == "RestrictionMode") |
| 180 | { |
| 181 | updateRestrictionMode(std::get<std::string>(property.second)); |
| 182 | } |
| 183 | } |
| 184 | } |
| 185 | else if (signal == "InterfacesAdded") |
| 186 | { |
| 187 | sdbusplus::message::object_path path; |
| 188 | DbusInterfaceMap restModeObj; |
| 189 | m.read(path, restModeObj); |
| 190 | auto intfItr = restModeObj.find(restrictionModeIntf); |
| 191 | if (intfItr == restModeObj.end()) |
| 192 | { |
| 193 | return; |
| 194 | } |
| 195 | PropertyMap& propertyList = intfItr->second; |
| 196 | auto itr = propertyList.find("RestrictionMode"); |
| 197 | if (itr == propertyList.end()) |
| 198 | { |
| 199 | return; |
| 200 | } |
| 201 | updateRestrictionMode(std::get<std::string>(itr->second)); |
| 202 | } |
| 203 | } |
| 204 | |
| 205 | void WhitelistFilter::updatePostComplete(const std::string& value) |
| 206 | { |
Andrei Kartashev | bc9a82d | 2021-12-27 17:21:50 +0300 | [diff] [blame] | 207 | // The short string "Standby" is deprecated in favor of the full enum string |
| 208 | // Support for the short string will be removed in the future. |
| 209 | postCompleted = (value == "Standby") || |
| 210 | (value == "xyz.openbmc_project.State.OperatingSystem." |
| 211 | "Status.OSStatus.Standby"); |
Vernon Mauery | 0d0cd16 | 2020-01-31 10:04:10 -0800 | [diff] [blame] | 212 | log<level::INFO>(postCompleted ? "Updated to POST Complete" |
| 213 | : "Updated to !POST Complete"); |
| 214 | } |
| 215 | |
Patrick Williams | f944d2e | 2022-07-22 19:26:52 -0500 | [diff] [blame] | 216 | void WhitelistFilter::handlePostCompleteChange(sdbusplus::message_t& m) |
Vernon Mauery | 0d0cd16 | 2020-01-31 10:04:10 -0800 | [diff] [blame] | 217 | { |
| 218 | std::string signal = m.get_member(); |
| 219 | if (signal == "PropertiesChanged") |
| 220 | { |
| 221 | std::string intf; |
| 222 | std::vector<std::pair<std::string, ipmi::Value>> propertyList; |
| 223 | m.read(intf, propertyList); |
| 224 | for (const auto& property : propertyList) |
| 225 | { |
| 226 | if (property.first == "OperatingSystemState") |
| 227 | { |
| 228 | updatePostComplete(std::get<std::string>(property.second)); |
| 229 | } |
| 230 | } |
| 231 | } |
| 232 | else if (signal == "InterfacesAdded") |
| 233 | { |
| 234 | sdbusplus::message::object_path path; |
| 235 | DbusInterfaceMap postCompleteObj; |
| 236 | m.read(path, postCompleteObj); |
| 237 | auto intfItr = postCompleteObj.find(systemOsStatusIntf); |
| 238 | if (intfItr == postCompleteObj.end()) |
| 239 | { |
| 240 | return; |
| 241 | } |
| 242 | PropertyMap& propertyList = intfItr->second; |
| 243 | auto itr = propertyList.find("OperatingSystemState"); |
| 244 | if (itr == propertyList.end()) |
| 245 | { |
| 246 | return; |
| 247 | } |
| 248 | updatePostComplete(std::get<std::string>(itr->second)); |
| 249 | } |
| 250 | } |
Arun P. Mohanan | a4cea1a | 2020-03-18 16:01:57 +0530 | [diff] [blame] | 251 | |
| 252 | void WhitelistFilter::cacheCoreBiosDone() |
| 253 | { |
| 254 | std::string coreBiosDonePath; |
| 255 | std::string coreBiosDoneService; |
| 256 | try |
| 257 | { |
| 258 | ipmi::DbusObjectInfo coreBiosDoneObj = |
| 259 | ipmi::getDbusObject(*bus, hostMiscIntf); |
| 260 | |
| 261 | coreBiosDonePath = coreBiosDoneObj.first; |
| 262 | coreBiosDoneService = coreBiosDoneObj.second; |
| 263 | } |
| 264 | catch (const std::exception&) |
| 265 | { |
| 266 | log<level::ERR>("Could not initialize CoreBiosDone, " |
| 267 | "coreBIOSDone asserted as default"); |
| 268 | return; |
| 269 | } |
| 270 | |
| 271 | bus->async_method_call( |
| 272 | [this](boost::system::error_code ec, const ipmi::Value& v) { |
| 273 | if (ec) |
| 274 | { |
| 275 | log<level::ERR>( |
| 276 | "async call failed, coreBIOSDone asserted as default"); |
| 277 | return; |
| 278 | } |
| 279 | coreBIOSDone = std::get<bool>(v); |
| 280 | log<level::INFO>("Read CoreBiosDone", |
| 281 | entry("VALUE=%d", static_cast<int>(coreBIOSDone))); |
| 282 | }, |
| 283 | coreBiosDoneService, coreBiosDonePath, |
| 284 | "org.freedesktop.DBus.Properties", "Get", hostMiscIntf, "CoreBiosDone"); |
| 285 | } |
| 286 | |
Patrick Williams | f944d2e | 2022-07-22 19:26:52 -0500 | [diff] [blame] | 287 | void WhitelistFilter::handleCoreBiosDoneChange(sdbusplus::message_t& msg) |
Arun P. Mohanan | a4cea1a | 2020-03-18 16:01:57 +0530 | [diff] [blame] | 288 | { |
| 289 | std::string signal = msg.get_member(); |
| 290 | if (signal == "PropertiesChanged") |
| 291 | { |
| 292 | std::string intf; |
| 293 | std::vector<std::pair<std::string, ipmi::Value>> propertyList; |
| 294 | msg.read(intf, propertyList); |
| 295 | auto it = |
| 296 | std::find_if(propertyList.begin(), propertyList.end(), |
| 297 | [](const std::pair<std::string, ipmi::Value>& prop) { |
| 298 | return prop.first == "CoreBiosDone"; |
| 299 | }); |
| 300 | |
| 301 | if (it != propertyList.end()) |
| 302 | { |
| 303 | coreBIOSDone = std::get<bool>(it->second); |
| 304 | log<level::INFO>(coreBIOSDone ? "coreBIOSDone asserted" |
| 305 | : "coreBIOSDone not asserted"); |
| 306 | } |
| 307 | } |
| 308 | else if (signal == "InterfacesAdded") |
| 309 | { |
| 310 | sdbusplus::message::object_path path; |
| 311 | DbusInterfaceMap eSpiresetObj; |
| 312 | msg.read(path, eSpiresetObj); |
| 313 | auto intfItr = eSpiresetObj.find(hostMiscIntf); |
| 314 | if (intfItr == eSpiresetObj.end()) |
| 315 | { |
| 316 | return; |
| 317 | } |
| 318 | PropertyMap& propertyList = intfItr->second; |
| 319 | auto itr = propertyList.find("CoreBiosDone"); |
| 320 | if (itr == propertyList.end()) |
| 321 | { |
| 322 | return; |
| 323 | } |
| 324 | coreBIOSDone = std::get<bool>(itr->second); |
| 325 | log<level::INFO>(coreBIOSDone ? "coreBIOSDone asserted" |
| 326 | : "coreBIOSDone not asserted"); |
| 327 | } |
| 328 | } |
| 329 | |
Vernon Mauery | 0d0cd16 | 2020-01-31 10:04:10 -0800 | [diff] [blame] | 330 | void WhitelistFilter::postInit() |
| 331 | { |
| 332 | // Wait for changes on Restricted mode |
| 333 | namespace rules = sdbusplus::bus::match::rules; |
| 334 | const std::string filterStrModeChange = |
| 335 | rules::type::signal() + rules::member("PropertiesChanged") + |
| 336 | rules::interface("org.freedesktop.DBus.Properties") + |
| 337 | rules::argN(0, restrictionModeIntf); |
| 338 | |
| 339 | const std::string filterStrModeIntfAdd = |
| 340 | rules::interfacesAdded() + |
| 341 | rules::argNpath( |
| 342 | 0, "/xyz/openbmc_project/control/security/restriction_mode"); |
| 343 | |
| 344 | const std::string filterStrPostComplete = |
| 345 | rules::type::signal() + rules::member("PropertiesChanged") + |
| 346 | rules::interface("org.freedesktop.DBus.Properties") + |
| 347 | rules::argN(0, systemOsStatusIntf); |
| 348 | |
| 349 | const std::string filterStrPostIntfAdd = |
| 350 | rules::interfacesAdded() + |
| 351 | rules::argNpath(0, "/xyz/openbmc_project/state/os"); |
| 352 | |
Arun P. Mohanan | a4cea1a | 2020-03-18 16:01:57 +0530 | [diff] [blame] | 353 | const std::string filterStrPlatStateChange = |
| 354 | rules::type::signal() + rules::member("PropertiesChanged") + |
| 355 | rules::interface("org.freedesktop.DBus.Properties") + |
| 356 | rules::argN(0, hostMiscIntf); |
| 357 | |
| 358 | const std::string filterStrPlatStateIntfAdd = |
| 359 | rules::interfacesAdded() + |
| 360 | rules::argNpath(0, "/xyz/openbmc_project/misc/platform_state"); |
| 361 | |
Patrick Williams | f944d2e | 2022-07-22 19:26:52 -0500 | [diff] [blame] | 362 | modeChangeMatch = std::make_unique<sdbusplus::bus::match_t>( |
| 363 | *bus, filterStrModeChange, |
| 364 | [this](sdbusplus::message_t& m) { handleRestrictedModeChange(m); }); |
| 365 | modeIntfAddedMatch = std::make_unique<sdbusplus::bus::match_t>( |
| 366 | *bus, filterStrModeIntfAdd, |
| 367 | [this](sdbusplus::message_t& m) { handleRestrictedModeChange(m); }); |
Vernon Mauery | 0d0cd16 | 2020-01-31 10:04:10 -0800 | [diff] [blame] | 368 | |
Patrick Williams | f944d2e | 2022-07-22 19:26:52 -0500 | [diff] [blame] | 369 | postCompleteMatch = std::make_unique<sdbusplus::bus::match_t>( |
| 370 | *bus, filterStrPostComplete, |
| 371 | [this](sdbusplus::message_t& m) { handlePostCompleteChange(m); }); |
Vernon Mauery | 0d0cd16 | 2020-01-31 10:04:10 -0800 | [diff] [blame] | 372 | |
Patrick Williams | f944d2e | 2022-07-22 19:26:52 -0500 | [diff] [blame] | 373 | postCompleteIntfAddedMatch = std::make_unique<sdbusplus::bus::match_t>( |
| 374 | *bus, filterStrPostIntfAdd, |
| 375 | [this](sdbusplus::message_t& m) { handlePostCompleteChange(m); }); |
Vernon Mauery | 0d0cd16 | 2020-01-31 10:04:10 -0800 | [diff] [blame] | 376 | |
Patrick Williams | f944d2e | 2022-07-22 19:26:52 -0500 | [diff] [blame] | 377 | platStateChangeMatch = std::make_unique<sdbusplus::bus::match_t>( |
| 378 | *bus, filterStrPlatStateChange, |
| 379 | [this](sdbusplus::message_t& m) { handleCoreBiosDoneChange(m); }); |
Arun P. Mohanan | a4cea1a | 2020-03-18 16:01:57 +0530 | [diff] [blame] | 380 | |
Patrick Williams | f944d2e | 2022-07-22 19:26:52 -0500 | [diff] [blame] | 381 | platStateIntfAddedMatch = std::make_unique<sdbusplus::bus::match_t>( |
Arun P. Mohanan | a4cea1a | 2020-03-18 16:01:57 +0530 | [diff] [blame] | 382 | *bus, filterStrPlatStateIntfAdd, |
Patrick Williams | f944d2e | 2022-07-22 19:26:52 -0500 | [diff] [blame] | 383 | [this](sdbusplus::message_t& m) { handleCoreBiosDoneChange(m); }); |
Arun P. Mohanan | a4cea1a | 2020-03-18 16:01:57 +0530 | [diff] [blame] | 384 | |
Vernon Mauery | 0d0cd16 | 2020-01-31 10:04:10 -0800 | [diff] [blame] | 385 | // Initialize restricted mode |
| 386 | cacheRestrictedAndPostCompleteMode(); |
Arun P. Mohanan | a4cea1a | 2020-03-18 16:01:57 +0530 | [diff] [blame] | 387 | // Initialize CoreBiosDone |
| 388 | cacheCoreBiosDone(); |
Vernon Mauery | 0d0cd16 | 2020-01-31 10:04:10 -0800 | [diff] [blame] | 389 | } |
| 390 | |
| 391 | ipmi::Cc WhitelistFilter::filterMessage(ipmi::message::Request::ptr request) |
| 392 | { |
| 393 | auto channelMask = static_cast<unsigned short>(1 << request->ctx->channel); |
| 394 | bool whitelisted = std::binary_search( |
| 395 | whitelist.cbegin(), whitelist.cend(), |
| 396 | std::make_tuple(request->ctx->netFn, request->ctx->cmd, channelMask), |
| 397 | [](const netfncmd_tuple& first, const netfncmd_tuple& value) { |
| 398 | return (std::get<2>(first) & std::get<2>(value)) |
| 399 | ? first < std::make_tuple(std::get<0>(value), |
| 400 | std::get<1>(value), |
| 401 | std::get<2>(first)) |
| 402 | : first < value; |
| 403 | }); |
| 404 | |
| 405 | // no special handling for non-system-interface channels |
arun-pm | 849c319 | 2020-02-12 18:10:32 +0530 | [diff] [blame] | 406 | if (!(request->ctx->channel == ipmi::channelSystemIface || |
| 407 | request->ctx->channel == channelSMM)) |
Vernon Mauery | 0d0cd16 | 2020-01-31 10:04:10 -0800 | [diff] [blame] | 408 | { |
| 409 | if (!whitelisted) |
| 410 | { |
| 411 | log<level::INFO>("Channel/NetFn/Cmd not whitelisted", |
| 412 | entry("CHANNEL=0x%X", request->ctx->channel), |
| 413 | entry("NETFN=0x%X", int(request->ctx->netFn)), |
| 414 | entry("CMD=0x%X", int(request->ctx->cmd))); |
Snehalatha V | 65b66ad | 2020-07-09 14:58:39 +0000 | [diff] [blame] | 415 | return ipmi::ccInsufficientPrivilege; |
Vernon Mauery | 0d0cd16 | 2020-01-31 10:04:10 -0800 | [diff] [blame] | 416 | } |
| 417 | return ipmi::ccSuccess; |
| 418 | } |
| 419 | |
| 420 | // for system interface, filtering is done as follows: |
| 421 | // Allow All: preboot ? ccSuccess : ccSuccess |
| 422 | // Restricted: preboot ? ccSuccess : |
Snehalatha V | 65b66ad | 2020-07-09 14:58:39 +0000 | [diff] [blame] | 423 | // ( whitelist ? ccSuccess : ccInsufficientPrivilege ) |
| 424 | // Deny All: preboot ? ccSuccess : ccInsufficientPrivilege |
Vernon Mauery | 0d0cd16 | 2020-01-31 10:04:10 -0800 | [diff] [blame] | 425 | |
Arun P. Mohanan | a4cea1a | 2020-03-18 16:01:57 +0530 | [diff] [blame] | 426 | if (!(postCompleted || coreBIOSDone)) |
Vernon Mauery | 0d0cd16 | 2020-01-31 10:04:10 -0800 | [diff] [blame] | 427 | { |
Arun P. Mohanan | a4cea1a | 2020-03-18 16:01:57 +0530 | [diff] [blame] | 428 | // Allow all commands, till POST or CoreBiosDone is completed |
Vernon Mauery | 0d0cd16 | 2020-01-31 10:04:10 -0800 | [diff] [blame] | 429 | return ipmi::ccSuccess; |
| 430 | } |
| 431 | |
| 432 | switch (restrictionMode) |
| 433 | { |
| 434 | case RestrictionMode::Modes::None: |
| 435 | case restrictionModeAllowAll: |
| 436 | { |
| 437 | // Allow All |
| 438 | return ipmi::ccSuccess; |
| 439 | break; |
| 440 | } |
| 441 | case restrictionModeRestricted: |
| 442 | { |
| 443 | // Restricted - follow whitelist |
| 444 | break; |
| 445 | } |
| 446 | case restrictionModeDenyAll: |
| 447 | { |
| 448 | // Deny All |
| 449 | whitelisted = false; |
| 450 | break; |
| 451 | } |
| 452 | default: // for whitelist and blacklist |
Snehalatha V | 65b66ad | 2020-07-09 14:58:39 +0000 | [diff] [blame] | 453 | return ipmi::ccInsufficientPrivilege; |
Vernon Mauery | 0d0cd16 | 2020-01-31 10:04:10 -0800 | [diff] [blame] | 454 | } |
| 455 | |
| 456 | if (!whitelisted) |
| 457 | { |
| 458 | log<level::INFO>("Channel/NetFn/Cmd not whitelisted", |
| 459 | entry("CHANNEL=0x%X", request->ctx->channel), |
| 460 | entry("NETFN=0x%X", int(request->ctx->netFn)), |
| 461 | entry("CMD=0x%X", int(request->ctx->cmd))); |
Snehalatha V | 65b66ad | 2020-07-09 14:58:39 +0000 | [diff] [blame] | 462 | return ipmi::ccInsufficientPrivilege; |
Vernon Mauery | 0d0cd16 | 2020-01-31 10:04:10 -0800 | [diff] [blame] | 463 | } |
| 464 | return ipmi::ccSuccess; |
| 465 | } // namespace |
| 466 | |
| 467 | // instantiate the WhitelistFilter when this shared object is loaded |
| 468 | WhitelistFilter whitelistFilter; |
| 469 | |
| 470 | } // namespace |
| 471 | |
| 472 | } // namespace ipmi |