blob: f8ed61c9ae127ca83c608b803f4f935d186dba41 [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
Daniel Rosenberg4f684712018-08-28 01:58:49 -070027#include "Checkpoint.h"
Eric Biggersa701c452018-10-23 13:06:55 -070028#include "FsCrypt.h"
Jeff Sharkey83b559c2017-09-12 16:30:52 -060029#include "MetadataCrypt.h"
Paul Crowleyedf7a4e2018-09-18 15:14:18 -070030#include "cryptfs.h"
Songchun Fanab55cec2019-12-02 10:50:12 -080031#include "incfs_ndk.h"
Jeff Sharkey83b559c2017-09-12 16:30:52 -060032
Jeff Sharkey068c6be2017-09-06 13:47:40 -060033#include <fstream>
Jeff Sharkey01a0e7f2017-10-17 16:06:32 -060034#include <thread>
Jeff Sharkey068c6be2017-09-06 13:47:40 -060035
36#include <android-base/logging.h>
37#include <android-base/stringprintf.h>
38#include <android-base/strings.h>
Jeff Sharkey11c2d382017-09-11 10:32:01 -060039#include <fs_mgr.h>
Eric Biggersa701c452018-10-23 13:06:55 -070040#include <fscrypt/fscrypt.h>
Jeff Sharkey068c6be2017-09-06 13:47:40 -060041#include <private/android_filesystem_config.h>
Jeff Sharkey67b8c492017-09-21 17:08:43 -060042#include <utils/Trace.h>
Jeff Sharkey068c6be2017-09-06 13:47:40 -060043
Jeff Sharkey068c6be2017-09-06 13:47:40 -060044using android::base::StringPrintf;
45using std::endl;
46
47namespace android {
48namespace vold {
49
50namespace {
51
52constexpr const char* kDump = "android.permission.DUMP";
53
54static binder::Status ok() {
55 return binder::Status::ok();
56}
57
58static binder::Status exception(uint32_t code, const std::string& msg) {
59 return binder::Status::fromExceptionCode(code, String8(msg.c_str()));
60}
61
Jeff Sharkey9462bdd2017-09-07 15:27:28 -060062static binder::Status error(const std::string& msg) {
63 PLOG(ERROR) << msg;
64 return binder::Status::fromServiceSpecificError(errno, String8(msg.c_str()));
65}
66
Jeff Sharkey83b559c2017-09-12 16:30:52 -060067static binder::Status translate(int status) {
Jeff Sharkey9462bdd2017-09-07 15:27:28 -060068 if (status == 0) {
69 return binder::Status::ok();
70 } else {
Jeff Sharkey11c2d382017-09-11 10:32:01 -060071 return binder::Status::fromServiceSpecificError(status);
Jeff Sharkey9462bdd2017-09-07 15:27:28 -060072 }
73}
74
Jeff Sharkey83b559c2017-09-12 16:30:52 -060075static binder::Status translateBool(bool status) {
76 if (status) {
77 return binder::Status::ok();
78 } else {
79 return binder::Status::fromServiceSpecificError(status);
80 }
81}
82
Jeff Sharkey068c6be2017-09-06 13:47:40 -060083binder::Status checkPermission(const char* permission) {
84 pid_t pid;
85 uid_t uid;
86
87 if (checkCallingPermission(String16(permission), reinterpret_cast<int32_t*>(&pid),
Paul Crowleyedf7a4e2018-09-18 15:14:18 -070088 reinterpret_cast<int32_t*>(&uid))) {
Jeff Sharkey068c6be2017-09-06 13:47:40 -060089 return ok();
90 } else {
91 return exception(binder::Status::EX_SECURITY,
Paul Crowleyedf7a4e2018-09-18 15:14:18 -070092 StringPrintf("UID %d / PID %d lacks permission %s", uid, pid, permission));
Jeff Sharkey068c6be2017-09-06 13:47:40 -060093 }
94}
95
Nikita Ioffe5d85bf32019-10-23 17:19:13 +010096binder::Status checkUidOrRoot(uid_t expectedUid) {
Jeff Sharkey068c6be2017-09-06 13:47:40 -060097 uid_t uid = IPCThreadState::self()->getCallingUid();
98 if (uid == expectedUid || uid == AID_ROOT) {
99 return ok();
100 } else {
101 return exception(binder::Status::EX_SECURITY,
Paul Crowleyedf7a4e2018-09-18 15:14:18 -0700102 StringPrintf("UID %d is not expected UID %d", uid, expectedUid));
Jeff Sharkey068c6be2017-09-06 13:47:40 -0600103 }
104}
105
Jeff Sharkeyec4fda22017-09-12 13:19:24 -0600106binder::Status checkArgumentId(const std::string& id) {
107 if (id.empty()) {
108 return exception(binder::Status::EX_ILLEGAL_ARGUMENT, "Missing ID");
109 }
110 for (const char& c : id) {
Zima438b242019-09-25 14:37:38 +0100111 if (!std::isalnum(c) && c != ':' && c != ',' && c != ';') {
Jeff Sharkeyec4fda22017-09-12 13:19:24 -0600112 return exception(binder::Status::EX_ILLEGAL_ARGUMENT,
Paul Crowleyedf7a4e2018-09-18 15:14:18 -0700113 StringPrintf("ID %s is malformed", id.c_str()));
Jeff Sharkeyec4fda22017-09-12 13:19:24 -0600114 }
115 }
116 return ok();
117}
118
119binder::Status checkArgumentPath(const std::string& path) {
120 if (path.empty()) {
121 return exception(binder::Status::EX_ILLEGAL_ARGUMENT, "Missing path");
122 }
123 if (path[0] != '/') {
124 return exception(binder::Status::EX_ILLEGAL_ARGUMENT,
Paul Crowleyedf7a4e2018-09-18 15:14:18 -0700125 StringPrintf("Path %s is relative", path.c_str()));
Jeff Sharkeyec4fda22017-09-12 13:19:24 -0600126 }
Jeff Sharkey01a0e7f2017-10-17 16:06:32 -0600127 if ((path + '/').find("/../") != std::string::npos) {
128 return exception(binder::Status::EX_ILLEGAL_ARGUMENT,
Paul Crowleyedf7a4e2018-09-18 15:14:18 -0700129 StringPrintf("Path %s is shady", path.c_str()));
Jeff Sharkey01a0e7f2017-10-17 16:06:32 -0600130 }
Jeff Sharkeyec4fda22017-09-12 13:19:24 -0600131 for (const char& c : path) {
132 if (c == '\0' || c == '\n') {
133 return exception(binder::Status::EX_ILLEGAL_ARGUMENT,
Paul Crowleyedf7a4e2018-09-18 15:14:18 -0700134 StringPrintf("Path %s is malformed", path.c_str()));
Jeff Sharkeyec4fda22017-09-12 13:19:24 -0600135 }
136 }
137 return ok();
138}
139
140binder::Status checkArgumentHex(const std::string& hex) {
141 // Empty hex strings are allowed
142 for (const char& c : hex) {
143 if (!std::isxdigit(c) && c != ':' && c != '-') {
144 return exception(binder::Status::EX_ILLEGAL_ARGUMENT,
Paul Crowleyedf7a4e2018-09-18 15:14:18 -0700145 StringPrintf("Hex %s is malformed", hex.c_str()));
Jeff Sharkeyec4fda22017-09-12 13:19:24 -0600146 }
147 }
148 return ok();
149}
150
Nikita Ioffe5d85bf32019-10-23 17:19:13 +0100151#define ENFORCE_SYSTEM_OR_ROOT \
152 { \
153 binder::Status status = checkUidOrRoot(AID_SYSTEM); \
154 if (!status.isOk()) { \
155 return status; \
156 } \
Paul Crowleyedf7a4e2018-09-18 15:14:18 -0700157 }
Jeff Sharkey068c6be2017-09-06 13:47:40 -0600158
Paul Crowleyedf7a4e2018-09-18 15:14:18 -0700159#define CHECK_ARGUMENT_ID(id) \
160 { \
161 binder::Status status = checkArgumentId((id)); \
162 if (!status.isOk()) { \
163 return status; \
164 } \
165 }
Jeff Sharkeyec4fda22017-09-12 13:19:24 -0600166
Paul Crowleyedf7a4e2018-09-18 15:14:18 -0700167#define CHECK_ARGUMENT_PATH(path) \
168 { \
169 binder::Status status = checkArgumentPath((path)); \
170 if (!status.isOk()) { \
171 return status; \
172 } \
173 }
Jeff Sharkeyec4fda22017-09-12 13:19:24 -0600174
Paul Crowleyedf7a4e2018-09-18 15:14:18 -0700175#define CHECK_ARGUMENT_HEX(hex) \
176 { \
177 binder::Status status = checkArgumentHex((hex)); \
178 if (!status.isOk()) { \
179 return status; \
180 } \
181 }
Jeff Sharkeyec4fda22017-09-12 13:19:24 -0600182
Paul Crowleyedf7a4e2018-09-18 15:14:18 -0700183#define ACQUIRE_LOCK \
Jeff Sharkey67b8c492017-09-21 17:08:43 -0600184 std::lock_guard<std::mutex> lock(VolumeManager::Instance()->getLock()); \
185 ATRACE_CALL();
Jeff Sharkey83b559c2017-09-12 16:30:52 -0600186
Paul Crowleyedf7a4e2018-09-18 15:14:18 -0700187#define ACQUIRE_CRYPT_LOCK \
Jeff Sharkey67b8c492017-09-21 17:08:43 -0600188 std::lock_guard<std::mutex> lock(VolumeManager::Instance()->getCryptLock()); \
189 ATRACE_CALL();
Jeff Sharkey9462bdd2017-09-07 15:27:28 -0600190
Jeff Sharkey068c6be2017-09-06 13:47:40 -0600191} // namespace
192
193status_t VoldNativeService::start() {
194 IPCThreadState::self()->disableBackgroundScheduling(true);
195 status_t ret = BinderService<VoldNativeService>::publish();
196 if (ret != android::OK) {
197 return ret;
198 }
199 sp<ProcessState> ps(ProcessState::self());
200 ps->startThreadPool();
201 ps->giveThreadPoolName();
202 return android::OK;
203}
204
Paul Crowleyedf7a4e2018-09-18 15:14:18 -0700205status_t VoldNativeService::dump(int fd, const Vector<String16>& /* args */) {
Jeff Sharkey068c6be2017-09-06 13:47:40 -0600206 auto out = std::fstream(StringPrintf("/proc/self/fd/%d", fd));
207 const binder::Status dump_permission = checkPermission(kDump);
208 if (!dump_permission.isOk()) {
209 out << dump_permission.toString8() << endl;
210 return PERMISSION_DENIED;
211 }
212
Jeff Sharkey9462bdd2017-09-07 15:27:28 -0600213 ACQUIRE_LOCK;
Jeff Sharkey068c6be2017-09-06 13:47:40 -0600214 out << "vold is happy!" << endl;
215 out.flush();
Jeff Sharkey068c6be2017-09-06 13:47:40 -0600216 return NO_ERROR;
217}
218
Jeff Sharkey814e9d32017-09-13 11:49:44 -0600219binder::Status VoldNativeService::setListener(
Paul Crowleyedf7a4e2018-09-18 15:14:18 -0700220 const android::sp<android::os::IVoldListener>& listener) {
Nikita Ioffe5d85bf32019-10-23 17:19:13 +0100221 ENFORCE_SYSTEM_OR_ROOT;
Jeff Sharkey814e9d32017-09-13 11:49:44 -0600222 ACQUIRE_LOCK;
223
224 VolumeManager::Instance()->setListener(listener);
225 return ok();
226}
227
Jeff Sharkeycbe69fc2017-09-15 16:50:28 -0600228binder::Status VoldNativeService::monitor() {
Nikita Ioffe5d85bf32019-10-23 17:19:13 +0100229 ENFORCE_SYSTEM_OR_ROOT;
Jeff Sharkeycbe69fc2017-09-15 16:50:28 -0600230
231 // Simply acquire/release each lock for watchdog
Paul Crowleyedf7a4e2018-09-18 15:14:18 -0700232 { ACQUIRE_LOCK; }
233 { ACQUIRE_CRYPT_LOCK; }
Jeff Sharkeycbe69fc2017-09-15 16:50:28 -0600234
235 return ok();
236}
237
Jeff Sharkey068c6be2017-09-06 13:47:40 -0600238binder::Status VoldNativeService::reset() {
Nikita Ioffe5d85bf32019-10-23 17:19:13 +0100239 ENFORCE_SYSTEM_OR_ROOT;
Jeff Sharkey9462bdd2017-09-07 15:27:28 -0600240 ACQUIRE_LOCK;
Jeff Sharkey068c6be2017-09-06 13:47:40 -0600241
Jeff Sharkey9462bdd2017-09-07 15:27:28 -0600242 return translate(VolumeManager::Instance()->reset());
243}
244
245binder::Status VoldNativeService::shutdown() {
Nikita Ioffe5d85bf32019-10-23 17:19:13 +0100246 ENFORCE_SYSTEM_OR_ROOT;
Jeff Sharkey9462bdd2017-09-07 15:27:28 -0600247 ACQUIRE_LOCK;
248
249 return translate(VolumeManager::Instance()->shutdown());
250}
251
Jeff Sharkey9462bdd2017-09-07 15:27:28 -0600252binder::Status VoldNativeService::onUserAdded(int32_t userId, int32_t userSerial) {
Nikita Ioffe5d85bf32019-10-23 17:19:13 +0100253 ENFORCE_SYSTEM_OR_ROOT;
Jeff Sharkey9462bdd2017-09-07 15:27:28 -0600254 ACQUIRE_LOCK;
255
256 return translate(VolumeManager::Instance()->onUserAdded(userId, userSerial));
257}
258
259binder::Status VoldNativeService::onUserRemoved(int32_t userId) {
Nikita Ioffe5d85bf32019-10-23 17:19:13 +0100260 ENFORCE_SYSTEM_OR_ROOT;
Jeff Sharkey9462bdd2017-09-07 15:27:28 -0600261 ACQUIRE_LOCK;
262
263 return translate(VolumeManager::Instance()->onUserRemoved(userId));
264}
265
Sudheer Shanka5fceb482019-04-29 10:46:35 -0700266binder::Status VoldNativeService::onUserStarted(int32_t userId) {
Nikita Ioffe5d85bf32019-10-23 17:19:13 +0100267 ENFORCE_SYSTEM_OR_ROOT;
Jeff Sharkey9462bdd2017-09-07 15:27:28 -0600268 ACQUIRE_LOCK;
269
Sudheer Shanka5fceb482019-04-29 10:46:35 -0700270 return translate(VolumeManager::Instance()->onUserStarted(userId));
Jeff Sharkey9462bdd2017-09-07 15:27:28 -0600271}
272
273binder::Status VoldNativeService::onUserStopped(int32_t userId) {
Nikita Ioffe5d85bf32019-10-23 17:19:13 +0100274 ENFORCE_SYSTEM_OR_ROOT;
Jeff Sharkey9462bdd2017-09-07 15:27:28 -0600275 ACQUIRE_LOCK;
276
277 return translate(VolumeManager::Instance()->onUserStopped(userId));
278}
279
Sudheer Shankad484aa92018-07-31 10:07:34 -0700280binder::Status VoldNativeService::addAppIds(const std::vector<std::string>& packageNames,
Paul Crowleyedf7a4e2018-09-18 15:14:18 -0700281 const std::vector<int32_t>& appIds) {
Sudheer Shanka5fceb482019-04-29 10:46:35 -0700282 return ok();
Sudheer Shankad484aa92018-07-31 10:07:34 -0700283}
284
Sudheer Shankad484aa92018-07-31 10:07:34 -0700285binder::Status VoldNativeService::addSandboxIds(const std::vector<int32_t>& appIds,
Paul Crowleyedf7a4e2018-09-18 15:14:18 -0700286 const std::vector<std::string>& sandboxIds) {
Sudheer Shanka5fceb482019-04-29 10:46:35 -0700287 return ok();
Sudheer Shankad484aa92018-07-31 10:07:34 -0700288}
289
Jeff Sharkey401b2602017-12-14 22:15:20 -0700290binder::Status VoldNativeService::onSecureKeyguardStateChanged(bool isShowing) {
Nikita Ioffe5d85bf32019-10-23 17:19:13 +0100291 ENFORCE_SYSTEM_OR_ROOT;
Jeff Sharkey401b2602017-12-14 22:15:20 -0700292 ACQUIRE_LOCK;
293
294 return translate(VolumeManager::Instance()->onSecureKeyguardStateChanged(isShowing));
295}
296
Jeff Sharkey11c2d382017-09-11 10:32:01 -0600297binder::Status VoldNativeService::partition(const std::string& diskId, int32_t partitionType,
Paul Crowleyedf7a4e2018-09-18 15:14:18 -0700298 int32_t ratio) {
Nikita Ioffe5d85bf32019-10-23 17:19:13 +0100299 ENFORCE_SYSTEM_OR_ROOT;
Jeff Sharkeyec4fda22017-09-12 13:19:24 -0600300 CHECK_ARGUMENT_ID(diskId);
Jeff Sharkey9462bdd2017-09-07 15:27:28 -0600301 ACQUIRE_LOCK;
302
303 auto disk = VolumeManager::Instance()->findDisk(diskId);
304 if (disk == nullptr) {
305 return error("Failed to find disk " + diskId);
306 }
307 switch (partitionType) {
Paul Crowleyedf7a4e2018-09-18 15:14:18 -0700308 case PARTITION_TYPE_PUBLIC:
309 return translate(disk->partitionPublic());
310 case PARTITION_TYPE_PRIVATE:
311 return translate(disk->partitionPrivate());
312 case PARTITION_TYPE_MIXED:
313 return translate(disk->partitionMixed(ratio));
314 default:
315 return error("Unknown type " + std::to_string(partitionType));
Jeff Sharkey9462bdd2017-09-07 15:27:28 -0600316 }
317}
318
Jeff Sharkey3ce18252017-10-24 11:08:45 -0600319binder::Status VoldNativeService::forgetPartition(const std::string& partGuid,
Paul Crowleyedf7a4e2018-09-18 15:14:18 -0700320 const std::string& fsUuid) {
Nikita Ioffe5d85bf32019-10-23 17:19:13 +0100321 ENFORCE_SYSTEM_OR_ROOT;
Jeff Sharkeyec4fda22017-09-12 13:19:24 -0600322 CHECK_ARGUMENT_HEX(partGuid);
Jeff Sharkey3ce18252017-10-24 11:08:45 -0600323 CHECK_ARGUMENT_HEX(fsUuid);
Jeff Sharkey9462bdd2017-09-07 15:27:28 -0600324 ACQUIRE_LOCK;
325
Jeff Sharkey3ce18252017-10-24 11:08:45 -0600326 return translate(VolumeManager::Instance()->forgetPartition(partGuid, fsUuid));
Jeff Sharkey9462bdd2017-09-07 15:27:28 -0600327}
328
Zim5048b4b2019-11-19 09:16:03 +0000329binder::Status VoldNativeService::mount(
330 const std::string& volId, int32_t mountFlags, int32_t mountUserId,
331 const android::sp<android::os::IVoldMountCallback>& callback) {
Nikita Ioffe5d85bf32019-10-23 17:19:13 +0100332 ENFORCE_SYSTEM_OR_ROOT;
Jeff Sharkeyec4fda22017-09-12 13:19:24 -0600333 CHECK_ARGUMENT_ID(volId);
Jeff Sharkey9462bdd2017-09-07 15:27:28 -0600334 ACQUIRE_LOCK;
335
336 auto vol = VolumeManager::Instance()->findVolume(volId);
337 if (vol == nullptr) {
338 return error("Failed to find volume " + volId);
339 }
340
341 vol->setMountFlags(mountFlags);
342 vol->setMountUserId(mountUserId);
343
Zim5048b4b2019-11-19 09:16:03 +0000344 vol->setMountCallback(callback);
Jeff Sharkey9462bdd2017-09-07 15:27:28 -0600345 int res = vol->mount();
Zim5048b4b2019-11-19 09:16:03 +0000346 vol->setMountCallback(nullptr);
347
Sudheer Shanka40ab6742018-09-18 13:07:45 -0700348 if (res != OK) {
349 return translate(res);
Jeff Sharkey9462bdd2017-09-07 15:27:28 -0600350 }
Zim3623a212019-07-19 16:46:53 +0100351
Sudheer Shanka40ab6742018-09-18 13:07:45 -0700352 if ((mountFlags & MOUNT_FLAG_PRIMARY) != 0) {
353 res = VolumeManager::Instance()->setPrimary(vol);
354 if (res != OK) {
355 return translate(res);
356 }
357 }
358 return translate(OK);
Jeff Sharkey9462bdd2017-09-07 15:27:28 -0600359}
360
361binder::Status VoldNativeService::unmount(const std::string& volId) {
Nikita Ioffe5d85bf32019-10-23 17:19:13 +0100362 ENFORCE_SYSTEM_OR_ROOT;
Jeff Sharkeyec4fda22017-09-12 13:19:24 -0600363 CHECK_ARGUMENT_ID(volId);
Jeff Sharkey9462bdd2017-09-07 15:27:28 -0600364 ACQUIRE_LOCK;
365
366 auto vol = VolumeManager::Instance()->findVolume(volId);
367 if (vol == nullptr) {
368 return error("Failed to find volume " + volId);
369 }
370 return translate(vol->unmount());
371}
372
373binder::Status VoldNativeService::format(const std::string& volId, const std::string& fsType) {
Nikita Ioffe5d85bf32019-10-23 17:19:13 +0100374 ENFORCE_SYSTEM_OR_ROOT;
Jeff Sharkeyec4fda22017-09-12 13:19:24 -0600375 CHECK_ARGUMENT_ID(volId);
Jeff Sharkey9462bdd2017-09-07 15:27:28 -0600376 ACQUIRE_LOCK;
377
378 auto vol = VolumeManager::Instance()->findVolume(volId);
379 if (vol == nullptr) {
380 return error("Failed to find volume " + volId);
381 }
382 return translate(vol->format(fsType));
383}
384
Jeff Sharkey2048a282017-06-15 09:59:43 -0600385static binder::Status pathForVolId(const std::string& volId, std::string* path) {
Jeff Sharkey52f7a912017-09-15 12:57:44 -0600386 if (volId == "private" || volId == "null") {
Jeff Sharkey2048a282017-06-15 09:59:43 -0600387 *path = "/data";
Jeff Sharkey52f7a912017-09-15 12:57:44 -0600388 } else {
389 auto vol = VolumeManager::Instance()->findVolume(volId);
390 if (vol == nullptr) {
391 return error("Failed to find volume " + volId);
392 }
393 if (vol->getType() != VolumeBase::Type::kPrivate) {
394 return error("Volume " + volId + " not private");
395 }
396 if (vol->getState() != VolumeBase::State::kMounted) {
397 return error("Volume " + volId + " not mounted");
398 }
Jeff Sharkey2048a282017-06-15 09:59:43 -0600399 *path = vol->getPath();
400 if (path->empty()) {
401 return error("Volume " + volId + " missing path");
402 }
Jeff Sharkey52f7a912017-09-15 12:57:44 -0600403 }
Jeff Sharkey2048a282017-06-15 09:59:43 -0600404 return ok();
405}
Jeff Sharkey52f7a912017-09-15 12:57:44 -0600406
Jeff Sharkey2048a282017-06-15 09:59:43 -0600407binder::Status VoldNativeService::benchmark(
408 const std::string& volId, const android::sp<android::os::IVoldTaskListener>& listener) {
Nikita Ioffe5d85bf32019-10-23 17:19:13 +0100409 ENFORCE_SYSTEM_OR_ROOT;
Jeff Sharkey2048a282017-06-15 09:59:43 -0600410 CHECK_ARGUMENT_ID(volId);
411 ACQUIRE_LOCK;
412
413 std::string path;
414 auto status = pathForVolId(volId, &path);
415 if (!status.isOk()) return status;
Jeff Sharkey52f7a912017-09-15 12:57:44 -0600416
Paul Crowleyedf7a4e2018-09-18 15:14:18 -0700417 std::thread([=]() { android::vold::Benchmark(path, listener); }).detach();
Jeff Sharkey068c6be2017-09-06 13:47:40 -0600418 return ok();
419}
420
Jeff Sharkey2048a282017-06-15 09:59:43 -0600421binder::Status VoldNativeService::checkEncryption(const std::string& volId) {
Nikita Ioffe5d85bf32019-10-23 17:19:13 +0100422 ENFORCE_SYSTEM_OR_ROOT;
Jeff Sharkey2048a282017-06-15 09:59:43 -0600423 CHECK_ARGUMENT_ID(volId);
424 ACQUIRE_LOCK;
425
426 std::string path;
427 auto status = pathForVolId(volId, &path);
428 if (!status.isOk()) return status;
429 return translate(android::vold::CheckEncryption(path));
430}
431
Paul Crowleyedf7a4e2018-09-18 15:14:18 -0700432binder::Status VoldNativeService::moveStorage(
433 const std::string& fromVolId, const std::string& toVolId,
434 const android::sp<android::os::IVoldTaskListener>& listener) {
Nikita Ioffe5d85bf32019-10-23 17:19:13 +0100435 ENFORCE_SYSTEM_OR_ROOT;
Jeff Sharkeyec4fda22017-09-12 13:19:24 -0600436 CHECK_ARGUMENT_ID(fromVolId);
437 CHECK_ARGUMENT_ID(toVolId);
Jeff Sharkey9462bdd2017-09-07 15:27:28 -0600438 ACQUIRE_LOCK;
439
440 auto fromVol = VolumeManager::Instance()->findVolume(fromVolId);
441 auto toVol = VolumeManager::Instance()->findVolume(toVolId);
442 if (fromVol == nullptr) {
443 return error("Failed to find volume " + fromVolId);
444 } else if (toVol == nullptr) {
445 return error("Failed to find volume " + toVolId);
446 }
Jeff Sharkey01a0e7f2017-10-17 16:06:32 -0600447
Paul Crowleyedf7a4e2018-09-18 15:14:18 -0700448 std::thread([=]() { android::vold::MoveStorage(fromVol, toVol, listener); }).detach();
Jeff Sharkey9462bdd2017-09-07 15:27:28 -0600449 return ok();
450}
451
452binder::Status VoldNativeService::remountUid(int32_t uid, int32_t remountMode) {
Nikita Ioffe5d85bf32019-10-23 17:19:13 +0100453 ENFORCE_SYSTEM_OR_ROOT;
Jeff Sharkey9462bdd2017-09-07 15:27:28 -0600454 ACQUIRE_LOCK;
455
Sudheer Shanka817b9112018-12-13 17:40:28 -0800456 return translate(VolumeManager::Instance()->remountUid(uid, remountMode));
Jeff Sharkey9462bdd2017-09-07 15:27:28 -0600457}
458
Martijn Coenen13ff6682019-12-24 12:57:16 +0100459binder::Status VoldNativeService::setupAppDir(const std::string& path,
460 const std::string& appDirRoot, int32_t appUid) {
Nikita Ioffe5d85bf32019-10-23 17:19:13 +0100461 ENFORCE_SYSTEM_OR_ROOT;
Jeff Sharkeyec4fda22017-09-12 13:19:24 -0600462 CHECK_ARGUMENT_PATH(path);
Martijn Coenen13ff6682019-12-24 12:57:16 +0100463 CHECK_ARGUMENT_PATH(appDirRoot);
Jeff Sharkey9462bdd2017-09-07 15:27:28 -0600464 ACQUIRE_LOCK;
465
Martijn Coenen13ff6682019-12-24 12:57:16 +0100466 return translate(VolumeManager::Instance()->setupAppDir(path, appDirRoot, appUid));
Jeff Sharkey9462bdd2017-09-07 15:27:28 -0600467}
468
Jeff Sharkey11c2d382017-09-11 10:32:01 -0600469binder::Status VoldNativeService::createObb(const std::string& sourcePath,
Paul Crowleyedf7a4e2018-09-18 15:14:18 -0700470 const std::string& sourceKey, int32_t ownerGid,
471 std::string* _aidl_return) {
Nikita Ioffe5d85bf32019-10-23 17:19:13 +0100472 ENFORCE_SYSTEM_OR_ROOT;
Jeff Sharkeyec4fda22017-09-12 13:19:24 -0600473 CHECK_ARGUMENT_PATH(sourcePath);
474 CHECK_ARGUMENT_HEX(sourceKey);
Jeff Sharkey11c2d382017-09-11 10:32:01 -0600475 ACQUIRE_LOCK;
476
477 return translate(
Paul Crowleyedf7a4e2018-09-18 15:14:18 -0700478 VolumeManager::Instance()->createObb(sourcePath, sourceKey, ownerGid, _aidl_return));
Jeff Sharkey11c2d382017-09-11 10:32:01 -0600479}
480
481binder::Status VoldNativeService::destroyObb(const std::string& volId) {
Nikita Ioffe5d85bf32019-10-23 17:19:13 +0100482 ENFORCE_SYSTEM_OR_ROOT;
Jeff Sharkeyec4fda22017-09-12 13:19:24 -0600483 CHECK_ARGUMENT_ID(volId);
Jeff Sharkey11c2d382017-09-11 10:32:01 -0600484 ACQUIRE_LOCK;
485
486 return translate(VolumeManager::Instance()->destroyObb(volId));
487}
488
Risan8c9f3322018-10-29 08:52:56 +0900489binder::Status VoldNativeService::createStubVolume(
490 const std::string& sourcePath, const std::string& mountPath, const std::string& fsType,
491 const std::string& fsUuid, const std::string& fsLabel, std::string* _aidl_return) {
Nikita Ioffe5d85bf32019-10-23 17:19:13 +0100492 ENFORCE_SYSTEM_OR_ROOT;
Risan8c9f3322018-10-29 08:52:56 +0900493 CHECK_ARGUMENT_PATH(sourcePath);
494 CHECK_ARGUMENT_PATH(mountPath);
495 CHECK_ARGUMENT_HEX(fsUuid);
496 // Label limitation seems to be different between fs (including allowed characters), so checking
497 // is quite meaningless.
498 ACQUIRE_LOCK;
499
500 return translate(VolumeManager::Instance()->createStubVolume(sourcePath, mountPath, fsType,
501 fsUuid, fsLabel, _aidl_return));
502}
503
504binder::Status VoldNativeService::destroyStubVolume(const std::string& volId) {
Nikita Ioffe5d85bf32019-10-23 17:19:13 +0100505 ENFORCE_SYSTEM_OR_ROOT;
Risan8c9f3322018-10-29 08:52:56 +0900506 CHECK_ARGUMENT_ID(volId);
507 ACQUIRE_LOCK;
508
509 return translate(VolumeManager::Instance()->destroyStubVolume(volId));
510}
511
Paul Crowleyedf7a4e2018-09-18 15:14:18 -0700512binder::Status VoldNativeService::fstrim(
513 int32_t fstrimFlags, const android::sp<android::os::IVoldTaskListener>& listener) {
Nikita Ioffe5d85bf32019-10-23 17:19:13 +0100514 ENFORCE_SYSTEM_OR_ROOT;
Jeff Sharkey11c2d382017-09-11 10:32:01 -0600515 ACQUIRE_LOCK;
516
Paul Crowleyedf7a4e2018-09-18 15:14:18 -0700517 std::thread([=]() { android::vold::Trim(listener); }).detach();
Jeff Sharkey11c2d382017-09-11 10:32:01 -0600518 return ok();
519}
520
Jin Qiana370c142017-10-17 15:41:45 -0700521binder::Status VoldNativeService::runIdleMaint(
Paul Crowleyedf7a4e2018-09-18 15:14:18 -0700522 const android::sp<android::os::IVoldTaskListener>& listener) {
Nikita Ioffe5d85bf32019-10-23 17:19:13 +0100523 ENFORCE_SYSTEM_OR_ROOT;
Jin Qiana370c142017-10-17 15:41:45 -0700524 ACQUIRE_LOCK;
525
Paul Crowleyedf7a4e2018-09-18 15:14:18 -0700526 std::thread([=]() { android::vold::RunIdleMaint(listener); }).detach();
Jin Qiana370c142017-10-17 15:41:45 -0700527 return ok();
528}
529
530binder::Status VoldNativeService::abortIdleMaint(
Paul Crowleyedf7a4e2018-09-18 15:14:18 -0700531 const android::sp<android::os::IVoldTaskListener>& listener) {
Nikita Ioffe5d85bf32019-10-23 17:19:13 +0100532 ENFORCE_SYSTEM_OR_ROOT;
Jin Qiana370c142017-10-17 15:41:45 -0700533 ACQUIRE_LOCK;
534
Paul Crowleyedf7a4e2018-09-18 15:14:18 -0700535 std::thread([=]() { android::vold::AbortIdleMaint(listener); }).detach();
Jin Qiana370c142017-10-17 15:41:45 -0700536 return ok();
537}
538
Risan8f6198d2018-10-26 20:56:45 -0600539binder::Status VoldNativeService::mountAppFuse(int32_t uid, int32_t mountId,
Paul Crowleyedf7a4e2018-09-18 15:14:18 -0700540 android::base::unique_fd* _aidl_return) {
Nikita Ioffe5d85bf32019-10-23 17:19:13 +0100541 ENFORCE_SYSTEM_OR_ROOT;
Jeff Sharkey11c2d382017-09-11 10:32:01 -0600542 ACQUIRE_LOCK;
543
Risan8f6198d2018-10-26 20:56:45 -0600544 return translate(VolumeManager::Instance()->mountAppFuse(uid, mountId, _aidl_return));
Jeff Sharkey11c2d382017-09-11 10:32:01 -0600545}
546
Risan8f6198d2018-10-26 20:56:45 -0600547binder::Status VoldNativeService::unmountAppFuse(int32_t uid, int32_t mountId) {
Nikita Ioffe5d85bf32019-10-23 17:19:13 +0100548 ENFORCE_SYSTEM_OR_ROOT;
Jeff Sharkey11c2d382017-09-11 10:32:01 -0600549 ACQUIRE_LOCK;
550
Risan8f6198d2018-10-26 20:56:45 -0600551 return translate(VolumeManager::Instance()->unmountAppFuse(uid, mountId));
552}
553
554binder::Status VoldNativeService::openAppFuseFile(int32_t uid, int32_t mountId, int32_t fileId,
555 int32_t flags,
556 android::base::unique_fd* _aidl_return) {
Nikita Ioffe5d85bf32019-10-23 17:19:13 +0100557 ENFORCE_SYSTEM_OR_ROOT;
Risan8f6198d2018-10-26 20:56:45 -0600558 ACQUIRE_LOCK;
559
560 int fd = VolumeManager::Instance()->openAppFuseFile(uid, mountId, fileId, flags);
561 if (fd == -1) {
562 return error("Failed to open AppFuse file for uid: " + std::to_string(uid) +
563 " mountId: " + std::to_string(mountId) + " fileId: " + std::to_string(fileId) +
564 " flags: " + std::to_string(flags));
565 }
566
567 *_aidl_return = android::base::unique_fd(fd);
568 return ok();
Jeff Sharkey11c2d382017-09-11 10:32:01 -0600569}
570
Jeff Sharkey83b559c2017-09-12 16:30:52 -0600571binder::Status VoldNativeService::fdeCheckPassword(const std::string& password) {
Nikita Ioffe5d85bf32019-10-23 17:19:13 +0100572 ENFORCE_SYSTEM_OR_ROOT;
Jeff Sharkey83b559c2017-09-12 16:30:52 -0600573 ACQUIRE_CRYPT_LOCK;
574
575 return translate(cryptfs_check_passwd(password.c_str()));
576}
577
578binder::Status VoldNativeService::fdeRestart() {
Nikita Ioffe5d85bf32019-10-23 17:19:13 +0100579 ENFORCE_SYSTEM_OR_ROOT;
Jeff Sharkey83b559c2017-09-12 16:30:52 -0600580 ACQUIRE_CRYPT_LOCK;
581
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(&cryptfs_restart).detach();
585 return ok();
586}
587
588binder::Status VoldNativeService::fdeComplete(int32_t* _aidl_return) {
Nikita Ioffe5d85bf32019-10-23 17:19:13 +0100589 ENFORCE_SYSTEM_OR_ROOT;
Jeff Sharkey83b559c2017-09-12 16:30:52 -0600590 ACQUIRE_CRYPT_LOCK;
591
592 *_aidl_return = cryptfs_crypto_complete();
593 return ok();
594}
595
596static int fdeEnableInternal(int32_t passwordType, const std::string& password,
Paul Crowleyedf7a4e2018-09-18 15:14:18 -0700597 int32_t encryptionFlags) {
Jeff Sharkey83b559c2017-09-12 16:30:52 -0600598 bool noUi = (encryptionFlags & VoldNativeService::ENCRYPTION_FLAG_NO_UI) != 0;
599
Jeff Sharkey83b559c2017-09-12 16:30:52 -0600600 for (int tries = 0; tries < 2; ++tries) {
601 int rc;
602 if (passwordType == VoldNativeService::PASSWORD_TYPE_DEFAULT) {
Paul Lawrence7ee87cf2017-12-22 10:12:06 -0800603 rc = cryptfs_enable_default(noUi);
Jeff Sharkey83b559c2017-09-12 16:30:52 -0600604 } else {
Paul Lawrence7ee87cf2017-12-22 10:12:06 -0800605 rc = cryptfs_enable(passwordType, password.c_str(), noUi);
Jeff Sharkey83b559c2017-09-12 16:30:52 -0600606 }
607
608 if (rc == 0) {
609 return 0;
610 } else if (tries == 0) {
Jeff Sharkey3472e522017-10-06 18:02:53 -0600611 KillProcessesWithOpenFiles(DATA_MNT_POINT, SIGKILL);
Jeff Sharkey83b559c2017-09-12 16:30:52 -0600612 }
613 }
614
615 return -1;
616}
617
Paul Crowleyedf7a4e2018-09-18 15:14:18 -0700618binder::Status VoldNativeService::fdeEnable(int32_t passwordType, const std::string& password,
619 int32_t encryptionFlags) {
Nikita Ioffe5d85bf32019-10-23 17:19:13 +0100620 ENFORCE_SYSTEM_OR_ROOT;
Jeff Sharkey83b559c2017-09-12 16:30:52 -0600621 ACQUIRE_CRYPT_LOCK;
622
Paul Crowley0fd26262018-01-30 09:48:19 -0800623 LOG(DEBUG) << "fdeEnable(" << passwordType << ", *, " << encryptionFlags << ")";
Eric Biggersa701c452018-10-23 13:06:55 -0700624 if (fscrypt_is_native()) {
625 LOG(ERROR) << "fscrypt_is_native, fdeEnable invalid";
626 return error("fscrypt_is_native, fdeEnable invalid");
Jeff Sharkey83b559c2017-09-12 16:30:52 -0600627 }
Eric Biggersa701c452018-10-23 13:06:55 -0700628 LOG(DEBUG) << "!fscrypt_is_native, spawning fdeEnableInternal";
Jeff Sharkey83b559c2017-09-12 16:30:52 -0600629
630 // Spawn as thread so init can issue commands back to vold without
631 // causing deadlock, usually as a result of prep_data_fs.
632 std::thread(&fdeEnableInternal, passwordType, password, encryptionFlags).detach();
633 return ok();
634}
635
636binder::Status VoldNativeService::fdeChangePassword(int32_t passwordType,
Paul Crowleyedf7a4e2018-09-18 15:14:18 -0700637 const std::string& password) {
Nikita Ioffe5d85bf32019-10-23 17:19:13 +0100638 ENFORCE_SYSTEM_OR_ROOT;
Jeff Sharkey83b559c2017-09-12 16:30:52 -0600639 ACQUIRE_CRYPT_LOCK;
640
641 return translate(cryptfs_changepw(passwordType, password.c_str()));
642}
643
644binder::Status VoldNativeService::fdeVerifyPassword(const std::string& password) {
Nikita Ioffe5d85bf32019-10-23 17:19:13 +0100645 ENFORCE_SYSTEM_OR_ROOT;
Jeff Sharkey83b559c2017-09-12 16:30:52 -0600646 ACQUIRE_CRYPT_LOCK;
647
648 return translate(cryptfs_verify_passwd(password.c_str()));
649}
650
Paul Crowleyedf7a4e2018-09-18 15:14:18 -0700651binder::Status VoldNativeService::fdeGetField(const std::string& key, std::string* _aidl_return) {
Nikita Ioffe5d85bf32019-10-23 17:19:13 +0100652 ENFORCE_SYSTEM_OR_ROOT;
Jeff Sharkey83b559c2017-09-12 16:30:52 -0600653 ACQUIRE_CRYPT_LOCK;
654
655 char buf[PROPERTY_VALUE_MAX];
656 if (cryptfs_getfield(key.c_str(), buf, sizeof(buf)) != CRYPTO_GETFIELD_OK) {
657 return error(StringPrintf("Failed to read field %s", key.c_str()));
658 } else {
659 *_aidl_return = buf;
660 return ok();
661 }
662}
663
Paul Crowleyedf7a4e2018-09-18 15:14:18 -0700664binder::Status VoldNativeService::fdeSetField(const std::string& key, const std::string& value) {
Nikita Ioffe5d85bf32019-10-23 17:19:13 +0100665 ENFORCE_SYSTEM_OR_ROOT;
Jeff Sharkey83b559c2017-09-12 16:30:52 -0600666 ACQUIRE_CRYPT_LOCK;
667
668 return translate(cryptfs_setfield(key.c_str(), value.c_str()));
669}
670
671binder::Status VoldNativeService::fdeGetPasswordType(int32_t* _aidl_return) {
Nikita Ioffe5d85bf32019-10-23 17:19:13 +0100672 ENFORCE_SYSTEM_OR_ROOT;
Jeff Sharkey83b559c2017-09-12 16:30:52 -0600673 ACQUIRE_CRYPT_LOCK;
674
675 *_aidl_return = cryptfs_get_password_type();
676 return ok();
677}
678
679binder::Status VoldNativeService::fdeGetPassword(std::string* _aidl_return) {
Nikita Ioffe5d85bf32019-10-23 17:19:13 +0100680 ENFORCE_SYSTEM_OR_ROOT;
Jeff Sharkey83b559c2017-09-12 16:30:52 -0600681 ACQUIRE_CRYPT_LOCK;
682
683 const char* res = cryptfs_get_password();
684 if (res != nullptr) {
685 *_aidl_return = res;
686 }
687 return ok();
688}
689
690binder::Status VoldNativeService::fdeClearPassword() {
Nikita Ioffe5d85bf32019-10-23 17:19:13 +0100691 ENFORCE_SYSTEM_OR_ROOT;
Jeff Sharkey83b559c2017-09-12 16:30:52 -0600692 ACQUIRE_CRYPT_LOCK;
693
694 cryptfs_clear_password();
695 return ok();
696}
697
698binder::Status VoldNativeService::fbeEnable() {
Nikita Ioffe5d85bf32019-10-23 17:19:13 +0100699 ENFORCE_SYSTEM_OR_ROOT;
Jeff Sharkey83b559c2017-09-12 16:30:52 -0600700 ACQUIRE_CRYPT_LOCK;
701
Paul Crowleyc8a3ef32019-09-11 15:00:08 -0700702 return translateBool(fscrypt_initialize_systemwide_keys());
Jeff Sharkey83b559c2017-09-12 16:30:52 -0600703}
704
705binder::Status VoldNativeService::mountDefaultEncrypted() {
Nikita Ioffe5d85bf32019-10-23 17:19:13 +0100706 ENFORCE_SYSTEM_OR_ROOT;
Jeff Sharkey83b559c2017-09-12 16:30:52 -0600707 ACQUIRE_CRYPT_LOCK;
708
Eric Biggersa701c452018-10-23 13:06:55 -0700709 if (!fscrypt_is_native()) {
Jeff Sharkey83b559c2017-09-12 16:30:52 -0600710 // Spawn as thread so init can issue commands back to vold without
711 // causing deadlock, usually as a result of prep_data_fs.
712 std::thread(&cryptfs_mount_default_encrypted).detach();
Jeff Sharkey83b559c2017-09-12 16:30:52 -0600713 }
Paul Crowley0fd26262018-01-30 09:48:19 -0800714 return ok();
Jeff Sharkey83b559c2017-09-12 16:30:52 -0600715}
716
717binder::Status VoldNativeService::initUser0() {
Nikita Ioffe5d85bf32019-10-23 17:19:13 +0100718 ENFORCE_SYSTEM_OR_ROOT;
Jeff Sharkey83b559c2017-09-12 16:30:52 -0600719 ACQUIRE_CRYPT_LOCK;
720
Eric Biggersa701c452018-10-23 13:06:55 -0700721 return translateBool(fscrypt_init_user0());
Jeff Sharkey83b559c2017-09-12 16:30:52 -0600722}
723
724binder::Status VoldNativeService::isConvertibleToFbe(bool* _aidl_return) {
Nikita Ioffe5d85bf32019-10-23 17:19:13 +0100725 ENFORCE_SYSTEM_OR_ROOT;
Jeff Sharkey83b559c2017-09-12 16:30:52 -0600726 ACQUIRE_CRYPT_LOCK;
727
728 *_aidl_return = cryptfs_isConvertibleToFBE() != 0;
729 return ok();
730}
731
Paul Lawrence236e5e82019-06-25 14:44:33 -0700732binder::Status VoldNativeService::mountFstab(const std::string& blkDevice,
733 const std::string& mountPoint) {
Nikita Ioffe5d85bf32019-10-23 17:19:13 +0100734 ENFORCE_SYSTEM_OR_ROOT;
Paul Crowley0fd26262018-01-30 09:48:19 -0800735 ACQUIRE_LOCK;
736
Paul Lawrence236e5e82019-06-25 14:44:33 -0700737 return translateBool(fscrypt_mount_metadata_encrypted(blkDevice, mountPoint, false));
Paul Crowley0fd26262018-01-30 09:48:19 -0800738}
739
Paul Lawrence236e5e82019-06-25 14:44:33 -0700740binder::Status VoldNativeService::encryptFstab(const std::string& blkDevice,
741 const std::string& mountPoint) {
Nikita Ioffe5d85bf32019-10-23 17:19:13 +0100742 ENFORCE_SYSTEM_OR_ROOT;
Paul Crowley0fd26262018-01-30 09:48:19 -0800743 ACQUIRE_LOCK;
744
Paul Lawrence236e5e82019-06-25 14:44:33 -0700745 return translateBool(fscrypt_mount_metadata_encrypted(blkDevice, mountPoint, true));
Paul Crowley0fd26262018-01-30 09:48:19 -0800746}
747
Paul Crowleyedf7a4e2018-09-18 15:14:18 -0700748binder::Status VoldNativeService::createUserKey(int32_t userId, int32_t userSerial, bool ephemeral) {
Nikita Ioffe5d85bf32019-10-23 17:19:13 +0100749 ENFORCE_SYSTEM_OR_ROOT;
Jeff Sharkey83b559c2017-09-12 16:30:52 -0600750 ACQUIRE_CRYPT_LOCK;
751
Eric Biggersa701c452018-10-23 13:06:55 -0700752 return translateBool(fscrypt_vold_create_user_key(userId, userSerial, ephemeral));
Jeff Sharkey83b559c2017-09-12 16:30:52 -0600753}
754
755binder::Status VoldNativeService::destroyUserKey(int32_t userId) {
Nikita Ioffe5d85bf32019-10-23 17:19:13 +0100756 ENFORCE_SYSTEM_OR_ROOT;
Jeff Sharkey83b559c2017-09-12 16:30:52 -0600757 ACQUIRE_CRYPT_LOCK;
758
Eric Biggersa701c452018-10-23 13:06:55 -0700759 return translateBool(fscrypt_destroy_user_key(userId));
Jeff Sharkey83b559c2017-09-12 16:30:52 -0600760}
761
762binder::Status VoldNativeService::addUserKeyAuth(int32_t userId, int32_t userSerial,
Paul Crowleyedf7a4e2018-09-18 15:14:18 -0700763 const std::string& token,
764 const std::string& secret) {
Nikita Ioffe5d85bf32019-10-23 17:19:13 +0100765 ENFORCE_SYSTEM_OR_ROOT;
Jeff Sharkey83b559c2017-09-12 16:30:52 -0600766 ACQUIRE_CRYPT_LOCK;
767
Eric Biggersa701c452018-10-23 13:06:55 -0700768 return translateBool(fscrypt_add_user_key_auth(userId, userSerial, token, secret));
Jeff Sharkey83b559c2017-09-12 16:30:52 -0600769}
770
Barani Muthukumaran9ad51ad2019-10-31 22:59:34 -0700771binder::Status VoldNativeService::clearUserKeyAuth(int32_t userId, int32_t userSerial,
772 const std::string& token,
773 const std::string& secret) {
774 ENFORCE_SYSTEM_OR_ROOT;
775 ACQUIRE_CRYPT_LOCK;
776
777 return translateBool(fscrypt_clear_user_key_auth(userId, userSerial, token, secret));
778}
779
Jeff Sharkey83b559c2017-09-12 16:30:52 -0600780binder::Status VoldNativeService::fixateNewestUserKeyAuth(int32_t userId) {
Nikita Ioffe5d85bf32019-10-23 17:19:13 +0100781 ENFORCE_SYSTEM_OR_ROOT;
Jeff Sharkey83b559c2017-09-12 16:30:52 -0600782 ACQUIRE_CRYPT_LOCK;
783
Eric Biggersa701c452018-10-23 13:06:55 -0700784 return translateBool(fscrypt_fixate_newest_user_key_auth(userId));
Jeff Sharkey83b559c2017-09-12 16:30:52 -0600785}
786
787binder::Status VoldNativeService::unlockUserKey(int32_t userId, int32_t userSerial,
Paul Crowleyedf7a4e2018-09-18 15:14:18 -0700788 const std::string& token,
789 const std::string& secret) {
Nikita Ioffe5d85bf32019-10-23 17:19:13 +0100790 ENFORCE_SYSTEM_OR_ROOT;
Jeff Sharkey83b559c2017-09-12 16:30:52 -0600791 ACQUIRE_CRYPT_LOCK;
792
Eric Biggersa701c452018-10-23 13:06:55 -0700793 return translateBool(fscrypt_unlock_user_key(userId, userSerial, token, secret));
Jeff Sharkey83b559c2017-09-12 16:30:52 -0600794}
795
796binder::Status VoldNativeService::lockUserKey(int32_t userId) {
Nikita Ioffe5d85bf32019-10-23 17:19:13 +0100797 ENFORCE_SYSTEM_OR_ROOT;
Jeff Sharkey83b559c2017-09-12 16:30:52 -0600798 ACQUIRE_CRYPT_LOCK;
799
Eric Biggersa701c452018-10-23 13:06:55 -0700800 return translateBool(fscrypt_lock_user_key(userId));
Jeff Sharkey83b559c2017-09-12 16:30:52 -0600801}
802
803binder::Status VoldNativeService::prepareUserStorage(const std::unique_ptr<std::string>& uuid,
Paul Crowleyedf7a4e2018-09-18 15:14:18 -0700804 int32_t userId, int32_t userSerial,
805 int32_t flags) {
Nikita Ioffe5d85bf32019-10-23 17:19:13 +0100806 ENFORCE_SYSTEM_OR_ROOT;
Paul Crowley3b71fc52017-10-09 10:55:21 -0700807 std::string empty_string = "";
808 auto uuid_ = uuid ? *uuid : empty_string;
Paul Crowley06f762d2017-10-16 10:59:51 -0700809 CHECK_ARGUMENT_HEX(uuid_);
810
811 ACQUIRE_CRYPT_LOCK;
Eric Biggersa701c452018-10-23 13:06:55 -0700812 return translateBool(fscrypt_prepare_user_storage(uuid_, userId, userSerial, flags));
Jeff Sharkey83b559c2017-09-12 16:30:52 -0600813}
814
815binder::Status VoldNativeService::destroyUserStorage(const std::unique_ptr<std::string>& uuid,
Paul Crowleyedf7a4e2018-09-18 15:14:18 -0700816 int32_t userId, int32_t flags) {
Nikita Ioffe5d85bf32019-10-23 17:19:13 +0100817 ENFORCE_SYSTEM_OR_ROOT;
Paul Crowley3b71fc52017-10-09 10:55:21 -0700818 std::string empty_string = "";
819 auto uuid_ = uuid ? *uuid : empty_string;
Paul Crowley06f762d2017-10-16 10:59:51 -0700820 CHECK_ARGUMENT_HEX(uuid_);
821
822 ACQUIRE_CRYPT_LOCK;
Eric Biggersa701c452018-10-23 13:06:55 -0700823 return translateBool(fscrypt_destroy_user_storage(uuid_, userId, flags));
Jeff Sharkey83b559c2017-09-12 16:30:52 -0600824}
825
Sudheer Shankafa6a1742018-10-04 16:26:22 -0700826binder::Status VoldNativeService::prepareSandboxForApp(const std::string& packageName,
827 int32_t appId, const std::string& sandboxId,
828 int32_t userId) {
Sudheer Shanka5fceb482019-04-29 10:46:35 -0700829 return ok();
Sudheer Shankafa6a1742018-10-04 16:26:22 -0700830}
831
832binder::Status VoldNativeService::destroySandboxForApp(const std::string& packageName,
Sudheer Shanka69bc40f2018-10-25 11:06:55 -0700833 const std::string& sandboxId,
Sudheer Shankafa6a1742018-10-04 16:26:22 -0700834 int32_t userId) {
Sudheer Shanka5fceb482019-04-29 10:46:35 -0700835 return ok();
Sudheer Shankac7562092018-08-24 10:20:56 -0700836}
837
Daniel Rosenberge334aab2018-10-10 18:52:04 -0700838binder::Status VoldNativeService::startCheckpoint(int32_t retry) {
Nikita Ioffe5d85bf32019-10-23 17:19:13 +0100839 ENFORCE_SYSTEM_OR_ROOT;
Daniel Rosenberg4f684712018-08-28 01:58:49 -0700840 ACQUIRE_LOCK;
841
Daniel Rosenberge334aab2018-10-10 18:52:04 -0700842 return cp_startCheckpoint(retry);
Daniel Rosenberg4f684712018-08-28 01:58:49 -0700843}
844
Daniel Rosenbergd3992492018-10-02 17:40:44 -0700845binder::Status VoldNativeService::needsRollback(bool* _aidl_return) {
Nikita Ioffe5d85bf32019-10-23 17:19:13 +0100846 ENFORCE_SYSTEM_OR_ROOT;
Daniel Rosenbergd3992492018-10-02 17:40:44 -0700847 ACQUIRE_LOCK;
848
849 *_aidl_return = cp_needsRollback();
850 return ok();
851}
852
Daniel Rosenberg4f684712018-08-28 01:58:49 -0700853binder::Status VoldNativeService::needsCheckpoint(bool* _aidl_return) {
Nikita Ioffe5d85bf32019-10-23 17:19:13 +0100854 ENFORCE_SYSTEM_OR_ROOT;
Daniel Rosenberg4f684712018-08-28 01:58:49 -0700855 ACQUIRE_LOCK;
856
857 *_aidl_return = cp_needsCheckpoint();
858 return ok();
859}
860
Daniel Rosenberge334aab2018-10-10 18:52:04 -0700861binder::Status VoldNativeService::commitChanges() {
Nikita Ioffe5d85bf32019-10-23 17:19:13 +0100862 ENFORCE_SYSTEM_OR_ROOT;
Daniel Rosenberg4f684712018-08-28 01:58:49 -0700863 ACQUIRE_LOCK;
864
Daniel Rosenberge334aab2018-10-10 18:52:04 -0700865 return cp_commitChanges();
Daniel Rosenberg4f684712018-08-28 01:58:49 -0700866}
867
Daniel Rosenberge334aab2018-10-10 18:52:04 -0700868binder::Status VoldNativeService::prepareCheckpoint() {
Nikita Ioffe5d85bf32019-10-23 17:19:13 +0100869 ENFORCE_SYSTEM_OR_ROOT;
Daniel Rosenberg4f684712018-08-28 01:58:49 -0700870 ACQUIRE_LOCK;
871
Daniel Rosenberge334aab2018-10-10 18:52:04 -0700872 return cp_prepareCheckpoint();
Daniel Rosenberg4f684712018-08-28 01:58:49 -0700873}
874
Daniel Rosenberge334aab2018-10-10 18:52:04 -0700875binder::Status VoldNativeService::restoreCheckpoint(const std::string& mountPoint) {
Nikita Ioffe5d85bf32019-10-23 17:19:13 +0100876 ENFORCE_SYSTEM_OR_ROOT;
Paul Lawrence1abb2fe2018-09-21 10:49:57 -0700877 CHECK_ARGUMENT_PATH(mountPoint);
878 ACQUIRE_LOCK;
879
Daniel Rosenberge334aab2018-10-10 18:52:04 -0700880 return cp_restoreCheckpoint(mountPoint);
Paul Lawrence1abb2fe2018-09-21 10:49:57 -0700881}
882
Daniel Rosenbergdda59812019-03-06 17:45:17 -0800883binder::Status VoldNativeService::restoreCheckpointPart(const std::string& mountPoint, int count) {
Nikita Ioffe5d85bf32019-10-23 17:19:13 +0100884 ENFORCE_SYSTEM_OR_ROOT;
Daniel Rosenbergdda59812019-03-06 17:45:17 -0800885 CHECK_ARGUMENT_PATH(mountPoint);
886 ACQUIRE_LOCK;
887
888 return cp_restoreCheckpoint(mountPoint, count);
889}
890
Daniel Rosenberge334aab2018-10-10 18:52:04 -0700891binder::Status VoldNativeService::markBootAttempt() {
Nikita Ioffe5d85bf32019-10-23 17:19:13 +0100892 ENFORCE_SYSTEM_OR_ROOT;
Daniel Rosenberg4f684712018-08-28 01:58:49 -0700893 ACQUIRE_LOCK;
894
Daniel Rosenberge334aab2018-10-10 18:52:04 -0700895 return cp_markBootAttempt();
Daniel Rosenberg4f684712018-08-28 01:58:49 -0700896}
897
Daniel Rosenberga59e4392019-03-20 17:02:47 -0700898binder::Status VoldNativeService::abortChanges(const std::string& message, bool retry) {
Nikita Ioffe5d85bf32019-10-23 17:19:13 +0100899 ENFORCE_SYSTEM_OR_ROOT;
Daniel Rosenberg4f684712018-08-28 01:58:49 -0700900 ACQUIRE_LOCK;
901
Daniel Rosenberga59e4392019-03-20 17:02:47 -0700902 cp_abortChanges(message, retry);
903 return ok();
Daniel Rosenberg4f684712018-08-28 01:58:49 -0700904}
905
Daniel Rosenberg9b667fb2019-01-22 17:27:25 -0800906binder::Status VoldNativeService::supportsCheckpoint(bool* _aidl_return) {
Nikita Ioffe5d85bf32019-10-23 17:19:13 +0100907 ENFORCE_SYSTEM_OR_ROOT;
Daniel Rosenberg9b667fb2019-01-22 17:27:25 -0800908 ACQUIRE_LOCK;
909
910 return cp_supportsCheckpoint(*_aidl_return);
911}
912
Paul Lawrencec5c79c52019-03-18 13:36:40 -0700913binder::Status VoldNativeService::supportsBlockCheckpoint(bool* _aidl_return) {
Nikita Ioffe5d85bf32019-10-23 17:19:13 +0100914 ENFORCE_SYSTEM_OR_ROOT;
Paul Lawrencec5c79c52019-03-18 13:36:40 -0700915 ACQUIRE_LOCK;
916
917 return cp_supportsBlockCheckpoint(*_aidl_return);
918}
919
920binder::Status VoldNativeService::supportsFileCheckpoint(bool* _aidl_return) {
Nikita Ioffe5d85bf32019-10-23 17:19:13 +0100921 ENFORCE_SYSTEM_OR_ROOT;
Paul Lawrencec5c79c52019-03-18 13:36:40 -0700922 ACQUIRE_LOCK;
923
924 return cp_supportsFileCheckpoint(*_aidl_return);
925}
926
Nikita Ioffea5798fc2019-10-11 16:38:21 +0100927binder::Status VoldNativeService::resetCheckpoint() {
Nikita Ioffe5d85bf32019-10-23 17:19:13 +0100928 ENFORCE_SYSTEM_OR_ROOT;
Nikita Ioffea5798fc2019-10-11 16:38:21 +0100929 ACQUIRE_LOCK;
930
931 cp_resetCheckpoint();
932 return ok();
933}
934
Songchun Fanab55cec2019-12-02 10:50:12 -0800935binder::Status VoldNativeService::incFsVersion(int32_t* _aidl_return) {
936 *_aidl_return = IncFs_Version();
937 return ok();
938}
939
940binder::Status VoldNativeService::mountIncFs(
941 const std::string& imagePath, const std::string& targetDir, int32_t flags,
942 ::android::os::incremental::IncrementalFileSystemControlParcel* _aidl_return) {
943 auto result = IncFs_Mount(imagePath.c_str(), targetDir.c_str(), flags,
944 INCFS_DEFAULT_READ_TIMEOUT_MS, 0777);
945 if (result.cmdFd < 0) {
946 return translate(result.cmdFd);
947 }
948 LOG(INFO) << "VoldNativeService::mountIncFs: everything is fine! " << result.cmdFd << "/"
949 << result.logFd;
950 using ParcelFileDescriptor = ::android::os::ParcelFileDescriptor;
951 using unique_fd = ::android::base::unique_fd;
952 _aidl_return->cmd = std::make_unique<ParcelFileDescriptor>(unique_fd(result.cmdFd));
953 if (result.logFd >= 0) {
954 _aidl_return->log = std::make_unique<ParcelFileDescriptor>(unique_fd(result.logFd));
955 }
956 return ok();
957}
958
959binder::Status VoldNativeService::unmountIncFs(const std::string& dir) {
960 return translate(IncFs_Unmount(dir.c_str()));
961}
962
963binder::Status VoldNativeService::bindMount(const std::string& sourceDir,
964 const std::string& targetDir) {
965 return translate(IncFs_BindMount(sourceDir.c_str(), targetDir.c_str()));
966}
967
Jeff Sharkey068c6be2017-09-06 13:47:40 -0600968} // namespace vold
969} // namespace android