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