blob: e894909f8ae446a807b26a7c02ed9cb09412ea61 [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"
Yurii Zubrytskyi9e7482e2020-03-24 16:23:51 -070029#include "VoldNativeServiceValidation.h"
Paul Crowley73be12d2020-02-03 12:22:03 -080030#include "VoldUtil.h"
31#include "VolumeManager.h"
Paul Crowleyedf7a4e2018-09-18 15:14:18 -070032#include "cryptfs.h"
Paul Crowley73be12d2020-02-03 12:22:03 -080033
Songchun Fanab55cec2019-12-02 10:50:12 -080034#include "incfs_ndk.h"
Jeff Sharkey83b559c2017-09-12 16:30:52 -060035
Jeff Sharkey068c6be2017-09-06 13:47:40 -060036#include <fstream>
Jeff Sharkey01a0e7f2017-10-17 16:06:32 -060037#include <thread>
Jeff Sharkey068c6be2017-09-06 13:47:40 -060038
39#include <android-base/logging.h>
40#include <android-base/stringprintf.h>
41#include <android-base/strings.h>
Jeff Sharkey11c2d382017-09-11 10:32:01 -060042#include <fs_mgr.h>
Eric Biggersa701c452018-10-23 13:06:55 -070043#include <fscrypt/fscrypt.h>
Jeff Sharkey068c6be2017-09-06 13:47:40 -060044#include <private/android_filesystem_config.h>
Jeff Sharkey67b8c492017-09-21 17:08:43 -060045#include <utils/Trace.h>
Jeff Sharkey068c6be2017-09-06 13:47:40 -060046
Jeff Sharkey068c6be2017-09-06 13:47:40 -060047using android::base::StringPrintf;
48using std::endl;
Yurii Zubrytskyi9e7482e2020-03-24 16:23:51 -070049using namespace std::literals;
Jeff Sharkey068c6be2017-09-06 13:47:40 -060050
51namespace android {
52namespace vold {
53
54namespace {
55
56constexpr const char* kDump = "android.permission.DUMP";
57
Jeff Sharkey9462bdd2017-09-07 15:27:28 -060058static binder::Status error(const std::string& msg) {
59 PLOG(ERROR) << msg;
60 return binder::Status::fromServiceSpecificError(errno, String8(msg.c_str()));
61}
62
Jeff Sharkey83b559c2017-09-12 16:30:52 -060063static binder::Status translate(int status) {
Jeff Sharkey9462bdd2017-09-07 15:27:28 -060064 if (status == 0) {
65 return binder::Status::ok();
66 } else {
Jeff Sharkey11c2d382017-09-11 10:32:01 -060067 return binder::Status::fromServiceSpecificError(status);
Jeff Sharkey9462bdd2017-09-07 15:27:28 -060068 }
69}
70
Jeff Sharkey83b559c2017-09-12 16:30:52 -060071static binder::Status translateBool(bool status) {
72 if (status) {
73 return binder::Status::ok();
74 } else {
75 return binder::Status::fromServiceSpecificError(status);
76 }
77}
78
Nikita Ioffee1fd4892019-10-23 17:19:13 +010079#define ENFORCE_SYSTEM_OR_ROOT \
80 { \
Yurii Zubrytskyi9e7482e2020-03-24 16:23:51 -070081 binder::Status status = CheckUidOrRoot(AID_SYSTEM); \
Nikita Ioffee1fd4892019-10-23 17:19:13 +010082 if (!status.isOk()) { \
83 return status; \
84 } \
Paul Crowleyedf7a4e2018-09-18 15:14:18 -070085 }
Jeff Sharkey068c6be2017-09-06 13:47:40 -060086
Paul Crowleyedf7a4e2018-09-18 15:14:18 -070087#define CHECK_ARGUMENT_ID(id) \
88 { \
Yurii Zubrytskyi9e7482e2020-03-24 16:23:51 -070089 binder::Status status = CheckArgumentId((id)); \
Paul Crowleyedf7a4e2018-09-18 15:14:18 -070090 if (!status.isOk()) { \
91 return status; \
92 } \
93 }
Jeff Sharkeyec4fda22017-09-12 13:19:24 -060094
Paul Crowleyedf7a4e2018-09-18 15:14:18 -070095#define CHECK_ARGUMENT_PATH(path) \
96 { \
Yurii Zubrytskyi9e7482e2020-03-24 16:23:51 -070097 binder::Status status = CheckArgumentPath((path)); \
Paul Crowleyedf7a4e2018-09-18 15:14:18 -070098 if (!status.isOk()) { \
99 return status; \
100 } \
101 }
Jeff Sharkeyec4fda22017-09-12 13:19:24 -0600102
Paul Crowleyedf7a4e2018-09-18 15:14:18 -0700103#define CHECK_ARGUMENT_HEX(hex) \
104 { \
Yurii Zubrytskyi9e7482e2020-03-24 16:23:51 -0700105 binder::Status status = CheckArgumentHex((hex)); \
Paul Crowleyedf7a4e2018-09-18 15:14:18 -0700106 if (!status.isOk()) { \
107 return status; \
108 } \
109 }
Jeff Sharkeyec4fda22017-09-12 13:19:24 -0600110
Paul Crowleyedf7a4e2018-09-18 15:14:18 -0700111#define ACQUIRE_LOCK \
Jeff Sharkey67b8c492017-09-21 17:08:43 -0600112 std::lock_guard<std::mutex> lock(VolumeManager::Instance()->getLock()); \
113 ATRACE_CALL();
Jeff Sharkey83b559c2017-09-12 16:30:52 -0600114
Paul Crowleyedf7a4e2018-09-18 15:14:18 -0700115#define ACQUIRE_CRYPT_LOCK \
Jeff Sharkey67b8c492017-09-21 17:08:43 -0600116 std::lock_guard<std::mutex> lock(VolumeManager::Instance()->getCryptLock()); \
117 ATRACE_CALL();
Jeff Sharkey9462bdd2017-09-07 15:27:28 -0600118
Jeff Sharkey068c6be2017-09-06 13:47:40 -0600119} // namespace
120
121status_t VoldNativeService::start() {
122 IPCThreadState::self()->disableBackgroundScheduling(true);
123 status_t ret = BinderService<VoldNativeService>::publish();
124 if (ret != android::OK) {
125 return ret;
126 }
127 sp<ProcessState> ps(ProcessState::self());
128 ps->startThreadPool();
129 ps->giveThreadPoolName();
130 return android::OK;
131}
132
Paul Crowleyedf7a4e2018-09-18 15:14:18 -0700133status_t VoldNativeService::dump(int fd, const Vector<String16>& /* args */) {
Jeff Sharkey068c6be2017-09-06 13:47:40 -0600134 auto out = std::fstream(StringPrintf("/proc/self/fd/%d", fd));
Yurii Zubrytskyi9e7482e2020-03-24 16:23:51 -0700135 const binder::Status dump_permission = CheckPermission(kDump);
Jeff Sharkey068c6be2017-09-06 13:47:40 -0600136 if (!dump_permission.isOk()) {
137 out << dump_permission.toString8() << endl;
138 return PERMISSION_DENIED;
139 }
140
Jeff Sharkey9462bdd2017-09-07 15:27:28 -0600141 ACQUIRE_LOCK;
Jeff Sharkey068c6be2017-09-06 13:47:40 -0600142 out << "vold is happy!" << endl;
Jeff Sharkey068c6be2017-09-06 13:47:40 -0600143 return NO_ERROR;
144}
145
Jeff Sharkey814e9d32017-09-13 11:49:44 -0600146binder::Status VoldNativeService::setListener(
Paul Crowleyedf7a4e2018-09-18 15:14:18 -0700147 const android::sp<android::os::IVoldListener>& listener) {
Nikita Ioffee1fd4892019-10-23 17:19:13 +0100148 ENFORCE_SYSTEM_OR_ROOT;
Jeff Sharkey814e9d32017-09-13 11:49:44 -0600149 ACQUIRE_LOCK;
150
151 VolumeManager::Instance()->setListener(listener);
Yurii Zubrytskyi9e7482e2020-03-24 16:23:51 -0700152 return Ok();
Jeff Sharkey814e9d32017-09-13 11:49:44 -0600153}
154
Jeff Sharkeycbe69fc2017-09-15 16:50:28 -0600155binder::Status VoldNativeService::monitor() {
Nikita Ioffee1fd4892019-10-23 17:19:13 +0100156 ENFORCE_SYSTEM_OR_ROOT;
Jeff Sharkeycbe69fc2017-09-15 16:50:28 -0600157
158 // Simply acquire/release each lock for watchdog
Paul Crowleyedf7a4e2018-09-18 15:14:18 -0700159 { ACQUIRE_LOCK; }
160 { ACQUIRE_CRYPT_LOCK; }
Jeff Sharkeycbe69fc2017-09-15 16:50:28 -0600161
Yurii Zubrytskyi9e7482e2020-03-24 16:23:51 -0700162 return Ok();
Jeff Sharkeycbe69fc2017-09-15 16:50:28 -0600163}
164
Jeff Sharkey068c6be2017-09-06 13:47:40 -0600165binder::Status VoldNativeService::reset() {
Nikita Ioffee1fd4892019-10-23 17:19:13 +0100166 ENFORCE_SYSTEM_OR_ROOT;
Jeff Sharkey9462bdd2017-09-07 15:27:28 -0600167 ACQUIRE_LOCK;
Jeff Sharkey068c6be2017-09-06 13:47:40 -0600168
Jeff Sharkey9462bdd2017-09-07 15:27:28 -0600169 return translate(VolumeManager::Instance()->reset());
170}
171
172binder::Status VoldNativeService::shutdown() {
Nikita Ioffee1fd4892019-10-23 17:19:13 +0100173 ENFORCE_SYSTEM_OR_ROOT;
Jeff Sharkey9462bdd2017-09-07 15:27:28 -0600174 ACQUIRE_LOCK;
175
176 return translate(VolumeManager::Instance()->shutdown());
177}
178
Jeff Sharkey9462bdd2017-09-07 15:27:28 -0600179binder::Status VoldNativeService::onUserAdded(int32_t userId, int32_t userSerial) {
Nikita Ioffee1fd4892019-10-23 17:19:13 +0100180 ENFORCE_SYSTEM_OR_ROOT;
Jeff Sharkey9462bdd2017-09-07 15:27:28 -0600181 ACQUIRE_LOCK;
182
183 return translate(VolumeManager::Instance()->onUserAdded(userId, userSerial));
184}
185
186binder::Status VoldNativeService::onUserRemoved(int32_t userId) {
Nikita Ioffee1fd4892019-10-23 17:19:13 +0100187 ENFORCE_SYSTEM_OR_ROOT;
Jeff Sharkey9462bdd2017-09-07 15:27:28 -0600188 ACQUIRE_LOCK;
189
190 return translate(VolumeManager::Instance()->onUserRemoved(userId));
191}
192
Sudheer Shanka5fceb482019-04-29 10:46:35 -0700193binder::Status VoldNativeService::onUserStarted(int32_t userId) {
Nikita Ioffee1fd4892019-10-23 17:19:13 +0100194 ENFORCE_SYSTEM_OR_ROOT;
Jeff Sharkey9462bdd2017-09-07 15:27:28 -0600195 ACQUIRE_LOCK;
196
Sudheer Shanka5fceb482019-04-29 10:46:35 -0700197 return translate(VolumeManager::Instance()->onUserStarted(userId));
Jeff Sharkey9462bdd2017-09-07 15:27:28 -0600198}
199
200binder::Status VoldNativeService::onUserStopped(int32_t userId) {
Nikita Ioffee1fd4892019-10-23 17:19:13 +0100201 ENFORCE_SYSTEM_OR_ROOT;
Jeff Sharkey9462bdd2017-09-07 15:27:28 -0600202 ACQUIRE_LOCK;
203
204 return translate(VolumeManager::Instance()->onUserStopped(userId));
205}
206
Sudheer Shankad484aa92018-07-31 10:07:34 -0700207binder::Status VoldNativeService::addAppIds(const std::vector<std::string>& packageNames,
Paul Crowleyedf7a4e2018-09-18 15:14:18 -0700208 const std::vector<int32_t>& appIds) {
Yurii Zubrytskyi9e7482e2020-03-24 16:23:51 -0700209 return Ok();
Sudheer Shankad484aa92018-07-31 10:07:34 -0700210}
211
Sudheer Shankad484aa92018-07-31 10:07:34 -0700212binder::Status VoldNativeService::addSandboxIds(const std::vector<int32_t>& appIds,
Paul Crowleyedf7a4e2018-09-18 15:14:18 -0700213 const std::vector<std::string>& sandboxIds) {
Yurii Zubrytskyi9e7482e2020-03-24 16:23:51 -0700214 return Ok();
Sudheer Shankad484aa92018-07-31 10:07:34 -0700215}
216
Jeff Sharkey401b2602017-12-14 22:15:20 -0700217binder::Status VoldNativeService::onSecureKeyguardStateChanged(bool isShowing) {
Nikita Ioffee1fd4892019-10-23 17:19:13 +0100218 ENFORCE_SYSTEM_OR_ROOT;
Jeff Sharkey401b2602017-12-14 22:15:20 -0700219 ACQUIRE_LOCK;
220
221 return translate(VolumeManager::Instance()->onSecureKeyguardStateChanged(isShowing));
222}
223
Jeff Sharkey11c2d382017-09-11 10:32:01 -0600224binder::Status VoldNativeService::partition(const std::string& diskId, int32_t partitionType,
Paul Crowleyedf7a4e2018-09-18 15:14:18 -0700225 int32_t ratio) {
Nikita Ioffee1fd4892019-10-23 17:19:13 +0100226 ENFORCE_SYSTEM_OR_ROOT;
Jeff Sharkeyec4fda22017-09-12 13:19:24 -0600227 CHECK_ARGUMENT_ID(diskId);
Jeff Sharkey9462bdd2017-09-07 15:27:28 -0600228 ACQUIRE_LOCK;
229
230 auto disk = VolumeManager::Instance()->findDisk(diskId);
231 if (disk == nullptr) {
232 return error("Failed to find disk " + diskId);
233 }
234 switch (partitionType) {
Paul Crowleyedf7a4e2018-09-18 15:14:18 -0700235 case PARTITION_TYPE_PUBLIC:
236 return translate(disk->partitionPublic());
237 case PARTITION_TYPE_PRIVATE:
238 return translate(disk->partitionPrivate());
239 case PARTITION_TYPE_MIXED:
240 return translate(disk->partitionMixed(ratio));
241 default:
242 return error("Unknown type " + std::to_string(partitionType));
Jeff Sharkey9462bdd2017-09-07 15:27:28 -0600243 }
244}
245
Jeff Sharkey3ce18252017-10-24 11:08:45 -0600246binder::Status VoldNativeService::forgetPartition(const std::string& partGuid,
Paul Crowleyedf7a4e2018-09-18 15:14:18 -0700247 const std::string& fsUuid) {
Nikita Ioffee1fd4892019-10-23 17:19:13 +0100248 ENFORCE_SYSTEM_OR_ROOT;
Jeff Sharkeyec4fda22017-09-12 13:19:24 -0600249 CHECK_ARGUMENT_HEX(partGuid);
Jeff Sharkey3ce18252017-10-24 11:08:45 -0600250 CHECK_ARGUMENT_HEX(fsUuid);
Jeff Sharkey9462bdd2017-09-07 15:27:28 -0600251 ACQUIRE_LOCK;
252
Jeff Sharkey3ce18252017-10-24 11:08:45 -0600253 return translate(VolumeManager::Instance()->forgetPartition(partGuid, fsUuid));
Jeff Sharkey9462bdd2017-09-07 15:27:28 -0600254}
255
Jeff Sharkey11c2d382017-09-11 10:32:01 -0600256binder::Status VoldNativeService::mount(const std::string& volId, int32_t mountFlags,
Paul Crowleyedf7a4e2018-09-18 15:14:18 -0700257 int32_t mountUserId) {
Nikita Ioffee1fd4892019-10-23 17:19:13 +0100258 ENFORCE_SYSTEM_OR_ROOT;
Jeff Sharkeyec4fda22017-09-12 13:19:24 -0600259 CHECK_ARGUMENT_ID(volId);
Jeff Sharkey9462bdd2017-09-07 15:27:28 -0600260 ACQUIRE_LOCK;
261
262 auto vol = VolumeManager::Instance()->findVolume(volId);
263 if (vol == nullptr) {
264 return error("Failed to find volume " + volId);
265 }
266
267 vol->setMountFlags(mountFlags);
268 vol->setMountUserId(mountUserId);
269
270 int res = vol->mount();
Sudheer Shanka40ab6742018-09-18 13:07:45 -0700271 if (res != OK) {
272 return translate(res);
Jeff Sharkey9462bdd2017-09-07 15:27:28 -0600273 }
Sudheer Shanka40ab6742018-09-18 13:07:45 -0700274 if ((mountFlags & MOUNT_FLAG_PRIMARY) != 0) {
275 res = VolumeManager::Instance()->setPrimary(vol);
276 if (res != OK) {
277 return translate(res);
278 }
279 }
280 return translate(OK);
Jeff Sharkey9462bdd2017-09-07 15:27:28 -0600281}
282
283binder::Status VoldNativeService::unmount(const std::string& volId) {
Nikita Ioffee1fd4892019-10-23 17:19:13 +0100284 ENFORCE_SYSTEM_OR_ROOT;
Jeff Sharkeyec4fda22017-09-12 13:19:24 -0600285 CHECK_ARGUMENT_ID(volId);
Jeff Sharkey9462bdd2017-09-07 15:27:28 -0600286 ACQUIRE_LOCK;
287
288 auto vol = VolumeManager::Instance()->findVolume(volId);
289 if (vol == nullptr) {
290 return error("Failed to find volume " + volId);
291 }
292 return translate(vol->unmount());
293}
294
295binder::Status VoldNativeService::format(const std::string& volId, const std::string& fsType) {
Nikita Ioffee1fd4892019-10-23 17:19:13 +0100296 ENFORCE_SYSTEM_OR_ROOT;
Jeff Sharkeyec4fda22017-09-12 13:19:24 -0600297 CHECK_ARGUMENT_ID(volId);
Jeff Sharkey9462bdd2017-09-07 15:27:28 -0600298 ACQUIRE_LOCK;
299
300 auto vol = VolumeManager::Instance()->findVolume(volId);
301 if (vol == nullptr) {
302 return error("Failed to find volume " + volId);
303 }
304 return translate(vol->format(fsType));
305}
306
Jeff Sharkey2048a282017-06-15 09:59:43 -0600307static binder::Status pathForVolId(const std::string& volId, std::string* path) {
Jeff Sharkey52f7a912017-09-15 12:57:44 -0600308 if (volId == "private" || volId == "null") {
Jeff Sharkey2048a282017-06-15 09:59:43 -0600309 *path = "/data";
Jeff Sharkey52f7a912017-09-15 12:57:44 -0600310 } else {
311 auto vol = VolumeManager::Instance()->findVolume(volId);
312 if (vol == nullptr) {
313 return error("Failed to find volume " + volId);
314 }
315 if (vol->getType() != VolumeBase::Type::kPrivate) {
316 return error("Volume " + volId + " not private");
317 }
318 if (vol->getState() != VolumeBase::State::kMounted) {
319 return error("Volume " + volId + " not mounted");
320 }
Jeff Sharkey2048a282017-06-15 09:59:43 -0600321 *path = vol->getPath();
322 if (path->empty()) {
323 return error("Volume " + volId + " missing path");
324 }
Jeff Sharkey52f7a912017-09-15 12:57:44 -0600325 }
Yurii Zubrytskyi9e7482e2020-03-24 16:23:51 -0700326 return Ok();
Jeff Sharkey2048a282017-06-15 09:59:43 -0600327}
Jeff Sharkey52f7a912017-09-15 12:57:44 -0600328
Jeff Sharkey2048a282017-06-15 09:59:43 -0600329binder::Status VoldNativeService::benchmark(
330 const std::string& volId, const android::sp<android::os::IVoldTaskListener>& listener) {
Nikita Ioffee1fd4892019-10-23 17:19:13 +0100331 ENFORCE_SYSTEM_OR_ROOT;
Jeff Sharkey2048a282017-06-15 09:59:43 -0600332 CHECK_ARGUMENT_ID(volId);
333 ACQUIRE_LOCK;
334
335 std::string path;
336 auto status = pathForVolId(volId, &path);
337 if (!status.isOk()) return status;
Jeff Sharkey52f7a912017-09-15 12:57:44 -0600338
Paul Crowleyedf7a4e2018-09-18 15:14:18 -0700339 std::thread([=]() { android::vold::Benchmark(path, listener); }).detach();
Yurii Zubrytskyi9e7482e2020-03-24 16:23:51 -0700340 return Ok();
Jeff Sharkey068c6be2017-09-06 13:47:40 -0600341}
342
Jeff Sharkey2048a282017-06-15 09:59:43 -0600343binder::Status VoldNativeService::checkEncryption(const std::string& volId) {
Nikita Ioffee1fd4892019-10-23 17:19:13 +0100344 ENFORCE_SYSTEM_OR_ROOT;
Jeff Sharkey2048a282017-06-15 09:59:43 -0600345 CHECK_ARGUMENT_ID(volId);
346 ACQUIRE_LOCK;
347
348 std::string path;
349 auto status = pathForVolId(volId, &path);
350 if (!status.isOk()) return status;
351 return translate(android::vold::CheckEncryption(path));
352}
353
Paul Crowleyedf7a4e2018-09-18 15:14:18 -0700354binder::Status VoldNativeService::moveStorage(
355 const std::string& fromVolId, const std::string& toVolId,
356 const android::sp<android::os::IVoldTaskListener>& listener) {
Nikita Ioffee1fd4892019-10-23 17:19:13 +0100357 ENFORCE_SYSTEM_OR_ROOT;
Jeff Sharkeyec4fda22017-09-12 13:19:24 -0600358 CHECK_ARGUMENT_ID(fromVolId);
359 CHECK_ARGUMENT_ID(toVolId);
Jeff Sharkey9462bdd2017-09-07 15:27:28 -0600360 ACQUIRE_LOCK;
361
362 auto fromVol = VolumeManager::Instance()->findVolume(fromVolId);
363 auto toVol = VolumeManager::Instance()->findVolume(toVolId);
364 if (fromVol == nullptr) {
365 return error("Failed to find volume " + fromVolId);
366 } else if (toVol == nullptr) {
367 return error("Failed to find volume " + toVolId);
368 }
Jeff Sharkey01a0e7f2017-10-17 16:06:32 -0600369
Paul Crowleyedf7a4e2018-09-18 15:14:18 -0700370 std::thread([=]() { android::vold::MoveStorage(fromVol, toVol, listener); }).detach();
Yurii Zubrytskyi9e7482e2020-03-24 16:23:51 -0700371 return Ok();
Jeff Sharkey9462bdd2017-09-07 15:27:28 -0600372}
373
374binder::Status VoldNativeService::remountUid(int32_t uid, int32_t remountMode) {
Nikita Ioffee1fd4892019-10-23 17:19:13 +0100375 ENFORCE_SYSTEM_OR_ROOT;
Jeff Sharkey9462bdd2017-09-07 15:27:28 -0600376 ACQUIRE_LOCK;
377
Sudheer Shanka817b9112018-12-13 17:40:28 -0800378 return translate(VolumeManager::Instance()->remountUid(uid, remountMode));
Jeff Sharkey9462bdd2017-09-07 15:27:28 -0600379}
380
381binder::Status VoldNativeService::mkdirs(const std::string& path) {
Nikita Ioffee1fd4892019-10-23 17:19:13 +0100382 ENFORCE_SYSTEM_OR_ROOT;
Jeff Sharkeyec4fda22017-09-12 13:19:24 -0600383 CHECK_ARGUMENT_PATH(path);
Jeff Sharkey9462bdd2017-09-07 15:27:28 -0600384 ACQUIRE_LOCK;
385
Jeff Sharkey3472e522017-10-06 18:02:53 -0600386 return translate(VolumeManager::Instance()->mkdirs(path));
Jeff Sharkey9462bdd2017-09-07 15:27:28 -0600387}
388
Jeff Sharkey11c2d382017-09-11 10:32:01 -0600389binder::Status VoldNativeService::createObb(const std::string& sourcePath,
Paul Crowleyedf7a4e2018-09-18 15:14:18 -0700390 const std::string& sourceKey, int32_t ownerGid,
391 std::string* _aidl_return) {
Nikita Ioffee1fd4892019-10-23 17:19:13 +0100392 ENFORCE_SYSTEM_OR_ROOT;
Jeff Sharkeyec4fda22017-09-12 13:19:24 -0600393 CHECK_ARGUMENT_PATH(sourcePath);
394 CHECK_ARGUMENT_HEX(sourceKey);
Jeff Sharkey11c2d382017-09-11 10:32:01 -0600395 ACQUIRE_LOCK;
396
397 return translate(
Paul Crowleyedf7a4e2018-09-18 15:14:18 -0700398 VolumeManager::Instance()->createObb(sourcePath, sourceKey, ownerGid, _aidl_return));
Jeff Sharkey11c2d382017-09-11 10:32:01 -0600399}
400
401binder::Status VoldNativeService::destroyObb(const std::string& volId) {
Nikita Ioffee1fd4892019-10-23 17:19:13 +0100402 ENFORCE_SYSTEM_OR_ROOT;
Jeff Sharkeyec4fda22017-09-12 13:19:24 -0600403 CHECK_ARGUMENT_ID(volId);
Jeff Sharkey11c2d382017-09-11 10:32:01 -0600404 ACQUIRE_LOCK;
405
406 return translate(VolumeManager::Instance()->destroyObb(volId));
407}
408
Risan8c9f3322018-10-29 08:52:56 +0900409binder::Status VoldNativeService::createStubVolume(
410 const std::string& sourcePath, const std::string& mountPath, const std::string& fsType,
411 const std::string& fsUuid, const std::string& fsLabel, std::string* _aidl_return) {
Nikita Ioffee1fd4892019-10-23 17:19:13 +0100412 ENFORCE_SYSTEM_OR_ROOT;
Risan8c9f3322018-10-29 08:52:56 +0900413 CHECK_ARGUMENT_PATH(sourcePath);
414 CHECK_ARGUMENT_PATH(mountPath);
415 CHECK_ARGUMENT_HEX(fsUuid);
416 // Label limitation seems to be different between fs (including allowed characters), so checking
417 // is quite meaningless.
418 ACQUIRE_LOCK;
419
420 return translate(VolumeManager::Instance()->createStubVolume(sourcePath, mountPath, fsType,
421 fsUuid, fsLabel, _aidl_return));
422}
423
424binder::Status VoldNativeService::destroyStubVolume(const std::string& volId) {
Nikita Ioffee1fd4892019-10-23 17:19:13 +0100425 ENFORCE_SYSTEM_OR_ROOT;
Risan8c9f3322018-10-29 08:52:56 +0900426 CHECK_ARGUMENT_ID(volId);
427 ACQUIRE_LOCK;
428
429 return translate(VolumeManager::Instance()->destroyStubVolume(volId));
430}
431
Paul Crowleyedf7a4e2018-09-18 15:14:18 -0700432binder::Status VoldNativeService::fstrim(
433 int32_t fstrimFlags, const android::sp<android::os::IVoldTaskListener>& listener) {
Nikita Ioffee1fd4892019-10-23 17:19:13 +0100434 ENFORCE_SYSTEM_OR_ROOT;
Jeff Sharkey11c2d382017-09-11 10:32:01 -0600435 ACQUIRE_LOCK;
436
Paul Crowleyedf7a4e2018-09-18 15:14:18 -0700437 std::thread([=]() { android::vold::Trim(listener); }).detach();
Yurii Zubrytskyi9e7482e2020-03-24 16:23:51 -0700438 return Ok();
Jeff Sharkey11c2d382017-09-11 10:32:01 -0600439}
440
Jin Qiana370c142017-10-17 15:41:45 -0700441binder::Status VoldNativeService::runIdleMaint(
Paul Crowleyedf7a4e2018-09-18 15:14:18 -0700442 const android::sp<android::os::IVoldTaskListener>& listener) {
Nikita Ioffee1fd4892019-10-23 17:19:13 +0100443 ENFORCE_SYSTEM_OR_ROOT;
Jin Qiana370c142017-10-17 15:41:45 -0700444 ACQUIRE_LOCK;
445
Paul Crowleyedf7a4e2018-09-18 15:14:18 -0700446 std::thread([=]() { android::vold::RunIdleMaint(listener); }).detach();
Yurii Zubrytskyi9e7482e2020-03-24 16:23:51 -0700447 return Ok();
Jin Qiana370c142017-10-17 15:41:45 -0700448}
449
450binder::Status VoldNativeService::abortIdleMaint(
Paul Crowleyedf7a4e2018-09-18 15:14:18 -0700451 const android::sp<android::os::IVoldTaskListener>& listener) {
Nikita Ioffee1fd4892019-10-23 17:19:13 +0100452 ENFORCE_SYSTEM_OR_ROOT;
Jin Qiana370c142017-10-17 15:41:45 -0700453 ACQUIRE_LOCK;
454
Paul Crowleyedf7a4e2018-09-18 15:14:18 -0700455 std::thread([=]() { android::vold::AbortIdleMaint(listener); }).detach();
Yurii Zubrytskyi9e7482e2020-03-24 16:23:51 -0700456 return Ok();
Jin Qiana370c142017-10-17 15:41:45 -0700457}
458
Risan8f6198d2018-10-26 20:56:45 -0600459binder::Status VoldNativeService::mountAppFuse(int32_t uid, int32_t mountId,
Paul Crowleyedf7a4e2018-09-18 15:14:18 -0700460 android::base::unique_fd* _aidl_return) {
Nikita Ioffee1fd4892019-10-23 17:19:13 +0100461 ENFORCE_SYSTEM_OR_ROOT;
Jeff Sharkey11c2d382017-09-11 10:32:01 -0600462 ACQUIRE_LOCK;
463
Risan8f6198d2018-10-26 20:56:45 -0600464 return translate(VolumeManager::Instance()->mountAppFuse(uid, mountId, _aidl_return));
Jeff Sharkey11c2d382017-09-11 10:32:01 -0600465}
466
Risan8f6198d2018-10-26 20:56:45 -0600467binder::Status VoldNativeService::unmountAppFuse(int32_t uid, int32_t mountId) {
Nikita Ioffee1fd4892019-10-23 17:19:13 +0100468 ENFORCE_SYSTEM_OR_ROOT;
Jeff Sharkey11c2d382017-09-11 10:32:01 -0600469 ACQUIRE_LOCK;
470
Risan8f6198d2018-10-26 20:56:45 -0600471 return translate(VolumeManager::Instance()->unmountAppFuse(uid, mountId));
472}
473
474binder::Status VoldNativeService::openAppFuseFile(int32_t uid, int32_t mountId, int32_t fileId,
475 int32_t flags,
476 android::base::unique_fd* _aidl_return) {
Nikita Ioffee1fd4892019-10-23 17:19:13 +0100477 ENFORCE_SYSTEM_OR_ROOT;
Risan8f6198d2018-10-26 20:56:45 -0600478 ACQUIRE_LOCK;
479
480 int fd = VolumeManager::Instance()->openAppFuseFile(uid, mountId, fileId, flags);
481 if (fd == -1) {
482 return error("Failed to open AppFuse file for uid: " + std::to_string(uid) +
483 " mountId: " + std::to_string(mountId) + " fileId: " + std::to_string(fileId) +
484 " flags: " + std::to_string(flags));
485 }
486
487 *_aidl_return = android::base::unique_fd(fd);
Yurii Zubrytskyi9e7482e2020-03-24 16:23:51 -0700488 return Ok();
Jeff Sharkey11c2d382017-09-11 10:32:01 -0600489}
490
Jeff Sharkey83b559c2017-09-12 16:30:52 -0600491binder::Status VoldNativeService::fdeCheckPassword(const std::string& password) {
Nikita Ioffee1fd4892019-10-23 17:19:13 +0100492 ENFORCE_SYSTEM_OR_ROOT;
Jeff Sharkey83b559c2017-09-12 16:30:52 -0600493 ACQUIRE_CRYPT_LOCK;
494
495 return translate(cryptfs_check_passwd(password.c_str()));
496}
497
498binder::Status VoldNativeService::fdeRestart() {
Nikita Ioffee1fd4892019-10-23 17:19:13 +0100499 ENFORCE_SYSTEM_OR_ROOT;
Jeff Sharkey83b559c2017-09-12 16:30:52 -0600500 ACQUIRE_CRYPT_LOCK;
501
502 // Spawn as thread so init can issue commands back to vold without
503 // causing deadlock, usually as a result of prep_data_fs.
504 std::thread(&cryptfs_restart).detach();
Yurii Zubrytskyi9e7482e2020-03-24 16:23:51 -0700505 return Ok();
Jeff Sharkey83b559c2017-09-12 16:30:52 -0600506}
507
508binder::Status VoldNativeService::fdeComplete(int32_t* _aidl_return) {
Nikita Ioffee1fd4892019-10-23 17:19:13 +0100509 ENFORCE_SYSTEM_OR_ROOT;
Jeff Sharkey83b559c2017-09-12 16:30:52 -0600510 ACQUIRE_CRYPT_LOCK;
511
512 *_aidl_return = cryptfs_crypto_complete();
Yurii Zubrytskyi9e7482e2020-03-24 16:23:51 -0700513 return Ok();
Jeff Sharkey83b559c2017-09-12 16:30:52 -0600514}
515
516static int fdeEnableInternal(int32_t passwordType, const std::string& password,
Paul Crowleyedf7a4e2018-09-18 15:14:18 -0700517 int32_t encryptionFlags) {
Jeff Sharkey83b559c2017-09-12 16:30:52 -0600518 bool noUi = (encryptionFlags & VoldNativeService::ENCRYPTION_FLAG_NO_UI) != 0;
519
Jeff Sharkey83b559c2017-09-12 16:30:52 -0600520 for (int tries = 0; tries < 2; ++tries) {
521 int rc;
522 if (passwordType == VoldNativeService::PASSWORD_TYPE_DEFAULT) {
Paul Lawrence7ee87cf2017-12-22 10:12:06 -0800523 rc = cryptfs_enable_default(noUi);
Jeff Sharkey83b559c2017-09-12 16:30:52 -0600524 } else {
Paul Lawrence7ee87cf2017-12-22 10:12:06 -0800525 rc = cryptfs_enable(passwordType, password.c_str(), noUi);
Jeff Sharkey83b559c2017-09-12 16:30:52 -0600526 }
527
528 if (rc == 0) {
529 return 0;
530 } else if (tries == 0) {
Jeff Sharkey3472e522017-10-06 18:02:53 -0600531 KillProcessesWithOpenFiles(DATA_MNT_POINT, SIGKILL);
Jeff Sharkey83b559c2017-09-12 16:30:52 -0600532 }
533 }
534
535 return -1;
536}
537
Paul Crowleyedf7a4e2018-09-18 15:14:18 -0700538binder::Status VoldNativeService::fdeEnable(int32_t passwordType, const std::string& password,
539 int32_t encryptionFlags) {
Nikita Ioffee1fd4892019-10-23 17:19:13 +0100540 ENFORCE_SYSTEM_OR_ROOT;
Jeff Sharkey83b559c2017-09-12 16:30:52 -0600541 ACQUIRE_CRYPT_LOCK;
542
Paul Crowley0fd26262018-01-30 09:48:19 -0800543 LOG(DEBUG) << "fdeEnable(" << passwordType << ", *, " << encryptionFlags << ")";
Eric Biggersa701c452018-10-23 13:06:55 -0700544 if (fscrypt_is_native()) {
545 LOG(ERROR) << "fscrypt_is_native, fdeEnable invalid";
546 return error("fscrypt_is_native, fdeEnable invalid");
Jeff Sharkey83b559c2017-09-12 16:30:52 -0600547 }
Eric Biggersa701c452018-10-23 13:06:55 -0700548 LOG(DEBUG) << "!fscrypt_is_native, spawning fdeEnableInternal";
Jeff Sharkey83b559c2017-09-12 16:30:52 -0600549
550 // Spawn as thread so init can issue commands back to vold without
551 // causing deadlock, usually as a result of prep_data_fs.
552 std::thread(&fdeEnableInternal, passwordType, password, encryptionFlags).detach();
Yurii Zubrytskyi9e7482e2020-03-24 16:23:51 -0700553 return Ok();
Jeff Sharkey83b559c2017-09-12 16:30:52 -0600554}
555
556binder::Status VoldNativeService::fdeChangePassword(int32_t passwordType,
Paul Crowleyedf7a4e2018-09-18 15:14:18 -0700557 const std::string& password) {
Nikita Ioffee1fd4892019-10-23 17:19:13 +0100558 ENFORCE_SYSTEM_OR_ROOT;
Jeff Sharkey83b559c2017-09-12 16:30:52 -0600559 ACQUIRE_CRYPT_LOCK;
560
561 return translate(cryptfs_changepw(passwordType, password.c_str()));
562}
563
564binder::Status VoldNativeService::fdeVerifyPassword(const std::string& password) {
Nikita Ioffee1fd4892019-10-23 17:19:13 +0100565 ENFORCE_SYSTEM_OR_ROOT;
Jeff Sharkey83b559c2017-09-12 16:30:52 -0600566 ACQUIRE_CRYPT_LOCK;
567
568 return translate(cryptfs_verify_passwd(password.c_str()));
569}
570
Paul Crowleyedf7a4e2018-09-18 15:14:18 -0700571binder::Status VoldNativeService::fdeGetField(const std::string& key, std::string* _aidl_return) {
Nikita Ioffee1fd4892019-10-23 17:19:13 +0100572 ENFORCE_SYSTEM_OR_ROOT;
Jeff Sharkey83b559c2017-09-12 16:30:52 -0600573 ACQUIRE_CRYPT_LOCK;
574
575 char buf[PROPERTY_VALUE_MAX];
576 if (cryptfs_getfield(key.c_str(), buf, sizeof(buf)) != CRYPTO_GETFIELD_OK) {
577 return error(StringPrintf("Failed to read field %s", key.c_str()));
578 } else {
579 *_aidl_return = buf;
Yurii Zubrytskyi9e7482e2020-03-24 16:23:51 -0700580 return Ok();
Jeff Sharkey83b559c2017-09-12 16:30:52 -0600581 }
582}
583
Paul Crowleyedf7a4e2018-09-18 15:14:18 -0700584binder::Status VoldNativeService::fdeSetField(const std::string& key, const std::string& value) {
Nikita Ioffee1fd4892019-10-23 17:19:13 +0100585 ENFORCE_SYSTEM_OR_ROOT;
Jeff Sharkey83b559c2017-09-12 16:30:52 -0600586 ACQUIRE_CRYPT_LOCK;
587
588 return translate(cryptfs_setfield(key.c_str(), value.c_str()));
589}
590
591binder::Status VoldNativeService::fdeGetPasswordType(int32_t* _aidl_return) {
Nikita Ioffee1fd4892019-10-23 17:19:13 +0100592 ENFORCE_SYSTEM_OR_ROOT;
Jeff Sharkey83b559c2017-09-12 16:30:52 -0600593 ACQUIRE_CRYPT_LOCK;
594
595 *_aidl_return = cryptfs_get_password_type();
Yurii Zubrytskyi9e7482e2020-03-24 16:23:51 -0700596 return Ok();
Jeff Sharkey83b559c2017-09-12 16:30:52 -0600597}
598
599binder::Status VoldNativeService::fdeGetPassword(std::string* _aidl_return) {
Nikita Ioffee1fd4892019-10-23 17:19:13 +0100600 ENFORCE_SYSTEM_OR_ROOT;
Jeff Sharkey83b559c2017-09-12 16:30:52 -0600601 ACQUIRE_CRYPT_LOCK;
602
603 const char* res = cryptfs_get_password();
604 if (res != nullptr) {
605 *_aidl_return = res;
606 }
Yurii Zubrytskyi9e7482e2020-03-24 16:23:51 -0700607 return Ok();
Jeff Sharkey83b559c2017-09-12 16:30:52 -0600608}
609
610binder::Status VoldNativeService::fdeClearPassword() {
Nikita Ioffee1fd4892019-10-23 17:19:13 +0100611 ENFORCE_SYSTEM_OR_ROOT;
Jeff Sharkey83b559c2017-09-12 16:30:52 -0600612 ACQUIRE_CRYPT_LOCK;
613
614 cryptfs_clear_password();
Yurii Zubrytskyi9e7482e2020-03-24 16:23:51 -0700615 return Ok();
Jeff Sharkey83b559c2017-09-12 16:30:52 -0600616}
617
618binder::Status VoldNativeService::fbeEnable() {
Nikita Ioffee1fd4892019-10-23 17:19:13 +0100619 ENFORCE_SYSTEM_OR_ROOT;
Jeff Sharkey83b559c2017-09-12 16:30:52 -0600620 ACQUIRE_CRYPT_LOCK;
621
Paul Crowleyc8a3ef32019-09-11 15:00:08 -0700622 return translateBool(fscrypt_initialize_systemwide_keys());
Jeff Sharkey83b559c2017-09-12 16:30:52 -0600623}
624
625binder::Status VoldNativeService::mountDefaultEncrypted() {
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 if (!fscrypt_is_native()) {
Jeff Sharkey83b559c2017-09-12 16:30:52 -0600630 // Spawn as thread so init can issue commands back to vold without
631 // causing deadlock, usually as a result of prep_data_fs.
632 std::thread(&cryptfs_mount_default_encrypted).detach();
Jeff Sharkey83b559c2017-09-12 16:30:52 -0600633 }
Yurii Zubrytskyi9e7482e2020-03-24 16:23:51 -0700634 return Ok();
Jeff Sharkey83b559c2017-09-12 16:30:52 -0600635}
636
637binder::Status VoldNativeService::initUser0() {
Nikita Ioffee1fd4892019-10-23 17:19:13 +0100638 ENFORCE_SYSTEM_OR_ROOT;
Jeff Sharkey83b559c2017-09-12 16:30:52 -0600639 ACQUIRE_CRYPT_LOCK;
640
Eric Biggersa701c452018-10-23 13:06:55 -0700641 return translateBool(fscrypt_init_user0());
Jeff Sharkey83b559c2017-09-12 16:30:52 -0600642}
643
644binder::Status VoldNativeService::isConvertibleToFbe(bool* _aidl_return) {
Nikita Ioffee1fd4892019-10-23 17:19:13 +0100645 ENFORCE_SYSTEM_OR_ROOT;
Jeff Sharkey83b559c2017-09-12 16:30:52 -0600646 ACQUIRE_CRYPT_LOCK;
647
648 *_aidl_return = cryptfs_isConvertibleToFBE() != 0;
Yurii Zubrytskyi9e7482e2020-03-24 16:23:51 -0700649 return Ok();
Jeff Sharkey83b559c2017-09-12 16:30:52 -0600650}
651
Paul Lawrence236e5e82019-06-25 14:44:33 -0700652binder::Status VoldNativeService::mountFstab(const std::string& blkDevice,
653 const std::string& mountPoint) {
Nikita Ioffee1fd4892019-10-23 17:19:13 +0100654 ENFORCE_SYSTEM_OR_ROOT;
Paul Crowley0fd26262018-01-30 09:48:19 -0800655 ACQUIRE_LOCK;
656
Paul Lawrence236e5e82019-06-25 14:44:33 -0700657 return translateBool(fscrypt_mount_metadata_encrypted(blkDevice, mountPoint, false));
Paul Crowley0fd26262018-01-30 09:48:19 -0800658}
659
Paul Lawrence236e5e82019-06-25 14:44:33 -0700660binder::Status VoldNativeService::encryptFstab(const std::string& blkDevice,
661 const std::string& mountPoint) {
Nikita Ioffee1fd4892019-10-23 17:19:13 +0100662 ENFORCE_SYSTEM_OR_ROOT;
Paul Crowley0fd26262018-01-30 09:48:19 -0800663 ACQUIRE_LOCK;
664
Paul Lawrence236e5e82019-06-25 14:44:33 -0700665 return translateBool(fscrypt_mount_metadata_encrypted(blkDevice, mountPoint, true));
Paul Crowley0fd26262018-01-30 09:48:19 -0800666}
667
Paul Crowleyedf7a4e2018-09-18 15:14:18 -0700668binder::Status VoldNativeService::createUserKey(int32_t userId, int32_t userSerial, bool ephemeral) {
Nikita Ioffee1fd4892019-10-23 17:19:13 +0100669 ENFORCE_SYSTEM_OR_ROOT;
Jeff Sharkey83b559c2017-09-12 16:30:52 -0600670 ACQUIRE_CRYPT_LOCK;
671
Eric Biggersa701c452018-10-23 13:06:55 -0700672 return translateBool(fscrypt_vold_create_user_key(userId, userSerial, ephemeral));
Jeff Sharkey83b559c2017-09-12 16:30:52 -0600673}
674
675binder::Status VoldNativeService::destroyUserKey(int32_t userId) {
Nikita Ioffee1fd4892019-10-23 17:19:13 +0100676 ENFORCE_SYSTEM_OR_ROOT;
Jeff Sharkey83b559c2017-09-12 16:30:52 -0600677 ACQUIRE_CRYPT_LOCK;
678
Eric Biggersa701c452018-10-23 13:06:55 -0700679 return translateBool(fscrypt_destroy_user_key(userId));
Jeff Sharkey83b559c2017-09-12 16:30:52 -0600680}
681
682binder::Status VoldNativeService::addUserKeyAuth(int32_t userId, int32_t userSerial,
Paul Crowleyedf7a4e2018-09-18 15:14:18 -0700683 const std::string& token,
684 const std::string& secret) {
Nikita Ioffee1fd4892019-10-23 17:19:13 +0100685 ENFORCE_SYSTEM_OR_ROOT;
Jeff Sharkey83b559c2017-09-12 16:30:52 -0600686 ACQUIRE_CRYPT_LOCK;
687
Eric Biggersa701c452018-10-23 13:06:55 -0700688 return translateBool(fscrypt_add_user_key_auth(userId, userSerial, token, secret));
Jeff Sharkey83b559c2017-09-12 16:30:52 -0600689}
690
Barani Muthukumaranb1927c22019-10-31 22:59:34 -0700691binder::Status VoldNativeService::clearUserKeyAuth(int32_t userId, int32_t userSerial,
692 const std::string& token,
693 const std::string& secret) {
694 ENFORCE_SYSTEM_OR_ROOT;
695 ACQUIRE_CRYPT_LOCK;
696
697 return translateBool(fscrypt_clear_user_key_auth(userId, userSerial, token, secret));
698}
699
Jeff Sharkey83b559c2017-09-12 16:30:52 -0600700binder::Status VoldNativeService::fixateNewestUserKeyAuth(int32_t userId) {
Nikita Ioffee1fd4892019-10-23 17:19:13 +0100701 ENFORCE_SYSTEM_OR_ROOT;
Jeff Sharkey83b559c2017-09-12 16:30:52 -0600702 ACQUIRE_CRYPT_LOCK;
703
Eric Biggersa701c452018-10-23 13:06:55 -0700704 return translateBool(fscrypt_fixate_newest_user_key_auth(userId));
Jeff Sharkey83b559c2017-09-12 16:30:52 -0600705}
706
707binder::Status VoldNativeService::unlockUserKey(int32_t userId, int32_t userSerial,
Paul Crowleyedf7a4e2018-09-18 15:14:18 -0700708 const std::string& token,
709 const std::string& secret) {
Nikita Ioffee1fd4892019-10-23 17:19:13 +0100710 ENFORCE_SYSTEM_OR_ROOT;
Jeff Sharkey83b559c2017-09-12 16:30:52 -0600711 ACQUIRE_CRYPT_LOCK;
712
Eric Biggersa701c452018-10-23 13:06:55 -0700713 return translateBool(fscrypt_unlock_user_key(userId, userSerial, token, secret));
Jeff Sharkey83b559c2017-09-12 16:30:52 -0600714}
715
716binder::Status VoldNativeService::lockUserKey(int32_t userId) {
Nikita Ioffee1fd4892019-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 return translateBool(fscrypt_lock_user_key(userId));
Jeff Sharkey83b559c2017-09-12 16:30:52 -0600721}
722
Jooyung Hand75a10a2020-01-23 13:23:26 +0900723binder::Status VoldNativeService::prepareUserStorage(const std::optional<std::string>& uuid,
Paul Crowleyedf7a4e2018-09-18 15:14:18 -0700724 int32_t userId, int32_t userSerial,
725 int32_t flags) {
Nikita Ioffee1fd4892019-10-23 17:19:13 +0100726 ENFORCE_SYSTEM_OR_ROOT;
Paul Crowley3b71fc52017-10-09 10:55:21 -0700727 std::string empty_string = "";
728 auto uuid_ = uuid ? *uuid : empty_string;
Paul Crowley06f762d2017-10-16 10:59:51 -0700729 CHECK_ARGUMENT_HEX(uuid_);
730
731 ACQUIRE_CRYPT_LOCK;
Eric Biggersa701c452018-10-23 13:06:55 -0700732 return translateBool(fscrypt_prepare_user_storage(uuid_, userId, userSerial, flags));
Jeff Sharkey83b559c2017-09-12 16:30:52 -0600733}
734
Jooyung Hand75a10a2020-01-23 13:23:26 +0900735binder::Status VoldNativeService::destroyUserStorage(const std::optional<std::string>& uuid,
Paul Crowleyedf7a4e2018-09-18 15:14:18 -0700736 int32_t userId, int32_t flags) {
Nikita Ioffee1fd4892019-10-23 17:19:13 +0100737 ENFORCE_SYSTEM_OR_ROOT;
Paul Crowley3b71fc52017-10-09 10:55:21 -0700738 std::string empty_string = "";
739 auto uuid_ = uuid ? *uuid : empty_string;
Paul Crowley06f762d2017-10-16 10:59:51 -0700740 CHECK_ARGUMENT_HEX(uuid_);
741
742 ACQUIRE_CRYPT_LOCK;
Eric Biggersa701c452018-10-23 13:06:55 -0700743 return translateBool(fscrypt_destroy_user_storage(uuid_, userId, flags));
Jeff Sharkey83b559c2017-09-12 16:30:52 -0600744}
745
Sudheer Shankafa6a1742018-10-04 16:26:22 -0700746binder::Status VoldNativeService::prepareSandboxForApp(const std::string& packageName,
747 int32_t appId, const std::string& sandboxId,
748 int32_t userId) {
Yurii Zubrytskyi9e7482e2020-03-24 16:23:51 -0700749 return Ok();
Sudheer Shankafa6a1742018-10-04 16:26:22 -0700750}
751
752binder::Status VoldNativeService::destroySandboxForApp(const std::string& packageName,
Sudheer Shanka69bc40f2018-10-25 11:06:55 -0700753 const std::string& sandboxId,
Sudheer Shankafa6a1742018-10-04 16:26:22 -0700754 int32_t userId) {
Yurii Zubrytskyi9e7482e2020-03-24 16:23:51 -0700755 return Ok();
Sudheer Shankac7562092018-08-24 10:20:56 -0700756}
757
Daniel Rosenberge334aab2018-10-10 18:52:04 -0700758binder::Status VoldNativeService::startCheckpoint(int32_t retry) {
Nikita Ioffee1fd4892019-10-23 17:19:13 +0100759 ENFORCE_SYSTEM_OR_ROOT;
Daniel Rosenberg4f684712018-08-28 01:58:49 -0700760 ACQUIRE_LOCK;
761
Daniel Rosenberge334aab2018-10-10 18:52:04 -0700762 return cp_startCheckpoint(retry);
Daniel Rosenberg4f684712018-08-28 01:58:49 -0700763}
764
Daniel Rosenbergd3992492018-10-02 17:40:44 -0700765binder::Status VoldNativeService::needsRollback(bool* _aidl_return) {
Nikita Ioffee1fd4892019-10-23 17:19:13 +0100766 ENFORCE_SYSTEM_OR_ROOT;
Daniel Rosenbergd3992492018-10-02 17:40:44 -0700767 ACQUIRE_LOCK;
768
769 *_aidl_return = cp_needsRollback();
Yurii Zubrytskyi9e7482e2020-03-24 16:23:51 -0700770 return Ok();
Daniel Rosenbergd3992492018-10-02 17:40:44 -0700771}
772
Daniel Rosenberg4f684712018-08-28 01:58:49 -0700773binder::Status VoldNativeService::needsCheckpoint(bool* _aidl_return) {
Nikita Ioffee1fd4892019-10-23 17:19:13 +0100774 ENFORCE_SYSTEM_OR_ROOT;
Daniel Rosenberg4f684712018-08-28 01:58:49 -0700775 ACQUIRE_LOCK;
776
777 *_aidl_return = cp_needsCheckpoint();
Yurii Zubrytskyi9e7482e2020-03-24 16:23:51 -0700778 return Ok();
Daniel Rosenberg4f684712018-08-28 01:58:49 -0700779}
780
Daniel Rosenberge334aab2018-10-10 18:52:04 -0700781binder::Status VoldNativeService::commitChanges() {
Nikita Ioffee1fd4892019-10-23 17:19:13 +0100782 ENFORCE_SYSTEM_OR_ROOT;
Daniel Rosenberg4f684712018-08-28 01:58:49 -0700783 ACQUIRE_LOCK;
784
Daniel Rosenberge334aab2018-10-10 18:52:04 -0700785 return cp_commitChanges();
Daniel Rosenberg4f684712018-08-28 01:58:49 -0700786}
787
Daniel Rosenberge334aab2018-10-10 18:52:04 -0700788binder::Status VoldNativeService::prepareCheckpoint() {
Nikita Ioffee1fd4892019-10-23 17:19:13 +0100789 ENFORCE_SYSTEM_OR_ROOT;
Daniel Rosenberg4f684712018-08-28 01:58:49 -0700790 ACQUIRE_LOCK;
791
Daniel Rosenberge334aab2018-10-10 18:52:04 -0700792 return cp_prepareCheckpoint();
Daniel Rosenberg4f684712018-08-28 01:58:49 -0700793}
794
Daniel Rosenberge334aab2018-10-10 18:52:04 -0700795binder::Status VoldNativeService::restoreCheckpoint(const std::string& mountPoint) {
Nikita Ioffee1fd4892019-10-23 17:19:13 +0100796 ENFORCE_SYSTEM_OR_ROOT;
Paul Lawrence1abb2fe2018-09-21 10:49:57 -0700797 CHECK_ARGUMENT_PATH(mountPoint);
798 ACQUIRE_LOCK;
799
Daniel Rosenberge334aab2018-10-10 18:52:04 -0700800 return cp_restoreCheckpoint(mountPoint);
Paul Lawrence1abb2fe2018-09-21 10:49:57 -0700801}
802
Daniel Rosenbergdda59812019-03-06 17:45:17 -0800803binder::Status VoldNativeService::restoreCheckpointPart(const std::string& mountPoint, int count) {
Nikita Ioffee1fd4892019-10-23 17:19:13 +0100804 ENFORCE_SYSTEM_OR_ROOT;
Daniel Rosenbergdda59812019-03-06 17:45:17 -0800805 CHECK_ARGUMENT_PATH(mountPoint);
806 ACQUIRE_LOCK;
807
808 return cp_restoreCheckpoint(mountPoint, count);
809}
810
Daniel Rosenberge334aab2018-10-10 18:52:04 -0700811binder::Status VoldNativeService::markBootAttempt() {
Nikita Ioffee1fd4892019-10-23 17:19:13 +0100812 ENFORCE_SYSTEM_OR_ROOT;
Daniel Rosenberg4f684712018-08-28 01:58:49 -0700813 ACQUIRE_LOCK;
814
Daniel Rosenberge334aab2018-10-10 18:52:04 -0700815 return cp_markBootAttempt();
Daniel Rosenberg4f684712018-08-28 01:58:49 -0700816}
817
Daniel Rosenberga59e4392019-03-20 17:02:47 -0700818binder::Status VoldNativeService::abortChanges(const std::string& message, bool retry) {
Nikita Ioffee1fd4892019-10-23 17:19:13 +0100819 ENFORCE_SYSTEM_OR_ROOT;
Daniel Rosenberg4f684712018-08-28 01:58:49 -0700820 ACQUIRE_LOCK;
821
Daniel Rosenberga59e4392019-03-20 17:02:47 -0700822 cp_abortChanges(message, retry);
Yurii Zubrytskyi9e7482e2020-03-24 16:23:51 -0700823 return Ok();
Daniel Rosenberg4f684712018-08-28 01:58:49 -0700824}
825
Daniel Rosenberg9b667fb2019-01-22 17:27:25 -0800826binder::Status VoldNativeService::supportsCheckpoint(bool* _aidl_return) {
Nikita Ioffee1fd4892019-10-23 17:19:13 +0100827 ENFORCE_SYSTEM_OR_ROOT;
Daniel Rosenberg9b667fb2019-01-22 17:27:25 -0800828 ACQUIRE_LOCK;
829
830 return cp_supportsCheckpoint(*_aidl_return);
831}
832
Paul Lawrencec5c79c52019-03-18 13:36:40 -0700833binder::Status VoldNativeService::supportsBlockCheckpoint(bool* _aidl_return) {
Nikita Ioffee1fd4892019-10-23 17:19:13 +0100834 ENFORCE_SYSTEM_OR_ROOT;
Paul Lawrencec5c79c52019-03-18 13:36:40 -0700835 ACQUIRE_LOCK;
836
837 return cp_supportsBlockCheckpoint(*_aidl_return);
838}
839
840binder::Status VoldNativeService::supportsFileCheckpoint(bool* _aidl_return) {
Nikita Ioffee1fd4892019-10-23 17:19:13 +0100841 ENFORCE_SYSTEM_OR_ROOT;
Paul Lawrencec5c79c52019-03-18 13:36:40 -0700842 ACQUIRE_LOCK;
843
844 return cp_supportsFileCheckpoint(*_aidl_return);
845}
846
Nikita Ioffea5798fc2019-10-11 16:38:21 +0100847binder::Status VoldNativeService::resetCheckpoint() {
Nikita Ioffee1fd4892019-10-23 17:19:13 +0100848 ENFORCE_SYSTEM_OR_ROOT;
Nikita Ioffea5798fc2019-10-11 16:38:21 +0100849 ACQUIRE_LOCK;
850
851 cp_resetCheckpoint();
Yurii Zubrytskyi9e7482e2020-03-24 16:23:51 -0700852 return Ok();
Nikita Ioffea5798fc2019-10-11 16:38:21 +0100853}
854
Yurii Zubrytskyid05bcc82020-01-10 11:54:06 -0800855binder::Status VoldNativeService::incFsEnabled(bool* _aidl_return) {
Yurii Zubrytskyi9e7482e2020-03-24 16:23:51 -0700856 ENFORCE_SYSTEM_OR_ROOT;
857
Yurii Zubrytskyid05bcc82020-01-10 11:54:06 -0800858 *_aidl_return = IncFs_IsEnabled();
Yurii Zubrytskyi9e7482e2020-03-24 16:23:51 -0700859 return Ok();
Songchun Fanab55cec2019-12-02 10:50:12 -0800860}
861
862binder::Status VoldNativeService::mountIncFs(
Yurii Zubrytskyid05bcc82020-01-10 11:54:06 -0800863 const std::string& backingPath, const std::string& targetDir, int32_t flags,
Songchun Fanab55cec2019-12-02 10:50:12 -0800864 ::android::os::incremental::IncrementalFileSystemControlParcel* _aidl_return) {
Yurii Zubrytskyi9e7482e2020-03-24 16:23:51 -0700865 ENFORCE_SYSTEM_OR_ROOT;
866 CHECK_ARGUMENT_PATH(backingPath);
867 CHECK_ARGUMENT_PATH(targetDir);
868
Yurii Zubrytskyid05bcc82020-01-10 11:54:06 -0800869 auto result = IncFs_Mount(backingPath.c_str(), targetDir.c_str(),
870 {.flags = IncFsMountFlags(flags),
871 .defaultReadTimeoutMs = INCFS_DEFAULT_READ_TIMEOUT_MS,
872 .readLogBufferPages = 4});
873 if (result.cmd < 0) {
874 return translate(result.cmd);
Songchun Fanab55cec2019-12-02 10:50:12 -0800875 }
Songchun Fanab55cec2019-12-02 10:50:12 -0800876 using unique_fd = ::android::base::unique_fd;
Yurii Zubrytskyid05bcc82020-01-10 11:54:06 -0800877 _aidl_return->cmd.emplace(unique_fd(result.cmd));
878 _aidl_return->pendingReads.emplace(unique_fd(result.pendingReads));
879 if (result.logs >= 0) {
880 _aidl_return->log.emplace(unique_fd(result.logs));
Songchun Fanab55cec2019-12-02 10:50:12 -0800881 }
Yurii Zubrytskyi9e7482e2020-03-24 16:23:51 -0700882 return Ok();
Songchun Fanab55cec2019-12-02 10:50:12 -0800883}
884
885binder::Status VoldNativeService::unmountIncFs(const std::string& dir) {
Yurii Zubrytskyi9e7482e2020-03-24 16:23:51 -0700886 ENFORCE_SYSTEM_OR_ROOT;
887 CHECK_ARGUMENT_PATH(dir);
888
Songchun Fanab55cec2019-12-02 10:50:12 -0800889 return translate(IncFs_Unmount(dir.c_str()));
890}
891
892binder::Status VoldNativeService::bindMount(const std::string& sourceDir,
893 const std::string& targetDir) {
Yurii Zubrytskyi9e7482e2020-03-24 16:23:51 -0700894 ENFORCE_SYSTEM_OR_ROOT;
895 CHECK_ARGUMENT_PATH(sourceDir);
896 CHECK_ARGUMENT_PATH(targetDir);
897
Songchun Fanab55cec2019-12-02 10:50:12 -0800898 return translate(IncFs_BindMount(sourceDir.c_str(), targetDir.c_str()));
899}
900
Jeff Sharkey068c6be2017-09-06 13:47:40 -0600901} // namespace vold
902} // namespace android