#include #include #include #include #include #include #include #include using namespace phosphor::logging; using namespace sdbusplus::xyz::openbmc_project::Common::Error; using namespace sdbusplus::xyz::openbmc_project::Control::Security::server; 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(AllowlistFilter const&) = delete; AllowlistFilter(AllowlistFilter&&) = delete; AllowlistFilter& operator=(AllowlistFilter const&) = delete; AllowlistFilter& operator=(AllowlistFilter&&) = delete; private: void postInit(); void cacheRestrictedAndPostCompleteMode(); void handleRestrictedModeChange(sdbusplus::message_t& m); void handlePostCompleteChange(sdbusplus::message_t& m); void updatePostComplete(const std::string& value); void updateRestrictionMode(const std::string& value); ipmi::Cc filterMessage(ipmi::message::Request::ptr request); void handleCoreBiosDoneChange(sdbusplus::message_t& m); void cacheCoreBiosDone(); // the BMC KCS Policy Control Modes document uses different names // than the RestrictionModes D-Bus interface; use aliases static constexpr RestrictionMode::Modes restrictionModeAllowAll = RestrictionMode::Modes::Provisioning; static constexpr RestrictionMode::Modes restrictionModeRestricted = RestrictionMode::Modes::ProvisionedHostWhitelist; static constexpr RestrictionMode::Modes restrictionModeDenyAll = RestrictionMode::Modes::ProvisionedHostDisabled; RestrictionMode::Modes restrictionMode = restrictionModeRestricted; bool postCompleted = true; bool coreBIOSDone = true; int channelSMM = -1; std::shared_ptr bus; std::unique_ptr modeChangeMatch; std::unique_ptr modeIntfAddedMatch; std::unique_ptr postCompleteMatch; std::unique_ptr postCompleteIntfAddedMatch; std::unique_ptr platStateChangeMatch; std::unique_ptr platStateIntfAddedMatch; static constexpr const char restrictionModeIntf[] = "xyz.openbmc_project.Control.Security.RestrictionMode"; static constexpr const char* systemOsStatusIntf = "xyz.openbmc_project.State.OperatingSystem.Status"; static constexpr const char* hostMiscIntf = "xyz.openbmc_project.State.Host.Misc"; static constexpr const char* restrictionModePath = "/xyz/openbmc_project/control/security/restriction_mode"; static constexpr const char* systemOsStatusPath = "/xyz/openbmc_project/state/os"; }; static inline uint8_t getSMMChannel() { ipmi::ChannelInfo chInfo; for (int channel = 0; channel < ipmi::maxIpmiChannels; channel++) { if (ipmi::getChannelInfo(channel, chInfo) != ipmi::ccSuccess) { continue; } if (static_cast(chInfo.mediumType) == ipmi::EChannelMediumType::systemInterface && channel != ipmi::channelSystemIface) { log("SMM channel number", entry("CHANNEL=%d", channel)); return channel; } } log("Unable to find SMM Channel Info"); return -1; } AllowlistFilter::AllowlistFilter() { bus = getSdBus(); log("Loading Allowlist filter"); ipmi::registerFilter(ipmi::prioOpenBmcBase, [this](ipmi::message::Request::ptr request) { return filterMessage(request); }); channelSMM = getSMMChannel(); // wait until io->run is going to fetch RestrictionMode post_work([this]() { postInit(); }); } void AllowlistFilter::cacheRestrictedAndPostCompleteMode() { try { auto service = ipmi::getService(*bus, restrictionModeIntf, restrictionModePath); ipmi::Value v = ipmi::getDbusProperty(*bus, service, restrictionModePath, restrictionModeIntf, "RestrictionMode"); auto& mode = std::get(v); restrictionMode = RestrictionMode::convertModesFromString(mode); log("Read restriction mode", entry("VALUE=%d", static_cast(restrictionMode))); } catch (const std::exception&) { log("Could not initialize provisioning mode, " "defaulting to restricted", entry("VALUE=%d", static_cast(restrictionMode))); } try { auto service = ipmi::getService(*bus, systemOsStatusIntf, systemOsStatusPath); ipmi::Value v = ipmi::getDbusProperty(*bus, service, systemOsStatusPath, systemOsStatusIntf, "OperatingSystemState"); auto& value = std::get(v); updatePostComplete(value); log("Read POST complete value", entry("VALUE=%d", postCompleted)); } catch (const std::exception&) { log("Error in OperatingSystemState Get"); postCompleted = true; } } void AllowlistFilter::updateRestrictionMode(const std::string& value) { restrictionMode = RestrictionMode::convertModesFromString(value); log("Updated restriction mode", entry("VALUE=%d", static_cast(restrictionMode))); } void AllowlistFilter::handleRestrictedModeChange(sdbusplus::message_t& m) { std::string signal = m.get_member(); if (signal == "PropertiesChanged") { std::string intf; std::vector> propertyList; m.read(intf, propertyList); for (const auto& property : propertyList) { if (property.first == "RestrictionMode") { updateRestrictionMode(std::get(property.second)); } } } else if (signal == "InterfacesAdded") { sdbusplus::message::object_path path; DbusInterfaceMap restModeObj; m.read(path, restModeObj); auto intfItr = restModeObj.find(restrictionModeIntf); if (intfItr == restModeObj.end()) { return; } PropertyMap& propertyList = intfItr->second; auto itr = propertyList.find("RestrictionMode"); if (itr == propertyList.end()) { return; } updateRestrictionMode(std::get(itr->second)); } } void AllowlistFilter::updatePostComplete(const std::string& value) { // The short string "Standby" is deprecated in favor of the full enum string // Support for the short string will be removed in the future. postCompleted = (value == "Standby") || (value == "xyz.openbmc_project.State.OperatingSystem." "Status.OSStatus.Standby"); log(postCompleted ? "Updated to POST Complete" : "Updated to !POST Complete"); } void AllowlistFilter::handlePostCompleteChange(sdbusplus::message_t& m) { std::string signal = m.get_member(); if (signal == "PropertiesChanged") { std::string intf; std::vector> propertyList; m.read(intf, propertyList); for (const auto& property : propertyList) { if (property.first == "OperatingSystemState") { updatePostComplete(std::get(property.second)); } } } else if (signal == "InterfacesAdded") { sdbusplus::message::object_path path; DbusInterfaceMap postCompleteObj; m.read(path, postCompleteObj); auto intfItr = postCompleteObj.find(systemOsStatusIntf); if (intfItr == postCompleteObj.end()) { return; } PropertyMap& propertyList = intfItr->second; auto itr = propertyList.find("OperatingSystemState"); if (itr == propertyList.end()) { return; } updatePostComplete(std::get(itr->second)); } } void AllowlistFilter::cacheCoreBiosDone() { std::string coreBiosDonePath; std::string coreBiosDoneService; try { ipmi::DbusObjectInfo coreBiosDoneObj = ipmi::getDbusObject(*bus, hostMiscIntf); coreBiosDonePath = coreBiosDoneObj.first; coreBiosDoneService = coreBiosDoneObj.second; } catch (const std::exception&) { log("Could not initialize CoreBiosDone, " "coreBIOSDone asserted as default"); return; } bus->async_method_call( [this](boost::system::error_code ec, const ipmi::Value& v) { if (ec) { log( "async call failed, coreBIOSDone asserted as default"); return; } coreBIOSDone = std::get(v); log("Read CoreBiosDone", entry("VALUE=%d", static_cast(coreBIOSDone))); }, coreBiosDoneService, coreBiosDonePath, "org.freedesktop.DBus.Properties", "Get", hostMiscIntf, "CoreBiosDone"); } void AllowlistFilter::handleCoreBiosDoneChange(sdbusplus::message_t& msg) { std::string signal = msg.get_member(); if (signal == "PropertiesChanged") { std::string intf; std::vector> propertyList; msg.read(intf, propertyList); auto it = std::find_if(propertyList.begin(), propertyList.end(), [](const std::pair& prop) { return prop.first == "CoreBiosDone"; }); if (it != propertyList.end()) { coreBIOSDone = std::get(it->second); log(coreBIOSDone ? "coreBIOSDone asserted" : "coreBIOSDone not asserted"); } } else if (signal == "InterfacesAdded") { sdbusplus::message::object_path path; DbusInterfaceMap eSpiresetObj; msg.read(path, eSpiresetObj); auto intfItr = eSpiresetObj.find(hostMiscIntf); if (intfItr == eSpiresetObj.end()) { return; } PropertyMap& propertyList = intfItr->second; auto itr = propertyList.find("CoreBiosDone"); if (itr == propertyList.end()) { return; } coreBIOSDone = std::get(itr->second); log(coreBIOSDone ? "coreBIOSDone asserted" : "coreBIOSDone not asserted"); } } void AllowlistFilter::postInit() { // Wait for changes on Restricted mode namespace rules = sdbusplus::bus::match::rules; const std::string filterStrModeChange = rules::type::signal() + rules::member("PropertiesChanged") + rules::interface("org.freedesktop.DBus.Properties") + rules::argN(0, restrictionModeIntf); const std::string filterStrModeIntfAdd = rules::interfacesAdded() + rules::argNpath( 0, "/xyz/openbmc_project/control/security/restriction_mode"); const std::string filterStrPostComplete = rules::type::signal() + rules::member("PropertiesChanged") + rules::interface("org.freedesktop.DBus.Properties") + rules::argN(0, systemOsStatusIntf); const std::string filterStrPostIntfAdd = rules::interfacesAdded() + rules::argNpath(0, "/xyz/openbmc_project/state/os"); const std::string filterStrPlatStateChange = rules::type::signal() + rules::member("PropertiesChanged") + rules::interface("org.freedesktop.DBus.Properties") + rules::argN(0, hostMiscIntf); const std::string filterStrPlatStateIntfAdd = rules::interfacesAdded() + rules::argNpath(0, "/xyz/openbmc_project/misc/platform_state"); modeChangeMatch = std::make_unique( *bus, filterStrModeChange, [this](sdbusplus::message_t& m) { handleRestrictedModeChange(m); }); modeIntfAddedMatch = std::make_unique( *bus, filterStrModeIntfAdd, [this](sdbusplus::message_t& m) { handleRestrictedModeChange(m); }); postCompleteMatch = std::make_unique( *bus, filterStrPostComplete, [this](sdbusplus::message_t& m) { handlePostCompleteChange(m); }); postCompleteIntfAddedMatch = std::make_unique( *bus, filterStrPostIntfAdd, [this](sdbusplus::message_t& m) { handlePostCompleteChange(m); }); platStateChangeMatch = std::make_unique( *bus, filterStrPlatStateChange, [this](sdbusplus::message_t& m) { handleCoreBiosDoneChange(m); }); platStateIntfAddedMatch = std::make_unique( *bus, filterStrPlatStateIntfAdd, [this](sdbusplus::message_t& m) { handleCoreBiosDoneChange(m); }); // Initialize restricted mode cacheRestrictedAndPostCompleteMode(); // Initialize CoreBiosDone cacheCoreBiosDone(); } ipmi::Cc AllowlistFilter::filterMessage(ipmi::message::Request::ptr request) { auto channelMask = static_cast(1 << request->ctx->channel); bool Allowlisted = std::binary_search( allowlist.cbegin(), allowlist.cend(), std::make_tuple(request->ctx->netFn, request->ctx->cmd, channelMask), [](const netfncmd_tuple& first, const netfncmd_tuple& value) { return (std::get<2>(first) & std::get<2>(value)) ? first < std::make_tuple(std::get<0>(value), std::get<1>(value), std::get<2>(first)) : first < value; }); // no special handling for non-system-interface channels if (!(request->ctx->channel == ipmi::channelSystemIface || request->ctx->channel == channelSMM)) { if (!Allowlisted) { log("Channel/NetFn/Cmd not Allowlisted", entry("CHANNEL=0x%X", request->ctx->channel), entry("NETFN=0x%X", int(request->ctx->netFn)), entry("CMD=0x%X", int(request->ctx->cmd))); return ipmi::ccInsufficientPrivilege; } return ipmi::ccSuccess; } // for system interface, filtering is done as follows: // Allow All: preboot ? ccSuccess : ccSuccess // Restricted: preboot ? ccSuccess : // ( Allowlist ? ccSuccess : ccInsufficientPrivilege ) // Deny All: preboot ? ccSuccess : ccInsufficientPrivilege if (!(postCompleted || coreBIOSDone)) { // Allow all commands, till POST or CoreBiosDone is completed return ipmi::ccSuccess; } switch (restrictionMode) { case RestrictionMode::Modes::None: case restrictionModeAllowAll: { // Allow All return ipmi::ccSuccess; break; } case restrictionModeRestricted: { // Restricted - follow Allowlist break; } case restrictionModeDenyAll: { // Deny All Allowlisted = false; break; } default: // for Allowlist and Blocklist return ipmi::ccInsufficientPrivilege; } if (!Allowlisted) { log("Channel/NetFn/Cmd not allowlisted", entry("CHANNEL=0x%X", request->ctx->channel), entry("NETFN=0x%X", int(request->ctx->netFn)), entry("CMD=0x%X", int(request->ctx->cmd))); return ipmi::ccInsufficientPrivilege; } return ipmi::ccSuccess; } // namespace // instantiate the AllowlistFilter when this shared object is loaded AllowlistFilter allowlistFilter; } // namespace } // namespace ipmi