blob: 374cb4c2dfd0788047b9fb1e54eba134b63938b3 [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
204binder::Status VoldNativeService::reset() {
205 ENFORCE_UID(AID_SYSTEM);
Jeff Sharkey9462bdd2017-09-07 15:27:28 -0600206 ACQUIRE_LOCK;
Jeff Sharkey068c6be2017-09-06 13:47:40 -0600207
Jeff Sharkey9462bdd2017-09-07 15:27:28 -0600208 return translate(VolumeManager::Instance()->reset());
209}
210
211binder::Status VoldNativeService::shutdown() {
212 ENFORCE_UID(AID_SYSTEM);
213 ACQUIRE_LOCK;
214
215 return translate(VolumeManager::Instance()->shutdown());
216}
217
Jeff Sharkey11c2d382017-09-11 10:32:01 -0600218binder::Status VoldNativeService::mountAll() {
Jeff Sharkey9462bdd2017-09-07 15:27:28 -0600219 ENFORCE_UID(AID_SYSTEM);
220 ACQUIRE_LOCK;
221
Jeff Sharkey11c2d382017-09-11 10:32:01 -0600222 struct fstab* fstab = fs_mgr_read_fstab_default();
223 int res = fs_mgr_mount_all(fstab, MOUNT_MODE_DEFAULT);
224 fs_mgr_free_fstab(fstab);
225 return translate(res);
Jeff Sharkey9462bdd2017-09-07 15:27:28 -0600226}
227
228binder::Status VoldNativeService::onUserAdded(int32_t userId, int32_t userSerial) {
229 ENFORCE_UID(AID_SYSTEM);
230 ACQUIRE_LOCK;
231
232 return translate(VolumeManager::Instance()->onUserAdded(userId, userSerial));
233}
234
235binder::Status VoldNativeService::onUserRemoved(int32_t userId) {
236 ENFORCE_UID(AID_SYSTEM);
237 ACQUIRE_LOCK;
238
239 return translate(VolumeManager::Instance()->onUserRemoved(userId));
240}
241
242binder::Status VoldNativeService::onUserStarted(int32_t userId) {
243 ENFORCE_UID(AID_SYSTEM);
244 ACQUIRE_LOCK;
245
246 return translate(VolumeManager::Instance()->onUserStarted(userId));
247}
248
249binder::Status VoldNativeService::onUserStopped(int32_t userId) {
250 ENFORCE_UID(AID_SYSTEM);
251 ACQUIRE_LOCK;
252
253 return translate(VolumeManager::Instance()->onUserStopped(userId));
254}
255
Jeff Sharkey11c2d382017-09-11 10:32:01 -0600256binder::Status VoldNativeService::partition(const std::string& diskId, int32_t partitionType,
257 int32_t ratio) {
Jeff Sharkey9462bdd2017-09-07 15:27:28 -0600258 ENFORCE_UID(AID_SYSTEM);
Jeff Sharkeyec4fda22017-09-12 13:19:24 -0600259 CHECK_ARGUMENT_ID(diskId);
Jeff Sharkey9462bdd2017-09-07 15:27:28 -0600260 ACQUIRE_LOCK;
261
262 auto disk = VolumeManager::Instance()->findDisk(diskId);
263 if (disk == nullptr) {
264 return error("Failed to find disk " + diskId);
265 }
266 switch (partitionType) {
267 case PARTITION_TYPE_PUBLIC: return translate(disk->partitionPublic());
268 case PARTITION_TYPE_PRIVATE: return translate(disk->partitionPrivate());
269 case PARTITION_TYPE_MIXED: return translate(disk->partitionMixed(ratio));
270 default: return error("Unknown type " + std::to_string(partitionType));
271 }
272}
273
274binder::Status VoldNativeService::forgetPartition(const std::string& partGuid) {
275 ENFORCE_UID(AID_SYSTEM);
Jeff Sharkeyec4fda22017-09-12 13:19:24 -0600276 CHECK_ARGUMENT_HEX(partGuid);
Jeff Sharkey9462bdd2017-09-07 15:27:28 -0600277 ACQUIRE_LOCK;
278
279 return translate(VolumeManager::Instance()->forgetPartition(partGuid));
280}
281
Jeff Sharkey11c2d382017-09-11 10:32:01 -0600282binder::Status VoldNativeService::mount(const std::string& volId, int32_t mountFlags,
283 int32_t mountUserId) {
Jeff Sharkey9462bdd2017-09-07 15:27:28 -0600284 ENFORCE_UID(AID_SYSTEM);
Jeff Sharkeyec4fda22017-09-12 13:19:24 -0600285 CHECK_ARGUMENT_ID(volId);
Jeff Sharkey9462bdd2017-09-07 15:27:28 -0600286 ACQUIRE_LOCK;
287
288 auto vol = VolumeManager::Instance()->findVolume(volId);
289 if (vol == nullptr) {
290 return error("Failed to find volume " + volId);
291 }
292
293 vol->setMountFlags(mountFlags);
294 vol->setMountUserId(mountUserId);
295
296 int res = vol->mount();
Jeff Sharkey83b559c2017-09-12 16:30:52 -0600297 if ((mountFlags & MOUNT_FLAG_PRIMARY) != 0) {
Jeff Sharkey9462bdd2017-09-07 15:27:28 -0600298 VolumeManager::Instance()->setPrimary(vol);
299 }
300 return translate(res);
301}
302
303binder::Status VoldNativeService::unmount(const std::string& volId) {
304 ENFORCE_UID(AID_SYSTEM);
Jeff Sharkeyec4fda22017-09-12 13:19:24 -0600305 CHECK_ARGUMENT_ID(volId);
Jeff Sharkey9462bdd2017-09-07 15:27:28 -0600306 ACQUIRE_LOCK;
307
308 auto vol = VolumeManager::Instance()->findVolume(volId);
309 if (vol == nullptr) {
310 return error("Failed to find volume " + volId);
311 }
312 return translate(vol->unmount());
313}
314
315binder::Status VoldNativeService::format(const std::string& volId, const std::string& fsType) {
316 ENFORCE_UID(AID_SYSTEM);
Jeff Sharkeyec4fda22017-09-12 13:19:24 -0600317 CHECK_ARGUMENT_ID(volId);
Jeff Sharkey9462bdd2017-09-07 15:27:28 -0600318 ACQUIRE_LOCK;
319
320 auto vol = VolumeManager::Instance()->findVolume(volId);
321 if (vol == nullptr) {
322 return error("Failed to find volume " + volId);
323 }
324 return translate(vol->format(fsType));
325}
326
327binder::Status VoldNativeService::benchmark(const std::string& volId, int64_t* _aidl_return) {
328 ENFORCE_UID(AID_SYSTEM);
Jeff Sharkeyec4fda22017-09-12 13:19:24 -0600329 CHECK_ARGUMENT_ID(volId);
Jeff Sharkey9462bdd2017-09-07 15:27:28 -0600330 ACQUIRE_LOCK;
331
332 *_aidl_return = VolumeManager::Instance()->benchmarkPrivate(volId);
Jeff Sharkey068c6be2017-09-06 13:47:40 -0600333 return ok();
334}
335
Jeff Sharkey11c2d382017-09-11 10:32:01 -0600336binder::Status VoldNativeService::moveStorage(const std::string& fromVolId,
337 const std::string& toVolId) {
Jeff Sharkey9462bdd2017-09-07 15:27:28 -0600338 ENFORCE_UID(AID_SYSTEM);
Jeff Sharkeyec4fda22017-09-12 13:19:24 -0600339 CHECK_ARGUMENT_ID(fromVolId);
340 CHECK_ARGUMENT_ID(toVolId);
Jeff Sharkey9462bdd2017-09-07 15:27:28 -0600341 ACQUIRE_LOCK;
342
343 auto fromVol = VolumeManager::Instance()->findVolume(fromVolId);
344 auto toVol = VolumeManager::Instance()->findVolume(toVolId);
345 if (fromVol == nullptr) {
346 return error("Failed to find volume " + fromVolId);
347 } else if (toVol == nullptr) {
348 return error("Failed to find volume " + toVolId);
349 }
350 (new android::vold::MoveTask(fromVol, toVol))->start();
351 return ok();
352}
353
354binder::Status VoldNativeService::remountUid(int32_t uid, int32_t remountMode) {
355 ENFORCE_UID(AID_SYSTEM);
356 ACQUIRE_LOCK;
357
358 std::string tmp;
359 switch (remountMode) {
360 case REMOUNT_MODE_NONE: tmp = "none"; break;
361 case REMOUNT_MODE_DEFAULT: tmp = "default"; break;
362 case REMOUNT_MODE_READ: tmp = "read"; break;
363 case REMOUNT_MODE_WRITE: tmp = "write"; break;
364 default: return error("Unknown mode " + std::to_string(remountMode));
365 }
366 return translate(VolumeManager::Instance()->remountUid(uid, tmp));
367}
368
369binder::Status VoldNativeService::mkdirs(const std::string& path) {
370 ENFORCE_UID(AID_SYSTEM);
Jeff Sharkeyec4fda22017-09-12 13:19:24 -0600371 CHECK_ARGUMENT_PATH(path);
Jeff Sharkey9462bdd2017-09-07 15:27:28 -0600372 ACQUIRE_LOCK;
373
374 return translate(VolumeManager::Instance()->mkdirs(path.c_str()));
375}
376
Jeff Sharkey11c2d382017-09-11 10:32:01 -0600377binder::Status VoldNativeService::createObb(const std::string& sourcePath,
378 const std::string& sourceKey, int32_t ownerGid, std::string* _aidl_return) {
379 ENFORCE_UID(AID_SYSTEM);
Jeff Sharkeyec4fda22017-09-12 13:19:24 -0600380 CHECK_ARGUMENT_PATH(sourcePath);
381 CHECK_ARGUMENT_HEX(sourceKey);
Jeff Sharkey11c2d382017-09-11 10:32:01 -0600382 ACQUIRE_LOCK;
383
384 return translate(
385 VolumeManager::Instance()->createObb(sourcePath, sourceKey, ownerGid, _aidl_return));
386}
387
388binder::Status VoldNativeService::destroyObb(const std::string& volId) {
389 ENFORCE_UID(AID_SYSTEM);
Jeff Sharkeyec4fda22017-09-12 13:19:24 -0600390 CHECK_ARGUMENT_ID(volId);
Jeff Sharkey11c2d382017-09-11 10:32:01 -0600391 ACQUIRE_LOCK;
392
393 return translate(VolumeManager::Instance()->destroyObb(volId));
394}
395
396binder::Status VoldNativeService::fstrim(int32_t fstrimFlags) {
397 ENFORCE_UID(AID_SYSTEM);
398 ACQUIRE_LOCK;
399
400 (new android::vold::TrimTask(fstrimFlags))->start();
401 return ok();
402}
403
404binder::Status VoldNativeService::mountAppFuse(int32_t uid, int32_t pid, int32_t mountId,
405 android::base::unique_fd* _aidl_return) {
406 ENFORCE_UID(AID_SYSTEM);
407 ACQUIRE_LOCK;
408
409 return translate(VolumeManager::Instance()->mountAppFuse(uid, pid, mountId, _aidl_return));
410}
411
412binder::Status VoldNativeService::unmountAppFuse(int32_t uid, int32_t pid, int32_t mountId) {
413 ENFORCE_UID(AID_SYSTEM);
414 ACQUIRE_LOCK;
415
416 return translate(VolumeManager::Instance()->unmountAppFuse(uid, pid, mountId));
417}
418
Jeff Sharkey83b559c2017-09-12 16:30:52 -0600419binder::Status VoldNativeService::fdeCheckPassword(const std::string& password) {
420 ENFORCE_UID(AID_SYSTEM);
421 ACQUIRE_CRYPT_LOCK;
422
423 return translate(cryptfs_check_passwd(password.c_str()));
424}
425
426binder::Status VoldNativeService::fdeRestart() {
427 ENFORCE_UID(AID_SYSTEM);
428 ACQUIRE_CRYPT_LOCK;
429
430 // Spawn as thread so init can issue commands back to vold without
431 // causing deadlock, usually as a result of prep_data_fs.
432 std::thread(&cryptfs_restart).detach();
433 return ok();
434}
435
436binder::Status VoldNativeService::fdeComplete(int32_t* _aidl_return) {
437 ENFORCE_UID(AID_SYSTEM);
438 ACQUIRE_CRYPT_LOCK;
439
440 *_aidl_return = cryptfs_crypto_complete();
441 return ok();
442}
443
444static int fdeEnableInternal(int32_t passwordType, const std::string& password,
445 int32_t encryptionFlags) {
446 bool noUi = (encryptionFlags & VoldNativeService::ENCRYPTION_FLAG_NO_UI) != 0;
447
448 std::string how;
449 if ((encryptionFlags & VoldNativeService::ENCRYPTION_FLAG_IN_PLACE) != 0) {
450 how = "inplace";
451 } else if ((encryptionFlags & VoldNativeService::ENCRYPTION_FLAG_WIPE) != 0) {
452 how = "wipe";
453 } else {
454 LOG(ERROR) << "Missing encryption flag";
455 return -1;
456 }
457
458 for (int tries = 0; tries < 2; ++tries) {
459 int rc;
460 if (passwordType == VoldNativeService::PASSWORD_TYPE_DEFAULT) {
461 rc = cryptfs_enable_default(how.c_str(), noUi);
462 } else {
463 rc = cryptfs_enable(how.c_str(), passwordType, password.c_str(), noUi);
464 }
465
466 if (rc == 0) {
467 return 0;
468 } else if (tries == 0) {
469 Process::killProcessesWithOpenFiles(DATA_MNT_POINT, SIGKILL);
470 }
471 }
472
473 return -1;
474}
475
476binder::Status VoldNativeService::fdeEnable(int32_t passwordType,
477 const std::string& password, int32_t encryptionFlags) {
478 ENFORCE_UID(AID_SYSTEM);
479 ACQUIRE_CRYPT_LOCK;
480
481 if (e4crypt_is_native()) {
482 if (passwordType != PASSWORD_TYPE_DEFAULT) {
483 return error("Unexpected password type");
484 }
485 if (encryptionFlags != (ENCRYPTION_FLAG_IN_PLACE | ENCRYPTION_FLAG_NO_UI)) {
486 return error("Unexpected flags");
487 }
488 return translateBool(e4crypt_enable_crypto());
489 }
490
491 // Spawn as thread so init can issue commands back to vold without
492 // causing deadlock, usually as a result of prep_data_fs.
493 std::thread(&fdeEnableInternal, passwordType, password, encryptionFlags).detach();
494 return ok();
495}
496
497binder::Status VoldNativeService::fdeChangePassword(int32_t passwordType,
498 const std::string& password) {
499 ENFORCE_UID(AID_SYSTEM);
500 ACQUIRE_CRYPT_LOCK;
501
502 return translate(cryptfs_changepw(passwordType, password.c_str()));
503}
504
505binder::Status VoldNativeService::fdeVerifyPassword(const std::string& password) {
506 ENFORCE_UID(AID_SYSTEM);
507 ACQUIRE_CRYPT_LOCK;
508
509 return translate(cryptfs_verify_passwd(password.c_str()));
510}
511
512binder::Status VoldNativeService::fdeGetField(const std::string& key,
513 std::string* _aidl_return) {
514 ENFORCE_UID(AID_SYSTEM);
515 ACQUIRE_CRYPT_LOCK;
516
517 char buf[PROPERTY_VALUE_MAX];
518 if (cryptfs_getfield(key.c_str(), buf, sizeof(buf)) != CRYPTO_GETFIELD_OK) {
519 return error(StringPrintf("Failed to read field %s", key.c_str()));
520 } else {
521 *_aidl_return = buf;
522 return ok();
523 }
524}
525
526binder::Status VoldNativeService::fdeSetField(const std::string& key,
527 const std::string& value) {
528 ENFORCE_UID(AID_SYSTEM);
529 ACQUIRE_CRYPT_LOCK;
530
531 return translate(cryptfs_setfield(key.c_str(), value.c_str()));
532}
533
534binder::Status VoldNativeService::fdeGetPasswordType(int32_t* _aidl_return) {
535 ENFORCE_UID(AID_SYSTEM);
536 ACQUIRE_CRYPT_LOCK;
537
538 *_aidl_return = cryptfs_get_password_type();
539 return ok();
540}
541
542binder::Status VoldNativeService::fdeGetPassword(std::string* _aidl_return) {
543 ENFORCE_UID(AID_SYSTEM);
544 ACQUIRE_CRYPT_LOCK;
545
546 const char* res = cryptfs_get_password();
547 if (res != nullptr) {
548 *_aidl_return = res;
549 }
550 return ok();
551}
552
553binder::Status VoldNativeService::fdeClearPassword() {
554 ENFORCE_UID(AID_SYSTEM);
555 ACQUIRE_CRYPT_LOCK;
556
557 cryptfs_clear_password();
558 return ok();
559}
560
561binder::Status VoldNativeService::fbeEnable() {
562 ENFORCE_UID(AID_SYSTEM);
563 ACQUIRE_CRYPT_LOCK;
564
565 return translateBool(e4crypt_initialize_global_de());
566}
567
568binder::Status VoldNativeService::mountDefaultEncrypted() {
569 ENFORCE_UID(AID_SYSTEM);
570 ACQUIRE_CRYPT_LOCK;
571
572 if (e4crypt_is_native()) {
573 return translateBool(e4crypt_mount_metadata_encrypted());
574 } else {
575 // Spawn as thread so init can issue commands back to vold without
576 // causing deadlock, usually as a result of prep_data_fs.
577 std::thread(&cryptfs_mount_default_encrypted).detach();
578 return ok();
579 }
580}
581
582binder::Status VoldNativeService::initUser0() {
583 ENFORCE_UID(AID_SYSTEM);
584 ACQUIRE_CRYPT_LOCK;
585
586 return translateBool(e4crypt_init_user0());
587}
588
589binder::Status VoldNativeService::isConvertibleToFbe(bool* _aidl_return) {
590 ENFORCE_UID(AID_SYSTEM);
591 ACQUIRE_CRYPT_LOCK;
592
593 *_aidl_return = cryptfs_isConvertibleToFBE() != 0;
594 return ok();
595}
596
597binder::Status VoldNativeService::createUserKey(int32_t userId, int32_t userSerial,
598 bool ephemeral) {
599 ENFORCE_UID(AID_SYSTEM);
600 ACQUIRE_CRYPT_LOCK;
601
602 return translateBool(e4crypt_vold_create_user_key(userId, userSerial, ephemeral));
603}
604
605binder::Status VoldNativeService::destroyUserKey(int32_t userId) {
606 ENFORCE_UID(AID_SYSTEM);
607 ACQUIRE_CRYPT_LOCK;
608
609 return translateBool(e4crypt_destroy_user_key(userId));
610}
611
612binder::Status VoldNativeService::addUserKeyAuth(int32_t userId, int32_t userSerial,
613 const std::string& token, const std::string& secret) {
614 ENFORCE_UID(AID_SYSTEM);
615 ACQUIRE_CRYPT_LOCK;
616
617 return translateBool(e4crypt_add_user_key_auth(userId, userSerial, token.c_str(), secret.c_str()));
618}
619
620binder::Status VoldNativeService::fixateNewestUserKeyAuth(int32_t userId) {
621 ENFORCE_UID(AID_SYSTEM);
622 ACQUIRE_CRYPT_LOCK;
623
624 return translateBool(e4crypt_fixate_newest_user_key_auth(userId));
625}
626
627binder::Status VoldNativeService::unlockUserKey(int32_t userId, int32_t userSerial,
628 const std::string& token, const std::string& secret) {
629 ENFORCE_UID(AID_SYSTEM);
630 ACQUIRE_CRYPT_LOCK;
631
632 return translateBool(e4crypt_unlock_user_key(userId, userSerial, token.c_str(), secret.c_str()));
633}
634
635binder::Status VoldNativeService::lockUserKey(int32_t userId) {
636 ENFORCE_UID(AID_SYSTEM);
637 ACQUIRE_CRYPT_LOCK;
638
639 return translateBool(e4crypt_lock_user_key(userId));
640}
641
642binder::Status VoldNativeService::prepareUserStorage(const std::unique_ptr<std::string>& uuid,
643 int32_t userId, int32_t userSerial, int32_t flags) {
644 ENFORCE_UID(AID_SYSTEM);
645 ACQUIRE_CRYPT_LOCK;
646
647 const char* uuid_ = uuid ? uuid->c_str() : nullptr;
648 return translateBool(e4crypt_prepare_user_storage(uuid_, userId, userSerial, flags));
649}
650
651binder::Status VoldNativeService::destroyUserStorage(const std::unique_ptr<std::string>& uuid,
652 int32_t userId, 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_destroy_user_storage(uuid_, userId, flags));
658}
659
660binder::Status VoldNativeService::secdiscard(const std::string& path) {
661 ENFORCE_UID(AID_SYSTEM);
662 ACQUIRE_CRYPT_LOCK;
663
664 return translateBool(e4crypt_secdiscard(path.c_str()));
665}
666
Jeff Sharkey068c6be2017-09-06 13:47:40 -0600667} // namespace vold
668} // namespace android