blob: 005347847ef4bf108bb1716afa288908598b1944 [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 Sharkey01a0e7f2017-10-17 16:06:32 -060021#include "Benchmark.h"
22#include "MoveStorage.h"
Jeff Sharkey83b559c2017-09-12 16:30:52 -060023#include "Process.h"
Jeff Sharkey01a0e7f2017-10-17 16:06:32 -060024#include "IdleMaint.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>
Jeff Sharkey01a0e7f2017-10-17 16:06:32 -060031#include <thread>
Jeff Sharkey068c6be2017-09-06 13:47:40 -060032
33#include <android-base/logging.h>
34#include <android-base/stringprintf.h>
35#include <android-base/strings.h>
Paul Crowley3b71fc52017-10-09 10:55:21 -070036#include <ext4_utils/ext4_crypt.h>
Jeff Sharkey11c2d382017-09-11 10:32:01 -060037#include <fs_mgr.h>
Jeff Sharkey068c6be2017-09-06 13:47:40 -060038#include <private/android_filesystem_config.h>
Jeff Sharkey67b8c492017-09-21 17:08:43 -060039#include <utils/Trace.h>
Jeff Sharkey068c6be2017-09-06 13:47:40 -060040
Jeff Sharkey068c6be2017-09-06 13:47:40 -060041using android::base::StringPrintf;
42using std::endl;
43
44namespace android {
45namespace vold {
46
47namespace {
48
49constexpr const char* kDump = "android.permission.DUMP";
50
51static binder::Status ok() {
52 return binder::Status::ok();
53}
54
55static binder::Status exception(uint32_t code, const std::string& msg) {
56 return binder::Status::fromExceptionCode(code, String8(msg.c_str()));
57}
58
Jeff Sharkey9462bdd2017-09-07 15:27:28 -060059static binder::Status error(const std::string& msg) {
60 PLOG(ERROR) << msg;
61 return binder::Status::fromServiceSpecificError(errno, String8(msg.c_str()));
62}
63
Jeff Sharkey83b559c2017-09-12 16:30:52 -060064static binder::Status translate(int status) {
Jeff Sharkey9462bdd2017-09-07 15:27:28 -060065 if (status == 0) {
66 return binder::Status::ok();
67 } else {
Jeff Sharkey11c2d382017-09-11 10:32:01 -060068 return binder::Status::fromServiceSpecificError(status);
Jeff Sharkey9462bdd2017-09-07 15:27:28 -060069 }
70}
71
Jeff Sharkey83b559c2017-09-12 16:30:52 -060072static binder::Status translateBool(bool status) {
73 if (status) {
74 return binder::Status::ok();
75 } else {
76 return binder::Status::fromServiceSpecificError(status);
77 }
78}
79
Jeff Sharkey068c6be2017-09-06 13:47:40 -060080binder::Status checkPermission(const char* permission) {
81 pid_t pid;
82 uid_t uid;
83
84 if (checkCallingPermission(String16(permission), reinterpret_cast<int32_t*>(&pid),
85 reinterpret_cast<int32_t*>(&uid))) {
86 return ok();
87 } else {
88 return exception(binder::Status::EX_SECURITY,
89 StringPrintf("UID %d / PID %d lacks permission %s", uid, pid, permission));
90 }
91}
92
93binder::Status checkUid(uid_t expectedUid) {
94 uid_t uid = IPCThreadState::self()->getCallingUid();
95 if (uid == expectedUid || uid == AID_ROOT) {
96 return ok();
97 } else {
98 return exception(binder::Status::EX_SECURITY,
99 StringPrintf("UID %d is not expected UID %d", uid, expectedUid));
100 }
101}
102
Jeff Sharkeyec4fda22017-09-12 13:19:24 -0600103binder::Status checkArgumentId(const std::string& id) {
104 if (id.empty()) {
105 return exception(binder::Status::EX_ILLEGAL_ARGUMENT, "Missing ID");
106 }
107 for (const char& c : id) {
108 if (!std::isalnum(c) && c != ':' && c != ',') {
109 return exception(binder::Status::EX_ILLEGAL_ARGUMENT,
110 StringPrintf("ID %s is malformed", id.c_str()));
111 }
112 }
113 return ok();
114}
115
116binder::Status checkArgumentPath(const std::string& path) {
117 if (path.empty()) {
118 return exception(binder::Status::EX_ILLEGAL_ARGUMENT, "Missing path");
119 }
120 if (path[0] != '/') {
121 return exception(binder::Status::EX_ILLEGAL_ARGUMENT,
122 StringPrintf("Path %s is relative", path.c_str()));
123 }
Jeff Sharkey01a0e7f2017-10-17 16:06:32 -0600124 if ((path + '/').find("/../") != std::string::npos) {
125 return exception(binder::Status::EX_ILLEGAL_ARGUMENT,
126 StringPrintf("Path %s is shady", path.c_str()));
127 }
Jeff Sharkeyec4fda22017-09-12 13:19:24 -0600128 for (const char& c : path) {
129 if (c == '\0' || c == '\n') {
130 return exception(binder::Status::EX_ILLEGAL_ARGUMENT,
131 StringPrintf("Path %s is malformed", path.c_str()));
132 }
133 }
134 return ok();
135}
136
137binder::Status checkArgumentHex(const std::string& hex) {
138 // Empty hex strings are allowed
139 for (const char& c : hex) {
140 if (!std::isxdigit(c) && c != ':' && c != '-') {
141 return exception(binder::Status::EX_ILLEGAL_ARGUMENT,
142 StringPrintf("Hex %s is malformed", hex.c_str()));
143 }
144 }
145 return ok();
146}
147
Jeff Sharkey068c6be2017-09-06 13:47:40 -0600148#define ENFORCE_UID(uid) { \
149 binder::Status status = checkUid((uid)); \
150 if (!status.isOk()) { \
151 return status; \
152 } \
153}
154
Jeff Sharkeyec4fda22017-09-12 13:19:24 -0600155#define CHECK_ARGUMENT_ID(id) { \
156 binder::Status status = checkArgumentId((id)); \
157 if (!status.isOk()) { \
158 return status; \
159 } \
160}
161
162#define CHECK_ARGUMENT_PATH(path) { \
163 binder::Status status = checkArgumentPath((path)); \
164 if (!status.isOk()) { \
165 return status; \
166 } \
167}
168
169#define CHECK_ARGUMENT_HEX(hex) { \
170 binder::Status status = checkArgumentHex((hex)); \
171 if (!status.isOk()) { \
172 return status; \
173 } \
174}
175
Jeff Sharkey83b559c2017-09-12 16:30:52 -0600176#define ACQUIRE_LOCK \
Jeff Sharkey67b8c492017-09-21 17:08:43 -0600177 std::lock_guard<std::mutex> lock(VolumeManager::Instance()->getLock()); \
178 ATRACE_CALL();
Jeff Sharkey83b559c2017-09-12 16:30:52 -0600179
180#define ACQUIRE_CRYPT_LOCK \
Jeff Sharkey67b8c492017-09-21 17:08:43 -0600181 std::lock_guard<std::mutex> lock(VolumeManager::Instance()->getCryptLock()); \
182 ATRACE_CALL();
Jeff Sharkey9462bdd2017-09-07 15:27:28 -0600183
Jeff Sharkey068c6be2017-09-06 13:47:40 -0600184} // namespace
185
186status_t VoldNativeService::start() {
187 IPCThreadState::self()->disableBackgroundScheduling(true);
188 status_t ret = BinderService<VoldNativeService>::publish();
189 if (ret != android::OK) {
190 return ret;
191 }
192 sp<ProcessState> ps(ProcessState::self());
193 ps->startThreadPool();
194 ps->giveThreadPoolName();
195 return android::OK;
196}
197
198status_t VoldNativeService::dump(int fd, const Vector<String16> & /* args */) {
199 auto out = std::fstream(StringPrintf("/proc/self/fd/%d", fd));
200 const binder::Status dump_permission = checkPermission(kDump);
201 if (!dump_permission.isOk()) {
202 out << dump_permission.toString8() << endl;
203 return PERMISSION_DENIED;
204 }
205
Jeff Sharkey9462bdd2017-09-07 15:27:28 -0600206 ACQUIRE_LOCK;
Jeff Sharkey068c6be2017-09-06 13:47:40 -0600207 out << "vold is happy!" << endl;
208 out.flush();
Jeff Sharkey068c6be2017-09-06 13:47:40 -0600209 return NO_ERROR;
210}
211
Jeff Sharkey814e9d32017-09-13 11:49:44 -0600212binder::Status VoldNativeService::setListener(
213 const android::sp<android::os::IVoldListener>& listener) {
214 ENFORCE_UID(AID_SYSTEM);
215 ACQUIRE_LOCK;
216
217 VolumeManager::Instance()->setListener(listener);
218 return ok();
219}
220
Jeff Sharkeycbe69fc2017-09-15 16:50:28 -0600221binder::Status VoldNativeService::monitor() {
222 ENFORCE_UID(AID_SYSTEM);
223
224 // Simply acquire/release each lock for watchdog
225 {
226 ACQUIRE_LOCK;
227 }
228 {
229 ACQUIRE_CRYPT_LOCK;
230 }
231
232 return ok();
233}
234
Jeff Sharkey068c6be2017-09-06 13:47:40 -0600235binder::Status VoldNativeService::reset() {
236 ENFORCE_UID(AID_SYSTEM);
Jeff Sharkey9462bdd2017-09-07 15:27:28 -0600237 ACQUIRE_LOCK;
Jeff Sharkey068c6be2017-09-06 13:47:40 -0600238
Jeff Sharkey9462bdd2017-09-07 15:27:28 -0600239 return translate(VolumeManager::Instance()->reset());
240}
241
242binder::Status VoldNativeService::shutdown() {
243 ENFORCE_UID(AID_SYSTEM);
244 ACQUIRE_LOCK;
245
246 return translate(VolumeManager::Instance()->shutdown());
247}
248
Jeff Sharkey11c2d382017-09-11 10:32:01 -0600249binder::Status VoldNativeService::mountAll() {
Jeff Sharkey9462bdd2017-09-07 15:27:28 -0600250 ENFORCE_UID(AID_SYSTEM);
251 ACQUIRE_LOCK;
252
Jeff Sharkey11c2d382017-09-11 10:32:01 -0600253 struct fstab* fstab = fs_mgr_read_fstab_default();
254 int res = fs_mgr_mount_all(fstab, MOUNT_MODE_DEFAULT);
255 fs_mgr_free_fstab(fstab);
256 return translate(res);
Jeff Sharkey9462bdd2017-09-07 15:27:28 -0600257}
258
259binder::Status VoldNativeService::onUserAdded(int32_t userId, int32_t userSerial) {
260 ENFORCE_UID(AID_SYSTEM);
261 ACQUIRE_LOCK;
262
263 return translate(VolumeManager::Instance()->onUserAdded(userId, userSerial));
264}
265
266binder::Status VoldNativeService::onUserRemoved(int32_t userId) {
267 ENFORCE_UID(AID_SYSTEM);
268 ACQUIRE_LOCK;
269
270 return translate(VolumeManager::Instance()->onUserRemoved(userId));
271}
272
273binder::Status VoldNativeService::onUserStarted(int32_t userId) {
274 ENFORCE_UID(AID_SYSTEM);
275 ACQUIRE_LOCK;
276
277 return translate(VolumeManager::Instance()->onUserStarted(userId));
278}
279
280binder::Status VoldNativeService::onUserStopped(int32_t userId) {
281 ENFORCE_UID(AID_SYSTEM);
282 ACQUIRE_LOCK;
283
284 return translate(VolumeManager::Instance()->onUserStopped(userId));
285}
286
Jeff Sharkey11c2d382017-09-11 10:32:01 -0600287binder::Status VoldNativeService::partition(const std::string& diskId, int32_t partitionType,
288 int32_t ratio) {
Jeff Sharkey9462bdd2017-09-07 15:27:28 -0600289 ENFORCE_UID(AID_SYSTEM);
Jeff Sharkeyec4fda22017-09-12 13:19:24 -0600290 CHECK_ARGUMENT_ID(diskId);
Jeff Sharkey9462bdd2017-09-07 15:27:28 -0600291 ACQUIRE_LOCK;
292
293 auto disk = VolumeManager::Instance()->findDisk(diskId);
294 if (disk == nullptr) {
295 return error("Failed to find disk " + diskId);
296 }
297 switch (partitionType) {
298 case PARTITION_TYPE_PUBLIC: return translate(disk->partitionPublic());
299 case PARTITION_TYPE_PRIVATE: return translate(disk->partitionPrivate());
300 case PARTITION_TYPE_MIXED: return translate(disk->partitionMixed(ratio));
301 default: return error("Unknown type " + std::to_string(partitionType));
302 }
303}
304
Jeff Sharkey3ce18252017-10-24 11:08:45 -0600305binder::Status VoldNativeService::forgetPartition(const std::string& partGuid,
306 const std::string& fsUuid) {
Jeff Sharkey9462bdd2017-09-07 15:27:28 -0600307 ENFORCE_UID(AID_SYSTEM);
Jeff Sharkeyec4fda22017-09-12 13:19:24 -0600308 CHECK_ARGUMENT_HEX(partGuid);
Jeff Sharkey3ce18252017-10-24 11:08:45 -0600309 CHECK_ARGUMENT_HEX(fsUuid);
Jeff Sharkey9462bdd2017-09-07 15:27:28 -0600310 ACQUIRE_LOCK;
311
Jeff Sharkey3ce18252017-10-24 11:08:45 -0600312 return translate(VolumeManager::Instance()->forgetPartition(partGuid, fsUuid));
Jeff Sharkey9462bdd2017-09-07 15:27:28 -0600313}
314
Jeff Sharkey11c2d382017-09-11 10:32:01 -0600315binder::Status VoldNativeService::mount(const std::string& volId, int32_t mountFlags,
316 int32_t mountUserId) {
Jeff Sharkey9462bdd2017-09-07 15:27:28 -0600317 ENFORCE_UID(AID_SYSTEM);
Jeff Sharkeyec4fda22017-09-12 13:19:24 -0600318 CHECK_ARGUMENT_ID(volId);
Jeff Sharkey9462bdd2017-09-07 15:27:28 -0600319 ACQUIRE_LOCK;
320
321 auto vol = VolumeManager::Instance()->findVolume(volId);
322 if (vol == nullptr) {
323 return error("Failed to find volume " + volId);
324 }
325
326 vol->setMountFlags(mountFlags);
327 vol->setMountUserId(mountUserId);
328
329 int res = vol->mount();
Jeff Sharkey83b559c2017-09-12 16:30:52 -0600330 if ((mountFlags & MOUNT_FLAG_PRIMARY) != 0) {
Jeff Sharkey9462bdd2017-09-07 15:27:28 -0600331 VolumeManager::Instance()->setPrimary(vol);
332 }
333 return translate(res);
334}
335
336binder::Status VoldNativeService::unmount(const std::string& volId) {
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 auto vol = VolumeManager::Instance()->findVolume(volId);
342 if (vol == nullptr) {
343 return error("Failed to find volume " + volId);
344 }
345 return translate(vol->unmount());
346}
347
348binder::Status VoldNativeService::format(const std::string& volId, const std::string& fsType) {
349 ENFORCE_UID(AID_SYSTEM);
Jeff Sharkeyec4fda22017-09-12 13:19:24 -0600350 CHECK_ARGUMENT_ID(volId);
Jeff Sharkey9462bdd2017-09-07 15:27:28 -0600351 ACQUIRE_LOCK;
352
353 auto vol = VolumeManager::Instance()->findVolume(volId);
354 if (vol == nullptr) {
355 return error("Failed to find volume " + volId);
356 }
357 return translate(vol->format(fsType));
358}
359
Jeff Sharkey52f7a912017-09-15 12:57:44 -0600360binder::Status VoldNativeService::benchmark(const std::string& volId,
361 const android::sp<android::os::IVoldTaskListener>& listener) {
Jeff Sharkey9462bdd2017-09-07 15:27:28 -0600362 ENFORCE_UID(AID_SYSTEM);
Jeff Sharkeyec4fda22017-09-12 13:19:24 -0600363 CHECK_ARGUMENT_ID(volId);
Jeff Sharkey9462bdd2017-09-07 15:27:28 -0600364 ACQUIRE_LOCK;
365
Jeff Sharkey52f7a912017-09-15 12:57:44 -0600366 std::string path;
367 if (volId == "private" || volId == "null") {
368 path = "/data";
369 } else {
370 auto vol = VolumeManager::Instance()->findVolume(volId);
371 if (vol == nullptr) {
372 return error("Failed to find volume " + volId);
373 }
374 if (vol->getType() != VolumeBase::Type::kPrivate) {
375 return error("Volume " + volId + " not private");
376 }
377 if (vol->getState() != VolumeBase::State::kMounted) {
378 return error("Volume " + volId + " not mounted");
379 }
380 path = vol->getPath();
381 }
382
383 if (path.empty()) {
384 return error("Volume " + volId + " missing path");
385 }
386
Jeff Sharkey01a0e7f2017-10-17 16:06:32 -0600387 std::thread([=]() {
388 android::vold::Benchmark(path, listener);
389 }).detach();
Jeff Sharkey068c6be2017-09-06 13:47:40 -0600390 return ok();
391}
392
Jeff Sharkey11c2d382017-09-11 10:32:01 -0600393binder::Status VoldNativeService::moveStorage(const std::string& fromVolId,
Jeff Sharkey52f7a912017-09-15 12:57:44 -0600394 const std::string& toVolId, const android::sp<android::os::IVoldTaskListener>& listener) {
Jeff Sharkey9462bdd2017-09-07 15:27:28 -0600395 ENFORCE_UID(AID_SYSTEM);
Jeff Sharkeyec4fda22017-09-12 13:19:24 -0600396 CHECK_ARGUMENT_ID(fromVolId);
397 CHECK_ARGUMENT_ID(toVolId);
Jeff Sharkey9462bdd2017-09-07 15:27:28 -0600398 ACQUIRE_LOCK;
399
400 auto fromVol = VolumeManager::Instance()->findVolume(fromVolId);
401 auto toVol = VolumeManager::Instance()->findVolume(toVolId);
402 if (fromVol == nullptr) {
403 return error("Failed to find volume " + fromVolId);
404 } else if (toVol == nullptr) {
405 return error("Failed to find volume " + toVolId);
406 }
Jeff Sharkey01a0e7f2017-10-17 16:06:32 -0600407
408 std::thread([=]() {
409 android::vold::MoveStorage(fromVol, toVol, listener);
410 }).detach();
Jeff Sharkey9462bdd2017-09-07 15:27:28 -0600411 return ok();
412}
413
414binder::Status VoldNativeService::remountUid(int32_t uid, int32_t remountMode) {
415 ENFORCE_UID(AID_SYSTEM);
416 ACQUIRE_LOCK;
417
418 std::string tmp;
419 switch (remountMode) {
420 case REMOUNT_MODE_NONE: tmp = "none"; break;
421 case REMOUNT_MODE_DEFAULT: tmp = "default"; break;
422 case REMOUNT_MODE_READ: tmp = "read"; break;
423 case REMOUNT_MODE_WRITE: tmp = "write"; break;
424 default: return error("Unknown mode " + std::to_string(remountMode));
425 }
426 return translate(VolumeManager::Instance()->remountUid(uid, tmp));
427}
428
429binder::Status VoldNativeService::mkdirs(const std::string& path) {
430 ENFORCE_UID(AID_SYSTEM);
Jeff Sharkeyec4fda22017-09-12 13:19:24 -0600431 CHECK_ARGUMENT_PATH(path);
Jeff Sharkey9462bdd2017-09-07 15:27:28 -0600432 ACQUIRE_LOCK;
433
Jeff Sharkey3472e522017-10-06 18:02:53 -0600434 return translate(VolumeManager::Instance()->mkdirs(path));
Jeff Sharkey9462bdd2017-09-07 15:27:28 -0600435}
436
Jeff Sharkey11c2d382017-09-11 10:32:01 -0600437binder::Status VoldNativeService::createObb(const std::string& sourcePath,
438 const std::string& sourceKey, int32_t ownerGid, std::string* _aidl_return) {
439 ENFORCE_UID(AID_SYSTEM);
Jeff Sharkeyec4fda22017-09-12 13:19:24 -0600440 CHECK_ARGUMENT_PATH(sourcePath);
441 CHECK_ARGUMENT_HEX(sourceKey);
Jeff Sharkey11c2d382017-09-11 10:32:01 -0600442 ACQUIRE_LOCK;
443
444 return translate(
445 VolumeManager::Instance()->createObb(sourcePath, sourceKey, ownerGid, _aidl_return));
446}
447
448binder::Status VoldNativeService::destroyObb(const std::string& volId) {
449 ENFORCE_UID(AID_SYSTEM);
Jeff Sharkeyec4fda22017-09-12 13:19:24 -0600450 CHECK_ARGUMENT_ID(volId);
Jeff Sharkey11c2d382017-09-11 10:32:01 -0600451 ACQUIRE_LOCK;
452
453 return translate(VolumeManager::Instance()->destroyObb(volId));
454}
455
Jeff Sharkey52f7a912017-09-15 12:57:44 -0600456binder::Status VoldNativeService::fstrim(int32_t fstrimFlags,
457 const android::sp<android::os::IVoldTaskListener>& listener) {
Jeff Sharkey11c2d382017-09-11 10:32:01 -0600458 ENFORCE_UID(AID_SYSTEM);
459 ACQUIRE_LOCK;
460
Jeff Sharkey01a0e7f2017-10-17 16:06:32 -0600461 std::thread([=]() {
462 android::vold::Trim(listener);
463 }).detach();
Jeff Sharkey11c2d382017-09-11 10:32:01 -0600464 return ok();
465}
466
467binder::Status VoldNativeService::mountAppFuse(int32_t uid, int32_t pid, int32_t mountId,
468 android::base::unique_fd* _aidl_return) {
469 ENFORCE_UID(AID_SYSTEM);
470 ACQUIRE_LOCK;
471
472 return translate(VolumeManager::Instance()->mountAppFuse(uid, pid, mountId, _aidl_return));
473}
474
475binder::Status VoldNativeService::unmountAppFuse(int32_t uid, int32_t pid, int32_t mountId) {
476 ENFORCE_UID(AID_SYSTEM);
477 ACQUIRE_LOCK;
478
479 return translate(VolumeManager::Instance()->unmountAppFuse(uid, pid, mountId));
480}
481
Jeff Sharkey83b559c2017-09-12 16:30:52 -0600482binder::Status VoldNativeService::fdeCheckPassword(const std::string& password) {
483 ENFORCE_UID(AID_SYSTEM);
484 ACQUIRE_CRYPT_LOCK;
485
486 return translate(cryptfs_check_passwd(password.c_str()));
487}
488
489binder::Status VoldNativeService::fdeRestart() {
490 ENFORCE_UID(AID_SYSTEM);
491 ACQUIRE_CRYPT_LOCK;
492
493 // Spawn as thread so init can issue commands back to vold without
494 // causing deadlock, usually as a result of prep_data_fs.
495 std::thread(&cryptfs_restart).detach();
496 return ok();
497}
498
499binder::Status VoldNativeService::fdeComplete(int32_t* _aidl_return) {
500 ENFORCE_UID(AID_SYSTEM);
501 ACQUIRE_CRYPT_LOCK;
502
503 *_aidl_return = cryptfs_crypto_complete();
504 return ok();
505}
506
507static int fdeEnableInternal(int32_t passwordType, const std::string& password,
508 int32_t encryptionFlags) {
509 bool noUi = (encryptionFlags & VoldNativeService::ENCRYPTION_FLAG_NO_UI) != 0;
510
511 std::string how;
512 if ((encryptionFlags & VoldNativeService::ENCRYPTION_FLAG_IN_PLACE) != 0) {
513 how = "inplace";
514 } else if ((encryptionFlags & VoldNativeService::ENCRYPTION_FLAG_WIPE) != 0) {
515 how = "wipe";
516 } else {
517 LOG(ERROR) << "Missing encryption flag";
518 return -1;
519 }
520
521 for (int tries = 0; tries < 2; ++tries) {
522 int rc;
523 if (passwordType == VoldNativeService::PASSWORD_TYPE_DEFAULT) {
524 rc = cryptfs_enable_default(how.c_str(), noUi);
525 } else {
526 rc = cryptfs_enable(how.c_str(), passwordType, password.c_str(), noUi);
527 }
528
529 if (rc == 0) {
530 return 0;
531 } else if (tries == 0) {
Jeff Sharkey3472e522017-10-06 18:02:53 -0600532 KillProcessesWithOpenFiles(DATA_MNT_POINT, SIGKILL);
Jeff Sharkey83b559c2017-09-12 16:30:52 -0600533 }
534 }
535
536 return -1;
537}
538
539binder::Status VoldNativeService::fdeEnable(int32_t passwordType,
540 const std::string& password, int32_t encryptionFlags) {
541 ENFORCE_UID(AID_SYSTEM);
542 ACQUIRE_CRYPT_LOCK;
543
544 if (e4crypt_is_native()) {
545 if (passwordType != PASSWORD_TYPE_DEFAULT) {
546 return error("Unexpected password type");
547 }
548 if (encryptionFlags != (ENCRYPTION_FLAG_IN_PLACE | ENCRYPTION_FLAG_NO_UI)) {
549 return error("Unexpected flags");
550 }
551 return translateBool(e4crypt_enable_crypto());
552 }
553
554 // Spawn as thread so init can issue commands back to vold without
555 // causing deadlock, usually as a result of prep_data_fs.
556 std::thread(&fdeEnableInternal, passwordType, password, encryptionFlags).detach();
557 return ok();
558}
559
560binder::Status VoldNativeService::fdeChangePassword(int32_t passwordType,
561 const std::string& password) {
562 ENFORCE_UID(AID_SYSTEM);
563 ACQUIRE_CRYPT_LOCK;
564
565 return translate(cryptfs_changepw(passwordType, password.c_str()));
566}
567
568binder::Status VoldNativeService::fdeVerifyPassword(const std::string& password) {
569 ENFORCE_UID(AID_SYSTEM);
570 ACQUIRE_CRYPT_LOCK;
571
572 return translate(cryptfs_verify_passwd(password.c_str()));
573}
574
575binder::Status VoldNativeService::fdeGetField(const std::string& key,
576 std::string* _aidl_return) {
577 ENFORCE_UID(AID_SYSTEM);
578 ACQUIRE_CRYPT_LOCK;
579
580 char buf[PROPERTY_VALUE_MAX];
581 if (cryptfs_getfield(key.c_str(), buf, sizeof(buf)) != CRYPTO_GETFIELD_OK) {
582 return error(StringPrintf("Failed to read field %s", key.c_str()));
583 } else {
584 *_aidl_return = buf;
585 return ok();
586 }
587}
588
589binder::Status VoldNativeService::fdeSetField(const std::string& key,
590 const std::string& value) {
591 ENFORCE_UID(AID_SYSTEM);
592 ACQUIRE_CRYPT_LOCK;
593
594 return translate(cryptfs_setfield(key.c_str(), value.c_str()));
595}
596
597binder::Status VoldNativeService::fdeGetPasswordType(int32_t* _aidl_return) {
598 ENFORCE_UID(AID_SYSTEM);
599 ACQUIRE_CRYPT_LOCK;
600
601 *_aidl_return = cryptfs_get_password_type();
602 return ok();
603}
604
605binder::Status VoldNativeService::fdeGetPassword(std::string* _aidl_return) {
606 ENFORCE_UID(AID_SYSTEM);
607 ACQUIRE_CRYPT_LOCK;
608
609 const char* res = cryptfs_get_password();
610 if (res != nullptr) {
611 *_aidl_return = res;
612 }
613 return ok();
614}
615
616binder::Status VoldNativeService::fdeClearPassword() {
617 ENFORCE_UID(AID_SYSTEM);
618 ACQUIRE_CRYPT_LOCK;
619
620 cryptfs_clear_password();
621 return ok();
622}
623
624binder::Status VoldNativeService::fbeEnable() {
625 ENFORCE_UID(AID_SYSTEM);
626 ACQUIRE_CRYPT_LOCK;
627
628 return translateBool(e4crypt_initialize_global_de());
629}
630
631binder::Status VoldNativeService::mountDefaultEncrypted() {
632 ENFORCE_UID(AID_SYSTEM);
633 ACQUIRE_CRYPT_LOCK;
634
635 if (e4crypt_is_native()) {
636 return translateBool(e4crypt_mount_metadata_encrypted());
637 } else {
638 // Spawn as thread so init can issue commands back to vold without
639 // causing deadlock, usually as a result of prep_data_fs.
640 std::thread(&cryptfs_mount_default_encrypted).detach();
641 return ok();
642 }
643}
644
645binder::Status VoldNativeService::initUser0() {
646 ENFORCE_UID(AID_SYSTEM);
647 ACQUIRE_CRYPT_LOCK;
648
649 return translateBool(e4crypt_init_user0());
650}
651
652binder::Status VoldNativeService::isConvertibleToFbe(bool* _aidl_return) {
653 ENFORCE_UID(AID_SYSTEM);
654 ACQUIRE_CRYPT_LOCK;
655
656 *_aidl_return = cryptfs_isConvertibleToFBE() != 0;
657 return ok();
658}
659
660binder::Status VoldNativeService::createUserKey(int32_t userId, int32_t userSerial,
661 bool ephemeral) {
662 ENFORCE_UID(AID_SYSTEM);
663 ACQUIRE_CRYPT_LOCK;
664
665 return translateBool(e4crypt_vold_create_user_key(userId, userSerial, ephemeral));
666}
667
668binder::Status VoldNativeService::destroyUserKey(int32_t userId) {
669 ENFORCE_UID(AID_SYSTEM);
670 ACQUIRE_CRYPT_LOCK;
671
672 return translateBool(e4crypt_destroy_user_key(userId));
673}
674
675binder::Status VoldNativeService::addUserKeyAuth(int32_t userId, int32_t userSerial,
676 const std::string& token, const std::string& secret) {
677 ENFORCE_UID(AID_SYSTEM);
678 ACQUIRE_CRYPT_LOCK;
679
Paul Crowley3b71fc52017-10-09 10:55:21 -0700680 return translateBool(e4crypt_add_user_key_auth(userId, userSerial, token, secret));
Jeff Sharkey83b559c2017-09-12 16:30:52 -0600681}
682
683binder::Status VoldNativeService::fixateNewestUserKeyAuth(int32_t userId) {
684 ENFORCE_UID(AID_SYSTEM);
685 ACQUIRE_CRYPT_LOCK;
686
687 return translateBool(e4crypt_fixate_newest_user_key_auth(userId));
688}
689
690binder::Status VoldNativeService::unlockUserKey(int32_t userId, int32_t userSerial,
691 const std::string& token, const std::string& secret) {
692 ENFORCE_UID(AID_SYSTEM);
693 ACQUIRE_CRYPT_LOCK;
694
Paul Crowley3b71fc52017-10-09 10:55:21 -0700695 return translateBool(e4crypt_unlock_user_key(userId, userSerial, token, secret));
Jeff Sharkey83b559c2017-09-12 16:30:52 -0600696}
697
698binder::Status VoldNativeService::lockUserKey(int32_t userId) {
699 ENFORCE_UID(AID_SYSTEM);
700 ACQUIRE_CRYPT_LOCK;
701
702 return translateBool(e4crypt_lock_user_key(userId));
703}
704
705binder::Status VoldNativeService::prepareUserStorage(const std::unique_ptr<std::string>& uuid,
706 int32_t userId, int32_t userSerial, int32_t flags) {
707 ENFORCE_UID(AID_SYSTEM);
Paul Crowley3b71fc52017-10-09 10:55:21 -0700708 std::string empty_string = "";
709 auto uuid_ = uuid ? *uuid : empty_string;
Paul Crowley06f762d2017-10-16 10:59:51 -0700710 CHECK_ARGUMENT_HEX(uuid_);
711
712 ACQUIRE_CRYPT_LOCK;
Jeff Sharkey83b559c2017-09-12 16:30:52 -0600713 return translateBool(e4crypt_prepare_user_storage(uuid_, userId, userSerial, flags));
714}
715
716binder::Status VoldNativeService::destroyUserStorage(const std::unique_ptr<std::string>& uuid,
717 int32_t userId, int32_t flags) {
718 ENFORCE_UID(AID_SYSTEM);
Paul Crowley3b71fc52017-10-09 10:55:21 -0700719 std::string empty_string = "";
720 auto uuid_ = uuid ? *uuid : empty_string;
Paul Crowley06f762d2017-10-16 10:59:51 -0700721 CHECK_ARGUMENT_HEX(uuid_);
722
723 ACQUIRE_CRYPT_LOCK;
Jeff Sharkey83b559c2017-09-12 16:30:52 -0600724 return translateBool(e4crypt_destroy_user_storage(uuid_, userId, flags));
725}
726
727binder::Status VoldNativeService::secdiscard(const std::string& path) {
728 ENFORCE_UID(AID_SYSTEM);
Jeff Sharkey01a0e7f2017-10-17 16:06:32 -0600729 CHECK_ARGUMENT_PATH(path);
Jeff Sharkey83b559c2017-09-12 16:30:52 -0600730 ACQUIRE_CRYPT_LOCK;
731
Paul Crowley3b71fc52017-10-09 10:55:21 -0700732 return translateBool(e4crypt_secdiscard(path));
Jeff Sharkey83b559c2017-09-12 16:30:52 -0600733}
734
Jeff Sharkey068c6be2017-09-06 13:47:40 -0600735} // namespace vold
736} // namespace android