18b7c156eSVernon Mauery #include <ipmi-allowlist.hpp>
28b7c156eSVernon Mauery #include <ipmid/api.hpp>
38b7c156eSVernon Mauery #include <ipmid/utils.hpp>
48b7c156eSVernon Mauery #include <phosphor-logging/elog-errors.hpp>
58b7c156eSVernon Mauery #include <phosphor-logging/log.hpp>
68b7c156eSVernon Mauery #include <xyz/openbmc_project/Control/Security/RestrictionMode/server.hpp>
78b7c156eSVernon Mauery 
88b7c156eSVernon Mauery #include <algorithm>
98b7c156eSVernon Mauery #include <array>
108b7c156eSVernon Mauery 
118b7c156eSVernon Mauery using namespace phosphor::logging;
128b7c156eSVernon Mauery using namespace sdbusplus::xyz::openbmc_project::Common::Error;
138b7c156eSVernon Mauery using namespace sdbusplus::xyz::openbmc_project::Control::Security::server;
148b7c156eSVernon Mauery 
158b7c156eSVernon Mauery namespace ipmi
168b7c156eSVernon Mauery {
178b7c156eSVernon Mauery 
188b7c156eSVernon Mauery // put the filter provider in an unnamed namespace
198b7c156eSVernon Mauery namespace
208b7c156eSVernon Mauery {
218b7c156eSVernon Mauery 
228b7c156eSVernon Mauery /** @class AllowlistFilter
238b7c156eSVernon Mauery  *
248b7c156eSVernon Mauery  * Class that implements an IPMI message filter based
258b7c156eSVernon Mauery  * on incoming interface and a restriction mode setting
268b7c156eSVernon Mauery  */
278b7c156eSVernon Mauery class AllowlistFilter
288b7c156eSVernon Mauery {
298b7c156eSVernon Mauery   public:
308b7c156eSVernon Mauery     AllowlistFilter();
318b7c156eSVernon Mauery     ~AllowlistFilter() = default;
32*b37abfb2SPatrick Williams     AllowlistFilter(const AllowlistFilter&) = delete;
338b7c156eSVernon Mauery     AllowlistFilter(AllowlistFilter&&) = delete;
34*b37abfb2SPatrick Williams     AllowlistFilter& operator=(const AllowlistFilter&) = delete;
358b7c156eSVernon Mauery     AllowlistFilter& operator=(AllowlistFilter&&) = delete;
368b7c156eSVernon Mauery 
378b7c156eSVernon Mauery   private:
388b7c156eSVernon Mauery     void postInit();
398b7c156eSVernon Mauery     void cacheRestrictedAndPostCompleteMode();
408b7c156eSVernon Mauery     void handleRestrictedModeChange(sdbusplus::message_t& m);
418b7c156eSVernon Mauery     void handlePostCompleteChange(sdbusplus::message_t& m);
428b7c156eSVernon Mauery     void updatePostComplete(const std::string& value);
438b7c156eSVernon Mauery     void updateRestrictionMode(const std::string& value);
448b7c156eSVernon Mauery     ipmi::Cc filterMessage(ipmi::message::Request::ptr request);
458b7c156eSVernon Mauery     void handleCoreBiosDoneChange(sdbusplus::message_t& m);
468b7c156eSVernon Mauery     void cacheCoreBiosDone();
478b7c156eSVernon Mauery 
488b7c156eSVernon Mauery     // the BMC KCS Policy Control Modes document uses different names
498b7c156eSVernon Mauery     // than the RestrictionModes D-Bus interface; use aliases
508b7c156eSVernon Mauery     static constexpr RestrictionMode::Modes restrictionModeAllowAll =
518b7c156eSVernon Mauery         RestrictionMode::Modes::Provisioning;
528b7c156eSVernon Mauery     static constexpr RestrictionMode::Modes restrictionModeRestricted =
538b7c156eSVernon Mauery         RestrictionMode::Modes::ProvisionedHostWhitelist;
548b7c156eSVernon Mauery     static constexpr RestrictionMode::Modes restrictionModeDenyAll =
558b7c156eSVernon Mauery         RestrictionMode::Modes::ProvisionedHostDisabled;
568b7c156eSVernon Mauery 
578b7c156eSVernon Mauery     RestrictionMode::Modes restrictionMode = restrictionModeRestricted;
588b7c156eSVernon Mauery     bool postCompleted = true;
598b7c156eSVernon Mauery     bool coreBIOSDone = true;
608b7c156eSVernon Mauery     int channelSMM = -1;
618b7c156eSVernon Mauery     std::shared_ptr<sdbusplus::asio::connection> bus;
628b7c156eSVernon Mauery     std::unique_ptr<sdbusplus::bus::match_t> modeChangeMatch;
638b7c156eSVernon Mauery     std::unique_ptr<sdbusplus::bus::match_t> modeIntfAddedMatch;
648b7c156eSVernon Mauery     std::unique_ptr<sdbusplus::bus::match_t> postCompleteMatch;
658b7c156eSVernon Mauery     std::unique_ptr<sdbusplus::bus::match_t> postCompleteIntfAddedMatch;
668b7c156eSVernon Mauery     std::unique_ptr<sdbusplus::bus::match_t> platStateChangeMatch;
678b7c156eSVernon Mauery     std::unique_ptr<sdbusplus::bus::match_t> platStateIntfAddedMatch;
688b7c156eSVernon Mauery 
698b7c156eSVernon Mauery     static constexpr const char restrictionModeIntf[] =
708b7c156eSVernon Mauery         "xyz.openbmc_project.Control.Security.RestrictionMode";
718b7c156eSVernon Mauery     static constexpr const char* systemOsStatusIntf =
728b7c156eSVernon Mauery         "xyz.openbmc_project.State.OperatingSystem.Status";
738b7c156eSVernon Mauery     static constexpr const char* hostMiscIntf =
748b7c156eSVernon Mauery         "xyz.openbmc_project.State.Host.Misc";
758b7c156eSVernon Mauery     static constexpr const char* restrictionModePath =
768b7c156eSVernon Mauery         "/xyz/openbmc_project/control/security/restriction_mode";
778b7c156eSVernon Mauery     static constexpr const char* systemOsStatusPath =
788b7c156eSVernon Mauery         "/xyz/openbmc_project/state/os";
798b7c156eSVernon Mauery };
808b7c156eSVernon Mauery 
818b7c156eSVernon Mauery static inline uint8_t getSMMChannel()
828b7c156eSVernon Mauery {
838b7c156eSVernon Mauery     ipmi::ChannelInfo chInfo;
848b7c156eSVernon Mauery 
858b7c156eSVernon Mauery     for (int channel = 0; channel < ipmi::maxIpmiChannels; channel++)
868b7c156eSVernon Mauery     {
878b7c156eSVernon Mauery         if (ipmi::getChannelInfo(channel, chInfo) != ipmi::ccSuccess)
888b7c156eSVernon Mauery         {
898b7c156eSVernon Mauery             continue;
908b7c156eSVernon Mauery         }
918b7c156eSVernon Mauery 
928b7c156eSVernon Mauery         if (static_cast<ipmi::EChannelMediumType>(chInfo.mediumType) ==
938b7c156eSVernon Mauery                 ipmi::EChannelMediumType::systemInterface &&
948b7c156eSVernon Mauery             channel != ipmi::channelSystemIface)
958b7c156eSVernon Mauery         {
968b7c156eSVernon Mauery             log<level::INFO>("SMM channel number",
978b7c156eSVernon Mauery                              entry("CHANNEL=%d", channel));
988b7c156eSVernon Mauery             return channel;
998b7c156eSVernon Mauery         }
1008b7c156eSVernon Mauery     }
1018b7c156eSVernon Mauery     log<level::ERR>("Unable to find SMM Channel Info");
1028b7c156eSVernon Mauery     return -1;
1038b7c156eSVernon Mauery }
1048b7c156eSVernon Mauery 
1058b7c156eSVernon Mauery AllowlistFilter::AllowlistFilter()
1068b7c156eSVernon Mauery {
1078b7c156eSVernon Mauery     bus = getSdBus();
1088b7c156eSVernon Mauery 
1098b7c156eSVernon Mauery     log<level::INFO>("Loading Allowlist filter");
1108b7c156eSVernon Mauery 
1118b7c156eSVernon Mauery     ipmi::registerFilter(ipmi::prioOpenBmcBase,
1128b7c156eSVernon Mauery                          [this](ipmi::message::Request::ptr request) {
1138b7c156eSVernon Mauery         return filterMessage(request);
1148b7c156eSVernon Mauery     });
1158b7c156eSVernon Mauery 
1168b7c156eSVernon Mauery     channelSMM = getSMMChannel();
1178b7c156eSVernon Mauery     // wait until io->run is going to fetch RestrictionMode
1188b7c156eSVernon Mauery     post_work([this]() { postInit(); });
1198b7c156eSVernon Mauery }
1208b7c156eSVernon Mauery 
1218b7c156eSVernon Mauery void AllowlistFilter::cacheRestrictedAndPostCompleteMode()
1228b7c156eSVernon Mauery {
1238b7c156eSVernon Mauery     try
1248b7c156eSVernon Mauery     {
125*b37abfb2SPatrick Williams         auto service = ipmi::getService(*bus, restrictionModeIntf,
126*b37abfb2SPatrick Williams                                         restrictionModePath);
1278b7c156eSVernon Mauery         ipmi::Value v =
1288b7c156eSVernon Mauery             ipmi::getDbusProperty(*bus, service, restrictionModePath,
1298b7c156eSVernon Mauery                                   restrictionModeIntf, "RestrictionMode");
1308b7c156eSVernon Mauery         auto& mode = std::get<std::string>(v);
1318b7c156eSVernon Mauery         restrictionMode = RestrictionMode::convertModesFromString(mode);
1328b7c156eSVernon Mauery         log<level::INFO>("Read restriction mode",
1338b7c156eSVernon Mauery                          entry("VALUE=%d", static_cast<int>(restrictionMode)));
1348b7c156eSVernon Mauery     }
1358b7c156eSVernon Mauery     catch (const std::exception&)
1368b7c156eSVernon Mauery     {
1378b7c156eSVernon Mauery         log<level::ERR>("Could not initialize provisioning mode, "
1388b7c156eSVernon Mauery                         "defaulting to restricted",
1398b7c156eSVernon Mauery                         entry("VALUE=%d", static_cast<int>(restrictionMode)));
1408b7c156eSVernon Mauery     }
1418b7c156eSVernon Mauery 
1428b7c156eSVernon Mauery     try
1438b7c156eSVernon Mauery     {
144*b37abfb2SPatrick Williams         auto service = ipmi::getService(*bus, systemOsStatusIntf,
145*b37abfb2SPatrick Williams                                         systemOsStatusPath);
146*b37abfb2SPatrick Williams         ipmi::Value v = ipmi::getDbusProperty(*bus, service, systemOsStatusPath,
147*b37abfb2SPatrick Williams                                               systemOsStatusIntf,
148*b37abfb2SPatrick Williams                                               "OperatingSystemState");
1498b7c156eSVernon Mauery         auto& value = std::get<std::string>(v);
1508b7c156eSVernon Mauery         updatePostComplete(value);
1518b7c156eSVernon Mauery         log<level::INFO>("Read POST complete value",
1528b7c156eSVernon Mauery                          entry("VALUE=%d", postCompleted));
1538b7c156eSVernon Mauery     }
1548b7c156eSVernon Mauery     catch (const std::exception&)
1558b7c156eSVernon Mauery     {
1568b7c156eSVernon Mauery         log<level::ERR>("Error in OperatingSystemState Get");
1578b7c156eSVernon Mauery         postCompleted = true;
1588b7c156eSVernon Mauery     }
1598b7c156eSVernon Mauery }
1608b7c156eSVernon Mauery 
1618b7c156eSVernon Mauery void AllowlistFilter::updateRestrictionMode(const std::string& value)
1628b7c156eSVernon Mauery {
1638b7c156eSVernon Mauery     restrictionMode = RestrictionMode::convertModesFromString(value);
1648b7c156eSVernon Mauery     log<level::INFO>("Updated restriction mode",
1658b7c156eSVernon Mauery                      entry("VALUE=%d", static_cast<int>(restrictionMode)));
1668b7c156eSVernon Mauery }
1678b7c156eSVernon Mauery 
1688b7c156eSVernon Mauery void AllowlistFilter::handleRestrictedModeChange(sdbusplus::message_t& m)
1698b7c156eSVernon Mauery {
1708b7c156eSVernon Mauery     std::string signal = m.get_member();
1718b7c156eSVernon Mauery     if (signal == "PropertiesChanged")
1728b7c156eSVernon Mauery     {
1738b7c156eSVernon Mauery         std::string intf;
1748b7c156eSVernon Mauery         std::vector<std::pair<std::string, ipmi::Value>> propertyList;
1758b7c156eSVernon Mauery         m.read(intf, propertyList);
1768b7c156eSVernon Mauery         for (const auto& property : propertyList)
1778b7c156eSVernon Mauery         {
1788b7c156eSVernon Mauery             if (property.first == "RestrictionMode")
1798b7c156eSVernon Mauery             {
1808b7c156eSVernon Mauery                 updateRestrictionMode(std::get<std::string>(property.second));
1818b7c156eSVernon Mauery             }
1828b7c156eSVernon Mauery         }
1838b7c156eSVernon Mauery     }
1848b7c156eSVernon Mauery     else if (signal == "InterfacesAdded")
1858b7c156eSVernon Mauery     {
1868b7c156eSVernon Mauery         sdbusplus::message::object_path path;
1878b7c156eSVernon Mauery         DbusInterfaceMap restModeObj;
1888b7c156eSVernon Mauery         m.read(path, restModeObj);
1898b7c156eSVernon Mauery         auto intfItr = restModeObj.find(restrictionModeIntf);
1908b7c156eSVernon Mauery         if (intfItr == restModeObj.end())
1918b7c156eSVernon Mauery         {
1928b7c156eSVernon Mauery             return;
1938b7c156eSVernon Mauery         }
1948b7c156eSVernon Mauery         PropertyMap& propertyList = intfItr->second;
1958b7c156eSVernon Mauery         auto itr = propertyList.find("RestrictionMode");
1968b7c156eSVernon Mauery         if (itr == propertyList.end())
1978b7c156eSVernon Mauery         {
1988b7c156eSVernon Mauery             return;
1998b7c156eSVernon Mauery         }
2008b7c156eSVernon Mauery         updateRestrictionMode(std::get<std::string>(itr->second));
2018b7c156eSVernon Mauery     }
2028b7c156eSVernon Mauery }
2038b7c156eSVernon Mauery 
2048b7c156eSVernon Mauery void AllowlistFilter::updatePostComplete(const std::string& value)
2058b7c156eSVernon Mauery {
2068b7c156eSVernon Mauery     // The short string "Standby" is deprecated in favor of the full enum string
2078b7c156eSVernon Mauery     // Support for the short string will be removed in the future.
2088b7c156eSVernon Mauery     postCompleted = (value == "Standby") ||
2098b7c156eSVernon Mauery                     (value == "xyz.openbmc_project.State.OperatingSystem."
2108b7c156eSVernon Mauery                               "Status.OSStatus.Standby");
2118b7c156eSVernon Mauery     log<level::INFO>(postCompleted ? "Updated to POST Complete"
2128b7c156eSVernon Mauery                                    : "Updated to !POST Complete");
2138b7c156eSVernon Mauery }
2148b7c156eSVernon Mauery 
2158b7c156eSVernon Mauery void AllowlistFilter::handlePostCompleteChange(sdbusplus::message_t& m)
2168b7c156eSVernon Mauery {
2178b7c156eSVernon Mauery     std::string signal = m.get_member();
2188b7c156eSVernon Mauery     if (signal == "PropertiesChanged")
2198b7c156eSVernon Mauery     {
2208b7c156eSVernon Mauery         std::string intf;
2218b7c156eSVernon Mauery         std::vector<std::pair<std::string, ipmi::Value>> propertyList;
2228b7c156eSVernon Mauery         m.read(intf, propertyList);
2238b7c156eSVernon Mauery         for (const auto& property : propertyList)
2248b7c156eSVernon Mauery         {
2258b7c156eSVernon Mauery             if (property.first == "OperatingSystemState")
2268b7c156eSVernon Mauery             {
2278b7c156eSVernon Mauery                 updatePostComplete(std::get<std::string>(property.second));
2288b7c156eSVernon Mauery             }
2298b7c156eSVernon Mauery         }
2308b7c156eSVernon Mauery     }
2318b7c156eSVernon Mauery     else if (signal == "InterfacesAdded")
2328b7c156eSVernon Mauery     {
2338b7c156eSVernon Mauery         sdbusplus::message::object_path path;
2348b7c156eSVernon Mauery         DbusInterfaceMap postCompleteObj;
2358b7c156eSVernon Mauery         m.read(path, postCompleteObj);
2368b7c156eSVernon Mauery         auto intfItr = postCompleteObj.find(systemOsStatusIntf);
2378b7c156eSVernon Mauery         if (intfItr == postCompleteObj.end())
2388b7c156eSVernon Mauery         {
2398b7c156eSVernon Mauery             return;
2408b7c156eSVernon Mauery         }
2418b7c156eSVernon Mauery         PropertyMap& propertyList = intfItr->second;
2428b7c156eSVernon Mauery         auto itr = propertyList.find("OperatingSystemState");
2438b7c156eSVernon Mauery         if (itr == propertyList.end())
2448b7c156eSVernon Mauery         {
2458b7c156eSVernon Mauery             return;
2468b7c156eSVernon Mauery         }
2478b7c156eSVernon Mauery         updatePostComplete(std::get<std::string>(itr->second));
2488b7c156eSVernon Mauery     }
2498b7c156eSVernon Mauery }
2508b7c156eSVernon Mauery 
2518b7c156eSVernon Mauery void AllowlistFilter::cacheCoreBiosDone()
2528b7c156eSVernon Mauery {
2538b7c156eSVernon Mauery     std::string coreBiosDonePath;
2548b7c156eSVernon Mauery     std::string coreBiosDoneService;
2558b7c156eSVernon Mauery     try
2568b7c156eSVernon Mauery     {
2578b7c156eSVernon Mauery         ipmi::DbusObjectInfo coreBiosDoneObj =
2588b7c156eSVernon Mauery             ipmi::getDbusObject(*bus, hostMiscIntf);
2598b7c156eSVernon Mauery 
2608b7c156eSVernon Mauery         coreBiosDonePath = coreBiosDoneObj.first;
2618b7c156eSVernon Mauery         coreBiosDoneService = coreBiosDoneObj.second;
2628b7c156eSVernon Mauery     }
2638b7c156eSVernon Mauery     catch (const std::exception&)
2648b7c156eSVernon Mauery     {
2658b7c156eSVernon Mauery         log<level::ERR>("Could not initialize CoreBiosDone, "
2668b7c156eSVernon Mauery                         "coreBIOSDone asserted as default");
2678b7c156eSVernon Mauery         return;
2688b7c156eSVernon Mauery     }
2698b7c156eSVernon Mauery 
2708b7c156eSVernon Mauery     bus->async_method_call(
2718b7c156eSVernon Mauery         [this](boost::system::error_code ec, const ipmi::Value& v) {
2728b7c156eSVernon Mauery         if (ec)
2738b7c156eSVernon Mauery         {
2748b7c156eSVernon Mauery             log<level::ERR>(
2758b7c156eSVernon Mauery                 "async call failed, coreBIOSDone asserted as default");
2768b7c156eSVernon Mauery             return;
2778b7c156eSVernon Mauery         }
2788b7c156eSVernon Mauery         coreBIOSDone = std::get<bool>(v);
2798b7c156eSVernon Mauery         log<level::INFO>("Read CoreBiosDone",
2808b7c156eSVernon Mauery                          entry("VALUE=%d", static_cast<int>(coreBIOSDone)));
2818b7c156eSVernon Mauery         },
2828b7c156eSVernon Mauery         coreBiosDoneService, coreBiosDonePath,
2838b7c156eSVernon Mauery         "org.freedesktop.DBus.Properties", "Get", hostMiscIntf, "CoreBiosDone");
2848b7c156eSVernon Mauery }
2858b7c156eSVernon Mauery 
2868b7c156eSVernon Mauery void AllowlistFilter::handleCoreBiosDoneChange(sdbusplus::message_t& msg)
2878b7c156eSVernon Mauery {
2888b7c156eSVernon Mauery     std::string signal = msg.get_member();
2898b7c156eSVernon Mauery     if (signal == "PropertiesChanged")
2908b7c156eSVernon Mauery     {
2918b7c156eSVernon Mauery         std::string intf;
2928b7c156eSVernon Mauery         std::vector<std::pair<std::string, ipmi::Value>> propertyList;
2938b7c156eSVernon Mauery         msg.read(intf, propertyList);
2948b7c156eSVernon Mauery         auto it =
2958b7c156eSVernon Mauery             std::find_if(propertyList.begin(), propertyList.end(),
2968b7c156eSVernon Mauery                          [](const std::pair<std::string, ipmi::Value>& prop) {
2978b7c156eSVernon Mauery             return prop.first == "CoreBiosDone";
2988b7c156eSVernon Mauery             });
2998b7c156eSVernon Mauery 
3008b7c156eSVernon Mauery         if (it != propertyList.end())
3018b7c156eSVernon Mauery         {
3028b7c156eSVernon Mauery             coreBIOSDone = std::get<bool>(it->second);
3038b7c156eSVernon Mauery             log<level::INFO>(coreBIOSDone ? "coreBIOSDone asserted"
3048b7c156eSVernon Mauery                                           : "coreBIOSDone not asserted");
3058b7c156eSVernon Mauery         }
3068b7c156eSVernon Mauery     }
3078b7c156eSVernon Mauery     else if (signal == "InterfacesAdded")
3088b7c156eSVernon Mauery     {
3098b7c156eSVernon Mauery         sdbusplus::message::object_path path;
3108b7c156eSVernon Mauery         DbusInterfaceMap eSpiresetObj;
3118b7c156eSVernon Mauery         msg.read(path, eSpiresetObj);
3128b7c156eSVernon Mauery         auto intfItr = eSpiresetObj.find(hostMiscIntf);
3138b7c156eSVernon Mauery         if (intfItr == eSpiresetObj.end())
3148b7c156eSVernon Mauery         {
3158b7c156eSVernon Mauery             return;
3168b7c156eSVernon Mauery         }
3178b7c156eSVernon Mauery         PropertyMap& propertyList = intfItr->second;
3188b7c156eSVernon Mauery         auto itr = propertyList.find("CoreBiosDone");
3198b7c156eSVernon Mauery         if (itr == propertyList.end())
3208b7c156eSVernon Mauery         {
3218b7c156eSVernon Mauery             return;
3228b7c156eSVernon Mauery         }
3238b7c156eSVernon Mauery         coreBIOSDone = std::get<bool>(itr->second);
3248b7c156eSVernon Mauery         log<level::INFO>(coreBIOSDone ? "coreBIOSDone asserted"
3258b7c156eSVernon Mauery                                       : "coreBIOSDone not asserted");
3268b7c156eSVernon Mauery     }
3278b7c156eSVernon Mauery }
3288b7c156eSVernon Mauery 
3298b7c156eSVernon Mauery void AllowlistFilter::postInit()
3308b7c156eSVernon Mauery {
3318b7c156eSVernon Mauery     // Wait for changes on Restricted mode
3328b7c156eSVernon Mauery     namespace rules = sdbusplus::bus::match::rules;
3338b7c156eSVernon Mauery     const std::string filterStrModeChange =
3348b7c156eSVernon Mauery         rules::type::signal() + rules::member("PropertiesChanged") +
3358b7c156eSVernon Mauery         rules::interface("org.freedesktop.DBus.Properties") +
3368b7c156eSVernon Mauery         rules::argN(0, restrictionModeIntf);
3378b7c156eSVernon Mauery 
3388b7c156eSVernon Mauery     const std::string filterStrModeIntfAdd =
3398b7c156eSVernon Mauery         rules::interfacesAdded() +
3408b7c156eSVernon Mauery         rules::argNpath(
3418b7c156eSVernon Mauery             0, "/xyz/openbmc_project/control/security/restriction_mode");
3428b7c156eSVernon Mauery 
3438b7c156eSVernon Mauery     const std::string filterStrPostComplete =
3448b7c156eSVernon Mauery         rules::type::signal() + rules::member("PropertiesChanged") +
3458b7c156eSVernon Mauery         rules::interface("org.freedesktop.DBus.Properties") +
3468b7c156eSVernon Mauery         rules::argN(0, systemOsStatusIntf);
3478b7c156eSVernon Mauery 
3488b7c156eSVernon Mauery     const std::string filterStrPostIntfAdd =
3498b7c156eSVernon Mauery         rules::interfacesAdded() +
3508b7c156eSVernon Mauery         rules::argNpath(0, "/xyz/openbmc_project/state/os");
3518b7c156eSVernon Mauery 
3528b7c156eSVernon Mauery     const std::string filterStrPlatStateChange =
3538b7c156eSVernon Mauery         rules::type::signal() + rules::member("PropertiesChanged") +
3548b7c156eSVernon Mauery         rules::interface("org.freedesktop.DBus.Properties") +
3558b7c156eSVernon Mauery         rules::argN(0, hostMiscIntf);
3568b7c156eSVernon Mauery 
3578b7c156eSVernon Mauery     const std::string filterStrPlatStateIntfAdd =
3588b7c156eSVernon Mauery         rules::interfacesAdded() +
3598b7c156eSVernon Mauery         rules::argNpath(0, "/xyz/openbmc_project/misc/platform_state");
3608b7c156eSVernon Mauery 
3618b7c156eSVernon Mauery     modeChangeMatch = std::make_unique<sdbusplus::bus::match_t>(
3628b7c156eSVernon Mauery         *bus, filterStrModeChange,
3638b7c156eSVernon Mauery         [this](sdbusplus::message_t& m) { handleRestrictedModeChange(m); });
3648b7c156eSVernon Mauery     modeIntfAddedMatch = std::make_unique<sdbusplus::bus::match_t>(
3658b7c156eSVernon Mauery         *bus, filterStrModeIntfAdd,
3668b7c156eSVernon Mauery         [this](sdbusplus::message_t& m) { handleRestrictedModeChange(m); });
3678b7c156eSVernon Mauery 
3688b7c156eSVernon Mauery     postCompleteMatch = std::make_unique<sdbusplus::bus::match_t>(
3698b7c156eSVernon Mauery         *bus, filterStrPostComplete,
3708b7c156eSVernon Mauery         [this](sdbusplus::message_t& m) { handlePostCompleteChange(m); });
3718b7c156eSVernon Mauery 
3728b7c156eSVernon Mauery     postCompleteIntfAddedMatch = std::make_unique<sdbusplus::bus::match_t>(
3738b7c156eSVernon Mauery         *bus, filterStrPostIntfAdd,
3748b7c156eSVernon Mauery         [this](sdbusplus::message_t& m) { handlePostCompleteChange(m); });
3758b7c156eSVernon Mauery 
3768b7c156eSVernon Mauery     platStateChangeMatch = std::make_unique<sdbusplus::bus::match_t>(
3778b7c156eSVernon Mauery         *bus, filterStrPlatStateChange,
3788b7c156eSVernon Mauery         [this](sdbusplus::message_t& m) { handleCoreBiosDoneChange(m); });
3798b7c156eSVernon Mauery 
3808b7c156eSVernon Mauery     platStateIntfAddedMatch = std::make_unique<sdbusplus::bus::match_t>(
3818b7c156eSVernon Mauery         *bus, filterStrPlatStateIntfAdd,
3828b7c156eSVernon Mauery         [this](sdbusplus::message_t& m) { handleCoreBiosDoneChange(m); });
3838b7c156eSVernon Mauery 
3848b7c156eSVernon Mauery     // Initialize restricted mode
3858b7c156eSVernon Mauery     cacheRestrictedAndPostCompleteMode();
3868b7c156eSVernon Mauery     // Initialize CoreBiosDone
3878b7c156eSVernon Mauery     cacheCoreBiosDone();
3888b7c156eSVernon Mauery }
3898b7c156eSVernon Mauery 
3908b7c156eSVernon Mauery ipmi::Cc AllowlistFilter::filterMessage(ipmi::message::Request::ptr request)
3918b7c156eSVernon Mauery {
3928b7c156eSVernon Mauery     auto channelMask = static_cast<unsigned short>(1 << request->ctx->channel);
3938b7c156eSVernon Mauery     bool Allowlisted = std::binary_search(
3948b7c156eSVernon Mauery         allowlist.cbegin(), allowlist.cend(),
3958b7c156eSVernon Mauery         std::make_tuple(request->ctx->netFn, request->ctx->cmd, channelMask),
3968b7c156eSVernon Mauery         [](const netfncmd_tuple& first, const netfncmd_tuple& value) {
3978b7c156eSVernon Mauery         return (std::get<2>(first) & std::get<2>(value))
3988b7c156eSVernon Mauery                    ? first < std::make_tuple(std::get<0>(value),
3998b7c156eSVernon Mauery                                              std::get<1>(value),
4008b7c156eSVernon Mauery                                              std::get<2>(first))
4018b7c156eSVernon Mauery                    : first < value;
4028b7c156eSVernon Mauery         });
4038b7c156eSVernon Mauery 
4048b7c156eSVernon Mauery     // no special handling for non-system-interface channels
4058b7c156eSVernon Mauery     if (!(request->ctx->channel == ipmi::channelSystemIface ||
4068b7c156eSVernon Mauery           request->ctx->channel == channelSMM))
4078b7c156eSVernon Mauery     {
4088b7c156eSVernon Mauery         if (!Allowlisted)
4098b7c156eSVernon Mauery         {
4108b7c156eSVernon Mauery             log<level::INFO>("Channel/NetFn/Cmd not Allowlisted",
4118b7c156eSVernon Mauery                              entry("CHANNEL=0x%X", request->ctx->channel),
4128b7c156eSVernon Mauery                              entry("NETFN=0x%X", int(request->ctx->netFn)),
4138b7c156eSVernon Mauery                              entry("CMD=0x%X", int(request->ctx->cmd)));
4148b7c156eSVernon Mauery             return ipmi::ccInsufficientPrivilege;
4158b7c156eSVernon Mauery         }
4168b7c156eSVernon Mauery         return ipmi::ccSuccess;
4178b7c156eSVernon Mauery     }
4188b7c156eSVernon Mauery 
4198b7c156eSVernon Mauery     // for system interface, filtering is done as follows:
4208b7c156eSVernon Mauery     // Allow All:  preboot ? ccSuccess : ccSuccess
4218b7c156eSVernon Mauery     // Restricted: preboot ? ccSuccess :
4228b7c156eSVernon Mauery     //                  ( Allowlist ? ccSuccess : ccInsufficientPrivilege )
4238b7c156eSVernon Mauery     // Deny All:   preboot ? ccSuccess : ccInsufficientPrivilege
4248b7c156eSVernon Mauery 
4258b7c156eSVernon Mauery     if (!(postCompleted || coreBIOSDone))
4268b7c156eSVernon Mauery     {
4278b7c156eSVernon Mauery         // Allow all commands, till POST or CoreBiosDone is completed
4288b7c156eSVernon Mauery         return ipmi::ccSuccess;
4298b7c156eSVernon Mauery     }
4308b7c156eSVernon Mauery 
4318b7c156eSVernon Mauery     switch (restrictionMode)
4328b7c156eSVernon Mauery     {
4338b7c156eSVernon Mauery         case RestrictionMode::Modes::None:
4348b7c156eSVernon Mauery         case restrictionModeAllowAll:
4358b7c156eSVernon Mauery         {
4368b7c156eSVernon Mauery             // Allow All
4378b7c156eSVernon Mauery             return ipmi::ccSuccess;
4388b7c156eSVernon Mauery             break;
4398b7c156eSVernon Mauery         }
4408b7c156eSVernon Mauery         case restrictionModeRestricted:
4418b7c156eSVernon Mauery         {
4428b7c156eSVernon Mauery             // Restricted - follow Allowlist
4438b7c156eSVernon Mauery             break;
4448b7c156eSVernon Mauery         }
4458b7c156eSVernon Mauery         case restrictionModeDenyAll:
4468b7c156eSVernon Mauery         {
4478b7c156eSVernon Mauery             // Deny All
4488b7c156eSVernon Mauery             Allowlisted = false;
4498b7c156eSVernon Mauery             break;
4508b7c156eSVernon Mauery         }
45180d4d5f9SMatt Simmering         default: // for Allowlist and Blocklist
4528b7c156eSVernon Mauery             return ipmi::ccInsufficientPrivilege;
4538b7c156eSVernon Mauery     }
4548b7c156eSVernon Mauery 
4558b7c156eSVernon Mauery     if (!Allowlisted)
4568b7c156eSVernon Mauery     {
4578b7c156eSVernon Mauery         log<level::INFO>("Channel/NetFn/Cmd not allowlisted",
4588b7c156eSVernon Mauery                          entry("CHANNEL=0x%X", request->ctx->channel),
4598b7c156eSVernon Mauery                          entry("NETFN=0x%X", int(request->ctx->netFn)),
4608b7c156eSVernon Mauery                          entry("CMD=0x%X", int(request->ctx->cmd)));
4618b7c156eSVernon Mauery         return ipmi::ccInsufficientPrivilege;
4628b7c156eSVernon Mauery     }
4638b7c156eSVernon Mauery     return ipmi::ccSuccess;
4648b7c156eSVernon Mauery } // namespace
4658b7c156eSVernon Mauery 
4668b7c156eSVernon Mauery // instantiate the AllowlistFilter when this shared object is loaded
4678b7c156eSVernon Mauery AllowlistFilter allowlistFilter;
4688b7c156eSVernon Mauery 
4698b7c156eSVernon Mauery } // namespace
4708b7c156eSVernon Mauery 
4718b7c156eSVernon Mauery } // namespace ipmi
472