John Wedig | 2098dab | 2021-09-14 13:56:28 -0700 | [diff] [blame] | 1 | |
| 2 | #include "estoraged.hpp" |
| 3 | |
John Edward Broadbent | 59dffa6 | 2022-01-13 17:41:32 -0800 | [diff] [blame] | 4 | #include "cryptErase.hpp" |
John Wedig | b810c92 | 2021-11-17 16:38:03 -0800 | [diff] [blame] | 5 | #include "cryptsetupInterface.hpp" |
John Edward Broadbent | 7f2ab64 | 2021-11-11 21:00:38 -0800 | [diff] [blame] | 6 | #include "pattern.hpp" |
John Edward Broadbent | 605085a | 2021-11-05 13:45:45 -0700 | [diff] [blame] | 7 | #include "sanitize.hpp" |
John Edward Broadbent | e6ffe70 | 2021-10-14 14:03:11 -0700 | [diff] [blame] | 8 | #include "verifyDriveGeometry.hpp" |
John Edward Broadbent | 4bc8a10 | 2021-12-30 16:11:49 -0800 | [diff] [blame] | 9 | #include "zero.hpp" |
John Edward Broadbent | 4e13b0a | 2021-11-15 15:21:59 -0800 | [diff] [blame] | 10 | |
John Wedig | b810c92 | 2021-11-17 16:38:03 -0800 | [diff] [blame] | 11 | #include <libcryptsetup.h> |
| 12 | #include <openssl/rand.h> |
John Wedig | b810c92 | 2021-11-17 16:38:03 -0800 | [diff] [blame] | 13 | |
| 14 | #include <phosphor-logging/lg2.hpp> |
John Wedig | 67a4744 | 2022-04-05 17:21:29 -0700 | [diff] [blame] | 15 | #include <sdbusplus/asio/object_server.hpp> |
John Wedig | 972c3fa | 2021-12-29 17:30:41 -0800 | [diff] [blame] | 16 | #include <xyz/openbmc_project/Common/error.hpp> |
John Wedig | b810c92 | 2021-11-17 16:38:03 -0800 | [diff] [blame] | 17 | |
Ed Tanous | 82897c3 | 2022-02-21 14:11:59 -0800 | [diff] [blame] | 18 | #include <cstdlib> |
John Wedig | b810c92 | 2021-11-17 16:38:03 -0800 | [diff] [blame] | 19 | #include <filesystem> |
John Wedig | 2098dab | 2021-09-14 13:56:28 -0700 | [diff] [blame] | 20 | #include <iostream> |
John Wedig | 67a4744 | 2022-04-05 17:21:29 -0700 | [diff] [blame] | 21 | #include <string> |
John Wedig | b810c92 | 2021-11-17 16:38:03 -0800 | [diff] [blame] | 22 | #include <string_view> |
John Wedig | 67a4744 | 2022-04-05 17:21:29 -0700 | [diff] [blame] | 23 | #include <utility> |
John Wedig | 2098dab | 2021-09-14 13:56:28 -0700 | [diff] [blame] | 24 | #include <vector> |
| 25 | |
| 26 | namespace estoraged |
| 27 | { |
| 28 | |
John Wedig | 6c0d8ce | 2022-04-22 14:00:43 -0700 | [diff] [blame] | 29 | using Association = std::tuple<std::string, std::string, std::string>; |
John Wedig | 972c3fa | 2021-12-29 17:30:41 -0800 | [diff] [blame] | 30 | using sdbusplus::xyz::openbmc_project::Common::Error::InternalFailure; |
John Wedig | 972c3fa | 2021-12-29 17:30:41 -0800 | [diff] [blame] | 31 | using sdbusplus::xyz::openbmc_project::Common::Error::UnsupportedRequest; |
John Edward Broadbent | 91c1ec1 | 2022-05-20 16:51:43 -0700 | [diff] [blame] | 32 | using sdbusplus::xyz::openbmc_project::Inventory::Item::server::Drive; |
John Wedig | 67a4744 | 2022-04-05 17:21:29 -0700 | [diff] [blame] | 33 | using sdbusplus::xyz::openbmc_project::Inventory::Item::server::Volume; |
John Wedig | b810c92 | 2021-11-17 16:38:03 -0800 | [diff] [blame] | 34 | |
John Wedig | 67a4744 | 2022-04-05 17:21:29 -0700 | [diff] [blame] | 35 | EStoraged::EStoraged(sdbusplus::asio::object_server& server, |
John Wedig | 6c0d8ce | 2022-04-22 14:00:43 -0700 | [diff] [blame] | 36 | const std::string& configPath, const std::string& devPath, |
| 37 | const std::string& luksName, uint64_t size, |
John Wedig | b483830 | 2022-07-22 13:51:16 -0700 | [diff] [blame] | 38 | uint8_t lifeTime, const std::string& partNumber, |
| 39 | const std::string& serialNumber, |
John Wedig | 67a4744 | 2022-04-05 17:21:29 -0700 | [diff] [blame] | 40 | std::unique_ptr<CryptsetupInterface> cryptInterface, |
| 41 | std::unique_ptr<FilesystemInterface> fsInterface) : |
| 42 | devPath(devPath), |
| 43 | containerName(luksName), mountPoint("/mnt/" + luksName + "_fs"), |
John Edward Broadbent | 6771c69 | 2022-06-22 19:49:27 -0700 | [diff] [blame] | 44 | cryptIface(std::move(cryptInterface)), fsIface(std::move(fsInterface)), |
John Wedig | 2443a02 | 2023-03-17 13:42:32 -0700 | [diff] [blame] | 45 | cryptDevicePath(cryptIface->cryptGetDir() + "/" + luksName), |
John Edward Broadbent | 6771c69 | 2022-06-22 19:49:27 -0700 | [diff] [blame] | 46 | objectServer(server) |
John Wedig | 67a4744 | 2022-04-05 17:21:29 -0700 | [diff] [blame] | 47 | { |
| 48 | /* Get the filename of the device (without "/dev/"). */ |
| 49 | std::string deviceName = std::filesystem::path(devPath).filename().string(); |
| 50 | /* DBus object path */ |
John Wedig | 6c0d8ce | 2022-04-22 14:00:43 -0700 | [diff] [blame] | 51 | std::string objectPath = |
| 52 | "/xyz/openbmc_project/inventory/storage/" + deviceName; |
John Wedig | 67a4744 | 2022-04-05 17:21:29 -0700 | [diff] [blame] | 53 | |
| 54 | /* Add Volume interface. */ |
| 55 | volumeInterface = objectServer.add_interface( |
John Wedig | 6c0d8ce | 2022-04-22 14:00:43 -0700 | [diff] [blame] | 56 | objectPath, "xyz.openbmc_project.Inventory.Item.Volume"); |
John Wedig | 67a4744 | 2022-04-05 17:21:29 -0700 | [diff] [blame] | 57 | volumeInterface->register_method( |
| 58 | "FormatLuks", [this](const std::vector<uint8_t>& password, |
| 59 | Volume::FilesystemType type) { |
| 60 | this->formatLuks(password, type); |
| 61 | }); |
| 62 | volumeInterface->register_method( |
| 63 | "Erase", |
| 64 | [this](Volume::EraseMethod eraseType) { this->erase(eraseType); }); |
| 65 | volumeInterface->register_method("Lock", [this]() { this->lock(); }); |
| 66 | volumeInterface->register_method( |
| 67 | "Unlock", |
| 68 | [this](std::vector<uint8_t>& password) { this->unlock(password); }); |
| 69 | volumeInterface->register_method( |
| 70 | "ChangePassword", [this](const std::vector<uint8_t>& oldPassword, |
| 71 | const std::vector<uint8_t>& newPassword) { |
| 72 | this->changePassword(oldPassword, newPassword); |
| 73 | }); |
| 74 | volumeInterface->register_property_r( |
| 75 | "Locked", lockedProperty, sdbusplus::vtable::property_::emits_change, |
| 76 | [this](bool& value) { |
| 77 | value = this->isLocked(); |
| 78 | return value; |
| 79 | }); |
| 80 | |
| 81 | /* Add Drive interface. */ |
| 82 | driveInterface = objectServer.add_interface( |
John Wedig | 6c0d8ce | 2022-04-22 14:00:43 -0700 | [diff] [blame] | 83 | objectPath, "xyz.openbmc_project.Inventory.Item.Drive"); |
John Wedig | 67a4744 | 2022-04-05 17:21:29 -0700 | [diff] [blame] | 84 | driveInterface->register_property("Capacity", size); |
John Edward Broadbent | 5d799bb | 2022-03-22 16:14:24 -0700 | [diff] [blame] | 85 | driveInterface->register_property("PredictedMediaLifeLeftPercent", |
| 86 | lifeTime); |
John Edward Broadbent | 14aee77 | 2022-04-20 13:46:48 -0700 | [diff] [blame] | 87 | /* This registers the Locked property for the Drives interface. |
| 88 | * Now it is the same as the volume Locked property */ |
| 89 | driveInterface->register_property_r( |
| 90 | "Locked", lockedProperty, sdbusplus::vtable::property_::emits_change, |
| 91 | [this](bool& value) { |
| 92 | value = this->isLocked(); |
| 93 | return value; |
| 94 | }); |
John Wedig | 67a4744 | 2022-04-05 17:21:29 -0700 | [diff] [blame] | 95 | |
John Edward Broadbent | 91c1ec1 | 2022-05-20 16:51:43 -0700 | [diff] [blame] | 96 | driveInterface->register_property_r( |
| 97 | "EncryptionStatus", encryptionStatus, |
| 98 | sdbusplus::vtable::property_::emits_change, |
| 99 | [this](Drive::DriveEncryptionState& value) { |
| 100 | value = this->findEncryptionStatus(); |
| 101 | return value; |
| 102 | }); |
| 103 | |
John Edward Broadbent | 4979641 | 2022-06-22 18:31:52 -0700 | [diff] [blame] | 104 | embeddedLocationInterface = objectServer.add_interface( |
| 105 | objectPath, "xyz.openbmc_project.Inventory.Connector.Embedded"); |
John Edward Broadbent | 740e94b | 2022-06-10 19:42:30 -0700 | [diff] [blame] | 106 | |
John Wedig | b483830 | 2022-07-22 13:51:16 -0700 | [diff] [blame] | 107 | /* Add Asset interface. */ |
| 108 | assetInterface = objectServer.add_interface( |
| 109 | objectPath, "xyz.openbmc_project.Inventory.Decorator.Asset"); |
| 110 | assetInterface->register_property("PartNumber", partNumber); |
| 111 | assetInterface->register_property("SerialNumber", serialNumber); |
| 112 | |
John Wedig | 67a4744 | 2022-04-05 17:21:29 -0700 | [diff] [blame] | 113 | volumeInterface->initialize(); |
| 114 | driveInterface->initialize(); |
John Edward Broadbent | 4979641 | 2022-06-22 18:31:52 -0700 | [diff] [blame] | 115 | embeddedLocationInterface->initialize(); |
John Wedig | b483830 | 2022-07-22 13:51:16 -0700 | [diff] [blame] | 116 | assetInterface->initialize(); |
John Wedig | 6c0d8ce | 2022-04-22 14:00:43 -0700 | [diff] [blame] | 117 | |
| 118 | /* Set up the association between chassis and drive. */ |
| 119 | association = objectServer.add_interface( |
| 120 | objectPath, "xyz.openbmc_project.Association.Definitions"); |
| 121 | |
| 122 | std::vector<Association> associations; |
| 123 | associations.emplace_back("chassis", "drive", |
| 124 | std::filesystem::path(configPath).parent_path()); |
| 125 | association->register_property("Associations", associations); |
| 126 | association->initialize(); |
John Wedig | 67a4744 | 2022-04-05 17:21:29 -0700 | [diff] [blame] | 127 | } |
| 128 | |
| 129 | EStoraged::~EStoraged() |
| 130 | { |
| 131 | objectServer.remove_interface(volumeInterface); |
| 132 | objectServer.remove_interface(driveInterface); |
John Edward Broadbent | 4979641 | 2022-06-22 18:31:52 -0700 | [diff] [blame] | 133 | objectServer.remove_interface(embeddedLocationInterface); |
John Wedig | b483830 | 2022-07-22 13:51:16 -0700 | [diff] [blame] | 134 | objectServer.remove_interface(assetInterface); |
John Wedig | 6c0d8ce | 2022-04-22 14:00:43 -0700 | [diff] [blame] | 135 | objectServer.remove_interface(association); |
John Wedig | 67a4744 | 2022-04-05 17:21:29 -0700 | [diff] [blame] | 136 | } |
| 137 | |
| 138 | void EStoraged::formatLuks(const std::vector<uint8_t>& password, |
| 139 | Volume::FilesystemType type) |
John Wedig | 2098dab | 2021-09-14 13:56:28 -0700 | [diff] [blame] | 140 | { |
John Edward Broadbent | 4e13b0a | 2021-11-15 15:21:59 -0800 | [diff] [blame] | 141 | std::string msg = "OpenBMC.0.1.DriveFormat"; |
| 142 | lg2::info("Starting format", "REDFISH_MESSAGE_ID", msg); |
John Wedig | b810c92 | 2021-11-17 16:38:03 -0800 | [diff] [blame] | 143 | |
John Wedig | 67a4744 | 2022-04-05 17:21:29 -0700 | [diff] [blame] | 144 | if (type != Volume::FilesystemType::ext4) |
John Wedig | 972c3fa | 2021-12-29 17:30:41 -0800 | [diff] [blame] | 145 | { |
| 146 | lg2::error("Only ext4 filesystems are supported currently", |
| 147 | "REDFISH_MESSAGE_ID", std::string("OpenBMC.0.1.FormatFail")); |
| 148 | throw UnsupportedRequest(); |
| 149 | } |
| 150 | |
John Edward Broadbent | b2c86be | 2022-04-15 11:45:53 -0700 | [diff] [blame] | 151 | formatLuksDev(password); |
| 152 | activateLuksDev(password); |
John Wedig | b810c92 | 2021-11-17 16:38:03 -0800 | [diff] [blame] | 153 | |
| 154 | createFilesystem(); |
| 155 | mountFilesystem(); |
John Wedig | 2098dab | 2021-09-14 13:56:28 -0700 | [diff] [blame] | 156 | } |
| 157 | |
John Wedig | 67a4744 | 2022-04-05 17:21:29 -0700 | [diff] [blame] | 158 | void EStoraged::erase(Volume::EraseMethod inEraseMethod) |
John Wedig | 2098dab | 2021-09-14 13:56:28 -0700 | [diff] [blame] | 159 | { |
| 160 | std::cerr << "Erasing encrypted eMMC" << std::endl; |
John Edward Broadbent | e6ffe70 | 2021-10-14 14:03:11 -0700 | [diff] [blame] | 161 | lg2::info("Starting erase", "REDFISH_MESSAGE_ID", |
| 162 | std::string("OpenBMC.0.1.DriveErase")); |
| 163 | switch (inEraseMethod) |
| 164 | { |
John Wedig | 67a4744 | 2022-04-05 17:21:29 -0700 | [diff] [blame] | 165 | case Volume::EraseMethod::CryptoErase: |
John Edward Broadbent | e6ffe70 | 2021-10-14 14:03:11 -0700 | [diff] [blame] | 166 | { |
John Edward Broadbent | 59dffa6 | 2022-01-13 17:41:32 -0800 | [diff] [blame] | 167 | CryptErase myCryptErase(devPath); |
| 168 | myCryptErase.doErase(); |
John Edward Broadbent | e6ffe70 | 2021-10-14 14:03:11 -0700 | [diff] [blame] | 169 | break; |
| 170 | } |
John Wedig | 67a4744 | 2022-04-05 17:21:29 -0700 | [diff] [blame] | 171 | case Volume::EraseMethod::VerifyGeometry: |
John Edward Broadbent | e6ffe70 | 2021-10-14 14:03:11 -0700 | [diff] [blame] | 172 | { |
| 173 | VerifyDriveGeometry myVerifyGeometry(devPath); |
John Edward Broadbent | a6e3b99 | 2022-03-17 14:33:15 -0700 | [diff] [blame] | 174 | myVerifyGeometry.geometryOkay(); |
John Edward Broadbent | e6ffe70 | 2021-10-14 14:03:11 -0700 | [diff] [blame] | 175 | break; |
| 176 | } |
John Wedig | 67a4744 | 2022-04-05 17:21:29 -0700 | [diff] [blame] | 177 | case Volume::EraseMethod::LogicalOverWrite: |
John Edward Broadbent | e6ffe70 | 2021-10-14 14:03:11 -0700 | [diff] [blame] | 178 | { |
John Edward Broadbent | 7f2ab64 | 2021-11-11 21:00:38 -0800 | [diff] [blame] | 179 | Pattern myErasePattern(devPath); |
John Edward Broadbent | a6e3b99 | 2022-03-17 14:33:15 -0700 | [diff] [blame] | 180 | myErasePattern.writePattern(); |
John Edward Broadbent | e6ffe70 | 2021-10-14 14:03:11 -0700 | [diff] [blame] | 181 | break; |
| 182 | } |
John Wedig | 67a4744 | 2022-04-05 17:21:29 -0700 | [diff] [blame] | 183 | case Volume::EraseMethod::LogicalVerify: |
John Edward Broadbent | e6ffe70 | 2021-10-14 14:03:11 -0700 | [diff] [blame] | 184 | { |
John Edward Broadbent | 7f2ab64 | 2021-11-11 21:00:38 -0800 | [diff] [blame] | 185 | Pattern myErasePattern(devPath); |
John Edward Broadbent | a6e3b99 | 2022-03-17 14:33:15 -0700 | [diff] [blame] | 186 | myErasePattern.verifyPattern(); |
John Edward Broadbent | e6ffe70 | 2021-10-14 14:03:11 -0700 | [diff] [blame] | 187 | break; |
| 188 | } |
John Wedig | 67a4744 | 2022-04-05 17:21:29 -0700 | [diff] [blame] | 189 | case Volume::EraseMethod::VendorSanitize: |
John Edward Broadbent | e6ffe70 | 2021-10-14 14:03:11 -0700 | [diff] [blame] | 190 | { |
John Edward Broadbent | 605085a | 2021-11-05 13:45:45 -0700 | [diff] [blame] | 191 | Sanitize mySanitize(devPath); |
| 192 | mySanitize.doSanitize(); |
John Edward Broadbent | e6ffe70 | 2021-10-14 14:03:11 -0700 | [diff] [blame] | 193 | break; |
| 194 | } |
John Wedig | 67a4744 | 2022-04-05 17:21:29 -0700 | [diff] [blame] | 195 | case Volume::EraseMethod::ZeroOverWrite: |
John Edward Broadbent | e6ffe70 | 2021-10-14 14:03:11 -0700 | [diff] [blame] | 196 | { |
John Edward Broadbent | 4bc8a10 | 2021-12-30 16:11:49 -0800 | [diff] [blame] | 197 | Zero myZero(devPath); |
John Edward Broadbent | a6e3b99 | 2022-03-17 14:33:15 -0700 | [diff] [blame] | 198 | myZero.writeZero(); |
John Edward Broadbent | e6ffe70 | 2021-10-14 14:03:11 -0700 | [diff] [blame] | 199 | break; |
| 200 | } |
John Wedig | 67a4744 | 2022-04-05 17:21:29 -0700 | [diff] [blame] | 201 | case Volume::EraseMethod::ZeroVerify: |
John Edward Broadbent | e6ffe70 | 2021-10-14 14:03:11 -0700 | [diff] [blame] | 202 | { |
John Edward Broadbent | 4bc8a10 | 2021-12-30 16:11:49 -0800 | [diff] [blame] | 203 | Zero myZero(devPath); |
John Edward Broadbent | a6e3b99 | 2022-03-17 14:33:15 -0700 | [diff] [blame] | 204 | myZero.verifyZero(); |
John Edward Broadbent | e6ffe70 | 2021-10-14 14:03:11 -0700 | [diff] [blame] | 205 | break; |
| 206 | } |
John Wedig | 67a4744 | 2022-04-05 17:21:29 -0700 | [diff] [blame] | 207 | case Volume::EraseMethod::SecuredLocked: |
John Edward Broadbent | e6ffe70 | 2021-10-14 14:03:11 -0700 | [diff] [blame] | 208 | { |
John Wedig | 47cd799 | 2022-10-05 15:45:11 -0700 | [diff] [blame] | 209 | if (!isLocked()) |
John Edward Broadbent | f59b729 | 2022-02-15 15:07:15 -0800 | [diff] [blame] | 210 | { |
| 211 | lock(); |
| 212 | } |
| 213 | // TODO: implement hardware locking |
| 214 | // Until that is done, we can lock using eStoraged::lock() |
John Edward Broadbent | e6ffe70 | 2021-10-14 14:03:11 -0700 | [diff] [blame] | 215 | break; |
| 216 | } |
| 217 | } |
John Wedig | 2098dab | 2021-09-14 13:56:28 -0700 | [diff] [blame] | 218 | } |
| 219 | |
Ed Tanous | 82897c3 | 2022-02-21 14:11:59 -0800 | [diff] [blame] | 220 | void EStoraged::lock() |
John Wedig | 2098dab | 2021-09-14 13:56:28 -0700 | [diff] [blame] | 221 | { |
John Edward Broadbent | 4e13b0a | 2021-11-15 15:21:59 -0800 | [diff] [blame] | 222 | std::string msg = "OpenBMC.0.1.DriveLock"; |
| 223 | lg2::info("Starting lock", "REDFISH_MESSAGE_ID", msg); |
John Wedig | b810c92 | 2021-11-17 16:38:03 -0800 | [diff] [blame] | 224 | |
| 225 | unmountFilesystem(); |
| 226 | deactivateLuksDev(); |
John Wedig | 2098dab | 2021-09-14 13:56:28 -0700 | [diff] [blame] | 227 | } |
| 228 | |
Ed Tanous | 82897c3 | 2022-02-21 14:11:59 -0800 | [diff] [blame] | 229 | void EStoraged::unlock(std::vector<uint8_t> password) |
John Wedig | 2098dab | 2021-09-14 13:56:28 -0700 | [diff] [blame] | 230 | { |
John Edward Broadbent | 4e13b0a | 2021-11-15 15:21:59 -0800 | [diff] [blame] | 231 | std::string msg = "OpenBMC.0.1.DriveUnlock"; |
| 232 | lg2::info("Starting unlock", "REDFISH_MESSAGE_ID", msg); |
John Wedig | b810c92 | 2021-11-17 16:38:03 -0800 | [diff] [blame] | 233 | |
John Edward Broadbent | b2c86be | 2022-04-15 11:45:53 -0700 | [diff] [blame] | 234 | activateLuksDev(std::move(password)); |
John Wedig | b810c92 | 2021-11-17 16:38:03 -0800 | [diff] [blame] | 235 | mountFilesystem(); |
John Wedig | 2098dab | 2021-09-14 13:56:28 -0700 | [diff] [blame] | 236 | } |
| 237 | |
John Wedig | 8d5a3a0 | 2022-09-29 15:25:58 -0700 | [diff] [blame] | 238 | void EStoraged::changePassword(const std::vector<uint8_t>& oldPassword, |
| 239 | const std::vector<uint8_t>& newPassword) |
John Wedig | 2098dab | 2021-09-14 13:56:28 -0700 | [diff] [blame] | 240 | { |
John Edward Broadbent | e6ffe70 | 2021-10-14 14:03:11 -0700 | [diff] [blame] | 241 | lg2::info("Starting change password", "REDFISH_MESSAGE_ID", |
| 242 | std::string("OpenBMC.0.1.DrivePasswordChanged")); |
John Wedig | 8d5a3a0 | 2022-09-29 15:25:58 -0700 | [diff] [blame] | 243 | |
| 244 | CryptHandle cryptHandle = loadLuksHeader(); |
| 245 | |
| 246 | int retval = cryptIface->cryptKeyslotChangeByPassphrase( |
| 247 | cryptHandle.get(), CRYPT_ANY_SLOT, CRYPT_ANY_SLOT, |
| 248 | reinterpret_cast<const char*>(oldPassword.data()), oldPassword.size(), |
| 249 | reinterpret_cast<const char*>(newPassword.data()), newPassword.size()); |
| 250 | if (retval < 0) |
| 251 | { |
| 252 | lg2::error("Failed to change password", "REDFISH_MESSAGE_ID", |
| 253 | std::string("OpenBMC.0.1.DrivePasswordChangeFail")); |
| 254 | throw InternalFailure(); |
| 255 | } |
| 256 | |
| 257 | lg2::info("Successfully changed password for {DEV}", "DEV", devPath, |
| 258 | "REDFISH_MESSAGE_ID", |
| 259 | std::string("OpenBMC.0.1.DrivePasswordChangeSuccess")); |
John Wedig | 2098dab | 2021-09-14 13:56:28 -0700 | [diff] [blame] | 260 | } |
| 261 | |
Ed Tanous | 82897c3 | 2022-02-21 14:11:59 -0800 | [diff] [blame] | 262 | bool EStoraged::isLocked() const |
John Wedig | b810c92 | 2021-11-17 16:38:03 -0800 | [diff] [blame] | 263 | { |
John Wedig | 2443a02 | 2023-03-17 13:42:32 -0700 | [diff] [blame] | 264 | /* |
| 265 | * Check if the mapped virtual device exists. If it exists, the LUKS volume |
| 266 | * is unlocked. |
| 267 | */ |
| 268 | try |
| 269 | { |
| 270 | std::filesystem::path mappedDevicePath(cryptDevicePath); |
| 271 | return (std::filesystem::exists(mappedDevicePath) == false); |
| 272 | } |
| 273 | catch (const std::exception& e) |
| 274 | { |
| 275 | lg2::error("Failed to query locked status: {EXCEPT}", "EXCEPT", |
| 276 | e.what(), "REDFISH_MESSAGE_ID", |
| 277 | std::string("OpenBMC.0.1.IsLockedFail")); |
| 278 | /* If we couldn't query the filesystem path, assume unlocked. */ |
| 279 | return false; |
| 280 | } |
John Wedig | b810c92 | 2021-11-17 16:38:03 -0800 | [diff] [blame] | 281 | } |
| 282 | |
Ed Tanous | 82897c3 | 2022-02-21 14:11:59 -0800 | [diff] [blame] | 283 | std::string_view EStoraged::getMountPoint() const |
John Wedig | b810c92 | 2021-11-17 16:38:03 -0800 | [diff] [blame] | 284 | { |
| 285 | return mountPoint; |
| 286 | } |
| 287 | |
John Edward Broadbent | b2c86be | 2022-04-15 11:45:53 -0700 | [diff] [blame] | 288 | void EStoraged::formatLuksDev(std::vector<uint8_t> password) |
John Wedig | b810c92 | 2021-11-17 16:38:03 -0800 | [diff] [blame] | 289 | { |
| 290 | lg2::info("Formatting device {DEV}", "DEV", devPath, "REDFISH_MESSAGE_ID", |
| 291 | std::string("OpenBMC.0.1.FormatLuksDev")); |
| 292 | |
| 293 | /* Generate the volume key. */ |
| 294 | const std::size_t keySize = 64; |
| 295 | std::vector<uint8_t> volumeKey(keySize); |
| 296 | if (RAND_bytes(volumeKey.data(), keySize) != 1) |
| 297 | { |
| 298 | lg2::error("Failed to create volume key", "REDFISH_MESSAGE_ID", |
| 299 | std::string("OpenBMC.0.1.FormatLuksDevFail")); |
John Wedig | 972c3fa | 2021-12-29 17:30:41 -0800 | [diff] [blame] | 300 | throw InternalFailure(); |
John Wedig | b810c92 | 2021-11-17 16:38:03 -0800 | [diff] [blame] | 301 | } |
John Edward Broadbent | b2c86be | 2022-04-15 11:45:53 -0700 | [diff] [blame] | 302 | |
| 303 | /* Create the handle. */ |
| 304 | CryptHandle cryptHandle(devPath); |
| 305 | |
John Wedig | b810c92 | 2021-11-17 16:38:03 -0800 | [diff] [blame] | 306 | /* Format the LUKS encrypted device. */ |
John Edward Broadbent | b2c86be | 2022-04-15 11:45:53 -0700 | [diff] [blame] | 307 | int retval = cryptIface->cryptFormat( |
| 308 | cryptHandle.get(), CRYPT_LUKS2, "aes", "xts-plain64", nullptr, |
| 309 | reinterpret_cast<const char*>(volumeKey.data()), volumeKey.size(), |
| 310 | nullptr); |
John Wedig | b810c92 | 2021-11-17 16:38:03 -0800 | [diff] [blame] | 311 | if (retval < 0) |
| 312 | { |
| 313 | lg2::error("Failed to format encrypted device: {RETVAL}", "RETVAL", |
| 314 | retval, "REDFISH_MESSAGE_ID", |
| 315 | std::string("OpenBMC.0.1.FormatLuksDevFail")); |
John Wedig | 972c3fa | 2021-12-29 17:30:41 -0800 | [diff] [blame] | 316 | throw InternalFailure(); |
John Wedig | b810c92 | 2021-11-17 16:38:03 -0800 | [diff] [blame] | 317 | } |
| 318 | |
John Wedig | b810c92 | 2021-11-17 16:38:03 -0800 | [diff] [blame] | 319 | /* Set the password. */ |
| 320 | retval = cryptIface->cryptKeyslotAddByVolumeKey( |
John Edward Broadbent | b2c86be | 2022-04-15 11:45:53 -0700 | [diff] [blame] | 321 | cryptHandle.get(), CRYPT_ANY_SLOT, nullptr, 0, |
John Wedig | b810c92 | 2021-11-17 16:38:03 -0800 | [diff] [blame] | 322 | reinterpret_cast<const char*>(password.data()), password.size()); |
| 323 | |
| 324 | if (retval < 0) |
| 325 | { |
| 326 | lg2::error("Failed to set encryption password", "REDFISH_MESSAGE_ID", |
| 327 | std::string("OpenBMC.0.1.FormatLuksDevFail")); |
John Wedig | 972c3fa | 2021-12-29 17:30:41 -0800 | [diff] [blame] | 328 | throw InternalFailure(); |
John Wedig | b810c92 | 2021-11-17 16:38:03 -0800 | [diff] [blame] | 329 | } |
| 330 | |
| 331 | lg2::info("Encrypted device {DEV} successfully formatted", "DEV", devPath, |
| 332 | "REDFISH_MESSAGE_ID", |
| 333 | std::string("OpenBMC.0.1.FormatLuksDevSuccess")); |
| 334 | } |
| 335 | |
John Edward Broadbent | 91c1ec1 | 2022-05-20 16:51:43 -0700 | [diff] [blame] | 336 | CryptHandle EStoraged::loadLuksHeader() |
John Wedig | b810c92 | 2021-11-17 16:38:03 -0800 | [diff] [blame] | 337 | { |
John Wedig | b810c92 | 2021-11-17 16:38:03 -0800 | [diff] [blame] | 338 | |
John Edward Broadbent | b2c86be | 2022-04-15 11:45:53 -0700 | [diff] [blame] | 339 | CryptHandle cryptHandle(devPath); |
| 340 | |
| 341 | int retval = cryptIface->cryptLoad(cryptHandle.get(), CRYPT_LUKS2, nullptr); |
John Wedig | b810c92 | 2021-11-17 16:38:03 -0800 | [diff] [blame] | 342 | if (retval < 0) |
| 343 | { |
| 344 | lg2::error("Failed to load LUKS header: {RETVAL}", "RETVAL", retval, |
| 345 | "REDFISH_MESSAGE_ID", |
| 346 | std::string("OpenBMC.0.1.ActivateLuksDevFail")); |
John Wedig | 972c3fa | 2021-12-29 17:30:41 -0800 | [diff] [blame] | 347 | throw InternalFailure(); |
John Wedig | b810c92 | 2021-11-17 16:38:03 -0800 | [diff] [blame] | 348 | } |
John Edward Broadbent | 91c1ec1 | 2022-05-20 16:51:43 -0700 | [diff] [blame] | 349 | return cryptHandle; |
| 350 | } |
John Wedig | b810c92 | 2021-11-17 16:38:03 -0800 | [diff] [blame] | 351 | |
John Edward Broadbent | 91c1ec1 | 2022-05-20 16:51:43 -0700 | [diff] [blame] | 352 | Drive::DriveEncryptionState EStoraged::findEncryptionStatus() |
| 353 | { |
| 354 | try |
| 355 | { |
| 356 | loadLuksHeader(); |
| 357 | return Drive::DriveEncryptionState::Encrypted; |
| 358 | } |
| 359 | catch (...) |
| 360 | { |
| 361 | return Drive::DriveEncryptionState::Unknown; |
| 362 | } |
| 363 | } |
| 364 | |
| 365 | void EStoraged::activateLuksDev(std::vector<uint8_t> password) |
| 366 | { |
| 367 | lg2::info("Activating LUKS dev {DEV}", "DEV", devPath, "REDFISH_MESSAGE_ID", |
| 368 | std::string("OpenBMC.0.1.ActivateLuksDev")); |
| 369 | |
| 370 | /* Create the handle. */ |
| 371 | CryptHandle cryptHandle = loadLuksHeader(); |
| 372 | |
| 373 | int retval = cryptIface->cryptActivateByPassphrase( |
John Edward Broadbent | b2c86be | 2022-04-15 11:45:53 -0700 | [diff] [blame] | 374 | cryptHandle.get(), containerName.c_str(), CRYPT_ANY_SLOT, |
John Wedig | b810c92 | 2021-11-17 16:38:03 -0800 | [diff] [blame] | 375 | reinterpret_cast<const char*>(password.data()), password.size(), 0); |
| 376 | |
| 377 | if (retval < 0) |
| 378 | { |
| 379 | lg2::error("Failed to activate LUKS dev: {RETVAL}", "RETVAL", retval, |
| 380 | "REDFISH_MESSAGE_ID", |
| 381 | std::string("OpenBMC.0.1.ActivateLuksDevFail")); |
John Wedig | 972c3fa | 2021-12-29 17:30:41 -0800 | [diff] [blame] | 382 | throw InternalFailure(); |
John Wedig | b810c92 | 2021-11-17 16:38:03 -0800 | [diff] [blame] | 383 | } |
| 384 | |
John Wedig | b810c92 | 2021-11-17 16:38:03 -0800 | [diff] [blame] | 385 | lg2::info("Successfully activated LUKS dev {DEV}", "DEV", devPath, |
| 386 | "REDFISH_MESSAGE_ID", |
| 387 | std::string("OpenBMC.0.1.ActivateLuksDevSuccess")); |
| 388 | } |
| 389 | |
Ed Tanous | 82897c3 | 2022-02-21 14:11:59 -0800 | [diff] [blame] | 390 | void EStoraged::createFilesystem() |
John Wedig | b810c92 | 2021-11-17 16:38:03 -0800 | [diff] [blame] | 391 | { |
| 392 | /* Run the command to create the filesystem. */ |
John Wedig | 2443a02 | 2023-03-17 13:42:32 -0700 | [diff] [blame] | 393 | int retval = fsIface->runMkfs(cryptDevicePath); |
Ed Tanous | 82897c3 | 2022-02-21 14:11:59 -0800 | [diff] [blame] | 394 | if (retval != 0) |
John Wedig | b810c92 | 2021-11-17 16:38:03 -0800 | [diff] [blame] | 395 | { |
| 396 | lg2::error("Failed to create filesystem: {RETVAL}", "RETVAL", retval, |
| 397 | "REDFISH_MESSAGE_ID", |
| 398 | std::string("OpenBMC.0.1.CreateFilesystemFail")); |
John Wedig | 972c3fa | 2021-12-29 17:30:41 -0800 | [diff] [blame] | 399 | throw InternalFailure(); |
John Wedig | b810c92 | 2021-11-17 16:38:03 -0800 | [diff] [blame] | 400 | } |
John Wedig | 2443a02 | 2023-03-17 13:42:32 -0700 | [diff] [blame] | 401 | lg2::info("Successfully created filesystem for {CONTAINER}", "CONTAINER", |
| 402 | cryptDevicePath, "REDFISH_MESSAGE_ID", |
John Wedig | b810c92 | 2021-11-17 16:38:03 -0800 | [diff] [blame] | 403 | std::string("OpenBMC.0.1.CreateFilesystemSuccess")); |
| 404 | } |
| 405 | |
Ed Tanous | 82897c3 | 2022-02-21 14:11:59 -0800 | [diff] [blame] | 406 | void EStoraged::mountFilesystem() |
John Wedig | b810c92 | 2021-11-17 16:38:03 -0800 | [diff] [blame] | 407 | { |
John Wedig | b17f825 | 2022-01-12 14:24:26 -0800 | [diff] [blame] | 408 | /* |
| 409 | * Create directory for the filesystem, if it's not already present. It |
| 410 | * might already exist if, for example, the BMC reboots after creating the |
| 411 | * directory. |
| 412 | */ |
| 413 | if (!fsIface->directoryExists(std::filesystem::path(mountPoint))) |
John Wedig | b810c92 | 2021-11-17 16:38:03 -0800 | [diff] [blame] | 414 | { |
John Wedig | b17f825 | 2022-01-12 14:24:26 -0800 | [diff] [blame] | 415 | bool success = |
| 416 | fsIface->createDirectory(std::filesystem::path(mountPoint)); |
| 417 | if (!success) |
| 418 | { |
| 419 | lg2::error("Failed to create mount point: {DIR}", "DIR", mountPoint, |
| 420 | "REDFISH_MESSAGE_ID", |
| 421 | std::string("OpenBMC.0.1.MountFilesystemFail")); |
| 422 | throw InternalFailure(); |
| 423 | } |
John Wedig | b810c92 | 2021-11-17 16:38:03 -0800 | [diff] [blame] | 424 | } |
| 425 | |
| 426 | /* Run the command to mount the filesystem. */ |
John Wedig | 2443a02 | 2023-03-17 13:42:32 -0700 | [diff] [blame] | 427 | int retval = fsIface->doMount(cryptDevicePath.c_str(), mountPoint.c_str(), |
John Wedig | b810c92 | 2021-11-17 16:38:03 -0800 | [diff] [blame] | 428 | "ext4", 0, nullptr); |
Ed Tanous | 82897c3 | 2022-02-21 14:11:59 -0800 | [diff] [blame] | 429 | if (retval != 0) |
John Wedig | b810c92 | 2021-11-17 16:38:03 -0800 | [diff] [blame] | 430 | { |
| 431 | lg2::error("Failed to mount filesystem: {RETVAL}", "RETVAL", retval, |
| 432 | "REDFISH_MESSAGE_ID", |
| 433 | std::string("OpenBMC.0.1.MountFilesystemFail")); |
| 434 | bool removeSuccess = |
| 435 | fsIface->removeDirectory(std::filesystem::path(mountPoint)); |
| 436 | if (!removeSuccess) |
| 437 | { |
| 438 | lg2::error("Failed to remove mount point: {DIR}", "DIR", mountPoint, |
| 439 | "REDFISH_MESSAGE_ID", |
| 440 | std::string("OpenBMC.0.1.MountFilesystemFail")); |
| 441 | } |
John Wedig | 972c3fa | 2021-12-29 17:30:41 -0800 | [diff] [blame] | 442 | throw InternalFailure(); |
John Wedig | b810c92 | 2021-11-17 16:38:03 -0800 | [diff] [blame] | 443 | } |
| 444 | |
| 445 | lg2::info("Successfully mounted filesystem at {DIR}", "DIR", mountPoint, |
| 446 | "REDFISH_MESSAGE_ID", |
| 447 | std::string("OpenBMC.0.1.MountFilesystemSuccess")); |
| 448 | } |
| 449 | |
Ed Tanous | 82897c3 | 2022-02-21 14:11:59 -0800 | [diff] [blame] | 450 | void EStoraged::unmountFilesystem() |
John Wedig | b810c92 | 2021-11-17 16:38:03 -0800 | [diff] [blame] | 451 | { |
| 452 | int retval = fsIface->doUnmount(mountPoint.c_str()); |
Ed Tanous | 82897c3 | 2022-02-21 14:11:59 -0800 | [diff] [blame] | 453 | if (retval != 0) |
John Wedig | b810c92 | 2021-11-17 16:38:03 -0800 | [diff] [blame] | 454 | { |
| 455 | lg2::error("Failed to unmount filesystem: {RETVAL}", "RETVAL", retval, |
| 456 | "REDFISH_MESSAGE_ID", |
| 457 | std::string("OpenBMC.0.1.UnmountFilesystemFail")); |
John Wedig | 972c3fa | 2021-12-29 17:30:41 -0800 | [diff] [blame] | 458 | throw InternalFailure(); |
John Wedig | b810c92 | 2021-11-17 16:38:03 -0800 | [diff] [blame] | 459 | } |
| 460 | |
| 461 | /* Remove the mount point. */ |
| 462 | bool success = fsIface->removeDirectory(std::filesystem::path(mountPoint)); |
| 463 | if (!success) |
| 464 | { |
| 465 | lg2::error("Failed to remove mount point {DIR}", "DIR", mountPoint, |
| 466 | "REDFISH_MESSAGE_ID", |
| 467 | std::string("OpenBMC.0.1.UnmountFilesystemFail")); |
John Wedig | 972c3fa | 2021-12-29 17:30:41 -0800 | [diff] [blame] | 468 | throw InternalFailure(); |
John Wedig | b810c92 | 2021-11-17 16:38:03 -0800 | [diff] [blame] | 469 | } |
| 470 | |
| 471 | lg2::info("Successfully unmounted filesystem at {DIR}", "DIR", mountPoint, |
| 472 | "REDFISH_MESSAGE_ID", |
| 473 | std::string("OpenBMC.0.1.MountFilesystemSuccess")); |
| 474 | } |
| 475 | |
Ed Tanous | 82897c3 | 2022-02-21 14:11:59 -0800 | [diff] [blame] | 476 | void EStoraged::deactivateLuksDev() |
John Wedig | b810c92 | 2021-11-17 16:38:03 -0800 | [diff] [blame] | 477 | { |
| 478 | lg2::info("Deactivating LUKS device {DEV}", "DEV", devPath, |
| 479 | "REDFISH_MESSAGE_ID", |
| 480 | std::string("OpenBMC.0.1.DeactivateLuksDev")); |
| 481 | |
| 482 | int retval = cryptIface->cryptDeactivate(nullptr, containerName.c_str()); |
| 483 | if (retval < 0) |
| 484 | { |
| 485 | lg2::error("Failed to deactivate crypt device: {RETVAL}", "RETVAL", |
| 486 | retval, "REDFISH_MESSAGE_ID", |
| 487 | std::string("OpenBMC.0.1.DeactivateLuksDevFail")); |
John Wedig | 972c3fa | 2021-12-29 17:30:41 -0800 | [diff] [blame] | 488 | throw InternalFailure(); |
John Wedig | b810c92 | 2021-11-17 16:38:03 -0800 | [diff] [blame] | 489 | } |
| 490 | |
John Wedig | b810c92 | 2021-11-17 16:38:03 -0800 | [diff] [blame] | 491 | lg2::info("Successfully deactivated LUKS device {DEV}", "DEV", devPath, |
| 492 | "REDFISH_MESSAGE_ID", |
| 493 | std::string("OpenBMC.0.1.DeactivateLuksDevSuccess")); |
| 494 | } |
| 495 | |
John Wedig | 2443a02 | 2023-03-17 13:42:32 -0700 | [diff] [blame] | 496 | std::string_view EStoraged::getCryptDevicePath() const |
John Wedig | 67a4744 | 2022-04-05 17:21:29 -0700 | [diff] [blame] | 497 | { |
John Wedig | 2443a02 | 2023-03-17 13:42:32 -0700 | [diff] [blame] | 498 | return cryptDevicePath; |
John Wedig | 67a4744 | 2022-04-05 17:21:29 -0700 | [diff] [blame] | 499 | } |
| 500 | |
John Wedig | 2098dab | 2021-09-14 13:56:28 -0700 | [diff] [blame] | 501 | } // namespace estoraged |