Matt Simmering | 68d9d40 | 2023-11-09 14:22:11 -0800 | [diff] [blame] | 1 | #include <ipmiallowlist.hpp> |
Vernon Mauery | 783dc07 | 2018-10-08 12:05:20 -0700 | [diff] [blame] | 2 | #include <ipmid/api.hpp> |
Vernon Mauery | 6a98fe7 | 2019-03-11 15:57:48 -0700 | [diff] [blame] | 3 | #include <ipmid/utils.hpp> |
Vernon Mauery | 783dc07 | 2018-10-08 12:05:20 -0700 | [diff] [blame] | 4 | #include <phosphor-logging/elog-errors.hpp> |
| 5 | #include <phosphor-logging/log.hpp> |
| 6 | #include <settings.hpp> |
Patrick Williams | 6856f1b | 2023-09-01 16:10:07 -0500 | [diff] [blame] | 7 | #include <xyz/openbmc_project/Common/error.hpp> |
Vernon Mauery | 783dc07 | 2018-10-08 12:05:20 -0700 | [diff] [blame] | 8 | #include <xyz/openbmc_project/Control/Security/RestrictionMode/server.hpp> |
| 9 | |
Patrick Williams | fbc6c9d | 2023-05-10 07:50:16 -0500 | [diff] [blame] | 10 | #include <algorithm> |
| 11 | #include <array> |
| 12 | |
Vernon Mauery | 783dc07 | 2018-10-08 12:05:20 -0700 | [diff] [blame] | 13 | using namespace phosphor::logging; |
Willy Tu | 523e2d1 | 2023-09-05 11:36:48 -0700 | [diff] [blame] | 14 | using namespace sdbusplus::error::xyz::openbmc_project::common; |
Vernon Mauery | 783dc07 | 2018-10-08 12:05:20 -0700 | [diff] [blame] | 15 | |
| 16 | namespace ipmi |
| 17 | { |
| 18 | |
| 19 | // put the filter provider in an unnamed namespace |
| 20 | namespace |
| 21 | { |
| 22 | |
Matt Simmering | 68d9d40 | 2023-11-09 14:22:11 -0800 | [diff] [blame] | 23 | /** @class AllowlistFilter |
Vernon Mauery | 783dc07 | 2018-10-08 12:05:20 -0700 | [diff] [blame] | 24 | * |
| 25 | * Class that implements an IPMI message filter based |
| 26 | * on incoming interface and a restriction mode setting |
| 27 | */ |
Matt Simmering | 68d9d40 | 2023-11-09 14:22:11 -0800 | [diff] [blame] | 28 | class AllowlistFilter |
Vernon Mauery | 783dc07 | 2018-10-08 12:05:20 -0700 | [diff] [blame] | 29 | { |
| 30 | public: |
Matt Simmering | 68d9d40 | 2023-11-09 14:22:11 -0800 | [diff] [blame] | 31 | AllowlistFilter(); |
| 32 | ~AllowlistFilter() = default; |
| 33 | AllowlistFilter(const AllowlistFilter&) = delete; |
| 34 | AllowlistFilter(AllowlistFilter&&) = delete; |
| 35 | AllowlistFilter& operator=(const AllowlistFilter&) = delete; |
| 36 | AllowlistFilter& operator=(AllowlistFilter&&) = delete; |
Vernon Mauery | 783dc07 | 2018-10-08 12:05:20 -0700 | [diff] [blame] | 37 | |
| 38 | private: |
| 39 | void postInit(); |
Kumar Thangavel | 902d90d | 2022-04-11 11:51:33 +0530 | [diff] [blame] | 40 | void cacheRestrictedMode(const std::vector<std::string>& devices); |
| 41 | void handleRestrictedModeChange( |
Patrick Williams | 01d1460 | 2022-11-30 02:36:04 -0600 | [diff] [blame] | 42 | sdbusplus::message_t& m, |
Kumar Thangavel | 902d90d | 2022-04-11 11:51:33 +0530 | [diff] [blame] | 43 | const std::map<std::string, size_t>& deviceList); |
Vernon Mauery | 783dc07 | 2018-10-08 12:05:20 -0700 | [diff] [blame] | 44 | ipmi::Cc filterMessage(ipmi::message::Request::ptr request); |
| 45 | |
Kumar Thangavel | 902d90d | 2022-04-11 11:51:33 +0530 | [diff] [blame] | 46 | std::vector<bool> restrictedMode; |
Vernon Mauery | 783dc07 | 2018-10-08 12:05:20 -0700 | [diff] [blame] | 47 | std::shared_ptr<sdbusplus::asio::connection> bus; |
| 48 | std::unique_ptr<settings::Objects> objects; |
Patrick Williams | 5d82f47 | 2022-07-22 19:26:53 -0500 | [diff] [blame] | 49 | std::unique_ptr<sdbusplus::bus::match_t> modeChangeMatch; |
Vernon Mauery | 783dc07 | 2018-10-08 12:05:20 -0700 | [diff] [blame] | 50 | |
| 51 | static constexpr const char restrictionModeIntf[] = |
| 52 | "xyz.openbmc_project.Control.Security.RestrictionMode"; |
| 53 | }; |
| 54 | |
Matt Simmering | 68d9d40 | 2023-11-09 14:22:11 -0800 | [diff] [blame] | 55 | AllowlistFilter::AllowlistFilter() |
Vernon Mauery | 783dc07 | 2018-10-08 12:05:20 -0700 | [diff] [blame] | 56 | { |
| 57 | bus = getSdBus(); |
| 58 | |
Matt Simmering | 68d9d40 | 2023-11-09 14:22:11 -0800 | [diff] [blame] | 59 | log<level::INFO>("Loading allowlist filter"); |
Vernon Mauery | 783dc07 | 2018-10-08 12:05:20 -0700 | [diff] [blame] | 60 | ipmi::registerFilter(ipmi::prioOpenBmcBase, |
| 61 | [this](ipmi::message::Request::ptr request) { |
Patrick Williams | fbc6c9d | 2023-05-10 07:50:16 -0500 | [diff] [blame] | 62 | return filterMessage(request); |
| 63 | }); |
Vernon Mauery | 783dc07 | 2018-10-08 12:05:20 -0700 | [diff] [blame] | 64 | |
| 65 | // wait until io->run is going to fetch RestrictionMode |
| 66 | post_work([this]() { postInit(); }); |
| 67 | } |
| 68 | |
Kumar Thangavel | 902d90d | 2022-04-11 11:51:33 +0530 | [diff] [blame] | 69 | /** @brief Get RestrictionMode of the devices which has RestrictionMode support |
| 70 | * enabled |
| 71 | * @param[in] devices - vector of devices object path |
| 72 | * @returns void. |
| 73 | */ |
| 74 | |
Matt Simmering | 68d9d40 | 2023-11-09 14:22:11 -0800 | [diff] [blame] | 75 | void AllowlistFilter::cacheRestrictedMode( |
Kumar Thangavel | 902d90d | 2022-04-11 11:51:33 +0530 | [diff] [blame] | 76 | const std::vector<std::string>& devices) |
Vernon Mauery | 783dc07 | 2018-10-08 12:05:20 -0700 | [diff] [blame] | 77 | { |
Willy Tu | 523e2d1 | 2023-09-05 11:36:48 -0700 | [diff] [blame] | 78 | using namespace sdbusplus::server::xyz::openbmc_project::control::security; |
Vernon Mauery | 783dc07 | 2018-10-08 12:05:20 -0700 | [diff] [blame] | 79 | std::string restrictionModeSetting; |
| 80 | std::string restrictionModeService; |
Kumar Thangavel | 902d90d | 2022-04-11 11:51:33 +0530 | [diff] [blame] | 81 | |
| 82 | for (auto& dev : devices) |
Vernon Mauery | 783dc07 | 2018-10-08 12:05:20 -0700 | [diff] [blame] | 83 | { |
Kumar Thangavel | 902d90d | 2022-04-11 11:51:33 +0530 | [diff] [blame] | 84 | try |
| 85 | { |
| 86 | restrictionModeSetting = dev; |
Patrick Williams | fbc6c9d | 2023-05-10 07:50:16 -0500 | [diff] [blame] | 87 | restrictionModeService = objects->service(restrictionModeSetting, |
| 88 | restrictionModeIntf); |
Kumar Thangavel | 902d90d | 2022-04-11 11:51:33 +0530 | [diff] [blame] | 89 | } |
| 90 | catch (const std::out_of_range& e) |
| 91 | { |
| 92 | log<level::ERR>( |
| 93 | "Could not look up restriction mode interface from cache"); |
| 94 | return; |
| 95 | } |
| 96 | |
| 97 | bus->async_method_call( |
| 98 | [this, index = std::distance(&*std::begin(devices), &dev)]( |
| 99 | boost::system::error_code ec, ipmi::Value v) { |
Patrick Williams | fbc6c9d | 2023-05-10 07:50:16 -0500 | [diff] [blame] | 100 | if (ec) |
| 101 | { |
| 102 | log<level::ERR>("Error in RestrictionMode Get"); |
| 103 | // Fail-safe to true. |
| 104 | restrictedMode[index] = true; |
| 105 | return; |
| 106 | } |
Kumar Thangavel | 902d90d | 2022-04-11 11:51:33 +0530 | [diff] [blame] | 107 | |
Patrick Williams | fbc6c9d | 2023-05-10 07:50:16 -0500 | [diff] [blame] | 108 | auto mode = std::get<std::string>(v); |
| 109 | auto restrictionMode = |
| 110 | RestrictionMode::convertModesFromString(mode); |
Kumar Thangavel | 902d90d | 2022-04-11 11:51:33 +0530 | [diff] [blame] | 111 | |
Patrick Williams | fbc6c9d | 2023-05-10 07:50:16 -0500 | [diff] [blame] | 112 | bool restrictMode = |
| 113 | (restrictionMode == RestrictionMode::Modes::Whitelist); |
| 114 | restrictedMode.emplace_back(restrictMode); |
Kumar Thangavel | 902d90d | 2022-04-11 11:51:33 +0530 | [diff] [blame] | 115 | |
Patrick Williams | fbc6c9d | 2023-05-10 07:50:16 -0500 | [diff] [blame] | 116 | log<level::INFO>((restrictMode ? "Set restrictedMode = true" |
| 117 | : "Set restrictedMode = false")); |
Patrick Williams | 369824e | 2023-10-20 11:18:23 -0500 | [diff] [blame] | 118 | }, |
Kumar Thangavel | 902d90d | 2022-04-11 11:51:33 +0530 | [diff] [blame] | 119 | restrictionModeService, restrictionModeSetting, |
| 120 | "org.freedesktop.DBus.Properties", "Get", restrictionModeIntf, |
| 121 | "RestrictionMode"); |
Vernon Mauery | 783dc07 | 2018-10-08 12:05:20 -0700 | [diff] [blame] | 122 | } |
Vernon Mauery | 783dc07 | 2018-10-08 12:05:20 -0700 | [diff] [blame] | 123 | } |
| 124 | |
Kumar Thangavel | 902d90d | 2022-04-11 11:51:33 +0530 | [diff] [blame] | 125 | /** @brief Update RestrictionMode if any changes in RestrictionMode |
| 126 | * @param[in] m - sdbusplus message. Using this to get Updated Mode dbus path |
| 127 | * @param[in] deviceList - map to store devices path and their index |
| 128 | * @returns void. |
| 129 | */ |
| 130 | |
Matt Simmering | 68d9d40 | 2023-11-09 14:22:11 -0800 | [diff] [blame] | 131 | void AllowlistFilter::handleRestrictedModeChange( |
Kumar Thangavel | 902d90d | 2022-04-11 11:51:33 +0530 | [diff] [blame] | 132 | sdbusplus::message_t& m, const std::map<std::string, size_t>& deviceList) |
Vernon Mauery | 783dc07 | 2018-10-08 12:05:20 -0700 | [diff] [blame] | 133 | { |
Willy Tu | 523e2d1 | 2023-09-05 11:36:48 -0700 | [diff] [blame] | 134 | using namespace sdbusplus::server::xyz::openbmc_project::control::security; |
Richard Marian Thomaiyar | 57bff01 | 2019-04-12 22:32:21 +0530 | [diff] [blame] | 135 | std::string intf; |
| 136 | std::vector<std::pair<std::string, ipmi::Value>> propertyList; |
| 137 | m.read(intf, propertyList); |
Kumar Thangavel | 902d90d | 2022-04-11 11:51:33 +0530 | [diff] [blame] | 138 | |
| 139 | std::string path = m.get_path(); |
| 140 | size_t hostId = 0; |
| 141 | auto it = deviceList.find(path); |
| 142 | |
| 143 | if (it == deviceList.end()) |
| 144 | { |
| 145 | log<level::ERR>("Key not found in deviceList "); |
| 146 | } |
| 147 | else |
| 148 | { |
| 149 | hostId = it->second; |
| 150 | } |
| 151 | |
Richard Marian Thomaiyar | 57bff01 | 2019-04-12 22:32:21 +0530 | [diff] [blame] | 152 | for (const auto& property : propertyList) |
| 153 | { |
| 154 | if (property.first == "RestrictionMode") |
| 155 | { |
| 156 | RestrictionMode::Modes restrictionMode = |
| 157 | RestrictionMode::convertModesFromString( |
| 158 | std::get<std::string>(property.second)); |
Kumar Thangavel | 902d90d | 2022-04-11 11:51:33 +0530 | [diff] [blame] | 159 | bool restrictMode = |
Richard Marian Thomaiyar | 57bff01 | 2019-04-12 22:32:21 +0530 | [diff] [blame] | 160 | (restrictionMode == RestrictionMode::Modes::Whitelist); |
Kumar Thangavel | 902d90d | 2022-04-11 11:51:33 +0530 | [diff] [blame] | 161 | restrictedMode[hostId] = restrictMode; |
| 162 | log<level::INFO>((restrictMode ? "Updated restrictedMode = true" |
| 163 | : "Updated restrictedMode = false")); |
Richard Marian Thomaiyar | 57bff01 | 2019-04-12 22:32:21 +0530 | [diff] [blame] | 164 | } |
| 165 | } |
Vernon Mauery | 783dc07 | 2018-10-08 12:05:20 -0700 | [diff] [blame] | 166 | } |
| 167 | |
Kumar Thangavel | 902d90d | 2022-04-11 11:51:33 +0530 | [diff] [blame] | 168 | /** @brief Get and Update RestrictionModes of supported devices |
| 169 | * @param[in] void |
| 170 | * @returns void. |
| 171 | */ |
| 172 | |
Matt Simmering | 68d9d40 | 2023-11-09 14:22:11 -0800 | [diff] [blame] | 173 | void AllowlistFilter::postInit() |
Vernon Mauery | 783dc07 | 2018-10-08 12:05:20 -0700 | [diff] [blame] | 174 | { |
| 175 | objects = std::make_unique<settings::Objects>( |
| 176 | *bus, std::vector<settings::Interface>({restrictionModeIntf})); |
| 177 | if (!objects) |
| 178 | { |
| 179 | log<level::ERR>( |
| 180 | "Failed to create settings object; defaulting to restricted mode"); |
| 181 | return; |
| 182 | } |
| 183 | |
Kumar Thangavel | 902d90d | 2022-04-11 11:51:33 +0530 | [diff] [blame] | 184 | std::vector<std::string> devices; |
Vernon Mauery | 783dc07 | 2018-10-08 12:05:20 -0700 | [diff] [blame] | 185 | try |
| 186 | { |
Kumar Thangavel | 902d90d | 2022-04-11 11:51:33 +0530 | [diff] [blame] | 187 | devices = objects->map.at(restrictionModeIntf); |
Vernon Mauery | 783dc07 | 2018-10-08 12:05:20 -0700 | [diff] [blame] | 188 | } |
| 189 | catch (const std::out_of_range& e) |
| 190 | { |
Kumar Thangavel | 902d90d | 2022-04-11 11:51:33 +0530 | [diff] [blame] | 191 | log<level::ERR>( |
| 192 | "Could not look up restriction mode interface from cache"); |
Vernon Mauery | 783dc07 | 2018-10-08 12:05:20 -0700 | [diff] [blame] | 193 | return; |
| 194 | } |
Kumar Thangavel | 902d90d | 2022-04-11 11:51:33 +0530 | [diff] [blame] | 195 | |
| 196 | // Initialize restricted mode |
| 197 | cacheRestrictedMode(devices); |
| 198 | // Wait for changes on Restricted mode |
| 199 | std::map<std::string, size_t> deviceList; |
| 200 | |
| 201 | for (size_t index = 0; index < devices.size(); index++) |
| 202 | { |
| 203 | deviceList.emplace(devices[index], index); |
| 204 | } |
| 205 | |
| 206 | std::string filterStr; |
| 207 | std::string devicesDbusPath{"/xyz/openbmc_project/control"}; |
| 208 | |
| 209 | filterStr = sdbusplus::bus::match::rules::propertiesChangedNamespace( |
| 210 | devicesDbusPath, restrictionModeIntf); |
| 211 | |
Patrick Williams | 5d82f47 | 2022-07-22 19:26:53 -0500 | [diff] [blame] | 212 | modeChangeMatch = std::make_unique<sdbusplus::bus::match_t>( |
Kumar Thangavel | 902d90d | 2022-04-11 11:51:33 +0530 | [diff] [blame] | 213 | *bus, filterStr, [this, deviceList](sdbusplus::message_t& m) { |
Patrick Williams | 369824e | 2023-10-20 11:18:23 -0500 | [diff] [blame] | 214 | handleRestrictedModeChange(m, deviceList); |
| 215 | }); |
Vernon Mauery | 783dc07 | 2018-10-08 12:05:20 -0700 | [diff] [blame] | 216 | } |
| 217 | |
Kumar Thangavel | 902d90d | 2022-04-11 11:51:33 +0530 | [diff] [blame] | 218 | /** @brief Filter IPMI messages with RestrictedMode |
| 219 | * @param[in] request - IPMI messahe request |
| 220 | * @returns IPMI completion code success or error. |
| 221 | */ |
| 222 | |
Matt Simmering | 68d9d40 | 2023-11-09 14:22:11 -0800 | [diff] [blame] | 223 | ipmi::Cc AllowlistFilter::filterMessage(ipmi::message::Request::ptr request) |
Vernon Mauery | 783dc07 | 2018-10-08 12:05:20 -0700 | [diff] [blame] | 224 | { |
Kumar Thangavel | 902d90d | 2022-04-11 11:51:33 +0530 | [diff] [blame] | 225 | /* Getting hostIdx for all IPMI devices like hosts, debugcard and other |
| 226 | devices from ipmi::message::Request and call postInit() to get the |
| 227 | restriction mode for all the IPMI commands */ |
| 228 | |
| 229 | size_t hostIdx = request->ctx->hostIdx; |
| 230 | |
| 231 | if (request->ctx->channel == ipmi::channelSystemIface && |
| 232 | restrictedMode[hostIdx]) |
Vernon Mauery | 783dc07 | 2018-10-08 12:05:20 -0700 | [diff] [blame] | 233 | { |
| 234 | if (!std::binary_search( |
Matt Simmering | 68d9d40 | 2023-11-09 14:22:11 -0800 | [diff] [blame] | 235 | allowlist.cbegin(), allowlist.cend(), |
Vernon Mauery | 783dc07 | 2018-10-08 12:05:20 -0700 | [diff] [blame] | 236 | std::make_pair(request->ctx->netFn, request->ctx->cmd))) |
| 237 | { |
Matt Simmering | 68d9d40 | 2023-11-09 14:22:11 -0800 | [diff] [blame] | 238 | log<level::ERR>("Net function not allowlisted", |
Vernon Mauery | 783dc07 | 2018-10-08 12:05:20 -0700 | [diff] [blame] | 239 | entry("NETFN=0x%X", int(request->ctx->netFn)), |
| 240 | entry("CMD=0x%X", int(request->ctx->cmd))); |
Kumar Thangavel | 902d90d | 2022-04-11 11:51:33 +0530 | [diff] [blame] | 241 | |
Vernon Mauery | 783dc07 | 2018-10-08 12:05:20 -0700 | [diff] [blame] | 242 | return ipmi::ccInsufficientPrivilege; |
| 243 | } |
| 244 | } |
| 245 | return ipmi::ccSuccess; |
| 246 | } |
| 247 | |
Matt Simmering | 68d9d40 | 2023-11-09 14:22:11 -0800 | [diff] [blame] | 248 | // instantiate the AllowlistFilter when this shared object is loaded |
| 249 | AllowlistFilter allowlistFilter; |
Vernon Mauery | 783dc07 | 2018-10-08 12:05:20 -0700 | [diff] [blame] | 250 | |
| 251 | } // namespace |
| 252 | |
| 253 | } // namespace ipmi |