blob: f5f0838a3f9d62b1b3025438618a41bc61d2b8c3 [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
17#include "VoldNativeService.h"
18#include "VolumeManager.h"
Jeff Sharkey9462bdd2017-09-07 15:27:28 -060019#include "MoveTask.h"
Jeff Sharkey83b559c2017-09-12 16:30:52 -060020#include "Process.h"
Jeff Sharkey11c2d382017-09-11 10:32:01 -060021#include "TrimTask.h"
Jeff Sharkey068c6be2017-09-06 13:47:40 -060022
Jeff Sharkey83b559c2017-09-12 16:30:52 -060023#include "cryptfs.h"
24#include "Ext4Crypt.h"
25#include "MetadataCrypt.h"
26
Jeff Sharkey068c6be2017-09-06 13:47:40 -060027#include <fstream>
28
29#include <android-base/logging.h>
30#include <android-base/stringprintf.h>
31#include <android-base/strings.h>
Jeff Sharkey11c2d382017-09-11 10:32:01 -060032#include <fs_mgr.h>
Jeff Sharkey068c6be2017-09-06 13:47:40 -060033#include <private/android_filesystem_config.h>
34
35#ifndef LOG_TAG
36#define LOG_TAG "vold"
37#endif
38
39using android::base::StringPrintf;
40using std::endl;
41
42namespace android {
43namespace vold {
44
45namespace {
46
47constexpr const char* kDump = "android.permission.DUMP";
48
49static binder::Status ok() {
50 return binder::Status::ok();
51}
52
53static binder::Status exception(uint32_t code, const std::string& msg) {
54 return binder::Status::fromExceptionCode(code, String8(msg.c_str()));
55}
56
Jeff Sharkey9462bdd2017-09-07 15:27:28 -060057static binder::Status error(const std::string& msg) {
58 PLOG(ERROR) << msg;
59 return binder::Status::fromServiceSpecificError(errno, String8(msg.c_str()));
60}
61
Jeff Sharkey83b559c2017-09-12 16:30:52 -060062static binder::Status translate(int status) {
Jeff Sharkey9462bdd2017-09-07 15:27:28 -060063 if (status == 0) {
64 return binder::Status::ok();
65 } else {
Jeff Sharkey11c2d382017-09-11 10:32:01 -060066 return binder::Status::fromServiceSpecificError(status);
Jeff Sharkey9462bdd2017-09-07 15:27:28 -060067 }
68}
69
Jeff Sharkey83b559c2017-09-12 16:30:52 -060070static binder::Status translateBool(bool status) {
71 if (status) {
72 return binder::Status::ok();
73 } else {
74 return binder::Status::fromServiceSpecificError(status);
75 }
76}
77
Jeff Sharkey068c6be2017-09-06 13:47:40 -060078binder::Status checkPermission(const char* permission) {
79 pid_t pid;
80 uid_t uid;
81
82 if (checkCallingPermission(String16(permission), reinterpret_cast<int32_t*>(&pid),
83 reinterpret_cast<int32_t*>(&uid))) {
84 return ok();
85 } else {
86 return exception(binder::Status::EX_SECURITY,
87 StringPrintf("UID %d / PID %d lacks permission %s", uid, pid, permission));
88 }
89}
90
91binder::Status checkUid(uid_t expectedUid) {
92 uid_t uid = IPCThreadState::self()->getCallingUid();
93 if (uid == expectedUid || uid == AID_ROOT) {
94 return ok();
95 } else {
96 return exception(binder::Status::EX_SECURITY,
97 StringPrintf("UID %d is not expected UID %d", uid, expectedUid));
98 }
99}
100
Jeff Sharkeyec4fda22017-09-12 13:19:24 -0600101binder::Status checkArgumentId(const std::string& id) {
102 if (id.empty()) {
103 return exception(binder::Status::EX_ILLEGAL_ARGUMENT, "Missing ID");
104 }
105 for (const char& c : id) {
106 if (!std::isalnum(c) && c != ':' && c != ',') {
107 return exception(binder::Status::EX_ILLEGAL_ARGUMENT,
108 StringPrintf("ID %s is malformed", id.c_str()));
109 }
110 }
111 return ok();
112}
113
114binder::Status checkArgumentPath(const std::string& path) {
115 if (path.empty()) {
116 return exception(binder::Status::EX_ILLEGAL_ARGUMENT, "Missing path");
117 }
118 if (path[0] != '/') {
119 return exception(binder::Status::EX_ILLEGAL_ARGUMENT,
120 StringPrintf("Path %s is relative", path.c_str()));
121 }
122 for (const char& c : path) {
123 if (c == '\0' || c == '\n') {
124 return exception(binder::Status::EX_ILLEGAL_ARGUMENT,
125 StringPrintf("Path %s is malformed", path.c_str()));
126 }
127 }
128 return ok();
129}
130
131binder::Status checkArgumentHex(const std::string& hex) {
132 // Empty hex strings are allowed
133 for (const char& c : hex) {
134 if (!std::isxdigit(c) && c != ':' && c != '-') {
135 return exception(binder::Status::EX_ILLEGAL_ARGUMENT,
136 StringPrintf("Hex %s is malformed", hex.c_str()));
137 }
138 }
139 return ok();
140}
141
Jeff Sharkey068c6be2017-09-06 13:47:40 -0600142#define ENFORCE_UID(uid) { \
143 binder::Status status = checkUid((uid)); \
144 if (!status.isOk()) { \
145 return status; \
146 } \
147}
148
Jeff Sharkeyec4fda22017-09-12 13:19:24 -0600149#define CHECK_ARGUMENT_ID(id) { \
150 binder::Status status = checkArgumentId((id)); \
151 if (!status.isOk()) { \
152 return status; \
153 } \
154}
155
156#define CHECK_ARGUMENT_PATH(path) { \
157 binder::Status status = checkArgumentPath((path)); \
158 if (!status.isOk()) { \
159 return status; \
160 } \
161}
162
163#define CHECK_ARGUMENT_HEX(hex) { \
164 binder::Status status = checkArgumentHex((hex)); \
165 if (!status.isOk()) { \
166 return status; \
167 } \
168}
169
Jeff Sharkey83b559c2017-09-12 16:30:52 -0600170#define ACQUIRE_LOCK \
171 std::lock_guard<std::mutex> lock(VolumeManager::Instance()->getLock());
172
173#define ACQUIRE_CRYPT_LOCK \
174 std::lock_guard<std::mutex> lock(VolumeManager::Instance()->getCryptLock());
Jeff Sharkey9462bdd2017-09-07 15:27:28 -0600175
Jeff Sharkey068c6be2017-09-06 13:47:40 -0600176} // namespace
177
178status_t VoldNativeService::start() {
179 IPCThreadState::self()->disableBackgroundScheduling(true);
180 status_t ret = BinderService<VoldNativeService>::publish();
181 if (ret != android::OK) {
182 return ret;
183 }
184 sp<ProcessState> ps(ProcessState::self());
185 ps->startThreadPool();
186 ps->giveThreadPoolName();
187 return android::OK;
188}
189
190status_t VoldNativeService::dump(int fd, const Vector<String16> & /* args */) {
191 auto out = std::fstream(StringPrintf("/proc/self/fd/%d", fd));
192 const binder::Status dump_permission = checkPermission(kDump);
193 if (!dump_permission.isOk()) {
194 out << dump_permission.toString8() << endl;
195 return PERMISSION_DENIED;
196 }
197
Jeff Sharkey9462bdd2017-09-07 15:27:28 -0600198 ACQUIRE_LOCK;
Jeff Sharkey068c6be2017-09-06 13:47:40 -0600199 out << "vold is happy!" << endl;
200 out.flush();
Jeff Sharkey068c6be2017-09-06 13:47:40 -0600201 return NO_ERROR;
202}
203
Jeff Sharkey814e9d32017-09-13 11:49:44 -0600204binder::Status VoldNativeService::setListener(
205 const android::sp<android::os::IVoldListener>& listener) {
206 ENFORCE_UID(AID_SYSTEM);
207 ACQUIRE_LOCK;
208
209 VolumeManager::Instance()->setListener(listener);
210 return ok();
211}
212
Jeff Sharkey068c6be2017-09-06 13:47:40 -0600213binder::Status VoldNativeService::reset() {
214 ENFORCE_UID(AID_SYSTEM);
Jeff Sharkey9462bdd2017-09-07 15:27:28 -0600215 ACQUIRE_LOCK;
Jeff Sharkey068c6be2017-09-06 13:47:40 -0600216
Jeff Sharkey9462bdd2017-09-07 15:27:28 -0600217 return translate(VolumeManager::Instance()->reset());
218}
219
220binder::Status VoldNativeService::shutdown() {
221 ENFORCE_UID(AID_SYSTEM);
222 ACQUIRE_LOCK;
223
224 return translate(VolumeManager::Instance()->shutdown());
225}
226
Jeff Sharkey11c2d382017-09-11 10:32:01 -0600227binder::Status VoldNativeService::mountAll() {
Jeff Sharkey9462bdd2017-09-07 15:27:28 -0600228 ENFORCE_UID(AID_SYSTEM);
229 ACQUIRE_LOCK;
230
Jeff Sharkey11c2d382017-09-11 10:32:01 -0600231 struct fstab* fstab = fs_mgr_read_fstab_default();
232 int res = fs_mgr_mount_all(fstab, MOUNT_MODE_DEFAULT);
233 fs_mgr_free_fstab(fstab);
234 return translate(res);
Jeff Sharkey9462bdd2017-09-07 15:27:28 -0600235}
236
237binder::Status VoldNativeService::onUserAdded(int32_t userId, int32_t userSerial) {
238 ENFORCE_UID(AID_SYSTEM);
239 ACQUIRE_LOCK;
240
241 return translate(VolumeManager::Instance()->onUserAdded(userId, userSerial));
242}
243
244binder::Status VoldNativeService::onUserRemoved(int32_t userId) {
245 ENFORCE_UID(AID_SYSTEM);
246 ACQUIRE_LOCK;
247
248 return translate(VolumeManager::Instance()->onUserRemoved(userId));
249}
250
251binder::Status VoldNativeService::onUserStarted(int32_t userId) {
252 ENFORCE_UID(AID_SYSTEM);
253 ACQUIRE_LOCK;
254
255 return translate(VolumeManager::Instance()->onUserStarted(userId));
256}
257
258binder::Status VoldNativeService::onUserStopped(int32_t userId) {
259 ENFORCE_UID(AID_SYSTEM);
260 ACQUIRE_LOCK;
261
262 return translate(VolumeManager::Instance()->onUserStopped(userId));
263}
264
Jeff Sharkey11c2d382017-09-11 10:32:01 -0600265binder::Status VoldNativeService::partition(const std::string& diskId, int32_t partitionType,
266 int32_t ratio) {
Jeff Sharkey9462bdd2017-09-07 15:27:28 -0600267 ENFORCE_UID(AID_SYSTEM);
Jeff Sharkeyec4fda22017-09-12 13:19:24 -0600268 CHECK_ARGUMENT_ID(diskId);
Jeff Sharkey9462bdd2017-09-07 15:27:28 -0600269 ACQUIRE_LOCK;
270
271 auto disk = VolumeManager::Instance()->findDisk(diskId);
272 if (disk == nullptr) {
273 return error("Failed to find disk " + diskId);
274 }
275 switch (partitionType) {
276 case PARTITION_TYPE_PUBLIC: return translate(disk->partitionPublic());
277 case PARTITION_TYPE_PRIVATE: return translate(disk->partitionPrivate());
278 case PARTITION_TYPE_MIXED: return translate(disk->partitionMixed(ratio));
279 default: return error("Unknown type " + std::to_string(partitionType));
280 }
281}
282
283binder::Status VoldNativeService::forgetPartition(const std::string& partGuid) {
284 ENFORCE_UID(AID_SYSTEM);
Jeff Sharkeyec4fda22017-09-12 13:19:24 -0600285 CHECK_ARGUMENT_HEX(partGuid);
Jeff Sharkey9462bdd2017-09-07 15:27:28 -0600286 ACQUIRE_LOCK;
287
288 return translate(VolumeManager::Instance()->forgetPartition(partGuid));
289}
290
Jeff Sharkey11c2d382017-09-11 10:32:01 -0600291binder::Status VoldNativeService::mount(const std::string& volId, int32_t mountFlags,
292 int32_t mountUserId) {
Jeff Sharkey9462bdd2017-09-07 15:27:28 -0600293 ENFORCE_UID(AID_SYSTEM);
Jeff Sharkeyec4fda22017-09-12 13:19:24 -0600294 CHECK_ARGUMENT_ID(volId);
Jeff Sharkey9462bdd2017-09-07 15:27:28 -0600295 ACQUIRE_LOCK;
296
297 auto vol = VolumeManager::Instance()->findVolume(volId);
298 if (vol == nullptr) {
299 return error("Failed to find volume " + volId);
300 }
301
302 vol->setMountFlags(mountFlags);
303 vol->setMountUserId(mountUserId);
304
305 int res = vol->mount();
Jeff Sharkey83b559c2017-09-12 16:30:52 -0600306 if ((mountFlags & MOUNT_FLAG_PRIMARY) != 0) {
Jeff Sharkey9462bdd2017-09-07 15:27:28 -0600307 VolumeManager::Instance()->setPrimary(vol);
308 }
309 return translate(res);
310}
311
312binder::Status VoldNativeService::unmount(const std::string& volId) {
313 ENFORCE_UID(AID_SYSTEM);
Jeff Sharkeyec4fda22017-09-12 13:19:24 -0600314 CHECK_ARGUMENT_ID(volId);
Jeff Sharkey9462bdd2017-09-07 15:27:28 -0600315 ACQUIRE_LOCK;
316
317 auto vol = VolumeManager::Instance()->findVolume(volId);
318 if (vol == nullptr) {
319 return error("Failed to find volume " + volId);
320 }
321 return translate(vol->unmount());
322}
323
324binder::Status VoldNativeService::format(const std::string& volId, const std::string& fsType) {
325 ENFORCE_UID(AID_SYSTEM);
Jeff Sharkeyec4fda22017-09-12 13:19:24 -0600326 CHECK_ARGUMENT_ID(volId);
Jeff Sharkey9462bdd2017-09-07 15:27:28 -0600327 ACQUIRE_LOCK;
328
329 auto vol = VolumeManager::Instance()->findVolume(volId);
330 if (vol == nullptr) {
331 return error("Failed to find volume " + volId);
332 }
333 return translate(vol->format(fsType));
334}
335
336binder::Status VoldNativeService::benchmark(const std::string& volId, int64_t* _aidl_return) {
337 ENFORCE_UID(AID_SYSTEM);
Jeff Sharkeyec4fda22017-09-12 13:19:24 -0600338 CHECK_ARGUMENT_ID(volId);
Jeff Sharkey9462bdd2017-09-07 15:27:28 -0600339 ACQUIRE_LOCK;
340
341 *_aidl_return = VolumeManager::Instance()->benchmarkPrivate(volId);
Jeff Sharkey068c6be2017-09-06 13:47:40 -0600342 return ok();
343}
344
Jeff Sharkey11c2d382017-09-11 10:32:01 -0600345binder::Status VoldNativeService::moveStorage(const std::string& fromVolId,
346 const std::string& toVolId) {
Jeff Sharkey9462bdd2017-09-07 15:27:28 -0600347 ENFORCE_UID(AID_SYSTEM);
Jeff Sharkeyec4fda22017-09-12 13:19:24 -0600348 CHECK_ARGUMENT_ID(fromVolId);
349 CHECK_ARGUMENT_ID(toVolId);
Jeff Sharkey9462bdd2017-09-07 15:27:28 -0600350 ACQUIRE_LOCK;
351
352 auto fromVol = VolumeManager::Instance()->findVolume(fromVolId);
353 auto toVol = VolumeManager::Instance()->findVolume(toVolId);
354 if (fromVol == nullptr) {
355 return error("Failed to find volume " + fromVolId);
356 } else if (toVol == nullptr) {
357 return error("Failed to find volume " + toVolId);
358 }
359 (new android::vold::MoveTask(fromVol, toVol))->start();
360 return ok();
361}
362
363binder::Status VoldNativeService::remountUid(int32_t uid, int32_t remountMode) {
364 ENFORCE_UID(AID_SYSTEM);
365 ACQUIRE_LOCK;
366
367 std::string tmp;
368 switch (remountMode) {
369 case REMOUNT_MODE_NONE: tmp = "none"; break;
370 case REMOUNT_MODE_DEFAULT: tmp = "default"; break;
371 case REMOUNT_MODE_READ: tmp = "read"; break;
372 case REMOUNT_MODE_WRITE: tmp = "write"; break;
373 default: return error("Unknown mode " + std::to_string(remountMode));
374 }
375 return translate(VolumeManager::Instance()->remountUid(uid, tmp));
376}
377
378binder::Status VoldNativeService::mkdirs(const std::string& path) {
379 ENFORCE_UID(AID_SYSTEM);
Jeff Sharkeyec4fda22017-09-12 13:19:24 -0600380 CHECK_ARGUMENT_PATH(path);
Jeff Sharkey9462bdd2017-09-07 15:27:28 -0600381 ACQUIRE_LOCK;
382
383 return translate(VolumeManager::Instance()->mkdirs(path.c_str()));
384}
385
Jeff Sharkey11c2d382017-09-11 10:32:01 -0600386binder::Status VoldNativeService::createObb(const std::string& sourcePath,
387 const std::string& sourceKey, int32_t ownerGid, std::string* _aidl_return) {
388 ENFORCE_UID(AID_SYSTEM);
Jeff Sharkeyec4fda22017-09-12 13:19:24 -0600389 CHECK_ARGUMENT_PATH(sourcePath);
390 CHECK_ARGUMENT_HEX(sourceKey);
Jeff Sharkey11c2d382017-09-11 10:32:01 -0600391 ACQUIRE_LOCK;
392
393 return translate(
394 VolumeManager::Instance()->createObb(sourcePath, sourceKey, ownerGid, _aidl_return));
395}
396
397binder::Status VoldNativeService::destroyObb(const std::string& volId) {
398 ENFORCE_UID(AID_SYSTEM);
Jeff Sharkeyec4fda22017-09-12 13:19:24 -0600399 CHECK_ARGUMENT_ID(volId);
Jeff Sharkey11c2d382017-09-11 10:32:01 -0600400 ACQUIRE_LOCK;
401
402 return translate(VolumeManager::Instance()->destroyObb(volId));
403}
404
405binder::Status VoldNativeService::fstrim(int32_t fstrimFlags) {
406 ENFORCE_UID(AID_SYSTEM);
407 ACQUIRE_LOCK;
408
409 (new android::vold::TrimTask(fstrimFlags))->start();
410 return ok();
411}
412
413binder::Status VoldNativeService::mountAppFuse(int32_t uid, int32_t pid, int32_t mountId,
414 android::base::unique_fd* _aidl_return) {
415 ENFORCE_UID(AID_SYSTEM);
416 ACQUIRE_LOCK;
417
418 return translate(VolumeManager::Instance()->mountAppFuse(uid, pid, mountId, _aidl_return));
419}
420
421binder::Status VoldNativeService::unmountAppFuse(int32_t uid, int32_t pid, int32_t mountId) {
422 ENFORCE_UID(AID_SYSTEM);
423 ACQUIRE_LOCK;
424
425 return translate(VolumeManager::Instance()->unmountAppFuse(uid, pid, mountId));
426}
427
Jeff Sharkey83b559c2017-09-12 16:30:52 -0600428binder::Status VoldNativeService::fdeCheckPassword(const std::string& password) {
429 ENFORCE_UID(AID_SYSTEM);
430 ACQUIRE_CRYPT_LOCK;
431
432 return translate(cryptfs_check_passwd(password.c_str()));
433}
434
435binder::Status VoldNativeService::fdeRestart() {
436 ENFORCE_UID(AID_SYSTEM);
437 ACQUIRE_CRYPT_LOCK;
438
439 // Spawn as thread so init can issue commands back to vold without
440 // causing deadlock, usually as a result of prep_data_fs.
441 std::thread(&cryptfs_restart).detach();
442 return ok();
443}
444
445binder::Status VoldNativeService::fdeComplete(int32_t* _aidl_return) {
446 ENFORCE_UID(AID_SYSTEM);
447 ACQUIRE_CRYPT_LOCK;
448
449 *_aidl_return = cryptfs_crypto_complete();
450 return ok();
451}
452
453static int fdeEnableInternal(int32_t passwordType, const std::string& password,
454 int32_t encryptionFlags) {
455 bool noUi = (encryptionFlags & VoldNativeService::ENCRYPTION_FLAG_NO_UI) != 0;
456
457 std::string how;
458 if ((encryptionFlags & VoldNativeService::ENCRYPTION_FLAG_IN_PLACE) != 0) {
459 how = "inplace";
460 } else if ((encryptionFlags & VoldNativeService::ENCRYPTION_FLAG_WIPE) != 0) {
461 how = "wipe";
462 } else {
463 LOG(ERROR) << "Missing encryption flag";
464 return -1;
465 }
466
467 for (int tries = 0; tries < 2; ++tries) {
468 int rc;
469 if (passwordType == VoldNativeService::PASSWORD_TYPE_DEFAULT) {
470 rc = cryptfs_enable_default(how.c_str(), noUi);
471 } else {
472 rc = cryptfs_enable(how.c_str(), passwordType, password.c_str(), noUi);
473 }
474
475 if (rc == 0) {
476 return 0;
477 } else if (tries == 0) {
478 Process::killProcessesWithOpenFiles(DATA_MNT_POINT, SIGKILL);
479 }
480 }
481
482 return -1;
483}
484
485binder::Status VoldNativeService::fdeEnable(int32_t passwordType,
486 const std::string& password, int32_t encryptionFlags) {
487 ENFORCE_UID(AID_SYSTEM);
488 ACQUIRE_CRYPT_LOCK;
489
490 if (e4crypt_is_native()) {
491 if (passwordType != PASSWORD_TYPE_DEFAULT) {
492 return error("Unexpected password type");
493 }
494 if (encryptionFlags != (ENCRYPTION_FLAG_IN_PLACE | ENCRYPTION_FLAG_NO_UI)) {
495 return error("Unexpected flags");
496 }
497 return translateBool(e4crypt_enable_crypto());
498 }
499
500 // Spawn as thread so init can issue commands back to vold without
501 // causing deadlock, usually as a result of prep_data_fs.
502 std::thread(&fdeEnableInternal, passwordType, password, encryptionFlags).detach();
503 return ok();
504}
505
506binder::Status VoldNativeService::fdeChangePassword(int32_t passwordType,
507 const std::string& password) {
508 ENFORCE_UID(AID_SYSTEM);
509 ACQUIRE_CRYPT_LOCK;
510
511 return translate(cryptfs_changepw(passwordType, password.c_str()));
512}
513
514binder::Status VoldNativeService::fdeVerifyPassword(const std::string& password) {
515 ENFORCE_UID(AID_SYSTEM);
516 ACQUIRE_CRYPT_LOCK;
517
518 return translate(cryptfs_verify_passwd(password.c_str()));
519}
520
521binder::Status VoldNativeService::fdeGetField(const std::string& key,
522 std::string* _aidl_return) {
523 ENFORCE_UID(AID_SYSTEM);
524 ACQUIRE_CRYPT_LOCK;
525
526 char buf[PROPERTY_VALUE_MAX];
527 if (cryptfs_getfield(key.c_str(), buf, sizeof(buf)) != CRYPTO_GETFIELD_OK) {
528 return error(StringPrintf("Failed to read field %s", key.c_str()));
529 } else {
530 *_aidl_return = buf;
531 return ok();
532 }
533}
534
535binder::Status VoldNativeService::fdeSetField(const std::string& key,
536 const std::string& value) {
537 ENFORCE_UID(AID_SYSTEM);
538 ACQUIRE_CRYPT_LOCK;
539
540 return translate(cryptfs_setfield(key.c_str(), value.c_str()));
541}
542
543binder::Status VoldNativeService::fdeGetPasswordType(int32_t* _aidl_return) {
544 ENFORCE_UID(AID_SYSTEM);
545 ACQUIRE_CRYPT_LOCK;
546
547 *_aidl_return = cryptfs_get_password_type();
548 return ok();
549}
550
551binder::Status VoldNativeService::fdeGetPassword(std::string* _aidl_return) {
552 ENFORCE_UID(AID_SYSTEM);
553 ACQUIRE_CRYPT_LOCK;
554
555 const char* res = cryptfs_get_password();
556 if (res != nullptr) {
557 *_aidl_return = res;
558 }
559 return ok();
560}
561
562binder::Status VoldNativeService::fdeClearPassword() {
563 ENFORCE_UID(AID_SYSTEM);
564 ACQUIRE_CRYPT_LOCK;
565
566 cryptfs_clear_password();
567 return ok();
568}
569
570binder::Status VoldNativeService::fbeEnable() {
571 ENFORCE_UID(AID_SYSTEM);
572 ACQUIRE_CRYPT_LOCK;
573
574 return translateBool(e4crypt_initialize_global_de());
575}
576
577binder::Status VoldNativeService::mountDefaultEncrypted() {
578 ENFORCE_UID(AID_SYSTEM);
579 ACQUIRE_CRYPT_LOCK;
580
581 if (e4crypt_is_native()) {
582 return translateBool(e4crypt_mount_metadata_encrypted());
583 } else {
584 // Spawn as thread so init can issue commands back to vold without
585 // causing deadlock, usually as a result of prep_data_fs.
586 std::thread(&cryptfs_mount_default_encrypted).detach();
587 return ok();
588 }
589}
590
591binder::Status VoldNativeService::initUser0() {
592 ENFORCE_UID(AID_SYSTEM);
593 ACQUIRE_CRYPT_LOCK;
594
595 return translateBool(e4crypt_init_user0());
596}
597
598binder::Status VoldNativeService::isConvertibleToFbe(bool* _aidl_return) {
599 ENFORCE_UID(AID_SYSTEM);
600 ACQUIRE_CRYPT_LOCK;
601
602 *_aidl_return = cryptfs_isConvertibleToFBE() != 0;
603 return ok();
604}
605
606binder::Status VoldNativeService::createUserKey(int32_t userId, int32_t userSerial,
607 bool ephemeral) {
608 ENFORCE_UID(AID_SYSTEM);
609 ACQUIRE_CRYPT_LOCK;
610
611 return translateBool(e4crypt_vold_create_user_key(userId, userSerial, ephemeral));
612}
613
614binder::Status VoldNativeService::destroyUserKey(int32_t userId) {
615 ENFORCE_UID(AID_SYSTEM);
616 ACQUIRE_CRYPT_LOCK;
617
618 return translateBool(e4crypt_destroy_user_key(userId));
619}
620
621binder::Status VoldNativeService::addUserKeyAuth(int32_t userId, int32_t userSerial,
622 const std::string& token, const std::string& secret) {
623 ENFORCE_UID(AID_SYSTEM);
624 ACQUIRE_CRYPT_LOCK;
625
626 return translateBool(e4crypt_add_user_key_auth(userId, userSerial, token.c_str(), secret.c_str()));
627}
628
629binder::Status VoldNativeService::fixateNewestUserKeyAuth(int32_t userId) {
630 ENFORCE_UID(AID_SYSTEM);
631 ACQUIRE_CRYPT_LOCK;
632
633 return translateBool(e4crypt_fixate_newest_user_key_auth(userId));
634}
635
636binder::Status VoldNativeService::unlockUserKey(int32_t userId, int32_t userSerial,
637 const std::string& token, const std::string& secret) {
638 ENFORCE_UID(AID_SYSTEM);
639 ACQUIRE_CRYPT_LOCK;
640
641 return translateBool(e4crypt_unlock_user_key(userId, userSerial, token.c_str(), secret.c_str()));
642}
643
644binder::Status VoldNativeService::lockUserKey(int32_t userId) {
645 ENFORCE_UID(AID_SYSTEM);
646 ACQUIRE_CRYPT_LOCK;
647
648 return translateBool(e4crypt_lock_user_key(userId));
649}
650
651binder::Status VoldNativeService::prepareUserStorage(const std::unique_ptr<std::string>& uuid,
652 int32_t userId, int32_t userSerial, int32_t flags) {
653 ENFORCE_UID(AID_SYSTEM);
654 ACQUIRE_CRYPT_LOCK;
655
656 const char* uuid_ = uuid ? uuid->c_str() : nullptr;
657 return translateBool(e4crypt_prepare_user_storage(uuid_, userId, userSerial, flags));
658}
659
660binder::Status VoldNativeService::destroyUserStorage(const std::unique_ptr<std::string>& uuid,
661 int32_t userId, int32_t flags) {
662 ENFORCE_UID(AID_SYSTEM);
663 ACQUIRE_CRYPT_LOCK;
664
665 const char* uuid_ = uuid ? uuid->c_str() : nullptr;
666 return translateBool(e4crypt_destroy_user_storage(uuid_, userId, flags));
667}
668
669binder::Status VoldNativeService::secdiscard(const std::string& path) {
670 ENFORCE_UID(AID_SYSTEM);
671 ACQUIRE_CRYPT_LOCK;
672
673 return translateBool(e4crypt_secdiscard(path.c_str()));
674}
675
Jeff Sharkey068c6be2017-09-06 13:47:40 -0600676} // namespace vold
677} // namespace android