VoldNativeService.cpp 28 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911
  1. /*
  2. * Copyright (C) 2017 The Android Open Source Project
  3. *
  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
  7. *
  8. * http://www.apache.org/licenses/LICENSE-2.0
  9. *
  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.
  15. */
  16. #define ATRACE_TAG ATRACE_TAG_PACKAGE_MANAGER
  17. #include "VoldNativeService.h"
  18. #include "Benchmark.h"
  19. #include "CheckEncryption.h"
  20. #include "IdleMaint.h"
  21. #include "MoveStorage.h"
  22. #include "Process.h"
  23. #include "VolumeManager.h"
  24. #include "Checkpoint.h"
  25. #include "FsCrypt.h"
  26. #include "MetadataCrypt.h"
  27. #include "cryptfs.h"
  28. #include <fstream>
  29. #include <thread>
  30. #include <android-base/logging.h>
  31. #include <android-base/stringprintf.h>
  32. #include <android-base/strings.h>
  33. #include <fs_mgr.h>
  34. #include <fscrypt/fscrypt.h>
  35. #include <private/android_filesystem_config.h>
  36. #include <utils/Trace.h>
  37. using android::base::StringPrintf;
  38. using std::endl;
  39. namespace android {
  40. namespace vold {
  41. namespace {
  42. constexpr const char* kDump = "android.permission.DUMP";
  43. static binder::Status ok() {
  44. return binder::Status::ok();
  45. }
  46. static binder::Status exception(uint32_t code, const std::string& msg) {
  47. return binder::Status::fromExceptionCode(code, String8(msg.c_str()));
  48. }
  49. static binder::Status error(const std::string& msg) {
  50. PLOG(ERROR) << msg;
  51. return binder::Status::fromServiceSpecificError(errno, String8(msg.c_str()));
  52. }
  53. static binder::Status translate(int status) {
  54. if (status == 0) {
  55. return binder::Status::ok();
  56. } else {
  57. return binder::Status::fromServiceSpecificError(status);
  58. }
  59. }
  60. static binder::Status translateBool(bool status) {
  61. if (status) {
  62. return binder::Status::ok();
  63. } else {
  64. return binder::Status::fromServiceSpecificError(status);
  65. }
  66. }
  67. binder::Status checkPermission(const char* permission) {
  68. pid_t pid;
  69. uid_t uid;
  70. if (checkCallingPermission(String16(permission), reinterpret_cast<int32_t*>(&pid),
  71. reinterpret_cast<int32_t*>(&uid))) {
  72. return ok();
  73. } else {
  74. return exception(binder::Status::EX_SECURITY,
  75. StringPrintf("UID %d / PID %d lacks permission %s", uid, pid, permission));
  76. }
  77. }
  78. binder::Status checkUid(uid_t expectedUid) {
  79. uid_t uid = IPCThreadState::self()->getCallingUid();
  80. if (uid == expectedUid || uid == AID_ROOT) {
  81. return ok();
  82. } else {
  83. return exception(binder::Status::EX_SECURITY,
  84. StringPrintf("UID %d is not expected UID %d", uid, expectedUid));
  85. }
  86. }
  87. binder::Status checkArgumentId(const std::string& id) {
  88. if (id.empty()) {
  89. return exception(binder::Status::EX_ILLEGAL_ARGUMENT, "Missing ID");
  90. }
  91. for (const char& c : id) {
  92. if (!std::isalnum(c) && c != ':' && c != ',') {
  93. return exception(binder::Status::EX_ILLEGAL_ARGUMENT,
  94. StringPrintf("ID %s is malformed", id.c_str()));
  95. }
  96. }
  97. return ok();
  98. }
  99. binder::Status checkArgumentPath(const std::string& path) {
  100. if (path.empty()) {
  101. return exception(binder::Status::EX_ILLEGAL_ARGUMENT, "Missing path");
  102. }
  103. if (path[0] != '/') {
  104. return exception(binder::Status::EX_ILLEGAL_ARGUMENT,
  105. StringPrintf("Path %s is relative", path.c_str()));
  106. }
  107. if ((path + '/').find("/../") != std::string::npos) {
  108. return exception(binder::Status::EX_ILLEGAL_ARGUMENT,
  109. StringPrintf("Path %s is shady", path.c_str()));
  110. }
  111. for (const char& c : path) {
  112. if (c == '\0' || c == '\n') {
  113. return exception(binder::Status::EX_ILLEGAL_ARGUMENT,
  114. StringPrintf("Path %s is malformed", path.c_str()));
  115. }
  116. }
  117. return ok();
  118. }
  119. binder::Status checkArgumentHex(const std::string& hex) {
  120. // Empty hex strings are allowed
  121. for (const char& c : hex) {
  122. if (!std::isxdigit(c) && c != ':' && c != '-') {
  123. return exception(binder::Status::EX_ILLEGAL_ARGUMENT,
  124. StringPrintf("Hex %s is malformed", hex.c_str()));
  125. }
  126. }
  127. return ok();
  128. }
  129. #define ENFORCE_UID(uid) \
  130. { \
  131. binder::Status status = checkUid((uid)); \
  132. if (!status.isOk()) { \
  133. return status; \
  134. } \
  135. }
  136. #define CHECK_ARGUMENT_ID(id) \
  137. { \
  138. binder::Status status = checkArgumentId((id)); \
  139. if (!status.isOk()) { \
  140. return status; \
  141. } \
  142. }
  143. #define CHECK_ARGUMENT_PATH(path) \
  144. { \
  145. binder::Status status = checkArgumentPath((path)); \
  146. if (!status.isOk()) { \
  147. return status; \
  148. } \
  149. }
  150. #define CHECK_ARGUMENT_HEX(hex) \
  151. { \
  152. binder::Status status = checkArgumentHex((hex)); \
  153. if (!status.isOk()) { \
  154. return status; \
  155. } \
  156. }
  157. #define ACQUIRE_LOCK \
  158. std::lock_guard<std::mutex> lock(VolumeManager::Instance()->getLock()); \
  159. ATRACE_CALL();
  160. #define ACQUIRE_CRYPT_LOCK \
  161. std::lock_guard<std::mutex> lock(VolumeManager::Instance()->getCryptLock()); \
  162. ATRACE_CALL();
  163. } // namespace
  164. status_t VoldNativeService::start() {
  165. IPCThreadState::self()->disableBackgroundScheduling(true);
  166. status_t ret = BinderService<VoldNativeService>::publish();
  167. if (ret != android::OK) {
  168. return ret;
  169. }
  170. sp<ProcessState> ps(ProcessState::self());
  171. ps->startThreadPool();
  172. ps->giveThreadPoolName();
  173. return android::OK;
  174. }
  175. status_t VoldNativeService::dump(int fd, const Vector<String16>& /* args */) {
  176. auto out = std::fstream(StringPrintf("/proc/self/fd/%d", fd));
  177. const binder::Status dump_permission = checkPermission(kDump);
  178. if (!dump_permission.isOk()) {
  179. out << dump_permission.toString8() << endl;
  180. return PERMISSION_DENIED;
  181. }
  182. ACQUIRE_LOCK;
  183. out << "vold is happy!" << endl;
  184. out.flush();
  185. return NO_ERROR;
  186. }
  187. binder::Status VoldNativeService::setListener(
  188. const android::sp<android::os::IVoldListener>& listener) {
  189. ENFORCE_UID(AID_SYSTEM);
  190. ACQUIRE_LOCK;
  191. VolumeManager::Instance()->setListener(listener);
  192. return ok();
  193. }
  194. binder::Status VoldNativeService::monitor() {
  195. ENFORCE_UID(AID_SYSTEM);
  196. // Simply acquire/release each lock for watchdog
  197. { ACQUIRE_LOCK; }
  198. { ACQUIRE_CRYPT_LOCK; }
  199. return ok();
  200. }
  201. binder::Status VoldNativeService::reset() {
  202. ENFORCE_UID(AID_SYSTEM);
  203. ACQUIRE_LOCK;
  204. return translate(VolumeManager::Instance()->reset());
  205. }
  206. binder::Status VoldNativeService::shutdown() {
  207. ENFORCE_UID(AID_SYSTEM);
  208. ACQUIRE_LOCK;
  209. return translate(VolumeManager::Instance()->shutdown());
  210. }
  211. binder::Status VoldNativeService::onUserAdded(int32_t userId, int32_t userSerial) {
  212. ENFORCE_UID(AID_SYSTEM);
  213. ACQUIRE_LOCK;
  214. return translate(VolumeManager::Instance()->onUserAdded(userId, userSerial));
  215. }
  216. binder::Status VoldNativeService::onUserRemoved(int32_t userId) {
  217. ENFORCE_UID(AID_SYSTEM);
  218. ACQUIRE_LOCK;
  219. return translate(VolumeManager::Instance()->onUserRemoved(userId));
  220. }
  221. binder::Status VoldNativeService::onUserStarted(int32_t userId) {
  222. ENFORCE_UID(AID_SYSTEM);
  223. ACQUIRE_LOCK;
  224. return translate(VolumeManager::Instance()->onUserStarted(userId));
  225. }
  226. binder::Status VoldNativeService::onUserStopped(int32_t userId) {
  227. ENFORCE_UID(AID_SYSTEM);
  228. ACQUIRE_LOCK;
  229. return translate(VolumeManager::Instance()->onUserStopped(userId));
  230. }
  231. binder::Status VoldNativeService::addAppIds(const std::vector<std::string>& packageNames,
  232. const std::vector<int32_t>& appIds) {
  233. return ok();
  234. }
  235. binder::Status VoldNativeService::addSandboxIds(const std::vector<int32_t>& appIds,
  236. const std::vector<std::string>& sandboxIds) {
  237. return ok();
  238. }
  239. binder::Status VoldNativeService::onSecureKeyguardStateChanged(bool isShowing) {
  240. ENFORCE_UID(AID_SYSTEM);
  241. ACQUIRE_LOCK;
  242. return translate(VolumeManager::Instance()->onSecureKeyguardStateChanged(isShowing));
  243. }
  244. binder::Status VoldNativeService::partition(const std::string& diskId, int32_t partitionType,
  245. int32_t ratio) {
  246. ENFORCE_UID(AID_SYSTEM);
  247. CHECK_ARGUMENT_ID(diskId);
  248. ACQUIRE_LOCK;
  249. auto disk = VolumeManager::Instance()->findDisk(diskId);
  250. if (disk == nullptr) {
  251. return error("Failed to find disk " + diskId);
  252. }
  253. switch (partitionType) {
  254. case PARTITION_TYPE_PUBLIC:
  255. return translate(disk->partitionPublic());
  256. case PARTITION_TYPE_PRIVATE:
  257. return translate(disk->partitionPrivate());
  258. case PARTITION_TYPE_MIXED:
  259. return translate(disk->partitionMixed(ratio));
  260. default:
  261. return error("Unknown type " + std::to_string(partitionType));
  262. }
  263. }
  264. binder::Status VoldNativeService::forgetPartition(const std::string& partGuid,
  265. const std::string& fsUuid) {
  266. ENFORCE_UID(AID_SYSTEM);
  267. CHECK_ARGUMENT_HEX(partGuid);
  268. CHECK_ARGUMENT_HEX(fsUuid);
  269. ACQUIRE_LOCK;
  270. return translate(VolumeManager::Instance()->forgetPartition(partGuid, fsUuid));
  271. }
  272. binder::Status VoldNativeService::mount(const std::string& volId, int32_t mountFlags,
  273. int32_t mountUserId) {
  274. ENFORCE_UID(AID_SYSTEM);
  275. CHECK_ARGUMENT_ID(volId);
  276. ACQUIRE_LOCK;
  277. auto vol = VolumeManager::Instance()->findVolume(volId);
  278. if (vol == nullptr) {
  279. return error("Failed to find volume " + volId);
  280. }
  281. vol->setMountFlags(mountFlags);
  282. vol->setMountUserId(mountUserId);
  283. int res = vol->mount();
  284. if (res != OK) {
  285. return translate(res);
  286. }
  287. if ((mountFlags & MOUNT_FLAG_PRIMARY) != 0) {
  288. res = VolumeManager::Instance()->setPrimary(vol);
  289. if (res != OK) {
  290. return translate(res);
  291. }
  292. }
  293. return translate(OK);
  294. }
  295. binder::Status VoldNativeService::unmount(const std::string& volId) {
  296. ENFORCE_UID(AID_SYSTEM);
  297. CHECK_ARGUMENT_ID(volId);
  298. ACQUIRE_LOCK;
  299. auto vol = VolumeManager::Instance()->findVolume(volId);
  300. if (vol == nullptr) {
  301. return error("Failed to find volume " + volId);
  302. }
  303. return translate(vol->unmount());
  304. }
  305. binder::Status VoldNativeService::format(const std::string& volId, const std::string& fsType) {
  306. ENFORCE_UID(AID_SYSTEM);
  307. CHECK_ARGUMENT_ID(volId);
  308. ACQUIRE_LOCK;
  309. auto vol = VolumeManager::Instance()->findVolume(volId);
  310. if (vol == nullptr) {
  311. return error("Failed to find volume " + volId);
  312. }
  313. return translate(vol->format(fsType));
  314. }
  315. static binder::Status pathForVolId(const std::string& volId, std::string* path) {
  316. if (volId == "private" || volId == "null") {
  317. *path = "/data";
  318. } else {
  319. auto vol = VolumeManager::Instance()->findVolume(volId);
  320. if (vol == nullptr) {
  321. return error("Failed to find volume " + volId);
  322. }
  323. if (vol->getType() != VolumeBase::Type::kPrivate) {
  324. return error("Volume " + volId + " not private");
  325. }
  326. if (vol->getState() != VolumeBase::State::kMounted) {
  327. return error("Volume " + volId + " not mounted");
  328. }
  329. *path = vol->getPath();
  330. if (path->empty()) {
  331. return error("Volume " + volId + " missing path");
  332. }
  333. }
  334. return ok();
  335. }
  336. binder::Status VoldNativeService::benchmark(
  337. const std::string& volId, const android::sp<android::os::IVoldTaskListener>& listener) {
  338. ENFORCE_UID(AID_SYSTEM);
  339. CHECK_ARGUMENT_ID(volId);
  340. ACQUIRE_LOCK;
  341. std::string path;
  342. auto status = pathForVolId(volId, &path);
  343. if (!status.isOk()) return status;
  344. std::thread([=]() { android::vold::Benchmark(path, listener); }).detach();
  345. return ok();
  346. }
  347. binder::Status VoldNativeService::checkEncryption(const std::string& volId) {
  348. ENFORCE_UID(AID_SYSTEM);
  349. CHECK_ARGUMENT_ID(volId);
  350. ACQUIRE_LOCK;
  351. std::string path;
  352. auto status = pathForVolId(volId, &path);
  353. if (!status.isOk()) return status;
  354. return translate(android::vold::CheckEncryption(path));
  355. }
  356. binder::Status VoldNativeService::moveStorage(
  357. const std::string& fromVolId, const std::string& toVolId,
  358. const android::sp<android::os::IVoldTaskListener>& listener) {
  359. ENFORCE_UID(AID_SYSTEM);
  360. CHECK_ARGUMENT_ID(fromVolId);
  361. CHECK_ARGUMENT_ID(toVolId);
  362. ACQUIRE_LOCK;
  363. auto fromVol = VolumeManager::Instance()->findVolume(fromVolId);
  364. auto toVol = VolumeManager::Instance()->findVolume(toVolId);
  365. if (fromVol == nullptr) {
  366. return error("Failed to find volume " + fromVolId);
  367. } else if (toVol == nullptr) {
  368. return error("Failed to find volume " + toVolId);
  369. }
  370. std::thread([=]() { android::vold::MoveStorage(fromVol, toVol, listener); }).detach();
  371. return ok();
  372. }
  373. binder::Status VoldNativeService::remountUid(int32_t uid, int32_t remountMode) {
  374. ENFORCE_UID(AID_SYSTEM);
  375. ACQUIRE_LOCK;
  376. return translate(VolumeManager::Instance()->remountUid(uid, remountMode));
  377. }
  378. binder::Status VoldNativeService::mkdirs(const std::string& path) {
  379. ENFORCE_UID(AID_SYSTEM);
  380. CHECK_ARGUMENT_PATH(path);
  381. ACQUIRE_LOCK;
  382. return translate(VolumeManager::Instance()->mkdirs(path));
  383. }
  384. binder::Status VoldNativeService::createObb(const std::string& sourcePath,
  385. const std::string& sourceKey, int32_t ownerGid,
  386. std::string* _aidl_return) {
  387. ENFORCE_UID(AID_SYSTEM);
  388. CHECK_ARGUMENT_PATH(sourcePath);
  389. CHECK_ARGUMENT_HEX(sourceKey);
  390. ACQUIRE_LOCK;
  391. return translate(
  392. VolumeManager::Instance()->createObb(sourcePath, sourceKey, ownerGid, _aidl_return));
  393. }
  394. binder::Status VoldNativeService::destroyObb(const std::string& volId) {
  395. ENFORCE_UID(AID_SYSTEM);
  396. CHECK_ARGUMENT_ID(volId);
  397. ACQUIRE_LOCK;
  398. return translate(VolumeManager::Instance()->destroyObb(volId));
  399. }
  400. binder::Status VoldNativeService::createStubVolume(
  401. const std::string& sourcePath, const std::string& mountPath, const std::string& fsType,
  402. const std::string& fsUuid, const std::string& fsLabel, std::string* _aidl_return) {
  403. ENFORCE_UID(AID_SYSTEM);
  404. CHECK_ARGUMENT_PATH(sourcePath);
  405. CHECK_ARGUMENT_PATH(mountPath);
  406. CHECK_ARGUMENT_HEX(fsUuid);
  407. // Label limitation seems to be different between fs (including allowed characters), so checking
  408. // is quite meaningless.
  409. ACQUIRE_LOCK;
  410. return translate(VolumeManager::Instance()->createStubVolume(sourcePath, mountPath, fsType,
  411. fsUuid, fsLabel, _aidl_return));
  412. }
  413. binder::Status VoldNativeService::destroyStubVolume(const std::string& volId) {
  414. ENFORCE_UID(AID_SYSTEM);
  415. CHECK_ARGUMENT_ID(volId);
  416. ACQUIRE_LOCK;
  417. return translate(VolumeManager::Instance()->destroyStubVolume(volId));
  418. }
  419. binder::Status VoldNativeService::fstrim(
  420. int32_t fstrimFlags, const android::sp<android::os::IVoldTaskListener>& listener) {
  421. ENFORCE_UID(AID_SYSTEM);
  422. ACQUIRE_LOCK;
  423. std::thread([=]() { android::vold::Trim(listener); }).detach();
  424. return ok();
  425. }
  426. binder::Status VoldNativeService::runIdleMaint(
  427. const android::sp<android::os::IVoldTaskListener>& listener) {
  428. ENFORCE_UID(AID_SYSTEM);
  429. ACQUIRE_LOCK;
  430. std::thread([=]() { android::vold::RunIdleMaint(listener); }).detach();
  431. return ok();
  432. }
  433. binder::Status VoldNativeService::abortIdleMaint(
  434. const android::sp<android::os::IVoldTaskListener>& listener) {
  435. ENFORCE_UID(AID_SYSTEM);
  436. ACQUIRE_LOCK;
  437. std::thread([=]() { android::vold::AbortIdleMaint(listener); }).detach();
  438. return ok();
  439. }
  440. binder::Status VoldNativeService::mountAppFuse(int32_t uid, int32_t mountId,
  441. android::base::unique_fd* _aidl_return) {
  442. ENFORCE_UID(AID_SYSTEM);
  443. ACQUIRE_LOCK;
  444. return translate(VolumeManager::Instance()->mountAppFuse(uid, mountId, _aidl_return));
  445. }
  446. binder::Status VoldNativeService::unmountAppFuse(int32_t uid, int32_t mountId) {
  447. ENFORCE_UID(AID_SYSTEM);
  448. ACQUIRE_LOCK;
  449. return translate(VolumeManager::Instance()->unmountAppFuse(uid, mountId));
  450. }
  451. binder::Status VoldNativeService::openAppFuseFile(int32_t uid, int32_t mountId, int32_t fileId,
  452. int32_t flags,
  453. android::base::unique_fd* _aidl_return) {
  454. ENFORCE_UID(AID_SYSTEM);
  455. ACQUIRE_LOCK;
  456. int fd = VolumeManager::Instance()->openAppFuseFile(uid, mountId, fileId, flags);
  457. if (fd == -1) {
  458. return error("Failed to open AppFuse file for uid: " + std::to_string(uid) +
  459. " mountId: " + std::to_string(mountId) + " fileId: " + std::to_string(fileId) +
  460. " flags: " + std::to_string(flags));
  461. }
  462. *_aidl_return = android::base::unique_fd(fd);
  463. return ok();
  464. }
  465. binder::Status VoldNativeService::fdeCheckPassword(const std::string& password) {
  466. ENFORCE_UID(AID_SYSTEM);
  467. ACQUIRE_CRYPT_LOCK;
  468. return translate(cryptfs_check_passwd(password.c_str()));
  469. }
  470. binder::Status VoldNativeService::fdeRestart() {
  471. ENFORCE_UID(AID_SYSTEM);
  472. ACQUIRE_CRYPT_LOCK;
  473. // Spawn as thread so init can issue commands back to vold without
  474. // causing deadlock, usually as a result of prep_data_fs.
  475. std::thread(&cryptfs_restart).detach();
  476. return ok();
  477. }
  478. binder::Status VoldNativeService::fdeComplete(int32_t* _aidl_return) {
  479. ENFORCE_UID(AID_SYSTEM);
  480. ACQUIRE_CRYPT_LOCK;
  481. *_aidl_return = cryptfs_crypto_complete();
  482. return ok();
  483. }
  484. static int fdeEnableInternal(int32_t passwordType, const std::string& password,
  485. int32_t encryptionFlags) {
  486. bool noUi = (encryptionFlags & VoldNativeService::ENCRYPTION_FLAG_NO_UI) != 0;
  487. for (int tries = 0; tries < 2; ++tries) {
  488. int rc;
  489. if (passwordType == VoldNativeService::PASSWORD_TYPE_DEFAULT) {
  490. rc = cryptfs_enable_default(noUi);
  491. } else {
  492. rc = cryptfs_enable(passwordType, password.c_str(), noUi);
  493. }
  494. if (rc == 0) {
  495. return 0;
  496. } else if (tries == 0) {
  497. KillProcessesWithOpenFiles(DATA_MNT_POINT, SIGKILL);
  498. }
  499. }
  500. return -1;
  501. }
  502. binder::Status VoldNativeService::fdeEnable(int32_t passwordType, const std::string& password,
  503. int32_t encryptionFlags) {
  504. ENFORCE_UID(AID_SYSTEM);
  505. ACQUIRE_CRYPT_LOCK;
  506. LOG(DEBUG) << "fdeEnable(" << passwordType << ", *, " << encryptionFlags << ")";
  507. if (fscrypt_is_native()) {
  508. LOG(ERROR) << "fscrypt_is_native, fdeEnable invalid";
  509. return error("fscrypt_is_native, fdeEnable invalid");
  510. }
  511. LOG(DEBUG) << "!fscrypt_is_native, spawning fdeEnableInternal";
  512. // Spawn as thread so init can issue commands back to vold without
  513. // causing deadlock, usually as a result of prep_data_fs.
  514. std::thread(&fdeEnableInternal, passwordType, password, encryptionFlags).detach();
  515. return ok();
  516. }
  517. binder::Status VoldNativeService::fdeChangePassword(int32_t passwordType,
  518. const std::string& password) {
  519. ENFORCE_UID(AID_SYSTEM);
  520. ACQUIRE_CRYPT_LOCK;
  521. return translate(cryptfs_changepw(passwordType, password.c_str()));
  522. }
  523. binder::Status VoldNativeService::fdeVerifyPassword(const std::string& password) {
  524. ENFORCE_UID(AID_SYSTEM);
  525. ACQUIRE_CRYPT_LOCK;
  526. return translate(cryptfs_verify_passwd(password.c_str()));
  527. }
  528. binder::Status VoldNativeService::fdeGetField(const std::string& key, std::string* _aidl_return) {
  529. ENFORCE_UID(AID_SYSTEM);
  530. ACQUIRE_CRYPT_LOCK;
  531. char buf[PROPERTY_VALUE_MAX];
  532. if (cryptfs_getfield(key.c_str(), buf, sizeof(buf)) != CRYPTO_GETFIELD_OK) {
  533. return error(StringPrintf("Failed to read field %s", key.c_str()));
  534. } else {
  535. *_aidl_return = buf;
  536. return ok();
  537. }
  538. }
  539. binder::Status VoldNativeService::fdeSetField(const std::string& key, const std::string& value) {
  540. ENFORCE_UID(AID_SYSTEM);
  541. ACQUIRE_CRYPT_LOCK;
  542. return translate(cryptfs_setfield(key.c_str(), value.c_str()));
  543. }
  544. binder::Status VoldNativeService::fdeGetPasswordType(int32_t* _aidl_return) {
  545. ENFORCE_UID(AID_SYSTEM);
  546. ACQUIRE_CRYPT_LOCK;
  547. *_aidl_return = cryptfs_get_password_type();
  548. return ok();
  549. }
  550. binder::Status VoldNativeService::fdeGetPassword(std::string* _aidl_return) {
  551. ENFORCE_UID(AID_SYSTEM);
  552. ACQUIRE_CRYPT_LOCK;
  553. const char* res = cryptfs_get_password();
  554. if (res != nullptr) {
  555. *_aidl_return = res;
  556. }
  557. return ok();
  558. }
  559. binder::Status VoldNativeService::fdeClearPassword() {
  560. ENFORCE_UID(AID_SYSTEM);
  561. ACQUIRE_CRYPT_LOCK;
  562. cryptfs_clear_password();
  563. return ok();
  564. }
  565. binder::Status VoldNativeService::fbeEnable() {
  566. ENFORCE_UID(AID_SYSTEM);
  567. ACQUIRE_CRYPT_LOCK;
  568. return translateBool(fscrypt_initialize_systemwide_keys());
  569. }
  570. binder::Status VoldNativeService::mountDefaultEncrypted() {
  571. ENFORCE_UID(AID_SYSTEM);
  572. ACQUIRE_CRYPT_LOCK;
  573. if (!fscrypt_is_native()) {
  574. // Spawn as thread so init can issue commands back to vold without
  575. // causing deadlock, usually as a result of prep_data_fs.
  576. std::thread(&cryptfs_mount_default_encrypted).detach();
  577. }
  578. return ok();
  579. }
  580. binder::Status VoldNativeService::initUser0() {
  581. ENFORCE_UID(AID_SYSTEM);
  582. ACQUIRE_CRYPT_LOCK;
  583. return translateBool(fscrypt_init_user0());
  584. }
  585. binder::Status VoldNativeService::isConvertibleToFbe(bool* _aidl_return) {
  586. ENFORCE_UID(AID_SYSTEM);
  587. ACQUIRE_CRYPT_LOCK;
  588. *_aidl_return = cryptfs_isConvertibleToFBE() != 0;
  589. return ok();
  590. }
  591. binder::Status VoldNativeService::mountFstab(const std::string& blkDevice,
  592. const std::string& mountPoint) {
  593. ENFORCE_UID(AID_SYSTEM);
  594. ACQUIRE_LOCK;
  595. return translateBool(fscrypt_mount_metadata_encrypted(blkDevice, mountPoint, false));
  596. }
  597. binder::Status VoldNativeService::encryptFstab(const std::string& blkDevice,
  598. const std::string& mountPoint) {
  599. ENFORCE_UID(AID_SYSTEM);
  600. ACQUIRE_LOCK;
  601. return translateBool(fscrypt_mount_metadata_encrypted(blkDevice, mountPoint, true));
  602. }
  603. binder::Status VoldNativeService::createUserKey(int32_t userId, int32_t userSerial, bool ephemeral) {
  604. ENFORCE_UID(AID_SYSTEM);
  605. ACQUIRE_CRYPT_LOCK;
  606. return translateBool(fscrypt_vold_create_user_key(userId, userSerial, ephemeral));
  607. }
  608. binder::Status VoldNativeService::destroyUserKey(int32_t userId) {
  609. ENFORCE_UID(AID_SYSTEM);
  610. ACQUIRE_CRYPT_LOCK;
  611. return translateBool(fscrypt_destroy_user_key(userId));
  612. }
  613. binder::Status VoldNativeService::addUserKeyAuth(int32_t userId, int32_t userSerial,
  614. const std::string& token,
  615. const std::string& secret) {
  616. ENFORCE_UID(AID_SYSTEM);
  617. ACQUIRE_CRYPT_LOCK;
  618. return translateBool(fscrypt_add_user_key_auth(userId, userSerial, token, secret));
  619. }
  620. binder::Status VoldNativeService::fixateNewestUserKeyAuth(int32_t userId) {
  621. ENFORCE_UID(AID_SYSTEM);
  622. ACQUIRE_CRYPT_LOCK;
  623. return translateBool(fscrypt_fixate_newest_user_key_auth(userId));
  624. }
  625. binder::Status VoldNativeService::unlockUserKey(int32_t userId, int32_t userSerial,
  626. const std::string& token,
  627. const std::string& secret) {
  628. ENFORCE_UID(AID_SYSTEM);
  629. ACQUIRE_CRYPT_LOCK;
  630. return translateBool(fscrypt_unlock_user_key(userId, userSerial, token, secret));
  631. }
  632. binder::Status VoldNativeService::lockUserKey(int32_t userId) {
  633. ENFORCE_UID(AID_SYSTEM);
  634. ACQUIRE_CRYPT_LOCK;
  635. return translateBool(fscrypt_lock_user_key(userId));
  636. }
  637. binder::Status VoldNativeService::prepareUserStorage(const std::unique_ptr<std::string>& uuid,
  638. int32_t userId, int32_t userSerial,
  639. int32_t flags) {
  640. ENFORCE_UID(AID_SYSTEM);
  641. std::string empty_string = "";
  642. auto uuid_ = uuid ? *uuid : empty_string;
  643. CHECK_ARGUMENT_HEX(uuid_);
  644. ACQUIRE_CRYPT_LOCK;
  645. return translateBool(fscrypt_prepare_user_storage(uuid_, userId, userSerial, flags));
  646. }
  647. binder::Status VoldNativeService::destroyUserStorage(const std::unique_ptr<std::string>& uuid,
  648. int32_t userId, int32_t flags) {
  649. ENFORCE_UID(AID_SYSTEM);
  650. std::string empty_string = "";
  651. auto uuid_ = uuid ? *uuid : empty_string;
  652. CHECK_ARGUMENT_HEX(uuid_);
  653. ACQUIRE_CRYPT_LOCK;
  654. return translateBool(fscrypt_destroy_user_storage(uuid_, userId, flags));
  655. }
  656. binder::Status VoldNativeService::prepareSandboxForApp(const std::string& packageName,
  657. int32_t appId, const std::string& sandboxId,
  658. int32_t userId) {
  659. return ok();
  660. }
  661. binder::Status VoldNativeService::destroySandboxForApp(const std::string& packageName,
  662. const std::string& sandboxId,
  663. int32_t userId) {
  664. return ok();
  665. }
  666. binder::Status VoldNativeService::startCheckpoint(int32_t retry) {
  667. ENFORCE_UID(AID_SYSTEM);
  668. ACQUIRE_LOCK;
  669. return cp_startCheckpoint(retry);
  670. }
  671. binder::Status VoldNativeService::needsRollback(bool* _aidl_return) {
  672. ENFORCE_UID(AID_SYSTEM);
  673. ACQUIRE_LOCK;
  674. *_aidl_return = cp_needsRollback();
  675. return ok();
  676. }
  677. binder::Status VoldNativeService::needsCheckpoint(bool* _aidl_return) {
  678. ENFORCE_UID(AID_SYSTEM);
  679. ACQUIRE_LOCK;
  680. *_aidl_return = cp_needsCheckpoint();
  681. return ok();
  682. }
  683. binder::Status VoldNativeService::commitChanges() {
  684. ENFORCE_UID(AID_SYSTEM);
  685. ACQUIRE_LOCK;
  686. return cp_commitChanges();
  687. }
  688. binder::Status VoldNativeService::prepareCheckpoint() {
  689. ENFORCE_UID(AID_SYSTEM);
  690. ACQUIRE_LOCK;
  691. return cp_prepareCheckpoint();
  692. }
  693. binder::Status VoldNativeService::restoreCheckpoint(const std::string& mountPoint) {
  694. ENFORCE_UID(AID_SYSTEM);
  695. CHECK_ARGUMENT_PATH(mountPoint);
  696. ACQUIRE_LOCK;
  697. return cp_restoreCheckpoint(mountPoint);
  698. }
  699. binder::Status VoldNativeService::restoreCheckpointPart(const std::string& mountPoint, int count) {
  700. ENFORCE_UID(AID_SYSTEM);
  701. CHECK_ARGUMENT_PATH(mountPoint);
  702. ACQUIRE_LOCK;
  703. return cp_restoreCheckpoint(mountPoint, count);
  704. }
  705. binder::Status VoldNativeService::markBootAttempt() {
  706. ENFORCE_UID(AID_SYSTEM);
  707. ACQUIRE_LOCK;
  708. return cp_markBootAttempt();
  709. }
  710. binder::Status VoldNativeService::abortChanges(const std::string& message, bool retry) {
  711. ENFORCE_UID(AID_SYSTEM);
  712. ACQUIRE_LOCK;
  713. cp_abortChanges(message, retry);
  714. return ok();
  715. }
  716. binder::Status VoldNativeService::supportsCheckpoint(bool* _aidl_return) {
  717. ENFORCE_UID(AID_SYSTEM);
  718. ACQUIRE_LOCK;
  719. return cp_supportsCheckpoint(*_aidl_return);
  720. }
  721. binder::Status VoldNativeService::supportsBlockCheckpoint(bool* _aidl_return) {
  722. ENFORCE_UID(AID_SYSTEM);
  723. ACQUIRE_LOCK;
  724. return cp_supportsBlockCheckpoint(*_aidl_return);
  725. }
  726. binder::Status VoldNativeService::supportsFileCheckpoint(bool* _aidl_return) {
  727. ENFORCE_UID(AID_SYSTEM);
  728. ACQUIRE_LOCK;
  729. return cp_supportsFileCheckpoint(*_aidl_return);
  730. }
  731. } // namespace vold
  732. } // namespace android