blob: 3643c7442cd5c6f7f00cf7914a6687ff876811f8 [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 Ioffe5d85bf32019-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) {
Zima438b242019-09-25 14:37:38 +0100113 if (!std::isalnum(c) && c != ':' && c != ',' && c != ';') {
Jeff Sharkeyec4fda22017-09-12 13:19:24 -0600114 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 Ioffe5d85bf32019-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 Ioffe5d85bf32019-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 Ioffe5d85bf32019-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 Ioffe5d85bf32019-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 Ioffe5d85bf32019-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 Ioffe5d85bf32019-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 Ioffe5d85bf32019-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 Ioffe5d85bf32019-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 Ioffe5d85bf32019-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 Ioffe5d85bf32019-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 Ioffe5d85bf32019-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 Ioffe5d85bf32019-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
Zim5048b4b2019-11-19 09:16:03 +0000331binder::Status VoldNativeService::mount(
332 const std::string& volId, int32_t mountFlags, int32_t mountUserId,
333 const android::sp<android::os::IVoldMountCallback>& callback) {
Nikita Ioffe5d85bf32019-10-23 17:19:13 +0100334 ENFORCE_SYSTEM_OR_ROOT;
Jeff Sharkeyec4fda22017-09-12 13:19:24 -0600335 CHECK_ARGUMENT_ID(volId);
Jeff Sharkey9462bdd2017-09-07 15:27:28 -0600336 ACQUIRE_LOCK;
337
338 auto vol = VolumeManager::Instance()->findVolume(volId);
339 if (vol == nullptr) {
340 return error("Failed to find volume " + volId);
341 }
342
343 vol->setMountFlags(mountFlags);
344 vol->setMountUserId(mountUserId);
345
Zim5048b4b2019-11-19 09:16:03 +0000346 vol->setMountCallback(callback);
Jeff Sharkey9462bdd2017-09-07 15:27:28 -0600347 int res = vol->mount();
Zim5048b4b2019-11-19 09:16:03 +0000348 vol->setMountCallback(nullptr);
349
Sudheer Shanka40ab6742018-09-18 13:07:45 -0700350 if (res != OK) {
351 return translate(res);
Jeff Sharkey9462bdd2017-09-07 15:27:28 -0600352 }
Zim3623a212019-07-19 16:46:53 +0100353
Sudheer Shanka40ab6742018-09-18 13:07:45 -0700354 if ((mountFlags & MOUNT_FLAG_PRIMARY) != 0) {
355 res = VolumeManager::Instance()->setPrimary(vol);
356 if (res != OK) {
357 return translate(res);
358 }
359 }
360 return translate(OK);
Jeff Sharkey9462bdd2017-09-07 15:27:28 -0600361}
362
363binder::Status VoldNativeService::unmount(const std::string& volId) {
Nikita Ioffe5d85bf32019-10-23 17:19:13 +0100364 ENFORCE_SYSTEM_OR_ROOT;
Jeff Sharkeyec4fda22017-09-12 13:19:24 -0600365 CHECK_ARGUMENT_ID(volId);
Jeff Sharkey9462bdd2017-09-07 15:27:28 -0600366 ACQUIRE_LOCK;
367
368 auto vol = VolumeManager::Instance()->findVolume(volId);
369 if (vol == nullptr) {
370 return error("Failed to find volume " + volId);
371 }
372 return translate(vol->unmount());
373}
374
375binder::Status VoldNativeService::format(const std::string& volId, const std::string& fsType) {
Nikita Ioffe5d85bf32019-10-23 17:19:13 +0100376 ENFORCE_SYSTEM_OR_ROOT;
Jeff Sharkeyec4fda22017-09-12 13:19:24 -0600377 CHECK_ARGUMENT_ID(volId);
Jeff Sharkey9462bdd2017-09-07 15:27:28 -0600378 ACQUIRE_LOCK;
379
380 auto vol = VolumeManager::Instance()->findVolume(volId);
381 if (vol == nullptr) {
382 return error("Failed to find volume " + volId);
383 }
384 return translate(vol->format(fsType));
385}
386
Jeff Sharkey2048a282017-06-15 09:59:43 -0600387static binder::Status pathForVolId(const std::string& volId, std::string* path) {
Jeff Sharkey52f7a912017-09-15 12:57:44 -0600388 if (volId == "private" || volId == "null") {
Jeff Sharkey2048a282017-06-15 09:59:43 -0600389 *path = "/data";
Jeff Sharkey52f7a912017-09-15 12:57:44 -0600390 } else {
391 auto vol = VolumeManager::Instance()->findVolume(volId);
392 if (vol == nullptr) {
393 return error("Failed to find volume " + volId);
394 }
395 if (vol->getType() != VolumeBase::Type::kPrivate) {
396 return error("Volume " + volId + " not private");
397 }
398 if (vol->getState() != VolumeBase::State::kMounted) {
399 return error("Volume " + volId + " not mounted");
400 }
Jeff Sharkey2048a282017-06-15 09:59:43 -0600401 *path = vol->getPath();
402 if (path->empty()) {
403 return error("Volume " + volId + " missing path");
404 }
Jeff Sharkey52f7a912017-09-15 12:57:44 -0600405 }
Jeff Sharkey2048a282017-06-15 09:59:43 -0600406 return ok();
407}
Jeff Sharkey52f7a912017-09-15 12:57:44 -0600408
Jeff Sharkey2048a282017-06-15 09:59:43 -0600409binder::Status VoldNativeService::benchmark(
410 const std::string& volId, const android::sp<android::os::IVoldTaskListener>& listener) {
Nikita Ioffe5d85bf32019-10-23 17:19:13 +0100411 ENFORCE_SYSTEM_OR_ROOT;
Jeff Sharkey2048a282017-06-15 09:59:43 -0600412 CHECK_ARGUMENT_ID(volId);
413 ACQUIRE_LOCK;
414
415 std::string path;
416 auto status = pathForVolId(volId, &path);
417 if (!status.isOk()) return status;
Jeff Sharkey52f7a912017-09-15 12:57:44 -0600418
Paul Crowleyedf7a4e2018-09-18 15:14:18 -0700419 std::thread([=]() { android::vold::Benchmark(path, listener); }).detach();
Jeff Sharkey068c6be2017-09-06 13:47:40 -0600420 return ok();
421}
422
Jeff Sharkey2048a282017-06-15 09:59:43 -0600423binder::Status VoldNativeService::checkEncryption(const std::string& volId) {
Nikita Ioffe5d85bf32019-10-23 17:19:13 +0100424 ENFORCE_SYSTEM_OR_ROOT;
Jeff Sharkey2048a282017-06-15 09:59:43 -0600425 CHECK_ARGUMENT_ID(volId);
426 ACQUIRE_LOCK;
427
428 std::string path;
429 auto status = pathForVolId(volId, &path);
430 if (!status.isOk()) return status;
431 return translate(android::vold::CheckEncryption(path));
432}
433
Paul Crowleyedf7a4e2018-09-18 15:14:18 -0700434binder::Status VoldNativeService::moveStorage(
435 const std::string& fromVolId, const std::string& toVolId,
436 const android::sp<android::os::IVoldTaskListener>& listener) {
Nikita Ioffe5d85bf32019-10-23 17:19:13 +0100437 ENFORCE_SYSTEM_OR_ROOT;
Jeff Sharkeyec4fda22017-09-12 13:19:24 -0600438 CHECK_ARGUMENT_ID(fromVolId);
439 CHECK_ARGUMENT_ID(toVolId);
Jeff Sharkey9462bdd2017-09-07 15:27:28 -0600440 ACQUIRE_LOCK;
441
442 auto fromVol = VolumeManager::Instance()->findVolume(fromVolId);
443 auto toVol = VolumeManager::Instance()->findVolume(toVolId);
444 if (fromVol == nullptr) {
445 return error("Failed to find volume " + fromVolId);
446 } else if (toVol == nullptr) {
447 return error("Failed to find volume " + toVolId);
448 }
Jeff Sharkey01a0e7f2017-10-17 16:06:32 -0600449
Paul Crowleyedf7a4e2018-09-18 15:14:18 -0700450 std::thread([=]() { android::vold::MoveStorage(fromVol, toVol, listener); }).detach();
Jeff Sharkey9462bdd2017-09-07 15:27:28 -0600451 return ok();
452}
453
454binder::Status VoldNativeService::remountUid(int32_t uid, int32_t remountMode) {
Nikita Ioffe5d85bf32019-10-23 17:19:13 +0100455 ENFORCE_SYSTEM_OR_ROOT;
Jeff Sharkey9462bdd2017-09-07 15:27:28 -0600456 ACQUIRE_LOCK;
457
Sudheer Shanka817b9112018-12-13 17:40:28 -0800458 return translate(VolumeManager::Instance()->remountUid(uid, remountMode));
Jeff Sharkey9462bdd2017-09-07 15:27:28 -0600459}
460
Martijn Coenen13ff6682019-12-24 12:57:16 +0100461binder::Status VoldNativeService::setupAppDir(const std::string& path,
462 const std::string& appDirRoot, int32_t appUid) {
Nikita Ioffe5d85bf32019-10-23 17:19:13 +0100463 ENFORCE_SYSTEM_OR_ROOT;
Jeff Sharkeyec4fda22017-09-12 13:19:24 -0600464 CHECK_ARGUMENT_PATH(path);
Martijn Coenen13ff6682019-12-24 12:57:16 +0100465 CHECK_ARGUMENT_PATH(appDirRoot);
Jeff Sharkey9462bdd2017-09-07 15:27:28 -0600466 ACQUIRE_LOCK;
467
Martijn Coenen13ff6682019-12-24 12:57:16 +0100468 return translate(VolumeManager::Instance()->setupAppDir(path, appDirRoot, appUid));
Jeff Sharkey9462bdd2017-09-07 15:27:28 -0600469}
470
Jeff Sharkey11c2d382017-09-11 10:32:01 -0600471binder::Status VoldNativeService::createObb(const std::string& sourcePath,
Paul Crowleyedf7a4e2018-09-18 15:14:18 -0700472 const std::string& sourceKey, int32_t ownerGid,
473 std::string* _aidl_return) {
Nikita Ioffe5d85bf32019-10-23 17:19:13 +0100474 ENFORCE_SYSTEM_OR_ROOT;
Jeff Sharkeyec4fda22017-09-12 13:19:24 -0600475 CHECK_ARGUMENT_PATH(sourcePath);
476 CHECK_ARGUMENT_HEX(sourceKey);
Jeff Sharkey11c2d382017-09-11 10:32:01 -0600477 ACQUIRE_LOCK;
478
479 return translate(
Paul Crowleyedf7a4e2018-09-18 15:14:18 -0700480 VolumeManager::Instance()->createObb(sourcePath, sourceKey, ownerGid, _aidl_return));
Jeff Sharkey11c2d382017-09-11 10:32:01 -0600481}
482
483binder::Status VoldNativeService::destroyObb(const std::string& volId) {
Nikita Ioffe5d85bf32019-10-23 17:19:13 +0100484 ENFORCE_SYSTEM_OR_ROOT;
Jeff Sharkeyec4fda22017-09-12 13:19:24 -0600485 CHECK_ARGUMENT_ID(volId);
Jeff Sharkey11c2d382017-09-11 10:32:01 -0600486 ACQUIRE_LOCK;
487
488 return translate(VolumeManager::Instance()->destroyObb(volId));
489}
490
Risan8c9f3322018-10-29 08:52:56 +0900491binder::Status VoldNativeService::createStubVolume(
492 const std::string& sourcePath, const std::string& mountPath, const std::string& fsType,
493 const std::string& fsUuid, const std::string& fsLabel, std::string* _aidl_return) {
Nikita Ioffe5d85bf32019-10-23 17:19:13 +0100494 ENFORCE_SYSTEM_OR_ROOT;
Risan8c9f3322018-10-29 08:52:56 +0900495 CHECK_ARGUMENT_PATH(sourcePath);
496 CHECK_ARGUMENT_PATH(mountPath);
497 CHECK_ARGUMENT_HEX(fsUuid);
498 // Label limitation seems to be different between fs (including allowed characters), so checking
499 // is quite meaningless.
500 ACQUIRE_LOCK;
501
502 return translate(VolumeManager::Instance()->createStubVolume(sourcePath, mountPath, fsType,
503 fsUuid, fsLabel, _aidl_return));
504}
505
506binder::Status VoldNativeService::destroyStubVolume(const std::string& volId) {
Nikita Ioffe5d85bf32019-10-23 17:19:13 +0100507 ENFORCE_SYSTEM_OR_ROOT;
Risan8c9f3322018-10-29 08:52:56 +0900508 CHECK_ARGUMENT_ID(volId);
509 ACQUIRE_LOCK;
510
511 return translate(VolumeManager::Instance()->destroyStubVolume(volId));
512}
513
Paul Crowleyedf7a4e2018-09-18 15:14:18 -0700514binder::Status VoldNativeService::fstrim(
515 int32_t fstrimFlags, const android::sp<android::os::IVoldTaskListener>& listener) {
Nikita Ioffe5d85bf32019-10-23 17:19:13 +0100516 ENFORCE_SYSTEM_OR_ROOT;
Jeff Sharkey11c2d382017-09-11 10:32:01 -0600517 ACQUIRE_LOCK;
518
Paul Crowleyedf7a4e2018-09-18 15:14:18 -0700519 std::thread([=]() { android::vold::Trim(listener); }).detach();
Jeff Sharkey11c2d382017-09-11 10:32:01 -0600520 return ok();
521}
522
Jin Qiana370c142017-10-17 15:41:45 -0700523binder::Status VoldNativeService::runIdleMaint(
Paul Crowleyedf7a4e2018-09-18 15:14:18 -0700524 const android::sp<android::os::IVoldTaskListener>& listener) {
Nikita Ioffe5d85bf32019-10-23 17:19:13 +0100525 ENFORCE_SYSTEM_OR_ROOT;
Jin Qiana370c142017-10-17 15:41:45 -0700526 ACQUIRE_LOCK;
527
Paul Crowleyedf7a4e2018-09-18 15:14:18 -0700528 std::thread([=]() { android::vold::RunIdleMaint(listener); }).detach();
Jin Qiana370c142017-10-17 15:41:45 -0700529 return ok();
530}
531
532binder::Status VoldNativeService::abortIdleMaint(
Paul Crowleyedf7a4e2018-09-18 15:14:18 -0700533 const android::sp<android::os::IVoldTaskListener>& listener) {
Nikita Ioffe5d85bf32019-10-23 17:19:13 +0100534 ENFORCE_SYSTEM_OR_ROOT;
Jin Qiana370c142017-10-17 15:41:45 -0700535 ACQUIRE_LOCK;
536
Paul Crowleyedf7a4e2018-09-18 15:14:18 -0700537 std::thread([=]() { android::vold::AbortIdleMaint(listener); }).detach();
Jin Qiana370c142017-10-17 15:41:45 -0700538 return ok();
539}
540
Risan8f6198d2018-10-26 20:56:45 -0600541binder::Status VoldNativeService::mountAppFuse(int32_t uid, int32_t mountId,
Paul Crowleyedf7a4e2018-09-18 15:14:18 -0700542 android::base::unique_fd* _aidl_return) {
Nikita Ioffe5d85bf32019-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()->mountAppFuse(uid, mountId, _aidl_return));
Jeff Sharkey11c2d382017-09-11 10:32:01 -0600547}
548
Risan8f6198d2018-10-26 20:56:45 -0600549binder::Status VoldNativeService::unmountAppFuse(int32_t uid, int32_t mountId) {
Nikita Ioffe5d85bf32019-10-23 17:19:13 +0100550 ENFORCE_SYSTEM_OR_ROOT;
Jeff Sharkey11c2d382017-09-11 10:32:01 -0600551 ACQUIRE_LOCK;
552
Risan8f6198d2018-10-26 20:56:45 -0600553 return translate(VolumeManager::Instance()->unmountAppFuse(uid, mountId));
554}
555
556binder::Status VoldNativeService::openAppFuseFile(int32_t uid, int32_t mountId, int32_t fileId,
557 int32_t flags,
558 android::base::unique_fd* _aidl_return) {
Nikita Ioffe5d85bf32019-10-23 17:19:13 +0100559 ENFORCE_SYSTEM_OR_ROOT;
Risan8f6198d2018-10-26 20:56:45 -0600560 ACQUIRE_LOCK;
561
562 int fd = VolumeManager::Instance()->openAppFuseFile(uid, mountId, fileId, flags);
563 if (fd == -1) {
564 return error("Failed to open AppFuse file for uid: " + std::to_string(uid) +
565 " mountId: " + std::to_string(mountId) + " fileId: " + std::to_string(fileId) +
566 " flags: " + std::to_string(flags));
567 }
568
569 *_aidl_return = android::base::unique_fd(fd);
570 return ok();
Jeff Sharkey11c2d382017-09-11 10:32:01 -0600571}
572
Jeff Sharkey83b559c2017-09-12 16:30:52 -0600573binder::Status VoldNativeService::fdeCheckPassword(const std::string& password) {
Nikita Ioffe5d85bf32019-10-23 17:19:13 +0100574 ENFORCE_SYSTEM_OR_ROOT;
Jeff Sharkey83b559c2017-09-12 16:30:52 -0600575 ACQUIRE_CRYPT_LOCK;
576
577 return translate(cryptfs_check_passwd(password.c_str()));
578}
579
580binder::Status VoldNativeService::fdeRestart() {
Nikita Ioffe5d85bf32019-10-23 17:19:13 +0100581 ENFORCE_SYSTEM_OR_ROOT;
Jeff Sharkey83b559c2017-09-12 16:30:52 -0600582 ACQUIRE_CRYPT_LOCK;
583
584 // Spawn as thread so init can issue commands back to vold without
585 // causing deadlock, usually as a result of prep_data_fs.
586 std::thread(&cryptfs_restart).detach();
587 return ok();
588}
589
590binder::Status VoldNativeService::fdeComplete(int32_t* _aidl_return) {
Nikita Ioffe5d85bf32019-10-23 17:19:13 +0100591 ENFORCE_SYSTEM_OR_ROOT;
Jeff Sharkey83b559c2017-09-12 16:30:52 -0600592 ACQUIRE_CRYPT_LOCK;
593
594 *_aidl_return = cryptfs_crypto_complete();
595 return ok();
596}
597
598static int fdeEnableInternal(int32_t passwordType, const std::string& password,
Paul Crowleyedf7a4e2018-09-18 15:14:18 -0700599 int32_t encryptionFlags) {
Jeff Sharkey83b559c2017-09-12 16:30:52 -0600600 bool noUi = (encryptionFlags & VoldNativeService::ENCRYPTION_FLAG_NO_UI) != 0;
601
Jeff Sharkey83b559c2017-09-12 16:30:52 -0600602 for (int tries = 0; tries < 2; ++tries) {
603 int rc;
604 if (passwordType == VoldNativeService::PASSWORD_TYPE_DEFAULT) {
Paul Lawrence7ee87cf2017-12-22 10:12:06 -0800605 rc = cryptfs_enable_default(noUi);
Jeff Sharkey83b559c2017-09-12 16:30:52 -0600606 } else {
Paul Lawrence7ee87cf2017-12-22 10:12:06 -0800607 rc = cryptfs_enable(passwordType, password.c_str(), noUi);
Jeff Sharkey83b559c2017-09-12 16:30:52 -0600608 }
609
610 if (rc == 0) {
611 return 0;
612 } else if (tries == 0) {
Jeff Sharkey3472e522017-10-06 18:02:53 -0600613 KillProcessesWithOpenFiles(DATA_MNT_POINT, SIGKILL);
Jeff Sharkey83b559c2017-09-12 16:30:52 -0600614 }
615 }
616
617 return -1;
618}
619
Paul Crowleyedf7a4e2018-09-18 15:14:18 -0700620binder::Status VoldNativeService::fdeEnable(int32_t passwordType, const std::string& password,
621 int32_t encryptionFlags) {
Nikita Ioffe5d85bf32019-10-23 17:19:13 +0100622 ENFORCE_SYSTEM_OR_ROOT;
Jeff Sharkey83b559c2017-09-12 16:30:52 -0600623 ACQUIRE_CRYPT_LOCK;
624
Paul Crowley0fd26262018-01-30 09:48:19 -0800625 LOG(DEBUG) << "fdeEnable(" << passwordType << ", *, " << encryptionFlags << ")";
Eric Biggersa701c452018-10-23 13:06:55 -0700626 if (fscrypt_is_native()) {
627 LOG(ERROR) << "fscrypt_is_native, fdeEnable invalid";
628 return error("fscrypt_is_native, fdeEnable invalid");
Jeff Sharkey83b559c2017-09-12 16:30:52 -0600629 }
Eric Biggersa701c452018-10-23 13:06:55 -0700630 LOG(DEBUG) << "!fscrypt_is_native, spawning fdeEnableInternal";
Jeff Sharkey83b559c2017-09-12 16:30:52 -0600631
632 // Spawn as thread so init can issue commands back to vold without
633 // causing deadlock, usually as a result of prep_data_fs.
634 std::thread(&fdeEnableInternal, passwordType, password, encryptionFlags).detach();
635 return ok();
636}
637
638binder::Status VoldNativeService::fdeChangePassword(int32_t passwordType,
Paul Crowleyedf7a4e2018-09-18 15:14:18 -0700639 const std::string& password) {
Nikita Ioffe5d85bf32019-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_changepw(passwordType, password.c_str()));
644}
645
646binder::Status VoldNativeService::fdeVerifyPassword(const std::string& password) {
Nikita Ioffe5d85bf32019-10-23 17:19:13 +0100647 ENFORCE_SYSTEM_OR_ROOT;
Jeff Sharkey83b559c2017-09-12 16:30:52 -0600648 ACQUIRE_CRYPT_LOCK;
649
650 return translate(cryptfs_verify_passwd(password.c_str()));
651}
652
Paul Crowleyedf7a4e2018-09-18 15:14:18 -0700653binder::Status VoldNativeService::fdeGetField(const std::string& key, std::string* _aidl_return) {
Nikita Ioffe5d85bf32019-10-23 17:19:13 +0100654 ENFORCE_SYSTEM_OR_ROOT;
Jeff Sharkey83b559c2017-09-12 16:30:52 -0600655 ACQUIRE_CRYPT_LOCK;
656
657 char buf[PROPERTY_VALUE_MAX];
658 if (cryptfs_getfield(key.c_str(), buf, sizeof(buf)) != CRYPTO_GETFIELD_OK) {
659 return error(StringPrintf("Failed to read field %s", key.c_str()));
660 } else {
661 *_aidl_return = buf;
662 return ok();
663 }
664}
665
Paul Crowleyedf7a4e2018-09-18 15:14:18 -0700666binder::Status VoldNativeService::fdeSetField(const std::string& key, const std::string& value) {
Nikita Ioffe5d85bf32019-10-23 17:19:13 +0100667 ENFORCE_SYSTEM_OR_ROOT;
Jeff Sharkey83b559c2017-09-12 16:30:52 -0600668 ACQUIRE_CRYPT_LOCK;
669
670 return translate(cryptfs_setfield(key.c_str(), value.c_str()));
671}
672
673binder::Status VoldNativeService::fdeGetPasswordType(int32_t* _aidl_return) {
Nikita Ioffe5d85bf32019-10-23 17:19:13 +0100674 ENFORCE_SYSTEM_OR_ROOT;
Jeff Sharkey83b559c2017-09-12 16:30:52 -0600675 ACQUIRE_CRYPT_LOCK;
676
677 *_aidl_return = cryptfs_get_password_type();
678 return ok();
679}
680
681binder::Status VoldNativeService::fdeGetPassword(std::string* _aidl_return) {
Nikita Ioffe5d85bf32019-10-23 17:19:13 +0100682 ENFORCE_SYSTEM_OR_ROOT;
Jeff Sharkey83b559c2017-09-12 16:30:52 -0600683 ACQUIRE_CRYPT_LOCK;
684
685 const char* res = cryptfs_get_password();
686 if (res != nullptr) {
687 *_aidl_return = res;
688 }
689 return ok();
690}
691
692binder::Status VoldNativeService::fdeClearPassword() {
Nikita Ioffe5d85bf32019-10-23 17:19:13 +0100693 ENFORCE_SYSTEM_OR_ROOT;
Jeff Sharkey83b559c2017-09-12 16:30:52 -0600694 ACQUIRE_CRYPT_LOCK;
695
696 cryptfs_clear_password();
697 return ok();
698}
699
700binder::Status VoldNativeService::fbeEnable() {
Nikita Ioffe5d85bf32019-10-23 17:19:13 +0100701 ENFORCE_SYSTEM_OR_ROOT;
Jeff Sharkey83b559c2017-09-12 16:30:52 -0600702 ACQUIRE_CRYPT_LOCK;
703
Paul Crowleyc8a3ef32019-09-11 15:00:08 -0700704 return translateBool(fscrypt_initialize_systemwide_keys());
Jeff Sharkey83b559c2017-09-12 16:30:52 -0600705}
706
707binder::Status VoldNativeService::mountDefaultEncrypted() {
Nikita Ioffe5d85bf32019-10-23 17:19:13 +0100708 ENFORCE_SYSTEM_OR_ROOT;
Jeff Sharkey83b559c2017-09-12 16:30:52 -0600709 ACQUIRE_CRYPT_LOCK;
710
Eric Biggersa701c452018-10-23 13:06:55 -0700711 if (!fscrypt_is_native()) {
Jeff Sharkey83b559c2017-09-12 16:30:52 -0600712 // Spawn as thread so init can issue commands back to vold without
713 // causing deadlock, usually as a result of prep_data_fs.
714 std::thread(&cryptfs_mount_default_encrypted).detach();
Jeff Sharkey83b559c2017-09-12 16:30:52 -0600715 }
Paul Crowley0fd26262018-01-30 09:48:19 -0800716 return ok();
Jeff Sharkey83b559c2017-09-12 16:30:52 -0600717}
718
719binder::Status VoldNativeService::initUser0() {
Nikita Ioffe5d85bf32019-10-23 17:19:13 +0100720 ENFORCE_SYSTEM_OR_ROOT;
Jeff Sharkey83b559c2017-09-12 16:30:52 -0600721 ACQUIRE_CRYPT_LOCK;
722
Eric Biggersa701c452018-10-23 13:06:55 -0700723 return translateBool(fscrypt_init_user0());
Jeff Sharkey83b559c2017-09-12 16:30:52 -0600724}
725
726binder::Status VoldNativeService::isConvertibleToFbe(bool* _aidl_return) {
Nikita Ioffe5d85bf32019-10-23 17:19:13 +0100727 ENFORCE_SYSTEM_OR_ROOT;
Jeff Sharkey83b559c2017-09-12 16:30:52 -0600728 ACQUIRE_CRYPT_LOCK;
729
730 *_aidl_return = cryptfs_isConvertibleToFBE() != 0;
731 return ok();
732}
733
Paul Lawrence236e5e82019-06-25 14:44:33 -0700734binder::Status VoldNativeService::mountFstab(const std::string& blkDevice,
735 const std::string& mountPoint) {
Nikita Ioffe5d85bf32019-10-23 17:19:13 +0100736 ENFORCE_SYSTEM_OR_ROOT;
Paul Crowley0fd26262018-01-30 09:48:19 -0800737 ACQUIRE_LOCK;
738
Paul Lawrence236e5e82019-06-25 14:44:33 -0700739 return translateBool(fscrypt_mount_metadata_encrypted(blkDevice, mountPoint, false));
Paul Crowley0fd26262018-01-30 09:48:19 -0800740}
741
Paul Lawrence236e5e82019-06-25 14:44:33 -0700742binder::Status VoldNativeService::encryptFstab(const std::string& blkDevice,
743 const std::string& mountPoint) {
Nikita Ioffe5d85bf32019-10-23 17:19:13 +0100744 ENFORCE_SYSTEM_OR_ROOT;
Paul Crowley0fd26262018-01-30 09:48:19 -0800745 ACQUIRE_LOCK;
746
Paul Lawrence236e5e82019-06-25 14:44:33 -0700747 return translateBool(fscrypt_mount_metadata_encrypted(blkDevice, mountPoint, true));
Paul Crowley0fd26262018-01-30 09:48:19 -0800748}
749
Paul Crowleyedf7a4e2018-09-18 15:14:18 -0700750binder::Status VoldNativeService::createUserKey(int32_t userId, int32_t userSerial, bool ephemeral) {
Nikita Ioffe5d85bf32019-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_vold_create_user_key(userId, userSerial, ephemeral));
Jeff Sharkey83b559c2017-09-12 16:30:52 -0600755}
756
757binder::Status VoldNativeService::destroyUserKey(int32_t userId) {
Nikita Ioffe5d85bf32019-10-23 17:19:13 +0100758 ENFORCE_SYSTEM_OR_ROOT;
Jeff Sharkey83b559c2017-09-12 16:30:52 -0600759 ACQUIRE_CRYPT_LOCK;
760
Eric Biggersa701c452018-10-23 13:06:55 -0700761 return translateBool(fscrypt_destroy_user_key(userId));
Jeff Sharkey83b559c2017-09-12 16:30:52 -0600762}
763
764binder::Status VoldNativeService::addUserKeyAuth(int32_t userId, int32_t userSerial,
Paul Crowleyedf7a4e2018-09-18 15:14:18 -0700765 const std::string& token,
766 const std::string& secret) {
Nikita Ioffe5d85bf32019-10-23 17:19:13 +0100767 ENFORCE_SYSTEM_OR_ROOT;
Jeff Sharkey83b559c2017-09-12 16:30:52 -0600768 ACQUIRE_CRYPT_LOCK;
769
Eric Biggersa701c452018-10-23 13:06:55 -0700770 return translateBool(fscrypt_add_user_key_auth(userId, userSerial, token, secret));
Jeff Sharkey83b559c2017-09-12 16:30:52 -0600771}
772
Barani Muthukumaran9ad51ad2019-10-31 22:59:34 -0700773binder::Status VoldNativeService::clearUserKeyAuth(int32_t userId, int32_t userSerial,
774 const std::string& token,
775 const std::string& secret) {
776 ENFORCE_SYSTEM_OR_ROOT;
777 ACQUIRE_CRYPT_LOCK;
778
779 return translateBool(fscrypt_clear_user_key_auth(userId, userSerial, token, secret));
780}
781
Jeff Sharkey83b559c2017-09-12 16:30:52 -0600782binder::Status VoldNativeService::fixateNewestUserKeyAuth(int32_t userId) {
Nikita Ioffe5d85bf32019-10-23 17:19:13 +0100783 ENFORCE_SYSTEM_OR_ROOT;
Jeff Sharkey83b559c2017-09-12 16:30:52 -0600784 ACQUIRE_CRYPT_LOCK;
785
Eric Biggersa701c452018-10-23 13:06:55 -0700786 return translateBool(fscrypt_fixate_newest_user_key_auth(userId));
Jeff Sharkey83b559c2017-09-12 16:30:52 -0600787}
788
789binder::Status VoldNativeService::unlockUserKey(int32_t userId, int32_t userSerial,
Paul Crowleyedf7a4e2018-09-18 15:14:18 -0700790 const std::string& token,
791 const std::string& secret) {
Nikita Ioffe5d85bf32019-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_unlock_user_key(userId, userSerial, token, secret));
Jeff Sharkey83b559c2017-09-12 16:30:52 -0600796}
797
798binder::Status VoldNativeService::lockUserKey(int32_t userId) {
Nikita Ioffe5d85bf32019-10-23 17:19:13 +0100799 ENFORCE_SYSTEM_OR_ROOT;
Jeff Sharkey83b559c2017-09-12 16:30:52 -0600800 ACQUIRE_CRYPT_LOCK;
801
Eric Biggersa701c452018-10-23 13:06:55 -0700802 return translateBool(fscrypt_lock_user_key(userId));
Jeff Sharkey83b559c2017-09-12 16:30:52 -0600803}
804
805binder::Status VoldNativeService::prepareUserStorage(const std::unique_ptr<std::string>& uuid,
Paul Crowleyedf7a4e2018-09-18 15:14:18 -0700806 int32_t userId, int32_t userSerial,
807 int32_t flags) {
Nikita Ioffe5d85bf32019-10-23 17:19:13 +0100808 ENFORCE_SYSTEM_OR_ROOT;
Paul Crowley3b71fc52017-10-09 10:55:21 -0700809 std::string empty_string = "";
810 auto uuid_ = uuid ? *uuid : empty_string;
Paul Crowley06f762d2017-10-16 10:59:51 -0700811 CHECK_ARGUMENT_HEX(uuid_);
812
813 ACQUIRE_CRYPT_LOCK;
Eric Biggersa701c452018-10-23 13:06:55 -0700814 return translateBool(fscrypt_prepare_user_storage(uuid_, userId, userSerial, flags));
Jeff Sharkey83b559c2017-09-12 16:30:52 -0600815}
816
817binder::Status VoldNativeService::destroyUserStorage(const std::unique_ptr<std::string>& uuid,
Paul Crowleyedf7a4e2018-09-18 15:14:18 -0700818 int32_t userId, int32_t flags) {
Nikita Ioffe5d85bf32019-10-23 17:19:13 +0100819 ENFORCE_SYSTEM_OR_ROOT;
Paul Crowley3b71fc52017-10-09 10:55:21 -0700820 std::string empty_string = "";
821 auto uuid_ = uuid ? *uuid : empty_string;
Paul Crowley06f762d2017-10-16 10:59:51 -0700822 CHECK_ARGUMENT_HEX(uuid_);
823
824 ACQUIRE_CRYPT_LOCK;
Eric Biggersa701c452018-10-23 13:06:55 -0700825 return translateBool(fscrypt_destroy_user_storage(uuid_, userId, flags));
Jeff Sharkey83b559c2017-09-12 16:30:52 -0600826}
827
Sudheer Shankafa6a1742018-10-04 16:26:22 -0700828binder::Status VoldNativeService::prepareSandboxForApp(const std::string& packageName,
829 int32_t appId, const std::string& sandboxId,
830 int32_t userId) {
Sudheer Shanka5fceb482019-04-29 10:46:35 -0700831 return ok();
Sudheer Shankafa6a1742018-10-04 16:26:22 -0700832}
833
834binder::Status VoldNativeService::destroySandboxForApp(const std::string& packageName,
Sudheer Shanka69bc40f2018-10-25 11:06:55 -0700835 const std::string& sandboxId,
Sudheer Shankafa6a1742018-10-04 16:26:22 -0700836 int32_t userId) {
Sudheer Shanka5fceb482019-04-29 10:46:35 -0700837 return ok();
Sudheer Shankac7562092018-08-24 10:20:56 -0700838}
839
Daniel Rosenberge334aab2018-10-10 18:52:04 -0700840binder::Status VoldNativeService::startCheckpoint(int32_t retry) {
Nikita Ioffe5d85bf32019-10-23 17:19:13 +0100841 ENFORCE_SYSTEM_OR_ROOT;
Daniel Rosenberg4f684712018-08-28 01:58:49 -0700842 ACQUIRE_LOCK;
843
Daniel Rosenberge334aab2018-10-10 18:52:04 -0700844 return cp_startCheckpoint(retry);
Daniel Rosenberg4f684712018-08-28 01:58:49 -0700845}
846
Daniel Rosenbergd3992492018-10-02 17:40:44 -0700847binder::Status VoldNativeService::needsRollback(bool* _aidl_return) {
Nikita Ioffe5d85bf32019-10-23 17:19:13 +0100848 ENFORCE_SYSTEM_OR_ROOT;
Daniel Rosenbergd3992492018-10-02 17:40:44 -0700849 ACQUIRE_LOCK;
850
851 *_aidl_return = cp_needsRollback();
852 return ok();
853}
854
Daniel Rosenberg4f684712018-08-28 01:58:49 -0700855binder::Status VoldNativeService::needsCheckpoint(bool* _aidl_return) {
Nikita Ioffe5d85bf32019-10-23 17:19:13 +0100856 ENFORCE_SYSTEM_OR_ROOT;
Daniel Rosenberg4f684712018-08-28 01:58:49 -0700857 ACQUIRE_LOCK;
858
859 *_aidl_return = cp_needsCheckpoint();
860 return ok();
861}
862
Daniel Rosenberge334aab2018-10-10 18:52:04 -0700863binder::Status VoldNativeService::commitChanges() {
Nikita Ioffe5d85bf32019-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_commitChanges();
Daniel Rosenberg4f684712018-08-28 01:58:49 -0700868}
869
Daniel Rosenberge334aab2018-10-10 18:52:04 -0700870binder::Status VoldNativeService::prepareCheckpoint() {
Nikita Ioffe5d85bf32019-10-23 17:19:13 +0100871 ENFORCE_SYSTEM_OR_ROOT;
Daniel Rosenberg4f684712018-08-28 01:58:49 -0700872 ACQUIRE_LOCK;
873
Daniel Rosenberge334aab2018-10-10 18:52:04 -0700874 return cp_prepareCheckpoint();
Daniel Rosenberg4f684712018-08-28 01:58:49 -0700875}
876
Daniel Rosenberge334aab2018-10-10 18:52:04 -0700877binder::Status VoldNativeService::restoreCheckpoint(const std::string& mountPoint) {
Nikita Ioffe5d85bf32019-10-23 17:19:13 +0100878 ENFORCE_SYSTEM_OR_ROOT;
Paul Lawrence1abb2fe2018-09-21 10:49:57 -0700879 CHECK_ARGUMENT_PATH(mountPoint);
880 ACQUIRE_LOCK;
881
Daniel Rosenberge334aab2018-10-10 18:52:04 -0700882 return cp_restoreCheckpoint(mountPoint);
Paul Lawrence1abb2fe2018-09-21 10:49:57 -0700883}
884
Daniel Rosenbergdda59812019-03-06 17:45:17 -0800885binder::Status VoldNativeService::restoreCheckpointPart(const std::string& mountPoint, int count) {
Nikita Ioffe5d85bf32019-10-23 17:19:13 +0100886 ENFORCE_SYSTEM_OR_ROOT;
Daniel Rosenbergdda59812019-03-06 17:45:17 -0800887 CHECK_ARGUMENT_PATH(mountPoint);
888 ACQUIRE_LOCK;
889
890 return cp_restoreCheckpoint(mountPoint, count);
891}
892
Daniel Rosenberge334aab2018-10-10 18:52:04 -0700893binder::Status VoldNativeService::markBootAttempt() {
Nikita Ioffe5d85bf32019-10-23 17:19:13 +0100894 ENFORCE_SYSTEM_OR_ROOT;
Daniel Rosenberg4f684712018-08-28 01:58:49 -0700895 ACQUIRE_LOCK;
896
Daniel Rosenberge334aab2018-10-10 18:52:04 -0700897 return cp_markBootAttempt();
Daniel Rosenberg4f684712018-08-28 01:58:49 -0700898}
899
Daniel Rosenberga59e4392019-03-20 17:02:47 -0700900binder::Status VoldNativeService::abortChanges(const std::string& message, bool retry) {
Nikita Ioffe5d85bf32019-10-23 17:19:13 +0100901 ENFORCE_SYSTEM_OR_ROOT;
Daniel Rosenberg4f684712018-08-28 01:58:49 -0700902 ACQUIRE_LOCK;
903
Daniel Rosenberga59e4392019-03-20 17:02:47 -0700904 cp_abortChanges(message, retry);
905 return ok();
Daniel Rosenberg4f684712018-08-28 01:58:49 -0700906}
907
Daniel Rosenberg9b667fb2019-01-22 17:27:25 -0800908binder::Status VoldNativeService::supportsCheckpoint(bool* _aidl_return) {
Nikita Ioffe5d85bf32019-10-23 17:19:13 +0100909 ENFORCE_SYSTEM_OR_ROOT;
Daniel Rosenberg9b667fb2019-01-22 17:27:25 -0800910 ACQUIRE_LOCK;
911
912 return cp_supportsCheckpoint(*_aidl_return);
913}
914
Paul Lawrencec5c79c52019-03-18 13:36:40 -0700915binder::Status VoldNativeService::supportsBlockCheckpoint(bool* _aidl_return) {
Nikita Ioffe5d85bf32019-10-23 17:19:13 +0100916 ENFORCE_SYSTEM_OR_ROOT;
Paul Lawrencec5c79c52019-03-18 13:36:40 -0700917 ACQUIRE_LOCK;
918
919 return cp_supportsBlockCheckpoint(*_aidl_return);
920}
921
922binder::Status VoldNativeService::supportsFileCheckpoint(bool* _aidl_return) {
Nikita Ioffe5d85bf32019-10-23 17:19:13 +0100923 ENFORCE_SYSTEM_OR_ROOT;
Paul Lawrencec5c79c52019-03-18 13:36:40 -0700924 ACQUIRE_LOCK;
925
926 return cp_supportsFileCheckpoint(*_aidl_return);
927}
928
Nikita Ioffea5798fc2019-10-11 16:38:21 +0100929binder::Status VoldNativeService::resetCheckpoint() {
Nikita Ioffe5d85bf32019-10-23 17:19:13 +0100930 ENFORCE_SYSTEM_OR_ROOT;
Nikita Ioffea5798fc2019-10-11 16:38:21 +0100931 ACQUIRE_LOCK;
932
933 cp_resetCheckpoint();
934 return ok();
935}
936
Yurii Zubrytskyi3497cb52020-01-10 11:54:06 -0800937binder::Status VoldNativeService::incFsEnabled(bool* _aidl_return) {
938 *_aidl_return = IncFs_IsEnabled();
Songchun Fanab55cec2019-12-02 10:50:12 -0800939 return ok();
940}
941
942binder::Status VoldNativeService::mountIncFs(
Yurii Zubrytskyi3497cb52020-01-10 11:54:06 -0800943 const std::string& backingPath, const std::string& targetDir, int32_t flags,
Songchun Fanab55cec2019-12-02 10:50:12 -0800944 ::android::os::incremental::IncrementalFileSystemControlParcel* _aidl_return) {
Yurii Zubrytskyi3497cb52020-01-10 11:54:06 -0800945 auto result = IncFs_Mount(backingPath.c_str(), targetDir.c_str(),
946 {.flags = IncFsMountFlags(flags),
947 .defaultReadTimeoutMs = INCFS_DEFAULT_READ_TIMEOUT_MS,
948 .readLogBufferPages = 4});
949 if (result.cmd < 0) {
950 return translate(result.cmd);
Songchun Fanab55cec2019-12-02 10:50:12 -0800951 }
Songchun Fanab55cec2019-12-02 10:50:12 -0800952 using unique_fd = ::android::base::unique_fd;
Yurii Zubrytskyi3497cb52020-01-10 11:54:06 -0800953 _aidl_return->cmd.reset(unique_fd(result.cmd));
954 _aidl_return->pendingReads.reset(unique_fd(result.pendingReads));
955 if (result.logs >= 0) {
956 _aidl_return->log.reset(unique_fd(result.logs));
Songchun Fanab55cec2019-12-02 10:50:12 -0800957 }
958 return ok();
959}
960
961binder::Status VoldNativeService::unmountIncFs(const std::string& dir) {
962 return translate(IncFs_Unmount(dir.c_str()));
963}
964
965binder::Status VoldNativeService::bindMount(const std::string& sourceDir,
966 const std::string& targetDir) {
967 return translate(IncFs_BindMount(sourceDir.c_str(), targetDir.c_str()));
968}
969
Jeff Sharkey068c6be2017-09-06 13:47:40 -0600970} // namespace vold
971} // namespace android