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