2 * Copyright (c) 2015-2017 Samsung Electronics Co., Ltd All Rights Reserved
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
8 * http://www.apache.org/licenses/LICENSE-2.0
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License
23 #include <sys/mount.h>
24 #include <sys/reboot.h>
30 #include <tzplatform_config.h>
31 #include <klay/process.h>
32 #include <klay/file-user.h>
33 #include <klay/filesystem.h>
34 #include <klay/dbus/connection.h>
38 #include "progress-bar.h"
40 #include "internal-encryption.h"
46 const char *INTERNAL_DEV_PATH = "/dev/disk/by-partlabel";
47 const char *INTERNAL_DEV_NAME = "USER";
48 const char *INTERNAL_PATH = "/opt/usr";
50 const char *PRIVILEGE_PLATFORM = "http://tizen.org/privilege/internal/default/platform";
52 // TODO: see recovery()
53 const std::string PROG_FACTORY_RESET = "/usr/bin/dbus-send";
54 const std::vector<std::string> wipeCommand = {
59 "--dest=com.samsung.factoryreset",
60 "/com/samsung/factoryreset",
61 "com.samsung.factoryreset.start.setting"
64 std::string findDevPath()
66 std::string source = INTERNAL_DEV_PATH + std::string("/") + INTERNAL_DEV_NAME;
68 runtime::DirectoryIterator iter(INTERNAL_DEV_PATH), end;
71 const std::string& path = (*iter).getPath();
72 std::string name = path.substr(path.rfind('/') + 1);
74 upper.reserve(name.size());
76 upper += std::toupper(c);
78 if (upper.compare(0, strlen(INTERNAL_DEV_NAME), INTERNAL_DEV_NAME) == 0) {
84 } catch (runtime::Exception &e) {}
86 char *dev = ::realpath(source.c_str(), NULL);
88 ERROR(SINK, "failed to get device path");
92 std::string devPath(dev);
98 void stopKnownSystemdServices() {
99 std::vector<std::string> knownSystemdServices;
100 dbus::Connection& systemDBus = dbus::Connection::getSystem();
101 dbus::VariantIterator iter;
103 systemDBus.methodcall("org.freedesktop.systemd1",
104 "/org/freedesktop/systemd1",
105 "org.freedesktop.systemd1.Manager",
107 -1, "(a(ssssssouso))", "")
108 .get("(a(ssssssouso))", &iter);
111 unsigned int dataUint;
115 ret = iter.get("(ssssssouso)", dataStr, dataStr + 1, dataStr + 2,
116 dataStr + 3, dataStr + 4, dataStr + 5,
117 dataStr + 6, &dataUint, dataStr + 7,
124 std::string service(dataStr[0]);
125 if (service.compare(0, 5, "user@") == 0 ||
126 service == "tlm.service" ||
127 service == "resourced.service") {
128 knownSystemdServices.push_back(service);
132 for (const std::string& service : knownSystemdServices) {
133 INFO(SINK, "Stop service - " + service);
134 systemDBus.methodcall("org.freedesktop.systemd1",
135 "/org/freedesktop/systemd1",
136 "org.freedesktop.systemd1.Manager",
138 -1, "", "(ss)", service.c_str(), "flush");
144 void stopDependedSystemdServices()
146 dbus::Connection& systemDBus = dbus::Connection::getSystem();
147 std::set<std::string> servicesToStop;
149 for (pid_t pid : runtime::FileUser::getList(INTERNAL_PATH, true)) {
152 systemDBus.methodcall("org.freedesktop.systemd1",
153 "/org/freedesktop/systemd1",
154 "org.freedesktop.systemd1.Manager",
156 -1, "(o)", "(u)", (unsigned int)pid)
157 .get("(o)", &service);
158 servicesToStop.insert(service);
159 } catch (runtime::Exception &e) {
160 INFO(SINK, "Close process - " + std::to_string(pid));
161 ::kill(pid, SIGKILL);
165 for (const std::string& service : servicesToStop) {
166 INFO(SINK, "Close service - " + service);
167 systemDBus.methodcall("org.freedesktop.systemd1",
169 "org.freedesktop.systemd1.Unit",
171 -1, "", "(s)", "flush");
175 void showProgressUI(const std::string type) {
176 ::tzplatform_set_user(::tzplatform_getuid(TZ_SYS_DEFAULT_USER));
177 std::string defaultUserHome(::tzplatform_getenv(TZ_USER_HOME));
178 ::tzplatform_reset_user();
181 runtime::File shareDirectory("/opt/home/root/share");
182 if (!shareDirectory.exists()) {
183 shareDirectory.makeDirectory(true);
186 runtime::File elmConfigDir(shareDirectory.getPath() + "/.elementary");
187 if (!elmConfigDir.exists()) {
188 runtime::File defaultElmConfigDir(defaultUserHome + "/share/.elementary");
189 defaultElmConfigDir.copyTo(shareDirectory.getPath());
191 } catch (runtime::Exception &e) {
192 ERROR(SINK, "Failed to set up elm configuration");
195 std::vector<std::string> args = {
196 "ode", "progress", type, "Internal"
199 runtime::Process proc("/usr/bin/ode", args);
203 unsigned int getOptions()
205 unsigned int result = 0;
209 ::vconf_get_bool(VCONFKEY_ODE_FAST_ENCRYPTION, &value);
211 result |= InternalEncryption::Option::IncludeUnusedRegion;
217 void setOptions(unsigned int options)
221 if (options & InternalEncryption::Option::IncludeUnusedRegion) {
226 ::vconf_set_bool(VCONFKEY_ODE_FAST_ENCRYPTION, value);
231 InternalEncryptionServer::InternalEncryptionServer(ServerContext& srv) :
234 server.expose(this, PRIVILEGE_PLATFORM, (int)(InternalEncryptionServer::setMountPassword)(std::string));
235 server.expose(this, PRIVILEGE_PLATFORM, (int)(InternalEncryptionServer::mount)());
236 server.expose(this, PRIVILEGE_PLATFORM, (int)(InternalEncryptionServer::umount)());
237 server.expose(this, PRIVILEGE_PLATFORM, (int)(InternalEncryptionServer::encrypt)(std::string, unsigned int));
238 server.expose(this, PRIVILEGE_PLATFORM, (int)(InternalEncryptionServer::decrypt)(std::string));
239 server.expose(this, "", (int)(InternalEncryptionServer::isPasswordInitialized)());
240 server.expose(this, PRIVILEGE_PLATFORM, (int)(InternalEncryptionServer::initPassword)(std::string));
241 server.expose(this, PRIVILEGE_PLATFORM, (int)(InternalEncryptionServer::cleanPassword)(std::string));
242 server.expose(this, PRIVILEGE_PLATFORM, (int)(InternalEncryptionServer::changePassword)(std::string, std::string));
243 server.expose(this, PRIVILEGE_PLATFORM, (int)(InternalEncryptionServer::verifyPassword)(std::string));
244 server.expose(this, "", (int)(InternalEncryptionServer::getState)());
245 server.expose(this, "", (unsigned int)(InternalEncryptionServer::getSupportedOptions)());
247 server.createNotification("InternalEncryptionServer::mount");
249 std::string source = findDevPath();
251 engine.reset(new INTERNAL_ENGINE(
252 source, INTERNAL_PATH,
253 ProgressBar([](int v) {
254 ::vconf_set_str(VCONFKEY_ODE_ENCRYPT_PROGRESS,
255 std::to_string(v).c_str());
260 InternalEncryptionServer::~InternalEncryptionServer()
264 int InternalEncryptionServer::setMountPassword(const std::string& password)
266 KeyManager::data pwData(password.begin(), password.end());
267 KeyManager keyManager(engine->getKeyMeta());
268 if (!keyManager.verifyPassword(pwData)) {
272 mountKey = keyManager.getMasterKey(pwData);
277 int InternalEncryptionServer::mount()
279 if (getState() != State::Encrypted) {
283 if (engine->isMounted()) {
284 INFO(SINK, "Already mounted");
289 INFO(SINK, "Mount internal storage...");
290 engine->mount(mountKey, getOptions());
293 server.notify("InternalEncryptionServer::mount");
295 runtime::File("/tmp/.lazy_mount").create(O_WRONLY);
296 runtime::File("/tmp/.unlock_mnt").create(O_WRONLY);
297 } catch (runtime::Exception &e) {
298 ERROR(SINK, "Mount failed - " + std::string(e.what()));
305 int InternalEncryptionServer::umount()
307 if (getState() != State::Encrypted) {
311 if (!engine->isMounted()) {
312 INFO(SINK, "Already umounted");
317 INFO(SINK, "Close all processes using internal storage...");
318 stopDependedSystemdServices();
319 INFO(SINK, "Umount internal storage...");
321 } catch (runtime::Exception &e) {
322 ERROR(SINK, "Umount failed - " + std::string(e.what()));
329 int InternalEncryptionServer::encrypt(const std::string& password, unsigned int options)
331 if (getState() != State::Unencrypted) {
335 KeyManager::data pwData(password.begin(), password.end());
336 KeyManager keyManager(engine->getKeyMeta());
338 if (!keyManager.verifyPassword(pwData)) {
342 KeyManager::data MasterKey = keyManager.getMasterKey(pwData);
343 auto encryptWorker = [MasterKey, options, this]() {
345 std::string source = engine->getSource();
346 std::string mntPath = findMntPath(source);
348 if (!mntPath.empty()) {
349 INFO(SINK, "Close all known systemd services that might be using internal storage...");
350 stopKnownSystemdServices();
351 INFO(SINK, "Close all processes using internal storage...");
352 stopDependedSystemdServices();
355 while (!mntPath.empty()) {
356 INFO(SINK, "Umount internal storage...");
357 while (::umount(mntPath.c_str()) == -1) {
358 if (errno != EBUSY) {
359 throw runtime::Exception("Umount error - " + std::to_string(errno));
361 stopDependedSystemdServices();
363 mntPath = findMntPath(source);
366 showProgressUI("Encrypting");
368 INFO(SINK, "Encryption started...");
369 engine->encrypt(MasterKey, options);
370 setOptions(options & getSupportedOptions());
372 INFO(SINK, "Encryption completed");
373 ::vconf_set_str(VCONFKEY_ODE_CRYPTO_STATE, "encrypted");
374 server.notify("InternalEncryptionServer::mount");
376 INFO(SINK, "Syncing disk and rebooting...");
378 ::reboot(RB_AUTOBOOT);
379 } catch (runtime::Exception &e) {
380 ::vconf_set_str(VCONFKEY_ODE_CRYPTO_STATE, "error_partially_encrypted");
381 ERROR(SINK, "Encryption failed - " + std::string(e.what()));
385 std::thread asyncWork(encryptWorker);
391 int InternalEncryptionServer::decrypt(const std::string& password)
393 if (getState() != State::Encrypted) {
397 KeyManager::data pwData(password.begin(), password.end());
398 KeyManager keyManager(engine->getKeyMeta());
400 if (!keyManager.verifyPassword(pwData)) {
404 KeyManager::data MasterKey = keyManager.getMasterKey(pwData);
405 auto decryptWorker = [MasterKey, this]() {
407 if (engine->isMounted()) {
408 INFO(SINK, "Close all known systemd services that might be using internal storage...");
409 stopKnownSystemdServices();
410 INFO(SINK, "Close all processes using internal storage...");
411 stopDependedSystemdServices();
413 INFO(SINK, "Umount internal storage...");
418 } catch (runtime::Exception& e) {
419 stopDependedSystemdServices();
424 showProgressUI("Decrypting");
426 INFO(SINK, "Decryption started...");
427 engine->decrypt(MasterKey, getOptions());
429 INFO(SINK, "Decryption completed");
430 ::vconf_set_str(VCONFKEY_ODE_CRYPTO_STATE, "unencrypted");
432 INFO(SINK, "Syncing disk and rebooting...");
434 ::reboot(RB_AUTOBOOT);
435 } catch (runtime::Exception &e) {
436 ::vconf_set_str(VCONFKEY_ODE_CRYPTO_STATE, "error_partially_encrypted");
437 ERROR(SINK, "Decryption failed - " + std::string(e.what()));
441 std::thread asyncWork(decryptWorker);
447 int InternalEncryptionServer::recovery()
449 if (getState() != State::Unencrypted) {
454 runtime::Process proc(PROG_FACTORY_RESET, wipeCommand);
455 if (proc.execute() == -1) {
456 ERROR(SINK, "Failed to launch factory-reset");
463 int InternalEncryptionServer::isPasswordInitialized()
465 if (engine->isKeyMetaSet()) {
471 int InternalEncryptionServer::initPassword(const std::string& password)
473 KeyManager::data pwData(password.begin(), password.end());
474 KeyManager keyManager;
476 keyManager.initPassword(pwData);
477 engine->setKeyMeta(keyManager.serialize());
481 int InternalEncryptionServer::cleanPassword(const std::string& password)
483 KeyManager::data pwData(password.begin(), password.end());
484 KeyManager keyManager(engine->getKeyMeta());
486 if (!keyManager.verifyPassword(pwData)) {
490 engine->clearKeyMeta();
494 int InternalEncryptionServer::changePassword(const std::string& oldPassword,
495 const std::string& newPassword)
497 KeyManager::data oldPwData(oldPassword.begin(), oldPassword.end());
498 KeyManager::data newPwData(newPassword.begin(), newPassword.end());
499 KeyManager keyManager(engine->getKeyMeta());
501 if (!keyManager.verifyPassword(oldPwData)) {
505 keyManager.changePassword(oldPwData, newPwData);
506 engine->setKeyMeta(keyManager.serialize());
511 int InternalEncryptionServer::verifyPassword(const std::string& password)
513 KeyManager::data pwData(password.begin(), password.end());
514 KeyManager keyManager(engine->getKeyMeta());
516 if (keyManager.verifyPassword(pwData)) {
522 int InternalEncryptionServer::getState()
524 char *value = ::vconf_get_str(VCONFKEY_ODE_CRYPTO_STATE);
526 throw runtime::Exception("Failed to get vconf value");
529 std::string valueStr(value);
532 if (valueStr == "encrypted") {
533 return State::Encrypted;
534 } else if (valueStr == "unencrypted") {
535 return State::Unencrypted;
537 return State::Corrupted;
543 unsigned int InternalEncryptionServer::getSupportedOptions()
545 return engine->getSupportedOptions();