blob: 08b4661995415c240d53b77870ba2ea57a3e7061 [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 Coenen8a68a072020-02-12 15:29:02 +0100461binder::Status VoldNativeService::setupAppDir(const std::string& path, int32_t appUid) {
Nikita Ioffe5d85bf32019-10-23 17:19:13 +0100462 ENFORCE_SYSTEM_OR_ROOT;
Jeff Sharkeyec4fda22017-09-12 13:19:24 -0600463 CHECK_ARGUMENT_PATH(path);
Jeff Sharkey9462bdd2017-09-07 15:27:28 -0600464 ACQUIRE_LOCK;
465
Martijn Coenen8a68a072020-02-12 15:29:02 +0100466 return translate(VolumeManager::Instance()->setupAppDir(path, appUid));
Jeff Sharkey9462bdd2017-09-07 15:27:28 -0600467}
468
Martijn Coenen816f4d92020-02-18 15:06:37 +0100469binder::Status VoldNativeService::fixupAppDir(const std::string& path, int32_t appUid) {
470 ENFORCE_SYSTEM_OR_ROOT;
471 CHECK_ARGUMENT_PATH(path);
472 ACQUIRE_LOCK;
473
474 return translate(VolumeManager::Instance()->fixupAppDir(path, appUid));
475}
476
Jeff Sharkey11c2d382017-09-11 10:32:01 -0600477binder::Status VoldNativeService::createObb(const std::string& sourcePath,
Paul Crowleyedf7a4e2018-09-18 15:14:18 -0700478 const std::string& sourceKey, int32_t ownerGid,
479 std::string* _aidl_return) {
Nikita Ioffe5d85bf32019-10-23 17:19:13 +0100480 ENFORCE_SYSTEM_OR_ROOT;
Jeff Sharkeyec4fda22017-09-12 13:19:24 -0600481 CHECK_ARGUMENT_PATH(sourcePath);
482 CHECK_ARGUMENT_HEX(sourceKey);
Jeff Sharkey11c2d382017-09-11 10:32:01 -0600483 ACQUIRE_LOCK;
484
485 return translate(
Paul Crowleyedf7a4e2018-09-18 15:14:18 -0700486 VolumeManager::Instance()->createObb(sourcePath, sourceKey, ownerGid, _aidl_return));
Jeff Sharkey11c2d382017-09-11 10:32:01 -0600487}
488
489binder::Status VoldNativeService::destroyObb(const std::string& volId) {
Nikita Ioffe5d85bf32019-10-23 17:19:13 +0100490 ENFORCE_SYSTEM_OR_ROOT;
Jeff Sharkeyec4fda22017-09-12 13:19:24 -0600491 CHECK_ARGUMENT_ID(volId);
Jeff Sharkey11c2d382017-09-11 10:32:01 -0600492 ACQUIRE_LOCK;
493
494 return translate(VolumeManager::Instance()->destroyObb(volId));
495}
496
Risan82e90de2020-02-04 16:07:21 +0900497binder::Status VoldNativeService::createStubVolume(const std::string& sourcePath,
498 const std::string& mountPath,
499 const std::string& fsType,
500 const std::string& fsUuid,
501 const std::string& fsLabel, int32_t flags,
502 std::string* _aidl_return) {
Nikita Ioffe5d85bf32019-10-23 17:19:13 +0100503 ENFORCE_SYSTEM_OR_ROOT;
Risan8c9f3322018-10-29 08:52:56 +0900504 CHECK_ARGUMENT_PATH(sourcePath);
505 CHECK_ARGUMENT_PATH(mountPath);
506 CHECK_ARGUMENT_HEX(fsUuid);
507 // Label limitation seems to be different between fs (including allowed characters), so checking
508 // is quite meaningless.
509 ACQUIRE_LOCK;
510
Risan82e90de2020-02-04 16:07:21 +0900511 return translate(VolumeManager::Instance()->createStubVolume(
512 sourcePath, mountPath, fsType, fsUuid, fsLabel, flags, _aidl_return));
Risan8c9f3322018-10-29 08:52:56 +0900513}
514
515binder::Status VoldNativeService::destroyStubVolume(const std::string& volId) {
Nikita Ioffe5d85bf32019-10-23 17:19:13 +0100516 ENFORCE_SYSTEM_OR_ROOT;
Risan8c9f3322018-10-29 08:52:56 +0900517 CHECK_ARGUMENT_ID(volId);
518 ACQUIRE_LOCK;
519
520 return translate(VolumeManager::Instance()->destroyStubVolume(volId));
521}
522
Paul Crowleyedf7a4e2018-09-18 15:14:18 -0700523binder::Status VoldNativeService::fstrim(
524 int32_t fstrimFlags, const android::sp<android::os::IVoldTaskListener>& listener) {
Nikita Ioffe5d85bf32019-10-23 17:19:13 +0100525 ENFORCE_SYSTEM_OR_ROOT;
Jeff Sharkey11c2d382017-09-11 10:32:01 -0600526 ACQUIRE_LOCK;
527
Paul Crowleyedf7a4e2018-09-18 15:14:18 -0700528 std::thread([=]() { android::vold::Trim(listener); }).detach();
Jeff Sharkey11c2d382017-09-11 10:32:01 -0600529 return ok();
530}
531
Jin Qiana370c142017-10-17 15:41:45 -0700532binder::Status VoldNativeService::runIdleMaint(
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::RunIdleMaint(listener); }).detach();
Jin Qiana370c142017-10-17 15:41:45 -0700538 return ok();
539}
540
541binder::Status VoldNativeService::abortIdleMaint(
Paul Crowleyedf7a4e2018-09-18 15:14:18 -0700542 const android::sp<android::os::IVoldTaskListener>& listener) {
Nikita Ioffe5d85bf32019-10-23 17:19:13 +0100543 ENFORCE_SYSTEM_OR_ROOT;
Jin Qiana370c142017-10-17 15:41:45 -0700544 ACQUIRE_LOCK;
545
Paul Crowleyedf7a4e2018-09-18 15:14:18 -0700546 std::thread([=]() { android::vold::AbortIdleMaint(listener); }).detach();
Jin Qiana370c142017-10-17 15:41:45 -0700547 return ok();
548}
549
Risan8f6198d2018-10-26 20:56:45 -0600550binder::Status VoldNativeService::mountAppFuse(int32_t uid, int32_t mountId,
Paul Crowleyedf7a4e2018-09-18 15:14:18 -0700551 android::base::unique_fd* _aidl_return) {
Nikita Ioffe5d85bf32019-10-23 17:19:13 +0100552 ENFORCE_SYSTEM_OR_ROOT;
Jeff Sharkey11c2d382017-09-11 10:32:01 -0600553 ACQUIRE_LOCK;
554
Risan8f6198d2018-10-26 20:56:45 -0600555 return translate(VolumeManager::Instance()->mountAppFuse(uid, mountId, _aidl_return));
Jeff Sharkey11c2d382017-09-11 10:32:01 -0600556}
557
Risan8f6198d2018-10-26 20:56:45 -0600558binder::Status VoldNativeService::unmountAppFuse(int32_t uid, int32_t mountId) {
Nikita Ioffe5d85bf32019-10-23 17:19:13 +0100559 ENFORCE_SYSTEM_OR_ROOT;
Jeff Sharkey11c2d382017-09-11 10:32:01 -0600560 ACQUIRE_LOCK;
561
Risan8f6198d2018-10-26 20:56:45 -0600562 return translate(VolumeManager::Instance()->unmountAppFuse(uid, mountId));
563}
564
565binder::Status VoldNativeService::openAppFuseFile(int32_t uid, int32_t mountId, int32_t fileId,
566 int32_t flags,
567 android::base::unique_fd* _aidl_return) {
Nikita Ioffe5d85bf32019-10-23 17:19:13 +0100568 ENFORCE_SYSTEM_OR_ROOT;
Risan8f6198d2018-10-26 20:56:45 -0600569 ACQUIRE_LOCK;
570
571 int fd = VolumeManager::Instance()->openAppFuseFile(uid, mountId, fileId, flags);
572 if (fd == -1) {
573 return error("Failed to open AppFuse file for uid: " + std::to_string(uid) +
574 " mountId: " + std::to_string(mountId) + " fileId: " + std::to_string(fileId) +
575 " flags: " + std::to_string(flags));
576 }
577
578 *_aidl_return = android::base::unique_fd(fd);
579 return ok();
Jeff Sharkey11c2d382017-09-11 10:32:01 -0600580}
581
Jeff Sharkey83b559c2017-09-12 16:30:52 -0600582binder::Status VoldNativeService::fdeCheckPassword(const std::string& password) {
Nikita Ioffe5d85bf32019-10-23 17:19:13 +0100583 ENFORCE_SYSTEM_OR_ROOT;
Jeff Sharkey83b559c2017-09-12 16:30:52 -0600584 ACQUIRE_CRYPT_LOCK;
585
586 return translate(cryptfs_check_passwd(password.c_str()));
587}
588
589binder::Status VoldNativeService::fdeRestart() {
Nikita Ioffe5d85bf32019-10-23 17:19:13 +0100590 ENFORCE_SYSTEM_OR_ROOT;
Jeff Sharkey83b559c2017-09-12 16:30:52 -0600591 ACQUIRE_CRYPT_LOCK;
592
593 // Spawn as thread so init can issue commands back to vold without
594 // causing deadlock, usually as a result of prep_data_fs.
595 std::thread(&cryptfs_restart).detach();
596 return ok();
597}
598
599binder::Status VoldNativeService::fdeComplete(int32_t* _aidl_return) {
Nikita Ioffe5d85bf32019-10-23 17:19:13 +0100600 ENFORCE_SYSTEM_OR_ROOT;
Jeff Sharkey83b559c2017-09-12 16:30:52 -0600601 ACQUIRE_CRYPT_LOCK;
602
603 *_aidl_return = cryptfs_crypto_complete();
604 return ok();
605}
606
607static int fdeEnableInternal(int32_t passwordType, const std::string& password,
Paul Crowleyedf7a4e2018-09-18 15:14:18 -0700608 int32_t encryptionFlags) {
Jeff Sharkey83b559c2017-09-12 16:30:52 -0600609 bool noUi = (encryptionFlags & VoldNativeService::ENCRYPTION_FLAG_NO_UI) != 0;
610
Jeff Sharkey83b559c2017-09-12 16:30:52 -0600611 for (int tries = 0; tries < 2; ++tries) {
612 int rc;
613 if (passwordType == VoldNativeService::PASSWORD_TYPE_DEFAULT) {
Paul Lawrence7ee87cf2017-12-22 10:12:06 -0800614 rc = cryptfs_enable_default(noUi);
Jeff Sharkey83b559c2017-09-12 16:30:52 -0600615 } else {
Paul Lawrence7ee87cf2017-12-22 10:12:06 -0800616 rc = cryptfs_enable(passwordType, password.c_str(), noUi);
Jeff Sharkey83b559c2017-09-12 16:30:52 -0600617 }
618
619 if (rc == 0) {
620 return 0;
621 } else if (tries == 0) {
Jeff Sharkey3472e522017-10-06 18:02:53 -0600622 KillProcessesWithOpenFiles(DATA_MNT_POINT, SIGKILL);
Jeff Sharkey83b559c2017-09-12 16:30:52 -0600623 }
624 }
625
626 return -1;
627}
628
Paul Crowleyedf7a4e2018-09-18 15:14:18 -0700629binder::Status VoldNativeService::fdeEnable(int32_t passwordType, const std::string& password,
630 int32_t encryptionFlags) {
Nikita Ioffe5d85bf32019-10-23 17:19:13 +0100631 ENFORCE_SYSTEM_OR_ROOT;
Jeff Sharkey83b559c2017-09-12 16:30:52 -0600632 ACQUIRE_CRYPT_LOCK;
633
Paul Crowley0fd26262018-01-30 09:48:19 -0800634 LOG(DEBUG) << "fdeEnable(" << passwordType << ", *, " << encryptionFlags << ")";
Eric Biggersa701c452018-10-23 13:06:55 -0700635 if (fscrypt_is_native()) {
636 LOG(ERROR) << "fscrypt_is_native, fdeEnable invalid";
637 return error("fscrypt_is_native, fdeEnable invalid");
Jeff Sharkey83b559c2017-09-12 16:30:52 -0600638 }
Eric Biggersa701c452018-10-23 13:06:55 -0700639 LOG(DEBUG) << "!fscrypt_is_native, spawning fdeEnableInternal";
Jeff Sharkey83b559c2017-09-12 16:30:52 -0600640
641 // Spawn as thread so init can issue commands back to vold without
642 // causing deadlock, usually as a result of prep_data_fs.
643 std::thread(&fdeEnableInternal, passwordType, password, encryptionFlags).detach();
644 return ok();
645}
646
647binder::Status VoldNativeService::fdeChangePassword(int32_t passwordType,
Paul Crowleyedf7a4e2018-09-18 15:14:18 -0700648 const std::string& password) {
Nikita Ioffe5d85bf32019-10-23 17:19:13 +0100649 ENFORCE_SYSTEM_OR_ROOT;
Jeff Sharkey83b559c2017-09-12 16:30:52 -0600650 ACQUIRE_CRYPT_LOCK;
651
652 return translate(cryptfs_changepw(passwordType, password.c_str()));
653}
654
655binder::Status VoldNativeService::fdeVerifyPassword(const std::string& password) {
Nikita Ioffe5d85bf32019-10-23 17:19:13 +0100656 ENFORCE_SYSTEM_OR_ROOT;
Jeff Sharkey83b559c2017-09-12 16:30:52 -0600657 ACQUIRE_CRYPT_LOCK;
658
659 return translate(cryptfs_verify_passwd(password.c_str()));
660}
661
Paul Crowleyedf7a4e2018-09-18 15:14:18 -0700662binder::Status VoldNativeService::fdeGetField(const std::string& key, std::string* _aidl_return) {
Nikita Ioffe5d85bf32019-10-23 17:19:13 +0100663 ENFORCE_SYSTEM_OR_ROOT;
Jeff Sharkey83b559c2017-09-12 16:30:52 -0600664 ACQUIRE_CRYPT_LOCK;
665
666 char buf[PROPERTY_VALUE_MAX];
667 if (cryptfs_getfield(key.c_str(), buf, sizeof(buf)) != CRYPTO_GETFIELD_OK) {
668 return error(StringPrintf("Failed to read field %s", key.c_str()));
669 } else {
670 *_aidl_return = buf;
671 return ok();
672 }
673}
674
Paul Crowleyedf7a4e2018-09-18 15:14:18 -0700675binder::Status VoldNativeService::fdeSetField(const std::string& key, const std::string& value) {
Nikita Ioffe5d85bf32019-10-23 17:19:13 +0100676 ENFORCE_SYSTEM_OR_ROOT;
Jeff Sharkey83b559c2017-09-12 16:30:52 -0600677 ACQUIRE_CRYPT_LOCK;
678
679 return translate(cryptfs_setfield(key.c_str(), value.c_str()));
680}
681
682binder::Status VoldNativeService::fdeGetPasswordType(int32_t* _aidl_return) {
Nikita Ioffe5d85bf32019-10-23 17:19:13 +0100683 ENFORCE_SYSTEM_OR_ROOT;
Jeff Sharkey83b559c2017-09-12 16:30:52 -0600684 ACQUIRE_CRYPT_LOCK;
685
686 *_aidl_return = cryptfs_get_password_type();
687 return ok();
688}
689
690binder::Status VoldNativeService::fdeGetPassword(std::string* _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 const char* res = cryptfs_get_password();
695 if (res != nullptr) {
696 *_aidl_return = res;
697 }
698 return ok();
699}
700
701binder::Status VoldNativeService::fdeClearPassword() {
Nikita Ioffe5d85bf32019-10-23 17:19:13 +0100702 ENFORCE_SYSTEM_OR_ROOT;
Jeff Sharkey83b559c2017-09-12 16:30:52 -0600703 ACQUIRE_CRYPT_LOCK;
704
705 cryptfs_clear_password();
706 return ok();
707}
708
709binder::Status VoldNativeService::fbeEnable() {
Nikita Ioffe5d85bf32019-10-23 17:19:13 +0100710 ENFORCE_SYSTEM_OR_ROOT;
Jeff Sharkey83b559c2017-09-12 16:30:52 -0600711 ACQUIRE_CRYPT_LOCK;
712
Paul Crowleyc8a3ef32019-09-11 15:00:08 -0700713 return translateBool(fscrypt_initialize_systemwide_keys());
Jeff Sharkey83b559c2017-09-12 16:30:52 -0600714}
715
716binder::Status VoldNativeService::mountDefaultEncrypted() {
Nikita Ioffe5d85bf32019-10-23 17:19:13 +0100717 ENFORCE_SYSTEM_OR_ROOT;
Jeff Sharkey83b559c2017-09-12 16:30:52 -0600718 ACQUIRE_CRYPT_LOCK;
719
Eric Biggersa701c452018-10-23 13:06:55 -0700720 if (!fscrypt_is_native()) {
Jeff Sharkey83b559c2017-09-12 16:30:52 -0600721 // Spawn as thread so init can issue commands back to vold without
722 // causing deadlock, usually as a result of prep_data_fs.
723 std::thread(&cryptfs_mount_default_encrypted).detach();
Jeff Sharkey83b559c2017-09-12 16:30:52 -0600724 }
Paul Crowley0fd26262018-01-30 09:48:19 -0800725 return ok();
Jeff Sharkey83b559c2017-09-12 16:30:52 -0600726}
727
728binder::Status VoldNativeService::initUser0() {
Nikita Ioffe5d85bf32019-10-23 17:19:13 +0100729 ENFORCE_SYSTEM_OR_ROOT;
Jeff Sharkey83b559c2017-09-12 16:30:52 -0600730 ACQUIRE_CRYPT_LOCK;
731
Eric Biggersa701c452018-10-23 13:06:55 -0700732 return translateBool(fscrypt_init_user0());
Jeff Sharkey83b559c2017-09-12 16:30:52 -0600733}
734
735binder::Status VoldNativeService::isConvertibleToFbe(bool* _aidl_return) {
Nikita Ioffe5d85bf32019-10-23 17:19:13 +0100736 ENFORCE_SYSTEM_OR_ROOT;
Jeff Sharkey83b559c2017-09-12 16:30:52 -0600737 ACQUIRE_CRYPT_LOCK;
738
739 *_aidl_return = cryptfs_isConvertibleToFBE() != 0;
740 return ok();
741}
742
Paul Lawrence236e5e82019-06-25 14:44:33 -0700743binder::Status VoldNativeService::mountFstab(const std::string& blkDevice,
744 const std::string& mountPoint) {
Nikita Ioffe5d85bf32019-10-23 17:19:13 +0100745 ENFORCE_SYSTEM_OR_ROOT;
Paul Crowley0fd26262018-01-30 09:48:19 -0800746 ACQUIRE_LOCK;
747
Paul Lawrence236e5e82019-06-25 14:44:33 -0700748 return translateBool(fscrypt_mount_metadata_encrypted(blkDevice, mountPoint, false));
Paul Crowley0fd26262018-01-30 09:48:19 -0800749}
750
Paul Lawrence236e5e82019-06-25 14:44:33 -0700751binder::Status VoldNativeService::encryptFstab(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, true));
Paul Crowley0fd26262018-01-30 09:48:19 -0800757}
758
Paul Crowleyedf7a4e2018-09-18 15:14:18 -0700759binder::Status VoldNativeService::createUserKey(int32_t userId, int32_t userSerial, bool ephemeral) {
Nikita Ioffe5d85bf32019-10-23 17:19:13 +0100760 ENFORCE_SYSTEM_OR_ROOT;
Jeff Sharkey83b559c2017-09-12 16:30:52 -0600761 ACQUIRE_CRYPT_LOCK;
762
Eric Biggersa701c452018-10-23 13:06:55 -0700763 return translateBool(fscrypt_vold_create_user_key(userId, userSerial, ephemeral));
Jeff Sharkey83b559c2017-09-12 16:30:52 -0600764}
765
766binder::Status VoldNativeService::destroyUserKey(int32_t userId) {
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_destroy_user_key(userId));
Jeff Sharkey83b559c2017-09-12 16:30:52 -0600771}
772
773binder::Status VoldNativeService::addUserKeyAuth(int32_t userId, int32_t userSerial,
Paul Crowleyedf7a4e2018-09-18 15:14:18 -0700774 const std::string& token,
775 const std::string& secret) {
Nikita Ioffe5d85bf32019-10-23 17:19:13 +0100776 ENFORCE_SYSTEM_OR_ROOT;
Jeff Sharkey83b559c2017-09-12 16:30:52 -0600777 ACQUIRE_CRYPT_LOCK;
778
Eric Biggersa701c452018-10-23 13:06:55 -0700779 return translateBool(fscrypt_add_user_key_auth(userId, userSerial, token, secret));
Jeff Sharkey83b559c2017-09-12 16:30:52 -0600780}
781
Barani Muthukumaran9ad51ad2019-10-31 22:59:34 -0700782binder::Status VoldNativeService::clearUserKeyAuth(int32_t userId, int32_t userSerial,
783 const std::string& token,
784 const std::string& secret) {
785 ENFORCE_SYSTEM_OR_ROOT;
786 ACQUIRE_CRYPT_LOCK;
787
788 return translateBool(fscrypt_clear_user_key_auth(userId, userSerial, token, secret));
789}
790
Jeff Sharkey83b559c2017-09-12 16:30:52 -0600791binder::Status VoldNativeService::fixateNewestUserKeyAuth(int32_t userId) {
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_fixate_newest_user_key_auth(userId));
Jeff Sharkey83b559c2017-09-12 16:30:52 -0600796}
797
798binder::Status VoldNativeService::unlockUserKey(int32_t userId, int32_t userSerial,
Paul Crowleyedf7a4e2018-09-18 15:14:18 -0700799 const std::string& token,
800 const std::string& secret) {
Nikita Ioffe5d85bf32019-10-23 17:19:13 +0100801 ENFORCE_SYSTEM_OR_ROOT;
Jeff Sharkey83b559c2017-09-12 16:30:52 -0600802 ACQUIRE_CRYPT_LOCK;
803
Eric Biggersa701c452018-10-23 13:06:55 -0700804 return translateBool(fscrypt_unlock_user_key(userId, userSerial, token, secret));
Jeff Sharkey83b559c2017-09-12 16:30:52 -0600805}
806
807binder::Status VoldNativeService::lockUserKey(int32_t userId) {
Nikita Ioffe5d85bf32019-10-23 17:19:13 +0100808 ENFORCE_SYSTEM_OR_ROOT;
Jeff Sharkey83b559c2017-09-12 16:30:52 -0600809 ACQUIRE_CRYPT_LOCK;
810
Eric Biggersa701c452018-10-23 13:06:55 -0700811 return translateBool(fscrypt_lock_user_key(userId));
Jeff Sharkey83b559c2017-09-12 16:30:52 -0600812}
813
814binder::Status VoldNativeService::prepareUserStorage(const std::unique_ptr<std::string>& uuid,
Paul Crowleyedf7a4e2018-09-18 15:14:18 -0700815 int32_t userId, int32_t userSerial,
816 int32_t flags) {
Nikita Ioffe5d85bf32019-10-23 17:19:13 +0100817 ENFORCE_SYSTEM_OR_ROOT;
Paul Crowley3b71fc52017-10-09 10:55:21 -0700818 std::string empty_string = "";
819 auto uuid_ = uuid ? *uuid : empty_string;
Paul Crowley06f762d2017-10-16 10:59:51 -0700820 CHECK_ARGUMENT_HEX(uuid_);
821
822 ACQUIRE_CRYPT_LOCK;
Eric Biggersa701c452018-10-23 13:06:55 -0700823 return translateBool(fscrypt_prepare_user_storage(uuid_, userId, userSerial, flags));
Jeff Sharkey83b559c2017-09-12 16:30:52 -0600824}
825
826binder::Status VoldNativeService::destroyUserStorage(const std::unique_ptr<std::string>& uuid,
Paul Crowleyedf7a4e2018-09-18 15:14:18 -0700827 int32_t userId, int32_t flags) {
Nikita Ioffe5d85bf32019-10-23 17:19:13 +0100828 ENFORCE_SYSTEM_OR_ROOT;
Paul Crowley3b71fc52017-10-09 10:55:21 -0700829 std::string empty_string = "";
830 auto uuid_ = uuid ? *uuid : empty_string;
Paul Crowley06f762d2017-10-16 10:59:51 -0700831 CHECK_ARGUMENT_HEX(uuid_);
832
833 ACQUIRE_CRYPT_LOCK;
Eric Biggersa701c452018-10-23 13:06:55 -0700834 return translateBool(fscrypt_destroy_user_storage(uuid_, userId, flags));
Jeff Sharkey83b559c2017-09-12 16:30:52 -0600835}
836
Sudheer Shankafa6a1742018-10-04 16:26:22 -0700837binder::Status VoldNativeService::prepareSandboxForApp(const std::string& packageName,
838 int32_t appId, const std::string& sandboxId,
839 int32_t userId) {
Sudheer Shanka5fceb482019-04-29 10:46:35 -0700840 return ok();
Sudheer Shankafa6a1742018-10-04 16:26:22 -0700841}
842
843binder::Status VoldNativeService::destroySandboxForApp(const std::string& packageName,
Sudheer Shanka69bc40f2018-10-25 11:06:55 -0700844 const std::string& sandboxId,
Sudheer Shankafa6a1742018-10-04 16:26:22 -0700845 int32_t userId) {
Sudheer Shanka5fceb482019-04-29 10:46:35 -0700846 return ok();
Sudheer Shankac7562092018-08-24 10:20:56 -0700847}
848
Daniel Rosenberge334aab2018-10-10 18:52:04 -0700849binder::Status VoldNativeService::startCheckpoint(int32_t retry) {
Nikita Ioffe5d85bf32019-10-23 17:19:13 +0100850 ENFORCE_SYSTEM_OR_ROOT;
Daniel Rosenberg4f684712018-08-28 01:58:49 -0700851 ACQUIRE_LOCK;
852
Daniel Rosenberge334aab2018-10-10 18:52:04 -0700853 return cp_startCheckpoint(retry);
Daniel Rosenberg4f684712018-08-28 01:58:49 -0700854}
855
Daniel Rosenbergd3992492018-10-02 17:40:44 -0700856binder::Status VoldNativeService::needsRollback(bool* _aidl_return) {
Nikita Ioffe5d85bf32019-10-23 17:19:13 +0100857 ENFORCE_SYSTEM_OR_ROOT;
Daniel Rosenbergd3992492018-10-02 17:40:44 -0700858 ACQUIRE_LOCK;
859
860 *_aidl_return = cp_needsRollback();
861 return ok();
862}
863
Daniel Rosenberg4f684712018-08-28 01:58:49 -0700864binder::Status VoldNativeService::needsCheckpoint(bool* _aidl_return) {
Nikita Ioffe5d85bf32019-10-23 17:19:13 +0100865 ENFORCE_SYSTEM_OR_ROOT;
Daniel Rosenberg4f684712018-08-28 01:58:49 -0700866 ACQUIRE_LOCK;
867
868 *_aidl_return = cp_needsCheckpoint();
869 return ok();
870}
871
Daniel Rosenberge334aab2018-10-10 18:52:04 -0700872binder::Status VoldNativeService::commitChanges() {
Nikita Ioffe5d85bf32019-10-23 17:19:13 +0100873 ENFORCE_SYSTEM_OR_ROOT;
Daniel Rosenberg4f684712018-08-28 01:58:49 -0700874 ACQUIRE_LOCK;
875
Daniel Rosenberge334aab2018-10-10 18:52:04 -0700876 return cp_commitChanges();
Daniel Rosenberg4f684712018-08-28 01:58:49 -0700877}
878
Daniel Rosenberge334aab2018-10-10 18:52:04 -0700879binder::Status VoldNativeService::prepareCheckpoint() {
Nikita Ioffe5d85bf32019-10-23 17:19:13 +0100880 ENFORCE_SYSTEM_OR_ROOT;
Daniel Rosenberg4f684712018-08-28 01:58:49 -0700881 ACQUIRE_LOCK;
882
Daniel Rosenberge334aab2018-10-10 18:52:04 -0700883 return cp_prepareCheckpoint();
Daniel Rosenberg4f684712018-08-28 01:58:49 -0700884}
885
Daniel Rosenberge334aab2018-10-10 18:52:04 -0700886binder::Status VoldNativeService::restoreCheckpoint(const std::string& mountPoint) {
Nikita Ioffe5d85bf32019-10-23 17:19:13 +0100887 ENFORCE_SYSTEM_OR_ROOT;
Paul Lawrence1abb2fe2018-09-21 10:49:57 -0700888 CHECK_ARGUMENT_PATH(mountPoint);
889 ACQUIRE_LOCK;
890
Daniel Rosenberge334aab2018-10-10 18:52:04 -0700891 return cp_restoreCheckpoint(mountPoint);
Paul Lawrence1abb2fe2018-09-21 10:49:57 -0700892}
893
Daniel Rosenbergdda59812019-03-06 17:45:17 -0800894binder::Status VoldNativeService::restoreCheckpointPart(const std::string& mountPoint, int count) {
Nikita Ioffe5d85bf32019-10-23 17:19:13 +0100895 ENFORCE_SYSTEM_OR_ROOT;
Daniel Rosenbergdda59812019-03-06 17:45:17 -0800896 CHECK_ARGUMENT_PATH(mountPoint);
897 ACQUIRE_LOCK;
898
899 return cp_restoreCheckpoint(mountPoint, count);
900}
901
Daniel Rosenberge334aab2018-10-10 18:52:04 -0700902binder::Status VoldNativeService::markBootAttempt() {
Nikita Ioffe5d85bf32019-10-23 17:19:13 +0100903 ENFORCE_SYSTEM_OR_ROOT;
Daniel Rosenberg4f684712018-08-28 01:58:49 -0700904 ACQUIRE_LOCK;
905
Daniel Rosenberge334aab2018-10-10 18:52:04 -0700906 return cp_markBootAttempt();
Daniel Rosenberg4f684712018-08-28 01:58:49 -0700907}
908
Daniel Rosenberga59e4392019-03-20 17:02:47 -0700909binder::Status VoldNativeService::abortChanges(const std::string& message, bool retry) {
Nikita Ioffe5d85bf32019-10-23 17:19:13 +0100910 ENFORCE_SYSTEM_OR_ROOT;
Daniel Rosenberg4f684712018-08-28 01:58:49 -0700911 ACQUIRE_LOCK;
912
Daniel Rosenberga59e4392019-03-20 17:02:47 -0700913 cp_abortChanges(message, retry);
914 return ok();
Daniel Rosenberg4f684712018-08-28 01:58:49 -0700915}
916
Daniel Rosenberg9b667fb2019-01-22 17:27:25 -0800917binder::Status VoldNativeService::supportsCheckpoint(bool* _aidl_return) {
Nikita Ioffe5d85bf32019-10-23 17:19:13 +0100918 ENFORCE_SYSTEM_OR_ROOT;
Daniel Rosenberg9b667fb2019-01-22 17:27:25 -0800919 ACQUIRE_LOCK;
920
921 return cp_supportsCheckpoint(*_aidl_return);
922}
923
Paul Lawrencec5c79c52019-03-18 13:36:40 -0700924binder::Status VoldNativeService::supportsBlockCheckpoint(bool* _aidl_return) {
Nikita Ioffe5d85bf32019-10-23 17:19:13 +0100925 ENFORCE_SYSTEM_OR_ROOT;
Paul Lawrencec5c79c52019-03-18 13:36:40 -0700926 ACQUIRE_LOCK;
927
928 return cp_supportsBlockCheckpoint(*_aidl_return);
929}
930
931binder::Status VoldNativeService::supportsFileCheckpoint(bool* _aidl_return) {
Nikita Ioffe5d85bf32019-10-23 17:19:13 +0100932 ENFORCE_SYSTEM_OR_ROOT;
Paul Lawrencec5c79c52019-03-18 13:36:40 -0700933 ACQUIRE_LOCK;
934
935 return cp_supportsFileCheckpoint(*_aidl_return);
936}
937
Nikita Ioffea5798fc2019-10-11 16:38:21 +0100938binder::Status VoldNativeService::resetCheckpoint() {
Nikita Ioffe5d85bf32019-10-23 17:19:13 +0100939 ENFORCE_SYSTEM_OR_ROOT;
Nikita Ioffea5798fc2019-10-11 16:38:21 +0100940 ACQUIRE_LOCK;
941
942 cp_resetCheckpoint();
943 return ok();
944}
945
Yurii Zubrytskyi3497cb52020-01-10 11:54:06 -0800946binder::Status VoldNativeService::incFsEnabled(bool* _aidl_return) {
947 *_aidl_return = IncFs_IsEnabled();
Songchun Fanab55cec2019-12-02 10:50:12 -0800948 return ok();
949}
950
951binder::Status VoldNativeService::mountIncFs(
Yurii Zubrytskyi3497cb52020-01-10 11:54:06 -0800952 const std::string& backingPath, const std::string& targetDir, int32_t flags,
Songchun Fanab55cec2019-12-02 10:50:12 -0800953 ::android::os::incremental::IncrementalFileSystemControlParcel* _aidl_return) {
Yurii Zubrytskyi3497cb52020-01-10 11:54:06 -0800954 auto result = IncFs_Mount(backingPath.c_str(), targetDir.c_str(),
955 {.flags = IncFsMountFlags(flags),
956 .defaultReadTimeoutMs = INCFS_DEFAULT_READ_TIMEOUT_MS,
957 .readLogBufferPages = 4});
958 if (result.cmd < 0) {
959 return translate(result.cmd);
Songchun Fanab55cec2019-12-02 10:50:12 -0800960 }
Songchun Fanab55cec2019-12-02 10:50:12 -0800961 using unique_fd = ::android::base::unique_fd;
Yurii Zubrytskyi3497cb52020-01-10 11:54:06 -0800962 _aidl_return->cmd.reset(unique_fd(result.cmd));
963 _aidl_return->pendingReads.reset(unique_fd(result.pendingReads));
964 if (result.logs >= 0) {
965 _aidl_return->log.reset(unique_fd(result.logs));
Songchun Fanab55cec2019-12-02 10:50:12 -0800966 }
967 return ok();
968}
969
970binder::Status VoldNativeService::unmountIncFs(const std::string& dir) {
971 return translate(IncFs_Unmount(dir.c_str()));
972}
973
974binder::Status VoldNativeService::bindMount(const std::string& sourceDir,
975 const std::string& targetDir) {
976 return translate(IncFs_BindMount(sourceDir.c_str(), targetDir.c_str()));
977}
978
Jeff Sharkey068c6be2017-09-06 13:47:40 -0600979} // namespace vold
980} // namespace android