Vernon Mauery | 783dc07 | 2018-10-08 12:05:20 -0700 | [diff] [blame] | 1 | #include <algorithm> |
| 2 | #include <array> |
| 3 | #include <ipmid/api.hpp> |
Vernon Mauery | 6a98fe7 | 2019-03-11 15:57:48 -0700 | [diff] [blame] | 4 | #include <ipmid/utils.hpp> |
Vernon Mauery | 783dc07 | 2018-10-08 12:05:20 -0700 | [diff] [blame] | 5 | #include <ipmiwhitelist.hpp> |
| 6 | #include <phosphor-logging/elog-errors.hpp> |
| 7 | #include <phosphor-logging/log.hpp> |
| 8 | #include <settings.hpp> |
| 9 | #include <xyz/openbmc_project/Control/Security/RestrictionMode/server.hpp> |
| 10 | |
| 11 | using namespace phosphor::logging; |
| 12 | using namespace sdbusplus::xyz::openbmc_project::Common::Error; |
| 13 | |
| 14 | namespace ipmi |
| 15 | { |
| 16 | |
| 17 | // put the filter provider in an unnamed namespace |
| 18 | namespace |
| 19 | { |
| 20 | |
| 21 | /** @class WhitelistFilter |
| 22 | * |
| 23 | * Class that implements an IPMI message filter based |
| 24 | * on incoming interface and a restriction mode setting |
| 25 | */ |
| 26 | class WhitelistFilter |
| 27 | { |
| 28 | public: |
| 29 | WhitelistFilter(); |
| 30 | ~WhitelistFilter() = default; |
| 31 | WhitelistFilter(WhitelistFilter const&) = delete; |
| 32 | WhitelistFilter(WhitelistFilter&&) = delete; |
| 33 | WhitelistFilter& operator=(WhitelistFilter const&) = delete; |
| 34 | WhitelistFilter& operator=(WhitelistFilter&&) = delete; |
| 35 | |
| 36 | private: |
| 37 | void postInit(); |
| 38 | void cacheRestrictedMode(); |
Patrick Williams | 5d82f47 | 2022-07-22 19:26:53 -0500 | [diff] [blame] | 39 | void handleRestrictedModeChange(sdbusplus::message_t& m); |
Vernon Mauery | 783dc07 | 2018-10-08 12:05:20 -0700 | [diff] [blame] | 40 | ipmi::Cc filterMessage(ipmi::message::Request::ptr request); |
| 41 | |
| 42 | bool restrictedMode = true; |
| 43 | std::shared_ptr<sdbusplus::asio::connection> bus; |
| 44 | std::unique_ptr<settings::Objects> objects; |
Patrick Williams | 5d82f47 | 2022-07-22 19:26:53 -0500 | [diff] [blame] | 45 | std::unique_ptr<sdbusplus::bus::match_t> modeChangeMatch; |
Vernon Mauery | 783dc07 | 2018-10-08 12:05:20 -0700 | [diff] [blame] | 46 | |
| 47 | static constexpr const char restrictionModeIntf[] = |
| 48 | "xyz.openbmc_project.Control.Security.RestrictionMode"; |
| 49 | }; |
| 50 | |
| 51 | WhitelistFilter::WhitelistFilter() |
| 52 | { |
| 53 | bus = getSdBus(); |
| 54 | |
| 55 | log<level::INFO>("Loading whitelist filter"); |
| 56 | |
| 57 | ipmi::registerFilter(ipmi::prioOpenBmcBase, |
| 58 | [this](ipmi::message::Request::ptr request) { |
| 59 | return filterMessage(request); |
| 60 | }); |
| 61 | |
| 62 | // wait until io->run is going to fetch RestrictionMode |
| 63 | post_work([this]() { postInit(); }); |
| 64 | } |
| 65 | |
| 66 | void WhitelistFilter::cacheRestrictedMode() |
| 67 | { |
| 68 | using namespace sdbusplus::xyz::openbmc_project::Control::Security::server; |
| 69 | std::string restrictionModeSetting; |
| 70 | std::string restrictionModeService; |
| 71 | try |
| 72 | { |
| 73 | restrictionModeSetting = objects->map.at(restrictionModeIntf).at(0); |
| 74 | restrictionModeService = |
| 75 | objects->service(restrictionModeSetting, restrictionModeIntf); |
| 76 | } |
| 77 | catch (const std::out_of_range& e) |
| 78 | { |
| 79 | log<level::ERR>( |
| 80 | "Could not look up restriction mode interface from cache"); |
| 81 | return; |
| 82 | } |
| 83 | bus->async_method_call( |
Vernon Mauery | 64a350d | 2019-03-06 09:24:55 -0800 | [diff] [blame] | 84 | [this](boost::system::error_code ec, ipmi::Value v) { |
Vernon Mauery | 783dc07 | 2018-10-08 12:05:20 -0700 | [diff] [blame] | 85 | if (ec) |
| 86 | { |
| 87 | log<level::ERR>("Error in RestrictionMode Get"); |
| 88 | // Fail-safe to true. |
| 89 | restrictedMode = true; |
| 90 | return; |
| 91 | } |
Vernon Mauery | 64a350d | 2019-03-06 09:24:55 -0800 | [diff] [blame] | 92 | auto mode = std::get<std::string>(v); |
Vernon Mauery | 783dc07 | 2018-10-08 12:05:20 -0700 | [diff] [blame] | 93 | auto restrictionMode = |
| 94 | RestrictionMode::convertModesFromString(mode); |
| 95 | restrictedMode = |
| 96 | (restrictionMode == RestrictionMode::Modes::Whitelist); |
| 97 | log<level::INFO>((restrictedMode ? "Set restrictedMode = true" |
| 98 | : "Set restrictedMode = false")); |
| 99 | }, |
Vernon Mauery | 64a350d | 2019-03-06 09:24:55 -0800 | [diff] [blame] | 100 | restrictionModeService, restrictionModeSetting, |
| 101 | "org.freedesktop.DBus.Properties", "Get", restrictionModeIntf, |
| 102 | "RestrictionMode"); |
Vernon Mauery | 783dc07 | 2018-10-08 12:05:20 -0700 | [diff] [blame] | 103 | } |
| 104 | |
Patrick Williams | 5d82f47 | 2022-07-22 19:26:53 -0500 | [diff] [blame] | 105 | void WhitelistFilter::handleRestrictedModeChange(sdbusplus::message_t& m) |
Vernon Mauery | 783dc07 | 2018-10-08 12:05:20 -0700 | [diff] [blame] | 106 | { |
| 107 | using namespace sdbusplus::xyz::openbmc_project::Control::Security::server; |
Richard Marian Thomaiyar | 57bff01 | 2019-04-12 22:32:21 +0530 | [diff] [blame] | 108 | std::string intf; |
| 109 | std::vector<std::pair<std::string, ipmi::Value>> propertyList; |
| 110 | m.read(intf, propertyList); |
| 111 | for (const auto& property : propertyList) |
| 112 | { |
| 113 | if (property.first == "RestrictionMode") |
| 114 | { |
| 115 | RestrictionMode::Modes restrictionMode = |
| 116 | RestrictionMode::convertModesFromString( |
| 117 | std::get<std::string>(property.second)); |
| 118 | restrictedMode = |
| 119 | (restrictionMode == RestrictionMode::Modes::Whitelist); |
| 120 | log<level::INFO>((restrictedMode |
| 121 | ? "Updated restrictedMode = true" |
| 122 | : "Updated restrictedMode = false")); |
| 123 | } |
| 124 | } |
Vernon Mauery | 783dc07 | 2018-10-08 12:05:20 -0700 | [diff] [blame] | 125 | } |
| 126 | |
| 127 | void WhitelistFilter::postInit() |
| 128 | { |
| 129 | objects = std::make_unique<settings::Objects>( |
| 130 | *bus, std::vector<settings::Interface>({restrictionModeIntf})); |
| 131 | if (!objects) |
| 132 | { |
| 133 | log<level::ERR>( |
| 134 | "Failed to create settings object; defaulting to restricted mode"); |
| 135 | return; |
| 136 | } |
| 137 | |
| 138 | // Initialize restricted mode |
| 139 | cacheRestrictedMode(); |
| 140 | // Wait for changes on Restricted mode |
| 141 | std::string filterStr; |
| 142 | try |
| 143 | { |
| 144 | filterStr = sdbusplus::bus::match::rules::propertiesChanged( |
| 145 | objects->map.at(restrictionModeIntf).at(0), restrictionModeIntf); |
| 146 | } |
| 147 | catch (const std::out_of_range& e) |
| 148 | { |
| 149 | log<level::ERR>("Failed to determine restriction mode filter string"); |
| 150 | return; |
| 151 | } |
Patrick Williams | 5d82f47 | 2022-07-22 19:26:53 -0500 | [diff] [blame] | 152 | modeChangeMatch = std::make_unique<sdbusplus::bus::match_t>( |
| 153 | *bus, filterStr, |
| 154 | [this](sdbusplus::message_t& m) { handleRestrictedModeChange(m); }); |
Vernon Mauery | 783dc07 | 2018-10-08 12:05:20 -0700 | [diff] [blame] | 155 | } |
| 156 | |
| 157 | ipmi::Cc WhitelistFilter::filterMessage(ipmi::message::Request::ptr request) |
| 158 | { |
| 159 | if (request->ctx->channel == ipmi::channelSystemIface && restrictedMode) |
| 160 | { |
| 161 | if (!std::binary_search( |
| 162 | whitelist.cbegin(), whitelist.cend(), |
| 163 | std::make_pair(request->ctx->netFn, request->ctx->cmd))) |
| 164 | { |
| 165 | log<level::ERR>("Net function not whitelisted", |
| 166 | entry("NETFN=0x%X", int(request->ctx->netFn)), |
| 167 | entry("CMD=0x%X", int(request->ctx->cmd))); |
| 168 | return ipmi::ccInsufficientPrivilege; |
| 169 | } |
| 170 | } |
| 171 | return ipmi::ccSuccess; |
| 172 | } |
| 173 | |
| 174 | // instantiate the WhitelistFilter when this shared object is loaded |
| 175 | WhitelistFilter whitelistFilter; |
| 176 | |
| 177 | } // namespace |
| 178 | |
| 179 | } // namespace ipmi |