blob: 6fb1731683c74f2f11d99f9279e458e866652943 [file] [log] [blame]
Jeff Sharkey068c6be2017-09-06 13:47:40 -06001/*
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
Jeff Sharkey67b8c492017-09-21 17:08:43 -060017#define ATRACE_TAG ATRACE_TAG_PACKAGE_MANAGER
18
Jeff Sharkey068c6be2017-09-06 13:47:40 -060019#include "VoldNativeService.h"
20#include "VolumeManager.h"
Jeff Sharkey52f7a912017-09-15 12:57:44 -060021#include "BenchmarkTask.h"
Jeff Sharkey9462bdd2017-09-07 15:27:28 -060022#include "MoveTask.h"
Jeff Sharkey83b559c2017-09-12 16:30:52 -060023#include "Process.h"
Jeff Sharkey11c2d382017-09-11 10:32:01 -060024#include "TrimTask.h"
Jeff Sharkey068c6be2017-09-06 13:47:40 -060025
Jeff Sharkey83b559c2017-09-12 16:30:52 -060026#include "cryptfs.h"
27#include "Ext4Crypt.h"
28#include "MetadataCrypt.h"
29
Jeff Sharkey068c6be2017-09-06 13:47:40 -060030#include <fstream>
31
32#include <android-base/logging.h>
33#include <android-base/stringprintf.h>
34#include <android-base/strings.h>
Paul Crowley3b71fc52017-10-09 10:55:21 -070035#include <ext4_utils/ext4_crypt.h>
Jeff Sharkey11c2d382017-09-11 10:32:01 -060036#include <fs_mgr.h>
Jeff Sharkey068c6be2017-09-06 13:47:40 -060037#include <private/android_filesystem_config.h>
Jeff Sharkey67b8c492017-09-21 17:08:43 -060038#include <utils/Trace.h>
Jeff Sharkey068c6be2017-09-06 13:47:40 -060039
40#ifndef LOG_TAG
41#define LOG_TAG "vold"
42#endif
43
44using android::base::StringPrintf;
45using std::endl;
46
47namespace android {
48namespace vold {
49
50namespace {
51
52constexpr const char* kDump = "android.permission.DUMP";
53
54static binder::Status ok() {
55 return binder::Status::ok();
56}
57
58static binder::Status exception(uint32_t code, const std::string& msg) {
59 return binder::Status::fromExceptionCode(code, String8(msg.c_str()));
60}
61
Jeff Sharkey9462bdd2017-09-07 15:27:28 -060062static binder::Status error(const std::string& msg) {
63 PLOG(ERROR) << msg;
64 return binder::Status::fromServiceSpecificError(errno, String8(msg.c_str()));
65}
66
Jeff Sharkey83b559c2017-09-12 16:30:52 -060067static binder::Status translate(int status) {
Jeff Sharkey9462bdd2017-09-07 15:27:28 -060068 if (status == 0) {
69 return binder::Status::ok();
70 } else {
Jeff Sharkey11c2d382017-09-11 10:32:01 -060071 return binder::Status::fromServiceSpecificError(status);
Jeff Sharkey9462bdd2017-09-07 15:27:28 -060072 }
73}
74
Jeff Sharkey83b559c2017-09-12 16:30:52 -060075static binder::Status translateBool(bool status) {
76 if (status) {
77 return binder::Status::ok();
78 } else {
79 return binder::Status::fromServiceSpecificError(status);
80 }
81}
82
Jeff Sharkey068c6be2017-09-06 13:47:40 -060083binder::Status checkPermission(const char* permission) {
84 pid_t pid;
85 uid_t uid;
86
87 if (checkCallingPermission(String16(permission), reinterpret_cast<int32_t*>(&pid),
88 reinterpret_cast<int32_t*>(&uid))) {
89 return ok();
90 } else {
91 return exception(binder::Status::EX_SECURITY,
92 StringPrintf("UID %d / PID %d lacks permission %s", uid, pid, permission));
93 }
94}
95
96binder::Status checkUid(uid_t expectedUid) {
97 uid_t uid = IPCThreadState::self()->getCallingUid();
98 if (uid == expectedUid || uid == AID_ROOT) {
99 return ok();
100 } else {
101 return exception(binder::Status::EX_SECURITY,
102 StringPrintf("UID %d is not expected UID %d", uid, expectedUid));
103 }
104}
105
Jeff Sharkeyec4fda22017-09-12 13:19:24 -0600106binder::Status checkArgumentId(const std::string& id) {
107 if (id.empty()) {
108 return exception(binder::Status::EX_ILLEGAL_ARGUMENT, "Missing ID");
109 }
110 for (const char& c : id) {
111 if (!std::isalnum(c) && c != ':' && c != ',') {
112 return exception(binder::Status::EX_ILLEGAL_ARGUMENT,
113 StringPrintf("ID %s is malformed", id.c_str()));
114 }
115 }
116 return ok();
117}
118
119binder::Status checkArgumentPath(const std::string& path) {
120 if (path.empty()) {
121 return exception(binder::Status::EX_ILLEGAL_ARGUMENT, "Missing path");
122 }
123 if (path[0] != '/') {
124 return exception(binder::Status::EX_ILLEGAL_ARGUMENT,
125 StringPrintf("Path %s is relative", path.c_str()));
126 }
127 for (const char& c : path) {
128 if (c == '\0' || c == '\n') {
129 return exception(binder::Status::EX_ILLEGAL_ARGUMENT,
130 StringPrintf("Path %s is malformed", path.c_str()));
131 }
132 }
133 return ok();
134}
135
136binder::Status checkArgumentHex(const std::string& hex) {
137 // Empty hex strings are allowed
138 for (const char& c : hex) {
139 if (!std::isxdigit(c) && c != ':' && c != '-') {
140 return exception(binder::Status::EX_ILLEGAL_ARGUMENT,
141 StringPrintf("Hex %s is malformed", hex.c_str()));
142 }
143 }
144 return ok();
145}
146
Jeff Sharkey068c6be2017-09-06 13:47:40 -0600147#define ENFORCE_UID(uid) { \
148 binder::Status status = checkUid((uid)); \
149 if (!status.isOk()) { \
150 return status; \
151 } \
152}
153
Jeff Sharkeyec4fda22017-09-12 13:19:24 -0600154#define CHECK_ARGUMENT_ID(id) { \
155 binder::Status status = checkArgumentId((id)); \
156 if (!status.isOk()) { \
157 return status; \
158 } \
159}
160
161#define CHECK_ARGUMENT_PATH(path) { \
162 binder::Status status = checkArgumentPath((path)); \
163 if (!status.isOk()) { \
164 return status; \
165 } \
166}
167
168#define CHECK_ARGUMENT_HEX(hex) { \
169 binder::Status status = checkArgumentHex((hex)); \
170 if (!status.isOk()) { \
171 return status; \
172 } \
173}
174
Jeff Sharkey83b559c2017-09-12 16:30:52 -0600175#define ACQUIRE_LOCK \
Jeff Sharkey67b8c492017-09-21 17:08:43 -0600176 std::lock_guard<std::mutex> lock(VolumeManager::Instance()->getLock()); \
177 ATRACE_CALL();
Jeff Sharkey83b559c2017-09-12 16:30:52 -0600178
179#define ACQUIRE_CRYPT_LOCK \
Jeff Sharkey67b8c492017-09-21 17:08:43 -0600180 std::lock_guard<std::mutex> lock(VolumeManager::Instance()->getCryptLock()); \
181 ATRACE_CALL();
Jeff Sharkey9462bdd2017-09-07 15:27:28 -0600182
Jeff Sharkey068c6be2017-09-06 13:47:40 -0600183} // namespace
184
185status_t VoldNativeService::start() {
186 IPCThreadState::self()->disableBackgroundScheduling(true);
187 status_t ret = BinderService<VoldNativeService>::publish();
188 if (ret != android::OK) {
189 return ret;
190 }
191 sp<ProcessState> ps(ProcessState::self());
192 ps->startThreadPool();
193 ps->giveThreadPoolName();
194 return android::OK;
195}
196
197status_t VoldNativeService::dump(int fd, const Vector<String16> & /* args */) {
198 auto out = std::fstream(StringPrintf("/proc/self/fd/%d", fd));
199 const binder::Status dump_permission = checkPermission(kDump);
200 if (!dump_permission.isOk()) {
201 out << dump_permission.toString8() << endl;
202 return PERMISSION_DENIED;
203 }
204
Jeff Sharkey9462bdd2017-09-07 15:27:28 -0600205 ACQUIRE_LOCK;
Jeff Sharkey068c6be2017-09-06 13:47:40 -0600206 out << "vold is happy!" << endl;
207 out.flush();
Jeff Sharkey068c6be2017-09-06 13:47:40 -0600208 return NO_ERROR;
209}
210
Jeff Sharkey814e9d32017-09-13 11:49:44 -0600211binder::Status VoldNativeService::setListener(
212 const android::sp<android::os::IVoldListener>& listener) {
213 ENFORCE_UID(AID_SYSTEM);
214 ACQUIRE_LOCK;
215
216 VolumeManager::Instance()->setListener(listener);
217 return ok();
218}
219
Jeff Sharkeycbe69fc2017-09-15 16:50:28 -0600220binder::Status VoldNativeService::monitor() {
221 ENFORCE_UID(AID_SYSTEM);
222
223 // Simply acquire/release each lock for watchdog
224 {
225 ACQUIRE_LOCK;
226 }
227 {
228 ACQUIRE_CRYPT_LOCK;
229 }
230
231 return ok();
232}
233
Jeff Sharkey068c6be2017-09-06 13:47:40 -0600234binder::Status VoldNativeService::reset() {
235 ENFORCE_UID(AID_SYSTEM);
Jeff Sharkey9462bdd2017-09-07 15:27:28 -0600236 ACQUIRE_LOCK;
Jeff Sharkey068c6be2017-09-06 13:47:40 -0600237
Jeff Sharkey9462bdd2017-09-07 15:27:28 -0600238 return translate(VolumeManager::Instance()->reset());
239}
240
241binder::Status VoldNativeService::shutdown() {
242 ENFORCE_UID(AID_SYSTEM);
243 ACQUIRE_LOCK;
244
245 return translate(VolumeManager::Instance()->shutdown());
246}
247
Jeff Sharkey11c2d382017-09-11 10:32:01 -0600248binder::Status VoldNativeService::mountAll() {
Jeff Sharkey9462bdd2017-09-07 15:27:28 -0600249 ENFORCE_UID(AID_SYSTEM);
250 ACQUIRE_LOCK;
251
Jeff Sharkey11c2d382017-09-11 10:32:01 -0600252 struct fstab* fstab = fs_mgr_read_fstab_default();
253 int res = fs_mgr_mount_all(fstab, MOUNT_MODE_DEFAULT);
254 fs_mgr_free_fstab(fstab);
255 return translate(res);
Jeff Sharkey9462bdd2017-09-07 15:27:28 -0600256}
257
258binder::Status VoldNativeService::onUserAdded(int32_t userId, int32_t userSerial) {
259 ENFORCE_UID(AID_SYSTEM);
260 ACQUIRE_LOCK;
261
262 return translate(VolumeManager::Instance()->onUserAdded(userId, userSerial));
263}
264
265binder::Status VoldNativeService::onUserRemoved(int32_t userId) {
266 ENFORCE_UID(AID_SYSTEM);
267 ACQUIRE_LOCK;
268
269 return translate(VolumeManager::Instance()->onUserRemoved(userId));
270}
271
272binder::Status VoldNativeService::onUserStarted(int32_t userId) {
273 ENFORCE_UID(AID_SYSTEM);
274 ACQUIRE_LOCK;
275
276 return translate(VolumeManager::Instance()->onUserStarted(userId));
277}
278
279binder::Status VoldNativeService::onUserStopped(int32_t userId) {
280 ENFORCE_UID(AID_SYSTEM);
281 ACQUIRE_LOCK;
282
283 return translate(VolumeManager::Instance()->onUserStopped(userId));
284}
285
Jeff Sharkey11c2d382017-09-11 10:32:01 -0600286binder::Status VoldNativeService::partition(const std::string& diskId, int32_t partitionType,
287 int32_t ratio) {
Jeff Sharkey9462bdd2017-09-07 15:27:28 -0600288 ENFORCE_UID(AID_SYSTEM);
Jeff Sharkeyec4fda22017-09-12 13:19:24 -0600289 CHECK_ARGUMENT_ID(diskId);
Jeff Sharkey9462bdd2017-09-07 15:27:28 -0600290 ACQUIRE_LOCK;
291
292 auto disk = VolumeManager::Instance()->findDisk(diskId);
293 if (disk == nullptr) {
294 return error("Failed to find disk " + diskId);
295 }
296 switch (partitionType) {
297 case PARTITION_TYPE_PUBLIC: return translate(disk->partitionPublic());
298 case PARTITION_TYPE_PRIVATE: return translate(disk->partitionPrivate());
299 case PARTITION_TYPE_MIXED: return translate(disk->partitionMixed(ratio));
300 default: return error("Unknown type " + std::to_string(partitionType));
301 }
302}
303
304binder::Status VoldNativeService::forgetPartition(const std::string& partGuid) {
305 ENFORCE_UID(AID_SYSTEM);
Jeff Sharkeyec4fda22017-09-12 13:19:24 -0600306 CHECK_ARGUMENT_HEX(partGuid);
Jeff Sharkey9462bdd2017-09-07 15:27:28 -0600307 ACQUIRE_LOCK;
308
309 return translate(VolumeManager::Instance()->forgetPartition(partGuid));
310}
311
Jeff Sharkey11c2d382017-09-11 10:32:01 -0600312binder::Status VoldNativeService::mount(const std::string& volId, int32_t mountFlags,
313 int32_t mountUserId) {
Jeff Sharkey9462bdd2017-09-07 15:27:28 -0600314 ENFORCE_UID(AID_SYSTEM);
Jeff Sharkeyec4fda22017-09-12 13:19:24 -0600315 CHECK_ARGUMENT_ID(volId);
Jeff Sharkey9462bdd2017-09-07 15:27:28 -0600316 ACQUIRE_LOCK;
317
318 auto vol = VolumeManager::Instance()->findVolume(volId);
319 if (vol == nullptr) {
320 return error("Failed to find volume " + volId);
321 }
322
323 vol->setMountFlags(mountFlags);
324 vol->setMountUserId(mountUserId);
325
326 int res = vol->mount();
Jeff Sharkey83b559c2017-09-12 16:30:52 -0600327 if ((mountFlags & MOUNT_FLAG_PRIMARY) != 0) {
Jeff Sharkey9462bdd2017-09-07 15:27:28 -0600328 VolumeManager::Instance()->setPrimary(vol);
329 }
330 return translate(res);
331}
332
333binder::Status VoldNativeService::unmount(const std::string& volId) {
334 ENFORCE_UID(AID_SYSTEM);
Jeff Sharkeyec4fda22017-09-12 13:19:24 -0600335 CHECK_ARGUMENT_ID(volId);
Jeff Sharkey9462bdd2017-09-07 15:27:28 -0600336 ACQUIRE_LOCK;
337
338 auto vol = VolumeManager::Instance()->findVolume(volId);
339 if (vol == nullptr) {
340 return error("Failed to find volume " + volId);
341 }
342 return translate(vol->unmount());
343}
344
345binder::Status VoldNativeService::format(const std::string& volId, const std::string& fsType) {
346 ENFORCE_UID(AID_SYSTEM);
Jeff Sharkeyec4fda22017-09-12 13:19:24 -0600347 CHECK_ARGUMENT_ID(volId);
Jeff Sharkey9462bdd2017-09-07 15:27:28 -0600348 ACQUIRE_LOCK;
349
350 auto vol = VolumeManager::Instance()->findVolume(volId);
351 if (vol == nullptr) {
352 return error("Failed to find volume " + volId);
353 }
354 return translate(vol->format(fsType));
355}
356
Jeff Sharkey52f7a912017-09-15 12:57:44 -0600357binder::Status VoldNativeService::benchmark(const std::string& volId,
358 const android::sp<android::os::IVoldTaskListener>& listener) {
Jeff Sharkey9462bdd2017-09-07 15:27:28 -0600359 ENFORCE_UID(AID_SYSTEM);
Jeff Sharkeyec4fda22017-09-12 13:19:24 -0600360 CHECK_ARGUMENT_ID(volId);
Jeff Sharkey9462bdd2017-09-07 15:27:28 -0600361 ACQUIRE_LOCK;
362
Jeff Sharkey52f7a912017-09-15 12:57:44 -0600363 std::string path;
364 if (volId == "private" || volId == "null") {
365 path = "/data";
366 } else {
367 auto vol = VolumeManager::Instance()->findVolume(volId);
368 if (vol == nullptr) {
369 return error("Failed to find volume " + volId);
370 }
371 if (vol->getType() != VolumeBase::Type::kPrivate) {
372 return error("Volume " + volId + " not private");
373 }
374 if (vol->getState() != VolumeBase::State::kMounted) {
375 return error("Volume " + volId + " not mounted");
376 }
377 path = vol->getPath();
378 }
379
380 if (path.empty()) {
381 return error("Volume " + volId + " missing path");
382 }
383
384 (new android::vold::BenchmarkTask(path, listener))->start();
Jeff Sharkey068c6be2017-09-06 13:47:40 -0600385 return ok();
386}
387
Jeff Sharkey11c2d382017-09-11 10:32:01 -0600388binder::Status VoldNativeService::moveStorage(const std::string& fromVolId,
Jeff Sharkey52f7a912017-09-15 12:57:44 -0600389 const std::string& toVolId, const android::sp<android::os::IVoldTaskListener>& listener) {
Jeff Sharkey9462bdd2017-09-07 15:27:28 -0600390 ENFORCE_UID(AID_SYSTEM);
Jeff Sharkeyec4fda22017-09-12 13:19:24 -0600391 CHECK_ARGUMENT_ID(fromVolId);
392 CHECK_ARGUMENT_ID(toVolId);
Jeff Sharkey9462bdd2017-09-07 15:27:28 -0600393 ACQUIRE_LOCK;
394
395 auto fromVol = VolumeManager::Instance()->findVolume(fromVolId);
396 auto toVol = VolumeManager::Instance()->findVolume(toVolId);
397 if (fromVol == nullptr) {
398 return error("Failed to find volume " + fromVolId);
399 } else if (toVol == nullptr) {
400 return error("Failed to find volume " + toVolId);
401 }
Jeff Sharkey52f7a912017-09-15 12:57:44 -0600402 (new android::vold::MoveTask(fromVol, toVol, listener))->start();
Jeff Sharkey9462bdd2017-09-07 15:27:28 -0600403 return ok();
404}
405
406binder::Status VoldNativeService::remountUid(int32_t uid, int32_t remountMode) {
407 ENFORCE_UID(AID_SYSTEM);
408 ACQUIRE_LOCK;
409
410 std::string tmp;
411 switch (remountMode) {
412 case REMOUNT_MODE_NONE: tmp = "none"; break;
413 case REMOUNT_MODE_DEFAULT: tmp = "default"; break;
414 case REMOUNT_MODE_READ: tmp = "read"; break;
415 case REMOUNT_MODE_WRITE: tmp = "write"; break;
416 default: return error("Unknown mode " + std::to_string(remountMode));
417 }
418 return translate(VolumeManager::Instance()->remountUid(uid, tmp));
419}
420
421binder::Status VoldNativeService::mkdirs(const std::string& path) {
422 ENFORCE_UID(AID_SYSTEM);
Jeff Sharkeyec4fda22017-09-12 13:19:24 -0600423 CHECK_ARGUMENT_PATH(path);
Jeff Sharkey9462bdd2017-09-07 15:27:28 -0600424 ACQUIRE_LOCK;
425
426 return translate(VolumeManager::Instance()->mkdirs(path.c_str()));
427}
428
Jeff Sharkey11c2d382017-09-11 10:32:01 -0600429binder::Status VoldNativeService::createObb(const std::string& sourcePath,
430 const std::string& sourceKey, int32_t ownerGid, std::string* _aidl_return) {
431 ENFORCE_UID(AID_SYSTEM);
Jeff Sharkeyec4fda22017-09-12 13:19:24 -0600432 CHECK_ARGUMENT_PATH(sourcePath);
433 CHECK_ARGUMENT_HEX(sourceKey);
Jeff Sharkey11c2d382017-09-11 10:32:01 -0600434 ACQUIRE_LOCK;
435
436 return translate(
437 VolumeManager::Instance()->createObb(sourcePath, sourceKey, ownerGid, _aidl_return));
438}
439
440binder::Status VoldNativeService::destroyObb(const std::string& volId) {
441 ENFORCE_UID(AID_SYSTEM);
Jeff Sharkeyec4fda22017-09-12 13:19:24 -0600442 CHECK_ARGUMENT_ID(volId);
Jeff Sharkey11c2d382017-09-11 10:32:01 -0600443 ACQUIRE_LOCK;
444
445 return translate(VolumeManager::Instance()->destroyObb(volId));
446}
447
Jeff Sharkey52f7a912017-09-15 12:57:44 -0600448binder::Status VoldNativeService::fstrim(int32_t fstrimFlags,
449 const android::sp<android::os::IVoldTaskListener>& listener) {
Jeff Sharkey11c2d382017-09-11 10:32:01 -0600450 ENFORCE_UID(AID_SYSTEM);
451 ACQUIRE_LOCK;
452
Jeff Sharkey52f7a912017-09-15 12:57:44 -0600453 (new android::vold::TrimTask(fstrimFlags, listener))->start();
Jeff Sharkey11c2d382017-09-11 10:32:01 -0600454 return ok();
455}
456
457binder::Status VoldNativeService::mountAppFuse(int32_t uid, int32_t pid, int32_t mountId,
458 android::base::unique_fd* _aidl_return) {
459 ENFORCE_UID(AID_SYSTEM);
460 ACQUIRE_LOCK;
461
462 return translate(VolumeManager::Instance()->mountAppFuse(uid, pid, mountId, _aidl_return));
463}
464
465binder::Status VoldNativeService::unmountAppFuse(int32_t uid, int32_t pid, int32_t mountId) {
466 ENFORCE_UID(AID_SYSTEM);
467 ACQUIRE_LOCK;
468
469 return translate(VolumeManager::Instance()->unmountAppFuse(uid, pid, mountId));
470}
471
Jeff Sharkey83b559c2017-09-12 16:30:52 -0600472binder::Status VoldNativeService::fdeCheckPassword(const std::string& password) {
473 ENFORCE_UID(AID_SYSTEM);
474 ACQUIRE_CRYPT_LOCK;
475
476 return translate(cryptfs_check_passwd(password.c_str()));
477}
478
479binder::Status VoldNativeService::fdeRestart() {
480 ENFORCE_UID(AID_SYSTEM);
481 ACQUIRE_CRYPT_LOCK;
482
483 // Spawn as thread so init can issue commands back to vold without
484 // causing deadlock, usually as a result of prep_data_fs.
485 std::thread(&cryptfs_restart).detach();
486 return ok();
487}
488
489binder::Status VoldNativeService::fdeComplete(int32_t* _aidl_return) {
490 ENFORCE_UID(AID_SYSTEM);
491 ACQUIRE_CRYPT_LOCK;
492
493 *_aidl_return = cryptfs_crypto_complete();
494 return ok();
495}
496
497static int fdeEnableInternal(int32_t passwordType, const std::string& password,
498 int32_t encryptionFlags) {
499 bool noUi = (encryptionFlags & VoldNativeService::ENCRYPTION_FLAG_NO_UI) != 0;
500
501 std::string how;
502 if ((encryptionFlags & VoldNativeService::ENCRYPTION_FLAG_IN_PLACE) != 0) {
503 how = "inplace";
504 } else if ((encryptionFlags & VoldNativeService::ENCRYPTION_FLAG_WIPE) != 0) {
505 how = "wipe";
506 } else {
507 LOG(ERROR) << "Missing encryption flag";
508 return -1;
509 }
510
511 for (int tries = 0; tries < 2; ++tries) {
512 int rc;
513 if (passwordType == VoldNativeService::PASSWORD_TYPE_DEFAULT) {
514 rc = cryptfs_enable_default(how.c_str(), noUi);
515 } else {
516 rc = cryptfs_enable(how.c_str(), passwordType, password.c_str(), noUi);
517 }
518
519 if (rc == 0) {
520 return 0;
521 } else if (tries == 0) {
522 Process::killProcessesWithOpenFiles(DATA_MNT_POINT, SIGKILL);
523 }
524 }
525
526 return -1;
527}
528
529binder::Status VoldNativeService::fdeEnable(int32_t passwordType,
530 const std::string& password, int32_t encryptionFlags) {
531 ENFORCE_UID(AID_SYSTEM);
532 ACQUIRE_CRYPT_LOCK;
533
534 if (e4crypt_is_native()) {
535 if (passwordType != PASSWORD_TYPE_DEFAULT) {
536 return error("Unexpected password type");
537 }
538 if (encryptionFlags != (ENCRYPTION_FLAG_IN_PLACE | ENCRYPTION_FLAG_NO_UI)) {
539 return error("Unexpected flags");
540 }
541 return translateBool(e4crypt_enable_crypto());
542 }
543
544 // Spawn as thread so init can issue commands back to vold without
545 // causing deadlock, usually as a result of prep_data_fs.
546 std::thread(&fdeEnableInternal, passwordType, password, encryptionFlags).detach();
547 return ok();
548}
549
550binder::Status VoldNativeService::fdeChangePassword(int32_t passwordType,
551 const std::string& password) {
552 ENFORCE_UID(AID_SYSTEM);
553 ACQUIRE_CRYPT_LOCK;
554
555 return translate(cryptfs_changepw(passwordType, password.c_str()));
556}
557
558binder::Status VoldNativeService::fdeVerifyPassword(const std::string& password) {
559 ENFORCE_UID(AID_SYSTEM);
560 ACQUIRE_CRYPT_LOCK;
561
562 return translate(cryptfs_verify_passwd(password.c_str()));
563}
564
565binder::Status VoldNativeService::fdeGetField(const std::string& key,
566 std::string* _aidl_return) {
567 ENFORCE_UID(AID_SYSTEM);
568 ACQUIRE_CRYPT_LOCK;
569
570 char buf[PROPERTY_VALUE_MAX];
571 if (cryptfs_getfield(key.c_str(), buf, sizeof(buf)) != CRYPTO_GETFIELD_OK) {
572 return error(StringPrintf("Failed to read field %s", key.c_str()));
573 } else {
574 *_aidl_return = buf;
575 return ok();
576 }
577}
578
579binder::Status VoldNativeService::fdeSetField(const std::string& key,
580 const std::string& value) {
581 ENFORCE_UID(AID_SYSTEM);
582 ACQUIRE_CRYPT_LOCK;
583
584 return translate(cryptfs_setfield(key.c_str(), value.c_str()));
585}
586
587binder::Status VoldNativeService::fdeGetPasswordType(int32_t* _aidl_return) {
588 ENFORCE_UID(AID_SYSTEM);
589 ACQUIRE_CRYPT_LOCK;
590
591 *_aidl_return = cryptfs_get_password_type();
592 return ok();
593}
594
595binder::Status VoldNativeService::fdeGetPassword(std::string* _aidl_return) {
596 ENFORCE_UID(AID_SYSTEM);
597 ACQUIRE_CRYPT_LOCK;
598
599 const char* res = cryptfs_get_password();
600 if (res != nullptr) {
601 *_aidl_return = res;
602 }
603 return ok();
604}
605
606binder::Status VoldNativeService::fdeClearPassword() {
607 ENFORCE_UID(AID_SYSTEM);
608 ACQUIRE_CRYPT_LOCK;
609
610 cryptfs_clear_password();
611 return ok();
612}
613
614binder::Status VoldNativeService::fbeEnable() {
615 ENFORCE_UID(AID_SYSTEM);
616 ACQUIRE_CRYPT_LOCK;
617
618 return translateBool(e4crypt_initialize_global_de());
619}
620
621binder::Status VoldNativeService::mountDefaultEncrypted() {
622 ENFORCE_UID(AID_SYSTEM);
623 ACQUIRE_CRYPT_LOCK;
624
625 if (e4crypt_is_native()) {
626 return translateBool(e4crypt_mount_metadata_encrypted());
627 } else {
628 // Spawn as thread so init can issue commands back to vold without
629 // causing deadlock, usually as a result of prep_data_fs.
630 std::thread(&cryptfs_mount_default_encrypted).detach();
631 return ok();
632 }
633}
634
635binder::Status VoldNativeService::initUser0() {
636 ENFORCE_UID(AID_SYSTEM);
637 ACQUIRE_CRYPT_LOCK;
638
639 return translateBool(e4crypt_init_user0());
640}
641
642binder::Status VoldNativeService::isConvertibleToFbe(bool* _aidl_return) {
643 ENFORCE_UID(AID_SYSTEM);
644 ACQUIRE_CRYPT_LOCK;
645
646 *_aidl_return = cryptfs_isConvertibleToFBE() != 0;
647 return ok();
648}
649
650binder::Status VoldNativeService::createUserKey(int32_t userId, int32_t userSerial,
651 bool ephemeral) {
652 ENFORCE_UID(AID_SYSTEM);
653 ACQUIRE_CRYPT_LOCK;
654
655 return translateBool(e4crypt_vold_create_user_key(userId, userSerial, ephemeral));
656}
657
658binder::Status VoldNativeService::destroyUserKey(int32_t userId) {
659 ENFORCE_UID(AID_SYSTEM);
660 ACQUIRE_CRYPT_LOCK;
661
662 return translateBool(e4crypt_destroy_user_key(userId));
663}
664
665binder::Status VoldNativeService::addUserKeyAuth(int32_t userId, int32_t userSerial,
666 const std::string& token, const std::string& secret) {
667 ENFORCE_UID(AID_SYSTEM);
668 ACQUIRE_CRYPT_LOCK;
669
Paul Crowley3b71fc52017-10-09 10:55:21 -0700670 return translateBool(e4crypt_add_user_key_auth(userId, userSerial, token, secret));
Jeff Sharkey83b559c2017-09-12 16:30:52 -0600671}
672
673binder::Status VoldNativeService::fixateNewestUserKeyAuth(int32_t userId) {
674 ENFORCE_UID(AID_SYSTEM);
675 ACQUIRE_CRYPT_LOCK;
676
677 return translateBool(e4crypt_fixate_newest_user_key_auth(userId));
678}
679
680binder::Status VoldNativeService::unlockUserKey(int32_t userId, int32_t userSerial,
681 const std::string& token, const std::string& secret) {
682 ENFORCE_UID(AID_SYSTEM);
683 ACQUIRE_CRYPT_LOCK;
684
Paul Crowley3b71fc52017-10-09 10:55:21 -0700685 return translateBool(e4crypt_unlock_user_key(userId, userSerial, token, secret));
Jeff Sharkey83b559c2017-09-12 16:30:52 -0600686}
687
688binder::Status VoldNativeService::lockUserKey(int32_t userId) {
689 ENFORCE_UID(AID_SYSTEM);
690 ACQUIRE_CRYPT_LOCK;
691
692 return translateBool(e4crypt_lock_user_key(userId));
693}
694
695binder::Status VoldNativeService::prepareUserStorage(const std::unique_ptr<std::string>& uuid,
696 int32_t userId, int32_t userSerial, int32_t flags) {
697 ENFORCE_UID(AID_SYSTEM);
698 ACQUIRE_CRYPT_LOCK;
699
Paul Crowley3b71fc52017-10-09 10:55:21 -0700700 std::string empty_string = "";
701 auto uuid_ = uuid ? *uuid : empty_string;
Jeff Sharkey83b559c2017-09-12 16:30:52 -0600702 return translateBool(e4crypt_prepare_user_storage(uuid_, userId, userSerial, flags));
703}
704
705binder::Status VoldNativeService::destroyUserStorage(const std::unique_ptr<std::string>& uuid,
706 int32_t userId, int32_t flags) {
707 ENFORCE_UID(AID_SYSTEM);
708 ACQUIRE_CRYPT_LOCK;
709
Paul Crowley3b71fc52017-10-09 10:55:21 -0700710 std::string empty_string = "";
711 auto uuid_ = uuid ? *uuid : empty_string;
Jeff Sharkey83b559c2017-09-12 16:30:52 -0600712 return translateBool(e4crypt_destroy_user_storage(uuid_, userId, flags));
713}
714
715binder::Status VoldNativeService::secdiscard(const std::string& path) {
716 ENFORCE_UID(AID_SYSTEM);
717 ACQUIRE_CRYPT_LOCK;
718
Paul Crowley3b71fc52017-10-09 10:55:21 -0700719 return translateBool(e4crypt_secdiscard(path));
Jeff Sharkey83b559c2017-09-12 16:30:52 -0600720}
721
Jeff Sharkey068c6be2017-09-06 13:47:40 -0600722} // namespace vold
723} // namespace android