blob: 6e3317bda42fc6f50b4274e27d65579c37e959fa [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
Sudheer Shankad484aa92018-07-31 10:07:34 -0700250// TODO: sanity-check these string arguments
Jeff Sharkey9462bdd2017-09-07 15:27:28 -0600251binder::Status VoldNativeService::onUserAdded(int32_t userId, int32_t userSerial) {
252 ENFORCE_UID(AID_SYSTEM);
253 ACQUIRE_LOCK;
254
255 return translate(VolumeManager::Instance()->onUserAdded(userId, userSerial));
256}
257
258binder::Status VoldNativeService::onUserRemoved(int32_t userId) {
259 ENFORCE_UID(AID_SYSTEM);
260 ACQUIRE_LOCK;
261
262 return translate(VolumeManager::Instance()->onUserRemoved(userId));
263}
264
265binder::Status VoldNativeService::onUserStarted(int32_t userId) {
266 ENFORCE_UID(AID_SYSTEM);
267 ACQUIRE_LOCK;
268
269 return translate(VolumeManager::Instance()->onUserStarted(userId));
270}
271
272binder::Status VoldNativeService::onUserStopped(int32_t userId) {
273 ENFORCE_UID(AID_SYSTEM);
274 ACQUIRE_LOCK;
275
276 return translate(VolumeManager::Instance()->onUserStopped(userId));
277}
278
Sudheer Shankad484aa92018-07-31 10:07:34 -0700279// TODO: sanity-check these string arguments
280binder::Status VoldNativeService::addAppIds(const std::vector<std::string>& packageNames,
281 const std::vector<int32_t>& appIds) {
282 ENFORCE_UID(AID_SYSTEM);
283 ACQUIRE_LOCK;
284
285 return translate(VolumeManager::Instance()->addAppIds(packageNames, appIds));
286}
287
288// TODO: sanity-check these string arguments
289binder::Status VoldNativeService::addSandboxIds(const std::vector<int32_t>& appIds,
290 const std::vector<std::string>& sandboxIds) {
291 ENFORCE_UID(AID_SYSTEM);
292 ACQUIRE_LOCK;
293
294 return translate(VolumeManager::Instance()->addSandboxIds(appIds, sandboxIds));
295}
296
Jeff Sharkey401b2602017-12-14 22:15:20 -0700297binder::Status VoldNativeService::onSecureKeyguardStateChanged(bool isShowing) {
298 ENFORCE_UID(AID_SYSTEM);
299 ACQUIRE_LOCK;
300
301 return translate(VolumeManager::Instance()->onSecureKeyguardStateChanged(isShowing));
302}
303
Jeff Sharkey11c2d382017-09-11 10:32:01 -0600304binder::Status VoldNativeService::partition(const std::string& diskId, int32_t partitionType,
305 int32_t ratio) {
Jeff Sharkey9462bdd2017-09-07 15:27:28 -0600306 ENFORCE_UID(AID_SYSTEM);
Jeff Sharkeyec4fda22017-09-12 13:19:24 -0600307 CHECK_ARGUMENT_ID(diskId);
Jeff Sharkey9462bdd2017-09-07 15:27:28 -0600308 ACQUIRE_LOCK;
309
310 auto disk = VolumeManager::Instance()->findDisk(diskId);
311 if (disk == nullptr) {
312 return error("Failed to find disk " + diskId);
313 }
314 switch (partitionType) {
315 case PARTITION_TYPE_PUBLIC: return translate(disk->partitionPublic());
316 case PARTITION_TYPE_PRIVATE: return translate(disk->partitionPrivate());
317 case PARTITION_TYPE_MIXED: return translate(disk->partitionMixed(ratio));
318 default: return error("Unknown type " + std::to_string(partitionType));
319 }
320}
321
Jeff Sharkey3ce18252017-10-24 11:08:45 -0600322binder::Status VoldNativeService::forgetPartition(const std::string& partGuid,
323 const std::string& fsUuid) {
Jeff Sharkey9462bdd2017-09-07 15:27:28 -0600324 ENFORCE_UID(AID_SYSTEM);
Jeff Sharkeyec4fda22017-09-12 13:19:24 -0600325 CHECK_ARGUMENT_HEX(partGuid);
Jeff Sharkey3ce18252017-10-24 11:08:45 -0600326 CHECK_ARGUMENT_HEX(fsUuid);
Jeff Sharkey9462bdd2017-09-07 15:27:28 -0600327 ACQUIRE_LOCK;
328
Jeff Sharkey3ce18252017-10-24 11:08:45 -0600329 return translate(VolumeManager::Instance()->forgetPartition(partGuid, fsUuid));
Jeff Sharkey9462bdd2017-09-07 15:27:28 -0600330}
331
Jeff Sharkey11c2d382017-09-11 10:32:01 -0600332binder::Status VoldNativeService::mount(const std::string& volId, int32_t mountFlags,
333 int32_t mountUserId) {
Jeff Sharkey9462bdd2017-09-07 15:27:28 -0600334 ENFORCE_UID(AID_SYSTEM);
Jeff Sharkeyec4fda22017-09-12 13:19:24 -0600335 CHECK_ARGUMENT_ID(volId);
Jeff Sharkey9462bdd2017-09-07 15:27:28 -0600336 ACQUIRE_LOCK;
337
338 auto vol = VolumeManager::Instance()->findVolume(volId);
339 if (vol == nullptr) {
340 return error("Failed to find volume " + volId);
341 }
342
343 vol->setMountFlags(mountFlags);
344 vol->setMountUserId(mountUserId);
345
346 int res = vol->mount();
Jeff Sharkey83b559c2017-09-12 16:30:52 -0600347 if ((mountFlags & MOUNT_FLAG_PRIMARY) != 0) {
Jeff Sharkey9462bdd2017-09-07 15:27:28 -0600348 VolumeManager::Instance()->setPrimary(vol);
349 }
350 return translate(res);
351}
352
353binder::Status VoldNativeService::unmount(const std::string& volId) {
354 ENFORCE_UID(AID_SYSTEM);
Jeff Sharkeyec4fda22017-09-12 13:19:24 -0600355 CHECK_ARGUMENT_ID(volId);
Jeff Sharkey9462bdd2017-09-07 15:27:28 -0600356 ACQUIRE_LOCK;
357
358 auto vol = VolumeManager::Instance()->findVolume(volId);
359 if (vol == nullptr) {
360 return error("Failed to find volume " + volId);
361 }
362 return translate(vol->unmount());
363}
364
365binder::Status VoldNativeService::format(const std::string& volId, const std::string& fsType) {
366 ENFORCE_UID(AID_SYSTEM);
Jeff Sharkeyec4fda22017-09-12 13:19:24 -0600367 CHECK_ARGUMENT_ID(volId);
Jeff Sharkey9462bdd2017-09-07 15:27:28 -0600368 ACQUIRE_LOCK;
369
370 auto vol = VolumeManager::Instance()->findVolume(volId);
371 if (vol == nullptr) {
372 return error("Failed to find volume " + volId);
373 }
374 return translate(vol->format(fsType));
375}
376
Jeff Sharkey2048a282017-06-15 09:59:43 -0600377static binder::Status pathForVolId(const std::string& volId, std::string* path) {
Jeff Sharkey52f7a912017-09-15 12:57:44 -0600378 if (volId == "private" || volId == "null") {
Jeff Sharkey2048a282017-06-15 09:59:43 -0600379 *path = "/data";
Jeff Sharkey52f7a912017-09-15 12:57:44 -0600380 } else {
381 auto vol = VolumeManager::Instance()->findVolume(volId);
382 if (vol == nullptr) {
383 return error("Failed to find volume " + volId);
384 }
385 if (vol->getType() != VolumeBase::Type::kPrivate) {
386 return error("Volume " + volId + " not private");
387 }
388 if (vol->getState() != VolumeBase::State::kMounted) {
389 return error("Volume " + volId + " not mounted");
390 }
Jeff Sharkey2048a282017-06-15 09:59:43 -0600391 *path = vol->getPath();
392 if (path->empty()) {
393 return error("Volume " + volId + " missing path");
394 }
Jeff Sharkey52f7a912017-09-15 12:57:44 -0600395 }
Jeff Sharkey2048a282017-06-15 09:59:43 -0600396 return ok();
397}
Jeff Sharkey52f7a912017-09-15 12:57:44 -0600398
Jeff Sharkey2048a282017-06-15 09:59:43 -0600399binder::Status VoldNativeService::benchmark(
400 const std::string& volId, const android::sp<android::os::IVoldTaskListener>& listener) {
401 ENFORCE_UID(AID_SYSTEM);
402 CHECK_ARGUMENT_ID(volId);
403 ACQUIRE_LOCK;
404
405 std::string path;
406 auto status = pathForVolId(volId, &path);
407 if (!status.isOk()) return status;
Jeff Sharkey52f7a912017-09-15 12:57:44 -0600408
Jeff Sharkey01a0e7f2017-10-17 16:06:32 -0600409 std::thread([=]() {
410 android::vold::Benchmark(path, listener);
411 }).detach();
Jeff Sharkey068c6be2017-09-06 13:47:40 -0600412 return ok();
413}
414
Jeff Sharkey2048a282017-06-15 09:59:43 -0600415binder::Status VoldNativeService::checkEncryption(const std::string& volId) {
416 ENFORCE_UID(AID_SYSTEM);
417 CHECK_ARGUMENT_ID(volId);
418 ACQUIRE_LOCK;
419
420 std::string path;
421 auto status = pathForVolId(volId, &path);
422 if (!status.isOk()) return status;
423 return translate(android::vold::CheckEncryption(path));
424}
425
Jeff Sharkey11c2d382017-09-11 10:32:01 -0600426binder::Status VoldNativeService::moveStorage(const std::string& fromVolId,
Jeff Sharkey52f7a912017-09-15 12:57:44 -0600427 const std::string& toVolId, const android::sp<android::os::IVoldTaskListener>& listener) {
Jeff Sharkey9462bdd2017-09-07 15:27:28 -0600428 ENFORCE_UID(AID_SYSTEM);
Jeff Sharkeyec4fda22017-09-12 13:19:24 -0600429 CHECK_ARGUMENT_ID(fromVolId);
430 CHECK_ARGUMENT_ID(toVolId);
Jeff Sharkey9462bdd2017-09-07 15:27:28 -0600431 ACQUIRE_LOCK;
432
433 auto fromVol = VolumeManager::Instance()->findVolume(fromVolId);
434 auto toVol = VolumeManager::Instance()->findVolume(toVolId);
435 if (fromVol == nullptr) {
436 return error("Failed to find volume " + fromVolId);
437 } else if (toVol == nullptr) {
438 return error("Failed to find volume " + toVolId);
439 }
Jeff Sharkey01a0e7f2017-10-17 16:06:32 -0600440
441 std::thread([=]() {
442 android::vold::MoveStorage(fromVol, toVol, listener);
443 }).detach();
Jeff Sharkey9462bdd2017-09-07 15:27:28 -0600444 return ok();
445}
446
447binder::Status VoldNativeService::remountUid(int32_t uid, int32_t remountMode) {
448 ENFORCE_UID(AID_SYSTEM);
449 ACQUIRE_LOCK;
450
451 std::string tmp;
452 switch (remountMode) {
453 case REMOUNT_MODE_NONE: tmp = "none"; break;
454 case REMOUNT_MODE_DEFAULT: tmp = "default"; break;
455 case REMOUNT_MODE_READ: tmp = "read"; break;
456 case REMOUNT_MODE_WRITE: tmp = "write"; break;
457 default: return error("Unknown mode " + std::to_string(remountMode));
458 }
459 return translate(VolumeManager::Instance()->remountUid(uid, tmp));
460}
461
462binder::Status VoldNativeService::mkdirs(const std::string& path) {
463 ENFORCE_UID(AID_SYSTEM);
Jeff Sharkeyec4fda22017-09-12 13:19:24 -0600464 CHECK_ARGUMENT_PATH(path);
Jeff Sharkey9462bdd2017-09-07 15:27:28 -0600465 ACQUIRE_LOCK;
466
Jeff Sharkey3472e522017-10-06 18:02:53 -0600467 return translate(VolumeManager::Instance()->mkdirs(path));
Jeff Sharkey9462bdd2017-09-07 15:27:28 -0600468}
469
Jeff Sharkey11c2d382017-09-11 10:32:01 -0600470binder::Status VoldNativeService::createObb(const std::string& sourcePath,
471 const std::string& sourceKey, int32_t ownerGid, std::string* _aidl_return) {
472 ENFORCE_UID(AID_SYSTEM);
Jeff Sharkeyec4fda22017-09-12 13:19:24 -0600473 CHECK_ARGUMENT_PATH(sourcePath);
474 CHECK_ARGUMENT_HEX(sourceKey);
Jeff Sharkey11c2d382017-09-11 10:32:01 -0600475 ACQUIRE_LOCK;
476
477 return translate(
478 VolumeManager::Instance()->createObb(sourcePath, sourceKey, ownerGid, _aidl_return));
479}
480
481binder::Status VoldNativeService::destroyObb(const std::string& volId) {
482 ENFORCE_UID(AID_SYSTEM);
Jeff Sharkeyec4fda22017-09-12 13:19:24 -0600483 CHECK_ARGUMENT_ID(volId);
Jeff Sharkey11c2d382017-09-11 10:32:01 -0600484 ACQUIRE_LOCK;
485
486 return translate(VolumeManager::Instance()->destroyObb(volId));
487}
488
Jeff Sharkey52f7a912017-09-15 12:57:44 -0600489binder::Status VoldNativeService::fstrim(int32_t fstrimFlags,
490 const android::sp<android::os::IVoldTaskListener>& listener) {
Jeff Sharkey11c2d382017-09-11 10:32:01 -0600491 ENFORCE_UID(AID_SYSTEM);
492 ACQUIRE_LOCK;
493
Jeff Sharkey01a0e7f2017-10-17 16:06:32 -0600494 std::thread([=]() {
495 android::vold::Trim(listener);
496 }).detach();
Jeff Sharkey11c2d382017-09-11 10:32:01 -0600497 return ok();
498}
499
Jin Qiana370c142017-10-17 15:41:45 -0700500binder::Status VoldNativeService::runIdleMaint(
501 const android::sp<android::os::IVoldTaskListener>& listener) {
502 ENFORCE_UID(AID_SYSTEM);
503 ACQUIRE_LOCK;
504
505 std::thread([=]() {
506 android::vold::RunIdleMaint(listener);
507 }).detach();
508 return ok();
509}
510
511binder::Status VoldNativeService::abortIdleMaint(
512 const android::sp<android::os::IVoldTaskListener>& listener) {
513 ENFORCE_UID(AID_SYSTEM);
514 ACQUIRE_LOCK;
515
516 std::thread([=]() {
517 android::vold::AbortIdleMaint(listener);
518 }).detach();
519 return ok();
520}
521
Jeff Sharkey11c2d382017-09-11 10:32:01 -0600522binder::Status VoldNativeService::mountAppFuse(int32_t uid, int32_t pid, int32_t mountId,
523 android::base::unique_fd* _aidl_return) {
524 ENFORCE_UID(AID_SYSTEM);
525 ACQUIRE_LOCK;
526
527 return translate(VolumeManager::Instance()->mountAppFuse(uid, pid, mountId, _aidl_return));
528}
529
530binder::Status VoldNativeService::unmountAppFuse(int32_t uid, int32_t pid, int32_t mountId) {
531 ENFORCE_UID(AID_SYSTEM);
532 ACQUIRE_LOCK;
533
534 return translate(VolumeManager::Instance()->unmountAppFuse(uid, pid, mountId));
535}
536
Jeff Sharkey83b559c2017-09-12 16:30:52 -0600537binder::Status VoldNativeService::fdeCheckPassword(const std::string& password) {
538 ENFORCE_UID(AID_SYSTEM);
539 ACQUIRE_CRYPT_LOCK;
540
541 return translate(cryptfs_check_passwd(password.c_str()));
542}
543
544binder::Status VoldNativeService::fdeRestart() {
545 ENFORCE_UID(AID_SYSTEM);
546 ACQUIRE_CRYPT_LOCK;
547
548 // Spawn as thread so init can issue commands back to vold without
549 // causing deadlock, usually as a result of prep_data_fs.
550 std::thread(&cryptfs_restart).detach();
551 return ok();
552}
553
554binder::Status VoldNativeService::fdeComplete(int32_t* _aidl_return) {
555 ENFORCE_UID(AID_SYSTEM);
556 ACQUIRE_CRYPT_LOCK;
557
558 *_aidl_return = cryptfs_crypto_complete();
559 return ok();
560}
561
562static int fdeEnableInternal(int32_t passwordType, const std::string& password,
563 int32_t encryptionFlags) {
564 bool noUi = (encryptionFlags & VoldNativeService::ENCRYPTION_FLAG_NO_UI) != 0;
565
Jeff Sharkey83b559c2017-09-12 16:30:52 -0600566 for (int tries = 0; tries < 2; ++tries) {
567 int rc;
568 if (passwordType == VoldNativeService::PASSWORD_TYPE_DEFAULT) {
Paul Lawrence7ee87cf2017-12-22 10:12:06 -0800569 rc = cryptfs_enable_default(noUi);
Jeff Sharkey83b559c2017-09-12 16:30:52 -0600570 } else {
Paul Lawrence7ee87cf2017-12-22 10:12:06 -0800571 rc = cryptfs_enable(passwordType, password.c_str(), noUi);
Jeff Sharkey83b559c2017-09-12 16:30:52 -0600572 }
573
574 if (rc == 0) {
575 return 0;
576 } else if (tries == 0) {
Jeff Sharkey3472e522017-10-06 18:02:53 -0600577 KillProcessesWithOpenFiles(DATA_MNT_POINT, SIGKILL);
Jeff Sharkey83b559c2017-09-12 16:30:52 -0600578 }
579 }
580
581 return -1;
582}
583
584binder::Status VoldNativeService::fdeEnable(int32_t passwordType,
585 const std::string& password, int32_t encryptionFlags) {
586 ENFORCE_UID(AID_SYSTEM);
587 ACQUIRE_CRYPT_LOCK;
588
Paul Crowley0fd26262018-01-30 09:48:19 -0800589 LOG(DEBUG) << "fdeEnable(" << passwordType << ", *, " << encryptionFlags << ")";
Jeff Sharkey83b559c2017-09-12 16:30:52 -0600590 if (e4crypt_is_native()) {
Paul Crowley0fd26262018-01-30 09:48:19 -0800591 LOG(ERROR) << "e4crypt_is_native, fdeEnable invalid";
592 return error("e4crypt_is_native, fdeEnable invalid");
Jeff Sharkey83b559c2017-09-12 16:30:52 -0600593 }
Paul Crowley0fd26262018-01-30 09:48:19 -0800594 LOG(DEBUG) << "!e4crypt_is_native, spawning fdeEnableInternal";
Jeff Sharkey83b559c2017-09-12 16:30:52 -0600595
596 // Spawn as thread so init can issue commands back to vold without
597 // causing deadlock, usually as a result of prep_data_fs.
598 std::thread(&fdeEnableInternal, passwordType, password, encryptionFlags).detach();
599 return ok();
600}
601
602binder::Status VoldNativeService::fdeChangePassword(int32_t passwordType,
603 const std::string& password) {
604 ENFORCE_UID(AID_SYSTEM);
605 ACQUIRE_CRYPT_LOCK;
606
607 return translate(cryptfs_changepw(passwordType, password.c_str()));
608}
609
610binder::Status VoldNativeService::fdeVerifyPassword(const std::string& password) {
611 ENFORCE_UID(AID_SYSTEM);
612 ACQUIRE_CRYPT_LOCK;
613
614 return translate(cryptfs_verify_passwd(password.c_str()));
615}
616
617binder::Status VoldNativeService::fdeGetField(const std::string& key,
618 std::string* _aidl_return) {
619 ENFORCE_UID(AID_SYSTEM);
620 ACQUIRE_CRYPT_LOCK;
621
622 char buf[PROPERTY_VALUE_MAX];
623 if (cryptfs_getfield(key.c_str(), buf, sizeof(buf)) != CRYPTO_GETFIELD_OK) {
624 return error(StringPrintf("Failed to read field %s", key.c_str()));
625 } else {
626 *_aidl_return = buf;
627 return ok();
628 }
629}
630
631binder::Status VoldNativeService::fdeSetField(const std::string& key,
632 const std::string& value) {
633 ENFORCE_UID(AID_SYSTEM);
634 ACQUIRE_CRYPT_LOCK;
635
636 return translate(cryptfs_setfield(key.c_str(), value.c_str()));
637}
638
639binder::Status VoldNativeService::fdeGetPasswordType(int32_t* _aidl_return) {
640 ENFORCE_UID(AID_SYSTEM);
641 ACQUIRE_CRYPT_LOCK;
642
643 *_aidl_return = cryptfs_get_password_type();
644 return ok();
645}
646
647binder::Status VoldNativeService::fdeGetPassword(std::string* _aidl_return) {
648 ENFORCE_UID(AID_SYSTEM);
649 ACQUIRE_CRYPT_LOCK;
650
651 const char* res = cryptfs_get_password();
652 if (res != nullptr) {
653 *_aidl_return = res;
654 }
655 return ok();
656}
657
658binder::Status VoldNativeService::fdeClearPassword() {
659 ENFORCE_UID(AID_SYSTEM);
660 ACQUIRE_CRYPT_LOCK;
661
662 cryptfs_clear_password();
663 return ok();
664}
665
666binder::Status VoldNativeService::fbeEnable() {
667 ENFORCE_UID(AID_SYSTEM);
668 ACQUIRE_CRYPT_LOCK;
669
670 return translateBool(e4crypt_initialize_global_de());
671}
672
673binder::Status VoldNativeService::mountDefaultEncrypted() {
674 ENFORCE_UID(AID_SYSTEM);
675 ACQUIRE_CRYPT_LOCK;
676
Paul Crowley0fd26262018-01-30 09:48:19 -0800677 if (!e4crypt_is_native()) {
Jeff Sharkey83b559c2017-09-12 16:30:52 -0600678 // Spawn as thread so init can issue commands back to vold without
679 // causing deadlock, usually as a result of prep_data_fs.
680 std::thread(&cryptfs_mount_default_encrypted).detach();
Jeff Sharkey83b559c2017-09-12 16:30:52 -0600681 }
Paul Crowley0fd26262018-01-30 09:48:19 -0800682 return ok();
Jeff Sharkey83b559c2017-09-12 16:30:52 -0600683}
684
685binder::Status VoldNativeService::initUser0() {
686 ENFORCE_UID(AID_SYSTEM);
687 ACQUIRE_CRYPT_LOCK;
688
689 return translateBool(e4crypt_init_user0());
690}
691
692binder::Status VoldNativeService::isConvertibleToFbe(bool* _aidl_return) {
693 ENFORCE_UID(AID_SYSTEM);
694 ACQUIRE_CRYPT_LOCK;
695
696 *_aidl_return = cryptfs_isConvertibleToFBE() != 0;
697 return ok();
698}
699
Paul Crowley0fd26262018-01-30 09:48:19 -0800700binder::Status VoldNativeService::mountFstab(const std::string& mountPoint) {
701 ENFORCE_UID(AID_SYSTEM);
702 ACQUIRE_LOCK;
703
704 return translateBool(e4crypt_mount_metadata_encrypted(mountPoint, false));
705}
706
707binder::Status VoldNativeService::encryptFstab(const std::string& mountPoint) {
708 ENFORCE_UID(AID_SYSTEM);
709 ACQUIRE_LOCK;
710
711 return translateBool(e4crypt_mount_metadata_encrypted(mountPoint, true));
712}
713
Jeff Sharkey83b559c2017-09-12 16:30:52 -0600714binder::Status VoldNativeService::createUserKey(int32_t userId, int32_t userSerial,
715 bool ephemeral) {
716 ENFORCE_UID(AID_SYSTEM);
717 ACQUIRE_CRYPT_LOCK;
718
719 return translateBool(e4crypt_vold_create_user_key(userId, userSerial, ephemeral));
720}
721
722binder::Status VoldNativeService::destroyUserKey(int32_t userId) {
723 ENFORCE_UID(AID_SYSTEM);
724 ACQUIRE_CRYPT_LOCK;
725
726 return translateBool(e4crypt_destroy_user_key(userId));
727}
728
729binder::Status VoldNativeService::addUserKeyAuth(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_add_user_key_auth(userId, userSerial, token, secret));
Jeff Sharkey83b559c2017-09-12 16:30:52 -0600735}
736
737binder::Status VoldNativeService::fixateNewestUserKeyAuth(int32_t userId) {
738 ENFORCE_UID(AID_SYSTEM);
739 ACQUIRE_CRYPT_LOCK;
740
741 return translateBool(e4crypt_fixate_newest_user_key_auth(userId));
742}
743
744binder::Status VoldNativeService::unlockUserKey(int32_t userId, int32_t userSerial,
745 const std::string& token, const std::string& secret) {
746 ENFORCE_UID(AID_SYSTEM);
747 ACQUIRE_CRYPT_LOCK;
748
Paul Crowley3b71fc52017-10-09 10:55:21 -0700749 return translateBool(e4crypt_unlock_user_key(userId, userSerial, token, secret));
Jeff Sharkey83b559c2017-09-12 16:30:52 -0600750}
751
752binder::Status VoldNativeService::lockUserKey(int32_t userId) {
753 ENFORCE_UID(AID_SYSTEM);
754 ACQUIRE_CRYPT_LOCK;
755
756 return translateBool(e4crypt_lock_user_key(userId));
757}
758
759binder::Status VoldNativeService::prepareUserStorage(const std::unique_ptr<std::string>& uuid,
760 int32_t userId, int32_t userSerial, int32_t flags) {
761 ENFORCE_UID(AID_SYSTEM);
Paul Crowley3b71fc52017-10-09 10:55:21 -0700762 std::string empty_string = "";
763 auto uuid_ = uuid ? *uuid : empty_string;
Paul Crowley06f762d2017-10-16 10:59:51 -0700764 CHECK_ARGUMENT_HEX(uuid_);
765
766 ACQUIRE_CRYPT_LOCK;
Jeff Sharkey83b559c2017-09-12 16:30:52 -0600767 return translateBool(e4crypt_prepare_user_storage(uuid_, userId, userSerial, flags));
768}
769
770binder::Status VoldNativeService::destroyUserStorage(const std::unique_ptr<std::string>& uuid,
771 int32_t userId, int32_t flags) {
772 ENFORCE_UID(AID_SYSTEM);
Paul Crowley3b71fc52017-10-09 10:55:21 -0700773 std::string empty_string = "";
774 auto uuid_ = uuid ? *uuid : empty_string;
Paul Crowley06f762d2017-10-16 10:59:51 -0700775 CHECK_ARGUMENT_HEX(uuid_);
776
777 ACQUIRE_CRYPT_LOCK;
Jeff Sharkey83b559c2017-09-12 16:30:52 -0600778 return translateBool(e4crypt_destroy_user_storage(uuid_, userId, flags));
779}
780
Jeff Sharkey068c6be2017-09-06 13:47:40 -0600781} // namespace vold
782} // namespace android