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