blob: e8e151f908c5e8c1e5e835c9ee6822e4a29bdf03 [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 Sharkey11c2d382017-09-11 10:32:01 -0600250binder::Status VoldNativeService::mountAll() {
Jeff Sharkey9462bdd2017-09-07 15:27:28 -0600251 ENFORCE_UID(AID_SYSTEM);
252 ACQUIRE_LOCK;
253
Jeff Sharkey11c2d382017-09-11 10:32:01 -0600254 struct fstab* fstab = fs_mgr_read_fstab_default();
255 int res = fs_mgr_mount_all(fstab, MOUNT_MODE_DEFAULT);
256 fs_mgr_free_fstab(fstab);
257 return translate(res);
Jeff Sharkey9462bdd2017-09-07 15:27:28 -0600258}
259
260binder::Status VoldNativeService::onUserAdded(int32_t userId, int32_t userSerial) {
261 ENFORCE_UID(AID_SYSTEM);
262 ACQUIRE_LOCK;
263
264 return translate(VolumeManager::Instance()->onUserAdded(userId, userSerial));
265}
266
267binder::Status VoldNativeService::onUserRemoved(int32_t userId) {
268 ENFORCE_UID(AID_SYSTEM);
269 ACQUIRE_LOCK;
270
271 return translate(VolumeManager::Instance()->onUserRemoved(userId));
272}
273
274binder::Status VoldNativeService::onUserStarted(int32_t userId) {
275 ENFORCE_UID(AID_SYSTEM);
276 ACQUIRE_LOCK;
277
278 return translate(VolumeManager::Instance()->onUserStarted(userId));
279}
280
281binder::Status VoldNativeService::onUserStopped(int32_t userId) {
282 ENFORCE_UID(AID_SYSTEM);
283 ACQUIRE_LOCK;
284
285 return translate(VolumeManager::Instance()->onUserStopped(userId));
286}
287
Jeff Sharkey11c2d382017-09-11 10:32:01 -0600288binder::Status VoldNativeService::partition(const std::string& diskId, int32_t partitionType,
289 int32_t ratio) {
Jeff Sharkey9462bdd2017-09-07 15:27:28 -0600290 ENFORCE_UID(AID_SYSTEM);
Jeff Sharkeyec4fda22017-09-12 13:19:24 -0600291 CHECK_ARGUMENT_ID(diskId);
Jeff Sharkey9462bdd2017-09-07 15:27:28 -0600292 ACQUIRE_LOCK;
293
294 auto disk = VolumeManager::Instance()->findDisk(diskId);
295 if (disk == nullptr) {
296 return error("Failed to find disk " + diskId);
297 }
298 switch (partitionType) {
299 case PARTITION_TYPE_PUBLIC: return translate(disk->partitionPublic());
300 case PARTITION_TYPE_PRIVATE: return translate(disk->partitionPrivate());
301 case PARTITION_TYPE_MIXED: return translate(disk->partitionMixed(ratio));
302 default: return error("Unknown type " + std::to_string(partitionType));
303 }
304}
305
Jeff Sharkey3ce18252017-10-24 11:08:45 -0600306binder::Status VoldNativeService::forgetPartition(const std::string& partGuid,
307 const std::string& fsUuid) {
Jeff Sharkey9462bdd2017-09-07 15:27:28 -0600308 ENFORCE_UID(AID_SYSTEM);
Jeff Sharkeyec4fda22017-09-12 13:19:24 -0600309 CHECK_ARGUMENT_HEX(partGuid);
Jeff Sharkey3ce18252017-10-24 11:08:45 -0600310 CHECK_ARGUMENT_HEX(fsUuid);
Jeff Sharkey9462bdd2017-09-07 15:27:28 -0600311 ACQUIRE_LOCK;
312
Jeff Sharkey3ce18252017-10-24 11:08:45 -0600313 return translate(VolumeManager::Instance()->forgetPartition(partGuid, fsUuid));
Jeff Sharkey9462bdd2017-09-07 15:27:28 -0600314}
315
Jeff Sharkey11c2d382017-09-11 10:32:01 -0600316binder::Status VoldNativeService::mount(const std::string& volId, int32_t mountFlags,
317 int32_t mountUserId) {
Jeff Sharkey9462bdd2017-09-07 15:27:28 -0600318 ENFORCE_UID(AID_SYSTEM);
Jeff Sharkeyec4fda22017-09-12 13:19:24 -0600319 CHECK_ARGUMENT_ID(volId);
Jeff Sharkey9462bdd2017-09-07 15:27:28 -0600320 ACQUIRE_LOCK;
321
322 auto vol = VolumeManager::Instance()->findVolume(volId);
323 if (vol == nullptr) {
324 return error("Failed to find volume " + volId);
325 }
326
327 vol->setMountFlags(mountFlags);
328 vol->setMountUserId(mountUserId);
329
330 int res = vol->mount();
Jeff Sharkey83b559c2017-09-12 16:30:52 -0600331 if ((mountFlags & MOUNT_FLAG_PRIMARY) != 0) {
Jeff Sharkey9462bdd2017-09-07 15:27:28 -0600332 VolumeManager::Instance()->setPrimary(vol);
333 }
334 return translate(res);
335}
336
337binder::Status VoldNativeService::unmount(const std::string& volId) {
338 ENFORCE_UID(AID_SYSTEM);
Jeff Sharkeyec4fda22017-09-12 13:19:24 -0600339 CHECK_ARGUMENT_ID(volId);
Jeff Sharkey9462bdd2017-09-07 15:27:28 -0600340 ACQUIRE_LOCK;
341
342 auto vol = VolumeManager::Instance()->findVolume(volId);
343 if (vol == nullptr) {
344 return error("Failed to find volume " + volId);
345 }
346 return translate(vol->unmount());
347}
348
349binder::Status VoldNativeService::format(const std::string& volId, const std::string& fsType) {
350 ENFORCE_UID(AID_SYSTEM);
Jeff Sharkeyec4fda22017-09-12 13:19:24 -0600351 CHECK_ARGUMENT_ID(volId);
Jeff Sharkey9462bdd2017-09-07 15:27:28 -0600352 ACQUIRE_LOCK;
353
354 auto vol = VolumeManager::Instance()->findVolume(volId);
355 if (vol == nullptr) {
356 return error("Failed to find volume " + volId);
357 }
358 return translate(vol->format(fsType));
359}
360
Jeff Sharkey2048a282017-06-15 09:59:43 -0600361static binder::Status pathForVolId(const std::string& volId, std::string* path) {
Jeff Sharkey52f7a912017-09-15 12:57:44 -0600362 if (volId == "private" || volId == "null") {
Jeff Sharkey2048a282017-06-15 09:59:43 -0600363 *path = "/data";
Jeff Sharkey52f7a912017-09-15 12:57:44 -0600364 } else {
365 auto vol = VolumeManager::Instance()->findVolume(volId);
366 if (vol == nullptr) {
367 return error("Failed to find volume " + volId);
368 }
369 if (vol->getType() != VolumeBase::Type::kPrivate) {
370 return error("Volume " + volId + " not private");
371 }
372 if (vol->getState() != VolumeBase::State::kMounted) {
373 return error("Volume " + volId + " not mounted");
374 }
Jeff Sharkey2048a282017-06-15 09:59:43 -0600375 *path = vol->getPath();
376 if (path->empty()) {
377 return error("Volume " + volId + " missing path");
378 }
Jeff Sharkey52f7a912017-09-15 12:57:44 -0600379 }
Jeff Sharkey2048a282017-06-15 09:59:43 -0600380 return ok();
381}
Jeff Sharkey52f7a912017-09-15 12:57:44 -0600382
Jeff Sharkey2048a282017-06-15 09:59:43 -0600383binder::Status VoldNativeService::benchmark(
384 const std::string& volId, const android::sp<android::os::IVoldTaskListener>& listener) {
385 ENFORCE_UID(AID_SYSTEM);
386 CHECK_ARGUMENT_ID(volId);
387 ACQUIRE_LOCK;
388
389 std::string path;
390 auto status = pathForVolId(volId, &path);
391 if (!status.isOk()) return status;
Jeff Sharkey52f7a912017-09-15 12:57:44 -0600392
Jeff Sharkey01a0e7f2017-10-17 16:06:32 -0600393 std::thread([=]() {
394 android::vold::Benchmark(path, listener);
395 }).detach();
Jeff Sharkey068c6be2017-09-06 13:47:40 -0600396 return ok();
397}
398
Jeff Sharkey2048a282017-06-15 09:59:43 -0600399binder::Status VoldNativeService::checkEncryption(const std::string& volId) {
400 ENFORCE_UID(AID_SYSTEM);
401 CHECK_ARGUMENT_ID(volId);
402 ACQUIRE_LOCK;
403
404 std::string path;
405 auto status = pathForVolId(volId, &path);
406 if (!status.isOk()) return status;
407 return translate(android::vold::CheckEncryption(path));
408}
409
Jeff Sharkey11c2d382017-09-11 10:32:01 -0600410binder::Status VoldNativeService::moveStorage(const std::string& fromVolId,
Jeff Sharkey52f7a912017-09-15 12:57:44 -0600411 const std::string& toVolId, const android::sp<android::os::IVoldTaskListener>& listener) {
Jeff Sharkey9462bdd2017-09-07 15:27:28 -0600412 ENFORCE_UID(AID_SYSTEM);
Jeff Sharkeyec4fda22017-09-12 13:19:24 -0600413 CHECK_ARGUMENT_ID(fromVolId);
414 CHECK_ARGUMENT_ID(toVolId);
Jeff Sharkey9462bdd2017-09-07 15:27:28 -0600415 ACQUIRE_LOCK;
416
417 auto fromVol = VolumeManager::Instance()->findVolume(fromVolId);
418 auto toVol = VolumeManager::Instance()->findVolume(toVolId);
419 if (fromVol == nullptr) {
420 return error("Failed to find volume " + fromVolId);
421 } else if (toVol == nullptr) {
422 return error("Failed to find volume " + toVolId);
423 }
Jeff Sharkey01a0e7f2017-10-17 16:06:32 -0600424
425 std::thread([=]() {
426 android::vold::MoveStorage(fromVol, toVol, listener);
427 }).detach();
Jeff Sharkey9462bdd2017-09-07 15:27:28 -0600428 return ok();
429}
430
431binder::Status VoldNativeService::remountUid(int32_t uid, int32_t remountMode) {
432 ENFORCE_UID(AID_SYSTEM);
433 ACQUIRE_LOCK;
434
435 std::string tmp;
436 switch (remountMode) {
437 case REMOUNT_MODE_NONE: tmp = "none"; break;
438 case REMOUNT_MODE_DEFAULT: tmp = "default"; break;
439 case REMOUNT_MODE_READ: tmp = "read"; break;
440 case REMOUNT_MODE_WRITE: tmp = "write"; break;
441 default: return error("Unknown mode " + std::to_string(remountMode));
442 }
443 return translate(VolumeManager::Instance()->remountUid(uid, tmp));
444}
445
446binder::Status VoldNativeService::mkdirs(const std::string& path) {
447 ENFORCE_UID(AID_SYSTEM);
Jeff Sharkeyec4fda22017-09-12 13:19:24 -0600448 CHECK_ARGUMENT_PATH(path);
Jeff Sharkey9462bdd2017-09-07 15:27:28 -0600449 ACQUIRE_LOCK;
450
Jeff Sharkey3472e522017-10-06 18:02:53 -0600451 return translate(VolumeManager::Instance()->mkdirs(path));
Jeff Sharkey9462bdd2017-09-07 15:27:28 -0600452}
453
Jeff Sharkey11c2d382017-09-11 10:32:01 -0600454binder::Status VoldNativeService::createObb(const std::string& sourcePath,
455 const std::string& sourceKey, int32_t ownerGid, std::string* _aidl_return) {
456 ENFORCE_UID(AID_SYSTEM);
Jeff Sharkeyec4fda22017-09-12 13:19:24 -0600457 CHECK_ARGUMENT_PATH(sourcePath);
458 CHECK_ARGUMENT_HEX(sourceKey);
Jeff Sharkey11c2d382017-09-11 10:32:01 -0600459 ACQUIRE_LOCK;
460
461 return translate(
462 VolumeManager::Instance()->createObb(sourcePath, sourceKey, ownerGid, _aidl_return));
463}
464
465binder::Status VoldNativeService::destroyObb(const std::string& volId) {
466 ENFORCE_UID(AID_SYSTEM);
Jeff Sharkeyec4fda22017-09-12 13:19:24 -0600467 CHECK_ARGUMENT_ID(volId);
Jeff Sharkey11c2d382017-09-11 10:32:01 -0600468 ACQUIRE_LOCK;
469
470 return translate(VolumeManager::Instance()->destroyObb(volId));
471}
472
Jeff Sharkey52f7a912017-09-15 12:57:44 -0600473binder::Status VoldNativeService::fstrim(int32_t fstrimFlags,
474 const android::sp<android::os::IVoldTaskListener>& listener) {
Jeff Sharkey11c2d382017-09-11 10:32:01 -0600475 ENFORCE_UID(AID_SYSTEM);
476 ACQUIRE_LOCK;
477
Jeff Sharkey01a0e7f2017-10-17 16:06:32 -0600478 std::thread([=]() {
479 android::vold::Trim(listener);
480 }).detach();
Jeff Sharkey11c2d382017-09-11 10:32:01 -0600481 return ok();
482}
483
Jin Qiana370c142017-10-17 15:41:45 -0700484binder::Status VoldNativeService::runIdleMaint(
485 const android::sp<android::os::IVoldTaskListener>& listener) {
486 ENFORCE_UID(AID_SYSTEM);
487 ACQUIRE_LOCK;
488
489 std::thread([=]() {
490 android::vold::RunIdleMaint(listener);
491 }).detach();
492 return ok();
493}
494
495binder::Status VoldNativeService::abortIdleMaint(
496 const android::sp<android::os::IVoldTaskListener>& listener) {
497 ENFORCE_UID(AID_SYSTEM);
498 ACQUIRE_LOCK;
499
500 std::thread([=]() {
501 android::vold::AbortIdleMaint(listener);
502 }).detach();
503 return ok();
504}
505
Jeff Sharkey11c2d382017-09-11 10:32:01 -0600506binder::Status VoldNativeService::mountAppFuse(int32_t uid, int32_t pid, int32_t mountId,
507 android::base::unique_fd* _aidl_return) {
508 ENFORCE_UID(AID_SYSTEM);
509 ACQUIRE_LOCK;
510
511 return translate(VolumeManager::Instance()->mountAppFuse(uid, pid, mountId, _aidl_return));
512}
513
514binder::Status VoldNativeService::unmountAppFuse(int32_t uid, int32_t pid, int32_t mountId) {
515 ENFORCE_UID(AID_SYSTEM);
516 ACQUIRE_LOCK;
517
518 return translate(VolumeManager::Instance()->unmountAppFuse(uid, pid, mountId));
519}
520
Jeff Sharkey83b559c2017-09-12 16:30:52 -0600521binder::Status VoldNativeService::fdeCheckPassword(const std::string& password) {
522 ENFORCE_UID(AID_SYSTEM);
523 ACQUIRE_CRYPT_LOCK;
524
525 return translate(cryptfs_check_passwd(password.c_str()));
526}
527
528binder::Status VoldNativeService::fdeRestart() {
529 ENFORCE_UID(AID_SYSTEM);
530 ACQUIRE_CRYPT_LOCK;
531
532 // Spawn as thread so init can issue commands back to vold without
533 // causing deadlock, usually as a result of prep_data_fs.
534 std::thread(&cryptfs_restart).detach();
535 return ok();
536}
537
538binder::Status VoldNativeService::fdeComplete(int32_t* _aidl_return) {
539 ENFORCE_UID(AID_SYSTEM);
540 ACQUIRE_CRYPT_LOCK;
541
542 *_aidl_return = cryptfs_crypto_complete();
543 return ok();
544}
545
546static int fdeEnableInternal(int32_t passwordType, const std::string& password,
547 int32_t encryptionFlags) {
548 bool noUi = (encryptionFlags & VoldNativeService::ENCRYPTION_FLAG_NO_UI) != 0;
549
550 std::string how;
551 if ((encryptionFlags & VoldNativeService::ENCRYPTION_FLAG_IN_PLACE) != 0) {
552 how = "inplace";
553 } else if ((encryptionFlags & VoldNativeService::ENCRYPTION_FLAG_WIPE) != 0) {
554 how = "wipe";
555 } else {
556 LOG(ERROR) << "Missing encryption flag";
557 return -1;
558 }
559
560 for (int tries = 0; tries < 2; ++tries) {
561 int rc;
562 if (passwordType == VoldNativeService::PASSWORD_TYPE_DEFAULT) {
563 rc = cryptfs_enable_default(how.c_str(), noUi);
564 } else {
565 rc = cryptfs_enable(how.c_str(), passwordType, password.c_str(), noUi);
566 }
567
568 if (rc == 0) {
569 return 0;
570 } else if (tries == 0) {
Jeff Sharkey3472e522017-10-06 18:02:53 -0600571 KillProcessesWithOpenFiles(DATA_MNT_POINT, SIGKILL);
Jeff Sharkey83b559c2017-09-12 16:30:52 -0600572 }
573 }
574
575 return -1;
576}
577
578binder::Status VoldNativeService::fdeEnable(int32_t passwordType,
579 const std::string& password, int32_t encryptionFlags) {
580 ENFORCE_UID(AID_SYSTEM);
581 ACQUIRE_CRYPT_LOCK;
582
583 if (e4crypt_is_native()) {
584 if (passwordType != PASSWORD_TYPE_DEFAULT) {
585 return error("Unexpected password type");
586 }
587 if (encryptionFlags != (ENCRYPTION_FLAG_IN_PLACE | ENCRYPTION_FLAG_NO_UI)) {
588 return error("Unexpected flags");
589 }
590 return translateBool(e4crypt_enable_crypto());
591 }
592
593 // Spawn as thread so init can issue commands back to vold without
594 // causing deadlock, usually as a result of prep_data_fs.
595 std::thread(&fdeEnableInternal, passwordType, password, encryptionFlags).detach();
596 return ok();
597}
598
599binder::Status VoldNativeService::fdeChangePassword(int32_t passwordType,
600 const std::string& password) {
601 ENFORCE_UID(AID_SYSTEM);
602 ACQUIRE_CRYPT_LOCK;
603
604 return translate(cryptfs_changepw(passwordType, password.c_str()));
605}
606
607binder::Status VoldNativeService::fdeVerifyPassword(const std::string& password) {
608 ENFORCE_UID(AID_SYSTEM);
609 ACQUIRE_CRYPT_LOCK;
610
611 return translate(cryptfs_verify_passwd(password.c_str()));
612}
613
614binder::Status VoldNativeService::fdeGetField(const std::string& key,
615 std::string* _aidl_return) {
616 ENFORCE_UID(AID_SYSTEM);
617 ACQUIRE_CRYPT_LOCK;
618
619 char buf[PROPERTY_VALUE_MAX];
620 if (cryptfs_getfield(key.c_str(), buf, sizeof(buf)) != CRYPTO_GETFIELD_OK) {
621 return error(StringPrintf("Failed to read field %s", key.c_str()));
622 } else {
623 *_aidl_return = buf;
624 return ok();
625 }
626}
627
628binder::Status VoldNativeService::fdeSetField(const std::string& key,
629 const std::string& value) {
630 ENFORCE_UID(AID_SYSTEM);
631 ACQUIRE_CRYPT_LOCK;
632
633 return translate(cryptfs_setfield(key.c_str(), value.c_str()));
634}
635
636binder::Status VoldNativeService::fdeGetPasswordType(int32_t* _aidl_return) {
637 ENFORCE_UID(AID_SYSTEM);
638 ACQUIRE_CRYPT_LOCK;
639
640 *_aidl_return = cryptfs_get_password_type();
641 return ok();
642}
643
644binder::Status VoldNativeService::fdeGetPassword(std::string* _aidl_return) {
645 ENFORCE_UID(AID_SYSTEM);
646 ACQUIRE_CRYPT_LOCK;
647
648 const char* res = cryptfs_get_password();
649 if (res != nullptr) {
650 *_aidl_return = res;
651 }
652 return ok();
653}
654
655binder::Status VoldNativeService::fdeClearPassword() {
656 ENFORCE_UID(AID_SYSTEM);
657 ACQUIRE_CRYPT_LOCK;
658
659 cryptfs_clear_password();
660 return ok();
661}
662
663binder::Status VoldNativeService::fbeEnable() {
664 ENFORCE_UID(AID_SYSTEM);
665 ACQUIRE_CRYPT_LOCK;
666
667 return translateBool(e4crypt_initialize_global_de());
668}
669
670binder::Status VoldNativeService::mountDefaultEncrypted() {
671 ENFORCE_UID(AID_SYSTEM);
672 ACQUIRE_CRYPT_LOCK;
673
674 if (e4crypt_is_native()) {
675 return translateBool(e4crypt_mount_metadata_encrypted());
676 } else {
677 // Spawn as thread so init can issue commands back to vold without
678 // causing deadlock, usually as a result of prep_data_fs.
679 std::thread(&cryptfs_mount_default_encrypted).detach();
680 return ok();
681 }
682}
683
684binder::Status VoldNativeService::initUser0() {
685 ENFORCE_UID(AID_SYSTEM);
686 ACQUIRE_CRYPT_LOCK;
687
688 return translateBool(e4crypt_init_user0());
689}
690
691binder::Status VoldNativeService::isConvertibleToFbe(bool* _aidl_return) {
692 ENFORCE_UID(AID_SYSTEM);
693 ACQUIRE_CRYPT_LOCK;
694
695 *_aidl_return = cryptfs_isConvertibleToFBE() != 0;
696 return ok();
697}
698
699binder::Status VoldNativeService::createUserKey(int32_t userId, int32_t userSerial,
700 bool ephemeral) {
701 ENFORCE_UID(AID_SYSTEM);
702 ACQUIRE_CRYPT_LOCK;
703
704 return translateBool(e4crypt_vold_create_user_key(userId, userSerial, ephemeral));
705}
706
707binder::Status VoldNativeService::destroyUserKey(int32_t userId) {
708 ENFORCE_UID(AID_SYSTEM);
709 ACQUIRE_CRYPT_LOCK;
710
711 return translateBool(e4crypt_destroy_user_key(userId));
712}
713
714binder::Status VoldNativeService::addUserKeyAuth(int32_t userId, int32_t userSerial,
715 const std::string& token, const std::string& secret) {
716 ENFORCE_UID(AID_SYSTEM);
717 ACQUIRE_CRYPT_LOCK;
718
Paul Crowley3b71fc52017-10-09 10:55:21 -0700719 return translateBool(e4crypt_add_user_key_auth(userId, userSerial, token, secret));
Jeff Sharkey83b559c2017-09-12 16:30:52 -0600720}
721
722binder::Status VoldNativeService::fixateNewestUserKeyAuth(int32_t userId) {
723 ENFORCE_UID(AID_SYSTEM);
724 ACQUIRE_CRYPT_LOCK;
725
726 return translateBool(e4crypt_fixate_newest_user_key_auth(userId));
727}
728
729binder::Status VoldNativeService::unlockUserKey(int32_t userId, int32_t userSerial,
730 const std::string& token, const std::string& secret) {
731 ENFORCE_UID(AID_SYSTEM);
732 ACQUIRE_CRYPT_LOCK;
733
Paul Crowley3b71fc52017-10-09 10:55:21 -0700734 return translateBool(e4crypt_unlock_user_key(userId, userSerial, token, secret));
Jeff Sharkey83b559c2017-09-12 16:30:52 -0600735}
736
737binder::Status VoldNativeService::lockUserKey(int32_t userId) {
738 ENFORCE_UID(AID_SYSTEM);
739 ACQUIRE_CRYPT_LOCK;
740
741 return translateBool(e4crypt_lock_user_key(userId));
742}
743
744binder::Status VoldNativeService::prepareUserStorage(const std::unique_ptr<std::string>& uuid,
745 int32_t userId, int32_t userSerial, int32_t flags) {
746 ENFORCE_UID(AID_SYSTEM);
Paul Crowley3b71fc52017-10-09 10:55:21 -0700747 std::string empty_string = "";
748 auto uuid_ = uuid ? *uuid : empty_string;
Paul Crowley06f762d2017-10-16 10:59:51 -0700749 CHECK_ARGUMENT_HEX(uuid_);
750
751 ACQUIRE_CRYPT_LOCK;
Jeff Sharkey83b559c2017-09-12 16:30:52 -0600752 return translateBool(e4crypt_prepare_user_storage(uuid_, userId, userSerial, flags));
753}
754
755binder::Status VoldNativeService::destroyUserStorage(const std::unique_ptr<std::string>& uuid,
756 int32_t userId, int32_t flags) {
757 ENFORCE_UID(AID_SYSTEM);
Paul Crowley3b71fc52017-10-09 10:55:21 -0700758 std::string empty_string = "";
759 auto uuid_ = uuid ? *uuid : empty_string;
Paul Crowley06f762d2017-10-16 10:59:51 -0700760 CHECK_ARGUMENT_HEX(uuid_);
761
762 ACQUIRE_CRYPT_LOCK;
Jeff Sharkey83b559c2017-09-12 16:30:52 -0600763 return translateBool(e4crypt_destroy_user_storage(uuid_, userId, flags));
764}
765
766binder::Status VoldNativeService::secdiscard(const std::string& path) {
767 ENFORCE_UID(AID_SYSTEM);
Jeff Sharkey01a0e7f2017-10-17 16:06:32 -0600768 CHECK_ARGUMENT_PATH(path);
Jeff Sharkey83b559c2017-09-12 16:30:52 -0600769 ACQUIRE_CRYPT_LOCK;
770
Paul Crowley3b71fc52017-10-09 10:55:21 -0700771 return translateBool(e4crypt_secdiscard(path));
Jeff Sharkey83b559c2017-09-12 16:30:52 -0600772}
773
Jeff Sharkey068c6be2017-09-06 13:47:40 -0600774} // namespace vold
775} // namespace android