blob: e345d2ff5c8fb96cce215c6c9379ea4bcdeb23d0 [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"
Daniel Rosenberg4f684712018-08-28 01:58:49 -070022#include "Checkpoint.h"
Eric Biggersa701c452018-10-23 13:06:55 -070023#include "FsCrypt.h"
Paul Crowley73be12d2020-02-03 12:22:03 -080024#include "IdleMaint.h"
Jeff Sharkey83b559c2017-09-12 16:30:52 -060025#include "MetadataCrypt.h"
Paul Crowley73be12d2020-02-03 12:22:03 -080026#include "MoveStorage.h"
27#include "Process.h"
Yurii Zubrytskyi9e7482e2020-03-24 16:23:51 -070028#include "VoldNativeServiceValidation.h"
Paul Crowley73be12d2020-02-03 12:22:03 -080029#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;
Yurii Zubrytskyi9e7482e2020-03-24 16:23:51 -070048using namespace std::literals;
Jeff Sharkey068c6be2017-09-06 13:47:40 -060049
50namespace android {
51namespace vold {
52
53namespace {
54
55constexpr const char* kDump = "android.permission.DUMP";
56
Jeff Sharkey9462bdd2017-09-07 15:27:28 -060057static binder::Status error(const std::string& msg) {
58 PLOG(ERROR) << msg;
59 return binder::Status::fromServiceSpecificError(errno, String8(msg.c_str()));
60}
61
Jeff Sharkey83b559c2017-09-12 16:30:52 -060062static binder::Status translate(int status) {
Jeff Sharkey9462bdd2017-09-07 15:27:28 -060063 if (status == 0) {
64 return binder::Status::ok();
65 } else {
Jeff Sharkey11c2d382017-09-11 10:32:01 -060066 return binder::Status::fromServiceSpecificError(status);
Jeff Sharkey9462bdd2017-09-07 15:27:28 -060067 }
68}
69
Jeff Sharkey83b559c2017-09-12 16:30:52 -060070static binder::Status translateBool(bool status) {
71 if (status) {
72 return binder::Status::ok();
73 } else {
74 return binder::Status::fromServiceSpecificError(status);
75 }
76}
77
Nikita Ioffee1fd4892019-10-23 17:19:13 +010078#define ENFORCE_SYSTEM_OR_ROOT \
79 { \
Yurii Zubrytskyi9e7482e2020-03-24 16:23:51 -070080 binder::Status status = CheckUidOrRoot(AID_SYSTEM); \
Nikita Ioffee1fd4892019-10-23 17:19:13 +010081 if (!status.isOk()) { \
82 return status; \
83 } \
Paul Crowleyedf7a4e2018-09-18 15:14:18 -070084 }
Jeff Sharkey068c6be2017-09-06 13:47:40 -060085
Paul Crowleyedf7a4e2018-09-18 15:14:18 -070086#define CHECK_ARGUMENT_ID(id) \
87 { \
Yurii Zubrytskyi9e7482e2020-03-24 16:23:51 -070088 binder::Status status = CheckArgumentId((id)); \
Paul Crowleyedf7a4e2018-09-18 15:14:18 -070089 if (!status.isOk()) { \
90 return status; \
91 } \
92 }
Jeff Sharkeyec4fda22017-09-12 13:19:24 -060093
Paul Crowleyedf7a4e2018-09-18 15:14:18 -070094#define CHECK_ARGUMENT_PATH(path) \
95 { \
Yurii Zubrytskyi9e7482e2020-03-24 16:23:51 -070096 binder::Status status = CheckArgumentPath((path)); \
Paul Crowleyedf7a4e2018-09-18 15:14:18 -070097 if (!status.isOk()) { \
98 return status; \
99 } \
100 }
Jeff Sharkeyec4fda22017-09-12 13:19:24 -0600101
Paul Crowleyedf7a4e2018-09-18 15:14:18 -0700102#define CHECK_ARGUMENT_HEX(hex) \
103 { \
Yurii Zubrytskyi9e7482e2020-03-24 16:23:51 -0700104 binder::Status status = CheckArgumentHex((hex)); \
Paul Crowleyedf7a4e2018-09-18 15:14:18 -0700105 if (!status.isOk()) { \
106 return status; \
107 } \
108 }
Jeff Sharkeyec4fda22017-09-12 13:19:24 -0600109
Paul Crowleyedf7a4e2018-09-18 15:14:18 -0700110#define ACQUIRE_LOCK \
Jeff Sharkey67b8c492017-09-21 17:08:43 -0600111 std::lock_guard<std::mutex> lock(VolumeManager::Instance()->getLock()); \
112 ATRACE_CALL();
Jeff Sharkey83b559c2017-09-12 16:30:52 -0600113
Paul Crowleyedf7a4e2018-09-18 15:14:18 -0700114#define ACQUIRE_CRYPT_LOCK \
Jeff Sharkey67b8c492017-09-21 17:08:43 -0600115 std::lock_guard<std::mutex> lock(VolumeManager::Instance()->getCryptLock()); \
116 ATRACE_CALL();
Jeff Sharkey9462bdd2017-09-07 15:27:28 -0600117
Jeff Sharkey068c6be2017-09-06 13:47:40 -0600118} // namespace
119
120status_t VoldNativeService::start() {
121 IPCThreadState::self()->disableBackgroundScheduling(true);
122 status_t ret = BinderService<VoldNativeService>::publish();
123 if (ret != android::OK) {
124 return ret;
125 }
126 sp<ProcessState> ps(ProcessState::self());
127 ps->startThreadPool();
128 ps->giveThreadPoolName();
129 return android::OK;
130}
131
Paul Crowleyedf7a4e2018-09-18 15:14:18 -0700132status_t VoldNativeService::dump(int fd, const Vector<String16>& /* args */) {
Jeff Sharkey068c6be2017-09-06 13:47:40 -0600133 auto out = std::fstream(StringPrintf("/proc/self/fd/%d", fd));
Yurii Zubrytskyi9e7482e2020-03-24 16:23:51 -0700134 const binder::Status dump_permission = CheckPermission(kDump);
Jeff Sharkey068c6be2017-09-06 13:47:40 -0600135 if (!dump_permission.isOk()) {
136 out << dump_permission.toString8() << endl;
137 return PERMISSION_DENIED;
138 }
139
Jeff Sharkey9462bdd2017-09-07 15:27:28 -0600140 ACQUIRE_LOCK;
Jeff Sharkey068c6be2017-09-06 13:47:40 -0600141 out << "vold is happy!" << endl;
Jeff Sharkey068c6be2017-09-06 13:47:40 -0600142 return NO_ERROR;
143}
144
Jeff Sharkey814e9d32017-09-13 11:49:44 -0600145binder::Status VoldNativeService::setListener(
Paul Crowleyedf7a4e2018-09-18 15:14:18 -0700146 const android::sp<android::os::IVoldListener>& listener) {
Nikita Ioffee1fd4892019-10-23 17:19:13 +0100147 ENFORCE_SYSTEM_OR_ROOT;
Jeff Sharkey814e9d32017-09-13 11:49:44 -0600148 ACQUIRE_LOCK;
149
150 VolumeManager::Instance()->setListener(listener);
Yurii Zubrytskyi9e7482e2020-03-24 16:23:51 -0700151 return Ok();
Jeff Sharkey814e9d32017-09-13 11:49:44 -0600152}
153
Jeff Sharkeycbe69fc2017-09-15 16:50:28 -0600154binder::Status VoldNativeService::monitor() {
Nikita Ioffee1fd4892019-10-23 17:19:13 +0100155 ENFORCE_SYSTEM_OR_ROOT;
Jeff Sharkeycbe69fc2017-09-15 16:50:28 -0600156
157 // Simply acquire/release each lock for watchdog
Paul Crowleyedf7a4e2018-09-18 15:14:18 -0700158 { ACQUIRE_LOCK; }
159 { ACQUIRE_CRYPT_LOCK; }
Jeff Sharkeycbe69fc2017-09-15 16:50:28 -0600160
Yurii Zubrytskyi9e7482e2020-03-24 16:23:51 -0700161 return Ok();
Jeff Sharkeycbe69fc2017-09-15 16:50:28 -0600162}
163
Jeff Sharkey068c6be2017-09-06 13:47:40 -0600164binder::Status VoldNativeService::reset() {
Nikita Ioffee1fd4892019-10-23 17:19:13 +0100165 ENFORCE_SYSTEM_OR_ROOT;
Jeff Sharkey9462bdd2017-09-07 15:27:28 -0600166 ACQUIRE_LOCK;
Jeff Sharkey068c6be2017-09-06 13:47:40 -0600167
Jeff Sharkey9462bdd2017-09-07 15:27:28 -0600168 return translate(VolumeManager::Instance()->reset());
169}
170
171binder::Status VoldNativeService::shutdown() {
Nikita Ioffee1fd4892019-10-23 17:19:13 +0100172 ENFORCE_SYSTEM_OR_ROOT;
Jeff Sharkey9462bdd2017-09-07 15:27:28 -0600173 ACQUIRE_LOCK;
174
175 return translate(VolumeManager::Instance()->shutdown());
176}
177
Jeff Sharkey9462bdd2017-09-07 15:27:28 -0600178binder::Status VoldNativeService::onUserAdded(int32_t userId, int32_t userSerial) {
Nikita Ioffee1fd4892019-10-23 17:19:13 +0100179 ENFORCE_SYSTEM_OR_ROOT;
Jeff Sharkey9462bdd2017-09-07 15:27:28 -0600180 ACQUIRE_LOCK;
181
182 return translate(VolumeManager::Instance()->onUserAdded(userId, userSerial));
183}
184
185binder::Status VoldNativeService::onUserRemoved(int32_t userId) {
Nikita Ioffee1fd4892019-10-23 17:19:13 +0100186 ENFORCE_SYSTEM_OR_ROOT;
Jeff Sharkey9462bdd2017-09-07 15:27:28 -0600187 ACQUIRE_LOCK;
188
189 return translate(VolumeManager::Instance()->onUserRemoved(userId));
190}
191
Sudheer Shanka5fceb482019-04-29 10:46:35 -0700192binder::Status VoldNativeService::onUserStarted(int32_t userId) {
Nikita Ioffee1fd4892019-10-23 17:19:13 +0100193 ENFORCE_SYSTEM_OR_ROOT;
Jeff Sharkey9462bdd2017-09-07 15:27:28 -0600194 ACQUIRE_LOCK;
195
Sudheer Shanka5fceb482019-04-29 10:46:35 -0700196 return translate(VolumeManager::Instance()->onUserStarted(userId));
Jeff Sharkey9462bdd2017-09-07 15:27:28 -0600197}
198
199binder::Status VoldNativeService::onUserStopped(int32_t userId) {
Nikita Ioffee1fd4892019-10-23 17:19:13 +0100200 ENFORCE_SYSTEM_OR_ROOT;
Jeff Sharkey9462bdd2017-09-07 15:27:28 -0600201 ACQUIRE_LOCK;
202
203 return translate(VolumeManager::Instance()->onUserStopped(userId));
204}
205
Sudheer Shankad484aa92018-07-31 10:07:34 -0700206binder::Status VoldNativeService::addAppIds(const std::vector<std::string>& packageNames,
Paul Crowleyedf7a4e2018-09-18 15:14:18 -0700207 const std::vector<int32_t>& appIds) {
Yurii Zubrytskyi9e7482e2020-03-24 16:23:51 -0700208 return Ok();
Sudheer Shankad484aa92018-07-31 10:07:34 -0700209}
210
Sudheer Shankad484aa92018-07-31 10:07:34 -0700211binder::Status VoldNativeService::addSandboxIds(const std::vector<int32_t>& appIds,
Paul Crowleyedf7a4e2018-09-18 15:14:18 -0700212 const std::vector<std::string>& sandboxIds) {
Yurii Zubrytskyi9e7482e2020-03-24 16:23:51 -0700213 return Ok();
Sudheer Shankad484aa92018-07-31 10:07:34 -0700214}
215
Jeff Sharkey401b2602017-12-14 22:15:20 -0700216binder::Status VoldNativeService::onSecureKeyguardStateChanged(bool isShowing) {
Nikita Ioffee1fd4892019-10-23 17:19:13 +0100217 ENFORCE_SYSTEM_OR_ROOT;
Jeff Sharkey401b2602017-12-14 22:15:20 -0700218 ACQUIRE_LOCK;
219
220 return translate(VolumeManager::Instance()->onSecureKeyguardStateChanged(isShowing));
221}
222
Jeff Sharkey11c2d382017-09-11 10:32:01 -0600223binder::Status VoldNativeService::partition(const std::string& diskId, int32_t partitionType,
Paul Crowleyedf7a4e2018-09-18 15:14:18 -0700224 int32_t ratio) {
Nikita Ioffee1fd4892019-10-23 17:19:13 +0100225 ENFORCE_SYSTEM_OR_ROOT;
Jeff Sharkeyec4fda22017-09-12 13:19:24 -0600226 CHECK_ARGUMENT_ID(diskId);
Jeff Sharkey9462bdd2017-09-07 15:27:28 -0600227 ACQUIRE_LOCK;
228
229 auto disk = VolumeManager::Instance()->findDisk(diskId);
230 if (disk == nullptr) {
231 return error("Failed to find disk " + diskId);
232 }
233 switch (partitionType) {
Paul Crowleyedf7a4e2018-09-18 15:14:18 -0700234 case PARTITION_TYPE_PUBLIC:
235 return translate(disk->partitionPublic());
236 case PARTITION_TYPE_PRIVATE:
237 return translate(disk->partitionPrivate());
238 case PARTITION_TYPE_MIXED:
239 return translate(disk->partitionMixed(ratio));
240 default:
241 return error("Unknown type " + std::to_string(partitionType));
Jeff Sharkey9462bdd2017-09-07 15:27:28 -0600242 }
243}
244
Jeff Sharkey3ce18252017-10-24 11:08:45 -0600245binder::Status VoldNativeService::forgetPartition(const std::string& partGuid,
Paul Crowleyedf7a4e2018-09-18 15:14:18 -0700246 const std::string& fsUuid) {
Nikita Ioffee1fd4892019-10-23 17:19:13 +0100247 ENFORCE_SYSTEM_OR_ROOT;
Jeff Sharkeyec4fda22017-09-12 13:19:24 -0600248 CHECK_ARGUMENT_HEX(partGuid);
Jeff Sharkey3ce18252017-10-24 11:08:45 -0600249 CHECK_ARGUMENT_HEX(fsUuid);
Jeff Sharkey9462bdd2017-09-07 15:27:28 -0600250 ACQUIRE_LOCK;
251
Jeff Sharkey3ce18252017-10-24 11:08:45 -0600252 return translate(VolumeManager::Instance()->forgetPartition(partGuid, fsUuid));
Jeff Sharkey9462bdd2017-09-07 15:27:28 -0600253}
254
Jeff Sharkey11c2d382017-09-11 10:32:01 -0600255binder::Status VoldNativeService::mount(const std::string& volId, int32_t mountFlags,
Paul Crowleyedf7a4e2018-09-18 15:14:18 -0700256 int32_t mountUserId) {
Nikita Ioffee1fd4892019-10-23 17:19:13 +0100257 ENFORCE_SYSTEM_OR_ROOT;
Jeff Sharkeyec4fda22017-09-12 13:19:24 -0600258 CHECK_ARGUMENT_ID(volId);
Jeff Sharkey9462bdd2017-09-07 15:27:28 -0600259 ACQUIRE_LOCK;
260
261 auto vol = VolumeManager::Instance()->findVolume(volId);
262 if (vol == nullptr) {
263 return error("Failed to find volume " + volId);
264 }
265
266 vol->setMountFlags(mountFlags);
267 vol->setMountUserId(mountUserId);
268
269 int res = vol->mount();
Sudheer Shanka40ab6742018-09-18 13:07:45 -0700270 if (res != OK) {
271 return translate(res);
Jeff Sharkey9462bdd2017-09-07 15:27:28 -0600272 }
Sudheer Shanka40ab6742018-09-18 13:07:45 -0700273 if ((mountFlags & MOUNT_FLAG_PRIMARY) != 0) {
274 res = VolumeManager::Instance()->setPrimary(vol);
275 if (res != OK) {
276 return translate(res);
277 }
278 }
279 return translate(OK);
Jeff Sharkey9462bdd2017-09-07 15:27:28 -0600280}
281
282binder::Status VoldNativeService::unmount(const std::string& volId) {
Nikita Ioffee1fd4892019-10-23 17:19:13 +0100283 ENFORCE_SYSTEM_OR_ROOT;
Jeff Sharkeyec4fda22017-09-12 13:19:24 -0600284 CHECK_ARGUMENT_ID(volId);
Jeff Sharkey9462bdd2017-09-07 15:27:28 -0600285 ACQUIRE_LOCK;
286
287 auto vol = VolumeManager::Instance()->findVolume(volId);
288 if (vol == nullptr) {
289 return error("Failed to find volume " + volId);
290 }
291 return translate(vol->unmount());
292}
293
294binder::Status VoldNativeService::format(const std::string& volId, const std::string& fsType) {
Nikita Ioffee1fd4892019-10-23 17:19:13 +0100295 ENFORCE_SYSTEM_OR_ROOT;
Jeff Sharkeyec4fda22017-09-12 13:19:24 -0600296 CHECK_ARGUMENT_ID(volId);
Jeff Sharkey9462bdd2017-09-07 15:27:28 -0600297 ACQUIRE_LOCK;
298
299 auto vol = VolumeManager::Instance()->findVolume(volId);
300 if (vol == nullptr) {
301 return error("Failed to find volume " + volId);
302 }
303 return translate(vol->format(fsType));
304}
305
Jeff Sharkey2048a282017-06-15 09:59:43 -0600306static binder::Status pathForVolId(const std::string& volId, std::string* path) {
Jeff Sharkey52f7a912017-09-15 12:57:44 -0600307 if (volId == "private" || volId == "null") {
Jeff Sharkey2048a282017-06-15 09:59:43 -0600308 *path = "/data";
Jeff Sharkey52f7a912017-09-15 12:57:44 -0600309 } else {
310 auto vol = VolumeManager::Instance()->findVolume(volId);
311 if (vol == nullptr) {
312 return error("Failed to find volume " + volId);
313 }
314 if (vol->getType() != VolumeBase::Type::kPrivate) {
315 return error("Volume " + volId + " not private");
316 }
317 if (vol->getState() != VolumeBase::State::kMounted) {
318 return error("Volume " + volId + " not mounted");
319 }
Jeff Sharkey2048a282017-06-15 09:59:43 -0600320 *path = vol->getPath();
321 if (path->empty()) {
322 return error("Volume " + volId + " missing path");
323 }
Jeff Sharkey52f7a912017-09-15 12:57:44 -0600324 }
Yurii Zubrytskyi9e7482e2020-03-24 16:23:51 -0700325 return Ok();
Jeff Sharkey2048a282017-06-15 09:59:43 -0600326}
Jeff Sharkey52f7a912017-09-15 12:57:44 -0600327
Jeff Sharkey2048a282017-06-15 09:59:43 -0600328binder::Status VoldNativeService::benchmark(
329 const std::string& volId, const android::sp<android::os::IVoldTaskListener>& listener) {
Nikita Ioffee1fd4892019-10-23 17:19:13 +0100330 ENFORCE_SYSTEM_OR_ROOT;
Jeff Sharkey2048a282017-06-15 09:59:43 -0600331 CHECK_ARGUMENT_ID(volId);
332 ACQUIRE_LOCK;
333
334 std::string path;
335 auto status = pathForVolId(volId, &path);
336 if (!status.isOk()) return status;
Jeff Sharkey52f7a912017-09-15 12:57:44 -0600337
Paul Crowleyedf7a4e2018-09-18 15:14:18 -0700338 std::thread([=]() { android::vold::Benchmark(path, listener); }).detach();
Yurii Zubrytskyi9e7482e2020-03-24 16:23:51 -0700339 return Ok();
Jeff Sharkey068c6be2017-09-06 13:47:40 -0600340}
341
Paul Crowleyedf7a4e2018-09-18 15:14:18 -0700342binder::Status VoldNativeService::moveStorage(
343 const std::string& fromVolId, const std::string& toVolId,
344 const android::sp<android::os::IVoldTaskListener>& listener) {
Nikita Ioffee1fd4892019-10-23 17:19:13 +0100345 ENFORCE_SYSTEM_OR_ROOT;
Jeff Sharkeyec4fda22017-09-12 13:19:24 -0600346 CHECK_ARGUMENT_ID(fromVolId);
347 CHECK_ARGUMENT_ID(toVolId);
Jeff Sharkey9462bdd2017-09-07 15:27:28 -0600348 ACQUIRE_LOCK;
349
350 auto fromVol = VolumeManager::Instance()->findVolume(fromVolId);
351 auto toVol = VolumeManager::Instance()->findVolume(toVolId);
352 if (fromVol == nullptr) {
353 return error("Failed to find volume " + fromVolId);
354 } else if (toVol == nullptr) {
355 return error("Failed to find volume " + toVolId);
356 }
Jeff Sharkey01a0e7f2017-10-17 16:06:32 -0600357
Paul Crowleyedf7a4e2018-09-18 15:14:18 -0700358 std::thread([=]() { android::vold::MoveStorage(fromVol, toVol, listener); }).detach();
Yurii Zubrytskyi9e7482e2020-03-24 16:23:51 -0700359 return Ok();
Jeff Sharkey9462bdd2017-09-07 15:27:28 -0600360}
361
362binder::Status VoldNativeService::remountUid(int32_t uid, int32_t remountMode) {
Nikita Ioffee1fd4892019-10-23 17:19:13 +0100363 ENFORCE_SYSTEM_OR_ROOT;
Jeff Sharkey9462bdd2017-09-07 15:27:28 -0600364 ACQUIRE_LOCK;
365
Sudheer Shanka817b9112018-12-13 17:40:28 -0800366 return translate(VolumeManager::Instance()->remountUid(uid, remountMode));
Jeff Sharkey9462bdd2017-09-07 15:27:28 -0600367}
368
369binder::Status VoldNativeService::mkdirs(const std::string& path) {
Nikita Ioffee1fd4892019-10-23 17:19:13 +0100370 ENFORCE_SYSTEM_OR_ROOT;
Jeff Sharkeyec4fda22017-09-12 13:19:24 -0600371 CHECK_ARGUMENT_PATH(path);
Jeff Sharkey9462bdd2017-09-07 15:27:28 -0600372 ACQUIRE_LOCK;
373
Jeff Sharkey3472e522017-10-06 18:02:53 -0600374 return translate(VolumeManager::Instance()->mkdirs(path));
Jeff Sharkey9462bdd2017-09-07 15:27:28 -0600375}
376
Jeff Sharkey11c2d382017-09-11 10:32:01 -0600377binder::Status VoldNativeService::createObb(const std::string& sourcePath,
Paul Crowleyedf7a4e2018-09-18 15:14:18 -0700378 const std::string& sourceKey, int32_t ownerGid,
379 std::string* _aidl_return) {
Nikita Ioffee1fd4892019-10-23 17:19:13 +0100380 ENFORCE_SYSTEM_OR_ROOT;
Jeff Sharkeyec4fda22017-09-12 13:19:24 -0600381 CHECK_ARGUMENT_PATH(sourcePath);
382 CHECK_ARGUMENT_HEX(sourceKey);
Jeff Sharkey11c2d382017-09-11 10:32:01 -0600383 ACQUIRE_LOCK;
384
385 return translate(
Paul Crowleyedf7a4e2018-09-18 15:14:18 -0700386 VolumeManager::Instance()->createObb(sourcePath, sourceKey, ownerGid, _aidl_return));
Jeff Sharkey11c2d382017-09-11 10:32:01 -0600387}
388
389binder::Status VoldNativeService::destroyObb(const std::string& volId) {
Nikita Ioffee1fd4892019-10-23 17:19:13 +0100390 ENFORCE_SYSTEM_OR_ROOT;
Jeff Sharkeyec4fda22017-09-12 13:19:24 -0600391 CHECK_ARGUMENT_ID(volId);
Jeff Sharkey11c2d382017-09-11 10:32:01 -0600392 ACQUIRE_LOCK;
393
394 return translate(VolumeManager::Instance()->destroyObb(volId));
395}
396
Risan8c9f3322018-10-29 08:52:56 +0900397binder::Status VoldNativeService::createStubVolume(
398 const std::string& sourcePath, const std::string& mountPath, const std::string& fsType,
399 const std::string& fsUuid, const std::string& fsLabel, std::string* _aidl_return) {
Nikita Ioffee1fd4892019-10-23 17:19:13 +0100400 ENFORCE_SYSTEM_OR_ROOT;
Risan8c9f3322018-10-29 08:52:56 +0900401 CHECK_ARGUMENT_PATH(sourcePath);
402 CHECK_ARGUMENT_PATH(mountPath);
403 CHECK_ARGUMENT_HEX(fsUuid);
404 // Label limitation seems to be different between fs (including allowed characters), so checking
405 // is quite meaningless.
406 ACQUIRE_LOCK;
407
408 return translate(VolumeManager::Instance()->createStubVolume(sourcePath, mountPath, fsType,
409 fsUuid, fsLabel, _aidl_return));
410}
411
412binder::Status VoldNativeService::destroyStubVolume(const std::string& volId) {
Nikita Ioffee1fd4892019-10-23 17:19:13 +0100413 ENFORCE_SYSTEM_OR_ROOT;
Risan8c9f3322018-10-29 08:52:56 +0900414 CHECK_ARGUMENT_ID(volId);
415 ACQUIRE_LOCK;
416
417 return translate(VolumeManager::Instance()->destroyStubVolume(volId));
418}
419
Paul Crowleyedf7a4e2018-09-18 15:14:18 -0700420binder::Status VoldNativeService::fstrim(
421 int32_t fstrimFlags, const android::sp<android::os::IVoldTaskListener>& listener) {
Nikita Ioffee1fd4892019-10-23 17:19:13 +0100422 ENFORCE_SYSTEM_OR_ROOT;
Jeff Sharkey11c2d382017-09-11 10:32:01 -0600423 ACQUIRE_LOCK;
424
Paul Crowleyedf7a4e2018-09-18 15:14:18 -0700425 std::thread([=]() { android::vold::Trim(listener); }).detach();
Yurii Zubrytskyi9e7482e2020-03-24 16:23:51 -0700426 return Ok();
Jeff Sharkey11c2d382017-09-11 10:32:01 -0600427}
428
Jin Qiana370c142017-10-17 15:41:45 -0700429binder::Status VoldNativeService::runIdleMaint(
Paul Crowleyedf7a4e2018-09-18 15:14:18 -0700430 const android::sp<android::os::IVoldTaskListener>& listener) {
Nikita Ioffee1fd4892019-10-23 17:19:13 +0100431 ENFORCE_SYSTEM_OR_ROOT;
Jin Qiana370c142017-10-17 15:41:45 -0700432 ACQUIRE_LOCK;
433
Paul Crowleyedf7a4e2018-09-18 15:14:18 -0700434 std::thread([=]() { android::vold::RunIdleMaint(listener); }).detach();
Yurii Zubrytskyi9e7482e2020-03-24 16:23:51 -0700435 return Ok();
Jin Qiana370c142017-10-17 15:41:45 -0700436}
437
438binder::Status VoldNativeService::abortIdleMaint(
Paul Crowleyedf7a4e2018-09-18 15:14:18 -0700439 const android::sp<android::os::IVoldTaskListener>& listener) {
Nikita Ioffee1fd4892019-10-23 17:19:13 +0100440 ENFORCE_SYSTEM_OR_ROOT;
Jin Qiana370c142017-10-17 15:41:45 -0700441 ACQUIRE_LOCK;
442
Paul Crowleyedf7a4e2018-09-18 15:14:18 -0700443 std::thread([=]() { android::vold::AbortIdleMaint(listener); }).detach();
Yurii Zubrytskyi9e7482e2020-03-24 16:23:51 -0700444 return Ok();
Jin Qiana370c142017-10-17 15:41:45 -0700445}
446
Risan8f6198d2018-10-26 20:56:45 -0600447binder::Status VoldNativeService::mountAppFuse(int32_t uid, int32_t mountId,
Paul Crowleyedf7a4e2018-09-18 15:14:18 -0700448 android::base::unique_fd* _aidl_return) {
Nikita Ioffee1fd4892019-10-23 17:19:13 +0100449 ENFORCE_SYSTEM_OR_ROOT;
Jeff Sharkey11c2d382017-09-11 10:32:01 -0600450 ACQUIRE_LOCK;
451
Risan8f6198d2018-10-26 20:56:45 -0600452 return translate(VolumeManager::Instance()->mountAppFuse(uid, mountId, _aidl_return));
Jeff Sharkey11c2d382017-09-11 10:32:01 -0600453}
454
Risan8f6198d2018-10-26 20:56:45 -0600455binder::Status VoldNativeService::unmountAppFuse(int32_t uid, int32_t mountId) {
Nikita Ioffee1fd4892019-10-23 17:19:13 +0100456 ENFORCE_SYSTEM_OR_ROOT;
Jeff Sharkey11c2d382017-09-11 10:32:01 -0600457 ACQUIRE_LOCK;
458
Risan8f6198d2018-10-26 20:56:45 -0600459 return translate(VolumeManager::Instance()->unmountAppFuse(uid, mountId));
460}
461
462binder::Status VoldNativeService::openAppFuseFile(int32_t uid, int32_t mountId, int32_t fileId,
463 int32_t flags,
464 android::base::unique_fd* _aidl_return) {
Nikita Ioffee1fd4892019-10-23 17:19:13 +0100465 ENFORCE_SYSTEM_OR_ROOT;
Risan8f6198d2018-10-26 20:56:45 -0600466 ACQUIRE_LOCK;
467
468 int fd = VolumeManager::Instance()->openAppFuseFile(uid, mountId, fileId, flags);
469 if (fd == -1) {
470 return error("Failed to open AppFuse file for uid: " + std::to_string(uid) +
471 " mountId: " + std::to_string(mountId) + " fileId: " + std::to_string(fileId) +
472 " flags: " + std::to_string(flags));
473 }
474
475 *_aidl_return = android::base::unique_fd(fd);
Yurii Zubrytskyi9e7482e2020-03-24 16:23:51 -0700476 return Ok();
Jeff Sharkey11c2d382017-09-11 10:32:01 -0600477}
478
Jeff Sharkey83b559c2017-09-12 16:30:52 -0600479binder::Status VoldNativeService::fdeCheckPassword(const std::string& password) {
Nikita Ioffee1fd4892019-10-23 17:19:13 +0100480 ENFORCE_SYSTEM_OR_ROOT;
Jeff Sharkey83b559c2017-09-12 16:30:52 -0600481 ACQUIRE_CRYPT_LOCK;
482
483 return translate(cryptfs_check_passwd(password.c_str()));
484}
485
486binder::Status VoldNativeService::fdeRestart() {
Nikita Ioffee1fd4892019-10-23 17:19:13 +0100487 ENFORCE_SYSTEM_OR_ROOT;
Jeff Sharkey83b559c2017-09-12 16:30:52 -0600488 ACQUIRE_CRYPT_LOCK;
489
490 // Spawn as thread so init can issue commands back to vold without
491 // causing deadlock, usually as a result of prep_data_fs.
492 std::thread(&cryptfs_restart).detach();
Yurii Zubrytskyi9e7482e2020-03-24 16:23:51 -0700493 return Ok();
Jeff Sharkey83b559c2017-09-12 16:30:52 -0600494}
495
496binder::Status VoldNativeService::fdeComplete(int32_t* _aidl_return) {
Nikita Ioffee1fd4892019-10-23 17:19:13 +0100497 ENFORCE_SYSTEM_OR_ROOT;
Jeff Sharkey83b559c2017-09-12 16:30:52 -0600498 ACQUIRE_CRYPT_LOCK;
499
500 *_aidl_return = cryptfs_crypto_complete();
Yurii Zubrytskyi9e7482e2020-03-24 16:23:51 -0700501 return Ok();
Jeff Sharkey83b559c2017-09-12 16:30:52 -0600502}
503
504static int fdeEnableInternal(int32_t passwordType, const std::string& password,
Paul Crowleyedf7a4e2018-09-18 15:14:18 -0700505 int32_t encryptionFlags) {
Jeff Sharkey83b559c2017-09-12 16:30:52 -0600506 bool noUi = (encryptionFlags & VoldNativeService::ENCRYPTION_FLAG_NO_UI) != 0;
507
Jeff Sharkey83b559c2017-09-12 16:30:52 -0600508 for (int tries = 0; tries < 2; ++tries) {
509 int rc;
510 if (passwordType == VoldNativeService::PASSWORD_TYPE_DEFAULT) {
Paul Lawrence7ee87cf2017-12-22 10:12:06 -0800511 rc = cryptfs_enable_default(noUi);
Jeff Sharkey83b559c2017-09-12 16:30:52 -0600512 } else {
Paul Lawrence7ee87cf2017-12-22 10:12:06 -0800513 rc = cryptfs_enable(passwordType, password.c_str(), noUi);
Jeff Sharkey83b559c2017-09-12 16:30:52 -0600514 }
515
516 if (rc == 0) {
517 return 0;
518 } else if (tries == 0) {
Jeff Sharkey3472e522017-10-06 18:02:53 -0600519 KillProcessesWithOpenFiles(DATA_MNT_POINT, SIGKILL);
Jeff Sharkey83b559c2017-09-12 16:30:52 -0600520 }
521 }
522
523 return -1;
524}
525
Paul Crowleyedf7a4e2018-09-18 15:14:18 -0700526binder::Status VoldNativeService::fdeEnable(int32_t passwordType, const std::string& password,
527 int32_t encryptionFlags) {
Nikita Ioffee1fd4892019-10-23 17:19:13 +0100528 ENFORCE_SYSTEM_OR_ROOT;
Jeff Sharkey83b559c2017-09-12 16:30:52 -0600529 ACQUIRE_CRYPT_LOCK;
530
Paul Crowley0fd26262018-01-30 09:48:19 -0800531 LOG(DEBUG) << "fdeEnable(" << passwordType << ", *, " << encryptionFlags << ")";
Eric Biggersa701c452018-10-23 13:06:55 -0700532 if (fscrypt_is_native()) {
533 LOG(ERROR) << "fscrypt_is_native, fdeEnable invalid";
534 return error("fscrypt_is_native, fdeEnable invalid");
Jeff Sharkey83b559c2017-09-12 16:30:52 -0600535 }
Eric Biggersa701c452018-10-23 13:06:55 -0700536 LOG(DEBUG) << "!fscrypt_is_native, spawning fdeEnableInternal";
Jeff Sharkey83b559c2017-09-12 16:30:52 -0600537
538 // Spawn as thread so init can issue commands back to vold without
539 // causing deadlock, usually as a result of prep_data_fs.
540 std::thread(&fdeEnableInternal, passwordType, password, encryptionFlags).detach();
Yurii Zubrytskyi9e7482e2020-03-24 16:23:51 -0700541 return Ok();
Jeff Sharkey83b559c2017-09-12 16:30:52 -0600542}
543
544binder::Status VoldNativeService::fdeChangePassword(int32_t passwordType,
Paul Crowleyedf7a4e2018-09-18 15:14:18 -0700545 const std::string& password) {
Nikita Ioffee1fd4892019-10-23 17:19:13 +0100546 ENFORCE_SYSTEM_OR_ROOT;
Jeff Sharkey83b559c2017-09-12 16:30:52 -0600547 ACQUIRE_CRYPT_LOCK;
548
549 return translate(cryptfs_changepw(passwordType, password.c_str()));
550}
551
552binder::Status VoldNativeService::fdeVerifyPassword(const std::string& password) {
Nikita Ioffee1fd4892019-10-23 17:19:13 +0100553 ENFORCE_SYSTEM_OR_ROOT;
Jeff Sharkey83b559c2017-09-12 16:30:52 -0600554 ACQUIRE_CRYPT_LOCK;
555
556 return translate(cryptfs_verify_passwd(password.c_str()));
557}
558
Paul Crowleyedf7a4e2018-09-18 15:14:18 -0700559binder::Status VoldNativeService::fdeGetField(const std::string& key, std::string* _aidl_return) {
Nikita Ioffee1fd4892019-10-23 17:19:13 +0100560 ENFORCE_SYSTEM_OR_ROOT;
Jeff Sharkey83b559c2017-09-12 16:30:52 -0600561 ACQUIRE_CRYPT_LOCK;
562
563 char buf[PROPERTY_VALUE_MAX];
564 if (cryptfs_getfield(key.c_str(), buf, sizeof(buf)) != CRYPTO_GETFIELD_OK) {
565 return error(StringPrintf("Failed to read field %s", key.c_str()));
566 } else {
567 *_aidl_return = buf;
Yurii Zubrytskyi9e7482e2020-03-24 16:23:51 -0700568 return Ok();
Jeff Sharkey83b559c2017-09-12 16:30:52 -0600569 }
570}
571
Paul Crowleyedf7a4e2018-09-18 15:14:18 -0700572binder::Status VoldNativeService::fdeSetField(const std::string& key, const std::string& value) {
Nikita Ioffee1fd4892019-10-23 17:19:13 +0100573 ENFORCE_SYSTEM_OR_ROOT;
Jeff Sharkey83b559c2017-09-12 16:30:52 -0600574 ACQUIRE_CRYPT_LOCK;
575
576 return translate(cryptfs_setfield(key.c_str(), value.c_str()));
577}
578
579binder::Status VoldNativeService::fdeGetPasswordType(int32_t* _aidl_return) {
Nikita Ioffee1fd4892019-10-23 17:19:13 +0100580 ENFORCE_SYSTEM_OR_ROOT;
Jeff Sharkey83b559c2017-09-12 16:30:52 -0600581 ACQUIRE_CRYPT_LOCK;
582
583 *_aidl_return = cryptfs_get_password_type();
Yurii Zubrytskyi9e7482e2020-03-24 16:23:51 -0700584 return Ok();
Jeff Sharkey83b559c2017-09-12 16:30:52 -0600585}
586
587binder::Status VoldNativeService::fdeGetPassword(std::string* _aidl_return) {
Nikita Ioffee1fd4892019-10-23 17:19:13 +0100588 ENFORCE_SYSTEM_OR_ROOT;
Jeff Sharkey83b559c2017-09-12 16:30:52 -0600589 ACQUIRE_CRYPT_LOCK;
590
591 const char* res = cryptfs_get_password();
592 if (res != nullptr) {
593 *_aidl_return = res;
594 }
Yurii Zubrytskyi9e7482e2020-03-24 16:23:51 -0700595 return Ok();
Jeff Sharkey83b559c2017-09-12 16:30:52 -0600596}
597
598binder::Status VoldNativeService::fdeClearPassword() {
Nikita Ioffee1fd4892019-10-23 17:19:13 +0100599 ENFORCE_SYSTEM_OR_ROOT;
Jeff Sharkey83b559c2017-09-12 16:30:52 -0600600 ACQUIRE_CRYPT_LOCK;
601
602 cryptfs_clear_password();
Yurii Zubrytskyi9e7482e2020-03-24 16:23:51 -0700603 return Ok();
Jeff Sharkey83b559c2017-09-12 16:30:52 -0600604}
605
606binder::Status VoldNativeService::fbeEnable() {
Nikita Ioffee1fd4892019-10-23 17:19:13 +0100607 ENFORCE_SYSTEM_OR_ROOT;
Jeff Sharkey83b559c2017-09-12 16:30:52 -0600608 ACQUIRE_CRYPT_LOCK;
609
Paul Crowleyc8a3ef32019-09-11 15:00:08 -0700610 return translateBool(fscrypt_initialize_systemwide_keys());
Jeff Sharkey83b559c2017-09-12 16:30:52 -0600611}
612
613binder::Status VoldNativeService::mountDefaultEncrypted() {
Nikita Ioffee1fd4892019-10-23 17:19:13 +0100614 ENFORCE_SYSTEM_OR_ROOT;
Jeff Sharkey83b559c2017-09-12 16:30:52 -0600615 ACQUIRE_CRYPT_LOCK;
616
Eric Biggersa701c452018-10-23 13:06:55 -0700617 if (!fscrypt_is_native()) {
Jeff Sharkey83b559c2017-09-12 16:30:52 -0600618 // Spawn as thread so init can issue commands back to vold without
619 // causing deadlock, usually as a result of prep_data_fs.
620 std::thread(&cryptfs_mount_default_encrypted).detach();
Jeff Sharkey83b559c2017-09-12 16:30:52 -0600621 }
Yurii Zubrytskyi9e7482e2020-03-24 16:23:51 -0700622 return Ok();
Jeff Sharkey83b559c2017-09-12 16:30:52 -0600623}
624
625binder::Status VoldNativeService::initUser0() {
Nikita Ioffee1fd4892019-10-23 17:19:13 +0100626 ENFORCE_SYSTEM_OR_ROOT;
Jeff Sharkey83b559c2017-09-12 16:30:52 -0600627 ACQUIRE_CRYPT_LOCK;
628
Eric Biggersa701c452018-10-23 13:06:55 -0700629 return translateBool(fscrypt_init_user0());
Jeff Sharkey83b559c2017-09-12 16:30:52 -0600630}
631
632binder::Status VoldNativeService::isConvertibleToFbe(bool* _aidl_return) {
Nikita Ioffee1fd4892019-10-23 17:19:13 +0100633 ENFORCE_SYSTEM_OR_ROOT;
Jeff Sharkey83b559c2017-09-12 16:30:52 -0600634 ACQUIRE_CRYPT_LOCK;
635
636 *_aidl_return = cryptfs_isConvertibleToFBE() != 0;
Yurii Zubrytskyi9e7482e2020-03-24 16:23:51 -0700637 return Ok();
Jeff Sharkey83b559c2017-09-12 16:30:52 -0600638}
639
Paul Lawrence236e5e82019-06-25 14:44:33 -0700640binder::Status VoldNativeService::mountFstab(const std::string& blkDevice,
641 const std::string& mountPoint) {
Nikita Ioffee1fd4892019-10-23 17:19:13 +0100642 ENFORCE_SYSTEM_OR_ROOT;
Paul Crowley0fd26262018-01-30 09:48:19 -0800643 ACQUIRE_LOCK;
644
Paul Lawrence236e5e82019-06-25 14:44:33 -0700645 return translateBool(fscrypt_mount_metadata_encrypted(blkDevice, mountPoint, false));
Paul Crowley0fd26262018-01-30 09:48:19 -0800646}
647
Paul Lawrence236e5e82019-06-25 14:44:33 -0700648binder::Status VoldNativeService::encryptFstab(const std::string& blkDevice,
649 const std::string& mountPoint) {
Nikita Ioffee1fd4892019-10-23 17:19:13 +0100650 ENFORCE_SYSTEM_OR_ROOT;
Paul Crowley0fd26262018-01-30 09:48:19 -0800651 ACQUIRE_LOCK;
652
Paul Lawrence236e5e82019-06-25 14:44:33 -0700653 return translateBool(fscrypt_mount_metadata_encrypted(blkDevice, mountPoint, true));
Paul Crowley0fd26262018-01-30 09:48:19 -0800654}
655
Paul Crowleyedf7a4e2018-09-18 15:14:18 -0700656binder::Status VoldNativeService::createUserKey(int32_t userId, int32_t userSerial, bool ephemeral) {
Nikita Ioffee1fd4892019-10-23 17:19:13 +0100657 ENFORCE_SYSTEM_OR_ROOT;
Jeff Sharkey83b559c2017-09-12 16:30:52 -0600658 ACQUIRE_CRYPT_LOCK;
659
Eric Biggersa701c452018-10-23 13:06:55 -0700660 return translateBool(fscrypt_vold_create_user_key(userId, userSerial, ephemeral));
Jeff Sharkey83b559c2017-09-12 16:30:52 -0600661}
662
663binder::Status VoldNativeService::destroyUserKey(int32_t userId) {
Nikita Ioffee1fd4892019-10-23 17:19:13 +0100664 ENFORCE_SYSTEM_OR_ROOT;
Jeff Sharkey83b559c2017-09-12 16:30:52 -0600665 ACQUIRE_CRYPT_LOCK;
666
Eric Biggersa701c452018-10-23 13:06:55 -0700667 return translateBool(fscrypt_destroy_user_key(userId));
Jeff Sharkey83b559c2017-09-12 16:30:52 -0600668}
669
670binder::Status VoldNativeService::addUserKeyAuth(int32_t userId, int32_t userSerial,
Paul Crowleyedf7a4e2018-09-18 15:14:18 -0700671 const std::string& token,
672 const std::string& secret) {
Nikita Ioffee1fd4892019-10-23 17:19:13 +0100673 ENFORCE_SYSTEM_OR_ROOT;
Jeff Sharkey83b559c2017-09-12 16:30:52 -0600674 ACQUIRE_CRYPT_LOCK;
675
Eric Biggersa701c452018-10-23 13:06:55 -0700676 return translateBool(fscrypt_add_user_key_auth(userId, userSerial, token, secret));
Jeff Sharkey83b559c2017-09-12 16:30:52 -0600677}
678
Barani Muthukumaranb1927c22019-10-31 22:59:34 -0700679binder::Status VoldNativeService::clearUserKeyAuth(int32_t userId, int32_t userSerial,
680 const std::string& token,
681 const std::string& secret) {
682 ENFORCE_SYSTEM_OR_ROOT;
683 ACQUIRE_CRYPT_LOCK;
684
685 return translateBool(fscrypt_clear_user_key_auth(userId, userSerial, token, secret));
686}
687
Jeff Sharkey83b559c2017-09-12 16:30:52 -0600688binder::Status VoldNativeService::fixateNewestUserKeyAuth(int32_t userId) {
Nikita Ioffee1fd4892019-10-23 17:19:13 +0100689 ENFORCE_SYSTEM_OR_ROOT;
Jeff Sharkey83b559c2017-09-12 16:30:52 -0600690 ACQUIRE_CRYPT_LOCK;
691
Eric Biggersa701c452018-10-23 13:06:55 -0700692 return translateBool(fscrypt_fixate_newest_user_key_auth(userId));
Jeff Sharkey83b559c2017-09-12 16:30:52 -0600693}
694
695binder::Status VoldNativeService::unlockUserKey(int32_t userId, int32_t userSerial,
Paul Crowleyedf7a4e2018-09-18 15:14:18 -0700696 const std::string& token,
697 const std::string& secret) {
Nikita Ioffee1fd4892019-10-23 17:19:13 +0100698 ENFORCE_SYSTEM_OR_ROOT;
Jeff Sharkey83b559c2017-09-12 16:30:52 -0600699 ACQUIRE_CRYPT_LOCK;
700
Eric Biggersa701c452018-10-23 13:06:55 -0700701 return translateBool(fscrypt_unlock_user_key(userId, userSerial, token, secret));
Jeff Sharkey83b559c2017-09-12 16:30:52 -0600702}
703
704binder::Status VoldNativeService::lockUserKey(int32_t userId) {
Nikita Ioffee1fd4892019-10-23 17:19:13 +0100705 ENFORCE_SYSTEM_OR_ROOT;
Jeff Sharkey83b559c2017-09-12 16:30:52 -0600706 ACQUIRE_CRYPT_LOCK;
707
Eric Biggersa701c452018-10-23 13:06:55 -0700708 return translateBool(fscrypt_lock_user_key(userId));
Jeff Sharkey83b559c2017-09-12 16:30:52 -0600709}
710
Jooyung Hand75a10a2020-01-23 13:23:26 +0900711binder::Status VoldNativeService::prepareUserStorage(const std::optional<std::string>& uuid,
Paul Crowleyedf7a4e2018-09-18 15:14:18 -0700712 int32_t userId, int32_t userSerial,
713 int32_t flags) {
Nikita Ioffee1fd4892019-10-23 17:19:13 +0100714 ENFORCE_SYSTEM_OR_ROOT;
Paul Crowley3b71fc52017-10-09 10:55:21 -0700715 std::string empty_string = "";
716 auto uuid_ = uuid ? *uuid : empty_string;
Paul Crowley06f762d2017-10-16 10:59:51 -0700717 CHECK_ARGUMENT_HEX(uuid_);
718
719 ACQUIRE_CRYPT_LOCK;
Eric Biggersa701c452018-10-23 13:06:55 -0700720 return translateBool(fscrypt_prepare_user_storage(uuid_, userId, userSerial, flags));
Jeff Sharkey83b559c2017-09-12 16:30:52 -0600721}
722
Jooyung Hand75a10a2020-01-23 13:23:26 +0900723binder::Status VoldNativeService::destroyUserStorage(const std::optional<std::string>& uuid,
Paul Crowleyedf7a4e2018-09-18 15:14:18 -0700724 int32_t userId, int32_t flags) {
Nikita Ioffee1fd4892019-10-23 17:19:13 +0100725 ENFORCE_SYSTEM_OR_ROOT;
Paul Crowley3b71fc52017-10-09 10:55:21 -0700726 std::string empty_string = "";
727 auto uuid_ = uuid ? *uuid : empty_string;
Paul Crowley06f762d2017-10-16 10:59:51 -0700728 CHECK_ARGUMENT_HEX(uuid_);
729
730 ACQUIRE_CRYPT_LOCK;
Eric Biggersa701c452018-10-23 13:06:55 -0700731 return translateBool(fscrypt_destroy_user_storage(uuid_, userId, flags));
Jeff Sharkey83b559c2017-09-12 16:30:52 -0600732}
733
Sudheer Shankafa6a1742018-10-04 16:26:22 -0700734binder::Status VoldNativeService::prepareSandboxForApp(const std::string& packageName,
735 int32_t appId, const std::string& sandboxId,
736 int32_t userId) {
Yurii Zubrytskyi9e7482e2020-03-24 16:23:51 -0700737 return Ok();
Sudheer Shankafa6a1742018-10-04 16:26:22 -0700738}
739
740binder::Status VoldNativeService::destroySandboxForApp(const std::string& packageName,
Sudheer Shanka69bc40f2018-10-25 11:06:55 -0700741 const std::string& sandboxId,
Sudheer Shankafa6a1742018-10-04 16:26:22 -0700742 int32_t userId) {
Yurii Zubrytskyi9e7482e2020-03-24 16:23:51 -0700743 return Ok();
Sudheer Shankac7562092018-08-24 10:20:56 -0700744}
745
Daniel Rosenberge334aab2018-10-10 18:52:04 -0700746binder::Status VoldNativeService::startCheckpoint(int32_t retry) {
Nikita Ioffee1fd4892019-10-23 17:19:13 +0100747 ENFORCE_SYSTEM_OR_ROOT;
Daniel Rosenberg4f684712018-08-28 01:58:49 -0700748 ACQUIRE_LOCK;
749
Daniel Rosenberge334aab2018-10-10 18:52:04 -0700750 return cp_startCheckpoint(retry);
Daniel Rosenberg4f684712018-08-28 01:58:49 -0700751}
752
Daniel Rosenbergd3992492018-10-02 17:40:44 -0700753binder::Status VoldNativeService::needsRollback(bool* _aidl_return) {
Nikita Ioffee1fd4892019-10-23 17:19:13 +0100754 ENFORCE_SYSTEM_OR_ROOT;
Daniel Rosenbergd3992492018-10-02 17:40:44 -0700755 ACQUIRE_LOCK;
756
757 *_aidl_return = cp_needsRollback();
Yurii Zubrytskyi9e7482e2020-03-24 16:23:51 -0700758 return Ok();
Daniel Rosenbergd3992492018-10-02 17:40:44 -0700759}
760
Daniel Rosenberg4f684712018-08-28 01:58:49 -0700761binder::Status VoldNativeService::needsCheckpoint(bool* _aidl_return) {
Nikita Ioffee1fd4892019-10-23 17:19:13 +0100762 ENFORCE_SYSTEM_OR_ROOT;
Daniel Rosenberg4f684712018-08-28 01:58:49 -0700763 ACQUIRE_LOCK;
764
765 *_aidl_return = cp_needsCheckpoint();
Yurii Zubrytskyi9e7482e2020-03-24 16:23:51 -0700766 return Ok();
Daniel Rosenberg4f684712018-08-28 01:58:49 -0700767}
768
David Anderson23850d32020-06-10 23:51:17 -0700769binder::Status VoldNativeService::isCheckpointing(bool* _aidl_return) {
770 ENFORCE_SYSTEM_OR_ROOT;
771 ACQUIRE_LOCK;
772
773 *_aidl_return = cp_isCheckpointing();
774 return Ok();
775}
776
Daniel Rosenberge334aab2018-10-10 18:52:04 -0700777binder::Status VoldNativeService::commitChanges() {
Nikita Ioffee1fd4892019-10-23 17:19:13 +0100778 ENFORCE_SYSTEM_OR_ROOT;
Daniel Rosenberg4f684712018-08-28 01:58:49 -0700779 ACQUIRE_LOCK;
780
Daniel Rosenberge334aab2018-10-10 18:52:04 -0700781 return cp_commitChanges();
Daniel Rosenberg4f684712018-08-28 01:58:49 -0700782}
783
Daniel Rosenberge334aab2018-10-10 18:52:04 -0700784binder::Status VoldNativeService::prepareCheckpoint() {
Nikita Ioffee1fd4892019-10-23 17:19:13 +0100785 ENFORCE_SYSTEM_OR_ROOT;
Daniel Rosenberg4f684712018-08-28 01:58:49 -0700786 ACQUIRE_LOCK;
787
Daniel Rosenberge334aab2018-10-10 18:52:04 -0700788 return cp_prepareCheckpoint();
Daniel Rosenberg4f684712018-08-28 01:58:49 -0700789}
790
Daniel Rosenberge334aab2018-10-10 18:52:04 -0700791binder::Status VoldNativeService::restoreCheckpoint(const std::string& mountPoint) {
Nikita Ioffee1fd4892019-10-23 17:19:13 +0100792 ENFORCE_SYSTEM_OR_ROOT;
Paul Lawrence1abb2fe2018-09-21 10:49:57 -0700793 CHECK_ARGUMENT_PATH(mountPoint);
794 ACQUIRE_LOCK;
795
Daniel Rosenberge334aab2018-10-10 18:52:04 -0700796 return cp_restoreCheckpoint(mountPoint);
Paul Lawrence1abb2fe2018-09-21 10:49:57 -0700797}
798
Daniel Rosenbergdda59812019-03-06 17:45:17 -0800799binder::Status VoldNativeService::restoreCheckpointPart(const std::string& mountPoint, int count) {
Nikita Ioffee1fd4892019-10-23 17:19:13 +0100800 ENFORCE_SYSTEM_OR_ROOT;
Daniel Rosenbergdda59812019-03-06 17:45:17 -0800801 CHECK_ARGUMENT_PATH(mountPoint);
802 ACQUIRE_LOCK;
803
804 return cp_restoreCheckpoint(mountPoint, count);
805}
806
Daniel Rosenberge334aab2018-10-10 18:52:04 -0700807binder::Status VoldNativeService::markBootAttempt() {
Nikita Ioffee1fd4892019-10-23 17:19:13 +0100808 ENFORCE_SYSTEM_OR_ROOT;
Daniel Rosenberg4f684712018-08-28 01:58:49 -0700809 ACQUIRE_LOCK;
810
Daniel Rosenberge334aab2018-10-10 18:52:04 -0700811 return cp_markBootAttempt();
Daniel Rosenberg4f684712018-08-28 01:58:49 -0700812}
813
Daniel Rosenberga59e4392019-03-20 17:02:47 -0700814binder::Status VoldNativeService::abortChanges(const std::string& message, bool retry) {
Nikita Ioffee1fd4892019-10-23 17:19:13 +0100815 ENFORCE_SYSTEM_OR_ROOT;
Daniel Rosenberg4f684712018-08-28 01:58:49 -0700816 ACQUIRE_LOCK;
817
Daniel Rosenberga59e4392019-03-20 17:02:47 -0700818 cp_abortChanges(message, retry);
Yurii Zubrytskyi9e7482e2020-03-24 16:23:51 -0700819 return Ok();
Daniel Rosenberg4f684712018-08-28 01:58:49 -0700820}
821
Daniel Rosenberg9b667fb2019-01-22 17:27:25 -0800822binder::Status VoldNativeService::supportsCheckpoint(bool* _aidl_return) {
Nikita Ioffee1fd4892019-10-23 17:19:13 +0100823 ENFORCE_SYSTEM_OR_ROOT;
Daniel Rosenberg9b667fb2019-01-22 17:27:25 -0800824 ACQUIRE_LOCK;
825
826 return cp_supportsCheckpoint(*_aidl_return);
827}
828
Paul Lawrencec5c79c52019-03-18 13:36:40 -0700829binder::Status VoldNativeService::supportsBlockCheckpoint(bool* _aidl_return) {
Nikita Ioffee1fd4892019-10-23 17:19:13 +0100830 ENFORCE_SYSTEM_OR_ROOT;
Paul Lawrencec5c79c52019-03-18 13:36:40 -0700831 ACQUIRE_LOCK;
832
833 return cp_supportsBlockCheckpoint(*_aidl_return);
834}
835
836binder::Status VoldNativeService::supportsFileCheckpoint(bool* _aidl_return) {
Nikita Ioffee1fd4892019-10-23 17:19:13 +0100837 ENFORCE_SYSTEM_OR_ROOT;
Paul Lawrencec5c79c52019-03-18 13:36:40 -0700838 ACQUIRE_LOCK;
839
840 return cp_supportsFileCheckpoint(*_aidl_return);
841}
842
Nikita Ioffea5798fc2019-10-11 16:38:21 +0100843binder::Status VoldNativeService::resetCheckpoint() {
Nikita Ioffee1fd4892019-10-23 17:19:13 +0100844 ENFORCE_SYSTEM_OR_ROOT;
Nikita Ioffea5798fc2019-10-11 16:38:21 +0100845 ACQUIRE_LOCK;
846
847 cp_resetCheckpoint();
Yurii Zubrytskyi9e7482e2020-03-24 16:23:51 -0700848 return Ok();
Nikita Ioffea5798fc2019-10-11 16:38:21 +0100849}
850
Yurii Zubrytskyid05bcc82020-01-10 11:54:06 -0800851binder::Status VoldNativeService::incFsEnabled(bool* _aidl_return) {
Yurii Zubrytskyi9e7482e2020-03-24 16:23:51 -0700852 ENFORCE_SYSTEM_OR_ROOT;
853
Yurii Zubrytskyid05bcc82020-01-10 11:54:06 -0800854 *_aidl_return = IncFs_IsEnabled();
Yurii Zubrytskyi9e7482e2020-03-24 16:23:51 -0700855 return Ok();
Songchun Fanab55cec2019-12-02 10:50:12 -0800856}
857
858binder::Status VoldNativeService::mountIncFs(
Yurii Zubrytskyid05bcc82020-01-10 11:54:06 -0800859 const std::string& backingPath, const std::string& targetDir, int32_t flags,
Songchun Fanab55cec2019-12-02 10:50:12 -0800860 ::android::os::incremental::IncrementalFileSystemControlParcel* _aidl_return) {
Yurii Zubrytskyi9e7482e2020-03-24 16:23:51 -0700861 ENFORCE_SYSTEM_OR_ROOT;
862 CHECK_ARGUMENT_PATH(backingPath);
863 CHECK_ARGUMENT_PATH(targetDir);
864
Songchun Fan27819332020-03-03 17:58:20 -0800865 auto control = IncFs_Mount(backingPath.c_str(), targetDir.c_str(),
866 {.flags = IncFsMountFlags(flags),
867 .defaultReadTimeoutMs = INCFS_DEFAULT_READ_TIMEOUT_MS,
868 .readLogBufferPages = 4});
869 if (control == nullptr) {
870 return translate(-1);
Songchun Fanab55cec2019-12-02 10:50:12 -0800871 }
Songchun Fanab55cec2019-12-02 10:50:12 -0800872 using unique_fd = ::android::base::unique_fd;
Songchun Fan27819332020-03-03 17:58:20 -0800873 _aidl_return->cmd.emplace(unique_fd(dup(IncFs_GetControlFd(control, CMD))));
874 _aidl_return->pendingReads.emplace(unique_fd(dup(IncFs_GetControlFd(control, PENDING_READS))));
875 auto logsFd = IncFs_GetControlFd(control, LOGS);
876 if (logsFd >= 0) {
877 _aidl_return->log.emplace(unique_fd(dup(logsFd)));
Songchun Fanab55cec2019-12-02 10:50:12 -0800878 }
Songchun Fan27819332020-03-03 17:58:20 -0800879 IncFs_DeleteControl(control);
Yurii Zubrytskyi9e7482e2020-03-24 16:23:51 -0700880 return Ok();
Songchun Fanab55cec2019-12-02 10:50:12 -0800881}
882
883binder::Status VoldNativeService::unmountIncFs(const std::string& dir) {
Yurii Zubrytskyi9e7482e2020-03-24 16:23:51 -0700884 ENFORCE_SYSTEM_OR_ROOT;
885 CHECK_ARGUMENT_PATH(dir);
886
Songchun Fanab55cec2019-12-02 10:50:12 -0800887 return translate(IncFs_Unmount(dir.c_str()));
888}
889
890binder::Status VoldNativeService::bindMount(const std::string& sourceDir,
891 const std::string& targetDir) {
Yurii Zubrytskyi9e7482e2020-03-24 16:23:51 -0700892 ENFORCE_SYSTEM_OR_ROOT;
893 CHECK_ARGUMENT_PATH(sourceDir);
894 CHECK_ARGUMENT_PATH(targetDir);
895
Songchun Fanab55cec2019-12-02 10:50:12 -0800896 return translate(IncFs_BindMount(sourceDir.c_str(), targetDir.c_str()));
897}
898
Jeff Sharkey068c6be2017-09-06 13:47:40 -0600899} // namespace vold
900} // namespace android