blob: 5a77a83ea65633f0b69af837fc36dd5b0a6fab04 [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 Zubrytskyi131365a2020-03-24 23:49:02 -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 Zubrytskyi131365a2020-03-24 23:49:02 -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 Ioffe5d85bf32019-10-23 17:19:13 +010079#define ENFORCE_SYSTEM_OR_ROOT \
80 { \
Yurii Zubrytskyi131365a2020-03-24 23:49:02 -070081 binder::Status status = CheckUidOrRoot(AID_SYSTEM); \
Nikita Ioffe5d85bf32019-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 Zubrytskyi131365a2020-03-24 23:49:02 -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 Zubrytskyi131365a2020-03-24 23:49:02 -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 Zubrytskyi131365a2020-03-24 23:49:02 -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 Zubrytskyi131365a2020-03-24 23:49:02 -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 Ioffe5d85bf32019-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 Zubrytskyi131365a2020-03-24 23:49:02 -0700152 return Ok();
Jeff Sharkey814e9d32017-09-13 11:49:44 -0600153}
154
Jeff Sharkeycbe69fc2017-09-15 16:50:28 -0600155binder::Status VoldNativeService::monitor() {
Nikita Ioffe5d85bf32019-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 Zubrytskyi131365a2020-03-24 23:49:02 -0700162 return Ok();
Jeff Sharkeycbe69fc2017-09-15 16:50:28 -0600163}
164
Jeff Sharkey068c6be2017-09-06 13:47:40 -0600165binder::Status VoldNativeService::reset() {
Nikita Ioffe5d85bf32019-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 Ioffe5d85bf32019-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 Ioffe5d85bf32019-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 Ioffe5d85bf32019-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 Ioffe5d85bf32019-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 Ioffe5d85bf32019-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 Zubrytskyi131365a2020-03-24 23:49:02 -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 Zubrytskyi131365a2020-03-24 23:49:02 -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 Ioffe5d85bf32019-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 Ioffe5d85bf32019-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 Ioffe5d85bf32019-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
Zim5048b4b2019-11-19 09:16:03 +0000256binder::Status VoldNativeService::mount(
257 const std::string& volId, int32_t mountFlags, int32_t mountUserId,
258 const android::sp<android::os::IVoldMountCallback>& callback) {
Nikita Ioffe5d85bf32019-10-23 17:19:13 +0100259 ENFORCE_SYSTEM_OR_ROOT;
Jeff Sharkeyec4fda22017-09-12 13:19:24 -0600260 CHECK_ARGUMENT_ID(volId);
Jeff Sharkey9462bdd2017-09-07 15:27:28 -0600261 ACQUIRE_LOCK;
262
263 auto vol = VolumeManager::Instance()->findVolume(volId);
264 if (vol == nullptr) {
265 return error("Failed to find volume " + volId);
266 }
267
268 vol->setMountFlags(mountFlags);
269 vol->setMountUserId(mountUserId);
270
Zim5048b4b2019-11-19 09:16:03 +0000271 vol->setMountCallback(callback);
Jeff Sharkey9462bdd2017-09-07 15:27:28 -0600272 int res = vol->mount();
Zim5048b4b2019-11-19 09:16:03 +0000273 vol->setMountCallback(nullptr);
274
Sudheer Shanka40ab6742018-09-18 13:07:45 -0700275 if (res != OK) {
276 return translate(res);
Jeff Sharkey9462bdd2017-09-07 15:27:28 -0600277 }
Zim3623a212019-07-19 16:46:53 +0100278
Sudheer Shanka40ab6742018-09-18 13:07:45 -0700279 if ((mountFlags & MOUNT_FLAG_PRIMARY) != 0) {
280 res = VolumeManager::Instance()->setPrimary(vol);
281 if (res != OK) {
282 return translate(res);
283 }
284 }
285 return translate(OK);
Jeff Sharkey9462bdd2017-09-07 15:27:28 -0600286}
287
288binder::Status VoldNativeService::unmount(const std::string& volId) {
Nikita Ioffe5d85bf32019-10-23 17:19:13 +0100289 ENFORCE_SYSTEM_OR_ROOT;
Jeff Sharkeyec4fda22017-09-12 13:19:24 -0600290 CHECK_ARGUMENT_ID(volId);
Jeff Sharkey9462bdd2017-09-07 15:27:28 -0600291 ACQUIRE_LOCK;
292
293 auto vol = VolumeManager::Instance()->findVolume(volId);
294 if (vol == nullptr) {
295 return error("Failed to find volume " + volId);
296 }
297 return translate(vol->unmount());
298}
299
300binder::Status VoldNativeService::format(const std::string& volId, const std::string& fsType) {
Nikita Ioffe5d85bf32019-10-23 17:19:13 +0100301 ENFORCE_SYSTEM_OR_ROOT;
Jeff Sharkeyec4fda22017-09-12 13:19:24 -0600302 CHECK_ARGUMENT_ID(volId);
Jeff Sharkey9462bdd2017-09-07 15:27:28 -0600303 ACQUIRE_LOCK;
304
305 auto vol = VolumeManager::Instance()->findVolume(volId);
306 if (vol == nullptr) {
307 return error("Failed to find volume " + volId);
308 }
309 return translate(vol->format(fsType));
310}
311
Jeff Sharkey2048a282017-06-15 09:59:43 -0600312static binder::Status pathForVolId(const std::string& volId, std::string* path) {
Jeff Sharkey52f7a912017-09-15 12:57:44 -0600313 if (volId == "private" || volId == "null") {
Jeff Sharkey2048a282017-06-15 09:59:43 -0600314 *path = "/data";
Jeff Sharkey52f7a912017-09-15 12:57:44 -0600315 } else {
316 auto vol = VolumeManager::Instance()->findVolume(volId);
317 if (vol == nullptr) {
318 return error("Failed to find volume " + volId);
319 }
320 if (vol->getType() != VolumeBase::Type::kPrivate) {
321 return error("Volume " + volId + " not private");
322 }
323 if (vol->getState() != VolumeBase::State::kMounted) {
324 return error("Volume " + volId + " not mounted");
325 }
Jeff Sharkey2048a282017-06-15 09:59:43 -0600326 *path = vol->getPath();
327 if (path->empty()) {
328 return error("Volume " + volId + " missing path");
329 }
Jeff Sharkey52f7a912017-09-15 12:57:44 -0600330 }
Yurii Zubrytskyi131365a2020-03-24 23:49:02 -0700331 return Ok();
Jeff Sharkey2048a282017-06-15 09:59:43 -0600332}
Jeff Sharkey52f7a912017-09-15 12:57:44 -0600333
Jeff Sharkey2048a282017-06-15 09:59:43 -0600334binder::Status VoldNativeService::benchmark(
335 const std::string& volId, const android::sp<android::os::IVoldTaskListener>& listener) {
Nikita Ioffe5d85bf32019-10-23 17:19:13 +0100336 ENFORCE_SYSTEM_OR_ROOT;
Jeff Sharkey2048a282017-06-15 09:59:43 -0600337 CHECK_ARGUMENT_ID(volId);
338 ACQUIRE_LOCK;
339
340 std::string path;
341 auto status = pathForVolId(volId, &path);
342 if (!status.isOk()) return status;
Jeff Sharkey52f7a912017-09-15 12:57:44 -0600343
Paul Crowleyedf7a4e2018-09-18 15:14:18 -0700344 std::thread([=]() { android::vold::Benchmark(path, listener); }).detach();
Yurii Zubrytskyi131365a2020-03-24 23:49:02 -0700345 return Ok();
Jeff Sharkey068c6be2017-09-06 13:47:40 -0600346}
347
Jeff Sharkey2048a282017-06-15 09:59:43 -0600348binder::Status VoldNativeService::checkEncryption(const std::string& volId) {
Nikita Ioffe5d85bf32019-10-23 17:19:13 +0100349 ENFORCE_SYSTEM_OR_ROOT;
Jeff Sharkey2048a282017-06-15 09:59:43 -0600350 CHECK_ARGUMENT_ID(volId);
351 ACQUIRE_LOCK;
352
353 std::string path;
354 auto status = pathForVolId(volId, &path);
355 if (!status.isOk()) return status;
356 return translate(android::vold::CheckEncryption(path));
357}
358
Paul Crowleyedf7a4e2018-09-18 15:14:18 -0700359binder::Status VoldNativeService::moveStorage(
360 const std::string& fromVolId, const std::string& toVolId,
361 const android::sp<android::os::IVoldTaskListener>& listener) {
Nikita Ioffe5d85bf32019-10-23 17:19:13 +0100362 ENFORCE_SYSTEM_OR_ROOT;
Jeff Sharkeyec4fda22017-09-12 13:19:24 -0600363 CHECK_ARGUMENT_ID(fromVolId);
364 CHECK_ARGUMENT_ID(toVolId);
Jeff Sharkey9462bdd2017-09-07 15:27:28 -0600365 ACQUIRE_LOCK;
366
367 auto fromVol = VolumeManager::Instance()->findVolume(fromVolId);
368 auto toVol = VolumeManager::Instance()->findVolume(toVolId);
369 if (fromVol == nullptr) {
370 return error("Failed to find volume " + fromVolId);
371 } else if (toVol == nullptr) {
372 return error("Failed to find volume " + toVolId);
373 }
Jeff Sharkey01a0e7f2017-10-17 16:06:32 -0600374
Paul Crowleyedf7a4e2018-09-18 15:14:18 -0700375 std::thread([=]() { android::vold::MoveStorage(fromVol, toVol, listener); }).detach();
Yurii Zubrytskyi131365a2020-03-24 23:49:02 -0700376 return Ok();
Jeff Sharkey9462bdd2017-09-07 15:27:28 -0600377}
378
379binder::Status VoldNativeService::remountUid(int32_t uid, int32_t remountMode) {
Nikita Ioffe5d85bf32019-10-23 17:19:13 +0100380 ENFORCE_SYSTEM_OR_ROOT;
Jeff Sharkey9462bdd2017-09-07 15:27:28 -0600381 ACQUIRE_LOCK;
382
Sudheer Shanka817b9112018-12-13 17:40:28 -0800383 return translate(VolumeManager::Instance()->remountUid(uid, remountMode));
Jeff Sharkey9462bdd2017-09-07 15:27:28 -0600384}
385
Ricky Wai6b122572020-02-28 16:30:47 +0000386binder::Status VoldNativeService::remountAppStorageDirs(int uid, int pid,
387 const std::vector<std::string>& packageNames) {
388 ENFORCE_SYSTEM_OR_ROOT;
389 ACQUIRE_LOCK;
390
391 return translate(VolumeManager::Instance()->remountAppStorageDirs(uid, pid, packageNames));
392}
393
Martijn Coenen8a68a072020-02-12 15:29:02 +0100394binder::Status VoldNativeService::setupAppDir(const std::string& path, int32_t appUid) {
Nikita Ioffe5d85bf32019-10-23 17:19:13 +0100395 ENFORCE_SYSTEM_OR_ROOT;
Jeff Sharkeyec4fda22017-09-12 13:19:24 -0600396 CHECK_ARGUMENT_PATH(path);
Jeff Sharkey9462bdd2017-09-07 15:27:28 -0600397 ACQUIRE_LOCK;
398
Martijn Coenen8a68a072020-02-12 15:29:02 +0100399 return translate(VolumeManager::Instance()->setupAppDir(path, appUid));
Jeff Sharkey9462bdd2017-09-07 15:27:28 -0600400}
401
Martijn Coenen816f4d92020-02-18 15:06:37 +0100402binder::Status VoldNativeService::fixupAppDir(const std::string& path, int32_t appUid) {
403 ENFORCE_SYSTEM_OR_ROOT;
404 CHECK_ARGUMENT_PATH(path);
405 ACQUIRE_LOCK;
406
407 return translate(VolumeManager::Instance()->fixupAppDir(path, appUid));
408}
409
Jeff Sharkey11c2d382017-09-11 10:32:01 -0600410binder::Status VoldNativeService::createObb(const std::string& sourcePath,
Paul Crowleyedf7a4e2018-09-18 15:14:18 -0700411 const std::string& sourceKey, int32_t ownerGid,
412 std::string* _aidl_return) {
Nikita Ioffe5d85bf32019-10-23 17:19:13 +0100413 ENFORCE_SYSTEM_OR_ROOT;
Jeff Sharkeyec4fda22017-09-12 13:19:24 -0600414 CHECK_ARGUMENT_PATH(sourcePath);
415 CHECK_ARGUMENT_HEX(sourceKey);
Jeff Sharkey11c2d382017-09-11 10:32:01 -0600416 ACQUIRE_LOCK;
417
418 return translate(
Paul Crowleyedf7a4e2018-09-18 15:14:18 -0700419 VolumeManager::Instance()->createObb(sourcePath, sourceKey, ownerGid, _aidl_return));
Jeff Sharkey11c2d382017-09-11 10:32:01 -0600420}
421
422binder::Status VoldNativeService::destroyObb(const std::string& volId) {
Nikita Ioffe5d85bf32019-10-23 17:19:13 +0100423 ENFORCE_SYSTEM_OR_ROOT;
Jeff Sharkeyec4fda22017-09-12 13:19:24 -0600424 CHECK_ARGUMENT_ID(volId);
Jeff Sharkey11c2d382017-09-11 10:32:01 -0600425 ACQUIRE_LOCK;
426
427 return translate(VolumeManager::Instance()->destroyObb(volId));
428}
429
Risan82e90de2020-02-04 16:07:21 +0900430binder::Status VoldNativeService::createStubVolume(const std::string& sourcePath,
431 const std::string& mountPath,
432 const std::string& fsType,
433 const std::string& fsUuid,
434 const std::string& fsLabel, int32_t flags,
435 std::string* _aidl_return) {
Nikita Ioffe5d85bf32019-10-23 17:19:13 +0100436 ENFORCE_SYSTEM_OR_ROOT;
Risan8c9f3322018-10-29 08:52:56 +0900437 CHECK_ARGUMENT_PATH(sourcePath);
438 CHECK_ARGUMENT_PATH(mountPath);
439 CHECK_ARGUMENT_HEX(fsUuid);
440 // Label limitation seems to be different between fs (including allowed characters), so checking
441 // is quite meaningless.
442 ACQUIRE_LOCK;
443
Risan82e90de2020-02-04 16:07:21 +0900444 return translate(VolumeManager::Instance()->createStubVolume(
445 sourcePath, mountPath, fsType, fsUuid, fsLabel, flags, _aidl_return));
Risan8c9f3322018-10-29 08:52:56 +0900446}
447
448binder::Status VoldNativeService::destroyStubVolume(const std::string& volId) {
Nikita Ioffe5d85bf32019-10-23 17:19:13 +0100449 ENFORCE_SYSTEM_OR_ROOT;
Risan8c9f3322018-10-29 08:52:56 +0900450 CHECK_ARGUMENT_ID(volId);
451 ACQUIRE_LOCK;
452
453 return translate(VolumeManager::Instance()->destroyStubVolume(volId));
454}
455
Paul Crowleyedf7a4e2018-09-18 15:14:18 -0700456binder::Status VoldNativeService::fstrim(
457 int32_t fstrimFlags, const android::sp<android::os::IVoldTaskListener>& listener) {
Nikita Ioffe5d85bf32019-10-23 17:19:13 +0100458 ENFORCE_SYSTEM_OR_ROOT;
Jeff Sharkey11c2d382017-09-11 10:32:01 -0600459 ACQUIRE_LOCK;
460
Paul Crowleyedf7a4e2018-09-18 15:14:18 -0700461 std::thread([=]() { android::vold::Trim(listener); }).detach();
Yurii Zubrytskyi131365a2020-03-24 23:49:02 -0700462 return Ok();
Jeff Sharkey11c2d382017-09-11 10:32:01 -0600463}
464
Jin Qiana370c142017-10-17 15:41:45 -0700465binder::Status VoldNativeService::runIdleMaint(
Paul Crowleyedf7a4e2018-09-18 15:14:18 -0700466 const android::sp<android::os::IVoldTaskListener>& listener) {
Nikita Ioffe5d85bf32019-10-23 17:19:13 +0100467 ENFORCE_SYSTEM_OR_ROOT;
Jin Qiana370c142017-10-17 15:41:45 -0700468 ACQUIRE_LOCK;
469
Paul Crowleyedf7a4e2018-09-18 15:14:18 -0700470 std::thread([=]() { android::vold::RunIdleMaint(listener); }).detach();
Yurii Zubrytskyi131365a2020-03-24 23:49:02 -0700471 return Ok();
Jin Qiana370c142017-10-17 15:41:45 -0700472}
473
474binder::Status VoldNativeService::abortIdleMaint(
Paul Crowleyedf7a4e2018-09-18 15:14:18 -0700475 const android::sp<android::os::IVoldTaskListener>& listener) {
Nikita Ioffe5d85bf32019-10-23 17:19:13 +0100476 ENFORCE_SYSTEM_OR_ROOT;
Jin Qiana370c142017-10-17 15:41:45 -0700477 ACQUIRE_LOCK;
478
Paul Crowleyedf7a4e2018-09-18 15:14:18 -0700479 std::thread([=]() { android::vold::AbortIdleMaint(listener); }).detach();
Yurii Zubrytskyi131365a2020-03-24 23:49:02 -0700480 return Ok();
Jin Qiana370c142017-10-17 15:41:45 -0700481}
482
Risan8f6198d2018-10-26 20:56:45 -0600483binder::Status VoldNativeService::mountAppFuse(int32_t uid, int32_t mountId,
Paul Crowleyedf7a4e2018-09-18 15:14:18 -0700484 android::base::unique_fd* _aidl_return) {
Nikita Ioffe5d85bf32019-10-23 17:19:13 +0100485 ENFORCE_SYSTEM_OR_ROOT;
Jeff Sharkey11c2d382017-09-11 10:32:01 -0600486 ACQUIRE_LOCK;
487
Risan8f6198d2018-10-26 20:56:45 -0600488 return translate(VolumeManager::Instance()->mountAppFuse(uid, mountId, _aidl_return));
Jeff Sharkey11c2d382017-09-11 10:32:01 -0600489}
490
Risan8f6198d2018-10-26 20:56:45 -0600491binder::Status VoldNativeService::unmountAppFuse(int32_t uid, int32_t mountId) {
Nikita Ioffe5d85bf32019-10-23 17:19:13 +0100492 ENFORCE_SYSTEM_OR_ROOT;
Jeff Sharkey11c2d382017-09-11 10:32:01 -0600493 ACQUIRE_LOCK;
494
Risan8f6198d2018-10-26 20:56:45 -0600495 return translate(VolumeManager::Instance()->unmountAppFuse(uid, mountId));
496}
497
498binder::Status VoldNativeService::openAppFuseFile(int32_t uid, int32_t mountId, int32_t fileId,
499 int32_t flags,
500 android::base::unique_fd* _aidl_return) {
Nikita Ioffe5d85bf32019-10-23 17:19:13 +0100501 ENFORCE_SYSTEM_OR_ROOT;
Risan8f6198d2018-10-26 20:56:45 -0600502 ACQUIRE_LOCK;
503
504 int fd = VolumeManager::Instance()->openAppFuseFile(uid, mountId, fileId, flags);
505 if (fd == -1) {
506 return error("Failed to open AppFuse file for uid: " + std::to_string(uid) +
507 " mountId: " + std::to_string(mountId) + " fileId: " + std::to_string(fileId) +
508 " flags: " + std::to_string(flags));
509 }
510
511 *_aidl_return = android::base::unique_fd(fd);
Yurii Zubrytskyi131365a2020-03-24 23:49:02 -0700512 return Ok();
Jeff Sharkey11c2d382017-09-11 10:32:01 -0600513}
514
Jeff Sharkey83b559c2017-09-12 16:30:52 -0600515binder::Status VoldNativeService::fdeCheckPassword(const std::string& password) {
Nikita Ioffe5d85bf32019-10-23 17:19:13 +0100516 ENFORCE_SYSTEM_OR_ROOT;
Jeff Sharkey83b559c2017-09-12 16:30:52 -0600517 ACQUIRE_CRYPT_LOCK;
518
519 return translate(cryptfs_check_passwd(password.c_str()));
520}
521
522binder::Status VoldNativeService::fdeRestart() {
Nikita Ioffe5d85bf32019-10-23 17:19:13 +0100523 ENFORCE_SYSTEM_OR_ROOT;
Jeff Sharkey83b559c2017-09-12 16:30:52 -0600524 ACQUIRE_CRYPT_LOCK;
525
526 // Spawn as thread so init can issue commands back to vold without
527 // causing deadlock, usually as a result of prep_data_fs.
528 std::thread(&cryptfs_restart).detach();
Yurii Zubrytskyi131365a2020-03-24 23:49:02 -0700529 return Ok();
Jeff Sharkey83b559c2017-09-12 16:30:52 -0600530}
531
532binder::Status VoldNativeService::fdeComplete(int32_t* _aidl_return) {
Nikita Ioffe5d85bf32019-10-23 17:19:13 +0100533 ENFORCE_SYSTEM_OR_ROOT;
Jeff Sharkey83b559c2017-09-12 16:30:52 -0600534 ACQUIRE_CRYPT_LOCK;
535
536 *_aidl_return = cryptfs_crypto_complete();
Yurii Zubrytskyi131365a2020-03-24 23:49:02 -0700537 return Ok();
Jeff Sharkey83b559c2017-09-12 16:30:52 -0600538}
539
540static int fdeEnableInternal(int32_t passwordType, const std::string& password,
Paul Crowleyedf7a4e2018-09-18 15:14:18 -0700541 int32_t encryptionFlags) {
Jeff Sharkey83b559c2017-09-12 16:30:52 -0600542 bool noUi = (encryptionFlags & VoldNativeService::ENCRYPTION_FLAG_NO_UI) != 0;
543
Jeff Sharkey83b559c2017-09-12 16:30:52 -0600544 for (int tries = 0; tries < 2; ++tries) {
545 int rc;
546 if (passwordType == VoldNativeService::PASSWORD_TYPE_DEFAULT) {
Paul Lawrence7ee87cf2017-12-22 10:12:06 -0800547 rc = cryptfs_enable_default(noUi);
Jeff Sharkey83b559c2017-09-12 16:30:52 -0600548 } else {
Paul Lawrence7ee87cf2017-12-22 10:12:06 -0800549 rc = cryptfs_enable(passwordType, password.c_str(), noUi);
Jeff Sharkey83b559c2017-09-12 16:30:52 -0600550 }
551
552 if (rc == 0) {
553 return 0;
554 } else if (tries == 0) {
Jeff Sharkey3472e522017-10-06 18:02:53 -0600555 KillProcessesWithOpenFiles(DATA_MNT_POINT, SIGKILL);
Jeff Sharkey83b559c2017-09-12 16:30:52 -0600556 }
557 }
558
559 return -1;
560}
561
Paul Crowleyedf7a4e2018-09-18 15:14:18 -0700562binder::Status VoldNativeService::fdeEnable(int32_t passwordType, const std::string& password,
563 int32_t encryptionFlags) {
Nikita Ioffe5d85bf32019-10-23 17:19:13 +0100564 ENFORCE_SYSTEM_OR_ROOT;
Jeff Sharkey83b559c2017-09-12 16:30:52 -0600565 ACQUIRE_CRYPT_LOCK;
566
Paul Crowley0fd26262018-01-30 09:48:19 -0800567 LOG(DEBUG) << "fdeEnable(" << passwordType << ", *, " << encryptionFlags << ")";
Eric Biggersa701c452018-10-23 13:06:55 -0700568 if (fscrypt_is_native()) {
569 LOG(ERROR) << "fscrypt_is_native, fdeEnable invalid";
570 return error("fscrypt_is_native, fdeEnable invalid");
Jeff Sharkey83b559c2017-09-12 16:30:52 -0600571 }
Eric Biggersa701c452018-10-23 13:06:55 -0700572 LOG(DEBUG) << "!fscrypt_is_native, spawning fdeEnableInternal";
Jeff Sharkey83b559c2017-09-12 16:30:52 -0600573
574 // Spawn as thread so init can issue commands back to vold without
575 // causing deadlock, usually as a result of prep_data_fs.
576 std::thread(&fdeEnableInternal, passwordType, password, encryptionFlags).detach();
Yurii Zubrytskyi131365a2020-03-24 23:49:02 -0700577 return Ok();
Jeff Sharkey83b559c2017-09-12 16:30:52 -0600578}
579
580binder::Status VoldNativeService::fdeChangePassword(int32_t passwordType,
Paul Crowleyedf7a4e2018-09-18 15:14:18 -0700581 const std::string& password) {
Nikita Ioffe5d85bf32019-10-23 17:19:13 +0100582 ENFORCE_SYSTEM_OR_ROOT;
Jeff Sharkey83b559c2017-09-12 16:30:52 -0600583 ACQUIRE_CRYPT_LOCK;
584
585 return translate(cryptfs_changepw(passwordType, password.c_str()));
586}
587
588binder::Status VoldNativeService::fdeVerifyPassword(const std::string& password) {
Nikita Ioffe5d85bf32019-10-23 17:19:13 +0100589 ENFORCE_SYSTEM_OR_ROOT;
Jeff Sharkey83b559c2017-09-12 16:30:52 -0600590 ACQUIRE_CRYPT_LOCK;
591
592 return translate(cryptfs_verify_passwd(password.c_str()));
593}
594
Paul Crowleyedf7a4e2018-09-18 15:14:18 -0700595binder::Status VoldNativeService::fdeGetField(const std::string& key, std::string* _aidl_return) {
Nikita Ioffe5d85bf32019-10-23 17:19:13 +0100596 ENFORCE_SYSTEM_OR_ROOT;
Jeff Sharkey83b559c2017-09-12 16:30:52 -0600597 ACQUIRE_CRYPT_LOCK;
598
599 char buf[PROPERTY_VALUE_MAX];
600 if (cryptfs_getfield(key.c_str(), buf, sizeof(buf)) != CRYPTO_GETFIELD_OK) {
601 return error(StringPrintf("Failed to read field %s", key.c_str()));
602 } else {
603 *_aidl_return = buf;
Yurii Zubrytskyi131365a2020-03-24 23:49:02 -0700604 return Ok();
Jeff Sharkey83b559c2017-09-12 16:30:52 -0600605 }
606}
607
Paul Crowleyedf7a4e2018-09-18 15:14:18 -0700608binder::Status VoldNativeService::fdeSetField(const std::string& key, const std::string& value) {
Nikita Ioffe5d85bf32019-10-23 17:19:13 +0100609 ENFORCE_SYSTEM_OR_ROOT;
Jeff Sharkey83b559c2017-09-12 16:30:52 -0600610 ACQUIRE_CRYPT_LOCK;
611
612 return translate(cryptfs_setfield(key.c_str(), value.c_str()));
613}
614
615binder::Status VoldNativeService::fdeGetPasswordType(int32_t* _aidl_return) {
Nikita Ioffe5d85bf32019-10-23 17:19:13 +0100616 ENFORCE_SYSTEM_OR_ROOT;
Jeff Sharkey83b559c2017-09-12 16:30:52 -0600617 ACQUIRE_CRYPT_LOCK;
618
619 *_aidl_return = cryptfs_get_password_type();
Yurii Zubrytskyi131365a2020-03-24 23:49:02 -0700620 return Ok();
Jeff Sharkey83b559c2017-09-12 16:30:52 -0600621}
622
623binder::Status VoldNativeService::fdeGetPassword(std::string* _aidl_return) {
Nikita Ioffe5d85bf32019-10-23 17:19:13 +0100624 ENFORCE_SYSTEM_OR_ROOT;
Jeff Sharkey83b559c2017-09-12 16:30:52 -0600625 ACQUIRE_CRYPT_LOCK;
626
627 const char* res = cryptfs_get_password();
628 if (res != nullptr) {
629 *_aidl_return = res;
630 }
Yurii Zubrytskyi131365a2020-03-24 23:49:02 -0700631 return Ok();
Jeff Sharkey83b559c2017-09-12 16:30:52 -0600632}
633
634binder::Status VoldNativeService::fdeClearPassword() {
Nikita Ioffe5d85bf32019-10-23 17:19:13 +0100635 ENFORCE_SYSTEM_OR_ROOT;
Jeff Sharkey83b559c2017-09-12 16:30:52 -0600636 ACQUIRE_CRYPT_LOCK;
637
638 cryptfs_clear_password();
Yurii Zubrytskyi131365a2020-03-24 23:49:02 -0700639 return Ok();
Jeff Sharkey83b559c2017-09-12 16:30:52 -0600640}
641
642binder::Status VoldNativeService::fbeEnable() {
Nikita Ioffe5d85bf32019-10-23 17:19:13 +0100643 ENFORCE_SYSTEM_OR_ROOT;
Jeff Sharkey83b559c2017-09-12 16:30:52 -0600644 ACQUIRE_CRYPT_LOCK;
645
Paul Crowleyc8a3ef32019-09-11 15:00:08 -0700646 return translateBool(fscrypt_initialize_systemwide_keys());
Jeff Sharkey83b559c2017-09-12 16:30:52 -0600647}
648
649binder::Status VoldNativeService::mountDefaultEncrypted() {
Nikita Ioffe5d85bf32019-10-23 17:19:13 +0100650 ENFORCE_SYSTEM_OR_ROOT;
Jeff Sharkey83b559c2017-09-12 16:30:52 -0600651 ACQUIRE_CRYPT_LOCK;
652
Eric Biggersa701c452018-10-23 13:06:55 -0700653 if (!fscrypt_is_native()) {
Jeff Sharkey83b559c2017-09-12 16:30:52 -0600654 // Spawn as thread so init can issue commands back to vold without
655 // causing deadlock, usually as a result of prep_data_fs.
656 std::thread(&cryptfs_mount_default_encrypted).detach();
Jeff Sharkey83b559c2017-09-12 16:30:52 -0600657 }
Yurii Zubrytskyi131365a2020-03-24 23:49:02 -0700658 return Ok();
Jeff Sharkey83b559c2017-09-12 16:30:52 -0600659}
660
661binder::Status VoldNativeService::initUser0() {
Nikita Ioffe5d85bf32019-10-23 17:19:13 +0100662 ENFORCE_SYSTEM_OR_ROOT;
Jeff Sharkey83b559c2017-09-12 16:30:52 -0600663 ACQUIRE_CRYPT_LOCK;
664
Eric Biggersa701c452018-10-23 13:06:55 -0700665 return translateBool(fscrypt_init_user0());
Jeff Sharkey83b559c2017-09-12 16:30:52 -0600666}
667
668binder::Status VoldNativeService::isConvertibleToFbe(bool* _aidl_return) {
Nikita Ioffe5d85bf32019-10-23 17:19:13 +0100669 ENFORCE_SYSTEM_OR_ROOT;
Jeff Sharkey83b559c2017-09-12 16:30:52 -0600670 ACQUIRE_CRYPT_LOCK;
671
672 *_aidl_return = cryptfs_isConvertibleToFBE() != 0;
Yurii Zubrytskyi131365a2020-03-24 23:49:02 -0700673 return Ok();
Jeff Sharkey83b559c2017-09-12 16:30:52 -0600674}
675
Paul Lawrence236e5e82019-06-25 14:44:33 -0700676binder::Status VoldNativeService::mountFstab(const std::string& blkDevice,
677 const std::string& mountPoint) {
Nikita Ioffe5d85bf32019-10-23 17:19:13 +0100678 ENFORCE_SYSTEM_OR_ROOT;
Paul Crowley0fd26262018-01-30 09:48:19 -0800679 ACQUIRE_LOCK;
680
Paul Lawrence236e5e82019-06-25 14:44:33 -0700681 return translateBool(fscrypt_mount_metadata_encrypted(blkDevice, mountPoint, false));
Paul Crowley0fd26262018-01-30 09:48:19 -0800682}
683
Paul Lawrence236e5e82019-06-25 14:44:33 -0700684binder::Status VoldNativeService::encryptFstab(const std::string& blkDevice,
685 const std::string& mountPoint) {
Nikita Ioffe5d85bf32019-10-23 17:19:13 +0100686 ENFORCE_SYSTEM_OR_ROOT;
Paul Crowley0fd26262018-01-30 09:48:19 -0800687 ACQUIRE_LOCK;
688
Paul Lawrence236e5e82019-06-25 14:44:33 -0700689 return translateBool(fscrypt_mount_metadata_encrypted(blkDevice, mountPoint, true));
Paul Crowley0fd26262018-01-30 09:48:19 -0800690}
691
Paul Crowleyedf7a4e2018-09-18 15:14:18 -0700692binder::Status VoldNativeService::createUserKey(int32_t userId, int32_t userSerial, bool ephemeral) {
Nikita Ioffe5d85bf32019-10-23 17:19:13 +0100693 ENFORCE_SYSTEM_OR_ROOT;
Jeff Sharkey83b559c2017-09-12 16:30:52 -0600694 ACQUIRE_CRYPT_LOCK;
695
Eric Biggersa701c452018-10-23 13:06:55 -0700696 return translateBool(fscrypt_vold_create_user_key(userId, userSerial, ephemeral));
Jeff Sharkey83b559c2017-09-12 16:30:52 -0600697}
698
699binder::Status VoldNativeService::destroyUserKey(int32_t userId) {
Nikita Ioffe5d85bf32019-10-23 17:19:13 +0100700 ENFORCE_SYSTEM_OR_ROOT;
Jeff Sharkey83b559c2017-09-12 16:30:52 -0600701 ACQUIRE_CRYPT_LOCK;
702
Eric Biggersa701c452018-10-23 13:06:55 -0700703 return translateBool(fscrypt_destroy_user_key(userId));
Jeff Sharkey83b559c2017-09-12 16:30:52 -0600704}
705
706binder::Status VoldNativeService::addUserKeyAuth(int32_t userId, int32_t userSerial,
Paul Crowleyedf7a4e2018-09-18 15:14:18 -0700707 const std::string& token,
708 const std::string& secret) {
Nikita Ioffe5d85bf32019-10-23 17:19:13 +0100709 ENFORCE_SYSTEM_OR_ROOT;
Jeff Sharkey83b559c2017-09-12 16:30:52 -0600710 ACQUIRE_CRYPT_LOCK;
711
Eric Biggersa701c452018-10-23 13:06:55 -0700712 return translateBool(fscrypt_add_user_key_auth(userId, userSerial, token, secret));
Jeff Sharkey83b559c2017-09-12 16:30:52 -0600713}
714
Barani Muthukumaran9ad51ad2019-10-31 22:59:34 -0700715binder::Status VoldNativeService::clearUserKeyAuth(int32_t userId, int32_t userSerial,
716 const std::string& token,
717 const std::string& secret) {
718 ENFORCE_SYSTEM_OR_ROOT;
719 ACQUIRE_CRYPT_LOCK;
720
721 return translateBool(fscrypt_clear_user_key_auth(userId, userSerial, token, secret));
722}
723
Jeff Sharkey83b559c2017-09-12 16:30:52 -0600724binder::Status VoldNativeService::fixateNewestUserKeyAuth(int32_t userId) {
Nikita Ioffe5d85bf32019-10-23 17:19:13 +0100725 ENFORCE_SYSTEM_OR_ROOT;
Jeff Sharkey83b559c2017-09-12 16:30:52 -0600726 ACQUIRE_CRYPT_LOCK;
727
Eric Biggersa701c452018-10-23 13:06:55 -0700728 return translateBool(fscrypt_fixate_newest_user_key_auth(userId));
Jeff Sharkey83b559c2017-09-12 16:30:52 -0600729}
730
731binder::Status VoldNativeService::unlockUserKey(int32_t userId, int32_t userSerial,
Paul Crowleyedf7a4e2018-09-18 15:14:18 -0700732 const std::string& token,
733 const std::string& secret) {
Nikita Ioffe5d85bf32019-10-23 17:19:13 +0100734 ENFORCE_SYSTEM_OR_ROOT;
Jeff Sharkey83b559c2017-09-12 16:30:52 -0600735 ACQUIRE_CRYPT_LOCK;
736
Eric Biggersa701c452018-10-23 13:06:55 -0700737 return translateBool(fscrypt_unlock_user_key(userId, userSerial, token, secret));
Jeff Sharkey83b559c2017-09-12 16:30:52 -0600738}
739
740binder::Status VoldNativeService::lockUserKey(int32_t userId) {
Nikita Ioffe5d85bf32019-10-23 17:19:13 +0100741 ENFORCE_SYSTEM_OR_ROOT;
Jeff Sharkey83b559c2017-09-12 16:30:52 -0600742 ACQUIRE_CRYPT_LOCK;
743
Eric Biggersa701c452018-10-23 13:06:55 -0700744 return translateBool(fscrypt_lock_user_key(userId));
Jeff Sharkey83b559c2017-09-12 16:30:52 -0600745}
746
747binder::Status VoldNativeService::prepareUserStorage(const std::unique_ptr<std::string>& uuid,
Paul Crowleyedf7a4e2018-09-18 15:14:18 -0700748 int32_t userId, int32_t userSerial,
749 int32_t flags) {
Nikita Ioffe5d85bf32019-10-23 17:19:13 +0100750 ENFORCE_SYSTEM_OR_ROOT;
Paul Crowley3b71fc52017-10-09 10:55:21 -0700751 std::string empty_string = "";
752 auto uuid_ = uuid ? *uuid : empty_string;
Paul Crowley06f762d2017-10-16 10:59:51 -0700753 CHECK_ARGUMENT_HEX(uuid_);
754
755 ACQUIRE_CRYPT_LOCK;
Eric Biggersa701c452018-10-23 13:06:55 -0700756 return translateBool(fscrypt_prepare_user_storage(uuid_, userId, userSerial, flags));
Jeff Sharkey83b559c2017-09-12 16:30:52 -0600757}
758
759binder::Status VoldNativeService::destroyUserStorage(const std::unique_ptr<std::string>& uuid,
Paul Crowleyedf7a4e2018-09-18 15:14:18 -0700760 int32_t userId, int32_t flags) {
Nikita Ioffe5d85bf32019-10-23 17:19:13 +0100761 ENFORCE_SYSTEM_OR_ROOT;
Paul Crowley3b71fc52017-10-09 10:55:21 -0700762 std::string empty_string = "";
763 auto uuid_ = uuid ? *uuid : empty_string;
Paul Crowley06f762d2017-10-16 10:59:51 -0700764 CHECK_ARGUMENT_HEX(uuid_);
765
766 ACQUIRE_CRYPT_LOCK;
Eric Biggersa701c452018-10-23 13:06:55 -0700767 return translateBool(fscrypt_destroy_user_storage(uuid_, userId, flags));
Jeff Sharkey83b559c2017-09-12 16:30:52 -0600768}
769
Sudheer Shankafa6a1742018-10-04 16:26:22 -0700770binder::Status VoldNativeService::prepareSandboxForApp(const std::string& packageName,
771 int32_t appId, const std::string& sandboxId,
772 int32_t userId) {
Yurii Zubrytskyi131365a2020-03-24 23:49:02 -0700773 return Ok();
Sudheer Shankafa6a1742018-10-04 16:26:22 -0700774}
775
776binder::Status VoldNativeService::destroySandboxForApp(const std::string& packageName,
Sudheer Shanka69bc40f2018-10-25 11:06:55 -0700777 const std::string& sandboxId,
Sudheer Shankafa6a1742018-10-04 16:26:22 -0700778 int32_t userId) {
Yurii Zubrytskyi131365a2020-03-24 23:49:02 -0700779 return Ok();
Sudheer Shankac7562092018-08-24 10:20:56 -0700780}
781
Daniel Rosenberge334aab2018-10-10 18:52:04 -0700782binder::Status VoldNativeService::startCheckpoint(int32_t retry) {
Nikita Ioffe5d85bf32019-10-23 17:19:13 +0100783 ENFORCE_SYSTEM_OR_ROOT;
Daniel Rosenberg4f684712018-08-28 01:58:49 -0700784 ACQUIRE_LOCK;
785
Daniel Rosenberge334aab2018-10-10 18:52:04 -0700786 return cp_startCheckpoint(retry);
Daniel Rosenberg4f684712018-08-28 01:58:49 -0700787}
788
Daniel Rosenbergd3992492018-10-02 17:40:44 -0700789binder::Status VoldNativeService::needsRollback(bool* _aidl_return) {
Nikita Ioffe5d85bf32019-10-23 17:19:13 +0100790 ENFORCE_SYSTEM_OR_ROOT;
Daniel Rosenbergd3992492018-10-02 17:40:44 -0700791 ACQUIRE_LOCK;
792
793 *_aidl_return = cp_needsRollback();
Yurii Zubrytskyi131365a2020-03-24 23:49:02 -0700794 return Ok();
Daniel Rosenbergd3992492018-10-02 17:40:44 -0700795}
796
Daniel Rosenberg4f684712018-08-28 01:58:49 -0700797binder::Status VoldNativeService::needsCheckpoint(bool* _aidl_return) {
Nikita Ioffe5d85bf32019-10-23 17:19:13 +0100798 ENFORCE_SYSTEM_OR_ROOT;
Daniel Rosenberg4f684712018-08-28 01:58:49 -0700799 ACQUIRE_LOCK;
800
801 *_aidl_return = cp_needsCheckpoint();
Yurii Zubrytskyi131365a2020-03-24 23:49:02 -0700802 return Ok();
Daniel Rosenberg4f684712018-08-28 01:58:49 -0700803}
804
Daniel Rosenberge334aab2018-10-10 18:52:04 -0700805binder::Status VoldNativeService::commitChanges() {
Nikita Ioffe5d85bf32019-10-23 17:19:13 +0100806 ENFORCE_SYSTEM_OR_ROOT;
Daniel Rosenberg4f684712018-08-28 01:58:49 -0700807 ACQUIRE_LOCK;
808
Daniel Rosenberge334aab2018-10-10 18:52:04 -0700809 return cp_commitChanges();
Daniel Rosenberg4f684712018-08-28 01:58:49 -0700810}
811
Daniel Rosenberge334aab2018-10-10 18:52:04 -0700812binder::Status VoldNativeService::prepareCheckpoint() {
Nikita Ioffe5d85bf32019-10-23 17:19:13 +0100813 ENFORCE_SYSTEM_OR_ROOT;
Daniel Rosenberg4f684712018-08-28 01:58:49 -0700814 ACQUIRE_LOCK;
815
Daniel Rosenberge334aab2018-10-10 18:52:04 -0700816 return cp_prepareCheckpoint();
Daniel Rosenberg4f684712018-08-28 01:58:49 -0700817}
818
Daniel Rosenberge334aab2018-10-10 18:52:04 -0700819binder::Status VoldNativeService::restoreCheckpoint(const std::string& mountPoint) {
Nikita Ioffe5d85bf32019-10-23 17:19:13 +0100820 ENFORCE_SYSTEM_OR_ROOT;
Paul Lawrence1abb2fe2018-09-21 10:49:57 -0700821 CHECK_ARGUMENT_PATH(mountPoint);
822 ACQUIRE_LOCK;
823
Daniel Rosenberge334aab2018-10-10 18:52:04 -0700824 return cp_restoreCheckpoint(mountPoint);
Paul Lawrence1abb2fe2018-09-21 10:49:57 -0700825}
826
Daniel Rosenbergdda59812019-03-06 17:45:17 -0800827binder::Status VoldNativeService::restoreCheckpointPart(const std::string& mountPoint, int count) {
Nikita Ioffe5d85bf32019-10-23 17:19:13 +0100828 ENFORCE_SYSTEM_OR_ROOT;
Daniel Rosenbergdda59812019-03-06 17:45:17 -0800829 CHECK_ARGUMENT_PATH(mountPoint);
830 ACQUIRE_LOCK;
831
832 return cp_restoreCheckpoint(mountPoint, count);
833}
834
Daniel Rosenberge334aab2018-10-10 18:52:04 -0700835binder::Status VoldNativeService::markBootAttempt() {
Nikita Ioffe5d85bf32019-10-23 17:19:13 +0100836 ENFORCE_SYSTEM_OR_ROOT;
Daniel Rosenberg4f684712018-08-28 01:58:49 -0700837 ACQUIRE_LOCK;
838
Daniel Rosenberge334aab2018-10-10 18:52:04 -0700839 return cp_markBootAttempt();
Daniel Rosenberg4f684712018-08-28 01:58:49 -0700840}
841
Daniel Rosenberga59e4392019-03-20 17:02:47 -0700842binder::Status VoldNativeService::abortChanges(const std::string& message, bool retry) {
Nikita Ioffe5d85bf32019-10-23 17:19:13 +0100843 ENFORCE_SYSTEM_OR_ROOT;
Daniel Rosenberg4f684712018-08-28 01:58:49 -0700844 ACQUIRE_LOCK;
845
Daniel Rosenberga59e4392019-03-20 17:02:47 -0700846 cp_abortChanges(message, retry);
Yurii Zubrytskyi131365a2020-03-24 23:49:02 -0700847 return Ok();
Daniel Rosenberg4f684712018-08-28 01:58:49 -0700848}
849
Daniel Rosenberg9b667fb2019-01-22 17:27:25 -0800850binder::Status VoldNativeService::supportsCheckpoint(bool* _aidl_return) {
Nikita Ioffe5d85bf32019-10-23 17:19:13 +0100851 ENFORCE_SYSTEM_OR_ROOT;
Daniel Rosenberg9b667fb2019-01-22 17:27:25 -0800852 ACQUIRE_LOCK;
853
854 return cp_supportsCheckpoint(*_aidl_return);
855}
856
Paul Lawrencec5c79c52019-03-18 13:36:40 -0700857binder::Status VoldNativeService::supportsBlockCheckpoint(bool* _aidl_return) {
Nikita Ioffe5d85bf32019-10-23 17:19:13 +0100858 ENFORCE_SYSTEM_OR_ROOT;
Paul Lawrencec5c79c52019-03-18 13:36:40 -0700859 ACQUIRE_LOCK;
860
861 return cp_supportsBlockCheckpoint(*_aidl_return);
862}
863
864binder::Status VoldNativeService::supportsFileCheckpoint(bool* _aidl_return) {
Nikita Ioffe5d85bf32019-10-23 17:19:13 +0100865 ENFORCE_SYSTEM_OR_ROOT;
Paul Lawrencec5c79c52019-03-18 13:36:40 -0700866 ACQUIRE_LOCK;
867
868 return cp_supportsFileCheckpoint(*_aidl_return);
869}
870
Nikita Ioffea5798fc2019-10-11 16:38:21 +0100871binder::Status VoldNativeService::resetCheckpoint() {
Nikita Ioffe5d85bf32019-10-23 17:19:13 +0100872 ENFORCE_SYSTEM_OR_ROOT;
Nikita Ioffea5798fc2019-10-11 16:38:21 +0100873 ACQUIRE_LOCK;
874
875 cp_resetCheckpoint();
Yurii Zubrytskyi131365a2020-03-24 23:49:02 -0700876 return Ok();
Nikita Ioffea5798fc2019-10-11 16:38:21 +0100877}
878
Yurii Zubrytskyi3497cb52020-01-10 11:54:06 -0800879binder::Status VoldNativeService::incFsEnabled(bool* _aidl_return) {
Yurii Zubrytskyi131365a2020-03-24 23:49:02 -0700880 ENFORCE_SYSTEM_OR_ROOT;
881
Yurii Zubrytskyi3497cb52020-01-10 11:54:06 -0800882 *_aidl_return = IncFs_IsEnabled();
Yurii Zubrytskyi131365a2020-03-24 23:49:02 -0700883 return Ok();
Songchun Fanab55cec2019-12-02 10:50:12 -0800884}
885
886binder::Status VoldNativeService::mountIncFs(
Yurii Zubrytskyi3497cb52020-01-10 11:54:06 -0800887 const std::string& backingPath, const std::string& targetDir, int32_t flags,
Songchun Fanab55cec2019-12-02 10:50:12 -0800888 ::android::os::incremental::IncrementalFileSystemControlParcel* _aidl_return) {
Yurii Zubrytskyi131365a2020-03-24 23:49:02 -0700889 ENFORCE_SYSTEM_OR_ROOT;
890 CHECK_ARGUMENT_PATH(backingPath);
891 CHECK_ARGUMENT_PATH(targetDir);
892
Yurii Zubrytskyi3497cb52020-01-10 11:54:06 -0800893 auto result = IncFs_Mount(backingPath.c_str(), targetDir.c_str(),
894 {.flags = IncFsMountFlags(flags),
895 .defaultReadTimeoutMs = INCFS_DEFAULT_READ_TIMEOUT_MS,
896 .readLogBufferPages = 4});
897 if (result.cmd < 0) {
898 return translate(result.cmd);
Songchun Fanab55cec2019-12-02 10:50:12 -0800899 }
Songchun Fanab55cec2019-12-02 10:50:12 -0800900 using unique_fd = ::android::base::unique_fd;
Yurii Zubrytskyi3497cb52020-01-10 11:54:06 -0800901 _aidl_return->cmd.reset(unique_fd(result.cmd));
902 _aidl_return->pendingReads.reset(unique_fd(result.pendingReads));
903 if (result.logs >= 0) {
904 _aidl_return->log.reset(unique_fd(result.logs));
Songchun Fanab55cec2019-12-02 10:50:12 -0800905 }
Yurii Zubrytskyi131365a2020-03-24 23:49:02 -0700906 return Ok();
Songchun Fanab55cec2019-12-02 10:50:12 -0800907}
908
909binder::Status VoldNativeService::unmountIncFs(const std::string& dir) {
Yurii Zubrytskyi131365a2020-03-24 23:49:02 -0700910 ENFORCE_SYSTEM_OR_ROOT;
911 CHECK_ARGUMENT_PATH(dir);
912
Songchun Fanab55cec2019-12-02 10:50:12 -0800913 return translate(IncFs_Unmount(dir.c_str()));
914}
915
916binder::Status VoldNativeService::bindMount(const std::string& sourceDir,
917 const std::string& targetDir) {
Yurii Zubrytskyi131365a2020-03-24 23:49:02 -0700918 ENFORCE_SYSTEM_OR_ROOT;
919 CHECK_ARGUMENT_PATH(sourceDir);
920 CHECK_ARGUMENT_PATH(targetDir);
921
Songchun Fanab55cec2019-12-02 10:50:12 -0800922 return translate(IncFs_BindMount(sourceDir.c_str(), targetDir.c_str()));
923}
924
Jeff Sharkey068c6be2017-09-06 13:47:40 -0600925} // namespace vold
926} // namespace android