blob: 999df94c2529fed563102651570a06484bb736f6 [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
Sudheer Shankac7562092018-08-24 10:20:56 -0700254#define CHECK_ARGUMENT_PACKAGE_NAME(packageName) { \
255 binder::Status status = checkArgumentPackageName((packageName)); \
256 if (!status.isOk()) { \
257 return status; \
258 } \
259}
260
261#define CHECK_ARGUMENT_SANDBOX_ID(sandboxId) { \
262 binder::Status status = checkArgumentSandboxId((sandboxId)); \
263 if (!status.isOk()) { \
264 return status; \
265 } \
266}
267
Jeff Sharkey83b559c2017-09-12 16:30:52 -0600268#define ACQUIRE_LOCK \
Jeff Sharkey67b8c492017-09-21 17:08:43 -0600269 std::lock_guard<std::mutex> lock(VolumeManager::Instance()->getLock()); \
270 ATRACE_CALL();
Jeff Sharkey83b559c2017-09-12 16:30:52 -0600271
272#define ACQUIRE_CRYPT_LOCK \
Jeff Sharkey67b8c492017-09-21 17:08:43 -0600273 std::lock_guard<std::mutex> lock(VolumeManager::Instance()->getCryptLock()); \
274 ATRACE_CALL();
Jeff Sharkey9462bdd2017-09-07 15:27:28 -0600275
Jeff Sharkey068c6be2017-09-06 13:47:40 -0600276} // namespace
277
278status_t VoldNativeService::start() {
279 IPCThreadState::self()->disableBackgroundScheduling(true);
280 status_t ret = BinderService<VoldNativeService>::publish();
281 if (ret != android::OK) {
282 return ret;
283 }
284 sp<ProcessState> ps(ProcessState::self());
285 ps->startThreadPool();
286 ps->giveThreadPoolName();
287 return android::OK;
288}
289
290status_t VoldNativeService::dump(int fd, const Vector<String16> & /* args */) {
291 auto out = std::fstream(StringPrintf("/proc/self/fd/%d", fd));
292 const binder::Status dump_permission = checkPermission(kDump);
293 if (!dump_permission.isOk()) {
294 out << dump_permission.toString8() << endl;
295 return PERMISSION_DENIED;
296 }
297
Jeff Sharkey9462bdd2017-09-07 15:27:28 -0600298 ACQUIRE_LOCK;
Jeff Sharkey068c6be2017-09-06 13:47:40 -0600299 out << "vold is happy!" << endl;
300 out.flush();
Jeff Sharkey068c6be2017-09-06 13:47:40 -0600301 return NO_ERROR;
302}
303
Jeff Sharkey814e9d32017-09-13 11:49:44 -0600304binder::Status VoldNativeService::setListener(
305 const android::sp<android::os::IVoldListener>& listener) {
306 ENFORCE_UID(AID_SYSTEM);
307 ACQUIRE_LOCK;
308
309 VolumeManager::Instance()->setListener(listener);
310 return ok();
311}
312
Jeff Sharkeycbe69fc2017-09-15 16:50:28 -0600313binder::Status VoldNativeService::monitor() {
314 ENFORCE_UID(AID_SYSTEM);
315
316 // Simply acquire/release each lock for watchdog
317 {
318 ACQUIRE_LOCK;
319 }
320 {
321 ACQUIRE_CRYPT_LOCK;
322 }
323
324 return ok();
325}
326
Jeff Sharkey068c6be2017-09-06 13:47:40 -0600327binder::Status VoldNativeService::reset() {
328 ENFORCE_UID(AID_SYSTEM);
Jeff Sharkey9462bdd2017-09-07 15:27:28 -0600329 ACQUIRE_LOCK;
Jeff Sharkey068c6be2017-09-06 13:47:40 -0600330
Jeff Sharkey9462bdd2017-09-07 15:27:28 -0600331 return translate(VolumeManager::Instance()->reset());
332}
333
334binder::Status VoldNativeService::shutdown() {
335 ENFORCE_UID(AID_SYSTEM);
336 ACQUIRE_LOCK;
337
338 return translate(VolumeManager::Instance()->shutdown());
339}
340
Jeff Sharkey9462bdd2017-09-07 15:27:28 -0600341binder::Status VoldNativeService::onUserAdded(int32_t userId, int32_t userSerial) {
342 ENFORCE_UID(AID_SYSTEM);
343 ACQUIRE_LOCK;
344
345 return translate(VolumeManager::Instance()->onUserAdded(userId, userSerial));
346}
347
348binder::Status VoldNativeService::onUserRemoved(int32_t userId) {
349 ENFORCE_UID(AID_SYSTEM);
350 ACQUIRE_LOCK;
351
352 return translate(VolumeManager::Instance()->onUserRemoved(userId));
353}
354
Sudheer Shankaebaad1c2018-07-31 16:39:59 -0700355binder::Status VoldNativeService::onUserStarted(int32_t userId,
356 const std::vector<std::string>& packageNames) {
Jeff Sharkey9462bdd2017-09-07 15:27:28 -0600357 ENFORCE_UID(AID_SYSTEM);
Sudheer Shankacc0df592018-08-02 10:21:42 -0700358 CHECK_ARGUMENT_PACKAGE_NAMES(packageNames);
Jeff Sharkey9462bdd2017-09-07 15:27:28 -0600359 ACQUIRE_LOCK;
360
Sudheer Shankaebaad1c2018-07-31 16:39:59 -0700361 return translate(VolumeManager::Instance()->onUserStarted(userId, packageNames));
Jeff Sharkey9462bdd2017-09-07 15:27:28 -0600362}
363
364binder::Status VoldNativeService::onUserStopped(int32_t userId) {
365 ENFORCE_UID(AID_SYSTEM);
366 ACQUIRE_LOCK;
367
368 return translate(VolumeManager::Instance()->onUserStopped(userId));
369}
370
Sudheer Shankad484aa92018-07-31 10:07:34 -0700371binder::Status VoldNativeService::addAppIds(const std::vector<std::string>& packageNames,
372 const std::vector<int32_t>& appIds) {
373 ENFORCE_UID(AID_SYSTEM);
Sudheer Shankacc0df592018-08-02 10:21:42 -0700374 CHECK_ARGUMENT_PACKAGE_NAMES(packageNames);
Sudheer Shankad484aa92018-07-31 10:07:34 -0700375 ACQUIRE_LOCK;
376
377 return translate(VolumeManager::Instance()->addAppIds(packageNames, appIds));
378}
379
Sudheer Shankad484aa92018-07-31 10:07:34 -0700380binder::Status VoldNativeService::addSandboxIds(const std::vector<int32_t>& appIds,
381 const std::vector<std::string>& sandboxIds) {
382 ENFORCE_UID(AID_SYSTEM);
Sudheer Shankacc0df592018-08-02 10:21:42 -0700383 CHECK_ARGUMENT_SANDBOX_IDS(sandboxIds);
Sudheer Shankad484aa92018-07-31 10:07:34 -0700384 ACQUIRE_LOCK;
385
386 return translate(VolumeManager::Instance()->addSandboxIds(appIds, sandboxIds));
387}
388
Jeff Sharkey401b2602017-12-14 22:15:20 -0700389binder::Status VoldNativeService::onSecureKeyguardStateChanged(bool isShowing) {
390 ENFORCE_UID(AID_SYSTEM);
391 ACQUIRE_LOCK;
392
393 return translate(VolumeManager::Instance()->onSecureKeyguardStateChanged(isShowing));
394}
395
Jeff Sharkey11c2d382017-09-11 10:32:01 -0600396binder::Status VoldNativeService::partition(const std::string& diskId, int32_t partitionType,
397 int32_t ratio) {
Jeff Sharkey9462bdd2017-09-07 15:27:28 -0600398 ENFORCE_UID(AID_SYSTEM);
Jeff Sharkeyec4fda22017-09-12 13:19:24 -0600399 CHECK_ARGUMENT_ID(diskId);
Jeff Sharkey9462bdd2017-09-07 15:27:28 -0600400 ACQUIRE_LOCK;
401
402 auto disk = VolumeManager::Instance()->findDisk(diskId);
403 if (disk == nullptr) {
404 return error("Failed to find disk " + diskId);
405 }
406 switch (partitionType) {
407 case PARTITION_TYPE_PUBLIC: return translate(disk->partitionPublic());
408 case PARTITION_TYPE_PRIVATE: return translate(disk->partitionPrivate());
409 case PARTITION_TYPE_MIXED: return translate(disk->partitionMixed(ratio));
410 default: return error("Unknown type " + std::to_string(partitionType));
411 }
412}
413
Jeff Sharkey3ce18252017-10-24 11:08:45 -0600414binder::Status VoldNativeService::forgetPartition(const std::string& partGuid,
415 const std::string& fsUuid) {
Jeff Sharkey9462bdd2017-09-07 15:27:28 -0600416 ENFORCE_UID(AID_SYSTEM);
Jeff Sharkeyec4fda22017-09-12 13:19:24 -0600417 CHECK_ARGUMENT_HEX(partGuid);
Jeff Sharkey3ce18252017-10-24 11:08:45 -0600418 CHECK_ARGUMENT_HEX(fsUuid);
Jeff Sharkey9462bdd2017-09-07 15:27:28 -0600419 ACQUIRE_LOCK;
420
Jeff Sharkey3ce18252017-10-24 11:08:45 -0600421 return translate(VolumeManager::Instance()->forgetPartition(partGuid, fsUuid));
Jeff Sharkey9462bdd2017-09-07 15:27:28 -0600422}
423
Jeff Sharkey11c2d382017-09-11 10:32:01 -0600424binder::Status VoldNativeService::mount(const std::string& volId, int32_t mountFlags,
425 int32_t mountUserId) {
Jeff Sharkey9462bdd2017-09-07 15:27:28 -0600426 ENFORCE_UID(AID_SYSTEM);
Jeff Sharkeyec4fda22017-09-12 13:19:24 -0600427 CHECK_ARGUMENT_ID(volId);
Jeff Sharkey9462bdd2017-09-07 15:27:28 -0600428 ACQUIRE_LOCK;
429
430 auto vol = VolumeManager::Instance()->findVolume(volId);
431 if (vol == nullptr) {
432 return error("Failed to find volume " + volId);
433 }
434
435 vol->setMountFlags(mountFlags);
436 vol->setMountUserId(mountUserId);
437
438 int res = vol->mount();
Jeff Sharkey83b559c2017-09-12 16:30:52 -0600439 if ((mountFlags & MOUNT_FLAG_PRIMARY) != 0) {
Jeff Sharkey9462bdd2017-09-07 15:27:28 -0600440 VolumeManager::Instance()->setPrimary(vol);
441 }
442 return translate(res);
443}
444
445binder::Status VoldNativeService::unmount(const std::string& volId) {
446 ENFORCE_UID(AID_SYSTEM);
Jeff Sharkeyec4fda22017-09-12 13:19:24 -0600447 CHECK_ARGUMENT_ID(volId);
Jeff Sharkey9462bdd2017-09-07 15:27:28 -0600448 ACQUIRE_LOCK;
449
450 auto vol = VolumeManager::Instance()->findVolume(volId);
451 if (vol == nullptr) {
452 return error("Failed to find volume " + volId);
453 }
454 return translate(vol->unmount());
455}
456
457binder::Status VoldNativeService::format(const std::string& volId, const std::string& fsType) {
458 ENFORCE_UID(AID_SYSTEM);
Jeff Sharkeyec4fda22017-09-12 13:19:24 -0600459 CHECK_ARGUMENT_ID(volId);
Jeff Sharkey9462bdd2017-09-07 15:27:28 -0600460 ACQUIRE_LOCK;
461
462 auto vol = VolumeManager::Instance()->findVolume(volId);
463 if (vol == nullptr) {
464 return error("Failed to find volume " + volId);
465 }
466 return translate(vol->format(fsType));
467}
468
Jeff Sharkey2048a282017-06-15 09:59:43 -0600469static binder::Status pathForVolId(const std::string& volId, std::string* path) {
Jeff Sharkey52f7a912017-09-15 12:57:44 -0600470 if (volId == "private" || volId == "null") {
Jeff Sharkey2048a282017-06-15 09:59:43 -0600471 *path = "/data";
Jeff Sharkey52f7a912017-09-15 12:57:44 -0600472 } else {
473 auto vol = VolumeManager::Instance()->findVolume(volId);
474 if (vol == nullptr) {
475 return error("Failed to find volume " + volId);
476 }
477 if (vol->getType() != VolumeBase::Type::kPrivate) {
478 return error("Volume " + volId + " not private");
479 }
480 if (vol->getState() != VolumeBase::State::kMounted) {
481 return error("Volume " + volId + " not mounted");
482 }
Jeff Sharkey2048a282017-06-15 09:59:43 -0600483 *path = vol->getPath();
484 if (path->empty()) {
485 return error("Volume " + volId + " missing path");
486 }
Jeff Sharkey52f7a912017-09-15 12:57:44 -0600487 }
Jeff Sharkey2048a282017-06-15 09:59:43 -0600488 return ok();
489}
Jeff Sharkey52f7a912017-09-15 12:57:44 -0600490
Jeff Sharkey2048a282017-06-15 09:59:43 -0600491binder::Status VoldNativeService::benchmark(
492 const std::string& volId, const android::sp<android::os::IVoldTaskListener>& listener) {
493 ENFORCE_UID(AID_SYSTEM);
494 CHECK_ARGUMENT_ID(volId);
495 ACQUIRE_LOCK;
496
497 std::string path;
498 auto status = pathForVolId(volId, &path);
499 if (!status.isOk()) return status;
Jeff Sharkey52f7a912017-09-15 12:57:44 -0600500
Jeff Sharkey01a0e7f2017-10-17 16:06:32 -0600501 std::thread([=]() {
502 android::vold::Benchmark(path, listener);
503 }).detach();
Jeff Sharkey068c6be2017-09-06 13:47:40 -0600504 return ok();
505}
506
Jeff Sharkey2048a282017-06-15 09:59:43 -0600507binder::Status VoldNativeService::checkEncryption(const std::string& volId) {
508 ENFORCE_UID(AID_SYSTEM);
509 CHECK_ARGUMENT_ID(volId);
510 ACQUIRE_LOCK;
511
512 std::string path;
513 auto status = pathForVolId(volId, &path);
514 if (!status.isOk()) return status;
515 return translate(android::vold::CheckEncryption(path));
516}
517
Jeff Sharkey11c2d382017-09-11 10:32:01 -0600518binder::Status VoldNativeService::moveStorage(const std::string& fromVolId,
Jeff Sharkey52f7a912017-09-15 12:57:44 -0600519 const std::string& toVolId, const android::sp<android::os::IVoldTaskListener>& listener) {
Jeff Sharkey9462bdd2017-09-07 15:27:28 -0600520 ENFORCE_UID(AID_SYSTEM);
Jeff Sharkeyec4fda22017-09-12 13:19:24 -0600521 CHECK_ARGUMENT_ID(fromVolId);
522 CHECK_ARGUMENT_ID(toVolId);
Jeff Sharkey9462bdd2017-09-07 15:27:28 -0600523 ACQUIRE_LOCK;
524
525 auto fromVol = VolumeManager::Instance()->findVolume(fromVolId);
526 auto toVol = VolumeManager::Instance()->findVolume(toVolId);
527 if (fromVol == nullptr) {
528 return error("Failed to find volume " + fromVolId);
529 } else if (toVol == nullptr) {
530 return error("Failed to find volume " + toVolId);
531 }
Jeff Sharkey01a0e7f2017-10-17 16:06:32 -0600532
533 std::thread([=]() {
534 android::vold::MoveStorage(fromVol, toVol, listener);
535 }).detach();
Jeff Sharkey9462bdd2017-09-07 15:27:28 -0600536 return ok();
537}
538
539binder::Status VoldNativeService::remountUid(int32_t uid, int32_t remountMode) {
540 ENFORCE_UID(AID_SYSTEM);
541 ACQUIRE_LOCK;
542
543 std::string tmp;
544 switch (remountMode) {
545 case REMOUNT_MODE_NONE: tmp = "none"; break;
546 case REMOUNT_MODE_DEFAULT: tmp = "default"; break;
547 case REMOUNT_MODE_READ: tmp = "read"; break;
548 case REMOUNT_MODE_WRITE: tmp = "write"; break;
549 default: return error("Unknown mode " + std::to_string(remountMode));
550 }
551 return translate(VolumeManager::Instance()->remountUid(uid, tmp));
552}
553
554binder::Status VoldNativeService::mkdirs(const std::string& path) {
555 ENFORCE_UID(AID_SYSTEM);
Jeff Sharkeyec4fda22017-09-12 13:19:24 -0600556 CHECK_ARGUMENT_PATH(path);
Jeff Sharkey9462bdd2017-09-07 15:27:28 -0600557 ACQUIRE_LOCK;
558
Jeff Sharkey3472e522017-10-06 18:02:53 -0600559 return translate(VolumeManager::Instance()->mkdirs(path));
Jeff Sharkey9462bdd2017-09-07 15:27:28 -0600560}
561
Jeff Sharkey11c2d382017-09-11 10:32:01 -0600562binder::Status VoldNativeService::createObb(const std::string& sourcePath,
563 const std::string& sourceKey, int32_t ownerGid, std::string* _aidl_return) {
564 ENFORCE_UID(AID_SYSTEM);
Jeff Sharkeyec4fda22017-09-12 13:19:24 -0600565 CHECK_ARGUMENT_PATH(sourcePath);
566 CHECK_ARGUMENT_HEX(sourceKey);
Jeff Sharkey11c2d382017-09-11 10:32:01 -0600567 ACQUIRE_LOCK;
568
569 return translate(
570 VolumeManager::Instance()->createObb(sourcePath, sourceKey, ownerGid, _aidl_return));
571}
572
573binder::Status VoldNativeService::destroyObb(const std::string& volId) {
574 ENFORCE_UID(AID_SYSTEM);
Jeff Sharkeyec4fda22017-09-12 13:19:24 -0600575 CHECK_ARGUMENT_ID(volId);
Jeff Sharkey11c2d382017-09-11 10:32:01 -0600576 ACQUIRE_LOCK;
577
578 return translate(VolumeManager::Instance()->destroyObb(volId));
579}
580
Jeff Sharkey52f7a912017-09-15 12:57:44 -0600581binder::Status VoldNativeService::fstrim(int32_t fstrimFlags,
582 const android::sp<android::os::IVoldTaskListener>& listener) {
Jeff Sharkey11c2d382017-09-11 10:32:01 -0600583 ENFORCE_UID(AID_SYSTEM);
584 ACQUIRE_LOCK;
585
Jeff Sharkey01a0e7f2017-10-17 16:06:32 -0600586 std::thread([=]() {
587 android::vold::Trim(listener);
588 }).detach();
Jeff Sharkey11c2d382017-09-11 10:32:01 -0600589 return ok();
590}
591
Jin Qiana370c142017-10-17 15:41:45 -0700592binder::Status VoldNativeService::runIdleMaint(
593 const android::sp<android::os::IVoldTaskListener>& listener) {
594 ENFORCE_UID(AID_SYSTEM);
595 ACQUIRE_LOCK;
596
597 std::thread([=]() {
598 android::vold::RunIdleMaint(listener);
599 }).detach();
600 return ok();
601}
602
603binder::Status VoldNativeService::abortIdleMaint(
604 const android::sp<android::os::IVoldTaskListener>& listener) {
605 ENFORCE_UID(AID_SYSTEM);
606 ACQUIRE_LOCK;
607
608 std::thread([=]() {
609 android::vold::AbortIdleMaint(listener);
610 }).detach();
611 return ok();
612}
613
Jeff Sharkey11c2d382017-09-11 10:32:01 -0600614binder::Status VoldNativeService::mountAppFuse(int32_t uid, int32_t pid, int32_t mountId,
615 android::base::unique_fd* _aidl_return) {
616 ENFORCE_UID(AID_SYSTEM);
617 ACQUIRE_LOCK;
618
619 return translate(VolumeManager::Instance()->mountAppFuse(uid, pid, mountId, _aidl_return));
620}
621
622binder::Status VoldNativeService::unmountAppFuse(int32_t uid, int32_t pid, int32_t mountId) {
623 ENFORCE_UID(AID_SYSTEM);
624 ACQUIRE_LOCK;
625
626 return translate(VolumeManager::Instance()->unmountAppFuse(uid, pid, mountId));
627}
628
Jeff Sharkey83b559c2017-09-12 16:30:52 -0600629binder::Status VoldNativeService::fdeCheckPassword(const std::string& password) {
630 ENFORCE_UID(AID_SYSTEM);
631 ACQUIRE_CRYPT_LOCK;
632
633 return translate(cryptfs_check_passwd(password.c_str()));
634}
635
636binder::Status VoldNativeService::fdeRestart() {
637 ENFORCE_UID(AID_SYSTEM);
638 ACQUIRE_CRYPT_LOCK;
639
640 // Spawn as thread so init can issue commands back to vold without
641 // causing deadlock, usually as a result of prep_data_fs.
642 std::thread(&cryptfs_restart).detach();
643 return ok();
644}
645
646binder::Status VoldNativeService::fdeComplete(int32_t* _aidl_return) {
647 ENFORCE_UID(AID_SYSTEM);
648 ACQUIRE_CRYPT_LOCK;
649
650 *_aidl_return = cryptfs_crypto_complete();
651 return ok();
652}
653
654static int fdeEnableInternal(int32_t passwordType, const std::string& password,
655 int32_t encryptionFlags) {
656 bool noUi = (encryptionFlags & VoldNativeService::ENCRYPTION_FLAG_NO_UI) != 0;
657
Jeff Sharkey83b559c2017-09-12 16:30:52 -0600658 for (int tries = 0; tries < 2; ++tries) {
659 int rc;
660 if (passwordType == VoldNativeService::PASSWORD_TYPE_DEFAULT) {
Paul Lawrence7ee87cf2017-12-22 10:12:06 -0800661 rc = cryptfs_enable_default(noUi);
Jeff Sharkey83b559c2017-09-12 16:30:52 -0600662 } else {
Paul Lawrence7ee87cf2017-12-22 10:12:06 -0800663 rc = cryptfs_enable(passwordType, password.c_str(), noUi);
Jeff Sharkey83b559c2017-09-12 16:30:52 -0600664 }
665
666 if (rc == 0) {
667 return 0;
668 } else if (tries == 0) {
Jeff Sharkey3472e522017-10-06 18:02:53 -0600669 KillProcessesWithOpenFiles(DATA_MNT_POINT, SIGKILL);
Jeff Sharkey83b559c2017-09-12 16:30:52 -0600670 }
671 }
672
673 return -1;
674}
675
676binder::Status VoldNativeService::fdeEnable(int32_t passwordType,
677 const std::string& password, int32_t encryptionFlags) {
678 ENFORCE_UID(AID_SYSTEM);
679 ACQUIRE_CRYPT_LOCK;
680
Paul Crowley0fd26262018-01-30 09:48:19 -0800681 LOG(DEBUG) << "fdeEnable(" << passwordType << ", *, " << encryptionFlags << ")";
Jeff Sharkey83b559c2017-09-12 16:30:52 -0600682 if (e4crypt_is_native()) {
Paul Crowley0fd26262018-01-30 09:48:19 -0800683 LOG(ERROR) << "e4crypt_is_native, fdeEnable invalid";
684 return error("e4crypt_is_native, fdeEnable invalid");
Jeff Sharkey83b559c2017-09-12 16:30:52 -0600685 }
Paul Crowley0fd26262018-01-30 09:48:19 -0800686 LOG(DEBUG) << "!e4crypt_is_native, spawning fdeEnableInternal";
Jeff Sharkey83b559c2017-09-12 16:30:52 -0600687
688 // Spawn as thread so init can issue commands back to vold without
689 // causing deadlock, usually as a result of prep_data_fs.
690 std::thread(&fdeEnableInternal, passwordType, password, encryptionFlags).detach();
691 return ok();
692}
693
694binder::Status VoldNativeService::fdeChangePassword(int32_t passwordType,
695 const std::string& password) {
696 ENFORCE_UID(AID_SYSTEM);
697 ACQUIRE_CRYPT_LOCK;
698
699 return translate(cryptfs_changepw(passwordType, password.c_str()));
700}
701
702binder::Status VoldNativeService::fdeVerifyPassword(const std::string& password) {
703 ENFORCE_UID(AID_SYSTEM);
704 ACQUIRE_CRYPT_LOCK;
705
706 return translate(cryptfs_verify_passwd(password.c_str()));
707}
708
709binder::Status VoldNativeService::fdeGetField(const std::string& key,
710 std::string* _aidl_return) {
711 ENFORCE_UID(AID_SYSTEM);
712 ACQUIRE_CRYPT_LOCK;
713
714 char buf[PROPERTY_VALUE_MAX];
715 if (cryptfs_getfield(key.c_str(), buf, sizeof(buf)) != CRYPTO_GETFIELD_OK) {
716 return error(StringPrintf("Failed to read field %s", key.c_str()));
717 } else {
718 *_aidl_return = buf;
719 return ok();
720 }
721}
722
723binder::Status VoldNativeService::fdeSetField(const std::string& key,
724 const std::string& value) {
725 ENFORCE_UID(AID_SYSTEM);
726 ACQUIRE_CRYPT_LOCK;
727
728 return translate(cryptfs_setfield(key.c_str(), value.c_str()));
729}
730
731binder::Status VoldNativeService::fdeGetPasswordType(int32_t* _aidl_return) {
732 ENFORCE_UID(AID_SYSTEM);
733 ACQUIRE_CRYPT_LOCK;
734
735 *_aidl_return = cryptfs_get_password_type();
736 return ok();
737}
738
739binder::Status VoldNativeService::fdeGetPassword(std::string* _aidl_return) {
740 ENFORCE_UID(AID_SYSTEM);
741 ACQUIRE_CRYPT_LOCK;
742
743 const char* res = cryptfs_get_password();
744 if (res != nullptr) {
745 *_aidl_return = res;
746 }
747 return ok();
748}
749
750binder::Status VoldNativeService::fdeClearPassword() {
751 ENFORCE_UID(AID_SYSTEM);
752 ACQUIRE_CRYPT_LOCK;
753
754 cryptfs_clear_password();
755 return ok();
756}
757
758binder::Status VoldNativeService::fbeEnable() {
759 ENFORCE_UID(AID_SYSTEM);
760 ACQUIRE_CRYPT_LOCK;
761
762 return translateBool(e4crypt_initialize_global_de());
763}
764
765binder::Status VoldNativeService::mountDefaultEncrypted() {
766 ENFORCE_UID(AID_SYSTEM);
767 ACQUIRE_CRYPT_LOCK;
768
Paul Crowley0fd26262018-01-30 09:48:19 -0800769 if (!e4crypt_is_native()) {
Jeff Sharkey83b559c2017-09-12 16:30:52 -0600770 // Spawn as thread so init can issue commands back to vold without
771 // causing deadlock, usually as a result of prep_data_fs.
772 std::thread(&cryptfs_mount_default_encrypted).detach();
Jeff Sharkey83b559c2017-09-12 16:30:52 -0600773 }
Paul Crowley0fd26262018-01-30 09:48:19 -0800774 return ok();
Jeff Sharkey83b559c2017-09-12 16:30:52 -0600775}
776
777binder::Status VoldNativeService::initUser0() {
778 ENFORCE_UID(AID_SYSTEM);
779 ACQUIRE_CRYPT_LOCK;
780
781 return translateBool(e4crypt_init_user0());
782}
783
784binder::Status VoldNativeService::isConvertibleToFbe(bool* _aidl_return) {
785 ENFORCE_UID(AID_SYSTEM);
786 ACQUIRE_CRYPT_LOCK;
787
788 *_aidl_return = cryptfs_isConvertibleToFBE() != 0;
789 return ok();
790}
791
Paul Crowley0fd26262018-01-30 09:48:19 -0800792binder::Status VoldNativeService::mountFstab(const std::string& mountPoint) {
793 ENFORCE_UID(AID_SYSTEM);
794 ACQUIRE_LOCK;
795
796 return translateBool(e4crypt_mount_metadata_encrypted(mountPoint, false));
797}
798
799binder::Status VoldNativeService::encryptFstab(const std::string& mountPoint) {
800 ENFORCE_UID(AID_SYSTEM);
801 ACQUIRE_LOCK;
802
803 return translateBool(e4crypt_mount_metadata_encrypted(mountPoint, true));
804}
805
Jeff Sharkey83b559c2017-09-12 16:30:52 -0600806binder::Status VoldNativeService::createUserKey(int32_t userId, int32_t userSerial,
807 bool ephemeral) {
808 ENFORCE_UID(AID_SYSTEM);
809 ACQUIRE_CRYPT_LOCK;
810
811 return translateBool(e4crypt_vold_create_user_key(userId, userSerial, ephemeral));
812}
813
814binder::Status VoldNativeService::destroyUserKey(int32_t userId) {
815 ENFORCE_UID(AID_SYSTEM);
816 ACQUIRE_CRYPT_LOCK;
817
818 return translateBool(e4crypt_destroy_user_key(userId));
819}
820
821binder::Status VoldNativeService::addUserKeyAuth(int32_t userId, int32_t userSerial,
822 const std::string& token, const std::string& secret) {
823 ENFORCE_UID(AID_SYSTEM);
824 ACQUIRE_CRYPT_LOCK;
825
Paul Crowley3b71fc52017-10-09 10:55:21 -0700826 return translateBool(e4crypt_add_user_key_auth(userId, userSerial, token, secret));
Jeff Sharkey83b559c2017-09-12 16:30:52 -0600827}
828
829binder::Status VoldNativeService::fixateNewestUserKeyAuth(int32_t userId) {
830 ENFORCE_UID(AID_SYSTEM);
831 ACQUIRE_CRYPT_LOCK;
832
833 return translateBool(e4crypt_fixate_newest_user_key_auth(userId));
834}
835
836binder::Status VoldNativeService::unlockUserKey(int32_t userId, int32_t userSerial,
837 const std::string& token, const std::string& secret) {
838 ENFORCE_UID(AID_SYSTEM);
839 ACQUIRE_CRYPT_LOCK;
840
Paul Crowley3b71fc52017-10-09 10:55:21 -0700841 return translateBool(e4crypt_unlock_user_key(userId, userSerial, token, secret));
Jeff Sharkey83b559c2017-09-12 16:30:52 -0600842}
843
844binder::Status VoldNativeService::lockUserKey(int32_t userId) {
845 ENFORCE_UID(AID_SYSTEM);
846 ACQUIRE_CRYPT_LOCK;
847
848 return translateBool(e4crypt_lock_user_key(userId));
849}
850
851binder::Status VoldNativeService::prepareUserStorage(const std::unique_ptr<std::string>& uuid,
852 int32_t userId, int32_t userSerial, int32_t flags) {
853 ENFORCE_UID(AID_SYSTEM);
Paul Crowley3b71fc52017-10-09 10:55:21 -0700854 std::string empty_string = "";
855 auto uuid_ = uuid ? *uuid : empty_string;
Paul Crowley06f762d2017-10-16 10:59:51 -0700856 CHECK_ARGUMENT_HEX(uuid_);
857
858 ACQUIRE_CRYPT_LOCK;
Jeff Sharkey83b559c2017-09-12 16:30:52 -0600859 return translateBool(e4crypt_prepare_user_storage(uuid_, userId, userSerial, flags));
860}
861
862binder::Status VoldNativeService::destroyUserStorage(const std::unique_ptr<std::string>& uuid,
863 int32_t userId, int32_t flags) {
864 ENFORCE_UID(AID_SYSTEM);
Paul Crowley3b71fc52017-10-09 10:55:21 -0700865 std::string empty_string = "";
866 auto uuid_ = uuid ? *uuid : empty_string;
Paul Crowley06f762d2017-10-16 10:59:51 -0700867 CHECK_ARGUMENT_HEX(uuid_);
868
869 ACQUIRE_CRYPT_LOCK;
Jeff Sharkey83b559c2017-09-12 16:30:52 -0600870 return translateBool(e4crypt_destroy_user_storage(uuid_, userId, flags));
871}
872
Sudheer Shankac7562092018-08-24 10:20:56 -0700873binder::Status VoldNativeService::mountExternalStorageForApp(const std::string& packageName,
874 int32_t appId, const std::string& sandboxId, int32_t userId) {
875 ENFORCE_UID(AID_SYSTEM);
876 CHECK_ARGUMENT_PACKAGE_NAME(packageName);
877 CHECK_ARGUMENT_SANDBOX_ID(sandboxId);
878 ACQUIRE_LOCK;
879
880 return translate(VolumeManager::Instance()->mountExternalStorageForApp(
881 packageName, appId, sandboxId, userId));
882}
883
Jeff Sharkey068c6be2017-09-06 13:47:40 -0600884} // namespace vold
885} // namespace android