blob: 1a15304892c0606104ff324fcad19a9db0e44187 [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 "Ext4Crypt.h"
28#include "MetadataCrypt.h"
Paul Crowley8915d622018-09-18 15:14:18 -070029#include "cryptfs.h"
Jeff Sharkey83b559c2017-09-12 16:30:52 -060030
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),
Paul Crowley8915d622018-09-18 15:14:18 -070086 reinterpret_cast<int32_t*>(&uid))) {
Jeff Sharkey068c6be2017-09-06 13:47:40 -060087 return ok();
88 } else {
89 return exception(binder::Status::EX_SECURITY,
Paul Crowley8915d622018-09-18 15:14:18 -070090 StringPrintf("UID %d / PID %d lacks permission %s", uid, pid, permission));
Jeff Sharkey068c6be2017-09-06 13:47:40 -060091 }
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,
Paul Crowley8915d622018-09-18 15:14:18 -0700100 StringPrintf("UID %d is not expected UID %d", uid, expectedUid));
Jeff Sharkey068c6be2017-09-06 13:47:40 -0600101 }
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,
Paul Crowley8915d622018-09-18 15:14:18 -0700111 StringPrintf("ID %s is malformed", id.c_str()));
Jeff Sharkeyec4fda22017-09-12 13:19:24 -0600112 }
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,
Paul Crowley8915d622018-09-18 15:14:18 -0700123 StringPrintf("Path %s is relative", path.c_str()));
Jeff Sharkeyec4fda22017-09-12 13:19:24 -0600124 }
Jeff Sharkey01a0e7f2017-10-17 16:06:32 -0600125 if ((path + '/').find("/../") != std::string::npos) {
126 return exception(binder::Status::EX_ILLEGAL_ARGUMENT,
Paul Crowley8915d622018-09-18 15:14:18 -0700127 StringPrintf("Path %s is shady", path.c_str()));
Jeff Sharkey01a0e7f2017-10-17 16:06:32 -0600128 }
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,
Paul Crowley8915d622018-09-18 15:14:18 -0700132 StringPrintf("Path %s is malformed", path.c_str()));
Jeff Sharkeyec4fda22017-09-12 13:19:24 -0600133 }
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,
Paul Crowley8915d622018-09-18 15:14:18 -0700143 StringPrintf("Hex %s is malformed", hex.c_str()));
Jeff Sharkeyec4fda22017-09-12 13:19:24 -0600144 }
145 }
146 return ok();
147}
148
Paul Crowley8915d622018-09-18 15:14:18 -0700149#define ENFORCE_UID(uid) \
150 { \
151 binder::Status status = checkUid((uid)); \
152 if (!status.isOk()) { \
153 return status; \
154 } \
155 }
Jeff Sharkey068c6be2017-09-06 13:47:40 -0600156
Paul Crowley8915d622018-09-18 15:14:18 -0700157#define CHECK_ARGUMENT_ID(id) \
158 { \
159 binder::Status status = checkArgumentId((id)); \
160 if (!status.isOk()) { \
161 return status; \
162 } \
163 }
Jeff Sharkeyec4fda22017-09-12 13:19:24 -0600164
Paul Crowley8915d622018-09-18 15:14:18 -0700165#define CHECK_ARGUMENT_PATH(path) \
166 { \
167 binder::Status status = checkArgumentPath((path)); \
168 if (!status.isOk()) { \
169 return status; \
170 } \
171 }
Jeff Sharkeyec4fda22017-09-12 13:19:24 -0600172
Paul Crowley8915d622018-09-18 15:14:18 -0700173#define CHECK_ARGUMENT_HEX(hex) \
174 { \
175 binder::Status status = checkArgumentHex((hex)); \
176 if (!status.isOk()) { \
177 return status; \
178 } \
179 }
Jeff Sharkeyec4fda22017-09-12 13:19:24 -0600180
Paul Crowley8915d622018-09-18 15:14:18 -0700181#define ACQUIRE_LOCK \
Jeff Sharkey67b8c492017-09-21 17:08:43 -0600182 std::lock_guard<std::mutex> lock(VolumeManager::Instance()->getLock()); \
183 ATRACE_CALL();
Jeff Sharkey83b559c2017-09-12 16:30:52 -0600184
Paul Crowley8915d622018-09-18 15:14:18 -0700185#define ACQUIRE_CRYPT_LOCK \
Jeff Sharkey67b8c492017-09-21 17:08:43 -0600186 std::lock_guard<std::mutex> lock(VolumeManager::Instance()->getCryptLock()); \
187 ATRACE_CALL();
Jeff Sharkey9462bdd2017-09-07 15:27:28 -0600188
Jeff Sharkey068c6be2017-09-06 13:47:40 -0600189} // namespace
190
191status_t VoldNativeService::start() {
192 IPCThreadState::self()->disableBackgroundScheduling(true);
193 status_t ret = BinderService<VoldNativeService>::publish();
194 if (ret != android::OK) {
195 return ret;
196 }
197 sp<ProcessState> ps(ProcessState::self());
198 ps->startThreadPool();
199 ps->giveThreadPoolName();
200 return android::OK;
201}
202
Paul Crowley8915d622018-09-18 15:14:18 -0700203status_t VoldNativeService::dump(int fd, const Vector<String16>& /* args */) {
Jeff Sharkey068c6be2017-09-06 13:47:40 -0600204 auto out = std::fstream(StringPrintf("/proc/self/fd/%d", fd));
205 const binder::Status dump_permission = checkPermission(kDump);
206 if (!dump_permission.isOk()) {
207 out << dump_permission.toString8() << endl;
208 return PERMISSION_DENIED;
209 }
210
Jeff Sharkey9462bdd2017-09-07 15:27:28 -0600211 ACQUIRE_LOCK;
Jeff Sharkey068c6be2017-09-06 13:47:40 -0600212 out << "vold is happy!" << endl;
213 out.flush();
Jeff Sharkey068c6be2017-09-06 13:47:40 -0600214 return NO_ERROR;
215}
216
Jeff Sharkey814e9d32017-09-13 11:49:44 -0600217binder::Status VoldNativeService::setListener(
Paul Crowley8915d622018-09-18 15:14:18 -0700218 const android::sp<android::os::IVoldListener>& listener) {
Jeff Sharkey814e9d32017-09-13 11:49:44 -0600219 ENFORCE_UID(AID_SYSTEM);
220 ACQUIRE_LOCK;
221
222 VolumeManager::Instance()->setListener(listener);
223 return ok();
224}
225
Jeff Sharkeycbe69fc2017-09-15 16:50:28 -0600226binder::Status VoldNativeService::monitor() {
227 ENFORCE_UID(AID_SYSTEM);
228
229 // Simply acquire/release each lock for watchdog
Paul Crowley8915d622018-09-18 15:14:18 -0700230 { ACQUIRE_LOCK; }
231 { ACQUIRE_CRYPT_LOCK; }
Jeff Sharkeycbe69fc2017-09-15 16:50:28 -0600232
233 return ok();
234}
235
Jeff Sharkey068c6be2017-09-06 13:47:40 -0600236binder::Status VoldNativeService::reset() {
237 ENFORCE_UID(AID_SYSTEM);
Jeff Sharkey9462bdd2017-09-07 15:27:28 -0600238 ACQUIRE_LOCK;
Jeff Sharkey068c6be2017-09-06 13:47:40 -0600239
Jeff Sharkey9462bdd2017-09-07 15:27:28 -0600240 return translate(VolumeManager::Instance()->reset());
241}
242
243binder::Status VoldNativeService::shutdown() {
244 ENFORCE_UID(AID_SYSTEM);
245 ACQUIRE_LOCK;
246
247 return translate(VolumeManager::Instance()->shutdown());
248}
249
Jeff Sharkey9462bdd2017-09-07 15:27:28 -0600250binder::Status VoldNativeService::onUserAdded(int32_t userId, int32_t userSerial) {
251 ENFORCE_UID(AID_SYSTEM);
252 ACQUIRE_LOCK;
253
254 return translate(VolumeManager::Instance()->onUserAdded(userId, userSerial));
255}
256
257binder::Status VoldNativeService::onUserRemoved(int32_t userId) {
258 ENFORCE_UID(AID_SYSTEM);
259 ACQUIRE_LOCK;
260
261 return translate(VolumeManager::Instance()->onUserRemoved(userId));
262}
263
264binder::Status VoldNativeService::onUserStarted(int32_t userId) {
265 ENFORCE_UID(AID_SYSTEM);
266 ACQUIRE_LOCK;
267
268 return translate(VolumeManager::Instance()->onUserStarted(userId));
269}
270
271binder::Status VoldNativeService::onUserStopped(int32_t userId) {
272 ENFORCE_UID(AID_SYSTEM);
273 ACQUIRE_LOCK;
274
275 return translate(VolumeManager::Instance()->onUserStopped(userId));
276}
277
Jeff Sharkey401b2602017-12-14 22:15:20 -0700278binder::Status VoldNativeService::onSecureKeyguardStateChanged(bool isShowing) {
279 ENFORCE_UID(AID_SYSTEM);
280 ACQUIRE_LOCK;
281
282 return translate(VolumeManager::Instance()->onSecureKeyguardStateChanged(isShowing));
283}
284
Jeff Sharkey11c2d382017-09-11 10:32:01 -0600285binder::Status VoldNativeService::partition(const std::string& diskId, int32_t partitionType,
Paul Crowley8915d622018-09-18 15:14:18 -0700286 int32_t ratio) {
Jeff Sharkey9462bdd2017-09-07 15:27:28 -0600287 ENFORCE_UID(AID_SYSTEM);
Jeff Sharkeyec4fda22017-09-12 13:19:24 -0600288 CHECK_ARGUMENT_ID(diskId);
Jeff Sharkey9462bdd2017-09-07 15:27:28 -0600289 ACQUIRE_LOCK;
290
291 auto disk = VolumeManager::Instance()->findDisk(diskId);
292 if (disk == nullptr) {
293 return error("Failed to find disk " + diskId);
294 }
295 switch (partitionType) {
Paul Crowley8915d622018-09-18 15:14:18 -0700296 case PARTITION_TYPE_PUBLIC:
297 return translate(disk->partitionPublic());
298 case PARTITION_TYPE_PRIVATE:
299 return translate(disk->partitionPrivate());
300 case PARTITION_TYPE_MIXED:
301 return translate(disk->partitionMixed(ratio));
302 default:
303 return error("Unknown type " + std::to_string(partitionType));
Jeff Sharkey9462bdd2017-09-07 15:27:28 -0600304 }
305}
306
Jeff Sharkey3ce18252017-10-24 11:08:45 -0600307binder::Status VoldNativeService::forgetPartition(const std::string& partGuid,
Paul Crowley8915d622018-09-18 15:14:18 -0700308 const std::string& fsUuid) {
Jeff Sharkey9462bdd2017-09-07 15:27:28 -0600309 ENFORCE_UID(AID_SYSTEM);
Jeff Sharkeyec4fda22017-09-12 13:19:24 -0600310 CHECK_ARGUMENT_HEX(partGuid);
Jeff Sharkey3ce18252017-10-24 11:08:45 -0600311 CHECK_ARGUMENT_HEX(fsUuid);
Jeff Sharkey9462bdd2017-09-07 15:27:28 -0600312 ACQUIRE_LOCK;
313
Jeff Sharkey3ce18252017-10-24 11:08:45 -0600314 return translate(VolumeManager::Instance()->forgetPartition(partGuid, fsUuid));
Jeff Sharkey9462bdd2017-09-07 15:27:28 -0600315}
316
Jeff Sharkey11c2d382017-09-11 10:32:01 -0600317binder::Status VoldNativeService::mount(const std::string& volId, int32_t mountFlags,
Paul Crowley8915d622018-09-18 15:14:18 -0700318 int32_t mountUserId) {
Jeff Sharkey9462bdd2017-09-07 15:27:28 -0600319 ENFORCE_UID(AID_SYSTEM);
Jeff Sharkeyec4fda22017-09-12 13:19:24 -0600320 CHECK_ARGUMENT_ID(volId);
Jeff Sharkey9462bdd2017-09-07 15:27:28 -0600321 ACQUIRE_LOCK;
322
323 auto vol = VolumeManager::Instance()->findVolume(volId);
324 if (vol == nullptr) {
325 return error("Failed to find volume " + volId);
326 }
327
328 vol->setMountFlags(mountFlags);
329 vol->setMountUserId(mountUserId);
330
331 int res = vol->mount();
Jeff Sharkey83b559c2017-09-12 16:30:52 -0600332 if ((mountFlags & MOUNT_FLAG_PRIMARY) != 0) {
Jeff Sharkey9462bdd2017-09-07 15:27:28 -0600333 VolumeManager::Instance()->setPrimary(vol);
334 }
335 return translate(res);
336}
337
338binder::Status VoldNativeService::unmount(const std::string& volId) {
339 ENFORCE_UID(AID_SYSTEM);
Jeff Sharkeyec4fda22017-09-12 13:19:24 -0600340 CHECK_ARGUMENT_ID(volId);
Jeff Sharkey9462bdd2017-09-07 15:27:28 -0600341 ACQUIRE_LOCK;
342
343 auto vol = VolumeManager::Instance()->findVolume(volId);
344 if (vol == nullptr) {
345 return error("Failed to find volume " + volId);
346 }
347 return translate(vol->unmount());
348}
349
350binder::Status VoldNativeService::format(const std::string& volId, const std::string& fsType) {
351 ENFORCE_UID(AID_SYSTEM);
Jeff Sharkeyec4fda22017-09-12 13:19:24 -0600352 CHECK_ARGUMENT_ID(volId);
Jeff Sharkey9462bdd2017-09-07 15:27:28 -0600353 ACQUIRE_LOCK;
354
355 auto vol = VolumeManager::Instance()->findVolume(volId);
356 if (vol == nullptr) {
357 return error("Failed to find volume " + volId);
358 }
359 return translate(vol->format(fsType));
360}
361
Jeff Sharkey2048a282017-06-15 09:59:43 -0600362static binder::Status pathForVolId(const std::string& volId, std::string* path) {
Jeff Sharkey52f7a912017-09-15 12:57:44 -0600363 if (volId == "private" || volId == "null") {
Jeff Sharkey2048a282017-06-15 09:59:43 -0600364 *path = "/data";
Jeff Sharkey52f7a912017-09-15 12:57:44 -0600365 } else {
366 auto vol = VolumeManager::Instance()->findVolume(volId);
367 if (vol == nullptr) {
368 return error("Failed to find volume " + volId);
369 }
370 if (vol->getType() != VolumeBase::Type::kPrivate) {
371 return error("Volume " + volId + " not private");
372 }
373 if (vol->getState() != VolumeBase::State::kMounted) {
374 return error("Volume " + volId + " not mounted");
375 }
Jeff Sharkey2048a282017-06-15 09:59:43 -0600376 *path = vol->getPath();
377 if (path->empty()) {
378 return error("Volume " + volId + " missing path");
379 }
Jeff Sharkey52f7a912017-09-15 12:57:44 -0600380 }
Jeff Sharkey2048a282017-06-15 09:59:43 -0600381 return ok();
382}
Jeff Sharkey52f7a912017-09-15 12:57:44 -0600383
Jeff Sharkey2048a282017-06-15 09:59:43 -0600384binder::Status VoldNativeService::benchmark(
385 const std::string& volId, const android::sp<android::os::IVoldTaskListener>& listener) {
386 ENFORCE_UID(AID_SYSTEM);
387 CHECK_ARGUMENT_ID(volId);
388 ACQUIRE_LOCK;
389
390 std::string path;
391 auto status = pathForVolId(volId, &path);
392 if (!status.isOk()) return status;
Jeff Sharkey52f7a912017-09-15 12:57:44 -0600393
Paul Crowley8915d622018-09-18 15:14:18 -0700394 std::thread([=]() { android::vold::Benchmark(path, listener); }).detach();
Jeff Sharkey068c6be2017-09-06 13:47:40 -0600395 return ok();
396}
397
Jeff Sharkey2048a282017-06-15 09:59:43 -0600398binder::Status VoldNativeService::checkEncryption(const std::string& volId) {
399 ENFORCE_UID(AID_SYSTEM);
400 CHECK_ARGUMENT_ID(volId);
401 ACQUIRE_LOCK;
402
403 std::string path;
404 auto status = pathForVolId(volId, &path);
405 if (!status.isOk()) return status;
406 return translate(android::vold::CheckEncryption(path));
407}
408
Paul Crowley8915d622018-09-18 15:14:18 -0700409binder::Status VoldNativeService::moveStorage(
410 const std::string& fromVolId, const std::string& toVolId,
411 const android::sp<android::os::IVoldTaskListener>& listener) {
Jeff Sharkey9462bdd2017-09-07 15:27:28 -0600412 ENFORCE_UID(AID_SYSTEM);
Jeff Sharkeyec4fda22017-09-12 13:19:24 -0600413 CHECK_ARGUMENT_ID(fromVolId);
414 CHECK_ARGUMENT_ID(toVolId);
Jeff Sharkey9462bdd2017-09-07 15:27:28 -0600415 ACQUIRE_LOCK;
416
417 auto fromVol = VolumeManager::Instance()->findVolume(fromVolId);
418 auto toVol = VolumeManager::Instance()->findVolume(toVolId);
419 if (fromVol == nullptr) {
420 return error("Failed to find volume " + fromVolId);
421 } else if (toVol == nullptr) {
422 return error("Failed to find volume " + toVolId);
423 }
Jeff Sharkey01a0e7f2017-10-17 16:06:32 -0600424
Paul Crowley8915d622018-09-18 15:14:18 -0700425 std::thread([=]() { android::vold::MoveStorage(fromVol, toVol, listener); }).detach();
Jeff Sharkey9462bdd2017-09-07 15:27:28 -0600426 return ok();
427}
428
429binder::Status VoldNativeService::remountUid(int32_t uid, int32_t remountMode) {
430 ENFORCE_UID(AID_SYSTEM);
431 ACQUIRE_LOCK;
432
433 std::string tmp;
434 switch (remountMode) {
Paul Crowley8915d622018-09-18 15:14:18 -0700435 case REMOUNT_MODE_NONE:
436 tmp = "none";
437 break;
438 case REMOUNT_MODE_DEFAULT:
439 tmp = "default";
440 break;
441 case REMOUNT_MODE_READ:
442 tmp = "read";
443 break;
444 case REMOUNT_MODE_WRITE:
445 tmp = "write";
446 break;
447 default:
448 return error("Unknown mode " + std::to_string(remountMode));
Jeff Sharkey9462bdd2017-09-07 15:27:28 -0600449 }
450 return translate(VolumeManager::Instance()->remountUid(uid, tmp));
451}
452
453binder::Status VoldNativeService::mkdirs(const std::string& path) {
454 ENFORCE_UID(AID_SYSTEM);
Jeff Sharkeyec4fda22017-09-12 13:19:24 -0600455 CHECK_ARGUMENT_PATH(path);
Jeff Sharkey9462bdd2017-09-07 15:27:28 -0600456 ACQUIRE_LOCK;
457
Jeff Sharkey3472e522017-10-06 18:02:53 -0600458 return translate(VolumeManager::Instance()->mkdirs(path));
Jeff Sharkey9462bdd2017-09-07 15:27:28 -0600459}
460
Jeff Sharkey11c2d382017-09-11 10:32:01 -0600461binder::Status VoldNativeService::createObb(const std::string& sourcePath,
Paul Crowley8915d622018-09-18 15:14:18 -0700462 const std::string& sourceKey, int32_t ownerGid,
463 std::string* _aidl_return) {
Jeff Sharkey11c2d382017-09-11 10:32:01 -0600464 ENFORCE_UID(AID_SYSTEM);
Jeff Sharkeyec4fda22017-09-12 13:19:24 -0600465 CHECK_ARGUMENT_PATH(sourcePath);
466 CHECK_ARGUMENT_HEX(sourceKey);
Jeff Sharkey11c2d382017-09-11 10:32:01 -0600467 ACQUIRE_LOCK;
468
469 return translate(
Paul Crowley8915d622018-09-18 15:14:18 -0700470 VolumeManager::Instance()->createObb(sourcePath, sourceKey, ownerGid, _aidl_return));
Jeff Sharkey11c2d382017-09-11 10:32:01 -0600471}
472
473binder::Status VoldNativeService::destroyObb(const std::string& volId) {
474 ENFORCE_UID(AID_SYSTEM);
Jeff Sharkeyec4fda22017-09-12 13:19:24 -0600475 CHECK_ARGUMENT_ID(volId);
Jeff Sharkey11c2d382017-09-11 10:32:01 -0600476 ACQUIRE_LOCK;
477
478 return translate(VolumeManager::Instance()->destroyObb(volId));
479}
480
Paul Crowley8915d622018-09-18 15:14:18 -0700481binder::Status VoldNativeService::fstrim(
482 int32_t fstrimFlags, const android::sp<android::os::IVoldTaskListener>& listener) {
Jeff Sharkey11c2d382017-09-11 10:32:01 -0600483 ENFORCE_UID(AID_SYSTEM);
484 ACQUIRE_LOCK;
485
Paul Crowley8915d622018-09-18 15:14:18 -0700486 std::thread([=]() { android::vold::Trim(listener); }).detach();
Jeff Sharkey11c2d382017-09-11 10:32:01 -0600487 return ok();
488}
489
Jin Qiana370c142017-10-17 15:41:45 -0700490binder::Status VoldNativeService::runIdleMaint(
Paul Crowley8915d622018-09-18 15:14:18 -0700491 const android::sp<android::os::IVoldTaskListener>& listener) {
Jin Qiana370c142017-10-17 15:41:45 -0700492 ENFORCE_UID(AID_SYSTEM);
493 ACQUIRE_LOCK;
494
Paul Crowley8915d622018-09-18 15:14:18 -0700495 std::thread([=]() { android::vold::RunIdleMaint(listener); }).detach();
Jin Qiana370c142017-10-17 15:41:45 -0700496 return ok();
497}
498
499binder::Status VoldNativeService::abortIdleMaint(
Paul Crowley8915d622018-09-18 15:14:18 -0700500 const android::sp<android::os::IVoldTaskListener>& listener) {
Jin Qiana370c142017-10-17 15:41:45 -0700501 ENFORCE_UID(AID_SYSTEM);
502 ACQUIRE_LOCK;
503
Paul Crowley8915d622018-09-18 15:14:18 -0700504 std::thread([=]() { android::vold::AbortIdleMaint(listener); }).detach();
Jin Qiana370c142017-10-17 15:41:45 -0700505 return ok();
506}
507
Jeff Sharkey11c2d382017-09-11 10:32:01 -0600508binder::Status VoldNativeService::mountAppFuse(int32_t uid, int32_t pid, int32_t mountId,
Paul Crowley8915d622018-09-18 15:14:18 -0700509 android::base::unique_fd* _aidl_return) {
Jeff Sharkey11c2d382017-09-11 10:32:01 -0600510 ENFORCE_UID(AID_SYSTEM);
511 ACQUIRE_LOCK;
512
513 return translate(VolumeManager::Instance()->mountAppFuse(uid, pid, mountId, _aidl_return));
514}
515
516binder::Status VoldNativeService::unmountAppFuse(int32_t uid, int32_t pid, int32_t mountId) {
517 ENFORCE_UID(AID_SYSTEM);
518 ACQUIRE_LOCK;
519
520 return translate(VolumeManager::Instance()->unmountAppFuse(uid, pid, mountId));
521}
522
Jeff Sharkey83b559c2017-09-12 16:30:52 -0600523binder::Status VoldNativeService::fdeCheckPassword(const std::string& password) {
524 ENFORCE_UID(AID_SYSTEM);
525 ACQUIRE_CRYPT_LOCK;
526
527 return translate(cryptfs_check_passwd(password.c_str()));
528}
529
530binder::Status VoldNativeService::fdeRestart() {
531 ENFORCE_UID(AID_SYSTEM);
532 ACQUIRE_CRYPT_LOCK;
533
534 // Spawn as thread so init can issue commands back to vold without
535 // causing deadlock, usually as a result of prep_data_fs.
536 std::thread(&cryptfs_restart).detach();
537 return ok();
538}
539
540binder::Status VoldNativeService::fdeComplete(int32_t* _aidl_return) {
541 ENFORCE_UID(AID_SYSTEM);
542 ACQUIRE_CRYPT_LOCK;
543
544 *_aidl_return = cryptfs_crypto_complete();
545 return ok();
546}
547
548static int fdeEnableInternal(int32_t passwordType, const std::string& password,
Paul Crowley8915d622018-09-18 15:14:18 -0700549 int32_t encryptionFlags) {
Jeff Sharkey83b559c2017-09-12 16:30:52 -0600550 bool noUi = (encryptionFlags & VoldNativeService::ENCRYPTION_FLAG_NO_UI) != 0;
551
Jeff Sharkey83b559c2017-09-12 16:30:52 -0600552 for (int tries = 0; tries < 2; ++tries) {
553 int rc;
554 if (passwordType == VoldNativeService::PASSWORD_TYPE_DEFAULT) {
Paul Lawrence7ee87cf2017-12-22 10:12:06 -0800555 rc = cryptfs_enable_default(noUi);
Jeff Sharkey83b559c2017-09-12 16:30:52 -0600556 } else {
Paul Lawrence7ee87cf2017-12-22 10:12:06 -0800557 rc = cryptfs_enable(passwordType, password.c_str(), noUi);
Jeff Sharkey83b559c2017-09-12 16:30:52 -0600558 }
559
560 if (rc == 0) {
561 return 0;
562 } else if (tries == 0) {
Jeff Sharkey3472e522017-10-06 18:02:53 -0600563 KillProcessesWithOpenFiles(DATA_MNT_POINT, SIGKILL);
Jeff Sharkey83b559c2017-09-12 16:30:52 -0600564 }
565 }
566
567 return -1;
568}
569
Paul Crowley8915d622018-09-18 15:14:18 -0700570binder::Status VoldNativeService::fdeEnable(int32_t passwordType, const std::string& password,
571 int32_t encryptionFlags) {
Jeff Sharkey83b559c2017-09-12 16:30:52 -0600572 ENFORCE_UID(AID_SYSTEM);
573 ACQUIRE_CRYPT_LOCK;
574
Paul Crowley0fd26262018-01-30 09:48:19 -0800575 LOG(DEBUG) << "fdeEnable(" << passwordType << ", *, " << encryptionFlags << ")";
Jeff Sharkey83b559c2017-09-12 16:30:52 -0600576 if (e4crypt_is_native()) {
Paul Crowley0fd26262018-01-30 09:48:19 -0800577 LOG(ERROR) << "e4crypt_is_native, fdeEnable invalid";
578 return error("e4crypt_is_native, fdeEnable invalid");
Jeff Sharkey83b559c2017-09-12 16:30:52 -0600579 }
Paul Crowley0fd26262018-01-30 09:48:19 -0800580 LOG(DEBUG) << "!e4crypt_is_native, spawning fdeEnableInternal";
Jeff Sharkey83b559c2017-09-12 16:30:52 -0600581
582 // Spawn as thread so init can issue commands back to vold without
583 // causing deadlock, usually as a result of prep_data_fs.
584 std::thread(&fdeEnableInternal, passwordType, password, encryptionFlags).detach();
585 return ok();
586}
587
588binder::Status VoldNativeService::fdeChangePassword(int32_t passwordType,
Paul Crowley8915d622018-09-18 15:14:18 -0700589 const std::string& password) {
Jeff Sharkey83b559c2017-09-12 16:30:52 -0600590 ENFORCE_UID(AID_SYSTEM);
591 ACQUIRE_CRYPT_LOCK;
592
593 return translate(cryptfs_changepw(passwordType, password.c_str()));
594}
595
596binder::Status VoldNativeService::fdeVerifyPassword(const std::string& password) {
597 ENFORCE_UID(AID_SYSTEM);
598 ACQUIRE_CRYPT_LOCK;
599
600 return translate(cryptfs_verify_passwd(password.c_str()));
601}
602
Paul Crowley8915d622018-09-18 15:14:18 -0700603binder::Status VoldNativeService::fdeGetField(const std::string& key, std::string* _aidl_return) {
Jeff Sharkey83b559c2017-09-12 16:30:52 -0600604 ENFORCE_UID(AID_SYSTEM);
605 ACQUIRE_CRYPT_LOCK;
606
607 char buf[PROPERTY_VALUE_MAX];
608 if (cryptfs_getfield(key.c_str(), buf, sizeof(buf)) != CRYPTO_GETFIELD_OK) {
609 return error(StringPrintf("Failed to read field %s", key.c_str()));
610 } else {
611 *_aidl_return = buf;
612 return ok();
613 }
614}
615
Paul Crowley8915d622018-09-18 15:14:18 -0700616binder::Status VoldNativeService::fdeSetField(const std::string& key, const std::string& value) {
Jeff Sharkey83b559c2017-09-12 16:30:52 -0600617 ENFORCE_UID(AID_SYSTEM);
618 ACQUIRE_CRYPT_LOCK;
619
620 return translate(cryptfs_setfield(key.c_str(), value.c_str()));
621}
622
623binder::Status VoldNativeService::fdeGetPasswordType(int32_t* _aidl_return) {
624 ENFORCE_UID(AID_SYSTEM);
625 ACQUIRE_CRYPT_LOCK;
626
627 *_aidl_return = cryptfs_get_password_type();
628 return ok();
629}
630
631binder::Status VoldNativeService::fdeGetPassword(std::string* _aidl_return) {
632 ENFORCE_UID(AID_SYSTEM);
633 ACQUIRE_CRYPT_LOCK;
634
635 const char* res = cryptfs_get_password();
636 if (res != nullptr) {
637 *_aidl_return = res;
638 }
639 return ok();
640}
641
642binder::Status VoldNativeService::fdeClearPassword() {
643 ENFORCE_UID(AID_SYSTEM);
644 ACQUIRE_CRYPT_LOCK;
645
646 cryptfs_clear_password();
647 return ok();
648}
649
650binder::Status VoldNativeService::fbeEnable() {
651 ENFORCE_UID(AID_SYSTEM);
652 ACQUIRE_CRYPT_LOCK;
653
654 return translateBool(e4crypt_initialize_global_de());
655}
656
657binder::Status VoldNativeService::mountDefaultEncrypted() {
658 ENFORCE_UID(AID_SYSTEM);
659 ACQUIRE_CRYPT_LOCK;
660
Paul Crowley0fd26262018-01-30 09:48:19 -0800661 if (!e4crypt_is_native()) {
Jeff Sharkey83b559c2017-09-12 16:30:52 -0600662 // Spawn as thread so init can issue commands back to vold without
663 // causing deadlock, usually as a result of prep_data_fs.
664 std::thread(&cryptfs_mount_default_encrypted).detach();
Jeff Sharkey83b559c2017-09-12 16:30:52 -0600665 }
Paul Crowley0fd26262018-01-30 09:48:19 -0800666 return ok();
Jeff Sharkey83b559c2017-09-12 16:30:52 -0600667}
668
669binder::Status VoldNativeService::initUser0() {
670 ENFORCE_UID(AID_SYSTEM);
671 ACQUIRE_CRYPT_LOCK;
672
673 return translateBool(e4crypt_init_user0());
674}
675
676binder::Status VoldNativeService::isConvertibleToFbe(bool* _aidl_return) {
677 ENFORCE_UID(AID_SYSTEM);
678 ACQUIRE_CRYPT_LOCK;
679
680 *_aidl_return = cryptfs_isConvertibleToFBE() != 0;
681 return ok();
682}
683
Paul Crowley0fd26262018-01-30 09:48:19 -0800684binder::Status VoldNativeService::mountFstab(const std::string& mountPoint) {
685 ENFORCE_UID(AID_SYSTEM);
686 ACQUIRE_LOCK;
687
688 return translateBool(e4crypt_mount_metadata_encrypted(mountPoint, false));
689}
690
691binder::Status VoldNativeService::encryptFstab(const std::string& mountPoint) {
692 ENFORCE_UID(AID_SYSTEM);
693 ACQUIRE_LOCK;
694
695 return translateBool(e4crypt_mount_metadata_encrypted(mountPoint, true));
696}
697
Paul Crowley8915d622018-09-18 15:14:18 -0700698binder::Status VoldNativeService::createUserKey(int32_t userId, int32_t userSerial, bool ephemeral) {
Jeff Sharkey83b559c2017-09-12 16:30:52 -0600699 ENFORCE_UID(AID_SYSTEM);
700 ACQUIRE_CRYPT_LOCK;
701
702 return translateBool(e4crypt_vold_create_user_key(userId, userSerial, ephemeral));
703}
704
705binder::Status VoldNativeService::destroyUserKey(int32_t userId) {
706 ENFORCE_UID(AID_SYSTEM);
707 ACQUIRE_CRYPT_LOCK;
708
709 return translateBool(e4crypt_destroy_user_key(userId));
710}
711
712binder::Status VoldNativeService::addUserKeyAuth(int32_t userId, int32_t userSerial,
Paul Crowley8915d622018-09-18 15:14:18 -0700713 const std::string& token,
714 const std::string& secret) {
Jeff Sharkey83b559c2017-09-12 16:30:52 -0600715 ENFORCE_UID(AID_SYSTEM);
716 ACQUIRE_CRYPT_LOCK;
717
Paul Crowley3b71fc52017-10-09 10:55:21 -0700718 return translateBool(e4crypt_add_user_key_auth(userId, userSerial, token, secret));
Jeff Sharkey83b559c2017-09-12 16:30:52 -0600719}
720
721binder::Status VoldNativeService::fixateNewestUserKeyAuth(int32_t userId) {
722 ENFORCE_UID(AID_SYSTEM);
723 ACQUIRE_CRYPT_LOCK;
724
725 return translateBool(e4crypt_fixate_newest_user_key_auth(userId));
726}
727
728binder::Status VoldNativeService::unlockUserKey(int32_t userId, int32_t userSerial,
Paul Crowley8915d622018-09-18 15:14:18 -0700729 const std::string& token,
730 const std::string& secret) {
Jeff Sharkey83b559c2017-09-12 16:30:52 -0600731 ENFORCE_UID(AID_SYSTEM);
732 ACQUIRE_CRYPT_LOCK;
733
Paul Crowley3b71fc52017-10-09 10:55:21 -0700734 return translateBool(e4crypt_unlock_user_key(userId, userSerial, token, secret));
Jeff Sharkey83b559c2017-09-12 16:30:52 -0600735}
736
737binder::Status VoldNativeService::lockUserKey(int32_t userId) {
738 ENFORCE_UID(AID_SYSTEM);
739 ACQUIRE_CRYPT_LOCK;
740
741 return translateBool(e4crypt_lock_user_key(userId));
742}
743
744binder::Status VoldNativeService::prepareUserStorage(const std::unique_ptr<std::string>& uuid,
Paul Crowley8915d622018-09-18 15:14:18 -0700745 int32_t userId, int32_t userSerial,
746 int32_t flags) {
Jeff Sharkey83b559c2017-09-12 16:30:52 -0600747 ENFORCE_UID(AID_SYSTEM);
Paul Crowley3b71fc52017-10-09 10:55:21 -0700748 std::string empty_string = "";
749 auto uuid_ = uuid ? *uuid : empty_string;
Paul Crowley06f762d2017-10-16 10:59:51 -0700750 CHECK_ARGUMENT_HEX(uuid_);
751
752 ACQUIRE_CRYPT_LOCK;
Jeff Sharkey83b559c2017-09-12 16:30:52 -0600753 return translateBool(e4crypt_prepare_user_storage(uuid_, userId, userSerial, flags));
754}
755
756binder::Status VoldNativeService::destroyUserStorage(const std::unique_ptr<std::string>& uuid,
Paul Crowley8915d622018-09-18 15:14:18 -0700757 int32_t userId, int32_t flags) {
Jeff Sharkey83b559c2017-09-12 16:30:52 -0600758 ENFORCE_UID(AID_SYSTEM);
Paul Crowley3b71fc52017-10-09 10:55:21 -0700759 std::string empty_string = "";
760 auto uuid_ = uuid ? *uuid : empty_string;
Paul Crowley06f762d2017-10-16 10:59:51 -0700761 CHECK_ARGUMENT_HEX(uuid_);
762
763 ACQUIRE_CRYPT_LOCK;
Jeff Sharkey83b559c2017-09-12 16:30:52 -0600764 return translateBool(e4crypt_destroy_user_storage(uuid_, userId, flags));
765}
766
Jeff Sharkey068c6be2017-09-06 13:47:40 -0600767} // namespace vold
768} // namespace android