blob: d8832d36d12cbd409f6804ec26874dcaaff959f8 [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
305binder::Status VoldNativeService::forgetPartition(const std::string& partGuid) {
306 ENFORCE_UID(AID_SYSTEM);
Jeff Sharkeyec4fda22017-09-12 13:19:24 -0600307 CHECK_ARGUMENT_HEX(partGuid);
Jeff Sharkey9462bdd2017-09-07 15:27:28 -0600308 ACQUIRE_LOCK;
309
310 return translate(VolumeManager::Instance()->forgetPartition(partGuid));
311}
312
Jeff Sharkey11c2d382017-09-11 10:32:01 -0600313binder::Status VoldNativeService::mount(const std::string& volId, int32_t mountFlags,
314 int32_t mountUserId) {
Jeff Sharkey9462bdd2017-09-07 15:27:28 -0600315 ENFORCE_UID(AID_SYSTEM);
Jeff Sharkeyec4fda22017-09-12 13:19:24 -0600316 CHECK_ARGUMENT_ID(volId);
Jeff Sharkey9462bdd2017-09-07 15:27:28 -0600317 ACQUIRE_LOCK;
318
319 auto vol = VolumeManager::Instance()->findVolume(volId);
320 if (vol == nullptr) {
321 return error("Failed to find volume " + volId);
322 }
323
324 vol->setMountFlags(mountFlags);
325 vol->setMountUserId(mountUserId);
326
327 int res = vol->mount();
Jeff Sharkey83b559c2017-09-12 16:30:52 -0600328 if ((mountFlags & MOUNT_FLAG_PRIMARY) != 0) {
Jeff Sharkey9462bdd2017-09-07 15:27:28 -0600329 VolumeManager::Instance()->setPrimary(vol);
330 }
331 return translate(res);
332}
333
334binder::Status VoldNativeService::unmount(const std::string& volId) {
335 ENFORCE_UID(AID_SYSTEM);
Jeff Sharkeyec4fda22017-09-12 13:19:24 -0600336 CHECK_ARGUMENT_ID(volId);
Jeff Sharkey9462bdd2017-09-07 15:27:28 -0600337 ACQUIRE_LOCK;
338
339 auto vol = VolumeManager::Instance()->findVolume(volId);
340 if (vol == nullptr) {
341 return error("Failed to find volume " + volId);
342 }
343 return translate(vol->unmount());
344}
345
346binder::Status VoldNativeService::format(const std::string& volId, const std::string& fsType) {
347 ENFORCE_UID(AID_SYSTEM);
Jeff Sharkeyec4fda22017-09-12 13:19:24 -0600348 CHECK_ARGUMENT_ID(volId);
Jeff Sharkey9462bdd2017-09-07 15:27:28 -0600349 ACQUIRE_LOCK;
350
351 auto vol = VolumeManager::Instance()->findVolume(volId);
352 if (vol == nullptr) {
353 return error("Failed to find volume " + volId);
354 }
355 return translate(vol->format(fsType));
356}
357
Jeff Sharkey52f7a912017-09-15 12:57:44 -0600358binder::Status VoldNativeService::benchmark(const std::string& volId,
359 const android::sp<android::os::IVoldTaskListener>& listener) {
Jeff Sharkey9462bdd2017-09-07 15:27:28 -0600360 ENFORCE_UID(AID_SYSTEM);
Jeff Sharkeyec4fda22017-09-12 13:19:24 -0600361 CHECK_ARGUMENT_ID(volId);
Jeff Sharkey9462bdd2017-09-07 15:27:28 -0600362 ACQUIRE_LOCK;
363
Jeff Sharkey52f7a912017-09-15 12:57:44 -0600364 std::string path;
365 if (volId == "private" || volId == "null") {
366 path = "/data";
367 } else {
368 auto vol = VolumeManager::Instance()->findVolume(volId);
369 if (vol == nullptr) {
370 return error("Failed to find volume " + volId);
371 }
372 if (vol->getType() != VolumeBase::Type::kPrivate) {
373 return error("Volume " + volId + " not private");
374 }
375 if (vol->getState() != VolumeBase::State::kMounted) {
376 return error("Volume " + volId + " not mounted");
377 }
378 path = vol->getPath();
379 }
380
381 if (path.empty()) {
382 return error("Volume " + volId + " missing path");
383 }
384
Jeff Sharkey01a0e7f2017-10-17 16:06:32 -0600385 std::thread([=]() {
386 android::vold::Benchmark(path, listener);
387 }).detach();
Jeff Sharkey068c6be2017-09-06 13:47:40 -0600388 return ok();
389}
390
Jeff Sharkey11c2d382017-09-11 10:32:01 -0600391binder::Status VoldNativeService::moveStorage(const std::string& fromVolId,
Jeff Sharkey52f7a912017-09-15 12:57:44 -0600392 const std::string& toVolId, const android::sp<android::os::IVoldTaskListener>& listener) {
Jeff Sharkey9462bdd2017-09-07 15:27:28 -0600393 ENFORCE_UID(AID_SYSTEM);
Jeff Sharkeyec4fda22017-09-12 13:19:24 -0600394 CHECK_ARGUMENT_ID(fromVolId);
395 CHECK_ARGUMENT_ID(toVolId);
Jeff Sharkey9462bdd2017-09-07 15:27:28 -0600396 ACQUIRE_LOCK;
397
398 auto fromVol = VolumeManager::Instance()->findVolume(fromVolId);
399 auto toVol = VolumeManager::Instance()->findVolume(toVolId);
400 if (fromVol == nullptr) {
401 return error("Failed to find volume " + fromVolId);
402 } else if (toVol == nullptr) {
403 return error("Failed to find volume " + toVolId);
404 }
Jeff Sharkey01a0e7f2017-10-17 16:06:32 -0600405
406 std::thread([=]() {
407 android::vold::MoveStorage(fromVol, toVol, listener);
408 }).detach();
Jeff Sharkey9462bdd2017-09-07 15:27:28 -0600409 return ok();
410}
411
412binder::Status VoldNativeService::remountUid(int32_t uid, int32_t remountMode) {
413 ENFORCE_UID(AID_SYSTEM);
414 ACQUIRE_LOCK;
415
416 std::string tmp;
417 switch (remountMode) {
418 case REMOUNT_MODE_NONE: tmp = "none"; break;
419 case REMOUNT_MODE_DEFAULT: tmp = "default"; break;
420 case REMOUNT_MODE_READ: tmp = "read"; break;
421 case REMOUNT_MODE_WRITE: tmp = "write"; break;
422 default: return error("Unknown mode " + std::to_string(remountMode));
423 }
424 return translate(VolumeManager::Instance()->remountUid(uid, tmp));
425}
426
427binder::Status VoldNativeService::mkdirs(const std::string& path) {
428 ENFORCE_UID(AID_SYSTEM);
Jeff Sharkeyec4fda22017-09-12 13:19:24 -0600429 CHECK_ARGUMENT_PATH(path);
Jeff Sharkey9462bdd2017-09-07 15:27:28 -0600430 ACQUIRE_LOCK;
431
Jeff Sharkey3472e522017-10-06 18:02:53 -0600432 return translate(VolumeManager::Instance()->mkdirs(path));
Jeff Sharkey9462bdd2017-09-07 15:27:28 -0600433}
434
Jeff Sharkey11c2d382017-09-11 10:32:01 -0600435binder::Status VoldNativeService::createObb(const std::string& sourcePath,
436 const std::string& sourceKey, int32_t ownerGid, std::string* _aidl_return) {
437 ENFORCE_UID(AID_SYSTEM);
Jeff Sharkeyec4fda22017-09-12 13:19:24 -0600438 CHECK_ARGUMENT_PATH(sourcePath);
439 CHECK_ARGUMENT_HEX(sourceKey);
Jeff Sharkey11c2d382017-09-11 10:32:01 -0600440 ACQUIRE_LOCK;
441
442 return translate(
443 VolumeManager::Instance()->createObb(sourcePath, sourceKey, ownerGid, _aidl_return));
444}
445
446binder::Status VoldNativeService::destroyObb(const std::string& volId) {
447 ENFORCE_UID(AID_SYSTEM);
Jeff Sharkeyec4fda22017-09-12 13:19:24 -0600448 CHECK_ARGUMENT_ID(volId);
Jeff Sharkey11c2d382017-09-11 10:32:01 -0600449 ACQUIRE_LOCK;
450
451 return translate(VolumeManager::Instance()->destroyObb(volId));
452}
453
Jeff Sharkey52f7a912017-09-15 12:57:44 -0600454binder::Status VoldNativeService::fstrim(int32_t fstrimFlags,
455 const android::sp<android::os::IVoldTaskListener>& listener) {
Jeff Sharkey11c2d382017-09-11 10:32:01 -0600456 ENFORCE_UID(AID_SYSTEM);
457 ACQUIRE_LOCK;
458
Jeff Sharkey01a0e7f2017-10-17 16:06:32 -0600459 std::thread([=]() {
460 android::vold::Trim(listener);
461 }).detach();
Jeff Sharkey11c2d382017-09-11 10:32:01 -0600462 return ok();
463}
464
465binder::Status VoldNativeService::mountAppFuse(int32_t uid, int32_t pid, int32_t mountId,
466 android::base::unique_fd* _aidl_return) {
467 ENFORCE_UID(AID_SYSTEM);
468 ACQUIRE_LOCK;
469
470 return translate(VolumeManager::Instance()->mountAppFuse(uid, pid, mountId, _aidl_return));
471}
472
473binder::Status VoldNativeService::unmountAppFuse(int32_t uid, int32_t pid, int32_t mountId) {
474 ENFORCE_UID(AID_SYSTEM);
475 ACQUIRE_LOCK;
476
477 return translate(VolumeManager::Instance()->unmountAppFuse(uid, pid, mountId));
478}
479
Jeff Sharkey83b559c2017-09-12 16:30:52 -0600480binder::Status VoldNativeService::fdeCheckPassword(const std::string& password) {
481 ENFORCE_UID(AID_SYSTEM);
482 ACQUIRE_CRYPT_LOCK;
483
484 return translate(cryptfs_check_passwd(password.c_str()));
485}
486
487binder::Status VoldNativeService::fdeRestart() {
488 ENFORCE_UID(AID_SYSTEM);
489 ACQUIRE_CRYPT_LOCK;
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(&cryptfs_restart).detach();
494 return ok();
495}
496
497binder::Status VoldNativeService::fdeComplete(int32_t* _aidl_return) {
498 ENFORCE_UID(AID_SYSTEM);
499 ACQUIRE_CRYPT_LOCK;
500
501 *_aidl_return = cryptfs_crypto_complete();
502 return ok();
503}
504
505static int fdeEnableInternal(int32_t passwordType, const std::string& password,
506 int32_t encryptionFlags) {
507 bool noUi = (encryptionFlags & VoldNativeService::ENCRYPTION_FLAG_NO_UI) != 0;
508
509 std::string how;
510 if ((encryptionFlags & VoldNativeService::ENCRYPTION_FLAG_IN_PLACE) != 0) {
511 how = "inplace";
512 } else if ((encryptionFlags & VoldNativeService::ENCRYPTION_FLAG_WIPE) != 0) {
513 how = "wipe";
514 } else {
515 LOG(ERROR) << "Missing encryption flag";
516 return -1;
517 }
518
519 for (int tries = 0; tries < 2; ++tries) {
520 int rc;
521 if (passwordType == VoldNativeService::PASSWORD_TYPE_DEFAULT) {
522 rc = cryptfs_enable_default(how.c_str(), noUi);
523 } else {
524 rc = cryptfs_enable(how.c_str(), passwordType, password.c_str(), noUi);
525 }
526
527 if (rc == 0) {
528 return 0;
529 } else if (tries == 0) {
Jeff Sharkey3472e522017-10-06 18:02:53 -0600530 KillProcessesWithOpenFiles(DATA_MNT_POINT, SIGKILL);
Jeff Sharkey83b559c2017-09-12 16:30:52 -0600531 }
532 }
533
534 return -1;
535}
536
537binder::Status VoldNativeService::fdeEnable(int32_t passwordType,
538 const std::string& password, int32_t encryptionFlags) {
539 ENFORCE_UID(AID_SYSTEM);
540 ACQUIRE_CRYPT_LOCK;
541
542 if (e4crypt_is_native()) {
543 if (passwordType != PASSWORD_TYPE_DEFAULT) {
544 return error("Unexpected password type");
545 }
546 if (encryptionFlags != (ENCRYPTION_FLAG_IN_PLACE | ENCRYPTION_FLAG_NO_UI)) {
547 return error("Unexpected flags");
548 }
549 return translateBool(e4crypt_enable_crypto());
550 }
551
552 // Spawn as thread so init can issue commands back to vold without
553 // causing deadlock, usually as a result of prep_data_fs.
554 std::thread(&fdeEnableInternal, passwordType, password, encryptionFlags).detach();
555 return ok();
556}
557
558binder::Status VoldNativeService::fdeChangePassword(int32_t passwordType,
559 const std::string& password) {
560 ENFORCE_UID(AID_SYSTEM);
561 ACQUIRE_CRYPT_LOCK;
562
563 return translate(cryptfs_changepw(passwordType, password.c_str()));
564}
565
566binder::Status VoldNativeService::fdeVerifyPassword(const std::string& password) {
567 ENFORCE_UID(AID_SYSTEM);
568 ACQUIRE_CRYPT_LOCK;
569
570 return translate(cryptfs_verify_passwd(password.c_str()));
571}
572
573binder::Status VoldNativeService::fdeGetField(const std::string& key,
574 std::string* _aidl_return) {
575 ENFORCE_UID(AID_SYSTEM);
576 ACQUIRE_CRYPT_LOCK;
577
578 char buf[PROPERTY_VALUE_MAX];
579 if (cryptfs_getfield(key.c_str(), buf, sizeof(buf)) != CRYPTO_GETFIELD_OK) {
580 return error(StringPrintf("Failed to read field %s", key.c_str()));
581 } else {
582 *_aidl_return = buf;
583 return ok();
584 }
585}
586
587binder::Status VoldNativeService::fdeSetField(const std::string& key,
588 const std::string& value) {
589 ENFORCE_UID(AID_SYSTEM);
590 ACQUIRE_CRYPT_LOCK;
591
592 return translate(cryptfs_setfield(key.c_str(), value.c_str()));
593}
594
595binder::Status VoldNativeService::fdeGetPasswordType(int32_t* _aidl_return) {
596 ENFORCE_UID(AID_SYSTEM);
597 ACQUIRE_CRYPT_LOCK;
598
599 *_aidl_return = cryptfs_get_password_type();
600 return ok();
601}
602
603binder::Status VoldNativeService::fdeGetPassword(std::string* _aidl_return) {
604 ENFORCE_UID(AID_SYSTEM);
605 ACQUIRE_CRYPT_LOCK;
606
607 const char* res = cryptfs_get_password();
608 if (res != nullptr) {
609 *_aidl_return = res;
610 }
611 return ok();
612}
613
614binder::Status VoldNativeService::fdeClearPassword() {
615 ENFORCE_UID(AID_SYSTEM);
616 ACQUIRE_CRYPT_LOCK;
617
618 cryptfs_clear_password();
619 return ok();
620}
621
622binder::Status VoldNativeService::fbeEnable() {
623 ENFORCE_UID(AID_SYSTEM);
624 ACQUIRE_CRYPT_LOCK;
625
626 return translateBool(e4crypt_initialize_global_de());
627}
628
629binder::Status VoldNativeService::mountDefaultEncrypted() {
630 ENFORCE_UID(AID_SYSTEM);
631 ACQUIRE_CRYPT_LOCK;
632
633 if (e4crypt_is_native()) {
634 return translateBool(e4crypt_mount_metadata_encrypted());
635 } else {
636 // Spawn as thread so init can issue commands back to vold without
637 // causing deadlock, usually as a result of prep_data_fs.
638 std::thread(&cryptfs_mount_default_encrypted).detach();
639 return ok();
640 }
641}
642
643binder::Status VoldNativeService::initUser0() {
644 ENFORCE_UID(AID_SYSTEM);
645 ACQUIRE_CRYPT_LOCK;
646
647 return translateBool(e4crypt_init_user0());
648}
649
650binder::Status VoldNativeService::isConvertibleToFbe(bool* _aidl_return) {
651 ENFORCE_UID(AID_SYSTEM);
652 ACQUIRE_CRYPT_LOCK;
653
654 *_aidl_return = cryptfs_isConvertibleToFBE() != 0;
655 return ok();
656}
657
658binder::Status VoldNativeService::createUserKey(int32_t userId, int32_t userSerial,
659 bool ephemeral) {
660 ENFORCE_UID(AID_SYSTEM);
661 ACQUIRE_CRYPT_LOCK;
662
663 return translateBool(e4crypt_vold_create_user_key(userId, userSerial, ephemeral));
664}
665
666binder::Status VoldNativeService::destroyUserKey(int32_t userId) {
667 ENFORCE_UID(AID_SYSTEM);
668 ACQUIRE_CRYPT_LOCK;
669
670 return translateBool(e4crypt_destroy_user_key(userId));
671}
672
673binder::Status VoldNativeService::addUserKeyAuth(int32_t userId, int32_t userSerial,
674 const std::string& token, const std::string& secret) {
675 ENFORCE_UID(AID_SYSTEM);
676 ACQUIRE_CRYPT_LOCK;
677
Paul Crowley3b71fc52017-10-09 10:55:21 -0700678 return translateBool(e4crypt_add_user_key_auth(userId, userSerial, token, secret));
Jeff Sharkey83b559c2017-09-12 16:30:52 -0600679}
680
681binder::Status VoldNativeService::fixateNewestUserKeyAuth(int32_t userId) {
682 ENFORCE_UID(AID_SYSTEM);
683 ACQUIRE_CRYPT_LOCK;
684
685 return translateBool(e4crypt_fixate_newest_user_key_auth(userId));
686}
687
688binder::Status VoldNativeService::unlockUserKey(int32_t userId, int32_t userSerial,
689 const std::string& token, const std::string& secret) {
690 ENFORCE_UID(AID_SYSTEM);
691 ACQUIRE_CRYPT_LOCK;
692
Paul Crowley3b71fc52017-10-09 10:55:21 -0700693 return translateBool(e4crypt_unlock_user_key(userId, userSerial, token, secret));
Jeff Sharkey83b559c2017-09-12 16:30:52 -0600694}
695
696binder::Status VoldNativeService::lockUserKey(int32_t userId) {
697 ENFORCE_UID(AID_SYSTEM);
698 ACQUIRE_CRYPT_LOCK;
699
700 return translateBool(e4crypt_lock_user_key(userId));
701}
702
703binder::Status VoldNativeService::prepareUserStorage(const std::unique_ptr<std::string>& uuid,
704 int32_t userId, int32_t userSerial, int32_t flags) {
705 ENFORCE_UID(AID_SYSTEM);
Paul Crowley3b71fc52017-10-09 10:55:21 -0700706 std::string empty_string = "";
707 auto uuid_ = uuid ? *uuid : empty_string;
Paul Crowley06f762d2017-10-16 10:59:51 -0700708 CHECK_ARGUMENT_HEX(uuid_);
709
710 ACQUIRE_CRYPT_LOCK;
Jeff Sharkey83b559c2017-09-12 16:30:52 -0600711 return translateBool(e4crypt_prepare_user_storage(uuid_, userId, userSerial, flags));
712}
713
714binder::Status VoldNativeService::destroyUserStorage(const std::unique_ptr<std::string>& uuid,
715 int32_t userId, int32_t flags) {
716 ENFORCE_UID(AID_SYSTEM);
Paul Crowley3b71fc52017-10-09 10:55:21 -0700717 std::string empty_string = "";
718 auto uuid_ = uuid ? *uuid : empty_string;
Paul Crowley06f762d2017-10-16 10:59:51 -0700719 CHECK_ARGUMENT_HEX(uuid_);
720
721 ACQUIRE_CRYPT_LOCK;
Jeff Sharkey83b559c2017-09-12 16:30:52 -0600722 return translateBool(e4crypt_destroy_user_storage(uuid_, userId, flags));
723}
724
725binder::Status VoldNativeService::secdiscard(const std::string& path) {
726 ENFORCE_UID(AID_SYSTEM);
Jeff Sharkey01a0e7f2017-10-17 16:06:32 -0600727 CHECK_ARGUMENT_PATH(path);
Jeff Sharkey83b559c2017-09-12 16:30:52 -0600728 ACQUIRE_CRYPT_LOCK;
729
Paul Crowley3b71fc52017-10-09 10:55:21 -0700730 return translateBool(e4crypt_secdiscard(path));
Jeff Sharkey83b559c2017-09-12 16:30:52 -0600731}
732
Jeff Sharkey068c6be2017-09-06 13:47:40 -0600733} // namespace vold
734} // namespace android