blob: 905407b5373faaed330a59f6dae06ab5c985b00d [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"
Paul Crowley73be12d2020-02-03 12:22:03 -080020
Jeff Sharkey01a0e7f2017-10-17 16:06:32 -060021#include "Benchmark.h"
Jeff Sharkey2048a282017-06-15 09:59:43 -060022#include "CheckEncryption.h"
Daniel Rosenberg4f684712018-08-28 01:58:49 -070023#include "Checkpoint.h"
Eric Biggersa701c452018-10-23 13:06:55 -070024#include "FsCrypt.h"
Paul Crowley73be12d2020-02-03 12:22:03 -080025#include "IdleMaint.h"
Jeff Sharkey83b559c2017-09-12 16:30:52 -060026#include "MetadataCrypt.h"
Paul Crowley73be12d2020-02-03 12:22:03 -080027#include "MoveStorage.h"
28#include "Process.h"
29#include "VoldUtil.h"
30#include "VolumeManager.h"
Paul Crowleyedf7a4e2018-09-18 15:14:18 -070031#include "cryptfs.h"
Paul Crowley73be12d2020-02-03 12:22:03 -080032
Songchun Fanab55cec2019-12-02 10:50:12 -080033#include "incfs_ndk.h"
Jeff Sharkey83b559c2017-09-12 16:30:52 -060034
Jeff Sharkey068c6be2017-09-06 13:47:40 -060035#include <fstream>
Jeff Sharkey01a0e7f2017-10-17 16:06:32 -060036#include <thread>
Jeff Sharkey068c6be2017-09-06 13:47:40 -060037
38#include <android-base/logging.h>
39#include <android-base/stringprintf.h>
40#include <android-base/strings.h>
Jeff Sharkey11c2d382017-09-11 10:32:01 -060041#include <fs_mgr.h>
Eric Biggersa701c452018-10-23 13:06:55 -070042#include <fscrypt/fscrypt.h>
Jeff Sharkey068c6be2017-09-06 13:47:40 -060043#include <private/android_filesystem_config.h>
Jeff Sharkey67b8c492017-09-21 17:08:43 -060044#include <utils/Trace.h>
Jeff Sharkey068c6be2017-09-06 13:47:40 -060045
Jeff Sharkey068c6be2017-09-06 13:47:40 -060046using android::base::StringPrintf;
47using std::endl;
48
49namespace android {
50namespace vold {
51
52namespace {
53
54constexpr const char* kDump = "android.permission.DUMP";
55
56static binder::Status ok() {
57 return binder::Status::ok();
58}
59
60static binder::Status exception(uint32_t code, const std::string& msg) {
61 return binder::Status::fromExceptionCode(code, String8(msg.c_str()));
62}
63
Jeff Sharkey9462bdd2017-09-07 15:27:28 -060064static binder::Status error(const std::string& msg) {
65 PLOG(ERROR) << msg;
66 return binder::Status::fromServiceSpecificError(errno, String8(msg.c_str()));
67}
68
Jeff Sharkey83b559c2017-09-12 16:30:52 -060069static binder::Status translate(int status) {
Jeff Sharkey9462bdd2017-09-07 15:27:28 -060070 if (status == 0) {
71 return binder::Status::ok();
72 } else {
Jeff Sharkey11c2d382017-09-11 10:32:01 -060073 return binder::Status::fromServiceSpecificError(status);
Jeff Sharkey9462bdd2017-09-07 15:27:28 -060074 }
75}
76
Jeff Sharkey83b559c2017-09-12 16:30:52 -060077static binder::Status translateBool(bool status) {
78 if (status) {
79 return binder::Status::ok();
80 } else {
81 return binder::Status::fromServiceSpecificError(status);
82 }
83}
84
Jeff Sharkey068c6be2017-09-06 13:47:40 -060085binder::Status checkPermission(const char* permission) {
86 pid_t pid;
87 uid_t uid;
88
89 if (checkCallingPermission(String16(permission), reinterpret_cast<int32_t*>(&pid),
Paul Crowleyedf7a4e2018-09-18 15:14:18 -070090 reinterpret_cast<int32_t*>(&uid))) {
Jeff Sharkey068c6be2017-09-06 13:47:40 -060091 return ok();
92 } else {
93 return exception(binder::Status::EX_SECURITY,
Paul Crowleyedf7a4e2018-09-18 15:14:18 -070094 StringPrintf("UID %d / PID %d lacks permission %s", uid, pid, permission));
Jeff Sharkey068c6be2017-09-06 13:47:40 -060095 }
96}
97
Nikita Ioffee1fd4892019-10-23 17:19:13 +010098binder::Status checkUidOrRoot(uid_t expectedUid) {
Jeff Sharkey068c6be2017-09-06 13:47:40 -060099 uid_t uid = IPCThreadState::self()->getCallingUid();
100 if (uid == expectedUid || uid == AID_ROOT) {
101 return ok();
102 } else {
103 return exception(binder::Status::EX_SECURITY,
Paul Crowleyedf7a4e2018-09-18 15:14:18 -0700104 StringPrintf("UID %d is not expected UID %d", uid, expectedUid));
Jeff Sharkey068c6be2017-09-06 13:47:40 -0600105 }
106}
107
Jeff Sharkeyec4fda22017-09-12 13:19:24 -0600108binder::Status checkArgumentId(const std::string& id) {
109 if (id.empty()) {
110 return exception(binder::Status::EX_ILLEGAL_ARGUMENT, "Missing ID");
111 }
112 for (const char& c : id) {
113 if (!std::isalnum(c) && c != ':' && c != ',') {
114 return exception(binder::Status::EX_ILLEGAL_ARGUMENT,
Paul Crowleyedf7a4e2018-09-18 15:14:18 -0700115 StringPrintf("ID %s is malformed", id.c_str()));
Jeff Sharkeyec4fda22017-09-12 13:19:24 -0600116 }
117 }
118 return ok();
119}
120
121binder::Status checkArgumentPath(const std::string& path) {
122 if (path.empty()) {
123 return exception(binder::Status::EX_ILLEGAL_ARGUMENT, "Missing path");
124 }
125 if (path[0] != '/') {
126 return exception(binder::Status::EX_ILLEGAL_ARGUMENT,
Paul Crowleyedf7a4e2018-09-18 15:14:18 -0700127 StringPrintf("Path %s is relative", path.c_str()));
Jeff Sharkeyec4fda22017-09-12 13:19:24 -0600128 }
Jeff Sharkey01a0e7f2017-10-17 16:06:32 -0600129 if ((path + '/').find("/../") != std::string::npos) {
130 return exception(binder::Status::EX_ILLEGAL_ARGUMENT,
Paul Crowleyedf7a4e2018-09-18 15:14:18 -0700131 StringPrintf("Path %s is shady", path.c_str()));
Jeff Sharkey01a0e7f2017-10-17 16:06:32 -0600132 }
Jeff Sharkeyec4fda22017-09-12 13:19:24 -0600133 for (const char& c : path) {
134 if (c == '\0' || c == '\n') {
135 return exception(binder::Status::EX_ILLEGAL_ARGUMENT,
Paul Crowleyedf7a4e2018-09-18 15:14:18 -0700136 StringPrintf("Path %s is malformed", path.c_str()));
Jeff Sharkeyec4fda22017-09-12 13:19:24 -0600137 }
138 }
139 return ok();
140}
141
142binder::Status checkArgumentHex(const std::string& hex) {
143 // Empty hex strings are allowed
144 for (const char& c : hex) {
145 if (!std::isxdigit(c) && c != ':' && c != '-') {
146 return exception(binder::Status::EX_ILLEGAL_ARGUMENT,
Paul Crowleyedf7a4e2018-09-18 15:14:18 -0700147 StringPrintf("Hex %s is malformed", hex.c_str()));
Jeff Sharkeyec4fda22017-09-12 13:19:24 -0600148 }
149 }
150 return ok();
151}
152
Nikita Ioffee1fd4892019-10-23 17:19:13 +0100153#define ENFORCE_SYSTEM_OR_ROOT \
154 { \
155 binder::Status status = checkUidOrRoot(AID_SYSTEM); \
156 if (!status.isOk()) { \
157 return status; \
158 } \
Paul Crowleyedf7a4e2018-09-18 15:14:18 -0700159 }
Jeff Sharkey068c6be2017-09-06 13:47:40 -0600160
Paul Crowleyedf7a4e2018-09-18 15:14:18 -0700161#define CHECK_ARGUMENT_ID(id) \
162 { \
163 binder::Status status = checkArgumentId((id)); \
164 if (!status.isOk()) { \
165 return status; \
166 } \
167 }
Jeff Sharkeyec4fda22017-09-12 13:19:24 -0600168
Paul Crowleyedf7a4e2018-09-18 15:14:18 -0700169#define CHECK_ARGUMENT_PATH(path) \
170 { \
171 binder::Status status = checkArgumentPath((path)); \
172 if (!status.isOk()) { \
173 return status; \
174 } \
175 }
Jeff Sharkeyec4fda22017-09-12 13:19:24 -0600176
Paul Crowleyedf7a4e2018-09-18 15:14:18 -0700177#define CHECK_ARGUMENT_HEX(hex) \
178 { \
179 binder::Status status = checkArgumentHex((hex)); \
180 if (!status.isOk()) { \
181 return status; \
182 } \
183 }
Jeff Sharkeyec4fda22017-09-12 13:19:24 -0600184
Paul Crowleyedf7a4e2018-09-18 15:14:18 -0700185#define ACQUIRE_LOCK \
Jeff Sharkey67b8c492017-09-21 17:08:43 -0600186 std::lock_guard<std::mutex> lock(VolumeManager::Instance()->getLock()); \
187 ATRACE_CALL();
Jeff Sharkey83b559c2017-09-12 16:30:52 -0600188
Paul Crowleyedf7a4e2018-09-18 15:14:18 -0700189#define ACQUIRE_CRYPT_LOCK \
Jeff Sharkey67b8c492017-09-21 17:08:43 -0600190 std::lock_guard<std::mutex> lock(VolumeManager::Instance()->getCryptLock()); \
191 ATRACE_CALL();
Jeff Sharkey9462bdd2017-09-07 15:27:28 -0600192
Jeff Sharkey068c6be2017-09-06 13:47:40 -0600193} // namespace
194
195status_t VoldNativeService::start() {
196 IPCThreadState::self()->disableBackgroundScheduling(true);
197 status_t ret = BinderService<VoldNativeService>::publish();
198 if (ret != android::OK) {
199 return ret;
200 }
201 sp<ProcessState> ps(ProcessState::self());
202 ps->startThreadPool();
203 ps->giveThreadPoolName();
204 return android::OK;
205}
206
Paul Crowleyedf7a4e2018-09-18 15:14:18 -0700207status_t VoldNativeService::dump(int fd, const Vector<String16>& /* args */) {
Jeff Sharkey068c6be2017-09-06 13:47:40 -0600208 auto out = std::fstream(StringPrintf("/proc/self/fd/%d", fd));
209 const binder::Status dump_permission = checkPermission(kDump);
210 if (!dump_permission.isOk()) {
211 out << dump_permission.toString8() << endl;
212 return PERMISSION_DENIED;
213 }
214
Jeff Sharkey9462bdd2017-09-07 15:27:28 -0600215 ACQUIRE_LOCK;
Jeff Sharkey068c6be2017-09-06 13:47:40 -0600216 out << "vold is happy!" << endl;
217 out.flush();
Jeff Sharkey068c6be2017-09-06 13:47:40 -0600218 return NO_ERROR;
219}
220
Jeff Sharkey814e9d32017-09-13 11:49:44 -0600221binder::Status VoldNativeService::setListener(
Paul Crowleyedf7a4e2018-09-18 15:14:18 -0700222 const android::sp<android::os::IVoldListener>& listener) {
Nikita Ioffee1fd4892019-10-23 17:19:13 +0100223 ENFORCE_SYSTEM_OR_ROOT;
Jeff Sharkey814e9d32017-09-13 11:49:44 -0600224 ACQUIRE_LOCK;
225
226 VolumeManager::Instance()->setListener(listener);
227 return ok();
228}
229
Jeff Sharkeycbe69fc2017-09-15 16:50:28 -0600230binder::Status VoldNativeService::monitor() {
Nikita Ioffee1fd4892019-10-23 17:19:13 +0100231 ENFORCE_SYSTEM_OR_ROOT;
Jeff Sharkeycbe69fc2017-09-15 16:50:28 -0600232
233 // Simply acquire/release each lock for watchdog
Paul Crowleyedf7a4e2018-09-18 15:14:18 -0700234 { ACQUIRE_LOCK; }
235 { ACQUIRE_CRYPT_LOCK; }
Jeff Sharkeycbe69fc2017-09-15 16:50:28 -0600236
237 return ok();
238}
239
Jeff Sharkey068c6be2017-09-06 13:47:40 -0600240binder::Status VoldNativeService::reset() {
Nikita Ioffee1fd4892019-10-23 17:19:13 +0100241 ENFORCE_SYSTEM_OR_ROOT;
Jeff Sharkey9462bdd2017-09-07 15:27:28 -0600242 ACQUIRE_LOCK;
Jeff Sharkey068c6be2017-09-06 13:47:40 -0600243
Jeff Sharkey9462bdd2017-09-07 15:27:28 -0600244 return translate(VolumeManager::Instance()->reset());
245}
246
247binder::Status VoldNativeService::shutdown() {
Nikita Ioffee1fd4892019-10-23 17:19:13 +0100248 ENFORCE_SYSTEM_OR_ROOT;
Jeff Sharkey9462bdd2017-09-07 15:27:28 -0600249 ACQUIRE_LOCK;
250
251 return translate(VolumeManager::Instance()->shutdown());
252}
253
Jeff Sharkey9462bdd2017-09-07 15:27:28 -0600254binder::Status VoldNativeService::onUserAdded(int32_t userId, int32_t userSerial) {
Nikita Ioffee1fd4892019-10-23 17:19:13 +0100255 ENFORCE_SYSTEM_OR_ROOT;
Jeff Sharkey9462bdd2017-09-07 15:27:28 -0600256 ACQUIRE_LOCK;
257
258 return translate(VolumeManager::Instance()->onUserAdded(userId, userSerial));
259}
260
261binder::Status VoldNativeService::onUserRemoved(int32_t userId) {
Nikita Ioffee1fd4892019-10-23 17:19:13 +0100262 ENFORCE_SYSTEM_OR_ROOT;
Jeff Sharkey9462bdd2017-09-07 15:27:28 -0600263 ACQUIRE_LOCK;
264
265 return translate(VolumeManager::Instance()->onUserRemoved(userId));
266}
267
Sudheer Shanka5fceb482019-04-29 10:46:35 -0700268binder::Status VoldNativeService::onUserStarted(int32_t userId) {
Nikita Ioffee1fd4892019-10-23 17:19:13 +0100269 ENFORCE_SYSTEM_OR_ROOT;
Jeff Sharkey9462bdd2017-09-07 15:27:28 -0600270 ACQUIRE_LOCK;
271
Sudheer Shanka5fceb482019-04-29 10:46:35 -0700272 return translate(VolumeManager::Instance()->onUserStarted(userId));
Jeff Sharkey9462bdd2017-09-07 15:27:28 -0600273}
274
275binder::Status VoldNativeService::onUserStopped(int32_t userId) {
Nikita Ioffee1fd4892019-10-23 17:19:13 +0100276 ENFORCE_SYSTEM_OR_ROOT;
Jeff Sharkey9462bdd2017-09-07 15:27:28 -0600277 ACQUIRE_LOCK;
278
279 return translate(VolumeManager::Instance()->onUserStopped(userId));
280}
281
Sudheer Shankad484aa92018-07-31 10:07:34 -0700282binder::Status VoldNativeService::addAppIds(const std::vector<std::string>& packageNames,
Paul Crowleyedf7a4e2018-09-18 15:14:18 -0700283 const std::vector<int32_t>& appIds) {
Sudheer Shanka5fceb482019-04-29 10:46:35 -0700284 return ok();
Sudheer Shankad484aa92018-07-31 10:07:34 -0700285}
286
Sudheer Shankad484aa92018-07-31 10:07:34 -0700287binder::Status VoldNativeService::addSandboxIds(const std::vector<int32_t>& appIds,
Paul Crowleyedf7a4e2018-09-18 15:14:18 -0700288 const std::vector<std::string>& sandboxIds) {
Sudheer Shanka5fceb482019-04-29 10:46:35 -0700289 return ok();
Sudheer Shankad484aa92018-07-31 10:07:34 -0700290}
291
Jeff Sharkey401b2602017-12-14 22:15:20 -0700292binder::Status VoldNativeService::onSecureKeyguardStateChanged(bool isShowing) {
Nikita Ioffee1fd4892019-10-23 17:19:13 +0100293 ENFORCE_SYSTEM_OR_ROOT;
Jeff Sharkey401b2602017-12-14 22:15:20 -0700294 ACQUIRE_LOCK;
295
296 return translate(VolumeManager::Instance()->onSecureKeyguardStateChanged(isShowing));
297}
298
Jeff Sharkey11c2d382017-09-11 10:32:01 -0600299binder::Status VoldNativeService::partition(const std::string& diskId, int32_t partitionType,
Paul Crowleyedf7a4e2018-09-18 15:14:18 -0700300 int32_t ratio) {
Nikita Ioffee1fd4892019-10-23 17:19:13 +0100301 ENFORCE_SYSTEM_OR_ROOT;
Jeff Sharkeyec4fda22017-09-12 13:19:24 -0600302 CHECK_ARGUMENT_ID(diskId);
Jeff Sharkey9462bdd2017-09-07 15:27:28 -0600303 ACQUIRE_LOCK;
304
305 auto disk = VolumeManager::Instance()->findDisk(diskId);
306 if (disk == nullptr) {
307 return error("Failed to find disk " + diskId);
308 }
309 switch (partitionType) {
Paul Crowleyedf7a4e2018-09-18 15:14:18 -0700310 case PARTITION_TYPE_PUBLIC:
311 return translate(disk->partitionPublic());
312 case PARTITION_TYPE_PRIVATE:
313 return translate(disk->partitionPrivate());
314 case PARTITION_TYPE_MIXED:
315 return translate(disk->partitionMixed(ratio));
316 default:
317 return error("Unknown type " + std::to_string(partitionType));
Jeff Sharkey9462bdd2017-09-07 15:27:28 -0600318 }
319}
320
Jeff Sharkey3ce18252017-10-24 11:08:45 -0600321binder::Status VoldNativeService::forgetPartition(const std::string& partGuid,
Paul Crowleyedf7a4e2018-09-18 15:14:18 -0700322 const std::string& fsUuid) {
Nikita Ioffee1fd4892019-10-23 17:19:13 +0100323 ENFORCE_SYSTEM_OR_ROOT;
Jeff Sharkeyec4fda22017-09-12 13:19:24 -0600324 CHECK_ARGUMENT_HEX(partGuid);
Jeff Sharkey3ce18252017-10-24 11:08:45 -0600325 CHECK_ARGUMENT_HEX(fsUuid);
Jeff Sharkey9462bdd2017-09-07 15:27:28 -0600326 ACQUIRE_LOCK;
327
Jeff Sharkey3ce18252017-10-24 11:08:45 -0600328 return translate(VolumeManager::Instance()->forgetPartition(partGuid, fsUuid));
Jeff Sharkey9462bdd2017-09-07 15:27:28 -0600329}
330
Jeff Sharkey11c2d382017-09-11 10:32:01 -0600331binder::Status VoldNativeService::mount(const std::string& volId, int32_t mountFlags,
Paul Crowleyedf7a4e2018-09-18 15:14:18 -0700332 int32_t mountUserId) {
Nikita Ioffee1fd4892019-10-23 17:19:13 +0100333 ENFORCE_SYSTEM_OR_ROOT;
Jeff Sharkeyec4fda22017-09-12 13:19:24 -0600334 CHECK_ARGUMENT_ID(volId);
Jeff Sharkey9462bdd2017-09-07 15:27:28 -0600335 ACQUIRE_LOCK;
336
337 auto vol = VolumeManager::Instance()->findVolume(volId);
338 if (vol == nullptr) {
339 return error("Failed to find volume " + volId);
340 }
341
342 vol->setMountFlags(mountFlags);
343 vol->setMountUserId(mountUserId);
344
345 int res = vol->mount();
Sudheer Shanka40ab6742018-09-18 13:07:45 -0700346 if (res != OK) {
347 return translate(res);
Jeff Sharkey9462bdd2017-09-07 15:27:28 -0600348 }
Sudheer Shanka40ab6742018-09-18 13:07:45 -0700349 if ((mountFlags & MOUNT_FLAG_PRIMARY) != 0) {
350 res = VolumeManager::Instance()->setPrimary(vol);
351 if (res != OK) {
352 return translate(res);
353 }
354 }
355 return translate(OK);
Jeff Sharkey9462bdd2017-09-07 15:27:28 -0600356}
357
358binder::Status VoldNativeService::unmount(const std::string& volId) {
Nikita Ioffee1fd4892019-10-23 17:19:13 +0100359 ENFORCE_SYSTEM_OR_ROOT;
Jeff Sharkeyec4fda22017-09-12 13:19:24 -0600360 CHECK_ARGUMENT_ID(volId);
Jeff Sharkey9462bdd2017-09-07 15:27:28 -0600361 ACQUIRE_LOCK;
362
363 auto vol = VolumeManager::Instance()->findVolume(volId);
364 if (vol == nullptr) {
365 return error("Failed to find volume " + volId);
366 }
367 return translate(vol->unmount());
368}
369
370binder::Status VoldNativeService::format(const std::string& volId, const std::string& fsType) {
Nikita Ioffee1fd4892019-10-23 17:19:13 +0100371 ENFORCE_SYSTEM_OR_ROOT;
Jeff Sharkeyec4fda22017-09-12 13:19:24 -0600372 CHECK_ARGUMENT_ID(volId);
Jeff Sharkey9462bdd2017-09-07 15:27:28 -0600373 ACQUIRE_LOCK;
374
375 auto vol = VolumeManager::Instance()->findVolume(volId);
376 if (vol == nullptr) {
377 return error("Failed to find volume " + volId);
378 }
379 return translate(vol->format(fsType));
380}
381
Jeff Sharkey2048a282017-06-15 09:59:43 -0600382static binder::Status pathForVolId(const std::string& volId, std::string* path) {
Jeff Sharkey52f7a912017-09-15 12:57:44 -0600383 if (volId == "private" || volId == "null") {
Jeff Sharkey2048a282017-06-15 09:59:43 -0600384 *path = "/data";
Jeff Sharkey52f7a912017-09-15 12:57:44 -0600385 } else {
386 auto vol = VolumeManager::Instance()->findVolume(volId);
387 if (vol == nullptr) {
388 return error("Failed to find volume " + volId);
389 }
390 if (vol->getType() != VolumeBase::Type::kPrivate) {
391 return error("Volume " + volId + " not private");
392 }
393 if (vol->getState() != VolumeBase::State::kMounted) {
394 return error("Volume " + volId + " not mounted");
395 }
Jeff Sharkey2048a282017-06-15 09:59:43 -0600396 *path = vol->getPath();
397 if (path->empty()) {
398 return error("Volume " + volId + " missing path");
399 }
Jeff Sharkey52f7a912017-09-15 12:57:44 -0600400 }
Jeff Sharkey2048a282017-06-15 09:59:43 -0600401 return ok();
402}
Jeff Sharkey52f7a912017-09-15 12:57:44 -0600403
Jeff Sharkey2048a282017-06-15 09:59:43 -0600404binder::Status VoldNativeService::benchmark(
405 const std::string& volId, const android::sp<android::os::IVoldTaskListener>& listener) {
Nikita Ioffee1fd4892019-10-23 17:19:13 +0100406 ENFORCE_SYSTEM_OR_ROOT;
Jeff Sharkey2048a282017-06-15 09:59:43 -0600407 CHECK_ARGUMENT_ID(volId);
408 ACQUIRE_LOCK;
409
410 std::string path;
411 auto status = pathForVolId(volId, &path);
412 if (!status.isOk()) return status;
Jeff Sharkey52f7a912017-09-15 12:57:44 -0600413
Paul Crowleyedf7a4e2018-09-18 15:14:18 -0700414 std::thread([=]() { android::vold::Benchmark(path, listener); }).detach();
Jeff Sharkey068c6be2017-09-06 13:47:40 -0600415 return ok();
416}
417
Jeff Sharkey2048a282017-06-15 09:59:43 -0600418binder::Status VoldNativeService::checkEncryption(const std::string& volId) {
Nikita Ioffee1fd4892019-10-23 17:19:13 +0100419 ENFORCE_SYSTEM_OR_ROOT;
Jeff Sharkey2048a282017-06-15 09:59:43 -0600420 CHECK_ARGUMENT_ID(volId);
421 ACQUIRE_LOCK;
422
423 std::string path;
424 auto status = pathForVolId(volId, &path);
425 if (!status.isOk()) return status;
426 return translate(android::vold::CheckEncryption(path));
427}
428
Paul Crowleyedf7a4e2018-09-18 15:14:18 -0700429binder::Status VoldNativeService::moveStorage(
430 const std::string& fromVolId, const std::string& toVolId,
431 const android::sp<android::os::IVoldTaskListener>& listener) {
Nikita Ioffee1fd4892019-10-23 17:19:13 +0100432 ENFORCE_SYSTEM_OR_ROOT;
Jeff Sharkeyec4fda22017-09-12 13:19:24 -0600433 CHECK_ARGUMENT_ID(fromVolId);
434 CHECK_ARGUMENT_ID(toVolId);
Jeff Sharkey9462bdd2017-09-07 15:27:28 -0600435 ACQUIRE_LOCK;
436
437 auto fromVol = VolumeManager::Instance()->findVolume(fromVolId);
438 auto toVol = VolumeManager::Instance()->findVolume(toVolId);
439 if (fromVol == nullptr) {
440 return error("Failed to find volume " + fromVolId);
441 } else if (toVol == nullptr) {
442 return error("Failed to find volume " + toVolId);
443 }
Jeff Sharkey01a0e7f2017-10-17 16:06:32 -0600444
Paul Crowleyedf7a4e2018-09-18 15:14:18 -0700445 std::thread([=]() { android::vold::MoveStorage(fromVol, toVol, listener); }).detach();
Jeff Sharkey9462bdd2017-09-07 15:27:28 -0600446 return ok();
447}
448
449binder::Status VoldNativeService::remountUid(int32_t uid, int32_t remountMode) {
Nikita Ioffee1fd4892019-10-23 17:19:13 +0100450 ENFORCE_SYSTEM_OR_ROOT;
Jeff Sharkey9462bdd2017-09-07 15:27:28 -0600451 ACQUIRE_LOCK;
452
Sudheer Shanka817b9112018-12-13 17:40:28 -0800453 return translate(VolumeManager::Instance()->remountUid(uid, remountMode));
Jeff Sharkey9462bdd2017-09-07 15:27:28 -0600454}
455
456binder::Status VoldNativeService::mkdirs(const std::string& path) {
Nikita Ioffee1fd4892019-10-23 17:19:13 +0100457 ENFORCE_SYSTEM_OR_ROOT;
Jeff Sharkeyec4fda22017-09-12 13:19:24 -0600458 CHECK_ARGUMENT_PATH(path);
Jeff Sharkey9462bdd2017-09-07 15:27:28 -0600459 ACQUIRE_LOCK;
460
Jeff Sharkey3472e522017-10-06 18:02:53 -0600461 return translate(VolumeManager::Instance()->mkdirs(path));
Jeff Sharkey9462bdd2017-09-07 15:27:28 -0600462}
463
Jeff Sharkey11c2d382017-09-11 10:32:01 -0600464binder::Status VoldNativeService::createObb(const std::string& sourcePath,
Paul Crowleyedf7a4e2018-09-18 15:14:18 -0700465 const std::string& sourceKey, int32_t ownerGid,
466 std::string* _aidl_return) {
Nikita Ioffee1fd4892019-10-23 17:19:13 +0100467 ENFORCE_SYSTEM_OR_ROOT;
Jeff Sharkeyec4fda22017-09-12 13:19:24 -0600468 CHECK_ARGUMENT_PATH(sourcePath);
469 CHECK_ARGUMENT_HEX(sourceKey);
Jeff Sharkey11c2d382017-09-11 10:32:01 -0600470 ACQUIRE_LOCK;
471
472 return translate(
Paul Crowleyedf7a4e2018-09-18 15:14:18 -0700473 VolumeManager::Instance()->createObb(sourcePath, sourceKey, ownerGid, _aidl_return));
Jeff Sharkey11c2d382017-09-11 10:32:01 -0600474}
475
476binder::Status VoldNativeService::destroyObb(const std::string& volId) {
Nikita Ioffee1fd4892019-10-23 17:19:13 +0100477 ENFORCE_SYSTEM_OR_ROOT;
Jeff Sharkeyec4fda22017-09-12 13:19:24 -0600478 CHECK_ARGUMENT_ID(volId);
Jeff Sharkey11c2d382017-09-11 10:32:01 -0600479 ACQUIRE_LOCK;
480
481 return translate(VolumeManager::Instance()->destroyObb(volId));
482}
483
Risan8c9f3322018-10-29 08:52:56 +0900484binder::Status VoldNativeService::createStubVolume(
485 const std::string& sourcePath, const std::string& mountPath, const std::string& fsType,
486 const std::string& fsUuid, const std::string& fsLabel, std::string* _aidl_return) {
Nikita Ioffee1fd4892019-10-23 17:19:13 +0100487 ENFORCE_SYSTEM_OR_ROOT;
Risan8c9f3322018-10-29 08:52:56 +0900488 CHECK_ARGUMENT_PATH(sourcePath);
489 CHECK_ARGUMENT_PATH(mountPath);
490 CHECK_ARGUMENT_HEX(fsUuid);
491 // Label limitation seems to be different between fs (including allowed characters), so checking
492 // is quite meaningless.
493 ACQUIRE_LOCK;
494
495 return translate(VolumeManager::Instance()->createStubVolume(sourcePath, mountPath, fsType,
496 fsUuid, fsLabel, _aidl_return));
497}
498
499binder::Status VoldNativeService::destroyStubVolume(const std::string& volId) {
Nikita Ioffee1fd4892019-10-23 17:19:13 +0100500 ENFORCE_SYSTEM_OR_ROOT;
Risan8c9f3322018-10-29 08:52:56 +0900501 CHECK_ARGUMENT_ID(volId);
502 ACQUIRE_LOCK;
503
504 return translate(VolumeManager::Instance()->destroyStubVolume(volId));
505}
506
Paul Crowleyedf7a4e2018-09-18 15:14:18 -0700507binder::Status VoldNativeService::fstrim(
508 int32_t fstrimFlags, const android::sp<android::os::IVoldTaskListener>& listener) {
Nikita Ioffee1fd4892019-10-23 17:19:13 +0100509 ENFORCE_SYSTEM_OR_ROOT;
Jeff Sharkey11c2d382017-09-11 10:32:01 -0600510 ACQUIRE_LOCK;
511
Paul Crowleyedf7a4e2018-09-18 15:14:18 -0700512 std::thread([=]() { android::vold::Trim(listener); }).detach();
Jeff Sharkey11c2d382017-09-11 10:32:01 -0600513 return ok();
514}
515
Jin Qiana370c142017-10-17 15:41:45 -0700516binder::Status VoldNativeService::runIdleMaint(
Paul Crowleyedf7a4e2018-09-18 15:14:18 -0700517 const android::sp<android::os::IVoldTaskListener>& listener) {
Nikita Ioffee1fd4892019-10-23 17:19:13 +0100518 ENFORCE_SYSTEM_OR_ROOT;
Jin Qiana370c142017-10-17 15:41:45 -0700519 ACQUIRE_LOCK;
520
Paul Crowleyedf7a4e2018-09-18 15:14:18 -0700521 std::thread([=]() { android::vold::RunIdleMaint(listener); }).detach();
Jin Qiana370c142017-10-17 15:41:45 -0700522 return ok();
523}
524
525binder::Status VoldNativeService::abortIdleMaint(
Paul Crowleyedf7a4e2018-09-18 15:14:18 -0700526 const android::sp<android::os::IVoldTaskListener>& listener) {
Nikita Ioffee1fd4892019-10-23 17:19:13 +0100527 ENFORCE_SYSTEM_OR_ROOT;
Jin Qiana370c142017-10-17 15:41:45 -0700528 ACQUIRE_LOCK;
529
Paul Crowleyedf7a4e2018-09-18 15:14:18 -0700530 std::thread([=]() { android::vold::AbortIdleMaint(listener); }).detach();
Jin Qiana370c142017-10-17 15:41:45 -0700531 return ok();
532}
533
Risan8f6198d2018-10-26 20:56:45 -0600534binder::Status VoldNativeService::mountAppFuse(int32_t uid, int32_t mountId,
Paul Crowleyedf7a4e2018-09-18 15:14:18 -0700535 android::base::unique_fd* _aidl_return) {
Nikita Ioffee1fd4892019-10-23 17:19:13 +0100536 ENFORCE_SYSTEM_OR_ROOT;
Jeff Sharkey11c2d382017-09-11 10:32:01 -0600537 ACQUIRE_LOCK;
538
Risan8f6198d2018-10-26 20:56:45 -0600539 return translate(VolumeManager::Instance()->mountAppFuse(uid, mountId, _aidl_return));
Jeff Sharkey11c2d382017-09-11 10:32:01 -0600540}
541
Risan8f6198d2018-10-26 20:56:45 -0600542binder::Status VoldNativeService::unmountAppFuse(int32_t uid, int32_t mountId) {
Nikita Ioffee1fd4892019-10-23 17:19:13 +0100543 ENFORCE_SYSTEM_OR_ROOT;
Jeff Sharkey11c2d382017-09-11 10:32:01 -0600544 ACQUIRE_LOCK;
545
Risan8f6198d2018-10-26 20:56:45 -0600546 return translate(VolumeManager::Instance()->unmountAppFuse(uid, mountId));
547}
548
549binder::Status VoldNativeService::openAppFuseFile(int32_t uid, int32_t mountId, int32_t fileId,
550 int32_t flags,
551 android::base::unique_fd* _aidl_return) {
Nikita Ioffee1fd4892019-10-23 17:19:13 +0100552 ENFORCE_SYSTEM_OR_ROOT;
Risan8f6198d2018-10-26 20:56:45 -0600553 ACQUIRE_LOCK;
554
555 int fd = VolumeManager::Instance()->openAppFuseFile(uid, mountId, fileId, flags);
556 if (fd == -1) {
557 return error("Failed to open AppFuse file for uid: " + std::to_string(uid) +
558 " mountId: " + std::to_string(mountId) + " fileId: " + std::to_string(fileId) +
559 " flags: " + std::to_string(flags));
560 }
561
562 *_aidl_return = android::base::unique_fd(fd);
563 return ok();
Jeff Sharkey11c2d382017-09-11 10:32:01 -0600564}
565
Jeff Sharkey83b559c2017-09-12 16:30:52 -0600566binder::Status VoldNativeService::fdeCheckPassword(const std::string& password) {
Nikita Ioffee1fd4892019-10-23 17:19:13 +0100567 ENFORCE_SYSTEM_OR_ROOT;
Jeff Sharkey83b559c2017-09-12 16:30:52 -0600568 ACQUIRE_CRYPT_LOCK;
569
570 return translate(cryptfs_check_passwd(password.c_str()));
571}
572
573binder::Status VoldNativeService::fdeRestart() {
Nikita Ioffee1fd4892019-10-23 17:19:13 +0100574 ENFORCE_SYSTEM_OR_ROOT;
Jeff Sharkey83b559c2017-09-12 16:30:52 -0600575 ACQUIRE_CRYPT_LOCK;
576
577 // Spawn as thread so init can issue commands back to vold without
578 // causing deadlock, usually as a result of prep_data_fs.
579 std::thread(&cryptfs_restart).detach();
580 return ok();
581}
582
583binder::Status VoldNativeService::fdeComplete(int32_t* _aidl_return) {
Nikita Ioffee1fd4892019-10-23 17:19:13 +0100584 ENFORCE_SYSTEM_OR_ROOT;
Jeff Sharkey83b559c2017-09-12 16:30:52 -0600585 ACQUIRE_CRYPT_LOCK;
586
587 *_aidl_return = cryptfs_crypto_complete();
588 return ok();
589}
590
591static int fdeEnableInternal(int32_t passwordType, const std::string& password,
Paul Crowleyedf7a4e2018-09-18 15:14:18 -0700592 int32_t encryptionFlags) {
Jeff Sharkey83b559c2017-09-12 16:30:52 -0600593 bool noUi = (encryptionFlags & VoldNativeService::ENCRYPTION_FLAG_NO_UI) != 0;
594
Jeff Sharkey83b559c2017-09-12 16:30:52 -0600595 for (int tries = 0; tries < 2; ++tries) {
596 int rc;
597 if (passwordType == VoldNativeService::PASSWORD_TYPE_DEFAULT) {
Paul Lawrence7ee87cf2017-12-22 10:12:06 -0800598 rc = cryptfs_enable_default(noUi);
Jeff Sharkey83b559c2017-09-12 16:30:52 -0600599 } else {
Paul Lawrence7ee87cf2017-12-22 10:12:06 -0800600 rc = cryptfs_enable(passwordType, password.c_str(), noUi);
Jeff Sharkey83b559c2017-09-12 16:30:52 -0600601 }
602
603 if (rc == 0) {
604 return 0;
605 } else if (tries == 0) {
Jeff Sharkey3472e522017-10-06 18:02:53 -0600606 KillProcessesWithOpenFiles(DATA_MNT_POINT, SIGKILL);
Jeff Sharkey83b559c2017-09-12 16:30:52 -0600607 }
608 }
609
610 return -1;
611}
612
Paul Crowleyedf7a4e2018-09-18 15:14:18 -0700613binder::Status VoldNativeService::fdeEnable(int32_t passwordType, const std::string& password,
614 int32_t encryptionFlags) {
Nikita Ioffee1fd4892019-10-23 17:19:13 +0100615 ENFORCE_SYSTEM_OR_ROOT;
Jeff Sharkey83b559c2017-09-12 16:30:52 -0600616 ACQUIRE_CRYPT_LOCK;
617
Paul Crowley0fd26262018-01-30 09:48:19 -0800618 LOG(DEBUG) << "fdeEnable(" << passwordType << ", *, " << encryptionFlags << ")";
Eric Biggersa701c452018-10-23 13:06:55 -0700619 if (fscrypt_is_native()) {
620 LOG(ERROR) << "fscrypt_is_native, fdeEnable invalid";
621 return error("fscrypt_is_native, fdeEnable invalid");
Jeff Sharkey83b559c2017-09-12 16:30:52 -0600622 }
Eric Biggersa701c452018-10-23 13:06:55 -0700623 LOG(DEBUG) << "!fscrypt_is_native, spawning fdeEnableInternal";
Jeff Sharkey83b559c2017-09-12 16:30:52 -0600624
625 // Spawn as thread so init can issue commands back to vold without
626 // causing deadlock, usually as a result of prep_data_fs.
627 std::thread(&fdeEnableInternal, passwordType, password, encryptionFlags).detach();
628 return ok();
629}
630
631binder::Status VoldNativeService::fdeChangePassword(int32_t passwordType,
Paul Crowleyedf7a4e2018-09-18 15:14:18 -0700632 const std::string& password) {
Nikita Ioffee1fd4892019-10-23 17:19:13 +0100633 ENFORCE_SYSTEM_OR_ROOT;
Jeff Sharkey83b559c2017-09-12 16:30:52 -0600634 ACQUIRE_CRYPT_LOCK;
635
636 return translate(cryptfs_changepw(passwordType, password.c_str()));
637}
638
639binder::Status VoldNativeService::fdeVerifyPassword(const std::string& password) {
Nikita Ioffee1fd4892019-10-23 17:19:13 +0100640 ENFORCE_SYSTEM_OR_ROOT;
Jeff Sharkey83b559c2017-09-12 16:30:52 -0600641 ACQUIRE_CRYPT_LOCK;
642
643 return translate(cryptfs_verify_passwd(password.c_str()));
644}
645
Paul Crowleyedf7a4e2018-09-18 15:14:18 -0700646binder::Status VoldNativeService::fdeGetField(const std::string& key, std::string* _aidl_return) {
Nikita Ioffee1fd4892019-10-23 17:19:13 +0100647 ENFORCE_SYSTEM_OR_ROOT;
Jeff Sharkey83b559c2017-09-12 16:30:52 -0600648 ACQUIRE_CRYPT_LOCK;
649
650 char buf[PROPERTY_VALUE_MAX];
651 if (cryptfs_getfield(key.c_str(), buf, sizeof(buf)) != CRYPTO_GETFIELD_OK) {
652 return error(StringPrintf("Failed to read field %s", key.c_str()));
653 } else {
654 *_aidl_return = buf;
655 return ok();
656 }
657}
658
Paul Crowleyedf7a4e2018-09-18 15:14:18 -0700659binder::Status VoldNativeService::fdeSetField(const std::string& key, const std::string& value) {
Nikita Ioffee1fd4892019-10-23 17:19:13 +0100660 ENFORCE_SYSTEM_OR_ROOT;
Jeff Sharkey83b559c2017-09-12 16:30:52 -0600661 ACQUIRE_CRYPT_LOCK;
662
663 return translate(cryptfs_setfield(key.c_str(), value.c_str()));
664}
665
666binder::Status VoldNativeService::fdeGetPasswordType(int32_t* _aidl_return) {
Nikita Ioffee1fd4892019-10-23 17:19:13 +0100667 ENFORCE_SYSTEM_OR_ROOT;
Jeff Sharkey83b559c2017-09-12 16:30:52 -0600668 ACQUIRE_CRYPT_LOCK;
669
670 *_aidl_return = cryptfs_get_password_type();
671 return ok();
672}
673
674binder::Status VoldNativeService::fdeGetPassword(std::string* _aidl_return) {
Nikita Ioffee1fd4892019-10-23 17:19:13 +0100675 ENFORCE_SYSTEM_OR_ROOT;
Jeff Sharkey83b559c2017-09-12 16:30:52 -0600676 ACQUIRE_CRYPT_LOCK;
677
678 const char* res = cryptfs_get_password();
679 if (res != nullptr) {
680 *_aidl_return = res;
681 }
682 return ok();
683}
684
685binder::Status VoldNativeService::fdeClearPassword() {
Nikita Ioffee1fd4892019-10-23 17:19:13 +0100686 ENFORCE_SYSTEM_OR_ROOT;
Jeff Sharkey83b559c2017-09-12 16:30:52 -0600687 ACQUIRE_CRYPT_LOCK;
688
689 cryptfs_clear_password();
690 return ok();
691}
692
693binder::Status VoldNativeService::fbeEnable() {
Nikita Ioffee1fd4892019-10-23 17:19:13 +0100694 ENFORCE_SYSTEM_OR_ROOT;
Jeff Sharkey83b559c2017-09-12 16:30:52 -0600695 ACQUIRE_CRYPT_LOCK;
696
Paul Crowleyc8a3ef32019-09-11 15:00:08 -0700697 return translateBool(fscrypt_initialize_systemwide_keys());
Jeff Sharkey83b559c2017-09-12 16:30:52 -0600698}
699
700binder::Status VoldNativeService::mountDefaultEncrypted() {
Nikita Ioffee1fd4892019-10-23 17:19:13 +0100701 ENFORCE_SYSTEM_OR_ROOT;
Jeff Sharkey83b559c2017-09-12 16:30:52 -0600702 ACQUIRE_CRYPT_LOCK;
703
Eric Biggersa701c452018-10-23 13:06:55 -0700704 if (!fscrypt_is_native()) {
Jeff Sharkey83b559c2017-09-12 16:30:52 -0600705 // Spawn as thread so init can issue commands back to vold without
706 // causing deadlock, usually as a result of prep_data_fs.
707 std::thread(&cryptfs_mount_default_encrypted).detach();
Jeff Sharkey83b559c2017-09-12 16:30:52 -0600708 }
Paul Crowley0fd26262018-01-30 09:48:19 -0800709 return ok();
Jeff Sharkey83b559c2017-09-12 16:30:52 -0600710}
711
712binder::Status VoldNativeService::initUser0() {
Nikita Ioffee1fd4892019-10-23 17:19:13 +0100713 ENFORCE_SYSTEM_OR_ROOT;
Jeff Sharkey83b559c2017-09-12 16:30:52 -0600714 ACQUIRE_CRYPT_LOCK;
715
Eric Biggersa701c452018-10-23 13:06:55 -0700716 return translateBool(fscrypt_init_user0());
Jeff Sharkey83b559c2017-09-12 16:30:52 -0600717}
718
719binder::Status VoldNativeService::isConvertibleToFbe(bool* _aidl_return) {
Nikita Ioffee1fd4892019-10-23 17:19:13 +0100720 ENFORCE_SYSTEM_OR_ROOT;
Jeff Sharkey83b559c2017-09-12 16:30:52 -0600721 ACQUIRE_CRYPT_LOCK;
722
723 *_aidl_return = cryptfs_isConvertibleToFBE() != 0;
724 return ok();
725}
726
Paul Lawrence236e5e82019-06-25 14:44:33 -0700727binder::Status VoldNativeService::mountFstab(const std::string& blkDevice,
728 const std::string& mountPoint) {
Nikita Ioffee1fd4892019-10-23 17:19:13 +0100729 ENFORCE_SYSTEM_OR_ROOT;
Paul Crowley0fd26262018-01-30 09:48:19 -0800730 ACQUIRE_LOCK;
731
Paul Lawrence236e5e82019-06-25 14:44:33 -0700732 return translateBool(fscrypt_mount_metadata_encrypted(blkDevice, mountPoint, false));
Paul Crowley0fd26262018-01-30 09:48:19 -0800733}
734
Paul Lawrence236e5e82019-06-25 14:44:33 -0700735binder::Status VoldNativeService::encryptFstab(const std::string& blkDevice,
736 const std::string& mountPoint) {
Nikita Ioffee1fd4892019-10-23 17:19:13 +0100737 ENFORCE_SYSTEM_OR_ROOT;
Paul Crowley0fd26262018-01-30 09:48:19 -0800738 ACQUIRE_LOCK;
739
Paul Lawrence236e5e82019-06-25 14:44:33 -0700740 return translateBool(fscrypt_mount_metadata_encrypted(blkDevice, mountPoint, true));
Paul Crowley0fd26262018-01-30 09:48:19 -0800741}
742
Paul Crowleyedf7a4e2018-09-18 15:14:18 -0700743binder::Status VoldNativeService::createUserKey(int32_t userId, int32_t userSerial, bool ephemeral) {
Nikita Ioffee1fd4892019-10-23 17:19:13 +0100744 ENFORCE_SYSTEM_OR_ROOT;
Jeff Sharkey83b559c2017-09-12 16:30:52 -0600745 ACQUIRE_CRYPT_LOCK;
746
Eric Biggersa701c452018-10-23 13:06:55 -0700747 return translateBool(fscrypt_vold_create_user_key(userId, userSerial, ephemeral));
Jeff Sharkey83b559c2017-09-12 16:30:52 -0600748}
749
750binder::Status VoldNativeService::destroyUserKey(int32_t userId) {
Nikita Ioffee1fd4892019-10-23 17:19:13 +0100751 ENFORCE_SYSTEM_OR_ROOT;
Jeff Sharkey83b559c2017-09-12 16:30:52 -0600752 ACQUIRE_CRYPT_LOCK;
753
Eric Biggersa701c452018-10-23 13:06:55 -0700754 return translateBool(fscrypt_destroy_user_key(userId));
Jeff Sharkey83b559c2017-09-12 16:30:52 -0600755}
756
757binder::Status VoldNativeService::addUserKeyAuth(int32_t userId, int32_t userSerial,
Paul Crowleyedf7a4e2018-09-18 15:14:18 -0700758 const std::string& token,
759 const std::string& secret) {
Nikita Ioffee1fd4892019-10-23 17:19:13 +0100760 ENFORCE_SYSTEM_OR_ROOT;
Jeff Sharkey83b559c2017-09-12 16:30:52 -0600761 ACQUIRE_CRYPT_LOCK;
762
Eric Biggersa701c452018-10-23 13:06:55 -0700763 return translateBool(fscrypt_add_user_key_auth(userId, userSerial, token, secret));
Jeff Sharkey83b559c2017-09-12 16:30:52 -0600764}
765
Barani Muthukumaranb1927c22019-10-31 22:59:34 -0700766binder::Status VoldNativeService::clearUserKeyAuth(int32_t userId, int32_t userSerial,
767 const std::string& token,
768 const std::string& secret) {
769 ENFORCE_SYSTEM_OR_ROOT;
770 ACQUIRE_CRYPT_LOCK;
771
772 return translateBool(fscrypt_clear_user_key_auth(userId, userSerial, token, secret));
773}
774
Jeff Sharkey83b559c2017-09-12 16:30:52 -0600775binder::Status VoldNativeService::fixateNewestUserKeyAuth(int32_t userId) {
Nikita Ioffee1fd4892019-10-23 17:19:13 +0100776 ENFORCE_SYSTEM_OR_ROOT;
Jeff Sharkey83b559c2017-09-12 16:30:52 -0600777 ACQUIRE_CRYPT_LOCK;
778
Eric Biggersa701c452018-10-23 13:06:55 -0700779 return translateBool(fscrypt_fixate_newest_user_key_auth(userId));
Jeff Sharkey83b559c2017-09-12 16:30:52 -0600780}
781
782binder::Status VoldNativeService::unlockUserKey(int32_t userId, int32_t userSerial,
Paul Crowleyedf7a4e2018-09-18 15:14:18 -0700783 const std::string& token,
784 const std::string& secret) {
Nikita Ioffee1fd4892019-10-23 17:19:13 +0100785 ENFORCE_SYSTEM_OR_ROOT;
Jeff Sharkey83b559c2017-09-12 16:30:52 -0600786 ACQUIRE_CRYPT_LOCK;
787
Eric Biggersa701c452018-10-23 13:06:55 -0700788 return translateBool(fscrypt_unlock_user_key(userId, userSerial, token, secret));
Jeff Sharkey83b559c2017-09-12 16:30:52 -0600789}
790
791binder::Status VoldNativeService::lockUserKey(int32_t userId) {
Nikita Ioffee1fd4892019-10-23 17:19:13 +0100792 ENFORCE_SYSTEM_OR_ROOT;
Jeff Sharkey83b559c2017-09-12 16:30:52 -0600793 ACQUIRE_CRYPT_LOCK;
794
Eric Biggersa701c452018-10-23 13:06:55 -0700795 return translateBool(fscrypt_lock_user_key(userId));
Jeff Sharkey83b559c2017-09-12 16:30:52 -0600796}
797
798binder::Status VoldNativeService::prepareUserStorage(const std::unique_ptr<std::string>& uuid,
Paul Crowleyedf7a4e2018-09-18 15:14:18 -0700799 int32_t userId, int32_t userSerial,
800 int32_t flags) {
Nikita Ioffee1fd4892019-10-23 17:19:13 +0100801 ENFORCE_SYSTEM_OR_ROOT;
Paul Crowley3b71fc52017-10-09 10:55:21 -0700802 std::string empty_string = "";
803 auto uuid_ = uuid ? *uuid : empty_string;
Paul Crowley06f762d2017-10-16 10:59:51 -0700804 CHECK_ARGUMENT_HEX(uuid_);
805
806 ACQUIRE_CRYPT_LOCK;
Eric Biggersa701c452018-10-23 13:06:55 -0700807 return translateBool(fscrypt_prepare_user_storage(uuid_, userId, userSerial, flags));
Jeff Sharkey83b559c2017-09-12 16:30:52 -0600808}
809
810binder::Status VoldNativeService::destroyUserStorage(const std::unique_ptr<std::string>& uuid,
Paul Crowleyedf7a4e2018-09-18 15:14:18 -0700811 int32_t userId, int32_t flags) {
Nikita Ioffee1fd4892019-10-23 17:19:13 +0100812 ENFORCE_SYSTEM_OR_ROOT;
Paul Crowley3b71fc52017-10-09 10:55:21 -0700813 std::string empty_string = "";
814 auto uuid_ = uuid ? *uuid : empty_string;
Paul Crowley06f762d2017-10-16 10:59:51 -0700815 CHECK_ARGUMENT_HEX(uuid_);
816
817 ACQUIRE_CRYPT_LOCK;
Eric Biggersa701c452018-10-23 13:06:55 -0700818 return translateBool(fscrypt_destroy_user_storage(uuid_, userId, flags));
Jeff Sharkey83b559c2017-09-12 16:30:52 -0600819}
820
Sudheer Shankafa6a1742018-10-04 16:26:22 -0700821binder::Status VoldNativeService::prepareSandboxForApp(const std::string& packageName,
822 int32_t appId, const std::string& sandboxId,
823 int32_t userId) {
Sudheer Shanka5fceb482019-04-29 10:46:35 -0700824 return ok();
Sudheer Shankafa6a1742018-10-04 16:26:22 -0700825}
826
827binder::Status VoldNativeService::destroySandboxForApp(const std::string& packageName,
Sudheer Shanka69bc40f2018-10-25 11:06:55 -0700828 const std::string& sandboxId,
Sudheer Shankafa6a1742018-10-04 16:26:22 -0700829 int32_t userId) {
Sudheer Shanka5fceb482019-04-29 10:46:35 -0700830 return ok();
Sudheer Shankac7562092018-08-24 10:20:56 -0700831}
832
Daniel Rosenberge334aab2018-10-10 18:52:04 -0700833binder::Status VoldNativeService::startCheckpoint(int32_t retry) {
Nikita Ioffee1fd4892019-10-23 17:19:13 +0100834 ENFORCE_SYSTEM_OR_ROOT;
Daniel Rosenberg4f684712018-08-28 01:58:49 -0700835 ACQUIRE_LOCK;
836
Daniel Rosenberge334aab2018-10-10 18:52:04 -0700837 return cp_startCheckpoint(retry);
Daniel Rosenberg4f684712018-08-28 01:58:49 -0700838}
839
Daniel Rosenbergd3992492018-10-02 17:40:44 -0700840binder::Status VoldNativeService::needsRollback(bool* _aidl_return) {
Nikita Ioffee1fd4892019-10-23 17:19:13 +0100841 ENFORCE_SYSTEM_OR_ROOT;
Daniel Rosenbergd3992492018-10-02 17:40:44 -0700842 ACQUIRE_LOCK;
843
844 *_aidl_return = cp_needsRollback();
845 return ok();
846}
847
Daniel Rosenberg4f684712018-08-28 01:58:49 -0700848binder::Status VoldNativeService::needsCheckpoint(bool* _aidl_return) {
Nikita Ioffee1fd4892019-10-23 17:19:13 +0100849 ENFORCE_SYSTEM_OR_ROOT;
Daniel Rosenberg4f684712018-08-28 01:58:49 -0700850 ACQUIRE_LOCK;
851
852 *_aidl_return = cp_needsCheckpoint();
853 return ok();
854}
855
Daniel Rosenberge334aab2018-10-10 18:52:04 -0700856binder::Status VoldNativeService::commitChanges() {
Nikita Ioffee1fd4892019-10-23 17:19:13 +0100857 ENFORCE_SYSTEM_OR_ROOT;
Daniel Rosenberg4f684712018-08-28 01:58:49 -0700858 ACQUIRE_LOCK;
859
Daniel Rosenberge334aab2018-10-10 18:52:04 -0700860 return cp_commitChanges();
Daniel Rosenberg4f684712018-08-28 01:58:49 -0700861}
862
Daniel Rosenberge334aab2018-10-10 18:52:04 -0700863binder::Status VoldNativeService::prepareCheckpoint() {
Nikita Ioffee1fd4892019-10-23 17:19:13 +0100864 ENFORCE_SYSTEM_OR_ROOT;
Daniel Rosenberg4f684712018-08-28 01:58:49 -0700865 ACQUIRE_LOCK;
866
Daniel Rosenberge334aab2018-10-10 18:52:04 -0700867 return cp_prepareCheckpoint();
Daniel Rosenberg4f684712018-08-28 01:58:49 -0700868}
869
Daniel Rosenberge334aab2018-10-10 18:52:04 -0700870binder::Status VoldNativeService::restoreCheckpoint(const std::string& mountPoint) {
Nikita Ioffee1fd4892019-10-23 17:19:13 +0100871 ENFORCE_SYSTEM_OR_ROOT;
Paul Lawrence1abb2fe2018-09-21 10:49:57 -0700872 CHECK_ARGUMENT_PATH(mountPoint);
873 ACQUIRE_LOCK;
874
Daniel Rosenberge334aab2018-10-10 18:52:04 -0700875 return cp_restoreCheckpoint(mountPoint);
Paul Lawrence1abb2fe2018-09-21 10:49:57 -0700876}
877
Daniel Rosenbergdda59812019-03-06 17:45:17 -0800878binder::Status VoldNativeService::restoreCheckpointPart(const std::string& mountPoint, int count) {
Nikita Ioffee1fd4892019-10-23 17:19:13 +0100879 ENFORCE_SYSTEM_OR_ROOT;
Daniel Rosenbergdda59812019-03-06 17:45:17 -0800880 CHECK_ARGUMENT_PATH(mountPoint);
881 ACQUIRE_LOCK;
882
883 return cp_restoreCheckpoint(mountPoint, count);
884}
885
Daniel Rosenberge334aab2018-10-10 18:52:04 -0700886binder::Status VoldNativeService::markBootAttempt() {
Nikita Ioffee1fd4892019-10-23 17:19:13 +0100887 ENFORCE_SYSTEM_OR_ROOT;
Daniel Rosenberg4f684712018-08-28 01:58:49 -0700888 ACQUIRE_LOCK;
889
Daniel Rosenberge334aab2018-10-10 18:52:04 -0700890 return cp_markBootAttempt();
Daniel Rosenberg4f684712018-08-28 01:58:49 -0700891}
892
Daniel Rosenberga59e4392019-03-20 17:02:47 -0700893binder::Status VoldNativeService::abortChanges(const std::string& message, bool retry) {
Nikita Ioffee1fd4892019-10-23 17:19:13 +0100894 ENFORCE_SYSTEM_OR_ROOT;
Daniel Rosenberg4f684712018-08-28 01:58:49 -0700895 ACQUIRE_LOCK;
896
Daniel Rosenberga59e4392019-03-20 17:02:47 -0700897 cp_abortChanges(message, retry);
898 return ok();
Daniel Rosenberg4f684712018-08-28 01:58:49 -0700899}
900
Daniel Rosenberg9b667fb2019-01-22 17:27:25 -0800901binder::Status VoldNativeService::supportsCheckpoint(bool* _aidl_return) {
Nikita Ioffee1fd4892019-10-23 17:19:13 +0100902 ENFORCE_SYSTEM_OR_ROOT;
Daniel Rosenberg9b667fb2019-01-22 17:27:25 -0800903 ACQUIRE_LOCK;
904
905 return cp_supportsCheckpoint(*_aidl_return);
906}
907
Paul Lawrencec5c79c52019-03-18 13:36:40 -0700908binder::Status VoldNativeService::supportsBlockCheckpoint(bool* _aidl_return) {
Nikita Ioffee1fd4892019-10-23 17:19:13 +0100909 ENFORCE_SYSTEM_OR_ROOT;
Paul Lawrencec5c79c52019-03-18 13:36:40 -0700910 ACQUIRE_LOCK;
911
912 return cp_supportsBlockCheckpoint(*_aidl_return);
913}
914
915binder::Status VoldNativeService::supportsFileCheckpoint(bool* _aidl_return) {
Nikita Ioffee1fd4892019-10-23 17:19:13 +0100916 ENFORCE_SYSTEM_OR_ROOT;
Paul Lawrencec5c79c52019-03-18 13:36:40 -0700917 ACQUIRE_LOCK;
918
919 return cp_supportsFileCheckpoint(*_aidl_return);
920}
921
Nikita Ioffea5798fc2019-10-11 16:38:21 +0100922binder::Status VoldNativeService::resetCheckpoint() {
Nikita Ioffee1fd4892019-10-23 17:19:13 +0100923 ENFORCE_SYSTEM_OR_ROOT;
Nikita Ioffea5798fc2019-10-11 16:38:21 +0100924 ACQUIRE_LOCK;
925
926 cp_resetCheckpoint();
927 return ok();
928}
929
Songchun Fanab55cec2019-12-02 10:50:12 -0800930binder::Status VoldNativeService::incFsVersion(int32_t* _aidl_return) {
931 *_aidl_return = IncFs_Version();
932 return ok();
933}
934
935binder::Status VoldNativeService::mountIncFs(
936 const std::string& imagePath, const std::string& targetDir, int32_t flags,
937 ::android::os::incremental::IncrementalFileSystemControlParcel* _aidl_return) {
938 auto result = IncFs_Mount(imagePath.c_str(), targetDir.c_str(), flags,
939 INCFS_DEFAULT_READ_TIMEOUT_MS, 0777);
940 if (result.cmdFd < 0) {
941 return translate(result.cmdFd);
942 }
943 LOG(INFO) << "VoldNativeService::mountIncFs: everything is fine! " << result.cmdFd << "/"
944 << result.logFd;
945 using ParcelFileDescriptor = ::android::os::ParcelFileDescriptor;
946 using unique_fd = ::android::base::unique_fd;
947 _aidl_return->cmd = std::make_unique<ParcelFileDescriptor>(unique_fd(result.cmdFd));
948 if (result.logFd >= 0) {
949 _aidl_return->log = std::make_unique<ParcelFileDescriptor>(unique_fd(result.logFd));
950 }
951 return ok();
952}
953
954binder::Status VoldNativeService::unmountIncFs(const std::string& dir) {
955 return translate(IncFs_Unmount(dir.c_str()));
956}
957
958binder::Status VoldNativeService::bindMount(const std::string& sourceDir,
959 const std::string& targetDir) {
960 return translate(IncFs_BindMount(sourceDir.c_str(), targetDir.c_str()));
961}
962
Jeff Sharkey068c6be2017-09-06 13:47:40 -0600963} // namespace vold
964} // namespace android