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