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