blob: 8445cd80d776819bb5128b2dbde1cd5b5281972c [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
Sudheer Shankacc0df592018-08-02 10:21:42 -0700149binder::Status checkArgumentPackageName(const std::string& packageName) {
150 // This logic is borrowed from PackageParser.java
151 bool hasSep = false;
152 bool front = true;
153
154 for (size_t i = 0; i < packageName.length(); ++i) {
155 char c = packageName[i];
156 if ((c >= 'a' && c <= 'z') || (c >= 'A' && c <= 'Z')) {
157 front = false;
158 continue;
159 }
160 if (!front) {
161 if ((c >= '0' && c <= '9') || c == '_') {
162 continue;
163 }
164 }
165 if (c == '.') {
166 hasSep = true;
167 front = true;
168 continue;
169 }
170 return exception(binder::Status::EX_ILLEGAL_ARGUMENT,
171 StringPrintf("Bad package character %c in %s", c, packageName.c_str()));
172 }
173
174 if (front) {
175 return exception(binder::Status::EX_ILLEGAL_ARGUMENT,
176 StringPrintf("Missing separator in %s", packageName.c_str()));
177 }
178
179 return ok();
180}
181
182binder::Status checkArgumentPackageNames(const std::vector<std::string>& packageNames) {
183 for (size_t i = 0; i < packageNames.size(); ++i) {
184 binder::Status status = checkArgumentPackageName(packageNames[i]);
185 if (!status.isOk()) {
186 return status;
187 }
188 }
189 return ok();
190}
191
192binder::Status checkArgumentSandboxId(const std::string& sandboxId) {
193 // sandboxId will be in either the format shared:<shared-user-id> or <package-name>
194 // and <shared-user-id> name has same requirements as <package-name>.
195 std::size_t nameStartIndex = 0;
Greg Kaisere3f59322018-08-06 06:16:29 -0700196 if (android::base::StartsWith(sandboxId, "shared:")) {
197 nameStartIndex = 7; // len("shared:")
Sudheer Shankacc0df592018-08-02 10:21:42 -0700198 }
199 return checkArgumentPackageName(sandboxId.substr(nameStartIndex));
200}
201
202binder::Status checkArgumentSandboxIds(const std::vector<std::string>& sandboxIds) {
203 for (size_t i = 0; i < sandboxIds.size(); ++i) {
204 binder::Status status = checkArgumentSandboxId(sandboxIds[i]);
205 if (!status.isOk()) {
206 return status;
207 }
208 }
209 return ok();
210}
211
Jeff Sharkey068c6be2017-09-06 13:47:40 -0600212#define ENFORCE_UID(uid) { \
213 binder::Status status = checkUid((uid)); \
214 if (!status.isOk()) { \
215 return status; \
216 } \
217}
218
Jeff Sharkeyec4fda22017-09-12 13:19:24 -0600219#define CHECK_ARGUMENT_ID(id) { \
220 binder::Status status = checkArgumentId((id)); \
221 if (!status.isOk()) { \
222 return status; \
223 } \
224}
225
226#define CHECK_ARGUMENT_PATH(path) { \
227 binder::Status status = checkArgumentPath((path)); \
228 if (!status.isOk()) { \
229 return status; \
230 } \
231}
232
233#define CHECK_ARGUMENT_HEX(hex) { \
234 binder::Status status = checkArgumentHex((hex)); \
235 if (!status.isOk()) { \
236 return status; \
237 } \
238}
239
Sudheer Shankacc0df592018-08-02 10:21:42 -0700240#define CHECK_ARGUMENT_PACKAGE_NAMES(packageNames) { \
241 binder::Status status = checkArgumentPackageNames((packageNames)); \
242 if (!status.isOk()) { \
243 return status; \
244 } \
245}
246
247#define CHECK_ARGUMENT_SANDBOX_IDS(sandboxIds) { \
248 binder::Status status = checkArgumentSandboxIds((sandboxIds)); \
249 if (!status.isOk()) { \
250 return status; \
251 } \
252}
253
Jeff Sharkey83b559c2017-09-12 16:30:52 -0600254#define ACQUIRE_LOCK \
Jeff Sharkey67b8c492017-09-21 17:08:43 -0600255 std::lock_guard<std::mutex> lock(VolumeManager::Instance()->getLock()); \
256 ATRACE_CALL();
Jeff Sharkey83b559c2017-09-12 16:30:52 -0600257
258#define ACQUIRE_CRYPT_LOCK \
Jeff Sharkey67b8c492017-09-21 17:08:43 -0600259 std::lock_guard<std::mutex> lock(VolumeManager::Instance()->getCryptLock()); \
260 ATRACE_CALL();
Jeff Sharkey9462bdd2017-09-07 15:27:28 -0600261
Jeff Sharkey068c6be2017-09-06 13:47:40 -0600262} // namespace
263
264status_t VoldNativeService::start() {
265 IPCThreadState::self()->disableBackgroundScheduling(true);
266 status_t ret = BinderService<VoldNativeService>::publish();
267 if (ret != android::OK) {
268 return ret;
269 }
270 sp<ProcessState> ps(ProcessState::self());
271 ps->startThreadPool();
272 ps->giveThreadPoolName();
273 return android::OK;
274}
275
276status_t VoldNativeService::dump(int fd, const Vector<String16> & /* args */) {
277 auto out = std::fstream(StringPrintf("/proc/self/fd/%d", fd));
278 const binder::Status dump_permission = checkPermission(kDump);
279 if (!dump_permission.isOk()) {
280 out << dump_permission.toString8() << endl;
281 return PERMISSION_DENIED;
282 }
283
Jeff Sharkey9462bdd2017-09-07 15:27:28 -0600284 ACQUIRE_LOCK;
Jeff Sharkey068c6be2017-09-06 13:47:40 -0600285 out << "vold is happy!" << endl;
286 out.flush();
Jeff Sharkey068c6be2017-09-06 13:47:40 -0600287 return NO_ERROR;
288}
289
Jeff Sharkey814e9d32017-09-13 11:49:44 -0600290binder::Status VoldNativeService::setListener(
291 const android::sp<android::os::IVoldListener>& listener) {
292 ENFORCE_UID(AID_SYSTEM);
293 ACQUIRE_LOCK;
294
295 VolumeManager::Instance()->setListener(listener);
296 return ok();
297}
298
Jeff Sharkeycbe69fc2017-09-15 16:50:28 -0600299binder::Status VoldNativeService::monitor() {
300 ENFORCE_UID(AID_SYSTEM);
301
302 // Simply acquire/release each lock for watchdog
303 {
304 ACQUIRE_LOCK;
305 }
306 {
307 ACQUIRE_CRYPT_LOCK;
308 }
309
310 return ok();
311}
312
Jeff Sharkey068c6be2017-09-06 13:47:40 -0600313binder::Status VoldNativeService::reset() {
314 ENFORCE_UID(AID_SYSTEM);
Jeff Sharkey9462bdd2017-09-07 15:27:28 -0600315 ACQUIRE_LOCK;
Jeff Sharkey068c6be2017-09-06 13:47:40 -0600316
Jeff Sharkey9462bdd2017-09-07 15:27:28 -0600317 return translate(VolumeManager::Instance()->reset());
318}
319
320binder::Status VoldNativeService::shutdown() {
321 ENFORCE_UID(AID_SYSTEM);
322 ACQUIRE_LOCK;
323
324 return translate(VolumeManager::Instance()->shutdown());
325}
326
Jeff Sharkey9462bdd2017-09-07 15:27:28 -0600327binder::Status VoldNativeService::onUserAdded(int32_t userId, int32_t userSerial) {
328 ENFORCE_UID(AID_SYSTEM);
329 ACQUIRE_LOCK;
330
331 return translate(VolumeManager::Instance()->onUserAdded(userId, userSerial));
332}
333
334binder::Status VoldNativeService::onUserRemoved(int32_t userId) {
335 ENFORCE_UID(AID_SYSTEM);
336 ACQUIRE_LOCK;
337
338 return translate(VolumeManager::Instance()->onUserRemoved(userId));
339}
340
Sudheer Shankaebaad1c2018-07-31 16:39:59 -0700341binder::Status VoldNativeService::onUserStarted(int32_t userId,
342 const std::vector<std::string>& packageNames) {
Jeff Sharkey9462bdd2017-09-07 15:27:28 -0600343 ENFORCE_UID(AID_SYSTEM);
Sudheer Shankacc0df592018-08-02 10:21:42 -0700344 CHECK_ARGUMENT_PACKAGE_NAMES(packageNames);
Jeff Sharkey9462bdd2017-09-07 15:27:28 -0600345 ACQUIRE_LOCK;
346
Sudheer Shankaebaad1c2018-07-31 16:39:59 -0700347 return translate(VolumeManager::Instance()->onUserStarted(userId, packageNames));
Jeff Sharkey9462bdd2017-09-07 15:27:28 -0600348}
349
350binder::Status VoldNativeService::onUserStopped(int32_t userId) {
351 ENFORCE_UID(AID_SYSTEM);
352 ACQUIRE_LOCK;
353
354 return translate(VolumeManager::Instance()->onUserStopped(userId));
355}
356
Sudheer Shankad484aa92018-07-31 10:07:34 -0700357binder::Status VoldNativeService::addAppIds(const std::vector<std::string>& packageNames,
358 const std::vector<int32_t>& appIds) {
359 ENFORCE_UID(AID_SYSTEM);
Sudheer Shankacc0df592018-08-02 10:21:42 -0700360 CHECK_ARGUMENT_PACKAGE_NAMES(packageNames);
Sudheer Shankad484aa92018-07-31 10:07:34 -0700361 ACQUIRE_LOCK;
362
363 return translate(VolumeManager::Instance()->addAppIds(packageNames, appIds));
364}
365
Sudheer Shankad484aa92018-07-31 10:07:34 -0700366binder::Status VoldNativeService::addSandboxIds(const std::vector<int32_t>& appIds,
367 const std::vector<std::string>& sandboxIds) {
368 ENFORCE_UID(AID_SYSTEM);
Sudheer Shankacc0df592018-08-02 10:21:42 -0700369 CHECK_ARGUMENT_SANDBOX_IDS(sandboxIds);
Sudheer Shankad484aa92018-07-31 10:07:34 -0700370 ACQUIRE_LOCK;
371
372 return translate(VolumeManager::Instance()->addSandboxIds(appIds, sandboxIds));
373}
374
Jeff Sharkey401b2602017-12-14 22:15:20 -0700375binder::Status VoldNativeService::onSecureKeyguardStateChanged(bool isShowing) {
376 ENFORCE_UID(AID_SYSTEM);
377 ACQUIRE_LOCK;
378
379 return translate(VolumeManager::Instance()->onSecureKeyguardStateChanged(isShowing));
380}
381
Jeff Sharkey11c2d382017-09-11 10:32:01 -0600382binder::Status VoldNativeService::partition(const std::string& diskId, int32_t partitionType,
383 int32_t ratio) {
Jeff Sharkey9462bdd2017-09-07 15:27:28 -0600384 ENFORCE_UID(AID_SYSTEM);
Jeff Sharkeyec4fda22017-09-12 13:19:24 -0600385 CHECK_ARGUMENT_ID(diskId);
Jeff Sharkey9462bdd2017-09-07 15:27:28 -0600386 ACQUIRE_LOCK;
387
388 auto disk = VolumeManager::Instance()->findDisk(diskId);
389 if (disk == nullptr) {
390 return error("Failed to find disk " + diskId);
391 }
392 switch (partitionType) {
393 case PARTITION_TYPE_PUBLIC: return translate(disk->partitionPublic());
394 case PARTITION_TYPE_PRIVATE: return translate(disk->partitionPrivate());
395 case PARTITION_TYPE_MIXED: return translate(disk->partitionMixed(ratio));
396 default: return error("Unknown type " + std::to_string(partitionType));
397 }
398}
399
Jeff Sharkey3ce18252017-10-24 11:08:45 -0600400binder::Status VoldNativeService::forgetPartition(const std::string& partGuid,
401 const std::string& fsUuid) {
Jeff Sharkey9462bdd2017-09-07 15:27:28 -0600402 ENFORCE_UID(AID_SYSTEM);
Jeff Sharkeyec4fda22017-09-12 13:19:24 -0600403 CHECK_ARGUMENT_HEX(partGuid);
Jeff Sharkey3ce18252017-10-24 11:08:45 -0600404 CHECK_ARGUMENT_HEX(fsUuid);
Jeff Sharkey9462bdd2017-09-07 15:27:28 -0600405 ACQUIRE_LOCK;
406
Jeff Sharkey3ce18252017-10-24 11:08:45 -0600407 return translate(VolumeManager::Instance()->forgetPartition(partGuid, fsUuid));
Jeff Sharkey9462bdd2017-09-07 15:27:28 -0600408}
409
Jeff Sharkey11c2d382017-09-11 10:32:01 -0600410binder::Status VoldNativeService::mount(const std::string& volId, int32_t mountFlags,
411 int32_t mountUserId) {
Jeff Sharkey9462bdd2017-09-07 15:27:28 -0600412 ENFORCE_UID(AID_SYSTEM);
Jeff Sharkeyec4fda22017-09-12 13:19:24 -0600413 CHECK_ARGUMENT_ID(volId);
Jeff Sharkey9462bdd2017-09-07 15:27:28 -0600414 ACQUIRE_LOCK;
415
416 auto vol = VolumeManager::Instance()->findVolume(volId);
417 if (vol == nullptr) {
418 return error("Failed to find volume " + volId);
419 }
420
421 vol->setMountFlags(mountFlags);
422 vol->setMountUserId(mountUserId);
423
424 int res = vol->mount();
Jeff Sharkey83b559c2017-09-12 16:30:52 -0600425 if ((mountFlags & MOUNT_FLAG_PRIMARY) != 0) {
Jeff Sharkey9462bdd2017-09-07 15:27:28 -0600426 VolumeManager::Instance()->setPrimary(vol);
427 }
428 return translate(res);
429}
430
431binder::Status VoldNativeService::unmount(const std::string& volId) {
432 ENFORCE_UID(AID_SYSTEM);
Jeff Sharkeyec4fda22017-09-12 13:19:24 -0600433 CHECK_ARGUMENT_ID(volId);
Jeff Sharkey9462bdd2017-09-07 15:27:28 -0600434 ACQUIRE_LOCK;
435
436 auto vol = VolumeManager::Instance()->findVolume(volId);
437 if (vol == nullptr) {
438 return error("Failed to find volume " + volId);
439 }
440 return translate(vol->unmount());
441}
442
443binder::Status VoldNativeService::format(const std::string& volId, const std::string& fsType) {
444 ENFORCE_UID(AID_SYSTEM);
Jeff Sharkeyec4fda22017-09-12 13:19:24 -0600445 CHECK_ARGUMENT_ID(volId);
Jeff Sharkey9462bdd2017-09-07 15:27:28 -0600446 ACQUIRE_LOCK;
447
448 auto vol = VolumeManager::Instance()->findVolume(volId);
449 if (vol == nullptr) {
450 return error("Failed to find volume " + volId);
451 }
452 return translate(vol->format(fsType));
453}
454
Jeff Sharkey2048a282017-06-15 09:59:43 -0600455static binder::Status pathForVolId(const std::string& volId, std::string* path) {
Jeff Sharkey52f7a912017-09-15 12:57:44 -0600456 if (volId == "private" || volId == "null") {
Jeff Sharkey2048a282017-06-15 09:59:43 -0600457 *path = "/data";
Jeff Sharkey52f7a912017-09-15 12:57:44 -0600458 } else {
459 auto vol = VolumeManager::Instance()->findVolume(volId);
460 if (vol == nullptr) {
461 return error("Failed to find volume " + volId);
462 }
463 if (vol->getType() != VolumeBase::Type::kPrivate) {
464 return error("Volume " + volId + " not private");
465 }
466 if (vol->getState() != VolumeBase::State::kMounted) {
467 return error("Volume " + volId + " not mounted");
468 }
Jeff Sharkey2048a282017-06-15 09:59:43 -0600469 *path = vol->getPath();
470 if (path->empty()) {
471 return error("Volume " + volId + " missing path");
472 }
Jeff Sharkey52f7a912017-09-15 12:57:44 -0600473 }
Jeff Sharkey2048a282017-06-15 09:59:43 -0600474 return ok();
475}
Jeff Sharkey52f7a912017-09-15 12:57:44 -0600476
Jeff Sharkey2048a282017-06-15 09:59:43 -0600477binder::Status VoldNativeService::benchmark(
478 const std::string& volId, const android::sp<android::os::IVoldTaskListener>& listener) {
479 ENFORCE_UID(AID_SYSTEM);
480 CHECK_ARGUMENT_ID(volId);
481 ACQUIRE_LOCK;
482
483 std::string path;
484 auto status = pathForVolId(volId, &path);
485 if (!status.isOk()) return status;
Jeff Sharkey52f7a912017-09-15 12:57:44 -0600486
Jeff Sharkey01a0e7f2017-10-17 16:06:32 -0600487 std::thread([=]() {
488 android::vold::Benchmark(path, listener);
489 }).detach();
Jeff Sharkey068c6be2017-09-06 13:47:40 -0600490 return ok();
491}
492
Jeff Sharkey2048a282017-06-15 09:59:43 -0600493binder::Status VoldNativeService::checkEncryption(const std::string& volId) {
494 ENFORCE_UID(AID_SYSTEM);
495 CHECK_ARGUMENT_ID(volId);
496 ACQUIRE_LOCK;
497
498 std::string path;
499 auto status = pathForVolId(volId, &path);
500 if (!status.isOk()) return status;
501 return translate(android::vold::CheckEncryption(path));
502}
503
Jeff Sharkey11c2d382017-09-11 10:32:01 -0600504binder::Status VoldNativeService::moveStorage(const std::string& fromVolId,
Jeff Sharkey52f7a912017-09-15 12:57:44 -0600505 const std::string& toVolId, const android::sp<android::os::IVoldTaskListener>& listener) {
Jeff Sharkey9462bdd2017-09-07 15:27:28 -0600506 ENFORCE_UID(AID_SYSTEM);
Jeff Sharkeyec4fda22017-09-12 13:19:24 -0600507 CHECK_ARGUMENT_ID(fromVolId);
508 CHECK_ARGUMENT_ID(toVolId);
Jeff Sharkey9462bdd2017-09-07 15:27:28 -0600509 ACQUIRE_LOCK;
510
511 auto fromVol = VolumeManager::Instance()->findVolume(fromVolId);
512 auto toVol = VolumeManager::Instance()->findVolume(toVolId);
513 if (fromVol == nullptr) {
514 return error("Failed to find volume " + fromVolId);
515 } else if (toVol == nullptr) {
516 return error("Failed to find volume " + toVolId);
517 }
Jeff Sharkey01a0e7f2017-10-17 16:06:32 -0600518
519 std::thread([=]() {
520 android::vold::MoveStorage(fromVol, toVol, listener);
521 }).detach();
Jeff Sharkey9462bdd2017-09-07 15:27:28 -0600522 return ok();
523}
524
525binder::Status VoldNativeService::remountUid(int32_t uid, int32_t remountMode) {
526 ENFORCE_UID(AID_SYSTEM);
527 ACQUIRE_LOCK;
528
529 std::string tmp;
530 switch (remountMode) {
531 case REMOUNT_MODE_NONE: tmp = "none"; break;
532 case REMOUNT_MODE_DEFAULT: tmp = "default"; break;
533 case REMOUNT_MODE_READ: tmp = "read"; break;
534 case REMOUNT_MODE_WRITE: tmp = "write"; break;
535 default: return error("Unknown mode " + std::to_string(remountMode));
536 }
537 return translate(VolumeManager::Instance()->remountUid(uid, tmp));
538}
539
540binder::Status VoldNativeService::mkdirs(const std::string& path) {
541 ENFORCE_UID(AID_SYSTEM);
Jeff Sharkeyec4fda22017-09-12 13:19:24 -0600542 CHECK_ARGUMENT_PATH(path);
Jeff Sharkey9462bdd2017-09-07 15:27:28 -0600543 ACQUIRE_LOCK;
544
Jeff Sharkey3472e522017-10-06 18:02:53 -0600545 return translate(VolumeManager::Instance()->mkdirs(path));
Jeff Sharkey9462bdd2017-09-07 15:27:28 -0600546}
547
Jeff Sharkey11c2d382017-09-11 10:32:01 -0600548binder::Status VoldNativeService::createObb(const std::string& sourcePath,
549 const std::string& sourceKey, int32_t ownerGid, std::string* _aidl_return) {
550 ENFORCE_UID(AID_SYSTEM);
Jeff Sharkeyec4fda22017-09-12 13:19:24 -0600551 CHECK_ARGUMENT_PATH(sourcePath);
552 CHECK_ARGUMENT_HEX(sourceKey);
Jeff Sharkey11c2d382017-09-11 10:32:01 -0600553 ACQUIRE_LOCK;
554
555 return translate(
556 VolumeManager::Instance()->createObb(sourcePath, sourceKey, ownerGid, _aidl_return));
557}
558
559binder::Status VoldNativeService::destroyObb(const std::string& volId) {
560 ENFORCE_UID(AID_SYSTEM);
Jeff Sharkeyec4fda22017-09-12 13:19:24 -0600561 CHECK_ARGUMENT_ID(volId);
Jeff Sharkey11c2d382017-09-11 10:32:01 -0600562 ACQUIRE_LOCK;
563
564 return translate(VolumeManager::Instance()->destroyObb(volId));
565}
566
Jeff Sharkey52f7a912017-09-15 12:57:44 -0600567binder::Status VoldNativeService::fstrim(int32_t fstrimFlags,
568 const android::sp<android::os::IVoldTaskListener>& listener) {
Jeff Sharkey11c2d382017-09-11 10:32:01 -0600569 ENFORCE_UID(AID_SYSTEM);
570 ACQUIRE_LOCK;
571
Jeff Sharkey01a0e7f2017-10-17 16:06:32 -0600572 std::thread([=]() {
573 android::vold::Trim(listener);
574 }).detach();
Jeff Sharkey11c2d382017-09-11 10:32:01 -0600575 return ok();
576}
577
Jin Qiana370c142017-10-17 15:41:45 -0700578binder::Status VoldNativeService::runIdleMaint(
579 const android::sp<android::os::IVoldTaskListener>& listener) {
580 ENFORCE_UID(AID_SYSTEM);
581 ACQUIRE_LOCK;
582
583 std::thread([=]() {
584 android::vold::RunIdleMaint(listener);
585 }).detach();
586 return ok();
587}
588
589binder::Status VoldNativeService::abortIdleMaint(
590 const android::sp<android::os::IVoldTaskListener>& listener) {
591 ENFORCE_UID(AID_SYSTEM);
592 ACQUIRE_LOCK;
593
594 std::thread([=]() {
595 android::vold::AbortIdleMaint(listener);
596 }).detach();
597 return ok();
598}
599
Jeff Sharkey11c2d382017-09-11 10:32:01 -0600600binder::Status VoldNativeService::mountAppFuse(int32_t uid, int32_t pid, int32_t mountId,
601 android::base::unique_fd* _aidl_return) {
602 ENFORCE_UID(AID_SYSTEM);
603 ACQUIRE_LOCK;
604
605 return translate(VolumeManager::Instance()->mountAppFuse(uid, pid, mountId, _aidl_return));
606}
607
608binder::Status VoldNativeService::unmountAppFuse(int32_t uid, int32_t pid, int32_t mountId) {
609 ENFORCE_UID(AID_SYSTEM);
610 ACQUIRE_LOCK;
611
612 return translate(VolumeManager::Instance()->unmountAppFuse(uid, pid, mountId));
613}
614
Jeff Sharkey83b559c2017-09-12 16:30:52 -0600615binder::Status VoldNativeService::fdeCheckPassword(const std::string& password) {
616 ENFORCE_UID(AID_SYSTEM);
617 ACQUIRE_CRYPT_LOCK;
618
619 return translate(cryptfs_check_passwd(password.c_str()));
620}
621
622binder::Status VoldNativeService::fdeRestart() {
623 ENFORCE_UID(AID_SYSTEM);
624 ACQUIRE_CRYPT_LOCK;
625
626 // Spawn as thread so init can issue commands back to vold without
627 // causing deadlock, usually as a result of prep_data_fs.
628 std::thread(&cryptfs_restart).detach();
629 return ok();
630}
631
632binder::Status VoldNativeService::fdeComplete(int32_t* _aidl_return) {
633 ENFORCE_UID(AID_SYSTEM);
634 ACQUIRE_CRYPT_LOCK;
635
636 *_aidl_return = cryptfs_crypto_complete();
637 return ok();
638}
639
640static int fdeEnableInternal(int32_t passwordType, const std::string& password,
641 int32_t encryptionFlags) {
642 bool noUi = (encryptionFlags & VoldNativeService::ENCRYPTION_FLAG_NO_UI) != 0;
643
Jeff Sharkey83b559c2017-09-12 16:30:52 -0600644 for (int tries = 0; tries < 2; ++tries) {
645 int rc;
646 if (passwordType == VoldNativeService::PASSWORD_TYPE_DEFAULT) {
Paul Lawrence7ee87cf2017-12-22 10:12:06 -0800647 rc = cryptfs_enable_default(noUi);
Jeff Sharkey83b559c2017-09-12 16:30:52 -0600648 } else {
Paul Lawrence7ee87cf2017-12-22 10:12:06 -0800649 rc = cryptfs_enable(passwordType, password.c_str(), noUi);
Jeff Sharkey83b559c2017-09-12 16:30:52 -0600650 }
651
652 if (rc == 0) {
653 return 0;
654 } else if (tries == 0) {
Jeff Sharkey3472e522017-10-06 18:02:53 -0600655 KillProcessesWithOpenFiles(DATA_MNT_POINT, SIGKILL);
Jeff Sharkey83b559c2017-09-12 16:30:52 -0600656 }
657 }
658
659 return -1;
660}
661
662binder::Status VoldNativeService::fdeEnable(int32_t passwordType,
663 const std::string& password, int32_t encryptionFlags) {
664 ENFORCE_UID(AID_SYSTEM);
665 ACQUIRE_CRYPT_LOCK;
666
Paul Crowley0fd26262018-01-30 09:48:19 -0800667 LOG(DEBUG) << "fdeEnable(" << passwordType << ", *, " << encryptionFlags << ")";
Jeff Sharkey83b559c2017-09-12 16:30:52 -0600668 if (e4crypt_is_native()) {
Paul Crowley0fd26262018-01-30 09:48:19 -0800669 LOG(ERROR) << "e4crypt_is_native, fdeEnable invalid";
670 return error("e4crypt_is_native, fdeEnable invalid");
Jeff Sharkey83b559c2017-09-12 16:30:52 -0600671 }
Paul Crowley0fd26262018-01-30 09:48:19 -0800672 LOG(DEBUG) << "!e4crypt_is_native, spawning fdeEnableInternal";
Jeff Sharkey83b559c2017-09-12 16:30:52 -0600673
674 // Spawn as thread so init can issue commands back to vold without
675 // causing deadlock, usually as a result of prep_data_fs.
676 std::thread(&fdeEnableInternal, passwordType, password, encryptionFlags).detach();
677 return ok();
678}
679
680binder::Status VoldNativeService::fdeChangePassword(int32_t passwordType,
681 const std::string& password) {
682 ENFORCE_UID(AID_SYSTEM);
683 ACQUIRE_CRYPT_LOCK;
684
685 return translate(cryptfs_changepw(passwordType, password.c_str()));
686}
687
688binder::Status VoldNativeService::fdeVerifyPassword(const std::string& password) {
689 ENFORCE_UID(AID_SYSTEM);
690 ACQUIRE_CRYPT_LOCK;
691
692 return translate(cryptfs_verify_passwd(password.c_str()));
693}
694
695binder::Status VoldNativeService::fdeGetField(const std::string& key,
696 std::string* _aidl_return) {
697 ENFORCE_UID(AID_SYSTEM);
698 ACQUIRE_CRYPT_LOCK;
699
700 char buf[PROPERTY_VALUE_MAX];
701 if (cryptfs_getfield(key.c_str(), buf, sizeof(buf)) != CRYPTO_GETFIELD_OK) {
702 return error(StringPrintf("Failed to read field %s", key.c_str()));
703 } else {
704 *_aidl_return = buf;
705 return ok();
706 }
707}
708
709binder::Status VoldNativeService::fdeSetField(const std::string& key,
710 const std::string& value) {
711 ENFORCE_UID(AID_SYSTEM);
712 ACQUIRE_CRYPT_LOCK;
713
714 return translate(cryptfs_setfield(key.c_str(), value.c_str()));
715}
716
717binder::Status VoldNativeService::fdeGetPasswordType(int32_t* _aidl_return) {
718 ENFORCE_UID(AID_SYSTEM);
719 ACQUIRE_CRYPT_LOCK;
720
721 *_aidl_return = cryptfs_get_password_type();
722 return ok();
723}
724
725binder::Status VoldNativeService::fdeGetPassword(std::string* _aidl_return) {
726 ENFORCE_UID(AID_SYSTEM);
727 ACQUIRE_CRYPT_LOCK;
728
729 const char* res = cryptfs_get_password();
730 if (res != nullptr) {
731 *_aidl_return = res;
732 }
733 return ok();
734}
735
736binder::Status VoldNativeService::fdeClearPassword() {
737 ENFORCE_UID(AID_SYSTEM);
738 ACQUIRE_CRYPT_LOCK;
739
740 cryptfs_clear_password();
741 return ok();
742}
743
744binder::Status VoldNativeService::fbeEnable() {
745 ENFORCE_UID(AID_SYSTEM);
746 ACQUIRE_CRYPT_LOCK;
747
748 return translateBool(e4crypt_initialize_global_de());
749}
750
751binder::Status VoldNativeService::mountDefaultEncrypted() {
752 ENFORCE_UID(AID_SYSTEM);
753 ACQUIRE_CRYPT_LOCK;
754
Paul Crowley0fd26262018-01-30 09:48:19 -0800755 if (!e4crypt_is_native()) {
Jeff Sharkey83b559c2017-09-12 16:30:52 -0600756 // Spawn as thread so init can issue commands back to vold without
757 // causing deadlock, usually as a result of prep_data_fs.
758 std::thread(&cryptfs_mount_default_encrypted).detach();
Jeff Sharkey83b559c2017-09-12 16:30:52 -0600759 }
Paul Crowley0fd26262018-01-30 09:48:19 -0800760 return ok();
Jeff Sharkey83b559c2017-09-12 16:30:52 -0600761}
762
763binder::Status VoldNativeService::initUser0() {
764 ENFORCE_UID(AID_SYSTEM);
765 ACQUIRE_CRYPT_LOCK;
766
767 return translateBool(e4crypt_init_user0());
768}
769
770binder::Status VoldNativeService::isConvertibleToFbe(bool* _aidl_return) {
771 ENFORCE_UID(AID_SYSTEM);
772 ACQUIRE_CRYPT_LOCK;
773
774 *_aidl_return = cryptfs_isConvertibleToFBE() != 0;
775 return ok();
776}
777
Paul Crowley0fd26262018-01-30 09:48:19 -0800778binder::Status VoldNativeService::mountFstab(const std::string& mountPoint) {
779 ENFORCE_UID(AID_SYSTEM);
780 ACQUIRE_LOCK;
781
782 return translateBool(e4crypt_mount_metadata_encrypted(mountPoint, false));
783}
784
785binder::Status VoldNativeService::encryptFstab(const std::string& mountPoint) {
786 ENFORCE_UID(AID_SYSTEM);
787 ACQUIRE_LOCK;
788
789 return translateBool(e4crypt_mount_metadata_encrypted(mountPoint, true));
790}
791
Jeff Sharkey83b559c2017-09-12 16:30:52 -0600792binder::Status VoldNativeService::createUserKey(int32_t userId, int32_t userSerial,
793 bool ephemeral) {
794 ENFORCE_UID(AID_SYSTEM);
795 ACQUIRE_CRYPT_LOCK;
796
797 return translateBool(e4crypt_vold_create_user_key(userId, userSerial, ephemeral));
798}
799
800binder::Status VoldNativeService::destroyUserKey(int32_t userId) {
801 ENFORCE_UID(AID_SYSTEM);
802 ACQUIRE_CRYPT_LOCK;
803
804 return translateBool(e4crypt_destroy_user_key(userId));
805}
806
807binder::Status VoldNativeService::addUserKeyAuth(int32_t userId, int32_t userSerial,
808 const std::string& token, const std::string& secret) {
809 ENFORCE_UID(AID_SYSTEM);
810 ACQUIRE_CRYPT_LOCK;
811
Paul Crowley3b71fc52017-10-09 10:55:21 -0700812 return translateBool(e4crypt_add_user_key_auth(userId, userSerial, token, secret));
Jeff Sharkey83b559c2017-09-12 16:30:52 -0600813}
814
815binder::Status VoldNativeService::fixateNewestUserKeyAuth(int32_t userId) {
816 ENFORCE_UID(AID_SYSTEM);
817 ACQUIRE_CRYPT_LOCK;
818
819 return translateBool(e4crypt_fixate_newest_user_key_auth(userId));
820}
821
822binder::Status VoldNativeService::unlockUserKey(int32_t userId, int32_t userSerial,
823 const std::string& token, const std::string& secret) {
824 ENFORCE_UID(AID_SYSTEM);
825 ACQUIRE_CRYPT_LOCK;
826
Paul Crowley3b71fc52017-10-09 10:55:21 -0700827 return translateBool(e4crypt_unlock_user_key(userId, userSerial, token, secret));
Jeff Sharkey83b559c2017-09-12 16:30:52 -0600828}
829
830binder::Status VoldNativeService::lockUserKey(int32_t userId) {
831 ENFORCE_UID(AID_SYSTEM);
832 ACQUIRE_CRYPT_LOCK;
833
834 return translateBool(e4crypt_lock_user_key(userId));
835}
836
837binder::Status VoldNativeService::prepareUserStorage(const std::unique_ptr<std::string>& uuid,
838 int32_t userId, int32_t userSerial, int32_t flags) {
839 ENFORCE_UID(AID_SYSTEM);
Paul Crowley3b71fc52017-10-09 10:55:21 -0700840 std::string empty_string = "";
841 auto uuid_ = uuid ? *uuid : empty_string;
Paul Crowley06f762d2017-10-16 10:59:51 -0700842 CHECK_ARGUMENT_HEX(uuid_);
843
844 ACQUIRE_CRYPT_LOCK;
Jeff Sharkey83b559c2017-09-12 16:30:52 -0600845 return translateBool(e4crypt_prepare_user_storage(uuid_, userId, userSerial, flags));
846}
847
848binder::Status VoldNativeService::destroyUserStorage(const std::unique_ptr<std::string>& uuid,
849 int32_t userId, int32_t flags) {
850 ENFORCE_UID(AID_SYSTEM);
Paul Crowley3b71fc52017-10-09 10:55:21 -0700851 std::string empty_string = "";
852 auto uuid_ = uuid ? *uuid : empty_string;
Paul Crowley06f762d2017-10-16 10:59:51 -0700853 CHECK_ARGUMENT_HEX(uuid_);
854
855 ACQUIRE_CRYPT_LOCK;
Jeff Sharkey83b559c2017-09-12 16:30:52 -0600856 return translateBool(e4crypt_destroy_user_storage(uuid_, userId, flags));
857}
858
Jeff Sharkey068c6be2017-09-06 13:47:40 -0600859} // namespace vold
860} // namespace android