blob: 81523c66a55334bc76f40fc571c7d704f707646b [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"
Jeff Sharkey01a0e7f2017-10-17 16:06:32 -060020#include "Benchmark.h"
Jeff Sharkey2048a282017-06-15 09:59:43 -060021#include "CheckEncryption.h"
22#include "IdleMaint.h"
Jeff Sharkey01a0e7f2017-10-17 16:06:32 -060023#include "MoveStorage.h"
Jeff Sharkey83b559c2017-09-12 16:30:52 -060024#include "Process.h"
Jeff Sharkey2048a282017-06-15 09:59:43 -060025#include "VolumeManager.h"
Jeff Sharkey068c6be2017-09-06 13:47:40 -060026
Jeff Sharkey83b559c2017-09-12 16:30:52 -060027#include "cryptfs.h"
28#include "Ext4Crypt.h"
29#include "MetadataCrypt.h"
30
Jeff Sharkey068c6be2017-09-06 13:47:40 -060031#include <fstream>
Jeff Sharkey01a0e7f2017-10-17 16:06:32 -060032#include <thread>
Jeff Sharkey068c6be2017-09-06 13:47:40 -060033
34#include <android-base/logging.h>
35#include <android-base/stringprintf.h>
36#include <android-base/strings.h>
Paul Crowley3b71fc52017-10-09 10:55:21 -070037#include <ext4_utils/ext4_crypt.h>
Jeff Sharkey11c2d382017-09-11 10:32:01 -060038#include <fs_mgr.h>
Jeff Sharkey068c6be2017-09-06 13:47:40 -060039#include <private/android_filesystem_config.h>
Jeff Sharkey67b8c492017-09-21 17:08:43 -060040#include <utils/Trace.h>
Jeff Sharkey068c6be2017-09-06 13:47:40 -060041
Jeff Sharkey068c6be2017-09-06 13:47:40 -060042using android::base::StringPrintf;
43using std::endl;
44
45namespace android {
46namespace vold {
47
48namespace {
49
50constexpr const char* kDump = "android.permission.DUMP";
51
52static binder::Status ok() {
53 return binder::Status::ok();
54}
55
56static binder::Status exception(uint32_t code, const std::string& msg) {
57 return binder::Status::fromExceptionCode(code, String8(msg.c_str()));
58}
59
Jeff Sharkey9462bdd2017-09-07 15:27:28 -060060static binder::Status error(const std::string& msg) {
61 PLOG(ERROR) << msg;
62 return binder::Status::fromServiceSpecificError(errno, String8(msg.c_str()));
63}
64
Jeff Sharkey83b559c2017-09-12 16:30:52 -060065static binder::Status translate(int status) {
Jeff Sharkey9462bdd2017-09-07 15:27:28 -060066 if (status == 0) {
67 return binder::Status::ok();
68 } else {
Jeff Sharkey11c2d382017-09-11 10:32:01 -060069 return binder::Status::fromServiceSpecificError(status);
Jeff Sharkey9462bdd2017-09-07 15:27:28 -060070 }
71}
72
Jeff Sharkey83b559c2017-09-12 16:30:52 -060073static binder::Status translateBool(bool status) {
74 if (status) {
75 return binder::Status::ok();
76 } else {
77 return binder::Status::fromServiceSpecificError(status);
78 }
79}
80
Jeff Sharkey068c6be2017-09-06 13:47:40 -060081binder::Status checkPermission(const char* permission) {
82 pid_t pid;
83 uid_t uid;
84
85 if (checkCallingPermission(String16(permission), reinterpret_cast<int32_t*>(&pid),
86 reinterpret_cast<int32_t*>(&uid))) {
87 return ok();
88 } else {
89 return exception(binder::Status::EX_SECURITY,
90 StringPrintf("UID %d / PID %d lacks permission %s", uid, pid, permission));
91 }
92}
93
94binder::Status checkUid(uid_t expectedUid) {
95 uid_t uid = IPCThreadState::self()->getCallingUid();
96 if (uid == expectedUid || uid == AID_ROOT) {
97 return ok();
98 } else {
99 return exception(binder::Status::EX_SECURITY,
100 StringPrintf("UID %d is not expected UID %d", uid, expectedUid));
101 }
102}
103
Jeff Sharkeyec4fda22017-09-12 13:19:24 -0600104binder::Status checkArgumentId(const std::string& id) {
105 if (id.empty()) {
106 return exception(binder::Status::EX_ILLEGAL_ARGUMENT, "Missing ID");
107 }
108 for (const char& c : id) {
109 if (!std::isalnum(c) && c != ':' && c != ',') {
110 return exception(binder::Status::EX_ILLEGAL_ARGUMENT,
111 StringPrintf("ID %s is malformed", id.c_str()));
112 }
113 }
114 return ok();
115}
116
117binder::Status checkArgumentPath(const std::string& path) {
118 if (path.empty()) {
119 return exception(binder::Status::EX_ILLEGAL_ARGUMENT, "Missing path");
120 }
121 if (path[0] != '/') {
122 return exception(binder::Status::EX_ILLEGAL_ARGUMENT,
123 StringPrintf("Path %s is relative", path.c_str()));
124 }
Jeff Sharkey01a0e7f2017-10-17 16:06:32 -0600125 if ((path + '/').find("/../") != std::string::npos) {
126 return exception(binder::Status::EX_ILLEGAL_ARGUMENT,
127 StringPrintf("Path %s is shady", path.c_str()));
128 }
Jeff Sharkeyec4fda22017-09-12 13:19:24 -0600129 for (const char& c : path) {
130 if (c == '\0' || c == '\n') {
131 return exception(binder::Status::EX_ILLEGAL_ARGUMENT,
132 StringPrintf("Path %s is malformed", path.c_str()));
133 }
134 }
135 return ok();
136}
137
138binder::Status checkArgumentHex(const std::string& hex) {
139 // Empty hex strings are allowed
140 for (const char& c : hex) {
141 if (!std::isxdigit(c) && c != ':' && c != '-') {
142 return exception(binder::Status::EX_ILLEGAL_ARGUMENT,
143 StringPrintf("Hex %s is malformed", hex.c_str()));
144 }
145 }
146 return ok();
147}
148
Jeff Sharkey068c6be2017-09-06 13:47:40 -0600149#define ENFORCE_UID(uid) { \
150 binder::Status status = checkUid((uid)); \
151 if (!status.isOk()) { \
152 return status; \
153 } \
154}
155
Jeff Sharkeyec4fda22017-09-12 13:19:24 -0600156#define CHECK_ARGUMENT_ID(id) { \
157 binder::Status status = checkArgumentId((id)); \
158 if (!status.isOk()) { \
159 return status; \
160 } \
161}
162
163#define CHECK_ARGUMENT_PATH(path) { \
164 binder::Status status = checkArgumentPath((path)); \
165 if (!status.isOk()) { \
166 return status; \
167 } \
168}
169
170#define CHECK_ARGUMENT_HEX(hex) { \
171 binder::Status status = checkArgumentHex((hex)); \
172 if (!status.isOk()) { \
173 return status; \
174 } \
175}
176
Jeff Sharkey83b559c2017-09-12 16:30:52 -0600177#define ACQUIRE_LOCK \
Jeff Sharkey67b8c492017-09-21 17:08:43 -0600178 std::lock_guard<std::mutex> lock(VolumeManager::Instance()->getLock()); \
179 ATRACE_CALL();
Jeff Sharkey83b559c2017-09-12 16:30:52 -0600180
181#define ACQUIRE_CRYPT_LOCK \
Jeff Sharkey67b8c492017-09-21 17:08:43 -0600182 std::lock_guard<std::mutex> lock(VolumeManager::Instance()->getCryptLock()); \
183 ATRACE_CALL();
Jeff Sharkey9462bdd2017-09-07 15:27:28 -0600184
Jeff Sharkey068c6be2017-09-06 13:47:40 -0600185} // namespace
186
187status_t VoldNativeService::start() {
188 IPCThreadState::self()->disableBackgroundScheduling(true);
189 status_t ret = BinderService<VoldNativeService>::publish();
190 if (ret != android::OK) {
191 return ret;
192 }
193 sp<ProcessState> ps(ProcessState::self());
194 ps->startThreadPool();
195 ps->giveThreadPoolName();
196 return android::OK;
197}
198
199status_t VoldNativeService::dump(int fd, const Vector<String16> & /* args */) {
200 auto out = std::fstream(StringPrintf("/proc/self/fd/%d", fd));
201 const binder::Status dump_permission = checkPermission(kDump);
202 if (!dump_permission.isOk()) {
203 out << dump_permission.toString8() << endl;
204 return PERMISSION_DENIED;
205 }
206
Jeff Sharkey9462bdd2017-09-07 15:27:28 -0600207 ACQUIRE_LOCK;
Jeff Sharkey068c6be2017-09-06 13:47:40 -0600208 out << "vold is happy!" << endl;
209 out.flush();
Jeff Sharkey068c6be2017-09-06 13:47:40 -0600210 return NO_ERROR;
211}
212
Jeff Sharkey814e9d32017-09-13 11:49:44 -0600213binder::Status VoldNativeService::setListener(
214 const android::sp<android::os::IVoldListener>& listener) {
215 ENFORCE_UID(AID_SYSTEM);
216 ACQUIRE_LOCK;
217
218 VolumeManager::Instance()->setListener(listener);
219 return ok();
220}
221
Jeff Sharkeycbe69fc2017-09-15 16:50:28 -0600222binder::Status VoldNativeService::monitor() {
223 ENFORCE_UID(AID_SYSTEM);
224
225 // Simply acquire/release each lock for watchdog
226 {
227 ACQUIRE_LOCK;
228 }
229 {
230 ACQUIRE_CRYPT_LOCK;
231 }
232
233 return ok();
234}
235
Jeff Sharkey068c6be2017-09-06 13:47:40 -0600236binder::Status VoldNativeService::reset() {
237 ENFORCE_UID(AID_SYSTEM);
Jeff Sharkey9462bdd2017-09-07 15:27:28 -0600238 ACQUIRE_LOCK;
Jeff Sharkey068c6be2017-09-06 13:47:40 -0600239
Jeff Sharkey9462bdd2017-09-07 15:27:28 -0600240 return translate(VolumeManager::Instance()->reset());
241}
242
243binder::Status VoldNativeService::shutdown() {
244 ENFORCE_UID(AID_SYSTEM);
245 ACQUIRE_LOCK;
246
247 return translate(VolumeManager::Instance()->shutdown());
248}
249
Jeff Sharkey9462bdd2017-09-07 15:27:28 -0600250binder::Status VoldNativeService::onUserAdded(int32_t userId, int32_t userSerial) {
251 ENFORCE_UID(AID_SYSTEM);
252 ACQUIRE_LOCK;
253
254 return translate(VolumeManager::Instance()->onUserAdded(userId, userSerial));
255}
256
257binder::Status VoldNativeService::onUserRemoved(int32_t userId) {
258 ENFORCE_UID(AID_SYSTEM);
259 ACQUIRE_LOCK;
260
261 return translate(VolumeManager::Instance()->onUserRemoved(userId));
262}
263
264binder::Status VoldNativeService::onUserStarted(int32_t userId) {
265 ENFORCE_UID(AID_SYSTEM);
266 ACQUIRE_LOCK;
267
268 return translate(VolumeManager::Instance()->onUserStarted(userId));
269}
270
271binder::Status VoldNativeService::onUserStopped(int32_t userId) {
272 ENFORCE_UID(AID_SYSTEM);
273 ACQUIRE_LOCK;
274
275 return translate(VolumeManager::Instance()->onUserStopped(userId));
276}
277
Jeff Sharkey401b2602017-12-14 22:15:20 -0700278binder::Status VoldNativeService::onSecureKeyguardStateChanged(bool isShowing) {
279 ENFORCE_UID(AID_SYSTEM);
280 ACQUIRE_LOCK;
281
282 return translate(VolumeManager::Instance()->onSecureKeyguardStateChanged(isShowing));
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
Jeff Sharkey3ce18252017-10-24 11:08:45 -0600303binder::Status VoldNativeService::forgetPartition(const std::string& partGuid,
304 const std::string& fsUuid) {
Jeff Sharkey9462bdd2017-09-07 15:27:28 -0600305 ENFORCE_UID(AID_SYSTEM);
Jeff Sharkeyec4fda22017-09-12 13:19:24 -0600306 CHECK_ARGUMENT_HEX(partGuid);
Jeff Sharkey3ce18252017-10-24 11:08:45 -0600307 CHECK_ARGUMENT_HEX(fsUuid);
Jeff Sharkey9462bdd2017-09-07 15:27:28 -0600308 ACQUIRE_LOCK;
309
Jeff Sharkey3ce18252017-10-24 11:08:45 -0600310 return translate(VolumeManager::Instance()->forgetPartition(partGuid, fsUuid));
Jeff Sharkey9462bdd2017-09-07 15:27:28 -0600311}
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 Sharkey2048a282017-06-15 09:59:43 -0600358static binder::Status pathForVolId(const std::string& volId, std::string* path) {
Jeff Sharkey52f7a912017-09-15 12:57:44 -0600359 if (volId == "private" || volId == "null") {
Jeff Sharkey2048a282017-06-15 09:59:43 -0600360 *path = "/data";
Jeff Sharkey52f7a912017-09-15 12:57:44 -0600361 } else {
362 auto vol = VolumeManager::Instance()->findVolume(volId);
363 if (vol == nullptr) {
364 return error("Failed to find volume " + volId);
365 }
366 if (vol->getType() != VolumeBase::Type::kPrivate) {
367 return error("Volume " + volId + " not private");
368 }
369 if (vol->getState() != VolumeBase::State::kMounted) {
370 return error("Volume " + volId + " not mounted");
371 }
Jeff Sharkey2048a282017-06-15 09:59:43 -0600372 *path = vol->getPath();
373 if (path->empty()) {
374 return error("Volume " + volId + " missing path");
375 }
Jeff Sharkey52f7a912017-09-15 12:57:44 -0600376 }
Jeff Sharkey2048a282017-06-15 09:59:43 -0600377 return ok();
378}
Jeff Sharkey52f7a912017-09-15 12:57:44 -0600379
Jeff Sharkey2048a282017-06-15 09:59:43 -0600380binder::Status VoldNativeService::benchmark(
381 const std::string& volId, const android::sp<android::os::IVoldTaskListener>& listener) {
382 ENFORCE_UID(AID_SYSTEM);
383 CHECK_ARGUMENT_ID(volId);
384 ACQUIRE_LOCK;
385
386 std::string path;
387 auto status = pathForVolId(volId, &path);
388 if (!status.isOk()) return status;
Jeff Sharkey52f7a912017-09-15 12:57:44 -0600389
Jeff Sharkey01a0e7f2017-10-17 16:06:32 -0600390 std::thread([=]() {
391 android::vold::Benchmark(path, listener);
392 }).detach();
Jeff Sharkey068c6be2017-09-06 13:47:40 -0600393 return ok();
394}
395
Jeff Sharkey2048a282017-06-15 09:59:43 -0600396binder::Status VoldNativeService::checkEncryption(const std::string& volId) {
397 ENFORCE_UID(AID_SYSTEM);
398 CHECK_ARGUMENT_ID(volId);
399 ACQUIRE_LOCK;
400
401 std::string path;
402 auto status = pathForVolId(volId, &path);
403 if (!status.isOk()) return status;
404 return translate(android::vold::CheckEncryption(path));
405}
406
Jeff Sharkey11c2d382017-09-11 10:32:01 -0600407binder::Status VoldNativeService::moveStorage(const std::string& fromVolId,
Jeff Sharkey52f7a912017-09-15 12:57:44 -0600408 const std::string& toVolId, const android::sp<android::os::IVoldTaskListener>& listener) {
Jeff Sharkey9462bdd2017-09-07 15:27:28 -0600409 ENFORCE_UID(AID_SYSTEM);
Jeff Sharkeyec4fda22017-09-12 13:19:24 -0600410 CHECK_ARGUMENT_ID(fromVolId);
411 CHECK_ARGUMENT_ID(toVolId);
Jeff Sharkey9462bdd2017-09-07 15:27:28 -0600412 ACQUIRE_LOCK;
413
414 auto fromVol = VolumeManager::Instance()->findVolume(fromVolId);
415 auto toVol = VolumeManager::Instance()->findVolume(toVolId);
416 if (fromVol == nullptr) {
417 return error("Failed to find volume " + fromVolId);
418 } else if (toVol == nullptr) {
419 return error("Failed to find volume " + toVolId);
420 }
Jeff Sharkey01a0e7f2017-10-17 16:06:32 -0600421
422 std::thread([=]() {
423 android::vold::MoveStorage(fromVol, toVol, listener);
424 }).detach();
Jeff Sharkey9462bdd2017-09-07 15:27:28 -0600425 return ok();
426}
427
428binder::Status VoldNativeService::remountUid(int32_t uid, int32_t remountMode) {
429 ENFORCE_UID(AID_SYSTEM);
430 ACQUIRE_LOCK;
431
432 std::string tmp;
433 switch (remountMode) {
434 case REMOUNT_MODE_NONE: tmp = "none"; break;
435 case REMOUNT_MODE_DEFAULT: tmp = "default"; break;
436 case REMOUNT_MODE_READ: tmp = "read"; break;
437 case REMOUNT_MODE_WRITE: tmp = "write"; break;
438 default: return error("Unknown mode " + std::to_string(remountMode));
439 }
440 return translate(VolumeManager::Instance()->remountUid(uid, tmp));
441}
442
443binder::Status VoldNativeService::mkdirs(const std::string& path) {
444 ENFORCE_UID(AID_SYSTEM);
Jeff Sharkeyec4fda22017-09-12 13:19:24 -0600445 CHECK_ARGUMENT_PATH(path);
Jeff Sharkey9462bdd2017-09-07 15:27:28 -0600446 ACQUIRE_LOCK;
447
Jeff Sharkey3472e522017-10-06 18:02:53 -0600448 return translate(VolumeManager::Instance()->mkdirs(path));
Jeff Sharkey9462bdd2017-09-07 15:27:28 -0600449}
450
Jeff Sharkey11c2d382017-09-11 10:32:01 -0600451binder::Status VoldNativeService::createObb(const std::string& sourcePath,
452 const std::string& sourceKey, int32_t ownerGid, std::string* _aidl_return) {
453 ENFORCE_UID(AID_SYSTEM);
Jeff Sharkeyec4fda22017-09-12 13:19:24 -0600454 CHECK_ARGUMENT_PATH(sourcePath);
455 CHECK_ARGUMENT_HEX(sourceKey);
Jeff Sharkey11c2d382017-09-11 10:32:01 -0600456 ACQUIRE_LOCK;
457
458 return translate(
459 VolumeManager::Instance()->createObb(sourcePath, sourceKey, ownerGid, _aidl_return));
460}
461
462binder::Status VoldNativeService::destroyObb(const std::string& volId) {
463 ENFORCE_UID(AID_SYSTEM);
Jeff Sharkeyec4fda22017-09-12 13:19:24 -0600464 CHECK_ARGUMENT_ID(volId);
Jeff Sharkey11c2d382017-09-11 10:32:01 -0600465 ACQUIRE_LOCK;
466
467 return translate(VolumeManager::Instance()->destroyObb(volId));
468}
469
Jeff Sharkey52f7a912017-09-15 12:57:44 -0600470binder::Status VoldNativeService::fstrim(int32_t fstrimFlags,
471 const android::sp<android::os::IVoldTaskListener>& listener) {
Jeff Sharkey11c2d382017-09-11 10:32:01 -0600472 ENFORCE_UID(AID_SYSTEM);
473 ACQUIRE_LOCK;
474
Jeff Sharkey01a0e7f2017-10-17 16:06:32 -0600475 std::thread([=]() {
476 android::vold::Trim(listener);
477 }).detach();
Jeff Sharkey11c2d382017-09-11 10:32:01 -0600478 return ok();
479}
480
Jin Qiana370c142017-10-17 15:41:45 -0700481binder::Status VoldNativeService::runIdleMaint(
482 const android::sp<android::os::IVoldTaskListener>& listener) {
483 ENFORCE_UID(AID_SYSTEM);
484 ACQUIRE_LOCK;
485
486 std::thread([=]() {
487 android::vold::RunIdleMaint(listener);
488 }).detach();
489 return ok();
490}
491
492binder::Status VoldNativeService::abortIdleMaint(
493 const android::sp<android::os::IVoldTaskListener>& listener) {
494 ENFORCE_UID(AID_SYSTEM);
495 ACQUIRE_LOCK;
496
497 std::thread([=]() {
498 android::vold::AbortIdleMaint(listener);
499 }).detach();
500 return ok();
501}
502
Jeff Sharkey11c2d382017-09-11 10:32:01 -0600503binder::Status VoldNativeService::mountAppFuse(int32_t uid, int32_t pid, int32_t mountId,
504 android::base::unique_fd* _aidl_return) {
505 ENFORCE_UID(AID_SYSTEM);
506 ACQUIRE_LOCK;
507
508 return translate(VolumeManager::Instance()->mountAppFuse(uid, pid, mountId, _aidl_return));
509}
510
511binder::Status VoldNativeService::unmountAppFuse(int32_t uid, int32_t pid, int32_t mountId) {
512 ENFORCE_UID(AID_SYSTEM);
513 ACQUIRE_LOCK;
514
515 return translate(VolumeManager::Instance()->unmountAppFuse(uid, pid, mountId));
516}
517
Jeff Sharkey83b559c2017-09-12 16:30:52 -0600518binder::Status VoldNativeService::fdeCheckPassword(const std::string& password) {
519 ENFORCE_UID(AID_SYSTEM);
520 ACQUIRE_CRYPT_LOCK;
521
522 return translate(cryptfs_check_passwd(password.c_str()));
523}
524
525binder::Status VoldNativeService::fdeRestart() {
526 ENFORCE_UID(AID_SYSTEM);
527 ACQUIRE_CRYPT_LOCK;
528
529 // Spawn as thread so init can issue commands back to vold without
530 // causing deadlock, usually as a result of prep_data_fs.
531 std::thread(&cryptfs_restart).detach();
532 return ok();
533}
534
535binder::Status VoldNativeService::fdeComplete(int32_t* _aidl_return) {
536 ENFORCE_UID(AID_SYSTEM);
537 ACQUIRE_CRYPT_LOCK;
538
539 *_aidl_return = cryptfs_crypto_complete();
540 return ok();
541}
542
543static int fdeEnableInternal(int32_t passwordType, const std::string& password,
544 int32_t encryptionFlags) {
545 bool noUi = (encryptionFlags & VoldNativeService::ENCRYPTION_FLAG_NO_UI) != 0;
546
Jeff Sharkey83b559c2017-09-12 16:30:52 -0600547 for (int tries = 0; tries < 2; ++tries) {
548 int rc;
549 if (passwordType == VoldNativeService::PASSWORD_TYPE_DEFAULT) {
Paul Lawrence7ee87cf2017-12-22 10:12:06 -0800550 rc = cryptfs_enable_default(noUi);
Jeff Sharkey83b559c2017-09-12 16:30:52 -0600551 } else {
Paul Lawrence7ee87cf2017-12-22 10:12:06 -0800552 rc = cryptfs_enable(passwordType, password.c_str(), noUi);
Jeff Sharkey83b559c2017-09-12 16:30:52 -0600553 }
554
555 if (rc == 0) {
556 return 0;
557 } else if (tries == 0) {
Jeff Sharkey3472e522017-10-06 18:02:53 -0600558 KillProcessesWithOpenFiles(DATA_MNT_POINT, SIGKILL);
Jeff Sharkey83b559c2017-09-12 16:30:52 -0600559 }
560 }
561
562 return -1;
563}
564
565binder::Status VoldNativeService::fdeEnable(int32_t passwordType,
566 const std::string& password, int32_t encryptionFlags) {
567 ENFORCE_UID(AID_SYSTEM);
568 ACQUIRE_CRYPT_LOCK;
569
Paul Crowley0fd26262018-01-30 09:48:19 -0800570 LOG(DEBUG) << "fdeEnable(" << passwordType << ", *, " << encryptionFlags << ")";
Jeff Sharkey83b559c2017-09-12 16:30:52 -0600571 if (e4crypt_is_native()) {
Paul Crowley0fd26262018-01-30 09:48:19 -0800572 LOG(ERROR) << "e4crypt_is_native, fdeEnable invalid";
573 return error("e4crypt_is_native, fdeEnable invalid");
Jeff Sharkey83b559c2017-09-12 16:30:52 -0600574 }
Paul Crowley0fd26262018-01-30 09:48:19 -0800575 LOG(DEBUG) << "!e4crypt_is_native, spawning fdeEnableInternal";
Jeff Sharkey83b559c2017-09-12 16:30:52 -0600576
577 // Spawn as thread so init can issue commands back to vold without
578 // causing deadlock, usually as a result of prep_data_fs.
579 std::thread(&fdeEnableInternal, passwordType, password, encryptionFlags).detach();
580 return ok();
581}
582
583binder::Status VoldNativeService::fdeChangePassword(int32_t passwordType,
584 const std::string& password) {
585 ENFORCE_UID(AID_SYSTEM);
586 ACQUIRE_CRYPT_LOCK;
587
588 return translate(cryptfs_changepw(passwordType, password.c_str()));
589}
590
591binder::Status VoldNativeService::fdeVerifyPassword(const std::string& password) {
592 ENFORCE_UID(AID_SYSTEM);
593 ACQUIRE_CRYPT_LOCK;
594
595 return translate(cryptfs_verify_passwd(password.c_str()));
596}
597
598binder::Status VoldNativeService::fdeGetField(const std::string& key,
599 std::string* _aidl_return) {
600 ENFORCE_UID(AID_SYSTEM);
601 ACQUIRE_CRYPT_LOCK;
602
603 char buf[PROPERTY_VALUE_MAX];
604 if (cryptfs_getfield(key.c_str(), buf, sizeof(buf)) != CRYPTO_GETFIELD_OK) {
605 return error(StringPrintf("Failed to read field %s", key.c_str()));
606 } else {
607 *_aidl_return = buf;
608 return ok();
609 }
610}
611
612binder::Status VoldNativeService::fdeSetField(const std::string& key,
613 const std::string& value) {
614 ENFORCE_UID(AID_SYSTEM);
615 ACQUIRE_CRYPT_LOCK;
616
617 return translate(cryptfs_setfield(key.c_str(), value.c_str()));
618}
619
620binder::Status VoldNativeService::fdeGetPasswordType(int32_t* _aidl_return) {
621 ENFORCE_UID(AID_SYSTEM);
622 ACQUIRE_CRYPT_LOCK;
623
624 *_aidl_return = cryptfs_get_password_type();
625 return ok();
626}
627
628binder::Status VoldNativeService::fdeGetPassword(std::string* _aidl_return) {
629 ENFORCE_UID(AID_SYSTEM);
630 ACQUIRE_CRYPT_LOCK;
631
632 const char* res = cryptfs_get_password();
633 if (res != nullptr) {
634 *_aidl_return = res;
635 }
636 return ok();
637}
638
639binder::Status VoldNativeService::fdeClearPassword() {
640 ENFORCE_UID(AID_SYSTEM);
641 ACQUIRE_CRYPT_LOCK;
642
643 cryptfs_clear_password();
644 return ok();
645}
646
647binder::Status VoldNativeService::fbeEnable() {
648 ENFORCE_UID(AID_SYSTEM);
649 ACQUIRE_CRYPT_LOCK;
650
651 return translateBool(e4crypt_initialize_global_de());
652}
653
654binder::Status VoldNativeService::mountDefaultEncrypted() {
655 ENFORCE_UID(AID_SYSTEM);
656 ACQUIRE_CRYPT_LOCK;
657
Paul Crowley0fd26262018-01-30 09:48:19 -0800658 if (!e4crypt_is_native()) {
Jeff Sharkey83b559c2017-09-12 16:30:52 -0600659 // Spawn as thread so init can issue commands back to vold without
660 // causing deadlock, usually as a result of prep_data_fs.
661 std::thread(&cryptfs_mount_default_encrypted).detach();
Jeff Sharkey83b559c2017-09-12 16:30:52 -0600662 }
Paul Crowley0fd26262018-01-30 09:48:19 -0800663 return ok();
Jeff Sharkey83b559c2017-09-12 16:30:52 -0600664}
665
666binder::Status VoldNativeService::initUser0() {
667 ENFORCE_UID(AID_SYSTEM);
668 ACQUIRE_CRYPT_LOCK;
669
670 return translateBool(e4crypt_init_user0());
671}
672
673binder::Status VoldNativeService::isConvertibleToFbe(bool* _aidl_return) {
674 ENFORCE_UID(AID_SYSTEM);
675 ACQUIRE_CRYPT_LOCK;
676
677 *_aidl_return = cryptfs_isConvertibleToFBE() != 0;
678 return ok();
679}
680
Paul Crowley0fd26262018-01-30 09:48:19 -0800681binder::Status VoldNativeService::mountFstab(const std::string& mountPoint) {
682 ENFORCE_UID(AID_SYSTEM);
683 ACQUIRE_LOCK;
684
685 return translateBool(e4crypt_mount_metadata_encrypted(mountPoint, false));
686}
687
688binder::Status VoldNativeService::encryptFstab(const std::string& mountPoint) {
689 ENFORCE_UID(AID_SYSTEM);
690 ACQUIRE_LOCK;
691
692 return translateBool(e4crypt_mount_metadata_encrypted(mountPoint, true));
693}
694
Jeff Sharkey83b559c2017-09-12 16:30:52 -0600695binder::Status VoldNativeService::createUserKey(int32_t userId, int32_t userSerial,
696 bool ephemeral) {
697 ENFORCE_UID(AID_SYSTEM);
698 ACQUIRE_CRYPT_LOCK;
699
700 return translateBool(e4crypt_vold_create_user_key(userId, userSerial, ephemeral));
701}
702
703binder::Status VoldNativeService::destroyUserKey(int32_t userId) {
704 ENFORCE_UID(AID_SYSTEM);
705 ACQUIRE_CRYPT_LOCK;
706
707 return translateBool(e4crypt_destroy_user_key(userId));
708}
709
710binder::Status VoldNativeService::addUserKeyAuth(int32_t userId, int32_t userSerial,
711 const std::string& token, const std::string& secret) {
712 ENFORCE_UID(AID_SYSTEM);
713 ACQUIRE_CRYPT_LOCK;
714
Paul Crowley3b71fc52017-10-09 10:55:21 -0700715 return translateBool(e4crypt_add_user_key_auth(userId, userSerial, token, secret));
Jeff Sharkey83b559c2017-09-12 16:30:52 -0600716}
717
718binder::Status VoldNativeService::fixateNewestUserKeyAuth(int32_t userId) {
719 ENFORCE_UID(AID_SYSTEM);
720 ACQUIRE_CRYPT_LOCK;
721
722 return translateBool(e4crypt_fixate_newest_user_key_auth(userId));
723}
724
725binder::Status VoldNativeService::unlockUserKey(int32_t userId, int32_t userSerial,
726 const std::string& token, const std::string& secret) {
727 ENFORCE_UID(AID_SYSTEM);
728 ACQUIRE_CRYPT_LOCK;
729
Paul Crowley3b71fc52017-10-09 10:55:21 -0700730 return translateBool(e4crypt_unlock_user_key(userId, userSerial, token, secret));
Jeff Sharkey83b559c2017-09-12 16:30:52 -0600731}
732
733binder::Status VoldNativeService::lockUserKey(int32_t userId) {
734 ENFORCE_UID(AID_SYSTEM);
735 ACQUIRE_CRYPT_LOCK;
736
737 return translateBool(e4crypt_lock_user_key(userId));
738}
739
740binder::Status VoldNativeService::prepareUserStorage(const std::unique_ptr<std::string>& uuid,
741 int32_t userId, int32_t userSerial, int32_t flags) {
742 ENFORCE_UID(AID_SYSTEM);
Paul Crowley3b71fc52017-10-09 10:55:21 -0700743 std::string empty_string = "";
744 auto uuid_ = uuid ? *uuid : empty_string;
Paul Crowley06f762d2017-10-16 10:59:51 -0700745 CHECK_ARGUMENT_HEX(uuid_);
746
747 ACQUIRE_CRYPT_LOCK;
Jeff Sharkey83b559c2017-09-12 16:30:52 -0600748 return translateBool(e4crypt_prepare_user_storage(uuid_, userId, userSerial, flags));
749}
750
751binder::Status VoldNativeService::destroyUserStorage(const std::unique_ptr<std::string>& uuid,
752 int32_t userId, int32_t flags) {
753 ENFORCE_UID(AID_SYSTEM);
Paul Crowley3b71fc52017-10-09 10:55:21 -0700754 std::string empty_string = "";
755 auto uuid_ = uuid ? *uuid : empty_string;
Paul Crowley06f762d2017-10-16 10:59:51 -0700756 CHECK_ARGUMENT_HEX(uuid_);
757
758 ACQUIRE_CRYPT_LOCK;
Jeff Sharkey83b559c2017-09-12 16:30:52 -0600759 return translateBool(e4crypt_destroy_user_storage(uuid_, userId, flags));
760}
761
Jeff Sharkey068c6be2017-09-06 13:47:40 -0600762} // namespace vold
763} // namespace android