blob: 57cee2353b86a8b3cc6e50ceb0a4cfb1e23b9cc6 [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
Ricky Wai6b122572020-02-28 16:30:47 +0000461binder::Status VoldNativeService::remountAppStorageDirs(int uid, int pid,
462 const std::vector<std::string>& packageNames) {
463 ENFORCE_SYSTEM_OR_ROOT;
464 ACQUIRE_LOCK;
465
466 return translate(VolumeManager::Instance()->remountAppStorageDirs(uid, pid, packageNames));
467}
468
Martijn Coenen8a68a072020-02-12 15:29:02 +0100469binder::Status VoldNativeService::setupAppDir(const std::string& path, int32_t appUid) {
Nikita Ioffe5d85bf32019-10-23 17:19:13 +0100470 ENFORCE_SYSTEM_OR_ROOT;
Jeff Sharkeyec4fda22017-09-12 13:19:24 -0600471 CHECK_ARGUMENT_PATH(path);
Jeff Sharkey9462bdd2017-09-07 15:27:28 -0600472 ACQUIRE_LOCK;
473
Martijn Coenen8a68a072020-02-12 15:29:02 +0100474 return translate(VolumeManager::Instance()->setupAppDir(path, appUid));
Jeff Sharkey9462bdd2017-09-07 15:27:28 -0600475}
476
Martijn Coenen816f4d92020-02-18 15:06:37 +0100477binder::Status VoldNativeService::fixupAppDir(const std::string& path, int32_t appUid) {
478 ENFORCE_SYSTEM_OR_ROOT;
479 CHECK_ARGUMENT_PATH(path);
480 ACQUIRE_LOCK;
481
482 return translate(VolumeManager::Instance()->fixupAppDir(path, appUid));
483}
484
Jeff Sharkey11c2d382017-09-11 10:32:01 -0600485binder::Status VoldNativeService::createObb(const std::string& sourcePath,
Paul Crowleyedf7a4e2018-09-18 15:14:18 -0700486 const std::string& sourceKey, int32_t ownerGid,
487 std::string* _aidl_return) {
Nikita Ioffe5d85bf32019-10-23 17:19:13 +0100488 ENFORCE_SYSTEM_OR_ROOT;
Jeff Sharkeyec4fda22017-09-12 13:19:24 -0600489 CHECK_ARGUMENT_PATH(sourcePath);
490 CHECK_ARGUMENT_HEX(sourceKey);
Jeff Sharkey11c2d382017-09-11 10:32:01 -0600491 ACQUIRE_LOCK;
492
493 return translate(
Paul Crowleyedf7a4e2018-09-18 15:14:18 -0700494 VolumeManager::Instance()->createObb(sourcePath, sourceKey, ownerGid, _aidl_return));
Jeff Sharkey11c2d382017-09-11 10:32:01 -0600495}
496
497binder::Status VoldNativeService::destroyObb(const std::string& volId) {
Nikita Ioffe5d85bf32019-10-23 17:19:13 +0100498 ENFORCE_SYSTEM_OR_ROOT;
Jeff Sharkeyec4fda22017-09-12 13:19:24 -0600499 CHECK_ARGUMENT_ID(volId);
Jeff Sharkey11c2d382017-09-11 10:32:01 -0600500 ACQUIRE_LOCK;
501
502 return translate(VolumeManager::Instance()->destroyObb(volId));
503}
504
Risan82e90de2020-02-04 16:07:21 +0900505binder::Status VoldNativeService::createStubVolume(const std::string& sourcePath,
506 const std::string& mountPath,
507 const std::string& fsType,
508 const std::string& fsUuid,
509 const std::string& fsLabel, int32_t flags,
510 std::string* _aidl_return) {
Nikita Ioffe5d85bf32019-10-23 17:19:13 +0100511 ENFORCE_SYSTEM_OR_ROOT;
Risan8c9f3322018-10-29 08:52:56 +0900512 CHECK_ARGUMENT_PATH(sourcePath);
513 CHECK_ARGUMENT_PATH(mountPath);
514 CHECK_ARGUMENT_HEX(fsUuid);
515 // Label limitation seems to be different between fs (including allowed characters), so checking
516 // is quite meaningless.
517 ACQUIRE_LOCK;
518
Risan82e90de2020-02-04 16:07:21 +0900519 return translate(VolumeManager::Instance()->createStubVolume(
520 sourcePath, mountPath, fsType, fsUuid, fsLabel, flags, _aidl_return));
Risan8c9f3322018-10-29 08:52:56 +0900521}
522
523binder::Status VoldNativeService::destroyStubVolume(const std::string& volId) {
Nikita Ioffe5d85bf32019-10-23 17:19:13 +0100524 ENFORCE_SYSTEM_OR_ROOT;
Risan8c9f3322018-10-29 08:52:56 +0900525 CHECK_ARGUMENT_ID(volId);
526 ACQUIRE_LOCK;
527
528 return translate(VolumeManager::Instance()->destroyStubVolume(volId));
529}
530
Paul Crowleyedf7a4e2018-09-18 15:14:18 -0700531binder::Status VoldNativeService::fstrim(
532 int32_t fstrimFlags, const android::sp<android::os::IVoldTaskListener>& listener) {
Nikita Ioffe5d85bf32019-10-23 17:19:13 +0100533 ENFORCE_SYSTEM_OR_ROOT;
Jeff Sharkey11c2d382017-09-11 10:32:01 -0600534 ACQUIRE_LOCK;
535
Paul Crowleyedf7a4e2018-09-18 15:14:18 -0700536 std::thread([=]() { android::vold::Trim(listener); }).detach();
Jeff Sharkey11c2d382017-09-11 10:32:01 -0600537 return ok();
538}
539
Jin Qiana370c142017-10-17 15:41:45 -0700540binder::Status VoldNativeService::runIdleMaint(
Paul Crowleyedf7a4e2018-09-18 15:14:18 -0700541 const android::sp<android::os::IVoldTaskListener>& listener) {
Nikita Ioffe5d85bf32019-10-23 17:19:13 +0100542 ENFORCE_SYSTEM_OR_ROOT;
Jin Qiana370c142017-10-17 15:41:45 -0700543 ACQUIRE_LOCK;
544
Paul Crowleyedf7a4e2018-09-18 15:14:18 -0700545 std::thread([=]() { android::vold::RunIdleMaint(listener); }).detach();
Jin Qiana370c142017-10-17 15:41:45 -0700546 return ok();
547}
548
549binder::Status VoldNativeService::abortIdleMaint(
Paul Crowleyedf7a4e2018-09-18 15:14:18 -0700550 const android::sp<android::os::IVoldTaskListener>& listener) {
Nikita Ioffe5d85bf32019-10-23 17:19:13 +0100551 ENFORCE_SYSTEM_OR_ROOT;
Jin Qiana370c142017-10-17 15:41:45 -0700552 ACQUIRE_LOCK;
553
Paul Crowleyedf7a4e2018-09-18 15:14:18 -0700554 std::thread([=]() { android::vold::AbortIdleMaint(listener); }).detach();
Jin Qiana370c142017-10-17 15:41:45 -0700555 return ok();
556}
557
Risan8f6198d2018-10-26 20:56:45 -0600558binder::Status VoldNativeService::mountAppFuse(int32_t uid, int32_t mountId,
Paul Crowleyedf7a4e2018-09-18 15:14:18 -0700559 android::base::unique_fd* _aidl_return) {
Nikita Ioffe5d85bf32019-10-23 17:19:13 +0100560 ENFORCE_SYSTEM_OR_ROOT;
Jeff Sharkey11c2d382017-09-11 10:32:01 -0600561 ACQUIRE_LOCK;
562
Risan8f6198d2018-10-26 20:56:45 -0600563 return translate(VolumeManager::Instance()->mountAppFuse(uid, mountId, _aidl_return));
Jeff Sharkey11c2d382017-09-11 10:32:01 -0600564}
565
Risan8f6198d2018-10-26 20:56:45 -0600566binder::Status VoldNativeService::unmountAppFuse(int32_t uid, int32_t mountId) {
Nikita Ioffe5d85bf32019-10-23 17:19:13 +0100567 ENFORCE_SYSTEM_OR_ROOT;
Jeff Sharkey11c2d382017-09-11 10:32:01 -0600568 ACQUIRE_LOCK;
569
Risan8f6198d2018-10-26 20:56:45 -0600570 return translate(VolumeManager::Instance()->unmountAppFuse(uid, mountId));
571}
572
573binder::Status VoldNativeService::openAppFuseFile(int32_t uid, int32_t mountId, int32_t fileId,
574 int32_t flags,
575 android::base::unique_fd* _aidl_return) {
Nikita Ioffe5d85bf32019-10-23 17:19:13 +0100576 ENFORCE_SYSTEM_OR_ROOT;
Risan8f6198d2018-10-26 20:56:45 -0600577 ACQUIRE_LOCK;
578
579 int fd = VolumeManager::Instance()->openAppFuseFile(uid, mountId, fileId, flags);
580 if (fd == -1) {
581 return error("Failed to open AppFuse file for uid: " + std::to_string(uid) +
582 " mountId: " + std::to_string(mountId) + " fileId: " + std::to_string(fileId) +
583 " flags: " + std::to_string(flags));
584 }
585
586 *_aidl_return = android::base::unique_fd(fd);
587 return ok();
Jeff Sharkey11c2d382017-09-11 10:32:01 -0600588}
589
Jeff Sharkey83b559c2017-09-12 16:30:52 -0600590binder::Status VoldNativeService::fdeCheckPassword(const std::string& password) {
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 return translate(cryptfs_check_passwd(password.c_str()));
595}
596
597binder::Status VoldNativeService::fdeRestart() {
Nikita Ioffe5d85bf32019-10-23 17:19:13 +0100598 ENFORCE_SYSTEM_OR_ROOT;
Jeff Sharkey83b559c2017-09-12 16:30:52 -0600599 ACQUIRE_CRYPT_LOCK;
600
601 // Spawn as thread so init can issue commands back to vold without
602 // causing deadlock, usually as a result of prep_data_fs.
603 std::thread(&cryptfs_restart).detach();
604 return ok();
605}
606
607binder::Status VoldNativeService::fdeComplete(int32_t* _aidl_return) {
Nikita Ioffe5d85bf32019-10-23 17:19:13 +0100608 ENFORCE_SYSTEM_OR_ROOT;
Jeff Sharkey83b559c2017-09-12 16:30:52 -0600609 ACQUIRE_CRYPT_LOCK;
610
611 *_aidl_return = cryptfs_crypto_complete();
612 return ok();
613}
614
615static int fdeEnableInternal(int32_t passwordType, const std::string& password,
Paul Crowleyedf7a4e2018-09-18 15:14:18 -0700616 int32_t encryptionFlags) {
Jeff Sharkey83b559c2017-09-12 16:30:52 -0600617 bool noUi = (encryptionFlags & VoldNativeService::ENCRYPTION_FLAG_NO_UI) != 0;
618
Jeff Sharkey83b559c2017-09-12 16:30:52 -0600619 for (int tries = 0; tries < 2; ++tries) {
620 int rc;
621 if (passwordType == VoldNativeService::PASSWORD_TYPE_DEFAULT) {
Paul Lawrence7ee87cf2017-12-22 10:12:06 -0800622 rc = cryptfs_enable_default(noUi);
Jeff Sharkey83b559c2017-09-12 16:30:52 -0600623 } else {
Paul Lawrence7ee87cf2017-12-22 10:12:06 -0800624 rc = cryptfs_enable(passwordType, password.c_str(), noUi);
Jeff Sharkey83b559c2017-09-12 16:30:52 -0600625 }
626
627 if (rc == 0) {
628 return 0;
629 } else if (tries == 0) {
Jeff Sharkey3472e522017-10-06 18:02:53 -0600630 KillProcessesWithOpenFiles(DATA_MNT_POINT, SIGKILL);
Jeff Sharkey83b559c2017-09-12 16:30:52 -0600631 }
632 }
633
634 return -1;
635}
636
Paul Crowleyedf7a4e2018-09-18 15:14:18 -0700637binder::Status VoldNativeService::fdeEnable(int32_t passwordType, const std::string& password,
638 int32_t encryptionFlags) {
Nikita Ioffe5d85bf32019-10-23 17:19:13 +0100639 ENFORCE_SYSTEM_OR_ROOT;
Jeff Sharkey83b559c2017-09-12 16:30:52 -0600640 ACQUIRE_CRYPT_LOCK;
641
Paul Crowley0fd26262018-01-30 09:48:19 -0800642 LOG(DEBUG) << "fdeEnable(" << passwordType << ", *, " << encryptionFlags << ")";
Eric Biggersa701c452018-10-23 13:06:55 -0700643 if (fscrypt_is_native()) {
644 LOG(ERROR) << "fscrypt_is_native, fdeEnable invalid";
645 return error("fscrypt_is_native, fdeEnable invalid");
Jeff Sharkey83b559c2017-09-12 16:30:52 -0600646 }
Eric Biggersa701c452018-10-23 13:06:55 -0700647 LOG(DEBUG) << "!fscrypt_is_native, spawning fdeEnableInternal";
Jeff Sharkey83b559c2017-09-12 16:30:52 -0600648
649 // Spawn as thread so init can issue commands back to vold without
650 // causing deadlock, usually as a result of prep_data_fs.
651 std::thread(&fdeEnableInternal, passwordType, password, encryptionFlags).detach();
652 return ok();
653}
654
655binder::Status VoldNativeService::fdeChangePassword(int32_t passwordType,
Paul Crowleyedf7a4e2018-09-18 15:14:18 -0700656 const std::string& password) {
Nikita Ioffe5d85bf32019-10-23 17:19:13 +0100657 ENFORCE_SYSTEM_OR_ROOT;
Jeff Sharkey83b559c2017-09-12 16:30:52 -0600658 ACQUIRE_CRYPT_LOCK;
659
660 return translate(cryptfs_changepw(passwordType, password.c_str()));
661}
662
663binder::Status VoldNativeService::fdeVerifyPassword(const std::string& password) {
Nikita Ioffe5d85bf32019-10-23 17:19:13 +0100664 ENFORCE_SYSTEM_OR_ROOT;
Jeff Sharkey83b559c2017-09-12 16:30:52 -0600665 ACQUIRE_CRYPT_LOCK;
666
667 return translate(cryptfs_verify_passwd(password.c_str()));
668}
669
Paul Crowleyedf7a4e2018-09-18 15:14:18 -0700670binder::Status VoldNativeService::fdeGetField(const std::string& key, std::string* _aidl_return) {
Nikita Ioffe5d85bf32019-10-23 17:19:13 +0100671 ENFORCE_SYSTEM_OR_ROOT;
Jeff Sharkey83b559c2017-09-12 16:30:52 -0600672 ACQUIRE_CRYPT_LOCK;
673
674 char buf[PROPERTY_VALUE_MAX];
675 if (cryptfs_getfield(key.c_str(), buf, sizeof(buf)) != CRYPTO_GETFIELD_OK) {
676 return error(StringPrintf("Failed to read field %s", key.c_str()));
677 } else {
678 *_aidl_return = buf;
679 return ok();
680 }
681}
682
Paul Crowleyedf7a4e2018-09-18 15:14:18 -0700683binder::Status VoldNativeService::fdeSetField(const std::string& key, const std::string& value) {
Nikita Ioffe5d85bf32019-10-23 17:19:13 +0100684 ENFORCE_SYSTEM_OR_ROOT;
Jeff Sharkey83b559c2017-09-12 16:30:52 -0600685 ACQUIRE_CRYPT_LOCK;
686
687 return translate(cryptfs_setfield(key.c_str(), value.c_str()));
688}
689
690binder::Status VoldNativeService::fdeGetPasswordType(int32_t* _aidl_return) {
Nikita Ioffe5d85bf32019-10-23 17:19:13 +0100691 ENFORCE_SYSTEM_OR_ROOT;
Jeff Sharkey83b559c2017-09-12 16:30:52 -0600692 ACQUIRE_CRYPT_LOCK;
693
694 *_aidl_return = cryptfs_get_password_type();
695 return ok();
696}
697
698binder::Status VoldNativeService::fdeGetPassword(std::string* _aidl_return) {
Nikita Ioffe5d85bf32019-10-23 17:19:13 +0100699 ENFORCE_SYSTEM_OR_ROOT;
Jeff Sharkey83b559c2017-09-12 16:30:52 -0600700 ACQUIRE_CRYPT_LOCK;
701
702 const char* res = cryptfs_get_password();
703 if (res != nullptr) {
704 *_aidl_return = res;
705 }
706 return ok();
707}
708
709binder::Status VoldNativeService::fdeClearPassword() {
Nikita Ioffe5d85bf32019-10-23 17:19:13 +0100710 ENFORCE_SYSTEM_OR_ROOT;
Jeff Sharkey83b559c2017-09-12 16:30:52 -0600711 ACQUIRE_CRYPT_LOCK;
712
713 cryptfs_clear_password();
714 return ok();
715}
716
717binder::Status VoldNativeService::fbeEnable() {
Nikita Ioffe5d85bf32019-10-23 17:19:13 +0100718 ENFORCE_SYSTEM_OR_ROOT;
Jeff Sharkey83b559c2017-09-12 16:30:52 -0600719 ACQUIRE_CRYPT_LOCK;
720
Paul Crowleyc8a3ef32019-09-11 15:00:08 -0700721 return translateBool(fscrypt_initialize_systemwide_keys());
Jeff Sharkey83b559c2017-09-12 16:30:52 -0600722}
723
724binder::Status VoldNativeService::mountDefaultEncrypted() {
Nikita Ioffe5d85bf32019-10-23 17:19:13 +0100725 ENFORCE_SYSTEM_OR_ROOT;
Jeff Sharkey83b559c2017-09-12 16:30:52 -0600726 ACQUIRE_CRYPT_LOCK;
727
Eric Biggersa701c452018-10-23 13:06:55 -0700728 if (!fscrypt_is_native()) {
Jeff Sharkey83b559c2017-09-12 16:30:52 -0600729 // Spawn as thread so init can issue commands back to vold without
730 // causing deadlock, usually as a result of prep_data_fs.
731 std::thread(&cryptfs_mount_default_encrypted).detach();
Jeff Sharkey83b559c2017-09-12 16:30:52 -0600732 }
Paul Crowley0fd26262018-01-30 09:48:19 -0800733 return ok();
Jeff Sharkey83b559c2017-09-12 16:30:52 -0600734}
735
736binder::Status VoldNativeService::initUser0() {
Nikita Ioffe5d85bf32019-10-23 17:19:13 +0100737 ENFORCE_SYSTEM_OR_ROOT;
Jeff Sharkey83b559c2017-09-12 16:30:52 -0600738 ACQUIRE_CRYPT_LOCK;
739
Eric Biggersa701c452018-10-23 13:06:55 -0700740 return translateBool(fscrypt_init_user0());
Jeff Sharkey83b559c2017-09-12 16:30:52 -0600741}
742
743binder::Status VoldNativeService::isConvertibleToFbe(bool* _aidl_return) {
Nikita Ioffe5d85bf32019-10-23 17:19:13 +0100744 ENFORCE_SYSTEM_OR_ROOT;
Jeff Sharkey83b559c2017-09-12 16:30:52 -0600745 ACQUIRE_CRYPT_LOCK;
746
747 *_aidl_return = cryptfs_isConvertibleToFBE() != 0;
748 return ok();
749}
750
Paul Lawrence236e5e82019-06-25 14:44:33 -0700751binder::Status VoldNativeService::mountFstab(const std::string& blkDevice,
752 const std::string& mountPoint) {
Nikita Ioffe5d85bf32019-10-23 17:19:13 +0100753 ENFORCE_SYSTEM_OR_ROOT;
Paul Crowley0fd26262018-01-30 09:48:19 -0800754 ACQUIRE_LOCK;
755
Paul Lawrence236e5e82019-06-25 14:44:33 -0700756 return translateBool(fscrypt_mount_metadata_encrypted(blkDevice, mountPoint, false));
Paul Crowley0fd26262018-01-30 09:48:19 -0800757}
758
Paul Lawrence236e5e82019-06-25 14:44:33 -0700759binder::Status VoldNativeService::encryptFstab(const std::string& blkDevice,
760 const std::string& mountPoint) {
Nikita Ioffe5d85bf32019-10-23 17:19:13 +0100761 ENFORCE_SYSTEM_OR_ROOT;
Paul Crowley0fd26262018-01-30 09:48:19 -0800762 ACQUIRE_LOCK;
763
Paul Lawrence236e5e82019-06-25 14:44:33 -0700764 return translateBool(fscrypt_mount_metadata_encrypted(blkDevice, mountPoint, true));
Paul Crowley0fd26262018-01-30 09:48:19 -0800765}
766
Paul Crowleyedf7a4e2018-09-18 15:14:18 -0700767binder::Status VoldNativeService::createUserKey(int32_t userId, int32_t userSerial, bool ephemeral) {
Nikita Ioffe5d85bf32019-10-23 17:19:13 +0100768 ENFORCE_SYSTEM_OR_ROOT;
Jeff Sharkey83b559c2017-09-12 16:30:52 -0600769 ACQUIRE_CRYPT_LOCK;
770
Eric Biggersa701c452018-10-23 13:06:55 -0700771 return translateBool(fscrypt_vold_create_user_key(userId, userSerial, ephemeral));
Jeff Sharkey83b559c2017-09-12 16:30:52 -0600772}
773
774binder::Status VoldNativeService::destroyUserKey(int32_t userId) {
Nikita Ioffe5d85bf32019-10-23 17:19:13 +0100775 ENFORCE_SYSTEM_OR_ROOT;
Jeff Sharkey83b559c2017-09-12 16:30:52 -0600776 ACQUIRE_CRYPT_LOCK;
777
Eric Biggersa701c452018-10-23 13:06:55 -0700778 return translateBool(fscrypt_destroy_user_key(userId));
Jeff Sharkey83b559c2017-09-12 16:30:52 -0600779}
780
781binder::Status VoldNativeService::addUserKeyAuth(int32_t userId, int32_t userSerial,
Paul Crowleyedf7a4e2018-09-18 15:14:18 -0700782 const std::string& token,
783 const std::string& secret) {
Nikita Ioffe5d85bf32019-10-23 17:19:13 +0100784 ENFORCE_SYSTEM_OR_ROOT;
Jeff Sharkey83b559c2017-09-12 16:30:52 -0600785 ACQUIRE_CRYPT_LOCK;
786
Eric Biggersa701c452018-10-23 13:06:55 -0700787 return translateBool(fscrypt_add_user_key_auth(userId, userSerial, token, secret));
Jeff Sharkey83b559c2017-09-12 16:30:52 -0600788}
789
Barani Muthukumaran9ad51ad2019-10-31 22:59:34 -0700790binder::Status VoldNativeService::clearUserKeyAuth(int32_t userId, int32_t userSerial,
791 const std::string& token,
792 const std::string& secret) {
793 ENFORCE_SYSTEM_OR_ROOT;
794 ACQUIRE_CRYPT_LOCK;
795
796 return translateBool(fscrypt_clear_user_key_auth(userId, userSerial, token, secret));
797}
798
Jeff Sharkey83b559c2017-09-12 16:30:52 -0600799binder::Status VoldNativeService::fixateNewestUserKeyAuth(int32_t userId) {
Nikita Ioffe5d85bf32019-10-23 17:19:13 +0100800 ENFORCE_SYSTEM_OR_ROOT;
Jeff Sharkey83b559c2017-09-12 16:30:52 -0600801 ACQUIRE_CRYPT_LOCK;
802
Eric Biggersa701c452018-10-23 13:06:55 -0700803 return translateBool(fscrypt_fixate_newest_user_key_auth(userId));
Jeff Sharkey83b559c2017-09-12 16:30:52 -0600804}
805
806binder::Status VoldNativeService::unlockUserKey(int32_t userId, int32_t userSerial,
Paul Crowleyedf7a4e2018-09-18 15:14:18 -0700807 const std::string& token,
808 const std::string& secret) {
Nikita Ioffe5d85bf32019-10-23 17:19:13 +0100809 ENFORCE_SYSTEM_OR_ROOT;
Jeff Sharkey83b559c2017-09-12 16:30:52 -0600810 ACQUIRE_CRYPT_LOCK;
811
Eric Biggersa701c452018-10-23 13:06:55 -0700812 return translateBool(fscrypt_unlock_user_key(userId, userSerial, token, secret));
Jeff Sharkey83b559c2017-09-12 16:30:52 -0600813}
814
815binder::Status VoldNativeService::lockUserKey(int32_t userId) {
Nikita Ioffe5d85bf32019-10-23 17:19:13 +0100816 ENFORCE_SYSTEM_OR_ROOT;
Jeff Sharkey83b559c2017-09-12 16:30:52 -0600817 ACQUIRE_CRYPT_LOCK;
818
Eric Biggersa701c452018-10-23 13:06:55 -0700819 return translateBool(fscrypt_lock_user_key(userId));
Jeff Sharkey83b559c2017-09-12 16:30:52 -0600820}
821
822binder::Status VoldNativeService::prepareUserStorage(const std::unique_ptr<std::string>& uuid,
Paul Crowleyedf7a4e2018-09-18 15:14:18 -0700823 int32_t userId, int32_t userSerial,
824 int32_t flags) {
Nikita Ioffe5d85bf32019-10-23 17:19:13 +0100825 ENFORCE_SYSTEM_OR_ROOT;
Paul Crowley3b71fc52017-10-09 10:55:21 -0700826 std::string empty_string = "";
827 auto uuid_ = uuid ? *uuid : empty_string;
Paul Crowley06f762d2017-10-16 10:59:51 -0700828 CHECK_ARGUMENT_HEX(uuid_);
829
830 ACQUIRE_CRYPT_LOCK;
Eric Biggersa701c452018-10-23 13:06:55 -0700831 return translateBool(fscrypt_prepare_user_storage(uuid_, userId, userSerial, flags));
Jeff Sharkey83b559c2017-09-12 16:30:52 -0600832}
833
834binder::Status VoldNativeService::destroyUserStorage(const std::unique_ptr<std::string>& uuid,
Paul Crowleyedf7a4e2018-09-18 15:14:18 -0700835 int32_t userId, int32_t flags) {
Nikita Ioffe5d85bf32019-10-23 17:19:13 +0100836 ENFORCE_SYSTEM_OR_ROOT;
Paul Crowley3b71fc52017-10-09 10:55:21 -0700837 std::string empty_string = "";
838 auto uuid_ = uuid ? *uuid : empty_string;
Paul Crowley06f762d2017-10-16 10:59:51 -0700839 CHECK_ARGUMENT_HEX(uuid_);
840
841 ACQUIRE_CRYPT_LOCK;
Eric Biggersa701c452018-10-23 13:06:55 -0700842 return translateBool(fscrypt_destroy_user_storage(uuid_, userId, flags));
Jeff Sharkey83b559c2017-09-12 16:30:52 -0600843}
844
Sudheer Shankafa6a1742018-10-04 16:26:22 -0700845binder::Status VoldNativeService::prepareSandboxForApp(const std::string& packageName,
846 int32_t appId, const std::string& sandboxId,
847 int32_t userId) {
Sudheer Shanka5fceb482019-04-29 10:46:35 -0700848 return ok();
Sudheer Shankafa6a1742018-10-04 16:26:22 -0700849}
850
851binder::Status VoldNativeService::destroySandboxForApp(const std::string& packageName,
Sudheer Shanka69bc40f2018-10-25 11:06:55 -0700852 const std::string& sandboxId,
Sudheer Shankafa6a1742018-10-04 16:26:22 -0700853 int32_t userId) {
Sudheer Shanka5fceb482019-04-29 10:46:35 -0700854 return ok();
Sudheer Shankac7562092018-08-24 10:20:56 -0700855}
856
Daniel Rosenberge334aab2018-10-10 18:52:04 -0700857binder::Status VoldNativeService::startCheckpoint(int32_t retry) {
Nikita Ioffe5d85bf32019-10-23 17:19:13 +0100858 ENFORCE_SYSTEM_OR_ROOT;
Daniel Rosenberg4f684712018-08-28 01:58:49 -0700859 ACQUIRE_LOCK;
860
Daniel Rosenberge334aab2018-10-10 18:52:04 -0700861 return cp_startCheckpoint(retry);
Daniel Rosenberg4f684712018-08-28 01:58:49 -0700862}
863
Daniel Rosenbergd3992492018-10-02 17:40:44 -0700864binder::Status VoldNativeService::needsRollback(bool* _aidl_return) {
Nikita Ioffe5d85bf32019-10-23 17:19:13 +0100865 ENFORCE_SYSTEM_OR_ROOT;
Daniel Rosenbergd3992492018-10-02 17:40:44 -0700866 ACQUIRE_LOCK;
867
868 *_aidl_return = cp_needsRollback();
869 return ok();
870}
871
Daniel Rosenberg4f684712018-08-28 01:58:49 -0700872binder::Status VoldNativeService::needsCheckpoint(bool* _aidl_return) {
Nikita Ioffe5d85bf32019-10-23 17:19:13 +0100873 ENFORCE_SYSTEM_OR_ROOT;
Daniel Rosenberg4f684712018-08-28 01:58:49 -0700874 ACQUIRE_LOCK;
875
876 *_aidl_return = cp_needsCheckpoint();
877 return ok();
878}
879
Daniel Rosenberge334aab2018-10-10 18:52:04 -0700880binder::Status VoldNativeService::commitChanges() {
Nikita Ioffe5d85bf32019-10-23 17:19:13 +0100881 ENFORCE_SYSTEM_OR_ROOT;
Daniel Rosenberg4f684712018-08-28 01:58:49 -0700882 ACQUIRE_LOCK;
883
Daniel Rosenberge334aab2018-10-10 18:52:04 -0700884 return cp_commitChanges();
Daniel Rosenberg4f684712018-08-28 01:58:49 -0700885}
886
Daniel Rosenberge334aab2018-10-10 18:52:04 -0700887binder::Status VoldNativeService::prepareCheckpoint() {
Nikita Ioffe5d85bf32019-10-23 17:19:13 +0100888 ENFORCE_SYSTEM_OR_ROOT;
Daniel Rosenberg4f684712018-08-28 01:58:49 -0700889 ACQUIRE_LOCK;
890
Daniel Rosenberge334aab2018-10-10 18:52:04 -0700891 return cp_prepareCheckpoint();
Daniel Rosenberg4f684712018-08-28 01:58:49 -0700892}
893
Daniel Rosenberge334aab2018-10-10 18:52:04 -0700894binder::Status VoldNativeService::restoreCheckpoint(const std::string& mountPoint) {
Nikita Ioffe5d85bf32019-10-23 17:19:13 +0100895 ENFORCE_SYSTEM_OR_ROOT;
Paul Lawrence1abb2fe2018-09-21 10:49:57 -0700896 CHECK_ARGUMENT_PATH(mountPoint);
897 ACQUIRE_LOCK;
898
Daniel Rosenberge334aab2018-10-10 18:52:04 -0700899 return cp_restoreCheckpoint(mountPoint);
Paul Lawrence1abb2fe2018-09-21 10:49:57 -0700900}
901
Daniel Rosenbergdda59812019-03-06 17:45:17 -0800902binder::Status VoldNativeService::restoreCheckpointPart(const std::string& mountPoint, int count) {
Nikita Ioffe5d85bf32019-10-23 17:19:13 +0100903 ENFORCE_SYSTEM_OR_ROOT;
Daniel Rosenbergdda59812019-03-06 17:45:17 -0800904 CHECK_ARGUMENT_PATH(mountPoint);
905 ACQUIRE_LOCK;
906
907 return cp_restoreCheckpoint(mountPoint, count);
908}
909
Daniel Rosenberge334aab2018-10-10 18:52:04 -0700910binder::Status VoldNativeService::markBootAttempt() {
Nikita Ioffe5d85bf32019-10-23 17:19:13 +0100911 ENFORCE_SYSTEM_OR_ROOT;
Daniel Rosenberg4f684712018-08-28 01:58:49 -0700912 ACQUIRE_LOCK;
913
Daniel Rosenberge334aab2018-10-10 18:52:04 -0700914 return cp_markBootAttempt();
Daniel Rosenberg4f684712018-08-28 01:58:49 -0700915}
916
Daniel Rosenberga59e4392019-03-20 17:02:47 -0700917binder::Status VoldNativeService::abortChanges(const std::string& message, bool retry) {
Nikita Ioffe5d85bf32019-10-23 17:19:13 +0100918 ENFORCE_SYSTEM_OR_ROOT;
Daniel Rosenberg4f684712018-08-28 01:58:49 -0700919 ACQUIRE_LOCK;
920
Daniel Rosenberga59e4392019-03-20 17:02:47 -0700921 cp_abortChanges(message, retry);
922 return ok();
Daniel Rosenberg4f684712018-08-28 01:58:49 -0700923}
924
Daniel Rosenberg9b667fb2019-01-22 17:27:25 -0800925binder::Status VoldNativeService::supportsCheckpoint(bool* _aidl_return) {
Nikita Ioffe5d85bf32019-10-23 17:19:13 +0100926 ENFORCE_SYSTEM_OR_ROOT;
Daniel Rosenberg9b667fb2019-01-22 17:27:25 -0800927 ACQUIRE_LOCK;
928
929 return cp_supportsCheckpoint(*_aidl_return);
930}
931
Paul Lawrencec5c79c52019-03-18 13:36:40 -0700932binder::Status VoldNativeService::supportsBlockCheckpoint(bool* _aidl_return) {
Nikita Ioffe5d85bf32019-10-23 17:19:13 +0100933 ENFORCE_SYSTEM_OR_ROOT;
Paul Lawrencec5c79c52019-03-18 13:36:40 -0700934 ACQUIRE_LOCK;
935
936 return cp_supportsBlockCheckpoint(*_aidl_return);
937}
938
939binder::Status VoldNativeService::supportsFileCheckpoint(bool* _aidl_return) {
Nikita Ioffe5d85bf32019-10-23 17:19:13 +0100940 ENFORCE_SYSTEM_OR_ROOT;
Paul Lawrencec5c79c52019-03-18 13:36:40 -0700941 ACQUIRE_LOCK;
942
943 return cp_supportsFileCheckpoint(*_aidl_return);
944}
945
Nikita Ioffea5798fc2019-10-11 16:38:21 +0100946binder::Status VoldNativeService::resetCheckpoint() {
Nikita Ioffe5d85bf32019-10-23 17:19:13 +0100947 ENFORCE_SYSTEM_OR_ROOT;
Nikita Ioffea5798fc2019-10-11 16:38:21 +0100948 ACQUIRE_LOCK;
949
950 cp_resetCheckpoint();
951 return ok();
952}
953
Yurii Zubrytskyi3497cb52020-01-10 11:54:06 -0800954binder::Status VoldNativeService::incFsEnabled(bool* _aidl_return) {
955 *_aidl_return = IncFs_IsEnabled();
Songchun Fanab55cec2019-12-02 10:50:12 -0800956 return ok();
957}
958
959binder::Status VoldNativeService::mountIncFs(
Yurii Zubrytskyi3497cb52020-01-10 11:54:06 -0800960 const std::string& backingPath, const std::string& targetDir, int32_t flags,
Songchun Fanab55cec2019-12-02 10:50:12 -0800961 ::android::os::incremental::IncrementalFileSystemControlParcel* _aidl_return) {
Yurii Zubrytskyi3497cb52020-01-10 11:54:06 -0800962 auto result = IncFs_Mount(backingPath.c_str(), targetDir.c_str(),
963 {.flags = IncFsMountFlags(flags),
964 .defaultReadTimeoutMs = INCFS_DEFAULT_READ_TIMEOUT_MS,
965 .readLogBufferPages = 4});
966 if (result.cmd < 0) {
967 return translate(result.cmd);
Songchun Fanab55cec2019-12-02 10:50:12 -0800968 }
Songchun Fanab55cec2019-12-02 10:50:12 -0800969 using unique_fd = ::android::base::unique_fd;
Yurii Zubrytskyi3497cb52020-01-10 11:54:06 -0800970 _aidl_return->cmd.reset(unique_fd(result.cmd));
971 _aidl_return->pendingReads.reset(unique_fd(result.pendingReads));
972 if (result.logs >= 0) {
973 _aidl_return->log.reset(unique_fd(result.logs));
Songchun Fanab55cec2019-12-02 10:50:12 -0800974 }
975 return ok();
976}
977
978binder::Status VoldNativeService::unmountIncFs(const std::string& dir) {
979 return translate(IncFs_Unmount(dir.c_str()));
980}
981
982binder::Status VoldNativeService::bindMount(const std::string& sourceDir,
983 const std::string& targetDir) {
984 return translate(IncFs_BindMount(sourceDir.c_str(), targetDir.c_str()));
985}
986
Jeff Sharkey068c6be2017-09-06 13:47:40 -0600987} // namespace vold
988} // namespace android